From dc25e8d924dcc05a4caf1b2159e358751815f7a2 Mon Sep 17 00:00:00 2001 From: Caspar Krieger Date: Mon, 2 Dec 2024 15:41:33 +0800 Subject: [PATCH] fix: sync layer stores enough world states to roll back fully As a result of https://github.com/gschup/ggrs/pull/79, the synclayer and the logic in p2psession's advance_frame() were off by one in their agreement on the number of world states that needed to be stored. This fixes that so that they agree. --- src/sync_layer.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/sync_layer.rs b/src/sync_layer.rs index 50de1ca..d2f3c2d 100644 --- a/src/sync_layer.rs +++ b/src/sync_layer.rs @@ -143,29 +143,22 @@ impl<'c, T> GameStateAccessor<'c, T> { pub(crate) struct SavedStates { pub states: Vec>, - max_pred: usize, } impl SavedStates { fn new(max_pred: usize) -> Self { - let mut states = Vec::with_capacity(max_pred); - for _ in 0..max_pred { + // we need to store the current frame plus the number of max predictions, so that we can + // roll back to the very first frame even when we have predicted as far ahead as we can. + let num_cells = max_pred + 1; + let mut states = Vec::with_capacity(num_cells); + for _ in 0..num_cells { states.push(GameStateCell::default()); } - // if lockstep, we still provide a single cell for saving. - if max_pred == 0 { - states.push(GameStateCell::default()); - } - - Self { states, max_pred } + Self { states } } fn get_cell(&self, frame: Frame) -> GameStateCell { - // if lockstep, we still provide a single cell for saving. - if self.max_pred == 0 { - return self.states[0].clone(); - } assert!(frame >= 0); let pos = frame as usize % self.states.len(); self.states[pos].clone()