<?php
/*
 * Piloting & Gunner Generator
 * Copyright 2011 Dylan Myers
 * Licensed under the LGPL v2.1
 */

if($_GET['download'] == "true")
{
	$file = "pilots.php";
	header('Content-Description: File Transfer');
	header('Content-Type: application/octet-stream');
	header('Content-Disposition: attachment; filename='.basename($file));
	header('Content-Transfer-Encoding: binary');
	header('Expires: 0');
	header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
	header('Pragma: public');
	header('Content-Length: ' . filesize($file));
	ob_clean();
	flush();
	readfile($file);
	exit;
}

$version = "1.1";
// We *really* shouldn't need to do this since we're controlling the input, but better safe than sorry.
$post = array("action" => "none");
foreach($_POST as $key => $val)
{
	$post[trim($key)] = trim($val);
	if($key == "faction")
	{
		$post[$key] == intval($post[$key]);
	}
}
$count_array = explode(",", $post['count']);
$num_items = count($count_array);
switch($post['faction'])
{
	case 0:
	case 2:
	case 3:
		$repcheck = (($post['type'] == "ba") ? 16 : ($post['type'] == "aero") ? 2 : 4);
		break;
	case 1:
		$repcheck = (($post['type'] == "ba") ? 25 : ($post['type'] == "aero") ? 10 : 5);
		break;
	case 4:
	case 5:
		$repcheck = (($post['type'] == "ba") ? 24 : 6);
		break;
	default:
		$repcheck = (($post['type'] == "ba") ? 16 : ($post['type'] == "aero") ? 2 : 4);
		break;
}
$xp_levels = array(
	"2"		=> "green",
	"3"		=> "green",
	"4"		=> "green",
	"5"		=> "green",
	"6"		=> "regular",
	"7"		=> "regular",
	"8"		=> "regular",
	"9"		=> "regular",
	"10"	=> "veteran",
	"11"	=> "veteran",
	"12"	=> "elite",
);
$pilot = array(
	"green"		=> array(
		"0"	=> "7",
		"1"	=> "7",
		"2"	=> "6",
		"3"	=> "6",
		"4"	=> "6",
		"5"	=> "6",
		"6"	=> "5",
		"7"	=> "5",
		"8"	=> "4",
	),
	"regular"	=> array(
		"0"	=> "6",
		"1"	=> "6",
		"2"	=> "6",
		"3"	=> "5",
		"4"	=> "5",
		"5"	=> "4",
		"6"	=> "4",
		"7"	=> "3",
		"8"	=> "3",
	),
	"veteran"	=> array(
		"0"	=> "6",
		"1"	=> "5",
		"2"	=> "5",
		"3"	=> "4",
		"4"	=> "4",
		"5"	=> "3",
		"6"	=> "3",
		"7"	=> "2",
		"8"	=> "2",
	),
	"elite"		=> array(
		"0"	=> "5",
		"1"	=> "4",
		"2"	=> "4",
		"3"	=> "3",
		"4"	=> "3",
		"5"	=> "2",
		"6"	=> "2",
		"7"	=> "1",
		"8"	=> "1",
	),
);
$gun = array(
	"green"		=> array(
		"0"	=> "7",
		"1"	=> "6",
		"2"	=> "5",
		"3"	=> "5",
		"4"	=> "4",
		"5"	=> "4",
		"6"	=> "4",
		"7"	=> "4",
		"8"	=> "3",
	),
	"regular"	=> array(
		"0"	=> "5",
		"1"	=> "4",
		"2"	=> "4",
		"3"	=> "4",
		"4"	=> "4",
		"5"	=> "3",
		"6"	=> "3",
		"7"	=> "2",
		"8"	=> "2",
	),
	"veteran"	=> array(
		"0"	=> "4",
		"1"	=> "4",
		"2"	=> "4",
		"3"	=> "3",
		"4"	=> "3",
		"5"	=> "2",
		"6"	=> "2",
		"7"	=> "1",
		"8"	=> "1",
	),
	"elite"		=> array(
		"0"	=> "4",
		"1"	=> "3",
		"2"	=> "3",
		"3"	=> "2",
		"4"	=> "2",
		"5"	=> "1",
		"6"	=> "1",
		"7"	=> "0",
		"8"	=> "0",
	),
);

$out = '<html>
	<body style="background: #efefef;text-align:center;color:blue">
		<div style="width:80%;background: #ffffff;border: 1px solid #e4e4e4;margin:auto auto;padding: 20px;text-align: center;">
';

/*
 * User has filled out the form, so we're generating the pilots.
 * $post should have the following elements:
 * action: must equal do
 * faction: IS = 0, Clan = 1, and Periphery = 2
 * count: how many pilots are we making?
 * type: mech, tank, aero, dropship, naval, or wet (water navy)
 * 
 * 
 * Will output the pilots in this format (Where G is the gunner and P is the piloting):
 * Gunnery/Piloting: G/P
 * And will be in an html table
 */
