/**
* Build an airport route. Find 2 cities that are big enough and try to build airport in both cities.
* Then we can build an aircraft and make some money.
*/
function WrightAI::BuildAirportRoute()
{
/* Check if we can build more aircraft. */
if (GetAircraftCount() >= AIGameSettings.GetValue("max_aircraft") || AIGameSettings.IsDisabledVehicleType(AIVehicle.VT_AIR)) return 0;
/* Create a list of available airports */
local airportTypes = AIList();
airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_INTERCON), AIAirport.AT_INTERCON); // 7
airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_INTERNATIONAL), AIAirport.AT_INTERNATIONAL); // 4
airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_METROPOLITAN), AIAirport.AT_METROPOLITAN); // 3
airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_LARGE), AIAirport.AT_LARGE); // 1
airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_COMMUTER), AIAirport.AT_COMMUTER); // 5
airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_SMALL), AIAirport.AT_SMALL); // 0
airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_HELISTATION), AIAirport.AT_HELISTATION); // 8
airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_HELIDEPOT), AIAirport.AT_HELIDEPOT); // 6
airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_HELIPORT), AIAirport.AT_HELIPORT); // 2
/* Filter out airports larger than the maximum value of a station size */
local list = airportTypes;
local station_spread = AIGameSettings.GetValue("station_spread");
for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
local airport_type = airportTypes.GetValue(i);
// AILog.Info("airport_type = " + airport_type);
local airport_x = AIAirport.GetAirportWidth(airport_type);
// AILog.Info("airport_x = " + airport_x);
local airport_y = AIAirport.GetAirportHeight(airport_type);
// AILog.Info("airport_y = " + airport_y);
if (airport_x > station_spread || airport_y > station_spread) {
airportTypes.RemoveValue(airport_type);
}
/* Also filter out unavailable airports */
if (!AIAirport.IsValidAirportType(airport_type)) {
airportTypes.RemoveValue(airport_type);
}
// /* Filter out heliports while helistations and helidepots aren't available, because it is required that one of the airports in a route to have a hangar */
// if (airport_type == AIAirport.AT_HELIPORT && !AIAirport.IsValidAirportType(AIAirport.AT_HELISTATION) && !AIAirport.IsValidAirportType(AIAirport.AT_HELIDEPOT)) {
// airportTypes.RemoveValue(airport_type);
// }
}
/* No airports available. Abort */
if (airportTypes.Count() == 0) return 0;
local available_engines = false;
local engine_costs = 0;
if (big_engine_list.Count() == 0) {
// airportTypes.RemoveValue(AIAirport.AT_INTERCON);
// airportTypes.RemoveValue(AIAirport.AT_INTERNATIONAL);
// airportTypes.RemoveValue(AIAirport.AT_METROPOLITAN);
// airportTypes.RemoveValue(AIAirport.AT_LARGE);
} else {
available_engines = true;
engine_costs = engine_costs + AIEngine.GetPrice(big_engine_list.Begin());
}
if (small_engine_list.Count() == 0) {
// airportTypes.RemoveValue(AIAirport.AT_COMMUTER);
// airportTypes.RemoveValue(AIAirport.AT_SMALL);
} else {
available_engines = true;
engine_costs = engine_costs + AIEngine.GetPrice(small_engine_list.Begin());
}
if (helicopter_list.Count() == 0) {
airportTypes.RemoveValue(AIAirport.AT_HELISTATION);
airportTypes.RemoveValue(AIAirport.AT_HELIDEPOT);
airportTypes.RemoveValue(AIAirport.AT_HELIPORT);
} else {
available_engines = true;
engine_costs = engine_costs + AIEngine.GetPrice(helicopter_list.Begin());
}
/* There are no engines available */
if (!available_engines) return 0;
/* Not enough money */
local estimated_costs = airportTypes.Begin() + engine_costs;
if (!this.HasMoney(estimated_costs + 25000)) return 0;
// AILog.Info("airportTypes contains " + airportTypes.Count() + " type " + airportTypes.GetValue(0));
airportTypes.Sort(AIList.SORT_BY_ITEM, AIList.SORT_DESCENDING);
/* Ensure there are at least 2 unused towns */
local town_list = AITownList();
if (AIController.GetSetting("cities_only")) {
town_list.Valuate(AITown.IsCity);
town_list.KeepValue(1);
}
if (town_list.Count() - this.towns_used.Count() < 2) return 0;
AILog.Info("Trying to build an airport route...");
local tile_1 = this.FindSuitableAirportSpot(airportTypes, 0, false, false, false);
local airport1_location = tile_1[0];
local airport1_type = tile_1[1];
local airport1_stationId = tile_1[5];
if (airport1_location < 0) {
AILog.Info("Couldn't find a suitable town to build the first airport in");
return -1;
}
if (airport1_type == AIAirport.AT_HELIPORT) {
airportTypes.RemoveValue(AIAirport.AT_HELIPORT);
airportTypes.Sort(AIList.SORT_BY_ITEM, AIList.SORT_ASCENDING);
}
local large_aircraft = tile_1[2];
local small_aircraft = tile_1[3];
local helicopter = tile_1[4];
local tile_2 = this.FindSuitableAirportSpot(airportTypes, airport1_location, large_aircraft, small_aircraft, helicopter);
local airport2_location = tile_2[0];
local airport2_type = tile_2[1];
local airport2_stationId = tile_2[5];
if (airport2_location < 0) {
AILog.Info("Couldn't find a suitable town to build the second airport in");
return -1;
}
/* Build the airports for real */
// local last_cost.AIAccounting();
local tap = TestBuildAirport();
if (!tap.TryBuild(airport1_location, airport1_type, airport1_stationId)) {
//
// local last_cost = AIAccounting();
// if (AITestMode() && AIAirport.BuildAirport(airport1_location, airport1_type, airport1_stationId)) {
// if (HasMoney(last_cost.GetCosts())) GetMoney(last_cost.GetCosts());
// }
// if (!AIAirport.BuildAirport(airport1_location, airport1_type, airport1_stationId)) {
// RepayLoan();
AILog.Warning("Although the testing told us we could build 2 airports, it still failed on the first airport at tile " + airport1_location + ".");
// assert(AIError.GetLastError() != AIError.ERR_STATION_TOO_SPREAD_OUT);
return -1;
}
// RepayLoan();
//
// last_cost.ResetCosts();
tap = TestBuildAirport();
if (!tap.TryBuild(airport2_location, airport2_type, airport2_stationId)) {
//
// if (AITestMode() && AIAirport.BuildAirport(airport2_location, airport2_type, airport2_stationId)) {
// if (HasMoney(last_cost.GetCosts())) GetMoney(last_cost.GetCosts());
// }
// if (!AIAirport.BuildAirport(airport2_location, airport2_type, airport2_stationId)) {
// RepayLoan();
AILog.Warning("Although the testing told us we could build 2 airports, it still failed on the second airport at tile " + airport2_location + ".");
// assert(AIError.GetLastError() != AIError.ERR_STATION_TOO_SPREAD_OUT);
return -1;
}
// RepayLoan();
local ret = this.BuildAircraft(airport1_location, airport2_location, false, GetAircraftCount() == 0 ? true : false);
if (ret < 0) {
return ret;
}
local airport1_town = AITile.GetClosestTown(airport1_location);
local airport2_town = AITile.GetClosestTown(airport2_location);
this.towns_used.AddItem(airport1_town, airport1_location);
this.towns_used.AddItem(airport2_town, airport2_location);
// AILog.Warning("Done building aicraft route.");
return ret;
}