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