KUDr@3900: /* $Id$ */ KUDr@3900: KUDr@3900: #ifndef YAPF_DESTRAIL_HPP KUDr@3900: #define YAPF_DESTRAIL_HPP KUDr@3900: KUDr@3900: class CYapfDestinationRailBase KUDr@3900: { KUDr@3900: protected: KUDr@3900: RailTypeMask m_compatible_railtypes; KUDr@3900: KUDr@3900: public: KUDr@3900: void SetDestination(Vehicle* v) KUDr@3900: { KUDr@3900: m_compatible_railtypes = v->u.rail.compatible_railtypes; KUDr@3900: } KUDr@3900: KUDr@3900: bool IsCompatibleRailType(RailType rt) KUDr@3900: { KUDr@3900: return HASBIT(m_compatible_railtypes, rt); KUDr@3900: } KUDr@3900: }; KUDr@3900: KUDr@3900: template KUDr@3900: class CYapfDestinationAnyDepotRailT KUDr@3900: : public CYapfDestinationRailBase KUDr@3900: { KUDr@3900: public: KUDr@3914: typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class) KUDr@3900: typedef typename Types::NodeList::Titem Node; ///< this will be our node type KUDr@3914: typedef typename Node::Key Key; ///< key to hash tables KUDr@3900: KUDr@3914: /// to access inherited path finder KUDr@3900: Tpf& Yapf() {return *static_cast(this);} KUDr@3900: KUDr@3914: /// Called by YAPF to detect if node ends in the desired destination KUDr@3900: FORCEINLINE bool PfDetectDestination(Node& n) KUDr@3900: { KUDr@3930: return PfDetectDestination(n.GetLastTile(), n.GetLastTrackdir()); KUDr@3930: } KUDr@3930: KUDr@3930: /// Called by YAPF to detect if node ends in the desired destination KUDr@3930: FORCEINLINE bool PfDetectDestination(TileIndex tile, Trackdir td) KUDr@3930: { KUDr@3930: bool bDest = IsTileDepotType(tile, TRANSPORT_RAIL); KUDr@3900: return bDest; KUDr@3900: } KUDr@3900: KUDr@3914: /** Called by YAPF to calculate cost estimate. Calculates distance to the destination KUDr@3914: * adds it to the actual cost from origin and stores the sum to the Node::m_estimate */ KUDr@3900: FORCEINLINE bool PfCalcEstimate(Node& n) KUDr@3900: { KUDr@3900: n.m_estimate = n.m_cost; KUDr@3900: return true; KUDr@3900: } KUDr@3900: }; KUDr@3900: KUDr@3900: template KUDr@3900: class CYapfDestinationTileOrStationRailT KUDr@3900: : public CYapfDestinationRailBase KUDr@3900: { KUDr@3900: public: KUDr@3914: typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class) KUDr@3900: typedef typename Types::NodeList::Titem Node; ///< this will be our node type KUDr@3914: typedef typename Node::Key Key; ///< key to hash tables KUDr@3900: KUDr@3900: protected: KUDr@3900: TileIndex m_destTile; KUDr@3900: TrackdirBits m_destTrackdirs; KUDr@3900: StationID m_dest_station_id; KUDr@3900: KUDr@3914: /// to access inherited path finder KUDr@3900: Tpf& Yapf() {return *static_cast(this);} KUDr@3900: KUDr@3900: static TileIndex CalcStationCenterTile(StationID station) KUDr@3900: { KUDr@3900: const Station* st = GetStation(station); KUDr@3900: KUDr@3900: uint x = TileX(st->train_tile) + st->trainst_w / 2; KUDr@3900: uint y = TileY(st->train_tile) + st->trainst_h / 2; KUDr@3900: // return the tile of our target coordinates KUDr@3900: return TileXY(x, y); KUDr@3900: } KUDr@3900: KUDr@3900: public: KUDr@3900: void SetDestination(Vehicle* v) KUDr@3900: { KUDr@3900: if (v->current_order.type == OT_GOTO_STATION) { KUDr@3900: m_destTile = CalcStationCenterTile(v->current_order.station); KUDr@3900: m_dest_station_id = v->current_order.station; KUDr@3900: m_destTrackdirs = INVALID_TRACKDIR_BIT; KUDr@3900: } else { KUDr@3900: m_destTile = v->dest_tile; KUDr@3900: m_dest_station_id = INVALID_STATION; KUDr@3900: m_destTrackdirs = (TrackdirBits)(GetTileTrackStatus(v->dest_tile, TRANSPORT_RAIL) & TRACKDIR_BIT_MASK); KUDr@3900: } KUDr@3900: CYapfDestinationRailBase::SetDestination(v); KUDr@3900: } KUDr@3900: KUDr@3914: /// Called by YAPF to detect if node ends in the desired destination KUDr@3900: FORCEINLINE bool PfDetectDestination(Node& n) KUDr@3900: { KUDr@3930: return PfDetectDestination(n.GetLastTile(), n.GetLastTrackdir()); KUDr@3930: } KUDr@3930: KUDr@3930: /// Called by YAPF to detect if node ends in the desired destination KUDr@3930: FORCEINLINE bool PfDetectDestination(TileIndex tile, Trackdir td) KUDr@3930: { KUDr@3900: bool bDest; KUDr@3900: if (m_dest_station_id != INVALID_STATION) { KUDr@3930: bDest = IsRailwayStationTile(tile) KUDr@3930: && (GetStationIndex(tile) == m_dest_station_id) KUDr@3930: && (GetRailStationTrack(tile) == TrackdirToTrack(td)); KUDr@3900: } else { KUDr@3930: bDest = (tile == m_destTile) KUDr@3930: && ((m_destTrackdirs & TrackdirToTrackdirBits(td)) != TRACKDIR_BIT_NONE); KUDr@3900: } KUDr@3900: return bDest; KUDr@3900: } KUDr@3900: KUDr@3914: /** Called by YAPF to calculate cost estimate. Calculates distance to the destination KUDr@3914: * adds it to the actual cost from origin and stores the sum to the Node::m_estimate */ KUDr@3900: FORCEINLINE bool PfCalcEstimate(Node& n) KUDr@3900: { KUDr@3900: static int dg_dir_to_x_offs[] = {-1, 0, 1, 0}; KUDr@3900: static int dg_dir_to_y_offs[] = {0, 1, 0, -1}; KUDr@3900: if (PfDetectDestination(n)) { KUDr@3900: n.m_estimate = n.m_cost; KUDr@3900: return true; KUDr@3900: } KUDr@3900: KUDr@3900: TileIndex tile = n.GetLastTile(); KUDr@3900: DiagDirection exitdir = TrackdirToExitdir(n.GetLastTrackdir()); KUDr@3900: int x1 = 2 * TileX(tile) + dg_dir_to_x_offs[(int)exitdir]; KUDr@3900: int y1 = 2 * TileY(tile) + dg_dir_to_y_offs[(int)exitdir]; KUDr@3900: int x2 = 2 * TileX(m_destTile); KUDr@3900: int y2 = 2 * TileY(m_destTile); KUDr@3900: int dx = abs(x1 - x2); KUDr@3900: int dy = abs(y1 - y2); KUDr@3900: int dmin = min(dx, dy); KUDr@3900: int dxy = abs(dx - dy); KUDr@3900: int d = dmin * YAPF_TILE_CORNER_LENGTH + (dxy - 1) * (YAPF_TILE_LENGTH / 2); KUDr@3900: n.m_estimate = n.m_cost + d; KUDr@3900: assert(n.m_estimate >= n.m_parent->m_estimate); KUDr@3900: return true; KUDr@3900: } KUDr@3900: }; KUDr@3900: KUDr@3900: KUDr@3900: #endif /* YAPF_DESTRAIL_HPP */