diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 57c7b9ee0..acd3c4cc5 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -363,6 +363,12 @@ impl Game<'_> { "open": true, "host": h.host_name, "port": h.settings.port, + // The server's own world: its finished chunks and how many it + // has generated or loaded so far. + "world": { + "chunks": self.world.chunks.len(), + "generated": self.streaming.generated_total, + }, "guests": h.hub.guests.iter().map(|g| json!({ "name": g.name, "position": g.position, @@ -1063,14 +1069,20 @@ impl Game<'_> { /// Keep the chunks around every guest loaded. fn lan_update_streaming(&mut self, host: &LanHost) { + // DistanceManager.PlayerTicketTracker: every chunk within the server's + // view distance (Chebyshev) of a player holds a PLAYER_LOADING ticket + // at the entity-ticking level (31), whatever the player asked for; + // levels rise by one a chunk, so FULL (33) reaches two chunks past + // it. An extra area finishes its chunks out to its radius + 1. That + // covers what the player tracks (`ChunkTrackingView`, within its view + // distance + 1) and those chunks' lit neighbours. + let radius = host.hub.config.view_distance + 1; self.streaming.extra = host .hub .guests .iter() .filter(|g| host.hub.here(g)) - // The tracked square's corners and their lit neighbours lie - // within (view distance + 2) * √2 of the player's chunk. - .map(|g| (g.chunk(), (g.view_distance + 2) * 3 / 2 + 1)) + .map(|g| (g.chunk(), radius)) .collect(); } diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 7b3f3dd50..bff0a8fd6 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -200,8 +200,10 @@ pub struct Streaming { /// Chunks come from a server (`insert_remote`/`forget`): nothing is /// generated and columns are only unloaded when the server says so. pub remote: bool, - /// More areas to keep loaded (centre chunk, radius), e.g. around guests - /// of a world open to the network. They are generated and kept but not meshed. + /// More areas to keep loaded (centre chunk, radius), e.g. around the + /// players of a server. Each is a square, like vanilla's ticket levels: + /// chunks finish out to `radius + 1` (Chebyshev) and generation reaches + /// past that as around the player. They are generated and kept but not meshed. pub extra: Vec<((i32, i32), i32)>, /// Mesh the sections for drawing (false on a server thread). pub mesh: bool, @@ -788,20 +790,17 @@ impl Streaming { world.dirty_sections.retain(|k| !far.contains(&(k.0, k.2))); } // Drop far proto chunks that are not checked out. - let areas: Vec<((i32, i32), i64)> = - std::iter::once((self.center, i64::from(self.radius + 4))) - .chain(self.extra.iter().map(|&(c, r)| (c, i64::from(r + 4)))) - .collect(); - let forced = &self.forced; + let (center, r4) = (self.center, i64::from(self.radius + 4)); + let ranges = self.ranges(); let far: Vec<(i32, i32)> = self .protos .iter() .filter(|&(&(x, z), p)| { p.is_some() - && !areas.iter().any(|&((cx, cz), r4)| { - i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2) <= r4 * r4 - }) - && !forced + && i64::from(x - center.0).pow(2) + i64::from(z - center.1).pow(2) > r4 * r4 + && !ranges.in_extra(x, z, 4) + && !ranges + .forced .iter() .any(|f| (x - f.0).abs() <= 3 && (z - f.1).abs() <= 3) }) @@ -1277,21 +1276,21 @@ impl Ranges<'_> { /// circles, so a fixed margin alone misses the corners of those squares /// (render distance 2 never finished the chunks next to the player). fn near_finished(&self, x: i32, z: i32, reach: i32) -> bool { - let near = |c: (i32, i32), radius: i32| { - let gap = |d: i32| i64::from((d.abs() - reach).max(0)); - let (dx, dz) = (gap(x - c.0), gap(z - c.1)); - let r = i64::from(radius + 1); - dx * dx + dz * dz <= r * r - }; - near(self.center, self.radius) || self.extra.iter().any(|&(c, r)| near(c, r)) + let gap = |d: i32| i64::from((d.abs() - reach).max(0)); + let (dx, dz) = (gap(x - self.center.0), gap(z - self.center.1)); + let r = i64::from(self.radius + 1); + // The extra areas are squares: their finished square grown by `reach`. + dx * dx + dz * dz <= r * r || self.in_extra(x, z, 1 + reach) } fn in_range(&self, x: i32, z: i32, margin: i32) -> bool { - self.in_main_range(x, z, margin) - || self.extra.iter().any(|&(c, radius)| { - let r = i64::from(radius + margin); - let (dx, dz) = (i64::from(x - c.0), i64::from(z - c.1)); - dx * dx + dz * dz <= r * r - }) + self.in_main_range(x, z, margin) || self.in_extra(x, z, margin) + } + /// Within `margin` of an extra area's square (Chebyshev distance, as + /// vanilla's ticket levels spread). + fn in_extra(&self, x: i32, z: i32, margin: i32) -> bool { + self.extra + .iter() + .any(|&(c, radius)| (x - c.0).abs().max((z - c.1).abs()) <= radius + margin) } /// The columns meshed for drawing: those vanilla draws around the /// player (`in_view_distance`), and forced ones. @@ -1778,4 +1777,58 @@ mod tests { } } } + + /// A server player's area (`lan_update_streaming`: the server view + /// distance + 1) finishes vanilla's FULL square: PLAYER_LOADING tickets at + /// level 31 out to the view distance, FULL (33) two chunks past it. Every + /// chunk the player tracks has its 3x3 neighbourhood finished, so it can + /// be lit and sent, and generation reaches vanilla's FEATURES (34) and + /// TERRAIN (35) rings. + #[test] + fn server_player_areas_finish_vanillas_full_square() { + let forced = HashSet::new(); + for view in 2..=32 { + let extra = [((40, -9), view + 1)]; + let r = Ranges { + // The window's own area, far away. + center: (-400, 300), + radius: 2, + extra: &extra, + forced: &forced, + }; + let level = |x: i32, z: i32| 31 + ((x - 40).abs().max((z + 9).abs()) - view).max(0); + for x in 40 - view - 8..=40 + view + 8 { + for z in -9 - view - 8..=-9 + view + 8 { + assert_eq!( + r.in_range(x, z, 1), + level(x, z) <= 33, + "view {view}: ({x}, {z})" + ); + assert_eq!( + r.requested(x, z, true), + level(x, z) <= 35, + "view {view}: ({x}, {z})" + ); + assert_eq!( + r.unloaded(x, z, true), + level(x, z) > 35, + "view {view}: ({x}, {z})" + ); + // ChunkTrackingView.contains, at any view distance the player may ask for. + let tracked = |v: i64| { + let gap = |d: i32| i64::from(((d).abs() - 2).max(0)); + gap(x - 40).pow(2) + gap(z + 9).pow(2) < v * v + }; + if tracked(i64::from(view)) { + for (dx, dz) in (-1..=1).flat_map(|dx| (-1..=1).map(move |dz| (dx, dz))) { + assert!( + r.in_range(x + dx, z + dz, 1), + "view {view}: ({x}, {z}) tracked, not lit" + ); + } + } + } + } + } + } }