//////////////////////////////////////////////////////////////////////////// // // Air Unit script to make AI move air units back to base. // // By Dale // // Version 1.1 14-May-2001 // //////////////////////////////////////////////////////////////////////////// /////////////////////////////////// // Mod notes! // // Variables: // AIR_TYPE[] - Contains the link to any air units contained in the mod/scenario. // Must be changed if you have different to the default CTP2 units. // LANDING_TYPE[] - Contains the link to any units/tile improvements that an aircraft can land on. // // This script will check every air unit in play for the current AI player, find a landing tile, and // direct the unit to that tile. If the air unit is already on a landing tile, it won't move. // // In MM2_IsLandingUnit, the script checks for landing units (like aircraft carrier) in the tile // supplied by the MoveOrder event, and then checks for tile improvements (like airbases). If either // is found, then the script directs the AI's air unit to that tile. // /////////////////////////////////// /////////////////////////////////// // Global variables /////////////////////////////////// int_t AIR_TYPE[]; // Array to hold air unit DB values int_t LANDING_TYPE[]; // Array to hold valid landing sites /////////////////////////////////// // Begin Game event /////////////////////////////////// HandleEvent(BeginTurn) 'MM2_AirGameStart' pre { AIR_TYPE[0] = UnitDB(UNIT_BOMBER); AIR_TYPE[1] = UnitDB(UNIT_CARGO_HELICOPTER); AIR_TYPE[2] = UnitDB(UNIT_CRUISE_MISSILE); AIR_TYPE[3] = UnitDB(UNIT_FIGHTER); AIR_TYPE[4] = UnitDB(UNIT_INTERCEPTOR); AIR_TYPE[5] = UnitDB(UNIT_NUKE); AIR_TYPE[6] = UnitDB(UNIT_SPACE_PLANE); AIR_TYPE[7] = UnitDB(UNIT_SPY_PLANE); AIR_TYPE[8] = UnitDB(UNIT_STEALTH_BOMBER); AIR_TYPE[9] = UnitDB(UNIT_STEALTH_FIGHTER); LANDING_TYPE[0] = UnitDB(UNIT_AIRCRAFT_CARRIER); LANDING_TYPE[1] = TerrainImprovementDB(TILEIMP_AIR_BASES); DisableTrigger('MM2_AirGameStart'); } /////////////////////////////////// // Check if air unit /////////////////////////////////// int_f MM2_IsAir(int_t theType) { int_t i; int_t typeOfUnit; typeOfUnit = theType; for (i = 0; i < AIR_TYPE.#; i = i + 1) { if (typeOfUnit == AIR_TYPE[i]) { return 1; } } return 0; } /////////////////////////////////// // Check for landing unit /////////////////////////////////// int_f MM2_IsLandingUnit(location_t theLoc) { int_t i; int_t units; unit_t tmpland; location_t thesite; thesite = theLoc; units = GetUnitsAtLocation(thesite); if(units > 0) { for(i = 0; i < units; i = i + 1) { GetUnitFromCell(thesite, i, tmpland); if(tmpland.type == LANDING_TYPE[0]) { return 1; } } } if(TileHasImprovement(thesite, LANDING_TYPE[1])) { return 1; } return 0; } /////////////////////////////////// // Move Unit event /////////////////////////////////// HandleEvent(MoveOrder) 'MM2_AirMove' pre { int_t i; int_t j; int_t h; int_t x; int_t y; int_t numcities; int_t armycounter; city_t tmpcity; unit_t tmpUnit; army_t tmpArmy; army_t tmpArmy1; location_t tmploc; location_t tmploc1; location_t tmploc3; location_t tmploc4; location_t armyloc; armycounter = 0; tmpArmy = army[0]; armyloc = tmpArmy.location; tmploc3 = armyloc; player[0] = g.player; for (i = 0; i < tmpArmy.size; i = i + 1) { GetUnitFromCell(armyloc, i, tmpUnit); if(MM2_IsAir(tmpUnit.type)) { numcities = player[0].cities; GetCityByIndex(player[0], 0, tmpcity); tmploc = tmpcity.location; for(h = 0; h < numcities; h = h + 1) { GetCityByIndex(player[0], h, tmpcity); tmploc1 = tmpcity.location; if(CityIsValid(tmpcity) && Distance(tmpUnit.location, tmploc1) < Distance(tmpUnit.location, tmploc)) { tmploc = tmploc1; } } for(j = 0; j < 10; j = j + 1) { GetNeighbor(tmploc3, 3, tmploc4); tmploc3 = tmploc4; } if(MM2_IsLandingUnit(tmploc3)) { if(Distance(tmpUnit.location, tmploc3) < Distance(tmpUnit.location, tmploc)) { tmploc = tmploc3; } } for(x = 0; x < 10; x = x + 1) { for(y = 0; y < 21; y = y + 1) { GetNeighbor(tmploc3, 2, tmploc4); tmploc3 = tmploc4; if(MM2_IsLandingUnit(tmploc3)) { if(Distance(tmpUnit.location, tmploc3) < Distance(tmpUnit.location, tmploc)) { tmploc = tmploc3; } } } GetNeighbor(tmploc3, 7, tmploc4); tmploc3 = tmploc4; if(MM2_IsLandingUnit(tmploc3)) { if(Distance(tmpUnit.location, tmploc3) < Distance(tmpUnit.location, tmploc)) { tmploc = tmploc3; } } for(y = 0; y < 21; y = y + 1) { GetNeighbor(tmploc3, 5, tmploc4); tmploc3 = tmploc4; if(MM2_IsLandingUnit(tmploc3)) { if(Distance(tmpUnit.location, tmploc3) < Distance(tmpUnit.location, tmploc)) { tmploc = tmploc3; } } } GetNeighbor(tmploc3, 7, tmploc4); tmploc3 = tmploc4; if(MM2_IsLandingUnit(tmploc3)) { if(Distance(tmpUnit.location, tmploc3) < Distance(tmpUnit.location, tmploc)) { tmploc = tmploc3; } } } if(armyloc != tmploc) { Event: UngroupOrder(tmpArmy); ClearOrders(tmpUnit); Event: AddUnitToArmy(tmpUnit, tmpArmy1, armycounter); armycounter = armycounter + 1; Event: MovePathOrder(tmpArmy1, tmploc); } } } }