Replays in full HD with shader effects driven by the recording (#56), recorded clock survives gaps (#57), cinematic intercepts: layered sound with travel delay, big flash, light burst, exposure punch, shock ring (#51) (fork patch 31)
build / mod (push) Successful in 3m21s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-30 03:59:47 +03:00
parent 4bab572293
commit 8a12225ba1
40 changed files with 1185 additions and 105 deletions
+9 -3
View File
@@ -251,9 +251,15 @@ impl Sim {
/// A fire for a replay: the event's power at age `age` ticks, as a glowing flame model's scale
/// (zero when it is out or not started) and a smoke strength.
pub fn replay_fire(power: u32, age: f64) -> (f32, f32) {
if age < 0.0 {
replay_fire_of(size_for(power), life_for(power), age)
}
/// The same for a fire of a given `size` and `life` (a merged fire has grown): out (zero) before
/// it starts and from the end of its life on, as the live fire is removed then.
pub fn replay_fire_of(size: f32, life: u32, age: f64) -> (f32, f32) {
if !(0.0..life as f64).contains(&age) {
return (0.0, 0.0);
}
let (fl, sm) = level(age, life_for(power) as f64);
(fl * size_for(power), sm)
let (fl, sm) = level(age, life as f64);
(fl * size, sm)
}
+7 -5
View File
@@ -2015,10 +2015,10 @@ impl Sim {
}
if let Some(p) = player {
let d = p.distance(pos);
let reach = [45.0, 45.0, 100.0, 160.0, 240.0][power];
let reach = [70.0, 45.0, 100.0, 160.0, 240.0][power];
if d < reach {
let strength = [0.4, 0.4, 0.9, 1.3, 1.8][power];
out.jolt((1.0 - d / reach) as f32 * strength, [0.8, 0.8, 1.6, 2.2, 3.0][power]);
let strength = [0.55, 0.4, 0.9, 1.3, 1.8][power];
out.jolt((1.0 - d / reach) as f32 * strength, [1.0, 0.8, 1.6, 2.2, 3.0][power]);
}
}
self.blast_damage(pos, power, out);
@@ -2075,11 +2075,13 @@ impl Sim {
if let Some(s) = self.sams.iter_mut().find(|s| s.id == by) {
s.kills += 1;
}
self.fireball(pos, 3.5, 8);
// The look of the kill goes by the warhead it blew up (replays use the same).
let (size, life, gain) = record::kill_look(self.drones[idx].kind);
self.fireball(pos, size as f64, life / 9);
out.particles("minecraft:explosion", pos, 0.6, DVec3::ZERO, 0.0, 4);
out.particles("minecraft:large_smoke", pos, 1.0, DVec3::ZERO, 0.06, 25);
out.particles("minecraft:firework", pos, 0.3, DVec3::ZERO, 0.35, 30);
out.sound("airdef:blast.air", pos, 1.0, 1.0 + self.rng.f() as f32 * 0.2);
out.sound("airdef:blast.air", pos, gain, 1.0 + self.rng.f() as f32 * 0.2);
for _ in 0..5 {
let id = self.id();
let v = vel + DVec3::new(self.rng.s(), self.rng.f() * 0.8, self.rng.s()) * 0.35;
+111 -37
View File
@@ -114,9 +114,11 @@ pub struct Recording {
/// The systems and objects as they stood: (id, kind, where).
pub sams: Vec<(u32, SamKind, DVec3)>,
pub objects: Vec<DVec3>,
/// The world clock's time of day (ticks) every `TIME_STEP` ticks of the recording, from its
/// start: replays show the sky of the raid, not the current one. Empty when the clock was unknown.
times: Vec<u64>,
/// The world clock's time of day as (recording tick, day ticks), sampled on every
/// `TIME_STEP`-th tick the clock was known: replays show the sky of the raid, not the current
/// one. A tick the clock was unknown has no sample (a gap), so a late start or a lost clock
/// never shifts later samples; empty when the clock was never known.
times: Vec<(u32, u64)>,
/// Ticks the sky has been clear.
quiet: u32,
}
@@ -166,6 +168,86 @@ impl Track {
}
}
/// A fire as a replay shows it: what the events lit and kept alight, merged as the live fires are.
#[derive(Clone, Copy, Debug)]
pub struct ReplayFire {
pub at: DVec3,
/// The recording's tick it was lit.
pub start: u32,
pub life: u32,
pub size: f32,
/// The event that lit it (the models' ids).
pub key: usize,
}
/// The look of an interception by the drone's warhead: (fireball radius, ticks it burns, loudness).
/// A bare decoy pops; a big warhead lights the sky. The same in the live raid and its replay.
pub fn kill_look(kind: DroneKind) -> (f32, u32, f32) {
let w = kind.spec().warhead.min(8) as f32;
(3.5 * (0.85 + 0.12 * w), (8 + (w as u32) / 2) * 9, 1.0 + 0.06 * w)
}
/// A blast's look for an event: (fireball radius, ticks it burns). The live blasts use the same.
pub fn blast_spec(kind: EventKind) -> (f32, u32) {
const SIZE: [f32; 5] = [2.5, 4.5, 7.0, 10.0, 13.0];
const LIFE: [u32; 5] = [11, 11, 18, 22, 26];
match kind {
EventKind::Kill { kind, .. } => {
let (size, life, _) = kill_look(kind);
(size, life)
}
EventKind::Hit { power, .. } | EventKind::Crash { power, .. } => {
let p = (power as usize).min(4);
(SIZE[p], LIFE[p] * 9)
}
EventKind::SamLost { .. } => (8.0, 18 * 9),
EventKind::Split { .. } => (2.0, 7 * 9),
}
}
/// The blasts of the recording's events burning at `t` (its ticks, fractional): the same
/// `Blast`s the live simulation keeps, with `start` in the recording's ticks and the event's
/// own id range. A pure function of `t`, so a seek or a pause shows the same frame.
pub fn replay_blasts(rec: &Recording, t: f64) -> Vec<Blast> {
rec.events
.iter()
.enumerate()
.filter_map(|(k, e)| {
let (size, life) = blast_spec(e.kind);
let age = t - e.at as f64;
(0.0..life as f64).contains(&age).then_some(Blast { id: 0x4000_0000 | k as u32, at: e.pos, size, start: e.at as u64, life })
})
.collect()
}
/// The fires burning at `t` in the recording: events within 6 blocks of a fire still burning
/// feed it (the greater size, the longer life), as `Sim::ignite` does; those after it died light
/// a new one. A pure function of `t`.
pub fn replay_fires(rec: &Recording, t: f64) -> Vec<ReplayFire> {
let mut fires: Vec<ReplayFire> = Vec::new();
for (k, e) in rec.events.iter().enumerate() {
if e.at as f64 > t {
break;
}
let power = match e.kind {
EventKind::Crash { power, .. } => power,
EventKind::Hit { power, .. } => power.max(1),
EventKind::SamLost { .. } => 2,
_ => continue,
};
let (size, life) = (fires::size_for(power), fires::life_for(power));
let alive = |f: &ReplayFire| e.at < f.start + f.life;
if let Some(f) = fires.iter_mut().find(|f| alive(f) && f.at.distance(e.pos) < 6.0) {
f.size = f.size.max(size);
f.life = f.life.max(e.at - f.start + life);
} else {
fires.push(ReplayFire { at: e.pos, start: e.at, life, size, key: k });
}
}
fires.retain(|f| t < (f.start + f.life) as f64);
fires
}
impl Recording {
fn new(number: u32, sim: &Sim) -> Self {
let origin = sim.targets.first().copied().or_else(|| sim.sams.first().map(|s| s.pos)).unwrap_or(DVec3::ZERO);
@@ -199,12 +281,16 @@ impl Recording {
/// clock as sampled, run on between samples at the pace it ran (a jump is a step), or None
/// when the clock was not known (the replay then shows the world's own time).
pub fn day_time(&self, t: f64) -> Option<u64> {
let k = (t.max(0.0) / TIME_STEP as f64).floor() as usize;
let i = k.min(self.times.len().checked_sub(1)?);
let a = self.times[i];
let into = if i == k { t.max(0.0) - (i as u32 * TIME_STEP) as f64 } else { 0.0 };
let t = t.max(0.0);
// The last sample at or before `t`; none before the first one means unknown.
let i = self.times.partition_point(|s| s.0 as f64 <= t).checked_sub(1)?;
let (ta, a) = self.times[i];
match self.times.get(i + 1) {
Some(&b) if b >= a && b - a <= 2 * TIME_STEP as u64 => Some(a + ((b - a) as f64 * into / TIME_STEP as f64) as u64),
// Neighbouring samples with no gap between: run on at the pace between them.
Some(&(tb, b)) if tb - ta <= TIME_STEP && b >= a && b - a <= 2 * TIME_STEP as u64 => {
let f = ((t - ta as f64) / (tb - ta) as f64).clamp(0.0, 1.0);
Some(a + ((b - a) as f64 * f) as u64)
}
_ => Some(a),
}
}
@@ -271,9 +357,8 @@ impl Sim {
let Some(r) = &mut self.rec else { return };
if r.len % TIME_STEP == 0
&& let Some(dt) = self.day_time
&& r.times.len() == (r.len / TIME_STEP) as usize
{
r.times.push(dt);
r.times.push((r.len, dt));
}
if r.len % STEP == 0 {
for d in &self.drones {
@@ -349,43 +434,32 @@ impl Sim {
}
}
}
// Fires: what the events left burning (the flames dim and the smoke thickens with time).
let mut lit: Vec<(DVec3, f64)> = Vec::new();
for (k, e) in rec.events.iter().enumerate() {
let power = match e.kind {
EventKind::Crash { power, .. } => power,
EventKind::Hit { power, .. } => power.max(1),
EventKind::SamLost { .. } => 2,
_ => continue,
};
let age = t - e.at as f64;
// One fire where several events met.
if lit.iter().any(|(p, at)| p.distance(e.pos) < 6.0 && *at <= e.at as f64) {
continue;
}
lit.push((e.pos, e.at as f64));
let (flame, smoke) = fires::replay_fire(power, age);
let id = base + 0x00c0_0000 + k as u32 * PARTS;
// Fires: what the events left burning (the flames dim and the smoke thickens with time),
// out when their life ends.
for f in replay_fires(rec, t) {
let age = t - f.start as f64;
let (flame, smoke) = fires::replay_fire_of(f.size, f.life, age);
let id = base + 0x00c0_0000 + f.key as u32 * PARTS;
if flame > 0.05 {
let wob = 1.0 + 0.15 * (age * 0.9 + k as f64).sin();
out.model(id, "airdef:flame", e.pos + DVec3::Y * 0.5, DQuat::from_rotation_y(age * 0.07), (flame as f64 * 0.8 * wob).max(0.05), true);
let wob = 1.0 + 0.15 * (age * 0.9 + f.key as f64).sin();
out.model(id, "airdef:flame", f.at + DVec3::Y * 0.5, DQuat::from_rotation_y(age * 0.07), (flame as f64 * 0.8 * wob).max(0.05), true);
}
if smoke > 0.1 && (age as u64) % 3 == 0 {
out.particles("minecraft:large_smoke", e.pos + DVec3::Y, 0.8, DVec3::new(0.0, 0.1, 0.0), 0.03, 1 + (smoke * 2.0) as u32);
out.particles("minecraft:large_smoke", f.at + DVec3::Y, 0.8, DVec3::new(0.0, 0.1, 0.0), 0.03, 1 + (smoke * 2.0) as u32);
}
}
// Blasts: a fireball growing and fading for 1.5 s after each.
// Blasts: with the shader effects on, the post pass and the HD lights draw them from
// `replay_blasts` (the same as the live ones); the models are the fallback without them.
if self.shader {
return;
}
for (k, e) in rec.events.iter().enumerate() {
let age = t - e.at as f64;
if !(0.0..30.0).contains(&age) {
continue;
}
let size = match e.kind {
EventKind::Kill { .. } => 3.5,
EventKind::Hit { power, .. } | EventKind::Crash { power, .. } => [2.5, 4.5, 7.0, 10.0, 13.0][(power as usize).min(4)],
EventKind::SamLost { .. } => 8.0,
EventKind::Split { .. } => 2.0,
};
let (size, _) = blast_spec(e.kind);
let size = size as f64;
let f = age / 30.0;
let s = size * (f * 4.0).min(1.0) * (1.0 - f * f);
let id = base + 0x0080_0000 + k as u32 * PARTS;
+75
View File
@@ -534,6 +534,81 @@ fn recordings_keep_the_time_of_day() {
}
}
// #57: a clock missing at the start, in the middle, or later recovering never loses or shifts the samples.
#[test]
fn recorded_clock_survives_gaps() {
let mut sim = Sim::new(1);
sim.add_drone(DroneKind::Geran3, DVec3::new(0.0, 80.0, 0.0), DVec3::Z, DVec3::new(0.0, 70.0, 500.0));
let mut g = Ground::default();
let mut out = Out::default();
for i in 0..100u64 {
// Unknown at the start (ticks 0-19) and for a stretch in the middle (50-69).
sim.day_time = if i < 20 || (50..70).contains(&i) { None } else { Some(1000 + i) };
sim.step(&mut g, None, &mut out);
}
let r = sim.rec.as_ref().unwrap();
assert_eq!(r.day_time(5.0), None, "before the first sample: unknown, not the current time");
let v = r.day_time(30.0).expect("recovered after a missing start");
assert!((1020..1040).contains(&v), "{v}");
// Inside the gap the last known time holds; nothing is invented.
let held = r.day_time(60.0).unwrap();
assert!((1030..1050).contains(&held), "{held}");
// After the gap the new samples land at their own ticks (seek forward and back alike).
let after = r.day_time(85.0).unwrap();
assert!((1080..1100).contains(&after), "{after}");
assert!(r.day_time(30.0).unwrap() < r.day_time(85.0).unwrap());
assert!(r.day_time(85.0).unwrap() > r.day_time(60.0).unwrap());
assert!(r.day_time(25.0).unwrap() <= r.day_time(35.0).unwrap());
}
// #56: a replay's fires and blasts are pure functions of its time: they end with their life,
// merge as the live ones do, and a seek back shows the same frame.
#[test]
fn replay_fires_and_blasts_follow_the_recording() {
use record::{Event, EventKind};
let mut sim = Sim::new(1);
sim.day_time = Some(1000);
sim.add_drone(DroneKind::Geran3, DVec3::new(0.0, 80.0, 0.0), DVec3::Z, DVec3::new(0.0, 70.0, 500.0));
let mut g = Ground::default();
let mut out = Out::default();
sim.step(&mut g, None, &mut out);
let mut rec = sim.rec.take().unwrap();
let at = DVec3::new(5.0, 70.0, 5.0);
let crash = |power| EventKind::Crash { drone: 1, kind: DroneKind::Geran3, power };
let life = fires::life_for(1);
rec.events = vec![
Event { at: 10, pos: at, kind: crash(1) },
// The same spot while it burns: one fire, burning longer.
Event { at: 500, pos: at + DVec3::X, kind: crash(3) },
// The same spot long after it died: a new fire.
Event { at: 500 + 4000 + fires::life_for(3), pos: at, kind: crash(1) },
];
assert!(record::replay_fires(&rec, 0.0).is_empty(), "before the first event");
let f = record::replay_fires(&rec, 600.0);
assert_eq!(f.len(), 1);
assert_eq!(f[0].start, 10);
assert!(f[0].life >= 490 + fires::life_for(3) && f[0].size >= fires::size_for(3), "fed: {:?}", f[0]);
// Past its life it is gone: no flames, no smoke.
let end = f[0].start + f[0].life;
assert!(record::replay_fires(&rec, end as f64 - 1.0).len() == 1);
assert!(record::replay_fires(&rec, end as f64 + 1.0).is_empty());
assert_eq!(fires::replay_fire(1, life as f64 + 10.0), (0.0, 0.0));
assert_eq!(fires::replay_fire(1, 1.0e5), (0.0, 0.0));
assert!(fires::replay_fire(1, 100.0).1 > 0.0);
// The later event lights a new one.
let f = record::replay_fires(&rec, (500 + 4000 + fires::life_for(3) + 5) as f64);
assert_eq!(f.len(), 1);
assert_eq!(f[0].start, 500 + 4000 + fires::life_for(3));
// Seeking back and forth gives the same blasts.
let a = record::replay_blasts(&rec, 20.0);
let _ = record::replay_blasts(&rec, 510.0);
let b = record::replay_blasts(&rec, 20.0);
assert_eq!(a.len(), 1);
assert_eq!((a[0].id, a[0].start, a[0].life), (b[0].id, b[0].start, b[0].life));
assert!(record::replay_blasts(&rec, 5.0).is_empty());
assert!(record::replay_blasts(&rec, 10.0 + a[0].life as f64).is_empty(), "burnt out");
}
fn sim_first(r: &record::Recording) -> u64 {
r.tracks.first().map_or(0, |t| t.first as u64)
}