ai_build.c
author tron
Fri, 26 Aug 2005 20:56:48 +0000
changeset 2366 346cbe2de606
parent 2186 db48cf29b983
permissions -rw-r--r--
(svn r2892) Fix indentation
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
     3
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1713
diff changeset
     4
#include "openttd.h"
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1217
diff changeset
     5
#include "debug.h"
2163
b17b313113a0 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     6
#include "functions.h"
679
04ca2cd69420 (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents: 193
diff changeset
     7
#include "map.h"
1209
2e00193652b2 (svn r1713) Split off several functions which query/set information about a single tile from map.h and put them into a seperate file tile.h
tron
parents: 1035
diff changeset
     8
#include "tile.h"
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
     9
#include "command.h"
2096
32de4f127e79 (svn r2606) -Codechange: renamed ai.c to ai_old.c, and ai.h to ai_new.h to make room
truelight
parents: 1977
diff changeset
    10
#include "ai_new.h"
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    11
#include "engine.h"
1217
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
    12
#include "station.h"
2159
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2096
diff changeset
    13
#include "variables.h"
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    14
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    15
// Build HQ
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    16
//  Params:
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    17
//    tile : tile where HQ is going to be build
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
    18
bool AiNew_Build_CompanyHQ(Player *p, TileIndex tile)
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
    19
{
1713
659ca3025cc5 (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
    20
	if (CmdFailed(DoCommandByTile(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
    21
		return false;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    22
	DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    23
	return true;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    24
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    25
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    26
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    27
// Build station
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    28
//  Params:
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    29
//    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
    30
//    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
    31
//    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
    32
//    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
    33
//    direction : the direction of the station
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    34
//    flag : flag passed to DoCommand (normally 0 to get the cost or DC_EXEC to build it)
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
    35
int AiNew_Build_Station(Player *p, byte type, TileIndex tile, byte length, byte numtracks, byte direction, byte flag)
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
    36
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    37
	if (type == AI_TRAIN)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    38
		return DoCommandByTile(tile, direction + (numtracks << 8) + (length << 16), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_STATION);
1713
659ca3025cc5 (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
    39
659ca3025cc5 (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
	if (type == AI_BUS)
1217
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
    41
		return DoCommandByTile(tile, direction, RS_BUS, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
1713
659ca3025cc5 (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
    42
659ca3025cc5 (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
	return DoCommandByTile(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
    44
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    45
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    46
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    47
// 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
    48
//  Params:
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    49
//   tile_a : starting point
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    50
//   tile_b : end point
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    51
//   flag : flag passed to DoCommand
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
    52
int AiNew_Build_Bridge(Player *p, TileIndex tile_a, TileIndex tile_b, byte flag)
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
    53
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    54
	int bridge_type, bridge_len, type, type2;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    55
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    56
	// 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
    57
	bridge_len = GetBridgeLength(tile_a, tile_b);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    58
	type = type2 = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    59
	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
    60
		if (CheckBridge_Stuff(bridge_type, bridge_len)) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    61
			type2 = type;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    62
			type = bridge_type;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    63
			// We found two bridges, exit
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    64
			if (type2 != 0) break;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    65
		}
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
	// 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
    68
	if (type2 == 0 && type != 0) type2 = type;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    69
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    70
	// Now, simply, build the bridge!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    71
	if (p->ainew.tbt == AI_TRAIN)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    72
		return DoCommandByTile(tile_a, tile_b, (0<<8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
1713
659ca3025cc5 (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
    73
659ca3025cc5 (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
	return DoCommandByTile(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
    75
}
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
// Build the route part by part
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    79
// 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
    80
//  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
    81
//  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
    82
//
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    83
//  Params:
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    84
//   PathFinderInfo : Pointer to the PathFinderInfo used for AiPathFinder
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    85
//   part : Which part we need to build
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    86
//
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    87
// TODO: skip already builded road-pieces (e.g.: cityroad)
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    88
int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag)
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    89
{
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    90
	int part = PathFinderInfo->position;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    91
	byte *route_extra = PathFinderInfo->route_extra;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    92
	TileIndex *route = PathFinderInfo->route;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    93
	int dir;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    94
	int old_dir = -1;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    95
	int cost = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    96
	int res;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    97
	// 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
    98
	//  the first pieces and the last piece
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    99
	if (part < 1) part = 1;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   100
	// When we are done, stop it
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   101
	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
   102
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   103
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   104
	if (PathFinderInfo->rail_or_road) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   105
		// Tunnel code
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   106
		if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   107
			cost += DoCommandByTile(route[part], 0, 0, flag, CMD_BUILD_TUNNEL);
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   108
			PathFinderInfo->position++;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   109
			// TODO: problems!
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   110
			if (CmdFailed(cost)) {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   111
				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
   112
				return 0;
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   113
			}
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   114
			return cost;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   115
		}
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   116
		// Bridge code
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   117
		if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   118
			cost += AiNew_Build_Bridge(p, route[part], route[part-1], flag);
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   119
			PathFinderInfo->position++;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   120
			// TODO: problems!
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   121
			if (CmdFailed(cost)) {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   122
				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
   123
				return 0;
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   124
			}
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   125
			return cost;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   126
		}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   127
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   128
		// Build normal rail
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   129
		// Keep it doing till we go an other way
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   130
		if (route_extra[part-1] == 0 && route_extra[part] == 0) {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   131
			while (route_extra[part] == 0) {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   132
				// Get the current direction
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   133
				dir = AiNew_GetRailDirection(route[part-1], route[part], route[part+1]);
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   134
				// Is it the same as the last one?
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   135
				if (old_dir != -1 && old_dir != dir) break;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   136
				old_dir = dir;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   137
				// Build the tile
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   138
				res = DoCommandByTile(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL);
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   139
				if (CmdFailed(res)) {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   140
					// Problem.. let's just abort it all!
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   141
					p->ainew.state = AI_STATE_NOTHING;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   142
					return 0;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   143
				}
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   144
				cost += res;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   145
				// Go to the next tile
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   146
				part++;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   147
				// Check if it is still in range..
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   148
				if (part >= PathFinderInfo->route_length - 1) break;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   149
			}
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   150
			part--;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   151
		}
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   152
		// We want to return the last position, so we go back one
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   153
		PathFinderInfo->position = part;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   154
	} else {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   155
		// Tunnel code
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   156
		if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   157
			cost += DoCommandByTile(route[part], 0x200, 0, flag, CMD_BUILD_TUNNEL);
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   158
			PathFinderInfo->position++;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   159
			// TODO: problems!
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   160
			if (CmdFailed(cost)) {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   161
				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
   162
				return 0;
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   163
			}
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   164
			return cost;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   165
		}
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   166
		// Bridge code
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   167
		if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   168
			cost += AiNew_Build_Bridge(p, route[part], route[part+1], flag);
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   169
			PathFinderInfo->position++;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   170
			// TODO: problems!
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   171
			if (CmdFailed(cost)) {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   172
				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
   173
				return 0;
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   174
			}
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   175
			return cost;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   176
		}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   177
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   178
		// Build normal road
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   179
		// Keep it doing till we go an other way
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   180
		// EnsureNoVehicle makes sure we don't build on a tile where a vehicle is. This way
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   181
		//  it will wait till the vehicle is gone..
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   182
		if (route_extra[part-1] == 0 && route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   183
			while (route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   184
				// Get the current direction
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   185
				dir = AiNew_GetRoadDirection(route[part-1], route[part], route[part+1]);
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   186
				// Is it the same as the last one?
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   187
				if (old_dir != -1 && old_dir != dir) break;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   188
				old_dir = dir;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   189
				// There is already some road, and it is a bridge.. don't build!!!
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   190
				if (!IsTileType(route[part], MP_TUNNELBRIDGE)) {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   191
					// Build the tile
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   192
					res = DoCommandByTile(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD);
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   193
					// Currently, we ignore CMD_ERRORs!
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   194
					if (CmdFailed(res) && flag == DC_EXEC && !IsTileType(route[part], MP_STREET) && !EnsureNoVehicle(route[part])) {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   195
						// Problem.. let's just abort it all!
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   196
						DEBUG(ai,0)("Darn, the route could not be builded.. aborting!");
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   197
						p->ainew.state = AI_STATE_NOTHING;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   198
						return 0;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   199
					}
1713
659ca3025cc5 (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
   200
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   201
					if (!CmdFailed(res)) cost += res;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   202
				}
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   203
				// Go to the next tile
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   204
				part++;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   205
				// Check if it is still in range..
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   206
				if (part >= PathFinderInfo->route_length - 1) break;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   207
			}
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   208
			part--;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   209
			// We want to return the last position, so we go back one
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   210
		}
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   211
		if (!EnsureNoVehicle(route[part]) && flag == DC_EXEC) part--;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   212
		PathFinderInfo->position = part;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   213
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   214
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   215
	return cost;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   216
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   217
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   218
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   219
// 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
   220
// It returns vehicle_id or -1 if not found
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   221
int AiNew_PickVehicle(Player *p)
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   222
{
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   223
	if (p->ainew.tbt == AI_TRAIN) {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   224
		// Not supported yet
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   225
		return -1;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   226
	} else {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   227
		int start, count, i, ret = CMD_ERROR;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   228
		start = _cargoc.ai_roadveh_start[p->ainew.cargo];
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   229
		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
   230
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   231
		// Let's check it backwards.. we simply want to best engine available..
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   232
		for (i=start+count-1;i>=start;i--) {
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   233
			// Is it availiable?
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   234
			// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   235
			if (!HASBIT(GetEngine(i)->player_avail, _current_player) || GetEngine(i)->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   236
			// Can we build it?
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   237
			ret = DoCommandByTile(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   238
			if (!CmdFailed(ret)) break;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   239
		}
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   240
		// We did not find a vehicle :(
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   241
		if (CmdFailed(ret)) { return -1; }
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   242
		return i;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   243
	}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   244
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   245
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   246
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   247
// Builds the best vehicle possible
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
   248
int AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag)
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
   249
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   250
	int i = AiNew_PickVehicle(p);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   251
	if (i == -1) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   252
1713
659ca3025cc5 (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
   253
	if (p->ainew.tbt == AI_TRAIN)
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   254
	return CMD_ERROR;
1713
659ca3025cc5 (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
659ca3025cc5 (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
   256
	return DoCommandByTile(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
37bbebf94434 (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
659ca3025cc5 (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
659ca3025cc5 (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
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   263
	if (p->ainew.tbt == AI_TRAIN)
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   264
		return DoCommandByTile(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
1713
659ca3025cc5 (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
2366
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   266
	ret = DoCommandByTile(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   267
	if (CmdFailed(ret)) return ret;
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   268
	// Try to build the road from the depot
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   269
	ret2 = DoCommandByTile(tile + TileOffsByDir(direction), _roadbits_by_dir[direction], 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   270
	// If it fails, ignore it..
346cbe2de606 (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   271
	if (CmdFailed(ret2)) return ret;
346cbe2de606 (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
}