src/ai/trolly/build.cpp
author rubidium
Sun, 03 Feb 2008 20:34:26 +0000
branchNewGRF_ports
changeset 6877 889301acc299
parent 6872 1c4a4a609f85
child 10184 fcf5fb2548eb
permissions -rw-r--r--
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
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: 2366
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: 2366
diff changeset
     4
#include "../../openttd.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: 2366
diff changeset
     5
#include "../../debug.h"
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3033
diff changeset
     6
#include "../../road_map.h"
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
     7
#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: 2366
diff changeset
     8
#include "trolly.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: 2366
diff changeset
     9
#include "../../engine.h"
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2366
diff changeset
    10
#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: 2366
diff changeset
    11
#include "../../variables.h"
3359
d4316b1af327 (svn r4154) -Moved MAX_BRIDGES in bridge.h and made it an enum. This makes two drops ...
celestar
parents: 3157
diff changeset
    12
#include "../../bridge.h"
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    13
#include "../../vehicle_func.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    14
#include "../../vehicle_base.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    15
#include "../../player_base.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    16
#include "../../player_func.h"
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
    17
#include "../ai.h"
6877
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    18
#include "../../tunnelbridge.h"
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    19
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    20
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    21
// Build HQ
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    22
//  Params:
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    23
//    tile : tile where HQ is going to be build
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
    24
bool AiNew_Build_CompanyHQ(Player *p, TileIndex tile)
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
    25
{
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
    26
	if (CmdFailed(AI_DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ)))
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    27
		return false;
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
    28
	AI_DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    29
	return true;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    30
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    31
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    32
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    33
// Build station
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    34
//  Params:
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    35
//    type : AI_TRAIN/AI_BUS/AI_TRUCK : indicates the type of station
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    36
//    tile : tile where station is going to be build
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    37
//    length : in case of AI_TRAIN: length of station
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    38
//    numtracks : in case of AI_TRAIN: tracks of station
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    39
//    direction : the direction of the station
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    40
//    flag : flag passed to DoCommand (normally 0 to get the cost or DC_EXEC to build it)
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
    41
