Loading

Paste #pqdtmtd9t

  1. /**
  2.  * Find a suitable spot for an airport, walking all towns hoping to find one.
  3.  * When a town is used, it is marked as such and not re-used.
  4.  */
  5. function WrightAI::FindSuitableAirportSpot(airportTypes, airport1_tile, large_aircraft, small_aircraft, helicopter, triedTowns)
  6. {
  7.     local town_list = AITownList();
  8.     /* Remove all the towns we already used */
  9.     town_list.RemoveList(this.towns_used);
  10.    
  11.     /* Remove towns we have tried recently */
  12.     town_list.RemoveList(triedTowns);
  13.    
  14.     if (AIController.GetSetting("cities_only")) {
  15.         town_list.Valuate(AITown.IsCity);
  16.         town_list.KeepValue(1);
  17.     }
  18.  
  19.     town_list.Valuate(AITown.GetLastMonthProduction, this.cargoId);
  20.     town_list.KeepAboveValue(LuDiAIAfterFix.cargoClass == AICargo.CC_PASSENGERS ? 70 : 18);
  21.     if (AIController.GetSetting("pick_random")) {
  22.         town_list.Valuate(AIBase.RandItem);
  23.     } else {
  24.         town_list.Sort(AIList.SORT_BY_VALUE, false);
  25.     }
  26.     /* Keep the best 10, if we can't find a station in there, just leave it anyway */
  27.     town_list.KeepTop(10);
  28.  
  29.     /* Now find a suitable town */
  30.     for (local town = town_list.Begin(); town_list.HasNext(); town = town_list.Next()) {
  31. //      /* Don't make this a CPU hog */
  32. //      Sleep(1);
  33.         local town_tile = AITown.GetLocation(town);
  34.         AILog.Info("Checking town " + AITown.GetName(town));
  35.        
  36.         local tileList = AITileList();
  37.         for (local i = airportTypes.Begin(); airportTypes.HasNext(); i = airportTypes.Next()) {
  38.             local a = airportTypes.GetValue(i);
  39.             local airport_x = AIAirport.GetAirportWidth(a);
  40.             local airport_y = AIAirport.GetAirportHeight(a);
  41.             local airport_rad = AIAirport.GetAirportCoverageRadius(a);
  42. //          AILog.Info("Checking airport of type " + a);
  43.             if (!AIAirport.IsValidAirportType(a)) continue;
  44.        
  45.             if (large_aircraft && a != AIAirport.AT_INTERCON && a != AIAirport.AT_INTERNATIONAL && a != AIAirport.AT_METROPOLITAN && a != AIAirport.AT_LARGE) {
  46.                 continue;
  47.             }
  48.             if (small_aircraft && (a == AIAirport.AT_INTERCON || a == AIAirport.AT_INTERNATIONAL || a == AIAirport.AT_METROPOLITAN || a == AIAirport.AT_LARGE)) {
  49.                 continue;
  50.             }
  51.             if (helicopter && a != AIAirport.AT_HELISTATION && a != AIAirport.AT_HELIDEPOT && a != AIAirport.AT_HELIPORT) {
  52.                 continue;
  53.             }
  54.  
  55.             local rectangleCoordinates = this.TownAirportRadRect(a, town);
  56.             tileList.AddRectangle(rectangleCoordinates[0], rectangleCoordinates[1]);
  57.             tileList.Valuate(AITile.GetClosestTown);
  58.             tileList.KeepValue(town);
  59.             tileList.Valuate(AITile.IsBuildableRectangle, airport_x, airport_y);
  60.             tileList.KeepValue(1)
  61.             if (tileList.Count() == 0) continue;
  62.        
  63.             if (airport1_tile != 0) {
  64.                 /* If we have the tile of the first airport, we don't want the second airport to be as close */
  65.                 tileList.Valuate(AITile.GetDistanceManhattanToTile, airport1_tile);
  66.                 tileList.KeepAboveValue(min_air_distance);
  67.                 if (tileList.Count() == 0) continue;
  68.             }
  69.             /* Sort on acceptance, remove places that don't have acceptance */
  70.             tileList.Valuate(AITile.GetCargoAcceptance, this.cargoId, airport_x, airport_y, airport_rad);
  71.             tileList.RemoveBelowValue(10);
  72.             if (tileList.Count() == 0) continue;
  73.        
  74.             tileList.Valuate(AITile.GetCargoProduction, this.cargoId, airport_x, airport_y, airport_rad);
  75.             tileList.RemoveBelowValue(10);
  76.             if (tileList.Count() == 0) continue;
  77.             tileList.Sort(AIList.SORT_BY_VALUE, false);
  78.        
  79.             /* Couldn't find a suitable place for this town, skip to the next */
  80.             if (tileList.Count() == 0) continue;
  81.        
  82.             /* Walk all the tiles and see if we can build the airport at all */
  83.             local test = AITestMode();
  84.             local good_tile = 0;
  85.             for (local tile = tileList.Begin(); tileList.HasNext(); tile = tileList.Next()) {
  86.                 local adjacentStationId = checkAdjacentStation(tile, a);
  87.                 local nearest_town;
  88.                 if (adjacentStationId == AIStation.STATION_NEW) {
  89.                     nearest_town = AITile.GetClosestTown(tile);
  90.                 } else {
  91.                     nearest_town = AIStation.GetNearestTown(adjacentStationId);
  92.                 }
  93.                 local noise = AIAirport.GetNoiseLevelIncrease(tile, a);
  94.                 local allowed_noise = AITown.GetAllowedNoise(AIAirport.GetNearestTown(tile, a));
  95.                 if (noise > allowed_noise) continue;
  96. //              AISign.BuildSign(tile, ("" + noise + " <= " + allowed_noise + ""));
  97.                 Sleep(1);
  98. //              local test = AITestMode();
  99.                 if (!AIAirport.BuildAirport(tile, a, AIStation.STATION_NEW)) continue;
  100.                 good_tile = tile;
  101. //              AILog.Info("Found a good spot for an airport near " + AITown.GetName(nearest_town) + " or " + AITown.GetName(aitile) + " at tile " + good_tile);
  102.                 AILog.Info("Found a good spot for an airport near " + AITown.GetName(nearest_town) + " at tile " + good_tile);
  103.  
  104.                 /* Make the town as used, so we don't use it again */
  105.                 assert(!towns_used.HasItem(nearest_town) && town == nearest_town);
  106.                 this.towns_used.AddItem(nearest_town, good_tile);
  107.                
  108.                 if (a == AIAirport.AT_INTERCON || a == AIAirport.AT_INTERNATIONAL || a == AIAirport.AT_METROPOLITAN || a == AIAirport.AT_LARGE) {
  109.                     large_aircraft = true;
  110.                 }
  111.                 if (a == AIAirport.AT_SMALL || a == AIAirport.AT_COMMUTER) {
  112.                     small_aircraft = true;
  113.                 }
  114.                 if (a == AIAirport.AT_HELISTATION || a == AIAirport.AT_HELIDEPOT || a == AIAirport.AT_HELIPORT) {
  115.                     helicopter = true;
  116.                 }
  117.                 return [good_tile, a, large_aircraft, small_aircraft, helicopter, triedTowns];
  118.             }
  119.         }
  120.        
  121.         /* All airport types were tried on this town and no suitable location was found */
  122.         if (airport1_tile == 0) {
  123.             triedTowns.AddItem(town, town_tile);
  124.         }
  125.     }
  126.    
  127.     /* We haven't found a suitable location for any airport type in any town */
  128.     return [-1, AIAirport.AT_INVALID, large_aircraft, small_aircraft, helicopter, triedTowns];
  129. }

Comments