<? include $DOCUMENT_ROOT."/zzzfilelist.inc"; ?>
<? if (!$dont_inc_general) { include Inc("functions_general.inc.php"); } ?>
<? if (!$dont_inc_images) {  include Inc("functions_images.inc.php"); } ?>
<? include Inc("metoo_loc.inc.php"); ?>
<? include Inc("metoo.inc.php"); ?>
<html>
<head><title>MeToo : Middle-Earth Tool for Overcoming Opposition</title></head>
<body>
<?
	if (!$goingtologin) { include Inc("testlogin.php");	}
?>
<? include Inc("checkgameaccess.inc.php"); ?>
<?
	$fn=Inc("mapdata.csv");
	$fp = fopen ($fn, "r");
	$mapcontent = fread($fp, filesize($fn));
	fclose($fp);
	Decrunch($mapcontent);
	$mapcontentcell=$cell;
	$fn=Inc("mapriver.csv");
	$fp = fopen ($fn, "r");
	$mapriver = fread($fp, filesize($fn));
	fclose($fp);
	Decrunch($mapriver);
	$mapriver=$cell;
	$fn=Inc("maproad.csv");
	$fp = fopen ($fn, "r");
	$maproad = fread($fp, filesize($fn));
	fclose($fp);
	Decrunch($maproad);
	$maproad=$cell;
	
	$game_checked=$game;
