Location: PHPKode > projects > Blackbeard's Revenge > battle.php
<?php
session_start(); 
//header("Cache-control: private");

require_once "headers.php";
require_once "common.php";
//  --------------------------------------------------
//  |  Blackbeard's Revenge                          |
//  |  Written By:  Brian Austin                     |
//  |  Contact at:  hide@address.com         |
//  |  CopyLeft 2003								 |
//  |  Module:  Battle                               |
//  |  Description:                                  |
//  |    This module handles ship to ship combat and |
//  |    treasure map discovery.                     |
//  --------------------------------------------------

//Initalize timer
//smt_rand((double)microtime()*1000000);

//echo "<html>";
//echo "<body text=#FFFFFF bgcolor=#000000 alink=#F0F0F0 vlink =#FAFAFA>";

//The Attack round where ships fire at each other and take damage
if($submit == "Fire")
{
	
	//reset all variables
	$attack = true;
	$tohit1 = false;
	$attack1 = 0;
	$tohit2 = false;
	$attack2 = 0;
		
	//Do player attack first

	$temp1 = mt_rand(0, 50);
	$temp2 = (($_SESSION[enemy]["basemanuver"]) + ($_SESSION[enemy]['basesailing'] - $_SESSION[user]["gunnery"])/4);

	if($DEBUG)
		echo $temp1." >< ".$temp2." = ((".$_SESSION[enemy]["basemanuver"].") + (".$_SESSION[enemy]['basesailing']." - ".$_SESSION[user]["gunnery"].")/4)";

	if($temp1 > $temp2)
	{	

		$tohit1 = true;
		if(($_SESSION[user]["crew"]/3) >= $_SESSION[user]["cannon"]) 
			$gunsfired1 = $_SESSION[user]["cannon"];
		else 
			$gunsfired1 = $_SESSION[user]["crew"]/3;

		settype($gunsfired1, "integer");

		$dmgpergun1 =(1+($_SESSION[user]["gunnery"]/8));
		//settype($dmgpergun1, "integer");

		$attack1 = (($gunsfired1*$dmgpergun1)-($_SESSION[enemy]["basearmour"] /2));
		settype($attack1, "integer");
		if($DEBUG)
			echo "&nbsp;&nbsp;&nbsp;&nbsp;".$attack1." = ((".$gunsfired1."*".$dmgpergun1.")-(".$_SESSION[enemy]["basearmour"]."/2))";

	}
	
	//Do enemy attack

	$temp1 = mt_rand(0, 50);
	$temp2 = (($_SESSION[user]["manuver"]) + ($_SESSION[user]["sailing"] - $_SESSION[enemy]['basegunnery'])/4);

	if($DEBUG)
		echo "<br>".$temp1." >< ".$temp2." = ((".$_SESSION[user]["manuver"].") + (".$_SESSION[user]["sailing"]." - ".$_SESSION[enemy]['basegunnery'].")/4)";

	if($temp1 > $temp2){	
		
		$tohit2 = true;
		//if(($_SESSION['ShipMaxCrew']/3) >= $_SESSION[enemy]["basecannon"]) 
			$gunsfired2 = $_SESSION[enemy]["basecannon"];
		//else 
		//	$gunsfired2 = $_SESSION['ShipMaxCrew']/3;
		settype($gunsfired2, "integer");

		$dmgpergun2 =(1+($_SESSION[enemy]['basegunnery']/8));
		//settype($dmgpergun2, "integer");

		$attack2 = (($gunsfired2*$dmgpergun2)-($_SESSION[user]["armour"]/2));
		settype($attack2, "integer");
		if($DEBUG)
			echo "&nbsp;&nbsp;&nbsp;&nbsp;".$attack2." = ((".$gunsfired2."*".$dmgpergun2.")-(".$_SESSION[enemy]["basearmour"]."/2))";
	}
	if($DEBUG) echo "<br>";
	//Keep attack values from going negative
	//if($DEBUG) echo "attack1 " . $attack1 ." -";
	//if($DEBUG) echo "attack2 " . $attack2 ." <br>";

	if($attack1 < 0) $attack1 = 0;
	if($attack2 < 0) $attack2 = 0;

	settype($attack1, "integer");
	settype($attack2, "integer");	

	$_SESSION[enemy]["actualhull"] = ($_SESSION[enemy]["actualhull"] - $attack1);
	$_SESSION[user]["hull"] = ($_SESSION[user]["hull"] - $attack2);

	//Keep hitpoints from going negative
	if($_SESSION[enemy]["actualhull"] < 0) $_SESSION[enemy]["actualhull"] = 0;
	if($_SESSION[user]["hull"] < 0) $_SESSION[user]["hull"] = 0;

	DisplayStats();

	// if target ship is at 0 HP and player is attacking, give plunder
	if($_SESSION[enemy]["actualhull"] <= 0 && $attack) 
	{
		$booty = mt_rand(15,$_SESSION[enemy]["maxbooty"]);
		$_SESSION[user]["gold"] = $_SESSION[user]["gold"] + $booty;
		$XP = mt_rand((int)($_SESSION[enemy]["maxexperience"]*.5), $_SESSION[enemy]["maxexperience"]);
		$_SESSION[user]["experience"] = $_SESSION[user]["experience"] + $XP;
		DisplayMessage("The " . $_SESSION[enemy]["class"] . " surrenders.  <br>You plunder " . $booty . " gold.<br>You earn ". $XP . " experience.");
		$_SESSION[enemy]["actualhull"] = 0;
		$attack = false;
		if($DEBUG) echo "Saving player info";
		SavePlayerShip();
		ClearEnemyShip();
	}
	// user's ship has been destroyed.  They lose all their gold.
	elseif($_SESSION[user]["hull"] <= 0)
	{
		DisplayMessage("Your ship sank mate,<br> you lose all your gold.<br>");
		$_SESSION[user]["gold"] = 0;
		$attack = false;
		$_SESSION[user]["turns"] = 0;
		SavePlayerShip();
		ClearEnemyShip();
	}
	else
	{
		//Otherwise, continue the attack
		//eventually these will be externalized with multiple attack/defend messages
		if($tohit1)
			$attackmsg = "You blast the " . $_SESSION[enemy]["class"] . " with a salvo of cannon fire.<br>";
		else
			$attackmsg = "Your cannon fire bounces harmlessly off the " . $_SESSION[enemy]["class"] . "<br>";

		if($tohit2)
		    $defendmsg = "The " . $_SESSION[enemy]["class"] . "'s cannon fire rips into your ship!!" . "<br>";
		else
			$defendmsg = "The " . $_SESSION[enemy]["class"] . "'s cannon fire bounces off your ship!!" . "<br>";

		DisplayMessage($attackmsg . $defendmsg);
	}
}
//Either escape or search for another ship to attack
elseif($submit == "Sail Away")
{
	$escape = mt_rand(0, 100)-($_SESSION[user]["sailing"] / $_SESSION[user]['level']);
	$enemyship = $_SESSION[enemy]["class"];
	//echo "Sail Away " .$escape . " ";
	if($escapep < 30 || $attack == false)
	{
		//Player was not engaqed so it was easier to escape
		ClearEnemyShip();
		SavePlayerShip();
		DisplayStats();
		$attack = false;
		DisplayMessage("You sail off before the ". $enemyship . " spots you...");
	}
	else if($escape < 15)
	{
		//Player was engaged but escaped anyway
		ClearEnemyShip();
		SavePlayerShip();
		DisplayStats();
		$attack = false;
		DisplayMessage("You break off the attack and manage to escape.");
	}
	else
	{
		//Player could not escape and got hammered by cannonfire
		$attack2 = mt_rand(0,$_SESSION[enemy]["basecannon"]) - $_SESSION[user]["armour"] ;
		
		//Keep attack values from going negative
		if($attack2 < 0) $attack1 = 0;
		$_SESSION[user]["hull"] = ($_SESSION[user]["hull"] - $attack2);

		DisplayStats();
		EscapeFailed();			
	}
}
//Repair the damage to the players ship
elseif($submit == "Repair")
{
	//User wants to repair ship
	$rate = mt_rand(5,15);
	$diff = $_SESSION[ships]['ShipBaseHull'][$_SESSION[user]["shiptype"]] - $_SESSION[user]["hull"];
	$cost = $diff * $rate;
	$repair = (int) (100 - (($_SESSION[user]["hull"] / $_SESSION[ships]['ShipBaseHull'][$_SESSION[user]["shiptype"]])*100));
	if($_SESSION[user]["gold"] > $rate && $_SESSION[user]["turns"] > 0)
	{
		if($_SESSION[user]["gold"] > $cost)
		{
			$_SESSION[user]["gold"] = $_SESSION[user]["gold"] - $cost;
			$_SESSION[user]["hull"] = $_SESSION[ships]['ShipBaseHull'][$_SESSION[user]["shiptype"]];			
		}
		else
		{
			$diff = (int) ($_SESSION[user]["gold"] / $rate);
			$_SESSION[user]["hull"] = $_SESSION[user]["hull"] + $diff;
			$_SESSION[user]["gold"] = $_SESSION[user]["gold"] - ($diff * $rate);			
		}

		

		DisplayStats();
		DisplayMessage("You repair " . $repair . "% hull damage for " . $cost . " gold");
		$_SESSION[user]["turns"] = $_SESSION[user]["turns"] - 1;
		SavePlayerShip();
		ClearEnemyShip();		
	}
	elseif($_SESSION[user]["turns"] < 1)
	{
		DisplayStats();
		DisplayMessage("You do not have turns to repair your ship.");
	}
	elseif($_SESSION[user]["hull"])
	{
		$_SESSION[user]["hull"] = $_SESSION[user]["hull"] + mt_rand(1,5);
		if($_SESSION[user]["hull"] > $_SESSION[ships]['ShipBaseHull'][$_SESSION[user]["shiptype"]]) $_SESSION[user]["hull"] = $_SESSION[ships]['ShipBaseHull'][$_SESSION[user]["shiptype"]];
		DisplayStats();
		DisplayMessage("You do not have enough gold to repair your ship<br> You limp into port and make minmal repairs");
		$_SESSION[user]["turns"] = $_SESSION[user]["turns"] - 3;
		if($_SESSION[user]["turns"] < 0) $_SESSION[user]["turns"] = 0;
		SavePlayerShip();
		ClearEnemyShip();
	}
	$attack = false;
	
}
elseif($submit == "Quit")
{
	//Log the user out
	SavePlayerShip();
	ClearEnemyShip();
	echo "<META HTTP-EQUIV=\"refresh\" content=\"1;URL=main.php?module=0\">";
	//echo "Return to the ";
	//setcookie( session_name() ,"",0,"/");
	//session_unset();
	//session_destroy();
	
}
elseif($submit == "Find Ship" && $_SESSION[user]["turns"] > 0)
{
	$shipmax = SHIPMAX;
	LoadPlayer($_SESSION[user]["uid"]);
	$_SESSION[enemy]["shipnum"] = mt_rand(1, (int) $shipmax);
	LoadEnemyShip($_SESSION[enemy]["shipnum"]);
	$target = $_SESSION[enemy]["class"];
	$ShipActualMaxHull  = mt_rand($_SESSION[ships]['ShipBaseHull'],$_SESSION['ShipMaxHull']);

	$_SESSION[user]["turns"] = $_SESSION[user]["turns"] - 1;

	$_SESSION[enemy]["actualhull"] = $ShipActualMaxHull;
	//$_SESSION[user]["hull"]  = mt_rand(5, $_SESSION[ships]['ShipBaseHull'][$_SESSION[user]["shiptype"]]);	
	DisplayStats();	
	DisplayMessage("You spy a " . $_SESSION[enemy]["class"] . " on the horizon and move in for the kill");
}
elseif($_SESSION[user]["turns"] == 0 && $attack == false)
{
	$attack = false;
	DisplayStats();
	DisplayMessage("Your out of turns for today.");
	SavePlayerShip();
	ClearEnemyShip();
}
elseif($_SESSION[user]["hull"] < 1)
{
	DisplayStats();
	DisplayMessage("Your ship sank, your outa luck until tomorrow");
	$_SESSION[user]["turns"] = 0;
	$attack = false;
	SavePlayerShip();
	ClearEnemyShip();
}
else
{
	LoadPlayer($_SESSION[user]["uid"]);
	DisplayStats();
	DisplayMessage("Click Find Ships to search for ships");
}
?>

