| Old revision #p2e9mosxp | New revision #pj8xtnxkg | ||
|---|---|---|---|
| 1 | class MyTest extends AIController { | 1 | class MyTest extends AIController { |
| 2 | oil = false; | ||
| 3 | oilFleet = []; | ||
| 4 | myRigs = AIList(); | ||
| 2 | function Start(); | 5 | function Start(); |
| 3 | } | 6 | } |
| 4 | 7 | ||
| 5 | function MyTest::Start() { | 8 | function MyTest::Start() { |
| 6 | local cargoes = AICargoList(); | 9 | local cargoes = AICargoList(); |
| 7 | |||
| 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)); | … | … |
| 27 | AISign.BuildSign(AIIndustry.GetLocation(myRef), "destination"); | 30 | AISign.BuildSign(AIIndustry.GetLocation(myRef), "destination"); |
| 28 | 31 | ||
| 29 | local myDock = this.BuildDock(myRef); | 32 | local myDock = this.BuildDock(myRef); |
| 33 | |||
| 34 | local stations = AIStationList(AIStation.STATION_DOCK); | ||
| 35 | |||
| 30 | local buoys = this.BuildBuoys(myDock, myRig); | 36 | local buoys = this.BuildBuoys(myDock, myRig); |
| 37 | local depot = this.BuildDepot(AIIndustry.GetLocation(myRig)); | ||
| 38 | local ship = this.BuildShip(depot, myDock, myRig, buoys); | ||
| 31 | 39 | ||
| 32 | while (true) { | 40 | while (true) { |
| 33 | this.Sleep(50); | 41 | this.ManageFleet(); |
| 42 | this.Sleep(72); | ||
| 34 | } | 43 | } |
| 35 | } | 44 | } |
| 36 | 45 | … | … |
| 57 | function MyTest::BuildBuoys(dock, rig) { | 66 | function MyTest::BuildBuoys(dock, rig) { |
| 58 | local rigStation = AIIndustry.GetDockLocation(rig); | 67 | local rigStation = AIIndustry.GetDockLocation(rig); |
| 59 | local distance = AIMap.DistanceMax(dock, rigStation); | 68 | local distance = AIMap.DistanceMax(dock, rigStation); |
| 60 | |||
| 61 | 69 | ||
| 62 | local xDistance = AIMap.GetTileX(rigStation) - AIMap.GetTileX(dock); | 70 | local xDistance = AIMap.GetTileX(rigStation) - AIMap.GetTileX(dock); |
| 63 | local yDistance = AIMap.GetTileY(rigStation) - AIMap.GetTileY(dock); | 71 | local yDistance = AIMap.GetTileY(rigStation) - AIMap.GetTileY(dock); |
| 64 | |||
| 65 | |||
| 66 | 72 | ||
| 67 | // This part is built on assumption that it's a good idea to place a buoy | 73 | // 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 | 74 | // about every 20 tiles, par https://wiki.openttd.org/Building_buoys#Finding_a_suitable_location |
| 69 | local numSegments = distance / 20; | 75 | local numSegments = distance / 20; |
| 70 | local | 70 | local buoys = AIList(); |
| 71 | | 71 | if (numSegments > 0) { |
| 72 | | 72 | local xSegSize = xDistance / numSegments; |
| 73 | | 73 | local ySegSize = yDistance / numSegments; |
| 74 | 80 | ||
| 75 | | 75 | local xLoc = AIMap.GetTileX(dock); |
| 76 | | 76 | local yLoc = AIMap.GetTileY(dock); |
| 77 | | 77 | for (local i = 0; i < numSegments; i++) { |
| 78 | | 78 | xLoc = xLoc + xSegSize; |
| 79 | | 79 | yLoc = yLoc + ySegSize; |
| 80 | | 80 | local buoy = this.BuildBuoy(xLoc, yLoc); |
| 81 | | 81 | if (buoy) { |
| 82 | | 82 | buoys.AddItem(buoy, 0); |
| 83 | | 83 | } |
| 84 | } | 90 | } |
| 85 | } | 91 | } |
| 86 | 92 | … | … |
| 95 | else { | 101 | else { |
| 96 | return false; | 102 | return false; |
| 97 | } | 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); | ||
| 98 | } | 194 | } |