diff options
Diffstat (limited to 'crates/fparkan-runtime/src/clan_clock.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/clan_clock.rs | 97 |
1 files changed, 82 insertions, 15 deletions
diff --git a/crates/fparkan-runtime/src/clan_clock.rs b/crates/fparkan-runtime/src/clan_clock.rs index dd61a959..586157df 100644 --- a/crates/fparkan-runtime/src/clan_clock.rs +++ b/crates/fparkan-runtime/src/clan_clock.rs @@ -20,12 +20,16 @@ impl Default for Clock { } #[derive(Default)] -pub(super) struct ClanClocks(BTreeMap<usize, Clock>); +pub(super) struct ClanClocks { + clans: BTreeMap<usize, Clock>, + paused_ms: u32, +} impl ClanClocks { /// ai.dll RVA 0x17ae: equality is not due; a late callback runs one cycle. pub(super) fn tick(&mut self, clan: usize, elapsed_ms: u32, rng: &mut u32) -> Option<u32> { - let clock = self.0.entry(clan).or_default(); + let elapsed_ms = self.wall_time_ms(elapsed_ms); + let clock = self.clans.entry(clan).or_default(); if i64::from(elapsed_ms) <= clock.deadline_ms { return None; } @@ -40,20 +44,37 @@ impl ClanClocks { } pub(super) fn advance_for_registration(&mut self, clan: usize) -> u64 { - let clock = self.0.entry(clan).or_default(); + let clock = self.clans.entry(clan).or_default(); clock.deadline_ms = clock.deadline_ms.wrapping_sub(1_400); clock.registrations += 1; clock.registrations } pub(super) fn ticks(&self) -> impl Iterator<Item = (usize, u64, u64)> + '_ { - self.0 + self.clans .iter() .map(|(&clan, clock)| (clan, clock.ticks, clock.registrations)) } pub(super) fn seconds(&self, clan: usize) -> u32 { - self.0.get(&clan).map_or(0, |clock| clock.seconds) + self.clans.get(&clan).map_or(0, |clock| clock.seconds) + } + + pub(super) fn wall_time_ms(&self, queue_ms: u32) -> u32 { + queue_ms.wrapping_add(self.paused_ms) + } + + pub(super) fn add_paused_time(&mut self, paused_ms: u32) { + self.paused_ms = self.paused_ms.wrapping_add(paused_ms); + } +} + +/// Records wall time excluded by the caller's paused world clock. +/// The next frame checks AI deadlines once, without advancing the world or replaying missed cycles. +pub fn note_mission_pause_elapsed(engine: &mut crate::Engine, elapsed_ms: u32) { + if let Some(loaded) = &mut engine.loaded { + loaded.clan_ai_events.add_paused_time(elapsed_ms); + eprintln!("script_ai_pause_elapsed duration_ms={elapsed_ms}"); } } @@ -84,18 +105,41 @@ mod tests { use super::*; #[test] + fn pause_ages_deadlines_without_ticking_or_replaying_missed_cycles() { + let mut clocks = ClanClocks::default(); + let mut rng = 1; + assert_eq!(clocks.tick(0, 0, &mut rng), Some(7)); + assert_eq!(clocks.tick(1, 0, &mut rng), Some(7)); + let before_pause = rng; + clocks.add_paused_time(22_300); + assert_eq!( + (clocks.seconds(0), clocks.seconds(1), rng), + (7, 7, before_pause) + ); + assert_eq!(clocks.tick(0, 500, &mut rng), Some(14)); + assert_eq!(clocks.tick(1, 500, &mut rng), Some(14)); + let resumed_rng = rng; + assert_eq!(clocks.tick(0, 500, &mut rng), None); + assert_eq!(clocks.tick(1, 500, &mut rng), None); + assert_eq!(rng, resumed_rng); + assert_eq!(clocks.clans[&0].deadline_ms, 22_800 + 7_334); + assert_eq!(clocks.tick(0, 7_834, &mut rng), None); + assert_eq!(clocks.tick(0, 7_835, &mut rng), Some(21)); + } + + #[test] fn native_rng_jitter_is_shared_between_clans_and_equality_does_not_consume_it() { let mut clocks = ClanClocks::default(); let mut rng = 1; assert_eq!(clocks.seconds(0), 0); assert_eq!(clocks.tick(0, 0, &mut rng), Some(7)); - assert_eq!((clocks.0[&0].deadline_ms, rng), (7_041, 2_745_024)); + assert_eq!((clocks.clans[&0].deadline_ms, rng), (7_041, 2_745_024)); assert_eq!(clocks.tick(1, 0, &mut rng), Some(7)); - assert_eq!((clocks.0[&1].deadline_ms, rng), (7_467, 3_357_800_067)); + assert_eq!((clocks.clans[&1].deadline_ms, rng), (7_467, 3_357_800_067)); assert_eq!(clocks.tick(0, 7_041, &mut rng), None); assert_eq!(rng, 3_357_800_067); assert_eq!(clocks.tick(0, 7_042, &mut rng), Some(14)); - assert_eq!((clocks.0[&0].deadline_ms, rng), (14_376, 415_139_642)); + assert_eq!((clocks.clans[&0].deadline_ms, rng), (14_376, 415_139_642)); assert_eq!((clocks.seconds(0), clocks.seconds(1)), (14, 7)); } @@ -104,7 +148,7 @@ mod tests { let mut clocks = ClanClocks::default(); let mut rng = 1; assert_eq!(clocks.tick(1, 100_000, &mut rng), Some(7)); - assert_eq!(clocks.0[&1].deadline_ms, 107_041); + assert_eq!(clocks.clans[&1].deadline_ms, 107_041); assert_eq!(clocks.tick(1, 100_000, &mut rng), None); assert_eq!(clocks.ticks().collect::<Vec<_>>(), [(1, 1, 0)]); } @@ -112,17 +156,17 @@ mod tests { #[test] fn native_registration_shift_accumulates_and_a_negative_deadline_is_due() { let mut clocks = ClanClocks::default(); - clocks.0.entry(1).or_default().deadline_ms = 8_000; + clocks.clans.entry(1).or_default().deadline_ms = 8_000; assert_eq!(clocks.advance_for_registration(1), 1); - assert_eq!(clocks.0[&1].deadline_ms, 6_600); + assert_eq!(clocks.clans[&1].deadline_ms, 6_600); assert_eq!(clocks.advance_for_registration(1), 2); - assert_eq!(clocks.0[&1].deadline_ms, 5_200); - clocks.0.get_mut(&1).unwrap().deadline_ms = 500; + assert_eq!(clocks.clans[&1].deadline_ms, 5_200); + clocks.clans.get_mut(&1).unwrap().deadline_ms = 500; clocks.advance_for_registration(1); - assert_eq!(clocks.0[&1].deadline_ms, -900); + assert_eq!(clocks.clans[&1].deadline_ms, -900); let mut rng = 1; assert_eq!(clocks.tick(1, 0, &mut rng), Some(7)); - assert_eq!(clocks.0[&1].deadline_ms, 7_041); + assert_eq!(clocks.clans[&1].deadline_ms, 7_041); assert_eq!(rng, 2_745_024); assert_eq!(clocks.ticks().collect::<Vec<_>>(), [(1, 1, 3)]); } @@ -171,5 +215,28 @@ mod tests { [(0, 4, 0), (1, 4, 1)] ); assert!(loaded.clan_ai_events.stops().next().is_none()); + note_mission_pause_elapsed(&mut engine, 22_300); + frame(&mut engine, 28_000).unwrap(); + assert_eq!( + engine + .loaded + .as_ref() + .unwrap() + .clan_ai_events + .clock_ticks() + .collect::<Vec<_>>(), + [(0, 5, 0), (1, 5, 1)] + ); + frame(&mut engine, 28_000).unwrap(); + assert_eq!( + engine + .loaded + .as_ref() + .unwrap() + .clan_ai_events + .clock_ticks() + .collect::<Vec<_>>(), + [(0, 5, 0), (1, 5, 1)] + ); } } |
