<?
	$nbtowns=0;
	$nbheroes=0;
	$nbarmies=0;
	$nbartefact=0;

	$basedir = $DOCUMENT_ROOT;
	$filenb=0;
	MakeList('');
	$game=$game_checked;
	
	$filenb_touse=0;
	for ($nbf=1; $nbf<=$filenb; $nbf++) {
		if (strpos($filename[$nbf], "eutral.csv")!=false) {
			$filenb_touse++;
			$filename_touse[$filenb_touse]=$filename[$nbf];
			$filedir_touse[$filenb_touse]=$filedir[$nbf];
	 	}
		if ((strpos($filedir[$nbf], "turn")!=false) && (strpos($filename[$nbf], ".csv")!=false)) {
			$fileword=str_replace(" ","-",$filename[$nbf]);
			$fileword=str_replace(".","-",$fileword);
			$fileword = explode("-", $fileword);
			$use=false;
			if (($fileword[0]=="Game") && ($fileword[1]==$game)) {
				$use=true;
			}
			if (($fileword[0]=="Game") && ($fileword[1]=="x")) {
				$use=true;
			}
			if ($use) {
				$filenb_touse++;
				$filename_touse[$filenb_touse]=$filename[$nbf];
				$filedir_touse[$filenb_touse]=$filedir[$nbf];
				$fn=$DOCUMENT_ROOT.$filedir_touse[$filenb_touse].'/'.$filename_touse[$filenb_touse];
				$filetime_touse[$filenb_touse]=filemtime($fn);
				$fileturn_touse[$filenb_touse]=$fileword[2];
				$filepeople_touse[$filenb_touse]=$fileword[3];
				$filesample_touse[$filenb_touse]=$fileword[1];
	 		}
	 	}
	}
	
	$turntodisplaymax=0;
	for ($i=1; $i<=25; $i++) {
		$turnmosttodate[$i]=0;
		$turnunderdisplay[$i]=-1;
		for ($nbf=1; $nbf<=$filenb_touse; $nbf++) {
			if ($filepeople_touse[$nbf]==$i) {
				if ($fileturn_touse[$nbf]>$turnmosttodate[$i]) {
					$turnmosttodate[$i]=$fileturn_touse[$nbf];
					if ($turnmosttodate[$i]>$turntodisplaymax) {
						$turntodisplaymax=$turnmosttodate[$i];
					}
				}
				if ($turntodisplay!="") {
					if (($fileturn_touse[$nbf]<=$turntodisplay) && ($fileturn_touse[$nbf]>$turnunderdisplay[$i])) {
						$turnunderdisplay[$i]=$fileturn_touse[$nbf];
					}
				}
			}
		}
	}

	for ($i=1; $i<=25; $i++) {
		for ($nbf=1; $nbf<=$filenb_touse; $nbf++) {
			if ($filesample_touse[$nbf]=="x") {
				for ($nbf2=1; $nbf2<=$filenb_touse; $nbf2++) {
					if (($filepeople_touse[$nbf2]==$filepeople_touse[$nbf]) && ($fileturn_touse[$nbf2]==$fileturn_touse[$nbf])) {
						if ($filesample_touse[$nbf2]!="x") {
							$filesample_touse[$nbf]="dontuse";
						}
					}
				}
			}
		}
	}

	$nbupdtodate=0;
	if ($verbosemem) { echo "<font color=white>memory start mem=".memory_get_usage()." (".(memory_get_usage()-$memmemory).")<br>"; $memmemory=memory_get_usage(); }
	if ($verbosemem) { echo "<table border=1>"; }
	for ($nbf=1; $nbf<=$filenb; $nbf++) {
		$done=false;
		$uptodated=false;
		if ($filename_touse[$nbf]=="Neutral.csv") {
			$done=true;
		}
		if ($turnmosttodate[$nbf]==$turntodisplaymax) {
			$nbupdtodate++;
		}
		if (($turntodisplay=="") && ($fileturn_touse[$nbf]==$turnmosttodate[$filepeople_touse[$nbf]])) {
			$done=true;
		}
		if (($turntodisplay!="") && ($fileturn_touse[$nbf]==$turnunderdisplay[$filepeople_touse[$nbf]])) {
			$done=true;
		}
		if ($filesample_touse[$nbf]=="dontuse") {
			$done=false;
		}
		if ($filename_touse[$nbf]=="") {
			$done=false;
		}
		if ($done) {
#			echo $filename_touse[$nbf]." turnmosttodate=".$turnmosttodate[$filepeople_touse[$nbf]]." turnunderdisplay=".$turnunderdisplay[$filepeople_touse[$nbf]]."<br>";
			$fn=$DOCUMENT_ROOT.$filedir_touse[$nbf].'/'.$filename_touse[$nbf];
			$fp = fopen ($fn, "r");
			if (filesize($fn)>0) { $content = fread($fp, filesize($fn)); }
			fclose($fp);
			Decrunch($content);

			$gamepast=true;
			if (($game_past[$game]=="1") && ($fileturn_touse[$nbf]==0) && (($turntodisplay>0) || ($turntodisplaymax>0))) { $gamepast=false; }

			for ($i=1; $i<=$lin; $i++) {
				switch ($cell[$i][1]) {
					case "people" :
						$peoplenb=$cell[$i][2];
						if ($cell[$i][5]=="turn") {
							$fileturn_touse[$nbf]=$cell[$i][6];
						}
					break;
					case "relations" :
						for ($r=1; $r<=24; $r++) {
							$rel=$cell[$i][$r+1];
							$relp=explode(":", $rel);
							$relationlevel[$peoplenb][$relp[0]]=$relp[1];
						}
					break;
					case "town" :
						$nbtowns++;
						$town[$nbtowns] = new Town;
						$town[$nbtowns]->turninfo=$fileturn_touse[$nbf];
						$town[$nbtowns]->people=$peoplenb;
						$town[$nbtowns]->name=$cell[$i][2];
						$town[$nbtowns]->capital=$cell[$i][3];
						$town[$nbtowns]->hex=$cell[$i][4];
						$town[$nbtowns]->climate=$cell[$i][5];
						$town[$nbtowns]->size=$cell[$i][6];
						$town[$nbtowns]->fortif=$cell[$i][7];
						$town[$nbtowns]->loyalty=$cell[$i][8];
						$town[$nbtowns]->dock=$cell[$i][9];
						$town[$nbtowns]->hidden=$cell[$i][10];
						$town[$nbtowns]->sieged=$cell[$i][11];
						for ($sf2=1; $sf2<=8; $sf2++) {
							$town[$nbtowns]->localprod[$sf2]=$cell[$i][11+$sf2];
						}
						$town[$nbtowns]->source=1;
						if ($verbosemem) { echo "<tr><td><font color=white>added town ".$town[$nbtowns]->name." [$nbtowns]</td><td><font color=white>mem=".memory_get_usage()."</td><td><font color=white>(".(memory_get_usage()-$memmemory).")</td></tr>"; $memmemory=memory_get_usage(); }
					break;
					case "hero" :
						if ($gamepast) {
							$nbheroes++;
							$hero[$nbheroes] = new Hero;
							$hero[$nbheroes]->people=$peoplenb;
							$hero[$nbheroes]->turninfo=$fileturn_touse[$nbf];
							$hero[$nbheroes]->name=$cell[$i][2];
							$hero[$nbheroes]->commander=$cell[$i][3];
							$hero[$nbheroes]->hex=$cell[$i][4];
							$hero[$nbheroes]->command=$cell[$i][5];
							$hero[$nbheroes]->agent=$cell[$i][6];
							$hero[$nbheroes]->emissary=$cell[$i][7];
							$hero[$nbheroes]->mage=$cell[$i][8];
							$hero[$nbheroes]->health=$cell[$i][9];
							$hero[$nbheroes]->stealth=$cell[$i][10];
							$hero[$nbheroes]->challenge=$cell[$i][11];
							$hero[$nbheroes]->artefactlist=$cell[$i][12];
							$hero[$nbheroes]->spelllist=$cell[$i][13];
							if ($verbosemem) { echo "<tr><td><font color=white>added hero [$nbheroes]</td><td><font color=white>mem=".memory_get_usage()."</td><td><font color=white>(".(memory_get_usage()-$memmemory).")</td></tr>"; $memmemory=memory_get_usage(); }
						}
					break;
					case "army" :
						if ($gamepast) {
							$nbarmies++;
							$army[$nbarmies] = new Army;
							$army[$nbarmies]->turninfo=$fileturn_touse[$nbf];
							$army[$nbarmies]->people=$peoplenb;
							$army[$nbarmies]->leader=$cell[$i][2];
							$army[$nbarmies]->hex=$cell[$i][3];
							$army[$nbarmies]->morale=$cell[$i][4];
							for ($n=1; $n<=7; $n++) {
								$army[$nbarmies]->unitsnb[$n]=$cell[$i][$n+4];
							}
							$army[$nbarmies]->climate=$cell[$i][12];
							for ($n=1; $n<=7; $n++) {
								$army[$nbarmies]->units_training[$n]=$cell[$i][$n+12];
								$army[$nbarmies]->units_weapons[$n]=$cell[$i][$n+18];
								$army[$nbarmies]->units_armors[$n]=$cell[$i][$n+24];
							}
							$army[$nbarmies]->food=$cell[$i][31];
							if ($verbosemem) { echo "<tr><td><font color=white>added army [$nbarmies]</td><td><font color=white>mem=".memory_get_usage()."</td><td><font color=white>(".(memory_get_usage()-$memmemory).")</td></tr>"; $memmemory=memory_get_usage(); }
						}
					break;
					case "market" :
						if (($turntodisplay=="") && ($fileturn_touse[$nbf]==$turnmosttodate[$filepeople_touse[$nbf]])) {
							for ($sf=1; $sf<=21; $sf++) {
								$market[$sf]=$cell[$i][$sf+1];
							}
						}
					break;
					case "stock" :
						for ($sf=1; $sf<=20; $sf++) {
							$stock[$peoplenb][$sf]=$cell[$i][$sf+1];
						}
					break;
				}
			}
			$filetime_people[$peoplenb]=$filetime_touse[$nbf];
			if ($turnmosttodate[$filepeople_touse[$nbf]]==$turntodisplaymax) { $uptodate[$peoplenb]=true; }
		}
	}
	$nbarmiesloaded=$nbarmies;
	if ($verbosemem) { echo "<tr><td><font color=white>end load files</td><td><font color=white>mem=".memory_get_usage()."</td><td><font color=white>(".(memory_get_usage()-$memmemory).")</td></tr>"; $memmemory=memory_get_usage(); }

	for ($i=1; $i<=$nbarmies; $i++) {
		$p=0;
		$j=0;
		$found=false;
		do {
			$j++;
			if ($hero[$j]->people==$army[$i]->people) {
				$p++;
				if ($army[$i]->leader==$p) {
					$found=true;
				}
			}
		} while ((!$found) && ($j<=$nbheroes));
		$army[$i]->leadername="?";
		if ($found) {
			$army[$i]->leader=$j;
			$army[$i]->leadername=$hero[$j]->name;
			$army[$i]->leadercommand=$hero[$j]->command;
		}
	}

	if ($turntodisplay=="") {
		$turntodisplay=$turntodisplaymax;
	}

	$fnsave="Data ".$game.".csv";
	$fnsave = $basedir.$metoobasedir.'turn/data/'.$fnsave;

	if ($verbosemem) { echo "<tr><td colspan=3 height=100><font color=white>DATA</td></tr>"; }
	if (file_exists($fnsave)) {
#		echo $fnsave."<br>";
		if ($verbosemem) { echo "<tr><td><font color=white>loading data file</td><td><font color=white>mem=".memory_get_usage()."</td><td><font color=white>(".(memory_get_usage()-$memmemory).")</td></tr>"; $memmemory=memory_get_usage(); }
		$fp = fopen ($fnsave, "r");
		if (filesize($fnsave)>0) { $loadingdata = fread($fp, filesize($fnsave)); }
		fclose($fp);
		Decrunch($loadingdata);
		$data=$cell;
		$cell=null;
		$loadingdata=null;
		$datalin=$lin;
		$datacol=$col;
		if ($verbosemem) { echo "<tr><td><font color=white>loaded data file</td><td><font color=white>mem=".memory_get_usage()."</td><td><font color=white>(".(memory_get_usage()-$memmemory).")</td></tr>"; $memmemory=memory_get_usage(); }
		
		for ($i=1; $i<=$datalin; $i++) {
			$ok=true;
			$gamepast=true;
			if ($data[$i][1]>$turntodisplay) { $ok=false; }
			if (($game_past[$game]=="1") && ($data[$i][1]<$turntodisplay)) { $gamepast=false; }
			
			if ($ok) {
			switch ($data[$i][2]) {
				case "neutral" :
					$NeutralSide[$data[$i][3]]=$data[$i][4];
					$NeutralSideTurn[$data[$i][3]]=$data[$i][5];
				break;
				case "townadded" :
					$nbtowns++;
					$town[$nbtowns] = new Town;
					$town[$nbtowns]->turninfo=$data[$i][1];
					$town[$nbtowns]->name=$data[$i][3];
					$town[$nbtowns]->hex=$data[$i][4];
					$town[$nbtowns]->people=$data[$i][5];
					$town[$nbtowns]->size=$data[$i][6];
					$town[$nbtowns]->fortif=$data[$i][7]+1;
					$town[$nbtowns]->loyalty=$data[$i][8];
					$town[$nbtowns]->dock=$data[$i][9];
					$town[$nbtowns]->hidden=$data[$i][10];
					if ($verbosemem) { echo "<tr><td><font color=white>added town $nbtowns</td><td><font color=white>mem=".memory_get_usage()."</td><td><font color=white>(".(memory_get_usage()-$memmemory).")</td></tr>"; $memmemory=memory_get_usage(); }
				break;
				case "townmodified" :
					$found=false;
					for ($t=1; $t<=$nbtowns; $t++) {
						if ($town[$t]->hex==$data[$i][4]) {
							$found=true;
							$k=$t;
						}
					}
					if (!$found) {
						$nbtowns++;
						$town[$nbtowns] = new Town;
						$k=$nbtowns;
					}
					$town[$k]->turninfo=$data[$i][1];
					$town[$k]->name=$data[$i][3];
					$town[$k]->hex=$data[$i][4];
					$town[$k]->people=$data[$i][5];
					$town[$k]->size=$data[$i][6];
					$town[$k]->fortif=$data[$i][7]+1;
					$town[$k]->loyalty=$data[$i][8];
					$town[$k]->dock=$data[$i][9];
					$town[$k]->hidden=$data[$i][10];
					if (!$found) {
						if ($verbosemem) { echo "<tr><td><font color=white><font color=white>added town $nbtowns</td><td><font color=white>mem=".memory_get_usage()."</td><td><font color=white>(".(memory_get_usage()-$memmemory).")</td></tr>"; $memmemory=memory_get_usage(); }
					}
				break;
				case "towndestroyed" :
					for ($t=1; $t<=$nbtowns; $t++) {
						if (($town[$t]->hex==$data[$i][3]) && ($town[$t]->people==$data[$i][4])) {
							$town[$t]->hex=0;
							$town[$t] -> destroy();
						}
					}
				break;
				case "armyadded" :
					if ($gamepast) {
						$nbarmies++;
						$army[$nbarmies] = new Army;
						$army[$nbarmies]->turninfo=$data[$i][1];
						$army[$nbarmies]->people=$data[$i][3];
						$army[$nbarmies]->leadername=$data[$i][4];
						$army[$nbarmies]->hex=$data[$i][5];
						$army[$nbarmies]->morale=$data[$i][6];
						for ($n=1; $n<=7; $n++) {
							$army[$nbarmies]->unitsnb[$n]=$data[$i][6+$n];
						}
						$army[$nbarmies]->climate=0;
						for ($n=1; $n<=6; $n++) {
							$army[$nbarmies]->units_training[$n]=$data[$i][6+7+1+$n];
							$army[$nbarmies]->units_weapons[$n]=$data[$i][6+7+1+6+$n];
							$army[$nbarmies]->units_armors[$n]=$data[$i][6+7+1+6+6+$n];
						}
						$army[$nbarmies]->leadercommand=$data[$i][6+7+1+6+6+6+1];
						$army[$nbarmies]->offmagic=$data[$i][6+7+1+6+6+6+2];
						$army[$nbarmies]->defmagic=$data[$i][6+7+1+6+6+6+3];
						$army[$nbarmies]->intel=$data[$i][6+7+1+6+6+6+4];
						if ($verbosemem) { echo "<tr><td><font color=white><font color=white>added army $nbarmies</td><td><font color=white>mem=".memory_get_usage()."</td><td><font color=white>(".(memory_get_usage()-$memmemory).")</td></tr>"; $memmemory=memory_get_usage(); }
					}
				break;
				case "armymodified" :
					if ($gamepast) {
						for ($t=1; $t<=$nbarmies; $t++) {
							if (($army[$t]->hex==$data[$i][5]) && ($army[$t]->people==$data[$i][3]) && ($army[$t]->leadername==$data[$i][4])) {
								$army[$t]->turninfo=$data[$i][1];
								$army[$t]->morale=$data[$i][6];
								for ($n=1; $n<=7; $n++) {
									$army[$t]->unitsnb[$n]=$data[$i][6+$n];
								}
								for ($n=1; $n<=6; $n++) {
									$army[$t]->units_training[$n]=$data[$i][6+7+1+$n];
									$army[$t]->units_weapons[$n]=$data[$i][6+7+1+6+$n];
									$army[$t]->units_armors[$n]=$data[$i][6+7+1+6+6+$n];
								}
								$army[$t]->leadercommand=$data[$i][6+7+1+6+6+6+1];
								$army[$t]->offmagic=$data[$i][6+7+1+6+6+6+2];
								$army[$t]->defmagic=$data[$i][6+7+1+6+6+6+3];
								$army[$t]->intel=$data[$i][6+7+1+6+6+6+4];
							}
						}
					}
				break;
				case "armyracemodified" :
					if ($gamepast) {
						for ($t=1; $t<=$nbarmies; $t++) {
							if (($army[$t]->hex==$data[$i][5]) && ($army[$t]->people==$data[$i][9]) && ($army[$t]->leadername==$data[$i][10])) {
								$army[$t]->turninfo=$data[$i][1];
								$army[$t]->people=$data[$i][3];
								$army[$t]->leadername=$data[$i][4];
								$army[$t]->leadercommand=$data[$i][6];
								$army[$t]->offmagic=$data[$i][7];
								$army[$t]->defmagic=$data[$i][8];
							}
						}
					}
				break;
				case "armymoved" :
					if ($gamepast) {
						for ($t=1; $t<=$nbarmies; $t++) {
							if (($army[$t]->people==$data[$i][3]) && ($army[$t]->leadername==$data[$i][4]) && ($army[$t]->hex==$data[$i][6])) {
								$army[$t]->turninfo=$data[$i][1];
								$army[$t]->hex=$data[$i][5];
							}
						}
					}
				break;
				case "armydestroyed" :
					if ($gamepast) {
						for ($t=1; $t<=$nbarmies; $t++) {
							if (($army[$t]->hex==$data[$i][5]) && ($army[$t]->people==$data[$i][3]) && ($army[$t]->leadername==$data[$i][4])) {
								$army[$t]->hex=0;
								$army[$t] -> destroy();
							}
						}
					}
				break;
				case "heroadded" :
					if ($gamepast) {
						$nbheroes++;
						$hero[$nbheroes] = new Hero;
						$hero[$nbheroes]->turninfo=$data[$i][1];
						$hero[$nbheroes]->people=$data[$i][3];
						$hero[$nbheroes]->name=$data[$i][4];
						$hero[$nbheroes]->hex=$data[$i][5];
						$hero[$nbheroes]->health=100;
						if ($verbosemem) { echo "<tr><td><font color=white>added hero $nbheroes</td><td><font color=white>mem=".memory_get_usage()."</td><td><font color=white>(".(memory_get_usage()-$memmemory).")</td></tr>"; $memmemory=memory_get_usage(); }
					}
				break;
				case "heromoved" :
					if ($gamepast) {
						for ($t=1; $t<=$nbheroes; $t++) {
							if (($hero[$t]->name==$data[$i][4]) && ($hero[$t]->people==$data[$i][3]) && ($hero[$t]->hex==$data[$i][6])) {
								$hero[$t]->turninfo=$data[$i][1];
								$hero[$t]->hex=$data[$i][5];
							}
						}
					}
				break;
				case "herodestroyed" :
					if ($gamepast) {
						for ($t=1; $t<=$nbheroes; $t++) {
							if (($hero[$t]->name==$data[$i][4]) && ($hero[$t]->people==$data[$i][3]) && ($hero[$t]->hex==$data[$i][5])) {
								$hero[$t]->health=0;
	#							$hero[$t] -> destroy();
							}
						}
					}
				break;
				case "artefactadded" :
					$nbartefact++;
					$artefact[$nbartefact] = new Artefact;
					$artefact[$nbartefact]->turninfo=$data[$i][1];
					$artefact[$nbartefact]->num=$data[$i][3];
					$artefact[$nbartefact]->hex=$data[$i][4];
				break;
				case "artefactmoved" :
					for ($t=1; $t<=$nbartefact; $t++) {
						if (($artefact[$t]->num==$data[$i][3]) && ($artefact[$t]->hex==$data[$i][5])) {
							$artefact[$t]->turninfo=$data[$i][1];
							$artefact[$t]->hex=$data[$i][4];
						}
					}
				break;
				case "artefactupdated" :
					$found=false;
					for ($t=1; $t<=$nbartefact; $t++) {
						if ($artefact[$t]->num==$data[$i][3]) {
							$artefact[$t]->hex=$data[$i][6];
							$found=true;
						}
					}
					if (!$found) {
						$nbartefact++;
						$artefact[$nbartefact] = new Artefact;
						$artefact[$nbartefact]->num=$data[$i][3];
						$artefact[$nbartefact]->hex=$data[$i][6];
					}
					if (($data[$i][4]!="") && ($data[$i][5]!="")) {
						$found=false;
						for ($t=1; $t<=$nbheroes; $t++) {
							if ($hero[$t]->name==$data[$i][4]) {
								$hero[$t]->turninfo=$data[$i][1];
								$hero[$t]->people=$data[$i][5];
								$hero[$t]->hex=$data[$i][6];
								if ($hero[$t]->artefactlist!="") {
									$hero[$t]->artefactlist.="#".$data[$i][3];
								} else {
									$hero[$t]->artefactlist=$data[$i][3];
								}
								$found=true;
							}
						}
						if (!$found) {
							$nbheroes++;
							$hero[$nbheroes] = new Hero;
							$hero[$nbheroes]->turninfo=$data[$i][1];
							$hero[$nbheroes]->name=$data[$i][4];
							$hero[$nbheroes]->people=$data[$i][5];
							$hero[$nbheroes]->hex=$data[$i][6];
							$hero[$nbheroes]->artefactlist=$data[$i][3];
							$hero[$nbheroes]->health=100;
						}
					}
				break;
				case "artefactdestroyed" :
					for ($t=1; $t<=$nbartefact; $t++) {
						if (($artefact[$t]->num==$data[$i][3]) && ($artefact[$t]->hex==$data[$i][4])) {
							$artefact[$t]->hex=0;
						}
					}
				break;
				case "markadded" :
					$nbmark++;
					$mark[$nbmark] = new Mark;
					$mark[$nbmark]->num=$data[$i][3];
					$mark[$nbmark]->hex=$data[$i][4];
				break;
				case "markdestroyed" :
					for ($t=1; $t<=$nbmark; $t++) {
						if (($mark[$t]->num==$data[$i][3]) && ($mark[$t]->hex==$data[$i][4])) {
							$mark[$t] -> destroy();
						}
					}
				break;
			}
		}
		}
		
	}
	if ($verbosemem) { echo "</table>"; }
	
