npf.c
author tron
Sun, 11 Sep 2005 15:01:00 +0000
changeset 2416 d3f8a53cedf6
parent 2403 f339737b38bc
child 2493 d834d0c1502a
permissions -rw-r--r--
(svn r2942) Staticise some functions
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2182
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2182
diff changeset
     2
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
     3
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1857
diff changeset
     4
#include "openttd.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1248
diff changeset
     5
#include "debug.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2115
diff changeset
     6
#include "functions.h"
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
     7
#include "npf.h"
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
     8
#include "aystar.h"
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
     9
#include "macros.h"
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    10
#include "pathfind.h"
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    11
#include "station.h"
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    12
#include "tile.h"
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
    13
#include "depot.h"
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    14
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
    15
static AyStar _npf_aystar;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    16
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    17
/* The cost of each trackdir. A diagonal piece is the full NPF_TILE_LENGTH,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    18
 * the shorter piece is sqrt(2)/2*NPF_TILE_LENGTH =~ 0.7071
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    19
 */
1677
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
    20
#define NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH)
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
    21
static const uint _trackdir_length[TRACKDIR_END] = {
1463
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
    22
	NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH,
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
    23
	0, 0,
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
    24
	NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    25
};
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    26
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    27
/**
2403
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    28
 * Calculates the minimum distance traveled to get from t0 to t1 when only
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    29
 * using tracks (ie, only making 45 degree turns). Returns the distance in the
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    30
 * NPF scale, ie the number of full tiles multiplied by NPF_TILE_LENGTH to
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    31
 * prevent rounding.
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    32
 */
2416
d3f8a53cedf6 (svn r2942) Staticise some functions
tron
parents: 2403
diff changeset
    33
static uint NPFDistanceTrack(TileIndex t0, TileIndex t1)
2403
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    34
{
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    35
	const uint dx = abs(TileX(t0) - TileX(t1));
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    36
	const uint dy = abs(TileY(t0) - TileY(t1));
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    37
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    38
	const uint straightTracks = 2 * min(dx, dy); /* The number of straight (not full length) tracks */
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    39
	/* OPTIMISATION:
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    40
	 * Original: diagTracks = max(dx, dy) - min(dx,dy);
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    41
	 * Proof:
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    42
	 * (dx+dy) - straightTracks  == (min + max) - straightTracks = min + max - 2 * min = max - min */
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    43
	const uint diagTracks = dx + dy - straightTracks; /* The number of diagonal (full tile length) tracks. */
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    44
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    45
	/* Don't factor out NPF_TILE_LENGTH below, this will round values and lose
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    46
	 * precision */
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    47
	return diagTracks * NPF_TILE_LENGTH + straightTracks * NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH;
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    48
}
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    49
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    50
/**
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    51
 * Check if a rail track is the end of the line. Will also consider 1-way signals to be the end of a line.
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    52
 * @param tile The tile on which the current track is.
2029
4a678baaaae5 (svn r2538) - Fix: Use IsCompatibleRailType() function instead of checking this yourself.
hackykid
parents: 2009
diff changeset
    53
 * @param trackdir The (track)direction in which you want to look.
4a678baaaae5 (svn r2538) - Fix: Use IsCompatibleRailType() function instead of checking this yourself.
hackykid
parents: 2009
diff changeset
    54
 * @param enginetype The type of the engine for which we are checking this.
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    55
 */
2416
d3f8a53cedf6 (svn r2942) Staticise some functions
tron
parents: 2403
diff changeset
    56
static bool IsEndOfLine(TileIndex tile, Trackdir trackdir, RailType enginetype)
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    57
{
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    58
	byte exitdir = TrackdirToExitdir(trackdir);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    59
	TileIndex dst_tile;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    60
	uint32 ts;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    61
2403
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    62
	/* Can always go into a tunnel */
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2029
diff changeset
    63
	if (IsTileType(tile, MP_TUNNELBRIDGE) && (_m[tile].m5 & 0xF0)==0 && (_m[tile].m5 & 3) == exitdir)
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    64
		return false;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    65
2403
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    66
	/* Cannot go through the back of a depot */
2009
e5981b336208 (svn r2517) - Fix: [pbs] Detect end-of-lines properly regarding depots.
hackykid
parents: 2008
diff changeset
    67
	if (IsTileDepotType(tile, TRANSPORT_RAIL) && (exitdir != GetDepotDirection(tile, TRANSPORT_RAIL)))
e5981b336208 (svn r2517) - Fix: [pbs] Detect end-of-lines properly regarding depots.
hackykid
parents: 2008
diff changeset
    68
		return true;
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    69
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    70
	/* Calculate next tile */
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    71
	dst_tile = tile + TileOffsByDir(exitdir);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    72
	// determine the track status on the next tile.
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    73
	ts = GetTileTrackStatus(dst_tile, TRANSPORT_RAIL) & TrackdirReachesTrackdirs(trackdir);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    74
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    75
	// when none of the trackdir bits are set, we cant enter the new tile
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    76
	if ( (ts & TRACKDIR_BIT_MASK) == 0)
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    77
		return true;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    78
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    79
	{
2009
e5981b336208 (svn r2517) - Fix: [pbs] Detect end-of-lines properly regarding depots.
hackykid
parents: 2008
diff changeset
    80
		byte dst_type = GetTileRailType(dst_tile, exitdir);
2029
4a678baaaae5 (svn r2538) - Fix: Use IsCompatibleRailType() function instead of checking this yourself.
hackykid
parents: 2009
diff changeset
    81
		if (!IsCompatibleRail(enginetype, dst_type))
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    82
			return true;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    83
		if (GetTileOwner(tile) != GetTileOwner(dst_tile))
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    84
			return true;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    85
2403
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    86
		/* Prevent us from entering a depot from behind */
2009
e5981b336208 (svn r2517) - Fix: [pbs] Detect end-of-lines properly regarding depots.
hackykid
parents: 2008
diff changeset
    87
		if (IsTileDepotType(dst_tile, TRANSPORT_RAIL) && (exitdir != ReverseDiagdir(GetDepotDirection(dst_tile, TRANSPORT_RAIL))))
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    88
			return true;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    89
2403
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    90
		/* Prevent us from falling off a slope into a tunnel exit */
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    91
		if (IsTileType(dst_tile, MP_TUNNELBRIDGE) && (_m[dst_tile].m5 & 0xF0)==0 && (DiagDirection)(_m[dst_tile].m5 & 3) == ReverseDiagdir(exitdir))
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    92
				return true;
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    93
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    94
		/* Check for oneway signal against us */
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    95
		if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TYPE_SIGNALS) {
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    96
			if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(FindFirstBit2x64(ts))) && !HasSignalOnTrackdir(dst_tile, FindFirstBit2x64(ts)))
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    97
				// if one way signal not pointing towards us, stop going in this direction.
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    98
				return true;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    99
		}
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   100
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   101
		return false;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   102
	}
2182
89e6e39734a4 (svn r2696) Remove stray semicolons
tron
parents: 2164
diff changeset
   103
}
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   104
2075
7f0ca01392db (svn r2585) - Fix [Makefile]: some small cleanups, remove warnings, and add mersenne to makefile (Luca)
Darkvater
parents: 2049
diff changeset
   105
#if 0
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   106
static uint NTPHash(uint key1, uint key2)
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   107
{
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
   108
	/* This function uses the old hash, which is fixed on 10 bits (1024 buckets) */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   109
	return PATHFIND_HASH_TILE(key1);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   110
}
2075
7f0ca01392db (svn r2585) - Fix [Makefile]: some small cleanups, remove warnings, and add mersenne to makefile (Luca)
Darkvater
parents: 2049
diff changeset
   111
#endif
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   112
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   113
/**
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   114
 * Calculates a hash value for use in the NPF.
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   115
 * @param key1	The TileIndex of the tile to hash
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   116
 * @param key1	The Trackdir of the track on the tile.
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   117
 *
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   118
 * @todo	Think of a better hash.
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   119
 */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   120
