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