<?if($submit != "Quit")
{?>
	<table class="data" style="width: 620px">
	<tr><td class="body" style="width: 50; color:#FFFF00; text-align:center; font-weight: bold">(O)</td><td style="width: 300">
	<form action="main.php?module=1" method="POST">
	<input type="hidden" name="firsttime" value=1>
	<center>
	<?php if($submit == "Find Ship" || $attack == true) echo "<input class=\"button\" type=\"Submit\" name=\"submit\" value=\"Fire\">"; ?>
	<?php if($submit == "Find Ship" || $attack == true) echo "<input class=\"button\" type=\"Submit\" name=\"submit\" value=\"Sail Away\">";
	      elseif ($_SESSION[user]["turns"] > 0) echo "<input class=\"button\" type=\"Submit\" name=\"submit\" value=\"Find Ship\">"; ?>
	<?php if($attack == false && $submit != "Find Ship" && $_SESSION[user]["turns"] > 0) echo "<input class=\"button\" type=\"Submit\" name=\"submit\" value=\"Repair\">"; ?>	
	<?php if($attack == false ) echo "<input class=\"button\" type=\"Submit\" name=\"submit\" value=\"Quit\">"; ?>
	</center>
	<input type="hidden" name="module" value=1>
	<input type="hidden" name="firsttime" value=0>
	</form>
	</td>
	<tr>
	<td class="body" style="width: 50; color:#FFFF00; text-align:center; font-weight: bold">Gold</td>
	<td class="body" cellpadding = 10 style="color:#FFFF00; text-align:left"><?php echo $_SESSION[user]["gold"] ?></td>
  	</tr><tr>
	<td class="body" style="width: 50; color:#FFFFFF; text-align:center; font-weight: bold">Turns</td>
	<td class="body" cellpadding = 10 style="color:#FFFFFF; text-align:left"><?php echo $_SESSION[user]["turns"] ?></td>
	</tr><tr>
	<td class="body" style="width: 50; color:#FFFFFF; text-align:center; font-weight: bold">Experience</td>
	<td class="body" cellpadding = 10 style="color:#FFFFFF; text-align:left"><?php echo $_SESSION[user]["experience"] ?></td>
	</tr>
	</table></td></tr></table>
<?php
}
else
{
	//echo "<a href=\"index.html\">Mainpage</a>";
}


