<?php

include_once("internal/log.php");
include_once("trackers/stage.php");


/*
// mapping between id and name:
	<region id='0' name='map1' position='507.167 424.251' size='317.45 288.05' texture_rect='0.00255368 0.56884 0.149521 0.702197'>
	<region id='1' name='map2' position='265.867 415.101' size='266.566 248.404' texture_rect='0.00255368 0.452172 0.125964 0.567173'>
	<region id='2' name='map3' position='738.034 386.636' size='240.018 249.571' texture_rect='0.21322 0.653129 0.32434 0.768671'>
	<region id='3' name='map4' position='475.616 241.287' size='348.876 243.276' texture_rect='0.457748 0.452172 0.619265 0.564799'>
	<region id='4' name='map5' position='295.878 588.127' size='262.178 195.27' texture_rect='0.457748 0.703863 0.579127 0.794266'>
	<region id='5' name='map6' position='509.833 663.708' size='328.132 312.572' texture_rect='0.00255367 0.795933 0.154467 0.940642'>
	<region id='6' name='map7' position='699.226 545.454' size='212.553 132.547' texture_rect='0.741748 0.475796 0.840152 0.53716'>
	<region id='7' name='map8' position='269.343 746.663' size='238.823 191.059' texture_rect='0.747081 0.675671 0.857648 0.764124'>
	<region id='8' name='map9' position='717.845 641.206' size='305.814 338.823' texture_rect='0.457748 0.795933 0.599329 0.952796'>
*/

function get_map_index_from_map_name($map_path) {
	// this assumes maps are named so that they end with the index, also it won't handle more than 9 maps well
	return substr($map_path, -1, 1);
}

// ----------------------------------------------------------------------------
function get_stage_index($map) {
	// warning, only map indices 1-9 can be handled currently:
	// adventure rotator peeks just one digit from the map name to 
	// determine map index
	if ($map == 2) return 0;
	if ($map == 4) return 1;
	if ($map == 3) return 2;
	if ($map == 7) return 3;
	if ($map == 1) return 4;
	if ($map == 5) return 5;
	if ($map == 6) return 6;
	if ($map == 8) return 7;
	if ($map == 9) return 8;

	_log("ERROR: get_stage_index, map=" . $map . ", not defined");
	die();
}

// ----------------------------------------------------------------------------
function get_map_index($stage) {
	if ($stage == 0) return 2;
	if ($stage == 1) return 4;
	if ($stage == 2) return 3;
	if ($stage == 3) return 7;
	if ($stage == 4) return 1;
	if ($stage == 5) return 5;
	if ($stage == 6) return 6;
	if ($stage == 7) return 8;
	if ($stage == 8) return 9;

	_log("ERROR: get_map_index, stage=" . $stage . ", not defined");
	die();
}

// ----------------------------------------------------------------------------
function get_transport_name($map_from, $map_to) {
	$transport_name = "map" . $map_from . "_to_map" . $map_to;
	return $transport_name;
}

// ----------------------------------------------------------------------------
function get_offender_visual($transport_name, $color_faction_id, $id) {
	// generated by atlas.exe
	include("trackers/advance_partial.php");
	return $c;
}

// ----------------------------------------------------------------------------
class World {
	protected $metagame = NULL;
	protected $offender_visual_ids = array();
	protected $transport_visual_ids = array();

	// --------------------------------------------
	function __construct($metagame) {
		$this->metagame = $metagame;
	}

	// ----------------------------------------------------------------------------
	// advance phase shares markers with offenders currently
	function set_advance($current_stage, $next_stage) {
		$transport_name = "map" . get_map_index($current_stage) . "_to_map" . get_map_index($next_stage);

		$visual = get_offender_visual($transport_name, 0, 300);

		if ($visual != "") {
			$command = "<command class='set_world_situation'>\n";
			$command = $command . "\t" . $visual . "\n";
			$command = $command . "</command>\n";
			$this->metagame->comms->send($command);
		}
	}

	// ----------------------------------------------------------------------------
	function get_region_situation($stage_index, &$stage) {
		$map_index = get_map_index($stage_index);
		$region_index = $map_index - 1;
		//_log("get_region_situation: stage_index=" . $stage_index . " map_index=" . $map_index . " region_index=" . $region_index); 

		$situation = 
			"	<region id='" . $region_index . "'>\n";
		$ratio = 1.0;
		if (count($stage->factions) > 1) {
			$ratio = 1.0 / count($stage->factions);
		}
		foreach ($stage->factions as $key => $f) {
			// the stages have greens present in each and every stage,
			// but in world view we only show the enemy occupation
			// (completed maps will override this in later phase of execution)
			if ($f->config->index != 0) {
				$situation = $situation .
					"		<occupant id='" . $f->config->index . "' value='" . $ratio . "'/>\n";
			}
		}
		$situation = $situation .
			"	</region>\n";

		return $situation;
	}

	// ----------------------------------------------------------------------------
	function refresh(&$stages, &$stages_completed) {
		$region_situation = array();
		foreach ($stages as $index => &$stage) {
			// the regions are added in the list with stage keys in stage order!
			$region_situation[] = $this->get_region_situation($index, $stage);			
		}

		$command = "<command class='set_world_situation'>\n";
		foreach ($region_situation as $stage_index => $value) {
			$map_index = get_map_index($stage_index); 
			$region_index = $map_index - 1;
			_log("refresh: region_index=" . $region_index . ", map_index=" . $map_index . ", stage_index=" . $stage_index); 
			// if greens have completed the region, mark it
			if (array_key_exists($stage_index, $stages_completed)) {
				$region = 
					"	<region id='" . $region_index . "'>\n" . 
					"		<occupant id='0' value='1.0'/>\n" .
					"	</region>\n";
				$command = $command . $region;
			} else {
				$command = $command . $value;
			}
		}
		$command = $command . "</command>\n";

		$this->metagame->comms->send($command);
	}