?>
<? include Inc("loaddata.inc.php"); ?>
<?
	$fortiftop=(int)($mapcell-($mapcell/4)-$imfortifsize);
	$fortifleft=(int)(($mapcell-$imfortifsize)/2);
	$fortifright=(int)(($mapcell-$imfortifsize)/2);
	$towntop=(int)($mapcell-($mapcell/4)-$imtownsize);
	$townleft=(int)(($mapcell-$imtownsize)/2);
	$townright=(int)(($mapcell-$imtownsize)/2);
	$flagtop=(int)($mapcell-($mapcell/4)-$imflagsize);
	$flagleft=(int)(($mapcell-$imflagsize)/2);
	$flagright=(int)(($mapcell-$imflagsize)/2);
	$armytop[1]=(int)($mapcell-$imitemsize);
	$armytop[2]=(int)($mapcell-$imitemsize);
	$armytop[3]=(int)($mapcell-$imitemsize);
	$armyleft[1]=1;
	$armyleft[2]=(int)($mapcell-$imitemsize)-1;
	$armyleft[3]=(int)(($mapcell-$imitemsize)/2);
	$armyright[1]=(int)($mapcell-$imitemsize)-1;
	$armyright[2]=1;
	$armyright[3]=(int)(($mapcell-$imitemsize)/2);
	$anchortop=1;
	$anchorleft=(int)($mapcell-$imitemsize)-1;
	$anchorright=(int)($mapcell-$imitemsize)-1;
	$herotop=1;
	$heroleft=(int)($mapcell-$imitemsize)-3;
	$heroright=1;
	
	$mapcolor=array ( "", "#E9E6CD", "#BDE6CD", "#1A9233", "#168F84", "#B9A869", "#817549", "#F0C355", "#20AACA", "#1266AB" );
	$mapcolornb=9;
	
	$verbose=false;
	$verbose1=false;
	$verbose2=false;

	if (!$getaccesslimited[$game_checked]) {

	$im2=@imagecreatefromgif (Inc("emptymap.gif"));
#	$im2=@imagecreatefromgif (Inc("emptymask.gif"));

	for ($i=1; $i<=$mapcolornb; $i++) {
		$r=substr($mapcolor[$i],1,2);
		$g=substr($mapcolor[$i],3,2);
		$b=substr($mapcolor[$i],5,2);
#		echo $mapcolor[$mapcontentcell[$j][$k]]."= 0x".$r." 0x".$g." 0x".$b.", ";
		$color[$i] = ImageColorAllocate($im2, hexdec("0x".$r), hexdec("0x".$g), hexdec("0x".$b));
	}
	$black = imagecolorallocate($im2, 0, 0, 0);
	$white = imagecolorallocate($im2, 255, 255, 255);

/*	$nbtowns=0;
	$nbheroes=0;
	$nbarmies=0;
*/	
	$font = 'gfx/arial.ttf';
	for ($j=1; $j<=$maplin; $j++) {
		$mapadd=0;
		for ($i=1; $i<=$mapcol+1; $i++) {
			
			$found=true;
			if (!(1&$j)) {
				if ($i==1) {
					$found=false;
					$mapadd=(int)(-$mapcell/2);
				}
			}
			if (1&$j) {
				if ($i==$mapcol+1) {
					$found=false;
				}
			}
			if ($found) {
				$k=$i;
				if (!(1&$j)) {
					$k=$i-1;
				}

/*				if ($mapcontentcell[$j][$k]!=9) {
					imagefilledrectangle($im2, ($i-1)*$mapcell+$mapadd, ($j-1)*$mapcell, $i*$mapcell+$mapadd, $j*$mapcell, $color[$mapcontentcell[$j][$k]]);
				}
				
				$found=false;
				if ((int)($mapriver[$j][$k])>0) {
					$v1="";
					$v2="";
					$mapriver[$j][$k]=trim($mapriver[$j][$k]);
					for ($td=1; $td<=6; $td++) {
						if ($mapriver[$j][$k][$td-1]=="1") {
							$v1.="1";
						} else {
							$v1.="0";
						}
						if ($mapriver[$j][$k][$td-1]=="2") {
							$v2.="1";
						} else {
							$v2.="0";
						}
					}
					if ($verbose1) echo $v1."<br>";
					$v1=bindec($v1);
					if ($verbose1) echo $v1."<br>";
					$v=$let[$v1];
					if ($verbose1) echo $v."<br>";

					if ($verbose1) echo $v2."<br>";
					$v2=bindec($v2);
					if ($verbose1) echo $v2."<br>";
					$v.=$let[$v2];
					if ($verbose1) echo $v."<br>";
					$fn=ImgCreate($v);
					$im1=@imagecreatefromgif("gfx/".$fn);
					imagecopy($im2,$im1, ($i-1)*$mapcell+$mapadd, ($j-1)*$mapcell, 0,0,$mapcell,$mapcell);
					imagedestroy($im1);
					$found=true;
				}
				if (!$found) {
					$im1=@imagecreatefromgif("gfx/empty.gif");
					imagecopy($im2,$im1, ($i-1)*$mapcell+$mapadd, ($j-1)*$mapcell, 0,0,$mapcell,$mapcell);
					imagedestroy($im1);
				}
				if ((int)($maproad[$j][$k])>0) {
					$v1="";
					$v2="";
					$v3="";
					$v="--";
					$maproad[$j][$k]=trim($maproad[$j][$k]);
					for ($td=1; $td<=6; $td++) {
						if (($maproad[$j][$k][$td-1]=="1") || ($maproad[$j][$k][$td-1]=="4") || ($maproad[$j][$k][$td-1]=="5")) {
							$v1.="1";
						} else {
							$v1.="0";
						}
						if (($maproad[$j][$k][$td-1]=="2") || ($maproad[$j][$k][$td-1]=="4")) {
							$v2.="1";
						} else {
							$v2.="0";
						}
						if (($maproad[$j][$k][$td-1]=="3") || ($maproad[$j][$k][$td-1]=="5")) {
							$v3.="1";
						} else {
							$v3.="0";
						}
					}
					if ($verbose2) echo $v1."<br>";
					$v1=bindec($v1);
					if ($verbose2) echo $v1."<br>";
					$v.=$let[$v1];
					if ($verbose2) echo $v."<br>";

					if ($verbose2) echo $v2."<br>";
					$v2=bindec($v2);
					if ($verbose2) echo $v2."<br>";
					$v.=$let[$v2];
					if ($verbose2) echo $v."<br>";

					if ($verbose2) echo $v3."<br>";
					$v3=bindec($v3);
					if ($verbose2) echo $v3."<br>";
					$v.=$let[$v3];
					if ($verbose2) echo $v."<br>";
					$fn=ImgCreate($v, false, 2);
					$im1=@imagecreatefromgif("gfx/".$fn);
					imagecopy($im2,$im1, ($i-1)*$mapcell+$mapadd, ($j-1)*$mapcell, 0,0,$mapcell,$mapcell);
					imagedestroy($im1);
				}
*/
				$hex="";
				if ($k<10) { $hex.="0"; } $hex.=$k;
				if ($j<10) { $hex.="0"; } $hex.=$j;
#				imagettftext($im2, 6, 0, $i*$mapcell+$mapadd-(int)($mapcell/2)-8, ($j-1)*$mapcell+8, $black, $font, $hex);
				
				if (strlen($towninhex_num[$k][$j])>0) {
					$pieces=explode(";",$towninhex_num[$k][$j]);
					for ($ip=0; $ip<=count($pieces)-1; $ip++) {
						$l=$pieces[$ip]*1;

						$jo=$town[$l]->people;
						if ($jo<10) { $p="0".$jo; } else { $p=$jo; }
						if ($jo<=10) { $pln=1; } else if ($jo<=20) { $pln=2; } else { $pln=3; }
						$pl="0".$pln;

						$town[$l]->def=0;
						# fortifications
						if ($town[$l]->fortif>1) {
							$f=$town[$l]->fortif-1;
							$town[$l]->def+=$FortifDef[$town[$l]->fortif-1];

							$im1=@imagecreatefromgif("gfx/fortif".$f."_".$pl.".gif");
							imagecopy($im2,$im1, ($i-1)*$mapcell+$mapadd+$fortifright, ($j-1)*$mapcell+$fortiftop, 0,0,35,35);
							imagedestroy($im1);
						}

						# capital
						if ($town[$l]->capital==1) {
							$im1=@imagecreatefromgif("gfx/flag_".$p.".gif");
							imagecopy($im2,$im1, ($i-1)*$mapcell+$mapadd+$flagright, ($j-1)*$mapcell+$flagtop, 0,0,47,47);
							imagedestroy($im1);
						}
						# town
						$s=$town[$l]->size;
						if ($s>0) {
							$pf=$p;
							if ($p==26) { $pf="06"; }
							if ($p==27) { $pf="16"; }
							if ($p==28) { $pf="24"; }
							$im1=@imagecreatefromgif("gfx/town".$s."_".$pf.".gif");
							imagecopy($im2,$im1, ($i-1)*$mapcell+$mapadd+$townright, ($j-1)*$mapcell+$towntop, 0,0,31,31);
							imagedestroy($im1);
						}
						# harbor
						if ($town[$l]->dock>1) {
							$h=$town[$l]->dock-1;
							$im1=@imagecreatefromgif("gfx/anchor".$h.".gif");
							imagecopy($im2,$im1, ($i-1)*$mapcell+$mapadd+$anchorright, ($j-1)*$mapcell+$anchortop, 0,0,13,13);
							imagedestroy($im1);
						}
					}
				}


				if (strlen($armyinhex_num[$k][$j])>0) {
					for ($in=1; $in<=3; $in++) {
						$nbici[$in]=0;
					}
					for ($pln=1; $pln<=3; $pln++) {
						$dec_top[$pln]=($armyinhex[$k][$j][$pln]-1)*($imitemsize-3);
					}
					$dec_right_neutre=(int)(($mapcell-$imitemsize)/2);
					for ($in=1; $in<=$armyinhex[$k][$j][3]-1; $in++) {
						$dec_right_neutre+=(int)(($imitemsize-3)/2);
					}
					$pieces=explode(";",$armyinhex_num[$k][$j]);
					for ($ip=0; $ip<=count($pieces)-1; $ip++) {
						$l=$pieces[$ip]*1;
				
							$jo=$army[$l]->people;
							if ($jo<10) { $p="0".$jo; } else { $p=$jo; }
							if ($jo<=10) { $pln=1; } else if ($jo<=20) { $pln=2; } else { $pln=3; }
							if ($armyinhex[$k][$j][0]>1) {
								$dtop=$armytop[$pln]-$dec_top[$pln]+$nbici[$pln]*($imitemsize-3);
								$dright=$armyright[$pln];
							} else {
								$dtop=$armytop[$pln];
								switch ($pln) {
									case 1 : $dright=$armyright[$pln]-$nbici[$pln]*($imitemsize-3);
									break;
									case 2 : $dright=$armyright[$pln]+$nbici[$pln]*($imitemsize-3);
									break;
									case 3 : $dright=$dec_right_neutre-$nbici[$pln]*($imitemsize-3);
									break;
								}
							}

							$pf=$p;
							if ($p==26) { $pf="06"; }
							if ($p==27) { $pf="16"; }
							if ($p==28) { $pf="24"; }
							$im1=@imagecreatefromgif("gfx/army_".$pf.".gif");
							imagecopy($im2,$im1, ($i-1)*$mapcell+$mapadd+$mapcell-13-1-$dright, ($j-1)*$mapcell+$dtop, 0,0,13,13);
							imagedestroy($im1);
							$nbici[$pln]++;
					}
				}

				if (strlen($heroinhex_num[$k][$j])>0) {
					$pieces=explode(";",$heroinhex_num[$k][$j]);
					for ($ip=0; $ip<=count($pieces)-1; $ip++) {
						$l=$pieces[$ip]*1;

						$jo=$hero[$l]->people;
						if ($jo<10) { $p="0".$jo; } else { $p=$jo; }
						if ($jo<=10) { $pln=1; } else if ($jo<=20) { $pln=2; } else if ($jo<=25) { $pln=3; }
						if ($hero[$l]->health>0) {
							$pf=$p;
							if ($p==26) { $pf="06"; }
							if ($p==27) { $pf="16"; }
							if ($p==28) { $pf="24"; }

							$im1=@imagecreatefromgif("gfx/hero_".$pf.".gif");
							imagecopy($im2,$im1, ($i-1)*$mapcell+$mapadd+$mapcell-7-1-($heroright+$ip*$imherosize), ($j-1)*$mapcell+$herotop, 0,0,7,7);
							imagedestroy($im1);
						}
					}
				}
			}
		}
	}
	
	imagegif($im2, $basedir.$metoobasedir.'gfx/printversion.gif');
#	imagegif($im2, $basedir.$metoobasedir.'gfx/emptymap.gif');
	imagedestroy($im2);
	
	echo "<img src='gfx/printversion.gif'>";
#	echo "<img src='gfx/emptymap.gif'>";
	
	}
?>
</body>
</html>