Skip to content

Commit

Permalink
Fix #8123: trams on half-tiles couldn't find depots
Browse files Browse the repository at this point in the history
Basically, follow_track.hpp contains a fix for half-tiles, but
this wasn't duplicated for when trying to find a depot and in
a few other places. This makes sure all places act the same.
  • Loading branch information
TrueBrain committed Feb 23, 2021
1 parent c93c9c0 commit 681ca8b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
23 changes: 4 additions & 19 deletions src/pathfinder/follow_track.hpp
Expand Up @@ -17,6 +17,7 @@
#include "../tunnelbridge.h"
#include "../tunnelbridge_map.h"
#include "../depot_map.h"
#include "pathfinder_func.h"
#include "pf_performance_timer.hpp"

/**
Expand Down Expand Up @@ -239,26 +240,10 @@ struct CFollowTrackT
CPerfStart perf(*m_pPerf);
if (IsRailTT() && IsPlainRailTile(m_new_tile)) {
m_new_td_bits = (TrackdirBits)(GetTrackBits(m_new_tile) * 0x101);
} else if (IsRoadTT()) {
m_new_td_bits = GetTrackdirBitsForRoad(m_new_tile, this->IsTram() ? RTT_TRAM : RTT_ROAD);
} else {
m_new_td_bits = TrackStatusToTrackdirBits(GetTileTrackStatus(m_new_tile, TT(), IsRoadTT() ? (this->IsTram() ? RTT_TRAM : RTT_ROAD) : 0));

if (IsTram() && m_new_td_bits == TRACKDIR_BIT_NONE) {
/* 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;
}
}
m_new_td_bits = TrackStatusToTrackdirBits(GetTileTrackStatus(m_new_tile, TT(), 0));
}
return (m_new_td_bits != TRACKDIR_BIT_NONE);
}
Expand Down
31 changes: 31 additions & 0 deletions src/pathfinder/pathfinder_func.h
Expand Up @@ -10,6 +10,7 @@
#ifndef PATHFINDER_FUNC_H
#define PATHFINDER_FUNC_H

#include "../tile_cmd.h"
#include "../waypoint_base.h"

/**
Expand Down Expand Up @@ -46,4 +47,34 @@ static inline TileIndex CalcClosestStationTile(StationID station, TileIndex tile
return TileXY(x, y);
}

static inline TrackdirBits GetTrackdirBitsForRoad(TileIndex tile, RoadTramType rtt)
{
TrackdirBits bits = TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_ROAD, rtt));

/* GetTileTrackStatus() returns 0 for single tram bits. But this is a valid
* tile to be on. So pretend that the tile is a straight tile, and
* CFollowTrackT will make sure it cannot be exited on the wrong side, and
* allow reversing on it. */
if (rtt == RTT_TRAM && bits == TRACKDIR_BIT_NONE) {
if (IsNormalRoadTile(tile)) {
RoadBits rb = GetRoadBits(tile, RTT_TRAM);
switch (rb) {
case ROAD_NE:
case ROAD_SW:
bits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW;
break;

case ROAD_NW:
case ROAD_SE:
bits = TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_SE;
break;

default: break;
}
}
}

return bits;
}

#endif /* PATHFINDER_FUNC_H */
7 changes: 4 additions & 3 deletions src/pathfinder/yapf/yapf_road.cpp
Expand Up @@ -370,7 +370,7 @@ class CYapfFollowRoadT
/* our source tile will be the next vehicle tile (should be the given one) */
TileIndex src_tile = tile;
/* get available trackdirs on the start tile */
TrackdirBits src_trackdirs = TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_ROAD, GetRoadTramType(v->roadtype)));
TrackdirBits src_trackdirs = GetTrackdirBitsForRoad(tile, GetRoadTramType(v->roadtype));
/* select reachable trackdirs only */
src_trackdirs &= DiagdirReachesTrackdirs(enterdir);

Expand Down Expand Up @@ -468,7 +468,7 @@ class CYapfFollowRoadT
/* set origin (tile, trackdir) */
TileIndex src_tile = v->tile;
Trackdir src_td = v->GetVehicleTrackdir();
if (!HasTrackdir(TrackStatusToTrackdirBits(GetTileTrackStatus(src_tile, TRANSPORT_ROAD, this->IsTram() ? RTT_TRAM : RTT_ROAD)), src_td)) {
if (!HasTrackdir(GetTrackdirBitsForRoad(src_tile, this->IsTram() ? RTT_TRAM : RTT_ROAD), src_td)) {
/* sometimes the roadveh is not on the road (it resides on non-existing track)
* how should we handle that situation? */
return false;
Expand Down Expand Up @@ -548,7 +548,8 @@ FindDepotData YapfRoadVehicleFindNearestDepot(const RoadVehicle *v, int max_dist
{
TileIndex tile = v->tile;
Trackdir trackdir = v->GetVehicleTrackdir();
if (!HasTrackdir(TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_ROAD, GetRoadTramType(v->roadtype))), trackdir)) {

if (!HasTrackdir(GetTrackdirBitsForRoad(tile, GetRoadTramType(v->roadtype)), trackdir)) {
return FindDepotData();
}

Expand Down

0 comments on commit 681ca8b

Please sign in to comment.