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