<?php

// --------------------------------------------
class CommonStageSettings {
	public $savegame = "unset_savegame";
	public $username = "unset_username";

	public $player_ai_compensation = 8;
	public $friendly_fire = 0;
	public $initial_xp = 0.0;
	public $initial_rp = 0.0;
	public $base_capture_system = "any";

	// these will be overwritten by values from gamemode_invasion, or from game is game supplies them
	public $fellow_ai_accuracy = 0.94;
	public $enemy_ai_accuracy = 0.94;
	public $xp_multiplier = 0.27;
	public $rp_multiplier = 1.0;
	public $fellow_capacity_factor = 1.05;
	public $enemy_capacity_factor = 1.0;
};
// used as global
$_common_stage_settings = new CommonStageSettings();


// --------------------------------------------
class FactionConfig {
	public $index = -1;
	public $file = "unset faction file";
	public $name = "unset faction name";

	function __construct($index, $file, $name) {
		$this->index = $index;
		$this->file = $file;
		$this->name = $name;
	}
};
$_faction_configs = array();
$_faction_configs[] = new FactionConfig(0, "green.xml", "Greenbelts");
$_faction_configs[] = new FactionConfig(1, "grey.xml", "Graycollars");
$_faction_configs[] = new FactionConfig(2, "brown.xml", "Brownpants");
$_faction_configs[] = new FactionConfig(3, "neutral.xml", "Neutral");


// --------------------------------------------
function get_common_resources($faction_id) {
	return 
		"<command class='faction_resources' faction_id='" . $faction_id . "' clear_weapons='1'>\n" .
		"	<weapon key='medikit.weapon' />\n" .
		"	<weapon key='riot_shield.weapon' />\n" .
		"	<weapon key='cover_resource.weapon' />\n" .
		"	<weapon key='mg_resource.weapon' />\n" .    

		"	<weapon key='aks74u.weapon' />\n" .
		"	<weapon key='p90.weapon' />\n" .
		"	<weapon key='benelli_m4.weapon' />\n" .
		"	<weapon key='f2000.weapon' />\n" .
		"	<weapon key='pepperdust.weapon' />\n" .
		"	<weapon key='vss_vintorez.weapon' />\n" .
		"	<weapon key='ns2000.weapon' />\n" .
		"	<weapon key='stoner_lmg.weapon' />\n" .
		"</command>\n";
}

// --------------------------------------------
function get_green_resources($faction_id) {
	// disable suitcase from greens - we only want enemies to drop them
	// no m79 by default for greens

	return 
		"<command class='faction_resources' faction_id='" . $faction_id . "'>\n" .
		"	<weapon key='m16a4.weapon' />\n" .
		"	<weapon key='m240.weapon' />\n" .
		"	<weapon key='mossberg.weapon' />\n" .
		"	<weapon key='m24_a2.weapon' />\n" .
		"	<weapon key='mp5sd.weapon' />\n" .
		"	<weapon key='m72_law.weapon' />\n" .    

		"   <carry_item key='suitcase.carry_item' enabled='0' />\n" .
    	"   <vehicle key='cargo_truck.vehicle' enabled='0' />\n" .
		"</command>\n";
}

// --------------------------------------------
function get_grey_resources($faction_id) {
	return 
		"<command class='faction_resources' faction_id='" . $faction_id . "'>\n" .
		"	<weapon key='g36.weapon' />\n" .
		"	<weapon key='famasg1.weapon' />\n" .
		"	<weapon key='pkm.weapon' />\n" .
		"	<weapon key='spas-12.weapon' />\n" .
		"	<weapon key='psg90.weapon' />\n" .
		"	<weapon key='scorpion-evo.weapon' />\n" .
		"	<weapon key='m2_carlgustav.weapon' />\n" .    

		"	<weapon key='m79.weapon' />\n" .

    	"   <vehicle key='armored_truck.vehicle' enabled='0' />\n" .
		"</command>\n";
}


// --------------------------------------------
function get_brown_resources($faction_id) {
	return 
		"<command class='faction_resources' faction_id='" . $faction_id . "'>\n" .
		"	<weapon key='ak47.weapon' />\n" .
		"	<weapon key='sg552.weapon' />\n" .
		"	<weapon key='imi_negev.weapon' />\n" .
		"	<weapon key='qbs-09.weapon' />\n" .
		"	<weapon key='dragunov_svd.weapon' />\n" .
		"	<weapon key='qcw-05.weapon' />\n" .
		"	<weapon key='rpg-7.weapon' />\n" .     

		"	<weapon key='m79.weapon' />\n" .

    	"   <vehicle key='armored_truck.vehicle' enabled='0' />\n" .
		"</command>\n";
}

