Skip to content

Commit

Permalink
remember time of save in replay
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Feb 14, 2024
1 parent 76253ab commit f381d57
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion native_app/src/newgui/hud/windows/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub fn load(uiw: &UiWorld, _: &Simulation, opened: &mut bool) {

if let Some(ref mut loading) = uiw.write::<SaveLoadState>().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),
Expand Down
3 changes: 3 additions & 0 deletions simulation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ impl Simulation {

game_schedule.execute(self);

self.resources.write::<Replay>().last_tick_recorded =
self.resources.read::<GameTime>().tick;

t.elapsed()
}

Expand Down
2 changes: 1 addition & 1 deletion simulation/src/tests/test_iso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 23 additions & 2 deletions simulation/src/utils/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
4 changes: 2 additions & 2 deletions simulation/src/world_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl WorldCommand {
let mut rep = sim.resources.write::<Replay>();
if rep.enabled {
let tick = sim.read::<GameTime>().tick;
rep.commands.push((tick, self.clone()));
rep.push(tick, self.clone());
}
drop(rep);

Expand Down Expand Up @@ -309,7 +309,7 @@ impl WorldCommand {
let mut rep = sim.resources.write::<Replay>();
rep.enabled = true;
let tick = sim.read::<GameTime>().tick;
rep.commands.push((tick, Init(opts.clone())));
rep.push(tick, Init(opts.clone()));
}

if opts.terrain_size > 0 {
Expand Down

0 comments on commit f381d57

Please sign in to comment.