# Help calculate this easyer, or more efficient way

This topic is 1755 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello.

Reason:

Currently i am balancing the game i am working on.

What i want to achieve:

i am trying to find a formula that will be more efficient then this peace of code

/* Example values
int calcRT = 0;
int statRT = 9;
*/
int tax = 0;
for(unsigned int n = 0; n < unit.statRT; n++)

{

unit.calcRT -= 100 - tax;

tax += 5;

}


What i want is for each statRT to reduce calcRT by 100, but each one after that to give by 5 less so.

If statRT = 1, calcRT = -100;(100)

if statRT = 2, calcRT = -195;(100 + 95)

If statRT = 3, calcRT = -285;(100 + 95 + 90)

if statRT = 10, calcRT = - ;(100 + 95 + 90... + 65 + 60 + 55)

What syntax i am looking for

/* Example values
int calcRT = 0;
int statRT = 9;
*/
calcRT = statRT * (100 - (5 * statRT));


##### Share on other sites

calcRT = max(statRT - 1, 0)*5-statRT*100

perhaps

EDIT:

You might want to set an upper bound for statRT inside the max, because else it would go like ...+ 65 + 60 + 55... + 5 + 0 -5 which you probably dont want to happen.

EDIT2:

Oh wait that doesnt seem to work... :c

Edited by Waterlimon

##### Share on other sites
Are you looking for a formula that gives the same results?
calcRT = -100 * statRT + 5 * statRT * (statRT - 1) / 2;
Edited by Álvaro

##### Share on other sites

Are you looking for a formula that gives the same results?

calcRT = -100 * statRT + 5 * statRT * (statRT - 1) / 2;

Well that reproduces the values / value i am looking for.

Much respect, thank you for this.

calcRT = max(statRT - 1, 0)*5-statRT*100

perhaps

EDIT:

You might want to set an upper bound for statRT inside the max, because else it would go like ...+ 65 + 60 + 55... + 5 + 0 -5 which you probably dont want to happen.

EDIT2:

Oh wait that doesnt seem to work...

Its ... not ... correct

Edited by BaneTrapper

##### Share on other sites

Use Alvaro's formula (although watch out for the behaviour when statRT is bigger than 20, may want to cap it instead of going negative).

The pattern is -100, -200 + 5, -300 + (5 + 10), -400 + (5 + 10 + 15), ...

which is -100 * statRT + 5 * (Sum (0 to (statRT-1)))

And since Sum(0 to X) is 0.5 * X * (X+1), Sum(0 to (startRT-1)) = 0.5 * statRT * (statRT - 1)

which gives Alvaro's formula

EDIT: You probably don't want to cap anything when statRT is > 20, since the formula gives a nice (parabolic) curve.

##### Share on other sites

And here's something about how the maths for the sum works...

What is the area of an N * (N+1) rectangle?

XXXXXXX

XXXXXXX

XXXXXXX

XXXXXXX

XXXXXXX

XXXXXXX

if N = 6, area is 6 * 7 = 42.

What use is that?

What is the area of this shape?

X

XX

XXX

XXXX

XXXXX

XXXXXX

Answer: if you rotate a copy of it 180 degrees and line it up it forms a 7 * 6 rectangle (using O for the copy of the shape)

XOOOOOO
XXOOOOO
XXXOOOO
XXXXOOO
XXXXXOO
XXXXXXO


Which works for any size N, so the area of the shape is 0.5 * N * (N+1)

##### Share on other sites

If your step and limits are fixed, couldn't you simply use a look-up table? Just calculate it once and you're done.

##### Share on other sites

Lookup table vs. simple formula? Simple formula for the win...

##### Share on other sites

Lookup table vs. simple formula? Simple formula for the win...

I am not sure. If you can make the table visible to a game designer, he can tweak it any way he wants.

##### Share on other sites

I suppose...

If I was living in some magical world in which designers actually tweaked values ;) (And not, hey that ends with a 7, that doesn't look neat, make it end with a zero instead, for no apparent reason).

Anyway you gave the correct answer and I explained some of the maths so we both win! Would probably help if we knew what the values were meant to represent in the game.

##### Share on other sites

Lookup table vs. simple formula? Simple formula for the win...

I am not sure. If you can make the table visible to a game designer, he can tweak it any way he wants.

Also it would allot bigger depth for the system.

But, it would also require more time to manage witch i lack sadly.