From 27d77fdbbbc4727b80765045437d50ad99a98b2b Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 31 Oct 2024 02:40:35 +0000 Subject: Fix pruning of link history The current size isn't important, the length after prune is, and it should be checked before adding the new link as most of the train will still be on the previous one. --- game/vehicles/linkHistory.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'game/vehicles') diff --git a/game/vehicles/linkHistory.cpp b/game/vehicles/linkHistory.cpp index 45aa0a8..77840ed 100644 --- a/game/vehicles/linkHistory.cpp +++ b/game/vehicles/linkHistory.cpp @@ -1,18 +1,27 @@ #include "linkHistory.h" #include "game/network/link.h" #include +#include LinkHistory::Entry LinkHistory::add(const Link::WPtr & l, unsigned char d) { + constexpr auto HISTORY_KEEP_LENGTH = 500'000.F; + while (const auto newLength = [this]() -> std::optional { + if (!links.empty()) { + const auto newLength = totalLen - links.back().first.lock()->length; + if (newLength >= HISTORY_KEEP_LENGTH) { + return newLength; + } + } + return std::nullopt; + }()) { + totalLen = newLength.value(); + links.pop_back(); + } links.insert(links.begin(), {l, d}); const auto lp = l.lock(); totalLen += lp->length; - constexpr auto HISTORY_KEEP_LENGTH = 1'000'000.F; - while (totalLen >= HISTORY_KEEP_LENGTH && !links.empty()) { - totalLen -= links.back().first.lock()->length; - links.pop_back(); - } return {lp, d}; } -- cgit v1.2.3