| 29 | local accepting = AITileList_IndustryAccepting(myRef, 4);
| 29 | local myDock = this.BuildDock(myRef);
|
|---|
| 30 | AILog.Info("I found " + accepting.Count() + " tiles for the initial candidate");
| 30 | local buoys = this.BuildBuoys(myDock, myRig);
|
|---|
| 31 | accepting.Valuate(AITile.IsCoastTile);
| | |
|---|
| 32 | accepting.KeepValue(1);
| | |
|---|
| 33 | AILog.Info("I found " + accepting.Count() + " coast tiles.");
| | |
|---|
| 34 |
| | |
|---|
| 35 | foreach(myTile, value in accepting) {
| | |
|---|
| 36 | local slope = AITile.GetSlope(myTile);
| | |
|---|
| 37 | AILog.Info("Slope at tile " + myTile + " is " + slope + ".");
| | |
|---|
| 38 | if (AIMarine.BuildDock(myTile, AIStation.STATION_NEW)) {
| | |
|---|
| 39 | break;
| | |
|---|
| 40 | }
| | |
|---|
| 41 | }
| | |
|---|
| | | 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 | }
|
|---|