diff --git a/crates/horizon-client/src/game/commands/pregen.rs b/crates/horizon-client/src/game/commands/pregen.rs index d61ba63ac..d497e2734 100644 --- a/crates/horizon-client/src/game/commands/pregen.rs +++ b/crates/horizon-client/src/game/commands/pregen.rs @@ -14,6 +14,8 @@ use std::time::{Duration, Instant}; /// How often the progress is reported. const REPORT_EVERY: Duration = Duration::from_secs(5); +/// How long the band may stay put before the task looks for what holds it. +const STALL: Duration = Duration::from_secs(30); pub(in crate::game) struct PregenTask { pregen: Pregen, @@ -23,6 +25,8 @@ pub(in crate::game) struct PregenTask { ran: Duration, resumed: Instant, reported: Instant, + /// When the band last moved, to notice a task that stopped advancing. + moved: Instant, /// The player who started it, told the progress too (a guest's connection). owner: Option, } @@ -113,6 +117,7 @@ impl Game<'_> { ran: Duration::ZERO, resumed: now, reported: now, + moved: now, owner: self.lan_acting, }); Ok(total as i32) @@ -181,13 +186,32 @@ impl Game<'_> { // (`postprocess_chunks`, on the tick), as vanilla's are by the time // they are FULL: then they may unload and be saved. let (streaming, chunks) = (&mut self.streaming, &self.world.chunks); - task.pregen.advance(|x, z| { + if task.pregen.advance(|x, z| { streaming.is_lit(x, z) && chunks .get(&(x, z)) .is_some_and(|c| c.postprocess.is_empty()) - }); + }) { + task.moved = Instant::now(); + } streaming.set_band(task.pregen.band()); + if task.moved.elapsed() >= STALL { + // Nothing advanced for a while: say what the first row waits + // for, and queue light again for chunks that lost their job. + task.moved = Instant::now(); + let row = task.pregen.first_row(); + let waiting = row + .iter() + .filter(|k| chunks.get(k).is_some_and(|c| !c.postprocess.is_empty())) + .count(); + let [missing, lighting, unlit] = streaming.unstick_light(&mut self.world, &row); + log::warn!( + "Horizon: pregeneration waits on row {:?}: {missing} not loaded, {lighting} being lit, \ + {unlit} unlit with no light job (queued again), {waiting} not post-processed; {:?}", + row.first(), + streaming.stages() + ); + } let finished = task.pregen.finished(); if finished || task.reported.elapsed() >= REPORT_EVERY { task.reported = Instant::now(); diff --git a/crates/horizon-client/src/pregen.rs b/crates/horizon-client/src/pregen.rs index f31a6d712..e714bcda2 100644 --- a/crates/horizon-client/src/pregen.rs +++ b/crates/horizon-client/src/pregen.rs @@ -105,6 +105,17 @@ impl Pregen { moved } + /// The chunks of the band's first row, which it waits for. + pub fn first_row(&self) -> Vec<(i32, i32)> { + let Some(&(x0, x1)) = self.strips.get(self.strip) else { + return Vec::new(); + }; + (x0..=x1) + .filter(|&x| self.contains(x, self.row)) + .map(|x| (x, self.row)) + .collect() + } + pub fn finished(&self) -> bool { self.strip >= self.strips.len() } diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 51c4eaea4..c9e858aea 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -1406,6 +1406,27 @@ impl Streaming { } self.band_ranked = Some((x0, x1, end.max(first - 1) + 1)); } + /// What holds up the columns `keys`, for a task that stopped advancing: + /// how many are not loaded, loaded but unlit with a light job in flight, + /// and unlit with none (those whose neighbours are all loaded are queued + /// for light again). + pub fn unstick_light(&mut self, world: &mut World, keys: &[(i32, i32)]) -> [usize; 3] { + let mut counts = [0; 3]; + for &key in keys { + if !world.chunks.contains_key(&key) { + counts[0] += 1; + } else if self.lit.contains(&key) { + } else if self.lighting.contains_key(&key) { + counts[1] += 1; + } else { + counts[2] += 1; + if Self::region(world, key.0, key.1).is_some() { + world.dirty_light.insert(key); + } + } + } + counts + } /// Wait up to `timeout` for a worker to finish a job; the next `update` /// takes it. A server pumps generation with this between its ticks. pub fn wait(&mut self, timeout: std::time::Duration) {