NameSilo

[PHP] Making a battle script

SpaceshipSpaceship
NamecheapNamecheap
Namecheap AuctionsNamecheap Auctions
Watch

liam_d

The original NP Emo KidEstablished Member
Impact
25
Well i have started making a battle script for use in my game but i am really stuck

i have all the values ready to use but i'm not exactly sure how to make the you and them battle?

PHP:
function fight($you, $them)
{
	$get_your_mecha = mysql_query('SELECT * FROM `mecha` WHERE `owner` = "' . (int) $you . '"');
	$your_mecha = mysql_fetch_array($get_your_mecha);
        
	$get_enemy_mecha = mysql_query('SELECT * FROM `mecha` WHERE `owner` = "' . (int) $them . '"');
	$enemy_mecha = mysql_fetch_array($get_your_mecha);
        
        if ($your_mecha['armour'] <= 0)
        {
            echo 'You need to repair your Mecha before you can battle!';
        }
        
        if ($your_mecha['armour'] <= 0)
        {
            echo 'You need to repair your Mecha before you can battle!';
        }
        
        // set your stats here
        $your_weapon = mysql_fetch_array(mysql_query('SELECT * FROM `weapon` WHERE `mecha_owner` = "' . $your_mecha['id'] . '"'));
        $your_armour = mysql_fetch_array(mysql_query('SELECT * FROM `armour` WHERE `mecha_owner` = "' . $your_mecha['id'] . '"'));
        $your_pack = mysql_fetch_array(mysql_query('SELECT * FROM `pack` WHERE `mecha_owner` = "' . $your_mecha['id'] . '"'));
        
        $your['strength'] = $your_mecha['strength'] + $your_weapon['power'];
        $your['armour'] = $your_mecha['armour'] + $your_armour['power'];
        $your['agility'] = $your_mecha['agility'] +  + $your_agility['power'];
        
        // set enemy stats here
        $enemy_weapon = mysql_fetch_array(mysql_query('SELECT * FROM `weapon` WHERE `mecha_owner` = "' . $enemy_mecha['id'] . '"'));
        $enemy_armour = mysql_fetch_array(mysql_query('SELECT * FROM `armour` WHERE `mecha_owner` = "' . $enemy_mecha['id'] . '"'));
        $enemy_pack = mysql_fetch_array(mysql_query('SELECT * FROM `pack` WHERE `mecha_owner` = "' . $enemy_mecha['id'] . '"'));
        
	$enemy['strength'] = $enemy_mecha['strength'] + $enemy_weapon['power'];
	$enemy['armour'] = $enemy_mecha['armour'] + $enemy_armour['power'];
	$enemy['agility'] = $enemy_mecha['agility'] +  + $enemy_agility['power'];
		
	// fighting
	/* determine who is first by who has better agility */
	if ($your['agility'] > $enemy['agility'])
	{
		
	}
		
	else
	{
	}
        

	// if both alive fight again
}
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
here are some thoughts:

Hit chance:

$your['hitchance'] = $your_mecha['agility'] / ( $your['agility'] - $enemy_mecha['agility'] )
$enemy['hitchance']= $enemy_mecha['agility'] / ( $enemy['agility'] - $your_mecha['agility'] )

Combat damage:

$your['maxdamage'] = $your_mecha['strength'] + $your_weapon['power'] - $enemy_mecha['armour']
$your['mindamage'] = $your_mecha['strength'] + $your_weapon['power'] - $enemy_mecha['armour'] + $enemy_armour['power']

( the same for the enemy )
 
0
Close
Thanks for the input, here is what i have now, think all that is left is to actually do the battle, to which i am unsure about how to do?

PHP:
<?php

function fight($you, $them)

{

	$get_your_mecha = mysql_query('SELECT * FROM `mecha` WHERE `owner` = "' . (int) $you . '"');

	$your_mecha = mysql_fetch_array($get_your_mecha);

        

	$get_enemy_mecha = mysql_query('SELECT * FROM `mecha` WHERE `owner` = "' . (int) $them . '"');

	$enemy_mecha = mysql_fetch_array($get_your_mecha);

        

	if ($your_mecha['armour'] <= 0)

	{

		echo 'You need to repair your Mecha before you can battle!';

	}

        

	if ($your_mecha['armour'] <= 0)

	{

		echo 'You need to repair your Mecha before you can battle!';

	}

        

	// set your stats here

	$your_weapon = mysql_fetch_array(mysql_query('SELECT * FROM `weapon` WHERE `mecha_owner` = "' . $your_mecha['id'] . '"'));

	$your_armour = mysql_fetch_array(mysql_query('SELECT * FROM `armour` WHERE `mecha_owner` = "' . $your_mecha['id'] . '"'));

	$your_pack = mysql_fetch_array(mysql_query('SELECT * FROM `pack` WHERE `mecha_owner` = "' . $your_mecha['id'] . '"'));

        

	$your['strength'] = $your_mecha['strength'] + $your_weapon['power'];

	$your['armour'] = $your_mecha['armour'] + $your_armour['power'];

	$your['agility'] = $your_mecha['agility'] +  + $your_agility['power'];

        

	// set enemy stats here

	$enemy_weapon = mysql_fetch_array(mysql_query('SELECT * FROM `weapon` WHERE `mecha_owner` = "' . $enemy_mecha['id'] . '"'));

	$enemy_armour = mysql_fetch_array(mysql_query('SELECT * FROM `armour` WHERE `mecha_owner` = "' . $enemy_mecha['id'] . '"'));

	$enemy_pack = mysql_fetch_array(mysql_query('SELECT * FROM `pack` WHERE `mecha_owner` = "' . $enemy_mecha['id'] . '"'));

        

	$enemy['strength'] = $enemy_mecha['strength'] + $enemy_weapon['power'];

	$enemy['armour'] = $enemy_mecha['armour'] + $enemy_armour['power'];

	$enemy['agility'] = $enemy_mecha['agility'] +  + $enemy_agility['power'];



	// chance of hitting first



	$your['hitchance'] = $your_mecha['agility'] / ( $your['agility'] - $enemy_mecha['agility'] );

	$enemy['hitchance'] = $enemy_mecha['agility'] / ( $enemy['agility'] - $your_mecha['agility'] );

	

	//Combat damage:

	

	$your['maxdamage'] = $your_mecha['strength'] + $your_weapon['power'] - $enemy_mecha['armour'];

	$your['mindamage'] = $your_mecha['strength'] + $your_weapon['power'] - $enemy_mecha['armour'] + $enemy_armour['power'];

	//Combat damage:

	

	$enemy['maxdamage'] = $your_mecha['strength'] + $your_weapon['power'] - $enemy_mecha['armour'];

	$enemy['mindamage'] = $enemy_mecha['strength'] + $enemy_weapon['power'] - $your_mecha['armour'] + $your_armour['power'];

	

	// if your go first

	if ($your['hitchance'] > $enemy['hitchance'])

	{

	}

	

	// if enemy go first

	else

	{

	}

}

?>
 
Last edited:
0
Close
friendly bump, any ideas?
 
0
Close
This is how i would do it :
This is not tested- just written to give you a idea

PHP:
<?php
function fight($you, $them) { 

$get_your_mecha = mysql_query('
SELECT 
user.id,user.strength,user.armour,user.agility, 
weapon.power as weapon_power, 
armour.power as armour_power, 
agility.power as agility_power

FROM mecha as user
LEFT JOIN weapon
ON weapon.mecha_owner = $you
AND
LEFT JOIN armour
ON armour.mecha_owner = $you
AND
LEFT JOIN pack
ON pack.mecha_owner = $you

WHERE user.owner = "' . (int) $you . '"'); 
$you = mysql_fetch_array($get_your_mecha); 

$get_enemy_mecha = mysql_query('
SELECT 
user.id,user.strength,user.armour,user.agility, 
weapon.power as weapon_power, 
armour.power as armour_power, 
agility.power as agility_power

FROM mecha as user
LEFT JOIN weapon
ON weapon.mecha_owner = $them
AND
LEFT JOIN armour
ON armor.mecha_owner = $them
AND
LEFT JOIN pack
ON pack.mecha_owner = $them

WHERE user.owner = "' . (int) $them . '"'); 
); 
$enemy = mysql_fetch_array($get_enemy_mecha); 

if ($you['armour_power'] <= 0)  {
echo 'You need to repair your Mecha before you can battle!'; 
}
    /*$your_weapon = mysql_fetch_array(mysql_query('SELECT * FROM `weapon` WHERE `mecha_owner` = "' . $your_mecha['id'] . '"')); 
    $your_armour = mysql_fetch_array(mysql_query('SELECT * FROM `armour` WHERE `mecha_owner` = "' . $your_mecha['id'] . '"')); 
    $your_pack = mysql_fetch_array(mysql_query('SELECT * FROM `pack` WHERE `mecha_owner` = "' . $your_mecha['id'] . '"')); 
	*/
         

    $you['strength'] = $you['strength'] + $you['weapon_power']; 
    $you['armour'] = $you['armour'] + $you['armour_power']; 
    $you['agility'] = $you['agility'] + $you['agility_power']; 
    // set enemy stats here 
	/*
	$your['strength'] = $your_mecha['strength'] + $your_weapon['power']; 

    $your['armour'] = $your_mecha['armour'] + $your_armour['power']; 

    $your['agility'] = $your_mecha['agility'] +  + $your_agility['power']; 
	

    $enemy_weapon = mysql_fetch_array(mysql_query('SELECT * FROM `weapon` WHERE `mecha_owner` = "' . $enemy_mecha['id'] . '"')); 
    $enemy_armour = mysql_fetch_array(mysql_query('SELECT * FROM `armour` WHERE `mecha_owner` = "' . $enemy_mecha['id'] . '"')); 
    $enemy_pack = mysql_fetch_array(mysql_query('SELECT * FROM `pack` WHERE `mecha_owner` = "' . $enemy_mecha['id'] . '"')); 
    $enemy['strength'] = $enemy_mecha['strength'] + $enemy_weapon['power']; 
    $enemy['armour'] = $enemy_mecha['armour'] + $enemy_armour['power']; 
    $enemy['agility'] = $enemy_mecha['agility'] +  + $enemy_agility['power']; 
*/
	
	$enemy['strength'] = $enemy['strength'] + $enemy['weapon_power']; 
    $enemy['armour'] = $enemy['armour'] + $enemy['armour_power']; 
    $enemy['agility'] = $enemy['agility'] + $enemy['agility_power']; 


    $your['hitchance'] = $you['agility'] / ( $you['agility'] - $enemy['agility'] ); 
    $enemy['hitchance'] = $enemy['agility'] / ( $enemy['agility'] - $you['agility'] ); 
   
    $your['maxdamage'] = $you['strength'] + $you['weapon_power'] - $enemy['armour_power']; 
    $your['mindamage'] = $you['strength'] + $you['weapon_power'] - $enemy['armour_power'] + $enemy['armour_power']; 

    //Combat damage: 
    $enemy['maxdamage'] = $you['strength'] + $your['weapon_power'] - $enemy['weapon_armour']; 
    $enemy['mindamage'] = $enemy['strength'] + $enemy['weapon_power'] - $you['armour'] + $your['weapon_power']; 

    // if your go first 

    if ($your['hitchance'] > $enemy['hitchance']) { 
	} else { 

    } 

} 
?>
 
0
Close
Thanks for the post this will help quite a lot i think and will definitely make it faster since using less queries :).

