src/pathfind.cpp
author skidd13
Mon, 19 Nov 2007 21:02:30 +0000
changeset 8424 4a488a90ccab
parent 8329 77e50bd6ad67
child 8426 89570a8b6ed0
permissions -rw-r--r--
(svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
     3
/** @file pathfind.cpp */
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1209
diff changeset
     6
#include "openttd.h"
3234
986c30171e92 (svn r3907) Replace many bridge related direct map accesses with calls to shiny new functions and mark some strange constructs with XXX
tron
parents: 3184
diff changeset
     7
#include "bridge_map.h"
3900
4984308f9125 (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"
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3894
diff changeset
     9
#include "depot.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2153
diff changeset
    10
#include "functions.h"
6949
72d11a1e1e60 (svn r9609) -Codechange: Move some function prototypes out of functions.h and into landscape.h, and add a few where they didn't exist.
maedhros
parents: 6678
diff changeset
    11
#include "landscape.h"
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents: 536
diff changeset
    12
#include "map.h"
1209
a1ac96655b79 (svn r1713) Split off several functions which query/set information about a single tile from map.h and put them into a seperate file tile.h
tron
parents: 1035
diff changeset
    13
#include "tile.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include "pathfind.h"
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
    15
#include "rail.h"
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
    16
#include "debug.h"
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
    17
#include "tunnel_map.h"
2153
91e89aa8c299 (svn r2663) Include variables.h only in these files which need it, not globally via openttd.h
tron
parents: 2137
diff changeset
    18
#include "variables.h"
3735
56a5bdee6c9a (svn r4715) - Fix: (FS#109) ? Wrongfully bad signal - Don't allow OPF to enter train depot from the back
KUDr
parents: 3618
diff changeset
    19
#include "depot.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
    21
/* remember which tiles we have already visited so we don't visit them again. */
1977
4392ae3d8e31 (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
5dd3db2b86d7 (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
5dd3db2b86d7 (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
68ec4a2f2d79 (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
68ec4a2f2d79 (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];
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
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
5dd3db2b86d7 (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
68ec4a2f2d79 (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
68ec4a2f2d79 (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
5dd3db2b86d7 (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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
static const byte _bits_mask[4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   114
	0x19,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   115
	0x16,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   116
	0x25,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   117
	0x2A,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   118
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   120
static const DiagDirection _tpf_new_direction[14] = {
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   121
	DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE,
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   122
	INVALID_DIAGDIR, INVALID_DIAGDIR,
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   123
	DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   125
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   126
static const DiagDirection _tpf_prev_direction[14] = {
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   127
	DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW,
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   128
	INVALID_DIAGDIR, INVALID_DIAGDIR,
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   129
	DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE, DIAGDIR_NW,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   133
static const byte _otherdir_mask[4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   134
	0x10,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   135
	0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   136
	0x5,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   137
	0x2A,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   138
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3132
diff changeset
   140
static void TPFMode2(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   141
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   142
	uint bits;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   143
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   144
	RememberData rd;
747
e795750b96de (svn r1203) -Fix: the pathfinder no longer sees rail with an other owner as a
truelight
parents: 679
diff changeset
   145
3618
dc8cebe5ffd3 (svn r4515) -Codechange: TPFMode2 is currently only used for TRANSPORT_WATER. So remove all stuff that deals with other transport types and assert TRANSPORT_WATER
celestar
parents: 3420
diff changeset
   146
	assert(tpf->tracktype == TRANSPORT_WATER);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   147
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   148
	/* This addition will sometimes overflow by a single tile.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   149
	 * The use of TILE_MASK here makes sure that we still point at a valid
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   150
	 * tile, and then this tile will be in the sentinel row/col, so GetTileTrackStatus will fail. */
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4406
diff changeset
   151
	tile = TILE_MASK(tile + TileOffsByDiagDir(direction));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   152
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   153
	if (++tpf->rd.cur_length > 50)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   154
		return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   155
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6987
diff changeset
   156
	bits = GetTileTrackStatus(tile, tpf->tracktype, tpf->sub_type);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   157
	bits = (byte)((bits | (bits >> 8)) & _bits_mask[direction]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   158
	if (bits == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   159
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   160
926
bd4312619522 (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
   161
	assert(TileX(tile) != MapMaxX() && TileY(tile) != MapMaxY());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   163
	if ( (bits & (bits - 1)) == 0 ) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
		/* only one direction */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
		i = 0;
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   166
		while (!(bits & 1))
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   167
			i++, bits >>= 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   168
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   169
		rd = tpf->rd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
		goto continue_here;
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
	/* several directions */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   173
	i=0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   174
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   175
		if (!(bits & 1)) continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   176
		rd = tpf->rd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   178
		/* Change direction 4 times only */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   179
		if ((byte)i != tpf->rd.pft_var6) {
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2782
diff changeset
   180
			if (++tpf->rd.depth > 4) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   181
				tpf->rd = rd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
				return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   183
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   184
			tpf->rd.pft_var6 = (byte)i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   187
continue_here:;
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8329
diff changeset
   188
		tpf->the_dir = (Trackdir)(i + (HasBit(_otherdir_mask[direction], i) ? 8 : 0));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   189
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   190
		if (!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length, NULL)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   191
			TPFMode2(tpf, tile, _tpf_new_direction[tpf->the_dir]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   192
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   193
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   194
		tpf->rd = rd;
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   195
	} while (++i, bits >>= 1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   196
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   197
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   198
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   199
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
   200
/* Returns the end tile and the length of a tunnel. The length does not
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
   201
 * include the starting tile (entry), it does include the end tile (exit).
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
   202
 */
3420
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   203
FindLengthOfTunnelResult FindLengthOfTunnel(TileIndex tile, DiagDirection dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
{
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4406
diff changeset
   205
	TileIndexDiff delta = TileOffsByDiagDir(dir);
3420
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   206
	uint z = GetTileZ(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   207
	FindLengthOfTunnelResult flotr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   208
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   209
	flotr.length = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   210
3420
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   211
	dir = ReverseDiagDir(dir);
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   212
	do {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   213
		flotr.length++;
3420
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   214
		tile += delta;
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   215
	} while(
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   216
		!IsTunnelTile(tile) ||
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   217
		GetTunnelDirection(tile) != dir ||
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   218
		GetTileZ(tile) != z
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   219
	);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   220
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   221
	flotr.tile = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   222
	return flotr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   223
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   224
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   225
static const uint16 _tpfmode1_and[4] = { 0x1009, 0x16, 0x520, 0x2A00 };
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   226
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
   227
static uint SkipToEndOfTunnel(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1901
diff changeset
   228
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   229
	FindLengthOfTunnelResult flotr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   230
	TPFSetTileBit(tpf, tile, 14);
159
139cf78bfb28 (svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents: 148
diff changeset
   231
	flotr = FindLengthOfTunnel(tile, direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   232
	tpf->rd.cur_length += flotr.length;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
	TPFSetTileBit(tpf, flotr.tile, 14);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
	return flotr.tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   235
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   236
8329
77e50bd6ad67 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 7577
diff changeset
   237
const byte _ffb_64[64] = {
4344
5d0e40cd67b9 (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
   238
 0,  0,  1,  0,  2,  0,  1,  0,
5d0e40cd67b9 (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
   239
 3,  0,  1,  0,  2,  0,  1,  0,
5d0e40cd67b9 (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
   240
 4,  0,  1,  0,  2,  0,  1,  0,
5d0e40cd67b9 (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
   241
 3,  0,  1,  0,  2,  0,  1,  0,
5d0e40cd67b9 (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
   242
 5,  0,  1,  0,  2,  0,  1,  0,
5d0e40cd67b9 (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
   243
 3,  0,  1,  0,  2,  0,  1,  0,
5d0e40cd67b9 (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
   244
 4,  0,  1,  0,  2,  0,  1,  0,
5d0e40cd67b9 (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
   245
 3,  0,  1,  0,  2,  0,  1,  0,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   247
7567
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   248
static void TPFMode1(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   249
7567
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   250
/** Most code of the "Normal" case of TPF Mode 1; for signals special tricks
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   251
 * have to be done, but those happen in TPFMode1; this is just to prevent
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   252
 * gotos ;). */
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   253
static inline void TPFMode1_NormalCase(TrackPathFinder* tpf, TileIndex tile, TileIndex tile_org, DiagDirection direction)
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   254
{
5706
5a060b22feaa (svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents: 5668
diff changeset
   255
	/* Check in case of rail if the owner is the same */
5a060b22feaa (svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents: 5668
diff changeset
   256
	if (tpf->tracktype == TRANSPORT_RAIL) {
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   257
		/* don't enter train depot from the back */
5706
5a060b22feaa (svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents: 5668
diff changeset
   258
		if (IsTileDepotType(tile, TRANSPORT_RAIL) && GetRailDepotDirection(tile) == direction) return;
5668
76262aa5075d (svn r7620) -Fix: [OPF] signal update was incorrectly propagated:
KUDr
parents: 5573
diff changeset
   259
5706
5a060b22feaa (svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents: 5668
diff changeset
   260
		if (IsTileType(tile_org, MP_RAILWAY) || IsTileType(tile_org, MP_STATION) || IsTileType(tile_org, MP_TUNNELBRIDGE))
5a060b22feaa (svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents: 5668
diff changeset
   261
			if (IsTileType(tile, MP_RAILWAY) || IsTileType(tile, MP_STATION) || IsTileType(tile, MP_TUNNELBRIDGE))
5a060b22feaa (svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents: 5668
diff changeset
   262
				if (GetTileOwner(tile_org) != GetTileOwner(tile)) return;
752
df3e096dd9db (svn r1208) -Fix: the owner-check introduced in r1203 now also works correctly for
truelight
parents: 747
diff changeset
   263
	}
747
e795750b96de (svn r1203) -Fix: the pathfinder no longer sees rail with an other owner as a
truelight
parents: 679
diff changeset
   264
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   265
	/* check if the new tile can be entered from that direction */
5706
5a060b22feaa (svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents: 5668
diff changeset
   266
	if (tpf->tracktype == TRANSPORT_ROAD) {
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   267
		/* road stops and depots now have a track (r4419)
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   268
		 * don't enter road stop from the back */
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5984
diff changeset
   269
		if (IsStandardRoadStopTile(tile) && ReverseDiagDir(GetRoadStopDir(tile)) != direction) return;
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   270
		/* don't enter road depot from the back */
5706
5a060b22feaa (svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents: 5668
diff changeset
   271
		if (IsTileDepotType(tile, TRANSPORT_ROAD) && ReverseDiagDir(GetRoadDepotDirection(tile)) != direction) return;
5a060b22feaa (svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents: 5668
diff changeset
   272
	}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3894
diff changeset
   273
5708
76382881636e (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5707
diff changeset
   274
	/* Check if the new tile is a tunnel or bridge head and that the direction
76382881636e (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5707
diff changeset
   275
	 * and transport type match */
76382881636e (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5707
diff changeset
   276
	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
76382881636e (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5707
diff changeset
   277
		if (IsTunnel(tile)) {
76382881636e (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5707
diff changeset
   278
			if (GetTunnelDirection(tile) != direction ||
76382881636e (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5707
diff changeset
   279
					GetTunnelTransportType(tile) != tpf->tracktype) {
76382881636e (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5707
diff changeset
   280
				return;
76382881636e (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5707
diff changeset
   281
			}
76382881636e (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5707
diff changeset
   282
		} else if (IsBridge(tile)) {
76382881636e (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5707
diff changeset
   283
			if (GetBridgeRampDirection(tile) != direction ||
76382881636e (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5707
diff changeset
   284
					GetBridgeTransportType(tile) != tpf->tracktype) {
76382881636e (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5707
diff changeset
   285
				return;
76382881636e (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5707
diff changeset
   286
			}
76382881636e (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5707
diff changeset
   287
		}
76382881636e (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5707
diff changeset
   288
	}
76382881636e (svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents: 5707
diff changeset
   289
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   290
	tpf->rd.cur_length++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   291
7567
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   292
	uint bits = GetTileTrackStatus(tile, tpf->tracktype, tpf->sub_type);
5706
5a060b22feaa (svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents: 5668
diff changeset
   293
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   294
	if ((byte)bits != tpf->var2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   295
		bits &= _tpfmode1_and[direction];
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   296
		bits = bits | (bits >> 8);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   297
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   298
	bits &= 0xBF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   299
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   300
	if (bits != 0) {
8329
77e50bd6ad67 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 7577
diff changeset
   301
		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
   302
			do {
7567
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   303
				int i = FIND_FIRST_BIT(bits);
8329
77e50bd6ad67 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 7577
diff changeset
   304
				bits = KillFirstBit(bits);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   305
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   306
				tpf->the_dir = (Trackdir)((_otherdir_mask[direction] & (byte)(1 << i)) ? (i + 8) : i);
7567
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   307
				RememberData rd = tpf->rd;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   308
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   309
				if (TPFSetTileBit(tpf, tile, tpf->the_dir) &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   310
						!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length, &tpf->rd.pft_var6) ) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   311
					TPFMode1(tpf, tile, _tpf_new_direction[tpf->the_dir]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   312
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   313
				tpf->rd = rd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   314
			} while (bits != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   315
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   316
	}
7567
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   317
}
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   318
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   319
static void TPFMode1(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   320
{
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   321
	TileIndex tile_org = tile;
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   322
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   323
	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   324
		if (IsTunnel(tile)) {
7576
38425ad72aaf (svn r10345) -Fix [FS#290]: Make OPF handle coming out of a tunnel as well as going into a tunnel, to support road vehicles looking back when finding a depot while in a tunnel.
matthijs
parents: 7567
diff changeset
   325
			if (GetTunnelTransportType(tile) != tpf->tracktype) {
7567
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   326
				return;
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   327
			}
7576
38425ad72aaf (svn r10345) -Fix [FS#290]: Make OPF handle coming out of a tunnel as well as going into a tunnel, to support road vehicles looking back when finding a depot while in a tunnel.
matthijs
parents: 7567
diff changeset
   328
			/* Only skip through the tunnel if heading inwards. We can
38425ad72aaf (svn r10345) -Fix [FS#290]: Make OPF handle coming out of a tunnel as well as going into a tunnel, to support road vehicles looking back when finding a depot while in a tunnel.
matthijs
parents: 7567
diff changeset
   329
			 * be headed outwards if our starting position was in a
38425ad72aaf (svn r10345) -Fix [FS#290]: Make OPF handle coming out of a tunnel as well as going into a tunnel, to support road vehicles looking back when finding a depot while in a tunnel.
matthijs
parents: 7567
diff changeset
   330
			 * tunnel and we're pathfinding backwards */
38425ad72aaf (svn r10345) -Fix [FS#290]: Make OPF handle coming out of a tunnel as well as going into a tunnel, to support road vehicles looking back when finding a depot while in a tunnel.
matthijs
parents: 7567
diff changeset
   331
			if (GetTunnelDirection(tile) == direction) {
38425ad72aaf (svn r10345) -Fix [FS#290]: Make OPF handle coming out of a tunnel as well as going into a tunnel, to support road vehicles looking back when finding a depot while in a tunnel.
matthijs
parents: 7567
diff changeset
   332
				tile = SkipToEndOfTunnel(tpf, tile, direction);
7577
13ae36e06a14 (svn r10346) -Fix: Forgotten "else" in r10345 (thanks peter1138).
matthijs
parents: 7576
diff changeset
   333
			} else if (GetTunnelDirection(tile) != ReverseDiagDir(direction)) {
7576
38425ad72aaf (svn r10345) -Fix [FS#290]: Make OPF handle coming out of a tunnel as well as going into a tunnel, to support road vehicles looking back when finding a depot while in a tunnel.
matthijs
parents: 7567
diff changeset
   334
				/* We don't support moving through the sides of a tunnel
38425ad72aaf (svn r10345) -Fix [FS#290]: Make OPF handle coming out of a tunnel as well as going into a tunnel, to support road vehicles looking back when finding a depot while in a tunnel.
matthijs
parents: 7567
diff changeset
   335
				 * entrance :-) */
38425ad72aaf (svn r10345) -Fix [FS#290]: Make OPF handle coming out of a tunnel as well as going into a tunnel, to support road vehicles looking back when finding a depot while in a tunnel.
matthijs
parents: 7567
diff changeset
   336
				return;
38425ad72aaf (svn r10345) -Fix [FS#290]: Make OPF handle coming out of a tunnel as well as going into a tunnel, to support road vehicles looking back when finding a depot while in a tunnel.
matthijs
parents: 7567
diff changeset
   337
			}
7567
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   338
		} else {
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   339
			TileIndex tile_end;
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   340
			if (GetBridgeRampDirection(tile) != direction ||
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   341
					GetBridgeTransportType(tile) != tpf->tracktype) {
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   342
				return;
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   343
			}
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   344
			//fprintf(stderr, "%s: Planning over bridge\n", __func__);
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   345
			// TODO doesn't work - WHAT doesn't work?
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   346
			TPFSetTileBit(tpf, tile, 14);
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   347
			tile_end = GetOtherBridgeEnd(tile);
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   348
			tpf->rd.cur_length += DistanceManhattan(tile, tile_end);
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   349
			tile = tile_end;
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   350
			TPFSetTileBit(tpf, tile, 14);
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   351
		}
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   352
	}
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   353
	tile += TileOffsByDiagDir(direction);
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   354
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   355
	TPFMode1_NormalCase(tpf, tile, tile_org, direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   356
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
	/* the next is only used when signals are checked.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   358
	 * seems to go in 2 directions simultaneously */
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   359
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   360
	/* if i can get rid of this, tail end recursion can be used to minimize
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   361
	 * stack space dramatically. */
2006
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1986
diff changeset
   362
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1986
diff changeset
   363
	/* If we are doing signal setting, we must reverse at evere tile, so we
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1986
diff changeset
   364
	 * iterate all the tracks in a signal block, even when a normal train would
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1986
diff changeset
   365
	 * not reach it (for example, when two lines merge */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   366
	if (tpf->hasbit_13)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   367
		return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   368
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3132
diff changeset
   369
	direction = ReverseDiagDir(direction);
5707
73fc8891bebc (svn r7717) -Fix (runknown): When following path for signals, don't skip back to the
peter1138
parents: 5706
diff changeset
   370
	tile += TileOffsByDiagDir(direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   371
7567
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   372
	uint bits = GetTileTrackStatus(tile, tpf->tracktype, tpf->sub_type);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   373
	bits |= (bits >> 8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   375
	if ( (byte)bits != tpf->var2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
		bits &= _bits_mask[direction];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   377
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   378
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   379
	bits &= 0xBF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   380
	if (bits == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   383
	do {
7567
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   384
		uint i = FIND_FIRST_BIT(bits);
8329
77e50bd6ad67 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 7577
diff changeset
   385
		bits = KillFirstBit(bits);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   386
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   387
		tpf->the_dir = (Trackdir)((_otherdir_mask[direction] & (byte)(1 << i)) ? (i + 8) : i);
7567
8723fa633d31 (svn r10336) -Fix [FS#910]: reaching the end of a line in certain cases incorrectly stopped signal updates
peter1138
parents: 7179
diff changeset
   388
		RememberData rd = tpf->rd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   389
		if (TPFSetTileBit(tpf, tile, tpf->the_dir) &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   390
				!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length, &tpf->rd.pft_var6) ) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   391
			TPFMode1(tpf, tile, _tpf_new_direction[tpf->the_dir]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   393
		tpf->rd = rd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   394
	} while (bits != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   395
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   396
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6987
diff changeset
   397
void FollowTrack(TileIndex tile, uint16 flags, 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
   398
{
318
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   399
	TrackPathFinder tpf;
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
	assert(direction < 4);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   402
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   403
	/* initialize path finder variables */
318
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   404
	tpf.userdata = data;
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   405
	tpf.enum_proc = enum_proc;
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   406
	tpf.new_link = tpf.links;
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   407
	tpf.num_links_left = lengthof(tpf.links);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   408
318
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   409
	tpf.rd.cur_length = 0;
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   410
	tpf.rd.depth = 0;
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   411
	tpf.rd.pft_var6 = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   412
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8329
diff changeset
   413
	tpf.var2 = HasBit(flags, 15) ? 0x43 : 0xFF; // 0x8000
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8329
diff changeset
   415
	tpf.disable_tile_hash = HasBit(flags, 12);  // 0x1000
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8329
diff changeset
   416
	tpf.hasbit_13         = HasBit(flags, 13);  // 0x2000
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   417
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   418
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   419
	tpf.tracktype = (TransportType)(flags & 0xFF);
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6987
diff changeset
   420
	tpf.sub_type = sub_type;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   421
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8329
diff changeset
   422
	if (HasBit(flags, 11)) {
318
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   423
		tpf.rd.pft_var6 = 0xFF;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   424
		tpf.enum_proc(tile, data, INVALID_TRACKDIR, 0, 0);
318
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   425
		TPFMode2(&tpf, tile, direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   426
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   427
		/* clear the hash_heads */
318
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   428
		memset(tpf.hash_head, 0, sizeof(tpf.hash_head));
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   429
		TPFMode1(&tpf, tile, direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   430
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   431
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
	if (after_proc != NULL)
318
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   433
		after_proc(&tpf);
0
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
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6498
diff changeset
   436
struct StackedItem {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   437
	TileIndex tile;
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   438
	uint16 cur_length; ///< This is the current length to this tile.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   439
	uint16 priority;   ///< This is the current length + estimated length to the goal.
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   440
	TrackdirByte track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   441
	byte depth;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   442
	byte state;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   443
	byte first_track;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6498
diff changeset
   444
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   446
static const Trackdir _new_trackdir[6][4] = {
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   447
{TRACKDIR_X_NE,    INVALID_TRACKDIR, TRACKDIR_X_SW,    INVALID_TRACKDIR,},
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   448
{INVALID_TRACKDIR, TRACKDIR_Y_SE,    INVALID_TRACKDIR, TRACKDIR_Y_NW,},
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   449
{INVALID_TRACKDIR, TRACKDIR_UPPER_E, TRACKDIR_UPPER_W, INVALID_TRACKDIR,},
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   450
{TRACKDIR_LOWER_E, INVALID_TRACKDIR, INVALID_TRACKDIR, TRACKDIR_LOWER_W,},
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   451
{TRACKDIR_LEFT_N,  TRACKDIR_LEFT_S,  INVALID_TRACKDIR, INVALID_TRACKDIR,},
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   452
{INVALID_TRACKDIR, INVALID_TRACKDIR, TRACKDIR_RIGHT_S, TRACKDIR_RIGHT_N,},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   453
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   454
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6498
diff changeset
   455
struct HashLink {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   456
	TileIndex tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   457
	uint16 typelength;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   458
	uint16 next;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6498
diff changeset
   459
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   460
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6498
diff changeset
   461
struct NewTrackPathFinder {
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   462
	NTPEnumProc *enum_proc;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   463
	void *userdata;
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   464
	TileIndex dest;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   465
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3267
diff changeset
   466
	TransportType tracktype;
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3267
diff changeset
   467
	RailTypeMask railtypes;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   468
	uint maxlength;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   469
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   470
	HashLink *new_link;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   471
	uint num_links_left;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   472
979
f12f96116cdd (svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   473
	uint nstack;
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   474
	StackedItem stack[256];     ///< priority queue of stacked items
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   475
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   476
	uint16 hash_head[0x400];    ///< hash heads. 0 means unused. 0xFFFC = length, 0x3 = dir
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   477
	TileIndex hash_tile[0x400]; ///< tiles. or links.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   478
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   479
	HashLink links[0x400];      ///< hash links
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   480
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6498
diff changeset
   481
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   482
#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
   483
#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
   484
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   485
#define ARR(i) tpf->stack[(i)-1]
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   486
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   487
/** called after a new element was added in the queue at the last index.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   488
 * move it down to the proper position */
536
aef4753985d3 (svn r907) Sprinkle holy ANSI water:
tron
parents: 500
diff changeset
   489
static inline void HeapifyUp(NewTrackPathFinder *tpf)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   490
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   491
	StackedItem si;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   492
	int i = ++tpf->nstack;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   493
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   494
	while (i != 1 && ARR(i).priority < ARR(i>>1).priority) {
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   495
		/* the child element is larger than the parent item.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   496
		 * swap the child item and the parent item. */
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   497
		si = ARR(i); ARR(i) = ARR(i >> 1); ARR(i >> 1) = si;
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   498
		i >>= 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   499
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   500
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   501
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   502
/** called after the element 0 was eaten. fill it with a new element */
536
aef4753985d3 (svn r907) Sprinkle holy ANSI water:
tron
parents: 500
diff changeset
   503
static inline void HeapifyDown(NewTrackPathFinder *tpf)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   504
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   505
	StackedItem si;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
	int i = 1, j;
979
f12f96116cdd (svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   507
	int n;
f12f96116cdd (svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   508
f12f96116cdd (svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   509
	assert(tpf->nstack > 0);
f12f96116cdd (svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   510
	n = --tpf->nstack;
0
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
	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
   513
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   514
	/* copy the last item to index 0. we use it as base for heapify. */
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   515
	ARR(1) = ARR(n + 1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   516
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   517
	while ((j = i * 2) <= n) {
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   518
		/* figure out which is smaller of the children. */
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   519
		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
   520
			j++; // right item is smaller
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   521
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   522
		assert(i <= n && j <= n);
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   523
		if (ARR(i).priority <= ARR(j).priority)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   524
			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
   525
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   526
		/* swap parent with the child */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   527
		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
   528
		i = j;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   529
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   530
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   531
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   532
/** mark a tile as visited and store the length of the path.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   533
 * if we already had a better path to this tile, return false.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   534
 * otherwise return true. */
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3132
diff changeset
   535
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
   536
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   537
	uint hash,head;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   538
	HashLink *link, *new_link;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   539
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   540
	assert(length < 16384-1);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   541
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
	hash = PATHFIND_HASH_TILE(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   543
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   544
	/* never visited before? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   545
	if ((head=tpf->hash_head[hash]) == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   546
		tpf->hash_tile[hash] = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   547
		tpf->hash_head[hash] = dir | (length << 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   548
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   549
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   550
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   551
	if (head != 0xffff) {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   552
		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
   553
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   554
			/* longer length */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   555
			if (length >= (head >> 2)) return false;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   556
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   557
			tpf->hash_head[hash] = dir | (length << 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   558
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   559
		}
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   560
		/* two tiles with the same hash, need to make a link
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   561
		 * allocate a link. if out of links, handle this by returning
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   562
		 * that a tile was already visisted. */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   563
		if (tpf->num_links_left == 0) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5028
diff changeset
   564
			DEBUG(ntp, 1, "No links left");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
			return false;
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   566
		}
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   567
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
		tpf->num_links_left--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   569
		link = tpf->new_link++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   570
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   571
		/* 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
   572
		 * 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
   573
		link->tile = tpf->hash_tile[hash];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   574
		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
   575
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   576
		link->typelength = tpf->hash_head[hash];
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   577
		tpf->hash_head[hash] = 0xFFFF; // multi link
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   578
		link->next = 0xFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   579
	} else {
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   580
		/* a linked list of many tiles,
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   581
		 * find the one corresponding to the tile, if it exists.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   582
		 * otherwise make a new link */
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   583
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   584
		uint offs = tpf->hash_tile[hash];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   585
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   586
			link = NTP_GET_LINK_PTR(tpf, offs);
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   587
			if (tile == link->tile && (link->typelength & 0x3U) == (uint)dir) {
3019
a09d8f7b48be (svn r3599) -Fix: added some casts to suppress some more warnings
truelight
parents: 3017
diff changeset
   588
				if (length >= (uint)(link->typelength >> 2)) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   589
				link->typelength = dir | (length << 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   590
				return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   591
			}
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   592
		} while ((offs = link->next) != 0xFFFF);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   593
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   594
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   595
	/* 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
   596
	 * first, allocate a new link, in the same way as before */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   597
	if (tpf->num_links_left == 0) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5028
diff changeset
   598
		DEBUG(ntp, 1, "No links left");
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   599
		return false;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   600
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   601
	tpf->num_links_left--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   602
	new_link = tpf->new_link++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   603
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   604
	/* 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
   605
	 * link to the new one */
1986
5dd3db2b86d7 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   606
	new_link->tile = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   607
	new_link->typelength = dir | (length << 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   608
	new_link->next = 0xFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   609
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   610
	link->next = NTP_GET_LINK_OFFS(tpf, new_link);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   611
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   612
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   613
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   614
/**
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   615
 * Checks if the shortest path to the given tile/dir so far is still the given
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   616
 * length.
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   617
 * @return true if the length is still the same
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   618
 * @pre    The given tile/dir combination should be present in the hash, by a
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   619
 *         previous call to NtpVisit().
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   620
 */
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1901
diff changeset
   621
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
   622
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   623
	uint hash,head,offs;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   624
	HashLink *link;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   625
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   626
	hash = PATHFIND_HASH_TILE(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   627
	head=tpf->hash_head[hash];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   628
	assert(head);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   629
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   630
	if (head != 0xffff) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
		assert( tpf->hash_tile[hash] == tile && (head & 3) == dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
		assert( (head >> 2) <= length);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
		return length == (head >> 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   634
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   635
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   636
	/* 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
   637
	offs = tpf->hash_tile[hash];
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2782
diff changeset
   638
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   639
		link = NTP_GET_LINK_PTR(tpf, offs);
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2952
diff changeset
   640
		if (tile == link->tile && (link->typelength & 0x3U) == dir) {
3019
a09d8f7b48be (svn r3599) -Fix: added some casts to suppress some more warnings
truelight
parents: 3017
diff changeset
   641
			assert((uint)(link->typelength >> 2) <= length);
a09d8f7b48be (svn r3599) -Fix: added some casts to suppress some more warnings
truelight
parents: 3017
diff changeset
   642
			return length == (uint)(link->typelength >> 2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   643
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   644
		offs = link->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   645
		assert(offs != 0xffff);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   646
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   647
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   648
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   649
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   650
static const uint16 _is_upwards_slope[15] = {
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   651
	0,                                           ///< no tileh
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   652
	(1 << TRACKDIR_X_SW) | (1 << TRACKDIR_Y_NW), ///< 1
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   653
	(1 << TRACKDIR_X_SW) | (1 << TRACKDIR_Y_SE), ///< 2
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   654
	(1 << TRACKDIR_X_SW),                        ///< 3
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   655
	(1 << TRACKDIR_X_NE) | (1 << TRACKDIR_Y_SE), ///< 4
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   656
	0,                                           ///< 5
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   657
	(1 << TRACKDIR_Y_SE),                        ///< 6
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   658
	0,                                           ///< 7
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   659
	(1 << TRACKDIR_X_NE) | (1 << TRACKDIR_Y_NW), ///< 8,
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   660
	(1 << TRACKDIR_Y_NW),                        ///< 9
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   661
	0,                                           ///< 10
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   662
	0,                                           ///< 11,
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   663
	(1 << TRACKDIR_X_NE),                        ///< 12
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   664
	0,                                           ///< 13
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   665
	0,                                           ///< 14
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   666
};
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   667
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   668
static uint DistanceMoo(TileIndex t0, TileIndex t1)
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   669
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   670
	const uint dx = delta(TileX(t0), TileX(t1));
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   671
	const uint dy = delta(TileY(t0), TileY(t1));
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   672
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   673
	const uint straightTracks = 2 * min(dx, dy); // The number of straight (not full length) tracks
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   674
	/* OPTIMISATION:
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   675
	 * Original: diagTracks = max(dx, dy) - min(dx,dy);
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   676
	 * Proof:
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   677
	 * (dx-dy) - straightTracks  == (min + max) - straightTracks = min + // max - 2 * min = max - min */
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   678
	const uint diagTracks = dx + dy - straightTracks; // The number of diagonal (full tile length) tracks.
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   679
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   680
	return diagTracks*DIAG_FACTOR + straightTracks*STR_FACTOR;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   681
}
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   682
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   683
/* These has to be small cause the max length of a track
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   684
 * is currently limited to 16384 */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   685
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   686
static const byte _length_of_track[16] = {
4344
5d0e40cd67b9 (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
   687
	DIAG_FACTOR, DIAG_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, 0, 0,
5d0e40cd67b9 (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
   688
	DIAG_FACTOR, DIAG_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, 0, 0
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   689
};
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   690
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   691
/* new more optimized pathfinder for trains...
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   692
 * Tile is the tile the train is at.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   693
 * direction is the tile the train is moving towards. */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   694
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3132
diff changeset
   695
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
   696
{
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   697
	TrackBits bits, allbits;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   698
	Trackdir track;
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   699
	TileIndex tile_org;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   700
	StackedItem si;
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   701
	int estimation;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   702
2261
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2186
diff changeset
   703
2136
2c9fda706e52 (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
   704
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   705
	/* Need to have a special case for the start.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   706
	 * We shouldn't call the callback for the current tile. */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   707
	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
   708
	si.depth = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   709
	si.state = 0;
2136
2c9fda706e52 (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
   710
	si.first_track = 0xFF;
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   711
	goto start_at;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   712
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2782
diff changeset
   713
	for (;;) {
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   714
		/* Get the next item to search from from the priority queue */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   715
		do {
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   716
			if (tpf->nstack == 0)
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   717
				return; // nothing left? then we're done!
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   718
			si = tpf->stack[0];
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   719
			tile = si.tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   720
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   721
			HeapifyDown(tpf);
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   722
			/* Make sure we havn't already visited this tile. */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   723
		} while (!NtpCheck(tpf, tile, _tpf_prev_direction[si.track], si.cur_length));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   724
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   725
		/* Add the length of this track. */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   726
		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
   727
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   728
callback_and_continue:
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   729
		if (tpf->enum_proc(tile, tpf->userdata, si.first_track, si.cur_length))
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   730
			return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   731
2136
2c9fda706e52 (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
   732
		assert(si.track <= 13);
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   733
		direction = _tpf_new_direction[si.track];
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   734
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   735
start_at:
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   736
		/* If the tile is the entry tile of a tunnel, and we're not going out of the tunnel,
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   737
		 *   need to find the exit of the tunnel. */
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   738
		if (IsTileType(tile, MP_TUNNELBRIDGE)) {
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   739
			if (IsTunnel(tile)) {
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   740
				if (GetTunnelDirection(tile) != ReverseDiagDir(direction)) {
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   741
					FindLengthOfTunnelResult flotr;
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   742
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   743
					/* We are not just driving out of the tunnel */
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   744
					if (GetTunnelDirection(tile) != direction ||
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   745
							GetTunnelTransportType(tile) != tpf->tracktype) {
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   746
						/* We are not driving into the tunnel, or it is an invalid tunnel */
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   747
						continue;
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   748
					}
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8329
diff changeset
   749
					if (!HasBit(tpf->railtypes, GetRailType(tile))) {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   750
						bits = TRACK_BIT_NONE;
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   751
						break;
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   752
					}
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   753
					flotr = FindLengthOfTunnel(tile, direction);
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   754
					si.cur_length += flotr.length * DIAG_FACTOR;
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   755
					tile = flotr.tile;
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   756
					/* tile now points to the exit tile of the tunnel */
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   757
				}
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   758
			} else {
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   759
				TileIndex tile_end;
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   760
				if (GetBridgeRampDirection(tile) != ReverseDiagDir(direction)) {
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   761
					/* We are not just leaving the bridge */
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   762
					if (GetBridgeRampDirection(tile) != direction ||
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   763
							GetBridgeTransportType(tile) != tpf->tracktype) {
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   764
						/* Not entering the bridge or not compatible */
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   765
						continue;
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   766
					}
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   767
				}
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   768
				tile_end = GetOtherBridgeEnd(tile);
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   769
				si.cur_length += DistanceManhattan(tile, tile_end) * DIAG_FACTOR;
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   770
				tile = tile_end;
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   771
			}
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   772
		}
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   773
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   774
		/* This is a special loop used to go through
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   775
		 * a rail net and find the first intersection */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   776
		tile_org = tile;
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2782
diff changeset
   777
		for (;;) {
2136
2c9fda706e52 (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
   778
			assert(direction <= 3);
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4406
diff changeset
   779
			tile += TileOffsByDiagDir(direction);
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   780
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   781
			/* too long search length? bail out. */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   782
			if (si.cur_length >= tpf->maxlength) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5028
diff changeset
   783
				DEBUG(ntp, 1, "Cur_length too big");
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   784
				bits = TRACK_BIT_NONE;
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   785
				break;
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   786
			}
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   787
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   788
			/* Not a regular rail tile?
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   789
			 * Then we can't use the code below, but revert to more general code. */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   790
			if (!IsTileType(tile, MP_RAILWAY) || !IsPlainRailTile(tile)) {
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   791
				/* We found a tile which is not a normal railway tile.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   792
				 * Determine which tracks that exist on this tile. */
7179
3e123c2b7c93 (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6987
diff changeset
   793
				uint32 ts = GetTileTrackStatus(tile, TRANSPORT_RAIL, 0) & _tpfmode1_and[direction];
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   794
				bits = TrackdirBitsToTrackBits((TrackdirBits)(ts & TRACKDIR_BIT_MASK));
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   795
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   796
				/* Check that the tile contains exactly one track */
8329
77e50bd6ad67 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 7577
diff changeset
   797
				if (bits == 0 || KillFirstBit(bits) != 0) break;
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   798
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8329
diff changeset
   799
				if (!HasBit(tpf->railtypes, GetRailType(tile))) {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   800
					bits = TRACK_BIT_NONE;
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   801
					break;
3804
13ee0f2f5cc9 (svn r4812) -Fix (FS#161) NTP properly checks for railtypes on non-plain-rail-tiles (Rubidium)
celestar
parents: 3735
diff changeset
   802
				}
13ee0f2f5cc9 (svn r4812) -Fix (FS#161) NTP properly checks for railtypes on non-plain-rail-tiles (Rubidium)
celestar
parents: 3735
diff changeset
   803
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   804
				/*******************
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   805
				 * If we reach here, the tile has exactly one track.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   806
				 *   tile - index to a tile that is not rail tile, but still straight (with optional signals)
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   807
				 *   bits - bitmask of which track that exist on the tile (exactly one bit is set)
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   808
				 *   direction - which direction are we moving in?
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   809
				 *******************/
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   810
				si.track = _new_trackdir[FIND_FIRST_BIT(bits)][direction];
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   811
				si.cur_length += _length_of_track[si.track];
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   812
				goto callback_and_continue;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   813
			}
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   814
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   815
			/* Regular rail tile, determine which tracks exist. */
3267
591027d10884 (svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
tron
parents: 3234
diff changeset
   816
			allbits = GetTrackBits(tile);
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   817
			/* Which tracks are reachable? */
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   818
			bits = allbits & DiagdirReachesTracks(direction);
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   819
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   820
			/* The tile has no reachable tracks => End of rail segment
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   821
			 * or Intersection => End of rail segment. We check this agains all the
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   822
			 * bits, not just reachable ones, to prevent infinite loops. */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   823
			if (bits == TRACK_BIT_NONE || TracksOverlap(allbits)) break;
2137
0e686b34c3a9 (svn r2647) Fix: [ntp] Fix assertion error introduced in r2635
ludde
parents: 2136
diff changeset
   824
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8329
diff changeset
   825
			if (!HasBit(tpf->railtypes, GetRailType(tile))) {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   826
				bits = TRACK_BIT_NONE;
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3267
diff changeset
   827
				break;
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3267
diff changeset
   828
			}
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3267
diff changeset
   829
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   830
			/* If we reach here, the tile has exactly one track, and this
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6949
diff changeset
   831
			 track is reachable = > Rail segment continues */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   832
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   833
			track = _new_trackdir[FIND_FIRST_BIT(bits)][direction];
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   834
			assert(track != INVALID_TRACKDIR);
2137
0e686b34c3a9 (svn r2647) Fix: [ntp] Fix assertion error introduced in r2635
ludde
parents: 2136
diff changeset
   835
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   836
			si.cur_length += _length_of_track[track];
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   837
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   838
			/* Check if this rail is an upwards slope. If it is, then add a penalty.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   839
			 * Small optimization here.. if (track&7)>1 then it can't be a slope so we avoid calling GetTileSlope */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   840
			if ((track & 7) <= 1 && (_is_upwards_slope[GetTileSlope(tile, NULL)] & (1 << track)) ) {
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   841
				// upwards slope. add some penalty.
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   842
				si.cur_length += 4 * DIAG_FACTOR;
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   843
			}
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   844
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   845
			/* railway tile with signals..? */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   846
			if (HasSignals(tile)) {
4631
3005b7b2e18c (svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents: 4559
diff changeset
   847
				if (!HasSignalOnTrackdir(tile, track)) {
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   848
					/* if one way signal not pointing towards us, stop going in this direction => End of rail segment. */
4631
3005b7b2e18c (svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents: 4559
diff changeset
   849
					if (HasSignalOnTrackdir(tile, ReverseTrackdir(track))) {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   850
						bits = TRACK_BIT_NONE;
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   851
						break;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   852
					}
4631
3005b7b2e18c (svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents: 4559
diff changeset
   853
				} else if (GetSignalStateByTrackdir(tile, track) == SIGNAL_STATE_GREEN) {
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   854
					/* green signal in our direction. either one way or two way. */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   855
					si.state |= 3;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   856
				} else {
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   857
					/* reached a red signal. */
4631
3005b7b2e18c (svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents: 4559
diff changeset
   858
					if (HasSignalOnTrackdir(tile, ReverseTrackdir(track))) {
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   859
						/* two way red signal. unless we passed another green signal on the way,
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   860
						 * stop going in this direction => End of rail segment.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   861
						 * this is to prevent us from going into a full platform. */
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   862
						if (!(si.state & 1)) {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   863
							bits = TRACK_BIT_NONE;
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   864
							break;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   865
						}
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   866
					}
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   867
					if (!(si.state & 2)) {
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   868
						/* Is this the first signal we see? And it's red... add penalty */
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   869
						si.cur_length += 10 * DIAG_FACTOR;
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   870
						si.state += 2; // remember that we added penalty.
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   871
						/* Because we added a penalty, we can't just continue as usual.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   872
						 * Need to get out and let A* do it's job with
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   873
						 * possibly finding an even shorter path. */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   874
						break;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   875
					}
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   876
				}
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   877
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   878
				if (tpf->enum_proc(tile, tpf->userdata, si.first_track, si.cur_length))
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   879
					return; // Don't process this tile any further
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   880
			}
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   881
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   882
			/* continue with the next track */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   883
			direction = _tpf_new_direction[track];
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   884
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   885
			/* safety check if we're running around chasing our tail... (infinite loop) */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   886
			if (tile == tile_org) {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   887
				bits = TRACK_BIT_NONE;
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   888
				break;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   889
			}
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   890
		}
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   891
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   892
		/* There are no tracks to choose between.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   893
		 * Stop searching in this direction */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   894
		if (bits == TRACK_BIT_NONE)
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   895
			continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   896
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   897
		/****************
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   898
		 * We got multiple tracks to choose between (intersection).
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   899
		 * Branch the search space into several branches.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   900
		 ****************/
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   901
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   902
		/* Check if we've already visited this intersection.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   903
		 * If we've already visited it with a better length, then
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   904
		 * there's no point in visiting it again. */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   905
		if (!NtpVisit(tpf, tile, direction, si.cur_length))
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   906
			continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   907
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   908
		/* Push all possible alternatives that we can reach from here
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   909
		 * onto the priority heap.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   910
		 * 'bits' contains the tracks that we can choose between. */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   911
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   912
		/* First compute the estimated distance to the target.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   913
		 * This is used to implement A* */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   914
		estimation = 0;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   915
		if (tpf->dest != 0)
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   916
			estimation = DistanceMoo(tile, tpf->dest);
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   917
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   918
		si.depth++;
2774
4b7d8beec975 (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
   919
		if (si.depth == 0)
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   920
			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
   921
		si.tile = tile;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   922
		while (bits != TRACK_BIT_NONE) {
5849
58039c9dc565 (svn r8052) - Codechange: RemoveFirstTrack() and RemoveFirstTrackdir() now accept pointer to TrackBits/TrackdirBits instead of reference.
KUDr
parents: 5838
diff changeset
   923
			Track track = RemoveFirstTrack(&bits);
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   924
			si.track = _new_trackdir[track][direction];
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   925
			assert(si.track != 0xFF);
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   926
			si.priority = si.cur_length + estimation;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   927
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   928
			/* out of stack items, bail out? */
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   929
			if (tpf->nstack >= lengthof(tpf->stack)) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5028
diff changeset
   930
				DEBUG(ntp, 1, "Out of stack");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   931
				break;
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   932
			}
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   933
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   934
			tpf->stack[tpf->nstack] = si;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   935
			HeapifyUp(tpf);
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   936
		};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   937
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   938
		/* If this is the first intersection, we need to fill the first_track member.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   939
		 * so the code outside knows which path is better.
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   940
		 * 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
   941
		if (si.depth == 1) {
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   942
			assert(tpf->nstack == 1 || tpf->nstack == 2 || tpf->nstack == 3);
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   943
			if (tpf->nstack != 1) {
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   944
				uint32 r = Random();
5984
fbef81292ff9 (svn r8276) -Fix
tron
parents: 5849
diff changeset
   945
				if (r & 1) Swap(tpf->stack[0].track, tpf->stack[1].track);
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   946
				if (tpf->nstack != 2) {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   947
					TrackdirByte t = tpf->stack[2].track;
5984
fbef81292ff9 (svn r8276) -Fix
tron
parents: 5849
diff changeset
   948
					if (r & 2) Swap(tpf->stack[0].track, t);
fbef81292ff9 (svn r8276) -Fix
tron
parents: 5849
diff changeset
   949
					if (r & 4) Swap(tpf->stack[1].track, t);
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   950
					tpf->stack[2].first_track = tpf->stack[2].track = t;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   951
				}
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   952
				tpf->stack[0].first_track = tpf->stack[0].track;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   953
				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
   954
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   955
		}
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   956
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   957
		/* Continue with the next from the queue... */
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   958
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   959
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   960
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   961
6678
6353b8865d42 (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6574
diff changeset
   962
/** new pathfinder for trains. better and faster. */
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3267
diff changeset
   963
void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypeMask railtypes, DiagDirection direction, NTPEnumProc* enum_proc, void* data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   964
{
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   965
	NewTrackPathFinder tpf;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   966
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   967
	tpf.dest = dest;
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   968
	tpf.userdata = data;
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   969
	tpf.enum_proc = enum_proc;
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3267
diff changeset
   970
	tpf.tracktype = TRANSPORT_RAIL;
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3267
diff changeset
   971
	tpf.railtypes = railtypes;
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   972
	tpf.maxlength = min(_patches.pf_maxlength * 3, 10000);
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   973
	tpf.nstack = 0;
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   974
	tpf.new_link = tpf.links;
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   975
	tpf.num_links_left = lengthof(tpf.links);
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   976
	memset(tpf.hash_head, 0, sizeof(tpf.hash_head));
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   977
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   978
	NTPEnum(&tpf, tile, direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   979
}