Air Units

Air Unit script to make AI move air units back to base — originally written by Dale.

The Script:

//--------------------------------------------------------------------------
// MoT Mod for CTP2 (Apolyton Edition) by BureauBert
// v1.1
// http://motmod.ctp2.info
//--------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////
// Based upon:
// Air Unit script to make AI move air units back to base.
// by Dale
// Version 1.1 14-May-2001
//
// 22-April-2006:
// - added !IsHumanPlayer check
// - added usage of MoT common functions:
//   -- IsAircraft
//   -- IsLandingLocation
//   -- MoveArmyToTarget
////////////////////////////////////////////////////////////////////////////

///////////////////////////////////
// Common Functions
///////////////////////////////////

// IsAircraft(), IsLandingLocation() and MoveArmyToTarget() in main.slc

///////////////////////////////////
// Move Unit event
///////////////////////////////////

HandleEvent(MoveOrder) 'AirMove' pre {
	int_t airPlayer;
	int_t airCountA;
	int_t airCountB;
	int_t airCountC;
	int_t airX;
	int_t airY;
	int_t airNumCities;
	int_t airArmyCount;
	int_t airTmpCityIdx;
	city_t airTmpCity;
	unit_t airTmpUnit;
	army_t airTmpArmy;
	army_t airTmpArmy1;
	location_t airTmpLoc;
	location_t airTmpLoc1;
	location_t airTmpLoc3;
	location_t airTmpLoc4;
	location_t airArmyLoc;

	airArmyCount = 0;
	airTmpArmy = army[0];
	airArmyLoc = airTmpArmy.location;
	airTmpLoc3 = airArmyLoc;
	airPlayer = army[0].owner;

	if(!IsHumanPlayer(airPlayer)) {
		for(airCountA = 0; airCountA < airTmpArmy.size; airCountA = airCountA + 1) {
			if(GetUnitFromCell(airArmyLoc, airCountA, airTmpUnit)) {
				if(IsAircraft(airTmpUnit)) {
					airNumCities = PlayerCityCount(airPlayer);
					airTmpCityIdx = GetCapital(airPlayer);
					if(GetCityByIndex(airPlayer, airTmpCityIdx, airTmpCity)) {
						airTmpLoc = airTmpCity.location;
					}
					for(airCountC = 0; airCountC < airNumCities; airCountC = airCountC + 1) {
						if(GetCityByIndex(airPlayer, airCountC, airTmpCity)) {
							airTmpLoc1 = airTmpCity.location;
						}
						if(Distance(airTmpUnit.location, airTmpLoc1) < Distance(airTmpUnit.location, airTmpLoc)) {
							airTmpLoc = airTmpLoc1;
						}
					}
					for(airCountB = 0; airCountB < 10; airCountB = airCountB + 1) {
						if(GetNeighbor(airTmpLoc3, 3, airTmpLoc4)) {
							airTmpLoc3 = airTmpLoc4;
						}
					}
					if(IsLandingLocation(airTmpLoc3)) {
						if(Distance(airTmpUnit.location, airTmpLoc3) < Distance(airTmpUnit.location, airTmpLoc)) {
							airTmpLoc = airTmpLoc3;
						}
					}
					for(airX = 0; airX < 10; airX = airX + 1) {
						for(airY = 0; airY < 21; airY = airY + 1) {
							if(GetNeighbor(airTmpLoc3, 2, airTmpLoc4)) {
								airTmpLoc3 = airTmpLoc4;
								if(IsLandingLocation(airTmpLoc3)) {
									if(Distance(airTmpUnit.location, airTmpLoc3) < Distance(airTmpUnit.location, airTmpLoc)) {
										airTmpLoc = airTmpLoc3;
									}
								}
							}
						}
						if(GetNeighbor(airTmpLoc3, 7, airTmpLoc4)) {
							airTmpLoc3 = airTmpLoc4;
							if(IsLandingLocation(airTmpLoc3)) {
								if(Distance(airTmpUnit.location, airTmpLoc3) < Distance(airTmpUnit.location, airTmpLoc)) {
									airTmpLoc = airTmpLoc3;
								}
							}
							for(airY = 0; airY < 21; airY = airY + 1) {
								if(GetNeighbor(airTmpLoc3, 5, airTmpLoc4)) {
									airTmpLoc3 = airTmpLoc4;
									if(IsLandingLocation(airTmpLoc3)) {
										if(Distance(airTmpUnit.location, airTmpLoc3) < Distance(airTmpUnit.location, airTmpLoc)) {
											airTmpLoc = airTmpLoc3;
										}
									}
								}
							}
						}
						if(GetNeighbor(airTmpLoc3, 7, airTmpLoc4)) {
							airTmpLoc3 = airTmpLoc4;
							if(IsLandingLocation(airTmpLoc3)) {
								if(Distance(airTmpUnit.location, airTmpLoc3) < Distance(airTmpUnit.location, airTmpLoc)) {
									airTmpLoc = airTmpLoc3;
								}
							}
						}
					}
					if(airArmyLoc != airTmpLoc) {
						ClearOrders(airTmpUnit);
						Event:UngroupOrder(airTmpArmy);
						Event:AddUnitToArmy(airTmpUnit, airTmpArmy1, airArmyCount);
						airArmyCount = airArmyCount + 1;
						MoveArmyToTarget(airTmpArmy1, airTmpLoc);
					}
				}
			}
		}
	}
	return CONTINUE;
}