static uint NPFHash(uint key1, uint key2)
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
   121
{
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
   122
	/* TODO: think of a better hash? */
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
   123
	uint part1 = TileX(key1) & NPF_HASH_HALFMASK;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
   124
	uint part2 = TileY(key1) & NPF_HASH_HALFMASK;
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   125
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   126
	assert(IsValidTrackdir(key2));
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   127
	assert(IsValidTile(key1));
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   128
	return ((((part1 << NPF_HASH_HALFBITS) | part2)) + (NPF_HASH_SIZE * key2 / TRACKDIR_END)) % NPF_HASH_SIZE;
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
   129
}
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
   130
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   131
static int32 NPFCalcZero(AyStar* as, AyStarNode* current, OpenListNode* parent)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   132
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   133
	return 0;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   134
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   135
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   136
/* Calcs the tile of given station that is closest to a given tile
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   137
 * for this we assume the station is a rectangle,
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   138
 * as defined by its top tile (st->train_tile) and its width/height (st->trainst_w, st->trainst_h)
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   139
 */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   140
static TileIndex CalcClosestStationTile(StationID station, TileIndex tile)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   141
{
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   142
	const Station* st = GetStation(station);
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   143
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   144
	uint minx = TileX(st->train_tile);  // topmost corner of station
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   145
	uint miny = TileY(st->train_tile);
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   146
	uint maxx = minx + st->trainst_w - 1; // lowermost corner of station
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   147
	uint maxy = miny + st->trainst_h - 1;
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   148
	uint x;
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   149
	uint y;
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   150
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   151
	// we are going the aim for the x coordinate of the closest corner
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   152
	// but if we are between those coordinates, we will aim for our own x coordinate
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   153
	x = clamp(TileX(tile), minx, maxx);
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   154
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   155
	// same for y coordinate, see above comment
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   156
	y = clamp(TileY(tile), miny, maxy);
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   157
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   158
	// return the tile of our target coordinates
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   159
	return TileXY(x, y);
2182
89e6e39734a4 (svn r2696) Remove stray semicolons
tron
parents: 2164
diff changeset
   160
}
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   161
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   162
/* On PBS pathfinding runs, this is called before pathfinding ends (BeforeExit aystar callback), and will
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   163
 * reserve the appropriate tracks, if needed. */
2416
d3f8a53cedf6 (svn r2942) Staticise some functions
tron
parents: 2403
diff changeset
   164
static void NPFReservePBSPath(AyStar *as)
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   165
{
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   166
	NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   167
	bool eol_end = false;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   168
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   169
	if (ftd->best_trackdir == 0xFF)
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   170
		return;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   171
2029
4a678baaaae5 (svn r2538) - Fix: Use IsCompatibleRailType() function instead of checking this yourself.
hackykid
parents: 2009
diff changeset
   172
	if (!NPFGetFlag(&ftd->node, NPF_FLAG_PBS_EXIT) && IsEndOfLine(ftd->node.tile, ftd->node.direction, as->user_data[NPF_RAILTYPE]) && !NPFGetFlag(&ftd->node, NPF_FLAG_SEEN_SIGNAL)) {
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   173
		/* The path ends in an end of line, we'll need to reserve a path.
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   174
		 * We treat and end of line as a red exit signal */
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   175
		eol_end = true;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   176
		NPFSetFlag(&ftd->node, NPF_FLAG_PBS_EXIT, true);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   177
		if (!NPFGetFlag(&ftd->node, NPF_FLAG_PBS_TARGET_SEEN))
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   178
			NPFSetFlag(&ftd->node, NPF_FLAG_PBS_RED, true);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   179
	}
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   180
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   181
	if (!NPFGetFlag(&ftd->node, NPF_FLAG_PBS_CHOICE)) {
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   182
		/* there have been no choices to make on our path, we dont care if our end signal is red */
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   183
		NPFSetFlag(&ftd->node, NPF_FLAG_PBS_RED, false);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   184
	}
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   185
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   186
	if (NPFGetFlag(&ftd->node, NPF_FLAG_PBS_EXIT) && // we passed an exit signal
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   187
		 !NPFGetFlag(&ftd->node, NPF_FLAG_PBS_BLOCKED) && // we didnt encounter reserver tracks
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   188
		 ((as->user_data[NPF_PBS_MODE] != PBS_MODE_GREEN) || (!NPFGetFlag(&ftd->node, NPF_FLAG_PBS_RED))) ) { // our mode permits having a red exit signal, or the signal is green
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   189
		PathNode parent;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   190
		PathNode *curr;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   191
		PathNode *prev;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   192
		TileIndex start = INVALID_TILE;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   193
		byte trackdir = 0;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   194
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   195
		parent.node = ftd->node;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   196
		parent.parent = &ftd->path;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   197
		curr = &parent;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   198
		prev = NULL;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   199
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   200
		do {
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   201
			if (!NPFGetFlag(&curr->node, NPF_FLAG_PBS_EXIT) || eol_end) {
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   202
				/* check for already reserved track on this path, if they clash with what we
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   203
				   currently trying to reserve, we have a self-crossing path :-( */
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   204
				if ((PBSTileUnavail(curr->node.tile) & (1 << curr->node.direction))
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   205
				&& !(PBSTileReserved(curr->node.tile) & (1 << (curr->node.direction & 7)))
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   206
				&& (start != INVALID_TILE)) {
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   207
					/* It's actually quite bad if this happens, it means the pathfinder
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   208
					 * found a path that is intersecting with itself, which is a very bad
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   209
					 * thing in a pbs block. Also there is not much we can do about it at
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   210
					 * this point....
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   211
					 * BUT, you have to have a pretty fucked up junction layout for this to happen,
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   212
					 * so we'll just stop this train, the user will eventually notice, so he can fix it.
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   213
					 */
2115
71e12444631c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2075
diff changeset
   214
					PBSClearPath(start, trackdir, curr->node.tile, curr->node.direction);
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   215
					NPFSetFlag(&ftd->node, NPF_FLAG_PBS_BLOCKED, true);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   216
					DEBUG(pbs, 1) ("PBS: Self-crossing path!!!");
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   217
					return;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   218
				};
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   219
2115
71e12444631c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2075
diff changeset
   220
				PBSReserveTrack(curr->node.tile, TrackdirToTrack(curr->node.direction) );
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   221
2115
71e12444631c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2075
diff changeset
   222
				/* we want to reserve the last tile (with the signal) on the path too
71e12444631c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2075
diff changeset
   223
				   also remember this tile, cause its the end of the path (where we exit the block) */
71e12444631c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2075
diff changeset
   224
				if (start == INVALID_TILE) {
71e12444631c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2075
diff changeset
   225
					if (prev != NULL) {
71e12444631c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2075
diff changeset
   226
						PBSReserveTrack(prev->node.tile, TrackdirToTrack(prev->node.direction) );
71e12444631c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2075
diff changeset
   227
						start = prev->node.tile;
71e12444631c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2075
diff changeset
   228
						trackdir = ReverseTrackdir(prev->node.direction);
71e12444631c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2075
diff changeset
   229
					} else {
71e12444631c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2075
diff changeset
   230
						start = curr->node.tile;
71e12444631c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2075
diff changeset
   231
						trackdir = curr->node.direction;
71e12444631c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2075
diff changeset
   232
					}
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   233
				}
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   234
			}
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   235
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   236
			prev = curr;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   237
			curr = curr->parent;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   238
		} while (curr != NULL);
2115
71e12444631c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2075
diff changeset
   239
		// we remember the tile/track where this path leaves the pbs junction
71e12444631c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2075
diff changeset
   240
		ftd->node.tile = start;
71e12444631c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2075
diff changeset
   241
		ftd->node.direction = trackdir;
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   242
	}
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   243
}
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   244
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   245
1677
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   246
/* Calcs the heuristic to the target station or tile. For train stations, it
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   247
 * takes into account the direction of approach.
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   248
 */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   249
