////////////////////////////////////////////////////////////////////////////
//
// Withdraw script to make AI withdraw troops on agreement
//
// Withdraw by Dale
//
// Version 1.3 - 14-May-2001
//
// See readme-withdraw.txt for teaser, and history-withdraw.txt for version changes.
//
////////////////////////////////////////////////////////////////////////////

///////////////////////////////////
// Mod notes!
//
// Variables:
// See "Global Variables" below for explanations.
//
// This script will check every player for a withdraw agreement between that player and the current
// player.  If an agreement is found, the script cycles through all units in the player's army
// and checks whether the tile it is on is owned by the agreement's other player.  If it is
// found the tile is of the other player's, then the script finds the nearest home city of that
// unit's civ and moves the unit to a tile within 2 tiles of the found city.
//
// By default, the script is set to withdraw troops after 5 turns of the agreement.  If you wish to
// change this, then change "GetAgreementDuration(player[0], WDT_civ, 3) >= 5" the number 5 to how
// many turns you want.
//
///////////////////////////////////

///////////////////////////////////
// Global variables
///////////////////////////////////

int_t WDT_civ;					// Civ holder
int_t WDT_NumOfPlayers;				// Der!
int_t WDT_numoftroops;				// Number of troops
int_t WDT_count;				// Counter
int_t WDT_count2;				// Counter 2
int_t WDT_waterunit;				// Water unit flag
int_t WDT_tmptype;				// Unit type holder
int_t WDT_veteran;				// Veteran flag
unit_t WDT_tmpunit;				// Unit holder
unit_t WDT_tmpunit2;				// Unit holder 2
army_t WDT_tmparmy;				// Army holder
location_t WDT_tmploc;				// Location holder
location_t WDT_tmploc2;				// Location holder 2

///////////////////////////////////
// Begin Game stuff
///////////////////////////////////

HandleEvent(BeginTurn) 'WDT_BeginGame' pre {
	if(IsHumanPlayer(g.player)) {
		WDT_NumOfPlayers = preference("NumPlayers");
		DisableTrigger('WDT_BeginGame');
	}
}

///////////////////////////////////
// Find nearest home city
///////////////////////////////////

int_f WDT_GetDistanceNearestCity(location_t theLoc, int_t thePlayer) {
	location_t tmpLoc;
	city_t tmpCity;
	int_t i;
	int_t min;
	int_t city;
	int_t cities;
	int_t val;
	int_t tmpPlayer;

	tmpPlayer = thePlayer;
	tmpLoc = theLoc;
	min = 10000;
	city = 0;				// If no other city is closer put it near the capital
	cities = Cities(tmpPlayer);

	for(i = 0; i < cities; i = i + 1) {
		GetCityByIndex(tmpPlayer, i, tmpCity);
		val = SquaredDistance(tmpCity.location, tmpLoc);
		if(val < min) {
			min = val;
			city = i;
		}
	}
	return city;
}

///////////////////////////////////
// The magic art of withdrawing
///////////////////////////////////

HandleEvent(BeginTurn) 'WDT_MainRoutine' pre {
	int_t tmpVal;
	city_t tmpCity;
	location_t tmploc2;
	location_t tmploc3;
	tmpVal = 0;
	player[0] = g.player;
	for(WDT_civ = 0; WDT_civ < WDT_NumOfPlayers; WDT_civ = WDT_civ + 1) {
		player[1] = WDT_civ;
		if(!(IsHumanPlayer(player[0])) && player[1] != player[0] && HasAgreement(player[0], player[1], 3)) {
			WDT_numoftroops = player[0].units;
			for(WDT_count = 0; WDT_count < WDT_numoftroops; WDT_count = WDT_count + 1) {
				GetUnitByIndex(player[0], WDT_count, WDT_tmpunit);
				tmploc2 = WDT_tmpunit.location;
				if(CellOwner(tmploc2) == WDT_civ && GetAgreementDuration(player[0], WDT_civ, 3) >= 5) {
					AddCenter(tmploc2);
					WDT_waterunit = 0;
					if(TerrainType(tmploc2) >= 10 && TerrainType(tmploc2) <= 17) {
						WDT_waterunit = 1;
					}
					WDT_veteran = 0;
					if(IsVeteran(WDT_tmpunit)) {
						WDT_veteran = 1;
					}
					if(!(IsCivilian(WDT_tmpunit)) && WDT_waterunit != 1) {
						tmpVal = WDT_GetDistanceNearestCity(tmploc2, player[0]);
						GetCityByIndex(player[0], tmpVal, tmpCity);
						tmploc3 = tmpCity.location;
						AddEffect(WDT_tmpunit.location, "SPECEFFECT_DIPLOMATIC", "SOUND_ID_EXPEL");
						WDT_tmptype = WDT_tmpunit.type;
						Event:DisbandUnit(WDT_tmpunit);
						CreateUnit(player[0], WDT_tmptype, tmploc3, 2, WDT_tmpunit2);
						if(WDT_veteran == 1) {
							ToggleVeteran(WDT_tmpunit2, 1);
						}
					}
				}
			}
		}
	}
}



