Author Topic: Calculating Level  (Read 642 times)

Offline Waizujin

  • Level 15
  • *
  • Posts: 132
  • Reputation: +1/-0
    • View Profile
Calculating Level
« on: April 07, 2011, 10:46:27 PM »
I am creating a game with a plethora of skills such as mining, woodcutting, fishing, etc.

I need to calculate the users level based off of a formula. I am using the below code, and I would like to know if you think that it is efficent and will it give me the proper level?

Code: [Select]
function grabLevel($exp) {
for($level = 1; (pow($level, 3) * 20) < $exp; $level++) {
return $level;
}
}

The experience formula is Level^3 * 20

So level 5 would require 2,500 Experience in that skill. All skills use the same level formula.

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Calculating Level
« Reply #1 on: April 08, 2011, 05:06:47 AM »
better to have a formula than a cycle
Code: [Select]
<?php
function getLevel$exp ) {
    return 
floor(pow(($exp/20), 1/3));
}
edit: added floor()
« Last Edit: April 08, 2011, 07:32:52 AM by Nox »
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Waizujin

  • Level 15
  • *
  • Posts: 132
  • Reputation: +1/-0
    • View Profile
Re: Calculating Level
« Reply #2 on: April 08, 2011, 12:24:33 PM »
So that will give the same result?

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Calculating Level
« Reply #3 on: April 08, 2011, 01:05:16 PM »
well you can try it for yourself
Code: [Select]
<?php
echo getLevel(0)."<br>\n";
echo 
getLevel(1)."<br>\n";
echo 
getLevel(25)."<br>\n";
echo 
getLevel(79)."<br>\n";
echo 
getLevel(1000)."<br>\n";
echo 
getLevel(2499)."<br>\n";
echo 
getLevel(2500)."<br>\n";
echo 
getLevel(2501)."<br>\n";
// ...

** in other words **

it should, it should be exactly reversed formula as you provided
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Waizujin

  • Level 15
  • *
  • Posts: 132
  • Reputation: +1/-0
    • View Profile
Re: Calculating Level
« Reply #4 on: April 08, 2011, 01:22:31 PM »
That's amazing! Thanks! :D I suck at math so I would have never figured it out. XD

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal