Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php game, formula to calculate a level based on exp

Im making a browser based PHP game and in my database for the players it has a record of that players total EXP or experience.

What i need is a formula to translate that exp into a level or rank, out of 100.

So they start off at level 1, and when they hit say, 50 exp, go to level 2, then when they hit maybe 125/150, level 2.

Basically a formula that steadily makes each level longer (more exp)

Can anyone help? I'm not very good at maths :P

like image 265
user880356 Avatar asked Aug 05 '11 10:08

user880356


3 Answers

Many formulas may suit your needs, depending on how fast you want the required exp to go up.

In fact, you really should make this configurable (or at least easily changed in one central location), so that you can balance the game later. In most games these (and other) formulas are determined only after playtesting and trying out several options.

Here's one formula: First level-up happens at 50 exp; second at 150exp; third at 300 exp; fourth at 500 exp; etc. In other words, first you have to gather 50 exp, then 100 exp, then 150exp, etc. It's an Arithmetic Progression.

For levelup X then you need 25*X*(1+X) exp.

Added: To get it the other way round you just use basic math. Like this:

y=25*X*(1+X)
0=25*X*X+25*X-y

That's a standard Quadratic equation, and you can solve for X with:

X = (-25±sqrt(625+100y))/50

Now, since we want both X and Y to be greater than 0, we can drop one of the answers and are left with:

X = (sqrt(625+100y)-25)/50

So, for example, if we have 300 exp, we see that:

(sqrt(625+100*300)-25)/50 = (sqrt(30625)-25)/50 = (175-25)/50 = 150/50 = 3

Now, this is the 3rd levelup, so that means level 4.

like image 63
Vilx- Avatar answered Oct 31 '22 01:10

Vilx-


If you wanted the following:

  • Level 1 @ 0 points
  • Level 2 @ 50 points
  • Level 3 @ 150 points
  • Level 4 @ 300 points
  • Level 5 @ 500 points etc.

An equation relating experience (X) with level (L) is:

X = 25 * L * L - 25 * L

To calculate the level for a given experience use the quadratic equation to get:

L = (25 + sqrt(25 * 25 - 4 * 25 * (-X) ))/ (2 * 25)

This simplifies to:

L = (25 + sqrt(625 + 100 * X)) / 50

Then round down using the floor function to get your final formula:

L = floor(25 + sqrt(625 + 100 * X)) / 50

Where L is the level, and X is the experience points

like image 42
Mike Avatar answered Oct 31 '22 00:10

Mike


It really depends on how you want the exp to scale for each level. Let's say

LvL1 : 50 Xp
Lvl2: LvL1*2=100Xp
LvL3: LvL2*2=200Xp
Lvl4: LvL3*2=400Xp

This means you have a geometric progression The Xp required to complete level n would be

`XPn=base*Q^(n-1)`

In my example base is the inital 50 xp and Q is 2 (ratio).

Provided a player starts at lvl1 with no xp:

when he dings lvl2 he would have 50 total Xp
at  lvl3 150xp
at  lvl4 350xp

and so forth The total xp a player has when he gets a new level up would be:

 base*(Q^n-1)/(Q-1)

In your case you already know how much xp the player has. For a ratio of 2 the formula gets simpler:

base * (2^n-1)=total xp at level n

to find out the level for a given xp amount all you need to do is apply a simple formula

$playerLevel=floor(log($playerXp/50+1,2));

But with a geometric progression it will get harder and harder and harder for players to level.

To display the XP required for next level you can just calculate total XP for next level.

$totalXpNextLevel=50*(pow(2,$playerLevel+1)-1);
$reqXp=$totalXpNextLevel - $playerXp;

Check start of the post: to get from lvl1 -> lvl2 you need 50 xp lvl2 ->lvl3 100xp

to get from lvl x to lvl(x+1) you would need

$totalXprequired=50*pow(2,$playerLevel-1);
like image 6
Sleeperson Avatar answered Oct 31 '22 00:10

Sleeperson