1
\$\begingroup\$

So I've been checking some games to get ideas for my own. Some designs I've reviewed rely on a few variables like:

  • Current level
  • Current exp
  • Exp to next level

I aim for a simplistic design where there is only an exp variable.

Example:

Level 1

Current exp: 0

Exp to next level: 100

Level 2

Current exp: 100

Exp to next level: 200

Level 3

Current exp: 200

Exp to next level: 400

Level 4

Current exp: 400

Exp to next level: 800

Every level, the exp required for next level doubles.

That's okay. Since I use only an exp variable, I need two formulas:

  1. Formula to calculate the current level
  2. Formula to calculate the exp required for next level

My progress so far:

Level = floor((exp + 100)/100)

This works for 0 exp (level 1) because floor(0 + 100)/100 equals 1, with 50 exp works fine too:

50 + 100 = 150 150/100 1.5 flooring 1,5 gets us to 1

If we hit 100 exp, we should be now at level 2.

floor((100+100)/100) = 2

Let's see level three.

floor((100 + 200)/100) = 3

Since level 3 is at least 200 exp, this thing keeps working.

Level 4 begins with 400 exp.

Let's see what happens if we have 399 exp. 399 + 100 499/100 = 4.9 floor(4.9) 4

No way. 399 should still be level 3.

So let's take into account the doubling factor.

Level = floor((exp + 100)/100)/2

399 + 100 = 499... / 100 = 4.99 .. floored -> 4 / 2 = 2

Level 2? No way , it should be level 3

Another try:

Level = floor((exp/100))

floor(399/100) = 3 floor(400/100) = 4

But if we apply the formula to level 2(starting at 100, ending at 200 which would be level 3)

floor(100/100) = 1

I can't seem to achieve this on my own. Any suggestions?

\$\endgroup\$
  • \$\begingroup\$ Please visit this page to merge your accounts, you'll be able to comment and accept the answer. \$\endgroup\$ – Vaillancourt Jul 23 '17 at 11:43
2
\$\begingroup\$

Because your EXP amount is doubling, simple division isn't enough. You have exponents, and you need to use their inverse, the logarithm function.

Calculating the Amount Needed for the Next Level

If you're on level n, the amount of experience points to get to level n+1 is 100 * 2^(n-1). (eg. if n is 1, you get 100; if n is 3, you get 400.)

If the notation isn't familiar to you, 2^(n-1) means "two to the power of (n minus one)." And of course, 2^0 = 1.

Calculating the Current Level

If you invert the formula and solve for n instead of solving for exp, you get:

1 + log(exp/100) / log(2)

or

        log(exp/100)
   1 +  ------------
        log(2)

exp is the current number of experience points; log is the logarithm function.

If I plug in in 376 for exp, I get a value of 2.91. Rounding down, the player is on level 2, which is correct.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.