author | rubidium |
Fri, 18 Apr 2008 04:37:06 +0000 | |
changeset 10229 | fba3f9fa44d7 |
parent 10222 | b6919c94cc77 |
child 10230 | bf81856a1101 |
permissions | -rw-r--r-- |
2423
84222db5a01a
(svn r2949) The AI no longer needs to 'cheat' to build aircraft; eg it builds them now from a hangar. Also, to query the price of a new aircraft tile information is not needed
Darkvater
parents:
2381
diff
changeset
|
1 |
/* $Id$ */ |
2381
de9053fe2a2c
(svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents:
2186
diff
changeset
|
2 |
|
de9053fe2a2c
(svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents:
2186
diff
changeset
|
3 |
#include "../../stdafx.h" |
de9053fe2a2c
(svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents:
2186
diff
changeset
|
4 |
#include "../../openttd.h" |
3964
7c1b20ebdb07
(svn r5127) Use IsRoadVehInDepotStopped() and IsAircraftInHangarStopped()
tron
parents:
3948
diff
changeset
|
5 |
#include "../../aircraft.h" |
3196 | 6 |
#include "../../bridge_map.h" |
8615
6b91ca653bad
(svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents:
8612
diff
changeset
|
7 |
#include "../../tile_cmd.h" |
6949
72d11a1e1e60
(svn r9609) -Codechange: Move some function prototypes out of functions.h and into landscape.h, and add a few where they didn't exist.
maedhros
parents:
6900
diff
changeset
|
8 |
#include "../../landscape.h" |
3185 | 9 |
#include "../../rail_map.h" |
3144
426b825578f9
(svn r3763) Adapt to the new 'map accessors go in foo_map.h'-scheme
tron
parents:
3088
diff
changeset
|
10 |
#include "../../road_map.h" |
3964
7c1b20ebdb07
(svn r5127) Use IsRoadVehInDepotStopped() and IsAircraftInHangarStopped()
tron
parents:
3948
diff
changeset
|
11 |
#include "../../roadveh.h" |
3315
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3269
diff
changeset
|
12 |
#include "../../station_map.h" |
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
|
13 |
#include "../../tunnel_map.h" |
8612
6414fc21c2f3
(svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents:
8604
diff
changeset
|
14 |
#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
|
15 |
#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
|
16 |
#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
|
17 |
#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
|
18 |
#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
|
19 |
#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
|
20 |
#include "../../bridge.h" |
8636
2b158acb649c
(svn r11702) -Codechange: move all date related stuff to date*.
rubidium
parents:
8635
diff
changeset
|
21 |
#include "../../date_func.h" |
8579
3efbb430092e
(svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents:
8502
diff
changeset
|
22 |
#include "../../tunnelbridge_map.h" |
8627
448ebf3a8291
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents:
8626
diff
changeset
|
23 |
#include "../../window_func.h" |
8640
1e93b81e96d2
(svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents:
8636
diff
changeset
|
24 |
#include "../../vehicle_func.h" |
8627
448ebf3a8291
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents:
8626
diff
changeset
|
25 |
#include "../../functions.h" |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
26 |
#include "../../saveload.h" |
8750
fdd6054e7bae
(svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents:
8732
diff
changeset
|
27 |
#include "../../player_func.h" |
fdd6054e7bae
(svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents:
8732
diff
changeset
|
28 |
#include "../../player_base.h" |
8766
c86cfa3a7580
(svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents:
8760
diff
changeset
|
29 |
#include "../../settings_type.h" |
2447
47f2b670fb22
(svn r2973) Move a function declaration somewhere where it belongs
tron
parents:
2425
diff
changeset
|
30 |
#include "default.h" |
8894
1e5b2d4380b8
(svn r11968) -Codechange: remove redundant FindLengthOfTunnel(), use GetTunnelBridgeLength() and/or GetOtherTunnelEnd() instead
smatz
parents:
8888
diff
changeset
|
31 |
#include "../../tunnelbridge.h" |
9280
9c03416d26b1
(svn r12488) -Codechange: split order.h into order_base.h and order_func.h.
rubidium
parents:
9251
diff
changeset
|
32 |
#include "../../order_func.h" |
0 | 33 |
|
8760
ce0891c412ce
(svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents:
8750
diff
changeset
|
34 |
#include "../../table/ai_rail.h" |
ce0891c412ce
(svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents:
8750
diff
changeset
|
35 |
|
0 | 36 |
// remove some day perhaps? |
192
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
37 |
static uint _ai_service_interval; |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
38 |
PlayerAI _players_ai[MAX_PLAYERS]; |
0 | 39 |
|
40 |
typedef void AiStateAction(Player *p); |
|
41 |
||
42 |
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
|
43 |
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
|
44 |
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
|
45 |
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
|
46 |
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
|
47 |
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
|
48 |
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
|
49 |
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
|
50 |
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
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
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
|
55 |
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
|
56 |
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
|
57 |
AIS_AIRPORT_STUFF = 14, |
0 | 58 |
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
|
59 |
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
|
60 |
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
|
61 |
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
|
62 |
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
|
63 |
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
|
64 |
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
|
65 |
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
|
66 |
AIS_REMOVE_SINGLE_RAIL_TILE = 23 |
0 | 67 |
}; |
68 |
||
69 |
||
9112
ec6800eaa340
(svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents:
9110
diff
changeset
|
70 |
static inline TrackBits GetRailTrackStatus(TileIndex tile) |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
71 |
{ |
9112
ec6800eaa340
(svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents:
9110
diff
changeset
|
72 |
return TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_RAIL, 0)); |
0 | 73 |
} |
74 |
||
75 |
||
76 |
static void AiCase0(Player *p) |
|
77 |
{ |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
78 |
_players_ai[p->index].state = AIS_REMOVE_TRACK; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
79 |
_players_ai[p->index].state_counter = 0; |
0 | 80 |
} |
81 |
||
82 |
static void AiCase1(Player *p) |
|
83 |
{ |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
84 |
_players_ai[p->index].cur_veh = NULL; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
85 |
_players_ai[p->index].state = AIS_VEH_LOOP; |
0 | 86 |
} |
87 |
||
88 |
static void AiStateVehLoop(Player *p) |
|
89 |
{ |
|
90 |
Vehicle *v; |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
91 |
uint index; |
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
92 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
93 |
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
|
94 |
|
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
95 |
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
|
96 |
if (v->owner != _current_player) continue; |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
97 |
|
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
|
98 |
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
|
99 |
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
|
100 |
(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
|
101 |
v->type == VEH_SHIP) { |
0 | 102 |
/* 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
|
103 |
if (v->type == VEH_TRAIN && v->engine_type < 3 && |
0 | 104 |
(_price.build_railvehicle >> 3) < p->player_money) { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
105 |
_players_ai[p->index].state = AIS_VEH_CHECK_REPLACE_VEHICLE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
106 |
_players_ai[p->index].cur_veh = v; |
0 | 107 |
return; |
108 |
} |
|
109 |
||
110 |
/* not profitable? */ |
|
111 |
if (v->age >= 730 && |
|
9110
55864e1fc263
(svn r12197) -Fix [FS#1788](r12134): show correct last year profit when the train had negative income
smatz
parents:
9107
diff
changeset
|
112 |
v->profit_last_year < _price.station_value * 5 * 256 && |
55864e1fc263
(svn r12197) -Fix [FS#1788](r12134): show correct last year profit when the train had negative income
smatz
parents:
9107
diff
changeset
|
113 |
v->profit_this_year < _price.station_value * 5 * 256) { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
114 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
115 |
_players_ai[p->index].state = AIS_SELL_VEHICLE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
116 |
_players_ai[p->index].cur_veh = v; |
0 | 117 |
return; |
118 |
} |
|
119 |
||
120 |
/* 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
|
121 |
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
|
122 |
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
|
123 |
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
|
124 |
)) { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
125 |
_players_ai[p->index].state = AIS_VEH_CHECK_REPLACE_VEHICLE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
126 |
_players_ai[p->index].cur_veh = v; |
0 | 127 |
return; |
128 |
} |
|
129 |
} |
|
130 |
} |
|
131 |
||
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
132 |
_players_ai[p->index].state = AIS_WANT_NEW_ROUTE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
133 |
_players_ai[p->index].state_counter = 0; |
0 | 134 |
} |
135 |
||
7486
d130c10f4dab
(svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents:
7446
diff
changeset
|
136 |
static EngineID AiChooseTrainToBuild(RailType railtype, Money money, byte flag, TileIndex tile) |
0 | 137 |
{ |
2544 | 138 |
EngineID best_veh_index = INVALID_ENGINE; |
0 | 139 |
byte best_veh_score = 0; |
2544 | 140 |
EngineID i; |
460
f11659d28617
(svn r677) -newgrf: Fix some custom electric trains appearing in maglev depots (pasky).
darkvater
parents:
410
diff
changeset
|
141 |
|
8885
b18a67c2eaeb
(svn r11959) -Codechange: Use macro to loop for specific engine types instead of using specific indexes each time.
peter1138
parents:
8847
diff
changeset
|
142 |
FOR_ALL_ENGINEIDS_OF_TYPE(i, VEH_TRAIN) { |
539 | 143 |
const RailVehicleInfo *rvi = RailVehInfo(i); |
1926
68d60188a22f
(svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents:
1901
diff
changeset
|
144 |
const Engine* e = GetEngine(i); |
460
f11659d28617
(svn r677) -newgrf: Fix some custom electric trains appearing in maglev depots (pasky).
darkvater
parents:
410
diff
changeset
|
145 |
|
6074 | 146 |
if (!IsCompatibleRail(rvi->railtype, railtype) || |
6119
b47985557d1e
(svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents:
6105
diff
changeset
|
147 |
rvi->railveh_type == RAILVEH_WAGON || |
b47985557d1e
(svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents:
6105
diff
changeset
|
148 |
(rvi->railveh_type == RAILVEH_MULTIHEAD && flag & 1) || |
8424
4a488a90ccab
(svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents:
8149
diff
changeset
|
149 |
!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
|
150 |
e->reliability < 0x8A3D) { |
0 | 151 |
continue; |
2529
97c0b71ee7dd
(svn r3058) The default AI may not build dual headed trains under certain circumstances.
tron
parents:
2523
diff
changeset
|
152 |
} |
0 | 153 |
|
8945
8cb92ac1b2fb
(svn r12019) -Codechange: Add support for passenger engine designation for AI-use, NewGRF property 0x08 for trains.
peter1138
parents:
8898
diff
changeset
|
154 |
/* Don't choose an engine designated for passenger use for freight. */ |
8cb92ac1b2fb
(svn r12019) -Codechange: Add support for passenger engine designation for AI-use, NewGRF property 0x08 for trains.
peter1138
parents:
8898
diff
changeset
|
155 |
if (rvi->ai_passenger_only != 0 && flag == 1) continue; |
8cb92ac1b2fb
(svn r12019) -Codechange: Add support for passenger engine designation for AI-use, NewGRF property 0x08 for trains.
peter1138
parents:
8898
diff
changeset
|
156 |
|
8885
b18a67c2eaeb
(svn r11959) -Codechange: Use macro to loop for specific engine types instead of using specific indexes each time.
peter1138
parents:
8847
diff
changeset
|
157 |
CommandCost ret = DoCommand(tile, i, 0, 0, CMD_BUILD_RAIL_VEHICLE); |
7446
1c4d469f986e
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents:
7442
diff
changeset
|
158 |
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
|
159 |
best_veh_score = rvi->ai_rank; |
0 | 160 |
best_veh_index = i; |
161 |
} |
|
460
f11659d28617
(svn r677) -newgrf: Fix some custom electric trains appearing in maglev depots (pasky).
darkvater
parents:
410
diff
changeset
|
162 |
} |
0 | 163 |
|
164 |
return best_veh_index; |
|
165 |
} |
|
166 |
||
7486
d130c10f4dab
(svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents:
7446
diff
changeset
|
167 |
static EngineID AiChooseRoadVehToBuild(CargoID cargo, Money money, TileIndex tile) |
0 | 168 |
{ |
2544 | 169 |
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
|
170 |
int32 best_veh_rating = 0; |
8885
b18a67c2eaeb
(svn r11959) -Codechange: Use macro to loop for specific engine types instead of using specific indexes each time.
peter1138
parents:
8847
diff
changeset
|
171 |
EngineID i; |
b18a67c2eaeb
(svn r11959) -Codechange: Use macro to loop for specific engine types instead of using specific indexes each time.
peter1138
parents:
8847
diff
changeset
|
172 |
|
b18a67c2eaeb
(svn r11959) -Codechange: Use macro to loop for specific engine types instead of using specific indexes each time.
peter1138
parents:
8847
diff
changeset
|
173 |
FOR_ALL_ENGINEIDS_OF_TYPE(i, VEH_ROAD) { |
5029
66c90d197b7b
(svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents:
5028
diff
changeset
|
174 |
const RoadVehicleInfo *rvi = RoadVehInfo(i); |
3034
230ec6a1af82
(svn r3614) turn 2 do-while-loop with 2 induction variables each into canocical for loops
tron
parents:
3033
diff
changeset
|
175 |
const Engine* e = GetEngine(i); |
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
176 |
|
8424
4a488a90ccab
(svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents:
8149
diff
changeset
|
177 |
if (!HasBit(e->player_avail, _current_player) || e->reliability < 0x8A3D) { |
0 | 178 |
continue; |
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
179 |
} |
0 | 180 |
|
5016
63281a3e8548
(svn r7057) -Codechange: Remove hardcoded lists of road vehicles for each cargo type in favour of just checking the cargo type of each vehicle.
peter1138
parents:
5004
diff
changeset
|
181 |
/* Skip vehicles which can't take our cargo type */ |
5031
940b1fec4220
(svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents:
5029
diff
changeset
|
182 |
if (rvi->cargo_type != cargo && !CanRefitTo(i, cargo)) continue; |
5029
66c90d197b7b
(svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents:
5028
diff
changeset
|
183 |
|
66c90d197b7b
(svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents:
5028
diff
changeset
|
184 |
/* Rate and compare the engine by speed & capacity */ |
8885
b18a67c2eaeb
(svn r11959) -Codechange: Use macro to loop for specific engine types instead of using specific indexes each time.
peter1138
parents:
8847
diff
changeset
|
185 |
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
|
186 |
if (rating <= best_veh_rating) continue; |
5016
63281a3e8548
(svn r7057) -Codechange: Remove hardcoded lists of road vehicles for each cargo type in favour of just checking the cargo type of each vehicle.
peter1138
parents:
5004
diff
changeset
|
187 |
|
8885
b18a67c2eaeb
(svn r11959) -Codechange: Use macro to loop for specific engine types instead of using specific indexes each time.
peter1138
parents:
8847
diff
changeset
|
188 |
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
|
189 |
if (CmdFailed(ret)) continue; |
940b1fec4220
(svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents:
5029
diff
changeset
|
190 |
|
940b1fec4220
(svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents:
5029
diff
changeset
|
191 |
/* Add the cost of refitting */ |
7446
1c4d469f986e
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents:
7442
diff
changeset
|
192 |
if (rvi->cargo_type != cargo) ret.AddCost(GetRefitCost(i)); |
1c4d469f986e
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents:
7442
diff
changeset
|
193 |
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
|
194 |
|
66c90d197b7b
(svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents:
5028
diff
changeset
|
195 |
best_veh_rating = rating; |
66c90d197b7b
(svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents:
5028
diff
changeset
|
196 |
best_veh_index = i; |
3034
230ec6a1af82
(svn r3614) turn 2 do-while-loop with 2 induction variables each into canocical for loops
tron
parents:
3033
diff
changeset
|
197 |
} |
0 | 198 |
|
199 |
return best_veh_index; |
|
200 |
} |
|
201 |
||
8898
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
202 |
/** |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
203 |
* Choose aircraft to build. |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
204 |
* @param money current AI money |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
205 |
* @param forbidden forbidden flags - AIR_HELI = 0 (always allowed), AIR_CTOL = 1 (bit 0), AIR_FAST = 2 (bit 1) |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
206 |
* @return EngineID of aircraft to build |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
207 |
*/ |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
208 |
static EngineID AiChooseAircraftToBuild(Money money, byte forbidden) |
0 | 209 |
{ |
2544 | 210 |
EngineID best_veh_index = INVALID_ENGINE; |
7486
d130c10f4dab
(svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents:
7446
diff
changeset
|
211 |
Money best_veh_cost = 0; |
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
212 |
EngineID i; |
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
213 |
|
8885
b18a67c2eaeb
(svn r11959) -Codechange: Use macro to loop for specific engine types instead of using specific indexes each time.
peter1138
parents:
8847
diff
changeset
|
214 |
FOR_ALL_ENGINEIDS_OF_TYPE(i, VEH_AIRCRAFT) { |
3034
230ec6a1af82
(svn r3614) turn 2 do-while-loop with 2 induction variables each into canocical for loops
tron
parents:
3033
diff
changeset
|
215 |
const Engine* e = GetEngine(i); |
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
216 |
|
8424
4a488a90ccab
(svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents:
8149
diff
changeset
|
217 |
if (!HasBit(e->player_avail, _current_player) || e->reliability < 0x8A3D) { |
0 | 218 |
continue; |
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
219 |
} |
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
220 |
|
8898
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
221 |
if ((AircraftVehInfo(i)->subtype & forbidden) != 0) continue; |
0 | 222 |
|
8885
b18a67c2eaeb
(svn r11959) -Codechange: Use macro to loop for specific engine types instead of using specific indexes each time.
peter1138
parents:
8847
diff
changeset
|
223 |
CommandCost ret = DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_AIRCRAFT); |
7446
1c4d469f986e
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents:
7442
diff
changeset
|
224 |
if (CmdSucceeded(ret) && ret.GetCost() <= money && ret.GetCost() >= best_veh_cost) { |
1c4d469f986e
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents:
7442
diff
changeset
|
225 |
best_veh_cost = ret.GetCost(); |
0 | 226 |
best_veh_index = i; |
227 |
} |
|
3034
230ec6a1af82
(svn r3614) turn 2 do-while-loop with 2 induction variables each into canocical for loops
tron
parents:
3033
diff
changeset
|
228 |
} |
0 | 229 |
|
230 |
return best_veh_index; |
|
231 |
} |
|
232 |
||
7486
d130c10f4dab
(svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents:
7446
diff
changeset
|
233 |
static Money AiGetBasePrice(const Player* p) |
0 | 234 |
{ |
7486
d130c10f4dab
(svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents:
7446
diff
changeset
|
235 |
Money base = _price.station_value; |
0 | 236 |
|
237 |
// adjust base price when more expensive vehicles are available |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
238 |
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
|
239 |
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
|
240 |
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
|
241 |
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
|
242 |
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
|
243 |
case RAILTYPE_MAGLEV: base *= 2; break; |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
244 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
245 |
|
0 | 246 |
return base; |
247 |
} |
|
248 |
||
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
249 |
static EngineID AiChooseRoadVehToReplaceWith(const Player* p, const Vehicle* v) |
0 | 250 |
{ |
7486
d130c10f4dab
(svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents:
7446
diff
changeset
|
251 |
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
|
252 |
return AiChooseRoadVehToBuild(v->cargo_type, avail_money, v->tile); |
0 | 253 |
} |
254 |
||
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
255 |
static EngineID AiChooseAircraftToReplaceWith(const Player* p, const Vehicle* v) |
0 | 256 |
{ |
7486
d130c10f4dab
(svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents:
7446
diff
changeset
|
257 |
Money avail_money = p->player_money + v->value; |
8898
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
258 |
|
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
259 |
/* determine forbidden aircraft bits */ |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
260 |
byte forbidden = 0; |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
261 |
const Order *o; |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
262 |
|
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
263 |
FOR_VEHICLE_ORDERS(v, o) { |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
264 |
if (!o->IsValid()) continue; |
9336
3f75a2c5e0d3
(svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents:
9335
diff
changeset
|
265 |
if (!IsValidStationID(o->GetDestination())) continue; |
3f75a2c5e0d3
(svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents:
9335
diff
changeset
|
266 |
const Station *st = GetStation(o->GetDestination()); |
8898
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
267 |
if (!(st->facilities & FACIL_AIRPORT)) continue; |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
268 |
|
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
269 |
AirportFTAClass::Flags flags = st->Airport()->flags; |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
270 |
if (!(flags & AirportFTAClass::AIRPLANES)) forbidden |= AIR_CTOL | AIR_FAST; // no planes for heliports / oil rigs |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
271 |
if (flags & AirportFTAClass::SHORT_STRIP) forbidden |= AIR_FAST; // no fast planes for small airports |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
272 |
} |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
273 |
|
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
|
274 |
return AiChooseAircraftToBuild( |
8898
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
275 |
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
|
276 |
); |
0 | 277 |
} |
278 |
||
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
279 |
static EngineID AiChooseTrainToReplaceWith(const Player* p, const Vehicle* v) |
0 | 280 |
{ |
7486
d130c10f4dab
(svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents:
7446
diff
changeset
|
281 |
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
|
282 |
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
|
283 |
int num = 0; |
0 | 284 |
|
7988
6075538f6111
(svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents:
7986
diff
changeset
|
285 |
while (++num, u->Next() != NULL) { |
6075538f6111
(svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents:
7986
diff
changeset
|
286 |
u = u->Next(); |
0 | 287 |
} |
288 |
||
289 |
// XXX: check if a wagon |
|
1223
e32459dd8000
(svn r1727) - Fix: now CmdBuildRailVehicle() behaves correctly if DC_EXEC is set
bjarni
parents:
1217
diff
changeset
|
290 |
return AiChooseTrainToBuild(v->u.rail.railtype, avail_money, 0, v->tile); |
0 | 291 |
} |
292 |
||
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
293 |
static EngineID AiChooseShipToReplaceWith(const Player* p, const Vehicle* v) |
0 | 294 |
{ |
7951
477c5cef5797
(svn r10942) -Fix [FS#1133]: make the AI not crash when it has ships as the AI does not support them.
rubidium
parents:
7886
diff
changeset
|
295 |
/* Ships are not implemented in this (broken) AI */ |
2544 | 296 |
return INVALID_ENGINE; |
0 | 297 |
} |
298 |
||
299 |
static void AiHandleGotoDepot(Player *p, int cmd) |
|
300 |
{ |
|
9332
2e120d0bd632
(svn r12584) -Codechange: do not access the order type directly.
rubidium
parents:
9296
diff
changeset
|
301 |
if (!_players_ai[p->index].cur_veh->current_order.IsType(OT_GOTO_DEPOT)) |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
302 |
DoCommand(0, _players_ai[p->index].cur_veh->index, 0, DC_EXEC, cmd); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
303 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
304 |
if (++_players_ai[p->index].state_counter <= 1387) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
305 |
_players_ai[p->index].state = AIS_VEH_DO_REPLACE_VEHICLE; |
0 | 306 |
return; |
307 |
} |
|
308 |
||
9332
2e120d0bd632
(svn r12584) -Codechange: do not access the order type directly.
rubidium
parents:
9296
diff
changeset
|
309 |
if (_players_ai[p->index].cur_veh->current_order.IsType(OT_GOTO_DEPOT)) { |
2e120d0bd632
(svn r12584) -Codechange: do not access the order type directly.
rubidium
parents:
9296
diff
changeset
|
310 |
_players_ai[p->index].cur_veh->current_order.MakeDummy(); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
311 |
InvalidateWindow(WC_VEHICLE_VIEW, _players_ai[p->index].cur_veh->index); |
0 | 312 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
313 |
} |
0 | 314 |
|
315 |
static void AiRestoreVehicleOrders(Vehicle *v, BackuppedOrders *bak) |
|
316 |
{ |
|
8818
d8d3812f8f35
(svn r11887) -Fix [FS#1658]: segmentation faults/wrong frees due uninitialized memory in the AI.
rubidium
parents:
8798
diff
changeset
|
317 |
if (bak->order == NULL) return; |
d8d3812f8f35
(svn r11887) -Fix [FS#1658]: segmentation faults/wrong frees due uninitialized memory in the AI.
rubidium
parents:
8798
diff
changeset
|
318 |
|
9332
2e120d0bd632
(svn r12584) -Codechange: do not access the order type directly.
rubidium
parents:
9296
diff
changeset
|
319 |
for (uint i = 0; !bak->order[i].IsType(OT_NOTHING); i++) { |
9335
18d3658adc60
(svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents:
9332
diff
changeset
|
320 |
if (!DoCommandP(0, v->index + (i << 16), bak->order[i].Pack(), NULL, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK)) |
0 | 321 |
break; |
2639 | 322 |
} |
0 | 323 |
} |
324 |
||
325 |
static void AiHandleReplaceTrain(Player *p) |
|
326 |
{ |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
327 |
const Vehicle* v = _players_ai[p->index].cur_veh; |
8149
eb78fb69096f
(svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents:
8141
diff
changeset
|
328 |
BackuppedOrders orderbak; |
2544 | 329 |
EngineID veh; |
0 | 330 |
|
331 |
// wait until the vehicle reaches the depot. |
|
10221
faef62283f5f
(svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents:
10213
diff
changeset
|
332 |
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
|
333 |
AiHandleGotoDepot(p, CMD_SEND_TRAIN_TO_DEPOT); |
0 | 334 |
return; |
335 |
} |
|
336 |
||
337 |
veh = AiChooseTrainToReplaceWith(p, v); |
|
2544 | 338 |
if (veh != INVALID_ENGINE) { |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1927
diff
changeset
|
339 |
TileIndex tile; |
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1927
diff
changeset
|
340 |
|
8149
eb78fb69096f
(svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents:
8141
diff
changeset
|
341 |
BackupVehicleOrders(v, &orderbak); |
0 | 342 |
tile = v->tile; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
343 |
|
7442
a4f7989f27d4
(svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents:
7439
diff
changeset
|
344 |
if (CmdSucceeded(DoCommand(0, v->index, 2, DC_EXEC, CMD_SELL_RAIL_WAGON)) && |
a4f7989f27d4
(svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents:
7439
diff
changeset
|
345 |
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
|
346 |
VehicleID veh = _new_vehicle_id; |
8149
eb78fb69096f
(svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents:
8141
diff
changeset
|
347 |
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
|
348 |
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
|
349 |
|
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3473
diff
changeset
|
350 |
DoCommand(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT); |
0 | 351 |
} |
352 |
} |
|
353 |
} |
|
354 |
||
355 |
static void AiHandleReplaceRoadVeh(Player *p) |
|
356 |
{ |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
357 |
const Vehicle* v = _players_ai[p->index].cur_veh; |
8818
d8d3812f8f35
(svn r11887) -Fix [FS#1658]: segmentation faults/wrong frees due uninitialized memory in the AI.
rubidium
parents:
8798
diff
changeset
|
358 |
BackuppedOrders orderbak; |
2544 | 359 |
EngineID veh; |
0 | 360 |
|
7986
881998b115c2
(svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents:
7951
diff
changeset
|
361 |
if (!v->IsStoppedInDepot()) { |
0 | 362 |
AiHandleGotoDepot(p, CMD_SEND_ROADVEH_TO_DEPOT); |
363 |
return; |
|
364 |
} |
|
365 |
||
366 |
veh = AiChooseRoadVehToReplaceWith(p, v); |
|
2544 | 367 |
if (veh != INVALID_ENGINE) { |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1927
diff
changeset
|
368 |
TileIndex tile; |
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1927
diff
changeset
|
369 |
|
8818
d8d3812f8f35
(svn r11887) -Fix [FS#1658]: segmentation faults/wrong frees due uninitialized memory in the AI.
rubidium
parents:
8798
diff
changeset
|
370 |
BackupVehicleOrders(v, &orderbak); |
0 | 371 |
tile = v->tile; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
372 |
|
7442
a4f7989f27d4
(svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents:
7439
diff
changeset
|
373 |
if (CmdSucceeded(DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH)) && |
a4f7989f27d4
(svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents:
7439
diff
changeset
|
374 |
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
|
375 |
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
|
376 |
|
8818
d8d3812f8f35
(svn r11887) -Fix [FS#1658]: segmentation faults/wrong frees due uninitialized memory in the AI.
rubidium
parents:
8798
diff
changeset
|
377 |
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
|
378 |
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
|
379 |
DoCommand(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT); |
0 | 380 |
} |
381 |
} |
|
382 |
} |
|
383 |
||
384 |
static void AiHandleReplaceAircraft(Player *p) |
|
385 |
{ |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
386 |
const Vehicle* v = _players_ai[p->index].cur_veh; |
8818
d8d3812f8f35
(svn r11887) -Fix [FS#1658]: segmentation faults/wrong frees due uninitialized memory in the AI.
rubidium
parents:
8798
diff
changeset
|
387 |
BackuppedOrders orderbak; |
2544 | 388 |
EngineID veh; |
0 | 389 |
|
7986
881998b115c2
(svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents:
7951
diff
changeset
|
390 |
if (!v->IsStoppedInDepot()) { |
0 | 391 |
AiHandleGotoDepot(p, CMD_SEND_AIRCRAFT_TO_HANGAR); |
392 |
return; |
|
393 |
} |
|
394 |
||
395 |
veh = AiChooseAircraftToReplaceWith(p, v); |
|
2544 | 396 |
if (veh != INVALID_ENGINE) { |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1927
diff
changeset
|
397 |
TileIndex tile; |
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1927
diff
changeset
|
398 |
|
8818
d8d3812f8f35
(svn r11887) -Fix [FS#1658]: segmentation faults/wrong frees due uninitialized memory in the AI.
rubidium
parents:
8798
diff
changeset
|
399 |
BackupVehicleOrders(v, &orderbak); |
0 | 400 |
tile = v->tile; |
401 |
||
7442
a4f7989f27d4
(svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents:
7439
diff
changeset
|
402 |
if (CmdSucceeded(DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT)) && |
a4f7989f27d4
(svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents:
7439
diff
changeset
|
403 |
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
|
404 |
VehicleID veh = _new_vehicle_id; |
8818
d8d3812f8f35
(svn r11887) -Fix [FS#1658]: segmentation faults/wrong frees due uninitialized memory in the AI.
rubidium
parents:
8798
diff
changeset
|
405 |
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
|
406 |
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
|
407 |
|
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3473
diff
changeset
|
408 |
DoCommand(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT); |
0 | 409 |
} |
410 |
} |
|
411 |
} |
|
412 |
||
413 |
static void AiHandleReplaceShip(Player *p) |
|
414 |
{ |
|
7951
477c5cef5797
(svn r10942) -Fix [FS#1133]: make the AI not crash when it has ships as the AI does not support them.
rubidium
parents:
7886
diff
changeset
|
415 |
/* Ships are not implemented in this (broken) AI */ |
0 | 416 |
} |
417 |
||
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
418 |
typedef EngineID CheckReplaceProc(const Player* p, const Vehicle* v); |
0 | 419 |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
420 |
static CheckReplaceProc* const _veh_check_replace_proc[] = { |
0 | 421 |
AiChooseTrainToReplaceWith, |
422 |
AiChooseRoadVehToReplaceWith, |
|
423 |
AiChooseShipToReplaceWith, |
|
424 |
AiChooseAircraftToReplaceWith, |
|
425 |
}; |
|
426 |
||
427 |
typedef void DoReplaceProc(Player *p); |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
428 |
static DoReplaceProc* const _veh_do_replace_proc[] = { |
0 | 429 |
AiHandleReplaceTrain, |
430 |
AiHandleReplaceRoadVeh, |
|
431 |
AiHandleReplaceShip, |
|
432 |
AiHandleReplaceAircraft |
|
433 |
}; |
|
434 |
||
435 |
static void AiStateCheckReplaceVehicle(Player *p) |
|
436 |
{ |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
437 |
const Vehicle* v = _players_ai[p->index].cur_veh; |
0 | 438 |
|
7883
7777bca58ae0
(svn r10756) -Codechange: use vehicle->IsValid in favour of IsValidVehicle(vehicle).
rubidium
parents:
7866
diff
changeset
|
439 |
if (!v->IsValid() || |
2544 | 440 |
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
|
441 |
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
|
442 |
_veh_check_replace_proc[v->type - VEH_TRAIN](p, v) == INVALID_ENGINE) { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
443 |
_players_ai[p->index].state = AIS_VEH_LOOP; |
0 | 444 |
} else { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
445 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
446 |
_players_ai[p->index].state = AIS_VEH_DO_REPLACE_VEHICLE; |
0 | 447 |
} |
448 |
} |
|
449 |
||
450 |
static void AiStateDoReplaceVehicle(Player *p) |
|
451 |
{ |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
452 |
const Vehicle* v = _players_ai[p->index].cur_veh; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
453 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
454 |
_players_ai[p->index].state = AIS_VEH_LOOP; |
0 | 455 |
// vehicle is not owned by the player anymore, something went very wrong. |
7883
7777bca58ae0
(svn r10756) -Codechange: use vehicle->IsValid in favour of IsValidVehicle(vehicle).
rubidium
parents:
7866
diff
changeset
|
456 |
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
|
457 |
_veh_do_replace_proc[v->type - VEH_TRAIN](p); |
0 | 458 |
} |
459 |
||
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
460 |
struct FoundRoute { |
0 | 461 |
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
|
462 |
CargoID cargo; |
0 | 463 |
void *from; |
464 |
void *to; |
|
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
465 |
}; |
0 | 466 |
|
6573 | 467 |
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
|
468 |
{ |
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
|
469 |
return GetRandomTown(); |
0 | 470 |
} |
471 |
||
6573 | 472 |
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
|
473 |
{ |
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
|
474 |
int num = RandomRange(GetMaxIndustryIndex()); |
7886
b02aa3532d1d
(svn r10759) -Codechange: make the industry struct use the pool item class as super class.
rubidium
parents:
7883
diff
changeset
|
475 |
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
|
476 |
|
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 |
return NULL; |
0 | 478 |
} |
479 |
||
480 |
static void AiFindSubsidyIndustryRoute(FoundRoute *fr) |
|
481 |
{ |
|
959
b031d88c76f3
(svn r1451) Fix some of the signed/unsigned comparison warnings
tron
parents:
926
diff
changeset
|
482 |
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
|
483 |
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
|
484 |
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
|
485 |
Industry* from; |
0 | 486 |
TileIndex to_xy; |
487 |
||
488 |
// initially error |
|
489 |
fr->distance = -1; |
|
490 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
491 |
// Randomize subsidy index.. |
0 | 492 |
i = RandomRange(lengthof(_subsidies) * 3); |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
493 |
if (i >= lengthof(_subsidies)) return; |
0 | 494 |
|
495 |
s = &_subsidies[i]; |
|
496 |
||
497 |
// Don't want passengers or mail |
|
498 |
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
|
499 |
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
|
500 |
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
|
501 |
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
|
502 |
s->age > 7) { |
0 | 503 |
return; |
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
504 |
} |
0 | 505 |
fr->cargo = cargo; |
506 |
||
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
507 |
fr->from = from = GetIndustry(s->from); |
0 | 508 |
|
509 |
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
|
510 |
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
|
511 |
|
4015
17f39cad5157
(svn r5228) Suppress MSVC warnings about signed/unsigned comparison
KUDr
parents:
4011
diff
changeset
|
512 |
if (to_tow->population < (cargo == CT_FOOD ? 200U : 900U)) return; // error |
0 | 513 |
fr->to = to_tow; |
514 |
to_xy = to_tow->xy; |
|
515 |
} else { |
|
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
516 |
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
|
517 |
|
0 | 518 |
fr->to = to_ind; |
519 |
to_xy = to_ind->xy; |
|
520 |
} |
|
521 |
||
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
522 |
fr->distance = DistanceManhattan(from->xy, to_xy); |
0 | 523 |
} |
524 |
||
525 |
static void AiFindSubsidyPassengerRoute(FoundRoute *fr) |
|
526 |
{ |
|
527 |
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
|
528 |
const Subsidy* s; |
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
529 |
Town *from, *to; |
0 | 530 |
|
531 |
// initially error |
|
532 |
fr->distance = -1; |
|
533 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
534 |
// Randomize subsidy index.. |
0 | 535 |
i = RandomRange(lengthof(_subsidies) * 3); |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
536 |
if (i >= lengthof(_subsidies)) return; |
0 | 537 |
|
538 |
s = &_subsidies[i]; |
|
539 |
||
540 |
// Only want passengers |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
541 |
if (s->cargo_type != CT_PASSENGERS || s->age > 7) return; |
0 | 542 |
fr->cargo = s->cargo_type; |
543 |
||
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
544 |
fr->from = from = GetTown(s->from); |
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
909
diff
changeset
|
545 |
fr->to = to = GetTown(s->to); |
0 | 546 |
|
547 |
// They must be big enough |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
548 |
if (from->population < 400 || to->population < 400) return; |
0 | 549 |
|
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
550 |
fr->distance = DistanceManhattan(from->xy, to->xy); |
0 | 551 |
} |
552 |
||
553 |
static void AiFindRandomIndustryRoute(FoundRoute *fr) |
|
554 |
{ |
|
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
555 |
Industry* i; |
0 | 556 |
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
|
557 |
CargoID cargo; |
0 | 558 |
|
559 |
// initially error |
|
560 |
fr->distance = -1; |
|
561 |
||
562 |
r = Random(); |
|
563 |
||
564 |
// pick a source |
|
565 |
fr->from = i = AiFindRandomIndustry(); |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
566 |
if (i == NULL) return; |
0 | 567 |
|
568 |
// pick a random produced cargo |
|
8141
c052dc0dc029
(svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents:
7988
diff
changeset
|
569 |
cargo = i->produced_cargo[0]; |
c052dc0dc029
(svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents:
7988
diff
changeset
|
570 |
if (r & 1 && i->produced_cargo[1] != CT_INVALID) cargo = i->produced_cargo[1]; |
0 | 571 |
|
572 |
fr->cargo = cargo; |
|
573 |
||
574 |
// 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
|
575 |
if (cargo == CT_INVALID || cargo == CT_PASSENGERS) return; |
0 | 576 |
|
577 |
if (cargo != CT_GOODS && cargo != CT_FOOD) { |
|
578 |
// 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
|
579 |
Industry* i2 = AiFindRandomIndustry(); |
8141
c052dc0dc029
(svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents:
7988
diff
changeset
|
580 |
if (i2 == NULL || i == i2 || |
c052dc0dc029
(svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents:
7988
diff
changeset
|
581 |
(i2->accepts_cargo[0] != cargo && |
c052dc0dc029
(svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents:
7988
diff
changeset
|
582 |
i2->accepts_cargo[1] != cargo && |
c052dc0dc029
(svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents:
7988
diff
changeset
|
583 |
i2->accepts_cargo[2] != cargo)) { |
0 | 584 |
return; |
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
585 |
} |
0 | 586 |
|
587 |
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
|
588 |
fr->distance = DistanceManhattan(i->xy, i2->xy); |
0 | 589 |
} else { |
590 |
// 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
|
591 |
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
|
592 |
|
4015
17f39cad5157
(svn r5228) Suppress MSVC warnings about signed/unsigned comparison
KUDr
parents:
4011
diff
changeset
|
593 |
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
|
594 |
|
0 | 595 |
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
|
596 |
fr->distance = DistanceManhattan(i->xy, t->xy); |
0 | 597 |
} |
598 |
} |
|
599 |
||
600 |
static void AiFindRandomPassengerRoute(FoundRoute *fr) |
|
601 |
{ |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
602 |
Town* source; |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
603 |
Town* dest; |
0 | 604 |
|
605 |
// initially error |
|
606 |
fr->distance = -1; |
|
607 |
||
608 |
fr->from = source = AiFindRandomTown(); |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
609 |
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
|
610 |
|
0 | 611 |
fr->to = dest = AiFindRandomTown(); |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
612 |
if (dest == NULL || source == dest || dest->population < 400) return; |
0 | 613 |
|
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
614 |
fr->distance = DistanceManhattan(source->xy, dest->xy); |
0 | 615 |
} |
616 |
||
617 |
// Warn: depends on 'xy' being the first element in both Town and Industry |
|
618 |
#define GET_TOWN_OR_INDUSTRY_TILE(p) (((Town*)(p))->xy) |
|
619 |
||
620 |
static bool AiCheckIfRouteIsGood(Player *p, FoundRoute *fr, byte bitmask) |
|
621 |
{ |
|
622 |
TileIndex from_tile, to_tile; |
|
623 |
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
|
624 |
int dist; |
0 | 625 |
uint same_station = 0; |
626 |
||
627 |
from_tile = GET_TOWN_OR_INDUSTRY_TILE(fr->from); |
|
628 |
to_tile = GET_TOWN_OR_INDUSTRY_TILE(fr->to); |
|
629 |
||
630 |
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
|
631 |
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
|
632 |
int cur; |
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
633 |
|
4346
3f00094f2670
(svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents:
4344
diff
changeset
|
634 |
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
|
635 |
cur = DistanceMax(from_tile, st->xy); |
0 | 636 |
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
|
637 |
cur = DistanceMax(to_tile, st->xy); |
0 | 638 |
if (cur < dist) dist = cur; |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
639 |
if (to_tile == from_tile && st->xy == to_tile) same_station++; |
0 | 640 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
641 |
|
0 | 642 |
// To prevent the AI from building ten busstations in the same town, do some calculations |
643 |
// 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
|
644 |
if ((bitmask == 2 || bitmask == 4) && |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
645 |
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
|
646 |
((Town*)fr->from)->population < same_station * 350) { |
0 | 647 |
return false; |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
648 |
} |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
649 |
|
7354
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
650 |
/* Requiring distance to nearest station to be always under 37 tiles may be suboptimal, |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
651 |
* Especially for longer aircraft routes that start and end pretty at any arbitrary place on map |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
652 |
* While it may be nice for AI to cluster their creations together, hardcoded limit is not ideal. |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
653 |
* If AI will randomly start on some isolated spot, it will never get out of there. |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
654 |
* AI will have chance of randomly rejecting routes further than 37 tiles from their network, |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
655 |
* so there will be some attempt to cluster the network together */ |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
656 |
|
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
657 |
/* Random value between 37 and 292. Low values are exponentially more likely |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
658 |
* With 50% chance the value will be under 52 tiles */ |
7362
4e2ac4d15263
(svn r10106) -Fix (r10098): there's always a compiler complaining about something...
rubidium
parents:
7354
diff
changeset
|
659 |
int min_distance = 36 + (1 << (Random() % 9)); // 0..8 |
7354
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
660 |
|
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
661 |
/* Make sure distance to closest station is < min_distance tiles. */ |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
662 |
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
|
663 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
664 |
if (_players_ai[p->index].route_type_mask != 0 && |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
665 |
!(_players_ai[p->index].route_type_mask & bitmask) && |
8463
3920ac0ab803
(svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents:
8450
diff
changeset
|
666 |
!Chance16(1, 5)) { |
0 | 667 |
return false; |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
668 |
} |
0 | 669 |
|
670 |
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
|
671 |
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
|
672 |
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
|
673 |
|
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
674 |
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
|
675 |
to->pct_pass_transported > 0x99) { |
0 | 676 |
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
|
677 |
} |
0 | 678 |
|
679 |
// 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
|
680 |
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
|
681 |
to->ratings[_current_player] < -100) { |
0 | 682 |
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
|
683 |
} |
0 | 684 |
} else { |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
685 |
const Industry* i = (const Industry*)fr->from; |
8141
c052dc0dc029
(svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents:
7988
diff
changeset
|
686 |
|
c052dc0dc029
(svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents:
7988
diff
changeset
|
687 |
if (i->last_month_pct_transported[fr->cargo != i->produced_cargo[0]] > 0x99 || |
c052dc0dc029
(svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents:
7988
diff
changeset
|
688 |
i->last_month_production[fr->cargo != i->produced_cargo[0]] == 0) { |
0 | 689 |
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
|
690 |
} |
0 | 691 |
} |
692 |
||
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
693 |
_players_ai[p->index].route_type_mask |= bitmask; |
0 | 694 |
return true; |
695 |
} |
|
696 |
||
697 |
static byte AiGetDirectionBetweenTiles(TileIndex a, TileIndex b) |
|
698 |
{ |
|
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
|
699 |
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
|
700 |
if (TileY(a) >= TileY(b)) i ^= 3; |
0 | 701 |
return i; |
702 |
} |
|
703 |
||
704 |
static TileIndex AiGetPctTileBetween(TileIndex a, TileIndex b, byte pct) |
|
705 |
{ |
|
1981 | 706 |
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
|
707 |
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
|
708 |
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
|
709 |
); |
0 | 710 |
} |
711 |
||
712 |
static void AiWantLongIndustryRoute(Player *p) |
|
713 |
{ |
|
714 |
int i; |
|
715 |
FoundRoute fr; |
|
716 |
||
717 |
i = 60; |
|
2952 | 718 |
for (;;) { |
0 | 719 |
// look for one from the subsidy list |
720 |
AiFindSubsidyIndustryRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
721 |
if (IsInsideMM(fr.distance, 60, 90 + 1)) break; |
0 | 722 |
|
723 |
// try a random one |
|
724 |
AiFindRandomIndustryRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
725 |
if (IsInsideMM(fr.distance, 60, 90 + 1)) break; |
0 | 726 |
|
727 |
// only test 60 times |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
728 |
if (--i == 0) return; |
0 | 729 |
} |
730 |
||
2639 | 731 |
if (!AiCheckIfRouteIsGood(p, &fr, 1)) return; |
0 | 732 |
|
733 |
// Fill the source field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
734 |
_players_ai[p->index].dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
735 |
_players_ai[p->index].src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
736 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
737 |
_players_ai[p->index].src.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
738 |
_players_ai[p->index].src.rand_rng = 9; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
739 |
_players_ai[p->index].src.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
740 |
_players_ai[p->index].src.unk6 = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
741 |
_players_ai[p->index].src.unk7 = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
742 |
_players_ai[p->index].src.buildcmd_a = 0x24; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
743 |
_players_ai[p->index].src.buildcmd_b = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
744 |
_players_ai[p->index].src.direction = AiGetDirectionBetweenTiles( |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
745 |
_players_ai[p->index].src.spec_tile, |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
746 |
_players_ai[p->index].dst.spec_tile |
2639 | 747 |
); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
748 |
_players_ai[p->index].src.cargo = fr.cargo | 0x80; |
0 | 749 |
|
750 |
// Fill the dest field |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
751 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
752 |
_players_ai[p->index].dst.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
753 |
_players_ai[p->index].dst.rand_rng = 9; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
754 |
_players_ai[p->index].dst.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
755 |
_players_ai[p->index].dst.unk6 = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
756 |
_players_ai[p->index].dst.unk7 = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
757 |
_players_ai[p->index].dst.buildcmd_a = 0x34; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
758 |
_players_ai[p->index].dst.buildcmd_b = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
759 |
_players_ai[p->index].dst.direction = AiGetDirectionBetweenTiles( |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
760 |
_players_ai[p->index].dst.spec_tile, |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
761 |
_players_ai[p->index].src.spec_tile |
2639 | 762 |
); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
763 |
_players_ai[p->index].dst.cargo = fr.cargo; |
0 | 764 |
|
765 |
// Fill middle field 1 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
766 |
_players_ai[p->index].mid1.spec_tile = AiGetPctTileBetween( |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
767 |
_players_ai[p->index].src.spec_tile, |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
768 |
_players_ai[p->index].dst.spec_tile, |
2639 | 769 |
0x55 |
770 |
); |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
771 |
_players_ai[p->index].mid1.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
772 |
_players_ai[p->index].mid1.rand_rng = 6; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
773 |
_players_ai[p->index].mid1.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
774 |
_players_ai[p->index].mid1.unk6 = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
775 |
_players_ai[p->index].mid1.unk7 = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
776 |
_players_ai[p->index].mid1.buildcmd_a = 0x30; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
777 |
_players_ai[p->index].mid1.buildcmd_b = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
778 |
_players_ai[p->index].mid1.direction = _players_ai[p->index].src.direction; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
779 |
_players_ai[p->index].mid1.cargo = fr.cargo; |
0 | 780 |
|
781 |
// Fill middle field 2 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
782 |
_players_ai[p->index].mid2.spec_tile = AiGetPctTileBetween( |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
783 |
_players_ai[p->index].src.spec_tile, |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
784 |
_players_ai[p->index].dst.spec_tile, |
2639 | 785 |
0xAA |
786 |
); |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
787 |
_players_ai[p->index].mid2.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
788 |
_players_ai[p->index].mid2.rand_rng = 6; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
789 |
_players_ai[p->index].mid2.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
790 |
_players_ai[p->index].mid2.unk6 = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
791 |
_players_ai[p->index].mid2.unk7 = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
792 |
_players_ai[p->index].mid2.buildcmd_a = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
793 |
_players_ai[p->index].mid2.buildcmd_b = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
794 |
_players_ai[p->index].mid2.direction = _players_ai[p->index].dst.direction; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
795 |
_players_ai[p->index].mid2.cargo = fr.cargo; |
0 | 796 |
|
797 |
// Fill common fields |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
798 |
_players_ai[p->index].cargo_type = fr.cargo; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
799 |
_players_ai[p->index].num_wagons = 3; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
800 |
_players_ai[p->index].build_kind = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
801 |
_players_ai[p->index].num_build_rec = 4; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
802 |
_players_ai[p->index].num_loco_to_build = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
803 |
_players_ai[p->index].num_want_fullload = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
804 |
_players_ai[p->index].wagon_list[0] = INVALID_VEHICLE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
805 |
_players_ai[p->index].order_list_blocks[0] = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
806 |
_players_ai[p->index].order_list_blocks[1] = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
807 |
_players_ai[p->index].order_list_blocks[2] = 255; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
808 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
809 |
_players_ai[p->index].state = AIS_BUILD_DEFAULT_RAIL_BLOCKS; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
810 |
_players_ai[p->index].state_mode = UCHAR_MAX; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
811 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
812 |
_players_ai[p->index].timeout_counter = 0; |
0 | 813 |
} |
814 |
||
815 |
static void AiWantMediumIndustryRoute(Player *p) |
|
816 |
{ |
|
817 |
int i; |
|
818 |
FoundRoute fr; |
|
819 |
||
820 |
i = 60; |
|
2952 | 821 |
for (;;) { |
0 | 822 |
// look for one from the subsidy list |
823 |
AiFindSubsidyIndustryRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
824 |
if (IsInsideMM(fr.distance, 40, 60 + 1)) break; |
0 | 825 |
|
826 |
// try a random one |
|
827 |
AiFindRandomIndustryRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
828 |
if (IsInsideMM(fr.distance, 40, 60 + 1)) break; |
0 | 829 |
|
830 |
// only test 60 times |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
831 |
if (--i == 0) return; |
0 | 832 |
} |
833 |
||
2639 | 834 |
if (!AiCheckIfRouteIsGood(p, &fr, 1)) return; |
0 | 835 |
|
836 |
// Fill the source field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
837 |
_players_ai[p->index].src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
838 |
_players_ai[p->index].src.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
839 |
_players_ai[p->index].src.rand_rng = 9; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
840 |
_players_ai[p->index].src.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
841 |
_players_ai[p->index].src.unk6 = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
842 |
_players_ai[p->index].src.unk7 = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
843 |
_players_ai[p->index].src.buildcmd_a = 0x10; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
844 |
_players_ai[p->index].src.buildcmd_b = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
845 |
_players_ai[p->index].src.direction = AiGetDirectionBetweenTiles( |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
846 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from), |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
847 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to) |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
848 |
); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
849 |
_players_ai[p->index].src.cargo = fr.cargo | 0x80; |
0 | 850 |
|
851 |
// Fill the dest field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
852 |
_players_ai[p->index].dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
853 |
_players_ai[p->index].dst.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
854 |
_players_ai[p->index].dst.rand_rng = 9; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
855 |
_players_ai[p->index].dst.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
856 |
_players_ai[p->index].dst.unk6 = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
857 |
_players_ai[p->index].dst.unk7 = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
858 |
_players_ai[p->index].dst.buildcmd_a = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
859 |
_players_ai[p->index].dst.buildcmd_b = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
860 |
_players_ai[p->index].dst.direction = AiGetDirectionBetweenTiles( |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
861 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to), |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
862 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from) |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
863 |
); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
864 |
_players_ai[p->index].dst.cargo = fr.cargo; |
0 | 865 |
|
866 |
// Fill common fields |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
867 |
_players_ai[p->index].cargo_type = fr.cargo; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
868 |
_players_ai[p->index].num_wagons = 3; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
869 |
_players_ai[p->index].build_kind = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
870 |
_players_ai[p->index].num_build_rec = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
871 |
_players_ai[p->index].num_loco_to_build = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
872 |
_players_ai[p->index].num_want_fullload = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
873 |
_players_ai[p->index].wagon_list[0] = INVALID_VEHICLE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
874 |
_players_ai[p->index].order_list_blocks[0] = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
875 |
_players_ai[p->index].order_list_blocks[1] = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
876 |
_players_ai[p->index].order_list_blocks[2] = 255; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
877 |
_players_ai[p->index].state = AIS_BUILD_DEFAULT_RAIL_BLOCKS; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
878 |
_players_ai[p->index].state_mode = UCHAR_MAX; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
879 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
880 |
_players_ai[p->index].timeout_counter = 0; |
0 | 881 |
} |
882 |
||
883 |
static void AiWantShortIndustryRoute(Player *p) |
|
884 |
{ |
|
885 |
int i; |
|
886 |
FoundRoute fr; |
|
887 |
||
888 |
i = 60; |
|
2952 | 889 |
for (;;) { |
0 | 890 |
// look for one from the subsidy list |
891 |
AiFindSubsidyIndustryRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
892 |
if (IsInsideMM(fr.distance, 15, 40 + 1)) break; |
0 | 893 |
|
894 |
// try a random one |
|
895 |
AiFindRandomIndustryRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
896 |
if (IsInsideMM(fr.distance, 15, 40 + 1)) break; |
0 | 897 |
|
898 |
// only test 60 times |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
899 |
if (--i == 0) return; |
0 | 900 |
} |
901 |
||
2639 | 902 |
if (!AiCheckIfRouteIsGood(p, &fr, 1)) return; |
0 | 903 |
|
904 |
// Fill the source field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
905 |
_players_ai[p->index].src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
906 |
_players_ai[p->index].src.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
907 |
_players_ai[p->index].src.rand_rng = 9; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
908 |
_players_ai[p->index].src.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
909 |
_players_ai[p->index].src.unk6 = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
910 |
_players_ai[p->index].src.unk7 = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
911 |
_players_ai[p->index].src.buildcmd_a = 0x10; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
912 |
_players_ai[p->index].src.buildcmd_b = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
913 |
_players_ai[p->index].src.direction = AiGetDirectionBetweenTiles( |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
914 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from), |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
915 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to) |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
916 |
); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
917 |
_players_ai[p->index].src.cargo = fr.cargo | 0x80; |
0 | 918 |
|
919 |
// Fill the dest field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
920 |
_players_ai[p->index].dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
921 |
_players_ai[p->index].dst.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
922 |
_players_ai[p->index].dst.rand_rng = 9; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
923 |
_players_ai[p->index].dst.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
924 |
_players_ai[p->index].dst.unk6 = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
925 |
_players_ai[p->index].dst.unk7 = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
926 |
_players_ai[p->index].dst.buildcmd_a = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
927 |
_players_ai[p->index].dst.buildcmd_b = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
928 |
_players_ai[p->index].dst.direction = AiGetDirectionBetweenTiles( |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
929 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to), |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
930 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from) |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
931 |
); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
932 |
_players_ai[p->index].dst.cargo = fr.cargo; |
0 | 933 |
|
934 |
// Fill common fields |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
935 |
_players_ai[p->index].cargo_type = fr.cargo; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
936 |
_players_ai[p->index].num_wagons = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
937 |
_players_ai[p->index].build_kind = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
938 |
_players_ai[p->index].num_build_rec = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
939 |
_players_ai[p->index].num_loco_to_build = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
940 |
_players_ai[p->index].num_want_fullload = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
941 |
_players_ai[p->index].wagon_list[0] = INVALID_VEHICLE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
942 |
_players_ai[p->index].order_list_blocks[0] = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
943 |
_players_ai[p->index].order_list_blocks[1] = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
944 |
_players_ai[p->index].order_list_blocks[2] = 255; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
945 |
_players_ai[p->index].state = AIS_BUILD_DEFAULT_RAIL_BLOCKS; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
946 |
_players_ai[p->index].state_mode = UCHAR_MAX; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
947 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
948 |
_players_ai[p->index].timeout_counter = 0; |
0 | 949 |
} |
950 |
||
951 |
static void AiWantMailRoute(Player *p) |
|
952 |
{ |
|
953 |
int i; |
|
954 |
FoundRoute fr; |
|
955 |
||
956 |
i = 60; |
|
2952 | 957 |
for (;;) { |
0 | 958 |
// look for one from the subsidy list |
959 |
AiFindSubsidyPassengerRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
960 |
if (IsInsideMM(fr.distance, 60, 110 + 1)) break; |
0 | 961 |
|
962 |
// try a random one |
|
963 |
AiFindRandomPassengerRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
964 |
if (IsInsideMM(fr.distance, 60, 110 + 1)) break; |
0 | 965 |
|
966 |
// only test 60 times |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
967 |
if (--i == 0) return; |
0 | 968 |
} |
969 |
||
970 |
fr.cargo = CT_MAIL; |
|
2639 | 971 |
if (!AiCheckIfRouteIsGood(p, &fr, 1)) return; |
0 | 972 |
|
973 |
// Fill the source field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
974 |
_players_ai[p->index].src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
975 |
_players_ai[p->index].src.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
976 |
_players_ai[p->index].src.rand_rng = 7; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
977 |
_players_ai[p->index].src.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
978 |
_players_ai[p->index].src.unk6 = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
979 |
_players_ai[p->index].src.unk7 = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
980 |
_players_ai[p->index].src.buildcmd_a = 0x24; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
981 |
_players_ai[p->index].src.buildcmd_b = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
982 |
_players_ai[p->index].src.direction = AiGetDirectionBetweenTiles( |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
983 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from), |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
984 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to) |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
985 |
); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
986 |
_players_ai[p->index].src.cargo = fr.cargo; |
0 | 987 |
|
988 |
// Fill the dest field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
989 |
_players_ai[p->index].dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
990 |
_players_ai[p->index].dst.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
991 |
_players_ai[p->index].dst.rand_rng = 7; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
992 |
_players_ai[p->index].dst.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
993 |
_players_ai[p->index].dst.unk6 = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
994 |
_players_ai[p->index].dst.unk7 = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
995 |
_players_ai[p->index].dst.buildcmd_a = 0x34; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
996 |
_players_ai[p->index].dst.buildcmd_b = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
997 |
_players_ai[p->index].dst.direction = AiGetDirectionBetweenTiles( |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
998 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to), |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
999 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from) |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1000 |
); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1001 |
_players_ai[p->index].dst.cargo = fr.cargo; |
0 | 1002 |
|
1003 |
// Fill middle field 1 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1004 |
_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
|
1005 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from), |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1006 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to), |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1007 |
0x55 |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1008 |
); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1009 |
_players_ai[p->index].mid1.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1010 |
_players_ai[p->index].mid1.rand_rng = 6; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1011 |
_players_ai[p->index].mid1.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1012 |
_players_ai[p->index].mid1.unk6 = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1013 |
_players_ai[p->index].mid1.unk7 = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1014 |
_players_ai[p->index].mid1.buildcmd_a = 0x30; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1015 |
_players_ai[p->index].mid1.buildcmd_b = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1016 |
_players_ai[p->index].mid1.direction = _players_ai[p->index].src.direction; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1017 |
_players_ai[p->index].mid1.cargo = fr.cargo; |
0 | 1018 |
|
1019 |
// Fill middle field 2 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1020 |
_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
|
1021 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from), |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1022 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to), |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1023 |
0xAA |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1024 |
); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1025 |
_players_ai[p->index].mid2.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1026 |
_players_ai[p->index].mid2.rand_rng = 6; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1027 |
_players_ai[p->index].mid2.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1028 |
_players_ai[p->index].mid2.unk6 = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1029 |
_players_ai[p->index].mid2.unk7 = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1030 |
_players_ai[p->index].mid2.buildcmd_a = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1031 |
_players_ai[p->index].mid2.buildcmd_b = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1032 |
_players_ai[p->index].mid2.direction = _players_ai[p->index].dst.direction; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1033 |
_players_ai[p->index].mid2.cargo = fr.cargo; |
0 | 1034 |
|
1035 |
// Fill common fields |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1036 |
_players_ai[p->index].cargo_type = fr.cargo; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1037 |
_players_ai[p->index].num_wagons = 3; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1038 |
_players_ai[p->index].build_kind = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1039 |
_players_ai[p->index].num_build_rec = 4; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1040 |
_players_ai[p->index].num_loco_to_build = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1041 |
_players_ai[p->index].num_want_fullload = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1042 |
_players_ai[p->index].wagon_list[0] = INVALID_VEHICLE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1043 |
_players_ai[p->index].order_list_blocks[0] = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1044 |
_players_ai[p->index].order_list_blocks[1] = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1045 |
_players_ai[p->index].order_list_blocks[2] = 255; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1046 |
_players_ai[p->index].state = AIS_BUILD_DEFAULT_RAIL_BLOCKS; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1047 |
_players_ai[p->index].state_mode = UCHAR_MAX; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1048 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1049 |
_players_ai[p->index].timeout_counter = 0; |
0 | 1050 |
} |
1051 |
||
1052 |
static void AiWantPassengerRoute(Player *p) |
|
1053 |
{ |
|
1054 |
int i; |
|
1055 |
FoundRoute fr; |
|
1056 |
||
1057 |
i = 60; |
|
2952 | 1058 |
for (;;) { |
0 | 1059 |
// look for one from the subsidy list |
1060 |
AiFindSubsidyPassengerRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
1061 |
if (IsInsideMM(fr.distance, 0, 55 + 1)) break; |
0 | 1062 |
|
1063 |
// try a random one |
|
1064 |
AiFindRandomPassengerRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
1065 |
if (IsInsideMM(fr.distance, 0, 55 + 1)) break; |
0 | 1066 |
|
1067 |
// only test 60 times |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1068 |
if (--i == 0) return; |
0 | 1069 |
} |
1070 |
||
1071 |
fr.cargo = CT_PASSENGERS; |
|
2639 | 1072 |
if (!AiCheckIfRouteIsGood(p, &fr, 1)) return; |
0 | 1073 |
|
1074 |
// Fill the source field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1075 |
_players_ai[p->index].src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1076 |
_players_ai[p->index].src.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1077 |
_players_ai[p->index].src.rand_rng = 7; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1078 |
_players_ai[p->index].src.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1079 |
_players_ai[p->index].src.unk6 = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1080 |
_players_ai[p->index].src.unk7 = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1081 |
_players_ai[p->index].src.buildcmd_a = 0x10; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1082 |
_players_ai[p->index].src.buildcmd_b = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1083 |
_players_ai[p->index].src.direction = AiGetDirectionBetweenTiles( |
2639 | 1084 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from), |
1085 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to) |
|
1086 |
); |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1087 |
_players_ai[p->index].src.cargo = fr.cargo; |
0 | 1088 |
|
1089 |
// Fill the dest field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1090 |
_players_ai[p->index].dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1091 |
_players_ai[p->index].dst.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1092 |
_players_ai[p->index].dst.rand_rng = 7; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1093 |
_players_ai[p->index].dst.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1094 |
_players_ai[p->index].dst.unk6 = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1095 |
_players_ai[p->index].dst.unk7 = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1096 |
_players_ai[p->index].dst.buildcmd_a = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1097 |
_players_ai[p->index].dst.buildcmd_b = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1098 |
_players_ai[p->index].dst.direction = AiGetDirectionBetweenTiles( |
2639 | 1099 |
GET_TOWN_OR_INDUSTRY_TILE(fr.to), |
1100 |
GET_TOWN_OR_INDUSTRY_TILE(fr.from) |
|
1101 |
); |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1102 |
_players_ai[p->index].dst.cargo = fr.cargo; |
0 | 1103 |
|
1104 |
// Fill common fields |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1105 |
_players_ai[p->index].cargo_type = fr.cargo; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1106 |
_players_ai[p->index].num_wagons = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1107 |
_players_ai[p->index].build_kind = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1108 |
_players_ai[p->index].num_build_rec = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1109 |
_players_ai[p->index].num_loco_to_build = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1110 |
_players_ai[p->index].num_want_fullload = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1111 |
_players_ai[p->index].wagon_list[0] = INVALID_VEHICLE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1112 |
_players_ai[p->index].order_list_blocks[0] = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1113 |
_players_ai[p->index].order_list_blocks[1] = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1114 |
_players_ai[p->index].order_list_blocks[2] = 255; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1115 |
_players_ai[p->index].state = AIS_BUILD_DEFAULT_RAIL_BLOCKS; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1116 |
_players_ai[p->index].state_mode = UCHAR_MAX; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1117 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1118 |
_players_ai[p->index].timeout_counter = 0; |
0 | 1119 |
} |
1120 |
||
1121 |
static void AiWantTrainRoute(Player *p) |
|
1122 |
{ |
|
2635 | 1123 |
uint16 r = GB(Random(), 0, 16); |
1124 |
||
8732
b18f578f7c16
(svn r11800) -Codechange: move some functions to a more logical location + some type safety.
rubidium
parents:
8726
diff
changeset
|
1125 |
_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
|
1126 |
|
0 | 1127 |
if (r > 0xD000) { |
1128 |
AiWantLongIndustryRoute(p); |
|
1129 |
} else if (r > 0x6000) { |
|
1130 |
AiWantMediumIndustryRoute(p); |
|
1131 |
} else if (r > 0x1000) { |
|
1132 |
AiWantShortIndustryRoute(p); |
|
1133 |
} else if (r > 0x800) { |
|
1134 |
AiWantPassengerRoute(p); |
|
1135 |
} else { |
|
1136 |
AiWantMailRoute(p); |
|
1137 |
} |
|
1138 |
} |
|
1139 |
||
1140 |
static void AiWantLongRoadIndustryRoute(Player *p) |
|
1141 |
{ |
|
1142 |
int i; |
|
1143 |
FoundRoute fr; |
|
1144 |
||
1145 |
i = 60; |
|
2952 | 1146 |
for (;;) { |
0 | 1147 |
// look for one from the subsidy list |
1148 |
AiFindSubsidyIndustryRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
1149 |
if (IsInsideMM(fr.distance, 35, 55 + 1)) break; |
0 | 1150 |
|
1151 |
// try a random one |
|
1152 |
AiFindRandomIndustryRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
1153 |
if (IsInsideMM(fr.distance, 35, 55 + 1)) break; |
0 | 1154 |
|
1155 |
// only test 60 times |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1156 |
if (--i == 0) return; |
0 | 1157 |
} |
1158 |
||
2639 | 1159 |
if (!AiCheckIfRouteIsGood(p, &fr, 2)) return; |
0 | 1160 |
|
1161 |
// Fill the source field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1162 |
_players_ai[p->index].src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1163 |
_players_ai[p->index].src.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1164 |
_players_ai[p->index].src.rand_rng = 9; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1165 |
_players_ai[p->index].src.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1166 |
_players_ai[p->index].src.buildcmd_a = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1167 |
_players_ai[p->index].src.direction = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1168 |
_players_ai[p->index].src.cargo = fr.cargo | 0x80; |
0 | 1169 |
|
1170 |
// Fill the dest field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1171 |
_players_ai[p->index].dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1172 |
_players_ai[p->index].dst.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1173 |
_players_ai[p->index].dst.rand_rng = 9; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1174 |
_players_ai[p->index].dst.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1175 |
_players_ai[p->index].dst.buildcmd_a = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1176 |
_players_ai[p->index].dst.direction = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1177 |
_players_ai[p->index].dst.cargo = fr.cargo; |
0 | 1178 |
|
1179 |
// Fill common fields |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1180 |
_players_ai[p->index].cargo_type = fr.cargo; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1181 |
_players_ai[p->index].num_build_rec = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1182 |
_players_ai[p->index].num_loco_to_build = 5; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1183 |
_players_ai[p->index].num_want_fullload = 5; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1184 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1185 |
// _players_ai[p->index].loco_id = INVALID_VEHICLE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1186 |
_players_ai[p->index].order_list_blocks[0] = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1187 |
_players_ai[p->index].order_list_blocks[1] = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1188 |
_players_ai[p->index].order_list_blocks[2] = 255; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1189 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1190 |
_players_ai[p->index].state = AIS_BUILD_DEFAULT_ROAD_BLOCKS; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1191 |
_players_ai[p->index].state_mode = UCHAR_MAX; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1192 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1193 |
_players_ai[p->index].timeout_counter = 0; |
0 | 1194 |
} |
1195 |
||
1196 |
static void AiWantMediumRoadIndustryRoute(Player *p) |
|
1197 |
{ |
|
1198 |
int i; |
|
1199 |
FoundRoute fr; |
|
1200 |
||
1201 |
i = 60; |
|
2952 | 1202 |
for (;;) { |
0 | 1203 |
// look for one from the subsidy list |
1204 |
AiFindSubsidyIndustryRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
1205 |
if (IsInsideMM(fr.distance, 15, 40 + 1)) break; |
0 | 1206 |
|
1207 |
// try a random one |
|
1208 |
AiFindRandomIndustryRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
1209 |
if (IsInsideMM(fr.distance, 15, 40 + 1)) break; |
0 | 1210 |
|
1211 |
// only test 60 times |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1212 |
if (--i == 0) return; |
0 | 1213 |
} |
1214 |
||
2639 | 1215 |
if (!AiCheckIfRouteIsGood(p, &fr, 2)) return; |
0 | 1216 |
|
1217 |
// Fill the source field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1218 |
_players_ai[p->index].src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1219 |
_players_ai[p->index].src.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1220 |
_players_ai[p->index].src.rand_rng = 9; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1221 |
_players_ai[p->index].src.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1222 |
_players_ai[p->index].src.buildcmd_a = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1223 |
_players_ai[p->index].src.direction = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1224 |
_players_ai[p->index].src.cargo = fr.cargo | 0x80; |
0 | 1225 |
|
1226 |
// Fill the dest field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1227 |
_players_ai[p->index].dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1228 |
_players_ai[p->index].dst.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1229 |
_players_ai[p->index].dst.rand_rng = 9; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1230 |
_players_ai[p->index].dst.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1231 |
_players_ai[p->index].dst.buildcmd_a = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1232 |
_players_ai[p->index].dst.direction = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1233 |
_players_ai[p->index].dst.cargo = fr.cargo; |
0 | 1234 |
|
1235 |
// Fill common fields |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1236 |
_players_ai[p->index].cargo_type = fr.cargo; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1237 |
_players_ai[p->index].num_build_rec = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1238 |
_players_ai[p->index].num_loco_to_build = 3; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1239 |
_players_ai[p->index].num_want_fullload = 3; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1240 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1241 |
// _players_ai[p->index].loco_id = INVALID_VEHICLE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1242 |
_players_ai[p->index].order_list_blocks[0] = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1243 |
_players_ai[p->index].order_list_blocks[1] = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1244 |
_players_ai[p->index].order_list_blocks[2] = 255; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1245 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1246 |
_players_ai[p->index].state = AIS_BUILD_DEFAULT_ROAD_BLOCKS; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1247 |
_players_ai[p->index].state_mode = UCHAR_MAX; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1248 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1249 |
_players_ai[p->index].timeout_counter = 0; |
0 | 1250 |
} |
1251 |
||
1252 |
static void AiWantLongRoadPassengerRoute(Player *p) |
|
1253 |
{ |
|
1254 |
int i; |
|
1255 |
FoundRoute fr; |
|
1256 |
||
1257 |
i = 60; |
|
2952 | 1258 |
for (;;) { |
0 | 1259 |
// look for one from the subsidy list |
1260 |
AiFindSubsidyPassengerRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
1261 |
if (IsInsideMM(fr.distance, 55, 180 + 1)) break; |
0 | 1262 |
|
1263 |
// try a random one |
|
1264 |
AiFindRandomPassengerRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
1265 |
if (IsInsideMM(fr.distance, 55, 180 + 1)) break; |
0 | 1266 |
|
1267 |
// only test 60 times |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1268 |
if (--i == 0) return; |
0 | 1269 |
} |
1270 |
||
1271 |
fr.cargo = CT_PASSENGERS; |
|
1272 |
||
2639 | 1273 |
if (!AiCheckIfRouteIsGood(p, &fr, 2)) return; |
0 | 1274 |
|
1275 |
// Fill the source field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1276 |
_players_ai[p->index].src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1277 |
_players_ai[p->index].src.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1278 |
_players_ai[p->index].src.rand_rng = 10; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1279 |
_players_ai[p->index].src.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1280 |
_players_ai[p->index].src.buildcmd_a = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1281 |
_players_ai[p->index].src.direction = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1282 |
_players_ai[p->index].src.cargo = CT_PASSENGERS; |
0 | 1283 |
|
1284 |
// Fill the dest field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1285 |
_players_ai[p->index].dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1286 |
_players_ai[p->index].dst.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1287 |
_players_ai[p->index].dst.rand_rng = 10; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1288 |
_players_ai[p->index].dst.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1289 |
_players_ai[p->index].dst.buildcmd_a = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1290 |
_players_ai[p->index].dst.direction = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1291 |
_players_ai[p->index].dst.cargo = CT_PASSENGERS; |
0 | 1292 |
|
1293 |
// Fill common fields |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1294 |
_players_ai[p->index].cargo_type = CT_PASSENGERS; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1295 |
_players_ai[p->index].num_build_rec = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1296 |
_players_ai[p->index].num_loco_to_build = 4; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1297 |
_players_ai[p->index].num_want_fullload = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1298 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1299 |
// _players_ai[p->index].loco_id = INVALID_VEHICLE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1300 |
_players_ai[p->index].order_list_blocks[0] = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1301 |
_players_ai[p->index].order_list_blocks[1] = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1302 |
_players_ai[p->index].order_list_blocks[2] = 255; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1303 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1304 |
_players_ai[p->index].state = AIS_BUILD_DEFAULT_ROAD_BLOCKS; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1305 |
_players_ai[p->index].state_mode = UCHAR_MAX; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1306 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1307 |
_players_ai[p->index].timeout_counter = 0; |
0 | 1308 |
} |
1309 |
||
1310 |
static void AiWantPassengerRouteInsideTown(Player *p) |
|
1311 |
{ |
|
1312 |
int i; |
|
1313 |
FoundRoute fr; |
|
1314 |
Town *t; |
|
1315 |
||
1316 |
i = 60; |
|
2952 | 1317 |
for (;;) { |
0 | 1318 |
// Find a town big enough |
1319 |
t = AiFindRandomTown(); |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1320 |
if (t != NULL && t->population >= 700) break; |
0 | 1321 |
|
1322 |
// only test 60 times |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1323 |
if (--i == 0) return; |
0 | 1324 |
} |
1325 |
||
1326 |
fr.cargo = CT_PASSENGERS; |
|
1327 |
fr.from = fr.to = t; |
|
1328 |
||
2639 | 1329 |
if (!AiCheckIfRouteIsGood(p, &fr, 2)) return; |
0 | 1330 |
|
1331 |
// Fill the source field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1332 |
_players_ai[p->index].src.spec_tile = t->xy; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1333 |
_players_ai[p->index].src.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1334 |
_players_ai[p->index].src.rand_rng = 10; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1335 |
_players_ai[p->index].src.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1336 |
_players_ai[p->index].src.buildcmd_a = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1337 |
_players_ai[p->index].src.direction = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1338 |
_players_ai[p->index].src.cargo = CT_PASSENGERS; |
0 | 1339 |
|
1340 |
// Fill the dest field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1341 |
_players_ai[p->index].dst.spec_tile = t->xy; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1342 |
_players_ai[p->index].dst.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1343 |
_players_ai[p->index].dst.rand_rng = 10; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1344 |
_players_ai[p->index].dst.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1345 |
_players_ai[p->index].dst.buildcmd_a = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1346 |
_players_ai[p->index].dst.direction = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1347 |
_players_ai[p->index].dst.cargo = CT_PASSENGERS; |
0 | 1348 |
|
1349 |
// Fill common fields |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1350 |
_players_ai[p->index].cargo_type = CT_PASSENGERS; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1351 |
_players_ai[p->index].num_build_rec = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1352 |
_players_ai[p->index].num_loco_to_build = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1353 |
_players_ai[p->index].num_want_fullload = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1354 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1355 |
// _players_ai[p->index].loco_id = INVALID_VEHICLE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1356 |
_players_ai[p->index].order_list_blocks[0] = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1357 |
_players_ai[p->index].order_list_blocks[1] = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1358 |
_players_ai[p->index].order_list_blocks[2] = 255; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1359 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1360 |
_players_ai[p->index].state = AIS_BUILD_DEFAULT_ROAD_BLOCKS; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1361 |
_players_ai[p->index].state_mode = UCHAR_MAX; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1362 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1363 |
_players_ai[p->index].timeout_counter = 0; |
0 | 1364 |
} |
1365 |
||
1366 |
static void AiWantRoadRoute(Player *p) |
|
1367 |
{ |
|
2635 | 1368 |
uint16 r = GB(Random(), 0, 16); |
0 | 1369 |
|
1370 |
if (r > 0x4000) { |
|
1371 |
AiWantLongRoadIndustryRoute(p); |
|
1372 |
} else if (r > 0x2000) { |
|
1373 |
AiWantMediumRoadIndustryRoute(p); |
|
1374 |
} else if (r > 0x1000) { |
|
1375 |
AiWantLongRoadPassengerRoute(p); |
|
1376 |
} else { |
|
1377 |
AiWantPassengerRouteInsideTown(p); |
|
1378 |
} |
|
1379 |
} |
|
1380 |
||
1381 |
static void AiWantPassengerAircraftRoute(Player *p) |
|
1382 |
{ |
|
1383 |
FoundRoute fr; |
|
1384 |
int i; |
|
1385 |
||
7354
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1386 |
/* Get aircraft that would be bought for this route |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1387 |
* (probably, as conditions may change before the route is fully built, |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1388 |
* like running out of money and having to select different aircraft, etc ...) */ |
8898
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
1389 |
EngineID veh = AiChooseAircraftToBuild(p->player_money, _players_ai[p->index].build_kind != 0 ? AIR_CTOL : 0); |
7354
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1390 |
|
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1391 |
/* No aircraft buildable mean no aircraft route */ |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1392 |
if (veh == INVALID_ENGINE) return; |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1393 |
|
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1394 |
const AircraftVehicleInfo *avi = AircraftVehInfo(veh); |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1395 |
|
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1396 |
/* For passengers, "optimal" number of days in transit is about 80 to 100 |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1397 |
* Calculate "maximum optimal number of squares" from speed for 80 days |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1398 |
* 20 days should be enough for takeoff, land, taxi, etc ... |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1399 |
* |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1400 |
* "A vehicle traveling at 100kph will cross 5.6 tiles per day" -> |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1401 |
* Since in table aircraft speeds are in "real km/h", this should be accurate |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1402 |
* We get max_squares = avi->max_speed * 5.6 / 100.0 * 80 */ |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1403 |
int max_squares = avi->max_speed * 448 / 100; |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1404 |
|
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1405 |
/* For example this will be 10456 tiles for 2334 km/h aircrafts with realistic aircraft speeds |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1406 |
* and 836 with "unrealistic" speeds, much more than the original 95 squares limit |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1407 |
* |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1408 |
* Size of the map, if not rectangular, it is the larger dimension of it |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1409 |
*/ |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1410 |
int map_size = max(MapSizeX(), MapSizeY()); |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1411 |
|
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1412 |
/* Minimum distance between airports is half of map size, clamped between 1% and 20% of optimum. |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1413 |
* May prevent building plane routes at all on small maps, but they will be ineffective there, so |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1414 |
* it is feature, not a bug. |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1415 |
* On smaller distances, buses or trains are usually more effective approach anyway. |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1416 |
* Additional safeguard is needing at least 20 squares, |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1417 |
* which may trigger in highly unusual configurations */ |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1418 |
int min_squares = max(20, max(max_squares / 100, min(max_squares / 5, map_size / 2))); |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1419 |
|
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1420 |
/* Should not happen, unless aircraft with real speed under approx. 5 km/h is selected. |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1421 |
* No such exist, unless using some NewGRF with ballons, zeppelins or similar |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1422 |
* slow-moving stuff. In that case, bail out, it is faster to walk by foot anyway :). */ |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1423 |
if (max_squares < min_squares) return; |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1424 |
|
0 | 1425 |
i = 60; |
2952 | 1426 |
for (;;) { |
7354
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1427 |
|
0 | 1428 |
// look for one from the subsidy list |
1429 |
AiFindSubsidyPassengerRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
1430 |
if (IsInsideMM(fr.distance, min_squares, max_squares + 1)) break; |
0 | 1431 |
|
1432 |
// try a random one |
|
1433 |
AiFindRandomPassengerRoute(&fr); |
|
8450
dce58137301f
(svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents:
8427
diff
changeset
|
1434 |
if (IsInsideMM(fr.distance, min_squares, max_squares + 1)) break; |
0 | 1435 |
|
1436 |
// only test 60 times |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1437 |
if (--i == 0) return; |
0 | 1438 |
} |
1439 |
||
1440 |
fr.cargo = CT_PASSENGERS; |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1441 |
if (!AiCheckIfRouteIsGood(p, &fr, 4)) return; |
0 | 1442 |
|
1443 |
||
1444 |
// Fill the source field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1445 |
_players_ai[p->index].src.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.to); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1446 |
_players_ai[p->index].src.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1447 |
_players_ai[p->index].src.rand_rng = 12; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1448 |
_players_ai[p->index].src.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1449 |
_players_ai[p->index].src.cargo = fr.cargo; |
0 | 1450 |
|
1451 |
// Fill the dest field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1452 |
_players_ai[p->index].dst.spec_tile = GET_TOWN_OR_INDUSTRY_TILE(fr.from); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1453 |
_players_ai[p->index].dst.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1454 |
_players_ai[p->index].dst.rand_rng = 12; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1455 |
_players_ai[p->index].dst.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1456 |
_players_ai[p->index].dst.cargo = fr.cargo; |
0 | 1457 |
|
1458 |
// Fill common fields |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1459 |
_players_ai[p->index].cargo_type = fr.cargo; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1460 |
_players_ai[p->index].build_kind = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1461 |
_players_ai[p->index].num_build_rec = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1462 |
_players_ai[p->index].num_loco_to_build = 1; |
7354
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1463 |
/* Using full load always may not be the best. |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1464 |
* Pick random value and rely on selling the vehicle & route |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1465 |
* afterwards if the choice was utterly wrong (or maybe altering the value if AI is improved) |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1466 |
* When traffic is very low or very assymetric, is is better not to full load |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1467 |
* When traffic is high, full/non-full make no difference |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1468 |
* It should be better to run with aircraft only one way full 6 times per year, |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1469 |
* rather than two way full 1 times. |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1470 |
* Practical experiments with AI show that the non-full-load aircrafts are usually |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1471 |
* those that survive |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1472 |
* Also, non-full load is more resistant against starving (by building better stations |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1473 |
* or using exclusive rights) |
4108080dce70
(svn r10098) -Codechange: bilbo cared about the performance of the AI with respect to the airport placement, so he rewrote the aircraft handling of the AI; now it can actually make lots of money again :)
rubidium
parents:
7338
diff
changeset
|
1474 |
*/ |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1475 |
_players_ai[p->index].num_want_fullload = Chance16(1, 5); // 20% chance |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1476 |
// _players_ai[p->index].loco_id = INVALID_VEHICLE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1477 |
_players_ai[p->index].order_list_blocks[0] = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1478 |
_players_ai[p->index].order_list_blocks[1] = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1479 |
_players_ai[p->index].order_list_blocks[2] = 255; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1480 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1481 |
_players_ai[p->index].state = AIS_AIRPORT_STUFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1482 |
_players_ai[p->index].timeout_counter = 0; |
0 | 1483 |
} |
1484 |
||
1485 |
static void AiWantOilRigAircraftRoute(Player *p) |
|
1486 |
{ |
|
1487 |
int i; |
|
1488 |
FoundRoute fr; |
|
1489 |
Town *t; |
|
1490 |
Industry *in; |
|
1491 |
||
1492 |
i = 60; |
|
2952 | 1493 |
for (;;) { |
0 | 1494 |
// Find a town |
1495 |
t = AiFindRandomTown(); |
|
1496 |
if (t != NULL) { |
|
1497 |
// Find a random oil rig industry |
|
4342
e2c427ed862e
(svn r6043) -Codechange: use wrapper if they are available
truelight
parents:
4322
diff
changeset
|
1498 |
in = AiFindRandomIndustry(); |
6896
b6051812d11f
(svn r9530) -Codechange: Replace a direct industry type access by a behaviour
belugas
parents:
6683
diff
changeset
|
1499 |
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
|
1500 |
if (DistanceManhattan(t->xy, in->xy) < 60) |
0 | 1501 |
break; |
1502 |
} |
|
1503 |
} |
|
1504 |
||
1505 |
// only test 60 times |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1506 |
if (--i == 0) return; |
0 | 1507 |
} |
1508 |
||
1509 |
fr.cargo = CT_PASSENGERS; |
|
1510 |
fr.from = fr.to = t; |
|
1511 |
||
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1512 |
if (!AiCheckIfRouteIsGood(p, &fr, 4)) return; |
0 | 1513 |
|
1514 |
// Fill the source field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1515 |
_players_ai[p->index].src.spec_tile = t->xy; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1516 |
_players_ai[p->index].src.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1517 |
_players_ai[p->index].src.rand_rng = 12; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1518 |
_players_ai[p->index].src.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1519 |
_players_ai[p->index].src.cargo = CT_PASSENGERS; |
0 | 1520 |
|
1521 |
// Fill the dest field |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1522 |
_players_ai[p->index].dst.spec_tile = in->xy; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1523 |
_players_ai[p->index].dst.use_tile = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1524 |
_players_ai[p->index].dst.rand_rng = 5; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1525 |
_players_ai[p->index].dst.cur_building_rule = 0xFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1526 |
_players_ai[p->index].dst.cargo = CT_PASSENGERS; |
0 | 1527 |
|
1528 |
// Fill common fields |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1529 |
_players_ai[p->index].cargo_type = CT_PASSENGERS; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1530 |
_players_ai[p->index].build_kind = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1531 |
_players_ai[p->index].num_build_rec = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1532 |
_players_ai[p->index].num_loco_to_build = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1533 |
_players_ai[p->index].num_want_fullload = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1534 |
// _players_ai[p->index].loco_id = INVALID_VEHICLE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1535 |
_players_ai[p->index].order_list_blocks[0] = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1536 |
_players_ai[p->index].order_list_blocks[1] = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1537 |
_players_ai[p->index].order_list_blocks[2] = 255; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1538 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1539 |
_players_ai[p->index].state = AIS_AIRPORT_STUFF; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1540 |
_players_ai[p->index].timeout_counter = 0; |
0 | 1541 |
} |
1542 |
||
1543 |
static void AiWantAircraftRoute(Player *p) |
|
1544 |
{ |
|
1545 |
uint16 r = (uint16)Random(); |
|
1546 |
||
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
|
1547 |
if (r >= 0x2AAA || _date < 0x3912 + DAYS_TILL_ORIGINAL_BASE_YEAR) { |
0 | 1548 |
AiWantPassengerAircraftRoute(p); |
1549 |
} else { |
|
1550 |
AiWantOilRigAircraftRoute(p); |
|
1551 |
} |
|
1552 |
} |
|
1553 |
||
1554 |
||
1555 |
||
1556 |
static void AiStateWantNewRoute(Player *p) |
|
1557 |
{ |
|
1558 |
uint16 r; |
|
1559 |
int i; |
|
1560 |
||
1561 |
if (p->player_money < AiGetBasePrice(p) * 500) { |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1562 |
_players_ai[p->index].state = AIS_0; |
0 | 1563 |
return; |
1564 |
} |
|
1565 |
||
1566 |
i = 200; |
|
2952 | 1567 |
for (;;) { |
0 | 1568 |
r = (uint16)Random(); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1569 |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1570 |
if (_patches.ai_disable_veh_train && |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1571 |
_patches.ai_disable_veh_roadveh && |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1572 |
_patches.ai_disable_veh_aircraft && |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1573 |
_patches.ai_disable_veh_ship) { |
76
30511cbc5188
(svn r77) -Fix: [1010788] AI service interval bug (tnx truesatan)
truelight
parents:
21
diff
changeset
|
1574 |
return; |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1575 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1576 |
|
0 | 1577 |
if (r < 0x7626) { |
20
13a47054d81e
(svn r21) Remove: PE_BYTE (bit-bools) in favour of PE_BOOL
truelight
parents:
0
diff
changeset
|
1578 |
if (_patches.ai_disable_veh_train) continue; |
0 | 1579 |
AiWantTrainRoute(p); |
1580 |
} else if (r < 0xC4EA) { |
|
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_roadveh) continue; |
0 | 1582 |
AiWantRoadRoute(p); |
1583 |
} else if (r < 0xD89B) { |
|
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_aircraft) continue; |
0 | 1585 |
AiWantAircraftRoute(p); |
1586 |
} else { |
|
7951
477c5cef5797
(svn r10942) -Fix [FS#1133]: make the AI not crash when it has ships as the AI does not support them.
rubidium
parents:
7886
diff
changeset
|
1587 |
/* Ships are not implemented in this (broken) AI */ |
0 | 1588 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1589 |
|
0 | 1590 |
// got a route? |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1591 |
if (_players_ai[p->index].state != AIS_WANT_NEW_ROUTE) break; |
0 | 1592 |
|
1593 |
// time out? |
|
1594 |
if (--i == 0) { |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1595 |
if (++_players_ai[p->index].state_counter == 556) _players_ai[p->index].state = AIS_0; |
0 | 1596 |
break; |
1597 |
} |
|
1598 |
} |
|
1599 |
} |
|
1600 |
||
1601 |
static bool AiCheckTrackResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo) |
|
1602 |
{ |
|
9069 | 1603 |
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
|
1604 |
|
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
1605 |
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
|
1606 |
AcceptedCargo values; |
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
1607 |
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
|
1608 |
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
|
1609 |
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
|
1610 |
|
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
1611 |
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
|
1612 |
|
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
1613 |
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
|
1614 |
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
|
1615 |
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
|
1616 |
|
6432 | 1617 |
if (p->attr & 1) Swap(w, h); |
0 | 1618 |
|
1619 |
if (cargo & 0x80) { |
|
568
6eb7d394fb35
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
1620 |
GetProductionAroundTiles(values, tile2, w, h, rad); |
0 | 1621 |
return values[cargo & 0x7F] != 0; |
1622 |
} else { |
|
568
6eb7d394fb35
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
1623 |
GetAcceptanceAroundTiles(values, tile2, w, h, rad); |
0 | 1624 |
if (!(values[cargo] & ~7)) |
1625 |
return false; |
|
1626 |
if (cargo != CT_MAIL) |
|
1627 |
return true; |
|
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
1628 |
return !!((values[cargo] >> 1) & ~7); |
0 | 1629 |
} |
1630 |
} |
|
1631 |
||
1632 |
return true; |
|
1633 |
} |
|
1634 |
||
7439
0c0e2945c890
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents:
7362
diff
changeset
|
1635 |
static CommandCost AiDoBuildDefaultRailTrack(TileIndex tile, const AiDefaultBlockData* p, RailType railtype, byte flag) |
0 | 1636 |
{ |
7439
0c0e2945c890
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents:
7362
diff
changeset
|
1637 |
CommandCost ret; |
8726
5ae45b46506b
(svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents:
8725
diff
changeset
|
1638 |
CommandCost total_cost(EXPENSES_CONSTRUCTION); |
0 | 1639 |
Town *t = NULL; |
1640 |
int rating = 0; |
|
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
1641 |
int i, j, k; |
0 | 1642 |
|
2952 | 1643 |
for (;;) { |
0 | 1644 |
// 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
|
1645 |
uint c = TILE_MASK(tile + ToTileIndexDiff(p->tileoffs)); |
0 | 1646 |
|
1647 |
_cleared_town = NULL; |
|
1648 |
||
1649 |
if (p->mode < 2) { |
|
1650 |
if (p->mode == 0) { |
|
1651 |
// Depot |
|
3965
8c857c1da8c2
(svn r5128) Remove the global variable _cur_ai_player
tron
parents:
3964
diff
changeset
|
1652 |
ret = DoCommand(c, railtype, p->attr, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_TRAIN_DEPOT); |
0 | 1653 |
} else { |
1654 |
// Station |
|
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
1655 |
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 | 1656 |
} |
1657 |
||
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
|
1658 |
if (CmdFailed(ret)) return CMD_ERROR; |
7446
1c4d469f986e
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents:
7442
diff
changeset
|
1659 |
total_cost.AddCost(ret); |
0 | 1660 |
|
1661 |
clear_town_stuff:; |
|
1662 |
if (_cleared_town != NULL) { |
|
1663 |
if (t != NULL && t != _cleared_town) |
|
1664 |
return CMD_ERROR; |
|
1665 |
t = _cleared_town; |
|
1666 |
rating += _cleared_town_rating; |
|
1667 |
} |
|
1668 |
} else if (p->mode == 2) { |
|
8502 | 1669 |
/* Rail */ |
2639 | 1670 |
if (IsTileType(c, MP_RAILWAY)) return CMD_ERROR; |
0 | 1671 |
|
1672 |
j = p->attr; |
|
1673 |
k = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1674 |
|
8502 | 1675 |
/* Build the rail |
1676 |
* note: FOR_EACH_SET_BIT cannot be used here |
|
1677 |
*/ |
|
1678 |
for (i = 0; i != 6; i++, j >>= 1) { |
|
1679 |
if (j & 1) { |
|
1680 |
k = i; |
|
1681 |
ret = DoCommand(c, railtype, i, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL); |
|
1682 |
if (CmdFailed(ret)) return CMD_ERROR; |
|
1683 |
total_cost.AddCost(ret); |
|
1684 |
} |
|
0 | 1685 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1686 |
|
0 | 1687 |
/* signals too? */ |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1688 |
if (j & 3) { |
5575
e33f975c5a4b
(svn r7575) -Codechange: Allow the AI to build signals under bridges (revert r4911)
celestar
parents:
5573
diff
changeset
|
1689 |
// Can't build signals on a road. |
7866
e19fda04e8d3
(svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents:
7813
diff
changeset
|
1690 |
if (IsTileType(c, MP_ROAD)) return CMD_ERROR; |
0 | 1691 |
|
1692 |
if (flag & DC_EXEC) { |
|
1693 |
j = 4 - j; |
|
1694 |
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
|
1695 |
ret = DoCommand(c, k, 0, flag, CMD_BUILD_SIGNALS); |
0 | 1696 |
} while (--j); |
1697 |
} else { |
|
7446
1c4d469f986e
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents:
7442
diff
changeset
|
1698 |
ret.AddCost(_price.build_signals); |
0 | 1699 |
} |
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
|
1700 |
if (CmdFailed(ret)) return CMD_ERROR; |
7446
1c4d469f986e
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents:
7442
diff
changeset
|
1701 |
total_cost.AddCost(ret); |
0 | 1702 |
} |
1703 |
} else if (p->mode == 3) { |
|
1704 |
//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
|
1705 |
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
|
1706 |
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
|
1707 |
if (CmdFailed(ret)) return CMD_ERROR; |
7446
1c4d469f986e
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents:
7442
diff
changeset
|
1708 |
total_cost.AddCost(ret); |
1c4d469f986e
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents:
7442
diff
changeset
|
1709 |
total_cost.AddCost(_price.build_rail); |
0 | 1710 |
|
1711 |
if (flag & DC_EXEC) { |
|
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
1712 |
DoCommand(c, railtype, p->attr & 1, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_SINGLE_RAIL); |
0 | 1713 |
} |
1714 |
||
1715 |
goto clear_town_stuff; |
|
1716 |
} else { |
|
1717 |
// Unk |
|
1718 |
break; |
|
1719 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1720 |
|
0 | 1721 |
p++; |
1722 |
} |
|
1723 |
||
1724 |
if (!(flag & DC_EXEC)) { |
|
1725 |
if (t != NULL && rating > t->ratings[_current_player]) { |
|
1726 |
return CMD_ERROR; |
|
1727 |
} |
|
1728 |
} |
|
1729 |
||
1730 |
return total_cost; |
|
1731 |
} |
|
1732 |
||
1733 |
// Returns rule and cost |
|
7439
0c0e2945c890
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents:
7362
diff
changeset
|
1734 |
static int AiBuildDefaultRailTrack(TileIndex tile, byte p0, byte p1, byte p2, byte p3, byte dir, byte cargo, RailType railtype, CommandCost* cost) |
0 | 1735 |
{ |
1736 |
int i; |
|
1737 |
const AiDefaultRailBlock *p; |
|
1738 |
||
2639 | 1739 |
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
|
1740 |
if (p->p0 == p0 && p->p1 == p1 && p->p2 == p2 && p->p3 == p3 && |
2639 | 1741 |
(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
|
1742 |
*cost = AiDoBuildDefaultRailTrack(tile, p->data, railtype, DC_NO_TOWN_RATING); |
7442
a4f7989f27d4
(svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents:
7439
diff
changeset
|
1743 |
if (CmdSucceeded(*cost) && AiCheckTrackResources(tile, p->data, cargo)) |
0 | 1744 |
return i; |
1745 |
} |
|
1746 |
} |
|
1747 |
||
1748 |
return -1; |
|
1749 |
} |
|
1750 |
||
1751 |
static const byte _terraform_up_flags[] = { |
|
1752 |
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
|
1753 |
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
|
1754 |
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
|
1755 |
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
|
1756 |
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
|
1757 |
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
|
1758 |
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
|
1759 |
2, 1 |
0 | 1760 |
}; |
1761 |
||
1762 |
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
|
1763 |
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
|
1764 |
5, 6, 1, 8, |
0 | 1765 |
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
|
1766 |
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
|
1767 |
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
|
1768 |
5, 6, 2, 8, |
0 | 1769 |
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
|
1770 |
8, 4 |
0 | 1771 |
}; |
1772 |
||
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
1773 |
static void AiDoTerraformLand(TileIndex tile, DiagDirection dir, int unk, int mode) |
0 | 1774 |
{ |
2544 | 1775 |
PlayerID old_player; |
0 | 1776 |
uint32 r; |
3644
45a307767dc1
(svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents:
3609
diff
changeset
|
1777 |
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
|
1778 |
uint h; |
0 | 1779 |
|
1780 |
old_player = _current_player; |
|
1781 |
_current_player = OWNER_NONE; |
|
1782 |
||
1783 |
r = Random(); |
|
1784 |
||
1785 |
unk &= (int)r; |
|
1786 |
||
1787 |
do { |
|
4559
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4527
diff
changeset
|
1788 |
tile = TILE_MASK(tile + TileOffsByDiagDir(dir)); |
0 | 1789 |
|
1790 |
r >>= 2; |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1791 |
if (r & 2) { |
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
1792 |
dir = ChangeDiagDir(dir, (r & 1) ? DIAGDIRDIFF_90LEFT : DIAGDIRDIFF_90RIGHT); |
0 | 1793 |
} |
1794 |
} while (--unk >= 0); |
|
1795 |
||
1796 |
slope = GetTileSlope(tile, &h); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1797 |
|
3644
45a307767dc1
(svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents:
3609
diff
changeset
|
1798 |
if (slope != SLOPE_FLAT) { |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1799 |
if (mode > 0 || (mode == 0 && !(r & 0xC))) { |
0 | 1800 |
// 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
|
1801 |
DoCommand(tile, _terraform_up_flags[slope - 1], 1, |
0 | 1802 |
DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND); |
1803 |
} else if (h != 0) { |
|
1804 |
// 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
|
1805 |
DoCommand(tile, _terraform_down_flags[slope - 1], 0, |
0 | 1806 |
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
|
1807 |
} |
0 | 1808 |
} |
1809 |
||
1810 |
_current_player = old_player; |
|
1811 |
} |
|
1812 |
||
1813 |
static void AiStateBuildDefaultRailBlocks(Player *p) |
|
1814 |
{ |
|
2639 | 1815 |
uint i; |
1816 |
int j; |
|
0 | 1817 |
AiBuildRec *aib; |
1818 |
int rule; |
|
7439
0c0e2945c890
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents:
7362
diff
changeset
|
1819 |
CommandCost cost; |
0 | 1820 |
|
1821 |
// time out? |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1822 |
if (++_players_ai[p->index].timeout_counter == 1388) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1823 |
_players_ai[p->index].state = AIS_DELETE_RAIL_BLOCKS; |
0 | 1824 |
return; |
1825 |
} |
|
1826 |
||
1827 |
// do the following 8 times |
|
2648
72973ab76048
(svn r3190) Turn some loops into canonical for loop form
tron
parents:
2643
diff
changeset
|
1828 |
for (i = 0; i < 8; i++) { |
0 | 1829 |
// check if we can build the default track |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1830 |
aib = &_players_ai[p->index].src; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1831 |
j = _players_ai[p->index].num_build_rec; |
0 | 1832 |
do { |
1833 |
// this item has already been built? |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1834 |
if (aib->cur_building_rule != 255) continue; |
0 | 1835 |
|
1836 |
// adjust the coordinate randomly, |
|
1837 |
// to make sure that we find a position. |
|
1838 |
aib->use_tile = AdjustTileCoordRandomly(aib->spec_tile, aib->rand_rng); |
|
1839 |
||
1840 |
// check if the track can be build there. |
|
1841 |
rule = AiBuildDefaultRailTrack(aib->use_tile, |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1842 |
_players_ai[p->index].build_kind, _players_ai[p->index].num_wagons, |
0 | 1843 |
aib->unk6, aib->unk7, |
3965
8c857c1da8c2
(svn r5128) Remove the global variable _cur_ai_player
tron
parents:
3964
diff
changeset
|
1844 |
aib->direction, aib->cargo, |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1845 |
_players_ai[p->index].railtype_to_use, |
3965
8c857c1da8c2
(svn r5128) Remove the global variable _cur_ai_player
tron
parents:
3964
diff
changeset
|
1846 |
&cost |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
1847 |
); |
0 | 1848 |
|
1849 |
if (rule == -1) { |
|
1850 |
// cannot build, terraform after a while |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1851 |
if (_players_ai[p->index].state_counter >= 600) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1852 |
AiDoTerraformLand(aib->use_tile, (DiagDirection)(Random() & 3), 3, (int8)_players_ai[p->index].state_mode); |
0 | 1853 |
} |
1854 |
// also try the other terraform direction |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1855 |
if (++_players_ai[p->index].state_counter >= 1000) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1856 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1857 |
_players_ai[p->index].state_mode = -_players_ai[p->index].state_mode; |
0 | 1858 |
} |
1859 |
} else if (CheckPlayerHasMoney(cost)) { |
|
1860 |
// player has money, build it. |
|
1861 |
aib->cur_building_rule = rule; |
|
1862 |
||
7098
cfa4db4b59f7
(svn r9817) -Fix [FS#772]: an assertion that was triggered by the AI building when its rating for the town was not high enough.
rubidium
parents:
6987
diff
changeset
|
1863 |
AiDoBuildDefaultRailTrack( |
0 | 1864 |
aib->use_tile, |
1865 |
_default_rail_track_data[rule]->data, |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1866 |
_players_ai[p->index].railtype_to_use, |
0 | 1867 |
DC_EXEC | DC_NO_TOWN_RATING |
1868 |
); |
|
1869 |
} |
|
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
1870 |
} while (++aib, --j); |
2648
72973ab76048
(svn r3190) Turn some loops into canonical for loop form
tron
parents:
2643
diff
changeset
|
1871 |
} |
0 | 1872 |
|
1873 |
// check if we're done with all of them |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1874 |
aib = &_players_ai[p->index].src; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1875 |
j = _players_ai[p->index].num_build_rec; |
0 | 1876 |
do { |
2639 | 1877 |
if (aib->cur_building_rule == 255) return; |
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
1878 |
} while (++aib, --j); |
0 | 1879 |
|
1880 |
// yep, all are done. switch state to the rail building state. |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1881 |
_players_ai[p->index].state = AIS_BUILD_RAIL; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1882 |
_players_ai[p->index].state_mode = 255; |
0 | 1883 |
} |
1884 |
||
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
1885 |
static TileIndex AiGetEdgeOfDefaultRailBlock(byte rule, TileIndex tile, byte cmd, DiagDirection *dir) |
0 | 1886 |
{ |
1887 |
const AiDefaultBlockData *p = _default_rail_track_data[rule]->data; |
|
1888 |
||
1889 |
while (p->mode != 3 || !((--cmd) & 0x80)) p++; |
|
1890 |
||
4559
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4527
diff
changeset
|
1891 |
return tile + ToTileIndexDiff(p->tileoffs) - TileOffsByDiagDir(*dir = p->attr); |
0 | 1892 |
} |
1893 |
||
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
1894 |
struct AiRailPathFindData { |
0 | 1895 |
TileIndex tile; |
1896 |
TileIndex tile2; |
|
1897 |
int count; |
|
1898 |
bool flag; |
|
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
1899 |
}; |
0 | 1900 |
|
9107
3e57b96e98f2
(svn r12193) -Codechange: Rename a magic variable, give it a decent type, and remove a 'goto'.
frosch
parents:
9098
diff
changeset
|
1901 |
static bool AiEnumFollowTrack(TileIndex tile, AiRailPathFindData *a, int track, uint length) |
0 | 1902 |
{ |
2639 | 1903 |
if (a->flag) return true; |
0 | 1904 |
|
1905 |
if (length > 20 || tile == a->tile) { |
|
1906 |
a->flag = true; |
|
1907 |
return true; |
|
1908 |
} |
|
1909 |
||
2639 | 1910 |
if (DistanceMax(tile, a->tile2) < 4) a->count++; |
0 | 1911 |
|
1912 |
return false; |
|
1913 |
} |
|
1914 |
||
2643 | 1915 |
static bool AiDoFollowTrack(const Player* p) |
0 | 1916 |
{ |
1917 |
AiRailPathFindData arpfd; |
|
2639 | 1918 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1919 |
arpfd.tile = _players_ai[p->index].start_tile_a; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1920 |
arpfd.tile2 = _players_ai[p->index].cur_tile_a; |
0 | 1921 |
arpfd.flag = false; |
1922 |
arpfd.count = 0; |
|
9296
5878a50c67b2
(svn r12540) -Codechange: Enumify some values in original pathfinder and remove an unused variable.
frosch
parents:
9291
diff
changeset
|
1923 |
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 | 1924 |
(TPFEnumProc*)AiEnumFollowTrack, NULL, &arpfd); |
1925 |
return arpfd.count > 8; |
|
1926 |
} |
|
1927 |
||
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
1928 |
struct AiRailFinder { |
0 | 1929 |
TileIndex final_tile; |
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
1930 |
DiagDirection final_dir; |
0 | 1931 |
byte depth; |
1932 |
byte recursive_mode; |
|
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
1933 |
DiagDirection cur_best_dir; |
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
1934 |
DiagDirection best_dir; |
0 | 1935 |
byte cur_best_depth; |
1936 |
byte best_depth; |
|
1937 |
uint cur_best_dist; |
|
1938 |
const byte *best_ptr; |
|
1939 |
uint best_dist; |
|
1940 |
TileIndex cur_best_tile, best_tile; |
|
1941 |
TileIndex bridge_end_tile; |
|
1942 |
Player *player; |
|
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
1943 |
}; |
0 | 1944 |
|
1945 |
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
|
1946 |
{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
|
1947 |
{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
|
1948 |
{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
|
1949 |
{1, 3, 5, 0, 3, 2, 128 + 3, 67} |
0 | 1950 |
}; |
1951 |
||
1952 |
||
2630 | 1953 |
static bool AiIsTileBanned(const Player* p, TileIndex tile, byte val) |
1954 |
{ |
|
0 | 1955 |
int i; |
1956 |
||
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1957 |
for (i = 0; i != _players_ai[p->index].banned_tile_count; i++) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1958 |
if (_players_ai[p->index].banned_tiles[i] == tile && _players_ai[p->index].banned_val[i] == val) { |
2639 | 1959 |
return true; |
1960 |
} |
|
1961 |
} |
|
0 | 1962 |
return false; |
1963 |
} |
|
1964 |
||
2639 | 1965 |
static void AiBanTile(Player* p, TileIndex tile, byte val) |
1966 |
{ |
|
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
1967 |
uint i; |
0 | 1968 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1969 |
for (i = lengthof(_players_ai[p->index].banned_tiles) - 1; i != 0; i--) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1970 |
_players_ai[p->index].banned_tiles[i] = _players_ai[p->index].banned_tiles[i - 1]; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1971 |
_players_ai[p->index].banned_val[i] = _players_ai[p->index].banned_val[i - 1]; |
0 | 1972 |
} |
1973 |
||
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1974 |
_players_ai[p->index].banned_tiles[0] = tile; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1975 |
_players_ai[p->index].banned_val[0] = val; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1976 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1977 |
if (_players_ai[p->index].banned_tile_count != lengthof(_players_ai[p->index].banned_tiles)) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
1978 |
_players_ai[p->index].banned_tile_count++; |
2639 | 1979 |
} |
0 | 1980 |
} |
1981 |
||
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
1982 |
static void AiBuildRailRecursive(AiRailFinder *arf, TileIndex tile, DiagDirection dir); |
0 | 1983 |
|
1984 |
static bool AiCheckRailPathBetter(AiRailFinder *arf, const byte *p) |
|
1985 |
{ |
|
1986 |
bool better = false; |
|
1987 |
||
1988 |
if (arf->recursive_mode < 1) { |
|
1989 |
// Mode is 0. This means destination has not been found yet. |
|
1990 |
// If the found path is shorter than the current one, remember it. |
|
1991 |
if (arf->cur_best_dist < arf->best_dist) { |
|
1992 |
arf->best_dir = arf->cur_best_dir; |
|
1993 |
arf->best_dist = arf->cur_best_dist; |
|
1994 |
arf->best_ptr = p; |
|
1995 |
arf->best_tile = arf->cur_best_tile; |
|
1996 |
better = true; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
1997 |
} |
0 | 1998 |
} else if (arf->recursive_mode > 1) { |
1999 |
// Mode is 2. |
|
2000 |
if (arf->best_dist != 0 || arf->cur_best_depth < arf->best_depth) { |
|
2001 |
arf->best_depth = arf->cur_best_depth; |
|
2002 |
arf->best_dist = 0; |
|
2003 |
arf->best_ptr = p; |
|
2004 |
arf->best_tile = 0; |
|
2005 |
better = true; |
|
2006 |
} |
|
2007 |
} |
|
2008 |
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
|
2009 |
arf->cur_best_dist = UINT_MAX; |
0 | 2010 |
arf->cur_best_depth = 0xff; |
2011 |
||
2012 |
return better; |
|
2013 |
} |
|
2014 |
||
536 | 2015 |
static inline void AiCheckBuildRailBridgeHere(AiRailFinder *arf, TileIndex tile, const byte *p) |
0 | 2016 |
{ |
3644
45a307767dc1
(svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents:
3609
diff
changeset
|
2017 |
Slope tileh; |
3088
4570504ac734
(svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents:
3071
diff
changeset
|
2018 |
uint z; |
0 | 2019 |
bool flag; |
2020 |
||
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
2021 |
DiagDirection dir2 = (DiagDirection)(p[0] & 3); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2022 |
|
3088
4570504ac734
(svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents:
3071
diff
changeset
|
2023 |
tileh = GetTileSlope(tile, &z); |
9291
1fb2b3c2099f
(svn r12533) -Cleanup: Remove some wizardry from default ai by using existings enums and helper functions.
frosch
parents:
9282
diff
changeset
|
2024 |
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
|
2025 |
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
|
2026 |
|
0 | 2027 |
// Allow bridges directly over bottom tiles |
3088
4570504ac734
(svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents:
3071
diff
changeset
|
2028 |
flag = z == 0; |
2952 | 2029 |
for (;;) { |
3088
4570504ac734
(svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents:
3071
diff
changeset
|
2030 |
TileType type; |
4570504ac734
(svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents:
3071
diff
changeset
|
2031 |
|
4559
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4527
diff
changeset
|
2032 |
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
|
2033 |
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
|
2034 |
type = GetTileType(tile_new); |
4570504ac734
(svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents:
3071
diff
changeset
|
2035 |
|
3644
45a307767dc1
(svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents:
3609
diff
changeset
|
2036 |
if (type == MP_CLEAR || type == MP_TREES || GetTileSlope(tile_new, NULL) != SLOPE_FLAT) { |
0 | 2037 |
if (!flag) return; |
2038 |
break; |
|
2039 |
} |
|
7866
e19fda04e8d3
(svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents:
7813
diff
changeset
|
2040 |
if (type != MP_WATER && type != MP_RAILWAY && type != MP_ROAD) return; |
0 | 2041 |
flag = true; |
2042 |
} |
|
2043 |
||
2044 |
// Is building a (rail)bridge possible at this place (type doesn't matter)? |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2045 |
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
|
2046 |
return; |
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
2047 |
} |
0 | 2048 |
AiBuildRailRecursive(arf, tile_new, dir2); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2049 |
|
0 | 2050 |
// At the bottom depth, check if the new path is better than the old one. |
2051 |
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
|
2052 |
if (AiCheckRailPathBetter(arf, p)) arf->bridge_end_tile = tile_new; |
0 | 2053 |
} |
2054 |
} |
|
2055 |
} |
|
2056 |
||
536 | 2057 |
static inline void AiCheckBuildRailTunnelHere(AiRailFinder *arf, TileIndex tile, const byte *p) |
0 | 2058 |
{ |
3088
4570504ac734
(svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents:
3071
diff
changeset
|
2059 |
uint z; |
4570504ac734
(svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents:
3071
diff
changeset
|
2060 |
|
9291
1fb2b3c2099f
(svn r12533) -Cleanup: Remove some wizardry from default ai by using existings enums and helper functions.
frosch
parents:
9282
diff
changeset
|
2061 |
if (GetTileSlope(tile, &z) == InclinedSlope((DiagDirection)(p[0] & 3)) && z != 0) { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2062 |
CommandCost cost = DoCommand(tile, _players_ai[arf->player->index].railtype_to_use, 0, DC_AUTO, CMD_BUILD_TUNNEL); |
0 | 2063 |
|
7446
1c4d469f986e
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents:
7442
diff
changeset
|
2064 |
if (CmdSucceeded(cost) && cost.GetCost() <= (arf->player->player_money >> 4)) { |
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
2065 |
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
|
2066 |
if (arf->depth == 1) AiCheckRailPathBetter(arf, p); |
0 | 2067 |
} |
2068 |
} |
|
2069 |
} |
|
2070 |
||
2071 |
||
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
2072 |
static void AiBuildRailRecursive(AiRailFinder *arf, TileIndex tile, DiagDirection dir) |
0 | 2073 |
{ |
2074 |
const byte *p; |
|
2075 |
||
4559
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4527
diff
changeset
|
2076 |
tile = TILE_MASK(tile + TileOffsByDiagDir(dir)); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2077 |
|
0 | 2078 |
// Reached destination? |
2079 |
if (tile == arf->final_tile) { |
|
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
2080 |
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
|
2081 |
if (arf->recursive_mode != 2) arf->recursive_mode = 1; |
0 | 2082 |
} else if (arf->recursive_mode != 2) { |
2083 |
arf->recursive_mode = 2; |
|
2084 |
arf->cur_best_depth = arf->depth; |
|
2085 |
} else { |
|
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
2086 |
if (arf->depth < arf->cur_best_depth) arf->cur_best_depth = arf->depth; |
0 | 2087 |
} |
2088 |
return; |
|
2089 |
} |
|
2090 |
||
2091 |
// Depth too deep? |
|
2092 |
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
|
2093 |
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
|
2094 |
|
0 | 2095 |
if (dist < arf->cur_best_dist) { |
2096 |
// Store the tile that is closest to the final position. |
|
2097 |
arf->cur_best_depth = arf->depth; |
|
2098 |
arf->cur_best_dist = dist; |
|
2099 |
arf->cur_best_tile = tile; |
|
2100 |
arf->cur_best_dir = dir; |
|
2101 |
} |
|
2102 |
return; |
|
2103 |
} |
|
2104 |
||
2105 |
// Increase recursion depth |
|
2106 |
arf->depth++; |
|
2107 |
||
2108 |
// Grab pointer to list of stuff that is possible to build |
|
2109 |
p = _ai_table_15[dir]; |
|
2110 |
||
2111 |
// 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
|
2112 |
if (GetTileZ(tile) == 0) { |
0 | 2113 |
p += 6; |
2114 |
} else { |
|
2115 |
do { |
|
2116 |
// Make sure the tile is not in the list of banned tiles and that a rail can be built here. |
|
2117 |
if (!AiIsTileBanned(arf->player, tile, p[0]) && |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2118 |
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))) { |
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
2119 |
AiBuildRailRecursive(arf, tile, (DiagDirection)p[1]); |
0 | 2120 |
} |
2121 |
||
2122 |
// 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
|
2123 |
if (arf->depth == 1) AiCheckRailPathBetter(arf, p); |
0 | 2124 |
|
2125 |
p += 2; |
|
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
2126 |
} while (!(p[0] & 0x80)); |
0 | 2127 |
} |
2128 |
||
2129 |
AiCheckBuildRailBridgeHere(arf, tile, p); |
|
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
2130 |
AiCheckBuildRailTunnelHere(arf, tile, p + 1); |
0 | 2131 |
|
2132 |
arf->depth--; |
|
2133 |
} |
|
2134 |
||
2135 |
||
2136 |
static void AiBuildRailConstruct(Player *p) |
|
2137 |
{ |
|
2138 |
AiRailFinder arf; |
|
2139 |
int i; |
|
2140 |
||
2141 |
// Check too much lookahead? |
|
2142 |
if (AiDoFollowTrack(p)) { |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2143 |
_players_ai[p->index].state_counter = (Random()&0xE)+6; // Destruct this amount of blocks |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2144 |
_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
|
2145 |
|
0 | 2146 |
// Ban this tile and don't reach it for a while. |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2147 |
AiBanTile(p, _players_ai[p->index].cur_tile_a, FindFirstBit(GetRailTrackStatus(_players_ai[p->index].cur_tile_a))); |
0 | 2148 |
return; |
2149 |
} |
|
2150 |
||
2151 |
// Setup recursive finder and call it. |
|
2152 |
arf.player = p; |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2153 |
arf.final_tile = _players_ai[p->index].cur_tile_b; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2154 |
arf.final_dir = _players_ai[p->index].cur_dir_b; |
0 | 2155 |
arf.depth = 0; |
2156 |
arf.recursive_mode = 0; |
|
2157 |
arf.best_ptr = NULL; |
|
2158 |
arf.cur_best_dist = (uint)-1; |
|
2159 |
arf.cur_best_depth = 0xff; |
|
2160 |
arf.best_dist = (uint)-1; |
|
2161 |
arf.best_depth = 0xff; |
|
2162 |
arf.cur_best_tile = 0; |
|
2163 |
arf.best_tile = 0; |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2164 |
AiBuildRailRecursive(&arf, _players_ai[p->index].cur_tile_a, _players_ai[p->index].cur_dir_a); |
0 | 2165 |
|
2166 |
// Reached destination? |
|
2167 |
if (arf.recursive_mode == 2 && arf.cur_best_depth == 0) { |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2168 |
_players_ai[p->index].state_mode = 255; |
0 | 2169 |
return; |
2170 |
} |
|
2171 |
||
2172 |
// Didn't find anything to build? |
|
2173 |
if (arf.best_ptr == NULL) { |
|
2174 |
// 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
|
2175 |
for (i = 0; i != 5; i++) { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2176 |
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
|
2177 |
} |
0 | 2178 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2179 |
if (++_players_ai[p->index].state_counter == 21) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2180 |
_players_ai[p->index].state_counter = 40; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2181 |
_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
|
2182 |
|
0 | 2183 |
// Ban this tile |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2184 |
AiBanTile(p, _players_ai[p->index].cur_tile_a, FindFirstBit(GetRailTrackStatus(_players_ai[p->index].cur_tile_a))); |
0 | 2185 |
} |
2186 |
return; |
|
2187 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2188 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2189 |
_players_ai[p->index].cur_tile_a += TileOffsByDiagDir(_players_ai[p->index].cur_dir_a); |
0 | 2190 |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2191 |
if (arf.best_ptr[0] & 0x80) { |
9113
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2192 |
TileIndex t1 = _players_ai[p->index].cur_tile_a; |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2193 |
TileIndex t2 = arf.bridge_end_tile; |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2194 |
|
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2195 |
int32 bridge_len = GetTunnelBridgeLength(t1, t2); |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2196 |
|
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2197 |
DiagDirection dir = (TileX(t1) == TileX(t2) ? DIAGDIR_SE : DIAGDIR_SW); |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2198 |
Track track = AxisToTrack(DiagDirToAxis(dir)); |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2199 |
|
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2200 |
if (t2 < t1) dir = ReverseDiagDir(dir); |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2201 |
|
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2202 |
/* try to build a long rail instead of bridge... */ |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2203 |
bool fail = false; |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2204 |
CommandCost cost; |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2205 |
TileIndex t = t1; |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2206 |
|
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2207 |
/* 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 */ |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2208 |
do { |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2209 |
cost = DoCommand(t, _players_ai[p->index].railtype_to_use, track, DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL); |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2210 |
/* do not allow building over existing track */ |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2211 |
if (CmdFailed(cost) || IsTileType(t, MP_RAILWAY)) { |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2212 |
fail = true; |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2213 |
break; |
0 | 2214 |
} |
9113
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2215 |
t += TileOffsByDiagDir(dir); |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2216 |
} while (t != t2); |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2217 |
|
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2218 |
/* can we build long track? */ |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2219 |
if (!fail) cost = DoCommand(t1, t2, _players_ai[p->index].railtype_to_use | (track << 4), DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_TRACK); |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2220 |
|
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2221 |
if (!fail && CmdSucceeded(cost) && cost.GetCost() <= p->player_money) { |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2222 |
DoCommand(t1, t2, _players_ai[p->index].railtype_to_use | (track << 4), DC_AUTO | DC_NO_WATER | DC_EXEC, CMD_BUILD_RAILROAD_TRACK); |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2223 |
} else { |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2224 |
|
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2225 |
/* Figure out which (rail)bridge type to build |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2226 |
* start with best bridge, then go down to worse and worse bridges |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2227 |
* unnecessary to check for worst bridge (i=0), since AI will always build that. */ |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2228 |
int i; |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2229 |
for (i = MAX_BRIDGES - 1; i != 0; i--) { |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2230 |
if (CheckBridge_Stuff(i, bridge_len)) { |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2231 |
CommandCost cost = DoCommand(t1, t2, i | (_players_ai[p->index].railtype_to_use << 8), DC_AUTO, CMD_BUILD_BRIDGE); |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2232 |
if (CmdSucceeded(cost) && cost.GetCost() < (p->player_money >> 1) && cost.GetCost() < ((p->player_money + _economy.max_loan - p->current_loan) >> 5)) break; |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2233 |
} |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2234 |
} |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2235 |
|
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2236 |
/* Build it */ |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2237 |
DoCommand(t1, t2, i | (_players_ai[p->index].railtype_to_use << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE); |
0 | 2238 |
} |
2239 |
||
9113
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
2240 |
_players_ai[p->index].cur_tile_a = t2; |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2241 |
_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
|
2242 |
} else if (arf.best_ptr[0] & 0x40) { |
0 | 2243 |
// tunnel |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2244 |
DoCommand(_players_ai[p->index].cur_tile_a, _players_ai[p->index].railtype_to_use, 0, DC_AUTO | DC_EXEC, CMD_BUILD_TUNNEL); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2245 |
_players_ai[p->index].cur_tile_a = _build_tunnel_endtile; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2246 |
_players_ai[p->index].state_counter = 0; |
0 | 2247 |
} else { |
2248 |
// rail |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2249 |
_players_ai[p->index].cur_dir_a = (DiagDirection)(arf.best_ptr[1] & 3); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2250 |
DoCommand(_players_ai[p->index].cur_tile_a, _players_ai[p->index].railtype_to_use, arf.best_ptr[0], |
0 | 2251 |
DC_EXEC | DC_AUTO | DC_NO_WATER | DC_NO_RAIL_OVERLAP, CMD_BUILD_SINGLE_RAIL); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2252 |
_players_ai[p->index].state_counter = 0; |
0 | 2253 |
} |
2254 |
||
2255 |
if (arf.best_tile != 0) { |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2256 |
for (i = 0; i != 2; i++) { |
0 | 2257 |
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
|
2258 |
} |
0 | 2259 |
} |
2260 |
} |
|
2261 |
||
2262 |
static bool AiRemoveTileAndGoForward(Player *p) |
|
2263 |
{ |
|
2264 |
const byte *ptr; |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2265 |
TileIndex tile = _players_ai[p->index].cur_tile_a; |
0 | 2266 |
TileIndex tilenew; |
2267 |
||
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
|
2268 |
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
|
2269 |
if (IsTunnel(tile)) { |
0 | 2270 |
// 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
|
2271 |
if (CmdFailed(DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR))) |
0 | 2272 |
return false; |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2273 |
_players_ai[p->index].cur_tile_a = TILE_MASK(_build_tunnel_endtile - TileOffsByDiagDir(_players_ai[p->index].cur_dir_a)); |
0 | 2274 |
return true; |
8886
9f2c7ebc7fc9
(svn r11960) -Cleanup: simplify some IsTunnel(Tile) / IsBridge(Tile) conditions
smatz
parents:
8885
diff
changeset
|
2275 |
} else { // IsBridge(tile) |
0 | 2276 |
// Check if the bridge points in the right direction. |
2277 |
// This is not really needed the first place AiRemoveTileAndGoForward is called. |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2278 |
if (DiagDirToAxis(GetTunnelBridgeDirection(tile)) != (_players_ai[p->index].cur_dir_a & 1)) return false; |
0 | 2279 |
|
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
|
2280 |
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
|
2281 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2282 |
tilenew = TILE_MASK(tile - TileOffsByDiagDir(_players_ai[p->index].cur_dir_a)); |
0 | 2283 |
// 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
|
2284 |
if (CmdFailed(DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR))) |
0 | 2285 |
return false; |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2286 |
_players_ai[p->index].cur_tile_a = tilenew; |
0 | 2287 |
return true; |
2288 |
} |
|
2289 |
} |
|
2290 |
||
2291 |
// Find the railtype at the position. Quit if no rail there. |
|
9291
1fb2b3c2099f
(svn r12533) -Cleanup: Remove some wizardry from default ai by using existings enums and helper functions.
frosch
parents:
9282
diff
changeset
|
2292 |
TrackBits bits = GetRailTrackStatus(tile) & DiagdirReachesTracks(ReverseDiagDir(_players_ai[p->index].cur_dir_a)); |
1fb2b3c2099f
(svn r12533) -Cleanup: Remove some wizardry from default ai by using existings enums and helper functions.
frosch
parents:
9282
diff
changeset
|
2293 |
if (bits == TRACK_BIT_NONE) return false; |
0 | 2294 |
|
2295 |
// Convert into a bit position that CMD_REMOVE_SINGLE_RAIL expects. |
|
9291
1fb2b3c2099f
(svn r12533) -Cleanup: Remove some wizardry from default ai by using existings enums and helper functions.
frosch
parents:
9282
diff
changeset
|
2296 |
Track track = FindFirstTrack(bits); |
0 | 2297 |
|
2298 |
// 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
|
2299 |
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
|
2300 |
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
|
2301 |
DoCommand(tile, 0, 0, DC_EXEC, CMD_REMOVE_SIGNALS); |
0 | 2302 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2303 |
|
0 | 2304 |
// And also remove the rail. |
9291
1fb2b3c2099f
(svn r12533) -Cleanup: Remove some wizardry from default ai by using existings enums and helper functions.
frosch
parents:
9282
diff
changeset
|
2305 |
if (CmdFailed(DoCommand(tile, 0, track, DC_EXEC, CMD_REMOVE_SINGLE_RAIL))) |
0 | 2306 |
return false; |
2307 |
||
2308 |
// Find the direction at the other edge of the rail. |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2309 |
ptr = _ai_table_15[ReverseDiagDir(_players_ai[p->index].cur_dir_a)]; |
9291
1fb2b3c2099f
(svn r12533) -Cleanup: Remove some wizardry from default ai by using existings enums and helper functions.
frosch
parents:
9282
diff
changeset
|
2310 |
while (ptr[0] != track) ptr += 2; |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2311 |
_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
|
2312 |
|
0 | 2313 |
// And then also switch tile. |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2314 |
_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 | 2315 |
|
2316 |
return true; |
|
2317 |
} |
|
2318 |
||
2319 |
||
2320 |
static void AiBuildRailDestruct(Player *p) |
|
2321 |
{ |
|
2322 |
// Decrease timeout. |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2323 |
if (!--_players_ai[p->index].state_counter) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2324 |
_players_ai[p->index].state_mode = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2325 |
_players_ai[p->index].state_counter = 0; |
0 | 2326 |
} |
2327 |
||
2328 |
// Don't do anything if the destination is already reached. |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2329 |
if (_players_ai[p->index].cur_tile_a == _players_ai[p->index].start_tile_a) return; |
0 | 2330 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2331 |
AiRemoveTileAndGoForward(p); |
0 | 2332 |
} |
2333 |
||
2334 |
||
2335 |
static void AiBuildRail(Player *p) |
|
2336 |
{ |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2337 |
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
|
2338 |
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
|
2339 |
AiBuildRailConstruct(p); |
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
2340 |
break; |
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
2341 |
|
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
2342 |
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
|
2343 |
AiBuildRailDestruct(p); |
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
2344 |
break; |
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
2345 |
|
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
2346 |
case 2: { |
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
2347 |
uint i; |
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 |
// 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
|
2350 |
for (i = 0; i != 4; i++) { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2351 |
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
|
2352 |
} |
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
2353 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2354 |
if (++_players_ai[p->index].state_counter == 4) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2355 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2356 |
_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
|
2357 |
} |
0 | 2358 |
} |
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
2359 |
|
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
2360 |
default: break; |
0 | 2361 |
} |
2362 |
} |
|
2363 |
||
2364 |
static void AiStateBuildRail(Player *p) |
|
2365 |
{ |
|
2366 |
int num; |
|
2367 |
AiBuildRec *aib; |
|
2368 |
byte cmd; |
|
2369 |
TileIndex tile; |
|
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
2370 |
DiagDirection dir; |
0 | 2371 |
|
2372 |
// time out? |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2373 |
if (++_players_ai[p->index].timeout_counter == 1388) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2374 |
_players_ai[p->index].state = AIS_DELETE_RAIL_BLOCKS; |
0 | 2375 |
return; |
2376 |
} |
|
2377 |
||
2378 |
// Currently building a rail between two points? |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2379 |
if (_players_ai[p->index].state_mode != 255) { |
0 | 2380 |
AiBuildRail(p); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2381 |
|
0 | 2382 |
// Alternate between edges |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2383 |
Swap(_players_ai[p->index].start_tile_a, _players_ai[p->index].start_tile_b); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2384 |
Swap(_players_ai[p->index].cur_tile_a, _players_ai[p->index].cur_tile_b); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2385 |
Swap(_players_ai[p->index].start_dir_a, _players_ai[p->index].start_dir_b); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2386 |
Swap(_players_ai[p->index].cur_dir_a, _players_ai[p->index].cur_dir_b); |
0 | 2387 |
return; |
2388 |
} |
|
2389 |
||
2390 |
// Now, find two new points to build between |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2391 |
num = _players_ai[p->index].num_build_rec; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2392 |
aib = &_players_ai[p->index].src; |
0 | 2393 |
|
2952 | 2394 |
for (;;) { |
0 | 2395 |
cmd = aib->buildcmd_a; |
2396 |
aib->buildcmd_a = 255; |
|
2397 |
if (cmd != 255) break; |
|
2398 |
||
2399 |
cmd = aib->buildcmd_b; |
|
2400 |
aib->buildcmd_b = 255; |
|
2401 |
if (cmd != 255) break; |
|
2402 |
||
2403 |
aib++; |
|
2404 |
if (--num == 0) { |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2405 |
_players_ai[p->index].state = AIS_BUILD_RAIL_VEH; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2406 |
_players_ai[p->index].state_counter = 0; // timeout |
0 | 2407 |
return; |
2408 |
} |
|
2409 |
} |
|
2410 |
||
2411 |
// Find first edge to build from. |
|
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
2412 |
tile = AiGetEdgeOfDefaultRailBlock(aib->cur_building_rule, aib->use_tile, cmd & 3, &dir); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2413 |
_players_ai[p->index].start_tile_a = tile; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2414 |
_players_ai[p->index].cur_tile_a = tile; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2415 |
_players_ai[p->index].start_dir_a = dir; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2416 |
_players_ai[p->index].cur_dir_a = dir; |
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
2417 |
DoCommand(TILE_MASK(tile + TileOffsByDiagDir(dir)), 0, (dir & 1) ? 1 : 0, DC_EXEC, CMD_REMOVE_SINGLE_RAIL); |
0 | 2418 |
|
2419 |
assert(TILE_MASK(tile) != 0xFF00); |
|
2420 |
||
2421 |
// Find second edge to build to |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2422 |
aib = (&_players_ai[p->index].src) + ((cmd >> 4) & 0xF); |
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
2423 |
tile = AiGetEdgeOfDefaultRailBlock(aib->cur_building_rule, aib->use_tile, (cmd >> 2) & 3, &dir); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2424 |
_players_ai[p->index].start_tile_b = tile; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2425 |
_players_ai[p->index].cur_tile_b = tile; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2426 |
_players_ai[p->index].start_dir_b = dir; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2427 |
_players_ai[p->index].cur_dir_b = dir; |
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
2428 |
DoCommand(TILE_MASK(tile + TileOffsByDiagDir(dir)), 0, (dir & 1) ? 1 : 0, DC_EXEC, CMD_REMOVE_SINGLE_RAIL); |
0 | 2429 |
|
2430 |
assert(TILE_MASK(tile) != 0xFF00); |
|
2431 |
||
2432 |
// And setup state. |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2433 |
_players_ai[p->index].state_mode = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2434 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2435 |
_players_ai[p->index].banned_tile_count = 0; |
0 | 2436 |
} |
2437 |
||
3315
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3269
diff
changeset
|
2438 |
static StationID AiGetStationIdByDef(TileIndex tile, int id) |
0 | 2439 |
{ |
2440 |
const AiDefaultBlockData *p = _default_rail_track_data[id]->data; |
|
2441 |
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
|
2442 |
return GetStationIndex(TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))); |
0 | 2443 |
} |
2444 |
||
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
|
2445 |
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
|
2446 |
{ |
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
|
2447 |
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
|
2448 |
EngineID i; |
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3348
diff
changeset
|
2449 |
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
|
2450 |
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
|
2451 |
uint speed; |
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3348
diff
changeset
|
2452 |
|
9251
f41952647dd4
(svn r12451) -Codechange: use of FOR_ALL_ENGINES/ENGINEIDS_OF_TYPE instead of for-loops
peter1138
parents:
9113
diff
changeset
|
2453 |
FOR_ALL_ENGINEIDS_OF_TYPE(i, VEH_TRAIN) { |
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
|
2454 |
const RailVehicleInfo *rvi = RailVehInfo(i); |
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3348
diff
changeset
|
2455 |
const Engine* e = GetEngine(i); |
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3348
diff
changeset
|
2456 |
|
6074 | 2457 |
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
|
2458 |
rvi->railveh_type != RAILVEH_WAGON || |
8424
4a488a90ccab
(svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents:
8149
diff
changeset
|
2459 |
!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
|
2460 |
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
|
2461 |
} |
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
|
2462 |
|
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
|
2463 |
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
|
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 |
/* 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
|
2466 |
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
|
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 |
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
|
2469 |
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
|
2470 |
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
|
2471 |
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
|
2472 |
} |
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 |
} |
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 |
|
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 |
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
|
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 |
|
0 | 2478 |
static void AiStateBuildRailVeh(Player *p) |
2479 |
{ |
|
2480 |
const AiDefaultBlockData *ptr; |
|
2481 |
TileIndex tile; |
|
2544 | 2482 |
EngineID veh; |
2483 |
int i; |
|
3348 | 2484 |
CargoID cargo; |
7439
0c0e2945c890
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents:
7362
diff
changeset
|
2485 |
CommandCost cost; |
0 | 2486 |
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
|
2487 |
VehicleID loco_id; |
0 | 2488 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2489 |
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
|
2490 |
while (ptr->mode != 0) ptr++; |
0 | 2491 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2492 |
tile = TILE_ADD(_players_ai[p->index].src.use_tile, ToTileIndexDiff(ptr->tileoffs)); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2493 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2494 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2495 |
cargo = _players_ai[p->index].cargo_type; |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2496 |
for (i = 0;;) { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2497 |
if (_players_ai[p->index].wagon_list[i] == INVALID_VEHICLE) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2498 |
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
|
2499 |
/* 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
|
2500 |
* 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
|
2501 |
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
|
2502 |
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
|
2503 |
if (CmdFailed(cost)) goto handle_nocash; |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2504 |
_players_ai[p->index].wagon_list[i] = _new_vehicle_id; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2505 |
_players_ai[p->index].wagon_list[i + 1] = INVALID_VEHICLE; |
0 | 2506 |
return; |
2507 |
} |
|
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
|
2508 |
if (cargo == CT_MAIL) cargo = CT_PASSENGERS; |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2509 |
if (++i == _players_ai[p->index].num_wagons * 2 - 1) break; |
0 | 2510 |
} |
2511 |
||
2512 |
// Which locomotive to build? |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2513 |
veh = AiChooseTrainToBuild(_players_ai[p->index].railtype_to_use, p->player_money, cargo != CT_PASSENGERS ? 1 : 0, tile); |
2544 | 2514 |
if (veh == INVALID_ENGINE) { |
0 | 2515 |
handle_nocash: |
2516 |
// after a while, if AI still doesn't have cash, get out of this block by selling the wagons. |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2517 |
if (++_players_ai[p->index].state_counter == 1000) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2518 |
for (i = 0; _players_ai[p->index].wagon_list[i] != INVALID_VEHICLE; i++) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2519 |
cost = DoCommand(tile, _players_ai[p->index].wagon_list[i], 0, DC_EXEC, CMD_SELL_RAIL_WAGON); |
7442
a4f7989f27d4
(svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents:
7439
diff
changeset
|
2520 |
assert(CmdSucceeded(cost)); |
0 | 2521 |
} |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2522 |
_players_ai[p->index].state = AIS_0; |
0 | 2523 |
} |
2524 |
return; |
|
2525 |
} |
|
2526 |
||
2527 |
// 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
|
2528 |
cost = DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE); |
7442
a4f7989f27d4
(svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents:
7439
diff
changeset
|
2529 |
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
|
2530 |
loco_id = _new_vehicle_id; |
0 | 2531 |
|
2532 |
// 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
|
2533 |
v = GetVehicle(loco_id); |
7988
6075538f6111
(svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents:
7986
diff
changeset
|
2534 |
if (v->Next() != NULL) { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2535 |
i = _players_ai[p->index].wagon_list[_players_ai[p->index].num_wagons * 2 - 2]; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2536 |
_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
|
2537 |
DoCommand(tile, i, 0, DC_EXEC, CMD_SELL_RAIL_WAGON); |
0 | 2538 |
} |
2539 |
||
2540 |
// Move the wagons onto the train |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2541 |
for (i = 0; _players_ai[p->index].wagon_list[i] != INVALID_VEHICLE; i++) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2542 |
DoCommand(tile, _players_ai[p->index].wagon_list[i] | (loco_id << 16), 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); |
0 | 2543 |
} |
2544 |
||
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2545 |
for (i = 0; _players_ai[p->index].order_list_blocks[i] != 0xFF; i++) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2546 |
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
|
2547 |
bool is_pass = ( |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2548 |
_players_ai[p->index].cargo_type == CT_PASSENGERS || |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2549 |
_players_ai[p->index].cargo_type == CT_MAIL || |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2550 |
(_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
|
2551 |
); |
558
9b115b39c515
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
2552 |
Order order; |
9b115b39c515
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
2553 |
|
9332
2e120d0bd632
(svn r12584) -Codechange: do not access the order type directly.
rubidium
parents:
9296
diff
changeset
|
2554 |
order.MakeGoToStation(AiGetStationIdByDef(aib->use_tile, aib->cur_building_rule)); |
558
9b115b39c515
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
2555 |
|
10081
e46b9eb2f9c1
(svn r12617) -Codechange: add type safety to the Order's load and unload types.
rubidium
parents:
9339
diff
changeset
|
2556 |
if (!is_pass && i == 1) order.SetUnloadType(OUFB_UNLOAD); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2557 |
if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0)) |
10081
e46b9eb2f9c1
(svn r12617) -Codechange: add type safety to the Order's load and unload types.
rubidium
parents:
9339
diff
changeset
|
2558 |
order.SetLoadType(OLFB_FULL_LOAD); |
558
9b115b39c515
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
2559 |
|
9335
18d3658adc60
(svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents:
9332
diff
changeset
|
2560 |
DoCommand(0, loco_id + (i << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER); |
0 | 2561 |
} |
2562 |
||
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3473
diff
changeset
|
2563 |
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
|
2564 |
|
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3473
diff
changeset
|
2565 |
DoCommand(0, loco_id, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT); |
0 | 2566 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2567 |
if (_players_ai[p->index].num_want_fullload != 0) _players_ai[p->index].num_want_fullload--; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2568 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2569 |
if (--_players_ai[p->index].num_loco_to_build != 0) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2570 |
// _players_ai[p->index].loco_id = INVALID_VEHICLE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2571 |
_players_ai[p->index].wagon_list[0] = INVALID_VEHICLE; |
0 | 2572 |
} else { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2573 |
_players_ai[p->index].state = AIS_0; |
0 | 2574 |
} |
2575 |
} |
|
2576 |
||
2577 |
static void AiStateDeleteRailBlocks(Player *p) |
|
2578 |
{ |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2579 |
const AiBuildRec* aib = &_players_ai[p->index].src; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2580 |
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
|
2581 |
|
0 | 2582 |
do { |
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
2583 |
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
|
2584 |
|
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
2585 |
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
|
2586 |
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
|
2587 |
DoCommand(TILE_ADD(aib->use_tile, ToTileIndexDiff(b->tileoffs)), 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); |
0 | 2588 |
} |
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
2589 |
} while (++aib, --num); |
0 | 2590 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2591 |
_players_ai[p->index].state = AIS_0; |
0 | 2592 |
} |
2593 |
||
2594 |
static bool AiCheckRoadResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo) |
|
2595 |
{ |
|
2596 |
uint values[NUM_CARGO]; |
|
568
6eb7d394fb35
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
2597 |
int rad; |
830
8114845804c9
(svn r1301) -Codechange: _industries finally has FOR_ALL_INDUSTRIES too
truelight
parents:
679
diff
changeset
|
2598 |
|
568
6eb7d394fb35
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
2599 |
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
|
2600 |
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
|
2601 |
} else { // change that at some point? |
568
6eb7d394fb35
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
2602 |
rad = 4; |
6eb7d394fb35
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
2603 |
} |
830
8114845804c9
(svn r1301) -Codechange: _industries finally has FOR_ALL_INDUSTRIES too
truelight
parents:
679
diff
changeset
|
2604 |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2605 |
for (;; p++) { |
0 | 2606 |
if (p->mode == 4) { |
2607 |
return true; |
|
2608 |
} else if (p->mode == 1) { |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1927
diff
changeset
|
2609 |
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
|
2610 |
|
0 | 2611 |
if (cargo & 0x80) { |
568
6eb7d394fb35
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
2612 |
GetProductionAroundTiles(values, tile2, 1, 1, rad); |
0 | 2613 |
return values[cargo & 0x7F] != 0; |
2614 |
} else { |
|
568
6eb7d394fb35
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
2615 |
GetAcceptanceAroundTiles(values, tile2, 1, 1, rad); |
0 | 2616 |
return (values[cargo]&~7) != 0; |
2617 |
} |
|
2618 |
} |
|
2619 |
} |
|
2620 |
} |
|
2621 |
||
2622 |
static bool _want_road_truck_station; |
|
7439
0c0e2945c890
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents:
7362
diff
changeset
|
2623 |
static CommandCost AiDoBuildDefaultRoadBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag); |
0 | 2624 |
|
2625 |
// Returns rule and cost |
|
7439
0c0e2945c890
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents:
7362
diff
changeset
|
2626 |
static int AiFindBestDefaultRoadBlock(TileIndex tile, byte direction, byte cargo, CommandCost *cost) |
0 | 2627 |
{ |
2628 |
int i; |
|
2629 |
const AiDefaultRoadBlock *p; |
|
2630 |
||
2631 |
_want_road_truck_station = (cargo & 0x7F) != CT_PASSENGERS; |
|
2632 |
||
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2633 |
for (i = 0; (p = _road_default_block_data[i]) != NULL; i++) { |
0 | 2634 |
if (p->dir == direction) { |
2635 |
*cost = AiDoBuildDefaultRoadBlock(tile, p->data, 0); |
|
7442
a4f7989f27d4
(svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents:
7439
diff
changeset
|
2636 |
if (CmdSucceeded(*cost) && AiCheckRoadResources(tile, p->data, cargo)) |
0 | 2637 |
return i; |
2638 |
} |
|
2639 |
} |
|
2640 |
||
2641 |
return -1; |
|
2642 |
} |
|
2643 |
||
7439
0c0e2945c890
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents:
7362
diff
changeset
|
2644 |
static CommandCost AiDoBuildDefaultRoadBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag) |
0 | 2645 |
{ |
7439
0c0e2945c890
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents:
7362
diff
changeset
|
2646 |
CommandCost ret; |
8726
5ae45b46506b
(svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents:
8725
diff
changeset
|
2647 |
CommandCost total_cost(EXPENSES_CONSTRUCTION); |
0 | 2648 |
Town *t = NULL; |
2649 |
int rating = 0; |
|
2650 |
int roadflag = 0; |
|
2651 |
||
2952 | 2652 |
for (;p->mode != 4;p++) { |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2653 |
TileIndex c = TILE_MASK(tile + ToTileIndexDiff(p->tileoffs)); |
0 | 2654 |
|
2655 |
_cleared_town = NULL; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2656 |
|
0 | 2657 |
if (p->mode == 2) { |
9059
04edde3eb0c6
(svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents:
9052
diff
changeset
|
2658 |
if (IsNormalRoadTile(c) && |
7157
270bc5b64e08
(svn r9892) -Codechange: lots of ground work for allowing multiple types of "road" with multiple owners on a single tile.
rubidium
parents:
7132
diff
changeset
|
2659 |
(GetRoadBits(c, ROADTYPE_ROAD) & p->attr) != 0) { |
0 | 2660 |
roadflag |= 2; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2661 |
|
0 | 2662 |
// all bits are already built? |
7157
270bc5b64e08
(svn r9892) -Codechange: lots of ground work for allowing multiple types of "road" with multiple owners on a single tile.
rubidium
parents:
7132
diff
changeset
|
2663 |
if ((GetRoadBits(c, ROADTYPE_ROAD) & p->attr) == p->attr) continue; |
0 | 2664 |
} |
2665 |
||
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3473
diff
changeset
|
2666 |
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
|
2667 |
if (CmdFailed(ret)) return CMD_ERROR; |
7446
1c4d469f986e
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents:
7442
diff
changeset
|
2668 |
total_cost.AddCost(ret); |
0 | 2669 |
|
2670 |
continue; |
|
2671 |
} |
|
2672 |
||
2673 |
if (p->mode == 0) { |
|
2674 |
// 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
|
2675 |
ret = DoCommand(c, p->attr, 0, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_ROAD_DEPOT); |
0 | 2676 |
goto clear_town_stuff; |
2677 |
} else if (p->mode == 1) { |
|
2678 |
if (_want_road_truck_station) { |
|
2679 |
// Truck station |
|
9281
d8cd9ac52a68
(svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents:
9280
diff
changeset
|
2680 |
ret = DoCommand(c, p->attr, ROADTYPES_ROAD << 2 | ROADSTOP_TRUCK, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_ROAD_STOP); |
0 | 2681 |
} else { |
2682 |
// Bus station |
|
9281
d8cd9ac52a68
(svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents:
9280
diff
changeset
|
2683 |
ret = DoCommand(c, p->attr, ROADTYPES_ROAD << 2 | ROADSTOP_BUS, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_ROAD_STOP); |
0 | 2684 |
} |
2685 |
clear_town_stuff:; |
|
2686 |
||
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
|
2687 |
if (CmdFailed(ret)) return CMD_ERROR; |
7446
1c4d469f986e
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents:
7442
diff
changeset
|
2688 |
total_cost.AddCost(ret); |
0 | 2689 |
|
2690 |
if (_cleared_town != NULL) { |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2691 |
if (t != NULL && t != _cleared_town) return CMD_ERROR; |
0 | 2692 |
t = _cleared_town; |
2693 |
rating += _cleared_town_rating; |
|
2694 |
} |
|
2695 |
} else if (p->mode == 3) { |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2696 |
if (flag & DC_EXEC) continue; |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2697 |
|
3644
45a307767dc1
(svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents:
3609
diff
changeset
|
2698 |
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
|
2699 |
|
9059
04edde3eb0c6
(svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents:
9052
diff
changeset
|
2700 |
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
|
2701 |
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
|
2702 |
if (CmdFailed(ret)) return CMD_ERROR; |
0 | 2703 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2704 |
|
0 | 2705 |
} |
2706 |
} |
|
2707 |
||
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2708 |
if (!_want_road_truck_station && !(roadflag & 2)) return CMD_ERROR; |
0 | 2709 |
|
2710 |
if (!(flag & DC_EXEC)) { |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2711 |
if (t != NULL && rating > t->ratings[_current_player]) return CMD_ERROR; |
0 | 2712 |
} |
2713 |
return total_cost; |
|
2714 |
} |
|
2715 |
||
2716 |
// Make sure the blocks are not too close to each other |
|
2717 |
static bool AiCheckBlockDistances(Player *p, TileIndex tile) |
|
2718 |
{ |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2719 |
const AiBuildRec* aib = &_players_ai[p->index].src; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2720 |
uint num = _players_ai[p->index].num_build_rec; |
0 | 2721 |
|
2722 |
do { |
|
2723 |
if (aib->cur_building_rule != 255) { |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2724 |
if (DistanceManhattan(aib->use_tile, tile) < 9) return false; |
0 | 2725 |
} |
2726 |
} while (++aib, --num); |
|
2727 |
||
2728 |
return true; |
|
2729 |
} |
|
2730 |
||
2731 |
||
2732 |
static void AiStateBuildDefaultRoadBlocks(Player *p) |
|
2733 |
{ |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2734 |
uint i; |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2735 |
int j; |
0 | 2736 |
AiBuildRec *aib; |
2737 |
int rule; |
|
7439
0c0e2945c890
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents:
7362
diff
changeset
|
2738 |
CommandCost cost; |
0 | 2739 |
|
2740 |
// time out? |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2741 |
if (++_players_ai[p->index].timeout_counter == 1388) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2742 |
_players_ai[p->index].state = AIS_DELETE_RAIL_BLOCKS; |
0 | 2743 |
return; |
2744 |
} |
|
2745 |
||
2746 |
// do the following 8 times |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2747 |
for (i = 0; i != 8; i++) { |
0 | 2748 |
// check if we can build the default track |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2749 |
aib = &_players_ai[p->index].src; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2750 |
j = _players_ai[p->index].num_build_rec; |
0 | 2751 |
do { |
2752 |
// this item has already been built? |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2753 |
if (aib->cur_building_rule != 255) continue; |
0 | 2754 |
|
2755 |
// adjust the coordinate randomly, |
|
2756 |
// to make sure that we find a position. |
|
2757 |
aib->use_tile = AdjustTileCoordRandomly(aib->spec_tile, aib->rand_rng); |
|
2758 |
||
2759 |
// 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
|
2760 |
rule = AiFindBestDefaultRoadBlock( |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2761 |
aib->use_tile, aib->direction, aib->cargo, &cost |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2762 |
); |
0 | 2763 |
|
2764 |
if (rule == -1) { |
|
2765 |
// cannot build, terraform after a while |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2766 |
if (_players_ai[p->index].state_counter >= 600) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2767 |
AiDoTerraformLand(aib->use_tile, (DiagDirection)(Random() & 3), 3, (int8)_players_ai[p->index].state_mode); |
0 | 2768 |
} |
2769 |
// also try the other terraform direction |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2770 |
if (++_players_ai[p->index].state_counter >= 1000) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2771 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2772 |
_players_ai[p->index].state_mode = -_players_ai[p->index].state_mode; |
0 | 2773 |
} |
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
2774 |
} else if (CheckPlayerHasMoney(cost) && AiCheckBlockDistances(p, aib->use_tile)) { |
7439
0c0e2945c890
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents:
7362
diff
changeset
|
2775 |
CommandCost r; |
0 | 2776 |
|
2777 |
// player has money, build it. |
|
2778 |
aib->cur_building_rule = rule; |
|
2779 |
||
2780 |
r = AiDoBuildDefaultRoadBlock( |
|
2781 |
aib->use_tile, |
|
2782 |
_road_default_block_data[rule]->data, |
|
2783 |
DC_EXEC | DC_NO_TOWN_RATING |
|
2784 |
); |
|
7442
a4f7989f27d4
(svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents:
7439
diff
changeset
|
2785 |
assert(CmdSucceeded(r)); |
0 | 2786 |
} |
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
2787 |
} while (++aib, --j); |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2788 |
} |
0 | 2789 |
|
2790 |
// check if we're done with all of them |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2791 |
aib = &_players_ai[p->index].src; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2792 |
j = _players_ai[p->index].num_build_rec; |
0 | 2793 |
do { |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2794 |
if (aib->cur_building_rule == 255) return; |
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
2795 |
} while (++aib, --j); |
0 | 2796 |
|
2797 |
// yep, all are done. switch state to the rail building state. |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2798 |
_players_ai[p->index].state = AIS_BUILD_ROAD; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2799 |
_players_ai[p->index].state_mode = 255; |
0 | 2800 |
} |
2801 |
||
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2802 |
struct AiRoadFinder { |
0 | 2803 |
TileIndex final_tile; |
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
2804 |
DiagDirection final_dir; |
0 | 2805 |
byte depth; |
2806 |
byte recursive_mode; |
|
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
2807 |
DiagDirection cur_best_dir; |
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
2808 |
DiagDirection best_dir; |
0 | 2809 |
byte cur_best_depth; |
2810 |
byte best_depth; |
|
2811 |
uint cur_best_dist; |
|
2812 |
const byte *best_ptr; |
|
2813 |
uint best_dist; |
|
2814 |
TileIndex cur_best_tile, best_tile; |
|
2815 |
TileIndex bridge_end_tile; |
|
2816 |
Player *player; |
|
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2817 |
}; |
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2818 |
|
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2819 |
struct AiRoadEnum { |
0 | 2820 |
TileIndex dest; |
2821 |
TileIndex best_tile; |
|
9291
1fb2b3c2099f
(svn r12533) -Cleanup: Remove some wizardry from default ai by using existings enums and helper functions.
frosch
parents:
9282
diff
changeset
|
2822 |
Trackdir best_track; |
0 | 2823 |
uint best_dist; |
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2824 |
}; |
0 | 2825 |
|
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
2826 |
static void AiBuildRoadRecursive(AiRoadFinder *arf, TileIndex tile, DiagDirection dir); |
0 | 2827 |
|
2828 |
static bool AiCheckRoadPathBetter(AiRoadFinder *arf, const byte *p) |
|
2829 |
{ |
|
2830 |
bool better = false; |
|
2831 |
||
2832 |
if (arf->recursive_mode < 1) { |
|
2833 |
// Mode is 0. This means destination has not been found yet. |
|
2834 |
// 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
|
2835 |
if (arf->cur_best_dist < arf->best_dist || |
0 | 2836 |
(arf->cur_best_dist == arf->best_dist && arf->cur_best_depth < arf->best_depth)) { |
2837 |
arf->best_depth = arf->cur_best_depth; |
|
2838 |
arf->best_dist = arf->cur_best_dist; |
|
2839 |
arf->best_dir = arf->cur_best_dir; |
|
2840 |
arf->best_ptr = p; |
|
2841 |
arf->best_tile = arf->cur_best_tile; |
|
2842 |
better = true; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2843 |
} |
0 | 2844 |
} else if (arf->recursive_mode > 1) { |
2845 |
// Mode is 2. |
|
2846 |
if (arf->best_dist != 0 || arf->cur_best_depth < arf->best_depth) { |
|
2847 |
arf->best_depth = arf->cur_best_depth; |
|
2848 |
arf->best_dist = 0; |
|
2849 |
arf->best_ptr = p; |
|
2850 |
arf->best_tile = 0; |
|
2851 |
better = true; |
|
2852 |
} |
|
2853 |
} |
|
2854 |
arf->recursive_mode = 0; |
|
2855 |
arf->cur_best_dist = (uint)-1; |
|
2856 |
arf->cur_best_depth = 0xff; |
|
2857 |
||
2858 |
return better; |
|
2859 |
} |
|
2860 |
||
2861 |
||
9291
1fb2b3c2099f
(svn r12533) -Cleanup: Remove some wizardry from default ai by using existings enums and helper functions.
frosch
parents:
9282
diff
changeset
|
2862 |
static bool AiEnumFollowRoad(TileIndex tile, AiRoadEnum *a, Trackdir track, uint length) |
0 | 2863 |
{ |
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1224
diff
changeset
|
2864 |
uint dist = DistanceManhattan(tile, a->dest); |
0 | 2865 |
|
2866 |
if (dist <= a->best_dist) { |
|
9291
1fb2b3c2099f
(svn r12533) -Cleanup: Remove some wizardry from default ai by using existings enums and helper functions.
frosch
parents:
9282
diff
changeset
|
2867 |
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
|
2868 |
|
9059
04edde3eb0c6
(svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents:
9052
diff
changeset
|
2869 |
if (IsNormalRoadTile(tile2)) { |
0 | 2870 |
a->best_dist = dist; |
2871 |
a->best_tile = tile; |
|
2872 |
a->best_track = track; |
|
2873 |
} |
|
2874 |
} |
|
2875 |
||
2876 |
return false; |
|
2877 |
} |
|
2878 |
||
410 | 2879 |
static bool AiCheckRoadFinished(Player *p) |
0 | 2880 |
{ |
2881 |
AiRoadEnum are; |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1927
diff
changeset
|
2882 |
TileIndex tile; |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2883 |
DiagDirection dir = _players_ai[p->index].cur_dir_a; |
0 | 2884 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2885 |
are.dest = _players_ai[p->index].cur_tile_b; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2886 |
tile = TILE_MASK(_players_ai[p->index].cur_tile_a + TileOffsByDiagDir(dir)); |
0 | 2887 |
|
10221
faef62283f5f
(svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents:
10213
diff
changeset
|
2888 |
if (IsRoadStopTile(tile) || IsRoadDepotTile(tile)) return false; |
9291
1fb2b3c2099f
(svn r12533) -Cleanup: Remove some wizardry from default ai by using existings enums and helper functions.
frosch
parents:
9282
diff
changeset
|
2889 |
TrackdirBits bits = TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_ROAD, ROADTYPES_ROAD)) & DiagdirReachesTrackdirs(dir); |
1fb2b3c2099f
(svn r12533) -Cleanup: Remove some wizardry from default ai by using existings enums and helper functions.
frosch
parents:
9282
diff
changeset
|
2890 |
if (bits == TRACKDIR_BIT_NONE) return false; |
0 | 2891 |
|
2892 |
are.best_dist = (uint)-1; |
|
2893 |
||
9291
1fb2b3c2099f
(svn r12533) -Cleanup: Remove some wizardry from default ai by using existings enums and helper functions.
frosch
parents:
9282
diff
changeset
|
2894 |
while (bits != TRACKDIR_BIT_NONE) { |
1fb2b3c2099f
(svn r12533) -Cleanup: Remove some wizardry from default ai by using existings enums and helper functions.
frosch
parents:
9282
diff
changeset
|
2895 |
Trackdir trackdir = RemoveFirstTrackdir(&bits); |
9296
5878a50c67b2
(svn r12540) -Codechange: Enumify some values in original pathfinder and remove an unused variable.
frosch
parents:
9291
diff
changeset
|
2896 |
FollowTrack(tile, PATHFIND_FLAGS_DISABLE_TILE_HASH, TRANSPORT_ROAD, ROADTYPES_ROAD, TrackdirToExitdir(trackdir), (TPFEnumProc*)AiEnumFollowRoad, NULL, &are); |
0 | 2897 |
} |
2898 |
||
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2899 |
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
|
2900 |
|
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2901 |
if (are.best_dist == 0) return true; |
0 | 2902 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
2903 |
_players_ai[p->index].cur_tile_a = are.best_tile; |
9291
1fb2b3c2099f
(svn r12533) -Cleanup: Remove some wizardry from default ai by using existings enums and helper functions.
frosch
parents:
9282
diff
changeset
|
2904 |
_players_ai[p->index].cur_dir_a = TrackdirToExitdir(are.best_track); |
0 | 2905 |
return false; |
2906 |
} |
|
2907 |
||
2908 |
||
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1927
diff
changeset
|
2909 |
static bool AiBuildRoadHelper(TileIndex tile, int flags, int type) |
0 | 2910 |
{ |
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
2911 |
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
|
2912 |
ROAD_X, |
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
2913 |
ROAD_Y, |
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
2914 |
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
|
2915 |
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
|
2916 |
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
|
2917 |
ROAD_SE | ROAD_NE |
0 | 2918 |
}; |
7442
a4f7989f27d4
(svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents:
7439
diff
changeset
|
2919 |
return CmdSucceeded(DoCommand(tile, _road_bits[type], 0, flags, CMD_BUILD_ROAD)); |
0 | 2920 |
} |
2921 |
||
536 | 2922 |
static inline void AiCheckBuildRoadBridgeHere(AiRoadFinder *arf, TileIndex tile, const byte *p) |
0 | 2923 |
{ |
3644
45a307767dc1
(svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents:
3609
diff
changeset
|
2924 |
Slope tileh; |
3088
4570504ac734
(svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents:
3071
diff
changeset
|
2925 |
uint z; |
0 | 2926 |
bool flag; |
2927 |
||
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
2928 |
DiagDirection dir2 = (DiagDirection)(p[0] & 3); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2929 |
|
3088
4570504ac734
(svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents:
3071
diff
changeset
|
2930 |
tileh = GetTileSlope(tile, &z); |
9291
1fb2b3c2099f
(svn r12533) -Cleanup: Remove some wizardry from default ai by using existings enums and helper functions.
frosch
parents:
9282
diff
changeset
|
2931 |
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
|
2932 |
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
|
2933 |
|
0 | 2934 |
// Allow bridges directly over bottom tiles |
3088
4570504ac734
(svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents:
3071
diff
changeset
|
2935 |
flag = z == 0; |
2952 | 2936 |
for (;;) { |
3088
4570504ac734
(svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents:
3071
diff
changeset
|
2937 |
TileType type; |
4570504ac734
(svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents:
3071
diff
changeset
|
2938 |
|
4559
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4527
diff
changeset
|
2939 |
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
|
2940 |
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
|
2941 |
type = GetTileType(tile_new); |
4570504ac734
(svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents:
3071
diff
changeset
|
2942 |
|
8847
98ccd8072daf
(svn r11917) -Fix (r3677): AI was reading wrong tile slope while building road bridge
smatz
parents:
8818
diff
changeset
|
2943 |
if (type == MP_CLEAR || type == MP_TREES || GetTileSlope(tile_new, NULL) != SLOPE_FLAT) { |
0 | 2944 |
// Allow a bridge if either we have a tile that's water, rail or street, |
2945 |
// or if we found an up tile. |
|
2946 |
if (!flag) return; |
|
2947 |
break; |
|
2948 |
} |
|
7866
e19fda04e8d3
(svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents:
7813
diff
changeset
|
2949 |
if (type != MP_WATER && type != MP_RAILWAY && type != MP_ROAD) return; |
0 | 2950 |
flag = true; |
2951 |
} |
|
2952 |
||
2953 |
// Is building a (rail)bridge possible at this place (type doesn't matter)? |
|
7338
5bb335d2eb54
(svn r10081) -Fix [FS851]: towns/AIs didn't build bridges anymore (boekabart).
rubidium
parents:
7315
diff
changeset
|
2954 |
if (CmdFailed(DoCommand(tile_new, tile, ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO, CMD_BUILD_BRIDGE))) |
0 | 2955 |
return; |
2956 |
AiBuildRoadRecursive(arf, tile_new, dir2); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2957 |
|
0 | 2958 |
// At the bottom depth, check if the new path is better than the old one. |
2959 |
if (arf->depth == 1) { |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
2960 |
if (AiCheckRoadPathBetter(arf, p)) arf->bridge_end_tile = tile_new; |
0 | 2961 |
} |
2962 |
} |
|
2963 |
} |
|
2964 |
||
536 | 2965 |
static inline void AiCheckBuildRoadTunnelHere(AiRoadFinder *arf, TileIndex tile, const byte *p) |
0 | 2966 |
{ |
3088
4570504ac734
(svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents:
3071
diff
changeset
|
2967 |
uint z; |
4570504ac734
(svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents:
3071
diff
changeset
|
2968 |
|
9291
1fb2b3c2099f
(svn r12533) -Cleanup: Remove some wizardry from default ai by using existings enums and helper functions.
frosch
parents:
9282
diff
changeset
|
2969 |
if (GetTileSlope(tile, &z) == InclinedSlope((DiagDirection)(p[0] & 3)) && z != 0) { |
7439
0c0e2945c890
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents:
7362
diff
changeset
|
2970 |
CommandCost cost = DoCommand(tile, 0x200, 0, DC_AUTO, CMD_BUILD_TUNNEL); |
0 | 2971 |
|
7446
1c4d469f986e
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents:
7442
diff
changeset
|
2972 |
if (CmdSucceeded(cost) && cost.GetCost() <= (arf->player->player_money >> 4)) { |
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
2973 |
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
|
2974 |
if (arf->depth == 1) AiCheckRoadPathBetter(arf, p); |
0 | 2975 |
} |
2976 |
} |
|
2977 |
} |
|
2978 |
||
2979 |
||
2980 |
||
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
2981 |
static void AiBuildRoadRecursive(AiRoadFinder *arf, TileIndex tile, DiagDirection dir) |
0 | 2982 |
{ |
2983 |
const byte *p; |
|
2984 |
||
4559
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4527
diff
changeset
|
2985 |
tile = TILE_MASK(tile + TileOffsByDiagDir(dir)); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2986 |
|
0 | 2987 |
// Reached destination? |
2988 |
if (tile == arf->final_tile) { |
|
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
2989 |
if (ReverseDiagDir(arf->final_dir) == dir) { |
0 | 2990 |
arf->recursive_mode = 2; |
2991 |
arf->cur_best_depth = arf->depth; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
2992 |
} |
0 | 2993 |
return; |
2994 |
} |
|
2995 |
||
2996 |
// Depth too deep? |
|
2997 |
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
|
2998 |
uint dist = DistanceMaxPlusManhattan(tile, arf->final_tile); |
0 | 2999 |
if (dist < arf->cur_best_dist) { |
3000 |
// Store the tile that is closest to the final position. |
|
3001 |
arf->cur_best_dist = dist; |
|
3002 |
arf->cur_best_tile = tile; |
|
3003 |
arf->cur_best_dir = dir; |
|
3004 |
arf->cur_best_depth = arf->depth; |
|
3005 |
} |
|
3006 |
return; |
|
3007 |
} |
|
3008 |
||
3009 |
// Increase recursion depth |
|
3010 |
arf->depth++; |
|
3011 |
||
3012 |
// Grab pointer to list of stuff that is possible to build |
|
3013 |
p = _ai_table_15[dir]; |
|
3014 |
||
3015 |
// 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
|
3016 |
if (GetTileZ(tile) == 0) { |
0 | 3017 |
p += 6; |
3018 |
} else { |
|
3019 |
do { |
|
3020 |
// Make sure that a road can be built here. |
|
3021 |
if (AiBuildRoadHelper(tile, DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, p[0])) { |
|
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
3022 |
AiBuildRoadRecursive(arf, tile, (DiagDirection)p[1]); |
0 | 3023 |
} |
3024 |
||
3025 |
// At the bottom depth? |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3026 |
if (arf->depth == 1) AiCheckRoadPathBetter(arf, p); |
0 | 3027 |
|
3028 |
p += 2; |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3029 |
} while (!(p[0] & 0x80)); |
0 | 3030 |
} |
3031 |
||
3032 |
AiCheckBuildRoadBridgeHere(arf, tile, p); |
|
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
3033 |
AiCheckBuildRoadTunnelHere(arf, tile, p + 1); |
0 | 3034 |
|
3035 |
arf->depth--; |
|
3036 |
} |
|
3037 |
||
3038 |
||
3039 |
static void AiBuildRoadConstruct(Player *p) |
|
3040 |
{ |
|
3041 |
AiRoadFinder arf; |
|
3042 |
int i; |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1927
diff
changeset
|
3043 |
TileIndex tile; |
0 | 3044 |
|
3045 |
// Reached destination? |
|
3046 |
if (AiCheckRoadFinished(p)) { |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3047 |
_players_ai[p->index].state_mode = 255; |
0 | 3048 |
return; |
3049 |
} |
|
3050 |
||
3051 |
// Setup recursive finder and call it. |
|
3052 |
arf.player = p; |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3053 |
arf.final_tile = _players_ai[p->index].cur_tile_b; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3054 |
arf.final_dir = _players_ai[p->index].cur_dir_b; |
0 | 3055 |
arf.depth = 0; |
3056 |
arf.recursive_mode = 0; |
|
3057 |
arf.best_ptr = NULL; |
|
3058 |
arf.cur_best_dist = (uint)-1; |
|
3059 |
arf.cur_best_depth = 0xff; |
|
3060 |
arf.best_dist = (uint)-1; |
|
3061 |
arf.best_depth = 0xff; |
|
3062 |
arf.cur_best_tile = 0; |
|
3063 |
arf.best_tile = 0; |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3064 |
AiBuildRoadRecursive(&arf, _players_ai[p->index].cur_tile_a, _players_ai[p->index].cur_dir_a); |
0 | 3065 |
|
3066 |
// Reached destination? |
|
3067 |
if (arf.recursive_mode == 2 && arf.cur_best_depth == 0) { |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3068 |
_players_ai[p->index].state_mode = 255; |
0 | 3069 |
return; |
3070 |
} |
|
3071 |
||
3072 |
// Didn't find anything to build? |
|
3073 |
if (arf.best_ptr == NULL) { |
|
3074 |
// Terraform some |
|
3075 |
do_some_terraform: |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3076 |
for (i = 0; i != 5; i++) |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3077 |
AiDoTerraformLand(_players_ai[p->index].cur_tile_a, _players_ai[p->index].cur_dir_a, 3, 0); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3078 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3079 |
if (++_players_ai[p->index].state_counter == 21) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3080 |
_players_ai[p->index].state_mode = 1; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3081 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3082 |
_players_ai[p->index].cur_tile_a = TILE_MASK(_players_ai[p->index].cur_tile_a + TileOffsByDiagDir(_players_ai[p->index].cur_dir_a)); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3083 |
_players_ai[p->index].cur_dir_a = ReverseDiagDir(_players_ai[p->index].cur_dir_a); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3084 |
_players_ai[p->index].state_counter = 0; |
0 | 3085 |
} |
3086 |
return; |
|
3087 |
} |
|
3088 |
||
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3089 |
tile = TILE_MASK(_players_ai[p->index].cur_tile_a + TileOffsByDiagDir(_players_ai[p->index].cur_dir_a)); |
0 | 3090 |
|
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
3091 |
if (arf.best_ptr[0] & 0x80) { |
9113
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3092 |
TileIndex t1 = tile; |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3093 |
TileIndex t2 = arf.bridge_end_tile; |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3094 |
|
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3095 |
int32 bridge_len = GetTunnelBridgeLength(t1, t2); |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3096 |
|
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3097 |
Axis axis = (TileX(t1) == TileX(t2) ? AXIS_Y : AXIS_X); |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3098 |
|
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3099 |
/* 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! */ |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3100 |
CommandCost cost = DoCommand(t2, t1, (t2 < t1 ? 1 : 2) | (axis << 2) | (ROADTYPE_ROAD << 3), DC_AUTO | DC_NO_WATER, CMD_BUILD_LONG_ROAD); |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3101 |
|
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3102 |
if (CmdSucceeded(cost) && cost.GetCost() <= p->player_money) { |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3103 |
DoCommand(t2, t1, (t2 < t1 ? 1 : 2) | (axis << 2) | (ROADTYPE_ROAD << 3), DC_AUTO | DC_EXEC | DC_NO_WATER, CMD_BUILD_LONG_ROAD); |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3104 |
} else { |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3105 |
int i; |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3106 |
|
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3107 |
/* Figure out what (road)bridge type to build |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3108 |
* start with best bridge, then go down to worse and worse bridges |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3109 |
* unnecessary to check for worse bridge (i=0), since AI will always build that */ |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3110 |
for (i = MAX_BRIDGES - 1; i != 0; i--) { |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3111 |
if (CheckBridge_Stuff(i, bridge_len)) { |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3112 |
CommandCost cost = DoCommand(t1, t2, i + ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO, CMD_BUILD_BRIDGE); |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3113 |
if (CmdSucceeded(cost) && cost.GetCost() < (p->player_money >> 1) && cost.GetCost() < ((p->player_money + _economy.max_loan - p->current_loan) >> 5)) break; |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3114 |
} |
0 | 3115 |
} |
9113
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3116 |
|
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3117 |
/* Build it */ |
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3118 |
DoCommand(t1, t2, i + ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE); |
0 | 3119 |
} |
3120 |
||
9113
4871417ad10c
(svn r12200) -Fix: force AI to build rail or road instead of bridges if possible, so it doesn't build bridges everywhere
smatz
parents:
9112
diff
changeset
|
3121 |
_players_ai[p->index].cur_tile_a = t2; |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3122 |
_players_ai[p->index].state_counter = 0; |
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
3123 |
} else if (arf.best_ptr[0] & 0x40) { |
0 | 3124 |
// 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
|
3125 |
DoCommand(tile, 0x200, 0, DC_AUTO | DC_EXEC, CMD_BUILD_TUNNEL); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3126 |
_players_ai[p->index].cur_tile_a = _build_tunnel_endtile; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3127 |
_players_ai[p->index].state_counter = 0; |
0 | 3128 |
} else { |
3129 |
// road |
|
3130 |
if (!AiBuildRoadHelper(tile, DC_EXEC | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, arf.best_ptr[0])) |
|
3131 |
goto do_some_terraform; |
|
3132 |
||
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3133 |
_players_ai[p->index].cur_dir_a = (DiagDirection)(arf.best_ptr[1] & 3); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3134 |
_players_ai[p->index].cur_tile_a = tile; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3135 |
_players_ai[p->index].state_counter = 0; |
0 | 3136 |
} |
3137 |
||
3138 |
if (arf.best_tile != 0) { |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3139 |
for (i = 0; i != 2; i++) |
0 | 3140 |
AiDoTerraformLand(arf.best_tile, arf.best_dir, 3, 0); |
3141 |
} |
|
3142 |
} |
|
3143 |
||
3144 |
||
3145 |
static void AiBuildRoad(Player *p) |
|
3146 |
{ |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3147 |
if (_players_ai[p->index].state_mode < 1) { |
0 | 3148 |
// Construct mode, build new road. |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3149 |
AiBuildRoadConstruct(p); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3150 |
} else if (_players_ai[p->index].state_mode == 1) { |
0 | 3151 |
// Destruct mode, not implemented for roads. |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3152 |
_players_ai[p->index].state_mode = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3153 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3154 |
} 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
|
3155 |
uint i; |
0 | 3156 |
|
3157 |
// 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
|
3158 |
for (i = 0; i != 4; i++) { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3159 |
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
|
3160 |
} |
0 | 3161 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3162 |
if (++_players_ai[p->index].state_counter == 4) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3163 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3164 |
_players_ai[p->index].state_mode = 0; |
0 | 3165 |
} |
3166 |
} |
|
3167 |
} |
|
3168 |
||
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
3169 |
static TileIndex AiGetRoadBlockEdge(byte rule, TileIndex tile, DiagDirection *dir) |
0 | 3170 |
{ |
3171 |
const AiDefaultBlockData *p = _road_default_block_data[rule]->data; |
|
3172 |
while (p->mode != 1) p++; |
|
3173 |
*dir = p->attr; |
|
909
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
3174 |
return TILE_ADD(tile, ToTileIndexDiff(p->tileoffs)); |
0 | 3175 |
} |
3176 |
||
3177 |
||
3178 |
static void AiStateBuildRoad(Player *p) |
|
3179 |
{ |
|
3180 |
int num; |
|
3181 |
AiBuildRec *aib; |
|
3182 |
byte cmd; |
|
3183 |
TileIndex tile; |
|
7813
49fbe41f8a04
(svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents:
7486
diff
changeset
|
3184 |
DiagDirection dir; |
0 | 3185 |
|
3186 |
// time out? |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3187 |
if (++_players_ai[p->index].timeout_counter == 1388) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3188 |
_players_ai[p->index].state = AIS_DELETE_ROAD_BLOCKS; |
0 | 3189 |
return; |
3190 |
} |
|
3191 |
||
3192 |
// Currently building a road between two points? |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3193 |
if (_players_ai[p->index].state_mode != 255) { |
0 | 3194 |
AiBuildRoad(p); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3195 |
|
0 | 3196 |
// Alternate between edges |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3197 |
Swap(_players_ai[p->index].start_tile_a, _players_ai[p->index].start_tile_b); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3198 |
Swap(_players_ai[p->index].cur_tile_a, _players_ai[p->index].cur_tile_b); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3199 |
Swap(_players_ai[p->index].start_dir_a, _players_ai[p->index].start_dir_b); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3200 |
Swap(_players_ai[p->index].cur_dir_a, _players_ai[p->index].cur_dir_b); |
0 | 3201 |
|
3202 |
return; |
|
3203 |
} |
|
3204 |
||
3205 |
// Now, find two new points to build between |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3206 |
num = _players_ai[p->index].num_build_rec; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3207 |
aib = &_players_ai[p->index].src; |
0 | 3208 |
|
2952 | 3209 |
for (;;) { |
0 | 3210 |
cmd = aib->buildcmd_a; |
3211 |
aib->buildcmd_a = 255; |
|
3212 |
if (cmd != 255) break; |
|
3213 |
||
3214 |
aib++; |
|
3215 |
if (--num == 0) { |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3216 |
_players_ai[p->index].state = AIS_BUILD_ROAD_VEHICLES; |
0 | 3217 |
return; |
3218 |
} |
|
3219 |
} |
|
3220 |
||
3221 |
// Find first edge to build from. |
|
3222 |
tile = AiGetRoadBlockEdge(aib->cur_building_rule, aib->use_tile, &dir); |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3223 |
_players_ai[p->index].start_tile_a = tile; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3224 |
_players_ai[p->index].cur_tile_a = tile; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3225 |
_players_ai[p->index].start_dir_a = dir; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3226 |
_players_ai[p->index].cur_dir_a = dir; |
0 | 3227 |
|
3228 |
// Find second edge to build to |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3229 |
aib = (&_players_ai[p->index].src) + (cmd & 0xF); |
0 | 3230 |
tile = AiGetRoadBlockEdge(aib->cur_building_rule, aib->use_tile, &dir); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3231 |
_players_ai[p->index].start_tile_b = tile; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3232 |
_players_ai[p->index].cur_tile_b = tile; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3233 |
_players_ai[p->index].start_dir_b = dir; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3234 |
_players_ai[p->index].cur_dir_b = dir; |
0 | 3235 |
|
3236 |
// And setup state. |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3237 |
_players_ai[p->index].state_mode = 2; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3238 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3239 |
_players_ai[p->index].banned_tile_count = 0; |
0 | 3240 |
} |
3241 |
||
3315
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3269
diff
changeset
|
3242 |
static StationID AiGetStationIdFromRoadBlock(TileIndex tile, int id) |
0 | 3243 |
{ |
3244 |
const AiDefaultBlockData *p = _road_default_block_data[id]->data; |
|
3245 |
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
|
3246 |
return GetStationIndex(TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))); |
0 | 3247 |
} |
3248 |
||
3249 |
static void AiStateBuildRoadVehicles(Player *p) |
|
3250 |
{ |
|
3251 |
const AiDefaultBlockData *ptr; |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1927
diff
changeset
|
3252 |
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
|
3253 |
VehicleID loco_id; |
2551
436aaaa22ba5
(svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents:
2544
diff
changeset
|
3254 |
EngineID veh; |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3255 |
uint i; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3256 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3257 |
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
|
3258 |
for (; ptr->mode != 0; ptr++) {} |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3259 |
tile = TILE_ADD(_players_ai[p->index].src.use_tile, ToTileIndexDiff(ptr->tileoffs)); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3260 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3261 |
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
|
3262 |
if (veh == INVALID_ENGINE) { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3263 |
_players_ai[p->index].state = AIS_0; |
0 | 3264 |
return; |
3265 |
} |
|
3266 |
||
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3473
diff
changeset
|
3267 |
if (CmdFailed(DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH))) return; |
0 | 3268 |
|
3948
95f9fa0ac551
(svn r5094) Remove _new_{aircraft,roadveh,ship,train,wagon}_id. _new_vehicle_id is enough.
tron
parents:
3933
diff
changeset
|
3269 |
loco_id = _new_vehicle_id; |
0 | 3270 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3271 |
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
|
3272 |
/* Cargo type doesn't match, so refit it */ |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3273 |
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
|
3274 |
/* Refit failed... sell the vehicle */ |
940b1fec4220
(svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents:
5029
diff
changeset
|
3275 |
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
|
3276 |
return; |
940b1fec4220
(svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents:
5029
diff
changeset
|
3277 |
} |
940b1fec4220
(svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents:
5029
diff
changeset
|
3278 |
} |
940b1fec4220
(svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents:
5029
diff
changeset
|
3279 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3280 |
for (i = 0; _players_ai[p->index].order_list_blocks[i] != 0xFF; i++) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3281 |
const AiBuildRec* aib = &_players_ai[p->index].src + _players_ai[p->index].order_list_blocks[i]; |
2639 | 3282 |
bool is_pass = ( |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3283 |
_players_ai[p->index].cargo_type == CT_PASSENGERS || |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3284 |
_players_ai[p->index].cargo_type == CT_MAIL || |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3285 |
(_opt.landscape == LT_TEMPERATE && _players_ai[p->index].cargo_type == CT_VALUABLES) |
2639 | 3286 |
); |
558
9b115b39c515
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3287 |
Order order; |
9b115b39c515
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3288 |
|
9332
2e120d0bd632
(svn r12584) -Codechange: do not access the order type directly.
rubidium
parents:
9296
diff
changeset
|
3289 |
order.MakeGoToStation(AiGetStationIdFromRoadBlock(aib->use_tile, aib->cur_building_rule)); |
558
9b115b39c515
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3290 |
|
10081
e46b9eb2f9c1
(svn r12617) -Codechange: add type safety to the Order's load and unload types.
rubidium
parents:
9339
diff
changeset
|
3291 |
if (!is_pass && i == 1) order.SetUnloadType(OUFB_UNLOAD); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3292 |
if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0)) |
10081
e46b9eb2f9c1
(svn r12617) -Codechange: add type safety to the Order's load and unload types.
rubidium
parents:
9339
diff
changeset
|
3293 |
order.SetLoadType(OLFB_FULL_LOAD); |
558
9b115b39c515
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3294 |
|
9335
18d3658adc60
(svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents:
9332
diff
changeset
|
3295 |
DoCommand(0, loco_id + (i << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER); |
0 | 3296 |
} |
3297 |
||
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3473
diff
changeset
|
3298 |
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
|
3299 |
DoCommand(0, loco_id, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT); |
0 | 3300 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3301 |
if (_players_ai[p->index].num_want_fullload != 0) _players_ai[p->index].num_want_fullload--; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3302 |
if (--_players_ai[p->index].num_loco_to_build == 0) _players_ai[p->index].state = AIS_0; |
0 | 3303 |
} |
3304 |
||
3305 |
static void AiStateDeleteRoadBlocks(Player *p) |
|
3306 |
{ |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3307 |
const AiBuildRec* aib = &_players_ai[p->index].src; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3308 |
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
|
3309 |
|
0 | 3310 |
do { |
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
3311 |
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
|
3312 |
|
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
3313 |
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
|
3314 |
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
|
3315 |
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
|
3316 |
DoCommand(TILE_ADD(aib->use_tile, ToTileIndexDiff(b->tileoffs)), 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); |
0 | 3317 |
} |
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
3318 |
} while (++aib, --num); |
0 | 3319 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3320 |
_players_ai[p->index].state = AIS_0; |
0 | 3321 |
} |
3322 |
||
3323 |
||
3324 |
static void AiStateAirportStuff(Player *p) |
|
3325 |
{ |
|
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
|
3326 |
const Station* st; |
0 | 3327 |
int i; |
3328 |
AiBuildRec *aib; |
|
3329 |
byte rule; |
|
3330 |
||
3331 |
// Here we look for an airport we could use instead of building a new |
|
3332 |
// one. If we find such an aiport for any waypoint, |
|
3333 |
// AiStateBuildDefaultAirportBlocks() will kindly skip that one when |
|
3334 |
// building the waypoints. |
|
3335 |
||
3336 |
i = 0; |
|
3337 |
do { |
|
3338 |
// We do this all twice - once for the source (town in the case |
|
3339 |
// of oilrig route) and then for the destination (oilrig in the |
|
3340 |
// case of oilrig route). |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3341 |
aib = &_players_ai[p->index].src + i; |
0 | 3342 |
|
3343 |
FOR_ALL_STATIONS(st) { |
|
3344 |
// Is this an airport? |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3345 |
if (!(st->facilities & FACIL_AIRPORT)) continue; |
0 | 3346 |
|
3347 |
// 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
|
3348 |
if (st->owner != OWNER_NONE && st->owner != _current_player) continue; |
0 | 3349 |
|
6358 | 3350 |
AirportFTAClass::Flags flags = st->Airport()->flags; |
6350 | 3351 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3352 |
if (!(flags & (_players_ai[p->index].build_kind == 1 && i == 0 ? AirportFTAClass::HELICOPTERS : AirportFTAClass::AIRPLANES))) { |
0 | 3353 |
continue; |
6350 | 3354 |
} |
0 | 3355 |
|
3356 |
// 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
|
3357 |
if (DistanceMax(st->airport_tile, aib->spec_tile) > aib->rand_rng) |
0 | 3358 |
continue; |
3359 |
||
3360 |
// It's ideal airport, let's take it! |
|
3361 |
||
3362 |
/* XXX: This part is utterly broken - rule should |
|
3363 |
* contain number of the rule appropriate for the |
|
3364 |
* airport type (country, town, ...), see |
|
3365 |
* _airport_default_block_data (rule is just an index |
|
3366 |
* in this array). But the only difference between the |
|
3367 |
* currently existing two rules (rule 0 - town and rule |
|
3368 |
* 1 - country) is the attr field which is used only |
|
3369 |
* when building new airports - and that's irrelevant |
|
3370 |
* for us. So using just about any rule will suffice |
|
3371 |
* here for now (some of the new airport types would be |
|
3372 |
* broken because they will probably need different |
|
3373 |
* tileoff values etc), no matter that |
|
3338
2c60ed0dcce3
(svn r4120) Use the new station functions where appropriate
tron
parents:
3336
diff
changeset
|
3374 |
* 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
|
3375 |
if (!(flags & AirportFTAClass::AIRPLANES)) { |
0 | 3376 |
/* Heliports should have maybe own rulesets but |
3377 |
* OTOH we don't want AI to pick them up when |
|
3378 |
* looking for a suitable airport type to build. |
|
3379 |
* So any of rules 0 or 1 would do for now. The |
|
3380 |
* original rule number was 2 but that's a bug |
|
3381 |
* because we have no such rule. */ |
|
3382 |
rule = 1; |
|
3383 |
} else { |
|
3338
2c60ed0dcce3
(svn r4120) Use the new station functions where appropriate
tron
parents:
3336
diff
changeset
|
3384 |
rule = IsHangarTile(st->airport_tile); |
0 | 3385 |
} |
3386 |
||
3387 |
aib->cur_building_rule = rule; |
|
3388 |
aib->use_tile = st->airport_tile; |
|
3389 |
break; |
|
3390 |
} |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3391 |
} while (++i != _players_ai[p->index].num_build_rec); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3392 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3393 |
_players_ai[p->index].state = AIS_BUILD_DEFAULT_AIRPORT_BLOCKS; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3394 |
_players_ai[p->index].state_mode = 255; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3395 |
_players_ai[p->index].state_counter = 0; |
0 | 3396 |
} |
3397 |
||
7439
0c0e2945c890
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents:
7362
diff
changeset
|
3398 |
static CommandCost AiDoBuildDefaultAirportBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag) |
0 | 3399 |
{ |
6222 | 3400 |
uint32 avail_airports = GetValidAirports(); |
8726
5ae45b46506b
(svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents:
8725
diff
changeset
|
3401 |
CommandCost ret,total_cost(EXPENSES_CONSTRUCTION); |
0 | 3402 |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3403 |
for (; p->mode == 0; p++) { |
8424
4a488a90ccab
(svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents:
8149
diff
changeset
|
3404 |
if (!HasBit(avail_airports, p->attr)) return CMD_ERROR; |
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
3405 |
ret = DoCommand(TILE_MASK(tile + ToTileIndexDiff(p->tileoffs)), p->attr, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_AIRPORT); |
1713
d970350410b2
(svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents:
1640
diff
changeset
|
3406 |
if (CmdFailed(ret)) return CMD_ERROR; |
7446
1c4d469f986e
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents:
7442
diff
changeset
|
3407 |
total_cost.AddCost(ret); |
0 | 3408 |
} |
3409 |
||
3410 |
return total_cost; |
|
3411 |
} |
|
3412 |
||
3413 |
static bool AiCheckAirportResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo) |
|
3414 |
{ |
|
3415 |
uint values[NUM_CARGO]; |
|
3416 |
||
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3417 |
for (; p->mode == 0; p++) { |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1927
diff
changeset
|
3418 |
TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs)); |
3876
c0d426e78b56
(svn r4918) Move the information about the size of airports from an global array into the struct which describes an airport
tron
parents:
3871
diff
changeset
|
3419 |
const AirportFTAClass* airport = GetAirport(p->attr); |
c0d426e78b56
(svn r4918) Move the information about the size of airports from an global array into the struct which describes an airport
tron
parents:
3871
diff
changeset
|
3420 |
uint w = airport->size_x; |
c0d426e78b56
(svn r4918) Move the information about the size of airports from an global array into the struct which describes an airport
tron
parents:
3871
diff
changeset
|
3421 |
uint h = airport->size_y; |
9070
e83a2f017f3d
(svn r12155) -Fix (r12154): some warnings (always compile before commit)
glx
parents:
9069
diff
changeset
|
3422 |
uint rad = _patches.modified_catchment ? airport->catchment : (uint)CA_UNMODIFIED; |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3423 |
|
0 | 3424 |
if (cargo & 0x80) { |
568
6eb7d394fb35
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
3425 |
GetProductionAroundTiles(values, tile2, w, h, rad); |
0 | 3426 |
return values[cargo & 0x7F] != 0; |
3427 |
} else { |
|
568
6eb7d394fb35
(svn r979) Allow more realistically sized catchment areas
Celestar
parents:
558
diff
changeset
|
3428 |
GetAcceptanceAroundTiles(values, tile2, w, h, rad); |
0 | 3429 |
return values[cargo] >= 8; |
3430 |
} |
|
3431 |
} |
|
3432 |
return true; |
|
3433 |
} |
|
3434 |
||
7439
0c0e2945c890
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents:
7362
diff
changeset
|
3435 |
static int AiFindBestDefaultAirportBlock(TileIndex tile, byte cargo, byte heli, CommandCost *cost) |
0 | 3436 |
{ |
3437 |
const AiDefaultBlockData *p; |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3438 |
uint i; |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3439 |
|
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3440 |
for (i = 0; (p = _airport_default_block_data[i]) != NULL; i++) { |
0 | 3441 |
// If we are doing a helicopter service, avoid building |
3442 |
// airports where they can't land. |
|
6350 | 3443 |
if (heli && !(GetAirport(p->attr)->flags & AirportFTAClass::HELICOPTERS)) continue; |
0 | 3444 |
|
3445 |
*cost = AiDoBuildDefaultAirportBlock(tile, p, 0); |
|
7442
a4f7989f27d4
(svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents:
7439
diff
changeset
|
3446 |
if (CmdSucceeded(*cost) && AiCheckAirportResources(tile, p, cargo)) |
0 | 3447 |
return i; |
3448 |
} |
|
3449 |
return -1; |
|
3450 |
} |
|
3451 |
||
3452 |
static void AiStateBuildDefaultAirportBlocks(Player *p) |
|
3453 |
{ |
|
3454 |
int i, j; |
|
3455 |
AiBuildRec *aib; |
|
3456 |
int rule; |
|
7439
0c0e2945c890
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents:
7362
diff
changeset
|
3457 |
CommandCost cost; |
0 | 3458 |
|
3459 |
// time out? |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3460 |
if (++_players_ai[p->index].timeout_counter == 1388) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3461 |
_players_ai[p->index].state = AIS_0; |
0 | 3462 |
return; |
3463 |
} |
|
3464 |
||
3465 |
// do the following 8 times |
|
3466 |
i = 8; |
|
3467 |
do { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3468 |
// check if we can build the default |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3469 |
aib = &_players_ai[p->index].src; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3470 |
j = _players_ai[p->index].num_build_rec; |
0 | 3471 |
do { |
3472 |
// this item has already been built? |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3473 |
if (aib->cur_building_rule != 255) continue; |
0 | 3474 |
|
3475 |
// adjust the coordinate randomly, |
|
3476 |
// to make sure that we find a position. |
|
3477 |
aib->use_tile = AdjustTileCoordRandomly(aib->spec_tile, aib->rand_rng); |
|
3478 |
||
3479 |
// check if the aircraft stuff can be built there. |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3480 |
rule = AiFindBestDefaultAirportBlock(aib->use_tile, aib->cargo, _players_ai[p->index].build_kind, &cost); |
0 | 3481 |
|
1861
8b6f1d021317
(svn r2367) Remove some unused functions, probably was some debug stuff
tron
parents:
1767
diff
changeset
|
3482 |
// SetRedErrorSquare(aib->use_tile); |
0 | 3483 |
|
3484 |
if (rule == -1) { |
|
3485 |
// cannot build, terraform after a while |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3486 |
if (_players_ai[p->index].state_counter >= 600) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3487 |
AiDoTerraformLand(aib->use_tile, (DiagDirection)(Random() & 3), 3, (int8)_players_ai[p->index].state_mode); |
0 | 3488 |
} |
3489 |
// also try the other terraform direction |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3490 |
if (++_players_ai[p->index].state_counter >= 1000) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3491 |
_players_ai[p->index].state_counter = 0; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3492 |
_players_ai[p->index].state_mode = -_players_ai[p->index].state_mode; |
0 | 3493 |
} |
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
3494 |
} else if (CheckPlayerHasMoney(cost) && AiCheckBlockDistances(p, aib->use_tile)) { |
0 | 3495 |
// player has money, build it. |
7439
0c0e2945c890
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents:
7362
diff
changeset
|
3496 |
CommandCost r; |
0 | 3497 |
|
3498 |
aib->cur_building_rule = rule; |
|
3499 |
||
3500 |
r = AiDoBuildDefaultAirportBlock( |
|
3501 |
aib->use_tile, |
|
3502 |
_airport_default_block_data[rule], |
|
3503 |
DC_EXEC | DC_NO_TOWN_RATING |
|
3504 |
); |
|
7442
a4f7989f27d4
(svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents:
7439
diff
changeset
|
3505 |
assert(CmdSucceeded(r)); |
0 | 3506 |
} |
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
3507 |
} while (++aib, --j); |
0 | 3508 |
} while (--i); |
3509 |
||
3510 |
// check if we're done with all of them |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3511 |
aib = &_players_ai[p->index].src; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3512 |
j = _players_ai[p->index].num_build_rec; |
0 | 3513 |
do { |
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3514 |
if (aib->cur_building_rule == 255) return; |
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
3515 |
} while (++aib, --j); |
0 | 3516 |
|
3517 |
// yep, all are done. switch state. |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3518 |
_players_ai[p->index].state = AIS_BUILD_AIRCRAFT_VEHICLES; |
0 | 3519 |
} |
3520 |
||
3315
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3269
diff
changeset
|
3521 |
static StationID AiGetStationIdFromAircraftBlock(TileIndex tile, int id) |
0 | 3522 |
{ |
3523 |
const AiDefaultBlockData *p = _airport_default_block_data[id]; |
|
3524 |
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
|
3525 |
return GetStationIndex(TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))); |
0 | 3526 |
} |
3527 |
||
3528 |
static void AiStateBuildAircraftVehicles(Player *p) |
|
3529 |
{ |
|
3530 |
const AiDefaultBlockData *ptr; |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1927
diff
changeset
|
3531 |
TileIndex tile; |
2544 | 3532 |
EngineID veh; |
0 | 3533 |
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
|
3534 |
VehicleID loco_id; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3535 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3536 |
ptr = _airport_default_block_data[_players_ai[p->index].src.cur_building_rule]; |
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3977
diff
changeset
|
3537 |
for (; ptr->mode != 0; ptr++) {} |
0 | 3538 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3539 |
tile = TILE_ADD(_players_ai[p->index].src.use_tile, ToTileIndexDiff(ptr->tileoffs)); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3540 |
|
8898
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
3541 |
/* determine forbidden aircraft bits */ |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
3542 |
byte forbidden = 0; |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
3543 |
for (i = 0; _players_ai[p->index].order_list_blocks[i] != 0xFF; i++) { |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
3544 |
const AiBuildRec *aib = (&_players_ai[p->index].src) + _players_ai[p->index].order_list_blocks[i]; |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
3545 |
const Station *st = GetStationByTile(aib->use_tile); |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
3546 |
|
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
3547 |
if (st == NULL || !(st->facilities & FACIL_AIRPORT)) continue; |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
3548 |
|
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
3549 |
AirportFTAClass::Flags flags = st->Airport()->flags; |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
3550 |
if (!(flags & AirportFTAClass::AIRPLANES)) forbidden |= AIR_CTOL | AIR_FAST; // no planes for heliports / oil rigs |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
3551 |
if (flags & AirportFTAClass::SHORT_STRIP) forbidden |= AIR_FAST; // no fast planes for small airports |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
3552 |
} |
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
3553 |
|
a36f4df63438
(svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents:
8894
diff
changeset
|
3554 |
veh = AiChooseAircraftToBuild(p->player_money, forbidden); |
2544 | 3555 |
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
|
3556 |
|
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
|
3557 |
/* 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
|
3558 |
* and offset to the FIRST depot because the AI picks the st->xy tile */ |
6358 | 3559 |
tile += ToTileIndexDiff(GetStationByTile(tile)->Airport()->airport_depots[0]); |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3473
diff
changeset
|
3560 |
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
|
3561 |
loco_id = _new_vehicle_id; |
0 | 3562 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3563 |
for (i = 0; _players_ai[p->index].order_list_blocks[i] != 0xFF; i++) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3564 |
AiBuildRec *aib = (&_players_ai[p->index].src) + _players_ai[p->index].order_list_blocks[i]; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3565 |
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
|
3566 |
Order order; |
9b115b39c515
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3567 |
|
9332
2e120d0bd632
(svn r12584) -Codechange: do not access the order type directly.
rubidium
parents:
9296
diff
changeset
|
3568 |
order.MakeGoToStation(AiGetStationIdFromAircraftBlock(aib->use_tile, aib->cur_building_rule)); |
558
9b115b39c515
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3569 |
|
10081
e46b9eb2f9c1
(svn r12617) -Codechange: add type safety to the Order's load and unload types.
rubidium
parents:
9339
diff
changeset
|
3570 |
if (!is_pass && i == 1) order.SetUnloadType(OUFB_UNLOAD); |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3571 |
if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0)) |
10081
e46b9eb2f9c1
(svn r12617) -Codechange: add type safety to the Order's load and unload types.
rubidium
parents:
9339
diff
changeset
|
3572 |
order.SetLoadType(OLFB_FULL_LOAD); |
558
9b115b39c515
(svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents:
555
diff
changeset
|
3573 |
|
9335
18d3658adc60
(svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents:
9332
diff
changeset
|
3574 |
DoCommand(0, loco_id + (i << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER); |
0 | 3575 |
} |
3576 |
||
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3473
diff
changeset
|
3577 |
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
|
3578 |
|
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3473
diff
changeset
|
3579 |
DoCommand(0, loco_id, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT); |
0 | 3580 |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3581 |
if (_players_ai[p->index].num_want_fullload != 0) _players_ai[p->index].num_want_fullload--; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3582 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3583 |
if (--_players_ai[p->index].num_loco_to_build == 0) _players_ai[p->index].state = AIS_0; |
0 | 3584 |
} |
3585 |
||
3586 |
static void AiStateCheckShipStuff(Player *p) |
|
3587 |
{ |
|
7951
477c5cef5797
(svn r10942) -Fix [FS#1133]: make the AI not crash when it has ships as the AI does not support them.
rubidium
parents:
7886
diff
changeset
|
3588 |
/* Ships are not implemented in this (broken) AI */ |
0 | 3589 |
} |
3590 |
||
3591 |
static void AiStateBuildDefaultShipBlocks(Player *p) |
|
3592 |
{ |
|
7951
477c5cef5797
(svn r10942) -Fix [FS#1133]: make the AI not crash when it has ships as the AI does not support them.
rubidium
parents:
7886
diff
changeset
|
3593 |
/* Ships are not implemented in this (broken) AI */ |
0 | 3594 |
} |
3595 |
||
3596 |
static void AiStateDoShipStuff(Player *p) |
|
3597 |
{ |
|
7951
477c5cef5797
(svn r10942) -Fix [FS#1133]: make the AI not crash when it has ships as the AI does not support them.
rubidium
parents:
7886
diff
changeset
|
3598 |
/* Ships are not implemented in this (broken) AI */ |
0 | 3599 |
} |
3600 |
||
3601 |
static void AiStateSellVeh(Player *p) |
|
3602 |
{ |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3603 |
Vehicle *v = _players_ai[p->index].cur_veh; |
0 | 3604 |
|
3605 |
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
|
3606 |
if (v->type == VEH_TRAIN) { |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3607 |
|
10221
faef62283f5f
(svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents:
10213
diff
changeset
|
3608 |
if (!IsRailDepotTile(v->tile) || v->u.rail.track != TRACK_BIT_DEPOT || !(v->vehstatus & VS_STOPPED)) { |
9332
2e120d0bd632
(svn r12584) -Codechange: do not access the order type directly.
rubidium
parents:
9296
diff
changeset
|
3609 |
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
|
3610 |
DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_TRAIN_TO_DEPOT); |
0 | 3611 |
goto going_to_depot; |
3612 |
} |
|
3613 |
||
3614 |
// 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
|
3615 |
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
|
3616 |
|
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
|
3617 |
} else if (v->type == VEH_ROAD) { |
7986
881998b115c2
(svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents:
7951
diff
changeset
|
3618 |
if (!v->IsStoppedInDepot()) { |
9332
2e120d0bd632
(svn r12584) -Codechange: do not access the order type directly.
rubidium
parents:
9296
diff
changeset
|
3619 |
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
|
3620 |
DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT); |
0 | 3621 |
goto going_to_depot; |
3622 |
} |
|
3623 |
||
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3473
diff
changeset
|
3624 |
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
|
3625 |
} else if (v->type == VEH_AIRCRAFT) { |
7986
881998b115c2
(svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents:
7951
diff
changeset
|
3626 |
if (!v->IsStoppedInDepot()) { |
9332
2e120d0bd632
(svn r12584) -Codechange: do not access the order type directly.
rubidium
parents:
9296
diff
changeset
|
3627 |
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
|
3628 |
DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_AIRCRAFT_TO_HANGAR); |
0 | 3629 |
goto going_to_depot; |
3630 |
} |
|
3631 |
||
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3473
diff
changeset
|
3632 |
DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT); |
7951
477c5cef5797
(svn r10942) -Fix [FS#1133]: make the AI not crash when it has ships as the AI does not support them.
rubidium
parents:
7886
diff
changeset
|
3633 |
} else if (v->type == VEH_SHIP) { |
477c5cef5797
(svn r10942) -Fix [FS#1133]: make the AI not crash when it has ships as the AI does not support them.
rubidium
parents:
7886
diff
changeset
|
3634 |
/* Ships are not implemented in this (broken) AI */ |
0 | 3635 |
} |
3636 |
} |
|
3637 |
||
3638 |
goto return_to_loop; |
|
3639 |
going_to_depot:; |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3640 |
if (++_players_ai[p->index].state_counter <= 832) return; |
0 | 3641 |
|
9332
2e120d0bd632
(svn r12584) -Codechange: do not access the order type directly.
rubidium
parents:
9296
diff
changeset
|
3642 |
if (v->current_order.IsType(OT_GOTO_DEPOT)) { |
2e120d0bd632
(svn r12584) -Codechange: do not access the order type directly.
rubidium
parents:
9296
diff
changeset
|
3643 |
v->current_order.MakeDummy(); |
0 | 3644 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
3645 |
} |
|
3646 |
return_to_loop:; |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3647 |
_players_ai[p->index].state = AIS_VEH_LOOP; |
0 | 3648 |
} |
3649 |
||
3650 |
static void AiStateRemoveStation(Player *p) |
|
3651 |
{ |
|
3652 |
// 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
|
3653 |
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
|
3654 |
const Station *st; |
55f4cbda781b
(svn r2144) - Fix: AI crash when trying to remove stations with index higher than 256.
darkvater
parents:
1576
diff
changeset
|
3655 |
TileIndex tile; |
0 | 3656 |
|
3657 |
// Go to this state when we're done. |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3658 |
_players_ai[p->index].state = AIS_1; |
0 | 3659 |
|
3660 |
// 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
|
3661 |
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
|
3662 |
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
|
3663 |
FOR_ALL_ORDERS(ord) { |
9336
3f75a2c5e0d3
(svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents:
9335
diff
changeset
|
3664 |
if (ord->IsType(OT_GOTO_STATION)) in_use[ord->GetDestination()] = 1; |
0 | 3665 |
} |
3666 |
||
3667 |
// Go through all stations and delete those that aren't in use |
|
3668 |
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
|
3669 |
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
|
3670 |
( (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
|
3671 |
(st->truck_stops != NULL && (tile = st->truck_stops->xy)) != 0 || |
0 | 3672 |
(tile = st->train_tile) != 0 || |
3673 |
(tile = st->dock_tile) != 0 || |
|
3674 |
(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
|
3675 |
DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); |
0 | 3676 |
} |
3677 |
} |
|
3678 |
||
1640
55f4cbda781b
(svn r2144) - Fix: AI crash when trying to remove stations with index higher than 256.
darkvater
parents:
1576
diff
changeset
|
3679 |
free(in_use); |
0 | 3680 |
} |
3681 |
||
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
|
3682 |
static void AiRemovePlayerRailOrRoad(Player *p, TileIndex tile) |
0 | 3683 |
{ |
3269
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3234
diff
changeset
|
3684 |
TrackBits rails; |
0 | 3685 |
|
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
|
3686 |
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
|
3687 |
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
|
3688 |
|
3269
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3234
diff
changeset
|
3689 |
if (IsPlainRailTile(tile)) { |
0 | 3690 |
is_rail_crossing:; |
3269
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3234
diff
changeset
|
3691 |
rails = GetRailTrackStatus(tile); |
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3234
diff
changeset
|
3692 |
|
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3234
diff
changeset
|
3693 |
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
|
3694 |
|
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3234
diff
changeset
|
3695 |
if (rails & TRACK_BIT_3WAY_NE) { |
0 | 3696 |
pos_0: |
3269
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3234
diff
changeset
|
3697 |
if ((GetRailTrackStatus(TILE_MASK(tile - TileDiffXY(1, 0))) & TRACK_BIT_3WAY_SW) == 0) { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3698 |
_players_ai[p->index].cur_dir_a = DIAGDIR_NE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3699 |
_players_ai[p->index].cur_tile_a = tile; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3700 |
_players_ai[p->index].state = AIS_REMOVE_SINGLE_RAIL_TILE; |
0 | 3701 |
return; |
3702 |
} |
|
3703 |
} |
|
3704 |
||
3269
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3234
diff
changeset
|
3705 |
if (rails & TRACK_BIT_3WAY_SE) { |
0 | 3706 |
pos_1: |
3269
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3234
diff
changeset
|
3707 |
if ((GetRailTrackStatus(TILE_MASK(tile + TileDiffXY(0, 1))) & TRACK_BIT_3WAY_NW) == 0) { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3708 |
_players_ai[p->index].cur_dir_a = DIAGDIR_SE; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3709 |
_players_ai[p->index].cur_tile_a = tile; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3710 |
_players_ai[p->index].state = AIS_REMOVE_SINGLE_RAIL_TILE; |
0 | 3711 |
return; |
3712 |
} |
|
3713 |
} |
|
3714 |
||
3269
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3234
diff
changeset
|
3715 |
if (rails & TRACK_BIT_3WAY_SW) { |
0 | 3716 |
pos_2: |
3269
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3234
diff
changeset
|
3717 |
if ((GetRailTrackStatus(TILE_MASK(tile + TileDiffXY(1, 0))) & TRACK_BIT_3WAY_NE) == 0) { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3718 |
_players_ai[p->index].cur_dir_a = DIAGDIR_SW; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3719 |
_players_ai[p->index].cur_tile_a = tile; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3720 |
_players_ai[p->index].state = AIS_REMOVE_SINGLE_RAIL_TILE; |
0 | 3721 |
return; |
3722 |
} |
|
3723 |
} |
|
3724 |
||
3269
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3234
diff
changeset
|
3725 |
if (rails & TRACK_BIT_3WAY_NW) { |
0 | 3726 |
pos_3: |
3269
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3234
diff
changeset
|
3727 |
if ((GetRailTrackStatus(TILE_MASK(tile - TileDiffXY(0, 1))) & TRACK_BIT_3WAY_SE) == 0) { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3728 |
_players_ai[p->index].cur_dir_a = DIAGDIR_NW; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3729 |
_players_ai[p->index].cur_tile_a = tile; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3730 |
_players_ai[p->index].state = AIS_REMOVE_SINGLE_RAIL_TILE; |
0 | 3731 |
return; |
3732 |
} |
|
3733 |
} |
|
3734 |
} else { |
|
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
3735 |
static const byte _depot_bits[] = {0x19, 0x16, 0x25, 0x2A}; |
0 | 3736 |
|
3185 | 3737 |
DiagDirection dir = GetRailDepotDirection(tile); |
3738 |
||
4559
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4527
diff
changeset
|
3739 |
if (GetRailTrackStatus(tile + TileOffsByDiagDir(dir)) & _depot_bits[dir]) |
0 | 3740 |
return; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3741 |
|
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3473
diff
changeset
|
3742 |
DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); |
0 | 3743 |
} |
7866
e19fda04e8d3
(svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents:
7813
diff
changeset
|
3744 |
} 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
|
3745 |
if (!IsTileOwner(tile, _current_player)) return; |
0 | 3746 |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3747 |
if (IsLevelCrossing(tile)) goto is_rail_crossing; |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3748 |
|
9059
04edde3eb0c6
(svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents:
9052
diff
changeset
|
3749 |
if (IsRoadDepot(tile)) { |
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
3750 |
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
|
3751 |
TileIndex t; |
0 | 3752 |
|
3753 |
// 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
|
3754 |
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
|
3755 |
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
|
3756 |
|
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
|
3757 |
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
|
3758 |
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
|
3759 |
|
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
|
3760 |
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
|
3761 |
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
|
3762 |
|
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
|
3763 |
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
|
3764 |
if (IsTileType(t, MP_STATION) && IsTileOwner(t, _current_player)) return; |
0 | 3765 |
|
3179 | 3766 |
dir = GetRoadDepotDirection(tile); |
0 | 3767 |
|
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3473
diff
changeset
|
3768 |
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
|
3769 |
DoCommand( |
4559
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4527
diff
changeset
|
3770 |
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
|
3771 |
DiagDirToRoadBits(ReverseDiagDir(dir)), |
0 | 3772 |
0, |
3773 |
DC_EXEC, |
|
3774 |
CMD_REMOVE_ROAD); |
|
3775 |
} |
|
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
|
3776 |
} 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
|
3777 |
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
|
3778 |
!IsBridge(tile) || |
8579
3efbb430092e
(svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents:
8502
diff
changeset
|
3779 |
GetTunnelBridgeTransportType(tile) != TRANSPORT_RAIL) { |
0 | 3780 |
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
|
3781 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3782 |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
3783 |
rails = TRACK_BIT_NONE; |
0 | 3784 |
|
8579
3efbb430092e
(svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents:
8502
diff
changeset
|
3785 |
switch (GetTunnelBridgeDirection(tile)) { |
3196 | 3786 |
default: |
3787 |
case DIAGDIR_NE: goto pos_2; |
|
3788 |
case DIAGDIR_SE: goto pos_3; |
|
3789 |
case DIAGDIR_SW: goto pos_0; |
|
3790 |
case DIAGDIR_NW: goto pos_1; |
|
3791 |
} |
|
0 | 3792 |
} |
3793 |
} |
|
3794 |
||
3795 |
static void AiStateRemoveTrack(Player *p) |
|
3796 |
{ |
|
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
|
3797 |
/* 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
|
3798 |
int num = MapSizeX() * 4; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3799 |
|
0 | 3800 |
do { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3801 |
TileIndex tile = ++_players_ai[p->index].state_counter; |
0 | 3802 |
|
3803 |
// 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
|
3804 |
if (tile >= MapSize()) { |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3805 |
_players_ai[p->index].state = AIS_REMOVE_STATION; |
0 | 3806 |
return; |
3807 |
} |
|
3808 |
||
3809 |
// Remove player stuff in that tile |
|
3810 |
AiRemovePlayerRailOrRoad(p, tile); |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3811 |
if (_players_ai[p->index].state != AIS_REMOVE_TRACK) return; |
0 | 3812 |
} while (--num); |
3813 |
} |
|
3814 |
||
3815 |
static void AiStateRemoveSingleRailTile(Player *p) |
|
3816 |
{ |
|
3817 |
// Remove until we can't remove more. |
|
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3818 |
if (!AiRemoveTileAndGoForward(p)) _players_ai[p->index].state = AIS_REMOVE_TRACK; |
0 | 3819 |
} |
3820 |
||
3821 |
static AiStateAction * const _ai_actions[] = { |
|
3822 |
AiCase0, |
|
3823 |
AiCase1, |
|
3824 |
AiStateVehLoop, |
|
3825 |
AiStateCheckReplaceVehicle, |
|
3826 |
AiStateDoReplaceVehicle, |
|
3827 |
AiStateWantNewRoute, |
|
3828 |
||
3829 |
AiStateBuildDefaultRailBlocks, |
|
3830 |
AiStateBuildRail, |
|
3831 |
AiStateBuildRailVeh, |
|
3832 |
AiStateDeleteRailBlocks, |
|
3833 |
||
3834 |
AiStateBuildDefaultRoadBlocks, |
|
3835 |
AiStateBuildRoad, |
|
3836 |
AiStateBuildRoadVehicles, |
|
3837 |
AiStateDeleteRoadBlocks, |
|
3838 |
||
3839 |
AiStateAirportStuff, |
|
3840 |
AiStateBuildDefaultAirportBlocks, |
|
3841 |
AiStateBuildAircraftVehicles, |
|
3842 |
||
3843 |
AiStateCheckShipStuff, |
|
3844 |
AiStateBuildDefaultShipBlocks, |
|
3845 |
AiStateDoShipStuff, |
|
3846 |
||
3847 |
AiStateSellVeh, |
|
3848 |
AiStateRemoveStation, |
|
3849 |
AiStateRemoveTrack, |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3850 |
|
0 | 3851 |
AiStateRemoveSingleRailTile |
3852 |
}; |
|
3853 |
||
3854 |
extern void ShowBuyCompanyDialog(uint player); |
|
3855 |
||
3856 |
static void AiHandleTakeover(Player *p) |
|
3857 |
{ |
|
3858 |
if (p->bankrupt_timeout != 0) { |
|
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3859 |
p->bankrupt_timeout -= 8; |
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3860 |
if (p->bankrupt_timeout > 0) return; |
0 | 3861 |
p->bankrupt_timeout = 0; |
3862 |
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
|
3863 |
if (IsLocalPlayer()) { |
0 | 3864 |
AskExitToGameMenu(); |
3865 |
return; |
|
3866 |
} |
|
4845
49105d3b5a9a
(svn r6771) -Codechange: Replace two macros with functions. IS_HUMAN_PLAYER and
Darkvater
parents:
4560
diff
changeset
|
3867 |
if (IsHumanPlayer(_current_player)) return; |
0 | 3868 |
} |
3869 |
||
3033
e4f7c60a1742
(svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents:
3022
diff
changeset
|
3870 |
if (p->bankrupt_asked == 255) return; |
0 | 3871 |
|
3872 |
{ |
|
3873 |
uint asked = p->bankrupt_asked; |
|
3874 |
Player *pp, *best_pl = NULL; |
|
3875 |
int32 best_val = -1; |
|
3876 |
||
3877 |
// Ask the guy with the highest performance hist. |
|
3878 |
FOR_ALL_PLAYERS(pp) { |
|
3879 |
if (pp->is_active && |
|
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
3880 |
!(asked & 1) && |
0 | 3881 |
pp->bankrupt_asked == 0 && |
3882 |
best_val < pp->old_economy[1].performance_history) { |
|
3883 |
best_val = pp->old_economy[1].performance_history; |
|
3884 |
best_pl = pp; |
|
3885 |
} |
|
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
3886 |
asked >>= 1; |
0 | 3887 |
} |
3888 |
||
3889 |
// Asked all players? |
|
3890 |
if (best_val == -1) { |
|
3891 |
p->bankrupt_asked = 255; |
|
3892 |
return; |
|
3893 |
} |
|
3894 |
||
8427
143b0be22af1
(svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents:
8424
diff
changeset
|
3895 |
SetBit(p->bankrupt_asked, best_pl->index); |
0 | 3896 |
|
3897 |
if (best_pl->index == _local_player) { |
|
3898 |
p->bankrupt_timeout = 4440; |
|
3899 |
ShowBuyCompanyDialog(_current_player); |
|
3900 |
return; |
|
3901 |
} |
|
4845
49105d3b5a9a
(svn r6771) -Codechange: Replace two macros with functions. IS_HUMAN_PLAYER and
Darkvater
parents:
4560
diff
changeset
|
3902 |
if (IsHumanPlayer(best_pl->index)) return; |
0 | 3903 |
|
3904 |
// Too little money for computer to buy it? |
|
3905 |
if (best_pl->player_money >> 1 >= p->bankrupt_value) { |
|
3906 |
// 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
|
3907 |
PlayerID old_p = _current_player; |
6900
872757461d92
(svn r9540) -Fix (r1): Bankrupt AIs no longer buy themselves (usage of wrong variable)
celestar
parents:
6896
diff
changeset
|
3908 |
_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
|
3909 |
DoCommand(0, old_p, 0, DC_EXEC, CMD_BUY_COMPANY); |
0 | 3910 |
_current_player = old_p; |
3911 |
} |
|
3912 |
} |
|
3913 |
} |
|
3914 |
||
4011
db5514de70cc
(svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents:
4000
diff
changeset
|
3915 |
static void AiAdjustLoan(const Player* p) |
0 | 3916 |
{ |
7486
d130c10f4dab
(svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents:
7446
diff
changeset
|
3917 |
Money base = AiGetBasePrice(p); |
0 | 3918 |
|
3919 |
if (p->player_money > base * 1400) { |
|
3920 |
// Decrease loan |
|
3921 |
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
|
3922 |
DoCommand(0, 0, 0, DC_EXEC, CMD_DECREASE_LOAN); |
0 | 3923 |
} |
3924 |
} else if (p->player_money < base * 500) { |
|
3925 |
// Increase loan |
|
3926 |
if (p->current_loan < _economy.max_loan && |
|
3927 |
p->num_valid_stat_ent >= 2 && |
|
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
3928 |
-(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
|
3929 |
DoCommand(0, 0, 0, DC_EXEC, CMD_INCREASE_LOAN); |
0 | 3930 |
} |
3931 |
} |
|
3932 |
} |
|
3933 |
||
3934 |
static void AiBuildCompanyHQ(Player *p) |
|
3935 |
{ |
|
3936 |
TileIndex tile; |
|
3937 |
||
3938 |
if (p->location_of_house == 0 && |
|
3939 |
p->last_build_coordinate != 0) { |
|
3940 |
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
|
3941 |
DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ); |
0 | 3942 |
} |
3943 |
} |
|
3944 |
||
3945 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
192
diff
changeset
|
3946 |
void AiDoGameLoop(Player *p) |
0 | 3947 |
{ |
3948 |
if (p->bankrupt_asked != 0) { |
|
3949 |
AiHandleTakeover(p); |
|
3950 |
return; |
|
3951 |
} |
|
3952 |
||
192
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
3953 |
// 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
|
3954 |
// to the patch-setting |
614bba52258d
(svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents:
159
diff
changeset
|
3955 |
// 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
|
3956 |
// or in % |
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
3957 |
_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
|
3958 |
|
4845
49105d3b5a9a
(svn r6771) -Codechange: Replace two macros with functions. IS_HUMAN_PLAYER and
Darkvater
parents:
4560
diff
changeset
|
3959 |
if (IsHumanPlayer(_current_player)) return; |
0 | 3960 |
|
3961 |
AiAdjustLoan(p); |
|
3962 |
AiBuildCompanyHQ(p); |
|
3963 |
||
3964 |
#if 0 |
|
3965 |
{ |
|
3966 |
static byte old_state = 99; |
|
3967 |
static bool hasdots = false; |
|
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6949
diff
changeset
|
3968 |
char *_ai_state_names[] = { |
0 | 3969 |
"AiCase0", |
3970 |
"AiCase1", |
|
3971 |
"AiStateVehLoop", |
|
3972 |
"AiStateCheckReplaceVehicle", |
|
3973 |
"AiStateDoReplaceVehicle", |
|
3974 |
"AiStateWantNewRoute", |
|
3975 |
"AiStateBuildDefaultRailBlocks", |
|
3976 |
"AiStateBuildRail", |
|
3977 |
"AiStateBuildRailVeh", |
|
3978 |
"AiStateDeleteRailBlocks", |
|
3979 |
"AiStateBuildDefaultRoadBlocks", |
|
3980 |
"AiStateBuildRoad", |
|
3981 |
"AiStateBuildRoadVehicles", |
|
3982 |
"AiStateDeleteRoadBlocks", |
|
3983 |
"AiStateAirportStuff", |
|
3984 |
"AiStateBuildDefaultAirportBlocks", |
|
3985 |
"AiStateBuildAircraftVehicles", |
|
3986 |
"AiStateCheckShipStuff", |
|
3987 |
"AiStateBuildDefaultShipBlocks", |
|
3988 |
"AiStateDoShipStuff", |
|
3989 |
"AiStateSellVeh", |
|
3990 |
"AiStateRemoveStation", |
|
3991 |
"AiStateRemoveTrack", |
|
3992 |
"AiStateRemoveSingleRailTile" |
|
3993 |
}; |
|
3994 |
||
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3995 |
if (_players_ai[p->index].state != old_state) { |
0 | 3996 |
if (hasdots) |
3997 |
printf("\n"); |
|
10229
fba3f9fa44d7
(svn r12761) -Codechange: lots of minor whitespace coding style fixes around operators.
rubidium
parents:
10222
diff
changeset
|
3998 |
hasdots = false; |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
3999 |
printf("AiState: %s\n", _ai_state_names[old_state=_players_ai[p->index].state]); |
0 | 4000 |
} else { |
4001 |
printf("."); |
|
10229
fba3f9fa44d7
(svn r12761) -Codechange: lots of minor whitespace coding style fixes around operators.
rubidium
parents:
10222
diff
changeset
|
4002 |
hasdots = true; |
0 | 4003 |
} |
4004 |
} |
|
4005 |
#endif |
|
4006 |
||
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4007 |
_ai_actions[_players_ai[p->index].state](p); |
0 | 4008 |
} |
8725
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4009 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4010 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4011 |
static const SaveLoad _player_ai_desc[] = { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4012 |
SLE_VAR(PlayerAI, state, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4013 |
SLE_VAR(PlayerAI, tick, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4014 |
SLE_CONDVAR(PlayerAI, state_counter, SLE_FILE_U16 | SLE_VAR_U32, 0, 12), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4015 |
SLE_CONDVAR(PlayerAI, state_counter, SLE_UINT32, 13, SL_MAX_VERSION), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4016 |
SLE_VAR(PlayerAI, timeout_counter, SLE_UINT16), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4017 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4018 |
SLE_VAR(PlayerAI, state_mode, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4019 |
SLE_VAR(PlayerAI, banned_tile_count, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4020 |
SLE_VAR(PlayerAI, railtype_to_use, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4021 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4022 |
SLE_VAR(PlayerAI, cargo_type, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4023 |
SLE_VAR(PlayerAI, num_wagons, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4024 |
SLE_VAR(PlayerAI, build_kind, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4025 |
SLE_VAR(PlayerAI, num_build_rec, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4026 |
SLE_VAR(PlayerAI, num_loco_to_build, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4027 |
SLE_VAR(PlayerAI, num_want_fullload, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4028 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4029 |
SLE_VAR(PlayerAI, route_type_mask, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4030 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4031 |
SLE_CONDVAR(PlayerAI, start_tile_a, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4032 |
SLE_CONDVAR(PlayerAI, start_tile_a, SLE_UINT32, 6, SL_MAX_VERSION), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4033 |
SLE_CONDVAR(PlayerAI, cur_tile_a, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4034 |
SLE_CONDVAR(PlayerAI, cur_tile_a, SLE_UINT32, 6, SL_MAX_VERSION), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4035 |
SLE_VAR(PlayerAI, start_dir_a, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4036 |
SLE_VAR(PlayerAI, cur_dir_a, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4037 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4038 |
SLE_CONDVAR(PlayerAI, start_tile_b, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4039 |
SLE_CONDVAR(PlayerAI, start_tile_b, SLE_UINT32, 6, SL_MAX_VERSION), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4040 |
SLE_CONDVAR(PlayerAI, cur_tile_b, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4041 |
SLE_CONDVAR(PlayerAI, cur_tile_b, SLE_UINT32, 6, SL_MAX_VERSION), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4042 |
SLE_VAR(PlayerAI, start_dir_b, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4043 |
SLE_VAR(PlayerAI, cur_dir_b, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4044 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4045 |
SLE_REF(PlayerAI, cur_veh, REF_VEHICLE), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4046 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4047 |
SLE_ARR(PlayerAI, wagon_list, SLE_UINT16, 9), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4048 |
SLE_ARR(PlayerAI, order_list_blocks, SLE_UINT8, 20), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4049 |
SLE_ARR(PlayerAI, banned_tiles, SLE_UINT16, 16), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4050 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4051 |
SLE_CONDNULL(64, 2, SL_MAX_VERSION), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4052 |
SLE_END() |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4053 |
}; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4054 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4055 |
static const SaveLoad _player_ai_build_rec_desc[] = { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4056 |
SLE_CONDVAR(AiBuildRec, spec_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4057 |
SLE_CONDVAR(AiBuildRec, spec_tile, SLE_UINT32, 6, SL_MAX_VERSION), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4058 |
SLE_CONDVAR(AiBuildRec, use_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4059 |
SLE_CONDVAR(AiBuildRec, use_tile, SLE_UINT32, 6, SL_MAX_VERSION), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4060 |
SLE_VAR(AiBuildRec, rand_rng, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4061 |
SLE_VAR(AiBuildRec, cur_building_rule, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4062 |
SLE_VAR(AiBuildRec, unk6, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4063 |
SLE_VAR(AiBuildRec, unk7, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4064 |
SLE_VAR(AiBuildRec, buildcmd_a, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4065 |
SLE_VAR(AiBuildRec, buildcmd_b, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4066 |
SLE_VAR(AiBuildRec, direction, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4067 |
SLE_VAR(AiBuildRec, cargo, SLE_UINT8), |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4068 |
SLE_END() |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4069 |
}; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4070 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4071 |
|
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4072 |
void SaveLoad_AI(PlayerID id) |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4073 |
{ |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4074 |
PlayerAI *pai = &_players_ai[id]; |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4075 |
SlObject(pai, _player_ai_desc); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4076 |
for (int i = 0; i != pai->num_build_rec; i++) { |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4077 |
SlObject(&pai->src + i, _player_ai_build_rec_desc); |
b769d0a63f06
(svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents:
8640
diff
changeset
|
4078 |
} |
8732
b18f578f7c16
(svn r11800) -Codechange: move some functions to a more logical location + some type safety.
rubidium
parents:
8726
diff
changeset
|
4079 |
} |