//! Read-only scene data shared by live rendering and recorded playback adapters. use crate::{presentation::Context, sim}; use glam::DVec3; use std::borrow::Cow; pub enum Source<'a> { Live(&'a sim::Sim), Recorded(&'a sim::record::Recording), Empty, } #[derive(Clone, Copy, Debug, PartialEq)] pub enum MoverKind { Drone(sim::DroneKind), Missile { kind: sim::MissileKind, engine_lit: bool }, Strike { physical: bool, engine_lit: bool }, } #[derive(Clone, Copy, Debug, PartialEq)] pub struct Mover { /// Stable identity within its `group`. pub id: u32, /// The old live-smoke trail categories: drone, interceptor, counter-strike. pub group: u8, pub kind: MoverKind, pub pos: DVec3, } pub enum Fire<'a> { Live(&'a sim::fires::Fire), Recorded(sim::record::ReplayFire), } impl Fire<'_> { pub fn key(&self) -> u32 { match self { Self::Live(f) => f.id, Self::Recorded(f) => f.key as u32, } } pub fn at(&self) -> DVec3 { match self { Self::Live(f) => f.at, Self::Recorded(f) => f.at, } } pub fn start(&self) -> u64 { match self { Self::Live(f) => f.start, Self::Recorded(f) => f.start as u64, } } pub fn age_offset(&self) -> u64 { match self { Self::Live(_) => 0, Self::Recorded(f) => f.age_offset as u64, } } pub fn size(&self) -> f32 { match self { Self::Live(f) => f.size, Self::Recorded(f) => f.size, } } pub fn quiet(&self) -> bool { match self { Self::Live(f) => f.quiet, Self::Recorded(f) => f.quiet, } } pub fn shown(&self) -> bool { match self { Self::Live(f) => f.shown, Self::Recorded(f) => f.shown, } } pub fn level(&self, tick: u64) -> (f32, f32) { match self { Self::Live(f) => f.level(tick), Self::Recorded(f) => { let (mut flame, mut smoke) = sim::fires::level( tick.saturating_sub(self.start()) as f64 + self.age_offset() as f64, f.life as f64, ); if f.water == Some(true) { flame = 0.0; smoke *= 0.5; } (flame, smoke) } } } } /// A selected instant from exactly one source. Recorded snapshots borrow only the recording; /// changing the current live simulation cannot change their contents. pub struct Snapshot<'a> { source: Source<'a>, time: f64, } impl<'a> Snapshot<'a> { pub fn at_context(ctx: &Context, source: Source<'a>) -> Self { let time = match &source { Source::Live(_) => ctx.combat_tick as f64, Source::Recorded(_) | Source::Empty => ctx.display_clock().value(), }; Self { source, time } } pub fn source(&self) -> &'static str { match &self.source { Source::Live(_) => "live", Source::Recorded(_) => "recorded", Source::Empty => "empty", } } pub fn time(&self) -> f64 { self.time } pub fn movers(&self) -> Vec { match &self.source { Source::Live(sim) => sim .drones .iter() .map(|d| Mover { id: d.id, group: 0, kind: MoverKind::Drone(d.kind), pos: d.pos, }) .chain(sim.missiles.iter().map(|m| Mover { id: m.id, group: 1, kind: MoverKind::Missile { kind: m.kind, engine_lit: m.engine_lit() }, pos: m.pos, })) .chain(sim.strikes.flights.iter().map(|f| { let physical = f.carrier.is_some(); Mover { id: f.id, group: 2, kind: MoverKind::Strike { physical, engine_lit: !physical || (self.time.max(0.0) as u64).saturating_sub(f.start) >= sim::strike::EJECT_TICKS as u64, }, pos: f.pos, } })) .collect(), Source::Recorded(rec) => rec .tracks .iter() .filter_map(|tr| { let pos = tr.at(rec.origin, self.time)?; let (group, kind) = match tr.what { sim::record::What::Drone(kind) => (0, MoverKind::Drone(kind)), sim::record::What::Missile(kind, _) => (1, MoverKind::Missile { kind, engine_lit: rec.missile_is_lit(tr, self.time, kind.lit_after()) }), sim::record::What::Strike => ( 2, MoverKind::Strike { physical: false, engine_lit: true, }, ), sim::record::What::StrikeCarrier(_) => ( 2, MoverKind::Strike { physical: true, engine_lit: (self.time - tr.first as f64).max(0.0) as u64 >= sim::strike::EJECT_TICKS as u64, }, ), }; Some(Mover { id: tr.id, group, kind, pos, }) }) .collect(), Source::Empty => Vec::new(), } } pub fn blasts(&self) -> Cow<'_, [sim::Blast]> { match &self.source { Source::Live(sim) => Cow::Borrowed(&sim.blasts), Source::Recorded(rec) => Cow::Owned(sim::record::replay_blasts(rec, self.time)), Source::Empty => Cow::Borrowed(&[]), } } /// Smoke billows intentionally outlive the visual blast. Live blasts are already retained /// according to their own lifetime; recorded events use the longer smoke projection. pub fn smoke_blasts(&self) -> Cow<'_, [sim::Blast]> { match &self.source { Source::Live(sim) => Cow::Borrowed(&sim.blasts), Source::Recorded(rec) => Cow::Owned(sim::record::replay_smoke_blasts(rec, self.time)), Source::Empty => Cow::Borrowed(&[]), } } pub fn fires(&self) -> Vec> { match &self.source { Source::Live(sim) => sim.fires.list.iter().map(Fire::Live).collect(), Source::Recorded(rec) => sim::record::replay_fires(rec, self.time) .into_iter() .map(Fire::Recorded) .collect(), Source::Empty => Vec::new(), } } pub fn recording(&self) -> Option<&sim::record::Recording> { match &self.source { Source::Live(_) => None, Source::Recorded(rec) => Some(rec), Source::Empty => None, } } /// Whether this snapshot was selected from a playback presentation, including a missing /// recording. An absent playback source must never expose cached live-only trails. pub fn is_recorded_view(&self) -> bool { matches!(&self.source, Source::Recorded(_) | Source::Empty) } } #[cfg(test)] mod tests { use super::*; use crate::presentation::{Clock, Mode}; #[test] fn recorded_snapshot_seek_is_independent_of_interleaved_live_simulations() { use sim::record::{Event, EventKind, Recording, What}; let mut rec = Recording::blank(160); rec.straight( 7, What::Drone(sim::DroneKind::Geran3), 0, 160, DVec3::new(3.0, 70.0, 0.0), DVec3::new(3.0, 70.0, 160.0), ); rec.events.push(Event { at: 40, pos: DVec3::new(3.0, 69.0, 40.0), kind: EventKind::Hit { drone: 7, kind: sim::DroneKind::Geran3, power: 2, object: None, sam: None, }, }); let mut live_a = sim::Sim::new(1); live_a.blasts.push(sim::Blast { id: 101, at: DVec3::X, size: 4.0, start: 40, life: 60, }); let mut live_b = sim::Sim::new(2); live_b.blasts.push(sim::Blast { id: 202, at: DVec3::Y, size: 8.0, start: 40, life: 60, }); let recorded_at = |time| { let ctx = Context::playback( Mode::Replay, 3, None, 0, 90_000, time, None, 0.25, DVec3::ZERO, true, ); let scene = Snapshot::at_context(&ctx, Source::Recorded(&rec)); let mover = scene.movers().into_iter().find(|m| m.id == 7).unwrap(); let blast_ids = scene.blasts().iter().map(|b| b.id).collect::>(); (scene.time(), mover.pos, blast_ids) }; let first = recorded_at(40.0); assert_eq!(first.0, 40.0); assert_eq!(first.2, vec![0x4000_0000]); let ctx_live = || Context::live(3, None, 0, 40, None, DVec3::ZERO); let a_ctx = ctx_live(); let a_scene = Snapshot::at_context(&a_ctx, Source::Live(&live_a)); assert_eq!(a_scene.blasts()[0].id, 101); let middle = recorded_at(120.0); let b_ctx = ctx_live(); let b_scene = Snapshot::at_context(&b_ctx, Source::Live(&live_b)); assert_eq!(b_scene.blasts()[0].id, 202); let again = recorded_at(40.0); assert_eq!( again, first, "seek back to the same recording instant is deterministic" ); assert_ne!( middle.1, first.1, "recorded mover poses advance with recording time" ); assert_eq!(first.1, DVec3::new(3.0, 70.0, 40.0)); assert_ne!(first.2, vec![101]); assert_ne!(first.2, vec![202]); assert_eq!(a_scene.time(), 40.0); assert_eq!(b_scene.time(), 40.0); assert!(matches!(a_ctx.clock, Clock::Live(40))); } }