src/npf.cpp
author rubidium
Wed, 19 Dec 2007 23:26:02 +0000
changeset 8604 8afdd9877afd
parent 8584 a8b6dffead63
child 8609 8c0c3e9dd6a0
permissions -rw-r--r--
(svn r11669) -Codechange: refactor tile.h -> tile_type.h and tile_map.h
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2182
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2182
diff changeset
     2
6674
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
     3
/** @file npf.cpp */
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
     4
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
     5
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1857
diff changeset
     6
#include "openttd.h"
3234
986c30171e92 (svn r3907) Replace many bridge related direct map accesses with calls to shiny new functions and mark some strange constructs with XXX
tron
parents: 3184
diff changeset
     7
#include "bridge_map.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1248
diff changeset
     8
#include "debug.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2115
diff changeset
     9
#include "functions.h"
6949
72d11a1e1e60 (svn r9609) -Codechange: Move some function prototypes out of functions.h and into landscape.h, and add a few where they didn't exist.
maedhros
parents: 6674
diff changeset
    10
#include "landscape.h"
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    11
#include "npf.h"
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    12
#include "aystar.h"
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    13
#include "macros.h"
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    14
#include "pathfind.h"
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    15
#include "station.h"
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3309
diff changeset
    16
#include "station_map.h"
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
    17
#include "depot.h"
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3147
diff changeset
    18
