Saturday, January 2, 2010

Battle to the Death!

This script automates a battle using the player and enemy stats generated. There is a chance for random blocks, evasions and random variation based upon the actual stats.

Some things to add in the next version is a check for special stats associated with a character's items or skills (burned, frozen, poisoned, scared, etc.) and apply the appropriate stat bonus or handicap during calculations.

Loot drops more in depth than what are currently in place could also be considered, with a treasure generation system based upon stats.

//Initiate Confrontation
echo "<br><br><b>Battle Phase</b><br><br> ";

//Who goes first?
$firstStrike = rand($level, $agility);
$nmeFirstStrike = rand($nmeLevel, $nmeAgility);
if($firstStrike >= $nmeFirstStrike){ $playerTurn = "Y"; } else { $playerTurn = "N"; }

//While player turn
while($playerTurn != "O"){

while($playerTurn == "Y"){
echo '<br><table border="1" bgcolor="#EEE666" text-align="left" width="50%"><tr><td style="padding: 10px;">';
//Attack Phase
//Is it dodged?
$struck = rand($level, $agility) + $agility;
$dodged = rand($nmeLevel, $nmeAgility) + $nmeLevel;

//Yes
if($dodged > $struck * 1.2){
//Output
echo "<font color=gray>Your attack was dodged!</font><br>";
$playerTurn = "N";

}
//No
else {
//Is it blocked?
$damaged = rand($level, $attack);
$blocked = rand($nmeLevel, $nmeDefense);
$totalDamage = $damaged - ($blocked * 0.6);
$totalDamage = round($totalDamage);
//Yes
if ($totalDamage <= 0){
$response = "BLOCKED";
//Output blocked
echo '<img src="img/blocked.gif" align="left"><font color=red>Your attack was <b>';
response($response);
echo "!</b></font><br>";
$playerTurn = "N";

}
//No
else {
//Calculate damage
$response = "VERB";
//Output damage
echo '<img src="img/attack.gif" align="left"><font color=blue>You <b>';
response($response);
echo "</b> the enemy for <b>".$totalDamage."</b> damage!</font><br>";

//Nme Health decreases
$nmeHealth = $nmeHealth - $totalDamage;
$nmeHelath = round($nmeHealth);

//Nme Defeated?
//Yes
if($nmeHealth <= 0){
//Output
echo "<br><i>NME Defeated!</i>";
$playerOutcome = "W";
$playerTurn = "O";
}
//No
else {
//Output enemy health remaining
echo 'The enemy now has <img src="img/nmeHealth.gif"><b>'.$nmeHealth.'</b> health.<br>';
$playerTurn = "N";
}
}

}


echo '</td></tr></table>';
}

//While enemy turn
while($playerTurn == "N"){
echo '<br><table border="1" bgcolor="#E6E6E6" text-align="left" width="50%"><tr><td style="padding: 10px;">';
//Attack Phase
//Is it dodged?
$struck = rand($nmeLevel, $nmeAgility) + $nmeAgility;
$dodged = rand($level, $agility) + $level;
//Yes
if($dodged >= $struck * 1.2){
//Output dodged
echo "<font color=gray>Enemy attack was <b>dodged!</b></font><br>";
$playerTurn = "Y";

}
//No
else {
$damaged = rand($nmeLevel, $nmeAttack);
$blocked = rand($level, $defense);
$totalDamage = $damaged - ($blocked * 0.6);
$totalDamage = round($totalDamage);

//Was it blocked?
//Yes
if ($totalDamage <= 0){
//Output blocked
$response = "BLOCKED";
echo '<img src="img/blocked.gif" align="left"><font color=blue>The Enemy attack was <b>';
response($response);
echo "!</b></font><br>";
$playerTurn = "Y";

}
//No
else {
//Calculate damage
$response = "VERB";
//Output damage
echo '<img src="img/attack.gif" align="left"><font color=red>The enemy <b>';
response($response);
echo "s</b> you for <b>".$totalDamage."</b> damage.</font><br>";

//Health decreases
$health = $health - $totalDamage;
$health = round($health);

//You is defeated?
if($health <= 0){
//Output defeated
echo "<br><i><b>You have been defeated!</b></i>";
$playerOutcome = "L";
$playerTurn = "O";
}
//You is alive
else {
//Output your health remaining
echo 'You now have <img src="img/health.gif"><b>'.$health.'</b> health.<br>';
$playerTurn = "Y";
}
}
}

}

echo '</td></tr></table>';

}


if ($playerOutcome == "W"){
//Player won, so calculate rewards.
$expGain = rand($level, $nmeLevel) * 2;
$goldGain = rand($level, $nmeTotal) / 2;

//Output rewards
echo '<font color=green><br><br><b>Your Health:</b> '.$health.'<img src="img/health.gif">';
echo '<br><b>Gold gained:</b> '.$goldGain;
echo '<br><b>EXP gained:</b> '.$expGain.'</font>';
}


if ($playerOutcome == "L"){
//Player won, so calculate rewards.
$expLost = rand($level, $nmeLevel) / 2;
$goldLost = rand($level, $nmeLevel) * 2;

//Output losses
echo '<font color=red><br><b>Gold lost:</b> '.$goldLost;
echo '<br><b>EXP lost:</b> '.$expLost.'</font>';
}

No comments:

Post a Comment