Help calculate this easyer, or more efficient way

Started by
9 comments, last by BaneTrapper 11 years ago

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));
Advertisement

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

o3o

Are you looking for a formula that gives the same results?
calcRT = -100 * statRT + 5 * statRT * (statRT - 1) / 2;

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

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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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)

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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

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

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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.

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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement