This is how i would do it :
This is not tested- just written to give you a idea
PHP Code:
<?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
$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
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'] . '"'));
*/
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?
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 by jessemiller : 03-19-2008 at 12:04 AM.
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?
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 Code:
<?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'] . '"'));
// 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->