fix: Prediction.CorrectHistory now adjusts afterIndex after removals/insertions

This commit is contained in:
mischa 2024-03-18 15:52:24 +08:00 committed by MrGadget
parent 39338b413d
commit 1d004dcd21

View File

@ -106,17 +106,21 @@ public static T CorrectHistory<T>(
T corrected, // corrected state with timestamp T corrected, // corrected state with timestamp
T before, // state in history before the correction T before, // state in history before the correction
T after, // state in history after the correction T after, // state in history after the correction
int afterIndex) // index of the 'after' value so we don't need to find it again here int afterIndex) // index of the 'after' value so we don't need to find it again here
where T: PredictedState where T: PredictedState
{ {
// respect the limit // respect the limit
// TODO unit test to check if it respects max size // TODO unit test to check if it respects max size
if (history.Count >= stateHistoryLimit) if (history.Count >= stateHistoryLimit)
{
history.RemoveAt(0); history.RemoveAt(0);
afterIndex -= 1; // we removed the first value so all indices are off by one now
}
// insert the corrected state into the history, or overwrite if already exists // insert the corrected state into the history, or overwrite if already exists
// SortedList insertions are O(N)! // SortedList insertions are O(N)!
history[corrected.timestamp] = corrected; history[corrected.timestamp] = corrected;
afterIndex += 1; // we inserted the corrected value before the previous index
// the entry behind the inserted one still has the delta from (before, after). // the entry behind the inserted one still has the delta from (before, after).
// we need to correct it to (corrected, after). // we need to correct it to (corrected, after).