|
1 /* $Id$ */ |
|
2 |
|
3 /* |
|
4 * This AI was created as a direct reaction to the big demand for some good AIs |
|
5 * in OTTD. Too bad it never left alpha-stage, and it is considered dead in its |
|
6 * current form. |
|
7 * By the time of writing this, we, the creator of this AI and a good friend of |
|
8 * mine, are designing a whole new AI-system that allows us to create AIs |
|
9 * easier and without all the fuzz we encountered while I was working on this |
|
10 * AI. By the time that system is finished, you can expect that this AI will |
|
11 * dissapear, because it is pretty obselete and bad programmed. |
|
12 * |
|
13 * Meanwhile I wish you all much fun with this AI; if you are interested as |
|
14 * AI-developer in this AI, I advise you not stare too long to some code, some |
|
15 * things in here really are... strange ;) But in either way: enjoy :) |
|
16 * |
|
17 * -- TrueLight :: 2005-09-01 |
|
18 */ |
|
19 |
|
20 #include "../../stdafx.h" |
|
21 #include "../../openttd.h" |
|
22 #include "../../debug.h" |
|
23 #include "../../functions.h" |
|
24 #include "../../road_map.h" |
|
25 #include "../../station_map.h" |
|
26 #include "table/strings.h" |
|
27 #include "../../map.h" |
|
28 #include "../../tile.h" |
|
29 #include "../../command.h" |
|
30 #include "trolly.h" |
|
31 #include "../../town.h" |
|
32 #include "../../industry.h" |
|
33 #include "../../station.h" |
|
34 #include "../../engine.h" |
|
35 #include "../../gui.h" |
|
36 #include "../../depot.h" |
|
37 #include "../../vehicle.h" |
|
38 #include "../../date.h" |
|
39 #include "../ai.h" |
|
40 |
|
41 // This function is called after StartUp. It is the init of an AI |
|
42 static void AiNew_State_FirstTime(Player *p) |
|
43 { |
|
44 // This assert is used to protect those function from misuse |
|
45 // You have quickly a small mistake in the state-array |
|
46 // With that, everything would go wrong. Finding that, is almost impossible |
|
47 // With this assert, that problem can never happen. |
|
48 assert(p->ainew.state == AI_STATE_FIRST_TIME); |
|
49 // We first have to init some things |
|
50 |
|
51 if (_current_player == 1) ShowErrorMessage(INVALID_STRING_ID, TEMP_AI_IN_PROGRESS, 0, 0); |
|
52 |
|
53 // The PathFinder (AyStar) |
|
54 // TODO: Maybe when an AI goes bankrupt, this is de-init |
|
55 // or when coming from a savegame.. should be checked out! |
|
56 p->ainew.path_info.start_tile_tl = 0; |
|
57 p->ainew.path_info.start_tile_br = 0; |
|
58 p->ainew.path_info.end_tile_tl = 0; |
|
59 p->ainew.path_info.end_tile_br = 0; |
|
60 p->ainew.pathfinder = new_AyStar_AiPathFinder(12, &p->ainew.path_info); |
|
61 |
|
62 p->ainew.idle = 0; |
|
63 p->ainew.last_vehiclecheck_date = _date; |
|
64 |
|
65 // We ALWAYS start with a bus route.. just some basic money ;) |
|
66 p->ainew.action = AI_ACTION_BUS_ROUTE; |
|
67 |
|
68 // Let's popup the news, and after that, start building.. |
|
69 p->ainew.state = AI_STATE_WAKE_UP; |
|
70 } |
|
71 |
|
72 |
|
73 // This function just waste some time |
|
74 // It keeps it more real. The AI can build on such tempo no normal user |
|
75 // can ever keep up with that. The competitor_speed already delays a bit |
|
76 // but after the AI finished a track it really needs to go to sleep. |
|
77 // |
|
78 // Let's say, we sleep between one and three days if the AI is put on Very Fast. |
|
79 // This means that on Very Slow it will be between 16 and 48 days.. slow enough? |
|
80 static void AiNew_State_Nothing(Player *p) |
|
81 { |
|
82 assert(p->ainew.state == AI_STATE_NOTHING); |
|
83 // If we are done idling, start over again |
|
84 if (p->ainew.idle == 0) p->ainew.idle = AI_RandomRange(DAY_TICKS * 2) + DAY_TICKS; |
|
85 if (--p->ainew.idle == 0) { |
|
86 // We are done idling.. what you say? Let's do something! |
|
87 // I mean.. the next tick ;) |
|
88 p->ainew.state = AI_STATE_WAKE_UP; |
|
89 } |
|
90 } |
|
91 |
|
92 |
|
93 // This function picks out a task we are going to do. |
|
94 // Currently supported: |
|
95 // - Make new route |
|
96 // - Check route |
|
97 // - Build HQ |
|
98 static void AiNew_State_WakeUp(Player *p) |
|
99 { |
|
100 int32 money; |
|
101 int c; |
|
102 assert(p->ainew.state == AI_STATE_WAKE_UP); |
|
103 // First, check if we have a HQ |
|
104 if (p->location_of_house == 0) { |
|
105 // We have no HQ yet, build one on a random place |
|
106 // Random till we found a place for it! |
|
107 // TODO: this should not be on a random place.. |
|
108 AiNew_Build_CompanyHQ(p, AI_Random() % MapSize()); |
|
109 // Enough for now, but we want to come back here the next time |
|
110 // so we do not change any status |
|
111 return; |
|
112 } |
|
113 |
|
114 money = p->player_money - AI_MINIMUM_MONEY; |
|
115 |
|
116 // Let's pick an action! |
|
117 if (p->ainew.action == AI_ACTION_NONE) { |
|
118 c = AI_Random() & 0xFF; |
|
119 if (p->current_loan > 0 && |
|
120 p->old_economy[1].income > AI_MINIMUM_INCOME_FOR_LOAN && |
|
121 c < 10) { |
|
122 p->ainew.action = AI_ACTION_REPAY_LOAN; |
|
123 } else if (p->ainew.last_vehiclecheck_date + AI_DAYS_BETWEEN_VEHICLE_CHECKS < _date) { |
|
124 // Check all vehicles once in a while |
|
125 p->ainew.action = AI_ACTION_CHECK_ALL_VEHICLES; |
|
126 p->ainew.last_vehiclecheck_date = _date; |
|
127 } else if (c < 100 && !_patches.ai_disable_veh_roadveh) { |
|
128 // Do we have any spots for road-vehicles left open? |
|
129 if (GetFreeUnitNumber(VEH_Road) <= _patches.max_roadveh) { |
|
130 if (c < 85) { |
|
131 p->ainew.action = AI_ACTION_TRUCK_ROUTE; |
|
132 } else { |
|
133 p->ainew.action = AI_ACTION_BUS_ROUTE; |
|
134 } |
|
135 } |
|
136 #if 0 |
|
137 } else if (c < 200 && !_patches.ai_disable_veh_train) { |
|
138 if (GetFreeUnitNumber(VEH_Train) <= _patches.max_trains) { |
|
139 p->ainew.action = AI_ACTION_TRAIN_ROUTE; |
|
140 } |
|
141 #endif |
|
142 } |
|
143 |
|
144 p->ainew.counter = 0; |
|
145 } |
|
146 |
|
147 if (p->ainew.counter++ > AI_MAX_TRIES_FOR_SAME_ROUTE) { |
|
148 p->ainew.action = AI_ACTION_NONE; |
|
149 return; |
|
150 } |
|
151 |
|
152 if (_patches.ai_disable_veh_roadveh && ( |
|
153 p->ainew.action == AI_ACTION_BUS_ROUTE || |
|
154 p->ainew.action == AI_ACTION_TRUCK_ROUTE |
|
155 )) { |
|
156 p->ainew.action = AI_ACTION_NONE; |
|
157 return; |
|
158 } |
|
159 |
|
160 if (p->ainew.action == AI_ACTION_REPAY_LOAN && |
|
161 money > AI_MINIMUM_LOAN_REPAY_MONEY) { |
|
162 // We start repaying some money.. |
|
163 p->ainew.state = AI_STATE_REPAY_MONEY; |
|
164 return; |
|
165 } |
|
166 |
|
167 if (p->ainew.action == AI_ACTION_CHECK_ALL_VEHICLES) { |
|
168 p->ainew.state = AI_STATE_CHECK_ALL_VEHICLES; |
|
169 return; |
|
170 } |
|
171 |
|
172 // It is useless to start finding a route if we don't have enough money |
|
173 // to build the route anyway.. |
|
174 if (p->ainew.action == AI_ACTION_BUS_ROUTE && |
|
175 money > AI_MINIMUM_BUS_ROUTE_MONEY) { |
|
176 if (GetFreeUnitNumber(VEH_Road) > _patches.max_roadveh) { |
|
177 p->ainew.action = AI_ACTION_NONE; |
|
178 return; |
|
179 } |
|
180 p->ainew.cargo = AI_NEED_CARGO; |
|
181 p->ainew.state = AI_STATE_LOCATE_ROUTE; |
|
182 p->ainew.tbt = AI_BUS; // Bus-route |
|
183 return; |
|
184 } |
|
185 if (p->ainew.action == AI_ACTION_TRUCK_ROUTE && |
|
186 money > AI_MINIMUM_TRUCK_ROUTE_MONEY) { |
|
187 if (GetFreeUnitNumber(VEH_Road) > _patches.max_roadveh) { |
|
188 p->ainew.action = AI_ACTION_NONE; |
|
189 return; |
|
190 } |
|
191 p->ainew.cargo = AI_NEED_CARGO; |
|
192 p->ainew.last_id = 0; |
|
193 p->ainew.state = AI_STATE_LOCATE_ROUTE; |
|
194 p->ainew.tbt = AI_TRUCK; |
|
195 return; |
|
196 } |
|
197 |
|
198 p->ainew.state = AI_STATE_NOTHING; |
|
199 } |
|
200 |
|
201 |
|
202 static void AiNew_State_ActionDone(Player *p) |
|
203 { |
|
204 p->ainew.action = AI_ACTION_NONE; |
|
205 p->ainew.state = AI_STATE_NOTHING; |
|
206 } |
|
207 |
|
208 |
|
209 // Check if a city or industry is good enough to start a route there |
|
210 static bool AiNew_Check_City_or_Industry(Player *p, int ic, byte type) |
|
211 { |
|
212 if (type == AI_CITY) { |
|
213 const Town* t = GetTown(ic); |
|
214 const Station* st; |
|
215 uint count = 0; |
|
216 int j = 0; |
|
217 |
|
218 // We don't like roadconstructions, don't even true such a city |
|
219 if (t->road_build_months != 0) return false; |
|
220 |
|
221 // Check if the rating in a city is high enough |
|
222 // If not, take a chance if we want to continue |
|
223 if (t->ratings[_current_player] < 0 && AI_CHANCE16(1,4)) return false; |
|
224 |
|
225 if (t->max_pass - t->act_pass < AI_CHECKCITY_NEEDED_CARGO && !AI_CHANCE16(1,AI_CHECKCITY_CITY_CHANCE)) return false; |
|
226 |
|
227 // Check if we have build a station in this town the last 6 months |
|
228 // else we don't do it. This is done, because stat updates can be slow |
|
229 // and sometimes it takes up to 4 months before the stats are corectly. |
|
230 // This way we don't get 12 busstations in one city of 100 population ;) |
|
231 FOR_ALL_STATIONS(st) { |
|
232 // Do we own it? |
|
233 if (st->owner == _current_player) { |
|
234 // Are we talking busses? |
|
235 if (p->ainew.tbt == AI_BUS && (FACIL_BUS_STOP & st->facilities) != FACIL_BUS_STOP) continue; |
|
236 // Is it the same city as we are in now? |
|
237 if (st->town != t) continue; |
|
238 // When was this station build? |
|
239 if (_date - st->build_date < AI_CHECKCITY_DATE_BETWEEN) return false; |
|
240 // Cound the amount of stations in this city that we own |
|
241 count++; |
|
242 } else { |
|
243 // We do not own it, request some info about the station |
|
244 // we want to know if this station gets the same good. If so, |
|
245 // we want to know its rating. If it is too high, we are not going |
|
246 // to build there |
|
247 if (!st->goods[CT_PASSENGERS].last_speed) continue; |
|
248 // Is it around our city |
|
249 if (DistanceManhattan(st->xy, t->xy) > 10) continue; |
|
250 // It does take this cargo.. what is his rating? |
|
251 if (st->goods[CT_PASSENGERS].rating < AI_CHECKCITY_CARGO_RATING) continue; |
|
252 j++; |
|
253 // When this is the first station, we build a second with no problem ;) |
|
254 if (j == 1) continue; |
|
255 // The rating is high.. second station... |
|
256 // a little chance that we still continue |
|
257 // But if there are 3 stations of this size, we never go on... |
|
258 if (j == 2 && AI_CHANCE16(1, AI_CHECKCITY_CARGO_RATING_CHANCE)) continue; |
|
259 // We don't like this station :( |
|
260 return false; |
|
261 } |
|
262 } |
|
263 |
|
264 // We are about to add one... |
|
265 count++; |
|
266 // Check if we the city can provide enough cargo for this amount of stations.. |
|
267 if (count * AI_CHECKCITY_CARGO_PER_STATION > t->max_pass) return false; |
|
268 |
|
269 // All check are okay, so we can build here! |
|
270 return true; |
|
271 } |
|
272 if (type == AI_INDUSTRY) { |
|
273 const Industry* i = GetIndustry(ic); |
|
274 const Station* st; |
|
275 int count = 0; |
|
276 int j = 0; |
|
277 |
|
278 if (i->town != NULL && i->town->ratings[_current_player] < 0 && AI_CHANCE16(1,4)) return false; |
|
279 |
|
280 // No limits on delevering stations! |
|
281 // Or for industry that does not give anything yet |
|
282 if (i->produced_cargo[0] == CT_INVALID || i->total_production[0] == 0) return true; |
|
283 |
|
284 if (i->total_production[0] - i->total_transported[0] < AI_CHECKCITY_NEEDED_CARGO) return false; |
|
285 |
|
286 // Check if we have build a station in this town the last 6 months |
|
287 // else we don't do it. This is done, because stat updates can be slow |
|
288 // and sometimes it takes up to 4 months before the stats are corectly. |
|
289 FOR_ALL_STATIONS(st) { |
|
290 // Do we own it? |
|
291 if (st->owner == _current_player) { |
|
292 // Are we talking trucks? |
|
293 if (p->ainew.tbt == AI_TRUCK && (FACIL_TRUCK_STOP & st->facilities) != FACIL_TRUCK_STOP) continue; |
|
294 // Is it the same city as we are in now? |
|
295 if (st->town != i->town) continue; |
|
296 // When was this station build? |
|
297 if (_date - st->build_date < AI_CHECKCITY_DATE_BETWEEN) return false; |
|
298 // Cound the amount of stations in this city that we own |
|
299 count++; |
|
300 } else { |
|
301 // We do not own it, request some info about the station |
|
302 // we want to know if this station gets the same good. If so, |
|
303 // we want to know its rating. If it is too high, we are not going |
|
304 // to build there |
|
305 if (i->produced_cargo[0] == CT_INVALID) continue; |
|
306 // It does not take this cargo |
|
307 if (!st->goods[i->produced_cargo[0]].last_speed) continue; |
|
308 // Is it around our industry |
|
309 if (DistanceManhattan(st->xy, i->xy) > 5) continue; |
|
310 // It does take this cargo.. what is his rating? |
|
311 if (st->goods[i->produced_cargo[0]].rating < AI_CHECKCITY_CARGO_RATING) continue; |
|
312 j++; |
|
313 // The rating is high.. a little chance that we still continue |
|
314 // But if there are 2 stations of this size, we never go on... |
|
315 if (j == 1 && AI_CHANCE16(1, AI_CHECKCITY_CARGO_RATING_CHANCE)) continue; |
|
316 // We don't like this station :( |
|
317 return false; |
|
318 } |
|
319 } |
|
320 |
|
321 // We are about to add one... |
|
322 count++; |
|
323 // Check if we the city can provide enough cargo for this amount of stations.. |
|
324 if (count * AI_CHECKCITY_CARGO_PER_STATION > i->total_production[0]) return false; |
|
325 |
|
326 // All check are okay, so we can build here! |
|
327 return true; |
|
328 } |
|
329 |
|
330 return true; |
|
331 } |
|
332 |
|
333 |
|
334 // This functions tries to locate a good route |
|
335 static void AiNew_State_LocateRoute(Player *p) |
|
336 { |
|
337 assert(p->ainew.state == AI_STATE_LOCATE_ROUTE); |
|
338 // For now, we only support PASSENGERS, CITY and BUSSES |
|
339 |
|
340 // We don't have a route yet |
|
341 if (p->ainew.cargo == AI_NEED_CARGO) { |
|
342 p->ainew.new_cost = 0; // No cost yet |
|
343 p->ainew.temp = -1; |
|
344 // Reset the counter |
|
345 p->ainew.counter = 0; |
|
346 |
|
347 p->ainew.from_ic = -1; |
|
348 p->ainew.to_ic = -1; |
|
349 if (p->ainew.tbt == AI_BUS) { |
|
350 // For now we only have a passenger route |
|
351 p->ainew.cargo = CT_PASSENGERS; |
|
352 |
|
353 // Find a route to cities |
|
354 p->ainew.from_type = AI_CITY; |
|
355 p->ainew.to_type = AI_CITY; |
|
356 } else if (p->ainew.tbt == AI_TRUCK) { |
|
357 p->ainew.cargo = AI_NO_CARGO; |
|
358 |
|
359 p->ainew.from_type = AI_INDUSTRY; |
|
360 p->ainew.to_type = AI_INDUSTRY; |
|
361 } |
|
362 |
|
363 // Now we are doing initing, we wait one tick |
|
364 return; |
|
365 } |
|
366 |
|
367 // Increase the counter and abort if it is taking too long! |
|
368 p->ainew.counter++; |
|
369 if (p->ainew.counter > AI_LOCATE_ROUTE_MAX_COUNTER) { |
|
370 // Switch back to doing nothing! |
|
371 p->ainew.state = AI_STATE_NOTHING; |
|
372 return; |
|
373 } |
|
374 |
|
375 // We are going to locate a city from where we are going to connect |
|
376 if (p->ainew.from_ic == -1) { |
|
377 if (p->ainew.temp == -1) { |
|
378 // First, we pick a random spot to search from |
|
379 if (p->ainew.from_type == AI_CITY) { |
|
380 p->ainew.temp = AI_RandomRange(GetMaxTownIndex() + 1); |
|
381 } else { |
|
382 p->ainew.temp = AI_RandomRange(GetMaxIndustryIndex() + 1); |
|
383 } |
|
384 } |
|
385 |
|
386 if (!AiNew_Check_City_or_Industry(p, p->ainew.temp, p->ainew.from_type)) { |
|
387 // It was not a valid city |
|
388 // increase the temp with one, and return. We will come back later here |
|
389 // to try again |
|
390 p->ainew.temp++; |
|
391 if (p->ainew.from_type == AI_CITY) { |
|
392 if (p->ainew.temp > GetMaxTownIndex()) p->ainew.temp = 0; |
|
393 } else { |
|
394 if (p->ainew.temp > GetMaxIndustryIndex()) p->ainew.temp = 0; |
|
395 } |
|
396 |
|
397 // Don't do an attempt if we are trying the same id as the last time... |
|
398 if (p->ainew.last_id == p->ainew.temp) return; |
|
399 p->ainew.last_id = p->ainew.temp; |
|
400 |
|
401 return; |
|
402 } |
|
403 |
|
404 // We found a good city/industry, save the data of it |
|
405 p->ainew.from_ic = p->ainew.temp; |
|
406 |
|
407 // Start the next tick with finding a to-city |
|
408 p->ainew.temp = -1; |
|
409 return; |
|
410 } |
|
411 |
|
412 // Find a to-city |
|
413 if (p->ainew.temp == -1) { |
|
414 // First, we pick a random spot to search to |
|
415 if (p->ainew.to_type == AI_CITY) { |
|
416 p->ainew.temp = AI_RandomRange(GetMaxTownIndex() + 1); |
|
417 } else { |
|
418 p->ainew.temp = AI_RandomRange(GetMaxIndustryIndex() + 1); |
|
419 } |
|
420 } |
|
421 |
|
422 // The same city is not allowed |
|
423 // Also check if the city is valid |
|
424 if (p->ainew.temp != p->ainew.from_ic && AiNew_Check_City_or_Industry(p, p->ainew.temp, p->ainew.to_type)) { |
|
425 // Maybe it is valid.. |
|
426 |
|
427 /* We need to know if they are not to far apart from eachother.. |
|
428 * We do that by checking how much cargo we have to move and how long the |
|
429 * route is. |
|
430 */ |
|
431 |
|
432 if (p->ainew.from_type == AI_CITY && p->ainew.tbt == AI_BUS) { |
|
433 const Town* town_from = GetTown(p->ainew.from_ic); |
|
434 const Town* town_temp = GetTown(p->ainew.temp); |
|
435 uint distance = DistanceManhattan(town_from->xy, town_temp->xy); |
|
436 int max_cargo; |
|
437 |
|
438 max_cargo = town_from->max_pass + town_temp->max_pass; |
|
439 max_cargo -= town_from->act_pass + town_temp->act_pass; |
|
440 |
|
441 // max_cargo is now the amount of cargo we can move between the two cities |
|
442 // If it is more than the distance, we allow it |
|
443 if (distance <= max_cargo * AI_LOCATEROUTE_BUS_CARGO_DISTANCE) { |
|
444 // We found a good city/industry, save the data of it |
|
445 p->ainew.to_ic = p->ainew.temp; |
|
446 p->ainew.state = AI_STATE_FIND_STATION; |
|
447 |
|
448 DEBUG(ai, 1, "[LocateRoute] found bus-route of %d tiles long (from %d to %d)", |
|
449 distance, |
|
450 p->ainew.from_ic, |
|
451 p->ainew.temp |
|
452 ); |
|
453 |
|
454 p->ainew.from_tile = 0; |
|
455 p->ainew.to_tile = 0; |
|
456 |
|
457 return; |
|
458 } |
|
459 } else if (p->ainew.tbt == AI_TRUCK) { |
|
460 const Industry* ind_from = GetIndustry(p->ainew.from_ic); |
|
461 const Industry* ind_temp = GetIndustry(p->ainew.temp); |
|
462 bool found = false; |
|
463 int max_cargo = 0; |
|
464 uint i; |
|
465 |
|
466 // TODO: in max_cargo, also check other cargo (beside [0]) |
|
467 // First we check if the from_ic produces cargo that this ic accepts |
|
468 if (ind_from->produced_cargo[0] != CT_INVALID && ind_from->total_production[0] != 0) { |
|
469 for (i = 0; i < lengthof(ind_temp->accepts_cargo); i++) { |
|
470 if (ind_temp->accepts_cargo[i] == CT_INVALID) break; |
|
471 if (ind_from->produced_cargo[0] == ind_temp->accepts_cargo[i]) { |
|
472 // Found a compatible industry |
|
473 max_cargo = ind_from->total_production[0] - ind_from->total_transported[0]; |
|
474 found = true; |
|
475 p->ainew.from_deliver = true; |
|
476 p->ainew.to_deliver = false; |
|
477 break; |
|
478 } |
|
479 } |
|
480 } |
|
481 if (!found && ind_temp->produced_cargo[0] != CT_INVALID && ind_temp->total_production[0] != 0) { |
|
482 // If not check if the current ic produces cargo that the from_ic accepts |
|
483 for (i = 0; i < lengthof(ind_from->accepts_cargo); i++) { |
|
484 if (ind_from->accepts_cargo[i] == CT_INVALID) break; |
|
485 if (ind_temp->produced_cargo[0] == ind_from->accepts_cargo[i]) { |
|
486 // Found a compatbiel industry |
|
487 found = true; |
|
488 max_cargo = ind_temp->total_production[0] - ind_temp->total_transported[0]; |
|
489 p->ainew.from_deliver = false; |
|
490 p->ainew.to_deliver = true; |
|
491 break; |
|
492 } |
|
493 } |
|
494 } |
|
495 if (found) { |
|
496 // Yeah, they are compatible!!! |
|
497 // Check the length against the amount of goods |
|
498 uint distance = DistanceManhattan(ind_from->xy, ind_temp->xy); |
|
499 |
|
500 if (distance > AI_LOCATEROUTE_TRUCK_MIN_DISTANCE && |
|
501 distance <= max_cargo * AI_LOCATEROUTE_TRUCK_CARGO_DISTANCE) { |
|
502 p->ainew.to_ic = p->ainew.temp; |
|
503 if (p->ainew.from_deliver) { |
|
504 p->ainew.cargo = ind_from->produced_cargo[0]; |
|
505 } else { |
|
506 p->ainew.cargo = ind_temp->produced_cargo[0]; |
|
507 } |
|
508 p->ainew.state = AI_STATE_FIND_STATION; |
|
509 |
|
510 DEBUG(ai, 1, "[LocateRoute] found truck-route of %d tiles long (from %d to %d)", |
|
511 distance, |
|
512 p->ainew.from_ic, |
|
513 p->ainew.temp |
|
514 ); |
|
515 |
|
516 p->ainew.from_tile = 0; |
|
517 p->ainew.to_tile = 0; |
|
518 |
|
519 return; |
|
520 } |
|
521 } |
|
522 } |
|
523 } |
|
524 |
|
525 // It was not a valid city |
|
526 // increase the temp with one, and return. We will come back later here |
|
527 // to try again |
|
528 p->ainew.temp++; |
|
529 if (p->ainew.to_type == AI_CITY) { |
|
530 if (p->ainew.temp > GetMaxTownIndex()) p->ainew.temp = 0; |
|
531 } else { |
|
532 if (p->ainew.temp > GetMaxIndustryIndex()) p->ainew.temp = 0; |
|
533 } |
|
534 |
|
535 // Don't do an attempt if we are trying the same id as the last time... |
|
536 if (p->ainew.last_id == p->ainew.temp) return; |
|
537 p->ainew.last_id = p->ainew.temp; |
|
538 } |
|
539 |
|
540 |
|
541 // Check if there are not more than a certain amount of vehicles pointed to a certain |
|
542 // station. This to prevent 10 busses going to one station, which gives... problems ;) |
|
543 static bool AiNew_CheckVehicleStation(Player *p, Station *st) |
|
544 { |
|
545 int count = 0; |
|
546 Vehicle *v; |
|
547 |
|
548 // Also check if we don't have already a lot of busses to this city... |
|
549 FOR_ALL_VEHICLES(v) { |
|
550 if (v->owner == _current_player) { |
|
551 const Order *order; |
|
552 |
|
553 FOR_VEHICLE_ORDERS(v, order) { |
|
554 if (order->type == OT_GOTO_STATION && GetStation(order->dest) == st) { |
|
555 // This vehicle has this city in its list |
|
556 count++; |
|
557 } |
|
558 } |
|
559 } |
|
560 } |
|
561 |
|
562 if (count > AI_CHECK_MAX_VEHICLE_PER_STATION) return false; |
|
563 return true; |
|
564 } |
|
565 |
|
566 // This function finds a good spot for a station |
|
567 static void AiNew_State_FindStation(Player *p) |
|
568 { |
|
569 TileIndex tile; |
|
570 Station *st; |
|
571 int count = 0; |
|
572 EngineID i; |
|
573 TileIndex new_tile = 0; |
|
574 byte direction = 0; |
|
575 Town *town = NULL; |
|
576 assert(p->ainew.state == AI_STATE_FIND_STATION); |
|
577 |
|
578 if (p->ainew.from_tile == 0) { |
|
579 // First we scan for a station in the from-city |
|
580 if (p->ainew.from_type == AI_CITY) { |
|
581 town = GetTown(p->ainew.from_ic); |
|
582 tile = town->xy; |
|
583 } else { |
|
584 tile = GetIndustry(p->ainew.from_ic)->xy; |
|
585 } |
|
586 } else if (p->ainew.to_tile == 0) { |
|
587 // Second we scan for a station in the to-city |
|
588 if (p->ainew.to_type == AI_CITY) { |
|
589 town = GetTown(p->ainew.to_ic); |
|
590 tile = town->xy; |
|
591 } else { |
|
592 tile = GetIndustry(p->ainew.to_ic)->xy; |
|
593 } |
|
594 } else { |
|
595 // Unsupported request |
|
596 // Go to FIND_PATH |
|
597 p->ainew.temp = -1; |
|
598 p->ainew.state = AI_STATE_FIND_PATH; |
|
599 return; |
|
600 } |
|
601 |
|
602 // First, we are going to look at the stations that already exist inside the city |
|
603 // If there is enough cargo left in the station, we take that station |
|
604 // If that is not possible, and there are more than 2 stations in the city, abort |
|
605 i = AiNew_PickVehicle(p); |
|
606 // Euhmz, this should not happen _EVER_ |
|
607 // Quit finding a route... |
|
608 if (i == INVALID_ENGINE) { |
|
609 p->ainew.state = AI_STATE_NOTHING; |
|
610 return; |
|
611 } |
|
612 |
|
613 FOR_ALL_STATIONS(st) { |
|
614 if (st->owner == _current_player) { |
|
615 if (p->ainew.tbt == AI_BUS && (FACIL_BUS_STOP & st->facilities) == FACIL_BUS_STOP) { |
|
616 if (st->town == town) { |
|
617 // Check how much cargo there is left in the station |
|
618 if ((st->goods[p->ainew.cargo].waiting_acceptance & 0xFFF) > RoadVehInfo(i)->capacity * AI_STATION_REUSE_MULTIPLER) { |
|
619 if (AiNew_CheckVehicleStation(p, st)) { |
|
620 // We did found a station that was good enough! |
|
621 new_tile = st->xy; |
|
622 direction = GetRoadStopDir(st->xy); |
|
623 break; |
|
624 } |
|
625 } |
|
626 count++; |
|
627 } |
|
628 } |
|
629 } |
|
630 } |
|
631 // We are going to add a new station... |
|
632 if (new_tile == 0) count++; |
|
633 // No more than 2 stations allowed in a city |
|
634 // This is because only the best 2 stations of one cargo do get any cargo |
|
635 if (count > 2) { |
|
636 p->ainew.state = AI_STATE_NOTHING; |
|
637 return; |
|
638 } |
|
639 |
|
640 if (new_tile == 0 && p->ainew.tbt == AI_BUS) { |
|
641 uint x, y, i = 0; |
|
642 int r; |
|
643 uint best; |
|
644 uint accepts[NUM_CARGO]; |
|
645 TileIndex found_spot[AI_FINDSTATION_TILE_RANGE*AI_FINDSTATION_TILE_RANGE*4]; |
|
646 uint found_best[AI_FINDSTATION_TILE_RANGE*AI_FINDSTATION_TILE_RANGE*4]; |
|
647 // To find a good spot we scan a range from the center, a get the point |
|
648 // where we get the most cargo and where it is buildable. |
|
649 // TODO: also check for station of myself and make sure we are not |
|
650 // taking eachothers passangers away (bad result when it does not) |
|
651 for (x = TileX(tile) - AI_FINDSTATION_TILE_RANGE; x <= TileX(tile) + AI_FINDSTATION_TILE_RANGE; x++) { |
|
652 for (y = TileY(tile) - AI_FINDSTATION_TILE_RANGE; y <= TileY(tile) + AI_FINDSTATION_TILE_RANGE; y++) { |
|
653 new_tile = TileXY(x, y); |
|
654 if (IsTileType(new_tile, MP_CLEAR) || IsTileType(new_tile, MP_TREES)) { |
|
655 // This tile we can build on! |
|
656 // Check acceptance |
|
657 // XXX - Get the catchment area |
|
658 GetAcceptanceAroundTiles(accepts, new_tile, 1, 1, 4); |
|
659 // >> 3 == 0 means no cargo |
|
660 if (accepts[p->ainew.cargo] >> 3 == 0) continue; |
|
661 // See if we can build the station |
|
662 r = AiNew_Build_Station(p, p->ainew.tbt, new_tile, 0, 0, 0, DC_QUERY_COST); |
|
663 if (CmdFailed(r)) continue; |
|
664 // We can build it, so add it to found_spot |
|
665 found_spot[i] = new_tile; |
|
666 found_best[i++] = accepts[p->ainew.cargo]; |
|
667 } |
|
668 } |
|
669 } |
|
670 |
|
671 // If i is still zero, we did not find anything |
|
672 if (i == 0) { |
|
673 p->ainew.state = AI_STATE_NOTHING; |
|
674 return; |
|
675 } |
|
676 |
|
677 // Go through all the found_best and check which has the highest value |
|
678 best = 0; |
|
679 new_tile = 0; |
|
680 |
|
681 for (x = 0; x < i; x++) { |
|
682 if (found_best[x] > best || |
|
683 (found_best[x] == best && DistanceManhattan(tile, new_tile) > DistanceManhattan(tile, found_spot[x]))) { |
|
684 new_tile = found_spot[x]; |
|
685 best = found_best[x]; |
|
686 } |
|
687 } |
|
688 |
|
689 // See how much it is going to cost us... |
|
690 r = AiNew_Build_Station(p, p->ainew.tbt, new_tile, 0, 0, 0, DC_QUERY_COST); |
|
691 p->ainew.new_cost += r; |
|
692 |
|
693 direction = AI_PATHFINDER_NO_DIRECTION; |
|
694 } else if (new_tile == 0 && p->ainew.tbt == AI_TRUCK) { |
|
695 // Truck station locater works differently.. a station can be on any place |
|
696 // as long as it is in range. So we give back code AI_STATION_RANGE |
|
697 // so the pathfinder routine can work it out! |
|
698 new_tile = AI_STATION_RANGE; |
|
699 direction = AI_PATHFINDER_NO_DIRECTION; |
|
700 } |
|
701 |
|
702 if (p->ainew.from_tile == 0) { |
|
703 p->ainew.from_tile = new_tile; |
|
704 p->ainew.from_direction = direction; |
|
705 // Now we found thisone, go in for to_tile |
|
706 return; |
|
707 } else if (p->ainew.to_tile == 0) { |
|
708 p->ainew.to_tile = new_tile; |
|
709 p->ainew.to_direction = direction; |
|
710 // K, done placing stations! |
|
711 p->ainew.temp = -1; |
|
712 p->ainew.state = AI_STATE_FIND_PATH; |
|
713 return; |
|
714 } |
|
715 } |
|
716 |
|
717 |
|
718 // We try to find a path between 2 points |
|
719 static void AiNew_State_FindPath(Player *p) |
|
720 { |
|
721 int r; |
|
722 assert(p->ainew.state == AI_STATE_FIND_PATH); |
|
723 |
|
724 // First time, init some data |
|
725 if (p->ainew.temp == -1) { |
|
726 // Init path_info |
|
727 if (p->ainew.from_tile == AI_STATION_RANGE) { |
|
728 const Industry* i = GetIndustry(p->ainew.from_ic); |
|
729 |
|
730 // For truck routes we take a range around the industry |
|
731 p->ainew.path_info.start_tile_tl = i->xy - TileDiffXY(1, 1); |
|
732 p->ainew.path_info.start_tile_br = i->xy + TileDiffXY(i->width + 1, i->height + 1); |
|
733 p->ainew.path_info.start_direction = p->ainew.from_direction; |
|
734 } else { |
|
735 p->ainew.path_info.start_tile_tl = p->ainew.from_tile; |
|
736 p->ainew.path_info.start_tile_br = p->ainew.from_tile; |
|
737 p->ainew.path_info.start_direction = p->ainew.from_direction; |
|
738 } |
|
739 |
|
740 if (p->ainew.to_tile == AI_STATION_RANGE) { |
|
741 const Industry* i = GetIndustry(p->ainew.to_ic); |
|
742 |
|
743 p->ainew.path_info.end_tile_tl = i->xy - TileDiffXY(1, 1); |
|
744 p->ainew.path_info.end_tile_br = i->xy + TileDiffXY(i->width + 1, i->height + 1); |
|
745 p->ainew.path_info.end_direction = p->ainew.to_direction; |
|
746 } else { |
|
747 p->ainew.path_info.end_tile_tl = p->ainew.to_tile; |
|
748 p->ainew.path_info.end_tile_br = p->ainew.to_tile; |
|
749 p->ainew.path_info.end_direction = p->ainew.to_direction; |
|
750 } |
|
751 |
|
752 p->ainew.path_info.rail_or_road = (p->ainew.tbt == AI_TRAIN); |
|
753 |
|
754 // First, clean the pathfinder with our new begin and endpoints |
|
755 clean_AyStar_AiPathFinder(p->ainew.pathfinder, &p->ainew.path_info); |
|
756 |
|
757 p->ainew.temp = 0; |
|
758 } |
|
759 |
|
760 // Start the pathfinder |
|
761 r = p->ainew.pathfinder->main(p->ainew.pathfinder); |
|
762 switch (r) { |
|
763 case AYSTAR_NO_PATH: |
|
764 DEBUG(ai, 1, "No route found by pathfinder"); |
|
765 // Start all over again |
|
766 p->ainew.state = AI_STATE_NOTHING; |
|
767 break; |
|
768 |
|
769 case AYSTAR_FOUND_END_NODE: // We found the end-point |
|
770 p->ainew.temp = -1; |
|
771 p->ainew.state = AI_STATE_FIND_DEPOT; |
|
772 break; |
|
773 |
|
774 // In any other case, we are still busy finding the route |
|
775 default: break; |
|
776 } |
|
777 } |
|
778 |
|
779 |
|
780 // This function tries to locate a good place for a depot! |
|
781 static void AiNew_State_FindDepot(Player *p) |
|
782 { |
|
783 // To place the depot, we walk through the route, and if we find a lovely spot (MP_CLEAR, MP_TREES), we place it there.. |
|
784 // Simple, easy, works! |
|
785 // To make the depot stand in the middle of the route, we start from the center.. |
|
786 // But first we walk through the route see if we can find a depot that is ours |
|
787 // this keeps things nice ;) |
|
788 int g, i, r; |
|
789 DiagDirection j; |
|
790 TileIndex tile; |
|
791 assert(p->ainew.state == AI_STATE_FIND_DEPOT); |
|
792 |
|
793 p->ainew.depot_tile = 0; |
|
794 |
|
795 for (i=2;i<p->ainew.path_info.route_length-2;i++) { |
|
796 tile = p->ainew.path_info.route[i]; |
|
797 for (j = 0; j < 4; j++) { |
|
798 TileIndex t = tile + TileOffsByDiagDir(j); |
|
799 |
|
800 if (IsTileType(t, MP_STREET) && |
|
801 GetRoadTileType(t) == ROAD_TILE_DEPOT && |
|
802 IsTileOwner(t, _current_player) && |
|
803 GetRoadDepotDirection(t) == ReverseDiagDir(j)) { |
|
804 p->ainew.depot_tile = t; |
|
805 p->ainew.depot_direction = ReverseDiagDir(j); |
|
806 p->ainew.state = AI_STATE_VERIFY_ROUTE; |
|
807 return; |
|
808 } |
|
809 } |
|
810 } |
|
811 |
|
812 // This routine let depot finding start in the middle, and work his way to the stations |
|
813 // It makes depot placing nicer :) |
|
814 i = p->ainew.path_info.route_length / 2; |
|
815 g = 1; |
|
816 while (i > 1 && i < p->ainew.path_info.route_length - 2) { |
|
817 i += g; |
|
818 g *= -1; |
|
819 (g < 0?g--:g++); |
|
820 |
|
821 if (p->ainew.path_info.route_extra[i] != 0 || p->ainew.path_info.route_extra[i+1] != 0) { |
|
822 // Bridge or tunnel.. we can't place a depot there |
|
823 continue; |
|
824 } |
|
825 |
|
826 tile = p->ainew.path_info.route[i]; |
|
827 |
|
828 for (j = 0; j < 4; j++) { |
|
829 TileIndex t = tile + TileOffsByDiagDir(j); |
|
830 |
|
831 // It may not be placed on the road/rail itself |
|
832 // And because it is not build yet, we can't see it on the tile.. |
|
833 // So check the surrounding tiles :) |
|
834 if (t == p->ainew.path_info.route[i - 1] || |
|
835 t == p->ainew.path_info.route[i + 1]) { |
|
836 continue; |
|
837 } |
|
838 // Not around a bridge? |
|
839 if (p->ainew.path_info.route_extra[i] != 0) continue; |
|
840 if (IsTileType(tile, MP_TUNNELBRIDGE)) continue; |
|
841 // Is the terrain clear? |
|
842 if (IsTileType(t, MP_CLEAR) || IsTileType(t, MP_TREES)) { |
|
843 // If the current tile is on a slope then we do not allow this |
|
844 if (GetTileSlope(tile, NULL) != SLOPE_FLAT) continue; |
|
845 // Check if everything went okay.. |
|
846 r = AiNew_Build_Depot(p, t, ReverseDiagDir(j), 0); |
|
847 if (CmdFailed(r)) continue; |
|
848 // Found a spot! |
|
849 p->ainew.new_cost += r; |
|
850 p->ainew.depot_tile = t; |
|
851 p->ainew.depot_direction = ReverseDiagDir(j); // Reverse direction |
|
852 p->ainew.state = AI_STATE_VERIFY_ROUTE; |
|
853 return; |
|
854 } |
|
855 } |
|
856 } |
|
857 |
|
858 // Failed to find a depot? |
|
859 p->ainew.state = AI_STATE_NOTHING; |
|
860 } |
|
861 |
|
862 |
|
863 // This function calculates how many vehicles there are needed on this |
|
864 // traject. |
|
865 // It works pretty simple: get the length, see how much we move around |
|
866 // and hussle that, and you know how many vehicles there are needed. |
|
867 // It returns the cost for the vehicles |
|
868 static int AiNew_HowManyVehicles(Player *p) |
|
869 { |
|
870 if (p->ainew.tbt == AI_BUS) { |
|
871 // For bus-routes we look at the time before we are back in the station |
|
872 EngineID i; |
|
873 int length, tiles_a_day; |
|
874 int amount; |
|
875 i = AiNew_PickVehicle(p); |
|
876 if (i == INVALID_ENGINE) return 0; |
|
877 // Passenger run.. how long is the route? |
|
878 length = p->ainew.path_info.route_length; |
|
879 // Calculating tiles a day a vehicle moves is not easy.. this is how it must be done! |
|
880 tiles_a_day = RoadVehInfo(i)->max_speed * DAY_TICKS / 256 / 16; |
|
881 // We want a vehicle in a station once a month at least, so, calculate it! |
|
882 // (the * 2 is because we have 2 stations ;)) |
|
883 amount = length * 2 * 2 / tiles_a_day / 30; |
|
884 if (amount == 0) amount = 1; |
|
885 return amount; |
|
886 } else if (p->ainew.tbt == AI_TRUCK) { |
|
887 // For truck-routes we look at the cargo |
|
888 EngineID i; |
|
889 int length, amount, tiles_a_day; |
|
890 int max_cargo; |
|
891 i = AiNew_PickVehicle(p); |
|
892 if (i == INVALID_ENGINE) return 0; |
|
893 // Passenger run.. how long is the route? |
|
894 length = p->ainew.path_info.route_length; |
|
895 // Calculating tiles a day a vehicle moves is not easy.. this is how it must be done! |
|
896 tiles_a_day = RoadVehInfo(i)->max_speed * DAY_TICKS / 256 / 16; |
|
897 if (p->ainew.from_deliver) { |
|
898 max_cargo = GetIndustry(p->ainew.from_ic)->total_production[0]; |
|
899 } else { |
|
900 max_cargo = GetIndustry(p->ainew.to_ic)->total_production[0]; |
|
901 } |
|
902 |
|
903 // This is because moving 60% is more than we can dream of! |
|
904 max_cargo *= 0.6; |
|
905 // We want all the cargo to be gone in a month.. so, we know the cargo it delivers |
|
906 // we know what the vehicle takes with him, and we know the time it takes him |
|
907 // to get back here.. now let's do some math! |
|
908 amount = 2 * length * max_cargo / tiles_a_day / 30 / RoadVehInfo(i)->capacity; |
|
909 amount += 1; |
|
910 return amount; |
|
911 } else { |
|
912 // Currently not supported |
|
913 return 0; |
|
914 } |
|
915 } |
|
916 |
|
917 |
|
918 // This function checks: |
|
919 // - If the route went okay |
|
920 // - Calculates the amount of money needed to build the route |
|
921 // - Calculates how much vehicles needed for the route |
|
922 static void AiNew_State_VerifyRoute(Player *p) |
|
923 { |
|
924 int res, i; |
|
925 assert(p->ainew.state == AI_STATE_VERIFY_ROUTE); |
|
926 |
|
927 // Let's calculate the cost of the path.. |
|
928 // new_cost already contains the cost of the stations |
|
929 p->ainew.path_info.position = -1; |
|
930 |
|
931 do { |
|
932 p->ainew.path_info.position++; |
|
933 p->ainew.new_cost += AiNew_Build_RoutePart(p, &p->ainew.path_info, DC_QUERY_COST); |
|
934 } while (p->ainew.path_info.position != -2); |
|
935 |
|
936 // Now we know the price of build station + path. Now check how many vehicles |
|
937 // we need and what the price for that will be |
|
938 res = AiNew_HowManyVehicles(p); |
|
939 // If res == 0, no vehicle was found, or an other problem did occour |
|
940 if (res == 0) { |
|
941 p->ainew.state = AI_STATE_NOTHING; |
|
942 return; |
|
943 } |
|
944 p->ainew.amount_veh = res; |
|
945 p->ainew.cur_veh = 0; |
|
946 |
|
947 // Check how much it it going to cost us.. |
|
948 for (i=0;i<res;i++) { |
|
949 p->ainew.new_cost += AiNew_Build_Vehicle(p, 0, DC_QUERY_COST); |
|
950 } |
|
951 |
|
952 // Now we know how much the route is going to cost us |
|
953 // Check if we have enough money for it! |
|
954 if (p->ainew.new_cost > p->player_money - AI_MINIMUM_MONEY) { |
|
955 // Too bad.. |
|
956 DEBUG(ai, 1, "Insufficient funds to build route (%d)", p->ainew.new_cost); |
|
957 p->ainew.state = AI_STATE_NOTHING; |
|
958 return; |
|
959 } |
|
960 |
|
961 // Now we can build the route, check the direction of the stations! |
|
962 if (p->ainew.from_direction == AI_PATHFINDER_NO_DIRECTION) { |
|
963 p->ainew.from_direction = AiNew_GetDirection(p->ainew.path_info.route[p->ainew.path_info.route_length-1], p->ainew.path_info.route[p->ainew.path_info.route_length-2]); |
|
964 } |
|
965 if (p->ainew.to_direction == AI_PATHFINDER_NO_DIRECTION) { |
|
966 p->ainew.to_direction = AiNew_GetDirection(p->ainew.path_info.route[0], p->ainew.path_info.route[1]); |
|
967 } |
|
968 if (p->ainew.from_tile == AI_STATION_RANGE) |
|
969 p->ainew.from_tile = p->ainew.path_info.route[p->ainew.path_info.route_length-1]; |
|
970 if (p->ainew.to_tile == AI_STATION_RANGE) |
|
971 p->ainew.to_tile = p->ainew.path_info.route[0]; |
|
972 |
|
973 p->ainew.state = AI_STATE_BUILD_STATION; |
|
974 p->ainew.temp = 0; |
|
975 |
|
976 DEBUG(ai, 1, "The route is set and buildable, building 0x%X to 0x%X...", p->ainew.from_tile, p->ainew.to_tile); |
|
977 } |
|
978 |
|
979 |
|
980 // Build the stations |
|
981 static void AiNew_State_BuildStation(Player *p) |
|
982 { |
|
983 int res = 0; |
|
984 assert(p->ainew.state == AI_STATE_BUILD_STATION); |
|
985 if (p->ainew.temp == 0) { |
|
986 if (!IsTileType(p->ainew.from_tile, MP_STATION)) |
|
987 res = AiNew_Build_Station(p, p->ainew.tbt, p->ainew.from_tile, 0, 0, p->ainew.from_direction, DC_EXEC); |
|
988 } else { |
|
989 if (!IsTileType(p->ainew.to_tile, MP_STATION)) |
|
990 res = AiNew_Build_Station(p, p->ainew.tbt, p->ainew.to_tile, 0, 0, p->ainew.to_direction, DC_EXEC); |
|
991 p->ainew.state = AI_STATE_BUILD_PATH; |
|
992 } |
|
993 if (CmdFailed(res)) { |
|
994 DEBUG(ai, 0, "[BuildStation] station could not be built (0x%X)", p->ainew.to_tile); |
|
995 p->ainew.state = AI_STATE_NOTHING; |
|
996 // If the first station _was_ build, destroy it |
|
997 if (p->ainew.temp != 0) |
|
998 AI_DoCommand(p->ainew.from_tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); |
|
999 return; |
|
1000 } |
|
1001 p->ainew.temp++; |
|
1002 } |
|
1003 |
|
1004 |
|
1005 // Build the path |
|
1006 static void AiNew_State_BuildPath(Player *p) |
|
1007 { |
|
1008 assert(p->ainew.state == AI_STATE_BUILD_PATH); |
|
1009 // p->ainew.temp is set to -1 when this function is called for the first time |
|
1010 if (p->ainew.temp == -1) { |
|
1011 DEBUG(ai, 1, "Starting to build new path"); |
|
1012 // Init the counter |
|
1013 p->ainew.counter = (4 - _opt.diff.competitor_speed) * AI_BUILDPATH_PAUSE + 1; |
|
1014 // Set the position to the startingplace (-1 because in a minute we do ++) |
|
1015 p->ainew.path_info.position = -1; |
|
1016 // And don't do this again |
|
1017 p->ainew.temp = 0; |
|
1018 } |
|
1019 // Building goes very fast on normal rate, so we are going to slow it down.. |
|
1020 // By let the counter count from AI_BUILDPATH_PAUSE to 0, we have a nice way :) |
|
1021 if (--p->ainew.counter != 0) return; |
|
1022 p->ainew.counter = (4 - _opt.diff.competitor_speed) * AI_BUILDPATH_PAUSE + 1; |
|
1023 |
|
1024 // Increase the building position |
|
1025 p->ainew.path_info.position++; |
|
1026 // Build route |
|
1027 AiNew_Build_RoutePart(p, &p->ainew.path_info, DC_EXEC); |
|
1028 if (p->ainew.path_info.position == -2) { |
|
1029 // This means we are done building! |
|
1030 |
|
1031 if (p->ainew.tbt == AI_TRUCK && !_patches.roadveh_queue) { |
|
1032 // If they not queue, they have to go up and down to try again at a station... |
|
1033 // We don't want that, so try building some road left or right of the station |
|
1034 int dir1, dir2, dir3; |
|
1035 TileIndex tile; |
|
1036 int i, ret; |
|
1037 for (i=0;i<2;i++) { |
|
1038 if (i == 0) { |
|
1039 tile = p->ainew.from_tile + TileOffsByDiagDir(p->ainew.from_direction); |
|
1040 dir1 = p->ainew.from_direction - 1; |
|
1041 if (dir1 < 0) dir1 = 3; |
|
1042 dir2 = p->ainew.from_direction + 1; |
|
1043 if (dir2 > 3) dir2 = 0; |
|
1044 dir3 = p->ainew.from_direction; |
|
1045 } else { |
|
1046 tile = p->ainew.to_tile + TileOffsByDiagDir(p->ainew.to_direction); |
|
1047 dir1 = p->ainew.to_direction - 1; |
|
1048 if (dir1 < 0) dir1 = 3; |
|
1049 dir2 = p->ainew.to_direction + 1; |
|
1050 if (dir2 > 3) dir2 = 0; |
|
1051 dir3 = p->ainew.to_direction; |
|
1052 } |
|
1053 |
|
1054 ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir1)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
|
1055 if (!CmdFailed(ret)) { |
|
1056 dir1 = TileOffsByDiagDir(dir1); |
|
1057 if (IsTileType(tile + dir1, MP_CLEAR) || IsTileType(tile + dir1, MP_TREES)) { |
|
1058 ret = AI_DoCommand(tile+dir1, AiNew_GetRoadDirection(tile, tile+dir1, tile+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
|
1059 if (!CmdFailed(ret)) { |
|
1060 if (IsTileType(tile + dir1 + dir1, MP_CLEAR) || IsTileType(tile + dir1 + dir1, MP_TREES)) |
|
1061 AI_DoCommand(tile+dir1+dir1, AiNew_GetRoadDirection(tile+dir1, tile+dir1+dir1, tile+dir1+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
|
1062 } |
|
1063 } |
|
1064 } |
|
1065 |
|
1066 ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir2)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
|
1067 if (!CmdFailed(ret)) { |
|
1068 dir2 = TileOffsByDiagDir(dir2); |
|
1069 if (IsTileType(tile + dir2, MP_CLEAR) || IsTileType(tile + dir2, MP_TREES)) { |
|
1070 ret = AI_DoCommand(tile+dir2, AiNew_GetRoadDirection(tile, tile+dir2, tile+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
|
1071 if (!CmdFailed(ret)) { |
|
1072 if (IsTileType(tile + dir2 + dir2, MP_CLEAR) || IsTileType(tile + dir2 + dir2, MP_TREES)) |
|
1073 AI_DoCommand(tile+dir2+dir2, AiNew_GetRoadDirection(tile+dir2, tile+dir2+dir2, tile+dir2+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
|
1074 } |
|
1075 } |
|
1076 } |
|
1077 |
|
1078 ret = AI_DoCommand(tile, DiagDirToRoadBits(dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
|
1079 if (!CmdFailed(ret)) { |
|
1080 dir3 = TileOffsByDiagDir(dir3); |
|
1081 if (IsTileType(tile + dir3, MP_CLEAR) || IsTileType(tile + dir3, MP_TREES)) { |
|
1082 ret = AI_DoCommand(tile+dir3, AiNew_GetRoadDirection(tile, tile+dir3, tile+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
|
1083 if (!CmdFailed(ret)) { |
|
1084 if (IsTileType(tile + dir3 + dir3, MP_CLEAR) || IsTileType(tile + dir3 + dir3, MP_TREES)) |
|
1085 AI_DoCommand(tile+dir3+dir3, AiNew_GetRoadDirection(tile+dir3, tile+dir3+dir3, tile+dir3+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
|
1086 } |
|
1087 } |
|
1088 } |
|
1089 } |
|
1090 } |
|
1091 |
|
1092 DEBUG(ai, 1, "Finished building path, cost: %d", p->ainew.new_cost); |
|
1093 p->ainew.state = AI_STATE_BUILD_DEPOT; |
|
1094 } |
|
1095 } |
|
1096 |
|
1097 |
|
1098 // Builds the depot |
|
1099 static void AiNew_State_BuildDepot(Player *p) |
|
1100 { |
|
1101 int res = 0; |
|
1102 assert(p->ainew.state == AI_STATE_BUILD_DEPOT); |
|
1103 |
|
1104 if (IsTileType(p->ainew.depot_tile, MP_STREET) && GetRoadTileType(p->ainew.depot_tile) == ROAD_TILE_DEPOT) { |
|
1105 if (IsTileOwner(p->ainew.depot_tile, _current_player)) { |
|
1106 // The depot is already built |
|
1107 p->ainew.state = AI_STATE_BUILD_VEHICLE; |
|
1108 return; |
|
1109 } else { |
|
1110 // There is a depot, but not of our team! :( |
|
1111 p->ainew.state = AI_STATE_NOTHING; |
|
1112 return; |
|
1113 } |
|
1114 } |
|
1115 |
|
1116 // There is a bus on the tile we want to build road on... idle till he is gone! (BAD PERSON! :p) |
|
1117 if (!EnsureNoVehicle(p->ainew.depot_tile + TileOffsByDiagDir(p->ainew.depot_direction))) |
|
1118 return; |
|
1119 |
|
1120 res = AiNew_Build_Depot(p, p->ainew.depot_tile, p->ainew.depot_direction, DC_EXEC); |
|
1121 if (CmdFailed(res)) { |
|
1122 DEBUG(ai, 0, "[BuildDepot] depot could not be built (0x%X)", p->ainew.depot_tile); |
|
1123 p->ainew.state = AI_STATE_NOTHING; |
|
1124 return; |
|
1125 } |
|
1126 |
|
1127 p->ainew.state = AI_STATE_BUILD_VEHICLE; |
|
1128 p->ainew.idle = 10; |
|
1129 p->ainew.veh_main_id = INVALID_VEHICLE; |
|
1130 } |
|
1131 |
|
1132 |
|
1133 // Build vehicles |
|
1134 static void AiNew_State_BuildVehicle(Player *p) |
|
1135 { |
|
1136 int res; |
|
1137 assert(p->ainew.state == AI_STATE_BUILD_VEHICLE); |
|
1138 |
|
1139 // Check if we need to build a vehicle |
|
1140 if (p->ainew.amount_veh == 0) { |
|
1141 // Nope, we are done! |
|
1142 // This means: we are all done! The route is open.. go back to NOTHING |
|
1143 // He will idle some time and it will all start over again.. :) |
|
1144 p->ainew.state = AI_STATE_ACTION_DONE; |
|
1145 return; |
|
1146 } |
|
1147 if (--p->ainew.idle != 0) return; |
|
1148 // It is realistic that the AI can only build 1 vehicle a day.. |
|
1149 // This makes sure of that! |
|
1150 p->ainew.idle = AI_BUILD_VEHICLE_TIME_BETWEEN; |
|
1151 |
|
1152 // Build the vehicle |
|
1153 res = AiNew_Build_Vehicle(p, p->ainew.depot_tile, DC_EXEC); |
|
1154 if (CmdFailed(res)) { |
|
1155 // This happens when the AI can't build any more vehicles! |
|
1156 p->ainew.state = AI_STATE_NOTHING; |
|
1157 return; |
|
1158 } |
|
1159 // Increase the current counter |
|
1160 p->ainew.cur_veh++; |
|
1161 // Decrease the total counter |
|
1162 p->ainew.amount_veh--; |
|
1163 // Go give some orders! |
|
1164 p->ainew.state = AI_STATE_WAIT_FOR_BUILD; |
|
1165 } |
|
1166 |
|
1167 |
|
1168 // Put the stations in the order list |
|
1169 static void AiNew_State_GiveOrders(Player *p) |
|
1170 { |
|
1171 int idx; |
|
1172 Order order; |
|
1173 |
|
1174 assert(p->ainew.state == AI_STATE_GIVE_ORDERS); |
|
1175 |
|
1176 if (p->ainew.veh_main_id != INVALID_VEHICLE) { |
|
1177 AI_DoCommand(0, p->ainew.veh_id + (p->ainew.veh_main_id << 16), 0, DC_EXEC, CMD_CLONE_ORDER); |
|
1178 |
|
1179 p->ainew.state = AI_STATE_START_VEHICLE; |
|
1180 return; |
|
1181 } else { |
|
1182 p->ainew.veh_main_id = p->ainew.veh_id; |
|
1183 } |
|
1184 |
|
1185 // Very handy for AI, goto depot.. but yeah, it needs to be activated ;) |
|
1186 if (_patches.gotodepot) { |
|
1187 idx = 0; |
|
1188 order.type = OT_GOTO_DEPOT; |
|
1189 order.flags = OF_UNLOAD; |
|
1190 order.dest = GetDepotByTile(p->ainew.depot_tile)->index; |
|
1191 AI_DoCommand(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER); |
|
1192 } |
|
1193 |
|
1194 idx = 0; |
|
1195 order.type = OT_GOTO_STATION; |
|
1196 order.flags = 0; |
|
1197 order.dest = GetStationIndex(p->ainew.to_tile); |
|
1198 if (p->ainew.tbt == AI_TRUCK && p->ainew.to_deliver) |
|
1199 order.flags |= OF_FULL_LOAD; |
|
1200 AI_DoCommand(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER); |
|
1201 |
|
1202 idx = 0; |
|
1203 order.type = OT_GOTO_STATION; |
|
1204 order.flags = 0; |
|
1205 order.dest = GetStationIndex(p->ainew.from_tile); |
|
1206 if (p->ainew.tbt == AI_TRUCK && p->ainew.from_deliver) |
|
1207 order.flags |= OF_FULL_LOAD; |
|
1208 AI_DoCommand(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER); |
|
1209 |
|
1210 // Start the engines! |
|
1211 p->ainew.state = AI_STATE_START_VEHICLE; |
|
1212 } |
|
1213 |
|
1214 |
|
1215 // Start the vehicle |
|
1216 static void AiNew_State_StartVehicle(Player *p) |
|
1217 { |
|
1218 assert(p->ainew.state == AI_STATE_START_VEHICLE); |
|
1219 |
|
1220 // Skip the first order if it is a second vehicle |
|
1221 // This to make vehicles go different ways.. |
|
1222 if (p->ainew.cur_veh & 1) |
|
1223 AI_DoCommand(0, p->ainew.veh_id, 0, DC_EXEC, CMD_SKIP_ORDER); |
|
1224 |
|
1225 // 3, 2, 1... go! (give START_STOP command ;)) |
|
1226 AI_DoCommand(0, p->ainew.veh_id, 0, DC_EXEC, CMD_START_STOP_ROADVEH); |
|
1227 // Try to build an other vehicle (that function will stop building when needed) |
|
1228 p->ainew.idle = 10; |
|
1229 p->ainew.state = AI_STATE_BUILD_VEHICLE; |
|
1230 } |
|
1231 |
|
1232 |
|
1233 // Repays money |
|
1234 static void AiNew_State_RepayMoney(Player *p) |
|
1235 { |
|
1236 uint i; |
|
1237 |
|
1238 for (i = 0; i < AI_LOAN_REPAY; i++) { |
|
1239 AI_DoCommand(0, 0, 0, DC_EXEC, CMD_DECREASE_LOAN); |
|
1240 } |
|
1241 p->ainew.state = AI_STATE_ACTION_DONE; |
|
1242 } |
|
1243 |
|
1244 |
|
1245 static void AiNew_CheckVehicle(Player *p, Vehicle *v) |
|
1246 { |
|
1247 // When a vehicle is under the 6 months, we don't check for anything |
|
1248 if (v->age < 180) return; |
|
1249 |
|
1250 // When a vehicle is older then 1 year, it should make money... |
|
1251 if (v->age > 360) { |
|
1252 // If both years together are not more than AI_MINIMUM_ROUTE_PROFIT, |
|
1253 // it is not worth the line I guess... |
|
1254 if (v->profit_last_year + v->profit_this_year < AI_MINIMUM_ROUTE_PROFIT || |
|
1255 (v->reliability * 100 >> 16) < 40) { |
|
1256 // There is a possibility that the route is fucked up... |
|
1257 if (v->cargo_days > AI_VEHICLE_LOST_DAYS) { |
|
1258 // The vehicle is lost.. check the route, or else, get the vehicle |
|
1259 // back to a depot |
|
1260 // TODO: make this piece of code |
|
1261 } |
|
1262 |
|
1263 |
|
1264 // We are already sending him back |
|
1265 if (AiNew_GetSpecialVehicleFlag(p, v) & AI_VEHICLEFLAG_SELL) { |
|
1266 if (v->type == VEH_Road && IsTileDepotType(v->tile, TRANSPORT_ROAD) && |
|
1267 (v->vehstatus&VS_STOPPED)) { |
|
1268 // We are at the depot, sell the vehicle |
|
1269 AI_DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH); |
|
1270 } |
|
1271 return; |
|
1272 } |
|
1273 |
|
1274 if (!AiNew_SetSpecialVehicleFlag(p, v, AI_VEHICLEFLAG_SELL)) return; |
|
1275 { |
|
1276 int ret = 0; |
|
1277 if (v->type == VEH_Road) |
|
1278 ret = AI_DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT); |
|
1279 // This means we can not find a depot :s |
|
1280 // if (CmdFailed(ret)) |
|
1281 } |
|
1282 } |
|
1283 } |
|
1284 } |
|
1285 |
|
1286 |
|
1287 // Checks all vehicles if they are still valid and make money and stuff |
|
1288 static void AiNew_State_CheckAllVehicles(Player *p) |
|
1289 { |
|
1290 Vehicle *v; |
|
1291 |
|
1292 FOR_ALL_VEHICLES(v) { |
|
1293 if (v->owner != p->index) continue; |
|
1294 // Currently, we only know how to handle road-vehicles |
|
1295 if (v->type != VEH_Road) continue; |
|
1296 |
|
1297 AiNew_CheckVehicle(p, v); |
|
1298 } |
|
1299 |
|
1300 p->ainew.state = AI_STATE_ACTION_DONE; |
|
1301 } |
|
1302 |
|
1303 |
|
1304 // Using the technique simular to the original AI |
|
1305 // Keeps things logical |
|
1306 // It really should be in the same order as the AI_STATE's are! |
|
1307 static AiNew_StateFunction* const _ainew_state[] = { |
|
1308 NULL, |
|
1309 AiNew_State_FirstTime, |
|
1310 AiNew_State_Nothing, |
|
1311 AiNew_State_WakeUp, |
|
1312 AiNew_State_LocateRoute, |
|
1313 AiNew_State_FindStation, |
|
1314 AiNew_State_FindPath, |
|
1315 AiNew_State_FindDepot, |
|
1316 AiNew_State_VerifyRoute, |
|
1317 AiNew_State_BuildStation, |
|
1318 AiNew_State_BuildPath, |
|
1319 AiNew_State_BuildDepot, |
|
1320 AiNew_State_BuildVehicle, |
|
1321 NULL, |
|
1322 AiNew_State_GiveOrders, |
|
1323 AiNew_State_StartVehicle, |
|
1324 AiNew_State_RepayMoney, |
|
1325 AiNew_State_CheckAllVehicles, |
|
1326 AiNew_State_ActionDone, |
|
1327 NULL, |
|
1328 }; |
|
1329 |
|
1330 static void AiNew_OnTick(Player *p) |
|
1331 { |
|
1332 if (_ainew_state[p->ainew.state] != NULL) |
|
1333 _ainew_state[p->ainew.state](p); |
|
1334 } |
|
1335 |
|
1336 |
|
1337 void AiNewDoGameLoop(Player *p) |
|
1338 { |
|
1339 if (p->ainew.state == AI_STATE_STARTUP) { |
|
1340 // The AI just got alive! |
|
1341 p->ainew.state = AI_STATE_FIRST_TIME; |
|
1342 p->ainew.tick = 0; |
|
1343 |
|
1344 // Only startup the AI |
|
1345 return; |
|
1346 } |
|
1347 |
|
1348 // We keep a ticker. We use it for competitor_speed |
|
1349 p->ainew.tick++; |
|
1350 |
|
1351 // If we come here, we can do a tick.. do so! |
|
1352 AiNew_OnTick(p); |
|
1353 } |