CommandCost AiNew_Build_Station(Player *p, byte type, TileIndex tile, byte length, byte numtracks, byte direction, byte flag)
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
    42
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    43
	if (type == AI_TRAIN)
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
    44
		return AI_DoCommand(tile, direction + (numtracks << 8) + (length << 16), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_STATION);
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: 1299
diff changeset
    45
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: 1299
diff changeset
    46
	if (type == AI_BUS)
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6087
diff changeset
    47
		return AI_DoCommand(tile, direction, ROADTYPES_ROAD << 2 | RoadStop::BUS, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
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: 1299
diff changeset
    48
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6087
diff changeset
    49
	return AI_DoCommand(tile, direction, ROADTYPES_ROAD << 2 | RoadStop::TRUCK, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    50
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    51
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    52
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    53
// Builds a brdige. The second best out of the ones available for this player
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    54
//  Params:
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    55
//   tile_a : starting point
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    56
//   tile_b : end point
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    57
//   flag : flag passed to DoCommand
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
    58
CommandCost AiNew_Build_Bridge(Player *p, TileIndex tile_a, TileIndex tile_b, byte flag)
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
    59
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    60
	int bridge_type, bridge_len, type, type2;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    61
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    62
	// Find a good bridgetype (the best money can buy)
6877
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    63
	bridge_len = GetTunnelBridgeLength(tile_a, tile_b);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    64
	type = type2 = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    65
	for (bridge_type = MAX_BRIDGES-1; bridge_type >= 0; bridge_type--) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    66
		if (CheckBridge_Stuff(bridge_type, bridge_len)) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    67
			type2 = type;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    68
			type = bridge_type;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    69
			// We found two bridges, exit
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    70
			if (type2 != 0) break;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    71
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    72
	}
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: 3946
diff changeset
    73
	// There is only one bridge that can be built
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    74
	if (type2 == 0 && type != 0) type2 = type;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    75
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    76
	// Now, simply, build the bridge!
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    77
	if (_players_ainew[p->index].tbt == AI_TRAIN) {
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: 3946
diff changeset
    78
		return AI_DoCommand(tile_a, tile_b, (0x00 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
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: 3946
diff changeset
    79
	} else {
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6087
diff changeset
    80
		return AI_DoCommand(tile_a, tile_b, ((0x80 | ROADTYPES_ROAD) << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3946
diff changeset
    81
	}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    82
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    83
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    84
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    85
// Build the route part by part
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    86
// Basicly what this function do, is build that amount of parts of the route
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    87
//  that go in the same direction. It sets 'part' to the last part of the route builded.
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    88
//  The return value is the cost for the builded parts
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    89
//
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    90
//  Params:
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    91
//   PathFinderInfo : Pointer to the PathFinderInfo used for AiPathFinder
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    92
//   part : Which part we need to build
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    93
//
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    94
// TODO: skip already builded road-pieces (e.g.: cityroad)
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
    95
CommandCost AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag)
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    96
{
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    97
	int part = PathFinderInfo->position;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    98
	byte *route_extra = PathFinderInfo->route_extra;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    99
	TileIndex *route = PathFinderInfo->route;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   100
	int dir;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   101
	int old_dir = -1;
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   102
	CommandCost cost;
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   103
	CommandCost res;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   104
	// We need to calculate the direction with the parent of the parent.. so we skip
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   105
	//  the first pieces and the last piece
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   106
	if (part < 1) part = 1;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   107
	// When we are done, stop it
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: 3946
diff changeset
   108
	if (part >= PathFinderInfo->route_length - 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: 3946
diff changeset
   109
		PathFinderInfo->position = -2;
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   110
		return CommandCost();
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: 3946
diff changeset
   111
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   112
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   113
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   114
	if (PathFinderInfo->rail_or_road) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   115
		// Tunnel code
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   116
		if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   117
			cost.AddCost(AI_DoCommand(route[part], 0, 0, flag, CMD_BUILD_TUNNEL));
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   118
			PathFinderInfo->position++;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   119
			// TODO: problems!
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   120
			if (CmdFailed(cost)) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5032
diff changeset
   121
				DEBUG(ai, 0, "[BuildPath] tunnel could not be built (0x%X)", route[part]);
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   122
				return CommandCost();
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   123
			}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   124
			return cost;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   125
		}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   126
		// Bridge code
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   127
		if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   128
			cost.AddCost(AiNew_Build_Bridge(p, route[part], route[part - 1], flag));
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   129
			PathFinderInfo->position++;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   130
			// TODO: problems!
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   131
			if (CmdFailed(cost)) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5032
diff changeset
   132
				DEBUG(ai, 0, "[BuildPath] bridge could not be built (0x%X, 0x%X)", route[part], route[part - 1]);
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   133
				return CommandCost();
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   134
			}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   135
			return cost;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   136
		}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   137
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   138
		// Build normal rail
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   139
		// Keep it doing till we go an other way
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: 3946
diff changeset
   140
		if (route_extra[part - 1] == 0 && route_extra[part] == 0) {
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   141
			while (route_extra[part] == 0) {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   142
				// Get the current direction
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   143
				dir = AiNew_GetRailDirection(route[part-1], route[part], route[part+1]);
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   144
				// Is it the same as the last one?
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   145
				if (old_dir != -1 && old_dir != dir) break;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   146
				old_dir = dir;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   147
				// Build the tile
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   148
				res = AI_DoCommand(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL);
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   149
				if (CmdFailed(res)) {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   150
					// Problem.. let's just abort it all!
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   151
					_players_ainew[p->index].state = AI_STATE_NOTHING;
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   152
					return CommandCost();
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   153
				}
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   154
				cost.AddCost(res);
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   155
				// Go to the next tile
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   156
				part++;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   157
				// Check if it is still in range..
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   158
				if (part >= PathFinderInfo->route_length - 1) break;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   159
			}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   160
			part--;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   161
		}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   162
		// We want to return the last position, so we go back one
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   163
		PathFinderInfo->position = part;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   164
	} else {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   165
		// Tunnel code
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   166
		if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   167
			cost.AddCost(AI_DoCommand(route[part], 0x200 | ROADTYPES_ROAD, 0, flag, CMD_BUILD_TUNNEL));
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   168
			PathFinderInfo->position++;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   169
			// TODO: problems!
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   170
			if (CmdFailed(cost)) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5032
diff changeset
   171
				DEBUG(ai, 0, "[BuildPath] tunnel could not be built (0x%X)", route[part]);
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   172
				return CommandCost();
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   173
			}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   174
			return cost;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   175
		}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   176
		// Bridge code
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   177
		if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   178
			cost.AddCost(AiNew_Build_Bridge(p, route[part], route[part + 1], flag));
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   179
			PathFinderInfo->position++;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   180
			// TODO: problems!
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   181
			if (CmdFailed(cost)) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5032
