Loading

Paste #pdvlasfad

  1. class LuDiAIAfterFix extends AIController {
  2.     MAX_TOWN_VEHICLES = AIGameSettings.GetValue("max_roadveh");
  3.     MIN_DISTANCE = 40;
  4.     MAX_DISTANCE = 115;
  5.     MAX_DISTANCE_INCREASE = 1.2;
  6.  
  7.     townManager = null;
  8.     routeManager = null;
  9.     buildManager = null;
  10.     scheduledRemoves = AIList();
  11.  
  12.     cargoClass = null;
  13.  
  14.     bestRoutesBuilt = null;
  15.     allRoutesBuilt = null;
  16.  
  17.     wrightAI = null;
  18.    
  19.     loading = null;
  20.     loadData = null;
  21.  
  22.     constructor() {
  23.         townManager = TownManager();
  24.         routeManager = RouteManager();
  25.         buildManager = BuildManager();
  26.  
  27.         if(!AIController.GetSetting("select_town_cargo")) {
  28.             cargoClass = AICargo.CC_PASSENGERS;
  29.         }
  30.         else {
  31.             cargoClass = AICargo.CC_MAIL;
  32.         }
  33.  
  34.         bestRoutesBuilt = false;
  35.         allRoutesBuilt = false;
  36.  
  37.         wrightAI = WrightAI(cargoClass, MIN_DISTANCE, MAX_DISTANCE);
  38.        
  39.         loading = true;
  40.     }
  41.  
  42.     function Start();
  43. ()
  44.     function Save() {
  45. //      while(loading) {
  46. //          AILog.Warning("Still loading, can't save yet...")
  47. //      }
  48.         AILog.Warning("Saving...");
  49.  
  50.         local table = {};
  51.         table.rawset("town_manager", townManager.saveTownManager());
  52.         table.rawset("route_manager", routeManager.saveRouteManager());
  53.         table.rawset("build_manager", buildManager.saveBuildManager());
  54.        
  55.         local scheduledRemovesTable = {};
  56.         for (local tile = scheduledRemoves.Begin(), i = 0; scheduledRemoves.HasNext(); tile = scheduledRemoves.Next(), ++i) {
  57.             scheduledRemovesTable.rawset(i, [tile, scheduledRemoves.GetValue(tile)]);
  58.         }
  59.         table.rawset("scheduled_removes", scheduledRemovesTable);
  60.  
  61.         table.rawset("best_routes_built", bestRoutesBuilt);
  62.         table.rawset("all_routes_built", allRoutesBuilt);
  63.  
  64.         table.rawset("wrightai", wrightAI.save());
  65.  
  66.         AILog.Warning("Saved!");
  67.  
  68.         return table;
  69.     }
  70.  
  71.     function Load(version, data) {
  72.         loading = true;
  73.         loadData = data;
  74.         AILog.Info("Loading...");
  75.     }
  76. }
  77.  
  78. function LuDiAIAfterFix::Start() {
  79.     local cargoId = Utils.getCargoId(cargoClass);
  80.     local cargostr = AICargo.GetCargoLabel(cargoId);
  81.     if (!AICompany.SetName("LuDiAI AfterFix " + cargostr)) {
  82.         local i = 2;
  83.         while (!AICompany.SetName("LuDiAI AfterFix " + cargostr + " #" + i)) {
  84.             ++i;
  85.         }
  86.     }
  87.    
  88.     if (loading) {
  89.         if (loadData != null) {
  90.             if(loadData.rawin("town_manager")) {
  91.                 townManager.loadTownManager(loadData.rawget("town_manager"));
  92.             }
  93.  
  94.             if(loadData.rawin("route_manager")) {
  95.                 routeManager.loadRouteManager(loadData.rawget("route_manager"));
  96.             }
  97.  
  98.             if(loadData.rawin("build_manager")) {
  99.                 buildManager.loadBuildManager(loadData.rawget("build_manager"));
  100.             }
  101.  
  102.             if(loadData.rawin("scheduled_removes")) {
  103.                 local scheduledRemovesTable = loadData.rawget("scheduled_removes");
  104.                 local i = 0;
  105.                 while(scheduledRemovesTable.rawin(i)) {
  106.                     local tile = scheduledRemovesTable.rawget(i);
  107.                     scheduledRemoves.AddItem(tile[0], tile[1]);
  108.                     ++i;
  109.                 }
  110.             }
  111.             AILog.Info("Loaded " + scheduledRemoves.Count() + " scheduled removals.");
  112.  
  113.             if(loadData.rawin("best_routes_built")) {
  114.                 bestRoutesBuilt = loadData.rawget("best_routes_built");
  115.             }
  116.             AILog.Info("Loaded " + bestRoutesBuilt + " best routes build.");
  117.  
  118.             if(loadData.rawin("all_routes_built")) {
  119.                 allRoutesBuilt = loadData.rawget("all_routes_built");
  120.             }
  121.             AILog.Info("Loaded " + allRoutesBuilt + " all routes built.");
  122.  
  123.             if(buildManager.hasUnfinishedRoute()) {
  124.                 AILog.Warning("Unfinished route between " + AITown.GetName(buildManager.m_cityFrom) + " and " + AITown.GetName(buildManager.m_cityTo) + " detected!");
  125.             }
  126.             AILog.Info("Loaded " + buildManager.hasUnfinishedRoute() + " has unfinished route.");
  127.  
  128.             if(loadData.rawin("wrightai")) {
  129.                 wrightAI.load(loadData.rawget("wrightai"));
  130.             }
  131.             AILog.Info("Loaded " + wrightAI + " wrightAI.");
  132.  
  133.             AILog.Warning("Game loaded.");
  134.             loadData = null;
  135.         }
  136.         loading = false;
  137.     }
  138. ()

Comments