Files
airdef/airdef-mod/src/sim/lights.rs
T
chexit ccc3e20902
build / mod (push) Successful in 4m4s
release / windows (push) Successful in 23m13s
Playtest fixes: smoke by engine, searchlights, far launch sites that load their chunks, air defence balance, last chance and radar-cued shots, radars in smart raids, echo, Windows release build from a tag
Smoke: one rule by engine, no heat on trails, no trail on P1-SUN. Searchlights: two lamps a target, off with an empty sky. Halos fade before their reach and their sum is limited. Launch sites load chunks through the server thread, go off-map as a last resort, and reach 4000 blocks. Air defence: ranges x1.5, shots grow with urgency, claims released, /airdef lastchance, long shots on a big radar's tracks. Build: rust-toolchain.toml, airdef.py check, generator manifest and --check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 23:44:14 +03:00

212 lines
9.2 KiB
Rust

//! Night fighting: guns aim by sight, and at night they hit far less unless
//! the target is lit. Searchlights follow the network's tracks with their
//! beams; illumination flares (fired by the guns on their own, or by the
//! player) hang over the sky for half a minute and light everything round.
use super::*;
/// A flare's life, how far it lights, and when it stops climbing.
const FLARE_LIFE: u32 = 600;
const FLARE_LIGHT: f64 = 90.0;
const FLARE_CLIMB: u32 = 28;
/// How far a searchlight's beam reaches.
pub const BEAM: f64 = 450.0;
/// A searchlight's cone: full strength inside the inner half angle, none past the outer (radians).
pub const BEAM_INNER: f64 = 0.03;
pub const BEAM_OUTER: f64 = 0.075;
/// A target is lit once the axis is this close to it (inside the cone's soft edge).
const LIT_ANGLE: f64 = 0.05;
/// Most lamps on one target.
const MAX_LAMPS: u32 = 2;
/// Ticks between casts of the beam's axis against the land.
const CAST_EVERY: u64 = 4;
/// Guns' hits at night on an unlit target.
pub const DARK_HITS: f64 = 0.4;
/// Ticks between a gun's own flares.
const FLARE_EVERY: u32 = 500;
pub struct Flare {
id: u32,
pub pos: DVec3,
vel: DVec3,
age: u32,
}
impl Sim {
/// Fires an illumination flare from `from` towards `dir`: it climbs, bursts and hangs.
pub fn fire_flare(&mut self, from: DVec3, dir: DVec3, out: &mut Out) {
let id = self.id();
let vel = dir.normalize_or(DVec3::Y) * 2.4 + DVec3::Y * 1.4;
self.flares.push(Flare { id, pos: from, vel, age: 0 });
out.sound("minecraft:entity.firework_rocket.launch", from, 2.0, 0.6);
if self.flares.len() > 12 {
self.flares.remove(0);
}
}
/// The flares and the searchlights, and what they light; the guns' own flares.
pub(super) fn step_lights(&mut self, ground: &mut Ground, out: &mut Out) {
self.lit.clear();
let now = self.tick;
// Flares: up, a burst, then down slowly, swaying, lighting all round.
let mut keep = Vec::with_capacity(self.flares.len());
for mut f in std::mem::take(&mut self.flares) {
f.age += 1;
if f.age < FLARE_CLIMB {
f.pos += f.vel;
f.vel = f.vel * 0.94 - DVec3::Y * 0.04;
} else {
if f.age == FLARE_CLIMB {
out.sound("minecraft:entity.firework_rocket.blast", f.pos, 3.0, 0.5);
out.particles("minecraft:firework", f.pos, 1.0, DVec3::ZERO, 0.15, 30);
}
let sway = (f.age as f64 * 0.05 + f.id as f64).sin() * 0.04;
f.pos += DVec3::new(sway, -0.06, sway * 0.5);
for d in self.drones.iter().filter(|d| d.alive() && d.pos.distance(f.pos) < FLARE_LIGHT) {
self.lit.push(d.id);
}
if now % 3 == 0 {
out.particles("minecraft:firework", f.pos, 0.2, DVec3::new(0.0, -0.05, 0.0), 0.02, 1);
}
}
let fade = if f.age > FLARE_LIFE - 60 { (FLARE_LIFE - f.age) as f64 / 60.0 } else { 1.0 };
out.model(f.id, "airdef:flare", f.pos, DQuat::IDENTITY, 1.4 * fade.max(0.05), true);
if f.age < FLARE_LIFE {
keep.push(f);
}
}
self.flares = keep;
// What the network tracks, for the searchlights.
let tracked: Vec<u32> = self.sams.iter().flat_map(|s| s.seen.iter().copied()).collect();
// A lamp holds a target only while fewer than two others do; the sky is swept only while
// something flies in it.
let mut held: HashMap<u32, u32> = HashMap::new();
let anything = self.drones.iter().any(|d| d.alive());
let gun_targets: Vec<u32> = self.sams.iter().filter_map(|s| s.gun_target).collect();
for i in 0..self.sams.len() {
if self.sams[i].kind != SamKind::Light || !self.sams[i].alive() || self.sams[i].moving() {
self.sams[i].beam_on = false;
continue;
}
let (pos, id) = (self.sams[i].pos, self.sams[i].id);
if !self.night {
let sam = &mut self.sams[i];
sam.target = None;
sam.beam_on = false;
sam.pitch += (0.0 - sam.pitch) * 0.05;
continue;
}
// The guns' target first, else the nearest tracked drone in reach.
let target = self
.drones
.iter()
.filter(|d| d.alive() && tracked.contains(&d.id) && d.pos.distance(pos) < BEAM && held.get(&d.id).copied().unwrap_or(0) < MAX_LAMPS)
.min_by(|a, b| {
let key = |d: &Drone| d.pos.distance(pos) * if gun_targets.contains(&d.id) { 0.3 } else { 1.0 };
key(a).total_cmp(&key(b))
})
.map(|d| (d.id, d.pos));
if let Some((t, _)) = target {
*held.entry(t).or_insert(0) += 1;
} else if !anything {
let sam = &mut self.sams[i];
sam.target = None;
sam.beam_on = false;
continue;
}
let sam = &mut self.sams[i];
let (head, _) = sam.mount();
let (want_yaw, want_pitch, reach) = match target {
Some((_, at)) => {
let to = at - head;
(heading_of(to), (to.y / to.length().max(0.01)).asin(), to.length())
}
// No target: a slow sweep of the sky.
None => (now as f64 * 0.01 + id as f64, 0.9 + (now as f64 * 0.013).sin() * 0.3, 260.0),
};
sam.yaw = slew(sam.yaw, want_yaw, 0.12);
sam.pitch = slew(sam.pitch, want_pitch.clamp(0.05, 1.5), 0.08);
sam.target = target.map(|t| t.0);
// The beam: a spot light from the head (see `Sam::beam`); the axis is cast against the land
// now and then, and the light and the lit mark stop where the land does.
let (head, rot) = self.sams[i].mount();
let dir = rot * DVec3::NEG_Z;
let sam = &mut self.sams[i];
sam.beam_on = true;
if now.saturating_sub(sam.beam_cast) >= CAST_EVERY || sam.beam_free <= 0.0 {
sam.beam_cast = now;
sam.beam_free = match ground.world.probe(head + dir, head + dir * BEAM, now) {
world::Sweep::Hit { point, .. } => point.distance(head).max(1.0),
_ => BEAM,
};
}
let free = sam.beam_free;
// The target is lit once the beam is on it and nothing of the land is between, not while
// it still swings round.
if let Some((t, at)) = target
&& dir.angle_between(at - head) < LIT_ANGLE
&& (at - head).length() <= free + 2.0
{
self.lit.push(t);
}
// Without the renderer's spot light: segments widening with distance, thin; none with
// the effects off.
if self.spot_hd || !self.shader {
continue;
}
let reach = reach.min(free);
let mut along = 1.0;
// Up to 14 segments (the unit's part ids 2..15), each longer than the last: one
// reaches about 550 blocks, past the beam's reach, about 9 blocks wide out there.
for k in 0..14u32 {
if along >= reach {
break;
}
let size = 0.5 + along * 0.1;
let len = 3.0 * size;
out.model(id + 2 + k, "airdef:beam", head + dir * (along + len / 2.0), rot, size, true);
along += len;
}
}
// Guns at night light their own target with a flare now and then.
if self.night {
let mut fire: Vec<(DVec3, DVec3)> = Vec::new();
for sam in &mut self.sams {
sam.flare_cd = sam.flare_cd.saturating_sub(1);
if sam.kind.spec().gun.is_none() || !sam.alive() || sam.flare_cd > 0 {
continue;
}
let Some(t) = sam.gun_target.and_then(|t| self.drones.iter().find(|d| d.id == t)) else { continue };
if self.lit.contains(&t.id) || self.flares.iter().any(|f| f.pos.distance(t.pos) < FLARE_LIGHT) {
continue;
}
sam.flare_cd = FLARE_EVERY;
let from = sam.pos + DVec3::Y * 2.0;
fire.push((from, (t.pos + DVec3::Y * 25.0) - from));
}
for (from, dir) in fire {
self.fire_flare(from, dir, out);
}
}
}
}
/// A searchlight's beam for the renderer: where it starts, which way it points and how far it runs free.
pub struct Beam {
pub id: u32,
pub origin: DVec3,
pub dir: DVec3,
pub free: f64,
}
impl Sam {
/// The beam of a lit searchlight, from the lens of its head.
pub fn spot_beam(&self) -> Option<Beam> {
if self.kind != SamKind::Light || !self.alive() || !self.beam_on {
return None;
}
let (head, rot) = self.mount();
let dir = rot * DVec3::NEG_Z;
Some(Beam { id: self.id, origin: head + dir * 1.0, dir, free: self.beam_free })
}
}