-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWaterMain.nut
More file actions
258 lines (235 loc) · 11.3 KB
/
Copy pathWaterMain.nut
File metadata and controls
258 lines (235 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
function LuDiAIAfterFix::BuildWaterRoute()
{
if (!AIController.GetSetting("water_support")) return true; // assume true to keep rotating this.transport_mode_rotation
local unfinished = this.water_build_manager.HasUnfinishedRoute();
if (unfinished || (this.water_route_manager.GetShipCount() < max(AIGameSettings.GetValue("max_ships") - 10, 10)) && Utils.ListHasValue(this.water_route_manager.m_routes_built.all, false)) {
local city_from = null;
local city_to = null;
local cheaper_route = false;
local cargo_class = this.water_route_manager.m_cargo_class;
if (!unfinished) {
if (!this.water_route_manager.IsDateTimerRunning()) this.water_route_manager.StartDateTimer();
// if (this.water_route_manager.m_reserved_money == 0) this.water_route_manager.SwapCargoClass();
local cargo_type = Utils.GetCargoType(cargo_class);
local engine_list = AIEngineList(AIVehicle.VT_WATER);
foreach (engine_id, _ in engine_list) {
if (!AIEngine.IsValidEngine(engine_id)) {
engine_list[engine_id] = null;
continue;
}
if (!AIEngine.IsBuildable(engine_id)) {
engine_list[engine_id] = null;
continue;
}
if (!AIEngine.CanRefitCargo(engine_id, cargo_type)) {
engine_list[engine_id] = null;
continue;
}
engine_list[engine_id] = AIEngine.GetPrice(engine_id);
}
if (engine_list.IsEmpty()) {
if (this.water_route_manager.m_reserved_money != 0) {
this.water_route_manager.ResetMoneyReservation();
}
this.water_route_manager.SwapCargoClass();
return true;
}
local best_engine_info = Utils.GetBestEngineIncome(engine_list, cargo_type, WaterRoute.COUNT_INTERVAL, false);
local max_distance = (WATER_DAYS_IN_TRANSIT * 2 * 74 * AIEngine.GetMaxSpeed(best_engine_info[0])) / (256 * 16);
local min_distance = max(20, max_distance * 2 / 3);
// AILog.Info("best_engine_info: best_engine = " + AIEngine.GetName(best_engine_info[0]) + "; best_distance = " + best_engine_info[1] + "; max_distance = " + max_distance + "; min_distance = " + min_distance);
local map_size = AIMap.GetMapSizeX() + AIMap.GetMapSizeY();
local min_dist = min_distance > map_size / 3 ? map_size / 3 : min_distance;
local max_dist = min_dist + MAX_DISTANCE_INCREASE > max_distance ? min_dist + MAX_DISTANCE_INCREASE : max_distance;
// AILog.Info("map_size = " + map_size + " ; min_dist = " + min_dist + " ; max_dist = " + max_dist);
local estimated_costs = 0;
local engine_costs = AIEngine.GetPrice(engine_list.Begin());
local canal_costs = AIMarine.GetBuildCost(AIMarine.BT_CANAL) * 2 * max_dist;
local clear_costs = AITile.GetBuildCost(AITile.BT_CLEAR_ROUGH) * max_dist;
local dock_costs = AIMarine.GetBuildCost(AIMarine.BT_DOCK) * 2;
local depot_cost = AIMarine.GetBuildCost(AIMarine.BT_DEPOT);
estimated_costs += engine_costs + canal_costs + clear_costs + dock_costs + depot_cost;
// AILog.Info("estimated_costs = " + estimated_costs + "; engine_costs = " + engine_costs + ", canal_costs = " + canal_costs + ", clear_costs = " + clear_costs + ", dock_costs = " + dock_costs + ", depot_cost = " + depot_cost);
if (this.water_route_manager.m_reserved_money != 0) {
this.water_route_manager.UpdateMoneyReservation(estimated_costs);
} else {
this.water_route_manager.SetMoneyReservation(estimated_costs);
}
if (!Utils.HasMoney(estimated_costs + ::caches.m_reserved_money - this.water_route_manager.GetNonPausedReservedMoney())) {
/* Try a cheaper route */
if (this.water_route_manager.m_routes_built.best[cargo_class]) {
if (this.water_route_manager.DaysElapsed() < AIController.GetSetting("exclusive_attempt_days")) {
return 0;
}
if (!this.water_route_manager.IsMoneyReservationPaused()) {
this.water_route_manager.PauseMoneyReservation();
}
return 2; // allow skipping to next transport mode
} else {
estimated_costs -= canal_costs + clear_costs;
this.water_route_manager.UpdateMoneyReservation(estimated_costs);
cheaper_route = true;
if (!Utils.HasMoney(estimated_costs + ::caches.m_reserved_money - this.water_route_manager.GetNonPausedReservedMoney())) {
if (this.water_route_manager.DaysElapsed() < AIController.GetSetting("exclusive_attempt_days")) {
return 0;
}
if (!this.water_route_manager.IsMoneyReservationPaused()) {
this.water_route_manager.PauseMoneyReservation();
}
return 2; // allow skipping to next transport mode
}
}
}
if (city_from == null) {
city_from = this.water_town_manager.GetUnusedCity(this.water_route_manager.m_routes_built.best[cargo_class], cargo_class);
if (city_from == null) {
if (AIController.GetSetting("randomize_towns")) {
this.water_town_manager.m_used_cities_list[cargo_class].Clear();
} else if (!this.water_route_manager.m_routes_built.best[cargo_class]) {
this.water_route_manager.m_routes_built.best[cargo_class] = true;
this.water_town_manager.m_used_cities_list[cargo_class].Clear();
// this.water_town_manager.m_near_city_pair_array[cargo_class].clear();
AILog.Warning("Best " + AICargo.GetCargoLabel(cargo_type) + " water routes have been used! Year: " + AIDate.GetYear(AIDate.GetCurrentDate()));
} else {
// this.water_town_manager.m_near_city_pair_array[cargo_class].clear();
if (!this.water_route_manager.m_routes_built.all[cargo_class]) {
AILog.Warning("All " + AICargo.GetCargoLabel(cargo_type) + " water routes have been used!");
}
this.water_route_manager.m_routes_built.all[cargo_class] = true;
}
}
}
if (city_from != null) {
// AILog.Info("s:New city found: " + AITown.GetName(city_from));
this.water_town_manager.FindNearCities(city_from, min_dist, max_dist, this.water_route_manager.m_routes_built.best[cargo_class], cargo_class);
if (!this.water_town_manager.m_near_city_pair_array[cargo_class].len()) {
AILog.Info("No near city available");
city_from = null;
}
}
if (city_from != null) {
foreach (near_city_pair in this.water_town_manager.m_near_city_pair_array[cargo_class]) {
if (city_from == near_city_pair[0]) {
if (!this.water_route_manager.TownRouteExists(city_from, near_city_pair[1], cargo_class)) {
city_to = near_city_pair[1];
if (!AIController.GetSetting("randomize_towns") && !this.water_route_manager.m_routes_built.all[cargo_class] && this.water_route_manager.HasMaxStationCount(city_from, city_to, cargo_class)) {
// AILog.Info("this.water_route_manager.HasMaxStationCount(" + AITown.GetName(city_from) + ", " + AITown.GetName(city_to) + ", " + cargo_class + ") == " + this.water_route_manager.HasMaxStationCount(city_from, city_to, cargo_class));
city_to = null;
continue;
} else {
// AILog.Info("this.water_route_manager.HasMaxStationCount(" + AITown.GetName(city_from) + ", " + AITown.GetName(city_to) + ", " + cargo_class + ") == " + this.water_route_manager.HasMaxStationCount(city_from, city_to, cargo_class));
break;
}
}
}
}
if (city_to == null) {
city_from = null;
}
}
if (city_from == null && city_to == null) {
this.water_route_manager.ResetMoneyReservation();
this.water_route_manager.SwapCargoClass();
}
} else {
if (!Utils.HasMoney(this.water_route_manager.GetReservedMoney())) {
if (this.water_route_manager.DaysElapsed() < AIController.GetSetting("exclusive_attempt_days")) {
return 0;
}
if (!this.water_route_manager.IsMoneyReservationPaused()) {
this.water_route_manager.PauseMoneyReservation();
}
return 2; // allow skipping to next transport mode
}
}
if (unfinished || (city_from != null && city_to != null)) {
if (!unfinished) {
AILog.Info("s:New city found: " + AITown.GetName(city_from));
AILog.Info("s:New near city found: " + AITown.GetName(city_to));
}
city_from = unfinished ? this.water_build_manager.m_city_from : city_from;
city_to = unfinished ? this.water_build_manager.m_city_to : city_to;
cargo_class = unfinished ? this.water_build_manager.m_cargo_class : cargo_class;
cheaper_route = unfinished ? this.water_build_manager.m_cheaper_route : cheaper_route;
return this.water_route_manager.BuildRoute(this.water_build_manager, city_from, city_to, cargo_class, cheaper_route);
}
}
return true;
}
function LuDiAIAfterFix::CheckForUnfinishedWaterRoute()
{
if (this.water_build_manager.HasUnfinishedRoute()) {
/* Look for potentially unregistered dock or ship depot tiles during save */
local dock_from = this.water_build_manager.m_dock_from;
local dock_to = this.water_build_manager.m_dock_to;
local depot_tile = this.water_build_manager.m_depot_tile;
local station_type = AIStation.STATION_DOCK;
if (dock_from == -1 || dock_to == -1) {
local station_list = AIStationList(station_type);
local all_station_tiles = AITileList();
foreach (station_id, _ in station_list) {
local station_tiles = AITileList_StationType(station_id, station_type);
all_station_tiles.AddList(station_tiles);
}
// AILog.Info("all_station_tiles has " + all_station_tiles.Count() + " tiles");
local all_tiles_found = AITileList();
if (dock_from != -1) all_tiles_found[dock_from] = 0;
foreach (tile, _ in all_station_tiles) {
if (::scheduled_removals[AITile.TRANSPORT_WATER].rawin(tile)) {
// AILog.Info("scheduled_removals[AITile.TRANSPORT_WATER] has tile " + tile);
all_tiles_found[tile] = 0;
break;
}
foreach (route in this.water_route_manager.m_town_route_array) {
if (route.m_dock_from == tile || route.m_dock_to == tile) {
// AILog.Info("Route " + i + " has tile " + tile);
local station_tiles = AITileList_StationType(AIStation.GetStationID(tile), station_type);
all_tiles_found.AddList(station_tiles);
break;
}
}
}
if (all_tiles_found.Count() != all_station_tiles.Count()) {
// AILog.Info(all_tiles_found.Count() + " != " + all_station_tiles.Count());
local all_tiles_missing = AITileList();
all_tiles_missing.AddList(all_station_tiles);
all_tiles_missing.RemoveList(all_tiles_found);
foreach (tile, _ in all_tiles_missing) {
if (AIMarine.IsDockTile(tile) && AITile.GetSlope(tile) != AITile.SLOPE_FLAT) {
// AILog.Info("Tile " + tile + " is missing");
::scheduled_removals[AITile.TRANSPORT_WATER].rawset(tile, 0);
}
}
}
}
if (depot_tile == -1) {
local all_depots_tiles = AIDepotList(AITile.TRANSPORT_WATER);
// AILog.Info("all_depots_tiles has " + all_depots_tiles.Count() + " tiles");
local all_tiles_found = AITileList();
foreach (tile, _ in all_depots_tiles) {
if (::scheduled_removals[AITile.TRANSPORT_WATER].rawin(tile)) {
// AILog.Info("scheduled_removals[AITile.TRANSPORT_WATER] has tile " + tile);
all_tiles_found[tile] = 0;
break;
}
foreach (route in this.water_route_manager.m_town_route_array) {
if (route.m_depot_tile == tile) {
// AILog.Info("Route " + i + " has tile " + tile);
all_tiles_found[tile] = 0;
break;
}
}
}
if (all_tiles_found.Count() != all_depots_tiles.Count()) {
// AILog.Info(all_tiles_found.Count() + " != " + all_depots_tiles.Count());
local all_tiles_missing = AITileList();
all_tiles_missing.AddList(all_depots_tiles);
all_tiles_missing.RemoveList(all_tiles_found);
foreach (tile, _ in all_tiles_missing) {
// AILog.Info("Tile " + tile + " is missing");
::scheduled_removals[AITile.TRANSPORT_WATER].rawset(tile, 0);
}
}
}
}
}