0
\$\begingroup\$

I am trying to create a formula to calculate the players level based on his XP alone. The game currently only increments his XP after certain actions.

I have the formula for calculating the required XP to level up:

var baseXP = 500;
var exponent = 1.04;
var requiredXP = Math.floor(baseXP + (baseXP * Math.pow(user.level - 1, exponent)));

The purpose of this formula is to increment the required level as so:

Level 1: 0-499xp
Level 2: 500-1527
Level 3: 1528-2066
Level 4: 2067-2613
Level 5: 2614-4165
etc...

I'm not sure if the formula is exactly what I need to use, and the exponent value is just arbitrary (incremented/decremented to make leveling take more/less time)

How would this formula be used or reversed to calculate the players current level. As a plus, any way to limit the maximum level to 100.

Thank you

\$\endgroup\$
  • \$\begingroup\$ You should probably ask the Math Q&A site: math.stackexchange.com I admit I started to find the math formula, but gave up when I realized I misread the question. \$\endgroup\$ – Olivier Grégoire Nov 23 '16 at 0:58
1
\$\begingroup\$

You do not need to have the inverse calculation at runtime at all. If you have "only" 100 possible levels, just store the amount of xp needed to level up and the according level in an array.

When you need to find out what level would result from a certain amount of xp at runtime, just search for the nearest lower value in your xp-level-array and there you have your result! The same goes for capping the possible levels at 100. When you need 1 Mio xp to get to level 100 and the player has 12 Mio, the nearest lower xp-value in your array is still 1 Mio, so the result is level 100.

\$\endgroup\$
0
\$\begingroup\$

Here is code for your question:

private static final double BASE_XP = 500;
private static final double EXPONENT = 1.04f;

public static void main(String args[]) {
    for (int i = 0; i < 10; i++) {
        double levelXp = calcXpForLevel(i);
        double max = calculateFullTargetXp(i);
        System.out.println(String.format("Level: %3d, xp for the next level: %10.2f, next level xp: %10.2f", i, levelXp, max));
    }
    System.out.println("====================");
    int xp = 14590;
    System.out.println("For " + xp + " xp  calculated level is " + calculateLevel(xp));
}

private static double calcXpForLevel(int level) {
    return BASE_XP + (BASE_XP * Math.pow(level, EXPONENT));
}

private static double calculateFullTargetXp(int level) {
    double requiredXP = 0;
    for (int i = 0; i <= level; i++) {
        requiredXP += calcXpForLevel(i);
    }
    return requiredXP;
}

private static int calculateLevel(double xp) {
    int level = 0;
    double maxXp = calcXpForLevel(0);
    do {
        maxXp += calcXpForLevel(++level);
    } while (maxXp < xp);
    return level;
}

output:

Level:   0, xp for the next level:     500.00, next level xp:     500.00
Level:   1, xp for the next level:    1000.00, next level xp:    1500.00
Level:   2, xp for the next level:    1528.11, next level xp:    3028.11
Level:   3, xp for the next level:    2067.39, next level xp:    5095.50
Level:   4, xp for the next level:    2614.04, next level xp:    7709.54
Level:   5, xp for the next level:    3166.24, next level xp:   10875.77
Level:   6, xp for the next level:    3722.90, next level xp:   14598.68
Level:   7, xp for the next level:    4283.31, next level xp:   18881.99
Level:   8, xp for the next level:    4846.94, next level xp:   23728.93
Level:   9, xp for the next level:    5413.40, next level xp:   29142.33
====================
For 14590 xp  calculated level is 6
\$\endgroup\$
  • 2
    \$\begingroup\$ Maybe you should explain what to do instead of just writing the code \$\endgroup\$ – realUser404 Nov 16 '16 at 12:33
  • \$\begingroup\$ @realUser404 I'm not an expert in math so this was the fastest and easiest solution which I can suggest. I've tried to find how to inverse his function XP=Base*SUM_Level(1+Exponent^Level) but unfortunately failed with that. So the decision was - make search level just calculating expected XP points step by step and compare with actual XP value. \$\endgroup\$ – ShchAlexander Nov 16 '16 at 15:15

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.