From c6fcfefe4deedd0abae65c760003289e8560f2c0 Mon Sep 17 00:00:00 2001 From: Koen Bussemaker Date: Mon, 11 Mar 2024 20:29:23 +0100 Subject: [PATCH] Codechange: Make the road vehicle cache behave more like a std::deque --- src/pathfinder/yapf/yapf_road.cpp | 13 +++---------- src/roadveh.h | 29 +++++++++++++++++++++++++++++ src/roadveh_cmd.cpp | 15 ++++++--------- 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/pathfinder/yapf/yapf_road.cpp b/src/pathfinder/yapf/yapf_road.cpp index 41102c2e15..7390404b42 100644 --- a/src/pathfinder/yapf/yapf_road.cpp +++ b/src/pathfinder/yapf/yapf_road.cpp @@ -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(); } } } diff --git a/src/roadveh.h b/src/roadveh.h index 19ba64d1d6..4c864b3329 100644 --- a/src/roadveh.h +++ b/src/roadveh.h @@ -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 td; std::deque 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 diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index 32150cc714..b775087caa 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -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; } }