ai/trolly/build.c
author matthijs
Wed, 22 Mar 2006 22:26:16 +0000
branch0.4.5
changeset 9958 bed516c67d61
parent 2682 94ca0b4dc53f
child 3033 e4f7c60a1742
permissions -rw-r--r--
(svn r4041) [Debian] Change next version number to 0.4.6 instead of 0.4.5.1.
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"
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
     6
#include "../../functions.h"
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2366
diff changeset
     7
#include "../../map.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
     8
#include "../../tile.h"
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2366
diff changeset
     9
#include "../../command.h"
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2366
diff changeset
    10
#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
    11
#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
    12
#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
    13
#include "../../variables.h"
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
    14
#include "../ai.h"
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    15
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    16
// Build HQ
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    17
//  Params:
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    18
//    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
    19
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
    20
{
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
    21
	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
    22
		return false;
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
    23
	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
    24
	return true;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    25
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    26
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    27
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    28
// Build station
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    29
//  Params:
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    30
//    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
    31
//    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
    32
//    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
    33
//    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
    34
//    direction : the direction of the station
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    35
//    flag : flag passed to DoCommand (normally 0 to get the cost or DC_EXEC to build it)
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
    36
int AiNew_Build_Station(Player *p, byte type, TileIndex tile, byte length, byte numtracks, byte direction, byte flag)
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
    37
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    38
	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
    39
		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
    40
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
    41
	if (type == AI_BUS)
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
    42
		return AI_DoCommand(tile, direction, RS_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
    43
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, RS_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
    45
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    46
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    47
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    48
// 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
    49
//  Params:
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    50
//   tile_a : starting point
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    51
//   tile_b : end point
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    52
//   flag : flag passed to DoCommand
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
    53
int AiNew_Build_Bridge(Player *p, TileIndex tile_a, TileIndex tile_b, byte flag)
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
    54
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    55
	int bridge_type, bridge_len, type, type2;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    56
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    57
	// Find a good bridgetype (the best money can buy)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    58
	bridge_len = GetBridgeLength(tile_a, tile_b);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    59
	type = type2 = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    60
	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
    61
		if (CheckBridge_Stuff(bridge_type, bridge_len)) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    62
			type2 = type;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    63
			type = bridge_type;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    64
			// We found two bridges, exit
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    65
			if (type2 != 0) break;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    66
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    67
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    68
	// There is only one bridge that can be build..
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    69
	if (type2 == 0 && type != 0) type2 = type;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    70
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    71
	// Now, simply, build the bridge!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    72
	if (p->ainew.tbt == AI_TRAIN)
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
    73
		return AI_DoCommand(tile_a, tile_b, (0<<8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
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
    74
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
    75
	return AI_DoCommand(tile_a, tile_b, (0x80 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    76
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    77
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    78
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    79
// Build the route part by part
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    80
// 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
    81
//  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
    82
//  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
    83
//
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    84
//  Params:
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    85
//   PathFinderInfo : Pointer to the PathFinderInfo used for AiPathFinder
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    86
//   part : Which part we need to build
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    87
//
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    88
// TODO: skip already builded road-pieces (e.g.: cityroad)
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    89
int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag)
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    90
{
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    91
	int part = PathFinderInfo->position;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    92
	byte *route_extra = PathFinderInfo->route_extra;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    93
	TileIndex *route = PathFinderInfo->route;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    94
	int dir;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    95
	int old_dir = -1;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    96
	int cost = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    97
	int res;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    98
	// 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
    99
	//  the first pieces and the last piece
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   100
	if (part < 1) part = 1;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   101
	// When we are done, stop it
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   102
	if (part >= PathFinderInfo->route_length - 1) { PathFinderInfo->position = -2; return 0; }
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   103
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   104
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   105
	if (PathFinderInfo->rail_or_road) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   106
		// Tunnel code
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   107
		if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   108
			cost += AI_DoCommand(route[part], 0, 0, flag, CMD_BUILD_TUNNEL);
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   109
			PathFinderInfo->position++;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   110
			// TODO: problems!
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   111
			if (CmdFailed(cost)) {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   112
				DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!");
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   113
				return 0;
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   114
			}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   115
			return cost;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   116
		}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   117
		// Bridge code
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   118
		if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   119
			cost += AiNew_Build_Bridge(p, route[part], route[part-1], flag);
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   120
			PathFinderInfo->position++;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   121
			// TODO: problems!
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   122
			if (CmdFailed(cost)) {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   123
				DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be build!");
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   124
				return 0;
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   125
			}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   126
			return cost;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   127
		}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   128
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   129
		// Build normal rail
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   130
		// Keep it doing till we go an other way
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   131
		if (route_extra[part-1] == 0 && route_extra[part] == 0) {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   132
			while (route_extra[part] == 0) {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   133
				// Get the current direction
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   134
				dir = AiNew_GetRailDirection(route[part-1], route[part], route[part+1]);
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   135
				// Is it the same as the last one?
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   136
				if (old_dir != -1 && old_dir != dir) break;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   137
				old_dir = dir;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   138
				// Build the tile
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   139
				res = AI_DoCommand(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL);
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   140
				if (CmdFailed(res)) {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   141
					// Problem.. let's just abort it all!
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   142
					p->ainew.state = AI_STATE_NOTHING;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   143
					return 0;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   144
				}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   145
				cost += res;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   146
				// Go to the next tile
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   147
				part++;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   148
				// Check if it is still in range..
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   149
				if (part >= PathFinderInfo->route_length - 1) break;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   150
			}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   151
			part--;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   152
		}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   153
		// We want to return the last position, so we go back one
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   154
		PathFinderInfo->position = part;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   155
	} else {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   156
		// Tunnel code
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   157
		if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   158
			cost += AI_DoCommand(route[part], 0x200, 0, flag, CMD_BUILD_TUNNEL);
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   159
			PathFinderInfo->position++;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   160
			// TODO: problems!
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   161
			if (CmdFailed(cost)) {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   162
				DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!");
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   163
				return 0;
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   164
			}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   165
			return cost;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   166
		}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   167
		// Bridge code
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   168
		if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   169
			cost += AiNew_Build_Bridge(p, route[part], route[part+1], flag);
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   170
			PathFinderInfo->position++;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   171
			// TODO: problems!
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   172
			if (CmdFailed(cost)) {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   173
				DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be build!");
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   174
				return 0;
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   175
			}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   176
			return cost;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   177
		}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   178
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   179
		// Build normal road
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   180
		// Keep it doing till we go an other way
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   181
		// EnsureNoVehicle makes sure we don't build on a tile where a vehicle is. This way
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   182
		//  it will wait till the vehicle is gone..
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   183
		if (route_extra[part-1] == 0 && route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   184
			while (route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   185
				// Get the current direction
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   186
				dir = AiNew_GetRoadDirection(route[part-1], route[part], route[part+1]);
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   187
				// Is it the same as the last one?
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   188
				if (old_dir != -1 && old_dir != dir) break;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   189
				old_dir = dir;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   190
				// There is already some road, and it is a bridge.. don't build!!!
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   191
				if (!IsTileType(route[part], MP_TUNNELBRIDGE)) {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   192
					// Build the tile
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   193
					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
   194
					// Currently, we ignore CMD_ERRORs!
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   195
					if (CmdFailed(res) && flag == DC_EXEC && !IsTileType(route[part], MP_STREET) && !EnsureNoVehicle(route[part])) {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   196
						// Problem.. let's just abort it all!
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   197
						DEBUG(ai,0)("Darn, the route could not be builded.. aborting!");
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   198
						p->ainew.state = AI_STATE_NOTHING;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   199
						return 0;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   200
					}
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
   201
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   202
					if (!CmdFailed(res)) cost += res;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   203
				}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   204
				// Go to the next tile
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   205
				part++;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   206
				// Check if it is still in range..
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   207
				if (part >= PathFinderInfo->route_length - 1) break;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   208
			}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   209
			part--;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   210
			// We want to return the last position, so we go back one
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   211
		}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   212
		if (!EnsureNoVehicle(route[part]) && flag == DC_EXEC) part--;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   213
		PathFinderInfo->position = part;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   214
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   215
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   216
	return cost;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   217
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   218
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   219
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   220
// This functions tries to find the best vehicle for this type of cargo
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   221
// It returns vehicle_id or -1 if not found
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   222
int AiNew_PickVehicle(Player *p)
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   223
{
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   224
	if (p->ainew.tbt == AI_TRAIN) {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   225
		// Not supported yet
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   226
		return -1;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   227
	} else {
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   228
		int start, count, i, ret = CMD_ERROR;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   229
		start = _cargoc.ai_roadveh_start[p->ainew.cargo];
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   230
		count = _cargoc.ai_roadveh_count[p->ainew.cargo];
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   231
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   232
		// Let's check it backwards.. we simply want to best engine available..
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2381
diff changeset
   233
		for (i = start + count - 1; i >= start; i--) {
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   234
			// Is it availiable?
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   235
			// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   236
			if (!HASBIT(GetEngine(i)->player_avail, _current_player) || GetEngine(i)->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   237
			// Can we build it?
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   238
			ret = AI_DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   239
			if (!CmdFailed(ret)) break;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   240
		}
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   241
		// We did not find a vehicle :(
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2381
diff changeset
   242
		if (CmdFailed(ret)) return -1;
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   243
		return i;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   244
	}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   245
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   246
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   247
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   248
// Builds the best vehicle possible
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
   249
int AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag)
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
   250
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   251
	int i = AiNew_PickVehicle(p);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   252
	if (i == -1) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   253
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2549
diff changeset
   254
	if (p->ainew.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
   255
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   256
	return AI_DoCommand(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   257
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   258
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
   259
int AiNew_Build_Depot(Player *p, TileIndex tile, byte 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
   260
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   261
	static const byte _roadbits_by_dir[4] = {2,1,8,4};
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
   262
	int ret, ret2;
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   263
	if (p->ainew.tbt == AI_TRAIN)
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   264
		return AI_DoCommand(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
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
   265
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   266
	ret = AI_DoCommand(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   267
	if (CmdFailed(ret)) return ret;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   268
	// Try to build the road from the depot
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   269
	ret2 = AI_DoCommand(tile + TileOffsByDir(direction), _roadbits_by_dir[direction], 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   270
	// If it fails, ignore it..
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   271
	if (CmdFailed(ret2)) return ret;
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   272
	return ret + ret2;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   273
}