static int32 NPFCalcStationOrTileHeuristic(AyStar* as, AyStarNode* current, OpenListNode* parent)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   250
{
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   251
	NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   252
	NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   253
	TileIndex from = current->tile;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   254
	TileIndex to = fstd->dest_coords;
1453
687663191db3 (svn r1957) Compilation fix.
pasky
parents: 1452
diff changeset
   255
	uint dist;
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   256
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   257
	// for train-stations, we are going to aim for the closest station tile
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   258
	if ((as->user_data[NPF_TYPE] == TRANSPORT_RAIL) && (fstd->station_index != -1))
1453
687663191db3 (svn r1957) Compilation fix.
pasky
parents: 1452
diff changeset
   259
		to = CalcClosestStationTile(fstd->station_index, from);
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   260
1677
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   261
	if (as->user_data[NPF_TYPE] == TRANSPORT_ROAD)
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   262
		/* Since roads only have diagonal pieces, we use manhattan distance here */
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   263
		dist = DistanceManhattan(from, to) * NPF_TILE_LENGTH;
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   264
	else
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   265
		/* Ships and trains can also go diagonal, so the minimum distance is shorter */
2403
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
   266
		dist = NPFDistanceTrack(from, to);
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   267
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   268
	DEBUG(npf, 4)("Calculating H for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), dist);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   269
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   270
	/* for pbs runs, we ignore tiles inside the pbs block for the tracking
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   271
	   of the 'closest' tile */
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   272
	if ((as->user_data[NPF_PBS_MODE] != PBS_MODE_NONE)
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   273
	&&  (!NPFGetFlag(current , NPF_FLAG_SEEN_SIGNAL))
2029
4a678baaaae5 (svn r2538) - Fix: Use IsCompatibleRailType() function instead of checking this yourself.
hackykid
parents: 2009
diff changeset
   274
	&&  (!IsEndOfLine(current->tile, current->direction, as->user_data[NPF_RAILTYPE])))
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   275
		return dist;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   276
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   277
	if ((dist < ftd->best_bird_dist) ||
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   278
		/* for pbs runs, prefer tiles that pass a green exit signal to the pbs blocks */
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   279
		((as->user_data[NPF_PBS_MODE] != PBS_MODE_NONE) && !NPFGetFlag(current, NPF_FLAG_PBS_RED) && NPFGetFlag(&ftd->node, NPF_FLAG_PBS_RED))
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   280
) {
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   281
		ftd->best_bird_dist = dist;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   282
		ftd->best_trackdir = current->user_data[NPF_TRACKDIR_CHOICE];
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   283
		ftd->path = parent->path;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   284
		ftd->node = *current;
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   285
	}
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   286
	return dist;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   287
}
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   288
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   289
/* Fills AyStarNode.user_data[NPF_TRACKDIRCHOICE] with the chosen direction to
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   290
 * get here, either getting it from the current choice or from the parent's
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   291
 * choice */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   292
static void NPFFillTrackdirChoice(AyStarNode* current, OpenListNode* parent)
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   293
{
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   294
	if (parent->path.parent == NULL) {
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   295
		Trackdir trackdir = (Trackdir)current->direction;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   296
		/* This is a first order decision, so we'd better save the
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   297
		 * direction we chose */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   298
		current->user_data[NPF_TRACKDIR_CHOICE] = trackdir;
1678
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   299
		DEBUG(npf, 6)("Saving trackdir: %#x", trackdir);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   300
	} else {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   301
		/* We've already made the decision, so just save our parent's
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   302
		 * decision */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   303
		current->user_data[NPF_TRACKDIR_CHOICE] = parent->path.node.user_data[NPF_TRACKDIR_CHOICE];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   304
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   305
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   306
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   307
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   308
/* Will return the cost of the tunnel. If it is an entry, it will return the
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   309
 * cost of that tile. If the tile is an exit, it will return the tunnel length
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   310
 * including the exit tile. Requires that this is a Tunnel tile */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   311
static uint NPFTunnelCost(AyStarNode* current)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   312
{
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   313
	DiagDirection exitdir = TrackdirToExitdir((Trackdir)current->direction);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   314
	TileIndex tile = current->tile;
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2029
diff changeset
   315
	if ( (DiagDirection)(_m[tile].m5 & 3) == ReverseDiagdir(exitdir)) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   316
		/* We just popped out if this tunnel, since were
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   317
		 * facing the tunnel exit */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   318
		FindLengthOfTunnelResult flotr;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   319
		flotr = FindLengthOfTunnel(tile, ReverseDiagdir(exitdir));
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   320
		return flotr.length * NPF_TILE_LENGTH;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   321
		//TODO: Penalty for tunnels?
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   322
	} else {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   323
		/* We are entering the tunnel, the enter tile is just a
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   324
		 * straight track */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   325
		return NPF_TILE_LENGTH;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   326
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   327
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   328
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   329
static uint NPFSlopeCost(AyStarNode* current)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   330
{
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   331
	TileIndex next = current->tile + TileOffsByDir(TrackdirToExitdir(current->direction));
1503
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   332
	int x,y;
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   333
	int8 z1,z2;
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   334
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   335
	x = TileX(current->tile) * TILE_SIZE;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   336
	y = TileY(current->tile) * TILE_SIZE;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   337
	/* get the height of the center of the current tile */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   338
	z1 = GetSlopeZ(x+TILE_HEIGHT, y+TILE_HEIGHT);
1503
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   339
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   340
	x = TileX(next) * TILE_SIZE;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   341
	y = TileY(next) * TILE_SIZE;
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   342
	/* get the height of the center of the next tile */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   343
	z2 = GetSlopeZ(x+TILE_HEIGHT, y+TILE_HEIGHT);
1503
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   344
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   345
	if ((z2 - z1) > 1) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   346
		/* Slope up */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   347
		return _patches.npf_rail_slope_penalty;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   348
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   349
	return 0;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   350
	/* Should we give a bonus for slope down? Probably not, we
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   351
	 * could just substract that bonus from the penalty, because
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   352
	 * there is only one level of steepness... */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   353
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   354
1678
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   355
/* Mark tiles by mowing the grass when npf debug level >= 1 */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   356
static void NPFMarkTile(TileIndex tile)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   357
{
1678
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   358
#ifdef NO_DEBUG_MESSAGES
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   359
	return;
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   360
#else
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   361
	if (_debug_npf_level >= 1)
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   362
		switch(GetTileType(tile)) {
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   363
			case MP_RAILWAY:
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   364
				/* DEBUG: mark visited tiles by mowing the grass under them
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   365
				 * ;-) */
1753
091f7a870a2a (svn r2257) - Fix: [NPF] NPF debug markings modify _map2 instead of _map3_hi for street tiles, corrupting them.
matthijs
parents: 1751
diff changeset
   366
				if (!IsTileDepotType(tile, TRANSPORT_RAIL)) {
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2029
diff changeset
   367
					_m[tile].m2 &= ~15; /* Clear bits 0-3 */
1753
091f7a870a2a (svn r2257) - Fix: [NPF] NPF debug markings modify _map2 instead of _map3_hi for street tiles, corrupting them.
matthijs
parents: 1751
diff changeset
   368
					MarkTileDirtyByTile(tile);
091f7a870a2a (svn r2257) - Fix: [NPF] NPF debug markings modify _map2 instead of _map3_hi for street tiles, corrupting them.
matthijs
parents: 1751
diff changeset
   369
				}
091f7a870a2a (svn r2257) - Fix: [NPF] NPF debug markings modify _map2 instead of _map3_hi for street tiles, corrupting them.
matthijs
parents: 1751
diff changeset
   370
				break;
091f7a870a2a (svn r2257) - Fix: [NPF] NPF debug markings modify _map2 instead of _map3_hi for street tiles, corrupting them.
matthijs
parents: 1751
diff changeset
   371
			case MP_STREET:
091f7a870a2a (svn r2257) - Fix: [NPF] NPF debug markings modify _map2 instead of _map3_hi for street tiles, corrupting them.
matthijs
parents: 1751
diff changeset
   372
				if (!IsTileDepotType(tile, TRANSPORT_ROAD)) {
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2029
diff changeset
   373
					_m[tile].m4 &= ~0x70; /* Clear bits 4-6 */
1753
091f7a870a2a (svn r2257) - Fix: [NPF] NPF debug markings modify _map2 instead of _map3_hi for street tiles, corrupting them.
matthijs
parents: 1751
diff changeset
   374
					MarkTileDirtyByTile(tile);
091f7a870a2a (svn r2257) - Fix: [NPF] NPF debug markings modify _map2 instead of _map3_hi for street tiles, corrupting them.
matthijs
parents: 1751
diff changeset
   375
				}
1678
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   376
				break;
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   377
			default:
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   378
				break;
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   379
		}
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   380
#endif
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   381
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   382
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   383
static int32 NPFWaterPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   384
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   385
	//TileIndex tile = current->tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   386
	int32 cost = 0;
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   387
	Trackdir trackdir = (Trackdir)current->direction;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   388
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   389
	cost = _trackdir_length[trackdir]; /* Should be different for diagonal tracks */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   390
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   391
	if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(trackdir))
1751
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   392
		cost += _patches.npf_buoy_penalty; /* A small penalty for going over buoys */
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   393
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   394
	if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction))
1751
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   395
		cost += _patches.npf_water_curve_penalty;
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   396
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   397
	/* TODO More penalties? */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   398
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   399
	return cost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   400
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   401
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   402
/* Determine the cost of this node, for road tracks */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   403
static int32 NPFRoadPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   404
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   405
	TileIndex tile = current->tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   406
	int32 cost = 0;
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   407
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   408
	/* Determine base length */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   409
	switch (GetTileType(tile)) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   410
		case MP_TUNNELBRIDGE:
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2029
diff changeset
   411
			if ((_m[tile].m5 & 0xF0)==0) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   412
				cost = NPFTunnelCost(current);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   413
				break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   414
			}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   415
			/* Fall through if above if is false, it is a bridge
2006
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1983
diff changeset
   416
			 * then. We treat that as ordinary road */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   417
		case MP_STREET:
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   418
			cost = NPF_TILE_LENGTH;
2006
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1983
diff changeset
   419
			/* Increase the cost for level crossings */
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2029
diff changeset
   420
			if ((_m[tile].m5 & 0xF0) == 0x10)
2006
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1983
diff changeset
   421
				cost += _patches.npf_crossing_penalty;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   422
			break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   423
		default:
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   424
			break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   425
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   426
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   427
	/* Determine extra costs */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   428
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   429
	/* Check for slope */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   430
	cost += NPFSlopeCost(current);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   431
1941
b1cb02c0401c (svn r2447) * Add: [NPF] Penalty for road vehicles making turns.
matthijs
parents: 1927
diff changeset
   432
	/* Check for turns. Road vehicles only really drive diagonal, turns are
b1cb02c0401c (svn r2447) * Add: [NPF] Penalty for road vehicles making turns.
matthijs
parents: 1927
diff changeset
   433
	 * represented by non-diagonal tracks */
b1cb02c0401c (svn r2447) * Add: [NPF] Penalty for road vehicles making turns.
matthijs
parents: 1927
diff changeset
   434
	if (!IsDiagonalTrackdir(current->direction))
b1cb02c0401c (svn r2447) * Add: [NPF] Penalty for road vehicles making turns.
matthijs
parents: 1927
diff changeset
   435
		cost += _patches.npf_road_curve_penalty;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   436
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   437
	NPFMarkTile(tile);
1678
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   438
	DEBUG(npf, 4)("Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   439
	return cost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   440
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   441
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   442
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   443
/* Determine the cost of this node, for railway tracks */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   444
static int32 NPFRailPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   445
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   446
	TileIndex tile = current->tile;
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   447
	Trackdir trackdir = (Trackdir)current->direction;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   448
	int32 cost = 0;
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   449
	/* HACK: We create a OpenListNode manualy, so we can call EndNodeCheck */
1617
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   450
	OpenListNode new_node;
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   451
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   452
	/* Determine base length */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   453
	switch (GetTileType(tile)) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   454
		case MP_TUNNELBRIDGE:
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2029
diff changeset
   455
			if ((_m[tile].m5 & 0xF0)==0) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   456
				cost = NPFTunnelCost(current);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   457
				break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   458
			}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   459
			/* Fall through if above if is false, it is a bridge
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   460
			 * then. We treat that as ordinary rail */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   461
		case MP_RAILWAY:
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   462
			cost = _trackdir_length[trackdir]; /* Should be different for diagonal tracks */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   463
			break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   464
		case MP_STREET: /* Railway crossing */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   465
			cost = NPF_TILE_LENGTH;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   466
			break;
1503
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   467
		case MP_STATION:
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   468
			/* We give a station tile a penalty. Logically we would only
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   469
					* want to give station tiles that are not our destination
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   470
					* this penalty. This would discourage trains to drive through
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   471
					* busy stations. But, we can just give any station tile a
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   472
					* penalty, because every possible route will get this penalty
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   473
					* exactly once, on its end tile (if it's a station) and it
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   474
			* will therefore not make a difference. */
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   475
			cost = NPF_TILE_LENGTH + _patches.npf_rail_station_penalty;
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   476
			break;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   477
		default:
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   478
			break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   479
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   480
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   481
	/* Determine extra costs */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   482
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   483
	/* Check for reserved tracks (PBS) */
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   484
	if ((as->user_data[NPF_PBS_MODE] != PBS_MODE_NONE) && !(NPFGetFlag(current, NPF_FLAG_PBS_EXIT)) && !(NPFGetFlag(current, NPF_FLAG_PBS_BLOCKED)) && (PBSTileUnavail(tile) & (1<<trackdir))) {
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   485
		NPFSetFlag(current, NPF_FLAG_PBS_BLOCKED, true);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   486
	};
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   487
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   488
	/* Check for signals */
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   489
	if (IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, trackdir)) {
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   490
		/* Ordinary track with signals */
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   491
		if (GetSignalState(tile, trackdir) == SIGNAL_STATE_RED) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   492
			/* Signal facing us is red */
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   493
			if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   494
				/* Penalize the first signal we
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   495
				 * encounter, if it is red */
1643
d38655053062 (svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents: 1617
diff changeset
   496
d38655053062 (svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents: 1617
diff changeset
   497
				/* Is this a presignal exit or combo? */
1945
74355187d85f (svn r2451) * Fix: Assertion caused by passing a trackdir where a track was expected.
matthijs
parents: 1944
diff changeset
   498
				SignalType sigtype = GetSignalType(tile, TrackdirToTrack(trackdir));
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   499
				if (sigtype == SIGTYPE_EXIT || sigtype == SIGTYPE_COMBO)
1643
d38655053062 (svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents: 1617
diff changeset
   500
					/* Penalise exit and combo signals differently (heavier) */
d38655053062 (svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents: 1617
diff changeset
   501
					cost += _patches.npf_rail_firstred_exit_penalty;
d38655053062 (svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents: 1617
diff changeset
   502
				else
d38655053062 (svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents: 1617
diff changeset
   503
					cost += _patches.npf_rail_firstred_penalty;
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   504
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   505
				/* for pbs runs, store the fact that the exit signal to the pbs block was red */
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   506
				if (!(NPFGetFlag(current, NPF_FLAG_PBS_EXIT)) && !(NPFGetFlag(current, NPF_FLAG_PBS_RED)) && NPFGetFlag(current, NPF_FLAG_PBS_CHOICE))
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   507
					NPFSetFlag(current, NPF_FLAG_PBS_RED, true);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   508
			}
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   509
			/* Record the state of this signal */
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   510
			NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, true);
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   511
		} else {
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   512
			/* Record the state of this signal */
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   513
			NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, false);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   514
		}
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   515
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   516
		if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL) && NPFGetFlag(current, NPF_FLAG_PBS_BLOCKED)) {
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   517
			/* penalise a path through the pbs block if it crosses reserved tracks */
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   518
			cost += 1000;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   519
		}
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   520
		if ((PBSIsPbsSignal(tile, trackdir)) && !NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   521
			/* we've encountered an exit signal to the pbs block */
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   522
			NPFSetFlag(current, NPF_FLAG_PBS_EXIT, true);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   523
		}
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   524
		NPFSetFlag(current, NPF_FLAG_SEEN_SIGNAL, true);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   525
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   526
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   527
	/* Penalise the tile if it is a target tile and the last signal was
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   528
	 * red */
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   529
	/* HACK: We create a new_node here so we can call EndNodeCheck. Ugly as hell
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   530
	 * of course... */
1617
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   531
	new_node.path.node = *current;
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   532
	if (as->EndNodeCheck(as, &new_node) == AYSTAR_FOUND_END_NODE && NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_RED))
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   533
		cost += _patches.npf_rail_lastred_penalty;
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   534
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   535
	/* Check for slope */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   536
	cost += NPFSlopeCost(current);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   537
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   538
	/* Check for turns */
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   539
	if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction))
