<?
include_once('mapcfg.php');

$link = mysql_connect($D2dbhostname .":" .$D2dbport, $D2dbuser, $D2dbpass);
mysql_select_db($D2db);

$query = "SELECT MatrixSize,Matrix,AllyRatio,NeutralRatio FROM politicaltensionmatrix";
$result = mysql_query($query);
$line = mysql_fetch_array($result, MYSQL_ASSOC);
$MatrixSize = $line['MatrixSize'];
$Matrix = $line['Matrix'];
$AllyRatio = $line['AllyRatio'];
$NeutralRatio = $line['NeutralRatio'];
mysql_free_result($result);

$query = "SELECT RaceList FROM campaigninfo";
$result = mysql_query($query);
$line = mysql_fetch_array($result, MYSQL_ASSOC);
$RaceList = $line['RaceList'];
mysql_free_result($result);

mysql_close($link);

$RaceListNoArray = array();
$n=0;
$RaceListNoCount = hexdec(substr($RaceList,0,2));
for ($m=8; $m <= 8*$RaceListNoCount; $m+=8) {
	$RaceListNoArray[$n] = hexdec(substr($RaceList,$m,2));
	$n = $n + 1;
}


$AllyRatio = $AllyRatio/100;
$NeutralRatio = $NeutralRatio/100;

$neutralRange = $NeutralRatio - $AllyRatio;
$enemyRange = 1 - $NeutralRatio;

// Alllied
$partner			= $AllyRatio * 0.2;	// 0.05
$trustedAlly			= $AllyRatio * 0.4;	// 0.10
$ally				= $AllyRatio * 0.6;	// 0.15
$tradePartner			= $AllyRatio * 0.8;	// 0.20
$workingOnTradeAgreement	= $AllyRatio;		// 0.25

// Neutral
$inTradeDiscusions		= $AllyRatio + $neutralRange * 0.2;	// 0.30
$friendly			= $AllyRatio + $neutralRange * 0.6;	// 0.40
$neutral			= $NeutralRatio;			// 0.50

// War
$stressed			= $NeutralRatio + $enemyRange * 0.16;	// 0.58
$heated				= $NeutralRatio + $enemyRange * 0.26;	// 0.63
$borderSkirmishes		= $NeutralRatio + $enemyRange * 0.46;	// 0.73
$enemy				= $NeutralRatio + $enemyRange * 0.66;	// 0.83
$war				= $NeutralRatio + $enemyRange * 0.86;	// 0.93

$MatrixChunks = str_split($Matrix,8);
$MatrixDimChunk = array_shift($MatrixChunks);
$MatrixDimVal = hexdec(substr($MatrixDimChunk,0,2));
$MatrixRaceSectionsSize = ($MatrixDimVal*3)+2;
$MatrixRaceSections = array_chunk($MatrixChunks, $MatrixRaceSectionsSize);



echo '<html>
<head>
	<meta http-equiv=Content-Style-Type content=text/css>
	<link rel=stylesheet href=', $cssloc, ' type=text/css>
	<title>', $D2campname, ' Politics</title>
</head>
<body>
<br>';

echo '<center><h3><b><font color="#FFCC33">', $D2campname, ' Political Tensions Matrix</font></b></h3></center>';

echo '<table width=80% cellspacing=0 cellpadding=0 border=1 align=center>
	<tr>
		<td width="60" align=center class=windowbg2>
		<font face=Arial size=2> - </font>
		</td>
		';
	for ($m=0; $m <= $RaceListNoCount-1; $m++) {
		echo '<td width=', 100/($RaceListNoCount+1), '% align=center class=windowbg2>
			<b><font face=Arial size=2 color=', $empcolor[$RaceListNoArray[$m]], '>',
			$empstring[$RaceListNoArray[$m]],
			'</font></b>
		</td>
		';
	}
	echo '</tr>
	';

				foreach ($MatrixRaceSections as $RaceSection) {

					$RaceSectionRace = array_shift($RaceSection);
					$RaceSectionRaceNumber = hexdec(substr($RaceSectionRace,0,2));
					$RaceSectionDimChunk = array_shift($RaceSection);
					$RaceSectionDimVal = hexdec(substr($RaceSectionDimChunk,0,2));

					if (in_array($RaceSectionRaceNumber,$RaceListNoArray)) {
echo	'<tr>';

					echo '<td width=', 100/($RaceListNoCount+1), '% align=center class=windowbg2>';
					echo '<b><font face=Arial size=2 color=', $empcolor[$RaceSectionRaceNumber], '>';
					echo $empstring[$RaceSectionRaceNumber];
					echo '</font></b>';
					echo '</td>';

					$RaceSectionEntries = array_chunk($RaceSection, 3);

					foreach ($RaceSectionEntries as $RaceSectionEntryTension) {
						$RaceSectionEntryRace = array_shift($RaceSectionEntryTension);
						$RaceSectionEntryRaceNum = hexdec(substr($RaceSectionEntryRace,0,2));
						$InitialTensionVal = hexdec(substr($RaceSectionEntryTension[0],0,2)) + hexdec(substr($RaceSectionEntryTension[0],2,2))*256;
						$CurrentRelTensionValToUnpack = chr(hexdec(substr($RaceSectionEntryTension[1],0,2))).chr(hexdec(substr($RaceSectionEntryTension[1],2,2))).chr(hexdec(substr($RaceSectionEntryTension[1],4,2))).chr(hexdec(substr($RaceSectionEntryTension[1],6,2)));
						$CurrentRelTensionVal = unpack ('freltension/', $CurrentRelTensionValToUnpack);
						$CurrentRelTension = $CurrentRelTensionVal['reltension'];
				
						if (in_array($RaceSectionEntryRaceNum,$RaceListNoArray)) {
						echo '<td width=', 100/($RaceListNoCount+1), '% align=center class=windowbg2>
						<font face=Arial size=2><br>';
						if ($RaceSectionRaceNumber <> $RaceSectionEntryRaceNum) {
							echo $CurrentRelTension*100;
						} else {
							echo ' - ';
						}
						echo '</font></td>';
						}
					}
echo	'</tr>';
					}
				}
