| | 44 | }
|
---|
| | 45 |
|
---|
| | 46 | function 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 |
|
---|
| | 66 | function 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 |
|
---|
| | 96 | function 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 |
|
---|
| | 106 | function 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 |
|
---|
| | 132 | function 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 |
|
---|
| | 168 | function 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 |
|
---|
| | 181 | function 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);
|
---|