diff --git a/native_app/src/newgui/hud/windows/load.rs b/native_app/src/newgui/hud/windows/load.rs index 1c0896b1..a595f0e3 100644 --- a/native_app/src/newgui/hud/windows/load.rs +++ b/native_app/src/newgui/hud/windows/load.rs @@ -71,7 +71,7 @@ pub fn load(uiw: &UiWorld, _: &Simulation, opened: &mut bool) { if let Some(ref mut loading) = uiw.write::().please_load { let ticks_done = loading.pastt.0; - let ticks_total = loading.replay.commands.last().map(|c| c.0 .0).unwrap_or(0); + let ticks_total = loading.replay.last_tick_recorded.0; ProgressBar { value: ticks_done as f32 / ticks_total as f32, size: Vec2::new(400.0, 25.0), diff --git a/simulation/src/lib.rs b/simulation/src/lib.rs index 43de16ac..5fc61192 100644 --- a/simulation/src/lib.rs +++ b/simulation/src/lib.rs @@ -254,6 +254,9 @@ impl Simulation { game_schedule.execute(self); + self.resources.write::().last_tick_recorded = + self.resources.read::().tick; + t.elapsed() } diff --git a/simulation/src/tests/test_iso.rs b/simulation/src/tests/test_iso.rs index a5e25db5..76f9842e 100644 --- a/simulation/src/tests/test_iso.rs +++ b/simulation/src/tests/test_iso.rs @@ -267,7 +267,7 @@ fn test_world_survives_serde() { "--- tick {} ({}/{})", sim.get_tick(), loader.pastt.0, - loader.replay.commands.last().unwrap().0 .0 + loader.replay.last_tick_recorded.0 ); let ser = common::saveload::Bincode::encode(&sim).unwrap(); diff --git a/simulation/src/utils/replay.rs b/simulation/src/utils/replay.rs index 50120a7d..6f68ba37 100644 --- a/simulation/src/utils/replay.rs +++ b/simulation/src/utils/replay.rs @@ -7,7 +7,14 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct Replay { pub enabled: bool, - pub commands: Vec<(Tick, WorldCommand)>, + commands: Vec<(Tick, WorldCommand)>, + pub last_tick_recorded: Tick, +} + +impl Replay { + pub fn push(&mut self, tick: Tick, command: WorldCommand) { + self.commands.push((tick, command)); + } } pub struct SimulationReplayLoader { @@ -55,7 +62,21 @@ impl SimulationReplayLoader { sim.tick(schedule, command_slice.iter().map(|(_, c)| c)); self.pastt.0 += 1; ticks_left -= 1; + if ticks_left == 0 { + return false; + } + } + if self.idx < self.replay.commands.len() { + return false; + } + while ticks_left > 0 && self.pastt < self.replay.last_tick_recorded { + sim.tick(schedule, &[]); + self.pastt.0 += 1; + ticks_left -= 1; + if ticks_left == 0 { + return false; + } } - self.idx >= self.replay.commands.len() + true } } diff --git a/simulation/src/world_command.rs b/simulation/src/world_command.rs index ba12fd94..7fd98c6f 100644 --- a/simulation/src/world_command.rs +++ b/simulation/src/world_command.rs @@ -221,7 +221,7 @@ impl WorldCommand { let mut rep = sim.resources.write::(); if rep.enabled { let tick = sim.read::().tick; - rep.commands.push((tick, self.clone())); + rep.push(tick, self.clone()); } drop(rep); @@ -309,7 +309,7 @@ impl WorldCommand { let mut rep = sim.resources.write::(); rep.enabled = true; let tick = sim.read::().tick; - rep.commands.push((tick, Init(opts.clone()))); + rep.push(tick, Init(opts.clone())); } if opts.terrain_size > 0 {