Loading

Revision differences

Old revision #pfif4lzeoNew revision #p2e9mosxp
26  local myRef = refineries.Begin();  26  local myRef = refineries.Begin();  
27  AISign.BuildSign(AIIndustry.GetLocation(myRef), "destination");  27  AISign.BuildSign(AIIndustry.GetLocation(myRef), "destination");  
28  28  
29  AILog.Info(AITile.GetSlope(0x418E));  // 0x418E comes from Land Area Information tool within the game  29  local myDock = this.BuildDock(myRef);
30  AILog.Info(AITile.IsCoastTile(0x418E));  30  local buoys = this.BuildBuoys(myDock, myRig);
31  AIMarine.BuildDock(0x418E, AIStation.STATION_NEW);    
32  31  
33  while (true) {  32  while (true) {  
34    this.Sleep(50);  33    this.Sleep(50);  
35  }  34  }  
  35}  
  36  
  37function 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  
  57function 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  
  90function 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  }  
36} 98}