2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file pathfind.cpp Implementation of the oldest supported pathfinder. */
|
2007-03-21 17:42:43 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2006-03-16 15:16:27 +00:00
|
|
|
#include "bridge_map.h"
|
2006-05-27 16:12:16 +00:00
|
|
|
#include "station_map.h"
|
2007-12-21 22:50:51 +00:00
|
|
|
#include "tile_cmd.h"
|
2007-04-12 13:07:15 +00:00
|
|
|
#include "landscape.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "pathfind.h"
|
2007-12-18 20:58:12 +00:00
|
|
|
#include "rail_type.h"
|
2005-07-12 20:28:19 +00:00
|
|
|
#include "debug.h"
|
2006-03-06 20:55:24 +00:00
|
|
|
#include "tunnel_map.h"
|
2008-01-13 14:37:30 +00:00
|
|
|
#include "settings_type.h"
|
2007-12-16 15:38:51 +00:00
|
|
|
#include "tunnelbridge_map.h"
|
2007-12-21 22:50:51 +00:00
|
|
|
#include "core/random_func.hpp"
|
2008-04-13 19:25:14 +00:00
|
|
|
#include "core/alloc_type.hpp"
|
2008-01-23 22:34:04 +00:00
|
|
|
#include "tunnelbridge.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* remember which tiles we have already visited so we don't visit them again. */
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool TPFSetTileBit(TrackPathFinder *tpf, TileIndex tile, int dir)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
uint hash, val, offs;
|
|
|
|
TrackPathFinderLink *link, *new_link;
|
|
|
|
uint bits = 1 << dir;
|
|
|
|
|
|
|
|
if (tpf->disable_tile_hash)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
hash = PATHFIND_HASH_TILE(tile);
|
|
|
|
|
|
|
|
val = tpf->hash_head[hash];
|
|
|
|
|
|
|
|
if (val == 0) {
|
|
|
|
/* unused hash entry, set the appropriate bit in it and return true
|
|
|
|
* to indicate that a bit was set. */
|
|
|
|
tpf->hash_head[hash] = bits;
|
2005-06-27 06:57:24 +00:00
|
|
|
tpf->hash_tile[hash] = tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
|
|
|
} else if (!(val & 0x8000)) {
|
|
|
|
/* single tile */
|
|
|
|
|
2005-06-27 06:57:24 +00:00
|
|
|
if (tile == tpf->hash_tile[hash]) {
|
2004-08-09 17:04:08 +00:00
|
|
|
/* found another bit for the same tile,
|
|
|
|
* check if this bit is already set, if so, return false */
|
|
|
|
if (val & bits)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* otherwise set the bit and return true to indicate that the bit
|
|
|
|
* was set */
|
|
|
|
tpf->hash_head[hash] = val | bits;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
/* two tiles with the same hash, need to make a link */
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* allocate a link. if out of links, handle this by returning
|
|
|
|
* that a tile was already visisted. */
|
2005-07-12 20:28:19 +00:00
|
|
|
if (tpf->num_links_left == 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
2005-07-12 20:28:19 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
tpf->num_links_left--;
|
|
|
|
link = tpf->new_link++;
|
|
|
|
|
|
|
|
/* move the data that was previously in the hash_??? variables
|
|
|
|
* to the link struct, and let the hash variables point to the link */
|
|
|
|
link->tile = tpf->hash_tile[hash];
|
|
|
|
tpf->hash_tile[hash] = PATHFIND_GET_LINK_OFFS(tpf, link);
|
|
|
|
|
|
|
|
link->flags = tpf->hash_head[hash];
|
2007-03-21 17:42:43 +00:00
|
|
|
tpf->hash_head[hash] = 0xFFFF; // multi link
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
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 */
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
offs = tpf->hash_tile[hash];
|
|
|
|
do {
|
|
|
|
link = PATHFIND_GET_LINK_PTR(tpf, offs);
|
2005-06-27 06:57:24 +00:00
|
|
|
if (tile == link->tile) {
|
2004-08-09 17:04:08 +00:00
|
|
|
/* found the tile in the link list,
|
|
|
|
* check if the bit was alrady set, if so return false to indicate that the
|
|
|
|
* bit was already set */
|
|
|
|
if (link->flags & bits)
|
|
|
|
return false;
|
|
|
|
link->flags |= bits;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} while ((offs=link->next) != 0xFFFF);
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* get here if we need to add a new link to link,
|
|
|
|
* first, allocate a new link, in the same way as before */
|
2005-07-12 20:28:19 +00:00
|
|
|
if (tpf->num_links_left == 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
2005-07-12 20:28:19 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
tpf->num_links_left--;
|
|
|
|
new_link = tpf->new_link++;
|
|
|
|
|
|
|
|
/* then fill the link with the new info, and establish a ptr from the old
|
|
|
|
* link to the new one */
|
2005-06-27 06:57:24 +00:00
|
|
|
new_link->tile = tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
new_link->flags = bits;
|
|
|
|
new_link->next = 0xFFFF;
|
|
|
|
|
|
|
|
link->next = PATHFIND_GET_LINK_OFFS(tpf, new_link);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-04-02 13:57:25 +00:00
|
|
|
static void TPFModeShip(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-04-22 05:36:22 +00:00
|
|
|
assert(tpf->tracktype == TRANSPORT_WATER);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-06-11 13:54:01 +00:00
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) {
|
|
|
|
/* wrong track type */
|
|
|
|
if (GetTunnelBridgeTransportType(tile) != tpf->tracktype) return;
|
|
|
|
|
|
|
|
DiagDirection dir = GetTunnelBridgeDirection(tile);
|
|
|
|
/* entering tunnel / bridge? */
|
|
|
|
if (dir == direction) {
|
|
|
|
TileIndex endtile = GetOtherTunnelBridgeEnd(tile);
|
|
|
|
|
|
|
|
tpf->rd.cur_length += GetTunnelBridgeLength(tile, endtile) + 1;
|
|
|
|
|
|
|
|
TPFSetTileBit(tpf, tile, 14);
|
|
|
|
TPFSetTileBit(tpf, endtile, 14);
|
|
|
|
|
|
|
|
tile = endtile;
|
|
|
|
} else {
|
|
|
|
/* leaving tunnel / bridge? */
|
|
|
|
if (ReverseDiagDir(dir) != direction) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* 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. */
|
2006-09-05 23:21:41 +00:00
|
|
|
tile = TILE_MASK(tile + TileOffsByDiagDir(direction));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (++tpf->rd.cur_length > 50)
|
|
|
|
return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-04-03 14:21:17 +00:00
|
|
|
TrackBits bits = TrackStatusToTrackBits(GetTileTrackStatus(tile, tpf->tracktype, tpf->sub_type)) & DiagdirReachesTracks(direction);
|
2008-02-20 15:13:42 +00:00
|
|
|
if (bits == TRACK_BIT_NONE) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-07 17:02:43 +00:00
|
|
|
assert(TileX(tile) != MapMaxX() && TileY(tile) != MapMaxY());
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-20 15:13:42 +00:00
|
|
|
bool only_one_track = true;
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2008-02-20 15:13:42 +00:00
|
|
|
Track track = RemoveFirstTrack(&bits);
|
|
|
|
if (bits != TRACK_BIT_NONE) only_one_track = false;
|
2008-06-11 13:54:01 +00:00
|
|
|
RememberData rd = tpf->rd;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Change direction 4 times only */
|
2008-02-20 15:13:42 +00:00
|
|
|
if (!only_one_track && track != tpf->rd.last_choosen_track) {
|
2006-02-01 07:36:15 +00:00
|
|
|
if (++tpf->rd.depth > 4) {
|
2004-09-10 19:02:27 +00:00
|
|
|
tpf->rd = rd;
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-02-20 15:13:42 +00:00
|
|
|
tpf->rd.last_choosen_track = track;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-04-03 14:21:17 +00:00
|
|
|
tpf->the_dir = TrackEnterdirToTrackdir(track, direction);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-02-20 15:13:42 +00:00
|
|
|
if (!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length)) {
|
2008-04-03 14:21:17 +00:00
|
|
|
TPFModeShip(tpf, tile, TrackdirToExitdir(tpf->the_dir));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tpf->rd = rd;
|
2008-02-20 15:13:42 +00:00
|
|
|
} while (bits != TRACK_BIT_NONE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-01-23 19:31:11 +00:00
|
|
|
/**
|
|
|
|
* Checks if any vehicle can enter/leave tile in given diagdir
|
|
|
|
* Checks only for rail/road depots and road non-drivethrough stations
|
|
|
|
* @param tile tile to check
|
|
|
|
* @param side side of tile we are trying to leave/enter
|
|
|
|
* @param tracktype type of transport
|
|
|
|
* @pre tile has trackbit at that diagdir
|
|
|
|
* @return true iff vehicle can enter/leve the tile in given side
|
|
|
|
*/
|
|
|
|
static inline bool CanAccessTileInDir(TileIndex tile, DiagDirection side, TransportType tracktype)
|
|
|
|
{
|
|
|
|
if (tracktype == TRANSPORT_RAIL) {
|
|
|
|
/* depot from wrong side */
|
2008-04-17 18:24:45 +00:00
|
|
|
if (IsRailDepotTile(tile) && GetRailDepotDirection(tile) != side) return false;
|
2008-01-23 19:31:11 +00:00
|
|
|
} else if (tracktype == TRANSPORT_ROAD) {
|
|
|
|
/* depot from wrong side */
|
2008-04-17 18:24:45 +00:00
|
|
|
if (IsRoadDepotTile(tile) && GetRoadDepotDirection(tile) != side) return false;
|
2008-01-23 19:31:11 +00:00
|
|
|
/* non-driverthrough road station from wrong side */
|
|
|
|
if (IsStandardRoadStopTile(tile) && GetRoadStopDir(tile) != side) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-04-02 13:57:25 +00:00
|
|
|
static void TPFModeNormal(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
|
2007-06-26 06:57:14 +00:00
|
|
|
{
|
2008-01-23 18:24:04 +00:00
|
|
|
const TileIndex tile_org = tile;
|
2008-01-23 17:30:28 +00:00
|
|
|
|
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) {
|
2008-01-23 18:24:04 +00:00
|
|
|
/* wrong track type */
|
|
|
|
if (GetTunnelBridgeTransportType(tile) != tpf->tracktype) return;
|
|
|
|
|
|
|
|
DiagDirection dir = GetTunnelBridgeDirection(tile);
|
|
|
|
/* entering tunnel / bridge? */
|
|
|
|
if (dir == direction) {
|
|
|
|
TileIndex endtile = GetOtherTunnelBridgeEnd(tile);
|
|
|
|
|
2008-01-23 22:34:04 +00:00
|
|
|
tpf->rd.cur_length += GetTunnelBridgeLength(tile, endtile) + 1;
|
2008-01-23 18:24:04 +00:00
|
|
|
|
2008-01-23 17:30:28 +00:00
|
|
|
TPFSetTileBit(tpf, tile, 14);
|
2008-01-23 18:24:04 +00:00
|
|
|
TPFSetTileBit(tpf, endtile, 14);
|
|
|
|
|
|
|
|
tile = endtile;
|
|
|
|
} else {
|
|
|
|
/* leaving tunnel / bridge? */
|
|
|
|
if (ReverseDiagDir(dir) != direction) return;
|
2008-01-23 17:30:28 +00:00
|
|
|
}
|
2008-01-23 19:31:11 +00:00
|
|
|
} else {
|
|
|
|
/* can we leave tile in this dir? */
|
|
|
|
if (!CanAccessTileInDir(tile, direction, tpf->tracktype)) return;
|
2008-01-23 17:30:28 +00:00
|
|
|
}
|
2008-01-23 18:24:04 +00:00
|
|
|
|
2008-01-23 17:30:28 +00:00
|
|
|
tile += TileOffsByDiagDir(direction);
|
|
|
|
|
2008-01-23 19:31:11 +00:00
|
|
|
/* can we enter tile in this dir? */
|
|
|
|
if (!CanAccessTileInDir(tile, ReverseDiagDir(direction), tpf->tracktype)) return;
|
|
|
|
|
2007-01-01 15:04:35 +00:00
|
|
|
/* Check if the new tile is a tunnel or bridge head and that the direction
|
|
|
|
* and transport type match */
|
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) {
|
2007-12-16 19:30:42 +00:00
|
|
|
if (GetTunnelBridgeDirection(tile) != direction ||
|
|
|
|
GetTunnelBridgeTransportType(tile) != tpf->tracktype) {
|
|
|
|
return;
|
2007-01-01 15:04:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-03 14:21:17 +00:00
|
|
|
TrackdirBits trackdirbits = TrackStatusToTrackdirBits(GetTileTrackStatus(tile, tpf->tracktype, tpf->sub_type));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-23 20:02:30 +00:00
|
|
|
/* Check in case of rail if the owner is the same */
|
|
|
|
if (tpf->tracktype == TRANSPORT_RAIL) {
|
2008-04-03 14:21:17 +00:00
|
|
|
if (trackdirbits != TRACKDIR_BIT_NONE && TrackStatusToTrackdirBits(GetTileTrackStatus(tile_org, TRANSPORT_RAIL, 0)) != TRACKDIR_BIT_NONE) {
|
2008-01-23 20:02:30 +00:00
|
|
|
if (GetTileOwner(tile_org) != GetTileOwner(tile)) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tpf->rd.cur_length++;
|
2007-01-01 14:53:57 +00:00
|
|
|
|
2008-04-03 14:21:17 +00:00
|
|
|
trackdirbits &= DiagdirReachesTrackdirs(direction);
|
|
|
|
TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-03 14:21:17 +00:00
|
|
|
if (bits != TRACK_BIT_NONE) {
|
2007-11-04 23:06:39 +00:00
|
|
|
if (!tpf->disable_tile_hash || (tpf->rd.cur_length <= 64 && (KillFirstBit(bits) == 0 || ++tpf->rd.depth <= 7))) {
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2008-04-03 14:21:17 +00:00
|
|
|
Track track = RemoveFirstTrack(&bits);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-03 14:21:17 +00:00
|
|
|
tpf->the_dir = TrackEnterdirToTrackdir(track, direction);
|
2007-06-26 06:57:14 +00:00
|
|
|
RememberData rd = tpf->rd;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-02-04 14:08:02 +00:00
|
|
|
/* make sure we are not leaving from invalid side */
|
|
|
|
if (TPFSetTileBit(tpf, tile, tpf->the_dir) && CanAccessTileInDir(tile, TrackdirToExitdir(tpf->the_dir), tpf->tracktype) &&
|
2008-02-20 15:13:42 +00:00
|
|
|
!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length) ) {
|
2008-04-03 14:21:17 +00:00
|
|
|
TPFModeNormal(tpf, tile, TrackdirToExitdir(tpf->the_dir));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
tpf->rd = rd;
|
2008-04-03 14:21:17 +00:00
|
|
|
} while (bits != TRACK_BIT_NONE);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-26 06:57:14 +00:00
|
|
|
}
|
|
|
|
|
2008-04-02 13:57:25 +00:00
|
|
|
void FollowTrack(TileIndex tile, PathfindFlags flags, TransportType tt, uint sub_type, DiagDirection direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-01 21:12:51 +00:00
|
|
|
assert(IsValidDiagDirection(direction));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-01 21:12:51 +00:00
|
|
|
SmallStackSafeStackAlloc<TrackPathFinder, 1> tpf;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
/* initialize path finder variables */
|
2008-04-01 21:12:51 +00:00
|
|
|
tpf->userdata = data;
|
|
|
|
tpf->enum_proc = enum_proc;
|
|
|
|
tpf->new_link = tpf->links;
|
|
|
|
tpf->num_links_left = lengthof(tpf->links);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-01 21:12:51 +00:00
|
|
|
tpf->rd.cur_length = 0;
|
|
|
|
tpf->rd.depth = 0;
|
|
|
|
tpf->rd.last_choosen_track = INVALID_TRACK;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-04-02 13:57:25 +00:00
|
|
|
tpf->disable_tile_hash = (flags & PATHFIND_FLAGS_DISABLE_TILE_HASH) != 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-02 13:57:25 +00:00
|
|
|
tpf->tracktype = tt;
|
2008-04-01 21:12:51 +00:00
|
|
|
tpf->sub_type = sub_type;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-02 13:57:25 +00:00
|
|
|
if ((flags & PATHFIND_FLAGS_SHIP_MODE) != 0) {
|
2008-04-01 21:12:51 +00:00
|
|
|
tpf->enum_proc(tile, data, INVALID_TRACKDIR, 0);
|
2008-04-02 13:57:25 +00:00
|
|
|
TPFModeShip(tpf, tile, direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
/* clear the hash_heads */
|
2008-04-01 21:12:51 +00:00
|
|
|
memset(tpf->hash_head, 0, sizeof(tpf->hash_head));
|
2008-04-02 13:57:25 +00:00
|
|
|
TPFModeNormal(tpf, tile, direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-04-01 21:12:51 +00:00
|
|
|
if (after_proc != NULL) after_proc(tpf);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct StackedItem {
|
2004-08-09 17:04:08 +00:00
|
|
|
TileIndex tile;
|
2007-03-21 17:42:43 +00:00
|
|
|
uint16 cur_length; ///< This is the current length to this tile.
|
|
|
|
uint16 priority; ///< This is the current length + estimated length to the goal.
|
2007-01-10 18:56:51 +00:00
|
|
|
TrackdirByte track;
|
2004-08-09 17:04:08 +00:00
|
|
|
byte depth;
|
|
|
|
byte state;
|
|
|
|
byte first_track;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct HashLink {
|
2004-08-09 17:04:08 +00:00
|
|
|
TileIndex tile;
|
|
|
|
uint16 typelength;
|
|
|
|
uint16 next;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct NewTrackPathFinder {
|
2005-07-19 11:42:40 +00:00
|
|
|
NTPEnumProc *enum_proc;
|
2004-08-09 17:04:08 +00:00
|
|
|
void *userdata;
|
2005-07-19 11:42:40 +00:00
|
|
|
TileIndex dest;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-03-29 16:30:26 +00:00
|
|
|
TransportType tracktype;
|
2008-01-09 21:05:03 +00:00
|
|
|
RailTypes railtypes;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint maxlength;
|
|
|
|
|
|
|
|
HashLink *new_link;
|
|
|
|
uint num_links_left;
|
|
|
|
|
2005-01-10 22:14:35 +00:00
|
|
|
uint nstack;
|
2007-03-21 17:42:43 +00:00
|
|
|
StackedItem stack[256]; ///< priority queue of stacked items
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
uint16 hash_head[0x400]; ///< hash heads. 0 means unused. 0xFFFC = length, 0x3 = dir
|
|
|
|
TileIndex hash_tile[0x400]; ///< tiles. or links.
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
HashLink links[0x400]; ///< hash links
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
#define NTP_GET_LINK_OFFS(tpf, link) ((byte*)(link) - (byte*)tpf->links)
|
|
|
|
#define NTP_GET_LINK_PTR(tpf, link_offs) (HashLink*)((byte*)tpf->links + (link_offs))
|
|
|
|
|
|
|
|
#define ARR(i) tpf->stack[(i)-1]
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/** called after a new element was added in the queue at the last index.
|
|
|
|
* move it down to the proper position */
|
2004-12-03 07:43:00 +00:00
|
|
|
static inline void HeapifyUp(NewTrackPathFinder *tpf)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
StackedItem si;
|
|
|
|
int i = ++tpf->nstack;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-07-19 11:42:40 +00:00
|
|
|
while (i != 1 && ARR(i).priority < ARR(i>>1).priority) {
|
2007-03-21 17:42:43 +00:00
|
|
|
/* 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;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/** called after the element 0 was eaten. fill it with a new element */
|
2004-12-03 07:43:00 +00:00
|
|
|
static inline void HeapifyDown(NewTrackPathFinder *tpf)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
StackedItem si;
|
|
|
|
int i = 1, j;
|
2005-01-10 22:14:35 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
assert(tpf->nstack > 0);
|
|
|
|
n = --tpf->nstack;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (n == 0) return; // heap is empty so nothing to do?
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* copy the last item to index 0. we use it as base for heapify. */
|
|
|
|
ARR(1) = ARR(n + 1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
while ((j = i * 2) <= n) {
|
|
|
|
/* figure out which is smaller of the children. */
|
|
|
|
if (j != n && ARR(j).priority > ARR(j + 1).priority)
|
2004-08-09 17:04:08 +00:00
|
|
|
j++; // right item is smaller
|
|
|
|
|
|
|
|
assert(i <= n && j <= n);
|
2005-07-19 11:42:40 +00:00
|
|
|
if (ARR(i).priority <= ARR(j).priority)
|
2004-08-09 17:04:08 +00:00
|
|
|
break; // base elem smaller than smallest, done!
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* swap parent with the child */
|
2004-08-09 17:04:08 +00:00
|
|
|
si = ARR(i); ARR(i) = ARR(j); ARR(j) = si;
|
|
|
|
i = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/** 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. */
|
2006-03-06 20:28:28 +00:00
|
|
|
static bool NtpVisit(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection dir, uint length)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
uint hash,head;
|
|
|
|
HashLink *link, *new_link;
|
|
|
|
|
2005-07-12 20:28:19 +00:00
|
|
|
assert(length < 16384-1);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
hash = PATHFIND_HASH_TILE(tile);
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* never visited before? */
|
2004-08-09 17:04:08 +00:00
|
|
|
if ((head=tpf->hash_head[hash]) == 0) {
|
|
|
|
tpf->hash_tile[hash] = tile;
|
|
|
|
tpf->hash_head[hash] = dir | (length << 2);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (head != 0xffff) {
|
2007-01-10 18:56:51 +00:00
|
|
|
if (tile == tpf->hash_tile[hash] && (head & 0x3) == (uint)dir) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* longer length */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (length >= (head >> 2)) return false;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
tpf->hash_head[hash] = dir | (length << 2);
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-21 17:42:43 +00:00
|
|
|
/* 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. */
|
2005-07-19 11:42:40 +00:00
|
|
|
if (tpf->num_links_left == 0) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ntp, 1, "No links left");
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
2005-07-19 11:42:40 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
tpf->num_links_left--;
|
|
|
|
link = tpf->new_link++;
|
|
|
|
|
|
|
|
/* move the data that was previously in the hash_??? variables
|
|
|
|
* to the link struct, and let the hash variables point to the link */
|
|
|
|
link->tile = tpf->hash_tile[hash];
|
|
|
|
tpf->hash_tile[hash] = NTP_GET_LINK_OFFS(tpf, link);
|
|
|
|
|
|
|
|
link->typelength = tpf->hash_head[hash];
|
2007-03-21 17:42:43 +00:00
|
|
|
tpf->hash_head[hash] = 0xFFFF; // multi link
|
2004-08-09 17:04:08 +00:00
|
|
|
link->next = 0xFFFF;
|
|
|
|
} else {
|
2007-03-21 17:42:43 +00:00
|
|
|
/* a linked list of many tiles,
|
|
|
|
* find the one corresponding to the tile, if it exists.
|
|
|
|
* otherwise make a new link */
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
uint offs = tpf->hash_tile[hash];
|
|
|
|
do {
|
|
|
|
link = NTP_GET_LINK_PTR(tpf, offs);
|
2007-01-10 18:56:51 +00:00
|
|
|
if (tile == link->tile && (link->typelength & 0x3U) == (uint)dir) {
|
2006-02-13 21:56:49 +00:00
|
|
|
if (length >= (uint)(link->typelength >> 2)) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
link->typelength = dir | (length << 2);
|
|
|
|
return true;
|
|
|
|
}
|
2006-02-13 21:15:00 +00:00
|
|
|
} while ((offs = link->next) != 0xFFFF);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* get here if we need to add a new link to link,
|
|
|
|
* first, allocate a new link, in the same way as before */
|
2005-07-19 11:42:40 +00:00
|
|
|
if (tpf->num_links_left == 0) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ntp, 1, "No links left");
|
2005-07-19 11:42:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
tpf->num_links_left--;
|
|
|
|
new_link = tpf->new_link++;
|
|
|
|
|
|
|
|
/* then fill the link with the new info, and establish a ptr from the old
|
|
|
|
* link to the new one */
|
2005-06-27 06:57:24 +00:00
|
|
|
new_link->tile = tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
new_link->typelength = dir | (length << 2);
|
|
|
|
new_link->next = 0xFFFF;
|
|
|
|
|
|
|
|
link->next = NTP_GET_LINK_OFFS(tpf, new_link);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-12-21 13:53:44 +00:00
|
|
|
/**
|
|
|
|
* Checks if the shortest path to the given tile/dir so far is still the given
|
|
|
|
* length.
|
|
|
|
* @return true if the length is still the same
|
|
|
|
* @pre The given tile/dir combination should be present in the hash, by a
|
|
|
|
* previous call to NtpVisit().
|
|
|
|
*/
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool NtpCheck(NewTrackPathFinder *tpf, TileIndex tile, uint dir, uint length)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
uint hash,head,offs;
|
|
|
|
HashLink *link;
|
|
|
|
|
|
|
|
hash = PATHFIND_HASH_TILE(tile);
|
|
|
|
head=tpf->hash_head[hash];
|
|
|
|
assert(head);
|
|
|
|
|
|
|
|
if (head != 0xffff) {
|
|
|
|
assert( tpf->hash_tile[hash] == tile && (head & 3) == dir);
|
|
|
|
assert( (head >> 2) <= length);
|
|
|
|
return length == (head >> 2);
|
|
|
|
}
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* else it's a linked list of many tiles */
|
2004-08-09 17:04:08 +00:00
|
|
|
offs = tpf->hash_tile[hash];
|
2006-02-01 07:36:15 +00:00
|
|
|
for (;;) {
|
2004-08-09 17:04:08 +00:00
|
|
|
link = NTP_GET_LINK_PTR(tpf, offs);
|
2006-02-13 21:15:00 +00:00
|
|
|
if (tile == link->tile && (link->typelength & 0x3U) == dir) {
|
2006-02-13 21:56:49 +00:00
|
|
|
assert((uint)(link->typelength >> 2) <= length);
|
|
|
|
return length == (uint)(link->typelength >> 2);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
offs = link->next;
|
|
|
|
assert(offs != 0xffff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-19 11:42:40 +00:00
|
|
|
static uint DistanceMoo(TileIndex t0, TileIndex t1)
|
|
|
|
{
|
2007-11-26 16:01:29 +00:00
|
|
|
const uint dx = Delta(TileX(t0), TileX(t1));
|
|
|
|
const uint dy = Delta(TileY(t0), TileY(t1));
|
2005-07-19 11:42:40 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
const uint straightTracks = 2 * min(dx, dy); // The number of straight (not full length) tracks
|
2005-07-19 11:42:40 +00:00
|
|
|
/* OPTIMISATION:
|
|
|
|
* Original: diagTracks = max(dx, dy) - min(dx,dy);
|
|
|
|
* Proof:
|
|
|
|
* (dx-dy) - straightTracks == (min + max) - straightTracks = min + // max - 2 * min = max - min */
|
2007-03-21 17:42:43 +00:00
|
|
|
const uint diagTracks = dx + dy - straightTracks; // The number of diagonal (full tile length) tracks.
|
2005-07-19 11:42:40 +00:00
|
|
|
|
|
|
|
return diagTracks*DIAG_FACTOR + straightTracks*STR_FACTOR;
|
|
|
|
}
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* These has to be small cause the max length of a track
|
|
|
|
* is currently limited to 16384 */
|
2005-07-19 11:42:40 +00:00
|
|
|
|
|
|
|
static const byte _length_of_track[16] = {
|
2006-08-22 14:38:37 +00:00
|
|
|
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
|
2005-07-19 11:42:40 +00:00
|
|
|
};
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* new more optimized pathfinder for trains...
|
|
|
|
* Tile is the tile the train is at.
|
|
|
|
* direction is the tile the train is moving towards. */
|
2005-07-19 11:42:40 +00:00
|
|
|
|
2006-03-06 20:28:28 +00:00
|
|
|
static void NTPEnum(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-12-21 13:53:44 +00:00
|
|
|
TrackBits bits, allbits;
|
2007-01-10 18:56:51 +00:00
|
|
|
Trackdir track;
|
2005-12-21 13:53:44 +00:00
|
|
|
TileIndex tile_org;
|
2004-08-09 17:04:08 +00:00
|
|
|
StackedItem si;
|
2005-07-19 11:42:40 +00:00
|
|
|
int estimation;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-08-01 16:31:19 +00:00
|
|
|
|
2005-07-20 09:17:20 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Need to have a special case for the start.
|
|
|
|
* We shouldn't call the callback for the current tile. */
|
2005-07-19 11:42:40 +00:00
|
|
|
si.cur_length = 1; // Need to start at 1 cause 0 is a reserved value.
|
2004-08-09 17:04:08 +00:00
|
|
|
si.depth = 0;
|
|
|
|
si.state = 0;
|
2005-07-20 09:17:20 +00:00
|
|
|
si.first_track = 0xFF;
|
2005-07-19 11:42:40 +00:00
|
|
|
goto start_at;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
for (;;) {
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Get the next item to search from from the priority queue */
|
2005-07-19 11:42:40 +00:00
|
|
|
do {
|
|
|
|
if (tpf->nstack == 0)
|
|
|
|
return; // nothing left? then we're done!
|
|
|
|
si = tpf->stack[0];
|
|
|
|
tile = si.tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-19 11:42:40 +00:00
|
|
|
HeapifyDown(tpf);
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Make sure we havn't already visited this tile. */
|
2008-04-03 14:21:17 +00:00
|
|
|
} while (!NtpCheck(tpf, tile, ReverseDiagDir(TrackdirToExitdir(ReverseTrackdir(si.track))), si.cur_length));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Add the length of this track. */
|
2005-07-19 11:42:40 +00:00
|
|
|
si.cur_length += _length_of_track[si.track];
|
2005-07-12 20:28:19 +00:00
|
|
|
|
2005-07-19 11:42:40 +00:00
|
|
|
callback_and_continue:
|
|
|
|
if (tpf->enum_proc(tile, tpf->userdata, si.first_track, si.cur_length))
|
|
|
|
return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-07-20 09:17:20 +00:00
|
|
|
assert(si.track <= 13);
|
2008-04-03 14:21:17 +00:00
|
|
|
direction = TrackdirToExitdir(si.track);
|
2005-07-19 11:42:40 +00:00
|
|
|
|
|
|
|
start_at:
|
2007-03-21 17:42:43 +00:00
|
|
|
/* 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. */
|
2006-12-27 12:38:02 +00:00
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) {
|
2008-03-07 00:47:42 +00:00
|
|
|
if (GetTunnelBridgeDirection(tile) != ReverseDiagDir(direction)) {
|
|
|
|
/* We are not just driving out of the tunnel/bridge */
|
|
|
|
if (GetTunnelBridgeDirection(tile) != direction ||
|
|
|
|
GetTunnelBridgeTransportType(tile) != tpf->tracktype) {
|
|
|
|
/* We are not driving into the tunnel/bridge, or it is an invalid tunnel/bridge */
|
|
|
|
continue;
|
2006-12-27 12:38:02 +00:00
|
|
|
}
|
2008-03-07 00:47:42 +00:00
|
|
|
if (!HasBit(tpf->railtypes, GetRailType(tile))) {
|
|
|
|
bits = TRACK_BIT_NONE;
|
|
|
|
break;
|
2006-12-27 12:38:02 +00:00
|
|
|
}
|
2008-03-07 00:47:42 +00:00
|
|
|
|
|
|
|
TileIndex endtile = GetOtherTunnelBridgeEnd(tile);
|
|
|
|
si.cur_length += DIAG_FACTOR * (GetTunnelBridgeLength(tile, endtile) + 1);
|
|
|
|
tile = endtile;
|
|
|
|
/* tile now points to the exit tile of the tunnel/bridge */
|
2006-05-10 09:38:29 +00:00
|
|
|
}
|
2005-07-12 20:28:19 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* This is a special loop used to go through
|
|
|
|
* a rail net and find the first intersection */
|
2005-07-19 11:42:40 +00:00
|
|
|
tile_org = tile;
|
2006-02-01 07:36:15 +00:00
|
|
|
for (;;) {
|
2005-07-20 09:17:20 +00:00
|
|
|
assert(direction <= 3);
|
2006-09-05 23:21:41 +00:00
|
|
|
tile += TileOffsByDiagDir(direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* too long search length? bail out. */
|
2005-07-19 11:42:40 +00:00
|
|
|
if (si.cur_length >= tpf->maxlength) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ntp, 1, "Cur_length too big");
|
2007-01-10 18:56:51 +00:00
|
|
|
bits = TRACK_BIT_NONE;
|
2005-07-19 11:42:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Not a regular rail tile?
|
|
|
|
* Then we can't use the code below, but revert to more general code. */
|
2005-07-19 11:42:40 +00:00
|
|
|
if (!IsTileType(tile, MP_RAILWAY) || !IsPlainRailTile(tile)) {
|
2007-03-21 17:42:43 +00:00
|
|
|
/* We found a tile which is not a normal railway tile.
|
|
|
|
* Determine which tracks that exist on this tile. */
|
2008-04-03 14:21:17 +00:00
|
|
|
bits = TrackdirBitsToTrackBits(TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_RAIL, 0)) & DiagdirReachesTrackdirs(direction));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Check that the tile contains exactly one track */
|
2007-11-04 23:06:39 +00:00
|
|
|
if (bits == 0 || KillFirstBit(bits) != 0) break;
|
2005-07-12 20:28:19 +00:00
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(tpf->railtypes, GetRailType(tile))) {
|
2007-01-10 18:56:51 +00:00
|
|
|
bits = TRACK_BIT_NONE;
|
2006-12-27 12:38:02 +00:00
|
|
|
break;
|
2006-05-10 09:38:29 +00:00
|
|
|
}
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/*******************
|
|
|
|
* 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?
|
|
|
|
*******************/
|
2008-04-03 14:21:17 +00:00
|
|
|
si.track = TrackEnterdirToTrackdir(FindFirstTrack(bits), direction);
|
2005-07-19 11:42:40 +00:00
|
|
|
si.cur_length += _length_of_track[si.track];
|
|
|
|
goto callback_and_continue;
|
|
|
|
}
|
2005-07-12 20:28:19 +00:00
|
|
|
|
2005-12-21 13:53:44 +00:00
|
|
|
/* Regular rail tile, determine which tracks exist. */
|
2006-03-19 12:06:12 +00:00
|
|
|
allbits = GetTrackBits(tile);
|
2005-12-21 13:53:44 +00:00
|
|
|
/* Which tracks are reachable? */
|
|
|
|
bits = allbits & DiagdirReachesTracks(direction);
|
2005-07-20 09:31:46 +00:00
|
|
|
|
2005-12-21 13:53:44 +00:00
|
|
|
/* The tile has no reachable tracks => End of rail segment
|
|
|
|
* or Intersection => End of rail segment. We check this agains all the
|
|
|
|
* bits, not just reachable ones, to prevent infinite loops. */
|
2007-01-10 18:56:51 +00:00
|
|
|
if (bits == TRACK_BIT_NONE || TracksOverlap(allbits)) break;
|
2005-07-20 09:31:46 +00:00
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(tpf->railtypes, GetRailType(tile))) {
|
2007-01-10 18:56:51 +00:00
|
|
|
bits = TRACK_BIT_NONE;
|
2006-03-29 16:30:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-12-21 13:53:44 +00:00
|
|
|
/* If we reach here, the tile has exactly one track, and this
|
2007-04-18 22:10:36 +00:00
|
|
|
track is reachable = > Rail segment continues */
|
2005-07-12 20:28:19 +00:00
|
|
|
|
2008-04-03 14:21:17 +00:00
|
|
|
track = TrackEnterdirToTrackdir(FindFirstTrack(bits), direction);
|
2007-01-10 18:56:51 +00:00
|
|
|
assert(track != INVALID_TRACKDIR);
|
2005-07-20 09:31:46 +00:00
|
|
|
|
2005-07-19 11:42:40 +00:00
|
|
|
si.cur_length += _length_of_track[track];
|
2005-07-12 20:28:19 +00:00
|
|
|
|
2008-02-28 17:34:34 +00:00
|
|
|
/* Check if this rail is an upwards slope. If it is, then add a penalty. */
|
|
|
|
if (IsDiagonalTrackdir(track) && IsUphillTrackdir(GetTileSlope(tile, NULL), track)) {
|
2005-07-19 11:42:40 +00:00
|
|
|
// upwards slope. add some penalty.
|
2007-03-21 17:42:43 +00:00
|
|
|
si.cur_length += 4 * DIAG_FACTOR;
|
2005-07-19 11:42:40 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* railway tile with signals..? */
|
2005-07-19 11:42:40 +00:00
|
|
|
if (HasSignals(tile)) {
|
2006-09-22 21:52:31 +00:00
|
|
|
if (!HasSignalOnTrackdir(tile, track)) {
|
2007-03-21 17:42:43 +00:00
|
|
|
/* if one way signal not pointing towards us, stop going in this direction => End of rail segment. */
|
2008-08-02 22:49:10 +00:00
|
|
|
if (HasSignalOnTrackdir(tile, ReverseTrackdir(track)) && IsOnewaySignal(tile, TrackdirToTrack(track))) {
|
2007-01-10 18:56:51 +00:00
|
|
|
bits = TRACK_BIT_NONE;
|
2005-07-19 11:42:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-09-22 21:52:31 +00:00
|
|
|
} else if (GetSignalStateByTrackdir(tile, track) == SIGNAL_STATE_GREEN) {
|
2007-03-21 17:42:43 +00:00
|
|
|
/* green signal in our direction. either one way or two way. */
|
2005-07-19 11:42:40 +00:00
|
|
|
si.state |= 3;
|
|
|
|
} else {
|
2007-03-21 17:42:43 +00:00
|
|
|
/* reached a red signal. */
|
2006-09-22 21:52:31 +00:00
|
|
|
if (HasSignalOnTrackdir(tile, ReverseTrackdir(track))) {
|
2007-03-21 17:42:43 +00:00
|
|
|
/* 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)) {
|
2007-01-10 18:56:51 +00:00
|
|
|
bits = TRACK_BIT_NONE;
|
2005-07-19 11:42:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(si.state & 2)) {
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Is this the first signal we see? And it's red... add penalty */
|
|
|
|
si.cur_length += 10 * DIAG_FACTOR;
|
2005-07-19 11:42:40 +00:00
|
|
|
si.state += 2; // remember that we added penalty.
|
2007-03-21 17:42:43 +00:00
|
|
|
/* 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. */
|
2005-07-19 11:42:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-19 11:42:40 +00:00
|
|
|
if (tpf->enum_proc(tile, tpf->userdata, si.first_track, si.cur_length))
|
2007-03-21 17:42:43 +00:00
|
|
|
return; // Don't process this tile any further
|
2005-07-19 11:42:40 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* continue with the next track */
|
2008-04-03 14:21:17 +00:00
|
|
|
direction = TrackdirToExitdir(track);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* safety check if we're running around chasing our tail... (infinite loop) */
|
2005-07-19 11:42:40 +00:00
|
|
|
if (tile == tile_org) {
|
2007-01-10 18:56:51 +00:00
|
|
|
bits = TRACK_BIT_NONE;
|
2005-07-19 11:42:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* There are no tracks to choose between.
|
|
|
|
* Stop searching in this direction */
|
2007-01-10 18:56:51 +00:00
|
|
|
if (bits == TRACK_BIT_NONE)
|
2005-07-19 11:42:40 +00:00
|
|
|
continue;
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/****************
|
|
|
|
* We got multiple tracks to choose between (intersection).
|
|
|
|
* Branch the search space into several branches.
|
|
|
|
****************/
|
2005-07-19 11:42:40 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* 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. */
|
2005-07-19 11:42:40 +00:00
|
|
|
if (!NtpVisit(tpf, tile, direction, si.cur_length))
|
|
|
|
continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Push all possible alternatives that we can reach from here
|
|
|
|
* onto the priority heap.
|
|
|
|
* 'bits' contains the tracks that we can choose between. */
|
2005-07-19 11:42:40 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* First compute the estimated distance to the target.
|
|
|
|
* This is used to implement A* */
|
2005-07-19 11:42:40 +00:00
|
|
|
estimation = 0;
|
|
|
|
if (tpf->dest != 0)
|
|
|
|
estimation = DistanceMoo(tile, tpf->dest);
|
|
|
|
|
|
|
|
si.depth++;
|
2005-12-20 00:50:16 +00:00
|
|
|
if (si.depth == 0)
|
2007-03-21 17:42:43 +00:00
|
|
|
continue; // We overflowed our depth. No more searching in this direction.
|
2004-08-09 17:04:08 +00:00
|
|
|
si.tile = tile;
|
2007-01-10 18:56:51 +00:00
|
|
|
while (bits != TRACK_BIT_NONE) {
|
2007-01-11 10:34:42 +00:00
|
|
|
Track track = RemoveFirstTrack(&bits);
|
2008-04-03 14:21:17 +00:00
|
|
|
si.track = TrackEnterdirToTrackdir(track, direction);
|
2005-12-21 13:53:44 +00:00
|
|
|
assert(si.track != 0xFF);
|
2005-07-19 11:42:40 +00:00
|
|
|
si.priority = si.cur_length + estimation;
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* out of stack items, bail out? */
|
2005-07-12 20:28:19 +00:00
|
|
|
if (tpf->nstack >= lengthof(tpf->stack)) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ntp, 1, "Out of stack");
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
2005-07-12 20:28:19 +00:00
|
|
|
}
|
2005-07-19 11:42:40 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
tpf->stack[tpf->nstack] = si;
|
|
|
|
HeapifyUp(tpf);
|
2007-01-10 18:56:51 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* 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. */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (si.depth == 1) {
|
2005-07-19 11:42:40 +00:00
|
|
|
assert(tpf->nstack == 1 || tpf->nstack == 2 || tpf->nstack == 3);
|
|
|
|
if (tpf->nstack != 1) {
|
|
|
|
uint32 r = Random();
|
2007-01-19 11:47:48 +00:00
|
|
|
if (r & 1) Swap(tpf->stack[0].track, tpf->stack[1].track);
|
2005-07-19 11:42:40 +00:00
|
|
|
if (tpf->nstack != 2) {
|
2007-01-10 18:56:51 +00:00
|
|
|
TrackdirByte t = tpf->stack[2].track;
|
2007-01-19 11:47:48 +00:00
|
|
|
if (r & 2) Swap(tpf->stack[0].track, t);
|
|
|
|
if (r & 4) Swap(tpf->stack[1].track, t);
|
2005-07-19 11:42:40 +00:00
|
|
|
tpf->stack[2].first_track = tpf->stack[2].track = t;
|
|
|
|
}
|
|
|
|
tpf->stack[0].first_track = tpf->stack[0].track;
|
|
|
|
tpf->stack[1].first_track = tpf->stack[1].track;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Continue with the next from the queue... */
|
2005-07-19 11:42:40 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/** new pathfinder for trains. better and faster. */
|
2008-01-09 21:05:03 +00:00
|
|
|
void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypes railtypes, DiagDirection direction, NTPEnumProc* enum_proc, void* data)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-01 21:12:51 +00:00
|
|
|
SmallStackSafeStackAlloc<NewTrackPathFinder, 1> tpf;
|
|
|
|
|
|
|
|
tpf->dest = dest;
|
|
|
|
tpf->userdata = data;
|
|
|
|
tpf->enum_proc = enum_proc;
|
|
|
|
tpf->tracktype = TRANSPORT_RAIL;
|
|
|
|
tpf->railtypes = railtypes;
|
2008-05-29 15:13:28 +00:00
|
|
|
tpf->maxlength = min(_settings_game.pf.opf.pf_maxlength * 3, 10000);
|
2008-04-01 21:12:51 +00:00
|
|
|
tpf->nstack = 0;
|
|
|
|
tpf->new_link = tpf->links;
|
|
|
|
tpf->num_links_left = lengthof(tpf->links);
|
|
|
|
memset(tpf->hash_head, 0, sizeof(tpf->hash_head));
|
|
|
|
|
|
|
|
NTPEnum(tpf, tile, direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|