src/pathfind.cpp
author rubidium
Thu, 18 Dec 2008 12:23:08 +0000
changeset 10436 8d3a9fbe8f19
parent 9792 e6efb2eb4851
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: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
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 pathfind.cpp Implementation of the oldest supported pathfinder. */
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1209
diff changeset
     6
#include "openttd.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
     7
#include "bridge_map.h"
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3894
diff changeset
     8
#include "station_map.h"
8119
52b48108425a (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents: 8108
diff changeset
     9
#include "tile_cmd.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: 6352
diff changeset
    10
#include "landscape.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
#include "pathfind.h"
8103
cf92483a0abf (svn r11664) -Codechange: use more specific ("rail_type.h" instead of "rail.h" that includes way more than only "rail_type.h") includes at some places.
rubidium
parents: 8088
diff changeset
    12
#include "rail_type.h"
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
    13
#include "debug.h"
3154
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
    14
#include "tunnel_map.h"
8270
e7c342f6b14c (svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents: 8236
diff changeset
    15
#include "settings_type.h"
8083
ad22eade501f (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 7971
diff changeset
    16
#include "tunnelbridge_map.h"
8119
52b48108425a (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents: 8108
diff changeset
    17
#include "core/random_func.hpp"
8925
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents: 8804
diff changeset
    18
#include "core/alloc_type.hpp"
8398
1e181e2e4e15 (svn r11968) -Codechange: remove redundant FindLengthOfTunnel(), use GetTunnelBridgeLength() and/or GetOtherTunnelEnd() instead
smatz
parents: 8397
diff changeset
    19
#include "tunnelbridge.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
    21
/* remember which tiles we have already visited so we don't visit them again. */
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1901
diff changeset
    22
static bool TPFSetTileBit(TrackPathFinder *tpf, TileIndex tile, int dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
	uint hash, val, offs;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
	TrackPathFinderLink *link, *new_link;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    26
	uint bits = 1 << dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    27
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    28
	if (tpf->disable_tile_hash)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    29
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    30
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    31
	hash = PATHFIND_HASH_TILE(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    32
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    33
	val = tpf->hash_head[hash];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    34
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    35
	if (val == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    36
		/* unused hash entry, set the appropriate bit in it and return true
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
		 * to indicate that a bit was set. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
		tpf->hash_head[hash] = bits;
1986
fcc849a38ae6 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
    39
		tpf->hash_tile[hash] = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    41
	} else if (!(val & 0x8000)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
		/* single tile */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    43
1986
fcc849a38ae6 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
    44
		if (tile == tpf->hash_tile[hash]) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
			/* found another bit for the same tile,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
			 * check if this bit is already set, if so, return false */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
			if (val & bits)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
				return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    49
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
			/* otherwise set the bit and return true to indicate that the bit
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
			 * was set */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
			tpf->hash_head[hash] = val | bits;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    53
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
			/* two tiles with the same hash, need to make a link */
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
    56
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    57
			/* allocate a link. if out of links, handle this by returning
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    58
			 * that a tile was already visisted. */
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
    59
			if (tpf->num_links_left == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    60
				return false;
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
    61
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
			tpf->num_links_left--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    63
			link = tpf->new_link++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    64
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    65
			/* move the data that was previously in the hash_??? variables
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    66
			 * to the link struct, and let the hash variables point to the link */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    67
			link->tile = tpf->hash_tile[hash];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
			tpf->hash_tile[hash] = PATHFIND_GET_LINK_OFFS(tpf, link);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
			link->flags = tpf->hash_head[hash];
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
    71
			tpf->hash_head[hash] = 0xFFFF; // multi link
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
			link->next = 0xFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    74
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
		/* a linked list of many tiles,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
		 * find the one corresponding to the tile, if it exists.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
		 * otherwise make a new link */
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
    79
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
		offs = tpf->hash_tile[hash];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    81
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    82
			link = PATHFIND_GET_LINK_PTR(tpf, offs);
1986
fcc849a38ae6 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
    83
			if (tile == link->tile) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    84
				/* found the tile in the link list,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    85
				 * check if the bit was alrady set, if so return false to indicate that the
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    86
				 * bit was already set */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    87
				if (link->flags & bits)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    88
					return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    89
				link->flags |= bits;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
				return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    91
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    92
		} while ((offs=link->next) != 0xFFFF);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    93
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
    94
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    95
	/* get here if we need to add a new link to link,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    96
	 * first, allocate a new link, in the same way as before */
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
    97
	if (tpf->num_links_left == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    98
			return false;
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
    99
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   100
	tpf->num_links_left--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
	new_link = tpf->new_link++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   102
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   103
	/* then fill the link with the new info, and establish a ptr from the old
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   104
	 * link to the new one */
1986
fcc849a38ae6 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   105
	new_link->tile = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
	new_link->flags = bits;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   107
	new_link->next = 0xFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
	link->next = PATHFIND_GET_LINK_OFFS(tpf, new_link);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   112
8800
c30102fee110 (svn r12540) -Codechange: Enumify some values in original pathfinder and remove an unused variable.
frosch
parents: 8798
diff changeset
   113
static void TPFModeShip(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   114
{
9490
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   115
	assert(tpf->tracktype == TRANSPORT_WATER);
747
4bf34cf669d0 (svn r1203) -Fix: the pathfinder no longer sees rail with an other owner as a
truelight
parents: 679
diff changeset
   116
9490
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   117
	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   118
		/* wrong track type */
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   119
		if (GetTunnelBridgeTransportType(tile) != tpf->tracktype) return;
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   120
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   121
		DiagDirection dir = GetTunnelBridgeDirection(tile);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   122
		/* entering tunnel / bridge? */
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   123
		if (dir == direction) {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   124
			TileIndex endtile = GetOtherTunnelBridgeEnd(tile);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   125
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   126
			tpf->rd.cur_length += GetTunnelBridgeLength(tile, endtile) + 1;
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   127
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   128
			TPFSetTileBit(tpf, tile, 14);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   129
			TPFSetTileBit(tpf, endtile, 14);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   130
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   131
			tile = endtile;
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   132
		} else {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   133
			/* leaving tunnel / bridge? */
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   134
			if (ReverseDiagDir(dir) != direction) return;
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   135
		}
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   136
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   137
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   138
	/* This addition will sometimes overflow by a single tile.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   139
	 * The use of TILE_MASK here makes sure that we still point at a valid
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   140
	 * tile, and then this tile will be in the sentinel row/col, so GetTileTrackStatus will fail. */
4559
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4406
diff changeset
   141
	tile = TILE_MASK(tile + TileOffsByDiagDir(direction));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   142
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   143
	if (++tpf->rd.cur_length > 50)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   144
		return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   145
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   146
	TrackBits bits = TrackStatusToTrackBits(GetTileTrackStatus(tile, tpf->tracktype, tpf->sub_type)) & DiagdirReachesTracks(direction);
8611
9037a4227d67 (svn r12193) -Codechange: Rename a magic variable, give it a decent type, and remove a 'goto'.
frosch
parents: 8480
diff changeset
   147
	if (bits == TRACK_BIT_NONE) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   148
926
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 913
diff changeset
   149
	assert(TileX(tile) != MapMaxX() && TileY(tile) != MapMaxY());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   150
8611
9037a4227d67 (svn r12193) -Codechange: Rename a magic variable, give it a decent type, and remove a 'goto'.
frosch
parents: 8480
diff changeset
   151
	bool only_one_track = true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   152
	do {
8611
9037a4227d67 (svn r12193) -Codechange: Rename a magic variable, give it a decent type, and remove a 'goto'.
frosch
parents: 8480
diff changeset
   153
		Track track = RemoveFirstTrack(&bits);
9037a4227d67 (svn r12193) -Codechange: Rename a magic variable, give it a decent type, and remove a 'goto'.
frosch
parents: 8480
diff changeset
   154
		if (bits != TRACK_BIT_NONE) only_one_track = false;
9490
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   155
		RememberData rd = tpf->rd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   156
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   157
		/* Change direction 4 times only */
8611
9037a4227d67 (svn r12193) -Codechange: Rename a magic variable, give it a decent type, and remove a 'goto'.
frosch
parents: 8480
diff changeset
   158
		if (!only_one_track && track != tpf->rd.last_choosen_track) {
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2782
diff changeset
   159
			if (++tpf->rd.depth > 4) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   160
				tpf->rd = rd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
				return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
			}
8611
9037a4227d67 (svn r12193) -Codechange: Rename a magic variable, give it a decent type, and remove a 'goto'.
frosch
parents: 8480
diff changeset
   163
			tpf->rd.last_choosen_track = track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   166
		tpf->the_dir = TrackEnterdirToTrackdir(track, direction);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   167
8611
9037a4227d67 (svn r12193) -Codechange: Rename a magic variable, give it a decent type, and remove a 'goto'.
frosch
parents: 8480
diff changeset
   168
		if (!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length)) {
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   169
			TPFModeShip(tpf, tile, TrackdirToExitdir(tpf->the_dir));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   171
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   172
		tpf->rd = rd;
8611
9037a4227d67 (svn r12193) -Codechange: Rename a magic variable, give it a decent type, and remove a 'goto'.
frosch
parents: 8480
diff changeset
   173
	} while (bits != TRACK_BIT_NONE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   174
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   175
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   176
8396
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   177
/**
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   178
 * Checks if any vehicle can enter/leave tile in given diagdir
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   179
 * Checks only for rail/road depots and road non-drivethrough stations
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   180
 * @param tile tile to check
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   181
 * @param side side of tile we are trying to leave/enter
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   182
 * @param tracktype type of transport
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   183
 * @pre tile has trackbit at that diagdir
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   184
 * @return true iff vehicle can enter/leve the tile in given side
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   185
 */
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   186
static inline bool CanAccessTileInDir(TileIndex tile, DiagDirection side, TransportType tracktype)
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   187
{
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   188
	if (tracktype == TRANSPORT_RAIL) {
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   189
		/* depot from wrong side */
8961
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
   190
		if (IsRailDepotTile(tile) && GetRailDepotDirection(tile) != side) return false;
8396
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   191
	} else if (tracktype == TRANSPORT_ROAD) {
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   192
		/* depot from wrong side */
8961
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
   193
		if (IsRoadDepotTile(tile) && GetRoadDepotDirection(tile) != side) return false;
8396
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   194
		/* non-driverthrough road station from wrong side */
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   195
		if (IsStandardRoadStopTile(tile) && GetRoadStopDir(tile) != side) return false;
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   196
	}
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   197
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   198
	return true;
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   199
}
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   200
8800
c30102fee110 (svn r12540) -Codechange: Enumify some values in original pathfinder and remove an unused variable.
frosch
parents: 8798
diff changeset
   201
static void TPFModeNormal(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
8392
e80cb3cd512d (svn r11962) -Cleanup: OPF is no longer used to update signals
smatz
parents: 8390
diff changeset
   202
{
8395
c7b4c6b0a4c2 (svn r11965) -Codechange: simplified tunnel/bridge code in TPFMode1
smatz
parents: 8392
diff changeset
   203
	const TileIndex tile_org = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
8392
e80cb3cd512d (svn r11962) -Cleanup: OPF is no longer used to update signals
smatz
parents: 8390
diff changeset
   205
	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
8395
c7b4c6b0a4c2 (svn r11965) -Codechange: simplified tunnel/bridge code in TPFMode1
smatz
parents: 8392
diff changeset
   206
		/* wrong track type */
c7b4c6b0a4c2 (svn r11965) -Codechange: simplified tunnel/bridge code in TPFMode1
smatz
parents: 8392
diff changeset
   207
		if (GetTunnelBridgeTransportType(tile) != tpf->tracktype) return;
c7b4c6b0a4c2 (svn r11965) -Codechange: simplified tunnel/bridge code in TPFMode1
smatz
parents: 8392
diff changeset
   208
c7b4c6b0a4c2 (svn r11965) -Codechange: simplified tunnel/bridge code in TPFMode1
smatz
parents: 8392
diff changeset
   209
		DiagDirection dir = GetTunnelBridgeDirection(tile);
c7b4c6b0a4c2 (svn r11965) -Codechange: simplified tunnel/bridge code in TPFMode1
smatz
parents: 8392
diff changeset
   210
		/* entering tunnel / bridge? */
c7b4c6b0a4c2 (svn r11965) -Codechange: simplified tunnel/bridge code in TPFMode1
smatz
parents: 8392
diff changeset
   211
		if (dir == direction) {
c7b4c6b0a4c2 (svn r11965) -Codechange: simplified tunnel/bridge code in TPFMode1
smatz
parents: 8392
diff changeset
   212
			TileIndex endtile = GetOtherTunnelBridgeEnd(tile);
c7b4c6b0a4c2 (svn r11965) -Codechange: simplified tunnel/bridge code in TPFMode1
smatz
parents: 8392
diff changeset
   213
8398
1e181e2e4e15 (svn r11968) -Codechange: remove redundant FindLengthOfTunnel(), use GetTunnelBridgeLength() and/or GetOtherTunnelEnd() instead
smatz
parents: 8397
diff changeset
   214
			tpf->rd.cur_length += GetTunnelBridgeLength(tile, endtile) + 1;
8395
c7b4c6b0a4c2 (svn r11965) -Codechange: simplified tunnel/bridge code in TPFMode1
smatz
parents: 8392
diff changeset
   215
8392
e80cb3cd512d (svn r11962) -Cleanup: OPF is no longer used to update signals
smatz
parents: 8390
diff changeset
   216
			TPFSetTileBit(tpf, tile, 14);
8395
c7b4c6b0a4c2 (svn r11965) -Codechange: simplified tunnel/bridge code in TPFMode1
smatz
parents: 8392
diff changeset
   217
			TPFSetTileBit(tpf, endtile, 14);
c7b4c6b0a4c2 (svn r11965) -Codechange: simplified tunnel/bridge code in TPFMode1
smatz
parents: 8392
diff changeset
   218
c7b4c6b0a4c2 (svn r11965) -Codechange: simplified tunnel/bridge code in TPFMode1
smatz
parents: 8392
diff changeset
   219
			tile = endtile;
c7b4c6b0a4c2 (svn r11965) -Codechange: simplified tunnel/bridge code in TPFMode1
smatz
parents: 8392
diff changeset
   220
		} else {
c7b4c6b0a4c2 (svn r11965) -Codechange: simplified tunnel/bridge code in TPFMode1
smatz
parents: 8392
diff changeset
   221
			/* leaving tunnel / bridge? */
c7b4c6b0a4c2 (svn r11965) -Codechange: simplified tunnel/bridge code in TPFMode1
smatz
parents: 8392
diff changeset
   222
			if (ReverseDiagDir(dir) != direction) return;
8392
e80cb3cd512d (svn r11962) -Cleanup: OPF is no longer used to update signals
smatz
parents: 8390
diff changeset
   223
		}
8396
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   224
	} else {
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   225
		/* can we leave tile in this dir? */
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   226
		if (!CanAccessTileInDir(tile, direction, tpf->tracktype)) return;
8392
e80cb3cd512d (svn r11962) -Cleanup: OPF is no longer used to update signals
smatz
parents: 8390
diff changeset
   227
	}
8395
c7b4c6b0a4c2 (svn r11965) -Codechange: simplified tunnel/bridge code in TPFMode1
smatz
parents: 8392
diff changeset
   228
8392
e80cb3cd512d (svn r11962) -Cleanup: OPF is no longer used to update signals
smatz
parents: 8390
diff changeset
   229
	tile += TileOffsByDiagDir(direction);
e80cb3cd512d (svn r11962) -Cleanup: OPF is no longer used to update signals
smatz
parents: 8390
diff changeset
   230
8396
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   231
	/* can we enter tile in this dir? */
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   232
	if (!CanAccessTileInDir(tile, ReverseDiagDir(direction), tpf->tracktype)) return;
ecadf587e067 (svn r11966) -Fix: OPF was searching through depots and normal road stops
smatz
parents: 8395
diff changeset
   233
5457
a0591c7d9db8 (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5456
diff changeset
   234
	/* Check if the new tile is a tunnel or bridge head and that the direction
a0591c7d9db8 (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5456
diff changeset
   235
	 * and transport type match */
a0591c7d9db8 (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5456
diff changeset
   236
	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
8088
92fca5b09665 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8083
diff changeset
   237
		if (GetTunnelBridgeDirection(tile) != direction ||
92fca5b09665 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8083
diff changeset
   238
				GetTunnelBridgeTransportType(tile) != tpf->tracktype) {
92fca5b09665 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8083
diff changeset
   239
			return;
5457
a0591c7d9db8 (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5456
diff changeset
   240
		}
a0591c7d9db8 (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5456
diff changeset
   241
	}
a0591c7d9db8 (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5456
diff changeset
   242
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   243
	TrackdirBits trackdirbits = TrackStatusToTrackdirBits(GetTileTrackStatus(tile, tpf->tracktype, tpf->sub_type));
8397
db3e5c72c257 (svn r11967) -Fix (r1400): MP_ROAD can have railbits too - OPF searching over rail of diffent owner behind crossing
smatz
parents: 8396
diff changeset
   244
db3e5c72c257 (svn r11967) -Fix (r1400): MP_ROAD can have railbits too - OPF searching over rail of diffent owner behind crossing
smatz
parents: 8396
diff changeset
   245
	/* Check in case of rail if the owner is the same */
db3e5c72c257 (svn r11967) -Fix (r1400): MP_ROAD can have railbits too - OPF searching over rail of diffent owner behind crossing
smatz
parents: 8396
diff changeset
   246
	if (tpf->tracktype == TRANSPORT_RAIL) {
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   247
		if (trackdirbits != TRACKDIR_BIT_NONE && TrackStatusToTrackdirBits(GetTileTrackStatus(tile_org, TRANSPORT_RAIL, 0)) != TRACKDIR_BIT_NONE) {
8397
db3e5c72c257 (svn r11967) -Fix (r1400): MP_ROAD can have railbits too - OPF searching over rail of diffent owner behind crossing
smatz
parents: 8396
diff changeset
   248
			if (GetTileOwner(tile_org) != GetTileOwner(tile)) return;
db3e5c72c257 (svn r11967) -Fix (r1400): MP_ROAD can have railbits too - OPF searching over rail of diffent owner behind crossing
smatz
parents: 8396
diff changeset
   249
		}
db3e5c72c257 (svn r11967) -Fix (r1400): MP_ROAD can have railbits too - OPF searching over rail of diffent owner behind crossing
smatz
parents: 8396
diff changeset
   250
	}
db3e5c72c257 (svn r11967) -Fix (r1400): MP_ROAD can have railbits too - OPF searching over rail of diffent owner behind crossing
smatz
parents: 8396
diff changeset
   251
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   252
	tpf->rd.cur_length++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   253
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   254
	trackdirbits &= DiagdirReachesTrackdirs(direction);
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   255
	TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   257
	if (bits != TRACK_BIT_NONE) {
7833
ba402b153b79 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 7081
diff changeset
   258
		if (!tpf->disable_tile_hash || (tpf->rd.cur_length <= 64 && (KillFirstBit(bits) == 0 || ++tpf->rd.depth <= 7))) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   259
			do {
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   260
				Track track = RemoveFirstTrack(&bits);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   261
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   262
				tpf->the_dir = TrackEnterdirToTrackdir(track, direction);
7071
625657fed875 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 6683
diff changeset
   263
				RememberData rd = tpf->rd;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   264
8480
1713959ed036 (svn r12055) -Fix: another way to fix AI trying to build road through depots
smatz
parents: 8398
diff changeset
   265
				/* make sure we are not leaving from invalid side */
1713959ed036 (svn r12055) -Fix: another way to fix AI trying to build road through depots
smatz
parents: 8398
diff changeset
   266
				if (TPFSetTileBit(tpf, tile, tpf->the_dir) && CanAccessTileInDir(tile, TrackdirToExitdir(tpf->the_dir), tpf->tracktype) &&
8611
9037a4227d67 (svn r12193) -Codechange: Rename a magic variable, give it a decent type, and remove a 'goto'.
frosch
parents: 8480
diff changeset
   267
						!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length) ) {
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   268
					TPFModeNormal(tpf, tile, TrackdirToExitdir(tpf->the_dir));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   269
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   270
				tpf->rd = rd;
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   271
			} while (bits != TRACK_BIT_NONE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   272
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   273
	}
7071
625657fed875 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 6683
diff changeset
   274
}
625657fed875 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 6683
diff changeset
   275
8800
c30102fee110 (svn r12540) -Codechange: Enumify some values in original pathfinder and remove an unused variable.
frosch
parents: 8798
diff changeset
   276
void FollowTrack(TileIndex tile, PathfindFlags flags, TransportType tt, uint sub_type, DiagDirection direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   277
{
8798
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   278
	assert(IsValidDiagDirection(direction));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   279
8798
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   280
	SmallStackSafeStackAlloc<TrackPathFinder, 1> tpf;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   281
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   282
	/* initialize path finder variables */
8798
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   283
	tpf->userdata = data;
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   284
	tpf->enum_proc = enum_proc;
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   285
	tpf->new_link = tpf->links;
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   286
	tpf->num_links_left = lengthof(tpf->links);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   287
8798
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   288
	tpf->rd.cur_length = 0;
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   289
	tpf->rd.depth = 0;
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   290
	tpf->rd.last_choosen_track = INVALID_TRACK;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   291
8800
c30102fee110 (svn r12540) -Codechange: Enumify some values in original pathfinder and remove an unused variable.
frosch
parents: 8798
diff changeset
   292
	tpf->disable_tile_hash = (flags & PATHFIND_FLAGS_DISABLE_TILE_HASH) != 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   293
8800
c30102fee110 (svn r12540) -Codechange: Enumify some values in original pathfinder and remove an unused variable.
frosch
parents: 8798
diff changeset
   294
	tpf->tracktype = tt;
8798
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   295
	tpf->sub_type = sub_type;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   296
8800
c30102fee110 (svn r12540) -Codechange: Enumify some values in original pathfinder and remove an unused variable.
frosch
parents: 8798
diff changeset
   297
	if ((flags & PATHFIND_FLAGS_SHIP_MODE) != 0) {
8798
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   298
		tpf->enum_proc(tile, data, INVALID_TRACKDIR, 0);
8800
c30102fee110 (svn r12540) -Codechange: Enumify some values in original pathfinder and remove an unused variable.
frosch
parents: 8798
diff changeset
   299
		TPFModeShip(tpf, tile, direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   300
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   301
		/* clear the hash_heads */
8798
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   302
		memset(tpf->hash_head, 0, sizeof(tpf->hash_head));
8800
c30102fee110 (svn r12540) -Codechange: Enumify some values in original pathfinder and remove an unused variable.
frosch
parents: 8798
diff changeset
   303
		TPFModeNormal(tpf, tile, direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   304
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   305
8798
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   306
	if (after_proc != NULL) after_proc(tpf);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   307
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   308
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6172
diff changeset
   309
struct StackedItem {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   310
	TileIndex tile;
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   311
	uint16 cur_length; ///< This is the current length to this tile.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   312
	uint16 priority;   ///< This is the current length + estimated length to the goal.
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   313
	TrackdirByte track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   314
	byte depth;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   315
	byte state;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   316
	byte first_track;
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6172
diff changeset
   317
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   318
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6172
diff changeset
   319
struct HashLink {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   320
	TileIndex tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   321
	uint16 typelength;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   322
	uint16 next;
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6172
diff changeset
   323
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   324
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6172
diff changeset
   325
struct NewTrackPathFinder {
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   326
	NTPEnumProc *enum_proc;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   327
	void *userdata;
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   328
	TileIndex dest;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   329
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: 3267
diff changeset
   330
	TransportType tracktype;
8236
8a5dd0b42e47 (svn r11800) -Codechange: move some functions to a more logical location + some type safety.
rubidium
parents: 8139
diff changeset
   331
	RailTypes railtypes;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   332
	uint maxlength;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   333
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   334
	HashLink *new_link;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   335
	uint num_links_left;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   336
979
11ea18598e16 (svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   337
	uint nstack;
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   338
	StackedItem stack[256];     ///< priority queue of stacked items
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   339
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   340
	uint16 hash_head[0x400];    ///< hash heads. 0 means unused. 0xFFFC = length, 0x3 = dir
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   341
	TileIndex hash_tile[0x400]; ///< tiles. or links.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   342
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   343
	HashLink links[0x400];      ///< hash links
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   344
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6172
diff changeset
   345
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   346
#define NTP_GET_LINK_OFFS(tpf, link) ((byte*)(link) - (byte*)tpf->links)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   347
#define NTP_GET_LINK_PTR(tpf, link_offs) (HashLink*)((byte*)tpf->links + (link_offs))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   348
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   349
#define ARR(i) tpf->stack[(i)-1]
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   350
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   351
/** called after a new element was added in the queue at the last index.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   352
 * move it down to the proper position */
536
03d80fecb999 (svn r907) Sprinkle holy ANSI water:
tron
parents: 500
diff changeset
   353
static inline void HeapifyUp(NewTrackPathFinder *tpf)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   354
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   355
	StackedItem si;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   356
	int i = ++tpf->nstack;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   357
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   358
	while (i != 1 && ARR(i).priority < ARR(i>>1).priority) {
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   359
		/* the child element is larger than the parent item.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   360
		 * swap the child item and the parent item. */
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   361
		si = ARR(i); ARR(i) = ARR(i >> 1); ARR(i >> 1) = si;
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   362
		i >>= 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   363
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   364
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   365
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   366
/** called after the element 0 was eaten. fill it with a new element */
536
03d80fecb999 (svn r907) Sprinkle holy ANSI water:
tron
parents: 500
diff changeset
   367
static inline void HeapifyDown(NewTrackPathFinder *tpf)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   368
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
	StackedItem si;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   370
	int i = 1, j;
979
11ea18598e16 (svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   371
	int n;
11ea18598e16 (svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   372
11ea18598e16 (svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   373
	assert(tpf->nstack > 0);
11ea18598e16 (svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   374
	n = --tpf->nstack;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   375
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
	if (n == 0) return; // heap is empty so nothing to do?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   377
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   378
	/* copy the last item to index 0. we use it as base for heapify. */
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   379
	ARR(1) = ARR(n + 1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   380
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   381
	while ((j = i * 2) <= n) {
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   382
		/* figure out which is smaller of the children. */
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   383
		if (j != n && ARR(j).priority > ARR(j + 1).priority)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   384
			j++; // right item is smaller
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
		assert(i <= n && j <= n);
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   387
		if (ARR(i).priority <= ARR(j).priority)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
			break; // base elem smaller than smallest, done!
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   389
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   390
		/* swap parent with the child */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   391
		si = ARR(i); ARR(i) = ARR(j); ARR(j) = si;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
		i = j;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   393
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   394
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   395
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   396
/** mark a tile as visited and store the length of the path.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   397
 * if we already had a better path to this tile, return false.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   398
 * otherwise return true. */
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3132
diff changeset
   399
static bool NtpVisit(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection dir, uint length)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   400
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   401
	uint hash,head;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   402
	HashLink *link, *new_link;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   403
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   404
	assert(length < 16384-1);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   405
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   406
	hash = PATHFIND_HASH_TILE(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   407
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   408
	/* never visited before? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   409
	if ((head=tpf->hash_head[hash]) == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
		tpf->hash_tile[hash] = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   411
		tpf->hash_head[hash] = dir | (length << 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   412
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   413
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   415
	if (head != 0xffff) {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   416
		if (tile == tpf->hash_tile[hash] && (head & 0x3) == (uint)dir) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   417
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   418
			/* longer length */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   419
			if (length >= (head >> 2)) return false;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   420
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   421
			tpf->hash_head[hash] = dir | (length << 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   422
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   423
		}
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   424
		/* two tiles with the same hash, need to make a link
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   425
		 * allocate a link. if out of links, handle this by returning
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   426
		 * that a tile was already visisted. */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   427
		if (tpf->num_links_left == 0) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5028
diff changeset
   428
			DEBUG(ntp, 1, "No links left");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   429
			return false;
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   430
		}
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   431
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
		tpf->num_links_left--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   433
		link = tpf->new_link++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   434
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
		/* move the data that was previously in the hash_??? variables
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   436
		 * to the link struct, and let the hash variables point to the link */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   437
		link->tile = tpf->hash_tile[hash];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
		tpf->hash_tile[hash] = NTP_GET_LINK_OFFS(tpf, link);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   439
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   440
		link->typelength = tpf->hash_head[hash];
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   441
		tpf->hash_head[hash] = 0xFFFF; // multi link
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   442
		link->next = 0xFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   443
	} else {
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   444
		/* a linked list of many tiles,
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   445
		 * find the one corresponding to the tile, if it exists.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   446
		 * otherwise make a new link */
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   447
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   448
		uint offs = tpf->hash_tile[hash];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   449
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   450
			link = NTP_GET_LINK_PTR(tpf, offs);
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   451
			if (tile == link->tile && (link->typelength & 0x3U) == (uint)dir) {
3019
9d641b5e630b (svn r3599) -Fix: added some casts to suppress some more warnings
truelight
parents: 3017
diff changeset
   452
				if (length >= (uint)(link->typelength >> 2)) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   453
				link->typelength = dir | (length << 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   454
				return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   455
			}
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
   456
		} while ((offs = link->next) != 0xFFFF);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   457
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   458
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   459
	/* get here if we need to add a new link to link,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   460
	 * first, allocate a new link, in the same way as before */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   461
	if (tpf->num_links_left == 0) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5028
diff changeset
   462
		DEBUG(ntp, 1, "No links left");
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   463
		return false;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   464
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   465
	tpf->num_links_left--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   466
	new_link = tpf->new_link++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   467
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   468
	/* then fill the link with the new info, and establish a ptr from the old
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   469
	 * link to the new one */
1986
fcc849a38ae6 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   470
	new_link->tile = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   471
	new_link->typelength = dir | (length << 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   472
	new_link->next = 0xFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   473
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   474
	link->next = NTP_GET_LINK_OFFS(tpf, new_link);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   475
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   476
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   477
2782
c7190cbfd309 (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   478
/**
c7190cbfd309 (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   479
 * Checks if the shortest path to the given tile/dir so far is still the given
c7190cbfd309 (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   480
 * length.
c7190cbfd309 (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   481
 * @return true if the length is still the same
c7190cbfd309 (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   482
 * @pre    The given tile/dir combination should be present in the hash, by a
c7190cbfd309 (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   483
 *         previous call to NtpVisit().
c7190cbfd309 (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   484
 */
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1901
diff changeset
   485
static bool NtpCheck(NewTrackPathFinder *tpf, TileIndex tile, uint dir, uint length)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   486
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   487
	uint hash,head,offs;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   488
	HashLink *link;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   489
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   490
	hash = PATHFIND_HASH_TILE(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   491
	head=tpf->hash_head[hash];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   492
	assert(head);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   493
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   494
	if (head != 0xffff) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   495
		assert( tpf->hash_tile[hash] == tile && (head & 3) == dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   496
		assert( (head >> 2) <= length);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   497
		return length == (head >> 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   498
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   499
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   500
	/* else it's a linked list of many tiles */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   501
	offs = tpf->hash_tile[hash];
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2782
diff changeset
   502
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   503
		link = NTP_GET_LINK_PTR(tpf, offs);
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
   504
		if (tile == link->tile && (link->typelength & 0x3U) == dir) {
3019
9d641b5e630b (svn r3599) -Fix: added some casts to suppress some more warnings
truelight
parents: 3017
diff changeset
   505
			assert((uint)(link->typelength >> 2) <= length);
9d641b5e630b (svn r3599) -Fix: added some casts to suppress some more warnings
truelight
parents: 3017
diff changeset
   506
			return length == (uint)(link->typelength >> 2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   507
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   508
		offs = link->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   509
		assert(offs != 0xffff);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   510
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   511
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   512
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   513
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   514
static uint DistanceMoo(TileIndex t0, TileIndex t1)
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   515
{
7970
7d6b9ab57081 (svn r11526) -Codechange: Rename the function delta fitting to the naming style
skidd13
parents: 7930
diff changeset
   516
	const uint dx = Delta(TileX(t0), TileX(t1));
7d6b9ab57081 (svn r11526) -Codechange: Rename the function delta fitting to the naming style
skidd13
parents: 7930
diff changeset
   517
	const uint dy = Delta(TileY(t0), TileY(t1));
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   518
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   519
	const uint straightTracks = 2 * min(dx, dy); // The number of straight (not full length) tracks
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   520
	/* OPTIMISATION:
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   521
	 * Original: diagTracks = max(dx, dy) - min(dx,dy);
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   522
	 * Proof:
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   523
	 * (dx-dy) - straightTracks  == (min + max) - straightTracks = min + // max - 2 * min = max - min */
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   524
	const uint diagTracks = dx + dy - straightTracks; // The number of diagonal (full tile length) tracks.
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   525
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   526
	return diagTracks*DIAG_FACTOR + straightTracks*STR_FACTOR;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   527
}
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   528
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   529
/* These has to be small cause the max length of a track
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   530
 * is currently limited to 16384 */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   531
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   532
static const byte _length_of_track[16] = {
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4081
diff changeset
   533
	DIAG_FACTOR, DIAG_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, 0, 0,
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4081
diff changeset
   534
	DIAG_FACTOR, DIAG_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, 0, 0
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   535
};
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   536
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   537
/* new more optimized pathfinder for trains...
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   538
 * Tile is the tile the train is at.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   539
 * direction is the tile the train is moving towards. */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   540
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3132
diff changeset
   541
static void NTPEnum(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
{
2782
c7190cbfd309 (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   543
	TrackBits bits, allbits;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   544
	Trackdir track;
2782
c7190cbfd309 (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   545
	TileIndex tile_org;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   546
	StackedItem si;
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   547
	int estimation;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   548
2261
d3554e5d3e86 (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2186
diff changeset
   549
2136
c87ed6a3c15f (svn r2646) Change: [ntp] Fix uninitialized variable and add some more asserts to be able to debug an assert error.
ludde
parents: 2125
diff changeset
   550
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   551
	/* Need to have a special case for the start.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   552
	 * We shouldn't call the callback for the current tile. */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   553
	si.cur_length = 1; // Need to start at 1 cause 0 is a reserved value.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
	si.depth = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   555
	si.state = 0;
2136
c87ed6a3c15f (svn r2646) Change: [ntp] Fix uninitialized variable and add some more asserts to be able to debug an assert error.
ludde
parents: 2125
diff changeset
   556
	si.first_track = 0xFF;
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   557
	goto start_at;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   558
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2782
diff changeset
   559
	for (;;) {
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   560
		/* Get the next item to search from from the priority queue */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   561
		do {
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   562
			if (tpf->nstack == 0)
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   563
				return; // nothing left? then we're done!
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   564
			si = tpf->stack[0];
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   565
			tile = si.tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   566
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   567
			HeapifyDown(tpf);
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   568
			/* Make sure we havn't already visited this tile. */
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   569
		} while (!NtpCheck(tpf, tile, ReverseDiagDir(TrackdirToExitdir(ReverseTrackdir(si.track))), si.cur_length));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   570
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   571
		/* Add the length of this track. */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   572
		si.cur_length += _length_of_track[si.track];
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   573
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   574
callback_and_continue:
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   575
		if (tpf->enum_proc(tile, tpf->userdata, si.first_track, si.cur_length))
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   576
			return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
2136
c87ed6a3c15f (svn r2646) Change: [ntp] Fix uninitialized variable and add some more asserts to be able to debug an assert error.
ludde
parents: 2125
diff changeset
   578
		assert(si.track <= 13);
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   579
		direction = TrackdirToExitdir(si.track);
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   580
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   581
start_at:
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   582
		/* If the tile is the entry tile of a tunnel, and we're not going out of the tunnel,
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   583
		 *   need to find the exit of the tunnel. */
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
   584
		if (IsTileType(tile, MP_TUNNELBRIDGE)) {
8682
49b98ef501e2 (svn r12348) -Fix (r7573): NTP skipped junction just after bridge end
smatz
parents: 8653
diff changeset
   585
			if (GetTunnelBridgeDirection(tile) != ReverseDiagDir(direction)) {
49b98ef501e2 (svn r12348) -Fix (r7573): NTP skipped junction just after bridge end
smatz
parents: 8653
diff changeset
   586
				/* We are not just driving out of the tunnel/bridge */
49b98ef501e2 (svn r12348) -Fix (r7573): NTP skipped junction just after bridge end
smatz
parents: 8653
diff changeset
   587
				if (GetTunnelBridgeDirection(tile) != direction ||
49b98ef501e2 (svn r12348) -Fix (r7573): NTP skipped junction just after bridge end
smatz
parents: 8653
diff changeset
   588
						GetTunnelBridgeTransportType(tile) != tpf->tracktype) {
49b98ef501e2 (svn r12348) -Fix (r7573): NTP skipped junction just after bridge end
smatz
parents: 8653
diff changeset
   589
					/* We are not driving into the tunnel/bridge, or it is an invalid tunnel/bridge */
49b98ef501e2 (svn r12348) -Fix (r7573): NTP skipped junction just after bridge end
smatz
parents: 8653
diff changeset
   590
					continue;
49b98ef501e2 (svn r12348) -Fix (r7573): NTP skipped junction just after bridge end
smatz
parents: 8653
diff changeset
   591
				}
49b98ef501e2 (svn r12348) -Fix (r7573): NTP skipped junction just after bridge end
smatz
parents: 8653
diff changeset
   592
				if (!HasBit(tpf->railtypes, GetRailType(tile))) {
49b98ef501e2 (svn r12348) -Fix (r7573): NTP skipped junction just after bridge end
smatz
parents: 8653
diff changeset
   593
					bits = TRACK_BIT_NONE;
49b98ef501e2 (svn r12348) -Fix (r7573): NTP skipped junction just after bridge end
smatz
parents: 8653
diff changeset
   594
					break;
49b98ef501e2 (svn r12348) -Fix (r7573): NTP skipped junction just after bridge end
smatz
parents: 8653
diff changeset
   595
				}
8398
1e181e2e4e15 (svn r11968) -Codechange: remove redundant FindLengthOfTunnel(), use GetTunnelBridgeLength() and/or GetOtherTunnelEnd() instead
smatz
parents: 8397
diff changeset
   596
8682
49b98ef501e2 (svn r12348) -Fix (r7573): NTP skipped junction just after bridge end
smatz
parents: 8653
diff changeset
   597
				TileIndex endtile = GetOtherTunnelBridgeEnd(tile);
49b98ef501e2 (svn r12348) -Fix (r7573): NTP skipped junction just after bridge end
smatz
parents: 8653
diff changeset
   598
				si.cur_length += DIAG_FACTOR * (GetTunnelBridgeLength(tile, endtile) + 1);
49b98ef501e2 (svn r12348) -Fix (r7573): NTP skipped junction just after bridge end
smatz
parents: 8653
diff changeset
   599
				tile = endtile;
49b98ef501e2 (svn r12348) -Fix (r7573): NTP skipped junction just after bridge end
smatz
parents: 8653
diff changeset
   600
				/* tile now points to the exit tile of the tunnel/bridge */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   601
			}
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   602
		}
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   603
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   604
		/* This is a special loop used to go through
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   605
		 * a rail net and find the first intersection */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   606
		tile_org = tile;
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2782
diff changeset
   607
		for (;;) {
2136
c87ed6a3c15f (svn r2646) Change: [ntp] Fix uninitialized variable and add some more asserts to be able to debug an assert error.
ludde
parents: 2125
diff changeset
   608
			assert(direction <= 3);
4559
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4406
diff changeset
   609
			tile += TileOffsByDiagDir(direction);
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   610
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   611
			/* too long search length? bail out. */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   612
			if (si.cur_length >= tpf->maxlength) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5028
diff changeset
   613
				DEBUG(ntp, 1, "Cur_length too big");
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   614
				bits = TRACK_BIT_NONE;
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   615
				break;
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   616
			}
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   617
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   618
			/* Not a regular rail tile?
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   619
			 * Then we can't use the code below, but revert to more general code. */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   620
			if (!IsTileType(tile, MP_RAILWAY) || !IsPlainRailTile(tile)) {
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   621
				/* We found a tile which is not a normal railway tile.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   622
				 * Determine which tracks that exist on this tile. */
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   623
				bits = TrackdirBitsToTrackBits(TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_RAIL, 0)) & DiagdirReachesTrackdirs(direction));
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   624
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   625
				/* Check that the tile contains exactly one track */
7833
ba402b153b79 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 7081
diff changeset
   626
				if (bits == 0 || KillFirstBit(bits) != 0) break;
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   627
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7833
diff changeset
   628
				if (!HasBit(tpf->railtypes, GetRailType(tile))) {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   629
					bits = TRACK_BIT_NONE;
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
   630
					break;
3804
53ad7c93c3d5 (svn r4812) -Fix (FS#161) NTP properly checks for railtypes on non-plain-rail-tiles (Rubidium)
celestar
parents: 3735
diff changeset
   631
				}
53ad7c93c3d5 (svn r4812) -Fix (FS#161) NTP properly checks for railtypes on non-plain-rail-tiles (Rubidium)
celestar
parents: 3735
diff changeset
   632
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   633
				/*******************
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   634
				 * If we reach here, the tile has exactly one track.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   635
				 *   tile - index to a tile that is not rail tile, but still straight (with optional signals)
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   636
				 *   bits - bitmask of which track that exist on the tile (exactly one bit is set)
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   637
				 *   direction - which direction are we moving in?
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   638
				 *******************/
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   639
				si.track = TrackEnterdirToTrackdir(FindFirstTrack(bits), direction);
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   640
				si.cur_length += _length_of_track[si.track];
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   641
				goto callback_and_continue;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   642
			}
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   643
2782
c7190cbfd309 (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   644
			/* Regular rail tile, determine which tracks exist. */
3267
feff95208a9f (svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
tron
parents: 3234
diff changeset
   645
			allbits = GetTrackBits(tile);
2782
c7190cbfd309 (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   646
			/* Which tracks are reachable? */
c7190cbfd309 (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   647
			bits = allbits & DiagdirReachesTracks(direction);
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   648
2782
c7190cbfd309 (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   649
			/* The tile has no reachable tracks => End of rail segment
c7190cbfd309 (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   650
			 * or Intersection => End of rail segment. We check this agains all the
c7190cbfd309 (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   651
			 * bits, not just reachable ones, to prevent infinite loops. */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   652
			if (bits == TRACK_BIT_NONE || TracksOverlap(allbits)) break;
2137
bbf04957feec (svn r2647) Fix: [ntp] Fix assertion error introduced in r2635
ludde
parents: 2136
diff changeset
   653
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7833
diff changeset
   654
			if (!HasBit(tpf->railtypes, GetRailType(tile))) {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   655
				bits = TRACK_BIT_NONE;
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: 3267
diff changeset
   656
				break;
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: 3267
diff changeset
   657
			}
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: 3267
diff changeset
   658
2782
c7190cbfd309 (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   659
			/* If we reach here, the tile has exactly one track, and this
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6453
diff changeset
   660
			 track is reachable = > Rail segment continues */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   661
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   662
			track = TrackEnterdirToTrackdir(FindFirstTrack(bits), direction);
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   663
			assert(track != INVALID_TRACKDIR);
2137
bbf04957feec (svn r2647) Fix: [ntp] Fix assertion error introduced in r2635
ludde
parents: 2136
diff changeset
   664
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   665
			si.cur_length += _length_of_track[track];
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   666
8653
527a6273a0a8 (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 8616
diff changeset
   667
			/* Check if this rail is an upwards slope. If it is, then add a penalty. */
527a6273a0a8 (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 8616
diff changeset
   668
			if (IsDiagonalTrackdir(track) && IsUphillTrackdir(GetTileSlope(tile, NULL), track)) {
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   669
				// upwards slope. add some penalty.
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   670
				si.cur_length += 4 * DIAG_FACTOR;
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   671
			}
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   672
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   673
			/* railway tile with signals..? */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   674
			if (HasSignals(tile)) {
4631
584a6da986b0 (svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents: 4559
diff changeset
   675
				if (!HasSignalOnTrackdir(tile, track)) {
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   676
					/* if one way signal not pointing towards us, stop going in this direction => End of rail segment. */
9792
e6efb2eb4851 (svn r13934) -Codechange [YAPP]: Handle through signals in the pathfinders. (michi_cc)
rubidium
parents: 9490
diff changeset
   677
					if (HasSignalOnTrackdir(tile, ReverseTrackdir(track)) && IsOnewaySignal(tile, TrackdirToTrack(track))) {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   678
						bits = TRACK_BIT_NONE;
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   679
						break;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   680
					}
4631
584a6da986b0 (svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents: 4559
diff changeset
   681
				} else if (GetSignalStateByTrackdir(tile, track) == SIGNAL_STATE_GREEN) {
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   682
					/* green signal in our direction. either one way or two way. */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   683
					si.state |= 3;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   684
				} else {
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   685
					/* reached a red signal. */
4631
584a6da986b0 (svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents: 4559
diff changeset
   686
					if (HasSignalOnTrackdir(tile, ReverseTrackdir(track))) {
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   687
						/* two way red signal. unless we passed another green signal on the way,
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   688
						 * stop going in this direction => End of rail segment.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   689
						 * this is to prevent us from going into a full platform. */
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   690
						if (!(si.state & 1)) {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   691
							bits = TRACK_BIT_NONE;
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   692
							break;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   693
						}
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   694
					}
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   695
					if (!(si.state & 2)) {
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   696
						/* Is this the first signal we see? And it's red... add penalty */
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   697
						si.cur_length += 10 * DIAG_FACTOR;
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   698
						si.state += 2; // remember that we added penalty.
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   699
						/* Because we added a penalty, we can't just continue as usual.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   700
						 * Need to get out and let A* do it's job with
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   701
						 * possibly finding an even shorter path. */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   702
						break;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   703
					}
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   704
				}
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   705
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   706
				if (tpf->enum_proc(tile, tpf->userdata, si.first_track, si.cur_length))
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   707
					return; // Don't process this tile any further
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   708
			}
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   709
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   710
			/* continue with the next track */
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   711
			direction = TrackdirToExitdir(track);
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   712
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   713
			/* safety check if we're running around chasing our tail... (infinite loop) */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   714
			if (tile == tile_org) {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   715
				bits = TRACK_BIT_NONE;
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   716
				break;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   717
			}
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   718
		}
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   719
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   720
		/* There are no tracks to choose between.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   721
		 * Stop searching in this direction */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   722
		if (bits == TRACK_BIT_NONE)
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   723
			continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   724
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   725
		/****************
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   726
		 * We got multiple tracks to choose between (intersection).
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   727
		 * Branch the search space into several branches.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   728
		 ****************/
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   729
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   730
		/* Check if we've already visited this intersection.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   731
		 * If we've already visited it with a better length, then
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   732
		 * there's no point in visiting it again. */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   733
		if (!NtpVisit(tpf, tile, direction, si.cur_length))
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   734
			continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   735
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   736
		/* Push all possible alternatives that we can reach from here
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   737
		 * onto the priority heap.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   738
		 * 'bits' contains the tracks that we can choose between. */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   739
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   740
		/* First compute the estimated distance to the target.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   741
		 * This is used to implement A* */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   742
		estimation = 0;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   743
		if (tpf->dest != 0)
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   744
			estimation = DistanceMoo(tile, tpf->dest);
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   745
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   746
		si.depth++;
2774
8ab8627b5ba1 (svn r3321) - Fix: A wrong use of the map m5 bits, where a previously calculated "bits" variable should have been used. This resulted in the pathfinder imagining junctions, which negatively affects performance somewhat (Darkvater).
matthijs
parents: 2548
diff changeset
   747
		if (si.depth == 0)
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   748
			continue; // We overflowed our depth. No more searching in this direction.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   749
		si.tile = tile;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   750
		while (bits != TRACK_BIT_NONE) {
5598
2fadbd43709d (svn r8052) - Codechange: RemoveFirstTrack() and RemoveFirstTrackdir() now accept pointer to TrackBits/TrackdirBits instead of reference.
KUDr
parents: 5587
diff changeset
   751
			Track track = RemoveFirstTrack(&bits);
8804
84876230b6b1 (svn r12545) -Cleanup: Replace some tables of magic values with already existing functions.
frosch
parents: 8800
diff changeset
   752
			si.track = TrackEnterdirToTrackdir(track, direction);
2782
c7190cbfd309 (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   753
			assert(si.track != 0xFF);
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   754
			si.priority = si.cur_length + estimation;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   755
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   756
			/* out of stack items, bail out? */
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   757
			if (tpf->nstack >= lengthof(tpf->stack)) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5028
diff changeset
   758
				DEBUG(ntp, 1, "Out of stack");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   759
				break;
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   760
			}
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   761
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   762
			tpf->stack[tpf->nstack] = si;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   763
			HeapifyUp(tpf);
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   764
		};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   765
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   766
		/* If this is the first intersection, we need to fill the first_track member.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   767
		 * so the code outside knows which path is better.
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   768
		 * also randomize the order in which we search through them. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   769
		if (si.depth == 1) {
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   770
			assert(tpf->nstack == 1 || tpf->nstack == 2 || tpf->nstack == 3);
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   771
			if (tpf->nstack != 1) {
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   772
				uint32 r = Random();
5733
388bb9dcb79b (svn r8276) -Fix
tron
parents: 5598
diff changeset
   773
				if (r & 1) Swap(tpf->stack[0].track, tpf->stack[1].track);
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   774
				if (tpf->nstack != 2) {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   775
					TrackdirByte t = tpf->stack[2].track;
5733
388bb9dcb79b (svn r8276) -Fix
tron
parents: 5598
diff changeset
   776
					if (r & 2) Swap(tpf->stack[0].track, t);
388bb9dcb79b (svn r8276) -Fix
tron
parents: 5598
diff changeset
   777
					if (r & 4) Swap(tpf->stack[1].track, t);
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   778
					tpf->stack[2].first_track = tpf->stack[2].track = t;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   779
				}
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   780
				tpf->stack[0].first_track = tpf->stack[0].track;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   781
				tpf->stack[1].first_track = tpf->stack[1].track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   782
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   783
		}
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   784
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   785
		/* Continue with the next from the queue... */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   786
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   787
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   788
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   789
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6248
diff changeset
   790
/** new pathfinder for trains. better and faster. */
8236
8a5dd0b42e47 (svn r11800) -Codechange: move some functions to a more logical location + some type safety.
rubidium
parents: 8139
diff changeset
   791
void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypes railtypes, DiagDirection direction, NTPEnumProc* enum_proc, void* data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   792
{
8798
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   793
	SmallStackSafeStackAlloc<NewTrackPathFinder, 1> tpf;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   794
8798
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   795
	tpf->dest = dest;
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   796
	tpf->userdata = data;
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   797
	tpf->enum_proc = enum_proc;
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   798
	tpf->tracktype = TRANSPORT_RAIL;
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   799
	tpf->railtypes = railtypes;
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
   800
	tpf->maxlength = min(_settings_game.pf.opf.pf_maxlength * 3, 10000);
8798
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   801
	tpf->nstack = 0;
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   802
	tpf->new_link = tpf->links;
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   803
	tpf->num_links_left = lengthof(tpf->links);
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   804
	memset(tpf->hash_head, 0, sizeof(tpf->hash_head));
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   805
8798
2c6062ce7dba (svn r12536) -Codechange: some stack allocations were too large for NDS, so use the SmallStackSafeStackAlloc wrapper. Allocate on the stack by default and on the heap for NDS (or other devices that have a very small stack).
rubidium
parents: 8682
diff changeset
   806
	NTPEnum(tpf, tile, direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   807
}