0
\$\begingroup\$

How do I calculate the amount of XP for a level where the first level is 110, and each level after is 10% more than the last. Preferably to do without a loop because the levels will have to be infinite and will need to be quickly calculated.

in js using a loop:

var xptest=110;
var lastLevel = 110;
for (var level = 2; xptest <= Number.MAX_SAFE_INTEGER || level < 100; level++) {

    lastLevel*=1.1;
    lastLevel = Math.round(lastLevel *1.1)
    xptest+= lastLevel;

    console.log('LEVEL',level,'('+lastLevel+' / '+xptest+')');
}
\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

Let's work through some cases, given \$baseXP = 110\$ and \$increase = 1.1\$:

$$\begin{align} targetXP(1) &= baseXP\\ targetXP(2) &= baseXP + baseXP \cdot increase\\ targetXP(3) &= baseXP + baseXP \cdot increase + baseXP\cdot increase^2\\ ...\\ targetXP(n) &= baseXP + baseXP \cdot increase + ... + baseXP \cdot increase ^ {n-1}\\ \end{align}$$

If we multiply \$targetXP(n)\$ by \$increase\$, we find that all it does is shift the terms down one:

$$\begin{align}targetXP(n)&\cdot increase\\ &= baseXP \cdot increase + baseXP \cdot increase^2 + ...+ baseXP \cdot increase^n\end{align}$$

So if we subtract the original from this shifted version, all the terms except the first and last will cancel out, and we get...

$$\begin{align} targetXP(n) \cdot increase - targetXP(n) &= baseXP \cdot increase^n - baseXP\\ targetXP(n) \cdot (increase - 1) &= baseXP \cdot (increase^n - 1)\\ targetXP(n) &= baseXP \cdot \frac {1 - increase^n} {1 - increase}\end{align}$$

This is what's called a Geometric Series - you can read more about the math behind this here.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks, I don't entirely understand it, but it definitely works Math.round(110*((1-Math.pow(1.1,level))/(1-1.1)),0); \$\endgroup\$
    – stackers
    Sep 12, 2018 at 23:58
2
\$\begingroup\$

You're correct to want to use a technique which is more professional and elegant than a brute force loop! [;)

$$XP_{next} = XP_{initial} (1 + Rate_{level})^{Level_{current}}$$

This is the compound interest formula. "Rate_level" is rate per level.

So, you're level five and you want to know the XP for level six:

$$\begin{align} NextLevel = 110(1+0.10)^5\\ = 110(1.10)^5\\ = 110 \times 1.61051\\ = 177.1561\\ \end{align}$$

Rounded down is 177xp.

To find what level you're currently at based on your current experience points:

$$Level_{current} = log_{(1+Rate_{initial})}\left(\frac{XP_{current}}{XP_{initial}}\right)$$

10% is quite a steep progression curve, but it sounds like you know what you're doing and that you want it that way. If this were a D&D style RPG I would lean more toward 195% to 210% per level progression, which would imply a relatively limited number of levels.

Good luck.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .