author | truelight |
Thu, 24 Mar 2005 14:43:40 +0000 | |
changeset 1540 | 6eef12dab5bf |
parent 1335 | a5f223b9f549 |
child 1562 | 20af59a8bc3b |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
679
04ca2cd69420
(svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
568
diff
changeset
|
3 |
#include "map.h" |
1209
2e00193652b2
(svn r1713) Split off several functions which query/set information about a single tile from map.h and put them into a seperate file tile.h
tron
parents:
1174
diff
changeset
|
4 |
#include "tile.h" |
0 | 5 |
#include "player.h" |
6 |
#include "vehicle.h" |
|
7 |
#include "engine.h" |
|
8 |
#include "command.h" |
|
9 |
#include "town.h" |
|
10 |
#include "industry.h" |
|
11 |
#include "station.h" |
|
12 |
#include "pathfind.h" |
|
13 |
#include "economy.h" |
|
14 |
#include "airport.h" |
|
1313
f1013ec3d318
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1245
diff
changeset
|
15 |
#include "depot.h" |
0 | 16 |
|
17 |
// remove some day perhaps? |
|
18 |
static Player *_cur_ai_player; |
|
192
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
19 |
static uint _ai_service_interval; |
0 | 20 |
|
21 |
typedef void AiStateAction(Player *p); |
|
22 |
||
23 |
enum { |
|
24 |
AIS_0 = 0, |
|
25 |
AIS_1 = 1, |
|
26 |
AIS_VEH_LOOP = 2, |
|
27 |
AIS_VEH_CHECK_REPLACE_VEHICLE = 3, |
|
28 |
AIS_VEH_DO_REPLACE_VEHICLE = 4, |
|
29 |
AIS_WANT_NEW_ROUTE = 5, |
|
30 |
AIS_BUILD_DEFAULT_RAIL_BLOCKS = 6, |
|
31 |
AIS_BUILD_RAIL = 7, |
|
32 |
AIS_BUILD_RAIL_VEH = 8, |
|
33 |
AIS_DELETE_RAIL_BLOCKS = 9, |
|
34 |
AIS_BUILD_DEFAULT_ROAD_BLOCKS = 10, |
|
35 |
AIS_BUILD_ROAD = 11, |
|
36 |
AIS_BUILD_ROAD_VEHICLES = 12, |
|
37 |
AIS_DELETE_ROAD_BLOCKS = 13, |
|
38 |
AIS_AIRPORT_STUFF = 14, |
|
39 |
AIS_BUILD_DEFAULT_AIRPORT_BLOCKS = 15, |
|
40 |
AIS_BUILD_AIRCRAFT_VEHICLES = 16, |
|
41 |
AIS_CHECK_SHIP_STUFF = 17, |
|
42 |
AIS_BUILD_DEFAULT_SHIP_BLOCKS = 18, |
|
43 |
AIS_DO_SHIP_STUFF = 19, |
|
44 |
AIS_SELL_VEHICLE = 20, |
|
45 |
AIS_REMOVE_STATION = 21, |
|
46 |
AIS_REMOVE_TRACK = 22, |
|
47 |
AIS_REMOVE_SINGLE_RAIL_TILE = 23 |
|
48 |
}; |
|
49 |
||
50 |
||
51 |
#include "table/ai_rail.h" |
|
52 |
||
53 |
static byte GetRailTrackStatus(TileIndex tile) { |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
76
diff
changeset
|
54 |
uint32 r = GetTileTrackStatus(tile, TRANSPORT_RAIL); |
0 | 55 |
return (byte) (r | r >> 8); |
56 |
} |
|
57 |
||
58 |
||
59 |
static void AiCase0(Player *p) |
|
60 |
{ |
|
61 |
p->ai.state = AIS_REMOVE_TRACK; |
|
62 |
p->ai.state_counter = 0; |
|
63 |
} |
|
64 |
||
65 |
static void AiCase1(Player *p) |
|
66 |
{ |
|
67 |
p->ai.cur_veh = NULL; |
|
68 |
p->ai.state = AIS_VEH_LOOP; |
|
69 |
} |
|
70 |
||
71 |
static void AiStateVehLoop(Player *p) |
|
72 |
{ |
|
73 |
Vehicle *v; |
|
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
74 |
uint index; |
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
75 |
|
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
76 |
index = (p->ai.cur_veh == NULL) ? 0 : p->ai.cur_veh->index + 1; |
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
77 |
|
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
78 |
FOR_ALL_VEHICLES_FROM(v, index) { |
0 | 79 |
if (v->type == 0 || v->owner != _current_player) |
80 |
continue; |
|
81 |
||
82 |
if ((v->type == VEH_Train && v->subtype==0) || |
|
83 |
v->type == VEH_Road || |
|
84 |
(v->type == VEH_Aircraft && v->subtype <= 2) || |
|
85 |
v->type == VEH_Ship) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
86 |
|
0 | 87 |
/* replace engine? */ |
88 |
if (v->type == VEH_Train && v->engine_type < 3 && |
|
89 |
(_price.build_railvehicle >> 3) < p->player_money) { |
|
90 |
p->ai.state = AIS_VEH_CHECK_REPLACE_VEHICLE; |
|
91 |
p->ai.cur_veh = v; |
|
92 |
return; |
|
93 |
} |
|
94 |
||
95 |
/* not profitable? */ |
|
96 |
if (v->age >= 730 && |
|
97 |
v->profit_last_year < _price.station_value*5 && |
|
98 |
v->profit_this_year < _price.station_value*5) { |
|
99 |
p->ai.state_counter = 0; |
|
100 |
p->ai.state = AIS_SELL_VEHICLE; |
|
101 |
p->ai.cur_veh = v; |
|
102 |
return; |
|
103 |
} |
|
104 |
||
105 |
/* not reliable? */ |
|
106 |
if ((v->age != 0 && |
|
107 |
_engines[v->engine_type].reliability < 35389) || |
|
108 |
v->age >= v->max_age) { |
|
109 |
p->ai.state = AIS_VEH_CHECK_REPLACE_VEHICLE; |
|
110 |
p->ai.cur_veh = v; |
|
111 |
return; |
|
112 |
} |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
p->ai.state = AIS_WANT_NEW_ROUTE; |
|
117 |
p->ai.state_counter = 0; |
|
118 |
} |
|
119 |
||
1223
228277b970ca
(svn r1727) - Fix: now CmdBuildRailVehicle() behaves correctly if DC_EXEC is set
bjarni
parents:
1217
diff
changeset
|
120 |
static int AiChooseTrainToBuild(byte railtype, int32 money, byte flag, TileIndex tile) |
0 | 121 |
{ |
122 |
int best_veh_index = -1; |
|
123 |
byte best_veh_score = 0; |
|
124 |
int32 r; |
|
460
7506c66ce4c3
(svn r677) -newgrf: Fix some custom electric trains appearing in maglev depots (pasky).
darkvater
parents:
410
diff
changeset
|
125 |
int i; |
7506c66ce4c3
(svn r677) -newgrf: Fix some custom electric trains appearing in maglev depots (pasky).
darkvater
parents:
410
diff
changeset
|
126 |
|
7506c66ce4c3
(svn r677) -newgrf: Fix some custom electric trains appearing in maglev depots (pasky).
darkvater
parents:
410
diff
changeset
|
127 |
for (i = 0; i < NUM_TRAIN_ENGINES; i++) { |
539 | 128 |
const RailVehicleInfo *rvi = RailVehInfo(i); |
460
7506c66ce4c3
(svn r677) -newgrf: Fix some custom electric trains appearing in maglev depots (pasky).
darkvater
parents:
410
diff
changeset
|
129 |
Engine *e = DEREF_ENGINE(i); |
7506c66ce4c3
(svn r677) -newgrf: Fix some custom electric trains appearing in maglev depots (pasky).
darkvater
parents:
410
diff
changeset
|
130 |
|
7506c66ce4c3
(svn r677) -newgrf: Fix some custom electric trains appearing in maglev depots (pasky).
darkvater
parents:
410
diff
changeset
|
131 |
if (e->railtype != railtype || rvi->flags & RVI_WAGON |
7506c66ce4c3
(svn r677) -newgrf: Fix some custom electric trains appearing in maglev depots (pasky).
darkvater
parents:
410
diff
changeset
|
132 |
|| !HASBIT(e->player_avail, _current_player) || e->reliability < 0x8A3D) |
0 | 133 |
continue; |
134 |
||
1223
228277b970ca
(svn r1727) - Fix: now CmdBuildRailVehicle() behaves correctly if DC_EXEC is set
bjarni
parents:
1217
diff
changeset
|
135 |
r = DoCommandByTile(tile, i, 0, 0, CMD_BUILD_RAIL_VEHICLE); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
136 |
if (r != CMD_ERROR && |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
137 |
(!(_cmd_build_rail_veh_var1&1) || !(flag&1)) && |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
138 |
r <= money && |
0 | 139 |
_cmd_build_rail_veh_score >= best_veh_score) { |
140 |
best_veh_score = _cmd_build_rail_veh_score; |
|
141 |
best_veh_index = i; |
|
142 |
} |
|
460
7506c66ce4c3
(svn r677) -newgrf: Fix some custom electric trains appearing in maglev depots (pasky).
darkvater
parents:
410
diff
changeset
|
143 |
} |
0 | 144 |
|
145 |
return best_veh_index; |
|
146 |
} |
|
147 |
||
1224
8046852a843f
(svn r1728) - Fix: now a player can only build a road vehicle in depots he owns (hacked client protection)
bjarni
parents:
1223
diff
changeset
|
148 |
static int AiChooseRoadVehToBuild(byte cargo, int32 money, TileIndex tile) |
0 | 149 |
{ |
150 |
int best_veh_index = -1; |
|
151 |
int32 best_veh_cost = 0; |
|
152 |
int32 r; |
|
153 |
||
154 |
int i = _cargoc.ai_roadveh_start[cargo]; |
|
155 |
int end = i + _cargoc.ai_roadveh_count[cargo]; |
|
156 |
Engine *e = &_engines[i]; |
|
157 |
do { |
|
158 |
if (!HASBIT(e->player_avail, _current_player) || e->reliability < 0x8A3D) |
|
159 |
continue; |
|
160 |
||
1224
8046852a843f
(svn r1728) - Fix: now a player can only build a road vehicle in depots he owns (hacked client protection)
bjarni
parents:
1223
diff
changeset
|
161 |
r = DoCommandByTile(tile, i, 0, 0, CMD_BUILD_ROAD_VEH); |
0 | 162 |
if (r != CMD_ERROR && r <= money && r >= best_veh_cost) { |
163 |
best_veh_cost = r; |
|
164 |
best_veh_index = i; |
|
165 |
} |
|
166 |
} while (++e, ++i != end); |
|
167 |
||
168 |
return best_veh_index; |
|
169 |
} |
|
170 |
||
171 |
static int AiChooseAircraftToBuild(int32 money, byte flag) |
|
172 |
{ |
|
173 |
int best_veh_index = -1; |
|
174 |
int32 best_veh_cost = 0; |
|
175 |
int32 r; |
|
176 |
||
177 |
int i = AIRCRAFT_ENGINES_INDEX; |
|
178 |
int end = i + NUM_AIRCRAFT_ENGINES; |
|
179 |
Engine *e = &_engines[i]; |
|
180 |
do { |
|
181 |
if (!HASBIT(e->player_avail, _current_player) || e->reliability < 0x8A3D) |
|
182 |
continue; |
|
183 |
||
184 |
if (flag&1) { |
|
185 |
if (i<253) continue; |
|
186 |
} else { |
|
187 |
if (i>=253) continue; |
|
188 |
} |
|
189 |
||
190 |
r = DoCommandByTile(0, i, 0, 0, CMD_BUILD_AIRCRAFT); |
|
191 |
if (r != CMD_ERROR && r <= money && r >= best_veh_cost) { |
|
192 |
best_veh_cost = r; |
|
193 |
best_veh_index = i; |
|
194 |
} |
|
195 |
} while (++e, ++i != end); |
|
196 |
||
197 |
return best_veh_index; |
|
198 |
} |
|
199 |
||
200 |
static int32 AiGetBasePrice(Player *p) |
|
201 |
{ |
|
202 |
int32 base = _price.station_value; |
|
203 |
||
204 |
// adjust base price when more expensive vehicles are available |
|
205 |
if (p->ai.railtype_to_use == 1) base = (base * 3) >> 1; |
|
206 |
else if (p->ai.railtype_to_use == 2) base *= 2; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
207 |
|
0 | 208 |
return base; |
209 |
} |
|
210 |
||
211 |
#if 0 |
|
212 |
static int AiChooseShipToBuild(byte cargo, int32 money) |
|
213 |
{ |
|
214 |
// XXX: not done |
|
215 |
return 0; |
|
216 |
} |
|
217 |
#endif |
|
218 |
||
219 |
static int AiChooseRoadVehToReplaceWith(Player *p, Vehicle *v) |
|
220 |
{ |
|
221 |
int32 avail_money = p->player_money + v->value; |
|
1224
8046852a843f
(svn r1728) - Fix: now a player can only build a road vehicle in depots he owns (hacked client protection)
bjarni
parents:
1223
diff
changeset
|
222 |
return AiChooseRoadVehToBuild(v->cargo_type, avail_money, v->tile); |
0 | 223 |
} |
224 |
||
225 |
static int AiChooseAircraftToReplaceWith(Player *p, Vehicle *v) |
|
226 |
{ |
|
227 |
int32 avail_money = p->player_money + v->value; |
|
228 |
return AiChooseAircraftToBuild(avail_money, v->engine_type>=253?1:0); |
|
229 |
} |
|
230 |
||
231 |
static int AiChooseTrainToReplaceWith(Player *p, Vehicle *v) |
|
232 |
{ |
|
233 |
int32 avail_money = p->player_money + v->value; |
|
234 |
int num=0; |
|
235 |
Vehicle *u = v; |
|
236 |
||
237 |
while (++num, u->next != NULL) { |
|
238 |
u = u->next; |
|
239 |
} |
|
240 |
||
241 |
// XXX: check if a wagon |
|
1223
228277b970ca
(svn r1727) - Fix: now CmdBuildRailVehicle() behaves correctly if DC_EXEC is set
bjarni
parents:
1217
diff
changeset
|
242 |
return AiChooseTrainToBuild(v->u.rail.railtype, avail_money, 0, v->tile); |
0 | 243 |
} |
244 |
||
245 |
static int AiChooseShipToReplaceWith(Player *p, Vehicle *v) |
|
246 |
{ |
|
247 |
error("!AiChooseShipToReplaceWith"); |
|
248 |
||
249 |
/* maybe useless, but avoids compiler warning this way */ |
|
250 |
return 0; |
|
251 |
} |
|
252 |
||
253 |
static void AiHandleGotoDepot(Player *p, int cmd) |
|
254 |
{ |
|
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
539
diff
changeset
|
255 |
if (p->ai.cur_veh->current_order.type != OT_GOTO_DEPOT) |
0 | 256 |
DoCommandByTile(0, p->ai.cur_veh->index, 0, DC_EXEC, cmd); |
257 |
||
258 |
if (++p->ai.state_counter <= 1387) { |
|
259 |
p->ai.state = AIS_VEH_DO_REPLACE_VEHICLE; |
|
260 |
return; |
|
261 |
} |
|
262 |
||
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
539
diff
changeset
|
263 |
if (p->ai.cur_veh->current_order.type == OT_GOTO_DEPOT) { |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
539
diff
changeset
|
264 |
p->ai.cur_veh->current_order.type = OT_DUMMY; |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
539
diff
changeset
|
265 |
p->ai.cur_veh->current_order.flags = 0; |
0 | 266 |
InvalidateWindow(WC_VEHICLE_VIEW, p->ai.cur_veh->index); |
267 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
268 |
} |
0 | 269 |
|
270 |
static void AiRestoreVehicleOrders(Vehicle *v, BackuppedOrders *bak) |
|
271 |
{ |
|
1141
d1ca5ddd5fc1
(svn r1642) -Fix: solve AI related order-problem
truelight
parents:
1093
diff
changeset
|
272 |
int i; |
d1ca5ddd5fc1
(svn r1642) -Fix: solve AI related order-problem
truelight
parents:
1093
diff
changeset
|
273 |
|
d1ca5ddd5fc1
(svn r1642) -Fix: solve AI related order-problem
truelight
parents:
1093
diff
changeset
|
274 |
for (i = 0; bak->order[i].type != OT_NOTHING; i++) |
d1ca5ddd5fc1
(svn r1642) -Fix: solve AI related order-problem
truelight
parents:
1093
diff
changeset
|
275 |
if (!DoCommandP(0, v->index + (i << 16), PackOrder(&bak->order[i]), NULL, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK)) |
0 | 276 |
break; |
277 |
} |
|
278 |
||
279 |
static void AiHandleReplaceTrain(Player *p) |
|
280 |
{ |
|
281 |
Vehicle *v = p->ai.cur_veh; |
|
282 |
BackuppedOrders orderbak[1]; |
|
283 |
int veh; |
|
284 |
uint tile; |
|
285 |
||
286 |
// wait until the vehicle reaches the depot. |
|
1330
5d76a0522a11
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
287 |
if (!IsTileDepotType(v->tile, TRANSPORT_RAIL) || v->u.rail.track != 0x80 || !(v->vehstatus&VS_STOPPED)) { |
0 | 288 |
AiHandleGotoDepot(p, CMD_TRAIN_GOTO_DEPOT); |
289 |
return; |
|
290 |
} |
|
291 |
||
292 |
veh = AiChooseTrainToReplaceWith(p, v); |
|
293 |
if (veh != -1) { |
|
294 |
BackupVehicleOrders(v, orderbak); |
|
295 |
tile = v->tile; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
296 |
|
0 | 297 |
if (DoCommandByTile(0, v->index, 2, DC_EXEC, CMD_SELL_RAIL_WAGON) != CMD_ERROR && |
298 |
DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE) != CMD_ERROR) { |
|
299 |
veh = _new_train_id; |
|
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
300 |
AiRestoreVehicleOrders(GetVehicle(veh), orderbak); |
0 | 301 |
DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_TRAIN); |
192
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
302 |
|
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
303 |
DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT); |
0 | 304 |
} |
305 |
} |
|
306 |
} |
|
307 |
||
308 |
static void AiHandleReplaceRoadVeh(Player *p) |
|
309 |
{ |
|
310 |
Vehicle *v = p->ai.cur_veh; |
|
311 |
BackuppedOrders orderbak[1]; |
|
312 |
int veh; |
|
313 |
uint tile; |
|
314 |
||
1330
5d76a0522a11
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
315 |
if (!IsTileDepotType(v->tile, TRANSPORT_ROAD) || v->u.road.state != 254 || !(v->vehstatus&VS_STOPPED)) { |
0 | 316 |
AiHandleGotoDepot(p, CMD_SEND_ROADVEH_TO_DEPOT); |
317 |
return; |
|
318 |
} |
|
319 |
||
320 |
veh = AiChooseRoadVehToReplaceWith(p, v); |
|
321 |
if (veh != -1) { |
|
322 |
BackupVehicleOrders(v, orderbak); |
|
323 |
tile = v->tile; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
324 |
|
0 | 325 |
if (DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH) != CMD_ERROR && |
326 |
DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH) != CMD_ERROR) { |
|
327 |
veh = _new_roadveh_id; |
|
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
328 |
AiRestoreVehicleOrders(GetVehicle(veh), orderbak); |
0 | 329 |
DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_ROADVEH); |
192
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
330 |
|
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
331 |
DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT); |
0 | 332 |
} |
333 |
} |
|
334 |
} |
|
335 |
||
336 |
static void AiHandleReplaceAircraft(Player *p) |
|
337 |
{ |
|
338 |
Vehicle *v = p->ai.cur_veh; |
|
339 |
int veh; |
|
340 |
BackuppedOrders orderbak[1]; |
|
341 |
uint tile; |
|
342 |
||
343 |
if (!IsAircraftHangarTile(v->tile) && !(v->vehstatus&VS_STOPPED)) { |
|
344 |
AiHandleGotoDepot(p, CMD_SEND_AIRCRAFT_TO_HANGAR); |
|
345 |
return; |
|
346 |
} |
|
347 |
||
348 |
veh = AiChooseAircraftToReplaceWith(p, v); |
|
349 |
if (veh != -1) { |
|
350 |
BackupVehicleOrders(v, orderbak); |
|
351 |
tile = v->tile; |
|
352 |
||
353 |
if (DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT) != CMD_ERROR && |
|
354 |
DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT) != CMD_ERROR) { |
|
355 |
veh = _new_aircraft_id; |
|
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
356 |
AiRestoreVehicleOrders(GetVehicle(veh), orderbak); |
0 | 357 |
DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_AIRCRAFT); |
192
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
358 |
|
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
359 |
DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT); |
0 | 360 |
} |
361 |
} |
|
362 |
} |
|
363 |
||
364 |
static void AiHandleReplaceShip(Player *p) |
|
365 |
{ |
|
366 |
error("!AiHandleReplaceShip"); |
|
367 |
} |
|
368 |
||
369 |
typedef int CheckReplaceProc(Player *p, Vehicle *v); |
|
370 |
||
371 |
static CheckReplaceProc * const _veh_check_replace_proc[] = { |
|
372 |
AiChooseTrainToReplaceWith, |
|
373 |
AiChooseRoadVehToReplaceWith, |
|
374 |
AiChooseShipToReplaceWith, |
|
375 |
AiChooseAircraftToReplaceWith, |
|
376 |
}; |
|
377 |
||
378 |
typedef void DoReplaceProc(Player *p); |
|
379 |
static DoReplaceProc * const _veh_do_replace_proc[] = { |
|
380 |
AiHandleReplaceTrain, |
|
381 |
AiHandleReplaceRoadVeh, |
|
382 |
AiHandleReplaceShip, |
|
383 |
AiHandleReplaceAircraft |
|
384 |
}; |
|
385 |
||
386 |
static void AiStateCheckReplaceVehicle(Player *p) |
|
387 |
{ |
|
388 |
Vehicle *v = p->ai.cur_veh; |
|
389 |
||
390 |
if (v->type == 0 || v->owner != _current_player || v->type > VEH_Ship || _veh_check_replace_proc[v->type - VEH_Train](p, v) == -1) { |
|
391 |
p->ai.state = AIS_VEH_LOOP; |
|
392 |
} else { |
|
393 |
p->ai.state_counter = 0; |
|
394 |
p->ai.state = AIS_VEH_DO_REPLACE_VEHICLE; |
|
395 |
} |
|
396 |
} |
|
397 |
||
398 |
static void AiStateDoReplaceVehicle(Player *p) |
|
399 |
{ |
|
400 |
Vehicle *v = p->ai.cur_veh; |
|
401 |
p->ai.state = AIS_VEH_LOOP; |
|
402 |
// vehicle is not owned by the player anymore, something went very wrong. |
|
403 |
if (v->type == 0 || v->owner != _current_player) |
|
404 |
return; |
|
405 |
_veh_do_replace_proc[v->type - VEH_Train](p); |
|
406 |
} |
|
407 |
||
408 |
typedef struct FoundRoute { |
|
409 |
int distance; |
|
410 |
byte cargo; |
|
411 |
void *from; |
|
412 |
void *to; |
|
413 |
} FoundRoute; |
|
414 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1043
diff
changeset
|
415 |
static Town *AiFindRandomTown(void) |
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1043
diff
changeset
|
416 |
{ |
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
417 |
Town *t = GetTown(RandomRange(_total_towns)); |
0 | 418 |
return (t->xy != 0) ? t : NULL; |
419 |
} |
|
420 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1043
diff
changeset
|
421 |
static Industry *AiFindRandomIndustry(void) |
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1043
diff
changeset
|
422 |
{ |
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
423 |
Industry *i = GetIndustry(RandomRange(_total_industries)); |
0 | 424 |
return (i->xy != 0) ? i : NULL; |
425 |
} |
|
426 |
||
427 |
static void AiFindSubsidyIndustryRoute(FoundRoute *fr) |
|
428 |
{ |
|
959
e6a3bbda610f
(svn r1451) Fix some of the signed/unsigned comparison warnings
tron
parents:
926
diff
changeset
|
429 |
uint i; |
0 | 430 |
byte cargo; |
431 |
Subsidy *s; |
|
432 |
Industry *from, *to_ind; |
|
433 |
Town *to_tow; |
|
434 |
TileIndex to_xy; |
|
435 |
||
436 |
// initially error |
|
437 |
fr->distance = -1; |
|
438 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
439 |
// Randomize subsidy index.. |
0 | 440 |
i = RandomRange(lengthof(_subsidies) * 3); |
441 |
if (i >= lengthof(_subsidies)) |
|
442 |
return; |
|
443 |
||
444 |
s = &_subsidies[i]; |
|
445 |
||
446 |
// Don't want passengers or mail |
|
447 |
cargo = s->cargo_type; |
|
448 |
if (cargo == 0xFF || cargo == CT_PASSENGERS || cargo == CT_MAIL || s->age > 7) |
|
449 |
return; |
|
450 |
fr->cargo = cargo; |
|
451 |
||
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
452 |
fr->from = from = GetIndustry(s->from); |
0 | 453 |
|
454 |
if (cargo == CT_GOODS || cargo == CT_FOOD) { |
|
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
455 |
to_tow = GetTown(s->to); |
0 | 456 |
if (to_tow->population < (uint32)(cargo == CT_FOOD ? 200 : 900)) |
457 |
return; // error |
|
458 |
fr->to = to_tow; |
|
459 |
to_xy = to_tow->xy; |
|
460 |
} else { |
|
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
461 |
to_ind = GetIndustry(s->to); |
0 | 462 |
fr->to = to_ind; |
463 |
to_xy = to_ind->xy; |
|
464 |
} |
|
465 |
||
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
466 |
fr->distance = DistanceManhattan(from->xy, to_xy); |
0 | 467 |
} |
468 |
||
469 |
static void AiFindSubsidyPassengerRoute(FoundRoute *fr) |
|
470 |
{ |
|
471 |
uint i; |
|
472 |
Subsidy *s; |
|
473 |
Town *from,*to; |
|
474 |
||
475 |
// initially error |
|
476 |
fr->distance = -1; |
|
477 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
478 |
// Randomize subsidy index.. |
0 | 479 |
i = RandomRange(lengthof(_subsidies) * 3); |
480 |
if (i >= lengthof(_subsidies)) |
|
481 |
return; |
|
482 |
||
483 |
s = &_subsidies[i]; |
|
484 |
||
485 |
// Only want passengers |
|
486 |
if (s->cargo_type != CT_PASSENGERS || s->age > 7) |
|
487 |
return; |
|
488 |
fr->cargo = s->cargo_type; |
|
489 |
||
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
490 |
fr->from = from = GetTown(s->from); |
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
491 |
fr->to = to = GetTown(s->to); |
0 | 492 |
|
493 |
// They must be big enough |
|
494 |
if (from->population < 400 || to->population < 400) |
|
495 |
return; |
|
496 |
||
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
497 |
fr->distance = DistanceManhattan(from->xy, to->xy); |
0 | 498 |
} |
499 |
||
500 |
static void AiFindRandomIndustryRoute(FoundRoute *fr) |
|
501 |
{ |
|
502 |
Industry *i,*i2; |
|
503 |
Town *t; |
|
504 |
uint32 r; |
|
505 |
byte cargo; |
|
506 |
||
507 |
// initially error |
|
508 |
fr->distance = -1; |
|
509 |
||
510 |
r = Random(); |
|
511 |
||
512 |
// pick a source |
|
513 |
fr->from = i = AiFindRandomIndustry(); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
514 |
if (i == NULL) |
0 | 515 |
return; |
516 |
||
517 |
// pick a random produced cargo |
|
518 |
cargo = i->produced_cargo[0]; |
|
519 |
if (r&1 && i->produced_cargo[1] != 0xFF) |
|
520 |
cargo = i->produced_cargo[1]; |
|
521 |
||
522 |
fr->cargo = cargo; |
|
523 |
||
524 |
// don't allow passengers |
|
525 |
if (cargo == 0xFF || cargo == CT_PASSENGERS) |
|
526 |
return; |
|
527 |
||
528 |
if (cargo != CT_GOODS && cargo != CT_FOOD) { |
|
529 |
// pick a dest, and see if it can receive |
|
530 |
i2 = AiFindRandomIndustry(); |
|
531 |
if (i2 == NULL || i == i2 || !(i2->accepts_cargo[0] == cargo || i2->accepts_cargo[1] == cargo || i2->accepts_cargo[2] == cargo)) |
|
532 |
return; |
|
533 |
||
534 |
fr->to = i2; |
|
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
535 |
fr->distance = DistanceManhattan(i->xy, i2->xy); |
0 | 536 |
} else { |
537 |
// pick a dest town, and see if it's big enough |
|
538 |
t = AiFindRandomTown(); |
|
539 |
if (t == NULL || t->population < (uint32)(cargo == CT_FOOD ? 200 : 900)) |
|
540 |
return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
541 |
|
0 | 542 |
fr->to = t; |
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
543 |
fr->distance = DistanceManhattan(i->xy, t->xy); |
0 | 544 |
} |
545 |
} |
|
546 |
||
547 |
static void AiFindRandomPassengerRoute(FoundRoute *fr) |
|
548 |
{ |
|
549 |
uint32 r; |
|
550 |
Town *source, *dest; |
|
551 |
||
552 |
// initially error |
|
553 |
fr->distance = -1; |
|
554 |
||
555 |
r = Random(); |
|
556 |
||
557 |
fr->from = source = AiFindRandomTown(); |
|
558 |
if (source == NULL || source->population < 400) |
|
559 |
return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
560 |
|
0 | 561 |
fr->to = dest = AiFindRandomTown(); |
562 |
if (dest == NULL || source == dest || dest->population < 400) |
|
563 |
return; |
|
564 |
||
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
565 |
fr->distance = DistanceManhattan(source->xy, dest->xy); |
0 | 566 |
} |
567 |
||
568 |
// Warn: depends on 'xy' being the first element in both Town and Industry |
|
569 |
#define GET_TOWN_OR_INDUSTRY_TILE(p) (((Town*)(p))->xy) |
|
570 |
||
571 |
static bool AiCheckIfRouteIsGood(Player *p, FoundRoute *fr, byte bitmask) |
|
572 |
{ |
|
573 |
TileIndex from_tile, to_tile; |
|
574 |
Station *st; |
|
575 |
int dist, cur; |
|
576 |
uint same_station = 0; |
|
577 |
||
578 |
// Make sure distance to closest station is < 37 pixels. |
|
579 |
from_tile = GET_TOWN_OR_INDUSTRY_TILE(fr->from); |
|
580 |
to_tile = GET_TOWN_OR_INDUSTRY_TILE(fr->to); |
|
581 |
||
582 |
dist = 0xFFFF; |
|
583 |
FOR_ALL_STATIONS(st) if (st->xy != 0 && st->owner == _current_player) { |
|
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
584 |
cur = DistanceMax(from_tile, st->xy); |
0 | 585 |
if (cur < dist) dist = cur; |
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
586 |
cur = DistanceMax(to_tile, st->xy); |
0 | 587 |
if (cur < dist) dist = cur; |
588 |
if (to_tile == from_tile && st->xy == to_tile) |
|
589 |
same_station++; |
|
590 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
591 |
|
0 | 592 |
// To prevent the AI from building ten busstations in the same town, do some calculations |
593 |
// For each road or airport station, we want 350 of population! |
|
594 |
if ((bitmask == 2 || bitmask == 4) && same_station > 2 && ((Town *)(fr->from))->population < same_station * 350) |
|
595 |
return false; |
|
596 |
||
597 |
if (dist != 0xFFFF && dist > 37) |
|
598 |
return false; |
|
599 |
||
600 |
if (p->ai.route_type_mask != 0 && !(p->ai.route_type_mask&bitmask) && !CHANCE16(1,5)) |
|
601 |
return false; |
|
602 |
||
603 |
if (fr->cargo == CT_PASSENGERS || fr->cargo == CT_MAIL) { |
|
604 |
if (((Town*)fr->from)->pct_pass_transported > 0x99 || |
|
605 |
((Town*)fr->to)->pct_pass_transported > 0x99) |
|
606 |
return false; |
|
607 |
||
608 |
// Make sure it has a reasonably good rating |
|
609 |
if ( ((Town*)fr->from)->ratings[_current_player] < -100 || |
|
610 |
((Town*)fr->to)->ratings[_current_player] < -100) |
|
611 |
return false; |
|
612 |
} else { |
|
613 |
Industry *i = (Industry*)fr->from; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
614 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
615 |
if (i->pct_transported[fr->cargo != i->produced_cargo[0]] > 0x99 || |
0 | 616 |
i->total_production[fr->cargo != i->produced_cargo[0]] == 0) |
617 |
return false; |
|
618 |
} |
|
619 |
||
620 |
p->ai.route_type_mask |= bitmask; |
|
621 |
return true; |
|
622 |
} |
|
623 |
||
624 |
static byte AiGetDirectionBetweenTiles(TileIndex a, TileIndex b) |
|
625 |
{ |
|
926
a6d140a6a4de
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
626 |
byte i = (TileX(a) < TileX(b)) ? 1 : 0; |
a6d140a6a4de
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
627 |
if (TileY(a) >= TileY(b)) i ^= 3; |
0 | 628 |
return i; |
629 |
} |
|
630 |
||
631 |
static TileIndex AiGetPctTileBetween(TileIndex a, TileIndex b, byte pct) |
|
632 |
{ |
|
633 |
return TILE_XY( |
|
926
a6d140a6a4de
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
634 |
TileX(a) + ((TileX(b) - TileX(a)) * pct >> 8), |
a6d140a6a4de
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
635 |
TileY(a) + ((TileY(b) - TileY(a)) * pct >> 8) |
a6d140a6a4de
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
636 |
); |
0 | 637 |
} |
638 |
||
639 |
static void AiWantLongIndustryRoute(Player *p) |
|
640 |
{ |
|
641 |
int i; |
|
642 |
FoundRoute fr; |
|
643 |
||
644 |
i = 60; |
|
645 |
for(;;) { |
|
646 |
// look for one from the subsidy list |
|
647 |
AiFindSubsidyIndustryRoute(&fr); |
|
648 |
if (IS_INT_INSIDE(fr.distance, 60, 90+1)) |
|
649 |
break; |
|
650 |
||
651 |
// try a random one |
|
652 |
AiFindRandomIndustryRoute(&fr); |
|
653 |
if (IS_INT_INSIDE(fr.distance, 60, 90+1)) |
|
654 |
break; |
|
655 |
||
656 |
// only test 60 times |
|
657 |
if (--i == 0) |
|
658 |
return; |
|
659 |
} |
|
660 |
||
661 |
if (!AiCheckIfRouteIsGood(p, &fr, 1)) |
|
662 |
return; |
|
663 |
||
664 |
// Fill the source field |
|
665 |
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
|
666 |
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
|
667 |
||
668 |
p->ai.src.use_tile = 0; |
|
669 |
p->ai.src.rand_rng = 9; |
|
670 |
p->ai.src.cur_building_rule = 0xFF; |
|
671 |
p->ai.src.unk6 = 1; |
|
672 |
p->ai.src.unk7 = 0; |
|
673 |
p->ai.src.buildcmd_a = 0x24; |
|
674 |
p->ai.src.buildcmd_b = 0xFF; |
|
675 |
p->ai.src.direction = AiGetDirectionBetweenTiles( |
|
676 |
p->ai.src.spec_tile, |
|
677 |
p->ai.dst.spec_tile |
|
678 |
); |
|
679 |
p->ai.src.cargo = fr.cargo | 0x80; |
|
680 |
||
681 |
// Fill the dest field |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
682 |
|
0 | 683 |
p->ai.dst.use_tile = 0; |
684 |
p->ai.dst.rand_rng = 9; |
|
685 |
p->ai.dst.cur_building_rule = 0xFF; |
|
686 |
p->ai.dst.unk6 = 1; |
|
687 |
p->ai.dst.unk7 = 0; |
|
688 |
p->ai.dst.buildcmd_a = 0x34; |
|
689 |
p->ai.dst.buildcmd_b = 0xFF; |
|
690 |
p->ai.dst.direction = AiGetDirectionBetweenTiles( |
|
691 |
p->ai.dst.spec_tile, |
|
692 |
p->ai.src.spec_tile |
|
693 |
); |
|
694 |
p->ai.dst.cargo = fr.cargo; |
|
695 |
||
696 |
// Fill middle field 1 |
|
697 |
p->ai.mid1.spec_tile = AiGetPctTileBetween( |
|
698 |
p->ai.src.spec_tile, |
|
699 |
p->ai.dst.spec_tile, |
|
700 |
0x55 |
|
701 |
); |
|
702 |
p->ai.mid1.use_tile = 0; |
|
703 |
p->ai.mid1.rand_rng = 6; |
|
704 |
p->ai.mid1.cur_building_rule = 0xFF; |
|
705 |
p->ai.mid1.unk6 = 2; |
|
706 |
p->ai.mid1.unk7 = 1; |
|
707 |
p->ai.mid1.buildcmd_a = 0x30; |
|
708 |
p->ai.mid1.buildcmd_b = 0xFF; |
|
709 |
p->ai.mid1.direction = p->ai.src.direction; |
|
710 |
p->ai.mid1.cargo = fr.cargo; |
|
711 |
||
712 |
// Fill middle field 2 |
|
713 |
p->ai.mid2.spec_tile = AiGetPctTileBetween( |
|
714 |
p->ai.src.spec_tile, |
|
715 |
p->ai.dst.spec_tile, |
|
716 |
0xAA |
|
717 |
); |
|
718 |
p->ai.mid2.use_tile = 0; |
|
719 |
p->ai.mid2.rand_rng = 6; |
|
720 |
p->ai.mid2.cur_building_rule = 0xFF; |
|
721 |
p->ai.mid2.unk6 = 2; |
|
722 |
p->ai.mid2.unk7 = 1; |
|
723 |
p->ai.mid2.buildcmd_a = 0xFF; |
|
724 |
p->ai.mid2.buildcmd_b = 0xFF; |
|
725 |
p->ai.mid2.direction = p->ai.dst.direction; |
|
726 |
p->ai.mid2.cargo = fr.cargo; |
|
727 |
||
728 |
// Fill common fields |
|
729 |
p->ai.cargo_type = fr.cargo; |
|
730 |
p->ai.num_wagons = 3; |
|
731 |
p->ai.build_kind = 2; |
|
732 |
p->ai.num_build_rec = 4; |
|
733 |
p->ai.num_loco_to_build = 2; |
|
734 |
p->ai.num_want_fullload = 2; |
|
735 |
p->ai.wagon_list[0] = INVALID_VEHICLE; |
|
736 |
p->ai.order_list_blocks[0] = 0; |
|
737 |
p->ai.order_list_blocks[1] = 1; |
|
738 |
p->ai.order_list_blocks[2] = 255; |
|
739 |
||
740 |
p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS; |
|
741 |
p->ai.state_mode = -1; |
|
742 |
p->ai.state_counter = 0; |
|
743 |
p->ai.timeout_counter = 0; |
|
744 |
} |
|
745 |
||
746 |
static void AiWantMediumIndustryRoute(Player *p) |
|
747 |
{ |
|
748 |
int i; |
|
749 |
FoundRoute fr; |
|
750 |
||
751 |
i = 60; |
|
752 |
for(;;) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
753 |
|
0 | 754 |
// look for one from the subsidy list |
755 |
AiFindSubsidyIndustryRoute(&fr); |
|
756 |
if (IS_INT_INSIDE(fr.distance, 40, 60+1)) |
|
757 |
break; |
|
758 |
||
759 |
// try a random one |
|
760 |
AiFindRandomIndustryRoute(&fr); |
|
761 |
if (IS_INT_INSIDE(fr.distance, 40, 60+1)) |
|
762 |
break; |
|
763 |
||
764 |
// only test 60 times |
|
765 |
if (--i == 0) |
|
766 |
return; |
|
767 |
} |
|
768 |
||
769 |
if (!AiCheckIfRouteIsGood(p, &fr, 1)) |
|
770 |
return; |
|
771 |
||
772 |
// Fill the source field |
|
773 |
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
|
774 |
p->ai.src.use_tile = 0; |
|
775 |
p->ai.src.rand_rng = 9; |
|
776 |
p->ai.src.cur_building_rule = 0xFF; |
|
777 |
p->ai.src.unk6 = 1; |
|
778 |
p->ai.src.unk7 = 0; |
|
779 |
p->ai.src.buildcmd_a = 0x10; |
|
780 |
p->ai.src.buildcmd_b = 0xFF; |
|
781 |
p->ai.src.direction = AiGetDirectionBetweenTiles( |
|
782 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from), |
|
783 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to) |
|
784 |
); |
|
785 |
p->ai.src.cargo = fr.cargo | 0x80; |
|
786 |
||
787 |
// Fill the dest field |
|
788 |
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
|
789 |
p->ai.dst.use_tile = 0; |
|
790 |
p->ai.dst.rand_rng = 9; |
|
791 |
p->ai.dst.cur_building_rule = 0xFF; |
|
792 |
p->ai.dst.unk6 = 1; |
|
793 |
p->ai.dst.unk7 = 0; |
|
794 |
p->ai.dst.buildcmd_a = 0xFF; |
|
795 |
p->ai.dst.buildcmd_b = 0xFF; |
|
796 |
p->ai.dst.direction = AiGetDirectionBetweenTiles( |
|
797 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to), |
|
798 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from) |
|
799 |
); |
|
800 |
p->ai.dst.cargo = fr.cargo; |
|
801 |
||
802 |
// Fill common fields |
|
803 |
p->ai.cargo_type = fr.cargo; |
|
804 |
p->ai.num_wagons = 3; |
|
805 |
p->ai.build_kind = 1; |
|
806 |
p->ai.num_build_rec = 2; |
|
807 |
p->ai.num_loco_to_build = 1; |
|
808 |
p->ai.num_want_fullload = 1; |
|
809 |
p->ai.wagon_list[0] = INVALID_VEHICLE; |
|
810 |
p->ai.order_list_blocks[0] = 0; |
|
811 |
p->ai.order_list_blocks[1] = 1; |
|
812 |
p->ai.order_list_blocks[2] = 255; |
|
813 |
p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS; |
|
814 |
p->ai.state_mode = -1; |
|
815 |
p->ai.state_counter = 0; |
|
816 |
p->ai.timeout_counter = 0; |
|
817 |
} |
|
818 |
||
819 |
static void AiWantShortIndustryRoute(Player *p) |
|
820 |
{ |
|
821 |
int i; |
|
822 |
FoundRoute fr; |
|
823 |
||
824 |
i = 60; |
|
825 |
for(;;) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
826 |
|
0 | 827 |
// look for one from the subsidy list |
828 |
AiFindSubsidyIndustryRoute(&fr); |
|
829 |
if (IS_INT_INSIDE(fr.distance, 15, 40+1)) |
|
830 |
break; |
|
831 |
||
832 |
// try a random one |
|
833 |
AiFindRandomIndustryRoute(&fr); |
|
834 |
if (IS_INT_INSIDE(fr.distance, 15, 40+1)) |
|
835 |
break; |
|
836 |
||
837 |
// only test 60 times |
|
838 |
if (--i == 0) |
|
839 |
return; |
|
840 |
} |
|
841 |
||
842 |
if (!AiCheckIfRouteIsGood(p, &fr, 1)) |
|
843 |
return; |
|
844 |
||
845 |
// Fill the source field |
|
846 |
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
|
847 |
p->ai.src.use_tile = 0; |
|
848 |
p->ai.src.rand_rng = 9; |
|
849 |
p->ai.src.cur_building_rule = 0xFF; |
|
850 |
p->ai.src.unk6 = 1; |
|
851 |
p->ai.src.unk7 = 0; |
|
852 |
p->ai.src.buildcmd_a = 0x10; |
|
853 |
p->ai.src.buildcmd_b = 0xFF; |
|
854 |
p->ai.src.direction = AiGetDirectionBetweenTiles( |
|
855 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from), |
|
856 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to) |
|
857 |
); |
|
858 |
p->ai.src.cargo = fr.cargo | 0x80; |
|
859 |
||
860 |
// Fill the dest field |
|
861 |
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
|
862 |
p->ai.dst.use_tile = 0; |
|
863 |
p->ai.dst.rand_rng = 9; |
|
864 |
p->ai.dst.cur_building_rule = 0xFF; |
|
865 |
p->ai.dst.unk6 = 1; |
|
866 |
p->ai.dst.unk7 = 0; |
|
867 |
p->ai.dst.buildcmd_a = 0xFF; |
|
868 |
p->ai.dst.buildcmd_b = 0xFF; |
|
869 |
p->ai.dst.direction = AiGetDirectionBetweenTiles( |
|
870 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to), |
|
871 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from) |
|
872 |
); |
|
873 |
p->ai.dst.cargo = fr.cargo; |
|
874 |
||
875 |
// Fill common fields |
|
876 |
p->ai.cargo_type = fr.cargo; |
|
877 |
p->ai.num_wagons = 2; |
|
878 |
p->ai.build_kind = 1; |
|
879 |
p->ai.num_build_rec = 2; |
|
880 |
p->ai.num_loco_to_build = 1; |
|
881 |
p->ai.num_want_fullload = 1; |
|
882 |
p->ai.wagon_list[0] = INVALID_VEHICLE; |
|
883 |
p->ai.order_list_blocks[0] = 0; |
|
884 |
p->ai.order_list_blocks[1] = 1; |
|
885 |
p->ai.order_list_blocks[2] = 255; |
|
886 |
p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS; |
|
887 |
p->ai.state_mode = -1; |
|
888 |
p->ai.state_counter = 0; |
|
889 |
p->ai.timeout_counter = 0; |
|
890 |
} |
|
891 |
||
892 |
static void AiWantMailRoute(Player *p) |
|
893 |
{ |
|
894 |
int i; |
|
895 |
FoundRoute fr; |
|
896 |
||
897 |
i = 60; |
|
898 |
for(;;) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
899 |
|
0 | 900 |
// look for one from the subsidy list |
901 |
AiFindSubsidyPassengerRoute(&fr); |
|
902 |
if (IS_INT_INSIDE(fr.distance, 60, 110+1)) |
|
903 |
break; |
|
904 |
||
905 |
// try a random one |
|
906 |
AiFindRandomPassengerRoute(&fr); |
|
907 |
if (IS_INT_INSIDE(fr.distance, 60, 110+1)) |
|
908 |
break; |
|
909 |
||
910 |
// only test 60 times |
|
911 |
if (--i == 0) |
|
912 |
return; |
|
913 |
} |
|
914 |
||
915 |
fr.cargo = CT_MAIL; |
|
916 |
if (!AiCheckIfRouteIsGood(p, &fr, 1)) |
|
917 |
return; |
|
918 |
||
919 |
// Fill the source field |
|
920 |
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
|
921 |
p->ai.src.use_tile = 0; |
|
922 |
p->ai.src.rand_rng = 7; |
|
923 |
p->ai.src.cur_building_rule = 0xFF; |
|
924 |
p->ai.src.unk6 = 1; |
|
925 |
p->ai.src.unk7 = 0; |
|
926 |
p->ai.src.buildcmd_a = 0x24; |
|
927 |
p->ai.src.buildcmd_b = 0xFF; |
|
928 |
p->ai.src.direction = AiGetDirectionBetweenTiles( |
|
929 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from), |
|
930 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to) |
|
931 |
); |
|
932 |
p->ai.src.cargo = fr.cargo; |
|
933 |
||
934 |
// Fill the dest field |
|
935 |
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
|
936 |
p->ai.dst.use_tile = 0; |
|
937 |
p->ai.dst.rand_rng = 7; |
|
938 |
p->ai.dst.cur_building_rule = 0xFF; |
|
939 |
p->ai.dst.unk6 = 1; |
|
940 |
p->ai.dst.unk7 = 0; |
|
941 |
p->ai.dst.buildcmd_a = 0x34; |
|
942 |
p->ai.dst.buildcmd_b = 0xFF; |
|
943 |
p->ai.dst.direction = AiGetDirectionBetweenTiles( |
|
944 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to), |
|
945 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from) |
|
946 |
); |
|
947 |
p->ai.dst.cargo = fr.cargo; |
|
948 |
||
949 |
// Fill middle field 1 |
|
950 |
p->ai.mid1.spec_tile = AiGetPctTileBetween( |
|
951 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from), |
|
952 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to), |
|
953 |
0x55 |
|
954 |
); |
|
955 |
p->ai.mid1.use_tile = 0; |
|
956 |
p->ai.mid1.rand_rng = 6; |
|
957 |
p->ai.mid1.cur_building_rule = 0xFF; |
|
958 |
p->ai.mid1.unk6 = 2; |
|
959 |
p->ai.mid1.unk7 = 1; |
|
960 |
p->ai.mid1.buildcmd_a = 0x30; |
|
961 |
p->ai.mid1.buildcmd_b = 0xFF; |
|
962 |
p->ai.mid1.direction = p->ai.src.direction; |
|
963 |
p->ai.mid1.cargo = fr.cargo; |
|
964 |
||
965 |
// Fill middle field 2 |
|
966 |
p->ai.mid2.spec_tile = AiGetPctTileBetween( |
|
967 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from), |
|
968 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to), |
|
969 |
0xAA |
|
970 |
); |
|
971 |
p->ai.mid2.use_tile = 0; |
|
972 |
p->ai.mid2.rand_rng = 6; |
|
973 |
p->ai.mid2.cur_building_rule = 0xFF; |
|
974 |
p->ai.mid2.unk6 = 2; |
|
975 |
p->ai.mid2.unk7 = 1; |
|
976 |
p->ai.mid2.buildcmd_a = 0xFF; |
|
977 |
p->ai.mid2.buildcmd_b = 0xFF; |
|
978 |
p->ai.mid2.direction = p->ai.dst.direction; |
|
979 |
p->ai.mid2.cargo = fr.cargo; |
|
980 |
||
981 |
// Fill common fields |
|
982 |
p->ai.cargo_type = fr.cargo; |
|
983 |
p->ai.num_wagons = 3; |
|
984 |
p->ai.build_kind = 2; |
|
985 |
p->ai.num_build_rec = 4; |
|
986 |
p->ai.num_loco_to_build = 2; |
|
987 |
p->ai.num_want_fullload = 0; |
|
988 |
p->ai.wagon_list[0] = INVALID_VEHICLE; |
|
989 |
p->ai.order_list_blocks[0] = 0; |
|
990 |
p->ai.order_list_blocks[1] = 1; |
|
991 |
p->ai.order_list_blocks[2] = 255; |
|
992 |
p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS; |
|
993 |
p->ai.state_mode = -1; |
|
994 |
p->ai.state_counter = 0; |
|
995 |
p->ai.timeout_counter = 0; |
|
996 |
} |
|
997 |
||
998 |
static void AiWantPassengerRoute(Player *p) |
|
999 |
{ |
|
1000 |
int i; |
|
1001 |
FoundRoute fr; |
|
1002 |
||
1003 |
i = 60; |
|
1004 |
for(;;) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1005 |
|
0 | 1006 |
// look for one from the subsidy list |
1007 |
AiFindSubsidyPassengerRoute(&fr); |
|
1008 |
if (IS_INT_INSIDE(fr.distance, 0, 55+1)) |
|
1009 |
break; |
|
1010 |
||
1011 |
// try a random one |
|
1012 |
AiFindRandomPassengerRoute(&fr); |
|
1013 |
if (IS_INT_INSIDE(fr.distance, 0, 55+1)) |
|
1014 |
break; |
|
1015 |
||
1016 |
// only test 60 times |
|
1017 |
if (--i == 0) |
|
1018 |
return; |
|
1019 |
} |
|
1020 |
||
1021 |
fr.cargo = CT_PASSENGERS; |
|
1022 |
if (!AiCheckIfRouteIsGood(p, &fr, 1)) |
|
1023 |
return; |
|
1024 |
||
1025 |
// Fill the source field |
|
1026 |
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
|
1027 |
p->ai.src.use_tile = 0; |
|
1028 |
p->ai.src.rand_rng = 7; |
|
1029 |
p->ai.src.cur_building_rule = 0xFF; |
|
1030 |
p->ai.src.unk6 = 1; |
|
1031 |
p->ai.src.unk7 = 0; |
|
1032 |
p->ai.src.buildcmd_a = 0x10; |
|
1033 |
p->ai.src.buildcmd_b = 0xFF; |
|
1034 |
p->ai.src.direction = AiGetDirectionBetweenTiles( |
|
1035 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from), |
|
1036 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to) |
|
1037 |
); |
|
1038 |
p->ai.src.cargo = fr.cargo; |
|
1039 |
||
1040 |
// Fill the dest field |
|
1041 |
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
|
1042 |
p->ai.dst.use_tile = 0; |
|
1043 |
p->ai.dst.rand_rng = 7; |
|
1044 |
p->ai.dst.cur_building_rule = 0xFF; |
|
1045 |
p->ai.dst.unk6 = 1; |
|
1046 |
p->ai.dst.unk7 = 0; |
|
1047 |
p->ai.dst.buildcmd_a = 0xFF; |
|
1048 |
p->ai.dst.buildcmd_b = 0xFF; |
|
1049 |
p->ai.dst.direction = AiGetDirectionBetweenTiles( |
|
1050 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to), |
|
1051 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from) |
|
1052 |
); |
|
1053 |
p->ai.dst.cargo = fr.cargo; |
|
1054 |
||
1055 |
// Fill common fields |
|
1056 |
p->ai.cargo_type = fr.cargo; |
|
1057 |
p->ai.num_wagons = 2; |
|
1058 |
p->ai.build_kind = 1; |
|
1059 |
p->ai.num_build_rec = 2; |
|
1060 |
p->ai.num_loco_to_build = 1; |
|
1061 |
p->ai.num_want_fullload = 0; |
|
1062 |
p->ai.wagon_list[0] = INVALID_VEHICLE; |
|
1063 |
p->ai.order_list_blocks[0] = 0; |
|
1064 |
p->ai.order_list_blocks[1] = 1; |
|
1065 |
p->ai.order_list_blocks[2] = 255; |
|
1066 |
p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS; |
|
1067 |
p->ai.state_mode = -1; |
|
1068 |
p->ai.state_counter = 0; |
|
1069 |
p->ai.timeout_counter = 0; |
|
1070 |
} |
|
1071 |
||
1072 |
static void AiWantTrainRoute(Player *p) |
|
1073 |
{ |
|
1074 |
uint16 r; |
|
1075 |
p->ai.railtype_to_use = p->max_railtype - 1; |
|
1076 |
r = (uint16)Random(); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1077 |
|
0 | 1078 |
if (r > 0xD000) { |
1079 |
AiWantLongIndustryRoute(p); |
|
1080 |
} else if (r > 0x6000) { |
|
1081 |
AiWantMediumIndustryRoute(p); |
|
1082 |
} else if (r > 0x1000) { |
|
1083 |
AiWantShortIndustryRoute(p); |
|
1084 |
} else if (r > 0x800) { |
|
1085 |
AiWantPassengerRoute(p); |
|
1086 |
} else { |
|
1087 |
AiWantMailRoute(p); |
|
1088 |
} |
|
1089 |
} |
|
1090 |
||
1091 |
static void AiWantLongRoadIndustryRoute(Player *p) |
|
1092 |
{ |
|
1093 |
int i; |
|
1094 |
FoundRoute fr; |
|
1095 |
||
1096 |
i = 60; |
|
1097 |
for(;;) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1098 |
|
0 | 1099 |
// look for one from the subsidy list |
1100 |
AiFindSubsidyIndustryRoute(&fr); |
|
1101 |
if (IS_INT_INSIDE(fr.distance, 35, 55+1)) |
|
1102 |
break; |
|
1103 |
||
1104 |
// try a random one |
|
1105 |
AiFindRandomIndustryRoute(&fr); |
|
1106 |
if (IS_INT_INSIDE(fr.distance, 35, 55+1)) |
|
1107 |
break; |
|
1108 |
||
1109 |
// only test 60 times |
|
1110 |
if (--i == 0) |
|
1111 |
return; |
|
1112 |
} |
|
1113 |
||
1114 |
if (!AiCheckIfRouteIsGood(p, &fr, 2)) |
|
1115 |
return; |
|
1116 |
||
1117 |
// Fill the source field |
|
1118 |
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
|
1119 |
p->ai.src.use_tile = 0; |
|
1120 |
p->ai.src.rand_rng = 9; |
|
1121 |
p->ai.src.cur_building_rule = 0xFF; |
|
1122 |
p->ai.src.buildcmd_a = 1; |
|
1123 |
p->ai.src.direction = 0; |
|
1124 |
p->ai.src.cargo = fr.cargo | 0x80; |
|
1125 |
||
1126 |
// Fill the dest field |
|
1127 |
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
|
1128 |
p->ai.dst.use_tile = 0; |
|
1129 |
p->ai.dst.rand_rng = 9; |
|
1130 |
p->ai.dst.cur_building_rule = 0xFF; |
|
1131 |
p->ai.dst.buildcmd_a = 0xFF; |
|
1132 |
p->ai.dst.direction = 0; |
|
1133 |
p->ai.dst.cargo = fr.cargo; |
|
1134 |
||
1135 |
// Fill common fields |
|
1136 |
p->ai.cargo_type = fr.cargo; |
|
1137 |
p->ai.num_build_rec = 2; |
|
1138 |
p->ai.num_loco_to_build = 5; |
|
1139 |
p->ai.num_want_fullload = 5; |
|
1140 |
||
1141 |
// p->ai.loco_id = INVALID_VEHICLE; |
|
1142 |
p->ai.order_list_blocks[0] = 0; |
|
1143 |
p->ai.order_list_blocks[1] = 1; |
|
1144 |
p->ai.order_list_blocks[2] = 255; |
|
1145 |
||
1146 |
p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS; |
|
1147 |
p->ai.state_mode = -1; |
|
1148 |
p->ai.state_counter = 0; |
|
1149 |
p->ai.timeout_counter = 0; |
|
1150 |
} |
|
1151 |
||
1152 |
static void AiWantMediumRoadIndustryRoute(Player *p) |
|
1153 |
{ |
|
1154 |
int i; |
|
1155 |
FoundRoute fr; |
|
1156 |
||
1157 |
i = 60; |
|
1158 |
for(;;) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1159 |
|
0 | 1160 |
// look for one from the subsidy list |
1161 |
AiFindSubsidyIndustryRoute(&fr); |
|
1162 |
if (IS_INT_INSIDE(fr.distance, 15, 40+1)) |
|
1163 |
break; |
|
1164 |
||
1165 |
// try a random one |
|
1166 |
AiFindRandomIndustryRoute(&fr); |
|
1167 |
if (IS_INT_INSIDE(fr.distance, 15, 40+1)) |
|
1168 |
break; |
|
1169 |
||
1170 |
// only test 60 times |
|
1171 |
if (--i == 0) |
|
1172 |
return; |
|
1173 |
} |
|
1174 |
||
1175 |
if (!AiCheckIfRouteIsGood(p, &fr, 2)) |
|
1176 |
return; |
|
1177 |
||
1178 |
// Fill the source field |
|
1179 |
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
|
1180 |
p->ai.src.use_tile = 0; |
|
1181 |
p->ai.src.rand_rng = 9; |
|
1182 |
p->ai.src.cur_building_rule = 0xFF; |
|
1183 |
p->ai.src.buildcmd_a = 1; |
|
1184 |
p->ai.src.direction = 0; |
|
1185 |
p->ai.src.cargo = fr.cargo | 0x80; |
|
1186 |
||
1187 |
// Fill the dest field |
|
1188 |
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
|
1189 |
p->ai.dst.use_tile = 0; |
|
1190 |
p->ai.dst.rand_rng = 9; |
|
1191 |
p->ai.dst.cur_building_rule = 0xFF; |
|
1192 |
p->ai.dst.buildcmd_a = 0xFF; |
|
1193 |
p->ai.dst.direction = 0; |
|
1194 |
p->ai.dst.cargo = fr.cargo; |
|
1195 |
||
1196 |
// Fill common fields |
|
1197 |
p->ai.cargo_type = fr.cargo; |
|
1198 |
p->ai.num_build_rec = 2; |
|
1199 |
p->ai.num_loco_to_build = 3; |
|
1200 |
p->ai.num_want_fullload = 3; |
|
1201 |
||
1202 |
// p->ai.loco_id = INVALID_VEHICLE; |
|
1203 |
p->ai.order_list_blocks[0] = 0; |
|
1204 |
p->ai.order_list_blocks[1] = 1; |
|
1205 |
p->ai.order_list_blocks[2] = 255; |
|
1206 |
||
1207 |
p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS; |
|
1208 |
p->ai.state_mode = -1; |
|
1209 |
p->ai.state_counter = 0; |
|
1210 |
p->ai.timeout_counter = 0; |
|
1211 |
} |
|
1212 |
||
1213 |
static void AiWantLongRoadPassengerRoute(Player *p) |
|
1214 |
{ |
|
1215 |
int i; |
|
1216 |
FoundRoute fr; |
|
1217 |
||
1218 |
i = 60; |
|
1219 |
for(;;) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1220 |
|
0 | 1221 |
// look for one from the subsidy list |
1222 |
AiFindSubsidyPassengerRoute(&fr); |
|
1223 |
if (IS_INT_INSIDE(fr.distance, 55, 180+1)) |
|
1224 |
break; |
|
1225 |
||
1226 |
// try a random one |
|
1227 |
AiFindRandomPassengerRoute(&fr); |
|
1228 |
if (IS_INT_INSIDE(fr.distance, 55, 180+1)) |
|
1229 |
break; |
|
1230 |
||
1231 |
// only test 60 times |
|
1232 |
if (--i == 0) |
|
1233 |
return; |
|
1234 |
} |
|
1235 |
||
1236 |
fr.cargo = CT_PASSENGERS; |
|
1237 |
||
1238 |
if (!AiCheckIfRouteIsGood(p, &fr, 2)) |
|
1239 |
return; |
|
1240 |
||
1241 |
// Fill the source field |
|
1242 |
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
|
1243 |
p->ai.src.use_tile = 0; |
|
1244 |
p->ai.src.rand_rng = 10; |
|
1245 |
p->ai.src.cur_building_rule = 0xFF; |
|
1246 |
p->ai.src.buildcmd_a = 1; |
|
1247 |
p->ai.src.direction = 0; |
|
1248 |
p->ai.src.cargo = CT_PASSENGERS; |
|
1249 |
||
1250 |
// Fill the dest field |
|
1251 |
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
|
1252 |
p->ai.dst.use_tile = 0; |
|
1253 |
p->ai.dst.rand_rng = 10; |
|
1254 |
p->ai.dst.cur_building_rule = 0xFF; |
|
1255 |
p->ai.dst.buildcmd_a = 0xFF; |
|
1256 |
p->ai.dst.direction = 0; |
|
1257 |
p->ai.dst.cargo = CT_PASSENGERS; |
|
1258 |
||
1259 |
// Fill common fields |
|
1260 |
p->ai.cargo_type = CT_PASSENGERS; |
|
1261 |
p->ai.num_build_rec = 2; |
|
1262 |
p->ai.num_loco_to_build = 4; |
|
1263 |
p->ai.num_want_fullload = 0; |
|
1264 |
||
1265 |
// p->ai.loco_id = INVALID_VEHICLE; |
|
1266 |
p->ai.order_list_blocks[0] = 0; |
|
1267 |
p->ai.order_list_blocks[1] = 1; |
|
1268 |
p->ai.order_list_blocks[2] = 255; |
|
1269 |
||
1270 |
p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS; |
|
1271 |
p->ai.state_mode = -1; |
|
1272 |
p->ai.state_counter = 0; |
|
1273 |
p->ai.timeout_counter = 0; |
|
1274 |
} |
|
1275 |
||
1276 |
static void AiWantPassengerRouteInsideTown(Player *p) |
|
1277 |
{ |
|
1278 |
int i; |
|
1279 |
FoundRoute fr; |
|
1280 |
Town *t; |
|
1281 |
||
1282 |
i = 60; |
|
1283 |
for(;;) { |
|
1284 |
// Find a town big enough |
|
1285 |
t = AiFindRandomTown(); |
|
1286 |
if (t != NULL && t->population >= 700) |
|
1287 |
break; |
|
1288 |
||
1289 |
// only test 60 times |
|
1290 |
if (--i == 0) |
|
1291 |
return; |
|
1292 |
} |
|
1293 |
||
1294 |
fr.cargo = CT_PASSENGERS; |
|
1295 |
fr.from = fr.to = t; |
|
1296 |
||
1297 |
if (!AiCheckIfRouteIsGood(p, &fr, 2)) |
|
1298 |
return; |
|
1299 |
||
1300 |
// Fill the source field |
|
1301 |
p->ai.src.spec_tile = t->xy; |
|
1302 |
p->ai.src.use_tile = 0; |
|
1303 |
p->ai.src.rand_rng = 10; |
|
1304 |
p->ai.src.cur_building_rule = 0xFF; |
|
1305 |
p->ai.src.buildcmd_a = 1; |
|
1306 |
p->ai.src.direction = 0; |
|
1307 |
p->ai.src.cargo = CT_PASSENGERS; |
|
1308 |
||
1309 |
// Fill the dest field |
|
1310 |
p->ai.dst.spec_tile = t->xy; |
|
1311 |
p->ai.dst.use_tile = 0; |
|
1312 |
p->ai.dst.rand_rng = 10; |
|
1313 |
p->ai.dst.cur_building_rule = 0xFF; |
|
1314 |
p->ai.dst.buildcmd_a = 0xFF; |
|
1315 |
p->ai.dst.direction = 0; |
|
1316 |
p->ai.dst.cargo = CT_PASSENGERS; |
|
1317 |
||
1318 |
// Fill common fields |
|
1319 |
p->ai.cargo_type = CT_PASSENGERS; |
|
1320 |
p->ai.num_build_rec = 2; |
|
1321 |
p->ai.num_loco_to_build = 2; |
|
1322 |
p->ai.num_want_fullload = 0; |
|
1323 |
||
1324 |
// p->ai.loco_id = INVALID_VEHICLE; |
|
1325 |
p->ai.order_list_blocks[0] = 0; |
|
1326 |
p->ai.order_list_blocks[1] = 1; |
|
1327 |
p->ai.order_list_blocks[2] = 255; |
|
1328 |
||
1329 |
p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS; |
|
1330 |
p->ai.state_mode = -1; |
|
1331 |
p->ai.state_counter = 0; |
|
1332 |
p->ai.timeout_counter = 0; |
|
1333 |
} |
|
1334 |
||
1335 |
static void AiWantRoadRoute(Player *p) |
|
1336 |
{ |
|
1337 |
uint16 r = (uint16)Random(); |
|
1338 |
||
1339 |
if (r > 0x4000) { |
|
1340 |
AiWantLongRoadIndustryRoute(p); |
|
1341 |
} else if (r > 0x2000) { |
|
1342 |
AiWantMediumRoadIndustryRoute(p); |
|
1343 |
} else if (r > 0x1000) { |
|
1344 |
AiWantLongRoadPassengerRoute(p); |
|
1345 |
} else { |
|
1346 |
AiWantPassengerRouteInsideTown(p); |
|
1347 |
} |
|
1348 |
||
1349 |
} |
|
1350 |
||
1351 |
static void AiWantPassengerAircraftRoute(Player *p) |
|
1352 |
{ |
|
1353 |
FoundRoute fr; |
|
1354 |
int i; |
|
1355 |
||
1356 |
i = 60; |
|
1357 |
for(;;) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1358 |
|
0 | 1359 |
// look for one from the subsidy list |
1360 |
AiFindSubsidyPassengerRoute(&fr); |
|
1361 |
if (IS_INT_INSIDE(fr.distance,0,95+1)) |
|
1362 |
break; |
|
1363 |
||
1364 |
// try a random one |
|
1365 |
AiFindRandomPassengerRoute(&fr); |
|
1366 |
if (IS_INT_INSIDE(fr.distance,0,95+1)) |
|
1367 |
break; |
|
1368 |
||
1369 |
// only test 60 times |
|
1370 |
if (--i == 0) |
|
1371 |
return; |
|
1372 |
} |
|
1373 |
||
1374 |
fr.cargo = CT_PASSENGERS; |
|
1375 |
if (!AiCheckIfRouteIsGood(p, &fr, 4)) |
|
1376 |
return; |
|
1377 |
||
1378 |
||
1379 |
// Fill the source field |
|
1380 |
p->ai.src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
|
1381 |
p->ai.src.use_tile = 0; |
|
1382 |
p->ai.src.rand_rng = 12; |
|
1383 |
p->ai.src.cur_building_rule = 0xFF; |
|
1384 |
p->ai.src.cargo = fr.cargo; |
|
1385 |
||
1386 |
// Fill the dest field |
|
1387 |
p->ai.dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
|
1388 |
p->ai.dst.use_tile = 0; |
|
1389 |
p->ai.dst.rand_rng = 12; |
|
1390 |
p->ai.dst.cur_building_rule = 0xFF; |
|
1391 |
p->ai.dst.cargo = fr.cargo; |
|
1392 |
||
1393 |
// Fill common fields |
|
1394 |
p->ai.cargo_type = fr.cargo; |
|
1395 |
p->ai.build_kind = 0; |
|
1396 |
p->ai.num_build_rec = 2; |
|
1397 |
p->ai.num_loco_to_build = 1; |
|
1398 |
p->ai.num_want_fullload = 1; |
|
1399 |
// p->ai.loco_id = INVALID_VEHICLE; |
|
1400 |
p->ai.order_list_blocks[0] = 0; |
|
1401 |
p->ai.order_list_blocks[1] = 1; |
|
1402 |
p->ai.order_list_blocks[2] = 255; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1403 |
|
0 | 1404 |
p->ai.state = AIS_AIRPORT_STUFF; |
1405 |
p->ai.timeout_counter = 0; |
|
1406 |
} |
|
1407 |
||
1408 |
static void AiWantOilRigAircraftRoute(Player *p) |
|
1409 |
{ |
|
1410 |
int i; |
|
1411 |
FoundRoute fr; |
|
1412 |
Town *t; |
|
1413 |
Industry *in; |
|
1414 |
||
1415 |
i = 60; |
|
1416 |
for(;;) { |
|
1417 |
// Find a town |
|
1418 |
t = AiFindRandomTown(); |
|
1419 |
if (t != NULL) { |
|
1420 |
// Find a random oil rig industry |
|
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
1421 |
in = GetIndustry(RandomRange(_total_industries)); |
0 | 1422 |
if (in != NULL && in->type == IT_OIL_RIG) { |
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
1423 |
if (DistanceManhattan(t->xy, in->xy) < 60) |
0 | 1424 |
break; |
1425 |
} |
|
1426 |
} |
|
1427 |
||
1428 |
// only test 60 times |
|
1429 |
if (--i == 0) |
|
1430 |
return; |
|
1431 |
} |
|
1432 |
||
1433 |
fr.cargo = CT_PASSENGERS; |
|
1434 |
fr.from = fr.to = t; |
|
1435 |
||
1436 |
if (!AiCheckIfRouteIsGood(p, &fr, 4)) |
|
1437 |
return; |
|
1438 |
||
1439 |
// Fill the source field |
|
1440 |
p->ai.src.spec_tile = t->xy; |
|
1441 |
p->ai.src.use_tile = 0; |
|
1442 |
p->ai.src.rand_rng = 12; |
|
1443 |
p->ai.src.cur_building_rule = 0xFF; |
|
1444 |
p->ai.src.cargo = CT_PASSENGERS; |
|
1445 |
||
1446 |
// Fill the dest field |
|
1447 |
p->ai.dst.spec_tile = in->xy; |
|
1448 |
p->ai.dst.use_tile = 0; |
|
1449 |
p->ai.dst.rand_rng = 5; |
|
1450 |
p->ai.dst.cur_building_rule = 0xFF; |
|
1451 |
p->ai.dst.cargo = CT_PASSENGERS; |
|
1452 |
||
1453 |
// Fill common fields |
|
1454 |
p->ai.cargo_type = CT_PASSENGERS; |
|
1455 |
p->ai.build_kind = 1; |
|
1456 |
p->ai.num_build_rec = 2; |
|
1457 |
p->ai.num_loco_to_build = 1; |
|
1458 |
p->ai.num_want_fullload = 0; |
|
1459 |
// p->ai.loco_id = INVALID_VEHICLE; |
|
1460 |
p->ai.order_list_blocks[0] = 0; |
|
1461 |
p->ai.order_list_blocks[1] = 1; |
|
1462 |
p->ai.order_list_blocks[2] = 255; |
|
1463 |
||
1464 |
p->ai.state = AIS_AIRPORT_STUFF; |
|
1465 |
p->ai.timeout_counter = 0; |
|
1466 |
} |
|
1467 |
||
1468 |
static void AiWantAircraftRoute(Player *p) |
|
1469 |
{ |
|
1470 |
uint16 r = (uint16)Random(); |
|
1471 |
||
1472 |
if (r >= 0x2AAA || _date < 0x3912) { |
|
1473 |
AiWantPassengerAircraftRoute(p); |
|
1474 |
} else { |
|
1475 |
AiWantOilRigAircraftRoute(p); |
|
1476 |
} |
|
1477 |
} |
|
1478 |
||
1479 |
static void AiWantShipRoute(Player *p) |
|
1480 |
{ |
|
1481 |
// XXX |
|
1482 |
// error("AiWaitShipRoute"); |
|
1483 |
} |
|
1484 |
||
1485 |
||
1486 |
||
1487 |
static void AiStateWantNewRoute(Player *p) |
|
1488 |
{ |
|
1489 |
uint16 r; |
|
1490 |
int i; |
|
1491 |
||
1492 |
if (p->player_money < AiGetBasePrice(p) * 500) { |
|
1493 |
p->ai.state = AIS_0; |
|
1494 |
return; |
|
1495 |
} |
|
1496 |
||
1497 |
i = 200; |
|
1498 |
for(;;) { |
|
1499 |
r = (uint16)Random(); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1500 |
|
76
30511cbc5188
(svn r77) -Fix: [1010788] AI service interval bug (tnx truesatan)
truelight
parents:
21
diff
changeset
|
1501 |
if (_patches.ai_disable_veh_train && _patches.ai_disable_veh_roadveh && |
30511cbc5188
(svn r77) -Fix: [1010788] AI service interval bug (tnx truesatan)
truelight
parents:
21
diff
changeset
|
1502 |
_patches.ai_disable_veh_aircraft && _patches.ai_disable_veh_ship) |
30511cbc5188
(svn r77) -Fix: [1010788] AI service interval bug (tnx truesatan)
truelight
parents:
21
diff
changeset
|
1503 |
return; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1504 |
|
0 | 1505 |
if (r < 0x7626) { |
20
13a47054d81e
(svn r21) Remove: PE_BYTE (bit-bools) in favour of PE_BOOL
truelight
parents:
0
diff
changeset
|
1506 |
if (_patches.ai_disable_veh_train) continue; |
0 | 1507 |
AiWantTrainRoute(p); |
1508 |
} else if (r < 0xC4EA) { |
|
20
13a47054d81e
(svn r21) Remove: PE_BYTE (bit-bools) in favour of PE_BOOL
truelight
parents:
0
diff
changeset
|
1509 |
if (_patches.ai_disable_veh_roadveh) continue; |
0 | 1510 |
AiWantRoadRoute(p); |
1511 |
} else if (r < 0xD89B) { |
|
20
13a47054d81e
(svn r21) Remove: PE_BYTE (bit-bools) in favour of PE_BOOL
truelight
parents:
0
diff
changeset
|
1512 |
if (_patches.ai_disable_veh_aircraft) continue; |
0 | 1513 |
AiWantAircraftRoute(p); |
1514 |
} else { |
|
20
13a47054d81e
(svn r21) Remove: PE_BYTE (bit-bools) in favour of PE_BOOL
truelight
parents:
0
diff
changeset
|
1515 |
if (_patches.ai_disable_veh_ship) continue; |
0 | 1516 |
AiWantShipRoute(p); |
1517 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1518 |
|
0 | 1519 |
// got a route? |
1520 |
if (p->ai.state != AIS_WANT_NEW_ROUTE) |
|
1521 |
break; |
|
1522 |
||
1523 |
// time out? |
|
1524 |
if (--i == 0) { |
|
1525 |
if (++p->ai.state_counter == 556) p->ai.state = AIS_0; |
|
1526 |
break; |
|
1527 |
} |
|
1528 |
} |
|
1529 |
} |
|
1530 |
||
1531 |
static bool AiCheckTrackResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo) |
|
1532 |
{ |
|
1533 |
uint values[NUM_CARGO]; |
|
1534 |
int w,h; |
|
1535 |
uint tile2; |
|
568
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
1536 |
int rad; |
0 | 1537 |
|
1538 |
for(;p->mode != 4;p++) if (p->mode == 1) { |
|
909
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
1539 |
tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs)); |
0 | 1540 |
|
1541 |
w = ((p->attr>>1) & 7); |
|
1542 |
h = ((p->attr>>4) & 7); |
|
1543 |
if (p->attr&1) intswap(w, h); |
|
1544 |
||
568
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
1545 |
|
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
1546 |
if (_patches.modified_catchment) { |
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
1547 |
rad = CA_TRAIN; |
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
1548 |
} else { |
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
1549 |
rad = 4; |
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
1550 |
} |
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
1551 |
|
0 | 1552 |
if (cargo & 0x80) { |
568
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
1553 |
GetProductionAroundTiles(values, tile2, w, h, rad); |
0 | 1554 |
return values[cargo & 0x7F] != 0; |
1555 |
} else { |
|
568
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
1556 |
GetAcceptanceAroundTiles(values, tile2, w, h, rad); |
0 | 1557 |
if (!(values[cargo] & ~7)) |
1558 |
return false; |
|
1559 |
if (cargo != CT_MAIL) |
|
1560 |
return true; |
|
1561 |
return !!((values[cargo]>>1) & ~7); |
|
1562 |
} |
|
1563 |
} |
|
1564 |
||
1565 |
return true; |
|
1566 |
} |
|
1567 |
||
1568 |
static int32 AiDoBuildDefaultRailTrack(TileIndex tile, const AiDefaultBlockData *p, byte flag) |
|
1569 |
{ |
|
1570 |
int32 r; |
|
1571 |
int32 total_cost = 0; |
|
1572 |
Town *t = NULL; |
|
1573 |
int rating = 0; |
|
1574 |
int i,j,k; |
|
1575 |
||
1576 |
for(;;) { |
|
1577 |
// This will seldomly overflow for valid reasons. Mask it to be on the safe side. |
|
909
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
1578 |
uint c = TILE_MASK(tile + ToTileIndexDiff(p->tileoffs)); |
0 | 1579 |
|
1580 |
_cleared_town = NULL; |
|
1581 |
||
1582 |
if (p->mode < 2) { |
|
1583 |
if (p->mode == 0) { |
|
1584 |
// Depot |
|
1585 |
r = DoCommandByTile(c, _cur_ai_player->ai.railtype_to_use, p->attr, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_TRAIN_DEPOT); |
|
1586 |
} else { |
|
1587 |
// Station |
|
1588 |
r = DoCommandByTile(c, (p->attr&1) | (p->attr>>4)<<8 | (p->attr>>1&7)<<16, _cur_ai_player->ai.railtype_to_use, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_RAILROAD_STATION); |
|
1589 |
} |
|
1590 |
||
1591 |
if (r == CMD_ERROR) return CMD_ERROR; |
|
1592 |
total_cost += r; |
|
1593 |
||
1594 |
clear_town_stuff:; |
|
1595 |
if (_cleared_town != NULL) { |
|
1596 |
if (t != NULL && t != _cleared_town) |
|
1597 |
return CMD_ERROR; |
|
1598 |
t = _cleared_town; |
|
1599 |
rating += _cleared_town_rating; |
|
1600 |
} |
|
1601 |
} else if (p->mode == 2) { |
|
1602 |
// Rail |
|
1035
812f837ee03f
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
1603 |
if (IsTileType(c, MP_RAILWAY)) |
0 | 1604 |
return CMD_ERROR; |
1605 |
||
1606 |
j = p->attr; |
|
1607 |
k = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1608 |
|
0 | 1609 |
// Build the rail |
1610 |
for(i=0; i!=6; i++,j>>=1) { |
|
1611 |
if (j&1) { |
|
1612 |
k = i; |
|
1613 |
r = DoCommandByTile(c, _cur_ai_player->ai.railtype_to_use, i, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL); |
|
1614 |
if (r == CMD_ERROR) return CMD_ERROR; |
|
1615 |
total_cost += r; |
|
1616 |
} |
|
1617 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1618 |
|
0 | 1619 |
/* signals too? */ |
1620 |
if (j&3) { |
|
1621 |
// Can't build signals on a road. |
|
1035
812f837ee03f
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
1622 |
if (IsTileType(c, MP_STREET)) |
0 | 1623 |
return CMD_ERROR; |
1624 |
||
1625 |
if (flag & DC_EXEC) { |
|
1626 |
j = 4 - j; |
|
1627 |
do { |
|
1628 |
r = DoCommandByTile(c, k, 0, flag, CMD_BUILD_SIGNALS); |
|
1629 |
} while (--j); |
|
1630 |
} else { |
|
1631 |
r = _price.build_signals; |
|
1632 |
} |
|
1633 |
if (r == CMD_ERROR) return CMD_ERROR; |
|
1634 |
total_cost += r; |
|
1635 |
} |
|
1636 |
} else if (p->mode == 3) { |
|
1637 |
//Clear stuff and then build single rail. |
|
1638 |
if (GetTileSlope(c,NULL) != 0) |
|
1639 |
return CMD_ERROR; |
|
1640 |
r = DoCommandByTile(c, 0, 0, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_LANDSCAPE_CLEAR); |
|
1641 |
if (r == CMD_ERROR) return CMD_ERROR; |
|
1642 |
total_cost += r + _price.build_rail; |
|
1643 |
||
1644 |
if (flag & DC_EXEC) { |
|
1645 |
DoCommandByTile(c, _cur_ai_player->ai.railtype_to_use, p->attr&1, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_SINGLE_RAIL); |
|
1646 |
} |
|
1647 |
||
1648 |
goto clear_town_stuff; |
|
1649 |
} else { |
|
1650 |
// Unk |
|
1651 |
break; |
|
1652 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1653 |
|
0 | 1654 |
p++; |
1655 |
} |
|
1656 |
||
1657 |
if (!(flag & DC_EXEC)) { |
|
1658 |
if (t != NULL && rating > t->ratings[_current_player]) { |
|
1659 |
return CMD_ERROR; |
|
1660 |
} |
|
1661 |
} |
|
1662 |
||
1663 |
return total_cost; |
|
1664 |
} |
|
1665 |
||
1666 |
// Returns rule and cost |
|
1667 |
static int AiBuildDefaultRailTrack(TileIndex tile, byte p0, byte p1, byte p2, byte p3, byte dir, byte cargo, int32 *cost) |
|
1668 |
{ |
|
1669 |
int i; |
|
1670 |
const AiDefaultRailBlock *p; |
|
1671 |
||
1672 |
for(i=0; (p = _default_rail_track_data[i]) != NULL; i++) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1673 |
if (p->p0 == p0 && p->p1 == p1 && p->p2 == p2 && p->p3 == p3 && |
0 | 1674 |
(p->dir == 0xFF || p->dir == dir || ((p->dir-1)&3) == dir)) { |
1675 |
*cost = AiDoBuildDefaultRailTrack(tile, p->data, DC_NO_TOWN_RATING); |
|
1676 |
if (*cost != CMD_ERROR && AiCheckTrackResources(tile, p->data, cargo)) |
|
1677 |
return i; |
|
1678 |
} |
|
1679 |
} |
|
1680 |
||
1681 |
return -1; |
|
1682 |
} |
|
1683 |
||
1684 |
static const byte _terraform_up_flags[] = { |
|
1685 |
14, 13, 12, 11, |
|
1686 |
10, 9, 8, 7, |
|
1687 |
6, 5, 4, 3, |
|
1688 |
2, 1, 0, 1, |
|
1689 |
2, 1, 4, 1, |
|
1690 |
2, 1, 8, 1, |
|
1691 |
2, 1, 4, 2, |
|
1692 |
2, 1 |
|
1693 |
}; |
|
1694 |
||
1695 |
static const byte _terraform_down_flags[] = { |
|
1696 |
1, 2, 3, 4, |
|
1697 |
5, 6, 1, 8, |
|
1698 |
9, 10, 8, 12, |
|
1699 |
4, 2, 0, 0, |
|
1700 |
1, 2, 3, 4, |
|
1701 |
5, 6, 2, 8, |
|
1702 |
9, 10, 1, 12, |
|
1703 |
8, 4 |
|
1704 |
}; |
|
1705 |
||
1706 |
static void AiDoTerraformLand(TileIndex tile, int dir, int unk, int mode) |
|
1707 |
{ |
|
1708 |
byte old_player; |
|
1709 |
uint32 r; |
|
1710 |
uint slope; |
|
1335
a5f223b9f549
(svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents:
1330
diff
changeset
|
1711 |
uint h; |
0 | 1712 |
|
1713 |
old_player = _current_player; |
|
1714 |
_current_player = OWNER_NONE; |
|
1715 |
||
1716 |
r = Random(); |
|
1717 |
||
1718 |
unk &= (int)r; |
|
1719 |
||
1720 |
do { |
|
900 | 1721 |
tile = TILE_MASK(tile + TileOffsByDir(dir)); |
0 | 1722 |
|
1723 |
r >>= 2; |
|
1724 |
if (r&2) { |
|
1725 |
dir++; |
|
1726 |
if (r&1) |
|
1727 |
dir -= 2; |
|
1728 |
} |
|
1729 |
dir &= 3; |
|
1730 |
} while (--unk >= 0); |
|
1731 |
||
1732 |
slope = GetTileSlope(tile, &h); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1733 |
|
0 | 1734 |
if (slope != 0) { |
1735 |
if (mode > 0 || (mode == 0 && !(r&0xC))) { |
|
1736 |
// Terraform up |
|
1737 |
DoCommandByTile(tile, _terraform_up_flags[slope-1], 1, |
|
1738 |
DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND); |
|
1739 |
} else if (h != 0) { |
|
1740 |
// Terraform down |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1741 |
DoCommandByTile(tile, _terraform_down_flags[slope-1], 0, |
0 | 1742 |
DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1743 |
} |
0 | 1744 |
} |
1745 |
||
1746 |
_current_player = old_player; |
|
1747 |
} |
|
1748 |
||
1749 |
static void AiStateBuildDefaultRailBlocks(Player *p) |
|
1750 |
{ |
|
1751 |
int i, j; |
|
1752 |
AiBuildRec *aib; |
|
1753 |
int rule; |
|
1754 |
int32 cost; |
|
1755 |
||
1756 |
// time out? |
|
1757 |
if (++p->ai.timeout_counter == 1388) { |
|
1758 |
p->ai.state = AIS_DELETE_RAIL_BLOCKS; |
|
1759 |
return; |
|
1760 |
} |
|
1761 |
||
1762 |
// do the following 8 times |
|
1763 |
i = 8; |
|
1764 |
do { |
|
1765 |
// check if we can build the default track |
|
1766 |
aib = &p->ai.src; |
|
1767 |
j = p->ai.num_build_rec; |
|
1768 |
do { |
|
1769 |
// this item has already been built? |
|
1770 |
if (aib->cur_building_rule != 255) |
|
1771 |
continue; |
|
1772 |
||
1773 |
// adjust the coordinate randomly, |
|
1774 |
// to make sure that we find a position. |
|
1775 |
aib->use_tile = AdjustTileCoordRandomly(aib->spec_tile, aib->rand_rng); |
|
1776 |
||
1777 |
// check if the track can be build there. |
|
1778 |
rule = AiBuildDefaultRailTrack(aib->use_tile, |
|
1779 |
p->ai.build_kind, p->ai.num_wagons, |
|
1780 |
aib->unk6, aib->unk7, |
|
1781 |
aib->direction, aib->cargo, &cost); |
|
1782 |
||
1783 |
if (rule == -1) { |
|
1784 |
// cannot build, terraform after a while |
|
1785 |
if (p->ai.state_counter >= 600) { |
|
1786 |
AiDoTerraformLand(aib->use_tile, Random()&3, 3, (int8)p->ai.state_mode); |
|
1787 |
} |
|
1788 |
// also try the other terraform direction |
|
1789 |
if (++p->ai.state_counter >= 1000) { |
|
1790 |
p->ai.state_counter = 0; |
|
1791 |
p->ai.state_mode = -p->ai.state_mode; |
|
1792 |
} |
|
1793 |
} else if (CheckPlayerHasMoney(cost)) { |
|
1794 |
int32 r; |
|
1795 |
// player has money, build it. |
|
1796 |
aib->cur_building_rule = rule; |
|
1797 |
||
1798 |
r = AiDoBuildDefaultRailTrack( |
|
1799 |
aib->use_tile, |
|
1800 |
_default_rail_track_data[rule]->data, |
|
1801 |
DC_EXEC | DC_NO_TOWN_RATING |
|
1802 |
); |
|
1803 |
assert(r != CMD_ERROR); |
|
1804 |
} |
|
1805 |
} while (++aib,--j); |
|
1806 |
} while (--i); |
|
1807 |
||
1808 |
// check if we're done with all of them |
|
1809 |
aib = &p->ai.src; |
|
1810 |
j = p->ai.num_build_rec; |
|
1811 |
do { |
|
1812 |
if (aib->cur_building_rule == 255) |
|
1813 |
return; |
|
1814 |
} while (++aib,--j); |
|
1815 |
||
1816 |
// yep, all are done. switch state to the rail building state. |
|
1817 |
p->ai.state = AIS_BUILD_RAIL; |
|
1818 |
p->ai.state_mode = 255; |
|
1819 |
} |
|
1820 |
||
1821 |
static TileIndex AiGetEdgeOfDefaultRailBlock(byte rule, TileIndex tile, byte cmd, int *dir) |
|
1822 |
{ |
|
1823 |
const AiDefaultBlockData *p = _default_rail_track_data[rule]->data; |
|
1824 |
||
1825 |
while (p->mode != 3 || !((--cmd) & 0x80)) p++; |
|
1826 |
||
909
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
1827 |
return tile + ToTileIndexDiff(p->tileoffs) - TileOffsByDir(*dir = p->attr); |
0 | 1828 |
} |
1829 |
||
1830 |
typedef struct AiRailPathFindData { |
|
1831 |
TileIndex tile; |
|
1832 |
TileIndex tile2; |
|
1833 |
int count; |
|
1834 |
bool flag; |
|
1835 |
} AiRailPathFindData; |
|
1836 |
||
1837 |
static bool AiEnumFollowTrack(uint tile, AiRailPathFindData *a, int track, uint length, byte *state) |
|
1838 |
{ |
|
1839 |
if (a->flag) |
|
1840 |
return true; |
|
1841 |
||
1842 |
if (length > 20 || tile == a->tile) { |
|
1843 |
a->flag = true; |
|
1844 |
return true; |
|
1845 |
} |
|
1846 |
||
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
1847 |
if (DistanceMax(tile, a->tile2) < 4) |
0 | 1848 |
a->count++; |
1849 |
||
1850 |
return false; |
|
1851 |
} |
|
1852 |
||
1853 |
static bool AiDoFollowTrack(Player *p) |
|
1854 |
{ |
|
1855 |
AiRailPathFindData arpfd; |
|
1856 |
arpfd.tile = p->ai.start_tile_a; |
|
1857 |
arpfd.tile2 = p->ai.cur_tile_a; |
|
1858 |
arpfd.flag = false; |
|
1859 |
arpfd.count = 0; |
|
900 | 1860 |
FollowTrack(p->ai.cur_tile_a + TileOffsByDir(p->ai.cur_dir_a), 0x2000 | TRANSPORT_RAIL, p->ai.cur_dir_a^2, |
0 | 1861 |
(TPFEnumProc*)AiEnumFollowTrack, NULL, &arpfd); |
1862 |
return arpfd.count > 8; |
|
1863 |
} |
|
1864 |
||
1865 |
typedef struct AiRailFinder { |
|
1866 |
TileIndex final_tile; |
|
1867 |
byte final_dir; |
|
1868 |
byte depth; |
|
1869 |
byte recursive_mode; |
|
1870 |
byte cur_best_dir; |
|
1871 |
byte best_dir; |
|
1872 |
byte cur_best_depth; |
|
1873 |
byte best_depth; |
|
1874 |
uint cur_best_dist; |
|
1875 |
const byte *best_ptr; |
|
1876 |
uint best_dist; |
|
1877 |
TileIndex cur_best_tile, best_tile; |
|
1878 |
TileIndex bridge_end_tile; |
|
1879 |
Player *player; |
|
1880 |
TileInfo ti; |
|
1881 |
} AiRailFinder; |
|
1882 |
||
1883 |
static const byte _ai_table_15[4][8] = { |
|
1884 |
{0, 0, 4, 3, 3, 1, 128+0, 64}, |
|
1885 |
{1, 1, 2, 0, 4, 2, 128+1, 65}, |
|
1886 |
{0, 2, 2, 3, 5, 1, 128+2, 66}, |
|
1887 |
{1, 3, 5, 0, 3, 2, 128+3, 67} |
|
1888 |
}; |
|
1889 |
||
1890 |
static const byte _dir_table_1[] = {3, 9, 12, 6}; |
|
1891 |
static const byte _dir_table_2[] = {12, 6, 3, 9}; |
|
1892 |
||
1893 |
||
1894 |
static bool AiIsTileBanned(Player *p, TileIndex tile, byte val) { |
|
1895 |
int i; |
|
1896 |
||
1897 |
for(i=0; i!=p->ai.banned_tile_count; i++) |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1898 |
if (p->ai.banned_tiles[i] == tile && |
0 | 1899 |
p->ai.banned_val[i] == val) |
1900 |
return true; |
|
1901 |
return false; |
|
1902 |
} |
|
1903 |
||
1904 |
static void AiBanTile(Player *p, TileIndex tile, byte val) { |
|
1905 |
int i; |
|
1906 |
||
1907 |
for(i=lengthof(p->ai.banned_tiles)-1; i!=0; i--) { |
|
1908 |
p->ai.banned_tiles[i] = p->ai.banned_tiles[i-1]; |
|
1909 |
p->ai.banned_val[i] = p->ai.banned_val[i-1]; |
|
1910 |
} |
|
1911 |
||
1912 |
p->ai.banned_tiles[0] = tile; |
|
1913 |
p->ai.banned_val[0] = val; |
|
1914 |
||
1915 |
if (p->ai.banned_tile_count != lengthof(p->ai.banned_tiles)) |
|
1916 |
p->ai.banned_tile_count++; |
|
1917 |
} |
|
1918 |
||
1919 |
static void AiBuildRailRecursive(AiRailFinder *arf, TileIndex tile, int dir); |
|
1920 |
||
1921 |
static bool AiCheckRailPathBetter(AiRailFinder *arf, const byte *p) |
|
1922 |
{ |
|
1923 |
bool better = false; |
|
1924 |
||
1925 |
if (arf->recursive_mode < 1) { |
|
1926 |
// Mode is 0. This means destination has not been found yet. |
|
1927 |
// If the found path is shorter than the current one, remember it. |
|
1928 |
if (arf->cur_best_dist < arf->best_dist) { |
|
1929 |
arf->best_dir = arf->cur_best_dir; |
|
1930 |
arf->best_dist = arf->cur_best_dist; |
|
1931 |
arf->best_ptr = p; |
|
1932 |
arf->best_tile = arf->cur_best_tile; |
|
1933 |
better = true; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1934 |
} |
0 | 1935 |
} else if (arf->recursive_mode > 1) { |
1936 |
// Mode is 2. |
|
1937 |
if (arf->best_dist != 0 || arf->cur_best_depth < arf->best_depth) { |
|
1938 |
arf->best_depth = arf->cur_best_depth; |
|
1939 |
arf->best_dist = 0; |
|
1940 |
arf->best_ptr = p; |
|
1941 |
arf->best_tile = 0; |
|
1942 |
better = true; |
|
1943 |
} |
|
1944 |
} |
|
1945 |
arf->recursive_mode = 0; |
|
1946 |
arf->cur_best_dist = (uint)-1; |
|
1947 |
arf->cur_best_depth = 0xff; |
|
1948 |
||
1949 |
return better; |
|
1950 |
} |
|
1951 |
||
536 | 1952 |
static inline void AiCheckBuildRailBridgeHere(AiRailFinder *arf, TileIndex tile, const byte *p) |
0 | 1953 |
{ |
1954 |
TileIndex tile_new; |
|
1955 |
bool flag; |
|
1956 |
||
1957 |
int dir2 = p[0] & 3; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1958 |
|
0 | 1959 |
FindLandscapeHeightByTile(&arf->ti, tile); |
1960 |
||
1961 |
if (arf->ti.tileh == _dir_table_1[dir2] || (arf->ti.tileh==0 && arf->ti.z!=0)) { |
|
1962 |
tile_new = tile; |
|
1963 |
// Allow bridges directly over bottom tiles |
|
1964 |
flag = arf->ti.z == 0; |
|
1965 |
for(;;) { |
|
1216
47aa486849d9
(svn r1720) - Fix: signed/unsigned warnings. Tile which is incremented/decremented every time until suitable ground is found is compared to the direction it is going in. If this wraps, return error. For this treat the tile itself as a TileIndexDiff
darkvater
parents:
1209
diff
changeset
|
1966 |
if ((TileIndexDiff)tile_new < -TileOffsByDir(dir2)) return; // Wraping around map, no bridge possible! |
900 | 1967 |
tile_new = TILE_MASK(tile_new + TileOffsByDir(dir2)); |
0 | 1968 |
FindLandscapeHeightByTile(&arf->ti, tile_new); |
1969 |
if (arf->ti.tileh != 0 || arf->ti.type == MP_CLEAR || arf->ti.type == MP_TREES) { |
|
1970 |
if (!flag) return; |
|
1971 |
break; |
|
1972 |
} |
|
1973 |
if (arf->ti.type != MP_WATER && arf->ti.type != MP_RAILWAY && arf->ti.type != MP_STREET) |
|
1974 |
return; |
|
1975 |
flag = true; |
|
1976 |
} |
|
1977 |
||
1978 |
// Is building a (rail)bridge possible at this place (type doesn't matter)? |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1979 |
if (DoCommandByTile(tile_new, tile, arf->player->ai.railtype_to_use<<8, |
0 | 1980 |
DC_AUTO, CMD_BUILD_BRIDGE) == CMD_ERROR) |
1981 |
return; |
|
1982 |
AiBuildRailRecursive(arf, tile_new, dir2); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1983 |
|
0 | 1984 |
// At the bottom depth, check if the new path is better than the old one. |
1985 |
if (arf->depth == 1) { |
|
1986 |
if (AiCheckRailPathBetter(arf, p)) |
|
1987 |
arf->bridge_end_tile = tile_new; |
|
1988 |
} |
|
1989 |
} |
|
1990 |
} |
|
1991 |
||
536 | 1992 |
static inline void AiCheckBuildRailTunnelHere(AiRailFinder *arf, TileIndex tile, const byte *p) |
0 | 1993 |
{ |
1994 |
FindLandscapeHeightByTile(&arf->ti, tile); |
|
1995 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1996 |
if (arf->ti.tileh == _dir_table_2[p[0]&3] && arf->ti.z!=0) { |
0 | 1997 |
int32 cost = DoCommandByTile(tile, arf->player->ai.railtype_to_use, 0, DC_AUTO, CMD_BUILD_TUNNEL); |
1998 |
||
1999 |
if (cost != CMD_ERROR && cost <= (arf->player->player_money>>4)) { |
|
2000 |
AiBuildRailRecursive(arf, _build_tunnel_endtile, p[0]&3); |
|
2001 |
if (arf->depth == 1) { |
|
2002 |
AiCheckRailPathBetter(arf, p); |
|
2003 |
} |
|
2004 |
} |
|
2005 |
} |
|
2006 |
} |
|
2007 |
||
2008 |
||
2009 |
static void AiBuildRailRecursive(AiRailFinder *arf, TileIndex tile, int dir) |
|
2010 |
{ |
|
2011 |
const byte *p; |
|
2012 |
||
900 | 2013 |
tile = TILE_MASK(tile + TileOffsByDir(dir)); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2014 |
|
0 | 2015 |
// Reached destination? |
2016 |
if (tile == arf->final_tile) { |
|
2017 |
if (arf->final_dir != (dir^2)) { |
|
2018 |
if (arf->recursive_mode != 2) |
|
2019 |
arf->recursive_mode = 1; |
|
2020 |
} else if (arf->recursive_mode != 2) { |
|
2021 |
arf->recursive_mode = 2; |
|
2022 |
arf->cur_best_depth = arf->depth; |
|
2023 |
} else { |
|
2024 |
if (arf->depth < arf->cur_best_depth) |
|
2025 |
arf->cur_best_depth = arf->depth; |
|
2026 |
} |
|
2027 |
return; |
|
2028 |
} |
|
2029 |
||
2030 |
// Depth too deep? |
|
2031 |
if (arf->depth >= 4) { |
|
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
2032 |
uint dist = DistanceMaxPlusManhattan(tile, arf->final_tile); |
0 | 2033 |
if (dist < arf->cur_best_dist) { |
2034 |
// Store the tile that is closest to the final position. |
|
2035 |
arf->cur_best_depth = arf->depth; |
|
2036 |
arf->cur_best_dist = dist; |
|
2037 |
arf->cur_best_tile = tile; |
|
2038 |
arf->cur_best_dir = dir; |
|
2039 |
} |
|
2040 |
return; |
|
2041 |
} |
|
2042 |
||
2043 |
// Increase recursion depth |
|
2044 |
arf->depth++; |
|
2045 |
||
2046 |
// Grab pointer to list of stuff that is possible to build |
|
2047 |
p = _ai_table_15[dir]; |
|
2048 |
||
2049 |
// Try to build a single rail in all directions. |
|
2050 |
FindLandscapeHeightByTile(&arf->ti, tile); |
|
2051 |
if (arf->ti.z == 0) { |
|
2052 |
p += 6; |
|
2053 |
} else { |
|
2054 |
do { |
|
2055 |
// Make sure the tile is not in the list of banned tiles and that a rail can be built here. |
|
2056 |
if (!AiIsTileBanned(arf->player, tile, p[0]) && |
|
2057 |
DoCommandByTile(tile, arf->player->ai.railtype_to_use, p[0], DC_AUTO | DC_NO_WATER | DC_NO_RAIL_OVERLAP, CMD_BUILD_SINGLE_RAIL) != CMD_ERROR) { |
|
2058 |
AiBuildRailRecursive(arf, tile, p[1]); |
|
2059 |
} |
|
2060 |
||
2061 |
// At the bottom depth? |
|
2062 |
if (arf->depth == 1) { |
|
2063 |
AiCheckRailPathBetter(arf, p); |
|
2064 |
} |
|
2065 |
||
2066 |
p += 2; |
|
2067 |
} while (!(p[0]&0x80)); |
|
2068 |
} |
|
2069 |
||
2070 |
AiCheckBuildRailBridgeHere(arf, tile, p); |
|
2071 |
AiCheckBuildRailTunnelHere(arf, tile, p+1); |
|
2072 |
||
2073 |
arf->depth--; |
|
2074 |
} |
|
2075 |
||
2076 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2077 |
static const byte _dir_table_3[]= {0x25, 0x2A, 0x19, 0x16}; |
0 | 2078 |
|
2079 |
static void AiBuildRailConstruct(Player *p) |
|
2080 |
{ |
|
2081 |
AiRailFinder arf; |
|
2082 |
int i; |
|
2083 |
||
2084 |
// Check too much lookahead? |
|
2085 |
if (AiDoFollowTrack(p)) { |
|
2086 |
p->ai.state_counter = (Random()&0xE)+6; // Destruct this amount of blocks |
|
2087 |
p->ai.state_mode = 1; // Start destruct |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2088 |
|
0 | 2089 |
// Ban this tile and don't reach it for a while. |
2090 |
AiBanTile(p, p->ai.cur_tile_a, FindFirstBit(GetRailTrackStatus(p->ai.cur_tile_a))); |
|
2091 |
return; |
|
2092 |
} |
|
2093 |
||
2094 |
// Setup recursive finder and call it. |
|
2095 |
arf.player = p; |
|
2096 |
arf.final_tile = p->ai.cur_tile_b; |
|
2097 |
arf.final_dir = p->ai.cur_dir_b; |
|
2098 |
arf.depth = 0; |
|
2099 |
arf.recursive_mode = 0; |
|
2100 |
arf.best_ptr = NULL; |
|
2101 |
arf.cur_best_dist = (uint)-1; |
|
2102 |
arf.cur_best_depth = 0xff; |
|
2103 |
arf.best_dist = (uint)-1; |
|
2104 |
arf.best_depth = 0xff; |
|
2105 |
arf.cur_best_tile = 0; |
|
2106 |
arf.best_tile = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2107 |
AiBuildRailRecursive(&arf, p->ai.cur_tile_a, p->ai.cur_dir_a); |
0 | 2108 |
|
2109 |
// Reached destination? |
|
2110 |
if (arf.recursive_mode == 2 && arf.cur_best_depth == 0) { |
|
2111 |
p->ai.state_mode = 255; |
|
2112 |
return; |
|
2113 |
} |
|
2114 |
||
2115 |
// Didn't find anything to build? |
|
2116 |
if (arf.best_ptr == NULL) { |
|
2117 |
// Terraform some |
|
2118 |
for(i=0; i!=5; i++) |
|
2119 |
AiDoTerraformLand(p->ai.cur_tile_a, p->ai.cur_dir_a, 3, 0); |
|
2120 |
||
2121 |
if (++p->ai.state_counter == 21) { |
|
2122 |
p->ai.state_counter = 40; |
|
2123 |
p->ai.state_mode = 1; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2124 |
|
0 | 2125 |
// Ban this tile |
2126 |
AiBanTile(p, p->ai.cur_tile_a, FindFirstBit(GetRailTrackStatus(p->ai.cur_tile_a))); |
|
2127 |
} |
|
2128 |
return; |
|
2129 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2130 |
|
900 | 2131 |
p->ai.cur_tile_a += TileOffsByDir(p->ai.cur_dir_a); |
0 | 2132 |
|
2133 |
if (arf.best_ptr[0]&0x80) { |
|
2134 |
int i; |
|
2135 |
int32 bridge_len = GetBridgeLength(arf.bridge_end_tile, p->ai.cur_tile_a); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2136 |
|
0 | 2137 |
/* Figure out what (rail)bridge type to build |
2138 |
start with best bridge, then go down to worse and worse bridges |
|
2139 |
unnecessary to check for worse bridge (i=0), since AI will always build that. |
|
2140 |
AI is so fucked up that fixing this small thing will probably not solve a thing |
|
2141 |
*/ |
|
2142 |
for(i = 10 + (p->ai.railtype_to_use << 8); i != 0; i--) { |
|
2143 |
if (CheckBridge_Stuff(i, bridge_len)) { |
|
2144 |
int32 cost = DoCommandByTile(arf.bridge_end_tile, p->ai.cur_tile_a, i, DC_AUTO, CMD_BUILD_BRIDGE); |
|
2145 |
if (cost != CMD_ERROR && cost < (p->player_money >> 5)) |
|
2146 |
break; |
|
2147 |
} |
|
2148 |
} |
|
2149 |
||
2150 |
// Build it |
|
2151 |
DoCommandByTile(arf.bridge_end_tile, p->ai.cur_tile_a, i, DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE); |
|
2152 |
||
2153 |
p->ai.cur_tile_a = arf.bridge_end_tile; |
|
2154 |
p->ai.state_counter = 0; |
|
2155 |
} else if (arf.best_ptr[0]&0x40) { |
|
2156 |
// tunnel |
|
2157 |
DoCommandByTile(p->ai.cur_tile_a, p->ai.railtype_to_use, 0, DC_AUTO | DC_EXEC, CMD_BUILD_TUNNEL); |
|
2158 |
p->ai.cur_tile_a = _build_tunnel_endtile; |
|
2159 |
p->ai.state_counter = 0; |
|
2160 |
} else { |
|
2161 |
// rail |
|
2162 |
p->ai.cur_dir_a = arf.best_ptr[1]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2163 |
DoCommandByTile(p->ai.cur_tile_a, p->ai.railtype_to_use, arf.best_ptr[0], |
0 | 2164 |
DC_EXEC | DC_AUTO | DC_NO_WATER | DC_NO_RAIL_OVERLAP, CMD_BUILD_SINGLE_RAIL); |
2165 |
p->ai.state_counter = 0; |
|
2166 |
} |
|
2167 |
||
2168 |
if (arf.best_tile != 0) { |
|
2169 |
for(i=0; i!=2; i++) |
|
2170 |
AiDoTerraformLand(arf.best_tile, arf.best_dir, 3, 0); |
|
2171 |
} |
|
2172 |
} |
|
2173 |
||
2174 |
static bool AiRemoveTileAndGoForward(Player *p) |
|
2175 |
{ |
|
2176 |
byte b; |
|
2177 |
int bit; |
|
2178 |
const byte *ptr; |
|
2179 |
uint tile = p->ai.cur_tile_a; |
|
2180 |
int offs; |
|
2181 |
TileIndex tilenew; |
|
2182 |
||
1035
812f837ee03f
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
2183 |
if (IsTileType(tile, MP_TUNNELBRIDGE)) { |
0 | 2184 |
if (!(_map5[tile] & 0x80)) { |
2185 |
// Clear the tunnel and continue at the other side of it. |
|
2186 |
if (DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR) == CMD_ERROR) |
|
2187 |
return false; |
|
900 | 2188 |
p->ai.cur_tile_a = TILE_MASK(_build_tunnel_endtile - TileOffsByDir(p->ai.cur_dir_a)); |
0 | 2189 |
return true; |
2190 |
} |
|
2191 |
||
2192 |
if (!(_map5[tile] & 0x40)) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2193 |
|
0 | 2194 |
// Check if the bridge points in the right direction. |
2195 |
// This is not really needed the first place AiRemoveTileAndGoForward is called. |
|
2196 |
if ((_map5[tile]&1) != (p->ai.cur_dir_a&1)) |
|
2197 |
return false; |
|
2198 |
||
2199 |
// Find other side of bridge. |
|
900 | 2200 |
offs = TileOffsByDir(p->ai.cur_dir_a); |
0 | 2201 |
do { |
2202 |
tile = TILE_MASK(tile - offs); |
|
2203 |
} while (_map5[tile] & 0x40); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2204 |
|
0 | 2205 |
tilenew = TILE_MASK(tile - offs); |
2206 |
// And clear the bridge. |
|
2207 |
if (DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR) == CMD_ERROR) |
|
2208 |
return false; |
|
2209 |
p->ai.cur_tile_a = tilenew; |
|
2210 |
return true; |
|
2211 |
} |
|
2212 |
} |
|
2213 |
||
2214 |
// Find the railtype at the position. Quit if no rail there. |
|
2215 |
b = GetRailTrackStatus(tile) & _dir_table_3[p->ai.cur_dir_a]; |
|
2216 |
if (b == 0) |
|
2217 |
return false; |
|
2218 |
||
2219 |
// Convert into a bit position that CMD_REMOVE_SINGLE_RAIL expects. |
|
2220 |
bit = FindFirstBit(b); |
|
2221 |
||
2222 |
// Then remove and signals if there are any. |
|
1035
812f837ee03f
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
2223 |
if (IsTileType(tile, MP_RAILWAY) && |
0 | 2224 |
(_map5[tile]&0xC0) == 0x40) { |
2225 |
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_REMOVE_SIGNALS); |
|
2226 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2227 |
|
0 | 2228 |
// And also remove the rail. |
2229 |
if (DoCommandByTile(tile, 0, bit, DC_EXEC, CMD_REMOVE_SINGLE_RAIL) == CMD_ERROR) |
|
2230 |
return false; |
|
2231 |
||
2232 |
// Find the direction at the other edge of the rail. |
|
2233 |
ptr = _ai_table_15[p->ai.cur_dir_a^2]; |
|
2234 |
while (ptr[0] != bit) ptr+=2; |
|
2235 |
p->ai.cur_dir_a = ptr[1] ^ 2; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2236 |
|
0 | 2237 |
// And then also switch tile. |
900 | 2238 |
p->ai.cur_tile_a = TILE_MASK(p->ai.cur_tile_a - TileOffsByDir(p->ai.cur_dir_a)); |
0 | 2239 |
|
2240 |
return true; |
|
2241 |
} |
|
2242 |
||
2243 |
||
2244 |
static void AiBuildRailDestruct(Player *p) |
|
2245 |
{ |
|
2246 |
// Decrease timeout. |
|
2247 |
if (!--p->ai.state_counter) { |
|
2248 |
p->ai.state_mode = 2; |
|
2249 |
p->ai.state_counter = 0; |
|
2250 |
} |
|
2251 |
||
2252 |
// Don't do anything if the destination is already reached. |
|
2253 |
if (p->ai.cur_tile_a == p->ai.start_tile_a) |
|
2254 |
return; |
|
2255 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2256 |
AiRemoveTileAndGoForward(p); |
0 | 2257 |
} |
2258 |
||
2259 |
||
2260 |
static void AiBuildRail(Player *p) |
|
2261 |
{ |
|
2262 |
int i; |
|
2263 |
||
2264 |
if (p->ai.state_mode < 1) { |
|
2265 |
// Construct mode, build new rail. |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2266 |
AiBuildRailConstruct(p); |
0 | 2267 |
} else if (p->ai.state_mode == 1) { |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2268 |
|
0 | 2269 |
// Destruct mode, destroy the rail currently built. |
2270 |
AiBuildRailDestruct(p); |
|
2271 |
} else if (p->ai.state_mode == 2) { |
|
2272 |
||
2273 |
// Terraform some and then try building again. |
|
2274 |
for(i=0; i!=4; i++) |
|
2275 |
AiDoTerraformLand(p->ai.cur_tile_a, p->ai.cur_dir_a, 3, 0); |
|
2276 |
||
2277 |
if (++p->ai.state_counter == 4) { |
|
2278 |
p->ai.state_counter = 0; |
|
2279 |
p->ai.state_mode = 0; |
|
2280 |
} |
|
2281 |
} |
|
2282 |
} |
|
2283 |
||
2284 |
static void AiStateBuildRail(Player *p) |
|
2285 |
{ |
|
2286 |
int num; |
|
2287 |
AiBuildRec *aib; |
|
2288 |
byte cmd; |
|
2289 |
TileIndex tile; |
|
2290 |
int dir; |
|
2291 |
||
2292 |
// time out? |
|
2293 |
if (++p->ai.timeout_counter == 1388) { |
|
2294 |
p->ai.state = AIS_DELETE_RAIL_BLOCKS; |
|
2295 |
return; |
|
2296 |
} |
|
2297 |
||
2298 |
// Currently building a rail between two points? |
|
2299 |
if (p->ai.state_mode != 255) { |
|
2300 |
AiBuildRail(p); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2301 |
|
0 | 2302 |
// Alternate between edges |
2303 |
swap_tile(&p->ai.start_tile_a, &p->ai.start_tile_b); |
|
2304 |
swap_tile(&p->ai.cur_tile_a, &p->ai.cur_tile_b); |
|
2305 |
swap_byte(&p->ai.start_dir_a, &p->ai.start_dir_b); |
|
2306 |
swap_byte(&p->ai.cur_dir_a, &p->ai.cur_dir_b); |
|
2307 |
return; |
|
2308 |
} |
|
2309 |
||
2310 |
// Now, find two new points to build between |
|
2311 |
num = p->ai.num_build_rec; |
|
2312 |
aib = &p->ai.src; |
|
2313 |
||
2314 |
for(;;) { |
|
2315 |
cmd = aib->buildcmd_a; |
|
2316 |
aib->buildcmd_a = 255; |
|
2317 |
if (cmd != 255) break; |
|
2318 |
||
2319 |
cmd = aib->buildcmd_b; |
|
2320 |
aib->buildcmd_b = 255; |
|
2321 |
if (cmd != 255) break; |
|
2322 |
||
2323 |
aib++; |
|
2324 |
if (--num == 0) { |
|
2325 |
p->ai.state = AIS_BUILD_RAIL_VEH; |
|
2326 |
p->ai.state_counter = 0; // timeout |
|
2327 |
return; |
|
2328 |
} |
|
2329 |
} |
|
2330 |
||
2331 |
// Find first edge to build from. |
|
2332 |
tile = AiGetEdgeOfDefaultRailBlock(aib->cur_building_rule, aib->use_tile, cmd&3, &dir); |
|
2333 |
p->ai.start_tile_a = tile; |
|
2334 |
p->ai.cur_tile_a = tile; |
|
2335 |
p->ai.start_dir_a = dir; |
|
2336 |
p->ai.cur_dir_a = dir; |
|
900 | 2337 |
DoCommandByTile(TILE_MASK(tile + TileOffsByDir(dir)), 0, (dir&1)?1:0, DC_EXEC, CMD_REMOVE_SINGLE_RAIL); |
0 | 2338 |
|
2339 |
assert(TILE_MASK(tile) != 0xFF00); |
|
2340 |
||
2341 |
// Find second edge to build to |
|
2342 |
aib = (&p->ai.src) + ((cmd >> 4)&0xF); |
|
2343 |
tile = AiGetEdgeOfDefaultRailBlock(aib->cur_building_rule, aib->use_tile, (cmd>>2)&3, &dir); |
|
2344 |
p->ai.start_tile_b = tile; |
|
2345 |
p->ai.cur_tile_b = tile; |
|
2346 |
p->ai.start_dir_b = dir; |
|
2347 |
p->ai.cur_dir_b = dir; |
|
900 | 2348 |
DoCommandByTile(TILE_MASK(tile + TileOffsByDir(dir)), 0, (dir&1)?1:0, DC_EXEC, CMD_REMOVE_SINGLE_RAIL); |
0 | 2349 |
|
2350 |
assert(TILE_MASK(tile) != 0xFF00); |
|
2351 |
||
2352 |
// And setup state. |
|
2353 |
p->ai.state_mode = 2; |
|
2354 |
p->ai.state_counter = 0; |
|
2355 |
p->ai.banned_tile_count = 0; |
|
2356 |
} |
|
2357 |
||
2358 |
static int AiGetStationIdByDef(TileIndex tile, int id) |
|
2359 |
{ |
|
2360 |
const AiDefaultBlockData *p = _default_rail_track_data[id]->data; |
|
2361 |
while (p->mode != 1) p++; |
|
909
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
2362 |
return _map2[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))]; |
0 | 2363 |
} |
2364 |
||
2365 |
static void AiStateBuildRailVeh(Player *p) |
|
2366 |
{ |
|
2367 |
const AiDefaultBlockData *ptr; |
|
2368 |
TileIndex tile; |
|
2369 |
int i, veh; |
|
2370 |
int cargo; |
|
2371 |
int32 cost; |
|
2372 |
Vehicle *v; |
|
2373 |
uint loco_id; |
|
2374 |
||
2375 |
ptr = _default_rail_track_data[p->ai.src.cur_building_rule]->data; |
|
2376 |
while (ptr->mode != 0) { ptr++; } |
|
2377 |
||
909
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
2378 |
tile = TILE_ADD(p->ai.src.use_tile, ToTileIndexDiff(ptr->tileoffs)); |
0 | 2379 |
|
2380 |
cargo = p->ai.cargo_type; |
|
2381 |
for(i=0;;) { |
|
2382 |
if (p->ai.wagon_list[i] == INVALID_VEHICLE) { |
|
2383 |
veh = _cargoc.ai_railwagon[p->ai.railtype_to_use][cargo]; |
|
2384 |
cost = DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE); |
|
2385 |
if (cost == CMD_ERROR) goto handle_nocash; |
|
2386 |
p->ai.wagon_list[i] = _new_wagon_id; |
|
2387 |
p->ai.wagon_list[i+1] = INVALID_VEHICLE; |
|
2388 |
return; |
|
2389 |
} |
|
2390 |
if (cargo == CT_MAIL) |
|
2391 |
cargo = CT_PASSENGERS; |
|
2392 |
if (++i == p->ai.num_wagons * 2 - 1) |
|
2393 |
break; |
|
2394 |
} |
|
2395 |
||
2396 |
// Which locomotive to build? |
|
1223
228277b970ca
(svn r1727) - Fix: now CmdBuildRailVehicle() behaves correctly if DC_EXEC is set
bjarni
parents:
1217
diff
changeset
|
2397 |
veh = AiChooseTrainToBuild(p->ai.railtype_to_use, p->player_money, (cargo!=CT_PASSENGERS)?1:0, tile); |
0 | 2398 |
if (veh == -1) { |
2399 |
handle_nocash: |
|
2400 |
// after a while, if AI still doesn't have cash, get out of this block by selling the wagons. |
|
2401 |
if (++p->ai.state_counter == 1000) { |
|
2402 |
for(i=0; p->ai.wagon_list[i] != INVALID_VEHICLE; i++) { |
|
2403 |
cost = DoCommandByTile(tile, p->ai.wagon_list[i], 0, DC_EXEC, CMD_SELL_RAIL_WAGON); |
|
2404 |
assert(cost != CMD_ERROR); |
|
2405 |
} |
|
2406 |
p->ai.state = AIS_0; |
|
2407 |
} |
|
2408 |
return; |
|
2409 |
} |
|
2410 |
||
2411 |
// Try to build the locomotive |
|
2412 |
cost = DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE); |
|
2413 |
assert(cost != CMD_ERROR); |
|
2414 |
loco_id = _new_train_id; |
|
2415 |
||
2416 |
// Sell a vehicle if the train is double headed. |
|
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
2417 |
v = GetVehicle(loco_id); |
0 | 2418 |
if (v->next != NULL) { |
2419 |
i = p->ai.wagon_list[p->ai.num_wagons*2-2]; |
|
2420 |
p->ai.wagon_list[p->ai.num_wagons*2-2] = INVALID_VEHICLE; |
|
2421 |
DoCommandByTile(tile, i, 0, DC_EXEC, CMD_SELL_RAIL_WAGON); |
|
2422 |
} |
|
2423 |
||
2424 |
// Move the wagons onto the train |
|
2425 |
for(i=0; p->ai.wagon_list[i] != INVALID_VEHICLE; i++) { |
|
2426 |
DoCommandByTile(tile, p->ai.wagon_list[i] | (loco_id << 16), 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); |
|
2427 |
} |
|
2428 |
||
2429 |
for(i=0; p->ai.order_list_blocks[i] != 0xFF; i++) { |
|
2430 |
AiBuildRec *aib = (&p->ai.src) + p->ai.order_list_blocks[i]; |
|
2431 |
bool is_pass = (p->ai.cargo_type == CT_PASSENGERS || |
|
2432 |
p->ai.cargo_type == CT_MAIL || |
|
2433 |
(_opt.landscape==LT_NORMAL && p->ai.cargo_type == CT_VALUABLES)); |
|
558
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
2434 |
Order order; |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
2435 |
|
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
2436 |
order.type = OT_GOTO_STATION; |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
2437 |
order.flags = 0; |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
2438 |
order.station = AiGetStationIdByDef(aib->use_tile, aib->cur_building_rule); |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
2439 |
|
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
2440 |
if (!is_pass && i == 1) order.flags |= OF_UNLOAD; |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
2441 |
if (p->ai.num_want_fullload != 0 && (is_pass || i == 0)) |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
2442 |
order.flags |= OF_FULL_LOAD; |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
2443 |
|
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
2444 |
DoCommandByTile(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER); |
0 | 2445 |
} |
2446 |
||
2447 |
DoCommandByTile(0, loco_id, 0, DC_EXEC, CMD_START_STOP_TRAIN); |
|
192
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
2448 |
|
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
2449 |
DoCommandByTile(0, loco_id, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT); |
0 | 2450 |
|
2451 |
if (p->ai.num_want_fullload != 0) |
|
2452 |
p->ai.num_want_fullload--; |
|
2453 |
||
2454 |
if (--p->ai.num_loco_to_build != 0) { |
|
2455 |
// p->ai.loco_id = INVALID_VEHICLE; |
|
2456 |
p->ai.wagon_list[0] = INVALID_VEHICLE; |
|
2457 |
} else { |
|
2458 |
p->ai.state = AIS_0; |
|
2459 |
} |
|
2460 |
} |
|
2461 |
||
2462 |
static void AiStateDeleteRailBlocks(Player *p) |
|
2463 |
{ |
|
2464 |
int num; |
|
2465 |
AiBuildRec *aib; |
|
2466 |
const AiDefaultBlockData *b; |
|
2467 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2468 |
num = p->ai.num_build_rec; |
0 | 2469 |
aib = &p->ai.src; |
2470 |
do { |
|
2471 |
if (aib->cur_building_rule != 255) { |
|
2472 |
b = _default_rail_track_data[aib->cur_building_rule]->data; |
|
2473 |
while (b->mode != 4) { |
|
909
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
2474 |
DoCommandByTile(TILE_ADD(aib->use_tile, ToTileIndexDiff(b->tileoffs)), 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); |
0 | 2475 |
b++; |
2476 |
} |
|
2477 |
} |
|
2478 |
} while (++aib,--num); |
|
2479 |
||
2480 |
p->ai.state = AIS_0; |
|
2481 |
} |
|
2482 |
||
2483 |
static bool AiCheckRoadResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo) |
|
2484 |
{ |
|
2485 |
uint values[NUM_CARGO]; |
|
568
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
2486 |
int rad; |
830
36c1366367e4
(svn r1301) -Codechange: _industries finally has FOR_ALL_INDUSTRIES too
truelight
parents:
679
diff
changeset
|
2487 |
|
568
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
2488 |
if (_patches.modified_catchment) { |
830
36c1366367e4
(svn r1301) -Codechange: _industries finally has FOR_ALL_INDUSTRIES too
truelight
parents:
679
diff
changeset
|
2489 |
rad = CA_TRUCK; //Same as CA_BUS at the moment? |
568
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
2490 |
} else { //change that at some point? |
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
2491 |
rad = 4; |
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
2492 |
} |
830
36c1366367e4
(svn r1301) -Codechange: _industries finally has FOR_ALL_INDUSTRIES too
truelight
parents:
679
diff
changeset
|
2493 |
|
0 | 2494 |
for(;;p++) { |
2495 |
if (p->mode == 4) { |
|
2496 |
return true; |
|
2497 |
} else if (p->mode == 1) { |
|
909
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
2498 |
uint tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs)); |
0 | 2499 |
if (cargo & 0x80) { |
568
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
2500 |
GetProductionAroundTiles(values, tile2, 1, 1, rad); |
0 | 2501 |
return values[cargo & 0x7F] != 0; |
2502 |
} else { |
|
568
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
2503 |
GetAcceptanceAroundTiles(values, tile2, 1, 1, rad); |
0 | 2504 |
return (values[cargo]&~7) != 0; |
2505 |
} |
|
2506 |
} |
|
2507 |
} |
|
2508 |
} |
|
2509 |
||
2510 |
static bool _want_road_truck_station; |
|
2511 |
static int32 AiDoBuildDefaultRoadBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag); |
|
2512 |
||
2513 |
// Returns rule and cost |
|
2514 |
static int AiFindBestDefaultRoadBlock(TileIndex tile, byte direction, byte cargo, int32 *cost) |
|
2515 |
{ |
|
2516 |
int i; |
|
2517 |
const AiDefaultRoadBlock *p; |
|
2518 |
||
2519 |
_want_road_truck_station = (cargo & 0x7F) != CT_PASSENGERS; |
|
2520 |
||
2521 |
for(i=0; (p = _road_default_block_data[i]) != NULL; i++) { |
|
2522 |
if (p->dir == direction) { |
|
2523 |
*cost = AiDoBuildDefaultRoadBlock(tile, p->data, 0); |
|
2524 |
if (*cost != CMD_ERROR && AiCheckRoadResources(tile, p->data, cargo)) |
|
2525 |
return i; |
|
2526 |
} |
|
2527 |
} |
|
2528 |
||
2529 |
return -1; |
|
2530 |
} |
|
2531 |
||
2532 |
static int32 AiDoBuildDefaultRoadBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag) |
|
2533 |
{ |
|
2534 |
int32 r; |
|
2535 |
int32 total_cost = 0; |
|
2536 |
Town *t = NULL; |
|
2537 |
int rating = 0; |
|
2538 |
int roadflag = 0; |
|
2539 |
||
2540 |
for(;p->mode != 4;p++) { |
|
909
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
2541 |
uint c = TILE_MASK(tile + ToTileIndexDiff(p->tileoffs)); |
0 | 2542 |
|
2543 |
_cleared_town = NULL; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2544 |
|
0 | 2545 |
if (p->mode == 2) { |
1035
812f837ee03f
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
2546 |
if (IsTileType(c, MP_STREET) && |
0 | 2547 |
(_map5[c]&0xF0)==0 && |
2548 |
(_map5[c]&p->attr)!=0) { |
|
2549 |
roadflag |= 2; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2550 |
|
0 | 2551 |
// all bits are already built? |
2552 |
if ((_map5[c]&p->attr)==p->attr) |
|
2553 |
continue; |
|
2554 |
} |
|
2555 |
||
2556 |
r = DoCommandByTile(c, p->attr, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD); |
|
2557 |
if (r == CMD_ERROR) |
|
2558 |
return CMD_ERROR; |
|
2559 |
total_cost += r; |
|
2560 |
||
2561 |
continue; |
|
2562 |
} |
|
2563 |
||
2564 |
if (p->mode == 0) { |
|
2565 |
// Depot |
|
2566 |
r = DoCommandByTile(c, p->attr, 0, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_ROAD_DEPOT); |
|
2567 |
goto clear_town_stuff; |
|
2568 |
} else if (p->mode == 1) { |
|
2569 |
if (_want_road_truck_station) { |
|
2570 |
// Truck station |
|
1217
59c024cfaf54
(svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents:
1216
diff
changeset
|
2571 |
r = DoCommandByTile(c, p->attr, RS_TRUCK, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_ROAD_STOP); |
0 | 2572 |
} else { |
2573 |
// Bus station |
|
1217
59c024cfaf54
(svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents:
1216
diff
changeset
|
2574 |
r = DoCommandByTile(c, p->attr, RS_BUS, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_ROAD_STOP); |
0 | 2575 |
} |
2576 |
clear_town_stuff:; |
|
2577 |
||
2578 |
if (r == CMD_ERROR) return CMD_ERROR; |
|
2579 |
total_cost += r; |
|
2580 |
||
2581 |
if (_cleared_town != NULL) { |
|
2582 |
if (t != NULL && t != _cleared_town) |
|
2583 |
return CMD_ERROR; |
|
2584 |
t = _cleared_town; |
|
2585 |
rating += _cleared_town_rating; |
|
2586 |
} |
|
2587 |
} else if (p->mode == 3) { |
|
2588 |
if (flag & DC_EXEC) |
|
2589 |
continue; |
|
2590 |
||
2591 |
if (GetTileSlope(c, NULL) != 0) |
|
2592 |
return CMD_ERROR; |
|
2593 |
||
1035
812f837ee03f
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
2594 |
if (!(IsTileType(c, MP_STREET) && (_map5[c] & 0xF0) == 0)) { |
0 | 2595 |
r = DoCommandByTile(c, 0, 0, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_LANDSCAPE_CLEAR); |
2596 |
if (r == CMD_ERROR) return CMD_ERROR; |
|
2597 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2598 |
|
0 | 2599 |
} |
2600 |
} |
|
2601 |
||
2602 |
if (!_want_road_truck_station && !(roadflag&2)) |
|
2603 |
return CMD_ERROR; |
|
2604 |
||
2605 |
if (!(flag & DC_EXEC)) { |
|
2606 |
if (t != NULL && rating > t->ratings[_current_player]) { |
|
2607 |
return CMD_ERROR; |
|
2608 |
} |
|
2609 |
} |
|
2610 |
return total_cost; |
|
2611 |
} |
|
2612 |
||
2613 |
// Make sure the blocks are not too close to each other |
|
2614 |
static bool AiCheckBlockDistances(Player *p, TileIndex tile) |
|
2615 |
{ |
|
2616 |
AiBuildRec *aib; |
|
2617 |
int num; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2618 |
|
0 | 2619 |
num = p->ai.num_build_rec; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2620 |
aib = &p->ai.src; |
0 | 2621 |
|
2622 |
do { |
|
2623 |
if (aib->cur_building_rule != 255) { |
|
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
2624 |
if (DistanceManhattan(aib->use_tile, tile) < 9) |
0 | 2625 |
return false; |
2626 |
} |
|
2627 |
} while (++aib, --num); |
|
2628 |
||
2629 |
return true; |
|
2630 |
} |
|
2631 |
||
2632 |
||
2633 |
static void AiStateBuildDefaultRoadBlocks(Player *p) |
|
2634 |
{ |
|
2635 |
int i, j; |
|
2636 |
AiBuildRec *aib; |
|
2637 |
int rule; |
|
2638 |
int32 cost; |
|
2639 |
||
2640 |
// time out? |
|
2641 |
if (++p->ai.timeout_counter == 1388) { |
|
2642 |
p->ai.state = AIS_DELETE_RAIL_BLOCKS; |
|
2643 |
return; |
|
2644 |
} |
|
2645 |
||
2646 |
// do the following 8 times |
|
2647 |
i = 8; |
|
2648 |
do { |
|
2649 |
// check if we can build the default track |
|
2650 |
aib = &p->ai.src; |
|
2651 |
j = p->ai.num_build_rec; |
|
2652 |
do { |
|
2653 |
// this item has already been built? |
|
2654 |
if (aib->cur_building_rule != 255) |
|
2655 |
continue; |
|
2656 |
||
2657 |
// adjust the coordinate randomly, |
|
2658 |
// to make sure that we find a position. |
|
2659 |
aib->use_tile = AdjustTileCoordRandomly(aib->spec_tile, aib->rand_rng); |
|
2660 |
||
2661 |
// check if the road can be built there. |
|
2662 |
rule = AiFindBestDefaultRoadBlock(aib->use_tile, |
|
2663 |
aib->direction, aib->cargo, &cost); |
|
2664 |
||
2665 |
if (rule == -1) { |
|
2666 |
// cannot build, terraform after a while |
|
2667 |
if (p->ai.state_counter >= 600) { |
|
2668 |
AiDoTerraformLand(aib->use_tile, Random()&3, 3, (int8)p->ai.state_mode); |
|
2669 |
} |
|
2670 |
// also try the other terraform direction |
|
2671 |
if (++p->ai.state_counter >= 1000) { |
|
2672 |
p->ai.state_counter = 0; |
|
2673 |
p->ai.state_mode = -p->ai.state_mode; |
|
2674 |
} |
|
2675 |
} else if (CheckPlayerHasMoney(cost) && AiCheckBlockDistances(p,aib->use_tile)) { |
|
2676 |
int32 r; |
|
2677 |
||
2678 |
// player has money, build it. |
|
2679 |
aib->cur_building_rule = rule; |
|
2680 |
||
2681 |
r = AiDoBuildDefaultRoadBlock( |
|
2682 |
aib->use_tile, |
|
2683 |
_road_default_block_data[rule]->data, |
|
2684 |
DC_EXEC | DC_NO_TOWN_RATING |
|
2685 |
); |
|
2686 |
assert(r != CMD_ERROR); |
|
2687 |
} |
|
2688 |
} while (++aib,--j); |
|
2689 |
} while (--i); |
|
2690 |
||
2691 |
// check if we're done with all of them |
|
2692 |
aib = &p->ai.src; |
|
2693 |
j = p->ai.num_build_rec; |
|
2694 |
do { |
|
2695 |
if (aib->cur_building_rule == 255) |
|
2696 |
return; |
|
2697 |
} while (++aib,--j); |
|
2698 |
||
2699 |
// yep, all are done. switch state to the rail building state. |
|
2700 |
p->ai.state = AIS_BUILD_ROAD; |
|
2701 |
p->ai.state_mode = 255; |
|
2702 |
} |
|
2703 |
||
2704 |
typedef struct { |
|
2705 |
TileIndex final_tile; |
|
2706 |
byte final_dir; |
|
2707 |
byte depth; |
|
2708 |
byte recursive_mode; |
|
2709 |
byte cur_best_dir; |
|
2710 |
byte best_dir; |
|
2711 |
byte cur_best_depth; |
|
2712 |
byte best_depth; |
|
2713 |
uint cur_best_dist; |
|
2714 |
const byte *best_ptr; |
|
2715 |
uint best_dist; |
|
2716 |
TileIndex cur_best_tile, best_tile; |
|
2717 |
TileIndex bridge_end_tile; |
|
2718 |
Player *player; |
|
2719 |
TileInfo ti; |
|
2720 |
} AiRoadFinder; |
|
2721 |
||
2722 |
typedef struct AiRoadEnum { |
|
2723 |
TileIndex dest; |
|
2724 |
TileIndex best_tile; |
|
2725 |
int best_track; |
|
2726 |
uint best_dist; |
|
2727 |
} AiRoadEnum; |
|
2728 |
||
2729 |
static const byte _dir_by_track[] = { |
|
2730 |
0,1,0,1,2,1, 0,0, |
|
2731 |
2,3,3,2,3,0, |
|
2732 |
}; |
|
2733 |
||
2734 |
static void AiBuildRoadRecursive(AiRoadFinder *arf, TileIndex tile, int dir); |
|
2735 |
||
2736 |
static bool AiCheckRoadPathBetter(AiRoadFinder *arf, const byte *p) |
|
2737 |
{ |
|
2738 |
bool better = false; |
|
2739 |
||
2740 |
if (arf->recursive_mode < 1) { |
|
2741 |
// Mode is 0. This means destination has not been found yet. |
|
2742 |
// If the found path is shorter than the current one, remember it. |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2743 |
if (arf->cur_best_dist < arf->best_dist || |
0 | 2744 |
(arf->cur_best_dist == arf->best_dist && arf->cur_best_depth < arf->best_depth)) { |
2745 |
arf->best_depth = arf->cur_best_depth; |
|
2746 |
arf->best_dist = arf->cur_best_dist; |
|
2747 |
arf->best_dir = arf->cur_best_dir; |
|
2748 |
arf->best_ptr = p; |
|
2749 |
arf->best_tile = arf->cur_best_tile; |
|
2750 |
better = true; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2751 |
} |
0 | 2752 |
} else if (arf->recursive_mode > 1) { |
2753 |
// Mode is 2. |
|
2754 |
if (arf->best_dist != 0 || arf->cur_best_depth < arf->best_depth) { |
|
2755 |
arf->best_depth = arf->cur_best_depth; |
|
2756 |
arf->best_dist = 0; |
|
2757 |
arf->best_ptr = p; |
|
2758 |
arf->best_tile = 0; |
|
2759 |
better = true; |
|
2760 |
} |
|
2761 |
} |
|
2762 |
arf->recursive_mode = 0; |
|
2763 |
arf->cur_best_dist = (uint)-1; |
|
2764 |
arf->cur_best_depth = 0xff; |
|
2765 |
||
2766 |
return better; |
|
2767 |
} |
|
2768 |
||
2769 |
||
2770 |
static bool AiEnumFollowRoad(uint tile, AiRoadEnum *a, int track, uint length, byte *state) |
|
2771 |
{ |
|
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
2772 |
uint dist = DistanceManhattan(tile, a->dest); |
0 | 2773 |
uint tile2; |
2774 |
||
2775 |
if (dist <= a->best_dist) { |
|
900 | 2776 |
tile2 = TILE_MASK(tile + TileOffsByDir(_dir_by_track[track])); |
1035
812f837ee03f
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
2777 |
if (IsTileType(tile2, MP_STREET) && |
0 | 2778 |
(_map5[tile2]&0xF0) == 0) { |
2779 |
a->best_dist = dist; |
|
2780 |
a->best_tile = tile; |
|
2781 |
a->best_track = track; |
|
2782 |
} |
|
2783 |
} |
|
2784 |
||
2785 |
return false; |
|
2786 |
} |
|
2787 |
||
2788 |
static const uint16 _ai_road_table_and[4] = { |
|
2789 |
0x1009, |
|
2790 |
0x16, |
|
2791 |
0x520, |
|
2792 |
0x2A00, |
|
2793 |
}; |
|
2794 |
||
410 | 2795 |
static bool AiCheckRoadFinished(Player *p) |
0 | 2796 |
{ |
2797 |
AiRoadEnum are; |
|
2798 |
uint tile; |
|
2799 |
int dir = p->ai.cur_dir_a; |
|
2800 |
uint32 bits; |
|
2801 |
int i; |
|
2802 |
||
2803 |
are.dest = p->ai.cur_tile_b; |
|
900 | 2804 |
tile = TILE_MASK(p->ai.cur_tile_a + TileOffsByDir(dir)); |
0 | 2805 |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
76
diff
changeset
|
2806 |
bits = GetTileTrackStatus(tile, TRANSPORT_ROAD) & _ai_road_table_and[dir]; |
0 | 2807 |
if (bits == 0) { |
2808 |
return false; |
|
2809 |
} |
|
2810 |
||
2811 |
are.best_dist = (uint)-1; |
|
2812 |
||
2813 |
for_each_bit(i, bits) { |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
76
diff
changeset
|
2814 |
FollowTrack(tile, 0x3000 | TRANSPORT_ROAD, _dir_by_track[i], (TPFEnumProc*)AiEnumFollowRoad, NULL, &are); |
0 | 2815 |
} |
2816 |
||
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
2817 |
if (DistanceManhattan(tile, are.dest) <= are.best_dist) |
0 | 2818 |
return false; |
2819 |
||
2820 |
if (are.best_dist == 0) |
|
2821 |
return true; |
|
2822 |
||
2823 |
p->ai.cur_tile_a = are.best_tile; |
|
2824 |
p->ai.cur_dir_a = _dir_by_track[are.best_track]; |
|
2825 |
return false; |
|
2826 |
} |
|
2827 |
||
2828 |
||
2829 |
static bool AiBuildRoadHelper(uint tile, int flags, int type) |
|
2830 |
{ |
|
2831 |
static const byte _road_bits[] = { |
|
2832 |
8+2, |
|
2833 |
1+4, |
|
2834 |
1+8, |
|
2835 |
4+2, |
|
2836 |
1+2, |
|
2837 |
8+4, |
|
2838 |
}; |
|
2839 |
return DoCommandByTile(tile, _road_bits[type], 0, flags, CMD_BUILD_ROAD) != CMD_ERROR; |
|
2840 |
} |
|
2841 |
||
536 | 2842 |
static inline void AiCheckBuildRoadBridgeHere(AiRoadFinder *arf, TileIndex tile, const byte *p) |
0 | 2843 |
{ |
2844 |
TileIndex tile_new; |
|
2845 |
bool flag; |
|
2846 |
||
2847 |
int dir2 = p[0] & 3; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2848 |
|
0 | 2849 |
FindLandscapeHeightByTile(&arf->ti, tile); |
2850 |
if (arf->ti.tileh == _dir_table_1[dir2] || (arf->ti.tileh==0 && arf->ti.z!=0)) { |
|
2851 |
tile_new = tile; |
|
2852 |
// Allow bridges directly over bottom tiles |
|
2853 |
flag = arf->ti.z == 0; |
|
2854 |
for(;;) { |
|
1216
47aa486849d9
(svn r1720) - Fix: signed/unsigned warnings. Tile which is incremented/decremented every time until suitable ground is found is compared to the direction it is going in. If this wraps, return error. For this treat the tile itself as a TileIndexDiff
darkvater
parents:
1209
diff
changeset
|
2855 |
if ((TileIndexDiff)tile_new < -TileOffsByDir(dir2)) return; // Wraping around map, no bridge possible! |
900 | 2856 |
tile_new = TILE_MASK(tile_new + TileOffsByDir(dir2)); |
0 | 2857 |
FindLandscapeHeightByTile(&arf->ti, tile_new); |
2858 |
if (arf->ti.tileh != 0 || arf->ti.type == MP_CLEAR || arf->ti.type == MP_TREES) { |
|
2859 |
// Allow a bridge if either we have a tile that's water, rail or street, |
|
2860 |
// or if we found an up tile. |
|
2861 |
if (!flag) return; |
|
2862 |
break; |
|
2863 |
} |
|
2864 |
if (arf->ti.type != MP_WATER && arf->ti.type != MP_RAILWAY && arf->ti.type != MP_STREET) |
|
2865 |
return; |
|
2866 |
flag = true; |
|
2867 |
} |
|
2868 |
||
2869 |
// Is building a (rail)bridge possible at this place (type doesn't matter)? |
|
2870 |
if (DoCommandByTile(tile_new, tile, 0x8000, DC_AUTO, CMD_BUILD_BRIDGE) == CMD_ERROR) |
|
2871 |
return; |
|
2872 |
AiBuildRoadRecursive(arf, tile_new, dir2); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2873 |
|
0 | 2874 |
// At the bottom depth, check if the new path is better than the old one. |
2875 |
if (arf->depth == 1) { |
|
2876 |
if (AiCheckRoadPathBetter(arf, p)) |
|
2877 |
arf->bridge_end_tile = tile_new; |
|
2878 |
} |
|
2879 |
} |
|
2880 |
} |
|
2881 |
||
536 | 2882 |
static inline void AiCheckBuildRoadTunnelHere(AiRoadFinder *arf, TileIndex tile, const byte *p) |
0 | 2883 |
{ |
2884 |
FindLandscapeHeightByTile(&arf->ti, tile); |
|
2885 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2886 |
if (arf->ti.tileh == _dir_table_2[p[0]&3] && arf->ti.z!=0) { |
0 | 2887 |
int32 cost = DoCommandByTile(tile, 0x200, 0, DC_AUTO, CMD_BUILD_TUNNEL); |
2888 |
||
2889 |
if (cost != CMD_ERROR && cost <= (arf->player->player_money>>4)) { |
|
2890 |
AiBuildRoadRecursive(arf, _build_tunnel_endtile, p[0]&3); |
|
2891 |
if (arf->depth == 1) { |
|
2892 |
AiCheckRoadPathBetter(arf, p); |
|
2893 |
} |
|
2894 |
} |
|
2895 |
} |
|
2896 |
} |
|
2897 |
||
2898 |
||
2899 |
||
2900 |
static void AiBuildRoadRecursive(AiRoadFinder *arf, TileIndex tile, int dir) |
|
2901 |
{ |
|
2902 |
const byte *p; |
|
2903 |
||
900 | 2904 |
tile = TILE_MASK(tile + TileOffsByDir(dir)); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2905 |
|
0 | 2906 |
// Reached destination? |
2907 |
if (tile == arf->final_tile) { |
|
2908 |
if ((arf->final_dir^2) == dir) { |
|
2909 |
arf->recursive_mode = 2; |
|
2910 |
arf->cur_best_depth = arf->depth; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2911 |
} |
0 | 2912 |
return; |
2913 |
} |
|
2914 |
||
2915 |
// Depth too deep? |
|
2916 |
if (arf->depth >= 4) { |
|
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
2917 |
uint dist = DistanceMaxPlusManhattan(tile, arf->final_tile); |
0 | 2918 |
if (dist < arf->cur_best_dist) { |
2919 |
// Store the tile that is closest to the final position. |
|
2920 |
arf->cur_best_dist = dist; |
|
2921 |
arf->cur_best_tile = tile; |
|
2922 |
arf->cur_best_dir = dir; |
|
2923 |
arf->cur_best_depth = arf->depth; |
|
2924 |
} |
|
2925 |
return; |
|
2926 |
} |
|
2927 |
||
2928 |
// Increase recursion depth |
|
2929 |
arf->depth++; |
|
2930 |
||
2931 |
// Grab pointer to list of stuff that is possible to build |
|
2932 |
p = _ai_table_15[dir]; |
|
2933 |
||
2934 |
// Try to build a single rail in all directions. |
|
2935 |
FindLandscapeHeightByTile(&arf->ti, tile); |
|
2936 |
if (arf->ti.z == 0) { |
|
2937 |
p += 6; |
|
2938 |
} else { |
|
2939 |
do { |
|
2940 |
// Make sure that a road can be built here. |
|
2941 |
if (AiBuildRoadHelper(tile, DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, p[0])) { |
|
2942 |
AiBuildRoadRecursive(arf, tile, p[1]); |
|
2943 |
} |
|
2944 |
||
2945 |
// At the bottom depth? |
|
2946 |
if (arf->depth == 1) { |
|
2947 |
AiCheckRoadPathBetter(arf, p); |
|
2948 |
} |
|
2949 |
||
2950 |
p += 2; |
|
2951 |
} while (!(p[0]&0x80)); |
|
2952 |
} |
|
2953 |
||
2954 |
AiCheckBuildRoadBridgeHere(arf, tile, p); |
|
2955 |
AiCheckBuildRoadTunnelHere(arf, tile, p+1); |
|
2956 |
||
2957 |
arf->depth--; |
|
2958 |
} |
|
2959 |
||
2960 |
int sw; |
|
2961 |
||
2962 |
||
2963 |
static void AiBuildRoadConstruct(Player *p) |
|
2964 |
{ |
|
2965 |
AiRoadFinder arf; |
|
2966 |
int i; |
|
2967 |
uint tile; |
|
2968 |
||
2969 |
// Reached destination? |
|
2970 |
if (AiCheckRoadFinished(p)) { |
|
2971 |
p->ai.state_mode = 255; |
|
2972 |
return; |
|
2973 |
} |
|
2974 |
||
2975 |
// Setup recursive finder and call it. |
|
2976 |
arf.player = p; |
|
2977 |
arf.final_tile = p->ai.cur_tile_b; |
|
2978 |
arf.final_dir = p->ai.cur_dir_b; |
|
2979 |
arf.depth = 0; |
|
2980 |
arf.recursive_mode = 0; |
|
2981 |
arf.best_ptr = NULL; |
|
2982 |
arf.cur_best_dist = (uint)-1; |
|
2983 |
arf.cur_best_depth = 0xff; |
|
2984 |
arf.best_dist = (uint)-1; |
|
2985 |
arf.best_depth = 0xff; |
|
2986 |
arf.cur_best_tile = 0; |
|
2987 |
arf.best_tile = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2988 |
AiBuildRoadRecursive(&arf, p->ai.cur_tile_a, p->ai.cur_dir_a); |
0 | 2989 |
|
2990 |
// Reached destination? |
|
2991 |
if (arf.recursive_mode == 2 && arf.cur_best_depth == 0) { |
|
2992 |
p->ai.state_mode = 255; |
|
2993 |
return; |
|
2994 |
} |
|
2995 |
||
2996 |
// Didn't find anything to build? |
|
2997 |
if (arf.best_ptr == NULL) { |
|
2998 |
// Terraform some |
|
2999 |
do_some_terraform: |
|
3000 |
for(i=0; i!=5; i++) |
|
3001 |
AiDoTerraformLand(p->ai.cur_tile_a, p->ai.cur_dir_a, 3, 0); |
|
3002 |
||
3003 |
if (++p->ai.state_counter == 21) { |
|
3004 |
p->ai.state_mode = 1; |
|
3005 |
||
900 | 3006 |
p->ai.cur_tile_a = TILE_MASK(p->ai.cur_tile_a + TileOffsByDir(p->ai.cur_dir_a)); |
0 | 3007 |
p->ai.cur_dir_a ^= 2; |
3008 |
p->ai.state_counter = 0; |
|
3009 |
} |
|
3010 |
return; |
|
3011 |
} |
|
3012 |
||
900 | 3013 |
tile = TILE_MASK(p->ai.cur_tile_a + TileOffsByDir(p->ai.cur_dir_a)); |
0 | 3014 |
|
3015 |
if (arf.best_ptr[0]&0x80) { |
|
3016 |
int i; |
|
3017 |
int32 bridge_len; |
|
3018 |
p->ai.cur_tile_a = arf.bridge_end_tile; |
|
3019 |
bridge_len = GetBridgeLength(tile, p->ai.cur_tile_a); // tile |
|
3020 |
||
3021 |
/* Figure out what (road)bridge type to build |
|
3022 |
start with best bridge, then go down to worse and worse bridges |
|
3023 |
unnecessary to check for worse bridge (i=0), since AI will always build that. |
|
3024 |
AI is so fucked up that fixing this small thing will probably not solve a thing |
|
3025 |
*/ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3026 |
for(i = 10; i != 0; i--) { |
0 | 3027 |
if (CheckBridge_Stuff(i, bridge_len)) { |
3028 |
int32 cost = DoCommandByTile(tile, p->ai.cur_tile_a, i + (0x80 << 8), DC_AUTO, CMD_BUILD_BRIDGE); |
|
3029 |
if (cost != CMD_ERROR && cost < (p->player_money >> 5)) |
|
3030 |
break; |
|
3031 |
} |
|
3032 |
} |
|
3033 |
||
3034 |
// Build it |
|
3035 |
DoCommandByTile(tile, p->ai.cur_tile_a, i + (0x80 << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE); |
|
3036 |
||
3037 |
p->ai.state_counter = 0; |
|
3038 |
} else if (arf.best_ptr[0]&0x40) { |
|
3039 |
// tunnel |
|
3040 |
DoCommandByTile(tile, 0x200, 0, DC_AUTO | DC_EXEC, CMD_BUILD_TUNNEL); |
|
3041 |
p->ai.cur_tile_a = _build_tunnel_endtile; |
|
3042 |
p->ai.state_counter = 0; |
|
3043 |
} else { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3044 |
|
0 | 3045 |
// road |
3046 |
if (!AiBuildRoadHelper(tile, DC_EXEC | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, arf.best_ptr[0])) |
|
3047 |
goto do_some_terraform; |
|
3048 |
||
3049 |
p->ai.cur_dir_a = arf.best_ptr[1]; |
|
3050 |
p->ai.cur_tile_a = tile; |
|
3051 |
p->ai.state_counter = 0; |
|
3052 |
} |
|
3053 |
||
3054 |
if (arf.best_tile != 0) { |
|
3055 |
for(i=0; i!=2; i++) |
|
3056 |
AiDoTerraformLand(arf.best_tile, arf.best_dir, 3, 0); |
|
3057 |
} |
|
3058 |
} |
|
3059 |
||
3060 |
||
3061 |
static void AiBuildRoad(Player *p) |
|
3062 |
{ |
|
3063 |
int i; |
|
3064 |
||
3065 |
if (p->ai.state_mode < 1) { |
|
3066 |
// Construct mode, build new road. |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3067 |
AiBuildRoadConstruct(p); |
0 | 3068 |
} else if (p->ai.state_mode == 1) { |
3069 |
// Destruct mode, not implemented for roads. |
|
3070 |
p->ai.state_mode = 2; |
|
3071 |
p->ai.state_counter = 0; |
|
3072 |
} else if (p->ai.state_mode == 2) { |
|
3073 |
||
3074 |
// Terraform some and then try building again. |
|
3075 |
for(i=0; i!=4; i++) |
|
3076 |
AiDoTerraformLand(p->ai.cur_tile_a, p->ai.cur_dir_a, 3, 0); |
|
3077 |
||
3078 |
if (++p->ai.state_counter == 4) { |
|
3079 |
p->ai.state_counter = 0; |
|
3080 |
p->ai.state_mode = 0; |
|
3081 |
} |
|
3082 |
} |
|
3083 |
} |
|
3084 |
||
3085 |
static TileIndex AiGetRoadBlockEdge(byte rule, TileIndex tile, int *dir) |
|
3086 |
{ |
|
3087 |
const AiDefaultBlockData *p = _road_default_block_data[rule]->data; |
|
3088 |
while (p->mode != 1) p++; |
|
3089 |
*dir = p->attr; |
|
909
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
3090 |
return TILE_ADD(tile, ToTileIndexDiff(p->tileoffs)); |
0 | 3091 |
} |
3092 |
||
3093 |
||
3094 |
static void AiStateBuildRoad(Player *p) |
|
3095 |
{ |
|
3096 |
int num; |
|
3097 |
AiBuildRec *aib; |
|
3098 |
byte cmd; |
|
3099 |
TileIndex tile; |
|
3100 |
int dir; |
|
3101 |
||
3102 |
// time out? |
|
3103 |
if (++p->ai.timeout_counter == 1388) { |
|
3104 |
p->ai.state = AIS_DELETE_ROAD_BLOCKS; |
|
3105 |
return; |
|
3106 |
} |
|
3107 |
||
3108 |
// Currently building a road between two points? |
|
3109 |
if (p->ai.state_mode != 255) { |
|
3110 |
AiBuildRoad(p); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3111 |
|
0 | 3112 |
// Alternate between edges |
3113 |
swap_tile(&p->ai.start_tile_a, &p->ai.start_tile_b); |
|
3114 |
swap_tile(&p->ai.cur_tile_a, &p->ai.cur_tile_b); |
|
3115 |
swap_byte(&p->ai.start_dir_a, &p->ai.start_dir_b); |
|
3116 |
swap_byte(&p->ai.cur_dir_a, &p->ai.cur_dir_b); |
|
3117 |
||
3118 |
sw ^= 1; |
|
3119 |
return; |
|
3120 |
} |
|
3121 |
||
3122 |
// Now, find two new points to build between |
|
3123 |
num = p->ai.num_build_rec; |
|
3124 |
aib = &p->ai.src; |
|
3125 |
||
3126 |
for(;;) { |
|
3127 |
cmd = aib->buildcmd_a; |
|
3128 |
aib->buildcmd_a = 255; |
|
3129 |
if (cmd != 255) break; |
|
3130 |
||
3131 |
aib++; |
|
3132 |
if (--num == 0) { |
|
3133 |
p->ai.state = AIS_BUILD_ROAD_VEHICLES; |
|
3134 |
return; |
|
3135 |
} |
|
3136 |
} |
|
3137 |
||
3138 |
// Find first edge to build from. |
|
3139 |
tile = AiGetRoadBlockEdge(aib->cur_building_rule, aib->use_tile, &dir); |
|
3140 |
p->ai.start_tile_a = tile; |
|
3141 |
p->ai.cur_tile_a = tile; |
|
3142 |
p->ai.start_dir_a = dir; |
|
3143 |
p->ai.cur_dir_a = dir; |
|
3144 |
||
3145 |
// Find second edge to build to |
|
3146 |
aib = (&p->ai.src) + (cmd&0xF); |
|
3147 |
tile = AiGetRoadBlockEdge(aib->cur_building_rule, aib->use_tile, &dir); |
|
3148 |
p->ai.start_tile_b = tile; |
|
3149 |
p->ai.cur_tile_b = tile; |
|
3150 |
p->ai.start_dir_b = dir; |
|
3151 |
p->ai.cur_dir_b = dir; |
|
3152 |
||
3153 |
// And setup state. |
|
3154 |
p->ai.state_mode = 2; |
|
3155 |
p->ai.state_counter = 0; |
|
3156 |
p->ai.banned_tile_count = 0; |
|
3157 |
} |
|
3158 |
||
3159 |
static int AiGetStationIdFromRoadBlock(TileIndex tile, int id) |
|
3160 |
{ |
|
3161 |
const AiDefaultBlockData *p = _road_default_block_data[id]->data; |
|
3162 |
while (p->mode != 1) p++; |
|
909
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
3163 |
return _map2[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))]; |
0 | 3164 |
} |
3165 |
||
3166 |
static void AiStateBuildRoadVehicles(Player *p) |
|
3167 |
{ |
|
3168 |
const AiDefaultBlockData *ptr; |
|
3169 |
uint tile,loco_id; |
|
3170 |
int veh, i; |
|
3171 |
int32 cost; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3172 |
|
0 | 3173 |
ptr = _road_default_block_data[p->ai.src.cur_building_rule]->data; |
3174 |
for(;ptr->mode != 0;ptr++) {} |
|
909
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
3175 |
tile = TILE_ADD(p->ai.src.use_tile, ToTileIndexDiff(ptr->tileoffs)); |
0 | 3176 |
|
1224
8046852a843f
(svn r1728) - Fix: now a player can only build a road vehicle in depots he owns (hacked client protection)
bjarni
parents:
1223
diff
changeset
|
3177 |
veh = AiChooseRoadVehToBuild(p->ai.cargo_type, p->player_money, tile); |
0 | 3178 |
if (veh == -1) { |
3179 |
p->ai.state = AIS_0; |
|
3180 |
return; |
|
3181 |
} |
|
3182 |
||
3183 |
cost = DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH); |
|
3184 |
if (cost == CMD_ERROR) |
|
3185 |
return; |
|
3186 |
||
3187 |
loco_id = _new_roadveh_id; |
|
3188 |
||
3189 |
for(i=0; p->ai.order_list_blocks[i] != 0xFF; i++) { |
|
3190 |
AiBuildRec *aib = (&p->ai.src) + p->ai.order_list_blocks[i]; |
|
3191 |
bool is_pass = (p->ai.cargo_type == CT_PASSENGERS || |
|
3192 |
p->ai.cargo_type == CT_MAIL || |
|
3193 |
(_opt.landscape==LT_NORMAL && p->ai.cargo_type == CT_VALUABLES)); |
|
558
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3194 |
Order order; |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3195 |
|
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3196 |
order.type = OT_GOTO_STATION; |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3197 |
order.flags = 0; |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3198 |
order.station = AiGetStationIdFromRoadBlock(aib->use_tile, aib->cur_building_rule); |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3199 |
|
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3200 |
if (!is_pass && i == 1) order.flags |= OF_UNLOAD; |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3201 |
if (p->ai.num_want_fullload != 0 && (is_pass || i == 0)) |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3202 |
order.flags |= OF_FULL_LOAD; |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3203 |
|
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3204 |
DoCommandByTile(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER); |
0 | 3205 |
} |
3206 |
||
3207 |
DoCommandByTile(0, loco_id, 0, DC_EXEC, CMD_START_STOP_ROADVEH); |
|
192
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
3208 |
|
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
3209 |
DoCommandByTile(0, loco_id, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT); |
0 | 3210 |
|
3211 |
if (p->ai.num_want_fullload != 0) |
|
3212 |
p->ai.num_want_fullload--; |
|
3213 |
||
3214 |
if (--p->ai.num_loco_to_build == 0) { |
|
3215 |
p->ai.state = AIS_0; |
|
3216 |
} |
|
3217 |
} |
|
3218 |
||
3219 |
static void AiStateDeleteRoadBlocks(Player *p) |
|
3220 |
{ |
|
3221 |
int num; |
|
3222 |
AiBuildRec *aib; |
|
3223 |
const AiDefaultBlockData *b; |
|
3224 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3225 |
num = p->ai.num_build_rec; |
0 | 3226 |
aib = &p->ai.src; |
3227 |
do { |
|
3228 |
if (aib->cur_building_rule != 255) { |
|
3229 |
b = _road_default_block_data[aib->cur_building_rule]->data; |
|
3230 |
while (b->mode != 4) { |
|
3231 |
if (b->mode <= 1) { |
|
909
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
3232 |
DoCommandByTile(TILE_ADD(aib->use_tile, ToTileIndexDiff(b->tileoffs)), 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); |
0 | 3233 |
} |
3234 |
b++; |
|
3235 |
} |
|
3236 |
} |
|
3237 |
} while (++aib,--num); |
|
3238 |
||
3239 |
p->ai.state = AIS_0; |
|
3240 |
} |
|
3241 |
||
3242 |
static bool AiCheckIfHangar(Station *st) |
|
3243 |
{ |
|
3244 |
uint tile = st->airport_tile; |
|
3245 |
// HANGAR of airports |
|
3246 |
// 0x20 - hangar large airport (32) |
|
3247 |
// 0x41 - hangar small airport (65) |
|
3248 |
return (_map5[tile] == 32 || _map5[tile] == 65); |
|
3249 |
} |
|
3250 |
||
3251 |
static void AiStateAirportStuff(Player *p) |
|
3252 |
{ |
|
3253 |
Station *st; |
|
3254 |
byte acc_planes; |
|
3255 |
int i; |
|
3256 |
AiBuildRec *aib; |
|
3257 |
byte rule; |
|
3258 |
||
3259 |
// Here we look for an airport we could use instead of building a new |
|
3260 |
// one. If we find such an aiport for any waypoint, |
|
3261 |
// AiStateBuildDefaultAirportBlocks() will kindly skip that one when |
|
3262 |
// building the waypoints. |
|
3263 |
||
3264 |
i = 0; |
|
3265 |
do { |
|
3266 |
// We do this all twice - once for the source (town in the case |
|
3267 |
// of oilrig route) and then for the destination (oilrig in the |
|
3268 |
// case of oilrig route). |
|
3269 |
aib = &p->ai.src + i; |
|
3270 |
||
3271 |
FOR_ALL_STATIONS(st) { |
|
3272 |
// Dismiss ghost stations. |
|
3273 |
if (st->xy == 0) |
|
3274 |
continue; |
|
3275 |
||
3276 |
// Is this an airport? |
|
3277 |
if (!(st->facilities & FACIL_AIRPORT)) |
|
3278 |
continue; |
|
3279 |
||
3280 |
// Do we own the airport? (Oilrigs aren't owned, though.) |
|
3281 |
if (st->owner != OWNER_NONE && st->owner != _current_player) |
|
3282 |
continue; |
|
3283 |
||
3284 |
acc_planes = GetAirport(st->airport_type)->acc_planes; |
|
3285 |
||
3286 |
// Dismiss heliports, unless we are checking an oilrig. |
|
3287 |
if (acc_planes == HELICOPTERS_ONLY && !(p->ai.build_kind == 1 && i == 1)) |
|
3288 |
continue; |
|
3289 |
||
3290 |
// Dismiss country airports if we are doing the other |
|
3291 |
// endpoint of an oilrig route. |
|
3292 |
if (acc_planes == AIRCRAFT_ONLY && (p->ai.build_kind == 1 && i == 0)) |
|
3293 |
continue; |
|
3294 |
||
3295 |
// Dismiss airports too far away. |
|
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
3296 |
if (DistanceMax(st->airport_tile, aib->spec_tile) > aib->rand_rng) |
0 | 3297 |
continue; |
3298 |
||
3299 |
// It's ideal airport, let's take it! |
|
3300 |
||
3301 |
/* XXX: This part is utterly broken - rule should |
|
3302 |
* contain number of the rule appropriate for the |
|
3303 |
* airport type (country, town, ...), see |
|
3304 |
* _airport_default_block_data (rule is just an index |
|
3305 |
* in this array). But the only difference between the |
|
3306 |
* currently existing two rules (rule 0 - town and rule |
|
3307 |
* 1 - country) is the attr field which is used only |
|
3308 |
* when building new airports - and that's irrelevant |
|
3309 |
* for us. So using just about any rule will suffice |
|
3310 |
* here for now (some of the new airport types would be |
|
3311 |
* broken because they will probably need different |
|
3312 |
* tileoff values etc), no matter that |
|
3313 |
* AiCheckIfHangar() makes no sense. --pasky */ |
|
3314 |
if (acc_planes == HELICOPTERS_ONLY) { |
|
3315 |
/* Heliports should have maybe own rulesets but |
|
3316 |
* OTOH we don't want AI to pick them up when |
|
3317 |
* looking for a suitable airport type to build. |
|
3318 |
* So any of rules 0 or 1 would do for now. The |
|
3319 |
* original rule number was 2 but that's a bug |
|
3320 |
* because we have no such rule. */ |
|
3321 |
rule = 1; |
|
3322 |
} else { |
|
3323 |
rule = AiCheckIfHangar(st); |
|
3324 |
} |
|
3325 |
||
3326 |
aib->cur_building_rule = rule; |
|
3327 |
aib->use_tile = st->airport_tile; |
|
3328 |
break; |
|
3329 |
} |
|
3330 |
} while (++i != p->ai.num_build_rec); |
|
3331 |
||
3332 |
p->ai.state = AIS_BUILD_DEFAULT_AIRPORT_BLOCKS; |
|
3333 |
p->ai.state_mode = 255; |
|
3334 |
p->ai.state_counter = 0; |
|
3335 |
} |
|
3336 |
||
3337 |
static int32 AiDoBuildDefaultAirportBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag) |
|
3338 |
{ |
|
3339 |
int32 total_cost = 0, r; |
|
3340 |
||
3341 |
for(;p->mode == 0;p++) { |
|
3342 |
if (!HASBIT(_avail_aircraft, p->attr)) |
|
3343 |
return CMD_ERROR; |
|
909
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
3344 |
r = DoCommandByTile(TILE_MASK(tile + ToTileIndexDiff(p->tileoffs)), p->attr,0,flag | DC_AUTO | DC_NO_WATER,CMD_BUILD_AIRPORT); |
0 | 3345 |
if (r == CMD_ERROR) |
3346 |
return CMD_ERROR; |
|
3347 |
total_cost += r; |
|
3348 |
} |
|
3349 |
||
3350 |
return total_cost; |
|
3351 |
} |
|
3352 |
||
3353 |
static bool AiCheckAirportResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo) |
|
3354 |
{ |
|
3355 |
uint values[NUM_CARGO]; |
|
3356 |
int w,h; |
|
3357 |
uint tile2; |
|
568
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
3358 |
int rad; |
830
36c1366367e4
(svn r1301) -Codechange: _industries finally has FOR_ALL_INDUSTRIES too
truelight
parents:
679
diff
changeset
|
3359 |
|
568
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
3360 |
if (_patches.modified_catchment) { |
830
36c1366367e4
(svn r1301) -Codechange: _industries finally has FOR_ALL_INDUSTRIES too
truelight
parents:
679
diff
changeset
|
3361 |
rad = CA_AIR_LARGE; //I Have NFI what airport the |
568
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
3362 |
} else { //AI is going to build here |
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
3363 |
rad = 4; |
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
3364 |
} |
0 | 3365 |
|
3366 |
for(;p->mode==0;p++) { |
|
909
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
3367 |
tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs)); |
0 | 3368 |
w = _airport_size_x[p->attr]; |
3369 |
h = _airport_size_y[p->attr]; |
|
3370 |
if (cargo & 0x80) { |
|
568
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
3371 |
GetProductionAroundTiles(values, tile2, w, h, rad); |
0 | 3372 |
return values[cargo & 0x7F] != 0; |
3373 |
} else { |
|
568
b0d0df062880
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
3374 |
GetAcceptanceAroundTiles(values, tile2, w, h, rad); |
0 | 3375 |
return values[cargo] >= 8; |
3376 |
} |
|
3377 |
} |
|
3378 |
return true; |
|
3379 |
} |
|
3380 |
||
3381 |
static int AiFindBestDefaultAirportBlock(TileIndex tile, byte cargo, byte heli, int32 *cost) |
|
3382 |
{ |
|
3383 |
int i; |
|
3384 |
const AiDefaultBlockData *p; |
|
3385 |
for(i=0; (p = _airport_default_block_data[i]) != NULL; i++) { |
|
3386 |
// If we are doing a helicopter service, avoid building |
|
3387 |
// airports where they can't land. |
|
3388 |
if (heli && GetAirport(p->attr)->acc_planes == AIRCRAFT_ONLY) |
|
3389 |
continue; |
|
3390 |
||
3391 |
*cost = AiDoBuildDefaultAirportBlock(tile, p, 0); |
|
3392 |
if (*cost != CMD_ERROR && AiCheckAirportResources(tile, p, cargo)) |
|
3393 |
return i; |
|
3394 |
} |
|
3395 |
return -1; |
|
3396 |
} |
|
3397 |
||
3398 |
static void AiStateBuildDefaultAirportBlocks(Player *p) |
|
3399 |
{ |
|
3400 |
int i, j; |
|
3401 |
AiBuildRec *aib; |
|
3402 |
int rule; |
|
3403 |
int32 cost; |
|
3404 |
||
3405 |
// time out? |
|
3406 |
if (++p->ai.timeout_counter == 1388) { |
|
3407 |
p->ai.state = AIS_0; |
|
3408 |
return; |
|
3409 |
} |
|
3410 |
||
3411 |
// do the following 8 times |
|
3412 |
i = 8; |
|
3413 |
do { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3414 |
// check if we can build the default |
0 | 3415 |
aib = &p->ai.src; |
3416 |
j = p->ai.num_build_rec; |
|
3417 |
do { |
|
3418 |
// this item has already been built? |
|
3419 |
if (aib->cur_building_rule != 255) |
|
3420 |
continue; |
|
3421 |
||
3422 |
// adjust the coordinate randomly, |
|
3423 |
// to make sure that we find a position. |
|
3424 |
aib->use_tile = AdjustTileCoordRandomly(aib->spec_tile, aib->rand_rng); |
|
3425 |
||
3426 |
// check if the aircraft stuff can be built there. |
|
3427 |
rule = AiFindBestDefaultAirportBlock(aib->use_tile, aib->cargo, p->ai.build_kind, &cost); |
|
3428 |
||
3429 |
#if 0 |
|
1035
812f837ee03f
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
3430 |
if (!IsTileType(aib->use_tile, MP_STREET) && |
812f837ee03f
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
3431 |
!IsTileType(aib->use_tile, MP_RAILWAY) && |
812f837ee03f
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
3432 |
!IsTileType(aib->use_tile, MP_STATION) |
0 | 3433 |
) { |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3434 |
|
0 | 3435 |
_map_type_and_height[aib->use_tile] = 0xa1; |
3436 |
_map5[aib->use_tile] = 0x80; |
|
3437 |
MarkTileDirtyByTile(aib->use_tile); |
|
3438 |
} |
|
3439 |
#endif |
|
3440 |
// redsq_debug(aib->use_tile); |
|
3441 |
||
3442 |
if (rule == -1) { |
|
3443 |
// cannot build, terraform after a while |
|
3444 |
if (p->ai.state_counter >= 600) { |
|
3445 |
AiDoTerraformLand(aib->use_tile, Random()&3, 3, (int8)p->ai.state_mode); |
|
3446 |
} |
|
3447 |
// also try the other terraform direction |
|
3448 |
if (++p->ai.state_counter >= 1000) { |
|
3449 |
p->ai.state_counter = 0; |
|
3450 |
p->ai.state_mode = -p->ai.state_mode; |
|
3451 |
} |
|
3452 |
} else if (CheckPlayerHasMoney(cost) && AiCheckBlockDistances(p,aib->use_tile)) { |
|
3453 |
// player has money, build it. |
|
3454 |
int32 r; |
|
3455 |
||
3456 |
aib->cur_building_rule = rule; |
|
3457 |
||
3458 |
r = AiDoBuildDefaultAirportBlock( |
|
3459 |
aib->use_tile, |
|
3460 |
_airport_default_block_data[rule], |
|
3461 |
DC_EXEC | DC_NO_TOWN_RATING |
|
3462 |
); |
|
3463 |
assert(r != CMD_ERROR); |
|
3464 |
} |
|
3465 |
} while (++aib,--j); |
|
3466 |
} while (--i); |
|
3467 |
||
3468 |
// check if we're done with all of them |
|
3469 |
aib = &p->ai.src; |
|
3470 |
j = p->ai.num_build_rec; |
|
3471 |
do { |
|
3472 |
if (aib->cur_building_rule == 255) |
|
3473 |
return; |
|
3474 |
} while (++aib,--j); |
|
3475 |
||
3476 |
// yep, all are done. switch state. |
|
3477 |
p->ai.state = AIS_BUILD_AIRCRAFT_VEHICLES; |
|
3478 |
} |
|
3479 |
||
3480 |
static int AiGetStationIdFromAircraftBlock(TileIndex tile, int id) |
|
3481 |
{ |
|
3482 |
const AiDefaultBlockData *p = _airport_default_block_data[id]; |
|
3483 |
while (p->mode != 1) p++; |
|
909
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
3484 |
return _map2[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))]; |
0 | 3485 |
} |
3486 |
||
3487 |
static void AiStateBuildAircraftVehicles(Player *p) |
|
3488 |
{ |
|
3489 |
const AiDefaultBlockData *ptr; |
|
3490 |
uint tile; |
|
3491 |
int veh; |
|
3492 |
int32 cost; |
|
3493 |
int i; |
|
3494 |
uint loco_id; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3495 |
|
0 | 3496 |
ptr = _airport_default_block_data[p->ai.src.cur_building_rule]; |
3497 |
for(;ptr->mode!=0;ptr++) {} |
|
3498 |
||
909
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
3499 |
tile = TILE_ADD(p->ai.src.use_tile, ToTileIndexDiff(ptr->tileoffs)); |
0 | 3500 |
|
3501 |
veh = AiChooseAircraftToBuild(p->player_money, p->ai.build_kind!=0 ? 1 : 0); |
|
3502 |
if (veh == -1) { |
|
3503 |
return; |
|
3504 |
} |
|
3505 |
||
3506 |
cost = DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT); |
|
3507 |
if (cost == CMD_ERROR) |
|
3508 |
return; |
|
3509 |
loco_id = _new_aircraft_id; |
|
3510 |
||
3511 |
for(i=0; p->ai.order_list_blocks[i] != 0xFF; i++) { |
|
3512 |
AiBuildRec *aib = (&p->ai.src) + p->ai.order_list_blocks[i]; |
|
3513 |
bool is_pass = (p->ai.cargo_type == CT_PASSENGERS || p->ai.cargo_type == CT_MAIL); |
|
558
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3514 |
Order order; |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3515 |
|
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3516 |
order.type = OT_GOTO_STATION; |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3517 |
order.flags = 0; |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3518 |
order.station = AiGetStationIdFromAircraftBlock(aib->use_tile, aib->cur_building_rule); |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3519 |
|
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3520 |
if (!is_pass && i == 1) order.flags |= OF_UNLOAD; |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3521 |
if (p->ai.num_want_fullload != 0 && (is_pass || i == 0)) |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3522 |
order.flags |= OF_FULL_LOAD; |
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3523 |
|
1ae2a752501e
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3524 |
DoCommandByTile(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER); |
0 | 3525 |
} |
3526 |
||
3527 |
DoCommandByTile(0, loco_id, 0, DC_EXEC, CMD_START_STOP_AIRCRAFT); |
|
192
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
3528 |
|
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
3529 |
DoCommandByTile(0, loco_id, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT); |
0 | 3530 |
|
3531 |
if (p->ai.num_want_fullload != 0) |
|
3532 |
p->ai.num_want_fullload--; |
|
3533 |
||
3534 |
if (--p->ai.num_loco_to_build == 0) { |
|
3535 |
p->ai.state = AIS_0; |
|
3536 |
} |
|
3537 |
} |
|
3538 |
||
3539 |
static void AiStateCheckShipStuff(Player *p) |
|
3540 |
{ |
|
3541 |
// XXX |
|
3542 |
error("!AiStateCheckShipStuff"); |
|
3543 |
} |
|
3544 |
||
3545 |
static void AiStateBuildDefaultShipBlocks(Player *p) |
|
3546 |
{ |
|
3547 |
// XXX |
|
3548 |
error("!AiStateBuildDefaultShipBlocks"); |
|
3549 |
} |
|
3550 |
||
3551 |
static void AiStateDoShipStuff(Player *p) |
|
3552 |
{ |
|
3553 |
// XXX |
|
3554 |
error("!AiStateDoShipStuff"); |
|
3555 |
} |
|
3556 |
||
3557 |
static void AiStateSellVeh(Player *p) |
|
3558 |
{ |
|
3559 |
Vehicle *v = p->ai.cur_veh; |
|
3560 |
||
3561 |
if (v->owner == _current_player) { |
|
3562 |
if (v->type == VEH_Train) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3563 |
|
1330
5d76a0522a11
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
3564 |
if (!IsTileDepotType(v->tile, TRANSPORT_RAIL) || v->u.rail.track != 0x80 || !(v->vehstatus&VS_STOPPED)) { |
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
539
diff
changeset
|
3565 |
if (v->current_order.type != OT_GOTO_DEPOT) |
0 | 3566 |
DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_TRAIN_GOTO_DEPOT); |
3567 |
goto going_to_depot; |
|
3568 |
} |
|
3569 |
||
3570 |
// Sell whole train |
|
3571 |
DoCommandByTile(v->tile, v->index, 1, DC_EXEC, CMD_SELL_RAIL_WAGON); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3572 |
|
0 | 3573 |
} else if (v->type == VEH_Road) { |
1330
5d76a0522a11
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
3574 |
if (!IsTileDepotType(v->tile, TRANSPORT_ROAD) || v->u.road.state != 254 || !(v->vehstatus&VS_STOPPED)) { |
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
539
diff
changeset
|
3575 |
if (v->current_order.type != OT_GOTO_DEPOT) |
0 | 3576 |
DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT); |
3577 |
goto going_to_depot; |
|
3578 |
} |
|
3579 |
||
3580 |
DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH); |
|
3581 |
} else if (v->type == VEH_Aircraft) { |
|
3582 |
if (!IsAircraftHangarTile(v->tile) && !(v->vehstatus&VS_STOPPED)) { |
|
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
539
diff
changeset
|
3583 |
if (v->current_order.type != OT_GOTO_DEPOT) |
0 | 3584 |
DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SEND_AIRCRAFT_TO_HANGAR); |
3585 |
goto going_to_depot; |
|
3586 |
} |
|
3587 |
||
3588 |
DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT); |
|
3589 |
} else if (v->type == VEH_Ship) { |
|
3590 |
// XXX: not implemented |
|
3591 |
error("!v->type == VEH_Ship"); |
|
3592 |
} |
|
3593 |
} |
|
3594 |
||
3595 |
goto return_to_loop; |
|
3596 |
going_to_depot:; |
|
3597 |
if (++p->ai.state_counter <= 832) |
|
3598 |
return; |
|
3599 |
||
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
539
diff
changeset
|
3600 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
539
diff
changeset
|
3601 |
v->current_order.type = OT_DUMMY; |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
539
diff
changeset
|
3602 |
v->current_order.flags = 0; |
0 | 3603 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
3604 |
} |
|
3605 |
return_to_loop:; |
|
3606 |
p->ai.state = AIS_VEH_LOOP; |
|
3607 |
} |
|
3608 |
||
3609 |
static void AiStateRemoveStation(Player *p) |
|
3610 |
{ |
|
3611 |
// Remove stations that aren't in use by any vehicle |
|
3612 |
byte in_use[256], *used; |
|
1043
44508ede2f92
(svn r1544) -Fix: SwapOrder did not use AssignOrder, which caused the saveroutine to
truelight
parents:
1035
diff
changeset
|
3613 |
const Order *ord; |
0 | 3614 |
Station *st; |
3615 |
uint tile; |
|
3616 |
||
3617 |
// Go to this state when we're done. |
|
3618 |
p->ai.state = AIS_1; |
|
3619 |
||
3620 |
// Get a list of all stations that are in use by a vehicle |
|
3621 |
memset(in_use, 0, sizeof(in_use)); |
|
1024
5e446b5b3ec5
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
959
diff
changeset
|
3622 |
FOR_ALL_ORDERS(ord) { |
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
539
diff
changeset
|
3623 |
if (ord->type == OT_GOTO_STATION) |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
539
diff
changeset
|
3624 |
in_use[ord->station] = 1; |
0 | 3625 |
} |
3626 |
||
3627 |
// Go through all stations and delete those that aren't in use |
|
3628 |
used=in_use; |
|
3629 |
FOR_ALL_STATIONS(st) { |
|
3630 |
if (st->xy != 0 && st->owner == _current_player && !*used && |
|
1217
59c024cfaf54
(svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents:
1216
diff
changeset
|
3631 |
( (st->bus_stops != NULL && (tile = st->bus_stops->xy) != 0) || |
59c024cfaf54
(svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents:
1216
diff
changeset
|
3632 |
(st->truck_stops != NULL && (tile = st->truck_stops->xy)) != 0 || |
0 | 3633 |
(tile = st->train_tile) != 0 || |
3634 |
(tile = st->dock_tile) != 0 || |
|
3635 |
(tile = st->airport_tile) != 0)) { |
|
3636 |
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); |
|
3637 |
} |
|
3638 |
used++; |
|
3639 |
} |
|
3640 |
||
3641 |
} |
|
3642 |
||
3643 |
static void AiRemovePlayerRailOrRoad(Player *p, uint tile) |
|
3644 |
{ |
|
3645 |
byte m5; |
|
3646 |
||
1035
812f837ee03f
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
3647 |
if (IsTileType(tile, MP_RAILWAY)) { |
0 | 3648 |
if (_map_owner[tile] != _current_player) |
3649 |
return; |
|
3650 |
m5 = _map5[tile]; |
|
3651 |
if ((m5&~0x3) != 0xC0) { |
|
3652 |
is_rail_crossing:; |
|
3653 |
m5 = GetRailTrackStatus(tile); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3654 |
|
0 | 3655 |
if (m5 == 0xC || m5 == 0x30) |
3656 |
return; |
|
3657 |
||
3658 |
if (m5&0x25) { |
|
3659 |
pos_0: |
|
3660 |
if (!(GetRailTrackStatus(TILE_MASK(tile-TILE_XY(1,0)))&0x19)) { |
|
3661 |
p->ai.cur_dir_a = 0; |
|
3662 |
p->ai.cur_tile_a = tile; |
|
3663 |
p->ai.state = AIS_REMOVE_SINGLE_RAIL_TILE; |
|
3664 |
return; |
|
3665 |
} |
|
3666 |
} |
|
3667 |
||
3668 |
if (m5&0x2A) { |
|
3669 |
pos_1: |
|
3670 |
if (!(GetRailTrackStatus(TILE_MASK(tile+TILE_XY(0,1)))&0x16)) { |
|
3671 |
p->ai.cur_dir_a = 1; |
|
3672 |
p->ai.cur_tile_a = tile; |
|
3673 |
p->ai.state = AIS_REMOVE_SINGLE_RAIL_TILE; |
|
3674 |
return; |
|
3675 |
} |
|
3676 |
} |
|
3677 |
||
3678 |
if (m5&0x19) { |
|
3679 |
pos_2: |
|
3680 |
if (!(GetRailTrackStatus(TILE_MASK(tile+TILE_XY(1,0)))&0x25)) { |
|
3681 |
p->ai.cur_dir_a = 2; |
|
3682 |
p->ai.cur_tile_a = tile; |
|
3683 |
p->ai.state = AIS_REMOVE_SINGLE_RAIL_TILE; |
|
3684 |
return; |
|
3685 |
} |
|
3686 |
} |
|
3687 |
||
3688 |
if (m5&0x16) { |
|
3689 |
pos_3: |
|
3690 |
if (!(GetRailTrackStatus(TILE_MASK(tile-TILE_XY(0,1)))&0x2A)) { |
|
3691 |
p->ai.cur_dir_a = 3; |
|
3692 |
p->ai.cur_tile_a = tile; |
|
3693 |
p->ai.state = AIS_REMOVE_SINGLE_RAIL_TILE; |
|
3694 |
return; |
|
3695 |
} |
|
3696 |
} |
|
3697 |
} else { |
|
3698 |
static const byte _depot_bits[] = {0x19,0x16,0x25,0x2A}; |
|
3699 |
||
3700 |
m5 &= 3; |
|
900 | 3701 |
if (GetRailTrackStatus(tile + TileOffsByDir(m5)) & _depot_bits[m5]) |
0 | 3702 |
return; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3703 |
|
0 | 3704 |
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); |
3705 |
} |
|
1035
812f837ee03f
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
3706 |
} else if (IsTileType(tile, MP_STREET)) { |
0 | 3707 |
if (_map_owner[tile] != _current_player) |
3708 |
return; |
|
3709 |
||
3710 |
if ( (_map5[tile]&0xF0) == 0x10) |
|
3711 |
goto is_rail_crossing; |
|
3712 |
||
3713 |
if ( (_map5[tile]&0xF0) == 0x20) { |
|
3714 |
int dir; |
|
3715 |
||
3716 |
// Check if there are any stations around. |
|
1035
812f837ee03f
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
3717 |
if (IsTileType(tile + TILE_XY(-1,0), MP_STATION) && |
0 | 3718 |
_map_owner[tile + TILE_XY(-1,0)] == _current_player) |
3719 |
return; |
|
3720 |
||
1035
812f837ee03f
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
3721 |
if (IsTileType(tile + TILE_XY(1,0), MP_STATION) && |
0 | 3722 |
_map_owner[tile + TILE_XY(1,0)] == _current_player) |
3723 |
return; |
|
3724 |
||
1035
812f837ee03f
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
3725 |
if (IsTileType(tile + TILE_XY(0,-1), MP_STATION) && |
0 | 3726 |
_map_owner[tile + TILE_XY(0,-1)] == _current_player) |
3727 |
return; |
|
3728 |
||
1035
812f837ee03f
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
3729 |
if (IsTileType(tile + TILE_XY(0,1), MP_STATION) && |
0 | 3730 |
_map_owner[tile + TILE_XY(0,1)] == _current_player) |
3731 |
return; |
|
3732 |
||
3733 |
dir = _map5[tile] & 3; |
|
3734 |
||
3735 |
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); |
|
3736 |
DoCommandByTile( |
|
900 | 3737 |
TILE_MASK(tile + TileOffsByDir(dir)), |
0 | 3738 |
8 >> (dir ^ 2), |
3739 |
0, |
|
3740 |
DC_EXEC, |
|
3741 |
CMD_REMOVE_ROAD); |
|
3742 |
} |
|
1035
812f837ee03f
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
3743 |
} else if (IsTileType(tile, MP_TUNNELBRIDGE)) { |
0 | 3744 |
byte b; |
3745 |
||
3746 |
if (_map_owner[tile] != _current_player || (_map5[tile] & 0xC6) != 0x80) |
|
3747 |
return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3748 |
|
0 | 3749 |
m5 = 0; |
3750 |
||
3751 |
b = _map5[tile] & 0x21; |
|
3752 |
if (b == 0) goto pos_0; |
|
3753 |
if (b == 1) goto pos_3; |
|
3754 |
if (b == 0x20) goto pos_2; |
|
3755 |
goto pos_1; |
|
3756 |
} |
|
3757 |
} |
|
3758 |
||
3759 |
static void AiStateRemoveTrack(Player *p) |
|
3760 |
{ |
|
3761 |
int num = 1000; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3762 |
|
0 | 3763 |
do { |
3764 |
uint tile = ++p->ai.state_counter; |
|
3765 |
||
3766 |
// Iterated all tiles? |
|
1174
6a5e747f3ba6
(svn r1676) Increase the size of TileIndex and TileIndexDiff to 32bits and adapt the save/load data and some other parts of the code to that change
tron
parents:
1141
diff
changeset
|
3767 |
if (tile >= MapSize()) { |
0 | 3768 |
p->ai.state = AIS_REMOVE_STATION; |
3769 |
return; |
|
3770 |
} |
|
3771 |
||
3772 |
// Remove player stuff in that tile |
|
3773 |
AiRemovePlayerRailOrRoad(p, tile); |
|
3774 |
if (p->ai.state != AIS_REMOVE_TRACK) |
|
3775 |
return; |
|
3776 |
} while (--num); |
|
3777 |
} |
|
3778 |
||
3779 |
static void AiStateRemoveSingleRailTile(Player *p) |
|
3780 |
{ |
|
3781 |
// Remove until we can't remove more. |
|
3782 |
if (!AiRemoveTileAndGoForward(p)) { |
|
3783 |
p->ai.state = AIS_REMOVE_TRACK; |
|
3784 |
} |
|
3785 |
} |
|
3786 |
||
3787 |
static AiStateAction * const _ai_actions[] = { |
|
3788 |
AiCase0, |
|
3789 |
AiCase1, |
|
3790 |
AiStateVehLoop, |
|
3791 |
AiStateCheckReplaceVehicle, |
|
3792 |
AiStateDoReplaceVehicle, |
|
3793 |
AiStateWantNewRoute, |
|
3794 |
||
3795 |
AiStateBuildDefaultRailBlocks, |
|
3796 |
AiStateBuildRail, |
|
3797 |
AiStateBuildRailVeh, |
|
3798 |
AiStateDeleteRailBlocks, |
|
3799 |
||
3800 |
AiStateBuildDefaultRoadBlocks, |
|
3801 |
AiStateBuildRoad, |
|
3802 |
AiStateBuildRoadVehicles, |
|
3803 |
AiStateDeleteRoadBlocks, |
|
3804 |
||
3805 |
AiStateAirportStuff, |
|
3806 |
AiStateBuildDefaultAirportBlocks, |
|
3807 |
AiStateBuildAircraftVehicles, |
|
3808 |
||
3809 |
AiStateCheckShipStuff, |
|
3810 |
AiStateBuildDefaultShipBlocks, |
|
3811 |
AiStateDoShipStuff, |
|
3812 |
||
3813 |
AiStateSellVeh, |
|
3814 |
AiStateRemoveStation, |
|
3815 |
AiStateRemoveTrack, |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3816 |
|
0 | 3817 |
AiStateRemoveSingleRailTile |
3818 |
}; |
|
3819 |
||
3820 |
extern void ShowBuyCompanyDialog(uint player); |
|
3821 |
||
3822 |
static void AiHandleTakeover(Player *p) |
|
3823 |
{ |
|
3824 |
if (p->bankrupt_timeout != 0) { |
|
3825 |
if ((p->bankrupt_timeout-=8) > 0) |
|
3826 |
return; |
|
3827 |
p->bankrupt_timeout = 0; |
|
3828 |
DeleteWindowById(WC_BUY_COMPANY, _current_player); |
|
3829 |
if (_current_player == _local_player) { |
|
3830 |
AskExitToGameMenu(); |
|
3831 |
return; |
|
3832 |
} |
|
3833 |
if (IS_HUMAN_PLAYER(_current_player)) |
|
3834 |
return; |
|
3835 |
} |
|
3836 |
||
3837 |
if (p->bankrupt_asked == 255) |
|
3838 |
return; |
|
3839 |
||
3840 |
{ |
|
3841 |
uint asked = p->bankrupt_asked; |
|
3842 |
Player *pp, *best_pl = NULL; |
|
3843 |
int32 best_val = -1; |
|
3844 |
uint old_p; |
|
3845 |
||
3846 |
// Ask the guy with the highest performance hist. |
|
3847 |
FOR_ALL_PLAYERS(pp) { |
|
3848 |
if (pp->is_active && |
|
3849 |
!(asked&1) && |
|
3850 |
pp->bankrupt_asked == 0 && |
|
3851 |
best_val < pp->old_economy[1].performance_history) { |
|
3852 |
best_val = pp->old_economy[1].performance_history; |
|
3853 |
best_pl = pp; |
|
3854 |
} |
|
3855 |
asked>>=1; |
|
3856 |
} |
|
3857 |
||
3858 |
// Asked all players? |
|
3859 |
if (best_val == -1) { |
|
3860 |
p->bankrupt_asked = 255; |
|
3861 |
return; |
|
3862 |
} |
|
3863 |
||
3864 |
SETBIT(p->bankrupt_asked, best_pl->index); |
|
3865 |
||
3866 |
if (best_pl->index == _local_player) { |
|
3867 |
p->bankrupt_timeout = 4440; |
|
3868 |
ShowBuyCompanyDialog(_current_player); |
|
3869 |
return; |
|
3870 |
} |
|
3871 |
if (IS_HUMAN_PLAYER(best_pl->index)) |
|
3872 |
return; |
|
3873 |
||
3874 |
// Too little money for computer to buy it? |
|
3875 |
if (best_pl->player_money >> 1 >= p->bankrupt_value) { |
|
3876 |
// Computer wants to buy it. |
|
3877 |
old_p = _current_player; |
|
3878 |
_current_player = p->index; |
|
3879 |
DoCommandByTile(0, old_p, 0, DC_EXEC, CMD_BUY_COMPANY); |
|
3880 |
_current_player = old_p; |
|
3881 |
} |
|
3882 |
} |
|
3883 |
} |
|
3884 |
||
3885 |
static void AiAdjustLoan(Player *p) |
|
3886 |
{ |
|
3887 |
int32 base = AiGetBasePrice(p); |
|
3888 |
||
3889 |
if (p->player_money > base * 1400) { |
|
3890 |
// Decrease loan |
|
3891 |
if (p->current_loan != 0) { |
|
3892 |
DoCommandByTile(0, _current_player, 0, DC_EXEC, CMD_DECREASE_LOAN); |
|
3893 |
} |
|
3894 |
} else if (p->player_money < base * 500) { |
|
3895 |
// Increase loan |
|
3896 |
if (p->current_loan < _economy.max_loan && |
|
3897 |
p->num_valid_stat_ent >= 2 && |
|
3898 |
-(p->old_economy[0].expenses+p->old_economy[1].expenses) < base * 60) { |
|
3899 |
DoCommandByTile(0, _current_player, 0, DC_EXEC, CMD_INCREASE_LOAN); |
|
3900 |
} |
|
3901 |
} |
|
3902 |
} |
|
3903 |
||
3904 |
static void AiBuildCompanyHQ(Player *p) |
|
3905 |
{ |
|
3906 |
TileIndex tile; |
|
3907 |
||
3908 |
if (p->location_of_house == 0 && |
|
3909 |
p->last_build_coordinate != 0) { |
|
3910 |
tile = AdjustTileCoordRandomly(p->last_build_coordinate, 8); |
|
3911 |
DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ); |
|
3912 |
} |
|
3913 |
} |
|
3914 |
||
3915 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3916 |
void AiDoGameLoop(Player *p) |
0 | 3917 |
{ |
3918 |
_cur_ai_player = p; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3919 |
|
0 | 3920 |
if (p->bankrupt_asked != 0) { |
3921 |
AiHandleTakeover(p); |
|
3922 |
return; |
|
3923 |
} |
|
3924 |
||
192
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
3925 |
// Ugly hack to make sure the service interval of the AI is good, not looking |
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
3926 |
// to the patch-setting |
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
3927 |
// Also, it takes into account the setting if the service-interval is in days |
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
3928 |
// or in % |
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
3929 |
_ai_service_interval = _patches.servint_ispercent?80:180; |
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
3930 |
|
0 | 3931 |
if (IS_HUMAN_PLAYER(_current_player)) |
3932 |
return; |
|
3933 |
||
3934 |
AiAdjustLoan(p); |
|
3935 |
AiBuildCompanyHQ(p); |
|
3936 |
||
3937 |
if (_opt.diff.competitor_speed == 4) { |
|
3938 |
/* ultraspeed */ |
|
3939 |
_ai_actions[p->ai.state](p); |
|
3940 |
if (p->bankrupt_asked != 0) |
|
3941 |
return; |
|
3942 |
} else if (_opt.diff.competitor_speed != 3) { |
|
3943 |
p->ai.tick++; |
|
3944 |
if (!(p->ai.tick&1)) |
|
3945 |
return; |
|
3946 |
if (_opt.diff.competitor_speed != 2) { |
|
3947 |
if (!(p->ai.tick&2)) |
|
3948 |
return; |
|
3949 |
if (_opt.diff.competitor_speed == 0) { |
|
3950 |
if (!(p->ai.tick&4)) |
|
3951 |
return; |
|
3952 |
} |
|
3953 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3954 |
} |
0 | 3955 |
#if 0 |
3956 |
{ |
|
3957 |
static byte old_state = 99; |
|
3958 |
static bool hasdots = false; |
|
3959 |
char *_ai_state_names[]={ |
|
3960 |
"AiCase0", |
|
3961 |
"AiCase1", |
|
3962 |
"AiStateVehLoop", |
|
3963 |
"AiStateCheckReplaceVehicle", |
|
3964 |
"AiStateDoReplaceVehicle", |
|
3965 |
"AiStateWantNewRoute", |
|
3966 |
"AiStateBuildDefaultRailBlocks", |
|
3967 |
"AiStateBuildRail", |
|
3968 |
"AiStateBuildRailVeh", |
|
3969 |
"AiStateDeleteRailBlocks", |
|
3970 |
"AiStateBuildDefaultRoadBlocks", |
|
3971 |
"AiStateBuildRoad", |
|
3972 |
"AiStateBuildRoadVehicles", |
|
3973 |
"AiStateDeleteRoadBlocks", |
|
3974 |
"AiStateAirportStuff", |
|
3975 |
"AiStateBuildDefaultAirportBlocks", |
|
3976 |
"AiStateBuildAircraftVehicles", |
|
3977 |
"AiStateCheckShipStuff", |
|
3978 |
"AiStateBuildDefaultShipBlocks", |
|
3979 |
"AiStateDoShipStuff", |
|
3980 |
"AiStateSellVeh", |
|
3981 |
"AiStateRemoveStation", |
|
3982 |
"AiStateRemoveTrack", |
|
3983 |
"AiStateRemoveSingleRailTile" |
|
3984 |
}; |
|
3985 |
||
3986 |
if (p->ai.state != old_state) { |
|
3987 |
if (hasdots) |
|
3988 |
printf("\n"); |
|
3989 |
hasdots=false; |
|
3990 |
printf("AiState: %s\n", _ai_state_names[old_state=p->ai.state]); |
|
3991 |
} else { |
|
3992 |
printf("."); |
|
3993 |
hasdots=true; |
|
3994 |
} |
|
3995 |
} |
|
3996 |
#endif |
|
3997 |
||
3998 |
_ai_actions[p->ai.state](p); |
|
3999 |
} |