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