almightyegg Posted April 13, 2007 Share Posted April 13, 2007 I'm making a game but I can't figure out howto do a battle script. All I need is suggestions on how to do it, not somebody to do it for me (hense why it's not on freelance). It is one player vs. one player each player has the variables: stamina agility strength magic lifeforce weapon armor what way would be best to make an automatic battle (Not action script) like this: player1 attacked you for 1,320 damage, 20 left. you attacked player1 for 3 damage, 2,550 left. you missed. you missed. player1 attacked you for 300 damage, 0 left. Victor: Player1 ideas? Quote Link to comment Share on other sites More sharing options...
StormTheGates Posted April 13, 2007 Share Posted April 13, 2007 Hey, I run a mafia game and code stuff like this every day. What I have done is have all variables(strength, stamina ect) in the database in the users table, then when theres a battle it calls it out, throws them into a formula, and then computes for damage. The formula youll have to do yourself to your own specifications. But then you can just loop through until one person is dead. For instance while($attackerhealth > 0 OR $defenderhealth > 0){ if($whosturn == attacker){ //some formula to get damage $whosturn = defender; $defenderhealth = $defenderhealth - $damage; if($defenderhealth <= 0){ $winner = attacker; } else { echo "You did $damage to $defender!"; } } else { //some formula to get damage $whosturn = attacker; $attackerhealth = $attackerhealth - $damage; if($attackerhealth <= 0){ $winner = defender; echo "$defender did $damage to you and killed you!"; } else { echo "$defender did $damage to you!"; } if($winner == attacker){ //some mysql query for winning } else { //some mysql query for defender } } } Or something like this. Youll have to work it to your specifications and bug test and whatnot, but thats the overall premise of it I think. Quote Link to comment Share on other sites More sharing options...
almightyegg Posted April 13, 2007 Author Share Posted April 13, 2007 Thanks, but I have 2 queries: 1) What sort of formula? do you have an example I could see? 2) Is there any way I could make the person with higher agility go first and then turns are taken alternatively? Quote Link to comment Share on other sites More sharing options...
StormTheGates Posted April 13, 2007 Share Posted April 13, 2007 Erm for a formula thats sorta complicated as to how you want to balance. Heres a basic one. $damage = $attackerstrength - $defenderdefense; Thats a very basic one, you can throw in your own stuff. And yes its very easy, basically all you need to do before the loop is something like this. if($defenderagility > $attackeragility){ $whosturn = defender ; } else { $whosturn = attacker; } Quote Link to comment Share on other sites More sharing options...
almightyegg Posted April 13, 2007 Author Share Posted April 13, 2007 But surely if you do that in a while it will never be te other persons turn ??? $formula = $mem[magic]*($mem[agility]/4)*($mem[stamina]/2)*$mem[strength]; That would work as a formula yes? Except I have differentweapons and armor, how would I incorporate them? Quote Link to comment Share on other sites More sharing options...
StormTheGates Posted April 13, 2007 Share Posted April 13, 2007 Yea that formula would work. What you didnt get about the agility is that you put it before the loop, that way it only sets the $whosturn variable once. The loop will never reach outside of itself so you would get that turn rotation. So for armor and weapons I would have something in the database like this Table Weapons |----------------------------| |Username | Weapon | Power| |----------------------------| |PersonOne | Dagger | 4 | |----------------------------| Then do a query to select the power from the table like this mysql_query("SELECT power FROM weapons WHERE username='$attacker'"); Then assign that to a variable called $attackpower and incorporate it into your formula You could do the same with defense and armor. Quote Link to comment Share on other sites More sharing options...
almightyegg Posted April 13, 2007 Author Share Posted April 13, 2007 if($defender[agility] >= $mem[agility]){ $whosturn = defender ; } else { $whosturn = attacker; } $aweap = mysql_query("SELECT power FROM weapons WHERE id = '{$mem['wid']}'"); $aarm = mysql_query("SELECT power FROM armor WHERE id = '{$mem['wid']}'"); $dweap = mysql_query("SELECT power FROM weapons WHERE id = '{$defender['wid']}'"); $darm = mysql_query("SELECT power FROM armor WHERE id = '{$defender['wid']}'"); $aform = $mem[magic]*($mem[agility]/4)*($mem[stamina]/2)*$mem[strength]+($aweap-$darm); $aform2 = $mem[magic]*($mem[agility]/4)*($mem[stamina]/2)*$mem[strength]+($aweap-$darm)*1.5; $aformula = rand($aform, $aform2); $dform = $defender[magic]*($defender[agility]/4)*($defender[stamina]/2)*$defender[strength]+($dweap-$aarm); $dform2 = $defender[magic]*($defender[agility]/4)*($defender[stamina]/2)*$defender[strength]+($dweap-$aarm)*1.5; $dformula = rand($dform, $dform2); while($mem[curlf] > 0 OR $defender[curlf] > 0){ if($whosturn == attacker){ $whosturn = defender; $defender[curlf] = $defender[curlf] - $aformula; if($defender[curlf] <= 0){ $winner = attacker; } else { echo "You did $aformula to $defender[username]!"; } } else { $whosturn = attacker; $mem[curlf] = $mem[curlf] - $dformula; if($mem[curlf] <= 0){ $winner = defender; echo "$defender[username] did $dformula to you and killed you!"; } else { echo "$defender[username] did $dformula to you!"; } if($winner == attacker){ echo "Victor: $mem[username]"; } else { echo "Victor: $defender[username]"; } } } It is taking forever to load the page...? Quote Link to comment Share on other sites More sharing options...
almightyegg Posted April 13, 2007 Author Share Posted April 13, 2007 It crashed my browser! Is it because the while doesn't know where to stop? Quote Link to comment Share on other sites More sharing options...
taith Posted April 13, 2007 Share Posted April 13, 2007 try somin like this... $goodguyhealth=100; $goodguyatt=100; $goodguydef=100; $badguyhealth=100; $badguyatt=100; $badguydef=100; while($dead!==true){ $attmodifier=$goodguyatt/$badguydef; $defmodifier=$goodguydef/$badguyatt; $goodguydamage=rand(0,$badguyatt)*defmodifier; $baddguyamage=rand(0,$goodguyatt)*attmodifier; $goodguyhealth=$goodguyhealth-$badguydamage $badguyhealth=$badguyhealth-$goodguydamage if($goodguyhealth<=0){ $dead=true; //loose exp/money... killchar(); }elseif($badguyhealth<=0){ $dead=true; //add exp/money/etc... } } or somin to that effect... havent tested the math... but it makes logical sense i think... Quote Link to comment Share on other sites More sharing options...
almightyegg Posted April 14, 2007 Author Share Posted April 14, 2007 Fatal error: Maximum execution time of 30 seconds exceeded in /home/lordofth/public_html/battle/battle.php on line 52 $goodguyhealth=100; $goodguyatt=100; $goodguydef=100; $badguyhealth=100; $badguyatt=100; $badguydef=100; while($dead!==true){ $attmodifier=$goodguyatt/$badguydef; $defmodifier=$goodguydef/$badguyatt; $goodguydamage=rand(0,$badguyatt)*defmodifier; $baddguyamage=rand(0,$goodguyatt)*attmodifier; $goodguyhealth=$goodguyhealth-$badguydamage; $badguyhealth=$badguyhealth-$goodguydamage; if($goodguyhealth<=0){ $dead=true; echo "You lost!"; killchar(); }elseif($badguyhealth<=0){ $dead=true; echo "You won!"; } } Quote Link to comment Share on other sites More sharing options...
almightyegg Posted April 14, 2007 Author Share Posted April 14, 2007 Line 52: $defmodifier=$goodguydef/$badguyatt; Quote Link to comment Share on other sites More sharing options...
almightyegg Posted April 14, 2007 Author Share Posted April 14, 2007 bump Quote Link to comment Share on other sites More sharing options...
almightyegg Posted April 15, 2007 Author Share Posted April 15, 2007 ^bump^ Quote Link to comment 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.