|
|
|
@ -1,5 +1,7 @@
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/** @file pathfind.cpp */
|
|
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
#include "openttd.h"
|
|
|
|
|
#include "bridge_map.h"
|
|
|
|
@ -15,7 +17,7 @@
|
|
|
|
|
#include "variables.h"
|
|
|
|
|
#include "depot.h"
|
|
|
|
|
|
|
|
|
|
// remember which tiles we have already visited so we don't visit them again.
|
|
|
|
|
/* remember which tiles we have already visited so we don't visit them again. */
|
|
|
|
|
static bool TPFSetTileBit(TrackPathFinder *tpf, TileIndex tile, int dir)
|
|
|
|
|
{
|
|
|
|
|
uint hash, val, offs;
|
|
|
|
@ -65,7 +67,7 @@ static bool TPFSetTileBit(TrackPathFinder *tpf, TileIndex tile, int dir)
|
|
|
|
|
tpf->hash_tile[hash] = PATHFIND_GET_LINK_OFFS(tpf, link);
|
|
|
|
|
|
|
|
|
|
link->flags = tpf->hash_head[hash];
|
|
|
|
|
tpf->hash_head[hash] = 0xFFFF; /* multi link */
|
|
|
|
|
tpf->hash_head[hash] = 0xFFFF; // multi link
|
|
|
|
|
|
|
|
|
|
link->next = 0xFFFF;
|
|
|
|
|
}
|
|
|
|
@ -142,9 +144,9 @@ static void TPFMode2(TrackPathFinder* tpf, TileIndex tile, DiagDirection directi
|
|
|
|
|
|
|
|
|
|
assert(tpf->tracktype == TRANSPORT_WATER);
|
|
|
|
|
|
|
|
|
|
// This addition will sometimes overflow by a single tile.
|
|
|
|
|
// The use of TILE_MASK here makes sure that we still point at a valid
|
|
|
|
|
// tile, and then this tile will be in the sentinel row/col, so GetTileTrackStatus will fail.
|
|
|
|
|
/* This addition will sometimes overflow by a single tile.
|
|
|
|
|
* The use of TILE_MASK here makes sure that we still point at a valid
|
|
|
|
|
* tile, and then this tile will be in the sentinel row/col, so GetTileTrackStatus will fail. */
|
|
|
|
|
tile = TILE_MASK(tile + TileOffsByDiagDir(direction));
|
|
|
|
|
|
|
|
|
|
if (++tpf->rd.cur_length > 50)
|
|
|
|
@ -160,8 +162,8 @@ static void TPFMode2(TrackPathFinder* tpf, TileIndex tile, DiagDirection directi
|
|
|
|
|
if ( (bits & (bits - 1)) == 0 ) {
|
|
|
|
|
/* only one direction */
|
|
|
|
|
i = 0;
|
|
|
|
|
while (!(bits&1))
|
|
|
|
|
i++, bits>>=1;
|
|
|
|
|
while (!(bits & 1))
|
|
|
|
|
i++, bits >>= 1;
|
|
|
|
|
|
|
|
|
|
rd = tpf->rd;
|
|
|
|
|
goto continue_here;
|
|
|
|
@ -172,7 +174,7 @@ static void TPFMode2(TrackPathFinder* tpf, TileIndex tile, DiagDirection directi
|
|
|
|
|
if (!(bits & 1)) continue;
|
|
|
|
|
rd = tpf->rd;
|
|
|
|
|
|
|
|
|
|
// Change direction 4 times only
|
|
|
|
|
/* Change direction 4 times only */
|
|
|
|
|
if ((byte)i != tpf->rd.pft_var6) {
|
|
|
|
|
if (++tpf->rd.depth > 4) {
|
|
|
|
|
tpf->rd = rd;
|
|
|
|
@ -189,7 +191,7 @@ continue_here:;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tpf->rd = rd;
|
|
|
|
|
} while (++i, bits>>=1);
|
|
|
|
|
} while (++i, bits >>= 1);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -284,7 +286,7 @@ static void TPFMode1(TrackPathFinder* tpf, TileIndex tile, DiagDirection directi
|
|
|
|
|
|
|
|
|
|
/* Check in case of rail if the owner is the same */
|
|
|
|
|
if (tpf->tracktype == TRANSPORT_RAIL) {
|
|
|
|
|
// don't enter train depot from the back
|
|
|
|
|
/* don't enter train depot from the back */
|
|
|
|
|
if (IsTileDepotType(tile, TRANSPORT_RAIL) && GetRailDepotDirection(tile) == direction) return;
|
|
|
|
|
|
|
|
|
|
if (IsTileType(tile_org, MP_RAILWAY) || IsTileType(tile_org, MP_STATION) || IsTileType(tile_org, MP_TUNNELBRIDGE))
|
|
|
|
@ -292,12 +294,12 @@ static void TPFMode1(TrackPathFinder* tpf, TileIndex tile, DiagDirection directi
|
|
|
|
|
if (GetTileOwner(tile_org) != GetTileOwner(tile)) return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if the new tile can be entered from that direction
|
|
|
|
|
/* check if the new tile can be entered from that direction */
|
|
|
|
|
if (tpf->tracktype == TRANSPORT_ROAD) {
|
|
|
|
|
// road stops and depots now have a track (r4419)
|
|
|
|
|
// don't enter road stop from the back
|
|
|
|
|
/* road stops and depots now have a track (r4419)
|
|
|
|
|
* don't enter road stop from the back */
|
|
|
|
|
if (IsStandardRoadStopTile(tile) && ReverseDiagDir(GetRoadStopDir(tile)) != direction) return;
|
|
|
|
|
// don't enter road depot from the back
|
|
|
|
|
/* don't enter road depot from the back */
|
|
|
|
|
if (IsTileDepotType(tile, TRANSPORT_ROAD) && ReverseDiagDir(GetRoadDepotDirection(tile)) != direction) return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -323,7 +325,7 @@ static void TPFMode1(TrackPathFinder* tpf, TileIndex tile, DiagDirection directi
|
|
|
|
|
|
|
|
|
|
if ((byte)bits != tpf->var2) {
|
|
|
|
|
bits &= _tpfmode1_and[direction];
|
|
|
|
|
bits = bits | (bits>>8);
|
|
|
|
|
bits = bits | (bits >> 8);
|
|
|
|
|
}
|
|
|
|
|
bits &= 0xBF;
|
|
|
|
|
|
|
|
|
@ -333,7 +335,7 @@ static void TPFMode1(TrackPathFinder* tpf, TileIndex tile, DiagDirection directi
|
|
|
|
|
i = FIND_FIRST_BIT(bits);
|
|
|
|
|
bits = KILL_FIRST_BIT(bits);
|
|
|
|
|
|
|
|
|
|
tpf->the_dir = (Trackdir)((_otherdir_mask[direction] & (byte)(1 << i)) ? (i+8) : i);
|
|
|
|
|
tpf->the_dir = (Trackdir)((_otherdir_mask[direction] & (byte)(1 << i)) ? (i + 8) : i);
|
|
|
|
|
rd = tpf->rd;
|
|
|
|
|
|
|
|
|
|
if (TPFSetTileBit(tpf, tile, tpf->the_dir) &&
|
|
|
|
@ -375,7 +377,7 @@ static void TPFMode1(TrackPathFinder* tpf, TileIndex tile, DiagDirection directi
|
|
|
|
|
i = FIND_FIRST_BIT(bits);
|
|
|
|
|
bits = KILL_FIRST_BIT(bits);
|
|
|
|
|
|
|
|
|
|
tpf->the_dir = (Trackdir)((_otherdir_mask[direction] & (byte)(1 << i)) ? (i+8) : i);
|
|
|
|
|
tpf->the_dir = (Trackdir)((_otherdir_mask[direction] & (byte)(1 << i)) ? (i + 8) : i);
|
|
|
|
|
rd = tpf->rd;
|
|
|
|
|
if (TPFSetTileBit(tpf, tile, tpf->the_dir) &&
|
|
|
|
|
!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length, &tpf->rd.pft_var6) ) {
|
|
|
|
@ -401,10 +403,10 @@ void FollowTrack(TileIndex tile, uint16 flags, DiagDirection direction, TPFEnumP
|
|
|
|
|
tpf.rd.depth = 0;
|
|
|
|
|
tpf.rd.pft_var6 = 0;
|
|
|
|
|
|
|
|
|
|
tpf.var2 = HASBIT(flags, 15) ? 0x43 : 0xFF; /* 0x8000 */
|
|
|
|
|
tpf.var2 = HASBIT(flags, 15) ? 0x43 : 0xFF; // 0x8000
|
|
|
|
|
|
|
|
|
|
tpf.disable_tile_hash = HASBIT(flags, 12); /* 0x1000 */
|
|
|
|
|
tpf.hasbit_13 = HASBIT(flags, 13); /* 0x2000 */
|
|
|
|
|
tpf.disable_tile_hash = HASBIT(flags, 12); // 0x1000
|
|
|
|
|
tpf.hasbit_13 = HASBIT(flags, 13); // 0x2000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tpf.tracktype = (TransportType)(flags & 0xFF);
|
|
|
|
@ -425,8 +427,8 @@ void FollowTrack(TileIndex tile, uint16 flags, DiagDirection direction, TPFEnumP
|
|
|
|
|
|
|
|
|
|
struct StackedItem {
|
|
|
|
|
TileIndex tile;
|
|
|
|
|
uint16 cur_length; // This is the current length to this tile.
|
|
|
|
|
uint16 priority; // This is the current length + estimated length to the goal.
|
|
|
|
|
uint16 cur_length; ///< This is the current length to this tile.
|
|
|
|
|
uint16 priority; ///< This is the current length + estimated length to the goal.
|
|
|
|
|
TrackdirByte track;
|
|
|
|
|
byte depth;
|
|
|
|
|
byte state;
|
|
|
|
@ -461,12 +463,12 @@ struct NewTrackPathFinder {
|
|
|
|
|
uint num_links_left;
|
|
|
|
|
|
|
|
|
|
uint nstack;
|
|
|
|
|
StackedItem stack[256]; // priority queue of stacked items
|
|
|
|
|
StackedItem stack[256]; ///< priority queue of stacked items
|
|
|
|
|
|
|
|
|
|
uint16 hash_head[0x400]; // hash heads. 0 means unused. 0xFFFC = length, 0x3 = dir
|
|
|
|
|
TileIndex hash_tile[0x400]; // tiles. or links.
|
|
|
|
|
uint16 hash_head[0x400]; ///< hash heads. 0 means unused. 0xFFFC = length, 0x3 = dir
|
|
|
|
|
TileIndex hash_tile[0x400]; ///< tiles. or links.
|
|
|
|
|
|
|
|
|
|
HashLink links[0x400]; // hash links
|
|
|
|
|
HashLink links[0x400]; ///< hash links
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
#define NTP_GET_LINK_OFFS(tpf, link) ((byte*)(link) - (byte*)tpf->links)
|
|
|
|
@ -474,22 +476,22 @@ struct NewTrackPathFinder {
|
|
|
|
|
|
|
|
|
|
#define ARR(i) tpf->stack[(i)-1]
|
|
|
|
|
|
|
|
|
|
// called after a new element was added in the queue at the last index.
|
|
|
|
|
// move it down to the proper position
|
|
|
|
|
/** called after a new element was added in the queue at the last index.
|
|
|
|
|
* move it down to the proper position */
|
|
|
|
|
static inline void HeapifyUp(NewTrackPathFinder *tpf)
|
|
|
|
|
{
|
|
|
|
|
StackedItem si;
|
|
|
|
|
int i = ++tpf->nstack;
|
|
|
|
|
|
|
|
|
|
while (i != 1 && ARR(i).priority < ARR(i>>1).priority) {
|
|
|
|
|
// the child element is larger than the parent item.
|
|
|
|
|
// swap the child item and the parent item.
|
|
|
|
|
si = ARR(i); ARR(i) = ARR(i>>1); ARR(i>>1) = si;
|
|
|
|
|
i>>=1;
|
|
|
|
|
/* the child element is larger than the parent item.
|
|
|
|
|
* swap the child item and the parent item. */
|
|
|
|
|
si = ARR(i); ARR(i) = ARR(i >> 1); ARR(i >> 1) = si;
|
|
|
|
|
i >>= 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// called after the element 0 was eaten. fill it with a new element
|
|
|
|
|
/** called after the element 0 was eaten. fill it with a new element */
|
|
|
|
|
static inline void HeapifyDown(NewTrackPathFinder *tpf)
|
|
|
|
|
{
|
|
|
|
|
StackedItem si;
|
|
|
|
@ -501,27 +503,27 @@ static inline void HeapifyDown(NewTrackPathFinder *tpf)
|
|
|
|
|
|
|
|
|
|
if (n == 0) return; // heap is empty so nothing to do?
|
|
|
|
|
|
|
|
|
|
// copy the last item to index 0. we use it as base for heapify.
|
|
|
|
|
ARR(1) = ARR(n+1);
|
|
|
|
|
/* copy the last item to index 0. we use it as base for heapify. */
|
|
|
|
|
ARR(1) = ARR(n + 1);
|
|
|
|
|
|
|
|
|
|
while ((j=i*2) <= n) {
|
|
|
|
|
// figure out which is smaller of the children.
|
|
|
|
|
if (j != n && ARR(j).priority > ARR(j+1).priority)
|
|
|
|
|
while ((j = i * 2) <= n) {
|
|
|
|
|
/* figure out which is smaller of the children. */
|
|
|
|
|
if (j != n && ARR(j).priority > ARR(j + 1).priority)
|
|
|
|
|
j++; // right item is smaller
|
|
|
|
|
|
|
|
|
|
assert(i <= n && j <= n);
|
|
|
|
|
if (ARR(i).priority <= ARR(j).priority)
|
|
|
|
|
break; // base elem smaller than smallest, done!
|
|
|
|
|
|
|
|
|
|
// swap parent with the child
|
|
|
|
|
/* swap parent with the child */
|
|
|
|
|
si = ARR(i); ARR(i) = ARR(j); ARR(j) = si;
|
|
|
|
|
i = j;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mark a tile as visited and store the length of the path.
|
|
|
|
|
// if we already had a better path to this tile, return false.
|
|
|
|
|
// otherwise return true.
|
|
|
|
|
/** mark a tile as visited and store the length of the path.
|
|
|
|
|
* if we already had a better path to this tile, return false.
|
|
|
|
|
* otherwise return true. */
|
|
|
|
|
static bool NtpVisit(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection dir, uint length)
|
|
|
|
|
{
|
|
|
|
|
uint hash,head;
|
|
|
|
@ -531,7 +533,7 @@ static bool NtpVisit(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection dir,
|
|
|
|
|
|
|
|
|
|
hash = PATHFIND_HASH_TILE(tile);
|
|
|
|
|
|
|
|
|
|
// never visited before?
|
|
|
|
|
/* never visited before? */
|
|
|
|
|
if ((head=tpf->hash_head[hash]) == 0) {
|
|
|
|
|
tpf->hash_tile[hash] = tile;
|
|
|
|
|
tpf->hash_head[hash] = dir | (length << 2);
|
|
|
|
@ -541,15 +543,15 @@ static bool NtpVisit(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection dir,
|
|
|
|
|
if (head != 0xffff) {
|
|
|
|
|
if (tile == tpf->hash_tile[hash] && (head & 0x3) == (uint)dir) {
|
|
|
|
|
|
|
|
|
|
// longer length
|
|
|
|
|
/* longer length */
|
|
|
|
|
if (length >= (head >> 2)) return false;
|
|
|
|
|
|
|
|
|
|
tpf->hash_head[hash] = dir | (length << 2);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// two tiles with the same hash, need to make a link
|
|
|
|
|
// allocate a link. if out of links, handle this by returning
|
|
|
|
|
// that a tile was already visisted.
|
|
|
|
|
/* two tiles with the same hash, need to make a link
|
|
|
|
|
* allocate a link. if out of links, handle this by returning
|
|
|
|
|
* that a tile was already visisted. */
|
|
|
|
|
if (tpf->num_links_left == 0) {
|
|
|
|
|
DEBUG(ntp, 1, "No links left");
|
|
|
|
|
return false;
|
|
|
|
@ -564,12 +566,12 @@ static bool NtpVisit(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection dir,
|
|
|
|
|
tpf->hash_tile[hash] = NTP_GET_LINK_OFFS(tpf, link);
|
|
|
|
|
|
|
|
|
|
link->typelength = tpf->hash_head[hash];
|
|
|
|
|
tpf->hash_head[hash] = 0xFFFF; /* multi link */
|
|
|
|
|
tpf->hash_head[hash] = 0xFFFF; // multi link
|
|
|
|
|
link->next = 0xFFFF;
|
|
|
|
|
} else {
|
|
|
|
|
// a linked list of many tiles,
|
|
|
|
|
// find the one corresponding to the tile, if it exists.
|
|
|
|
|
// otherwise make a new link
|
|
|
|
|
/* a linked list of many tiles,
|
|
|
|
|
* find the one corresponding to the tile, if it exists.
|
|
|
|
|
* otherwise make a new link */
|
|
|
|
|
|
|
|
|
|
uint offs = tpf->hash_tile[hash];
|
|
|
|
|
do {
|
|
|
|
@ -623,7 +625,7 @@ static bool NtpCheck(NewTrackPathFinder *tpf, TileIndex tile, uint dir, uint len
|
|
|
|
|
return length == (head >> 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// else it's a linked list of many tiles
|
|
|
|
|
/* else it's a linked list of many tiles */
|
|
|
|
|
offs = tpf->hash_tile[hash];
|
|
|
|
|
for (;;) {
|
|
|
|
|
link = NTP_GET_LINK_PTR(tpf, offs);
|
|
|
|
@ -638,21 +640,21 @@ static bool NtpCheck(NewTrackPathFinder *tpf, TileIndex tile, uint dir, uint len
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const uint16 _is_upwards_slope[15] = {
|
|
|
|
|
0, // no tileh
|
|
|
|
|
(1 << TRACKDIR_X_SW) | (1 << TRACKDIR_Y_NW), // 1
|
|
|
|
|
(1 << TRACKDIR_X_SW) | (1 << TRACKDIR_Y_SE), // 2
|
|
|
|
|
(1 << TRACKDIR_X_SW), // 3
|
|
|
|
|
(1 << TRACKDIR_X_NE) | (1 << TRACKDIR_Y_SE), // 4
|
|
|
|
|
0, // 5
|
|
|
|
|
(1 << TRACKDIR_Y_SE), // 6
|
|
|
|
|
0, // 7
|
|
|
|
|
(1 << TRACKDIR_X_NE) | (1 << TRACKDIR_Y_NW), // 8,
|
|
|
|
|
(1 << TRACKDIR_Y_NW), // 9
|
|
|
|
|
0, //10
|
|
|
|
|
0, //11,
|
|
|
|
|
(1 << TRACKDIR_X_NE), //12
|
|
|
|
|
0, //13
|
|
|
|
|
0, //14
|
|
|
|
|
0, ///< no tileh
|
|
|
|
|
(1 << TRACKDIR_X_SW) | (1 << TRACKDIR_Y_NW), ///< 1
|
|
|
|
|
(1 << TRACKDIR_X_SW) | (1 << TRACKDIR_Y_SE), ///< 2
|
|
|
|
|
(1 << TRACKDIR_X_SW), ///< 3
|
|
|
|
|
(1 << TRACKDIR_X_NE) | (1 << TRACKDIR_Y_SE), ///< 4
|
|
|
|
|
0, ///< 5
|
|
|
|
|
(1 << TRACKDIR_Y_SE), ///< 6
|
|
|
|
|
0, ///< 7
|
|
|
|
|
(1 << TRACKDIR_X_NE) | (1 << TRACKDIR_Y_NW), ///< 8,
|
|
|
|
|
(1 << TRACKDIR_Y_NW), ///< 9
|
|
|
|
|
0, ///< 10
|
|
|
|
|
0, ///< 11,
|
|
|
|
|
(1 << TRACKDIR_X_NE), ///< 12
|
|
|
|
|
0, ///< 13
|
|
|
|
|
0, ///< 14
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static uint DistanceMoo(TileIndex t0, TileIndex t1)
|
|
|
|
@ -660,27 +662,27 @@ static uint DistanceMoo(TileIndex t0, TileIndex t1)
|
|
|
|
|
const uint dx = delta(TileX(t0), TileX(t1));
|
|
|
|
|
const uint dy = delta(TileY(t0), TileY(t1));
|
|
|
|
|
|
|
|
|
|
const uint straightTracks = 2 * min(dx, dy); /* The number of straight (not full length) tracks */
|
|
|
|
|
const uint straightTracks = 2 * min(dx, dy); // The number of straight (not full length) tracks
|
|
|
|
|
/* OPTIMISATION:
|
|
|
|
|
* Original: diagTracks = max(dx, dy) - min(dx,dy);
|
|
|
|
|
* Proof:
|
|
|
|
|
* (dx-dy) - straightTracks == (min + max) - straightTracks = min + // max - 2 * min = max - min */
|
|
|
|
|
const uint diagTracks = dx + dy - straightTracks; /* The number of diagonal (full tile length) tracks. */
|
|
|
|
|
const uint diagTracks = dx + dy - straightTracks; // The number of diagonal (full tile length) tracks.
|
|
|
|
|
|
|
|
|
|
return diagTracks*DIAG_FACTOR + straightTracks*STR_FACTOR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// These has to be small cause the max length of a track
|
|
|
|
|
// is currently limited to 16384
|
|
|
|
|
/* These has to be small cause the max length of a track
|
|
|
|
|
* is currently limited to 16384 */
|
|
|
|
|
|
|
|
|
|
static const byte _length_of_track[16] = {
|
|
|
|
|
DIAG_FACTOR, DIAG_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, 0, 0,
|
|
|
|
|
DIAG_FACTOR, DIAG_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, 0, 0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// new more optimized pathfinder for trains...
|
|
|
|
|
// Tile is the tile the train is at.
|
|
|
|
|
// direction is the tile the train is moving towards.
|
|
|
|
|
/* new more optimized pathfinder for trains...
|
|
|
|
|
* Tile is the tile the train is at.
|
|
|
|
|
* direction is the tile the train is moving towards. */
|
|
|
|
|
|
|
|
|
|
static void NTPEnum(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
|
|
|
|
|
{
|
|
|
|
@ -692,8 +694,8 @@ static void NTPEnum(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection direc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Need to have a special case for the start.
|
|
|
|
|
// We shouldn't call the callback for the current tile.
|
|
|
|
|
/* Need to have a special case for the start.
|
|
|
|
|
* We shouldn't call the callback for the current tile. */
|
|
|
|
|
si.cur_length = 1; // Need to start at 1 cause 0 is a reserved value.
|
|
|
|
|
si.depth = 0;
|
|
|
|
|
si.state = 0;
|
|
|
|
@ -701,7 +703,7 @@ static void NTPEnum(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection direc
|
|
|
|
|
goto start_at;
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
// Get the next item to search from from the priority queue
|
|
|
|
|
/* Get the next item to search from from the priority queue */
|
|
|
|
|
do {
|
|
|
|
|
if (tpf->nstack == 0)
|
|
|
|
|
return; // nothing left? then we're done!
|
|
|
|
@ -709,10 +711,10 @@ static void NTPEnum(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection direc
|
|
|
|
|
tile = si.tile;
|
|
|
|
|
|
|
|
|
|
HeapifyDown(tpf);
|
|
|
|
|
// Make sure we havn't already visited this tile.
|
|
|
|
|
/* Make sure we havn't already visited this tile. */
|
|
|
|
|
} while (!NtpCheck(tpf, tile, _tpf_prev_direction[si.track], si.cur_length));
|
|
|
|
|
|
|
|
|
|
// Add the length of this track.
|
|
|
|
|
/* Add the length of this track. */
|
|
|
|
|
si.cur_length += _length_of_track[si.track];
|
|
|
|
|
|
|
|
|
|
callback_and_continue:
|
|
|
|
@ -723,8 +725,8 @@ callback_and_continue:
|
|
|
|
|
direction = _tpf_new_direction[si.track];
|
|
|
|
|
|
|
|
|
|
start_at:
|
|
|
|
|
// If the tile is the entry tile of a tunnel, and we're not going out of the tunnel,
|
|
|
|
|
// need to find the exit of the tunnel.
|
|
|
|
|
/* If the tile is the entry tile of a tunnel, and we're not going out of the tunnel,
|
|
|
|
|
* need to find the exit of the tunnel. */
|
|
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) {
|
|
|
|
|
if (IsTunnel(tile)) {
|
|
|
|
|
if (GetTunnelDirection(tile) != ReverseDiagDir(direction)) {
|
|
|
|
@ -733,7 +735,7 @@ start_at:
|
|
|
|
|
/* We are not just driving out of the tunnel */
|
|
|
|
|
if (GetTunnelDirection(tile) != direction ||
|
|
|
|
|
GetTunnelTransportType(tile) != tpf->tracktype) {
|
|
|
|
|
// We are not driving into the tunnel, or it is an invalid tunnel
|
|
|
|
|
/* We are not driving into the tunnel, or it is an invalid tunnel */
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!HASBIT(tpf->railtypes, GetRailType(tile))) {
|
|
|
|
@ -743,15 +745,15 @@ start_at:
|
|
|
|
|
flotr = FindLengthOfTunnel(tile, direction);
|
|
|
|
|
si.cur_length += flotr.length * DIAG_FACTOR;
|
|
|
|
|
tile = flotr.tile;
|
|
|
|
|
// tile now points to the exit tile of the tunnel
|
|
|
|
|
/* tile now points to the exit tile of the tunnel */
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
TileIndex tile_end;
|
|
|
|
|
if (GetBridgeRampDirection(tile) != ReverseDiagDir(direction)) {
|
|
|
|
|
// We are not just leaving the bridge
|
|
|
|
|
/* We are not just leaving the bridge */
|
|
|
|
|
if (GetBridgeRampDirection(tile) != direction ||
|
|
|
|
|
GetBridgeTransportType(tile) != tpf->tracktype) {
|
|
|
|
|
// Not entering the bridge or not compatible
|
|
|
|
|
/* Not entering the bridge or not compatible */
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -761,29 +763,29 @@ start_at:
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This is a special loop used to go through
|
|
|
|
|
// a rail net and find the first intersection
|
|
|
|
|
/* This is a special loop used to go through
|
|
|
|
|
* a rail net and find the first intersection */
|
|
|
|
|
tile_org = tile;
|
|
|
|
|
for (;;) {
|
|
|
|
|
assert(direction <= 3);
|
|
|
|
|
tile += TileOffsByDiagDir(direction);
|
|
|
|
|
|
|
|
|
|
// too long search length? bail out.
|
|
|
|
|
/* too long search length? bail out. */
|
|
|
|
|
if (si.cur_length >= tpf->maxlength) {
|
|
|
|
|
DEBUG(ntp, 1, "Cur_length too big");
|
|
|
|
|
bits = TRACK_BIT_NONE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Not a regular rail tile?
|
|
|
|
|
// Then we can't use the code below, but revert to more general code.
|
|
|
|
|
/* Not a regular rail tile?
|
|
|
|
|
* Then we can't use the code below, but revert to more general code. */
|
|
|
|
|
if (!IsTileType(tile, MP_RAILWAY) || !IsPlainRailTile(tile)) {
|
|
|
|
|
// We found a tile which is not a normal railway tile.
|
|
|
|
|
// Determine which tracks that exist on this tile.
|
|
|
|
|
/* We found a tile which is not a normal railway tile.
|
|
|
|
|
* Determine which tracks that exist on this tile. */
|
|
|
|
|
uint32 ts = GetTileTrackStatus(tile, TRANSPORT_RAIL) & _tpfmode1_and[direction];
|
|
|
|
|
bits = TrackdirBitsToTrackBits((TrackdirBits)(ts & TRACKDIR_BIT_MASK));
|
|
|
|
|
|
|
|
|
|
// Check that the tile contains exactly one track
|
|
|
|
|
/* Check that the tile contains exactly one track */
|
|
|
|
|
if (bits == 0 || KILL_FIRST_BIT(bits) != 0) break;
|
|
|
|
|
|
|
|
|
|
if (!HASBIT(tpf->railtypes, GetRailType(tile))) {
|
|
|
|
@ -791,12 +793,12 @@ start_at:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////
|
|
|
|
|
// If we reach here, the tile has exactly one track.
|
|
|
|
|
// tile - index to a tile that is not rail tile, but still straight (with optional signals)
|
|
|
|
|
// bits - bitmask of which track that exist on the tile (exactly one bit is set)
|
|
|
|
|
// direction - which direction are we moving in?
|
|
|
|
|
///////////////////
|
|
|
|
|
/*******************
|
|
|
|
|
* If we reach here, the tile has exactly one track.
|
|
|
|
|
* tile - index to a tile that is not rail tile, but still straight (with optional signals)
|
|
|
|
|
* bits - bitmask of which track that exist on the tile (exactly one bit is set)
|
|
|
|
|
* direction - which direction are we moving in?
|
|
|
|
|
*******************/
|
|
|
|
|
si.track = _new_trackdir[FIND_FIRST_BIT(bits)][direction];
|
|
|
|
|
si.cur_length += _length_of_track[si.track];
|
|
|
|
|
goto callback_and_continue;
|
|
|
|
@ -825,89 +827,89 @@ start_at:
|
|
|
|
|
|
|
|
|
|
si.cur_length += _length_of_track[track];
|
|
|
|
|
|
|
|
|
|
// Check if this rail is an upwards slope. If it is, then add a penalty.
|
|
|
|
|
// Small optimization here.. if (track&7)>1 then it can't be a slope so we avoid calling GetTileSlope
|
|
|
|
|
/* Check if this rail is an upwards slope. If it is, then add a penalty.
|
|
|
|
|
* Small optimization here.. if (track&7)>1 then it can't be a slope so we avoid calling GetTileSlope */
|
|
|
|
|
if ((track & 7) <= 1 && (_is_upwards_slope[GetTileSlope(tile, NULL)] & (1 << track)) ) {
|
|
|
|
|
// upwards slope. add some penalty.
|
|
|
|
|
si.cur_length += 4*DIAG_FACTOR;
|
|
|
|
|
si.cur_length += 4 * DIAG_FACTOR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// railway tile with signals..?
|
|
|
|
|
/* railway tile with signals..? */
|
|
|
|
|
if (HasSignals(tile)) {
|
|
|
|
|
if (!HasSignalOnTrackdir(tile, track)) {
|
|
|
|
|
// if one way signal not pointing towards us, stop going in this direction => End of rail segment.
|
|
|
|
|
/* if one way signal not pointing towards us, stop going in this direction => End of rail segment. */
|
|
|
|
|
if (HasSignalOnTrackdir(tile, ReverseTrackdir(track))) {
|
|
|
|
|
bits = TRACK_BIT_NONE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else if (GetSignalStateByTrackdir(tile, track) == SIGNAL_STATE_GREEN) {
|
|
|
|
|
// green signal in our direction. either one way or two way.
|
|
|
|
|
/* green signal in our direction. either one way or two way. */
|
|
|
|
|
si.state |= 3;
|
|
|
|
|
} else {
|
|
|
|
|
// reached a red signal.
|
|
|
|
|
/* reached a red signal. */
|
|
|
|
|
if (HasSignalOnTrackdir(tile, ReverseTrackdir(track))) {
|
|
|
|
|
// two way red signal. unless we passed another green signal on the way,
|
|
|
|
|
// stop going in this direction => End of rail segment.
|
|
|
|
|
// this is to prevent us from going into a full platform.
|
|
|
|
|
if (!(si.state&1)) {
|
|
|
|
|
/* two way red signal. unless we passed another green signal on the way,
|
|
|
|
|
* stop going in this direction => End of rail segment.
|
|
|
|
|
* this is to prevent us from going into a full platform. */
|
|
|
|
|
if (!(si.state & 1)) {
|
|
|
|
|
bits = TRACK_BIT_NONE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!(si.state & 2)) {
|
|
|
|
|
// Is this the first signal we see? And it's red... add penalty
|
|
|
|
|
si.cur_length += 10*DIAG_FACTOR;
|
|
|
|
|
/* Is this the first signal we see? And it's red... add penalty */
|
|
|
|
|
si.cur_length += 10 * DIAG_FACTOR;
|
|
|
|
|
si.state += 2; // remember that we added penalty.
|
|
|
|
|
// Because we added a penalty, we can't just continue as usual.
|
|
|
|
|
// Need to get out and let A* do it's job with
|
|
|
|
|
// possibly finding an even shorter path.
|
|
|
|
|
/* Because we added a penalty, we can't just continue as usual.
|
|
|
|
|
* Need to get out and let A* do it's job with
|
|
|
|
|
* possibly finding an even shorter path. */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tpf->enum_proc(tile, tpf->userdata, si.first_track, si.cur_length))
|
|
|
|
|
return; /* Don't process this tile any further */
|
|
|
|
|
return; // Don't process this tile any further
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// continue with the next track
|
|
|
|
|
/* continue with the next track */
|
|
|
|
|
direction = _tpf_new_direction[track];
|
|
|
|
|
|
|
|
|
|
// safety check if we're running around chasing our tail... (infinite loop)
|
|
|
|
|
/* safety check if we're running around chasing our tail... (infinite loop) */
|
|
|
|
|
if (tile == tile_org) {
|
|
|
|
|
bits = TRACK_BIT_NONE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// There are no tracks to choose between.
|
|
|
|
|
// Stop searching in this direction
|
|
|
|
|
/* There are no tracks to choose between.
|
|
|
|
|
* Stop searching in this direction */
|
|
|
|
|
if (bits == TRACK_BIT_NONE)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
////////////////
|
|
|
|
|
// We got multiple tracks to choose between (intersection).
|
|
|
|
|
// Branch the search space into several branches.
|
|
|
|
|
////////////////
|
|
|
|
|
/****************
|
|
|
|
|
* We got multiple tracks to choose between (intersection).
|
|
|
|
|
* Branch the search space into several branches.
|
|
|
|
|
****************/
|
|
|
|
|
|
|
|
|
|
// Check if we've already visited this intersection.
|
|
|
|
|
// If we've already visited it with a better length, then
|
|
|
|
|
// there's no point in visiting it again.
|
|
|
|
|
/* Check if we've already visited this intersection.
|
|
|
|
|
* If we've already visited it with a better length, then
|
|
|
|
|
* there's no point in visiting it again. */
|
|
|
|
|
if (!NtpVisit(tpf, tile, direction, si.cur_length))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// Push all possible alternatives that we can reach from here
|
|
|
|
|
// onto the priority heap.
|
|
|
|
|
// 'bits' contains the tracks that we can choose between.
|
|
|
|
|
/* Push all possible alternatives that we can reach from here
|
|
|
|
|
* onto the priority heap.
|
|
|
|
|
* 'bits' contains the tracks that we can choose between. */
|
|
|
|
|
|
|
|
|
|
// First compute the estimated distance to the target.
|
|
|
|
|
// This is used to implement A*
|
|
|
|
|
/* First compute the estimated distance to the target.
|
|
|
|
|
* This is used to implement A* */
|
|
|
|
|
estimation = 0;
|
|
|
|
|
if (tpf->dest != 0)
|
|
|
|
|
estimation = DistanceMoo(tile, tpf->dest);
|
|
|
|
|
|
|
|
|
|
si.depth++;
|
|
|
|
|
if (si.depth == 0)
|
|
|
|
|
continue; /* We overflowed our depth. No more searching in this direction. */
|
|
|
|
|
continue; // We overflowed our depth. No more searching in this direction.
|
|
|
|
|
si.tile = tile;
|
|
|
|
|
while (bits != TRACK_BIT_NONE) {
|
|
|
|
|
Track track = RemoveFirstTrack(&bits);
|
|
|
|
@ -915,7 +917,7 @@ start_at:
|
|
|
|
|
assert(si.track != 0xFF);
|
|
|
|
|
si.priority = si.cur_length + estimation;
|
|
|
|
|
|
|
|
|
|
// out of stack items, bail out?
|
|
|
|
|
/* out of stack items, bail out? */
|
|
|
|
|
if (tpf->nstack >= lengthof(tpf->stack)) {
|
|
|
|
|
DEBUG(ntp, 1, "Out of stack");
|
|
|
|
|
break;
|
|
|
|
@ -925,9 +927,9 @@ start_at:
|
|
|
|
|
HeapifyUp(tpf);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// If this is the first intersection, we need to fill the first_track member.
|
|
|
|
|
// so the code outside knows which path is better.
|
|
|
|
|
// also randomize the order in which we search through them.
|
|
|
|
|
/* If this is the first intersection, we need to fill the first_track member.
|
|
|
|
|
* so the code outside knows which path is better.
|
|
|
|
|
* also randomize the order in which we search through them. */
|
|
|
|
|
if (si.depth == 1) {
|
|
|
|
|
assert(tpf->nstack == 1 || tpf->nstack == 2 || tpf->nstack == 3);
|
|
|
|
|
if (tpf->nstack != 1) {
|
|
|
|
@ -944,12 +946,12 @@ start_at:
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Continue with the next from the queue...
|
|
|
|
|
/* Continue with the next from the queue... */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// new pathfinder for trains. better and faster.
|
|
|
|
|
/** new pathfinder for trains. better and faster. */
|
|
|
|
|
void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypeMask railtypes, DiagDirection direction, NTPEnumProc* enum_proc, void* data)
|
|
|
|
|
{
|
|
|
|
|
NewTrackPathFinder tpf;
|
|
|
|
|