Jump to content

Battle script


almightyegg

Recommended Posts

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...?

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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!";
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.