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