From 30cdd6298a6b13b51a34d59b30b80825f11ae7ef Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 01:20:13 +0200 Subject: [PATCH 001/124] Version sources: a release build says its version (0.2.3), not 0.1.0 or a folder name release.yml builds every game release with HORIZON_VERSION, the tag without its v (docs/versioning.md), as launcher.yml already does for the launcher: no version-bump commits. - horizon_core::release::{RELEASE, VERSION}: the built-in version, or Cargo.toml's for a build from source. (horizon_core::version stays Minecraft's, generated by gen_version_consts.py.) - horizon-server and horizon-prepare --version, the server's /api/v1/status, horizon-ctl's MCP serverInfo and the "Horizon/" user agents use it; they all said 0.1.0. - The client's crash reports and telemetry say v0.2.3 from the built-in version wherever the game is installed; releases from before 0.2 (no version built in) still read the launcher's folder name. - The launcher's benchmark telemetry used Cargo.toml's version instead of the release's. - release.yml checks that horizon-prepare and horizon-server --version say the release's version. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/release.yml | 17 ++++++++-- crates/horizon-client/src/auth.rs | 2 +- .../horizon-client/src/bin/horizon-ctl/mcp.rs | 2 +- crates/horizon-client/src/crash.rs | 29 +++++++++++++---- crates/horizon-client/src/telemetry.rs | 2 +- crates/horizon-core/src/lib.rs | 1 + crates/horizon-core/src/release.rs | 32 +++++++++++++++++++ crates/horizon-prepare/src/main.rs | 9 +++++- crates/horizon-server-files/src/profiles.rs | 4 +-- crates/horizon-server/src/api.rs | 2 +- crates/horizon-server/src/main.rs | 9 +++++- launcher/src/perf.rs | 4 +-- 12 files changed, 94 insertions(+), 19 deletions(-) create mode 100644 crates/horizon-core/src/release.rs diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 2c4e13cf2..d3631a851 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -44,6 +44,7 @@ jobs: outputs: release: ${{ steps.decide.outputs.release }} tag: ${{ steps.decide.outputs.tag }} + version: ${{ steps.decide.outputs.version }} head: ${{ steps.decide.outputs.head }} dry_run: ${{ steps.mode.outputs.dry_run }} steps: @@ -64,6 +65,9 @@ jobs: run: | python3 tools/release_train.py decide --kind game ${HEAD_SHA:+--head "$HEAD_SHA"} ${FORCE:+--force} | tee decision.json echo "### $(jq -r '.reason' decision.json)" >> "$GITHUB_STEP_SUMMARY" + # The version the programs are built with (docs/versioning.md): the tag without its v. + tag=$(jq -r '.tag' decision.json) + echo "version=${tag#v}" >> "$GITHUB_OUTPUT" # A dry run builds whatever the decision, to test the builds. if [[ "${{ steps.mode.outputs.dry_run }}" == true ]]; then echo "release=true" >> "$GITHUB_OUTPUT"; fi @@ -98,6 +102,9 @@ jobs: timeout-minutes: ${{ matrix.mac && 60 || 40 }} env: RUSTFLAGS: ${{ matrix.rustflags }} + # The release's version, built in (horizon_core::release): --version, user agents, crash reports and + # telemetry say it. No version-bump commits. + HORIZON_VERSION: ${{ needs.decide.outputs.version }} defaults: run: shell: bash @@ -161,9 +168,13 @@ jobs: bin=target/release "$bin/horizon-client${{ matrix.exe }}" --help | grep -q -- '--resources' "$bin/horizon-ctl${{ matrix.exe }}" --help > /dev/null - "$bin/horizon-prepare${{ matrix.exe }}" --version "$bin/horizon-prepare${{ matrix.exe }}" --help | grep -q -- '--minecraft-dir' - "$bin/horizon-server${{ matrix.exe }}" --version + # The version is the release's. + for binary in horizon-prepare horizon-server; do + out=$("$bin/$binary${{ matrix.exe }}" --version | tr -d '\r') + echo "$out" + [[ "$out" == "$binary $HORIZON_VERSION" ]] || { echo "::error::expected $binary $HORIZON_VERSION"; exit 1; } + done - name: Tools from the runner if: runner.environment == 'self-hosted' run: bash tools/ci/runner-env.sh @@ -244,7 +255,7 @@ jobs: 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 + [[ "$("$folder/horizon-server" --version)" == "horizon-server $HORIZON_VERSION" ]] cd dist rm -f "${{ matrix.server_asset }}" tar czf "${{ matrix.server_asset }}" "${{ matrix.server_dir }}" diff --git a/crates/horizon-client/src/auth.rs b/crates/horizon-client/src/auth.rs index a6dfab1ee..b899ea5d5 100644 --- a/crates/horizon-client/src/auth.rs +++ b/crates/horizon-client/src/auth.rs @@ -174,7 +174,7 @@ pub fn discovery_url() -> String { fn agent() -> ureq::Agent { ureq::AgentBuilder::new() .timeout(Duration::from_secs(15)) - .user_agent(concat!("Horizon/", env!("CARGO_PKG_VERSION"))) + .user_agent(&format!("Horizon/{}", horizon_core::release::VERSION)) .build() } diff --git a/crates/horizon-client/src/bin/horizon-ctl/mcp.rs b/crates/horizon-client/src/bin/horizon-ctl/mcp.rs index fa60b38f9..8d67953a5 100644 --- a/crates/horizon-client/src/bin/horizon-ctl/mcp.rs +++ b/crates/horizon-client/src/bin/horizon-ctl/mcp.rs @@ -130,7 +130,7 @@ impl Session { "initialize" => Ok(json!({ "protocolVersion": params.get("protocolVersion").and_then(Value::as_str).unwrap_or(PROTOCOL), "capabilities": { "tools": {} }, - "serverInfo": { "name": "horizon-ctl", "version": env!("CARGO_PKG_VERSION") }, + "serverInfo": { "name": "horizon-ctl", "version": horizon_core::release::VERSION }, "instructions": "Play Horizon, a Rust reimplementation of Minecraft Java Edition. Start with launch (it finds the game installed by the Horizon launcher and opens or creates a world), then read the world with state, inventory, screen, entities and find_block, and act with goto, look_at_block, break, place, interact, click_slot and click_button. Actions block until they finish and report what happened. Coordinates are block coordinates; yaw 0 faces south (+Z).", })), "ping" => Ok(json!({})), diff --git a/crates/horizon-client/src/crash.rs b/crates/horizon-client/src/crash.rs index 8ee5803b5..e147bfb4c 100644 --- a/crates/horizon-client/src/crash.rs +++ b/crates/horizon-client/src/crash.rs @@ -105,18 +105,23 @@ impl Report { } } -/// The release tag (the launcher installs each build in a folder named after it) and the client's -/// own version, or just the latter. +/// The release this binary is (`v0.2.3`); for a release from before 0.2, its tag and the client's +/// crate version; for a build from source, that version alone. pub fn version() -> String { let version = env!("CARGO_PKG_VERSION"); - match release_tag() { - Some(tag) => format!("{tag} (client {version})"), - None => version.to_owned(), + match (horizon_core::release::RELEASE, release_tag()) { + (Some(_), Some(tag)) => tag, + (None, Some(tag)) => format!("{tag} (client {version})"), + (_, None) => version.to_owned(), } } -/// The release this binary came with: launcher installs live in a folder named after the tag. +/// The release this binary came with: the version release.yml built in (docs/versioning.md), else +/// the folder the launcher installed it in, named after the tag (releases from before 0.2). pub fn release_tag() -> Option { + if let Some(version) = horizon_core::release::RELEASE { + return Some(format!("v{version}")); + } let exe = std::env::current_exe().ok()?; let folder = exe.parent()?.file_name()?.to_str()?; (folder.starts_with('v') && folder[1..].starts_with(|c: char| c.is_ascii_digit())) @@ -517,6 +522,18 @@ pub fn debug_crash_after() -> Option { mod tests { use super::*; + #[test] + fn a_release_reports_its_built_in_version() { + // release.yml builds with HORIZON_VERSION (docs/versioning.md): `v0.2.3`, wherever it is installed. + match horizon_core::release::RELEASE { + Some(version) => { + assert_eq!(release_tag(), Some(format!("v{version}"))); + assert_eq!(super::version(), format!("v{version}")); + } + None => assert!(release_tag().is_none() || super::version().contains("(client ")), + } + } + fn report() -> Report { Report { time: chrono::TimeZone::with_ymd_and_hms(&Local, 2026, 10, 14, 9, 5, 7).unwrap(), diff --git a/crates/horizon-client/src/telemetry.rs b/crates/horizon-client/src/telemetry.rs index d0140f941..2010df04a 100644 --- a/crates/horizon-client/src/telemetry.rs +++ b/crates/horizon-client/src/telemetry.rs @@ -75,7 +75,7 @@ pub fn system() -> System { system } -/// The release (`v2026.10.15`), or the client's crate version for a build from source. +/// The release (`v0.2.3`), or the client's crate version for a build from source. pub fn version() -> String { crate::crash::release_tag().unwrap_or_else(|| format!("dev {}", env!("CARGO_PKG_VERSION"))) } diff --git a/crates/horizon-core/src/lib.rs b/crates/horizon-core/src/lib.rs index c8077b327..9034ef2eb 100644 --- a/crates/horizon-core/src/lib.rs +++ b/crates/horizon-core/src/lib.rs @@ -17,6 +17,7 @@ pub mod storage; pub mod text; pub mod ticks; +pub mod release; pub mod version; pub use version::VANILLA_VERSION; diff --git a/crates/horizon-core/src/release.rs b/crates/horizon-core/src/release.rs new file mode 100644 index 000000000..54a3cb94e --- /dev/null +++ b/crates/horizon-core/src/release.rs @@ -0,0 +1,32 @@ +//! Which Horizon release this build is (docs/versioning.md). Not Minecraft's version: that is +//! [`crate::version`], generated from the game's own files. + +/// The release's version (`0.2.3`, no `v`): release.yml builds every release with `HORIZON_VERSION` +/// set from its tag, so there are no version-bump commits. None for a build from source. +pub const RELEASE: Option<&str> = match option_env!("HORIZON_VERSION") { + Some(version) if !version.is_empty() => Some(version), + _ => None, +}; + +/// [`RELEASE`], or this crate's Cargo.toml version for a build from source: what `--version`, user +/// agents and reports say. +pub const VERSION: &str = match RELEASE { + Some(version) => version, + None => env!("CARGO_PKG_VERSION"), +}; + +#[cfg(test)] +mod tests { + #[test] + fn the_version_is_the_release_or_the_crate() { + assert_eq!( + super::RELEASE, + option_env!("HORIZON_VERSION").filter(|v| !v.is_empty()) + ); + assert_eq!( + super::VERSION, + super::RELEASE.unwrap_or(env!("CARGO_PKG_VERSION")) + ); + assert!(!super::VERSION.starts_with('v')); + } +} diff --git a/crates/horizon-prepare/src/main.rs b/crates/horizon-prepare/src/main.rs index 0e8adc927..71b7a40cc 100644 --- a/crates/horizon-prepare/src/main.rs +++ b/crates/horizon-prepare/src/main.rs @@ -5,6 +5,13 @@ use std::io::Write; use std::path::{Path, PathBuf}; use std::process::ExitCode; +/// This release's version: release.yml builds it with `HORIZON_VERSION` from the tag +/// (docs/versioning.md); a build from source says Cargo.toml's. +const VERSION: &str = match option_env!("HORIZON_VERSION") { + Some(version) if !version.is_empty() => version, + _ => env!("CARGO_PKG_VERSION"), +}; + const HELP: &str = "\ horizon-prepare: prepare Horizon's resources from your own Minecraft Java Edition @@ -92,7 +99,7 @@ fn main() -> ExitCode { return ExitCode::SUCCESS; } "-V" | "--version" => { - println!("horizon-prepare {}", env!("CARGO_PKG_VERSION")); + println!("horizon-prepare {VERSION}"); return ExitCode::SUCCESS; } _ => Err(format!("unrecognized argument: {text}")), diff --git a/crates/horizon-server-files/src/profiles.rs b/crates/horizon-server-files/src/profiles.rs index 3e16e2a41..3f7e2e509 100644 --- a/crates/horizon-server-files/src/profiles.rs +++ b/crates/horizon-server-files/src/profiles.rs @@ -45,7 +45,7 @@ pub fn resolve(name: &str, online: bool) -> Option { pub fn find_by_name(discovery: &str, name: &str) -> Option { let agent = ureq::AgentBuilder::new() .timeout(Duration::from_secs(15)) - .user_agent(concat!("Horizon/", env!("CARGO_PKG_VERSION"))) + .user_agent(&format!("Horizon/{}", horizon_core::release::VERSION)) .build(); let doc = json::parse(&agent.get(discovery).call().ok()?.into_string().ok()?, true).ok()?; let url = endpoint(&doc, "profiles", "getByName")?.replace("{name}", &name.to_lowercase()); @@ -61,7 +61,7 @@ pub fn fetch_profile_key_keys(discovery: &str) -> Result, &'st const FAILED: &str = "Failed to request Minecraft Services public key"; let agent = ureq::AgentBuilder::new() .timeout(Duration::from_secs(15)) - .user_agent(concat!("Horizon/", env!("CARGO_PKG_VERSION"))) + .user_agent(&format!("Horizon/{}", horizon_core::release::VERSION)) .build(); let get = |url: &str| agent.get(url).call().ok()?.into_string().ok(); let doc = get(discovery) diff --git a/crates/horizon-server/src/api.rs b/crates/horizon-server/src/api.rs index 03f7ae079..c9e553fab 100644 --- a/crates/horizon-server/src/api.rs +++ b/crates/horizon-server/src/api.rs @@ -286,7 +286,7 @@ fn status(shared: &Shared) -> Value { let state = shared.state.lock().unwrap(); json!({ "phase": state.phase.name(), - "version": env!("CARGO_PKG_VERSION"), + "version": crate::VERSION, "pid": std::process::id(), "uptime_seconds": shared.started.elapsed().as_secs(), "engine_pid": state.engine_pid, diff --git a/crates/horizon-server/src/main.rs b/crates/horizon-server/src/main.rs index 627961d81..b4512530c 100644 --- a/crates/horizon-server/src/main.rs +++ b/crates/horizon-server/src/main.rs @@ -18,6 +18,13 @@ mod update; use std::{path::PathBuf, process::ExitCode}; +/// This release's version: release.yml builds it with `HORIZON_VERSION` from the tag +/// (docs/versioning.md); a build from source says Cargo.toml's. +const VERSION: &str = match option_env!("HORIZON_VERSION") { + Some(version) if !version.is_empty() => version, + _ => env!("CARGO_PKG_VERSION"), +}; + const HELP: &str = "\ horizon-server: host a Horizon (Minecraft Java Edition 26.3) server @@ -86,7 +93,7 @@ fn main() -> ExitCode { Ok(()) } Some("version" | "-V" | "--version") => { - println!("horizon-server {}", env!("CARGO_PKG_VERSION")); + println!("horizon-server {VERSION}"); Ok(()) } Some("init") => init_answers(&args[1..]).and_then(|a| init::run(&dir, a)), diff --git a/launcher/src/perf.rs b/launcher/src/perf.rs index 864f6cfa0..7d652283f 100644 --- a/launcher/src/perf.rs +++ b/launcher/src/perf.rs @@ -307,7 +307,7 @@ pub fn benchmark(repeats: usize) -> Result { if let Ok(data) = serde_json::to_value(report.for_upload()) { let item = horizon_telemetry::Item::new( horizon_telemetry::Source::Launcher, - env!("CARGO_PKG_VERSION"), + crate::backend::VERSION, horizon_telemetry::Payload::Benchmark(data), ); let _ = horizon_telemetry::outbox::push(&crate::backend::game_home(), &item); @@ -320,7 +320,7 @@ pub fn benchmark(repeats: usize) -> Result { fn app() -> App { App { name: "horizon-launcher".into(), - version: env!("CARGO_PKG_VERSION").into(), + version: crate::backend::VERSION.into(), ..App::default() } } From da314fa658e9b07b0e1c8d5552a126bbeb9c7bc2 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 21 Oct 2026 19:28:55 +0000 Subject: [PATCH 002/124] Server tick: cheaper view requeue and chunk ticket levels Two costs that grow with players on the server thread: - Hub::update_view rebuilt a player's chunk queue on every chunk crossing by scanning the queue (a Vec) once for each chunk of the view. It now checks a set. Same queue, same order (differential test against the old scan). 111 -> 16 us per crossing at view 10, 6.5 ms -> 0.13 ms at 32. - ChunkTickets rebuilt a hash map of every ticketed chunk whenever any player changed chunk, and every block entity, mob and ticking chunk hashed into it. Levels are now a grid over the tickets' bounding box, with the map kept for tickets spread too far apart. At 50 players: build 0.23 -> 0.08 ms, lookups about 2x faster (differential test over 200 random ticket layouts). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/lan/hub.rs | 112 ++++++++-- crates/horizon-client/src/ticket_levels.rs | 232 ++++++++++++++++++++- 2 files changed, 322 insertions(+), 22 deletions(-) diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index a961d57a4..76a9551fe 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -240,6 +240,30 @@ fn tracked(cx: i32, cz: i32, r: i32, x: i32, z: i32) -> bool { dx * dx + dz * dz < i64::from(r) * i64::from(r) } +/// The view's queue around a new centre: the queued chunks still tracked, then +/// the tracked ones neither sent nor queued, closest first (a stable sort, so +/// equally close chunks keep their order). +fn requeue( + queue: &mut Vec<(i32, i32)>, + sent: &crate::fast_hash::FxHashSet<(i32, i32)>, + (cx, cz): (i32, i32), + r: i32, +) { + queue.retain(|&(x, z)| tracked(cx, cz, r, x, z)); + // A walking player changes chunk every few ticks with hundreds of chunks + // queued: a set, not a scan of the queue for each of the view's chunks. + let mut queued: crate::fast_hash::FxHashSet<(i32, i32)> = queue.iter().copied().collect(); + for dz in -r - 2..=r + 2 { + for dx in -r - 2..=r + 2 { + let key = (cx + dx, cz + dz); + if tracked(cx, cz, r, key.0, key.1) && !sent.contains(&key) && queued.insert(key) { + queue.push(key); + } + } + } + queue.sort_by_key(|&(x, z)| i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2)); +} + /// What a level's players were last told of its clock and weather. #[derive(Clone, Copy)] struct LevelSync { @@ -1288,20 +1312,7 @@ impl Hub { g.sent_chunks.remove(&(x, z)); g.send(&C::ForgetLevelChunk(play::ForgetLevelChunk { pos: [x, z] })); } - g.chunk_queue.retain(|&(x, z)| tracked(cx, cz, r, x, z)); - for dz in -r - 2..=r + 2 { - for dx in -r - 2..=r + 2 { - let key = (cx + dx, cz + dz); - if tracked(cx, cz, r, key.0, key.1) - && !g.sent_chunks.contains(&key) - && !g.chunk_queue.contains(&key) - { - g.chunk_queue.push(key); - } - } - } - g.chunk_queue - .sort_by_key(|&(x, z)| i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2)); + requeue(&mut g.chunk_queue, &g.sent_chunks, (cx, cz), r); } /// PlayerChunkSender.sendNextChunks' choice: ready chunks, closest first, @@ -2457,3 +2468,76 @@ fn ticking_state(rate: f32, frozen: bool) -> C { w.bool(frozen); raw(ids::TICKING_STATE, w.data) } + +#[cfg(test)] +mod tests { + use super::*; + use crate::fast_hash::FxHashSet; + + /// `requeue` as it was, scanning the queue for each chunk of the view. + fn requeue_scan( + queue: &mut Vec<(i32, i32)>, + sent: &FxHashSet<(i32, i32)>, + (cx, cz): (i32, i32), + r: i32, + ) { + queue.retain(|&(x, z)| tracked(cx, cz, r, x, z)); + for dz in -r - 2..=r + 2 { + for dx in -r - 2..=r + 2 { + let key = (cx + dx, cz + dz); + if tracked(cx, cz, r, key.0, key.1) && !sent.contains(&key) && !queue.contains(&key) + { + queue.push(key); + } + } + } + queue.sort_by_key(|&(x, z)| i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2)); + } + + /// A player walking a loop, sending a few chunks a tick: the queue + /// after every move is the one the scan built. + #[test] + fn requeue_matches_the_scan() { + for r in [2, 5, 10, 12, 32] { + let (mut a, mut b) = (Vec::new(), Vec::new()); + let mut sent = FxHashSet::default(); + for step in 0..60i32 { + let center = ((step % 17) - 8, (step * 3 % 11) - 5); + requeue(&mut a, &sent, center, r); + requeue_scan(&mut b, &sent, center, r); + assert_eq!(a, b, "radius {r}, step {step}"); + for key in a.drain(..a.len().min(7)) { + sent.insert(key); + } + b.drain(..b.len().min(7)); + sent.retain(|&(x, z)| tracked(center.0, center.1, r, x, z)); + } + } + } + + /// cargo test --release -p horizon-client --lib lan::hub::tests::requeue_cost -- --ignored --nocapture + #[test] + #[ignore] + fn requeue_cost() { + for r in [10, 16, 32] { + for (name, f) in [ + ("scan", requeue_scan as fn(&mut _, &_, _, _)), + ("set", requeue as fn(&mut _, &_, _, _)), + ] { + let sent = FxHashSet::default(); + let mut queue = Vec::new(); + let start = std::time::Instant::now(); + let moves = 200; + for step in 0..moves { + // Nothing sent yet (an explorer waiting on generation). + f(&mut queue, &sent, (step, 0), r); + } + let per = start.elapsed().as_secs_f64() * 1e6 / f64::from(moves); + println!( + "view {r}: {name} {per:.1} us per chunk crossed, {} queued", + queue.len() + ); + } + } + } +} diff --git a/crates/horizon-client/src/ticket_levels.rs b/crates/horizon-client/src/ticket_levels.rs index 216815de1..de2c427ad 100644 --- a/crates/horizon-client/src/ticket_levels.rs +++ b/crates/horizon-client/src/ticket_levels.rs @@ -45,13 +45,52 @@ pub struct Sources { /// 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>>); +pub struct Levels(Option>); + +/// The levels by chunk: a grid over the tickets' bounding box while it is +/// small (players near each other, the usual case), a map otherwise. Every +/// block entity, mob and ticking chunk asks each tick, so an index beats a +/// hash; and a player crossing into another chunk rebuilds it all. +enum Map { + Grid { + x0: i32, + z0: i32, + width: u32, + depth: u32, + cells: Box<[(u8, u8)]>, + }, + Sparse(FxHashMap<(i32, i32), (u8, u8)>), +} + +/// The largest grid, in chunks: 1 MiB of levels, a box 724 chunks wide. +const GRID_CELLS: u64 = 1 << 19; + +impl Map { + fn get(&self, x: i32, z: i32) -> (u8, u8) { + match self { + Self::Grid { + x0, + z0, + width, + depth, + cells, + } => { + let (dx, dz) = (x.wrapping_sub(*x0) as u32, z.wrapping_sub(*z0) as u32); + if dx < *width && dz < *depth { + cells[(dz * width + dx) as usize] + } else { + NONE + } + } + Self::Sparse(map) => map.get(&(x, z)).copied().unwrap_or(NONE), + } + } +} 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)) + Some(self.0.as_ref()?.get(x, z)) } /// `DistanceManager.inEntityTickingRange`, in an entity-ticking chunk: /// where entities tick. @@ -112,19 +151,89 @@ impl ChunkTickets { } } -fn compute(s: &Sources) -> FxHashMap<(i32, i32), (u8, u8)> { +/// How far a player's tickets reach, and the simulation ticket's level. +fn reach(s: &Sources) -> (i32, i32, i32) { let view = s.view_distance.clamp(0, 32); let simulation = s.simulation_distance.clamp(0, 32); + // 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); + (view, player_sim, reach) +} + +fn compute(s: &Sources) -> Map { + let (view, player_sim, reach) = reach(s); + let boxes = s + .players + .iter() + .map(|&p| (p, reach)) + .chain(s.forced.iter().map(|&f| (f, 1))); + let Some((x0, z0, x1, z1)) = boxes.fold(None, |b, ((x, z), r)| { + let (lo_x, lo_z, hi_x, hi_z) = ( + i64::from(x) - i64::from(r), + i64::from(z) - i64::from(r), + i64::from(x) + i64::from(r), + i64::from(z) + i64::from(r), + ); + Some(match b { + None => (lo_x, lo_z, hi_x, hi_z), + Some((a, b, c, d)) => (lo_x.min(a), lo_z.min(b), hi_x.max(c), hi_z.max(d)), + }) + }) else { + return Map::Sparse(FxHashMap::default()); + }; + let (width, depth) = ((x1 - x0 + 1) as u64, (z1 - z0 + 1) as u64); + if width * depth > GRID_CELLS { + return Map::Sparse(compute_sparse(s)); + } + let (x0, z0, width, depth) = (x0 as i32, z0 as i32, width as u32, depth as u32); + let mut cells = vec![NONE; (width * depth) as usize].into_boxed_slice(); + let mut lower = |x: i32, z: i32, loading: i32, sim: i32| { + let i = ((z - z0) as u32 * width + (x - x0) as u32) as usize; + let e = &mut cells[i]; + 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); + }; + 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); + } + } + } + Map::Grid { + x0, + z0, + width, + depth, + cells, + } +} + +/// The levels as a map, for tickets spread too far apart for a grid. +fn compute_sparse(s: &Sources) -> FxHashMap<(i32, i32), (u8, u8)> { + let (view, player_sim, reach) = reach(s); 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 { @@ -229,4 +338,111 @@ mod tests { t.update(None); assert!(t.levels().0.is_none()); } + + /// The grid answers as the map does, everywhere around the tickets. + #[test] + fn grid_matches_the_map() { + let mut seed = 0x2545_f491_4f6c_dd1du64; + let mut next = |n: i32| { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + (seed % (2 * n as u64 + 1)) as i32 - n + }; + for case in 0..200 { + let spread = [3, 20, 80][case % 3]; + let players: Vec<_> = (0..1 + case % 12) + .map(|_| (next(spread), next(spread))) + .collect(); + let forced: Vec<_> = (0..case % 4) + .map(|_| (next(spread), next(spread))) + .collect(); + let s = Sources { + players, + forced, + view_distance: 2 + next(12).abs(), + simulation_distance: 2 + next(12).abs(), + }; + let Map::Grid { .. } = compute(&s) else { + panic!("case {case} fell back to the map"); + }; + let (grid, map) = (compute(&s), Map::Sparse(compute_sparse(&s))); + let r = spread + 50; + for z in -r..=r { + for x in -r..=r { + assert_eq!(grid.get(x, z), map.get(x, z), "case {case} at ({x}, {z})"); + } + } + } + } + + #[test] + fn far_apart_tickets_use_the_map() { + let l = levels( + &[(0, 0), (100_000, -100_000)], + &[(i32::MAX - 2, i32::MIN + 2)], + 10, + 10, + ); + assert!(matches!(**l.0.as_ref().unwrap(), Map::Sparse(_))); + assert!(l.entity_ticking(100_010, -100_010) && !l.block_ticking(50_000, 0)); + assert!(l.entity_ticking(i32::MAX - 2, i32::MIN + 2)); + } + + /// cargo test --release -p horizon-client --lib ticket_levels::tests::levels_cost -- --ignored --nocapture + #[test] + #[ignore] + fn levels_cost() { + for players in [1usize, 10, 25, 50] { + // The benchmark's bots: most near spawn, a fifth flying outwards. + let at: Vec<_> = (0..players) + .map(|i| { + let a = i as f64 * 2.399; + let d = if i % 5 == 0 { 30.0 } else { 3.0 }; + ((a.cos() * d) as i32, (a.sin() * d) as i32) + }) + .collect(); + let s = Sources { + players: at.clone(), + forced: Vec::new(), + view_distance: 10, + simulation_distance: 10, + }; + let rounds = 200; + let t = std::time::Instant::now(); + for _ in 0..rounds { + std::hint::black_box(compute_sparse(std::hint::black_box(&s))); + } + let map_build = t.elapsed().as_secs_f64() * 1e3 / rounds as f64; + let t = std::time::Instant::now(); + for _ in 0..rounds { + std::hint::black_box(compute(std::hint::black_box(&s))); + } + let grid_build = t.elapsed().as_secs_f64() * 1e3 / rounds as f64; + // A tick's questions: every chunk within 12 of every player. + let (map, grid) = (Map::Sparse(compute_sparse(&s)), compute(&s)); + let ask = |m: &Map| { + let t = std::time::Instant::now(); + let mut n = 0u32; + for _ in 0..rounds { + for &(px, pz) in &at { + for z in pz - 12..=pz + 12 { + for x in px - 12..=px + 12 { + n += + u32::from(std::hint::black_box(m.get(x, z)).0 <= BLOCK_TICKING); + } + } + } + } + std::hint::black_box(n); + t.elapsed().as_secs_f64() * 1e3 / rounds as f64 + }; + println!( + "{players} players: build {map_build:.3} -> {grid_build:.3} ms, {} lookups {:.3} -> {:.3} ms", + players * 625, + ask(&map), + ask(&grid) + ); + } + } } From 62874d8dc43d131c93b2800a767e68f37b0b4be6 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 00:31:00 +0000 Subject: [PATCH 003/124] Furnaces: look up the cooking recipe in an index, without cloning it Every lit furnace, smoker and blast furnace called Recipes::cook each tick, which scanned all cooking recipes (string compares and hash lookups) and the furnace then cloned the recipe found, its ingredient's item set included. Recipes now keeps, built at the first lookup, the first recipe of each kind for each item, which is what the scan returned; the furnace borrows it. 1000 lookups over 320 recipes: 2.07 -> 0.08 ms Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/containers.rs | 2 +- crates/horizon-client/src/crafting.rs | 96 +++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/containers.rs b/crates/horizon-client/src/containers.rs index 6a135799c..854332815 100644 --- a/crates/horizon-client/src/containers.rs +++ b/crates/horizon-client/src/containers.rs @@ -528,7 +528,7 @@ impl Furnace { let has_fuel = self.items[1].is_some(); if lit || has_fuel && has_ingredient { if let Some(ingredient) = self.items[0].clone() { - if let Some(recipe) = recipes.cook(self.kind.recipe_type(), &ingredient).cloned() { + if let Some(recipe) = recipes.cook(self.kind.recipe_type(), &ingredient) { if self.cooking_timer == 0 { self.cooking_total = total_time(recipe.time, self.speed); } diff --git a/crates/horizon-client/src/crafting.rs b/crates/horizon-client/src/crafting.rs index e96ca0067..d0b8906ee 100644 --- a/crates/horizon-client/src/crafting.rs +++ b/crates/horizon-client/src/crafting.rs @@ -1102,7 +1102,11 @@ pub struct Recipes { facts: HashMap, Facts>, /// The `#minecraft:curse` enchantments. curses: HashSet, + /// The first cooking recipe of each kind taking each item, built at the + /// first lookup (furnaces look theirs up every tick). + cook_index: std::sync::OnceLock, } +type CookIndex = HashMap>; /// `MinMaxBounds.Ints` as (min, max) with the given defaults. fn bounds(v: Option<&Value>, default: (i64, i64), open: (i64, i64)) -> (i64, i64) { match v { @@ -1421,6 +1425,7 @@ impl Recipes { meta, facts, curses, + cook_index: Default::default(), }) } /// Match a `size`x`size` grid (row-major) against the crafting recipes. @@ -1606,6 +1611,22 @@ impl Recipes { out } pub fn cook(&self, kind: &str, input: &ItemStack) -> Option<&Cooking> { + let index = self.cook_index.get_or_init(|| { + let mut index = CookIndex::new(); + for (i, c) in self.cooking.iter().enumerate() { + let of_kind = index.entry(c.kind.clone()).or_default(); + for item in &c.ingredient.0 { + of_kind.entry(item.clone()).or_insert(i); + } + } + index + }); + let i = *index.get(kind)?.get(&*input.item)?; + self.cooking.get(i) + } + /// `cook` by scanning every recipe, as it was before the index. + #[cfg(test)] + fn cook_scan(&self, kind: &str, input: &ItemStack) -> Option<&Cooking> { self.cooking .iter() .find(|c| c.kind == kind && c.ingredient.test(input)) @@ -1625,6 +1646,80 @@ mod tests { s.data_mut().banner_patterns = vec![BannerLayer::new("minecraft:cross", "red")]; s } + /// About vanilla's count of cooking recipes: four kinds, ingredients of + /// one to twelve items, overlapping (several recipes take some items). + fn cooking_book() -> Recipes { + let kinds = ["smelting", "blasting", "smoking", "campfire_cooking"]; + let cooking = (0..320usize) + .map(|i| Cooking { + id: format!("r{i}"), + category: "misc".into(), + group: String::new(), + kind: kinds[i % 4].into(), + ingredient: ingredient( + &(0..1 + i % 12) + .map(|k| format!("minecraft:item_{}", (i * 7 + k * 13) % 400)) + .collect::>() + .iter() + .map(String::as_str) + .collect::>(), + ), + result: ItemStack::new("minecraft:stone", 1), + experience: 0.1, + time: 200, + }) + .collect(); + Recipes { + crafting: Vec::new(), + cooking, + meta: HashMap::new(), + facts: HashMap::new(), + curses: HashSet::new(), + cook_index: Default::default(), + } + } + #[test] + fn cook_finds_the_first_recipe_the_scan_finds() { + let recipes = cooking_book(); + for kind in [ + "smelting", + "blasting", + "smoking", + "campfire_cooking", + "other", + ] { + for n in 0..420 { + let stack = ItemStack::new(&format!("minecraft:item_{n}"), 1); + let a = recipes.cook(kind, &stack).map(|c| &c.id); + let b = recipes.cook_scan(kind, &stack).map(|c| &c.id); + assert_eq!(a, b, "{kind} item_{n}"); + } + } + } + /// A furnace's recipe lookup, once per lit furnace per tick. + #[test] + #[ignore] + fn cook_cost() { + let recipes = cooking_book(); + let stacks: Vec = (0..1000) + .map(|n| ItemStack::new(&format!("minecraft:item_{}", n % 400), 1)) + .collect(); + let t = std::time::Instant::now(); + let a = stacks + .iter() + .filter(|s| recipes.cook_scan("smelting", s).cloned().is_some()) + .count(); + let before = t.elapsed().as_secs_f64() * 1e3; + recipes.cook("smelting", &stacks[0]); + let t = std::time::Instant::now(); + let b = stacks + .iter() + .filter(|s| recipes.cook("smelting", s).is_some()) + .count(); + let after = t.elapsed().as_secs_f64() * 1e3; + assert_eq!(a, b); + println!("1000 furnace lookups (scan and clone -> index): {before:.3} ms -> {after:.3} ms"); + } fn run(special: &Special, cells: &[ItemStack]) -> Option { let recipes = Recipes { crafting: Vec::new(), @@ -1632,6 +1727,7 @@ mod tests { meta: HashMap::new(), facts: HashMap::new(), curses: HashSet::new(), + cook_index: Default::default(), }; let ctx = Context { recipes: &recipes, From 2ecbdd57ebfc50459e4cd5017cbe24f3a790ae8a Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 00:46:17 +0000 Subject: [PATCH 004/124] Mob pushes: one list per mob once a crowd is pushing Pushes between mobs wait in a list until their target ticks; each mob's tick scanned the whole list, and take_mob_pushes removed each push from the events in place, shifting the rest. In a pen where every mob pushes its neighbours both grow with the square of the crowd per mob. The pending pushes stay a list while short (64) and become one list per mob past that, each in push order, so every velocity adds up the same values in the same order. take_mob_pushes splits the events in one pass, keeping the order of the others. A test checks velocities bit for bit and the remaining events against the old code. 30 mobs pushing each other: 0.11 -> 0.04 ms per tick 100 mobs pushing each other: 1.73 -> 0.37 ms 200 mobs pushing each other: 9.07 -> 2.61 ms Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- .../horizon-client/examples/mob_ai_probe.rs | 2 +- crates/horizon-client/src/game/creatures.rs | 2 +- crates/horizon-client/src/mobs.rs | 282 +++++++++++++++++- 3 files changed, 270 insertions(+), 16 deletions(-) diff --git a/crates/horizon-client/examples/mob_ai_probe.rs b/crates/horizon-client/examples/mob_ai_probe.rs index 2a4969221..974293ee5 100644 --- a/crates/horizon-client/examples/mob_ai_probe.rs +++ b/crates/horizon-client/examples/mob_ai_probe.rs @@ -1132,7 +1132,7 @@ fn main() -> horizon_client::Result<()> { } #[allow(unused_mut)] // filled only with the rng-trace feature let mut traces: Vec = Vec::new(); - let mut pushes = Vec::new(); + let mut pushes = horizon_client::mobs::PendingPushes::default(); let mut born = Vec::new(); for i in 0..mobs.len() { if mobs[i].removed { diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 113558e34..baf968729 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -498,7 +498,7 @@ impl Game<'_> { // The level random, lent to the mobs (brain behaviours draw from // it) and taken back after their ticks. let mut scratch = self.world.random.clone(); - let mut pushes = Vec::new(); + let mut pushes = horizon_client::mobs::PendingPushes::default(); let mut keep = Vec::with_capacity(mobs.len()); let mut grid = horizon_client::mobs::seen_grid::SeenGrid::new(snapshot.iter().map(|s| s.bounds)); diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index c8155984c..2c19b9251 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -1308,31 +1308,67 @@ impl Mob { /// `Entity.push` on other mobs acts at once in vanilla: a mob ticking later /// moves with it this tick. Takes the `Push` events aimed at mobs out of /// `events[from..]` into `pending`, which the caller adds to each mob's -/// velocity before its tick (and to every mob once all have ticked). -pub fn take_mob_pushes(events: &mut Vec, from: usize, pending: &mut Vec<(u32, Vec3)>) { - let mut i = from; - while i < events.len() { +/// velocity before its tick (and to every mob once all have ticked). The +/// other events keep their order. +pub fn take_mob_pushes(events: &mut Vec, from: usize, pending: &mut PendingPushes) { + // One pass: a crowd's tick holds a push per neighbour, and removing each + // in place would shift the rest every time. + let mut kept = from; + for i in from..events.len() { if let MobEvent::Push { target: Who::Mob(id), velocity, } = events[i] { - pending.push((id, Vec3::new(velocity[0], velocity[1], velocity[2]))); - events.remove(i); + pending.push(id, Vec3::new(velocity[0], velocity[1], velocity[2])); } else { - i += 1; + events.swap(kept, i); + kept += 1; } } + events.truncate(kept); +} +/// Pushes waiting for their mob's tick, in push order. A list while short; +/// past [`PendingPushes::LIST_MAX`] (a crowd pushing each other) one list +/// per mob, as scanning the whole list for each mob grows with its square. +#[derive(Default)] +pub struct PendingPushes { + list: Vec<(u32, Vec3)>, + by_mob: crate::fast_hash::FxHashMap>, +} +impl PendingPushes { + const LIST_MAX: usize = 64; + pub fn push(&mut self, id: u32, v: Vec3) { + if self.by_mob.is_empty() && self.list.len() < Self::LIST_MAX { + self.list.push((id, v)); + return; + } + // Older pushes first, so each mob's stay in order. + for (id, v) in self.list.drain(..) { + self.by_mob.entry(id).or_default().push(v); + } + self.by_mob.entry(id).or_default().push(v); + } } /// Adds the pending pushes aimed at `m` to its velocity, in push order. -pub fn apply_mob_pushes(m: &mut Mob, pending: &mut Vec<(u32, Vec3)>) { - pending.retain(|(id, v)| { - if *id != m.id { - return true; - } +pub fn apply_mob_pushes(m: &mut Mob, pending: &mut PendingPushes) { + let mut add = |v: Vec3| { m.velocity = Vec3::new(m.velocity.x + v.x, m.velocity.y + v.y, m.velocity.z + v.z); - false - }); + }; + if !pending.list.is_empty() { + pending.list.retain(|(id, v)| { + if *id != m.id { + return true; + } + add(*v); + false + }); + } + if !pending.by_mob.is_empty() + && let Some(pushes) = pending.by_mob.remove(&m.id) + { + pushes.into_iter().for_each(add); + } } /// Applies the brain memories a mob's tick set on other mobs (from event /// index `from` on), at once: vanilla sets them on the other entity during @@ -2132,3 +2168,221 @@ mod elytra_tests { assert!((wings.angles[1] - 0.08726646).abs() < 0.00001); } } +#[cfg(test)] +mod push_tests { + use super::*; + use horizon_core::geometry::Vec3; + use serde_json::json; + + fn kind() -> Arc { + Arc::new(MobType { + id: "minecraft:cow".into(), + species: Species::Cow, + width: 0.9, + height: 1.4, + eye_height: 1.3, + category: "creature".into(), + max_health: 10.0, + speed: 0.2, + flying_speed: 0.4, + attack_damage: 0.0, + attack_knockback: 0.0, + follow_range: 16.0, + step_height: 0.6, + armor: 0.0, + armor_toughness: 0.0, + knockback_resistance: 0.0, + jump_strength: 0.42, + attributes: Arc::default(), + gravity: 0.08, + safe_fall_distance: 3.0, + fall_damage_multiplier: 1.0, + water_efficiency: 0.0, + water_slowdown: 0.8, + air_speed: 0.02, + baby_scale: 0.5, + head_rot_speed: 10.0, + max_head_x_rot: 40.0, + max_head_y_rot: 75.0, + ambient_interval: 120, + flags: Flags { + animal: true, + ..Default::default() + }, + model: Arc::new(Part::parse("root", &json!({}))), + layers: HashMap::new(), + texture: String::new(), + brain: Brain::Passive, + anim: Anim::Rabbit, + sounds: [""; 3], + step_sound: None, + food: Arc::default(), + tempt: Arc::default(), + }) + } + + /// The pending pushes as one list, scanned for each mob, as before. + fn take_old(events: &mut Vec, from: usize, pending: &mut Vec<(u32, Vec3)>) { + let mut i = from; + while i < events.len() { + if let MobEvent::Push { + target: Who::Mob(id), + velocity, + } = events[i] + { + pending.push((id, Vec3::new(velocity[0], velocity[1], velocity[2]))); + events.remove(i); + } else { + i += 1; + } + } + } + fn apply_old(velocity: &mut Vec3, id: u32, pending: &mut Vec<(u32, Vec3)>) { + pending.retain(|(to, v)| { + if *to != id { + return true; + } + *velocity = Vec3::new(velocity.x + v.x, velocity.y + v.y, velocity.z + v.z); + false + }); + } + fn mobs(n: u32) -> Vec { + let kind = kind(); + (0..n) + .map(|id| Mob::new(id, kind.clone(), Vec3::ZERO, 0.0)) + .collect() + } + /// A crowd tick: each mob takes its pushes, then pushes every other + /// mob (and the player, and hisses now and then); then all take theirs. + fn crowd_tick( + n: u32, + tick: u32, + mut on_push: impl FnMut(&mut Vec, usize), + mut on_apply: impl FnMut(u32), + ) -> Vec { + let mut events = Vec::new(); + for i in 0..n { + on_apply(i); + let from = events.len(); + for j in 0..n { + if j != i && !(i * 31 + j * 17 + tick).is_multiple_of(5) { + let f = f64::from(i * 7 + j + tick) * 0.001; + events.push(MobEvent::Push { + target: Who::Mob(j), + velocity: [f, -f * 0.5, f * 0.25], + }); + } + if j.is_multiple_of(9) { + events.push(MobEvent::Hiss { mob: j }); + } + } + events.push(MobEvent::Push { + target: Who::Player, + velocity: [0.1, 0.0, 0.1], + }); + on_push(&mut events, from); + } + for i in 0..n { + on_apply(i); + } + events + } + + #[test] + fn pushes_land_as_with_the_list() { + for n in [1u32, 3, 8, 20, 60] { + let mut new_mobs = mobs(n); + let mut old_v = vec![Vec3::ZERO; n as usize]; + let mut pending = PendingPushes::default(); + let mut old_pending = Vec::new(); + for tick in 0..4 { + let new_events = { + let pending = std::cell::RefCell::new(&mut pending); + let mobs = std::cell::RefCell::new(&mut new_mobs); + crowd_tick( + n, + tick, + |e, from| take_mob_pushes(e, from, &mut pending.borrow_mut()), + |i| { + apply_mob_pushes( + &mut mobs.borrow_mut()[i as usize], + &mut pending.borrow_mut(), + ) + }, + ) + }; + let old_events = { + let pending = std::cell::RefCell::new(&mut old_pending); + let v = std::cell::RefCell::new(&mut old_v); + crowd_tick( + n, + tick, + |e, from| take_old(e, from, &mut pending.borrow_mut()), + |i| { + apply_old( + &mut v.borrow_mut()[i as usize], + i, + &mut pending.borrow_mut(), + ) + }, + ) + }; + assert_eq!(format!("{new_events:?}"), format!("{old_events:?}")); + for i in 0..n as usize { + let (a, b) = (new_mobs[i].velocity, old_v[i]); + assert_eq!( + (a.x.to_bits(), a.y.to_bits(), a.z.to_bits()), + (b.x.to_bits(), b.y.to_bits(), b.z.to_bits()), + "{n} mobs, tick {tick}, mob {i}" + ); + } + } + } + } + + /// One tick of a pen of `n` mobs pushing each other. + #[test] + #[ignore] + fn push_cost() { + for n in [10u32, 30, 100, 200] { + let mut ms = mobs(n); + let mut v = vec![Vec3::ZERO; n as usize]; + let t = std::time::Instant::now(); + { + let pending = std::cell::RefCell::new(Vec::new()); + let v = std::cell::RefCell::new(&mut v); + crowd_tick( + n, + 0, + |e, from| take_old(e, from, &mut pending.borrow_mut()), + |i| { + apply_old( + &mut v.borrow_mut()[i as usize], + i, + &mut pending.borrow_mut(), + ) + }, + ); + } + let before = t.elapsed().as_secs_f64() * 1e3; + let t = std::time::Instant::now(); + { + let pending = std::cell::RefCell::new(PendingPushes::default()); + let ms = std::cell::RefCell::new(&mut ms); + crowd_tick( + n, + 0, + |e, from| take_mob_pushes(e, from, &mut pending.borrow_mut()), + |i| { + apply_mob_pushes( + &mut ms.borrow_mut()[i as usize], + &mut pending.borrow_mut(), + ) + }, + ); + } + let after = t.elapsed().as_secs_f64() * 1e3; + println!("{n} mobs pushing each other: {before:.3} ms -> {after:.3} ms"); + } + } +} From 6216b846691aab89cf46a105a66e871703670401 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 21 Oct 2026 23:54:13 +0000 Subject: [PATCH 005/124] Mob tick: build the mob world once a tick, index the snapshot tick_creature_entities called mob_world_base for every mob. That rebuilt the player view (its allies set scans every mob UUID) and, with any scoreboard team, an owned copy of every mob's team, each a clone of the team's player set: cubic in the mobs on a team. The base is now built once and cloned per mob, with the teams borrowed behind an Arc. 100 mobs, half on a team: 37.7 ms -> 0.06 ms per tick 500 mobs, half on a team: 4297 ms -> 0.31 ms per tick MobWorld::seen looked mobs up by scanning the snapshot; the mob ticks now pass an id index (first slot wins, as the scan did): 2000 lookups over 2000 mobs 1.09 -> 0.01 ms. The nearest-living-entity sensors, the look-at goal, the ravager's roar and MobWorld::nearby read candidates from the tick's position grid instead of every mob, in the same order: 2000 mobs 0.39 -> 0.11 ms per tick. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/game/creatures.rs | 20 +- crates/horizon-client/src/game/scoreboard.rs | 8 +- crates/horizon-client/src/mobs.rs | 253 +++++++++++++++++- .../horizon-client/src/mobs/animal_brain.rs | 2 +- crates/horizon-client/src/mobs/goals.rs | 3 +- .../horizon-client/src/mobs/monster_brain.rs | 2 +- crates/horizon-client/src/mobs/ravager.rs | 2 +- .../horizon-client/src/mobs/villager_brain.rs | 2 +- .../horizon-client/src/mobs/warden_brain.rs | 2 +- crates/horizon-client/src/mobs/world.rs | 18 +- 10 files changed, 285 insertions(+), 27 deletions(-) diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 113558e34..d538b5828 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -389,6 +389,7 @@ impl Game<'_> { player, others, others_grid: None, + others_index: None, daylight: self.dim != Dim::Nether && self.sky_darken() < 4, game_time: self.world.game_time, day_time: self.world.day_time, @@ -401,7 +402,7 @@ impl Game<'_> { end: self.dim == Dim::End, world_seed: self.level.seed, sea_level: if self.dim == Dim::Nether { 32 } else { 63 }, - teams: self.mob_teams(), + teams: std::sync::Arc::new(self.mob_teams()), items: Some(self.res.items()), difficulty: self.local_difficulty( self.player @@ -502,9 +503,22 @@ impl Game<'_> { let mut keep = Vec::with_capacity(mobs.len()); let mut grid = horizon_client::mobs::seen_grid::SeenGrid::new(snapshot.iter().map(|s| s.bounds)); + // All but the snapshot is the same for every mob of the tick, so + // build it once: the player view and the teams scan every UUID. + let base = self.mob_world_base(&[]); + // Each mob keeps its snapshot slot all tick; the first slot of an + // id answers, as a scan would. + let mut index = horizon_client::fast_hash::FxHashMap::::default(); + for (i, s) in snapshot.iter().enumerate() { + index.entry(s.id).or_insert(i as u32); + } for i in 0..mobs.len() { - let mut w = self.mob_world_base(&snapshot); - w.others_grid = Some(&grid); + let mut w = MobWorld { + others: &snapshot, + others_grid: Some(&grid), + others_index: Some(&index), + ..base.clone() + }; let m = &mut mobs[i]; w.nearest_player = super::natural_spawning::nearest_player( &players, diff --git a/crates/horizon-client/src/game/scoreboard.rs b/crates/horizon-client/src/game/scoreboard.rs index 99dae39a5..e01d6ed03 100644 --- a/crates/horizon-client/src/game/scoreboard.rs +++ b/crates/horizon-client/src/game/scoreboard.rs @@ -431,20 +431,18 @@ impl Game<'_> { self.score.board.players_team(self.acting_player_name()) } /// The teams of the player and of the mobs on one (empty without teams). - pub(super) fn mob_teams(&self) -> horizon_client::mobs::Teams { + pub(super) fn mob_teams(&self) -> horizon_client::mobs::Teams<'_> { let board = &self.score.board; if board.teams.is_empty() { return Default::default(); } horizon_client::mobs::Teams { - player: self.player_team().cloned(), + player: self.player_team(), mobs: self .persist .uuids .iter() - .filter_map(|(id, uuid)| { - Some((*id, board.players_team(&uuid_text(*uuid))?.clone())) - }) + .filter_map(|(id, uuid)| Some((*id, board.players_team(&uuid_text(*uuid))?))) .collect(), } } diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index c8155984c..10d5c19cb 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -1404,6 +1404,7 @@ pub struct PlayerView { /// (`EntitySelector.pushableBy` on the client). pub remote: bool, } +#[derive(Clone)] pub struct MobWorld<'a> { pub world: &'a World, pub registry: &'a horizon_world::registry::BlockRegistry, @@ -1418,6 +1419,8 @@ pub struct MobWorld<'a> { pub others: &'a [Seen], /// `others` by position, when the caller keeps one (the mob ticks). pub others_grid: Option<&'a seen_grid::SeenGrid>, + /// Where each id stands in `others`, when the caller keeps one. + pub others_index: Option<&'a crate::fast_hash::FxHashMap>, pub daylight: bool, pub game_time: i64, pub day_time: i64, @@ -1436,22 +1439,23 @@ pub struct MobWorld<'a> { pub world_seed: i64, /// `Level.getSeaLevel` (63 in the overworld, -63 in a flat world). pub sea_level: i32, - /// The scoreboard teams of the player and the mobs (collision rules). - pub teams: Teams, + /// The scoreboard teams of the player and the mobs (collision rules), + /// shared: the mob ticks build them once a tick, not once a mob. + pub teams: std::sync::Arc>, /// Item data (max stack sizes), when the host has it. pub items: Option<&'a crate::items::Items>, } /// `Entity.getTeam` of the player and of the mobs on a team. #[derive(Clone, Debug, Default)] -pub struct Teams { - pub player: Option, - pub mobs: HashMap, +pub struct Teams<'a> { + pub player: Option<&'a crate::scoreboard::Team>, + pub mobs: HashMap, } -impl Teams { - pub fn of(&self, who: Who) -> Option<&crate::scoreboard::Team> { +impl<'a> Teams<'a> { + pub fn of(&self, who: Who) -> Option<&'a crate::scoreboard::Team> { match who { - Who::Player => self.player.as_ref(), - Who::Mob(id) => self.mobs.get(&id), + Who::Player => self.player, + Who::Mob(id) => self.mobs.get(&id).copied(), } } } @@ -2132,3 +2136,234 @@ mod elytra_tests { assert!((wings.angles[1] - 0.08726646).abs() < 0.00001); } } +#[cfg(test)] +mod teams_tests { + use super::*; + use crate::save::uuid_text; + use crate::scoreboard::{Scoreboard, Team}; + + /// A board where every other mob is on the player's team, as with a + /// datapack that colours its mobs' names. + fn board(mobs: u32) -> (Scoreboard, HashMap) { + let mut board = Scoreboard::default(); + board.add_team("red"); + board.add_to_team("Steve", "red"); + let uuids: HashMap = (0..mobs) + .map(|id| (id, [id as i32, 7, -(id as i32), 11])) + .collect(); + for (id, uuid) in &uuids { + if id % 2 == 0 { + board.add_to_team(&uuid_text(*uuid), "red"); + } + } + (board, uuids) + } + /// `Game::mob_teams` as built for each mob before: owned clones of the teams. + fn owned(board: &Scoreboard, uuids: &HashMap) -> HashMap { + uuids + .iter() + .filter_map(|(id, u)| Some((*id, board.players_team(&uuid_text(*u))?.clone()))) + .collect() + } + fn borrowed<'a>(board: &'a Scoreboard, uuids: &HashMap) -> Teams<'a> { + Teams { + player: board.players_team("Steve"), + mobs: uuids + .iter() + .filter_map(|(id, u)| Some((*id, board.players_team(&uuid_text(*u))?))) + .collect(), + } + } + + #[test] + fn borrowed_teams_answer_as_the_clones() { + let (board, uuids) = board(40); + let teams = borrowed(&board, &uuids); + let old = owned(&board, &uuids); + for id in 0..41 { + assert_eq!(teams.of(Who::Mob(id)), old.get(&id), "mob {id}"); + } + assert_eq!(teams.of(Who::Player).map(|t| t.name.as_str()), Some("red")); + } + + /// One tick's worth of team building: once per mob (before) against once. + #[test] + #[ignore] + fn teams_cost() { + for mobs in [100u32, 250, 500] { + let (board, uuids) = board(mobs); + let t = std::time::Instant::now(); + for _ in 0..mobs { + std::hint::black_box(owned(&board, &uuids)); + } + let before = t.elapsed(); + let t = std::time::Instant::now(); + let reps = 20; + for _ in 0..reps { + std::hint::black_box(borrowed(&board, &uuids)); + } + let after = t.elapsed() / reps; + println!( + "{mobs} mobs, half on a team: per tick {:.3} ms -> {:.3} ms", + before.as_secs_f64() * 1e3, + after.as_secs_f64() * 1e3 + ); + } + } +} +#[cfg(test)] +mod sensor_scan_tests { + use super::*; + use horizon_core::geometry::Vec3; + use serde_json::json; + + fn kind() -> Arc { + Arc::new(MobType { + id: "minecraft:cow".into(), + species: Species::Cow, + width: 0.9, + height: 1.4, + eye_height: 1.3, + category: "creature".into(), + max_health: 10.0, + speed: 0.2, + flying_speed: 0.4, + attack_damage: 0.0, + attack_knockback: 0.0, + follow_range: 16.0, + step_height: 0.6, + armor: 0.0, + armor_toughness: 0.0, + knockback_resistance: 0.0, + jump_strength: 0.42, + attributes: Arc::default(), + gravity: 0.08, + safe_fall_distance: 3.0, + fall_damage_multiplier: 1.0, + water_efficiency: 0.0, + water_slowdown: 0.8, + air_speed: 0.02, + baby_scale: 0.5, + head_rot_speed: 10.0, + max_head_x_rot: 40.0, + max_head_y_rot: 75.0, + ambient_interval: 120, + flags: Flags { + animal: true, + ..Default::default() + }, + model: Arc::new(Part::parse("root", &json!({}))), + layers: HashMap::new(), + texture: String::new(), + brain: Brain::Passive, + anim: Anim::Rabbit, + sounds: [""; 3], + step_sound: None, + food: Arc::default(), + tempt: Arc::default(), + }) + } + /// `n` mobs in pens of 20 spread over a square of `side` blocks, as on a + /// server's farms; their snapshot and its grid. + fn herd(n: u32, side: f64) -> (Vec, seen_grid::SeenGrid) { + let kind = kind(); + let mut seed = 0x9e37_79b9_7f4a_7c15u64; + let mut next = |range: f64| { + seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1); + (seed >> 11) as f64 / (1u64 << 53) as f64 * range + }; + let mut pen = Vec3::ZERO; + let seen: Vec = (0..n) + .map(|id| { + if id % 20 == 0 { + pen = Vec3::new(next(side), 64.0, next(side)); + } + let at = Vec3::new(pen.x + next(8.0), 64.0, pen.z + next(8.0)); + Mob::new(id, kind.clone(), at, 0.0).seen(None) + }) + .collect(); + let grid = seen_grid::SeenGrid::new(seen.iter().map(|s| s.bounds)); + (seen, grid) + } + /// `NearestLivingEntitySensor` over every mob, as the brains ran it. + fn scan(seen: &[Seen], me: &Seen, area: Aabb) -> Vec { + seen.iter() + .filter(|o| o.id != me.id && o.alive && o.bounds.intersects(area)) + .map(|o| o.id) + .collect() + } + fn indexed(seen: &[Seen], grid: &seen_grid::SeenGrid, me: &Seen, area: Aabb) -> Vec { + let mut near = Vec::new(); + assert!(grid.candidates(area, &mut near)); + near.iter() + .map(|&i| &seen[i as usize]) + .filter(|o| o.id != me.id && o.alive && o.bounds.intersects(area)) + .map(|o| o.id) + .collect() + } + + #[test] + fn the_grid_finds_what_the_scan_finds() { + let (seen, grid) = herd(600, 160.0); + for me in &seen { + let area = me.bounds.inflate(Vec3::new(16.0, 16.0, 16.0)); + assert_eq!(indexed(&seen, &grid, me, area), scan(&seen, me, area)); + } + } + + /// `MobWorld::seen` once per mob (a target, a breeding partner...), by + /// scan as before and by the tick's id index. + #[test] + #[ignore] + fn lookup_cost() { + for n in [500u32, 1000, 2000] { + let (seen, _) = herd(n, 256.0); + let index: crate::fast_hash::FxHashMap = seen + .iter() + .enumerate() + .map(|(i, s)| (s.id, i as u32)) + .collect(); + let wanted: Vec = (0..n).map(|i| (i * 7919) % n).collect(); + let t = std::time::Instant::now(); + let a: usize = wanted + .iter() + .filter_map(|&id| seen.iter().find(|s| s.id == id)) + .map(|s| s.id as usize) + .sum(); + let before = t.elapsed().as_secs_f64() * 1e3; + let t = std::time::Instant::now(); + let b: usize = wanted + .iter() + .filter_map(|&id| index.get(&id).map(|&i| &seen[i as usize])) + .map(|s| s.id as usize) + .sum(); + let after = t.elapsed().as_secs_f64() * 1e3; + assert_eq!(a, b); + println!( + "{n} lookups over {n} mobs ({} B each): {before:.3} ms -> {after:.3} ms", + std::mem::size_of::() + ); + } + } + + /// One tick of sensing: a twentieth of the mobs fire their sensor. + #[test] + #[ignore] + fn sensor_cost() { + for (n, side) in [(500u32, 256.0), (1000, 384.0), (2000, 512.0)] { + let (seen, grid) = herd(n, side); + let time = |f: &dyn Fn(&Seen, Aabb) -> usize| { + let t = std::time::Instant::now(); + let mut found = 0; + for me in &seen { + found += f(me, me.bounds.inflate(Vec3::new(16.0, 16.0, 16.0))); + } + (t.elapsed().as_secs_f64() * 1e3 / 20.0, found) + }; + let (before, a) = time(&|me, area| scan(&seen, me, area).len()); + let (after, b) = time(&|me, area| indexed(&seen, &grid, me, area).len()); + assert_eq!(a, b); + println!("{n} mobs: per tick {before:.3} ms -> {after:.3} ms"); + } + } +} diff --git a/crates/horizon-client/src/mobs/animal_brain.rs b/crates/horizon-client/src/mobs/animal_brain.rs index 9920235a9..331870b4a 100644 --- a/crates/horizon-client/src/mobs/animal_brain.rs +++ b/crates/horizon-client/src/mobs/animal_brain.rs @@ -606,7 +606,7 @@ fn sense_living(m: &mut Mob, w: &MobWorld) { let r = m.attrs.follow_range; let area = m.bounds().inflate(Vec3::new(r, r, r)); let mut found: Vec<(Who, f64)> = Vec::new(); - for o in w.others { + for o in w.others_near(area) { if o.id != m.id && o.alive && o.bounds.intersects(area) { found.push((Who::Mob(o.id), m.distance_sqr(o.position))); } diff --git a/crates/horizon-client/src/mobs/goals.rs b/crates/horizon-client/src/mobs/goals.rs index 9419a23ba..38108f8bc 100644 --- a/crates/horizon-client/src/mobs/goals.rs +++ b/crates/horizon-client/src/mobs/goals.rs @@ -970,8 +970,7 @@ impl Goal for LookAtPlayer { let (id, only) = (m.id, self.only); let at = Vec3::new(m.position.x, m.eye_y(), m.position.z); let candidates: Vec<(u32, Vec3)> = - cx.w.others - .iter() + cx.w.others_near(area) .filter(|s| s.id != id && s.alive && s.bounds.intersects(area)) .filter(|s| only.is_none_or(|o| s.species == o)) .map(|s| (s.id, s.position)) diff --git a/crates/horizon-client/src/mobs/monster_brain.rs b/crates/horizon-client/src/mobs/monster_brain.rs index 238653dad..eeb2c42f2 100644 --- a/crates/horizon-client/src/mobs/monster_brain.rs +++ b/crates/horizon-client/src/mobs/monster_brain.rs @@ -299,7 +299,7 @@ fn sense_living(m: &mut Mob, w: &MobWorld) { let r = m.attrs.follow_range; let area = m.bounds().inflate(Vec3::new(r, r, r)); let mut found: Vec<(Who, f64)> = Vec::new(); - for o in w.others { + for o in w.others_near(area) { if o.id != m.id && o.alive && o.bounds.intersects(area) { found.push((Who::Mob(o.id), m.distance_sqr(o.position))); } diff --git a/crates/horizon-client/src/mobs/ravager.rs b/crates/horizon-client/src/mobs/ravager.rs index ef71b8f77..49bac4d9f 100644 --- a/crates/horizon-client/src/mobs/ravager.rs +++ b/crates/horizon-client/src/mobs/ravager.rs @@ -177,7 +177,7 @@ fn roar(m: &mut Mob, w: &MobWorld, events: &mut Vec) { velocity: fling(p.position), }); } - for s in w.others.iter() { + for s in w.others_near(area) { if s.id == m.id || !s.alive || s.species == Species::Ravager || !s.bounds.intersects(area) { continue; } diff --git a/crates/horizon-client/src/mobs/villager_brain.rs b/crates/horizon-client/src/mobs/villager_brain.rs index 5b8500c0d..d401ea6da 100644 --- a/crates/horizon-client/src/mobs/villager_brain.rs +++ b/crates/horizon-client/src/mobs/villager_brain.rs @@ -568,7 +568,7 @@ fn sense_living(m: &mut Mob, w: &MobWorld) { let r = m.attrs.follow_range; let area = m.bounds().inflate(Vec3::new(r, r, r)); let mut found: Vec<(Who, f64)> = Vec::new(); - for o in w.others { + for o in w.others_near(area) { if o.id != m.id && o.alive && o.bounds.intersects(area) { found.push((Who::Mob(o.id), m.distance_sqr(o.position))); } diff --git a/crates/horizon-client/src/mobs/warden_brain.rs b/crates/horizon-client/src/mobs/warden_brain.rs index 41353fc83..518347e77 100644 --- a/crates/horizon-client/src/mobs/warden_brain.rs +++ b/crates/horizon-client/src/mobs/warden_brain.rs @@ -492,7 +492,7 @@ fn sense_entities(m: &mut Mob, w: &MobWorld) { let r = m.attrs.follow_range; let area = m.bounds().inflate(Vec3::new(r, r, r)); let mut found: Vec<(Who, f64)> = Vec::new(); - for o in w.others { + for o in w.others_near(area) { if o.id != m.id && o.alive && o.bounds.intersects(area) { found.push((Who::Mob(o.id), m.distance_sqr(o.position))); } diff --git a/crates/horizon-client/src/mobs/world.rs b/crates/horizon-client/src/mobs/world.rs index cc4110dd7..79bbaa30c 100644 --- a/crates/horizon-client/src/mobs/world.rs +++ b/crates/horizon-client/src/mobs/world.rs @@ -226,7 +226,13 @@ impl MobWorld<'_> { .chain(all.into_iter().flatten()) } pub fn seen(&self, id: u32) -> Option<&Seen> { - self.others.iter().find(|s| s.id == id) + match self.others_index { + Some(index) => index + .get(&id) + .and_then(|&i| self.others.get(i as usize)) + .filter(|s| s.id == id), + None => self.others.iter().find(|s| s.id == id), + } } /// Living mobs of the given species within `range` of `from`, nearest first. pub fn nearby<'s>( @@ -235,9 +241,15 @@ impl MobWorld<'_> { range: f64, pred: impl Fn(&Seen) -> bool + 's, ) -> impl Iterator + 's { + // A position within `range` puts the box's bottom face in this area + // (one block wider, as the box test is strict). + let r = range + 1.0; + let area = Aabb::new( + Vec3::new(from.x - r, from.y - r, from.z - r), + Vec3::new(from.x + r, from.y + r, from.z + r), + ); let mut v: Vec<&Seen> = self - .others - .iter() + .others_near(area) .filter(|s| { s.alive && (s.position.x - from.x).abs() <= range From 67ca65bb3b003999c511a4bd729274acc0f6da38 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 21 Oct 2026 23:20:00 +0000 Subject: [PATCH 006/124] Mob tick: drop gone mobs' guest bindings with a set, not a scan per binding On a server every ticked mob is bound to its nearest player, and each tick the bindings of mobs that are gone were dropped by scanning every mob for each binding. Mob is 1.5 KB, so the scan misses the cache on every mob: with 1000 loaded mobs that was 0.44 ms a tick, with 2000 1.86 ms; a set of the live ids makes it 0.02 and 0.04 ms (stand-in structs of Mob's size). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/game/creatures.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 113558e34..053047b42 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -588,7 +588,11 @@ impl Game<'_> { if let Some(host) = self.lan.as_mut() && !guests.is_empty() { - bindings.retain(|id, _| mobs.iter().any(|m| m.id == *id)); + // A set: a scan of every mob for each binding is quadratic, and a + // server's loaded mobs run into the thousands. + let live: horizon_client::fast_hash::FxHashSet = + mobs.iter().map(|m| m.id).collect(); + bindings.retain(|id, _| live.contains(id)); host.mob_players = bindings; } self.mobs.append(&mut mobs); From 3b21a975bb04548d8f64a5adde7d4b24859332ce Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 01:01:03 +0000 Subject: [PATCH 007/124] Mob goals: read nearby mobs from the tick's grid in four more places FollowParentGoal (every baby, every tick), the pufferfish's puff check, the enderman's block placing and the iron golem's DefendVillage scanned every mob. They now read candidates from the position grid around the area they test (one block wider where they test positions, as a box whose bottom face holds the position meets it), in snapshot order, and test exactly as before. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/mobs/aquatic.rs | 2 +- crates/horizon-client/src/mobs/enderman.rs | 11 +++++------ crates/horizon-client/src/mobs/goals.rs | 13 +++++++++++-- crates/horizon-client/src/mobs/golem.rs | 5 ++++- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/crates/horizon-client/src/mobs/aquatic.rs b/crates/horizon-client/src/mobs/aquatic.rs index 702e3740d..adb2cbe3a 100644 --- a/crates/horizon-client/src/mobs/aquatic.rs +++ b/crates/horizon-client/src/mobs/aquatic.rs @@ -438,7 +438,7 @@ impl Goal for PufferfishPuff { .as_ref() .is_some_and(|p| p.alive && !p.creative && area.intersects(p.bounds)); // TARGETING_CONDITIONS: not other fish (not_scary_for_pufferfish) and alive. - let mob = cx.w.others.iter().any(|s| { + let mob = cx.w.others_near(area).any(|s| { s.id != m.id && s.alive && !matches!( diff --git a/crates/horizon-client/src/mobs/enderman.rs b/crates/horizon-client/src/mobs/enderman.rs index 13e6c8a44..fcfba832e 100644 --- a/crates/horizon-client/src/mobs/enderman.rs +++ b/crates/horizon-client/src/mobs/enderman.rs @@ -391,12 +391,11 @@ impl Goal for LeaveBlock { let target = cx.w.world.block(x, y, z); let below = cx.w.world.block(x, y - 1, z); let bf = cx.w.facts.get(below); - let free = !cx.w.others.iter().any(|s| { - s.bounds.intersects(Aabb::new( - Vec3::new(f64::from(x), f64::from(y), f64::from(z)), - Vec3::new(f64::from(x + 1), f64::from(y + 1), f64::from(z + 1)), - )) - }); + let block = Aabb::new( + Vec3::new(f64::from(x), f64::from(y), f64::from(z)), + Vec3::new(f64::from(x + 1), f64::from(y + 1), f64::from(z + 1)), + ); + let free = !cx.w.others_near(block).any(|s| s.bounds.intersects(block)); if cx.w.facts.is_air(target) && !cx.w.facts.is_air(below) && &*bf.name != "minecraft:bedrock" diff --git a/crates/horizon-client/src/mobs/goals.rs b/crates/horizon-client/src/mobs/goals.rs index 38108f8bc..c12f9e7ee 100644 --- a/crates/horizon-client/src/mobs/goals.rs +++ b/crates/horizon-client/src/mobs/goals.rs @@ -1,7 +1,11 @@ //! Vanilla's `GoalSelector` (priorities, MOVE/LOOK/JUMP/TARGET control flags, //! interruption) and the shared goals of `net.minecraft.world.entity.ai.goal`. use super::{Mob, MobEvent, MobWorld, Seen, Species, Who, world::TargetInfo}; -use horizon_core::{geometry::Vec3, jmath, random::RandomSource}; +use horizon_core::{ + geometry::{Aabb, Vec3}, + jmath, + random::RandomSource, +}; use std::sync::Arc; pub const MOVE: u8 = 1; @@ -1901,7 +1905,12 @@ impl Goal for FollowParent { let species = m.species(); let pos = m.position; let mut best: Option<(u32, f64)> = None; - for s in cx.w.others.iter().filter(|s| { + // Every position in range puts the box's bottom face in this area. + let area = Aabb::new( + Vec3::new(pos.x - 9.0, pos.y - 5.0, pos.z - 9.0), + Vec3::new(pos.x + 9.0, pos.y + 5.0, pos.z + 9.0), + ); + for s in cx.w.others_near(area).filter(|s| { s.alive && s.species == species && !s.baby diff --git a/crates/horizon-client/src/mobs/golem.rs b/crates/horizon-client/src/mobs/golem.rs index d4d67660b..c41c34f11 100644 --- a/crates/horizon-client/src/mobs/golem.rs +++ b/crates/horizon-client/src/mobs/golem.rs @@ -68,7 +68,10 @@ impl Goal for DefendVillage { if !b.intersects(p.bounds) || m.position.distance_squared(p.position) > 64.0 * 64.0 { return false; } - let bad = cx.w.others.iter().any(|s| { + // A position inside `b` puts the box's bottom face there (one block + // wider, as the box test is strict). + let near = b.inflate(Vec3::new(1.0, 1.0, 1.0)); + let bad = cx.w.others_near(near).any(|s| { s.species == Species::Villager && s.alive && s.player_rep <= -100 From a893fa6125eacaa13ea9db58d2c8d0e278bee1f3 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 00:54:15 +0000 Subject: [PATCH 008/124] Villagers: count the others' POI tickets once per search pois_in_range (AcquirePoi and the job, bed and meeting point searches) asked, for every candidate POI in range, how many tickets it had, and each answer scanned every other mob for villagers holding it: POIs times mobs per search. The others' tickets are now counted into a map at the first candidate and read from it; this villager's own memories are still checked per POI, as before. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- .../horizon-client/src/mobs/villager_brain.rs | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/crates/horizon-client/src/mobs/villager_brain.rs b/crates/horizon-client/src/mobs/villager_brain.rs index 5b8500c0d..e981e4dda 100644 --- a/crates/horizon-client/src/mobs/villager_brain.rs +++ b/crates/horizon-client/src/mobs/villager_brain.rs @@ -898,8 +898,9 @@ fn poi_matches(m: &Mob, f: PoiFilter, p: Poi) -> bool { _ => false, } } -/// Tickets taken on a POI: by this villager's memories and by others'. -fn tickets(m: &Mob, w: &MobWorld, p: [i32; 3]) -> usize { +/// Tickets taken on a POI: by this villager's memories and by others' +/// (counted once per search by [`others_tickets`]). +fn tickets(m: &Mob, others: &crate::fast_hash::FxHashMap<[i32; 3], usize>, p: [i32; 3]) -> usize { let mut n = 0; if let Some(mm) = mem_ref(m) { for s in [ @@ -913,16 +914,22 @@ fn tickets(m: &Mob, w: &MobWorld, p: [i32; 3]) -> usize { } } } + n + others.get(&p).copied().unwrap_or(0) +} +/// The tickets the other living villagers' POIs take, by POI. +fn others_tickets(m: &Mob, w: &MobWorld) -> crate::fast_hash::FxHashMap<[i32; 3], usize> { + let mut taken = crate::fast_hash::FxHashMap::default(); for o in w.others { if o.id != m.id && o.alive && o.species == Species::Villager { - for q in [o.job, o.bed, o.potential_job, o.meeting] { - if q == Some(p) { - n += 1; - } + for q in [o.job, o.bed, o.potential_job, o.meeting] + .into_iter() + .flatten() + { + *taken.entry(q).or_insert(0) += 1; } } } - n + taken } /// The POIs of chunk `(qx, qz)` from `y0` to `y1` (within the world), in /// scan order: y, then z, then x. Only the indexed POI blocks of the sections @@ -962,6 +969,8 @@ fn pois_in_range( ) -> Vec<(Poi, [i32; 3])> { let c = block_of(m.position); let mut found = Vec::new(); + // Every candidate asks how taken it is: count the others' once. + let mut taken = None; let r2 = i64::from(radius) * i64::from(radius); let (cx, cz) = (c[0] >> 4, c[2] >> 4); let chunks = radius.div_euclid(16) + 1; @@ -974,7 +983,8 @@ fn pois_in_range( || (p[2] - c[2]).abs() > radius || dist_sqr(p, c) > r2 || !poi_matches(m, f, poi) - || tickets(m, w, p) >= poi.max_tickets() + || tickets(m, taken.get_or_insert_with(|| others_tickets(m, w)), p) + >= poi.max_tickets() { return; } From 3917f35e3e781954186d7c2c5be5fa739a333e5e Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 00:07:18 +0000 Subject: [PATCH 009/124] Natural spawning: answer mob box queries from a grid after a few scans GameLevel's isUnobstructed, noCollision (living shulkers) and the spawners' entity count each scanned every mob, computing each 1.5 KB mob's box. A spawn pass holds the game immutably, so after eight queries it builds a grid of the mobs' boxes once and reads candidates from it; the first queries still scan, as the build costs a few scans. 100 box tests, 2000 mobs: 3.48 -> 0.31 ms (grid build included) Also gives the mob_ai_probe example the new others_index field. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- .../horizon-client/examples/mob_ai_probe.rs | 1 + .../src/game/natural_spawning.rs | 88 ++++++++++++++++--- crates/horizon-client/src/mobs.rs | 52 +++++++++++ 3 files changed, 127 insertions(+), 14 deletions(-) diff --git a/crates/horizon-client/examples/mob_ai_probe.rs b/crates/horizon-client/examples/mob_ai_probe.rs index 2a4969221..66d06aa84 100644 --- a/crates/horizon-client/examples/mob_ai_probe.rs +++ b/crates/horizon-client/examples/mob_ai_probe.rs @@ -1027,6 +1027,7 @@ fn main() -> horizon_client::Result<()> { nearest_player: player.as_ref().filter(|p| !p.spectator).map(|p| p.position), others: $snapshot, others_grid: $grid, + others_index: None, daylight: (world.day_time.rem_euclid(24000)) < 12000, game_time: world.game_time, day_time: world.day_time, diff --git a/crates/horizon-client/src/game/natural_spawning.rs b/crates/horizon-client/src/game/natural_spawning.rs index 0fab09bde..6e7ddf500 100644 --- a/crates/horizon-client/src/game/natural_spawning.rs +++ b/crates/horizon-client/src/game/natural_spawning.rs @@ -9,6 +9,7 @@ use horizon_client::{ mobs::{ Mob, MobWorld, Species, data::{self, MobData}, + seen_grid::SeenGrid, spawn_rules::{Dimension, SpawnLevel}, spawn_selection::{ self, Category, Counted, Heightmap, NaturalLevel, SpawnCost, SpawnPlayer, SpawnRequest, @@ -75,8 +76,72 @@ pub(super) struct GameLevel<'g, 'a> { pub(super) pending: std::cell::RefCell>, /// The types and boxes of what a spawner created this tick. pub(super) pending_ids: std::cell::RefCell>, + /// The game's mobs by box, built once a pass has asked more than + /// [`SCANNED_QUERIES`] box queries (the mobs do not change while it + /// holds the game); the first ones scan, as the build costs a few scans. + pub(super) boxes: std::cell::OnceCell, + pub(super) box_queries: std::cell::Cell, +} +const SCANNED_QUERIES: u32 = 8; +/// A mob not removed: its index in `Game::mobs`, box, alive, and whether +/// it is a living shulker. +type MobBox = (usize, Aabb, bool, bool); +fn mob_box(i: usize, m: &Mob) -> MobBox { + let alive = m.alive(); + ( + i, + m.bounds(), + alive, + alive && m.species() == Species::Shulker, + ) +} +/// The boxes of the game's mobs not removed, and a grid over them. +pub(super) struct MobBoxes { + entries: Vec, + grid: SeenGrid, +} +impl MobBoxes { + fn new(mobs: &[Mob]) -> Self { + let entries: Vec<_> = mobs + .iter() + .enumerate() + .filter(|(_, m)| !m.removed) + .map(|(i, m)| mob_box(i, m)) + .collect(); + let grid = SeenGrid::new(entries.iter().map(|e| e.1)); + Self { entries, grid } + } + /// The entries whose boxes meet `area` (and possibly a few more). + fn near(&self, area: Aabb) -> impl Iterator + '_ { + let mut near = Vec::new(); + let indexed = self.grid.candidates(area, &mut near); + let all = (!indexed).then(|| self.entries.iter()); + near.into_iter() + .map(|i| self.entries[i as usize]) + .chain(all.into_iter().flatten().copied()) + } } impl GameLevel<'_, '_> { + /// The mobs not removed whose boxes may meet `area` (callers test them). + fn mobs_near(&self, area: Aabb) -> Box + '_> { + let asked = self.box_queries.get() + 1; + self.box_queries.set(asked); + if asked <= SCANNED_QUERIES && self.boxes.get().is_none() { + return Box::new( + self.game + .mobs + .iter() + .enumerate() + .filter(|(_, m)| !m.removed) + .map(|(i, m)| mob_box(i, m)), + ); + } + Box::new( + self.boxes + .get_or_init(|| MobBoxes::new(&self.game.mobs)) + .near(area), + ) + } fn biome(&self, x: i32, y: i32, z: i32) -> Option<&BiomeInfo> { let chunk = self.game.world.chunks.get(&(x >> 4, z >> 4))?; Some(self.game.res.biomes().get(chunk.biome(x, y, z))) @@ -218,12 +283,9 @@ impl NaturalLevel for GameLevel<'_, '_> { // getEntityCollisions(null, box): living shulkers are solid. let grown = bounds.inflate(Vec3::new(1e-7, 1e-7, 1e-7)); no_collision_empty(&mut scene, bounds) - && !self.game.mobs.iter().any(|m| { - m.species() == Species::Shulker - && m.alive() - && !m.removed - && m.bounds().intersects(grown) - }) + && !self + .mobs_near(grown) + .any(|(_, b, _, shulker)| shulker && b.intersects(grown)) && !self .pending_ids .borrow() @@ -238,10 +300,8 @@ impl NaturalLevel for GameLevel<'_, '_> { !player && !self.pending.borrow().iter().any(|b| b.intersects(bounds)) && !self - .game - .mobs - .iter() - .any(|m| m.alive() && !m.removed && m.bounds().intersects(bounds)) + .mobs_near(bounds) + .any(|(_, b, alive, _)| alive && b.intersects(bounds)) } fn spawn_mobs(&self) -> bool { self.game.ui.rules.bool("spawn_mobs") @@ -283,10 +343,8 @@ impl horizon_client::mobs::spawner::SpawnerLevel for GameLevel<'_, '_> { } fn count_entities(&self, id: &str, bounds: Aabb) -> usize { let mobs = self - .game - .mobs - .iter() - .filter(|m| !m.removed && m.kind.id == id && m.bounds().intersects(bounds)) + .mobs_near(bounds) + .filter(|&(i, b, ..)| self.game.mobs[i].kind.id == id && b.intersects(bounds)) .count(); let pending = self .pending_ids @@ -305,6 +363,8 @@ impl<'a> Game<'a> { players: self.level_players(), pending: Default::default(), pending_ids: Default::default(), + boxes: Default::default(), + box_queries: Default::default(), } } /// Creates a mob the spawner accepted, finalized the game's way; the diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 10d5c19cb..ecce6fede 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -2346,6 +2346,58 @@ mod sensor_scan_tests { } } + /// Natural spawning's `isUnobstructed` box test against every mob (real + /// `Mob`s), by scan and by a grid built once for the pass. + #[test] + #[ignore] + fn spawn_box_cost() { + let kind = kind(); + for n in [500u32, 1000, 2000] { + let (seen, _) = herd(n, 384.0); + let mobs: Vec = seen + .iter() + .map(|s| Mob::new(s.id, kind.clone(), s.position, 0.0)) + .collect(); + let mut seed = 7u64; + let mut next = |range: f64| { + seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1); + (seed >> 11) as f64 / (1u64 << 53) as f64 * range + }; + let queries: Vec = (0..100) + .map(|_| { + let (x, z) = (next(384.0), next(384.0)); + Aabb::new(Vec3::new(x, 64.0, z), Vec3::new(x + 0.9, 65.4, z + 0.9)) + }) + .collect(); + let t = std::time::Instant::now(); + let a: Vec = queries + .iter() + .map(|&q| { + mobs.iter() + .any(|m| m.alive() && !m.removed && m.bounds().intersects(q)) + }) + .collect(); + let before = t.elapsed().as_secs_f64() * 1e3; + let t = std::time::Instant::now(); + let boxes: Vec = mobs.iter().map(|m| m.bounds()).collect(); + let grid = seen_grid::SeenGrid::new(boxes.iter().copied()); + let build = t.elapsed().as_secs_f64() * 1e3; + let mut near = Vec::new(); + let b: Vec = queries + .iter() + .map(|&q| { + assert!(grid.candidates(q, &mut near)); + near.iter().any(|&i| boxes[i as usize].intersects(q)) + }) + .collect(); + let after = t.elapsed().as_secs_f64() * 1e3; + assert_eq!(a, b); + println!( + "{n} mobs, 100 spawn box tests: {before:.3} ms -> {after:.3} ms (grid build {build:.3} ms included)" + ); + } + } + /// One tick of sensing: a twentieth of the mobs fire their sensor. #[test] #[ignore] From 863a3091612f065e9118fc94c709249fa14ada68 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 00:34:52 +0000 Subject: [PATCH 010/124] Allays and piglins: hoist per-tick lookups out of the per-mob loop tick_allays scanned every block entity of the level for each allay, every tick, to find a playing jukebox within 10 blocks. The playing jukeboxes are now collected once, at the first allay (no allay starts or stops one). piglin_pickups looked up the piglin_loved tag and hashed every item entity's id once per piglin. Each item's flag is now computed once and kept until an item is added (always pushed at the end under a new id) or removed. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/game/allays.rs | 22 ++++++++++++++++------ crates/horizon-client/src/game/piglins.rs | 14 +++++++++++++- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/crates/horizon-client/src/game/allays.rs b/crates/horizon-client/src/game/allays.rs index 52dfa7e1c..bed835663 100644 --- a/crates/horizon-client/src/game/allays.rs +++ b/crates/horizon-client/src/game/allays.rs @@ -9,18 +9,28 @@ impl Game<'_> { pub(super) fn tick_allays(&mut self) { let griefing = self.ui.rules.bool("mob_griefing"); let player = self.player.as_ref().map(|p| p.position); + // The playing jukeboxes, found at the first allay: no allay starts + // or stops one, and the block entities run into the thousands. + let mut jukeboxes: Option> = None; for i in 0..self.mobs.len() { if self.mobs[i].species() != Species::Allay || !self.mobs[i].alive() { continue; } let pos = self.mobs[i].position; // Dancing: a playing jukebox within 10 blocks. - let dancing = self.world.block_entities.iter().any(|(k, e)| { - matches!(e, BlockEntity::Jukebox(j) if j.playing) - && (f64::from(k.0) + 0.5 - pos.x).powi(2) - + (f64::from(k.1) + 0.5 - pos.y).powi(2) - + (f64::from(k.2) + 0.5 - pos.z).powi(2) - <= 100.0 + let jukeboxes = jukeboxes.get_or_insert_with(|| { + self.world + .block_entities + .iter() + .filter(|(_, e)| matches!(e, BlockEntity::Jukebox(j) if j.playing)) + .map(|(k, _)| *k) + .collect() + }); + let dancing = jukeboxes.iter().any(|k| { + (f64::from(k.0) + 0.5 - pos.x).powi(2) + + (f64::from(k.1) + 0.5 - pos.y).powi(2) + + (f64::from(k.2) + 0.5 - pos.z).powi(2) + <= 100.0 }); self.mobs[i].extra.flag = dancing && !horizon_client::mobs::goals::should_panic(&self.mobs[i]); diff --git a/crates/horizon-client/src/game/piglins.rs b/crates/horizon-client/src/game/piglins.rs index 9df5311aa..6a1e2d6b7 100644 --- a/crates/horizon-client/src/game/piglins.rs +++ b/crates/horizon-client/src/game/piglins.rs @@ -34,6 +34,9 @@ impl Game<'_> { if !self.ui.rules.bool("mob_griefing") { return; } + // Whether each item is piglin-loved, kept until an item is added or + // removed (added ones are pushed at the end under a new id). + let mut loved: (Option<(usize, Option)>, Vec) = (None, Vec::new()); for i in 0..self.mobs.len() { let m = &self.mobs[i]; if m.species() != Species::Piglin @@ -46,11 +49,20 @@ impl Game<'_> { continue; } let pos = m.position; + let key = (self.dropped.len(), self.dropped.last().map(|e| e.id)); + if loved.0 != Some(key) { + loved.0 = Some(key); + loved.1 = self + .dropped + .iter() + .map(|e| self.piglin_loved(&e.stack.item)) + .collect(); + } let best = self .dropped .iter() .enumerate() - .filter(|(_, e)| self.piglin_loved(&e.stack.item)) + .filter(|&(j, _)| loved.1[j]) .map(|(j, e)| (j, e.position.distance_squared(pos))) .filter(|(_, d)| *d <= 81.0) .min_by(|a, b| a.1.total_cmp(&b.1)); From cb0bbe7d19e0123e11915244f906791786dd244d Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 00:33:01 +0000 Subject: [PATCH 011/124] Sculk: skip the loaded-chunk diff while no chunk came or went sculk_sync_index collected every loaded chunk key into a set and diffed it against the known chunks every ten ticks, thousands of keys on a busy server. It now remembers the chunk map's membership revision and skips the diff while it is unchanged; a replaced chunk keeps its key and was not rescanned before either. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/game/sculk.rs | 7 ++++++- crates/horizon-client/src/game/sculk_fixture.rs | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/game/sculk.rs b/crates/horizon-client/src/game/sculk.rs index 096562608..8667f305f 100644 --- a/crates/horizon-client/src/game/sculk.rs +++ b/crates/horizon-client/src/game/sculk.rs @@ -71,6 +71,8 @@ pub(super) struct SculkState { /// The sculk block entities in the order they were added. index: Index, pub known: HashSet<(i32, i32)>, + /// The chunk map's membership revision `known` was taken at. + pub known_revision: Option, // Keyed by mob ids the server gives out: the fast hasher is safe. steps: FxHashMap, damaged: FxHashMap, @@ -336,7 +338,10 @@ impl Game<'_> { /// the block writes recorded since the last tick. pub(super) fn sculk_sync_index(&mut self) { // Chunks that arrived or left (every half second is plenty). - if self.sculk.ticks.is_multiple_of(10) { + // Nothing to do while no chunk came or went. + let revision = self.world.chunks.membership_revision(); + if self.sculk.ticks.is_multiple_of(10) && self.sculk.known_revision != Some(revision) { + self.sculk.known_revision = Some(revision); let loaded: HashSet<(i32, i32)> = self.world.chunks.keys().copied().collect(); let arrived: Vec<(i32, i32)> = loaded.difference(&self.sculk.known).copied().collect(); for key in arrived { diff --git a/crates/horizon-client/src/game/sculk_fixture.rs b/crates/horizon-client/src/game/sculk_fixture.rs index 890e571b8..df376a7e3 100644 --- a/crates/horizon-client/src/game/sculk_fixture.rs +++ b/crates/horizon-client/src/game/sculk_fixture.rs @@ -433,6 +433,7 @@ impl Game<'_> { }); self.level_events.clear(); self.sculk.known = self.world.chunks.keys().copied().collect(); + self.sculk.known_revision = Some(self.world.chunks.membership_revision()); self.sculk_sync_index(); let mut names: HashMap = HashMap::new(); let mut sources: Vec = Vec::new(); From 21998a7e6ac1ca57b937a849d51ef67b0a87141f Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 02:45:32 +0200 Subject: [PATCH 012/124] Shader packs: read pack sources as Iris's JCPP does; legalise what naga refuses Preprocessor (GLSL and shaders.properties), as JCPP does: - a #define replaces an earlier macro (Bloop, Steadfast: DefineRedefined); - in #if, a float counts as its integer part (Steadfast's shaders.properties float token), and a macro always defined empty counts as 0 (Iris's MC_GL_VENDOR_OTHER, Bloop's TAA_USE_YCOCG); - an #if may run to the end of the input (Bloop's taa.fsh); - #error "..." lexes in skipped blocks (Photon); - shaders.properties keeps its empty lines and hides backslashes from the preprocessor (IRIS_PASSTHROUGHBACKSLASH), so a continued value no longer swallows the next directive or line (Photon, Bliss, Steadfast's custom uniforms; Complementary's isCold uniform is now complete). Legaliser: % through overloads (naga won't take ivec2 % int: Noble), sqrt of a negative constant left to the GPU (Bloop's NaN), constants naga can't fold (?:, mix: Bliss) become globals set in main, and 16/8-bit types (u16vec2 & co., Noble on NVIDIA) become their 32-bit ones. horizon-client: real_packs_load runs the game's own load over the packs in HORIZON_TEST_SHADERPACKS (the compatibility table's source). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/java_pack.rs | 39 +++ crates/horizon-shaderpack/src/preprocess.rs | 180 ++++++++++++- crates/horizon-shaderpack/src/properties.rs | 68 ++++- crates/horizon-shaderpack/src/translate.rs | 252 ++++++++++++++++++- crates/horizon-shaderpack/tests/pack.rs | 63 +++++ crates/horizon-shaderpack/tests/translate.rs | 41 +++ 6 files changed, 629 insertions(+), 14 deletions(-) diff --git a/crates/horizon-client/src/java_pack.rs b/crates/horizon-client/src/java_pack.rs index fb170d152..a5bc2904e 100644 --- a/crates/horizon-client/src/java_pack.rs +++ b/crates/horizon-client/src/java_pack.rs @@ -179,4 +179,43 @@ mod tests { ); assert_eq!(vendor_name(0, "Apple M4 Pro"), "Apple"); } + + /// The packs in `HORIZON_TEST_SHADERPACKS` (folders or zips) load as the + /// game loads them, with their default options: the compatibility table + /// of docs/shader-packs.md. Skipped when the variable isn't set. + #[test] + fn real_packs_load() { + let Some(dir) = std::env::var_os("HORIZON_TEST_SHADERPACKS") else { + return; + }; + let mut entries: Vec<_> = std::fs::read_dir(dir) + .unwrap() + .flatten() + .map(|e| e.path()) + .filter(|p| is_java_pack(p)) + .collect(); + entries.sort(); + let mut failures = Vec::new(); + for path in entries { + let name = path.file_name().unwrap().to_string_lossy().into_owned(); + match load(&path, &BTreeMap::new(), "Apple", "Apple M4 Pro") { + Ok(pack) => { + let folders: Vec = pack + .folders + .iter() + .map(|f| { + let n = f.passes.len() + usize::from(f.final_pass.is_some()); + format!("{} {n} passes", f.folder) + }) + .collect(); + println!("{name}: loads ({})", folders.join(", ")); + } + Err(e) => { + println!("{name}: {e}"); + failures.push(name); + } + } + } + assert!(failures.is_empty(), "{failures:?}"); + } } diff --git a/crates/horizon-shaderpack/src/preprocess.rs b/crates/horizon-shaderpack/src/preprocess.rs index 18c890496..8a0c4e694 100644 --- a/crates/horizon-shaderpack/src/preprocess.rs +++ b/crates/horizon-shaderpack/src/preprocess.rs @@ -6,8 +6,9 @@ use crate::include::Inlined; use pp_rs::{ pp::Preprocessor, - token::{Punct, TokenValue}, + token::{PreprocessorError, Punct, TokenValue}, }; +use std::collections::HashMap; /// The version of Minecraft packs are told they run on (`MC_VERSION`, /// Iris's `formatVersionString("26.3")`). @@ -286,14 +287,176 @@ fn mark_comments(text: &str) -> (String, Vec) { (out, comments) } +/// Whether trimmed `line` is an `#if` or `#elif` directive. +pub(crate) fn is_condition(line: &str) -> bool { + line.strip_prefix('#') + .map(str::trim_start) + .and_then(|d| d.strip_prefix("elif").or_else(|| d.strip_prefix("if"))) + .is_some_and(|r| !r.starts_with(|c: char| c.is_ascii_alphanumeric() || c == '_')) +} + +/// Whether `word` is a float literal. +pub(crate) fn is_float(word: &str) -> bool { + word.starts_with(|c: char| c.is_ascii_digit() || c == '.') + && !word.starts_with("0x") + && !word.starts_with("0X") + && word.contains(['.', 'e', 'E']) +} + +/// What JCPP's `#if` makes of a number (`Preprocessor.expr` takes +/// `NumericValue.longValue`): a float counts as its integer part, so +/// `0.75` is 0 and `1.5e1` is 10. `None` for a non-number. +pub(crate) fn jcpp_integer(number: &str) -> Option { + let number = number.trim_end_matches(['f', 'F', 'l', 'L', 'u', 'U']); + let (mantissa, exponent) = match number.find(['e', 'E']) { + Some(i) => (&number[..i], number[i + 1..].parse::().ok()?), + None => (number, 0), + }; + let integer = mantissa.split('.').next()?; + let value = if integer.is_empty() { + 0 + } else { + integer.parse::().ok()? + }; + Some((value as f64 * 10f64.powi(exponent)) as i64) +} + +/// `line` with each word (identifier or number, with an exponent's sign: +/// `1e-3`) replaced by what `f` returns for it, if anything. The directive's +/// name and the names after `defined` are kept. +pub(crate) fn rewrite_condition(line: &str, f: impl Fn(&str) -> Option) -> String { + let mut out = String::with_capacity(line.len()); + let (mut rest, mut first, mut after_defined) = (line, true, false); + while let Some(c) = rest.chars().next() { + if !(c.is_ascii_alphanumeric() || c == '_' || c == '.') { + if !c.is_whitespace() && c != '(' && c != '#' { + after_defined = false; + } + out.push(c); + rest = &rest[c.len_utf8()..]; + continue; + } + let bytes = rest.as_bytes(); + let mut len = 0; + while let Some(&b) = bytes.get(len) { + let sign = (b == b'+' || b == b'-') + && bytes[0].is_ascii_digit() + && matches!(bytes[len - 1], b'e' | b'E'); + if !(b.is_ascii_alphanumeric() || b == b'_' || b == b'.' || sign) { + break; + } + len += 1; + } + let word = &rest[..len]; + let kept = first || after_defined; + match (!kept).then(|| f(word)).flatten() { + Some(replacement) => out.push_str(&replacement), + None => out.push_str(word), + } + after_defined = word == "defined"; + first = false; + rest = &rest[len..]; + } + out +} + +/// Make pp-rs read `text` as JCPP, Iris's preprocessor, does: +/// - a `#define` replaces an earlier macro (`Preprocessor.addMacro`; Bloop +/// and Steadfast redefine theirs), where pp-rs refuses a different +/// redefinition: an `#undef` line goes before every `#define`; +/// - in `#if`/`#elif`, a float counts as its integer part, and so does a +/// macro always defined as the same float (an option: `#define LEVEL 0.5`); +/// a macro always defined empty (Iris's `MC_GL_VENDOR_OTHER`, Bloop's +/// `#define TAA_USE_YCOCG`) counts as 0: JCPP reports the missing operand +/// and goes on with 0. pp-rs stops on both; +/// - `#error "..."` (Photon's guard in programs it disables) lexes: pp-rs +/// has no string tokens and fails on the quote, even in a skipped block. +/// +/// Returns the text and the added lines' 1-based numbers in it. +fn jcpp_compatible(text: &str, env: &Environment) -> (String, Vec) { + // A `#define` line's name and body. + let define = |l: &str| { + let rest = l + .trim_start() + .strip_prefix('#')? + .trim_start() + .strip_prefix("define") + .filter(|rest| rest.starts_with([' ', '\t']))? + .trim_start(); + let end = rest + .find(|c: char| !(c.is_ascii_alphanumeric() || c == '_')) + .unwrap_or(rest.len()); + let (name, body) = rest.split_at(end); + let body = body.split("//").next().unwrap_or("").trim(); + (!name.is_empty()).then(|| (name.to_owned(), body.to_owned())) + }; + // Each macro's body, if every definition has the same one. + let mut bodies: HashMap> = HashMap::new(); + let definitions = env + .defines + .iter() + .cloned() + .chain(text.lines().filter_map(define)); + for (name, body) in definitions { + bodies + .entry(name) + .and_modify(|b| { + if b.as_ref() != Some(&body) { + *b = None; + } + }) + .or_insert(Some(body)); + } + let number = |word: &str| -> Option { + let word = match bodies.get(word) { + Some(Some(body)) if body.is_empty() => return Some("0".into()), + Some(Some(body)) => body.as_str(), + Some(None) => return None, + None => word, + }; + is_float(word) + .then(|| jcpp_integer(word)) + .flatten() + .map(|n| n.to_string()) + }; + let mut out = String::with_capacity(text.len() + text.len() / 16); + let mut added = Vec::new(); + let (mut line, mut continued) = (1, false); + for l in text.split_inclusive('\n') { + if !continued && let Some((name, _)) = define(l) { + out.push_str(&format!("#undef {name}\n")); + added.push(line); + line += 1; + } + let directive = l.trim_start().strip_prefix('#').map(str::trim_start); + if !continued + && directive.is_some_and(|d| d.starts_with("error") || d.starts_with("warning")) + { + out.push_str(&l.replace(['"', '\''], "")); + } else if !continued && is_condition(l.trim_start()) { + out.push_str(&rewrite_condition(l, number)); + } else { + out.push_str(l); + } + continued = l.trim_end().ends_with('\\'); + line += 1; + } + (out, added) +} + /// Expand `source` with `env`'s macros. Errors name the file and line. pub fn preprocess(source: &Inlined, env: &Environment) -> Result { let (marked, comments) = mark_comments(&source.text); - let text = &marked; + let (text, added) = jcpp_compatible(&marked, env); + let text = &text; let line_starts: Vec = std::iter::once(0) .chain(text.match_indices('\n').map(|(i, _)| i + 1)) .collect(); - let line_of = |offset: u32| line_starts.partition_point(|&s| s <= offset as usize) as u32; + // Lines of the source: the added `#undef` lines don't count. + let line_of = |offset: u32| { + let line = line_starts.partition_point(|&s| s <= offset as usize) as u32; + line - added.partition_point(|&a| a < line) as u32 + }; let mut pp = Preprocessor::new(text); for (name, value) in &env.defines { pp.add_define(name, value) @@ -307,8 +470,15 @@ pub fn preprocess(source: &Inlined, env: &Environment) -> Result break, + item => { + item.map_err(|(e, at)| format!("{}: {e:?}", source.locate(line_of(at.start))))? + } + }; let at = line_of(token.location.start); let piece = match token.value { TokenValue::Version(v) => { diff --git a/crates/horizon-shaderpack/src/properties.rs b/crates/horizon-shaderpack/src/properties.rs index 8c3f29c8f..3c6ee251c 100644 --- a/crates/horizon-shaderpack/src/properties.rs +++ b/crates/horizon-shaderpack/src/properties.rs @@ -6,7 +6,7 @@ //! so `#if` blocks pick the directives that apply. use crate::options::{OptionSet, OptionValues}; -use crate::preprocess::Environment; +use crate::preprocess::{Environment, is_condition, is_float, jcpp_integer, rewrite_condition}; use pp_rs::{pp::Preprocessor, token::TokenValue}; use std::collections::BTreeMap; @@ -120,9 +120,32 @@ const DIRECTIVES: &[&str] = &[ "#warning", "#pragma", "#line", "#include", ]; +/// An `#if`/`#elif` line with its floats as JCPP reads them (Steadfast tests +/// `#if ATMOSPHERE_FOG_STRENGTH_NOON == 0.75` with the option at 0.5, which +/// Iris reads as `0 == 0`): float literals and the options set to a float +/// become their integer part. +fn jcpp_condition(line: &str, options: &OptionSet, values: &OptionValues) -> String { + rewrite_condition(line, |word| { + let value = match options.strings.get(word) { + Some(o) => values.strings.get(word).unwrap_or(&o.default).as_str(), + None => word, + }; + is_float(value) + .then(|| jcpp_integer(value)) + .flatten() + .map(|n| n.to_string()) + }) +} + +/// What backslashes are while the macros expand, as in Iris: the +/// preprocessor must not join a `\`-continued value with the directive on +/// the next line (Photon ends an `#if`'d value with `) \` before `#else`). +const BACKSLASH: &str = "IRIS_PASSTHROUGHBACKSLASH"; + /// `PropertiesPreprocessor.preprocessSource`: comment lines dropped, other -/// `#`s removed, then the macros expanded. Tokens keep their source spelling -/// and spacing (`0.50` stays `0.50`, `` stays one word). +/// `#`s removed, then the macros expanded with the backslashes hidden. +/// Tokens keep their source spelling and spacing (`0.50` stays `0.50`, +/// `` stays one word). pub fn preprocess( text: &str, env: &Environment, @@ -132,7 +155,6 @@ pub fn preprocess( let filtered: String = text .lines() .map(str::trim) - .filter(|l| !l.is_empty()) .map(|l| match l.starts_with('#') { // JCPP lets a #define replace a macro (Complementary sets // COLORED_LIGHTING 0 on macOS); pp-rs refuses, so undefine first. @@ -142,11 +164,12 @@ pub fn preprocess( None => l.to_owned(), } } + true if is_condition(l) => jcpp_condition(l, options, values), true if DIRECTIVES.iter().any(|d| l.starts_with(d)) => l.to_owned(), true => String::new(), false => l.replace('#', ""), }) - .map(|l| l + "\n") + .map(|l| l.replace('\\', BACKSLASH) + "\n") .collect(); let mut pp = Preprocessor::new(&filtered); let mut define = |name: &str, value: &str| { @@ -202,7 +225,7 @@ pub fn preprocess( }); } out.push('\n'); - Ok(out) + Ok(out.replace(BACKSLASH, "\\")) } /// One element of an option screen (`OptionMenuElement`). @@ -698,6 +721,39 @@ mod tests { ); } + #[test] + fn floats_in_conditions_count_as_their_integer_part() { + assert_eq!( + [".5", "0.75", "1.0", "2.5f", "1.5e1", "7", "0x10"].map(jcpp_integer), + [Some(0), Some(0), Some(1), Some(2), Some(10), Some(7), None] + ); + // Steadfast's fog strength: the option is 0.5, so Iris takes the + // 0.75 branch (0 == 0). + let set = options(&["#define FOG 0.5 // [0.0 0.5 0.75 1.0]"]); + let text = "\ +#if FOG == 1.0 +a = 1 +#elif FOG == 0.75 +a = 0.75 +#else +a = 0.5 +#endif +#if(FOG<1e1) +b = 1 +#endif +"; + let env = Environment { + defines: Vec::new(), + }; + let values = OptionValues::default(); + let out = preprocess(text, &env, &set, &values).unwrap(); + assert_eq!(out, "a = 0.75\nb = 1\n"); + assert_eq!( + jcpp_condition("#elif FOG == 0.75 && X > 2.5", &set, &values), + "#elif 0 == 0 && X > 2" + ); + } + #[test] fn boolean_expressions() { let set = options(&["#define A", "//#define B", "#define C"]); diff --git a/crates/horizon-shaderpack/src/translate.rs b/crates/horizon-shaderpack/src/translate.rs index f9a61169e..708ab5771 100644 --- a/crates/horizon-shaderpack/src/translate.rs +++ b/crates/horizon-shaderpack/src/translate.rs @@ -18,7 +18,11 @@ //! swizzled shadow lookup is widened first (naga has no scalar swizzle); //! - global initialisers run at the start of `main` (drivers accept //! non-constant ones), unsized global arrays are sized from them; -//! - `isnan`, `isinf` and `modf` are helper functions (WGSL has none). +//! - `isnan`, `isinf` and `modf` are helper functions (WGSL has none); +//! - what naga refuses and drivers accept: `%` goes through overloads (naga +//! won't take `ivec2 % int`), `sqrt` of a negative constant isn't folded, +//! constants naga can't fold (`?:`, `mix`) become globals set in `main`, +//! and 16/8-bit types are their 32-bit ones. //! //! The programs of one pass share a [`Link`], so their locations, bindings and //! uniform block agree. @@ -234,6 +238,81 @@ vec3 iris_modf(vec3 x, out vec3 i) { i = trunc(x); return x - i; } vec4 iris_modf(vec4 x, out vec4 i) { i = trunc(x); return x - i; } "; +/// `%` as overloads: naga's GLSL front end splats a scalar operand for +/// `+ - / & ^ |` but not for `%`, and rejects `ivec2(uv) % 256` (Noble). +fn modulo_helpers() -> String { + let mut out = String::new(); + for (s, v) in [("int", "ivec"), ("uint", "uvec")] { + out.push_str(&format!("{s} iris_mod({s} a, {s} b) {{ return a % b; }}\n")); + for n in 2..=4 { + let v = format!("{v}{n}"); + out.push_str(&format!("{v} iris_mod({v} a, {v} b) {{ return a % b; }}\n")); + out.push_str(&format!( + "{v} iris_mod({v} a, {s} b) {{ return a % {v}(b); }}\n" + )); + out.push_str(&format!( + "{v} iris_mod({s} a, {v} b) {{ return {v}(a) % b; }}\n" + )); + } + } + out +} + +/// 16- and 8-bit types (`GL_NV_gpu_shader5`, `GL_EXT_shader_explicit_arithmetic_types`) +/// and their built-ins, as the 32-bit ones WGSL has: Noble declares +/// `u16vec2` & co. when Iris reports an NVIDIA GPU, and falls back to these +/// same aliases elsewhere. +const SMALL_TYPES: &[(&str, &str)] = &[ + ("float16_t", "float"), + ("f16vec2", "vec2"), + ("f16vec3", "vec3"), + ("f16vec4", "vec4"), + ("int16_t", "int"), + ("i16vec2", "ivec2"), + ("i16vec3", "ivec3"), + ("i16vec4", "ivec4"), + ("uint16_t", "uint"), + ("u16vec2", "uvec2"), + ("u16vec3", "uvec3"), + ("u16vec4", "uvec4"), + ("int8_t", "int"), + ("i8vec2", "ivec2"), + ("i8vec3", "ivec3"), + ("i8vec4", "ivec4"), + ("uint8_t", "uint"), + ("u8vec2", "uvec2"), + ("u8vec3", "uvec3"), + ("u8vec4", "uvec4"), + ("packFloat2x16", "packHalf2x16"), + ("unpackFloat2x16", "unpackHalf2x16"), +]; + +/// `source` with [`SMALL_TYPES`] replaced, word by word. +fn widen_small_types(source: &str) -> std::borrow::Cow<'_, str> { + if !SMALL_TYPES.iter().any(|(t, _)| source.contains(t)) { + return source.into(); + } + let mut out = String::with_capacity(source.len()); + let mut rest = source; + while let Some(c) = rest.chars().next() { + let len = if c.is_ascii_alphanumeric() || c == '_' { + rest.find(|c: char| !(c.is_ascii_alphanumeric() || c == '_')) + .unwrap_or(rest.len()) + } else { + c.len_utf8() + }; + let word = &rest[..len]; + out.push_str( + SMALL_TYPES + .iter() + .find(|(t, _)| *t == word) + .map_or(word, |(_, w)| w), + ); + rest = &rest[len..]; + } + out.into() +} + /// Translate one preprocessed program. `locate` turns a line of `source` /// into `file:line` for errors. pub fn translate( @@ -248,7 +327,8 @@ pub fn translate( Stage::Compute => naga::ShaderStage::Compute, _ => return Err(format!("{stage:?} programs are not supported")), }; - let mut unit = ast::TranslationUnit::parse(source).map_err(|e| { + let source = widen_small_types(source); + let mut unit = ast::TranslationUnit::parse(&source).map_err(|e| { let line = e.line() + 1; format!("{}: {}", locate(line), e.inner()) })?; @@ -281,6 +361,111 @@ pub fn translate( }) } +/// The GLSL built-ins naga's constant evaluator folds (`MathFunction`s it +/// implements), besides type constructors. +const FOLDABLE: &[&str] = &[ + "abs", + "acos", + "acosh", + "asin", + "asinh", + "atan", + "atanh", + "ceil", + "clamp", + "cos", + "cosh", + "cross", + "degrees", + "distance", + "dot", + "exp", + "exp2", + "floor", + "fma", + "fract", + "inversesqrt", + "length", + "log", + "log2", + "max", + "min", + "normalize", + "pow", + "radians", + "round", + "sign", + "sin", + "sinh", + "sqrt", + "step", + "tan", + "tanh", + "trunc", +]; + +/// Whether a constant's initialisers need what naga can't fold and drivers +/// do: a `?:`, another built-in (Bliss's `mix`), or a constant that had to +/// become a global (`runtime`). +fn needs_runtime(list: &ast::InitDeclaratorList, runtime: &HashSet) -> bool { + let constructor = |name: &str| { + let n = name.trim_start_matches(['i', 'u', 'b', 'd']); + matches!(name, "float" | "int" | "uint" | "bool" | "double") + || n.starts_with("vec") + || n.starts_with("mat") + }; + let expr = |e: &ast::Expr| { + fn walk(e: &ast::Expr, f: &dyn Fn(&ast::Expr) -> bool) -> bool { + use ast::ExprData as E; + f(e) || match &e.content { + E::Unary(_, a) | E::PostInc(a) | E::PostDec(a) | E::Dot(a, _) => walk(a, f), + E::Binary(_, a, b) | E::Assignment(a, _, b) | E::Comma(a, b) | E::Bracket(a, b) => { + walk(a, f) || walk(b, f) + } + E::Ternary(a, b, c) => walk(a, f) || walk(b, f) || walk(c, f), + E::FunCall(_, args) => args.iter().any(|a| walk(a, f)), + _ => false, + } + } + walk(e, &|e| match &e.content { + ast::ExprData::Ternary(..) => true, + ast::ExprData::Variable(v) => runtime.contains(v.as_str()), + ast::ExprData::FunCall(f, _) => { + callee(f).is_some_and(|c| !constructor(&c) && !FOLDABLE.contains(&c.as_str())) + } + _ => false, + }) + }; + fn init(i: &ast::Initializer, expr: &dyn Fn(&ast::Expr) -> bool) -> bool { + match &i.content { + ast::InitializerData::Simple(e) => expr(e), + ast::InitializerData::List(items) => items.iter().any(|i| init(i, expr)), + } + } + let tail = list.tail.iter().filter_map(|t| t.initializer.as_ref()); + list.head + .initializer + .iter() + .chain(tail) + .any(|i| init(i, &expr)) +} + +/// A negative number: `-1.0`, `-1`. +fn negative_constant(e: &ast::Expr) -> bool { + use ast::ExprData as E; + match &e.content { + E::Unary(op, a) if op.content == ast::UnaryOpData::Minus => { + matches!( + a.content, + E::FloatConst(v) if v > 0.0 + ) || matches!(a.content, E::IntConst(v) if v > 0) + || matches!(a.content, E::DoubleConst(v) if v > 0.0) + } + E::FloatConst(v) => *v < 0.0, + _ => false, + } +} + fn first_lines(s: &str) -> String { s.lines().take(8).collect::>().join("\n") } @@ -316,6 +501,14 @@ struct Legaliser<'a> { /// Identifiers used, for the implicit declarations. used: HashSet, errors: Vec, + /// Inside a constant initialiser: no helper calls there. + constant: bool, + /// The program uses `%` ([`modulo_helpers`]) or `iris_sqrt`: their + /// helpers go in. + modulo: bool, + nan: bool, + /// Constants turned into globals ([`needs_runtime`]). + runtime_constants: HashSet, } fn var(name: &str) -> ast::Expr { @@ -507,6 +700,10 @@ impl<'a> Legaliser<'a> { declared: HashSet::new(), used: HashSet::new(), errors: Vec::new(), + constant: false, + modulo: false, + nan: false, + runtime_constants: HashSet::new(), } } @@ -736,9 +933,25 @@ impl<'a> Legaliser<'a> { } return Ok(false); } - if has(ast::StorageQualifierData::Const) || has(ast::StorageQualifierData::Shared) { + // A constant naga can't fold becomes a global set at the start of `main`. + if has(ast::StorageQualifierData::Const) && needs_runtime(list, &self.runtime_constants) { + self.runtime_constants + .extend(names.iter().map(|(n, _)| n.clone())); + let q = &mut list.head.ty.qualifier; + if let Some(q) = q { + q.qualifiers.retain(|q| { + !matches!(&q.content, ast::TypeQualifierSpecData::Storage(s) + if s.content == ast::StorageQualifierData::Const) + }); + } + if q.as_ref().is_some_and(|q| q.qualifiers.is_empty()) { + *q = None; + } + } else if has(ast::StorageQualifierData::Const) || has(ast::StorageQualifierData::Shared) { self.sized(list); + self.constant = true; self.global_initialisers(list, false); + self.constant = false; return Ok(true); } self.sized(list); @@ -930,6 +1143,8 @@ impl<'a> Legaliser<'a> { return; }; self.sized(list); + self.constant = + storage(&list.head.ty.qualifier).contains(&ast::StorageQualifierData::Const); if let Some(i) = &mut list.head.initializer { self.initializer(i); } @@ -944,6 +1159,7 @@ impl<'a> Legaliser<'a> { let n = t.ident.ident.as_str().to_owned(); self.declare(&n); } + self.constant = false; } fn initializer(&mut self, i: &mut ast::Initializer) { @@ -1148,6 +1364,24 @@ impl<'a> Legaliser<'a> { | E::FloatConst(_) | E::DoubleConst(_) => {} E::Unary(_, a) | E::PostInc(a) | E::PostDec(a) => self.expr(a), + E::Binary(op, a, b) if op.content == ast::BinaryOpData::Mod && !self.constant => { + self.expr(a); + self.expr(b); + self.modulo = true; + let (a, b) = ( + std::mem::replace(&mut **a, var("_")), + std::mem::replace(&mut **b, var("_")), + ); + *e = call("iris_mod", vec![a, b]); + } + E::Assignment(a, op, b) if op.content == ast::AssignmentOpData::Mod => { + self.expr(a); + self.expr(b); + self.modulo = true; + let rhs = std::mem::replace(&mut **b, var("_")); + **b = call("iris_mod", vec![(**a).clone(), rhs]); + *op = ast::AssignmentOpData::Equal.into_node(); + } E::Binary(_, a, b) | E::Assignment(a, _, b) | E::Comma(a, b) => { self.expr(a); self.expr(b); @@ -1268,6 +1502,12 @@ impl<'a> Legaliser<'a> { if self.functions.contains(&name) { return; } + // `sqrt(-1.0)`, Bloop's NaN: naga folds it and fails, drivers give NaN. + if name == "sqrt" && !self.constant && args.len() == 1 && negative_constant(&args[0]) { + *f = ast::FunIdentifierData::Expr(Box::new(var("iris_sqrt"))).into_node(); + self.nan = true; + return; + } if let Some((_, new)) = LEGACY_CALLS.iter().find(|(old, _)| *old == name) { *f = ast::FunIdentifierData::Expr(Box::new(var(new))).into_node(); } else if let Some((_, new)) = SHADOW_CALLS.iter().find(|(old, _)| *old == name) { @@ -1307,6 +1547,12 @@ impl<'a> Legaliser<'a> { } let mut out = String::from("#version 450 core\n"); out.push_str(HELPERS); + if self.modulo { + out.push_str(&modulo_helpers()); + } + if self.nan { + out.push_str("float iris_sqrt(float x) { return sqrt(x); }\n"); + } if !self.link.uniforms.is_empty() { out.push_str("layout(std140, set = 0, binding = 0) uniform IrisUniforms {\n"); for (_, decl) in &self.link.uniforms { diff --git a/crates/horizon-shaderpack/tests/pack.rs b/crates/horizon-shaderpack/tests/pack.rs index 7aa45e8fa..c957d7fb4 100644 --- a/crates/horizon-shaderpack/tests/pack.rs +++ b/crates/horizon-shaderpack/tests/pack.rs @@ -121,6 +121,69 @@ fn preprocesses_with_defaults_and_with_values() { ); } +/// JCPP lets a `#define` replace a macro (Bloop's `TILT_ANGLE`); the added +/// `#undef`s don't move the lines errors name. +#[test] +fn a_define_replaces_an_earlier_one() { + use horizon_shaderpack::{include::Inlined, preprocess::preprocess}; + let text = "#define TILT 5.0\nfloat a = TILT;\n # define TILT 1.5 // radians\nfloat b = TILT;\n#define F(x) x\n#define F(x) (x * 2)\nfloat c = F(1);\n#if\n"; + let source = Inlined { + text: text.into(), + origin: (1..=8).map(|l| ("lib/specular.glsl".into(), l)).collect(), + }; + let error = preprocess(&source, &env()).unwrap_err(); + assert!(error.starts_with("lib/specular.glsl:8:"), "{error}"); + let source = Inlined { + text: text.replace("#if\n", ""), + ..source + }; + let out = preprocess(&source, &env()).unwrap().text; + let lines: Vec<&str> = out.lines().collect(); + assert_eq!(lines[1], "float a = 5.0 ;"); + assert_eq!(lines[3], "float b = 1.5 ;"); + assert_eq!(lines[6], "float c = ( 1 * 2 ) ;"); +} + +/// Conditions as JCPP reads them: an empty macro counts as 0 (Bloop's +/// `#if MC_GL_VENDOR_INTEL || MC_GL_VENDOR_OTHER` with Iris's empty vendor +/// macro, its `#define TAA_USE_YCOCG`), a float as its integer part; a +/// quoted `#error` lexes, and an `#if` may run to the end. +#[test] +fn conditions_read_as_jcpp_does() { + use horizon_shaderpack::{include::Inlined, preprocess::preprocess}; + let mut env = env(); + env.define("MC_GL_VENDOR_OTHER", ""); + let text = "#define TAA_USE_YCOCG +#define LEVEL 0.5 +#if MC_GL_VENDOR_INTEL || MC_GL_VENDOR_OTHER +float gather = 0.0; +#else +float gather = 1.0; +#endif +#if TAA_USE_YCOCG +float ycocg = 1.0; +#endif +#if defined TAA_USE_YCOCG && defined(MC_GL_VENDOR_OTHER) && LEVEL == 0 +float both = 1.0; +#endif +#ifndef TAA_USE_YCOCG +#error \"This program should be disabled\" +#endif +#ifdef TAA_USE_YCOCG +float open = 1.0; +"; + let source = Inlined { + text: text.into(), + origin: Vec::new(), + }; + let out = preprocess(&source, &env).unwrap().text; + let words: Vec<&str> = out.split_whitespace().collect(); + assert_eq!( + words.join(" "), + "float gather = 1.0 ; float both = 1.0 ; float open = 1.0 ;" + ); +} + #[test] fn reports_missing_includes_and_cycles() { let pack = JavaPack::open(&mini()).unwrap(); diff --git a/crates/horizon-shaderpack/tests/translate.rs b/crates/horizon-shaderpack/tests/translate.rs index da8dc7e32..bb4a544c2 100644 --- a/crates/horizon-shaderpack/tests/translate.rs +++ b/crates/horizon-shaderpack/tests/translate.rs @@ -290,6 +290,47 @@ fn reports_errors_with_their_place() { assert_eq!(err, "uniform a is declared as both `float a` and `int a`"); } +/// What naga refuses and drivers accept: `vec % scalar` (Noble), `sqrt` of +/// a negative constant (Bloop's NaN), constants naga can't fold (Bliss's +/// `?:`, `mix`), and 16-bit types (Noble on NVIDIA). +#[test] +fn legalises_what_naga_refuses() { + let mut link = Link::default(); + let fs = run( + Stage::Fragment, + "uniform vec2 uv; +const int RES = 256 % 100; +const vec3 TARGET = length(vec3(1.0)) < 0.001 ? vec3(0.0) : normalize(vec3(1.0)); +const vec3 TINT = mix(TARGET, vec3(1.0), 0.5); +const vec3 HALF = TINT * 0.5; +u16vec2 pack(uint v) { return u16vec2(v & 65535u, v >> 16u); } +float nan() { return sqrt(-1.0); } +void main() { + ivec2 texel = ivec2(uv) % RES; + texel %= 7; + uint packed = packFloat2x16(f16vec2(HALF.xy)); + gl_FragColor = vec4(vec2(texel), float(pack(packed).x), nan()); +} +", + &mut link, + ) + .unwrap(); + for wanted in [ + "const int RES = 256 % 100;", + "vec3 TARGET;", + "vec3 TINT;", + "vec3 HALF;", + "ivec2 texel = iris_mod(ivec2(uv), RES);", + "texel = iris_mod(texel, 7);", + "uvec2 pack(uint v)", + "return iris_sqrt(-1.);", + "uint packed = packHalf2x16(vec2(HALF.xy));", + ] { + assert!(fs.glsl.contains(wanted), "{wanted}\n{}", fs.glsl); + } + reparse(&fs.wgsl).unwrap(); +} + /// The written WGSL, read back and validated: what wgpu is given. fn reparse(wgsl: &str) -> Result<(), String> { let module = naga::front::wgsl::parse_str(wgsl).map_err(|e| e.emit_to_string(wgsl))?; From e570561db9115d055d70317b0386801280737aa1 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 03:21:44 +0200 Subject: [PATCH 013/124] Shader packs: write narrow fragment outputs through a vec4 GL fills the channels a fragment shader leaves out; wgpu refuses a pipeline whose output has fewer channels than its target. Steadfast's final pass writes `out vec3` and failed to build in the game. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-shaderpack/src/translate.rs | 40 +++++++++++++- crates/horizon-shaderpack/tests/translate.rs | 56 ++++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/crates/horizon-shaderpack/src/translate.rs b/crates/horizon-shaderpack/src/translate.rs index 708ab5771..8a32795af 100644 --- a/crates/horizon-shaderpack/src/translate.rs +++ b/crates/horizon-shaderpack/src/translate.rs @@ -238,6 +238,31 @@ vec3 iris_modf(vec3 x, out vec3 i) { i = trunc(x); return x - i; } vec4 iris_modf(vec4 x, out vec4 i) { i = trunc(x); return x - i; } "; +/// A fragment output narrower than four channels (`out vec3 color`, +/// Steadfast's `final`): the four-channel type it is written through and the +/// epilogue line that fills it, with alpha 1 and the rest 0. `None` when the +/// output already has four channels. +fn widen_output(ty: &str, name: &str) -> Option<(String, String)> { + let (wide, one, zero) = match ty { + "float" | "vec2" | "vec3" => ("vec4", "1.0", "0.0"), + "int" | "ivec2" | "ivec3" => ("ivec4", "1", "0"), + "uint" | "uvec2" | "uvec3" => ("uvec4", "1u", "0u"), + _ => return None, + }; + let channels = match ty.chars().last() { + Some('2') => 2, + Some('3') => 3, + _ => 1, + }; + let mut parts = vec![name.to_owned()]; + parts.extend((channels..3).map(|_| zero.to_owned())); + parts.push(one.to_owned()); + Some(( + wide.to_owned(), + format!("iris_out_{name} = {wide}({});", parts.join(", ")), + )) +} + /// `%` as overloads: naga's GLSL front end splats a scalar operand for /// `+ - / & ^ |` but not for `%`, and rejects `ivec2(uv) % 256` (Noble). fn modulo_helpers() -> String { @@ -924,8 +949,19 @@ impl<'a> Legaliser<'a> { let at = explicit .map(|l: u32| l + i as u32) .unwrap_or_else(|| self.next_output()); - self.header - .push(format!("layout(location = {at}) out {full} {name}{array};")); + if let (Some(widened), "") = (widen_output(&full, name), array.as_str()) { + // GL fills the missing channels of a narrow output; + // wgpu wants every channel of the target written. + let (wide, fill) = widened; + self.header.push(format!("{full} {name};")); + self.header.push(format!( + "layout(location = {at}) out {wide} iris_out_{name};" + )); + self.epilogue.push(fill); + } else { + self.header + .push(format!("layout(location = {at}) out {full} {name}{array};")); + } self.interface.outputs.insert(at, name.clone()); } else { self.varying(dir, &interp, &full, name, array); diff --git a/crates/horizon-shaderpack/tests/translate.rs b/crates/horizon-shaderpack/tests/translate.rs index bb4a544c2..3c71d8546 100644 --- a/crates/horizon-shaderpack/tests/translate.rs +++ b/crates/horizon-shaderpack/tests/translate.rs @@ -331,6 +331,62 @@ void main() { reparse(&fs.wgsl).unwrap(); } +/// A fragment output narrower than its four-channel target (Steadfast's +/// `final` writes `out vec3 color`) is written through a `vec4` with alpha 1: +/// GL fills the missing channels, wgpu refuses the pipeline. +#[test] +fn narrow_fragment_outputs_fill_four_channels() { + let mut link = Link::default(); + let fs = run( + Stage::Fragment, + "uniform vec3 tint; +layout(location = 0) out vec3 color; +layout(location = 1) out float depth; +out vec4 extra; +void main() { + color = tint; + depth = 0.5; + extra = vec4(tint, 0.25); +} +", + &mut link, + ) + .unwrap(); + for wanted in [ + "layout(location = 0) out vec4 iris_out_color;", + "iris_out_color = vec4(color, 1.0);", + "layout(location = 1) out vec4 iris_out_depth;", + "iris_out_depth = vec4(depth, 0.0, 0.0, 1.0);", + "layout(location = 2) out vec4 extra;", + ] { + assert!(fs.glsl.contains(wanted), "{wanted}\n{}", fs.glsl); + } + assert_eq!( + fs.interface.outputs.values().collect::>(), + ["color", "depth", "extra"] + ); + let module = naga::front::wgsl::parse_str(&fs.wgsl).unwrap(); + let entry = &module.entry_points[0]; + let result = entry.function.result.as_ref().unwrap(); + let naga::TypeInner::Struct { members, .. } = &module.types[result.ty].inner else { + panic!("{}", fs.wgsl); + }; + for m in members { + assert!( + matches!( + module.types[m.ty].inner, + naga::TypeInner::Vector { + size: naga::VectorSize::Quad, + .. + } + ), + "{}", + fs.wgsl + ); + } + reparse(&fs.wgsl).unwrap(); +} + /// The written WGSL, read back and validated: what wgpu is given. fn reparse(wgsl: &str) -> Result<(), String> { let module = naga::front::wgsl::parse_str(wgsl).map_err(|e| e.emit_to_string(wgsl))?; From 6ea7a51ff35983c2082cfa4e002136a461261845 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 03:05:29 +0200 Subject: [PATCH 014/124] Shader Packs page: the folder's path, Open Folder, a switch per pack The Shader Pack setting cycled through what shaderpacks/ held, so with the folder empty (or the packs in the launcher's version folder, as brabus_ had them) it silently stayed Off. The row now opens a Shader Packs page: a sidebar section in the Horizon menus, Video Settings > Shader Packs... in the Classic ones. It shows the game home's shaderpacks/ path and an Open Folder button (made if missing), says when there is no pack, and lists one switch per pack. A pack that fails to load says why on its row. The list is read again every second while the page shows. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/options.rs | 5 + .../horizon-client/src/game/shader_packs.rs | 8 +- .../horizon-client/src/game/startup_menu.rs | 5 + crates/horizon-client/src/graphics_ui.rs | 47 ++-- crates/horizon-client/src/options_horizon.rs | 167 ++++++++++++- crates/horizon-client/src/options_ui.rs | 219 +++++++++++++++++- crates/horizon-client/src/shader_pack.rs | 32 ++- docs/shader-packs.md | 20 +- 8 files changed, 462 insertions(+), 41 deletions(-) diff --git a/crates/horizon-client/src/game/options.rs b/crates/horizon-client/src/game/options.rs index 12efde5be..61036d18c 100644 --- a/crates/horizon-client/src/game/options.rs +++ b/crates/horizon-client/src/game/options.rs @@ -230,6 +230,11 @@ impl Game<'_> { let _ = std::fs::create_dir_all(&dir); super::packs::open_folder(&dir); } + Event::OpenShaderPackFolder => { + let dir = horizon_client::shader_pack::folder(); + let _ = std::fs::create_dir_all(&dir); + super::packs::open_folder(&dir); + } } } Ok(()) diff --git a/crates/horizon-client/src/game/shader_packs.rs b/crates/horizon-client/src/game/shader_packs.rs index 6b9ee33dc..052f64d26 100644 --- a/crates/horizon-client/src/game/shader_packs.rs +++ b/crates/horizon-client/src/game/shader_packs.rs @@ -37,11 +37,8 @@ impl ShaderPacks { } } -/// `shaderpacks/` beside `options.txt`. pub(super) fn dir() -> PathBuf { - std::env::current_dir() - .unwrap_or_default() - .join(shader_pack::DIR) + shader_pack::folder() } impl Game<'_> { @@ -77,6 +74,7 @@ impl Game<'_> { return self.shader_pack_error(&id, &e.to_string()); } self.shader_packs.pack = pack; + self.ui.options.shader_pack_error = None; } /// Give the loaded pack to the renderer (also after it is rebuilt). pub(super) fn apply_shader_pack(&mut self) -> horizon_client::Result<()> { @@ -105,6 +103,8 @@ impl Game<'_> { } fn shader_pack_error(&mut self, id: &str, error: &str) { log::warn!("Horizon: shader pack {id}: {error}"); + // The Shader Packs page says why on the pack's row. + self.ui.options.shader_pack_error = Some((id.to_owned(), error.to_owned())); self.system_message(Text::literal(format!("Shader pack {id}: {error}"))); } } diff --git a/crates/horizon-client/src/game/startup_menu.rs b/crates/horizon-client/src/game/startup_menu.rs index c5b19763c..ffbbe7193 100644 --- a/crates/horizon-client/src/game/startup_menu.rs +++ b/crates/horizon-client/src/game/startup_menu.rs @@ -373,6 +373,11 @@ impl Loading { let _ = std::fs::create_dir_all(&dir); packs::open_folder(&dir); } + Event::OpenShaderPackFolder => { + let dir = horizon_client::shader_pack::folder(); + let _ = std::fs::create_dir_all(&dir); + packs::open_folder(&dir); + } Event::ButtonClick => {} } } diff --git a/crates/horizon-client/src/graphics_ui.rs b/crates/horizon-client/src/graphics_ui.rs index eea2d03cb..9722ddc21 100644 --- a/crates/horizon-client/src/graphics_ui.rs +++ b/crates/horizon-client/src/graphics_ui.rs @@ -237,7 +237,7 @@ pub fn rows(level: Level) -> Vec { h( H::ShaderPack, High, - "Redraws the world with a pack from shaderpacks/. Off is vanilla.", + "Redraws the world with a pack from the shaderpacks folder. Off is vanilla.", ), Row::Header("Preferences"), v(Opt::ViewBobbing, None, "Sways the view while walking."), @@ -422,17 +422,6 @@ impl H { H::Antialiasing => g.fxaa = !g.fxaa, H::BatteryMode => g.battery = !g.battery, H::GraphicsApi => g.api = step(&GraphicsApi::ALL, g.api, back), - H::ShaderPack => { - // Off, then every pack in shaderpacks/ by folder name. - let dir = std::env::current_dir() - .unwrap_or_default() - .join(crate::shader_pack::DIR); - let mut all = vec![String::new()]; - all.extend(crate::shader_pack::list(&dir).into_iter().map(|(id, _)| id)); - let i = all.iter().position(|x| *x == g.shader_pack).unwrap_or(0); - let n = all.len(); - g.shader_pack = all[if back { (i + n - 1) % n } else { (i + 1) % n }].clone(); - } H::Pacing => { g.pacing = match g.pacing { Pacing::Efficient => Pacing::Precise, @@ -559,6 +548,8 @@ pub enum Action { Changed, OpenAdvanced, OpenSimple, + /// The Shader Pack row: the Shader Packs page (where packs are picked). + OpenShaderPacks, Done, } @@ -784,6 +775,9 @@ impl GraphicsUi { } return match wd.target { Target::Button(action) => vec![Action::ButtonClick, action], + Target::Item(Item::Horizon(H::ShaderPack)) => { + vec![Action::ButtonClick, Action::OpenShaderPacks] + } Target::Item(item) => { if item.slider(s).is_some() { self.dragging = Some(item); @@ -1003,11 +997,30 @@ mod tests { ); s.horizon.shader_pack = "horizon-sample".into(); assert_eq!(row.label(&s).plain(), "Shader Pack: horizon-sample"); - // Picking a pack keeps the preset. - Item::Horizon(H::Preset).cycle(&mut s, false, [854, 480]); - let preset = s.horizon.preset; - row.cycle(&mut s, false, [854, 480]); - assert_eq!(s.horizon.preset, preset); + // The row opens the Shader Packs page, where packs are picked. + let mut ui = GraphicsUi::default(); + let find = |ui: &mut GraphicsUi| { + let (widgets, ..) = ui.layout(Level::Advanced, 854.0, 480.0); + widgets + .into_iter() + .find(|w| matches!(w.target, Target::Item(i) if i == row)) + .unwrap() + }; + ui.scroll = find(&mut ui).y - GraphicsUi::bounds(480.0).0 - 4.0; + let w = find(&mut ui); + assert_eq!( + ui.click( + Level::Advanced, + &mut s, + (w.x + 1.0, w.y + 1.0), + (854.0, 480.0), + [854, 480], + false, + false + ), + [Action::ButtonClick, Action::OpenShaderPacks] + ); + assert_eq!(s.horizon.shader_pack, "horizon-sample"); } #[test] diff --git a/crates/horizon-client/src/options_horizon.rs b/crates/horizon-client/src/options_horizon.rs index 75458ae40..50a42f337 100644 --- a/crates/horizon-client/src/options_horizon.rs +++ b/crates/horizon-client/src/options_horizon.rs @@ -38,7 +38,14 @@ pub enum HRow { Mod(usize), /// Opens the directory where Horizon mods are installed. OpenModFolder, + /// `shaderpacks/`'s path and a button opening it. + ShaderPackFolder, + /// Shader pack `i` of `OptionsUi::shader_packs` and its switch. + ShaderPack(usize), } + +/// The sidebar section listing the shader packs. +pub const SHADER_PACKS: &str = "Shader Packs"; /// A sidebar section: name, description, rows. pub struct Section { pub name: &'static str, @@ -172,6 +179,12 @@ pub const SECTIONS: &[Section] = &[ .to_vec() }, }, + Section { + name: SHADER_PACKS, + blurb: "Packs that redraw the world. Off is the vanilla look.", + // The packs follow, from the folder (`horizon_rows`). + rows: |_| vec![HRow::ShaderPackFolder], + }, Section { name: "Interface", blurb: "Menus and resource packs.", @@ -309,7 +322,15 @@ fn horizon_batch(framebuffer: [i32; 2]) -> HudBatch { } impl OptionsUi { fn horizon_rows(&self, s: &Settings, section: usize) -> Vec { - let rows = (SECTIONS[section].rows)(s); + let mut rows = (SECTIONS[section].rows)(s); + if SECTIONS[section].name == SHADER_PACKS { + if self.shader_packs.packs.is_empty() { + rows.push(HRow::Header( + "No shader packs: put a pack's folder or .zip in this folder", + )); + } + rows.extend((0..self.shader_packs.packs.len()).map(HRow::ShaderPack)); + } let query = self.settings_search.value.to_lowercase(); if query.is_empty() || !searchable_section(section) { return rows; @@ -511,6 +532,38 @@ impl OptionsUi { ui.described_row(x, y, &installed.title, "", 0.0, cost, text, cw - 60.0); ui.switch(ROW + i as u64, control_x, y + ROW_H / 2.0, s.mods.is_on(m)); } + HRow::ShaderPackFolder => { + let label = "Open Folder"; + let bw = ui.button_width(label, false).max(96.0); + let dir = self.shader_packs.dir.display().to_string(); + ui.described_row(x, y, "Folder", "", 0.0, (0, ""), &dir, cw - bw - 24.0); + ui.button( + ROW + i as u64, + [control_x - bw, y + 12.0, bw, CONTROL], + label, + Button::Secondary, + true, + ); + } + HRow::ShaderPack(p) => { + let (id, info) = &self.shader_packs.packs[p]; + let error = match info { + Err(e) => Some(e.as_str()), + Ok(_) => self + .shader_pack_error + .as_ref() + .filter(|(failed, _)| failed == id) + .map(|(_, e)| e.as_str()), + }; + let (cost, text) = match (error, info) { + (Some(e), _) => ((3, "Failed"), e), + (None, Ok(info)) => ((0, ""), info.description.as_str()), + (None, Err(_)) => unreachable!(), + }; + ui.described_row(x, y, id, "", 0.0, cost, text, cw - 60.0); + let on = s.horizon.shader_pack == *id; + ui.switch(ROW + i as u64, control_x, y + ROW_H / 2.0, on); + } HRow::OpenModFolder => { ui.row(x, y, "Mod folder", "", 0.0); let label = "Open Folder"; @@ -756,13 +809,7 @@ impl OptionsUi { return events; } match target { - Some(Target::Nav(i)) => { - if searchable_section(self.section) || searchable_section(i) { - self.settings_search.set_value(""); - } - self.section = i; - self.hscroll = 0.0; - } + Some(Target::Nav(i)) => self.select_section(i), Some(Target::Search) => { self.settings_search.focused = true; self.settings_search.move_cursor_to_end(false); @@ -798,6 +845,10 @@ impl OptionsUi { } events.push(Event::Changed); } + Some(HRow::Graphics(Item::Horizon(graphics_ui::H::ShaderPack), ..)) => { + events.push(Event::ButtonClick); + self.open_section(SHADER_PACKS); + } Some(HRow::Graphics(item, ..)) => { item.cycle(s, right, framebuffer); events.push(Event::Changed); @@ -818,6 +869,14 @@ impl OptionsUi { events.push(Event::ButtonClick); events.push(Event::OpenModFolder); } + Some(HRow::ShaderPackFolder) if !right => { + events.push(Event::ButtonClick); + events.push(Event::OpenShaderPackFolder); + } + Some(HRow::ShaderPack(p)) => { + self.toggle_shader_pack(s, *p); + events.push(Event::Changed); + } _ => {} } } @@ -825,6 +884,19 @@ impl OptionsUi { } events } + fn select_section(&mut self, i: usize) { + if searchable_section(self.section) || searchable_section(i) { + self.settings_search.set_value(""); + } + self.section = i; + self.hscroll = 0.0; + } + /// Show the sidebar section called `name`. + fn open_section(&mut self, name: &str) { + if let Some(i) = SECTIONS.iter().position(|s| s.name == name) { + self.select_section(i); + } + } pub(crate) fn scroll_horizon(&mut self, amount: f32) { self.hscroll -= amount * 40.0; } @@ -1057,6 +1129,85 @@ mod tests { assert_eq!(ui.hscroll, 0.0); } + /// The Shader Packs section: the folder's path and Open Folder, a switch + /// per pack; the Video section's Shader Pack row leads here (#941, #925). + #[test] + fn shader_packs_section_opens_the_folder_and_picks_a_pack() { + let atlas = atlas(); + let framebuffer = [2560, 1440]; + let dir = + std::env::temp_dir().join(format!("horizon-shader-section-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let mut options = section_ui(SHADER_PACKS); + let mut settings = Settings::default(); + options.shader_packs.poll(dir.clone()); + assert!(matches!( + options.horizon_rows(&settings, options.section)[..], + [HRow::ShaderPackFolder, HRow::Header(_)] + )); + std::fs::write(dir.join("Pack.zip"), b"").unwrap(); + options.shader_packs = Default::default(); + options.shader_packs.poll(dir.clone()); + // Click the control with hit id `id`, scrolling until it shows. + let click = |options: &mut OptionsUi, settings: &mut Settings, id: u64| { + for _ in 0..40 { + let mut batch = horizon_batch(framebuffer); + let mut ui = Ui::dry(&atlas, &mut batch, (0.0, 0.0)); + options.horizon_layout(settings, &mut ui, 1.0); + if let Some(hit) = ui.hits.iter().find(|hit| hit.id == id) { + let r = hit.rect; + let scale = ui.density / ui.batch.scale; + let at = ((r[0] + r[2]) * 0.5 * scale, (r[1] + r[3]) * 0.5 * scale); + drop(ui); + return options.click_horizon( + settings, + &atlas, + at.0, + at.1, + framebuffer, + false, + false, + ); + } + drop(ui); + options.hscroll += 200.0; + } + panic!("no control {id}"); + }; + assert_eq!( + click(&mut options, &mut settings, ROW), + [Event::ButtonClick, Event::OpenShaderPackFolder] + ); + assert_eq!( + click(&mut options, &mut settings, ROW + 1), + [Event::Changed] + ); + assert_eq!(settings.horizon.shader_pack, "Pack.zip"); + click(&mut options, &mut settings, ROW + 1); + assert_eq!(settings.horizon.shader_pack, ""); + + let mut options = section_ui("Video"); + settings.horizon.advanced = true; + let video = SECTIONS.iter().position(|s| s.name == "Video").unwrap(); + let row = options + .horizon_rows(&settings, video) + .iter() + .position(|r| { + matches!( + r, + HRow::Graphics(Item::Horizon(graphics_ui::H::ShaderPack), ..) + ) + }) + .unwrap(); + assert_eq!( + click(&mut options, &mut settings, ROW + row as u64), + [Event::ButtonClick] + ); + assert_eq!(SECTIONS[options.section].name, SHADER_PACKS); + let _ = std::fs::remove_dir_all(&dir); + } + #[test] fn mods_section_opens_folder_from_its_button() { let atlas = atlas(); diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index 0ea49cb72..7d86263df 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -862,6 +862,8 @@ pub enum Page { Font, Packs, Mods, + /// The shader packs in `shaderpacks/`: pick one, open the folder. + ShaderPacks, /// Horizon's graphics settings (`graphics_ui`). Graphics, GraphicsAdvanced, @@ -888,6 +890,9 @@ enum Act { HorizonMenus, ToggleMod(usize), OpenModFolder, + /// Pick shader pack `i` of `OptionsUi::shader_packs` (again: off). + ToggleShaderPack(usize), + OpenShaderPackFolder, } enum WKind { Button { @@ -935,6 +940,8 @@ pub enum Event { Packs(Vec), OpenPackFolder, OpenModFolder, + /// Create `shaderpacks/` if needed and open it. + OpenShaderPackFolder, } /// `SliderableValueSet.applyValueImmediately`: these apply 600 ms after /// the last change (`OptionInstanceSliderButton.delayedApplyAt`). @@ -993,6 +1000,10 @@ pub struct OptionsUi { /// Scroll the selected language into the middle on the next layout. center_language: bool, pub graphics: crate::graphics_ui::GraphicsUi, + /// `shaderpacks/` and its packs, read while the Shader Packs page shows. + pub shader_packs: crate::shader_pack::Folder, + /// The pack that failed to load last and why (set by the game). + pub shader_pack_error: Option<(String, String)>, } impl Default for OptionsUi { fn default() -> Self { @@ -1025,6 +1036,8 @@ impl Default for OptionsUi { in_world: false, center_language: false, graphics: Default::default(), + shader_packs: Default::default(), + shader_pack_error: None, } } } @@ -1073,6 +1086,8 @@ enum Row { Big(Opt), Pair(Opt, Option), Header(&'static str), + /// A line of grey text (a path). + Note(String), Buttons(Widget, Option), } fn pairs(opts: &[Opt]) -> Vec { @@ -1131,6 +1146,33 @@ impl OptionsUi { self.graphics.reset(); } } + /// Pick shader pack `i` of the folder, or turn it off when it is the + /// one running. + pub(crate) fn toggle_shader_pack(&self, s: &mut Settings, i: usize) { + let Some((id, Ok(_))) = self.shader_packs.packs.get(i) else { + return; + }; + let g = &mut s.horizon; + g.shader_pack = if g.shader_pack == *id { + String::new() + } else { + id.clone() + }; + } + pub(crate) fn shader_pack_failed(&self, id: &str) -> bool { + self.shader_pack_error + .as_ref() + .is_some_and(|(e, _)| e == id) + } + /// Whether the Shader Packs page (or the Horizon menus' section) shows. + fn shader_packs_shown(&self) -> bool { + self.page() == Page::ShaderPacks + || crate::ui_theme::horizon() + && self.page() == Page::Main + && crate::options_horizon::SECTIONS + .get(self.section) + .is_some_and(|s| s.name == crate::options_horizon::SHADER_PACKS) + } /// The graphics screen level of `page`, if it is one. fn graphics_level(page: Page) -> Option { match page { @@ -1153,6 +1195,7 @@ impl OptionsUi { Page::Font => "options.font.title", Page::Packs => "resourcePack.title", Page::Mods => "Mods", + Page::ShaderPacks => "Shader Packs", Page::Graphics | Page::GraphicsAdvanced => "options.videoTitle", Page::Controller => "Controller Settings", Page::PadBinds => "Controller Binds", @@ -1262,6 +1305,16 @@ impl OptionsUi { Act::Push(Page::GraphicsAdvanced), )), )]; + rows.push(Row::Buttons( + button( + 0.0, + 0.0, + 310.0, + Text::literal("Shader Packs..."), + Act::Push(Page::ShaderPacks), + ), + None, + )); // The way back from Classic menus. Horizon-only like the row // above, here rather than on the main Options screen so that // one keeps vanilla's exact layout; vanilla's interface @@ -1450,6 +1503,32 @@ impl OptionsUi { }) .collect() } + Page::ShaderPacks => { + let folder = &self.shader_packs; + let mut rows = vec![Row::Note(folder.dir.display().to_string())]; + if folder.packs.is_empty() { + rows.push(Row::Header( + "No shader packs: put a pack's folder or .zip in this folder", + )); + } + for (i, (id, info)) in folder.packs.iter().enumerate() { + let on = s.horizon.shader_pack == *id; + let mut label = Text::literal(format!("{id}: ")).append(on_off(on)); + if info.is_err() || self.shader_pack_failed(id) { + label = label.append(" (Failed)"); + } + let widget = button(0.0, 0.0, 310.0, label, Act::ToggleShaderPack(i)); + rows.push(Row::Buttons( + if info.is_ok() { + widget + } else { + inactive(widget) + }, + None, + )); + } + rows + } _ => { let _ = s; Vec::new() @@ -1849,6 +1928,21 @@ impl OptionsUi { }); y += padding + 18.0; } + Row::Note(text) => { + out.push(Widget { + x: 0.0, + y: y + 4.0, + w, + h: 9.0, + kind: WKind::Text { + text: Text::literal(text), + color: 0xFFA0A0A0, + centered: true, + }, + list: false, + }); + y += 16.0; + } Row::Big(opt) => { out.push(option_widget(s, opt, cx - 155.0, y + 2.0, 310.0)); y += 25.0; @@ -1875,13 +1969,20 @@ impl OptionsUi { } content = y + self.scroll - top + 2.0; out[list_start..].iter_mut().for_each(|w| w.list = true); - if page == Page::Mods { + let folder = match page { + Page::Mods => Some(("Open Mod Folder", Act::OpenModFolder)), + Page::ShaderPacks => { + Some(("Open Shader Pack Folder", Act::OpenShaderPackFolder)) + } + _ => None, + }; + if let Some((label, act)) = folder { out.push(button( cx - 154.0, h - 27.0, 150.0, - Text::literal("Open Mod Folder"), - Act::OpenModFolder, + Text::literal(label), + act, )); out.push(button(cx + 4.0, h - 27.0, 150.0, tr("gui.done"), Act::Done)); } else { @@ -1911,6 +2012,9 @@ impl OptionsUi { atlas: &HudAtlas, cursor: (f32, f32), ) { + if self.shader_packs_shown() { + self.shader_packs.poll(crate::shader_pack::folder()); + } let (w, h) = (batch.width, batch.height); if let Some(error) = &self.resource_error { ui::menu_background(batch, atlas, 0.0, 0.0, w, h, self.in_world); @@ -2210,6 +2314,7 @@ impl OptionsUi { Action::ButtonClick => events.push(Event::ButtonClick), Action::Changed => events.push(Event::Changed), Action::OpenAdvanced => self.push(Page::GraphicsAdvanced), + Action::OpenShaderPacks => self.push(Page::ShaderPacks), Action::OpenSimple => { if self.stack.len() >= 2 && self.stack[self.stack.len() - 2] == Page::Graphics @@ -2377,6 +2482,11 @@ impl OptionsUi { events.push(Event::Changed); } Act::OpenModFolder => events.push(Event::OpenModFolder), + Act::ToggleShaderPack(i) => { + self.toggle_shader_pack(s, i); + events.push(Event::Changed); + } + Act::OpenShaderPackFolder => events.push(Event::OpenShaderPackFolder), Act::None => {} } } @@ -2831,6 +2941,109 @@ mod tests { assert!(ui.resource_error.is_none()); } + /// The Shader Packs page (Video Settings > Shader Packs..., or the + /// Shader Pack row of Advanced graphics): the folder's path, Open Shader + /// Pack Folder, and a switch per pack (#941, #925). + #[test] + fn classic_shader_packs_page_shows_the_folder_and_picks_a_pack() { + let atlas = atlas(); + let mut s = Settings::default(); + let mut ui = OptionsUi::default(); + let size = (480.0, 240.0); + let dir = std::env::temp_dir().join(format!("horizon-shaderpacks-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let find = |ui: &mut OptionsUi, s: &Settings, act: &dyn Fn(&Act) -> bool| { + ui.layout(s, size.0, size.1) + .0 + .into_iter() + .find(|w| matches!(&w.kind, WKind::Button { act: a, active: true, .. } if act(a))) + }; + ui.show(Page::Video); + let open = find(&mut ui, &s, &|a| *a == Act::Push(Page::ShaderPacks)).unwrap(); + ui.click( + &mut s, + &atlas, + open.x + 1.0, + open.y + 1.0, + size, + [480, 240], + false, + false, + ); + assert_eq!(ui.page(), Page::ShaderPacks); + + ui.shader_packs.poll(dir.clone()); + let empty = ui.layout(&s, size.0, size.1).0; + let path = dir.display().to_string(); + for line in [path.as_str(), "No shader packs"] { + assert!( + empty.iter().any( + |w| matches!(&w.kind, WKind::Text { text, .. } if text.plain().contains(line)) + ), + "{line}" + ); + } + let folder = find(&mut ui, &s, &|a| *a == Act::OpenShaderPackFolder).unwrap(); + assert_eq!( + ui.click( + &mut s, + &atlas, + folder.x + 1.0, + folder.y + 1.0, + size, + [480, 240], + false, + false + ), + vec![Event::ButtonClick, Event::OpenShaderPackFolder] + ); + + std::fs::write(dir.join("Pack.zip"), b"").unwrap(); + ui.shader_packs = Default::default(); + ui.shader_packs.poll(dir.clone()); + let pack = find(&mut ui, &s, &|a| *a == Act::ToggleShaderPack(0)).unwrap(); + let events = ui.click( + &mut s, + &atlas, + pack.x + 1.0, + pack.y + 1.0, + size, + [480, 240], + false, + false, + ); + assert!(events.contains(&Event::Changed)); + assert_eq!(s.horizon.shader_pack, "Pack.zip"); + ui.shader_pack_error = Some(("Pack.zip".into(), "broken".into())); + let label = |ui: &mut OptionsUi, s: &Settings| match find(ui, s, &|a| { + *a == Act::ToggleShaderPack(0) + }) + .unwrap() + .kind + { + WKind::Button { label, .. } => label.plain(), + _ => unreachable!(), + }; + assert!( + label(&mut ui, &s).ends_with("(Failed)"), + "{}", + label(&mut ui, &s) + ); + ui.click( + &mut s, + &atlas, + pack.x + 1.0, + pack.y + 1.0, + size, + [480, 240], + false, + false, + ); + assert_eq!(s.horizon.shader_pack, "", "a second click turns it off"); + let _ = std::fs::remove_dir_all(&dir); + } + #[test] fn classic_mods_page_toggles_mods_and_keeps_dev_and_invalid_entries_inactive() { let atlas = atlas(); diff --git a/crates/horizon-client/src/shader_pack.rs b/crates/horizon-client/src/shader_pack.rs index ec86edc77..f3cf46028 100644 --- a/crates/horizon-client/src/shader_pack.rs +++ b/crates/horizon-client/src/shader_pack.rs @@ -171,11 +171,41 @@ pub fn list(dir: &Path) -> Vec<(String, Result)> { packs } +/// `shaderpacks/` beside `options.txt` (the game's working folder: the game +/// home, not the launcher's version folder). +pub fn folder() -> PathBuf { + std::env::current_dir().unwrap_or_default().join(DIR) +} + +/// `shaderpacks/` and its packs as the Shader Packs page lists them, read +/// again at most once a second while the page is open, so a pack dropped +/// into the folder shows up. +#[derive(Debug, Default)] +pub struct Folder { + pub dir: PathBuf, + pub packs: Vec<(String, Result)>, + read: Option, +} + +impl Folder { + pub fn poll(&mut self, dir: PathBuf) { + let fresh = self + .read + .is_some_and(|t| t.elapsed() < std::time::Duration::from_secs(1)); + if fresh && self.dir == dir { + return; + } + self.packs = list(&dir); + self.dir = dir; + self.read = Some(std::time::Instant::now()); + } +} + /// A Java pack has no manifest: its name is its file name. fn java_info(name: String) -> Info { Info { name, - description: String::new(), + description: "Iris/OptiFine pack: its full-screen passes run.".into(), authors: Vec::new(), version: String::new(), license: String::new(), diff --git a/docs/shader-packs.md b/docs/shader-packs.md index 3cd068f2d..faf3c7d3f 100644 --- a/docs/shader-packs.md +++ b/docs/shader-packs.md @@ -13,7 +13,7 @@ Both are off by default. With no pack selected the renderer draws exactly what i | Part | State | | --- | --- | | Renderer layout (below) | today's code | -| Horizon shader packs: `composite` stage, uniforms, hot reload, Shader Packs screen | first slice planned | +| Horizon shader packs: `composite` stage, uniforms, hot reload, Shader Packs page | done (the page: list, pick, open the folder) | | Horizon shader packs: `deferred`, `gbuffer`, `shadow` stages, custom textures | planned | | Java shader packs: preprocessing + GLSL parse coverage | spike done: 450 of 470 programs reach valid WGSL (below) | | Java shader packs: preprocessor (`crates/horizon-shaderpack`: includes, options, Iris's macros) | done: every program of the three test packs preprocesses | @@ -56,15 +56,19 @@ The lightmap, fog and sky colours are computed on the CPU (`environment.rs`, `fo ### Where packs live and how one is picked - Packs go in `shaderpacks/` beside `options.txt` in the game folder (the same folder Iris uses, so one folder - holds both kinds). A pack is a folder (zips planned). A pack with a `pack.toml` at its root is a Horizon pack; a pack - with a `shaders/` folder is a Java pack. -- **Options → Video Settings → Shader Packs...** (Classic screens, laid out like Iris's: the packs on the left with - "(off)" first, Apply and Done, *Open Shader Pack Folder*, *Shader Pack Settings...*) and a **Shader Packs** row in - the Horizon menus' Video section open the same list. + holds both kinds). That is the game home the launcher starts the game in (`%APPDATA%\horizon` on Windows, + `~/Library/Application Support/horizon` on macOS, `~/.local/share/horizon` on Linux), not the launcher's version + folder. A pack with a `pack.toml` at its root is a Horizon pack; a folder with `shaders/`, or a `.zip`, is a Java + pack. +- The **Shader Packs** page lists them: a sidebar section in the Horizon menus, **Video Settings → Shader Packs...** + in the Classic ones, and the Shader Pack row of the graphics settings leads to it. It shows the folder's full path + and an *Open Folder* button (the folder is made if it is missing), then one switch per pack: turning one on picks + it, turning it off is the vanilla picture. With no pack, it says so under the path. The list is read again every + second while the page is open, so a pack dropped into the folder shows up. - The choice is kept in `horizon-graphics.txt` (`shaderPack:`, empty = off). A pack's option values are kept in `shaderpacks/.txt`, as Iris does. -- A pack that fails to load or compile leaves the vanilla picture on and shows why, in red, on its row (and in the - log). Nothing a pack does can stop the game from starting. +- A pack that fails to load or compile leaves the vanilla picture (or the last pack that worked) on and says why on + its row ("Failed", with the error), in chat and in the log. Nothing a pack does can stop the game from starting. - Benchmarks (`--benchmark`) and the parity harnesses never load a pack. ### Layout From acbfb8ceca74de42569849f036c3db11d5b8f5b1 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 03:32:17 +0200 Subject: [PATCH 015/124] docs/shader-packs.md: the ten packs tested and what stops the rest Co-Authored-By: Claude Opus 5.5 --- docs/shader-packs.md | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/docs/shader-packs.md b/docs/shader-packs.md index 3cd068f2d..ecb71b5f6 100644 --- a/docs/shader-packs.md +++ b/docs/shader-packs.md @@ -16,7 +16,7 @@ Both are off by default. With no pack selected the renderer draws exactly what i | Horizon shader packs: `composite` stage, uniforms, hot reload, Shader Packs screen | first slice planned | | Horizon shader packs: `deferred`, `gbuffer`, `shadow` stages, custom textures | planned | | Java shader packs: preprocessing + GLSL parse coverage | spike done: 450 of 470 programs reach valid WGSL (below) | -| Java shader packs: preprocessor (`crates/horizon-shaderpack`: includes, options, Iris's macros) | done: every program of the three test packs preprocesses | +| Java shader packs: preprocessor (`crates/horizon-shaderpack`: includes, options, Iris's macros, JCPP's `#if` and `#define` rules) | done: every program of the three test packs preprocesses; ten packs tested (Compatibility, below) | | Java shader packs: GLSL → WGSL (`horizon_shaderpack::translate`: syntax-tree legaliser, naga) | done: the 464 programs the three test packs enable by default translate (below) | | Java shader packs: `shaders.properties` (`horizon_shaderpack::properties`: screens, sliders, profiles, `program.*.enabled`, image formats, blend modes) | done, no screen yet: read like Iris for the three test packs | | Java shader packs: full-screen pass plan (`horizon_shaderpack::pipeline`: `DRAWBUFFERS`/`RENDERTARGETS`, `colortexN` formats and clearing, buffer flips) | done: planned for the three test packs | @@ -378,6 +378,43 @@ shadows, water or sky look yet; Distant Horizons' and Voxy's depth read as far. compute (`.csh`; a pack that needs them doesn't load), mipmapped targets (`colortexNMipmapEnabled` is read but ignored), custom textures, the option screen; shadow matrices are identity. +### Compatibility + +Tested on 2026-10-22 on the Mac mini (M4 Pro, Metal) with each pack's default options, the 26.3 builds from Modrinth or +the authors' pages (dev-only test inputs, kept outside the repository). "Loads" means every pass the pack enables for +the overworld, Nether and End is preprocessed, translated and validated, as the game does when the pack is picked +(`java_pack::tests::real_packs_load` with `HORIZON_TEST_SHADERPACKS`); "drawn" means it was also picked in the game +and screenshotted. Only full-screen passes run (above), so no pack has its own terrain, water, shadows or sky yet. + +| Pack | Version | Result | First thing that stops it | +| --- | --- | --- | --- | +| BSL | v10.1.8 | drawn: 26 passes | | +| Complementary Reimagined | r5.9.3 | drawn: 27 passes | | +| Complementary Unbound | r5.8 | loads: 27 passes | | +| Sildur's Vibrant Extreme | 2.02 | drawn: 18 passes, only its sky (above) | | +| Steadfast | 0.8.0 | drawn: 3 passes | | +| Bliss | 2.1.2 | fails | `world0/deferred.vsh`: an operation naga forbids in a vertex shader | +| Bloop | 1.8.0-Alpha-3 | fails | `world0/prepare1.fsh`: `mat2(vec4)` and other constructors naga's GLSL front end refuses | +| Noble | v1.9.9 | fails (Apple and NVIDIA) | `world0/deferred4.vsh`: a `vec3[9][9]` varying (nested arrays between stages) | +| Photon | v1.4 | fails | `world0/prepare.vsh`: a struct varying (`CloudsParameters`) | +| Rethinking Voxels | r0.1-beta9 | fails | `world0/prepare.fsh`: `1 << 31` overflows naga's constant folding; it also needs images | + +What the translator does for these packs, as Iris (JCPP and its GLSL transformer) does: + +- **`#if` reads floats as JCPP does**: a float in a condition, written there or through a macro, counts as its + integer part, and an empty operand as 0. The same holds in `shaders.properties`, where Steadfast's + `#elif ATMOSPHERE_FOG_STRENGTH_NOON == 0.5` stopped the pack (`UnexpectedToken(Float(0.5))`). +- **A later `#define` replaces an earlier one** instead of failing (Bloop's `DefineRedefined`), and an unterminated + `#if` runs to the end of the file. +- **`shaders.properties`** keeps its empty lines and backslashes through the preprocessor, as Iris's does, so a + continued line doesn't swallow the next one. +- **16-bit and 8-bit types** (`float16_t`, `u16vec2`, `packFloat2x16`...), which Noble uses when the GPU reports + NVIDIA (`NV_gpu_shader5`), become their 32-bit types: WGSL has none. +- **What naga refuses**: `%` with a scalar and a vector operand goes through overloads, a `const` naga can't fold + (`mix`, `?:`, `smoothstep`) becomes a global set at the start of `main`, `sqrt` of a negative constant is + evaluated at run time, and a fragment output narrower than four channels (`out vec3`, Steadfast's `final`) is + written through a `vec4` with alpha 1 as OpenGL fills it. + ### Order to implement 1. **Translator crate** (`horizon-shaderpack`): the preprocessor (includes, option `#define`s with the player's From ac25d415784c69b3ca0fcd910317329a4daa55d3 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 03:27:28 +0200 Subject: [PATCH 016/124] Shader Packs page: show the first line of a pack's error Translator errors go on with a drawn source excerpt that the row can't show. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/shader_packs.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/game/shader_packs.rs b/crates/horizon-client/src/game/shader_packs.rs index 052f64d26..7d628ef44 100644 --- a/crates/horizon-client/src/game/shader_packs.rs +++ b/crates/horizon-client/src/game/shader_packs.rs @@ -103,8 +103,11 @@ impl Game<'_> { } fn shader_pack_error(&mut self, id: &str, error: &str) { log::warn!("Horizon: shader pack {id}: {error}"); - // The Shader Packs page says why on the pack's row. - self.ui.options.shader_pack_error = Some((id.to_owned(), error.to_owned())); + // The Shader Packs page says why on the pack's row: the first line, + // since translator errors go on with a drawn source excerpt. + let first = error.lines().map(str::trim).find(|l| !l.is_empty()); + self.ui.options.shader_pack_error = + Some((id.to_owned(), first.unwrap_or(error).to_owned())); self.system_message(Text::literal(format!("Shader pack {id}: {error}"))); } } From 186777110f6c7996889726d90d02a80f8f0cf9fa Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 00:22:26 +0000 Subject: [PATCH 017/124] Hoppers: skip the item scan when no item reaches the hopper's column HopperBlockEntity.suckInItems scanned every item entity for each hopper that ticked without a container above. During the block-entity pass the game now keeps the set of block columns item boxes reach; a hopper whose column is not in it has nothing to suck, and the scan (same order, same item taken) runs only for the rest. Items do not move within the pass: removals leave a superset, and additions (pushed at the end with a new id) or removing the last item rebuild the set. It is dropped at the end of the pass. 500 hoppers, 200 items: 1.36 -> 0.05 ms per tick 2000 hoppers, 500 items: 13.52 -> 0.26 ms per tick 2000 hoppers, 2000 items: 60.6 -> 3.3 ms per tick Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/game.rs | 3 + .../src/game/block_entity_ticks.rs | 3 + crates/horizon-client/src/game/machines.rs | 164 ++++++++++++++++++ 3 files changed, 170 insertions(+) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 56d37a3af..c87875e4d 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1393,6 +1393,8 @@ struct Game<'a> { arrow_model: Option, tnts: Vec, hoppers: HashMap<(i32, i32, i32), machines::HopperState>, + /// Where item entities are, while the block entities tick (hoppers). + item_columns: Option, creative_screen: Creative, control: Option, frame_stats: horizon_client::control::FrameStats, @@ -11381,6 +11383,7 @@ impl<'a> Game<'a> { arrow_model: prepared.arrow_model, tnts: Vec::new(), hoppers: HashMap::new(), + item_columns: None, creative_screen: prepared.creative_screen, control: None, frame_stats: Default::default(), diff --git a/crates/horizon-client/src/game/block_entity_ticks.rs b/crates/horizon-client/src/game/block_entity_ticks.rs index bb1cadd15..094aed6ef 100644 --- a/crates/horizon-client/src/game/block_entity_ticks.rs +++ b/crates/horizon-client/src/game/block_entity_ticks.rs @@ -24,6 +24,7 @@ impl Game<'_> { let ticking = self.block_ticking_area(); let mut spawners = Vec::new(); let mut trial_spawners = Vec::new(); + self.item_columns = Some(Default::default()); for slot in self.world.block_entity_tickers() { let pos = slot.0; // Ticks before this one can remove it (`TickingBlockEntity.isRemoved`). @@ -56,6 +57,8 @@ impl Game<'_> { _ => {} } } + // Items move between passes: the columns hold for this one only. + self.item_columns = None; self.update_song_volumes(); self.scan_for_hearts(); // The client ticks (particles, sounds, the turning mob) are for a diff --git a/crates/horizon-client/src/game/machines.rs b/crates/horizon-client/src/game/machines.rs index c05c7295f..8ce1940c9 100644 --- a/crates/horizon-client/src/game/machines.rs +++ b/crates/horizon-client/src/game/machines.rs @@ -327,6 +327,46 @@ impl Containers<'_> { } } +/// The block columns any item entity's box reaches, so that a hopper with +/// no item over it skips the scan of every item. Kept for one block-entity +/// pass, in which items do not move: removing one leaves a superset, still +/// right, and adding one (always pushed at the end, under a new id) or +/// removing the last rebuilds the columns. +#[derive(Default)] +pub(super) struct ItemColumns { + /// The item count and last item id when last asked, if built. + seen: Option<(usize, Option)>, + cells: horizon_client::fast_hash::FxHashSet<(i32, i32)>, +} +impl ItemColumns { + /// Whether an item's box may reach into column (x, z). + fn may_reach( + &mut self, + items: &[horizon_client::entities::ItemEntity], + x: i32, + z: i32, + ) -> bool { + let (len, last) = (items.len(), items.last().map(|i| i.id)); + let stale = self + .seen + .is_none_or(|(seen_len, seen_last)| len > seen_len || last != seen_last); + self.seen = Some((len, last)); + if stale { + self.cells.clear(); + for item in items { + let b = item.bounds(); + let (lo, hi) = (b.min_position(), b.max_position()); + for cx in lo.x.floor() as i32..=hi.x.floor() as i32 { + for cz in lo.z.floor() as i32..=hi.z.floor() as i32 { + self.cells.insert((cx, cz)); + } + } + } + } + self.cells.contains(&(x, z)) + } +} + fn bone_meal() -> ItemStack { ItemStack { item: "minecraft:bone_meal".into(), @@ -648,6 +688,11 @@ impl Game<'_> { f64::from(pos.2) + 1.0, ], ); + if let Some(columns) = self.item_columns.as_mut() + && !columns.may_reach(&self.dropped, pos.0, pos.2) + { + return false; + } let mut i = 0; while i < self.dropped.len() { let b = self.dropped[i].bounds(); @@ -880,3 +925,122 @@ impl Game<'_> { true } } + +#[cfg(test)] +mod item_column_tests { + use super::ItemColumns; + use horizon_client::{entities::ItemEntity, items::ItemStack}; + use horizon_core::geometry::Vec3; + + fn item(id: u32, x: f64, y: f64, z: f64) -> ItemEntity { + let at = Vec3::new(x, y, z); + ItemEntity { + health: 5, + fire_ticks: 0, + id, + stack: ItemStack::new("minecraft:stone", 1), + thrower: None, + position: at, + previous: at, + velocity: Vec3::ZERO, + on_ground: true, + age: 0, + pickup_delay: 0, + bob_offset: 0.0, + ticks: 0, + } + } + /// `hopper_suck`'s test: some item box meets the hopper's SUCK_AABB. + fn scan(items: &[ItemEntity], (x, y, z): (i32, i32, i32)) -> bool { + let (min, max) = ( + Vec3::new(f64::from(x), f64::from(y) + 11.0 / 16.0, f64::from(z)), + Vec3::new(f64::from(x) + 1.0, f64::from(y) + 2.0, f64::from(z) + 1.0), + ); + items.iter().any(|i| { + let b = i.bounds(); + let (lo, hi) = (b.min_position(), b.max_position()); + lo.x < max.x + && hi.x > min.x + && lo.y < max.y + && hi.y > min.y + && lo.z < max.z + && hi.z > min.z + }) + } + + /// One tick of empty hoppers looking for items: each scans every item + /// (before) or asks the columns first (after, build included). + #[test] + #[ignore] + fn hopper_suck_cost() { + for (hoppers, count) in [(500i32, 200u32), (2000, 500), (2000, 2000)] { + let mut seed = 99u64; + let mut next = |range: f64| { + seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1); + (seed >> 11) as f64 / (1u64 << 53) as f64 * range + }; + let items: Vec = (0..count) + .map(|id| item(id, next(160.0), 64.0, next(160.0))) + .collect(); + let at: Vec<(i32, i32, i32)> = (0..hoppers) + .map(|_| (next(160.0) as i32, 63, next(160.0) as i32)) + .collect(); + let t = std::time::Instant::now(); + let a: Vec = at.iter().map(|&h| scan(&items, h)).collect(); + let before = t.elapsed().as_secs_f64() * 1e3; + let t = std::time::Instant::now(); + let mut columns = ItemColumns::default(); + let b: Vec = at + .iter() + .map(|&h| columns.may_reach(&items, h.0, h.2) && scan(&items, h)) + .collect(); + let after = t.elapsed().as_secs_f64() * 1e3; + assert_eq!(a, b); + println!("{hoppers} hoppers, {count} items: {before:.3} ms -> {after:.3} ms"); + } + } + + #[test] + fn no_hopper_with_an_item_over_it_is_skipped() { + let mut seed = 0x1234_5678_9abc_def1u64; + let mut next = |range: f64| { + seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1); + (seed >> 11) as f64 / (1u64 << 53) as f64 * range + }; + let mut items: Vec = Vec::new(); + let mut columns = ItemColumns::default(); + let mut next_id = 0; + let mut skipped = 0; + for round in 0..3000 { + // Items come and go as hoppers take them and crafters drop them. + match round % 5 { + 0 | 1 => { + next_id += 1; + // On edges too: a quarter of them sit on a block line. + let snap = |v: f64, edge: bool| if edge { v.floor() + 0.125 } else { v }; + let edge = round % 4 == 0; + items.push(item( + next_id, + snap(next(24.0) - 12.0, edge), + 64.0, + snap(next(24.0) - 12.0, edge), + )); + } + 2 if !items.is_empty() => { + let i = next(items.len() as f64) as usize; + items.remove(i); + } + _ => {} + } + let hopper = (next(26.0) as i32 - 13, 63, next(26.0) as i32 - 13); + let reach = columns.may_reach(&items, hopper.0, hopper.2); + let found = scan(&items, hopper); + assert!( + reach || !found, + "round {round}: skipped a hopper with an item over it" + ); + skipped += usize::from(!reach); + } + assert!(skipped > 1000, "skipped only {skipped}"); + } +} From 9019d708d0b49df09945213fe6e33246aa8b2dc0 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 21 Oct 2026 23:10:14 +0000 Subject: [PATCH 018/124] Streaming: answer "near a player?" from a grid of the players' squares On a server every player is an extra area, and streaming asks of every loaded and wanted column, several times a tick (unloading, dropping protos, finishing, decoration, the generation scan), whether one of those squares reaches it: a scan of every player's square per question. The squares are now one grid of distances, made again when a player changes chunk, and in_extra reads it. Differential test against the scan over 60 random layouts, every margin. At 50 players, 6081 columns, 3 questions each: 0.45 ms -> 0.04 ms, plus 0.02 ms to make the grid. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/streaming.rs | 235 +++++++++++++++++++++++++ 1 file changed, 235 insertions(+) diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 858d05d30..49d1a052b 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -276,6 +276,8 @@ pub struct Streaming { /// chunks finish out to `radius + 1` (Chebyshev) and generation reaches /// past that as around the player. They are generated and kept but not meshed. pub extra: Vec<((i32, i32), i32)>, + /// `extra`'s squares as a grid, made again when `extra` changes. + cover: Option, /// Mesh the sections for drawing (false on a server thread). pub mesh: bool, } @@ -391,6 +393,7 @@ impl GenerationScan { radius: input.ranges.radius, extra: input.ranges.extra, forced: input.ranges.forced, + cover: input.ranges.cover, }; self.remember(ranges, input.world); } else { @@ -562,6 +565,7 @@ impl Streaming { meshed_total: 0, remote: false, extra: Vec::new(), + cover: None, mesh: true, } } @@ -808,6 +812,7 @@ impl Streaming { if self.paused { return (uploads, Vec::new()); } + ExtraCover::refresh(&mut self.cover, &self.extra); self.relight_edits(world); let deferred = std::mem::take(&mut self.deferred); let received = std::iter::from_fn(|| self.results.try_recv().ok()); @@ -982,6 +987,7 @@ impl Streaming { radius: self.radius, extra: &self.extra, forced: &self.forced, + cover: ExtraCover::matching(&self.cover, &self.extra), }; let (protos, generating) = (&self.protos, &self.generating); self.decoration_order.retain(|x, z| { @@ -1109,6 +1115,7 @@ impl Streaming { radius: self.radius, extra: &self.extra, forced: &self.forced, + cover: ExtraCover::matching(&self.cover, &self.extra), }; self.waiting.retain(|&(x, sy, z)| { let back = ranges.in_view(x, z); @@ -1185,6 +1192,7 @@ impl Streaming { radius: self.radius, extra: &self.extra, forced: &self.forced, + cover: ExtraCover::matching(&self.cover, &self.extra), }, world, generating: &self.generating, @@ -1280,6 +1288,7 @@ impl Streaming { radius: self.radius, extra: &self.extra, forced: &self.forced, + cover: ExtraCover::matching(&self.cover, &self.extra), } } fn near_finished(&self, x: i32, z: i32, reach: i32) -> bool { @@ -1365,6 +1374,99 @@ struct Ranges<'a> { radius: i32, extra: &'a [((i32, i32), i32)], forced: &'a HashSet<(i32, i32)>, + /// `extra` as a grid, when one was made for it: `in_extra` without a + /// scan of every area. + cover: Option<&'a ExtraCover>, +} +/// For every column near the extra areas, how far it lies outside the +/// nearest one's square (Chebyshev distance less its radius; 0 inside). A server has an area per player, and streaming asks of every +/// loaded and wanted column whether one reaches it, several times a tick: +/// with fifty players, a scan of fifty squares per question. +struct ExtraCover { + extra: Vec<((i32, i32), i32)>, + x0: i32, + z0: i32, + width: u32, + depth: u32, + cells: Box<[i8]>, +} +impl ExtraCover { + /// The largest margin the grid answers for; beyond it (and outside the + /// grid) a column is further than that from every square. + const REACH: i32 = 4; + /// The largest grid, in columns (a mebibyte). + const CELLS: u64 = 1 << 20; + fn matching<'a>( + cover: &'a Option, + extra: &[((i32, i32), i32)], + ) -> Option<&'a ExtraCover> { + cover.as_ref().filter(|c| c.extra == extra) + } + /// Make the grid again if `extra` changed; none for one area or none + /// (a scan is as quick), or areas too far apart. + fn refresh(cover: &mut Option, extra: &[((i32, i32), i32)]) { + if cover.as_ref().is_some_and(|c| c.extra == extra) { + return; + } + *cover = Self::new(extra); + } + fn new(extra: &[((i32, i32), i32)]) -> Option { + if extra.len() < 2 || extra.iter().any(|&(_, r)| !(0..=64).contains(&r)) { + return None; + } + let k = i64::from(Self::REACH); + let (mut x0, mut z0, mut x1, mut z1) = (i64::MAX, i64::MAX, i64::MIN, i64::MIN); + for &((x, z), r) in extra { + let (x, z, r) = (i64::from(x), i64::from(z), i64::from(r) + k); + (x0, z0, x1, z1) = (x0.min(x - r), z0.min(z - r), x1.max(x + r), z1.max(z + r)); + } + let (width, depth) = ((x1 - x0 + 1) as u64, (z1 - z0 + 1) as u64); + if width * depth > Self::CELLS || x0 < i64::from(i32::MIN) || x1 > i64::from(i32::MAX) { + return None; + } + let (x0, z0, width, depth) = (x0 as i32, z0 as i32, width as u32, depth as u32); + let mut cells = vec![(Self::REACH + 1) as i8; (width * depth) as usize].into_boxed_slice(); + // Inside a square counts as 0: no margin asked for is below it. Each + // row of a square is a run of one value with a slope at either end. + let lower = |run: &mut [i8], v: i8| run.iter_mut().for_each(|c| *c = (*c).min(v)); + for &((cx, cz), r) in extra { + let reach = r + Self::REACH; + for dz in -reach..=reach { + let start = (cz + dz - z0) as usize * width as usize + (cx - x0) as usize; + let row = &mut cells[start - reach as usize..=start + reach as usize]; + let flat = dz.abs().max(r); + let centre = reach as usize; + lower( + &mut row[centre - flat as usize..=centre + flat as usize], + (flat - r) as i8, + ); + for d in flat + 1..=reach { + let v = (d - r) as i8; + lower(&mut row[centre - d as usize..=centre - d as usize], v); + lower(&mut row[centre + d as usize..=centre + d as usize], v); + } + } + } + Some(Self { + extra: extra.to_vec(), + x0, + z0, + width, + depth, + cells, + }) + } + /// Whether (x, z) is within `margin` of an area's square, for a + /// `margin` up to `REACH`. + fn within(&self, x: i32, z: i32, margin: i32) -> bool { + let (dx, dz) = ( + x.wrapping_sub(self.x0) as u32, + z.wrapping_sub(self.z0) as u32, + ); + dx < self.width + && dz < self.depth + && i32::from(self.cells[(dz * self.width + dx) as usize]) <= margin + } } impl Ranges<'_> { /// `distance`, or `rings` while the level loading screen is up. @@ -1441,6 +1543,11 @@ impl Ranges<'_> { /// Within `margin` of an extra area's square (Chebyshev distance, as /// vanilla's ticket levels spread). fn in_extra(&self, x: i32, z: i32, margin: i32) -> bool { + if let Some(cover) = self.cover + && (0..=ExtraCover::REACH).contains(&margin) + { + return cover.within(x, z, margin); + } self.extra .iter() .any(|&(c, radius)| (x - c.0).abs().max((z - c.1).abs()) <= radius + margin) @@ -1554,6 +1661,7 @@ mod tests { radius: self.radius, extra: &self.extra, forced: &self.forced, + cover: None, }, world: &self.world, generating: &self.generating, @@ -1866,6 +1974,7 @@ mod tests { radius, extra: &[], forced: &forced, + cover: None, }; let square = |x: i32, z: i32, n: i32| { (-n..=n).flat_map(move |dz| (-n..=n).map(move |dx| (x + dx, z + dz))) @@ -1920,6 +2029,7 @@ mod tests { radius: 12, extra: &[], forced: &forced, + cover: None, }; let at = |dx: i32, dz: i32| (2 + dx, -1 + dz); let key = |(x, z): (i32, i32), loading| r.priority(x, z, loading); @@ -1945,6 +2055,7 @@ mod tests { radius, extra: &[((radius * 2 + 9, 3), 4)], forced: &forced, + cover: None, }; let span = radius * 3 + 20; for x in -span..=span { @@ -1984,6 +2095,7 @@ mod tests { radius: 2, extra: &extra, forced: &forced, + cover: None, }; let level = |x: i32, z: i32| 31 + ((x - 40).abs().max((z + 9).abs()) - view).max(0); for x in 40 - view - 8..=40 + view + 8 { @@ -2021,3 +2133,126 @@ mod tests { } } } + +#[cfg(test)] +mod cover_tests { + use super::*; + + fn areas(seed: &mut u64, n: usize, spread: i32) -> Vec<((i32, i32), i32)> { + let mut next = |m: i32| { + *seed ^= *seed << 13; + *seed ^= *seed >> 7; + *seed ^= *seed << 17; + (*seed % (2 * m as u64 + 1)) as i32 - m + }; + (0..n) + .map(|_| ((next(spread), next(spread)), 2 + next(14).abs())) + .collect() + } + + /// Every question streaming asks gets the same answer with the grid. + #[test] + fn the_grid_answers_as_the_scan() { + let mut seed = 0x9E37_79B9_7F4A_7C15u64; + let forced: HashSet<(i32, i32)> = [(40, -40)].into(); + for case in 0..60 { + let extra = areas(&mut seed, 2 + case % 20, [4, 30, 120][case % 3]); + let cover = ExtraCover::new(&extra).expect("a grid"); + let ranges = |cover| Ranges { + center: (3, -2), + radius: 10, + extra: &extra, + forced: &forced, + cover, + }; + let (scan, grid) = (ranges(None), ranges(Some(&cover))); + let span = [4, 30, 120][case % 3] + 40; + for z in -span..=span { + for x in -span..=span { + for m in 0..=ExtraCover::REACH + 2 { + assert_eq!( + scan.in_extra(x, z, m), + grid.in_extra(x, z, m), + "case {case} ({x}, {z}) {m}" + ); + } + for d in [false, true] { + assert_eq!(scan.unloaded(x, z, d), grid.unloaded(x, z, d)); + assert_eq!(scan.requested(x, z, d), grid.requested(x, z, d)); + assert_eq!(scan.chunk_kept(x, z, d), grid.chunk_kept(x, z, d)); + } + } + } + } + } + + #[test] + fn a_stale_or_impossible_grid_is_not_used() { + let mut cover = None; + ExtraCover::refresh(&mut cover, &[((0, 0), 11), ((5, 5), 11)]); + assert!(ExtraCover::matching(&cover, &[((0, 0), 11), ((5, 5), 11)]).is_some()); + assert!(ExtraCover::matching(&cover, &[((0, 0), 11), ((5, 6), 11)]).is_none()); + // Too far apart for a mebibyte of grid, and one area alone: scans. + assert!(ExtraCover::new(&[((0, 0), 11), ((100_000, 0), 11)]).is_none()); + assert!(ExtraCover::new(&[((0, 0), 11)]).is_none()); + assert!(ExtraCover::new(&[((i32::MAX - 1, 0), 11), ((i32::MAX - 3, 0), 11)]).is_none()); + } + + /// cargo test --release -p horizon-client --lib cover_tests::cover_cost -- --ignored --nocapture + #[test] + #[ignore] + fn cover_cost() { + let forced = HashSet::new(); + for players in [10usize, 25, 50] { + // The benchmark's bots: most near spawn, a fifth flying outwards. + let extra: Vec<_> = (0..players) + .map(|i| { + let a = i as f64 * 2.399; + let d = if i % 5 == 0 { 30.0 } else { 3.0 }; + (((a.cos() * d) as i32, (a.sin() * d) as i32), 11) + }) + .collect(); + let cover = ExtraCover::new(&extra).unwrap(); + let ranges = |cover| Ranges { + center: (0, 0), + radius: 10, + extra: &extra, + forced: &forced, + cover, + }; + // The columns streaming asks about: everything kept, and the + // generation scan's ring past it. + let columns: Vec<(i32, i32)> = (-60..=60) + .flat_map(|z| (-60..=60).map(move |x| (x, z))) + .filter(|&(x, z)| { + ranges(None).requested(x, z, true) || !ranges(None).unloaded(x, z, true) + }) + .collect(); + let rounds = 100; + let time = |r: &Ranges| { + let t = std::time::Instant::now(); + let mut n = 0usize; + for _ in 0..rounds { + for &(x, z) in &columns { + n += usize::from(r.unloaded(x, z, true)); + n += usize::from(r.requested(x, z, true)); + n += usize::from(r.in_range(x, z, 1)); + } + } + std::hint::black_box(n); + t.elapsed().as_secs_f64() * 1e3 / f64::from(rounds) + }; + let t = std::time::Instant::now(); + for _ in 0..rounds { + std::hint::black_box(ExtraCover::new(std::hint::black_box(&extra))); + } + let build = t.elapsed().as_secs_f64() * 1e3 / f64::from(rounds); + println!( + "{players} players, {} columns, 3 questions each: scan {:.3} ms, grid {:.3} ms (+ {build:.3} ms to build)", + columns.len(), + time(&ranges(None)), + time(&ranges(Some(&cover))), + ); + } + } +} From dfe795bc090809090993c44c2a032f108efbf3f4 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 02:42:12 +0000 Subject: [PATCH 019/124] Faster Perlin volumes and climate search, same results PerlinNoise::add_volume worked out each Y coordinate's cell, offset and fade once per X/Z column. Every column walks the same Y coordinates, so they are now computed once per volume. The arithmetic is unchanged, so the output is bit for bit the same. ClimateIndex kept its tree as nodes with a Vec of child ids each. The built tree is now renumbered breadth first into flat arrays, so a branch's children are one contiguous run. The search visits the same nodes in the same order, so ties resolve exactly as before. terrain_bench (seed 12345, 8x8 chunks, 1 thread, medians of 3 runs): terrain 14.5 -> 12.4 ms/chunk, biomes 1.20 -> 1.06 ms/chunk. Checked against vanilla 26.3 in-process: 2304 chunks on seeds 12345, -1 and 987654321987 identical, and the biome-source and biome-generation parity suites (2.1M lines) match. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01M3pNrSHA2ifNrH7SAMHgSW --- crates/horizon-core/src/noise/perlin.rs | 58 ++++++++++++----- .../src/worldgen/climate/index.rs | 65 ++++++++++++------- 2 files changed, 84 insertions(+), 39 deletions(-) diff --git a/crates/horizon-core/src/noise/perlin.rs b/crates/horizon-core/src/noise/perlin.rs index 725373eff..92fa2a880 100644 --- a/crates/horizon-core/src/noise/perlin.rs +++ b/crates/horizon-core/src/noise/perlin.rs @@ -164,6 +164,31 @@ impl PerlinNoise { return Err(DensityError::BufferTooShort); } let [size_x, size_y, size_z] = volume.sizes(); + // Every X/Z column walks the same Y coordinates, so their cells, offsets + // and fades are computed once per volume instead of once per column. + let mut stack = [YSample::default(); STACK_Y]; + let mut heap = Vec::new(); + let ys = if size_y <= STACK_Y { + &mut stack[..size_y] + } else { + heap.resize(size_y, YSample::default()); + &mut heap[..] + }; + for (y, sample) in ys.iter_mut().enumerate() { + let input_y = f64::from(volume.coordinate(1, y)) * y_scale; + let shifted_y = wrap(input_y) + self.table.offsets[1]; + let cell = floor_i32(shifted_y); + let dy = shifted_y - f64::from(cell); + *sample = YSample { + cell, + fade: smoothstep(dy as f32), + offset: match fudge_y_scale { + Some(scale) => (dy - compute_fudge_y(input_y, dy, scale)) as f32, + None => dy as f32, + }, + }; + } + let ys = &*ys; let mut index = 0; for z in 0..size_z { let shifted_z = @@ -182,37 +207,40 @@ impl PerlinNoise { let mut previous_y = i32::MIN; let mut xz_dots = [0.0_f32; 8]; let mut y_gradients = [0.0_f32; 8]; - for y in 0..size_y { - let input_y = f64::from(volume.coordinate(1, y)) * y_scale; - let shifted_y = wrap(input_y) + self.table.offsets[1]; - let cell_y = floor_i32(shifted_y); - let dy = shifted_y - f64::from(cell_y); - let fade_y = smoothstep(dy as f32); - if previous_y != cell_y { - let gradients = self.gradients_at(p, q, cell_y, cell_z); + for (sample, out) in ys.iter().zip(&mut buffer[index..index + size_y]) { + if previous_y != sample.cell { + let gradients = self.gradients_at(p, q, sample.cell, cell_z); for i in 0..8 { xz_dots[i] = gradients[i][0] as f32 * (dx - (i & 1) as f32) + gradients[i][2] as f32 * (dz - ((i >> 2) & 1) as f32); y_gradients[i] = gradients[i][1] as f32; } - previous_y = cell_y; + previous_y = sample.cell; } - let dy = match fudge_y_scale { - Some(scale) => (dy - compute_fudge_y(input_y, dy, scale)) as f32, - None => dy as f32, - }; + let dy = sample.offset; let values = std::array::from_fn(|i| { xz_dots[i] + y_gradients[i] * (dy - ((i >> 1) & 1) as f32) }); - buffer[index] += amplitude * lerp3([fade_x, fade_y, fade_z], values); - index += 1; + *out += amplitude * lerp3([fade_x, sample.fade, fade_z], values); } + index += size_y; } } Ok(()) } } +/// Y columns up to this height keep their per-Y samples on the stack. +const STACK_Y: usize = 64; + +#[derive(Clone, Copy, Default)] +struct YSample { + cell: i32, + fade: f32, + /// The Y offset inside the cell used for the corner values, after any fudge. + offset: f32, +} + #[derive(Clone, Debug)] pub struct SmearedPerlinNoise { base: PerlinNoise, diff --git a/crates/horizon-world/src/worldgen/climate/index.rs b/crates/horizon-world/src/worldgen/climate/index.rs index 3e9b50f51..6a602ed92 100644 --- a/crates/horizon-world/src/worldgen/climate/index.rs +++ b/crates/horizon-world/src/worldgen/climate/index.rs @@ -202,8 +202,11 @@ fn build( #[derive(Debug)] pub struct ClimateIndex { entries: Vec<(ParameterPoint, T)>, - nodes: Vec, - root: usize, + /// The built tree renumbered breadth first, root at 0, so each branch's + /// children are one contiguous run and a search walks memory in order. + spaces: Vec<[Parameter; 7]>, + /// Per node: its first child and child count, or its entry and 0 for a leaf. + links: Vec<(u32, u32)>, } impl ClimateIndex { pub fn new(entries: Vec<(ParameterPoint, T)>) -> Result { @@ -232,10 +235,25 @@ impl ClimateIndex { .collect(); let children = (0..nodes.len()).collect(); let root = build(&mut nodes, children, branching)?; + let mut order = vec![root]; + let mut links = Vec::with_capacity(nodes.len()); + let mut next = 0; + while next < order.len() { + let node = &nodes[order[next]]; + next += 1; + links.push(match &node.children { + Children::Leaf(entry) => (*entry as u32, 0), + Children::Branch(children) => { + let first = order.len() as u32; + order.extend_from_slice(children); + (first, children.len() as u32) + } + }); + } Ok(Self { entries, - nodes, - root, + spaces: order.iter().map(|id| nodes[*id].space).collect(), + links, }) } pub fn entries(&self) -> &[(ParameterPoint, T)] { @@ -263,18 +281,18 @@ impl ClimateIndex { } /// Preorder leaf entry indices expose stable tree layout for comparison. pub fn leaf_order(&self) -> Vec { - fn walk(nodes: &[Node], id: usize, out: &mut Vec) { - match &nodes[id].children { - Children::Leaf(entry) => out.push(*entry), - Children::Branch(children) => { - for child in children { - walk(nodes, *child, out); + fn walk(links: &[(u32, u32)], id: usize, out: &mut Vec) { + match links[id] { + (entry, 0) => out.push(entry as usize), + (first, count) => { + for child in first..first + count { + walk(links, child as usize, out); } } } } let mut result = Vec::with_capacity(self.entries.len()); - walk(&self.nodes, self.root, &mut result); + walk(&self.links, 0, &mut result); result } fn search( @@ -283,19 +301,20 @@ impl ClimateIndex { values: &[i64; 7], previous: Option, ) -> Result { - let Children::Branch(children) = &self.nodes[id].children else { + let (first, count) = self.links[id]; + if count == 0 { return Ok(id); - }; + } let mut best = previous; - let mut fitness = previous.map_or(i64::MAX, |id| distance(&self.nodes[id].space, values)); - for child in children { - let lower = distance(&self.nodes[*child].space, values); + let mut fitness = previous.map_or(i64::MAX, |id| distance(&self.spaces[id], values)); + for child in first as usize..(first + count) as usize { + let lower = distance(&self.spaces[child], values); if fitness > lower { - let result = self.search(*child, values, best)?; - let next = if result == *child { + let result = self.search(child, values, best)?; + let next = if result == child { lower } else { - distance(&self.nodes[result].space, values) + distance(&self.spaces[result], values) }; if fitness > next { fitness = next; @@ -313,13 +332,11 @@ pub struct ClimateSearch<'a, T> { } impl<'a, T> ClimateSearch<'a, T> { pub fn find(&mut self, target: TargetPoint) -> Result<&'a T, IndexError> { - let result = self - .index - .search(self.index.root, &target.parameters(), self.last)?; + let result = self.index.search(0, &target.parameters(), self.last)?; self.last = Some(result); - let Children::Leaf(entry) = self.index.nodes[result].children else { + let (entry, 0) = self.index.links[result] else { unreachable!("search returns a leaf"); }; - Ok(&self.index.entries[entry].1) + Ok(&self.index.entries[entry as usize].1) } } From 86b58b71fab6f682201002c2d258996450dde3f0 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Wed, 21 Oct 2026 21:19:46 +0200 Subject: [PATCH 020/124] Client of a server: abilities from the server, flight sent back A player who joined already in creative could not fly: the remote client kept only instabuild and the flying speed of ClientboundPlayerAbilitiesPacket, and the motion step clears flying for a player that may not fly. The packet also comes before the client has a player (it is made at the first teleport). Single player creative on its server thread could not fly at all; a LAN guest only after a game mode change. - The remote client keeps the last abilities and applies flying, may fly, invulnerable and the flying speed to the player, also when the player is made (ClientPacketListener.handlePlayerAbilities). - When its flight turns on or off (double jump, spectator, landing) it sends ServerboundPlayerAbilitiesPacket (LocalPlayer.onUpdateAbilities). - The hub keeps it as the guest's flying when the guest may fly (handlePlayerAbilities), so the save has it, and a game mode change sets it as GameType.updatePlayerAbilities does. - lan_e2e `flight`: the guest, and on the server thread the host's window, fly in creative, stay up, and stop in survival. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/remote.rs | 35 +++++++++++++- crates/horizon-client/src/lan/hub.rs | 13 ++++++ crates/horizon-protocol/src/packets/play.rs | 14 ++++++ tools/lan_e2e.py | 51 ++++++++++++++++++++- 4 files changed, 109 insertions(+), 4 deletions(-) diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 17c6e0299..f7346199a 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -99,6 +99,10 @@ pub(super) struct Remote { last_input: u8, was_sprinting: bool, carried: Option, + /// The last ClientboundPlayerAbilitiesPacket. Vanilla's LocalPlayer + /// holds its abilities from the login on; here the player is made at + /// the first teleport, which takes them then. + abilities: Option, loaded_sent: bool, /// The view distance last sent in ClientInformation. sent_view: i8, @@ -313,6 +317,7 @@ impl Game<'_> { last_input: 0, was_sprinting: false, carried: None, + abilities: None, loaded_sent: false, sent_view: view, predictions: HashMap::new(), @@ -1840,9 +1845,10 @@ impl Game<'_> { }, C::PlayerAbilities(p) => { self.creative = p.flags & 8 != 0; - if let Some(player) = &mut self.player { - player.flying_speed = p.flying_speed; + if let Some(r) = self.remote.as_mut() { + r.abilities = Some(p); } + self.apply_remote_abilities(); } C::SetHeldSlot(p) => { self.selected = p.slot.clamp(0, 8) as usize; @@ -2485,6 +2491,7 @@ impl Game<'_> { self.previous_eye = 1.62; self.clock.reset(); self.apply_remote_health(); + self.apply_remote_abilities(); } } self.previous_position = position; @@ -2620,6 +2627,7 @@ impl Game<'_> { [player.velocity.x, player.velocity.y, player.velocity.z], [player.position.x, player.position.y, player.position.z], ); + let was_flying = player.flying; player.yaw = self.yaw; let dead = self.screen == Screen::Dead; // LivingEntity.isImmobile: a sleeper lies still in the bed. @@ -2642,6 +2650,9 @@ impl Game<'_> { } .controlled_simulation_tick(keys)?; } + // LocalPlayer.onUpdateAbilities: flight turned on or off (a double + // jump, a spectator, landing) is sent to the server. + let flying = player.flying; let events = std::mem::take(&mut player.events); self.previous_eye = self.eye; self.eye += (player.dimensions.eye_height - self.eye) * 0.5; @@ -2676,9 +2687,29 @@ impl Game<'_> { if !dead { self.remote_tick_using(); } + if flying != was_flying { + self.send(S::PlayerAbilities(play::PlayerAbilitiesRequest { + flags: if flying { 2 } else { 0 }, + })); + } self.remote_send_state(input); Ok(()) } + + /// ClientPacketListener.handlePlayerAbilities: the player flies, may fly + /// and is invulnerable as the server says (bits 2, 4 and 1). + fn apply_remote_abilities(&mut self) { + let (Some(player), Some(a)) = ( + self.player.as_mut(), + self.remote.as_ref().and_then(|r| r.abilities.as_ref()), + ) else { + return; + }; + player.invulnerable = a.flags & 1 != 0; + player.flying = a.flags & 2 != 0; + player.may_fly = a.flags & 4 != 0; + player.flying_speed = a.flying_speed; + } /// The local player's share of every touching entity's /// `LivingEntity.pushEntities`: on a client `EntitySelector.pushableBy` /// lets entities push only the local player, so the server never sends diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index 9c65e9292..4e8269e80 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -1872,6 +1872,7 @@ impl Hub { // (handlePlayerKnownMovement), PlayerInput once loaded. let acted = match &packet { S::AcceptTeleportation(_) + | S::PlayerAbilities(_) | S::MovePlayerStatus(_) | S::MovePlayerRot(_) | S::PlayerLoaded(_) @@ -1909,6 +1910,11 @@ impl Hub { self.position_change(i, [p.x, p.y, p.z], rot, 0, ctx, events); } } + // handlePlayerAbilities: the client flies only if it may. + S::PlayerAbilities(p) => { + let g = &mut self.guests[i]; + g.flying = p.flags & 2 != 0 && (g.creative() || g.spectator()); + } S::MovePlayerPos(p) => { self.move_player(i, Some([p.x, p.y, p.z]), None, p.flags, ctx, events); } @@ -2183,6 +2189,13 @@ impl Hub { return; }; g.game_mode = mode; + // GameType.updatePlayerAbilities: a spectator flies, survival and + // adventure don't, creative keeps what it had. + if g.spectator() { + g.flying = true; + } else if !g.creative() { + g.flying = false; + } let uuid = g.uuid; g.send(&C::GameEvent(play::GameEvent { event: game_event::CHANGE_GAME_MODE, diff --git a/crates/horizon-protocol/src/packets/play.rs b/crates/horizon-protocol/src/packets/play.rs index 95a68d1ef..c5d5eb68c 100644 --- a/crates/horizon-protocol/src/packets/play.rs +++ b/crates/horizon-protocol/src/packets/play.rs @@ -1674,6 +1674,8 @@ packet!( ); packet!(ChangeDifficultyRequest { difficulty: i32 => VarInt }); packet!(LockDifficulty { locked: bool => Bool }); +// ServerboundPlayerAbilitiesPacket: only whether the player flies (2). +packet!(PlayerAbilitiesRequest { flags: i8 => Byte }); packet!(SetGameRule { entries: Vec => List }); packet!(ChunkBatchReceived { chunks_per_tick: f32 => Float }); packet!( @@ -1786,6 +1788,7 @@ packet_enum!(Serverbound, crate::ids::play::serverbound { MovePlayerStatus(MovePlayerStatus) = MOVE_PLAYER_STATUS_ONLY, PickItemFromBlock(PickItemFromBlock) = PICK_ITEM_FROM_BLOCK, PingRequest(crate::packets::status::Ping) = PING_REQUEST, + PlayerAbilities(PlayerAbilitiesRequest) = PLAYER_ABILITIES, PlayerAction(PlayerAction) = PLAYER_ACTION, PlayerCommand(PlayerCommand) = PLAYER_COMMAND, PlayerInput(PlayerInput) = PLAYER_INPUT, @@ -1805,6 +1808,17 @@ mod tests { use super::*; use crate::{chunk::BlockEntityInfo, packets::round_trip, text::literal}; + #[test] + fn serverbound_abilities_carry_only_flying() { + // ServerboundPlayerAbilitiesPacket.write: one byte, 2 when flying. + let flying = Serverbound::PlayerAbilities(PlayerAbilitiesRequest { flags: 2 }); + assert_eq!( + flying.encode(), + [crate::ids::play::serverbound::PLAYER_ABILITIES as u8, 2] + ); + round_trip(flying, Serverbound::encode, Serverbound::decode); + } + #[test] fn world_options_packets_match_vanilla_stream_codecs() { // mcsrc.dev 26.3: difficulty is a VarInt, locking is a Boolean, diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 0b40e55e4..61697550b 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -74,6 +74,9 @@ address. The script then checks, from both sides, what a guest's game used to ge 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. +- flight: a player in creative double-taps jump and flies, stays up, and stops flying in survival: + the guest, and on the server thread the host's window too, take what may fly from the + server's abilities (ClientPacketListener.handlePlayerAbilities). - 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). @@ -111,7 +114,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', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth'] +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', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight'] class Game: @@ -1607,6 +1610,50 @@ 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_flight(host, guest, out): + def double_jump(game): + # LocalPlayer.aiStep: a second jump press within 7 ticks of the first toggles flight. + for hold in (1, 4): + game.raw('key', 'space', 'down') + game.wait(hold) + game.raw('key', 'space', 'up') + game.wait(2) + game.wait(10) + return game('state')['player'] + + def set_mode(game, mode): + # On a client of a server the mode and its abilities (may fly) come back with the server's + # answer, later when the machine is busy: a double jump before them is two plain jumps. + game('command', command=f'gamemode {mode}') + for _ in range(100): + if game('state').get('gamemode') == mode: + break + game.wait(1) + game.wait(10) + + players = [('guest', guest)] + ([('host', host)] if SERVER_THREAD else []) + seen = {} + try: + for name, game in players: + set_mode(game, 'creative') + ground = game('state')['player'] + up = double_jump(game) + game.wait(20) + later = game('state')['player'] + set_mode(game, 'survival') + fell = game('state')['player'] + seen[name] = {'flying': up['flying'], 'rose': round(up['position'][1] - ground['position'][1], 2), + 'stayed': round(later['position'][1] - up['position'][1], 2), + 'survival_flying': fell['flying']} + finally: + for _, game in players: + game('command', command='gamemode survival') + for name, s in seen.items(): + assert s['flying'] and s['rose'] > 1 and abs(s['stayed']) < 0.1, f'{name} does not fly in creative: {seen}' + assert not s['survival_flying'], f'{name} still flies in survival: {seen}' + return seen + + 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) @@ -1712,7 +1759,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, 'cracks': check_cracks, - 'time': check_time, 'growth': check_growth} + 'time': check_time, 'growth': check_growth, 'flight': check_flight} def run(ctl, out, resources, checks): From 0468b75c435215617e187ebdd87db194e917f6dc Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 16:17:11 +0200 Subject: [PATCH 021/124] Run single player on its server thread by default Single player's world now runs on the integrated server thread unless asked otherwise, as vanilla's does, so a world tick no longer holds up a frame. `--no-server-thread` or `HORIZON_SERVER_THREAD=0` keeps the world in the frame as an escape hatch; `--server-thread` stays. lan_e2e.py sets the host's mode explicitly either way. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 4ff23b686d44249c9c8d6dc9d0f9d3c12ed6afcb) --- crates/horizon-client/src/game.rs | 10 ++++++---- crates/horizon-client/src/game/integrated.rs | 6 +++--- tools/lan_e2e.py | 4 ++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index a25e8fdeb..35814dd6c 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -267,8 +267,9 @@ struct Options { benchmark: Option, /// `--dev-mod PATH` (repeatable): mods always on and reloaded when rebuilt. dev_mods: Vec, - /// `--server-thread` or `HORIZON_SERVER_THREAD=1`: single player runs on - /// an integrated server thread (game/integrated.rs). + /// Single player runs on an integrated server thread (game/integrated.rs), + /// as vanilla's does: the default. `--no-server-thread` or + /// `HORIZON_SERVER_THREAD=0` runs the world in the frame instead. server_thread: bool, /// Set on the integrated server's own options: it draws nothing, so it skips meshing. integrated: bool, @@ -323,7 +324,7 @@ impl Options { dedicated: None, benchmark: None, dev_mods: Vec::new(), - server_thread: std::env::var("HORIZON_SERVER_THREAD").is_ok_and(|v| v == "1"), + server_thread: std::env::var("HORIZON_SERVER_THREAD").map_or(true, |v| v != "0"), integrated: false, }; let mut session_file = std::env::var("HORIZON_SESSION_FILE").ok(); @@ -332,7 +333,7 @@ impl Options { match arg.as_str() { "--help" | "-h" => { println!( - "Horizon — Minecraft Java Edition {} in Rust\n\n --resources PATH Prepared resources (horizon-prepare)\n --render-distance N Chunks, 2..64 (default 12)\n --world NAME World folder under saves/ (default \"New World\")\n --seed N World seed for new worlds (default 0)\n --creative Creative mode (default survival)\n --test-terrain Simple test terrain instead of vanilla generation\n --frames N Exit after N frames (testing)\n --world-frames N Exit after N frames drawn in the world (testing)\n --smoke-report PATH Write a JSON report on exit\n --benchmark SCENARIO Run a benchmark (standard, village, forest, ocean, mountain), write its report to benchmarks/ and quit\n --control Accept horizon-ctl commands on a local socket\n --control-socket PATH Control socket path (default $XDG_RUNTIME_DIR/horizon-control.sock)\n --server HOST[:PORT] Join a multiplayer server (offline mode) at start\n --name NAME Player name for multiplayer (default: generated once, kept in horizon-player.txt)\n --session-file PATH Signed-in account from the launcher (read once, then deleted)\n --dev-mod PATH A Horizon mod (.hmod or folder) to develop: always on, reloaded when rebuilt\n\nControls: mouse look, WASD move, Space jump, Left Shift sneak, Left Ctrl sprint,\nleft click break, right click place/use, 1-9/scroll hotbar, E inventory,\nQ drop, F3 debug, Esc pause.", + "Horizon — Minecraft Java Edition {} in Rust\n\n --resources PATH Prepared resources (horizon-prepare)\n --render-distance N Chunks, 2..64 (default 12)\n --world NAME World folder under saves/ (default \"New World\")\n --seed N World seed for new worlds (default 0)\n --creative Creative mode (default survival)\n --test-terrain Simple test terrain instead of vanilla generation\n --frames N Exit after N frames (testing)\n --world-frames N Exit after N frames drawn in the world (testing)\n --smoke-report PATH Write a JSON report on exit\n --benchmark SCENARIO Run a benchmark (standard, village, forest, ocean, mountain), write its report to benchmarks/ and quit\n --control Accept horizon-ctl commands on a local socket\n --control-socket PATH Control socket path (default $XDG_RUNTIME_DIR/horizon-control.sock)\n --server HOST[:PORT] Join a multiplayer server (offline mode) at start\n --name NAME Player name for multiplayer (default: generated once, kept in horizon-player.txt)\n --session-file PATH Signed-in account from the launcher (read once, then deleted)\n --dev-mod PATH A Horizon mod (.hmod or folder) to develop: always on, reloaded when rebuilt\n --no-server-thread Run single player's world in the frame, not on its server thread\n\nControls: mouse look, WASD move, Space jump, Left Shift sneak, Left Ctrl sprint,\nleft click break, right click place/use, 1-9/scroll hotbar, E inventory,\nQ drop, F3 debug, Esc pause.", horizon_core::VANILLA_VERSION ); return Ok(None); @@ -347,6 +348,7 @@ impl Options { "--offscreen" => o.offscreen = true, "--control" => o.control = Some(horizon_client::control::default_path()), "--server-thread" => o.server_thread = true, + "--no-server-thread" => o.server_thread = false, _ => { let value = args .next() diff --git a/crates/horizon-client/src/game/integrated.rs b/crates/horizon-client/src/game/integrated.rs index 6e1fdf589..ef5775174 100644 --- a/crates/horizon-client/src/game/integrated.rs +++ b/crates/horizon-client/src/game/integrated.rs @@ -6,9 +6,9 @@ //! the world's options) with [`Integrated::call`], as vanilla's client calls //! into its `IntegratedServer`. //! -//! Opt-in for now (`--server-thread` or `HORIZON_SERVER_THREAD=1`): the -//! player joins as a network player, and the server side of a network player -//! does not do everything single player does yet. +//! On by default. `--no-server-thread` or `HORIZON_SERVER_THREAD=0` runs +//! the world in the frame instead (the escape hatch while the network +//! player's side catches up with the in-frame one). use super::{ Game, Options, PackResources, Resources, UiResources, lan::{LanSettings, Owner}, diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 40c081a5a..70b44407b 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -1777,8 +1777,8 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'name def run(ctl, out, resources, checks): host, guest = Game('host', ctl, resources), Game('guest', ctl, resources) - if SERVER_THREAD: - host.d.env['HORIZON_SERVER_THREAD'] = '1' + # Either way explicitly: the game's default is the server thread. + host.d.env['HORIZON_SERVER_THREAD'] = '1' if SERVER_THREAD else '0' results = {} try: host('launch', wait=False, offscreen=sys.platform == 'darwin') From 2f883b5cdbc44dd573bbe4f16dca2eec920ae8d3 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 20:54:13 +0200 Subject: [PATCH 022/124] Keep the command harness in the frame; compare finished border chunks - The command harness (HORIZON_COMMAND_SCRIPT) drives the world itself, without the event loop, so it does not start the server thread. - `status` lists the minecarts a client of a server was sent. - save_roundtrip: single player's server thread finishes the chunks its player tracks, as vanilla's integrated server does, including some that vanilla's server (view distance 4) left before their terrain or light. Those had no blocks, light or heightmaps to compare yet. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 2818c89f242fd0856c098ceeb7c809c776120240) --- crates/horizon-client/src/game.rs | 5 +++- crates/horizon-client/src/game/control.rs | 4 ++- crates/horizon-client/src/game/remote.rs | 22 ++++++++++++++++ tools/test_save_roundtrip_parity.py | 32 ++++++++++++++++++++--- 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 35814dd6c..affc120e7 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -324,7 +324,10 @@ impl Options { dedicated: None, benchmark: None, dev_mods: Vec::new(), - server_thread: std::env::var("HORIZON_SERVER_THREAD").map_or(true, |v| v != "0"), + // The command harness (HORIZON_COMMAND_SCRIPT) drives the world + // itself, without the event loop: it stays in the frame. + server_thread: std::env::var("HORIZON_SERVER_THREAD").map_or(true, |v| v != "0") + && std::env::var_os("HORIZON_COMMAND_SCRIPT").is_none(), integrated: false, }; let mut session_file = std::env::var("HORIZON_SESSION_FILE").ok(); diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index 6c69904ac..28e6c56c0 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -879,7 +879,9 @@ impl Game<'_> { "tridents": self.tridents.len(), "rockets": self.rockets.len(), "fight": self.dragon_status(), - "carts": self.carts.iter().map(|c| serde_json::json!({"kind": c.kind.id(), "position": [c.position.x, c.position.y, c.position.z], "velocity": [c.velocity.x, c.velocity.y, c.velocity.z]})).collect::>(), + // A client of a server (single player's server thread) knows the + // carts it was sent. + "carts": self.remote_carts().unwrap_or_else(|| self.carts.iter().map(|c| serde_json::json!({"kind": c.kind.id(), "position": [c.position.x, c.position.y, c.position.z], "velocity": [c.velocity.x, c.velocity.y, c.velocity.z]})).collect::>()), "riding": self.riding, "using": self.using, "render_distance": self.streaming.radius, diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index f7346199a..b770e71a9 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -529,6 +529,28 @@ impl Game<'_> { } } + /// The minecarts the server sent, for the control socket's `status` + /// (their velocity is not tracked here). + pub(super) fn remote_carts(&self) -> Option> { + let r = self.remote.as_ref()?; + let mut carts: Vec<&Tracked> = r + .entities + .values() + .filter(|t| t.kind.ends_with("minecart")) + .collect(); + carts.sort_by(|a, b| { + a.position + .partial_cmp(&b.position) + .unwrap_or(std::cmp::Ordering::Equal) + }); + Some( + carts + .into_iter() + .map(|t| serde_json::json!({"kind": t.kind, "position": t.position, "velocity": [0.0, 0.0, 0.0]})) + .collect(), + ) + } + /// The names in the server's player list, sorted. pub(super) fn remote_online_names(&self) -> Option> { let r = self.remote.as_ref()?; diff --git a/tools/test_save_roundtrip_parity.py b/tools/test_save_roundtrip_parity.py index e2531a657..72bea8845 100644 --- a/tools/test_save_roundtrip_parity.py +++ b/tools/test_save_roundtrip_parity.py @@ -68,6 +68,14 @@ IGNORED = { # differently (no worldgen heightmaps, entities in entities/) is accepted. r'^chunk\.promoted\.(Status|Heightmaps\.(OCEAN_FLOOR_WG|WORLD_SURFACE_WG)|entities)$': 'a proto chunk vanilla left at its view distance, finished by Horizon', + # Single player's server thread sends the chunks its player tracks (view distance 6 + # and its outer ring), as vanilla's integrated server does, so it finishes some that + # vanilla's server (view distance 4) left before their terrain: those had no blocks, + # light or heightmaps to compare yet. + r'^chunk\.blank\.': + 'a chunk vanilla left before its terrain (no blocks yet), finished by Horizon', + r'^chunk\.unlit\.': + 'a chunk vanilla left before its light, finished (and lit) by Horizon', # Horizon keeps simulating between loading and saving (it needs a few seconds of # chunk generation first), so counters and positions of live things move on. r'^chunk\.block_entities\.\(2, -60, 5\)\.(cooking_time_spent|lit_time_remaining|Items\[\]\.count)$': @@ -464,6 +472,13 @@ def normalize_chunk(root): return out, sections +# ChunkStatus before NOISE: nothing has filled the chunk's blocks yet. +BLANK_STATUSES = {'minecraft:empty', 'minecraft:structure_starts', 'minecraft:structure_references', + 'minecraft:biomes'} +# ChunkStatus LIGHT and later: the chunk's light is saved. +LIT_STATUSES = {'minecraft:light', 'minecraft:spawn', 'minecraft:full'} + + def compare_chunks(vanilla, horizon, report): for key in sorted(set(vanilla) | set(horizon)): if key not in horizon: @@ -476,9 +491,10 @@ def compare_chunks(vanilla, horizon, report): ha, hs = normalize_chunk(horizon[key]) out = [] diff(va, ha, 'chunk', out) - if va.get('Status') != 'minecraft:full' and ha.get('Status') == 'minecraft:full': - promoted = re.compile(r'^chunk\.(Status|Heightmaps\.\w+_WG|entities)$') - out = [(path.replace('chunk.', 'chunk.promoted.', 1) if promoted.match(path) else path, p, q) + promoted = va.get('Status') != 'minecraft:full' and ha.get('Status') == 'minecraft:full' + if promoted: + rule = re.compile(r'^chunk\.(Status|Heightmaps\.\w+_WG|entities)$') + out = [(path.replace('chunk.', 'chunk.promoted.', 1) if rule.match(path) else path, p, q) for path, p, q in out] for y in sorted(set(vs) | set(hs)): empty = {'blocks': ['minecraft:air'] * 4096, 'biomes': None, 'BlockLight': None, 'SkyLight': None} @@ -493,6 +509,16 @@ def compare_chunks(vanilla, horizon, report): if a[light] != b[light]: out.append((f'chunk.sections.{light}[{y}]', None if a[light] is None else 'present', None if b[light] is None else 'present')) + # Before its terrain a chunk has no blocks, light or heightmaps yet. + if promoted and va.get('Status') in BLANK_STATUSES: + blank = re.compile(r'^chunk\.(Heightmaps|sections)\.') + out = [(path.replace('chunk.', 'chunk.blank.', 1) if blank.match(path) else path, p, q) + for path, p, q in out] + # Before its light step a chunk saves no light; Horizon lights what it finishes. + elif promoted and va.get('Status') not in LIT_STATUSES: + unlit = re.compile(r'^chunk\.sections\.(SkyLight|BlockLight)\[') + out = [(path.replace('chunk.', 'chunk.unlit.', 1) if unlit.match(path) else path, p, q) + for path, p, q in out] for path, p, q in out: report.append((f'{path} @{key}', p, q)) From 55891f9c95d083afd6247f97bd7ea9fd3b6b5864 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 19:34:46 +0200 Subject: [PATCH 023/124] Wait for the server's block in the agent's place, try hidden spots On a client of a server (single player's server thread, a LAN guest) the placed block comes with the server's answer: `place` waits up to a second for it before reporting no change. agent_control_e2e tries the next free spot when the tree's trunk above the broken log hides a spot's faces. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 9821bb7ef54112200c720c2ca91f10187b4ea693) --- crates/horizon-client/src/game/agent.rs | 12 +++++++++++- tools/agent_control_e2e.py | 23 +++++++++++++++-------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/crates/horizon-client/src/game/agent.rs b/crates/horizon-client/src/game/agent.rs index 5d7d294dd..9a5cd6a58 100644 --- a/crates/horizon-client/src/game/agent.rs +++ b/crates/horizon-client/src/game/agent.rs @@ -1840,11 +1840,21 @@ impl Game<'_> { if self.ticks < *report_tick && !timed_out { return None; } - let after = pos.map(|p| self.block_json(p)); let changed = match (*pos, *before) { (Some(p), Some(b)) => self.world.block(p[0], p[1], p[2]) != b, _ => false, }; + // A client of a server (single player's server thread, a LAN + // guest) sees the block once the server's answer comes. + if *place + && !changed + && self.remote.is_some() + && self.ticks < *report_tick + 20 + && !timed_out + { + return None; + } + let after = pos.map(|p| self.block_json(p)); let used = delta(inventory, &self.inventory_counts()); let mut v = json!({ "ok": !*place || changed, diff --git a/tools/agent_control_e2e.py b/tools/agent_control_e2e.py index 4edc7ae55..a3690f9d7 100644 --- a/tools/agent_control_e2e.py +++ b/tools/agent_control_e2e.py @@ -253,20 +253,27 @@ def scenario(d, out): # 5. Place a plank beside the player and read it back. p = d('state')['player']['block_pos'] - target = None + hotbar = d('inventory')['hotbar'] + if not any(s.get('item') == planks_id for s in hotbar): + raise Failed(f'{planks_id} did not land in the hotbar: {hotbar}') + target = placed = None for dx, dz in [(2, 0), (-2, 0), (0, 2), (0, -2), (2, 1), (1, 2), (-2, -1), (-1, -2)]: at = (p[0] + dx, p[1], p[2] + dz) here = d('block', x=at[0], y=at[1], z=at[2])['block'] below = d('block', x=at[0], y=at[1] - 1, z=at[2])['block'] - if here in ('minecraft:air', 'minecraft:short_grass') and below not in ('minecraft:air', 'minecraft:water'): - target = at - break + if here not in ('minecraft:air', 'minecraft:short_grass') or below in ('minecraft:air', 'minecraft:water'): + continue + # The tree's trunk above the broken log can hide a spot's faces: try the next one. + try: + placed = d('place', x=at[0], y=at[1], z=at[2], item=planks_id) + except Failed as e: + if 'no visible face' in str(e): + continue + raise + target = at + break if target is None: raise Failed(f'no free spot beside {p} to place a block') - hotbar = d('inventory')['hotbar'] - if not any(s.get('item') == planks_id for s in hotbar): - raise Failed(f'{planks_id} did not land in the hotbar: {hotbar}') - placed = d('place', x=target[0], y=target[1], z=target[2], item=planks_id) assert placed['changed'], placed back = d('block', x=target[0], y=target[1], z=target[2]) assert back['block'] == planks_id, back From 8b75ac3cdbb2a1432464a416df26380a2fc6367f Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Wed, 21 Oct 2026 17:31:43 +0200 Subject: [PATCH 024/124] lan_e2e and agent_control_e2e on the server thread From b9f3821f (fix/server-thread-default): the host window draws what it was sent on the server thread, and agent_control_e2e waits two ticks where the server answers a click. Co-Authored-By: Claude Opus 5.5 --- tools/agent_control_e2e.py | 4 ++++ tools/lan_e2e.py | 26 ++++++++++++++++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/tools/agent_control_e2e.py b/tools/agent_control_e2e.py index a3690f9d7..a01c25ef9 100644 --- a/tools/agent_control_e2e.py +++ b/tools/agent_control_e2e.py @@ -244,10 +244,12 @@ def scenario(d, out): d('click_slot', slot=log_slot) # pick up the logs d('click_slot', slot=100, right=True) # put one in the crafting grid d('click_slot', slot=log_slot) # put the rest back + d('wait', ticks=2) # the server crafts the result (single player's server thread) result = next(s for s in d('screen')['slots'] if s['slot'] == 200) assert result.get('item') == planks_id and result['count'] == 4, result d('click_slot', slot=200, shift=True) # craft into the inventory d('close') + d('wait', ticks=2) inventory = d('inventory') assert inventory['counts'].get(planks_id, 0) >= 4, inventory @@ -329,6 +331,7 @@ def ui_scenario(d, out): # 3. The inventory's slots are widgets too, with their stacks. d('give', item='oak_log', count=2) + d('wait', ticks=2) # single player's server thread gives them d('open_inventory') tree = widgets(d) log = next(w for w in tree.values() if w['kind'] == 'slot' and (w.get('stack') or {}).get('item') == 'minecraft:oak_log') @@ -336,6 +339,7 @@ def ui_scenario(d, out): d('click', id=log['id']) d('click', id='slot-100', right=True) d('click', id=log['id']) + d('wait', ticks=2) assert widgets(d)['slot-200']['stack']['item'] == 'minecraft:oak_planks' d('close') shot = d('screenshot', path=str(out / f'{d.name}-ui.png')) diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 70b44407b..40ab181e1 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -168,6 +168,11 @@ def remote(guest): return guest('status')['server'] +def dead(guest): + """Whether the guest is dead: its death screen (the last health it was sent can stay above 0).""" + return guest('state').get('screen') == 'dead' or remote(guest)['health'] <= 0 + + def back_to_game(game): """Close whatever screen a failed check left open, without opening the pause menu.""" try: @@ -227,9 +232,15 @@ def check_armour(host, guest, out): face_each_other(host, guest) host.shot(out, 'armour') guest.shot(out, 'armour') - seen = lan_guest(host).get('model_armor') - assert seen == [None, 'minecraft:diamond_chestplate', 'minecraft:diamond_leggings', None], \ - f'the host draws the guest in {seen}' + if SERVER_THREAD: + # The host's window is a client of its server thread: what it draws is what it was sent. + seen = [e for e in remote(host).get('player_equipment', [])] + assert all(any(item in json.dumps(e) for e in seen) for item in + ('diamond_chestplate', 'diamond_leggings')), f'the host draws the guest in {seen}' + else: + seen = lan_guest(host).get('model_armor') + assert seen == [None, 'minecraft:diamond_chestplate', 'minecraft:diamond_leggings', None], \ + f'the host draws the guest in {seen}' host_gear = [e for e in remote(guest).get('player_equipment', [])] assert any('golden_chestplate' in json.dumps(e) for e in host_gear), f'the guest sees the host in {host_gear}' return 'each in their own' @@ -559,7 +570,7 @@ def check_hazards(host, guest, out): x, y, z = hx + 3, hy, hz for name, fill, ticks in (('lava', 'minecraft:lava', 60), ('wall', 'minecraft:stone', 60), ('drowning', 'minecraft:water', 500), ('void', None, 60)): - if remote(guest)['health'] <= 0: + if dead(guest): guest.raw('respawn') guest.wait(40) # A stone box around the guest holds the lava and the water in. @@ -582,7 +593,7 @@ def check_hazards(host, guest, out): host('command', command=f'fill {x - 1} {y - 1} {z - 1} {x + 1} {y + 2} {z + 1} minecraft:air') host('command', command=f'fill {x - 1} {y - 1} {z - 1} {x + 1} {y - 1} {z + 1} minecraft:stone') guest.wait(20) - if remote(guest)['health'] <= 0: + if dead(guest): guest.raw('respawn') guest.wait(40) # [food, saturation, exhaustion]: 8 s of sprint-jumping is about 2 of exhaustion; every 4 @@ -595,6 +606,9 @@ def check_hazards(host, guest, out): def check_pvp(host, guest, out): + if dead(guest): + guest.raw('respawn') + guest.wait(40) for g in (host, guest): g('command', command='gamemode survival') g('command', command='item replace entity @s weapon.mainhand with minecraft:iron_sword') @@ -1104,7 +1118,7 @@ def check_walls(host, guest, out): for g in (host, guest): back_to_game(g) # The trial spawner's zombies may have killed the guest. - if guest('state').get('screen') == 'dead' or remote(guest)['health'] <= 0: + if dead(guest): guest.raw('respawn') guest.wait(40) face_each_other(host, guest) From 05e169f4e3cb32aa655b24eb5f1c9ea298af4ebd Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Wed, 21 Oct 2026 20:20:31 +0200 Subject: [PATCH 025/124] agent_control_e2e pick: wait for the server to clear the cursor Step 2 clones a stack in the creative tab and closes the screen. The clone lived only on the client: ItemPickerMenu's carried stack is the inventoryMenu's, and AbstractContainerMenu.removed clears the carried stack only for a ServerPlayer, so vanilla's client still holds it when the survival inventory opens. With a stack in hand the middle button's release sends CLONE (AbstractContainerScreen.mouseReleased); the server finds the carried stack it was told about wrong and sends the empty one (broadcastChanges). On single player's server thread that answer takes a round trip, as in vanilla, so step 3 now waits up to 20 ticks for it. The in-frame world puts the clone away when the menu closes. Co-Authored-By: Claude Opus 5.5 --- tools/agent_control_e2e.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tools/agent_control_e2e.py b/tools/agent_control_e2e.py index a01c25ef9..6464425fa 100644 --- a/tools/agent_control_e2e.py +++ b/tools/agent_control_e2e.py @@ -500,6 +500,15 @@ def pick_scenario(d, out): d('give', item='oak_log', count=2) assert d('open_inventory')['screen'] == 'inventory' d('click', id='slot-0', middle=True) + # The clone of step 2 lived only on this client: ItemPickerMenu's carried stack is the + # inventoryMenu's, and AbstractContainerMenu.removed clears it only for a ServerPlayer. With a + # stack in hand, mouseReleased sends CLONE, the server finds the carried stack it was told about + # wrong and sends the empty one back (broadcastChanges). On the server thread that takes a + # round trip, as in vanilla; in the frame, closing the menu already put the clone away. + for _ in range(20): + if carried() is None: + break + d('wait', ticks=1) assert carried() is None, carried() assert widgets(d)['slot-0']['stack'] == {'item': 'minecraft:oak_log', 'count': 2} d('close') From b7453b129bda2b6531a4f7816ca52f34b412c235 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Wed, 21 Oct 2026 20:55:30 +0200 Subject: [PATCH 026/124] agent_control_e2e survival: wait for the log's drop or its pickup On single player's server thread the broken log's drop and its pickup both come from the server, a few ticks after `break` answers: the pickup could land before the scenario looked for the drop, which it then never found (StopIteration). Step 3 now waits up to 20 ticks for the logs in the inventory or the drop to walk to. Co-Authored-By: Claude Opus 5.5 --- tools/agent_control_e2e.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tools/agent_control_e2e.py b/tools/agent_control_e2e.py index 6464425fa..977d2e292 100644 --- a/tools/agent_control_e2e.py +++ b/tools/agent_control_e2e.py @@ -227,11 +227,18 @@ def scenario(d, out): raise Failed('no reachable tree near spawn') assert broke['block_before'].startswith(log_id), broke assert broke['drops'].get(log_id, 0) >= 1, broke - if broke['picked_up'].get(log_id, 0) < 1: + # On a client of a server (single player's server thread) the drop and its pickup both come from + # the server a few ticks later: wait for the logs in the inventory, or walk to the drop. + for _ in range(20): + if broke['picked_up'].get(log_id, 0) >= 1 or d('inventory')['counts'].get(log_id, 0) >= 1: + break items = d('entities', radius=8, type='item')['entities'] - drop = next(e for e in items if e['item']['item'] == log_id) - d('goto', x=drop['pos'][0], y=drop['pos'][1], z=drop['pos'][2], near=1.0, timeout=20) # pickup reaches a block; the drop may lie under the trunk - d('wait', ticks=10) + drop = next((e for e in items if e['item']['item'] == log_id), None) + if drop is not None: + d('goto', x=drop['pos'][0], y=drop['pos'][1], z=drop['pos'][2], near=1.0, timeout=20) # pickup reaches a block; the drop may lie under the trunk + d('wait', ticks=10) + break + d('wait', ticks=1) inventory = d('inventory') assert inventory['counts'].get(log_id, 0) >= 1, inventory From f59f6e3e84483ea6a7ef2c76062176260b8b2bf0 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 05:16:44 +0200 Subject: [PATCH 027/124] lan_e2e portals: take the portals away afterwards The guest came home into its Nether portal's frame, with an obsidian ceiling three blocks up, and the host onto the End portal it had set at spawn; both stayed there for the rest of the run. Under that ceiling a double jump rises about 1.2 blocks at most, and whether the flight check saw it depended on timing: `flight` failed in full --server-thread runs (portals runs only there) and passed alone. The check now clears the frame and the End portal in the overworld, and puts the guest back where it started, also when it fails. Co-Authored-By: Claude Opus 5.5 --- tools/lan_e2e.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 61697550b..40c081a5a 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -421,6 +421,20 @@ def check_portals(host, guest, out): if not SERVER_THREAD: return 'skipped (needs --server-thread)' x, y, z = (int(v // 1) for v in pos_of(guest)) + hx, hy, hz = (int(v // 1) for v in pos_of(host)) + try: + return portals(host, guest, out, x, y, z, hx, hy, hz) + finally: + # The guest comes home into its portal's frame (an obsidian ceiling three blocks up) and the + # host onto its End portal at spawn: take both away so the next checks start in the open. + home = 'execute in minecraft:overworld run' + guest('command', command=f'{home} fill {x - 1} {y} {z + 3} {x + 2} {y + 3} {z + 3} minecraft:air') + host('command', command=f'{home} setblock {hx} {hy} {hz} minecraft:air') + guest('command', command=f'{home} tp @s {x + 0.5} {y} {z + 0.5}') + guest.wait(20) + + +def portals(host, guest, out, x, y, z, hx, hy, hz): nether_portal(guest, x, y, z + 3) guest.wait(5) guest('command', command=f'tp @s {x + 0.5} {y} {z + 3.5}') @@ -435,7 +449,6 @@ def check_portals(host, guest, out): below = guest.raw('block', arrived[0], arrived[1] - 1, arrived[2]) assert below.get('loaded') and below.get('block') != 'minecraft:air', f'under the guest in the Nether: {below}' # The host takes an End portal while the guest is in the Nether: three levels at once. - hx, hy, hz = (int(v // 1) for v in pos_of(host)) host('command', command=f'setblock {hx} {hy} {hz} minecraft:end_portal') end = wait_dimension(host, 'minecraft:the_end', 600) host.wait(40) From 832f879b27f2fd9e4db913db51c6f836bf928d3f Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 03:52:00 +0200 Subject: [PATCH 028/124] Dynamic lights: held, dropped and burning light sources (Horizon Extras) A new Horizon Extras option, Lighting > Dynamic lights (`dynamicLights`, off by default): a torch, lantern or other light-emitting item held by a player or mob, dropped as an item, or any entity on fire lights the blocks, entities and hand around it, as LambDynamicLights and OptiFine do. Client-only and at render time: the light engine, chunk meshes and saves never see it. Each frame the game picks the 16 sources nearest the camera (dynamic_light::select) and passes them in Globals; terrain.wgsl and entity.wgsl raise each vertex's block light to `level - distance` when that is brighter. Off, the count in globals.time.z is 0, nothing is gathered and the shaders' loop runs zero times, so no pipeline is rebuilt either way. An item glows like its block's default state (torch 14, lantern and glowstone 15, lit redstone torch 7); a lava bucket 15; burning entities 15. Fixes #814 Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/dynamic_light.rs | 133 ++++++++++++++++++ crates/horizon-client/src/entity.wgsl | 18 ++- crates/horizon-client/src/extras.rs | 25 +++- crates/horizon-client/src/game.rs | 14 ++ .../horizon-client/src/game/dynamic_lights.rs | 67 +++++++++ crates/horizon-client/src/gfx.rs | 16 ++- crates/horizon-client/src/lib.rs | 1 + crates/horizon-client/src/terrain.wgsl | 20 ++- docs/extras.md | 18 ++- 9 files changed, 301 insertions(+), 11 deletions(-) create mode 100644 crates/horizon-client/src/dynamic_light.rs create mode 100644 crates/horizon-client/src/game/dynamic_lights.rs diff --git a/crates/horizon-client/src/dynamic_light.rs b/crates/horizon-client/src/dynamic_light.rs new file mode 100644 index 000000000..4502b3356 --- /dev/null +++ b/crates/horizon-client/src/dynamic_light.rs @@ -0,0 +1,133 @@ +//! Horizon Extras' dynamic lights (docs/extras.md): a torch, lantern or other light source held by +//! a player or mob, dropped as an item, or an entity on fire lights the blocks and entities +//! around it. Not in vanilla; off by default. +//! +//! Client-only and at render time: the light engine, chunk meshes and saves never see it. Each +//! frame the game picks the [`MAX`] sources nearest the camera; terrain.wgsl and entity.wgsl +//! raise a vertex's block light to `level - distance` (one level per block, as block light falls +//! off) when that is brighter. Off, the list is empty and the shaders skip the loop. +use crate::blocks::BlockFacts; +use glam::DVec3; +use horizon_world::registry::BlockRegistry; +use std::collections::HashMap; + +/// Lights the shaders read: `Globals.dynamic_lights` in gfx.rs, terrain.wgsl and entity.wgsl. +pub const MAX: usize = 16; +/// What an entity on fire gives off, as a lit campfire or fire block does. +pub const BURNING: u8 = 15; +/// Sources farther than this from the camera light nothing it can see well. +const REACH: f64 = 96.0; + +/// How brightly each item glows when held or dropped. +#[derive(Debug, Default)] +pub struct Luminance(HashMap); + +impl Luminance { + /// A block item glows like its block's default state (`getLightEmission`): torches 14, + /// lanterns and glowstone 15, a lit redstone torch 7; a furnace's item, unlit, not at all. + pub fn new(facts: &BlockFacts, registry: &BlockRegistry) -> Self { + let mut map = HashMap::new(); + for (id, state) in facts.states.iter().enumerate() { + if state.emission == 0 || *state.item != *state.name { + continue; + } + if registry.default_state(&state.name).map(|d| d.0 as usize) == Some(id) { + map.insert(state.item.to_string(), state.emission); + } + } + // A bucket of lava glows as lava does. + map.insert("minecraft:lava_bucket".into(), 15); + Self(map) + } + + pub fn of(&self, item: &str) -> u8 { + self.0.get(item).copied().unwrap_or(0) + } + + #[cfg(test)] + fn from_pairs(pairs: &[(&str, u8)]) -> Self { + Self(pairs.iter().map(|(k, v)| (k.to_string(), *v)).collect()) + } +} + +/// One light this frame: where it is (world space) and its level, 1 to 15. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Source { + pub at: DVec3, + pub level: u8, +} + +/// The [`MAX`] sources nearest `camera` within reach, as the shaders read them: camera-relative +/// position and level. Overwrites `out`. +pub fn select(sources: &[Source], camera: DVec3, out: &mut Vec<[f32; 4]>) { + out.clear(); + let key = |s: &Source| s.at.distance_squared(camera); + let mut near: Vec<&Source> = sources + .iter() + .filter(|s| s.level > 0 && key(s) <= REACH * REACH) + .collect(); + near.sort_by(|a, b| key(a).total_cmp(&key(b))); + out.extend(near.into_iter().take(MAX).map(|s| { + let p = (s.at - camera).as_vec3(); + [p.x, p.y, p.z, f32::from(s.level.min(15))] + })); +} + +/// The block light a vertex at `pos` (camera-relative) gets from `lights`, 0 to 15: the WGSL +/// `dynamic_block_light` in Rust, for tests. +pub fn level_at(pos: [f32; 3], lights: &[[f32; 4]]) -> f32 { + lights.iter().fold(0.0f32, |level, l| { + let d = + ((pos[0] - l[0]).powi(2) + (pos[1] - l[1]).powi(2) + (pos[2] - l[2]).powi(2)).sqrt(); + level.max(l[3] - d) + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn nearest_sources_win() { + let camera = DVec3::new(100.0, 64.0, 100.0); + let mut sources: Vec = (0..40) + .map(|i| Source { + at: camera + DVec3::new(f64::from(i), 0.0, 0.0), + level: 14, + }) + .collect(); + sources.push(Source { + at: camera + DVec3::new(0.5, 0.0, 0.0), + level: 0, + }); + sources.push(Source { + at: camera + DVec3::new(0.0, 0.0, 200.0), + level: 15, + }); + let mut out = Vec::new(); + select(&sources, camera, &mut out); + assert_eq!(out.len(), MAX); + assert_eq!(out[0], [0.0, 0.0, 0.0, 14.0]); + assert_eq!(out[MAX - 1][0], (MAX - 1) as f32); + select(&[], camera, &mut out); + assert!(out.is_empty()); + } + + #[test] + fn light_falls_off_a_level_per_block() { + let torch = [[0.0, 0.0, 0.0, 14.0]]; + assert_eq!(level_at([0.0, 0.0, 0.0], &torch), 14.0); + assert_eq!(level_at([3.0, 4.0, 0.0], &torch), 9.0); + assert_eq!(level_at([20.0, 0.0, 0.0], &torch), 0.0); + let two = [[0.0, 0.0, 0.0, 14.0], [10.0, 0.0, 0.0, 15.0]]; + assert_eq!(level_at([8.0, 0.0, 0.0], &two), 13.0); + assert_eq!(level_at([0.0; 3], &[]), 0.0); + } + + #[test] + fn item_luminance() { + let l = Luminance::from_pairs(&[("minecraft:torch", 14)]); + assert_eq!(l.of("minecraft:torch"), 14); + assert_eq!(l.of("minecraft:stone"), 0); + } +} diff --git a/crates/horizon-client/src/entity.wgsl b/crates/horizon-client/src/entity.wgsl index 42113df78..2c6af26f9 100644 --- a/crates/horizon-client/src/entity.wgsl +++ b/crates/horizon-client/src/entity.wgsl @@ -14,11 +14,13 @@ struct Globals { // Atlas rects (u0, v0, width, height) of end_sky.png and end_portal.png. end_portal_sprites: array, 2>, // x: vanilla's GameTime ((game time % 24000 + partial tick) / 24000) - // y: Nether cardinal lighting. + // y: Nether cardinal lighting, z: how many dynamic_lights are set. time: vec4, crumbling_sprite: vec4, // Selection outline parameters (read by terrain.wgsl). outline_params: vec4, + // Horizon Extras' dynamic lights: camera-relative position and level (dynamic_light.rs). + dynamic_lights: array, 16>, } @group(0) @binding(0) var globals: Globals; @group(0) @binding(1) var atlas: texture_2d; @@ -58,13 +60,25 @@ fn overlay_texel(overlay: u32) -> vec4 { if overlay >= 16u && overlay < 32u { return vec4(1.0, 1.0, 1.0, WHITE_OVERLAY_ALPHA[overlay - 16u]); } return vec4(1.0); } +// Horizon Extras' dynamic lights (dynamic_light.rs): the block light, in the vertex's units +// (16 per level), a vertex at camera-relative `pos` gets from the frame's light sources, one +// level less per block. time.z of them are set: none when the extra is off, so no loop runs. +fn dynamic_block_light(pos: vec3) -> f32 { + let count = min(u32(globals.time.z), 16u); + var level = 0.0; + for (var i = 0u; i < count; i++) { + let l = globals.dynamic_lights[i]; + level = max(level, l.w - distance(pos, l.xyz)); + } + return level * 16.0; +} fn entity_vertex(position: vec3, uv: vec2, overlay: u32, color: vec4, light: vec4, normal: vec4) -> Out { var out: Out; out.clip = globals.view_proj * vec4(position, 1.0); out.proj = vec4(out.clip.xy * 0.5 + out.clip.w * 0.5, out.clip.zw); out.uv = uv; - let l = vec2(f32(light.y), f32(light.x)); + let l = vec2(max(f32(light.y), dynamic_block_light(position)), f32(light.x)); let lm = textureSampleLevel(lightmap, lightmap_sampler, clamp(l / 256.0 + 0.5 / 16.0, vec2(0.5 / 16.0), vec2(15.5 / 16.0)), 0.0); // Celestial bodies, clouds, beacon beams, End portals and breaking cracks are unlit. diff --git a/crates/horizon-client/src/extras.rs b/crates/horizon-client/src/extras.rs index adbc6e23a..c7be78019 100644 --- a/crates/horizon-client/src/extras.rs +++ b/crates/horizon-client/src/extras.rs @@ -69,6 +69,8 @@ impl Corner { #[derive(Clone, Debug, PartialEq)] pub struct Extras { pub fullbright: bool, + /// Held, dropped and burning light sources light their surroundings (`dynamic_light`). + pub dynamic_lights: bool, pub zoom: bool, /// A vanilla key name (`key.keyboard.z`). pub zoom_key: String, @@ -110,6 +112,7 @@ impl Default for Extras { fn default() -> Self { Self { fullbright: false, + dynamic_lights: false, zoom: false, zoom_key: "key.keyboard.z".into(), zoom_level: 4, @@ -158,6 +161,7 @@ impl Extras { }; match key.trim() { "fullbright" => e.fullbright = flag(e.fullbright), + "dynamicLights" => e.dynamic_lights = flag(e.dynamic_lights), "zoom" => e.zoom = flag(e.zoom), "zoomKey" if value.starts_with("key.") => e.zoom_key = value.to_owned(), "zoomLevel" => { @@ -201,8 +205,9 @@ impl Extras { } pub fn serialize(&self) -> String { format!( - "fullbright:{}\nzoom:{}\nzoomKey:{}\nzoomLevel:{}\nzoomSmooth:{}\nzoomSensitivity:{}\nfps:{}\nfpsCorner:{}\nfpsLows:{}\nkeystrokes:{}\nkeystrokesCorner:{}\nkeystrokesCps:{}\nminimap:{}\nminimapCorner:{}\nminimapSize:{}\nminimapShape:{}\nminimapZoom:{}\nminimapRotate:{}\nautoReconnect:{}\nmouseTweaks:{}\nmouseTweaksWheel:{}\nsort:{}\nsortKey:{}\n", + "fullbright:{}\ndynamicLights:{}\nzoom:{}\nzoomKey:{}\nzoomLevel:{}\nzoomSmooth:{}\nzoomSensitivity:{}\nfps:{}\nfpsCorner:{}\nfpsLows:{}\nkeystrokes:{}\nkeystrokesCorner:{}\nkeystrokesCps:{}\nminimap:{}\nminimapCorner:{}\nminimapSize:{}\nminimapShape:{}\nminimapZoom:{}\nminimapRotate:{}\nautoReconnect:{}\nmouseTweaks:{}\nmouseTweaksWheel:{}\nsort:{}\nsortKey:{}\n", bool_text(self.fullbright), + bool_text(self.dynamic_lights), bool_text(self.zoom), self.zoom_key, self.zoom_level, @@ -423,6 +428,7 @@ impl Cps { #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Item { Fullbright, + DynamicLights, Zoom, ZoomKey, ZoomLevel, @@ -452,6 +458,7 @@ pub fn rows() -> Vec> { vec![ Err("Lighting"), Ok(Fullbright), + Ok(DynamicLights), Err("Zoom"), Ok(Zoom), Ok(ZoomKey), @@ -501,6 +508,7 @@ impl Item { pub fn caption(self) -> &'static str { match self { Item::Fullbright => "Fullbright", + Item::DynamicLights => "Dynamic lights", Item::Zoom => "Zoom", Item::ZoomKey => "Zoom key", Item::ZoomLevel => "Starting zoom", @@ -533,7 +541,8 @@ impl Item { KeystrokesCorner | KeystrokesCps => Keystrokes, MouseTweaksWheel => MouseTweaks, SortKey => Sort, - Fullbright | Zoom | Minimap | Fps | Keystrokes | MouseTweaks | Sort | AutoReconnect => { + Fullbright | DynamicLights | Zoom | Minimap | Fps | Keystrokes | MouseTweaks | Sort + | AutoReconnect => { return None; } }) @@ -542,6 +551,9 @@ impl Item { pub fn description(self) -> &'static str { match self { Item::Fullbright => "Show blocks and entities at full light, even in dark areas.", + Item::DynamicLights => { + "Torches and other light sources held, dropped or on fire light the blocks around them." + } Item::Zoom => "Hold the zoom key to zoom; the wheel changes it while held.", Item::ZoomKey => "Click, then press a key. Escape unbinds it.", Item::ZoomLevel => "The zoom the key starts at.", @@ -581,7 +593,7 @@ impl Item { use crate::graphics_style::Cost; match self { Item::Fullbright | Item::Zoom | Item::Fps | Item::Keystrokes => Some(Cost::None), - Item::Minimap => Some(Cost::Low), + Item::Minimap | Item::DynamicLights => Some(Cost::Low), Item::AutoReconnect | Item::MouseTweaks | Item::Sort => Some(Cost::None), _ => None, } @@ -590,6 +602,7 @@ impl Item { pub fn switch(self, e: &Extras) -> Option { Some(match self { Item::Fullbright => e.fullbright, + Item::DynamicLights => e.dynamic_lights, Item::Zoom => e.zoom, Item::ZoomSmooth => e.zoom_smooth, Item::ZoomSensitivity => e.zoom_sensitivity, @@ -632,6 +645,7 @@ impl Item { pub fn cycle(self, e: &mut Extras, back: bool) { match self { Item::Fullbright => e.fullbright = !e.fullbright, + Item::DynamicLights => e.dynamic_lights = !e.dynamic_lights, Item::Zoom => e.zoom = !e.zoom, Item::ZoomSmooth => e.zoom_smooth = !e.zoom_smooth, Item::ZoomSensitivity => e.zoom_sensitivity = !e.zoom_sensitivity, @@ -677,7 +691,7 @@ mod tests { #[test] fn everything_is_off_by_default_and_round_trips() { let d = Extras::default(); - assert!(!d.fullbright); + assert!(!d.fullbright && !d.dynamic_lights); assert!(!d.zoom && !d.fps && !d.keystrokes && !d.minimap && !d.auto_reconnect); assert!(!d.mouse_tweaks && !d.sort); assert_eq!(Extras::parse(""), d); @@ -685,6 +699,9 @@ mod tests { let mut e = d.clone(); Item::Fullbright.cycle(&mut e, false); assert_eq!(Item::Fullbright.switch(&e), Some(true)); + Item::DynamicLights.cycle(&mut e, false); + assert_eq!(Item::DynamicLights.switch(&e), Some(true)); + assert_eq!(Extras::parse(&e.serialize()), e); e.zoom = true; e.zoom_key = "key.keyboard.c".into(); e.zoom_level = 6; diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index a25e8fdeb..092783ca8 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -37,6 +37,7 @@ mod dedicated; mod dispense_fixture; mod dispenser; mod dragon; +mod dynamic_lights; mod enchant_effects; mod end; mod entity_culling; @@ -1177,6 +1178,10 @@ struct Game<'a> { renderer: Option, /// CPU geometry storage retained across frames, including empty/title frames. entity_vertices: Vec, + /// Horizon Extras' dynamic lights this frame (`dynamic_lights.rs`), and how brightly each + /// item glows (built the first time the extra is on). + dynamic_lights: Vec<[f32; 4]>, + luminance: Option, /// Name tags drawn over everything (`RenderPipelines.TEXT_SEE_THROUGH`). see_through_vertices: Vec, started: Instant, @@ -9636,6 +9641,11 @@ impl Game<'_> { .then(|| self.border_draw(camera.to_array(), alpha as f32)) .flatten(); let see_through = std::mem::take(&mut self.see_through_vertices); + if title { + self.dynamic_lights.clear(); + } else { + self.gather_dynamic_lights(camera, alpha); + } let frame = Frame { shader: self.shader_inputs(camera, alpha as f32), nether_lighting: self.dim == Dim::Nether, @@ -9693,6 +9703,7 @@ impl Game<'_> { color: sky.cloud_color, }), entities: &entities, + dynamic_lights: &self.dynamic_lights, additive: &additive, see_through: &see_through, celestial: if self.settings.horizon.sky || title { @@ -11283,6 +11294,8 @@ impl<'a> Game<'a> { window: None, renderer: None, entity_vertices: Vec::new(), + dynamic_lights: Vec::new(), + luminance: None, see_through_vertices: Vec::new(), health_hud: hud_layout::HealthHud::default(), started: now, @@ -12156,6 +12169,7 @@ fn loading_frame(hud: &HudBatch) -> Frame<'_> { hud, nether_lighting: false, entities: &[], + dynamic_lights: &[], clouds: None, additive: &[], see_through: &[], diff --git a/crates/horizon-client/src/game/dynamic_lights.rs b/crates/horizon-client/src/game/dynamic_lights.rs new file mode 100644 index 000000000..cd4223d82 --- /dev/null +++ b/crates/horizon-client/src/game/dynamic_lights.rs @@ -0,0 +1,67 @@ +//! Horizon Extras' dynamic lights (`horizon_client::dynamic_light`): the light sources near the +//! camera this frame. Nothing is gathered while the extra is off. +use super::{Game, shield}; +use glam::DVec3; +use horizon_client::dynamic_light::{self, BURNING, Luminance, Source}; + +impl Game<'_> { + /// Fills `dynamic_lights` for the frame: the player's and mobs' held items and fire, and + /// glowing dropped items, the nearest [`dynamic_light::MAX`] of them. + pub(super) fn gather_dynamic_lights(&mut self, camera: DVec3, alpha: f64) { + let mut out = std::mem::take(&mut self.dynamic_lights); + out.clear(); + if !self.settings.extras.dynamic_lights { + self.dynamic_lights = out; + return; + } + if self.luminance.is_none() { + self.luminance = Some(Luminance::new(self.res.facts(), self.pack.assets.registry)); + } + let lum = self.luminance.as_ref().expect("built above"); + let held = |item: Option<&str>| item.map_or(0, |i| lum.of(i)); + let mut sources = Vec::new(); + if let Some(p) = &self.player + && !p.spectator + { + let feet = self + .previous_position + .lerp(p.position, self.clock.camera_fraction()); + let level = held(self.stack_in(self.selected).map(|s| &*s.item)) + .max(held(self.stack_in(shield::OFFHAND_SLOT).map(|s| &*s.item))) + .max(if p.fire_ticks > 0 { BURNING } else { 0 }); + // About where the hands are. + sources.push(Source { + at: DVec3::new(feet.x, feet.y + 1.2, feet.z), + level, + }); + } + for m in self.mobs.iter().filter(|m| !m.removed) { + let level = held(m.extra.main_hand.as_deref()) + .max(held(m.extra.off_hand.as_deref())) + .max(if m.fire_ticks > 0 { BURNING } else { 0 }); + if level == 0 { + continue; + } + let at = m.previous.lerp(m.position, alpha); + sources.push(Source { + at: DVec3::new(at.x, at.y + f64::from(m.height()) * 0.6, at.z), + level, + }); + } + for item in &self.dropped { + let level = lum + .of(&item.stack.item) + .max(if item.fire_ticks > 0 { BURNING } else { 0 }); + if level == 0 { + continue; + } + let at = item.previous.lerp(item.position, alpha); + sources.push(Source { + at: DVec3::new(at.x, at.y + 0.25, at.z), + level, + }); + } + dynamic_light::select(&sources, camera, &mut out); + self.dynamic_lights = out; + } +} diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 127aaf737..da53c42ed 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -107,13 +107,16 @@ struct Globals { fog_ends: [f32; 4], /// Atlas rects (u0, v0, width, height) of end_sky.png and end_portal.png. end_portal_sprites: [[f32; 4]; 2], - /// x: vanilla's `GameTime` uniform; y: nether cardinal lighting, zw unused. + /// x: vanilla's `GameTime` uniform; y: nether cardinal lighting; z: how many + /// `dynamic_lights` are set; w unused. time: [f32; 4], /// Atlas rect of a destroy stage (entity.wgsl takes only its size: every stage's is the same, /// and each crack carries its own stage's origin). crumbling_sprite: [f32; 4], /// Framebuffer size, selection stroke width, perspective layering scale. outline_params: [f32; 4], + /// Horizon Extras' dynamic lights: camera-relative position and level (`dynamic_light`). + dynamic_lights: [[f32; 4]; crate::dynamic_light::MAX], } pub type SectionKey = (i32, i32, i32); struct SectionGpu { @@ -186,6 +189,8 @@ pub struct Frame<'a> { pub nether_lighting: bool, /// Camera-relative entity geometry textured from the shared atlas. pub entities: &'a [Vertex], + /// Horizon Extras' dynamic lights, nearest first (`dynamic_light::select`); empty when off. + pub dynamic_lights: &'a [[f32; 4]], /// Cloud mesh inputs; geometry and GPU storage are retained by the renderer. pub clouds: Option>, /// Additive translucent geometry after the entities (lightning, dragon rays). @@ -2908,10 +2913,17 @@ impl Renderer { time: [ frame.shader_game_time, f32::from(frame.nether_lighting), - 0.0, + frame.dynamic_lights.len().min(crate::dynamic_light::MAX) as f32, 0.0, ], outline_params: outline_params(self.config.width, self.config.height), + dynamic_lights: { + let mut lights = [[0.0; 4]; crate::dynamic_light::MAX]; + for (to, from) in lights.iter_mut().zip(frame.dynamic_lights) { + *to = *from; + } + lights + }, }; self.uploads .buffer(&mut encoder, &self.globals, 0, bytemuck::bytes_of(&globals)); diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 7a69de4aa..4cf0c5c48 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -51,6 +51,7 @@ pub mod datapack; pub mod debug_chart; pub mod debug_keys; pub mod debug_overlay; +pub mod dynamic_light; pub mod effects; pub mod enchant; pub mod end_crystal; diff --git a/crates/horizon-client/src/terrain.wgsl b/crates/horizon-client/src/terrain.wgsl index be3405b23..c0a2fc6fd 100644 --- a/crates/horizon-client/src/terrain.wgsl +++ b/crates/horizon-client/src/terrain.wgsl @@ -16,11 +16,14 @@ struct Globals { fog_ends: vec4, // Atlas rects (u0, v0, width, height) of end_sky.png and end_portal.png. end_portal_sprites: array, 2>, - // x: vanilla's GameTime ((game time % 24000 + partial tick) / 24000) + // x: vanilla's GameTime ((game time % 24000 + partial tick) / 24000), z: how many + // dynamic_lights are set time: vec4, // Atlas rect of the current destroy stage (read by entity.wgsl). crumbling_sprite: vec4, outline_params: vec4, + // Horizon Extras' dynamic lights: camera-relative position and level (dynamic_light.rs). + dynamic_lights: array, 16>, } @group(0) @binding(0) var globals: Globals; @group(0) @binding(1) var sprites: texture_2d_array; @@ -67,6 +70,18 @@ struct TerrainOut { @location(3) spherical: f32, @location(4) cylindrical: f32, } +// Horizon Extras' dynamic lights (dynamic_light.rs): the block light, in the vertex's units +// (16 per level), a vertex at camera-relative `pos` gets from the frame's light sources, one +// level less per block. time.z of them are set: none when the extra is off, so no loop runs. +fn dynamic_block_light(pos: vec3) -> f32 { + let count = min(u32(globals.time.z), 16u); + var level = 0.0; + for (var i = 0u; i < count; i++) { + let l = globals.dynamic_lights[i]; + level = max(level, l.w - distance(pos, l.xyz)); + } + return level * 16.0; +} fn sample_lightmap(sky: f32, block: f32) -> vec4 { let uv = vec2(block, sky) * 16.0; return textureSampleLevel(lightmap, lightmap_sampler, @@ -105,7 +120,8 @@ fn terrain_vertex(position: vec3, uv: vec2, layer: u32, color: vec4 Date: Thu, 22 Oct 2026 01:57:06 +0200 Subject: [PATCH 029/124] Riptide: run vanilla's auto-spin attack after the launch Releasing a Riptide trident now starts LivingEntity.startAutoSpinAttack(20, 8.0): for 20 ticks the player takes the SPIN_ATTACK pose (0.6x0.6, eye height 0.4), hits the first living entity its swept box touches for the spin's damage and bounces back (velocity x -0.2), and stops on a horizontal collision. The player model spins (setupRotations: rotX(-90 - xRot), rotY(age * -75)), SpinAttackEffectLayer draws the riptide boxes, and the first-person hand uses ItemInHandRenderer's spin transform. LAN guests see the host's spin through living flag bit 4. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/first_person.rs | 8 ++ crates/horizon-client/src/game.rs | 33 ++++++++- crates/horizon-client/src/game/lan.rs | 4 +- crates/horizon-client/src/game/perspective.rs | 44 +++++++++++ crates/horizon-client/src/game/trident.rs | 35 +++++++++ crates/horizon-client/src/mobs.rs | 2 + crates/horizon-client/src/mobs/render.rs | 9 ++- crates/horizon-client/src/motion.rs | 74 +++++++++++++++++++ crates/horizon-client/src/pose.rs | 8 +- 9 files changed, 210 insertions(+), 7 deletions(-) diff --git a/crates/horizon-client/src/first_person.rs b/crates/horizon-client/src/first_person.rs index a25d9f537..463d51437 100644 --- a/crates/horizon-client/src/first_person.rs +++ b/crates/horizon-client/src/first_person.rs @@ -21,6 +21,8 @@ pub enum ItemPose { Trident { ticks_used: f32, }, + /// Riptide's spin (`isAutoSpinAttack`): the item held up like a fork. + SpinAttack, Bow { ticks_used: f32, }, @@ -67,6 +69,12 @@ pub fn item_pose(left: bool, inverse_height: f32, attack: f32, pose: ItemPose) - if matches!(pose, ItemPose::Using) { return m; } + if matches!(pose, ItemPose::SpinAttack) { + return m + * Mat4::from_translation(Vec3::new(invert * -0.4, 0.8, 0.3)) + * Mat4::from_rotation_y((invert * 65.0f32).to_radians()) + * Mat4::from_rotation_z((invert * -85.0f32).to_radians()); + } let charging = match pose { ItemPose::Bow { ticks_used } => { let power = ticks_used / 20.0; diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 2df6aa9b0..36c16e61f 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -896,6 +896,8 @@ struct WorldRest { behaviours: Behaviours, player_arm: Option, player_type: Option>, + /// `SpinAttackEffectModel` (Riptide's spinning boxes). + spin_attack: Option, stations: horizon_client::stations::StationData, } @@ -971,6 +973,9 @@ impl Resources { fn player_type(&self) -> &Option> { &self.rest().player_type } + fn spin_attack(&self) -> &Option { + &self.rest().spin_attack + } fn stations(&self) -> &horizon_client::stations::StationData { &self.rest().stations } @@ -3907,6 +3912,7 @@ impl Game<'_> { } let riding = self.riding.is_some(); let ghast_collisions = self.happy_ghast_player_collisions(); + let spin_targets = self.spin_targets(); let Some(player) = self.player.as_mut() else { // A dedicated server has no local player: the world still runs. if self.options.dedicated.is_some() { @@ -3984,6 +3990,7 @@ impl Game<'_> { .any(|s| immune.is_some_and(|t| t.contains(&*s.item))); // The dimension's FAST_LAVA: the Nether's lava pushes harder. player.fluids.fast_lava = self.dim == Dim::Nether; + player.spin_targets = spin_targets; let grounded = player.ground.on_ground; let before = ( grounded, @@ -4006,6 +4013,7 @@ impl Game<'_> { } } let events = std::mem::take(&mut player.events); + let spin_hit = player.spin_hit.take(); let glider_wear = std::mem::take(&mut player.glider_wear); if glider_wear > 0 && !self.creative @@ -4024,6 +4032,7 @@ impl Game<'_> { self.eye += (camera_eye.unwrap_or(eye_height) - self.eye) * 0.5; let crouching = player.pose == horizon_client::pose::Pose::Crouching; self.tick_avatar(before, riding); + self.end_spin_attack(spin_hit); self.record_combat(&events); for event in events { match event { @@ -6751,7 +6760,13 @@ impl Game<'_> { /// enchantments. pub(super) fn melee_damage(&mut self, target_kind: &str) -> (f32, f32, bool) { let hand_damage = 1.0 + self.attack_item_info().map_or(0.0, |i| i.attack_damage); - let damage = hand_damage + self.effect_attack_bonus(); + // Riptide's spin replaces the attack damage attribute. + let spin = self.player.as_ref().map_or(0.0, |p| p.auto_spin_attack_dmg); + let damage = if spin > 0.0 { + spin + } else { + hand_damage + self.effect_attack_bonus() + }; let scale = self.attack_strength_scale(); let mut amount = damage * (0.2 + scale * scale * 0.8); // Item.getAttackDamageBonus (a mace's smash), before the critical hit. @@ -6879,6 +6894,14 @@ impl Game<'_> { return false; } } + self.attack_mob(index); + // Minecraft.startAttack swings after the attack. + self.swing_arm(); + true + } + /// `Player.attack` on a mob: the damage, knockback, sweep, particles and + /// sounds. + pub(super) fn attack_mob(&mut self, index: usize) { let target_kind = self.mobs[index].kind.id.clone(); let target_kind = format!("minecraft:{}", target_kind.trim_start_matches("minecraft:")); let (amount, scale, crit) = self.melee_damage(&target_kind); @@ -6896,7 +6919,6 @@ impl Game<'_> { self.audio .play("minecraft:entity.player.attack.knockback", from, 1.0, 1.0); } - self.swing_arm(); // LivingEntity.setLastHurtMob (tamed wolves join in). self.combat.last_hurt_mob = Some((self.mobs[index].id, self.world.game_time)); let old_health = self.mobs[index].health; @@ -6989,7 +7011,6 @@ impl Game<'_> { self.audio .play("minecraft:entity.player.attack.nodamage", from, 1.0, 1.0); } - true } /// ServerExplosion: rays from a 16^3 grid lose (resistance + 0.3) * 0.3 per step. /// A mob explosion (`Level.ExplosionInteraction.MOB`: drop decay with the @@ -8489,6 +8510,10 @@ impl Game<'_> { } let shown = self.hands_to_render(); let used_hand = self.used_hand(); + let spinning = self + .player + .as_ref() + .is_some_and(|p| p.auto_spin_attack_ticks > 0); for (i, (hand, item)) in Hand::BOTH .into_iter() .zip(self.visible_hand_items()) @@ -8608,6 +8633,7 @@ impl Game<'_> { ItemPose::Using } } + _ if spinning => ItemPose::SpinAttack, _ => ItemPose::Swing, }; let m = to_world @@ -12052,6 +12078,7 @@ fn load_world_rest( player_type: perspective::player_type(root), player_arm: horizon_client::mobs::load_model(root, "minecraft:player#main") .and_then(|m| m.children.into_iter().find(|c| c.name == "right_arm")), + spin_attack: horizon_client::mobs::load_model(root, "minecraft:spin_attack#main"), }) } diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index f9ea1262b..8e641b61f 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -740,7 +740,9 @@ impl Game<'_> { equipment, health: p.health.health(), attributes: horizon_client::lan::attributes::syncable(&p.attributes), - living_flags: living_flags(self.using), + // Bit 4: Riptide's spin (LivingEntity.isAutoSpinAttack). + living_flags: living_flags(self.using) + | if p.auto_spin_attack_ticks > 0 { 4 } else { 0 }, bed: self.sleeping, }) } diff --git a/crates/horizon-client/src/game/perspective.rs b/crates/horizon-client/src/game/perspective.rs index ec46120dd..bd2721648 100644 --- a/crates/horizon-client/src/game/perspective.rs +++ b/crates/horizon-client/src/game/perspective.rs @@ -312,6 +312,7 @@ impl Game<'_> { let crouching = p.pose == Pose::Crouching; let (position, flying) = (p.position, p.flying || p.fall_flying); let (fall_flying, fly_ticks, velocity) = (p.fall_flying, p.fall_fly_ticks, p.velocity); + let spinning = p.auto_spin_attack_ticks > 0; let using = self.using.map(|(_, ticks)| ticks as i32 + 1).unwrap_or(0); let using_off_hand = self .using @@ -397,6 +398,7 @@ impl Game<'_> { m.velocity = velocity; m.extra.fall_flying = fall_flying; m.extra.fall_fly_ticks = fly_ticks; + m.extra.auto_spin_attack = spinning; m.extra.main_hand_left = self.settings.main_hand_left; m.extra.using_item = using; m.extra.using_off_hand = using_off_hand; @@ -451,6 +453,7 @@ impl Game<'_> { out, ); self.player_elytra_geometry(m, camera, alpha as f32, packed, out); + self.spin_attack_geometry(m, camera, alpha as f32, packed, out); self.held_item_geometry(m, camera, alpha, packed, true, out); } /// `extractEntityInInventoryFollowsMouse`: the player in the box @@ -592,6 +595,47 @@ impl Game<'_> { out, ); } + /// `SpinAttackEffectLayer`: Riptide's two boxes turning around a spinning + /// player (`SpinAttackEffectModel.setupAnim`). + fn spin_attack_geometry( + &self, + m: &Mob, + camera: glam::DVec3, + partial: f32, + light: u32, + out: &mut Vec, + ) { + if !m.extra.auto_spin_attack { + return; + } + let (Some(model), Some(rect)) = ( + self.res.spin_attack().as_ref(), + self.gui.atlas.sprites.get("entity/trident/trident_riptide"), + ) else { + return; + }; + let age = m.age as f32 + partial; + horizon_client::mobs::render_model( + model, + horizon_client::mobs::render::base_transform(m, partial, camera), + &|part| { + let mut p = part.pose; + if let Some(i) = part + .name + .strip_prefix("box") + .and_then(|i| i.parse::().ok()) + { + let angle = age * -(45.0 + f32::from(i + 1) * 5.0); + p[4] = wrap(angle).to_radians(); + } + p + }, + [rect.u0, rect.v0, rect.u1, rect.v1], + light, + 0, + out, + ); + } } fn player_preview_quads( diff --git a/crates/horizon-client/src/game/trident.rs b/crates/horizon-client/src/game/trident.rs index b24f4540e..c31094596 100644 --- a/crates/horizon-client/src/game/trident.rs +++ b/crates/horizon-client/src/game/trident.rs @@ -122,6 +122,38 @@ impl Game<'_> { let sky = self.world.light(x, y + 1, z) >> 4 == 15; water || (self.world.weather.raining && sky) } + /// The living entities a spinning player can touch this tick, for + /// `LivingEntity.checkAutoSpinAttack` (none when not spinning). + pub(super) fn spin_targets(&self) -> Vec<(u32, Aabb)> { + let Some(p) = self + .player + .as_ref() + .filter(|p| p.auto_spin_attack_ticks > 0) + else { + return Vec::new(); + }; + // A spin moves a few blocks a tick at most. + let near = p.bounds().inflate(Vec3::new(16.0, 16.0, 16.0)); + self.mobs + .iter() + .filter(|m| m.alive() && m.bounds().intersects(near)) + .map(|m| (m.id, m.bounds())) + .collect() + } + /// `Player.doAutoAttackOnTouch`: the touched entity takes `Player.attack` + /// with the spin's damage; once the spin is over its damage is cleared. + pub(super) fn end_spin_attack(&mut self, hit: Option) { + if let Some(id) = hit + && let Some(index) = self.mobs.iter().position(|m| m.id == id) + { + self.attack_mob(index); + } + if let Some(p) = self.player.as_mut() + && p.auto_spin_attack_ticks <= 0 + { + p.auto_spin_attack_dmg = 0.0; + } + } /// `TridentItem.releaseUsing`. pub(super) fn release_trident(&mut self, ticks: u32) { if ticks < 10 { @@ -152,6 +184,9 @@ impl Game<'_> { p.velocity.y + d[1] * f64::from(strength), p.velocity.z + d[2] * f64::from(strength), ); + // Player.startAutoSpinAttack(20, 8, stack). + p.auto_spin_attack_ticks = 20; + p.auto_spin_attack_dmg = 8.0; if p.ground.on_ground { p.position.y += 1.2; } diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 28bcadeed..e87032f07 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -690,6 +690,8 @@ pub struct Extra { pub blocking: bool, pub fall_flying: bool, pub fall_fly_ticks: i32, + /// `LivingEntity.isAutoSpinAttack` (Riptide). + pub auto_spin_attack: bool, pub main_hand_left: bool, pub elytra: ElytraAnimation, pub swim: SwimAnimation, diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs index 99548ec3f..4248f22c6 100644 --- a/crates/horizon-client/src/mobs/render.rs +++ b/crates/horizon-client/src/mobs/render.rs @@ -771,6 +771,11 @@ pub fn base_transform_at(m: &Mob, alpha: f32, origin: glam::Mat4) -> glam::Mat4 .sqrt() .min(1.0); base *= glam::Mat4::from_rotation_z(f * flip_degrees(m).to_radians()); + } else if m.extra.auto_spin_attack { + // Riptide: lying along the look direction, spinning 75 degrees a tick. + let pitch = m.previous_head_pitch + (m.head_pitch - m.previous_head_pitch) * alpha; + base *= glam::Mat4::from_rotation_x((-90.0 - pitch).to_radians()) + * glam::Mat4::from_rotation_y(((m.age as f32 + alpha) * -75.0).to_radians()); } else if matches!(m.custom_name.as_deref(), Some("Dinnerbone" | "Grumm")) { // isEntityUpsideDown base *= glam::Mat4::from_translation(glam::Vec3::new(0.0, m.height() + 0.1, 0.0)) @@ -781,7 +786,9 @@ pub fn base_transform_at(m: &Mob, alpha: f32, origin: glam::Mat4) -> glam::Mat4 let ticks = m.extra.fall_fly_ticks as f32 + alpha; let scale = (ticks * ticks / 100.0).min(1.0); let pitch = m.previous_head_pitch + (m.head_pitch - m.previous_head_pitch) * alpha; - base *= glam::Mat4::from_rotation_x((scale * (-90.0 - pitch)).to_radians()); + if !m.extra.auto_spin_attack { + base *= glam::Mat4::from_rotation_x((scale * (-90.0 - pitch)).to_radians()); + } let yaw = m.previous_head_yaw + wrap(m.head_yaw - m.previous_head_yaw) * alpha; let (look_x, look_z) = (-(yaw.to_radians()).msin(), (yaw.to_radians()).mcos()); let v = m.velocity; diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index 7bdaea785..192e900a1 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -247,6 +247,14 @@ pub struct PlayerMotion { pub glide_ending: bool, /// Glider durability to take this tick (updateFallFlying). pub glider_wear: u32, + /// `LivingEntity.autoSpinAttackTicks` and `autoSpinAttackDmg`: Riptide's + /// spin. The damage stays until the game has dealt the hit. + pub auto_spin_attack_ticks: i32, + pub auto_spin_attack_dmg: f32, + /// The living entities the game put near a spinning player (id, box). + pub spin_targets: Vec<(u32, Aabb)>, + /// The entity the spin touched this tick (`doAutoAttackOnTouch`). + pub spin_hit: Option, pub sprinting: bool, pub sprint_input: crate::sprint::SprintState, pub shift_key_down: bool, @@ -409,6 +417,10 @@ impl PlayerMotion { fall_fly_ticks: 0, glide_ending: false, glider_wear: 0, + auto_spin_attack_ticks: 0, + auto_spin_attack_dmg: 0.0, + spin_targets: Vec::new(), + spin_hit: None, sprinting: false, sprint_input: crate::sprint::SprintState::default(), shift_key_down: false, @@ -919,6 +931,7 @@ impl MotionWorld<'_, '_, '_> { input.z = 0.0; } self.jump_phase()?; + let before_travel = self.player.bounds(); // Player.travel: swim steering, then LivingEntity.travel (fluids // first, then gliding, then air), keeping 0.6 of the vertical speed // while flying. @@ -973,8 +986,26 @@ impl MotionWorld<'_, '_, '_> { { self.freeze_damage()?; } + if self.player.auto_spin_attack_ticks > 0 { + self.player.auto_spin_attack_ticks -= 1; + self.check_auto_spin_attack(before_travel); + } Ok(()) } + /// `LivingEntity.checkAutoSpinAttack`: the first living entity in the + /// box swept this tick takes the hit and ends the spin, bouncing the + /// player back at a fifth of its speed; a wall ends it too. + fn check_auto_spin_attack(&mut self, before: Aabb) { + let swept = before.union_bounds(self.player.bounds()); + let p = &mut self.player; + if let Some(&(id, _)) = p.spin_targets.iter().find(|(_, b)| b.intersects(swept)) { + p.spin_hit = Some(id); + p.auto_spin_attack_ticks = 0; + p.velocity = p.velocity.scale(-0.2); + } else if p.collision.horizontal { + p.auto_spin_attack_ticks = 0; + } + } /// `hurtServer(damageSources().freeze(), 1)` for a fully frozen player. fn freeze_damage(&mut self) -> Result<()> { let id = self @@ -2659,6 +2690,49 @@ mod tests { } } #[test] + fn a_riptide_spin_lies_flat_and_ends_on_the_first_entity_it_touches() { + let root = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../.local/horizon-client") + .join(horizon_core::VANILLA_VERSION); + if !root.join("movement-properties.nbt").exists() { + return; + } + let assets = Assets::load(&root).unwrap(); + let render = assets.reload(&root).unwrap(); + let binding = crate::collisions::load(&root, assets.registry).unwrap(); + let resources = MotionResources::load(&root, &assets).unwrap(); + let mut scene = World::test_scene(&assets, &render, 2).unwrap(); + let ahead = Aabb::new(Vec3::new(20.2, 9.0, 21.5), Vec3::new(20.8, 11.0, 22.1)); + for target in [None, Some(ahead)] { + let mut player = PlayerMotion::new(&resources, Vec3::new(20.5, 10.0, 20.5)).unwrap(); + player.auto_spin_attack_ticks = 20; + player.auto_spin_attack_dmg = 8.0; + player.velocity = Vec3::new(0.0, 0.0, 1.0); + player.spin_targets = target.map(|b| (7, b)).into_iter().collect(); + let mut w = MotionWorld { + entities: &[], + player: &mut player, + scene: &mut scene, + assets: &assets, + collision_binding: &binding, + resources: &resources, + }; + w.simulation_tick([0.0, 0.0], false).unwrap(); + if target.is_some() { + // Touched: the hit, no more spin, bounced back at a fifth. + assert_eq!(w.player.spin_hit, Some(7)); + assert_eq!(w.player.auto_spin_attack_ticks, 0); + assert!(w.player.velocity.z < 0.0, "{:?}", w.player.velocity); + assert_eq!(w.player.pose, crate::pose::Pose::Standing); + } else { + assert_eq!(w.player.spin_hit, None); + assert_eq!(w.player.auto_spin_attack_ticks, 19); + assert_eq!(w.player.pose, crate::pose::Pose::SpinAttack); + assert_eq!(w.player.dimensions.height, 0.6); + } + } + } + #[test] fn a_landing_glide_lasts_a_tick_so_a_jump_keeps_its_speed() { let root = Path::new(env!("CARGO_MANIFEST_DIR")) .join("../../.local/horizon-client") diff --git a/crates/horizon-client/src/pose.rs b/crates/horizon-client/src/pose.rs index 9c8124b0e..e34b1f1fc 100644 --- a/crates/horizon-client/src/pose.rs +++ b/crates/horizon-client/src/pose.rs @@ -14,6 +14,7 @@ pub enum Pose { Crouching, Swimming, FallFlying, + SpinAttack, } impl Pose { pub fn name(self) -> &'static str { @@ -22,13 +23,14 @@ impl Pose { Self::Crouching => "CROUCHING", Self::Swimming => "SWIMMING", Self::FallFlying => "FALL_FLYING", + Self::SpinAttack => "SPIN_ATTACK", } } pub fn dimensions(self, scale: f32) -> Dimensions { let (height, eye_height) = match self { Self::Standing => (1.8, 1.62), Self::Crouching => (1.5, 1.27), - Self::Swimming | Self::FallFlying => (0.6, 0.4), + Self::Swimming | Self::FallFlying | Self::SpinAttack => (0.6, 0.4), }; Dimensions { width: 0.6 * scale, @@ -98,11 +100,13 @@ impl MotionWorld<'_, '_, '_> { if !self.can_fit_pose(Pose::Swimming)? { return Ok(()); } - // Player.getDesiredPose: swimming before gliding. + // Player.getDesiredPose: swimming, gliding, Riptide's spin. let desired = if self.player.swimming { Pose::Swimming } else if self.player.fall_flying { Pose::FallFlying + } else if self.player.auto_spin_attack_ticks > 0 { + Pose::SpinAttack } else if self.player.shift_key_down && !self.player.flying { Pose::Crouching } else { From c2b7f3a4ea2dc5fdb86f4aa6469eabc4b2a26e22 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 07:55:54 +0200 Subject: [PATCH 030/124] Slow-frame and slow-tick lines show the CPU time of each span HORIZON_SPIKES spans keep the thread's CPU time beside their wall time, and the slow-frame line gives the render thread's CPU time in the frame. A span that mostly waited (for the GPU, a lock or a core) prints both (`submit 30.2/0.1`), so a frame that waited reads apart from one that worked. Found the cause of #678's long frames: the render thread losing its core in queue.submit. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 21 +++-- crates/horizon-client/src/game/levels.rs | 7 +- crates/horizon-client/src/spans.rs | 100 ++++++++++++++++++++--- docs/graphics.md | 2 +- 4 files changed, 103 insertions(+), 27 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 2df6aa9b0..518fba2db 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -10933,6 +10933,10 @@ impl ApplicationHandler for Game<'_> { WindowEvent::RedrawRequested => { self.observe_fullscreen(); let now = Instant::now(); + let frame_cpu = self + .options + .spikes + .and_then(|_| horizon_client::spans::thread_cpu()); let elapsed = now.duration_since(self.previous); self.frame_ticks = elapsed.as_secs_f32() * 20.0; self.previous = now; @@ -10999,15 +11003,16 @@ impl ApplicationHandler for Game<'_> { let cpu = now.elapsed().saturating_sub(r.last_wait).as_secs_f32() * 1000.0; let spans = horizon_client::spans::take(); if cpu > threshold { - let list: Vec = spans - .iter() - .filter(|(_, ms)| *ms >= 0.5) - .map(|(n, ms)| format!("{n} {ms:.1}")) - .collect(); + // The render thread's own CPU time in the frame: + // far below the wall time, the frame waited. + let worked = horizon_client::spans::thread_cpu() + .zip(frame_cpu) + .map_or_else(String::new, |(end, start)| { + format!(", cpu {:.1} ms", (end - start).as_secs_f32() * 1000.0) + }); log::info!( - "Horizon: slow frame {cpu:.1} ms ({} ticks): {}", - steps, - list.join(", ") + "Horizon: slow frame {cpu:.1} ms{worked} ({steps} ticks): {}", + horizon_client::spans::describe(&spans) ); } } diff --git a/crates/horizon-client/src/game/levels.rs b/crates/horizon-client/src/game/levels.rs index 31b34a9d0..7df5541b4 100644 --- a/crates/horizon-client/src/game/levels.rs +++ b/crates/horizon-client/src/game/levels.rs @@ -312,15 +312,10 @@ impl Game<'_> { .map_or_else(String::new, |(end, start)| { format!(", cpu {:.1} ms", (end - start).as_secs_f32() * 1000.0) }); - let list: Vec = spans - .iter() - .filter(|(_, ms)| *ms >= 0.5) - .map(|(n, ms)| format!("{n} {ms:.1}")) - .collect(); log::info!( "Horizon: slow tick {wall:.1} ms{cpu} ({} mobs): {}", self.mobs.len(), - list.join(", ") + horizon_client::spans::describe(&spans) ); } result diff --git a/crates/horizon-client/src/spans.rs b/crates/horizon-client/src/spans.rs index 5c37f2bfc..ce69be8ce 100644 --- a/crates/horizon-client/src/spans.rs +++ b/crates/horizon-client/src/spans.rs @@ -1,8 +1,10 @@ //! Named timings within a frame, for finding what makes a frame slow: //! `HORIZON_SPIKES=MS` prints the spans of every frame whose CPU time //! exceeds MS milliseconds, and of every server tick (dedicated or -//! integrated) longer than MS. Costs one clock read per span when enabled -//! and a relaxed load otherwise. +//! integrated) longer than MS. Each span keeps its wall time and the +//! thread's CPU time inside it, so a span that waited (for a lock, the GPU +//! or a core on a busy machine) shows apart from one that worked. Costs two +//! clock reads per span when enabled and a relaxed load otherwise. use std::{ cell::RefCell, sync::atomic::{AtomicBool, Ordering}, @@ -11,7 +13,15 @@ use std::{ static ENABLED: AtomicBool = AtomicBool::new(false); thread_local! { - static SPANS: RefCell> = const { RefCell::new(Vec::new()) }; + static SPANS: RefCell> = const { RefCell::new(Vec::new()) }; +} + +/// A named timing: wall milliseconds and the thread's CPU milliseconds in it. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Span { + pub name: &'static str, + pub ms: f32, + pub cpu_ms: f32, } pub fn enable(on: bool) { @@ -23,10 +33,13 @@ pub fn time(name: &'static str, f: impl FnOnce() -> T) -> T { if !ENABLED.load(Ordering::Relaxed) { return f(); } - let started = Instant::now(); + let (started, cpu) = (Instant::now(), thread_cpu()); let out = f(); let ms = started.elapsed().as_secs_f32() * 1000.0; - SPANS.with(|s| s.borrow_mut().push((name, ms))); + let cpu_ms = thread_cpu() + .zip(cpu) + .map_or(ms, |(end, start)| (end - start).as_secs_f32() * 1000.0); + SPANS.with(|s| s.borrow_mut().push(Span { name, ms, cpu_ms })); out } /// The CPU time this thread has used, where the platform tells it: a tick @@ -42,21 +55,84 @@ pub fn thread_cpu() -> Option { None } /// The frame's spans, summed by name, slowest first; clears them. -pub fn take() -> Vec<(&'static str, f32)> { +pub fn take() -> Vec { let spans = SPANS.with(|s| std::mem::take(&mut *s.borrow_mut())); - let mut sums: Vec<(&'static str, f32)> = Vec::new(); - for (name, ms) in spans { - match sums.iter_mut().find(|(n, _)| *n == name) { - Some(entry) => entry.1 += ms, - None => sums.push((name, ms)), + let mut sums: Vec = Vec::new(); + for span in spans { + match sums.iter_mut().find(|s| s.name == span.name) { + Some(sum) => { + sum.ms += span.ms; + sum.cpu_ms += span.cpu_ms; + } + None => sums.push(span), } } - sums.sort_by(|a, b| b.1.total_cmp(&a.1)); + sums.sort_by(|a, b| b.ms.total_cmp(&a.ms)); sums } +/// `name wall` for spans of at least 0.5 ms, with `/cpu` when the thread +/// worked for less than nine tenths of it: the slow-frame and slow-tick lines. +pub fn describe(spans: &[Span]) -> String { + let list: Vec = spans + .iter() + .filter(|s| s.ms >= 0.5) + .map(|s| { + if s.cpu_ms < s.ms * 0.9 { + format!("{} {:.1}/{:.1}", s.name, s.ms, s.cpu_ms) + } else { + format!("{} {:.1}", s.name, s.ms) + } + }) + .collect(); + list.join(", ") +} #[cfg(test)] mod tests { + use super::Span; + + /// Spans of one name add up; a span the thread spent mostly waiting + /// shows its CPU time after the wall time, short ones are left out. + #[test] + fn spans_sum_by_name_and_show_waits() { + super::SPANS.with(|s| { + s.borrow_mut().extend([ + Span { + name: "tick", + ms: 2.0, + cpu_ms: 2.0, + }, + Span { + name: "submit", + ms: 30.0, + cpu_ms: 0.2, + }, + Span { + name: "tick", + ms: 3.0, + cpu_ms: 2.9, + }, + Span { + name: "hud", + ms: 0.3, + cpu_ms: 0.3, + }, + ]); + }); + let spans = super::take(); + assert_eq!( + spans[0], + Span { + name: "submit", + ms: 30.0, + cpu_ms: 0.2 + } + ); + assert_eq!((spans[1].name, spans[1].ms), ("tick", 5.0)); + assert_eq!(super::describe(&spans), "submit 30.0/0.2, tick 5.0"); + assert!(super::take().is_empty()); + } + /// The thread's CPU clock moves with work done on it, not with time /// spent asleep. #[cfg(unix)] diff --git a/docs/graphics.md b/docs/graphics.md index b1c3c0209..34032a641 100644 --- a/docs/graphics.md +++ b/docs/graphics.md @@ -64,7 +64,7 @@ python3 tools/graphics_bench.py --compare baseline-4080 mine-gpu # markdown ta vscreen is a headless compositor whose frames are software-rendered (lavapipe), so its frame times measure the CPU, rasterisation included. `--offscreen` (`horizon-client --offscreen`) renders into a texture on the most powerful GPU instead of the window, which measures the RTX 4080 under the same compositor. `--distances`, `--scenes` and `--build` pick a subset or another build. Results go to `benchmarks/graphics/