seany123 Posted September 22, 2008 Share Posted September 22, 2008 Im making a game and i need help with one of the parts of the game. im doing a crime mod which basically gives people exp and takes away 'nerve'.. that works fine, but obviously because i havnt coded it yet, it suceeds everytime. this is the code: <?php include("lib.php"); define("PAGENAME", "Badge Car"); $player = check_user($secret_key, $db); $training[] = "you Badged a Car!"; if ($_GET['act'] == "go") { if ($player->nerve <= 0) { include("templates/private_header.php"); echo "You don't have enough nerve left!"; include("templates/private_footer.php"); exit; } $expgain = 10; //Random EXP gains $trainkey = array_rand($training); //Random training message $output = $training[$trainkey]; if ($expgain + $player->exp >= $player->maxexp) //Player gained a level! { //Update player, gained a level $output .= "<br /><br />You gained <b>" . $expgain . "</b> EXP and level up!"; $newexp = $expgain + $player->exp - $player->maxexp; $query = $db->execute("update `players` set `stat_points`=?, `level`=?, `maxexp`=?, `maxhp`=?, `exp`=?, `gold`=?, `hp`=?, `nerve`=? where `id`=?", array($player->stat_points + 3, $player->level + 1, ($player->level+1) * 70 - 20, $player->maxhp + 30, $newexp, $player->nerve - 1, $player->id)); } else { //Update player $output .= "<br /><br />You gained <b>" . $expgain . "</b> EXP!"; $query = $db->execute("update `players` set `exp`=?, `nerve`=? where `id`=?", array($player->exp + $expgain, $player->nerve - 1, $player->id)); } $player = check_user($secret_key, $db); include("templates/private_header.php"); echo $output; include("templates/private_footer.php"); exit; } include("templates/private_header.php"); echo "So you've some for some training, huh?<br /><br />\n"; echo "<a href=\"crime1.php?act=go\">Train!</a>\n"; include("templates/private_footer.php"); ?> instead of whats above I want there to be a Chance of Failure... wait for it (based on another value 'awake'. I want there to be a 80% chance of success if Awake is 100% - 75% full. 60% chance if awake 74% - 50% 40% chance if awake 49% - 25% 20% chance if awake 24% - 0% I know this is very confusing but can it be done? ~ Sean Link to comment https://forums.phpfreaks.com/topic/125363-working-with-percentages-help/ Share on other sites More sharing options...
Minase Posted September 22, 2008 Share Posted September 22, 2008 yes and is actually preety easy here is an example $awake = 1000; // lets say if ( $awake < (($awake / 100) * 25)) { $chance = 0; } elseif ( $awake < (($awake / 100) * 50)) { $chance = 25; } elseif ( $awake < (($awake / 100) * 75)) { $chance = 50; } elseif ( $awake <= (($awake / 100) * 100)) { $chance = 80; } Link to comment https://forums.phpfreaks.com/topic/125363-working-with-percentages-help/#findComment-648095 Share on other sites More sharing options...
discomatt Posted September 22, 2008 Share Posted September 22, 2008 <?php if( $awake >= 75 ) $max = 80; elseif( $awake >= 50 ) $max = 60; elseif( $awake >= 25 ) $max = 40; else $max = 20; $rand = mt_rand(1,100); if ( $rand <= $max ) echo 'Accomplished'; else echo 'Failed'; ?> Link to comment https://forums.phpfreaks.com/topic/125363-working-with-percentages-help/#findComment-648097 Share on other sites More sharing options...
seany123 Posted September 22, 2008 Author Share Posted September 22, 2008 Big problem with the codes you have provided is.... 'Awake' Changes. Link to comment https://forums.phpfreaks.com/topic/125363-working-with-percentages-help/#findComment-648104 Share on other sites More sharing options...
Minase Posted September 22, 2008 Share Posted September 22, 2008 yes lool ) you can make something like this // START OF LOL $awake = $your_variable; // END OF LOL Link to comment https://forums.phpfreaks.com/topic/125363-working-with-percentages-help/#findComment-648109 Share on other sites More sharing options...
corbin Posted September 22, 2008 Share Posted September 22, 2008 Edit: Bleh went afk half way through typing this..... Posting it anyway. No offense, but if you're making an OOP game, this shouldn't be very confusing. Think about what you want to do. You want to find the probability of something based on a seeded value. So, first you need to decide which seed to use. If the Awake value is 100-75, you want a 80% chance, else if... so on.... So: if($awake >= .75) $chance = 80; elseif($awake >= .5) $chance = 60; elseif($awake >= .25) $chance = 40; else $chance = 20; if(rand(1, 100) <= $chance) { //I could've seen this part being confusing.... Kind of ghetto. echo 'Success!'; } else { echo 'Failure'; } Link to comment https://forums.phpfreaks.com/topic/125363-working-with-percentages-help/#findComment-648111 Share on other sites More sharing options...
Yesideez Posted September 22, 2008 Share Posted September 22, 2008 Corbin, that's pretty much how I did it. seany123, I've written something similar - if you want the source PM me an email address and I'll send the lot to you, it's about 3.5MB zipped or you can have the ENTIRE source including graphics (Photoshop files etc.) and it's 19MB... There's more than enough source there for you to learn by. EDIT: I can upload the zip to my web space and PM you the link maybe? Link to comment https://forums.phpfreaks.com/topic/125363-working-with-percentages-help/#findComment-648144 Share on other sites More sharing options...
discomatt Posted September 22, 2008 Share Posted September 22, 2008 if($awake >= .75) $chance = 80; elseif($awake >= .5) $chance = 60; elseif($awake >= .25) $chance = 40; else $chance = 20; if(rand(1, 100) <= $chance) { //I could've seen this part being confusing.... Kind of ghetto. echo 'Success!'; } else { echo 'Failure'; } <?php if( $awake >= 75 ) $max = 80; elseif( $awake >= 50 ) $max = 60; elseif( $awake >= 25 ) $max = 40; else $max = 20; $rand = mt_rand(1,100); if ( $rand <= $max ) echo 'Accomplished'; else echo 'Failed'; ?> Deja vu Link to comment https://forums.phpfreaks.com/topic/125363-working-with-percentages-help/#findComment-648156 Share on other sites More sharing options...
Yesideez Posted September 22, 2008 Share Posted September 22, 2008 OK I have to go to bed. The game isn't online any more, no idea what happened because I left the team a long time ago. This was my first site using PHP and MySQL and I wrote 90% of what is in the archive. http://www.pictureinthesky.net/files/gangstersonline.zip The size is 19Mb. I did find this little review: http://www.reviewcentre.com/reviews97610.html There is a fair bit of "bad practice" going on there but hopefully someone can make use of it instead of sitting on my PC wasting away. I've also started writing another - will gladly surrender the source if wanted but will have to upload tomorrow when I get home from work. I'm only giving this away now that the website itself is offline. Link to comment https://forums.phpfreaks.com/topic/125363-working-with-percentages-help/#findComment-648184 Share on other sites More sharing options...
seany123 Posted September 23, 2008 Author Share Posted September 23, 2008 yeah i get exactly how its done now... just wondering though, how would i implement that into the script above? Link to comment https://forums.phpfreaks.com/topic/125363-working-with-percentages-help/#findComment-648948 Share on other sites More sharing options...
seany123 Posted September 23, 2008 Author Share Posted September 23, 2008 and i guess to make it go to prison id do... if(rand(1, 100) >= $chance) { then prison code Link to comment https://forums.phpfreaks.com/topic/125363-working-with-percentages-help/#findComment-648968 Share on other sites More sharing options...
seany123 Posted September 23, 2008 Author Share Posted September 23, 2008 But how would i put it into the code i gave? Link to comment https://forums.phpfreaks.com/topic/125363-working-with-percentages-help/#findComment-648988 Share on other sites More sharing options...
seany123 Posted September 23, 2008 Author Share Posted September 23, 2008 Anyone able to give me a hand putting these codes together? Link to comment https://forums.phpfreaks.com/topic/125363-working-with-percentages-help/#findComment-649058 Share on other sites More sharing options...
discomatt Posted September 23, 2008 Share Posted September 23, 2008 Spamming yourself up to the top isn't going to get you any help. On top of that, I don't think you're asking for help... you're kinda asking someone to do it for you. We gave you working examples... it's up to you to figure out where they have to go. If you're really not sure, you've gotta start understanding your code better. If it's not your code, you're in the wrong section. Link to comment https://forums.phpfreaks.com/topic/125363-working-with-percentages-help/#findComment-649067 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.