	// ----------------------------------------------------------------------------
	function clear_visuals(&$visual_ids) {
		$command = "<command class='set_world_situation'>\n";
		foreach ($visual_ids as $key => $value) {
			$p = "<visual id='" . $value . "' layer='1' enabled='false' />";
			$command = $command . $p . "\n";
		}
		$command = $command . "</command>\n";

		$this->metagame->comms->send($command);
	}

	// ----------------------------------------------------------------------------
	function clear_offender_visuals() {
		$this->clear_visuals($this->offender_visual_ids);
	}

	// ----------------------------------------------------------------------------
	function set_offenders($stage) {
		// clear ids first
		$this->offender_visual_ids = array();

		$id = 300;
		$visuals = array();

		// TODO:
		// when a stage has both enemies present, the first one 
		// is thought to occupy the area primarily, and the second
		// just attacks there, same as greens

		// most of the time, there should be a nearby map which is
		// occupied by the enemy invader, and if so, there should be 
		// a similar invasion visual what greens have to indicate an attack

		/*
		if ($stage == 0) {
			// TODO:
			// greens attack from "none" to map2
			//$visuals[] = get_offender_visual(get_transport_name("0", "2"), 0, $id);
			//$offender_visual_ids[] = $id; $id++;
		} else if ($stage == 1) {
			// greens attack from map2 to map4
			$visuals[] = get_offender_visual(get_transport_name("2", "4"), 0, $id);
			$this->offender_visual_ids[] = $id; $id++;
		} else if ($stage == 2) {
			// greens attack from map4 to map3
			$visuals[] = get_offender_visual(get_transport_name("4", "3"), 0, $id);
			$this->offender_visual_ids[] = $id; $id++;
		} else if ($stage == 3) {
			// greens attack from map3 to map7
			$visuals[] = get_offender_visual(get_transport_name("3", "7"), 0, $id);
			$this->offender_visual_ids[] = $id; $id++;
			// occupied by grey, also brown's there.. we don't have an arrow for browns tho

		} else if ($stage == 4) {
			// greens attack from map3 to map1
			$visuals[] = get_offender_visual(get_transport_name("3", "1"), 0, $id);
			$this->offender_visual_ids[] = $id; $id++;
			// occupied by grey,
			// browns are also at map1
			$visuals[] = get_offender_visual(get_transport_name("6", "1"), 2, $id);
			$this->offender_visual_ids[] = $id; $id++;
		} else if ($stage == 5) {
			// greens attack from map1 to map5
			$visuals[] = get_offender_visual(get_transport_name("1", "5"), 0, $id);
			$this->offender_visual_ids[] = $id; $id++;
			// occupied by grey
			// browns are also at map5
			$visuals[] = get_offender_visual(get_transport_name("6", "5"), 2, $id);
			$this->offender_visual_ids[] = $id; $id++;
		} else if ($stage == 6) {
			// greens attack from map5 to map6
			$visuals[] = get_offender_visual(get_transport_name("1", "6"), 0, $id);
			$this->offender_visual_ids[] = $id; $id++;
		} else if ($stage == 7) {
			// greens attack from map6 to map8
			$visuals[] = get_offender_visual(get_transport_name("6", "8"), 0, $id);
			$this->offender_visual_ids[] = $id; $id++;
			// occupied by brown,
		} else if ($stage == 8) {
		}
		*/

		$command = "<command class='set_world_situation'>\n";
		foreach ($visuals as $key => $value) {
			$command = $command . $value . "\n";
		}
		$command = $command . "</command>\n";

		$this->metagame->comms->send($command);
	}

	// ----------------------------------------------------------------------------
	function clear_transport_visuals() {
		$this->clear_visuals($this->transport_visual_ids);
	}

	// ----------------------------------------------------------------------------
	function set_available_transports($transports) {
		// clear ids first
		$this->transport_visual_ids = array();

		$id = 400;
		$visuals = array();

		foreach ($transports as $key => $transport) {
			// using the "offender" visual here, it's the advance arrow
			$visual = get_offender_visual($transport, 0, $id);
			if ($visual != "") {
				$visuals[] = $visual;
				$this->transport_visual_ids[] = $id; $id++;
			}
		} 

		if (count($visuals) > 0) {
			$command = "<command class='set_world_situation'>\n";
			foreach ($visuals as $key => $value) {
				$command = $command . $value . "\n";
			}
			$command = $command . "</command>\n";

			$this->metagame->comms->send($command);
		}
	}

	// ----------------------------------------------------------------------------
	function set_current_location($stage) {
		$map_name = "map" . get_map_index($stage);

		// generated by atlas.exe
		include("trackers/cursor_partial.php");

		$command = "<command class='set_world_situation'>\n";
		$command = $command . "\t" . $c . "\n";
		$command = $command . "</command>\n";

		$this->metagame->comms->send($command);
	}

	// ----------------------------------------------------------------------------
	function init(&$stages, &$maps_completed, $index) {
		$doc = new DOMDocument();
		if ($doc->load("trackers/init_worldview.xml")) {
			$this->metagame->comms->send($doc->saveXML());
		}

		// send world view setup
		$this->refresh($stages, $maps_completed);

		// set current offender setup
		$this->set_offenders($index);

		// set current location in world view
		$this->set_current_location($index);
	}

}


?>