Friday, October 12, 2007

Basic Strategy game - simple attack script

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] => <a href=\"battle.php?attack=".$battle[id]."\">attack</a> <br>";

//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];
//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;
//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;
//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];
$target_w3 = $attack[weapon3];

if ($attack[weapon2]<=($attack[units]-$target_w3)) {
$target_w2 = $attack[units]-$target_w3;
$target_w2 = $attack[weapon2];

if ($attack[weapon1]<=($attack[units]-$target_w3-$target_w2)) {
$target_w1 = $attack[units]-$target_w3-$target_w2;
$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.


Unknown said...

Nice tutorial :D

Black Swan said...

There`s a bug ;)
If you spend all your money in soldiers before you log out, when somebody attack you, he won`t get money. It must be made to check the last time defender was and to give him money that he earnd, and than to attack.

Sorry for my English. Peace

Saljutin said...

Well you cannot steal money user does not have :) But yes I forgot to include formula that checks if attacked user gained any money from time of his last update and time of this attack. Thanks for notice :)

Danny said...

Hi, great tutorials. I'm new to php and have been learning by reading your scripts and putting everything together. I've had errors using it before but have managed to fix it with a bit of tweaking. But I'm stuck on this particular tutorial. I get this error:

"Parse error: parse error, unexpected ')' on line 97"

Line 97: if ($target_w0_die >) { $target_w0_die=$target_w0; }

Would appreciate any help in solving this problem. I really don't understand it. Thanks!

August said...

If i understood your problem correctly it should be:

Line 97: if ($target_w0_die > { $target_w0_die=$target_w0; } )

I'm very new to PHP, correct me if I'm wrong.

jaynes said...

hey, kindly help me here.. i have my parse error line number 92. i just copy and i paste it as it is... email me at hope you do...

Unknown said...

Hey I like this tutorial, very informative. I tried to make a game called army that needed such a script. Thankfully its still sitting on my portable hard drive, maybe I can get it working again eh?

The cool thing about that game was that your army was generated based on how many people you recruited to the game. Gold was won from battles, etc, etc. Get a hold of me sometime, I love coding PHP!


Unknown said...

Hi, I'm wondering if you can zip all the files and database and let people download it?

justImagine said...

Hey what do you think of this
My Game (Archaeozoic Area)
The battle system is simple, but I put some extra features on it. Hope you like it :)