SaMz Posted June 14, 2012 Posted June 14, 2012 Right, im having a problem with high level users gaining negative Exp when they attack a user. I cant find a way to resolve this. Really messes up the users account and gives unlimited levels and have to reset their exp and level back to mormal. My Attacktake.php exp line: $qe=$r['level']*$r['level']*$r['level']; $expgain=rand($qe/2,$qe); $expperc=(int) ($expgain/$ir['exp_needed']*100); print "and gained $expperc% EXP!<br /> Any help will be appriciated. If someone come out with a fix i will send them $15 through paypal. Quote
risingsparky Posted June 15, 2012 Posted June 15, 2012 (edited) Re: http://stackoverflow.com/questions/1587799/why-would-rand-return-a-negative-value-when-min-and-max-values-are-positive Simple fix, change this: $expgain=rand($qe/2,$qe); to this: $expgain = $qe / 100 * rand( 50, 100 ); Edited June 15, 2012 by risingsparky Quote
SaMz Posted June 15, 2012 Author Posted June 15, 2012 Thanks! I will test that out and see if the problem still persists. I have another major problem that once a user reaches over level 1620 they get negative exp and gain levels everytime they refresh...I cant seem to find out why this happens. Quote
lucky3809 Posted June 15, 2012 Posted June 15, 2012 Easy solution is to refresh the page that is doing it, make sure what that page is, then look over your coding, at calculations and queries, or post the page on here for someone to look at for you, if you are lost. If it's not in that page it could very well be the check_level function in global_func.php that is causing it... Quote
SaMz Posted June 15, 2012 Author Posted June 15, 2012 Easy solution is to refresh the page that is doing it, make sure what that page is, then look over your coding, at calculations and queries, or post the page on here for someone to look at for you, if you are lost. If it's not in that page it could very well be the check_level function in global_func.php that is causing it... I cannot find the problem, Once you reach the level 1624-5 it starts giving negative exp and automatically levels up. exp line in header.php $experc=(int) ($ir['exp']/$ir['exp_needed']*100); check_level global func.php function check_level() { global $db; global $ir,$c,$userid; $ir['exp_needed']=(int) (($ir['level']+2)*($ir['level']+2)*($ir['level']+2)*0.5); if($ir['exp'] >= $ir['exp_needed']) { $expu=$ir['exp']-$ir['exp_needed']; $ir['level']+=1; $ir['exp']=$expu; $ir['brave']+=2; $ir['maxbrave']+=2; $ir['hp']+=50; $ir['maxhp']+=50; $ir['exp_needed']=(int) (($ir['level']+2)*($ir['level']+2)*($ir['level']+2)*0.5); $db->query("UPDATE users SET level=level+1,exp=$expu,brave=brave+2,maxbrave=maxbrave+2, hp=hp+50,maxhp=maxhp+50,mlevel=mlevel+1 where userid=$userid"); } } Quote
lucky3809 Posted June 16, 2012 Posted June 16, 2012 (edited) I looked at what you have posted it has nothing to do with any of it post your attacktake.php, you must have a query incorrect causing it to go negative or, you have a statement that is less than or greater than a certain level or it could be your fields in your database set incorrectly.. Edited June 16, 2012 by lucky3809 Quote
SaMz Posted June 16, 2012 Author Posted June 16, 2012 I looked at what you have posted it has nothing to do with any of it post your attacktake.php, you must have a query incorrect causing it to go negative or, you have a statement that is less than or greater than a certain level or it could be your fields in your database set incorrectly.. It has nothing to do with attack take anymore. It just happens. Quote
Dabomstew Posted June 17, 2012 Posted June 17, 2012 Hi samz, This happens because the EXP needed to advance past level 1624 is a number greater than 2147483647. So on this line: $ir['exp_needed']=(int) (($ir['level']+2)*($ir['level']+2)*($ir['level']+2)*0.5); When the (int) typecast occurs, it tries to change a number bigger than 2147483647 into an integer, which can only be 2147483647 at most on most servers. So it wraps back around to a negative number, causing your problem. Possible solutions: * Don't use ints for storing/calculating EXP - floats can hold bigger numbers * Reduce EXP requirements per level by adjusting the numbers in the formula * Put on a level cap, since levels 1600+ are pretty ridiculous to start with. Quote
SaMz Posted June 20, 2012 Author Posted June 20, 2012 Hi samz, This happens because the EXP needed to advance past level 1624 is a number greater than 2147483647. So on this line: $ir['exp_needed']=(int) (($ir['level']+2)*($ir['level']+2)*($ir['level']+2)*0.5); When the (int) typecast occurs, it tries to change a number bigger than 2147483647 into an integer, which can only be 2147483647 at most on most servers. So it wraps back around to a negative number, causing your problem. Possible solutions: * Don't use ints for storing/calculating EXP - floats can hold bigger numbers * Reduce EXP requirements per level by adjusting the numbers in the formula * Put on a level cap, since levels 1600+ are pretty ridiculous to start with. Thanks, thats what i thaught... Can someone help me with how to implement a level cap? Quote
rulerofzu Posted June 20, 2012 Posted June 20, 2012 Hi samz, This happens because the EXP needed to advance past level 1624 is a number greater than 2147483647. So on this line: $ir['exp_needed']=(int) (($ir['level']+2)*($ir['level']+2)*($ir['level']+2)*0.5); When the (int) typecast occurs, it tries to change a number bigger than 2147483647 into an integer, which can only be 2147483647 at most on most servers. So it wraps back around to a negative number, causing your problem. Possible solutions: * Don't use ints for storing/calculating EXP - floats can hold bigger numbers * Reduce EXP requirements per level by adjusting the numbers in the formula * Put on a level cap, since levels 1600+ are pretty ridiculous to start with. Hahaha really Dabs? 64bit os its been around for a long time now? you not heard of it?? Alternatively you could simply change this and use a exp table where you set your own rate of what exp the player is required to obtain before they level up. As for the level 1600+ being ridiculous....well players like something to do. Gaining levels gives them something to do. So if i set a incentive on a game to be the first player to, for example, get to level 2000 and then players spend rl $ to be able to achieve it then thats game play and good business management. Believe me it works....seen it. Have done it. Its a good part of any game. Quote
newttster Posted June 22, 2012 Posted June 22, 2012 Alternatively you could simply change this and use a exp table where you set your own rate of what exp the player is required to obtain before they level up. How would you do something like that? Could you not do that with a function as well? If level=1 and exp =75 then level =2 etc. That kind of thing. Quote
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.