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
build / mod (push) Successful in 3m21s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
//! The cinematic look of a blast (#51): a layered sound (a close crack, a low thump, a rolling
|
||||
//! tail; from afar the muffled report and a late boom), and the camera's reaction (a brief
|
||||
//! exposure and bloom punch). Everything is a function of the blast, its distance and its age, so
|
||||
//! a replay hears and sees the same as the live raid did, from the replay's camera.
|
||||
|
||||
/// The power (0 air burst .. 4 ballistic) of a blast sound event, if it is one.
|
||||
pub fn blast_power(event: &str) -> Option<usize> {
|
||||
Some(match event {
|
||||
"airdef:blast.air" => 0,
|
||||
"airdef:blast.medium" => 1,
|
||||
"airdef:blast.big" => 2,
|
||||
"airdef:blast.heavy" => 3,
|
||||
"airdef:blast.huge" => 4,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// One voice of a blast: the sound event (with `_far` where the listener is far), its loudness
|
||||
/// and pitch, and the ticks it follows the first after the sound has travelled.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Layer {
|
||||
pub name: String,
|
||||
pub volume: f32,
|
||||
pub pitch: f32,
|
||||
pub delay: u64,
|
||||
}
|
||||
|
||||
/// Blasts of one tick that get all their layers, the nearest first: the rest of a mass raid
|
||||
/// plays its one voice, so the key events are not buried and the mix does not clip.
|
||||
pub const FULL: usize = 3;
|
||||
|
||||
/// The voices of a blast of `power`, `dist` blocks from the listener (`far`: where the muffled
|
||||
/// variants take over), `gain` its loudness (1 normal; bigger warheads more), the `rank`-th
|
||||
/// nearest of its tick.
|
||||
pub fn layers(power: usize, dist: f64, far: f64, gain: f32, pitch: f32, rank: usize) -> Vec<Layer> {
|
||||
let g = gain.clamp(0.3, 2.0);
|
||||
let own = ["airdef:blast.air", "airdef:blast.medium", "airdef:blast.big", "airdef:blast.heavy", "airdef:blast.huge"][power.min(4)];
|
||||
let layer = |name: &str, volume: f32, pitch: f32, delay: u64| Layer { name: name.to_string(), volume, pitch, delay };
|
||||
let far_away = dist > far;
|
||||
let suffix = if far_away { "_far" } else { "" };
|
||||
if rank >= FULL {
|
||||
return vec![layer(&format!("{own}{suffix}"), g, pitch, 0)];
|
||||
}
|
||||
if far_away {
|
||||
// The muffled report, then a deep boom that arrives late.
|
||||
return vec![
|
||||
layer(&format!("{own}_far"), g, pitch, 0),
|
||||
layer("airdef:blast.big_far", 0.7 * g, pitch * 0.6, 10 + 2 * power as u64),
|
||||
];
|
||||
}
|
||||
let tail = (0.45 + 0.08 * power as f32) * g;
|
||||
if power == 0 {
|
||||
// An air burst has no body of its own: the crack, the thump, the roll.
|
||||
vec![
|
||||
layer("airdef:blast.air", g, pitch * 1.12, 0),
|
||||
layer("airdef:blast.big", 0.85 * g, pitch * 0.66, 2),
|
||||
layer("airdef:blast.huge", tail, pitch * 0.5, 7),
|
||||
]
|
||||
} else {
|
||||
vec![
|
||||
layer(own, g, pitch, 0),
|
||||
layer("airdef:blast.air", 0.7 * g, pitch * 1.15, 0),
|
||||
layer("airdef:blast.huge", tail, pitch * 0.5, 7),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
/// How hard the picture is punched now (0 to 1) by a blast of fireball radius `size` that went
|
||||
/// off `age` ticks ago `dist` blocks from the camera: bright and brief, near and big blasts most.
|
||||
pub fn punch(size: f32, age: f32, dist: f64) -> f32 {
|
||||
if age < 0.0 {
|
||||
return 0.0;
|
||||
}
|
||||
let reach = (size as f64 * 45.0).max(30.0);
|
||||
let near = (1.0 - dist / reach).clamp(0.0, 1.0) as f32;
|
||||
let big = (size / 8.0).clamp(0.35, 1.0);
|
||||
// A rise over a tick, then gone in about half a second.
|
||||
(near * big * (-age / 3.0).exp() * (age / 0.8).min(1.0)).clamp(0.0, 1.0)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_near_intercept_is_layered_and_a_mass_raid_is_bounded() {
|
||||
let l = layers(0, 40.0, 140.0, 1.2, 1.0, 0);
|
||||
assert_eq!(l.len(), 3);
|
||||
assert!(l.iter().all(|v| !v.name.ends_with("_far")));
|
||||
assert!(l[0].delay == 0 && l[2].delay > l[1].delay, "crack, then thump, then the roll");
|
||||
// A far one is the muffled report and a late boom.
|
||||
let f = layers(0, 400.0, 140.0, 1.0, 1.0, 0);
|
||||
assert!(f.iter().all(|v| v.name.ends_with("_far")) && f[1].delay > 0);
|
||||
// The rest of a tick's blasts play one voice, and the whole mix stays bounded.
|
||||
let total: f32 = (0..12).flat_map(|r| layers(2, 60.0, 140.0, 1.0, 1.0, r)).map(|v| v.volume).sum();
|
||||
let voices: usize = (0..12).map(|r| layers(2, 60.0, 140.0, 1.0, 1.0, r).len()).sum();
|
||||
assert_eq!(voices, FULL * 3 + (12 - FULL));
|
||||
assert!(total < 18.0, "{total}");
|
||||
assert_eq!(blast_power("airdef:blast.huge"), Some(4));
|
||||
assert_eq!(blast_power("airdef:boom.sonic"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_punch_is_brief_near_and_bounded() {
|
||||
assert_eq!(punch(5.0, -1.0, 10.0), 0.0);
|
||||
assert!(punch(5.0, 1.5, 20.0) > punch(5.0, 1.5, 150.0));
|
||||
assert!(punch(5.0, 1.5, 20.0) > punch(2.0, 1.5, 20.0));
|
||||
assert!(punch(13.0, 1.0, 0.0) <= 1.0);
|
||||
assert!(punch(5.0, 30.0, 20.0) < 0.01, "gone in a second and a half");
|
||||
}
|
||||
}
|
||||
+128
-16
@@ -12,6 +12,7 @@ use horizon_mod_sdk::{
|
||||
ui::{self, ButtonWidget, ItemWidget, ListWidget, Screen, Widget},
|
||||
};
|
||||
|
||||
mod cinema;
|
||||
mod monitor;
|
||||
mod panel;
|
||||
mod radar;
|
||||
@@ -88,6 +89,8 @@ struct AirDef {
|
||||
menu: Option<Page>,
|
||||
/// Where the player is, for the sounds' delay and far variants.
|
||||
listener: DVec3,
|
||||
/// The picture punch (#51) the HD exposure and bloom were last set for.
|
||||
punch: f32,
|
||||
/// Sounds on their way to the player at the speed of sound.
|
||||
pending: Vec<PendingSound>,
|
||||
/// The handles `render.model-id` gave the model names (valid under `model_generation`); none:
|
||||
@@ -163,6 +166,16 @@ fn say(text: &str) {
|
||||
chat::show(text);
|
||||
}
|
||||
|
||||
/// What the effects draw this tick (`Mod::shown`).
|
||||
struct Shown {
|
||||
blasts: Vec<sim::Blast>,
|
||||
/// The clock the blasts' `start` is on: the simulation's ticks, or the recording's.
|
||||
now: f32,
|
||||
/// 1 minus the share of the frame's partial tick the effects advance with.
|
||||
hold: f32,
|
||||
replay: bool,
|
||||
}
|
||||
|
||||
fn player() -> Option<game::Player> {
|
||||
game::get_player()
|
||||
}
|
||||
@@ -303,14 +316,37 @@ impl AirDef {
|
||||
/// blasts (a flash, then the fireball's glow dying down), burning wrecks and the
|
||||
/// searchlights' spots.
|
||||
fn lights(&self, out: &Out) -> Vec<PointLight> {
|
||||
let shown = self.shown();
|
||||
let mut lights: Vec<PointLight> = Vec::new();
|
||||
let mut add = |id: u32, at: DVec3, rgb: (f32, f32, f32), intensity: f32, radius: f32| {
|
||||
if lights.len() < 500 && intensity > 0.05 {
|
||||
lights.push(PointLight { id, x: at.x, y: at.y, z: at.z, color: rgb, intensity, radius: radius.clamp(1.0, 256.0) });
|
||||
}
|
||||
};
|
||||
let tick = self.sim.tick;
|
||||
let tick = shown.now as u64;
|
||||
let flicker = |id: u32| 0.85 + 0.15 * (((tick as u32).wrapping_mul(2654435761) ^ id.wrapping_mul(97)) % 100) as f32 / 100.0;
|
||||
// Blasts first, the nearest first: a crowded sky never costs the key intercept its light.
|
||||
let viewer = self.viewer(&shown);
|
||||
let mut near: Vec<&sim::Blast> = shown.blasts.iter().collect();
|
||||
if let Some(e) = viewer {
|
||||
near.sort_by(|a, b| a.at.distance_squared(e).total_cmp(&b.at.distance_squared(e)));
|
||||
}
|
||||
for (rank, b) in near.into_iter().enumerate() {
|
||||
let age = (shown.now - b.start as f32).max(0.0);
|
||||
let size = b.size;
|
||||
let glow = (-age / (b.life as f32 * 0.15).max(1.0)).exp();
|
||||
let flash = (-age * 0.35).exp() * 4.0;
|
||||
// Its own id range: the models' and the systems' ids are small numbers.
|
||||
let id = 0x6100_0000 | (b.id & 0x00ff_ffff);
|
||||
let centre = b.at + DVec3::Y * size as f64 * 0.5;
|
||||
add(id, centre, (1.0, 0.58, 0.25), size * size * 18.0 * (glow + flash), size * 14.0);
|
||||
// The punch (#51): a white burst that lights the land, the smoke and the wrecks far round
|
||||
// for a moment; for the nearest few, scaled by the blast.
|
||||
if rank < 6 {
|
||||
let burst = (-age / 2.0).exp();
|
||||
add(0x6300_0000 | (b.id & 0x00ff_ffff), centre, (1.0, 0.9, 0.74), size * size * 95.0 * burst, (size * 30.0).min(256.0));
|
||||
}
|
||||
}
|
||||
for m in out.models.iter().filter(|m| m.glow) {
|
||||
let s = m.scale.max(0.1);
|
||||
match m.name {
|
||||
@@ -323,14 +359,9 @@ impl AirDef {
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
for b in &self.sim.blasts {
|
||||
let age = tick.saturating_sub(b.start) as f32;
|
||||
let size = b.size;
|
||||
let glow = (-age / (b.life as f32 * 0.15).max(1.0)).exp();
|
||||
let flash = (-age * 0.35).exp() * 4.0;
|
||||
// Its own id range: the models' and the systems' ids are small numbers.
|
||||
let id = 0x6100_0000 | (b.id & 0x00ff_ffff);
|
||||
add(id, b.at + DVec3::Y * size as f64 * 0.5, (1.0, 0.58, 0.25), size * size * 18.0 * (glow + flash), size * 14.0);
|
||||
// A replay shows its own flames (the models of the ghosts) and none of the live world's.
|
||||
if shown.replay {
|
||||
return lights;
|
||||
}
|
||||
// Fires burn on for minutes: a flickering, wide glow that dies down with the flames.
|
||||
for f in self.sim.fires.list.iter().filter(|f| f.shown) {
|
||||
@@ -373,6 +404,7 @@ impl AirDef {
|
||||
/// Switches the HD lighting.
|
||||
fn apply_hd(&self) {
|
||||
render::set_lighting(Lighting { hd: self.hd, exposure: 1.0, bloom: 0.55 });
|
||||
// (The punch of the next blast sets its own.)
|
||||
if !self.hd {
|
||||
render::set_lights(&[]);
|
||||
}
|
||||
@@ -381,15 +413,15 @@ impl AirDef {
|
||||
/// Hands the simulation's output to the game.
|
||||
/// The post pass's parameters: blasts and glowing parts (shaders/effects.wgsl).
|
||||
fn shader_params(&self, out: &Out) -> Vec<f32> {
|
||||
let shown = self.shown();
|
||||
let mut p = vec![0.0f32; 4];
|
||||
p[0] = self.sim.tick as f32;
|
||||
p[0] = shown.now;
|
||||
p[3] = if self.smoke.hd { 2.0 } else if self.hd { 1.0 } else { 0.0 };
|
||||
let mut blasts: Vec<&sim::Blast> = self.sim.blasts.iter().rev().take(48).collect();
|
||||
let mut blasts: Vec<&sim::Blast> = shown.blasts.iter().rev().take(48).collect();
|
||||
// The shader composites the volumes front to back with one shared transmittance, so
|
||||
// the nearest to the player goes first. This is exact for volumes that do not
|
||||
// overlap; where two intersect, no order by centre is right along every ray.
|
||||
if let Some(pl) = player() {
|
||||
let e = eye(&pl);
|
||||
if let Some(e) = self.viewer(&shown) {
|
||||
blasts.sort_by(|a, b| a.at.distance_squared(e).total_cmp(&b.at.distance_squared(e)));
|
||||
}
|
||||
p[1] = blasts.len() as f32;
|
||||
@@ -420,9 +452,52 @@ impl AirDef {
|
||||
lights += 1;
|
||||
}
|
||||
p[2] = lights as f32;
|
||||
// The last vec4: how much of the frame's partial tick the effects run on (0 live; a
|
||||
// slowed or paused replay runs on its own time, not the frame's).
|
||||
p.resize(1024, 0.0);
|
||||
p[1020] = shown.hold;
|
||||
p
|
||||
}
|
||||
|
||||
/// The smoke volumes the renderer draws and the clock they are aged by: the live ones, or (in a
|
||||
/// replay) the blasts' billows at the replay's moment, never the live raid's trails and fires.
|
||||
/// The billows are the live ones' own function (`Smoke::segments`), run on the recording's
|
||||
/// blasts and time with the live state set aside for the call.
|
||||
fn shown_smoke(&mut self) -> (f64, Vec<render::SmokeSegment>) {
|
||||
let shown = self.shown();
|
||||
if !shown.replay {
|
||||
return (self.sim.tick as f64, self.smoke.segments(&self.sim));
|
||||
}
|
||||
let blasts = std::mem::replace(&mut self.sim.blasts, shown.blasts);
|
||||
let fires = std::mem::take(&mut self.sim.fires.list);
|
||||
let tick = std::mem::replace(&mut self.sim.tick, shown.now as u64);
|
||||
let segments = smoke::Smoke::default().segments(&self.sim);
|
||||
self.sim.blasts = blasts;
|
||||
self.sim.fires.list = fires;
|
||||
self.sim.tick = tick;
|
||||
(f64::from(shown.now), segments)
|
||||
}
|
||||
|
||||
/// Where the picture is seen from: the replay's camera, or the player's eyes.
|
||||
fn viewer(&self, shown: &Shown) -> Option<DVec3> {
|
||||
match &self.replay {
|
||||
Some(r) if shown.replay => Some(r.eye()),
|
||||
_ => player().map(|pl| eye(&pl)),
|
||||
}
|
||||
}
|
||||
|
||||
/// The blasts the effects show and the clock they run on: the live ones, or (in a replay) the
|
||||
/// recording's at the replay's moment, never the live world's.
|
||||
fn shown(&self) -> Shown {
|
||||
if let Some(r) = &self.replay
|
||||
&& r.filming()
|
||||
&& let Some(rec) = self.sim.replay(r.rec)
|
||||
{
|
||||
return Shown { blasts: sim::record::replay_blasts(rec, r.time()), now: r.time() as f32, hold: 1.0 - r.rate().clamp(0.0, 1.0), replay: true };
|
||||
}
|
||||
Shown { blasts: self.sim.blasts.clone(), now: self.sim.tick as f32, hold: 0.0, replay: false }
|
||||
}
|
||||
|
||||
/// The models to the game by handle: each name crosses once, the buffer stays allocated.
|
||||
fn send_models(&mut self, models: &[sim::Model]) {
|
||||
let generation = render::model_generation();
|
||||
@@ -460,8 +535,18 @@ impl AirDef {
|
||||
render::set_params(&self.shader_params(&out));
|
||||
}
|
||||
if self.hd {
|
||||
// The camera's reaction to a near blast: a brief exposure and bloom punch.
|
||||
let shown = self.shown();
|
||||
let p = self.viewer(&shown).map_or(0.0, |e| {
|
||||
shown.blasts.iter().map(|b| cinema::punch(b.size, shown.now - b.start as f32, b.at.distance(e))).fold(0.0, f32::max)
|
||||
});
|
||||
if (p - self.punch).abs() > 0.01 || (p == 0.0 && self.punch != 0.0) {
|
||||
self.punch = p;
|
||||
render::set_lighting(Lighting { hd: true, exposure: 1.0 + 0.4 * p, bloom: 0.55 + 0.9 * p });
|
||||
}
|
||||
render::set_lights(&self.lights(&out));
|
||||
self.smoke.hd = render::set_smoke(self.sim.tick as f64, &self.smoke.segments(&self.sim));
|
||||
let (clock, segments) = self.shown_smoke();
|
||||
self.smoke.hd = render::set_smoke(clock, &segments);
|
||||
} else {
|
||||
self.smoke.hd = false;
|
||||
}
|
||||
@@ -469,7 +554,7 @@ impl AirDef {
|
||||
self.send_models(&out.models);
|
||||
}
|
||||
if !out.particles.is_empty() {
|
||||
let hd_smoke = self.smoke.hd;
|
||||
let hd_smoke = self.smoke.hd && !self.replay.as_ref().is_some_and(|r| r.filming());
|
||||
let spawns: Vec<ParticleSpawn> = out
|
||||
.particles
|
||||
.into_iter()
|
||||
@@ -492,9 +577,13 @@ impl AirDef {
|
||||
render::spawn_particles(&spawns);
|
||||
}
|
||||
let now = self.sim.tick;
|
||||
// Blasts play layered (cinema.rs), the nearest ones in full.
|
||||
let mut booms: Vec<(f64, usize, DVec3, f32, f32)> = Vec::new();
|
||||
for (event, at, volume, pitch) in out.sounds {
|
||||
let dist = at.distance(self.listener);
|
||||
if travels(event) {
|
||||
if let Some(power) = cinema::blast_power(event) {
|
||||
booms.push((dist, power, at, volume, pitch));
|
||||
} else if travels(event) {
|
||||
// Seen first, heard later; from afar muffled.
|
||||
let name = if dist > FAR { format!("{event}_far") } else { event.to_string() };
|
||||
let delay = (dist / SOUND_SPEED) as u64;
|
||||
@@ -507,6 +596,18 @@ impl AirDef {
|
||||
render::play_sound(event, at.x, at.y, at.z, volume, pitch);
|
||||
}
|
||||
}
|
||||
booms.sort_by(|a, b| a.0.total_cmp(&b.0));
|
||||
for (rank, (dist, power, at, volume, pitch)) in booms.into_iter().enumerate() {
|
||||
for l in cinema::layers(power, dist, FAR, volume, pitch, rank) {
|
||||
// Seen first, heard later: each voice after the sound has travelled.
|
||||
let delay = (dist / SOUND_SPEED) as u64 + l.delay;
|
||||
if delay == 0 {
|
||||
render::play_sound(&l.name, at.x, at.y, at.z, l.volume, l.pitch);
|
||||
} else if self.pending.len() < 256 {
|
||||
self.pending.push(PendingSound { due: now + delay, name: l.name, at, volume: l.volume, pitch: l.pitch });
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut i = 0;
|
||||
while i < self.pending.len() {
|
||||
if self.pending[i].due <= now {
|
||||
@@ -794,6 +895,7 @@ impl AirDef {
|
||||
fn stop_replay(&mut self) {
|
||||
self.replay = None;
|
||||
render::set_time_override(None);
|
||||
render::set_main_view(None);
|
||||
input::claim_wheel(false);
|
||||
render::set_hud(&[]);
|
||||
render::set_views(&[]);
|
||||
@@ -1013,6 +1115,7 @@ impl Mod for AirDef {
|
||||
health_mark: 0,
|
||||
menu: None,
|
||||
listener: DVec3::ZERO,
|
||||
punch: 0.0,
|
||||
pending: Vec::new(),
|
||||
model_ids: std::collections::HashMap::new(),
|
||||
model_generation: u32::MAX,
|
||||
@@ -1148,6 +1251,13 @@ impl Mod for AirDef {
|
||||
render::query_sections(render::Policy::Collision, §ions);
|
||||
}
|
||||
if let Some(mut r) = self.replay.take() {
|
||||
// A replay shows the recording alone: not the live raid's models, particles, sounds or jolts.
|
||||
if r.filming() {
|
||||
out.models.clear();
|
||||
out.particles.clear();
|
||||
out.sounds.clear();
|
||||
out.shake = None;
|
||||
}
|
||||
r.tick(&self.sim, self.listener, &mut out);
|
||||
if r.done {
|
||||
self.replay = Some(r);
|
||||
@@ -1163,6 +1273,8 @@ impl Mod for AirDef {
|
||||
let size = render::gui_size();
|
||||
render::set_hud(&r.hud(&self.sim, size));
|
||||
render::set_views(&r.views(size));
|
||||
// The world through the director's camera, with the full pipeline (HD, the post pass).
|
||||
render::set_main_view(r.filming().then(|| r.view_id()));
|
||||
// The sky of the raid, not of now (None for a raid whose clock was unknown).
|
||||
render::set_time_override(r.day_time(&self.sim));
|
||||
self.monitor_shown = true;
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::sim::record::{EventKind, Recording, STEP, What};
|
||||
use std::cell::Cell;
|
||||
use crate::sim::{Class, DroneKind, Out, Sim};
|
||||
use glam::DVec3;
|
||||
use horizon_mod_sdk::render::{CameraView, FillShape, Point, QuadShape, Rect, Shape, TextShape, ViewShape};
|
||||
use horizon_mod_sdk::render::{CameraView, FillShape, Point, QuadShape, Rect, Shape, TextShape};
|
||||
|
||||
/// Ghost models' ids, clear of the live ones.
|
||||
const GHOSTS: u32 = 0x5000_0000;
|
||||
@@ -37,6 +37,8 @@ pub struct Player {
|
||||
pub mode: Mode,
|
||||
t: f64,
|
||||
speed: f64,
|
||||
/// How fast the replay's time ran this tick (0 paused or held on a card; slowed round a moment).
|
||||
rate: f64,
|
||||
/// The director's current shot (event, shot) and the view id it films with.
|
||||
shot: (usize, u8),
|
||||
view: u32,
|
||||
@@ -213,6 +215,7 @@ impl Player {
|
||||
mode,
|
||||
t: start,
|
||||
speed,
|
||||
rate: 0.0,
|
||||
shot: (usize::MAX, 0),
|
||||
view: VIEW,
|
||||
fade: 0,
|
||||
@@ -297,6 +300,26 @@ impl Player {
|
||||
self.fade = 6;
|
||||
}
|
||||
|
||||
/// Where the camera is this tick.
|
||||
pub fn eye(&self) -> DVec3 {
|
||||
self.eye
|
||||
}
|
||||
/// The replay's moment, in the recording's ticks.
|
||||
pub fn time(&self) -> f64 {
|
||||
self.t
|
||||
}
|
||||
/// How fast the replay's time runs now, as a share of real time (1 normal, 0 held).
|
||||
pub fn rate(&self) -> f32 {
|
||||
self.rate as f32
|
||||
}
|
||||
/// Whether the world is filmed (the map is drawn on the HUD instead).
|
||||
pub fn filming(&self) -> bool {
|
||||
!matches!(self.mode, Mode::Map)
|
||||
}
|
||||
/// The camera the world is rendered from (`set-main-view`).
|
||||
pub fn view_id(&self) -> u32 {
|
||||
self.view
|
||||
}
|
||||
/// The time of day the raid was flown in, as of the replay's moment now.
|
||||
pub fn day_time(&self, sim: &Sim) -> Option<u64> {
|
||||
sim.replay(self.rec)?.day_time(self.t)
|
||||
@@ -325,6 +348,7 @@ impl Player {
|
||||
speed = 0.0;
|
||||
}
|
||||
}
|
||||
self.rate = if self.paused { 0.0 } else { speed };
|
||||
if !self.paused {
|
||||
self.t += speed;
|
||||
}
|
||||
@@ -365,7 +389,8 @@ impl Player {
|
||||
EventKind::SamLost { .. } => "airdef:blast.big",
|
||||
EventKind::Split { .. } => "airdef:boom.sonic",
|
||||
};
|
||||
out.sound(event, heard(e.pos), 1.0, 1.0);
|
||||
let gain = if let EventKind::Kill { kind, .. } = e.kind { crate::sim::record::kill_look(kind).2 } else { 1.0 };
|
||||
out.sound(event, heard(e.pos), gain, 1.0);
|
||||
}
|
||||
for tr in rec.tracks.iter().filter(|tr| (self.heard..self.t).contains(&(tr.first as f64))) {
|
||||
if let What::Missile(kind, _) = tr.what
|
||||
@@ -508,8 +533,8 @@ impl Player {
|
||||
if matches!(self.mode, Mode::Map) {
|
||||
return self.map(rec, gw, gh);
|
||||
}
|
||||
let mut s = vec![fill(0.0, 0.0, gw, gh, 0xFF000000)];
|
||||
s.push(Shape::View(ViewShape { view: self.view, rect: Rect { x: 0.0, y: 0.0, w: gw, h: gh }, mode: 0 }));
|
||||
// The world itself is the camera's (`set-main-view`): the HUD only frames it.
|
||||
let mut s: Vec<Shape> = Vec::new();
|
||||
// Letterbox, the recording's marks.
|
||||
let bar = (gh * 0.09).round();
|
||||
s.push(fill(0.0, 0.0, gw, bar, 0xFF000000));
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user