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

5

I created this calculation based on multiples below to determine how much the character will need to have experience 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, setting experienceRatio equal to 100, and user level is 1, it 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 it 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.

    
asked by anonymous 05.10.2016 / 20:31

1 answer

8

Your problem is not about programming, it's about math.

Your experience calculation uses the following "formula":

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

Calling neededExperience ,levelof and experienceRatio , this is mathematically equivalent to:

Soyoucanusealgebraicmanipulationstoisolate in a equation of the second degree:

Rememberingyourhighschooleducation,youcanusethe Báscara Formula to get the "solutions "(the roots) of a second-degree equation, using the terms of each" part "of the equation:

Sothevalueof iscalculatedbydoing:

  

Instep4onlythepositivesignismaintainedbecausetherootvalueofthe  discriminating willbealmostalwaysgreaterthan  that ,sothattheequationhastwosolutions:one  positiveandnegative.Butsincethereisnonegativelevel,itdoesnot  meaningthenegativesolutionoftheequation.

Notealsothatthismathfunctioniscontinuous,soitreturnsrealvalues(broken,thatis,floatordoubleinsteadofint).Asinyourcasetheexperiencelimitstheabovelevels,intheimplementationyoushouldtruncatetheresulttoaninteger(simplyignoringthedecimalplaces).

IhaveimplementedthisequationinExceltoillustratetheanswers:

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);
}
    
06.10.2016 / 04:35