Darkneo
05-19-2008, 12:44 AM
Originally Posted By Shwaza
Ta dah! Here's another one I kind of forgot about it for a while until someone added me on MSN asking if I would ever write the tutorial for an attack system. If you haven't read my other (completely mind-blowing *rolls eyes*) you can find them here:
Part 1 (http://forums.hostmatrix.org/showthread.php?t=3388)
Part 2 (http://forums.hostmatrix.org/showthread.php?t=3389)
Part 3 (http://forums.hostmatrix.org/showthread.php?t=3390)
You should read the above tutorials if you haven't already
Just a note before I begin, I'd like to tell you that these tutorials should be used for learning not for simply taking code from. Putting these tutorials together will not make an entire game for you, they're just outlines to guide you along the way. Just taking code from them and not trying to learn or understand what's going on will not help you at all.
With that out of the way, let's begin . This tutorial will be on an attack system which is always a fun thing to have in a game. It will be focused on an army vs. army style as opposed to a unit vs. unit style that you may get from other kinds of RPG games.
To begin, here is a form that will initiate the attack. You would target this to your attack page, so for now lets call this file start_attack.php. Note: this is not a complete file, just the form that would go in it.
<form name=”attack_form” action=”attack.php” method=”post”>
<h2>Attack</h2>
<p>
Country: <input type=”text” name=”country” />
</p>
<p>
Unit one: <input type=”text” name=”unit1” />
</p>
<p>
Unit two: <input type=”text” name=”unit2” />
</p>
<p>
<input type=”submit” name=”attack” value=”Attack!” />
</p>
This is a very simple bit of code so I won't describe the entire block, I'll just give you a rundown of what it is. It's just a form that sends you to attack.php. It collects the name of the country you wish to attack, the amount of “unit1” and “unit2” you would like to send to battle, then gives you a submit button to send the attack.
Next I'll write a simple piece of code for attack.php then explain it. Again it's not the full page, just the attack script.
<?php
//Assuming you already have a mysql connection
//Assuming you already have your sessions set with one for username and password
if(!$_POST['attack']){
die(“You must send an attack to view this page!”); //Stops the script if the user has not submitted the form on start_attack.php
}
//Set variables for posted values
$unit1_sent = $_POST['unit1'];
$unit2_sent = $_POST['unit2'];
$defender = $_POST['country'];
//Query user's row
$query = mysql_query(“SELECT * FROM accounts WHERE username = “. $_SESSION['username']. “ and password = “. $_SESSION['password']);
if(mysql_num_rows($query) != 1){
die(“We did not find your account. Please relogin”); //If row is not found, kill script
}
while($r = mysql_fetch_assoc($query)){
//Define variables for user's unit1 and unit2 for
$unit1 = $r['unit1'];
$unit2 = $r['unit2'];
$money = $r['money'];
}
//Query defender's row
$query = mysql_query(“SELECT * FROM accounts WHERE country = '$defender'”);
if(mysql_num_rows($query) != 1){
die(“You have entered an invalid country name, please go back and try again.”); //If row is not found, kill script and tell them to retry
}
while($r = mysql_fetch_assoc($query)){
//Define variables for defender's unit1 and unit2 for
$def_unit1 = $r['unit1'];
$def_unit2 = $r['unit2'];
$def_money = $r['money'];
}
//Units' powers
$unit1_pwr = 1; //Set that to whatever you want, a higher number makes that unit stronger
$unit2_pwr = 2; //Same deal
//Conquer/kill values
$money_cqr = 0.10; //As a percent, 0.10 will give 10% of the defenders money to the attacker with a successful attack
//Check if units sent are not too many
if($unit1_sent > $unit1 || $unit2_sent > $unit2){
die(“Sorry, you have tried to send more units than you have.”); //kill script if too many units were sent
}
//Set powers
//Now that we have unit power values, we can determine who has the highest overall power
$power = $unit1_sent * $unit1_pwr + $unit2_sent * $unit2_pwr;
$def_power = $def_unit1 * $unit1_pwr + $def_unit2 * $unit2_pwr;
//Check who won
if($power > $def_power){
$successful = TRUE;
}
//If the attack was successful let's kill some units and earn some money :D
if($successful){
$new_unit1 = $unit1*.98; //Kill 2% of attacking unit1
$new_unit2 = $unit2*.98; //Kill 2% of attacking unit2
$new_def_unit1 = $def_unit1*.9; //Kill 10% of defending unit1
$new_def_unit2 = $def_unit2*.9; //Kill 10% of defending unit2
$new_money = $money + $def_money*$money_cqr; //add the conquer percent of money
$new_def_money = $def_money - $def_money*$money_cqr; //subtract the conquer percent of money
//Set a message to be outputted
$message = “Attack successful! You lost “. $unit1 - $new_unit1. “ unit1, “. $unit2 - $new_unit2. “ unit2. You killed “. $def_unit1 - $new_def_unit1. “ unit1, “. $def_unit2 - $new_def_unit2. “ unit2. You conquered “. $new_money - $money. “!”;
}
//Unsuccessful attack
else{
$new_unit1 = $unit1*.9; //Kill 10% of attacking unit1
$new_unit2 = $unit2*.9; //Kill 10% of attacking unit2
$new_def_unit1 = $def_unit1*.98; //Kill 2% of defending unit1
$new_def_unit2 = $def_unit2*.98; //Kill 2% of defending unit2
$new_money = $money;
$new_def_money = $def_money;
//Set a message to be outputted
$message = “Attack unsuccessful. You lost “. $unit1 - $new_unit1. “ unit1, “. $unit2 - $new_unit2. “ unit2. You killed “. $def_unit1 - $new_def_unit1. “ unit1, “. $def_unit2 - $new_def_unit2. “ unit2.”;
}
echo $message; //Output the message you've got
//Now let's update the database
mysql_query(“UPDATE accounts SET unit1 = $new_unit1, unit2 = $new_unit2, money = $new_money WHERE username = “. $_SESSION['username']. “ AND password = ”. $_SESSION['password']);
mysql_query(“UPDATE accounts SET unit1 = $new_def_unit1, unit2 = $new_def_unit2, money = $new_def_money WHERE country = '$defender'”);
//And we're done :)
?>
Well there's a very basic script just riddled with comments so I don't think you'll have too much trouble understanding it
Notes
-Again I have to say that this is just some example code, I wouldn't try to base an entire game off of just these tutorials
-Please let me know if you have any questions/problems with this tutorial
Advanced tips
For the more advanced reader you may want to try some of these more advanced tips
-Notification of the defender. You may want to create a “log” kind of system to record these attacks for the defender. Possibly this could be a further tutorial
-More units and losses. Feel free to add in more units with different names to this, just use the unit1 and unit2 as templates. You can also add in more things lost like land, buildings, whatever you like, it's your game
Ta dah! Here's another one I kind of forgot about it for a while until someone added me on MSN asking if I would ever write the tutorial for an attack system. If you haven't read my other (completely mind-blowing *rolls eyes*) you can find them here:
Part 1 (http://forums.hostmatrix.org/showthread.php?t=3388)
Part 2 (http://forums.hostmatrix.org/showthread.php?t=3389)
Part 3 (http://forums.hostmatrix.org/showthread.php?t=3390)
You should read the above tutorials if you haven't already
Just a note before I begin, I'd like to tell you that these tutorials should be used for learning not for simply taking code from. Putting these tutorials together will not make an entire game for you, they're just outlines to guide you along the way. Just taking code from them and not trying to learn or understand what's going on will not help you at all.
With that out of the way, let's begin . This tutorial will be on an attack system which is always a fun thing to have in a game. It will be focused on an army vs. army style as opposed to a unit vs. unit style that you may get from other kinds of RPG games.
To begin, here is a form that will initiate the attack. You would target this to your attack page, so for now lets call this file start_attack.php. Note: this is not a complete file, just the form that would go in it.
<form name=”attack_form” action=”attack.php” method=”post”>
<h2>Attack</h2>
<p>
Country: <input type=”text” name=”country” />
</p>
<p>
Unit one: <input type=”text” name=”unit1” />
</p>
<p>
Unit two: <input type=”text” name=”unit2” />
</p>
<p>
<input type=”submit” name=”attack” value=”Attack!” />
</p>
This is a very simple bit of code so I won't describe the entire block, I'll just give you a rundown of what it is. It's just a form that sends you to attack.php. It collects the name of the country you wish to attack, the amount of “unit1” and “unit2” you would like to send to battle, then gives you a submit button to send the attack.
Next I'll write a simple piece of code for attack.php then explain it. Again it's not the full page, just the attack script.
<?php
//Assuming you already have a mysql connection
//Assuming you already have your sessions set with one for username and password
if(!$_POST['attack']){
die(“You must send an attack to view this page!”); //Stops the script if the user has not submitted the form on start_attack.php
}
//Set variables for posted values
$unit1_sent = $_POST['unit1'];
$unit2_sent = $_POST['unit2'];
$defender = $_POST['country'];
//Query user's row
$query = mysql_query(“SELECT * FROM accounts WHERE username = “. $_SESSION['username']. “ and password = “. $_SESSION['password']);
if(mysql_num_rows($query) != 1){
die(“We did not find your account. Please relogin”); //If row is not found, kill script
}
while($r = mysql_fetch_assoc($query)){
//Define variables for user's unit1 and unit2 for
$unit1 = $r['unit1'];
$unit2 = $r['unit2'];
$money = $r['money'];
}
//Query defender's row
$query = mysql_query(“SELECT * FROM accounts WHERE country = '$defender'”);
if(mysql_num_rows($query) != 1){
die(“You have entered an invalid country name, please go back and try again.”); //If row is not found, kill script and tell them to retry
}
while($r = mysql_fetch_assoc($query)){
//Define variables for defender's unit1 and unit2 for
$def_unit1 = $r['unit1'];
$def_unit2 = $r['unit2'];
$def_money = $r['money'];
}
//Units' powers
$unit1_pwr = 1; //Set that to whatever you want, a higher number makes that unit stronger
$unit2_pwr = 2; //Same deal
//Conquer/kill values
$money_cqr = 0.10; //As a percent, 0.10 will give 10% of the defenders money to the attacker with a successful attack
//Check if units sent are not too many
if($unit1_sent > $unit1 || $unit2_sent > $unit2){
die(“Sorry, you have tried to send more units than you have.”); //kill script if too many units were sent
}
//Set powers
//Now that we have unit power values, we can determine who has the highest overall power
$power = $unit1_sent * $unit1_pwr + $unit2_sent * $unit2_pwr;
$def_power = $def_unit1 * $unit1_pwr + $def_unit2 * $unit2_pwr;
//Check who won
if($power > $def_power){
$successful = TRUE;
}
//If the attack was successful let's kill some units and earn some money :D
if($successful){
$new_unit1 = $unit1*.98; //Kill 2% of attacking unit1
$new_unit2 = $unit2*.98; //Kill 2% of attacking unit2
$new_def_unit1 = $def_unit1*.9; //Kill 10% of defending unit1
$new_def_unit2 = $def_unit2*.9; //Kill 10% of defending unit2
$new_money = $money + $def_money*$money_cqr; //add the conquer percent of money
$new_def_money = $def_money - $def_money*$money_cqr; //subtract the conquer percent of money
//Set a message to be outputted
$message = “Attack successful! You lost “. $unit1 - $new_unit1. “ unit1, “. $unit2 - $new_unit2. “ unit2. You killed “. $def_unit1 - $new_def_unit1. “ unit1, “. $def_unit2 - $new_def_unit2. “ unit2. You conquered “. $new_money - $money. “!”;
}
//Unsuccessful attack
else{
$new_unit1 = $unit1*.9; //Kill 10% of attacking unit1
$new_unit2 = $unit2*.9; //Kill 10% of attacking unit2
$new_def_unit1 = $def_unit1*.98; //Kill 2% of defending unit1
$new_def_unit2 = $def_unit2*.98; //Kill 2% of defending unit2
$new_money = $money;
$new_def_money = $def_money;
//Set a message to be outputted
$message = “Attack unsuccessful. You lost “. $unit1 - $new_unit1. “ unit1, “. $unit2 - $new_unit2. “ unit2. You killed “. $def_unit1 - $new_def_unit1. “ unit1, “. $def_unit2 - $new_def_unit2. “ unit2.”;
}
echo $message; //Output the message you've got
//Now let's update the database
mysql_query(“UPDATE accounts SET unit1 = $new_unit1, unit2 = $new_unit2, money = $new_money WHERE username = “. $_SESSION['username']. “ AND password = ”. $_SESSION['password']);
mysql_query(“UPDATE accounts SET unit1 = $new_def_unit1, unit2 = $new_def_unit2, money = $new_def_money WHERE country = '$defender'”);
//And we're done :)
?>
Well there's a very basic script just riddled with comments so I don't think you'll have too much trouble understanding it
Notes
-Again I have to say that this is just some example code, I wouldn't try to base an entire game off of just these tutorials
-Please let me know if you have any questions/problems with this tutorial
Advanced tips
For the more advanced reader you may want to try some of these more advanced tips
-Notification of the defender. You may want to create a “log” kind of system to record these attacks for the defender. Possibly this could be a further tutorial
-More units and losses. Feel free to add in more units with different names to this, just use the unit1 and unit2 as templates. You can also add in more things lost like land, buildings, whatever you like, it's your game