<?php
include_once('mapcfg.php');

if($pagecomp == 1){
ob_start("ob_gzhandler");
}

if($usepost == 1){
$zoom = $_POST["zoom"];
$mtype = $_POST["mtype"];
$layer = $_POST["layer"];
}

//if($zoom = ""){
//$zoom = 4;
//$mtype = "Terrain";
//$layer = "Empire";
//}

echo ("<html>");
echo ("<head>");
echo ("<meta http-equiv=Content-Style-Type content=text/css>");
echo ("<link rel=stylesheet href=$cssloc type=text/css>");
echo ("<LINK REL='SHORTCUT ICON' href='favicon.ico'>");

if($mtype == "Economy" or $mtype == "Defence" or $mtype == "Battles"){
	echo ("<title>$dbdecarray[campaignname] Webmap - $layer $mtype</title>");
} else {
	echo ("<title>$dbdecarray[campaignname] Webmap - $layer</title>");
}

echo ("</head>");
echo ("<body>");

$x = $dbdecarray['mapnumcolumns'];
$y = $dbdecarray['mapnumrows'];
	
			
$map_x = array();
$map_y = array();
$control = array();
$terrain = array();
$planet = array();
$base = array();
$econ = array();
$dv = array();	


for ($i = 1; $i <= $x*$y; $i++) {

	$map_x[$i] = $dbdecarray['maphex'.$i.'column'];
	$map_y[$i] = $dbdecarray['maphex'.$i.'row'];
	$terrain[$i] = $dbdecarray['maphex'.$i.'terrain'];
	$planet[$i] = $dbdecarray['maphex'.$i.'planet'];
	$base[$i] = $dbdecarray['maphex'.$i.'base'];
	$econ[$i] = $dbdecarray['maphex'.$i.'currecon'];

	if ($layer == "Empire"){
		$control[$i] = $dbdecarray['maphex'.$i.'race'];
		$dv[$i] = $dbdecarray['maphex'.$i.'currdv'];
	}
	if ($layer == "Cartel"){
		$control[$i] = $dbdecarray['maphex'.$i.'cartel'];
		$dv[$i] = $dbdecarray['maphex'.$i.'cartelcurrdv'];
	}
	
}
		
array_multisort($map_x, $map_y, $terrain, $planet, $base, $econ, $control, $dv);


$maxecon = max($econ);
$maxdv = max($dv);

$terrstring = array();
$terrstring[1] = "Normal Space";
$terrstring[2] = "Normal Space";
$terrstring[4] = "Normal Space";
$terrstring[8] = "Normal Space";
$terrstring[16] = "Normal Space";
$terrstring[32] = "Normal Space";
$terrstring[64] = "Asteroids";
$terrstring[128] = "Asteroids";
$terrstring[256] = "Asteroids";
$terrstring[512] = "Asteroids";
$terrstring[1024] = "Asteroids";
$terrstring[2048] = "Asteroids";
$terrstring[4096] = "Nebula"; //Blue
$terrstring[8192] = "Nebula"; //Red
$terrstring[16384] = "Nebula"; //Green
$terrstring[32768] = "Nebula";
$terrstring[65536] = "Nebula";
$terrstring[131072] = "Nebula";
$terrstring[262144] = "Black Hole"; 
$terrstring[524288] = "Black Hole";
$terrstring[1048576] = "Black Hole";
$terrstring[2097152] = "Black Hole"; 
$terrstring[4194304] = "Black Hole"; 
$terrstring[8388608] = "Black Hole";
$terrstring[16777216] = "Dustclouds";
$terrstring[33554432] = "Shipping Lane";
$terrstring[67108864] = "Normal Space";

$planetstring = array();
$planetstring[0] = "";
$planetstring[1] = "Homeworld 1";
$planetstring[2] = "Homeworld 2";
$planetstring[4] = "Homeworld 3";
$planetstring[8] = "Core World 1";
$planetstring[16] = "Core World 2";
$planetstring[32] = "Core World 3";
$planetstring[64] = "Colony 1";
$planetstring[128] = "Colony 2";
$planetstring[256] = "Colony 3";
$planetstring[512] = "Asteroid Base 1";
$planetstring[1024] = "Asteroid Base 2";
$planetstring[2048] = "Asteroid Base 3";

$basestring = array();
$basestring[0] = "";
$basestring[1] = "Starbase";
$basestring[2] = "Battle Station";
$basestring[4] = "Base Station";
$basestring[8] = "Weapons Platform";
$basestring[16] = "Listening Post";

$imgwidth=122/$zoom;
$imgheight=104/$zoom;
	
$tx=2/$zoom;
$ty=2/$zoom;
$ihex=0;
for ($t = 0; $t <= $x-1; $t++) {
	for ($u = 0; $u <= $y-1; $u++) {
		if($t/2 == (int)($t/2)) {

			echo makehexhtml ($tx, $ty, $terrain[$ihex], $planet[$ihex], $base[$ihex], $control[$ihex], $map_x[$ihex], $map_y[$ihex], $empstring[$control[$ihex]], $econ[$ihex], $dv[$ihex], $terrstring[$terrain[$ihex]], $planetstring[$planet[$ihex]], $basestring[$base[$ihex]], $imgwidth, $imgheight, $mtype, $maxecon, $maxdv, $layer, $hexname[$map_x[$ihex]][$map_y[$ihex]], $hexregion[$map_x[$ihex]][$map_y[$ihex]],$showbases,$showplanets);
			
			$ihex=$ihex+1;		
		}

		if($t/2 != (int)($t/2)) {
			$ty=$ty+53/$zoom;

			echo makehexhtml ($tx, $ty, $terrain[$ihex], $planet[$ihex], $base[$ihex], $control[$ihex], $map_x[$ihex], $map_y[$ihex], $empstring[$control[$ihex]], $econ[$ihex], $dv[$ihex], $terrstring[$terrain[$ihex]], $planetstring[$planet[$ihex]], $basestring[$base[$ihex]], $imgwidth, $imgheight, $mtype, $maxecon, $maxdv, $layer, $hexname[$map_x[$ihex]][$map_y[$ihex]], $hexregion[$map_x[$ihex]][$map_y[$ihex]],$showbases,$showplanets);

			$ihex=$ihex+1;
			$ty=$ty-53/$zoom;
		}
	$ty=$ty+106/$zoom;
	}
$ty=2/$zoom;
$tx=$tx+94/$zoom;
}

