Loading

Revision differences

Old revision #pfif4lzeoNew revision #pj8xtnxkg
1class MyTest extends AIController {  1class MyTest extends AIController {  
  2  oil = false;  
  3  oilFleet = [];  
  4  myRigs = AIList();  
2  function Start();  5  function Start();  
3}  6}  
4  7  
5function MyTest::Start() {  8function MyTest::Start() {  
6  local cargoes = AICargoList();  9  local cargoes = AICargoList();  
7  local oil;    
8  foreach (cargo, value in cargoes) {  10  foreach (cargo, value in cargoes) {  
9    if( AICargo.GetCargoLabel(cargo) == "OIL_") {  11    if( AICargo.GetCargoLabel(cargo) == "OIL_") {  
10      oil = cargo;  12      oil = cargo;  
  
19  oilrigs.Valuate(AIIndustry.GetLastMonthProduction, oil);  21  oilrigs.Valuate(AIIndustry.GetLastMonthProduction, oil);  
20  local myRig = oilrigs.Begin();  22  local myRig = oilrigs.Begin();  
21  AISign.BuildSign(AIIndustry.GetLocation(myRig), "origin");  23  AISign.BuildSign(AIIndustry.GetLocation(myRig), "origin");  
  24  myRigs.AddItem(myRig, 0);  
22  25  
23  local refineries = AIIndustryList_CargoAccepting(oil);  26  local refineries = AIIndustryList_CargoAccepting(oil);  
24  refineries.Valuate(AIIndustry.GetDistanceManhattanToTile, AIIndustry.GetLocation(myRig));  27  refineries.Valuate(AIIndustry.GetDistanceManhattanToTile, AIIndustry.GetLocation(myRig));  
  
26  local myRef = refineries.Begin();  29  local myRef = refineries.Begin();  
27  AISign.BuildSign(AIIndustry.GetLocation(myRef), "destination");  30  AISign.BuildSign(AIIndustry.GetLocation(myRef), "destination");  
28  31  
29  AILog.Info(AITile.GetSlope(0x418E));  // 0x418E comes from Land Area Information tool within the game  32  local myDock = this.BuildDock(myRef);
30  AILog.Info(AITile.IsCoastTile(0x418E));  33
31  AIMarine.BuildDock(0x418E, AIStation.STATION_NEW);  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);
32  39  
33  while (true) {  40  while (true) {  
34    this.Sleep(50);  41    this.ManageFleet();
   42    this.Sleep(72);
35  }  43  }  
  44}  
  45  
  46function 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  
  66function 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  
  96function 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  
  106function 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  
  132function 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  
  168function 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  
  181function 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);  
36} 194}