diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-10-31 02:40:35 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-11-05 20:06:48 +0000 |
commit | 27d77fdbbbc4727b80765045437d50ad99a98b2b (patch) | |
tree | f29f28c3c137120694f3ce7b18b53c2eec2fb5c0 | |
parent | Extract const for link history length (diff) | |
download | ilt-27d77fdbbbc4727b80765045437d50ad99a98b2b.tar.bz2 ilt-27d77fdbbbc4727b80765045437d50ad99a98b2b.tar.xz ilt-27d77fdbbbc4727b80765045437d50ad99a98b2b.zip |
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.
-rw-r--r-- | game/vehicles/linkHistory.cpp | 19 |
1 files changed, 14 insertions, 5 deletions
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 <memory> +#include <optional> 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<decltype(totalLen)> { + 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}; } |