1460
ebd1cfae9588 (svn r1964) - Add: [NPF] Added a penalty
matthijs
parents: 1459
diff changeset
   540
		cost += _patches.npf_rail_curve_penalty;
ebd1cfae9588 (svn r1964) - Add: [NPF] Added a penalty
matthijs
parents: 1459
diff changeset
   541
	//TODO, with realistic acceleration, also the amount of straight track between
ebd1cfae9588 (svn r1964) - Add: [NPF] Added a penalty
matthijs
parents: 1459
diff changeset
   542
	//      curves should be taken into account, as this affects the speed limit.
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   543
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   544
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   545
	/* Check for depots */
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   546
	if (IsTileDepotType(tile, TRANSPORT_RAIL)) {
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   547
		/* Penalise any depot tile that is not the last tile in the path. This
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   548
		 * _should_ penalise every occurence of reversing in a depot (and only
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   549
		 * that) */
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   550
		if (as->EndNodeCheck(as, &new_node) != AYSTAR_FOUND_END_NODE)
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   551
			cost += _patches.npf_rail_depot_reverse_penalty;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   552
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   553
		/* Do we treat this depot as a pbs signal? */
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   554
		if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   555
			if (NPFGetFlag(current, NPF_FLAG_PBS_BLOCKED)) {
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   556
				cost += 1000;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   557
			}
2164
74c8163d1e14 (svn r2674) - CodeChange: [pbs] Generalise the PSBISPbsDepot function so it can check if an arbitrary junction is a pbs junction. Preparations for making pbs more safe.
hackykid
parents: 2163
diff changeset
   558
			if (PBSIsPbsSegment(tile, ReverseTrackdir(trackdir))) {
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   559
				NPFSetFlag(current, NPF_FLAG_PBS_EXIT, true);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   560
				NPFSetFlag(current, NPF_FLAG_SEEN_SIGNAL, true);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   561
			}
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   562
		}
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   563
		NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, false);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   564
	}
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   565
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   566
	/* Check for occupied track */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   567
	//TODO
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   568
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   569
	NPFMarkTile(tile);
