Help with leveling system (RPG)

0

Hello, I'm trying to build a leveling system (like an RPG). So far I have this:

Math.floor(1000 * Math.pow(level, 1.5F));

Here I enter the level, and I get the EXP needed to reach it. But now I need to do the opposite, I need to enter the EXP and get the current level. I'm really bad with math, does anyone have my solution?

    
asked by anonymous 17.02.2018 / 19:18

1 answer

2

Let's start with this method:

public static int levelToExp(int level) {
    return (int) Math.floor(1000 * Math.pow(level, 1.5));
}

This formula takes the number level , raises to 1.5, multiplies by 1000, and rounds.

The inverse would be, ignoring the rounding at the end, divide by 1000 and raise to the inverse of 1.5.

What is the inverse of 1.5? Well, considering that 1.5 = 3/2, then the inverse is 2/3.

So the formula would look something like this:

Math.pow(exp / 1000.0, 2 / 3.0);

Why similar ? Why we still have to deal with the rounding and with the errors that he introduces. For example:

class TesteExp {
    public static void main(String[] args) {
        int level = 2;
        int exp = (int) Math.floor(1000 * Math.pow(level, 1.5));
        System.out.println(exp);
        double level2 = Math.pow(exp / 1000.0, 2 / 3.0);
        System.out.println(level2);
        System.out.println(Math.floor(level2));
    }
}

Here's the output ( see here working on ideone ):

2828
1.9997986463957098
1.0

In other words, to reach level 2, you must have 2828 EXP points. But by inverting the equation, we get to level 1.9997986463957098, which we round down would give level 1, not 2. This is because the required EXP would be a number between 2828 and 2829, but Math.floor rounded it down .

If instead of rounding the level down, we were rounding up, it would not work either because in case the EXP amount is 2827, it would be at level 2, rather than level 1.

So we do not know yet if we should round up or down. What can we do? Kick both forms and see which of the corresponding levels it serves:

public static int expToLevel(int exp) {
    double level = Math.pow(exp / 1000.0, 2 / 3.0);
    int a = (int) Math.floor(level);
    int b = (int) Math.ceil(level);
    return levelToExp(b) > exp ? a : b;
}

Let's test this whole code:

public static void main(String[] args) {
    for (int i = 0; i <= 20; i++) {
        int exp = levelToExp(i);
        System.out.print("Para o level " + i + " precisamos de " + exp + " EXP. ");
        int provaReal = expToLevel(exp);
        int antes = expToLevel(exp - 1);
        int depois = expToLevel(exp + 1);
        System.out.print((exp - 1) + " EXP = Level " + antes + ". ");
        System.out.print(exp + " EXP = Level " + provaReal + ". ");
        System.out.print((exp + 1) + " EXP = Level " + depois + ".");
        System.out.println();
    }
}

Here's the output ( See here working on ideone ):

Para o level 0 precisamos de 0 EXP. -1 EXP = Level 0. 0 EXP = Level 0. 1 EXP = Level 0.
Para o level 1 precisamos de 1000 EXP. 999 EXP = Level 0. 1000 EXP = Level 1. 1001 EXP = Level 1.
Para o level 2 precisamos de 2828 EXP. 2827 EXP = Level 1. 2828 EXP = Level 2. 2829 EXP = Level 2.
Para o level 3 precisamos de 5196 EXP. 5195 EXP = Level 2. 5196 EXP = Level 3. 5197 EXP = Level 3.
Para o level 4 precisamos de 8000 EXP. 7999 EXP = Level 3. 8000 EXP = Level 4. 8001 EXP = Level 4.
Para o level 5 precisamos de 11180 EXP. 11179 EXP = Level 4. 11180 EXP = Level 5. 11181 EXP = Level 5.
Para o level 6 precisamos de 14696 EXP. 14695 EXP = Level 5. 14696 EXP = Level 6. 14697 EXP = Level 6.
Para o level 7 precisamos de 18520 EXP. 18519 EXP = Level 6. 18520 EXP = Level 7. 18521 EXP = Level 7.
Para o level 8 precisamos de 22627 EXP. 22626 EXP = Level 7. 22627 EXP = Level 8. 22628 EXP = Level 8.
Para o level 9 precisamos de 27000 EXP. 26999 EXP = Level 8. 27000 EXP = Level 9. 27001 EXP = Level 9.
Para o level 10 precisamos de 31622 EXP. 31621 EXP = Level 9. 31622 EXP = Level 10. 31623 EXP = Level 10.
Para o level 11 precisamos de 36482 EXP. 36481 EXP = Level 10. 36482 EXP = Level 11. 36483 EXP = Level 11.
Para o level 12 precisamos de 41569 EXP. 41568 EXP = Level 11. 41569 EXP = Level 12. 41570 EXP = Level 12.
Para o level 13 precisamos de 46872 EXP. 46871 EXP = Level 12. 46872 EXP = Level 13. 46873 EXP = Level 13.
Para o level 14 precisamos de 52383 EXP. 52382 EXP = Level 13. 52383 EXP = Level 14. 52384 EXP = Level 14.
Para o level 15 precisamos de 58094 EXP. 58093 EXP = Level 14. 58094 EXP = Level 15. 58095 EXP = Level 15.
Para o level 16 precisamos de 64000 EXP. 63999 EXP = Level 15. 64000 EXP = Level 16. 64001 EXP = Level 16.
Para o level 17 precisamos de 70092 EXP. 70091 EXP = Level 16. 70092 EXP = Level 17. 70093 EXP = Level 17.
Para o level 18 precisamos de 76367 EXP. 76366 EXP = Level 17. 76367 EXP = Level 18. 76368 EXP = Level 18.
Para o level 19 precisamos de 82819 EXP. 82818 EXP = Level 18. 82819 EXP = Level 19. 82820 EXP = Level 19.
Para o level 20 precisamos de 89442 EXP. 89441 EXP = Level 19. 89442 EXP = Level 20. 89443 EXP = Level 20.

Note that the output matches what you expect. With one less EXP point, it drops to the previous level. With an extra EXP point, it remains at the same level.

    
17.02.2018 / 21:07