• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Solved] Formula for calculating experience needed from level x to level y

Status
Not open for further replies.
Level 19
Joined
Aug 8, 2007
Messages
2,765
tittle. say x = 2 y = 5, how do i know how much exp is needed to get from 2 to 5
 
I'll get you the formula in a sec.
But beware, level 6553 and beyond have negative experience (Because of those incompetent coders at Bizzard)

edit
Damn it >_>
I found a new Warcraft III bug.
Setting the level of a level 1 hero to 1 makes him level 2 >_>
gg Blizzard.

edit
Ok, the values:
1 -> 200
2 -> 500
3 -> 900
4 -> 1400
5 -> 2000

Basically, to get the xp required for the next level, they start at 200, add 0 and 200, increase the 200 to 300, add to get 500, increase by 300 to 400, add to get 900, etc...

The formula should be:
(level + 1) * 100 + prevLevelXp

Which gets turned into a recursive algorithm >:O

If you have low levels (like 10 or 20 in your map), it would be fine to use this... but then again, why wouldn't it be fine to cache the amounts required yourself in such cases? ._.
If you have values like 100, this recursive algorithm is going to be costly :/
100 function calls...
NEVERMIND. You can use a loop.

It's going to look like this:

JASS:
function GetRequiredXP takes integer level returns integer
    local integer xp = 0
    loop
        exitwhen level == 0
        set xp = xp + (level + 1) * 100
        set level = level - 1
    endloop
    return xp
endfunction

Now, to get the differences, you could do this:
GetRequiredXP(5) - GetRequiredXP(2)

Or, for more optimizations, you could use this:

JASS:
function GetRequiredXPDifference takes integer level1, integer level2 returns integer
    local integer xp = 0
    loop
        exitwhen level1 == level2
        set xp = xp + (level1 + 1) * 100
        set level1 = level1 - 1
    endloop
    return xp
endfunction

edit
All of this assumes you haven't touched any of the XP-config stuff in the "Gameplay Constants"

edit
GetRequiredXPDifference is optimized, so it's going to crash if level1 is less than level2.
You can use checks for that :>

JASS:
if int1 >= int2 then
    set xp = GetRequiredXPDifference(int1, int2)
else
    set xp = GetRequiredXPDifference(int2, int1)
endif
 
Last edited:
Status
Not open for further replies.
Top