// --------------------------------------------
function get_disable_faction_resources_command($faction_id, $resources) {
	$command = "<command class='faction_resources' faction_id='" . $faction_id . "'>\n";
	foreach ($resources as $key => $r) {
		$command .= "<" . $r->type . " key='" . $r->key . "' enabled='0'/>\n";
	}
	$command .= "</command>";
	return $command;
}

// --------------------------------------------
function create_fellow_commander_ai_command($faction, $base = 0.64, $border = 0.14) {
	return 
		"<command\n" . 
		"  class='commander_ai'" . 
		"  faction='" . $faction . "'" .
		"  base_defense='" . $base . "'" .
		"  border_defense='" . $border ."'>" .
		"</command>\n";
}



// --------------------------------------------
// enemy will defend a bit harder than attack by default --> makes it easier for player
function create_commander_ai_command($faction, $base = 0.70, $border = 0.14) {
	return 
		"<command\n" . 
		"  class='commander_ai'" . 
		"  faction='" . $faction . "'" .
		"  base_defense='" . $base . "'" .
		"  border_defense='" . $border ."'>" .
		"</command>\n";
}


// --------------------------------------------
// stage-specific faction info
class Faction {
	public $config = NULL;
	public $default_commander_ai_command = "";

	public $bases = -1;

	public $over_capacity = 0;
	public $capacity_multiplier = 1.0;
	public $capacity_offset = 0;

	// this is optional, for now
	public $owned_bases = array();

	function __construct($config, $default_command) {
		$this->config = $config;
		$this->default_commander_ai_command = $default_command;
	}

	function is_neutral() {
		return $this->capacity_multiplier <= 0.0;
	}

};


// --------------------------------------------
class Stage {
	public $map_info = NULL;

	public $factions = array();

	// stage specific tracker classes
	public $trackers = array();

	public $max_soldiers = 0;
	public $soldier_capacity_variance = 0.30;
	public $soldier_capacity_model = "variable";
	public $defense_win_time = -1;

	// metadata, mostly for instructions comment selection logic
	public $primary_objective = "capture";
	public $koth_target_base = "center base";
	public $radio_objective_present = true;

	// --------------------------------------------
	public function __construct() {
		$this->map_info = new MapInfo();
	}

	// --------------------------------------------
	public function is_capture() {
		return $this->primary_objective == "capture";
	}

	// --------------------------------------------
	public function is_koth() {
		return $this->primary_objective == "koth";
	}

	public function has_side_objectives() {
		return true;
	}

	public function has_loot_objective() {
		return true;
	}

	public function has_radio_objective() {
		return $this->radio_objective_present;
	}

	// --------------------------------------------
	public function get_change_map_command() {

		include "common_invasion_variables.php";

		$map_config = "<map_config>\n";

		foreach ($this->factions as $key => $f) {
			$map_config = $map_config .
				"  <faction file='" . $f->config->file . "' />\n";
		}

		$map_config = $map_config .
			$all_items . "\n" .
			$all_calls . "\n" .
			$all_vehicles . "\n" .
			"</map_config>\n";

		$change_map_command = 
			"<command class='change_map'" .
			"	map='" . $this->map_info->path . "'>" .
			$map_config .
			"</command>";

		return $change_map_command;
	}

	// --------------------------------------------
	public function get_extra_commands($completion_percentage, $friendly_id) {
		$commands = array();

		foreach ($this->factions as $key => $f) {
			// TODO: this won't work nicely with mods
			$commands[] = get_common_resources($key);
			if ($f->config->file == "green.xml") {
				$commands[] = get_green_resources($key);
			} else if ($f->config->file == "grey.xml") {
				$commands[] = get_grey_resources($key);
			} else if ($f->config->file == "brown.xml") {
				$commands[] = get_brown_resources($key);
			}
		}

		// we want to have a sense of progression 
		// with the starting map vs other maps played before extra final maps

		// we can achieve that by having all or friendly/enemy factions 
		// not having certain interesting/important vehicles:
		// - cargo truck
		// - comms truck
		// - radio tower
		// - tanks

		// e.g. 
		// tanks will activate only once the player has completed two maps
		// player faction will not have objective-related vehicles until player has completed two maps
		// enemy factions will not have objective-related vehicles until player has completed one map
		_log("completion percentage: " . $completion_percentage);
		if ($completion_percentage < 0.10) {
			_log("below 10%");
			foreach ($this->factions as $key => $f) {
				$resources = array();
				// disable tanks from all factions
				$resources[] = new ResourceRef("tank.vehicle", "vehicle");
				// disable comms truck, cargo and radio tower on all factions
				$resources[] = new ResourceRef("radar_truck.vehicle", "vehicle");
				$resources[] = new ResourceRef("cargo_truck.vehicle", "vehicle");
				$resources[] = new ResourceRef("radar_tower.vehicle", "vehicle");

				if ($key == $friendly_id) {
				} else {
				}

				if (count($resources) > 0) {
					$commands[] = get_disable_faction_resources_command($key, $resources); 
				}
			}
			// a bit odd that we change stage members here in a getter function, but do it, it's just metadata
			$this->radio_objective_present = false;

		} else if ($completion_percentage < 0.25) {
			_log("below 25%, above 10%");
			foreach ($this->factions as $key => $f) {
				$resources = array();
				if ($key == $friendly_id) {
					// disable comms truck, cargo and radio tower on friendly faction only
					$resources[] = new ResourceRef("comms_truck.vehicle", "vehicle");
					$resources[] = new ResourceRef("radar_truck.vehicle", "vehicle");
					$resources[] = new ResourceRef("radar_tower.vehicle", "vehicle");
				} else {
				}
				if (count($resources) > 0) {
					$commands[] = get_disable_faction_resources_command($key, $resources); 
				}
			}
		}

		return $commands;
	}

