-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAirMain.nut
281 lines (250 loc) · 13.3 KB
/
AirMain.nut
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
function LuDiAIAfterFix::BuildAirRoute(cityFrom, unfinished) {
if (unfinished || (airRouteManager.getAircraftCount() < MAX_AIR_VEHICLES - 10) && ((allRoutesBuilt >> 4) & 3) != 3) {
local cityTo = null;
local cC = cargoClassAir;
if (!unfinished) {
local cargo = Utils.getCargoId(cC);
local tempList = AIEngineList(AIVehicle.VT_AIR);
local engineList = AIList();
for (local engine = tempList.Begin(); !tempList.IsEnd(); engine = tempList.Next()) {
if (AIEngine.IsValidEngine(engine) && AIEngine.IsBuildable(engine) && AIEngine.CanRefitCargo(engine, cargo)) {
engineList.AddItem(engine, AIEngine.GetPrice(engine));
}
}
if (engineList.Count() == 0) {
return;
}
engineList.Sort(AIList.SORT_BY_VALUE, AIList.SORT_DESCENDING); // sort price
// local bestengineinfo = WrightAI.GetBestEngineIncome(engineList, cargo, AirBuildManager.DAYS_INTERVAL);
// if (bestengineinfo[0] == null) {
// return;
// }
// local fakedist = bestengineinfo[1];
// local infrastructure = AIGameSettings.GetValue("infrastructure_maintenance");
// local max_order_dist = WrightAI.GetMaximumOrderDistance(bestengineinfo[0]);
// local max_dist = max_order_dist > AIMap.GetMapSize() ? AIMap.GetMapSize() : max_order_dist;
// local min_order_dist = (fakedist / 2) * (fakedist / 2);
// local min_dist = min_order_dist > max_dist * 3 / 4 ? !infrastructure && max_dist * 3 / 4 > AIMap.GetMapSize() / 8 ? AIMap.GetMapSize() / 8 : max_dist * 3 / 4 : min_order_dist;
if (cityFrom == null) {
cityFrom = airTownManager.getUnusedCity(((((bestRoutesBuilt >> 4) & 3) & (1 << (cC == AICargo.CC_PASSENGERS ? 0 : 1))) != 0), cC);
if (cityFrom == null) {
if (AIController.GetSetting("pick_mode") == 1) {
if (cC == AICargo.CC_PASSENGERS) {
airTownManager.m_usedCitiesPassTable.clear();
} else {
airTownManager.m_usedCitiesMailTable.clear();
}
} else {
if ((((bestRoutesBuilt >> 4) & 3) & (1 << (cC == AICargo.CC_PASSENGERS ? 0 : 1))) == 0) {
bestRoutesBuilt = bestRoutesBuilt | (1 << (4 + (cC == AICargo.CC_PASSENGERS ? 0 : 1)));
if (cC == AICargo.CC_PASSENGERS) {
airTownManager.m_usedCitiesPassTable.clear();
} else {
airTownManager.m_usedCitiesMailTable.clear();
}
// airTownManager.ClearCargoClassArray(cC);
AILog.Warning("Best " + AICargo.GetCargoLabel(cargo) + " air routes have been used! Year: " + AIDate.GetYear(AIDate.GetCurrentDate()));
} else {
// airTownManager.ClearCargoClassArray(cC);
if ((((allRoutesBuilt >> 4) & 3) & (1 << (cC == AICargo.CC_PASSENGERS ? 0 : 1))) == 0) {
AILog.Warning("All " + AICargo.GetCargoLabel(cargo) + " air routes have been used!");
}
allRoutesBuilt = allRoutesBuilt | (1 << (4 + (cC == AICargo.CC_PASSENGERS ? 0 : 1)));
}
}
}
}
// if (cityFrom != null) {
// AILog.Info("New city found: " + AITown.GetName(cityFrom));
// airTownManager.findNearCities(cityFrom, min_dist, max_dist, ((((bestRoutesBuilt >> 4) & 3) & (1 << (cC == AICargo.CC_PASSENGERS ? 0 : 1))) != 0), cC, fakedist);
// if (!airTownManager.HasArrayCargoClassPairs(cC)) {
// AILog.Info("No near city available");
// cityFrom = null;
// }
// }
// if (cityFrom != null) {
// for (local i = 0; i < airTownManager.m_nearCityPairArray.len(); ++i) {
// if (cityFrom == airTownManager.m_cityFrom(airTownManager.m_nearCityPairArray[i]) && cC == airTownManager.m_cargoClass(airTownManager.m_nearCityPairArray[i])) {
// if (!airRouteManager.townRouteExists(cityFrom, airTownManager.m_cityTo(airTownManager.m_nearCityPairArray[i]), cC)) {
// cityTo = airTownManager.m_cityTo(airTownManager.m_nearCityPairArray[i]);
// if (AIController.GetSetting("pick_mode") != 1 && ((((allRoutesBuilt >> 4) & 3) & (1 << (cC == AICargo.CC_PASSENGERS ? 0 : 1))) == 0) && airRouteManager.hasMaxStationCount(cityFrom, cityTo, cC)) {
// AILog.Info("airRouteManager.hasMaxStationCount(" + AITown.GetName(cityFrom) + ", " + AITown.GetName(cityTo) + ", " + cC + ") == " + airRouteManager.hasMaxStationCount(cityFrom, cityTo, cC));
// cityTo = null;
// continue;
// } else {
// AILog.Info("airRouteManager.hasMaxStationCount(" + AITown.GetName(cityFrom) + ", " + AITown.GetName(cityTo) + ", " + cC + ") == " + airRouteManager.hasMaxStationCount(cityFrom, cityTo, cC));
// break;
// }
// }
// }
// }
// if (cityTo == null) {
// cityFrom = null;
// }
// }
// if (cityFrom == null && cityTo == null) {
// reservedMoney -= reservedMoneyAir;
// reservedMoneyAir = 0;
// }
} else {
if (!Utils.HasMoney(reservedMoneyAir)) {
return;
}
}
if (unfinished || cityFrom != null/* && cityTo != null*/) {
if (!unfinished) {
// AILog.Info("New city found: " + AITown.GetName(cityFrom));
// AILog.Info("New near city found: " + AITown.GetName(cityTo));
}
if (!unfinished) buildTimerAir = 0;
local from = unfinished ? airBuildManager.m_cityFrom : cityFrom;
local to = unfinished ? airBuildManager.m_cityTo : cityTo;
local cargoC = unfinished ? airBuildManager.m_cargoClass : cC;
local best_routes = unfinished ? airBuildManager.m_best_routes_built : ((((bestRoutesBuilt >> 4) & 3) & (1 << (cC == AICargo.CC_PASSENGERS ? 0 : 1))) != 0);
local all_routes = (((allRoutesBuilt >> 4) & 3) & (1 << (cargoC == AICargo.CC_PASSENGERS ? 0 : 1))) == 0;
local start_date = AIDate.GetCurrentDate();
local routeResult = airRouteManager.buildRoute(airRouteManager, airBuildManager, airTownManager, from, to, cargoC, best_routes, all_routes);
buildTimerAir += AIDate.GetCurrentDate() - start_date;
if (routeResult[0] != null) {
if (routeResult[0] != 0) {
reservedMoney -= reservedMoneyAir;
reservedMoneyAir = 0;
AILog.Warning("Built " + AICargo.GetCargoLabel(Utils.getCargoId(cargoC)) + " air route between " + AIBaseStation.GetName(AIStation.GetStationID(routeResult[1])) + " and " + AIBaseStation.GetName(AIStation.GetStationID(routeResult[2])) + " in " + buildTimerAir + " day" + (buildTimerAir != 1 ? "s" : "") + ".");
}
} else {
reservedMoney -= reservedMoneyAir;
reservedMoneyAir = 0;
if (to != null) airTownManager.removeUsedCityPair(from, to, cC, false);
AILog.Error("a:" + buildTimerAir + " day" + (buildTimerAir != 1 ? "s" : "") + " wasted!");
}
// cityFrom = cityTo; // use this line to look for a new town from the last town
cityFrom = null;
}
}
}
function LuDiAIAfterFix::ResetAirManagementVariables() {
if (lastAirManagedArray < 0) lastAirManagedArray = airRouteManager.m_townRouteArray.len() - 1;
if (lastAirManagedManagement < 0) lastAirManagedManagement = 6;
}
function LuDiAIAfterFix::InterruptAirManagement(cur_date) {
if (AIDate.GetCurrentDate() - cur_date > 1) {
if (lastAirManagedArray == -1) lastAirManagedManagement--;
return true;
}
return false;
}
function LuDiAIAfterFix::updateAirVehicles() {
local max_aircraft = AIGameSettings.GetValue("max_aircraft");
if (max_aircraft != MAX_AIR_VEHICLES) {
MAX_AIR_VEHICLES = max_aircraft;
AILog.Info("MAX_AIR_VEHICLES = " + MAX_AIR_VEHICLES);
}
local cur_date = AIDate.GetCurrentDate();
ResetAirManagementVariables();
// for (local i = lastAirManagedArray; i >= 0; --i) {
// if (lastAirManagedManagement != 7) break;
// lastAirManagedArray--;
// AILog.Info("Route " + i + " from " + AIBaseStation.GetName(AIStation.GetStationID(airRouteManager.m_townRouteArray[i].m_airportFrom)) + " to " + AIBaseStation.GetName(AIStation.GetStationID(airRouteManager.m_townRouteArray[i].m_airportTo)));
// if (InterruptAirManagement(cur_date)) return;
// }
// ResetAirManagementVariables();
// if (lastAirManagedManagement == 7) lastAirManagedManagement--;
//
// local start_tick = AIController.GetTick();
for (local i = lastAirManagedArray; i >= 0; --i) {
if (lastAirManagedManagement != 6) break;
lastAirManagedArray--;
// AILog.Info("managing route " + i + ". renewVehicles");
airRouteManager.m_townRouteArray[i].renewVehicles();
if (InterruptAirManagement(cur_date)) return;
}
ResetAirManagementVariables();
if (lastAirManagedManagement == 6) lastAirManagedManagement--;
// local management_ticks = AIController.GetTick() - start_tick;
// AILog.Info("Managed " + airRouteManager.m_townRouteArray.len() + " air route" + (airRouteManager.m_townRouteArray.len() != 1 ? "s" : "") + " in " + management_ticks + " tick" + (management_ticks != 1 ? "s" : "") + ".");
// local start_tick = AIController.GetTick();
for (local i = lastAirManagedArray; i >= 0; --i) {
if (lastAirManagedManagement != 5) break;
lastAirManagedArray--;
// AILog.Info("managing route " + i + ". sendNegativeProfitVehiclesToDepot");
airRouteManager.m_townRouteArray[i].sendNegativeProfitVehiclesToDepot();
if (InterruptAirManagement(cur_date)) return;
}
ResetAirManagementVariables();
if (lastAirManagedManagement == 5) lastAirManagedManagement--;
// local management_ticks = AIController.GetTick() - start_tick;
// AILog.Info("Managed " + airRouteManager.m_townRouteArray.len() + " air route" + (airRouteManager.m_townRouteArray.len() != 1 ? "s" : "") + " in " + management_ticks + " tick" + (management_ticks != 1 ? "s" : "") + ".");
// local start_tick = AIController.GetTick();
local num_vehs = airRouteManager.getAircraftCount();
local maxAllRoutesProfit = airRouteManager.highestProfitLastYear();
for (local i = lastAirManagedArray; i >= 0; --i) {
if (lastAirManagedManagement != 4) break;
lastAirManagedArray--;
// AILog.Info("managing route " + i + ". sendLowProfitVehiclesToDepot");
if (MAX_AIR_VEHICLES * 0.95 < num_vehs) {
airRouteManager.m_townRouteArray[i].sendLowProfitVehiclesToDepot(maxAllRoutesProfit);
}
if (InterruptAirManagement(cur_date)) return;
}
ResetAirManagementVariables();
if (lastAirManagedManagement == 4) lastAirManagedManagement--;
// local management_ticks = AIController.GetTick() - start_tick;
// AILog.Info("Managed " + airRouteManager.m_townRouteArray.len() + " air route" + (airRouteManager.m_townRouteArray.len() != 1 ? "s" : "") + " in " + management_ticks + " tick" + (management_ticks != 1 ? "s" : "") + ".");
// local start_tick = AIController.GetTick();
for (local i = lastAirManagedArray; i >= 0; --i) {
if (lastAirManagedManagement != 3) break;
lastAirManagedArray--;
// AILog.Info("managing route " + i + ". updateEngine");
airRouteManager.m_townRouteArray[i].updateEngine();
if (InterruptAirManagement(cur_date)) return;
}
ResetAirManagementVariables();
if (lastAirManagedManagement == 3) lastAirManagedManagement--;
// local management_ticks = AIController.GetTick() - start_tick;
// AILog.Info("Managed " + airRouteManager.m_townRouteArray.len() + " air route" + (airRouteManager.m_townRouteArray.len() != 1 ? "s" : "") + " in " + management_ticks + " tick" + (management_ticks != 1 ? "s" : "") + ".");
// local start_tick = AIController.GetTick();
for (local i = lastAirManagedArray; i >= 0; --i) {
if (lastAirManagedManagement != 2) break;
lastAirManagedArray--;
// AILog.Info("managing route " + i + ". sellVehiclesInDepot");
airRouteManager.m_townRouteArray[i].sellVehiclesInDepot();
if (InterruptAirManagement(cur_date)) return;
}
ResetAirManagementVariables();
if (lastAirManagedManagement == 2) lastAirManagedManagement--;
// local management_ticks = AIController.GetTick() - start_tick;
// AILog.Info("Managed " + airRouteManager.m_townRouteArray.len() + " air route" + (airRouteManager.m_townRouteArray.len() != 1 ? "s" : "") + " in " + management_ticks + " tick" + (management_ticks != 1 ? "s" : "") + ".");
// local start_tick = AIController.GetTick();
num_vehs = airRouteManager.getAircraftCount();
for (local i = lastAirManagedArray; i >= 0; --i) {
if (lastAirManagedManagement != 1) break;
lastAirManagedArray--;
// AILog.Info("managing route " + i + ". addremoveVehicleToRoute");
if (num_vehs < MAX_AIR_VEHICLES) {
num_vehs += airRouteManager.m_townRouteArray[i].addremoveVehicleToRoute(num_vehs < MAX_AIR_VEHICLES);
}
if (InterruptAirManagement(cur_date)) return;
}
ResetAirManagementVariables();
if (lastAirManagedManagement == 1) lastAirManagedManagement--;
// local management_ticks = AIController.GetTick() - start_tick;
// AILog.Info("Managed " + airRouteManager.m_townRouteArray.len() + " air route" + (airRouteManager.m_townRouteArray.len() != 1 ? "s" : "") + " in " + management_ticks + " tick" + (management_ticks != 1 ? "s" : "") + ".");
// local start_tick = AIController.GetTick();
for (local i = lastAirManagedArray; i >= 0; --i) {
if (lastAirManagedManagement != 0) break;
lastAirManagedArray--;
// AILog.Info("managing route " + i + ". removeIfUnserviced");
local cityFrom = airRouteManager.m_townRouteArray[i].m_cityFrom;
local cityTo = airRouteManager.m_townRouteArray[i].m_cityTo;
local cargoC = airRouteManager.m_townRouteArray[i].m_cargoClass;
if (airRouteManager.m_townRouteArray[i].removeIfUnserviced()) {
airRouteManager.m_townRouteArray.remove(i);
airTownManager.removeUsedCityPair(cityFrom, cityTo, cargoC, true);
}
if (InterruptAirManagement(cur_date)) return;
}
ResetAirManagementVariables();
if (lastAirManagedManagement == 0) lastAirManagedManagement--;
// local management_ticks = AIController.GetTick() - start_tick;
// AILog.Info("Managed " + airRouteManager.m_townRouteArray.len() + " air route" + (airRouteManager.m_townRouteArray.len() != 1 ? "s" : "") + " in " + management_ticks + " tick" + (management_ticks != 1 ? "s" : "") + ".");
}