1678
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   570
	DEBUG(npf, 4)("Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   571
	return cost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   572
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   573
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   574
/* Will find any depot */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   575
static int32 NPFFindDepot(AyStar* as, OpenListNode *current)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   576
{
1617
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   577
	TileIndex tile = current->path.node.tile;
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   578
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   579
	/* It's not worth caching the result with NPF_FLAG_IS_TARGET here as below,
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   580
	 * since checking the cache not that much faster than the actual check */
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   581
	if (IsTileDepotType(tile, as->user_data[NPF_TYPE]))
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   582
		return AYSTAR_FOUND_END_NODE;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   583
	else
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   584
		return AYSTAR_DONE;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   585
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   586
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   587
/* Will find a station identified using the NPFFindStationOrTileData */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   588
static int32 NPFFindStationOrTile(AyStar* as, OpenListNode *current)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   589
{
1464
266d3b0ee2c8 (svn r1968) - Fix: [NPF] Mixed declarations and statements
matthijs
parents: 1463
diff changeset
   590
	NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target;
1617
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   591
	AyStarNode *node = &current->path.node;
1464
266d3b0ee2c8 (svn r1968) - Fix: [NPF] Mixed declarations and statements
matthijs
parents: 1463
diff changeset
   592
	TileIndex tile = node->tile;
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   593
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   594
	if (tile == 0x4611c) {
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   595
		tile++;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   596
		tile--;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   597
	}
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   598
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   599
	/* If GetNeighbours said we could get here, we assume the station type
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   600
	 * is correct */
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   601
	if (
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   602
		(fstd->station_index == -1 && tile == fstd->dest_coords) || /* We've found the tile, or */
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2029
diff changeset
   603
		(IsTileType(tile, MP_STATION) && _m[tile].m2 == fstd->station_index) || /* the station */
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   604
		(NPFGetFlag(node, NPF_FLAG_PBS_TARGET_SEEN)) /* or, we've passed it already (for pbs) */
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   605
	) {
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   606
		NPFSetFlag(&current->path.node, NPF_FLAG_PBS_TARGET_SEEN, true);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   607
		/* for pbs runs, only accept we've found the target if we've also found a way out of the block */
2029
4a678baaaae5 (svn r2538) - Fix: Use IsCompatibleRailType() function instead of checking this yourself.
hackykid
parents: 2009
diff changeset
   608
		if ((as->user_data[NPF_PBS_MODE] != PBS_MODE_NONE) && !NPFGetFlag(node, NPF_FLAG_SEEN_SIGNAL) && !IsEndOfLine(node->tile, node->direction, as->user_data[NPF_RAILTYPE]))
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   609
			return AYSTAR_DONE;
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   610
		return AYSTAR_FOUND_END_NODE;
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   611
	} else {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   612
		return AYSTAR_DONE;
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   613
	}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   614
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   615
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   616
/* To be called when current contains the (shortest route to) the target node.
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   617
 * Will fill the contents of the NPFFoundTargetData using
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   618
 * AyStarNode[NPF_TRACKDIR_CHOICE].
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   619
 */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   620
static void NPFSaveTargetData(AyStar* as, OpenListNode* current)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   621
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   622
	NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path;
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   623
	ftd->best_trackdir = (Trackdir)current->path.node.user_data[NPF_TRACKDIR_CHOICE];
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   624
	ftd->best_path_dist = current->g;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   625
	ftd->best_bird_dist = 0;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   626
	ftd->node = current->path.node;
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   627
	ftd->path = current->path;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   628
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   629
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   630
/**
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   631
 * Finds out if a given player's vehicles are allowed to enter a given tile.
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   632
 * @param owner    The owner of the vehicle.
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   633
 * @param tile     The tile that is about to be entered.
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   634
 * @param enterdir The direction from which the vehicle wants to enter the tile.
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   635
 * @return         true if the vehicle can enter the tile.
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   636
 * @todo           This function should be used in other places than just NPF,
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   637
 *                 maybe moved to another file too.
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   638
 */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   639
static bool VehicleMayEnterTile(Owner owner, TileIndex tile, DiagDirection enterdir)
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   640
{
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   641
	if (
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   642
		IsTileType(tile, MP_RAILWAY) /* Rail tile (also rail depot) */
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   643
		|| IsTrainStationTile(tile) /* Rail station tile */
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   644
		|| IsTileDepotType(tile, TRANSPORT_ROAD) /* Road depot tile */
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   645
		|| IsRoadStationTile(tile) /* Road station tile */
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   646
		|| IsTileDepotType(tile, TRANSPORT_WATER) /* Water depot tile */
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   647
		)
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   648
		return IsTileOwner(tile, owner); /* You need to own these tiles entirely to use them */
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   649
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   650
	switch (GetTileType(tile)) {
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   651
		case MP_STREET:
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   652
			/* rail-road crossing : are we looking at the railway part? */
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   653
			if (IsLevelCrossing(tile) && GetCrossingTransportType(tile, TrackdirToTrack(DiagdirToDiagTrackdir(enterdir))) == TRANSPORT_RAIL)
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   654
				return IsTileOwner(tile, owner); /* Railway needs owner check, while the street is public */
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   655
			break;
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   656
		case MP_TUNNELBRIDGE:
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   657
#if 0
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   658
/* OPTIMISATION: If we are on the middle of a bridge, we will not do the cpu
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   659
 * intensive owner check, instead we will just assume that if the vehicle
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   660
 * managed to get on the bridge, it is probably allowed to :-)
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   661
 */
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2029
diff changeset
   662
			if ((_m[tile].m5 & 0xC6) == 0xC0 && (unsigned)(_m[tile].m5 & 0x1) == (enterdir & 0x1)) {
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   663
				/* on the middle part of a railway bridge: find bridge ending */
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2029
diff changeset
   664
				while (IsTileType(tile, MP_TUNNELBRIDGE) && !((_m[tile].m5 & 0xC6) == 0x80)) {
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2029
diff changeset
   665
					tile += TileOffsByDir(_m[tile].m5 & 0x1);
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   666
				}
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   667
			}
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   668
			/* if we were on a railway middle part, we are now at a railway bridge ending */
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   669
#endif
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   670
			if (
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2029
diff changeset
   671
				(_m[tile].m5 & 0xFC) == 0 /* railway tunnel */
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2029
diff changeset
   672
				|| (_m[tile].m5 & 0xC6) == 0x80 /* railway bridge ending */
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2029
diff changeset
   673
				|| ((_m[tile].m5 & 0xF8) == 0xE0 && ((unsigned)_m[tile].m5 & 0x1) != (enterdir & 0x1)) /* railway under bridge */
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   674
				)
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   675
				return IsTileOwner(tile, owner);
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   676
			break;
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   677
		default:
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   678
			break;
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   679
	}
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   680
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   681
	return true; /* no need to check */
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   682
}
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   683
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   684
/* Will just follow the results of GetTileTrackStatus concerning where we can
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   685
 * go and where not. Uses AyStar.user_data[NPF_TYPE] as the transport type and
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   686
 * an argument to GetTileTrackStatus. Will skip tunnels, meaning that the
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   687
 * entry and exit are neighbours. Will fill
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   688
 * AyStarNode.user_data[NPF_TRACKDIR_CHOICE] with an appropriate value, and
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   689
 * copy AyStarNode.user_data[NPF_NODE_FLAGS] from the parent */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   690
static void NPFFollowTrack(AyStar* aystar, OpenListNode* current)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   691
{
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   692
	Trackdir src_trackdir = (Trackdir)current->path.node.direction;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   693
	TileIndex src_tile = current->path.node.tile;
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   694
	DiagDirection src_exitdir = TrackdirToExitdir(src_trackdir);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   695
	FindLengthOfTunnelResult flotr;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   696
	TileIndex dst_tile;
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   697
	int i;
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   698
	TrackdirBits trackdirbits, ts;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   699
	TransportType type = aystar->user_data[NPF_TYPE];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   700
	/* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   701
	aystar->num_neighbours = 0;
1678
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   702
	DEBUG(npf, 4)("Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   703
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   704
	aystar->EndNodeCheck(aystar, current);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   705
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   706
	/* Find dest tile */
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2029
diff changeset
   707
	if (IsTileType(src_tile, MP_TUNNELBRIDGE) && (_m[src_tile].m5 & 0xF0)==0 && (DiagDirection)(_m[src_tile].m5 & 3) == src_exitdir) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   708
		/* This is a tunnel. We know this tunnel is our type,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   709
		 * otherwise we wouldn't have got here. It is also facing us,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   710
		 * so we should skip it's body */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   711
		flotr = FindLengthOfTunnel(src_tile, src_exitdir);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   712
		dst_tile = flotr.tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   713
	} else {
1650
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   714
		if (type != TRANSPORT_WATER && (IsRoadStationTile(src_tile) || IsTileDepotType(src_tile, type))){
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   715
			/* This is a road station or a train or road depot. We can enter and exit
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   716
			 * those from one side only. Trackdirs don't support that (yet), so we'll
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   717
			 * do this here. */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   718
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   719
			DiagDirection exitdir;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   720
			/* Find out the exit direction first */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   721
			if (IsRoadStationTile(src_tile))
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   722
				exitdir = GetRoadStationDir(src_tile);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   723
			else /* Train or road depot. Direction is stored the same for both, in map5 */
1650
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   724
				exitdir = GetDepotDirection(src_tile, type);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   725
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   726
			/* Let's see if were headed the right way into the depot, and reverse
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   727
			 * otherwise (only for trains, since only with trains you can
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   728
			 * (sometimes) reach tiles after reversing that you couldn't reach
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   729
			 * without reversing. */
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   730
			if (src_trackdir == DiagdirToDiagTrackdir(ReverseDiagdir(exitdir)) && type == TRANSPORT_RAIL)
1644
1a08c3ebdcd8 (svn r2148) - Add: [NPF] Trains can now plan taking into account that they can reverse in depots. This makes forced servicing tracks work with NPF.
matthijs
parents: 1643
diff changeset
   731
				/* We are headed inwards. We can only reverse here, so we'll not
1a08c3ebdcd8 (svn r2148) - Add: [NPF] Trains can now plan taking into account that they can reverse in depots. This makes forced servicing tracks work with NPF.
matthijs
parents: 1643
diff changeset
   732
				 * consider this direction, but jump ahead to the reverse direction.
1a08c3ebdcd8 (svn r2148) - Add: [NPF] Trains can now plan taking into account that they can reverse in depots. This makes forced servicing tracks work with NPF.
matthijs
parents: 1643
diff changeset
   733
				 * It would be nicer to return one neighbour here (the reverse
1a08c3ebdcd8 (svn r2148) - Add: [NPF] Trains can now plan taking into account that they can reverse in depots. This makes forced servicing tracks work with NPF.
matthijs
parents: 1643
diff changeset
   734
				 * trackdir of the one we are considering now) and then considering
1a08c3ebdcd8 (svn r2148) - Add: [NPF] Trains can now plan taking into account that they can reverse in depots. This makes forced servicing tracks work with NPF.
matthijs
parents: 1643
diff changeset
   735
				 * that one to return the tracks outside of the depot. But, because
1a08c3ebdcd8 (svn r2148) - Add: [NPF] Trains can now plan taking into account that they can reverse in depots. This makes forced servicing tracks work with NPF.
matthijs
parents: 1643
diff changeset
   736
				 * the code layout is cleaner this way, we will just pretend we are
1a08c3ebdcd8 (svn r2148) - Add: [NPF] Trains can now plan taking into account that they can reverse in depots. This makes forced servicing tracks work with NPF.
matthijs
parents: 1643
diff changeset
   737
				 * reversed already */
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   738
				src_trackdir = ReverseTrackdir(src_trackdir);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   739
		}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   740
		/* This a normal tile, a bridge, a tunnel exit, etc. */
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   741
		dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDir(TrackdirToExitdir(src_trackdir)));
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   742
		if (dst_tile == INVALID_TILE) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   743
			/* We reached the border of the map */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   744
			/* TODO Nicer control flow for this */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   745
			return;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   746
		}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   747
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   748
1965
ef27280abb9a (svn r2471) - Fix: [ 1221249 ] [NPF] Vehicles try to drive into a tunnel entrance from above.
matthijs
parents: 1950
diff changeset
   749
	/* I can't enter a tunnel entry/exit tile from a tile above the tunnel. Note
ef27280abb9a (svn r2471) - Fix: [ 1221249 ] [NPF] Vehicles try to drive into a tunnel entrance from above.
matthijs
parents: 1950
diff changeset
   750
	 * that I can enter the tunnel from a tile below the tunnel entrance. This
ef27280abb9a (svn r2471) - Fix: [ 1221249 ] [NPF] Vehicles try to drive into a tunnel entrance from above.
matthijs
parents: 1950
diff changeset
   751
	 * solves the problem of vehicles wanting to drive off a tunnel entrance */
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2029
diff changeset
   752
	if (IsTileType(dst_tile, MP_TUNNELBRIDGE) && (_m[dst_tile].m5 & 0xF0) == 0 && GetTileZ(dst_tile) < GetTileZ(src_tile))
1965
ef27280abb9a (svn r2471) - Fix: [ 1221249 ] [NPF] Vehicles try to drive into a tunnel entrance from above.
matthijs
parents: 1950
diff changeset
   753
		return;
ef27280abb9a (svn r2471) - Fix: [ 1221249 ] [NPF] Vehicles try to drive into a tunnel entrance from above.
matthijs
parents: 1950
diff changeset
   754
2006
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1983
diff changeset
   755
	/* check correct rail type (mono, maglev, etc) */
1749
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   756
	if (type == TRANSPORT_RAIL) {
2006
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1983
diff changeset
   757
		RailType dst_type = GetTileRailType(dst_tile, src_trackdir);
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1983
diff changeset
   758
		if (!IsCompatibleRail(aystar->user_data[NPF_RAILTYPE], dst_type))
1749
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   759
			return;
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   760
	}
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   761
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   762
	/* Check the owner of the tile */
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   763
	if (!VehicleMayEnterTile(aystar->user_data[NPF_OWNER], dst_tile, TrackdirToExitdir(src_trackdir))) {
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   764
		return;
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   765
	}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   766
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   767
	/* Determine available tracks */
1655
f45015d2df03 (svn r2159) - Fix: [NPF] Road vehicles never found their target station or depots (introduced in r2154)
matthijs
parents: 1650
diff changeset
   768
	if (type != TRANSPORT_WATER && (IsRoadStationTile(dst_tile) || IsTileDepotType(dst_tile, type))){
f45015d2df03 (svn r2159) - Fix: [NPF] Road vehicles never found their target station or depots (introduced in r2154)
matthijs
parents: 1650
diff changeset
   769
		/* Road stations and road and train depots return 0 on GTTS, so we have to do this by hand... */
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   770
		DiagDirection exitdir;
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   771
		if (IsRoadStationTile(dst_tile))
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   772
			exitdir = GetRoadStationDir(dst_tile);
1655
f45015d2df03 (svn r2159) - Fix: [NPF] Road vehicles never found their target station or depots (introduced in r2154)
matthijs
parents: 1650
diff changeset
   773
		else /* Road or train depot */
1650
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   774
			exitdir = GetDepotDirection(dst_tile, type);
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   775
		/* Find the trackdirs that are available for a depot or station with this
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   776
		 * orientation. They are only "inwards", since we are reaching this tile
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   777
		 * from some other tile. This prevents vehicles driving into depots from
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   778
		 * the back */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   779
		ts = TrackdirToTrackdirBits(DiagdirToDiagTrackdir(ReverseDiagdir(exitdir)));
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   780
	} else {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   781
		ts = GetTileTrackStatus(dst_tile, type);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   782
	}
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   783
	trackdirbits = ts & TRACKDIR_BIT_MASK; /* Filter out signal status and the unused bits */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   784
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   785
	DEBUG(npf, 4)("Next node: (%d, %d) [%d], possible trackdirs: %#x", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirbits);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   786
	/* Select only trackdirs we can reach from our current trackdir */
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   787
	trackdirbits &= TrackdirReachesTrackdirs(src_trackdir);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   788
	if (_patches.forbid_90_deg && (type == TRANSPORT_RAIL || type == TRANSPORT_WATER)) /* Filter out trackdirs that would make 90 deg turns for trains */
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   789
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   790
	trackdirbits &= ~TrackdirCrossesTrackdirs(src_trackdir);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   791
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   792
	if (KillFirstBit2x64(trackdirbits) != 0)
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   793
		NPFSetFlag(&current->path.node, NPF_FLAG_PBS_CHOICE, true);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   794
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   795
	/* When looking for 'any' route, ie when already inside a pbs block, discard all tracks that would cross
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   796
	   other reserved tracks, so we *always* will find a valid route if there is one */
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   797
	if (!(NPFGetFlag(&current->path.node, NPF_FLAG_PBS_EXIT)) && (aystar->user_data[NPF_PBS_MODE] == PBS_MODE_ANY))
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   798
		trackdirbits &= ~PBSTileUnavail(dst_tile);
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   799
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   800
	DEBUG(npf,6)("After filtering: (%d, %d), possible trackdirs: %#x", TileX(dst_tile), TileY(dst_tile), trackdirbits);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   801
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   802
	i = 0;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   803
	/* Enumerate possible track */
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   804
	while (trackdirbits != 0) {
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   805
		Trackdir dst_trackdir;
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   806
		dst_trackdir =  FindFirstBit2x64(trackdirbits);
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   807
		trackdirbits = KillFirstBit2x64(trackdirbits);
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   808
		DEBUG(npf, 5)("Expanded into trackdir: %d, remaining trackdirs: %#x", dst_trackdir, trackdirbits);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   809
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   810
		/* Check for oneway signal against us */
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   811
		if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TYPE_SIGNALS) {
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   812
			if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(dst_trackdir)) && !HasSignalOnTrackdir(dst_tile, dst_trackdir))
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   813
				// if one way signal not pointing towards us, stop going in this direction.
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   814
				break;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   815
		}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   816
		{
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   817
			/* We've found ourselves a neighbour :-) */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   818
			AyStarNode* neighbour = &aystar->neighbours[i];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   819
			neighbour->tile = dst_tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   820
			neighbour->direction = dst_trackdir;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   821
			/* Save user data */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   822
			neighbour->user_data[NPF_NODE_FLAGS] = current->path.node.user_data[NPF_NODE_FLAGS];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   823
			NPFFillTrackdirChoice(neighbour, current);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   824
		}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   825
		i++;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   826
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   827
	aystar->num_neighbours = i;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   828
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   829
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   830
/*
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   831
 * Plan a route to the specified target (which is checked by target_proc),
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   832
 * from start1 and if not NULL, from start2 as well. The type of transport we
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   833
 * are checking is in type. reverse_penalty is applied to all routes that
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   834
 * originate from the second start node.
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   835
 * When we are looking for one specific target (optionally multiple tiles), we
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   836
 * should use a good heuristic to perform aystar search. When we search for
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   837
 * multiple targets that are spread around, we should perform a breadth first
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   838
 * search by specifiying CalcZero as our heuristic.
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   839
 */
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   840
static NPFFoundTargetData NPFRouteInternal(AyStarNode* start1, AyStarNode* start2, NPFFindStationOrTileData* target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, TransportType type, Owner owner, RailType railtype, uint reverse_penalty, byte pbs_mode)
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   841
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   842
	int r;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   843
	NPFFoundTargetData result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   844
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   845
	/* Initialize procs */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   846
	_npf_aystar.CalculateH = heuristic_proc;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   847
	_npf_aystar.EndNodeCheck = target_proc;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   848
	_npf_aystar.FoundEndNode = NPFSaveTargetData;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   849
	_npf_aystar.GetNeighbours = NPFFollowTrack;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   850
	if (type == TRANSPORT_RAIL)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   851
		_npf_aystar.CalculateG = NPFRailPathCost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   852
	else if (type == TRANSPORT_ROAD)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   853
		_npf_aystar.CalculateG = NPFRoadPathCost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   854
	else if (type == TRANSPORT_WATER)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   855
		_npf_aystar.CalculateG = NPFWaterPathCost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   856
	else
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   857
		assert(0);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   858
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   859
	if (pbs_mode != PBS_MODE_NONE)
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   860
		_npf_aystar.BeforeExit = NPFReservePBSPath;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   861
	else
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   862
		_npf_aystar.BeforeExit = NULL;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   863
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   864
	/* Initialize Start Node(s) */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   865
	start1->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   866
	start1->user_data[NPF_NODE_FLAGS] = 0;
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   867
	_npf_aystar.addstart(&_npf_aystar, start1, 0);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   868
	if (start2) {
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   869
		start2->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   870
		start2->user_data[NPF_NODE_FLAGS] = 0;
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   871
		NPFSetFlag(start2, NPF_FLAG_REVERSE, true);
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   872
		_npf_aystar.addstart(&_npf_aystar, start2, reverse_penalty);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   873
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   874
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   875
	/* Initialize result */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   876
	result.best_bird_dist = (uint)-1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   877
	result.best_path_dist = (uint)-1;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   878
	result.best_trackdir = INVALID_TRACKDIR;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   879
	_npf_aystar.user_path = &result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   880
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   881
	/* Initialize target */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   882
	_npf_aystar.user_target = target;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   883
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   884
	/* Initialize user_data */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   885
	_npf_aystar.user_data[NPF_TYPE] = type;
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   886
	_npf_aystar.user_data[NPF_OWNER] = owner;
2006
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1983
diff changeset
   887
	_npf_aystar.user_data[NPF_RAILTYPE] = railtype;
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   888
	_npf_aystar.user_data[NPF_PBS_MODE] = pbs_mode;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   889
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   890
	/* GO! */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   891
	r = AyStarMain_Main(&_npf_aystar);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   892
	assert(r != AYSTAR_STILL_BUSY);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   893
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   894
	if (result.best_bird_dist != 0) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   895
		if (target) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   896
			DEBUG(misc, 1) ("NPF: Could not find route to 0x%x from 0x%x.", target->dest_coords, start1->tile);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   897
		} else {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   898
			/* Assumption: target == NULL, so we are looking for a depot */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   899
			DEBUG(misc, 1) ("NPF: Could not find route to a depot from 0x%x.", start1->tile);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   900
		}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   901
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   902
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   903
	return result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   904
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   905
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   906
NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailType railtype, byte pbs_mode)
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   907
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   908
	AyStarNode start1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   909
	AyStarNode start2;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   910
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   911
	start1.tile = tile1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   912
	start2.tile = tile2;
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   913
	/* We set this in case the target is also the start tile, we will just
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   914
	 * return a not found then */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   915
	start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   916
	start1.direction = trackdir1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   917
	start2.direction = trackdir2;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   918
	start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   919
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   920
	return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, owner, railtype, 0, pbs_mode);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   921
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   922
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   923
NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailType railtype, byte pbs_mode)
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   924
{
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   925
	return NPFRouteToStationOrTileTwoWay(tile, trackdir, INVALID_TILE, 0, target, type, owner, railtype, pbs_mode);
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   926
}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   927
2006
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1983
diff changeset
   928
NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, TransportType type, Owner owner, RailType railtype, uint reverse_penalty)
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   929
{
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   930
	AyStarNode start1;
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   931
	AyStarNode start2;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   932
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   933
	start1.tile = tile1;
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   934
	start2.tile = tile2;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   935
	/* We set this in case the target is also the start tile, we will just
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   936
	 * return a not found then */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   937
	start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   938
	start1.direction = trackdir1;
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   939
	start2.direction = trackdir2;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   940
	start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   941
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   942
	/* perform a breadth first search. Target is NULL,
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   943
	 * since we are just looking for any depot...*/
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   944
	return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), NULL, NPFFindDepot, NPFCalcZero, type, owner, railtype, reverse_penalty, PBS_MODE_NONE);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   945
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   946
2006
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1983
diff changeset
   947
NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, TransportType type, Owner owner, RailType railtype)
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   948
{
2006
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1983
diff changeset
   949
	return NPFRouteToDepotBreadthFirstTwoWay(tile, trackdir, INVALID_TILE, 0, type, owner, railtype, 0);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   950
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   951
2006
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1983
diff changeset
   952
NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, TransportType type, Owner owner, RailType railtype)
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   953
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   954
	/* Okay, what we're gonna do. First, we look at all depots, calculate
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   955
	 * the manhatten distance to get to each depot. We then sort them by
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   956
	 * distance. We start by trying to plan a route to the closest, then
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   957
	 * the next closest, etc. We stop when the best route we have found so
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   958
	 * far, is shorter than the manhattan distance. This will obviously
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   959
	 * always find the closest depot. It will probably be most efficient
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   960
	 * for ships, since the heuristic will not be to far off then. I hope.
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   961
	 */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   962
	Queue depots;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   963
	int r;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   964
	NPFFoundTargetData best_result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   965
	NPFFoundTargetData result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   966
	NPFFindStationOrTileData target;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   967
	AyStarNode start;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   968
	Depot* current;
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
   969
	Depot *depot;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   970
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   971
	init_InsSort(&depots);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   972
	/* Okay, let's find all depots that we can use first */
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
   973
	FOR_ALL_DEPOTS(depot) {
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   974
		/* Check if this is really a valid depot, it is of the needed type and
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   975
		 * owner */
1338
9e3e0bc7fe61 (svn r1842) Fix another typo made in r1834
tron
parents: 1330
diff changeset
   976
		if (IsValidDepot(depot) && IsTileDepotType(depot->xy, type) && IsTileOwner(depot->xy, owner))
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   977
			/* If so, let's add it to the queue, sorted by distance */
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
   978
			depots.push(&depots, depot, DistanceManhattan(tile, depot->xy));
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   979
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   980
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   981
	/* Now, let's initialise the aystar */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   982
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   983
	/* Initialize procs */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   984
	_npf_aystar.CalculateH = NPFCalcStationOrTileHeuristic;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   985
	_npf_aystar.EndNodeCheck = NPFFindStationOrTile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   986
	_npf_aystar.FoundEndNode = NPFSaveTargetData;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   987
	_npf_aystar.GetNeighbours = NPFFollowTrack;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   988
	if (type == TRANSPORT_RAIL)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   989
		_npf_aystar.CalculateG = NPFRailPathCost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   990
	else if (type == TRANSPORT_ROAD)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   991
		_npf_aystar.CalculateG = NPFRoadPathCost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   992
	else if (type == TRANSPORT_WATER)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   993
		_npf_aystar.CalculateG = NPFWaterPathCost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   994
	else
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   995
		assert(0);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   996
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   997
	_npf_aystar.BeforeExit = NULL;
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   998
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   999
	/* Initialize target */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1000
	target.station_index = -1; /* We will initialize dest_coords inside the loop below */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1001
	_npf_aystar.user_target = &target;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1002
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1003
	/* Initialize user_data */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1004
	_npf_aystar.user_data[NPF_TYPE] = type;
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
  1005
	_npf_aystar.user_data[NPF_OWNER] = owner;
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1006
	_npf_aystar.user_data[NPF_PBS_MODE] = PBS_MODE_NONE;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1007
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1008
	/* Initialize Start Node */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1009
	start.tile = tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1010
	start.direction = trackdir; /* We will initialize user_data inside the loop below */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1011
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1012
	/* Initialize Result */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1013
	_npf_aystar.user_path = &result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1014
	best_result.best_path_dist = (uint)-1;
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
  1015
	best_result.best_bird_dist = (uint)-1;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1016
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1017
	/* Just iterate the depots in order of increasing distance */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1018
	while ((current = depots.pop(&depots))) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1019
		/* Check to see if we already have a path shorter than this
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
  1020
		 * depot's manhattan distance. HACK: We call DistanceManhattan
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1021
		 * again, we should probably modify the queue to give us that
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1022
		 * value... */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1023
		if ( DistanceManhattan(tile, current->xy * NPF_TILE_LENGTH) > best_result.best_path_dist)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1024
			break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1025
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1026
		/* Initialize Start Node */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1027
		/* We set this in case the target is also the start tile, we will just
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1028
		 * return a not found then */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
  1029
		start.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1030
		start.user_data[NPF_NODE_FLAGS] = 0;
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
  1031
		_npf_aystar.addstart(&_npf_aystar, &start, 0);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1032
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1033
		/* Initialize result */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1034
		result.best_bird_dist = (uint)-1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1035
		result.best_path_dist = (uint)-1;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
  1036
		result.best_trackdir = INVALID_TRACKDIR;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1037
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1038
		/* Initialize target */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1039
		target.dest_coords = current->xy;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1040
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1041
		/* GO! */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1042
		r = AyStarMain_Main(&_npf_aystar);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1043
		assert(r != AYSTAR_STILL_BUSY);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1044
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1045
		/* This depot is closer */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1046
		if (result.best_path_dist < best_result.best_path_dist)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1047
			best_result = result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1048
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1049
	if (result.best_bird_dist != 0) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1050
		DEBUG(misc, 1) ("NPF: Could not find route to any depot from 0x%x.", tile);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1051
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1052
	return best_result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1053
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1054
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1055
void InitializeNPF(void)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1056
{
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
  1057
	init_AyStar(&_npf_aystar, NPFHash, NPF_HASH_SIZE);
1463
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
  1058
	_npf_aystar.loops_per_tick = 0;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
  1059
	_npf_aystar.max_path_cost = 0;
1700
b8ecf0494fdd (svn r2204) - Add: [NPF] NPF now has a maximum number of nodes it will search. The default value is 5000 for now, which is an educated guess. Probably needs some finetuning. Hopefully this "feature" can be removed later on, when more sophisticated means of limiting the pathfinder have been implemented. This should make ships and larger networks playable for now, though.
matthijs
parents: 1678
diff changeset
  1060
	//_npf_aystar.max_search_nodes = 0;
b8ecf0494fdd (svn r2204) - Add: [NPF] NPF now has a maximum number of nodes it will search. The default value is 5000 for now, which is an educated guess. Probably needs some finetuning. Hopefully this "feature" can be removed later on, when more sophisticated means of limiting the pathfinder have been implemented. This should make ships and larger networks playable for now, though.
matthijs
parents: 1678
diff changeset
  1061
	/* We will limit the number of nodes for now, until we have a better
b8ecf0494fdd (svn r2204) - Add: [NPF] NPF now has a maximum number of nodes it will search. The default value is 5000 for now, which is an educated guess. Probably needs some finetuning. Hopefully this "feature" can be removed later on, when more sophisticated means of limiting the pathfinder have been implemented. This should make ships and larger networks playable for now, though.
matthijs
parents: 1678
diff changeset
  1062
	 * solution to really fix performance */
b8ecf0494fdd (svn r2204) - Add: [NPF] NPF now has a maximum number of nodes it will search. The default value is 5000 for now, which is an educated guess. Probably needs some finetuning. Hopefully this "feature" can be removed later on, when more sophisticated means of limiting the pathfinder have been implemented. This should make ships and larger networks playable for now, though.
matthijs
parents: 1678
diff changeset
  1063
	_npf_aystar.max_search_nodes = _patches.npf_max_search_nodes;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1064
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1065
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
  1066
void NPFFillWithOrderData(NPFFindStationOrTileData* fstd, Vehicle* v)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
  1067
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1068
	/* Ships don't really reach their stations, but the tile in front. So don't
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1069
	 * save the station id for ships. For roadvehs we don't store it either,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1070
	 * because multistop depends on vehicles actually reaching the exact
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1071
	 * dest_tile, not just any stop of that station.
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1072
	 * So only for train orders to stations we fill fstd->station_index, for all
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1073
	 * others only dest_coords */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1074
	if ((v->current_order.type) == OT_GOTO_STATION && v->type == VEH_Train) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1075
		fstd->station_index = v->current_order.station;
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
  1076
		/* Let's take the closest tile of the station as our target for trains */
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
  1077
		fstd->dest_coords = CalcClosestStationTile(v->current_order.station, v->tile);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1078
	} else {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1079
		fstd->dest_coords = v->dest_tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1080
		fstd->station_index = -1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1081
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1082
}