echo '</table>';

echo '<br>';
echo '<center><h3><b><font color="#FFCC33">', $D2campname, ' Political Tensions List</font></b></h3></center>';

echo '<table width=80% cellspacing=0 cellpadding=0 border=0 align=center>
	<tr>
		<td width=100% align=center class=windowbg2>
			<font face=Arial size=2><br>';

				foreach ($MatrixRaceSections as $RaceSection) {

					$RaceSectionRace = array_shift($RaceSection);
					$RaceSectionRaceNumber = hexdec(substr($RaceSectionRace,0,2));
					$RaceSectionDimChunk = array_shift($RaceSection);
					$RaceSectionDimVal = hexdec(substr($RaceSectionDimChunk,0,2));

					//if ($RaceSectionRaceNumber > 16) {
						//break;
					//}

					if (in_array($RaceSectionRaceNumber,$RaceListNoArray)) {

					echo 'The ';
					echo '<b><font color=', $empcolor[$RaceSectionRaceNumber], '>';
					echo $empstring[$RaceSectionRaceNumber];
					echo '</font></b>';
					echo '<br>
					';

					$RaceSectionEntries = array_chunk($RaceSection, 3);

					foreach ($RaceSectionEntries as $RaceSectionEntryTension) {
						$RaceSectionEntryRace = array_shift($RaceSectionEntryTension);
						$RaceSectionEntryRaceNum = hexdec(substr($RaceSectionEntryRace,0,2));
						$InitialTensionVal = hexdec(substr($RaceSectionEntryTension[0],0,2)) + hexdec(substr($RaceSectionEntryTension[0],2,2))*256;
						$CurrentRelTensionValToUnpack = chr(hexdec(substr($RaceSectionEntryTension[1],0,2))).chr(hexdec(substr($RaceSectionEntryTension[1],2,2))).chr(hexdec(substr($RaceSectionEntryTension[1],4,2))).chr(hexdec(substr($RaceSectionEntryTension[1],6,2)));
						$CurrentRelTensionVal = unpack ('freltension/', $CurrentRelTensionValToUnpack);
						$CurrentRelTension = $CurrentRelTensionVal['reltension'];

						//if ($RaceSectionEntryRaceNum > 16) {
							//break;
						//}
						
						if (in_array($RaceSectionEntryRaceNum,$RaceListNoArray)) {
						if ($RaceSectionRaceNumber <> $RaceSectionEntryRaceNum) {

							if ($CurrentRelTension < $partner) {
								echo 'is a long term partner of the ';
							} elseif ($CurrentRelTension < $trustedAlly) {
								echo 'is a highly trusted ally with the ';
							} elseif ($CurrentRelTension < $ally) {
								echo 'is a friendly ally with the ';
							} elseif ($CurrentRelTension < $tradePartner) {
								echo 'has good trade relations with the ';
							} elseif ($CurrentRelTension < $workingOnTradeAgreement) {
								echo 'is working on a trade agreement with the ';
							} elseif ($CurrentRelTension < $inTradeDiscusions) {
								echo 'is discussing the details of an alliance with the ';
							} elseif ($CurrentRelTension < $friendly) {
								echo 'is on friendly terms with the ';
							} elseif ($CurrentRelTension < $neutral) {
								echo 'has a non-aggression pact with the ';
							} elseif ($CurrentRelTension < $stressed) {
								echo 'distrusts the ';
							} elseif ($CurrentRelTension < $heated) {
								echo 'does not interact with the ';
							} elseif ($CurrentRelTension < $borderSkirmishes) {
								echo 'is having border skirmishes with the ';
							} elseif ($CurrentRelTension < $enemy) {
								echo 'is an enemy of the ';
							} elseif ($CurrentRelTension < $war) {
								echo 'is at war with the ';
							} else {
								echo 'is a mortal enemy of the ';
							}

							echo $empstring[$RaceSectionEntryRaceNum];
							//echo ' (Initial Tension: ', $InitialTensionVal, ') ', $CurrentRelTension;
							echo '<br>
							';
						}
						}
					}
					echo '<br><br>
					';
					}
				}
			
			
echo			'</font>			
		</td>
	</tr>
</table>';
echo'<br>
</body>
</html>';
?>