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