function makehexhtml ($FNtx, $FNty, $FNterrain, $FNplanet, $FNbase, $FNcontrol, $FNmap_x, $FNmap_y, $FNempstring, $FNecon, $FNdv, $FNterrstring, $FNplanetstring, $FNbasestring, $FNimgwidth, $FNimgheight, $FNmtype, $FNmaxecon, $FNmaxdv, $FNlayer, $FNhexname, $FNhexregion,$FNshowbases,$FNshowplanets) {

	$hexhtml = "";

	if($FNmtype == "Terrain"){
	if($FNterrain == 67108864 or $FNterrain == 1 or $FNterrain == 2 or $FNterrain == 4 or $FNterrain == 8 or $FNterrain == 16 or $FNterrain == 32){
	$hexhtml .= "<img border=0 src=NormalSpace.gif width=$FNimgwidth height=$FNimgheight ";
	}
	if($FNterrain == 64 or $FNterrain == 128 or $FNterrain == 256 or $FNterrain == 512 or $FNterrain == 1024 or $FNterrain == 2048){
	$hexhtml .= "<img border=0 src=Asteroid3.gif width=$FNimgwidth height=$FNimgheight ";
	}
	if($FNterrain == 4096){
	$hexhtml .= "<img border=0 src=NebulaBlue.gif width=$FNimgwidth height=$FNimgheight ";
	}
	if($FNterrain == 8192){
	$hexhtml .= "<img border=0 src=NebulaRed.gif width=$FNimgwidth height=$FNimgheight ";
	}
	if($FNterrain == 16384 or $FNterrain == 32768 or $FNterrain == 65536 or $FNterrain == 131072){
	// need to split into last three nebula types
	$hexhtml .= "<img border=0 src=NebulaGreen.gif width=$FNimgwidth height=$FNimgheight ";
	}
	if($FNterrain == 262144 or $FNterrain == 524288 or $FNterrain == 1048576 or $FNterrain == 2097152 or $FNterrain == 4194304 or $FNterrain == 8388608){
	// need to split into other black hole images
	$hexhtml .= "<img border=0 src=BlackHole.gif width=$FNimgwidth height=$FNimgheight ";
	}
	if($FNterrain == 33554432){
	$hexhtml .= "<img border=0 src=ShippingLane.gif width=$FNimgwidth height=$FNimgheight ";
	}
	if($FNterrain == 16777216){
	// dustclouds - same image as normal space in game
	$hexhtml .= "<img border=0 src=NormalSpace.gif width=$FNimgwidth height=$FNimgheight ";
	}			
	$hexhtml .= "style='position: absolute; left:";
	$hexhtml .= $FNtx;
	$hexhtml .= "; top:";
	$hexhtml .=  $FNty;
	$hexhtml .= "; z-index: 1'>";
	}

	if($FNmtype == "Economy"){		
	$hexhtml .= "<img border=0 src=hexUL";
	$hexhtml .= round(($FNecon/$FNmaxecon)*5);
	$hexhtml .= ".gif width=$FNimgwidth height=$FNimgheight ";
	$hexhtml .= "style=\"position: absolute; left:";
	$hexhtml .= $FNtx;
	$hexhtml .= "; top:";
	$hexhtml .= $FNty;
	$hexhtml .= "; z-index: 1\">";
	}

	if($FNmtype == "Defence"){		
	$hexhtml .= "<img border=0 src=hexUL";
	$hexhtml .= round(($FNdv/$FNmaxdv)*5);
	$hexhtml .= ".gif width=$FNimgwidth height=$FNimgheight ";
	$hexhtml .= "style=\"position: absolute; left:";
	$hexhtml .= $FNtx;
	$hexhtml .= "; top:";
	$hexhtml .= $FNty;
	$hexhtml .= "; z-index: 1\">";
	}

	if($FNshowplanets == 1){
	if($FNplanet == 1 or $FNplanet == 2 or $FNplanet == 4 or $FNplanet == 8 or $FNplanet == 16 or $FNplanet == 32 or $FNplanet == 64 or $FNplanet == 128 or $FNplanet == 256 or $FNplanet == 512 or $FNplanet == 1024 or $FNplanet == 2048){	
	$hexhtml .= "<img border=0 src=Planet";
	$hexhtml .= $FNcontrol;
	$hexhtml .= ".gif width=$FNimgwidth height=$FNimgheight ";
	$hexhtml .= "style='position: absolute; left:";
	$hexhtml .= $FNtx;
	$hexhtml .= "; top:";
	$hexhtml .= $FNty;
	$hexhtml .= "; z-index: 2'>";
	}
	}

	if($FNshowbases == 1){
	if($FNbase == 1 or $FNbase == 2 or $FNbase == 4 or $FNbase == 8 or $FNbase == 16){
	$hexhtml .= "<img border=0 src=Starbase";
	$hexhtml .= $FNcontrol;
	$hexhtml .= ".gif width=$FNimgwidth height=$FNimgheight ";
	$hexhtml .= "style='position: absolute; left:";
	$hexhtml .= $FNtx;
	$hexhtml .= "; top:";
	$hexhtml .= $FNty;
	$hexhtml .= "; z-index: 3'>";
	}
	}

	$hexhtml .= "<img border=0 src=hex";
	$hexhtml .= $FNcontrol;
	$hexhtml .= ".gif width=$FNimgwidth height=$FNimgheight title=\"";

	$altstring = $FNmap_x ."," .$FNmap_y .": " .$FNempstring ."\n Economy: " .$FNecon;
	if ($FNlayer == "Empire"){
		$altstring .= "\n Defence Value: ";
	}
	if ($FNlayer == "Cartel"){
		$altstring .= "\n Defence Value: ";
	}
	$altstring .= $FNdv ."\n Terrain: " .$FNterrstring;
	if($FNshowplanets == 1){
	if ($FNplanetstring <> ""){
 		$altstring .= "\n Planet: " .$FNplanetstring;
	}
	}
	if($FNshowbases == 1){
	if ($FNbasestring <> ""){
		$altstring .= "\n Base: " .$FNbasestring; 
	}
	}
	if ($FNhexname <> ""){
		$altstring .= "\n Name: " .$FNhexname;
	}
	if ($FNhexregion <> ""){
		$altstring .= "\n Region: " .$FNhexregion;
	}

	$hexhtml .= $altstring;
	$hexhtml .= "\"";
	$hexhtml .= " style='position: absolute; left:";
	$hexhtml .= $FNtx;
	$hexhtml .= "; top:";
	$hexhtml .= $FNty;
	$hexhtml .= "; z-index: 6'>";

    	return $hexhtml;
}

echo ("</body>");
echo ("</html>");
?>