	// --------------------------------------------
	public function get_start_game_command($completion_percentage = 1.0) {
		global $_common_stage_settings;

		// TODO:
		// progressive settings, just a bit
		// first map:
		// own   $f->capacity_multiplier = 1.1;
		// enemy $lower_ai_accuracy

		$command = 
			"<command class='start_game'" .
			"   savegame='" . $_common_stage_settings->savegame . "'" .
			"	vehicles='1'" .
			"	max_soldiers='" . $this->max_soldiers . "'" .
			"	soldier_capacity_variance='" . $this->soldier_capacity_variance . "'" .
			"	soldier_capacity_model='" . $this->soldier_capacity_model . "'" .
			"	player_ai_compensation='" . $_common_stage_settings->player_ai_compensation . "'" .
			"	xp_multiplier='" . $_common_stage_settings->xp_multiplier . "'" .
			"	rp_multiplier='" . $_common_stage_settings->rp_multiplier . "'" .
			"   initial_xp='" . $_common_stage_settings->initial_xp . "'" . 
			"   initial_rp='" . $_common_stage_settings->initial_rp . "'" . 
			"	base_capture_system='" . $_common_stage_settings->base_capture_system . "'" .
			"	friendly_fire='" . $_common_stage_settings->friendly_fire . "'";

		if ($this->defense_win_time >= 0) {
			$command = $command . 
				"  defense_win_time='" . $this->defense_win_time . "'";
		}
		$command = $command .
			">\n";

		foreach ($this->factions as $key => $f) {
			$command = $command .
				"	<faction capacity_offset='" . $f->capacity_offset . "' initial_over_capacity='" . $f->over_capacity . "' ";

			if ($key == 0) {
				// friendly faction
				$multiplier = $_common_stage_settings->fellow_capacity_factor * $f->capacity_multiplier;

				// drain friendly faction power the farther the game goes;
				// player will gain power and will become more effective, so this works as an attempt to counter that a bit
				_log("completion: " . $completion_percentage);
				if ($completion_percentage > 0.8) {
					$multiplier *= 0.75;
				} else if ($completion_percentage > 0.6) {
					$multiplier *= 0.82;
				} else if ($completion_percentage > 0.4) {
					$multiplier *= 0.9;
				}

				$command = $command . "capacity_multiplier='" . $multiplier . "' ";
				$command = $command . "ai_accuracy='" . $_common_stage_settings->fellow_ai_accuracy . "' ";
			} else {
				// enemy
				$multiplier = $_common_stage_settings->enemy_capacity_factor * $f->capacity_multiplier;

				// first map: reduce enemies a bit, let it flow easier
				if ($completion_percentage < 0.1) {
					$multiplier *= 0.88;
				}

				$command = $command . "capacity_multiplier='" . $multiplier . "' ";
				$command = $command . "ai_accuracy='" . $_common_stage_settings->enemy_ai_accuracy . "' ";
			}			

			// if base amount has been declared for the faction, use it

			// allow adventure mode to use the amount of bases we managed to capture 
			// if we were in the map previously

			// there's a bit of risk here, only use this for the first faction, i.e. greens
			if ($key == 0 && count($f->owned_bases) > 0) {
				$command = $command . "initial_occupied_bases='" . count($f->owned_bases) . "' ";
			} else if ($f->bases >= 0) {
				$command = $command . "initial_occupied_bases='" . $f->bases . "' ";
			}

			$command = $command . "/>\n";
		}

		$command = $command .
			"   <local_player faction_id='0' username='" . $_common_stage_settings->username . "' />\n" .
			"</command>\n";

		return $command;
	}
};


?>