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