mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-08-12 17:19:09 +00:00
Codechange: Make the road vehicle cache behave more like a std::deque
This commit is contained in:
@@ -393,8 +393,7 @@ public:
|
|||||||
while (pNode->m_parent != nullptr) {
|
while (pNode->m_parent != nullptr) {
|
||||||
steps--;
|
steps--;
|
||||||
if (pNode->GetIsChoice() && steps < YAPF_ROADVEH_PATH_CACHE_SEGMENTS) {
|
if (pNode->GetIsChoice() && steps < YAPF_ROADVEH_PATH_CACHE_SEGMENTS) {
|
||||||
path_cache.td.push_front(pNode->GetTrackdir());
|
path_cache.push_front({ pNode->GetTile(), pNode->GetTrackdir() });
|
||||||
path_cache.tile.push_front(pNode->GetTile());
|
|
||||||
}
|
}
|
||||||
pNode = pNode->m_parent;
|
pNode = pNode->m_parent;
|
||||||
}
|
}
|
||||||
@@ -403,10 +402,7 @@ public:
|
|||||||
assert(best_next_node.GetTile() == tile);
|
assert(best_next_node.GetTile() == tile);
|
||||||
next_trackdir = best_next_node.GetTrackdir();
|
next_trackdir = best_next_node.GetTrackdir();
|
||||||
/* remove last element for the special case when tile == dest_tile */
|
/* remove last element for the special case when tile == dest_tile */
|
||||||
if (path_found && !path_cache.empty() && tile == v->dest_tile) {
|
if (path_found && !path_cache.empty() && tile == v->dest_tile) path_cache.pop_back();
|
||||||
path_cache.td.pop_back();
|
|
||||||
path_cache.tile.pop_back();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Check if target is a station, and cached path ends within 8 tiles of the dest tile */
|
/* Check if target is a station, and cached path ends within 8 tiles of the dest tile */
|
||||||
const Station *st = Yapf().GetDestinationStation();
|
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 */
|
* 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;
|
TileArea non_cached_area = v->IsBus() ? st->bus_station : st->truck_station;
|
||||||
non_cached_area.Expand(YAPF_ROADVEH_PATH_CACHE_DESTINATION_LIMIT);
|
non_cached_area.Expand(YAPF_ROADVEH_PATH_CACHE_DESTINATION_LIMIT);
|
||||||
while (!path_cache.empty() && non_cached_area.Contains(path_cache.tile.back())) {
|
while (!path_cache.empty() && non_cached_area.Contains(path_cache.back().tile)) path_cache.pop_back();
|
||||||
path_cache.td.pop_back();
|
|
||||||
path_cache.tile.pop_back();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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);
|
void GetRoadVehSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type);
|
||||||
|
|
||||||
struct RoadVehPathCache {
|
struct RoadVehPathCache {
|
||||||
|
friend class SlVehicleRoadVeh;
|
||||||
|
private:
|
||||||
std::deque<Trackdir> td;
|
std::deque<Trackdir> td;
|
||||||
std::deque<TileIndex> tile;
|
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 bool empty() const { return this->td.empty(); }
|
||||||
|
|
||||||
inline size_t size() const
|
inline size_t size() const
|
||||||
|
@@ -963,7 +963,7 @@ static Trackdir RoadFindPathToDest(RoadVehicle *v, TileIndex tile, DiagDirection
|
|||||||
|
|
||||||
/* Only one track to choose between? */
|
/* Only one track to choose between? */
|
||||||
if (KillFirstBit(trackdirs) == TRACKDIR_BIT_NONE) {
|
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. */
|
/* Vehicle expected a choice here, invalidate its path. */
|
||||||
v->path.clear();
|
v->path.clear();
|
||||||
}
|
}
|
||||||
@@ -972,15 +972,14 @@ static Trackdir RoadFindPathToDest(RoadVehicle *v, TileIndex tile, DiagDirection
|
|||||||
|
|
||||||
/* Attempt to follow cached path. */
|
/* Attempt to follow cached path. */
|
||||||
if (!v->path.empty()) {
|
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. */
|
/* Vehicle didn't expect a choice here, invalidate its path. */
|
||||||
v->path.clear();
|
v->path.clear();
|
||||||
} else {
|
} else {
|
||||||
Trackdir trackdir = v->path.td.front();
|
Trackdir trackdir = v->path.front().trackdir;
|
||||||
|
|
||||||
if (HasBit(trackdirs, trackdir)) {
|
if (HasBit(trackdirs, trackdir)) {
|
||||||
v->path.td.pop_front();
|
v->path.pop_front();
|
||||||
v->path.tile.pop_front();
|
|
||||||
return_track(trackdir);
|
return_track(trackdir);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1291,8 +1290,7 @@ again:
|
|||||||
if (u != nullptr) {
|
if (u != nullptr) {
|
||||||
v->cur_speed = u->First()->cur_speed;
|
v->cur_speed = u->First()->cur_speed;
|
||||||
/* We might be blocked, prevent pathfinding rerun as we already know where we are heading to. */
|
/* We might be blocked, prevent pathfinding rerun as we already know where we are heading to. */
|
||||||
v->path.tile.push_front(tile);
|
v->path.push_front({ tile, dir });
|
||||||
v->path.td.push_front(dir);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1407,8 +1405,7 @@ again:
|
|||||||
if (u != nullptr) {
|
if (u != nullptr) {
|
||||||
v->cur_speed = u->First()->cur_speed;
|
v->cur_speed = u->First()->cur_speed;
|
||||||
/* We might be blocked, prevent pathfinding rerun as we already know where we are heading to. */
|
/* 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.push_front({ v->tile, dir });
|
||||||
v->path.td.push_front(dir);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user