From 8bb39f95726174912121cfd98b33270bc1a58845 Mon Sep 17 00:00:00 2001 From: Sofia Date: Mon, 27 Jul 2026 23:54:35 +0300 Subject: [PATCH] Optimize instant replay --- rust/src/game_manager.rs | 15 +++++++++++++-- rust/src/replay.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/rust/src/game_manager.rs b/rust/src/game_manager.rs index b6641e7..a3071d6 100644 --- a/rust/src/game_manager.rs +++ b/rust/src/game_manager.rs @@ -257,6 +257,7 @@ pub struct Game { pub goals: HashMap, pub replay_recorder: Option>, + pub current_replay_frame_start: usize, pub current_replay_playback: Option>, pub last_replay_clone: f64, @@ -361,7 +362,10 @@ impl INode for Game { { self.last_replay_clone += delta; if self.last_replay_clone >= 1. { - playback.bind_mut().replay = recorder.bind().replay.clone(); + playback.bind_mut().replay = recorder + .bind() + .replay + .clone_since(self.current_replay_frame_start); self.last_replay_clone = 0. } } @@ -404,7 +408,14 @@ impl Game { pub fn start_playback(&mut self, time_delta: f64, spectated_id: u16) { if let Some(recorder) = &self.replay_recorder { let mut playback = ReplayPlayback::new_alloc(); - playback.bind_mut().replay = recorder.bind().replay.clone(); + + let frame = recorder + .bind() + .replay + .find_frame(recorder.bind().time_elapsed + time_delta); + self.current_replay_frame_start = frame; + + playback.bind_mut().replay = recorder.bind().replay.clone_since(frame); playback.bind_mut().current_map = self.current_map.clone(); playback.bind_mut().current_time = (recorder.bind().time_elapsed + time_delta).max(0.); playback.bind_mut().fast_forwarding = true; diff --git a/rust/src/replay.rs b/rust/src/replay.rs index f1335c0..362f033 100644 --- a/rust/src/replay.rs +++ b/rust/src/replay.rs @@ -508,6 +508,33 @@ impl Default for Replay { } } +impl Replay { + pub fn find_frame(&self, elapsed: f64) -> usize { + self.frames + .iter() + .enumerate() + .skip_while(|(_, f)| f.time < elapsed) + .next() + .map(|(i, _)| i) + .unwrap_or(0) + } + + pub fn clone_since(&self, frame_idx: usize) -> Replay { + Replay { + map_idx: self.map_idx, + players: self.players.clone(), + teams: self.teams.clone(), + goals: self.goals.clone(), + frames: self + .frames + .iter() + .skip(frame_idx) + .cloned() + .collect::>(), + } + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ReplayTeam { pub name: String,