diff --git a/AIRDEF-FORK.md b/AIRDEF-FORK.md new file mode 100644 index 000000000..56cf43035 --- /dev/null +++ b/AIRDEF-FORK.md @@ -0,0 +1,17 @@ +# AirDef Horizon fork + +Canonical repository: https://gitea.maoniao.team/chexit/horizon-airdef +Upstream: https://github.com/horizon-rust/horizon + +The initial import preserves upstream history and the 44 AirDef engine commits previously +reconstructed from airdef/horizon-patches. Its upstream baseline is +b6c936f82b08798057161bdfa577e3aac53fb91e (v0.2.93). +Updating the baseline is a separate change (AirDef issue #72). + +AirDef pins this repository with the horizon/ git submodule. Push engine commits here first, +then update the gitlink in AirDef after verification. New upstream commits never move an +existing AirDef checkout automatically. Engine source and SDK/WIT live here; gameplay and +content remain in the AirDef repository. Keep upstream licensing and attribution intact. + +Do not commit prepared Minecraft resources, game jars, run homes or build outputs. +Run tools/check_public_tree.py before publication (including history on initial import). diff --git a/crates/horizon-client/src/acoustics.rs b/crates/horizon-client/src/acoustics.rs index a5281d1b6..1d9387011 100644 --- a/crates/horizon-client/src/acoustics.rs +++ b/crates/horizon-client/src/acoustics.rs @@ -445,7 +445,7 @@ impl Acoustics { // A job in flight returns into a world that is gone: its sequence number is stale. self.seq += 1; self.floor = self.seq; - self.in_flight = None; + // The stale job still occupies the worker until its completion is received. } fn forget_rooms(&mut self) { @@ -501,15 +501,12 @@ impl Acoustics { self.stats.invalidated += self.cache.len() as u64; self.cache.clear(); self.echo_cache.clear(); - self.echo_applied.clear(); - self.echo_pending.clear(); - self.forget_rooms(); + // Keep mixer bookkeeping until scope reconciliation can recall effects. + self.room_cache.clear(); + self.room_now = None; self.log.clear(); self.seq += 1; self.floor = self.seq; - self.in_flight = None; - self.applied.clear(); - self.scoped.clear(); return; } for &s in &changes.sections { @@ -570,6 +567,7 @@ impl Acoustics { changes: &Changes, ) -> Vec { self.clock += 1; + let new_epoch = changes.epoch != self.epoch; self.invalidate(changes); // The resident chunks changed: a job traced from the snapshot before them applies to // chunks that may be gone, so its answers are dropped with the old residency's answers. @@ -577,7 +575,6 @@ impl Acoustics { self.residency = Some(changes.residency); self.seq += 1; self.floor = self.seq; - self.in_flight = None; } // The worker's results; one that began before a change in its box is not kept. if let Some((_, rx)) = &self.worker { @@ -640,6 +637,14 @@ impl Acoustics { let Some(geometry) = geometry else { return told; }; + if new_epoch { + // Reannounce live voices in the new world after reconciling those that left. + for s in &live { + self.applied.remove(&s.id); + self.echo_applied.remove(&s.id); + self.sends_applied.remove(&s.id); + } + } // The loudest first; ties by id, so the order never depends on a hash. let mut order: Vec = (0..live.len()).collect(); order.sort_by(|&a, &b| live[b].loud.total_cmp(&live[a].loud).then(live[a].id.cmp(&live[b].id))); @@ -1281,6 +1286,140 @@ mod tests { assert!(gone[0].cutoff > 0.5, "{gone:?}"); } + #[test] + fn reload_and_unload_invalidate_known_geometry_below_zero() { + let mut a = Acoustics::new(true); + let open = Wall::new(0..0, 1.0); + let wall = Wall::new(40..42, 1.0); + let listener = [0.5, -30.5, 0.5]; + let sources = [Source { + at: [90.5, -30.5, 0.5], + ..src(1, 90.5) + }]; + assert!(a.step(Some(&open), listener, &sources, &Changes::default())[0].cutoff > 0.5); + let mut columns = crate::world::ModChanges::default(); + columns.column(2, 0); + let (sections, overflow) = columns.take(); + let changed = Changes { + sections, + overflow, + residency: 1, + ..Default::default() + }; + assert!(a.step(Some(&wall), listener, &sources, &changed)[0].cutoff < 0.01); + columns.column(2, 0); + let (sections, overflow) = columns.take(); + let changed = Changes { + sections, + overflow, + residency: 2, + ..Default::default() + }; + assert!(a.step(Some(&open), listener, &sources, &changed)[0].cutoff > 0.5); + } + + #[test] + fn overflow_preserves_mixer_resets_for_scope_exit_and_missing_geometry() { + for missing in [false, true] { + let mut a = Acoustics::new(true); + let wall = Wall::new(100..103, 1.0); + let listener = [0.5, 70.5, 0.5]; + let near = [src(1, 200.5)]; + assert!(a.step(Some(&wall), listener, &near, &Changes::default())[0].gain < 1.0); + a.echo_applied.insert( + 1, + vec![Tap { + delay: 0.1, + path: 1.0, + level: 0.5, + cut: 1.0, + dir: [1.0, 0.0, 0.0], + }], + ); + a.sends_applied.insert(1, [0.5, 0.0, 0.0, 0.0]); + a.take_echoes(); + a.take_sends(); + let sources = if missing { near } else { [src(1, 600.5)] }; + let geometry = if missing { None } else { Some(&wall) }; + let out = a.step( + geometry, + listener, + &sources, + &Changes { + overflow: true, + ..Default::default() + }, + ); + assert_eq!(out.len(), 1); + assert_eq!(out[0].gain, 1.0); + assert_eq!(a.take_echoes(), vec![(1, vec![])]); + assert_eq!(a.take_sends(), vec![(1, [0.0; 4])]); + assert!(a.step(Some(&wall), listener, &near, &Changes::default())[0].gain < 1.0); + } + } + + #[test] + fn invalidation_does_not_enqueue_behind_a_busy_worker() { + struct Gate { + ready: std::sync::Mutex, + wake: std::sync::Condvar, + } + impl Geometry for Gate { + fn walker(&self) -> Box Option + '_> { + if std::thread::current().name() == Some("acoustics") { + let mut ready = self.ready.lock().unwrap(); + while !*ready { + ready = self.wake.wait(ready).unwrap(); + } + } + Box::new(|_, _, _| Some(0.0)) + } + } + let gate = Arc::new(Gate { + ready: false.into(), + wake: Default::default(), + }); + let geometry: Arc = gate.clone(); + let mut a = Acoustics::new(false); + let listener = [0.5, 70.5, 0.5]; + let sources = [src(1, 90.5)]; + for residency in 0..200 { + if residency % 25 == 24 { + a.reset(); + } + a.step( + Some(&geometry), + listener, + &sources, + &Changes { + residency, + overflow: residency % 11 == 10, + ..Default::default() + }, + ); + } + let retained = Arc::strong_count(&geometry) - 2; + *gate.ready.lock().unwrap() = true; + gate.wake.notify_all(); + assert!(retained <= 1, "retained jobs: {retained}"); + assert!(a.stats.skipped_busy > 0); + let changes = Changes { + residency: 199, + ..Default::default() + }; + for _ in 0..400 { + a.step(Some(&geometry), listener, &sources, &changes); + if a.stats.dropped_late > 0 && a.stats.jobs >= 2 { + return; + } + std::thread::sleep(std::time::Duration::from_millis(5)); + } + panic!( + "worker did not discard stale work and process the new snapshot: {:?}", + a.stats + ); + } + /// (AirDef fork #111.) A sound (or a listener) that walks out of the acoustic range gives /// up its old wall and takes the plain numbers of its new distance until it comes back. #[test] diff --git a/crates/horizon-client/src/world.rs b/crates/horizon-client/src/world.rs index 5c2b0c267..80b95a6a5 100644 --- a/crates/horizon-client/src/world.rs +++ b/crates/horizon-client/src/world.rs @@ -140,7 +140,7 @@ impl ModChanges { /// keyed to sections. Bounded like `note` is. pub fn column(&mut self, x: i32, z: i32) { if self.sections.len() < Self::MAX { - for sy in 0..SECTIONS as i32 { + for sy in (MIN_Y >> 4)..(MIN_Y >> 4) + SECTIONS as i32 { self.sections.insert((x, sy, z)); } } @@ -629,6 +629,24 @@ mod tests { use horizon_world::registry::BlockStateId; use std::sync::Arc; + #[test] + fn changed_column_uses_world_section_coordinates_and_stays_bounded() { + let mut changes = super::ModChanges::default(); + changes.column(2, -3); + let (sections, overflow) = changes.take(); + assert!(!overflow); + assert_eq!(sections.len(), crate::chunk::SECTIONS); + for sy in + (crate::chunk::MIN_Y >> 4)..(crate::chunk::MIN_Y >> 4) + crate::chunk::SECTIONS as i32 + { + assert!(sections.contains(&(2, sy, -3))); + } + for x in 0..12 { + changes.column(x, 0); + } + assert!(changes.take().1); + } + /// A box misses a state only where no section it reaches lists it, and /// unloaded columns or heights past the world count as their air. #[test]