/*	for ($n=1; $n<=5; $n++) {
		if (($NeutralSide[$n]!=0) && ($NeutralSideTurn[$n]<=$turntodisplay)) {
			for ($l=1; $l<=$nbtowns; $l++) {
				if ($town[$l]->people==$n+20) {
					$town[$l]->hex=0;
					$town[$l]->destroy();
				}
			}
			for ($l=1; $l<=$nbarmies; $l++) {
				if ($army[$l]->people==$n+20) {
					$army[$l]->hex=0;
					$army[$l]->destroy();
				}
			}
			for ($l=1; $l<=$nbheroes; $l++) {
				if ($hero[$l]->people==$n+20) {
					$hero[$l]->health=0;
					$hero[$l]->destroy();
				}
			}
		}
	}
*/	
	for ($l=1; $l<=$nbtowns; $l++) {
		if ($town[$l]->source==1) {
			for ($l2=1; $l2<=$nbtowns; $l2++) {
				if (($l!=$l2) && ($town[$l2]->hex==$town[$l]->hex)) {
					if ($town[$l]->turninfo>$town[$l2]->turninfo) {
						$town[$l2]->hex=0;
						$town[$l2]->destroy();
					}
				}
			}
		}
	}
	for ($l=1; $l<=$nbtowns; $l++) {
		$town[$l]->def=0;
		# fortifications
		if ($town[$l]->fortif>1) {
			$town[$l]->def+=$FortifDef[$town[$l]->fortif-1];
		}
		$town[$l]->def+=$TownDef[$town[$l]->size];
		$town[$l]->def*=(100+$town[$l]->loyalty)/100;
		$town[$l]->def=(int)($town[$l]->def);
		$town[$l]->localprodvalue=0;
		for ($sf2=1; $sf2<=7; $sf2++) {
			$town[$l]->localprodvalue+=$town[$l]->localprod[$sf2]*$market[$sf2+14];
		}
	}

	for ($l=1; $l<=$nbarmies; $l++) {
		if ($army[$l]->hex!=0) {
			$x=substr($army[$l]->hex,0,2)*1;
			$y=substr($army[$l]->hex,2,2)*1;
			$army[$l]->terrain=$mapcontentcell[$y][$x];
			$army[$l]->tactic=3;
			$army[$l]->vstactic=3;
			CalculateArmyStr($l);
			$armystr_temp[$l]=$army[$l]->str_final;
		}
	}
	for ($l2=1; $l2<=$nbarmies; $l2++) {
		$strmin=1000000;
		$k=0;
		for ($l=1; $l<=$nbarmies; $l++) {
			if ($armystr_temp[$l]<$strmin) {
				$k=$l;
				$strmin=$armystr_temp[$l];
			}
		}
		$armyorder[$l2]=$k;
		$armystr_temp[$k]=1000000+10;
	}
	
	for ($l2=1; $l2<=$nbarmies; $l2++) {
		$l=$armyorder[$l2];
		$x=substr($army[$l]->hex,0,2)*1;
		$y=substr($army[$l]->hex,2,2)*1;
		$jo=$army[$l]->people;
		if ($jo<=10) { $pln=1; } else if ($jo<=20) { $pln=2; } else { $pln=3; }
		$armyinhex[$x][$y][$pln]++;
		if ($armyinhex_num[$x][$y]!=0) { $armyinhex_num[$x][$y].=";"; }
		$armyinhex_num[$x][$y].=$l;
	}
	for ($y=1; $y<=39; $y++) {
		for ($x=1; $x<=44; $x++) {
			for ($pln=1; $pln<=3; $pln++) {
				if ($armyinhex[$x][$y][$pln]!=0) {
					$armyinhex[$x][$y][0]++;
				}
			}
		}
	}
	for ($l=1; $l<=$nbheroes; $l++) {
		$x=substr($hero[$l]->hex,0,2)*1;
		$y=substr($hero[$l]->hex,2,2)*1;
		if ($hero[$l]->commander!=1) {
			if ($heroinhex_num[$x][$y]!=0) { $heroinhex_num[$x][$y].=";"; }
			$heroinhex_num[$x][$y].=$l;
		}
	}
	for ($l=1; $l<=$nbtowns; $l++) {
		$x=substr($town[$l]->hex,0,2)*1;
		$y=substr($town[$l]->hex,2,2)*1;
		if ($towninhex_num[$x][$y]!=0) { $towninhex_num[$x][$y].=";"; }
		$towninhex_num[$x][$y].=$l;
	}
	for ($l=1; $l<=$nbartefact; $l++) {
		$x=substr($artefact[$l]->hex,0,2)*1;
		$y=substr($artefact[$l]->hex,2,2)*1;
		if ($artefactinhex_num[$x][$y]!=0) { $artefactinhex_num[$x][$y].=";"; }
		$artefactinhex_num[$x][$y].=$l;
	}
	for ($l=1; $l<=$nbmark; $l++) {
		$x=substr($mark[$l]->hex,0,2)*1;
		$y=substr($mark[$l]->hex,2,2)*1;
		if ($markinhex_num[$x][$y]!=0) { $markinhex_num[$x][$y].=";"; }
		$markinhex_num[$x][$y].=$l;
	}
?>