Loading

Paste #pyxd59mk8

  1. require("BuildManager.nut");
  2.  
  3. class RouteManager {
  4.     MAX_STATION_COUNT = 2;
  5.  
  6.     m_townRouteArray = null;
  7.     m_airportTowns = null;
  8.  
  9.     constructor() {
  10.         m_townRouteArray = [];
  11.         m_airportTowns = AIList();
  12.     }
  13.  
  14.     function buildRoute(buildManager, cityFrom, cityTo, cargoClass) {
  15.         local route = buildManager.buildRoute(cityFrom, cityTo, cargoClass);
  16.         if (route != null) {
  17.             m_townRouteArray.append(route);
  18.             buildManager.setRouteFinish();
  19.             return 1;
  20.         }
  21.  
  22.         return null;
  23.     }
  24.  
  25.  
  26.     function getRoadVehicleCount() {
  27.         //local list = AIVehicleList();
  28.         //list.Valuate(AIVehicle.GetVehicleType);
  29.         //list.KeepValue(AIVehicle.VT_ROAD);
  30.         //return list.Count();
  31.  
  32.         local count = 0;
  33.  
  34.         for(local i = 0; i < m_townRouteArray.len(); ++i) {
  35.             count += AIVehicleList_Depot(m_townRouteArray[i].m_depotTile).Count();
  36.         }
  37.  
  38.         return count;
  39.     }
  40.  
  41.     function townRouteExists(cityFrom, cityTo) {
  42.         for(local i = 0; i < m_townRouteArray.len(); ++i) {
  43.             if(TownPair(cityFrom, cityTo).isEqual(m_townRouteArray[i].m_cityFrom, m_townRouteArray[i].m_cityTo)) {
  44.                 return 1;
  45.             }
  46.         }
  47.  
  48.         return 0;
  49.     }
  50.  
  51.     function airRouteExists(cityFrom, cityTo) {
  52.         for(local i = 0; i < m_townRouteArray.len(); ++i) {
  53.             if(TownPair(cityFrom, cityTo).isEqual(m_airRouteArray[i].m_cityFrom, m_airRouteArray[i].m_cityTo)) {
  54.                 return 1;
  55.             }
  56.         }
  57.  
  58.         return 0;
  59.     }
  60.  
  61.     //the highest last years profit out of all vehicles
  62.     function highestProfitLastYear() {
  63.         local maxAllRoutesProfit = 0;
  64.  
  65.         for(local i = 0; i < m_townRouteArray.len(); ++i) {
  66.             local maxRouteProfit = AIVehicleList_Depot(m_townRouteArray[i].m_depotTile);
  67.             maxRouteProfit.Valuate(AIVehicle.GetProfitLastYear);
  68.             maxRouteProfit.Sort(AIList.SORT_BY_VALUE, false);
  69.             maxRouteProfit = maxRouteProfit.GetValue(maxRouteProfit.Begin());
  70.  
  71.             if(maxRouteProfit > maxAllRoutesProfit) {
  72.                 maxAllRoutesProfit = maxRouteProfit;
  73.             }
  74.         }
  75.  
  76.         return maxAllRoutesProfit;
  77.     }
  78.  
  79.     //wont build any new stations if 1
  80.     function hasMaxStationCount(cityFrom, cityTo) {
  81.         local maxTownStationFrom = (1 + AITown.GetPopulation(cityFrom) / 1000).tointeger();
  82.         local maxTownStationTo = (1 + AITown.GetPopulation(cityTo) / 1000).tointeger();
  83.  
  84.         local cityFromCount = 0;
  85.         local cityToCount = 0;
  86.  
  87.         for(local i = 0; i < m_townRouteArray.len(); ++i) {
  88.             if(m_townRouteArray[i].m_cityFrom == cityFrom || m_townRouteArray[i].m_cityFrom == cityTo) {
  89.                 ++cityFromCount;
  90.             }
  91.  
  92.             if(m_townRouteArray[i].m_cityTo == cityTo || m_townRouteArray[i].m_cityTo == cityFrom) {
  93.                 ++cityToCount;
  94.             }
  95.         }
  96.  
  97.         if((cityFromCount >= maxTownStationFrom) || (cityToCount >= maxTownStationTo)) {
  98.             return 1;
  99.         }
  100.  
  101.         return 0;
  102.     }
  103.  
  104.     function saveRouteManager() {
  105.         local table = {};
  106.  
  107.         for(local i = 0; i < m_townRouteArray.len(); ++i) {
  108.             table.rawset(i, m_townRouteArray[i].saveRoute());
  109.         }
  110.  
  111.         return table;
  112.     }
  113.  
  114.     function loadRouteManager(data) {
  115.         if(m_townRouteArray == null) {
  116.             m_townRouteArray = [];
  117.         }
  118.  
  119.         local i = 0;
  120.         while(data.rawin(i)) {
  121.             local route = Route.loadRoute(data.rawget(i));
  122.             m_townRouteArray.append(route);
  123.  
  124.             ++i;
  125.         }
  126.  
  127.         AILog.Info("Loaded " + m_townRouteArray.len() + " routes.");
  128.     }
  129.    
  130. }

Comments