on http://www.rpgbattler.info
I am using no database. all sessions.
Line 175.
<?
/***************************************************************************
* battle_action.php
* -------------------
* begin : 2/24/2011
* copyright : (C) 2011 William Hughes aka Sim
* email : [email]
[email protected][/email]
*
*
*
*
***************************************************************************/
session_start();
if($_GET['action'] == "attack")
{
player_attack();
}
if($_GET['action'] == "fight")
{
reset_stats();
fight();
}
function reset_stats()
{
//player start up stats
$_SESSION['player_hp'] = 100;
$_SESSION['player_str'] = 100;
$_SESSION['player_def'] = 100;
$_SESSION['player_att'] = 100;
$_SESSION['player_dodge'] = 100;
$_SESSION['player_dmg'] = 100;
//enemy start up stats
$_SESSION['enemy_name'] = "First Oppoment";
$_SESSION['enemy_hp'] = 100;
$_SESSION['enemy_str'] = rand(1,100);
$_SESSION['enemy_def'] = rand(1,100);
$_SESSION['enemy_att'] = rand(1,100);
$_SESSION['enemy_dodge'] = rand(1,100);
$_SESSION['enemy_dmg'] = rand(1,100);
die($_SESSION['enemy_str'] . ":" . $_SESSION['enemy_def'] . ":" . $_SESSION['enemy_att'] . ":" . $_SESSION['enemy_dodge'] . ":" . $_SESSION['enemy_dmg']);
}
function fight()
{
//calculate players hit roll
if($_SESSION['player_att'] > $_SESSION['player_dodge'])
{
$_SESSION['player_hit_roll'] = $_SESSION['player_att'];
}
else
{
$_SESSION['player_hit_roll'] = $_SESSION['player_dodge'];
}
//calculate enemy hit roll
if($_SESSION['enemy_att'] > $_SESSION['enemy_dodge'])
{
$_SESSION['enemy_hit_roll'] = $_SESSION['enemy_att'];
}
else
{
$_SESSION['enemy_hit_roll'] = $_SESSION['enemy_dodge'];
}
//calculate the damagerolls
//damage roll is calculated by the str of the striker - def of the strikee
//if damage roll = less than or equal to 0 then damage roll is = 1
//this is the number that is randomized to see how much damage is inflicted
//calculate player first
$_SESSION['player_dmg_roll'] = $_SESSION['player_str'] - $_SESSION['enemy_def'];
//if negative then = 1
if($_SESSION['player_dmg_roll'] <= 0)
{
$_SESSION['player_dmg_roll'] = 1;
}
//calculate the enemy's in the same fashion
$_SESSION['enemy_dmg_roll'] = $_SESSION['enemy_str'] - $_SESSION['player_def'];
//if negative then = 1
if($_SESSION['enemy_dmg_roll'] <= 0)
{
$_SESSION['enemy_dmg_roll'] = 1;
}
//calculate the damage bonus
//this calculation subtracts the str from def, and if positve it is
//added to the damage inflicted
//calculat players damage
$_SESSION['player_dmg_bonus'] = $_SESSION['player_str'] - $_SESSION['enemy_def'];
//check if negative and convert to 0, if it is
if($_SESSION['player_dmg_bonus'] < 0)
{
$_SESSION['player_dmg_bonus'] =0;
}
//calculate the enemys in the same fashion
$_SESSION['enemy_dmg_bonus'] = $_SESSION['enemy_str'] - $_SESSION['player_def'];
//check if negative
if($_SESSION['enemy_dmg_bonus'] < 0)
{
$_SESSION['enemy_dmg_bonus'] = 0;
}
//Lastly there is the multiplyer
//this is a damage multiplyey. the str is divided by the enemys defense
//and this number is how much the damage is multiplyed
//for example, pstr = 10 and estr = 5 then the damage multiplyer = 2
//calculate players
$_SESSION['player_multiplyer'] = (int) ($_SESSION['player_str'] / $_SESSION['enemy_def']);
if($_SESSION['player_multiplyer'] = 0)
{
$_SESSION['player_multiplyer'] = 1;
}
//calculate enemys
$_SESSION['enemy_multiplyer'] = (int) ($_SESSION['enemy_str'] / $_SESSION['player_def']);
if($_SESSION['enemy_multiplyer'] = 0)
{
$_SESSION['enemy_multiplyer'] = 1;
}
$roll = rand(1,$_SESSION['player_hit_roll']);
//check to see if hit
if($roll <= $_SESSION['player_att'])
{
}//oppoment gets first strike
else
{
enemy_attack();
}
}
function player_attack()
{
//unset message
unset($_SESSION['msg']);
//the player attacks routine, basically three checks are perfomed here
//hit or miss, damage if hit, and victory
//calculate hit or miss
$roll = rand(1, $_SESSION['player_hit_roll']);
//player missed
if($roll > $_SESSION['player_att'])
{
$_SESSION['msg'] = $_SESSION['enemy_hp'] . ":You missed!!
:";
enemy_attack();
die($_SESSION['msg']);
}
//player made it this far. attack successfull
//calculate random damage
die($_SESSION['player_dmg_roll'] . '-' . $_SESSION['player_dmg_bonus']);
$roll = rand(1, $_SESSION['player_dmg_roll']);
$roll += $_SESSION['player_dmg_bonus'];
//$roll = $roll * $_SESSION['player_multiplyer'];
$_SESSION['enemy_hp'] -= $roll;
if($_SESSION['enemy_hp'] < 0)
{
$_SESSION['enemy_hp'] = 0;
}
//check to see if enemy is dead
if($_SESSION['enemy_hp'] == 0)
{
$_SESSION['msg'] = $_SESSION['enemy_hp'] . ":You have defeated the enemy!! Press Fight to begin another battle...
::";
die($_SESSION['msg']);
}
else
{
$_SESSION['msg'] = $_SESSION['enemy_hp'] . ":You hit the enemy and inflict $roll damage....
:";
}
enemy_attack();
//display messages
die($_SESSION['msg']);
}
function enemy_attack()
{
//the enemy attacks routine, basically three checks are perfomed here
//hit or miss, damage if hit, and players death
//calculate the hit or miss first
//roll number
$roll = rand(1, $_SESSION['enemy_hit_roll']);
//check to see if hit, if miss, exit sub
if($roll > $_SESSION['enemy_att'])
{//this misses and exits sub, bringing up players turn
$_SESSION['msg'] .= $_SESSION['player_hp'] . ":Enemy Misses...
:";
die($_SESSION['msg']);
}
//if it is here, it means a hit, calculate damage
//even if damage is 1 then the roll will always be 1
//calculate random damage
$roll = rand(1, $_SESSION['enemy_dmg_roll']);
//add damagebonus
$roll += $_SESSION['enemy_dmg_bonus'];
//now mutiply by multiplyer
//$roll = $roll * $_SESSION['enemy_multiplyer'];
//now subtract damage from player hp, check if negative
$_SESSION['player_hp'] -= $roll;
if($_SESSION['player_hp'] < 0)
{
$_SESSION['player_hp'] = 0;
}
//now print the hit and damage
$_SESSION['msg'] .= $_SESSION['player_hp'] . ":You have taken $roll damage....";
//check to see if player is dead
if($_SESSION['player_hp'] == 0)
{
$_SESSION['msg'] .= "$msg
You have been defeated by the enemy!! Press Fight to begin another battle..";
}
//with more studying one could put in a counterstrike, routine, but i think
//one would have to have another stat, or calculate with the bonus or roll
}
?>