diff changeset
   182
				DEBUG(ai, 0, "[BuildPath] bridge could not be built (0x%X, 0x%X)", route[part], route[part + 1]);
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   183
				return CommandCost();
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   184
			}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   185
			return cost;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   186
		}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   187
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   188
		// Build normal road
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   189
		// Keep it doing till we go an other way
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6743
diff changeset
   190
		// EnsureNoVehicleOnGround makes sure we don't build on a tile where a vehicle is. This way
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   191
		//  it will wait till the vehicle is gone..
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6743
diff changeset
   192
		if (route_extra[part-1] == 0 && route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicleOnGround(route[part]))) {
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6743
diff changeset
   193
			while (route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicleOnGround(route[part]))) {
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   194
				// Get the current direction
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   195
				dir = AiNew_GetRoadDirection(route[part-1], route[part], route[part+1]);
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   196
				// Is it the same as the last one?
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   197
				if (old_dir != -1 && old_dir != dir) break;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   198
				old_dir = dir;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   199
				// There is already some road, and it is a bridge.. don't build!!!
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   200
				if (!IsTileType(route[part], MP_TUNNELBRIDGE)) {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   201
					// Build the tile
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   202
					res = AI_DoCommand(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD);
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   203
					// Currently, we ignore CMD_ERRORs!
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6743
diff changeset
   204
					if (CmdFailed(res) && flag == DC_EXEC && !IsTileType(route[part], MP_ROAD) && !EnsureNoVehicleOnGround(route[part])) {
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   205
						// Problem.. let's just abort it all!
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5032
diff changeset
   206
						DEBUG(ai, 0, "[BuidPath] route building failed at tile 0x%X, aborting", route[part]);
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   207
						_players_ainew[p->index].state = AI_STATE_NOTHING;
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   208
						return CommandCost();
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   209
					}
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: 1299
diff changeset
   210
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   211
					if (CmdSucceeded(res)) cost.AddCost(res);
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   212
				}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   213
				// Go to the next tile
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   214
				part++;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   215
				// Check if it is still in range..
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   216
				if (part >= PathFinderInfo->route_length - 1) break;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   217
			}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   218
			part--;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   219
			// We want to return the last position, so we go back one
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   220
		}
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6743
diff changeset
   221
		if (!EnsureNoVehicleOnGround(route[part]) && flag == DC_EXEC) part--;
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   222
		PathFinderInfo->position = part;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   223
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   224
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   225
	return cost;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   226
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   227
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   228
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   229
// This functions tries to find the best vehicle for this type of cargo
3885
327cdee1f0b6 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3359
diff changeset
   230
// It returns INVALID_ENGINE if not suitable engine is found
327cdee1f0b6 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3359
diff changeset
   231
