Codechange: Make the road vehicle cache behave more like a std::deque

This commit is contained in:
Koen Bussemaker 2024-03-11 20:29:23 +01:00
parent c768f4fc7a
commit c6fcfefe4d
3 changed files with 38 additions and 19 deletions

View File

@ -393,8 +393,7 @@ public:
while (pNode->m_parent != nullptr) {
steps--;
if (pNode->GetIsChoice() && steps < YAPF_ROADVEH_PATH_CACHE_SEGMENTS) {
path_cache.td.push_front(pNode->GetTrackdir());
path_cache.tile.push_front(pNode->GetTile());
path_cache.push_front({ pNode->GetTile(), pNode->GetTrackdir() });
}
pNode = pNode->m_parent;
}
@ -403,10 +402,7 @@ public:
assert(best_next_node.GetTile() == tile);
next_trackdir = best_next_node.GetTrackdir();
/* 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();
path_cache.tile.pop_back();
}
if (path_found && !path_cache.empty() && tile == v->dest_tile) path_cache.pop_back();
/* Check if target is a station, and cached path ends within 8 tiles of the dest tile */
const Station *st = Yapf().GetDestinationStation();
@ -417,10 +413,7 @@ public:
* trim end of path cache within a number of tiles of road stop tile area */
TileArea non_cached_area = v->IsBus() ? st->bus_station : st->truck_station;
non_cached_area.Expand(YAPF_ROADVEH_PATH_CACHE_DESTINATION_LIMIT);
while (!path_cache.empty() && non_cached_area.Contains(path_cache.tile.back())) {
path_cache.td.pop_back();
path_cache.tile.pop_back();
}
while (!path_cache.empty() && non_cached_area.Contains(path_cache.back().tile)) path_cache.pop_back();
}
}
}

View File

@ -82,9 +82,38 @@ 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 {
friend class SlVehicleRoadVeh;
private:
std::deque<Trackdir> td;
std::deque<TileIndex> tile;
public:
struct Item {
TileIndex tile;
Trackdir trackdir;
};
Item front() { return {this->tile.front(), this->td.front()}; }
Item back() { return {this->tile.back(), this->td.back()}; }
inline void push_front(Item item)
{
this->tile.push_front(item.tile);
this->td.push_front(item.trackdir);
}
inline void pop_front()
{
this->td.pop_front();
this->tile.pop_front();
}
inline void pop_back()
{
this->td.pop_back();
this->tile.pop_back();
}
inline bool empty() const { return this->td.empty(); }
inline size_t size() const

View File

@ -963,7 +963,7 @@ static Trackdir RoadFindPathToDest(RoadVehicle *v, TileIndex tile, DiagDirection
/* Only one track to choose between? */
if (KillFirstBit(trackdirs) == TRACKDIR_BIT_NONE) {
if (!v->path.empty() && v->path.tile.front() == tile) {
if (!v->path.empty() && v->path.front().tile == tile) {
/* Vehicle expected a choice here, invalidate its path. */
v->path.clear();
}
@ -972,15 +972,14 @@ static Trackdir RoadFindPathToDest(RoadVehicle *v, TileIndex tile, DiagDirection
/* Attempt to follow cached path. */
if (!v->path.empty()) {
if (v->path.tile.front() != tile) {
if (v->path.front().tile != tile) {
/* Vehicle didn't expect a choice here, invalidate its path. */
v->path.clear();
} else {
Trackdir trackdir = v->path.td.front();
Trackdir trackdir = v->path.front().trackdir;
if (HasBit(trackdirs, trackdir)) {
v->path.td.pop_front();
v->path.tile.pop_front();
v->path.pop_front();
return_track(trackdir);
}
@ -1291,8 +1290,7 @@ again:
if (u != nullptr) {
v->cur_speed = u->First()->cur_speed;
/* We might be blocked, prevent pathfinding rerun as we already know where we are heading to. */
v->path.tile.push_front(tile);
v->path.td.push_front(dir);
v->path.push_front({ tile, dir });
return false;
}
}
@ -1407,8 +1405,7 @@ again:
if (u != nullptr) {
v->cur_speed = u->First()->cur_speed;
/* We might be blocked, prevent pathfinding rerun as we already know where we are heading to. */
v->path.tile.push_front(v->tile);
v->path.td.push_front(dir);
v->path.push_front({ v->tile, dir });
return false;
}
}