Loading

Paste #pj8xtnxkg

  1. class MyTest extends AIController {
  2.   oil = false;
  3.   oilFleet = [];
  4.   myRigs = AIList();
  5.   function Start();
  6. }
  7.  
  8. function MyTest::Start() {
  9.   local cargoes = AICargoList();
  10.   foreach (cargo, value in cargoes) {
  11.     if( AICargo.GetCargoLabel(cargo) == "OIL_") {
  12.       oil = cargo;
  13.       break;
  14.     }
  15.   }
  16.  
  17.   local oilrigs = AIIndustryList_CargoProducing(oil);
  18.   oilrigs.Valuate(AIIndustry.HasDock);
  19.   oilrigs.KeepValue(1);
  20.  
  21.   oilrigs.Valuate(AIIndustry.GetLastMonthProduction, oil);
  22.   local myRig = oilrigs.Begin();
  23.   AISign.BuildSign(AIIndustry.GetLocation(myRig), "origin");
  24.   myRigs.AddItem(myRig, 0);
  25.  
  26.   local refineries = AIIndustryList_CargoAccepting(oil);
  27.   refineries.Valuate(AIIndustry.GetDistanceManhattanToTile, AIIndustry.GetLocation(myRig));
  28.   refineries.Sort(AIList.SORT_BY_VALUE, true);
  29.   local myRef = refineries.Begin();
  30.   AISign.BuildSign(AIIndustry.GetLocation(myRef), "destination");
  31.  
  32.   local myDock = this.BuildDock(myRef);
  33.  
  34.   local stations = AIStationList(AIStation.STATION_DOCK);
  35.  
  36.   local buoys = this.BuildBuoys(myDock, myRig);
  37.   local depot = this.BuildDepot(AIIndustry.GetLocation(myRig));
  38.   local ship = this.BuildShip(depot, myDock, myRig, buoys);
  39.  
  40.   while (true) {
  41.     this.ManageFleet();
  42.     this.Sleep(72);
  43.   }
  44. }
  45.  
  46. function MyTest::BuildDock(industry) {
  47.   local dockLocation = false;
  48.  
  49.   // Depending on the Realistic Catchment Area setting, the catchment area radius
  50.   // for a dock can be either 4 or 5.
  51.   local accepting = AITileList_IndustryAccepting(industry, 4);
  52.   accepting.Valuate(AITile.IsCoastTile);
  53.   accepting.KeepValue(1);
  54.  
  55.   foreach(myTile, value in accepting) {
  56.     local slope = AITile.GetSlope(myTile);
  57.     // Keep trying to build a dock until it succeeds
  58.     if (AIMarine.BuildDock(myTile, AIStation.STATION_NEW)) {
  59.       dockLocation = myTile;
  60.       break;
  61.     }
  62.   }
  63.   return dockLocation;
  64. }
  65.  
  66. function MyTest::BuildBuoys(dock, rig) {
  67.   local rigStation = AIIndustry.GetDockLocation(rig);
  68.   local distance = AIMap.DistanceMax(dock, rigStation);
  69.  
  70.   local xDistance = AIMap.GetTileX(rigStation) - AIMap.GetTileX(dock);
  71.   local yDistance = AIMap.GetTileY(rigStation) - AIMap.GetTileY(dock);
  72.  
  73.   // This part is built on assumption that it's a good idea to place a buoy
  74.   // about every 20 tiles, par https://wiki.openttd.org/Building_buoys#Finding_a_suitable_location
  75.   local numSegments = distance / 20;
  76.   local buoys = AIList();
  77.   if (numSegments > 0) {
  78.     local xSegSize = xDistance / numSegments;
  79.     local ySegSize = yDistance / numSegments;
  80.  
  81.     local xLoc = AIMap.GetTileX(dock);
  82.     local yLoc = AIMap.GetTileY(dock);
  83.     for (local i = 0; i < numSegments; i++) {
  84.       xLoc = xLoc + xSegSize;
  85.       yLoc = yLoc + ySegSize;
  86.       local buoy = this.BuildBuoy(xLoc, yLoc);
  87.       if (buoy) {
  88.         buoys.AddItem(buoy, 0);
  89.       }
  90.     }
  91.   }
  92.  
  93.   return buoys;
  94. }
  95.  
  96. function MyTest::BuildBuoy(x, y) {
  97.   local tile = AIMap.GetTileIndex(x, y);
  98.   if (AIMarine.BuildBuoy(tile)) {
  99.     return AIWaypoint.GetWaypointID(tile);
  100.   }
  101.   else {
  102.     return false;
  103.   }
  104. }
  105.  
  106. function MyTest::BuildDepot(tile) {
  107.   local xLoc = AIMap.GetTileX(tile);
  108.   local yLoc = AIMap.GetTileY(tile);
  109.   local myDock = false;
  110.  
  111.   xLoc = xLoc - 5;
  112.   yLoc = yLoc - 5;
  113.   if (xLoc < 0) xLoc = 0;
  114.   if (yLoc < 0) yLoc = 0;
  115.  
  116.   for (local x = 0; x < 12; x++) {
  117.     for (local y = 0; y < 12; y++) {
  118.       local tile1 = AIMap.GetTileIndex(xLoc+x, yLoc+y);
  119.       local tile2 = AIMap.GetTileIndex(xLoc+x+1, yLoc+y);
  120.       if (AIMarine.BuildWaterDepot(tile1, tile2)) {
  121.         myDock = tile1;
  122.         break;
  123.       }
  124.     }
  125.     if (myDock) {
  126.       break;
  127.     }
  128.   }
  129.   return myDock;
  130. }
  131.  
  132. function MyTest::BuildShip(depot, dock, rig, buoys) {
  133.   local engines = AIEngineList(AIVehicle.VT_WATER);
  134.   engines.Valuate(AIEngine.CanRefitCargo, oil);
  135.   engines.KeepValue(1);
  136.   engines.Valuate(AIEngine.GetCapacity);
  137.   local engine = engines.Begin();
  138.  
  139.   local ship = AIVehicle.BuildVehicle(depot, engine);
  140.  
  141.   AIVehicle.RefitVehicle(ship, oil);
  142.   local rigStation = AIIndustry.GetDockLocation(rig);
  143.   AIOrder.InsertOrder(ship, 0, rigStation, AIOrder.OF_FULL_LOAD_ANY);
  144.   local counter = 1;
  145.  
  146.   foreach (buoy, value in buoys) {
  147.     local location = AIWaypoint.GetLocation(buoy);
  148.     AIOrder.InsertOrder(ship, counter, location, AIOrder.OF_NONE);
  149.     counter++;
  150.   }
  151.  
  152.   AIOrder.InsertOrder(ship, counter, dock, (AIOrder.OF_NO_LOAD + AIOrder.OF_UNLOAD));
  153.   counter++;
  154.  
  155.   buoys.Sort(AIList.SORT_BY_ITEM, true);
  156.   foreach (buoy, value in buoys) {
  157.     local location = AIWaypoint.GetLocation(buoy);
  158.     AIOrder.InsertOrder(ship, counter, location, AIOrder.OF_NONE);
  159.     counter++;
  160.   }
  161.  
  162.   AIVehicle.StartStopVehicle(ship);
  163.   Sleep(500);
  164.  
  165.   return ship;
  166. }
  167.  
  168. function MyTest::ManageFleet() {
  169.   foreach (rig, value in myRigs) {
  170.     local dockLocation = AIIndustry.GetDockLocation(rig);
  171.     local dock = AIStation.GetStationID(dockLocation);
  172.     if (AIStation.HasCargoRating(dock, oil)) {
  173.       // If a station rating falls below 57%, clone a ship
  174.       if (AIStation.GetCargoRating(dock, oil) < 57) {
  175.         this.CloneFleet(dock);
  176.       }
  177.     }
  178.   }
  179. }
  180.  
  181. function MyTest::CloneFleet(station) {
  182.   local vehicles = AIVehicleList_Station(station);
  183.   local vehicle = vehicles.Begin();
  184.   local stationLocation = AIStation.GetLocation(station);
  185.  
  186.   local depots = AIDepotList(AITile.TRANSPORT_WATER);
  187.   depots.Valuate(AITile.GetDistanceSquareToTile, stationLocation);
  188.   depots.Sort(AIList.SORT_BY_VALUE, true);
  189.   local depot = depots.Begin();
  190.  
  191.   local newShip = AIVehicle.CloneVehicle(depot, vehicle, true);
  192.   AIVehicle.StartStopVehicle(newShip);
  193.   Sleep(500);
  194. }

Version history

Revision # Author Created at
p2e9mosxp Sylf 27 Aug 2017, 05:14:32 UTC Diff
pdfyikkqi Sylf 26 Aug 2017, 17:47:49 UTC Diff
pfif4lzeo Sylf 26 Aug 2017, 03:36:24 UTC Diff
prif7bccz Sylf 26 Aug 2017, 03:31:28 UTC Diff

Comments