Tuesday, May 17, 2011

Calculating XP

There are thousands of games that have some form of experience bar in them. You level your items/cards/monsters/abilities/players/characters... but what's amusing is that when you go to create your own leveling system, something as simple as calculating XP is a little harder than I had expected.

My first attempt at it was some simple thing that I drafted in excel. It scaled very poorly. Then I thought maybe it was exponential growth. HA! I realized very quickly that leveling to higher levels would take astronomical amounts of time.

But surely I don't need to recreate the wheel. That's when I hit up google. I was sure SOMEWHERE someone would have a formula for this. Sure enough, they're out there, and they're not nearly so simple as I had expected.

But for a game, you're going to need two formulas... one to convert from level to XP value, and another to convert from XP value to level.

I settled on the following using Excel formula notation (since that's where I drafted it):  The original I believe comes from dungeons and dragons, but this is slightly modified.

To convert from level to XP :
 =100*(B8+FACT(B8)/(FACT($C$4)*FACT(B8-$C$4))) where B8 is the level-1 and C4 is a constant (in this case 2)

To convert from XP value to level:
=(1+SQRT(H7/125+1))/2 where H7 is the amount of experience
In Corona (lua) it's relatively trivial to turn these into functions, with the exception of calculating factorial where you'll have to roll your own since it's not part of math.lua. And to convert XP to level, you'll have to use math.floor to round down to the nearest level unless you want your player to be level 5.2349542.

So many little things you take for granted when you're on the game-playing side of the fence that when you have to make it yourself... you sit there scratching your head wondering... HOW did they do that?  Thank goodness for google.  I really don't know how programmers did it back in the day... low processing power, insanely little memory, no object oriented programming, no libraries, no google.  Mad props to them.



No comments:

Post a Comment