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