Hello again. First of all, I like to say sorry to all readers of this blog (if any ). I was quite busy for last 2 weeks, but hey at least you all have time to really get into last tutorials and understand things!
So lets start. In this tutorial I will try to explain how to create simple attack script.
As always theory first:
user browses user list and attack user he wants to
soldier will have basic strength of 100
equipping weapon1 would double output of soldier, so 200
weapon2 is 400 and with weapon3 is 800
number of casualties on each side will depend on amount of damage dealt
Now we need some file where we will browse users, choose who to attack and see battle report. Lets create file like battle.php (don't forget to include config.php in beginning of file).
//this file will be all in php
//if user did not select who to attack, we show him list of users
if (!$_GET[attack]) {
//we select id and username of everyone except us
$battleQ = mysql_query("SELECT id,username FROM users WHERE id<>'$user[id]'");
while ($battle = mysql_fetch_array($battleQ)) {
//we show all selected users as well as link to their "attack" subpage
echo "$battle[username] =>
attack ";
}
}
//if user selected link to attack player
if ($_GET[attack]) {
$attack_id = $_GET[attack];
//we check if ID submited is numerical
if (!is_numeric($attack_id)) { die("Bad target ID!"); }
//we select user with that ID
$attackQ = mysql_query("SELECT * FROM users WHERE id='$user[id]' LIMIT 1");
$attackR = mysql_num_rows($attackQ);
//we check that rows are 1. if there are zero rows this means user is modifying link so we close our script!
if ($attackR <> 1) { die("Bad target ID!"); }
//all is fine, we select attacked user
$attack = mysql_fetch_array($attackQ);
//now we divide weapons to our people
if ($user[weapon3]<=$user[units]) {
//if weapon3 is more or the same of all people, then all people have it
$user_w3 = $user[units];
}else{
//only some users get weapon3
$user_w3 = $user[weapon3];
}
if ($user[weapon2]<=($user[units]-$user_w3)) {
//if weapon2 is more or the same of our people without any weapons, then they have it
$user_w2 = $user[units]-$user_w3;
}else{
//only some users get weapon3
$user_w2 = $user[weapon2];
}
if ($user[weapon1]<=($user[units]-$user_w3-$user_w2)) {
//if weapon1 is more or the same of our people without any weapons, then they have it
$user_w1 = $user[units]-$user_w3-$user_w2;
}else{
//only some users get weapon3
$user_w1 = $user[weapon1];
}
//those are our people without weapons
$user_w0 = $user[units]-$user_w3-$user_w2-$user_w1;
//now we calculate our strength
$user_strength = $user_w0*100 + $user_w1*200 + $user_w2*400 + $user_w3*800;
//We do the same thing for attacked player
//now we divide weapons to attacker
if ($attack[weapon3]<=$attack[units]) {
$target_w3 = $attack[units];
}else{
$target_w3 = $attack[weapon3];
}
if ($attack[weapon2]<=($attack[units]-$target_w3)) {
$target_w2 = $attack[units]-$target_w3;
}else{
$target_w2 = $attack[weapon2];
}
if ($attack[weapon1]<=($attack[units]-$target_w3-$target_w2)) {
$target_w1 = $attack[units]-$target_w3-$target_w2;
}else{
$target_w1 = $attack[weapon1];
}
//those are people who we attacked without weapons
$target_w0 = $attack[units]-$target_w3-$target_w2-$target_w1;
//now we calculate defender strength
$target_strength = $target_w0*100 + $target_w1*200 + $target_w2*400 + $target_w3*800;
//now to calculate casualties; we would divide strength with four (4) and then check the percentage of troops died
$att_cas = $target_strength/4;
$def_cas = $user_strength/4;
//we create formula but see that we have $target_w0 on both sides of / so we can use simple formula
//$target_w0_die = ($def_cas/$target_w0*100)*$target_w0;
$target_w0_die = ($def_cas/100);
//we cannot have more death people then we have
if ($target_w0_die >) { $target_w0_die=$target_w0; }
$target_w1_die = ($def_cas/200);
if ($target_w1_die >) { $target_w1_die=$target_w1; }
$target_w2_die = ($def_cas/400);
if ($target_w2_die >) { $target_w2_die=$target_w2; }
$target_w3_die = ($def_cas/800);
if ($target_w3_die >) { $target_w3_die=$target_w3; }
$target_killed = $target_w0_die+$target_w1_die+$target_w2_die+$target_w3_die;
//the same for our troops
$user_w0_die = ($att_cas/100);
if ($user_w0_die >) { $user_w0_die=$user_w0; }
$user_w1_die = ($att_cas/200);
if ($user_w1_die >) { $user_w1_die=$user_w1; }
$user_w2_die = ($att_cas/400);
if ($user_w2_die >) { $user_w2_die=$user_w2; }
$user_w3_die = ($att_cas/800);
if ($user_w3_die >) { $user_w3_die=$user_w3; }
$user_killed = $user_w0_die+$user_w1_die+$user_w2_die+$user_w3_die;
//we have two options
//attacker won
if ($user_strength > $target_strength) {
echo "Your troops won. You lost $user_killed units! You managed to plunder $attack[money] money.";
//we update attacker and defender, add money to attacker
mysql_query("UPDATE users SET money=money+'$attack[money]', units=units-'$user_killed' WHERE id='$user[id]'");
mysql_query("UPDATE users SET money='0', units=units-'$target_killed' WHERE id='$attack[id]'");
}
//defender won
if ($user_strength <= $target_strength) {
echo "Enemy troops were too strong. You lost $user_killed units!";
//we update attacker and defender
mysql_query("UPDATE users SET units=units-'$user_killed' WHERE id='$user[id]'");
mysql_query("UPDATE users SET units=units-'$target_killed' WHERE id='$attack[id]'");
}
}
This concludes our tutorial. I hope it was worth to wait for it!
Remember: It is not your PHP knowledge that would make game good, it is your logic and thinking! Once you have all formulas done, its easy to put them in PHP code! Read first post in this blog and you can learn basics of creating good plan of game.
What else can we do in this script:
formula to determine percentage of money won
formula to calculate loss of weapons
report system (so attacked user can see report of attack)
But this is up to you. I will be glad if you comment on this post, tell me about your ideas or show-off your game
Till next time ... enjoy.
POSTED BY SALJUTIN AT 8:47 AM