Skip to content

Commit

Permalink
fix: sync layer stores enough world states to roll back fully
Browse files Browse the repository at this point in the history
As a result of #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.
  • Loading branch information
caspark committed Dec 14, 2024
1 parent 534170e commit dc25e8d
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/sync_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,29 +143,22 @@ impl<'c, T> GameStateAccessor<'c, T> {

pub(crate) struct SavedStates<T> {
pub states: Vec<GameStateCell<T>>,
max_pred: usize,
}

impl<T> SavedStates<T> {
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<T> {
// 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()
Expand Down

0 comments on commit dc25e8d

Please sign in to comment.