<?php

include_once("internal/tracker.php");
include_once("internal/log.php");

// --------------------------------------------

class PeacefulLastBase extends Tracker {
	protected $metagame = NULL;
	protected $friendly_id = 0;
	protected $started = false;

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

	// --------------------------------------------
	function start() {
		_log("starting peaceful");
		$this->set_enemies_defend();
		$this->started = true;
		
		$this->refresh();
	}

    // ----------------------------------------------------
	protected function refresh() {
		// query for total count of bases for friendly faction
		// if 1, make enemy defend
		// if more, apply default commander settings
	
        $list_query_doc = new DOMDocument();

        // query about bases
        $command =
                "<command class='make_query' id='bases_query_1'>\n" .
                "       <data class='bases'/>\n" .
                "</command>";
        $list_query_doc->loadXML($command);

        // send the query command now
        $list_doc = $this->metagame->comms->query($list_query_doc);
		$bases = 2; // default to going into normal mode
        if ($list_doc) {
			$bases = 0;
			// go through list of bases
            $base_list = $list_doc->getElementsByTagName("base");
			for ($i = 0; $i < $base_list->length; $i++) {
				$base = $base_list->item($i);
				if ($base->getAttribute("owner_id") == $this->friendly_id) {
					$bases++;
				}
			}
            _log("* " . $bases . " friendly bases");
        }

		if ($bases == 1) {
			$this->set_enemies_defend();
			// go to boosted attack mode with player faction
			// - our capacity is pretty low when there's just one base
			//   and usually defending is a priority to avoid just a
			//   run through with the attack
			$this->apply_offensive_mode($this->friendly_id);
		} else if ($bases > 1) {
			$this->set_enemies_default();
			// go to normal mode with player faction as well
			$this->apply_default_mode($this->friendly_id);
		} // else 0, don't care
	}

	// ----------------------------------------------------
    protected function handle_base_owner_change_event($doc) {
        // base_id
        // owner_id (faction)

		$this->refresh();
    }


	// --------------------------------------------
	function has_ended() {
		// always on
		return false;
	}

	// --------------------------------------------
	function has_started() {
		return $this->started;
	}

	// --------------------------------------------
	function apply_defensive_mode($enemy_id) {
		$command =
			"<command class='commander_ai'" .
 			"       faction='" . $enemy_id . "'" .
 			"       base_defense='0.8'" .
 			"       border_defense='0.2'>" .
        	        "</command>";
		$this->metagame->comms->send($command);
	}

	// --------------------------------------------
	function apply_offensive_mode($id) {
		$command =
			"<command class='commander_ai'" .
 			"       faction='" . $id . "'" .
 			"       base_defense='0.1'" .
 			"       border_defense='0.1'>" .
        	        "</command>";
		$this->metagame->comms->send($command);
	}

	// --------------------------------------------
	function apply_default_mode($enemy_id) {
/*
		$command =
                	"<command class='commander_ai'" .
                        "       faction='" . $enemy_id . "'" .
                        "       base_defense='0.64'" .
                        "       border_defense='0.14'>" .
                        "</command>";
*/
		$factions = $this->metagame->get_factions();

		$command = $factions[$enemy_id]->default_commander_ai_command;
		$this->metagame->comms->send($command);
	}

	// --------------------------------------------
	function set_enemies_defend() {
		foreach ($this->metagame->get_factions() as $key => $value) {
			if ($key != $this->friendly_id) {
				$this->apply_defensive_mode($key);
			}
		}
	}

	// --------------------------------------------
	function set_enemies_default() {
		foreach ($this->metagame->get_factions() as $key => $value) {
			if ($key != $this->friendly_id) {
				$this->apply_default_mode($key);
			}
		}
	}

}

?>