AceHart
Your Friendly Neighborhood Admin
- Reaction score
- 1,495
Well, "nice"... no idea what to call this.
or if there even is any kind of need...
Anyway, ever wanted something like
do 5 damage on level 1,
do 8 damage on level 2,
do 12 damage on level 3,
do 17 damage on level 4,
...
Only to sit there and try to come up with a formula instead of "if level == 1 then ..."?
So,
5, 8, 12, 17, 23
take the differences from one to the next:
3, 4, 5, 6
take the differences from one to the next:
1, 1, 1
The line is constant!
Given it was the second, the most common case,
our damage can be calculated with this formula:
damage = a * level * level + b * level + c
But... what are a, b and c?
That's where this function comes to the rescue:
Examples:
call solve(5, 8, 12) (always call with the expected values on levels 1 to 3)
Which will say
0.5 * level * level + 1.5 * level + 3
Or,
call solve(10, 18, 34):
4 * level * level - 4 * level + 10
Of course, this could be improved by telling it to simplify
4 * level * level - 4 * level + 10
to
4 * level * (level - 1) + 10
but, then again, that would be the famous "exercise for the interested reader"
Just because it's fun, here's an online version that runs in your browser:
Formula finder
or if there even is any kind of need...
Anyway, ever wanted something like
do 5 damage on level 1,
do 8 damage on level 2,
do 12 damage on level 3,
do 17 damage on level 4,
...
Only to sit there and try to come up with a formula instead of "if level == 1 then ..."?
So,
5, 8, 12, 17, 23
take the differences from one to the next:
3, 4, 5, 6
take the differences from one to the next:
1, 1, 1
The line is constant!
Given it was the second, the most common case,
our damage can be calculated with this formula:
damage = a * level * level + b * level + c
But... what are a, b and c?
That's where this function comes to the rescue:
JASS:
function solve takes real y1,real y2,real y3 returns nothing
local real a = 0.5 * (y3 - 2 * y2 + y1)
local real b = y2 - y1 - 3 * a
local real c = y1 - a - b
local string s1 = "+"
local string s2 = "+"
if b < 0 then
set s1 = "-"
set b = -b
endif
if c < 0 then
set s2 = "-"
set c = -c
endif
call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, R2S(a) + " * level * level " + s1 + " " + R2S(b) + " * level " + s2 + " " + R2S(c))
endfunction
Examples:
call solve(5, 8, 12) (always call with the expected values on levels 1 to 3)
Which will say
0.5 * level * level + 1.5 * level + 3
Or,
call solve(10, 18, 34):
4 * level * level - 4 * level + 10
Of course, this could be improved by telling it to simplify
4 * level * level - 4 * level + 10
to
4 * level * (level - 1) + 10
but, then again, that would be the famous "exercise for the interested reader"
Just because it's fun, here's an online version that runs in your browser:
Formula finder