EngineID AiNew_PickVehicle(Player *p)
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   232
{
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   233
	if (_players_ainew[p->index].tbt == AI_TRAIN) {
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   234
		// Not supported yet
3885
327cdee1f0b6 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3359
diff changeset
   235
		return INVALID_ENGINE;
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   236
	} else {
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: 5016
diff changeset
   237
		EngineID best_veh_index = INVALID_ENGINE;
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: 5016
diff changeset
   238
		int32 best_veh_rating = 0;
3885
327cdee1f0b6 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3359
diff changeset
   239
		EngineID i;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   240
5030
dec51dbf1380 (svn r7072) -Fix (r7070): Go up, not down, through the engines. And replace the comment too...
peter1138
parents: 5029
diff changeset
   241
		/* Loop through all road vehicles */
6877
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   242
		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: 5016
diff changeset
   243
			const RoadVehicleInfo *rvi = RoadVehInfo(i);
3887
3e44ae3b1e7c (svn r4943) uint tile -> TileIndex tile, byte player -> PlayerID player
tron
parents: 3885
diff changeset
   244
			const Engine* e = GetEngine(i);
3885
327cdee1f0b6 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3359
diff changeset
   245
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: 5015
diff changeset
   246
			/* Skip vehicles which can't take our cargo type */
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   247
			if (rvi->cargo_type != _players_ainew[p->index].cargo && !CanRefitTo(i, _players_ainew[p->index].cargo)) 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: 5015
diff changeset
   248
6877
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   249
			/* Skip trams */
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   250
			if (HasBit(EngInfo(i)->misc_flags, EF_ROAD_TRAM)) continue;
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   251
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   252
			// Is it availiable?
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   253
			// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6743
diff changeset
   254
			if (!HasBit(e->player_avail, _current_player) || e->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) 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: 5016
diff changeset
   255
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: 5016
diff changeset
   256
			/* Rate and compare the engine by speed & capacity */
6877
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   257
			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: 5016
diff changeset
   258
			if (rating <= best_veh_rating) continue;
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: 5016
diff changeset
   259
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   260
			// Can we build it?
6877
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   261
			CommandCost ret = AI_DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
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: 5016
diff changeset
   262
			if (CmdFailed(ret)) continue;
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: 5016
diff changeset
   263
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: 5016
diff changeset
   264
			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: 5016
diff changeset
   265
			best_veh_index = i;
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   266
		}
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: 5016
diff changeset
   267
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: 5016
diff changeset
   268
		return best_veh_index;
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   269
	}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   270
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   271
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   272
3946
c9e039a60682 (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   273
void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2)
c9e039a60682 (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   274
{
c9e039a60682 (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   275
	Player* p = GetPlayer(_current_player);
c9e039a60682 (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   276
c9e039a60682 (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   277
	if (success) {
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   278
		_players_ainew[p->index].state = AI_STATE_GIVE_ORDERS;
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   279
		_players_ainew[p->index].veh_id = _new_vehicle_id;
5031
940b1fec4220 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5030
diff changeset
   280
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   281
		if (GetVehicle(_players_ainew[p->index].veh_id)->cargo_type != _players_ainew[p->index].cargo) {
5031
940b1fec4220 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5030
diff changeset
   282
			/* Cargo type doesn't match, so refit it */
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   283
			if (CmdFailed(DoCommand(tile, _players_ainew[p->index].veh_id, _players_ainew[p->index].cargo, DC_EXEC, CMD_REFIT_ROAD_VEH))) {
5031
940b1fec4220 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5030
diff changeset
   284
				/* Refit failed, so sell the vehicle */
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   285
				DoCommand(tile, _players_ainew[p->index].veh_id, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   286
				_players_ainew[p->index].state = AI_STATE_NOTHING;
5031
940b1fec4220 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5030
diff changeset
   287
			}
940b1fec4220 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5030
diff changeset
   288
		}
3946
c9e039a60682 (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   289
	} else {
c9e039a60682 (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   290
		/* XXX this should be handled more gracefully */
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   291
		_players_ainew[p->index].state = AI_STATE_NOTHING;
3946
c9e039a60682 (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   292
	}
c9e039a60682 (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   293
}
c9e039a60682 (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   294
c9e039a60682 (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   295
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   296
// Builds the best vehicle possible
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   297
CommandCost AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag)
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
   298
{
3885
327cdee1f0b6 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3359
diff changeset
   299
	EngineID i = AiNew_PickVehicle(p);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   300
3885
327cdee1f0b6 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3359
diff changeset
   301
	if (i == INVALID_ENGINE) return CMD_ERROR;
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   302
	if (_players_ainew[p->index].tbt == AI_TRAIN) return CMD_ERROR;
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: 1299
diff changeset
   303
3946
c9e039a60682 (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   304
	if (flag & DC_EXEC) {
c9e039a60682 (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   305
		return AI_DoCommandCc(tile, i, 0, flag, CMD_BUILD_ROAD_VEH, CcAI);
c9e039a60682 (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   306
	} else {
c9e039a60682 (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   307
		return AI_DoCommand(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
c9e039a60682 (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   308
	}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   309
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   310
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   311
CommandCost AiNew_Build_Depot(Player* p, TileIndex tile, DiagDirection direction, byte flag)
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: 1299
diff changeset
   312
{
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   313
	CommandCost ret, ret2;
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   314
	if (_players_ainew[p->index].tbt == AI_TRAIN) {
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   315
		return AI_DoCommand(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   316
	} else {
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   317
		ret = AI_DoCommand(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   318
		if (CmdFailed(ret2)) return ret;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   319
		// Try to build the road from the depot
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4077
diff changeset
   320
		ret2 = AI_DoCommand(tile + TileOffsByDiagDir(direction), DiagDirToRoadBits(ReverseDiagDir(direction)), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   321
		// If it fails, ignore it..
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   322
		if (CmdFailed(ret2)) return ret;
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   323
		ret.AddCost(ret2);
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   324
		return ret;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   325
	}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   326
}