How to determine the character’s level by gaining experience?

Asked

Viewed 220 times

6

I created this calculation based on multiples below to determine how much experience the character will need to level up.

float NeededExperience(int level, float experienceRatio = 100F)
{
    float neededExperience = ((level - 1) * experienceRatio) * level;

    if(neededExperience == 0F)
        neededExperience = experienceRatio;

    return neededExperience;
}

With this function, defining that the experienceRatio is equal to 100, and the user level is 1, he will need 200 experience to reach level 2 and 600 to level 3...

But I would like to do the opposite calculation, which calculates the level of the character from the amount of experience he has, for example: a user who has between 2000 and 2999 experience is at level 5.

Or if this is not the best way to calculate experience, I also accept suggestions.

1 answer

9


Your problem is not programming, but mathematics.

Your experiment calculation uses the following "formula":

float neededExperience = ((level - 1) * experienceRatio) * level;

Calling neededExperience of e, level of l and experienceRatio of r, This is mathematically equivalent to:

equação matemática

Therefore, algebraic manipulations can be used to isolate l in a second degree equation:

isolando l

Remembering your high school, you can use the Báscara formula to obtain the "solutions" (the roots) of a second-degree equation, using the terms of each "part" of the equation:

termos

Hence the value of l is calculated by making:

inserir a descrição da imagem aqui

In step 4 only the positive sign is maintained because the root value of the discriminating raiz do discriminante will almost always be greater than that 1, so that the equation has two solutions: a positive and one negative. But since there is no negative level, it does not the negative solution of the equation.

Note also that this mathematical function is continuous, so that it returns real values (broken, ie a float or double instead of a int). As in your case experience limits the levels above, in the implementation you should truncate the result to an integer (simply ignoring the decimals).

I implemented this equation in Excel to illustrate the answers obtained:

inserir a descrição da imagem aqui

Note that the formula that is in cell C8 is exactly the equation above. It has been copied to the other cells, and always references the experienceRatio fixed in cell D5.

In your case, to implement in C#, just do:

private static int CurrentLevelByExperience(float currentExperience, float experienceRatio = 100F){
    return Math.Truncate((1 + Math.sqrt(1 + 4*currentExperience/experienceRatio)) / 2);
}
  • With this method I could modify to also have the result in the opposite direction? passing as parameter the level and returning the necessary experience.

  • 1

    But you already do exactly that in its original function. There is nothing to "modify".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.