//! 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 { 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 { 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"); } }