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