function DisplayStats()
{
	if($_SESSION[enemy]["shipnum"] != 0)
		$hp1font = "statusgreen";
	else
		$hp1font = "statusnone";

	$hp2font = "statusgreen";

	if($_SESSION[enemy]["shipnum"] == 0) $opponentshipname = "No ship sighted";
	else $opponentshipname = $_SESSION[enemy]["class"];

	//Determine font colors for HP
	if($_SESSION['ShipActualHull'] < ($_SESSION['opponentshipmaxhull'] * .35) && $_SESSION['ShipActualHull'] > 0) $hp1font = "statusyellow";
	elseif($_SESSION['ShipActualHull'] == 0 && $_SESSION[enemy]["shipnum"] != 0) $hp1font = "statusred";

	if($_SESSION[user]["hull"] < ($_SESSION[ships]['ShipBaseHull'][$_SESSION[user]["shiptype"]] * .35) && $_SESSION[user]["hull"] > 0) $hp2font = "statusyellow";
	elseif($_SESSION[user]["hull"] == 0) $hp2font = "statusred";

	
	//Avoid division by Zero error
	if($_SESSION['ShipActualHull'] < 1) $targetpercent = 0;
	else $targetpercent = (int) (($_SESSION['ShipActualHull']/$_SESSION[ships][$_SESSION[enemy]["shipnum"]]['maxhull'])*100);

	if($_SESSION[user]["hull"]  < 1) $playerpercent = -10;
	else $playerpercent = (int) (($_SESSION[user]["hull"] /$_SESSION[ships][$_SESSION[user]["shiptype"]]['maxhull'])*100);

  //Displays both Ship Stats
	echo "<table class=\"menu\">";
	echo "<tr><td colspan=2 class=\"menuheader\" style=\"height:25px\">Search for Ships</td></tr>";
    echo "<tr><td><table class=\"data\" style=\"width:620px;border-bottom:1px solid black\"><tr></td>";
	echo "<td class=\"body\"><table class=\"data\" style=\"width: 300px\"><tr><td class=\"header\" style=\"align: center\" colspan=2>Ship-to-Ship Battle</td></tr>";
	echo "<tr><td class=\"". $hp1font ."\" style=\"width: 100px; text-align:left\" colspan=2>" . $opponentshipname . "</td></tr><tr>";
	echo "<td class=\"". $hp1font ."\">Hull Strength</td><td class=\"". $hp1font ."\" style=\"width: 50px; text-align:right\">" . $targetpercent  ."%</td></tr><tr>";
	echo "<td class=\"". $hp2font ."\" style=\"text-align:left\" leftcolspan=2>The " . $_SESSION['myshipname'] . "</td></tr><tr>";
	echo "<td class=\"". $hp2font ."\">Hull Strength</td><td  class = \"". $hp2font ."\" style=\"text-align:right\">" . $playerpercent ."%</td></tr></table></td><td class=\"data\">";
	include ("shipstats.php");	
	echo "</td></tr></table>";
}

function EscapeFailed()
{
	//Display message when user tries to escape but can not.
	if($_SESSION[user]["hull"] <= 0)
	{
		$failure = "You get nailed trying to flee and your ship sinks.<br> You lose all your gold.<br>";
		$_SESSION[user]["gold"] = 0;
	}
	else
	{
		$failure = "You fail to escape!!!<br>";
		if($attack2 == 0)
		{
			$message = "By some stroke of luck, the " . $shipname . " misses!!" . "<br>";
		}
		if($attack2 > 0)
		{
			$message = "The " . $shipname . " broadsides you with cannon fire.<br>";
		}
		
		$failure = $failure . $message;
	}	

	DisplayMessage($failure);
}


include("footer.php");
?>
Return current item: Blackbeard's Revenge