What i need now is to do the actual battle, which i have no idea how to do properly :(

Thanks!
 
0
Close
basically - you can do like the above - you have to add certain health (which is i guess armour) to be deducted from the total amount untill one of them drops to zero
might be handy if they can choose what attack - since i assume every attack has their own strength?
 
0
Close
I wrote this up in this post when i saw your post, so i definately didnt test it, but its a very good start to the direction your heading in

Code:
<?
session_start();

// lets tell the script that if the user is not logged in they cannot access
// the game, when the user is logging in, some variable should be
// set that they are logged in, a basic easy one is
// $_SESSION['loggedin']=1;
// and when they logout destroy the session, google is your friend.

if($_SESSION['loggedin']==0) { header("Location: login.php");exit; }

$userid=$_SESSION['userid']; //assuming you have sessions set
$id=$_GET['userid']; // assuming the url uses a GET function

$z=mysql_query("SELECT * FROM users WHERE userid='$userid'");
$x=mysql_fetch_array($z);

// Protect the admin, you.

if($_GET['userid'] == '1') {
print "You cannot attack the admin, i am all powerful"; die;
}

// Lets make sure they are not attacking themselves

if($_GET['userid'] == '$userid') {
print "You cannot attack yourself"; die;
}

if(isset($_GET['userid'])) {
$q=mysql_query("SELECT * FROM users WHERE userid='$id'");
$r=mysql_fetch_array($q);

// Set some variables

$offender=$x['name'];
$defender=$r['name'];
$offhealth=$x['health'];
$defhealth=$r['health'];
$offstat1=$x['stat1'];
$defstat1=$r['stat1'];
$offstat2=$x['stat2'];
$defstat2=$r['stat2'];

// Lets make sure the user isn't trying to kill a dead user
// Or trying to kill people even though they are dead

if($offhealth<1) {
print "You are dead, you cannot attack while dead, jerk."; die;
}

if($defhealth<1) {
print "This Player Is Dead"; die;
}

// Set a random factor so the most powerful is not always the winner

$offrand=rand(0,100);
$defrand=rand(0,100);
$rand=rand(100,999);

// Do some math

$offpower=($offstat1+$offstat2+$offrand);
$defpower=($defstat1+$defstat2+$defrand);

$offdamage=($defpower-$defrand/*3);
$defdamage=($offpower-$offrand/*3);

// This is where you let the user know what is going on
// Send them to a link
// attack.php?userid=$id&math=$rand
// Print the names and health of each user

if($offpower>$defpower){

// print the action that took place, defense lost health

mysql_query("UPDATE users SET health=health-'$defdamamge' WHERE userid='$id'"); }

if($offpower<$defpower){

// print the action that took place, offense lost health

mysql_query("UPDATE users SET health=health-'$offdamamge' WHERE userid='$userid'"); }

// when a player reaches 1 health or lower it will state the player is dead
// this script will need editing if stats are involved
// but this is a good start to get you on your way
}
?>

I hope this helps you bud.
 
Last edited:
0
Close
JFS said:
here are some thoughts:

Hit chance:

$your['hitchance'] = $your_mecha['agility'] / ( $your['agility'] - $enemy_mecha['agility'] )
$enemy['hitchance']= $enemy_mecha['agility'] / ( $enemy['agility'] - $your_mecha['agility'] )

Combat damage:

$your['maxdamage'] = $your_mecha['strength'] + $your_weapon['power'] - $enemy_mecha['armour']
$your['mindamage'] = $your_mecha['strength'] + $your_weapon['power'] - $enemy_mecha['armour'] + $enemy_armour['power']

( the same for the enemy )

Just a question about the hitchance here, why is the hitchance for myself for example be my agility divided by my agility take the enemys agility?
Is that the best way?
 
0
Close
Okay i have gotten it working for all but one thing, the attack_back function and the hitchance dont seem to work properly, for one thing i seem to always attack first even if the enemy hitchance is bigger and i always attack_back not the enemy?

PHP:
<?php
class attacking
{
	var $your;
	var $enemy;
	var $your_mecha;
	var $enemy_mecha;
	
	function fight($you, $them)
	{
		$get_your_mecha = mysql_query('SELECT * FROM `mecha` WHERE `owner` = "' . (int) $you . '"');
		$this->your_mecha = mysql_fetch_array($get_your_mecha);
			
		$get_enemy_mecha = mysql_query('SELECT * FROM `mecha` WHERE `owner` = "' . (int) $them . '"');
		$this->enemy_mecha = mysql_fetch_array($get_enemy_mecha);
		
		if ($this->your_mecha['armour'] <= 0)
		{
			echo 'You need to repair your Mecha before you can battle!';
		}
			
		else if ($this->enemy_mecha['armour'] <= 0)
		{
			echo 'The enemy has already been disabled!!';
		}
		
		else
		{
			
			// set your stats here
			$your_weapon = mysql_fetch_array(mysql_query('SELECT * FROM `weapon` WHERE `mecha_owner` = "' . $your_mecha['id'] . '"'));
			$your_armour = mysql_fetch_array(mysql_query('SELECT * FROM `armour` WHERE `mecha_owner` = "' . $your_mecha['id'] . '"'));
			$your_pack = mysql_fetch_array(mysql_query('SELECT * FROM `pack` WHERE `mecha_owner` = "' . $your_mecha['id'] . '"'));
				
			$this->your['strength'] = $this->your_mecha['strength'] + $your_weapon['power'];
			$this->your['armour'] = $this->your_mecha['armour'] + $your_armour['power'];
			$this->your['agility'] = $this->your_mecha['agility'] +  + $your_agility['power'];
				
			// set enemy stats here
			$enemy_weapon = mysql_fetch_array(mysql_query('SELECT * FROM `weapon` WHERE `mecha_owner` = "' . $enemy_mecha['id'] . '"'));
			$enemy_armour = mysql_fetch_array(mysql_query('SELECT * FROM `armour` WHERE `mecha_owner` = "' . $enemy_mecha['id'] . '"'));
			$enemy_pack = mysql_fetch_array(mysql_query('SELECT * FROM `pack` WHERE `mecha_owner` = "' . $enemy_mecha['id'] . '"'));
				
			$this->enemy['strength'] = $this->enemy_mecha['strength'] + $enemy_weapon['power'];
			$this->enemy['armour'] = $this->enemy_mecha['armour'] + $enemy_armour['power'];
			$this->enemy['agility'] = $this->enemy_mecha['agility'] +  + $enemy_agility['power'];
		
			// chance of hitting first
		
			$this->your['hitchance'] = $this->your_mecha['agility'] - $this->enemy_mecha['agility'];
			$this->enemy['hitchance'] = $this->enemy_mecha['agility'] - $this->your_mecha['agility'];
			
			//Combat damage:
			
			$this->your['maxdamage'] = $this->your_mecha['strength'] + $your_weapon['power'];
			$this->your['mindamage'] = $this->your_mecha['strength'] + $your_weapon['power'] / 3;
		
			//Combat damage:
			
			$this->enemy['maxdamage'] = $this->enemy_mecha['strength'] + $enemy_weapon['power'];
			$this->enemy['mindamage'] = $this->enemy_mecha['strength'] + $enemy_weapon['power'] / 3;
			
			// if your go first
			if ($this->your['hitchance'] > $this->enemy['hitchance'])
			{
				$this->attack(1);
			}
			
			// if enemy go first
			else
			{
				$this->attack(2);
			}
		}
	}
	
	// for first attack
	function attack ($first)
	{
		if ($first = 1)
		{
			// Get power of attack as a random number between min and max damage you can cause
			$power = rand($this->your['mindamage'], $this->your['maxdamage']);
			
			// take that power away from enemy armour
			$this->enemy['armour'] = $this->enemy['armour'] - $power;
			
			echo "You attack the enemy and cause {$power} damage. Enemy armour is now at {$this->enemy['armour']}<br />";
			
			if ($this->enemy['armour'] <= 0)
			{
				mysql_query("UPDATE `mecha` SET `armour` = '0' WHERE `id` = '{$this->enemy_mecha['id']}'");
				echo 'You have disabled the enemy, you have won the battle!<br />';
			}
				
			else
			{
				$this->attack_back(2);
			}
		}
		
		else if ($first = 2)
		{
			// Get power of attack as a random number between min and max damage you can cause
			$power = rand($this->enemy['mindamage'], $this->enemy['maxdamage']);
			
			// take that power away from enemy armour
			$this->your['armour'] = $this->your['armour'] - $power;
			
			echo "The enemy attacks and causes {$power} damage. Your armour is now at {$this->your['armour']}<br />";
			
			if ($this->your['armour'] <= 0)
			{
				mysql_query("UPDATE `mecha` SET `armour` = '0' WHERE `id` = '{$this->your_mecha['id']}'");
				echo 'You have disabled the enemy, you have won the battle!<br />';
			}
				
			else
			{
				$this->attack_back(1);
			}
		}
	}
	
	// for all other attacking
	function attack_back ($who)
	{
		if ($who = 1)
		{
			// Get power of attack as a random number between min and max damage you can cause
			$power = rand($this->your['mindamage'], $this->your['maxdamage']);
			
			// take that power away from enemy armour
			$this->enemy['armour'] = $this->enemy['armour'] - $power;
			
			echo "You attack the enemy again and cause {$power} damage. Enemy armour is now at {$this->enemy['armour']}<br />";
			
			if ($this->enemy['armour'] <= 0)
			{
				mysql_query("UPDATE `mecha` SET `armour` = '0' WHERE `id` = '{$this->enemy_mecha['id']}'");
				echo 'You have disabled the enemy, you have won the battle!<br />';
			}
				
			else
			{
				$this->attack_back(2);
			}
		}
		
		else if ($who = 2)
		{
			// Get power of attack as a random number between min and max damage you can cause
			$power = rand($this->enemy['mindamage'], $this->enemy['maxdamage']);
			
			// take that power away from enemy armour
			$this->your['armour'] = $this->your['armour'] - $power;
			
			echo "The enemy attacks again and causes {$power} damage. Your armour is now at {$this->your['armour']}<br />";
			
			if ($this->your['armour'] <= 0)
			{
				mysql_query("UPDATE `mecha` SET `armour` = '0' WHERE `id` = '{$this->your_mecha['id']}'");
				echo 'You have disabled the enemy, you have won the battle!<br />';
			}
				
			else
			{
				$this->attack_back(1);
			}
		}
	}
}
?>
 
0
Close
DomainEasy: white label portfolios are live. Enable yours.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back