$rep = 1;
if($post['action'] == "do")
{
	$out .= '		<div>
			Dylan\'s BattleTech Pilot Generator<br />
			Version: '.$version.'<br />
			<a href="mailto:ralgith@gmail.com">Contact Me</a> - <a href="pilots.php?download=true" target="_blank">Download</a><br />
		</div>
		<table border="1" style="background: #81A2C4;width:75%;margin: auto auto;border: 1px solid #0F5C8E;">
';
foreach($count_array as $count)
{
	$count = intval($count);
	$out .= '			<tr>
				<th width="75%">Type</th>
				<th width="25%">Skill</th>
			</tr>
';
	for($i = 0;$i < $count;$i++)
	{
		// Find out what experience level should be rolled for
		$rand = rand(1,6);
		$rand += rand(1,6);
		if($post['faction'] == 1)
		{
			if($post['type'] == "tank")
			{
				$rand--;
			}
			$rand++;
		}
		$rand = max(2, min(12, $rand)); // Safety, in case we're outside our parameters :/
		$level = $xp_levels[$rand];
		
		// Roll for gunnery
		$rand = rand(1,6);
		if($post['faction'] == 1)
		{
			if($post['type'] == "tank")
			{
				$rand--;
			}
			$rand++;
		}
		$rand = max(1, min(8, $rand)); // Safety, in case we're outside our parameters :/
		$G = $gun[$level][$rand];
		
		// Roll for piloting
		$rand = rand(1,6);
		if($post['faction'] == 1)
		{
			if($post['type'] == "tank")
			{
				$rand--;
			}
			$rand++;
		}
		$rand = max(1, min(8, $rand)); // Safety, in case we're outside our parameters :/
		$P = $gun[$level][$rand];
		
		// Add this pilot to the output
		$out .= "			<tr>
					<td>".ucfirst($post['type'])." Gunnery/Piloting</td>
					<td>{$G}/{$P}</td>
			</tr>
";
		
		// Divide into lances, stars, or level I as appropriate
		$rep++;
		if($rep > $repcheck)
		{
			$rep = 1;
			$out .= '			<tr><td colspan="2"></td></tr>
';
		}
	}
	$out .= '			<tr><td colspan="2"></td></tr>
			<tr><td colspan="2"></td></tr>
';
}
	$out .= '		</table><br /><br />
		<a href="pilots.php">Return to main page.</a>
';
}
/*
 * To get us started we have a nice simple form neatly wrapped in a table structure.
 */
else
{
	$out .= '		<div>
			Dylan\'s BattleTech Pilot Generator<br />
			Version: '.$version.'<br />
			<a href="mailto:ralgith@gmail.com">Contact Me</a> - <a href="pilots.php?download=true" target="_blank">Download</a><br />
			<div style="text-align:left">Usage:<br />
			I would hope most everything is self explanatory, but I\'ll explain a few quirks.<br />
			The text box for "Number of Warriors" can be filled with either a single number (ie: 155) or, with
			comma separated subdivided units (ie: 15,15,10,12 would get me two trinaries, a binary, and a company).
			The program further subdivides based on unit type as well. Into stars, lances, or level Is as the program
			deems appropriate. Which leads to the next quirk, for battle armor you do not put in your warriors by
			numbers of points/squads in a star/lance, you put in the total number for the whole star/lance. Which means
			for two binaries of clan elementals you would put 50,50 in the warriors box in order to get two stars of
			warriors in each binary. Make sure you put in the proper number of warriors for the unit type & divisions
			you\'re using or you\'ll just confuse yourself. I haven\'t implemented tank crews this way, or added
			"infantry" at all, because there are variable numbers of soldiers in each of those. Enjoy!<br /><br />
			</div>
		</div>
		<form name="input" action="pilots.php" method="post">
			<table border="1" style="background: #81A2C4;width:75%;margin: auto auto;border: 1px solid #0F5C8E;">
				<tr>
					<td width="75%">Number of Warriors:</td>
					<td width="25%"><input type="text" name="count" /></td>
				<tr>
				<tr>
					<td>Faction:</td>
					<td>
						<input type="radio" name="faction" value="0" /> Inner Sphere<br />
						<input type="radio" name="faction" value="1" /> Clan<br />
						<input type="radio" name="faction" value="2" /> Periphery<br />
						<input type="radio" name="faction" value="2" /> Mercenary<br />
						<input type="radio" name="faction" value="2" /> Comstar<br />
						<input type="radio" name="faction" value="2" /> Word of Blake<br />
					</td>
				</tr>
				<tr>
					<td>Type:</td>
					<td>
						<input type="radio" name="type" value="mech" /> Mech<br />
						<input type="radio" name="type" value="tank" /> Tank<br />
						<input type="radio" name="type" value="aero" /> Aerospace<br />
						<input type="radio" name="type" value="dropship" /> Dropship<br />
						<input type="radio" name="type" value="naval" /> Naval<br />
						<input type="radio" name="type" value="wet" /> Wet Naval<br />
						<input type="radio" name="type" value="ba" /> Battle Armor<br />
					</td>
				</tr>
			</table>
			<input type="hidden" name="action" value="do" />
			<div style="text-align:center"><input type="submit" value="Submit" /></div>
		</form>
';
}
$out .= "		</div>
";
// Finally, lets spit out our input!
echo $out;
if(isset($debug))
{
	echo "Debugging:<br />
<pre>
";
print_r($debug);
echo "</pre>
";
}
echo "	</body>
</html>";
?>
