src/ai/default/default.cpp
author rubidium
Sun, 01 Jun 2008 16:45:32 +0000
branch0.6
changeset 10801 3ad9dfb5430d
parent 9113 4871417ad10c
permissions -rw-r--r--
(svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
- Fix: Industry tiles would sometimes tell they need a 'level' slope when they do not want the slope (r13348)
- Fix: Attempts to make the old AI perform better (r13217, r13221, r13222)
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
29717e930f9a (svn r3857) Add and use GetBridgeRampDirection()
tron
parents: 3185
diff changeset
     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
c0ef161f700f (svn r3831) Add and use GetRailDepotDirection()
tron
parents: 3184
diff changeset
     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"
2381
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2186
diff changeset
    14
#include "../../engine.h"
8612
6414fc21c2f3 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8604
diff changeset
    15
#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
    16
#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
    17
#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
    18
#include "../../station.h"
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2186
diff changeset
    19
#include "../../pathfind.h"
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2186
diff changeset
    20
#include "../../airport.h"
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2186
diff changeset
    21
#include "../../depot.h"
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2186
diff changeset
    22
#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
    23
#include "../../bridge.h"
8636
2b158acb649c (svn r11702) -Codechange: move all date related stuff to date*.
rubidium
parents: 8635
diff changeset
    24
#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
    25
#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
    26
#include "../../window_func.h"
8640
1e93b81e96d2 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8636
diff changeset
    27
#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
    28
#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
    29
#include "../../saveload.h"
8750
fdd6054e7bae (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8732
diff changeset
    30
#include "../../player_func.h"
fdd6054e7bae (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8732
diff changeset
    31
#include "../../player_base.h"
8766
c86cfa3a7580 (svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents: 8760
diff changeset
    32
#include "../../settings_type.h"
2447
47f2b670fb22 (svn r2973) Move a function declaration somewhere where it belongs
tron
parents: 2425
diff changeset
    33
#include "default.h"
8894
1e5b2d4380b8 (svn r11968) -Codechange: remove redundant FindLengthOfTunnel(), use GetTunnelBridgeLength() and/or GetOtherTunnelEnd() instead
smatz
parents: 8888
diff changeset
    34
#include "../../tunnelbridge.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    35
8760
ce0891c412ce (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8750
diff changeset
    36
#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
    37
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
// remove some day perhaps?
192
614bba52258d (svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents: 159
diff changeset
    39
static uint _ai_service_interval;
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
    40
PlayerAI _players_ai[MAX_PLAYERS];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    41
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
typedef void AiStateAction(Player *p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    43
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    44
enum {
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    45
	AIS_0                            =  0,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    46
	AIS_1                            =  1,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    47
	AIS_VEH_LOOP                     =  2,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    48
	AIS_VEH_CHECK_REPLACE_VEHICLE    =  3,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    49
	AIS_VEH_DO_REPLACE_VEHICLE       =  4,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    50
	AIS_WANT_NEW_ROUTE               =  5,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    51
	AIS_BUILD_DEFAULT_RAIL_BLOCKS    =  6,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    52
	AIS_BUILD_RAIL                   =  7,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    53
	AIS_BUILD_RAIL_VEH               =  8,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    54
	AIS_DELETE_RAIL_BLOCKS           =  9,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    55
	AIS_BUILD_DEFAULT_ROAD_BLOCKS    = 10,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    56
	AIS_BUILD_ROAD                   = 11,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    57
	AIS_BUILD_ROAD_VEHICLES          = 12,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    58
	AIS_DELETE_ROAD_BLOCKS           = 13,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    59
	AIS_AIRPORT_STUFF                = 14,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    60
	AIS_BUILD_DEFAULT_AIRPORT_BLOCKS = 15,
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    61
	AIS_BUILD_AIRCRAFT_VEHICLES      = 16,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    62
	AIS_CHECK_SHIP_STUFF             = 17,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    63
	AIS_BUILD_DEFAULT_SHIP_BLOCKS    = 18,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    64
	AIS_DO_SHIP_STUFF                = 19,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    65
	AIS_SELL_VEHICLE                 = 20,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    66
	AIS_REMOVE_STATION               = 21,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    67
	AIS_REMOVE_TRACK                 = 22,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
    68
	AIS_REMOVE_SINGLE_RAIL_TILE      = 23
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    71
9112
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9110
diff changeset
    72
static inline TrackBits GetRailTrackStatus(TileIndex tile)
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
    73
{
9112
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9110
diff changeset
    74
	return TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_RAIL, 0));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
static void AiCase0(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
{
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
    80
	_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
    81
	_players_ai[p->index].state_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    82
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    83
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    84
static void AiCase1(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    85
{
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
    86
	_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
    87
	_players_ai[p->index].state = AIS_VEH_LOOP;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    88
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    89
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
static void AiStateVehLoop(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    91
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    92
	Vehicle *v;
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
    93
	uint index;
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
    94
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
    95
	index = (_players_ai[p->index].cur_veh == NULL) ? 0 : _players_ai[p->index].cur_veh->index + 1;
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
    96
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
    97
	FOR_ALL_VEHICLES_FROM(v, index) {
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
    98
		if (v->owner != _current_player) continue;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
    99
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   100
		if ((v->type == VEH_TRAIN && v->subtype == 0) ||
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   101
				v->type == VEH_ROAD ||
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   102
				(v->type == VEH_AIRCRAFT && IsNormalAircraft(v)) ||
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   103
				v->type == VEH_SHIP) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   104
			/* replace engine? */
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   105
			if (v->type == VEH_TRAIN && v->engine_type < 3 &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
					(_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
   107
				_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
   108
				_players_ai[p->index].cur_veh = v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
				return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   112
			/* not profitable? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
			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
   114
					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
   115
					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
   116
				_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
   117
				_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
   118
				_players_ai[p->index].cur_veh = v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
				return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
			/* not reliable? */
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   123
			if (v->age >= v->max_age || (
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   124
						v->age != 0 &&
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   125
						GetEngine(v->engine_type)->reliability < 35389
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   126
					)) {
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
   127
				_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
   128
				_players_ai[p->index].cur_veh = v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   129
				return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   133
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
   134
	_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
   135
	_players_ai[p->index].state_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   136
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   137
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
   138
static EngineID AiChooseTrainToBuild(RailType railtype, Money money, byte flag, TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
{
2544
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
   140
	EngineID best_veh_index = INVALID_ENGINE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   141
	byte best_veh_score = 0;
2544
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
   142
	EngineID i;
460
f11659d28617 (svn r677) -newgrf: Fix some custom electric trains appearing in maglev depots (pasky).
darkvater
parents: 410
diff changeset
   143
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
   144
	FOR_ALL_ENGINEIDS_OF_TYPE(i, VEH_TRAIN) {
539
ccf9518e6cfd (svn r923) Forgot s/rail_vehinfo/RailVehInfo/
tron
parents: 538
diff changeset
   145
		const RailVehicleInfo *rvi = RailVehInfo(i);
1926
68d60188a22f (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1901
diff changeset
   146
		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
   147
6074
e70d63ef4d62 (svn r8385) -Fix
tron
parents: 5984
diff changeset
   148
		if (!IsCompatibleRail(rvi->railtype, railtype) ||
6119
b47985557d1e (svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents: 6105
diff changeset
   149
				rvi->railveh_type == RAILVEH_WAGON ||
b47985557d1e (svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents: 6105
diff changeset
   150
				(rvi->railveh_type == RAILVEH_MULTIHEAD && flag & 1) ||
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8149
diff changeset
   151
				!HasBit(e->player_avail, _current_player) ||
2529
97c0b71ee7dd (svn r3058) The default AI may not build dual headed trains under certain circumstances.
tron
parents: 2523
diff changeset
   152
				e->reliability < 0x8A3D) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   153
			continue;
2529
97c0b71ee7dd (svn r3058) The default AI may not build dual headed trains under certain circumstances.
tron
parents: 2523
diff changeset
   154
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   155
8945
8cb92ac1b2fb (svn r12019) -Codechange: Add support for passenger engine designation for AI-use, NewGRF property 0x08 for trains.
peter1138
parents: 8898
diff changeset
   156
		/* 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
   157
		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
   158
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
   159
		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
   160
		if (CmdSucceeded(ret) && ret.GetCost() <= money && rvi->ai_rank >= best_veh_score) {
3022
236a3e5b3c69 (svn r3602) - Move _railveh_score data to _rail_vehicle_info->ai_rank and remove global variable to return data as we can now access this directly.
peter1138
parents: 2989
diff changeset
   161
			best_veh_score = rvi->ai_rank;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
			best_veh_index = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   163
		}
460
f11659d28617 (svn r677) -newgrf: Fix some custom electric trains appearing in maglev depots (pasky).
darkvater
parents: 410
diff changeset
   164
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   166
	return best_veh_index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   167
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   168
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
   169
static EngineID AiChooseRoadVehToBuild(CargoID cargo, Money money, TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
{
2544
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
   171
	EngineID best_veh_index = INVALID_ENGINE;
5029
66c90d197b7b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5028
diff changeset
   172
	int32 best_veh_rating = 0;
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
   173
	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
   174
b18a67c2eaeb (svn r11959) -Codechange: Use macro to loop for specific engine types instead of using specific indexes each time.
peter1138
parents: 8847
diff changeset
   175
	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
   176
		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
   177
		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
   178
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8149
diff changeset
   179
		if (!HasBit(e->player_avail, _current_player) || e->reliability < 0x8A3D) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   180
			continue;
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   181
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
5016
63281a3e8548 (svn r7057) -Codechange: Remove hardcoded lists of road vehicles for each cargo type in favour of just checking the cargo type of each vehicle.
peter1138
parents: 5004
diff changeset
   183
		/* Skip vehicles which can't take our cargo type */
5031
940b1fec4220 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5029
diff changeset
   184
		if (rvi->cargo_type != cargo && !CanRefitTo(i, cargo)) continue;
5029
66c90d197b7b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5028
diff changeset
   185
66c90d197b7b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5028
diff changeset
   186
		/* Rate and compare the engine by speed & capacity */
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
   187
		int rating = rvi->max_speed * rvi->capacity;
5029
66c90d197b7b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5028
diff changeset
   188
		if (rating <= best_veh_rating) continue;
5016
63281a3e8548 (svn r7057) -Codechange: Remove hardcoded lists of road vehicles for each cargo type in favour of just checking the cargo type of each vehicle.
peter1138
parents: 5004
diff changeset
   189
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
   190
		CommandCost ret = DoCommand(tile, i, 0, 0, CMD_BUILD_ROAD_VEH);
5031
940b1fec4220 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5029
diff changeset
   191
		if (CmdFailed(ret)) continue;
940b1fec4220 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5029
diff changeset
   192
940b1fec4220 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5029
diff changeset
   193
		/* Add the cost of refitting */
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7442
diff changeset
   194
		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
   195
		if (ret.GetCost() > money) continue;
5029
66c90d197b7b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5028
diff changeset
   196
66c90d197b7b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5028
diff changeset
   197
		best_veh_rating = rating;
66c90d197b7b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5028
diff changeset
   198
		best_veh_index = i;
3034
230ec6a1af82 (svn r3614) turn 2 do-while-loop with 2 induction variables each into canocical for loops
tron
parents: 3033
diff changeset
   199
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   200
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
	return best_veh_index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   202
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   203
8898
a36f4df63438 (svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents: 8894
diff changeset
   204
/**
a36f4df63438 (svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents: 8894
diff changeset
   205
 * 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
   206
 * @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
   207
 * @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
   208
 * @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
   209
 */
a36f4df63438 (svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents: 8894
diff changeset
   210
static EngineID AiChooseAircraftToBuild(Money money, byte forbidden)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   211
{
2544
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
   212
	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
   213
	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
   214
	EngineID i;
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   215
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
   216
	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
   217
		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
   218
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8149
diff changeset
   219
		if (!HasBit(e->player_avail, _current_player) || e->reliability < 0x8A3D) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   220
			continue;
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   221
		}
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   222
8898
a36f4df63438 (svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents: 8894
diff changeset
   223
		if ((AircraftVehInfo(i)->subtype & forbidden) != 0) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   224
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
   225
		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
   226
		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
   227
			best_veh_cost = ret.GetCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   228
			best_veh_index = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   229
		}
3034
230ec6a1af82 (svn r3614) turn 2 do-while-loop with 2 induction variables each into canocical for loops
tron
parents: 3033
diff changeset
   230
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   231
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   232
	return best_veh_index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   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
static Money AiGetBasePrice(const Player* p)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   236
{
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
   237
	Money base = _price.station_value;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   238
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   239
	// 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
   240
	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
   241
		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
   242
		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
   243
		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
   244
		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
   245
		case RAILTYPE_MAGLEV:   base *= 2; break;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   246
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
   247
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   248
	return base;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   249
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   250
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   251
static EngineID AiChooseRoadVehToReplaceWith(const Player* p, const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   252
{
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
   253
	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
   254
	return AiChooseRoadVehToBuild(v->cargo_type, avail_money, v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   255
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   257
static EngineID AiChooseAircraftToReplaceWith(const Player* p, const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   258
{
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
   259
	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
   260
a36f4df63438 (svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents: 8894
diff changeset
   261
	/* 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
   262
	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
   263
	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
   264
a36f4df63438 (svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents: 8894
diff changeset
   265
	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
   266
		if (!o->IsValid()) continue;
a36f4df63438 (svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents: 8894
diff changeset
   267
		if (!IsValidStationID(o->dest)) continue;
a36f4df63438 (svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents: 8894
diff changeset
   268
		const Station *st = GetStation(o->dest);
a36f4df63438 (svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents: 8894
diff changeset
   269
		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
   270
a36f4df63438 (svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents: 8894
diff changeset
   271
		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
   272
		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
   273
		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
   274
	}
a36f4df63438 (svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents: 8894
diff changeset
   275
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
	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
   277
		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
   278
	);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   279
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   280
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   281
static EngineID AiChooseTrainToReplaceWith(const Player* p, const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
{
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
   283
	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
   284
	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
   285
	int num = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   286
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
   287
	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
   288
		u = u->Next();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   289
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   290
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   291
	// XXX: check if a wagon
1223
e32459dd8000 (svn r1727) - Fix: now CmdBuildRailVehicle() behaves correctly if DC_EXEC is set
bjarni
parents: 1217
diff changeset
   292
	return AiChooseTrainToBuild(v->u.rail.railtype, avail_money, 0, v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   293
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   294
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   295
static EngineID AiChooseShipToReplaceWith(const Player* p, const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   296
{
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
   297
	/* Ships are not implemented in this (broken) AI */
2544
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
   298
	return INVALID_ENGINE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   299
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   300
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   301
static void AiHandleGotoDepot(Player *p, int cmd)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   302
{
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
   303
	if (_players_ai[p->index].cur_veh->current_order.type != OT_GOTO_DEPOT)
b769d0a63f06 (svn r11792) -Codechange: 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
		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
   305
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
   306
	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
   307
		_players_ai[p->index].state = AIS_VEH_DO_REPLACE_VEHICLE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   308
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   309
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   310
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
	if (_players_ai[p->index].cur_veh->current_order.type == OT_GOTO_DEPOT) {
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
   312
		_players_ai[p->index].cur_veh->current_order.type = OT_DUMMY;
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
   313
		_players_ai[p->index].cur_veh->current_order.flags = 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
   314
		InvalidateWindow(WC_VEHICLE_VIEW, _players_ai[p->index].cur_veh->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   315
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
   316
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   317
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   318
static void AiRestoreVehicleOrders(Vehicle *v, BackuppedOrders *bak)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   319
{
8818
d8d3812f8f35 (svn r11887) -Fix [FS#1658]: segmentation faults/wrong frees due uninitialized memory in the AI.
rubidium
parents: 8798
diff changeset
   320
	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
   321
d8d3812f8f35 (svn r11887) -Fix [FS#1658]: segmentation faults/wrong frees due uninitialized memory in the AI.
rubidium
parents: 8798
diff changeset
   322
	for (uint i = 0; bak->order[i].type != OT_NOTHING; i++) {
4560
f744bf3dd85a (svn r6407) -Fix: Check return values of DoCommand() with CmdFailed and that of DoCommandP
Darkvater
parents: 4559
diff changeset
   323
		if (!DoCommandP(0, v->index + (i << 16), PackOrder(&bak->order[i]), NULL, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   324
			break;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   325
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   326
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   327
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   328
static void AiHandleReplaceTrain(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   329
{
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
   330
	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
   331
	BackuppedOrders orderbak;
2544
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
   332
	EngineID veh;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   333
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   334
	// wait until the vehicle reaches the depot.
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   335
	if (!IsTileDepotType(v->tile, TRANSPORT_RAIL) || v->u.rail.track != 0x80 || !(v->vehstatus&VS_STOPPED)) {
4495
b44566fc9025 (svn r6280) -Codechange: Use the same naming for trains as for other vehicles:
Darkvater
parents: 4434
diff changeset
   336
		AiHandleGotoDepot(p, CMD_SEND_TRAIN_TO_DEPOT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   337
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   338
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   339
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   340
	veh = AiChooseTrainToReplaceWith(p, v);
2544
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
   341
	if (veh != INVALID_ENGINE) {
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1927
diff changeset
   342
		TileIndex tile;
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1927
diff changeset
   343
8149
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 8141
diff changeset
   344
		BackupVehicleOrders(v, &orderbak);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   345
		tile = v->tile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
   346
7442
a4f7989f27d4 (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 7439
diff changeset
   347
		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
   348
				CmdSucceeded(DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE))) {
3948
95f9fa0ac551 (svn r5094) Remove _new_{aircraft,roadveh,ship,train,wagon}_id. _new_vehicle_id is enough.
tron
parents: 3933
diff changeset
   349
			VehicleID veh = _new_vehicle_id;
8149
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 8141
diff changeset
   350
			AiRestoreVehicleOrders(GetVehicle(veh), &orderbak);
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
   351
			DoCommand(0, veh, 0, DC_EXEC, CMD_START_STOP_TRAIN);
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
   352
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
   353
			DoCommand(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   354
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   355
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   356
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   358
static void AiHandleReplaceRoadVeh(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   359
{
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
   360
	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
   361
	BackuppedOrders orderbak;
2544
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
   362
	EngineID veh;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   363
7986
881998b115c2 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7951
diff changeset
   364
	if (!v->IsStoppedInDepot()) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   365
		AiHandleGotoDepot(p, CMD_SEND_ROADVEH_TO_DEPOT);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   366
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   367
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   368
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
	veh = AiChooseRoadVehToReplaceWith(p, v);
2544
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
   370
	if (veh != INVALID_ENGINE) {
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1927
diff changeset
   371
		TileIndex tile;
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1927
diff changeset
   372
8818
d8d3812f8f35 (svn r11887) -Fix [FS#1658]: segmentation faults/wrong frees due uninitialized memory in the AI.
rubidium
parents: 8798
diff changeset
   373
		BackupVehicleOrders(v, &orderbak);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
		tile = v->tile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
   375
7442
a4f7989f27d4 (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 7439
diff changeset
   376
		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
   377
				CmdSucceeded(DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH))) {
3948
95f9fa0ac551 (svn r5094) Remove _new_{aircraft,roadveh,ship,train,wagon}_id. _new_vehicle_id is enough.
tron
parents: 3933
diff changeset
   378
			VehicleID veh = _new_vehicle_id;
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3977
diff changeset
   379
8818
d8d3812f8f35 (svn r11887) -Fix [FS#1658]: segmentation faults/wrong frees due uninitialized memory in the AI.
rubidium
parents: 8798
diff changeset
   380
			AiRestoreVehicleOrders(GetVehicle(veh), &orderbak);
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
   381
			DoCommand(0, veh, 0, DC_EXEC, CMD_START_STOP_ROADVEH);
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
   382
			DoCommand(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   383
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   384
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   387
static void AiHandleReplaceAircraft(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
{
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
   389
	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
   390
	BackuppedOrders orderbak;
2544
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
   391
	EngineID veh;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
7986
881998b115c2 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7951
diff changeset
   393
	if (!v->IsStoppedInDepot()) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   394
		AiHandleGotoDepot(p, CMD_SEND_AIRCRAFT_TO_HANGAR);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   395
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   396
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   397
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   398
	veh = AiChooseAircraftToReplaceWith(p, v);
2544
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
   399
	if (veh != INVALID_ENGINE) {
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1927
diff changeset
   400
		TileIndex tile;
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1927
diff changeset
   401
8818
d8d3812f8f35 (svn r11887) -Fix [FS#1658]: segmentation faults/wrong frees due uninitialized memory in the AI.
rubidium
parents: 8798
diff changeset
   402
		BackupVehicleOrders(v, &orderbak);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   403
		tile = v->tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   404
7442
a4f7989f27d4 (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 7439
diff changeset
   405
		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
   406
				CmdSucceeded(DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT))) {
3948
95f9fa0ac551 (svn r5094) Remove _new_{aircraft,roadveh,ship,train,wagon}_id. _new_vehicle_id is enough.
tron
parents: 3933
diff changeset
   407
			VehicleID veh = _new_vehicle_id;
8818
d8d3812f8f35 (svn r11887) -Fix [FS#1658]: segmentation faults/wrong frees due uninitialized memory in the AI.
rubidium
parents: 8798
diff changeset
   408
			AiRestoreVehicleOrders(GetVehicle(veh), &orderbak);
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
   409
			DoCommand(0, veh, 0, DC_EXEC, CMD_START_STOP_AIRCRAFT);
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
   410
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
   411
			DoCommand(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   412
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   413
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   415
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   416
static void AiHandleReplaceShip(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   417
{
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
   418
	/* Ships are not implemented in this (broken) AI */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   419
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   420
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   421
typedef EngineID CheckReplaceProc(const Player* p, const Vehicle* v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   422
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   423
static CheckReplaceProc* const _veh_check_replace_proc[] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   424
	AiChooseTrainToReplaceWith,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   425
	AiChooseRoadVehToReplaceWith,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   426
	AiChooseShipToReplaceWith,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   427
	AiChooseAircraftToReplaceWith,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   428
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   429
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   430
typedef void DoReplaceProc(Player *p);
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   431
static DoReplaceProc* const _veh_do_replace_proc[] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
	AiHandleReplaceTrain,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   433
	AiHandleReplaceRoadVeh,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   434
	AiHandleReplaceShip,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
	AiHandleReplaceAircraft
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   436
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   437
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
static void AiStateCheckReplaceVehicle(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   439
{
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
   440
	const Vehicle* v = _players_ai[p->index].cur_veh;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   441
7883
7777bca58ae0 (svn r10756) -Codechange: use vehicle->IsValid in favour of IsValidVehicle(vehicle).
rubidium
parents: 7866
diff changeset
   442
	if (!v->IsValid() ||
2544
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
   443
			v->owner != _current_player ||
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   444
			v->type > VEH_SHIP ||
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   445
			_veh_check_replace_proc[v->type - VEH_TRAIN](p, v) == INVALID_ENGINE) {
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
   446
		_players_ai[p->index].state = AIS_VEH_LOOP;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   447
	} 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
   448
		_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
   449
		_players_ai[p->index].state = AIS_VEH_DO_REPLACE_VEHICLE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   450
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   451
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   452
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   453
static void AiStateDoReplaceVehicle(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   454
{
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
   455
	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
   456
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
   457
	_players_ai[p->index].state = AIS_VEH_LOOP;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   458
	// 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
   459
	if (!v->IsValid() || v->owner != _current_player) return;
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   460
	_veh_do_replace_proc[v->type - VEH_TRAIN](p);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   461
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   462
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
   463
struct FoundRoute {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
	int distance;
3344
d86c852715fa (svn r4128) - CodeChange: Add proper semantics for CargoID for such variables instead of using the general byte-type.
Darkvater
parents: 3338
diff changeset
   465
	CargoID cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   466
	void *from;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   467
	void *to;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
   468
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   469
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6432
diff changeset
   470
static Town *AiFindRandomTown()
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1043
diff changeset
   471
{
4356
bc52a48e2590 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
   472
	return GetRandomTown();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   473
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   474
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6432
diff changeset
   475
static Industry *AiFindRandomIndustry()
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1043
diff changeset
   476
{
6640
642fb706f4fa (svn r9251) -Fix(r6057, FS#644): Use a less CPU-intensive algorithm to find a random industry for the AI to prevent it slowing down the game. The AI now sucks a even a bit more than before (if that's even possible).
celestar
parents: 6585
diff changeset
   477
	int num = RandomRange(GetMaxIndustryIndex());
7886
b02aa3532d1d (svn r10759) -Codechange: make the industry struct use the pool item class as super class.
rubidium
parents: 7883
diff changeset
   478
	if (IsValidIndustryID(num)) return GetIndustry(num);
6640
642fb706f4fa (svn r9251) -Fix(r6057, FS#644): Use a less CPU-intensive algorithm to find a random industry for the AI to prevent it slowing down the game. The AI now sucks a even a bit more than before (if that's even possible).
celestar
parents: 6585
diff changeset
   479
642fb706f4fa (svn r9251) -Fix(r6057, FS#644): Use a less CPU-intensive algorithm to find a random industry for the AI to prevent it slowing down the game. The AI now sucks a even a bit more than before (if that's even possible).
celestar
parents: 6585
diff changeset
   480
	return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   481
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   482
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   483
static void AiFindSubsidyIndustryRoute(FoundRoute *fr)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   484
{
959
b031d88c76f3 (svn r1451) Fix some of the signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   485
	uint i;
3344
d86c852715fa (svn r4128) - CodeChange: Add proper semantics for CargoID for such variables instead of using the general byte-type.
Darkvater
parents: 3338
diff changeset
   486
	CargoID cargo;
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   487
	const Subsidy* s;
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   488
	Industry* from;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   489
	TileIndex to_xy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   490
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   491
	// initially error
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   492
	fr->distance = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   493
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
   494
	// Randomize subsidy index..
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   495
	i = RandomRange(lengthof(_subsidies) * 3);
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   496
	if (i >= lengthof(_subsidies)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   497
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   498
	s = &_subsidies[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   499
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   500
	// Don't want passengers or mail
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   501
	cargo = s->cargo_type;
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   502
	if (cargo == CT_INVALID ||
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   503
			cargo == CT_PASSENGERS ||
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   504
			cargo == CT_MAIL ||
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   505
			s->age > 7) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
		return;
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   507
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   508
	fr->cargo = cargo;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   509
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
   510
	fr->from = from = GetIndustry(s->from);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   511
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   512
	if (cargo == CT_GOODS || cargo == CT_FOOD) {
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   513
		Town* to_tow = GetTown(s->to);
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   514
4015
17f39cad5157 (svn r5228) Suppress MSVC warnings about signed/unsigned comparison
KUDr
parents: 4011
diff changeset
   515
		if (to_tow->population < (cargo == CT_FOOD ? 200U : 900U)) return; // error
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   516
		fr->to = to_tow;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   517
		to_xy = to_tow->xy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   518
	} else {
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   519
		Industry* to_ind = GetIndustry(s->to);
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   520
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   521
		fr->to = to_ind;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   522
		to_xy = to_ind->xy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   523
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   524
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1224
diff changeset
   525
	fr->distance = DistanceManhattan(from->xy, to_xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   526
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   527
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   528
static void AiFindSubsidyPassengerRoute(FoundRoute *fr)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   529
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   530
	uint i;
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   531
	const Subsidy* s;
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
   532
	Town *from, *to;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   533
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   534
	// initially error
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   535
	fr->distance = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   536
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
   537
	// Randomize subsidy index..
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   538
	i = RandomRange(lengthof(_subsidies) * 3);
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   539
	if (i >= lengthof(_subsidies)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   541
	s = &_subsidies[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   543
	// Only want passengers
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   544
	if (s->cargo_type != CT_PASSENGERS || s->age > 7) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   545
	fr->cargo = s->cargo_type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   546
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
   547
	fr->from = from = GetTown(s->from);
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
   548
	fr->to = to = GetTown(s->to);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   549
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   550
	// They must be big enough
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   551
	if (from->population < 400 || to->population < 400) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   552
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1224
diff changeset
   553
	fr->distance = DistanceManhattan(from->xy, to->xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   555
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   556
static void AiFindRandomIndustryRoute(FoundRoute *fr)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   557
{
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   558
	Industry* i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   559
	uint32 r;
3344
d86c852715fa (svn r4128) - CodeChange: Add proper semantics for CargoID for such variables instead of using the general byte-type.
Darkvater
parents: 3338
diff changeset
   560
	CargoID cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   561
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   562
	// initially error
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   563
	fr->distance = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   564
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
	r = Random();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   566
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   567
	// pick a source
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
	fr->from = i = AiFindRandomIndustry();
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   569
	if (i == NULL) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   570
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   571
	// 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
   572
	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
   573
	if (r & 1 && i->produced_cargo[1] != CT_INVALID) cargo = i->produced_cargo[1];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   574
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   575
	fr->cargo = cargo;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   576
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
	// don't allow passengers
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   578
	if (cargo == CT_INVALID || cargo == CT_PASSENGERS) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   579
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
	if (cargo != CT_GOODS && cargo != CT_FOOD) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
		// pick a dest, and see if it can receive
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   582
		Industry* i2 = AiFindRandomIndustry();
8141
c052dc0dc029 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7988
diff changeset
   583
		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
   584
				(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
   585
				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
   586
				i2->accepts_cargo[2] != cargo)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   587
			return;
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   588
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   589
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   590
		fr->to = i2;
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1224
diff changeset
   591
		fr->distance = DistanceManhattan(i->xy, i2->xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   592
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   593
		// pick a dest town, and see if it's big enough
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   594
		Town* t = AiFindRandomTown();
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   595
4015
17f39cad5157 (svn r5228) Suppress MSVC warnings about signed/unsigned comparison
KUDr
parents: 4011
diff changeset
   596
		if (t == NULL || t->population < (cargo == CT_FOOD ? 200U : 900U)) return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
   597
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   598
		fr->to = t;
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1224
diff changeset
   599
		fr->distance = DistanceManhattan(i->xy, t->xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   600
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   601
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   602
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   603
static void AiFindRandomPassengerRoute(FoundRoute *fr)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   604
{
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   605
	Town* source;
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   606
	Town* dest;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   607
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   608
	// initially error
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   609
	fr->distance = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   610
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   611
	fr->from = source = AiFindRandomTown();
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   612
	if (source == NULL || source->population < 400) return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
   613
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   614
	fr->to = dest = AiFindRandomTown();
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   615
	if (dest == NULL || source == dest || dest->population < 400) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   616
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1224
diff changeset
   617
	fr->distance = DistanceManhattan(source->xy, dest->xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   618
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   619
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   620
// Warn: depends on 'xy' being the first element in both Town and Industry
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   621
#define GET_TOWN_OR_INDUSTRY_TILE(p) (((Town*)(p))->xy)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   622
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   623
static bool AiCheckIfRouteIsGood(Player *p, FoundRoute *fr, byte bitmask)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   624
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   625
	TileIndex from_tile, to_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   626
	Station *st;
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   627
	int dist;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   628
	uint same_station = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   629
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   630
	from_tile = GET_TOWN_OR_INDUSTRY_TILE(fr->from);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
	to_tile = GET_TOWN_OR_INDUSTRY_TILE(fr->to);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
	dist = 0xFFFF;
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   634
	FOR_ALL_STATIONS(st) {
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   635
		int cur;
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   636
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   637
		if (st->owner != _current_player) continue;
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1224
diff changeset
   638
		cur = DistanceMax(from_tile, st->xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   639
		if (cur < dist) dist = cur;
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1224
diff changeset
   640
		cur = DistanceMax(to_tile, st->xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   641
		if (cur < dist) dist = cur;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   642
		if (to_tile == from_tile && st->xy == to_tile) same_station++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   643
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
   644
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   645
	// To prevent the AI from building ten busstations in the same town, do some calculations
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   646
	//  For each road or airport station, we want 350 of population!
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   647
	if ((bitmask == 2 || bitmask == 4) &&
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   648
			same_station > 2 &&
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   649
			((Town*)fr->from)->population < same_station * 350) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   650
		return false;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   651
	}
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   652
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
   653
	/* 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
   654
	 * 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
   655
	 * 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
   656
	 * 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
   657
	 * 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
   658
	 * 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
   659
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
	/* 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
   661
	 * 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
   662
	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
   663
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
   664
	/* 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
   665
	if (dist != 0xFFFF && dist > min_distance) return false;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   666
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
   667
	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
   668
			!(_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
   669
			!Chance16(1, 5)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   670
		return false;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   671
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   672
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   673
	if (fr->cargo == CT_PASSENGERS || fr->cargo == CT_MAIL) {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   674
		const Town* from = (const Town*)fr->from;
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   675
		const Town* to   = (const Town*)fr->to;
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   676
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   677
		if (from->pct_pass_transported > 0x99 ||
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   678
				to->pct_pass_transported > 0x99) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   679
			return false;
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   680
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   681
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   682
		// Make sure it has a reasonably good rating
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   683
		if (from->ratings[_current_player] < -100 ||
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   684
				to->ratings[_current_player] < -100) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   685
			return false;
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   686
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   687
	} else {
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   688
		const Industry* i = (const Industry*)fr->from;
8141
c052dc0dc029 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7988
diff changeset
   689
c052dc0dc029 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7988
diff changeset
   690
		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
   691
				i->last_month_production[fr->cargo != i->produced_cargo[0]] == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   692
			return false;
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
   693
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   694
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   695
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
   696
	_players_ai[p->index].route_type_mask |= bitmask;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   697
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   698
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   699
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   700
static byte AiGetDirectionBetweenTiles(TileIndex a, TileIndex b)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   701
{
926
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 919
diff changeset
   702
	byte i = (TileX(a) < TileX(b)) ? 1 : 0;
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 919
diff changeset
   703
	if (TileY(a) >= TileY(b)) i ^= 3;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   704
	return i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   705
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   706
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   707
static TileIndex AiGetPctTileBetween(TileIndex a, TileIndex b, byte pct)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   708
{
1981
de031d2aed47 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1977
diff changeset
   709
	return TileXY(
926
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 919
diff changeset
   710
		TileX(a) + ((TileX(b) - TileX(a)) * pct >> 8),
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 919
diff changeset
   711
		TileY(a) + ((TileY(b) - TileY(a)) * pct >> 8)
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 919
diff changeset
   712
	);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   713
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   714
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   715
static void AiWantLongIndustryRoute(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   716
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   717
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   718
	FoundRoute fr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   719
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   720
	i = 60;
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
   721
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   722
		// look for one from the subsidy list
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   723
		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
   724
		if (IsInsideMM(fr.distance, 60, 90 + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   725
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   726
		// try a random one
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   727
		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
   728
		if (IsInsideMM(fr.distance, 60, 90 + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   729
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   730
		// only test 60 times
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   731
		if (--i == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   732
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   733
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   734
	if (!AiCheckIfRouteIsGood(p, &fr, 1)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   735
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   736
	// 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
   737
	_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
   738
	_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
   739
b769d0a63f06 (svn r11792) -Codechange: 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.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
   741
	_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
   742
	_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
   743
	_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
   744
	_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
   745
	_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
   746
	_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
   747
	_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
   748
		_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
   749
		_players_ai[p->index].dst.spec_tile
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   750
	);
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
   751
	_players_ai[p->index].src.cargo = fr.cargo | 0x80;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   752
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   753
	// Fill the dest field
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
   754
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
   755
	_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
   756
	_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
   757
	_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
   758
	_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
   759
	_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
   760
	_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
   761
	_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
   762
	_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
   763
		_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
   764
		_players_ai[p->index].src.spec_tile
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   765
	);
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].dst.cargo = fr.cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   767
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   768
	// 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
   769
	_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
   770
		_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
   771
		_players_ai[p->index].dst.spec_tile,
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   772
		0x55
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   773
	);
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
   774
	_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
   775
	_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
   776
	_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
   777
	_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
   778
	_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
   779
	_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
   780
	_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
   781
	_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
   782
	_players_ai[p->index].mid1.cargo = fr.cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   783
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   784
	// 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
   785
	_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
   786
		_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
   787
		_players_ai[p->index].dst.spec_tile,
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   788
		0xAA
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   789
	);
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
   790
	_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
   791
	_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
   792
	_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
   793
	_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
   794
	_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
   795
	_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
   796
	_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
   797
	_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
   798
	_players_ai[p->index].mid2.cargo = fr.cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   799
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   800
	// 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
   801
	_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
   802
	_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
   803
	_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
   804
	_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
   805
	_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
   806
	_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
   807
	_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
   808
	_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
   809
	_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
   810
	_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
   811
b769d0a63f06 (svn r11792) -Codechange: 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].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
   813
	_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
   814
	_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
   815
	_players_ai[p->index].timeout_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   816
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   817
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   818
static void AiWantMediumIndustryRoute(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   819
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   820
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
	FoundRoute fr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   822
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   823
	i = 60;
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
   824
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   825
		// look for one from the subsidy list
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   826
		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
   827
		if (IsInsideMM(fr.distance, 40, 60 + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   828
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   829
		// try a random one
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   830
		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
   831
		if (IsInsideMM(fr.distance, 40, 60 + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   832
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   833
		// only test 60 times
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   834
		if (--i == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   835
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   836
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   837
	if (!AiCheckIfRouteIsGood(p, &fr, 1)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   838
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   839
	// 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
   840
	_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
   841
	_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
   842
	_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
   843
	_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
   844
	_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
   845
	_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
   846
	_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
   847
	_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
   848
	_players_ai[p->index].src.direction = AiGetDirectionBetweenTiles(
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   849
		GET_TOWN_OR_INDUSTRY_TILE(fr.from),
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   850
		GET_TOWN_OR_INDUSTRY_TILE(fr.to)
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   851
	);
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].src.cargo = fr.cargo | 0x80;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   853
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   854
	// 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
   855
	_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
   856
	_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
   857
	_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
   858
	_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
   859
	_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
   860
	_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
   861
	_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
   862
	_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
   863
	_players_ai[p->index].dst.direction = AiGetDirectionBetweenTiles(
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   864
		GET_TOWN_OR_INDUSTRY_TILE(fr.to),
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   865
		GET_TOWN_OR_INDUSTRY_TILE(fr.from)
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   866
	);
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].dst.cargo = fr.cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   868
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   869
	// 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
   870
	_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
   871
	_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
   872
	_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
   873
	_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
   874
	_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
   875
	_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
   876
	_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
   877
	_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
   878
	_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
   879
	_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
   880
	_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
   881
	_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
   882
	_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
   883
	_players_ai[p->index].timeout_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   884
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   885
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   886
static void AiWantShortIndustryRoute(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   887
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   888
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   889
	FoundRoute fr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   890
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   891
	i = 60;
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
   892
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   893
		// look for one from the subsidy list
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   894
		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
   895
		if (IsInsideMM(fr.distance, 15, 40 + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   896
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   897
		// try a random one
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   898
		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
   899
		if (IsInsideMM(fr.distance, 15, 40 + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   900
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   901
		// only test 60 times
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   902
		if (--i == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   903
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   904
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   905
	if (!AiCheckIfRouteIsGood(p, &fr, 1)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   906
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   907
	// 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
   908
	_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
   909
	_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
   910
	_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
   911
	_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
   912
	_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
   913
	_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
   914
	_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
   915
	_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
   916
	_players_ai[p->index].src.direction = AiGetDirectionBetweenTiles(
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   917
		GET_TOWN_OR_INDUSTRY_TILE(fr.from),
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   918
		GET_TOWN_OR_INDUSTRY_TILE(fr.to)
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   919
	);
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].src.cargo = fr.cargo | 0x80;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   921
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   922
	// 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
   923
	_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
   924
	_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
   925
	_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
   926
	_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
   927
	_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
   928
	_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
   929
	_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
   930
	_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
   931
	_players_ai[p->index].dst.direction = AiGetDirectionBetweenTiles(
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   932
		GET_TOWN_OR_INDUSTRY_TILE(fr.to),
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   933
		GET_TOWN_OR_INDUSTRY_TILE(fr.from)
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   934
	);
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].dst.cargo = fr.cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   936
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   937
	// 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
   938
	_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
   939
	_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
   940
	_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
   941
	_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
   942
	_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
   943
	_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
   944
	_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
   945
	_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
   946
	_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
   947
	_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
   948
	_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
   949
	_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
   950
	_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
   951
	_players_ai[p->index].timeout_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   952
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   953
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   954
static void AiWantMailRoute(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   955
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   956
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   957
	FoundRoute fr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   958
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   959
	i = 60;
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
   960
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   961
		// look for one from the subsidy list
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   962
		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
   963
		if (IsInsideMM(fr.distance, 60, 110 + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   964
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   965
		// try a random one
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   966
		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
   967
		if (IsInsideMM(fr.distance, 60, 110 + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   968
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   969
		// only test 60 times
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   970
		if (--i == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   971
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   972
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   973
	fr.cargo = CT_MAIL;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   974
	if (!AiCheckIfRouteIsGood(p, &fr, 1)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   975
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   976
	// 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
   977
	_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
   978
	_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
   979
	_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
   980
	_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
   981
	_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
   982
	_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
   983
	_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
   984
	_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
   985
	_players_ai[p->index].src.direction = AiGetDirectionBetweenTiles(
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   986
		GET_TOWN_OR_INDUSTRY_TILE(fr.from),
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   987
		GET_TOWN_OR_INDUSTRY_TILE(fr.to)
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
   988
	);
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].src.cargo = fr.cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   990
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   991
	// 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
   992
	_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
   993
	_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
   994
	_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
   995
	_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
   996
	_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
   997
	_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
   998
	_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
   999
	_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
  1000
	_players_ai[p->index].dst.direction = AiGetDirectionBetweenTiles(
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1001
		GET_TOWN_OR_INDUSTRY_TILE(fr.to),
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1002
		GET_TOWN_OR_INDUSTRY_TILE(fr.from)
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1003
	);
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].dst.cargo = fr.cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1005
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1006
	// 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
  1007
	_players_ai[p->index].mid1.spec_tile = AiGetPctTileBetween(
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1008
		GET_TOWN_OR_INDUSTRY_TILE(fr.from),
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1009
		GET_TOWN_OR_INDUSTRY_TILE(fr.to),
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1010
		0x55
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1011
	);
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
  1012
	_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
  1013
	_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
  1014
	_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
  1015
	_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
  1016
	_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
  1017
	_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
  1018
	_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
  1019
	_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
  1020
	_players_ai[p->index].mid1.cargo = fr.cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1021
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1022
	// 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
  1023
	_players_ai[p->index].mid2.spec_tile = AiGetPctTileBetween(
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1024
		GET_TOWN_OR_INDUSTRY_TILE(fr.from),
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1025
		GET_TOWN_OR_INDUSTRY_TILE(fr.to),
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1026
		0xAA
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1027
	);
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
  1028
	_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
  1029
	_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
  1030
	_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
  1031
	_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
  1032
	_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
  1033
	_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
  1034
	_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
  1035
	_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
  1036
	_players_ai[p->index].mid2.cargo = fr.cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1037
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1038
	// 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
  1039
	_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
  1040
	_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
  1041
	_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
  1042
	_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
  1043
	_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
  1044
	_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
  1045
	_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
  1046
	_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
  1047
	_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
  1048
	_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
  1049
	_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
  1050
	_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
  1051
	_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
  1052
	_players_ai[p->index].timeout_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1053
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1054
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1055
static void AiWantPassengerRoute(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1056
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1057
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1058
	FoundRoute fr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1059
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1060
	i = 60;
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
  1061
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1062
		// look for one from the subsidy list
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1063
		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
  1064
		if (IsInsideMM(fr.distance, 0, 55 + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1065
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1066
		// try a random one
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1067
		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
  1068
		if (IsInsideMM(fr.distance, 0, 55 + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1069
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1070
		// only test 60 times
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1071
		if (--i == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1072
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1073
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1074
	fr.cargo = CT_PASSENGERS;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1075
	if (!AiCheckIfRouteIsGood(p, &fr, 1)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1076
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1077
	// 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
  1078
	_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
  1079
	_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
  1080
	_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
  1081
	_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
  1082
	_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
  1083
	_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
  1084
	_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
  1085
	_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
  1086
	_players_ai[p->index].src.direction = AiGetDirectionBetweenTiles(
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1087
		GET_TOWN_OR_INDUSTRY_TILE(fr.from),
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1088
		GET_TOWN_OR_INDUSTRY_TILE(fr.to)
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1089
	);
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].src.cargo = fr.cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1091
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1092
	// 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
  1093
	_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
  1094
	_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
  1095
	_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
  1096
	_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
  1097
	_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
  1098
	_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
  1099
	_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
  1100
	_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
  1101
	_players_ai[p->index].dst.direction = AiGetDirectionBetweenTiles(
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1102
		GET_TOWN_OR_INDUSTRY_TILE(fr.to),
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1103
		GET_TOWN_OR_INDUSTRY_TILE(fr.from)
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1104
	);
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].dst.cargo = fr.cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1106
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1107
	// 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
  1108
	_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
  1109
	_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
  1110
	_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
  1111
	_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
  1112
	_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
  1113
	_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
  1114
	_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
  1115
	_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
  1116
	_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
  1117
	_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
  1118
	_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
  1119
	_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
  1120
	_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
  1121
	_players_ai[p->index].timeout_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1122
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1123
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1124
static void AiWantTrainRoute(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1125
{
2635
88b8b74c01ac (svn r3177) GB, CLRBIT, HASBIT, TOGGLEBIT
tron
parents: 2630
diff changeset
  1126
	uint16 r = GB(Random(), 0, 16);
88b8b74c01ac (svn r3177) GB, CLRBIT, HASBIT, TOGGLEBIT
tron
parents: 2630
diff changeset
  1127
8732
b18f578f7c16 (svn r11800) -Codechange: move some functions to a more logical location + some type safety.
rubidium
parents: 8726
diff changeset
  1128
	_players_ai[p->index].railtype_to_use = GetBestRailtype(p->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  1129
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1130
	if (r > 0xD000) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1131
		AiWantLongIndustryRoute(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1132
	} else if (r > 0x6000) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1133
		AiWantMediumIndustryRoute(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1134
	} else if (r > 0x1000) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1135
		AiWantShortIndustryRoute(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1136
	} else if (r > 0x800) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1137
		AiWantPassengerRoute(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1138
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1139
		AiWantMailRoute(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1140
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1141
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1142
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1143
static void AiWantLongRoadIndustryRoute(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1144
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1145
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1146
	FoundRoute fr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1147
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1148
	i = 60;
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
  1149
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1150
		// look for one from the subsidy list
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1151
		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
  1152
		if (IsInsideMM(fr.distance, 35, 55 + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1153
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1154
		// try a random one
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1155
		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
  1156
		if (IsInsideMM(fr.distance, 35, 55 + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1157
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1158
		// only test 60 times
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1159
		if (--i == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1160
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1161
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1162
	if (!AiCheckIfRouteIsGood(p, &fr, 2)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1163
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1164
	// 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
  1165
	_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
  1166
	_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
  1167
	_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
  1168
	_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
  1169
	_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
  1170
	_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
  1171
	_players_ai[p->index].src.cargo = fr.cargo | 0x80;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1172
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1173
	// 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
  1174
	_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
  1175
	_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
  1176
	_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
  1177
	_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
  1178
	_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
  1179
	_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
  1180
	_players_ai[p->index].dst.cargo = fr.cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1181
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1182
	// 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
  1183
	_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
  1184
	_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
  1185
	_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
  1186
	_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
  1187
b769d0a63f06 (svn r11792) -Codechange: 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].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
  1189
	_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
  1190
	_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
  1191
	_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
  1192
b769d0a63f06 (svn r11792) -Codechange: 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].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
  1194
	_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
  1195
	_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
  1196
	_players_ai[p->index].timeout_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1197
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1198
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1199
static void AiWantMediumRoadIndustryRoute(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1200
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1201
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1202
	FoundRoute fr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1203
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1204
	i = 60;
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
  1205
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1206
		// look for one from the subsidy list
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1207
		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
  1208
		if (IsInsideMM(fr.distance, 15, 40 + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1209
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1210
		// try a random one
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1211
		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
  1212
		if (IsInsideMM(fr.distance, 15, 40 + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1213
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1214
		// only test 60 times
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1215
		if (--i == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1216
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1217
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1218
	if (!AiCheckIfRouteIsGood(p, &fr, 2)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1219
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1220
	// 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
  1221
	_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
  1222
	_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
  1223
	_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
  1224
	_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
  1225
	_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
  1226
	_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
  1227
	_players_ai[p->index].src.cargo = fr.cargo | 0x80;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1228
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1229
	// 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
  1230
	_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
  1231
	_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
  1232
	_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
  1233
	_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
  1234
	_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
  1235
	_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
  1236
	_players_ai[p->index].dst.cargo = fr.cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1237
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1238
	// 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
  1239
	_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
  1240
	_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
  1241
	_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
  1242
	_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
  1243
b769d0a63f06 (svn r11792) -Codechange: 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].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
  1245
	_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
  1246
	_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
  1247
	_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
  1248
b769d0a63f06 (svn r11792) -Codechange: 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].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
  1250
	_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
  1251
	_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
  1252
	_players_ai[p->index].timeout_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1253
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1254
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1255
static void AiWantLongRoadPassengerRoute(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1256
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1257
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1258
	FoundRoute fr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1259
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1260
	i = 60;
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
  1261
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1262
		// look for one from the subsidy list
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1263
		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
  1264
		if (IsInsideMM(fr.distance, 55, 180 + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1265
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1266
		// try a random one
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1267
		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
  1268
		if (IsInsideMM(fr.distance, 55, 180 + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1269
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1270
		// only test 60 times
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1271
		if (--i == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1272
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1273
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1274
	fr.cargo = CT_PASSENGERS;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1275
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1276
	if (!AiCheckIfRouteIsGood(p, &fr, 2)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1277
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1278
	// 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
  1279
	_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
  1280
	_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
  1281
	_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
  1282
	_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
  1283
	_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
  1284
	_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
  1285
	_players_ai[p->index].src.cargo = CT_PASSENGERS;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1286
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1287
	// 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
  1288
	_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
  1289
	_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
  1290
	_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
  1291
	_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
  1292
	_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
  1293
	_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
  1294
	_players_ai[p->index].dst.cargo = CT_PASSENGERS;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1295
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1296
	// 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
  1297
	_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
  1298
	_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
  1299
	_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
  1300
	_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
  1301
b769d0a63f06 (svn r11792) -Codechange: 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].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
  1303
	_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
  1304
	_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
  1305
	_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
  1306
b769d0a63f06 (svn r11792) -Codechange: 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].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
  1308
	_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
  1309
	_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
  1310
	_players_ai[p->index].timeout_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1311
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1312
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1313
static void AiWantPassengerRouteInsideTown(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1314
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1315
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1316
	FoundRoute fr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1317
	Town *t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1318
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1319
	i = 60;
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
  1320
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1321
		// Find a town big enough
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1322
		t = AiFindRandomTown();
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1323
		if (t != NULL && t->population >= 700) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1324
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1325
		// only test 60 times
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1326
		if (--i == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1327
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1328
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1329
	fr.cargo = CT_PASSENGERS;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1330
	fr.from = fr.to = t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1331
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1332
	if (!AiCheckIfRouteIsGood(p, &fr, 2)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1333
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1334
	// 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
  1335
	_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
  1336
	_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
  1337
	_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
  1338
	_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
  1339
	_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
  1340
	_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
  1341
	_players_ai[p->index].src.cargo = CT_PASSENGERS;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1342
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1343
	// 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
  1344
	_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
  1345
	_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
  1346
	_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
  1347
	_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
  1348
	_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
  1349
	_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
  1350
	_players_ai[p->index].dst.cargo = CT_PASSENGERS;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1351
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1352
	// 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
  1353
	_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
  1354
	_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
  1355
	_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
  1356
	_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
  1357
b769d0a63f06 (svn r11792) -Codechange: 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].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
  1359
	_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
  1360
	_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
  1361
	_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
  1362
b769d0a63f06 (svn r11792) -Codechange: 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].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
  1364
	_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
  1365
	_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
  1366
	_players_ai[p->index].timeout_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1367
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1368
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1369
static void AiWantRoadRoute(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1370
{
2635
88b8b74c01ac (svn r3177) GB, CLRBIT, HASBIT, TOGGLEBIT
tron
parents: 2630
diff changeset
  1371
	uint16 r = GB(Random(), 0, 16);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1372
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1373
	if (r > 0x4000) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1374
		AiWantLongRoadIndustryRoute(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1375
	} else if (r > 0x2000) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1376
		AiWantMediumRoadIndustryRoute(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1377
	} else if (r > 0x1000) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1378
		AiWantLongRoadPassengerRoute(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1379
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1380
		AiWantPassengerRouteInsideTown(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1381
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1382
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1383
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1384
static void AiWantPassengerAircraftRoute(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1385
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1386
	FoundRoute fr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1387
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1388
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
  1389
	/* 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
  1390
	 * (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
  1391
	 * 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
  1392
	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
  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
	/* 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
  1395
	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
  1396
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
	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
  1398
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
	/* 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
  1400
	 * 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
  1401
	 * 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
  1402
	 *
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
	 * "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
  1404
	 * 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
  1405
	 * 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
  1406
	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
  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
	/* 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
  1409
	 * 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
  1410
	 *
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
	 * 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
  1412
	 */
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
	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
  1414
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
	/* 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
  1416
	 * 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
  1417
	 * 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
  1418
	 * 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
  1419
	 * 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
  1420
	 * 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
  1421
	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
  1422
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
	/* 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
  1424
	 * 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
  1425
	 * 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
  1426
	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
  1427
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1428
	i = 60;
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
  1429
	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
  1430
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1431
		// look for one from the subsidy list
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1432
		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
  1433
		if (IsInsideMM(fr.distance, min_squares, max_squares + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1434
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1435
		// try a random one
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1436
		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
  1437
		if (IsInsideMM(fr.distance, min_squares, max_squares + 1)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1438
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1439
		// only test 60 times
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1440
		if (--i == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1441
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1442
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1443
	fr.cargo = CT_PASSENGERS;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1444
	if (!AiCheckIfRouteIsGood(p, &fr, 4)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1445
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1446
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1447
	// 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
  1448
	_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
  1449
	_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
  1450
	_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
  1451
	_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
  1452
	_players_ai[p->index].src.cargo = fr.cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1453
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1454
	// 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
  1455
	_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
  1456
	_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
  1457
	_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
  1458
	_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
  1459
	_players_ai[p->index].dst.cargo = fr.cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1460
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1461
	// 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
  1462
	_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
  1463
	_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
  1464
	_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
  1465
	_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
  1466
	/* 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
  1467
	 * 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
  1468
	 * 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
  1469
	 * 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
  1470
	 * 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
  1471
	 * 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
  1472
	 * 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
  1473
	 * 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
  1474
	 * 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
  1475
	 * 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
  1476
	 * 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
  1477
	 */
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
  1478
	_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
  1479
//	_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
  1480
	_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
  1481
	_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
  1482
	_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
  1483
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  1484
	_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
  1485
	_players_ai[p->index].timeout_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1486
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1487
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1488
static void AiWantOilRigAircraftRoute(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1489
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1490
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1491
	FoundRoute fr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1492
	Town *t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1493
	Industry *in;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1494
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1495
	i = 60;
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
  1496
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1497
		// Find a town
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1498
		t = AiFindRandomTown();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1499
		if (t != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1500
			// Find a random oil rig industry
4342
e2c427ed862e (svn r6043) -Codechange: use wrapper if they are available
truelight
parents: 4322
diff changeset
  1501
			in = AiFindRandomIndustry();
6896
b6051812d11f (svn r9530) -Codechange: Replace a direct industry type access by a behaviour
belugas
parents: 6683
diff changeset
  1502
			if (in != NULL && GetIndustrySpec(in->type)->behaviour & INDUSTRYBEH_AI_AIRSHIP_ROUTES) {
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1224
diff changeset
  1503
				if (DistanceManhattan(t->xy, in->xy) < 60)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1504
					break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1505
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1506
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1507
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1508
		// only test 60 times
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1509
		if (--i == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1510
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1511
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1512
	fr.cargo = CT_PASSENGERS;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1513
	fr.from = fr.to = t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1514
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1515
	if (!AiCheckIfRouteIsGood(p, &fr, 4)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1516
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1517
	// 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
  1518
	_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
  1519
	_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
  1520
	_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
  1521
	_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
  1522
	_players_ai[p->index].src.cargo = CT_PASSENGERS;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1523
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1524
	// 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
  1525
	_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
  1526
	_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
  1527
	_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
  1528
	_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
  1529
	_players_ai[p->index].dst.cargo = CT_PASSENGERS;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1530
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1531
	// 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
  1532
	_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
  1533
	_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
  1534
	_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
  1535
	_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
  1536
	_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
  1537
//	_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
  1538
	_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
  1539
	_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
  1540
	_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
  1541
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  1542
	_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
  1543
	_players_ai[p->index].timeout_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1544
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1545
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1546
static void AiWantAircraftRoute(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1547
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1548
	uint16 r = (uint16)Random();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1549
4322
0bee6d9bc452 (svn r5975) -Cleanup: use ORIGINAL_BASE_YEAR & ORIGINAL_MAX_YEAR where the functions really depend on the original date format.
rubidium
parents: 4261
diff changeset
  1550
	if (r >= 0x2AAA || _date < 0x3912 + DAYS_TILL_ORIGINAL_BASE_YEAR) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1551
		AiWantPassengerAircraftRoute(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1552
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1553
		AiWantOilRigAircraftRoute(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1554
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1555
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1556
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1557
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1558
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1559
static void AiStateWantNewRoute(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1560
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1561
	uint16 r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1562
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1563
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1564
	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
  1565
		_players_ai[p->index].state = AIS_0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1566
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1567
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1568
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1569
	i = 200;
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
  1570
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1571
		r = (uint16)Random();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  1572
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1573
		if (_patches.ai_disable_veh_train &&
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1574
				_patches.ai_disable_veh_roadveh &&
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1575
				_patches.ai_disable_veh_aircraft &&
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1576
				_patches.ai_disable_veh_ship) {
76
30511cbc5188 (svn r77) -Fix: [1010788] AI service interval bug (tnx truesatan)
truelight
parents: 21
diff changeset
  1577
			return;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1578
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  1579
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1580
		if (r < 0x7626) {
20
13a47054d81e (svn r21) Remove: PE_BYTE (bit-bools) in favour of PE_BOOL
truelight
parents: 0
diff changeset
  1581
			if (_patches.ai_disable_veh_train) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1582
			AiWantTrainRoute(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1583
		} else if (r < 0xC4EA) {
20
13a47054d81e (svn r21) Remove: PE_BYTE (bit-bools) in favour of PE_BOOL
truelight
parents: 0
diff changeset
  1584
			if (_patches.ai_disable_veh_roadveh) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1585
			AiWantRoadRoute(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1586
		} else if (r < 0xD89B) {
20
13a47054d81e (svn r21) Remove: PE_BYTE (bit-bools) in favour of PE_BOOL
truelight
parents: 0
diff changeset
  1587
			if (_patches.ai_disable_veh_aircraft) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1588
			AiWantAircraftRoute(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1589
		} 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
  1590
			/* Ships are not implemented in this (broken) AI */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1591
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  1592
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1593
		// 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
  1594
		if (_players_ai[p->index].state != AIS_WANT_NEW_ROUTE) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1595
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1596
		// time out?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1597
		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
  1598
			if (++_players_ai[p->index].state_counter == 556) _players_ai[p->index].state = AIS_0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1599
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1600
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1601
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1602
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1603
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1604
static bool AiCheckTrackResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1605
{
9069
7b9573ccc411 (svn r12154) -Codechange: removed a magic number
glx
parents: 9059
diff changeset
  1606
	uint rad = (_patches.modified_catchment) ? CA_TRAIN : CA_UNMODIFIED;
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  1607
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  1608
	for (; p->mode != 4; p++) {
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  1609
		AcceptedCargo values;
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  1610
		TileIndex tile2;
2140
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2096
diff changeset
  1611
		uint w;
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2096
diff changeset
  1612
		uint h;
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2096
diff changeset
  1613
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  1614
		if (p->mode != 1) continue;
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  1615
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  1616
		tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
2140
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2096
diff changeset
  1617
		w = GB(p->attr, 1, 3);
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2096
diff changeset
  1618
		h = GB(p->attr, 4, 3);
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  1619
6432
8fb778a7f2d7 (svn r8841) -Fix
tron
parents: 6367
diff changeset
  1620
		if (p->attr & 1) Swap(w, h);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1621
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1622
		if (cargo & 0x80) {
568
6eb7d394fb35 (svn r979) Allow more realistically sized catchment areas
Celestar
parents: 558
diff changeset
  1623
			GetProductionAroundTiles(values, tile2, w, h, rad);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1624
			return values[cargo & 0x7F] != 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1625
		} else {
568
6eb7d394fb35 (svn r979) Allow more realistically sized catchment areas
Celestar
parents: 558
diff changeset
  1626
			GetAcceptanceAroundTiles(values, tile2, w, h, rad);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1627
			if (!(values[cargo] & ~7))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1628
				return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1629
			if (cargo != CT_MAIL)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1630
				return true;
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  1631
			return !!((values[cargo] >> 1) & ~7);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1632
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1633
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1634
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1635
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1636
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1637
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7362
diff changeset
  1638
static CommandCost AiDoBuildDefaultRailTrack(TileIndex tile, const AiDefaultBlockData* p, RailType railtype, byte flag)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1639
{
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7362
diff changeset
  1640
	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
  1641
	CommandCost total_cost(EXPENSES_CONSTRUCTION);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1642
	Town *t = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1643
	int rating = 0;
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  1644
	int i, j, k;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1645
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
  1646
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1647
		// This will seldomly overflow for valid reasons. Mask it to be on the safe side.
909
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1648
		uint c = TILE_MASK(tile + ToTileIndexDiff(p->tileoffs));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1649
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1650
		_cleared_town = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1651
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1652
		if (p->mode < 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1653
			if (p->mode == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1654
				// Depot
3965
8c857c1da8c2 (svn r5128) Remove the global variable _cur_ai_player
tron
parents: 3964
diff changeset
  1655
				ret = DoCommand(c, railtype, p->attr, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_TRAIN_DEPOT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1656
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1657
				// Station
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  1658
				ret = DoCommand(c, (p->attr & 1) | (p->attr >> 4) << 8 | (p->attr >> 1 & 7) << 16, railtype, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_RAILROAD_STATION);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1659
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1660
1713
d970350410b2 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1640
diff changeset
  1661
			if (CmdFailed(ret)) return CMD_ERROR;
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7442
diff changeset
  1662
			total_cost.AddCost(ret);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1663
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1664
clear_town_stuff:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1665
			if (_cleared_town != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1666
				if (t != NULL && t != _cleared_town)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1667
					return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1668
				t = _cleared_town;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1669
				rating += _cleared_town_rating;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1670
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1671
		} else if (p->mode == 2) {
8502
b71d49f01f5e (svn r11565) -Revert: part of r11564
smatz
parents: 8501
diff changeset
  1672
			/* Rail */
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1673
			if (IsTileType(c, MP_RAILWAY)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1674
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1675
			j = p->attr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1676
			k = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  1677
8502
b71d49f01f5e (svn r11565) -Revert: part of r11564
smatz
parents: 8501
diff changeset
  1678
			/* Build the rail
b71d49f01f5e (svn r11565) -Revert: part of r11564
smatz
parents: 8501
diff changeset
  1679
			 * note: FOR_EACH_SET_BIT cannot be used here
b71d49f01f5e (svn r11565) -Revert: part of r11564
smatz
parents: 8501
diff changeset
  1680
			 */
b71d49f01f5e (svn r11565) -Revert: part of r11564
smatz
parents: 8501
diff changeset
  1681
			for (i = 0; i != 6; i++, j >>= 1) {
b71d49f01f5e (svn r11565) -Revert: part of r11564
smatz
parents: 8501
diff changeset
  1682
				if (j & 1) {
b71d49f01f5e (svn r11565) -Revert: part of r11564
smatz
parents: 8501
diff changeset
  1683
					k = i;
b71d49f01f5e (svn r11565) -Revert: part of r11564
smatz
parents: 8501
diff changeset
  1684
					ret = DoCommand(c, railtype, i, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL);
b71d49f01f5e (svn r11565) -Revert: part of r11564
smatz
parents: 8501
diff changeset
  1685
					if (CmdFailed(ret)) return CMD_ERROR;
b71d49f01f5e (svn r11565) -Revert: part of r11564
smatz
parents: 8501
diff changeset
  1686
					total_cost.AddCost(ret);
b71d49f01f5e (svn r11565) -Revert: part of r11564
smatz
parents: 8501
diff changeset
  1687
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1688
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  1689
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1690
			/* signals too? */
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1691
			if (j & 3) {
5575
e33f975c5a4b (svn r7575) -Codechange: Allow the AI to build signals under bridges (revert r4911)
celestar
parents: 5573
diff changeset
  1692
				// Can't build signals on a road.
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
  1693
				if (IsTileType(c, MP_ROAD)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1694
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1695
				if (flag & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1696
					j = 4 - j;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1697
					do {
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
  1698
						ret = DoCommand(c, k, 0, flag, CMD_BUILD_SIGNALS);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1699
					} while (--j);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1700
				} else {
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7442
diff changeset
  1701
					ret.AddCost(_price.build_signals);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1702
				}
1713
d970350410b2 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1640
diff changeset
  1703
				if (CmdFailed(ret)) return CMD_ERROR;
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7442
diff changeset
  1704
				total_cost.AddCost(ret);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1705
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1706
		} else if (p->mode == 3) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1707
			//Clear stuff and then build single rail.
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3977
diff changeset
  1708
			if (GetTileSlope(c, NULL) != SLOPE_FLAT) return CMD_ERROR;
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
  1709
			ret = DoCommand(c, 0, 0, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_LANDSCAPE_CLEAR);
1713
d970350410b2 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1640
diff changeset
  1710
			if (CmdFailed(ret)) return CMD_ERROR;
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7442
diff changeset
  1711
			total_cost.AddCost(ret);
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7442
diff changeset
  1712
			total_cost.AddCost(_price.build_rail);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1713
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1714
			if (flag & DC_EXEC) {
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  1715
				DoCommand(c, railtype, p->attr & 1, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_SINGLE_RAIL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1716
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1717
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1718
			goto clear_town_stuff;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1719
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1720
			// Unk
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1721
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1722
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  1723
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1724
		p++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1725
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1726
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1727
	if (!(flag & DC_EXEC)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1728
		if (t != NULL && rating > t->ratings[_current_player]) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1729
			return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1730
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1731
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1732
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1733
	return total_cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1734
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1735
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1736
// Returns rule and cost
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7362
diff changeset
  1737
static int AiBuildDefaultRailTrack(TileIndex tile, byte p0, byte p1, byte p2, byte p3, byte dir, byte cargo, RailType railtype, CommandCost* cost)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1738
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1739
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1740
	const AiDefaultRailBlock *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1741
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1742
	for (i = 0; (p = _default_rail_track_data[i]) != NULL; i++) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  1743
		if (p->p0 == p0 && p->p1 == p1 && p->p2 == p2 && p->p3 == p3 &&
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1744
				(p->dir == 0xFF || p->dir == dir || ((p->dir - 1) & 3) == dir)) {
3965
8c857c1da8c2 (svn r5128) Remove the global variable _cur_ai_player
tron
parents: 3964
diff changeset
  1745
			*cost = AiDoBuildDefaultRailTrack(tile, p->data, railtype, DC_NO_TOWN_RATING);
7442
a4f7989f27d4 (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 7439
diff changeset
  1746
			if (CmdSucceeded(*cost) && AiCheckTrackResources(tile, p->data, cargo))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1747
				return i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1748
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1749
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1750
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1751
	return -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1752
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1753
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1754
static const byte _terraform_up_flags[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1755
	14, 13, 12, 11,
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1756
	10,  9,  8,  7,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1757
	 6,  5,  4,  3,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1758
	 2,  1,  0,  1,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1759
	 2,  1,  4,  1,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1760
	 2,  1,  8,  1,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1761
	 2,  1,  4,  2,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1762
	 2,  1
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1763
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1764
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1765
static const byte _terraform_down_flags[] = {
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1766
	1,  2, 3,  4,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1767
	5,  6, 1,  8,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1768
	9, 10, 8, 12,
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1769
	4,  2, 0,  0,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1770
	1,  2, 3,  4,
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1771
	5,  6, 2,  8,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1772
	9, 10, 1, 12,
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1773
	8,  4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1774
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1775
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  1776
static void AiDoTerraformLand(TileIndex tile, DiagDirection dir, int unk, int mode)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1777
{
2544
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
  1778
	PlayerID old_player;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1779
	uint32 r;
3644
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3609
diff changeset
  1780
	Slope slope;
1335
a635854c23b6 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1330
diff changeset
  1781
	uint h;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1782
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1783
	old_player = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1784
	_current_player = OWNER_NONE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1785
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1786
	r = Random();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1787
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1788
	unk &= (int)r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1789
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1790
	do {
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4527
diff changeset
  1791
		tile = TILE_MASK(tile + TileOffsByDiagDir(dir));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1792
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1793
		r >>= 2;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1794
		if (r & 2) {
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  1795
			dir = ChangeDiagDir(dir, (r & 1) ? DIAGDIRDIFF_90LEFT : DIAGDIRDIFF_90RIGHT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1796
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1797
	} while (--unk >= 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1798
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1799
	slope = GetTileSlope(tile, &h);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  1800
3644
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3609
diff changeset
  1801
	if (slope != SLOPE_FLAT) {
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1802
		if (mode > 0 || (mode == 0 && !(r & 0xC))) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1803
			// Terraform up
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
  1804
			DoCommand(tile, _terraform_up_flags[slope - 1], 1,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1805
				DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1806
		} else if (h != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1807
			// Terraform down
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
  1808
			DoCommand(tile, _terraform_down_flags[slope - 1], 0,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1809
				DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  1810
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1811
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1812
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1813
	_current_player = old_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1814
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1815
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1816
static void AiStateBuildDefaultRailBlocks(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1817
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1818
	uint i;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1819
	int j;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1820
	AiBuildRec *aib;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1821
	int rule;
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7362
diff changeset
  1822
	CommandCost cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1823
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1824
	// 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
  1825
	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
  1826
		_players_ai[p->index].state = AIS_DELETE_RAIL_BLOCKS;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1827
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1828
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1829
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1830
	// do the following 8 times
2648
72973ab76048 (svn r3190) Turn some loops into canonical for loop form
tron
parents: 2643
diff changeset
  1831
	for (i = 0; i < 8; i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1832
		// 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
  1833
		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
  1834
		j = _players_ai[p->index].num_build_rec;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1835
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1836
			// this item has already been built?
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1837
			if (aib->cur_building_rule != 255) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1838
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1839
			// adjust the coordinate randomly,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1840
			// to make sure that we find a position.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1841
			aib->use_tile = AdjustTileCoordRandomly(aib->spec_tile, aib->rand_rng);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1842
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1843
			// check if the track can be build there.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1844
			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
  1845
				_players_ai[p->index].build_kind, _players_ai[p->index].num_wagons,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1846
				aib->unk6, aib->unk7,
3965
8c857c1da8c2 (svn r5128) Remove the global variable _cur_ai_player
tron
parents: 3964
diff changeset
  1847
				aib->direction, aib->cargo,
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
  1848
				_players_ai[p->index].railtype_to_use,
3965
8c857c1da8c2 (svn r5128) Remove the global variable _cur_ai_player
tron
parents: 3964
diff changeset
  1849
				&cost
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  1850
			);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1851
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1852
			if (rule == -1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1853
				// 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
  1854
				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
  1855
					AiDoTerraformLand(aib->use_tile, (DiagDirection)(Random() & 3), 3, (int8)_players_ai[p->index].state_mode);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1856
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1857
				// 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
  1858
				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
  1859
					_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
  1860
					_players_ai[p->index].state_mode = -_players_ai[p->index].state_mode;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1861
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1862
			} else if (CheckPlayerHasMoney(cost)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1863
				// player has money, build it.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1864
				aib->cur_building_rule = rule;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1865
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
  1866
				AiDoBuildDefaultRailTrack(
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1867
					aib->use_tile,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1868
					_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
  1869
					_players_ai[p->index].railtype_to_use,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1870
					DC_EXEC | DC_NO_TOWN_RATING
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1871
				);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1872
			}
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  1873
		} while (++aib, --j);
2648
72973ab76048 (svn r3190) Turn some loops into canonical for loop form
tron
parents: 2643
diff changeset
  1874
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1875
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1876
	// 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
  1877
	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
  1878
	j = _players_ai[p->index].num_build_rec;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1879
	do {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1880
		if (aib->cur_building_rule == 255) return;
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  1881
	} while (++aib, --j);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1882
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1883
	// 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
  1884
	_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
  1885
	_players_ai[p->index].state_mode = 255;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1886
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1887
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  1888
static TileIndex AiGetEdgeOfDefaultRailBlock(byte rule, TileIndex tile, byte cmd, DiagDirection *dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1889
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1890
	const AiDefaultBlockData *p = _default_rail_track_data[rule]->data;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1891
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1892
	while (p->mode != 3 || !((--cmd) & 0x80)) p++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1893
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4527
diff changeset
  1894
	return tile + ToTileIndexDiff(p->tileoffs) - TileOffsByDiagDir(*dir = p->attr);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1895
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1896
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1897
struct AiRailPathFindData {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1898
	TileIndex tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1899
	TileIndex tile2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1900
	int count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1901
	bool flag;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1902
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1903
9107
3e57b96e98f2 (svn r12193) -Codechange: Rename a magic variable, give it a decent type, and remove a 'goto'.
frosch
parents: 9098
diff changeset
  1904
static bool AiEnumFollowTrack(TileIndex tile, AiRailPathFindData *a, int track, uint length)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1905
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1906
	if (a->flag) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1907
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1908
	if (length > 20 || tile == a->tile) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1909
		a->flag = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1910
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1911
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1912
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1913
	if (DistanceMax(tile, a->tile2) < 4) a->count++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1914
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1915
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1916
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1917
2643
735ef1e7884b (svn r3185) const
tron
parents: 2639
diff changeset
  1918
static bool AiDoFollowTrack(const Player* p)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1919
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1920
	AiRailPathFindData arpfd;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1921
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
  1922
	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
  1923
	arpfd.tile2 = _players_ai[p->index].cur_tile_a;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1924
	arpfd.flag = false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1925
	arpfd.count = 0;
8888
94cc8a25185f (svn r11962) -Cleanup: OPF is no longer used to update signals
smatz
parents: 8886
diff changeset
  1926
	FollowTrack(_players_ai[p->index].cur_tile_a + TileOffsByDiagDir(_players_ai[p->index].cur_dir_a), TRANSPORT_RAIL, 0, ReverseDiagDir(_players_ai[p->index].cur_dir_a),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1927
		(TPFEnumProc*)AiEnumFollowTrack, NULL, &arpfd);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1928
	return arpfd.count > 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1929
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1930
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1931
struct AiRailFinder {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1932
	TileIndex final_tile;
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  1933
	DiagDirection final_dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1934
	byte depth;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1935
	byte recursive_mode;
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  1936
	DiagDirection cur_best_dir;
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  1937
	DiagDirection best_dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1938
	byte cur_best_depth;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1939
	byte best_depth;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1940
	uint cur_best_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1941
	const byte *best_ptr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1942
	uint best_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1943
	TileIndex cur_best_tile, best_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1944
	TileIndex bridge_end_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1945
	Player *player;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1946
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1947
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1948
static const byte _ai_table_15[4][8] = {
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1949
	{0, 0, 4, 3, 3, 1, 128 + 0, 64},
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1950
	{1, 1, 2, 0, 4, 2, 128 + 1, 65},
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1951
	{0, 2, 2, 3, 5, 1, 128 + 2, 66},
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1952
	{1, 3, 5, 0, 3, 2, 128 + 3, 67}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1953
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1954
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
  1955
static const byte _dir_table_1[] = { 3, 9, 12, 6};
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4342
diff changeset
  1956
static const byte _dir_table_2[] = {12, 6,  3, 9};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1957
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1958
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2551
diff changeset
  1959
static bool AiIsTileBanned(const Player* p, TileIndex tile, byte val)
35249d2ded3e (svn r3172) static, const
tron
parents: 2551
diff changeset
  1960
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1961
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1962
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
  1963
	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
  1964
		if (_players_ai[p->index].banned_tiles[i] == tile && _players_ai[p->index].banned_val[i] == val) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1965
			return true;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1966
		}
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1967
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1968
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1969
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1970
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1971
static void AiBanTile(Player* p, TileIndex tile, byte val)
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1972
{
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  1973
	uint i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1974
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
  1975
	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
  1976
		_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
  1977
		_players_ai[p->index].banned_val[i] = _players_ai[p->index].banned_val[i - 1];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1978
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1979
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
  1980
	_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
  1981
	_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
  1982
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  1983
	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
  1984
		_players_ai[p->index].banned_tile_count++;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1985
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1986
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1987
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  1988
static void AiBuildRailRecursive(AiRailFinder *arf, TileIndex tile, DiagDirection dir);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1989
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1990
static bool AiCheckRailPathBetter(AiRailFinder *arf, const byte *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1991
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1992
	bool better = false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1993
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1994
	if (arf->recursive_mode < 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1995
		// Mode is 0. This means destination has not been found yet.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1996
		// If the found path is shorter than the current one, remember it.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1997
		if (arf->cur_best_dist < arf->best_dist) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1998
			arf->best_dir = arf->cur_best_dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1999
			arf->best_dist = arf->cur_best_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2000
			arf->best_ptr = p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2001
			arf->best_tile = arf->cur_best_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2002
			better = true;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  2003
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2004
	} else if (arf->recursive_mode > 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2005
		// Mode is 2.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2006
		if (arf->best_dist != 0 || arf->cur_best_depth < arf->best_depth) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2007
			arf->best_depth = arf->cur_best_depth;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2008
			arf->best_dist = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2009
			arf->best_ptr = p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2010
			arf->best_tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2011
			better = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2012
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2013
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2014
	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
  2015
	arf->cur_best_dist = UINT_MAX;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2016
	arf->cur_best_depth = 0xff;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2017
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2018
	return better;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2019
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2020
536
aef4753985d3 (svn r907) Sprinkle holy ANSI water:
tron
parents: 460
diff changeset
  2021
static inline void AiCheckBuildRailBridgeHere(AiRailFinder *arf, TileIndex tile, const byte *p)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2022
{
3644
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3609
diff changeset
  2023
	Slope tileh;
3088
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2024
	uint z;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2025
	bool flag;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2026
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  2027
	DiagDirection dir2 = (DiagDirection)(p[0] & 3);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  2028
3088
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2029
	tileh = GetTileSlope(tile, &z);
3644
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3609
diff changeset
  2030
	if (tileh == _dir_table_1[dir2] || (tileh == SLOPE_FLAT && z != 0)) {
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3977
diff changeset
  2031
		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
  2032
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2033
		// Allow bridges directly over bottom tiles
3088
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2034
		flag = z == 0;
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
  2035
		for (;;) {
3088
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2036
			TileType type;
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2037
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4527
diff changeset
  2038
			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
  2039
			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
  2040
			type = GetTileType(tile_new);
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2041
3644
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3609
diff changeset
  2042
			if (type == MP_CLEAR || type == MP_TREES || GetTileSlope(tile_new, NULL) != SLOPE_FLAT) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2043
				if (!flag) return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2044
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2045
			}
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
  2046
			if (type != MP_WATER && type != MP_RAILWAY && type != MP_ROAD) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2047
			flag = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2048
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2049
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2050
		// 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
  2051
		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
  2052
			return;
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2053
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2054
		AiBuildRailRecursive(arf, tile_new, dir2);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  2055
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2056
		// At the bottom depth, check if the new path is better than the old one.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2057
		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
  2058
			if (AiCheckRailPathBetter(arf, p)) arf->bridge_end_tile = tile_new;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2059
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2060
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2061
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2062
536
aef4753985d3 (svn r907) Sprinkle holy ANSI water:
tron
parents: 460
diff changeset
  2063
static inline void AiCheckBuildRailTunnelHere(AiRailFinder *arf, TileIndex tile, const byte *p)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2064
{
3088
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2065
	uint z;
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2066
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2067
	if (GetTileSlope(tile, &z) == _dir_table_2[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
  2068
		CommandCost cost = DoCommand(tile, _players_ai[arf->player->index].railtype_to_use, 0, DC_AUTO, CMD_BUILD_TUNNEL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2069
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7442
diff changeset
  2070
		if (CmdSucceeded(cost) && cost.GetCost() <= (arf->player->player_money >> 4)) {
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  2071
			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
  2072
			if (arf->depth == 1) AiCheckRailPathBetter(arf, p);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2073
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2074
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2075
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2076
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2077
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  2078
static void AiBuildRailRecursive(AiRailFinder *arf, TileIndex tile, DiagDirection dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2079
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2080
	const byte *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2081
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4527
diff changeset
  2082
	tile = TILE_MASK(tile + TileOffsByDiagDir(dir));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  2083
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2084
	// Reached destination?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2085
	if (tile == arf->final_tile) {
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  2086
		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
  2087
			if (arf->recursive_mode != 2) arf->recursive_mode = 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2088
		} else if (arf->recursive_mode != 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2089
			arf->recursive_mode = 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2090
			arf->cur_best_depth = arf->depth;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2091
		} else {
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2092
			if (arf->depth < arf->cur_best_depth) arf->cur_best_depth = arf->depth;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2093
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2094
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2095
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2096
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2097
	// Depth too deep?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2098
	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
  2099
		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
  2100
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2101
		if (dist < arf->cur_best_dist) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2102
			// Store the tile that is closest to the final position.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2103
			arf->cur_best_depth = arf->depth;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2104
			arf->cur_best_dist = dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2105
			arf->cur_best_tile = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2106
			arf->cur_best_dir = dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2107
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2108
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2109
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2110
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2111
	// Increase recursion depth
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2112
	arf->depth++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2113
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2114
	// Grab pointer to list of stuff that is possible to build
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2115
	p = _ai_table_15[dir];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2116
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2117
	// 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
  2118
	if (GetTileZ(tile) == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2119
		p += 6;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2120
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2121
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2122
			// Make sure the tile is not in the list of banned tiles and that a rail can be built here.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2123
			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
  2124
					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
  2125
				AiBuildRailRecursive(arf, tile, (DiagDirection)p[1]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2126
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2127
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2128
			// 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
  2129
			if (arf->depth == 1) AiCheckRailPathBetter(arf, p);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2130
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2131
			p += 2;
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  2132
		} while (!(p[0] & 0x80));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2133
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2134
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2135
	AiCheckBuildRailBridgeHere(arf, tile, p);
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  2136
	AiCheckBuildRailTunnelHere(arf, tile, p + 1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2137
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2138
	arf->depth--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2139
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2140
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2141
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  2142
static const byte _dir_table_3[] = {0x25, 0x2A, 0x19, 0x16};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2143
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2144
static void AiBuildRailConstruct(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2145
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2146
	AiRailFinder arf;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2147
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2148
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2149
	// Check too much lookahead?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2150
	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
  2151
		_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
  2152
		_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
  2153
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2154
		// 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
  2155
		AiBanTile(p, _players_ai[p->index].cur_tile_a, FindFirstBit(GetRailTrackStatus(_players_ai[p->index].cur_tile_a)));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2156
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2157
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2158
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2159
	// Setup recursive finder and call it.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2160
	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
  2161
	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
  2162
	arf.final_dir = _players_ai[p->index].cur_dir_b;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2163
	arf.depth = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2164
	arf.recursive_mode = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2165
	arf.best_ptr = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2166
	arf.cur_best_dist = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2167
	arf.cur_best_depth = 0xff;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2168
	arf.best_dist = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2169
	arf.best_depth = 0xff;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2170
	arf.cur_best_tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2171
	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
  2172
	AiBuildRailRecursive(&arf, _players_ai[p->index].cur_tile_a, _players_ai[p->index].cur_dir_a);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2173
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2174
	// Reached destination?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2175
	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
  2176
		_players_ai[p->index].state_mode = 255;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2177
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2178
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2179
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2180
	// Didn't find anything to build?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2181
	if (arf.best_ptr == NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2182
		// 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
  2183
		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
  2184
			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
  2185
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2186
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
  2187
		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
  2188
			_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
  2189
			_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
  2190
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2191
			// 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
  2192
			AiBanTile(p, _players_ai[p->index].cur_tile_a, FindFirstBit(GetRailTrackStatus(_players_ai[p->index].cur_tile_a)));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2193
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2194
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2195
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  2196
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
  2197
	_players_ai[p->index].cur_tile_a += TileOffsByDiagDir(_players_ai[p->index].cur_dir_a);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2198
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2199
	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
  2200
		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
  2201
		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
  2202
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
		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
  2204
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
		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
  2206
		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
  2207
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
		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
  2209
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
		/* 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
  2211
		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
  2212
		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
  2213
		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
  2214
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
		/* 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
  2216
		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
  2217
			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
  2218
			/* 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
  2219
			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
  2220
				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
  2221
				break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2222
			}
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
  2223
			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
  2224
		} 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
  2225
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
		/* 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
  2227
		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
  2228
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
		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
  2230
			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
  2231
		} 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
  2232
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
			/* 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
  2234
			 * 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
  2235
			 * 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
  2236
			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
  2237
			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
  2238
				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
  2239
					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
  2240
					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
  2241
				}
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
  2242
			}
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
  2243
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
  2244
			/* 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
  2245
			DoCommand(t1, t2, i | (_players_ai[p->index].railtype_to_use << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2246
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2247
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
  2248
		_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
  2249
		_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
  2250
	} else if (arf.best_ptr[0] & 0x40) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2251
		// 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
  2252
		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
  2253
		_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
  2254
		_players_ai[p->index].state_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2255
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2256
		// 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
  2257
		_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
  2258
		DoCommand(_players_ai[p->index].cur_tile_a, _players_ai[p->index].railtype_to_use, arf.best_ptr[0],
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2259
			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
  2260
		_players_ai[p->index].state_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2261
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2262
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2263
	if (arf.best_tile != 0) {
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2264
		for (i = 0; i != 2; i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2265
			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
  2266
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2267
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2268
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2269
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2270
static bool AiRemoveTileAndGoForward(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2271
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2272
	byte b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2273
	int bit;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2274
	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
  2275
	TileIndex tile = _players_ai[p->index].cur_tile_a;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2276
	TileIndex tilenew;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2277
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
  2278
	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
  2279
		if (IsTunnel(tile)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2280
			// 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
  2281
			if (CmdFailed(DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2282
				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
  2283
			_players_ai[p->index].cur_tile_a = TILE_MASK(_build_tunnel_endtile - TileOffsByDiagDir(_players_ai[p->index].cur_dir_a));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2284
			return true;
8886
9f2c7ebc7fc9 (svn r11960) -Cleanup: simplify some IsTunnel(Tile) / IsBridge(Tile) conditions
smatz
parents: 8885
diff changeset
  2285
		} else { // IsBridge(tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2286
			// Check if the bridge points in the right direction.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2287
			// 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
  2288
			if (DiagDirToAxis(GetTunnelBridgeDirection(tile)) != (_players_ai[p->index].cur_dir_a & 1)) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2289
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
  2290
			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
  2291
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
  2292
			tilenew = TILE_MASK(tile - TileOffsByDiagDir(_players_ai[p->index].cur_dir_a));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2293
			// 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
  2294
			if (CmdFailed(DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2295
				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
  2296
			_players_ai[p->index].cur_tile_a = tilenew;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2297
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2298
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2299
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2300
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2301
	// Find the railtype at the position. Quit if no rail 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
  2302
	b = GetRailTrackStatus(tile) & _dir_table_3[_players_ai[p->index].cur_dir_a];
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2303
	if (b == 0) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2304
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2305
	// Convert into a bit position that CMD_REMOVE_SINGLE_RAIL expects.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2306
	bit = FindFirstBit(b);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2307
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2308
	// 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
  2309
	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
  2310
			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
  2311
		DoCommand(tile, 0, 0, DC_EXEC, CMD_REMOVE_SIGNALS);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2312
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  2313
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2314
	// And also remove the rail.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
  2315
	if (CmdFailed(DoCommand(tile, 0, bit, DC_EXEC, CMD_REMOVE_SINGLE_RAIL)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2316
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2317
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2318
	// 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
  2319
	ptr = _ai_table_15[ReverseDiagDir(_players_ai[p->index].cur_dir_a)];
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2320
	while (ptr[0] != bit) 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
  2321
	_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
  2322
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2323
	// 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
  2324
	_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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2325
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2326
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2327
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2328
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2329
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2330
static void AiBuildRailDestruct(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2331
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2332
	// 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
  2333
	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
  2334
		_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
  2335
		_players_ai[p->index].state_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2336
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2337
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2338
	// 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
  2339
	if (_players_ai[p->index].cur_tile_a == _players_ai[p->index].start_tile_a) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2340
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  2341
	AiRemoveTileAndGoForward(p);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2342
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2343
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2344
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2345
static void AiBuildRail(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2346
{
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
  2347
	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
  2348
		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
  2349
			AiBuildRailConstruct(p);
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2350
			break;
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2351
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2352
		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
  2353
			AiBuildRailDestruct(p);
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2354
			break;
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2355
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2356
		case 2: {
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2357
			uint i;
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2358
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2359
			// 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
  2360
			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
  2361
				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
  2362
			}
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2363
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
  2364
			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
  2365
				_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
  2366
				_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
  2367
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2368
		}
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2369
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2370
		default: break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2371
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2372
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2373
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2374
static void AiStateBuildRail(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2375
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2376
	int num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2377
	AiBuildRec *aib;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2378
	byte cmd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2379
	TileIndex tile;
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  2380
	DiagDirection dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2381
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2382
	// 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
  2383
	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
  2384
		_players_ai[p->index].state = AIS_DELETE_RAIL_BLOCKS;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2385
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2386
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2387
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2388
	// 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
  2389
	if (_players_ai[p->index].state_mode != 255) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2390
		AiBuildRail(p);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  2391
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2392
		// 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
  2393
		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
  2394
		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
  2395
		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
  2396
		Swap(_players_ai[p->index].cur_dir_a,    _players_ai[p->index].cur_dir_b);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2397
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2398
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2399
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2400
	// 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
  2401
	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
  2402
	aib = &_players_ai[p->index].src;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2403
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
  2404
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2405
		cmd = aib->buildcmd_a;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2406
		aib->buildcmd_a = 255;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2407
		if (cmd != 255) break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2408
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2409
		cmd = aib->buildcmd_b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2410
		aib->buildcmd_b = 255;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2411
		if (cmd != 255) break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2412
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2413
		aib++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2414
		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
  2415
			_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
  2416
			_players_ai[p->index].state_counter = 0; // timeout
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2417
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2418
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2419
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2420
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2421
	// Find first edge to build from.
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  2422
	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
  2423
	_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
  2424
	_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
  2425
	_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
  2426
	_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
  2427
	DoCommand(TILE_MASK(tile + TileOffsByDiagDir(dir)), 0, (dir & 1) ? 1 : 0, DC_EXEC, CMD_REMOVE_SINGLE_RAIL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2428
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2429
	assert(TILE_MASK(tile) != 0xFF00);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2430
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2431
	// 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
  2432
	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
  2433
	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
  2434
	_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
  2435
	_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
  2436
	_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
  2437
	_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
  2438
	DoCommand(TILE_MASK(tile + TileOffsByDiagDir(dir)), 0, (dir & 1) ? 1 : 0, DC_EXEC, CMD_REMOVE_SINGLE_RAIL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2439
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2440
	assert(TILE_MASK(tile) != 0xFF00);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2441
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2442
	// 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
  2443
	_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
  2444
	_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
  2445
	_players_ai[p->index].banned_tile_count = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2446
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2447
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
  2448
static StationID AiGetStationIdByDef(TileIndex tile, int id)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2449
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2450
	const AiDefaultBlockData *p = _default_rail_track_data[id]->data;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2451
	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
  2452
	return GetStationIndex(TILE_ADD(tile, ToTileIndexDiff(p->tileoffs)));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2453
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2454
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
  2455
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
  2456
{
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
  2457
	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
  2458
	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
  2459
	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
  2460
	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
  2461
	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
  2462
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3348
diff changeset
  2463
	for (i = 0; i < NUM_TRAIN_ENGINES; i++) {
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3348
diff changeset
  2464
		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
  2465
		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
  2466
6074
e70d63ef4d62 (svn r8385) -Fix
tron
parents: 5984
diff changeset
  2467
		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
  2468
				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
  2469
				!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
  2470
			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
  2471
		}
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
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
  2473
		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
  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
		/* 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
  2476
		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
  2477
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3348
diff changeset
  2478
		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
  2479
			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
  2480
			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
  2481
			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
  2482
		}
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
  2483
	}
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
  2484
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
  2485
	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
  2486
}
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
  2487
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2488
static void AiStateBuildRailVeh(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2489
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2490
	const AiDefaultBlockData *ptr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2491
	TileIndex tile;
2544
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
  2492
	EngineID veh;
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
  2493
	int i;
3348
85c0eb15ad87 (svn r4132) - Missed 3 CargoID's
Darkvater
parents: 3344
diff changeset
  2494
	CargoID cargo;
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7362
diff changeset
  2495
	CommandCost cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2496
	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
  2497
	VehicleID loco_id;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2498
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
  2499
	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
  2500
	while (ptr->mode != 0) ptr++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2501
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
  2502
	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
  2503
b769d0a63f06 (svn r11792) -Codechange: 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
b769d0a63f06 (svn r11792) -Codechange: 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
	cargo = _players_ai[p->index].cargo_type;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2506
	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
  2507
		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
  2508
			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
  2509
			/* 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
  2510
			 * 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
  2511
			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
  2512
			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
  2513
			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
  2514
			_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
  2515
			_players_ai[p->index].wagon_list[i + 1] = INVALID_VEHICLE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2516
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2517
		}
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
  2518
		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
  2519
		if (++i == _players_ai[p->index].num_wagons * 2 - 1) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2520
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2521
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2522
	// 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
  2523
	veh = AiChooseTrainToBuild(_players_ai[p->index].railtype_to_use, p->player_money, cargo != CT_PASSENGERS ? 1 : 0, tile);
2544
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
  2524
	if (veh == INVALID_ENGINE) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2525
handle_nocash:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2526
		// 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
  2527
		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
  2528
			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
  2529
				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
  2530
				assert(CmdSucceeded(cost));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2531
			}
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
  2532
			_players_ai[p->index].state = AIS_0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2533
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2534
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2535
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2536
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2537
	// 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
  2538
	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
  2539
	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
  2540
	loco_id = _new_vehicle_id;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2541
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2542
	// 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
  2543
	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
  2544
	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
  2545
		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
  2546
		_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
  2547
		DoCommand(tile, i, 0, DC_EXEC, CMD_SELL_RAIL_WAGON);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2548
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2549
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2550
	// 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
  2551
	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
  2552
		DoCommand(tile, _players_ai[p->index].wagon_list[i] | (loco_id << 16), 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2553
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2554
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
  2555
	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
  2556
		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
  2557
		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
  2558
			_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
  2559
			_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
  2560
			(_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
  2561
		);
558
9b115b39c515 (svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents: 555
diff changeset
  2562
		Order order;
9b115b39c515 (svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents: 555
diff changeset
  2563
9b115b39c515 (svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents: 555
diff changeset
  2564
		order.type = OT_GOTO_STATION;
9b115b39c515 (svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents: 555
diff changeset
  2565
		order.flags = 0;
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4495
diff changeset
  2566
		order.dest = AiGetStationIdByDef(aib->use_tile, aib->cur_building_rule);
558
9b115b39c515 (svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents: 555
diff changeset
  2567
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8766
diff changeset
  2568
		if (!is_pass && i == 1) order.flags |= OFB_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
  2569
		if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8766
diff changeset
  2570
			order.flags |= OFB_FULL_LOAD;
558
9b115b39c515 (svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents: 555
diff changeset
  2571
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
  2572
		DoCommand(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2573
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2574
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
  2575
	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
  2576
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
  2577
	DoCommand(0, loco_id, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  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
	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
  2580
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  2581
	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
  2582
//		_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
  2583
		_players_ai[p->index].wagon_list[0] = INVALID_VEHICLE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2584
	} 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
  2585
		_players_ai[p->index].state = AIS_0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2586
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2587
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2588
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2589
static void AiStateDeleteRailBlocks(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  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
	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
  2592
	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
  2593
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2594
	do {
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2595
		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
  2596
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2597
		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
  2598
		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
  2599
			DoCommand(TILE_ADD(aib->use_tile, ToTileIndexDiff(b->tileoffs)), 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2600
		}
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  2601
	} while (++aib, --num);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2602
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
  2603
	_players_ai[p->index].state = AIS_0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2604
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2605
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2606
static bool AiCheckRoadResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2607
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2608
	uint values[NUM_CARGO];
568
6eb7d394fb35 (svn r979) Allow more realistically sized catchment areas
Celestar
parents: 558
diff changeset
  2609
	int rad;
830
8114845804c9 (svn r1301) -Codechange: _industries finally has FOR_ALL_INDUSTRIES too
truelight
parents: 679
diff changeset
  2610
568
6eb7d394fb35 (svn r979) Allow more realistically sized catchment areas
Celestar
parents: 558
diff changeset
  2611
	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
  2612
		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
  2613
	} else { // change that at some point?
568
6eb7d394fb35 (svn r979) Allow more realistically sized catchment areas
Celestar
parents: 558
diff changeset
  2614
		rad = 4;
6eb7d394fb35 (svn r979) Allow more realistically sized catchment areas
Celestar
parents: 558
diff changeset
  2615
	}
830
8114845804c9 (svn r1301) -Codechange: _industries finally has FOR_ALL_INDUSTRIES too
truelight
parents: 679
diff changeset
  2616
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2617
	for (;; p++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2618
		if (p->mode == 4) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2619
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2620
		} else if (p->mode == 1) {
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1927
diff changeset
  2621
			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
  2622
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2623
			if (cargo & 0x80) {
568
6eb7d394fb35 (svn r979) Allow more realistically sized catchment areas
Celestar
parents: 558
diff changeset
  2624
				GetProductionAroundTiles(values, tile2, 1, 1, rad);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2625
				return values[cargo & 0x7F] != 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2626
			} else {
568
6eb7d394fb35 (svn r979) Allow more realistically sized catchment areas
Celestar
parents: 558
diff changeset
  2627
				GetAcceptanceAroundTiles(values, tile2, 1, 1, rad);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2628
				return (values[cargo]&~7) != 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2629
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2630
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2631
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2632
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2633
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2634
static bool _want_road_truck_station;
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7362
diff changeset
  2635
static CommandCost AiDoBuildDefaultRoadBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2636
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2637
// Returns rule and cost
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7362
diff changeset
  2638
static int AiFindBestDefaultRoadBlock(TileIndex tile, byte direction, byte cargo, CommandCost *cost)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2639
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2640
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2641
	const AiDefaultRoadBlock *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2642
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2643
	_want_road_truck_station = (cargo & 0x7F) != CT_PASSENGERS;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2644
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2645
	for (i = 0; (p = _road_default_block_data[i]) != NULL; i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2646
		if (p->dir == direction) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2647
			*cost = AiDoBuildDefaultRoadBlock(tile, p->data, 0);
7442
a4f7989f27d4 (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 7439
diff changeset
  2648
			if (CmdSucceeded(*cost) && AiCheckRoadResources(tile, p->data, cargo))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2649
				return i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2650
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2651
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2652
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2653
	return -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2654
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2655
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7362
diff changeset
  2656
static CommandCost AiDoBuildDefaultRoadBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2657
{
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7362
diff changeset
  2658
	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
  2659
	CommandCost total_cost(EXPENSES_CONSTRUCTION);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2660
	Town *t = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2661
	int rating = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2662
	int roadflag = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2663
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
  2664
	for (;p->mode != 4;p++) {
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2665
		TileIndex c = TILE_MASK(tile + ToTileIndexDiff(p->tileoffs));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2666
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2667
		_cleared_town = NULL;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  2668
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2669
		if (p->mode == 2) {
9059
04edde3eb0c6 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 9052
diff changeset
  2670
			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
  2671
					(GetRoadBits(c, ROADTYPE_ROAD) & p->attr) != 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2672
				roadflag |= 2;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  2673
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2674
				// 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
  2675
				if ((GetRoadBits(c, ROADTYPE_ROAD) & p->attr) == p->attr) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2676
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2677
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
  2678
			ret = DoCommand(c, p->attr, 0, flag | DC_AUTO | DC_NO_WATER, 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
  2679
			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
  2680
			total_cost.AddCost(ret);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2681
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2682
			continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2683
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2684
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2685
		if (p->mode == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2686
			// 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
  2687
			ret = DoCommand(c, p->attr, 0, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_ROAD_DEPOT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2688
			goto clear_town_stuff;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2689
		} else if (p->mode == 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2690
			if (_want_road_truck_station) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2691
				// Truck station
7177
08330bb931a1 (svn r9912) -Fix: the AIs could build any road(station)s.
rubidium
parents: 7157
diff changeset
  2692
				ret = DoCommand(c, p->attr, ROADTYPES_ROAD << 2 | RoadStop::TRUCK, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_ROAD_STOP);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2693
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2694
				// Bus station
7177
08330bb931a1 (svn r9912) -Fix: the AIs could build any road(station)s.
rubidium
parents: 7157
diff changeset
  2695
				ret = DoCommand(c, p->attr, ROADTYPES_ROAD << 2 | RoadStop::BUS, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_ROAD_STOP);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2696
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2697
clear_town_stuff:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2698
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
  2699
			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
  2700
			total_cost.AddCost(ret);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2701
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2702
			if (_cleared_town != NULL) {
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2703
				if (t != NULL && t != _cleared_town) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2704
				t = _cleared_town;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2705
				rating += _cleared_town_rating;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2706
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2707
		} else if (p->mode == 3) {
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2708
			if (flag & DC_EXEC) continue;
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2709
3644
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3609
diff changeset
  2710
			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
  2711
9059
04edde3eb0c6 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 9052
diff changeset
  2712
			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
  2713
				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
  2714
				if (CmdFailed(ret)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2715
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  2716
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2717
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2718
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2719
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2720
	if (!_want_road_truck_station && !(roadflag & 2)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2721
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2722
	if (!(flag & DC_EXEC)) {
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2723
		if (t != NULL && rating > t->ratings[_current_player]) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2724
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2725
	return total_cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2726
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2727
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2728
// Make sure the blocks are not too close to each other
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2729
static bool AiCheckBlockDistances(Player *p, TileIndex tile)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2730
{
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
  2731
	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
  2732
	uint num = _players_ai[p->index].num_build_rec;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2733
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2734
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2735
		if (aib->cur_building_rule != 255) {
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2736
			if (DistanceManhattan(aib->use_tile, tile) < 9) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2737
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2738
	} while (++aib, --num);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2739
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2740
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2741
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2742
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2743
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2744
static void AiStateBuildDefaultRoadBlocks(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2745
{
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2746
	uint i;
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2747
	int j;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2748
	AiBuildRec *aib;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2749
	int rule;
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7362
diff changeset
  2750
	CommandCost cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2751
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2752
	// 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
  2753
	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
  2754
		_players_ai[p->index].state = AIS_DELETE_RAIL_BLOCKS;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2755
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2756
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2757
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2758
	// do the following 8 times
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2759
	for (i = 0; i != 8; i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2760
		// 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
  2761
		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
  2762
		j = _players_ai[p->index].num_build_rec;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2763
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2764
			// this item has already been built?
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2765
			if (aib->cur_building_rule != 255) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2766
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2767
			// adjust the coordinate randomly,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2768
			// to make sure that we find a position.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2769
			aib->use_tile = AdjustTileCoordRandomly(aib->spec_tile, aib->rand_rng);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2770
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2771
			// 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
  2772
			rule = AiFindBestDefaultRoadBlock(
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2773
				aib->use_tile, aib->direction, aib->cargo, &cost
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2774
			);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2775
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2776
			if (rule == -1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2777
				// 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
  2778
				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
  2779
					AiDoTerraformLand(aib->use_tile, (DiagDirection)(Random() & 3), 3, (int8)_players_ai[p->index].state_mode);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2780
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2781
				// 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
  2782
				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
  2783
					_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
  2784
					_players_ai[p->index].state_mode = -_players_ai[p->index].state_mode;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2785
				}
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  2786
			} 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
  2787
				CommandCost r;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2788
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2789
				// player has money, build it.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2790
				aib->cur_building_rule = rule;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2791
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2792
				r = AiDoBuildDefaultRoadBlock(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2793
					aib->use_tile,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2794
					_road_default_block_data[rule]->data,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2795
					DC_EXEC | DC_NO_TOWN_RATING
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2796
				);
7442
a4f7989f27d4 (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 7439
diff changeset
  2797
				assert(CmdSucceeded(r));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2798
			}
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  2799
		} while (++aib, --j);
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2800
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2801
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2802
	// 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
  2803
	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
  2804
	j = _players_ai[p->index].num_build_rec;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2805
	do {
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2806
		if (aib->cur_building_rule == 255) return;
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  2807
	} while (++aib, --j);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2808
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2809
	// 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
  2810
	_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
  2811
	_players_ai[p->index].state_mode = 255;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2812
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2813
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  2814
struct AiRoadFinder {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2815
	TileIndex final_tile;
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  2816
	DiagDirection final_dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2817
	byte depth;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2818
	byte recursive_mode;
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  2819
	DiagDirection cur_best_dir;
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  2820
	DiagDirection best_dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2821
	byte cur_best_depth;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2822
	byte best_depth;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2823
	uint cur_best_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2824
	const byte *best_ptr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2825
	uint best_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2826
	TileIndex cur_best_tile, best_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2827
	TileIndex bridge_end_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2828
	Player *player;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  2829
};
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  2830
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  2831
struct AiRoadEnum {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2832
	TileIndex dest;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2833
	TileIndex best_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2834
	int best_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2835
	uint best_dist;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  2836
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2837
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  2838
static const DiagDirection _dir_by_track[] = {
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  2839
	DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE,
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  2840
	DIAGDIR_NE, DIAGDIR_NE,
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  2841
	DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2842
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2843
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  2844
static void AiBuildRoadRecursive(AiRoadFinder *arf, TileIndex tile, DiagDirection dir);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2845
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2846
static bool AiCheckRoadPathBetter(AiRoadFinder *arf, const byte *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2847
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2848
	bool better = false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2849
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2850
	if (arf->recursive_mode < 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2851
		// Mode is 0. This means destination has not been found yet.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2852
		// 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
  2853
		if (arf->cur_best_dist < arf->best_dist ||
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2854
			(arf->cur_best_dist == arf->best_dist && arf->cur_best_depth < arf->best_depth)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2855
			arf->best_depth = arf->cur_best_depth;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2856
			arf->best_dist = arf->cur_best_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2857
			arf->best_dir = arf->cur_best_dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2858
			arf->best_ptr = p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2859
			arf->best_tile = arf->cur_best_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2860
			better = true;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  2861
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2862
	} else if (arf->recursive_mode > 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2863
		// Mode is 2.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2864
		if (arf->best_dist != 0 || arf->cur_best_depth < arf->best_depth) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2865
			arf->best_depth = arf->cur_best_depth;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2866
			arf->best_dist = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2867
			arf->best_ptr = p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2868
			arf->best_tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2869
			better = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2870
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2871
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2872
	arf->recursive_mode = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2873
	arf->cur_best_dist = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2874
	arf->cur_best_depth = 0xff;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2875
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2876
	return better;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2877
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2878
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2879
9107
3e57b96e98f2 (svn r12193) -Codechange: Rename a magic variable, give it a decent type, and remove a 'goto'.
frosch
parents: 9098
diff changeset
  2880
static bool AiEnumFollowRoad(TileIndex tile, AiRoadEnum *a, int track, uint length)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2881
{
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1224
diff changeset
  2882
	uint dist = DistanceManhattan(tile, a->dest);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2883
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2884
	if (dist <= a->best_dist) {
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4527
diff changeset
  2885
		TileIndex tile2 = TILE_MASK(tile + TileOffsByDiagDir(_dir_by_track[track]));
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1927
diff changeset
  2886
9059
04edde3eb0c6 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 9052
diff changeset
  2887
		if (IsNormalRoadTile(tile2)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2888
			a->best_dist = dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2889
			a->best_tile = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2890
			a->best_track = track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2891
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2892
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2893
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2894
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2895
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2896
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2897
static const uint16 _ai_road_table_and[4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2898
	0x1009,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2899
	0x16,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2900
	0x520,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2901
	0x2A00,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2902
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2903
410
8de2aaf20800 (svn r607) -Patch: [ 985102 ] static cleanup
tron
parents: 193
diff changeset
  2904
static bool AiCheckRoadFinished(Player *p)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2905
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2906
	AiRoadEnum are;
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1927
diff changeset
  2907
	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
  2908
	DiagDirection dir = _players_ai[p->index].cur_dir_a;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2909
	uint32 bits;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2910
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
  2911
	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
  2912
	tile = TILE_MASK(_players_ai[p->index].cur_tile_a + TileOffsByDiagDir(dir));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2913
5028
fef7184a6b6c (svn r7069) -Fix: AI tried to build road from the back or side of road stop/depot (peter1138)
KUDr
parents: 5016
diff changeset
  2914
	if (IsRoadStopTile(tile) || IsTileDepotType(tile, TRANSPORT_ROAD)) return false;
9112
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9110
diff changeset
  2915
	bits = TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_ROAD, ROADTYPES_ROAD)) & _ai_road_table_and[dir];
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2916
	if (bits == 0) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2917
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2918
	are.best_dist = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2919
8501
66d88113ba24 (svn r11564) -Codechange: Increase the usage of the for_each_bit macro and rename it fitting to the naming style
skidd13
parents: 8463
diff changeset
  2920
	uint i;
66d88113ba24 (svn r11564) -Codechange: Increase the usage of the for_each_bit macro and rename it fitting to the naming style
skidd13
parents: 8463
diff changeset
  2921
	FOR_EACH_SET_BIT(i, bits) {
8888
94cc8a25185f (svn r11962) -Cleanup: OPF is no longer used to update signals
smatz
parents: 8886
diff changeset
  2922
		FollowTrack(tile, 0x1000 | TRANSPORT_ROAD, ROADTYPES_ROAD, (DiagDirection)_dir_by_track[i], (TPFEnumProc*)AiEnumFollowRoad, NULL, &are);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2923
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2924
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2925
	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
  2926
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2927
	if (are.best_dist == 0) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2928
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
  2929
	_players_ai[p->index].cur_tile_a = are.best_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
  2930
	_players_ai[p->index].cur_dir_a = _dir_by_track[are.best_track];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2931
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2932
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2933
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2934
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1927
diff changeset
  2935
static bool AiBuildRoadHelper(TileIndex tile, int flags, int type)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2936
{
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2937
	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
  2938
		ROAD_X,
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2939
		ROAD_Y,
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  2940
		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
  2941
		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
  2942
		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
  2943
		ROAD_SE | ROAD_NE
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2944
	};
7442
a4f7989f27d4 (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 7439
diff changeset
  2945
	return CmdSucceeded(DoCommand(tile, _road_bits[type], 0, flags, CMD_BUILD_ROAD));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2946
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2947
536
aef4753985d3 (svn r907) Sprinkle holy ANSI water:
tron
parents: 460
diff changeset
  2948
static inline void AiCheckBuildRoadBridgeHere(AiRoadFinder *arf, TileIndex tile, const byte *p)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2949
{
3644
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3609
diff changeset
  2950
	Slope tileh;
3088
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2951
	uint z;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2952
	bool flag;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2953
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  2954
	DiagDirection dir2 = (DiagDirection)(p[0] & 3);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  2955
3088
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2956
	tileh = GetTileSlope(tile, &z);
3644
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3609
diff changeset
  2957
	if (tileh == _dir_table_1[dir2] || (tileh == SLOPE_FLAT && z != 0)) {
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3977
diff changeset
  2958
		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
  2959
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2960
		// Allow bridges directly over bottom tiles
3088
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2961
		flag = z == 0;
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
  2962
		for (;;) {
3088
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2963
			TileType type;
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2964
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4527
diff changeset
  2965
			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
  2966
			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
  2967
			type = GetTileType(tile_new);
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2968
8847
98ccd8072daf (svn r11917) -Fix (r3677): AI was reading wrong tile slope while building road bridge
smatz
parents: 8818
diff changeset
  2969
			if (type == MP_CLEAR || type == MP_TREES || GetTileSlope(tile_new, NULL) != SLOPE_FLAT) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2970
				// Allow a bridge if either we have a tile that's water, rail or street,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2971
				// or if we found an up tile.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2972
				if (!flag) return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2973
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2974
			}
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
  2975
			if (type != MP_WATER && type != MP_RAILWAY && type != MP_ROAD) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2976
			flag = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2977
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2978
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2979
		// 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
  2980
		if (CmdFailed(DoCommand(tile_new, tile, ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO, CMD_BUILD_BRIDGE)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2981
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2982
		AiBuildRoadRecursive(arf, tile_new, dir2);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  2983
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2984
		// At the bottom depth, check if the new path is better than the old one.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2985
		if (arf->depth == 1) {
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  2986
			if (AiCheckRoadPathBetter(arf, p)) arf->bridge_end_tile = tile_new;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2987
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2988
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2989
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2990
536
aef4753985d3 (svn r907) Sprinkle holy ANSI water:
tron
parents: 460
diff changeset
  2991
static inline void AiCheckBuildRoadTunnelHere(AiRoadFinder *arf, TileIndex tile, const byte *p)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2992
{
3088
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2993
	uint z;
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2994
4570504ac734 (svn r3677) Remove the FindLandscapeHeightByTile()s from the default AI.
tron
parents: 3071
diff changeset
  2995
	if (GetTileSlope(tile, &z) == _dir_table_2[p[0] & 3] && z != 0) {
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7362
diff changeset
  2996
		CommandCost cost = DoCommand(tile, 0x200, 0, DC_AUTO, CMD_BUILD_TUNNEL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2997
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7442
diff changeset
  2998
		if (CmdSucceeded(cost) && cost.GetCost() <= (arf->player->player_money >> 4)) {
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  2999
			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
  3000
			if (arf->depth == 1)  AiCheckRoadPathBetter(arf, p);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3001
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3002
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3003
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3004
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3005
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3006
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  3007
static void AiBuildRoadRecursive(AiRoadFinder *arf, TileIndex tile, DiagDirection dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3008
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3009
	const byte *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3010
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4527
diff changeset
  3011
	tile = TILE_MASK(tile + TileOffsByDiagDir(dir));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  3012
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3013
	// Reached destination?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3014
	if (tile == arf->final_tile) {
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  3015
		if (ReverseDiagDir(arf->final_dir) == dir) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3016
			arf->recursive_mode = 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3017
			arf->cur_best_depth = arf->depth;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  3018
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3019
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3020
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3021
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3022
	// Depth too deep?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3023
	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
  3024
		uint dist = DistanceMaxPlusManhattan(tile, arf->final_tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3025
		if (dist < arf->cur_best_dist) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3026
			// Store the tile that is closest to the final position.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3027
			arf->cur_best_dist = dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3028
			arf->cur_best_tile = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3029
			arf->cur_best_dir = dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3030
			arf->cur_best_depth = arf->depth;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3031
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3032
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3033
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3034
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3035
	// Increase recursion depth
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3036
	arf->depth++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3037
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3038
	// Grab pointer to list of stuff that is possible to build
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3039
	p = _ai_table_15[dir];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3040
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3041
	// 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
  3042
	if (GetTileZ(tile) == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3043
		p += 6;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3044
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3045
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3046
			// Make sure that a road can be built here.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3047
			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
  3048
				AiBuildRoadRecursive(arf, tile, (DiagDirection)p[1]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3049
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3050
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3051
			// At the bottom depth?
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  3052
			if (arf->depth == 1) AiCheckRoadPathBetter(arf, p);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3053
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3054
			p += 2;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  3055
		} while (!(p[0] & 0x80));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3056
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3057
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3058
	AiCheckBuildRoadBridgeHere(arf, tile, p);
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  3059
	AiCheckBuildRoadTunnelHere(arf, tile, p + 1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3060
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3061
	arf->depth--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3062
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3063
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3064
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3065
static void AiBuildRoadConstruct(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3066
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3067
	AiRoadFinder arf;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3068
	int i;
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1927
diff changeset
  3069
	TileIndex tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3070
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3071
	// Reached destination?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3072
	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
  3073
		_players_ai[p->index].state_mode = 255;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3074
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3075
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3076
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3077
	// Setup recursive finder and call it.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3078
	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
  3079
	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
  3080
	arf.final_dir = _players_ai[p->index].cur_dir_b;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3081
	arf.depth = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3082
	arf.recursive_mode = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3083
	arf.best_ptr = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3084
	arf.cur_best_dist = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3085
	arf.cur_best_depth = 0xff;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3086
	arf.best_dist = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3087
	arf.best_depth =  0xff;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3088
	arf.cur_best_tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3089
	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
  3090
	AiBuildRoadRecursive(&arf, _players_ai[p->index].cur_tile_a, _players_ai[p->index].cur_dir_a);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3091
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3092
	// Reached destination?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3093
	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
  3094
		_players_ai[p->index].state_mode = 255;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3095
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3096
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3097
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3098
	// Didn't find anything to build?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3099
	if (arf.best_ptr == NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3100
		// Terraform some
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3101
do_some_terraform:
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  3102
		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
  3103
			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
  3104
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  3105
		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
  3106
			_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
  3107
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  3108
			_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
  3109
			_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
  3110
			_players_ai[p->index].state_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3111
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3112
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3113
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3114
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
  3115
	tile = TILE_MASK(_players_ai[p->index].cur_tile_a + TileOffsByDiagDir(_players_ai[p->index].cur_dir_a));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3116
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  3117
	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
  3118
		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
  3119
		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
  3120
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
		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
  3122
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
  3123
		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
  3124
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
  3125
		/* 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
  3126
 		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
  3127
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
  3128
		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
  3129
			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
  3130
		} 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
  3131
			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
  3132
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
  3133
			/* 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
  3134
			 * 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
  3135
			 * 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
  3136
			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
  3137
				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
  3138
					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
  3139
					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
  3140
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3141
			}
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
  3142
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
  3143
			/* 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
  3144
			DoCommand(t1, t2, i + ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3145
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3146
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
  3147
		_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
  3148
		_players_ai[p->index].state_counter = 0;
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  3149
	} else if (arf.best_ptr[0] & 0x40) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3150
		// 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
  3151
		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
  3152
		_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
  3153
		_players_ai[p->index].state_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3154
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3155
		// road
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3156
		if (!AiBuildRoadHelper(tile, DC_EXEC | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, arf.best_ptr[0]))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3157
			goto do_some_terraform;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3158
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
		_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
  3160
		_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
  3161
		_players_ai[p->index].state_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3162
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3163
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3164
	if (arf.best_tile != 0) {
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  3165
		for (i = 0; i != 2; i++)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3166
			AiDoTerraformLand(arf.best_tile, arf.best_dir, 3, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3167
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3168
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3169
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3170
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3171
static void AiBuildRoad(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3172
{
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
  3173
	if (_players_ai[p->index].state_mode < 1) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3174
		// Construct mode, build new road.
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  3175
		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
  3176
	} else if (_players_ai[p->index].state_mode == 1) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3177
		// 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
  3178
		_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
  3179
		_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
  3180
	} 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
  3181
		uint i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3182
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3183
		// 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
  3184
		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
  3185
			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
  3186
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3187
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
  3188
		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
  3189
			_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
  3190
			_players_ai[p->index].state_mode = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3191
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3192
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3193
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3194
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  3195
static TileIndex AiGetRoadBlockEdge(byte rule, TileIndex tile, DiagDirection *dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3196
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3197
	const AiDefaultBlockData *p = _road_default_block_data[rule]->data;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3198
	while (p->mode != 1) p++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3199
	*dir = p->attr;
909
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  3200
	return TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3201
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3202
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3203
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3204
static void AiStateBuildRoad(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3205
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3206
	int num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3207
	AiBuildRec *aib;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3208
	byte cmd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3209
	TileIndex tile;
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7486
diff changeset
  3210
	DiagDirection dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3211
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3212
	// 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
  3213
	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
  3214
		_players_ai[p->index].state = AIS_DELETE_ROAD_BLOCKS;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3215
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3216
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3217
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3218
	// 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
  3219
	if (_players_ai[p->index].state_mode != 255) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3220
		AiBuildRoad(p);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  3221
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3222
		// 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
  3223
		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
  3224
		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
  3225
		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
  3226
		Swap(_players_ai[p->index].cur_dir_a,    _players_ai[p->index].cur_dir_b);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3227
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3228
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3229
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3230
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3231
	// 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
  3232
	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
  3233
	aib = &_players_ai[p->index].src;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3234
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2819
diff changeset
  3235
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3236
		cmd = aib->buildcmd_a;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3237
		aib->buildcmd_a = 255;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3238
		if (cmd != 255) break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3239
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3240
		aib++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3241
		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
  3242
			_players_ai[p->index].state = AIS_BUILD_ROAD_VEHICLES;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3243
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3244
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3245
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3246
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3247
	// Find first edge to build from.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3248
	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
  3249
	_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
  3250
	_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
  3251
	_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
  3252
	_players_ai[p->index].cur_dir_a = dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3253
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3254
	// 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
  3255
	aib = (&_players_ai[p->index].src) + (cmd & 0xF);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3256
	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
  3257
	_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
  3258
	_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
  3259
	_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
  3260
	_players_ai[p->index].cur_dir_b = dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3261
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3262
	// 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
  3263
	_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
  3264
	_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
  3265
	_players_ai[p->index].banned_tile_count = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3266
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3267
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
  3268
static StationID AiGetStationIdFromRoadBlock(TileIndex tile, int id)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3269
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3270
	const AiDefaultBlockData *p = _road_default_block_data[id]->data;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3271
	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
  3272
	return GetStationIndex(TILE_ADD(tile, ToTileIndexDiff(p->tileoffs)));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3273
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3274
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3275
static void AiStateBuildRoadVehicles(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3276
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3277
	const AiDefaultBlockData *ptr;
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1927
diff changeset
  3278
	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
  3279
	VehicleID loco_id;
2551
436aaaa22ba5 (svn r3080) byte -> PlayerID, int -> EngineID, -1 -> INVALID_ENGINE
tron
parents: 2544
diff changeset
  3280
	EngineID veh;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  3281
	uint i;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  3282
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
	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
  3284
	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
  3285
	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
  3286
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  3287
	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
  3288
	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
  3289
		_players_ai[p->index].state = AIS_0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3290
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3291
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3292
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
  3293
	if (CmdFailed(DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH))) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3294
3948
95f9fa0ac551 (svn r5094) Remove _new_{aircraft,roadveh,ship,train,wagon}_id. _new_vehicle_id is enough.
tron
parents: 3933
diff changeset
  3295
	loco_id = _new_vehicle_id;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3296
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
  3297
	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
  3298
		/* 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
  3299
		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
  3300
			/* Refit failed... sell the vehicle */
940b1fec4220 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5029
diff changeset
  3301
			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
  3302
			return;
940b1fec4220 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5029
diff changeset
  3303
		}
940b1fec4220 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5029
diff changeset
  3304
	}
940b1fec4220 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5029
diff changeset
  3305
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
  3306
	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
  3307
		const AiBuildRec* aib = &_players_ai[p->index].src + _players_ai[p->index].order_list_blocks[i];
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  3308
		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
  3309
			_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
  3310
			_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
  3311
			(_opt.landscape == LT_TEMPERATE && _players_ai[p->index].cargo_type == CT_VALUABLES)
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  3312
		);
558
9b115b39c515 (svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents: 555
diff changeset
  3313
		Order order;
9b115b39c515 (svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents: 555
diff changeset
  3314
9b115b39c515 (svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents: 555
diff changeset
  3315
		order.type = OT_GOTO_STATION;
9b115b39c515 (svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents: 555
diff changeset
  3316
		order.flags = 0;
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4495
diff changeset
  3317
		order.dest = AiGetStationIdFromRoadBlock(aib->use_tile, aib->cur_building_rule);
558
9b115b39c515 (svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents: 555
diff changeset
  3318
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8766
diff changeset
  3319
		if (!is_pass && i == 1) order.flags |= OFB_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
  3320
		if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8766
diff changeset
  3321
			order.flags |= OFB_FULL_LOAD;
558
9b115b39c515 (svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents: 555
diff changeset
  3322
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
  3323
		DoCommand(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3324
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3325
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
  3326
	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
  3327
	DoCommand(0, loco_id, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3328
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
  3329
	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
  3330
	if (--_players_ai[p->index].num_loco_to_build == 0) _players_ai[p->index].state = AIS_0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3331
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3332
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3333
static void AiStateDeleteRoadBlocks(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3334
{
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
  3335
	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
  3336
	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
  3337
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3338
	do {
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  3339
		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
  3340
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  3341
		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
  3342
		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
  3343
			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
  3344
			DoCommand(TILE_ADD(aib->use_tile, ToTileIndexDiff(b->tileoffs)), 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3345
		}
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  3346
	} while (++aib, --num);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3347
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
  3348
	_players_ai[p->index].state = AIS_0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3349
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3350
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3351
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3352
static void AiStateAirportStuff(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3353
{
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
  3354
	const Station* st;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3355
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3356
	AiBuildRec *aib;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3357
	byte rule;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3358
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3359
	// Here we look for an airport we could use instead of building a new
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3360
	// one. If we find such an aiport for any waypoint,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3361
	// AiStateBuildDefaultAirportBlocks() will kindly skip that one when
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3362
	// building the waypoints.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3363
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3364
	i = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3365
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3366
		// We do this all twice - once for the source (town in the case
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3367
		// of oilrig route) and then for the destination (oilrig in the
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3368
		// 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
  3369
		aib = &_players_ai[p->index].src + i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3370
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3371
		FOR_ALL_STATIONS(st) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3372
			// Is this an airport?
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  3373
			if (!(st->facilities & FACIL_AIRPORT)) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3374
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3375
			// 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
  3376
			if (st->owner != OWNER_NONE && st->owner != _current_player) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3377
6358
e7997742bf02 (svn r8755) -Fix
tron
parents: 6353
diff changeset
  3378
			AirportFTAClass::Flags flags = st->Airport()->flags;
6350
c644e180cdf8 (svn r8747) -Fix
tron
parents: 6222
diff changeset
  3379
10801
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3380
			/* if airport doesn't accept our kind of plane, dismiss it */
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3381
			if (!(flags & (_players_ai[p->index].build_kind == 1 ? AirportFTAClass::HELICOPTERS : AirportFTAClass::AIRPLANES))) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3382
				continue;
6350
c644e180cdf8 (svn r8747) -Fix
tron
parents: 6222
diff changeset
  3383
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3384
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3385
			// 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
  3386
			if (DistanceMax(st->airport_tile, aib->spec_tile) > aib->rand_rng)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3387
				continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3388
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3389
			// It's ideal airport, let's take it!
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3390
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3391
			/* XXX: This part is utterly broken - rule should
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3392
			 * contain number of the rule appropriate for the
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3393
			 * airport type (country, town, ...), see
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3394
			 * _airport_default_block_data (rule is just an index
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3395
			 * in this array). But the only difference between the
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3396
			 * currently existing two rules (rule 0 - town and rule
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3397
			 * 1 - country) is the attr field which is used only
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3398
			 * when building new airports - and that's irrelevant
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3399
			 * for us. So using just about any rule will suffice
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3400
			 * here for now (some of the new airport types would be
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3401
			 * broken because they will probably need different
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3402
			 * tileoff values etc), no matter that
3338
2c60ed0dcce3 (svn r4120) Use the new station functions where appropriate
tron
parents: 3336
diff changeset
  3403
			 * 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
  3404
			if (!(flags & AirportFTAClass::AIRPLANES)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3405
				/* Heliports should have maybe own rulesets but
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3406
				 * OTOH we don't want AI to pick them up when
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3407
				 * looking for a suitable airport type to build.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3408
				 * So any of rules 0 or 1 would do for now. The
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3409
				 * original rule number was 2 but that's a bug
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3410
				 * because we have no such rule. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3411
				rule = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3412
			} else {
3338
2c60ed0dcce3 (svn r4120) Use the new station functions where appropriate
tron
parents: 3336
diff changeset
  3413
				rule = IsHangarTile(st->airport_tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3414
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3415
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3416
			aib->cur_building_rule = rule;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3417
			aib->use_tile = st->airport_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3418
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3419
		}
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
  3420
	} 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
  3421
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  3422
	_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
  3423
	_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
  3424
	_players_ai[p->index].state_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3425
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3426
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7362
diff changeset
  3427
static CommandCost AiDoBuildDefaultAirportBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3428
{
6222
50e47f803549 (svn r8660) -Fix
tron
parents: 6119
diff changeset
  3429
	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
  3430
	CommandCost ret,total_cost(EXPENSES_CONSTRUCTION);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3431
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  3432
	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
  3433
		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
  3434
		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
  3435
		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
  3436
		total_cost.AddCost(ret);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3437
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3438
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3439
	return total_cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3440
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3441
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3442
static bool AiCheckAirportResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3443
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3444
	uint values[NUM_CARGO];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3445
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  3446
	for (; p->mode == 0; p++) {
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1927
diff changeset
  3447
		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
  3448
		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
  3449
		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
  3450
		uint h = airport->size_y;
9070
e83a2f017f3d (svn r12155) -Fix (r12154): some warnings (always compile before commit)
glx
parents: 9069
diff changeset
  3451
		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
  3452
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3453
		if (cargo & 0x80) {
568
6eb7d394fb35 (svn r979) Allow more realistically sized catchment areas
Celestar
parents: 558
diff changeset
  3454
			GetProductionAroundTiles(values, tile2, w, h, rad);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3455
			return values[cargo & 0x7F] != 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3456
		} else {
568
6eb7d394fb35 (svn r979) Allow more realistically sized catchment areas
Celestar
parents: 558
diff changeset
  3457
			GetAcceptanceAroundTiles(values, tile2, w, h, rad);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3458
			return values[cargo] >= 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3459
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3460
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3461
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3462
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3463
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7362
diff changeset
  3464
static int AiFindBestDefaultAirportBlock(TileIndex tile, byte cargo, byte heli, CommandCost *cost)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3465
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3466
	const AiDefaultBlockData *p;
10801
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3467
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3468
	bool no_small = false;
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3469
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3470
	if (!heli) {
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3471
		/* do not build small airport if we have large available and we are not building heli route */
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3472
		uint valid = GetValidAirports();
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3473
		for (uint i = 0; (p = _airport_default_block_data[i]) != NULL; i++) {
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3474
			uint flags = GetAirport(p->attr)->flags;
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3475
			if (HasBit(valid, p->attr) && (flags & AirportFTAClass::AIRPLANES) && !(flags & AirportFTAClass::SHORT_STRIP)) {
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3476
				no_small = true;
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3477
				break;
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3478
			}
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3479
		}
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3480
	}
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3481
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3482
	for (uint i = 0; (p = _airport_default_block_data[i]) != NULL; i++) {
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3483
		uint flags = GetAirport(p->attr)->flags;
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3484
		/* If we are doing a helicopter service, avoid building airports where they can't land */
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3485
		if (heli && !(flags & AirportFTAClass::HELICOPTERS)) continue;
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3486
		/* Similiar with aircraft ... */
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3487
		if (!heli && !(flags & AirportFTAClass::AIRPLANES)) continue;
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3488
		/* Do not build small airport if we prefer large */
3ad9dfb5430d (svn r13352) [0.6] -Backport from trunk (r13348, r13222, r13221, r13217):
rubidium
parents: 9113
diff changeset
  3489
		if (no_small && (flags & AirportFTAClass::SHORT_STRIP)) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3490
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3491
		*cost = AiDoBuildDefaultAirportBlock(tile, p, 0);
7442
a4f7989f27d4 (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 7439
diff changeset
  3492
		if (CmdSucceeded(*cost) && AiCheckAirportResources(tile, p, cargo))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3493
			return i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3494
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3495
	return -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3496
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3497
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3498
static void AiStateBuildDefaultAirportBlocks(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3499
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3500
	int i, j;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3501
	AiBuildRec *aib;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3502
	int rule;
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7362
diff changeset
  3503
	CommandCost cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3504
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3505
	// 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
  3506
	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
  3507
		_players_ai[p->index].state = AIS_0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3508
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3509
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3510
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3511
	// do the following 8 times
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3512
	i = 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3513
	do {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  3514
		// 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
  3515
		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
  3516
		j = _players_ai[p->index].num_build_rec;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3517
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3518
			// this item has already been built?
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  3519
			if (aib->cur_building_rule != 255) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3520
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3521
			// adjust the coordinate randomly,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3522
			// to make sure that we find a position.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3523
			aib->use_tile = AdjustTileCoordRandomly(aib->spec_tile, aib->rand_rng);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3524
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3525
			// 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
  3526
			rule = AiFindBestDefaultAirportBlock(aib->use_tile, aib->cargo, _players_ai[p->index].build_kind, &cost);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3527
1861
8b6f1d021317 (svn r2367) Remove some unused functions, probably was some debug stuff
tron
parents: 1767
diff changeset
  3528
//			SetRedErrorSquare(aib->use_tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3529
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3530
			if (rule == -1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3531
				// 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
  3532
				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
  3533
					AiDoTerraformLand(aib->use_tile, (DiagDirection)(Random() & 3), 3, (int8)_players_ai[p->index].state_mode);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3534
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3535
				// 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
  3536
				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
  3537
					_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
  3538
					_players_ai[p->index].state_mode = -_players_ai[p->index].state_mode;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3539
				}
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  3540
			} else if (CheckPlayerHasMoney(cost) && AiCheckBlockDistances(p, aib->use_tile)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3541
				// player has money, build it.
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7362
diff changeset
  3542
				CommandCost r;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3543
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3544
				aib->cur_building_rule = rule;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3545
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3546
				r = AiDoBuildDefaultAirportBlock(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3547
					aib->use_tile,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3548
					_airport_default_block_data[rule],
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3549
					DC_EXEC | DC_NO_TOWN_RATING
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3550
				);
7442
a4f7989f27d4 (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 7439
diff changeset
  3551
				assert(CmdSucceeded(r));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3552
			}
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  3553
		} while (++aib, --j);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3554
	} while (--i);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3555
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3556
	// 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
  3557
	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
  3558
	j = _players_ai[p->index].num_build_rec;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3559
	do {
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  3560
		if (aib->cur_building_rule == 255) return;
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  3561
	} while (++aib, --j);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3562
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3563
	// 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
  3564
	_players_ai[p->index].state = AIS_BUILD_AIRCRAFT_VEHICLES;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3565
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3566
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
  3567
static StationID AiGetStationIdFromAircraftBlock(TileIndex tile, int id)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3568
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3569
	const AiDefaultBlockData *p = _airport_default_block_data[id];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3570
	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
  3571
	return GetStationIndex(TILE_ADD(tile, ToTileIndexDiff(p->tileoffs)));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3572
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3573
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3574
static void AiStateBuildAircraftVehicles(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3575
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3576
	const AiDefaultBlockData *ptr;
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1927
diff changeset
  3577
	TileIndex tile;
2544
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
  3578
	EngineID veh;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3579
	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
  3580
	VehicleID loco_id;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  3581
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
  3582
	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
  3583
	for (; ptr->mode != 0; ptr++) {}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3584
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
  3585
	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
  3586
8898
a36f4df63438 (svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents: 8894
diff changeset
  3587
	/* 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
  3588
	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
  3589
	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
  3590
		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
  3591
		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
  3592
a36f4df63438 (svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents: 8894
diff changeset
  3593
		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
  3594
a36f4df63438 (svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents: 8894
diff changeset
  3595
		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
  3596
		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
  3597
		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
  3598
	}
a36f4df63438 (svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents: 8894
diff changeset
  3599
a36f4df63438 (svn r11972) -Fix: old AI shouldn't build fast planes with a small airport in orders
smatz
parents: 8894
diff changeset
  3600
	veh = AiChooseAircraftToBuild(p->player_money, forbidden);
2544
50cb52cdeb73 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2529
diff changeset
  3601
	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
  3602
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
  3603
	/* 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
  3604
	 * and offset to the FIRST depot because the AI picks the st->xy tile */
6358
e7997742bf02 (svn r8755) -Fix
tron
parents: 6353
diff changeset
  3605
	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
  3606
	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
  3607
	loco_id = _new_vehicle_id;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3608
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
  3609
	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
  3610
		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
  3611
		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
  3612
		Order order;
9b115b39c515 (svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents: 555
diff changeset
  3613
9b115b39c515 (svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents: 555
diff changeset
  3614
		order.type = OT_GOTO_STATION;
9b115b39c515 (svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents: 555
diff changeset
  3615
		order.flags = 0;
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4495
diff changeset
  3616
		order.dest = AiGetStationIdFromAircraftBlock(aib->use_tile, aib->cur_building_rule);
558
9b115b39c515 (svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents: 555
diff changeset
  3617
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8766
diff changeset
  3618
		if (!is_pass && i == 1) order.flags |= OFB_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
  3619
		if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8766
diff changeset
  3620
			order.flags |= OFB_FULL_LOAD;
558
9b115b39c515 (svn r960) -Fix: forgotten conversion of orders for the AI (Tron)
darkvater
parents: 555
diff changeset
  3621
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
  3622
		DoCommand(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3623
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3624
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
  3625
	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
  3626
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
  3627
	DoCommand(0, loco_id, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3628
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
  3629
	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
  3630
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  3631
	if (--_players_ai[p->index].num_loco_to_build == 0) _players_ai[p->index].state = AIS_0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3632
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3633
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3634
static void AiStateCheckShipStuff(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3635
{
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
  3636
	/* Ships are not implemented in this (broken) AI */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3637
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3638
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3639
static void AiStateBuildDefaultShipBlocks(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3640
{
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
  3641
	/* Ships are not implemented in this (broken) AI */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3642
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3643
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3644
static void AiStateDoShipStuff(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3645
{
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
  3646
	/* Ships are not implemented in this (broken) AI */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3647
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3648
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3649
static void AiStateSellVeh(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3650
{
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
  3651
	Vehicle *v = _players_ai[p->index].cur_veh;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3652
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3653
	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
  3654
		if (v->type == VEH_TRAIN) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  3655
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
  3656
			if (!IsTileDepotType(v->tile, TRANSPORT_RAIL) || v->u.rail.track != 0x80 || !(v->vehstatus&VS_STOPPED)) {
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 539
diff changeset
  3657
				if (v->current_order.type != OT_GOTO_DEPOT)
4495
b44566fc9025 (svn r6280) -Codechange: Use the same naming for trains as for other vehicles:
Darkvater
parents: 4434
diff changeset
  3658
					DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_TRAIN_TO_DEPOT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3659
				goto going_to_depot;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3660
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3661
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3662
			// 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
  3663
			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
  3664
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
  3665
		} 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
  3666
			if (!v->IsStoppedInDepot()) {
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 539
diff changeset
  3667
				if (v->current_order.type != OT_GOTO_DEPOT)
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
  3668
					DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3669
				goto going_to_depot;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3670
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3671
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
  3672
			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
  3673
		} 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
  3674
			if (!v->IsStoppedInDepot()) {
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 539
diff changeset
  3675
				if (v->current_order.type != OT_GOTO_DEPOT)
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3473
diff changeset
  3676
					DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_AIRCRAFT_TO_HANGAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3677
				goto going_to_depot;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3678
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3679
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
  3680
			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
  3681
		} 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
  3682
			/* Ships are not implemented in this (broken) AI */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3683
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3684
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3685
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3686
	goto return_to_loop;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3687
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
  3688
	if (++_players_ai[p->index].state_counter <= 832) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3689
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 539
diff changeset
  3690
	if (v->current_order.type == OT_GOTO_DEPOT) {
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 539
diff changeset
  3691
		v->current_order.type = OT_DUMMY;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 539
diff changeset
  3692
		v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3693
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3694
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3695
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
  3696
	_players_ai[p->index].state = AIS_VEH_LOOP;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3697
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3698
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3699
static void AiStateRemoveStation(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3700
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3701
	// 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
  3702
	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
  3703
	const Station *st;
55f4cbda781b (svn r2144) - Fix: AI crash when trying to remove stations with index higher than 256.
darkvater
parents: 1576
diff changeset
  3704
	TileIndex tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3705
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3706
	// 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
  3707
	_players_ai[p->index].state = AIS_1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3708
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3709
	// 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
  3710
	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
  3711
	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
  3712
	FOR_ALL_ORDERS(ord) {
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4495
diff changeset
  3713
		if (ord->type == OT_GOTO_STATION) in_use[ord->dest] = 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3714
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3715
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3716
	// Go through all stations and delete those that aren't in use
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3717
	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
  3718
		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
  3719
				( (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
  3720
					(st->truck_stops != NULL && (tile = st->truck_stops->xy)) != 0 ||
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3721
					(tile = st->train_tile) != 0 ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3722
					(tile = st->dock_tile) != 0 ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3723
					(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
  3724
			DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3725
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3726
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3727
1640
55f4cbda781b (svn r2144) - Fix: AI crash when trying to remove stations with index higher than 256.
darkvater
parents: 1576
diff changeset
  3728
	free(in_use);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3729
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3730
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
  3731
static void AiRemovePlayerRailOrRoad(Player *p, TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3732
{
3269
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3234
diff changeset
  3733
	TrackBits rails;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3734
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
  3735
	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
  3736
		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
  3737
3269
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3234
diff changeset
  3738
		if (IsPlainRailTile(tile)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3739
is_rail_crossing:;
3269
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3234
diff changeset
  3740
			rails = GetRailTrackStatus(tile);
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3234
diff changeset
  3741
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3234
diff changeset
  3742
			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
  3743
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3234
diff changeset
  3744
			if (rails & TRACK_BIT_3WAY_NE) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3745
pos_0:
3269
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3234
diff changeset
  3746
				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
  3747
					_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
  3748
					_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
  3749
					_players_ai[p->index].state = AIS_REMOVE_SINGLE_RAIL_TILE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3750
					return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3751
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3752
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3753
3269
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3234
diff changeset
  3754
			if (rails & TRACK_BIT_3WAY_SE) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3755
pos_1:
3269
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3234
diff changeset
  3756
				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
  3757
					_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
  3758
					_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
  3759
					_players_ai[p->index].state = AIS_REMOVE_SINGLE_RAIL_TILE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3760
					return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3761
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3762
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3763
3269
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3234
diff changeset
  3764
			if (rails & TRACK_BIT_3WAY_SW) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3765
pos_2:
3269
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3234
diff changeset
  3766
				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
  3767
					_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
  3768
					_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
  3769
					_players_ai[p->index].state = AIS_REMOVE_SINGLE_RAIL_TILE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3770
					return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3771
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3772
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3773
3269
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3234
diff changeset
  3774
			if (rails & TRACK_BIT_3WAY_NW) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3775
pos_3:
3269
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3234
diff changeset
  3776
				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
  3777
					_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
  3778
					_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
  3779
					_players_ai[p->index].state = AIS_REMOVE_SINGLE_RAIL_TILE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3780
					return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3781
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3782
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3783
		} else {
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  3784
			static const byte _depot_bits[] = {0x19, 0x16, 0x25, 0x2A};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3785
3185
c0ef161f700f (svn r3831) Add and use GetRailDepotDirection()
tron
parents: 3184
diff changeset
  3786
			DiagDirection dir = GetRailDepotDirection(tile);
c0ef161f700f (svn r3831) Add and use GetRailDepotDirection()
tron
parents: 3184
diff changeset
  3787
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4527
diff changeset
  3788
			if (GetRailTrackStatus(tile + TileOffsByDiagDir(dir)) & _depot_bits[dir])
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3789
				return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  3790
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
  3791
			DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3792
		}
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
  3793
	} 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
  3794
		if (!IsTileOwner(tile, _current_player)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3795
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  3796
		if (IsLevelCrossing(tile)) goto is_rail_crossing;
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  3797
9059
04edde3eb0c6 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 9052
diff changeset
  3798
		if (IsRoadDepot(tile)) {
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
  3799
			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
  3800
			TileIndex t;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3801
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3802
			// 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
  3803
			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
  3804
			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
  3805
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
  3806
			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
  3807
			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
  3808
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
  3809
			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
  3810
			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
  3811
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
  3812
			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
  3813
			if (IsTileType(t, MP_STATION) && IsTileOwner(t, _current_player)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3814
3179
774b1e6553a7 (svn r3816) Use existing accessors
tron
parents: 3153
diff changeset
  3815
			dir = GetRoadDepotDirection(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3816
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
  3817
			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
  3818
			DoCommand(
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4527
diff changeset
  3819
				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
  3820
				DiagDirToRoadBits(ReverseDiagDir(dir)),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3821
				0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3822
				DC_EXEC,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3823
				CMD_REMOVE_ROAD);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3824
		}
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
  3825
	} 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
  3826
		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
  3827
				!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
  3828
				GetTunnelBridgeTransportType(tile) != TRANSPORT_RAIL) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3829
			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
  3830
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  3831
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  3832
		rails = TRACK_BIT_NONE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3833
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
  3834
		switch (GetTunnelBridgeDirection(tile)) {
3196
29717e930f9a (svn r3857) Add and use GetBridgeRampDirection()
tron
parents: 3185
diff changeset
  3835
			default:
29717e930f9a (svn r3857) Add and use GetBridgeRampDirection()
tron
parents: 3185
diff changeset
  3836
			case DIAGDIR_NE: goto pos_2;
29717e930f9a (svn r3857) Add and use GetBridgeRampDirection()
tron
parents: 3185
diff changeset
  3837
			case DIAGDIR_SE: goto pos_3;
29717e930f9a (svn r3857) Add and use GetBridgeRampDirection()
tron
parents: 3185
diff changeset
  3838
			case DIAGDIR_SW: goto pos_0;
29717e930f9a (svn r3857) Add and use GetBridgeRampDirection()
tron
parents: 3185
diff changeset
  3839
			case DIAGDIR_NW: goto pos_1;
29717e930f9a (svn r3857) Add and use GetBridgeRampDirection()
tron
parents: 3185
diff changeset
  3840
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3841
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3842
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3843
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3844
static void AiStateRemoveTrack(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3845
{
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
  3846
	/* 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
  3847
	int num = MapSizeX() * 4;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  3848
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3849
	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
  3850
		TileIndex tile = ++_players_ai[p->index].state_counter;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3851
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3852
		// 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
  3853
		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
  3854
			_players_ai[p->index].state = AIS_REMOVE_STATION;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3855
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3856
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3857
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3858
		// Remove player stuff in that tile
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3859
		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
  3860
		if (_players_ai[p->index].state != AIS_REMOVE_TRACK) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3861
	} while (--num);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3862
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3863
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3864
static void AiStateRemoveSingleRailTile(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3865
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3866
	// 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
  3867
	if (!AiRemoveTileAndGoForward(p)) _players_ai[p->index].state = AIS_REMOVE_TRACK;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3868
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3869
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3870
static AiStateAction * const _ai_actions[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3871
	AiCase0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3872
	AiCase1,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3873
	AiStateVehLoop,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3874
	AiStateCheckReplaceVehicle,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3875
	AiStateDoReplaceVehicle,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3876
	AiStateWantNewRoute,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3877
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3878
	AiStateBuildDefaultRailBlocks,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3879
	AiStateBuildRail,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3880
	AiStateBuildRailVeh,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3881
	AiStateDeleteRailBlocks,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3882
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3883
	AiStateBuildDefaultRoadBlocks,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3884
	AiStateBuildRoad,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3885
	AiStateBuildRoadVehicles,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3886
	AiStateDeleteRoadBlocks,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3887
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3888
	AiStateAirportStuff,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3889
	AiStateBuildDefaultAirportBlocks,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3890
	AiStateBuildAircraftVehicles,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3891
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3892
	AiStateCheckShipStuff,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3893
	AiStateBuildDefaultShipBlocks,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3894
	AiStateDoShipStuff,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3895
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3896
	AiStateSellVeh,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3897
	AiStateRemoveStation,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3898
	AiStateRemoveTrack,
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  3899
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3900
	AiStateRemoveSingleRailTile
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3901
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3902
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3903
extern void ShowBuyCompanyDialog(uint player);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3904
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3905
static void AiHandleTakeover(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3906
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3907
	if (p->bankrupt_timeout != 0) {
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  3908
		p->bankrupt_timeout -= 8;
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  3909
		if (p->bankrupt_timeout > 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3910
		p->bankrupt_timeout = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3911
		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
  3912
		if (IsLocalPlayer()) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3913
			AskExitToGameMenu();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3914
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3915
		}
4845
49105d3b5a9a (svn r6771) -Codechange: Replace two macros with functions. IS_HUMAN_PLAYER and
Darkvater
parents: 4560
diff changeset
  3916
		if (IsHumanPlayer(_current_player)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3917
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3918
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3022
diff changeset
  3919
	if (p->bankrupt_asked == 255) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3920
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3921
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3922
		uint asked = p->bankrupt_asked;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3923
		Player *pp, *best_pl = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3924
		int32 best_val = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3925
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3926
		// Ask the guy with the highest performance hist.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3927
		FOR_ALL_PLAYERS(pp) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3928
			if (pp->is_active &&
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  3929
					!(asked & 1) &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3930
					pp->bankrupt_asked == 0 &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3931
					best_val < pp->old_economy[1].performance_history) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3932
				best_val = pp->old_economy[1].performance_history;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3933
				best_pl = pp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3934
			}
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  3935
			asked >>= 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3936
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3937
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3938
		// Asked all players?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3939
		if (best_val == -1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3940
			p->bankrupt_asked = 255;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3941
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3942
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3943
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
  3944
		SetBit(p->bankrupt_asked, best_pl->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3945
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3946
		if (best_pl->index == _local_player) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3947
			p->bankrupt_timeout = 4440;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3948
			ShowBuyCompanyDialog(_current_player);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3949
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3950
		}
4845
49105d3b5a9a (svn r6771) -Codechange: Replace two macros with functions. IS_HUMAN_PLAYER and
Darkvater
parents: 4560
diff changeset
  3951
		if (IsHumanPlayer(best_pl->index)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3952
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3953
		// Too little money for computer to buy it?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3954
		if (best_pl->player_money >> 1 >= p->bankrupt_value) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3955
			// 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
  3956
			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
  3957
			_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
  3958
			DoCommand(0, old_p, 0, DC_EXEC, CMD_BUY_COMPANY);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3959
			_current_player = old_p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3960
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3961
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3962
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3963
4011
db5514de70cc (svn r5222) Misc. smaller changes, mostly const, variable scope and replacing some magic numbers by enums
tron
parents: 4000
diff changeset
  3964
static void AiAdjustLoan(const Player* p)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3965
{
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
  3966
	Money base = AiGetBasePrice(p);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3967
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3968
	if (p->player_money > base * 1400) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3969
		// Decrease loan
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3970
		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
  3971
			DoCommand(0, 0, 0, DC_EXEC, CMD_DECREASE_LOAN);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3972
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3973
	} else if (p->player_money < base * 500) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3974
		// Increase loan
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3975
		if (p->current_loan < _economy.max_loan &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3976
				p->num_valid_stat_ent >= 2 &&
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  3977
				-(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
  3978
			DoCommand(0, 0, 0, DC_EXEC, CMD_INCREASE_LOAN);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3979
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3980
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3981
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3982
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3983
static void AiBuildCompanyHQ(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3984
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3985
	TileIndex tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3986
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3987
	if (p->location_of_house == 0 &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3988
			p->last_build_coordinate != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3989
		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
  3990
		DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3991
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3992
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3993
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3994
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 192
diff changeset
  3995
void AiDoGameLoop(Player *p)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3996
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3997
	if (p->bankrupt_asked != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3998
		AiHandleTakeover(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3999
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4000
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4001
192
614bba52258d (svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents: 159
diff changeset
  4002
	// 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
  4003
	//  to the patch-setting
614bba52258d (svn r193) -Fix: [OldAI] If service-interval is in %, it works correct now.
truelight
parents: 159
diff changeset
  4004
	// 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
  4005
	//  or in %
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  4006
	_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
  4007
4845
49105d3b5a9a (svn r6771) -Codechange: Replace two macros with functions. IS_HUMAN_PLAYER and
Darkvater
parents: 4560
diff changeset
  4008
	if (IsHumanPlayer(_current_player)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4009
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4010
	AiAdjustLoan(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4011
	AiBuildCompanyHQ(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4012
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4013
#if 0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4014
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4015
		static byte old_state = 99;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4016
		static bool hasdots = false;
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
  4017
		char *_ai_state_names[] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4018
			"AiCase0",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4019
			"AiCase1",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4020
			"AiStateVehLoop",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4021
			"AiStateCheckReplaceVehicle",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4022
			"AiStateDoReplaceVehicle",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4023
			"AiStateWantNewRoute",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4024
			"AiStateBuildDefaultRailBlocks",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4025
			"AiStateBuildRail",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4026
			"AiStateBuildRailVeh",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4027
			"AiStateDeleteRailBlocks",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4028
			"AiStateBuildDefaultRoadBlocks",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4029
			"AiStateBuildRoad",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4030
			"AiStateBuildRoadVehicles",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4031
			"AiStateDeleteRoadBlocks",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4032
			"AiStateAirportStuff",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4033
			"AiStateBuildDefaultAirportBlocks",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4034
			"AiStateBuildAircraftVehicles",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4035
			"AiStateCheckShipStuff",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4036
			"AiStateBuildDefaultShipBlocks",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4037
			"AiStateDoShipStuff",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4038
			"AiStateSellVeh",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4039
			"AiStateRemoveStation",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4040
			"AiStateRemoveTrack",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4041
			"AiStateRemoveSingleRailTile"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4042
		};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4043
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
  4044
		if (_players_ai[p->index].state != old_state) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4045
			if (hasdots)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4046
				printf("\n");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4047
			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
  4048
			printf("AiState: %s\n", _ai_state_names[old_state=_players_ai[p->index].state]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4049
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4050
			printf(".");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4051
			hasdots=true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4052
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4053
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4054
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4055
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
  4056
	_ai_actions[_players_ai[p->index].state](p);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4057
}
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
  4058
b769d0a63f06 (svn r11792) -Codechange: 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
b769d0a63f06 (svn r11792) -Codechange: 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
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
  4061
	    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
  4062
	    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
  4063
	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
  4064
	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
  4065
	    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
  4066
b769d0a63f06 (svn r11792) -Codechange: 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(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
  4068
	    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
  4069
	    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
  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
	    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
  4072
	    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
  4073
	    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
  4074
	    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
  4075
	    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
  4076
	    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
  4077
b769d0a63f06 (svn r11792) -Codechange: 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
	    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
  4079
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  4080
	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
  4081
	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
  4082
	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
  4083
	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
  4084
	    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
  4085
	    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
  4086
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  4087
	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
  4088
	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
  4089
	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
  4090
	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
  4091
	    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
  4092
	    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
  4093
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  4094
	    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
  4095
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  4096
	    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
  4097
	    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
  4098
	    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
  4099
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  4100
	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
  4101
	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
  4102
};
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  4103
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  4104
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
  4105
	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
  4106
	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
  4107
	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
  4108
	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
  4109
	    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
  4110
	    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
  4111
	    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
  4112
	    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
  4113
	    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
  4114
	    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
  4115
	    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
  4116
	    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
  4117
	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
  4118
};
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  4119
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  4120
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  4121
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
  4122
{
b769d0a63f06 (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8640
diff changeset
  4123
	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
  4124
	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
  4125
	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
  4126
		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
  4127
	}
8732
b18f578f7c16 (svn r11800) -Codechange: move some functions to a more logical location + some type safety.
rubidium
parents: 8726
diff changeset
  4128
}