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