Navigation Menu

Skip to content

Commit

Permalink
Fix OpenTTD#7670: Cache the origin tile to prevent recurring calls to…
Browse files Browse the repository at this point in the history
… the road pathfinder when a vehicle is blocked by another
  • Loading branch information
SamuXarick committed Mar 13, 2020
1 parent 5f0ed8b commit dbf0d34
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/pathfinder/yapf/yapf_road.cpp
Expand Up @@ -402,6 +402,8 @@ class CYapfFollowRoadT
Node &best_next_node = *pNode;
assert(best_next_node.GetTile() == tile);
next_trackdir = best_next_node.GetTrackdir();
path_cache.origin_td = next_trackdir;
path_cache.origin_tile = tile;
/* remove last element for the special case when tile == dest_tile */
if (path_found && !path_cache.empty() && tile == v->dest_tile) {
path_cache.td.pop_back();
Expand Down
4 changes: 4 additions & 0 deletions src/roadveh.h
Expand Up @@ -83,6 +83,8 @@ void RoadVehUpdateCache(RoadVehicle *v, bool same_length = false);
void GetRoadVehSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type);

struct RoadVehPathCache {
Trackdir origin_td = INVALID_TRACKDIR;
TileIndex origin_tile = INVALID_TILE;
std::deque<Trackdir> td;
std::deque<TileIndex> tile;

Expand All @@ -96,6 +98,8 @@ struct RoadVehPathCache {

inline void clear()
{
this->origin_td = INVALID_TRACKDIR;
this->origin_tile = INVALID_TILE;
this->td.clear();
this->tile.clear();
}
Expand Down
5 changes: 5 additions & 0 deletions src/roadveh_cmd.cpp
Expand Up @@ -968,6 +968,9 @@ static Trackdir RoadFindPathToDest(RoadVehicle *v, TileIndex tile, DiagDirection
/* Attempt to follow cached path. */
if (!v->path.empty()) {
if (v->path.tile.front() != tile) {
if (IsValidTile(v->path.origin_tile) && v->path.origin_tile == tile && HasBit(trackdirs, v->path.origin_td)) {
return_track(v->path.origin_td);
}
/* Vehicle didn't expect a choice here, invalidate its path. */
v->path.clear();
} else {
Expand All @@ -977,6 +980,8 @@ static Trackdir RoadFindPathToDest(RoadVehicle *v, TileIndex tile, DiagDirection
v->path.td.pop_front();
v->path.tile.pop_front();
return_track(trackdir);
} else if (IsValidTile(v->path.origin_tile) && v->path.origin_tile == tile && HasBit(trackdirs, v->path.origin_td)) {
return_track(v->path.origin_td);
}

/* Vehicle expected a choice which is no longer available. */
Expand Down

0 comments on commit dbf0d34

Please sign in to comment.