Snippet A nice math function...

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,494
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:

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" :p


Just because it's fun, here's an online version that runs in your browser:
Formula finder
 

SerraAvenger

Cuz I can
Reaction score
234
Pretty cool idea (even though having to test the map just to get the values would be annoying :p)

Exactly. Doing this in JASS is like trying to teach a squirrel fly.

Also, wouldn't this require to have the graph of the damage increase have a certain shape?
The line is constant!
Don't make it too easy for yourself. That was just "randomly" true ( actually I believe you designed the sequence like that :p ).
You can't draw a 3rd degree polynomial through ALL sets of points.


So, why not ( Damien will hate me for this :evilgrin: ) do an executable of it with something easy like Python2Exe ^.-
Python comes here quite handy; You don't need no call for MAXXXXEFFICIENCYY!!!!, so you also needn't call for Perl ( oO ).
Anyway, just choose a language that
+ knows regex ( for an input like a;b;c;d )
+ can handle higher degree polynomials
+ has a lightweight + nice GUI toolkit ( like PythonQT for python )
+ dynamically allocates arrays

And code a similiar function there. Perhaps I'll do it some sleepless weekend :p
Best regards, Davey
 

DrEvil

FCRI Associate!
Reaction score
111
If i was to use this system ( which i probably wont )

How would i get [ real y1 , real y2 and real y3 ] ?

JASS:
function solve takes real y1,real y2,real y3 returns nothing


Sorry to ask im just trying to get used to using JASS functions taking stuff in [ Somehow , i dont know ]

Thanks for answering {[( In future post's )]}
 

DrEvil

FCRI Associate!
Reaction score
111
So
Code:
call solve (5, 6, 8)

Would call the function solve giving ?
real y1 = 5
real y2 = 6
real y3 = 8

EDIT : call solve ( 5, 6, 8) { Would be a gui custom script ? Calling frm a spell cast maybe ? }
 

DrEvil

FCRI Associate!
Reaction score
111
Nice system you got there Ace :thup:

Yes , Thanks for helping me understand Flare .

Offtopic

And for other functions i could use like getting x,y coordinates and returning for a spell ?
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Almost pointless post?

Basic Derivatives, or algebra, depending on how you look at it :p:

damage = a * level * level + b * level + c

ax² + bx + c is the basic quadratic equation.

You basically wrote a guide on how to get a formula when you already know the intervals. Basically, it's finding the derivative of an equation you don't know.

The reverse derivative (being able to find the output from any level):

X is the level.
Y is the damage amount.
Y' is the difference of damage between one level and the next.
Y'' is how much Y' changes every level.

Y'' = c __ c is a constant number. Since Acehart solved the difference in difference in damage (Y'') to being 1, we can just plug in 1 for c.

Y' = cx + d __ d is an unknown constant. In this case, you can substitute 3, 4, 5, 6 for difference in damage (Y'), making d become 2.

Y = c x²/2 + dx + e __ e is an unknown constant. in this case, we can plug in 5, 8, 12, 17, 23 for damage (Y) and solve for e.

c = 1 :
Y'' = 1
Y' = x + d
Y = X²/2 + dx + e

(1) + d = 3

c = 1, d = 2:

Y'' = 1
Y' = x + 2
Y = X²/2 + 2x + e

(1)²/2 + 2(1) + e = 5

c = 1, d = 2, e = 2.5

Y'' = 1
Y' = x + 2
Y = X²/2 + 2x + 2.5

Now you can calculate the amount of damage dealt to any level using Y = X²/2 + 2x + 3

*Work in Progress Post, the formula is not quite working out as I intended. Sorry to post it incomplete, but I have a class to be at. :S*

See? Calculus is useful! :p
 
Reaction score
456
(The title of the thread fights against all those title writing rules.)

Why did you let this drop? It's awesome!

(you should return to the chat, so I could bomb you at my random questions)
 

UndeadDragon

Super Moderator
Reaction score
447
Wow, that formula finder is useful :thup:
 

Furby

Current occupation: News poster
Reaction score
144
Just an idea

What about some function like this:

Code:
...
set value = GetValue(a,b,c,level)
...


which takes a,b,c like in your first post and returns value for level taken in function?

I think it will be more helpfull like this because if i want to find formula I will go to some page with the Formula Finder. I think no one will use it by map if he know where the formula finder is. :rolleyes:
 

Larcenist

REP: Respect, Envy, Prosperity?
Reaction score
211
Another interesting thing would be to know the equations for the x^3 and x^4 coefficients in the formula finder (since I'm too lazy to figure them out myself).
 

emjlr3

Change can be a good thing
Reaction score
395
you sure that thing works..
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top