Loading

Paste #p2e9mosxp

  1. class MyTest extends AIController {
  2.   function Start();
  3. }
  4.  
  5. function MyTest::Start() {
  6.   local cargoes = AICargoList();
  7.   local oil;
  8.   foreach (cargo, value in cargoes) {
  9.     if( AICargo.GetCargoLabel(cargo) == "OIL_") {
  10.       oil = cargo;
  11.       break;
  12.     }
  13.   }
  14.  
  15.   local oilrigs = AIIndustryList_CargoProducing(oil);
  16.   oilrigs.Valuate(AIIndustry.HasDock);
  17.   oilrigs.KeepValue(1);
  18.  
  19.   oilrigs.Valuate(AIIndustry.GetLastMonthProduction, oil);
  20.   local myRig = oilrigs.Begin();
  21.   AISign.BuildSign(AIIndustry.GetLocation(myRig), "origin");
  22.  
  23.   local refineries = AIIndustryList_CargoAccepting(oil);
  24.   refineries.Valuate(AIIndustry.GetDistanceManhattanToTile, AIIndustry.GetLocation(myRig));
  25.   refineries.Sort(AIList.SORT_BY_VALUE, true);
  26.   local myRef = refineries.Begin();
  27.   AISign.BuildSign(AIIndustry.GetLocation(myRef), "destination");
  28.  
  29.   local myDock = this.BuildDock(myRef);
  30.   local buoys = this.BuildBuoys(myDock, myRig);
  31.  
  32.   while (true) {
  33.     this.Sleep(50);
  34.   }
  35. }
  36.  
  37. function MyTest::BuildDock(industry) {
  38.   local dockLocation = false;
  39.  
  40.   // Depending on the Realistic Catchment Area setting, the catchment area radius
  41.   // for a dock can be either 4 or 5.
  42.   local accepting = AITileList_IndustryAccepting(industry, 4);
  43.   accepting.Valuate(AITile.IsCoastTile);
  44.   accepting.KeepValue(1);
  45.  
  46.   foreach(myTile, value in accepting) {
  47.     local slope = AITile.GetSlope(myTile);
  48.     // Keep trying to build a dock until it succeeds
  49.     if (AIMarine.BuildDock(myTile, AIStation.STATION_NEW)) {
  50.       dockLocation = myTile;
  51.       break;
  52.     }
  53.   }
  54.   return dockLocation;
  55. }
  56.  
  57. function MyTest::BuildBuoys(dock, rig) {
  58.   local rigStation = AIIndustry.GetDockLocation(rig);
  59.   local distance = AIMap.DistanceMax(dock, rigStation);
  60.   AILog.Info("Two stations are " + distance + " tiles apart.");
  61.  
  62.   local xDistance = AIMap.GetTileX(rigStation) - AIMap.GetTileX(dock);
  63.   local yDistance = AIMap.GetTileY(rigStation) - AIMap.GetTileY(dock);
  64.   AILog.Info("X distance: " + xDistance);
  65.   AILog.Info("Y distance: " + yDistance);
  66.  
  67.   // This part is built on assumption that it's a good idea to place a buoy
  68.   // about every 20 tiles, par https://wiki.openttd.org/Building_buoys#Finding_a_suitable_location
  69.   local numSegments = distance / 20;
  70.   local xSegSize = xDistance / numSegments;
  71.   local ySegSize = yDistance / numSegments;
  72.   AILog.Info("X segment size: " + xSegSize);
  73.   AILog.Info("Y segment size: " + ySegSize);
  74.  
  75.   local buoys = AIList();
  76.   local xLoc = AIMap.GetTileX(dock);
  77.   local yLoc = AIMap.GetTileY(dock);
  78.   for (local i = 0; i < numSegments; i++) {
  79.     xLoc = xLoc + xSegSize;
  80.     yLoc = yLoc + ySegSize;
  81.     local buoy = this.BuildBuoy(xLoc, yLoc);
  82.     if (buoy) {
  83.       buoys.AddItem(buoy, 0);
  84.     }
  85.   }
  86.  
  87.   return buoys;
  88. }
  89.  
  90. function MyTest::BuildBuoy(x, y) {
  91.   local tile = AIMap.GetTileIndex(x, y);
  92.   if (AIMarine.BuildBuoy(tile)) {
  93.     return AIWaypoint.GetWaypointID(tile);
  94.   }
  95.   else {
  96.     return false;
  97.   }
  98. }

Version history

Revision # Author Created at
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