#include "tunnel_map.h"
5720
cc0ceeafaa55 (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5573
diff changeset
    19
#include "network/network.h"
3957
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
    20
#include "water_map.h"
8579
3efbb430092e (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 8466
diff changeset
    21
#include "tunnelbridge_map.h"
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    22
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
    23
static AyStar _npf_aystar;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    24
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    25
/* The cost of each trackdir. A diagonal piece is the full NPF_TILE_LENGTH,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    26
 * the shorter piece is sqrt(2)/2*NPF_TILE_LENGTH =~ 0.7071
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    27
 */
1677
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
    28
#define NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH)
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
    29
static const uint _trackdir_length[TRACKDIR_END] = {
1463
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
    30
	NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH,
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
    31
	0, 0,
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
    32
	NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    33
};
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    34
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    35
/**
2403
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    36
 * Calculates the minimum distance traveled to get from t0 to t1 when only
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    37
 * using tracks (ie, only making 45 degree turns). Returns the distance in the
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    38
 * NPF scale, ie the number of full tiles multiplied by NPF_TILE_LENGTH to
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    39
 * prevent rounding.
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    40
 */
2416
d3f8a53cedf6 (svn r2942) Staticise some functions
tron
parents: 2403
diff changeset
    41
static uint NPFDistanceTrack(TileIndex t0, TileIndex t1)
2403
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    42
{
8466
9ce95e16f9f9 (svn r11526) -Codechange: Rename the function delta fitting to the naming style
skidd13
parents: 8424
diff changeset
    43
	const uint dx = Delta(TileX(t0), TileX(t1));
9ce95e16f9f9 (svn r11526) -Codechange: Rename the function delta fitting to the naming style
skidd13
parents: 8424
diff changeset
    44
	const uint dy = Delta(TileY(t0), TileY(t1));
2403
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    45
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    46
	const uint straightTracks = 2 * min(dx, dy); /* The number of straight (not full length) tracks */
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    47
	/* OPTIMISATION:
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    48
	 * Original: diagTracks = max(dx, dy) - min(dx,dy);
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    49
	 * Proof:
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    50
	 * (dx+dy) - straightTracks  == (min + max) - straightTracks = min + max - 2 * min = max - min */
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    51
	const uint diagTracks = dx + dy - straightTracks; /* The number of diagonal (full tile length) tracks. */
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    52
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    53
	/* Don't factor out NPF_TILE_LENGTH below, this will round values and lose
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    54
	 * precision */
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    55
	return diagTracks * NPF_TILE_LENGTH + straightTracks * NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH;
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    56
}
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    57
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
    58
2075
7f0ca01392db (svn r2585) - Fix [Makefile]: some small cleanups, remove warnings, and add mersenne to makefile (Luca)
Darkvater
parents: 2049
diff changeset
    59
#if 0
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
    60
static uint NTPHash(uint key1, uint key2)
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    61
{
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
    62
	/* This function uses the old hash, which is fixed on 10 bits (1024 buckets) */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    63
	return PATHFIND_HASH_TILE(key1);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    64
}
2075
7f0ca01392db (svn r2585) - Fix [Makefile]: some small cleanups, remove warnings, and add mersenne to makefile (Luca)
Darkvater
parents: 2049
diff changeset
    65
#endif
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    66
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
    67
/**
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
    68
 * Calculates a hash value for use in the NPF.
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4424
diff changeset
    69
 * @param key1 The TileIndex of the tile to hash
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4424
diff changeset
    70
 * @param key2 The Trackdir of the track on the tile.
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
    71
 *
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4424
diff changeset
    72
 * @todo Think of a better hash.
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
    73
 */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
    74
static uint NPFHash(uint key1, uint key2)
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
    75
{
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
    76
	/* TODO: think of a better hash? */
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
    77
	uint part1 = TileX(key1) & NPF_HASH_HALFMASK;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
    78
	uint part2 = TileY(key1) & NPF_HASH_HALFMASK;
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
    79
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    80
	assert(IsValidTrackdir((Trackdir)key2));
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
    81
	assert(IsValidTile(key1));
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
    82
	return ((part1 << NPF_HASH_HALFBITS | part2) + (NPF_HASH_SIZE * key2 / TRACKDIR_END)) % NPF_HASH_SIZE;
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
    83
}
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
    84
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
    85
static int32 NPFCalcZero(AyStar* as, AyStarNode* current, OpenListNode* parent)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
    86
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    87
	return 0;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    88
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    89
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
    90
/* Calcs the tile of given station that is closest to a given tile
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
    91
 * for this we assume the station is a rectangle,
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
    92
 * as defined by its top tile (st->train_tile) and its width/height (st->trainst_w, st->trainst_h)
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    93
 */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
    94
static TileIndex CalcClosestStationTile(StationID station, TileIndex tile)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
    95
{
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
    96
	const Station* st = GetStation(station);
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
    97
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
    98
	uint minx = TileX(st->train_tile);  // topmost corner of station
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
    99
	uint miny = TileY(st->train_tile);
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   100
	uint maxx = minx + st->trainst_w - 1; // lowermost corner of station
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   101
	uint maxy = miny + st->trainst_h - 1;
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   102
	uint x;
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   103
	uint y;
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   104
6674
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
   105
	/* we are going the aim for the x coordinate of the closest corner
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
   106
	 * but if we are between those coordinates, we will aim for our own x coordinate */
8418
b49fc6be1ab9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 8260
diff changeset
   107
	x = Clamp(TileX(tile), minx, maxx);
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   108
6674
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
   109
	/* same for y coordinate, see above comment */
8418
b49fc6be1ab9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 8260
diff changeset
   110
	y = Clamp(TileY(tile), miny, maxy);
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   111
6674
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
   112
	/* return the tile of our target coordinates */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   113
	return TileXY(x, y);
2182
89e6e39734a4 (svn r2696) Remove stray semicolons
tron
parents: 2164
diff changeset
   114
}
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   115
1677
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   116
/* Calcs the heuristic to the target station or tile. For train stations, it
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   117
 * takes into account the direction of approach.
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   118
 */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   119
static int32 NPFCalcStationOrTileHeuristic(AyStar* as, AyStarNode* current, OpenListNode* parent)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   120
{
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   121
	NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   122
	NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   123
	TileIndex from = current->tile;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   124
	TileIndex to = fstd->dest_coords;
1453
687663191db3 (svn r1957) Compilation fix.
pasky
parents: 1452
diff changeset
   125
	uint dist;
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   126
6674
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
   127
	/* for train-stations, we are going to aim for the closest station tile */
3135
3a20ced2e34b (svn r3750) Use INVALID_STATION instead of -1 in NPF
tron
parents: 3053
diff changeset
   128
	if (as->user_data[NPF_TYPE] == TRANSPORT_RAIL && fstd->station_index != INVALID_STATION)
1453
687663191db3 (svn r1957) Compilation fix.
pasky
parents: 1452
diff changeset
   129
		to = CalcClosestStationTile(fstd->station_index, from);
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   130
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   131
	if (as->user_data[NPF_TYPE] == TRANSPORT_ROAD) {
1677
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   132
		/* Since roads only have diagonal pieces, we use manhattan distance here */
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   133
		dist = DistanceManhattan(from, to) * NPF_TILE_LENGTH;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   134
	} else {
1677
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   135
		/* Ships and trains can also go diagonal, so the minimum distance is shorter */
2403
f339737b38bc (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
   136
		dist = NPFDistanceTrack(from, to);
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   137
	}
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   138
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   139
	DEBUG(npf, 4, "Calculating H for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), dist);
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   140
2916
8f1aa489701f (svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents: 2878
diff changeset
   141
	if (dist < ftd->best_bird_dist) {
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   142
		ftd->best_bird_dist = dist;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   143
		ftd->best_trackdir = (Trackdir)current->user_data[NPF_TRACKDIR_CHOICE];
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   144
	}
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   145
	return dist;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   146
}
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   147
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   148
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   149
/* Fills AyStarNode.user_data[NPF_TRACKDIRCHOICE] with the chosen direction to
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   150
 * get here, either getting it from the current choice or from the parent's
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   151
 * choice */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   152
static void NPFFillTrackdirChoice(AyStarNode* current, OpenListNode* parent)
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   153
{
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   154
	if (parent->path.parent == NULL) {
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   155
		Trackdir trackdir = (Trackdir)current->direction;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   156
		/* This is a first order decision, so we'd better save the
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   157
		 * direction we chose */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   158
		current->user_data[NPF_TRACKDIR_CHOICE] = trackdir;
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   159
		DEBUG(npf, 6, "Saving trackdir: 0x%X", trackdir);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   160
	} else {
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   161
		/* We've already made the decision, so just save our parent's decision */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   162
		current->user_data[NPF_TRACKDIR_CHOICE] = parent->path.node.user_data[NPF_TRACKDIR_CHOICE];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   163
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   164
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   165
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   166
/* Will return the cost of the tunnel. If it is an entry, it will return the
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   167
 * cost of that tile. If the tile is an exit, it will return the tunnel length
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   168
 * including the exit tile. Requires that this is a Tunnel tile */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   169
static uint NPFTunnelCost(AyStarNode* current)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   170
{
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   171
	DiagDirection exitdir = TrackdirToExitdir((Trackdir)current->direction);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   172
	TileIndex tile = current->tile;
8579
3efbb430092e (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 8466
diff changeset
   173
	if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   174
		/* We just popped out if this tunnel, since were
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   175
		 * facing the tunnel exit */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   176
		FindLengthOfTunnelResult flotr;
3147
0a09ce6d651a (svn r3767) Move all direction related enums and functions to a separate header
tron
parents: 3135
diff changeset
   177
		flotr = FindLengthOfTunnel(tile, ReverseDiagDir(exitdir));
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   178
		return flotr.length * NPF_TILE_LENGTH;
6674
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
   179
		/* @todo: Penalty for tunnels? */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   180
	} else {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   181
		/* We are entering the tunnel, the enter tile is just a
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   182
		 * straight track */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   183
		return NPF_TILE_LENGTH;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   184
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   185
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   186
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   187
static inline uint NPFBridgeCost(AyStarNode *current)
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   188
{
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   189
	return NPF_TILE_LENGTH * GetBridgeLength(current->tile, GetOtherBridgeEnd(current->tile));
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   190
}
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   191
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   192
static uint NPFSlopeCost(AyStarNode* current)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   193
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   194
	TileIndex next = current->tile + TileOffsByDiagDir(TrackdirToExitdir((Trackdir)current->direction));
1503
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   195
8260
c7d75cebe3f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7866
diff changeset
   196
	/* Get center of tiles */
c7d75cebe3f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7866
diff changeset
   197
	int x1 = TileX(current->tile) * TILE_SIZE + TILE_SIZE / 2;
c7d75cebe3f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7866
diff changeset
   198
	int y1 = TileY(current->tile) * TILE_SIZE + TILE_SIZE / 2;
c7d75cebe3f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7866
diff changeset
   199
	int x2 = TileX(next) * TILE_SIZE + TILE_SIZE / 2;
c7d75cebe3f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7866
diff changeset
   200
	int y2 = TileY(next) * TILE_SIZE + TILE_SIZE / 2;
1503
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   201
8260
c7d75cebe3f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7866
diff changeset
   202
	int dx4 = (x2 - x1) / 4;
c7d75cebe3f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7866
diff changeset
   203
	int dy4 = (y2 - y1) / 4;
c7d75cebe3f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7866
diff changeset
   204
c7d75cebe3f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7866
diff changeset
   205
	/* Get the height on both sides of the tile edge.
c7d75cebe3f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7866
diff changeset
   206
	 * Avoid testing the height on the tile-center. This will fail for halftile-foundations.
c7d75cebe3f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7866
diff changeset
   207
	 */
c7d75cebe3f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7866
diff changeset
   208
	int z1 = GetSlopeZ(x1 + dx4, y1 + dy4);
c7d75cebe3f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7866
diff changeset
   209
	int z2 = GetSlopeZ(x2 - dx4, y2 - dy4);
1503
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   210
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   211
	if (z2 - z1 > 1) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   212
		/* Slope up */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   213
		return _patches.npf_rail_slope_penalty;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   214
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   215
	return 0;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   216
	/* Should we give a bonus for slope down? Probably not, we
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   217
	 * could just substract that bonus from the penalty, because
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   218
	 * there is only one level of steepness... */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   219
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   220
3532
446072c37643 (svn r4389) -Fix: [NPF] Don't mark tiles when debugging in multiplayer, this will cause desyncs.
matthijs
parents: 3531
diff changeset
   221
/**
446072c37643 (svn r4389) -Fix: [NPF] Don't mark tiles when debugging in multiplayer, this will cause desyncs.
matthijs
parents: 3531
diff changeset
   222
 * Mark tiles by mowing the grass when npf debug level >= 1.
446072c37643 (svn r4389) -Fix: [NPF] Don't mark tiles when debugging in multiplayer, this will cause desyncs.
matthijs
parents: 3531
diff changeset
   223
 * Will not work for multiplayer games, since it can (will) cause desyncs.
446072c37643 (svn r4389) -Fix: [NPF] Don't mark tiles when debugging in multiplayer, this will cause desyncs.
matthijs
parents: 3531
diff changeset
   224
 */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   225
static void NPFMarkTile(TileIndex tile)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   226
{
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4048
diff changeset
   227
#ifndef NO_DEBUG_MESSAGES
3532
446072c37643 (svn r4389) -Fix: [NPF] Don't mark tiles when debugging in multiplayer, this will cause desyncs.
matthijs
parents: 3531
diff changeset
   228
	if (_debug_npf_level < 1 || _networking) return;
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   229
	switch (GetTileType(tile)) {
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   230
		case MP_RAILWAY:
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   231
			/* DEBUG: mark visited tiles by mowing the grass under them ;-) */
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   232
			if (!IsTileDepotType(tile, TRANSPORT_RAIL)) {
3531
07fb46c7d469 (svn r4387) -Codechange: Use map accessors even for debugging purposes (npf).
celestar
parents: 3520
diff changeset
   233
				SetRailGroundType(tile, RAIL_GROUND_BARREN);
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   234
				MarkTileDirtyByTile(tile);
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   235
			}
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   236
			break;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   237
7866
e19fda04e8d3 (svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents: 7586
diff changeset
   238
		case MP_ROAD:
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   239
			if (!IsTileDepotType(tile, TRANSPORT_ROAD)) {
4048
e773f441eb4f (svn r5317) s/RGT_/ROADSIDE_/ and some minor changes
tron
parents: 4000
diff changeset
   240
				SetRoadside(tile, ROADSIDE_BARREN);
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   241
				MarkTileDirtyByTile(tile);
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   242
			}
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   243
			break;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   244
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   245
		default:
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   246
			break;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   247
	}
1678
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   248
#endif
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   249
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   250
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   251
static int32 NPFWaterPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   252
{
6674
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
   253
	/* TileIndex tile = current->tile; */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   254
	int32 cost = 0;
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   255
	Trackdir trackdir = (Trackdir)current->direction;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   256
6674
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
   257
	cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   258
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   259
	if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(trackdir))
6674
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
   260
		cost += _patches.npf_buoy_penalty; // A small penalty for going over buoys
1751
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   261
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   262
	if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction))
1751
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   263
		cost += _patches.npf_water_curve_penalty;
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   264
6674
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
   265
	/* @todo More penalties? */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   266
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   267
	return cost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   268
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   269
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   270
/* Determine the cost of this node, for road tracks */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   271
static int32 NPFRoadPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   272
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   273
	TileIndex tile = current->tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   274
	int32 cost = 0;
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   275
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   276
	/* Determine base length */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   277
	switch (GetTileType(tile)) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   278
		case MP_TUNNELBRIDGE:
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   279
			cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
2605
d9d7eecdd1f1 (svn r3142) Remove a now invalid case fall-through and use helper function instead of map access.
peter1138
parents: 2493
diff changeset
   280
			break;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   281
7866
e19fda04e8d3 (svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents: 7586
diff changeset
   282
		case MP_ROAD:
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   283
			cost = NPF_TILE_LENGTH;
2006
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1983
diff changeset
   284
			/* Increase the cost for level crossings */
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3993
diff changeset
   285
			if (IsLevelCrossing(tile)) cost += _patches.npf_crossing_penalty;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   286
			break;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   287
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5849
diff changeset
   288
		case MP_STATION:
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5849
diff changeset
   289
			cost = NPF_TILE_LENGTH;
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5849
diff changeset
   290
			/* Increase the cost for drive-through road stops */
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5849
diff changeset
   291
			if (IsDriveThroughStopTile(tile)) cost += _patches.npf_road_drive_through_penalty;
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5849
diff changeset
   292
			break;
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5849
diff changeset
   293
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   294
		default:
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   295
			break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   296
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   297
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   298
	/* Determine extra costs */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   299
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   300
	/* Check for slope */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   301
	cost += NPFSlopeCost(current);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   302
1941
b1cb02c0401c (svn r2447) * Add: [NPF] Penalty for road vehicles making turns.
matthijs
parents: 1927
diff changeset
   303
	/* Check for turns. Road vehicles only really drive diagonal, turns are
b1cb02c0401c (svn r2447) * Add: [NPF] Penalty for road vehicles making turns.
matthijs
parents: 1927
diff changeset
   304
	 * represented by non-diagonal tracks */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   305
	if (!IsDiagonalTrackdir((Trackdir)current->direction))
1941
b1cb02c0401c (svn r2447) * Add: [NPF] Penalty for road vehicles making turns.
matthijs
parents: 1927
diff changeset
   306
		cost += _patches.npf_road_curve_penalty;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   307
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   308
	NPFMarkTile(tile);
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   309
	DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   310
	return cost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   311
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   312
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   313
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   314
/* Determine the cost of this node, for railway tracks */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   315
static int32 NPFRailPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   316
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   317
	TileIndex tile = current->tile;
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   318
	Trackdir trackdir = (Trackdir)current->direction;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   319
	int32 cost = 0;
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   320
	/* HACK: We create a OpenListNode manually, so we can call EndNodeCheck */
1617
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   321
	OpenListNode new_node;
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   322
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   323
	/* Determine base length */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   324
	switch (GetTileType(tile)) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   325
		case MP_TUNNELBRIDGE:
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   326
			cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   327
			break;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   328
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   329
		case MP_RAILWAY:
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   330
			cost = _trackdir_length[trackdir]; /* Should be different for diagonal tracks */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   331
			break;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   332
7866
e19fda04e8d3 (svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents: 7586
diff changeset
   333
		case MP_ROAD: /* Railway crossing */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   334
			cost = NPF_TILE_LENGTH;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   335
			break;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   336
1503
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   337
		case MP_STATION:
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   338
			/* We give a station tile a penalty. Logically we would only want to give
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   339
			 * station tiles that are not our destination this penalty. This would
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   340
			 * discourage trains to drive through busy stations. But, we can just
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   341
			 * give any station tile a penalty, because every possible route will get
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   342
			 * this penalty exactly once, on its end tile (if it's a station) and it
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   343
			 * will therefore not make a difference. */
1503
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   344
			cost = NPF_TILE_LENGTH + _patches.npf_rail_station_penalty;
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   345
			break;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   346
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   347
		default:
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   348
			break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   349
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   350
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   351
	/* Determine extra costs */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   352
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   353
	/* Check for signals */
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   354
	if (IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, trackdir)) {
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   355
		/* Ordinary track with signals */
3520
092efede26b3 (svn r4376) -Codechange Renamed GetSignalState to GetSignalStateByTrackdir
celestar
parents: 3498
diff changeset
   356
		if (GetSignalStateByTrackdir(tile, trackdir) == SIGNAL_STATE_RED) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   357
			/* Signal facing us is red */
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   358
			if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   359
				/* Penalize the first signal we
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   360
				 * encounter, if it is red */
1643
d38655053062 (svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents: 1617
diff changeset
   361
d38655053062 (svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents: 1617
diff changeset
   362
				/* Is this a presignal exit or combo? */
7266
4b75e7b9fa91 (svn r10006) -Fix (FS#362): allow different signal types on one tile
glx
parents: 7179
diff changeset
   363
				SignalType sigtype = GetSignalType(tile, TrackdirToTrack(trackdir));
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   364
				if (sigtype == SIGTYPE_EXIT || sigtype == SIGTYPE_COMBO) {
1643
d38655053062 (svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents: 1617
diff changeset
   365
					/* Penalise exit and combo signals differently (heavier) */
d38655053062 (svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents: 1617
diff changeset
   366
					cost += _patches.npf_rail_firstred_exit_penalty;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   367
				} else {
1643
d38655053062 (svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents: 1617
diff changeset
   368
					cost += _patches.npf_rail_firstred_penalty;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   369
				}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   370
			}
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   371
			/* Record the state of this signal */
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   372
			NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, true);
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   373
		} else {
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   374
			/* Record the state of this signal */
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   375
			NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, false);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   376
		}
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   377
		NPFSetFlag(current, NPF_FLAG_SEEN_SIGNAL, true);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   378
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   379
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   380
	/* Penalise the tile if it is a target tile and the last signal was
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   381
	 * red */
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   382
	/* HACK: We create a new_node here so we can call EndNodeCheck. Ugly as hell
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   383
	 * of course... */
1617
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   384
	new_node.path.node = *current;
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   385
	if (as->EndNodeCheck(as, &new_node) == AYSTAR_FOUND_END_NODE && NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_RED))
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   386
		cost += _patches.npf_rail_lastred_penalty;
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   387
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   388
	/* Check for slope */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   389
	cost += NPFSlopeCost(current);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   390
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   391
	/* Check for turns */
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   392
	if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction))
1460
ebd1cfae9588 (svn r1964) - Add: [NPF] Added a penalty
matthijs
parents: 1459
diff changeset
   393
		cost += _patches.npf_rail_curve_penalty;
6674
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
   394
	/*TODO, with realistic acceleration, also the amount of straight track between
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
   395
	 *      curves should be taken into account, as this affects the speed limit. */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   396
2916
8f1aa489701f (svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents: 2878
diff changeset
   397
	/* Check for reverse in depot */
8f1aa489701f (svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents: 2878
diff changeset
   398
	if (IsTileDepotType(tile, TRANSPORT_RAIL) && as->EndNodeCheck(as, &new_node) != AYSTAR_FOUND_END_NODE) {
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   399
		/* Penalise any depot tile that is not the last tile in the path. This
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   400
		 * _should_ penalise every occurence of reversing in a depot (and only
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   401
		 * that) */
2916
8f1aa489701f (svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents: 2878
diff changeset
   402
		cost += _patches.npf_rail_depot_reverse_penalty;
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   403
	}
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   404
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   405
	/* Check for occupied track */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   406
	//TODO
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   407
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   408
	NPFMarkTile(tile);
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   409
	DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   410
	return cost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   411
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   412
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   413
/* Will find any depot */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   414
static int32 NPFFindDepot(AyStar* as, OpenListNode *current)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   415
{
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   416
	/* It's not worth caching the result with NPF_FLAG_IS_TARGET here as below,
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   417
	 * since checking the cache not that much faster than the actual check */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   418
	return IsTileDepotType(current->path.node.tile, (TransportType)as->user_data[NPF_TYPE]) ?
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3993
diff changeset
   419
		AYSTAR_FOUND_END_NODE : AYSTAR_DONE;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   420
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   421
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   422
/* Will find a station identified using the NPFFindStationOrTileData */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   423
static int32 NPFFindStationOrTile(AyStar* as, OpenListNode *current)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   424
{
1464
266d3b0ee2c8 (svn r1968) - Fix: [NPF] Mixed declarations and statements
matthijs
parents: 1463
diff changeset
   425
	NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target;
1617
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   426
	AyStarNode *node = &current->path.node;
1464
266d3b0ee2c8 (svn r1968) - Fix: [NPF] Mixed declarations and statements
matthijs
parents: 1463
diff changeset
   427
	TileIndex tile = node->tile;
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   428
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   429
	/* If GetNeighbours said we could get here, we assume the station type
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   430
	 * is correct */
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   431
	if (
3135
3a20ced2e34b (svn r3750) Use INVALID_STATION instead of -1 in NPF
tron
parents: 3053
diff changeset
   432
		(fstd->station_index == INVALID_STATION && tile == fstd->dest_coords) || /* We've found the tile, or */
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3309
diff changeset
   433
		(IsTileType(tile, MP_STATION) && GetStationIndex(tile) == fstd->station_index) /* the station */
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   434
	) {
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   435
		return AYSTAR_FOUND_END_NODE;
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   436
	} else {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   437
		return AYSTAR_DONE;
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   438
	}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   439
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   440
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   441
/* To be called when current contains the (shortest route to) the target node.
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   442
 * Will fill the contents of the NPFFoundTargetData using
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   443
 * AyStarNode[NPF_TRACKDIR_CHOICE].
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   444
 */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   445
static void NPFSaveTargetData(AyStar* as, OpenListNode* current)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   446
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   447
	NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path;
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   448
	ftd->best_trackdir = (Trackdir)current->path.node.user_data[NPF_TRACKDIR_CHOICE];
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   449
	ftd->best_path_dist = current->g;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   450
	ftd->best_bird_dist = 0;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   451
	ftd->node = current->path.node;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   452
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   453
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   454
/**
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   455
 * Finds out if a given player's vehicles are allowed to enter a given tile.
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   456
 * @param owner    The owner of the vehicle.
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   457
 * @param tile     The tile that is about to be entered.
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   458
 * @param enterdir The direction from which the vehicle wants to enter the tile.
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   459
 * @return         true if the vehicle can enter the tile.
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   460
 * @todo           This function should be used in other places than just NPF,
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   461
 *                 maybe moved to another file too.
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   462
 */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   463
static bool VehicleMayEnterTile(Owner owner, TileIndex tile, DiagDirection enterdir)
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   464
{
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   465
	if (IsTileType(tile, MP_RAILWAY) ||           /* Rail tile (also rail depot) */
3442
c494e7246d7b (svn r4272) -Codechange: Moved the map-accessing stuff from station.h into station_map.h
celestar
parents: 3404
diff changeset
   466
			IsRailwayStationTile(tile) ||               /* Rail station tile */
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   467
			IsTileDepotType(tile, TRANSPORT_ROAD) ||  /* Road depot tile */
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5849
diff changeset
   468
			IsStandardRoadStopTile(tile) || /* Road station tile (but not drive-through stops) */
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   469
			IsTileDepotType(tile, TRANSPORT_WATER)) { /* Water depot tile */
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   470
		return IsTileOwner(tile, owner); /* You need to own these tiles entirely to use them */
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   471
	}
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   472
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   473
	switch (GetTileType(tile)) {
7866
e19fda04e8d3 (svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents: 7586
diff changeset
   474
		case MP_ROAD:
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   475
			/* rail-road crossing : are we looking at the railway part? */
3498
3441bc83c28a (svn r4349) Remove GetCrossingTransportType(), it's slightly overkill
tron
parents: 3442
diff changeset
   476
			if (IsLevelCrossing(tile) &&
3441bc83c28a (svn r4349) Remove GetCrossingTransportType(), it's slightly overkill
tron
parents: 3442
diff changeset
   477
					DiagDirToAxis(enterdir) != GetCrossingRoadAxis(tile)) {
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   478
				return IsTileOwner(tile, owner); /* Railway needs owner check, while the street is public */
3498
3441bc83c28a (svn r4349) Remove GetCrossingTransportType(), it's slightly overkill
tron
parents: 3442
diff changeset
   479
			}
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   480
			break;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   481
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   482
		case MP_TUNNELBRIDGE:
8584
a8b6dffead63 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8579
diff changeset
   483
			if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) {
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   484
				return IsTileOwner(tile, owner);
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   485
			}
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   486
			break;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   487
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   488
		default:
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   489
			break;
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   490
	}
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   491
6674
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
   492
	return true; // no need to check
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   493
}
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   494
3957
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   495
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   496
/**
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   497
 * Returns the direction the exit of the depot on the given tile is facing.
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   498
 */
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   499
static DiagDirection GetDepotDirection(TileIndex tile, TransportType type)
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   500
{
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   501
	assert(IsTileDepotType(tile, type));
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   502
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   503
	switch (type) {
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   504
		case TRANSPORT_RAIL:  return GetRailDepotDirection(tile);
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   505
		case TRANSPORT_ROAD:  return GetRoadDepotDirection(tile);
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   506
		case TRANSPORT_WATER: return GetShipDepotDirection(tile);
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   507
		default: return INVALID_DIAGDIR; /* Not reached */
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   508
	}
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   509
}
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   510
3c39694becd2 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   511
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   512
/* Will just follow the results of GetTileTrackStatus concerning where we can
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   513
 * go and where not. Uses AyStar.user_data[NPF_TYPE] as the transport type and
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   514
 * an argument to GetTileTrackStatus. Will skip tunnels, meaning that the
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   515
 * entry and exit are neighbours. Will fill
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   516
 * AyStarNode.user_data[NPF_TRACKDIR_CHOICE] with an appropriate value, and
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   517
 * copy AyStarNode.user_data[NPF_NODE_FLAGS] from the parent */
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   518
static void NPFFollowTrack(AyStar* aystar, OpenListNode* current)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   519
{
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   520
	Trackdir src_trackdir = (Trackdir)current->path.node.direction;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   521
	TileIndex src_tile = current->path.node.tile;
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   522
	DiagDirection src_exitdir = TrackdirToExitdir(src_trackdir);
3295
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   523
	TileIndex dst_tile = INVALID_TILE;
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   524
	int i;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   525
	uint32 ts;
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   526
	TrackdirBits trackdirbits;
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   527
	TransportType type = (TransportType)aystar->user_data[NPF_TYPE];
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6949
diff changeset
   528
	uint subtype = aystar->user_data[NPF_SUB_TYPE];
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   529
	bool override_dst_check = false;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   530
	/* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   531
	aystar->num_neighbours = 0;
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   532
	DEBUG(npf, 4, "Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   533
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   534
	/* Find dest tile */
8584
a8b6dffead63 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8579
diff changeset
   535
	if (IsTileType(src_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(src_tile) == src_exitdir) {
a8b6dffead63 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8579
diff changeset
   536
		/* This is a tunnel/bridge. We know this tunnel/bridge is our type,
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   537
		 * otherwise we wouldn't have got here. It is also facing us,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   538
		 * so we should skip it's body */
8584
a8b6dffead63 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8579
diff changeset
   539
		dst_tile = IsTunnel(src_tile) ? GetOtherTunnelEnd(src_tile) : GetOtherBridgeEnd(src_tile);
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   540
		override_dst_check = true;
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5849
diff changeset
   541
	} else if (type != TRANSPORT_WATER && (IsStandardRoadStopTile(src_tile) || IsTileDepotType(src_tile, type))) {
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5849
diff changeset
   542
		/* This is a road station (non drive-through) or a train or road depot. We can enter and exit
3295
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   543
		 * those from one side only. Trackdirs don't support that (yet), so we'll
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   544
		 * do this here. */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   545
3295
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   546
		DiagDirection exitdir;
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   547
		/* Find out the exit direction first */
3404
3ac4f7fedfb5 (svn r4215) -Codechange: Renamed *RoadStation* functions to *RoadStop* and moved them to station_map.h to keep consistency
celestar
parents: 3355
diff changeset
   548
		if (IsRoadStopTile(src_tile)) {
3ac4f7fedfb5 (svn r4215) -Codechange: Renamed *RoadStation* functions to *RoadStop* and moved them to station_map.h to keep consistency
celestar
parents: 3355
diff changeset
   549
			exitdir = GetRoadStopDir(src_tile);
6674
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
   550
		} else { // Train or road depot
3295
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   551
			exitdir = GetDepotDirection(src_tile, type);
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   552
		}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   553
3295
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   554
		/* Let's see if were headed the right way into the depot */
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   555
		if (src_trackdir == DiagdirToDiagTrackdir(ReverseDiagDir(exitdir))) {
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   556
			/* We are headed inwards. We cannot go through the back of the depot.
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   557
			 * For rail, we can now reverse. Reversing for road vehicles is never
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   558
			 * useful, since you cannot take paths you couldn't take before
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   559
			 * reversing (as with rail). */
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   560
			if (type == TRANSPORT_RAIL) {
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   561
				/* We can only reverse here, so we'll not consider this direction, but
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   562
				 * jump ahead to the reverse direction.  It would be nicer to return
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   563
				 * one neighbour here (the reverse trackdir of the one we are
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   564
				 * considering now) and then considering that one to return the tracks
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   565
				 * outside of the depot. But, because the code layout is cleaner this
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   566
				 * way, we will just pretend we are reversed already */
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   567
				src_trackdir = ReverseTrackdir(src_trackdir);
4561
0779e5c11331 (svn r6408) -Fix(r6406): compilation was broken
glx
parents: 4559
diff changeset
   568
				dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDiagDir(exitdir));
3295
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   569
			} else {
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   570
				dst_tile = INVALID_TILE; /* Road vehicle heading inwards: dead end */
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   571
			}
3309
dc08a15ad54e (svn r4071) - Fix: [NPF] Trains & busses were unable to find a route when leaving a depot or bus stop. Small omission from r4023 (fix by glx)
matthijs
parents: 3295
diff changeset
   572
		} else {
4561
0779e5c11331 (svn r6408) -Fix(r6406): compilation was broken
glx
parents: 4559
diff changeset
   573
			dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDiagDir(exitdir));
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   574
		}
3295
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   575
	} else {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   576
		/* This a normal tile, a bridge, a tunnel exit, etc. */
4561
0779e5c11331 (svn r6408) -Fix(r6406): compilation was broken
glx
parents: 4559
diff changeset
   577
		dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDiagDir(TrackdirToExitdir(src_trackdir)));
3295
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   578
	}
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   579
	if (dst_tile == INVALID_TILE) {
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   580
		/* We reached the border of the map */
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   581
		/* TODO Nicer control flow for this */
36c287fd4ceb (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   582
		return;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   583
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   584
1965
ef27280abb9a (svn r2471) - Fix: [ 1221249 ] [NPF] Vehicles try to drive into a tunnel entrance from above.
matthijs
parents: 1950
diff changeset
   585
	/* I can't enter a tunnel entry/exit tile from a tile above the tunnel. Note
ef27280abb9a (svn r2471) - Fix: [ 1221249 ] [NPF] Vehicles try to drive into a tunnel entrance from above.
matthijs
parents: 1950
diff changeset
   586
	 * that I can enter the tunnel from a tile below the tunnel entrance. This
ef27280abb9a (svn r2471) - Fix: [ 1221249 ] [NPF] Vehicles try to drive into a tunnel entrance from above.
matthijs
parents: 1950
diff changeset
   587
	 * solves the problem of vehicles wanting to drive off a tunnel entrance */
8584
a8b6dffead63 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8579
diff changeset
   588
	if (!override_dst_check && IsTileType(dst_tile, MP_TUNNELBRIDGE) &&
a8b6dffead63 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8579
diff changeset
   589
			GetTunnelBridgeDirection(dst_tile) != src_exitdir) {
a8b6dffead63 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8579
diff changeset
   590
		return;
2493
d834d0c1502a (svn r3019) -Codechange: Replace explicit shifting/anding/oring with GB and SB
tron
parents: 2416
diff changeset
   591
	}
1965
ef27280abb9a (svn r2471) - Fix: [ 1221249 ] [NPF] Vehicles try to drive into a tunnel entrance from above.
matthijs
parents: 1950
diff changeset
   592
2006
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1983
diff changeset
   593
	/* check correct rail type (mono, maglev, etc) */
1749
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   594
	if (type == TRANSPORT_RAIL) {
6480
767cfeae4dbf (svn r8899) -Fix
tron
parents: 6338
diff changeset
   595
		RailType dst_type = GetTileRailType(dst_tile);
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8418
diff changeset
   596
		if (!HasBit(aystar->user_data[NPF_RAILTYPES], dst_type))
1749
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   597
			return;
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   598
	}
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   599
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   600
	/* Check the owner of the tile */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   601
	if (!VehicleMayEnterTile((Owner)aystar->user_data[NPF_OWNER], dst_tile, TrackdirToExitdir(src_trackdir))) {
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   602
		return;
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   603
	}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   604
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   605
	/* Determine available tracks */
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5849
diff changeset
   606
	if (type != TRANSPORT_WATER && (IsStandardRoadStopTile(dst_tile) || IsTileDepotType(dst_tile, type))){
1655
f45015d2df03 (svn r2159) - Fix: [NPF] Road vehicles never found their target station or depots (introduced in r2154)
matthijs
parents: 1650
diff changeset
   607
		/* Road stations and road and train depots return 0 on GTTS, so we have to do this by hand... */
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   608
		DiagDirection exitdir;
3404
3ac4f7fedfb5 (svn r4215) -Codechange: Renamed *RoadStation* functions to *RoadStop* and moved them to station_map.h to keep consistency
celestar
parents: 3355
diff changeset
   609
		if (IsRoadStopTile(dst_tile)) {
3ac4f7fedfb5 (svn r4215) -Codechange: Renamed *RoadStation* functions to *RoadStop* and moved them to station_map.h to keep consistency
celestar
parents: 3355
diff changeset
   610
			exitdir = GetRoadStopDir(dst_tile);
6674
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
   611
		} else { // Road or train depot
1650
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   612
			exitdir = GetDepotDirection(dst_tile, type);
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   613
		}
1650
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   614
		/* Find the trackdirs that are available for a depot or station with this
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   615
		 * orientation. They are only "inwards", since we are reaching this tile
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   616
		 * from some other tile. This prevents vehicles driving into depots from
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   617
		 * the back */
3147
0a09ce6d651a (svn r3767) Move all direction related enums and functions to a separate header
tron
parents: 3135
diff changeset
   618
		ts = TrackdirToTrackdirBits(DiagdirToDiagTrackdir(ReverseDiagDir(exitdir)));
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   619
	} else {
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6949
diff changeset
   620
		ts = GetTileTrackStatus(dst_tile, type, subtype);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   621
	}
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   622
	trackdirbits = (TrackdirBits)(ts & TRACKDIR_BIT_MASK); /* Filter out signal status and the unused bits */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   623
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   624
	DEBUG(npf, 4, "Next node: (%d, %d) [%d], possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirbits);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   625
	/* Select only trackdirs we can reach from our current trackdir */
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   626
	trackdirbits &= TrackdirReachesTrackdirs(src_trackdir);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   627
	if (_patches.forbid_90_deg && (type == TRANSPORT_RAIL || type == TRANSPORT_WATER)) /* Filter out trackdirs that would make 90 deg turns for trains */
2916
8f1aa489701f (svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents: 2878
diff changeset
   628
		trackdirbits &= ~TrackdirCrossesTrackdirs(src_trackdir);
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   629
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   630
	DEBUG(npf, 6, "After filtering: (%d, %d), possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), trackdirbits);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   631
1950
6e4d384034d9 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   632
	i = 0;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   633
	/* Enumerate possible track */
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   634
	while (trackdirbits != 0) {
5849
58039c9dc565 (svn r8052) - Codechange: RemoveFirstTrack() and RemoveFirstTrackdir() now accept pointer to TrackBits/TrackdirBits instead of reference.
KUDr
parents: 5838
diff changeset
   635
		Trackdir dst_trackdir = RemoveFirstTrackdir(&trackdirbits);
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   636
		DEBUG(npf, 5, "Expanded into trackdir: %d, remaining trackdirs: 0x%X", dst_trackdir, trackdirbits);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   637
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   638
		/* Check for oneway signal against us */
3792
2eb8388731bf (svn r4788) - Codechange: RAILTYPE_{NORMAL,ELECTRIC,...} and RAIL_TYPE_{NORMAL,SIGNAL,...} have nearly the same name, rename RAIL_TYPE_* to RAIL_TILE_* of extra clarity
rubidium
parents: 3630
diff changeset
   639
		if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TILE_SIGNALS) {
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   640
			if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(dst_trackdir)) && !HasSignalOnTrackdir(dst_tile, dst_trackdir))
6674
64f4781b4653 (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6585
diff changeset
   641
				/* if one way signal not pointing towards us, stop going in this direction. */
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   642
				break;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   643
		}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   644
		{
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   645
			/* We've found ourselves a neighbour :-) */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   646
			AyStarNode* neighbour = &aystar->neighbours[i];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   647
			neighbour->tile = dst_tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   648
			neighbour->direction = dst_trackdir;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   649
			/* Save user data */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   650
			neighbour->user_data[NPF_NODE_FLAGS] = current->path.node.user_data[NPF_NODE_FLAGS];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   651
			NPFFillTrackdirChoice(neighbour, current);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   652
		}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   653
		i++;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   654
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   655
	aystar->num_neighbours = i;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   656
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   657
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   658
/*
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   659
 * Plan a route to the specified target (which is checked by target_proc),
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   660
 * from start1 and if not NULL, from start2 as well. The type of transport we
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   661
 * are checking is in type. reverse_penalty is applied to all routes that
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   662
 * originate from the second start node.
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   663
 * When we are looking for one specific target (optionally multiple tiles), we
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   664
 * should use a good heuristic to perform aystar search. When we search for
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   665
 * multiple targets that are spread around, we should perform a breadth first
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   666
 * search by specifiying CalcZero as our heuristic.
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   667
 */
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6949
diff changeset
   668
static NPFFoundTargetData NPFRouteInternal(AyStarNode* start1, AyStarNode* start2, NPFFindStationOrTileData* target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes, uint reverse_penalty)
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   669
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   670
	int r;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   671
	NPFFoundTargetData result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   672
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   673
	/* Initialize procs */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   674
	_npf_aystar.CalculateH = heuristic_proc;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   675
	_npf_aystar.EndNodeCheck = target_proc;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   676
	_npf_aystar.FoundEndNode = NPFSaveTargetData;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   677
	_npf_aystar.GetNeighbours = NPFFollowTrack;
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3993
diff changeset
   678
	switch (type) {
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3993
diff changeset
   679
		default: NOT_REACHED();
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3993
diff changeset
   680
		case TRANSPORT_RAIL:  _npf_aystar.CalculateG = NPFRailPathCost;  break;
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3993
diff changeset
   681
		case TRANSPORT_ROAD:  _npf_aystar.CalculateG = NPFRoadPathCost;  break;
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3993
diff changeset
   682
		case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break;
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3993
diff changeset
   683
	}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   684
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   685
	/* Initialize Start Node(s) */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   686
	start1->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   687
	start1->user_data[NPF_NODE_FLAGS] = 0;
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   688
	_npf_aystar.addstart(&_npf_aystar, start1, 0);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   689
	if (start2) {
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   690
		start2->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   691
		start2->user_data[NPF_NODE_FLAGS] = 0;
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   692
		NPFSetFlag(start2, NPF_FLAG_REVERSE, true);
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   693
		_npf_aystar.addstart(&_npf_aystar, start2, reverse_penalty);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   694
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   695
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   696
	/* Initialize result */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   697
	result.best_bird_dist = (uint)-1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   698
	result.best_path_dist = (uint)-1;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   699
	result.best_trackdir = INVALID_TRACKDIR;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   700
	_npf_aystar.user_path = &result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   701
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   702
	/* Initialize target */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   703
	_npf_aystar.user_target = target;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   704
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   705
	/* Initialize user_data */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   706
	_npf_aystar.user_data[NPF_TYPE] = type;
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6949
diff changeset
   707
	_npf_aystar.user_data[NPF_SUB_TYPE] = sub_type;
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   708
	_npf_aystar.user_data[NPF_OWNER] = owner;
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3333
diff changeset
   709
	_npf_aystar.user_data[NPF_RAILTYPES] = railtypes;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   710
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   711
	/* GO! */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   712
	r = AyStarMain_Main(&_npf_aystar);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   713
	assert(r != AYSTAR_STILL_BUSY);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   714
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   715
	if (result.best_bird_dist != 0) {
3053
708bd89b6acb (svn r3642) There is a 'npf' debug level for a reason, use that instead of bastardizing 'misc'. Also, pointers are checked against NULL in an if-statement
Darkvater
parents: 3017
diff changeset
   716
		if (target != NULL) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   717
			DEBUG(npf, 1, "Could not find route to tile 0x%X from 0x%X.", target->dest_coords, start1->tile);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   718
		} else {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   719
			/* Assumption: target == NULL, so we are looking for a depot */
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   720
			DEBUG(npf, 1, "Could not find route to a depot from tile 0x%X.", start1->tile);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   721
		}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   722
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   723
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   724
	return result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   725
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   726
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6949
diff changeset
   727
NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes)
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   728
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   729
	AyStarNode start1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   730
	AyStarNode start2;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   731
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   732
	start1.tile = tile1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   733
	start2.tile = tile2;
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   734
	/* We set this in case the target is also the start tile, we will just
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   735
	 * return a not found then */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   736
	start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   737
	start1.direction = trackdir1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   738
	start2.direction = trackdir2;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   739
	start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   740
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6949
diff changeset
   741
	return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, sub_type, owner, railtypes, 0);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   742
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   743
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6949
diff changeset
   744
NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes)
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   745
{
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6949
diff changeset
   746
	return NPFRouteToStationOrTileTwoWay(tile, trackdir, INVALID_TILE, INVALID_TRACKDIR, target, type, sub_type, owner, railtypes);
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   747
}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   748
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6949
diff changeset
   749
NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes, uint reverse_penalty)
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   750
{
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   751
	AyStarNode start1;
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   752
	AyStarNode start2;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   753
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   754
	start1.tile = tile1;
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   755
	start2.tile = tile2;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   756
	/* We set this in case the target is also the start tile, we will just
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   757
	 * return a not found then */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   758
	start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   759
	start1.direction = trackdir1;
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   760
	start2.direction = trackdir2;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   761
	start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   762
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   763
	/* perform a breadth first search. Target is NULL,
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   764
	 * since we are just looking for any depot...*/
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6949
diff changeset
   765
	return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), NULL, NPFFindDepot, NPFCalcZero, type, sub_type, owner, railtypes, reverse_penalty);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   766
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   767
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6949
diff changeset
   768
NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes)
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   769
{
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6949
diff changeset
   770
	return NPFRouteToDepotBreadthFirstTwoWay(tile, trackdir, INVALID_TILE, INVALID_TRACKDIR, type, sub_type, owner, railtypes, 0);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   771
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   772
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6949
diff changeset
   773
NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes)
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   774
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   775
	/* Okay, what we're gonna do. First, we look at all depots, calculate
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   776
	 * the manhatten distance to get to each depot. We then sort them by
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   777
	 * distance. We start by trying to plan a route to the closest, then
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   778
	 * the next closest, etc. We stop when the best route we have found so
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   779
	 * far, is shorter than the manhattan distance. This will obviously
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   780
	 * always find the closest depot. It will probably be most efficient
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   781
	 * for ships, since the heuristic will not be to far off then. I hope.
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   782
	 */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   783
	Queue depots;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   784
	int r;
4424
5b7aca737cfa (svn r6179) - Silence the old npf warnings
glx
parents: 4389
diff changeset
   785
	NPFFoundTargetData best_result = {(uint)-1, (uint)-1, INVALID_TRACKDIR, {INVALID_TILE, 0, {0, 0}}};
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   786
	NPFFoundTargetData result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   787
	NPFFindStationOrTileData target;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   788
	AyStarNode start;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   789
	Depot* current;
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
   790
	Depot *depot;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   791
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   792
	init_InsSort(&depots);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   793
	/* Okay, let's find all depots that we can use first */
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
   794
	FOR_ALL_DEPOTS(depot) {
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   795
		/* Check if this is really a valid depot, it is of the needed type and
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   796
		 * owner */
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4077
diff changeset
   797
		if (IsTileDepotType(depot->xy, type) && IsTileOwner(depot->xy, owner))
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   798
			/* If so, let's add it to the queue, sorted by distance */
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
   799
			depots.push(&depots, depot, DistanceManhattan(tile, depot->xy));
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   800
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   801
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   802
	/* Now, let's initialise the aystar */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   803
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   804
	/* Initialize procs */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   805
	_npf_aystar.CalculateH = NPFCalcStationOrTileHeuristic;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   806
	_npf_aystar.EndNodeCheck = NPFFindStationOrTile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   807
	_npf_aystar.FoundEndNode = NPFSaveTargetData;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   808
	_npf_aystar.GetNeighbours = NPFFollowTrack;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4048
diff changeset
   809
	switch (type) {
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4048
diff changeset
   810
		default: NOT_REACHED();
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4048
diff changeset
   811
		case TRANSPORT_RAIL:  _npf_aystar.CalculateG = NPFRailPathCost;  break;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4048
diff changeset
   812
		case TRANSPORT_ROAD:  _npf_aystar.CalculateG = NPFRoadPathCost;  break;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4048
diff changeset
   813
		case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4048
diff changeset
   814
	}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   815
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   816
	/* Initialize target */
3135
3a20ced2e34b (svn r3750) Use INVALID_STATION instead of -1 in NPF
tron
parents: 3053
diff changeset
   817
	target.station_index = INVALID_STATION; /* We will initialize dest_coords inside the loop below */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   818
	_npf_aystar.user_target = &target;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   819
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   820
	/* Initialize user_data */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   821
	_npf_aystar.user_data[NPF_TYPE] = type;
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6949
diff changeset
   822
	_npf_aystar.user_data[NPF_SUB_TYPE] = sub_type;
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   823
	_npf_aystar.user_data[NPF_OWNER] = owner;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   824
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   825
	/* Initialize Start Node */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   826
	start.tile = tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   827
	start.direction = trackdir; /* We will initialize user_data inside the loop below */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   828
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   829
	/* Initialize Result */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   830
	_npf_aystar.user_path = &result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   831
	best_result.best_path_dist = (uint)-1;
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   832
	best_result.best_bird_dist = (uint)-1;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   833
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   834
	/* Just iterate the depots in order of increasing distance */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   835
	while ((current = (Depot*)depots.pop(&depots))) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   836
		/* Check to see if we already have a path shorter than this
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   837
		 * depot's manhattan distance. HACK: We call DistanceManhattan
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   838
		 * again, we should probably modify the queue to give us that
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   839
		 * value... */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   840
		if ( DistanceManhattan(tile, current->xy * NPF_TILE_LENGTH) > best_result.best_path_dist)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   841
			break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   842
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   843
		/* Initialize Start Node */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   844
		/* We set this in case the target is also the start tile, we will just
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   845
		 * return a not found then */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   846
		start.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   847
		start.user_data[NPF_NODE_FLAGS] = 0;
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   848
		_npf_aystar.addstart(&_npf_aystar, &start, 0);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   849
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   850
		/* Initialize result */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   851
		result.best_bird_dist = (uint)-1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   852
		result.best_path_dist = (uint)-1;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   853
		result.best_trackdir = INVALID_TRACKDIR;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   854
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   855
		/* Initialize target */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   856
		target.dest_coords = current->xy;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   857
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   858
		/* GO! */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   859
		r = AyStarMain_Main(&_npf_aystar);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   860
		assert(r != AYSTAR_STILL_BUSY);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   861
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   862
		/* This depot is closer */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   863
		if (result.best_path_dist < best_result.best_path_dist)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   864
			best_result = result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   865
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   866
	if (result.best_bird_dist != 0) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   867
		DEBUG(npf, 1, "Could not find route to any depot from tile 0x%X.", tile);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   868
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   869
	return best_result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   870
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   871
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6480
diff changeset
   872
void InitializeNPF()
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   873
{
7586
0ee2ad46cd14 (svn r10357) -Fix: and when we're at it... yet another memory leak, but only when you start a new game; can't find any other memory leaks we can fix.
rubidium
parents: 7266
diff changeset
   874
	static bool first_init = true;
0ee2ad46cd14 (svn r10357) -Fix: and when we're at it... yet another memory leak, but only when you start a new game; can't find any other memory leaks we can fix.
rubidium
parents: 7266
diff changeset
   875
	if (first_init) {
0ee2ad46cd14 (svn r10357) -Fix: and when we're at it... yet another memory leak, but only when you start a new game; can't find any other memory leaks we can fix.
rubidium
parents: 7266
diff changeset
   876
		first_init = false;
0ee2ad46cd14 (svn r10357) -Fix: and when we're at it... yet another memory leak, but only when you start a new game; can't find any other memory leaks we can fix.
rubidium
parents: 7266
diff changeset
   877
		init_AyStar(&_npf_aystar, NPFHash, NPF_HASH_SIZE);
0ee2ad46cd14 (svn r10357) -Fix: and when we're at it... yet another memory leak, but only when you start a new game; can't find any other memory leaks we can fix.
rubidium
parents: 7266
diff changeset
   878
	} else {
0ee2ad46cd14 (svn r10357) -Fix: and when we're at it... yet another memory leak, but only when you start a new game; can't find any other memory leaks we can fix.
rubidium
parents: 7266
diff changeset
   879
		AyStarMain_Clear(&_npf_aystar);
0ee2ad46cd14 (svn r10357) -Fix: and when we're at it... yet another memory leak, but only when you start a new game; can't find any other memory leaks we can fix.
rubidium
parents: 7266
diff changeset
   880
	}
1463
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   881
	_npf_aystar.loops_per_tick = 0;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   882
	_npf_aystar.max_path_cost = 0;
1700
b8ecf0494fdd (svn r2204) - Add: [NPF] NPF now has a maximum number of nodes it will search. The default value is 5000 for now, which is an educated guess. Probably needs some finetuning. Hopefully this "feature" can be removed later on, when more sophisticated means of limiting the pathfinder have been implemented. This should make ships and larger networks playable for now, though.
matthijs
parents: 1678
diff changeset
   883
	//_npf_aystar.max_search_nodes = 0;
b8ecf0494fdd (svn r2204) - Add: [NPF] NPF now has a maximum number of nodes it will search. The default value is 5000 for now, which is an educated guess. Probably needs some finetuning. Hopefully this "feature" can be removed later on, when more sophisticated means of limiting the pathfinder have been implemented. This should make ships and larger networks playable for now, though.
matthijs
parents: 1678
diff changeset
   884
	/* We will limit the number of nodes for now, until we have a better
b8ecf0494fdd (svn r2204) - Add: [NPF] NPF now has a maximum number of nodes it will search. The default value is 5000 for now, which is an educated guess. Probably needs some finetuning. Hopefully this "feature" can be removed later on, when more sophisticated means of limiting the pathfinder have been implemented. This should make ships and larger networks playable for now, though.
matthijs
parents: 1678
diff changeset
   885
	 * solution to really fix performance */
b8ecf0494fdd (svn r2204) - Add: [NPF] NPF now has a maximum number of nodes it will search. The default value is 5000 for now, which is an educated guess. Probably needs some finetuning. Hopefully this "feature" can be removed later on, when more sophisticated means of limiting the pathfinder have been implemented. This should make ships and larger networks playable for now, though.
matthijs
parents: 1678
diff changeset
   886
	_npf_aystar.max_search_nodes = _patches.npf_max_search_nodes;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   887
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   888
1983
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   889
void NPFFillWithOrderData(NPFFindStationOrTileData* fstd, Vehicle* v)
0e87e9e56f0a (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   890
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   891
	/* Ships don't really reach their stations, but the tile in front. So don't
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   892
	 * save the station id for ships. For roadvehs we don't store it either,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   893
	 * because multistop depends on vehicles actually reaching the exact
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   894
	 * dest_tile, not just any stop of that station.
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   895
	 * So only for train orders to stations we fill fstd->station_index, for all
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   896
	 * others only dest_coords */
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
   897
	if (v->current_order.type == OT_GOTO_STATION && v->type == VEH_TRAIN) {
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4434
diff changeset
   898
		fstd->station_index = v->current_order.dest;
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   899
		/* Let's take the closest tile of the station as our target for trains */
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4434
diff changeset
   900
		fstd->dest_coords = CalcClosestStationTile(v->current_order.dest, v->tile);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   901
	} else {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   902
		fstd->dest_coords = v->dest_tile;
3135
3a20ced2e34b (svn r3750) Use INVALID_STATION instead of -1 in NPF
tron
parents: 3053
diff changeset
   903
		fstd->station_index = INVALID_STATION;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   904
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   905
}