Loading

Paste #ptzbjupwa

  1. /**
  2.  * Build an airport route. Find 2 cities that are big enough and try to build airport in both cities.
  3.  * Then we can build an aircraft and make some money.
  4.  */
  5. function WrightAI::BuildAirportRoute()
  6. {
  7.     /* Check if we can build more aircraft. */
  8.     if (GetAircraftCount() >= AIGameSettings.GetValue("max_aircraft") || AIGameSettings.IsDisabledVehicleType(AIVehicle.VT_AIR)) return 0;
  9.    
  10.     /* Create a list of available airports */
  11.     local airportTypes = AIList();
  12.     airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_INTERCON), AIAirport.AT_INTERCON);           // 7
  13.     airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_INTERNATIONAL), AIAirport.AT_INTERNATIONAL); // 4
  14.     airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_METROPOLITAN), AIAirport.AT_METROPOLITAN);   // 3
  15.     airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_LARGE), AIAirport.AT_LARGE);                 // 1
  16.     airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_COMMUTER), AIAirport.AT_COMMUTER);           // 5
  17.     airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_SMALL), AIAirport.AT_SMALL);                 // 0
  18.     airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_HELISTATION), AIAirport.AT_HELISTATION);     // 8
  19.     airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_HELIDEPOT), AIAirport.AT_HELIDEPOT);         // 6
  20.     airportTypes.AddItem(AIAirport.GetPrice(AIAirport.AT_HELIPORT), AIAirport.AT_HELIPORT);           // 2
  21.    
  22.     /* Filter out airports larger than the maximum value of a station size */
  23.     local list = airportTypes;
  24.     local station_spread = AIGameSettings.GetValue("station_spread");
  25.     for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
  26.         local airport_type = airportTypes.GetValue(i);
  27. //      AILog.Info("airport_type = " + airport_type);
  28.         local airport_x = AIAirport.GetAirportWidth(airport_type);
  29. //      AILog.Info("airport_x = " + airport_x);
  30.         local airport_y = AIAirport.GetAirportHeight(airport_type);
  31. //      AILog.Info("airport_y = " + airport_y);
  32.         if (airport_x > station_spread || airport_y > station_spread) {
  33.             airportTypes.RemoveValue(airport_type);
  34.         }
  35.         /* Also filter out unavailable airports */
  36.         if (!AIAirport.IsValidAirportType(airport_type)) {
  37.             airportTypes.RemoveValue(airport_type);
  38.         }
  39. //      /* 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 */
  40. //      if (airport_type == AIAirport.AT_HELIPORT && !AIAirport.IsValidAirportType(AIAirport.AT_HELISTATION) && !AIAirport.IsValidAirportType(AIAirport.AT_HELIDEPOT)) {
  41. //          airportTypes.RemoveValue(airport_type);
  42. //      }
  43.     }
  44.     /* No airports available. Abort */
  45.     if (airportTypes.Count() == 0) return 0;
  46.    
  47.     local available_engines = false;
  48.     local engine_costs = 0;
  49.     if (big_engine_list.Count() == 0) {
  50. //      airportTypes.RemoveValue(AIAirport.AT_INTERCON);
  51. //      airportTypes.RemoveValue(AIAirport.AT_INTERNATIONAL);
  52. //      airportTypes.RemoveValue(AIAirport.AT_METROPOLITAN);
  53. //      airportTypes.RemoveValue(AIAirport.AT_LARGE);
  54.     } else {
  55.         available_engines = true;
  56.         engine_costs = engine_costs + AIEngine.GetPrice(big_engine_list.Begin());
  57.     }
  58.  
  59.     if (small_engine_list.Count() == 0) {
  60. //      airportTypes.RemoveValue(AIAirport.AT_COMMUTER);
  61. //      airportTypes.RemoveValue(AIAirport.AT_SMALL);
  62.     } else {
  63.         available_engines = true;
  64.         engine_costs = engine_costs + AIEngine.GetPrice(small_engine_list.Begin());
  65.     }
  66.    
  67.     if (helicopter_list.Count() == 0) {
  68.         airportTypes.RemoveValue(AIAirport.AT_HELISTATION);
  69.         airportTypes.RemoveValue(AIAirport.AT_HELIDEPOT);
  70.         airportTypes.RemoveValue(AIAirport.AT_HELIPORT);
  71.     } else {
  72.         available_engines = true;
  73.         engine_costs = engine_costs + AIEngine.GetPrice(helicopter_list.Begin());
  74.     }
  75.    
  76.     /* There are no engines available */
  77.     if (!available_engines) return 0;
  78.    
  79.     /* Not enough money */
  80.     local estimated_costs = airportTypes.Begin() + engine_costs;
  81.     if (!this.HasMoney(estimated_costs + 25000)) return 0;
  82.  
  83. //  AILog.Info("airportTypes contains " + airportTypes.Count() + " type " + airportTypes.GetValue(0));
  84.     airportTypes.Sort(AIList.SORT_BY_ITEM, AIList.SORT_DESCENDING);
  85.    
  86.     /* Ensure there are at least 2 unused towns */
  87.     local town_list = AITownList();
  88.     if (AIController.GetSetting("cities_only")) {
  89.         town_list.Valuate(AITown.IsCity);
  90.         town_list.KeepValue(1);
  91.     }
  92.     if (town_list.Count() - this.towns_used.Count() < 2) return 0;
  93.  
  94.     AILog.Info("Trying to build an airport route...");
  95.    
  96.     local tile_1 = this.FindSuitableAirportSpot(airportTypes, 0, false, false, false);
  97.    
  98.     local airport1_location = tile_1[0];
  99.     local airport1_type = tile_1[1];
  100.     local airport1_stationId = tile_1[5];
  101.    
  102.     if (airport1_location < 0) {
  103.         AILog.Info("Couldn't find a suitable town to build the first airport in");
  104.         return -1;
  105.     }
  106.    
  107.     if (airport1_type == AIAirport.AT_HELIPORT) {
  108.         airportTypes.RemoveValue(AIAirport.AT_HELIPORT);
  109.         airportTypes.Sort(AIList.SORT_BY_ITEM, AIList.SORT_ASCENDING);
  110.     }
  111.    
  112.     local large_aircraft = tile_1[2];
  113.     local small_aircraft = tile_1[3];
  114.     local helicopter = tile_1[4];
  115.    
  116.     local tile_2 = this.FindSuitableAirportSpot(airportTypes, airport1_location, large_aircraft, small_aircraft, helicopter);
  117.    
  118.     local airport2_location = tile_2[0];
  119.     local airport2_type = tile_2[1];
  120.     local airport2_stationId = tile_2[5];
  121.    
  122.     if (airport2_location < 0) {
  123.         AILog.Info("Couldn't find a suitable town to build the second airport in");
  124.         return -1;
  125.     }
  126.  
  127.     /* Build the airports for real */
  128. //  local last_cost.AIAccounting();
  129.     local tap = TestBuildAirport();
  130.     if (!tap.TryBuild(airport1_location, airport1_type, airport1_stationId)) {
  131. // 
  132. //  local last_cost = AIAccounting();
  133. //  if (AITestMode() && AIAirport.BuildAirport(airport1_location, airport1_type, airport1_stationId)) {
  134. //      if (HasMoney(last_cost.GetCosts())) GetMoney(last_cost.GetCosts());
  135. //  }
  136. //  if (!AIAirport.BuildAirport(airport1_location, airport1_type, airport1_stationId)) {
  137. //      RepayLoan();
  138.         AILog.Warning("Although the testing told us we could build 2 airports, it still failed on the first airport at tile " + airport1_location + ".");
  139. //      assert(AIError.GetLastError() != AIError.ERR_STATION_TOO_SPREAD_OUT);
  140.         return -1;
  141.     }
  142. //  RepayLoan();
  143. //
  144. //  last_cost.ResetCosts();
  145.     tap = TestBuildAirport();
  146.     if (!tap.TryBuild(airport2_location, airport2_type, airport2_stationId)) {
  147. // 
  148. //  if (AITestMode() && AIAirport.BuildAirport(airport2_location, airport2_type, airport2_stationId)) {
  149. //      if (HasMoney(last_cost.GetCosts())) GetMoney(last_cost.GetCosts());
  150. //  }
  151. //  if (!AIAirport.BuildAirport(airport2_location, airport2_type, airport2_stationId)) {
  152. //      RepayLoan();
  153.         AILog.Warning("Although the testing told us we could build 2 airports, it still failed on the second airport at tile " + airport2_location + ".");
  154. //      assert(AIError.GetLastError() != AIError.ERR_STATION_TOO_SPREAD_OUT);
  155.         return -1;
  156.     }
  157. //  RepayLoan();
  158.  
  159.     local ret = this.BuildAircraft(airport1_location, airport2_location, false, GetAircraftCount() == 0 ? true : false);
  160.     if (ret < 0) {
  161.         return ret;
  162.     }
  163.    
  164.     local airport1_town = AITile.GetClosestTown(airport1_location);
  165.     local airport2_town = AITile.GetClosestTown(airport2_location);
  166.     this.towns_used.AddItem(airport1_town, airport1_location);
  167.     this.towns_used.AddItem(airport2_town, airport2_location);
  168.  
  169. //  AILog.Warning("Done building aicraft route.");
  170.     return ret;
  171. }

Comments