class MyTest extends AIController {
function Start();
}
function MyTest::Start() {
local cargoes = AICargoList();
local oil;
foreach (cargo, value in cargoes) {
if( AICargo.GetCargoLabel(cargo) == "OIL_") {
oil = cargo;
break;
}
}
local oilrigs = AIIndustryList_CargoProducing(oil);
oilrigs.Valuate(AIIndustry.HasDock);
oilrigs.KeepValue(1);
oilrigs.Valuate(AIIndustry.GetLastMonthProduction, oil);
local myRig = oilrigs.Begin();
AISign.BuildSign(AIIndustry.GetLocation(myRig), "origin");
local refineries = AIIndustryList_CargoAccepting(oil);
refineries.Valuate(AIIndustry.GetDistanceManhattanToTile, AIIndustry.GetLocation(myRig));
refineries.Sort(AIList.SORT_BY_VALUE, true);
local myRef = refineries.Begin();
AISign.BuildSign(AIIndustry.GetLocation(myRef), "destination");
local myDock = this.BuildDock(myRef);
local buoys = this.BuildBuoys(myDock, myRig);
while (true) {
this.Sleep(50);
}
}
function MyTest::BuildDock(industry) {
local dockLocation = false;
// Depending on the Realistic Catchment Area setting, the catchment area radius
// for a dock can be either 4 or 5.
local accepting = AITileList_IndustryAccepting(industry, 4);
accepting.Valuate(AITile.IsCoastTile);
accepting.KeepValue(1);
foreach(myTile, value in accepting) {
local slope = AITile.GetSlope(myTile);
// Keep trying to build a dock until it succeeds
if (AIMarine.BuildDock(myTile, AIStation.STATION_NEW)) {
dockLocation = myTile;
break;
}
}
return dockLocation;
}
function MyTest::BuildBuoys(dock, rig) {
local rigStation = AIIndustry.GetDockLocation(rig);
local distance = AIMap.DistanceMax(dock, rigStation);
AILog.Info("Two stations are " + distance + " tiles apart.");
local xDistance = AIMap.GetTileX(rigStation) - AIMap.GetTileX(dock);
local yDistance = AIMap.GetTileY(rigStation) - AIMap.GetTileY(dock);
AILog.Info("X distance: " + xDistance);
AILog.Info("Y distance: " + yDistance);
// This part is built on assumption that it's a good idea to place a buoy
// about every 20 tiles, par https://wiki.openttd.org/Building_buoys#Finding_a_suitable_location
local numSegments = distance / 20;
local xSegSize = xDistance / numSegments;
local ySegSize = yDistance / numSegments;
AILog.Info("X segment size: " + xSegSize);
AILog.Info("Y segment size: " + ySegSize);
local buoys = AIList();
local xLoc = AIMap.GetTileX(dock);
local yLoc = AIMap.GetTileY(dock);
for (local i = 0; i < numSegments; i++) {
xLoc = xLoc + xSegSize;
yLoc = yLoc + ySegSize;
local buoy = this.BuildBuoy(xLoc, yLoc);
if (buoy) {
buoys.AddItem(buoy, 0);
}
}
return buoys;
}
function MyTest::BuildBuoy(x, y) {
local tile = AIMap.GetTileIndex(x, y);
if (AIMarine.BuildBuoy(tile)) {
return AIWaypoint.GetWaypointID(tile);
}
else {
return false;
}
}