2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file pathfind.h The oldest pathfinder that's supported. */
|
2007-03-21 17:42:43 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#ifndef PATHFIND_H
|
|
|
|
#define PATHFIND_H
|
|
|
|
|
2007-12-18 19:52:14 +00:00
|
|
|
#include "direction_type.h"
|
2009-07-22 11:35:35 +00:00
|
|
|
#include "station_base.h"
|
2009-07-24 15:18:25 +00:00
|
|
|
#include "waypoint_base.h"
|
2006-03-06 20:28:28 +00:00
|
|
|
|
2006-08-26 21:54:04 +00:00
|
|
|
enum {
|
|
|
|
STR_FACTOR = 2,
|
|
|
|
DIAG_FACTOR = 3
|
|
|
|
};
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
//#define PF_BENCH // perform simple benchmarks on the train pathfinder (not
|
|
|
|
//supported on all archs)
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct TrackPathFinder;
|
2008-02-20 15:13:42 +00:00
|
|
|
typedef bool TPFEnumProc(TileIndex tile, void *data, Trackdir trackdir, uint length);
|
2004-08-09 17:04:08 +00:00
|
|
|
typedef void TPFAfterProc(TrackPathFinder *tpf);
|
|
|
|
|
2005-07-19 11:42:40 +00:00
|
|
|
typedef bool NTPEnumProc(TileIndex tile, void *data, int track, uint length);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
#define PATHFIND_GET_LINK_OFFS(tpf, link) ((byte*)(link) - (byte*)tpf->links)
|
|
|
|
#define PATHFIND_GET_LINK_PTR(tpf, link_offs) (TrackPathFinderLink*)((byte*)tpf->links + (link_offs))
|
|
|
|
|
|
|
|
/* y7 y6 y5 y4 y3 y2 y1 y0 x7 x6 x5 x4 x3 x2 x1 x0
|
|
|
|
* y7 y6 y5 y4 y3 y2 y1 y0 x4 x3 x2 x1 x0 0 0 0
|
|
|
|
* 0 0 y7 y6 y5 y4 y3 y2 y1 y0 x4 x3 x2 x1 x0 0
|
|
|
|
* 0 0 0 0 y5 y4 y3 y2 y1 y0 x4 x3 x2 x1 x0 0
|
|
|
|
*/
|
2005-01-07 17:02:43 +00:00
|
|
|
#define PATHFIND_HASH_TILE(tile) (TileX(tile) & 0x1F) + ((TileY(tile) & 0x1F) << 5)
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct TrackPathFinderLink {
|
2004-08-09 17:04:08 +00:00
|
|
|
TileIndex tile;
|
|
|
|
uint16 flags;
|
|
|
|
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 RememberData {
|
2004-08-09 17:04:08 +00:00
|
|
|
uint16 cur_length;
|
|
|
|
byte depth;
|
2008-02-20 15:13:42 +00:00
|
|
|
Track last_choosen_track;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
struct TrackPathFinder {
|
|
|
|
int num_links_left;
|
|
|
|
TrackPathFinderLink *new_link;
|
|
|
|
|
|
|
|
TPFEnumProc *enum_proc;
|
|
|
|
|
|
|
|
void *userdata;
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
RememberData rd;
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
TrackdirByte the_dir;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-13 00:28:01 +00:00
|
|
|
TransportType tracktype;
|
2007-05-24 22:41:50 +00:00
|
|
|
uint sub_type;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
bool disable_tile_hash;
|
|
|
|
|
|
|
|
uint16 hash_head[0x400];
|
2007-03-21 17:42:43 +00:00
|
|
|
TileIndex hash_tile[0x400]; ///< stores the link index when multi link.
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
TrackPathFinderLink links[0x400]; ///< hopefully, this is enough.
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2008-04-02 13:57:25 +00:00
|
|
|
/** Some flags to modify the behaviour of original pathfinder */
|
|
|
|
enum PathfindFlags {
|
|
|
|
PATHFIND_FLAGS_NONE = 0,
|
|
|
|
PATHFIND_FLAGS_SHIP_MODE = 0x0800, ///< pathfinder with some optimizations for ships, but does not work for other types.
|
|
|
|
PATHFIND_FLAGS_DISABLE_TILE_HASH = 0x1000, ///< do not check for searching in circles
|
|
|
|
};
|
|
|
|
DECLARE_ENUM_AS_BIT_SET(PathfindFlags)
|
|
|
|
|
2009-01-10 00:31:47 +00:00
|
|
|
void FollowTrack(TileIndex tile, PathfindFlags flags, TransportType tt, uint sub_type, DiagDirection direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data);
|
|
|
|
void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypes railtypes, DiagDirection direction, NTPEnumProc *enum_proc, void *data);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-02-19 00:15:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the tile of given station that is closest to a given tile
|
|
|
|
* for this we assume the station is a rectangle,
|
|
|
|
* as defined by its top tile (st->train_tile) and its width/height (st->trainst_w, st->trainst_h)
|
|
|
|
* @param station The station to calculate the distance to
|
|
|
|
* @param tile The tile from where to calculate the distance
|
|
|
|
* @return The closest station tile to the given tile.
|
|
|
|
*/
|
|
|
|
static inline TileIndex CalcClosestStationTile(StationID station, TileIndex tile)
|
|
|
|
{
|
2009-07-24 15:18:25 +00:00
|
|
|
const BaseStation *bst = BaseStation::Get(station);
|
|
|
|
if (Waypoint::IsExpected(bst)) return bst->xy;
|
|
|
|
|
|
|
|
const Station *st = Station::From(bst);
|
2009-02-19 00:15:36 +00:00
|
|
|
|
|
|
|
/* If the rail station is (temporarily) not present, use the station sign to drive near the station */
|
|
|
|
if (st->train_tile == INVALID_TILE) return st->xy;
|
|
|
|
|
|
|
|
uint minx = TileX(st->train_tile); // topmost corner of station
|
|
|
|
uint miny = TileY(st->train_tile);
|
|
|
|
uint maxx = minx + st->trainst_w - 1; // lowermost corner of station
|
|
|
|
uint maxy = miny + st->trainst_h - 1;
|
|
|
|
|
|
|
|
/* we are going the aim for the x coordinate of the closest corner
|
|
|
|
* but if we are between those coordinates, we will aim for our own x coordinate */
|
|
|
|
uint x = ClampU(TileX(tile), minx, maxx);
|
|
|
|
|
|
|
|
/* same for y coordinate, see above comment */
|
|
|
|
uint y = ClampU(TileY(tile), miny, maxy);
|
|
|
|
|
|
|
|
/* return the tile of our target coordinates */
|
|
|
|
return TileXY(x, y);
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#endif /* PATHFIND_H */
|