2006-05-27 16:12:16 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-02-23 08:37:33 +00:00
|
|
|
/** @file follow_track.hpp Template function for track followers */
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
#ifndef FOLLOW_TRACK_HPP
|
|
|
|
#define FOLLOW_TRACK_HPP
|
|
|
|
|
|
|
|
#include "yapf.hpp"
|
2008-04-17 19:10:30 +00:00
|
|
|
#include "../depot_map.h"
|
2009-05-22 20:22:20 +00:00
|
|
|
#include "../roadveh.h"
|
2009-05-22 22:22:46 +00:00
|
|
|
#include "../train.h"
|
2008-02-25 15:09:22 +00:00
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
/** Track follower helper template class (can serve pathfinders and vehicle
|
2006-09-04 20:40:33 +00:00
|
|
|
* controllers). See 6 different typedefs below for 3 different transport
|
2008-08-02 22:51:53 +00:00
|
|
|
* types w/ or w/o 90-deg turns allowed */
|
|
|
|
template <TransportType Ttr_type_, bool T90deg_turns_allowed_ = true, bool Tmask_reserved_tracks = false>
|
2008-02-25 15:09:22 +00:00
|
|
|
struct CFollowTrackT
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2008-02-25 15:09:22 +00:00
|
|
|
enum ErrorCode {
|
|
|
|
EC_NONE,
|
|
|
|
EC_OWNER,
|
|
|
|
EC_RAIL_TYPE,
|
|
|
|
EC_90DEG,
|
|
|
|
EC_NO_WAY,
|
2008-08-02 22:51:53 +00:00
|
|
|
EC_RESERVED,
|
2008-02-25 15:09:22 +00:00
|
|
|
};
|
|
|
|
|
2008-06-02 06:44:06 +00:00
|
|
|
const Vehicle *m_veh; ///< moving vehicle
|
2008-08-02 22:52:08 +00:00
|
|
|
Owner m_veh_owner; ///< owner of the vehicle
|
2008-02-25 15:09:22 +00:00
|
|
|
TileIndex m_old_tile; ///< the origin (vehicle moved from) before move
|
|
|
|
Trackdir m_old_td; ///< the trackdir (the vehicle was on) before move
|
|
|
|
TileIndex m_new_tile; ///< the new tile (the vehicle has entered)
|
|
|
|
TrackdirBits m_new_td_bits; ///< the new set of available trackdirs
|
|
|
|
DiagDirection m_exitdir; ///< exit direction (leaving the old tile)
|
|
|
|
bool m_is_tunnel; ///< last turn passed tunnel
|
|
|
|
bool m_is_bridge; ///< last turn passed bridge ramp
|
|
|
|
bool m_is_station; ///< last turn passed station
|
|
|
|
int m_tiles_skipped; ///< number of skipped tunnel or station tiles
|
|
|
|
ErrorCode m_err;
|
2008-06-02 06:44:06 +00:00
|
|
|
CPerformanceTimer *m_pPerf;
|
2008-08-02 22:50:38 +00:00
|
|
|
RailTypes m_railtypes;
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2008-08-02 22:50:38 +00:00
|
|
|
FORCEINLINE CFollowTrackT(const Vehicle *v = NULL, RailTypes railtype_override = INVALID_RAILTYPES, CPerformanceTimer *pPerf = NULL)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2008-08-02 22:50:38 +00:00
|
|
|
Init(v, railtype_override, pPerf);
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
|
2008-08-02 22:52:08 +00:00
|
|
|
FORCEINLINE CFollowTrackT(Owner o, RailTypes railtype_override = INVALID_RAILTYPES, CPerformanceTimer *pPerf = NULL)
|
|
|
|
{
|
|
|
|
m_veh = NULL;
|
|
|
|
Init(o, railtype_override, pPerf);
|
|
|
|
}
|
|
|
|
|
2008-08-02 22:50:38 +00:00
|
|
|
FORCEINLINE void Init(const Vehicle *v, RailTypes railtype_override, CPerformanceTimer *pPerf)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2007-03-08 16:27:54 +00:00
|
|
|
assert(!IsRailTT() || (v != NULL && v->type == VEH_TRAIN));
|
2006-05-27 16:12:16 +00:00
|
|
|
m_veh = v;
|
2009-06-06 16:54:22 +00:00
|
|
|
Init(v != NULL ? v->owner : INVALID_OWNER, IsRailTT() && railtype_override == INVALID_RAILTYPES ? Train::From(v)->compatible_railtypes : railtype_override, pPerf);
|
2008-08-02 22:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE void Init(Owner o, RailTypes railtype_override, CPerformanceTimer *pPerf)
|
|
|
|
{
|
|
|
|
assert((!IsRoadTT() || m_veh != NULL) && (!IsRailTT() || railtype_override != INVALID_RAILTYPES));
|
|
|
|
m_veh_owner = o;
|
2006-05-27 16:12:16 +00:00
|
|
|
m_pPerf = pPerf;
|
2009-03-15 00:32:18 +00:00
|
|
|
/* don't worry, all is inlined so compiler should remove unnecessary initializations */
|
2006-05-27 16:12:16 +00:00
|
|
|
m_new_tile = INVALID_TILE;
|
|
|
|
m_new_td_bits = TRACKDIR_BIT_NONE;
|
|
|
|
m_exitdir = INVALID_DIAGDIR;
|
2006-12-27 12:38:02 +00:00
|
|
|
m_is_station = m_is_bridge = m_is_tunnel = false;
|
2006-06-01 21:39:35 +00:00
|
|
|
m_tiles_skipped = 0;
|
2007-07-09 18:27:42 +00:00
|
|
|
m_err = EC_NONE;
|
2008-08-02 22:52:08 +00:00
|
|
|
m_railtypes = railtype_override;
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE static TransportType TT() {return Ttr_type_;}
|
|
|
|
FORCEINLINE static bool IsWaterTT() {return TT() == TRANSPORT_WATER;}
|
|
|
|
FORCEINLINE static bool IsRailTT() {return TT() == TRANSPORT_RAIL;}
|
2009-06-06 16:54:22 +00:00
|
|
|
FORCEINLINE bool IsTram() {return IsRoadTT() && HasBit(RoadVehicle::From(m_veh)->compatible_roadtypes, ROADTYPE_TRAM);}
|
2006-05-27 16:12:16 +00:00
|
|
|
FORCEINLINE static bool IsRoadTT() {return TT() == TRANSPORT_ROAD;}
|
|
|
|
FORCEINLINE static bool Allow90degTurns() {return T90deg_turns_allowed_;}
|
2008-08-11 22:58:30 +00:00
|
|
|
FORCEINLINE static bool DoTrackMasking() {return IsRailTT() && Tmask_reserved_tracks;}
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2008-02-01 17:27:45 +00:00
|
|
|
/** Tests if a tile is a road tile with a single tramtrack (tram can reverse) */
|
|
|
|
FORCEINLINE DiagDirection GetSingleTramBit(TileIndex tile)
|
|
|
|
{
|
2008-09-17 00:32:35 +00:00
|
|
|
assert(IsTram()); // this function shouldn't be called in other cases
|
|
|
|
|
|
|
|
if (IsNormalRoadTile(tile)) {
|
2008-02-01 17:27:45 +00:00
|
|
|
RoadBits rb = GetRoadBits(tile, ROADTYPE_TRAM);
|
|
|
|
switch (rb) {
|
|
|
|
case ROAD_NW: return DIAGDIR_NW;
|
|
|
|
case ROAD_SW: return DIAGDIR_SW;
|
|
|
|
case ROAD_SE: return DIAGDIR_SE;
|
|
|
|
case ROAD_NE: return DIAGDIR_NE;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return INVALID_DIAGDIR;
|
|
|
|
}
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
/** main follower routine. Fills all members and return true on success.
|
2006-09-04 20:40:33 +00:00
|
|
|
* Otherwise returns false if track can't be followed. */
|
2008-08-02 22:50:38 +00:00
|
|
|
inline bool Follow(TileIndex old_tile, Trackdir old_td)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
|
|
|
m_old_tile = old_tile;
|
|
|
|
m_old_td = old_td;
|
2007-07-09 18:27:42 +00:00
|
|
|
m_err = EC_NONE;
|
2009-06-06 16:54:22 +00:00
|
|
|
assert(((TrackStatusToTrackdirBits(GetTileTrackStatus(m_old_tile, TT(), IsRoadTT() && m_veh != NULL ? RoadVehicle::From(m_veh)->compatible_roadtypes : 0)) & TrackdirToTrackdirBits(m_old_td)) != 0) ||
|
2008-09-17 00:32:35 +00:00
|
|
|
(IsTram() && GetSingleTramBit(m_old_tile) != INVALID_DIAGDIR)); // Disable the assertion for single tram bits
|
2006-05-27 16:12:16 +00:00
|
|
|
m_exitdir = TrackdirToExitdir(m_old_td);
|
2008-02-01 17:27:45 +00:00
|
|
|
if (ForcedReverse()) return true;
|
2006-05-27 16:12:16 +00:00
|
|
|
if (!CanExitOldTile()) return false;
|
|
|
|
FollowTileExit();
|
2006-06-07 18:41:58 +00:00
|
|
|
if (!QueryNewTileTrackStatus()) return TryReverse();
|
2006-05-27 16:12:16 +00:00
|
|
|
if (!CanEnterNewTile()) return false;
|
|
|
|
m_new_td_bits &= DiagdirReachesTrackdirs(m_exitdir);
|
2007-07-09 18:27:42 +00:00
|
|
|
if (m_new_td_bits == TRACKDIR_BIT_NONE) {
|
|
|
|
m_err = EC_NO_WAY;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!Allow90degTurns()) {
|
2006-05-27 16:12:16 +00:00
|
|
|
m_new_td_bits &= (TrackdirBits)~(int)TrackdirCrossesTrackdirs(m_old_td);
|
2007-07-09 18:27:42 +00:00
|
|
|
if (m_new_td_bits == TRACKDIR_BIT_NONE) {
|
|
|
|
m_err = EC_90DEG;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2008-08-11 22:58:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool MaskReservedTracks()
|
|
|
|
{
|
|
|
|
if (!DoTrackMasking()) return true;
|
|
|
|
|
|
|
|
if (m_is_station) {
|
|
|
|
/* Check skipped station tiles as well. */
|
|
|
|
TileIndexDiff diff = TileOffsByDiagDir(m_exitdir);
|
|
|
|
for (TileIndex tile = m_new_tile - diff * m_tiles_skipped; tile != m_new_tile; tile += diff) {
|
2009-07-21 22:37:10 +00:00
|
|
|
if (HasStationReservation(tile)) {
|
2008-08-11 22:58:30 +00:00
|
|
|
m_new_td_bits = TRACKDIR_BIT_NONE;
|
|
|
|
m_err = EC_RESERVED;
|
|
|
|
return false;
|
2008-08-08 09:31:50 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-11 22:58:30 +00:00
|
|
|
}
|
2008-08-08 09:31:50 +00:00
|
|
|
|
2008-08-11 22:58:30 +00:00
|
|
|
TrackBits reserved = GetReservedTrackbits(m_new_tile);
|
|
|
|
/* Mask already reserved trackdirs. */
|
|
|
|
m_new_td_bits &= ~TrackBitsToTrackdirBits(reserved);
|
|
|
|
/* Mask out all trackdirs that conflict with the reservation. */
|
|
|
|
uint bits = (uint)TrackdirBitsToTrackBits(m_new_td_bits);
|
|
|
|
int i;
|
|
|
|
FOR_EACH_SET_BIT(i, bits) {
|
|
|
|
if (TracksOverlap(reserved | TrackToTrackBits((Track)i))) m_new_td_bits &= ~TrackToTrackdirBits((Track)i);
|
|
|
|
}
|
|
|
|
if (m_new_td_bits == TRACKDIR_BIT_NONE) {
|
|
|
|
m_err = EC_RESERVED;
|
|
|
|
return false;
|
2008-08-02 22:51:53 +00:00
|
|
|
}
|
2007-07-09 18:27:42 +00:00
|
|
|
return true;
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2006-06-01 21:39:35 +00:00
|
|
|
/** Follow the m_exitdir from m_old_tile and fill m_new_tile and m_tiles_skipped */
|
2006-05-27 16:12:16 +00:00
|
|
|
FORCEINLINE void FollowTileExit()
|
|
|
|
{
|
2006-12-27 12:38:02 +00:00
|
|
|
m_is_station = m_is_bridge = m_is_tunnel = false;
|
|
|
|
m_tiles_skipped = 0;
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* extra handling for tunnels and bridges in our direction */
|
2008-01-23 22:34:04 +00:00
|
|
|
if (IsTileType(m_old_tile, MP_TUNNELBRIDGE)) {
|
|
|
|
DiagDirection enterdir = GetTunnelBridgeDirection(m_old_tile);
|
|
|
|
if (enterdir == m_exitdir) {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* we are entering the tunnel / bridge */
|
2008-01-23 22:34:04 +00:00
|
|
|
if (IsTunnel(m_old_tile)) {
|
|
|
|
m_is_tunnel = true;
|
|
|
|
m_new_tile = GetOtherTunnelEnd(m_old_tile);
|
|
|
|
} else { // IsBridge(m_old_tile)
|
|
|
|
m_is_bridge = true;
|
|
|
|
m_new_tile = GetOtherBridgeEnd(m_old_tile);
|
|
|
|
}
|
|
|
|
m_tiles_skipped = GetTunnelBridgeLength(m_new_tile, m_old_tile);
|
2006-05-27 16:12:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-01-23 22:34:04 +00:00
|
|
|
assert(ReverseDiagDir(enterdir) == m_exitdir);
|
2006-12-27 12:38:02 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* normal or station tile, do one step */
|
2006-09-05 23:21:41 +00:00
|
|
|
TileIndexDiff diff = TileOffsByDiagDir(m_exitdir);
|
2006-05-27 16:12:16 +00:00
|
|
|
m_new_tile = TILE_ADD(m_old_tile, diff);
|
2006-06-01 21:39:35 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* special handling for stations */
|
2009-07-24 11:47:12 +00:00
|
|
|
if (IsRailTT() && IsRailStationTile(m_new_tile)) {
|
2006-06-01 21:39:35 +00:00
|
|
|
m_is_station = true;
|
|
|
|
} else if (IsRoadTT() && IsRoadStopTile(m_new_tile)) {
|
|
|
|
m_is_station = true;
|
|
|
|
} else {
|
|
|
|
m_is_station = false;
|
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** stores track status (available trackdirs) for the new tile into m_new_td_bits */
|
|
|
|
FORCEINLINE bool QueryNewTileTrackStatus()
|
|
|
|
{
|
|
|
|
CPerfStart perf(*m_pPerf);
|
2009-05-18 01:26:23 +00:00
|
|
|
if (IsRailTT() && IsPlainRailTile(m_new_tile)) {
|
2006-05-27 16:12:16 +00:00
|
|
|
m_new_td_bits = (TrackdirBits)(GetTrackBits(m_new_tile) * 0x101);
|
|
|
|
} else {
|
2009-06-06 16:54:22 +00:00
|
|
|
m_new_td_bits = TrackStatusToTrackdirBits(GetTileTrackStatus(m_new_tile, TT(), IsRoadTT() && m_veh != NULL ? RoadVehicle::From(m_veh)->compatible_roadtypes : 0));
|
2008-02-01 17:27:45 +00:00
|
|
|
|
2008-09-17 00:32:35 +00:00
|
|
|
if (IsTram() && m_new_td_bits == 0) {
|
2008-02-01 17:27:45 +00:00
|
|
|
/* GetTileTrackStatus() returns 0 for single tram bits.
|
|
|
|
* As we cannot change it there (easily) without breaking something, change it here */
|
|
|
|
switch (GetSingleTramBit(m_new_tile)) {
|
|
|
|
case DIAGDIR_NE:
|
|
|
|
case DIAGDIR_SW:
|
|
|
|
m_new_td_bits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIAGDIR_NW:
|
|
|
|
case DIAGDIR_SE:
|
|
|
|
m_new_td_bits = TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_SE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
return (m_new_td_bits != TRACKDIR_BIT_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** return true if we can leave m_old_tile in m_exitdir */
|
|
|
|
FORCEINLINE bool CanExitOldTile()
|
|
|
|
{
|
2009-03-15 00:32:18 +00:00
|
|
|
/* road stop can be left at one direction only unless it's a drive-through stop */
|
2007-02-14 16:37:16 +00:00
|
|
|
if (IsRoadTT() && IsStandardRoadStopTile(m_old_tile)) {
|
2006-05-27 16:12:16 +00:00
|
|
|
DiagDirection exitdir = GetRoadStopDir(m_old_tile);
|
2007-07-09 18:27:42 +00:00
|
|
|
if (exitdir != m_exitdir) {
|
|
|
|
m_err = EC_NO_WAY;
|
2006-05-27 16:12:16 +00:00
|
|
|
return false;
|
2007-07-09 18:27:42 +00:00
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
|
2008-02-01 17:27:45 +00:00
|
|
|
/* single tram bits can only be left in one direction */
|
2008-09-17 00:32:35 +00:00
|
|
|
if (IsTram()) {
|
|
|
|
DiagDirection single_tram = GetSingleTramBit(m_old_tile);
|
|
|
|
if (single_tram != INVALID_DIAGDIR && single_tram != m_exitdir) {
|
|
|
|
m_err = EC_NO_WAY;
|
|
|
|
return false;
|
|
|
|
}
|
2008-02-01 17:27:45 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* road depots can be also left in one direction only */
|
2008-04-17 00:44:20 +00:00
|
|
|
if (IsRoadTT() && IsDepotTypeTile(m_old_tile, TT())) {
|
2006-05-27 16:12:16 +00:00
|
|
|
DiagDirection exitdir = GetRoadDepotDirection(m_old_tile);
|
2007-07-09 18:27:42 +00:00
|
|
|
if (exitdir != m_exitdir) {
|
|
|
|
m_err = EC_NO_WAY;
|
2006-05-27 16:12:16 +00:00
|
|
|
return false;
|
2007-07-09 18:27:42 +00:00
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** return true if we can enter m_new_tile from m_exitdir */
|
|
|
|
FORCEINLINE bool CanEnterNewTile()
|
|
|
|
{
|
2007-02-14 16:37:16 +00:00
|
|
|
if (IsRoadTT() && IsStandardRoadStopTile(m_new_tile)) {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* road stop can be entered from one direction only unless it's a drive-through stop */
|
2006-05-27 16:12:16 +00:00
|
|
|
DiagDirection exitdir = GetRoadStopDir(m_new_tile);
|
2007-07-09 18:27:42 +00:00
|
|
|
if (ReverseDiagDir(exitdir) != m_exitdir) {
|
|
|
|
m_err = EC_NO_WAY;
|
2006-05-27 16:12:16 +00:00
|
|
|
return false;
|
2007-07-09 18:27:42 +00:00
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
|
2008-02-01 17:27:45 +00:00
|
|
|
/* single tram bits can only be entered from one direction */
|
2008-09-17 00:32:35 +00:00
|
|
|
if (IsTram()) {
|
|
|
|
DiagDirection single_tram = GetSingleTramBit(m_new_tile);
|
|
|
|
if (single_tram != INVALID_DIAGDIR && single_tram != ReverseDiagDir(m_exitdir)) {
|
|
|
|
m_err = EC_NO_WAY;
|
|
|
|
return false;
|
|
|
|
}
|
2008-02-01 17:27:45 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* road and rail depots can also be entered from one direction only */
|
2008-04-17 00:44:20 +00:00
|
|
|
if (IsRoadTT() && IsDepotTypeTile(m_new_tile, TT())) {
|
2006-05-27 16:12:16 +00:00
|
|
|
DiagDirection exitdir = GetRoadDepotDirection(m_new_tile);
|
2007-07-09 18:27:42 +00:00
|
|
|
if (ReverseDiagDir(exitdir) != m_exitdir) {
|
|
|
|
m_err = EC_NO_WAY;
|
2006-05-27 16:12:16 +00:00
|
|
|
return false;
|
2007-07-09 18:27:42 +00:00
|
|
|
}
|
2009-03-15 00:32:18 +00:00
|
|
|
/* don't try to enter other company's depots */
|
2008-08-02 22:52:08 +00:00
|
|
|
if (GetTileOwner(m_new_tile) != m_veh_owner) {
|
2007-07-09 18:27:42 +00:00
|
|
|
m_err = EC_OWNER;
|
2006-08-26 22:24:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
2008-04-17 00:44:20 +00:00
|
|
|
if (IsRailTT() && IsDepotTypeTile(m_new_tile, TT())) {
|
2006-05-27 16:12:16 +00:00
|
|
|
DiagDirection exitdir = GetRailDepotDirection(m_new_tile);
|
2007-07-09 18:27:42 +00:00
|
|
|
if (ReverseDiagDir(exitdir) != m_exitdir) {
|
|
|
|
m_err = EC_NO_WAY;
|
2006-05-27 16:12:16 +00:00
|
|
|
return false;
|
2007-07-09 18:27:42 +00:00
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* rail transport is possible only on tiles with the same owner as vehicle */
|
2008-08-02 22:52:08 +00:00
|
|
|
if (IsRailTT() && GetTileOwner(m_new_tile) != m_veh_owner) {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* different owner */
|
2007-07-09 18:27:42 +00:00
|
|
|
m_err = EC_NO_WAY;
|
2006-12-27 12:38:02 +00:00
|
|
|
return false;
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* rail transport is possible only on compatible rail types */
|
2006-05-27 16:12:16 +00:00
|
|
|
if (IsRailTT()) {
|
2007-02-25 11:36:19 +00:00
|
|
|
RailType rail_type = GetTileRailType(m_new_tile);
|
2008-08-02 22:50:38 +00:00
|
|
|
if (!HasBit(m_railtypes, rail_type)) {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* incompatible rail type */
|
2007-07-09 18:27:42 +00:00
|
|
|
m_err = EC_RAIL_TYPE;
|
2006-05-27 16:12:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* tunnel holes and bridge ramps can be entered only from proper direction */
|
2008-06-11 13:54:01 +00:00
|
|
|
if (IsTileType(m_new_tile, MP_TUNNELBRIDGE)) {
|
2006-12-29 18:40:02 +00:00
|
|
|
if (IsTunnel(m_new_tile)) {
|
|
|
|
if (!m_is_tunnel) {
|
2007-12-16 15:38:51 +00:00
|
|
|
DiagDirection tunnel_enterdir = GetTunnelBridgeDirection(m_new_tile);
|
2007-07-09 18:27:42 +00:00
|
|
|
if (tunnel_enterdir != m_exitdir) {
|
2007-07-09 19:24:45 +00:00
|
|
|
m_err = EC_NO_WAY;
|
2007-07-09 18:27:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-12-29 18:40:02 +00:00
|
|
|
}
|
2008-01-23 14:51:36 +00:00
|
|
|
} else { // IsBridge(m_new_tile)
|
2006-12-29 18:40:02 +00:00
|
|
|
if (!m_is_bridge) {
|
2007-12-16 15:38:51 +00:00
|
|
|
DiagDirection ramp_enderdir = GetTunnelBridgeDirection(m_new_tile);
|
2007-07-09 18:27:42 +00:00
|
|
|
if (ramp_enderdir != m_exitdir) {
|
2007-07-09 19:24:45 +00:00
|
|
|
m_err = EC_NO_WAY;
|
2007-07-09 18:27:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-12-29 18:40:02 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
2006-06-01 21:39:35 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* special handling for rail stations - get to the end of platform */
|
2006-06-01 21:39:35 +00:00
|
|
|
if (IsRailTT() && m_is_station) {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* entered railway station
|
|
|
|
* get platform length */
|
2009-06-24 17:39:54 +00:00
|
|
|
uint length = Station::GetByTile(m_new_tile)->GetPlatformLength(m_new_tile, TrackdirToExitdir(m_old_td));
|
2009-03-15 00:32:18 +00:00
|
|
|
/* how big step we must do to get to the last platform tile; */
|
2006-06-01 21:39:35 +00:00
|
|
|
m_tiles_skipped = length - 1;
|
2009-03-15 00:32:18 +00:00
|
|
|
/* move to the platform end */
|
2006-09-05 23:21:41 +00:00
|
|
|
TileIndexDiff diff = TileOffsByDiagDir(m_exitdir);
|
2006-06-01 21:39:35 +00:00
|
|
|
diff *= m_tiles_skipped;
|
|
|
|
m_new_tile = TILE_ADD(m_new_tile, diff);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-02-01 17:27:45 +00:00
|
|
|
/** return true if we must reverse (in depots and single tram bits) */
|
|
|
|
FORCEINLINE bool ForcedReverse()
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2009-03-15 00:32:18 +00:00
|
|
|
/* rail and road depots cause reversing */
|
2008-04-17 00:44:20 +00:00
|
|
|
if (!IsWaterTT() && IsDepotTypeTile(m_old_tile, TT())) {
|
2006-06-07 18:41:58 +00:00
|
|
|
DiagDirection exitdir = IsRailTT() ? GetRailDepotDirection(m_old_tile) : GetRoadDepotDirection(m_old_tile);
|
2006-05-27 16:12:16 +00:00
|
|
|
if (exitdir != m_exitdir) {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* reverse */
|
2006-05-27 16:12:16 +00:00
|
|
|
m_new_tile = m_old_tile;
|
|
|
|
m_new_td_bits = TrackdirToTrackdirBits(ReverseTrackdir(m_old_td));
|
|
|
|
m_exitdir = exitdir;
|
2006-06-01 21:39:35 +00:00
|
|
|
m_tiles_skipped = 0;
|
2006-12-27 12:38:02 +00:00
|
|
|
m_is_tunnel = m_is_bridge = m_is_station = false;
|
2006-05-27 16:12:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2008-02-01 17:27:45 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* single tram bits cause reversing */
|
2008-09-17 00:32:35 +00:00
|
|
|
if (IsTram() && GetSingleTramBit(m_old_tile) == ReverseDiagDir(m_exitdir)) {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* reverse */
|
2008-02-01 17:27:45 +00:00
|
|
|
m_new_tile = m_old_tile;
|
|
|
|
m_new_td_bits = TrackdirToTrackdirBits(ReverseTrackdir(m_old_td));
|
|
|
|
m_exitdir = ReverseDiagDir(m_exitdir);
|
|
|
|
m_tiles_skipped = 0;
|
|
|
|
m_is_tunnel = m_is_bridge = m_is_station = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-06-07 18:41:58 +00:00
|
|
|
|
|
|
|
/** return true if we successfully reversed at end of road/track */
|
|
|
|
FORCEINLINE bool TryReverse()
|
|
|
|
{
|
2008-02-01 17:27:45 +00:00
|
|
|
if (IsRoadTT() && !IsTram()) {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* if we reached the end of road, we can reverse the RV and continue moving */
|
2006-06-07 18:41:58 +00:00
|
|
|
m_exitdir = ReverseDiagDir(m_exitdir);
|
2009-03-15 00:32:18 +00:00
|
|
|
/* new tile will be the same as old one */
|
2006-06-07 18:41:58 +00:00
|
|
|
m_new_tile = m_old_tile;
|
2009-03-15 00:32:18 +00:00
|
|
|
/* set new trackdir bits to all reachable trackdirs */
|
2006-06-07 18:41:58 +00:00
|
|
|
QueryNewTileTrackStatus();
|
|
|
|
m_new_td_bits &= DiagdirReachesTrackdirs(m_exitdir);
|
|
|
|
if (m_new_td_bits != TRACKDIR_BIT_NONE) {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* we have some trackdirs reachable after reversal */
|
2006-06-07 18:41:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2007-07-09 18:27:42 +00:00
|
|
|
m_err = EC_NO_WAY;
|
2006-06-07 18:41:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
public:
|
|
|
|
/** Helper for pathfinders - get min/max speed on the m_old_tile/m_old_td */
|
2007-06-24 13:18:54 +00:00
|
|
|
int GetSpeedLimit(int *pmin_speed = NULL) const
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
|
|
|
int min_speed = 0;
|
|
|
|
int max_speed = INT_MAX; // no limit
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* for now we handle only on-bridge speed limit */
|
2006-12-27 12:38:02 +00:00
|
|
|
if (!IsWaterTT() && IsBridgeTile(m_old_tile)) {
|
2008-02-05 05:21:02 +00:00
|
|
|
int spd = GetBridgeSpec(GetBridgeType(m_old_tile))->speed;
|
2006-12-27 12:38:02 +00:00
|
|
|
if (IsRoadTT()) spd *= 2;
|
|
|
|
if (max_speed > spd) max_speed = spd;
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* if min speed was requested, return it */
|
2006-05-27 16:12:16 +00:00
|
|
|
if (pmin_speed) *pmin_speed = min_speed;
|
|
|
|
return max_speed;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef CFollowTrackT<TRANSPORT_WATER, true > CFollowTrackWater;
|
|
|
|
typedef CFollowTrackT<TRANSPORT_ROAD , true > CFollowTrackRoad;
|
|
|
|
typedef CFollowTrackT<TRANSPORT_RAIL , true > CFollowTrackRail;
|
|
|
|
|
|
|
|
typedef CFollowTrackT<TRANSPORT_WATER, false> CFollowTrackWaterNo90;
|
|
|
|
typedef CFollowTrackT<TRANSPORT_ROAD , false> CFollowTrackRoadNo90;
|
|
|
|
typedef CFollowTrackT<TRANSPORT_RAIL , false> CFollowTrackRailNo90;
|
|
|
|
|
2008-08-02 22:51:53 +00:00
|
|
|
typedef CFollowTrackT<TRANSPORT_RAIL , true , true> CFollowTrackFreeRail;
|
|
|
|
typedef CFollowTrackT<TRANSPORT_RAIL , false, true> CFollowTrackFreeRailNo90;
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
#endif /* FOLLOW_TRACK_HPP */
|