diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-10-31 02:40:35 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-10-31 02:42:35 +0000 |
commit | 2054a4a444888ac6285aa710974729d4072e7894 (patch) | |
tree | f29f28c3c137120694f3ce7b18b53c2eec2fb5c0 /game/vehicles/linkHistory.cpp | |
parent | Extract const for link history length (diff) | |
download | ilt-2054a4a444888ac6285aa710974729d4072e7894.tar.bz2 ilt-2054a4a444888ac6285aa710974729d4072e7894.tar.xz ilt-2054a4a444888ac6285aa710974729d4072e7894.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.
Diffstat (limited to 'game/vehicles/linkHistory.cpp')
-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}; } |