LOUDMOUTH Posted June 19, 2009 Share Posted June 19, 2009 I have this code for a EXP system in a game. The way it is right now it is gaining exp to fast per level. So by the time you get to level 40 the exp is far too great. I am looking for some help with a correct Algorithm to match this Ratio. I have tried changing the $a += floor($x+1500*pow(4, ($x/7))); many times to different equations and have no luck so far at solving this. Anyone have any suggestions? I want level 1 to start off with 450 exp, and per level, increase by around 450. with no max EXP The below code equals this ratio //level 2 - 500 //level 3 - 1500 //level 4 - 3500 //level 5 - 6000 I am looking for this ratio //level 2 - 988 //level 3 - 1482 //level 4 - 1976 //level 5 - 2471 function experience($L) { $a=0; $end=0; for($x=1; $x<$L; $x++) { $a += floor($x+1500*pow(4, ($x/7))); } return floor($a/4); } function Get_The_Level($exp) { $a=0; $end =0; for($x=1; ($end==0 && $x<100); $x++) { $a += floor($x+1500*pow(4, ($x/7))); if ($exp >= floor($a/4)){ } else { return $x; $end=1; } } } function Get_Max_Exp($L){ $end=0; if ($exp == 0){ return 457; $end =1; } for($L=1;($L<100 && $end==0);$L++) { $exp = experience($L); //echo $exp; if ($exp >= $user_class->exp){ return $exp; $end=1; } } } Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/ Share on other sites More sharing options...
LOUDMOUTH Posted June 19, 2009 Author Share Posted June 19, 2009 ??? Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-859480 Share on other sites More sharing options...
Mark Baker Posted June 19, 2009 Share Posted June 19, 2009 function experience($L) { return $L * 495; } function Get_The_Level($exp) { return floor($exp / 495) + 1; } Then go read some basic mathematics books Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-859491 Share on other sites More sharing options...
LOUDMOUTH Posted June 19, 2009 Author Share Posted June 19, 2009 Thanks for the help. When I get to level 2 I still have the exp from the prior level towards the next level though. this is what It shows right after I got to level 2 EXP: 500 / 1485 [33%] How can I make it EXP: 0/1485 [0%] when you get to level 2 Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-859510 Share on other sites More sharing options...
Mark Baker Posted June 19, 2009 Share Posted June 19, 2009 where you're doing the echo to display experience, sonmething like: echo 'EXP: '.$exp.' / '.experience($L+1); change it to something like echo 'EXP: '.($exp - experience($L)).' / '.experience($L+1); Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-859539 Share on other sites More sharing options...
LOUDMOUTH Posted June 19, 2009 Author Share Posted June 19, 2009 This echo 'EXP: '.($exp - experience($L)).' / '.experience($L+1); is showing 0/495 for every level now Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-859549 Share on other sites More sharing options...
LOUDMOUTH Posted June 19, 2009 Author Share Posted June 19, 2009 Also this is in the same file as the EXP code $this->exp = $worked['exp']; $this->level = Get_The_Level($this->exp); $this->maxexp = experience($this->level +1); $this->exppercent = ($this->exp == 0) ? 0 : floor(($this->exp / $this->maxexp) * 100); $this->formattedexp = $this->exp." / ".$this->maxexp." [".$this->exppercent."%]"; Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-859550 Share on other sites More sharing options...
Daniel0 Posted June 19, 2009 Share Posted June 19, 2009 Maybe what you're looking for is a function that grows logarithmically? Logarithmic growth "decreases" as the independent variable grows. Or rather, the derivative decreases if you know calculus. What this practically means in your case is that higher levels would need more exp to level up than lower levels would. I assume that's the kind of behavior your looking for. I've attached a graph to give you an idea of how logarithmic growth is. Specifically, that graphed function is the natural logarithm. I've also attached a graph of the function you're using now. It's essentially linear. The numbers you posted in your first post can, with good approximation, be both linear, quadratic, exponential and power growth (probably others as well, I stopped checking after that), so it's a bit difficult giving you a good formula without knowing more about it. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-859559 Share on other sites More sharing options...
LOUDMOUTH Posted June 19, 2009 Author Share Posted June 19, 2009 Yes I need a logarithmically based EXP system. Like you said Daniel, It would be nice to have the players gradually have to gain more exp as the Level # gets higher. Math isnt my strong point, so any input you may have would help a ton right now I have a Classes.php that has the first codes I posted here. Then I have it echo the EXP in the Index.php here is everything I have in the classes.php page that has to do with the EXP. If I can get that behaving properly then echo is cake. function experience($L) { $a=0; $end=0; for($x=1; $x<$L; $x++) { $a += floor($x+1500*pow(4, ($x/7))); } return floor($a/4); } function Get_The_Level($exp) { $a=0; $end =0; for($x=1; ($end==0 && $x<100); $x++) { $a += floor($x+1500*pow(4, ($x/7))); if ($exp >= floor($a/4)){ } else { return $x; $end=1; } } } function Get_Max_Exp($L){ $end=0; if ($exp == 0){ return 457; $end =1; } for($L=1;($L<100 && $end==0);$L++) { $exp = experience($L); //echo $exp; if ($exp >= $user_class->exp){ return $exp; $end=1; } } } Then further down the page is this $this->exp = $worked['exp']; $this->level = Get_The_Level($this->exp); $this->maxexp = experience($this->level +1); $this->exppercent = ($this->exp == 0) ? 0 : floor(($this->exp / $this->maxexp) * 100); $this->formattedexp = $this->exp." / ".$this->maxexp." [".$this->exppercent."%]"; <td width='35%'><?php echo $user_class->formattedexp; ?></td> Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-859568 Share on other sites More sharing options...
Mark Baker Posted June 19, 2009 Share Posted June 19, 2009 echo 'EXP: '.($this->exp - experience($this->level)).' / '.experience($this->level+1); Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-859580 Share on other sites More sharing options...
Daniel0 Posted June 19, 2009 Share Posted June 19, 2009 Maybe a function like this: [tex]f(x) = \mbox{floor}\left( \left(\frac{\log_{10}\left(\frac{x}{200}\right)}{0.3}\right)^{1.12} \right)+1[/tex] Or in php: function getLevel($exp) { return floor(pow(log10($exp) / 200, 1.12)) + 1; } That function will grow as in the attached graph. It will not be the same numbers as you requested, but the levels would be like this: Level 2: 400 Level 3: 722 Level 4: 1263 Level 5: 2165 Level 6: 3660 Level 7: 6119 Level 8: 10135 [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-859582 Share on other sites More sharing options...
Daniel0 Posted June 19, 2009 Share Posted June 19, 2009 Actually, maybe that's not a very good function. 1,000,000 exp would then be level 17. I don't know how hard you want to make or how much exp you hand out, but it's a bit high. You can try to tinker with the function yourself perhaps. Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-859590 Share on other sites More sharing options...
LOUDMOUTH Posted June 19, 2009 Author Share Posted June 19, 2009 I am looking for something along these lines Level/Exp 1=494 2=988 3=1,482 4=1,976 5=2,471 6=2,965 7=3,459 8=3,953 9=4,447 Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-859595 Share on other sites More sharing options...
Daniel0 Posted June 19, 2009 Share Posted June 19, 2009 Maybe you could give us some higher level requirements as well (not necessarily still in steps of 1)? As you see with the function I proposed, it look fine (IMO) on lower levels, but it turned out to quickly get out of hand as x grew. Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-859599 Share on other sites More sharing options...
LOUDMOUTH Posted June 19, 2009 Author Share Posted June 19, 2009 I made a sample for you here http://pledgeresistance.com/expguidesample.html But as you can see in the sample it kinda gets messy when you get close to 500, I was thinking if the system was Infinite then there would be no need to make the higher levels such a great amount of exp, what do you think? Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-859602 Share on other sites More sharing options...
LOUDMOUTH Posted June 20, 2009 Author Share Posted June 20, 2009 Using this I was able to get the EXP to set 495 exp per level function experience($L) { return $L * 495; } function Get_The_Level($exp) { return floor($exp / 495) + 1; } This echo is returning 0/495 EXP at all times now, echo 'EXP: '.($exp - experience($L)).' / '.experience($L+1); I just need their total exp subtracted from the exp they have. If I can get the echo to work properly I think we have got it, any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-859974 Share on other sites More sharing options...
Mark Baker Posted June 20, 2009 Share Posted June 20, 2009 I just need their total exp subtracted from the exp they have. If I can get the echo to work properly I think we have got it, any suggestions? Look to reply #9 Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-860080 Share on other sites More sharing options...
LOUDMOUTH Posted June 20, 2009 Author Share Posted June 20, 2009 I tried echo 'EXP: '.($this->exp - experience($this->level)).' / '.experience($this->level+1); I got a syntax error Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-860115 Share on other sites More sharing options...
Mark Baker Posted June 20, 2009 Share Posted June 20, 2009 I tried echo 'EXP: '.($this->exp - experience($this->level)).' / '.experience($this->level+1); I got a syntax error OK, give us something to work with. I'm having to guess the variable names that you're using, and have no idea whether they exist, or they're in scope. What error are you getting? Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-860119 Share on other sites More sharing options...
LOUDMOUTH Posted June 20, 2009 Author Share Posted June 20, 2009 Fatal error: Using $this when not in object context in /index.php on line 44 Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-860129 Share on other sites More sharing options...
Mark Baker Posted June 20, 2009 Share Posted June 20, 2009 Fatal error: Using $this when not in object context in /index.php on line 44So now we're back to guess the variable name Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-860350 Share on other sites More sharing options...
LOUDMOUTH Posted June 21, 2009 Author Share Posted June 21, 2009 This is showing 0/495 EXP no matter how much exp I gain right now echo 'EXP: '.($exp - experience($L)).' / '.experience($L+1); This one show syntax error echo 'EXP: '.($this->exp - experience($this->level)).' / '.experience($this->level+1); This one shows 495/1485 EXP <--close but I need it to set the exp to 0 towards next Levels' EXP requirement when you gain a level. <?php echo $user_class->formattedexp; ?></td> This is everything on the classes.php that has the exp codes in it. //level 2 - 500 //level 3 - 1500 //level 4 - 3500 //level 5 - 6000 function experience($L) { $a=0; $end=0; for($x=1; $x<$L; $x++) { $a += floor($x+1500*pow(4, ($x/7))); } return floor($a/4); } function Get_The_Level($exp) { $a=0; $end =0; for($x=1; ($end==0 && $x<100); $x++) { $a += floor($x+1500*pow(4, ($x/7))); if ($exp >= floor($a/4)){ } else { return $x; $end=1; } } } function Get_Max_Exp($L){ $end=0; if ($exp == 0){ return 457; $end =1; } for($L=1;($L<100 && $end==0);$L++) { $exp = experience($L); //echo $exp; if ($exp >= $user_class->exp){ return $exp; $end=1; } } } class User { $this->exp = $worked['exp']; $this->level = Get_The_Level($this->exp); $this->maxexp = experience($this->level +1); $this->exppercent = ($this->exp == 0) ? 0 : floor(($this->exp / $this->maxexp) * 100); $this->formattedexp = $this->exp." / ".$this->maxexp." [".$this->exppercent."%]"; Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-860460 Share on other sites More sharing options...
LOUDMOUTH Posted June 25, 2009 Author Share Posted June 25, 2009 Also view here if you are looking to make some money doing some PHP help http://www.phpfreaks.com/forums/index.php/topic,258134.0.html Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-863588 Share on other sites More sharing options...
LOUDMOUTH Posted June 25, 2009 Author Share Posted June 25, 2009 This is working great to give me 495 exp per Level, but I need it to also add the prior level's exp to the new goal exp. Meaning if you are Level 1 you have 0/495 exp and when you are level 2 you have 0/900 exp, Level 3 you would have 0/1350 exp. function experience($L) { return $L * 495; } function Get_The_Level($exp) { return floor($exp / 495) + 1; } can anyone help get this right? Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-863778 Share on other sites More sharing options...
sasa Posted June 26, 2009 Share Posted June 26, 2009 try <?php function my_level($exp){ $per_lev = 495; $cur_lev = 0; while ($exp >= $cur_lev * $per_lev){ $exp -= $cur_lev * $per_lev; $cur_lev++; } return array('lev' => $cur_lev, 'exp' => $exp, 'for_next_lev' => $cur_lev * $per_lev); } $x = my_level(1500); echo 'you are on level ', $x['lev'], ', ', $x['exp'], ' / ', $x['for_next_lev'] , ' (',round($x['exp'] / $x['for_next_lev'] *100, 2), '%)'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/162876-solved-php-math-problem/#findComment-863919 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.