Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix #7670: Cache the origin tile to prevent recurring calls to the ro…
…ad pathfinder when a vehicle is blocked by another
  • Loading branch information
SamuXarick committed Dec 22, 2020
1 parent 7f0fefd commit 3fe07ea
Show file tree
Hide file tree
Showing 6 changed files with 21 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
7 changes: 7 additions & 0 deletions src/saveload/afterload.cpp
Expand Up @@ -3133,6 +3133,13 @@ bool AfterLoadGame()
}
}

if (IsSavegameVersionBefore(SLV_FIX_ROADVEH_PATH_CACHE_ORIGIN)) {
for (RoadVehicle *rv : RoadVehicle::Iterate()) {
rv->path.origin_td = INVALID_TRACKDIR;
rv->path.origin_tile = INVALID_TILE;
}
}

/* Compute station catchment areas. This is needed here in case UpdateStationAcceptance is called below. */
Station::RecomputeCatchmentForAll();

Expand Down
1 change: 1 addition & 0 deletions src/saveload/saveload.h
Expand Up @@ -321,6 +321,7 @@ enum SaveLoadVersion : uint16 {
SLV_END_PATCHPACKS = 286, ///< 286 Last known patchpack to use a version just above ours.

SLV_GS_INDUSTRY_CONTROL, ///< 287 PR#7912 and PR#8115 GS industry control.
SLV_FIX_ROADVEH_PATH_CACHE_ORIGIN, ///< 288 PR#7822 Cache the origin tile/td of a road vehicle pathfinder search.

SL_MAX_VERSION, ///< Highest possible saveload version
};
Expand Down
2 changes: 2 additions & 0 deletions src/saveload/vehicle_sl.cpp
Expand Up @@ -746,6 +746,8 @@ const SaveLoad *GetVehicleDescription(VehicleType vt)
SLE_VAR(RoadVehicle, overtaking_ctr, SLE_UINT8),
SLE_VAR(RoadVehicle, crashed_ctr, SLE_UINT16),
SLE_VAR(RoadVehicle, reverse_ctr, SLE_UINT8),
SLE_CONDVAR(RoadVehicle, path.origin_td, SLE_UINT8, SLV_FIX_ROADVEH_PATH_CACHE_ORIGIN, SL_MAX_VERSION),
SLE_CONDVAR(RoadVehicle, path.origin_tile, SLE_UINT32, SLV_FIX_ROADVEH_PATH_CACHE_ORIGIN, SL_MAX_VERSION),
SLE_CONDDEQUE(RoadVehicle, path.td, SLE_UINT8, SLV_ROADVEH_PATH_CACHE, SL_MAX_VERSION),
SLE_CONDDEQUE(RoadVehicle, path.tile, SLE_UINT32, SLV_ROADVEH_PATH_CACHE, SL_MAX_VERSION),

Expand Down

0 comments on commit 3fe07ea

Please sign in to comment.