From 4de112fe1672088ce7fe4143ae6c416bc4b9fc50 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 18 Oct 2026 19:06:34 +0100 Subject: [PATCH 01/12] Fix multiplayer tick ordering and interaction target parity --- .../examples/interaction_input_fixture.rs | 122 ++++++ .../examples/player_sync_fixture.rs | 72 ++++ crates/horizon-client/src/game/hand.rs | 2 +- crates/horizon-client/src/game/remote.rs | 378 ++++++++++++------ .../horizon-client/src/interaction_input.rs | 186 +++++++++ crates/horizon-client/src/lib.rs | 2 + crates/horizon-client/src/net.rs | 63 ++- crates/horizon-client/src/player_sync.rs | 86 ++++ .../parity/VanillaInteractionInputOracle.java | 161 ++++++++ .../parity/VanillaPlayerSyncOracle.java | 109 +++++ tools/test_interaction_input_parity.py | 53 +++ tools/test_player_sync_parity.py | 76 ++++ 12 files changed, 1183 insertions(+), 127 deletions(-) create mode 100644 crates/horizon-client/examples/interaction_input_fixture.rs create mode 100644 crates/horizon-client/examples/player_sync_fixture.rs create mode 100644 crates/horizon-client/src/interaction_input.rs create mode 100644 crates/horizon-client/src/player_sync.rs create mode 100644 mods/horizon/src/test/java/dev/horizonlab/parity/VanillaInteractionInputOracle.java create mode 100644 mods/horizon/src/test/java/dev/horizonlab/parity/VanillaPlayerSyncOracle.java create mode 100644 tools/test_interaction_input_parity.py create mode 100644 tools/test_player_sync_parity.py diff --git a/crates/horizon-client/examples/interaction_input_fixture.rs b/crates/horizon-client/examples/interaction_input_fixture.rs new file mode 100644 index 000000000..bb408dd88 --- /dev/null +++ b/crates/horizon-client/examples/interaction_input_fixture.rs @@ -0,0 +1,122 @@ +use horizon_client::interaction_input::{ + Button, InteractionInput, InteractionWorld, TargetUseWorld, use_on_target, +}; +use serde_json::Value; +use std::io::{BufRead, Write}; + +struct World { + using: bool, + destroying: bool, + entity: bool, + dig_on_attack: bool, + begin_using: bool, + delay: u32, + events: Vec, + passing: bool, +} +impl InteractionWorld for World { + fn using_item(&self) -> bool { + self.using + } + fn release_using(&mut self) { + self.events.push("release".into()); + self.using = false; + } + fn start_attack(&mut self) -> bool { + self.events.push("attack".into()); + if !self.entity { + self.destroying = self.dig_on_attack; + } + false + } + fn start_use(&mut self) { + if self.destroying { + return; + } + self.delay = 4; + let entity = self.entity; + use_on_target(self, entity); + } + fn pick(&mut self) { + self.events.push("pick".into()); + } + fn use_delay(&self) -> u32 { + self.delay + } + fn continue_attack(&mut self, down: bool) { + if self.using { + return; + } + let down = down && !self.entity; + self.events.push(format!("continue {down}")); + if !down { + self.destroying = false; + } + } +} + +impl TargetUseWorld for World { + fn use_entity(&mut self) -> bool { + self.events + .push(if self.passing { "entity" } else { "use" }.into()); + if self.passing { + false + } else { + self.using = self.begin_using; + true + } + } + fn use_block(&mut self) -> bool { + self.events + .push(if self.passing { "block" } else { "use" }.into()); + if self.passing { + false + } else { + self.using = self.begin_using; + true + } + } + fn use_item(&mut self) { + self.events.push("item".into()); + self.using = self.begin_using; + } +} + +fn main() -> Result<(), Box> { + let args: Vec<_> = std::env::args().collect(); + let input = std::io::BufReader::new(std::fs::File::open(&args[1])?); + let mut out = std::io::BufWriter::new(std::fs::File::create(&args[2])?); + for (i, line) in input.lines().enumerate() { + let row: Value = serde_json::from_str(&line?)?; + let flag = |key: &str| row[key].as_bool().unwrap(); + let mut world = World { + using: flag("using"), + destroying: flag("destroying"), + entity: flag("entity"), + dig_on_attack: flag("dig_on_attack"), + begin_using: flag("begin_using"), + delay: row["delay"].as_u64().unwrap() as u32, + events: vec![], + passing: row["passing"].as_bool().unwrap_or(false), + }; + let mut input = InteractionInput::default(); + for button in row["clicks"].as_array().unwrap() { + input.press(match button.as_u64().unwrap() { + 0 => Button::Attack, + 1 => Button::Use, + _ => Button::Pick, + }); + } + input.tick(&mut world, flag("attack_down"), flag("use_down")); + writeln!(out, "== {i}")?; + for event in world.events { + writeln!(out, "{event}")?; + } + writeln!( + out, + "state {} {} {}", + world.using, world.destroying, world.delay + )?; + } + Ok(()) +} diff --git a/crates/horizon-client/examples/player_sync_fixture.rs b/crates/horizon-client/examples/player_sync_fixture.rs new file mode 100644 index 000000000..b50a672af --- /dev/null +++ b/crates/horizon-client/examples/player_sync_fixture.rs @@ -0,0 +1,72 @@ +use horizon_client::player_sync::{PositionSender, teleport_velocity}; +use horizon_core::geometry::Vec3; +use horizon_protocol::packets::play::{PlayerPosition, Serverbound as S}; +use serde_json::Value; +use std::io::{BufRead, Write}; + +fn number(value: &Value) -> f64 { + f64::from_bits(u64::from_str_radix(value.as_str().unwrap(), 16).unwrap()) +} + +fn main() -> Result<(), Box> { + let args: Vec<_> = std::env::args().collect(); + let input = std::io::BufReader::new(std::fs::File::open(&args[1])?); + let mut out = std::io::BufWriter::new(std::fs::File::create(&args[2])?); + for line in input.lines() { + let row: Value = serde_json::from_str(&line?)?; + if row["kind"] == "movement" { + let mut sender = PositionSender::default(); + for op in row["ops"].as_array().unwrap() { + let pos = std::array::from_fn(|i| number(&op[i])); + let rot = (number(&op[3]) as f32, number(&op[4]) as f32); + if let Some(packet) = + sender.update(pos, rot, op[5].as_bool().unwrap(), op[6].as_bool().unwrap()) + { + let name = match packet { + S::MovePlayerPos(_) => "pos", + S::MovePlayerPosRot(_) => "posrot", + S::MovePlayerRot(_) => "rot", + S::MovePlayerStatus(_) => "status", + _ => unreachable!(), + }; + write!(out, "{name} ")?; + for byte in &packet.encode()[1..] { + write!(out, "{byte:02x}")?; + } + writeln!(out)?; + } else { + writeln!(out, "-")?; + } + } + } else { + let vector = |key: &str| { + Vec3::new( + number(&row[key][0]), + number(&row[key][1]), + number(&row[key][2]), + ) + }; + let rotation = |key: &str| (number(&row[key][0]) as f32, number(&row[key][1]) as f32); + let old = rotation("old"); + let new = rotation("new"); + let delta = vector("delta"); + let packet = PlayerPosition { + teleport: 0, + position: [0.0; 3], + delta: [delta.x, delta.y, delta.z], + yaw: new.0, + pitch: new.1, + relatives: row["flags"].as_i64().unwrap() as i32, + }; + let v = teleport_velocity(vector("velocity"), old, new, &packet); + writeln!( + out, + "{:016x} {:016x} {:016x}", + v.x.to_bits(), + v.y.to_bits(), + v.z.to_bits() + )?; + } + } + Ok(()) +} diff --git a/crates/horizon-client/src/game/hand.rs b/crates/horizon-client/src/game/hand.rs index 13309af87..6e11febbd 100644 --- a/crates/horizon-client/src/game/hand.rs +++ b/crates/horizon-client/src/game/hand.rs @@ -39,7 +39,7 @@ impl Game<'_> { }); self.clicks.destroying = destroying; self.attack_clicks = 0; - self.stop_remote_mining(); + self.request_remote_mining_stop(); self.mining = None; } /// `LivingEntity.swing` with `getModifiedSwingDuration`. diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 545133427..e4d1af1b1 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -94,11 +94,7 @@ pub(super) struct Remote { biome_bits: u8, view_distance: i32, sequence: i32, - last_pos: [f64; 3], - last_rot: (f32, f32), - last_on_ground: bool, - last_collision: bool, - reminder: u32, + position_sender: horizon_client::player_sync::PositionSender, last_input: u8, was_sprinting: bool, carried: Option, @@ -144,6 +140,8 @@ pub(super) struct Remote { profile: u128, destroying: Option<([i32; 3], u8, f32)>, destroy_delay: u32, + input: horizon_client::interaction_input::InteractionInput, + stop_mining: bool, placing: Option<([i32; 3], u64)>, death_message: Option, player_type: Option>, @@ -273,6 +271,7 @@ impl Game<'_> { self.container = None; self.carried = None; self.using = None; + self.clicks.miss_time = 0; self.set_mode_id(0); self.experience = Experience { level: 0, @@ -301,11 +300,7 @@ impl Game<'_> { biome_bits: 7, view_distance: self.settings.render_distance, sequence: 0, - last_pos: [0.0; 3], - last_rot: (0.0, 0.0), - last_on_ground: false, - last_collision: false, - reminder: 0, + position_sender: Default::default(), last_input: 0, was_sprinting: false, carried: None, @@ -334,6 +329,8 @@ impl Game<'_> { profile: 0, destroying: None, destroy_delay: 0, + input: Default::default(), + stop_mining: false, placing: None, death_message: None, player_type, @@ -657,6 +654,8 @@ impl Game<'_> { r.dimension = dimension; r.game_mode = spawn.game_type; r.loaded_sent = false; + // A replacement LocalPlayer has a fresh sendPosition baseline. + r.position_sender = Default::default(); r.predictions.clear(); self.set_mode_id(spawn.game_type); self.dim = Dim::from_id(&spawn.dimension); @@ -1211,6 +1210,9 @@ impl Game<'_> { fn handle_packet(&mut self, packet: C) -> Result<()> { match packet { + // ClientCommonPacketListenerImpl.handlePing runs on the game + // thread, after all preceding packets (especially teleports). + C::Ping(p) => self.send(S::Pong(p)), // PingDebugMonitor.onPongReceived. C::PongResponse(p) => { let delay = self.debug_millis() - p.time; @@ -2352,10 +2354,11 @@ impl Game<'_> { p.pitch }) .clamp(-90.0, 90.0); - let delta = Vec3::new( - pick(rel::DELTA_X, velocity.x, p.delta[0]), - pick(rel::DELTA_Y, velocity.y, p.delta[1]), - pick(rel::DELTA_Z, velocity.z, p.delta[2]), + let delta = horizon_client::player_sync::teleport_velocity( + velocity, + (self.yaw, self.pitch), + (yaw, pitch), + p, ); match self.player.as_mut() { Some(player) => { @@ -2378,8 +2381,8 @@ impl Game<'_> { self.yaw = yaw; self.pitch = pitch; if let Some(r) = self.remote.as_mut() { - r.last_pos = [position.x, position.y, position.z]; - r.last_rot = (yaw, pitch); + // handleMovePlayer does not change LocalPlayer.sendPosition's + // baseline: the next tick must still report the corrected state. r.destroying = None; } self.mining = None; @@ -2396,6 +2399,16 @@ impl Game<'_> { /// One client tick while connected (Minecraft.tick with a ClientLevel). pub(super) fn tick_remote(&mut self) -> Result<()> { + self.tick_remote_level()?; + // Minecraft.tick sends this even when the player cannot tick yet, + // including while waiting for chunks. Keep it outside early returns. + if self.remote.as_ref().is_some_and(|r| r.joined) { + self.send(S::ClientTickEnd(Empty)); + } + Ok(()) + } + + fn tick_remote_level(&mut self) -> Result<()> { self.ticks += 1; // Options.broadcastOptions: a changed render distance is sent again. let view = client_view_distance(self.settings.render_distance); @@ -2425,6 +2438,10 @@ impl Game<'_> { if self.use_delay > 0 { self.use_delay -= 1; } + // Minecraft.handleKeybinds precedes entity ticking and sendChanges. + // Window callbacks only queue clicks, so an interaction cannot leak + // between CLIENT_TICK_END and a later pong. + self.remote_handle_input(); // ClientLevel.tick: the border's size change. self.border.tick(); self.tick_remote_entities(running); @@ -2447,13 +2464,11 @@ impl Game<'_> { (player.position.z.floor() as i32) >> 4, ); if !self.streaming.is_lit(cx, cz) { - // Moved where nothing is loaded by horizon-ctl (`tp`, `fly`): - // the server hears of it, and sends the chunks there. A player - // who walked there has already sent that move. - let pos = [player.position.x, player.position.y, player.position.z]; + // LocalPlayer.tick can wait for chunks, but Minecraft.tick still + // calls sendChanges: rotations and the 20-tick position reminder + // must continue, as well as moves into the unloaded area. if let Some(r) = self.remote.as_ref() && r.loaded_sent - && r.last_pos != pos { let input = r.last_input; self.remote_send_state(input); @@ -2552,16 +2567,9 @@ impl Game<'_> { // The player's own model (third person, the inventory's preview). self.tick_player_model(); if !dead { - self.remote_mining(); - // Minecraft.handleKeybinds: no new use while an item is in use. - if self.right && self.use_delay == 0 && playing && self.using.is_none() { - self.remote_use(); - self.use_delay = 4; - } self.remote_tick_using(); } self.remote_send_state(input); - self.send(S::ClientTickEnd(Empty)); Ok(()) } /// The local player's share of every touching entity's @@ -2791,41 +2799,12 @@ impl Game<'_> { data: 0, })); } - r.reminder += 1; - let d = [ - pos[0] - r.last_pos[0], - pos[1] - r.last_pos[1], - pos[2] - r.last_pos[2], - ]; - let moved = d[0] * d[0] + d[1] * d[1] + d[2] * d[2] > 2.0e-4 * 2.0e-4 || r.reminder >= 20; - let rotated = yaw != r.last_rot.0 || pitch != r.last_rot.1; - let flags = u8::from(on_ground) | u8::from(collision) << 1; - let [x, y, z] = pos; - if moved && rotated { - out.push(S::MovePlayerPosRot(play::MovePlayerPosRot { - x, - y, - z, - yaw, - pitch, - flags, - })); - } else if moved { - out.push(S::MovePlayerPos(play::MovePlayerPos { x, y, z, flags })); - } else if rotated { - out.push(S::MovePlayerRot(play::MovePlayerRot { yaw, pitch, flags })); - } else if r.last_on_ground != on_ground || r.last_collision != collision { - out.push(S::MovePlayerStatus(play::MovePlayerStatus { flags })); + if let Some(packet) = r + .position_sender + .update(pos, (yaw, pitch), on_ground, collision) + { + out.push(packet); } - if moved { - r.last_pos = pos; - r.reminder = 0; - } - if rotated { - r.last_rot = (yaw, pitch); - } - r.last_on_ground = on_ground; - r.last_collision = collision; // Creative inventory edits are sent slot by slot (SetCreativeModeSlot). let mut creative_slots = Vec::new(); if self.creative { @@ -2868,16 +2847,12 @@ impl Game<'_> { } /// MultiPlayerGameMode.startDestroyBlock / continueDestroyBlock. - fn remote_mining(&mut self) { + fn remote_mining(&mut self, start: bool) { let Some(r) = self.remote.as_mut() else { return; }; - if r.destroy_delay > 0 { + if !start && r.destroy_delay > 0 { r.destroy_delay -= 1; - } - if !self.left || self.screen != Screen::Playing { - self.stop_remote_mining(); - self.mining = None; return; } let Some(hit) = self.pick() else { @@ -2889,11 +2864,6 @@ impl Game<'_> { if self.res.facts().is_air(state) { return; } - if self.remote.as_ref().is_some_and(|r| r.destroy_delay > 0) { - return; - } - self.swing_arm(); - self.send(S::Punch(Empty)); if self.creative { let seq = self.next_sequence(); self.predict_break(hit.pos, state, seq); @@ -2931,6 +2901,9 @@ impl Game<'_> { .as_ref() .and_then(|r| r.destroying) .is_some_and(|(p, _, _)| p == hit.pos); + if start && same { + return; + } if !same { if let Some((old, _, _)) = self.remote.as_mut().and_then(|r| r.destroying.take()) { self.send(S::PlayerAction(play::PlayerAction { @@ -3045,15 +3018,25 @@ impl Game<'_> { } self.mining = None; } - /// Minecraft.startUseItem: use on the targeted block, then the item itself. + /// Minecraft.startUseItem: use one crosshair target, then the item itself. fn remote_use(&mut self) { - let held_block = self - .held() - .and_then(|h| self.res.items().get(&h.item)) - .is_some_and(|i| i.block.is_some()); + // Minecraft.startUseItem returns before resetting rightClickDelay + // while a survival block break is active. + if self.remote.as_ref().is_some_and(|r| r.destroying.is_some()) { + return; + } + self.use_delay = 4; + // Resolve the crosshair once. A passing entity interaction keeps + // that target and may only fall back to using the item itself. + let entity = self.entity_targeted(); + horizon_client::interaction_input::use_on_target(&mut RemoteUseWorld(self), entity); + } + + fn remote_use_entity(&mut self) -> bool { let sneaking = self.keys.contains(&KeyCode::ShiftLeft); - // Entities first (interact), like the hit result order. - if let Some((i, _)) = self.mob_hit() { + if let Some((i, distance)) = self.mob_hit() + && self.before_block(distance) + { let id = self.mobs[i].id as i32; self.send(S::Interact(play::Interact { id, @@ -3069,9 +3052,17 @@ impl Game<'_> { .is_some_and(|r| r.level_players.contains_key(&id)); if !player { self.swing_arm(); - return; + return true; } } + false + } + + fn remote_use_block(&mut self) -> bool { + let held_block = self + .held() + .and_then(|h| self.res.items().get(&h.item)) + .is_some_and(|i| i.block.is_some()); if let Some(hit) = self.pick() { let seq = self.next_sequence(); let cursor = [ @@ -3100,9 +3091,13 @@ impl Game<'_> { } if held_block { self.swing_arm(); - return; + return true; } } + false + } + + fn remote_use_item(&mut self) { if let Some(held) = self.held().cloned() { let seq = self.next_sequence(); self.send(S::UseItem(play::UseItem { @@ -3134,7 +3129,10 @@ impl Game<'_> { self.using = None; return; }; - if !self.right || slot != self.selected || self.screen != Screen::Playing { + // Button release belongs to the next handleKeybinds phase, which + // sends RELEASE_USE_ITEM. Do not silently clear a use begun by a + // quick press/release between ticks. + if slot != self.selected { self.using = None; return; } @@ -3153,52 +3151,139 @@ impl Game<'_> { self.using = Some((slot, ticks + 1)); } } - /// Left click: attack the targeted entity (MultiPlayerGameMode.attack). + /// MouseHandler updates KeyMapping; handleKeybinds sends packets on a tick. pub(super) fn remote_mouse(&mut self, button: MouseButton, pressed: bool) -> Result<()> { - if self.screen == Screen::Chat { - return Ok(()); - } - match button { + use horizon_client::interaction_input::Button; + let input = match button { MouseButton::Left => { - if pressed && let Some((i, _)) = self.mob_hit() { - let id = self.mobs[i].id as i32; - self.send(S::Attack(play::Attack { id })); - self.send(S::Punch(Empty)); - self.swing_arm(); - return Ok(()); - } - if pressed && self.pick().is_none() { - self.send(S::Punch(Empty)); - self.swing_arm(); - } - self.left = pressed; + self.toggles + .button(2, pressed, false, self.settings.toggle_attack); + self.left = self.toggles.is_down(2); + Some(Button::Attack) } MouseButton::Right => { - self.right = pressed; - if pressed { - self.remote_use(); - self.use_delay = 4; - } else { - self.send(S::PlayerAction(play::PlayerAction { - action: 6, - pos: [0; 3], - direction: 0, - sequence: 0, - })); - } + self.toggles + .button(3, pressed, false, self.settings.toggle_use); + self.right = self.toggles.is_down(3); + Some(Button::Use) } - MouseButton::Middle if pressed => { - if let Some(hit) = self.pick() { - self.send(S::PickItemFromBlock(play::PickItemFromBlock { - pos: hit.pos, - include_data: self.has_control_down(), - })); - } - } - _ => {} + MouseButton::Middle => Some(Button::Pick), + _ => None, + }; + if pressed + && let Some(button) = input + && let Some(remote) = self.remote.as_mut() + { + remote.input.press(button); } Ok(()) } + + pub(super) fn request_remote_mining_stop(&mut self) { + if let Some(r) = self.remote.as_mut() { + r.stop_mining = true; + } + } + + fn remote_handle_input(&mut self) { + let Some(r) = self.remote.as_mut() else { + return; + }; + let mut input = std::mem::take(&mut r.input); + let stop = std::mem::take(&mut r.stop_mining); + let ready = r.loaded_sent && self.screen == Screen::Playing && self.sleeping.is_none(); + if stop || !ready { + self.stop_remote_mining(); + } + if !ready { + return; + } + // MultiPlayerGameMode.tick syncs the carried slot before keybinds. + let carried_changed = self.remote.as_mut().is_some_and(|r| { + if r.carried == Some(self.selected) { + false + } else { + r.carried = Some(self.selected); + true + } + }); + if carried_changed { + self.send(S::SetCarriedItem(play::SetCarriedItem { + slot: self.selected as i16, + })); + } + let (attack, use_item) = (self.left, self.right); + input.tick(&mut RemoteInputWorld(self), attack, use_item); + if self.clicks.miss_time > 0 { + self.clicks.miss_time -= 1; + } + if let Some(r) = self.remote.as_mut() { + r.input = input; + } + } + + fn remote_start_attack(&mut self) -> bool { + if self.clicks.miss_time > 0 { + return false; + } + if self.spectator() { + return true; + } + let mut instant = false; + if let Some((i, distance)) = self.mob_hit() + && self.before_block(distance) + { + self.send(S::Attack(play::Attack { + id: self.mobs[i].id as i32, + })); + self.reset_attack_strength(); + } else if !self.entity_targeted() + && let Some(hit) = self.pick() + { + self.remote_mining(true); + instant = self + .res + .facts() + .is_air(self.world.block(hit.pos[0], hit.pos[1], hit.pos[2])); + } else { + if !self.creative { + self.clicks.miss_time = 10; + } + self.reset_attack_strength(); + } + self.swing_arm(); + self.send(S::Punch(Empty)); + instant + } + + fn remote_continue_attack(&mut self, down: bool) { + if !down { + self.clicks.miss_time = 0; + } + if self.clicks.miss_time > 0 || self.using.is_some() { + return; + } + let entity = self.entity_targeted(); + if down + && !entity + && let Some(hit) = self.pick() + { + let state = self.world.block(hit.pos[0], hit.pos[1], hit.pos[2]); + if !self.res.facts().is_air(state) { + self.remote_mining(false); + self.swing_arm(); + self.send(S::Punch(Empty)); + } else { + if let Some(r) = self.remote.as_mut() { + r.destroying = None; + } + self.mining = None; + } + } else { + self.stop_remote_mining(); + self.mining = None; + } + } /// Keys with a multiplayer meaning; returns true when consumed. pub(super) fn remote_key(&mut self, key: KeyCode) -> Result { if self.screen != Screen::Playing { @@ -3886,6 +3971,57 @@ fn client_view_distance(render_distance: i32) -> i8 { render_distance.clamp(2, i32::from(i8::MAX)) as i8 } +struct RemoteInputWorld<'g, 'a>(&'g mut Game<'a>); + +struct RemoteUseWorld<'g, 'a>(&'g mut Game<'a>); + +impl horizon_client::interaction_input::TargetUseWorld for RemoteUseWorld<'_, '_> { + fn use_entity(&mut self) -> bool { + self.0.remote_use_entity() + } + fn use_block(&mut self) -> bool { + self.0.remote_use_block() + } + fn use_item(&mut self) { + self.0.remote_use_item(); + } +} + +impl horizon_client::interaction_input::InteractionWorld for RemoteInputWorld<'_, '_> { + fn using_item(&self) -> bool { + self.0.using.is_some() + } + fn release_using(&mut self) { + self.0.send(S::PlayerAction(play::PlayerAction { + action: 6, + pos: [0; 3], + direction: 0, + sequence: 0, + })); + self.0.using = None; + } + fn start_attack(&mut self) -> bool { + self.0.remote_start_attack() + } + fn start_use(&mut self) { + self.0.remote_use(); + } + fn pick(&mut self) { + if let Some(hit) = self.0.pick() { + self.0.send(S::PickItemFromBlock(play::PickItemFromBlock { + pos: hit.pos, + include_data: self.0.has_control_down(), + })); + } + } + fn use_delay(&self) -> u32 { + self.0.use_delay + } + fn continue_attack(&mut self, down: bool) { + self.0.remote_continue_attack(down); + } +} + #[cfg(test)] mod tests { use super::Game; diff --git a/crates/horizon-client/src/interaction_input.rs b/crates/horizon-client/src/interaction_input.rs new file mode 100644 index 000000000..d17cb938c --- /dev/null +++ b/crates/horizon-client/src/interaction_input.rs @@ -0,0 +1,186 @@ +//! The mouse-key portion of Minecraft.handleKeybinds. Events only count clicks; +//! the client tick consumes them before player movement and CLIENT_TICK_END. +#[derive(Default)] +pub struct InteractionInput { + clicks: [u32; 3], +} + +#[derive(Clone, Copy)] +pub enum Button { + Attack, + Use, + Pick, +} + +pub trait InteractionWorld { + fn using_item(&self) -> bool; + fn release_using(&mut self); + fn start_attack(&mut self) -> bool; + fn start_use(&mut self); + fn pick(&mut self); + fn use_delay(&self) -> u32; + fn continue_attack(&mut self, down: bool); +} + +/// Minecraft.startUseItem dispatches one hit result per hand. A passing +/// entity interaction falls back to the item, without trying a block behind it. +pub trait TargetUseWorld { + fn use_entity(&mut self) -> bool; + fn use_block(&mut self) -> bool; + fn use_item(&mut self); +} + +pub fn use_on_target(world: &mut impl TargetUseWorld, entity: bool) { + let consumed = if entity { + world.use_entity() + } else { + world.use_block() + }; + if !consumed { + world.use_item(); + } +} + +impl InteractionInput { + pub fn press(&mut self, button: Button) { + self.clicks[button as usize] += 1; + } + + pub fn tick(&mut self, world: &mut impl InteractionWorld, attack_down: bool, use_down: bool) { + let [attack, use_item, pick] = std::mem::take(&mut self.clicks); + let mut instant = false; + if world.using_item() { + if !use_down { + world.release_using(); + } + // Vanilla drains all three click queues while using an item, + // including the tick that releases it. + } else { + for _ in 0..attack { + instant |= world.start_attack(); + } + for _ in 0..use_item { + world.start_use(); + } + for _ in 0..pick { + world.pick(); + } + } + if use_down && world.use_delay() == 0 && !world.using_item() { + world.start_use(); + } + world.continue_attack(!instant && attack_down); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[derive(Default)] + struct World { + using: bool, + destroying: bool, + delay: u32, + begin_using: bool, + events: Vec<&'static str>, + } + impl InteractionWorld for World { + fn using_item(&self) -> bool { + self.using + } + fn release_using(&mut self) { + self.events.push("release"); + self.using = false; + } + fn start_attack(&mut self) -> bool { + self.events.push("attack"); + false + } + fn start_use(&mut self) { + if !self.destroying { + self.events.push("use"); + self.delay = 4; + self.using = self.begin_using; + } + } + fn pick(&mut self) { + self.events.push("pick"); + } + fn use_delay(&self) -> u32 { + self.delay + } + fn continue_attack(&mut self, _: bool) { + self.events.push("continue"); + } + } + + #[test] + fn quick_clicks_wait_for_a_tick_and_follow_keybind_order() { + let mut input = InteractionInput::default(); + let mut world = World::default(); + input.press(Button::Use); + input.press(Button::Pick); + input.press(Button::Attack); + assert!(world.events.is_empty()); + // Both edges can arrive between ticks; the press still acts once. + input.tick(&mut world, false, false); + assert_eq!(world.events, ["attack", "use", "pick", "continue"]); + world.events.clear(); + input.tick(&mut world, false, false); + assert_eq!( + world.events, + ["continue"], + "no spurious release or repeated click" + ); + } + + #[test] + fn releasing_actual_item_use_discards_clicks_for_that_tick() { + let mut input = InteractionInput::default(); + let mut world = World { + using: true, + ..Default::default() + }; + for button in [Button::Attack, Button::Use, Button::Pick] { + input.press(button); + } + input.tick(&mut world, true, false); + assert_eq!(world.events, ["release", "continue"]); + } + + #[test] + fn holding_use_respects_the_delay_and_active_digging() { + let mut input = InteractionInput::default(); + let mut world = World { + destroying: true, + ..Default::default() + }; + input.press(Button::Use); + input.tick(&mut world, true, true); + assert_eq!(world.events, ["continue"]); + world.destroying = false; + world.events.clear(); + input.tick(&mut world, false, true); + assert_eq!(world.events, ["use", "continue"]); + world.events.clear(); + input.tick(&mut world, false, true); + assert_eq!(world.events, ["continue"]); + } + + #[test] + fn a_quick_use_click_releases_on_the_following_tick() { + let mut input = InteractionInput::default(); + let mut world = World { + begin_using: true, + ..Default::default() + }; + input.press(Button::Use); + input.tick(&mut world, false, false); + assert_eq!(world.events, ["use", "continue"]); + assert!(world.using); + world.events.clear(); + input.tick(&mut world, false, false); + assert_eq!(world.events, ["release", "continue"]); + } +} diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 1e2c8c331..ef4ee42bc 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -85,6 +85,7 @@ pub mod hud_layout; pub mod hud_overlays; pub mod hunger; pub mod input; +pub mod interaction_input; pub mod inv_sort; pub mod inventory_effects; pub mod item_data; @@ -133,6 +134,7 @@ pub mod particles; pub mod patrol; pub mod perf_overlay; pub mod pick_item; +pub mod player_sync; pub mod poi_sections; pub mod portal_sections; pub mod pose; diff --git a/crates/horizon-client/src/net.rs b/crates/horizon-client/src/net.rs index f9ad5992a..74f1b0b6d 100644 --- a/crates/horizon-client/src/net.rs +++ b/crates/horizon-client/src/net.rs @@ -1,7 +1,8 @@ //! Multiplayer session: a network thread that connects to a server, logs in, //! runs the configuration phase and then forwards decoded play packets to the -//! game thread. Keep-alives and pings are answered on the network thread like -//! vanilla's connection handlers so a busy frame never times the client out. +//! game thread. Keep-alives are answered on the network thread; pings wait for +//! the game thread, like ClientCommonPacketListenerImpl.handlePing, so a pong +//! cannot overtake an earlier world update or teleport acknowledgement. //! Online-mode servers need the Microsoft account the launcher signed in //! (crate::auth): the login joins the server through the session service and //! encrypts the connection (ClientHandshakePacketListenerImpl.handleHello). @@ -382,8 +383,17 @@ fn run( uuid: profile.uuid, name: profile.name, }); + receive_play(&mut reader, shared_writer, lang, send) +} + +fn receive_play( + reader: &mut ConnReader, + shared_writer: &Mutex>, + lang: &Language, + send: &Sender, +) -> Result { loop { - let (id, payload) = reader.recv().map_err(e)?; + let (id, payload) = reader.recv().map_err(|e| e.to_string())?; let packet = match play::Clientbound::decode(id, &payload) { Ok(p) => p, Err(err) => { @@ -404,7 +414,6 @@ fn run( }; match packet { play::Clientbound::KeepAlive(k) => reply(play::Serverbound::KeepAlive(k)), - play::Clientbound::Ping(p) => reply(play::Serverbound::Pong(p)), play::Clientbound::BundleDelimiter(_) => {} play::Clientbound::ResourcePackPush(p) => { // Declined: Horizon has no server resource packs. @@ -415,7 +424,7 @@ fn run( } play::Clientbound::StartConfiguration(_) => { reply(play::Serverbound::ConfigurationAcknowledged(Empty)); - let registries = configure(&mut reader, shared_writer, lang, send)?; + let registries = configure(reader, shared_writer, lang, send)?; let _ = send.send(Event::Reconfigured(Arc::new(registries))); } play::Clientbound::Disconnect(d) => return Ok(plain(&d.reason, lang)), @@ -585,6 +594,50 @@ pub fn ping(address: &str, timeout: Duration) -> Result<(serde_json::Value, u128 mod tests { use super::*; + #[test] + fn pings_wait_behind_teleports_but_keepalives_do_not_wait_for_the_game() { + let (client, server) = horizon_protocol::connection::Pipe::pair(); + let (mut reader, writer) = Connection::local(client).split(); + let writer = Arc::new(Mutex::new(Some(writer))); + let (send, events) = channel(); + let thread_writer = writer.clone(); + let thread = std::thread::spawn(move || { + receive_play(&mut reader, &thread_writer, &Language::new(), &send) + }); + let mut server = Connection::local(server); + let teleport = play::Clientbound::PlayerPosition(play::PlayerPosition { + teleport: 42, + position: [10.0, 64.0, 10.0], + delta: [0.0; 3], + yaw: 90.0, + pitch: 0.0, + relatives: 0, + }); + server.send(&teleport.encode()).unwrap(); + server + .send(&play::Clientbound::Ping(cfg::Ping { id: 7 }).encode()) + .unwrap(); + // A reply to this proves the receive loop has passed the ping. The + // old implementation instead replies with Pong before this keepalive. + server + .send(&play::Clientbound::KeepAlive(cfg::KeepAlive { id: 9 }).encode()) + .unwrap(); + let (id, body) = server.recv().unwrap(); + assert_eq!( + play::Serverbound::decode(id, &body).unwrap(), + play::Serverbound::KeepAlive(cfg::KeepAlive { id: 9 }) + ); + let timeout = Duration::from_secs(5); + assert!( + matches!(events.recv_timeout(timeout).unwrap(), Event::Packet(p) if *p == teleport) + ); + assert!( + matches!(events.recv_timeout(timeout).unwrap(), Event::Packet(p) if *p == play::Clientbound::Ping(cfg::Ping { id: 7 })) + ); + writer.lock().unwrap().take().unwrap().shutdown(); + assert!(thread.join().unwrap().is_err()); + } + #[test] fn server_brand_payload_is_decoded_and_invalid_or_unrelated_payloads_are_ignored() { let (send, events) = channel(); diff --git a/crates/horizon-client/src/player_sync.rs b/crates/horizon-client/src/player_sync.rs new file mode 100644 index 000000000..96b09c708 --- /dev/null +++ b/crates/horizon-client/src/player_sync.rs @@ -0,0 +1,86 @@ +//! LocalPlayer.sendPosition and PositionMoveRotation.calculateAbsolute. +use horizon_core::geometry::Vec3; +use horizon_protocol::packets::play::{self, PlayerPosition, Serverbound as S, relative}; + +/// Only movement updates advance this baseline. A teleport acknowledgement is +/// separate and must not suppress the next tick's corrected position or look. +#[derive(Default)] +pub struct PositionSender { + position: [f64; 3], + rotation: (f32, f32), + on_ground: bool, + collision: bool, + reminder: u32, +} + +impl PositionSender { + pub fn update( + &mut self, + position: [f64; 3], + rotation: (f32, f32), + on_ground: bool, + collision: bool, + ) -> Option { + self.reminder += 1; + let d: [f64; 3] = std::array::from_fn(|i| position[i] - self.position[i]); + let moved = + d[0] * d[0] + d[1] * d[1] + d[2] * d[2] > 2.0e-4 * 2.0e-4 || self.reminder >= 20; + let rotated = rotation.0 - self.rotation.0 != 0.0 || rotation.1 - self.rotation.1 != 0.0; + let flags = u8::from(on_ground) | u8::from(collision) << 1; + let [x, y, z] = position; + let (yaw, pitch) = rotation; + let packet = if moved && rotated { + Some(S::MovePlayerPosRot(play::MovePlayerPosRot { + x, + y, + z, + yaw, + pitch, + flags, + })) + } else if moved { + Some(S::MovePlayerPos(play::MovePlayerPos { x, y, z, flags })) + } else if rotated { + Some(S::MovePlayerRot(play::MovePlayerRot { yaw, pitch, flags })) + } else if self.on_ground != on_ground || self.collision != collision { + Some(S::MovePlayerStatus(play::MovePlayerStatus { flags })) + } else { + None + }; + if moved { + self.position = position; + self.reminder = 0; + } + if rotated { + self.rotation = rotation; + } + self.on_ground = on_ground; + self.collision = collision; + packet + } +} + +/// Rotate the old velocity before adding the relative delta components. +/// Angles deliberately use Java's double toRadians followed by a float cast. +pub fn teleport_velocity( + mut velocity: Vec3, + old_rotation: (f32, f32), + new_rotation: (f32, f32), + packet: &PlayerPosition, +) -> Vec3 { + if packet.relatives & relative::ROTATE_DELTA != 0 { + let pitch = f64::from(old_rotation.1 - new_rotation.1).to_radians() as f32; + let yaw = f64::from(old_rotation.0 - new_rotation.0).to_radians() as f32; + velocity = velocity.rotate_x(pitch).rotate_y(yaw); + } + let old = [velocity.x, velocity.y, velocity.z]; + let flags = [relative::DELTA_X, relative::DELTA_Y, relative::DELTA_Z]; + let result: [f64; 3] = std::array::from_fn(|i| { + if packet.relatives & flags[i] != 0 { + old[i] + packet.delta[i] + } else { + packet.delta[i] + } + }); + Vec3::new(result[0], result[1], result[2]) +} diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaInteractionInputOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaInteractionInputOracle.java new file mode 100644 index 000000000..884e5042a --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaInteractionInputOracle.java @@ -0,0 +1,161 @@ +package dev.horizonlab.parity; + +import com.google.gson.*; +import java.io.PrintWriter; +import java.lang.reflect.*; +import java.nio.file.*; +import java.util.*; +import net.minecraft.client.*; +import net.minecraft.client.gui.Gui; +import net.minecraft.client.multiplayer.*; +import net.minecraft.client.player.LocalPlayer; +import net.minecraft.core.*; +import net.minecraft.network.protocol.Packet; +import net.minecraft.world.*; +import net.minecraft.world.entity.*; +import net.minecraft.world.entity.player.*; +import net.minecraft.world.flag.*; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.component.SwingAnimation; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.border.WorldBorder; +import net.minecraft.world.phys.*; + +/** Runs the shipped handleKeybinds; only game-mode effects and UI are shells. */ +public final class VanillaInteractionInputOracle { + static Object unsafe; + static Method allocate; + static List events = new ArrayList<>(); + static Field field(Class type, String name) throws Exception { + var f = type.getDeclaredField(name); f.setAccessible(true); return f; + } + static T make(Class type) throws Exception { return type.cast(allocate.invoke(unsafe, type)); } + public static class MC extends Minecraft { + public MC() { super(null); } + @Override public boolean hasControlDown() { return false; } + } + public static class UI extends Gui { + public UI() { super(null, null, null); } + @Override public void handleKeybinds() {} + } + public static class Mouse extends MouseHandler { + public Mouse() { super(null); } + @Override public boolean isMouseGrabbed() { return true; } + } + public static class Level extends ClientLevel { + public Level() { super(null, null, null, null, 0, 0, null, false, 0L, 0); } + @Override public BlockState getBlockState(BlockPos pos) { return Blocks.STONE.defaultBlockState(); } + @Override public FeatureFlagSet enabledFeatures() { return FeatureFlags.REGISTRY.allFlags(); } + } + public static class Connection extends ClientPacketListener { + public Connection() { super(null, null, null); } + @Override public void send(Packet packet) {} + } + public static class Human extends LocalPlayer { + boolean using; + ItemStack held = ItemStack.EMPTY; + public Human() { super(null, null, null, null, null, null, false, null, null); } + @Override public boolean isUsingItem() { return using; } + @Override public boolean isSpectator() { return false; } + @Override public boolean isCreative() { return false; } + @Override public boolean isHandsBusy() { return false; } + @Override public ItemStack getItemInHand(InteractionHand hand) { + return hand == InteractionHand.MAIN_HAND ? held : ItemStack.EMPTY; + } + @Override public void itemUsed(InteractionHand hand) {} + @Override public boolean cannotAttackWithItem(ItemStack item, int slot) { return false; } + @Override public boolean isWithinEntityInteractionRange(Entity e, double buffer) { return true; } + @Override public boolean swing(InteractionHand hand, SwingAnimation animation, boolean send) { return false; } + } + public static class Mode extends MultiPlayerGameMode { + boolean destroying, digOnAttack, beginUsing, passing; + Human player; + public Mode() { super(null, null); } + @Override public boolean isDestroying() { return destroying; } + @Override public boolean isSpectator() { return false; } + @Override public boolean startDestroyBlock(BlockPos pos, Direction face) { + events.add("attack"); destroying = digOnAttack; return true; + } + @Override public boolean continueDestroyBlock(BlockPos pos, Direction face) { + events.add("continue true"); return true; + } + @Override public void stopDestroyBlock() { events.add("continue false"); destroying = false; } + @Override public void attack(Player p, Entity e) { events.add("attack"); } + @Override public void releaseUsingItem(Player p) { events.add("release"); player.using = false; } + InteractionResult use() { events.add("use"); player.using = beginUsing; return InteractionResult.SUCCESS_SERVER; } + @Override public InteractionResult useItemOn(LocalPlayer p, InteractionHand h, BlockHitResult hit) { + if (passing) { events.add("block"); return InteractionResult.PASS; } + return use(); + } + @Override public InteractionResult interact(Player p, Entity e, EntityHitResult hit, InteractionHand h) { + if (passing) { events.add("entity"); return InteractionResult.PASS; } + return use(); + } + @Override public InteractionResult useItem(Player p, InteractionHand h) { + events.add("item"); player.using = beginUsing; return InteractionResult.SUCCESS_SERVER; + } + @Override public void handlePickItemFromBlock(BlockPos pos, boolean data) { events.add("pick"); } + @Override public void handlePickItemFromEntity(Entity entity, boolean data) { events.add("pick"); } + } + static boolean flag(JsonObject row, String name) { return row.get(name).getAsBoolean(); } + public static void main(String[] args) throws Exception { + ParityData.init(); + var u = Class.forName("sun.misc.Unsafe"); unsafe = field(u, "theUnsafe").get(null); + allocate = u.getMethod("allocateInstance", Class.class); + var handle = Minecraft.class.getDeclaredMethod("handleKeybinds"); handle.setAccessible(true); + try (var out = new PrintWriter(Files.newBufferedWriter(Path.of(args[1]))); + var lines = Files.lines(Path.of(args[0]))) { + int i = 0; + for (String line : (Iterable) lines::iterator) { + var row = JsonParser.parseString(line).getAsJsonObject(); + var mc = make(MC.class); var options = make(Options.class); + for (var f : Options.class.getDeclaredFields()) { + if (Modifier.isStatic(f.getModifiers())) continue; + f.setAccessible(true); + if (f.getType() == KeyMapping.class) f.set(options, make(KeyMapping.class)); + if (f.getType() == KeyMapping[].class) { + var keys = new KeyMapping[9]; + for (int n = 0; n < keys.length; n++) keys[n] = make(KeyMapping.class); + f.set(options, keys); + } + } + var player = make(Human.class); player.using = flag(row, "using"); + boolean passing = row.has("passing") && flag(row, "passing"); + player.held = passing ? new ItemStack(Blocks.STONE.asItem()) : ItemStack.EMPTY; + var level = make(Level.class); + field(ClientLevel.class, "worldBorder").set(level, new WorldBorder()); + field(Player.class, "abilities").set(player, new Abilities()); + field(LocalPlayer.class, "connection").set(player, make(Connection.class)); + var mode = make(Mode.class); mode.player = player; + mode.destroying = flag(row, "destroying"); mode.digOnAttack = flag(row, "dig_on_attack"); + mode.beginUsing = flag(row, "begin_using"); + mode.passing = passing; + field(Minecraft.class, "options").set(mc, options); + field(Minecraft.class, "gui").set(mc, make(UI.class)); + field(Minecraft.class, "mouseHandler").set(mc, make(Mouse.class)); + field(Minecraft.class, "player").set(mc, player); + field(Minecraft.class, "level").set(mc, level); + field(Minecraft.class, "gameMode").set(mc, mode); + var entity = make(net.minecraft.world.entity.item.ItemEntity.class); + field(Entity.class, "blockPosition").set(entity, BlockPos.ZERO); + field(Minecraft.class, "hitResult").set(mc, flag(row, "entity") + ? new EntityHitResult(entity, Vec3.ZERO) + : new BlockHitResult(Vec3.ZERO, Direction.UP, BlockPos.ZERO, false)); + String[] names = {"keyAttack", "keyUse", "keyPickItem"}; + for (var click : row.getAsJsonArray("clicks")) { + var key = (KeyMapping)field(Options.class, names[click.getAsInt()]).get(options); + var count = field(KeyMapping.class, "clickCount"); + count.setInt(key, count.getInt(key) + 1); + } + field(KeyMapping.class, "isDown").setBoolean(options.keyAttack, flag(row, "attack_down")); + field(KeyMapping.class, "isDown").setBoolean(options.keyUse, flag(row, "use_down")); + field(Minecraft.class, "rightClickDelay").setInt(mc, row.get("delay").getAsInt()); + events.clear(); handle.invoke(mc); + out.println("== " + i++); + for (String event : events) out.println(event); + out.println("state " + player.using + " " + mode.destroying + " " + field(Minecraft.class, "rightClickDelay").getInt(mc)); + } + } + } +} diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaPlayerSyncOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaPlayerSyncOracle.java new file mode 100644 index 000000000..056240bb8 --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaPlayerSyncOracle.java @@ -0,0 +1,109 @@ +package dev.horizonlab.parity; + +import com.google.gson.*; +import io.netty.buffer.Unpooled; +import java.io.PrintWriter; +import java.lang.reflect.*; +import java.nio.file.*; +import java.util.HexFormat; +import net.minecraft.client.*; +import net.minecraft.client.multiplayer.ClientPacketListener; +import net.minecraft.client.player.LocalPlayer; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.Packet; +import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket; +import net.minecraft.world.entity.*; +import net.minecraft.world.phys.Vec3; + +/** Runs shipped methods; only the world lifecycle and packet sink are fixtures. */ +public final class VanillaPlayerSyncOracle { + static Object unsafe; + static Method allocate; + static PrintWriter out; + static Field field(Class type, String name) throws Exception { + var f = type.getDeclaredField(name); f.setAccessible(true); return f; + } + static T make(Class type) throws Exception { + return type.cast(allocate.invoke(unsafe, type)); + } + public static final class Player extends LocalPlayer { + boolean ground; + public Player() { super(null, null, null, null, null, null, false, null, null); } + @Override protected boolean isControlledCamera() { return true; } + @Override public boolean isSprinting() { return false; } + @Override public boolean onGround() { return ground; } + } + public static final class Sink extends ClientPacketListener { + boolean sent; + public Sink() { super(null, null, null); } + @Override public void send(Packet packet) { + sent = true; + var buf = new FriendlyByteBuf(Unpooled.buffer()); + String name; + try { + if (packet instanceof ServerboundMovePlayerPacket.PosRot p) { + name = "posrot"; ServerboundMovePlayerPacket.PosRot.STREAM_CODEC.encode(buf, p); + } else if (packet instanceof ServerboundMovePlayerPacket.Pos p) { + name = "pos"; ServerboundMovePlayerPacket.Pos.STREAM_CODEC.encode(buf, p); + } else if (packet instanceof ServerboundMovePlayerPacket.Rot p) { + name = "rot"; ServerboundMovePlayerPacket.Rot.STREAM_CODEC.encode(buf, p); + } else if (packet instanceof ServerboundMovePlayerPacket.StatusOnly p) { + name = "status"; ServerboundMovePlayerPacket.StatusOnly.STREAM_CODEC.encode(buf, p); + } else { throw new AssertionError(packet); } + byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); + out.println(name + " " + HexFormat.of().formatHex(bytes)); + } finally { buf.release(); } + } + } + static double number(JsonElement value) { + return Double.longBitsToDouble(Long.parseUnsignedLong(value.getAsString(), 16)); + } + static Vec3 vector(JsonObject row, String key) { + var a = row.getAsJsonArray(key); + return new Vec3(number(a.get(0)), number(a.get(1)), number(a.get(2))); + } + public static void main(String[] args) throws Exception { + net.minecraft.SharedConstants.tryDetectVersion(); + net.minecraft.server.Bootstrap.bootStrap(); + var u = Class.forName("sun.misc.Unsafe"); + unsafe = field(u, "theUnsafe").get(null); + allocate = u.getMethod("allocateInstance", Class.class); + var sendPosition = LocalPlayer.class.getDeclaredMethod("sendPosition"); + sendPosition.setAccessible(true); + var minecraft = make(Minecraft.class); + var options = make(Options.class); + field(Options.class, "autoJump").set(options, OptionInstance.createBoolean("test", false)); + field(Minecraft.class, "options").set(minecraft, options); + try (var output = new PrintWriter(Files.newBufferedWriter(Path.of(args[1]))); + var lines = Files.lines(Path.of(args[0]))) { + out = output; + for (String line : (Iterable) lines::iterator) { + var row = JsonParser.parseString(line).getAsJsonObject(); + if (row.get("kind").getAsString().equals("movement")) { + var p = make(Player.class); + var sink = make(Sink.class); + field(LocalPlayer.class, "connection").set(p, sink); + field(LocalPlayer.class, "minecraft").set(p, minecraft); + for (var value : row.getAsJsonArray("ops")) { + var op = value.getAsJsonArray(); + field(Entity.class, "position").set(p, new Vec3(number(op.get(0)), number(op.get(1)), number(op.get(2)))); + field(Entity.class, "yRot").setFloat(p, (float)number(op.get(3))); + field(Entity.class, "xRot").setFloat(p, (float)number(op.get(4))); + p.ground = op.get(5).getAsBoolean(); + p.horizontalCollision = op.get(6).getAsBoolean(); + sink.sent = false; + sendPosition.invoke(p); + if (!sink.sent) out.println("-"); + } + } else { + var old = row.getAsJsonArray("old"); + var next = row.getAsJsonArray("new"); + var base = new PositionMoveRotation(Vec3.ZERO, vector(row, "velocity"), (float)number(old.get(0)), (float)number(old.get(1))); + var change = new PositionMoveRotation(Vec3.ZERO, vector(row, "delta"), (float)number(next.get(0)), (float)number(next.get(1))); + var v = PositionMoveRotation.calculateAbsolute(base, change, Relative.unpack(row.get("flags").getAsInt())).deltaMovement(); + out.printf("%016x %016x %016x%n", Double.doubleToRawLongBits(v.x), Double.doubleToRawLongBits(v.y), Double.doubleToRawLongBits(v.z)); + } + } + } + } +} diff --git a/tools/test_interaction_input_parity.py b/tools/test_interaction_input_parity.py new file mode 100644 index 000000000..1aa34a73f --- /dev/null +++ b/tools/test_interaction_input_parity.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""Compare queued interaction ordering with the real Minecraft.handleKeybinds.""" +import itertools +import json +import random + +try: + from . import lab, oracle_harness as harness +except ImportError: + import lab, oracle_harness as harness + + +def corpus(): + rng = random.Random(0x434C49434B) + for using, destroying, attack_down, use_down, entity in itertools.product([False, True], repeat=5): + for clicks in [[], [1], [0, 1], [1, 0], [2, 1, 0], [0, 0, 1, 1, 2]]: + for delay in [0, 3]: + yield dict(using=using, destroying=destroying, attack_down=attack_down, + use_down=use_down, entity=entity, clicks=clicks, delay=delay, + dig_on_attack=rng.choice([False, True]), + begin_using=rng.choice([False, True])) + # A player interaction returning PASS must go directly to the held item. + # The level still reports a solid block underneath the entity hit. + for entity in [False, True]: + for clicks in [[1], [1, 1], [0, 1], [1, 2]]: + yield dict(using=False, destroying=False, attack_down=False, use_down=False, + entity=entity, clicks=clicks, delay=0, dig_on_attack=False, + begin_using=False, passing=True) + + +def main(): + def inputs(build): + rows = list(corpus()) + path = build / "inputs.jsonl" + path.write_text("".join(json.dumps(row) + "\n" for row in rows)) + return path, len(rows) + + harness.differential( + "interaction-input-parity", "VanillaInteractionInputOracle", "interaction_input_fixture", inputs, + "Invokes Minecraft.handleKeybinds itself with real KeyMapping click counters. " + "Compares attack/use/pick ordering, release gating, drained clicks during use, " + "held-button repetition and use cooldowns for block/entity targets. " + "Includes passing entity interactions over solid blocks: only the held " + "item may be tried afterward. " + "Game-mode side effects and world simulation are controlled fixtures.", + sources=[lab.ROOT / "crates/horizon-client/src/interaction_input.rs", + lab.ROOT / "crates/horizon-client/src/game/remote.rs", + lab.ROOT / "crates/horizon-client/src/game/hand.rs", + lab.ROOT / "mods/horizon/src/test/java/dev/horizonlab/parity/ParityData.java"]) + + +if __name__ == "__main__": + main() diff --git a/tools/test_player_sync_parity.py b/tools/test_player_sync_parity.py new file mode 100644 index 000000000..4c78b439f --- /dev/null +++ b/tools/test_player_sync_parity.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python3 +"""Compare movement packet selection and teleport velocity with the pinned client.""" +import json +import random +import struct + +try: + from . import lab, oracle_harness as harness +except ImportError: + import lab, oracle_harness as harness + + +def corpus(): + # Stationary heartbeats, rotation-only updates, status changes and corrected + # positions followed by unchanged looks must retain vanilla's packet types. + yield dict(kind="movement", ops=[[0, 0, 0, 0, 0, False, False]] * 61) + yield dict(kind="movement", ops=[ + [0, 64, 0, 90, 0, True, False], + [0, 64, 0, 90, 0, True, False], + [0, 64, 0, 91, 0, True, False], + [100, 80, -20, 180, 20, False, False], + [100, 80, -20, 180, 20, True, False], + [100, 80, -20, 180, 20, True, True], + ]) + rng = random.Random(0x53594E43) + for _ in range(300): + state = [0, 0, 0, 0, 0, False, False] + ops = [] + for _ in range(80): + if rng.random() < 0.5: + axis = rng.randrange(3) + state[axis] += rng.choice([0, 0.0001, 0.0002, 0.0002000001, 0.1, 10]) + if rng.random() < 0.3: + state[3] += rng.choice([-360, -1, 0, 0.001, 1, 360]) + state[4] = rng.choice([-90, 0, 30, 90]) + if rng.random() < 0.2: + state[rng.randrange(5, 7)] = rng.choice([False, True]) + ops.append(state.copy()) + yield dict(kind="movement", ops=ops) + for flags in range(16): + for _ in range(40): + yield dict(kind="velocity", flags=flags << 5, + velocity=[rng.uniform(-4, 4) for _ in range(3)], + delta=[rng.uniform(-4, 4) for _ in range(3)], + old=[rng.uniform(-720, 720), rng.uniform(-90, 90)], + new=[rng.uniform(-720, 720), rng.uniform(-90, 90)]) + + +def main(): + def inputs(build): + rows = list(corpus()) + def bits(v): + return struct.pack(">d", v).hex() + for row in rows: + if row["kind"] == "movement": + row["ops"] = [[*map(bits, op[:5]), *op[5:]] for op in row["ops"]] + else: + for key in ["velocity", "delta", "old", "new"]: + row[key] = list(map(bits, row[key])) + path = build / "inputs.jsonl" + path.write_text("".join(json.dumps(row) + "\n" for row in rows)) + return path, len(rows) + + harness.differential( + "player-sync-parity", "VanillaPlayerSyncOracle", "player_sync_fixture", inputs, + "Actual LocalPlayer.sendPosition packet payloads over persistent movement " + "sequences; PositionMoveRotation.calculateAbsolute velocity with every " + "relative-delta/rotate-delta combination. World simulation, connection " + "scheduling and live anticheat behaviour are outside this harness.", + sources=[lab.ROOT / "crates/horizon-client/src/player_sync.rs", + lab.ROOT / "crates/horizon-client/src/game/remote.rs", + lab.ROOT / "crates/horizon-client/src/net.rs"]) + + +if __name__ == "__main__": + main() From 519ec1217cbf1c4c91bc427ccd2d61a6fdcc383e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 20:17:21 +0200 Subject: [PATCH 02/12] Server build: the game without sound or controllers, so Linux servers need neither ALSA nor udev horizon-client's cpal output and its controllers (gilrs, the DualSense's HID output) move behind the audio and controllers features, on by default, so the game is unchanged. Built with --no-default-features it links neither libasound.so.2 nor libudev.so.1. release.yml packages that build as horizon-engine in horizon-server-linux-x86_64.tar.gz (xtask package-server, checked by check_imports.py --server); install.sh installs it on Linux (and --archive installs a local one); horizon-server already prefers horizon-engine. The Dockerfile uses it too, and CI lints it. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 2 + .github/workflows/release.yml | 28 +- crates/horizon-client/Cargo.toml | 15 +- crates/horizon-client/src/audio.rs | 21 +- crates/horizon-client/src/game/pad.rs | 404 ++--------------- .../horizon-client/src/game/pad/hardware.rs | 409 ++++++++++++++++++ crates/horizon-client/src/gamepad.rs | 3 + crates/horizon-server/src/engine.rs | 25 +- docs/release-flow.md | 1 + docs/server.md | 50 ++- packaging/server/Dockerfile | 15 +- packaging/server/install.sh | 80 ++-- tools/check_imports.py | 13 +- xtask/src/main.rs | 58 ++- 14 files changed, 670 insertions(+), 454 deletions(-) create mode 100644 crates/horizon-client/src/game/pad/hardware.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5d5e285c5..da23874b8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -56,6 +56,8 @@ jobs: run: | cargo +stable fmt --all -- --check cargo +stable clippy --workspace --all-targets -- -D warnings + # The server build (released as horizon-engine): the game without sound or controllers. + cargo +stable clippy -p horizon-client --all-targets --no-default-features -- -D warnings - name: Test Rust core and voxel kernel correctness if: steps.rust.outputs.required != 'false' run: cargo +stable test --workspace diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a4780c85b..bd916514b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -76,6 +76,9 @@ jobs: os: ubuntu-22.04 dir: horizon-linux-x86_64 asset: horizon-linux-x86_64.tar.gz + # The server build too: packaging/server/install.sh installs it on Linux. + server_dir: horizon-server-linux-x86_64 + server_asset: horizon-server-linux-x86_64.tar.gz - name: windows os: windows-latest dir: horizon-windows-x64 @@ -190,6 +193,29 @@ jobs: path: dist/${{ matrix.asset }} if-no-files-found: error retention-days: 3 + - name: The server build (the game without sound or controllers, as horizon-engine) + if: matrix.server_asset + run: | + set -euo pipefail + # After the client is packaged: target/release/horizon-client is rebuilt without the audio and + # controllers features, so it needs neither libasound nor libudev (minimal server images lack them). + cargo build --release --locked -p horizon-client --no-default-features --bin horizon-client + cargo run --locked -p xtask -- package-server target/release/horizon-client "dist/${{ matrix.server_dir }}" + folder="dist/${{ matrix.server_dir }}" + python3 tools/check_imports.py --server "$folder/horizon-engine" "$folder/horizon-server" "$folder/horizon-prepare" + "$folder/horizon-engine" --help | grep -q -- '--resources' + "$folder/horizon-server" --version + cd dist + rm -f "${{ matrix.server_asset }}" + tar czf "${{ matrix.server_asset }}" "${{ matrix.server_dir }}" + ls -l "${{ matrix.server_asset }}" + - uses: actions/upload-artifact@v4 + if: matrix.server_asset + with: + name: ${{ matrix.server_asset }} + path: dist/${{ matrix.server_asset }} + if-no-files-found: error + retention-days: 3 publish: needs: [decide, build] @@ -220,7 +246,7 @@ jobs: run: | gh release create "$TAG" --target "${{ needs.decide.outputs.head }}" --title "$(cat notes/title.txt)" \ --notes-file notes/body.md --latest dist/horizon-linux-x86_64.tar.gz dist/horizon-windows-x64.zip \ - dist/horizon-macos-arm64.zip + dist/horizon-macos-arm64.zip dist/horizon-server-linux-x86_64.tar.gz - name: Dry run, the release that would be published if: needs.decide.outputs.dry_run == 'true' run: | diff --git a/crates/horizon-client/Cargo.toml b/crates/horizon-client/Cargo.toml index 8713bc1b7..82dca390d 100644 --- a/crates/horizon-client/Cargo.toml +++ b/crates/horizon-client/Cargo.toml @@ -31,7 +31,8 @@ toml = "1.1" naga = { version = "30.0.1", features = ["wgsl-in"] } # Java (OptiFine/Iris) shader packs, translated to WGSL. horizon-shaderpack = { path = "../horizon-shaderpack" } -cpal = "0.16" +# Sound (ALSA on Linux); the `audio` feature. +cpal = { version = "0.16", optional = true } symphonia = { version = "0.5", default-features = false, features = ["ogg", "vorbis"] } miniz_oxide = "0.8" ab_glyph = "0.2.32" @@ -44,10 +45,10 @@ horizon-telemetry = { path = "../horizon-telemetry", default-features = false } horizon-bench = { path = "../horizon-bench" } arboard = { version = "3.6.1", default-features = false, features = ["wayland-data-control"] } zip = { version = "2", default-features = false, features = ["deflate"] } -# Controllers (evdev + libudev on Linux, IOKit on macOS, Windows.Gaming.Input). -gilrs = "0.11" +# Controllers (evdev + libudev on Linux, IOKit on macOS, Windows.Gaming.Input); the `controllers` feature. +gilrs = { version = "0.11", optional = true } # The DualSense's rumble, adaptive triggers and lights over HID (gilrs only reads it). -horizon-dualsense = { path = "../horizon-dualsense" } +horizon-dualsense = { path = "../horizon-dualsense", optional = true } # HTTPS to the Minecraft services (online-mode session join). ureq = { version = "2", default-features = false, features = ["tls"] } @@ -60,6 +61,12 @@ rustix = { version = "1", default-features = false, features = ["std", "time"] } winresource = "0.1" [features] +default = ["audio", "controllers"] +# Sound output. Without it (and `controllers`) the program links neither libasound nor libudev on +# Linux: the server build (`--no-default-features`, released as horizon-engine; docs/server.md). +audio = ["dep:cpal"] +# Controllers: gilrs, and the DualSense's own output. Without it only `horizon-ctl pad`'s virtual pad. +controllers = ["dep:gilrs", "dep:horizon-dualsense"] # Records the call sites of mob random draws (`mob_ai_probe` traces). rng-trace = ["horizon-core/rng-trace"] diff --git a/crates/horizon-client/src/audio.rs b/crates/horizon-client/src/audio.rs index 0c45fa934..250babf22 100644 --- a/crates/horizon-client/src/audio.rs +++ b/crates/horizon-client/src/audio.rs @@ -9,6 +9,7 @@ use crate::sound_engine::{ Sounds, TickContext, }; use crate::sound_events::{Sound, SoundRegistry}; +#[cfg(feature = "audio")] use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use horizon_core::random::{LegacyRandom, RandomSource}; use std::{ @@ -134,8 +135,14 @@ fn attach(mixer: &Mutex, channel: ChannelId, samples: Samples, looping: b c.looping |= looping; } } +/// The open output stream. A build without the `audio` feature (the server build, which must not +/// need libasound) never has one. +#[cfg(feature = "audio")] +type Stream = cpal::Stream; +#[cfg(not(feature = "audio"))] +type Stream = std::convert::Infallible; pub struct Audio { - _stream: Option, + _stream: Option, pub engine: SoundEngine, backend: MixerBackend, /// Record `Heard` sounds (the subtitle overlay is listening). @@ -333,6 +340,8 @@ fn distance_gain(linear: Option, distance: f32) -> f32 { Some(_) => 0.0, } } +// Only the output stream mixes; a build without one (no `audio` feature) never does. +#[cfg_attr(not(feature = "audio"), allow(dead_code))] fn mix(mixer: &mut Mixer, data: &mut [f32], channels: usize, rate: u32) { data.fill(0.0); let listener = mixer.listener; @@ -415,6 +424,7 @@ fn mix(mixer: &mut Mixer, data: &mut [f32], channels: usize, rate: u32) { /// `HORIZON_NO_AUDIO`, or a virtual screen (`vscreen`), where agents and /// harnesses run the game and must stay off the user's speakers. /// `HORIZON_AUDIO=1` opens the output anyway (testing into a null sink). +#[cfg(any(feature = "audio", test))] fn output_disabled(var: impl Fn(&str) -> Option) -> Option { if var("HORIZON_NO_AUDIO").is_some() { return Some("audio off: HORIZON_NO_AUDIO is set".into()); @@ -430,6 +440,7 @@ fn output_disabled(var: impl Fn(&str) -> Option) -> Option>, @@ -450,6 +461,7 @@ fn open_device( stream.play()?; Ok((stream, config)) } +#[cfg(feature = "audio")] fn build_stream>( device: &cpal::Device, config: &cpal::SupportedStreamConfig, @@ -523,7 +535,12 @@ impl Audio { capture: None, } } - fn open(mixer: Arc>) -> Result { + #[cfg(not(feature = "audio"))] + fn open(_: Arc>) -> Result { + Err("this build has no sound output (built without the `audio` feature)".into()) + } + #[cfg(feature = "audio")] + fn open(mixer: Arc>) -> Result { if let Some(reason) = output_disabled(|name| std::env::var_os(name)) { return Err(reason.into()); } diff --git a/crates/horizon-client/src/game/pad.rs b/crates/horizon-client/src/game/pad.rs index 216b547b0..9a71bdc75 100644 --- a/crates/horizon-client/src/game/pad.rs +++ b/crates/horizon-client/src/game/pad.rs @@ -6,12 +6,12 @@ use super::{Game, Screen}; use horizon_client::{ Result, - gamepad::{self, Button, Edges, GlyphStyle, HealthWatch, PadState, Rumble, TriggerFeel}, + gamepad::{self, Button, Edges, GlyphStyle, HealthWatch, PadState, Rumble}, keybinds::{KeyBinds, key_code, mouse_button}, mouse::Rotation, }; use serde_json::{Value, json}; -use std::time::{Duration, Instant}; +use std::time::Instant; use winit::{event_loop::ActiveEventLoop, keyboard::KeyCode}; pub(super) const HELP: &[&str] = &[ @@ -25,10 +25,9 @@ pub(super) const HELP: &[&str] = &[ /// The controllers and the state the game last acted on. #[derive(Default)] pub(super) struct Pads { - gilrs: Option, - started: bool, - /// The pad last touched; only its input counts. - active: Option, + /// The real controllers and the DualSense's output. + #[cfg(feature = "controllers")] + devices: hardware::Devices, /// The virtual pad of `horizon-ctl pad`. injected: PadState, /// The virtual pad was in use at the last poll. @@ -48,49 +47,38 @@ pub(super) struct Pads { pub(super) style: GlyphStyle, /// The cursor the pad drives in menus. pub(super) menu: super::pad_menu::MenuCursor, - /// Vibrations playing (an effect stops when dropped) and when they end. - rumbles: Vec<(gilrs::ff::Effect, Instant)>, /// The last vibration asked for, for `pad status`. last_rumble: Option<(&'static str, gamepad::Pulse)>, /// The player's health between polls (a drop is damage taken). health: HealthWatch, - /// The active pad is a DualSense. - dualsense: bool, - /// Its own output over HID (rumble here; gilrs only reads it), started once this window - /// has the pad in hand; dropped, it releases the pad. - output: Option, - /// Its HD haptics, on USB: looked for once each time the output opens. - haptics: Option, - haptics_tried: bool, - /// What it was last told to show. - shown: Option, } -fn gilrs_button(button: Button) -> gilrs::Button { - use gilrs::Button as G; - match button { - Button::South => G::South, - Button::East => G::East, - Button::West => G::West, - Button::North => G::North, - Button::LeftBumper => G::LeftTrigger, - Button::RightBumper => G::RightTrigger, - Button::LeftTrigger => G::LeftTrigger2, - Button::RightTrigger => G::RightTrigger2, - Button::Select => G::Select, - Button::Start => G::Start, - Button::LeftStick => G::LeftThumb, - Button::RightStick => G::RightThumb, - Button::DPadUp => G::DPadUp, - Button::DPadDown => G::DPadDown, - Button::DPadLeft => G::DPadLeft, - Button::DPadRight => G::DPadRight, +#[cfg(feature = "controllers")] +mod hardware; + +/// A build without the `controllers` feature (the server build, which must not need libudev): no +/// hardware pads, only `horizon-ctl pad`'s virtual one. +#[cfg(not(feature = "controllers"))] +mod hardware { + use super::{super::Game, Pads}; + use horizon_client::gamepad::{PadState, Pulse, Rumble}; + use serde_json::{Value, json}; + + impl Pads { + pub(super) fn read(&mut self) -> PadState { + PadState::default() + } } -} -fn is_dualsense(pad: &gilrs::Gamepad) -> bool { - matches!((pad.vendor_id(), pad.product_id()), - (Some(vendor), Some(product)) if horizon_dualsense::is_dualsense(vendor, product)) + impl Game<'_> { + pub(super) fn pad_output_off(&mut self) {} + pub(super) fn pad_output_frame(&mut self) {} + pub(in crate::game) fn pad_close_output(&mut self) {} + pub(super) fn pad_vibrate(&mut self, _: Rumble, _: Pulse) {} + pub(super) fn pad_devices_status(&self) -> Value { + json!({ "backend": false, "controllers": [] }) + } + } } impl Pads { @@ -99,93 +87,6 @@ impl Pads { pub(super) fn player_replaced(&mut self) { self.health.forget(); } - /// Open gilrs on first use. A platform without it (or a Linux without - /// udev) leaves controllers off; the game runs as before. - fn start(&mut self) { - if self.started { - return; - } - self.started = true; - self.gilrs = match gilrs::Gilrs::new() { - Ok(g) => { - for (_, pad) in g.gamepads() { - log::info!("Controller connected: {}", pad.name()); - } - Some(g) - } - Err(gilrs::Error::NotImplemented(_)) => None, - Err(e) => { - log::warn!("Horizon: controllers are unavailable: {e}"); - None - } - }; - } - - /// Drain gilrs' events and read the active pad. - fn read(&mut self) -> PadState { - let Some(gilrs) = self.gilrs.as_mut() else { - return PadState::default(); - }; - while let Some(gilrs::Event { id, event, .. }) = gilrs.next_event() { - use gilrs::EventType as E; - match event { - E::Connected => { - log::info!("Controller connected: {}", gilrs.gamepad(id).name()); - if let Some(output) = &self.output { - output.scan(); - } - } - E::Disconnected => { - log::info!("Controller disconnected: {}", gilrs.gamepad(id).name()); - if self.active == Some(id) { - self.active = None; - } - if let Some(output) = &self.output { - output.scan(); - } - } - E::ButtonPressed(..) => self.active = Some(id), - E::AxisChanged(_, value, _) | E::ButtonChanged(_, value, _) - if value.abs() > 0.5 => - { - self.active = Some(id) - } - _ => {} - } - } - let Some(pad) = self.active.and_then(|id| gilrs.connected_gamepad(id)) else { - // Before any pad is touched (the virtual pad playing), a DualSense still shows it. - self.dualsense = gilrs.gamepads().any(|(_, pad)| is_dualsense(&pad)); - return PadState::default(); - }; - self.style = GlyphStyle::of_vendor(pad.vendor_id()); - self.dualsense = is_dualsense(&pad); - let mut state = PadState::default(); - for button in Button::ALL { - if !matches!(button, Button::LeftTrigger | Button::RightTrigger) { - state.set(button, pad.is_pressed(gilrs_button(button))); - } - } - let axis = |a| pad.value(a); - use gilrs::Axis as A; - state.left = [axis(A::LeftStickX), axis(A::LeftStickY)]; - state.right = [axis(A::RightStickX), axis(A::RightStickY)]; - // Triggers arrive as analog buttons on most mappings, as plain - // buttons on some. - let trigger = |b: gilrs::Button| { - let value = pad.button_data(b).map_or(0.0, |d| d.value()); - if pad.is_pressed(b) { - value.max(1.0) - } else { - value - } - }; - state.triggers = [ - trigger(gilrs::Button::LeftTrigger2), - trigger(gilrs::Button::RightTrigger2), - ]; - state - } } impl Game<'_> { @@ -193,9 +94,7 @@ impl Game<'_> { pub(super) fn poll_pads(&mut self, event_loop: &ActiveEventLoop) { let settings = &self.settings.controller; if !settings.enabled { - // Dropping the output frees the triggers. - self.pad.output = None; - self.pad.shown = None; + self.pad_output_off(); if self.pad.state != PadState::default() { self.pad.state = PadState::default(); for (button, down) in self.pad.edges.release_all() { @@ -204,7 +103,6 @@ impl Game<'_> { } return; } - self.pad.start(); let hardware = self.pad.read(); self.ui.options.pad_style = self.settings.controller.glyphs.resolve(self.pad.style); let state = hardware.merge(&self.pad.injected); @@ -219,11 +117,7 @@ impl Game<'_> { self.pad.in_use = true; } self.pad.state = state; - if self.pad.dualsense && self.pad.in_use && self.focused && self.pad.output.is_none() { - self.pad.output = Some(horizon_dualsense::Link::start()); - } - self.pad_haptics(); - self.pad_show(); + self.pad_output_frame(); // Damage from any source, here or on a server; nothing counts on // the title screen. let health = self @@ -234,8 +128,6 @@ impl Game<'_> { if let Some(damage) = self.pad.health.observe(health) { self.pad_rumble(Rumble::Hurt(damage)); } - let now = Instant::now(); - self.pad.rumbles.retain(|(_, end)| *end > now); let ticking = self.title.is_none() && self.screen == Screen::Playing && !self.pause; for button in std::mem::take(&mut self.pad.late_releases) { if !ticking || self.pad.pressed_at[button as usize] != Some(self.ticks) { @@ -404,150 +296,8 @@ impl Game<'_> { self.pitch = rotation.x_rot; } - /// The DualSense's HID output, while it is the active pad, open, and this window has focus - /// (another game on the same pad keeps it otherwise). - fn pad_output(&self) -> Option<&horizon_dualsense::Link> { - let output = self.pad.output.as_ref()?; - (self.pad.dualsense && self.focused && output.open().is_some()).then_some(output) - } - - /// What the DualSense shows: while playing, its triggers feel what their actions do; in - /// menus, paused, unfocused or with another pad active, they are free. - fn pad_show(&mut self) { - if self.pad.output.is_none() { - return; - } - let playing = self.pad.dualsense - && self.focused - && self.title.is_none() - && self.screen == Screen::Playing - && !self.pause; - let (using, attacking) = if playing { - self.trigger_feels() - } else { - (TriggerFeel::Free, TriggerFeel::Free) - }; - // The lightbar follows the player; menus keep the last colour. - let in_world = self.title.is_none(); - let lightbar = match self.player.as_mut().filter(|_| in_world) { - Some(p) => { - let max = p.attribute("minecraft:max_health").unwrap_or(20.0) as f32; - let health = p.health.health() / max.max(1.0); - self.settings - .controller - .lightbar - .colour(health, self.dim.id()) - } - None => self.pad.shown.and_then(|s| s.lightbar), - }; - let lights = self.settings.controller.lightbar != gamepad::Lightbar::Off; - let state = horizon_dualsense::device::State { - triggers: self.settings.controller.trigger_effects(using, attacking), - lightbar: lightbar.filter(|_| lights), - player_leds: lights.then_some(horizon_dualsense::PLAYER_1), - }; - if self.pad.shown != Some(state) - && let Some(output) = &self.pad.output - { - output.set(state); - self.pad.shown = Some(state); - } - } - - /// What `key.use` and `key.attack` are doing, for the triggers bound to them. - fn trigger_feels(&self) -> (TriggerFeel, TriggerFeel) { - let using = match self.using { - Some((slot, ticks)) => match self.stack_in(slot) { - Some(s) if &*s.item == "minecraft:bow" => { - TriggerFeel::Draw(gamepad::bow_power(ticks)) - } - // TridentItem.THROW_THRESHOLD_TIME. - Some(s) if &*s.item == "minecraft:trident" => { - TriggerFeel::Draw(ticks as f32 / 10.0) - } - Some(s) if &*s.item == "minecraft:crossbow" => { - TriggerFeel::Draw(ticks as f32 / Self::charge_ticks(s).max(1) as f32) - } - Some(s) if self.blocks_attacks(s).is_some() => TriggerFeel::Brace, - _ => TriggerFeel::Free, - }, - None => self.idle_use_feel(), - }; - // Creative breaks at once; blocks that can't be broken have a negative hardness. - let attacking = match &self.mining { - Some(m) if !self.creative => { - let state = self.world.block(m.pos[0], m.pos[1], m.pos[2]); - TriggerFeel::Texture(self.res.facts().get(state).hardness) - } - _ => TriggerFeel::Free, - }; - (using, attacking) - } - - /// The use trigger while nothing is in use: a loaded crossbow clicks, an item that boosts (a - /// rocket while gliding, the stick of the mount being ridden) catches, a shield weighs, and - /// locks while an axe has disabled it. - fn idle_use_feel(&self) -> TriggerFeel { - let gliding = self.player.as_ref().is_some_and(|p| p.fall_flying); - let mount = self - .riding - .and_then(|id| self.mobs.iter().find(|m| m.id == id)); - let mount = mount.map_or("", |m| m.kind.id.as_str()); - let feel = |stack: Option<&horizon_client::items::ItemStack>| match stack - .map(|s| (s, &*s.item)) - { - Some((s, "minecraft:crossbow")) if s.data().is_some_and(|d| !d.charged.is_empty()) => { - TriggerFeel::Click - } - Some((_, "minecraft:firework_rocket")) if gliding => TriggerFeel::Boost, - Some((_, "minecraft:carrot_on_a_stick")) if mount == "minecraft:pig" => { - TriggerFeel::Boost - } - Some((_, "minecraft:warped_fungus_on_a_stick")) if mount == "minecraft:strider" => { - TriggerFeel::Boost - } - Some((s, _)) if self.blocks_attacks(s).is_some() => match self.shield_cooldown { - 0 => TriggerFeel::Brace, - _ => TriggerFeel::Locked, - }, - _ => TriggerFeel::Free, - }; - // Use tries the main hand first. - match feel(self.stack_in(self.selected)) { - TriggerFeel::Free => feel(self.offhand.as_ref()), - main => main, - } - } - - /// Open the pad's haptics once its output is open over USB (its audio device is there - /// only then); close them when it goes. Opt-in with `HORIZON_DUALSENSE_HAPTICS=1` until - /// they are proven on a pad (docs/controllers.md); never for games without sound (tests). - fn pad_haptics(&mut self) { - let usb = self.pad.output.as_ref().and_then(|o| o.open()) - == Some(horizon_dualsense::Transport::Usb); - if !usb { - self.pad.haptics = None; - self.pad.haptics_tried = false; - } else if !self.pad.haptics_tried { - self.pad.haptics_tried = true; - if std::env::var_os("HORIZON_DUALSENSE_HAPTICS").is_some_and(|v| v == "1") - && std::env::var_os("HORIZON_NO_AUDIO").is_none() - { - self.pad.haptics = horizon_dualsense::haptics::Haptics::open(); - } - } - } - - /// Release the DualSense's output (the game is quitting). - pub(super) fn pad_close_output(&mut self) { - self.pad.haptics = None; - self.pad.output = None; - } - /// Vibrate the active pad, when vibration is on and the pad is the one - /// playing (a pad lying idle while the mouse plays stays still): a - /// DualSense through its own report, else gilrs' force feedback, which - /// platforms without it (gilrs on macOS) skip. + /// playing (a pad lying idle while the mouse plays stays still). pub(super) fn pad_rumble(&mut self, rumble: Rumble) { let settings = &self.settings.controller; if !settings.enabled || !settings.vibration || !self.pad.in_use { @@ -557,68 +307,7 @@ impl Game<'_> { return; }; self.pad.last_rumble = Some((rumble.name(), pulse)); - let level = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; - if let Some(haptics) = self - .pad - .haptics - .as_ref() - .filter(|_| self.pad_output().is_some()) - { - let steps = rumble.shape().into_iter().map(|step| { - ( - step.strong, - step.weak, - Duration::from_millis(u64::from(step.millis)), - ) - }); - haptics.play(&steps.collect::>()); - return; - } - if let Some(output) = self.pad_output() { - let steps = rumble.shape().into_iter().map(|step| { - let length = Duration::from_millis(u64::from(step.millis)); - ([level(step.strong), level(step.weak)], length) - }); - output.rumble(steps.collect()); - return; - } - let (Some(gilrs), Some(id)) = (self.pad.gilrs.as_mut(), self.pad.active) else { - return; - }; - if !gilrs - .connected_gamepad(id) - .is_some_and(|g| g.is_ff_supported()) - { - return; - } - use gilrs::ff::{BaseEffect, BaseEffectType, EffectBuilder, Repeat, Replay, Ticks}; - let length = Ticks::from_ms(pulse.millis); - let motor = |kind| BaseEffect { - kind, - scheduling: Replay { - play_for: length, - ..Default::default() - }, - envelope: Default::default(), - }; - let magnitude = |v: f32| (v.clamp(0.0, 1.0) * f32::from(u16::MAX)) as u16; - let effect = EffectBuilder::new() - .add_effect(motor(BaseEffectType::Strong { - magnitude: magnitude(pulse.strong), - })) - .add_effect(motor(BaseEffectType::Weak { - magnitude: magnitude(pulse.weak), - })) - .repeat(Repeat::For(length)) - .gamepads(&[id]) - .finish(gilrs); - match effect.and_then(|e| e.play().map(|()| e)) { - Ok(effect) => { - let end = Instant::now() + Duration::from_millis(u64::from(pulse.millis) + 50); - self.pad.rumbles.push((effect, end)); - } - Err(e) => log::debug!("Horizon: controller vibration: {e}"), - } + self.pad_vibrate(rumble, pulse); } /// `horizon-ctl pad ...`. @@ -639,32 +328,15 @@ impl Game<'_> { }; match arg(1)? { "status" => { - let pads: Vec = self.pad.gilrs.as_ref().map_or(Vec::new(), |g| { - g.gamepads() - .map(|(id, p)| { - json!({ - "name": p.name(), - "vendor": p.vendor_id(), - "product": p.product_id(), - "active": self.pad.active == Some(id), - }) - }) - .collect() - }); let s = &self.pad.state; let held: Vec<&str> = Button::ALL .into_iter() .filter(|b| self.pad.edges.is_held(*b)) .map(Button::name) .collect(); - Ok(json!({ + let mut status = json!({ "enabled": self.settings.controller.enabled, - "backend": self.pad.gilrs.is_some(), - "controllers": pads, "glyphs": self.pad.style.name(), - "dualsense_output": self.pad.output.as_ref().and_then(|o| o.open()) - .map(|t| format!("{t:?}")), - "trigger_effects": self.pad.shown.map(|s| s.triggers), "last_rumble": self.pad.last_rumble.map(|(why, p)| json!({ "reason": why, "strong": p.strong, "weak": p.weak, "ms": p.millis, })), @@ -674,7 +346,13 @@ impl Game<'_> { "left": s.left, "right": s.right, "triggers": s.triggers, - })) + }); + if let (Some(status), Value::Object(devices)) = + (status.as_object_mut(), self.pad_devices_status()) + { + status.extend(devices); + } + Ok(status) } "release" => { self.pad.injected = PadState::default(); diff --git a/crates/horizon-client/src/game/pad/hardware.rs b/crates/horizon-client/src/game/pad/hardware.rs new file mode 100644 index 000000000..b464ef3ee --- /dev/null +++ b/crates/horizon-client/src/game/pad/hardware.rs @@ -0,0 +1,409 @@ +//! The real controllers (the `controllers` feature): gilrs' pads, and the DualSense's own output +//! over HID: its rumble, adaptive triggers, lights and HD haptics. A build without the feature (the +//! server build) has the stub in pad.rs instead, and only `horizon-ctl pad`'s virtual pad. +use super::{ + super::{Game, Screen}, + Pads, +}; +use horizon_client::gamepad::{self, Button, GlyphStyle, PadState, Rumble, TriggerFeel}; +use serde_json::{Value, json}; +use std::time::{Duration, Instant}; + +/// The controllers gilrs sees and the DualSense's output. +#[derive(Default)] +pub(super) struct Devices { + gilrs: Option, + started: bool, + /// The pad last touched; only its input counts. + active: Option, + /// Vibrations playing (an effect stops when dropped) and when they end. + rumbles: Vec<(gilrs::ff::Effect, Instant)>, + /// The active pad is a DualSense. + dualsense: bool, + /// Its own output over HID (rumble here; gilrs only reads it), started once this window + /// has the pad in hand; dropped, it releases the pad. + output: Option, + /// Its HD haptics, on USB: looked for once each time the output opens. + haptics: Option, + haptics_tried: bool, + /// What it was last told to show. + shown: Option, +} + +fn gilrs_button(button: Button) -> gilrs::Button { + use gilrs::Button as G; + match button { + Button::South => G::South, + Button::East => G::East, + Button::West => G::West, + Button::North => G::North, + Button::LeftBumper => G::LeftTrigger, + Button::RightBumper => G::RightTrigger, + Button::LeftTrigger => G::LeftTrigger2, + Button::RightTrigger => G::RightTrigger2, + Button::Select => G::Select, + Button::Start => G::Start, + Button::LeftStick => G::LeftThumb, + Button::RightStick => G::RightThumb, + Button::DPadUp => G::DPadUp, + Button::DPadDown => G::DPadDown, + Button::DPadLeft => G::DPadLeft, + Button::DPadRight => G::DPadRight, + } +} + +fn is_dualsense(pad: &gilrs::Gamepad) -> bool { + matches!((pad.vendor_id(), pad.product_id()), + (Some(vendor), Some(product)) if horizon_dualsense::is_dualsense(vendor, product)) +} + +impl Pads { + /// Open gilrs on first use. A platform without it (or a Linux without + /// udev) leaves controllers off; the game runs as before. + fn start(&mut self) { + if self.devices.started { + return; + } + self.devices.started = true; + self.devices.gilrs = match gilrs::Gilrs::new() { + Ok(g) => { + for (_, pad) in g.gamepads() { + log::info!("Controller connected: {}", pad.name()); + } + Some(g) + } + Err(gilrs::Error::NotImplemented(_)) => None, + Err(e) => { + log::warn!("Horizon: controllers are unavailable: {e}"); + None + } + }; + } + + /// Drain gilrs' events and read the active pad (opening gilrs the first time). + pub(super) fn read(&mut self) -> PadState { + self.start(); + let Some(gilrs) = self.devices.gilrs.as_mut() else { + return PadState::default(); + }; + while let Some(gilrs::Event { id, event, .. }) = gilrs.next_event() { + use gilrs::EventType as E; + match event { + E::Connected => { + log::info!("Controller connected: {}", gilrs.gamepad(id).name()); + if let Some(output) = &self.devices.output { + output.scan(); + } + } + E::Disconnected => { + log::info!("Controller disconnected: {}", gilrs.gamepad(id).name()); + if self.devices.active == Some(id) { + self.devices.active = None; + } + if let Some(output) = &self.devices.output { + output.scan(); + } + } + E::ButtonPressed(..) => self.devices.active = Some(id), + E::AxisChanged(_, value, _) | E::ButtonChanged(_, value, _) + if value.abs() > 0.5 => + { + self.devices.active = Some(id) + } + _ => {} + } + } + let Some(pad) = self + .devices + .active + .and_then(|id| gilrs.connected_gamepad(id)) + else { + // Before any pad is touched (the virtual pad playing), a DualSense still shows it. + self.devices.dualsense = gilrs.gamepads().any(|(_, pad)| is_dualsense(&pad)); + return PadState::default(); + }; + self.style = GlyphStyle::of_vendor(pad.vendor_id()); + self.devices.dualsense = is_dualsense(&pad); + let mut state = PadState::default(); + for button in Button::ALL { + if !matches!(button, Button::LeftTrigger | Button::RightTrigger) { + state.set(button, pad.is_pressed(gilrs_button(button))); + } + } + let axis = |a| pad.value(a); + use gilrs::Axis as A; + state.left = [axis(A::LeftStickX), axis(A::LeftStickY)]; + state.right = [axis(A::RightStickX), axis(A::RightStickY)]; + // Triggers arrive as analog buttons on most mappings, as plain + // buttons on some. + let trigger = |b: gilrs::Button| { + let value = pad.button_data(b).map_or(0.0, |d| d.value()); + if pad.is_pressed(b) { + value.max(1.0) + } else { + value + } + }; + state.triggers = [ + trigger(gilrs::Button::LeftTrigger2), + trigger(gilrs::Button::RightTrigger2), + ]; + state + } +} + +impl Game<'_> { + /// Controllers are turned off: dropping the DualSense's output frees its triggers. + pub(super) fn pad_output_off(&mut self) { + self.pad.devices.output = None; + self.pad.devices.shown = None; + } + + /// After each poll: the DualSense's output opens once this window has the pad in hand, then + /// shows what the game is doing; vibrations that are over stop. + pub(super) fn pad_output_frame(&mut self) { + if self.pad.devices.dualsense + && self.pad.in_use + && self.focused + && self.pad.devices.output.is_none() + { + self.pad.devices.output = Some(horizon_dualsense::Link::start()); + } + self.pad_haptics(); + self.pad_show(); + let now = Instant::now(); + self.pad.devices.rumbles.retain(|(_, end)| *end > now); + } + + /// The DualSense's HID output, while it is the active pad, open, and this window has focus + /// (another game on the same pad keeps it otherwise). + fn pad_output(&self) -> Option<&horizon_dualsense::Link> { + let output = self.pad.devices.output.as_ref()?; + (self.pad.devices.dualsense && self.focused && output.open().is_some()).then_some(output) + } + + /// What the DualSense shows: while playing, its triggers feel what their actions do; in + /// menus, paused, unfocused or with another pad active, they are free. + fn pad_show(&mut self) { + if self.pad.devices.output.is_none() { + return; + } + let playing = self.pad.devices.dualsense + && self.focused + && self.title.is_none() + && self.screen == Screen::Playing + && !self.pause; + let (using, attacking) = if playing { + self.trigger_feels() + } else { + (TriggerFeel::Free, TriggerFeel::Free) + }; + // The lightbar follows the player; menus keep the last colour. + let in_world = self.title.is_none(); + let lightbar = match self.player.as_mut().filter(|_| in_world) { + Some(p) => { + let max = p.attribute("minecraft:max_health").unwrap_or(20.0) as f32; + let health = p.health.health() / max.max(1.0); + self.settings + .controller + .lightbar + .colour(health, self.dim.id()) + } + None => self.pad.devices.shown.and_then(|s| s.lightbar), + }; + let lights = self.settings.controller.lightbar != gamepad::Lightbar::Off; + let state = horizon_dualsense::device::State { + triggers: self.settings.controller.trigger_effects(using, attacking), + lightbar: lightbar.filter(|_| lights), + player_leds: lights.then_some(horizon_dualsense::PLAYER_1), + }; + if self.pad.devices.shown != Some(state) + && let Some(output) = &self.pad.devices.output + { + output.set(state); + self.pad.devices.shown = Some(state); + } + } + + /// What `key.use` and `key.attack` are doing, for the triggers bound to them. + fn trigger_feels(&self) -> (TriggerFeel, TriggerFeel) { + let using = match self.using { + Some((slot, ticks)) => match self.stack_in(slot) { + Some(s) if &*s.item == "minecraft:bow" => { + TriggerFeel::Draw(gamepad::bow_power(ticks)) + } + // TridentItem.THROW_THRESHOLD_TIME. + Some(s) if &*s.item == "minecraft:trident" => { + TriggerFeel::Draw(ticks as f32 / 10.0) + } + Some(s) if &*s.item == "minecraft:crossbow" => { + TriggerFeel::Draw(ticks as f32 / Self::charge_ticks(s).max(1) as f32) + } + Some(s) if self.blocks_attacks(s).is_some() => TriggerFeel::Brace, + _ => TriggerFeel::Free, + }, + None => self.idle_use_feel(), + }; + // Creative breaks at once; blocks that can't be broken have a negative hardness. + let attacking = match &self.mining { + Some(m) if !self.creative => { + let state = self.world.block(m.pos[0], m.pos[1], m.pos[2]); + TriggerFeel::Texture(self.res.facts().get(state).hardness) + } + _ => TriggerFeel::Free, + }; + (using, attacking) + } + + /// The use trigger while nothing is in use: a loaded crossbow clicks, an item that boosts (a + /// rocket while gliding, the stick of the mount being ridden) catches, a shield weighs, and + /// locks while an axe has disabled it. + fn idle_use_feel(&self) -> TriggerFeel { + let gliding = self.player.as_ref().is_some_and(|p| p.fall_flying); + let mount = self + .riding + .and_then(|id| self.mobs.iter().find(|m| m.id == id)); + let mount = mount.map_or("", |m| m.kind.id.as_str()); + let feel = |stack: Option<&horizon_client::items::ItemStack>| match stack + .map(|s| (s, &*s.item)) + { + Some((s, "minecraft:crossbow")) if s.data().is_some_and(|d| !d.charged.is_empty()) => { + TriggerFeel::Click + } + Some((_, "minecraft:firework_rocket")) if gliding => TriggerFeel::Boost, + Some((_, "minecraft:carrot_on_a_stick")) if mount == "minecraft:pig" => { + TriggerFeel::Boost + } + Some((_, "minecraft:warped_fungus_on_a_stick")) if mount == "minecraft:strider" => { + TriggerFeel::Boost + } + Some((s, _)) if self.blocks_attacks(s).is_some() => match self.shield_cooldown { + 0 => TriggerFeel::Brace, + _ => TriggerFeel::Locked, + }, + _ => TriggerFeel::Free, + }; + // Use tries the main hand first. + match feel(self.stack_in(self.selected)) { + TriggerFeel::Free => feel(self.offhand.as_ref()), + main => main, + } + } + + /// Open the pad's haptics once its output is open over USB (its audio device is there + /// only then); close them when it goes. Opt-in with `HORIZON_DUALSENSE_HAPTICS=1` until + /// they are proven on a pad (docs/controllers.md); never for games without sound (tests). + fn pad_haptics(&mut self) { + let usb = self.pad.devices.output.as_ref().and_then(|o| o.open()) + == Some(horizon_dualsense::Transport::Usb); + if !usb { + self.pad.devices.haptics = None; + self.pad.devices.haptics_tried = false; + } else if !self.pad.devices.haptics_tried { + self.pad.devices.haptics_tried = true; + if std::env::var_os("HORIZON_DUALSENSE_HAPTICS").is_some_and(|v| v == "1") + && std::env::var_os("HORIZON_NO_AUDIO").is_none() + { + self.pad.devices.haptics = horizon_dualsense::haptics::Haptics::open(); + } + } + } + + /// Release the DualSense's output (the game is quitting). + pub(in crate::game) fn pad_close_output(&mut self) { + self.pad.devices.haptics = None; + self.pad.devices.output = None; + } + + /// Play a vibration on the active pad: a DualSense through its own report, else gilrs' force + /// feedback, which platforms without it (gilrs on macOS) skip. + pub(super) fn pad_vibrate(&mut self, rumble: Rumble, pulse: gamepad::Pulse) { + let level = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; + if let Some(haptics) = self + .pad + .devices + .haptics + .as_ref() + .filter(|_| self.pad_output().is_some()) + { + let steps = rumble.shape().into_iter().map(|step| { + ( + step.strong, + step.weak, + Duration::from_millis(u64::from(step.millis)), + ) + }); + haptics.play(&steps.collect::>()); + return; + } + if let Some(output) = self.pad_output() { + let steps = rumble.shape().into_iter().map(|step| { + let length = Duration::from_millis(u64::from(step.millis)); + ([level(step.strong), level(step.weak)], length) + }); + output.rumble(steps.collect()); + return; + } + let (Some(gilrs), Some(id)) = (self.pad.devices.gilrs.as_mut(), self.pad.devices.active) + else { + return; + }; + if !gilrs + .connected_gamepad(id) + .is_some_and(|g| g.is_ff_supported()) + { + return; + } + use gilrs::ff::{BaseEffect, BaseEffectType, EffectBuilder, Repeat, Replay, Ticks}; + let length = Ticks::from_ms(pulse.millis); + let motor = |kind| BaseEffect { + kind, + scheduling: Replay { + play_for: length, + ..Default::default() + }, + envelope: Default::default(), + }; + let magnitude = |v: f32| (v.clamp(0.0, 1.0) * f32::from(u16::MAX)) as u16; + let effect = EffectBuilder::new() + .add_effect(motor(BaseEffectType::Strong { + magnitude: magnitude(pulse.strong), + })) + .add_effect(motor(BaseEffectType::Weak { + magnitude: magnitude(pulse.weak), + })) + .repeat(Repeat::For(length)) + .gamepads(&[id]) + .finish(gilrs); + match effect.and_then(|e| e.play().map(|()| e)) { + Ok(effect) => { + let end = Instant::now() + Duration::from_millis(u64::from(pulse.millis) + 50); + self.pad.devices.rumbles.push((effect, end)); + } + Err(e) => log::debug!("Horizon: controller vibration: {e}"), + } + } + + /// The hardware part of `pad status`. + pub(super) fn pad_devices_status(&self) -> Value { + let devices = &self.pad.devices; + let pads: Vec = devices.gilrs.as_ref().map_or(Vec::new(), |g| { + g.gamepads() + .map(|(id, p)| { + json!({ + "name": p.name(), + "vendor": p.vendor_id(), + "product": p.product_id(), + "active": devices.active == Some(id), + }) + }) + .collect() + }); + json!({ + "backend": devices.gilrs.is_some(), + "controllers": pads, + "dualsense_output": devices.output.as_ref().and_then(|o| o.open()).map(|t| format!("{t:?}")), + "trigger_effects": devices.shown.map(|s| s.triggers), + }) + } +} diff --git a/crates/horizon-client/src/gamepad.rs b/crates/horizon-client/src/gamepad.rs index a250f5e8c..220f74a5b 100644 --- a/crates/horizon-client/src/gamepad.rs +++ b/crates/horizon-client/src/gamepad.rs @@ -1009,6 +1009,7 @@ pub enum TriggerFeel { /// Mining a block of this hardness: a buzz, stronger and slower on harder blocks. Texture(f32), } +#[cfg(feature = "controllers")] impl TriggerFeel { /// The effect, for a trigger that presses its action at `threshold` (0..1) of its travel. pub fn effect(self, threshold: f32) -> horizon_dualsense::TriggerEffect { @@ -1044,6 +1045,7 @@ impl TriggerFeel { } } } +#[cfg(feature = "controllers")] impl PadSettings { /// The left and right triggers' effects: each feels the action bound to it, `key.use` or /// `key.attack`. @@ -1404,6 +1406,7 @@ mod tests { assert_eq!(w.observe(Some(1.0)), Some(1.0)); } + #[cfg(feature = "controllers")] #[test] fn triggers_feel_their_action() { use horizon_dualsense::{TRIGGER_OFF, trigger}; diff --git a/crates/horizon-server/src/engine.rs b/crates/horizon-server/src/engine.rs index d1373bb05..aa83693cb 100644 --- a/crates/horizon-server/src/engine.rs +++ b/crates/horizon-server/src/engine.rs @@ -11,7 +11,9 @@ use std::{ time::Duration, }; -/// The game's program names, most specific first: a headless engine once there is one, else the game. +/// The game's program names, most specific first: the server build (the game without sound or +/// controllers, so it needs neither ALSA nor udev on Linux; `horizon-server-linux-x86_64.tar.gz` holds it), +/// else the full game. const PROGRAMS: [&str; 2] = ["horizon-engine", "horizon-client"]; fn exe(name: &str) -> String { @@ -50,8 +52,8 @@ pub fn program(config: &Config, dir: &Path) -> Result { } /// Checks that the game program starts at all: `--help` loads no game files, so it fails only when the -/// system can't load the program, as on a Linux without the ALSA and udev libraries it links (even a -/// server needs them until there is a headless engine). The reason, with what to install. +/// system can't load the program, as on a Linux without the ALSA and udev libraries the full game links +/// (the server build doesn't). The reason, with what to install. pub fn check_starts(program: &Path) -> Result<(), String> { let output = Command::new(program) .arg("--help") @@ -84,10 +86,13 @@ fn start_failure(program: &Path, code: Option, stderr: &str) -> String { format!(": {reason}") } ); - if reason.contains("error while loading shared libraries") { + if reason.contains("error while loading shared libraries") + && (reason.contains("libasound") || reason.contains("libudev")) + { text.push_str( - "\nIt needs ALSA's and udev's libraries (libasound.so.2, libudev.so.1), even on a server. Install \ - them:\n Debian, Ubuntu: sudo apt install libasound2 libudev1 (Ubuntu 24.04 and later: \ + "\nThe full game needs ALSA's and udev's libraries (libasound.so.2, libudev.so.1). Install the \ + server build, which needs neither (packaging/server/install.sh picks it on Linux), or the \ + libraries:\n Debian, Ubuntu: sudo apt install libasound2 libudev1 (Ubuntu 24.04 and later: \ libasound2t64)\n Fedora, RHEL: sudo dnf install alsa-lib systemd-libs\n Arch: sudo pacman -S \ alsa-lib systemd-libs\n NixOS: enable programs.nix-ld with alsa-lib and udev in its libraries", ); @@ -256,6 +261,14 @@ mod tests { error while loading shared libraries: libasound.so.2" )); assert!(text.contains("sudo apt install libasound2 libudev1")); + // Another library: no ALSA advice (the server build, horizon-engine, links neither). + let engine = start_failure( + Path::new("/opt/horizon/horizon-engine"), + Some(127), + "/opt/horizon/horizon-engine: error while loading shared libraries: libgcc_s.so.1: cannot open \ + shared object file: No such file or directory\n", + ); + assert!(!engine.contains("libasound"), "{engine}"); let other = start_failure(program, Some(1), "something else\n"); assert_eq!( other, diff --git a/docs/release-flow.md b/docs/release-flow.md index f1015a31f..b72d896b8 100644 --- a/docs/release-flow.md +++ b/docs/release-flow.md @@ -131,6 +131,7 @@ the notes. | Archive | Runs on | Checks | | --- | --- | --- | | `horizon-linux-x86_64.tar.gz` | GitHub `ubuntu-22.04` (an older glibc, so it runs on any recent distribution) | the standard loader and libraries only (`tools/check_imports.py`) | +| `horizon-server-linux-x86_64.tar.gz` | the same Linux job, after the client archive: `horizon-server`, `horizon-prepare` and the game built with `--no-default-features` as `horizon-engine` (docs/server.md) | as above, and neither `libasound.so.2` nor `libudev.so.1` (`tools/check_imports.py --server`) | | `horizon-windows-x64.zip` | GitHub `windows-latest`, C runtime linked in | system DLLs only (`tools/check_imports.py`) | | `horizon-macos-arm64.zip` | The Mac mini's runner (`vars.MAC_RUNNER=macos-latest` sends it to GitHub) | system libraries only (`otool -L`); a 1500-frame offscreen run of a new world (about 12 s; 300 frames end before the world has loaded) with resources prepared from the runner's own Minecraft (`--offscreen --world smoke --frames 1500 --smoke-report`: frames drawn, chunks generated and meshed, a player, no error) | diff --git a/docs/server.md b/docs/server.md index 923eddf2c..015ded555 100644 --- a/docs/server.md +++ b/docs/server.md @@ -55,9 +55,10 @@ distance 10. The vanilla server was the pinned 26.3 `server.jar` on Java 25 with | What has to be installed | one 46.8 MB binary + 589 MB of prepared resources | Java 25 + the 62 MB jar | | Crates linked | 384 (wgpu, winit, cpal, gilrs, arboard, naga…) | n/a | -On Linux the binary loads `libasound.so.2` (cpal) and `libudev.so.1` (gilrs) at startup, even on a server. -wgpu, winit and the Wayland/X11 libraries are loaded only when a window opens. That makes a minimal -container image need ALSA and udev for a program that never plays a sound or reads a controller. +On Linux the full game loads `libasound.so.2` (cpal) and `libudev.so.1` (gilrs) at startup. wgpu, winit +and the Wayland/X11 libraries are loaded only when a window opens. The server build (`horizon-engine`, +section 3) is the same program built without its `audio` and `controllers` features, so it needs neither +library, and a minimal server image runs it as it is. The two clear losses are RAM and idle CPU. The dedicated server loads everything the client needs to draw (the HUD atlas, item renderer, block-entity models, panorama, fog and music tables, entity models), and it @@ -145,16 +146,22 @@ game built in `Game::new`, the integrated server on its own thread. On that base 1. The dedicated path skips everything only a window uses: the HUD atlas, item renderer, block-entity and entity models, panorama, fog, music, and mesh tables (perf-ticks-2 already stops server-side meshing). This is the RAM and start-time item. -2. The game moves into a library crate, with the window, audio and controller parts behind a `client` - feature. A `horizon-engine` binary built without that feature links no wgpu, winit, cpal or gilrs, and - needs neither ALSA nor udev. The supervisor finds it beside itself and falls back to `horizon-client`. +2. The game moves into a library crate, with the window parts behind a `client` feature, so + `horizon-engine` links no wgpu or winit either. Sound and controllers are already optional (the + `audio` and `controllers` features, on by default): `horizon-engine` today is the game built without + them (section 3). ## 3. Packaging -**Binaries.** The release archives already hold `horizon-client`, `horizon-ctl` and `horizon-prepare`. -`horizon-server` joins them (PR 3: `xtask package` and `release.yml` build it and check it starts). Once -`horizon-engine` exists (section 2), a separate small archive per platform -(`horizon-server--.tar.gz` or `.zip`) holds just the server's binaries. +**Binaries.** The release archives hold `horizon-client`, `horizon-ctl`, `horizon-prepare` and +`horizon-server`. On Linux the release also has `horizon-server-linux-x86_64.tar.gz`: `horizon-server`, +`horizon-prepare` and `horizon-engine`, the game built with `--no-default-features` (no sound, no +controllers), which links neither `libasound.so.2` nor `libudev.so.1`. `release.yml` builds it after the +client archive (`xtask package-server`) and fails if `tools/check_imports.py --server` finds either library; +CI lints the same build. `horizon-server` runs `horizon-engine` when it is beside it or on the PATH, else +`horizon-client`. Build it yourself with `cargo build --release -p horizon-client --no-default-features` +and copy `target/release/horizon-client` as `horizon-engine`. macOS and Windows have no such libraries to +avoid, so they keep the one archive. **Game files: the owner's own copy, as with `prepare`.** Nothing of Mojang's is redistributed and nothing is downloaded on the owner's behalf. `horizon-server` looks for prepared resources in this order: @@ -181,15 +188,16 @@ comes out of the headless-engine work. - Linux and macOS: `curl -fsSL https://raw.githubusercontent.com/ludovic111/horizon/main/packaging/server/install.sh | sh` - The script picks the release archive for the OS and architecture (Linux x86_64, macOS arm64), downloads - it from GitHub Releases, and checks it against the SHA-256 GitHub publishes for each asset. It never asks - for sudo. Options: `--version vX.Y.Z`, `--dir` (default `~/.local/share/horizon`), `--bin` (default - `~/.local/bin`). - On Linux the game links ALSA's and udev's libraries (`libasound.so.2`, `libudev.so.1`) even as a server, - until the headless engine exists. Minimal server images often lack ALSA (Debian and Ubuntu: - `sudo apt install libasound2 libudev1`, `libasound2t64` on Ubuntu 24.04 and later). The script lists any - that are missing, `init` warns, and `run` stops at once with what to install, rather than restarting a - game that can't load. + The script picks the release archive for the OS and architecture (on Linux x86_64 the server archive, + on macOS arm64 the game's), downloads it from GitHub Releases, and checks it against the SHA-256 GitHub + publishes for each asset. It never asks for sudo. Options: `--version vX.Y.Z`, `--dir` (default + `~/.local/share/horizon`), `--bin` (default `~/.local/bin`), and `--archive FILE` to install an archive + already on the machine (a build of your own; no download and no checksum). + A Linux release from before the server archive installs the full game, which links ALSA's and udev's + libraries (`libasound.so.2`, `libudev.so.1`); minimal server images often lack them (Debian and Ubuntu: + `sudo apt install libasound2 libudev1`, `libasound2t64` on Ubuntu 24.04 and later). Then the script lists + any that are missing, `init` warns, and `run` stops at once with what to install, rather than restarting + a game that can't load. - Windows: `irm https://raw.githubusercontent.com/ludovic111/horizon/main/packaging/server/install.ps1 | iex` The script does the same into `%LOCALAPPDATA%\Horizon` (`-Version`, `-Dir`), with junctions instead of @@ -201,8 +209,8 @@ comes out of the headless-engine work. (`horizon-prepare`, `horizon-server init`). **Docker** (`packaging/server/Dockerfile`, built the same way as the relay's image): a multi-stage build that -ends on `debian:bookworm-slim` as user `horizon`. It adds `libasound2` and `libudev1` until the headless engine -lands. It uses volume `/data` for the server folder, takes the owner's prepared game files as a read-only +ends on `debian:bookworm-slim` as user `horizon`, with the server build as `horizon-engine`, so the image needs +no ALSA or udev. It uses volume `/data` for the server folder, takes the owner's prepared game files as a read-only mount on `/opt/horizon/.local/horizon-client/26.3`, exposes port `25565/tcp`, and keeps the control API (and later the panel) on 127.0.0.1 inside the container: `docker exec horizon-server status|cmd|backup`. On the first start, `HORIZON_EULA=true` and `HORIZON_WORLD`, `HORIZON_SEED`, `HORIZON_PORT`, diff --git a/packaging/server/Dockerfile b/packaging/server/Dockerfile index bbb74ba26..5ae395205 100644 --- a/packaging/server/Dockerfile +++ b/packaging/server/Dockerfile @@ -13,20 +13,15 @@ # start writes /data/server.toml from HORIZON_* (EULA, WORLD, SEED, PORT, WHITELIST, PLAYERS, OP); later # starts use the file. Manage it with `docker exec horizon horizon-server status|cmd|backup|logs ...`. FROM rust:1-slim-bookworm AS build -RUN apt-get update \ - && apt-get install -y --no-install-recommends pkg-config libasound2-dev libudev-dev \ - && rm -rf /var/lib/apt/lists/* WORKDIR /src COPY . . -RUN cargo build --release --locked -p horizon-client --bin horizon-client -p horizon-server --bin horizon-server \ - && mkdir /out && cp target/release/horizon-client target/release/horizon-server /out/ +# The server build of the game (no sound, no controllers: it needs neither ALSA nor udev) as horizon-engine. +RUN cargo build --release --locked -p horizon-client --no-default-features --bin horizon-client \ + && cargo build --release --locked -p horizon-server --bin horizon-server \ + && mkdir /out && cp target/release/horizon-client /out/horizon-engine && cp target/release/horizon-server /out/ FROM debian:bookworm-slim -# The game links ALSA and udev even when it runs headless with no sound. -RUN apt-get update \ - && apt-get install -y --no-install-recommends libasound2 libudev1 \ - && rm -rf /var/lib/apt/lists/* \ - && useradd --system --home-dir /data --shell /usr/sbin/nologin horizon \ +RUN useradd --system --home-dir /data --shell /usr/sbin/nologin horizon \ && mkdir -p /data /opt/horizon/.local/horizon-client \ && chown horizon:horizon /data COPY --from=build /out/ /opt/horizon/ diff --git a/packaging/server/install.sh b/packaging/server/install.sh index 08b710e37..eed312c74 100755 --- a/packaging/server/install.sh +++ b/packaging/server/install.sh @@ -1,9 +1,11 @@ #!/bin/sh # Installs horizon-server (docs/server.md) on Linux or macOS: the latest Horizon release for this computer, -# downloaded from GitHub Releases and checked against the SHA-256 GitHub publishes for it. +# downloaded from GitHub Releases and checked against the SHA-256 GitHub publishes for it. On Linux that is +# the server build (horizon-server-linux-x86_64.tar.gz), which needs neither ALSA nor udev. # # curl -fsSL https://raw.githubusercontent.com/ludovic111/horizon/main/packaging/server/install.sh | sh # curl -fsSL .../install.sh | sh -s -- --version v0.5.0 --dir /opt/horizon --bin /usr/local/bin +# sh install.sh --archive horizon-server-linux-x86_64.tar.gz --version mybuild (an archive you have) # # It needs curl, tar or unzip, and sha256sum or shasum; it never asks for sudo (pick folders you can write). # Releases go to /releases/, /current points at the one in use, and /horizon-server @@ -15,6 +17,7 @@ REPO=ludovic111/horizon VERSION=latest DIR="${XDG_DATA_HOME:-$HOME/.local/share}/horizon" BIN="$HOME/.local/bin" +ARCHIVE= die() { echo "horizon install: $*" >&2; exit 1; } @@ -23,45 +26,58 @@ while [ $# -gt 0 ]; do --version) [ $# -ge 2 ] || die "--version needs a tag"; VERSION=$2; shift 2 ;; --dir) [ $# -ge 2 ] || die "--dir needs a folder"; DIR=$2; shift 2 ;; --bin) [ $# -ge 2 ] || die "--bin needs a folder"; BIN=$2; shift 2 ;; - -h|--help) sed -n '2,12p' "$0" 2>/dev/null || true; exit 0 ;; + --archive) [ $# -ge 2 ] || die "--archive needs a file"; ARCHIVE=$2; shift 2 ;; + -h|--help) sed -n '2,14p' "$0" 2>/dev/null || true; exit 0 ;; *) die "unknown option $1" ;; esac done +# The archives to look for, in order: on Linux the server build, else (releases before it) the full game. case "$(uname -s)-$(uname -m)" in - Linux-x86_64) ASSET=horizon-linux-x86_64.tar.gz ;; - Darwin-arm64) ASSET=horizon-macos-arm64.zip ;; + Linux-x86_64) ASSETS="horizon-server-linux-x86_64.tar.gz horizon-linux-x86_64.tar.gz" ;; + Darwin-arm64) ASSETS=horizon-macos-arm64.zip ;; *) die "no Horizon release for $(uname -s) $(uname -m) yet: build it from source (docs/server.md)" ;; esac -command -v curl >/dev/null || die "curl is needed" -if command -v sha256sum >/dev/null; then SHA="sha256sum" -elif command -v shasum >/dev/null; then SHA="shasum -a 256" -else die "sha256sum or shasum is needed"; fi - -if [ "$VERSION" = latest ]; then API="https://api.github.com/repos/$REPO/releases/latest" -else API="https://api.github.com/repos/$REPO/releases/tags/$VERSION"; fi -JSON=$(curl -fsSL -H "Accept: application/vnd.github+json" "$API") || die "no release $VERSION on GitHub" - -TAG=$(printf '%s\n' "$JSON" | sed -n 's/^ "tag_name": "\([^"]*\)".*/\1/p' | head -n 1) -[ -n "$TAG" ] || die "could not read the release's tag" -# The asset's download address and digest, from its block in the release's JSON. -FIELDS=$(printf '%s\n' "$JSON" | awk -v asset="$ASSET" ' - /"name": / { line = $0; sub(/.*"name": "/, "", line); sub(/".*/, "", line); current = line } - current == asset && /"digest": / { line = $0; sub(/.*"digest": "sha256:/, "", line); sub(/".*/, "", line); digest = line } - current == asset && /"browser_download_url": / { line = $0; sub(/.*"browser_download_url": "/, "", line); sub(/".*/, "", line); url = line } - END { print url; print digest }') -URL=$(printf '%s\n' "$FIELDS" | sed -n 1p) -DIGEST=$(printf '%s\n' "$FIELDS" | sed -n 2p) -[ -n "$URL" ] || die "release $TAG has no $ASSET" -[ -n "$DIGEST" ] || die "release $TAG publishes no SHA-256 for $ASSET" - TMP=$(mktemp -d) trap 'rm -rf "$TMP"' EXIT -echo "Downloading Horizon $TAG ($ASSET)..." -curl -fL --progress-bar -o "$TMP/$ASSET" "$URL" -GOT=$($SHA "$TMP/$ASSET" | cut -d' ' -f1) -[ "$GOT" = "$DIGEST" ] || die "$ASSET doesn't match its SHA-256 (got $GOT, expected $DIGEST): not installed" +if [ -n "$ARCHIVE" ]; then + # An archive already here (a build of your own, or one fetched by hand): checking it is up to you. + [ -f "$ARCHIVE" ] || die "no archive $ARCHIVE" + if [ "$VERSION" = latest ]; then TAG=local; else TAG=$VERSION; fi + ASSET=$(basename "$ARCHIVE") + cp "$ARCHIVE" "$TMP/$ASSET" +else + command -v curl >/dev/null || die "curl is needed" + if command -v sha256sum >/dev/null; then SHA="sha256sum" + elif command -v shasum >/dev/null; then SHA="shasum -a 256" + else die "sha256sum or shasum is needed"; fi + + if [ "$VERSION" = latest ]; then API="https://api.github.com/repos/$REPO/releases/latest" + else API="https://api.github.com/repos/$REPO/releases/tags/$VERSION"; fi + JSON=$(curl -fsSL -H "Accept: application/vnd.github+json" "$API") || die "no release $VERSION on GitHub" + + TAG=$(printf '%s\n' "$JSON" | sed -n 's/^ "tag_name": "\([^"]*\)".*/\1/p' | head -n 1) + [ -n "$TAG" ] || die "could not read the release's tag" + # The first asset the release has: its download address and digest, from its block in the release's JSON. + for ASSET in $ASSETS; do + FIELDS=$(printf '%s\n' "$JSON" | awk -v asset="$ASSET" ' + /"name": / { line = $0; sub(/.*"name": "/, "", line); sub(/".*/, "", line); current = line } + current == asset && /"digest": / { line = $0; sub(/.*"digest": "sha256:/, "", line); sub(/".*/, "", line); digest = line } + current == asset && /"browser_download_url": / { line = $0; sub(/.*"browser_download_url": "/, "", line); sub(/".*/, "", line); url = line } + END { print url; print digest }') + URL=$(printf '%s\n' "$FIELDS" | sed -n 1p) + DIGEST=$(printf '%s\n' "$FIELDS" | sed -n 2p) + [ -z "$URL" ] || break + done + [ -n "$URL" ] || die "release $TAG has none of $ASSETS" + [ -n "$DIGEST" ] || die "release $TAG publishes no SHA-256 for $ASSET" + + echo "Downloading Horizon $TAG ($ASSET)..." + curl -fL --progress-bar -o "$TMP/$ASSET" "$URL" + GOT=$($SHA "$TMP/$ASSET" | cut -d' ' -f1) + [ "$GOT" = "$DIGEST" ] || die "$ASSET doesn't match its SHA-256 (got $GOT, expected $DIGEST): not installed" +fi mkdir -p "$TMP/x" case "$ASSET" in @@ -86,8 +102,8 @@ for tool in horizon-server horizon-prepare; do done echo "Installed Horizon $TAG in $RELEASE; horizon-server and horizon-prepare are in $BIN." -# The game links ALSA and udev, even as a server; minimal Linux images often lack them. -if [ "$(uname -s)" = Linux ] && command -v ldd >/dev/null; then +# The full game (releases before the server build) links ALSA and udev; minimal Linux images often lack them. +if [ "$(uname -s)" = Linux ] && [ ! -e "$RELEASE/horizon-engine" ] && command -v ldd >/dev/null; then MISSING=$(ldd "$RELEASE/horizon-client" 2>/dev/null | awk '/not found/ { printf "%s ", $1 }') if [ -n "$MISSING" ]; then echo "The game needs libraries this system doesn't have: $MISSING" diff --git a/tools/check_imports.py b/tools/check_imports.py index 8cdabe45b..85afb541d 100644 --- a/tools/check_imports.py +++ b/tools/check_imports.py @@ -5,7 +5,8 @@ Windows .exe: every DLL in the import and delay-import tables must ship with Windows (no VCRUNTIME140.dll, vulkan-1.dll, ...): Vulkan and the like are loaded at run time, so a missing driver is an error message, not a dialog. Linux ELF: the loader must be the standard one (not a /nix/store path) and the -needed libraries the ones every desktop distribution has. +needed libraries the ones every desktop distribution has. `--server`: a server +build, which may not need ALSA or udev either (minimal server images lack them). """ import argparse @@ -30,6 +31,8 @@ LINUX_SYSTEM = { "libc.so.6", "libm.so.6", "libgcc_s.so.1", "libdl.so.2", "libpthread.so.0", "librt.so.1", "ld-linux-x86-64.so.2", "libasound.so.2", "libudev.so.1", } +# The game's sound (cpal) and controllers (gilrs): a desktop has them, a server image may not. +LINUX_DESKTOP_ONLY = {"libasound.so.2", "libudev.so.1"} def windows_allowed(name): @@ -111,7 +114,7 @@ def elf_needs(data): return interpreter, needed, runpath -def check(path): +def check(path, server=False): data = Path(path).read_bytes() problems = [] if data[:2] == b"MZ": @@ -123,6 +126,8 @@ def check(path): if interpreter not in (None, LINUX_LOADER): problems.append(f"loader is {interpreter}, not {LINUX_LOADER}") problems += [f"needs {n}, which not every distribution has" for n in needed if n not in LINUX_SYSTEM] + if server: + problems += [f"needs {n}, which a server may not have" for n in needed if n in LINUX_DESKTOP_ONLY] problems += [f"has a run path to {p}" for p in runpath if "/nix/store" in p] summary = f"{interpreter}; " + ", ".join(needed) print(f"{path}: {summary}") @@ -134,8 +139,10 @@ def check(path): def main(): parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("binaries", nargs="+") + parser.add_argument("--server", action="store_true", + help="a server build: not even ALSA or udev (the game's sound and controllers)") args = parser.parse_args() - ok = all([check(path) for path in args.binaries]) + ok = all([check(path, args.server) for path in args.binaries]) sys.exit(0 if ok else 1) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 05b84553b..28b591626 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -6,6 +6,10 @@ //! them to its own), and PLAY.txt. It holds none of Minecraft's files: players prepare those //! from their own copy. .github/workflows/release.yml makes the release folders with it. //! +//! `package-server ENGINE OUTDIR`: a server folder (docs/server.md): the server build of the game +//! (`cargo build --release -p horizon-client --no-default-features`, without sound or controllers) +//! as horizon-engine, with horizon-prepare and horizon-server from beside it and the same sources. +//! //! `tokens`: writes `website/src/tokens.css`, the design tokens of `crates/horizon-theme` as CSS //! custom properties (a horizon-theme test fails when the file is out of date). //! @@ -17,7 +21,7 @@ use std::fs; use std::path::{Component, Path, PathBuf}; use std::process::{Command, ExitCode}; -const USAGE: &str = "usage: cargo run -p xtask -- package BINARY OUTDIR | tokens | icons"; +const USAGE: &str = "usage: cargo run -p xtask -- package BINARY OUTDIR | package-server ENGINE OUTDIR | tokens | icons"; /// What the release folder takes from the repository at HEAD, as `git archive` writes it. const ARCHIVED: [&str; 5] = [ @@ -34,10 +38,16 @@ const ARCHIVED: [&str; 5] = [ /// (docs/server.md). const TOOLS: [&str; 3] = ["horizon-ctl", "horizon-prepare", "horizon-server"]; +/// The programs beside the engine in a server folder. +const SERVER_TOOLS: [&str; 2] = ["horizon-prepare", "horizon-server"]; + fn main() -> ExitCode { let args: Vec = std::env::args().skip(1).collect(); let result = match args.as_slice() { [task, binary, out] if task == "package" => package(Path::new(binary), Path::new(out)), + [task, engine, out] if task == "package-server" => { + package_server(Path::new(engine), Path::new(out)) + } [task] if task == "tokens" => tokens(), [task] if task == "icons" => icons(), _ => Err(USAGE.to_string()), @@ -209,6 +219,21 @@ fn icons() -> Result<(), String> { } fn package(binary: &Path, out: &Path) -> Result<(), String> { + let repo = new_folder(out)?; + copy_into(binary, out)?; + copy_beside(binary, &TOOLS, out)?; + let play = repo.join("packaging/PLAY.txt"); + copy(&play, &out.join("PLAY.txt")) +} + +fn package_server(engine: &Path, out: &Path) -> Result<(), String> { + new_folder(out)?; + copy(engine, &out.join(program_name(engine, "horizon-engine")))?; + copy_beside(engine, &SERVER_TOOLS, out) +} + +/// `OUT`, emptied, with the sources the release folders take (`ARCHIVED`); the repository's path. +fn new_folder(out: &Path) -> Result { let top = git( Path::new(env!("CARGO_MANIFEST_DIR")), &["rev-parse", "--show-toplevel"], @@ -224,15 +249,25 @@ fn package(binary: &Path, out: &Path) -> Result<(), String> { let mut args = vec!["archive", "--format=tar", "HEAD"]; args.extend(ARCHIVED); extract(&git(&repo, &args)?, out)?; - copy_into(binary, out)?; - let exe = binary.extension().is_some_and(|e| e == "exe"); - for tool in TOOLS { - let name = if exe { - format!("{tool}.exe") - } else { - tool.to_string() - }; - let path = binary.parent().unwrap_or(Path::new("")).join(name); + Ok(repo) +} + +/// `name`, with `.exe` when `program` has it. +fn program_name(program: &Path, name: &str) -> String { + if program.extension().is_some_and(|e| e == "exe") { + format!("{name}.exe") + } else { + name.to_string() + } +} + +/// Copies `tools`, built beside `program`, into `out`. +fn copy_beside(program: &Path, tools: &[&str], out: &Path) -> Result<(), String> { + for tool in tools { + let path = program + .parent() + .unwrap_or(Path::new("")) + .join(program_name(program, tool)); if !path.is_file() { return Err(format!( "{} is missing; build it too: cargo build --release -p horizon-client --bins -p horizon-prepare -p horizon-server", @@ -241,8 +276,7 @@ fn package(binary: &Path, out: &Path) -> Result<(), String> { } copy_into(&path, out)?; } - let play = repo.join("packaging/PLAY.txt"); - copy(&play, &out.join("PLAY.txt")) + Ok(()) } /// `cp FILE DIR/`. From d634870f1569cac382b38ceaebec541f13f24833 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 20:48:34 +0200 Subject: [PATCH 03/12] Structures: Repurposed's matter phase, piece origin and y value rule tests Repurposed Structures' mineshafts and Nether monument test blocks with three rule tests of their own. Vanilla, and the game, refuse a processor list naming a type they do not know, and with it every pool naming the list: the 5 Nether mineshaft start pools in a ported pack never load. The game now runs them under its own types, with the mod's fields: - horizon:matter_phase (MatterPhaseRuleTest): solid (not air, no fluid, canOcclude), liquid, air, and air or liquid with rails and chains, optionally inverted. Structure processors now bind the block properties to read canOcclude. - horizon:piece_origin_axis_aligned_linear_pos: vanilla's axis-aligned chance over the block's distance from its template's origin. - horizon:y_value: the block's world height in a range. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-world/src/block_properties.rs | 23 ++- .../src/worldgen/block_feature.rs | 3 + .../src/worldgen/fluid_predicate.rs | 3 + .../src/worldgen/generator_structures.rs | 10 +- .../horizon-world/src/worldgen/rule_test.rs | 89 ++++++++- .../src/worldgen/structure/processor.rs | 179 ++++++++++++++---- .../horizon-world/tests/rule_test_bounds.rs | 77 ++++++++ 7 files changed, 342 insertions(+), 42 deletions(-) diff --git a/crates/horizon-world/src/block_properties.rs b/crates/horizon-world/src/block_properties.rs index 7c0e17f61..98e06f7cb 100644 --- a/crates/horizon-world/src/block_properties.rs +++ b/crates/horizon-world/src/block_properties.rs @@ -79,15 +79,18 @@ impl BlockProperty { #[derive(Clone, Debug)] pub struct BoundBlockProperty { states: Arc<[u8]>, - property: BlockProperty, + mask: u8, } impl BoundBlockProperty { pub fn contains(&self, id: BlockStateId) -> bool { self.states .get(id.0 as usize) - .is_some_and(|flags| flags & self.property.mask() != 0) + .is_some_and(|flags| flags & self.mask != 0) } } +/// The per-state flag of the block definition's `canOcclude`, past the +/// [`BlockProperty`] bits. +const CAN_OCCLUDE: u8 = 16; /// Immutable FULL face-occlusion results, independent of support/collision. #[derive(Clone, Debug)] @@ -195,7 +198,12 @@ impl<'r> BlockPropertiesRegistry<'r> { .ok_or_else(|| invalid("missing block occlusion shape"))?, ) .map_err(|error| invalid(error.to_string()))?, - ) << 3), + ) << 3) + | if properties.can_occlude { + CAN_OCCLUDE + } else { + 0 + }, ); } Ok(Self { @@ -225,7 +233,14 @@ impl<'r> BlockPropertiesRegistry<'r> { pub fn bind(&self, property: BlockProperty) -> BoundBlockProperty { BoundBlockProperty { states: Arc::clone(&self.states), - property, + mask: property.mask(), + } + } + /// The states whose block definition can occlude (BlockState.canOcclude). + pub fn bind_can_occlude(&self) -> BoundBlockProperty { + BoundBlockProperty { + states: Arc::clone(&self.states), + mask: CAN_OCCLUDE, } } } diff --git a/crates/horizon-world/src/worldgen/block_feature.rs b/crates/horizon-world/src/worldgen/block_feature.rs index a901a583d..00f0c64c1 100644 --- a/crates/horizon-world/src/worldgen/block_feature.rs +++ b/crates/horizon-world/src/worldgen/block_feature.rs @@ -23,6 +23,7 @@ pub struct FeatureBlockBindings<'r> { )>, physical_properties: Option<[BoundBlockProperty; 4]>, pub(crate) occlusion_faces: Option, + pub(crate) can_occlude: Option, biomes: Option>, pub(crate) fluids: super::fluid_predicate::FluidPredicateBindings, pub(crate) codec: BlockStateJsonCodec<'r>, @@ -88,6 +89,7 @@ impl<'r> FeatureBlockBindings<'r> { properties.bind(BlockProperty::SolidRender), ]); self.occlusion_faces = Some(properties.bind_occlusion_faces()); + self.can_occlude = Some(properties.bind_can_occlude()); Ok(self) } pub fn with_biomes( @@ -156,6 +158,7 @@ impl<'r> FeatureBlockBindings<'r> { biomes: None, physical_properties: None, occlusion_faces: None, + can_occlude: None, fluids: super::fluid_predicate::FluidPredicateBindings::new(registry)?, blocks: blocks .into_iter() diff --git a/crates/horizon-world/src/worldgen/fluid_predicate.rs b/crates/horizon-world/src/worldgen/fluid_predicate.rs index d8bcb10ab..1394445c9 100644 --- a/crates/horizon-world/src/worldgen/fluid_predicate.rs +++ b/crates/horizon-world/src/worldgen/fluid_predicate.rs @@ -45,6 +45,9 @@ impl FluidPredicateBindings { self.tags = resolved; Ok(self) } + pub(crate) fn table(&self) -> &Arc { + &self.table + } pub fn state(&self, block: BlockStateId) -> Option { self.table.get(block) } diff --git a/crates/horizon-world/src/worldgen/generator_structures.rs b/crates/horizon-world/src/worldgen/generator_structures.rs index 3b6d058ae..11765ab57 100644 --- a/crates/horizon-world/src/worldgen/generator_structures.rs +++ b/crates/horizon-world/src/worldgen/generator_structures.rs @@ -195,11 +195,19 @@ impl StructureState { for (i, def) in registry.structures.iter().enumerate() { by_step[def.step].push(i); } - let bindings = FeatureBlockBindings::with_tags( + let mut bindings = FeatureBlockBindings::with_tags( data.registry(), data.integers().clone(), &data.block_tags, )?; + // Processors that test what a block is made of read canOcclude. + if let Some(blocks) = data + .feature_resources + .as_ref() + .and_then(|r| r.blocks.as_ref()) + { + bindings = bindings.with_block_properties(&blocks.properties)?; + } Ok(Some(Self { registry, resources: resources.clone(), diff --git a/crates/horizon-world/src/worldgen/rule_test.rs b/crates/horizon-world/src/worldgen/rule_test.rs index d9f836961..5202baa31 100644 --- a/crates/horizon-world/src/worldgen/rule_test.rs +++ b/crates/horizon-world/src/worldgen/rule_test.rs @@ -3,7 +3,11 @@ use super::{ DecodeError, Object, block_feature::FeatureBlockBindings, density::identifier, integer, number_text, required, }; -use crate::registry::BlockStateId; +use crate::{ + block_properties::BoundBlockProperty, + fluid::{FluidKind, FluidStateTable}, + registry::BlockStateId, +}; use horizon_core::random::RandomSource; use serde_json::value::RawValue; use std::{collections::BTreeSet, sync::Arc}; @@ -21,6 +25,85 @@ enum Node { RandomStates(Arc>, f32), RandomState(BlockStateId, f32), Height(i32, i32), + Phase(Box), +} +/// `horizon:matter_phase`: Repurposed Structures' MatterPhaseRuleTest, which +/// its mineshafts and Nether monument use to match a block by what it is +/// made of (`phase_to_test_for`), optionally inverted (`invert_condition`). +#[derive(Clone, Debug)] +struct MatterPhase { + phase: Phase, + invert: bool, + air: [BlockStateId; 3], + fluids: Arc, + /// Only for `SOLID`. + can_occlude: Option, + /// Blocks.RAIL and Blocks.CHAIN (26.3's iron chain). + rail_or_chain: BTreeSet, +} +#[derive(Clone, Copy, Debug)] +enum Phase { + Solid, + Liquid, + Air, + AirRailOrChain, + LiquidRailOrChain, +} +impl MatterPhase { + fn decode(object: &Object, bindings: &FeatureBlockBindings<'_>) -> Result { + let name: String = serde_json::from_str(required(object, "phase_to_test_for")?.get())?; + let phase = match name.as_str() { + "SOLID" => Phase::Solid, + "LIQUID" => Phase::Liquid, + "AIR" => Phase::Air, + "AIR_RAIL_OR_CHAIN" => Phase::AirRailOrChain, + "LIQUID_RAIL_OR_CHAIN" => Phase::LiquidRailOrChain, + _ => return Err(DecodeError::Field("matter phase")), + }; + let invert = match object.get("invert_condition") { + Some(v) => serde_json::from_str(v.get())?, + None => false, + }; + let can_occlude = match phase { + Phase::Solid => Some( + bindings + .can_occlude + .clone() + .ok_or(DecodeError::Field("matter phase block properties"))?, + ), + _ => None, + }; + let mut rail_or_chain = BTreeSet::new(); + for name in ["minecraft:rail", "minecraft:iron_chain"] { + rail_or_chain.extend(bindings.blocks.get(name).into_iter().flat_map(|s| s.iter())); + } + Ok(Self { + phase, + invert, + air: bindings.air, + fluids: bindings.fluids.table().clone(), + can_occlude, + rail_or_chain, + }) + } + fn test(&self, state: BlockStateId) -> bool { + let air = self.air.contains(&state); + let liquid = self + .fluids + .get(state) + .is_some_and(|f| f.kind() != FluidKind::Empty); + let rail_or_chain = || self.rail_or_chain.contains(&state); + let matched = match self.phase { + Phase::Solid => { + !air && !liquid && self.can_occlude.as_ref().is_some_and(|c| c.contains(state)) + } + Phase::Liquid => liquid, + Phase::Air => air, + Phase::AirRailOrChain => air || rail_or_chain(), + Phase::LiquidRailOrChain => liquid || rail_or_chain(), + }; + matched != self.invert + } } impl RuleTest { pub fn from_json(json: &str, bindings: &FeatureBlockBindings<'_>) -> Result { @@ -104,6 +187,9 @@ impl RuleTest { Node::Any(values) } } + "horizon:matter_phase" => { + Node::Phase(Box::new(MatterPhase::decode(&object, bindings)?)) + } _ => return Err(DecodeError::Field("replacement rule type")), }; Ok(Self(node)) @@ -128,6 +214,7 @@ impl RuleTest { state == *expected && random.next_float() < *probability } Node::Height(min, max) => *min <= position[1] && position[1] <= *max, + Node::Phase(phase) => phase.test(state), } } } diff --git a/crates/horizon-world/src/worldgen/structure/processor.rs b/crates/horizon-world/src/worldgen/structure/processor.rs index 76e1a4cd1..4001f25cb 100644 --- a/crates/horizon-world/src/worldgen/structure/processor.rs +++ b/crates/horizon-world/src/worldgen/structure/processor.rs @@ -49,6 +49,22 @@ enum PosRule { max_dist: i32, axis: Option, }, + /// `horizon:piece_origin_axis_aligned_linear_pos`: Repurposed Structures' + /// PieceOriginAxisAlignedLinearPosRuleTest, vanilla's axis-aligned chance + /// over the block's distance from the template's origin along one axis. + PieceOrigin { + min_chance: f32, + max_chance: f32, + min_dist: i32, + max_dist: i32, + axis: usize, + }, + /// `horizon:y_value`: Repurposed Structures' YValuePosRuleTest, the + /// block's world height in `min_y_value..=max_y_value`. + YValue { + min: i32, + max: i32, + }, } #[derive(Clone, Debug)] enum Modifier { @@ -83,9 +99,97 @@ impl PosRule { } }), }, + "horizon:piece_origin_axis_aligned_linear_pos" => { + let int = |key| p.get(key).and_then(Value::as_i64).unwrap_or(0) as i32; + let (min_dist, max_dist) = (int("min_dist"), int("max_dist")); + if min_dist >= max_dist { + return Err(DecodeError::Field("position predicate distance range")); + } + Self::PieceOrigin { + min_chance: p.get("min_chance").and_then(Value::as_f64).unwrap_or(0.) as f32, + max_chance: p.get("max_chance").and_then(Value::as_f64).unwrap_or(0.) as f32, + min_dist, + max_dist, + axis: match p.get("axis").map(|a| a.as_str()) { + Some(Some("x")) => 0, + None | Some(Some("y")) => 1, + Some(Some("z")) => 2, + _ => return Err(DecodeError::Field("position predicate axis")), + }, + } + } + "horizon:y_value" => { + let int = |key| { + p.get(key) + .and_then(Value::as_i64) + .map(|v| v as i32) + .ok_or(DecodeError::Field("position predicate y value")) + }; + let (min, max) = (int("min_y_value")?, int("max_y_value")?); + if min > max { + return Err(DecodeError::Field("position predicate y range")); + } + Self::YValue { min, max } + } _ => return Err(DecodeError::Field("position predicate")), }) } + /// PosRuleTest.test for a block at `world`, `relative` in its template, + /// in a piece placed at `reference`. + fn test( + &self, + relative: [i32; 3], + world: [i32; 3], + reference: [i32; 3], + random: &mut impl RandomSource, + ) -> bool { + match *self { + Self::Always => true, + Self::Linear { + min_chance, + max_chance, + min_dist, + max_dist, + axis, + } => { + let dist = match axis { + None => (0..3).map(|i| (world[i] - reference[i]).abs()).sum::(), + Some(a) => ((world[a] - reference[a]) as f32).abs() as i32, + }; + random.next_float() + <= clamped_chance(min_chance, max_chance, min_dist, max_dist, dist) + } + Self::PieceOrigin { + min_chance, + max_chance, + min_dist, + max_dist, + axis, + } => { + let dist = (relative[axis] as f32).abs() as i32; + random.next_float() + <= clamped_chance(min_chance, max_chance, min_dist, max_dist, dist) + } + Self::YValue { min, max } => (min..=max).contains(&world[1]), + } + } +} +/// Mth.clampedLerp(min_chance, max_chance, Mth.inverseLerp(dist, min_dist, max_dist)). +fn clamped_chance( + min_chance: f32, + max_chance: f32, + min_dist: i32, + max_dist: i32, + dist: i32, +) -> f32 { + let t = (dist - min_dist) as f32 / (max_dist - min_dist) as f32; + if t < 0. { + min_chance + } else if t > 1. { + max_chance + } else { + min_chance + t * (max_chance - min_chance) + } } impl Modifier { /// A rule's `block_entity_modifier` (passthrough when missing). @@ -589,42 +693,9 @@ fn process_block( if !rule.location.test(located, info.pos, &mut random) { continue; } - let position_ok = match &rule.position { - PosRule::Always => true, - PosRule::Linear { - min_chance, - max_chance, - min_dist, - max_dist, - axis, - } => { - let dist = match axis { - None => (0..3) - .map(|i| (info.pos[i] - reference[i]).abs()) - .sum::(), - Some(a) => { - let d: [f32; 3] = std::array::from_fn(|i| { - if i == *a { - ((info.pos[i] - reference[i]) as f32).abs() - } else { - 0. - } - }); - (d[0] + d[1] + d[2]) as i32 - } - }; - let roll = random.next_float(); - let t = (dist - min_dist) as f32 / (max_dist - min_dist) as f32; - let chance = if t < 0. { - *min_chance - } else if t > 1. { - *max_chance - } else { - min_chance + t * (max_chance - min_chance) - }; - roll <= chance - } - }; + let position_ok = rule + .position + .test(relative, info.pos, reference, &mut random); if !position_ok { continue; } @@ -1250,6 +1321,42 @@ mod tests { assert!(SpawnerMobs::decode(&bad, |_| Ok(BlockStateId(0))).is_err()); } + #[test] + fn repurposed_position_rules() { + let decode = |v: Value| PosRule::decode(Some(&v)); + // Over 3..4 blocks from the template's origin along y, 0 to 1. + let rule = decode( + json!({"predicate_type": "horizon:piece_origin_axis_aligned_linear_pos", + "axis": "y", "min_chance": 0, "max_chance": 1, "min_dist": 3, "max_dist": 4}), + ) + .unwrap(); + let mut random = LegacyRandom::new(1); + let world = [100, 64, -100]; + // At or under min_dist the chance is 0, a roll of 0 aside; past max_dist it is 1. + assert!(!rule.test([9, 2, 9], world, world, &mut random)); + assert!(rule.test([0, -5, 0], world, world, &mut random)); + // The world position and the piece's do not count. + assert!(rule.test([0, 4, 0], [0, 0, 0], [50, 50, 50], &mut random)); + // Repurposed refuses an empty distance range and an unknown axis. + for bad in [ + json!({"predicate_type": "horizon:piece_origin_axis_aligned_linear_pos"}), + json!({"predicate_type": "horizon:piece_origin_axis_aligned_linear_pos", + "min_dist": 0, "max_dist": 1, "axis": "w"}), + json!({"predicate_type": "horizon:y_value", "min_y_value": 5, "max_y_value": 4}), + json!({"predicate_type": "horizon:y_value", "min_y_value": 5}), + ] { + assert!(decode(bad.clone()).is_err(), "{bad}"); + } + let y = decode( + json!({"predicate_type": "horizon:y_value", "min_y_value": 0, "max_y_value": 31}), + ) + .unwrap(); + assert!(y.test([0; 3], [7, 31, 7], [0; 3], &mut random)); + assert!(y.test([0, 99, 0], [7, 0, 7], [0; 3], &mut random)); + assert!(!y.test([0; 3], [7, 32, 7], [0; 3], &mut random)); + assert!(!y.test([0; 3], [7, -1, 7], [0; 3], &mut random)); + } + #[test] fn rule_parts_read_ids_without_a_namespace() { // CTOV writes "always_true"; vanilla reads it as minecraft:always_true. diff --git a/crates/horizon-world/tests/rule_test_bounds.rs b/crates/horizon-world/tests/rule_test_bounds.rs index de4862f54..e3f614590 100644 --- a/crates/horizon-world/tests/rule_test_bounds.rs +++ b/crates/horizon-world/tests/rule_test_bounds.rs @@ -67,3 +67,80 @@ fn binding_rejects_unknown_tag_blocks_and_duplicate_canonical_tags() { ]); assert!(FeatureBlockBindings::with_tags(®istry, BTreeSet::new(), &tags).is_err()); } +#[test] +fn matter_phase_matches_what_a_block_is_made_of() { + // air, cave_air, void_air, stone, then water (level 0), rail, iron_chain. + let mut states: Vec = [ + "minecraft:air", + "minecraft:cave_air", + "minecraft:void_air", + "minecraft:stone", + ] + .iter() + .map(|name| Tag::compound([("id", Tag::String((*name).into()))])) + .collect(); + states.push(Tag::compound([ + ("id", Tag::String("minecraft:water".into())), + ( + "properties", + Tag::compound([("level", Tag::String("0".into()))]), + ), + ])); + for name in ["minecraft:rail", "minecraft:iron_chain"] { + states.push(Tag::compound([("id", Tag::String(name.into()))])); + } + let names = [ + "minecraft:air", + "minecraft:cave_air", + "minecraft:void_air", + "minecraft:stone", + "minecraft:water", + "minecraft:rail", + "minecraft:iron_chain", + ]; + let registry = BlockRegistry::from_nbt(&Tag::compound([ + ("DataVersion", Tag::Int(DATA_VERSION)), + ("states", Tag::List(states)), + ( + "defaults", + Tag::Compound( + names + .iter() + .enumerate() + .map(|(id, name)| ((*name).into(), Tag::Int(id as i32))) + .collect(), + ), + ), + ])) + .unwrap(); + let bindings = FeatureBlockBindings::new(®istry, BTreeSet::new()).unwrap(); + let mut random = horizon_core::random::LegacyRandom::new(0); + let matches = |phase: &str, invert: bool, random: &mut horizon_core::random::LegacyRandom| { + let json = format!( + r#"{{"predicate_type":"horizon:matter_phase","phase_to_test_for":"{phase}","invert_condition":{invert}}}"# + ); + let test = RuleTest::from_json(&json, &bindings).unwrap(); + (0..names.len() as u32) + .filter(|&id| test.test(horizon_world::registry::BlockStateId(id), [0; 3], random)) + .collect::>() + }; + assert_eq!(matches("AIR", false, &mut random), [0, 1, 2]); + assert_eq!(matches("LIQUID", false, &mut random), [4]); + assert_eq!( + matches("AIR_RAIL_OR_CHAIN", false, &mut random), + [0, 1, 2, 5, 6] + ); + assert_eq!( + matches("LIQUID_RAIL_OR_CHAIN", false, &mut random), + [4, 5, 6] + ); + assert_eq!(matches("AIR", true, &mut random), [3, 4, 5, 6]); + // The phase is the enum's name as written; SOLID needs canOcclude. + for json in [ + r#"{"predicate_type":"horizon:matter_phase","phase_to_test_for":"air"}"#, + r#"{"predicate_type":"horizon:matter_phase","phase_to_test_for":"SOLID"}"#, + r#"{"predicate_type":"horizon:matter_phase"}"#, + ] { + assert!(RuleTest::from_json(json, &bindings).is_err(), "{json}"); + } +} From 5fcea03fb8c8401d969d3afb294a6c94be90087a Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 20:49:23 +0200 Subject: [PATCH 04/12] Carry: Repurposed's rule tests as the game's, and the chain as the iron chain Two things kept Repurposed Structures' mineshaft processor lists from loading in 26.3, and with them the mod's 5 Nether mineshaft pools: - Its three rule tests written in Java. Carry renames them to the game's horizon:matter_phase, horizon:piece_origin_axis_aligned_linear_pos and horizon:y_value (213 in 75 lists), counted in the report's rule_tests_carried. - 1.21.1's minecraft:chain, which 26.3 calls minecraft:iron_chain (its datafixers rename it in worlds). Carry renames it in recipes, loot tables and worldgen files: 112 files in 5 of ATM10's mods. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-port-pack/src/carry.rs | 58 ++++++++++++- crates/horizon-port-pack/src/processors.rs | 97 ++++++++++++++++++++++ crates/horizon-port-pack/src/worldgen.rs | 6 +- docs/modpacks.md | 11 +++ 4 files changed, 169 insertions(+), 3 deletions(-) diff --git a/crates/horizon-port-pack/src/carry.rs b/crates/horizon-port-pack/src/carry.rs index b342f45fd..fcf9f3498 100644 --- a/crates/horizon-port-pack/src/carry.rs +++ b/crates/horizon-port-pack/src/carry.rs @@ -205,6 +205,29 @@ pub fn upgrade_recipe(recipe: &mut Value) -> bool { true } +/// Block and item ids vanilla renamed after 1.21.1, as its datafixers +/// rename them in worlds (DataVersion 4541: the chain became the iron chain +/// when copper chains came in). A data file naming the old id fails to load +/// in 26.3: a recipe, a loot entry, a processor's block match. +const RENAMED_IDS: [(&str, &str); 1] = [("minecraft:chain", "minecraft:iron_chain")]; + +/// Every string of a data file that is a renamed id ([`RENAMED_IDS`]) set to +/// the new one, in place; whether any was. +pub fn renamed_ids(v: &mut Value) -> bool { + match v { + Value::String(s) => match RENAMED_IDS.iter().find(|(old, _)| s == old) { + Some((_, new)) => { + *s = (*new).to_owned(); + true + } + None => false, + }, + Value::Array(a) => a.iter_mut().fold(false, |c, x| renamed_ids(x) | c), + Value::Object(o) => o.values_mut().fold(false, |c, x| renamed_ids(x) | c), + _ => false, + } +} + /// A 1.21.1 loot table in 26.3's shape, in place: `conditions` lists become /// one `condition` (`all_of` for several), `functions` become `modifier`, /// and conditions and functions name their kind in `type`. A tool match on @@ -410,12 +433,16 @@ pub fn carry( { if let Ok(mut recipe) = serde_json::from_slice::(&bytes) { tags_used(&recipe, &mut convention); - if upgrade_recipe(&mut recipe) { + let renamed = renamed_ids(&mut recipe); + let upgraded = upgrade_recipe(&mut recipe); + if upgraded { report.recipes_upgraded += 1; - bytes = serde_json::to_vec_pretty(&recipe).unwrap_or(bytes); } else { report.recipes_kept.push(file.to_owned()); } + if upgraded || renamed { + bytes = serde_json::to_vec_pretty(&recipe).unwrap_or(bytes); + } } } else if let Some(kind) = rel .strip_prefix("worldgen/") @@ -433,6 +460,10 @@ pub fn carry( &mut v, &processor_data, &mut types.processors_carried, + ) | crate::processors::rule_tests( + kind, + &mut v, + &mut types.rule_tests_carried, ); let mut changed = crate::worldgen::upgrade(kind, &mut v, types, &set_tags) || carried; @@ -463,6 +494,7 @@ pub fn carry( &processor_data, &mut types.processors_carried, ); + crate::processors::rule_tests(kind, &mut added, &mut types.rule_tests_carried); crate::worldgen::upgrade(kind, &mut added, types, &set_tags); let to = format!( "data/{id}/worldgen/{kind}/{}/{path}", @@ -487,6 +519,7 @@ pub fn carry( if let Ok(mut table) = serde_json::from_slice::(&bytes) { let before = table.clone(); upgrade_loot(&mut table); + renamed_ids(&mut table); if table != before { report.loot_tables_upgraded += 1; bytes = serde_json::to_vec_pretty(&table).unwrap_or(bytes); @@ -809,6 +842,27 @@ mod tests { assert_eq!(new, glass); } + #[test] + fn renames_ids_vanilla_renamed() { + let mut recipe = json!({"type": "minecraft:crafting_shaped", "pattern": ["c"], + "key": {"c": {"item": "minecraft:chain"}}, "result": {"id": "m:sign", "count": 1}}); + assert!(renamed_ids(&mut recipe)); + assert_eq!(recipe["key"]["c"]["item"], "minecraft:iron_chain"); + let mut rule = json!({"input_predicate": {"predicate_type": "minecraft:block_match", + "block": "minecraft:chain"}, "output_state": {"id": "minecraft:chain", + "properties": {"axis": "y"}}}); + assert!(renamed_ids(&mut rule)); + assert_eq!(rule["input_predicate"]["block"], "minecraft:iron_chain"); + assert_eq!(rule["output_state"]["id"], "minecraft:iron_chain"); + // Only whole ids: other blocks and mods' own chains stay. + let mut other = json!([ + "minecraft:chain_command_block", + "m:chain", + "minecraft:iron_chain" + ]); + assert!(!renamed_ids(&mut other)); + } + #[test] fn templates_from_blockstate_properties() { let door = diff --git a/crates/horizon-port-pack/src/processors.rs b/crates/horizon-port-pack/src/processors.rs index 7fcc0fd94..6e22133df 100644 --- a/crates/horizon-port-pack/src/processors.rs +++ b/crates/horizon-port-pack/src/processors.rs @@ -150,6 +150,49 @@ pub fn carry( }) } +/// Repurposed Structures' rule tests and the game's types that run them +/// (`horizon_world::worldgen`'s `rule_test` and `structure::processor`). +const RULE_TESTS: [(&str, &str); 3] = [ + ( + "repurposed_structures:matter_phase_rule_test", + "horizon:matter_phase", + ), + ( + "repurposed_structures:piece_origin_axis_aligned_linear_pos_rule_test", + "horizon:piece_origin_axis_aligned_linear_pos", + ), + ( + "repurposed_structures:y_value_pos_rule_test", + "horizon:y_value", + ), +]; + +/// Renames the rule tests written in Java that the game runs under its own +/// type ([`RULE_TESTS`]) in a worldgen file's processor lists; each is +/// counted by type in `carried`. Their fields are the mod's. Whether +/// anything changed. Without this the game, like vanilla, refuses the list +/// and every pool naming it: Repurposed's mineshafts and Nether monument. +pub fn rule_tests(kind: &str, v: &mut Value, carried: &mut BTreeMap) -> bool { + fn rename(v: &mut Value, carried: &mut BTreeMap) -> bool { + match v { + Value::Object(o) => { + let mut changed = false; + if let Some(Value::String(t)) = o.get_mut("predicate_type") + && let Some((old, new)) = RULE_TESTS.iter().find(|(old, _)| t == old) + { + *carried.entry((*old).to_owned()).or_default() += 1; + *t = (*new).to_owned(); + changed = true; + } + o.values_mut().fold(changed, |c, x| rename(x, carried) | c) + } + Value::Array(a) => a.iter_mut().fold(false, |c, x| rename(x, carried) | c), + _ => false, + } + } + each_list(kind, v, &mut |list| rename(list, carried)) +} + /// The data form of a Java processor: the processors to put in its place /// (none for a no-op), or `None` when it has none. fn as_data(kind: &str, p: &Value, data: &ProcessorData) -> Option> { @@ -502,4 +545,58 @@ mod tests { assert_eq!(p[3]["processor_type"], "betterdungeons:candle_processor"); assert_eq!(carried.values().sum::(), 3); } + + #[test] + fn repurposed_rule_tests_become_the_games() { + let rule = |input: Value, position: Value| { + json!({"input_predicate": input, "location_predicate": {"predicate_type": + "repurposed_structures:matter_phase_rule_test", "phase_to_test_for": "AIR"}, + "position_predicate": position, "output_state": {"id": "minecraft:lava"}}) + }; + let mut pool = json!({"elements": [{"weight": 1, "element": { + "element_type": "minecraft:single_pool_element", "processors": {"processors": [ + {"processor_type": "minecraft:rule", "rules": [rule( + json!({"predicate_type": "minecraft:always_true"}), + json!({"predicate_type": "repurposed_structures:y_value_pos_rule_test", + "min_y_value": 0, "max_y_value": 31}))]}, + {"processor_type": "minecraft:capped", "limit": 2, "delegate": { + "processor_type": "minecraft:rule", "rules": [rule( + json!({"predicate_type": "minecraft:block_match", "block": "minecraft:rail"}), + json!({"predicate_type": + "repurposed_structures:piece_origin_axis_aligned_linear_pos_rule_test", + "axis": "y", "min_dist": 0, "max_dist": 1}))]}}]}}}]}); + let mut carried = BTreeMap::new(); + assert!(rule_tests("template_pool", &mut pool, &mut carried)); + let list = &pool["elements"][0]["element"]["processors"]["processors"]; + let first = &list[0]["rules"][0]; + assert_eq!( + first["location_predicate"]["predicate_type"], + "horizon:matter_phase" + ); + assert_eq!(first["location_predicate"]["phase_to_test_for"], "AIR"); + assert_eq!( + first["position_predicate"]["predicate_type"], + "horizon:y_value" + ); + assert_eq!(first["position_predicate"]["max_y_value"], 31); + let delegated = &list[1]["delegate"]["rules"][0]; + assert_eq!( + delegated["position_predicate"]["predicate_type"], + "horizon:piece_origin_axis_aligned_linear_pos" + ); + assert_eq!( + delegated["input_predicate"]["predicate_type"], + "minecraft:block_match" + ); + assert_eq!(carried.values().sum::(), 4); + assert_eq!(carried["repurposed_structures:matter_phase_rule_test"], 2); + // A file holding none is left as it is. + assert!(!rule_tests("template_pool", &mut pool, &mut carried)); + assert!(!rule_tests( + "structure", + &mut json!({"predicate_type": + "repurposed_structures:y_value_pos_rule_test"}), + &mut carried + )); + } } diff --git a/crates/horizon-port-pack/src/worldgen.rs b/crates/horizon-port-pack/src/worldgen.rs index 7de9f39ce..507819566 100644 --- a/crates/horizon-port-pack/src/worldgen.rs +++ b/crates/horizon-port-pack/src/worldgen.rs @@ -131,6 +131,9 @@ pub struct Upgrade { /// Processors written in Java carried as data the game runs /// ([`crate::processors`]), by type. pub processors_carried: BTreeMap, + /// Rule tests written in Java carried as the game's own + /// ([`crate::processors::rule_tests`]), by type. + pub rule_tests_carried: BTreeMap, /// Entries added to other structure sets and template pools /// (`horizon:add_to`), by the set or pool they join. pub added_to: BTreeMap, @@ -345,10 +348,11 @@ pub fn upgrade( ) -> bool { let states = block_states(v); report.block_states += usize::from(states); + let renamed = crate::carry::renamed_ids(v); let dropped = crate::processors::each_list(kind, v, &mut |list| { drop_java_processors(list, &mut report.processors_dropped) }); - upgrade_types(kind, v, report, set_tags) || states || dropped + upgrade_types(kind, v, report, set_tags) || states || dropped || renamed } /// Takes the processors written in Java (any type outside `minecraft:` and diff --git a/docs/modpacks.md b/docs/modpacks.md index 12e522e24..0e8a0121f 100644 --- a/docs/modpacks.md +++ b/docs/modpacks.md @@ -455,6 +455,17 @@ Two cases the oracle fills in: Moog's were empty (763 spawners with a mob, up from 628); in the Nether the 52 empty ones in Moog's Nether Structures are filled (162, up from 110). Chests are unchanged: 2,777 and 1,405 with a loot table. + - **Rule tests** written in Java. Repurposed Structures' mineshafts and Nether monument test + blocks with three of the mod's own rule tests inside vanilla `rule` processors: what a + block is made of (solid, liquid, air, with rails and chains), a chance over the distance + from the template's origin along one axis, and a world height range. The game runs them + as `horizon:matter_phase`, `horizon:piece_origin_axis_aligned_linear_pos` and + `horizon:y_value` with the mod's fields, and carry renames them (213 in 75 lists). + - **Renamed ids.** 26.3 calls 1.21.1's chain the iron chain (its datafixers rename it in + worlds). Carry renames it in recipes, loot tables and worldgen files (112 files in 5 mods: + Repurposed's mineshafts, Better Dungeons' processors, Regions Unexplored's hanging sign + recipes...), which otherwise fail to load in 26.3. With both, the 5 Nether mineshaft pools load: 47 of + Repurposed's Nether mineshafts in the 128×128-chunk square (seed 1234), under the lava sea. - **Big templates** (#838). Vanilla reads structure templates with no limit on their tag count; Horizon now does too (`nbt::Limits::templates`). Seven of ATM10's 8,962 carried templates are past the old 1,000,000-tag cap: YUNG's monument start, four When Dungeons From f1d5bf520986b00a5b968a84d85809fca1a039fa Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 20:57:07 +0200 Subject: [PATCH 05/12] server_bench: compare each bot count's work across servers, and flag runs that aren't equal Co-Authored-By: Claude Opus 5.5 --- tests/test_server_bench.py | 32 +++++++++++++++++++++++++ tools/server_bench.py | 48 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 tests/test_server_bench.py diff --git a/tests/test_server_bench.py b/tests/test_server_bench.py new file mode 100644 index 000000000..f392297bc --- /dev/null +++ b/tests/test_server_bench.py @@ -0,0 +1,32 @@ +import unittest + +from tools.server_bench import work + + +def run(server, bots, sent, new, waited=0.0, behaviour="mixed"): + return {"server": server, "behaviour": behaviour, "bots": bots, "chunks_per_second": sent, + "unique_per_second": new, "waited_ticks_per_second": waited} + + +class ServerBenchWorkTests(unittest.TestCase): + def test_shares_of_the_busiest_server(self): + [row] = work([run("horizon", 10, 98.2, 36.5), run("vanilla", 10, 24.3, 19.1, 12.0)]) + self.assertFalse(row["equal"]) + self.assertEqual(row["servers"]["horizon"]["chunks_per_second"], {"value": 98.2, "share": 1.0}) + self.assertEqual(row["servers"]["vanilla"]["chunks_per_second"]["share"], 0.247) + self.assertEqual(row["servers"]["vanilla"]["waited_ticks_per_second"], 12.0) + + def test_equal_within_ten_percent_or_a_chunk(self): + [row] = work([run("horizon", 25, 236, 28.9), run("vanilla", 25, 252, 28.9)]) + self.assertTrue(row["equal"]) + # One bot: 0.3 against 1.1 new chunks a second is within a chunk. + [row] = work([run("horizon", 1, 9.9, 0.3), run("vanilla", 1, 9.9, 1.1)]) + self.assertTrue(row["equal"]) + + def test_only_counts_run_on_several_servers(self): + failed = dict(run("vanilla", 50, 0, 0), error="watchdog") + self.assertEqual(work([run("horizon", 50, 450, 10), failed, run("horizon", 1, 9, 0.3)]), []) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/server_bench.py b/tools/server_bench.py index c79d6aab1..0ba99617a 100755 --- a/tools/server_bench.py +++ b/tools/server_bench.py @@ -19,7 +19,9 @@ The bots can run on another machine, so they don't take the server's CPU: `--bot `--bots-bin` naming horizon-bots there and `--connect` the address it reaches this machine at. `--nice 19` runs every server at the lowest priority (a shared machine). The explore bots fly as fast as each server sends them chunks, so a faster server gets more terrain to generate; `--explore-speed` slows them to a rate -both servers keep up with, for equal work. +both servers keep up with, for equal work. After the runs, the work table compares the servers at each bot +count: chunks sent and new chunks generated a second, against the busiest server's. A run whose work is more +than 10 % apart isn't a fair comparison of its tick times. The vanilla side runs the pinned server jar from tools/lab.py prepare (your own copy). It needs Java 25. The Horizon side runs target/release/horizon-server with the engine beside it; build both first: @@ -306,6 +308,38 @@ def run_once(kind, bots, behaviour, args, out): return result +# How far apart two servers' chunks a second (sent, and new) may be and still count as the same work: 10 %, +# or a chunk a second when there are few. +EQUAL_WORK = 0.10 + + +def work(runs): + """Each pass and bot count that ran on more than one server: every server's chunks sent and new chunks a + second as a share of the busiest one's, the ticks a second its bots stood waiting for their own chunk, + and whether the work was equal (within EQUAL_WORK on both).""" + groups = {} + for r in runs: + if 'error' not in r: + groups.setdefault((r['behaviour'], r['bots']), []).append(r) + rows = [] + for (behaviour, bots), group in groups.items(): + if len(group) < 2: + continue + row = {'behaviour': behaviour, 'bots': bots, 'servers': {}, 'equal': True} + for key in ('chunks_per_second', 'unique_per_second'): + top = max(r.get(key) or 0 for r in group) + for r in group: + value = r.get(key) or 0 + share = round(value / top, 3) if top else 1.0 + row['servers'].setdefault(r['server'], {})[key] = {'value': value, 'share': share} + if top - value > max(EQUAL_WORK * top, 1.0): + row['equal'] = False + for r in group: + row['servers'][r['server']]['waited_ticks_per_second'] = r.get('waited_ticks_per_second') + rows.append(row) + return rows + + def main(): parser = argparse.ArgumentParser(description=__doc__.split('\n\n')[0]) parser.add_argument('--servers', default='horizon,vanilla') @@ -346,6 +380,7 @@ def main(): 'java': subprocess.run(['java', '--version'], capture_output=True, text=True).stdout.splitlines()[:1], 'settings': {**vars(args), 'out': str(args.out), 'properties': PROPERTIES}, 'runs': runs, + 'work': work(runs), } path = args.out or out / 'report.json' path.parent.mkdir(parents=True, exist_ok=True) @@ -356,11 +391,20 @@ def main(): if 'error' in r: print(f'{r["server"]:8} {r["behaviour"]:8} {r["bots"]:>4} failed: {r["error"].splitlines()[0]}') continue - per_bot = r['chunks_per_bot'].get(r['behaviour']) + # A mixed pass: every bot's. + per_bot = r['chunks_per_bot'].get(r['behaviour']) or round((r['chunks_total'] or 0) / r['bots']) print(f'{r["server"]:8} {r["behaviour"]:8} {r["bots"]:>4} {r["start_seconds"]:>8} {r["mspt"]["average"]!s:>6} ' f'{r["mspt"]["p95"]!s:>6} {r["mspt"]["p99"]!s:>6} {r["rss_mb"]["mean"]!s:>7} {r["cpu_percent"]!s:>6} ' f'{r["chunks_per_second"]!s:>8} {per_bot!s:>6} {r["resent"]!s:>6} {r["pulled_back"]!s:>6} ' f'{r["bots_joined"]!s:>6}') + if report['work']: + print(f'\nWork, against the busiest server (chunks a second sent, new; ticks a second bots waited):') + for row in report['work']: + cells = ' '.join( + f'{server} {w["chunks_per_second"]["value"]} ({w["chunks_per_second"]["share"]:.0%}), ' + f'{w["unique_per_second"]["value"]} ({w["unique_per_second"]["share"]:.0%}), ' + f'{w["waited_ticks_per_second"]}' for server, w in row['servers'].items()) + print(f'{row["behaviour"]:8} {row["bots"]:>4} {"equal" if row["equal"] else "NOT EQUAL"} {cells}') print(f'\nWrote {path}') From 82c8de262f3d10f16c0126c78f55a986f59b1428 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 20:55:14 +0200 Subject: [PATCH 06/12] Repurposed's mineshafts: laid out under the terrain, carried as jigsaws Repurposed Structures' 10 overworld mineshafts (birch, desert, icy, jungle, ocean...) are its generic jigsaw with one change in MineshaftStructure.postLayoutAdjustments: the layout is not centred on the chunk; its top goes 15 blocks under the lowest ocean floor at the chunk's middle and 16 blocks to each side (taking one off after each side, as the mod does), never up, within y_allowance. - Game: "layout": "mineshaft" in horizon:piece_limited runs that step. - Carry: repurposed_structures:generic_mineshaft becomes minecraft:jigsaw with the piece-limited layout and that key, as its generic jigsaw does. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-port-pack/src/worldgen.rs | 21 ++++- .../src/worldgen/structure/piece_limited.rs | 77 ++++++++++++++++++- 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/crates/horizon-port-pack/src/worldgen.rs b/crates/horizon-port-pack/src/worldgen.rs index 7de9f39ce..ce5dc23fe 100644 --- a/crates/horizon-port-pack/src/worldgen.rs +++ b/crates/horizon-port-pack/src/worldgen.rs @@ -42,6 +42,9 @@ const REPURPOSED_CHECKED: [(&str, &str); 3] = [ ), ("repurposed_structures:stronghold_end", "stronghold_end"), ]; +/// Repurposed Structures' mineshaft: its generic jigsaw laid out under the +/// terrain (`"layout": "mineshaft"` in `horizon:piece_limited`). +const REPURPOSED_MINESHAFT: &str = "repurposed_structures:generic_mineshaft"; /// Layout rules the piece-limited assembler keeps (`horizon:piece_limited`). const PIECE_LIMITED_RULES: [&str; 4] = [ "y_allowance", @@ -421,8 +424,10 @@ fn upgrade_types( Some(t) if JIGSAWS.contains(&t) || NETHER_JIGSAWS.contains(&t) + || t == REPURPOSED_MINESHAFT || REPURPOSED_CHECKED.iter().any(|(r, _)| *r == t) => { + let mineshaft = t == REPURPOSED_MINESHAFT; let check = REPURPOSED_CHECKED.iter().find(|(r, _)| *r == t); let check = check.map(|(_, c)| *c); let library = match t.split_once(':').map(|(ns, _)| ns) { @@ -435,7 +440,10 @@ fn upgrade_types( } else { "lowest" }; - let limited = library.map(|l| piece_limited(v, l)); + let mut limited = library.map(|l| piece_limited(v, l)); + if let Some(limited) = limited.as_mut().filter(|_| mineshaft) { + limited["layout"] = "mineshaft".into(); + } start_checks(v, liquid, lost, limited.is_some()); if let Some(check) = check { if !v["horizon:start_checks"].is_object() { @@ -1345,6 +1353,17 @@ mod tests { moogs["horizon:piece_limited"], json!({"library": "moogs", "burying": "average_land"}) ); + // Repurposed's mineshaft is laid out under the terrain. + let mut mineshaft = json!({"type": "repurposed_structures:generic_mineshaft", + "start_pool": "rs:mineshafts/birch/start_pool", "size": 9, + "liquid_settings": "ignore_waterlogging"}); + assert!(upgrade("structure", &mut mineshaft, &mut report, &NO_TAGS)); + assert_eq!(mineshaft["type"], "minecraft:jigsaw"); + assert_eq!( + mineshaft["horizon:piece_limited"], + json!({"library": "repurposed", "layout": "mineshaft"}) + ); + assert_eq!(mineshaft["liquid_settings"], "ignore_waterlogging"); // Lithostitched's jigsaw is vanilla's: no key. let mut litho = json!({"type": "lithostitched:jigsaw"}); assert!(upgrade("structure", &mut litho, &mut report, &NO_TAGS)); diff --git a/crates/horizon-world/src/worldgen/structure/piece_limited.rs b/crates/horizon-world/src/worldgen/structure/piece_limited.rs index 1372f247b..753df0258 100644 --- a/crates/horizon-world/src/worldgen/structure/piece_limited.rs +++ b/crates/horizon-world/src/worldgen/structure/piece_limited.rs @@ -13,7 +13,8 @@ //! "ignore_bounds_pools": ["x:pool"], //! "land_search": "highest_land", // the Nether variants: or "lowest_land", "fixed_height" //! "ledge_offset_y": 2, -//! "pieces": [{"name": "x:piece", "required": 1, "min_depth": 3, "max": 2}] +//! "pieces": [{"name": "x:piece", "required": 1, "min_depth": 3, "max": 2}], +//! "layout": "mineshaft" // Repurposed's mineshafts: moved under the terrain //! } //! ``` //! @@ -25,6 +26,8 @@ //! (`required`, from `min_depth`), the whole layout retried up to 40 times //! until every required piece is in, and after it the pieces centred on the //! chunk corner, then buried in the terrain or moved onto Nether land. +//! Repurposed's overworld mineshafts are not centred: their top goes 15 +//! blocks under the terrain around the chunk. use super::{ BoundingBox, Rotation, StructureResources, StructureTemplate, jigsaw::{ @@ -63,6 +66,15 @@ pub enum Burying { LowestSide, } +/// The structure type's postLayoutAdjustments. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Layout { + /// GenericJigsawStructure's: centred, then buried or moved onto land. + Generic, + /// Repurposed Structures' MineshaftStructure. + Mineshaft, +} + #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum LandSearch { Highest, @@ -89,6 +101,7 @@ pub struct Config { ignore_bounds: HashSet, land_search: Option, ledge: i32, + layout: Layout, /// Counted templates by id, in the order of Java's HashMap of the /// required pieces (the first one still short is forced in first). pieces: Vec<(Identifier, Counted)>, @@ -152,6 +165,10 @@ impl Config { _ => None, }, ledge: int("ledge_offset_y").unwrap_or(0), + layout: match v["layout"].as_str() { + Some("mineshaft") => Layout::Mineshaft, + _ => Layout::Generic, + }, pieces: sorted, } } @@ -353,6 +370,10 @@ fn adjust( if pieces.is_empty() { return; } + if limits.layout == Layout::Mineshaft { + below_terrain(limits, context, start, pieces); + return; + } // GeneralUtils.centerAllPieces on the chunk's corner. let center = pieces[0].bounding_box.center(); let d = [start[0] - center[0], 0, start[2] - center[2]]; @@ -435,6 +456,44 @@ fn adjust( shift_all(limits, pieces, [0, dy, 0]); } +/// MineshaftStructure.postLayoutAdjustments: the layout's top moved to 15 +/// blocks under the lowest ocean floor at the chunk's middle and 16 blocks +/// to each side, within y_allowance; never up. +fn below_terrain( + limits: &Config, + context: &mut impl StartContext, + start: [i32; 3], + pieces: &mut [PoolPiece], +) { + let target = mineshaft_top(start, limits.min_y, |x, z| { + context.base_height(x, z, HeightmapType::OceanFloorWg) + }); + let top = pieces + .iter() + .map(|p| p.bounding_box.max[1]) + .max() + .unwrap_or(i32::MIN); + if target < limits.max_y.unwrap_or(i32::MAX) && target < top { + shift_all(limits, pieces, [0, target - top, 0]); + } +} + +/// Where [`below_terrain`] puts a mineshaft's top: `floor` is the first free +/// height over the ocean floor (getFirstFreeHeight, OCEAN_FLOOR_WG). +fn mineshaft_top( + start: [i32; 3], + min_y: Option, + mut floor: impl FnMut(i32, i32) -> i32, +) -> i32 { + let (x, z) = (start[0] + 7, start[2] + 7); + let mut terrain = floor(x, z) - 1; + // Plane.HORIZONTAL's order; the mod takes one more off after each. + for (dx, dz) in [(0, -16), (16, 0), (0, 16), (-16, 0)] { + terrain = terrain.min(floor(x + dx, z + dz)) - 1; + } + (terrain - 15).max(min_y.unwrap_or(i32::MIN)) +} + /// AVERAGE_LAND: the mean land height over a grid on the first piece's /// footprint; no structure when none of the heights is allowed. fn average_land( @@ -918,6 +977,22 @@ mod tests { assert_eq!(limits.library, Library::Repurposed); } + #[test] + fn mineshafts_go_under_the_lowest_floor_around() { + // Floor 80 at the middle (7, 7), 70 north, 90 east, 60 south, 85 west. + let floor = |x, z| match (x, z) { + (7, 7) => 80, + (7, -9) => 70, + (23, 7) => 90, + (7, 23) => 60, + (-9, 7) => 85, + other => panic!("{other:?}"), + }; + // 79, min(79, 70) - 1 = 69, 68, min(68, 60) - 1 = 59, 58; 15 under it. + assert_eq!(mineshaft_top([0, 50, 0], None, floor), 43); + assert_eq!(mineshaft_top([0, 50, 0], Some(48), floor), 48); + } + #[test] fn nether_land_is_found_in_the_column() { // A column from y 0: netherrack to 40, lava 41..=45, air, a ledge 70..=72. From e3909f4370f3628dc8b821690a2a043257b9344f Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 21:13:25 +0200 Subject: [PATCH 07/12] Carry: Better Dungeons' small Nether dungeon, as YUNG's jigsaw betterdungeons:small_nether_dungeon is YUNG's jigsaw structure (the same fields and YungJigsawManager assembly) behind the mod's smallNetherDungeons config switch, which ships on. Carry maps it as it maps yungsapi:yung_jigsaw: minecraft:jigsaw with horizon:yungsapi. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-port-pack/src/worldgen.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/horizon-port-pack/src/worldgen.rs b/crates/horizon-port-pack/src/worldgen.rs index 7de9f39ce..9ab2f7f2d 100644 --- a/crates/horizon-port-pack/src/worldgen.rs +++ b/crates/horizon-port-pack/src/worldgen.rs @@ -414,7 +414,9 @@ fn upgrade_types( let mut unsupported = |t: &str| *report.unsupported.entry(t.to_owned()).or_default() += 1; let changed = match kind { "structure" => match v["type"].as_str() { - Some("yungsapi:yung_jigsaw") => { + // Better Dungeons' small Nether dungeon is YUNG's jigsaw behind a + // config switch that ships on. + Some("yungsapi:yung_jigsaw" | "betterdungeons:small_nether_dungeon") => { yung_structure(v, lost); true } @@ -1182,6 +1184,19 @@ mod tests { for gone in ["max_y", "x_offset_in_chunk", "enhanced_terrain_adaptation"] { assert!(s.get(gone).is_none(), "{gone}"); } + // Better Dungeons' small Nether dungeon is the same jigsaw. + let mut nether = json!({"type": "betterdungeons:small_nether_dungeon", + "start_pool": "betterdungeons:small_nether_dungeon", "size": 10, + "start_jigsaw_name": "betterdungeons:small_nether_dungeon_anchor", + "max_distance_from_center": 80, "liquid_settings": "ignore_waterlogging"}); + assert!(upgrade("structure", &mut nether, &mut report, &NO_TAGS)); + assert_eq!(nether["type"], "minecraft:jigsaw"); + assert_eq!(nether["horizon:yungsapi"], json!({})); + assert_eq!( + nether["start_jigsaw_name"], + "betterdungeons:small_nether_dungeon_anchor" + ); + assert!(report.unsupported.is_empty()); let mut pool = json!({"elements": [{"weight": 3, "element": { "element_type": "yungsapi:yung_single_element", "location": "y:room", "processors": "minecraft:empty", "projection": "rigid", From 1e800099e96a1a8f288e8e9a99bd6178810a2cab Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 21:18:34 +0200 Subject: [PATCH 08/12] Carry: vertical_distance_from_start_piece is a dead field, not a lost rule Repurposed Structures' jungle fortress and End stronghold still write vertical_distance_from_start_piece, but no codec of the mod (or of Moog's Structure Lib) reads it any more: its own structures ignore it. Carry drops it without counting it in rules_lost, which now lists only rules the mods do run. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-port-pack/src/worldgen.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/horizon-port-pack/src/worldgen.rs b/crates/horizon-port-pack/src/worldgen.rs index 7de9f39ce..a10503164 100644 --- a/crates/horizon-port-pack/src/worldgen.rs +++ b/crates/horizon-port-pack/src/worldgen.rs @@ -94,14 +94,17 @@ const START_RULES: [(&str, &str); 3] = [ ]; /// The jigsaws' rules on laying out pieces, which vanilla's jigsaw has no /// place for. -const LAYOUT_RULES: [&str; 6] = [ +const LAYOUT_RULES: [&str; 5] = [ "y_allowance", "burying_type", "use_bounding_box_hack", "pools_that_ignore_boundaries", "enhanced_terrain_adaptation", - "vertical_distance_from_start_piece", ]; +/// Fields of older versions the mods' codecs no longer read: their own +/// structures ignore them, and so does carry. Repurposed's jungle fortress +/// and End stronghold still write `vertical_distance_from_start_piece`. +const DEAD_FIELDS: [&str; 1] = ["vertical_distance_from_start_piece"]; /// Pool elements that are vanilla's with extra fields: (mod type, vanilla type). const ELEMENTS: [(&str, &str); 3] = [ ( @@ -646,6 +649,9 @@ fn start_checks(v: &mut Value, liquid: &str, lost: &mut BTreeMap, checks.insert(to.into(), n.clone()); } } + for field in DEAD_FIELDS { + o.remove(field); + } for rule in LAYOUT_RULES { let kept = limited && PIECE_LIMITED_RULES.contains(&rule); if o.remove(rule).is_some_and(|r| r != Value::Bool(false)) && !kept { @@ -1101,11 +1107,13 @@ mod tests { assert!(moogs.get(gone).is_none(), "{gone}"); } assert_eq!(moogs["size"], 7); - // Repurposed Structures reads the block above the bottom. + // Repurposed Structures reads the block above the bottom. Its jungle + // fortress's vertical distance is a field its code no longer reads. let mut rs = json!({"type": "repurposed_structures:generic_jigsaw_structure", - "cannot_spawn_in_liquid": true}); + "cannot_spawn_in_liquid": true, "vertical_distance_from_start_piece": 35}); assert!(upgrade("structure", &mut rs, &mut report, &NO_TAGS)); assert_eq!(rs["horizon:start_checks"], json!({"liquid": "lowest"})); + assert!(rs.get("vertical_distance_from_start_piece").is_none()); // Repurposed's own checks: the Nether city, Nether shipwrecks, the End stronghold. for (t, check) in [ ("city_nether_structure", "city_nether"), From 4b18b79d1586ad91389cb0e0fa21271dce81929d Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 21:46:09 +0200 Subject: [PATCH 09/12] vanilla_client.py on macOS: the real vanilla 26.3 client joins Horizon's server The tool picks this machine's natives (macOS arm64 or Linux), adds -XstartOnFirstThread on macOS and turns vsync off there: with it on, an unseen window (a locked session) blocks in SDL_GL_SwapWindow before the client joins. docs/server.md records the join. Co-Authored-By: Claude Opus 5.5 --- docs/server.md | 10 +++++++--- tools/vanilla_client.py | 23 +++++++++++++++++------ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/docs/server.md b/docs/server.md index 923eddf2c..a8855905d 100644 --- a/docs/server.md +++ b/docs/server.md @@ -74,9 +74,13 @@ not tick cost, so it is in scope here (see section 9). headless client built from vanilla's own codecs, pings and joins Horizon's server and vanilla's from the same world copy. It compares every login, configuration and play packet up to the first chunk batch, the chunk bytes, chat, `/list`, failing commands, the console lines and the files left behind. Result on main: - see "Harnesses on main" below. The real vanilla GUI client (`tools/vanilla_client.py`) could not be run - here: the tool only knows Linux natives, and the Mac mini's session was locked, which stalls the client's - rendering. It runs on the desktop under vscreen. + see "Harnesses on main" below. +- **The real vanilla 26.3 client** (`tools/vanilla_client.py`, from the owner's own files, dev-only) joins + too. On 2026-10-18 it joined the server build of the game (`horizon-engine`, section 3) on the Mac mini: + `python3 tools/vanilla_client.py --name Vanilla --join localhost:PORT`. It logged in, drew the terrain + around spawn and stayed connected for four minutes, with no warning or error on the server. The tool + runs on Linux under vscreen and on macOS from the logged-in screen session, even a locked one, with vsync + off. ### What fails or is missing diff --git a/tools/vanilla_client.py b/tools/vanilla_client.py index 88c6aba5b..41ae38556 100644 --- a/tools/vanilla_client.py +++ b/tools/vanilla_client.py @@ -5,6 +5,10 @@ servers: --offlineDeveloperMode) and optionally join a server straight away vscreen start python3 tools/vanilla_client.py --name Guest --join localhost:25565 +On Linux (x86_64) and on macOS (Apple silicon, from the logged-in screen session), from the files +tools/lab.py prepared on that machine. On macOS vsync is off: with it on, a window nobody can see (a +locked session, or covered) blocks in SDL_GL_SwapWindow and the client stops before it joins. + Sound goes to OpenAL's null backend. The game directory gets an options.txt that skips the first-run screens, keeps the game running unfocused and renders few chunks, so a harness can drive it with vscreen keys. @@ -21,6 +25,9 @@ from pathlib import Path ROOT = Path(__file__).resolve().parents[1] GAME = ROOT / ".local/minecraft/26.3" +MAC = sys.platform == "darwin" +# The natives of this machine's platform, and the shared libraries in them. +NATIVES, LIBRARY_SUFFIX = ("natives-macos-arm64.jar", ".dylib") if MAC else ("natives-linux.jar", ".so") OPTIONS = { "onboardAccessibility": "false", @@ -40,6 +47,8 @@ OPTIONS = { "onlyShowSecureChat": "false", "rawMouseInput": "false", } +if sys.platform == "darwin": + OPTIONS["enableVsync"] = "false" def offline_uuid(name): @@ -49,10 +58,12 @@ def offline_uuid(name): return uuid.UUID(bytes=bytes(digest)) -def linux_library(path): +def host_library(path): name = Path(path).name if "natives-" in name: - return name.endswith("natives-linux.jar") + return name.endswith(NATIVES) + if MAC: + return "netty-transport-native" not in name or "linux" not in name if "netty-transport-native" in name and ("osx" in name or "aarch_64" in name): return False return "java-objc-bridge" not in name @@ -60,18 +71,18 @@ def linux_library(path): def classpath(): prepared = json.loads((GAME / "prepared.json").read_text()) - jars = [ROOT / p for p in prepared["libraries"]["client"] if linux_library(p)] + jars = [ROOT / p for p in prepared["libraries"]["client"] if host_library(p)] return [GAME / "jars/client.jar", *jars] def extract_natives(jars, target): target.mkdir(parents=True, exist_ok=True) for jar in jars: - if not jar.name.endswith("natives-linux.jar"): + if not jar.name.endswith(NATIVES): continue with zipfile.ZipFile(jar) as archive: for name in archive.namelist(): - if name.endswith(".so") and "arm" not in name and "aarch" not in name: + if name.endswith(LIBRARY_SUFFIX) and (MAC or ("arm" not in name and "aarch" not in name)): out = target / Path(name).name if not out.exists(): out.write_bytes(archive.read(name)) @@ -105,7 +116,7 @@ def command(args): extract_natives(jars, natives) assets = args.assets or (args.game_dir / "assets") (assets / "indexes").mkdir(parents=True, exist_ok=True) - cmd = ["java", f"-Xmx{args.heap}", "-XX:StackShadowPages=32", + cmd = ["java", f"-Xmx{args.heap}", *(["-XstartOnFirstThread"] if MAC else []), "-XX:StackShadowPages=32", "--enable-native-access=ALL-UNNAMED", "--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED", f"-Djava.library.path={natives}", f"-Dorg.lwjgl.system.SharedLibraryExtractPath={natives}", From a0bcc13f1c046f845b81c95bc7bfcc3903b6e7ff Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 20:24:09 +0200 Subject: [PATCH 10/12] Tick blocks around every player, from vanilla's chunk tickets (#890) Without a local player (dedicated server, --server-thread) no block ticked: tick_blocks returned early, so crops, fluids, redstone, pistons and pressure plates stood still, and a LAN host ticked only around itself. ticket_levels ports DistanceManager's levels: PLAYER_LOADING at 31 within the view distance and PLAYER_SIMULATION at 31 - simulation distance for every player in the level (the local player, LAN guests, a server's players), and FORCED at 31. Random ticks run in simulation entity-ticking chunks that are ticking chunks, scheduled ticks, block events and block entities in block ticking ones, entities in entity ticking ones; the sculk blocks follow the same rule. Saved ticks unpack when a chunk starts ticking (startTickingChunk), not when it loads. The hub keeps the tick-rate state current while nobody is in the game, so a player joining a frozen server is told once (updateJoiningPlayer). dedicated_server parity froze nothing while the client was sent its chunks: saved fluid ticks there (a lava fall that never settles) ran before or after the chunk was sent depending on each server's loading speed, and it failed on main too. It now freezes the game until the client has its chunks. lan_e2e gets a growth check: water flows and wheat grows next to a guest 1000 blocks from the host. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 79 +++++- .../src/game/block_entity_ticks.rs | 25 +- crates/horizon-client/src/game/creatures.rs | 21 +- crates/horizon-client/src/game/levels.rs | 6 +- crates/horizon-client/src/game/persist.rs | 8 +- crates/horizon-client/src/game/sculk.rs | 14 +- .../horizon-client/src/game/sculk_fixture.rs | 2 + crates/horizon-client/src/lan/hub.rs | 6 + crates/horizon-client/src/lib.rs | 1 + .../src/tick_chunk_membership.rs | 45 ++++ crates/horizon-client/src/ticket_levels.rs | 232 ++++++++++++++++++ tools/lan_e2e.py | 48 +++- tools/test_dedicated_server_parity.py | 10 + 13 files changed, 431 insertions(+), 66 deletions(-) create mode 100644 crates/horizon-client/src/ticket_levels.rs diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index dc56fc4fa..2cbdabaa4 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -480,6 +480,8 @@ struct GameLevel<'w> { rand_sequences: &'w mut horizon_client::random_sequences::RandomSequences, pending: &'w mut spill::Pending, spill: &'w mut spill::Spill, + /// Where the chunk tickets let blocks tick. + tickets: horizon_client::ticket_levels::Levels, } struct PlayerCollision { y: f64, @@ -677,6 +679,11 @@ impl LevelAccess for GameLevel<'_> { ) .unwrap_or(false) } + /// `ServerLevel.isPositionTickingWithEntitiesLoaded`: block ticking, and + /// a ticking chunk. + fn is_ticking_chunk(&self, chunk: horizon_core::position::ChunkPos) -> bool { + self.tickets.block_ticking(chunk.x, chunk.z) && ticking_chunk(self.world, chunk.x, chunk.z) + } /// Entities that trigger pressure plates, tripwires and detector rails: /// the players (the host and the LAN guests, mobs) and the minecarts. fn count_entities(&self, area: Aabb, query: EntityQuery) -> usize { @@ -953,6 +960,12 @@ struct DragView { left: Option, } /// Ctrl+Q reads the physical keyboard, independently of sprint key mappings. +/// A chunk whose ticking future can complete (`ChunkMap.prepareTickingChunk`): +/// lit, with its neighbours loaded, as `postprocess_chunks` waits for. +fn ticking_chunk(world: &World, x: i32, z: i32) -> bool { + world.chunks.get(&(x, z)).is_some_and(|c| c.lit) + && (-1..=1).all(|dz| (-1..=1).all(|dx| world.chunks.contains_key(&(x + dx, z + dz)))) +} fn drop_whole_stack(physical_keys: &HashSet) -> bool { physical_keys.contains(&KeyCode::ControlLeft) || physical_keys.contains(&KeyCode::ControlRight) } @@ -1205,6 +1218,7 @@ struct Game<'a> { end: end::EndState, block_ticks: Ticks, tick_chunks: horizon_client::tick_chunk_membership::TickChunkMembership, + tickets: horizon_client::ticket_levels::ChunkTickets, random_tick_sections: horizon_client::random_tick_sections::RandomTickSections, level_events: Vec, falling: Vec, @@ -2038,6 +2052,7 @@ impl Game<'_> { rand_sequences: &mut self.rand_sequences, pending: &mut self.pending, spill: &mut self.spill, + tickets: self.tickets.levels(), }; let result = f(self.res.behaviours(), &mut level, &mut self.block_ticks); self.flush_spill(); @@ -2080,22 +2095,62 @@ impl Game<'_> { } self.tick_chunks.recycle_added(added); } - /// ServerLevel.tick block phases: scheduled ticks, random ticks within the - /// simulation distance, block events and moving pistons. + /// `DistanceManager`'s tickets of the level in place, from every player in + /// it (the local player, LAN guests, a server's players) and its forced + /// chunks. A game with neither a server nor a player (the headless + /// fixtures) ticks everywhere. + fn update_chunk_tickets(&mut self) { + let mut players: Vec<(i32, i32)> = self.lan.as_ref().map_or_else(Vec::new, |h| { + h.hub + .guests + .iter() + .filter(|g| h.hub.here(g)) + .map(|g| g.chunk()) + .collect() + }); + if let Some(p) = &self.player { + players.push(( + (p.position.x.floor() as i32) >> 4, + (p.position.z.floor() as i32) >> 4, + )); + } + let sources = (self.options.dedicated.is_some() || !players.is_empty()).then(|| { + horizon_client::ticket_levels::Sources { + players, + forced: self.streaming.forced.iter().copied().collect(), + view_distance: self.settings.render_distance.clamp(2, 32), + simulation_distance: self.settings.simulation_distance.max(2), + } + }); + self.tickets.update(sources); + } + /// `ServerLevel.startTickingChunk`: a loaded chunk's saved ticks are + /// scheduled from the game time at which it becomes a ticking chunk + /// (block ticking, with its neighbours loaded). Chunks become ticking + /// between ticks in vanilla: before this tick's clock advanced. + fn start_ticking_chunks(&mut self) { + let levels = self.tickets.levels(); + let world = &self.world; + let started = self + .tick_chunks + .start_ticking(|(x, z)| levels.loaded_ticking(x, z) && ticking_chunk(world, x, z)); + let time = self.world.game_time - 1; + for (x, z) in started { + let pos = horizon_core::position::ChunkPos::new(x, z); + self.block_ticks.blocks.unpack_chunk(pos, time); + self.block_ticks.fluids.unpack_chunk(pos, time); + } + } + /// ServerLevel.tick block phases: scheduled ticks, random ticks in the + /// chunks the tickets simulate, block events and moving pistons. fn tick_blocks(&mut self) -> Result<()> { self.sync_tick_chunks(); - let Some(p) = self.player.as_ref() else { - return Ok(()); - }; - let (pcx, pcz) = ( - (p.position.x.floor() as i32) >> 4, - (p.position.z.floor() as i32) >> 4, - ); - let simulation = self.settings.render_distance.min(8); + self.start_ticking_chunks(); + let levels = self.tickets.levels(); let mut ticking = Vec::new(); let facts = self.res.facts(); for (&(x, z), chunk) in &self.world.chunks { - if (x - pcx).abs() > simulation || (z - pcz).abs() > simulation || !chunk.lit { + if !levels.random_ticking(x, z) || !ticking_chunk(&self.world, x, z) { continue; } // Capture eligibility before inside-block and scheduled updates, as @@ -3872,6 +3927,7 @@ impl Game<'_> { /// What the tick runs after the local player's own update: effects, the /// world's entities and blocks, and the network hub. fn tick_world(&mut self, running: bool) -> Result<()> { + self.update_chunk_tickets(); horizon_client::spans::time("tick_effects", || self.tick_effects()); horizon_client::spans::time("tick_using", || self.tick_using()); horizon_client::spans::time("tick_hud", || self.tick_hud()); @@ -10683,6 +10739,7 @@ impl<'a> Game<'a> { end: Default::default(), block_ticks: Ticks::new(0), tick_chunks: Default::default(), + tickets: Default::default(), random_tick_sections: Default::default(), level_events: Vec::new(), falling: Vec::new(), diff --git a/crates/horizon-client/src/game/block_entity_ticks.rs b/crates/horizon-client/src/game/block_entity_ticks.rs index 1c0258871..bb1cadd15 100644 --- a/crates/horizon-client/src/game/block_entity_ticks.rs +++ b/crates/horizon-client/src/game/block_entity_ticks.rs @@ -101,27 +101,12 @@ impl Game<'_> { }) .collect() } - /// `ServerLevel.shouldTickBlocksAt`: chunks within the simulation - /// distance plus one (`DistanceManager.inBlockTickingRange`, Chebyshev - /// in chunks) of the player or a guest. With nobody there, a dedicated - /// server ticks none; the game alone (no player yet, headless fixtures) - /// ticks every chunk. + /// `ServerLevel.shouldTickBlocksAt` in a block-ticking chunk + /// (`LevelChunk.isTicking`, block coordinates), from every player's + /// tickets (`update_chunk_tickets`). fn block_ticking_area(&self) -> impl Fn(i32, i32) -> bool + use<> { - let d = self.settings.simulation_distance.max(2) + 1; - let mut centres: Vec<(i32, i32)> = self.streaming.extra.iter().map(|&(c, _)| c).collect(); - if let Some(p) = &self.player { - centres.push(( - (p.position.x.floor() as i32) >> 4, - (p.position.z.floor() as i32) >> 4, - )); - } - let everywhere = centres.is_empty() && self.options.dedicated.is_none(); - move |x, z| { - everywhere - || centres - .iter() - .any(|&(cx, cz)| ((x >> 4) - cx).abs().max(((z >> 4) - cz).abs()) <= d) - } + let levels = self.tickets.levels(); + move |x, z| levels.block_ticking_at(x, z) } /// `AbstractFurnaceBlockEntity.serverTick` of the furnace at `pos`, and /// its block's `lit` following it. diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 6e9b3d84a..422d050b6 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -475,24 +475,11 @@ impl Game<'_> { self.tick_raids(); self.tick_creature_entities() } - /// `DistanceManager.inEntityTickingRange`: chunks within the simulation - /// distance (Chebyshev, in chunks) of the player or of a guest. + /// `DistanceManager.inEntityTickingRange` in an entity-ticking chunk + /// (block coordinates), from every player's tickets (`update_chunk_tickets`). pub(super) fn entity_ticking_area(&self) -> impl Fn(i32, i32) -> bool + use<> { - let d = self.settings.simulation_distance.max(2); - let mut centres: Vec<(i32, i32)> = self.streaming.extra.iter().map(|&(c, _)| c).collect(); - if let Some(p) = &self.player { - centres.push(( - (p.position.x.floor() as i32) >> 4, - (p.position.z.floor() as i32) >> 4, - )); - } - let everywhere = centres.is_empty(); - move |x, z| { - everywhere - || centres - .iter() - .any(|&(cx, cz)| ((x >> 4) - cx).abs().max(((z >> 4) - cz).abs()) <= d) - } + let levels = self.tickets.levels(); + move |x, z| levels.entity_ticking_at(x, z) } /// The entity phase of `tick_creatures`: every mob's tick, deaths, and the /// projectiles and effects that follow from them. diff --git a/crates/horizon-client/src/game/levels.rs b/crates/horizon-client/src/game/levels.rs index bd2f84efb..3225408f5 100644 --- a/crates/horizon-client/src/game/levels.rs +++ b/crates/horizon-client/src/game/levels.rs @@ -20,7 +20,8 @@ use super::{ }; use horizon_client::{ Result, block_entity_anim::BlockEntityAnims, random_tick_sections::RandomTickSections, - save::SaveDir, streaming::Streaming, tick_chunk_membership::TickChunkMembership, world::World, + save::SaveDir, streaming::Streaming, tick_chunk_membership::TickChunkMembership, + ticket_levels::ChunkTickets, world::World, }; use horizon_world::level::{LevelEvent, Ticks}; use std::{collections::HashMap, sync::Arc, time::Instant}; @@ -37,6 +38,7 @@ pub(super) struct Parked { save: Arc, block_ticks: Ticks, tick_chunks: TickChunkMembership, + tickets: ChunkTickets, random_tick_sections: RandomTickSections, level_events: Vec, falling: Vec, @@ -113,6 +115,7 @@ impl Game<'_> { save, block_ticks: Ticks::new(self.level.seed as i32), tick_chunks: Default::default(), + tickets: Default::default(), random_tick_sections: Default::default(), level_events: Vec::new(), falling: Vec::new(), @@ -159,6 +162,7 @@ impl Game<'_> { swap(&mut self.save, &mut p.save); swap(&mut self.block_ticks, &mut p.block_ticks); swap(&mut self.tick_chunks, &mut p.tick_chunks); + swap(&mut self.tickets, &mut p.tickets); swap(&mut self.random_tick_sections, &mut p.random_tick_sections); swap(&mut self.level_events, &mut p.level_events); swap(&mut self.falling, &mut p.falling); diff --git a/crates/horizon-client/src/game/persist.rs b/crates/horizon-client/src/game/persist.rs index 2574f45d7..7a967a024 100644 --- a/crates/horizon-client/src/game/persist.rs +++ b/crates/horizon-client/src/game/persist.rs @@ -105,7 +105,9 @@ impl Game<'_> { chunk.saved = Some(Arc::new(extras)); } /// Register a newly resident chunk with the tick scheduler, with the - /// block and fluid ticks it was saved with. + /// block and fluid ticks it was saved with (`LevelChunk.registerTickContainerInLevel` + /// at FULL). They are unpacked when the chunk starts ticking + /// (`start_ticking_chunks`). pub(super) fn register_chunk_ticks(&mut self, key: (i32, i32)) { let pos = ChunkPos::new(key.0, key.1); let saved = self.world.chunks.get(&key).and_then(|c| c.saved.clone()); @@ -130,9 +132,7 @@ impl Game<'_> { .is_ok() && loaded { - let time = self.world.game_time; - self.block_ticks.blocks.unpack_chunk(pos, time); - self.block_ticks.fluids.unpack_chunk(pos, time); + self.tick_chunks.wait(key); } // The saved list is consumed: later saves take the scheduler's ticks. if loaded && let Some(c) = self.world.chunks.get_mut(&key) { diff --git a/crates/horizon-client/src/game/sculk.rs b/crates/horizon-client/src/game/sculk.rs index fc6c03f9e..de7ea6df2 100644 --- a/crates/horizon-client/src/game/sculk.rs +++ b/crates/horizon-client/src/game/sculk.rs @@ -705,18 +705,10 @@ impl Game<'_> { } Ok(()) } - /// `ServerLevel.shouldTickBlocksAt`, as `tick_blocks` decides it. + /// `ServerLevel.shouldTickBlocksAt` in a block-ticking chunk, from every + /// player's tickets (`update_chunk_tickets`), once the chunk is lit. fn chunk_ticks(&self, cx: i32, cz: i32) -> bool { - let Some(p) = self.player.as_ref() else { - return false; - }; - let (pcx, pcz) = ( - (p.position.x.floor() as i32) >> 4, - (p.position.z.floor() as i32) >> 4, - ); - let simulation = self.settings.render_distance.min(8); - (cx - pcx).abs() <= simulation - && (cz - pcz).abs() <= simulation + self.tickets.levels().block_ticking(cx, cz) && self.world.chunks.get(&(cx, cz)).is_some_and(|c| c.lit) } fn tick_sculk_block(&mut self, key: (i32, i32, i32)) -> Result<()> { diff --git a/crates/horizon-client/src/game/sculk_fixture.rs b/crates/horizon-client/src/game/sculk_fixture.rs index b57f559b6..890e571b8 100644 --- a/crates/horizon-client/src/game/sculk_fixture.rs +++ b/crates/horizon-client/src/game/sculk_fixture.rs @@ -696,6 +696,8 @@ impl Game<'_> { seed_fresh(m, 0x5eed + (named + i + 1) as i64); } } + // The player's tickets, as `tick_world` takes them. + self.update_chunk_tickets(); // Entities: the player's warden spawn tracker, then the mobs. self.tick_sculk_entities(); // (ServerChunkCache.tick, natural spawning and its chunk shuffle, is not run.) diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index 6456b1001..cab5a9d16 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -1012,6 +1012,12 @@ impl Hub { } } if !self.guests.iter().any(|g| self.here(g)) { + // ServerTickRateManager broadcasts a change to the players there + // are; one who joins later is sent the state as it is then + // (`updateJoiningPlayer`). + if !self.guests.iter().any(|g| g.placed) { + self.ticking = (ctx.tick_rate, ctx.tick_frozen); + } if !self .guests .iter() diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 1e2c8c331..437fbfa9d 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -172,6 +172,7 @@ pub mod telemetry; pub mod testgen; pub mod text; pub mod tick_chunk_membership; +pub mod ticket_levels; pub mod title; pub mod title_edition; pub mod toasts; diff --git a/crates/horizon-client/src/tick_chunk_membership.rs b/crates/horizon-client/src/tick_chunk_membership.rs index 42785f289..a7e934443 100644 --- a/crates/horizon-client/src/tick_chunk_membership.rs +++ b/crates/horizon-client/src/tick_chunk_membership.rs @@ -10,6 +10,9 @@ pub struct TickChunkMembership { revision: Option, added: Vec, removed: Vec, + /// Loaded chunks with saved ticks that have not started ticking yet + /// (`LevelChunkTicks.pendingTicks`, unpacked by `startTickingChunk`). + waiting: HashSet, } impl TickChunkMembership { @@ -20,6 +23,7 @@ impl TickChunkMembership { self.revision = None; self.added.clear(); self.removed.clear(); + self.waiting.clear(); } /// Returns false in O(1) when resident membership is unchanged. On change, @@ -36,6 +40,7 @@ impl TickChunkMembership { true } else { self.removed.push(*key); + self.waiting.remove(key); false } }); @@ -62,6 +67,28 @@ impl TickChunkMembership { pub fn removed(&self) -> &[Key] { &self.removed } + + /// `key` was registered with saved ticks to unpack once it ticks. + pub fn wait(&mut self, key: Key) { + self.waiting.insert(key); + } + + /// The waiting chunks that `ticking` says now tick, no longer waiting. + pub fn start_ticking(&mut self, mut ticking: impl FnMut(Key) -> bool) -> Vec { + if self.waiting.is_empty() { + return Vec::new(); + } + let started: Vec = self + .waiting + .iter() + .copied() + .filter(|&k| ticking(k)) + .collect(); + for key in &started { + self.waiting.remove(key); + } + started + } } #[cfg(test)] @@ -124,6 +151,24 @@ mod tests { compare(&mut cache, &mut old, &map); } + #[test] + fn waiting_chunks_start_ticking_once_and_unloaded_ones_stop_waiting() { + let mut map = ChunkMap::default(); + let mut cache = TickChunkMembership::default(); + for key in [(0, 0), (1, 0), (2, 0)] { + map.insert(key, Arc::new(Chunk::empty(key.0, key.1))); + cache.wait(key); + } + cache.synchronize(&map); + assert_eq!(cache.start_ticking(|k| k == (0, 0)), vec![(0, 0)]); + assert!(cache.start_ticking(|k| k == (0, 0)).is_empty()); + map.remove(&(1, 0)); + cache.synchronize(&map); + let mut started = cache.start_ticking(|_| true); + started.sort_unstable(); + assert_eq!(started, vec![(2, 0)]); + } + #[test] fn scheduler_keeps_saved_ticks_on_stable_membership_and_removes_unloaded_chunks() { use horizon_core::{ diff --git a/crates/horizon-client/src/ticket_levels.rs b/crates/horizon-client/src/ticket_levels.rs new file mode 100644 index 000000000..216815de1 --- /dev/null +++ b/crates/horizon-client/src/ticket_levels.rs @@ -0,0 +1,232 @@ +//! Which chunks tick, from the chunk tickets of `DistanceManager`. +//! +//! Every player holds two kinds of ticket: +//! - `PLAYER_LOADING` at level 31 (`ENTITY_TICKING`) on every chunk within +//! the server's view distance (Chebyshev), from `PlayerTicketTracker`; +//! - `PLAYER_SIMULATION` at level `31 - simulationDistance` on its own chunk. +//! +//! A `/forceload`ed chunk holds a `FORCED` ticket at level 31 of both kinds. +//! Levels rise by one per chunk away from a ticket (`ChunkTracker`) and each +//! chunk takes the lowest. The loading level gives the chunk's +//! `FullChunkStatus` (`ChunkLevel.fullStatus`: 31 entity ticking, 32 block +//! ticking, 33 full) and the simulation level gives what +//! `inEntityTickingRange` (31 or less) and `inBlockTickingRange` (32 or less) +//! answer. Vanilla needs both: +//! - entities tick where both levels are entity ticking (`ServerLevel.tick`, +//! and `EntityTickList` holds only the entities of entity-ticking chunks); +//! - random ticks run in the simulation tracker's entity-ticking chunks that +//! have a ticking chunk (`ChunkMap.forEachBlockTickingChunk`); +//! - scheduled ticks, block events and block entities run where both are +//! block ticking (`isPositionTickingWithEntitiesLoaded`, +//! `shouldTickBlocksAt`, `LevelChunk.isTicking`). +//! +//! Whoever the players are (the local player, LAN guests, a dedicated +//! server's players), they count the same. +use crate::fast_hash::FxHashMap; +use std::sync::Arc; + +/// `ChunkLevel`'s levels. +pub const ENTITY_TICKING: u8 = 31; +pub const BLOCK_TICKING: u8 = 32; +/// What a chunk with no ticket near it has (`FULL` and above count as none). +const NONE: (u8, u8) = (BLOCK_TICKING + 1, BLOCK_TICKING + 1); + +/// The tickets of one level: where its players are, its forced chunks, and +/// the server's view and simulation distances. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Sources { + pub players: Vec<(i32, i32)>, + pub forced: Vec<(i32, i32)>, + pub view_distance: i32, + pub simulation_distance: i32, +} + +/// Each chunk's (loading, simulation) level, for the chunks where either is +/// block ticking or lower; `None` ticks everywhere (a game with no server and +/// no player: the headless fixtures). +#[derive(Clone, Default)] +pub struct Levels(Option>>); + +impl Levels { + /// Chunk coordinates. + fn get(&self, x: i32, z: i32) -> Option<(u8, u8)> { + let map = self.0.as_ref()?; + Some(map.get(&(x, z)).copied().unwrap_or(NONE)) + } + /// `DistanceManager.inEntityTickingRange`, in an entity-ticking chunk: + /// where entities tick. + pub fn entity_ticking(&self, x: i32, z: i32) -> bool { + self.get(x, z) + .is_none_or(|(l, s)| l <= ENTITY_TICKING && s <= ENTITY_TICKING) + } + /// `inBlockTickingRange` in a block-ticking chunk: where scheduled ticks, + /// block events and block entities run. + pub fn block_ticking(&self, x: i32, z: i32) -> bool { + self.get(x, z) + .is_none_or(|(l, s)| l <= BLOCK_TICKING && s <= BLOCK_TICKING) + } + /// `inEntityTickingRange` in a block-ticking chunk: where random ticks run. + pub fn random_ticking(&self, x: i32, z: i32) -> bool { + self.get(x, z) + .is_none_or(|(l, s)| l <= BLOCK_TICKING && s <= ENTITY_TICKING) + } + /// `ChunkHolder.getFullStatus` is block ticking or more: the chunk's + /// ticking future can complete (its saved ticks unpack then). + pub fn loaded_ticking(&self, x: i32, z: i32) -> bool { + self.get(x, z).is_none_or(|(l, _)| l <= BLOCK_TICKING) + } + /// The same as [`Self::entity_ticking`] at a block position. + pub fn entity_ticking_at(&self, x: i32, z: i32) -> bool { + self.entity_ticking(x >> 4, z >> 4) + } + /// The same as [`Self::block_ticking`] at a block position. + pub fn block_ticking_at(&self, x: i32, z: i32) -> bool { + self.block_ticking(x >> 4, z >> 4) + } +} + +/// The levels, rebuilt only when their sources change (a player crossing +/// into another chunk, a distance or a forced chunk changing). +#[derive(Default)] +pub struct ChunkTickets { + sources: Option, + levels: Levels, +} + +impl ChunkTickets { + /// `None`: tick everywhere. + pub fn update(&mut self, mut sources: Option) { + if let Some(s) = sources.as_mut() { + s.players.sort_unstable(); + s.players.dedup(); + s.forced.sort_unstable(); + } + if sources == self.sources { + return; + } + self.levels = Levels(sources.as_ref().map(|s| Arc::new(compute(s)))); + self.sources = sources; + } + pub fn levels(&self) -> Levels { + self.levels.clone() + } +} + +fn compute(s: &Sources) -> FxHashMap<(i32, i32), (u8, u8)> { + let view = s.view_distance.clamp(0, 32); + let simulation = s.simulation_distance.clamp(0, 32); + let mut levels: FxHashMap<(i32, i32), (u8, u8)> = FxHashMap::default(); + let mut lower = |key, loading: i32, sim: i32| { + let e = levels.entry(key).or_insert(NONE); + e.0 = e.0.min(loading.min(i32::from(NONE.0)) as u8); + e.1 = e.1.min(sim.min(i32::from(NONE.1)) as u8); + }; + // PLAYER_LOADING within the view distance, PLAYER_SIMULATION at the + // player's chunk (`getPlayerTicketLevel`: never below 0). + let player_sim = (i32::from(ENTITY_TICKING) - simulation).max(0); + let reach = (view + 1).max(i32::from(BLOCK_TICKING) - player_sim); + for &(px, pz) in &s.players { + for dz in -reach..=reach { + for dx in -reach..=reach { + let d = dx.abs().max(dz.abs()); + lower( + (px + dx, pz + dz), + i32::from(ENTITY_TICKING) + (d - view).max(0), + player_sim + d, + ); + } + } + } + for &(fx, fz) in &s.forced { + for dz in -1i32..=1 { + for dx in -1i32..=1 { + let level = i32::from(ENTITY_TICKING) + dx.abs().max(dz.abs()); + lower((fx + dx, fz + dz), level, level); + } + } + } + levels.retain(|_, l| *l != NONE); + levels +} + +#[cfg(test)] +mod tests { + use super::*; + + fn levels(players: &[(i32, i32)], forced: &[(i32, i32)], view: i32, sim: i32) -> Levels { + let mut t = ChunkTickets::default(); + t.update(Some(Sources { + players: players.to_vec(), + forced: forced.to_vec(), + view_distance: view, + simulation_distance: sim, + })); + t.levels() + } + + #[test] + fn simulation_distance_inside_the_view() { + // view 8, simulation 4: entities and random ticks out to 4 chunks, + // scheduled ticks out to 5. + let l = levels(&[(0, 0)], &[], 8, 4); + assert!(l.entity_ticking(4, -4) && !l.entity_ticking(5, 0)); + assert!(l.random_ticking(-4, 4) && !l.random_ticking(0, 5)); + assert!(l.block_ticking(5, 5) && !l.block_ticking(6, 0)); + assert!(l.loaded_ticking(9, 9) && !l.loaded_ticking(10, 0)); + } + + #[test] + fn view_distance_inside_the_simulation() { + // view 4, simulation 10: what isn't loaded at entity ticking (beyond + // the view distance) runs no entities, and block ticks stop where the + // chunk is only full (beyond the view distance + 1). + let l = levels(&[(0, 0)], &[], 4, 10); + assert!(l.entity_ticking(4, 0) && !l.entity_ticking(5, 0)); + assert!(l.random_ticking(5, 0) && !l.random_ticking(6, 0)); + assert!(l.block_ticking(5, 5) && !l.block_ticking(6, 0)); + } + + #[test] + fn every_player_and_forced_chunk() { + let l = levels(&[(0, 0), (40, 0)], &[(-100, 7)], 4, 4); + assert!(l.entity_ticking(44, 0) && l.entity_ticking(0, -4)); + assert!(!l.entity_ticking(20, 0)); + // A forced chunk entity ticks, its neighbours only block tick. + assert!(l.entity_ticking(-100, 7) && !l.entity_ticking(-101, 7)); + assert!(l.block_ticking(-101, 8) && !l.block_ticking(-102, 7)); + assert!(!l.random_ticking(-101, 7)); + assert!(l.entity_ticking_at(-1600, 112) && !l.block_ticking_at(-1617, 112)); + } + + #[test] + fn no_tickets_tick_nothing_and_no_sources_everything() { + let none = levels(&[], &[], 10, 10); + assert!(!none.block_ticking(0, 0) && !none.entity_ticking(0, 0)); + let everywhere = ChunkTickets::default().levels(); + assert!(everywhere.random_ticking(1000, -1000)); + } + + #[test] + fn rebuilt_only_when_sources_change() { + let mut t = ChunkTickets::default(); + let s = |players: Vec<(i32, i32)>| { + Some(Sources { + players, + forced: Vec::new(), + view_distance: 2, + simulation_distance: 2, + }) + }; + t.update(s(vec![(0, 0), (5, 5)])); + let first = t.levels(); + t.update(s(vec![(5, 5), (0, 0)])); + assert!(Arc::ptr_eq( + first.0.as_ref().unwrap(), + t.levels().0.as_ref().unwrap() + )); + t.update(s(vec![(1, 0), (5, 5)])); + assert!(t.levels().entity_ticking(3, 0) && !first.entity_ticking(3, 0)); + t.update(None); + assert!(t.levels().0.is_none()); + } +} diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index f0db98e40..144ed6961 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -69,6 +69,8 @@ address. The script then checks, from both sides, what a guest's game used to ge swing when its hoe tills dirt (the server's use) (#848). - time: the guest's clock is the host's: it stops with `/time pause`, and catches up with the server's game time after the guest's game stalls for a few seconds (#848). +- growth: far from the host, the chunks around the guest tick (#890): water placed there flows + and wheat on farmland grows (random ticks), however the host runs its world. - light: the guest's light matches the host's: around a torch placed near it, under a roof and once a block of the roof is removed under the sky, and in the last chunk it tracks next to glowstone in a chunk it doesn't (only the server's light updates bring that one). @@ -105,7 +107,7 @@ ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'hurt', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'trials', 'clicks', 'sleep', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'light', 'time', 'swings', 'plates', 'effects', 'portals', 'distance'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'hurt', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'trials', 'clicks', 'sleep', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'light', 'time', 'swings', 'plates', 'effects', 'portals', 'distance', 'growth'] class Game: @@ -1528,6 +1530,48 @@ def same_light(host, guest, lo, hi, what, ticks=100): f'{h["sky"][i]} block {h["block"][i]}, guest sky {g["sky"][i]} block {g["block"][i]}') +def check_growth(host, guest, out): + """Block ticks near a guest 1000 blocks from the host: only the guest's tickets tick them.""" + x0, y0, z0 = pos_of(guest) + guest('command', command='gamemode creative') + guest('command', command=f'tp @s {x0 + 1000:.2f} 200 {z0:.2f}') + guest.wait(100) + x, y, z = (int(v // 1) for v in pos_of(guest)) + # A platform in the air over wherever the guest landed: a water source two blocks east of the + # guest, wheat on farmland two blocks west. Vanilla's water spreads only towards the nearest + # drop (FlowingFluid.getSpread), so any of the source's four neighbours may take it. Wheat + # grows only in daylight (raw brightness 9 or more). + top = max(y + 12, 120) + water = (x + 2, top + 1, z) + beside = [(water[0] + dx, water[1], water[2] + dz) for dx, dz in ((1, 0), (-1, 0), (0, 1), (0, -1))] + wheat = (x - 2, top + 1, z) + flowed = [] + try: + host('command', command='time set noon') + guest('command', command=f'fill {x - 4} {top} {z - 2} {x + 4} {top} {z + 2} minecraft:stone') + guest('command', command=f'setblock {wheat[0]} {top} {wheat[2]} minecraft:farmland[moisture=7]') + guest('command', command=f'setblock {wheat[0]} {wheat[1]} {wheat[2]} minecraft:wheat[age=0]') + guest('command', command='gamerule random_tick_speed 1000') + guest('command', command=f'setblock {water[0]} {water[1]} {water[2]} minecraft:water') + for _ in range(40): + guest.wait(10) + flowed = [p for p in beside if guest.raw('block', *p).get('block') == 'minecraft:water'] + if flowed: + break + age = wait_state(guest, wheat, 'age', '7', 2400) + guest.shot(out, 'growth') + finally: + guest('command', command='gamerule random_tick_speed 3') + host('command', command='time set day') + guest('command', command=f'tp @s {x0:.2f} {y0:.2f} {z0:.2f}') + guest('command', command='gamemode survival') + guest.wait(40) + detail = {'flowed': flowed, 'wheat age': age} + assert flowed, f'the water beside the guest did not flow: {detail}' + assert age == '7', f'the wheat beside the guest did not grow: {detail}' + return detail + + def check_light(host, guest, out): gx, gy, gz = (int(v // 1) for v in pos_of(guest)) results = {} @@ -1591,7 +1635,7 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'name 'xp': check_xp, 'objects': check_objects, 'projectiles': check_projectiles, 'light': check_light, 'plates': check_plates, 'swings': check_swings, - 'time': check_time} + 'time': check_time, 'growth': check_growth} def run(ctl, out, resources, checks): diff --git a/tools/test_dedicated_server_parity.py b/tools/test_dedicated_server_parity.py index 6ac9aecbe..e8497d7f8 100644 --- a/tools/test_dedicated_server_parity.py +++ b/tools/test_dedicated_server_parity.py @@ -268,6 +268,12 @@ def run_side(label, command, directory, client): # it stays put; experience keeps its profession). server.console('summon minecraft:villager ~1 ~ ~ {NoAI:1b,Xp:1,VillagerData:' '{profession:"minecraft:librarian",level:2,type:"minecraft:plains"}}') + # The saved world still has fluid ticks due in the area the client is sent (a lava fall + # that never settles), and they run once their chunk ticks. When that is, against when the + # chunk is sent, follows how fast the server loads the chunks around the joining player: + # each run, vanilla sends a different set of them already flowed. Frozen until the client + # has its chunks, both servers send the world as saved; blocks tick again from the chat on. + server.console('tick freeze') port = int(PROPERTIES_PORTS[label]) legacy = legacy_ping(port) # The client asks to be made an operator once it has traded, runs /kill and respawns. @@ -282,7 +288,11 @@ def run_side(label, command, directory, client): joined = threading.Thread(target=join) joined.start() deadline = time.time() + 300 + frozen = True while joined.is_alive() and not any(' ready to respawn' in l for l in server.lines): + if frozen and any(' hello from the parity client' in l for l in server.lines): + server.console('tick unfreeze') + frozen = False if time.time() > deadline: break time.sleep(0.2) From 32a37e8937069331e06f03ee3751335852639095 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 23:22:07 +0200 Subject: [PATCH 11/12] Benchmark: Horizon's server against vanilla 26.3 with mixed bots, equal work Explore bots paced at 0.8 blocks a second and a 300-second warm-up, so both servers serve the same paths; the work table shows it. Horizon ran as horizon-engine (the server build, from its archive). Co-Authored-By: Claude Opus 5.5 --- .../server-equal-mixed-2026-10-18/README.md | 137 ++++ .../server-equal-mixed-2026-10-18/load.txt | 702 ++++++++++++++++++ .../mixed-equal.json | 421 +++++++++++ docs/server.md | 17 +- 4 files changed, 1276 insertions(+), 1 deletion(-) create mode 100644 benchmarks/server-equal-mixed-2026-10-18/README.md create mode 100644 benchmarks/server-equal-mixed-2026-10-18/load.txt create mode 100644 benchmarks/server-equal-mixed-2026-10-18/mixed-equal.json diff --git a/benchmarks/server-equal-mixed-2026-10-18/README.md b/benchmarks/server-equal-mixed-2026-10-18/README.md new file mode 100644 index 000000000..5849e72ba --- /dev/null +++ b/benchmarks/server-equal-mixed-2026-10-18/README.md @@ -0,0 +1,137 @@ +# Horizon's server against vanilla 26.3: mixed bots, equal work (2026-10-18) + +This reruns `../server-mixed-2026-10-18/` with the bots' load made the same on both servers. In that run the +`explore` bots flew as fast as each server sent them chunks. Horizon kept up and vanilla didn't, so Horizon +sent 2.9 to 5.9 times as many chunks a second, and vanilla's walking bots got a fifth of the chunks Horizon's +got. The comparison of tick times wasn't fair. + +## Summary + +- **The work was equal** at 1 and 10 bots. At 25 bots it was equal in chunks sent a second (158.6 against 157.6); + vanilla was still catching up on chunks its bots hadn't had yet, see "Equal work" below. Every bot joined, none + was dropped, no chunk was sent twice, and no bot ever waited for the chunk it stood in. +- **Tick time (MSPT), same work:** + - 1 bot: 1.8 ms on Horizon against 11.3 on vanilla; + - 10 bots: 4.7 against 34.3 ms; + - 25 bots: 11.7 against 50.5 ms. Vanilla dropped just under 20 TPS (19.8) and its worst 5-second P99 was + 1388 ms, against Horizon's 130; + - 50 bots: Horizon ran at 23.8 ms (20 TPS). Vanilla's watchdog stopped its server 30 s after the last bot + joined ("A single server tick took 60.01 seconds"). +- **Memory:** Horizon's RSS was 4.1 to 4.9 times smaller: 0.69 against 3.40 GB with one bot, 0.95 against + 3.87 GB with 25, and 1.03 GB with 50. Vanilla's heap is fixed at 4 GB, so its RSS is mostly that heap. +- **CPU** (100 % is one of the machine's 4 threads): 30 against 128 % at 10 bots, 58 against 192 % at 25. +- **What changed since the first run:** the first run's vanilla held 50 bots at 29.7 ms because its walking + bots got 640 to 1100 chunks each. With the explore bots paced, vanilla serves the walking bots in full + (5400 to 6100 chunks each, against Horizon's 6100 to 6600), and that is the load it can't carry at 50. + +## Results + +MSPT is milliseconds per tick from each server's own `/tick query` (50 is the limit for 20 TPS). P95 and P99 +are over the 300 measured seconds. "Worst P99" is the highest P99 of a single 5-second query. RSS and CPU +cover the server's process tree. "First-time chunks/s" counts chunks a bot hadn't received before (summed +over the bots). "Waited" is ticks a second that bots stood still waiting for their own chunk. "Pulled back" +counts position packets that moved a bot back. + +| Bots | Server | MSPT | P95 | P99 | Worst P99 | TPS | RSS (mean / max) | CPU | Start | Chunks/s | First-time chunks/s | Waited | Pulled back | +|---:|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:| +| 1 | Horizon | **1.77** | 4.21 | 13.04 | 47.5 | 20 | **0.69 / 0.69 GB** | 28 % | 19.7 s | 9.94 | 0.01 | 0 | 0 | +| 1 | vanilla | 11.34 | 17.12 | 33.22 | 119.1 | 20 | 3.40 / 3.42 GB | 36 % | 20.4 s | 9.98 | 0.01 | 0 | 0 | +| 10 | Horizon | **4.74** | 10.48 | 19.69 | 120.1 | 20 | **0.86 / 0.88 GB** | 30 % | 19.5 s | 61.35 | 1.79 | 0 | 4159 | +| 10 | vanilla | 34.30 | 53.72 | 89.08 | 375.8 | 20 | 3.71 / 3.75 GB | 128 % | 19.9 s | 61.66 | 1.93 | 0 | 3976 | +| 25 | Horizon | **11.72** | 24.45 | 43.79 | 129.8 | 20 | **0.95 / 1.09 GB** | 58 % | 9.6 s | 158.57 | 5.78 | 0 | 3496 | +| 25 | vanilla | 50.45 | 74.09 | 191.21 | 1388.1 | 19.82 | 3.87 / 4.75 GB | 192 % | 37.8 s | 157.63 | 8.25 | 0 | 3629 | +| 50 | Horizon | **23.82** | 50.98 | 74.64 | 195.9 | 20 | **1.03 / 1.20 GB** | 98 % | 14.9 s | 302.81 | 10.96 | 0 | 7565 | +| 50 | vanilla | stopped by its watchdog | | | | | | | 11.2 s | | | | | + +Chunks each bot received over the whole run, by behaviour (mean): + +| Bots | Server | walk | explore | idle | +|---:|---|---:|---:|---:| +| 1 | Horizon | 6092 | | | +| 1 | vanilla | 6084 | | | +| 10 | Horizon | 6248 | 1165 | 473 | +| 10 | vanilla | 5729 | 1124 | 473 | +| 25 | Horizon | 6629 | 1267 | 473 | +| 25 | vanilla | 5387 | 1119 | 473 | +| 50 | Horizon | 6497 | 1256 | 473 | + +Both servers pulled bots back about as often (`moved too quickly` 20 and 296 times on vanilla at 10 and 25 +bots). + +## Equal work + +`tools/server_bench.py` now prints a work table after the runs. At each bot count it compares every server's +chunks sent a second and first-time chunks a second with the busiest server's. Work counts as equal when +both are within 10 % (or within a chunk a second when there are few). The report keeps the table as `work`. + +| Bots | Chunks/s (Horizon, vanilla) | First-time chunks/s (Horizon, vanilla) | Equal | +|---:|---|---|---| +| 1 | 9.94, 9.98 | 0.01, 0.01 | yes | +| 10 | 61.35, 61.66 | 1.79, 1.93 | yes | +| 25 | 158.57, 157.63 | 5.78, 8.25 | no: vanilla sent 2.5 more first-time chunks a second | + +Two settings made the load the same: + +- **Explore bots paced at 0.8 blocks a second** (`--explore-speed 0.8`), the rate the 2026-10-17 run showed + both servers keep up with. Every bot then follows the same path on both servers. The path is deterministic + per bot, and a bot only stops while the chunk it stands in hasn't arrived, which never happened here + (`Waited` 0). So both servers had the same chunks to send. +- **A 300-second warm-up** before the 300 measured seconds. A first pass with the old 60-second warm-up + (aborted after 10 bots) sent the same chunks a second on both (63.1 against 61.4). But vanilla was still + delivering the bots' first views during the measurement (12.9 first-time chunks a second against Horizon's + 4.1), so its window held more work than Horizon's. With 300 seconds both were in steady state at 1 and 10 + bots. At 25, vanilla still sent 2.5 more first-time chunks a second than Horizon: 1.6 % of all it sent, and + its walking bots ended the run with 19 % fewer chunks. Read the 25-bot tick times with that in mind: the + extra first-time chunks are work vanilla had still to do because it fell behind. + +## Method + +`tools/server_bench.py` ran each configuration on a fresh server: + +- the same `server.properties` on both: seed 12345, view and simulation distance 10, offline, creative, + difficulty easy, `sync-chunk-writes=true`, white-list off; +- `gamerule respawn_radius 0`, and otherwise vanilla's default game rules (the locator bar on for both); +- the server waits for `Done`, the bots join one a second, warm up for 300 seconds and are measured for 300, + with `/tick query` every 5 seconds and RSS and CPU every second. + +The bots are `horizon-bots --behaviour mixed`: 60 % walk, 20 % explore and 20 % idle, each choice deterministic +per bot. All are creative and fly 24 blocks above spawn. `walk` flies to random points within 48 blocks at +5.6 blocks a second, `explore` flies straight out at 0.8 blocks a second, and `idle` hovers. + +**Machine.** The ThinkPad: Intel Core i7-7500U (2 cores, 4 threads), 32 GB, NixOS. The desktop (24 threads) +was meant to run it, but it didn't wake (`~/fleet/power wake desktop` timed out, as it did for the first run). +No other session ran on the ThinkPad during the runs (`load.txt`, a sample a minute). The servers ran under +`nice -n 19`. The bots ran on the Mac mini (M4 Pro, shared with other work at 70 to 90 % CPU) and reached the +ThinkPad over the tailnet. + +**What ran.** + +- Horizon: the server build from #916 (`horizon-client --no-default-features`, no sound or controllers) at + 519ec121, packaged with `xtask package-server` and installed with `install.sh --archive`, as Linux owners + will get it. `horizon-server` ran it as `horizon-engine`. The game files were prepared from the owner's own + Minecraft. `horizon-bots` and `server_bench.py` came from this PR. +- Vanilla: the pinned 26.3 server jar (the owner's own copy) on OpenJDK 25.0.4.1, `-Xms4g -Xmx4g`. + +`server_bench.py` stopped after vanilla's 50-bot server crashed and before it wrote its report. +`mixed-equal.json` holds the run lines it printed. The vanilla 50 entry and its error come from that server's +log, and the `work` table was computed with `server_bench.work`. `load.txt` is the fleet monitor's view of the +machines during the runs. + +## Reproduce + + curl -fsSL https://raw.githubusercontent.com/ludovic111/horizon/main/packaging/server/install.sh | sh + horizon-prepare --prepare --server-jar /path/to/server.jar # your own copy + cargo build --release -p horizon-bots # on the machine that runs the bots + # in a checkout whose .local/minecraft holds the pinned vanilla jars (tools/lab.py prepare): + HORIZON_BENCH_BIN=~/.local/share/horizon/current \ + python3 tools/server_bench.py --servers horizon,vanilla --bots 1,10,25,50 --behaviour mixed \ + --explore-speed 0.8 --warmup 300 --nice 19 \ + --bots-via 'ssh ' --bots-bin /path/to/horizon-bots --connect \ + --out mixed-equal.json + +## Next + +- One pass per configuration ran. Vanilla at 50 bots, in particular, needs a second pass, ideally on the + desktop (24 threads), where neither server is CPU-bound. +- 100 bots, and Paper and Fabric + Lithium (docs/server.md section 9). +- A longer warm-up, or measuring from when the first-time chunk rate settles, so the 25-bot work is equal too. diff --git a/benchmarks/server-equal-mixed-2026-10-18/load.txt b/benchmarks/server-equal-mixed-2026-10-18/load.txt new file mode 100644 index 000000000..2992b4a26 --- /dev/null +++ b/benchmarks/server-equal-mixed-2026-10-18/load.txt @@ -0,0 +1,702 @@ +2026-10-18T19:32:39Z +ludothinkpad ok: 27.2 GB free, 4 threads 17% busy, 0 sessions +desktop offline +macmini ok: 25.9 GB free, 14 threads 73% busy, 1 sessions +macbook offline + RAM 27.0 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 42% busy (agents 0.0, rest 1.6 cores) · 80°C · load 5.9 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:33:44Z +ludothinkpad ok: 26.7 GB free, 4 threads 19% busy, 0 sessions +desktop offline +macmini busy: 26.7 GB free, 14 threads 93% busy, 1 sessions +macbook offline + RAM 26.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 34% busy (agents 0.0, rest 1.7 cores) · 81°C · load 4.0 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:34:48Z +ludothinkpad ok: 26.4 GB free, 4 threads 56% busy, 0 sessions +desktop offline +macmini busy: 26.9 GB free, 14 threads 90% busy, 1 sessions +macbook offline + RAM 26.4 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 54% busy (agents 0.0, rest 2.2 cores) · 79°C · load 2.8 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:35:52Z +ludothinkpad ok: 26.7 GB free, 4 threads 27% busy, 0 sessions +desktop offline +macmini ok: 25.4 GB free, 14 threads 85% busy, 1 sessions +macbook offline + RAM 26.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 30% busy (agents 0.0, rest 1.1 cores) · 82°C · load 2.2 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:36:57Z +ludothinkpad ok: 26.7 GB free, 4 threads 25% busy, 0 sessions +desktop offline +macmini busy: 25.4 GB free, 14 threads 93% busy, 1 sessions +macbook offline + RAM 26.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 37% busy (agents 0.0, rest 1.2 cores) · 80°C · load 1.5 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:38:01Z +ludothinkpad ok: 26.6 GB free, 4 threads 54% busy, 0 sessions +desktop offline +macmini ok: 26.7 GB free, 14 threads 64% busy, 1 sessions +macbook offline + RAM 26.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 21% busy (agents 0.0, rest 1.2 cores) · 81°C · load 1.9 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:39:05Z +ludothinkpad ok: 26.7 GB free, 4 threads 20% busy, 0 sessions +desktop offline +macmini ok: 26.1 GB free, 14 threads 70% busy, 1 sessions +macbook offline + RAM 26.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 28% busy (agents 0.0, rest 1.0 cores) · 81°C · load 1.3 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:40:10Z +ludothinkpad ok: 26.7 GB free, 4 threads 39% busy, 0 sessions +desktop offline +macmini ok: 29.2 GB free, 14 threads 73% busy, 1 sessions +macbook offline + RAM 26.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 32% busy (agents 0.0, rest 1.7 cores) · 80°C · load 0.9 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:41:14Z +ludothinkpad ok: 26.7 GB free, 4 threads 31% busy, 0 sessions +desktop offline +macmini ok: 28.8 GB free, 14 threads 77% busy, 1 sessions +macbook offline + RAM 26.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 15% busy (agents 0.0, rest 1.1 cores) · 82°C · load 1.3 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:42:19Z +ludothinkpad ok: 26.5 GB free, 4 threads 26% busy, 0 sessions +desktop offline +macmini busy: 26.1 GB free, 14 threads 94% busy, 1 sessions +macbook offline + RAM 26.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 30% busy (agents 0.0, rest 1.3 cores) · 78°C · load 1.1 + · disk 289.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:43:23Z +ludothinkpad busy: 26.7 GB free, 4 threads 99% busy, 0 sessions +desktop offline +macmini ok: 28.2 GB free, 14 threads 83% busy, 1 sessions +macbook offline + RAM 26.3 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 99% busy (agents 0.0, rest 3.8 cores) · 86°C · load 2.4 + · disk 289.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:44:27Z +ludothinkpad busy: 25.2 GB free, 4 threads 98% busy, 0 sessions +desktop offline +macmini ok: 29.0 GB free, 14 threads 73% busy, 1 sessions +macbook offline + RAM 24.9 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 83% busy (agents 0.0, rest 3.7 cores) · 83°C · load 5.5 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:45:31Z +ludothinkpad ok: 24.5 GB free, 4 threads 39% busy, 0 sessions +desktop offline +macmini busy: 27.0 GB free, 14 threads 93% busy, 1 sessions +macbook offline + RAM 24.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 34% busy (agents 0.0, rest 1.5 cores) · 84°C · load 3.5 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:46:35Z +ludothinkpad ok: 23.7 GB free, 4 threads 69% busy, 0 sessions +desktop offline +macmini busy: 26.2 GB free, 14 threads 90% busy, 1 sessions +macbook offline + RAM 23.8 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 75% busy (agents 0.0, rest 2.8 cores) · 87°C · load 2.7 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:47:40Z +ludothinkpad ok: 23.8 GB free, 4 threads 66% busy, 0 sessions +desktop offline +macmini busy: 25.9 GB free, 14 threads 85% busy, 1 sessions +macbook offline + RAM 23.8 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 59% busy (agents 0.0, rest 2.5 cores) · 83°C · load 2.9 + · disk 291.7 GB free · battery 96% · Ludovic: away + +2026-10-18T19:48:44Z +ludothinkpad ok: 24.2 GB free, 4 threads 61% busy, 0 sessions +desktop offline +macmini busy: 29.0 GB free, 14 threads 90% busy, 1 sessions +macbook offline + RAM 24.3 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 62% busy (agents 0.0, rest 2.5 cores) · 84°C · load 3.2 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:49:48Z +ludothinkpad ok: 25.1 GB free, 4 threads 59% busy, 0 sessions +desktop offline +macmini ok: 31.1 GB free, 14 threads 81% busy, 1 sessions +macbook offline + RAM 25.1 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 57% busy (agents 0.0, rest 2.4 cores) · 85°C · load 2.9 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:50:52Z +ludothinkpad ok: 25.7 GB free, 4 threads 39% busy, 0 sessions +desktop offline +macmini busy: 28.4 GB free, 14 threads 88% busy, 2 sessions +macbook offline + RAM 25.8 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 18% busy (agents 0.0, rest 1.4 cores) · 67°C · load 2.4 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:51:57Z +ludothinkpad ok: 23.7 GB free, 4 threads 40% busy, 0 sessions +desktop offline +macmini ok: 27.9 GB free, 14 threads 83% busy, 1 sessions +macbook offline + RAM 23.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 23% busy (agents 0.0, rest 1.4 cores) · 81°C · load 1.8 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:53:01Z +ludothinkpad ok: 25.3 GB free, 4 threads 25% busy, 0 sessions +desktop offline +macmini ok: 27.1 GB free, 14 threads 84% busy, 1 sessions +macbook offline + RAM 25.1 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 31% busy (agents 0.0, rest 1.2 cores) · 80°C · load 1.8 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:54:05Z +ludothinkpad ok: 26.5 GB free, 4 threads 75% busy, 0 sessions +desktop offline +macmini busy: 27.1 GB free, 14 threads 91% busy, 1 sessions +macbook offline + RAM 26.5 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 69% busy (agents 0.0, rest 2.5 cores) · 78°C · load 2.3 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:55:09Z +ludothinkpad busy: 26.4 GB free, 4 threads 89% busy, 0 sessions +desktop offline +macmini busy: 26.3 GB free, 14 threads 86% busy, 1 sessions +macbook offline + RAM 26.3 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 32% busy (agents 0.0, rest 2.4 cores) · 79°C · load 2.9 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:56:14Z +ludothinkpad ok: 26.4 GB free, 4 threads 39% busy, 0 sessions +desktop offline +macmini ok: 27.1 GB free, 14 threads 68% busy, 1 sessions +macbook offline + RAM 26.4 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 63% busy (agents 0.0, rest 1.8 cores) · 82°C · load 2.3 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:57:18Z +ludothinkpad ok: 26.3 GB free, 4 threads 20% busy, 0 sessions +desktop offline +macmini busy: 29.2 GB free, 14 threads 85% busy, 1 sessions +macbook offline + RAM 26.3 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 19% busy (agents 0.0, rest 1.0 cores) · 80°C · load 1.8 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:58:22Z +ludothinkpad ok: 26.3 GB free, 4 threads 28% busy, 0 sessions +desktop offline +macmini busy: 27.3 GB free, 14 threads 88% busy, 1 sessions +macbook offline + RAM 26.1 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 93% busy (agents 0.0, rest 2.5 cores) · 80°C · load 2.0 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:59:27Z +ludothinkpad ok: 26.2 GB free, 4 threads 52% busy, 0 sessions +desktop offline +macmini ok: 35.0 GB free, 14 threads 85% busy, 1 sessions +macbook offline + RAM 26.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 22% busy (agents 0.0, rest 1.8 cores) · 80°C · load 3.6 + · disk 291.4 GB free · battery 96% · Ludovic: away + +2026-10-18T20:00:31Z +ludothinkpad busy: 25.8 GB free, 4 threads 87% busy, 0 sessions +desktop offline +macmini busy: 33.0 GB free, 14 threads 87% busy, 1 sessions +macbook offline + RAM 27.5 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 51% busy (agents 0.0, rest 2.9 cores) · 79°C · load 3.6 + · disk 291.5 GB free · battery 96% · Ludovic: away + +2026-10-18T20:01:35Z +ludothinkpad ok: 25.6 GB free, 4 threads 35% busy, 0 sessions +desktop offline +macmini ok: 32.4 GB free, 14 threads 81% busy, 1 sessions +macbook ok: 7.2 GB free, 12 threads 32% busy, 0 sessions + RAM 25.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 34% busy (agents 0.0, rest 1.3 cores) · 81°C · load 2.5 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T20:02:40Z +ludothinkpad ok: 26.6 GB free, 4 threads 35% busy, 0 sessions +desktop offline +macmini ok: 29.2 GB free, 14 threads 79% busy, 1 sessions +macbook ok: 6.5 GB free, 12 threads 39% busy, 0 sessions + RAM 26.5 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 33% busy (agents 0.0, rest 1.3 cores) · 81°C · load 2.7 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T20:03:44Z +ludothinkpad ok: 25.5 GB free, 4 threads 56% busy, 0 sessions +desktop offline +macmini busy: 31.1 GB free, 14 threads 89% busy, 1 sessions +macbook ok: 6.8 GB free, 12 threads 6% busy, 0 sessions + RAM 25.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 66% busy (agents 0.0, rest 2.5 cores) · 87°C · load 2.5 + · disk 291.3 GB free · battery 96% · Ludovic: away + +2026-10-18T20:04:48Z +ludothinkpad busy: 26.2 GB free, 4 threads 99% busy, 0 sessions +desktop offline +macmini busy: 30.0 GB free, 14 threads 89% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 31% busy, 0 sessions + RAM 25.8 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 84% busy (agents 0.0, rest 3.7 cores) · 85°C · load 3.4 + · disk 291.3 GB free · battery 96% · Ludovic: away + +2026-10-18T20:05:52Z +ludothinkpad busy: 24.5 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini busy: 34.1 GB free, 14 threads 89% busy, 1 sessions +macbook ok: 6.6 GB free, 12 threads 10% busy, 0 sessions + RAM 24.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 100% busy (agents 0.0, rest 4.0 cores) · 83°C · load 9.0 + · disk 291.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:06:57Z +ludothinkpad busy: 23.5 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini busy: 28.3 GB free, 14 threads 88% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 5% busy, 0 sessions + RAM 23.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 99% busy (agents 0.0, rest 4.0 cores) · 82°C · load 10.1 + · disk 309.2 GB free · battery 96% · Ludovic: away + +2026-10-18T20:08:01Z +ludothinkpad busy: 23.4 GB free, 4 threads 88% busy, 0 sessions +desktop offline +macmini ok: 29.6 GB free, 14 threads 83% busy, 1 sessions +macbook ok: 6.6 GB free, 12 threads 2% busy, 0 sessions + RAM 23.4 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 77% busy (agents 0.0, rest 3.4 cores) · 84°C · load 7.5 + · disk 309.2 GB free · battery 96% · Ludovic: away + +2026-10-18T20:09:05Z +ludothinkpad busy: 24.0 GB free, 4 threads 89% busy, 0 sessions +desktop offline +macmini ok: 25.9 GB free, 14 threads 83% busy, 1 sessions +macbook ok: 7.1 GB free, 12 threads 19% busy, 0 sessions + RAM 23.9 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 91% busy (agents 0.0, rest 3.6 cores) · 82°C · load 5.9 + · disk 309.1 GB free · battery 96% · Ludovic: away + +2026-10-18T20:10:09Z +ludothinkpad busy: 24.0 GB free, 4 threads 98% busy, 0 sessions +desktop offline +macmini busy: 27.1 GB free, 14 threads 88% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 19% busy, 0 sessions + RAM 24.0 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 92% busy (agents 0.0, rest 3.7 cores) · 84°C · load 6.6 + · disk 309.1 GB free · battery 96% · Ludovic: away + +2026-10-18T20:11:14Z +ludothinkpad busy: 24.3 GB free, 4 threads 92% busy, 0 sessions +desktop offline +macmini ok: 26.3 GB free, 14 threads 84% busy, 1 sessions +macbook ok: 6.6 GB free, 12 threads 18% busy, 0 sessions + RAM 24.4 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 99% busy (agents 0.0, rest 3.7 cores) · 85°C · load 6.5 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:12:18Z +ludothinkpad busy: 24.7 GB free, 4 threads 89% busy, 0 sessions +desktop offline +macmini ok: 28.6 GB free, 14 threads 78% busy, 1 sessions +macbook ok: 7.3 GB free, 12 threads 20% busy, 0 sessions + RAM 24.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 82% busy (agents 0.0, rest 3.4 cores) · 84°C · load 6.1 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:13:22Z +ludothinkpad ok: 23.4 GB free, 4 threads 53% busy, 0 sessions +desktop offline +macmini ok: 28.1 GB free, 14 threads 77% busy, 1 sessions +macbook ok: 7.3 GB free, 12 threads 20% busy, 0 sessions + RAM 23.4 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 48% busy (agents 0.0, rest 2.4 cores) · 80°C · load 4.2 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:14:27Z +ludothinkpad ok: 24.0 GB free, 4 threads 44% busy, 0 sessions +desktop offline +macmini ok: 28.0 GB free, 14 threads 71% busy, 1 sessions +macbook ok: 6.9 GB free, 12 threads 2% busy, 0 sessions + RAM 24.1 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 23% busy (agents 0.0, rest 2.0 cores) · 77°C · load 4.3 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:15:31Z +ludothinkpad ok: 22.7 GB free, 4 threads 31% busy, 0 sessions +desktop offline +macmini busy: 27.2 GB free, 14 threads 86% busy, 1 sessions +macbook ok: 6.9 GB free, 12 threads 19% busy, 0 sessions + RAM 22.5 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 30% busy (agents 0.0, rest 1.3 cores) · 77°C · load 2.6 + · disk 309.1 GB free · battery 96% · Ludovic: away + +2026-10-18T20:16:35Z +ludothinkpad ok: 26.8 GB free, 4 threads 37% busy, 0 sessions +desktop offline +macmini ok: 28.6 GB free, 14 threads 82% busy, 1 sessions +macbook ok: 6.8 GB free, 12 threads 9% busy, 0 sessions + RAM 26.8 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 42% busy (agents 0.0, rest 1.7 cores) · 81°C · load 2.3 + · disk 309.1 GB free · battery 96% · Ludovic: away + +2026-10-18T20:17:39Z +ludothinkpad ok: 26.3 GB free, 4 threads 63% busy, 0 sessions +desktop offline +macmini ok: 26.8 GB free, 14 threads 78% busy, 1 sessions +macbook ok: 7.0 GB free, 12 threads 19% busy, 0 sessions + RAM 26.4 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 64% busy (agents 0.0, rest 2.6 cores) · 87°C · load 1.9 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:18:43Z +ludothinkpad ok: 26.9 GB free, 4 threads 37% busy, 0 sessions +desktop offline +macmini busy: 32.2 GB free, 14 threads 87% busy, 1 sessions +macbook ok: 7.0 GB free, 12 threads 10% busy, 0 sessions + RAM 26.9 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 56% busy (agents 0.0, rest 1.9 cores) · 84°C · load 3.9 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:19:48Z +ludothinkpad ok: 26.0 GB free, 4 threads 77% busy, 0 sessions +desktop offline +macmini ok: 29.2 GB free, 14 threads 83% busy, 1 sessions +macbook ok: 7.0 GB free, 12 threads 10% busy, 0 sessions + RAM 26.0 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 66% busy (agents 0.0, rest 2.9 cores) · 86°C · load 3.2 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:20:52Z +ludothinkpad ok: 25.9 GB free, 4 threads 80% busy, 0 sessions +desktop offline +macmini ok: 26.8 GB free, 14 threads 84% busy, 1 sessions +macbook ok: 7.0 GB free, 12 threads 17% busy, 0 sessions + RAM 25.9 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 68% busy (agents 0.0, rest 3.2 cores) · 86°C · load 4.4 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:21:56Z +ludothinkpad ok: 26.3 GB free, 4 threads 63% busy, 0 sessions +desktop offline +macmini ok: 26.3 GB free, 14 threads 77% busy, 1 sessions +macbook ok: 6.8 GB free, 12 threads 14% busy, 0 sessions + RAM 26.3 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 66% busy (agents 0.0, rest 2.8 cores) · 84°C · load 3.1 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:23:00Z +ludothinkpad ok: 26.8 GB free, 4 threads 74% busy, 0 sessions +desktop offline +macmini ok: 26.1 GB free, 14 threads 82% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 11% busy, 0 sessions + RAM 26.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 65% busy (agents 0.0, rest 2.8 cores) · 84°C · load 5.0 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:24:05Z +ludothinkpad ok: 26.9 GB free, 4 threads 64% busy, 0 sessions +desktop offline +macmini busy: 28.3 GB free, 14 threads 90% busy, 1 sessions +macbook ok: 6.8 GB free, 12 threads 8% busy, 0 sessions + RAM 26.9 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 64% busy (agents 0.0, rest 2.7 cores) · 86°C · load 4.9 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:25:09Z +ludothinkpad ok: 26.0 GB free, 4 threads 56% busy, 0 sessions +desktop offline +macmini busy: 26.2 GB free, 14 threads 88% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 12% busy, 0 sessions + RAM 26.0 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 49% busy (agents 0.0, rest 2.1 cores) · 72°C · load 5.5 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:26:13Z +ludothinkpad ok: 25.8 GB free, 4 threads 47% busy, 0 sessions +desktop offline +macmini ok: 27.8 GB free, 14 threads 78% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 8% busy, 0 sessions + RAM 25.8 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 56% busy (agents 0.0, rest 2.1 cores) · 78°C · load 3.1 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:27:18Z +ludothinkpad busy: 25.5 GB free, 4 threads 94% busy, 0 sessions +desktop offline +macmini ok: 26.6 GB free, 14 threads 82% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 17% busy, 0 sessions + RAM 25.4 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 100% busy (agents 0.0, rest 3.9 cores) · 78°C · load 4.7 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:28:22Z +ludothinkpad busy: 24.6 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini ok: 26.4 GB free, 14 threads 80% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 8% busy, 0 sessions + RAM 24.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 99% busy (agents 0.0, rest 4.0 cores) · 77°C · load 10.5 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:29:27Z +ludothinkpad busy: 24.3 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini ok: 29.8 GB free, 14 threads 80% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 8% busy, 0 sessions + RAM 24.0 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 99% busy (agents 0.0, rest 4.0 cores) · 82°C · load 10.0 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:30:31Z +ludothinkpad busy: 23.7 GB free, 4 threads 91% busy, 0 sessions +desktop offline +macmini ok: 30.1 GB free, 14 threads 71% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 9% busy, 0 sessions + RAM 23.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 94% busy (agents 0.0, rest 3.8 cores) · 80°C · load 10.3 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:31:36Z +ludothinkpad busy: 23.6 GB free, 4 threads 93% busy, 0 sessions +desktop offline +macmini ok: 28.0 GB free, 14 threads 81% busy, 1 sessions +macbook ok: 6.6 GB free, 12 threads 14% busy, 0 sessions + RAM 23.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 95% busy (agents 0.0, rest 3.7 cores) · 80°C · load 10.7 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:32:40Z +ludothinkpad busy: 23.6 GB free, 4 threads 94% busy, 0 sessions +desktop offline +macmini ok: 26.9 GB free, 14 threads 76% busy, 1 sessions +macbook ok: 6.8 GB free, 12 threads 22% busy, 0 sessions + RAM 23.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 84% busy (agents 0.0, rest 3.5 cores) · 80°C · load 9.7 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:33:45Z +ludothinkpad ok: 25.2 GB free, 4 threads 51% busy, 0 sessions +desktop offline +macmini ok: 27.1 GB free, 14 threads 82% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 17% busy, 0 sessions + RAM 25.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 48% busy (agents 0.0, rest 2.1 cores) · 87°C · load 6.5 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:34:49Z +ludothinkpad ok: 25.1 GB free, 4 threads 56% busy, 0 sessions +desktop offline +macmini busy: 28.2 GB free, 14 threads 87% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 17% busy, 0 sessions + RAM 24.5 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 78% busy (agents 0.0, rest 2.7 cores) · 88°C · load 4.0 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:35:53Z +ludothinkpad busy: 23.6 GB free, 4 threads 86% busy, 0 sessions +desktop offline +macmini busy: 27.9 GB free, 14 threads 86% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 17% busy, 0 sessions + RAM 23.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 88% busy (agents 0.0, rest 3.3 cores) · 85°C · load 4.9 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:36:58Z +ludothinkpad ok: 23.6 GB free, 4 threads 75% busy, 0 sessions +desktop offline +macmini ok: 29.4 GB free, 14 threads 84% busy, 1 sessions +macbook ok: 6.5 GB free, 12 threads 18% busy, 0 sessions + RAM 23.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 85% busy (agents 0.0, rest 3.2 cores) · 85°C · load 5.1 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:38:02Z +ludothinkpad ok: 22.6 GB free, 4 threads 55% busy, 0 sessions +desktop offline +macmini ok: 26.5 GB free, 14 threads 80% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 10% busy, 0 sessions + RAM 22.3 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 54% busy (agents 0.0, rest 2.2 cores) · 85°C · load 4.5 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:39:06Z +ludothinkpad ok: 27.2 GB free, 4 threads 55% busy, 0 sessions +desktop offline +macmini busy: 29.5 GB free, 14 threads 85% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 9% busy, 0 sessions + RAM 27.3 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 48% busy (agents 0.0, rest 2.2 cores) · 87°C · load 4.0 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:40:10Z +ludothinkpad ok: 26.2 GB free, 4 threads 72% busy, 0 sessions +desktop offline +macmini ok: 27.7 GB free, 14 threads 84% busy, 1 sessions +macbook ok: 6.6 GB free, 12 threads 19% busy, 0 sessions + RAM 26.3 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 80% busy (agents 0.0, rest 3.1 cores) · 86°C · load 10.8 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:41:15Z +ludothinkpad busy: 26.4 GB free, 4 threads 86% busy, 0 sessions +desktop offline +macmini ok: 28.5 GB free, 14 threads 79% busy, 1 sessions +macbook ok: 6.6 GB free, 12 threads 10% busy, 0 sessions + RAM 26.4 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 90% busy (agents 0.0, rest 3.4 cores) · 86°C · load 12.8 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:42:19Z +ludothinkpad ok: 26.2 GB free, 4 threads 82% busy, 0 sessions +desktop offline +macmini ok: 26.9 GB free, 14 threads 78% busy, 1 sessions +macbook ok: 6.6 GB free, 12 threads 11% busy, 0 sessions + RAM 26.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 74% busy (agents 0.0, rest 3.3 cores) · 85°C · load 8.8 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:43:23Z +ludothinkpad busy: 26.7 GB free, 4 threads 93% busy, 0 sessions +desktop offline +macmini ok: 27.3 GB free, 14 threads 81% busy, 1 sessions +macbook ok: 6.6 GB free, 12 threads 10% busy, 0 sessions + RAM 26.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 88% busy (agents 0.0, rest 3.4 cores) · 85°C · load 5.4 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:44:27Z +ludothinkpad ok: 27.0 GB free, 4 threads 82% busy, 0 sessions +desktop offline +macmini busy: 26.9 GB free, 14 threads 87% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 13% busy, 0 sessions + RAM 27.0 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 75% busy (agents 0.0, rest 3.3 cores) · 82°C · load 9.8 + · disk 308.8 GB free · battery 96% · Ludovic: away + +2026-10-18T20:45:32Z +ludothinkpad ok: 27.2 GB free, 4 threads 76% busy, 0 sessions +desktop offline +macmini ok: 26.8 GB free, 14 threads 80% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 10% busy, 0 sessions + RAM 27.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 84% busy (agents 0.0, rest 3.2 cores) · 85°C · load 6.9 + · disk 308.8 GB free · battery 96% · Ludovic: away + +2026-10-18T20:46:36Z +ludothinkpad busy: 27.1 GB free, 4 threads 87% busy, 0 sessions +desktop offline +macmini busy: 26.8 GB free, 14 threads 92% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 9% busy, 0 sessions + RAM 26.8 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 85% busy (agents 0.0, rest 3.2 cores) · 82°C · load 4.4 + · disk 308.8 GB free · battery 96% · Ludovic: away + +2026-10-18T20:47:40Z +ludothinkpad busy: 25.3 GB free, 4 threads 86% busy, 0 sessions +desktop offline +macmini ok: 26.5 GB free, 14 threads 72% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 9% busy, 0 sessions + RAM 25.5 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 85% busy (agents 0.0, rest 3.4 cores) · 86°C · load 3.7 + · disk 308.8 GB free · battery 96% · Ludovic: away + +2026-10-18T20:48:45Z +ludothinkpad busy: 26.1 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini ok: 28.5 GB free, 14 threads 84% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 15% busy, 0 sessions + RAM 26.1 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 98% busy (agents 0.0, rest 3.9 cores) · 80°C · load 6.5 + · disk 308.8 GB free · battery 96% · Ludovic: away + +2026-10-18T20:49:50Z +ludothinkpad ok: 26.8 GB free, 4 threads 71% busy, 0 sessions +desktop offline +macmini busy: 27.7 GB free, 14 threads 93% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 10% busy, 0 sessions + RAM 26.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 92% busy (agents 0.0, rest 3.4 cores) · 81°C · load 5.2 + · disk 308.7 GB free · battery 96% · Ludovic: away + +2026-10-18T20:50:54Z +ludothinkpad busy: 25.8 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini busy: 31.3 GB free, 14 threads 95% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 11% busy, 0 sessions + RAM 25.8 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 99% busy (agents 0.0, rest 4.0 cores) · 82°C · load 8.1 + · disk 308.7 GB free · battery 96% · Ludovic: away + +2026-10-18T20:51:59Z +ludothinkpad ok: 27.2 GB free, 4 threads 12% busy, 0 sessions +desktop offline +macmini ok: 31.2 GB free, 14 threads 76% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 8% busy, 0 sessions + RAM 27.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 15% busy (agents 0.0, rest 1.3 cores) · 81°C · load 7.9 + · disk 308.7 GB free · battery 96% · Ludovic: away + +2026-10-18T20:53:03Z +ludothinkpad ok: 27.3 GB free, 4 threads 42% busy, 0 sessions +desktop offline +macmini busy: 27.8 GB free, 14 threads 87% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 9% busy, 0 sessions + RAM 27.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 14% busy (agents 0.0, rest 0.8 cores) · 82°C · load 3.1 + · disk 308.7 GB free · battery 96% · Ludovic: away + +2026-10-18T20:54:08Z +ludothinkpad ok: 28.9 GB free, 4 threads 4% busy, 0 sessions +desktop offline +macmini ok: 29.2 GB free, 14 threads 80% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 9% busy, 0 sessions + RAM 28.9 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 2% busy (agents 0.0, rest 0.3 cores) · 82°C · load 1.3 + · disk 308.7 GB free · battery 96% · Ludovic: away + +2026-10-18T20:55:12Z +ludothinkpad ok: 27.7 GB free, 4 threads 27% busy, 0 sessions +desktop offline +macmini ok: 31.2 GB free, 14 threads 70% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 10% busy, 0 sessions + RAM 27.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 26% busy (agents 0.0, rest 1.2 cores) · 69°C · load 1.0 + · disk 308.8 GB free · battery 96% · Ludovic: away + diff --git a/benchmarks/server-equal-mixed-2026-10-18/mixed-equal.json b/benchmarks/server-equal-mixed-2026-10-18/mixed-equal.json new file mode 100644 index 000000000..c494f3b24 --- /dev/null +++ b/benchmarks/server-equal-mixed-2026-10-18/mixed-equal.json @@ -0,0 +1,421 @@ +{ + "kind": "horizon-server-benchmark", + "date": "2026-10-18T19:32:00+00:00", + "machine": { + "system": "Linux", + "machine": "x86_64", + "cpus": 4, + "name": "ThinkPad, Intel Core i7-7500U, 32 GB, NixOS" + }, + "commit": "f1d5bf520986b00a5b968a84d85809fca1a039fa", + "engine": "horizon-engine: the server build of #916 (horizon-client --no-default-features) at 519ec121, as install.sh --archive installed it", + "java": [ + "openjdk 25.0.4.1 2026-08-18" + ], + "settings": { + "servers": "horizon,vanilla", + "bots": "1,10,25,50", + "behaviour": "mixed", + "join_every": 1.0, + "warmup": 300, + "seconds": 300, + "seed": 12345, + "explore_speed": 0.8, + "gamerule": "", + "heap": "4g", + "nice": 19, + "bots_via": "ssh macmini", + "connect": "the ThinkPad's tailnet address", + "properties": { + "level-seed": "12345", + "view-distance": "10", + "simulation-distance": "10", + "online-mode": "false", + "white-list": "false", + "gamemode": "creative", + "force-gamemode": "true", + "spawn-protection": "0", + "max-players": "200", + "difficulty": "easy", + "sync-chunk-writes": "true" + } + }, + "note": "server_bench.py was stopped after vanilla's 50-bot server crashed, before it wrote this file; the runs are the lines it printed, and the vanilla 50 entry is taken from that server's log.", + "runs": [ + { + "server": "horizon", + "bots": 1, + "behaviour": "mixed", + "start_seconds": 19.73, + "mspt": { + "average": 1.77, + "p50": 1.34, + "p95": 4.21, + "p99": 13.04 + }, + "mspt_worst_p99": 47.5, + "tps": 20.0, + "rss_mb": { + "mean": 686.54, + "max": 686.7 + }, + "cpu_percent": 27.92, + "chunks_per_second": 9.94, + "unique_per_second": 0.01, + "forgotten_per_second": 9.9, + "waited_ticks_per_second": 0.0, + "chunks_total": 6092, + "chunks_per_bot": { + "walk": 6092.0 + }, + "unique_chunks_per_bot": { + "walk": 699.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 1, + "behaviour": "mixed", + "start_seconds": 20.45, + "mspt": { + "average": 11.34, + "p50": 10.6, + "p95": 17.12, + "p99": 33.22 + }, + "mspt_worst_p99": 119.1, + "tps": 20.0, + "rss_mb": { + "mean": 3404.16, + "max": 3416.3 + }, + "cpu_percent": 35.93, + "chunks_per_second": 9.98, + "unique_per_second": 0.01, + "forgotten_per_second": 9.93, + "waited_ticks_per_second": 0.0, + "chunks_total": 6084, + "chunks_per_bot": { + "walk": 6084.0 + }, + "unique_chunks_per_bot": { + "walk": 699.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "horizon", + "bots": 10, + "behaviour": "mixed", + "start_seconds": 19.54, + "mspt": { + "average": 4.74, + "p50": 3.83, + "p95": 10.48, + "p99": 19.69 + }, + "mspt_worst_p99": 120.1, + "tps": 20.0, + "rss_mb": { + "mean": 855.26, + "max": 882.7 + }, + "cpu_percent": 29.78, + "chunks_per_second": 61.35, + "unique_per_second": 1.79, + "forgotten_per_second": 61.47, + "waited_ticks_per_second": 0.0, + "chunks_total": 40762, + "chunks_per_bot": { + "walk": 6247.67, + "explore": 1165.0, + "idle": 473.0 + }, + "unique_chunks_per_bot": { + "walk": 701.83, + "explore": 1086.0, + "idle": 473.0 + }, + "resent": 0, + "pulled_back": 4159, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "mixed", + "start_seconds": 19.95, + "mspt": { + "average": 34.3, + "p50": 32.2, + "p95": 53.72, + "p99": 89.08 + }, + "mspt_worst_p99": 375.8, + "tps": 20.0, + "rss_mb": { + "mean": 3713.5, + "max": 3754.9 + }, + "cpu_percent": 128.07, + "chunks_per_second": 61.66, + "unique_per_second": 1.93, + "forgotten_per_second": 61.71, + "waited_ticks_per_second": 0.0, + "chunks_total": 37568, + "chunks_per_bot": { + "walk": 5729.0, + "explore": 1124.0, + "idle": 473.0 + }, + "unique_chunks_per_bot": { + "walk": 701.17, + "explore": 1055.0, + "idle": 473.0 + }, + "resent": 0, + "pulled_back": 3976, + "moved_too_quickly": 20, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "mixed", + "start_seconds": 9.61, + "mspt": { + "average": 11.72, + "p50": 9.88, + "p95": 24.45, + "p99": 43.79 + }, + "mspt_worst_p99": 129.8, + "tps": 20.0, + "rss_mb": { + "mean": 947.35, + "max": 1086.1 + }, + "cpu_percent": 57.83, + "chunks_per_second": 158.57, + "unique_per_second": 5.78, + "forgotten_per_second": 158.45, + "waited_ticks_per_second": 0.0, + "chunks_total": 108126, + "chunks_per_bot": { + "walk": 6628.53, + "explore": 1266.6, + "idle": 473.0 + }, + "unique_chunks_per_bot": { + "walk": 704.67, + "explore": 1189.2, + "idle": 473.0 + }, + "resent": 0, + "pulled_back": 3496, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "mixed", + "start_seconds": 37.78, + "mspt": { + "average": 50.45, + "p50": 46.39, + "p95": 74.09, + "p99": 191.21 + }, + "mspt_worst_p99": 1388.1, + "tps": 19.82, + "rss_mb": { + "mean": 3866.18, + "max": 4745.0 + }, + "cpu_percent": 192.42, + "chunks_per_second": 157.63, + "unique_per_second": 8.25, + "forgotten_per_second": 156.12, + "waited_ticks_per_second": 0.0, + "chunks_total": 88763, + "chunks_per_bot": { + "walk": 5387.0, + "explore": 1118.6, + "idle": 473.0 + }, + "unique_chunks_per_bot": { + "walk": 699.0, + "explore": 1075.0, + "idle": 473.0 + }, + "resent": 2, + "pulled_back": 3629, + "moved_too_quickly": 296, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "mixed", + "start_seconds": 14.93, + "mspt": { + "average": 23.82, + "p50": 20.11, + "p95": 50.98, + "p99": 74.64 + }, + "mspt_worst_p99": 195.9, + "tps": 20.0, + "rss_mb": { + "mean": 1031.41, + "max": 1201.2 + }, + "cpu_percent": 98.21, + "chunks_per_second": 302.81, + "unique_per_second": 10.96, + "forgotten_per_second": 303.47, + "waited_ticks_per_second": 0.0, + "chunks_total": 212181, + "chunks_per_bot": { + "walk": 6496.53, + "explore": 1255.5, + "idle": 473.0 + }, + "unique_chunks_per_bot": { + "walk": 701.9, + "explore": 1184.7, + "idle": 473.0 + }, + "resent": 0, + "pulled_back": 7565, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 50, + "behaviour": "mixed", + "start_seconds": 11.16, + "error": "stopped by its watchdog 30 s after the last bot joined: \"A single server tick took 60.01 seconds (should be max 0.05)\" (Player.tick)" + } + ], + "work": [ + { + "behaviour": "mixed", + "bots": 1, + "servers": { + "horizon": { + "chunks_per_second": { + "value": 9.94, + "share": 0.996 + }, + "unique_per_second": { + "value": 0.01, + "share": 1.0 + }, + "waited_ticks_per_second": 0.0 + }, + "vanilla": { + "chunks_per_second": { + "value": 9.98, + "share": 1.0 + }, + "unique_per_second": { + "value": 0.01, + "share": 1.0 + }, + "waited_ticks_per_second": 0.0 + } + }, + "equal": true + }, + { + "behaviour": "mixed", + "bots": 10, + "servers": { + "horizon": { + "chunks_per_second": { + "value": 61.35, + "share": 0.995 + }, + "unique_per_second": { + "value": 1.79, + "share": 0.927 + }, + "waited_ticks_per_second": 0.0 + }, + "vanilla": { + "chunks_per_second": { + "value": 61.66, + "share": 1.0 + }, + "unique_per_second": { + "value": 1.93, + "share": 1.0 + }, + "waited_ticks_per_second": 0.0 + } + }, + "equal": true + }, + { + "behaviour": "mixed", + "bots": 25, + "servers": { + "horizon": { + "chunks_per_second": { + "value": 158.57, + "share": 1.0 + }, + "unique_per_second": { + "value": 5.78, + "share": 0.701 + }, + "waited_ticks_per_second": 0.0 + }, + "vanilla": { + "chunks_per_second": { + "value": 157.63, + "share": 0.994 + }, + "unique_per_second": { + "value": 8.25, + "share": 1.0 + }, + "waited_ticks_per_second": 0.0 + } + }, + "equal": false + } + ] +} diff --git a/docs/server.md b/docs/server.md index 923eddf2c..0d27b4481 100644 --- a/docs/server.md +++ b/docs/server.md @@ -495,6 +495,20 @@ reproducible by anyone, and show the same gameplay on both sides. 24.0 against 31.6) and longer with 50 (35.2 against 29.7). Its P99 was lower at every count. - Horizon also did more work: it kept up with the exploring bots, so it generated 1.9 to 2.9 times the new terrain a second. +- **Mixed bots, equal work (2026-10-18,** `benchmarks/server-equal-mixed-2026-10-18/`). This is the same + run with the load made equal. The explore bots fly at 0.8 blocks a second, so every bot follows the same + path on both servers and none waits for terrain. A 300-second warm-up lets both finish the bots' first + views before the measurement. `server_bench.py` now prints a work table that compares each count's chunks + a second across servers and flags a run that isn't equal. Horizon ran as `horizon-engine`, the server + build, installed from its archive. Results, on the ThinkPad again (the desktop didn't wake): + - The work was equal at 1 and 10 bots. At 25 the chunks sent a second were equal (158.6 against 157.6), + and vanilla still sent 2.5 more first-time chunks a second while it caught up. + - Tick: 1.8 against 11.3 ms at 1 bot, 4.7 against 34.3 at 10, 11.7 against 50.5 at 25 (vanilla 19.8 TPS). + At 50 Horizon ran at 23.8 ms. Vanilla's watchdog stopped its server 30 s after the last bot joined (a + 60-second tick). This is one pass, to repeat on a bigger machine. + - Horizon's RSS was 4.1 to 4.9 times smaller (1.03 GB at 50). + - Served in full, vanilla's walking bots got 5400 to 6100 chunks each, against 640 to 1100 in the first + run. That is why vanilla held 50 bots there and not here. - **Ownership.** perf-server-1 owns making the tick faster. This work only measures it, files what it finds, and fixes startup, RAM and packaging. @@ -513,7 +527,8 @@ vanilla. archive in the release. 3. The web panel MVP: status, live TPS/MSPT/RAM/CPU, players, console, backups and login links. 4. `horizon-bots` and `tools/server_bench.py`, with a first run against vanilla (1/10/25/50 bots, `mixed`) - published in `benchmarks/`: done, `benchmarks/server-mixed-2026-10-18/` (section 9). + published in `benchmarks/`: done, `benchmarks/server-mixed-2026-10-18/`, and with equal work + `benchmarks/server-equal-mixed-2026-10-18/` (section 9). 5. Small engine fixes found on the way: logging refused logins (vanilla's lines). **Where it stands (2026-10-16):** items 1 to 4 are built and in review (section 11). What's left for the 23rd: From 13cd89578a3ef14d9e74ac8be81a3032aad699c6 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 18:46:40 +0200 Subject: [PATCH 12/12] Terrain: occlusion culling of solid sections (Vulkan) Sections that terrain in front hides entirely are drawn with an instance count of 0. Phase 1 draws the sections the last test read back saw; in the same pass every other section's box (and an eighth of phase 1's) is drawn against phase 1's depth, front faces only, with a fragment shader forced past an early depth test that marks the section seen. Phase 2, in a second pass, draws what the test saw. The test is exact per pixel and needs no depth read or Hi-Z, and every solid draw stays one entry of one list, so the CPU records as many draws as before (Mesa unrolls a multi-draw per entry on Intel Gen9). Culling pauses while the last result hid under a tenth of the solid indices, with one frame in 64 testing everything. HD 620 at RD16: +23-42% FPS in five of perf-frame-6's six views, the peak view unchanged; RD32: +14-109%. Pixels unchanged, also on camera jumps. Vulkan only: naga's MSL has no early fragment tests. HORIZON_GPU_CULL=0 turns it off, `all` culls every frame; `section-cull on|off` in a game. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/control.rs | 11 + .../horizon-client/src/game/render_control.rs | 1 + crates/horizon-client/src/gfx.rs | 186 ++++++- crates/horizon-client/src/gfx/cull.rs | 511 ++++++++++++++++++ crates/horizon-client/src/gfx/cull.wgsl | 101 ++++ 5 files changed, 800 insertions(+), 10 deletions(-) create mode 100644 crates/horizon-client/src/gfx/cull.rs create mode 100644 crates/horizon-client/src/gfx/cull.wgsl diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index 0183559ef..3e0711aeb 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -197,6 +197,16 @@ impl Game<'_> { } Ok(Some(json!({ "ok": true, "flat": r.flat_draws() }))) } + "section-cull" => { + let r = self.renderer.as_mut().ok_or("no renderer")?; + match words.get(1).copied() { + None => {} + Some("on") => r.set_section_cull(true)?, + Some("off") => r.set_section_cull(false)?, + Some(other) => return Err(format!("unknown section cull mode {other}").into()), + } + Ok(Some(json!({ "ok": true, "cull": r.section_cull() }))) + } "sculk" => Ok(Some(self.sculk_command(&words)?)), "status" => Ok(Some(self.control_status())), "pad" => Ok(Some(self.pad_command(&words)?)), @@ -714,6 +724,7 @@ impl Game<'_> { "drawn_sections": r.drawn_sections, "section_draws": r.section_draws, "section_draw_calls": r.section_draw_calls, + "culled_sections": r.culled_sections(), "uploaded_bytes": r.uploaded_bytes, "resident_sections": r.resident_sections(), "arena_bytes": arena_bytes, diff --git a/crates/horizon-client/src/game/render_control.rs b/crates/horizon-client/src/game/render_control.rs index c8355a395..31a7a33a0 100644 --- a/crates/horizon-client/src/game/render_control.rs +++ b/crates/horizon-client/src/game/render_control.rs @@ -12,6 +12,7 @@ pub const HELP: &[&str] = &[ "particle NAME X Y Z [DX DY DZ] [COUNT] spawn particles (vanilla /particle spread)", "signedit X Y Z [front|back] open the sign editor for the sign at X Y Z", "section-draws [flat|indirect] how terrain sections are drawn (A/B checks); replies the mode", + "section-cull [on|off] occlusion culling of solid sections (A/B checks); replies the mode", ]; impl Game<'_> { diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index d3b71bc60..8481fdcab 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -58,6 +58,7 @@ pub fn outline_vertices(edges: &[[f64; 6]], camera: glam::DVec3) -> Vec<[f32; 6] } mod arena; +mod cull; mod flat; mod java_stages; mod occlusion; @@ -270,6 +271,9 @@ pub struct Renderer { flat: Option, /// `flat` draws this frame's sections (off: the per-section draws). flat_on: bool, + /// Occlusion culling of solid section draws, where multi-draw is native. + cull: Option, + cull_on: bool, sky: wgpu::RenderPipeline, dark_disc: wgpu::RenderPipeline, lines: wgpu::RenderPipeline, @@ -365,6 +369,10 @@ pub struct Renderer { draws: Vec<[u32; 5]>, /// Vertex arena page and optional sorted index arena page of each draw. draw_pages: Vec<(u32, Option)>, + /// Solid draws left to occlusion culling's phase 2, while the list is + /// built, and their pages. + phase2_draws: Vec<[u32; 5]>, + phase2_pages: Vec<(u32, Option)>, /// Block-entity blocks per meshed section (world position, state id). block_entities: HashMap>, /// Changes whenever `block_entities` does (unique across renderers). @@ -654,6 +662,7 @@ impl Renderer { // Mappable primary buffers: section pages that no // longer fit in video memory go to system memory. required_features: timing::GpuTimer::features(&adapter) + | cull::Cull::features(&adapter) | (adapter.features() & (wgpu::Features::INDIRECT_FIRST_INSTANCE | wgpu::Features::MAPPABLE_PRIMARY_BUFFERS)), @@ -1100,7 +1109,9 @@ impl Renderer { let instances = device.create_buffer(&wgpu::BufferDescriptor { label: Some("Section origins"), size: instance_capacity * 16, - usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, + usage: wgpu::BufferUsages::VERTEX + | wgpu::BufferUsages::COPY_DST + | wgpu::BufferUsages::STORAGE, mapped_at_creation: false, }); let outline = device.create_buffer(&wgpu::BufferDescriptor { @@ -1745,6 +1756,15 @@ impl Renderer { let multi_draw = device .features() .contains(wgpu::Features::INDIRECT_FIRST_INSTANCE); + // HORIZON_GPU_CULL=0 turns occlusion culling off, `all` culls every + // frame however little it hides (A/B checks). + let cull_env = std::env::var("HORIZON_GPU_CULL"); + let cull = (cull_env.as_deref() != Ok("0") && cull::Cull::supported(&adapter, &device)) + .then(|| { + let mut cull = cull::Cull::new(&device, config.format.remove_srgb_suffix()); + cull.always = cull_env.as_deref() == Ok("all"); + cull + }); let world_post = world_post::Post::new(&device, config.format.remove_srgb_suffix()); let uploads = upload::Uploads::new(&device); let section_uploads = upload::Uploads::with_chunk(&device, SECTION_UPLOAD_CHUNK); @@ -1772,6 +1792,8 @@ impl Renderer { solid, translucent, flat_on: flat.is_some(), + cull_on: cull.is_some(), + cull, flat, sky, dark_disc, @@ -1845,6 +1867,8 @@ impl Renderer { origins: Vec::new(), draws: Vec::new(), draw_pages: Vec::new(), + phase2_draws: Vec::new(), + phase2_pages: Vec::new(), block_entities: HashMap::new(), block_entity_revision: next_block_entity_revision(), occlusion: occlusion::Occlusion::new(crate::chunk::HEIGHT / 16), @@ -1998,6 +2022,27 @@ impl Renderer { self.flat_on = on; Ok(()) } + /// Whether solid sections are occlusion culled (`cull`). + pub fn section_cull(&self) -> bool { + self.cull_on + } + /// Switches occlusion culling, for A/B checks. + pub fn set_section_cull(&mut self, on: bool) -> Result<()> { + if on && self.cull.is_none() { + return Err("occlusion culling is off on this device".into()); + } + self.cull_on = on; + Ok(()) + } + /// Of the boxes occlusion culling's last test read back drew, how many + /// it did not see and how many it drew, and the share of solid indices + /// it hid. + pub fn culled_sections(&self) -> Option<([usize; 2], f32)> { + self.cull + .as_ref() + .filter(|_| self.cull_on) + .map(|c| c.hidden) + } pub fn section_count(&self) -> usize { self.sections.len() } @@ -2117,7 +2162,15 @@ impl Renderer { /// origins bound: one indirect multi-draw, or a loop without it. /// Consecutive draws in one arena page share a vertex buffer binding, /// keeping the list's order (translucent sections go back to front). - fn draw_sections(&self, pass: &mut wgpu::RenderPass<'_>, first: u32, count: u32) { + /// `indirect` holds the list's draws from `base`. + fn draw_sections( + &self, + pass: &mut wgpu::RenderPass<'_>, + indirect: &wgpu::Buffer, + base: u32, + first: u32, + count: u32, + ) { let (mut i, end) = (first as usize, (first + count) as usize); while i < end { let (page, index_page) = self.draw_pages[i]; @@ -2129,7 +2182,8 @@ impl Renderer { let indices = index_page.map_or(&self.indices, |p| self.sorted_indices.buffer(p)); pass.set_index_buffer(indices.slice(..), wgpu::IndexFormat::Uint32); if self.multi_draw { - pass.multi_draw_indexed_indirect(&self.indirect, i as u64 * 20, run as u32); + let offset = (i - base as usize) as u64 * 20; + pass.multi_draw_indexed_indirect(indirect, offset, run as u32); } else { for d in &self.draws[i..i + run] { pass.draw_indexed(d[2]..d[2] + d[0], d[3] as i32, d[4]..d[4] + 1); @@ -2574,7 +2628,9 @@ impl Renderer { self.instances = self.device.create_buffer(&wgpu::BufferDescriptor { label: Some("Section origins"), size: self.instance_capacity * 16, - usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, + usage: wgpu::BufferUsages::VERTEX + | wgpu::BufferUsages::COPY_DST + | wgpu::BufferUsages::STORAGE, mapped_at_creation: false, }); } @@ -2603,15 +2659,48 @@ impl Renderer { let eye = (frame.bob * crate::camera::view_matrix(frame.yaw, frame.pitch)) .inverse() .transform_point3(Vec3::ZERO); + // Occlusion culling leaves the sections its last result did not see + // to phase 2, after the others, with instance counts of 0. + let mut cull = self + .cull + .as_mut() + .filter(|_| { + self.cull_on + && !frame.title + && self.multi_draw + && !(self.flat_on && self.flat.is_some()) + }) + .and_then(|cull| cull.begin(&self.device).then_some(cull)); + self.phase2_draws.clear(); + self.phase2_pages.clear(); for (i, v) in visible.iter().enumerate() { let s = self.sections.get(v.slot); + let first_phase = cull.as_ref().is_none_or(|c| c.first(v.slot)); + let (draws, pages) = if first_phase { + (&mut self.draws, &mut self.draw_pages) + } else { + (&mut self.phase2_draws, &mut self.phase2_pages) + }; + let start = draws.len(); + let instances = u32::from(first_phase); + let mut indices = 0; // Only the solid groups that can face the eye. s.faces.ranges(eye - Vec3::from(v.origin), |first, quads| { let first = s.start + u64::from(first) * 4; - self.draws.push([quads * 6, 1, 0, first as u32, i as u32]); - self.draw_pages.push((s.page, None)); + draws.push([quads * 6, instances, 0, first as u32, i as u32]); + pages.push((s.page, None)); + indices += quads * 6; }); + match &mut cull { + Some(c) if first_phase => c.drawn(i, v.slot, indices), + Some(c) => c.deferred(i, v.slot, start, draws.len() - start, indices), + None => {} + } } + let phase1_draws = self.draws.len() as u32; + self.draws.extend_from_slice(&self.phase2_draws); + self.draw_pages.extend_from_slice(&self.phase2_pages); + let culling = cull.is_some(); let solid_draws = self.draws.len() as u32; // LevelRenderer: nearby sections each frame, plus a rotating eighth // (at least 15) of the visible list. Cached views avoid needless sorts. @@ -2661,6 +2750,7 @@ impl Renderer { (0..self.draws.len()) .filter(|&i| { i == 0 + || i == phase1_draws as usize || i == solid_draws as usize || self.draw_pages[i] != self.draw_pages[i - 1] }) @@ -2874,6 +2964,16 @@ impl Renderer { 0, bytemuck::cast_slice(&self.draws), ); + if culling && let Some(cull) = &mut self.cull { + cull.upload( + &self.device, + &mut self.uploads, + &mut encoder, + &self.draws[phase1_draws as usize..solid_draws as usize], + &self.instances, + &self.globals, + ); + } } let view_target = target_texture.create_view(&wgpu::TextureViewDescriptor { format: Some(self.config.format.remove_srgb_suffix()), @@ -2891,6 +2991,7 @@ impl Renderer { Some((color, depth)) => (color, depth), None => (&view_target, &self.depth), }; + let cull = self.cull.as_ref().filter(|_| culling && solid_draws > 0); { let attachments = [Some(wgpu::RenderPassColorAttachment { view: world_view, @@ -2919,8 +3020,9 @@ impl Renderer { view: world_depth, depth_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Clear(0.0), - // A shader pack reads the world's depth. - store: if pack { + // A shader pack reads the world's depth, and + // phase 2 of occlusion culling draws over it. + store: if pack || cull.is_some() { wgpu::StoreOp::Store } else { wgpu::StoreOp::Discard @@ -2970,7 +3072,59 @@ impl Renderer { pass.set_pipeline(&self.solid); pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); pass.set_vertex_buffer(1, self.instances.slice(..)); - self.draw_sections(&mut pass, 0, solid_draws); + match cull { + Some(cull) => { + // Phase 1 and the boxes against its depth, then + // what they found in sight, in a pass that loads + // both targets (it reads the test's results). + self.draw_sections(&mut pass, &self.indirect, 0, 0, phase1_draws); + cull.test(&mut pass); + drop(pass); + let attachments = [Some(wgpu::RenderPassColorAttachment { + view: world_view, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Load, + store: wgpu::StoreOp::Store, + }, + })]; + pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Horizon world, after culling"), + color_attachments: &attachments, + depth_stencil_attachment: Some( + wgpu::RenderPassDepthStencilAttachment { + view: world_depth, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Load, + store: if pack { + wgpu::StoreOp::Store + } else { + wgpu::StoreOp::Discard + }, + }), + stencil_ops: None, + }, + ), + ..Default::default() + }); + pass.set_bind_group(0, &self.group, &[]); + pass.set_pipeline(&self.solid); + pass.set_index_buffer( + self.indices.slice(..), + wgpu::IndexFormat::Uint32, + ); + pass.set_vertex_buffer(1, self.instances.slice(..)); + self.draw_sections( + &mut pass, + &cull.phase2, + phase1_draws, + phase1_draws, + solid_draws - phase1_draws, + ); + } + None => self.draw_sections(&mut pass, &self.indirect, 0, 0, solid_draws), + } } } if entity_quads > 0 { @@ -3022,7 +3176,13 @@ impl Renderer { pass.set_pipeline(&self.translucent); pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); pass.set_vertex_buffer(1, self.instances.slice(..)); - self.draw_sections(&mut pass, solid_draws, translucent_draws); + self.draw_sections( + &mut pass, + &self.indirect, + 0, + solid_draws, + translucent_draws, + ); } } // LevelRenderer.executeClassicTransparency: translucent targets' @@ -3236,6 +3396,9 @@ impl Renderer { frame.hud, [self.config.width, self.config.height], ); + if culling && let Some(cull) = &mut self.cull { + cull.end(&self.device, &mut encoder); + } if let Some(timer) = &mut self.gpu_timer { timer.end(&mut encoder); } @@ -3250,6 +3413,9 @@ impl Renderer { } let commands = crate::spans::time("encode", || encoder.finish()); let submission = crate::spans::time("submit", || self.queue.submit([commands])); + if let Some(cull) = &mut self.cull { + cull.submitted(); + } if let Some(timer) = &mut self.gpu_timer { timer.submitted(); timer.collect(&self.device, &mut self.gpu_times); diff --git a/crates/horizon-client/src/gfx/cull.rs b/crates/horizon-client/src/gfx/cull.rs new file mode 100644 index 000000000..6926aab70 --- /dev/null +++ b/crates/horizon-client/src/gfx/cull.rs @@ -0,0 +1,511 @@ +//! Occlusion culling of solid section draws, in two phases. +//! +//! Frustum and cave culling leave many sections that terrain in front hides +//! entirely (about half of the solid vertices drawn from the ground at render +//! distance 16, more at 32), and drawing fewer vertices is what helps an +//! integrated GPU. Phase 1 draws the sections that the last test read back +//! saw. Then, in the same pass, the box of every other section (and of a +//! share of phase 1's, to notice when they go out of sight) is drawn against +//! phase 1's depth with no writes but to the test's results. Phase 2, in a +//! second pass, draws the sections whose box was seen; the others' draws have +//! an instance count of 0. +//! +//! The picture stays the one without culling: the boxes (grown by a block, +//! the farthest a block model reaches out of its cell) are drawn front faces +//! only, so a fragment is the box's nearest point on its pixel, and the +//! fragment shader runs only where the depth test passes (forced early), so a +//! section is left out only where all of its fragments would fail the depth +//! test against the final depth, which is only nearer. What the CPU knows of +//! the results only sorts sections between the phases: a stale result draws +//! a hidden section in phase 1, or tests a section that is in sight. +//! +//! Every solid draw is an entry of one of the two lists, so the CPU records +//! as many draws as without culling: drivers that unroll an indirect +//! multi-draw on the CPU (Mesa's on Intel Gen9) pay per entry. Phase 2's +//! counts come from the GPU, which needs a native multi-draw and forced +//! early depth tests: Vulkan. Translucent draws are not culled. +use std::num::NonZeroU64; +use std::sync::{ + Arc, + atomic::{AtomicU8, Ordering}, +}; + +const WORDS: u64 = 5 * 4; +/// Phase 1's sections are each tested every `RETEST` frames. +const RETEST: u32 = 8; +/// Culling runs while the last result read back hid at least this share of +/// the frame's solid indices: below it the test and the second pass cost +/// more than they save. Else one frame in `PROBE` tests every section. +const MIN_SHARE: f32 = 0.1; +const PROBE: u32 = 64; +/// Boxes over more than this share of the screen count as seen untested. +const BIG: f32 = 0.25; +/// Results read back at once, some frames late so the CPU never waits. +const READBACKS: usize = 3; +const FREE: u8 = 0; +const PENDING: u8 = 1; +const MAPPED: u8 = 2; + +#[repr(C)] +#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] +struct Params { + stamp: u32, + big: f32, + pad: [u32; 2], +} + +/// One frame's results on their way back. +struct Readback { + buffer: wgpu::Buffer, + capacity: u64, + state: Arc, + stamp: u32, + /// The resident slot of each test, and its section's solid indices. + slots: Vec, + indices: Vec, + /// The frame's solid indices. + total: u64, +} + +/// The bind group and the buffers from outside it was made with. +struct Group { + origins: wgpu::Buffer, + globals: wgpu::Buffer, + group: wgpu::BindGroup, +} + +pub struct Cull { + pipeline: wgpu::RenderPipeline, + layout: wgpu::BindGroupLayout, + group: Option, + params: wgpu::Buffer, + /// Phase 2's draws: the solid draws after phase 1's in the frame's list. + pub phase2: wgpu::Buffer, + phase2_capacity: u64, + tests: wgpu::Buffer, + tests_capacity: u64, + /// This frame's tests: visible index, first phase 2 draw, phase 2 draws. + test_data: Vec<[u32; 4]>, + test_slots: Vec, + test_indices: Vec, + total: u64, + /// The frame tests every section, as culling was off. + probe: bool, + results: wgpu::Buffer, + readbacks: Vec, + next: usize, + current: Option, + /// Per resident slot: whether the last result read back saw it. + seen: Vec, + stamp: u32, + /// Of the boxes the last test read back drew, how many it did not see + /// and how many it drew, and the share of solid indices hidden. + pub hidden: ([usize; 2], f32), + /// Cull every frame, whatever the hidden share (A/B checks). + pub always: bool, +} + +fn buffer( + device: &wgpu::Device, + label: &str, + size: u64, + usage: wgpu::BufferUsages, +) -> wgpu::Buffer { + device.create_buffer(&wgpu::BufferDescriptor { + label: Some(label), + size, + usage, + mapped_at_creation: false, + }) +} + +const PHASE2_USAGE: wgpu::BufferUsages = wgpu::BufferUsages::INDIRECT + .union(wgpu::BufferUsages::STORAGE) + .union(wgpu::BufferUsages::COPY_DST); +const TESTS_USAGE: wgpu::BufferUsages = + wgpu::BufferUsages::STORAGE.union(wgpu::BufferUsages::COPY_DST); +const RESULTS_USAGE: wgpu::BufferUsages = + wgpu::BufferUsages::STORAGE.union(wgpu::BufferUsages::COPY_SRC); +const READBACK_USAGE: wgpu::BufferUsages = + wgpu::BufferUsages::MAP_READ.union(wgpu::BufferUsages::COPY_DST); + +impl Cull { + /// The device feature culling needs, where the adapter has it. + pub fn features(adapter: &wgpu::Adapter) -> wgpu::Features { + adapter.features() & wgpu::Features::SHADER_EARLY_DEPTH_TEST + } + /// Whether the device draws an indirect multi-draw natively and runs + /// fragment shaders that write storage only past an early depth test. + pub fn supported(adapter: &wgpu::Adapter, device: &wgpu::Device) -> bool { + adapter.get_info().backend == wgpu::Backend::Vulkan + && device.features().contains( + wgpu::Features::SHADER_EARLY_DEPTH_TEST | wgpu::Features::INDIRECT_FIRST_INSTANCE, + ) + && adapter.get_downlevel_capabilities().flags.contains( + wgpu::DownlevelFlags::VERTEX_STORAGE + | wgpu::DownlevelFlags::FRAGMENT_WRITABLE_STORAGE, + ) + } + /// `color` is the world pass's colour format. + pub fn new(device: &wgpu::Device, color: wgpu::TextureFormat) -> Self { + let entry = |binding, visibility, ty| wgpu::BindGroupLayoutEntry { + binding, + visibility, + ty, + count: None, + }; + let storage = |read_only| wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Storage { read_only }, + has_dynamic_offset: false, + min_binding_size: None, + }; + let uniform = wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }; + let both = wgpu::ShaderStages::VERTEX_FRAGMENT; + let vertex = wgpu::ShaderStages::VERTEX; + let fragment = wgpu::ShaderStages::FRAGMENT; + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Section culling"), + entries: &[ + entry(0, both, storage(true)), + entry(1, fragment, storage(false)), + entry(2, fragment, storage(false)), + entry(3, vertex, storage(true)), + entry(4, vertex, uniform), + entry(5, both, uniform), + ], + }); + let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("Section culling"), + source: wgpu::ShaderSource::Wgsl(include_str!("cull.wgsl").into()), + }); + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("Section culling"), + bind_group_layouts: &[Some(&layout)], + ..Default::default() + }); + let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("Section culling"), + layout: Some(&pipeline_layout), + vertex: wgpu::VertexState { + module: &module, + entry_point: Some("vs_box"), + compilation_options: Default::default(), + buffers: &[], + }, + primitive: wgpu::PrimitiveState { + cull_mode: Some(wgpu::Face::Back), + ..Default::default() + }, + // The world pass's reversed depth test, without writes. + depth_stencil: Some(wgpu::DepthStencilState { + format: wgpu::TextureFormat::Depth32Float, + depth_write_enabled: Some(false), + depth_compare: Some(wgpu::CompareFunction::GreaterEqual), + stencil: Default::default(), + bias: Default::default(), + }), + multisample: Default::default(), + fragment: Some(wgpu::FragmentState { + module: &module, + entry_point: Some("fs_box"), + compilation_options: Default::default(), + targets: &[Some(wgpu::ColorTargetState { + format: color, + blend: None, + write_mask: wgpu::ColorWrites::empty(), + })], + }), + multiview_mask: None, + cache: None, + }); + Self { + pipeline, + layout, + group: None, + params: buffer( + device, + "Section culling parameters", + std::mem::size_of::() as u64, + wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + ), + phase2: buffer(device, "Section draws, phase 2", WORDS, PHASE2_USAGE), + phase2_capacity: 1, + tests: buffer(device, "Section tests", 16, TESTS_USAGE), + tests_capacity: 1, + test_data: Vec::new(), + test_slots: Vec::new(), + test_indices: Vec::new(), + total: 0, + probe: false, + results: buffer(device, "Section test results", 4, RESULTS_USAGE), + readbacks: (0..READBACKS) + .map(|_| Readback { + buffer: buffer(device, "Section test readback", 4, READBACK_USAGE), + capacity: 1, + state: Arc::new(AtomicU8::new(FREE)), + stamp: 0, + slots: Vec::new(), + indices: Vec::new(), + total: 0, + }) + .collect(), + next: 0, + current: None, + seen: Vec::new(), + stamp: 0, + hidden: ([0; 2], 1.0), + always: false, + } + } + + /// Start a frame: take in the results that came back, and tell whether + /// the frame culls. + pub fn begin(&mut self, device: &wgpu::Device) -> bool { + let _ = device.poll(wgpu::PollType::Poll); + for i in 0..READBACKS { + let r = &mut self.readbacks[(self.next + i) % READBACKS]; + if r.state.load(Ordering::Acquire) != MAPPED { + continue; + } + let mapped = r.buffer.slice(..r.slots.len() as u64 * 4); + if let Ok(data) = mapped.get_mapped_range() { + let results: &[u32] = bytemuck::cast_slice(&data); + let (mut hidden, mut indices) = (0, 0u64); + for (&result, &n) in results.iter().zip(&r.indices) { + if result != r.stamp { + hidden += 1; + indices += u64::from(n); + } + } + let share = indices as f32 / r.total.max(1) as f32; + self.hidden = ([hidden, results.len()], share); + for (&slot, &result) in r.slots.iter().zip(results) { + let slot = slot as usize; + if slot >= self.seen.len() { + self.seen.resize(slot + 1, false); + } + self.seen[slot] = result == r.stamp; + } + } + r.buffer.unmap(); + r.state.store(FREE, Ordering::Release); + } + self.stamp = self.stamp.wrapping_add(1).max(1); + self.test_data.clear(); + self.test_slots.clear(); + self.test_indices.clear(); + self.total = 0; + self.probe = !self.always && self.hidden.1 < MIN_SHARE; + !self.probe || self.stamp.is_multiple_of(PROBE) + } + + /// Whether phase 1 draws the section in resident `slot`. + pub fn first(&self, slot: u32) -> bool { + self.seen.get(slot as usize).copied().unwrap_or(false) + } + + /// A section phase 1 draws, `index` in the visible list, with `indices` + /// solid indices: tested in turn. + pub fn drawn(&mut self, index: usize, slot: u32, indices: u32) { + self.total += u64::from(indices); + if indices > 0 && (self.probe || slot.wrapping_add(self.stamp).is_multiple_of(RETEST)) { + self.push([index as u32, 0, 0, 0], slot, indices); + } + } + + /// A section left to phase 2: its `draws` from phase 2's `first`. + pub fn deferred(&mut self, index: usize, slot: u32, first: usize, draws: usize, indices: u32) { + self.total += u64::from(indices); + if draws > 0 { + self.push([index as u32, first as u32, draws as u32, 0], slot, indices); + } + } + + fn push(&mut self, test: [u32; 4], slot: u32, indices: u32) { + self.test_data.push(test); + self.test_slots.push(slot); + self.test_indices.push(indices); + } + + /// Stage this frame's inputs: phase 2's draws, with instance counts of 0, + /// and the tests, which read the frame's camera-relative `origins` and + /// view projection (the start of `globals`). + pub fn upload( + &mut self, + device: &wgpu::Device, + uploads: &mut super::upload::Uploads, + encoder: &mut wgpu::CommandEncoder, + phase2: &[[u32; 5]], + origins: &wgpu::Buffer, + globals: &wgpu::Buffer, + ) { + let draws = phase2.len() as u64; + if draws > self.phase2_capacity { + self.phase2_capacity = draws.next_power_of_two(); + self.phase2 = buffer( + device, + "Section draws, phase 2", + self.phase2_capacity * WORDS, + PHASE2_USAGE, + ); + self.group = None; + } + let tests = self.test_data.len() as u64; + if tests > self.tests_capacity { + self.tests_capacity = tests.next_power_of_two(); + self.tests = buffer( + device, + "Section tests", + self.tests_capacity * 16, + TESTS_USAGE, + ); + self.results = buffer( + device, + "Section test results", + self.tests_capacity * 4, + RESULTS_USAGE, + ); + self.group = None; + } + if draws > 0 { + uploads.buffer(encoder, &self.phase2, 0, bytemuck::cast_slice(phase2)); + } + if tests > 0 { + uploads.buffer( + encoder, + &self.tests, + 0, + bytemuck::cast_slice(&self.test_data), + ); + } + let params = Params { + stamp: self.stamp, + big: BIG, + pad: [0; 2], + }; + uploads.buffer(encoder, &self.params, 0, bytemuck::bytes_of(¶ms)); + if !self + .group + .as_ref() + .is_some_and(|g| g.origins == *origins && g.globals == *globals) + { + self.group = Some(self.make_group(device, origins, globals)); + } + } + + /// In the world pass after phase 1: draw the tested boxes against its + /// depth. Leaves the pass's pipeline and group 0 changed. + pub fn test(&self, pass: &mut wgpu::RenderPass<'_>) { + let Some(Group { group, .. }) = &self.group else { + return; + }; + if self.test_data.is_empty() { + return; + } + pass.set_pipeline(&self.pipeline); + pass.set_bind_group(0, group, &[]); + pass.draw(0..36, 0..self.test_data.len() as u32); + } + + /// After the world passes: copy the results for reading back, when a + /// readback buffer is free. + pub fn end(&mut self, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder) { + let tests = self.test_data.len() as u64; + let r = &mut self.readbacks[self.next]; + if tests == 0 || r.state.load(Ordering::Acquire) != FREE { + return; + } + if tests > r.capacity { + r.capacity = tests.next_power_of_two(); + r.buffer = buffer( + device, + "Section test readback", + r.capacity * 4, + READBACK_USAGE, + ); + } + encoder.copy_buffer_to_buffer(&self.results, 0, &r.buffer, 0, tests * 4); + r.stamp = self.stamp; + r.slots.clear(); + r.slots.extend_from_slice(&self.test_slots); + r.indices.clear(); + r.indices.extend_from_slice(&self.test_indices); + r.total = self.total; + self.current = Some(self.next); + } + + /// After the frame's submission: start reading its results back. + pub fn submitted(&mut self) { + let Some(i) = self.current.take() else { + return; + }; + let r = &self.readbacks[i]; + let state = r.state.clone(); + state.store(PENDING, Ordering::Release); + r.buffer + .slice(..r.slots.len() as u64 * 4) + .map_async(wgpu::MapMode::Read, move |result| { + state.store( + if result.is_ok() { MAPPED } else { FREE }, + Ordering::Release, + ); + }); + self.next = (i + 1) % READBACKS; + } + + fn make_group( + &self, + device: &wgpu::Device, + origins: &wgpu::Buffer, + globals: &wgpu::Buffer, + ) -> Group { + fn whole(binding: u32, buffer: &wgpu::Buffer) -> wgpu::BindGroupEntry<'_> { + wgpu::BindGroupEntry { + binding, + resource: buffer.as_entire_binding(), + } + } + let group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Section culling"), + layout: &self.layout, + entries: &[ + whole(0, &self.tests), + whole(1, &self.results), + whole(2, &self.phase2), + whole(3, origins), + wgpu::BindGroupEntry { + binding: 4, + resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { + buffer: globals, + offset: 0, + size: NonZeroU64::new(64), + }), + }, + whole(5, &self.params), + ], + }); + Group { + origins: origins.clone(), + globals: globals.clone(), + group, + } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn shader_validates() { + let module = naga::front::wgsl::parse_str(include_str!("cull.wgsl")).unwrap(); + naga::valid::Validator::new( + naga::valid::ValidationFlags::all(), + naga::valid::Capabilities::all(), + ) + .validate(&module) + .unwrap(); + } +} diff --git a/crates/horizon-client/src/gfx/cull.wgsl b/crates/horizon-client/src/gfx/cull.wgsl new file mode 100644 index 000000000..faa495caf --- /dev/null +++ b/crates/horizon-client/src/gfx/cull.wgsl @@ -0,0 +1,101 @@ +// Occlusion culling of solid section draws (gfx/cull.rs). + +struct Params { + // This frame's test: a test that sees its box stores the number. + stamp: u32, + // Boxes covering more than this share of the screen count as seen. + big: f32, +} +// The leading member of terrain.wgsl's Globals. +struct Globals { + view_proj: mat4x4, +} + +// One test per section: its visible index (which names its origin), its +// first draw in `phase2` and how many draws it has there (0: phase 1 drew +// it, and the test only tells whether it is still in sight). +@group(0) @binding(0) var tests: array>; +// Per test: the stamp of the frame whose test saw the box. +@group(0) @binding(1) var results: array; +// Phase 2's draws, 5 words each, with instance counts of 0 until seen. +@group(0) @binding(2) var phase2: array; +// Camera-relative origin of each visible section. +@group(0) @binding(3) var origins: array>; +@group(0) @binding(4) var globals: Globals; +@group(0) @binding(5) var params: Params; + +// The corners (bit 0: x, bit 1: y, bit 2: z) of the box's 12 triangles, +// counter-clockwise seen from outside. +const CORNERS = array( + 0u, 6u, 2u, 0u, 4u, 6u, 1u, 3u, 7u, 1u, 7u, 5u, 0u, 1u, 5u, 0u, 5u, 4u, + 2u, 7u, 3u, 2u, 6u, 7u, 0u, 3u, 1u, 0u, 2u, 3u, 4u, 5u, 7u, 4u, 7u, 6u, +); + +struct Box { + @builtin(position) position: vec4, + @location(0) @interpolate(flat) test: u32, +} + +fn corner(origin: vec3, c: u32) -> vec3 { + return origin + vec3( + select(-1.0, 17.0, (c & 1u) != 0u), + select(-1.0, 17.0, (c & 2u) != 0u), + select(-1.0, 17.0, (c & 4u) != 0u), + ); +} + +// Each tested section's box, grown by a block (block models reach one block +// out of their cell), drawn after phase 1 against its depth with back faces +// culled: a front face fragment that passes the depth test is the box's +// nearest point on that pixel in front of phase 1's depth, so the section +// may show there. A box failing everywhere would have every fragment of its +// section fail the depth test against the final depth, which is only nearer. +@vertex +fn vs_box(@builtin(vertex_index) v: u32, @builtin(instance_index) t: u32) -> Box { + let origin = origins[tests[t].x].xyz; + var out: Box; + out.test = t; + // The box's screen rectangle, from the corners in front of the camera. + var lo = vec2(1e30); + var hi = vec2(-1e30); + var behind = false; + for (var c = 0u; c < 8u; c++) { + let p = globals.view_proj * vec4(corner(origin, c), 1.0); + behind = behind || p.w <= 0.0; + let q = p.xy / max(p.w, 1e-6); + lo = min(lo, q); + hi = max(hi, q); + } + let size = max(min(hi, vec2(1.0)) - max(lo, vec2(-1.0)), vec2(0.0)); + // A box within a block of the camera may cross the near plane, which + // clips its front faces away, and a box over a large part of the screen + // is nearly always in sight and costly to test: both count as seen, + // through a small triangle in the top left corner at the nearest depth. + let near = all(origin < vec3(2.0)) && all(origin > vec3(-18.0)); + if near || (!behind && size.x * size.y > params.big * 4.0) { + var p = vec2(-1.0, 1.0); + if v == 1u { + p.y -= 0.05; + } else if v == 2u { + p.x += 0.05; + } + out.position = select(vec4(0.0, 0.0, 0.0, 1.0), vec4(p, 1.0, 1.0), v < 3u); + return out; + } + out.position = globals.view_proj * vec4(corner(origin, CORNERS[v]), 1.0); + return out; +} + +// Runs only for fragments that pass the depth test (forced early). +@fragment +@early_depth_test(force) +fn fs_box(in: Box) -> @location(0) vec4 { + if results[in.test] != params.stamp { + results[in.test] = params.stamp; + let test = tests[in.test]; + for (var k = 0u; k < test.z; k++) { + phase2[(test.y + k) * 5u + 1u] = 1u; + } + } + return vec4(0.0); +}