1

OK, using the answers over at this question I was able to produce the following code to calculated the experience needed to level on a text-based RPG I am working on. I'll say from the get-go that my math skills are not great, so please "dumb it down" as much as you can.

function calcExp($L) {
    if($L <= 5) { 
        //If Level 5 or less then just 12 exp per level
        return 12*$L;
    }
    if($L > 1000) {
        //If over level 1,000, we need to slow them down
        //add an additional 500k per level required
        $calc = $L - 1000;
        $c2 = 500000*$calc;
        return (5*$L*$L-5*$L)+$c2;
    }
    else {
        //otherwise, calculate as follows
        $exp = 5 * $L * $L - 5 * $L;
        return $exp;
    }
}

That returns the correct amount of experience, now I'm trying to figure out how to reverse the process so that I can checkLvl($EXPERIENCE) and have it return the level it should be at.

So then if I were to

$level = 10;
$exp = 450;
$check = checkLvl($exp);
if($check != $level) { die('Unknown Error'); }
else { echo "Success!"; }

Note: the above code is just me trying to be clear.

I'm at a loss as to how to reverse the little mess I created.

Community
  • 1
  • 1
Solstice
  • 23
  • 3
  • Wow, a downvote within seconds. Any particular reason why? – Solstice Aug 14 '14 at 14:41
  • What have you tried for you checkLvl method? Could you take the math for calcExp, and solve an algebra equation? – Brian J Aug 14 '14 at 14:42
  • @BrianJ - according to the answers I linked something like floor(12 + sqrt(12 * 12 - 4 * 12 * (-$exp) ))/ 12 should reverse it, but doesn't even come close; throw in the modifications I made to < 5 and > 1000 & I'm totally lost as to how to reverse it. – Solstice Aug 14 '14 at 14:46
  • @Solstice - if I understand what you're looking for... You just need to reverse engineer the conditions you've got. So you need one check to see if the XP is 60 or less; one to see if it's over (er) 4,995,000; then just do the appropriate calculation. – andrewsi Aug 14 '14 at 14:55
  • @andrewsi - Yup, I was over-complicating it. Too much thinking, not enough sleep lol Thanks! – Solstice Aug 14 '14 at 15:33

1 Answers1

0

Your first five levels are only tweleve exp per level, so you could reverse and say the first 60 exp (5 * 12) should be floor($exp / 12)

Your second level range is from 6 to 1000. So if the exp is greater than 60, but less than whatever exp you need for level 1000, you'd solve $exp = 5 * $L * $L - 5 * $L for $L, which is a quadratic equation. It would look something like $L = (5 + sqrt(25 - 4 * 5 * -$exp)) / (2 * 5)

Everything else above level 1000 should solve a similar quadratic equation, $exp = (5 * $L * $L - 5 * $L) + 500000 * ($L - 1000) for $L. It would look like $L = ((5 + 500000) + sqrt((5 + 500000)^2 - 4 * 5 * (500000 - $exp))) / (2 * 5)

Brian J
  • 694
  • 1
  • 21
  • 34
  • In other words, I've over-complicated the hell out of it, had the answer and was too tired to see it. Thanks, between a fresh cup of coffee & your logic here I got it. *hangs head in shame* – Solstice Aug 14 '14 at 15:32
  • Sometimes all it takes is a fresh set of eyes. The last step would be to figure out if you're solving for a level in the range of 6 to 1000, and I didn't feel like finding out how much exp is required for level 1000. – Brian J Aug 14 '14 at 15:37
  • I wrote a `while()` loop to do it for me lol In the end I just ended up having `checkLvl($current_experience,$current_level)` just run the `calcExp($current_level)` and see if it's within range or not; if it's more exp than needed, level up...occam's razor at it's finest. – Solstice Aug 14 '14 at 15:43