xoligy Posted September 27, 2008 Share Posted September 27, 2008 Ok as some may of guessed im into online gaming now what i would like to do is make random events happen, of cause most the time nothing should happen bu every now and again i would like something to happen lol like the player gets a bonus of something, or his men die or he finds something lol The way i was thinking was that i would need some sort or array with the list of things that "could" happen with maybe a random factor. I would also want it so only one bonus at a time could happen, can someone give me some advise? Way i was thinking would be in a function using a random factor for the chance of something happening like 5-20% then the array of what could happen with another random factor hope that makes sense i know it dont to me lol Link to comment https://forums.phpfreaks.com/topic/126037-solved-random-events/ Share on other sites More sharing options...
beansandsausages Posted September 27, 2008 Share Posted September 27, 2008 i use some thid like this, $rand = rand(1,7); $r = rand(0,876)/1000; $r = number_format($r, 3); $stat = rand(1,; if($stat == 1) { $stat = strength; } elseif($stat == 2) {$stat = speed; } elseif($stat == 3) {$stat = magic; } elseif($stat == 4) {$stat = agility; } elseif($stat == 5) {$stat = speed; } elseif($stat == 6) {$stat = strength; } elseif($stat == 7) {$stat = agility; } elseif($stat == 9) {$stat = magic; } elseif($stat == {$stat = strength; } $color = rand(1,4); if($color == 1) { $color = red; } elseif($color == 2) { $color = yellow; } elseif($color == 3) { $color = blue; } elseif($color == 4) { $color = green; } if($rand == 1) { if($info['lab'] >= 1) { echo " He injected you, You feel fine. Noting happend. <br /><br /><a href=town.php />Back to town</a> | <a href=?mod=lab&pill=yes />Take another Injection</a> "; $update_messages = mysql_query("UPDATE `members` SET lab=lab-1 WHERE `id`='{$info['id']}'") or die(mysql_error()); ;} else { echo " He has no injections left for you. <br /><br /><a href=\"town.php\" />Back to town</a> ";} } elseif($rand == 2) { if($info['lab'] >= 1) { echo " He injected you, You feel fine. You gain <strong><font color=\"green\">{$r}</font></strong> in <strong><font color=\"green\">{$stat}</font></strong> <br /><br /><a href=town.php />Back to town</a> | <a href=?mod=lab&pill=yes />Take another Injection</a> "; $update_messages = mysql_query("UPDATE `members` SET {$stat}={$stat}+{$r}, lab=lab-1 WHERE `id`='{$info['id']}'") or die(mysql_error()); ;} else { echo " He has no injections left for you. <br /><br /><a href=\"town.php\" />Back to town</a> ";} } elseif($rand == 3) { if($info['lab'] >= 1) { echo " He injected you, You pass out due to extream pain as you wake you feel stronger some how, You gain <strong><font color=\"green\">{$r}</font></strong> in <strong><font color=\"green\">{$stat}</font></strong> <br /><br /><a href=town.php />Back to town</a> | <a href=?mod=lab&pill=yes />Take another Injection</a> "; $update_messages = mysql_query("UPDATE `members` SET {$stat}={$stat}+{$r}, lab=lab-1 WHERE `id`='{$info['id']}'") or die(mysql_error()); ;} else { echo " He has no injections left for you. <br /><br /><a href=\"town.php\" />Back to town</a> ";} } elseif($rand == 4) { if($info['lab'] >= 1) { echo " He injected you, You pass out due to extream pain, You lost <strong><font color=\"red\">-{$r}</font></strong> in <strong><font color=\"red\">{$stat}</font></strong> <br /><br /><a href=town.php />Back to town</a> | <a href=?mod=lab&pill=yes />Take another Injection</a> "; $update_messages = mysql_query("UPDATE `members` SET {$stat}={$stat}-{$r}, lab=lab-1 WHERE `id`='{$info['id']}'") or die(mysql_error()); ;} else { echo " He has no injections left for you. <br /><br /><a href=\"town.php\" />Back to town</a> ";} } elseif($rand == 5) { if($info['lab'] >= 1) { echo "You awake your body feels limp your eyes are all blured, You awake in the town square <meta http-equiv=\"refresh\" content=\"1;url=town.php\" /> "; $update_messages = mysql_query("UPDATE `members` SET lab=lab-1 WHERE `id`='{$info['id']}'") or die(mysql_error()); ;} else { echo " He has no injections left for you. <br /><br /><a href=\"town.php\" />Back to town</a> ";} } elseif($rand == 6) { if($info['lab'] >= 1) { echo " You can feel the power flowing throught your veins you gain <strong><font color=\"green\">{$r}</font></strong> in <strong><font color=\"green\">{$stat}</font></strong>, As you leave the lab you notice a <font color={$color}>{$color} gem</font> in your pocket. <br /><br /><a href=town.php />Back to town</a> | <a href=?mod=lab&pill=yes />Take another Injection</a> "; $update_messages = mysql_query("UPDATE `members` SET {$stat}={$stat}+{$r}, {$color}_gem={$color}_gem+1, lab=lab-1 WHERE `id`='{$info['id']}'") or die(mysql_error()); ;} else { echo " He has no injections left for you. <br /><br /><a href=\"town.php\" />Back to town</a> ";} } elseif($rand == 7) { if($info['lab'] >= 1) { echo "You lose <strong><font color=\"red\">-{$r}</font></strong> in <strong><font color=\"red\">{$stat}</font></strong> due to the injection. <br /><br /><a href=town.php />Back to town</a> | <a href=?mod=lab&pill=yes />Take another Injection</a> "; $update_messages = mysql_query("UPDATE `members` SET {$stat}={$stat}-{$r}, lab=lab-1 WHERE `id`='{$info['id']}'") or die(mysql_error()); ;} else { echo " He has no injections left for you. <br /><br /><a href=\"town.php\" />Back to town</a> ";} } there my be other ways to do it even easier ways but that works for me. Link to comment https://forums.phpfreaks.com/topic/126037-solved-random-events/#findComment-651776 Share on other sites More sharing options...
xoligy Posted September 27, 2008 Author Share Posted September 27, 2008 i was thinking it would be a load of if, elseif statments but wasnt sure lol tthanks for that may come in handy for a reference tho i know mine would have to be wrote alot different Link to comment https://forums.phpfreaks.com/topic/126037-solved-random-events/#findComment-651778 Share on other sites More sharing options...
beansandsausages Posted September 27, 2008 Share Posted September 27, 2008 np could always made a db table with them then pull them from the database by there id? Link to comment https://forums.phpfreaks.com/topic/126037-solved-random-events/#findComment-651779 Share on other sites More sharing options...
thebadbad Posted September 27, 2008 Share Posted September 27, 2008 This block: <?php if($stat == 1) { $stat = strength; } elseif($stat == 2) {$stat = speed; } elseif($stat == 3) {$stat = magic; } elseif($stat == 4) {$stat = agility; } elseif($stat == 5) {$stat = speed; } elseif($stat == 6) {$stat = strength; } elseif($stat == 7) {$stat = agility; } elseif($stat == 9) {$stat = magic; } elseif($stat == {$stat = strength; } ?> Can be rewritten into: <?php $powers = array('', 'strength', 'speed', 'magic', 'agility', 'speed', 'strength', 'agility', 'strength', 'magic'); if (in_array($stat, range(1,9))) { $stat = $powers[$stat]; } ?> Just to give you some ideas for optimizing. And instead of long if/elseif blocks, use switch(). Switch on true, if you want to check more than one variable. Example: <?php switch(true) { case ($a == 1 && $b == 2): //do something break; //etc. } ?> When switching on true, it may not take up less code, but it looks a lot cleaner to me. Link to comment https://forums.phpfreaks.com/topic/126037-solved-random-events/#findComment-651797 Share on other sites More sharing options...
xoligy Posted September 27, 2008 Author Share Posted September 27, 2008 For the way i have to do it, it be something like rand factor for something to actually happen so lets say there is a 50% chance of that then i would need a random choice of what "could" happen, i really need to try and write something and see how far off i am with this but at the moment my head isn't working :-/ Link to comment https://forums.phpfreaks.com/topic/126037-solved-random-events/#findComment-651808 Share on other sites More sharing options...
thebadbad Posted September 27, 2008 Share Posted September 27, 2008 Example script below. Each time it's loaded, there's a 50% chance that one of the specified events will happen. These events are equally likely to happen. <?php $chance = 50; //chance that an event will happen, in whole percent $events = array( 'got strong', 'got speedy', 'got slow' ); if (mt_rand(1, 100) <= $chance) { echo $events[mt_rand(0, count($events) - 1)]; } ?> Link to comment https://forums.phpfreaks.com/topic/126037-solved-random-events/#findComment-651824 Share on other sites More sharing options...
xoligy Posted September 28, 2008 Author Share Posted September 28, 2008 thanks for that thebadbad seems to work a treat Link to comment https://forums.phpfreaks.com/topic/126037-solved-random-events/#findComment-652281 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.