src/npf.cpp
author rubidium
Thu, 18 Dec 2008 12:23:08 +0000
changeset 10436 8d3a9fbe8f19
parent 10236 50afe9dd466e
permissions -rw-r--r--
(svn r14689) -Change: make configure die on commonly made user mistakes, like not having SDL development files or zlib headers installed; you can still compile a dedicated server or a binary without zlib, but you have to explicitly force it.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2182
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2182
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 8962
diff changeset
     3
/** @file npf.cpp Implementation of the NPF pathfinder. */
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6259
diff changeset
     4
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1857
diff changeset
     6
#include "openttd.h"
8139
4e91c448c409 (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8131
diff changeset
     7
#include "npf.h"
3234
a2791a480b71 (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
     8
#include "bridge_map.h"
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1248
diff changeset
     9
#include "debug.h"
8119
52b48108425a (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents: 8113
diff changeset
    10
#include "tile_cmd.h"
52b48108425a (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents: 8113
diff changeset
    11
#include "bridge.h"
6453
226bcddeba32 (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: 6348
diff changeset
    12
#include "landscape.h"
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    13
#include "aystar.h"
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    14
#include "pathfind.h"
8785
871586967963 (svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents: 8616
diff changeset
    15
#include "station_base.h"
3315
c9d989783c55 (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"
8962
1b263c69799d (svn r12754) -Codechange: split depot.h into depot_map.h, depot_func.h and depot_base.h and remove quite a lot of unneeded (before this) includes of depot.h.
rubidium
parents: 8961
diff changeset
    17
#include "depot_base.h"
1b263c69799d (svn r12754) -Codechange: split depot.h into depot_map.h, depot_func.h and depot_base.h and remove quite a lot of unneeded (before this) includes of depot.h.
rubidium
parents: 8961
diff changeset
    18
#include "depot_map.h"
3154
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents: 3147
diff changeset
    19
#include "tunnel_map.h"
5469
7edfc643abbc (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: 5385
diff changeset
    20
#include "network/network.h"
3957
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
    21
#include "water_map.h"
8083
ad22eade501f (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 7970
diff changeset
    22
#include "tunnelbridge_map.h"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8119
diff changeset
    23
#include "functions.h"
8144
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8139
diff changeset
    24
#include "vehicle_base.h"
8211
29a8510dfd62 (svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium
parents: 8197
diff changeset
    25
#include "settings_type.h"
8398
1e181e2e4e15 (svn r11968) -Codechange: remove redundant FindLengthOfTunnel(), use GetTunnelBridgeLength() and/or GetOtherTunnelEnd() instead
smatz
parents: 8236
diff changeset
    26
#include "tunnelbridge.h"
9805
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
    27
#include "pbs.h"
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    28
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
    29
static AyStar _npf_aystar;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    30
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    31
/* The cost of each trackdir. A diagonal piece is the full NPF_TILE_LENGTH,
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    32
 * the shorter piece is sqrt(2)/2*NPF_TILE_LENGTH =~ 0.7071
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    33
 */
1677
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
    34
#define NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH)
1950
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
    35
static const uint _trackdir_length[TRACKDIR_END] = {
1463
85a05f2da980 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
    36
	NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH,
85a05f2da980 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
    37
	0, 0,
85a05f2da980 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
    38
	NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    39
};
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    40
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    41
/**
2403
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    42
 * Calculates the minimum distance traveled to get from t0 to t1 when only
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    43
 * using tracks (ie, only making 45 degree turns). Returns the distance in the
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    44
 * NPF scale, ie the number of full tiles multiplied by NPF_TILE_LENGTH to
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    45
 * prevent rounding.
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    46
 */
2416
ba01d6a50765 (svn r2942) Staticise some functions
tron
parents: 2403
diff changeset
    47
static uint NPFDistanceTrack(TileIndex t0, TileIndex t1)
2403
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    48
{
7970
7d6b9ab57081 (svn r11526) -Codechange: Rename the function delta fitting to the naming style
skidd13
parents: 7928
diff changeset
    49
	const uint dx = Delta(TileX(t0), TileX(t1));
7d6b9ab57081 (svn r11526) -Codechange: Rename the function delta fitting to the naming style
skidd13
parents: 7928
diff changeset
    50
	const uint dy = Delta(TileY(t0), TileY(t1));
2403
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    51
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    52
	const uint straightTracks = 2 * min(dx, dy); /* The number of straight (not full length) tracks */
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    53
	/* OPTIMISATION:
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    54
	 * Original: diagTracks = max(dx, dy) - min(dx,dy);
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    55
	 * Proof:
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    56
	 * (dx+dy) - straightTracks  == (min + max) - straightTracks = min + max - 2 * min = max - min */
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    57
	const uint diagTracks = dx + dy - straightTracks; /* The number of diagonal (full tile length) tracks. */
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    58
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    59
	/* Don't factor out NPF_TILE_LENGTH below, this will round values and lose
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    60
	 * precision */
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    61
	return diagTracks * NPF_TILE_LENGTH + straightTracks * NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH;
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    62
}
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
    63
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
    64
2075
21d2986569d1 (svn r2585) - Fix [Makefile]: some small cleanups, remove warnings, and add mersenne to makefile (Luca)
Darkvater
parents: 2049
diff changeset
    65
#if 0
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
    66
static uint NTPHash(uint key1, uint key2)
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    67
{
1661
f3799f2c84fa (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
    68
	/* This function uses the old hash, which is fixed on 10 bits (1024 buckets) */
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    69
	return PATHFIND_HASH_TILE(key1);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    70
}
2075
21d2986569d1 (svn r2585) - Fix [Makefile]: some small cleanups, remove warnings, and add mersenne to makefile (Luca)
Darkvater
parents: 2049
diff changeset
    71
#endif
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    72
1950
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
    73
/**
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
    74
 * Calculates a hash value for use in the NPF.
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4424
diff changeset
    75
 * @param key1 The TileIndex of the tile to hash
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4424
diff changeset
    76
 * @param key2 The Trackdir of the track on the tile.
1950
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
    77
 *
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4424
diff changeset
    78
 * @todo Think of a better hash.
1950
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
    79
 */
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
    80
static uint NPFHash(uint key1, uint key2)
1661
f3799f2c84fa (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
    81
{
f3799f2c84fa (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
    82
	/* TODO: think of a better hash? */
f3799f2c84fa (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
    83
	uint part1 = TileX(key1) & NPF_HASH_HALFMASK;
f3799f2c84fa (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
    84
	uint part2 = TileY(key1) & NPF_HASH_HALFMASK;
1950
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
    85
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    86
	assert(IsValidTrackdir((Trackdir)key2));
1950
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
    87
	assert(IsValidTile(key1));
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
    88
	return ((part1 << NPF_HASH_HALFBITS | part2) + (NPF_HASH_SIZE * key2 / TRACKDIR_END)) % NPF_HASH_SIZE;
1661
f3799f2c84fa (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
    89
}
f3799f2c84fa (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
    90
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
    91
static int32 NPFCalcZero(AyStar* as, AyStarNode* current, OpenListNode* parent)
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
    92
{
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    93
	return 0;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    94
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    95
1452
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
    96
/* Calcs the tile of given station that is closest to a given tile
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
    97
 * for this we assume the station is a rectangle,
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
    98
 * as defined by its top tile (st->train_tile) and its width/height (st->trainst_w, st->trainst_h)
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    99
 */
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   100
static TileIndex CalcClosestStationTile(StationID station, TileIndex tile)
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   101
{
1452
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   102
	const Station* st = GetStation(station);
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   103
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   104
	uint minx = TileX(st->train_tile);  // topmost corner of station
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   105
	uint miny = TileY(st->train_tile);
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   106
	uint maxx = minx + st->trainst_w - 1; // lowermost corner of station
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   107
	uint maxy = miny + st->trainst_h - 1;
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   108
	uint x;
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   109
	uint y;
1452
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   110
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6259
diff changeset
   111
	/* we are going the aim for the x coordinate of the closest corner
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6259
diff changeset
   112
	 * but if we are between those coordinates, we will aim for our own x coordinate */
7922
a7e266f966d9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 7764
diff changeset
   113
	x = Clamp(TileX(tile), minx, maxx);
1452
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   114
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6259
diff changeset
   115
	/* same for y coordinate, see above comment */
7922
a7e266f966d9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 7764
diff changeset
   116
	y = Clamp(TileY(tile), miny, maxy);
1452
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   117
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6259
diff changeset
   118
	/* return the tile of our target coordinates */
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   119
	return TileXY(x, y);
2182
8464b9efd19e (svn r2696) Remove stray semicolons
tron
parents: 2164
diff changeset
   120
}
1452
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   121
1677
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   122
/* Calcs the heuristic to the target station or tile. For train stations, it
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   123
 * takes into account the direction of approach.
1452
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   124
 */
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   125
static int32 NPFCalcStationOrTileHeuristic(AyStar* as, AyStarNode* current, OpenListNode* parent)
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   126
{
1452
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   127
	NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target;
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   128
	NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path;
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   129
	TileIndex from = current->tile;
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   130
	TileIndex to = fstd->dest_coords;
1453
a97bad7fc002 (svn r1957) Compilation fix.
pasky
parents: 1452
diff changeset
   131
	uint dist;
1452
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   132
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6259
diff changeset
   133
	/* for train-stations, we are going to aim for the closest station tile */
3135
a6897653e648 (svn r3750) Use INVALID_STATION instead of -1 in NPF
tron
parents: 3053
diff changeset
   134
	if (as->user_data[NPF_TYPE] == TRANSPORT_RAIL && fstd->station_index != INVALID_STATION)
1453
a97bad7fc002 (svn r1957) Compilation fix.
pasky
parents: 1452
diff changeset
   135
		to = CalcClosestStationTile(fstd->station_index, from);
1452
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   136
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   137
	if (as->user_data[NPF_TYPE] == TRANSPORT_ROAD) {
1677
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   138
		/* Since roads only have diagonal pieces, we use manhattan distance here */
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   139
		dist = DistanceManhattan(from, to) * NPF_TILE_LENGTH;
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   140
	} else {
1677
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   141
		/* Ships and trains can also go diagonal, so the minimum distance is shorter */
2403
41852918a741 (svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents: 2186
diff changeset
   142
		dist = NPFDistanceTrack(from, to);
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   143
	}
1452
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   144
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   145
	DEBUG(npf, 4, "Calculating H for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), dist);
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   146
2916
b687477adcba (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
   147
	if (dist < ftd->best_bird_dist) {
1452
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   148
		ftd->best_bird_dist = dist;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   149
		ftd->best_trackdir = (Trackdir)current->user_data[NPF_TRACKDIR_CHOICE];
1452
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   150
	}
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   151
	return dist;
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   152
}
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   153
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   154
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   155
/* Fills AyStarNode.user_data[NPF_TRACKDIRCHOICE] with the chosen direction to
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   156
 * get here, either getting it from the current choice or from the parent's
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   157
 * choice */
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   158
static void NPFFillTrackdirChoice(AyStarNode* current, OpenListNode* parent)
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   159
{
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   160
	if (parent->path.parent == NULL) {
1950
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   161
		Trackdir trackdir = (Trackdir)current->direction;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   162
		/* This is a first order decision, so we'd better save the
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   163
		 * direction we chose */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   164
		current->user_data[NPF_TRACKDIR_CHOICE] = trackdir;
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   165
		DEBUG(npf, 6, "Saving trackdir: 0x%X", trackdir);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   166
	} else {
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   167
		/* We've already made the decision, so just save our parent's decision */
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   168
		current->user_data[NPF_TRACKDIR_CHOICE] = parent->path.node.user_data[NPF_TRACKDIR_CHOICE];
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   169
	}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   170
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   171
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   172
/* Will return the cost of the tunnel. If it is an entry, it will return the
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   173
 * cost of that tile. If the tile is an exit, it will return the tunnel length
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   174
 * including the exit tile. Requires that this is a Tunnel tile */
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   175
static uint NPFTunnelCost(AyStarNode* current)
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   176
{
1950
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   177
	DiagDirection exitdir = TrackdirToExitdir((Trackdir)current->direction);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   178
	TileIndex tile = current->tile;
8083
ad22eade501f (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 7970
diff changeset
   179
	if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) {
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   180
		/* We just popped out if this tunnel, since were
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   181
		 * facing the tunnel exit */
8398
1e181e2e4e15 (svn r11968) -Codechange: remove redundant FindLengthOfTunnel(), use GetTunnelBridgeLength() and/or GetOtherTunnelEnd() instead
smatz
parents: 8236
diff changeset
   182
		return NPF_TILE_LENGTH * (GetTunnelBridgeLength(current->tile, GetOtherTunnelEnd(current->tile)) + 1);
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6259
diff changeset
   183
		/* @todo: Penalty for tunnels? */
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   184
	} else {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   185
		/* We are entering the tunnel, the enter tile is just a
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   186
		 * straight track */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   187
		return NPF_TILE_LENGTH;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   188
	}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   189
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   190
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
   191
static inline uint NPFBridgeCost(AyStarNode *current)
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
   192
{
8398
1e181e2e4e15 (svn r11968) -Codechange: remove redundant FindLengthOfTunnel(), use GetTunnelBridgeLength() and/or GetOtherTunnelEnd() instead
smatz
parents: 8236
diff changeset
   193
	return NPF_TILE_LENGTH * GetTunnelBridgeLength(current->tile, GetOtherBridgeEnd(current->tile));
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
   194
}
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
   195
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   196
static uint NPFSlopeCost(AyStarNode* current)
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   197
{
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   198
	TileIndex next = current->tile + TileOffsByDiagDir(TrackdirToExitdir((Trackdir)current->direction));
1503
39d0f85977cf (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   199
7764
e594296e90f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7370
diff changeset
   200
	/* Get center of tiles */
e594296e90f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7370
diff changeset
   201
	int x1 = TileX(current->tile) * TILE_SIZE + TILE_SIZE / 2;
e594296e90f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7370
diff changeset
   202
	int y1 = TileY(current->tile) * TILE_SIZE + TILE_SIZE / 2;
e594296e90f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7370
diff changeset
   203
	int x2 = TileX(next) * TILE_SIZE + TILE_SIZE / 2;
e594296e90f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7370
diff changeset
   204
	int y2 = TileY(next) * TILE_SIZE + TILE_SIZE / 2;
1503
39d0f85977cf (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   205
7764
e594296e90f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7370
diff changeset
   206
	int dx4 = (x2 - x1) / 4;
e594296e90f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7370
diff changeset
   207
	int dy4 = (y2 - y1) / 4;
e594296e90f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7370
diff changeset
   208
e594296e90f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7370
diff changeset
   209
	/* Get the height on both sides of the tile edge.
e594296e90f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7370
diff changeset
   210
	 * Avoid testing the height on the tile-center. This will fail for halftile-foundations.
e594296e90f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7370
diff changeset
   211
	 */
e594296e90f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7370
diff changeset
   212
	int z1 = GetSlopeZ(x1 + dx4, y1 + dy4);
e594296e90f6 (svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents: 7370
diff changeset
   213
	int z2 = GetSlopeZ(x2 - dx4, y2 - dy4);
1503
39d0f85977cf (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   214
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   215
	if (z2 - z1 > 1) {
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   216
		/* Slope up */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
   217
		return _settings_game.pf.npf.npf_rail_slope_penalty;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   218
	}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   219
	return 0;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   220
	/* Should we give a bonus for slope down? Probably not, we
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   221
	 * could just substract that bonus from the penalty, because
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   222
	 * there is only one level of steepness... */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   223
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   224
9805
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   225
static uint NPFReservedTrackCost(AyStarNode *current)
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   226
{
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   227
	TileIndex tile = current->tile;
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   228
	TrackBits track = TrackToTrackBits(TrackdirToTrack((Trackdir)current->direction));
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   229
	TrackBits res = GetReservedTrackbits(tile);
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   230
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   231
	if (NPFGetFlag(current, NPF_FLAG_3RD_SIGNAL) || ((res & track) == TRACK_BIT_NONE && !TracksOverlap(res | track))) return 0;
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   232
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   233
	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   234
		DiagDirection exitdir = TrackdirToExitdir((Trackdir)current->direction);
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   235
		if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) {
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   236
			return  _settings_game.pf.npf.npf_rail_pbs_cross_penalty * (GetTunnelBridgeLength(tile, GetOtherTunnelBridgeEnd(tile)) + 1);
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   237
		}
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   238
	}
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   239
	return  _settings_game.pf.npf.npf_rail_pbs_cross_penalty;
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   240
}
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   241
3532
2eb08a39d78d (svn r4389) -Fix: [NPF] Don't mark tiles when debugging in multiplayer, this will cause desyncs.
matthijs
parents: 3531
diff changeset
   242
/**
2eb08a39d78d (svn r4389) -Fix: [NPF] Don't mark tiles when debugging in multiplayer, this will cause desyncs.
matthijs
parents: 3531
diff changeset
   243
 * Mark tiles by mowing the grass when npf debug level >= 1.
2eb08a39d78d (svn r4389) -Fix: [NPF] Don't mark tiles when debugging in multiplayer, this will cause desyncs.
matthijs
parents: 3531
diff changeset
   244
 * Will not work for multiplayer games, since it can (will) cause desyncs.
2eb08a39d78d (svn r4389) -Fix: [NPF] Don't mark tiles when debugging in multiplayer, this will cause desyncs.
matthijs
parents: 3531
diff changeset
   245
 */
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   246
static void NPFMarkTile(TileIndex tile)
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   247
{
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4048
diff changeset
   248
#ifndef NO_DEBUG_MESSAGES
3532
2eb08a39d78d (svn r4389) -Fix: [NPF] Don't mark tiles when debugging in multiplayer, this will cause desyncs.
matthijs
parents: 3531
diff changeset
   249
	if (_debug_npf_level < 1 || _networking) return;
3017
a75caf4efa2d (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
   250
	switch (GetTileType(tile)) {
a75caf4efa2d (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
   251
		case MP_RAILWAY:
a75caf4efa2d (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
   252
			/* DEBUG: mark visited tiles by mowing the grass under them ;-) */
8961
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
   253
			if (!IsRailDepot(tile)) {
3531
38916043bbac (svn r4387) -Codechange: Use map accessors even for debugging purposes (npf).
celestar
parents: 3520
diff changeset
   254
				SetRailGroundType(tile, RAIL_GROUND_BARREN);
3017
a75caf4efa2d (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
   255
				MarkTileDirtyByTile(tile);
a75caf4efa2d (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
   256
			}
a75caf4efa2d (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
   257
			break;
a75caf4efa2d (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
   258
7370
41adc721b1fa (svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents: 7090
diff changeset
   259
		case MP_ROAD:
8961
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
   260
			if (!IsRoadDepot(tile)) {
4048
5d653b47d349 (svn r5317) s/RGT_/ROADSIDE_/ and some minor changes
tron
parents: 4000
diff changeset
   261
				SetRoadside(tile, ROADSIDE_BARREN);
3017
a75caf4efa2d (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
   262
				MarkTileDirtyByTile(tile);
a75caf4efa2d (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
   263
			}
a75caf4efa2d (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
   264
			break;
a75caf4efa2d (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
   265
a75caf4efa2d (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
   266
		default:
a75caf4efa2d (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
   267
			break;
a75caf4efa2d (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
   268
	}
1678
187385f01cc9 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   269
#endif
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   270
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   271
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   272
static int32 NPFWaterPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent)
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   273
{
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6259
diff changeset
   274
	/* TileIndex tile = current->tile; */
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   275
	int32 cost = 0;
1950
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   276
	Trackdir trackdir = (Trackdir)current->direction;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   277
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6259
diff changeset
   278
	cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   279
1950
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   280
	if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(trackdir))
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
   281
		cost += _settings_game.pf.npf.npf_buoy_penalty; // A small penalty for going over buoys
1751
009a240d035a (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   282
1950
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   283
	if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction))
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
   284
		cost += _settings_game.pf.npf.npf_water_curve_penalty;
1751
009a240d035a (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   285
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6259
diff changeset
   286
	/* @todo More penalties? */
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   287
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   288
	return cost;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   289
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   290
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   291
/* Determine the cost of this node, for road tracks */
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   292
static int32 NPFRoadPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent)
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   293
{
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   294
	TileIndex tile = current->tile;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   295
	int32 cost = 0;
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   296
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   297
	/* Determine base length */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   298
	switch (GetTileType(tile)) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   299
		case MP_TUNNELBRIDGE:
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
   300
			cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
2605
0e7993eabf16 (svn r3142) Remove a now invalid case fall-through and use helper function instead of map access.
peter1138
parents: 2493
diff changeset
   301
			break;
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   302
7370
41adc721b1fa (svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents: 7090
diff changeset
   303
		case MP_ROAD:
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   304
			cost = NPF_TILE_LENGTH;
2006
9d5d7fd428c2 (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1983
diff changeset
   305
			/* Increase the cost for level crossings */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
   306
			if (IsLevelCrossing(tile)) cost += _settings_game.pf.npf.npf_crossing_penalty;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   307
			break;
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   308
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5598
diff changeset
   309
		case MP_STATION:
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5598
diff changeset
   310
			cost = NPF_TILE_LENGTH;
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5598
diff changeset
   311
			/* Increase the cost for drive-through road stops */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
   312
			if (IsDriveThroughStopTile(tile)) cost += _settings_game.pf.npf.npf_road_drive_through_penalty;
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5598
diff changeset
   313
			break;
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5598
diff changeset
   314
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   315
		default:
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   316
			break;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   317
	}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   318
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   319
	/* Determine extra costs */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   320
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   321
	/* Check for slope */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   322
	cost += NPFSlopeCost(current);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   323
1941
ca268f8837df (svn r2447) * Add: [NPF] Penalty for road vehicles making turns.
matthijs
parents: 1927
diff changeset
   324
	/* Check for turns. Road vehicles only really drive diagonal, turns are
ca268f8837df (svn r2447) * Add: [NPF] Penalty for road vehicles making turns.
matthijs
parents: 1927
diff changeset
   325
	 * represented by non-diagonal tracks */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   326
	if (!IsDiagonalTrackdir((Trackdir)current->direction))
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
   327
		cost += _settings_game.pf.npf.npf_road_curve_penalty;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   328
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   329
	NPFMarkTile(tile);
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   330
	DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   331
	return cost;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   332
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   333
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   334
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   335
/* Determine the cost of this node, for railway tracks */
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   336
static int32 NPFRailPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent)
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   337
{
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   338
	TileIndex tile = current->tile;
1950
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   339
	Trackdir trackdir = (Trackdir)current->direction;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   340
	int32 cost = 0;
3017
a75caf4efa2d (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
   341
	/* HACK: We create a OpenListNode manually, so we can call EndNodeCheck */
1617
c3d3caad6d1e (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   342
	OpenListNode new_node;
c3d3caad6d1e (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   343
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   344
	/* Determine base length */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   345
	switch (GetTileType(tile)) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   346
		case MP_TUNNELBRIDGE:
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
   347
			cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
   348
			break;
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   349
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   350
		case MP_RAILWAY:
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   351
			cost = _trackdir_length[trackdir]; /* Should be different for diagonal tracks */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   352
			break;
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   353
7370
41adc721b1fa (svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents: 7090
diff changeset
   354
		case MP_ROAD: /* Railway crossing */
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   355
			cost = NPF_TILE_LENGTH;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   356
			break;
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   357
1503
39d0f85977cf (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   358
		case MP_STATION:
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   359
			/* We give a station tile a penalty. Logically we would only want to give
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   360
			 * station tiles that are not our destination this penalty. This would
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   361
			 * discourage trains to drive through busy stations. But, we can just
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   362
			 * give any station tile a penalty, because every possible route will get
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   363
			 * this penalty exactly once, on its end tile (if it's a station) and it
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   364
			 * will therefore not make a difference. */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
   365
			cost = NPF_TILE_LENGTH + _settings_game.pf.npf.npf_rail_station_penalty;
1503
39d0f85977cf (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   366
			break;
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   367
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   368
		default:
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   369
			break;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   370
	}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   371
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   372
	/* Determine extra costs */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   373
1459
19333d7f99b3 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   374
	/* Check for signals */
9805
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   375
	if (IsTileType(tile, MP_RAILWAY)) {
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   376
		if (HasSignalOnTrackdir(tile, trackdir)) {
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   377
			/* Ordinary track with signals */
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   378
			if (GetSignalStateByTrackdir(tile, trackdir) == SIGNAL_STATE_RED) {
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   379
				/* Signal facing us is red */
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   380
				if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   381
					/* Penalize the first signal we
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   382
					 * encounter, if it is red */
1643
420cad9e62e4 (svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents: 1617
diff changeset
   383
9805
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   384
					/* Is this a presignal exit or combo? */
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   385
					SignalType sigtype = GetSignalType(tile, TrackdirToTrack(trackdir));
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   386
					if (!IsPbsSignal(sigtype)) {
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   387
						if (sigtype == SIGTYPE_EXIT || sigtype == SIGTYPE_COMBO) {
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   388
							/* Penalise exit and combo signals differently (heavier) */
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   389
							cost += _settings_game.pf.npf.npf_rail_firstred_exit_penalty;
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   390
						} else {
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   391
							cost += _settings_game.pf.npf.npf_rail_firstred_penalty;
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   392
						}
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   393
					}
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   394
				}
9805
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   395
				/* Record the state of this signal */
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   396
				NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, true);
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   397
			} else {
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   398
				/* Record the state of this signal */
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   399
				NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, false);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   400
			}
9805
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   401
			if (NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   402
				if (NPFGetFlag(current, NPF_FLAG_2ND_SIGNAL)) {
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   403
					NPFSetFlag(current, NPF_FLAG_3RD_SIGNAL, true);
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   404
				} else {
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   405
					NPFSetFlag(current, NPF_FLAG_2ND_SIGNAL, true);
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   406
				}
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   407
			} else {
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   408
				NPFSetFlag(current, NPF_FLAG_SEEN_SIGNAL, true);
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   409
			}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   410
		}
9805
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   411
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   412
		if (HasPbsSignalOnTrackdir(tile, ReverseTrackdir(trackdir)) && !NPFGetFlag(current, NPF_FLAG_3RD_SIGNAL)) {
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   413
			cost += _settings_game.pf.npf.npf_rail_pbs_signal_back_penalty;
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   414
		}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   415
	}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   416
1459
19333d7f99b3 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   417
	/* Penalise the tile if it is a target tile and the last signal was
19333d7f99b3 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   418
	 * red */
1950
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   419
	/* HACK: We create a new_node here so we can call EndNodeCheck. Ugly as hell
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   420
	 * of course... */
1617
c3d3caad6d1e (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   421
	new_node.path.node = *current;
1950
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   422
	if (as->EndNodeCheck(as, &new_node) == AYSTAR_FOUND_END_NODE && NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_RED))
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
   423
		cost += _settings_game.pf.npf.npf_rail_lastred_penalty;
1459
19333d7f99b3 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   424
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   425
	/* Check for slope */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   426
	cost += NPFSlopeCost(current);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   427
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   428
	/* Check for turns */
1950
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   429
	if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction))
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
   430
		cost += _settings_game.pf.npf.npf_rail_curve_penalty;
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6259
diff changeset
   431
	/*TODO, with realistic acceleration, also the amount of straight track between
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6259
diff changeset
   432
	 *      curves should be taken into account, as this affects the speed limit. */
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   433
2916
b687477adcba (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
   434
	/* Check for reverse in depot */
8961
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
   435
	if (IsRailDepotTile(tile) && as->EndNodeCheck(as, &new_node) != AYSTAR_FOUND_END_NODE) {
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   436
		/* Penalise any depot tile that is not the last tile in the path. This
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   437
		 * _should_ penalise every occurence of reversing in a depot (and only
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   438
		 * that) */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
   439
		cost += _settings_game.pf.npf.npf_rail_depot_reverse_penalty;
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   440
	}
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   441
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   442
	/* Check for occupied track */
9805
c6e69a451215 (svn r13947) -Codechange [YAPP]: Added YAPP-related penalties to NPF. (michi_cc)
rubidium
parents: 9804
diff changeset
   443
	cost += NPFReservedTrackCost(current);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   444
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   445
	NPFMarkTile(tile);
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   446
	DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   447
	return cost;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   448
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   449
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   450
/* Will find any depot */
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   451
static int32 NPFFindDepot(AyStar* as, OpenListNode *current)
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   452
{
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   453
	/* It's not worth caching the result with NPF_FLAG_IS_TARGET here as below,
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   454
	 * since checking the cache not that much faster than the actual check */
8954
3993bae3bfb8 (svn r12745) -Codechange: a bit of naming conventions, introduce Is*DepotTile()
smatz
parents: 8840
diff changeset
   455
	return IsDepotTypeTile(current->path.node.tile, (TransportType)as->user_data[NPF_TYPE]) ?
4000
4009d092b306 (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
   456
		AYSTAR_FOUND_END_NODE : AYSTAR_DONE;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   457
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   458
9806
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   459
/** Find any safe and free tile. */
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   460
static int32 NPFFindSafeTile(AyStar *as, OpenListNode *current)
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   461
{
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   462
	const Vehicle *v = ((NPFFindStationOrTileData*)as->user_target)->v;
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   463
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   464
	return
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   465
		IsSafeWaitingPosition(v, current->path.node.tile, (Trackdir)current->path.node.direction, true, _settings_game.pf.forbid_90_deg) &&
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   466
		IsWaitingPositionFree(v, current->path.node.tile, (Trackdir)current->path.node.direction, _settings_game.pf.forbid_90_deg) ?
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   467
			AYSTAR_FOUND_END_NODE : AYSTAR_DONE;
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   468
}
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   469
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   470
/* Will find a station identified using the NPFFindStationOrTileData */
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   471
static int32 NPFFindStationOrTile(AyStar* as, OpenListNode *current)
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   472
{
1464
fe5fcc14b2a2 (svn r1968) - Fix: [NPF] Mixed declarations and statements
matthijs
parents: 1463
diff changeset
   473
	NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target;
1617
c3d3caad6d1e (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   474
	AyStarNode *node = &current->path.node;
1464
fe5fcc14b2a2 (svn r1968) - Fix: [NPF] Mixed declarations and statements
matthijs
parents: 1463
diff changeset
   475
	TileIndex tile = node->tile;
1459
19333d7f99b3 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   476
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   477
	/* If GetNeighbours said we could get here, we assume the station type
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   478
	 * is correct */
1459
19333d7f99b3 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   479
	if (
3135
a6897653e648 (svn r3750) Use INVALID_STATION instead of -1 in NPF
tron
parents: 3053
diff changeset
   480
		(fstd->station_index == INVALID_STATION && tile == fstd->dest_coords) || /* We've found the tile, or */
3315
c9d989783c55 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3309
diff changeset
   481
		(IsTileType(tile, MP_STATION) && GetStationIndex(tile) == fstd->station_index) /* the station */
1459
19333d7f99b3 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   482
	) {
19333d7f99b3 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   483
		return AYSTAR_FOUND_END_NODE;
19333d7f99b3 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   484
	} else {
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   485
		return AYSTAR_DONE;
1459
19333d7f99b3 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   486
	}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   487
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   488
9804
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   489
/**
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   490
 * Find the node containing the first signal on the path.
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   491
 *
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   492
 * If the first signal is on the very first two tiles of the path,
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   493
 * the second signal is returnd. If no suitable signal is present, the
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   494
 * last node of the path is returned.
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   495
 */
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   496
static const PathNode* FindSafePosition(PathNode *path, const Vehicle *v)
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   497
{
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   498
	/* If there is no signal, reserve the whole path. */
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   499
	PathNode *sig = path;
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   500
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   501
	for(; path->parent != NULL; path = path->parent) {
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   502
		if (IsSafeWaitingPosition(v, path->node.tile, (Trackdir)path->node.direction, true, _settings_game.pf.forbid_90_deg)) {
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   503
			sig = path;
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   504
		}
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   505
	}
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   506
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   507
	return sig;
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   508
}
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   509
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   510
/**
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   511
 * Lift the reservation of the tiles from @p start till @p end, excluding @p end itself.
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   512
 */
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   513
static void ClearPathReservation(const PathNode *start, const PathNode *end)
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   514
{
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   515
	bool first_run = true;
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   516
	for (; start != end; start = start->parent) {
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   517
		if (IsRailwayStationTile(start->node.tile) && first_run) {
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   518
			SetRailwayStationPlatformReservation(start->node.tile, TrackdirToExitdir((Trackdir)start->node.direction), false);
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   519
		} else {
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   520
			UnreserveRailTrack(start->node.tile, TrackdirToTrack((Trackdir)start->node.direction));
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   521
		}
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   522
		first_run = false;
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   523
	}
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   524
}
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   525
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   526
/**
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   527
 * To be called when @p current contains the (shortest route to) the target node.
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   528
 * Will fill the contents of the NPFFoundTargetData using
9804
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   529
 * AyStarNode[NPF_TRACKDIR_CHOICE]. If requested, path reservation
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   530
 * is done here.
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   531
 */
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   532
static void NPFSaveTargetData(AyStar* as, OpenListNode* current)
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   533
{
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   534
	NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path;
1950
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   535
	ftd->best_trackdir = (Trackdir)current->path.node.user_data[NPF_TRACKDIR_CHOICE];
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   536
	ftd->best_path_dist = current->g;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   537
	ftd->best_bird_dist = 0;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   538
	ftd->node = current->path.node;
9804
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   539
	ftd->res_okay = false;
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   540
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   541
	if (as->user_target != NULL && ((NPFFindStationOrTileData*)as->user_target)->reserve_path && as->user_data[NPF_TYPE] == TRANSPORT_RAIL) {
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   542
		/* Path reservation is requested. */
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   543
		const Vehicle  *v = ((NPFFindStationOrTileData*)as->user_target)->v;
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   544
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   545
		const PathNode *target = FindSafePosition(&current->path, v);
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   546
		ftd->node = target->node;
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   547
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   548
		/* If the target is a station skip to platform end. */
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   549
		if (IsRailwayStationTile(target->node.tile)) {
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   550
			DiagDirection dir = TrackdirToExitdir((Trackdir)target->node.direction);
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   551
			uint len = GetStationByTile(target->node.tile)->GetPlatformLength(target->node.tile, dir);
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   552
			TileIndex end_tile = TILE_ADD(target->node.tile, (len - 1) * TileOffsByDiagDir(dir));
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   553
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   554
			/* Update only end tile, trackdir of a station stays the same. */
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   555
			ftd->node.tile = end_tile;
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   556
			if (!IsWaitingPositionFree(v, end_tile, (Trackdir)target->node.direction, _settings_game.pf.forbid_90_deg)) return;
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   557
			SetRailwayStationPlatformReservation(target->node.tile, dir, true);
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   558
			SetRailwayStationReservation(target->node.tile, false);
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   559
		} else {
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   560
			if (!IsWaitingPositionFree(v, target->node.tile, (Trackdir)target->node.direction, _settings_game.pf.forbid_90_deg)) return;
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   561
		}
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   562
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   563
		for (const PathNode *cur = target; cur->parent != NULL; cur = cur->parent) {
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   564
			if (!TryReserveRailTrack(cur->node.tile, TrackdirToTrack((Trackdir)cur->node.direction))) {
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   565
				/* Reservation failed, undo. */
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   566
				ClearPathReservation(target, cur);
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   567
				return;
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   568
			}
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   569
		}
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   570
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   571
		ftd->res_okay = true;
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   572
	}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   573
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   574
1967
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   575
/**
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 9806
diff changeset
   576
 * Finds out if a given company's vehicles are allowed to enter a given tile.
1967
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   577
 * @param owner    The owner of the vehicle.
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   578
 * @param tile     The tile that is about to be entered.
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   579
 * @param enterdir The direction in which the vehicle wants to enter the tile.
1967
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   580
 * @return         true if the vehicle can enter the tile.
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   581
 * @todo           This function should be used in other places than just NPF,
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   582
 *                 maybe moved to another file too.
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   583
 */
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   584
static bool CanEnterTileOwnerCheck(Owner owner, TileIndex tile, DiagDirection enterdir)
1967
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   585
{
8961
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
   586
	if (IsTileType(tile, MP_RAILWAY) ||             /* Rail tile (also rail depot) */
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
   587
			IsRailwayStationTile(tile) ||   /* Rail station tile */
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
   588
			IsRoadDepotTile(tile) ||        /* Road depot tile */
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   589
			IsStandardRoadStopTile(tile)) { /* Road station tile (but not drive-through stops) */
1967
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   590
		return IsTileOwner(tile, owner); /* You need to own these tiles entirely to use them */
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   591
	}
1967
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   592
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   593
	switch (GetTileType(tile)) {
7370
41adc721b1fa (svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents: 7090
diff changeset
   594
		case MP_ROAD:
1967
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   595
			/* rail-road crossing : are we looking at the railway part? */
3498
ece6348becad (svn r4349) Remove GetCrossingTransportType(), it's slightly overkill
tron
parents: 3442
diff changeset
   596
			if (IsLevelCrossing(tile) &&
ece6348becad (svn r4349) Remove GetCrossingTransportType(), it's slightly overkill
tron
parents: 3442
diff changeset
   597
					DiagDirToAxis(enterdir) != GetCrossingRoadAxis(tile)) {
1967
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   598
				return IsTileOwner(tile, owner); /* Railway needs owner check, while the street is public */
3498
ece6348becad (svn r4349) Remove GetCrossingTransportType(), it's slightly overkill
tron
parents: 3442
diff changeset
   599
			}
1967
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   600
			break;
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   601
1967
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   602
		case MP_TUNNELBRIDGE:
8088
92fca5b09665 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8083
diff changeset
   603
			if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) {
1967
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   604
				return IsTileOwner(tile, owner);
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   605
			}
1967
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   606
			break;
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2916
diff changeset
   607
1967
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   608
		default:
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   609
			break;
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   610
	}
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   611
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6259
diff changeset
   612
	return true; // no need to check
1967
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   613
}
a01f4d5dd957 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1965
diff changeset
   614
3957
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   615
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   616
/**
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   617
 * Returns the direction the exit of the depot on the given tile is facing.
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   618
 */
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   619
static DiagDirection GetDepotDirection(TileIndex tile, TransportType type)
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   620
{
8954
3993bae3bfb8 (svn r12745) -Codechange: a bit of naming conventions, introduce Is*DepotTile()
smatz
parents: 8840
diff changeset
   621
	assert(IsDepotTypeTile(tile, type));
3957
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   622
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   623
	switch (type) {
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   624
		case TRANSPORT_RAIL:  return GetRailDepotDirection(tile);
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   625
		case TRANSPORT_ROAD:  return GetRoadDepotDirection(tile);
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   626
		case TRANSPORT_WATER: return GetShipDepotDirection(tile);
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   627
		default: return INVALID_DIAGDIR; /* Not reached */
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   628
	}
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   629
}
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   630
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   631
/** Tests if a tile is a road tile with a single tramtrack (tram can reverse) */
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   632
static DiagDirection GetSingleTramBit(TileIndex tile)
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   633
{
8563
13b1a41e46f1 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 8510
diff changeset
   634
	if (IsNormalRoadTile(tile)) {
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   635
		RoadBits rb = GetRoadBits(tile, ROADTYPE_TRAM);
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   636
		switch (rb) {
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   637
			case ROAD_NW: return DIAGDIR_NW;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   638
			case ROAD_SW: return DIAGDIR_SW;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   639
			case ROAD_SE: return DIAGDIR_SE;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   640
			case ROAD_NE: return DIAGDIR_NE;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   641
			default: break;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   642
		}
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   643
	}
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   644
	return INVALID_DIAGDIR;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   645
}
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   646
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   647
/**
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   648
 * Tests if a tile can be entered or left only from one side.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   649
 *
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   650
 * Depots, non-drive-through roadstops, and tiles with single trambits are tested.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   651
 *
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   652
 * @param tile The tile of interest.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   653
 * @param type The transporttype of the vehicle.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   654
 * @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   655
 * @return The single entry/exit-direction of the tile, or INVALID_DIAGDIR if there are more or less directions
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   656
 */
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   657
static DiagDirection GetTileSingleEntry(TileIndex tile, TransportType type, uint subtype)
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   658
{
8954
3993bae3bfb8 (svn r12745) -Codechange: a bit of naming conventions, introduce Is*DepotTile()
smatz
parents: 8840
diff changeset
   659
	if (type != TRANSPORT_WATER && IsDepotTypeTile(tile, type)) return GetDepotDirection(tile, type);
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   660
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   661
	if (type == TRANSPORT_ROAD) {
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   662
		if (IsStandardRoadStopTile(tile)) return GetRoadStopDir(tile);
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   663
		if (HasBit(subtype, ROADTYPE_TRAM)) return GetSingleTramBit(tile);
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   664
	}
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   665
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   666
	return INVALID_DIAGDIR;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   667
}
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   668
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   669
/**
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   670
 * Tests if a vehicle must reverse on a tile.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   671
 *
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   672
 * @param tile The tile of interest.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   673
 * @param dir The direction in which the vehicle drives on a tile.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   674
 * @param type The transporttype of the vehicle.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   675
 * @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   676
 * @return true iff the vehicle must reverse on the tile.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   677
 */
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   678
static inline bool ForceReverse(TileIndex tile, DiagDirection dir, TransportType type, uint subtype)
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   679
{
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   680
	DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype);
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   681
	return single_entry != INVALID_DIAGDIR && single_entry != dir;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   682
}
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   683
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   684
/**
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   685
 * Tests if a vehicle can enter a tile.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   686
 *
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   687
 * @param tile The tile of interest.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   688
 * @param dir The direction in which the vehicle drives onto a tile.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   689
 * @param type The transporttype of the vehicle.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   690
 * @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   691
 * @param railtypes For TRANSPORT_RAIL the compatible RailTypes of the vehicle.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   692
 * @param owner The owner of the vehicle.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   693
 * @return true iff the vehicle can enter the tile.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   694
 */
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   695
static bool CanEnterTile(TileIndex tile, DiagDirection dir, TransportType type, uint subtype, RailTypes railtypes, Owner owner)
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   696
{
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   697
	/* Check tunnel entries and bridge ramps */
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   698
	if (IsTileType(tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(tile) != dir) return false;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   699
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   700
	/* Test ownership */
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   701
	if (!CanEnterTileOwnerCheck(owner, tile, dir)) return false;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   702
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   703
	/* check correct rail type (mono, maglev, etc) */
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   704
	if (type == TRANSPORT_RAIL) {
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   705
		RailType rail_type = GetTileRailType(tile);
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   706
		if (!HasBit(railtypes, rail_type)) return false;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   707
	}
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   708
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   709
	/* Depots, standard roadstops and single tram bits can only be entered from one direction */
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   710
	DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype);
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   711
	if (single_entry != INVALID_DIAGDIR && single_entry != ReverseDiagDir(dir)) return false;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   712
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   713
	return true;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   714
}
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   715
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   716
/**
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   717
 * Returns the driveable Trackdirs on a tile.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   718
 *
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   719
 * One-way-roads are taken into account. Signals are not tested.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   720
 *
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   721
 * @param dst_tile The tile of interest.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   722
 * @param src_trackdir The direction the vehicle is currently moving.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   723
 * @param type The transporttype of the vehicle.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   724
 * @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   725
 * @return The Trackdirs the vehicle can continue moving on.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   726
 */
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   727
static TrackdirBits GetDriveableTrackdirBits(TileIndex dst_tile, Trackdir src_trackdir, TransportType type, uint subtype)
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   728
{
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8563
diff changeset
   729
	TrackdirBits trackdirbits = TrackStatusToTrackdirBits(GetTileTrackStatus(dst_tile, type, subtype));
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   730
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   731
	if (trackdirbits == 0 && type == TRANSPORT_ROAD && HasBit(subtype, ROADTYPE_TRAM)) {
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   732
		/* GetTileTrackStatus() returns 0 for single tram bits.
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   733
		 * As we cannot change it there (easily) without breaking something, change it here */
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   734
		switch (GetSingleTramBit(dst_tile)) {
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   735
			case DIAGDIR_NE:
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   736
			case DIAGDIR_SW:
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   737
				trackdirbits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   738
				break;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   739
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   740
			case DIAGDIR_NW:
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   741
			case DIAGDIR_SE:
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   742
				trackdirbits = TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_SE;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   743
				break;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   744
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   745
			default: break;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   746
		}
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   747
	}
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   748
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   749
	DEBUG(npf, 4, "Next node: (%d, %d) [%d], possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirbits);
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   750
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   751
	/* Select only trackdirs we can reach from our current trackdir */
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   752
	trackdirbits &= TrackdirReachesTrackdirs(src_trackdir);
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   753
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   754
	/* Filter out trackdirs that would make 90 deg turns for trains */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
   755
	if (_settings_game.pf.forbid_90_deg && (type == TRANSPORT_RAIL || type == TRANSPORT_WATER)) trackdirbits &= ~TrackdirCrossesTrackdirs(src_trackdir);
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   756
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   757
	DEBUG(npf, 6, "After filtering: (%d, %d), possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), trackdirbits);
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   758
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   759
	return trackdirbits;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   760
}
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   761
3957
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3933
diff changeset
   762
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   763
/* Will just follow the results of GetTileTrackStatus concerning where we can
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   764
 * go and where not. Uses AyStar.user_data[NPF_TYPE] as the transport type and
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   765
 * an argument to GetTileTrackStatus. Will skip tunnels, meaning that the
1459
19333d7f99b3 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   766
 * entry and exit are neighbours. Will fill
19333d7f99b3 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   767
 * AyStarNode.user_data[NPF_TRACKDIR_CHOICE] with an appropriate value, and
19333d7f99b3 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   768
 * copy AyStarNode.user_data[NPF_NODE_FLAGS] from the parent */
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   769
static void NPFFollowTrack(AyStar* aystar, OpenListNode* current)
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   770
{
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   771
	/* We leave src_tile on track src_trackdir in direction src_exitdir */
1950
aeb6067adc30 (svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents: 1945
diff changeset
   772
	Trackdir src_trackdir = (Trackdir)current->path.node.direction;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   773
	TileIndex src_tile = current->path.node.tile;
1944
dd9cba5fab2a (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   774
	DiagDirection src_exitdir = TrackdirToExitdir(src_trackdir);
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   775
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   776
	/* Is src_tile valid, and can be used?
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   777
	 * When choosing track on a junction src_tile is the tile neighboured to the junction wrt. exitdir.
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   778
	 * But we must not check the validity of this move, as src_tile is totally unrelated to the move, if a roadvehicle reversed on a junction. */
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   779
	bool ignore_src_tile = (current->path.parent == NULL && NPFGetFlag(&current->path.node, NPF_FLAG_IGNORE_START_TILE));
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   780
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   781
	/* Information about the vehicle: TransportType (road/rail/water) and SubType (compatible rail/road types) */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   782
	TransportType type = (TransportType)aystar->user_data[NPF_TYPE];
6683
b88ae30866ce (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6453
diff changeset
   783
	uint subtype = aystar->user_data[NPF_SUB_TYPE];
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   784
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   785
	/* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   786
	aystar->num_neighbours = 0;
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   787
	DEBUG(npf, 4, "Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   788
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   789
	/* We want to determine the tile we arrive, and which choices we have there */
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   790
	TileIndex dst_tile;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   791
	TrackdirBits trackdirbits;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   792
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   793
	/* Find dest tile */
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   794
	if (ignore_src_tile) {
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   795
		/* Do not perform any checks that involve src_tile */
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   796
		dst_tile = src_tile + TileOffsByDiagDir(src_exitdir);
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   797
		trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   798
	} else if (IsTileType(src_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(src_tile) == src_exitdir) {
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   799
		/* We drive through the wormhole and arrive on the other side */
8197
3af783256580 (svn r11760) -Codechange: unify the way how other end of a tunnel/bridge is determined at some places
smatz
parents: 8144
diff changeset
   800
		dst_tile = GetOtherTunnelBridgeEnd(src_tile);
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   801
		trackdirbits = TrackdirToTrackdirBits(src_trackdir);
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   802
	} else if (ForceReverse(src_tile, src_exitdir, type, subtype)) {
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   803
		/* We can only reverse on this tile */
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   804
		dst_tile = src_tile;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   805
		src_trackdir = ReverseTrackdir(src_trackdir);
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   806
		trackdirbits = TrackdirToTrackdirBits(src_trackdir);
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   807
	} else {
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   808
		/* We leave src_tile in src_exitdir and reach dst_tile */
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   809
		dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDiagDir(src_exitdir));
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   810
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   811
		if (dst_tile != INVALID_TILE && !CanEnterTile(dst_tile, src_exitdir, type, subtype, (RailTypes)aystar->user_data[NPF_RAILTYPES], (Owner)aystar->user_data[NPF_OWNER])) dst_tile = INVALID_TILE;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   812
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   813
		if (dst_tile == INVALID_TILE) {
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   814
			/* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   815
			if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   816
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   817
			dst_tile = src_tile;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   818
			src_trackdir = ReverseTrackdir(src_trackdir);
3295
39a2c60c23e9 (svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents: 3238
diff changeset
   819
		}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   820
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   821
		trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   822
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   823
		if (trackdirbits == 0) {
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   824
			/* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   825
			if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   826
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   827
			dst_tile = src_tile;
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   828
			src_trackdir = ReverseTrackdir(src_trackdir);
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   829
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   830
			trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   831
		}
2493
f6b4300cc2b0 (svn r3019) -Codechange: Replace explicit shifting/anding/oring with GB and SB
tron
parents: 2416
diff changeset
   832
	}
1965
71079b9c26a8 (svn r2471) - Fix: [ 1221249 ] [NPF] Vehicles try to drive into a tunnel entrance from above.
matthijs
parents: 1950
diff changeset
   833
9806
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   834
	if (NPFGetFlag(&current->path.node, NPF_FLAG_IGNORE_RESERVED)) {
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   835
		/* Mask out any reserved tracks. */
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   836
		TrackBits reserved = GetReservedTrackbits(dst_tile);
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   837
		trackdirbits &= ~TrackBitsToTrackdirBits(reserved);
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   838
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   839
		uint bits = TrackdirBitsToTrackBits(trackdirbits);
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   840
		int i;
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   841
		FOR_EACH_SET_BIT(i, bits) {
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   842
			if (TracksOverlap(reserved | TrackToTrackBits((Track)i))) trackdirbits &= ~TrackToTrackdirBits((Track)i);
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   843
		}
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   844
	}
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
   845
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   846
	/* Enumerate possible track */
8483
a15e052321fc (svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents: 8398
diff changeset
   847
	uint i = 0;
1944
dd9cba5fab2a (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   848
	while (trackdirbits != 0) {
5598
2fadbd43709d (svn r8052) - Codechange: RemoveFirstTrack() and RemoveFirstTrackdir() now accept pointer to TrackBits/TrackdirBits instead of reference.
KUDr
parents: 5587
diff changeset
   849
		Trackdir dst_trackdir = RemoveFirstTrackdir(&trackdirbits);
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   850
		DEBUG(npf, 5, "Expanded into trackdir: %d, remaining trackdirs: 0x%X", dst_trackdir, trackdirbits);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   851
9792
e6efb2eb4851 (svn r13934) -Codechange [YAPP]: Handle through signals in the pathfinders. (michi_cc)
rubidium
parents: 9413
diff changeset
   852
		/* Tile with signals? */
3792
67c865c9315c (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
   853
		if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TILE_SIGNALS) {
9792
e6efb2eb4851 (svn r13934) -Codechange [YAPP]: Handle through signals in the pathfinders. (michi_cc)
rubidium
parents: 9413
diff changeset
   854
			if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(dst_trackdir)) && !HasSignalOnTrackdir(dst_tile, dst_trackdir) && IsOnewaySignal(dst_tile, TrackdirToTrack(dst_trackdir)))
e6efb2eb4851 (svn r13934) -Codechange [YAPP]: Handle through signals in the pathfinders. (michi_cc)
rubidium
parents: 9413
diff changeset
   855
				/* If there's a one-way signal not pointing towards us, stop going in this direction. */
1944
dd9cba5fab2a (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   856
				break;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   857
		}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   858
		{
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   859
			/* We've found ourselves a neighbour :-) */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   860
			AyStarNode* neighbour = &aystar->neighbours[i];
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   861
			neighbour->tile = dst_tile;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   862
			neighbour->direction = dst_trackdir;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   863
			/* Save user data */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   864
			neighbour->user_data[NPF_NODE_FLAGS] = current->path.node.user_data[NPF_NODE_FLAGS];
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   865
			NPFFillTrackdirChoice(neighbour, current);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   866
		}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   867
		i++;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   868
	}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   869
	aystar->num_neighbours = i;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   870
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   871
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   872
/*
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   873
 * Plan a route to the specified target (which is checked by target_proc),
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   874
 * from start1 and if not NULL, from start2 as well. The type of transport we
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   875
 * are checking is in type. reverse_penalty is applied to all routes that
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   876
 * originate from the second start node.
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   877
 * When we are looking for one specific target (optionally multiple tiles), we
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   878
 * should use a good heuristic to perform aystar search. When we search for
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   879
 * multiple targets that are spread around, we should perform a breadth first
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   880
 * search by specifiying CalcZero as our heuristic.
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   881
 */
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   882
static NPFFoundTargetData NPFRouteInternal(AyStarNode* start1, bool ignore_start_tile1, AyStarNode* start2, bool ignore_start_tile2, NPFFindStationOrTileData* target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty)
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   883
{
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   884
	int r;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   885
	NPFFoundTargetData result;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   886
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   887
	/* Initialize procs */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   888
	_npf_aystar.CalculateH = heuristic_proc;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   889
	_npf_aystar.EndNodeCheck = target_proc;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   890
	_npf_aystar.FoundEndNode = NPFSaveTargetData;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   891
	_npf_aystar.GetNeighbours = NPFFollowTrack;
4000
4009d092b306 (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
   892
	switch (type) {
4009d092b306 (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
   893
		default: NOT_REACHED();
4009d092b306 (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
   894
		case TRANSPORT_RAIL:  _npf_aystar.CalculateG = NPFRailPathCost;  break;
4009d092b306 (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
   895
		case TRANSPORT_ROAD:  _npf_aystar.CalculateG = NPFRoadPathCost;  break;
4009d092b306 (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
   896
		case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break;
4009d092b306 (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
   897
	}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   898
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   899
	/* Initialize Start Node(s) */
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   900
	start1->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   901
	start1->user_data[NPF_NODE_FLAGS] = 0;
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   902
	NPFSetFlag(start1, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile1);
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   903
	_npf_aystar.addstart(&_npf_aystar, start1, 0);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   904
	if (start2) {
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   905
		start2->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1459
19333d7f99b3 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   906
		start2->user_data[NPF_NODE_FLAGS] = 0;
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   907
		NPFSetFlag(start2, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile2);
1459
19333d7f99b3 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   908
		NPFSetFlag(start2, NPF_FLAG_REVERSE, true);
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   909
		_npf_aystar.addstart(&_npf_aystar, start2, reverse_penalty);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   910
	}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   911
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   912
	/* Initialize result */
10236
50afe9dd466e (svn r14464) -Codechange: replace (uint)-1 with UINT_MAX (PhilSophus)
rubidium
parents: 10207
diff changeset
   913
	result.best_bird_dist = UINT_MAX;
50afe9dd466e (svn r14464) -Codechange: replace (uint)-1 with UINT_MAX (PhilSophus)
rubidium
parents: 10207
diff changeset
   914
	result.best_path_dist = UINT_MAX;
9804
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   915
	result.best_trackdir  = INVALID_TRACKDIR;
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   916
	result.node.tile      = INVALID_TILE;
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
   917
	result.res_okay       = false;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   918
	_npf_aystar.user_path = &result;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   919
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   920
	/* Initialize target */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   921
	_npf_aystar.user_target = target;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   922
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   923
	/* Initialize user_data */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   924
	_npf_aystar.user_data[NPF_TYPE] = type;
6683
b88ae30866ce (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6453
diff changeset
   925
	_npf_aystar.user_data[NPF_SUB_TYPE] = sub_type;
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   926
	_npf_aystar.user_data[NPF_OWNER] = owner;
3355
e414a0b104a6 (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
   927
	_npf_aystar.user_data[NPF_RAILTYPES] = railtypes;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   928
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   929
	/* GO! */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   930
	r = AyStarMain_Main(&_npf_aystar);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   931
	assert(r != AYSTAR_STILL_BUSY);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   932
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   933
	if (result.best_bird_dist != 0) {
3053
403425c55a3c (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
   934
		if (target != NULL) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   935
			DEBUG(npf, 1, "Could not find route to tile 0x%X from 0x%X.", target->dest_coords, start1->tile);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   936
		} else {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   937
			/* Assumption: target == NULL, so we are looking for a depot */
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
   938
			DEBUG(npf, 1, "Could not find route to a depot from tile 0x%X.", start1->tile);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   939
		}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   940
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   941
	}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   942
	return result;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   943
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   944
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   945
NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   946
{
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   947
	AyStarNode start1;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   948
	AyStarNode start2;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   949
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   950
	start1.tile = tile1;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   951
	start2.tile = tile2;
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   952
	/* We set this in case the target is also the start tile, we will just
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   953
	 * return a not found then */
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   954
	start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   955
	start1.direction = trackdir1;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   956
	start2.direction = trackdir2;
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   957
	start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   958
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   959
	return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, sub_type, owner, railtypes, 0);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   960
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   961
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   962
NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   963
{
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   964
	return NPFRouteToStationOrTileTwoWay(tile, trackdir, ignore_start_tile, INVALID_TILE, INVALID_TRACKDIR, false, target, type, sub_type, owner, railtypes);
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   965
}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   966
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   967
NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty)
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   968
{
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   969
	AyStarNode start1;
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   970
	AyStarNode start2;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   971
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   972
	start1.tile = tile1;
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   973
	start2.tile = tile2;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   974
	/* We set this in case the target is also the start tile, we will just
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   975
	 * return a not found then */
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   976
	start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   977
	start1.direction = trackdir1;
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   978
	start2.direction = trackdir2;
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
   979
	start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   980
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   981
	/* perform a breadth first search. Target is NULL,
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
   982
	 * since we are just looking for any depot...*/
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   983
	return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, NULL, NPFFindDepot, NPFCalcZero, type, sub_type, owner, railtypes, reverse_penalty);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   984
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   985
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   986
NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   987
{
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   988
	return NPFRouteToDepotBreadthFirstTwoWay(tile, trackdir, ignore_start_tile, INVALID_TILE, INVALID_TRACKDIR, false, type, sub_type, owner, railtypes, 0);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   989
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   990
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
   991
NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
   992
{
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   993
	/* Okay, what we're gonna do. First, we look at all depots, calculate
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   994
	 * the manhatten distance to get to each depot. We then sort them by
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   995
	 * distance. We start by trying to plan a route to the closest, then
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   996
	 * the next closest, etc. We stop when the best route we have found so
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   997
	 * far, is shorter than the manhattan distance. This will obviously
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   998
	 * always find the closest depot. It will probably be most efficient
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   999
	 * for ships, since the heuristic will not be to far off then. I hope.
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1000
	 */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1001
	Queue depots;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1002
	int r;
10236
50afe9dd466e (svn r14464) -Codechange: replace (uint)-1 with UINT_MAX (PhilSophus)
rubidium
parents: 10207
diff changeset
  1003
	NPFFoundTargetData best_result = {UINT_MAX, UINT_MAX, INVALID_TRACKDIR, {INVALID_TILE, 0, {0, 0}}, false};
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1004
	NPFFoundTargetData result;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1005
	NPFFindStationOrTileData target;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1006
	AyStarNode start;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1007
	Depot* current;
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
  1008
	Depot *depot;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1009
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1010
	init_InsSort(&depots);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1011
	/* Okay, let's find all depots that we can use first */
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
  1012
	FOR_ALL_DEPOTS(depot) {
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
  1013
		/* Check if this is really a valid depot, it is of the needed type and
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
  1014
		 * owner */
8954
3993bae3bfb8 (svn r12745) -Codechange: a bit of naming conventions, introduce Is*DepotTile()
smatz
parents: 8840
diff changeset
  1015
		if (IsDepotTypeTile(depot->xy, type) && IsTileOwner(depot->xy, owner))
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
  1016
			/* If so, let's add it to the queue, sorted by distance */
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
  1017
			depots.push(&depots, depot, DistanceManhattan(tile, depot->xy));
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1018
	}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1019
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1020
	/* Now, let's initialise the aystar */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1021
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1022
	/* Initialize procs */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1023
	_npf_aystar.CalculateH = NPFCalcStationOrTileHeuristic;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1024
	_npf_aystar.EndNodeCheck = NPFFindStationOrTile;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1025
	_npf_aystar.FoundEndNode = NPFSaveTargetData;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1026
	_npf_aystar.GetNeighbours = NPFFollowTrack;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4048
diff changeset
  1027
	switch (type) {
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4048
diff changeset
  1028
		default: NOT_REACHED();
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4048
diff changeset
  1029
		case TRANSPORT_RAIL:  _npf_aystar.CalculateG = NPFRailPathCost;  break;
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4048
diff changeset
  1030
		case TRANSPORT_ROAD:  _npf_aystar.CalculateG = NPFRoadPathCost;  break;
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4048
diff changeset
  1031
		case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break;
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4048
diff changeset
  1032
	}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1033
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1034
	/* Initialize target */
3135
a6897653e648 (svn r3750) Use INVALID_STATION instead of -1 in NPF
tron
parents: 3053
diff changeset
  1035
	target.station_index = INVALID_STATION; /* We will initialize dest_coords inside the loop below */
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1036
	_npf_aystar.user_target = &target;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1037
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1038
	/* Initialize user_data */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1039
	_npf_aystar.user_data[NPF_TYPE] = type;
6683
b88ae30866ce (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6453
diff changeset
  1040
	_npf_aystar.user_data[NPF_SUB_TYPE] = sub_type;
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
  1041
	_npf_aystar.user_data[NPF_OWNER] = owner;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1042
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1043
	/* Initialize Start Node */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1044
	start.tile = tile;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1045
	start.direction = trackdir; /* We will initialize user_data inside the loop below */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1046
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1047
	/* Initialize Result */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1048
	_npf_aystar.user_path = &result;
10236
50afe9dd466e (svn r14464) -Codechange: replace (uint)-1 with UINT_MAX (PhilSophus)
rubidium
parents: 10207
diff changeset
  1049
	best_result.best_path_dist = UINT_MAX;
50afe9dd466e (svn r14464) -Codechange: replace (uint)-1 with UINT_MAX (PhilSophus)
rubidium
parents: 10207
diff changeset
  1050
	best_result.best_bird_dist = UINT_MAX;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1051
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1052
	/* Just iterate the depots in order of increasing distance */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1053
	while ((current = (Depot*)depots.pop(&depots))) {
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1054
		/* Check to see if we already have a path shorter than this
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
  1055
		 * depot's manhattan distance. HACK: We call DistanceManhattan
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1056
		 * again, we should probably modify the queue to give us that
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1057
		 * value... */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1058
		if ( DistanceManhattan(tile, current->xy * NPF_TILE_LENGTH) > best_result.best_path_dist)
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1059
			break;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1060
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1061
		/* Initialize Start Node */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1062
		/* We set this in case the target is also the start tile, we will just
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1063
		 * return a not found then */
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
  1064
		start.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1065
		start.user_data[NPF_NODE_FLAGS] = 0;
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8483
diff changeset
  1066
		NPFSetFlag(&start, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile);
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1753
diff changeset
  1067
		_npf_aystar.addstart(&_npf_aystar, &start, 0);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1068
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1069
		/* Initialize result */
10236
50afe9dd466e (svn r14464) -Codechange: replace (uint)-1 with UINT_MAX (PhilSophus)
rubidium
parents: 10207
diff changeset
  1070
		result.best_bird_dist = UINT_MAX;
50afe9dd466e (svn r14464) -Codechange: replace (uint)-1 with UINT_MAX (PhilSophus)
rubidium
parents: 10207
diff changeset
  1071
		result.best_path_dist = UINT_MAX;
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1941
diff changeset
  1072
		result.best_trackdir = INVALID_TRACKDIR;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1073
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1074
		/* Initialize target */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1075
		target.dest_coords = current->xy;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1076
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1077
		/* GO! */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1078
		r = AyStarMain_Main(&_npf_aystar);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1079
		assert(r != AYSTAR_STILL_BUSY);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1080
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1081
		/* This depot is closer */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1082
		if (result.best_path_dist < best_result.best_path_dist)
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1083
			best_result = result;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1084
	}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1085
	if (result.best_bird_dist != 0) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4561
diff changeset
  1086
		DEBUG(npf, 1, "Could not find route to any depot from tile 0x%X.", tile);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1087
	}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1088
	return best_result;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1089
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1090
9806
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1091
NPFFoundTargetData NPFRouteToSafeTile(const Vehicle *v, TileIndex tile, Trackdir trackdir, bool override_railtype)
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1092
{
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1093
	assert(v->type == VEH_TRAIN);
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1094
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1095
	NPFFindStationOrTileData fstd;
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1096
	fstd.v = v;
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1097
	fstd.reserve_path = true;
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1098
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1099
	AyStarNode start1;
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1100
	start1.tile = tile;
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1101
	/* We set this in case the target is also the start tile, we will just
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1102
	 * return a not found then */
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1103
	start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1104
	start1.direction = trackdir;
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1105
	NPFSetFlag(&start1, NPF_FLAG_IGNORE_RESERVED, true);
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1106
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1107
	RailTypes railtypes = v->u.rail.compatible_railtypes;
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1108
	if (override_railtype) railtypes |= GetRailTypeInfo(v->u.rail.railtype)->compatible_railtypes;
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1109
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1110
	/* perform a breadth first search. Target is NULL,
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1111
	 * since we are just looking for any safe tile...*/
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1112
	return NPFRouteInternal(&start1, true, NULL, false, &fstd, NPFFindSafeTile, NPFCalcZero, TRANSPORT_RAIL, 0, v->owner, railtypes, 0);
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1113
}
5d8e73afcf1f (svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9805
diff changeset
  1114
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6154
diff changeset
  1115
void InitializeNPF()
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1116
{
7090
e20ded5c6250 (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: 6770
diff changeset
  1117
	static bool first_init = true;
e20ded5c6250 (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: 6770
diff changeset
  1118
	if (first_init) {
e20ded5c6250 (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: 6770
diff changeset
  1119
		first_init = false;
e20ded5c6250 (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: 6770
diff changeset
  1120
		init_AyStar(&_npf_aystar, NPFHash, NPF_HASH_SIZE);
e20ded5c6250 (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: 6770
diff changeset
  1121
	} else {
e20ded5c6250 (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: 6770
diff changeset
  1122
		AyStarMain_Clear(&_npf_aystar);
e20ded5c6250 (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: 6770
diff changeset
  1123
	}
1463
85a05f2da980 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
  1124
	_npf_aystar.loops_per_tick = 0;
85a05f2da980 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
  1125
	_npf_aystar.max_path_cost = 0;
1700
e1fe3446d013 (svn r2204) - Add: [NPF] NPF now has a maximum number of nodes it will search. The default value is 5000 for now, which is an educated guess. Probably needs some finetuning. Hopefully this "feature" can be removed later on, when more sophisticated means of limiting the pathfinder have been implemented. This should make ships and larger networks playable for now, though.
matthijs
parents: 1678
diff changeset
  1126
	//_npf_aystar.max_search_nodes = 0;
e1fe3446d013 (svn r2204) - Add: [NPF] NPF now has a maximum number of nodes it will search. The default value is 5000 for now, which is an educated guess. Probably needs some finetuning. Hopefully this "feature" can be removed later on, when more sophisticated means of limiting the pathfinder have been implemented. This should make ships and larger networks playable for now, though.
matthijs
parents: 1678
diff changeset
  1127
	/* We will limit the number of nodes for now, until we have a better
e1fe3446d013 (svn r2204) - Add: [NPF] NPF now has a maximum number of nodes it will search. The default value is 5000 for now, which is an educated guess. Probably needs some finetuning. Hopefully this "feature" can be removed later on, when more sophisticated means of limiting the pathfinder have been implemented. This should make ships and larger networks playable for now, though.
matthijs
parents: 1678
diff changeset
  1128
	 * solution to really fix performance */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
  1129
	_npf_aystar.max_search_nodes = _settings_game.pf.npf.npf_max_search_nodes;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1130
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1131
9804
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
  1132
void NPFFillWithOrderData(NPFFindStationOrTileData *fstd, Vehicle *v, bool reserve_path)
1983
4fcefeef2feb (svn r2489) static, bracing style and use clamp()
tron
parents: 1981
diff changeset
  1133
{
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1134
	/* Ships don't really reach their stations, but the tile in front. So don't
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1135
	 * save the station id for ships. For roadvehs we don't store it either,
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1136
	 * because multistop depends on vehicles actually reaching the exact
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1137
	 * dest_tile, not just any stop of that station.
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1138
	 * So only for train orders to stations we fill fstd->station_index, for all
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1139
	 * others only dest_coords */
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8785
diff changeset
  1140
	if (v->current_order.IsType(OT_GOTO_STATION) && v->type == VEH_TRAIN) {
8840
332412c2e9c1 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 8836
diff changeset
  1141
		fstd->station_index = v->current_order.GetDestination();
1452
a978fdcbca3e (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
  1142
		/* Let's take the closest tile of the station as our target for trains */
8840
332412c2e9c1 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 8836
diff changeset
  1143
		fstd->dest_coords = CalcClosestStationTile(fstd->station_index, v->tile);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1144
	} else {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1145
		fstd->dest_coords = v->dest_tile;
3135
a6897653e648 (svn r3750) Use INVALID_STATION instead of -1 in NPF
tron
parents: 3053
diff changeset
  1146
		fstd->station_index = INVALID_STATION;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1147
	}
9804
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
  1148
	fstd->reserve_path = reserve_path;
fbe3e33a2c96 (svn r13946) -Add [YAPP]: Implement track reserving for NPF as well. (michi_cc)
rubidium
parents: 9792
diff changeset
  1149
	fstd->v = v;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
  1150
}