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