pathfind.c
author KUDr
Tue, 02 Jan 2007 18:40:37 +0000
branchcustombridgeheads
changeset 5641 d4d00a16ef26
parent 5635 58d96074685e
permissions -rw-r--r--
(svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
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
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     3
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1209
diff changeset
     4
#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
     5
#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
     6
#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
     7
#include "depot.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2153
diff changeset
     8
#include "functions.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
     9
#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
    10
#include "tile.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
#include "pathfind.h"
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
    12
#include "rail.h"
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
    13
#include "debug.h"
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
    14
#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
    15
#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
    16
#include "depot.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
// 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
    19
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
    20
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
	uint hash, val, offs;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
	TrackPathFinderLink *link, *new_link;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
	uint bits = 1 << dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
	if (tpf->disable_tile_hash)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    26
		return true;
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
	hash = PATHFIND_HASH_TILE(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    29
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    30
	val = tpf->hash_head[hash];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    31
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    32
	if (val == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    33
		/* 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
    34
		 * to indicate that a bit was set. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    35
		tpf->hash_head[hash] = bits;
1986
5dd3db2b86d7 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
    36
		tpf->hash_tile[hash] = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
	} else if (!(val & 0x8000)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
		/* single tile */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
1986
5dd3db2b86d7 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
    41
		if (tile == tpf->hash_tile[hash]) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
			/* found another bit for the same tile,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    43
			 * 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
    44
			if (val & bits)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
				return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
			/* 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
    48
			 * was set */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    49
			tpf->hash_head[hash] = val | bits;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
			/* 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
    53
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
			/* 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
    55
			 * 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
    56
			if (tpf->num_links_left == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    57
				return false;
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
    58
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
			tpf->num_links_left--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    60
			link = tpf->new_link++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    61
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
			/* 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
    63
			 * 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
    64
			link->tile = tpf->hash_tile[hash];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    65
			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
    66
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    67
			link->flags = tpf->hash_head[hash];
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
    68
			tpf->hash_head[hash] = 0xFFFF; /* multi link */
0
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->next = 0xFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    71
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
		/* a linked list of many tiles,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    74
		 * 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
    75
		 * otherwise make a new link */
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
    76
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
		offs = tpf->hash_tile[hash];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
			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
    80
			if (tile == link->tile) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    81
				/* found the tile in the link list,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    82
				 * 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
    83
				 * bit was already set */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    84
				if (link->flags & bits)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    85
					return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    86
				link->flags |= bits;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    87
				return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    88
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    89
		} while ((offs=link->next) != 0xFFFF);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
    91
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    92
	/* 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
    93
	 * 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
    94
	if (tpf->num_links_left == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    95
			return false;
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
    96
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    97
	tpf->num_links_left--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    98
	new_link = tpf->new_link++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    99
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   100
	/* 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
   101
	 * link to the new one */
1986
5dd3db2b86d7 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   102
	new_link->tile = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   103
	new_link->flags = bits;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   104
	new_link->next = 0xFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   105
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
	link->next = PATHFIND_GET_LINK_OFFS(tpf, new_link);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   107
	return true;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
static const byte _bits_mask[4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
	0x19,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   112
	0x16,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
	0x25,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   114
	0x2A,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   115
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   116
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   117
static const byte _tpf_new_direction[14] = {
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
   118
	0, 1, 0, 1, 2, 1,
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
   119
	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
   120
	2, 3, 3, 2, 3, 0,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
static const byte _tpf_prev_direction[14] = {
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
   124
	0, 1, 1, 0, 1, 2,
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
   125
	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
   126
	2, 3, 2, 3, 0, 3,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   128
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   129
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
static const byte _otherdir_mask[4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
	0x10,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
	0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   133
	0x5,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   134
	0x2A,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   135
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   136
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3132
diff changeset
   137
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
   138
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
	uint bits;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   140
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   141
	RememberData rd;
747
e795750b96de (svn r1203) -Fix: the pathfinder no longer sees rail with an other owner as a
truelight
parents: 679
diff changeset
   142
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
   143
	assert(tpf->tracktype == TRANSPORT_WATER);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   144
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   145
	// This addition will sometimes overflow by a single tile.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   146
	// The use of TILE_MASK here makes sure that we still point at a valid
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   147
	// 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
   148
	tile = TILE_MASK(tile + TileOffsByDiagDir(direction));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   149
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   150
	if (++tpf->rd.cur_length > 50)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   151
		return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   152
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   153
	bits = GetTileTrackStatus(tile, tpf->tracktype);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   154
	bits = (byte)((bits | (bits >> 8)) & _bits_mask[direction]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   155
	if (bits == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   156
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   157
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
   158
	assert(TileX(tile) != MapMaxX() && TileY(tile) != MapMaxY());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   159
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   160
	if ( (bits & (bits - 1)) == 0 ) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
		/* only one direction */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
		i = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   163
		while (!(bits&1))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
			i++, bits>>=1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   166
		rd = tpf->rd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   167
		goto continue_here;
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
	/* several directions */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
	i=0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   171
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   172
		if (!(bits & 1)) continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   173
		rd = tpf->rd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   174
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   175
		// Change direction 4 times only
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   176
		if ((byte)i != tpf->rd.pft_var6) {
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2782
diff changeset
   177
			if (++tpf->rd.depth > 4) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   178
				tpf->rd = rd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   179
				return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   180
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   181
			tpf->rd.pft_var6 = (byte)i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
		}
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
continue_here:;
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3977
diff changeset
   185
		tpf->the_dir = 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
   186
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   187
		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
   188
			TPFMode2(tpf, tile, _tpf_new_direction[tpf->the_dir]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   189
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   190
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   191
		tpf->rd = rd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   192
	} while (++i, bits>>=1);
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
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   195
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   196
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
   197
/* 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
   198
 * 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
   199
 */
3420
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   200
FindLengthOfTunnelResult FindLengthOfTunnel(TileIndex tile, DiagDirection dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
{
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4406
diff changeset
   202
	TileIndexDiff delta = TileOffsByDiagDir(dir);
3420
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   203
	uint z = GetTileZ(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
	FindLengthOfTunnelResult flotr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   205
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   206
	flotr.length = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   207
3420
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   208
	dir = ReverseDiagDir(dir);
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   209
	do {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   210
		flotr.length++;
3420
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   211
		tile += delta;
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   212
	} while(
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   213
		!IsTunnelTile(tile) ||
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   214
		GetTunnelDirection(tile) != dir ||
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   215
		GetTileZ(tile) != z
85a5201aeb14 (svn r4245) Simplify FindLengthOfTunnel()
tron
parents: 3355
diff changeset
   216
	);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   217
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   218
	flotr.tile = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
	return flotr;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   222
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
   223
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
   224
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
   225
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   226
	FindLengthOfTunnelResult flotr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   227
	TPFSetTileBit(tpf, tile, 14);
159
139cf78bfb28 (svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents: 148
diff changeset
   228
	flotr = FindLengthOfTunnel(tile, direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   229
	tpf->rd.cur_length += flotr.length;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   230
	TPFSetTileBit(tpf, flotr.tile, 14);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   231
	return flotr.tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   232
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
const byte _ffb_64[128] = {
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
   235
 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
   236
 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
   237
 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
   238
 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
   239
 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
   240
 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
   241
 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
   242
 3,  0,  1,  0,  2,  0,  1,  0,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   243
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
   244
 0,  0,  0,  2,  0,  4,  4,  6,
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
 0,  8,  8, 10,  8, 12, 12, 14,
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
   246
 0, 16, 16, 18, 16, 20, 20, 22,
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
   247
16, 24, 24, 26, 24, 28, 28, 30,
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
   248
 0, 32, 32, 34, 32, 36, 36, 38,
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
   249
32, 40, 40, 42, 40, 44, 44, 46,
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
   250
32, 48, 48, 50, 48, 52, 52, 54,
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
   251
48, 56, 56, 58, 56, 60, 60, 62,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   252
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   253
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3132
diff changeset
   254
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
   255
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
	uint bits;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   257
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   258
	RememberData rd;
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1901
diff changeset
   259
	TileIndex tile_org = tile;
5631
932c1a268feb (svn r7737) [cbh] - Fix: signal updates are now propagated through cbh
KUDr
parents: 5623
diff changeset
   260
	bool is_bridge = false;
5635
58d96074685e (svn r7744) [cbh] - Fix: signal update didn't work when new tunnel built (peter1138)
KUDr
parents: 5631
diff changeset
   261
	bool is_tunnel = false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   262
5635
58d96074685e (svn r7744) [cbh] - Fix: signal update didn't work when new tunnel built (peter1138)
KUDr
parents: 5631
diff changeset
   263
	if (IsTileType(tile, MP_TUNNEL) && GetTunnelDirection(tile) == direction) {
58d96074685e (svn r7744) [cbh] - Fix: signal update didn't work when new tunnel built (peter1138)
KUDr
parents: 5631
diff changeset
   264
		if (GetTunnelTransportType(tile) != tpf->tracktype) {
5590
dc34c43fc3eb (svn r7597) [cbh] - Codechange: Remove MP_TUNNELBRIDGE and introduce three new TileTypes: MP_TUNNEL, MP_STREET_BRIDGE, MP_RAILWAY_BRIDGE
celestar
parents: 5573
diff changeset
   265
			return;
2493
d834d0c1502a (svn r3019) -Codechange: Replace explicit shifting/anding/oring with GB and SB
tron
parents: 2486
diff changeset
   266
		}
5590
dc34c43fc3eb (svn r7597) [cbh] - Codechange: Remove MP_TUNNELBRIDGE and introduce three new TileTypes: MP_TUNNEL, MP_STREET_BRIDGE, MP_RAILWAY_BRIDGE
celestar
parents: 5573
diff changeset
   267
		tile = SkipToEndOfTunnel(tpf, tile, direction);
5635
58d96074685e (svn r7744) [cbh] - Fix: signal update didn't work when new tunnel built (peter1138)
KUDr
parents: 5631
diff changeset
   268
		is_tunnel = true;
58d96074685e (svn r7744) [cbh] - Fix: signal update didn't work when new tunnel built (peter1138)
KUDr
parents: 5631
diff changeset
   269
	} else if ((IsTileType(tile, MP_STREET_BRIDGE) || IsTileType(tile, MP_RAILWAY_BRIDGE)) && GetBridgeRampDirection(tile) == direction) {
5590
dc34c43fc3eb (svn r7597) [cbh] - Codechange: Remove MP_TUNNELBRIDGE and introduce three new TileTypes: MP_TUNNEL, MP_STREET_BRIDGE, MP_RAILWAY_BRIDGE
celestar
parents: 5573
diff changeset
   270
		TileIndex tile_end;
5631
932c1a268feb (svn r7737) [cbh] - Fix: signal updates are now propagated through cbh
KUDr
parents: 5623
diff changeset
   271
		if (GetBridgeTransportType(tile) != tpf->tracktype) {
5590
dc34c43fc3eb (svn r7597) [cbh] - Codechange: Remove MP_TUNNELBRIDGE and introduce three new TileTypes: MP_TUNNEL, MP_STREET_BRIDGE, MP_RAILWAY_BRIDGE
celestar
parents: 5573
diff changeset
   272
			return;
dc34c43fc3eb (svn r7597) [cbh] - Codechange: Remove MP_TUNNELBRIDGE and introduce three new TileTypes: MP_TUNNEL, MP_STREET_BRIDGE, MP_RAILWAY_BRIDGE
celestar
parents: 5573
diff changeset
   273
		}
dc34c43fc3eb (svn r7597) [cbh] - Codechange: Remove MP_TUNNELBRIDGE and introduce three new TileTypes: MP_TUNNEL, MP_STREET_BRIDGE, MP_RAILWAY_BRIDGE
celestar
parents: 5573
diff changeset
   274
		//fprintf(stderr, "%s: Planning over bridge\n", __func__);
dc34c43fc3eb (svn r7597) [cbh] - Codechange: Remove MP_TUNNELBRIDGE and introduce three new TileTypes: MP_TUNNEL, MP_STREET_BRIDGE, MP_RAILWAY_BRIDGE
celestar
parents: 5573
diff changeset
   275
		// TODO doesn't work - WHAT doesn't work?
dc34c43fc3eb (svn r7597) [cbh] - Codechange: Remove MP_TUNNELBRIDGE and introduce three new TileTypes: MP_TUNNEL, MP_STREET_BRIDGE, MP_RAILWAY_BRIDGE
celestar
parents: 5573
diff changeset
   276
		TPFSetTileBit(tpf, tile, 14);
dc34c43fc3eb (svn r7597) [cbh] - Codechange: Remove MP_TUNNELBRIDGE and introduce three new TileTypes: MP_TUNNEL, MP_STREET_BRIDGE, MP_RAILWAY_BRIDGE
celestar
parents: 5573
diff changeset
   277
		tile_end = GetOtherBridgeEnd(tile);
dc34c43fc3eb (svn r7597) [cbh] - Codechange: Remove MP_TUNNELBRIDGE and introduce three new TileTypes: MP_TUNNEL, MP_STREET_BRIDGE, MP_RAILWAY_BRIDGE
celestar
parents: 5573
diff changeset
   278
		tpf->rd.cur_length += DistanceManhattan(tile, tile_end);
dc34c43fc3eb (svn r7597) [cbh] - Codechange: Remove MP_TUNNELBRIDGE and introduce three new TileTypes: MP_TUNNEL, MP_STREET_BRIDGE, MP_RAILWAY_BRIDGE
celestar
parents: 5573
diff changeset
   279
		tile = tile_end;
dc34c43fc3eb (svn r7597) [cbh] - Codechange: Remove MP_TUNNELBRIDGE and introduce three new TileTypes: MP_TUNNEL, MP_STREET_BRIDGE, MP_RAILWAY_BRIDGE
celestar
parents: 5573
diff changeset
   280
		TPFSetTileBit(tpf, tile, 14);
5631
932c1a268feb (svn r7737) [cbh] - Fix: signal updates are now propagated through cbh
KUDr
parents: 5623
diff changeset
   281
		is_bridge = true;
932c1a268feb (svn r7737) [cbh] - Fix: signal updates are now propagated through cbh
KUDr
parents: 5623
diff changeset
   282
	} else {
932c1a268feb (svn r7737) [cbh] - Fix: signal updates are now propagated through cbh
KUDr
parents: 5623
diff changeset
   283
		tile += TileOffsByDiagDir(direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   284
	}
747
e795750b96de (svn r1203) -Fix: the pathfinder no longer sees rail with an other owner as a
truelight
parents: 679
diff changeset
   285
e795750b96de (svn r1203) -Fix: the pathfinder no longer sees rail with an other owner as a
truelight
parents: 679
diff changeset
   286
	/* Check in case of rail if the owner is the same */
752
df3e096dd9db (svn r1208) -Fix: the owner-check introduced in r1203 now also works correctly for
truelight
parents: 747
diff changeset
   287
	if (tpf->tracktype == TRANSPORT_RAIL) {
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
   288
		// don't enter train depot from the back
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
   289
		if (IsTileDepotType(tile, TRANSPORT_RAIL) && GetRailDepotDirection(tile) == direction) return;
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
   290
5590
dc34c43fc3eb (svn r7597) [cbh] - Codechange: Remove MP_TUNNELBRIDGE and introduce three new TileTypes: MP_TUNNEL, MP_STREET_BRIDGE, MP_RAILWAY_BRIDGE
celestar
parents: 5573
diff changeset
   291
		if (IsTileType(tile_org, MP_RAILWAY) || IsTileType(tile_org, MP_STATION) || IsTileType(tile_org, MP_TUNNEL) || IsTileType(tile_org, MP_RAILWAY_BRIDGE))
dc34c43fc3eb (svn r7597) [cbh] - Codechange: Remove MP_TUNNELBRIDGE and introduce three new TileTypes: MP_TUNNEL, MP_STREET_BRIDGE, MP_RAILWAY_BRIDGE
celestar
parents: 5573
diff changeset
   292
			if (IsTileType(tile, MP_RAILWAY) || IsTileType(tile, MP_STATION) || IsTileType(tile, MP_TUNNEL) || IsTileType(tile, MP_RAILWAY_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
   293
				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
   294
	}
747
e795750b96de (svn r1203) -Fix: the pathfinder no longer sees rail with an other owner as a
truelight
parents: 679
diff changeset
   295
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3894
diff changeset
   296
	// check if the new tile can be entered from that direction
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3894
diff changeset
   297
	if (tpf->tracktype == TRANSPORT_ROAD) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3894
diff changeset
   298
		// road stops and depots now have a track (r4419)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3894
diff changeset
   299
		// don't enter road stop from the back
5028
fef7184a6b6c (svn r7069) -Fix: AI tried to build road from the back or side of road stop/depot (peter1138)
KUDr
parents: 4631
diff changeset
   300
		if (IsRoadStopTile(tile) && ReverseDiagDir(GetRoadStopDir(tile)) != direction) return;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3894
diff changeset
   301
		// don't enter road depot from the back
5028
fef7184a6b6c (svn r7069) -Fix: AI tried to build road from the back or side of road stop/depot (peter1138)
KUDr
parents: 4631
diff changeset
   302
		if (IsTileDepotType(tile, TRANSPORT_ROAD) && ReverseDiagDir(GetRoadDepotDirection(tile)) != direction) return;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3894
diff changeset
   303
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3894
diff changeset
   304
5623
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5590
diff changeset
   305
	/* Check if the new tile is a tunnel or bridge head and that the direction
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5590
diff changeset
   306
	 * and transport type match */
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5590
diff changeset
   307
	if (IsTunnelTile(tile)) {
5635
58d96074685e (svn r7744) [cbh] - Fix: signal update didn't work when new tunnel built (peter1138)
KUDr
parents: 5631
diff changeset
   308
		if ((!is_tunnel && GetTunnelDirection(tile) != direction) ||
5623
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5590
diff changeset
   309
				GetTunnelTransportType(tile) != tpf->tracktype) {
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5590
diff changeset
   310
			return;
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5590
diff changeset
   311
		}
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5590
diff changeset
   312
	}
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5590
diff changeset
   313
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5590
diff changeset
   314
	if (IsBridgeTile(tile)) {
5631
932c1a268feb (svn r7737) [cbh] - Fix: signal updates are now propagated through cbh
KUDr
parents: 5623
diff changeset
   315
		if ((!is_bridge && GetBridgeRampDirection(tile) == ReverseDiagDir(direction)) ||
5623
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5590
diff changeset
   316
				GetBridgeTransportType(tile) != tpf->tracktype) {
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5590
diff changeset
   317
			return;
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5590
diff changeset
   318
		}
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5590
diff changeset
   319
	}
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5590
diff changeset
   320
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   321
	tpf->rd.cur_length++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   322
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   323
	bits = GetTileTrackStatus(tile, tpf->tracktype);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   324
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   325
	if ((byte)bits != tpf->var2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   326
		bits &= _tpfmode1_and[direction];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   327
		bits = bits | (bits>>8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   328
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   329
	bits &= 0xBF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   330
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   331
	if (bits != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   332
		if (!tpf->disable_tile_hash || (tpf->rd.cur_length <= 64 && (KILL_FIRST_BIT(bits) == 0 || ++tpf->rd.depth <= 7))) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   333
			do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   334
				i = FIND_FIRST_BIT(bits);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   335
				bits = KILL_FIRST_BIT(bits);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   336
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   337
				tpf->the_dir = (_otherdir_mask[direction] & (byte)(1 << i)) ? (i+8) : i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   338
				rd = tpf->rd;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   339
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   340
				if (TPFSetTileBit(tpf, tile, tpf->the_dir) &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   341
						!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
   342
					TPFMode1(tpf, tile, _tpf_new_direction[tpf->the_dir]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   343
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   344
				tpf->rd = rd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   345
			} while (bits != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   346
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   347
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   348
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   349
	/* the next is only used when signals are checked.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   350
	 * 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
   351
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   352
	/* 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
   353
	 * stack space dramatically. */
2006
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1986
diff changeset
   354
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1986
diff changeset
   355
	/* 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
   356
	 * 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
   357
	 * 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
   358
	if (tpf->hasbit_13)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   359
		return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   360
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3132
diff changeset
   361
	direction = ReverseDiagDir(direction);
5631
932c1a268feb (svn r7737) [cbh] - Fix: signal updates are now propagated through cbh
KUDr
parents: 5623
diff changeset
   362
	//tile += TileOffsByDiagDir(direction);
932c1a268feb (svn r7737) [cbh] - Fix: signal updates are now propagated through cbh
KUDr
parents: 5623
diff changeset
   363
	tile = tile_org;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   364
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   365
	bits = GetTileTrackStatus(tile, tpf->tracktype);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   366
	bits |= (bits >> 8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   367
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   368
	if ( (byte)bits != tpf->var2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
		bits &= _bits_mask[direction];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   370
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   371
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   372
	bits &= 0xBF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   373
	if (bits == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   375
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   377
		i = FIND_FIRST_BIT(bits);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   378
		bits = KILL_FIRST_BIT(bits);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   379
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   380
		tpf->the_dir = (_otherdir_mask[direction] & (byte)(1 << i)) ? (i+8) : i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
		rd = tpf->rd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
		if (TPFSetTileBit(tpf, tile, tpf->the_dir) &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   383
				!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
   384
			TPFMode1(tpf, tile, _tpf_new_direction[tpf->the_dir]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
		tpf->rd = rd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   387
	} while (bits != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   389
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3132
diff changeset
   390
void FollowTrack(TileIndex tile, uint16 flags, 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
   391
{
318
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   392
	TrackPathFinder tpf;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   393
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   394
	assert(direction < 4);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   395
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   396
	/* initialize path finder variables */
318
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   397
	tpf.userdata = data;
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   398
	tpf.enum_proc = enum_proc;
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   399
	tpf.new_link = tpf.links;
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   400
	tpf.num_links_left = lengthof(tpf.links);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   401
318
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   402
	tpf.rd.cur_length = 0;
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   403
	tpf.rd.depth = 0;
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   404
	tpf.rd.pft_var6 = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   405
318
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   406
	tpf.var2 = HASBIT(flags, 15) ? 0x43 : 0xFF; /* 0x8000 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   407
3132
724ede39bda9 (svn r3747) Change HASBIT() to return 0/1 instead of 0/value of tested bit, because the name suggests it does the former and current behavior broke in some places in very subtle ways (for example HASBIT(x, 0) != HASBIT(y, 1) doesn't work, returning a bool after HASBIT(x, 9) neither)
tron
parents: 3102
diff changeset
   408
	tpf.disable_tile_hash = HASBIT(flags, 12);  /* 0x1000 */
724ede39bda9 (svn r3747) Change HASBIT() to return 0/1 instead of 0/value of tested bit, because the name suggests it does the former and current behavior broke in some places in very subtle ways (for example HASBIT(x, 0) != HASBIT(y, 1) doesn't work, returning a bool after HASBIT(x, 9) neither)
tron
parents: 3102
diff changeset
   409
	tpf.hasbit_13         = HASBIT(flags, 13);  /* 0x2000 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   411
318
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   412
	tpf.tracktype = (byte)flags;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   413
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   414
	if (HASBIT(flags, 11)) {
318
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   415
		tpf.rd.pft_var6 = 0xFF;
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   416
		tpf.enum_proc(tile, data, 0, 0, 0);
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   417
		TPFMode2(&tpf, tile, direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   418
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   419
		/* clear the hash_heads */
318
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   420
		memset(tpf.hash_head, 0, sizeof(tpf.hash_head));
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   421
		TPFMode1(&tpf, tile, direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   422
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   423
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   424
	if (after_proc != NULL)
318
65ebd0cab39b (svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents: 241
diff changeset
   425
		after_proc(&tpf);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   426
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   427
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   428
typedef struct {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   429
	TileIndex tile;
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   430
	uint16 cur_length; // This is the current length to this tile.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   431
	uint16 priority; // This is the current length + estimated length to the goal.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
	byte track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   433
	byte depth;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   434
	byte state;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
	byte first_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   436
} StackedItem;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   437
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
static const byte _new_track[6][4] = {
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
   439
{0,    0xff, 8,    0xff,},
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
   440
{0xff, 1,    0xff, 9,},
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
   441
{0xff, 2,    10,   0xff,},
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
   442
{3,    0xff, 0xff, 11,},
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
   443
{12,   4,    0xff, 0xff,},
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
   444
{0xff, 0xff, 5,    13,},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   447
typedef struct HashLink {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   448
	TileIndex tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   449
	uint16 typelength;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   450
	uint16 next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   451
} HashLink;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   452
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   453
typedef struct {
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   454
	NTPEnumProc *enum_proc;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   455
	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
   456
	TileIndex dest;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   457
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
   458
	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
   459
	RailTypeMask railtypes;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   460
	uint maxlength;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   461
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   462
	HashLink *new_link;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   463
	uint num_links_left;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
979
f12f96116cdd (svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   465
	uint nstack;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   466
	StackedItem stack[256]; // priority queue of stacked items
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   467
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   468
	uint16 hash_head[0x400]; // hash heads. 0 means unused. 0xFFFC = length, 0x3 = dir
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   469
	TileIndex hash_tile[0x400]; // tiles. or links.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   470
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   471
	HashLink links[0x400]; // hash links
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   472
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   473
} NewTrackPathFinder;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   474
#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
   475
#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
   476
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   477
#define ARR(i) tpf->stack[(i)-1]
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   478
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   479
// called after a new element was added in the queue at the last index.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   480
// move it down to the proper position
536
aef4753985d3 (svn r907) Sprinkle holy ANSI water:
tron
parents: 500
diff changeset
   481
static inline void HeapifyUp(NewTrackPathFinder *tpf)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   482
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   483
	StackedItem si;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   484
	int i = ++tpf->nstack;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   485
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   486
	while (i != 1 && ARR(i).priority < ARR(i>>1).priority) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   487
		// the child element is larger than the parent item.
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   488
		// swap the child item and the parent item.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   489
		si = ARR(i); ARR(i) = ARR(i>>1); ARR(i>>1) = si;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   490
		i>>=1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   491
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   492
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   493
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   494
// 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
   495
static inline void HeapifyDown(NewTrackPathFinder *tpf)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   496
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   497
	StackedItem si;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   498
	int i = 1, j;
979
f12f96116cdd (svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   499
	int n;
f12f96116cdd (svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   500
f12f96116cdd (svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   501
	assert(tpf->nstack > 0);
f12f96116cdd (svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   502
	n = --tpf->nstack;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   503
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   504
	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
   505
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
	// copy the last item to index 0. we use it as base for heapify.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   507
	ARR(1) = ARR(n+1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   508
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   509
	while ((j=i*2) <= n) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   510
		// figure out which is smaller of the children.
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   511
		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
   512
			j++; // right item is smaller
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   513
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   514
		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
   515
		if (ARR(i).priority <= ARR(j).priority)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   516
			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
   517
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   518
		// swap parent with the child
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   519
		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
   520
		i = j;
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
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   523
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   524
// mark a tile as visited and store the length of the path.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   525
// if we already had a better path to this tile, return false.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   526
// otherwise return true.
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3132
diff changeset
   527
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
   528
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   529
	uint hash,head;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   530
	HashLink *link, *new_link;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   531
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   532
	assert(length < 16384-1);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   533
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   534
	hash = PATHFIND_HASH_TILE(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   535
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   536
	// never visited before?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   537
	if ((head=tpf->hash_head[hash]) == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   538
		tpf->hash_tile[hash] = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   539
		tpf->hash_head[hash] = dir | (length << 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   541
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   543
	if (head != 0xffff) {
1986
5dd3db2b86d7 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   544
		if (tile == tpf->hash_tile[hash] && (head & 0x3) == dir) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   545
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   546
			// longer length
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   547
			if (length >= (head >> 2)) return false;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   548
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   549
			tpf->hash_head[hash] = dir | (length << 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   550
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   551
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   552
		// two tiles with the same hash, need to make a link
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   553
		// 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
   554
		// 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
   555
		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
   556
			DEBUG(ntp, 1, "No links left");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   557
			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
   558
		}
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   559
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   560
		tpf->num_links_left--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   561
		link = tpf->new_link++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   562
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   563
		/* 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
   564
		 * 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
   565
		link->tile = tpf->hash_tile[hash];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   566
		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
   567
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
		link->typelength = tpf->hash_head[hash];
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   569
		tpf->hash_head[hash] = 0xFFFF; /* multi link */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   570
		link->next = 0xFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   571
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   572
		// a linked list of many tiles,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   573
		// 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
   574
		// otherwise make a new link
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   575
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   576
		uint offs = tpf->hash_tile[hash];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   578
			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
   579
			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
   580
				if (length >= (uint)(link->typelength >> 2)) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
				link->typelength = dir | (length << 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   582
				return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
			}
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
   584
		} while ((offs = link->next) != 0xFFFF);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   585
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   586
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   587
	/* 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
   588
	 * 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
   589
	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
   590
		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
   591
		return false;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   592
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   593
	tpf->num_links_left--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   594
	new_link = tpf->new_link++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   595
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   596
	/* 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
   597
	 * link to the new one */
1986
5dd3db2b86d7 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   598
	new_link->tile = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   599
	new_link->typelength = dir | (length << 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   600
	new_link->next = 0xFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   601
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   602
	link->next = NTP_GET_LINK_OFFS(tpf, new_link);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   603
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   604
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   605
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   606
/**
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   607
 * 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
   608
 * length.
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   609
 * @return true if the length is still the same
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   610
 * @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
   611
 *         previous call to NtpVisit().
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   612
 */
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1901
diff changeset
   613
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
   614
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   615
	uint hash,head,offs;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   616
	HashLink *link;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   617
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   618
	hash = PATHFIND_HASH_TILE(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   619
	head=tpf->hash_head[hash];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   620
	assert(head);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   621
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   622
	if (head != 0xffff) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   623
		assert( tpf->hash_tile[hash] == tile && (head & 3) == dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   624
		assert( (head >> 2) <= length);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   625
		return length == (head >> 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   626
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   627
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   628
	// else it's a linked list of many tiles
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   629
	offs = tpf->hash_tile[hash];
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2782
diff changeset
   630
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
		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
   632
		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
   633
			assert((uint)(link->typelength >> 2) <= length);
a09d8f7b48be (svn r3599) -Fix: added some casts to suppress some more warnings
truelight
parents: 3017
diff changeset
   634
			return length == (uint)(link->typelength >> 2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   635
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   636
		offs = link->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
		assert(offs != 0xffff);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   638
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   639
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   640
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   641
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   642
static const uint16 _is_upwards_slope[15] = {
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   643
	0, // no tileh
3102
fde95020fc8e (svn r3697) Rename DIAG1/DIAG2 to X resp. Y as this conveys a bit better in which direction a pieces of rail goes
tron
parents: 3019
diff changeset
   644
	(1 << TRACKDIR_X_SW) | (1 << TRACKDIR_Y_NW), // 1
fde95020fc8e (svn r3697) Rename DIAG1/DIAG2 to X resp. Y as this conveys a bit better in which direction a pieces of rail goes
tron
parents: 3019
diff changeset
   645
	(1 << TRACKDIR_X_SW) | (1 << TRACKDIR_Y_SE), // 2
fde95020fc8e (svn r3697) Rename DIAG1/DIAG2 to X resp. Y as this conveys a bit better in which direction a pieces of rail goes
tron
parents: 3019
diff changeset
   646
	(1 << TRACKDIR_X_SW), // 3
fde95020fc8e (svn r3697) Rename DIAG1/DIAG2 to X resp. Y as this conveys a bit better in which direction a pieces of rail goes
tron
parents: 3019
diff changeset
   647
	(1 << TRACKDIR_X_NE) | (1 << TRACKDIR_Y_SE), // 4
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   648
	0, // 5
3102
fde95020fc8e (svn r3697) Rename DIAG1/DIAG2 to X resp. Y as this conveys a bit better in which direction a pieces of rail goes
tron
parents: 3019
diff changeset
   649
	(1 << TRACKDIR_Y_SE), // 6
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   650
	0, // 7
3102
fde95020fc8e (svn r3697) Rename DIAG1/DIAG2 to X resp. Y as this conveys a bit better in which direction a pieces of rail goes
tron
parents: 3019
diff changeset
   651
	(1 << TRACKDIR_X_NE) | (1 << TRACKDIR_Y_NW), // 8,
fde95020fc8e (svn r3697) Rename DIAG1/DIAG2 to X resp. Y as this conveys a bit better in which direction a pieces of rail goes
tron
parents: 3019
diff changeset
   652
	(1 << TRACKDIR_Y_NW), // 9
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   653
	0, //10
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   654
	0, //11,
3102
fde95020fc8e (svn r3697) Rename DIAG1/DIAG2 to X resp. Y as this conveys a bit better in which direction a pieces of rail goes
tron
parents: 3019
diff changeset
   655
	(1 << TRACKDIR_X_NE), //12
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   656
	0, //13
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   657
	0, //14
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   658
};
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   659
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   660
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
   661
{
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   662
	const uint dx = abs(TileX(t0) - TileX(t1));
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   663
	const uint dy = abs(TileY(t0) - TileY(t1));
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   664
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   665
	const uint straightTracks = 2 * min(dx, dy); /* The number of straight (not full length) tracks */
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   666
	/* OPTIMISATION:
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   667
	 * 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
   668
	 * Proof:
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   669
	 * (dx-dy) - straightTracks  == (min + max) - straightTracks = min + // max - 2 * min = max - min */
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   670
	const uint diagTracks = dx + dy - straightTracks; /* The number of diagonal (full tile length) tracks. */
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   671
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   672
	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
   673
}
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   674
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   675
// These has to be small cause the max length of a track
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   676
// is currently limited to 16384
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   677
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   678
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
   679
	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
   680
	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
   681
};
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   682
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   683
// new more optimized pathfinder for trains...
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   684
// Tile is the tile the train is at.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   685
// direction is the tile the train is moving towards.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   686
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3132
diff changeset
   687
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
   688
{
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   689
	TrackBits bits, allbits;
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   690
	uint track;
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   691
	TileIndex tile_org;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   692
	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
   693
	int estimation;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   694
2261
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2186
diff changeset
   695
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
   696
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   697
	// Need to have a special case for the start.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   698
	// We shouldn't call the callback for the current tile.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   699
	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
   700
	si.depth = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   701
	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
   702
	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
   703
	goto start_at;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   704
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2782
diff changeset
   705
	for (;;) {
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   706
		// Get the next item to search from from the priority queue
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   707
		do {
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   708
			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
   709
				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
   710
			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
   711
			tile = si.tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   712
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   713
			HeapifyDown(tpf);
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   714
			// Make sure we havn't already visited this tile.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   715
		} 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
   716
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   717
		// Add the length of this track.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   718
		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
   719
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   720
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
   721
		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
   722
			return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   723
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
   724
		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
   725
		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
   726
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   727
start_at:
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   728
		// This is a special loop used to go through
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   729
		// a rail net and find the first intersection
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   730
		tile_org = tile;
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2782
diff changeset
   731
		for (;;) {
5641
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   732
			uint16 warp_length;
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
   733
			assert(direction <= 3);
5641
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   734
			// move 'tile' forward in our 'direction'
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   735
			// tunnel/bridge can move us more than by just one tile forward if it is in our direction
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   736
			if (IsTileType(tile, MP_RAILWAY_BRIDGE) && GetBridgeRampDirection(tile) == direction) {
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   737
				// entering bridge ramp
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   738
				TileIndex end_tile;
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   739
				if (GetBridgeTransportType(tile) != tpf->tracktype) {
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   740
					// wrong track type
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   741
					bits = 0;
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   742
					break;
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   743
				}
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   744
				// pass the bridge wormhole
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   745
				end_tile = GetOtherBridgeEnd(tile);
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   746
				warp_length = DistanceManhattan(tile, end_tile) * DIAG_FACTOR;
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   747
				tile = end_tile;
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   748
			} else if (IsTileType(tile, MP_TUNNEL) && GetTunnelDirection(tile) == direction) {
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   749
				// entering tunnel
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   750
				FindLengthOfTunnelResult flotr;
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   751
				if (GetTunnelTransportType(tile) != tpf->tracktype) {
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   752
					// wrong track type
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   753
					bits = 0;
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   754
					break;
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   755
				}
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   756
				// pass the tunnel wormhole
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   757
				flotr = FindLengthOfTunnel(tile, direction);
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   758
				warp_length = flotr.length * DIAG_FACTOR;
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   759
				tile = flotr.tile;
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   760
			} else {
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   761
				// not a tunnel/bridge, move just one tile forward
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   762
				tile += TileOffsByDiagDir(direction);
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   763
				warp_length = 0;
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   764
				// disable entering bridge from under-bridge track
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   765
				if (IsTileType(tile, MP_RAILWAY_BRIDGE)) {
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   766
					// it is allowed only when we are just at the start of pathfinding (si.first_track == 0xFF)
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   767
					if (GetBridgeRampDirection(tile) == ReverseDiagDir(direction) && si.first_track != 0xFF) {
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   768
						// entered bridge ramp from its back
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   769
						bits = 0;
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   770
						break;
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   771
					}
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   772
				} else if (IsTileType(tile, MP_TUNNEL)) {
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   773
					// disable entering tunnel from regular track in the opposite direction
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   774
					if (GetTunnelDirection(tile) == ReverseDiagDir(direction)) {
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   775
						// entered tunnel from its back
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   776
						bits = 0;
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   777
						break;
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   778
					}
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   779
				}
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   780
			}
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   781
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   782
			// too long search length? bail out.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   783
			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
   784
				DEBUG(ntp, 1, "Cur_length too big");
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   785
				bits = 0;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   786
				break;
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   787
			}
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   788
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   789
			// Not a regular rail tile?
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   790
			// Then we can't use the code below, but revert to more general code.
5641
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   791
			if (!(IsTileType(tile, MP_RAILWAY) || IsTileType(tile, MP_RAILWAY_BRIDGE)) || !IsPlainRailTile(tile)) {
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   792
				// We found a tile which is not a normal railway tile or bridge ramp (cbh behaves as normal railway).
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   793
				// Determine which tracks that exist on this tile.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   794
				bits = GetTileTrackStatus(tile, TRANSPORT_RAIL) & _tpfmode1_and[direction];
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   795
				bits = (bits | (bits >> 8)) & 0x3F;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   796
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   797
				// Check that the tile contains exactly one track
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   798
				if (bits == 0 || KILL_FIRST_BIT(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
   799
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   800
				if (!HASBIT(tpf->railtypes, IsTileType(tile, MP_STREET) ? GetRailTypeCrossing(tile) : GetRailType(tile))) {
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   801
					bits = 0;
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   802
					break;
3804
13ee0f2f5cc9 (svn r4812) -Fix (FS#161) NTP properly checks for railtypes on non-plain-rail-tiles (Rubidium)
celestar
parents: 3735
diff changeset
   803
				}
13ee0f2f5cc9 (svn r4812) -Fix (FS#161) NTP properly checks for railtypes on non-plain-rail-tiles (Rubidium)
celestar
parents: 3735
diff changeset
   804
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   805
				///////////////////
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   806
				// If we reach here, the tile has exactly one track.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   807
				//   tile - index to a tile that is not rail tile, but still straight (with optional signals)
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   808
				//   bits - bitmask of which track that exist on the tile (exactly one bit is set)
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   809
				//   direction - which direction are we moving in?
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   810
				///////////////////
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   811
				si.track = _new_track[FIND_FIRST_BIT(bits)][direction];
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   812
				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
   813
				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
   814
			}
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   815
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   816
			/* 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
   817
			allbits = GetTrackBits(tile);
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   818
			/* Which tracks are reachable? */
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   819
			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
   820
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   821
			/* The tile has no reachable tracks => End of rail segment
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   822
			 * or Intersection => End of rail segment. We check this agains all the
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   823
			 * bits, not just reachable ones, to prevent infinite loops. */
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   824
			if (bits == 0 || TracksOverlap(allbits)) break;
2137
0e686b34c3a9 (svn r2647) Fix: [ntp] Fix assertion error introduced in r2635
ludde
parents: 2136
diff changeset
   825
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
   826
			if (!HASBIT(tpf->railtypes, GetRailType(tile))) {
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
				bits = 0;
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
				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
   829
			}
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
   830
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   831
			/* If we reach here, the tile has exactly one track, and this
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   832
			 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
   833
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   834
			track = _new_track[FIND_FIRST_BIT(bits)][direction];
2137
0e686b34c3a9 (svn r2647) Fix: [ntp] Fix assertion error introduced in r2635
ludde
parents: 2136
diff changeset
   835
			assert(track != 0xff);
0e686b34c3a9 (svn r2647) Fix: [ntp] Fix assertion error introduced in r2635
ludde
parents: 2136
diff changeset
   836
5641
d4d00a16ef26 (svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
KUDr
parents: 5635
diff changeset
   837
			si.cur_length += _length_of_track[track] + warp_length;
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   838
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   839
			// Check if this rail is an upwards slope. If it is, then add a penalty.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   840
			// Small optimization here.. if (track&7)>1 then it can't be a slope so we avoid calling GetTileSlope
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   841
			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
   842
				// upwards slope. add some penalty.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   843
				si.cur_length += 4*DIAG_FACTOR;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   844
			}
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   845
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   846
			// railway tile with signals..?
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   847
			if (HasSignals(tile)) {
4631
3005b7b2e18c (svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents: 4559
diff changeset
   848
				if (!HasSignalOnTrackdir(tile, track)) {
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   849
					// 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
   850
					if (HasSignalOnTrackdir(tile, ReverseTrackdir(track))) {
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   851
						bits = 0;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   852
						break;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   853
					}
4631
3005b7b2e18c (svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents: 4559
diff changeset
   854
				} else if (GetSignalStateByTrackdir(tile, track) == SIGNAL_STATE_GREEN) {
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   855
					// green signal in our direction. either one way or two way.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   856
					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
   857
				} else {
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   858
					// reached a red signal.
4631
3005b7b2e18c (svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents: 4559
diff changeset
   859
					if (HasSignalOnTrackdir(tile, ReverseTrackdir(track))) {
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   860
						// two way red signal. unless we passed another green signal on the way,
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   861
						// stop going in this direction => End of rail segment.
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   862
						// this is to prevent us from going into a full platform.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   863
						if (!(si.state&1)) {
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   864
							bits = 0;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   865
							break;
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
					}
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   868
					if (!(si.state & 2)) {
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   869
						// Is this the first signal we see? And it's red... add penalty
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   870
						si.cur_length += 10*DIAG_FACTOR;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   871
						si.state += 2; // remember that we added penalty.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   872
						// Because we added a penalty, we can't just continue as usual.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   873
						// Need to get out and let A* do it's job with
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   874
						// possibly finding an even shorter path.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   875
						break;
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
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   879
				if (tpf->enum_proc(tile, tpf->userdata, si.first_track, si.cur_length))
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2774
diff changeset
   880
					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
   881
			}
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   882
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   883
			// continue with the next track
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   884
			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
   885
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   886
			// safety check if we're running around chasing our tail... (infinite loop)
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   887
			if (tile == tile_org) {
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   888
				bits = 0;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   889
				break;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   890
			}
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   891
		}
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   892
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   893
		// There are no tracks to choose between.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   894
		// Stop searching in this direction
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   895
		if (bits == 0)
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   896
			continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   897
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   898
		////////////////
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   899
		// We got multiple tracks to choose between (intersection).
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   900
		// Branch the search space into several branches.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   901
		////////////////
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   902
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   903
		// Check if we've already visited this intersection.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   904
		// If we've already visited it with a better length, then
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   905
		// there's no point in visiting it again.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   906
		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
   907
			continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   908
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   909
		// Push all possible alternatives that we can reach from here
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   910
		// onto the priority heap.
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   911
		// 'bits' contains the tracks that we can choose between.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   912
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   913
		// First compute the estimated distance to the target.
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   914
		// This is used to implement A*
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   915
		estimation = 0;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   916
		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
   917
			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
   918
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   919
		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
   920
		if (si.depth == 0)
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
   921
			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
   922
		si.tile = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   923
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   924
			si.track = _new_track[FIND_FIRST_BIT(bits)][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
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
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);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   936
		} while ((bits = KILL_FIRST_BIT(bits)) != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   937
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2006
diff changeset
   938
		// If this is the first intersection, we need to fill the first_track member.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   939
		// so the code outside knows which path is better.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   940
		// also randomize the order in which we search through them.
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();
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   945
				if (r&1) swap_byte(&tpf->stack[0].track, &tpf->stack[1].track);
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) {
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   947
					byte t = tpf->stack[2].track;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   948
					if (r&2) swap_byte(&tpf->stack[0].track, &t);
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   949
					if (r&4) swap_byte(&tpf->stack[1].track, &t);
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
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2049
diff changeset
   957
		// Continue with the next from the queue...
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
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
}