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