From 3d4b63c19e0dde3e6aab138e056e2445dab0c42a Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 15:25:10 +0200 Subject: [PATCH 001/238] wasmtime 48 LTS: one wasm-tools version for the runtime, packing and the SDK wasmtime 36 read WebAssembly with wasm-tools 0.236, wit-component (cargo horizon-mod, the sandbox tests) used 0.239 and the SDK's wit-bindgen 0.247: three copies of wasmparser, wasm-encoder, wit-parser and wasm-metadata. wasmtime 48, the next long-term-support line (fixes until August 2028), wit-component 0.254 and wit-bindgen 0.60 all use 0.254. No code change; horizon-mods now needs Rust 1.95. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 541 +++++++++++++----------------- crates/horizon-mod-sdk/Cargo.toml | 2 +- crates/horizon-mods/Cargo.toml | 10 +- docs/mods.md | 8 +- 4 files changed, 241 insertions(+), 320 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f832e248..420f2226c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,9 +20,9 @@ checksum = "366ffbaa4442f4684d91e2cd7c5ea7c4ed8add41959a31447066e279e432b618" [[package]] name = "addr2line" -version = "0.25.1" +version = "0.26.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b5d307320b3181d6d7954e663bd7c774a838b8220fe0593c86d9fb09f498b4b" +checksum = "59317f77929f0e679d39364702289274de2f0f0b22cbf50b2b8cff2169a0b27a" dependencies = [ "gimli", ] @@ -155,7 +155,7 @@ dependencies = [ "objc2-foundation 0.3.2", "parking_lot", "percent-encoding", - "windows-sys 0.59.0", + "windows-sys 0.60.2", "wl-clipboard-rs", "x11rb", ] @@ -196,6 +196,17 @@ dependencies = [ "libloading", ] +[[package]] +name = "async-trait" +version = "0.1.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82f6aeea286b8eb4dd3431a1be1b59d290ace00f5bfd8e2a159bc2a05e2c1667" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -572,7 +583,7 @@ dependencies = [ "jni 0.21.1", "js-sys", "libc", - "mach2", + "mach2 0.4.3", "ndk", "ndk-context", "num-derive", @@ -586,6 +597,15 @@ dependencies = [ "windows 0.54.0", ] +[[package]] +name = "cpp_demangle" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0667304c32ea56cb4cd6d2d7c0cfe9a2f8041229db8c033af7f8d69492429def" +dependencies = [ + "cfg-if", +] + [[package]] name = "cpufeatures" version = "0.2.17" @@ -597,46 +617,48 @@ dependencies = [ [[package]] name = "cranelift-assembler-x64" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e92993516bcc43fd4e511b31d2e0c0c24f6b7ac18bb39e32cac2bfc05ea378e" +checksum = "e6071a4f1df4837aa03d1e4d53067784acafe270ffc7dad66a7f39832204da3f" dependencies = [ "cranelift-assembler-x64-meta", ] [[package]] name = "cranelift-assembler-x64-meta" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97eeeb61b08ec484f274119faf3a8a1b80dd300b0208c0247d16d05982fc89fe" +checksum = "5e3b92a9cc90388fe70e63640af30b734387449445aef0176ab2028e9892d70e" dependencies = [ "cranelift-srcgen", ] [[package]] name = "cranelift-bforest" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "782eb44158681a105d157cb0ce63e3dc83f78ef5674266c12234f49da3f5c6e8" +checksum = "f45c85646c4425cc85b868effaf20c302b437a3fb79bb27a64513dd522f07411" dependencies = [ "cranelift-entity", + "wasmtime-internal-core", ] [[package]] name = "cranelift-bitset" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edd59af193204c4f025fb2c2d9ee0aeb1f60929edf000b0dfa3d45543a62f264" +checksum = "94f56b5c7d564cfa03aa269a6b98c916cab4d545196a391d31403a23ef39333a" dependencies = [ "serde", "serde_derive", + "wasmtime-internal-core", ] [[package]] name = "cranelift-codegen" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1c29bd985057d790db25f6bc60b369b311aa2d75c2f496a7e9993585bfc7d60" +checksum = "2c073641ce6e382814a8220331cc1ea2c782a9ea5d5da6d0f7f7c0541192693e" dependencies = [ "bumpalo", "cranelift-assembler-x64", @@ -648,22 +670,26 @@ dependencies = [ "cranelift-entity", "cranelift-isle", "gimli", - "hashbrown 0.15.5", + "hashbrown 0.17.1", + "libm", "log", + "postcard", "pulley-interpreter", "regalloc2", "rustc-hash 2.1.3", "serde", + "serde_derive", + "sha2", "smallvec", "target-lexicon", - "wasmtime-internal-math", + "wasmtime-internal-core", ] [[package]] name = "cranelift-codegen-meta" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9ef8a8495f19212f92db8425f768ffc67000c5cb417a0664475a8d09596700f" +checksum = "db226fafea5194921de10dccf35ac2863500313990f770cbe8b26c51eebec4d4" dependencies = [ "cranelift-assembler-x64-meta", "cranelift-codegen-shared", @@ -674,37 +700,39 @@ dependencies = [ [[package]] name = "cranelift-codegen-shared" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3aa5099097666baf45bd68ef036327c8f29d8785fffaaa7db3b3b6b7e94bcc6" +checksum = "cf8ac46b2c8e328ca979810426ea635ef90ecc8c54fd3296bde31ab470a48a1d" [[package]] name = "cranelift-control" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26f67f4f4e92c2934f40c2ff4574400bd27326932aad937a438b7fb459f60a63" +checksum = "6754e0ab97915209ea6ce6549f6443b018e1338142ada4c856cec213ee82e4b8" dependencies = [ "arbitrary", ] [[package]] name = "cranelift-entity" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8393d09f0abaa504896069e79f4eb6f15eb1853e5645732b082dac1ed292aaa8" +checksum = "774d25e36df18696f63a284e9c4ea43455e2ebcca6308e0e02301f18c8e777e2" dependencies = [ "cranelift-bitset", "serde", "serde_derive", + "wasmtime-internal-core", ] [[package]] name = "cranelift-frontend" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "580cfaecbd11476dd82f6a393123dcbd9dfe583467df8d0c8441c641444d4985" +checksum = "69cfcb31405e2de2a48e5a6de8e07a3d8b77f35caa3e944d627780734b36c138" dependencies = [ "cranelift-codegen", + "hashbrown 0.17.1", "log", "smallvec", "target-lexicon", @@ -712,15 +740,15 @@ dependencies = [ [[package]] name = "cranelift-isle" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c044843adb7d94b1facebaad846c0a762f79a77953417e3f0182093bcbbdc38" +checksum = "2f5a26df22db546ab92be6feaf21835ae91082af0a32bbb398b277df3765412f" [[package]] name = "cranelift-native" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9d4c1707214e53f2040e4d13430069ea22623287b2750ed6d62e40eba88d91a" +checksum = "19f258c08e590f90dcaf9904d25ba142ee2980c3aaeac4bb45f1452b1a407c2a" dependencies = [ "cranelift-codegen", "libc", @@ -729,9 +757,9 @@ dependencies = [ [[package]] name = "cranelift-srcgen" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d355a35edc49f628db7b793f3527eed31b560e77c3e8ed0e5258aad8df8a5249" +checksum = "67055b4f315e8f6af3e8eccec74ad38f7407f0fd854adbd49b4b3b27bdb2e3be" [[package]] name = "crc32fast" @@ -1003,12 +1031,6 @@ version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b5343afd4a8365a643ac588dab4cf234a190c7f6c88c9f6dd6ffe00837661b7" -[[package]] -name = "fallible-iterator" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" - [[package]] name = "fdeflate" version = "0.3.7" @@ -1101,12 +1123,48 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "futures" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a31d2a3fbaaeb2af2368bbdd904aa8e812d3c04a1ee10d3171f52d556e5d0a3" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f9e3d69d39e4862ffed03ed071a76f9a13ba1d9109d355b0f0aa6b15e393c4" +dependencies = [ + "futures-core", + "futures-sink", +] + [[package]] name = "futures-core" version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" +[[package]] +name = "futures-io" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53c0fa8157de1303bfffdaa1cc2a673bfffb60102f76b0ef4441659124373fed" + +[[package]] +name = "futures-sink" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1944426bf7d03f1d14f708785e4b33efd750b36d48a157b836b3efc15ede8e1d" + [[package]] name = "futures-task" version = "0.3.34" @@ -1119,8 +1177,12 @@ version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" dependencies = [ + "futures-channel", "futures-core", + "futures-io", + "futures-sink", "futures-task", + "memchr", "pin-project-lite", "slab", ] @@ -1215,11 +1277,12 @@ dependencies = [ [[package]] name = "gimli" -version = "0.32.3" +version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7" +checksum = "0bf7f043f89559805f8c7cacc432749b2fa0d0a0a9ee46ce47164ed5ba7f126c" dependencies = [ - "fallible-iterator", + "fnv", + "hashbrown 0.16.1", "indexmap", "stable_deref_trait", ] @@ -1315,7 +1378,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" dependencies = [ "foldhash 0.1.5", - "serde", ] [[package]] @@ -1336,6 +1398,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" dependencies = [ "foldhash 0.2.0", + "serde", + "serde_core", ] [[package]] @@ -1486,7 +1550,7 @@ dependencies = [ name = "horizon-mod-sdk" version = "0.1.0" dependencies = [ - "wit-bindgen", + "wit-bindgen 0.60.0", ] [[package]] @@ -1499,8 +1563,8 @@ dependencies = [ "serde_json", "toml", "wasmtime", - "wit-component 0.239.0", - "wit-parser 0.239.0", + "wit-component", + "wit-parser", "zip", ] @@ -2127,6 +2191,12 @@ dependencies = [ "libc", ] +[[package]] +name = "mach2" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dae608c151f68243f2b000364e1f7b186d9c29845f7d2d85bd31b9ad77ad552b" + [[package]] name = "memchr" version = "2.8.3" @@ -2662,12 +2732,12 @@ dependencies = [ [[package]] name = "object" -version = "0.37.3" +version = "0.39.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff76201f031d8863c38aa7f905eca4f53abbfa15f609db4277d44cd8938f33fe" +checksum = "2e5a6c098c7a3b6547378093f5cc30bc54fd361ce711e05293a5cc589562739b" dependencies = [ "crc32fast", - "hashbrown 0.15.5", + "hashbrown 0.17.1", "indexmap", "memchr", ] @@ -2972,21 +3042,21 @@ checksum = "3d595e54a326bc53c1c197b32d295e14b169e3cfeaa8dc82b529f947fba6bcf5" [[package]] name = "pulley-interpreter" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "334ae8982dcf354e5f28e79fc6f935601e028b729cc174d3937eeeaa859057b8" +checksum = "2b8187c8e7db87d085865fc106502e7b34d5c25f8ce4c272945b21503371f6a0" dependencies = [ "cranelift-bitset", "log", "pulley-macros", - "wasmtime-internal-math", + "wasmtime-internal-core", ] [[package]] name = "pulley-macros" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cd207bca4bc5e46cb4fcea87196dcbd78befc628c64c67d11dc99fce5cd5369" +checksum = "7f98ce0e381d9e45e63ea1d20bba3cf131797086077f43af9203f1fb7de0ed6b" dependencies = [ "proc-macro2", "quote", @@ -3094,15 +3164,16 @@ dependencies = [ [[package]] name = "regalloc2" -version = "0.12.2" +version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5216b1837de2149f8bc8e6d5f88a9326b63b8c836ed58ce4a0a29ec736a59734" +checksum = "757712e8e61590d6d4f5d563483755538b5aa13467837a3b41cd9832509a7f85" dependencies = [ "allocator-api2", "bumpalo", - "hashbrown 0.15.5", + "hashbrown 0.17.1", "log", "rustc-hash 2.1.3", + "serde", "smallvec", ] @@ -3155,6 +3226,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustc-demangle" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b74b56ffa8bb2830709a538c2cbcae9aa062db0d2a42563bfb09bdaae44020eb" + [[package]] name = "rustc-hash" version = "1.1.0" @@ -3995,7 +4072,7 @@ version = "1.0.4+wasi-0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487" dependencies = [ - "wit-bindgen", + "wit-bindgen 0.57.1", ] [[package]] @@ -4056,124 +4133,66 @@ dependencies = [ [[package]] name = "wasm-encoder" -version = "0.236.1" +version = "0.254.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "724fccfd4f3c24b7e589d333fc0429c68042897a7e8a5f8694f31792471841e7" +checksum = "ebaca0b540bdb15d195ff34fb23074255948708b8916a11a339ba4513dc61906" dependencies = [ "leb128fmt", - "wasmparser 0.236.1", -] - -[[package]] -name = "wasm-encoder" -version = "0.239.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5be00faa2b4950c76fe618c409d2c3ea5a3c9422013e079482d78544bb2d184c" -dependencies = [ - "leb128fmt", - "wasmparser 0.239.0", -] - -[[package]] -name = "wasm-encoder" -version = "0.247.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30b6733b8b91d010a6ac5b0fb237dc46a19650bc4c67db66857e2e787d437204" -dependencies = [ - "leb128fmt", - "wasmparser 0.247.0", + "wasmparser", ] [[package]] name = "wasm-metadata" -version = "0.239.0" +version = "0.254.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20b3ec880a9ac69ccd92fbdbcf46ee833071cf09f82bb005b2327c7ae6025ae2" +checksum = "5d1880dd9a848aa68f387966256c34ad49abb4607e3cfa35720d2723d0591cd3" dependencies = [ "anyhow", "indexmap", - "wasm-encoder 0.239.0", - "wasmparser 0.239.0", -] - -[[package]] -name = "wasm-metadata" -version = "0.247.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "665fe59e56cc9b419ca6fcca56673e3421d1a5011e3b65caf6b726fd9e041d10" -dependencies = [ - "anyhow", - "indexmap", - "wasm-encoder 0.247.0", - "wasmparser 0.247.0", + "wasm-encoder", + "wasmparser", ] [[package]] name = "wasmparser" -version = "0.236.1" +version = "0.254.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9b1e81f3eb254cf7404a82cee6926a4a3ccc5aad80cc3d43608a070c67aa1d7" +checksum = "c5ff8685b587722894b6f25958fc608d315b93b8d23647bfb567664e7006d757" dependencies = [ "bitflags 2.13.2", - "hashbrown 0.15.5", + "hashbrown 0.17.1", "indexmap", "semver", "serde", ] -[[package]] -name = "wasmparser" -version = "0.239.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c9d90bb93e764f6beabf1d02028c70a2156a6583e63ac4218dd07ef733368b0" -dependencies = [ - "bitflags 2.13.2", - "hashbrown 0.15.5", - "indexmap", - "semver", -] - -[[package]] -name = "wasmparser" -version = "0.247.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e6fb4c2bee46c5ea4d40f8cdb5c131725cd976718ec56f1c8e82fbde5fa2a80" -dependencies = [ - "bitflags 2.13.2", - "hashbrown 0.17.1", - "indexmap", - "semver", -] - [[package]] name = "wasmprinter" -version = "0.236.1" +version = "0.254.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2df225df06a6df15b46e3f73ca066ff92c2e023670969f7d50ce7d5e695abbb1" +checksum = "8b03c8f270f3f384f70f42869b5ff7f6253fe4f0aa8590f7edd332c1943e5f51" dependencies = [ "anyhow", "termcolor", - "wasmparser 0.236.1", + "wasmparser", ] [[package]] name = "wasmtime" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70095b9fe63cc2a87ed206729e11544610decc184c62898c5d201ff1d666d82e" +checksum = "12300d040e8bea7e7ccde75b9919d27ebba47a217a64b09ba058ae8d3b4290b6" dependencies = [ "addr2line", - "anyhow", + "async-trait", "bitflags 2.13.2", "bumpalo", "cc", - "cfg-if", "encoding_rs", - "hashbrown 0.15.5", - "indexmap", + "futures", "libc", "log", - "mach2", + "mach2 0.6.0", "memfd", "object", "once_cell", @@ -4185,62 +4204,56 @@ dependencies = [ "serde_derive", "smallvec", "target-lexicon", - "wasmparser 0.236.1", + "wasmparser", "wasmtime-environ", - "wasmtime-internal-asm-macros", "wasmtime-internal-component-macro", "wasmtime-internal-component-util", + "wasmtime-internal-core", "wasmtime-internal-cranelift", "wasmtime-internal-fiber", "wasmtime-internal-jit-debug", "wasmtime-internal-jit-icache-coherence", - "wasmtime-internal-math", - "wasmtime-internal-slab", "wasmtime-internal-unwinder", "wasmtime-internal-versioned-export-macros", - "wasmtime-internal-winch", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] name = "wasmtime-environ" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a67ea8904b6e369e2218780bd1f9186f1980cb616445eb2c1f3c4e45c8397865" +checksum = "805d6554eb6dd6e62891061a31383a89d69fd891f785e5ae68f0835a554b7a2a" dependencies = [ "anyhow", + "cpp_demangle", + "cranelift-bforest", "cranelift-bitset", "cranelift-entity", "gimli", + "hashbrown 0.17.1", "indexmap", "log", "object", "postcard", + "rustc-demangle", "semver", "serde", "serde_derive", + "sha2", "smallvec", "target-lexicon", - "wasm-encoder 0.236.1", - "wasmparser 0.236.1", + "wasm-encoder", + "wasmparser", "wasmprinter", "wasmtime-internal-component-util", -] - -[[package]] -name = "wasmtime-internal-asm-macros" -version = "36.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0488a52ae93e654baecb08ce04d15d478756eb2168c2bdab7915710109161ce" -dependencies = [ - "cfg-if", + "wasmtime-internal-core", ] [[package]] name = "wasmtime-internal-component-macro" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "377e406c3f8e7bea64db3cb3ee0d406c331e8fc09d8cb2e7bf9ad74cb5d3b31a" +checksum = "b832b80853365d9d3a741681044554be291a4f977aae074b318b55c7a4835f83" dependencies = [ "anyhow", "proc-macro2", @@ -4248,23 +4261,32 @@ dependencies = [ "syn 2.0.119", "wasmtime-internal-component-util", "wasmtime-internal-wit-bindgen", - "wit-parser 0.236.1", + "wit-parser", ] [[package]] name = "wasmtime-internal-component-util" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97c24503bb712afe2c797dc291b96e57556ee62c35fd951f87e96088324de37c" +checksum = "4c8a11aaf68d8945991429d45950f32490a30ddcdcdb56629816cb41a255d8fe" + +[[package]] +name = "wasmtime-internal-core" +version = "48.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea266d6cfb31c324c075c8432386654bcbb1559aa8d29be9d47eacf7dcc41012" +dependencies = [ + "hashbrown 0.17.1", + "libm", + "serde", +] [[package]] name = "wasmtime-internal-cranelift" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f63ea7ee133de9109a4a1fc63dae3afc9ea32e92b8dada824fa4595dbf175f80" +checksum = "d427759553b07c296c8ed7bff8ac930a94b9ba99e6a79243ea79bcc51aa58d96" dependencies = [ - "anyhow", - "cfg-if", "cranelift-codegen", "cranelift-control", "cranelift-entity", @@ -4278,33 +4300,32 @@ dependencies = [ "smallvec", "target-lexicon", "thiserror 2.0.21", - "wasmparser 0.236.1", + "wasmparser", "wasmtime-environ", - "wasmtime-internal-math", + "wasmtime-internal-core", + "wasmtime-internal-unwinder", "wasmtime-internal-versioned-export-macros", ] [[package]] name = "wasmtime-internal-fiber" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69662a0a4dbe9784d1e8add9e3c5d2db0ca348085c624c27a70992dbe2eff4a3" +checksum = "b54c8c5b02ea985149a90f917e1761af99734c6c27e6d7fdd5b6f69fc1db3950" dependencies = [ - "anyhow", "cc", - "cfg-if", "libc", "rustix 1.1.5", - "wasmtime-internal-asm-macros", + "wasmtime-environ", "wasmtime-internal-versioned-export-macros", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] name = "wasmtime-internal-jit-debug" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00cb07d23903c0be359d6b920d94774ea322759c3f4d6721b61afa03602e4804" +checksum = "72bb10c94724f072b6eca515085033e61da9efae65d054545126df37a15530c3" dependencies = [ "cc", "wasmtime-internal-versioned-export-macros", @@ -4312,83 +4333,50 @@ dependencies = [ [[package]] name = "wasmtime-internal-jit-icache-coherence" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa2b47c37a6d026e2755fe2cf7ec5c1f4b0fba62e448ea910cdf6a07a4982c6a" +checksum = "1b781a27e8958fd07fd3135c0be216f77301efecd15cbf4f14ff210cdad5f169" dependencies = [ - "anyhow", - "cfg-if", "libc", - "windows-sys 0.60.2", + "wasmtime-internal-core", + "windows-sys 0.61.2", ] -[[package]] -name = "wasmtime-internal-math" -version = "36.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b269f0ba44102c57fd5f94f1bf4395ccd4fc2b84698fc78f627a6f2a47131a62" -dependencies = [ - "libm", -] - -[[package]] -name = "wasmtime-internal-slab" -version = "36.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e35ac9d503d503703cd24d532e53e2e93c86109b451df40a9801500045aee25" - [[package]] name = "wasmtime-internal-unwinder" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83ce1d0930da44ed375701e6b4f187b46249f7efe2b1d8c1c77229a4adc82de9" +checksum = "121188de1b2741b8e35530a9b0c8adf525b3aa5913a2ca3d4522509cfbc0494a" dependencies = [ - "anyhow", - "cfg-if", "cranelift-codegen", "log", "object", + "wasmtime-environ", ] [[package]] name = "wasmtime-internal-versioned-export-macros" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b6633be262400bd7abab4ff767a16e8b6a41ff117be54bf76dabecea9f96225" +checksum = "933f1990e4e5177547a4abd51dcfa5cb7616fe42b9fb6f0f2b7423627758223c" dependencies = [ "proc-macro2", "quote", "syn 2.0.119", ] -[[package]] -name = "wasmtime-internal-winch" -version = "36.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "509d1e1cf8e3b3306f291407c92bca8381d9294f8c54ad74b692538f1f5b23c7" -dependencies = [ - "anyhow", - "cranelift-codegen", - "gimli", - "object", - "target-lexicon", - "wasmparser 0.236.1", - "wasmtime-environ", - "wasmtime-internal-cranelift", - "winch-codegen", -] - [[package]] name = "wasmtime-internal-wit-bindgen" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6d9a5b51b3400b4df1055724c4536ad5aa58fafe83cea5ca94f88cc9eb97563" +checksum = "417ebd61d2581d86d65413517f3a41294a9ad105da26f0dc7039912a0d97e1ed" dependencies = [ "anyhow", "bitflags 2.13.2", "heck", "indexmap", - "wit-parser 0.236.1", + "wit-component", + "wit-parser", ] [[package]] @@ -4698,26 +4686,6 @@ dependencies = [ "windows-sys 0.61.2", ] -[[package]] -name = "winch-codegen" -version = "36.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e470d7e674c41ad32dddc02d8d8a94983e0a4c516028bda60747e6dd862a567" -dependencies = [ - "anyhow", - "cranelift-assembler-x64", - "cranelift-codegen", - "gimli", - "regalloc2", - "smallvec", - "target-lexicon", - "thiserror 2.0.21", - "wasmparser 0.236.1", - "wasmtime-environ", - "wasmtime-internal-cranelift", - "wasmtime-internal-math", -] - [[package]] name = "windows" version = "0.54.0" @@ -5164,42 +5132,48 @@ name = "wit-bindgen" version = "0.57.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "wit-bindgen" +version = "0.60.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a301904d6657d6364c758d869e5389d05d393b16d5b65db60b4f03cbe71bb80d" dependencies = [ "wit-bindgen-rust-macro", ] [[package]] name = "wit-bindgen-core" -version = "0.57.1" +version = "0.60.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02dee27a2dc20d1008016c742ec9fc6ea498492994ba3750be7454cbc97ff04c" +checksum = "48521bb96e56cbb9e031ad306c80dc20e89e69e49ada02781ee06f60fbcb545f" dependencies = [ "anyhow", "heck", - "wit-parser 0.247.0", + "wit-parser", ] [[package]] name = "wit-bindgen-rust" -version = "0.57.1" +version = "0.60.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5007dae772945b7a5003d69d90a3a4a78929d41f19d004e980c4259a6af4484" +checksum = "3df3fe9b9a0066f82fd0c2f07f79d0ffc0f85c53fa5f998516d8722712479042" dependencies = [ "anyhow", "heck", "indexmap", "prettyplease", "syn 2.0.119", - "wasm-metadata 0.247.0", + "wasm-metadata", "wit-bindgen-core", - "wit-component 0.247.0", + "wit-component", ] [[package]] name = "wit-bindgen-rust-macro" -version = "0.57.1" +version = "0.60.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af9237d678e3513ad24e96fe98beacdc0db6405284ba2a2400418cf0d42caa89" +checksum = "d689c4bc9d6af067c651cfba444766343c9a4bdef15fad1e2efab95c0c277af0" dependencies = [ "anyhow", "prettyplease", @@ -5212,9 +5186,9 @@ dependencies = [ [[package]] name = "wit-component" -version = "0.239.0" +version = "0.254.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88a866b19dba2c94d706ec58c92a4c62ab63e482b4c935d2a085ac94caecb136" +checksum = "1c96b3cea6838dbea55173baa56991fcb087896c321b0a770db201e5a0bd3506" dependencies = [ "anyhow", "bitflags 2.13.2", @@ -5223,72 +5197,17 @@ dependencies = [ "serde", "serde_derive", "serde_json", - "wasm-encoder 0.239.0", - "wasm-metadata 0.239.0", - "wasmparser 0.239.0", - "wit-parser 0.239.0", -] - -[[package]] -name = "wit-component" -version = "0.247.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d567162a6b9843080e5e0053f696623ff694bae8ae017c9ec536d1873bbe3d8" -dependencies = [ - "anyhow", - "bitflags 2.13.2", - "indexmap", - "log", - "serde", - "serde_derive", - "serde_json", - "wasm-encoder 0.247.0", - "wasm-metadata 0.247.0", - "wasmparser 0.247.0", - "wit-parser 0.247.0", + "wasm-encoder", + "wasm-metadata", + "wasmparser", + "wit-parser", ] [[package]] name = "wit-parser" -version = "0.236.1" +version = "0.254.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16e4833a20cd6e85d6abfea0e63a399472d6f88c6262957c17f546879a80ba15" -dependencies = [ - "anyhow", - "id-arena", - "indexmap", - "log", - "semver", - "serde", - "serde_derive", - "serde_json", - "unicode-xid", - "wasmparser 0.236.1", -] - -[[package]] -name = "wit-parser" -version = "0.239.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55c92c939d667b7bf0c6bf2d1f67196529758f99a2a45a3355cc56964fd5315d" -dependencies = [ - "anyhow", - "id-arena", - "indexmap", - "log", - "semver", - "serde", - "serde_derive", - "serde_json", - "unicode-xid", - "wasmparser 0.239.0", -] - -[[package]] -name = "wit-parser" -version = "0.247.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ffe4064318cdf3c08cb99343b44c039fcefe61ccdf58aa9975285f13d74d1fc" +checksum = "22672d61bfb9df46a16b46ce38522293aee1cc4749fc84a3464ce4083b3d5fd6" dependencies = [ "anyhow", "hashbrown 0.17.1", @@ -5299,8 +5218,8 @@ dependencies = [ "serde", "serde_derive", "serde_json", - "unicode-xid", - "wasmparser 0.247.0", + "unicode-ident", + "wasmparser", ] [[package]] diff --git a/crates/horizon-mod-sdk/Cargo.toml b/crates/horizon-mod-sdk/Cargo.toml index df223b28c..899fb555a 100644 --- a/crates/horizon-mod-sdk/Cargo.toml +++ b/crates/horizon-mod-sdk/Cargo.toml @@ -7,7 +7,7 @@ rust-version = "1.85" description = "Write Horizon mods in Rust: WebAssembly components for the horizon:mod world (docs/mods.md)" [dependencies] -wit-bindgen = { version = "0.57.1", default-features = false, features = ["macros", "realloc"] } +wit-bindgen = { version = "0.60.0", default-features = false, features = ["macros", "realloc"] } [lints] workspace = true diff --git a/crates/horizon-mods/Cargo.toml b/crates/horizon-mods/Cargo.toml index 8db24ac35..d44648691 100644 --- a/crates/horizon-mods/Cargo.toml +++ b/crates/horizon-mods/Cargo.toml @@ -3,7 +3,7 @@ name = "horizon-mods" version = "0.1.0" edition.workspace = true license.workspace = true -rust-version = "1.86" +rust-version = "1.95" description = "Horizon's mod runtime: sandboxed WebAssembly components (docs/mods.md)" [dependencies] @@ -12,11 +12,11 @@ serde_json = "1" toml = "1.1.6" zip = { version = "2", default-features = false, features = ["deflate"] } log = "0.4" -# The 36 LTS line: security fixes until August 2027, and it builds on the client's Rust. -wasmtime = { version = "36.0.18", default-features = false, features = ["runtime", "cranelift", "component-model", "std"], optional = true } +# The 48 LTS line: security fixes until August 2028, on the same wasm-tools (0.254) as wit-component and the SDK's wit-bindgen. +wasmtime = { version = "48.0.7", default-features = false, features = ["runtime", "cranelift", "component-model", "std"], optional = true } # Turns a mod's core module into a component (cargo horizon-mod build, the tests). -wit-component = { version = "0.239", optional = true } -wit-parser = { version = "0.239", optional = true } +wit-component = { version = "0.254", optional = true } +wit-parser = { version = "0.254", optional = true } [features] default = ["runtime"] diff --git a/docs/mods.md b/docs/mods.md index 4b356d336..6aef6295a 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -14,7 +14,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | The contract (WIT) | `crates/horizon-mod-sdk/wit/horizon.wit` | 0.1: log, game-read, chat, hud, storage; tick, frame, chat and command hooks | | SDK for mod authors | `crates/horizon-mod-sdk` | 0.1 | | Example mod | `crates/horizon-mod-sdk/examples/coords-hud` | coordinates, facing and time HUD, `/where`, `/coords` | -| Runtime | `crates/horizon-mods` | wasmtime 36 LTS, component model | +| Runtime | `crates/horizon-mods` | wasmtime 48 LTS, component model | | Authoring tool | `crates/cargo-horizon-mod` | `cargo horizon-mod new / build / run` | | In the game | `crates/horizon-client/src/game/mods.rs`, Options > Mods | client mods | | Launcher mod browser (Modrinth, CurseForge with a key) | [mod-browser.md](mod-browser.md) | resource packs, shader packs, modpacks | @@ -130,8 +130,10 @@ There is no file, socket, process or raw-input interface, on purpose. | native `cdylib` plugins | none | no: per OS and arch, unstable Rust ABI | yes | none | unsafe for players | | an embedded JVM | none per mod | yes | Java | 100+ MB, seconds of startup | see the research | -wasmtime 36 is a long-term-support line (security fixes until August 2027) -that builds on the client's Rust 1.90. Only its `runtime`, `cranelift`, +wasmtime 48 is a long-term-support line (security fixes until August 2028) +and needs Rust 1.95. It reads WebAssembly with the same wasm-tools release +(0.254) as the `wit-component` that packs mods and the SDK's `wit-bindgen`, so +the build carries one copy of them. Only its `runtime`, `cranelift`, `component-model` and `std` features are on: no WASI, no async, no GC proposal, no profiling. Its cranelift backend generates native code for x86-64 and ARM64 on Windows, macOS and Linux, which is why one `.hmod` serves them From d4b2e24535111e7b8e1b839bcc16c4ef126fa663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Dr=C3=A4ger?= <20014538+sdraeger@users.noreply.github.com> Date: Sat, 24 Oct 2026 13:21:23 -0700 Subject: [PATCH 002/238] Add mod command capability and Predator Missile example --- AGENTS.md | 2 +- Cargo.lock | 7 + Cargo.toml | 2 +- crates/horizon-client/src/game/mods.rs | 13 ++ .../examples/predator-missile/Cargo.toml | 16 ++ .../predator-missile/horizon-mod.toml | 8 + .../examples/predator-missile/src/lib.rs | 161 ++++++++++++++++++ crates/horizon-mod-sdk/src/lib.rs | 4 +- crates/horizon-mod-sdk/wit/horizon.wit | 9 + crates/horizon-mods/src/manifest.rs | 12 +- crates/horizon-mods/src/runtime.rs | 17 +- crates/horizon-mods/tests/sandbox.rs | 45 ++++- docs/api/mods-reference.md | 13 +- docs/mods.md | 3 +- 14 files changed, 300 insertions(+), 12 deletions(-) create mode 100644 crates/horizon-mod-sdk/examples/predator-missile/Cargo.toml create mode 100644 crates/horizon-mod-sdk/examples/predator-missile/horizon-mod.toml create mode 100644 crates/horizon-mod-sdk/examples/predator-missile/src/lib.rs diff --git a/AGENTS.md b/AGENTS.md index f6f6b463b..fda05c1e2 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -80,7 +80,7 @@ python3 tools/agent_control_e2e.py # horizon-ctl / MCP end to end ~25, a file of the client lib ~103, of `horizon-world` ~103–109, of `horizon-core` ~119. Plan for it. - Rust integration tests: `crates/horizon-client/tests/` (`lan_bot.rs`, `generation_saves.rs`, `streaming_meshes.rs`). - **macOS:** `cargo build/test --workspace` may fail to link the two wasm guest cdylibs (undefined - `horizon:mods/events` symbols) until #1446 lands: add `--exclude coords-hud --exclude horizon-mods-test-guest`. + `horizon:mods/events` symbols) until #1446 lands: add `--exclude coords-hud --exclude predator-missile --exclude horizon-mods-test-guest`. Their real check is `cargo test -p horizon-mods` (builds them for `wasm32-unknown-unknown`). - Linux needs `libasound2-dev libudev-dev`; `--no-default-features` drops audio and controllers (the server build). - Run and drive the game (starts the `horizon-client` beside `horizon-ctl`; other arguments go to the game): diff --git a/Cargo.lock b/Cargo.lock index 27eaad0c4..1468327ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2620,6 +2620,13 @@ dependencies = [ "zerovec", ] +[[package]] +name = "predator-missile" +version = "0.1.0" +dependencies = [ + "horizon-mod-sdk", +] + [[package]] name = "presser" version = "0.3.1" diff --git a/Cargo.toml b/Cargo.toml index f197c34fe..a0feaba87 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] exclude = ["launcher", "crates/horizon-telemetry-server", "third_party/wgpu-hal"] -members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-release-sign", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"] +members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-release-sign", "crates/horizon-mod-sdk/examples/coords-hud", "crates/horizon-mod-sdk/examples/predator-missile", "xtask"] resolver = "2" [workspace.package] diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs index 572f9fe2b..c203d0ca2 100644 --- a/crates/horizon-client/src/game/mods.rs +++ b/crates/horizon-client/src/game/mods.rs @@ -342,6 +342,19 @@ impl Game<'_> { } Output::Feedback { text, error: false } => Text::literal(text), Output::Feedback { text, error: true } => Text::literal(text).with_color(text::RED), + // Vanilla's commands only: a mod running a mod command could loop. + Output::Command { line } => { + let name = line.split(' ').next().unwrap_or_default(); + // As handle_chat_input, without the chat history. + if !vanilla_command(name) { + // dropped + } else if self.remote.is_some() { + self.send_chat(&format!("/{line}")); + } else { + self.run_command(&line); + } + continue; + } Output::Stopped { name, reason } => { self.list_mods(); self.sync_mod_commands(); diff --git a/crates/horizon-mod-sdk/examples/predator-missile/Cargo.toml b/crates/horizon-mod-sdk/examples/predator-missile/Cargo.toml new file mode 100644 index 000000000..a3a77ce0b --- /dev/null +++ b/crates/horizon-mod-sdk/examples/predator-missile/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "predator-missile" +version = "0.1.0" +edition.workspace = true +license.workspace = true +publish = false +description = "Example Horizon mod: a missile you launch with /missile and steer with the mouse" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +horizon-mod-sdk = { path = "../.." } + +[lints] +workspace = true diff --git a/crates/horizon-mod-sdk/examples/predator-missile/horizon-mod.toml b/crates/horizon-mod-sdk/examples/predator-missile/horizon-mod.toml new file mode 100644 index 000000000..3baaf7d87 --- /dev/null +++ b/crates/horizon-mod-sdk/examples/predator-missile/horizon-mod.toml @@ -0,0 +1,8 @@ +id = "predator_missile" +name = "Predator Missile" +version = "0.1.0" +description = "/missile launches a guided missile 60 blocks above you. Steer it with the mouse; it explodes on whatever it hits. Needs cheats (it runs vanilla commands as you)." +authors = ["Horizon"] +api = "0.1" +side = "client" +capabilities = ["game-read", "hud", "chat", "command"] diff --git a/crates/horizon-mod-sdk/examples/predator-missile/src/lib.rs b/crates/horizon-mod-sdk/examples/predator-missile/src/lib.rs new file mode 100644 index 000000000..75ebbbdec --- /dev/null +++ b/crates/horizon-mod-sdk/examples/predator-missile/src/lib.rs @@ -0,0 +1,161 @@ +//! Example Horizon mod (`docs/mods.md`): `/missile` turns you into a guided +//! missile 60 blocks up. You fly along your own look, so the mouse steers; +//! on a block or a mob a stack of primed TNT goes off, and you are put back +//! where you launched. Built from vanilla commands (capability `command`), +//! so it needs cheats. +use horizon_mod_sdk::{ + Mod, Registration, + command::run, + game::{self, Player}, + hud::{self, Corner, Line}, +}; + +/// Blocks per tick (a fired arrow does about 3). +const SPEED: f64 = 1.5; +const ALTITUDE: i32 = 60; +/// Ticks of flight before the missile detonates on its own. +const FUEL: u32 = 30 * 20; +/// Ticks to watch the blast before going back. +const AFTERMATH: u32 = 40; +/// Primed TNT per blast: the stack's explosions add up. +const WARHEAD: usize = 4; +/// What the impact checks print; the mod hears it through the chat hook. +const IMPACT: &str = "Predator: impact"; + +enum State { + Idle, + Flying { from: Player, ticks: u32 }, + Aftermath { from: Player, ticks: u32 }, +} + +struct Predator { + state: State, +} + +fn launch() { + run("gamerule send_command_feedback false"); + run("execute if entity @s[gamemode=creative] run tag @s add pm_creative"); + run("gamemode spectator"); + run(&format!("tp @s ~ ~{ALTITUDE} ~ ~ 60")); + run("effect give @s night_vision 60 0 true"); + // The thermal camera: every mob outlined. + run("effect give @e[type=!player,distance=..256] glowing 60 0 true"); +} + +/// One tick of flight: stop at the surface (the next step would enter a +/// block or a mob), else move one step along the look. +fn fly() { + run(&format!( + "execute at @s unless block ^ ^ ^{SPEED} #minecraft:air run tellraw @s \"{IMPACT}\"" + )); + run(&format!( + "execute at @s positioned ^ ^ ^{SPEED} if entity @e[type=!player,distance=..2] run tellraw @s \"{IMPACT}\"" + )); + run(&format!( + "execute at @s if block ^ ^ ^{SPEED} #minecraft:air run tp @s ^ ^ ^{SPEED}" + )); +} + +fn detonate() { + for _ in 0..WARHEAD { + run("execute at @s run summon tnt ^ ^ ^1 {fuse:0s}"); + } +} + +fn land(from: &Player) { + run(&format!( + "tp @s {} {} {} {} {}", + from.x, from.y, from.z, from.yaw, from.pitch + )); + run("execute if entity @s[tag=pm_creative] run gamemode creative"); + run("execute unless entity @s[tag=pm_creative] run gamemode survival"); + run("execute if entity @s[tag=pm_creative] run tag @s remove pm_creative"); + run("effect clear @s night_vision"); + // shortcut: assumes feedback was on before launch, as in a new world. + run("gamerule send_command_feedback true"); +} + +fn panel(p: &Player, ticks: u32) -> Vec { + let green = 0x7CFF7C; + let left = (FUEL - ticks.min(FUEL)) / 20; + [ + "PREDATOR".to_string(), + format!("ALT {:.0}", p.y), + format!("FUEL {left}s"), + "Mouse to steer, /missile to detonate".to_string(), + ] + .into_iter() + .map(|text| Line { text, color: green }) + .collect() +} + +impl Predator { + fn impact(&mut self) { + if let State::Flying { from, .. } = &self.state { + detonate(); + self.state = State::Aftermath { + from: from.clone(), + ticks: 0, + }; + } + } +} + +impl Mod for Predator { + fn init() -> Result<(Self, Registration), String> { + let hooks = Registration::new().command("missile").on_tick().on_chat(); + Ok((Predator { state: State::Idle }, hooks)) + } + + fn tick(&mut self, _tick: u64) { + match &mut self.state { + State::Idle => {} + State::Flying { ticks, .. } => { + *ticks += 1; + let ticks = *ticks; + if ticks >= FUEL { + return self.impact(); + } + // The first tick waits for the launch teleport. + if ticks > 1 { + fly(); + } + if let Some(p) = game::get_player() { + hud::set_panel(Corner::TopLeft, &panel(&p, ticks)); + } + } + State::Aftermath { from, ticks } => { + *ticks += 1; + if *ticks >= AFTERMATH { + land(from); + hud::clear(); + self.state = State::Idle; + } + } + } + } + + fn chat(&mut self, text: &str) { + if text.contains(IMPACT) { + self.impact(); + } + } + + fn command(&mut self, _name: &str, _args: &str) -> Result, String> { + match self.state { + State::Idle => { + let from = game::get_player().ok_or("Not in a world")?; + launch(); + self.state = State::Flying { from, ticks: 0 }; + Ok(Some("Missile away".into())) + } + State::Flying { .. } => { + self.impact(); + Ok(None) + } + State::Aftermath { .. } => Err("Wait for the blast to clear".into()), + } + } +} + +horizon_mod_sdk::export_mod!(Predator); diff --git a/crates/horizon-mod-sdk/src/lib.rs b/crates/horizon-mod-sdk/src/lib.rs index 71c36cf80..7efcf20b3 100644 --- a/crates/horizon-mod-sdk/src/lib.rs +++ b/crates/horizon-mod-sdk/src/lib.rs @@ -21,7 +21,7 @@ //! ``` //! //! Every interface but `log` is a capability: list the ones the mod uses in -//! `horizon-mod.toml` (`game-read`, `chat`, `hud`, `storage`). +//! `horizon-mod.toml` (`game-read`, `chat`, `hud`, `storage`, `command`). wit_bindgen::generate!({ path: "wit", @@ -32,7 +32,7 @@ wit_bindgen::generate!({ }); pub use exports::horizon::mods::events::{Guest as __Guest, Registration}; -pub use horizon::mods::{chat, game, hud, log, storage}; +pub use horizon::mods::{chat, command, game, hud, log, storage}; /// The game's calls into a mod. Only the hooks [`Registration`] asks for /// are called; the others may stay as their defaults. diff --git a/crates/horizon-mod-sdk/wit/horizon.wit b/crates/horizon-mod-sdk/wit/horizon.wit index 9f00a12b1..e14913782 100644 --- a/crates/horizon-mod-sdk/wit/horizon.wit +++ b/crates/horizon-mod-sdk/wit/horizon.wit @@ -86,6 +86,14 @@ interface storage { delete: func(key: string); } +/// Capability `command`: vanilla commands run as the player, with the +/// player's permissions, after the hook call returns (8 per call). A mod's +/// own commands are not run this way. +interface command { + /// `line` without its slash; its feedback goes to the chat as if typed. + run: func(line: string); +} + /// What the game calls. Only the hooks `init` asks for are ever called. interface events { record registration { @@ -117,5 +125,6 @@ world horizon-mod { import chat; import hud; import storage; + import command; export events; } diff --git a/crates/horizon-mods/src/manifest.rs b/crates/horizon-mods/src/manifest.rs index a71210883..6f1674e05 100644 --- a/crates/horizon-mods/src/manifest.rs +++ b/crates/horizon-mods/src/manifest.rs @@ -69,16 +69,19 @@ pub enum Capability { Chat, Hud, Storage, + /// Runs vanilla commands as the player, with the player's permissions. + Command, /// New blocks and items from the mod's `data/` (API 0.2): no WIT /// interface, the game reads them before the world loads. Registries, } impl Capability { - pub const ALL: [Capability; 5] = [ + pub const ALL: [Capability; 6] = [ Capability::GameRead, Capability::Chat, Capability::Hud, Capability::Storage, + Capability::Command, Capability::Registries, ]; /// The WIT interface it grants, in `horizon:mods`. @@ -88,6 +91,7 @@ impl Capability { Capability::Chat => "chat", Capability::Hud => "hud", Capability::Storage => "storage", + Capability::Command => "command", Capability::Registries => "registries", } } @@ -98,6 +102,7 @@ impl Capability { Capability::Chat => "show you chat messages", Capability::Hud => "draw text on your screen", Capability::Storage => "save its own settings to disk", + Capability::Command => "run commands as you, with your permissions", Capability::Registries => "add blocks and items to your worlds", } } @@ -128,6 +133,7 @@ impl fmt::Display for Capability { Capability::Chat => "chat", Capability::Hud => "hud", Capability::Storage => "storage", + Capability::Command => "command", Capability::Registries => "registries", }; f.write_str(name) @@ -366,6 +372,10 @@ capabilities = ["game-read", "hud"] storage, Err("uses capability storage, missing from horizon-mod.toml".into()) ); + assert_eq!( + m.check_imports(["horizon:mods/command@0.1.0"]), + Err("uses capability command, missing from horizon-mod.toml".into()) + ); let with_storage = EXAMPLE.replace("\"hud\"", "\"hud\", \"storage\""); let m = Manifest::parse(&with_storage).unwrap(); assert_eq!(m.check_imports(["horizon:mods/storage@0.1.0"]), Ok(())); diff --git a/crates/horizon-mods/src/runtime.rs b/crates/horizon-mods/src/runtime.rs index 9dd2eb54e..07be81909 100644 --- a/crates/horizon-mods/src/runtime.rs +++ b/crates/horizon-mods/src/runtime.rs @@ -23,7 +23,7 @@ pub use bindings::horizon::mods::{ use bindings::{ HorizonMod, exports::horizon::mods::events::Registration, - horizon::mods::{chat, game, hud, log as mod_log, storage}, + horizon::mods::{chat, command, game, hud, log as mod_log, storage}, }; /// A mod's linear memory cap. @@ -67,6 +67,9 @@ pub enum Output { Chat { name: String, text: String }, /// A mod command's feedback, red when `error`. Feedback { text: String, error: bool }, + /// `command.run`: a vanilla command line, without its slash, to run as + /// the player. + Command { line: String }, /// The mod was turned off: it trapped, overran or failed to start. Stopped { name: String, reason: String }, } @@ -514,6 +517,18 @@ impl chat::Host for State { } } } +impl command::Host for State { + fn run(&mut self, line: String) { + let queued = self + .out + .iter() + .filter(|o| matches!(o, Output::Command { .. })) + .count(); + if queued < CHAT_PER_CALL { + self.out.push(Output::Command { line: clip(line) }); + } + } +} impl hud::Host for State { fn set_panel(&mut self, corner: Corner, lines: Vec) { let lines = lines diff --git a/crates/horizon-mods/tests/sandbox.rs b/crates/horizon-mods/tests/sandbox.rs index a246c07b0..c8e4fb883 100644 --- a/crates/horizon-mods/tests/sandbox.rs +++ b/crates/horizon-mods/tests/sandbox.rs @@ -1,4 +1,5 @@ -//! Loads real mods built with the SDK: the coords-hud example and a guest +//! Loads real mods built with the SDK: the coords-hud and predator-missile +//! examples and a guest //! that misbehaves on command. Needs `rustup target add wasm32-unknown-unknown`. use horizon_mods::{ Clock, Corner, Mods, Output, Package, Player, Snapshot, Status, manifest::componentize, @@ -18,7 +19,7 @@ api = "0.1" capabilities = ["chat", "storage"] "#; -/// Builds both mods once and writes their `.hmod` files; returns the folder. +/// Builds the mods once and writes their `.hmod` files; returns the folder. fn mods_dir() -> &'static Path { static DIR: OnceLock = OnceLock::new(); DIR.get_or_init(|| { @@ -31,6 +32,8 @@ fn mods_dir() -> &'static Path { "-p", "coords-hud", "-p", + "predator-missile", + "-p", "horizon-mods-test-guest", "--target-dir", ]) @@ -44,9 +47,13 @@ fn mods_dir() -> &'static Path { let built = target.join("wasm32-unknown-unknown/release"); let dir = target.join("mods"); std::fs::create_dir_all(&dir).unwrap(); - let example = root.join("crates/horizon-mod-sdk/examples/coords-hud/horizon-mod.toml"); + let example = |name: &str| { + let path = format!("crates/horizon-mod-sdk/examples/{name}/horizon-mod.toml"); + std::fs::read_to_string(root.join(path)).unwrap() + }; let mods = [ - ("coords_hud", std::fs::read_to_string(example).unwrap()), + ("coords_hud", example("coords-hud")), + ("predator_missile", example("predator-missile")), ("horizon_mods_test_guest", GUEST_MANIFEST.to_string()), ]; for (name, manifest) in mods { @@ -81,6 +88,36 @@ fn player() -> Snapshot { } } +/// Capability `command`: `/missile` queues vanilla commands, and the chat +/// line its impact check prints sets off the warhead. +#[test] +fn predator_missile_runs_commands() { + let mut mods = load("predator_missile.hmod"); + assert_eq!(mods.entries()[0].status, Status::Running); + mods.set_snapshot(player()); + let commands = |mods: &mut Mods| -> Vec { + mods.drain() + .into_iter() + .filter_map(|o| match o { + Output::Command { line } => Some(line), + _ => None, + }) + .collect() + }; + assert!(mods.command("missile")); + let launch = commands(&mut mods); + assert_eq!(launch.len(), 6); + assert!(launch.contains(&"gamemode spectator".to_string())); + mods.tick(1); + mods.tick(2); + let flight = commands(&mut mods); + assert!(flight.iter().any(|l| l.ends_with("run tp @s ^ ^ ^1.5"))); + mods.chat("Predator: impact"); + let blast = commands(&mut mods); + assert_eq!(blast.len(), 4); + assert!(blast.iter().all(|l| l.contains("summon tnt"))); +} + #[test] fn coords_hud_draws_and_answers() { let mut mods = load("coords_hud.hmod"); diff --git a/docs/api/mods-reference.md b/docs/api/mods-reference.md index ca2fe9d7a..28703e784 100644 --- a/docs/api/mods-reference.md +++ b/docs/api/mods-reference.md @@ -11,11 +11,12 @@ The contract between Horizon and its mods: the WIT package `horizon:mods@0.1.0`, | `chat` | [`chat`](#chat) | show you chat messages | | `hud` | [`hud`](#hud) | draw text on your screen | | `storage` | [`storage`](#storage) | save its own settings to disk | +| `command` | [`command`](#command) | run commands as you, with your permissions | | `registries` | data files (`assets/`, `data/`), no interface | add blocks and items to your worlds | ## World `horizon-mod` -- Imports (what the game offers): `log`, `game`, `chat`, `hud`, `storage` +- Imports (what the game offers): `log`, `game`, `chat`, `hud`, `storage`, `command` - Exports (what the mod provides): `events` ## `log` @@ -117,6 +118,16 @@ Capability `storage`: a per-mod folder of small key/value files, quota'd (docs/m | `set` | `func(key: string, value: list) -> result<_, string>` | Errs on a malformed key or over quota (bytes for the whole mod). | | `delete` | `func(key: string)` | No error if `key` was already absent. | +## `command` + +Access: capability `command`. + +Capability `command`: vanilla commands run as the player, with the player's permissions, after the hook call returns (8 per call). A mod's own commands are not run this way. + +| Function | Signature | What it does | +| --- | --- | --- | +| `run` | `func(line: string)` | `line` without its slash; its feedback goes to the chat as if typed. | + ## `events` Access: exported by the mod: the game calls it. diff --git a/docs/mods.md b/docs/mods.md index d8ea925b3..5d386720b 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -13,7 +13,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3. |---|---|---| | The contract (WIT) | `crates/horizon-mod-sdk/wit/horizon.wit` | 0.1: log, game-read, chat, hud, storage; tick, frame, chat and command hooks | | SDK for mod authors | `crates/horizon-mod-sdk` | 0.1 | -| Example mod | `crates/horizon-mod-sdk/examples/coords-hud` | coordinates, facing and time HUD, `/where`, `/coords` | +| Example mods | `crates/horizon-mod-sdk/examples/coords-hud`, `examples/predator-missile` | coordinates, facing and time HUD, `/where`, `/coords`; `/missile`, a guided missile from vanilla commands | | Runtime | `crates/horizon-mods` | wasmtime 36 LTS, component model | | Authoring tool | `crates/cargo-horizon-mod` | `cargo horizon-mod new / build / run` | | In the game | `crates/horizon-client/src/game/mods.rs`, Options > Mods | client mods | @@ -84,6 +84,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | `chat` | `chat` | `show(text)`: a line only the local player sees, after the mod's name (8 per call) | | `hud` | `hud` | `set-panel(corner, lines)`, `clear()`: up to 16 lines of 256 characters | | `storage` | `storage` | `get(key)`, `set(key, value)`, `delete(key)`: a per-mod folder of up to 64 files, 4 MiB total, at most 1 MiB written per call | +| `command` | `command` | `run(line)`: a vanilla command run as the player, with the player's permissions, after the hook returns (8 per call); mods' own commands are not run | | Hook (export) | When | |---|---| From 372f918e44a14f984e1359001d4285a5ed3958ef Mon Sep 17 00:00:00 2001 From: socksprox Date: Sun, 25 Oct 2026 01:18:53 +0200 Subject: [PATCH 003/238] Multiplayer: keep firework flight duration, boost gliding players Two halves of FireworkRocketEntity on a server were missing: - SetCreativeModeSlot encoded only damage, so a creative-picked rocket lost its minecraft:fireworks component (flight_duration and explosions gone); the wire codec also had no decoder for it or firework_explosion, so a stack carrying them failed to decode at all. The components now follow Fireworks.STREAM_CODEC/FireworkExplosion.STREAM_CODEC both ways (component ids 71/70). - A remote rocket never read DATA_ATTACHED_TO_TARGET, DATA_ID_FIREWORKS_ITEM or DATA_SHOT_AT_ANGLE, so the server-attached boost rocket was an inert marker: attached_to now comes from EntityDataSerializers.OPTIONAL_UNSIGNED_INT (the id plus one), the item feeds the explosion list, and FireworkRocketEntity.tick's isFallFlying boost applies to the local player. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- crates/horizon-client/src/game/fireworks.rs | 9 +- crates/horizon-client/src/game/remote.rs | 48 +++++++++ .../src/game/remote_projectiles.rs | 54 +++++++++- crates/horizon-protocol/src/item.rs | 99 +++++++++++++++++++ 4 files changed, 207 insertions(+), 3 deletions(-) diff --git a/crates/horizon-client/src/game/fireworks.rs b/crates/horizon-client/src/game/fireworks.rs index 85fccc2de..a0aa276b7 100644 --- a/crates/horizon-client/src/game/fireworks.rs +++ b/crates/horizon-client/src/game/fireworks.rs @@ -25,9 +25,12 @@ pub(super) struct Rocket { pub(super) velocity: Vec3, life: i32, lifetime: i32, - explosions: Vec, + pub(super) explosions: Vec, /// Boosting the player (attachedToEntity). pub(super) attached: bool, + /// `DATA_ATTACHED_TO_TARGET`: the server entity the rocket follows; + /// boosting happens when it is the local player. + pub(super) attached_to: Option, /// `DATA_SHOT_AT_ANGLE` (dispensers): constant velocity, exploding on /// the first block it hits. pub(super) angled: bool, @@ -48,6 +51,7 @@ impl Rocket { lifetime: i32::MAX, explosions, attached: false, + attached_to: None, angled: false, item, } @@ -63,7 +67,7 @@ impl Rocket { (self.position, self.velocity) } } -fn fireworks_of(stack: &ItemStack) -> (u8, Vec) { +pub(super) fn fireworks_of(stack: &ItemStack) -> (u8, Vec) { // The item's default component: flight 1, no explosions. stack .data() @@ -89,6 +93,7 @@ impl Game<'_> { lifetime, explosions, attached, + attached_to: None, angled: false, item: stack.clone(), } diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 45ba4bebe..29a7ab46f 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -596,6 +596,28 @@ impl Game<'_> { out.tidy(); Some(out) } + /// A wire `FireworkExplosion` as vanilla's NBT codec writes it + /// (`FireworkExplosion.Shape`'s BY_ID order). + fn fireworks_tag(e: &horizon_protocol::item::FireworkExplosion) -> Tag { + const SHAPES: [&str; 5] = ["small_ball", "large_ball", "star", "creeper", "burst"]; + let ints = |c: &[i32]| Tag::List(c.iter().map(|c| Tag::Int(*c)).collect()); + Tag::compound([ + ( + "shape", + Tag::String( + SHAPES + .get(e.shape.max(0) as usize) + .copied() + .unwrap_or("small_ball") + .into(), + ), + ), + ("colors", ints(&e.colors)), + ("fade_colors", ints(&e.fade)), + ("has_trail", Tag::Byte(i8::from(e.trail))), + ("has_twinkle", Tag::Byte(i8::from(e.twinkle))), + ]) + } /// The item's patched components as vanilla's `components` compound. fn protocol_components(&self, s: &Stack) -> Option { use horizon_protocol::item::{Component, component}; @@ -633,6 +655,14 @@ impl Game<'_> { ("pages", Tag::List(b.pages.clone())), ("resolved", Tag::Byte(i8::from(b.resolved))), ]), + Component::Fireworks { flight, explosions } => Tag::compound([ + ("flight_duration", Tag::Byte(*flight as i8)), + ( + "explosions", + Tag::List(explosions.iter().map(Self::fireworks_tag).collect()), + ), + ]), + Component::FireworkExplosion(e) => Self::fireworks_tag(e), Component::Enchantments(list) => { // Enchantments are a synced (data-driven) registry: the // ids index the server's RegistryData, not the builtin @@ -719,6 +749,24 @@ impl Game<'_> { horizon_protocol::item::Component::Int(s.damage as i32), )); } + if let Some((flight, explosions)) = s.data().and_then(|d| d.fireworks.clone()) { + stack.added.push(( + horizon_protocol::item::component::FIREWORKS, + horizon_protocol::item::Component::Fireworks { + flight: i32::from(flight), + explosions: explosions + .iter() + .map(|e| horizon_protocol::item::FireworkExplosion { + shape: e.shape as i32, + colors: e.colors.iter().map(|c| *c as i32).collect(), + fade: e.fade.iter().map(|c| *c as i32).collect(), + trail: e.trail, + twinkle: e.twinkle, + }) + .collect(), + }, + )); + } Some(stack) } /// A line in the connection's chat, with its styles, click and hover diff --git a/crates/horizon-client/src/game/remote_projectiles.rs b/crates/horizon-client/src/game/remote_projectiles.rs index 58f82e3a9..dbf7f3ccf 100644 --- a/crates/horizon-client/src/game/remote_projectiles.rs +++ b/crates/horizon-client/src/game/remote_projectiles.rs @@ -2,7 +2,7 @@ //! end crystals on a server's client (`ClientPacketListener.handleAddEntity` //! and `handleSetEntityData`): the server simulates them, the client draws //! them where it says and interpolates their moves. -use super::{FallingBlock, Game}; +use super::{FallingBlock, Game, fireworks::fireworks_of, view_vector}; use horizon_client::projectiles::{Projectile, ProjectileKind}; use horizon_core::{geometry::Vec3, position::BlockPos}; use horizon_protocol::entity_data::Value; @@ -117,6 +117,37 @@ impl Game<'_> { pub(super) fn remote_projectile_data(&mut self, id: i32, values: &[(u8, i32, Value)]) { let id = id as u32; self.remote_hook_data(id, values); + if self.rockets.iter().any(|r| r.id == id) { + for (index, _, value) in values { + match (index, value) { + // FireworkRocketEntity.DATA_ID_FIREWORKS_ITEM: its + // explosions for `createFireworks`. + (8, Value::Item(stack)) => { + if let Some(stack) = self.to_stack(stack) + && let Some(r) = self.rockets.iter_mut().find(|r| r.id == id) + { + let (_, explosions) = fireworks_of(&stack); + r.explosions = explosions; + r.item = stack; + } + } + // DATA_ATTACHED_TO_TARGET (OptionalInt: the id plus 1). + (9, Value::Id(v)) => { + let v = (*v > 0).then(|| (*v - 1) as u32); + if let Some(r) = self.rockets.iter_mut().find(|r| r.id == id) { + r.attached_to = v; + } + } + // DATA_SHOT_AT_ANGLE. + (10, Value::Bool(a)) => { + if let Some(r) = self.rockets.iter_mut().find(|r| r.id == id) { + r.angled = *a; + } + } + _ => {} + } + } + } if self.potions.has_thrown(id) { // ThrowableItemProjectile.DATA_ITEM_STACK: the potion it is. for (index, _, value) in values { @@ -199,9 +230,30 @@ impl Game<'_> { let id = t.id; t.remote_step(|at| step(id, at)); } + // FireworkRocketEntity.tick: a rocket attached to the gliding local + // player boosts it client-side too (attachedToEntity.isFallFlying). + let me = self + .remote + .as_ref() + .and_then(|r| u32::try_from(r.entity_id).ok()); for r in &mut self.rockets { let id = r.id; r.remote_step(|at| step(id, at)); + if me.is_some() && r.attached_to == me { + if let Some(p) = self.player.as_mut() + && p.fall_flying + { + let look = view_vector(self.yaw, self.pitch); + let m = p.velocity; + p.velocity = Vec3::new( + m.x + look[0] * 0.1 + (look[0] * 1.5 - m.x) * 0.5, + m.y + look[1] * 0.1 + (look[1] * 1.5 - m.y) * 0.5, + m.z + look[2] * 0.1 + (look[2] * 1.5 - m.z) * 0.5, + ); + r.position = p.position; + r.velocity = p.velocity; + } + } } for c in &mut self.crystals { c.time += 1; diff --git a/crates/horizon-protocol/src/item.rs b/crates/horizon-protocol/src/item.rs index 650fa4baa..753fc7349 100644 --- a/crates/horizon-protocol/src/item.rs +++ b/crates/horizon-protocol/src/item.rs @@ -35,6 +35,8 @@ pub mod component { pub const CHARGED_PROJECTILES: i32 = 51; pub const WRITABLE_BOOK_CONTENT: i32 = 56; pub const WRITTEN_BOOK_CONTENT: i32 = 57; + pub const FIREWORK_EXPLOSION: i32 = 70; + pub const FIREWORKS: i32 = 71; } /// A written book's content (`WrittenBookContent.STREAM_CODEC`): the @@ -78,6 +80,48 @@ pub enum Component { WrittenBook(WrittenBook), /// `ChargedProjectiles`: a loaded crossbow's ammunition, as templates. Stacks(Vec), + /// `Fireworks`: flight duration and its explosions. + Fireworks { + flight: i32, + explosions: Vec, + }, + /// `FireworkExplosion` (a firework star). + FireworkExplosion(FireworkExplosion), +} + +/// `FireworkExplosion.STREAM_CODEC`: shape id, colors, fade colors, trail +/// and twinkle (FireworkExplosion.Shape's BY_ID order). +#[derive(Clone, Debug, PartialEq)] +pub struct FireworkExplosion { + pub shape: i32, + pub colors: Vec, + pub fade: Vec, + pub trail: bool, + pub twinkle: bool, +} +impl FireworkExplosion { + fn read(r: &mut Reader) -> Result { + let ints = |r: &mut Reader| -> Result> { + let n = r.count(1024)?; + (0..n).map(|_| r.i32()).collect() + }; + Ok(Self { + shape: r.varint()?, + colors: ints(r)?, + fade: ints(r)?, + trail: r.bool()?, + twinkle: r.bool()?, + }) + } + fn write(&self, w: &mut Writer) { + w.varint(self.shape); + w.varint(self.colors.len() as i32); + self.colors.iter().for_each(|c| w.i32(*c)); + w.varint(self.fade.len() as i32); + self.fade.iter().for_each(|c| w.i32(*c)); + w.bool(self.trail); + w.bool(self.twinkle); + } } /// `Filterable.streamCodec`: the raw value, then an optional filtered one @@ -224,6 +268,17 @@ impl Component { hidden: (0..n).map(|_| r.varint()).collect::>()?, } } + FIREWORKS => { + let flight = r.varint()?; + let n = r.count(256)?; + Self::Fireworks { + flight, + explosions: (0..n) + .map(|_| FireworkExplosion::read(r)) + .collect::>()?, + } + } + FIREWORK_EXPLOSION => Self::FireworkExplosion(FireworkExplosion::read(r)?), other => return Err(Error::new(format!("unsupported item component {other}"))), }) } @@ -294,6 +349,12 @@ impl Component { w.varint(stacks.len() as i32); stacks.iter().for_each(|s| Template::write(s, w)); } + Self::Fireworks { flight, explosions } => { + w.varint(*flight); + w.varint(explosions.len() as i32); + explosions.iter().for_each(|e| e.write(w)); + } + Self::FireworkExplosion(e) => e.write(w), Self::WritableBook(pages) => { w.varint(pages.len() as i32); for page in pages { @@ -544,6 +605,44 @@ mod tests { round::, HashedSlot>(None); } + #[test] + fn fireworks_components_round_trip() { + let mut rocket = Stack::new(42, 1); + rocket.added.push(( + component::FIREWORKS, + Component::Fireworks { + flight: 3, + explosions: vec![ + FireworkExplosion { + shape: 4, + colors: vec![0xff0000, 0x00ff00], + fade: vec![0x0000ff], + trail: true, + twinkle: false, + }, + FireworkExplosion { + shape: 0, + colors: vec![], + fade: vec![], + trail: false, + twinkle: true, + }, + ], + }, + )); + rocket.added.push(( + component::FIREWORK_EXPLOSION, + Component::FireworkExplosion(FireworkExplosion { + shape: 2, + colors: vec![7], + fade: vec![], + trail: false, + twinkle: false, + }), + )); + round::, Slot>(Some(rocket)); + } + #[test] fn charged_projectiles_are_item_stack_templates() { // A crossbow (id 7) loaded with one arrow (id 9): ChargedProjectiles From 0f9de6dbb0ce0e1aeef50e5b960a3040357a6132 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 01:39:28 +0200 Subject: [PATCH 004/238] Shader packs, clean room 14: terrain drawn by the pack's program With a pack whose gbuffers_terrain (or a fallback) loads, solid and cutout terrain is drawn by it into the pack's buffers, in place, between prepare and deferred, and Horizon's own solid terrain draw is skipped. - depthtex0 is now a Depth32Float attachment: the blit writes the world's depth there (GL window depth, GL rows), terrain tests and writes against it, so entities and terrain occlude each other; later programs sample it as before. - The vertex glue reads Horizon's TerrainVertex on both draw paths (instanced, and flat with the page tables in bind group 1) and gives atlas coordinates, lightmap coordinates in 0-240, the vertex colour, mc_midTexCoord at the sprite's centre and mc_Entity = -1. Normals, tangents and block ids come later. - texture/gtexture/tex sample the block atlas, lightmap the lightmap; atlasSize is set. - Occlusion culling is off while a pack draws terrain (it reads the depth of Horizon's terrain draw), and flat draws stay on. - Uniforms a program reads that are not provided yet are reported. - Program building is shared by quads and terrain (build_pass, bind). - terrain-test: Horizon's own pack for this slice. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/src/game/shader_packs.rs | 3 +- crates/horizon-client/src/gfx.rs | 106 ++- crates/horizon-client/src/gfx/flat.rs | 3 +- crates/horizon-client/src/gfx/pack_atlas.rs | 69 +- crates/horizon-client/src/gfx/pack_post.rs | 716 +++++++++++------- crates/horizon-client/src/gfx/pack_post.wgsl | 7 +- .../horizon-client/src/gfx/pack_uniforms.rs | 5 + .../tests/packs/terrain-test/README.md | 8 + .../packs/terrain-test/shaders/composite.fsh | 19 + .../packs/terrain-test/shaders/composite.vsh | 8 + .../terrain-test/shaders/gbuffers_terrain.fsh | 22 + .../terrain-test/shaders/gbuffers_terrain.vsh | 16 + 12 files changed, 702 insertions(+), 280 deletions(-) create mode 100644 crates/horizon-shaders/tests/packs/terrain-test/README.md create mode 100644 crates/horizon-shaders/tests/packs/terrain-test/shaders/composite.fsh create mode 100644 crates/horizon-shaders/tests/packs/terrain-test/shaders/composite.vsh create mode 100644 crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.fsh create mode 100644 crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.vsh diff --git a/crates/horizon-client/src/game/shader_packs.rs b/crates/horizon-client/src/game/shader_packs.rs index 04af297b6..d819eddef 100644 --- a/crates/horizon-client/src/game/shader_packs.rs +++ b/crates/horizon-client/src/game/shader_packs.rs @@ -42,7 +42,8 @@ impl Game<'_> { ); } let folder = dimension_folder(&pack, dimension); - let post = load_post(&pack, &folder, &properties, ¯os, None); + let terrain = renderer.pack_terrain_inputs(); + let post = load_post(&pack, &folder, &properties, ¯os, terrain.as_ref()); if let Some((programs, notes)) = renderer.set_shader_pack(dimension, Some(post)) { for note in notes { diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 9f05adb65..c117b190f 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -471,6 +471,8 @@ pub struct Renderer { pack_post: Option<(i32, Option)>, /// The block sprites as one atlas, while a shader pack is loaded. pack_atlas: Option, + /// The pack's terrain program reads flat draws' vertices (`pack_terrain_inputs`). + pack_terrain_flat: bool, pub hud: HudRenderer, /// Vanilla's menu background blur, made on first use. menu_blur: Option, @@ -2319,6 +2321,7 @@ impl Renderer { world_post, pack_post: None, pack_atlas: None, + pack_terrain_flat: false, hud, menu_blur: None, info, @@ -2744,17 +2747,50 @@ impl Renderer { dimension: i32, pack: Option, ) -> Option<(Vec, Vec)> { + if pack.is_none() { + self.pack_atlas = None; + } + let instance = wgpu::vertex_attr_array![5=>Float32x4]; + let vertex = wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as u64, + step_mode: wgpu::VertexStepMode::Vertex, + attributes: &TERRAIN_ATTRIBUTES, + }; + let instances = wgpu::VertexBufferLayout { + array_stride: 16, + step_mode: wgpu::VertexStepMode::Instance, + attributes: &instance, + }; + let buffers = [Some(vertex), Some(instances)]; + let flat = self.pack_terrain_flat; let post = pack.map(|post| { + let world = pack_post::World { + atlas: self.pack_atlas.as_ref().map(|a| (a.view.clone(), a.size())), + lightmap: self.lightmap_view.clone(), + terrain: self.pack_atlas.as_ref().map(|_| pack_post::Geometry { + buffers: if flat { &buffers[..1] } else { &buffers }, + table: self.flat.as_ref().filter(|_| flat).map(|f| &f.draw_layout), + }), + }; pack_post::PackPost::new( &self.device, &self.queue, post, self.config.format.remove_srgb_suffix(), + world, ) }); - if post.is_none() { - self.pack_atlas = None; - } else if self.pack_atlas.is_none() { + let report = post.as_ref().map(|p| { + let programs = p.programs().into_iter().map(str::to_owned).collect(); + (programs, p.notes.clone()) + }); + self.pack_post = Some((dimension, post)); + report + } + /// What a shader pack's terrain program reads from Horizon's terrain vertices, in the + /// pack's block atlas (made here when missing); `None` when the atlas does not fit. + pub fn pack_terrain_inputs(&mut self) -> Option { + if self.pack_atlas.is_none() { let mut encoder = self.device.create_command_encoder(&Default::default()); let layers = self .sprite_textures @@ -2771,12 +2807,13 @@ impl Renderer { ); self.queue.submit([encoder.finish()]); } - let report = post.as_ref().map(|p| { - let programs = p.programs().into_iter().map(str::to_owned).collect(); - (programs, p.notes.clone()) - }); - self.pack_post = Some((dimension, post)); - report + // Flat draws whenever the device has them (`render` keeps them on with a pack). + self.pack_terrain_flat = self.flat.is_some() && self.flat_on; + Some( + self.pack_atlas + .as_ref()? + .terrain_inputs(self.pack_terrain_flat), + ) } /// The dimension the shader pack was last set for. pub fn shader_pack_dimension(&self) -> Option { @@ -3184,11 +3221,23 @@ impl Renderer { // Occlusion culling leaves the sections its last result did not see // to phase 2, after the others, with instance counts of 0. Whether // the frame has draws enough goes by the last one's. + // A shader pack drawing terrain draws all of it: occlusion culling reads the depth of + // Horizon's own terrain draw. + let pack_terrain = !frame.title + && self + .pack_post + .as_ref() + .and_then(|p| p.1.as_ref()) + .is_some_and(|p| p.has_terrain()); let mut cull = self .cull .as_mut() .filter(|c| { - self.cull_on && !frame.title && self.multi_draw && self.section_draws >= c.min_draws + self.cull_on + && !frame.title + && !pack_terrain + && self.multi_draw + && self.section_draws >= c.min_draws }) .and_then(|cull| cull.begin(&self.device).then_some(cull)); self.phase2_draws.clear(); @@ -3591,7 +3640,9 @@ impl Renderer { } let flat_frame = match &mut self.flat { Some(flat) - if self.flat_on && !self.draws.is_empty() && self.draws.len() >= flat.min_draws => + if self.flat_on + && !self.draws.is_empty() + && (self.draws.len() >= flat.min_draws || pack_terrain) => { let sections = &self.sections; let lods = &self.lods; @@ -3651,6 +3702,8 @@ impl Renderer { } // The world's targets: the screen, or the scaled target of `post`. let pack_post = !frame.title && self.pack_post.as_ref().is_some_and(|p| p.1.is_some()); + // The pack's program reads the vertices this frame's draws bind. + let pack_draws_terrain = pack_terrain && flat_frame == self.pack_terrain_flat; let scaled = self.world_post.prepare( &self.device, [self.config.width, self.config.height], @@ -3739,7 +3792,14 @@ impl Renderer { pass.draw(0..3, 0..1); } } - match self.flat.as_ref().filter(|_| flat_frame) { + match self + .flat + .as_ref() + .filter(|_| flat_frame && !pack_draws_terrain) + { + _ if pack_draws_terrain => { + pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); + } Some(flat) => { pass.set_pipeline(&flat.solid); let vertices = |p| self.arena.buffer(p); @@ -3947,9 +4007,27 @@ impl Renderer { } if let Some((color, depth)) = &scaled && pack_post - && let Some((_, Some(post))) = &mut self.pack_post + && let Some((dimension, Some(mut post))) = self.pack_post.take() { let size = self.world_post.size().unwrap_or([1, 1]); + let flat = self.flat.as_ref().filter(|_| flat_frame); + let mut terrain = |rp: &mut wgpu::RenderPass<'_>| { + if !pack_draws_terrain { + return; + } + match flat { + Some(flat) => { + flat.draw(rp, &flat.calls[..flat.solid_calls], |p| { + self.arena.buffer(p) + }); + } + None => { + rp.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); + rp.set_vertex_buffer(1, self.instances.slice(..)); + self.draw_sections(rp, &self.indirect, 0, 0, solid_draws); + } + } + }; post.run( &self.device, &self.queue, @@ -3961,7 +4039,9 @@ impl Renderer { &frame.pack, size, [self.config.width, self.config.height], + &mut terrain, ); + self.pack_post = Some((dimension, Some(post))); } else if scaled.is_some() { self.world_post.finish(&mut encoder, &view_target); } diff --git a/crates/horizon-client/src/gfx/flat.rs b/crates/horizon-client/src/gfx/flat.rs index f6823c5d8..4c337025a 100644 --- a/crates/horizon-client/src/gfx/flat.rs +++ b/crates/horizon-client/src/gfx/flat.rs @@ -73,7 +73,8 @@ pub struct Flat { phase2_call_data: Vec<[u32; 5]>, /// The copy of phase 2's draws, and its ranges. phase2_group: Option<(wgpu::BindGroup, u32)>, - draw_layout: wgpu::BindGroupLayout, + /// The layout of a page's bind group 1 (shader packs' terrain pipelines share it). + pub(super) draw_layout: wgpu::BindGroupLayout, blocks_per_page: u64, pages: Vec>, free_ids: Vec, diff --git a/crates/horizon-client/src/gfx/pack_atlas.rs b/crates/horizon-client/src/gfx/pack_atlas.rs index c90fa6ac1..011dff379 100644 --- a/crates/horizon-client/src/gfx/pack_atlas.rs +++ b/crates/horizon-client/src/gfx/pack_atlas.rs @@ -3,7 +3,6 @@ //! layer is copied into a grid, row by row from the top left, and an animated layer again //! whenever its frame changes. -#[expect(dead_code, reason = "the gbuffers pass binds it (next slice)")] pub(super) struct PackAtlas { texture: wgpu::Texture, pub view: wgpu::TextureView, @@ -64,10 +63,6 @@ impl PackAtlas { } /// The atlas's size in pixels (`atlasSize`). - #[cfg_attr( - not(test), - expect(dead_code, reason = "the gbuffers pass reads it (next slice)") - )] pub fn size(&self) -> [u32; 2] { [self.texture.width(), self.texture.height()] } @@ -118,6 +113,70 @@ impl PackAtlas { } } +impl PackAtlas { + /// The vertex inputs of a gbuffers program drawing terrain (`TerrainVertex`, terrain.wgsl's + /// `vs_terrain` or, for `flat` draws, `vs_terrain_flat`), in atlas coordinates. + pub fn terrain_inputs(&self, flat: bool) -> horizon_shaders::GbuffersInputs { + let mut declarations = String::from( + "layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in uvec2 hz_v_uv;\n\ + layout(location = 2) in uint hz_v_layer;\nlayout(location = 3) in vec4 hz_v_color;\n\ + layout(location = 4) in uvec2 hz_v_light;\n", + ); + let origin = if flat { + declarations.push_str( + "layout(set = 1, binding = 0) readonly buffer HzBlockSlots { uint hz_block_slots[]; };\n\ + layout(set = 1, binding = 1) readonly buffer HzSlotOrigins { vec4 hz_slot_origins[]; };\n", + ); + "hz_slot_origins[hz_block_slots[uint(gl_VertexIndex) >> 8u]]" + } else { + declarations.push_str("layout(location = 5) in vec4 hz_v_origin;\n"); + "hz_v_origin" + }; + let [width, height] = self.size(); + let scale = [ + self.sprite as f32 / width as f32, + self.sprite as f32 / height as f32, + ]; + let columns = self.columns; + // UVs past a sprite (models up to 2, distant tiles) cannot repeat in an atlas: clamped. + let body = format!( + " vec4 hz_origin = {origin};\n \ + uint hz_layer = hz_v_layer & 32767u;\n \ + hz_cell = vec2(float(hz_layer % {columns}u), float(hz_layer / {columns}u));\n \ + hz_scale = vec2({:?}, {:?});\n \ + hz_Vertex = vec4(hz_v_pos + hz_origin.xyz, 1.0);\n \ + hz_MultiTexCoord0 = vec4((hz_cell + clamp(vec2(hz_v_uv) * hz_origin.w, 0.0, 1.0)) * hz_scale, 0.0, 1.0);\n \ + hz_MultiTexCoord1 = vec4(float(hz_v_light.y), float(hz_v_light.x), 0.0, 1.0);\n \ + hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \ + hz_Color = hz_v_color;\n \ + hz_Normal = vec3(0.0, 1.0, 0.0);\n", + scale[0], scale[1], + ); + declarations.push_str("vec2 hz_cell; vec2 hz_scale;\n"); + let attributes = [ + ("mc_Entity", "vec4(-1.0, -1.0, 0.0, 0.0)"), + ( + "mc_midTexCoord", + "vec4((hz_cell + 0.5) * hz_scale, 0.0, 1.0)", + ), + ("at_tangent", "vec4(1.0, 0.0, 0.0, 1.0)"), + ("vaPosition", "hz_Vertex.xyz"), + ("vaUV0", "hz_MultiTexCoord0.xy"), + ("vaUV2", "hz_MultiTexCoord1.xy"), + ("vaColor", "hz_Color"), + ("vaNormal", "hz_Normal"), + ]; + horizon_shaders::GbuffersInputs { + declarations, + body, + attributes: attributes + .iter() + .map(|&(n, v)| (n.to_owned(), v.to_owned())) + .collect(), + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index d5aa9fb7c..81a517dba 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -29,6 +29,9 @@ enum Source { Dummy, /// A 1×1 depth of 1.0 behind a comparison sampler, until shadow maps exist: no shadow. Shadow, + /// The block atlas (pack_atlas.rs), in gbuffers programs. + Atlas, + Lightmap, } struct Binding { @@ -66,9 +69,22 @@ struct Targets { depth: wgpu::TextureView, } +/// What a pack's gbuffers programs read and draw from the game's world. +pub(super) struct World<'a> { + /// The block atlas and its size. + pub atlas: Option<(wgpu::TextureView, [u32; 2])>, + pub lightmap: wgpu::TextureView, + /// Terrain's geometry, when the pack draws it. + pub terrain: Option>, +} + pub struct PackPost { pub notes: Vec, passes: Vec, + /// What draws terrain (`gbuffers_terrain` or its fallback), between `prepare` and `deferred`. + terrain: Option, + atlas: Option, + lightmap: wgpu::TextureView, deferred_pre: u16, composite_pre: u16, formats: [wgpu::TextureFormat; BUFFERS], @@ -85,6 +101,7 @@ pub struct PackPost { linear: wgpu::Sampler, nearest: wgpu::Sampler, repeat: wgpu::Sampler, + nearest_repeat: wgpu::Sampler, blit: (wgpu::RenderPipeline, wgpu::BindGroupLayout), /// The blit also writes depthtex0 (some program reads it) and clears these buffers. blit_depth: bool, @@ -133,6 +150,8 @@ fn source(name: &str) -> Source { match name { "depthtex0" | "gdepthtex" | "depthtex1" | "depthtex2" => Source::Depth, "noisetex" => Source::Noise, + "texture" | "gtexture" | "tex" => Source::Atlas, + "lightmap" => Source::Lightmap, n => horizon_shaders::buffer_index(n).map_or(Source::Dummy, Source::Color), } } @@ -228,6 +247,7 @@ fn helper( entry: &str, entries: &[wgpu::BindGroupLayoutEntry], targets: &[Option], + depth: Option, ) -> (wgpu::RenderPipeline, wgpu::BindGroupLayout) { let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("Shader pack helpers"), @@ -252,7 +272,7 @@ fn helper( buffers: &[], }, primitive: Default::default(), - depth_stencil: None, + depth_stencil: depth, multisample: Default::default(), fragment: Some(wgpu::FragmentState { module: &module, @@ -306,6 +326,211 @@ fn attach( }) } +/// The game's geometry a gbuffers program draws: its vertex buffers and, for flat draws +/// (flat.rs), the layout of their bind group 1. +pub(super) struct Geometry<'a> { + pub buffers: &'a [Option>], + pub table: Option<&'a wgpu::BindGroupLayout>, +} + +/// The pass of program `p`: a quad, or `geometry` drawn by the game. `None` (with a note) when +/// wgpu refuses it or it writes nothing. +fn build_pass( + device: &wgpu::Device, + p: horizon_shaders::PostProgram, + formats: &[wgpu::TextureFormat; BUFFERS], + screen: wgpu::TextureFormat, + notes: &mut Vec, + geometry: Option<&Geometry<'_>>, +) -> Option { + let features = device.features(); + let mask = |flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0); + let interface = &p.program.interface; + let mut entries = vec![entry( + 0, + wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }, + )]; + let mut samplers = Vec::new(); + let mut reads = 0u16; + for s in &interface.samplers { + let mut src = source(&s.name); + if src == Source::Atlas && geometry.is_none() { + src = Source::Dummy; + } + let float = wgpu::TextureSampleType::Float { filterable: true }; + let mut sample_type = match src { + Source::Color(n) => formats[n] + .sample_type(None, Some(features)) + .unwrap_or(float), + Source::Depth => wgpu::TextureSampleType::Float { filterable: false }, + // Unfilterable on WebGPU-level devices; the lightmap is sampled at texel centres. + Source::Lightmap => float, + _ => float, + }; + let int = !matches!(sample_type, wgpu::TextureSampleType::Float { .. }); + if s.ty == "sampler2DShadow" { + (src, sample_type) = (Source::Shadow, wgpu::TextureSampleType::Depth); + } + let wanted = match s.ty.as_str() { + "sampler2DShadow" => true, + "sampler2D" => !int, + "isampler2D" => sample_type == wgpu::TextureSampleType::Sint, + "usampler2D" => sample_type == wgpu::TextureSampleType::Uint, + _ => false, + }; + if !wanted { + notes.push(format!( + "{}: `{} {}` is not provided yet", + p.name, s.ty, s.name + )); + (src, sample_type) = (Source::Dummy, float); + } + let filtering = sample_type == float; + if let Source::Color(n) = src { + reads |= 1 << n; + } + entries.push(texture_entry(s.binding, sample_type)); + entries.push(entry( + s.binding + 1, + wgpu::BindingType::Sampler(if src == Source::Shadow { + wgpu::SamplerBindingType::Comparison + } else if filtering { + wgpu::SamplerBindingType::Filtering + } else { + wgpu::SamplerBindingType::NonFiltering + }), + )); + samplers.push(Binding { + source: src, + binding: s.binding, + filtering, + }); + } + let final_pass = p.group == Group::Final; + let mut targets = Vec::new(); + let mut writes = Vec::new(); + for &location in &interface.outputs { + let l = location as usize; + if targets.len() <= l { + targets.resize(l + 1, None); + } + if final_pass { + if location == 0 { + targets[0] = target(screen); + } + } else if let Some(&buffer) = p.buffers.get(l) { + targets[l] = target(formats[buffer as usize]); + writes.push((location, buffer as usize)); + } + } + if !final_pass && writes.is_empty() { + notes.push(format!("{}: writes no buffer; skipped", p.name)); + return None; + } + let written = writes.iter().fold(0u16, |m, w| m | 1 << w.1); + let kept = p + .flips + .iter() + .filter(|f| !f.1) + .fold(0u16, |m, f| m | 1 << f.0); + // Gbuffers programs write in place (6.1.6). + let flip = if final_pass || geometry.is_some() { + 0 + } else { + (written & !kept) | mask(&p.flips) + }; + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some(&p.name), + entries: &entries, + }); + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some(&p.name), + bind_group_layouts: &[Some(&layout), geometry.and_then(|g| g.table)], + ..Default::default() + }); + let created = checked(device, || { + let module = |m: naga::Module| { + device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some(&p.name), + source: wgpu::ShaderSource::Naga(std::borrow::Cow::Owned(m)), + }) + }; + let (vs, fs) = (module(p.program.vertex), module(p.program.fragment)); + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some(&p.name), + layout: Some(&pipeline_layout), + vertex: wgpu::VertexState { + module: &vs, + entry_point: Some("main"), + compilation_options: Default::default(), + buffers: geometry.map_or(&[], |g| g.buffers), + }, + primitive: Default::default(), + depth_stencil: geometry.map(|_| wgpu::DepthStencilState { + format: wgpu::TextureFormat::Depth32Float, + depth_write_enabled: Some(true), + depth_compare: Some(wgpu::CompareFunction::LessEqual), + stencil: Default::default(), + bias: Default::default(), + }), + multisample: Default::default(), + fragment: Some(wgpu::FragmentState { + module: &fs, + entry_point: Some("main"), + compilation_options: Default::default(), + targets: &targets, + }), + multiview_mask: None, + cache: None, + }) + }); + let pipeline = match created { + Ok(p) => p, + Err(e) => { + notes.push(format!("{}: skipped: {e}", p.name)); + return None; + } + }; + let interface = &p.program.interface; + let missing: Vec<&str> = interface + .uniforms + .iter() + .filter(|u| !u.name.starts_with("hz_") && pack_uniforms::index(&u.name).is_none()) + .map(|u| u.name.as_str()) + .collect(); + if !missing.is_empty() { + notes.push(format!("{}: zero for now: {}", p.name, missing.join(" "))); + } + Some(Pass { + ubo: device.create_buffer(&wgpu::BufferDescriptor { + label: Some(&p.name), + size: u64::from(interface.uniform_size.max(16)), + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }), + block: vec![0; interface.uniform_size as usize], + uniforms: interface + .uniforms + .iter() + .map(|u| (pack_uniforms::index(&u.name), u.ty.clone(), u.offset)) + .collect(), + name: p.name, + group: p.group, + pipeline, + layout, + samplers, + writes, + flip, + reads, + written, + groups: HashMap::new(), + }) +} + impl PackPost { /// Builds the pipelines of `post` for a screen of `screen` format. pub fn new( @@ -313,6 +538,7 @@ impl PackPost { queue: &wgpu::Queue, post: Post, screen: wgpu::TextureFormat, + world: World<'_>, ) -> Self { let features = device.features(); let formats: [wgpu::TextureFormat; BUFFERS] = @@ -329,205 +555,56 @@ impl PackPost { let mask = |flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0); let mut used = 1u16; - let mut passes = Vec::new(); - for p in post.programs { - let interface = &p.program.interface; - let mut entries = vec![entry( - 0, - wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, - has_dynamic_offset: false, - min_binding_size: None, - }, - )]; - let mut samplers = Vec::new(); - let mut reads = 0u16; - for s in &interface.samplers { - let mut src = source(&s.name); - let float = wgpu::TextureSampleType::Float { filterable: true }; - let mut sample_type = match src { - Source::Color(n) => formats[n] - .sample_type(None, Some(features)) - .unwrap_or(float), - Source::Depth => wgpu::TextureSampleType::Float { filterable: false }, - _ => float, - }; - let int = !matches!(sample_type, wgpu::TextureSampleType::Float { .. }); - if s.ty == "sampler2DShadow" { - (src, sample_type) = (Source::Shadow, wgpu::TextureSampleType::Depth); - } - let wanted = match s.ty.as_str() { - "sampler2DShadow" => true, - "sampler2D" => !int, - "isampler2D" => sample_type == wgpu::TextureSampleType::Sint, - "usampler2D" => sample_type == wgpu::TextureSampleType::Uint, - _ => false, - }; - if !wanted { - notes.push(format!( - "{}: `{} {}` is not provided yet", - p.name, s.ty, s.name - )); - (src, sample_type) = (Source::Dummy, float); - } - let filtering = sample_type == float; - if let Source::Color(n) = src { - reads |= 1 << n; - } - entries.push(texture_entry(s.binding, sample_type)); - entries.push(entry( - s.binding + 1, - wgpu::BindingType::Sampler(if src == Source::Shadow { - wgpu::SamplerBindingType::Comparison - } else if filtering { - wgpu::SamplerBindingType::Filtering - } else { - wgpu::SamplerBindingType::NonFiltering - }), - )); - samplers.push(Binding { - source: src, - binding: s.binding, - filtering, - }); - } - let final_pass = p.group == Group::Final; - let mut targets = Vec::new(); - let mut writes = Vec::new(); - for &location in &interface.outputs { - let l = location as usize; - if targets.len() <= l { - targets.resize(l + 1, None); - } - if final_pass { - if location == 0 { - targets[0] = target(screen); - } - } else if let Some(&buffer) = p.buffers.get(l) { - targets[l] = target(formats[buffer as usize]); - writes.push((location, buffer as usize)); - } - } - if !final_pass && writes.is_empty() { - notes.push(format!("{}: writes no buffer; skipped", p.name)); - continue; - } - let written = writes.iter().fold(0u16, |m, w| m | 1 << w.1); - let kept = p - .flips - .iter() - .filter(|f| !f.1) - .fold(0u16, |m, f| m | 1 << f.0); - let flip = if final_pass { - 0 - } else { - (written & !kept) | mask(&p.flips) - }; - used |= reads | written | flip; - let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - label: Some(&p.name), - entries: &entries, - }); - let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - label: Some(&p.name), - bind_group_layouts: &[Some(&layout)], - ..Default::default() - }); - let created = checked(device, || { - let module = |m: naga::Module| { - device.create_shader_module(wgpu::ShaderModuleDescriptor { - label: Some(&p.name), - source: wgpu::ShaderSource::Naga(std::borrow::Cow::Owned(m)), - }) - }; - let (vs, fs) = (module(p.program.vertex), module(p.program.fragment)); - device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - label: Some(&p.name), - layout: Some(&pipeline_layout), - vertex: wgpu::VertexState { - module: &vs, - entry_point: Some("main"), - compilation_options: Default::default(), - buffers: &[], - }, - primitive: Default::default(), - depth_stencil: None, - multisample: Default::default(), - fragment: Some(wgpu::FragmentState { - module: &fs, - entry_point: Some("main"), - compilation_options: Default::default(), - targets: &targets, - }), - multiview_mask: None, - cache: None, - }) - }); - let pipeline = match created { - Ok(p) => p, - Err(e) => { - notes.push(format!("{}: skipped: {e}", p.name)); - continue; - } - }; - let interface = &p.program.interface; - passes.push(Pass { - ubo: device.create_buffer(&wgpu::BufferDescriptor { - label: Some(&p.name), - size: u64::from(interface.uniform_size.max(16)), - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, - mapped_at_creation: false, - }), - block: vec![0; interface.uniform_size as usize], - uniforms: interface - .uniforms - .iter() - .map(|u| (pack_uniforms::index(&u.name), u.ty.clone(), u.offset)) - .collect(), - name: p.name, - group: p.group, - pipeline, - layout, - samplers, - writes, - flip, - reads, - written, - groups: HashMap::new(), - }); + let mut build = |p: horizon_shaders::PostProgram, geometry: Option<&Geometry<'_>>| { + let pass = build_pass(device, p, &formats, screen, &mut notes, geometry)?; + used |= pass.reads | pass.written | pass.flip; + Some(pass) + }; + let terrain = post + .terrain + .zip(world.terrain.as_ref()) + .and_then(|(p, g)| build(p, Some(g))); + let passes: Vec = post + .programs + .into_iter() + .filter_map(|p| build(p, None)) + .collect(); + if let Some(t) = &terrain { + notes.push(format!("terrain draws with {}", t.name)); } // A clear only matters when a program reads the buffer before one writes all of it - // (composite passes cover their whole target); the others are skipped. + // (composite passes cover their whole target, terrain does not); the others are skipped. let mut cleared = Vec::new(); for b in 1..BUFFERS { let bit = 1u16 << b; - let first = passes - .iter() - .find(|p| (p.reads | p.flip | p.written) & bit != 0); - if clears[b].is_some() - && first.is_some_and(|p| p.reads & bit != 0 || p.written & bit == 0) - { + let touched = |p: &&Pass| (p.reads | p.flip | p.written) & bit != 0; + let first = terrain.iter().find(touched).map(|_| true).or_else(|| { + let p = passes.iter().find(touched)?; + Some(p.reads & bit != 0 || p.written & bit == 0) + }); + if clears[b].is_some() && first == Some(true) { cleared.push(b); } } - let blit_depth = passes - .iter() - .any(|p| p.samplers.iter().any(|s| s.source == Source::Depth)); - let room = 8 - 1 - usize::from(blit_depth); + // Terrain tests against the world's depth (entities...), so the blit copies it then. + let blit_depth = terrain.is_some() + || passes + .iter() + .any(|p| p.samplers.iter().any(|s| s.source == Source::Depth)); + let room = 8 - 1; let other_clears = cleared.split_off(room.min(cleared.len())); let float = wgpu::TextureSampleType::Float { filterable: true }; - let mut blit_targets = vec![target(formats[0])]; - if blit_depth { - blit_targets.push(target(wgpu::TextureFormat::R32Float)); - } + let blit_targets = std::iter::once(target(formats[0])); // Cleared by the pass's load operation; the shader writes nothing there. - blit_targets.extend(cleared.iter().map(|&b| { - Some(wgpu::ColorTargetState { - format: formats[b], - blend: None, - write_mask: wgpu::ColorWrites::empty(), - }) - })); + let blit_targets: Vec<_> = blit_targets + .chain(cleared.iter().map(|&b| { + Some(wgpu::ColorTargetState { + format: formats[b], + blend: None, + write_mask: wgpu::ColorWrites::empty(), + }) + })) + .collect(); let blit = helper( device, "vs_full", @@ -541,6 +618,14 @@ impl PackPost { texture_entry(1, wgpu::TextureSampleType::Depth), ], &blit_targets, + // GL's window depth, written for depthtex0 and the terrain's depth test. + blit_depth.then_some(wgpu::DepthStencilState { + format: wgpu::TextureFormat::Depth32Float, + depth_write_enabled: Some(true), + depth_compare: Some(wgpu::CompareFunction::Always), + stencil: Default::default(), + bias: Default::default(), + }), ); let copy = helper( device, @@ -554,11 +639,15 @@ impl PackPost { ), ], &[target(screen)], + None, ); let white = [255u8; 4]; Self { notes, passes, + terrain, + atlas: world.atlas.as_ref().map(|a| a.0.clone()), + lightmap: world.lightmap, deferred_pre: mask(&post.deferred_pre), composite_pre: mask(&post.composite_pre), formats, @@ -608,12 +697,153 @@ impl PackPost { wgpu::AddressMode::ClampToEdge, ), repeat: sampler(device, wgpu::FilterMode::Linear, wgpu::AddressMode::Repeat), + nearest_repeat: sampler(device, wgpu::FilterMode::Nearest, wgpu::AddressMode::Repeat), blit, copy, - uniforms: FrameUniforms::default(), + uniforms: { + let mut u = FrameUniforms::default(); + u.atlas = world.atlas.map_or([0; 2], |a| a.1); + u + }, } } + /// Writes `pass`'s uniforms for a target of `out` pixels; its bind group for the buffers' + /// current mapping. + fn bind( + &self, + device: &wgpu::Device, + queue: &wgpu::Queue, + pass: &mut Pass, + out: [u32; 2], + ) -> wgpu::BindGroup { + let fog = self.uniforms.fog(); + // Our block members first (`hz_target`, `hz_fog_color`, `hz_fog`), then the pack's. + let ours = [ + [ + out[0] as f32, + out[1] as f32, + 1.0 / out[0] as f32, + 1.0 / out[1] as f32, + ], + fog[0].to_array(), + fog[1].to_array(), + ]; + let head: &[u8] = bytemuck::cast_slice(&ours); + let n = head.len().min(pass.block.len()); + pass.block[..n].copy_from_slice(&head[..n]); + for (index, ty, offset) in &pass.uniforms { + if let Some(v) = index.and_then(|i| self.uniforms.values.get(i)) { + v.write(ty, &mut pass.block, *offset as usize); + } + } + if !pass.block.is_empty() { + queue.write_buffer(&pass.ubo, 0, &pass.block); + } + let targets = self.targets.as_ref().expect("resized"); + let key = self.main & pass.reads; + let group = pass.groups.entry(key).or_insert_with(|| { + let mut entries = vec![wgpu::BindGroupEntry { + binding: 0, + resource: pass.ubo.as_entire_binding(), + }]; + for s in &pass.samplers { + let filter = if s.filtering { + &self.linear + } else { + &self.nearest + }; + let (texture, sampler) = match s.source { + Source::Color(i) => ( + targets.color[i] + .as_ref() + .map_or(&self.dummy, |t| &t[usize::from(key & 1 << i != 0)]), + filter, + ), + Source::Depth => (&targets.depth, &self.nearest), + Source::Noise => (&self.noise, &self.repeat), + Source::Dummy => (&self.dummy, &self.linear), + Source::Shadow => (&self.shadow.0, &self.shadow.1), + // Blocks look as the game draws them: nearest texels. + Source::Atlas => ( + self.atlas.as_ref().unwrap_or(&self.dummy), + &self.nearest_repeat, + ), + Source::Lightmap => (&self.lightmap, &self.linear), + }; + entries.push(wgpu::BindGroupEntry { + binding: s.binding, + resource: wgpu::BindingResource::TextureView(texture), + }); + entries.push(wgpu::BindGroupEntry { + binding: s.binding + 1, + resource: wgpu::BindingResource::Sampler(sampler), + }); + } + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some(&pass.name), + layout: &pass.layout, + entries: &entries, + }) + }); + group.clone() + } + + /// Terrain with the pack's program into the buffers it writes, in place, tested against + /// the world's depth; `draw` issues the game's draws. + fn draw_terrain( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + size: [u32; 2], + draw: &mut dyn FnMut(&mut wgpu::RenderPass<'_>), + ) { + let Some(mut pass) = self.terrain.take() else { + return; + }; + let group = self.bind(device, queue, &mut pass, size); + let targets = self.targets.as_ref().expect("resized"); + let load = wgpu::Operations { + load: wgpu::LoadOp::Load, + store: wgpu::StoreOp::Store, + }; + let mut attachments: Vec> = Vec::new(); + for &(location, buffer) in &pass.writes { + let l = location as usize; + if attachments.len() <= l { + attachments.resize(l + 1, None); + } + attachments[l] = targets.color[buffer] + .as_ref() + .and_then(|t| attach(&t[usize::from(self.main & 1 << buffer != 0)], load)); + } + { + let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some(&pass.name), + color_attachments: &attachments, + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: &targets.depth, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Load, + store: wgpu::StoreOp::Store, + }), + stencil_ops: None, + }), + ..Default::default() + }); + rp.set_pipeline(&pass.pipeline); + rp.set_bind_group(0, &group, &[]); + draw(&mut rp); + } + self.terrain = Some(pass); + } + + /// Whether the pack draws terrain itself. + pub fn has_terrain(&self) -> bool { + self.terrain.is_some() + } + /// The names of the programs that run, in order. pub fn programs(&self) -> Vec<&str> { self.passes.iter().map(|p| p.name.as_str()).collect() @@ -651,9 +881,9 @@ impl PackPost { self.targets = Some(Targets { size, color, - depth: make("depthtex0", wgpu::TextureFormat::R32Float), + depth: make("depthtex0", wgpu::TextureFormat::Depth32Float), }); - for pass in &mut self.passes { + for pass in self.passes.iter_mut().chain(&mut self.terrain) { pass.groups.clear(); } } @@ -673,6 +903,7 @@ impl PackPost { inputs: &PackInputs, size: [u32; 2], window: [u32; 2], + terrain: &mut dyn FnMut(&mut wgpu::RenderPass<'_>), ) { self.resize(device, size); self.uniforms.update(frame, inputs, size); @@ -751,9 +982,6 @@ impl PackPost { view(self.main, 0, false).expect("colortex0"), overwrite, )]; - if self.blit_depth { - attachments.push(attach(&targets.depth, overwrite)); - } for &b in &self.blit_clears { attachments.push(attach( view(self.main, b, false).expect("used buffer"), @@ -763,6 +991,16 @@ impl PackPost { let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("Shader pack world"), color_attachments: &attachments, + depth_stencil_attachment: self.blit_depth.then_some( + wgpu::RenderPassDepthStencilAttachment { + view: &targets.depth, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(1.0), + store: wgpu::StoreOp::Store, + }), + stencil_ops: None, + }, + ), ..Default::default() }); pass.set_pipeline(&self.blit.0); @@ -770,10 +1008,15 @@ impl PackPost { pass.draw(0..3, 0..1); } - let fog = self.uniforms.fog(); + let mut passes = std::mem::take(&mut self.passes); let mut seen = (false, false); let mut shown = false; - for pass in &mut self.passes { + let mut terrain_drawn = self.terrain.is_none(); + for pass in &mut passes { + if !terrain_drawn && pass.group != Group::Prepare { + terrain_drawn = true; + self.draw_terrain(device, queue, encoder, size, terrain); + } if pass.group == Group::Deferred && !seen.0 { seen.0 = true; self.main ^= self.deferred_pre; @@ -783,63 +1026,13 @@ impl PackPost { self.main ^= self.composite_pre; } let final_pass = pass.group == Group::Final; - let out = if final_pass { window } else { size }; - // Our block members first (`hz_target`, `hz_fog_color`, `hz_fog`), then the pack's. - let ours = [ - [ - out[0] as f32, - out[1] as f32, - 1.0 / out[0] as f32, - 1.0 / out[1] as f32, - ], - fog[0].to_array(), - fog[1].to_array(), - ]; - let head: &[u8] = bytemuck::cast_slice(&ours); - let n = head.len().min(pass.block.len()); - pass.block[..n].copy_from_slice(&head[..n]); - for (index, ty, offset) in &pass.uniforms { - if let Some(v) = index.and_then(|i| self.uniforms.values.get(i)) { - v.write(ty, &mut pass.block, *offset as usize); - } - } - if !pass.block.is_empty() { - queue.write_buffer(&pass.ubo, 0, &pass.block); - } - let key = self.main & pass.reads; - let group = pass.groups.entry(key).or_insert_with(|| { - let mut entries = vec![wgpu::BindGroupEntry { - binding: 0, - resource: pass.ubo.as_entire_binding(), - }]; - for s in &pass.samplers { - let filter = if s.filtering { - &self.linear - } else { - &self.nearest - }; - let (texture, sampler) = match s.source { - Source::Color(i) => (view(key, i, false).unwrap_or(&self.dummy), filter), - Source::Depth => (&targets.depth, &self.nearest), - Source::Noise => (&self.noise, &self.repeat), - Source::Dummy => (&self.dummy, &self.linear), - Source::Shadow => (&self.shadow.0, &self.shadow.1), - }; - entries.push(wgpu::BindGroupEntry { - binding: s.binding, - resource: wgpu::BindingResource::TextureView(texture), - }); - entries.push(wgpu::BindGroupEntry { - binding: s.binding + 1, - resource: wgpu::BindingResource::Sampler(sampler), - }); - } - device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some(&pass.name), - layout: &pass.layout, - entries: &entries, - }) - }); + let group = self.bind(device, queue, pass, if final_pass { window } else { size }); + let targets = self.targets.as_ref().expect("resized"); + let view = |i: usize| { + targets.color[i] + .as_ref() + .map(|t| &t[usize::from(self.main & 1 << i == 0)]) + }; let mut attachments: Vec> = Vec::new(); if final_pass { attachments.push(attach(screen, overwrite)); @@ -850,8 +1043,7 @@ impl PackPost { if attachments.len() <= l { attachments.resize(l + 1, None); } - attachments[l] = - view(self.main, buffer, true).and_then(|v| attach(v, overwrite)); + attachments[l] = view(buffer).and_then(|v| attach(v, overwrite)); } } { @@ -861,11 +1053,21 @@ impl PackPost { ..Default::default() }); rp.set_pipeline(&pass.pipeline); - rp.set_bind_group(0, &*group, &[]); + rp.set_bind_group(0, &group, &[]); rp.draw(0..6, 0..1); } self.main ^= pass.flip; } + self.passes = passes; + if !terrain_drawn { + self.draw_terrain(device, queue, encoder, size, terrain); + } + let targets = self.targets.as_ref().expect("resized"); + let view = |m: u16, i: usize, alt: bool| { + targets.color[i] + .as_ref() + .map(|t| &t[main(m, i) ^ usize::from(alt)]) + }; if !shown { let group = device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("Shader pack colortex0"), diff --git a/crates/horizon-client/src/gfx/pack_post.wgsl b/crates/horizon-client/src/gfx/pack_post.wgsl index 30345b00f..75877dddc 100644 --- a/crates/horizon-client/src/gfx/pack_post.wgsl +++ b/crates/horizon-client/src/gfx/pack_post.wgsl @@ -1,5 +1,6 @@ // Helpers of a shader pack's post-processing (pack_post.rs): the world into colortex0 and -// depthtex0, and colortex0 onto the screen for packs without `final`. +// depthtex0 (a depth attachment, for terrain's depth test), and colortex0 onto the screen for +// packs without `final`. @vertex fn vs_full(@builtin(vertex_index) i: u32) -> @builtin(position) vec4 { @@ -12,7 +13,7 @@ fn vs_full(@builtin(vertex_index) i: u32) -> @builtin(position) vec4 { struct World { @location(0) color: vec4, - @location(1) depth: vec4, + @builtin(frag_depth) depth: f32, } // Pack buffers keep GL's rows (row 0 at the bottom), and GL's window depth: Horizon's @@ -23,7 +24,7 @@ fn fs_blit(@builtin(position) p: vec4) -> World { let at = vec2(i32(p.x), size.y - 1 - i32(p.y)); var out: World; out.color = textureLoad(source, at, 0); - out.depth = vec4(1.0 - textureLoad(world_depth, at, 0), 0.0, 0.0, 1.0); + out.depth = 1.0 - textureLoad(world_depth, at, 0); return out; } diff --git a/crates/horizon-client/src/gfx/pack_uniforms.rs b/crates/horizon-client/src/gfx/pack_uniforms.rs index 7d2a73a6a..92eac4a75 100644 --- a/crates/horizon-client/src/gfx/pack_uniforms.rs +++ b/crates/horizon-client/src/gfx/pack_uniforms.rs @@ -140,11 +140,14 @@ pub const NAMES: &[&str] = &[ "gbufferProjectionInverse", "gbufferPreviousModelView", "gbufferPreviousProjection", + "atlasSize", ]; /// Values carried from frame to frame. pub struct FrameUniforms { pub values: Vec, + /// The block atlas's size (`atlasSize`), 0 without one. + pub atlas: [u32; 2], start: std::time::Instant, last: Option, frame: u32, @@ -157,6 +160,7 @@ impl Default for FrameUniforms { fn default() -> Self { Self { values: Vec::new(), + atlas: [0; 2], start: std::time::Instant::now(), last: None, frame: 0, @@ -243,6 +247,7 @@ impl FrameUniforms { "worldDay" => Value::i(&[world_day as i32]), "moonPhase" => Value::i(&[world_day.rem_euclid(8) as i32]), "frameCounter" => Value::i(&[self.frame as i32]), + "atlasSize" => Value::i(&[self.atlas[0] as i32, self.atlas[1] as i32]), "frameTime" => Value::f(&[dt]), "frameTimeCounter" => Value::f(&[seconds]), "viewWidth" => Value::f(&[size[0] as f32]), diff --git a/crates/horizon-shaders/tests/packs/terrain-test/README.md b/crates/horizon-shaders/tests/packs/terrain-test/README.md new file mode 100644 index 000000000..bb8ddd060 --- /dev/null +++ b/crates/horizon-shaders/tests/packs/terrain-test/README.md @@ -0,0 +1,8 @@ +# terrain-test + +Horizon's own shader pack for testing gbuffers terrain (written for Horizon, no other pack's code). +`gbuffers_terrain` draws blocks from the atlas with their vertex colour and lightmap, tinted by +the lightmap coordinate's sky light (cooler where the sky is hidden); `mc_midTexCoord` marks each +face's centre with a dot. `composite` puts the depth buffer's view-space distance into the blue +channel of the far half. Copy the folder into `shaderpacks/` and run the game with +`HORIZON_SHADER_PACK=terrain-test`. diff --git a/crates/horizon-shaders/tests/packs/terrain-test/shaders/composite.fsh b/crates/horizon-shaders/tests/packs/terrain-test/shaders/composite.fsh new file mode 100644 index 000000000..d473ef240 --- /dev/null +++ b/crates/horizon-shaders/tests/packs/terrain-test/shaders/composite.fsh @@ -0,0 +1,19 @@ +#version 120 + +uniform sampler2D colortex0; +uniform sampler2D depthtex0; +uniform mat4 gbufferProjectionInverse; +uniform float far; + +varying vec2 texcoord; + +void main() { + vec3 color = texture2D(colortex0, texcoord).rgb; + if (texcoord.x > 0.5) { + float depth = texture2D(depthtex0, texcoord).r; + vec4 view = gbufferProjectionInverse * vec4(vec3(texcoord, depth) * 2.0 - 1.0, 1.0); + color.b = clamp(length(view.xyz / view.w) / far, 0.0, 1.0); + } + /* DRAWBUFFERS:0 */ + gl_FragData[0] = vec4(color, 1.0); +} diff --git a/crates/horizon-shaders/tests/packs/terrain-test/shaders/composite.vsh b/crates/horizon-shaders/tests/packs/terrain-test/shaders/composite.vsh new file mode 100644 index 000000000..38e4e455c --- /dev/null +++ b/crates/horizon-shaders/tests/packs/terrain-test/shaders/composite.vsh @@ -0,0 +1,8 @@ +#version 120 + +varying vec2 texcoord; + +void main() { + gl_Position = ftransform(); + texcoord = gl_MultiTexCoord0.xy; +} diff --git a/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.fsh b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.fsh new file mode 100644 index 000000000..902cf3525 --- /dev/null +++ b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.fsh @@ -0,0 +1,22 @@ +#version 120 + +uniform sampler2D texture; +uniform sampler2D lightmap; +uniform ivec2 atlasSize; + +varying vec2 texcoord; +varying vec2 lmcoord; +varying vec2 mid; +varying vec4 glcolor; + +void main() { + vec4 color = texture2D(texture, texcoord) * glcolor; + if (color.a < 0.1) discard; + color.rgb *= texture2D(lightmap, lmcoord).rgb; + // Less sky light: bluer. + color.rgb = mix(color.rgb * vec3(0.6, 0.7, 1.2), color.rgb, lmcoord.y); + // A dot at the face's centre, one texel wide. + if (length((texcoord - mid) * vec2(atlasSize)) < 1.0) color.rgb = vec3(1.0, 0.0, 1.0); + /* DRAWBUFFERS:0 */ + gl_FragData[0] = color; +} diff --git a/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.vsh b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.vsh new file mode 100644 index 000000000..bc16f4aad --- /dev/null +++ b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.vsh @@ -0,0 +1,16 @@ +#version 120 + +attribute vec4 mc_midTexCoord; + +varying vec2 texcoord; +varying vec2 lmcoord; +varying vec2 mid; +varying vec4 glcolor; + +void main() { + gl_Position = ftransform(); + texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy; + lmcoord = (gl_TextureMatrix[1] * gl_MultiTexCoord1).xy; + mid = mc_midTexCoord.xy; + glcolor = gl_Color; +} From 348675c5387a11a8992a99428b7609bd78047c58 Mon Sep 17 00:00:00 2001 From: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:52:19 +0200 Subject: [PATCH 005/238] Packs: open streamed archives the way Java's ZipFile does A resource pack written while it streams (Nexo's server behind resourcepack.gommehd.io, gommehd.net's required pack) leaves every local header a stub, every uncompressed size at 0xFFFFFFFF and the END record's entry counts and disk number at 0 and 0xFFFF. Java tolerates all three; the zip crate's ZipArchive refuses the END record as multi-disk and empty, so a required server pack failed its reload and the join never got through (each retry also rebuilt the renderer, once a second). Count the real central-directory entries and normalize the END record's disk and count fields when they disagree with it, and bound the inflate output when an entry's size is the 0xFFFFFFFF sentinel. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- crates/horizon-client/src/zip.rs | 108 ++++++++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/zip.rs b/crates/horizon-client/src/zip.rs index 22cab132d..17442858e 100644 --- a/crates/horizon-client/src/zip.rs +++ b/crates/horizon-client/src/zip.rs @@ -23,7 +23,8 @@ impl Zip { pub fn open(path: &Path) -> io::Result { Self::from_bytes(fs::read(path)?) } - pub fn from_bytes(data: Vec) -> io::Result { + pub fn from_bytes(mut data: Vec) -> io::Result { + normalize_end(&mut data); let archive = ZipArchive::new(Cursor::new(Arc::from(data))).map_err(invalid)?; let entries = (0..archive.len()) .filter_map(|i| Some((archive.name_for_index(i)?.replace('\\', "/"), i))) @@ -51,7 +52,14 @@ impl Zip { match method { CompressionMethod::Stored => Ok(raw), CompressionMethod::Deflated => { - miniz_oxide::inflate::decompress_to_vec_with_limit(&raw, size.max(1) * 2 + 1024) + // Streamed archives write 0xFFFFFFFF for the uncompressed + // size; bound the output instead of trusting it. + let limit = if size == u32::MAX as usize { + 1 << 30 + } else { + size.max(1) * 2 + 1024 + }; + miniz_oxide::inflate::decompress_to_vec_with_limit(&raw, limit) .map_err(|_| invalid("corrupt deflate stream")) } _ => Err(invalid("unsupported zip compression")), @@ -59,6 +67,59 @@ impl Zip { } } +/// The 22 fixed bytes of the end-of-central-directory record. +const END_HEADER: usize = 22; + +/// Java's `ZipFile` ignores the END record's disk numbers and file counts, +/// reading the central directory from `cenpos` to `cenpos + cenlen` whatever +/// they say; the `zip` crate insists the record agrees with the directory. +/// Packs written while streaming (Nexo serves them for gommehd.net, for one) +/// leave every file's local header a stub, the uncompressed sizes at +/// 0xFFFFFFFF and the END counts and disk number at 0 and 0xFFFF — archives +/// Java opens but the crate refuses. Count the real directory entries and +/// normalize the record so it does not look multi-disk or empty. +fn normalize_end(data: &mut [u8]) { + if data.len() < END_HEADER { + return; + } + let u16_at = |data: &[u8], at: usize| u16::from_le_bytes(data[at..at + 2].try_into().unwrap()); + let u32_at = |data: &[u8], at: usize| u32::from_le_bytes(data[at..at + 4].try_into().unwrap()); + // The END record's comment can hide the signature: scan back through the + // last 64 KiB, like every zip reader. + let start = data.len().saturating_sub(END_HEADER + u16::MAX as usize); + let mut end = None; + for pos in (start..=data.len().saturating_sub(END_HEADER)).rev() { + if data[pos..pos + 4] == [0x50, 0x4b, 0x05, 0x06] + && pos + END_HEADER + u16_at(data, pos + 20) as usize <= data.len() + { + end = Some(pos); + break; + } + } + let Some(end) = end else { return }; + // The directory's real position and length, as Java derives them; the + // recorded offset is relative to the archive's start. + let cenlen = u32_at(data, end + 12) as usize; + let cenpos = end.saturating_sub(cenlen); + let mut count = 0u16; + let mut p = cenpos; + while p + 46 <= end && data[p..p + 4] == [0x50, 0x4b, 0x01, 0x02] { + count = count.saturating_add(1); + p += 46 + + u16_at(data, p + 28) as usize + + u16_at(data, p + 30) as usize + + u16_at(data, p + 32) as usize; + } + let disk = u16_at(data, end + 4); + let with_directory = u16_at(data, end + 6); + if disk == with_directory && u16_at(data, end + 8) == count && u16_at(data, end + 10) == count { + return; + } + data[end + 4..end + 10].fill(0); + data[end + 8..end + 10].copy_from_slice(&count.to_le_bytes()); + data[end + 10..end + 12].copy_from_slice(&count.to_le_bytes()); +} + #[cfg(test)] mod tests { use super::*; @@ -207,4 +268,47 @@ mod tests { cut.truncate(cut.len() / 2); assert!(Zip::from_bytes(cut).is_err()); } + + /// A pack as a streaming writer serves it (Nexo/gommehd.net): every + /// local header is a stub, the uncompressed sizes are 0xFFFFFFFF and the + /// END record's counts and disk numbers are wrong. Java's ZipFile opens + /// it anyway; so do we. + #[test] + fn reads_a_streamed_archive() { + let files: [(&str, &[u8], bool); 2] = [ + ("pack.mcmeta", b"{\"pack\":{}}", true), + ("a/b.txt", b"streamed entry", true), + ]; + let mut out = Vec::new(); + let mut central = Vec::new(); + for (name, body, _) in files { + let data = miniz_oxide::deflate::compress_to_vec(body, 6); + let offset = out.len() as u32; + out.extend(0x0403_4b50u32.to_le_bytes()); + out.extend([0; 26]); + out.extend(&data); + central.extend(0x0201_4b50u32.to_le_bytes()); + central.extend([20, 0, 20, 0, 0, 0]); + central.extend(8u16.to_le_bytes()); + central.extend([0; 8]); + central.extend((data.len() as u32).to_le_bytes()); + central.extend(u32::MAX.to_le_bytes()); + central.extend((name.len() as u16).to_le_bytes()); + central.extend([0; 12]); + central.extend(offset.to_le_bytes()); + central.extend(name.as_bytes()); + } + let start = out.len() as u32; + out.extend(¢ral); + out.extend(0x0605_4b50u32.to_le_bytes()); + out.extend(u16::MAX.to_le_bytes()); + out.extend([0; 6]); + out.extend((central.len() as u32).to_le_bytes()); + out.extend(start.to_le_bytes()); + out.extend([0; 2]); + let zip = Zip::from_bytes(out).unwrap(); + assert_eq!(zip.names().collect::>(), ["a/b.txt", "pack.mcmeta"]); + assert_eq!(zip.read("pack.mcmeta").unwrap(), b"{\"pack\":{}}"); + assert_eq!(zip.read("a/b.txt").unwrap(), b"streamed entry"); + } } From 1fa46979c84c216f3e5c0d10d0704160db705f12 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:57:26 +0200 Subject: [PATCH 006/238] Server clients: primed TNT falls and burns its fuse by itself between updates PrimedTnt keeps Entity's NO_OP InterpolationHandler and ticks on a client too: a position packet puts it there at once, its own tick moves it on (gravity, collisions, bounces, drag) and burns the fuse down, and a client discards it when the fuse runs out. Horizon interpolated it towards each update instead, so it trailed about two ticks behind the server. lan_e2e's falling check now drops primed TNT too and checks that the guest has it exactly where its fall from rest puts it for the fuse the server sent. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/remote.rs | 19 +++++--- .../src/game/remote_projectiles.rs | 19 ++++++-- crates/horizon-client/src/game/tnt.rs | 2 +- tools/lan_e2e.py | 44 +++++++++++++++---- 4 files changed, 64 insertions(+), 20 deletions(-) diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 1c7e30abb..4a563f01c 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -1465,10 +1465,10 @@ impl Game<'_> { if let Some(p) = r.pending_items.get_mut(&id) { p.0 = position; } - // ItemEntity and FallingBlockEntity keep Entity's NO_OP - // InterpolationHandler: moveOrInterpolateTo puts them there at - // once, and their own tick moves them on between the server's - // updates (`tick_remote_entities`). + // ItemEntity, FallingBlockEntity and PrimedTnt keep Entity's + // NO_OP InterpolationHandler: moveOrInterpolateTo puts them there + // at once, and their own tick moves them on between the server's + // updates (`tick_remote_entities`, `tick_remote_projectiles`). let at = Vec3::new(position[0], position[1], position[2]); if let Some(d) = self.dropped.iter_mut().find(|d| d.id == id as u32) { d.position = at; @@ -1479,6 +1479,11 @@ impl Game<'_> { f.position = at; return; } + if let Some(t) = self.tnts.iter_mut().find(|t| t.id == id as u32) { + t.position = at; + t.on_ground = on_ground; + return; + } // AbstractInterpolationHandler.interpolateTo: without a rotation, // the one it was heading for. let rotation = rotation.or_else(|| r.lerps.get(&id).and_then(|l| l.rotation)); @@ -2974,14 +2979,16 @@ impl Game<'_> { }); } /// `Entity.lerpMotion` (and `recreateFromPacket`'s motion) for the - /// entities whose own tick moves them on a client: dropped items and - /// falling blocks. + /// entities whose own tick moves them on a client: dropped items, falling + /// blocks and primed TNT. fn remote_motion(&mut self, id: i32, velocity: [f64; 3]) { let v = Vec3::new(velocity[0], velocity[1], velocity[2]); if let Some(d) = self.dropped.iter_mut().find(|d| d.id == id as u32) { d.velocity = v; } else if let Some(f) = self.falling.iter_mut().find(|f| f.id == id as u32) { f.velocity = v; + } else if let Some(t) = self.tnts.iter_mut().find(|t| t.id == id as u32) { + t.velocity = v; } else if let Some(p) = self .remote .as_mut() diff --git a/crates/horizon-client/src/game/remote_projectiles.rs b/crates/horizon-client/src/game/remote_projectiles.rs index 33c69728b..5098e79da 100644 --- a/crates/horizon-client/src/game/remote_projectiles.rs +++ b/crates/horizon-client/src/game/remote_projectiles.rs @@ -181,10 +181,21 @@ impl Game<'_> { /// One tick of interpolation, as `tick_remote_objects`. pub(super) fn tick_remote_projectiles(&mut self, mut step: impl FnMut(u32, Vec3) -> Vec3) { - for t in &mut self.tnts { - t.previous = t.position; - t.position = step(t.id, t.position); - } + // PrimedTnt.tick runs on a client too: it falls, bounces and burns its + // fuse down (the swell and flash follow it) between the server's + // updates (every 10 ticks), and a client discards it when the fuse + // runs out; only the server explodes it. + let (world, collision, assets, facts) = ( + &self.world, + self.collision, + &self.pack.assets, + self.res.facts(), + ); + self.tnts.retain_mut(|t| { + // A fluid lookup that fails leaves it where its move put it. + let _ = t.tick(world, collision, assets, facts); + t.fuse > 0 + }); // FallingBlockEntity.tick runs on a client too: the block falls by // itself between the server's updates (every 20 ticks), then waits // where it lands for the server to place it and remove it. diff --git a/crates/horizon-client/src/game/tnt.rs b/crates/horizon-client/src/game/tnt.rs index fddc02125..6bd2ae956 100644 --- a/crates/horizon-client/src/game/tnt.rs +++ b/crates/horizon-client/src/game/tnt.rs @@ -33,7 +33,7 @@ impl PrimedTnt { ), ) } - fn tick( + pub(super) fn tick( &mut self, world: &horizon_client::world::World, collision: &horizon_world::block_collision::BlockCollisionRegistry<'_>, diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index a0fe0e181..fad55dfe8 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -228,9 +228,10 @@ address. The script then checks, from both sides, what a guest's game used to ge - villagers: the guest sees each villager's and zombie villager's type, profession and level as the host has them, and an unemployed villager that takes a composter becomes a farmer on the guest too (the guest used to draw every villager as an unemployed plains one). -- falling: an item and a falling block the host summons high up fall smoothly on the guest, a - little further every tick between the server's updates (every 20 ticks), as their own - client tick moves them (they used to hang, then jump down once a second, #1568). +- falling: an item, a falling block and primed TNT the host summons high up fall smoothly on the + guest, a little further every tick between the server's updates (every 20 ticks, 10 for + TNT), as their own client tick moves them (they used to hang, then jump down once a + second, #1568), and the TNT's fuse burns down on the guest too. Each check is reported on its own (a failing one doesn't stop the others) and the screenshots of both sides go to --out (host-*.png, guest-*.png). @@ -5500,10 +5501,11 @@ CHECK_FNS.update(villagers=check_villagers) def check_falling(host, guest, out): - """ItemEntity and FallingBlockEntity tick on a client too (gravity, collisions, drag), and their - position packets snap them (Entity's NO_OP InterpolationHandler). The server sends an item's - and a falling block's position every 20 ticks, so between two updates the guest's own tick is - what moves them: sampled every 2 ticks over the first 16, both are lower nearly every time.""" + """ItemEntity, FallingBlockEntity and PrimedTnt tick on a client too (gravity, collisions, drag), + and their position packets snap them (Entity's NO_OP InterpolationHandler). The server sends an + item's and a falling block's position every 20 ticks and TNT's every 10, so between two updates + the guest's own tick is what moves them: sampled every 2 ticks over the first 16, all three are + lower nearly every time. The TNT's fuse (sent once, at spawn) counts down on the guest too.""" x, y, z = (int(v // 1) for v in pos_of(guest)) top = y + 30 tag = 'falling_e2e' @@ -5512,7 +5514,10 @@ def check_falling(host, guest, out): f'{{Tags:["{tag}"],Item:{{id:"minecraft:diamond",count:1}},PickupDelay:32767}}') host('command', command=f'summon minecraft:falling_block {x + 2.5} {top} {z - 0.5} ' f'{{Tags:["{tag}"],BlockState:{{Name:"minecraft:sand"}},DropItem:0b}}') - heights = {'item': [], 'falling block': []} + # Horizon's selectors don't see primed TNT yet: its fuse ends it in the air, 19 blocks down. + host('command', command=f'summon minecraft:tnt {x + 4.5} {top} {z + 0.5} {{fuse:34s}}') + heights = {'item': [], 'falling block': [], 'primed tnt': []} + fuses = [] try: for _ in range(8): items = [e['pos'][1] for e in guest.raw('entities', 48, 'minecraft:item')['entities'] @@ -5520,8 +5525,12 @@ def check_falling(host, guest, out): blocks = [o['position'][1] for o in guest('status')['projectiles'] if o.get('kind') == 'minecraft:falling_block' and abs(o['position'][0] - x - 2.5) < 0.5 and abs(o['position'][2] - z + 0.5) < 0.5] + tnts = [o for o in guest('status')['projectiles'] if o.get('kind') == 'minecraft:tnt' + and abs(o['position'][0] - x - 4.5) < 0.5 and abs(o['position'][2] - z - 0.5) < 0.5] heights['item'].append(items[0] if items else None) heights['falling block'].append(blocks[0] if blocks else None) + heights['primed tnt'].append(tnts[0]['position'][1] if tnts else None) + fuses.append(tnts[0]['fuse'] if tnts else None) guest.wait(2) guest.shot(out, 'falling') finally: @@ -5533,7 +5542,24 @@ def check_falling(host, guest, out): drops = sum(b < a for a, b in zip(seen, seen[1:])) # A tick or two can pass between two samples with no move; never most of them. assert drops >= len(seen) - 2, f'the {name} fell in {drops} of {len(seen) - 1} samples: {heights}' - return heights + # The server sends the TNT's fuse with each move, so each sample says how many ticks it has + # fallen from rest: snapped to the server's place and ticking on, the guest has it exactly + # there (it used to trail two ticks behind, interpolating towards each update). + fell = [(h, 34 - f) for h, f in zip(heights['primed tnt'], fuses) if f is not None] + assert fell and fell[-1][1] > fell[0][1], f'the TNT\'s fuse on the guest: {fuses}' + off = max(abs(h - tnt_fall(top, n)) for h, n in fell) + assert off < 0.01, f'the guest\'s TNT is {off:.3f} off its fall (height, ticks): {fell}' + return {**heights, 'fuse': fuses} + + +def tnt_fall(top, ticks): + """PrimedTnt.tick in open air from rest: its height after `ticks` ticks.""" + y, v = top, 0.0 + for _ in range(ticks): + v -= 0.04 + y += v + v *= 0.98 + return y CHECKS += ['falling'] From 6c8880fbd887a69af1f12acc5f2f9c3b64cbec85 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:00:40 +0100 Subject: [PATCH 007/238] Shader packs, clean room 15: globals initialized at run time, switch options naga keeps only constant initializers of globals, so every initialized global that is not `const` (and every conditional one) gets its value from a function of ours that main calls first, one per declarator, converting as GLSL's initialization does (`vec2 v = atlasSize`). `vec3 a = x, b = y;` no longer loses `b`. Program-enable expressions (3.3) now see the pack's switch options that are on by default (8.1): `#define NAME` with nothing after it, tested with `#ifdef`/`#ifndef` anywhere in the pack (OQ-13 working answer). BSL's `deferred` (ambient occlusion) and its TAA ran with options off before. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, sections 3.3, 8.1; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-shaders/src/lib.rs | 2 + crates/horizon-shaders/src/options.rs | 80 +++++++++++++ crates/horizon-shaders/src/pack.rs | 33 ++++++ crates/horizon-shaders/src/post.rs | 23 +++- crates/horizon-shaders/src/translate/mod.rs | 15 +++ crates/horizon-shaders/src/translate/scan.rs | 116 +++++++++++++++---- 6 files changed, 245 insertions(+), 24 deletions(-) create mode 100644 crates/horizon-shaders/src/options.rs diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index e31eaec6f..d29a0a632 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -17,6 +17,7 @@ mod config; mod discover; mod lang; mod macros; +mod options; mod pack; mod post; mod preprocess; @@ -28,6 +29,7 @@ pub use config::{BUFFERS, Buffer, Config, Format, Kind, buffer_index, draw_buffe pub use discover::{Found, discover}; pub use lang::Lang; pub use macros::{MC_VERSION, Macros, RENDER_STAGES}; +pub use options::switches_on; pub use pack::{Pack, PackError}; pub use post::{Group, Post, PostProgram, dimension_folder, load_post}; pub use preprocess::{Diagnostic, Origin, Preprocessed, preprocess}; diff --git a/crates/horizon-shaders/src/options.rs b/crates/horizon-shaders/src/options.rs new file mode 100644 index 000000000..687fd40e0 --- /dev/null +++ b/crates/horizon-shaders/src/options.rs @@ -0,0 +1,80 @@ +//! Switch options (spec 8.1), as far as program-enable expressions (3.3) need them: which are on +//! by default. A switch is `#define NAME` (on) or `//#define NAME` (off) with nothing after the +//! name but a comment, in any stage or included file of the pack. Packs keep their options in +//! included `.glsl` files and test them elsewhere (OQ-13), so the test the spec asks for +//! (`#ifdef NAME` / `#ifndef NAME`) may be in any file of the pack. + +use std::collections::BTreeSet; + +use crate::Pack; + +const SOURCES: [&str; 8] = ["vsh", "fsh", "gsh", "csh", "tcs", "tes", "glsl", "inc"]; + +/// The switch options that are on by default. +pub fn switches_on(pack: &Pack) -> BTreeSet { + let (mut on, mut off, mut tested) = (BTreeSet::new(), BTreeSet::new(), BTreeSet::new()); + for path in pack.all_files() { + let ext = path.rsplit_once('.').map_or("", |(_, e)| e); + if !SOURCES.contains(&ext) { + continue; + } + let Some(text) = pack.read_text(&path) else { + continue; + }; + for line in text.lines() { + let line = line.trim(); + let (commented, rest) = match line.strip_prefix("//") { + Some(rest) => (true, rest.trim_start()), + None => (false, line), + }; + let Some((directive, rest)) = rest + .strip_prefix('#') + .map(str::trim_start) + .and_then(|r| r.split_once(|c: char| c.is_whitespace())) + else { + continue; + }; + let mut words = rest.split_whitespace(); + let Some(name) = words.next() else { continue }; + match directive { + "ifdef" | "ifndef" if !commented => _ = tested.insert(name.to_owned()), + "define" if words.next().is_none_or(|w| w.starts_with("//")) => { + let set = if commented { &mut off } else { &mut on }; + set.insert(name.to_owned()); + } + _ => {} + } + } + } + // Defined both ways is ambiguous (8.1): it keeps its default only where it is on everywhere. + on.retain(|name| tested.contains(name) && !off.contains(name)); + on +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::pack::tests::zip; + + #[test] + fn switches_need_a_test_and_no_value() { + let pack = Pack::from_zip_bytes( + "P.zip", + zip(&[ + ( + "shaders/lib/settings.glsl", + " #define AO // ambient occlusion\n//#define BLOOM\n#define LEVEL 2 //[1 2]\n\ + #define SPLIT\n#define UNTESTED\n", + ), + ( + "shaders/world0/deferred.fsh", + "#ifdef AO\n#endif\n#ifndef BLOOM\n#endif\n#ifdef SPLIT\n#endif\n#ifdef LEVEL\n#endif\n", + ), + ("shaders/world-1/deferred.fsh", "//#define SPLIT\n"), + ("shaders/shaders.properties", "#define NOT_SOURCE\n#ifdef NOT_SOURCE\n"), + ]), + ) + .unwrap(); + assert_eq!(switches_on(&pack), BTreeSet::from(["AO".to_owned()])); + } +} diff --git a/crates/horizon-shaders/src/pack.rs b/crates/horizon-shaders/src/pack.rs index ae2230618..77aeded35 100644 --- a/crates/horizon-shaders/src/pack.rs +++ b/crates/horizon-shaders/src/pack.rs @@ -167,6 +167,39 @@ impl Pack { names.sort(); names } + + /// Every file of the pack, by its path inside `shaders`, sorted. + pub fn all_files(&self) -> Vec { + let mut names = match &self.files { + Files::Folder(root) => { + let mut names = Vec::new(); + let mut dirs = vec![String::new()]; + while let Some(dir) = dirs.pop() { + for e in fs::read_dir(root.join(&dir)) + .into_iter() + .flatten() + .flatten() + { + let name = e.file_name().to_string_lossy().into_owned(); + let path = if dir.is_empty() { + name + } else { + format!("{dir}/{name}") + }; + match e.file_type() { + Ok(t) if t.is_dir() => dirs.push(path), + Ok(t) if t.is_file() => names.push(path), + _ => {} + } + } + } + names + } + Files::Zip { entries, .. } => entries.keys().cloned().collect(), + }; + names.sort(); + names + } } /// `a/./b/../c` → `a/c`; `None` for a path that climbs out of `shaders`. diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs index cdf7fae80..c96730f0a 100644 --- a/crates/horizon-shaders/src/post.rs +++ b/crates/horizon-shaders/src/post.rs @@ -10,6 +10,8 @@ //! - A program that fails to translate is reported and treated as missing (2.5). //! - Terrain draws with the first program of its fallback chain that loads (2.3). +use std::collections::BTreeSet; + use crate::config::{self, Config}; use crate::{GbuffersInputs, Macros, Pack, Preprocessed, Program, Properties, Target}; use crate::{eval_condition, preprocess, translate_composite, translate_gbuffers}; @@ -99,6 +101,7 @@ pub fn load_post( folder, properties, macros, + switches: crate::switches_on(pack), post: &mut post, }; for (group, base) in GROUPS { @@ -135,6 +138,7 @@ struct Loader<'a> { folder: &'a str, properties: &'a Properties, macros: &'a Macros, + switches: BTreeSet, post: &'a mut Post, } @@ -156,7 +160,13 @@ impl Loader<'_> { if !self.pack.exists(&fsh) || !self.pack.exists(&vsh) { return None; } - if !enabled(self.properties, self.folder, &name, self.macros) { + if !enabled( + self.properties, + self.folder, + &name, + self.macros, + &self.switches, + ) { return None; } if self.pack.exists(&path("gsh")) { @@ -215,8 +225,14 @@ fn diagnostics(pp: &Preprocessed) -> Vec { } /// `program..enabled` (or `program./.enabled`): a boolean expression over -/// switch options (3.3). An option is on when its macro is defined. -fn enabled(properties: &Properties, folder: &str, name: &str, macros: &Macros) -> bool { +/// switch options (3.3): those on by default (8.1) and the defined macros. +fn enabled( + properties: &Properties, + folder: &str, + name: &str, + macros: &Macros, + switches: &BTreeSet, +) -> bool { let keys = [ format!("program.{folder}/{name}.enabled"), format!("program.{name}.enabled"), @@ -230,6 +246,7 @@ fn enabled(properties: &Properties, folder: &str, name: &str, macros: &Macros) - match token { "true" => condition.push('1'), "false" => condition.push('0'), + t if switches.contains(t) => condition.push('1'), t if t.starts_with(|c: char| c.is_alphabetic() || c == '_') => { condition.push_str(&format!("defined({t})")); } diff --git a/crates/horizon-shaders/src/translate/mod.rs b/crates/horizon-shaders/src/translate/mod.rs index 32eecb3cc..83dcb3af9 100644 --- a/crates/horizon-shaders/src/translate/mod.rs +++ b/crates/horizon-shaders/src/translate/mod.rs @@ -472,6 +472,21 @@ mod tests { assert_eq!(i.outputs, [0, 1]); } + /// naga drops non-constant global initializers: they run in our `main`, one per declarator, + /// converting as GLSL's initialization does. + #[test] + fn global_initializers_read_uniforms() { + let fsh = "#version 120\nuniform float rainStrength;\nuniform ivec2 atlasSize;\n\ + vec2 size = atlasSize;\nvec3 a = vec3(rainStrength), b = a * 2.0, c;\n\ + const float k = rainStrength > 0.5 ? 1.0 : 0.0;\nfloat w[] = float[](rainStrength, 1.0);\n\ + void main() { c = b; gl_FragColor = vec4(a + c, size.x * k + w[0]); }\n"; + let p = program( + &[("composite.vsh", VSH_120), ("composite.fsh", fsh)], + Target::Window, + ); + assert!(p.is_ok(), "{:?}", p.err()); + } + #[test] fn core_programs_translate() { let vsh = "#version 330 core\nin vec3 vaPosition;\nin vec2 vaUV0;\nuniform mat4 modelViewMatrix, projectionMatrix;\n\ diff --git a/crates/horizon-shaders/src/translate/scan.rs b/crates/horizon-shaders/src/translate/scan.rs index 6789893f4..392538f03 100644 --- a/crates/horizon-shaders/src/translate/scan.rs +++ b/crates/horizon-shaders/src/translate/scan.rs @@ -338,41 +338,115 @@ impl<'p> Stage<'p> { } } - /// A global `T name = a ? b : c;`: naga can't evaluate a conditional at global scope, so - /// the initializer becomes a function of ours on the same line (`T name; T hz_init_name() { - /// return a ? b : c; }`), called by our `main` before the pack's. + /// A global `T name = a ? b : c;` or, without `const`, any initialized global: naga only + /// evaluates constant initializers at global scope (a conditional is not one; GL drivers + /// also take uniforms and other globals there), so each initializer becomes a function of + /// ours on the same line (`T a; T hz_init_a() { return T(...); } T b; ...`), called by our + /// `main` before the pack's, in file order. The return converts as GLSL's initialization + /// does (`vec2 v = ivec2(...)`). pub(super) fn initializer(&mut self, words: &[usize], to: usize) { let text = |i: usize| self.tokens[i]; let Some(eq) = words.iter().position(|&i| text(i) == "=") else { return; }; - if !words[eq..].iter().any(|&i| text(i) == "?") - || words[..eq].iter().any(|&i| text(i) == ",") - { + // Declarators, split at top-level commas: (first word, `=` if any, end). + let mut declarators = Vec::new(); + let (mut begin, mut nesting, mut assign) = (0, 0i32, None); + for (n, &i) in words.iter().enumerate() { + match text(i) { + "(" | "[" | "{" => nesting += 1, + ")" | "]" | "}" => nesting -= 1, + "=" if nesting == 0 && assign.is_none() => assign = Some(n), + "," if nesting == 0 => { + declarators.push((begin, assign.take(), n)); + begin = n + 1; + } + _ => {} + } + } + declarators.push((begin, assign, words.len())); + let constant = words[..eq].iter().any(|&i| text(i) == "const"); + let conditional = + |d: &(usize, Option, usize)| words[d.0..d.2].iter().any(|&i| text(i) == "?"); + if constant && !declarators.iter().any(conditional) { return; } - let Some(name_at) = words[..eq].iter().rposition(|&i| is_ident(text(i))) else { + // The first declarator's name ends the type. + let first_end = declarators[0].1.unwrap_or(declarators[0].2); + let Some(name_at) = words[..first_end].iter().rposition(|&i| is_ident(text(i))) else { return; }; - let name = text(words[name_at]).to_owned(); - let array: String = words[name_at + 1..eq].iter().map(|&i| text(i)).collect(); - let ty: Vec<&str> = words[..name_at] - .iter() - .map(|&i| text(i)) - .filter(|&t| t != "const") - .collect(); - let Some(ty) = ty.last() else { return }; + // The type, with a `vec3[]`'s count written in. + let mut ty = String::new(); + for &i in &words[..name_at] { + match text(i) { + "const" => {} + "]" if ty.ends_with('[') => match self.array_length(i) { + Some(n) => write!(ty, "{n}]").unwrap(), + None => return, + }, + t if ty.is_empty() || t == "[" => ty.push_str(t), + // Qualifiers before the type (`highp`...): the type is the last word. + t => ty = t.to_owned(), + } + } + if ty.is_empty() { + return; + } + let mut replaced = Vec::new(); + let mut names = Vec::new(); + for (n, &(begin, assign, end)) in declarators.iter().enumerate() { + let name_at = if n == 0 { name_at } else { begin }; + let name = text(words[name_at]).to_owned(); + if !is_ident(&name) { + return; + } + let stop = assign.unwrap_or(end); + let mut array: String = words[name_at + 1..stop].iter().map(|&i| text(i)).collect(); + if array == "[]" { + // `T x[] = T[](a, b)`: the count is written in, as the rewrite does for the name. + match assign.and_then(|a| self.array_length(words[a - 1])) { + Some(n) => array = format!("[{n}]"), + None => return, + } + } + let next = if n + 1 < declarators.len() { + format!(" {ty}") + } else { + String::new() + }; + let end_token = words.get(end).copied().unwrap_or(to); + match assign { + Some(a) => { + // Scalars, vectors and matrices convert; arrays and structs must match. + let scalar = array.is_empty() + && !ty.contains('[') + && (matches!(ty.as_str(), "float" | "int" | "uint" | "bool") + || ty.contains("vec") + || ty.starts_with("mat")); + let (open, close) = if scalar { + (format!("{ty}("), ")") + } else { + (String::new(), "") + }; + replaced.push(( + words[a], + format!("; {ty}{array} hz_init_{name}() {{ return {open}"), + )); + replaced.push((end_token, format!("{close}; }}{next}"))); + names.push(name); + } + None if n + 1 < declarators.len() => replaced.push((end_token, format!(";{next}"))), + None => {} + } + } for &i in &words[..name_at] { if text(i) == "const" { self.removed.push(i..i + 1); } } - self.replaced.insert( - words[eq], - format!("; {ty}{array} hz_init_{name}() {{ return"), - ); - self.replaced.insert(to, "; }".into()); - self.inits.push(name); + self.replaced.extend(replaced); + self.inits.extend(names); } /// A function definition or prototype: remember which parameters are samplers. From 505e8e960f9956a51bde92a52b737d4f7f413c69 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:01:48 +0100 Subject: [PATCH 008/238] Shader packs, clean room 16: custom uniforms `uniform..` and `variable..` of shaders.properties (3.4): an expression parser with C precedence, the function table (math, vectors, `if`, `between`, `equals`, `smooth`, `random`...), evaluated on the CPU once per frame in file order after the frame's own uniforms. `smooth` keeps its state per call site and halves the distance to its target every fade time (OQ-26 working answer). A name neither the frame nor the pack defines reads 0 and is listed once in the log. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, section 3.4; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx/pack_post.rs | 36 +- .../horizon-client/src/gfx/pack_uniforms.rs | 41 ++ crates/horizon-shaders/src/custom.rs | 570 ++++++++++++++++++ crates/horizon-shaders/src/lib.rs | 2 + crates/horizon-shaders/src/post.rs | 4 + crates/horizon-shaders/tests/real_packs.rs | 7 + 6 files changed, 656 insertions(+), 4 deletions(-) create mode 100644 crates/horizon-shaders/src/custom.rs diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index 81a517dba..6520565ed 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -13,7 +13,7 @@ use std::collections::HashMap; -use horizon_shaders::{Format, Group, Kind, Post}; +use horizon_shaders::{Format, Group, Kind, Post, Val}; use super::pack_uniforms::{self, FrameUniforms, PackInputs}; @@ -83,6 +83,8 @@ pub struct PackPost { passes: Vec, /// What draws terrain (`gbuffers_terrain` or its fallback), between `prepare` and `deferred`. terrain: Option, + /// `shaders.properties`' custom uniforms, after the frame's in `uniforms.values`. + custom: horizon_shaders::Custom, atlas: Option, lightmap: wgpu::TextureView, deferred_pre: u16, @@ -342,8 +344,16 @@ fn build_pass( screen: wgpu::TextureFormat, notes: &mut Vec, geometry: Option<&Geometry<'_>>, + custom: &[String], ) -> Option { let features = device.features(); + // The frame's uniforms, then the pack's custom ones. + let index = |name: &str| { + pack_uniforms::index(name).or_else(|| { + let k = custom.iter().position(|c| c == name)?; + Some(pack_uniforms::NAMES.len() + k) + }) + }; let mask = |flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0); let interface = &p.program.interface; let mut entries = vec![entry( @@ -499,7 +509,7 @@ fn build_pass( let missing: Vec<&str> = interface .uniforms .iter() - .filter(|u| !u.name.starts_with("hz_") && pack_uniforms::index(&u.name).is_none()) + .filter(|u| !u.name.starts_with("hz_") && index(&u.name).is_none()) .map(|u| u.name.as_str()) .collect(); if !missing.is_empty() { @@ -516,7 +526,7 @@ fn build_pass( uniforms: interface .uniforms .iter() - .map(|u| (pack_uniforms::index(&u.name), u.ty.clone(), u.offset)) + .map(|u| (index(&u.name), u.ty.clone(), u.offset)) .collect(), name: p.name, group: p.group, @@ -552,11 +562,27 @@ impl PackPost { }); let mut notes = post.notes; notes.extend(post.config.notes); + let mut custom = post.custom; + let custom_names: Vec = custom.uniforms().map(|u| u.0.to_owned()).collect(); + // A first run finds the names neither the frame nor the pack defines. + custom.eval(0.0, &|n| pack_uniforms::index(n).map(|_| Val::scalar(0.0))); + if !custom.notes.is_empty() { + notes.push(format!("custom uniforms: {}", custom.notes.join("; "))); + custom.notes.clear(); + } let mask = |flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0); let mut used = 1u16; let mut build = |p: horizon_shaders::PostProgram, geometry: Option<&Geometry<'_>>| { - let pass = build_pass(device, p, &formats, screen, &mut notes, geometry)?; + let pass = build_pass( + device, + p, + &formats, + screen, + &mut notes, + geometry, + &custom_names, + )?; used |= pass.reads | pass.written | pass.flip; Some(pass) }; @@ -646,6 +672,7 @@ impl PackPost { notes, passes, terrain, + custom, atlas: world.atlas.as_ref().map(|a| a.0.clone()), lightmap: world.lightmap, deferred_pre: mask(&post.deferred_pre), @@ -907,6 +934,7 @@ impl PackPost { ) { self.resize(device, size); self.uniforms.update(frame, inputs, size); + self.uniforms.eval_custom(&mut self.custom); let Some(targets) = &self.targets else { return }; let main = |m: u16, i: usize| usize::from(m & 1 << i != 0); let view = |m: u16, i: usize, alt: bool| { diff --git a/crates/horizon-client/src/gfx/pack_uniforms.rs b/crates/horizon-client/src/gfx/pack_uniforms.rs index 92eac4a75..5a27e62a0 100644 --- a/crates/horizon-client/src/gfx/pack_uniforms.rs +++ b/crates/horizon-client/src/gfx/pack_uniforms.rs @@ -304,6 +304,47 @@ impl FrameUniforms { } } +impl FrameUniforms { + /// Evaluates the pack's custom uniforms with this frame's values and puts them after + /// `NAMES`' values, in `custom.uniforms()` order. + pub fn eval_custom(&mut self, custom: &mut horizon_shaders::Custom) { + self.values.truncate(NAMES.len()); + let values = &self.values; + let builtin = |name: &str| { + let v = values.get(index(name)?)?; + let n = components(name); + Some(match *v { + Value::F(f) => horizon_shaders::Val::vector(&f.map(f64::from)[..n]), + Value::I(i) => horizon_shaders::Val::vector(&i.map(f64::from)[..n.min(3)]), + }) + }; + let dt = match index("frameTime").and_then(|i| values.get(i)) { + Some(Value::F(f)) => f64::from(f[0]), + _ => 0.0, + }; + let out = custom.eval(dt, &builtin); + self.values + .extend(out.iter().map(|v| Value::f(&v.v.map(|x| x as f32)[..v.n]))); + } +} + +/// How many components the uniform `name` has (16 for a matrix). +fn components(name: &str) -> usize { + match name { + "cameraPosition" + | "previousCameraPosition" + | "upPosition" + | "sunPosition" + | "moonPosition" + | "shadowLightPosition" + | "fogColor" + | "skyColor" => 3, + "eyeBrightness" | "eyeBrightnessSmooth" | "atlasSize" => 2, + n if n.starts_with("gbuffer") || n.starts_with("shadow") && n != "shadowAngle" => 16, + _ => 1, + } +} + /// The position of `name` in [`NAMES`]. pub fn index(name: &str) -> Option { NAMES.iter().position(|n| *n == name) diff --git a/crates/horizon-shaders/src/custom.rs b/crates/horizon-shaders/src/custom.rs new file mode 100644 index 000000000..34510247e --- /dev/null +++ b/crates/horizon-shaders/src/custom.rs @@ -0,0 +1,570 @@ +//! Custom uniforms and variables of `shaders.properties` (spec 3.4): `uniform..` and +//! `variable..` expressions, parsed once and evaluated on the CPU every frame in +//! file order. Uniforms reach every program; variables only later expressions. +//! +//! Decisions (impl-plan.md): +//! - OQ-25: numbers are doubles; booleans are 1 and 0; `%` is C's remainder (`fmod` floors); +//! comparisons and logic read the first component; a value takes its declared type at the +//! end (`int` truncates toward zero, `bool` is "not zero"). Unknown names read 0 and are +//! reported once. +//! - OQ-26: `smooth` runs per frame with the frame time; its fade times are seconds (1 by +//! default) to cover half of the remaining distance, `fadeIn` while rising, `fadeOut` while +//! falling. Each call keeps its own state, so the optional id is not needed. + +use std::collections::{HashMap, HashSet}; + +use crate::Properties; + +/// A value: up to 16 components (matrices only as names, for `m.row.column`). +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Val { + pub v: [f64; 16], + /// Components: 1 for a scalar. + pub n: usize, +} + +impl Val { + pub fn scalar(x: f64) -> Self { + let mut v = [0.0; 16]; + v[0] = x; + Self { v, n: 1 } + } + pub fn vector(c: &[f64]) -> Self { + let mut v = [0.0; 16]; + v[..c.len()].copy_from_slice(c); + Self { v, n: c.len() } + } + fn x(&self) -> f64 { + self.v[0] + } + fn truth(&self) -> bool { + self.v[0] != 0.0 + } + /// `f` per component; a scalar operand stretches to the other's size. + fn zip(self, o: Val, f: impl Fn(f64, f64) -> f64) -> Val { + let n = self.n.max(o.n).min(4); + let at = |a: &Val, i: usize| if a.n == 1 { a.v[0] } else { a.v[i] }; + let mut out = Val::scalar(0.0); + out.n = n; + for i in 0..n { + out.v[i] = f(at(&self, i), at(&o, i)); + } + out + } + fn map(self, f: impl Fn(f64) -> f64) -> Val { + let mut out = self; + for i in 0..self.n.min(4) { + out.v[i] = f(self.v[i]); + } + out + } +} + +#[derive(Clone, Debug)] +enum Expr { + Num(f64), + /// A name and its component (`x`, `2`, `2.1` as row 2, column 1). + Name(String, Option<(usize, Option)>), + Call(String, Vec, usize), + Neg(Box), + Not(Box), + Bin(&'static str, Box, Box), +} + +#[derive(Debug)] +struct Item { + name: String, + ty: String, + uniform: bool, + expr: Expr, +} + +/// The pack's custom uniforms and variables, with the state `smooth` and `random` keep. +#[derive(Debug, Default)] +pub struct Custom { + items: Vec, + /// `smooth` state per call site. + smooth: HashMap, + seed: u64, + unknown: HashSet, + /// Expressions that did not parse, and unknown names, as `key: message`. + pub notes: Vec, + calls: usize, +} + +impl Custom { + /// Reads every `uniform.*` and `variable.*` key of `properties`, in file order. + pub fn parse(properties: &Properties) -> Self { + let mut out = Custom { + seed: 0x9e37_79b9_7f4a_7c15, + ..Default::default() + }; + for e in &properties.entries { + let (uniform, rest) = match e.key.split_once('.') { + Some(("uniform", r)) => (true, r), + Some(("variable", r)) => (false, r), + _ => continue, + }; + let Some((ty, name)) = rest.split_once('.') else { + continue; + }; + if !matches!(ty, "float" | "int" | "bool" | "vec2" | "vec3" | "vec4") { + out.notes + .push(format!("{}: type `{ty}` is not supported", e.key)); + continue; + } + match Parser::new(&e.value, &mut out.calls).expr_all() { + Ok(expr) => out.items.push(Item { + name: name.to_owned(), + ty: ty.to_owned(), + uniform, + expr, + }), + Err(m) => out.notes.push(format!("{}: {m}", e.key)), + } + } + out + } + + /// The uniforms' names and declared types, in file order. + pub fn uniforms(&self) -> impl Iterator { + self.items + .iter() + .filter(|i| i.uniform) + .map(|i| (i.name.as_str(), i.ty.as_str())) + } + + /// Evaluates everything for a frame `dt` seconds after the last; `builtin` gives the + /// values of the game's uniforms and constants (`BIOME_*`...). Returns the uniforms' + /// values, in the order of `uniforms`. + pub fn eval(&mut self, dt: f64, builtin: &dyn Fn(&str) -> Option) -> Vec { + let mut vars: HashMap = HashMap::new(); + let mut out = Vec::new(); + let items = std::mem::take(&mut self.items); + for item in &items { + let v = self.value(&item.expr, dt, &vars, builtin); + let v = match item.ty.as_str() { + "int" => Val::scalar(v.x().trunc()), + "bool" => Val::scalar(f64::from(u8::from(v.truth()))), + "float" => Val::scalar(v.x()), + t => { + let n = (t.as_bytes()[3] - b'0') as usize; + let mut v = v; + if v.n == 1 { + let x = v.v[0]; + v.v[1..n].fill(x); + } + v.n = n; + v + } + }; + vars.insert(item.name.clone(), v); + if item.uniform { + out.push(v); + } + } + self.items = items; + out + } + + fn random(&mut self) -> f64 { + self.seed ^= self.seed << 13; + self.seed ^= self.seed >> 7; + self.seed ^= self.seed << 17; + (self.seed >> 11) as f64 / (1u64 << 53) as f64 + } + + fn value( + &mut self, + e: &Expr, + dt: f64, + vars: &HashMap, + builtin: &dyn Fn(&str) -> Option, + ) -> Val { + let ev = |e: &Expr, s: &mut Self| s.value(e, dt, vars, builtin); + match e { + Expr::Num(x) => Val::scalar(*x), + Expr::Name(name, component) => { + let v = match name.as_str() { + "pi" => Some(Val::scalar(std::f64::consts::PI)), + "true" => Some(Val::scalar(1.0)), + "false" => Some(Val::scalar(0.0)), + n => vars.get(n).copied().or_else(|| builtin(n)), + }; + let Some(v) = v else { + if self.unknown.insert(name.clone()) { + self.notes.push(format!("`{name}` is unknown; it reads 0")); + } + return Val::scalar(0.0); + }; + match component { + None => v, + // GLSL's m[column][row] is stored column after column. + Some((row, Some(column))) => Val::scalar(v.v[column * 4 + row]), + Some((i, None)) => Val::scalar(v.v[*i]), + } + } + Expr::Neg(a) => ev(a, self).map(|x| -x), + Expr::Not(a) => Val::scalar(f64::from(u8::from(!ev(a, self).truth()))), + Expr::Bin(op, a, b) => { + let (a, b) = (ev(a, self), ev(b, self)); + let bool_ = |c: bool| Val::scalar(f64::from(u8::from(c))); + match *op { + "+" => a.zip(b, |x, y| x + y), + "-" => a.zip(b, |x, y| x - y), + "*" => a.zip(b, |x, y| x * y), + "/" => a.zip(b, |x, y| x / y), + "%" => a.zip(b, |x, y| x % y), + "<" => bool_(a.x() < b.x()), + "<=" => bool_(a.x() <= b.x()), + ">" => bool_(a.x() > b.x()), + ">=" => bool_(a.x() >= b.x()), + "==" => bool_(a.x() == b.x()), + "!=" => bool_(a.x() != b.x()), + "&&" => bool_(a.truth() && b.truth()), + _ => bool_(a.truth() || b.truth()), + } + } + Expr::Call(f, args, site) => { + let a: Vec = args.iter().map(|e| ev(e, self)).collect(); + let arg = |i: usize| a.get(i).copied().unwrap_or(Val::scalar(0.0)); + let x = arg(0).x(); + let one = |g: fn(f64) -> f64| arg(0).map(g); + match f.as_str() { + "sin" => one(f64::sin), + "cos" => one(f64::cos), + "tan" => one(f64::tan), + "asin" => one(f64::asin), + "acos" => one(f64::acos), + "atan" if a.len() == 2 => Val::scalar(x.atan2(arg(1).x())), + "atan" => one(f64::atan), + "atan2" => Val::scalar(x.atan2(arg(1).x())), + "torad" | "radians" => one(f64::to_radians), + "todeg" | "degrees" => one(f64::to_degrees), + "min" => a + .iter() + .copied() + .reduce(|p, q| p.zip(q, f64::min)) + .unwrap_or(arg(0)), + "max" => a + .iter() + .copied() + .reduce(|p, q| p.zip(q, f64::max)) + .unwrap_or(arg(0)), + "clamp" => arg(0).zip(arg(1), f64::max).zip(arg(2), f64::min), + "abs" => one(f64::abs), + "floor" => one(f64::floor), + "ceil" => one(f64::ceil), + "exp" => one(f64::exp), + "exp2" => one(f64::exp2), + "exp10" => one(|v| 10f64.powf(v)), + "frac" => one(|v| v - v.floor()), + "log" if a.len() == 2 => Val::scalar(arg(1).x().log(x)), + "log" => one(f64::ln), + "log2" => one(f64::log2), + "log10" => one(f64::log10), + "sqrt" => one(f64::sqrt), + "round" => one(f64::round), + "signum" | "sign" => one(|v| if v == 0.0 { 0.0 } else { v.signum() }), + "pow" => arg(0).zip(arg(1), f64::powf), + "fmod" => arg(0).zip(arg(1), |p, q| p - q * (p / q).floor()), + "lerp" => arg(1).zip(arg(2), |p, q| p + (q - p) * x), + "mix" => arg(0).zip(arg(1), |p, q| p + (q - p) * arg(2).x()), + "edge" => Val::scalar(if arg(1).x() < x { 0.0 } else { 1.0 }), + "random" if a.len() == 2 => Val::scalar(x + (arg(1).x() - x) * self.random()), + "random" => Val::scalar(self.random()), + "randomInt" if a.len() == 2 => { + Val::scalar((x + (arg(1).x() - x) * self.random()).floor()) + } + "randomInt" => Val::scalar((self.random() * f64::from(i32::MAX)).floor()), + "if" | "ifb" => { + let mut i = 0; + while i + 1 < a.len() { + if a[i].truth() { + return a[i + 1]; + } + i += 2; + } + a.last().copied().unwrap_or(Val::scalar(0.0)) + } + "smooth" => { + let (val, fade_in, fade_out) = match a.len() { + 4 => (arg(1).x(), arg(2).x(), arg(3).x()), + 3 => (x, arg(1).x(), arg(2).x()), + 2 => (x, arg(1).x(), arg(1).x()), + _ => (x, 1.0, 1.0), + }; + let state = self.smooth.entry(*site).or_insert(val); + let fade = if val > *state { fade_in } else { fade_out }; + if fade > 0.0 { + *state = val + (*state - val) * 0.5f64.powf(dt / fade); + } else { + *state = val; + } + Val::scalar(*state) + } + "print" => arg(2), + "between" => { + Val::scalar(f64::from(u8::from(x >= arg(1).x() && x <= arg(2).x()))) + } + "equals" => { + Val::scalar(f64::from(u8::from((x - arg(1).x()).abs() <= arg(2).x()))) + } + "in" => Val::scalar(f64::from(u8::from(a[1..].iter().any(|v| v.x() == x)))), + "vec2" | "vec3" | "vec4" => { + let n = (f.as_bytes()[3] - b'0') as usize; + let mut c: Vec = a.iter().flat_map(|v| v.v[..v.n].to_vec()).collect(); + if c.len() == 1 { + c = vec![c[0]; n]; + } + c.resize(n, 0.0); + Val::vector(&c) + } + _ => { + if self.unknown.insert(f.clone()) { + self.notes.push(format!("`{f}()` is unknown; it gives 0")); + } + Val::scalar(0.0) + } + } + } + } + } +} + +struct Parser<'a> { + tokens: Vec, + at: usize, + calls: &'a mut usize, +} + +const OPERATORS: [&str; 18] = [ + "||", "&&", "==", "!=", "<=", ">=", "<", ">", "+", "-", "*", "/", "%", "!", "(", ")", ",", ".", +]; + +impl<'a> Parser<'a> { + fn new(text: &str, calls: &'a mut usize) -> Self { + let mut tokens = Vec::new(); + let b = text.as_bytes(); + let mut i = 0; + while i < b.len() { + let c = b[i] as char; + if c.is_whitespace() { + i += 1; + } else if c.is_ascii_digit() + || (c == '.' + && b.get(i + 1).is_some_and(u8::is_ascii_digit) + && !tokens + .last() + .is_some_and(|t: &String| t == "." || is_name(t))) + { + let start = i; + while i < b.len() + && (b[i].is_ascii_alphanumeric() + || b[i] == b'.' + || ((b[i] == b'-' || b[i] == b'+') && matches!(b[i - 1], b'e' | b'E'))) + { + i += 1; + } + tokens.push(text[start..i].to_owned()); + } else if c.is_ascii_alphabetic() || c == '_' { + let start = i; + while i < b.len() && (b[i].is_ascii_alphanumeric() || b[i] == b'_') { + i += 1; + } + tokens.push(text[start..i].to_owned()); + } else { + let op = OPERATORS + .iter() + .find(|o| text[i..].starts_with(**o)) + .map_or(&text[i..i + c.len_utf8()], |o| o); + i += op.len(); + tokens.push(op.to_owned()); + } + } + Parser { + tokens, + at: 0, + calls, + } + } + fn peek(&self) -> Option<&str> { + self.tokens.get(self.at).map(String::as_str) + } + fn eat(&mut self, t: &str) -> bool { + let hit = self.peek() == Some(t); + self.at += usize::from(hit); + hit + } + fn expr_all(&mut self) -> Result { + let e = self.binary(0)?; + match self.peek() { + None => Ok(e), + Some(t) => Err(format!("unexpected `{t}`")), + } + } + /// Precedence levels from the loosest: `||`, `&&`, equality, relations, sums, products. + fn binary(&mut self, level: usize) -> Result { + const LEVELS: [&[&str]; 6] = [ + &["||"], + &["&&"], + &["==", "!="], + &["<", "<=", ">", ">="], + &["+", "-"], + &["*", "/", "%"], + ]; + if level == LEVELS.len() { + return self.unary(); + } + let mut left = self.binary(level + 1)?; + while let Some(op) = self + .peek() + .and_then(|t| LEVELS[level].iter().find(|o| **o == t)) + { + self.at += 1; + let right = self.binary(level + 1)?; + left = Expr::Bin(op, Box::new(left), Box::new(right)); + } + Ok(left) + } + fn unary(&mut self) -> Result { + if self.eat("-") { + return Ok(Expr::Neg(Box::new(self.unary()?))); + } + if self.eat("!") { + return Ok(Expr::Not(Box::new(self.unary()?))); + } + self.eat("+"); + self.primary() + } + fn primary(&mut self) -> Result { + let Some(t) = self.peek().map(str::to_owned) else { + return Err("the expression ends early".into()); + }; + self.at += 1; + if t == "(" { + let e = self.binary(0)?; + return if self.eat(")") { + Ok(e) + } else { + Err("missing `)`".into()) + }; + } + if t.starts_with(|c: char| c.is_ascii_digit() || c == '.') { + return t + .trim_end_matches(['f', 'F']) + .parse() + .map(Expr::Num) + .map_err(|_| format!("bad number `{t}`")); + } + if !is_name(&t) { + return Err(format!("unexpected `{t}`")); + } + if self.eat("(") { + let mut args = Vec::new(); + if !self.eat(")") { + loop { + args.push(self.binary(0)?); + if self.eat(")") { + break; + } + if !self.eat(",") { + return Err(format!("missing `)` after the arguments of `{t}`")); + } + } + } + *self.calls += 1; + return Ok(Expr::Call(t, args, *self.calls)); + } + let component = if self.eat(".") { + let c = self.peek().map(str::to_owned).ok_or("nothing after `.`")?; + self.at += 1; + let index = |s: &str| match s { + "x" | "r" | "s" | "0" => Some(0), + "y" | "g" | "t" | "1" => Some(1), + "z" | "b" | "p" | "2" => Some(2), + "w" | "a" | "q" | "3" => Some(3), + _ => None, + }; + Some(match c.split_once('.') { + Some((r, col)) => ( + index(r).ok_or(format!("bad component `{c}`"))?, + Some(index(col).ok_or(format!("bad component `{c}`"))?), + ), + None => (index(&c).ok_or(format!("bad component `{c}`"))?, None), + }) + } else { + None + }; + Ok(Expr::Name(t, component)) + } +} + +fn is_name(t: &str) -> bool { + t.starts_with(|c: char| c.is_ascii_alphabetic() || c == '_') +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::Macros; + + fn custom(text: &str) -> Custom { + Custom::parse(&Properties::parse(text, &Macros::standard())) + } + + #[test] + fn expressions_follow_c_precedence_and_file_order() { + let mut c = custom( + "variable.float.a = 1 + 2 * 3 - 4 % 3\n\ + uniform.float.b = a / 2\n\ + uniform.bool.c = a > 5 && !(b < 1) || false\n\ + uniform.int.d = -7 / 2\n\ + uniform.vec3.e = vec3(a, cameraPosition.y, 2) * 2\n\ + uniform.float.f = if(a < 0, 1, a < 7, 2, 3) + clamp(fmod(-1, 4), 0, 2.5) + min(4, 2, 3)\n\ + uniform.float.g = gbufferModelView.1.0 + frac(sunAngle - 0.25) + pi * 0\n\ + uniform.float.h = unknownThing + in(biome, 3, BIOME_DESERT)\n", + ); + assert!(c.notes.is_empty(), "{:?}", c.notes); + let names: Vec<_> = c.uniforms().map(|u| u.0).collect(); + assert_eq!(names, ["b", "c", "d", "e", "f", "g", "h"]); + let builtin = |n: &str| match n { + "cameraPosition" => Some(Val::vector(&[1.0, 64.0, 2.0])), + "sunAngle" => Some(Val::scalar(0.125)), + "biome" => Some(Val::scalar(5.0)), + "BIOME_DESERT" => Some(Val::scalar(5.0)), + "gbufferModelView" => { + let mut m = Val::scalar(0.0); + m.v[1] = 9.0; // column 0, row 1 + m.n = 16; + Some(m) + } + _ => None, + }; + let v = c.eval(0.016, &builtin); + let x: Vec = v.iter().map(|v| v.v[0]).collect(); + assert_eq!(x, [3.0, 1.0, -3.0, 12.0, 2.0 + 2.5 + 2.0, 9.0 + 0.875, 1.0]); + assert_eq!(&v[3].v[..3], &[12.0, 128.0, 4.0]); + assert_eq!(c.notes, ["`unknownThing` is unknown; it reads 0"]); + } + + #[test] + fn smooth_halves_the_distance_per_fade_time() { + let mut c = custom("uniform.float.s = smooth(1, target, 2, 0.5)\n"); + let mut target = 0.0; + let mut run = |t: f64, dt: f64| { + target = t; + c.eval(dt, &|n: &str| (n == "target").then(|| Val::scalar(target)))[0].v[0] + }; + assert_eq!(run(0.0, 0.0), 0.0); + assert!((run(1.0, 2.0) - 0.5).abs() < 1e-9, "rising: fadeIn"); + assert!((run(0.0, 0.5) - 0.25).abs() < 1e-9, "falling: fadeOut"); + } + + #[test] + fn bad_expressions_are_reported() { + let c = custom("uniform.float.a = (1 + \nuniform.mat4.m = 1\nuniform.float.ok = 2\n"); + assert_eq!(c.uniforms().count(), 1); + assert_eq!(c.notes.len(), 2, "{:?}", c.notes); + } +} diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index d29a0a632..47cbcca11 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -14,6 +14,7 @@ mod condition; mod config; +mod custom; mod discover; mod lang; mod macros; @@ -26,6 +27,7 @@ mod translate; pub use condition::eval_condition; pub use config::{BUFFERS, Buffer, Config, Format, Kind, buffer_index, draw_buffers}; +pub use custom::{Custom, Val}; pub use discover::{Found, discover}; pub use lang::Lang; pub use macros::{MC_VERSION, Macros, RENDER_STAGES}; diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs index c96730f0a..d6142c70a 100644 --- a/crates/horizon-shaders/src/post.rs +++ b/crates/horizon-shaders/src/post.rs @@ -13,6 +13,7 @@ use std::collections::BTreeSet; use crate::config::{self, Config}; +use crate::custom::Custom; use crate::{GbuffersInputs, Macros, Pack, Preprocessed, Program, Properties, Target}; use crate::{eval_condition, preprocess, translate_composite, translate_gbuffers}; @@ -62,6 +63,8 @@ pub struct Post { pub composite_pre: Vec<(u32, bool)>, /// What draws solid and cutout terrain, when the game asked for it and one loads. pub terrain: Option, + /// The custom uniforms of `shaders.properties` (3.4). + pub custom: Custom, pub config: Config, /// Programs skipped and why, as `program: message` or `file:line: message`. pub notes: Vec, @@ -93,6 +96,7 @@ pub fn load_post( deferred_pre: flips(properties, "deferred_pre"), composite_pre: flips(properties, "composite_pre"), terrain: None, + custom: Custom::parse(properties), config: Config::default(), notes: Vec::new(), }; diff --git a/crates/horizon-shaders/tests/real_packs.rs b/crates/horizon-shaders/tests/real_packs.rs index d0a28d71d..78ba97bfb 100644 --- a/crates/horizon-shaders/tests/real_packs.rs +++ b/crates/horizon-shaders/tests/real_packs.rs @@ -182,6 +182,13 @@ fn real_packs_read() { post.notes.len() + post.config.notes.len(), start.elapsed().as_secs_f64() * 1000.0 ); + let mut custom = post.custom; + custom.eval(0.016, &|_| None); + println!( + " {} custom uniforms; notes: {:?}", + custom.uniforms().count(), + custom.notes.iter().take(4).collect::>() + ); for n in post.notes.iter().chain(&post.config.notes).take(12) { println!(" {n}"); } From a2ec3fc791e773c69b852922ca53af1be298cd47 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:03:17 +0100 Subject: [PATCH 009/238] Shader packs, clean room 17: real packs' buffers and samplers - RGBA16 and R11F_G11F_B10F buffers get their own formats where the device has them (16-bit normalized render targets need the adapter's format features): the float stand-in kept NaN and values past 1, and BSL's TAA history went black for good. - Textures share one sampler variable per kind (linear, nearest, repeat, nearest-repeat, comparison): devices have as few as 16 sampler slots, Complementary's terrain program declares 23 textures. - A gbuffers pass reading the depth buffer or a buffer it draws into reads a blank texture instead (a feedback loop: undefined in GL, refused by wgpu, and a crash here). - A bind group layout that fails validation skips the program with the error's details instead of ending the game. - shadowModelView / shadowProjection (and inverses) for programs that read them before the shadow pass exists: an orthographic camera 160 blocks each side, toward the shadow light. - HORIZON_SHADER_PACK_SKIP=a,b leaves programs out (a developers' aid to bisect a pack). Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, sections 6.1, 6.7, 5.2; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 9 +- crates/horizon-client/src/gfx/pack_post.rs | 243 ++++++++++++++---- .../horizon-client/src/gfx/pack_uniforms.rs | 16 ++ 3 files changed, 209 insertions(+), 59 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index c117b190f..a33d54fa3 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -816,12 +816,17 @@ async fn open_device( continue; } // Mappable primary buffers: section pages that no longer fit in - // video memory go to system memory. + // video memory go to system memory. Shader packs' 16-bit normalized buffers (render + // targets with the adapter's own format features) and R11F_G11F_B10F: a float + // stand-in neither clamps nor drops NaN. let features = timing::GpuTimer::features(&adapter) | cull::Cull::features(&adapter) | (adapter.features() & (wgpu::Features::INDIRECT_FIRST_INSTANCE - | wgpu::Features::MAPPABLE_PRIMARY_BUFFERS)); + | wgpu::Features::MAPPABLE_PRIMARY_BUFFERS + | wgpu::Features::TEXTURE_FORMAT_16BIT_NORM + | wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES + | wgpu::Features::RG11B10UFLOAT_RENDERABLE)); let driver = format!("{} {}", info.driver, info.driver_info); startup_step(format_args!( "requesting the device (driver {}) with features [{features}] and the adapter's \ diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index 6520565ed..0201c7976 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -39,6 +39,82 @@ struct Binding { binding: u32, /// A filtering sampler (the texture is filterable). filtering: bool, + /// Where this texture's sampler object is bound: the first texture of each kind binds it + /// (`binding + 1`), the others share it (devices have as few as 16 sampler slots). + sampler: u32, +} + +/// The sampler objects a pass binds, one slot per kind it uses. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +enum SamplerKind { + Linear, + Nearest, + Repeat, + NearestRepeat, + Comparison, +} + +impl SamplerKind { + fn of(source: Source, filtering: bool) -> Self { + match source { + Source::Color(_) | Source::Dummy if !filtering => Self::Nearest, + Source::Depth => Self::Nearest, + Source::Noise => Self::Repeat, + Source::Shadow => Self::Comparison, + // Blocks look as the game draws them: nearest texels. + Source::Atlas => Self::NearestRepeat, + Source::Color(_) | Source::Dummy | Source::Lightmap => Self::Linear, + } + } + + fn binding_type(self) -> wgpu::SamplerBindingType { + match self { + Self::Linear | Self::Repeat => wgpu::SamplerBindingType::Filtering, + Self::Nearest | Self::NearestRepeat => wgpu::SamplerBindingType::NonFiltering, + Self::Comparison => wgpu::SamplerBindingType::Comparison, + } + } +} + +/// Points every sampler object of `module` at the slot `slot` gives its binding, so that +/// samplers sharing a slot become one variable (two variables may not share a binding). +fn share_samplers(module: &mut naga::Module, slot: &HashMap) { + let mut first = HashMap::new(); + let mut same = HashMap::new(); + for (h, g) in module.global_variables.iter_mut() { + let Some(b) = g.binding.as_mut() else { + continue; + }; + if !matches!(module.types[g.ty].inner, naga::TypeInner::Sampler { .. }) { + continue; + } + let Some(&to) = slot.get(&b.binding) else { + continue; + }; + b.binding = to; + match first.entry(to) { + std::collections::hash_map::Entry::Occupied(e) => _ = same.insert(h, *e.get()), + std::collections::hash_map::Entry::Vacant(e) => _ = e.insert(h), + } + } + if same.is_empty() { + return; + } + let fix = |expressions: &mut naga::Arena| { + for (_, e) in expressions.iter_mut() { + if let naga::Expression::GlobalVariable(g) = e + && let Some(&to) = same.get(g) + { + *g = to; + } + } + }; + for (_, f) in module.functions.iter_mut() { + fix(&mut f.expressions); + } + for ep in &mut module.entry_points { + fix(&mut ep.function.expressions); + } } struct Pass { @@ -128,7 +204,12 @@ fn texture_format(f: Format, features: wgpu::Features) -> wgpu::TextureFormat { let half = pick(T::R16Float, T::Rg16Float, T::Rgba16Float); match (f.kind, f.bits) { (Kind::Unorm, 10) => T::Rgb10a2Unorm, - (Kind::Unorm, 16) if features.contains(wgpu::Features::TEXTURE_FORMAT_16BIT_NORM) => { + (Kind::Unorm, 16) + if features.contains( + wgpu::Features::TEXTURE_FORMAT_16BIT_NORM + | wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES, + ) => + { pick(T::R16Unorm, T::Rg16Unorm, T::Rgba16Unorm) } (Kind::Unorm, b) if b <= 8 => pick(T::R8Unorm, T::Rg8Unorm, T::Rgba8Unorm), @@ -229,8 +310,11 @@ fn checked(device: &wgpu::Device, create: impl FnOnce() -> T) -> Result { - // The details are in the error's sources. - let mut message = e.to_string(); + // The details are in the error's description or its sources. + let mut message = match &e { + wgpu::Error::Validation { source, .. } => format!("{source:?}"), + e => e.to_string(), + }; let mut source = std::error::Error::source(&e); while let Some(s) = source { message = format!("{message}: {s}"); @@ -365,12 +449,22 @@ fn build_pass( }, )]; let mut samplers = Vec::new(); + let (mut kinds, mut slots) = (HashMap::new(), HashMap::new()); let mut reads = 0u16; + // A gbuffers pass draws into its buffers and the depth buffer: reading them as well would + // be a feedback loop (undefined in GL, refused by wgpu), so those read a blank texture. + let in_place = match geometry { + Some(_) => p.buffers.iter().fold(0u16, |m, &b| m | 1 << b), + None => 0, + }; for s in &interface.samplers { let mut src = source(&s.name); - if src == Source::Atlas && geometry.is_none() { - src = Source::Dummy; - } + let blank = match src { + Source::Atlas => geometry.is_none(), + Source::Depth => geometry.is_some(), + Source::Color(n) => in_place & 1 << n != 0, + _ => false, + }; let float = wgpu::TextureSampleType::Float { filterable: true }; let mut sample_type = match src { Source::Color(n) => formats[n] @@ -399,25 +493,32 @@ fn build_pass( )); (src, sample_type) = (Source::Dummy, float); } + if blank { + // The blank texture is float: an integer sampler then fails the pipeline (a note). + src = Source::Dummy; + if int { + sample_type = float; + } + } let filtering = sample_type == float; if let Source::Color(n) = src { reads |= 1 << n; } entries.push(texture_entry(s.binding, sample_type)); - entries.push(entry( - s.binding + 1, - wgpu::BindingType::Sampler(if src == Source::Shadow { - wgpu::SamplerBindingType::Comparison - } else if filtering { - wgpu::SamplerBindingType::Filtering - } else { - wgpu::SamplerBindingType::NonFiltering - }), - )); + let kind = SamplerKind::of(src, filtering); + let sampler = *kinds.entry(kind).or_insert_with(|| { + entries.push(entry( + s.binding + 1, + wgpu::BindingType::Sampler(kind.binding_type()), + )); + s.binding + 1 + }); + slots.insert(s.binding + 1, sampler); samplers.push(Binding { source: src, binding: s.binding, filtering, + sampler, }); } let final_pass = p.group == Group::Final; @@ -453,24 +554,25 @@ fn build_pass( } else { (written & !kept) | mask(&p.flips) }; - let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - label: Some(&p.name), - entries: &entries, - }); - let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - label: Some(&p.name), - bind_group_layouts: &[Some(&layout), geometry.and_then(|g| g.table)], - ..Default::default() - }); let created = checked(device, || { - let module = |m: naga::Module| { + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some(&p.name), + entries: &entries, + }); + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some(&p.name), + bind_group_layouts: &[Some(&layout), geometry.and_then(|g| g.table)], + ..Default::default() + }); + let module = |mut m: naga::Module| { + share_samplers(&mut m, &slots); device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some(&p.name), source: wgpu::ShaderSource::Naga(std::borrow::Cow::Owned(m)), }) }; let (vs, fs) = (module(p.program.vertex), module(p.program.fragment)); - device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: Some(&p.name), layout: Some(&pipeline_layout), vertex: wgpu::VertexState { @@ -496,10 +598,11 @@ fn build_pass( }), multiview_mask: None, cache: None, - }) + }); + (layout, pipeline) }); - let pipeline = match created { - Ok(p) => p, + let (layout, pipeline) = match created { + Ok(created) => created, Err(e) => { notes.push(format!("{}: skipped: {e}", p.name)); return None; @@ -590,9 +693,12 @@ impl PackPost { .terrain .zip(world.terrain.as_ref()) .and_then(|(p, g)| build(p, Some(g))); + // Developers' aid: `HORIZON_SHADER_PACK_SKIP=composite,final` leaves programs out. + let skip = std::env::var("HORIZON_SHADER_PACK_SKIP").unwrap_or_default(); let passes: Vec = post .programs .into_iter() + .filter(|p| !skip.split(',').any(|s| s == p.name)) .filter_map(|p| build(p, None)) .collect(); if let Some(t) = &terrain { @@ -775,37 +881,34 @@ impl PackPost { resource: pass.ubo.as_entire_binding(), }]; for s in &pass.samplers { - let filter = if s.filtering { - &self.linear - } else { - &self.nearest + let texture = match s.source { + Source::Color(i) => targets.color[i] + .as_ref() + .map_or(&self.dummy, |t| &t[usize::from(key & 1 << i != 0)]), + Source::Depth => &targets.depth, + Source::Noise => &self.noise, + Source::Dummy => &self.dummy, + Source::Shadow => &self.shadow.0, + Source::Atlas => self.atlas.as_ref().unwrap_or(&self.dummy), + Source::Lightmap => &self.lightmap, }; - let (texture, sampler) = match s.source { - Source::Color(i) => ( - targets.color[i] - .as_ref() - .map_or(&self.dummy, |t| &t[usize::from(key & 1 << i != 0)]), - filter, - ), - Source::Depth => (&targets.depth, &self.nearest), - Source::Noise => (&self.noise, &self.repeat), - Source::Dummy => (&self.dummy, &self.linear), - Source::Shadow => (&self.shadow.0, &self.shadow.1), - // Blocks look as the game draws them: nearest texels. - Source::Atlas => ( - self.atlas.as_ref().unwrap_or(&self.dummy), - &self.nearest_repeat, - ), - Source::Lightmap => (&self.lightmap, &self.linear), + let sampler = match SamplerKind::of(s.source, s.filtering) { + SamplerKind::Linear => &self.linear, + SamplerKind::Nearest => &self.nearest, + SamplerKind::Repeat => &self.repeat, + SamplerKind::NearestRepeat => &self.nearest_repeat, + SamplerKind::Comparison => &self.shadow.1, }; entries.push(wgpu::BindGroupEntry { binding: s.binding, resource: wgpu::BindingResource::TextureView(texture), }); - entries.push(wgpu::BindGroupEntry { - binding: s.binding + 1, - resource: wgpu::BindingResource::Sampler(sampler), - }); + if s.sampler == s.binding + 1 { + entries.push(wgpu::BindGroupEntry { + binding: s.sampler, + resource: wgpu::BindingResource::Sampler(sampler), + }); + } } device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some(&pass.name), @@ -1130,6 +1233,31 @@ mod tests { use super::*; use wgpu::TextureFormat as T; + /// Two textures with a sampler each become two textures sharing one sampler variable. + #[test] + fn samplers_of_a_kind_share_one_variable() { + let mut module = naga::front::wgsl::parse_str( + "@group(0) @binding(1) var a: texture_2d;\n\ + @group(0) @binding(2) var sa: sampler;\n\ + @group(0) @binding(3) var b: texture_2d;\n\ + @group(0) @binding(4) var sb: sampler;\n\ + @fragment fn main() -> @location(0) vec4 {\n\ + return textureSample(a, sa, vec2(0.5)) + textureSample(b, sb, vec2(0.5));\n}\n", + ) + .unwrap(); + share_samplers(&mut module, &HashMap::from([(2, 2), (4, 2)])); + let info = naga::valid::Validator::new(Default::default(), Default::default()) + .validate(&module) + .unwrap(); + let used: Vec = module + .global_variables + .iter() + .filter(|(h, _)| !info.get_entry_point(0)[*h].is_empty()) + .filter_map(|(_, g)| g.binding.as_ref().map(|b| b.binding)) + .collect(); + assert_eq!(used, [1, 2, 3]); + } + #[test] fn pack_formats_map_to_renderable_ones() { let f = |name| texture_format(Format::parse(name).unwrap(), wgpu::Features::empty()); @@ -1142,8 +1270,9 @@ mod tests { assert_eq!(f("RGBA16"), T::Rgba16Float, "16-bit norm needs a feature"); assert_eq!(f("RGBA8_SNORM"), T::Rgba16Float, "snorm is not renderable"); assert_eq!(f("R11F_G11F_B10F"), T::Rgba16Float); - let all = - wgpu::Features::TEXTURE_FORMAT_16BIT_NORM | wgpu::Features::RG11B10UFLOAT_RENDERABLE; + let all = wgpu::Features::TEXTURE_FORMAT_16BIT_NORM + | wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES + | wgpu::Features::RG11B10UFLOAT_RENDERABLE; let g = |name| texture_format(Format::parse(name).unwrap(), all); assert_eq!(g("RGBA16"), T::Rgba16Unorm); assert_eq!(g("R11F_G11F_B10F"), T::Rg11b10Ufloat); diff --git a/crates/horizon-client/src/gfx/pack_uniforms.rs b/crates/horizon-client/src/gfx/pack_uniforms.rs index 5a27e62a0..c7c4cfd8b 100644 --- a/crates/horizon-client/src/gfx/pack_uniforms.rs +++ b/crates/horizon-client/src/gfx/pack_uniforms.rs @@ -141,6 +141,10 @@ pub const NAMES: &[&str] = &[ "gbufferPreviousModelView", "gbufferPreviousProjection", "atlasSize", + "shadowModelView", + "shadowModelViewInverse", + "shadowProjection", + "shadowProjectionInverse", ]; /// Values carried from frame to frame. @@ -226,6 +230,14 @@ impl FrameUniforms { ), None => (frame.sky.fog_color, frame.distance * 0.9, frame.distance), }; + // The shadow camera (OQ-6, impl-plan.md): orthographic, `shadowDistance` (160) blocks + // each side, 100 blocks from the player toward the shadow light, world +z up. + let light = view + .inverse() + .transform_vector3(if day { sun } else { moon }) + .normalize(); + let shadow_view = Mat4::look_at_rh(light * 100.0, Vec3::ZERO, Vec3::Z); + let shadow_projection = Mat4::orthographic_rh_gl(-160.0, 160.0, -160.0, 160.0, 0.05, 256.0); let day_time = inputs.day_time.rem_euclid(24000) as i32; let world_day = inputs.day_time.div_euclid(24000); let seconds = (now - self.start).as_secs_f32() % 3600.0; @@ -247,6 +259,10 @@ impl FrameUniforms { "worldDay" => Value::i(&[world_day as i32]), "moonPhase" => Value::i(&[world_day.rem_euclid(8) as i32]), "frameCounter" => Value::i(&[self.frame as i32]), + "shadowModelView" => m4(shadow_view), + "shadowModelViewInverse" => m4(shadow_view.inverse()), + "shadowProjection" => m4(shadow_projection), + "shadowProjectionInverse" => m4(shadow_projection.inverse()), "atlasSize" => Value::i(&[self.atlas[0] as i32, self.atlas[1] as i32]), "frameTime" => Value::f(&[dt]), "frameTimeCounter" => Value::f(&[seconds]), From 3e2c30a852452a3ee0a4cf778d85aee55b98c205 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:47:14 +0100 Subject: [PATCH 010/238] Shader packs, clean room 18: translucent terrain drawn by the pack With a pack whose gbuffers_water (or a fallback down gbuffers_terrain's chain) loads, translucent terrain (water, stained glass) is drawn by it after the deferred programs and before the composites, in place, sorted back to front as the game sorts it, and Horizon's own translucent terrain draw is skipped. - blend. (off, or four GL factors); translucent terrain defaults to SRC_ALPHA ONE_MINUS_SRC_ALPHA ONE ONE_MINUS_SRC_ALPHA, and only blendable buffer formats blend. - depthtex1/depthtex2: depth without translucent terrain, a copy taken just before it when a later program reads them (before it, depthtex0 itself). The hand is drawn after the pack, so depthtex2 = depthtex1. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, sections 2.1, 2.3, 3.2 (blend), 6.2 (depthtex1/2); no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 26 ++- crates/horizon-client/src/gfx/pack_post.rs | 213 +++++++++++++++++---- crates/horizon-shaders/src/lib.rs | 4 +- crates/horizon-shaders/src/post.rs | 130 ++++++++++++- crates/horizon-shaders/tests/real_packs.rs | 3 +- 5 files changed, 327 insertions(+), 49 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index a33d54fa3..9a7356d6e 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -3709,6 +3709,12 @@ impl Renderer { let pack_post = !frame.title && self.pack_post.as_ref().is_some_and(|p| p.1.is_some()); // The pack's program reads the vertices this frame's draws bind. let pack_draws_terrain = pack_terrain && flat_frame == self.pack_terrain_flat; + let pack_draws_water = pack_draws_terrain + && self + .pack_post + .as_ref() + .and_then(|p| p.1.as_ref()) + .is_some_and(|p| p.has_water()); let scaled = self.world_post.prepare( &self.device, [self.config.width, self.config.height], @@ -3940,6 +3946,8 @@ impl Renderer { pass.draw(0..4, debug_depth..debug_depth + debug_top); } match self.flat.as_ref().filter(|_| flat_frame) { + // The pack draws translucent terrain after its deferred programs. + _ if pack_draws_water => {} Some(flat) => { pass.set_pipeline(&flat.translucent); flat.draw(&mut pass, &flat.calls[flat.solid_calls..], |p| { @@ -4016,20 +4024,28 @@ impl Renderer { { let size = self.world_post.size().unwrap_or([1, 1]); let flat = self.flat.as_ref().filter(|_| flat_frame); - let mut terrain = |rp: &mut wgpu::RenderPass<'_>| { + let mut terrain = |rp: &mut wgpu::RenderPass<'_>, translucent: bool| { if !pack_draws_terrain { return; } match flat { Some(flat) => { - flat.draw(rp, &flat.calls[..flat.solid_calls], |p| { - self.arena.buffer(p) - }); + let calls = if translucent { + &flat.calls[flat.solid_calls..] + } else { + &flat.calls[..flat.solid_calls] + }; + flat.draw(rp, calls, |p| self.arena.buffer(p)); } None => { rp.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); rp.set_vertex_buffer(1, self.instances.slice(..)); - self.draw_sections(rp, &self.indirect, 0, 0, solid_draws); + let (first, count) = if translucent { + (solid_draws, translucent_draws) + } else { + (0, solid_draws) + }; + self.draw_sections(rp, &self.indirect, 0, first, count); } } }; diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index 0201c7976..b96da2599 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -24,6 +24,8 @@ const BUFFERS: usize = horizon_shaders::BUFFERS; enum Source { Color(usize), Depth, + /// `depthtex1`/`depthtex2`: depth without translucent terrain (a copy taken before it). + Depth1, Noise, /// A 1×1 stand-in for names this slice does not provide yet (custom textures...). Dummy, @@ -58,7 +60,7 @@ impl SamplerKind { fn of(source: Source, filtering: bool) -> Self { match source { Source::Color(_) | Source::Dummy if !filtering => Self::Nearest, - Source::Depth => Self::Nearest, + Source::Depth | Source::Depth1 => Self::Nearest, Source::Noise => Self::Repeat, Source::Shadow => Self::Comparison, // Blocks look as the game draws them: nearest texels. @@ -143,6 +145,9 @@ struct Targets { /// A and B of each buffer used. color: [Option<[wgpu::TextureView; 2]>; BUFFERS], depth: wgpu::TextureView, + depth_texture: wgpu::Texture, + /// `depthtex1`, when a program after translucent terrain reads it. + depth1: Option<(wgpu::Texture, wgpu::TextureView)>, } /// What a pack's gbuffers programs read and draw from the game's world. @@ -159,6 +164,10 @@ pub struct PackPost { passes: Vec, /// What draws terrain (`gbuffers_terrain` or its fallback), between `prepare` and `deferred`. terrain: Option, + /// What draws translucent terrain (`gbuffers_water` or its fallback), after `deferred`. + water: Option, + /// A program after translucent terrain reads `depthtex1`: it is copied before water. + depth1: bool, /// `shaders.properties`' custom uniforms, after the frame's in `uniforms.values`. custom: horizon_shaders::Custom, atlas: Option, @@ -231,7 +240,9 @@ fn texture_format(f: Format, features: wgpu::Features) -> wgpu::TextureFormat { fn source(name: &str) -> Source { match name { - "depthtex0" | "gdepthtex" | "depthtex1" | "depthtex2" => Source::Depth, + "depthtex0" | "gdepthtex" => Source::Depth, + // The hand is drawn after the pack, so `depthtex2` (without it) is `depthtex1`. + "depthtex1" | "depthtex2" => Source::Depth1, "noisetex" => Source::Noise, "texture" | "gtexture" | "tex" => Source::Atlas, "lightmap" => Source::Lightmap, @@ -392,6 +403,34 @@ fn texture_entry(binding: u32, sample_type: wgpu::TextureSampleType) -> wgpu::Bi ) } +/// `blend.`'s factors as wgpu's, adding both terms. +fn blend_state(b: horizon_shaders::Blend) -> wgpu::BlendState { + use horizon_shaders::Factor as F; + use wgpu::BlendFactor as W; + let f = |f| match f { + F::Zero => W::Zero, + F::One => W::One, + F::SrcColor => W::Src, + F::OneMinusSrcColor => W::OneMinusSrc, + F::DstColor => W::Dst, + F::OneMinusDstColor => W::OneMinusDst, + F::SrcAlpha => W::SrcAlpha, + F::OneMinusSrcAlpha => W::OneMinusSrcAlpha, + F::DstAlpha => W::DstAlpha, + F::OneMinusDstAlpha => W::OneMinusDstAlpha, + F::SrcAlphaSaturate => W::SrcAlphaSaturated, + }; + let component = |src, dst| wgpu::BlendComponent { + src_factor: f(src), + dst_factor: f(dst), + operation: wgpu::BlendOperation::Add, + }; + wgpu::BlendState { + color: component(b[0], b[1]), + alpha: component(b[2], b[3]), + } +} + fn target(format: wgpu::TextureFormat) -> Option { Some(wgpu::ColorTargetState { format, @@ -462,6 +501,8 @@ fn build_pass( let blank = match src { Source::Atlas => geometry.is_none(), Source::Depth => geometry.is_some(), + // Translucent terrain reads the copy; opaque terrain would read itself. + Source::Depth1 => p.group == Group::Gbuffers, Source::Color(n) => in_place & 1 << n != 0, _ => false, }; @@ -470,7 +511,7 @@ fn build_pass( Source::Color(n) => formats[n] .sample_type(None, Some(features)) .unwrap_or(float), - Source::Depth => wgpu::TextureSampleType::Float { filterable: false }, + Source::Depth | Source::Depth1 => wgpu::TextureSampleType::Float { filterable: false }, // Unfilterable on WebGPU-level devices; the lightmap is sampled at texel centres. Source::Lightmap => float, _ => float, @@ -534,7 +575,17 @@ fn build_pass( targets[0] = target(screen); } } else if let Some(&buffer) = p.buffers.get(l) { - targets[l] = target(formats[buffer as usize]); + let format = formats[buffer as usize]; + let mut state = target(format); + if let (Some(t), Some(b)) = (&mut state, p.blend) + && format + .guaranteed_format_features(features) + .flags + .contains(wgpu::TextureFormatFeatureFlags::BLENDABLE) + { + t.blend = Some(blend_state(b)); + } + targets[l] = state; writes.push((location, buffer as usize)); } } @@ -693,6 +744,10 @@ impl PackPost { .terrain .zip(world.terrain.as_ref()) .and_then(|(p, g)| build(p, Some(g))); + let water = post + .water + .zip(world.terrain.as_ref()) + .and_then(|(p, g)| build(p, Some(g))); // Developers' aid: `HORIZON_SHADER_PACK_SKIP=composite,final` leaves programs out. let skip = std::env::var("HORIZON_SHADER_PACK_SKIP").unwrap_or_default(); let passes: Vec = post @@ -704,25 +759,43 @@ impl PackPost { if let Some(t) = &terrain { notes.push(format!("terrain draws with {}", t.name)); } + if let Some(w) = &water { + notes.push(format!("translucent terrain draws with {}", w.name)); + } + let depth1 = water.is_some() + && passes + .iter() + .filter(|p| matches!(p.group, Group::Composite | Group::Final)) + .chain(&water) + .any(|p| p.samplers.iter().any(|s| s.source == Source::Depth1)); // A clear only matters when a program reads the buffer before one writes all of it // (composite passes cover their whole target, terrain does not); the others are skipped. let mut cleared = Vec::new(); for b in 1..BUFFERS { let bit = 1u16 << b; let touched = |p: &&Pass| (p.reads | p.flip | p.written) & bit != 0; - let first = terrain.iter().find(touched).map(|_| true).or_else(|| { - let p = passes.iter().find(touched)?; - Some(p.reads & bit != 0 || p.written & bit == 0) - }); + // Gbuffers passes cover part of their targets: a clear is needed if they touch it. + let first = terrain + .iter() + .chain(&water) + .find(touched) + .map(|_| true) + .or_else(|| { + let p = passes.iter().find(touched)?; + Some(p.reads & bit != 0 || p.written & bit == 0) + }); if clears[b].is_some() && first == Some(true) { cleared.push(b); } } // Terrain tests against the world's depth (entities...), so the blit copies it then. let blit_depth = terrain.is_some() - || passes - .iter() - .any(|p| p.samplers.iter().any(|s| s.source == Source::Depth)); + || water.is_some() + || passes.iter().any(|p| { + p.samplers + .iter() + .any(|s| matches!(s.source, Source::Depth | Source::Depth1)) + }); let room = 8 - 1; let other_clears = cleared.split_off(room.min(cleared.len())); let float = wgpu::TextureSampleType::Float { filterable: true }; @@ -778,6 +851,8 @@ impl PackPost { notes, passes, terrain, + water, + depth1, custom, atlas: world.atlas.as_ref().map(|a| a.0.clone()), lightmap: world.lightmap, @@ -886,6 +961,18 @@ impl PackPost { .as_ref() .map_or(&self.dummy, |t| &t[usize::from(key & 1 << i != 0)]), Source::Depth => &targets.depth, + // Before translucent terrain the two are the same. + Source::Depth1 => match &targets.depth1 { + Some(d) + if matches!( + pass.group, + Group::Composite | Group::Final | Group::Translucent + ) => + { + &d.1 + } + _ => &targets.depth, + }, Source::Noise => &self.noise, Source::Dummy => &self.dummy, Source::Shadow => &self.shadow.0, @@ -919,19 +1006,36 @@ impl PackPost { group.clone() } - /// Terrain with the pack's program into the buffers it writes, in place, tested against - /// the world's depth; `draw` issues the game's draws. + /// Terrain (or, `translucent`, translucent terrain) with the pack's program into the + /// buffers it writes, in place, tested against the world's depth; `draw` issues the + /// game's draws. fn draw_terrain( &mut self, device: &wgpu::Device, queue: &wgpu::Queue, encoder: &mut wgpu::CommandEncoder, size: [u32; 2], - draw: &mut dyn FnMut(&mut wgpu::RenderPass<'_>), + translucent: bool, + draw: &mut dyn FnMut(&mut wgpu::RenderPass<'_>, bool), ) { - let Some(mut pass) = self.terrain.take() else { + let slot = if translucent { + &mut self.water + } else { + &mut self.terrain + }; + let Some(mut pass) = slot.take() else { return; }; + if translucent + && let Some(t) = &self.targets + && let Some((copy, _)) = &t.depth1 + { + encoder.copy_texture_to_texture( + t.depth_texture.as_image_copy(), + copy.as_image_copy(), + t.depth_texture.size(), + ); + } let group = self.bind(device, queue, &mut pass, size); let targets = self.targets.as_ref().expect("resized"); let load = wgpu::Operations { @@ -964,9 +1068,13 @@ impl PackPost { }); rp.set_pipeline(&pass.pipeline); rp.set_bind_group(0, &group, &[]); - draw(&mut rp); + draw(&mut rp, translucent); + } + if translucent { + self.water = Some(pass); + } else { + self.terrain = Some(pass); } - self.terrain = Some(pass); } /// Whether the pack draws terrain itself. @@ -974,6 +1082,11 @@ impl PackPost { self.terrain.is_some() } + /// Whether the pack draws translucent terrain itself. + pub fn has_water(&self) -> bool { + self.water.is_some() + } + /// The names of the programs that run, in order. pub fn programs(&self) -> Vec<&str> { self.passes.iter().map(|p| p.name.as_str()).collect() @@ -983,23 +1096,24 @@ impl PackPost { if self.targets.as_ref().is_some_and(|t| t.size == size) { return; } + let texture = |label: &str, format, usage| { + device.create_texture(&wgpu::TextureDescriptor { + label: Some(label), + size: wgpu::Extent3d { + width: size[0], + height: size[1], + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage: usage | wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }) + }; let make = |label: &str, format| { - device - .create_texture(&wgpu::TextureDescriptor { - label: Some(label), - size: wgpu::Extent3d { - width: size[0], - height: size[1], - depth_or_array_layers: 1, - }, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format, - usage: wgpu::TextureUsages::RENDER_ATTACHMENT - | wgpu::TextureUsages::TEXTURE_BINDING, - view_formats: &[], - }) + texture(label, format, wgpu::TextureUsages::RENDER_ATTACHMENT) .create_view(&Default::default()) }; let color = std::array::from_fn(|i| { @@ -1008,12 +1122,26 @@ impl PackPost { [make(&label, self.formats[i]), make(&label, self.formats[i])] }) }); + let depth32 = wgpu::TextureFormat::Depth32Float; + let depth_texture = texture( + "depthtex0", + depth32, + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC, + ); + let depth1 = self.depth1.then(|| { + let t = texture("depthtex1", depth32, wgpu::TextureUsages::COPY_DST); + let view = t.create_view(&Default::default()); + (t, view) + }); self.targets = Some(Targets { size, color, - depth: make("depthtex0", wgpu::TextureFormat::Depth32Float), + depth: depth_texture.create_view(&Default::default()), + depth_texture, + depth1, }); - for pass in self.passes.iter_mut().chain(&mut self.terrain) { + let gbuffers = self.terrain.iter_mut().chain(&mut self.water); + for pass in self.passes.iter_mut().chain(gbuffers) { pass.groups.clear(); } } @@ -1033,7 +1161,7 @@ impl PackPost { inputs: &PackInputs, size: [u32; 2], window: [u32; 2], - terrain: &mut dyn FnMut(&mut wgpu::RenderPass<'_>), + terrain: &mut dyn FnMut(&mut wgpu::RenderPass<'_>, bool), ) { self.resize(device, size); self.uniforms.update(frame, inputs, size); @@ -1143,10 +1271,15 @@ impl PackPost { let mut seen = (false, false); let mut shown = false; let mut terrain_drawn = self.terrain.is_none(); + let mut water_drawn = self.water.is_none(); for pass in &mut passes { if !terrain_drawn && pass.group != Group::Prepare { terrain_drawn = true; - self.draw_terrain(device, queue, encoder, size, terrain); + self.draw_terrain(device, queue, encoder, size, false, terrain); + } + if !water_drawn && matches!(pass.group, Group::Composite | Group::Final) { + water_drawn = true; + self.draw_terrain(device, queue, encoder, size, true, terrain); } if pass.group == Group::Deferred && !seen.0 { seen.0 = true; @@ -1191,7 +1324,10 @@ impl PackPost { } self.passes = passes; if !terrain_drawn { - self.draw_terrain(device, queue, encoder, size, terrain); + self.draw_terrain(device, queue, encoder, size, false, terrain); + } + if !water_drawn { + self.draw_terrain(device, queue, encoder, size, true, terrain); } let targets = self.targets.as_ref().expect("resized"); let view = |m: u16, i: usize, alt: bool| { @@ -1282,6 +1418,7 @@ mod tests { fn sampler_names() { assert!(source("gaux1") == Source::Color(4)); assert!(source("gdepthtex") == Source::Depth); + assert!(source("depthtex2") == Source::Depth1); assert!(source("noisetex") == Source::Noise); assert!(source("shadowcolor0") == Source::Dummy); } diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index 47cbcca11..f5819d435 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -33,7 +33,9 @@ pub use lang::Lang; pub use macros::{MC_VERSION, Macros, RENDER_STAGES}; pub use options::switches_on; pub use pack::{Pack, PackError}; -pub use post::{Group, Post, PostProgram, dimension_folder, load_post}; +pub use post::{ + Blend, Factor, Group, Post, PostProgram, TRANSLUCENT_BLEND, dimension_folder, load_post, +}; pub use preprocess::{Diagnostic, Origin, Preprocessed, preprocess}; pub use properties::{Entry, Note, Properties}; pub use translate::{ diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs index d6142c70a..e50eb4797 100644 --- a/crates/horizon-shaders/src/post.rs +++ b/crates/horizon-shaders/src/post.rs @@ -24,10 +24,67 @@ pub enum Group { Deferred, Composite, Final, - /// A gbuffers program: the game draws its geometry with it. + /// A gbuffers program: the game draws its geometry with it, before deferred. Gbuffers, + /// A gbuffers program drawing translucent geometry, after deferred (2.1). + Translucent, } +/// A blend factor of `blend.` (3.2), as GL names it. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Factor { + Zero, + One, + SrcColor, + OneMinusSrcColor, + DstColor, + OneMinusDstColor, + SrcAlpha, + OneMinusSrcAlpha, + DstAlpha, + OneMinusDstAlpha, + SrcAlphaSaturate, +} + +impl Factor { + fn parse(word: &str) -> Option { + Some(match word { + "ZERO" => Self::Zero, + "ONE" => Self::One, + "SRC_COLOR" => Self::SrcColor, + "ONE_MINUS_SRC_COLOR" => Self::OneMinusSrcColor, + "DST_COLOR" => Self::DstColor, + "ONE_MINUS_DST_COLOR" => Self::OneMinusDstColor, + "SRC_ALPHA" => Self::SrcAlpha, + "ONE_MINUS_SRC_ALPHA" => Self::OneMinusSrcAlpha, + "DST_ALPHA" => Self::DstAlpha, + "ONE_MINUS_DST_ALPHA" => Self::OneMinusDstAlpha, + "SRC_ALPHA_SATURATE" => Self::SrcAlphaSaturate, + _ => return None, + }) + } +} + +/// Source and destination factors for colour, then for alpha. +pub type Blend = [Factor; 4]; + +/// The default blend of translucent geometry (3.2). +pub const TRANSLUCENT_BLEND: Blend = [ + Factor::SrcAlpha, + Factor::OneMinusSrcAlpha, + Factor::One, + Factor::OneMinusSrcAlpha, +]; + +/// `gbuffers_water` and its fallbacks (2.3). +const WATER: [&str; 5] = [ + "gbuffers_water", + "gbuffers_terrain", + "gbuffers_textured_lit", + "gbuffers_textured", + "gbuffers_basic", +]; + /// `gbuffers_terrain` and its fallbacks (2.3). const TERRAIN: [&str; 4] = [ "gbuffers_terrain", @@ -52,6 +109,8 @@ pub struct PostProgram { pub buffers: Vec, /// `flip..`: buffers forced to flip (true) or kept (false) after this program. pub flips: Vec<(u32, bool)>, + /// How a gbuffers program's output blends into its buffers; `None` writes them. + pub blend: Option, } pub struct Post { @@ -63,6 +122,8 @@ pub struct Post { pub composite_pre: Vec<(u32, bool)>, /// What draws solid and cutout terrain, when the game asked for it and one loads. pub terrain: Option, + /// What draws translucent terrain (water, stained glass) after deferred. + pub water: Option, /// The custom uniforms of `shaders.properties` (3.4). pub custom: Custom, pub config: Config, @@ -96,6 +157,7 @@ pub fn load_post( deferred_pre: flips(properties, "deferred_pre"), composite_pre: flips(properties, "composite_pre"), terrain: None, + water: None, custom: Custom::parse(properties), config: Config::default(), notes: Vec::new(), @@ -127,11 +189,24 @@ pub fn load_post( } } if let Some(inputs) = terrain { - loader.post.terrain = TERRAIN.iter().find_map(|name| { - loader.load(name.to_string(), Group::Gbuffers, |v, f| { - translate_gbuffers(v, f, Target::Buffers, inputs) + let mut gbuffers = |names: &[&str], group| { + names.iter().find_map(|name| { + loader.load(name.to_string(), group, |v, f| { + translate_gbuffers(v, f, Target::Buffers, inputs) + }) }) + }; + let terrain = gbuffers(&TERRAIN, Group::Gbuffers); + // gbuffers_water falls back to gbuffers_terrain's chain (2.3). + let water = gbuffers(&WATER, Group::Translucent); + loader.post.terrain = terrain; + loader.post.water = water.map(|mut p| { + p.blend = blend(properties, &p.name).unwrap_or(Some(TRANSLUCENT_BLEND)); + p }); + if let Some(p) = &mut loader.post.terrain { + p.blend = blend(properties, &p.name).flatten(); + } } post.config = std::mem::take(&mut post.config).finish(); post @@ -211,6 +286,7 @@ impl Loader<'_> { group, program, buffers, + blend: None, }) } } @@ -260,6 +336,20 @@ fn enabled( eval_condition(&condition, macros).ok() != Some(0) } +/// `blend.`: `Some(None)` for `off`, `Some(factors)`, or `None` when absent or not +/// four known factors. +fn blend(properties: &Properties, program: &str) -> Option> { + let value = properties.get(&format!("blend.{program}"))?.trim(); + if value == "off" { + return Some(None); + } + let factors: Vec = value + .split_whitespace() + .map(Factor::parse) + .collect::>()?; + Some(Some(factors.try_into().ok()?)) +} + /// `flip..=` keys of one program. fn flips(properties: &Properties, program: &str) -> Vec<(u32, bool)> { let prefix = format!("flip.{program}."); @@ -355,6 +445,38 @@ mod tests { assert!(post.programs.iter().any(|p| p.name == "composite3")); } + /// Water falls back to the terrain chain and blends by default; `blend.*` overrides. + #[test] + fn water_falls_back_to_terrain_and_blends() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/packs/terrain-test"); + let pack = Pack::open(&dir).unwrap(); + let macros = Macros::standard(); + let inputs = crate::GbuffersInputs { + declarations: "layout(location = 0) in vec3 hz_v_pos;".into(), + body: " hz_Vertex = vec4(hz_v_pos, 1.0);\n hz_Color = vec4(1.0);\n".into(), + attributes: Vec::new(), + }; + let post = load_post(&pack, "", &Properties::default(), ¯os, Some(&inputs)); + let (terrain, water) = (post.terrain.unwrap(), post.water.unwrap()); + assert_eq!((terrain.group, terrain.blend), (Group::Gbuffers, None)); + assert_eq!(water.name, "gbuffers_terrain"); + assert_eq!( + (water.group, water.blend), + (Group::Translucent, Some(TRANSLUCENT_BLEND)) + ); + let properties = Properties::parse( + "blend.gbuffers_terrain = ONE ONE ZERO ONE\nblend.gbuffers_water = off\n", + ¯os, + ); + let terrain_blend = blend(&properties, "gbuffers_terrain"); + assert_eq!( + terrain_blend, + Some(Some([Factor::One, Factor::One, Factor::Zero, Factor::One])) + ); + assert_eq!(blend(&properties, "gbuffers_water"), Some(None)); + assert_eq!(blend(&properties, "gbuffers_basic"), None); + } + #[test] fn horizons_test_pack_loads() { let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/packs/post-test"); diff --git a/crates/horizon-shaders/tests/real_packs.rs b/crates/horizon-shaders/tests/real_packs.rs index 78ba97bfb..c5d4defac 100644 --- a/crates/horizon-shaders/tests/real_packs.rs +++ b/crates/horizon-shaders/tests/real_packs.rs @@ -176,9 +176,10 @@ fn real_packs_read() { Some(&terrain), ); println!( - " dimension {id} (folder `{folder}`): {} post programs, terrain {:?}, {} notes, {:.0} ms", + " dimension {id} (folder `{folder}`): {} post programs, terrain {:?}, water {:?}, {} notes, {:.0} ms", post.programs.len(), post.terrain.as_ref().map(|t| &t.name), + post.water.as_ref().map(|t| &t.name), post.notes.len() + post.config.notes.len(), start.elapsed().as_secs_f64() * 1000.0 ); From b6345fc61d640de07afc9a2f3695039e02ac3201 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:56:31 +0100 Subject: [PATCH 011/238] Shader packs, clean room 19: GPU time of each pass (developers' aid) HORIZON_SHADER_PACK_TIMING=1 puts timestamps around every pass of a shader pack, one frame in 30, and logs each pass's median every 30 timed frames. The frame after a timed one waits for the GPU before reading them (Metal resolves timestamps without waiting for the passes that write them). Passes overlap on tiled GPUs, so a pass is charged from the later of its beginning and the previous pass's end. This is what attributes a pack's cost to its own programs or to ours when the machine is shared and FPS comparisons are noisy. Clean-room: Horizon's own tooling, no spec section; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 1 + crates/horizon-client/src/gfx/pack_post.rs | 11 ++ crates/horizon-client/src/gfx/pack_timing.rs | 147 +++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 crates/horizon-client/src/gfx/pack_timing.rs diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 9a7356d6e..f0987dddd 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -70,6 +70,7 @@ mod lod; mod occlusion; mod pack_atlas; mod pack_post; +mod pack_timing; pub mod pack_uniforms; mod page_runs; pub mod panorama; diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index b96da2599..739688784 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -168,6 +168,8 @@ pub struct PackPost { water: Option, /// A program after translucent terrain reads `depthtex1`: it is copied before water. depth1: bool, + /// `HORIZON_SHADER_PACK_TIMING`: GPU time of each pass, in the log. + timing: Option, /// `shaders.properties`' custom uniforms, after the frame's in `uniforms.values`. custom: horizon_shaders::Custom, atlas: Option, @@ -853,6 +855,7 @@ impl PackPost { terrain, water, depth1, + timing: super::pack_timing::PassTiming::new(device, queue), custom, atlas: world.atlas.as_ref().map(|a| a.0.clone()), lightmap: world.lightmap, @@ -1053,9 +1056,11 @@ impl PackPost { .and_then(|t| attach(&t[usize::from(self.main & 1 << buffer != 0)], load)); } { + let timestamp_writes = self.timing.as_mut().and_then(|t| t.writes(&pass.name)); let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some(&pass.name), color_attachments: &attachments, + timestamp_writes, depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { view: &targets.depth, depth_ops: Some(wgpu::Operations { @@ -1164,6 +1169,9 @@ impl PackPost { terrain: &mut dyn FnMut(&mut wgpu::RenderPass<'_>, bool), ) { self.resize(device, size); + if let Some(t) = &mut self.timing { + t.begin(device, queue); + } self.uniforms.update(frame, inputs, size); self.uniforms.eval_custom(&mut self.custom); let Some(targets) = &self.targets else { return }; @@ -1214,6 +1222,7 @@ impl PackPost { encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("Shader pack clear"), color_attachments: &attachments, + timestamp_writes: self.timing.as_mut().and_then(|t| t.writes("clear")), ..Default::default() }); } @@ -1250,6 +1259,7 @@ impl PackPost { let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("Shader pack world"), color_attachments: &attachments, + timestamp_writes: self.timing.as_mut().and_then(|t| t.writes("world")), depth_stencil_attachment: self.blit_depth.then_some( wgpu::RenderPassDepthStencilAttachment { view: &targets.depth, @@ -1314,6 +1324,7 @@ impl PackPost { let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some(&pass.name), color_attachments: &attachments, + timestamp_writes: self.timing.as_mut().and_then(|t| t.writes(&pass.name)), ..Default::default() }); rp.set_pipeline(&pass.pipeline); diff --git a/crates/horizon-client/src/gfx/pack_timing.rs b/crates/horizon-client/src/gfx/pack_timing.rs new file mode 100644 index 000000000..61ab721e5 --- /dev/null +++ b/crates/horizon-client/src/gfx/pack_timing.rs @@ -0,0 +1,147 @@ +//! Developers' aid: `HORIZON_SHADER_PACK_TIMING=1` times every pass of a shader pack on the +//! GPU, one frame in `EVERY`, and logs each pass's median every `REPORT` timed frames. The +//! frame after a timed one waits for the GPU before reading the timestamps (Metal resolves +//! them without waiting for the passes that write them), so timing slows the game. + +const EVERY: u32 = 30; +const REPORT: usize = 30; +/// Passes timed per frame at most (two timestamps each). +const CAPACITY: u32 = 64; + +pub(super) struct PassTiming { + queries: wgpu::QuerySet, + resolve: wgpu::Buffer, + read: wgpu::Buffer, + period_ns: f32, + frame: u32, + /// The passes timed in the frame being recorded or waiting to be read, in order. + names: Vec, + timed: bool, + samples: Vec<(String, Vec)>, + reports: usize, +} + +impl PassTiming { + pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Option { + if std::env::var_os("HORIZON_SHADER_PACK_TIMING").is_none() + || !device.features().contains(wgpu::Features::TIMESTAMP_QUERY) + { + return None; + } + let size = u64::from(CAPACITY) * 2 * 8; + let buffer = |label, usage| { + device.create_buffer(&wgpu::BufferDescriptor { + label: Some(label), + size, + usage, + mapped_at_creation: false, + }) + }; + Some(Self { + queries: device.create_query_set(&wgpu::QuerySetDescriptor { + label: Some("Shader pack pass timing"), + ty: wgpu::QueryType::Timestamp, + count: CAPACITY * 2, + }), + resolve: buffer( + "Shader pack timing resolve", + wgpu::BufferUsages::QUERY_RESOLVE | wgpu::BufferUsages::COPY_SRC, + ), + read: buffer( + "Shader pack timing read", + wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, + ), + period_ns: queue.get_timestamp_period(), + frame: 0, + names: Vec::new(), + timed: false, + samples: Vec::new(), + reports: 0, + }) + } + + /// At the start of a frame: reads the last timed frame's passes, and decides whether this + /// one is timed. + pub fn begin(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) { + if self.timed && !self.names.is_empty() { + self.collect(device, queue); + } + self.names.clear(); + self.frame += 1; + self.timed = self.frame.is_multiple_of(EVERY); + } + + /// Timestamp writes for the pass `name`, in a timed frame. + pub fn writes(&mut self, name: &str) -> Option> { + let i = self.names.len() as u32; + if !self.timed || i >= CAPACITY { + return None; + } + self.names.push(name.to_owned()); + Some(wgpu::RenderPassTimestampWrites { + query_set: &self.queries, + beginning_of_pass_write_index: Some(2 * i), + end_of_pass_write_index: Some(2 * i + 1), + }) + } + + fn collect(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) { + // The timed frame first, then the resolve, so every timestamp is written. + _ = device.poll(wgpu::PollType::wait_indefinitely()); + let count = self.names.len() as u32 * 2; + let bytes = u64::from(count) * 8; + let mut encoder = device.create_command_encoder(&Default::default()); + encoder.resolve_query_set(&self.queries, 0..count, &self.resolve, 0); + encoder.copy_buffer_to_buffer(&self.resolve, 0, &self.read, 0, bytes); + queue.submit([encoder.finish()]); + let slice = self.read.slice(..bytes); + slice.map_async(wgpu::MapMode::Read, |_| {}); + _ = device.poll(wgpu::PollType::wait_indefinitely()); + let Ok(data) = slice.get_mapped_range() else { + return; + }; + let stamps: Vec = data + .as_chunks::<8>() + .0 + .iter() + .map(|b| u64::from_le_bytes(*b)) + .collect(); + drop(data); + self.read.unmap(); + // Passes overlap on tiled GPUs (a pass begins before the one before it ends): a pass + // costs the time from the later of its beginning and the previous pass's end. + let mut previous_end = 0; + for (name, pair) in self.names.iter().zip(stamps.as_chunks::<2>().0) { + if pair[0] == 0 || pair[1] < pair[0] { + continue; + } + let from = pair[0].max(previous_end).min(pair[1]); + previous_end = pair[1]; + let ms = (pair[1] - from) as f32 * self.period_ns / 1e6; + match self.samples.iter_mut().find(|s| s.0 == *name) { + Some(s) => s.1.push(ms), + None => self.samples.push((name.clone(), vec![ms])), + } + } + self.reports += 1; + if self.reports.is_multiple_of(REPORT) { + self.report(); + } + } + + fn report(&mut self) { + let mut total = 0.0; + let mut parts = Vec::new(); + for (name, v) in &mut self.samples { + v.sort_by(f32::total_cmp); + let median = v[v.len() / 2]; + total += median; + parts.push(format!("{name} {median:.3}")); + v.clear(); + } + log::info!( + "shader pack GPU time per pass (median ms of {REPORT} frames): {} = {total:.3}", + parts.join(", ") + ); + } +} From 1d90d72a64418071f943e404166fa39b09f50981 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:14:59 +0100 Subject: [PATCH 012/238] Server dialogs: the packets and the dialog model show_dialog and clear_dialog in configuration and play, and custom_click_action, the answer, in both. Dialog.DIRECT_CODEC as horizon_client::dialog: the five dialog types, their bodies, inputs and actions with vanilla's defaults, the values inputs hold, and Action.createAction (a command template's $(key) substitution with escapeWithoutQuotes, a custom action's payload of every input's tag). Nothing uses them yet: the screen comes next. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/dialog.rs | 794 ++++++++++++++++++ crates/horizon-client/src/lib.rs | 1 + .../src/packets/configuration.rs | 52 ++ crates/horizon-protocol/src/packets/play.rs | 60 +- 4 files changed, 905 insertions(+), 2 deletions(-) create mode 100644 crates/horizon-client/src/dialog.rs diff --git a/crates/horizon-client/src/dialog.rs b/crates/horizon-client/src/dialog.rs new file mode 100644 index 000000000..7acd3e86a --- /dev/null +++ b/crates/horizon-client/src/dialog.rs @@ -0,0 +1,794 @@ +//! Server dialogs (`net.minecraft.server.dialog`): what `Dialog.DIRECT_CODEC` +//! decodes from a `show_dialog` packet, the values its inputs hold, and the +//! click event an action makes of them (`Action.createAction`). +use crate::text::Text; +use horizon_world::nbt::Tag; +use serde_json::{Value, json}; + +/// `CommonDialogData` and the type's own fields. +#[derive(Clone, Debug)] +pub struct Dialog { + pub kind: Kind, + pub title: Text, + pub external_title: Option, + pub can_close_with_escape: bool, + pub pause: bool, + pub after_action: AfterAction, + pub body: Vec, + pub inputs: Vec, +} + +/// The dialog types (`DialogTypes`). +#[derive(Clone, Debug)] +pub enum Kind { + /// `NoticeDialog`: one button, "OK" when the server names none. + Notice(Button), + Confirmation { + yes: Button, + no: Button, + }, + MultiAction { + actions: Vec + + + +
+ + +
+ + +
+{{HUB_OVERVIEW}} +
+ +
+
+{{HUB_AREAS}} +
+
+{{HUB_ROADMAP}} +
+

The roadmap lives in the repository: ROADMAP.md, generated from roadmap/roadmap.toml. Pull requests carry the roadmap:<id> label of their item.

+
+ +
+
+

Good first tasks

+

Each one is a small difference from vanilla, with an issue that says how to see it.

+

Each links its issue; the technical text says where in the code and in vanilla to start.

+
    {{HUB_HELP_WANTED}}
+
+
+

Ways to help

+
+
+

Tell us what differs

+

Something that doesn't play like vanilla is a bug. Say what you did, what happened and what vanilla does, with your Horizon version and system.

+

DiscordGitHub issue

+
+
+

Take a task

+

Pick a good first task or an open issue. On Discord, /claim 1234 tells everyone you are on issue 1234, so nobody starts the same work; /unclaim frees it.

+

Open issues

+
+
+

Bring your AI agent

+

On Discord, /agent connect gives your coding agent a personal token for Kira's MCP server: it reads the work board, claims tasks and posts its progress in your name.

+

Join the Discord

+
+
+

Benchmark your computer

+

Run the built-in benchmark and share the report: every new machine makes the performance work more accurate. No coding needed.

+

How to

+
+
+
+
+

The rules

+
    +
  1. Vanilla is the specification. Wherever Minecraft 26.3 has the feature, Horizon does exactly what it does: read vanilla's code, then match it.
  2. +
  3. Small pull requests, proven. One change each, under about 500 lines, with the block python3 tools/pr_ready.py prints: formatting, Clippy, tests and the harnesses your change can affect.
  4. +
  5. Say who wrote it. If an AI agent wrote or posts it, the pull request says so, and commits keep its co-author line.
  6. +
  7. Never commit Mojang's files. No game jars, assets, decompiled code or prepared resources, and nothing taken from other mods' code.
  8. +
+

The contributing guideCONTRIBUTING.md

+
+
+

Not an official Minecraft product. Not approved by or associated with Mojang or Microsoft.

+ + +{{SITE_FOOTER}} + + + diff --git a/website/src/hub.js b/website/src/hub.js new file mode 100644 index 000000000..37e26738c --- /dev/null +++ b/website/src/hub.js @@ -0,0 +1,175 @@ +// The hub (/hub): the roadmap from website/data/roadmap.json (generated from roadmap/roadmap.toml by +// tools/roadmap.py), with each item's issues and pull requests in their live GitHub state. Shared by build.mjs +// (baked into the page) and hub-page.js (refreshed from GitHub in the browser). Every item says the same thing +// twice: `plain` (.hub-plain) for players and `dev` (.hub-dev) for contributors; the page's Simple / Technical +// switch shows one of them. +import { escape, inline } from "./markdown.js"; + +export const REPO = "ludovic111/horizon"; +const issueUrl = (n) => `https://github.com/${REPO}/issues/${n}`; +const fileUrl = (path) => `https://github.com/${REPO}/blob/main/${path}`; + +// The issues a pull request closes when merged: "Fixes #12", "closes #3, resolves #4" (as Kira's board reads them). +export function fixesIn(text) { + const out = new Set(); + for (const m of String(text ?? "").matchAll(/\b(?:fix(?:e[sd])?|close[sd]?|resolve[sd]?)\s*:?\s+((?:#\d+(?:\s*(?:,|and)\s*)?)+)/gi)) { + for (const n of m[1].matchAll(/#(\d+)/g)) out.add(Number(n[1])); + } + return [...out].sort((a, b) => a - b); +} + +// A GitHub issue or pull request (from /issues, which lists both) reduced to what the hub shows. +export function slimIssue(raw) { + const pr = Boolean(raw.pull_request); + const merged = pr && raw.pull_request.merged_at; + return { + n: raw.number, + title: String(raw.title || ""), + kind: pr ? "pr" : "issue", + state: merged ? "merged" : raw.state === "open" ? "open" : raw.state_reason === "not_planned" ? "dropped" : "closed", + draft: Boolean(raw.draft), + user: raw.user?.login || "", + labels: (raw.labels || []).map((l) => (typeof l === "string" ? l : l.name)).filter(Boolean), + assigned: (raw.assignees || []).length > 0, + fixes: pr ? fixesIn(raw.body) : [], + at: merged || raw.closed_at || raw.updated_at || "", + url: raw.html_url || issueUrl(raw.number), + }; +} + +const number = (link) => Number((String(link).match(/^#(\d+)$/) || [])[1]) || null; + +// "#12 in review", "#13 merged": a link's state, when GitHub told us. +function linkState(issue) { + if (!issue) return null; + if (issue.kind === "pr") return issue.state === "open" ? (issue.draft ? "in progress" : "in review") : issue.state; + return issue.state === "open" ? "open" : "closed"; +} + +function linkHtml(link, live) { + const n = number(link); + if (!n) { + const url = link.startsWith("https://") ? link : fileUrl(link); + return `${escape(link.replace(/^https:\/\//, ""))}`; + } + const issue = live.get(n); + const state = linkState(issue); + const label = issue ? `${issue.kind === "pr" ? "Pull request" : "Issue"} #${n}: ${issue.title}` : `#${n}`; + return `#${n}${state ? ` ${state}` : ""}`; +} + +// The technical text with its #N and the item's own paths as links. +function devHtml(item) { + let text = item.dev.replace(/(^|[^\w[/`])#(\d+)\b/g, (_, before, n) => `${before}[#${n}](${issueUrl(n)})`); + for (const link of item.links || []) { + if (!number(link) && !link.startsWith("https://")) text = text.replace(link, `[${link}](${fileUrl(link)})`); + } + return inline(text); +} + +// "2 pull requests in review · 3 merged · 1 issue open", from the item's links. +export function progress(item, live) { + const known = (item.links || []).map(number).filter(Boolean).map((n) => live.get(n)).filter(Boolean); + const pulls = known.filter((i) => i.kind === "pr"); + const count = (list, word) => (list.length ? `${list.length} ${word}` : ""); + const parts = [ + count(pulls.filter((p) => p.state === "open" && !p.draft), pulls.filter((p) => p.state === "open" && !p.draft).length === 1 ? "pull request in review" : "pull requests in review"), + count(pulls.filter((p) => p.state === "open" && p.draft), "in progress"), + count(pulls.filter((p) => p.state === "merged"), "merged"), + count(known.filter((i) => i.kind === "issue" && i.state === "open"), known.filter((i) => i.kind === "issue" && i.state === "open").length === 1 ? "issue open" : "issues open"), + ].filter(Boolean); + return parts.join(" · "); +} + +const areaName = (roadmap, id) => roadmap.areas.find((a) => a.id === id)?.name || id; + +export function itemHtml(item, roadmap, live) { + const done = item.status === "shipped"; + const state = done ? "" : progress(item, live); + return `
+

${escape(areaName(roadmap, item.area))}${done ? `${escape(item.shipped_in)}` : ""}${item.ongoing ? `Ongoing` : ""}

+

${escape(item.title)}

+

${inline(item.plain)}

+

${devHtml(item)}

+ ${state ? `

${escape(state)}

` : ""} + +
`; +} + +const SECTION_LEDE = { + now: "Being worked on, with pull requests open or just merged.", + next: "Decided, and up after what is being done now.", + later: "Decided, further out.", + "help-wanted": "Good first contributions: a clear vanilla behaviour, an issue with the steps, a place to start.", + shipped: "Out in a release. The journal has every change.", +}; + +// The Roadmap tab: one section per status, the items in the file's order (areas by priority). +export function roadmapHtml(roadmap, live = new Map()) { + const order = new Map(roadmap.areas.map((a, i) => [a.id, i])); + return roadmap.statuses + .map(({ id, name }) => { + const items = roadmap.items.filter((i) => i.status === id); + if (!items.length) return ""; + // Shipped stays newest first; the others go by the areas' priority. + if (id !== "shipped") items.sort((a, b) => order.get(a.area) - order.get(b.area)); + return `
+

${escape(name)}

${escape(SECTION_LEDE[id] || "")}

+
+${items.map((item) => itemHtml(item, roadmap, live)).join("\n")} +
+
`; + }) + .join("\n"); +} + +// The area filter's buttons: every area that has an item. +export function areaFilterHtml(roadmap) { + const used = roadmap.areas.filter((a) => roadmap.items.some((i) => i.area === a.id)); + return [``] + .concat(used.map((a) => ``)) + .join("\n"); +} + +// The Overview tab: where Horizon is, the 3 to 5 things in focus, what is next, what shipped last, how to help. +export function overviewHtml(roadmap, live = new Map()) { + const o = roadmap.overview; + const focus = roadmap.items.filter((i) => i.focus); + const next = roadmap.items.filter((i) => i.status === "next"); + const shipped = roadmap.items.filter((i) => i.status === "shipped").slice(0, 3); + const row = (item, i, meta) => `
  • ${String(i + 1).padStart(2, "0")}${escape(item.title)}${inline(item.plain)}${inline(item.dev)}${escape(meta(item))}
  • `; + return `
    +

    ${inline(o.today_plain)}

    +

    ${inline(o.today_dev)}

    +
    +
    +

    Being done now

    +
      ${focus.map((item, i) => row(item, i, (x) => progress(x, live))).join("")}
    +
    +
    +

    Next

    +
      ${next.map((item, i) => row(item, i, (x) => areaName(roadmap, x.area))).join("")}
    +
    +
    +

    Recently shipped

    +
      ${shipped.map((item, i) => row(item, i, (x) => `In ${x.shipped_in}`)).join("")}
    +
    +
    +

    How to help

    +

    ${inline(o.help_plain)}

    +

    ${inline(o.help_dev)}

    +

    ContributeHelp wanted

    +
    `; +} + +// The Contribute tab's list of good first tasks. +export function helpWantedHtml(roadmap) { + return roadmap.items + .filter((i) => i.status === "help-wanted") + .map((item, i) => { + const first = (item.links || []).map(number).find(Boolean); + const href = first ? issueUrl(first) : `#item-${item.id}`; + return `
  • ${String(i + 1).padStart(2, "0")}${escape(item.title)}${inline(item.plain)}${inline(item.dev)}${first ? `#${first}` : ""}
  • `; + }) + .join(""); +} diff --git a/website/src/hub.test.mjs b/website/src/hub.test.mjs new file mode 100644 index 000000000..e73c9a8af --- /dev/null +++ b/website/src/hub.test.mjs @@ -0,0 +1,48 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import { fixesIn, overviewHtml, progress, roadmapHtml, slimIssue } from "./hub.js"; + +const roadmap = JSON.parse(readFileSync(new URL("../data/roadmap.json", import.meta.url), "utf8")); + +const pr = (number, extra = {}) => ({ number, title: `PR ${number}`, state: "open", pull_request: { merged_at: null }, user: { login: "a" }, labels: [], assignees: [], body: "", ...extra }); +const issue = (number, extra = {}) => ({ number, title: `Issue ${number}`, state: "open", user: { login: "b" }, labels: [{ name: "bug" }], assignees: [], ...extra }); + +test("pull requests and issues reduce to their hub state", () => { + assert.deepEqual(fixesIn("Fixes #12, #13 and closes #4. Mentions #9."), [4, 12, 13]); + const merged = slimIssue(pr(5, { state: "closed", pull_request: { merged_at: "2030-01-01T00:00:00Z" }, body: "Fixes #3" })); + assert.equal(merged.kind, "pr"); + assert.equal(merged.state, "merged"); + assert.deepEqual(merged.fixes, [3]); + assert.equal(slimIssue(issue(6, { state: "closed", state_reason: "not_planned" })).state, "dropped"); + assert.deepEqual(slimIssue(issue(7)).labels, ["bug"]); +}); + +test("an item's progress counts its pull requests and issues", () => { + const live = new Map([ + [1, slimIssue(pr(1))], + [2, slimIssue(pr(2, { draft: true }))], + [3, slimIssue(pr(3, { state: "closed", pull_request: { merged_at: "2030-01-01T00:00:00Z" } }))], + [4, slimIssue(issue(4))], + ]); + const item = { links: ["#1", "#2", "#3", "#4", "#99", "docs/x.md"] }; + assert.equal(progress(item, live), "1 pull request in review · 1 in progress · 1 merged · 1 issue open"); + assert.equal(progress(item, new Map()), ""); +}); + +test("the roadmap renders every item once, with both texts, in its status's section", () => { + const html = roadmapHtml(roadmap); + for (const item of roadmap.items) { + assert.equal(html.split(`id="item-${item.id}"`).length, 2, item.id); + } + assert.ok(html.indexOf('id="now"') < html.indexOf('id="next"')); + assert.ok(html.includes('class="hub-plain"') && html.includes('class="hub-dev"')); + // Shipped items say which release had them. + assert.ok(html.includes(`>${roadmap.items.find((i) => i.status === "shipped").shipped_in}<`)); +}); + +test("the overview lists the focus items and never nests links", () => { + const html = overviewHtml(roadmap); + for (const item of roadmap.items.filter((i) => i.focus)) assert.ok(html.includes(`href="#item-${item.id}"`), item.id); + for (const row of html.match(/
  • [\s\S]*?<\/li>/g)) assert.equal(row.split(" .title { margin-bottom: 16px; } +.hub-intro { margin-bottom: 16px; color: var(--ink-soft); max-width: var(--measure); } +.hub-rows .row > a { grid-template-columns: var(--index) minmax(0, 1fr) auto; align-items: baseline; } +.hub-row-body { display: grid; gap: 4px; min-width: 0; } +.hub-row-title { font-size: clamp(18px, 1.8vw, 22px); font-weight: 500; color: var(--ink); } +.hub-row-body .hub-plain, .hub-row-body .hub-dev { color: var(--ink-soft); font-size: var(--text-ui); max-width: 80ch; } +.hub-rows .row-meta { max-width: 16rem; } +.hub-section { margin-top: 48px; } +.hub-section-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 8px 20px; } +.hub-items { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr)); gap: 8px; margin-top: 16px; } +.hub-item { + display: flex; flex-direction: column; gap: 10px; padding: 18px 20px; scroll-margin-top: calc(var(--nav) + 24px); + background: var(--panel); border: var(--block-outline) solid var(--outline); box-shadow: var(--panel-edges); +} +.hub-item:target { box-shadow: var(--panel-edges), 0 0 0 3px var(--accent); } +.hub-item-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; } +.hub-item-meta .badge { margin-right: 0; } +.hub-ongoing { font: var(--text-micro) / 1 var(--mono); letter-spacing: var(--text-micro-tracking); color: var(--muted); } +.hub-item-title { font-size: var(--text-heading); line-height: var(--text-heading-line); } +.hub-item .hub-plain, .hub-item .hub-dev { font-size: var(--text-ui); color: var(--ink-soft); } +.hub-progress { font: var(--text-micro) / 1.4 var(--mono); letter-spacing: var(--text-micro-tracking); color: var(--ink); } +.hub-links { display: flex; flex-wrap: wrap; gap: 6px; margin-top: auto; padding-top: 4px; } +.hub-link { + display: inline-flex; gap: 6px; align-items: baseline; padding: 3px 7px; font: var(--text-micro) / 1.3 var(--mono); + color: var(--ink-soft); background: var(--well); box-shadow: 0 0 0 var(--block-outline) var(--outline); +} +.hub-link span { color: var(--muted); } +.hub-link:hover { color: var(--accent); } +.hub-link.is-merged { background: color-mix(in srgb, var(--success) 18%, var(--well)); } +.hub-link.is-in-review { background: color-mix(in srgb, var(--accent) 18%, var(--well)); } +.hub-link.is-in-progress { background: color-mix(in srgb, var(--warn) 22%, var(--well)); } +.hub-link.is-merged span, .hub-link.is-in-review span, .hub-link.is-in-progress span { color: var(--ink); } +.hub-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 260px), 1fr)); gap: 8px; } +.hub-cards .highlight { justify-content: flex-start; } +.hub-cards h3 { font-size: var(--text-heading); line-height: var(--text-heading-line); } +.hub-cards p { color: var(--ink-soft); font-size: var(--text-ui); } +.hub-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; } +.hub-cards .hub-actions { margin-top: auto; } +.hub-rules { list-style: decimal; padding-left: 20px; max-width: var(--measure); } +.hub-rules li + li { margin-top: 10px; } +.hub-rules li::marker { color: var(--muted); font-family: var(--mono); } +.hub-source { margin-top: 40px; max-width: var(--measure); } +@media (max-width: 720px) { + .hub-rows .row > a { grid-template-columns: var(--index) minmax(0, 1fr); } + .hub-rows .row-meta { grid-column: 2; text-align: left; } +} From 8ffd595575358067bf29cf02a7166bffef56af9b Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 25 Oct 2026 14:58:42 +0000 Subject: [PATCH 085/238] fix: suppress secondary explosions from destroyed End crystals Pass the crystal damage cause through destruction so explosion damage removes crystals without another blast, including during dragon respawn. Keep fight callbacks and direct-hit blasts. Co-Authored-By: OpenAI Codex --- crates/horizon-client/src/game/dragon.rs | 40 +++++++++++++------ tools/lan_e2e.py | 50 ++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 12 deletions(-) diff --git a/crates/horizon-client/src/game/dragon.rs b/crates/horizon-client/src/game/dragon.rs index 54b260c67..701c09e64 100644 --- a/crates/horizon-client/src/game/dragon.rs +++ b/crates/horizon-client/src/game/dragon.rs @@ -336,6 +336,12 @@ enum StrafeRequest { /// Anything else: the player nearest the crystal. Nearest(Vec3), } +/// Whether `EndCrystal.hurtServer` creates a blast before notifying the fight. +#[derive(Clone, Copy, PartialEq)] +enum CrystalDamage { + Direct, + Explosion, +} /// What `EnderDragon.hurt` made of a hit. #[derive(Clone, Copy, Debug, PartialEq)] pub(super) enum Hurt { @@ -867,8 +873,9 @@ impl Game<'_> { } } } - /// A crystal took damage: `EndCrystal.hurtServer` blows it up (power 6). - fn destroy_crystal(&mut self, index: usize, by: Option) { + /// `EndCrystal.hurtServer`: remove it, blast only for non-explosion damage, + /// then notify the dragon fight even when no blast was created. + fn destroy_crystal(&mut self, index: usize, by: Option, damage: CrystalDamage) { if index >= self.crystals.len() || self.crystals[index].invulnerable { return; } @@ -880,14 +887,23 @@ impl Game<'_> { .iter() .any(|p| p.distance_squared(c.position) < 0.01) { - let at = [c.position.x, c.position.y, c.position.z]; - let _ = - self.server_explode(at, 6.0, super::explosion::BlockInteraction::Destroy, false); + if damage == CrystalDamage::Direct { + let at = [c.position.x, c.position.y, c.position.z]; + let _ = self.server_explode( + at, + 6.0, + super::explosion::BlockInteraction::Destroy, + false, + ); + } self.abort_respawn(); return; } - let at = [c.position.x, c.position.y, c.position.z]; - let _ = self.server_explode(at, 6.0, super::explosion::BlockInteraction::Destroy, false); + if damage == CrystalDamage::Direct { + let at = [c.position.x, c.position.y, c.position.z]; + let _ = + self.server_explode(at, 6.0, super::explosion::BlockInteraction::Destroy, false); + } // EnderDragonFight.onCrystalDestroyed. self.count_crystals(); if let Some(d) = self.dragon.as_mut() { @@ -962,7 +978,7 @@ impl Game<'_> { return Hurt::Refused; } let by = by_player.then(|| self.acting_player()); - self.destroy_crystal(index, by); + self.destroy_crystal(index, by, CrystalDamage::Direct); Hurt::Landed { full: true } } else if id >= SPECIAL { self.hurt_dragon((id - SPECIAL) as usize, amount, by_player, arrow) @@ -2634,8 +2650,8 @@ impl Crystal { } } impl Game<'_> { - /// `ServerExplosion.hurtEntities` on the end crystals (each in reach - /// blows up in turn) and the dragon (explosions always hurt dragons). + /// `ServerExplosion.hurtEntities`: crystals in reach are removed without + /// another blast; explosions always hurt dragons. pub(super) fn explosion_hits_special(&mut self, center: [f64; 3], radius: f32) { let diameter = f64::from(radius * 2.0); let in_reach = |p: Vec3| { @@ -2651,9 +2667,9 @@ impl Game<'_> { .map(|c| c.position) .collect(); for at in crystals { - // One blowing up may already have taken the next. + // Look up the current index after each removal. if let Some(i) = self.crystals.iter().position(|c| c.position == at) { - self.destroy_crystal(i, None); + self.destroy_crystal(i, None, CrystalDamage::Explosion); } } let Some(d) = self.dragon.as_ref() else { diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 320a9c56a..9aa1e1278 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -6252,5 +6252,55 @@ CHECKS += ['crystals'] CHECK_FNS.update(crystals=check_crystals) +def check_crystal_blast(host, guest, out): + """#1533: TNT removes a crystal without a blast; a punched crystal blasts once. + Both clients must see the surviving distant and invulnerable crystals.""" + spots = apart(host, guest) + seen = {} + for label, player, other in (('host', host, guest), ('guest', guest, host)): + x, y, z = spots[label] + y += 32 + player('command', command='gamemode creative') + for offset, cause in enumerate(('tnt', 'melee')): + # Leave survivors in separate arenas; the scratch world is discarded. + z = spots[label][2] + (0 if label == 'host' else 80) + offset * 40 + case = f'{label}-{cause}' + host('command', command=f'fill {x - 3} {y - 1} {z - 3} {x + 25} {y - 1} {z + 5} minecraft:bedrock') + host('command', command=f'fill {x - 3} {y} {z - 3} {x + 25} {y + 4} {z + 5} minecraft:air') + positions = [[x + dx, y, z + dz] for dx, dz in ((2.5, 0.5), (12.5, 0.5), (22.5, 0.5), (4.5, 3.5))] + + def remaining(game): + crystals = [p for p in game('status')['projectiles'] if p['kind'] == 'minecraft:end_crystal'] + return [i for i, at in enumerate(positions) + if any(math.dist(p['position'], at) < 0.01 for p in crystals)] + + for i, at in enumerate(positions): + host('command', command=f'summon minecraft:end_crystal {at[0]} {at[1]} {at[2]} {{Invulnerable:{1 if i == 3 else 0}b}}') + player('command', command=f'tp @s {x + 0.5} {y} {z + 0.5}') + other('command', command='gamemode creative') + other('command', command=f'tp @s {x + 0.5} {y} {z + 4.5}') + player('command', command='clear @s') + player.wait(20) + for game in (host, guest): + assert remaining(game) == [0, 1, 2, 3], f'{case}: crystals not tracked on {game.name}' + if cause == 'tnt': + player('command', command=f'summon minecraft:tnt {x + 0.5} {y} {z + 0.5} {{fuse:10s}}') + else: + player.raw('look-at', positions[0][0], y + 1, positions[0][2]) + player.raw('mouse', 'left', 'click') + player.wait(80) + result = {game.name: remaining(game) for game in (host, guest)} + seen[case] = result + # TNT reaches the first crystal only. A direct crystal blast reaches + # the second too, but that crystal must not detonate the third. + expected = [1, 2, 3] if cause == 'tnt' else [2, 3] + assert all(ids == expected for ids in result.values()), f'{case}: expected {expected}, got {result}' + return seen + + +CHECKS += ['crystal_blast'] +CHECK_FNS.update(crystal_blast=check_crystal_blast) + + if __name__ == '__main__': main() From 68234ac4075d4e55b2b5ee5f3acc5d5e9136b08e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 15:59:31 +0100 Subject: [PATCH 086/238] Hub: the roadmap at /data/roadmap.json and items at hub#, as Kira reads them Kira (kira #10-#14) fetches https://playhorizon.dev/data/roadmap.json and links items as #: the JSON moves there (readable from any origin) with the announce flag, and each item's anchor is its id. Ids the page uses itself (tabs, sections, headings) are refused by the check; a test keeps every id of the page unique, which caught the overview's headings sharing ids with the roadmap's. Co-Authored-By: Claude Opus 5.5 --- tests/test_roadmap.py | 4 ++++ tools/roadmap.py | 10 ++++++++-- website/Caddyfile | 5 ++++- website/README.md | 2 +- website/build.mjs | 6 +++--- website/data/roadmap.json | 15 ++++++++++----- website/src/hub-page.js | 6 +++--- website/src/hub.js | 22 +++++++++++----------- website/src/hub.test.mjs | 11 +++++++++-- 9 files changed, 53 insertions(+), 28 deletions(-) diff --git a/tests/test_roadmap.py b/tests/test_roadmap.py index 641d4e11b..605843f25 100644 --- a/tests/test_roadmap.py +++ b/tests/test_roadmap.py @@ -121,6 +121,10 @@ class Rules(unittest.TestCase): self.assertTrue(any("link" in p for p in self.problems(links=["docs/nothing-here.md"]))) self.assertTrue(any("link" in p for p in self.problems(links=["http://example.com"]))) + def test_ids_are_free_anchors_of_the_hub(self): + for taken in ("roadmap", "now", "h-next", "tab-board"): + self.assertTrue(any("taken by the hub page" in p for p in self.problems(id=taken)), taken) + def test_flags_are_true_or_false(self): self.assertEqual(self.problems(announce=True), []) self.assertTrue(any("announce is true or false" in p for p in self.problems(announce="yes"))) diff --git a/tools/roadmap.py b/tools/roadmap.py index 1a4f58136..f09224147 100644 --- a/tools/roadmap.py +++ b/tools/roadmap.py @@ -28,7 +28,7 @@ from pathlib import Path ROOT = Path(__file__).resolve().parent.parent SOURCE = ROOT / "roadmap" / "roadmap.toml" OUTPUT = ROOT / "ROADMAP.md" -# What the website's hub reads (website/build.mjs), and serves at /hub/roadmap.json for Kira and others. +# What the website's hub reads (website/build.mjs), and serves at /data/roadmap.json for Kira and others. SITE_DATA = ROOT / "website" / "data" / "roadmap.json" REPO = "ludovic111/horizon" SITE = "https://playhorizon.dev/hub" @@ -52,6 +52,9 @@ ITEM_KEYS = {"id", "title", "status", "area", "plain", "dev", "links", "focus", ISSUE = re.compile(r"#(\d+)$") ID = re.compile(r"[a-z0-9]+(-[a-z0-9]+)*$") +# An item is the anchor playhorizon.dev/hub#: it can't take the name of a tab, a section or a heading. +RESERVED = {"main", "overview", "roadmap", "board", "journal", "contribute", "hub-data", *STATUSES} +RESERVED_PREFIXES = ("h-", "tab-", "week-") RELEASE = re.compile(r"(v\d[\w.]*|launcher@\d[\w.]*|launcher-v\d[\w.]*|website)$") MONTHS = ("January|February|March|April|June|July|August|September|October|November|December") # Kira's rule: no dates and no promises of when. "Coming soon" is the one allowed phrase. @@ -105,6 +108,8 @@ def validate(data, root=ROOT): ident = item.get("id", "") if ident and not ID.match(ident): problems.append(f"{where}: ids are lowercase words joined by dashes") + if ident in RESERVED or ident.startswith(RESERVED_PREFIXES): + problems.append(f"{where}: the id is taken by the hub page (hub#{ident} is a tab, a section or a heading)") if ident in seen: problems.append(f"{where}: the id appears twice") seen.add(ident) @@ -231,7 +236,8 @@ def render(data): def site_data(data): """website/data/roadmap.json: the items as the hub and Kira read them, in the file's order.""" - keys = ("id", "title", "status", "area", "plain", "dev", "links", "focus", "ongoing", "shipped_in", "post") + keys = ("id", "title", "status", "area", "plain", "dev", "links", "focus", "ongoing", "shipped_in", "post", + "announce") out = { "format": "horizon-roadmap", "version": 1, diff --git a/website/Caddyfile b/website/Caddyfile index 50ba44381..a160f2259 100644 --- a/website/Caddyfile +++ b/website/Caddyfile @@ -38,8 +38,11 @@ redir /roadmap /hub#roadmap permanent @doc path_regexp doc ^/docs/([a-z-]+)$ rewrite @doc /docs/{re.doc.1}.html - @pages path / /index.html /404.html /privacy.html /releases /releases.html /benchmarks /benchmarks.html /benchmarks.json /hub /hub.html /hub/* /docs /docs/* /sitemap.xml + @pages path / /index.html /404.html /privacy.html /releases /releases.html /benchmarks /benchmarks.html /benchmarks.json /hub /hub.html /data/* /docs /docs/* /sitemap.xml header @pages Cache-Control "no-cache" + # The hub's data for Kira and anyone else: readable from any origin. + @data path /data/* + header @data Access-Control-Allow-Origin "*" @legal path /LEGAL.md /fonts/*.txt header @legal Content-Type "text/plain; charset=utf-8" diff --git a/website/README.md b/website/README.md index c73683c73..8f35fbcf4 100644 --- a/website/README.md +++ b/website/README.md @@ -42,7 +42,7 @@ src/releases.js the home page's "Recently added" and the Releases page's mark src/contributors.js the contributors' filtering and markup, shared by build and page src/hub.js the hub's markup from data/roadmap.json and GitHub's issue states, shared by build and page src/hub-page.js the hub page: tabs by #hash, the Simple / Technical switch (localStorage), the area filter, live states -data/roadmap.json the roadmap, generated by tools/roadmap.py from roadmap/roadmap.toml (do not edit); served at /hub/roadmap.json +data/roadmap.json the roadmap, generated by tools/roadmap.py from roadmap/roadmap.toml (do not edit); served at /data/roadmap.json public/ fonts, screenshots, icons, og.png, robots.txt, benchmarks.json: copied as is (sitemap.xml is written by the build) data/benchmarks/ our raw benchmark reports (tools/perf_bench.py) and fleet.json, the list scripts/benchmarks.mjs reads scripts/benchmarks.mjs benchmark reports -> public/benchmarks.json (ours, and players' groups of 3+ installs) diff --git a/website/build.mjs b/website/build.mjs index b36e0e103..563bc6fa4 100644 --- a/website/build.mjs +++ b/website/build.mjs @@ -428,9 +428,9 @@ for (const { file, values: own } of docPages) { docPageFiles.push(file); } -// The roadmap for other readers (Kira, scripts): /hub/roadmap.json, as tools/roadmap.py wrote it. -await mkdir(join(dist, "hub"), { recursive: true }); -await cp(join(site, "data", "roadmap.json"), join(dist, "hub", "roadmap.json")); +// The roadmap for other readers (Kira, scripts): /data/roadmap.json, as tools/roadmap.py wrote it. +await mkdir(join(dist, "data"), { recursive: true }); +await cp(join(site, "data", "roadmap.json"), join(dist, "data", "roadmap.json")); // The pages' inline scripts (they mark the page as scripted and apply the saved theme before CSS // applies) and the inlined stylesheet: the Content-Security-Policy in the Caddyfile allows them by hash. diff --git a/website/data/roadmap.json b/website/data/roadmap.json index 142b27011..d4124e534 100644 --- a/website/data/roadmap.json +++ b/website/data/roadmap.json @@ -116,7 +116,8 @@ "#1757", "docs/modpacks.md" ], - "focus": true + "focus": true, + "announce": true }, { "id": "server-clients", @@ -175,7 +176,8 @@ "#1640", "docs/mods.md" ], - "focus": true + "focus": true, + "announce": true }, { "id": "server-dialogs", @@ -232,7 +234,8 @@ "links": [ "#1787", "#1788" - ] + ], + "announce": true }, { "id": "lean-code", @@ -262,7 +265,8 @@ "links": [ "docs/microsoft-account.md", "#1270" - ] + ], + "announce": true }, { "id": "curseforge", @@ -274,7 +278,8 @@ "links": [ "docs/mod-browser.md", "docs/modpacks.md" - ] + ], + "announce": true }, { "id": "benchmarks", diff --git a/website/src/hub-page.js b/website/src/hub-page.js index 039e98826..97d6f3d60 100644 --- a/website/src/hub-page.js +++ b/website/src/hub-page.js @@ -1,4 +1,4 @@ -// The hub page (/hub): its tabs (one per #hash, so /hub#roadmap and /hub#item- can be linked), the +// The hub page (/hub): its tabs (one per #hash, so /hub#roadmap and /hub# can be linked), the // Simple / Technical switch (remembered in localStorage), the area filter, and the live state of every // linked issue and pull request, refreshed from GitHub's public API. Without JavaScript, every tab reads // one after the other with what was baked at build time. @@ -30,7 +30,7 @@ function choose(tab, { focus = false, push = false } = {}) { if (focus) tab.focus(); if (push && location.hash !== `#${panelOf(tab).id}`) history.replaceState(null, "", `#${panelOf(tab).id}`); } -// "#roadmap" opens its tab; "#item-x" or "#help-wanted" opens the tab holding it and scrolls there. +// "#roadmap" opens its tab; "#" or "#help-wanted" opens the tab holding it and scrolls there. function follow() { const target = location.hash && document.getElementById(decodeURIComponent(location.hash.slice(1))); const tab = tabs.find((t) => target && (panelOf(t) === target || panelOf(t).contains(target))) || tabs[0]; @@ -116,4 +116,4 @@ async function refresh() { setUpTheme(); setUpMenu(); follow(); -refresh().then(() => location.hash.startsWith("#item-") && follow()); +refresh().then(() => data.roadmap.items.some((i) => `#${i.id}` === location.hash) && follow()); diff --git a/website/src/hub.js b/website/src/hub.js index 37e26738c..ecfb4da80 100644 --- a/website/src/hub.js +++ b/website/src/hub.js @@ -86,7 +86,7 @@ const areaName = (roadmap, id) => roadmap.areas.find((a) => a.id === id)?.name | export function itemHtml(item, roadmap, live) { const done = item.status === "shipped"; const state = done ? "" : progress(item, live); - return `
    + return `

    ${escape(areaName(roadmap, item.area))}${done ? `${escape(item.shipped_in)}` : ""}${item.ongoing ? `Ongoing` : ""}

    ${escape(item.title)}

    ${inline(item.plain)}

    @@ -137,25 +137,25 @@ export function overviewHtml(roadmap, live = new Map()) { const focus = roadmap.items.filter((i) => i.focus); const next = roadmap.items.filter((i) => i.status === "next"); const shipped = roadmap.items.filter((i) => i.status === "shipped").slice(0, 3); - const row = (item, i, meta) => `
  • ${String(i + 1).padStart(2, "0")}${escape(item.title)}${inline(item.plain)}${inline(item.dev)}${escape(meta(item))}
  • `; + const row = (item, i, meta) => `
  • ${String(i + 1).padStart(2, "0")}${escape(item.title)}${inline(item.plain)}${inline(item.dev)}${escape(meta(item))}
  • `; return `

    ${inline(o.today_plain)}

    ${inline(o.today_dev)}

    -
    -

    Being done now

    +
    +

    Being done now

      ${focus.map((item, i) => row(item, i, (x) => progress(x, live))).join("")}
    -
    -

    Next

    +
    +

    Next

      ${next.map((item, i) => row(item, i, (x) => areaName(roadmap, x.area))).join("")}
    -
    -

    Recently shipped

    +
    +

    Recently shipped

      ${shipped.map((item, i) => row(item, i, (x) => `In ${x.shipped_in}`)).join("")}
    -
    -

    How to help

    +
    +

    How to help

    ${inline(o.help_plain)}

    ${inline(o.help_dev)}

    ContributeHelp wanted

    @@ -168,7 +168,7 @@ export function helpWantedHtml(roadmap) { .filter((i) => i.status === "help-wanted") .map((item, i) => { const first = (item.links || []).map(number).find(Boolean); - const href = first ? issueUrl(first) : `#item-${item.id}`; + const href = first ? issueUrl(first) : `#${item.id}`; return `
  • ${String(i + 1).padStart(2, "0")}${escape(item.title)}${inline(item.plain)}${inline(item.dev)}${first ? `#${first}` : ""}
  • `; }) .join(""); diff --git a/website/src/hub.test.mjs b/website/src/hub.test.mjs index e73c9a8af..eb9b8a087 100644 --- a/website/src/hub.test.mjs +++ b/website/src/hub.test.mjs @@ -33,7 +33,7 @@ test("an item's progress counts its pull requests and issues", () => { test("the roadmap renders every item once, with both texts, in its status's section", () => { const html = roadmapHtml(roadmap); for (const item of roadmap.items) { - assert.equal(html.split(`id="item-${item.id}"`).length, 2, item.id); + assert.equal(html.split(`id="${item.id}"`).length, 2, item.id); } assert.ok(html.indexOf('id="now"') < html.indexOf('id="next"')); assert.ok(html.includes('class="hub-plain"') && html.includes('class="hub-dev"')); @@ -41,8 +41,15 @@ test("the roadmap renders every item once, with both texts, in its status's sect assert.ok(html.includes(`>${roadmap.items.find((i) => i.status === "shipped").shipped_in}<`)); }); +test("an item is its own anchor (hub#, as Kira links it) and takes no other id of the page", () => { + const page = readFileSync(new URL("./hub.html", import.meta.url), "utf8") + roadmapHtml(roadmap) + overviewHtml(roadmap); + const ids = [...page.matchAll(/ id="([^"]+)"/g)].map((m) => m[1]); + assert.deepEqual(ids.filter((id, i) => ids.indexOf(id) !== i), []); + for (const item of roadmap.items) assert.ok(ids.includes(item.id), item.id); +}); + test("the overview lists the focus items and never nests links", () => { const html = overviewHtml(roadmap); - for (const item of roadmap.items.filter((i) => i.focus)) assert.ok(html.includes(`href="#item-${item.id}"`), item.id); + for (const item of roadmap.items.filter((i) => i.focus)) assert.ok(html.includes(`href="#${item.id}"`), item.id); for (const row of html.match(/
  • [\s\S]*?<\/li>/g)) assert.equal(row.split(" Date: Sun, 25 Oct 2026 15:59:56 +0100 Subject: [PATCH 087/238] Endermen, endermites, blazes, eyes of ender, dried ghasts and end portals make their ambient effects The client-side branches vanilla runs on its own level: Enderman and Endermite aiStep (two portal particles a tick), Blaze aiStep (two large smoke particles, the burn sound one tick in 24), EyeOfEnder.tick (a portal particle behind it, four bubbles in water), and the DriedGhastBlock and EndPortalBlock animateTick. They run on the window's game in-frame and as a server's client, never on a server. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/animate_tick.rs | 50 +++++++++++++++++ crates/horizon-client/src/game.rs | 2 + .../horizon-client/src/game/client_ambient.rs | 37 +++++++++++++ crates/horizon-client/src/game/end.rs | 36 +++++++++++- crates/horizon-client/src/game/remote.rs | 1 + crates/horizon-client/src/mobs.rs | 1 + crates/horizon-client/src/mobs/ambient.rs | 55 +++++++++++++++++++ 7 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 crates/horizon-client/src/game/client_ambient.rs create mode 100644 crates/horizon-client/src/mobs/ambient.rs diff --git a/crates/horizon-client/src/animate_tick.rs b/crates/horizon-client/src/animate_tick.rs index ce34df510..48e8f16f4 100644 --- a/crates/horizon-client/src/animate_tick.rs +++ b/crates/horizon-client/src/animate_tick.rs @@ -63,6 +63,10 @@ enum Animate { SculkSensor, /// Potent sulfur that is not dry. PotentSulfur, + DriedGhast { + waterlogged: bool, + }, + EndPortal, } fn step(facing: &str) -> [i32; 3] { match facing { @@ -137,6 +141,10 @@ fn classify(name: &str, prop: &dyn Fn(&str) -> Option) -> Animate { drag: prop("drag").as_deref() == Some("true"), }, "fire" | "soul_fire" => Animate::Fire, + "dried_ghast" => Animate::DriedGhast { + waterlogged: prop("waterlogged").as_deref() == Some("true"), + }, + "end_portal" => Animate::EndPortal, "sculk_sensor" | "calibrated_sculk_sensor" if prop("sculk_sensor_phase").as_deref() == Some("active") => { @@ -180,6 +188,8 @@ pub struct AnimateTables { impermeable: Vec, power_provider: Vec, power_transmitter: Vec, + /// `#triggers_ambient_dried_ghast_block_sounds` (soul sand, soul soil). + dried_ghast_sounds: Vec, /// Biome id -> ambient particles and probabilities (`minecraft:visual/ambient_particles`). ambient: Vec>, } @@ -237,6 +247,7 @@ impl AnimateTables { impermeable: by_state("minecraft:impermeable"), power_provider: by_state("minecraft:enchantment_power_provider"), power_transmitter: by_state("minecraft:enchantment_power_transmitter"), + dried_ghast_sounds: by_state("minecraft:triggers_ambient_dried_ghast_block_sounds"), ambient, }) } @@ -869,6 +880,45 @@ fn block_animate_tick( ]; add(engine, T::Smoke, p, [0.0; 3]); } + Animate::DriedGhast { waterlogged } => { + // DriedGhastBlock.animateTick. + let c = [fx + 0.5, fy + 0.5, fz + 0.5]; + if waterlogged { + if next_int(engine.random(), 40) == 0 { + let name = "minecraft:block.dried_ghast.ambient_water"; + sound(engine, name, c, 1.0, 1.0); + } + let r = engine.random(); + if next_int(r, 6) == 0 { + let x = c[0] + f64::from((r.next_float() * 2.0 - 1.0) / 3.0); + let y = c[1] + 0.4; + let z = c[2] + f64::from((r.next_float() * 2.0 - 1.0) / 3.0); + let v = [0.0, f64::from(r.next_float()), 0.0]; + add(engine, T::HappyVillager, [x, y, z], v); + } + } else { + let below = ctx.state([pos[0], pos[1] - 1, pos[2]]); + let triggers = || { + tables + .dried_ghast_sounds + .get(below.0 as usize) + .copied() + .unwrap_or(false) + }; + if next_int(engine.random(), 40) == 0 && triggers() { + sound(engine, "minecraft:block.dried_ghast.ambient", c, 1.0, 1.0); + } + if next_int(engine.random(), 6) == 0 { + add(engine, T::WhiteSmoke, c, [0.0, 0.02, 0.0]); + } + } + } + Animate::EndPortal => { + // EndPortalBlock.animateTick: one smoke particle above the surface. + let r = engine.random(); + let p = [fx + r.next_double(), fy + 0.8, fz + r.next_double()]; + add(engine, T::Smoke, p, [0.0; 3]); + } Animate::EnderChest => { for _ in 0..3 { let r = engine.random(); diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 699468651..46621082e 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -20,6 +20,7 @@ mod bossbars; mod breezes; mod cartography; mod chat; +mod client_ambient; mod clipboard; mod combat; mod command_blocks; @@ -4447,6 +4448,7 @@ impl Game<'_> { self.tick_block_entity_anims() }); horizon_client::spans::time("tick_particles", || self.tick_particles()); + self.tick_client_ambient(); } // ServerBossEvent broadcasts, after this level's entities have ticked. self.publish_boss_bars(); diff --git a/crates/horizon-client/src/game/client_ambient.rs b/crates/horizon-client/src/game/client_ambient.rs new file mode 100644 index 000000000..a10c6da49 --- /dev/null +++ b/crates/horizon-client/src/game/client_ambient.rs @@ -0,0 +1,37 @@ +//! The window's own ambient effects of entities, which a client makes for +//! itself (`level().isClientSide()` branches): endermen's and endermites' +//! portal particles, blazes' smoke and burn, eyes of ender's trail. They run +//! on the game that has the window, in-frame or following a server, never on +//! a server: nothing of them goes over the network. +use super::Game; +use horizon_client::mobs::{MobEvent, ambient}; +use horizon_client::particles::{ParticleOptions, ParticleType}; + +impl Game<'_> { + pub(super) fn tick_client_ambient(&mut self) { + if self.serving.is_some() || self.options.dedicated.is_some() { + return; + } + let mut events = Vec::new(); + for m in self.mobs.iter_mut().filter(|m| !m.removed) { + ambient::client_ai_step(m, &mut events); + } + for event in events { + match event { + MobEvent::ParticleAt { kind, at, velocity } => { + if let Some(ty) = ParticleType::from_name(kind) { + self.particles + .add(&ParticleOptions::simple(ty), at, velocity); + } + } + // Each guest of a LAN host makes its own. + MobEvent::LocalSound(name, at, volume, pitch) => { + self.audio + .play_unshared(&format!("minecraft:{name}"), Some(at), volume, pitch) + } + _ => {} + } + } + self.eye_particles(); + } +} diff --git a/crates/horizon-client/src/game/end.rs b/crates/horizon-client/src/game/end.rs index 599158e3e..655971a5c 100644 --- a/crates/horizon-client/src/game/end.rs +++ b/crates/horizon-client/src/game/end.rs @@ -4,7 +4,12 @@ //! inactive until the dragon dies. use super::{Dim, Game}; use horizon_client::{ - Result, environment::Environment, items::ItemStack, mesher::Vertex, motion::PlayerMotion, + Result, + environment::Environment, + items::ItemStack, + mesher::Vertex, + motion::PlayerMotion, + particles::{ParticleOptions, ParticleType}, vanillagen::VanillaTerrain, }; use horizon_core::{ @@ -1122,6 +1127,35 @@ impl Game<'_> { self.end.eyes = eyes; } + /// `EyeOfEnder.tick` on a client: behind each eye (a quarter of its last + /// move back), four bubbles in water, else a portal particle. + pub(super) fn eye_particles(&mut self) { + for i in 0..self.end.eyes.len() { + let e = &self.end.eyes[i]; + let movement = (e.position - e.previous).to_array(); + let origin = (e.position - (e.position - e.previous) * 0.25).to_array(); + let block = origin.map(|v| v.floor() as i32); + let water = self + .res + .facts() + .get(self.world.block(block[0], block[1], block[2])) + .fluid + == horizon_client::blocks::Fluid::Water; + if water { + let bubble = ParticleOptions::simple(ParticleType::Bubble); + for _ in 0..4 { + self.particles.add(&bubble, origin, movement); + } + } else { + let r = self.particles.random(); + let x = origin[0] + r.next_double() * 0.6 - 0.3; + let z = origin[2] + r.next_double() * 0.6 - 0.3; + let portal = ParticleOptions::simple(ParticleType::Portal); + self.particles + .add(&portal, [x, origin[1] - 0.5, z], movement); + } + } + } /// ThrownItemRenderer: the ender eye item facing the camera. fn eye_geometry(&self, camera: glam::DVec3, out: &mut Vec) { let Some(r) = self.gui.atlas.sprites.get("item/ender_eye").copied() else { diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 044e43835..711753869 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -3187,6 +3187,7 @@ impl Game<'_> { self.tick_block_entity_anims(); self.tick_hand(); self.tick_particles(); + self.tick_client_ambient(); self.tick_client_effects(); // Player.tick's sleep counter (the darkening and the wake-up fade). self.sleep_timer = super::tick_sleep_timer(self.sleep_timer, self.sleeping.is_some()); diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 4e3879fe5..6dee6a829 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -7,6 +7,7 @@ //! behaviour, models, animations and sounds. pub mod ai; pub mod allay; +pub mod ambient; pub mod anim; pub mod animal_brain; pub mod aquatic; diff --git a/crates/horizon-client/src/mobs/ambient.rs b/crates/horizon-client/src/mobs/ambient.rs new file mode 100644 index 000000000..cb4146e08 --- /dev/null +++ b/crates/horizon-client/src/mobs/ambient.rs @@ -0,0 +1,55 @@ +//! What a client's copy of a mob makes on its own, in the +//! `level().isClientSide()` branches of `aiStep`: nothing of it reaches the +//! server or goes over the network, so the window's game runs it, whether it +//! simulates the world (in-frame) or follows a server. +use super::{Mob, MobEvent, Species}; +use horizon_core::random::RandomSource; + +/// `Enderman.aiStep`, `Endermite.aiStep` and `Blaze.aiStep` on a client: +/// two portal particles a tick around endermen and endermites; two large +/// smoke particles a tick around blazes and, one tick in 24, their burn. +pub fn client_ai_step(m: &mut Mob, out: &mut Vec) { + let species = m.species(); + if !matches!( + species, + Species::Enderman | Species::Endermite | Species::Blaze + ) { + return; + } + let (w, h) = (f64::from(m.width()), f64::from(m.height())); + let p = m.position; + if species == Species::Blaze && m.random.next_int_bound(24).unwrap_or(1) == 0 && !m.silent { + let volume = 1.0 + m.random.next_float(); + let pitch = m.random.next_float() * 0.7 + 0.3; + out.push(MobEvent::LocalSound( + "entity.blaze.burn", + [p.x + 0.5, p.y + 0.5, p.z + 0.5], + volume, + pitch, + )); + } + for _ in 0..2 { + // Entity.getRandomX(0.5), getRandomY(), getRandomZ(0.5). + let r = &mut m.random; + let x = p.x + w * (2.0 * r.next_double() - 1.0) * 0.5; + let y = p.y + h * r.next_double(); + let z = p.z + w * (2.0 * r.next_double() - 1.0) * 0.5; + let (kind, at, velocity) = if species == Species::Blaze { + ("large_smoke", [x, y, z], [0.0; 3]) + } else { + // The enderman's are a quarter block lower. + let y = if species == Species::Enderman { + y - 0.25 + } else { + y + }; + let velocity = [ + (r.next_double() - 0.5) * 2.0, + -r.next_double(), + (r.next_double() - 0.5) * 2.0, + ]; + ("portal", [x, y, z], velocity) + }; + out.push(MobEvent::ParticleAt { kind, at, velocity }); + } +} From c04b7afdb9b515b57cde9459f2e01386e4e327bd Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 15:35:27 +0100 Subject: [PATCH 088/238] Hub: the Kanban and the Journal tabs, and /hub/journal.json Kanban: every open issue and pull request and what closed in the last seven days, placed in To do, In progress, In review and Done by the rules of Kira's work board, each card with its roadmap item (its roadmap: label or the item linking it) and a filter by item. Journal: every release with its changes grouped by roadmap item, the merges no release has yet, and a weekly summary from roadmap/journal/-.md. The same journal is /hub/journal.json, for Kira's #updates and posts elsewhere: each entry names the roadmap items it moved and shipped, with their 240-character text. Co-Authored-By: Claude Opus 5.5 --- roadmap/README.md | 24 ++++- roadmap/journal/2026-43.md | 17 ++++ tests/test_roadmap.py | 17 ++++ tools/roadmap.py | 24 +++++ website/README.md | 4 +- website/build.mjs | 11 ++- website/data/roadmap.json | 9 ++ website/src/hub-page.js | 33 ++++++- website/src/hub.html | 19 ++++ website/src/hub.js | 198 +++++++++++++++++++++++++++++++++++++ website/src/hub.test.mjs | 47 ++++++++- website/src/style.css | 39 ++++++++ 12 files changed, 435 insertions(+), 7 deletions(-) create mode 100644 roadmap/journal/2026-43.md diff --git a/roadmap/README.md b/roadmap/README.md index 656303464..f16e83f8c 100644 --- a/roadmap/README.md +++ b/roadmap/README.md @@ -50,8 +50,30 @@ Whoever moves the work (the orchestrator on each heartbeat, or anyone in their p item, updates their colour (by status) and description, and adds the label to the open pull requests an item links. Label a new pull request for an item by hand or by linking it and running `labels` again. Labels of removed items are listed, never deleted. -5. Suggestions from the website and Discord are read and triaged by the maintainers. A good one becomes an +5. Once a week, write the week's summary in `roadmap/journal/-.md` (for example + `2026-43.md`): `# Title`, a paragraph or two for players, then `## Technical` and the same week for + contributors, with its pull requests. `build` puts it into website/data/roadmap.json; the hub's Journal tab + shows it among the releases, and `/hub/journal.json` carries it to Kira's #updates and to posts elsewhere. +6. Suggestions from the website and Discord are read and triaged by the maintainers. A good one becomes an item in a normal pull request; none is published automatically. CI runs `tests/test_roadmap.py` (in the Python tests), which fails when ROADMAP.md is not what `build` writes or when the file breaks a rule; `python3 tools/roadmap.py check --offline` runs the same rules without GitHub. + +## The journal + +The hub's Journal tab and `https://playhorizon.dev/hub/journal.json` are built with each deploy of the website +(`website/src/hub.js`, `journalFrom`): every game and launcher release with its notes' changes, each placed under +the roadmap item it moves (the pull request's `roadmap:` label, else the item that links it); the merged pull +requests no release has yet; and the weekly summaries above. Its JSON, newest first: + +```json +{ "format": "horizon-journal", "version": 1, "generated": "…", "url": "https://playhorizon.dev/hub#journal", + "entries": [ { "id": "v0.2.57", "kind": "release | launcher | merged | week", "date": "…", "title": "…", + "plain": "one line for players", "url": "…", "dev": "weeks only", + "changes": [ { "section": "new | fixed | merged", "text": "…", "prs": [1787], "item": "discord-presence" } ], + "items": [ { "id", "title", "plain", "post" } ], // roadmap items the entry moved + "shipped": [ { "id", "title", "plain", "post" } ] } ] } // items that shipped in this release +``` + +`post` is at most 240 characters, ready to announce as is. Readers should remember the ids they have posted. diff --git a/roadmap/journal/2026-43.md b/roadmap/journal/2026-43.md new file mode 100644 index 000000000..388f5d544 --- /dev/null +++ b/roadmap/journal/2026-43.md @@ -0,0 +1,17 @@ +# Single player becomes a server, servers send their packs + +Single player now runs on its own server thread, as vanilla does, and a run of fixes made shields, tridents, +elytra, maps, fishing, signs and books work there and for every server player. Servers can send you their +resource packs, the launcher got its blocky new look, and the first pieces of Horizon's mod registry landed. +Shader packs were taken out, to come back written from scratch. + +## Technical + +The server thread became the default (#1514), after guests and server-thread players reached parity: totems +(#1463), death drops (#1464), signs and books (#1465, #1466), shields, tridents, crossbows and spyglasses +(#1476), elytra (#1475), maps (#1510), fishing (#1509), wind charges (#1508), strikes and mob effects (#1512), +the cursor stack on close (#1513). Server resource packs: the dedicated server offers them (#1562) and the +client downloads, asks and applies them (#1569, #1570). Mod registry: index schema 1 and `.hpack` (#1634), +`cargo horizon-mod check` (#1642), the submission service (#1659). Shader packs were removed (#1561) and the +clean-room rewrite started from the format's documentation (#1584 onward). Releases now ship in batches of five +pull requests, or six hours after the oldest (#1554). diff --git a/tests/test_roadmap.py b/tests/test_roadmap.py index 605843f25..6c7b14e31 100644 --- a/tests/test_roadmap.py +++ b/tests/test_roadmap.py @@ -171,6 +171,23 @@ class GitHubState(unittest.TestCase): self.assertTrue(any("#10 is neither" in p for p in problems)) +class Journal(unittest.TestCase): + def test_weekly_summaries_newest_first_with_their_monday(self): + import tempfile + from pathlib import Path + with tempfile.TemporaryDirectory() as folder: + folder = Path(folder) + (folder / "2026-43.md").write_text("# A week\n\nPlain\nwords.\n\n## Technical\n\nDev (#1).\n") + (folder / "2026-42.md").write_text("# Older\n\nPlain.\n\n## Technical\n\nDev.\n") + (folder / "week-44.md").write_text("# Bad name\n") + (folder / "2026-45.md").write_text("# No technical part\n\nPlain.\n") + entries, problems = roadmap.weeks(folder) + self.assertEqual([e["week"] for e in entries], ["2026-43", "2026-42"]) + self.assertEqual(entries[0], {"week": "2026-43", "date": "2026-10-19", "title": "A week", + "plain": "Plain words.", "dev": "Dev (#1)."}) + self.assertEqual(len(problems), 2) + + class Labels(unittest.TestCase): def test_creates_updates_and_adds_labels(self): data = tomllib.loads(SAMPLE) diff --git a/tools/roadmap.py b/tools/roadmap.py index f09224147..ab0147e70 100644 --- a/tools/roadmap.py +++ b/tools/roadmap.py @@ -30,6 +30,9 @@ SOURCE = ROOT / "roadmap" / "roadmap.toml" OUTPUT = ROOT / "ROADMAP.md" # What the website's hub reads (website/build.mjs), and serves at /data/roadmap.json for Kira and others. SITE_DATA = ROOT / "website" / "data" / "roadmap.json" +# The journal's weekly summaries, one file per ISO week: roadmap/journal/2026-43.md (roadmap/README.md). +JOURNAL = ROOT / "roadmap" / "journal" +WEEK = re.compile(r"(\d{4})-(\d{2})\.md$") REPO = "ludovic111/horizon" SITE = "https://playhorizon.dev/hub" @@ -153,6 +156,7 @@ def validate(data, root=ROOT): continue if link.startswith(("/", "http:")) or ".." in link or not (root / link).exists(): problems.append(f"{where}: link {link!r} is neither #N, https:// nor a path in the repository") + problems += weeks()[1] if root == ROOT else [] focus = sum(1 for item in items if item.get("focus")) low, high = FOCUS_RANGE if not low <= focus <= high: @@ -234,6 +238,25 @@ def render(data): return "\n".join(out) + "\n" +def weeks(directory=JOURNAL): + """The weekly summaries, newest first: {"week", "date" (its Monday), "title", "plain", "dev"}, and problems.""" + entries, problems = [], [] + for path in sorted(directory.glob("*.md"), reverse=True) if directory.exists() else []: + name = WEEK.match(path.name) + text = path.read_text() + parts = re.match(r"# (.+)\n\n(.+?)\n\n## Technical\n\n(.+)", text.strip() + "\n", re.S) + if not name or not 1 <= int(name[2]) <= 53: + problems.append(f"journal/{path.name}: name it by its ISO week, like 2026-43.md") + continue + if not parts: + problems.append(f"journal/{path.name}: needs '# Title', the plain text, then '## Technical' and its text") + continue + monday = datetime.date.fromisocalendar(int(name[1]), int(name[2]), 1) + entries.append({"week": path.stem, "date": monday.isoformat(), "title": parts[1].strip(), + "plain": " ".join(parts[2].split()), "dev": " ".join(parts[3].split())}) + return entries, problems + + def site_data(data): """website/data/roadmap.json: the items as the hub and Kira read them, in the file's order.""" keys = ("id", "title", "status", "area", "plain", "dev", "links", "focus", "ongoing", "shipped_in", "post", @@ -247,6 +270,7 @@ def site_data(data): "areas": [{"id": key, "name": name} for key, name in areas_in_order(data)], "items": [{key: (" ".join(item[key].split()) if isinstance(item[key], str) else item[key]) for key in keys if key in item} for item in data.get("item", [])], + "journal": weeks()[0], } return json.dumps(out, indent=1, ensure_ascii=False) + "\n" diff --git a/website/README.md b/website/README.md index 8f35fbcf4..56fa515b9 100644 --- a/website/README.md +++ b/website/README.md @@ -25,7 +25,7 @@ src/404.html the not-found page src/privacy.html what the launcher can send when players allow it, retention, deletion (/privacy) src/releases.html every release, newest first, with what it adds and fixes, its notes and downloads (/releases) src/benchmarks.html Horizon against vanilla 26.3 on our machines, the hardware picker, players' results (/benchmarks) -src/hub.html the hub (/hub, /roadmap redirects to its Roadmap tab): Overview, Roadmap, Contribute, a Simple / Technical switch +src/hub.html the hub (/hub, /roadmap redirects to its Roadmap tab): Overview, Roadmap, Kanban, Journal, Contribute, a Simple / Technical switch src/docs.html the template of every documentation page (/docs/...), with its left navigation src/tokens.css the design tokens, generated from crates/horizon-theme (do not edit) src/style.css every style, on top of the tokens @@ -40,7 +40,7 @@ src/estimate.js estimates for unmeasured hardware from the nearest measured m src/markdown.js a small Markdown renderer (escapes everything), shared by build and page src/releases.js the home page's "Recently added" and the Releases page's markup, shared by build and page src/contributors.js the contributors' filtering and markup, shared by build and page -src/hub.js the hub's markup from data/roadmap.json and GitHub's issue states, shared by build and page +src/hub.js the hub's roadmap, board and journal from data/roadmap.json, GitHub's issues and the releases, shared by build and page; /hub/journal.json src/hub-page.js the hub page: tabs by #hash, the Simple / Technical switch (localStorage), the area filter, live states data/roadmap.json the roadmap, generated by tools/roadmap.py from roadmap/roadmap.toml (do not edit); served at /data/roadmap.json public/ fonts, screenshots, icons, og.png, robots.txt, benchmarks.json: copied as is (sitemap.xml is written by the build) diff --git a/website/build.mjs b/website/build.mjs index 563bc6fa4..17768e5fc 100644 --- a/website/build.mjs +++ b/website/build.mjs @@ -16,7 +16,7 @@ import { machinesHtml, playersHtml } from "./src/bench.js"; import { CPUS, GPUS, singleThread } from "./src/hardware.js"; import { isGame, isLauncher, launcherVersion, newestFirst } from "./src/versions.js"; import { contributorsFrom, contributorsHtml } from "./src/contributors.js"; -import { areaFilterHtml, helpWantedHtml, overviewHtml, roadmapHtml, slimIssue } from "./src/hub.js"; +import { areaFilterHtml, boardFilterHtml, boardFrom, boardHtml, helpWantedHtml, journalFrom, journalHtml, journalJson, overviewHtml, roadmapHtml, slimIssue } from "./src/hub.js"; const site = dirname(fileURLToPath(import.meta.url)); const dist = join(site, "dist"); @@ -116,6 +116,8 @@ try { console.warn(`hub: ${e.message}; the page will load the states itself`); } const hubLinked = new Map(hubLive.filter((i) => linked.has(i.n)).map((i) => [i.n, i])); +const board = boardFrom(hubLive, roadmap); +const journal = journalFrom(releases, hubLive, roadmap); // --- The parity table ------------------------------------------------------------------ @@ -327,6 +329,9 @@ const values = { HUB_ROADMAP: roadmapHtml(roadmap, hubLinked), HUB_AREAS: areaFilterHtml(roadmap), HUB_HELP_WANTED: helpWantedHtml(roadmap), + HUB_BOARD: hubLive.length ? boardHtml(board, roadmap) : `

    The board could not be loaded here.

    `, + HUB_BOARD_FILTER: boardFilterHtml(board, roadmap), + HUB_JOURNAL: journalHtml(journal, roadmap) || unavailable, HUB_DATA: JSON.stringify({ roadmap, live: [...hubLinked.values()] }).replace(/ b.addEventListener("click", () => filter(b.dataset.areaFilter))); +// --- The board: a roadmap item's cards, and the long columns' "Show more" ---------------------------------------- + +const boardSelect = document.querySelector("[data-board-item]"); +function showItem() { + const want = boardSelect.value; + for (const card of document.querySelectorAll("[data-board] .hub-card")) { + card.hidden = want === "-" ? !card.dataset.item : want ? card.dataset.item !== want : false; + } + for (const column of document.querySelectorAll("[data-board] .hub-column")) { + column.querySelector("[data-count]").textContent = column.querySelectorAll(".hub-card:not([hidden])").length; + // A filtered column shows all its cards. + column.classList.toggle("is-open", Boolean(want) || column.classList.contains("was-opened")); + } +} +boardSelect?.addEventListener("change", showItem); +document.querySelector("[data-board]")?.addEventListener("click", (e) => { + const button = e.target.closest("[data-more-button]"); + if (!button) return; + button.closest(".hub-column").classList.add("is-open", "was-opened"); + button.remove(); +}); + // --- Live state from GitHub ------------------------------------------------------------------------------------ // The open issues and pull requests, and those closed or merged lately: four requests at most, kept ten minutes. @@ -100,6 +122,7 @@ async function loadLive() { const closed = await get(`https://api.github.com/repos/${REPO}/issues?state=closed&sort=updated&direction=desc&per_page=100`); return list.concat(closed.map(slimIssue)); } +const loadReleases = () => get(`https://api.github.com/repos/${REPO}/releases?per_page=40`); async function refresh() { const fresh = await cached("horizon:hub-live", loadLive).catch(() => null); @@ -109,6 +132,14 @@ async function refresh() { document.querySelector("[data-overview]").innerHTML = overviewHtml(data.roadmap, live); document.querySelector("[data-roadmap]").innerHTML = roadmapHtml(data.roadmap, live); filter(area); + const board = boardFrom(fresh, data.roadmap); + const chosen = boardSelect.value; + boardSelect.innerHTML = boardFilterHtml(board, data.roadmap); + boardSelect.value = [...boardSelect.options].some((o) => o.value === chosen) ? chosen : ""; + document.querySelector("[data-board]").innerHTML = boardHtml(board, data.roadmap); + showItem(); + const releases = await cached("horizon:hub-releases", loadReleases).catch(() => null); + if (releases) document.querySelector("[data-journal]").innerHTML = journalHtml(journalFrom(releases, fresh, data.roadmap), data.roadmap); const note = document.querySelector("[data-live-note]"); if (note) note.textContent = "Pull request and issue states are live from GitHub."; } diff --git a/website/src/hub.html b/website/src/hub.html index 5aff05639..a6e146080 100644 --- a/website/src/hub.html +++ b/website/src/hub.html @@ -36,6 +36,8 @@ + +
    @@ -58,6 +60,23 @@

    The roadmap lives in the repository: ROADMAP.md, generated from roadmap/roadmap.toml. Pull requests carry the roadmap:<id> label of their item.

    +
    +
    +

    Every open issue and pull request, placed by its state on GitHub, as on Kira's work board in Discord.

    + +
    +
    +{{HUB_BOARD}} +
    +
    + +
    +

    Every release with what it changed, grouped by roadmap item in Technical, and a summary of each week. Also as journal.json.

    +
    +{{HUB_JOURNAL}} +
    +
    +

    Good first tasks

    diff --git a/website/src/hub.js b/website/src/hub.js index ecfb4da80..cda3b4e5b 100644 --- a/website/src/hub.js +++ b/website/src/hub.js @@ -4,6 +4,8 @@ // twice: `plain` (.hub-plain) for players and `dev` (.hub-dev) for contributors; the page's Simple / Technical // switch shows one of them. import { escape, inline } from "./markdown.js"; +import { parts, plainItem, sections } from "./releases.js"; +import { isGame, isLauncher } from "./versions.js"; export const REPO = "ludovic111/horizon"; const issueUrl = (n) => `https://github.com/${REPO}/issues/${n}`; @@ -173,3 +175,199 @@ export function helpWantedHtml(roadmap) { }) .join(""); } + +// --- The work board -------------------------------------------------------------------------------------------- + +export const COLUMNS = [ + { id: "todo", name: "To do", about: "Open and free: pick one, and say so with /claim on Discord." }, + { id: "progress", name: "In progress", about: "Claimed, assigned, or a draft pull request." }, + { id: "review", name: "In review", about: "A pull request is ready for review." }, + { id: "done", name: "Done", about: "Closed or merged in the last seven days." }, +]; +const DONE_DAYS = 7; + +// The roadmap item an issue or pull request moves: its roadmap: label, else the item linking it, else (for a +// pull request) the item linking an issue it fixes. +export function itemOf(issue, roadmap) { + const label = issue.labels.find((l) => l.startsWith("roadmap:")); + const ids = new Set(roadmap.items.map((i) => i.id)); + if (label && ids.has(label.slice(8))) return label.slice(8); + const linking = (n) => roadmap.items.find((i) => (i.links || []).includes(`#${n}`))?.id; + return linking(issue.n) || issue.fixes.map(linking).find(Boolean) || null; +} + +// The board from GitHub's open issues and pull requests and those closed lately, placed as Kira's work board places +// them: an issue goes to review when an open, ready pull request fixes it, to progress when a draft one does or it is +// claimed or assigned; a pull request that fixes no open issue is a card of its own. +export function boardFrom(list, roadmap, now = Date.now()) { + const columns = Object.fromEntries(COLUMNS.map((c) => [c.id, []])); + const open = list.filter((i) => i.state === "open"); + const openIssues = new Set(open.filter((i) => i.kind === "issue").map((i) => i.n)); + const fixing = (n) => open.filter((p) => p.kind === "pr" && p.fixes.includes(n)); + const card = (i, prs = []) => ({ n: i.n, kind: i.kind, title: i.title, user: i.user, url: i.url, item: itemOf(i, roadmap) || prs.map((p) => itemOf(p, roadmap)).find(Boolean) || null, prs: prs.map((p) => p.n) }); + for (const i of open) { + if (i.kind === "issue") { + const prs = fixing(i.n); + const claimed = i.assigned || i.labels.some((l) => /^(in progress|claimed)$/i.test(l)); + const column = prs.some((p) => !p.draft) ? "review" : prs.length || claimed ? "progress" : "todo"; + columns[column].push(card(i, prs)); + } else if (!i.fixes.some((n) => openIssues.has(n))) { + columns[i.draft ? "progress" : "review"].push(card(i)); + } + } + const since = now - DONE_DAYS * 864e5; + const seen = new Set(); + for (const i of list) { + if (seen.has(i.n) || !["merged", "closed"].includes(i.state) || !(Date.parse(i.at) >= since)) continue; + seen.add(i.n); + columns.done.push(card(i)); + } + columns.done.sort((a, b) => b.n - a.n); + return columns; +} + +const SHOWN = 20; // cards per column before "Show more" + +function cardHtml(c, roadmap, more) { + const item = c.item && roadmap.items.find((i) => i.id === c.item); + return `
  • ${c.kind === "pr" ? "PR" : "Issue"} #${c.n}${c.prs.length ? ` · ${c.prs.map((n) => `PR #${n}`).join(", ")}` : ""}${escape(c.title)}${item ? `${escape(item.title)}` : ""}${c.user ? `${escape(c.user)}` : ""}
  • `; +} + +export function boardHtml(columns, roadmap) { + return COLUMNS.map(({ id, name, about }) => { + const cards = columns[id]; + return `
    +

    ${escape(name)}${cards.length}

    +

    ${escape(about)}

    +
      ${cards.map((c, i) => cardHtml(c, roadmap, i >= SHOWN)).join("")}
    + ${cards.length > SHOWN ? `` : ""} +
    `; + }).join("\n"); +} + +// The board's roadmap filter: every item that has a card. +export function boardFilterHtml(columns, roadmap) { + const used = new Set(Object.values(columns).flat().map((c) => c.item).filter(Boolean)); + return [``, ``] + .concat(roadmap.items.filter((i) => used.has(i.id)).map((i) => ``)) + .join(""); +} + +// --- The journal ----------------------------------------------------------------------------------------------- + +export const JOURNAL_URL = "https://playhorizon.dev/hub#journal"; +const ENTRIES = 40; + +// The journal, newest first: each release with its changes grouped by roadmap item, the weekly summaries +// (roadmap/journal/), and what merged since the newest game release. releases: GitHub's release objects; list: the +// hub's issues and pull requests (their roadmap: labels place changes the roadmap doesn't link). +export function journalFrom(releases, list, roadmap) { + const prs = new Map(list.filter((i) => i.kind === "pr").map((i) => [i.n, i])); + const placed = (numbers) => { + for (const n of numbers) { + const known = prs.get(n); + const id = known ? itemOf(known, roadmap) : roadmap.items.find((i) => (i.links || []).includes(`#${n}`))?.id; + if (id) return id; + } + return null; + }; + const ours = releases.filter((r) => !r.draft && (isGame(r.tag_name) || isLauncher(r.tag_name))); + const entries = ours.map((r) => { + const changes = sections(r.body) + .filter((x) => /^(new|what's new|fixed)/i.test(x.title)) + .flatMap((x) => x.items.map((text) => ({ section: /^fixed/i.test(x.title) ? "fixed" : "new", text }))) + .map((c) => { + const numbers = [...c.text.matchAll(/#(\d+)/g)].map((m) => Number(m[1])); + return { ...c, text: plainItem(c.text), prs: numbers, item: placed(numbers) }; + }); + return { + id: r.tag_name, + kind: isLauncher(r.tag_name) ? "launcher" : "release", + date: r.published_at || "", + title: r.name || r.tag_name, + plain: parts(r).line, + url: r.html_url, + changes, + shipped: roadmap.items.filter((i) => i.status === "shipped" && i.shipped_in === r.tag_name).map((i) => i.id), + }; + }); + // Merged but in no release's notes yet: a release is cut from main a while after its last merge, so the newest + // releases' notes, not their dates, say what they hold. Pull requests players don't see (skip-release) never are. + const games = ours.filter((r) => isGame(r.tag_name)).map((r) => r.published_at).sort().reverse(); + const since = games[Math.min(4, games.length - 1)] || ""; + const noted = new Set(entries.flatMap((e) => e.changes.flatMap((c) => c.prs))); + const pending = list + .filter((i) => i.kind === "pr" && i.state === "merged" && i.at > since && !noted.has(i.n) && !i.labels.includes("skip-release")) + .sort((a, b) => b.at.localeCompare(a.at)); + if (pending.length) { + entries.push({ + id: "next-release", + kind: "merged", + date: pending[0].at, + title: "Merged, in the next release", + plain: `${pending.length} change${pending.length === 1 ? "" : "s"} merged since the last release.`, + url: `https://github.com/${REPO}/pulls?q=is%3Apr+is%3Amerged`, + changes: pending.map((p) => ({ section: "merged", text: p.title, prs: [p.n], item: itemOf(p, roadmap) })), + shipped: [], + }); + } + for (const w of roadmap.journal || []) { + // A week's summary sits after its last day's releases. + const end = new Date(Date.parse(w.date) + 7 * 864e5 - 1).toISOString(); + entries.push({ id: `week-${w.week}`, kind: "week", date: end, title: w.title, plain: w.plain, dev: w.dev, url: `${JOURNAL_URL.replace("#journal", "")}#week-${w.week}`, changes: [], shipped: [] }); + } + return entries.sort((a, b) => b.date.localeCompare(a.date)).slice(0, ENTRIES); +} + +// /hub/journal.json: the journal for Kira's #updates and posts elsewhere, with each shipped item's short text. +export function journalJson(entries, roadmap, generated = new Date().toISOString()) { + const item = (id) => { + const i = roadmap.items.find((x) => x.id === id); + return i && { id: i.id, title: i.title, plain: i.plain, post: i.post || i.plain }; + }; + return { + format: "horizon-journal", + version: 1, + generated, + url: JOURNAL_URL, + entries: entries.map((e) => ({ ...e, items: [...new Set(e.changes.map((c) => c.item).filter(Boolean))].map(item).filter(Boolean), shipped: e.shipped.map(item).filter(Boolean) })), + }; +} + +const KIND = { release: "Game", launcher: "Launcher", merged: "Merged", week: "This week" }; + +function changesHtml(entry, roadmap) { + if (!entry.changes.length) return ""; + const line = (c) => `
  • ${inline(c.text)}${c.prs.length ? ` ${c.prs.map((n) => `#${n}`).join(" ")}` : ""}
  • `; + // Simple: what is new, then how many fixes. Technical: every change, under the roadmap item it moves. + const added = entry.changes.filter((c) => c.section !== "fixed"); + const fixes = entry.changes.length - added.length; + const simple = `
    ${added.length ? `
      ${added.map((c) => `
    • ${inline(c.text)}
    • `).join("")}
    ` : ""}${fixes ? `

    ${added.length ? "And " : ""}${fixes} fix${fixes === 1 ? "" : "es"}.

    ` : ""}
    `; + const groups = new Map(); + for (const c of entry.changes) groups.set(c.item || "", [...(groups.get(c.item || "") || []), c]); + const order = [...groups.keys()].sort((a, b) => (a === "") - (b === "")); + const dev = `
    ${order + .map((id) => { + const item = roadmap.items.find((i) => i.id === id); + return `

    ${item ? `${escape(item.title)}` : "Other changes"}

      ${groups.get(id).map(line).join("")}
    `; + }) + .join("")}
    `; + return simple + dev; +} + +export function journalHtml(entries, roadmap) { + return entries + .map((e) => { + const shipped = e.shipped.map((id) => roadmap.items.find((i) => i.id === id)).filter(Boolean); + const items = [...new Set(e.changes.map((c) => c.item).filter(Boolean))].map((id) => roadmap.items.find((i) => i.id === id)).filter(Boolean); + return `
    + +

    ${e.url ? `${escape(e.title)}` : escape(e.title)}

    + ${e.kind === "week" ? `

    ${inline(e.plain)}

    ${inline(e.dev.replace(/(^|[^\w[/`])#(\d+)\b/g, (_, b, n) => `${b}[#${n}](${issueUrl(n)})`))}

    ` : ""} + ${shipped.length ? `

    Shipped: ${shipped.map((i) => `${escape(i.title)}`).join(", ")}

    ` : ""} + ${items.length ? `

    ${items.map((i) => `${escape(i.title)}`).join("")}

    ` : ""} + ${changesHtml(e, roadmap)} +
    `; + }) + .join("\n"); +} diff --git a/website/src/hub.test.mjs b/website/src/hub.test.mjs index eb9b8a087..a6337399b 100644 --- a/website/src/hub.test.mjs +++ b/website/src/hub.test.mjs @@ -1,7 +1,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; -import { fixesIn, overviewHtml, progress, roadmapHtml, slimIssue } from "./hub.js"; +import { boardFrom, fixesIn, itemOf, journalFrom, journalHtml, journalJson, overviewHtml, progress, roadmapHtml, slimIssue } from "./hub.js"; const roadmap = JSON.parse(readFileSync(new URL("../data/roadmap.json", import.meta.url), "utf8")); @@ -53,3 +53,48 @@ test("the overview lists the focus items and never nests links", () => { for (const item of roadmap.items.filter((i) => i.focus)) assert.ok(html.includes(`href="#${item.id}"`), item.id); for (const row of html.match(/
  • [\s\S]*?<\/li>/g)) assert.equal(row.split(" { + const now = Date.parse("2030-01-10T00:00:00Z"); + const list = [ + issue(1), + issue(2), + issue(3, { assignees: [{ login: "x" }] }), + pr(10, { body: "Fixes #1" }), + pr(11, { body: "Fixes #2", draft: true }), + pr(12, { labels: [{ name: `roadmap:${roadmap.items[0].id}` }] }), + pr(13, { draft: true }), + pr(14, { state: "closed", pull_request: { merged_at: "2030-01-09T00:00:00Z" } }), + pr(15, { state: "closed", pull_request: { merged_at: "2029-12-01T00:00:00Z" } }), + ].map(slimIssue); + const board = boardFrom(list, roadmap, now); + const ids = (column) => board[column].map((c) => c.n); + assert.deepEqual(ids("todo"), []); + assert.deepEqual(ids("progress"), [2, 3, 13]); + assert.deepEqual(ids("review"), [1, 12]); + assert.deepEqual(ids("done"), [14]); + assert.deepEqual(board.review[0].prs, [10]); + assert.equal(board.review[1].item, roadmap.items[0].id); + assert.equal(itemOf(slimIssue(pr(16, { body: "Fixes #1438" })), roadmap), "end-city-banners"); +}); + +test("the journal groups a release's changes by roadmap item and lists what awaits a release", () => { + const releases = [ + { tag_name: "v0.2.2", name: "Horizon 0.2.2: Rich presence", published_at: "2030-01-02T00:00:00Z", html_url: "u2", body: "### New\n- Discord Rich Presence (#1787)\n\n### Fixed\n- A fix (#5, fixes #4)\n" }, + { tag_name: "launcher@0.2.1", name: "Horizon launcher 0.2.1", published_at: "2030-01-01T00:00:00Z", html_url: "u1", body: "### Fixed\n- Window (#6)\n" }, + ]; + const list = [ + pr(1788, { state: "closed", pull_request: { merged_at: "2030-01-03T00:00:00Z" } }), + pr(7, { state: "closed", pull_request: { merged_at: "2030-01-03T00:00:00Z" }, labels: [{ name: "skip-release" }] }), + ].map(slimIssue); + const entries = journalFrom(releases, list, { ...roadmap, journal: [] }); + assert.deepEqual(entries.map((e) => e.id), ["next-release", "v0.2.2", "launcher@0.2.1"]); + assert.deepEqual(entries[0].changes.map((c) => [c.prs, c.item]), [[[1788], "discord-presence"]]); + assert.deepEqual(entries[1].changes.map((c) => [c.section, c.text, c.item]), [["new", "Discord Rich Presence", "discord-presence"], ["fixed", "A fix", null]]); + const json = journalJson(entries, roadmap, "now"); + assert.equal(json.format, "horizon-journal"); + assert.equal(json.entries[1].items[0].post.length <= 240, true); + const html = journalHtml(entries, roadmap); + assert.ok(html.includes('href="#item-discord-presence"')); + assert.ok(html.includes("And 1 fix.")); +}); diff --git a/website/src/style.css b/website/src/style.css index 4ec688812..f32e24a30 100644 --- a/website/src/style.css +++ b/website/src/style.css @@ -783,7 +783,46 @@ code { border-radius: 0; background: var(--well); box-shadow: 0 0 0 1px var(--ou .hub-rules li + li { margin-top: 10px; } .hub-rules li::marker { color: var(--muted); font-family: var(--mono); } .hub-source { margin-top: 40px; max-width: var(--measure); } +.hub-board-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px 24px; } +.hub-board-head .hub-intro { margin-bottom: 0; } +.hub-select { display: inline-flex; align-items: center; gap: 10px; } +.hub-select select { + height: 32px; max-width: 22rem; padding: 0 10px; font: var(--text-label) / 1 var(--font); color: var(--ink); + background: var(--well); border: 0; box-shadow: 0 0 0 var(--block-outline) var(--outline); +} +.hub-board { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; margin-top: 24px; align-items: start; } +.hub-column { padding: 14px; background: var(--panel); border: var(--block-outline) solid var(--outline); box-shadow: var(--panel-edges); } +.hub-column h3 { display: flex; justify-content: space-between; align-items: baseline; font-size: var(--text-heading); } +.hub-count { font: var(--text-micro) / 1 var(--mono); color: var(--muted); } +.hub-column > .meta { margin: 6px 0 12px; } +.hub-cards-list { display: grid; gap: 6px; } +.js .hub-column:not(.is-open) .hub-card[data-more] { display: none; } +.hub-card a { display: grid; gap: 4px; padding: 10px 12px; background: var(--well); box-shadow: 0 0 0 var(--block-outline) var(--outline); color: var(--ink); } +.hub-card a:hover { box-shadow: 0 0 0 var(--block-outline) var(--accent); } +.hub-card-ref, .hub-card-meta { font: var(--text-micro) / 1.4 var(--mono); letter-spacing: var(--text-micro-tracking); color: var(--muted); } +.hub-card-title { font-size: var(--text-label); line-height: 1.4; overflow-wrap: anywhere; } +.hub-card-meta { display: flex; flex-wrap: wrap; gap: 4px 10px; } +.hub-card-item { color: var(--accent); } +.hub-more { margin-top: 10px; width: 100%; justify-content: center; } +.hub-journal { display: grid; gap: 8px; margin-top: 24px; max-width: 56rem; } +.hub-entry { display: grid; gap: 10px; padding: 18px 20px; background: var(--panel); border: var(--block-outline) solid var(--outline); box-shadow: var(--panel-edges); scroll-margin-top: calc(var(--nav) + 24px); } +.hub-entry-week { background: color-mix(in srgb, var(--accent) 8%, var(--panel)); } +.hub-entry-meta { display: flex; align-items: center; gap: 10px; font: var(--text-micro) / 1 var(--mono); color: var(--muted); } +.hub-entry-meta .badge { margin-right: 0; } +.hub-entry-title { font-size: var(--text-heading); line-height: var(--text-heading-line); } +.hub-entry-title a { color: var(--ink); } +.hub-entry-title a:hover { color: var(--accent); } +.hub-entry .hub-plain, .hub-entry .hub-dev { font-size: var(--text-ui); color: var(--ink-soft); } +.hub-entry-items { display: flex; flex-wrap: wrap; gap: 6px; } +.hub-shipped { font-size: var(--text-ui); color: var(--ink); } +.hub-changes { list-style: square; padding-left: 18px; font-size: var(--text-ui); color: var(--ink-soft); } +.hub-changes li + li { margin-top: 4px; } +.hub-ref { font: var(--text-micro) / 1 var(--mono); } +.hub-group { margin-top: 8px; font: 500 var(--text-label) / 1.4 var(--font); } +.hub-group:first-child { margin-top: 0; } +@media (max-width: 1100px) { .hub-board { grid-template-columns: repeat(2, minmax(0, 1fr)); } } @media (max-width: 720px) { + .hub-board { grid-template-columns: minmax(0, 1fr); } .hub-rows .row > a { grid-template-columns: var(--index) minmax(0, 1fr); } .hub-rows .row-meta { grid-column: 2; text-align: left; } } From 0481dbdb81e49134f42dccafb58474504466a440 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Sun, 25 Oct 2026 15:10:56 +0100 Subject: [PATCH 089/238] Make hardcore players spectators when they respawn A hardcore death offered another survival life. Vanilla's death screen says Spectate in hardcore, and the server makes the respawned player a spectator (ServerGamePacketListenerImpl.handleClientCommand). Do the same on the server and in the in-frame world, and add a harness that compares a hardcore respawn on a dedicated server with vanilla's. --- crates/horizon-client/src/game/agent.rs | 8 +- crates/horizon-client/src/game/lan_actions.rs | 5 + crates/horizon-client/src/game/screens.rs | 27 +++- .../horizonlab/parity/LocatorBarClient.java | 6 +- tools/test_hardcore_respawn_parity.py | 139 ++++++++++++++++++ 5 files changed, 177 insertions(+), 8 deletions(-) create mode 100644 tools/test_hardcore_respawn_parity.py diff --git a/crates/horizon-client/src/game/agent.rs b/crates/horizon-client/src/game/agent.rs index f6b68aa74..b85710146 100644 --- a/crates/horizon-client/src/game/agent.rs +++ b/crates/horizon-client/src/game/agent.rs @@ -1598,8 +1598,12 @@ impl Game<'_> { if self.screen != Screen::Dead { return Err("respawn needs the death screen".into()); } - let label = - horizon_client::text::Text::translate("deathScreen.respawn", &[]).plain(); + let key = if self.level.hardcore { + "deathScreen.spectate" + } else { + "deathScreen.respawn" + }; + let label = horizon_client::text::Text::translate(key, &[]).plain(); let w = widgets::find(&widgets::last(), &label)?; if !w.enabled { // DeathScreen enables its buttons after 20 ticks. diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 2c7019c73..2942bea7d 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -1649,6 +1649,11 @@ impl Game<'_> { border: &self.border, }; host.hub.respawn(i, spawn, &ctx); + // ServerGamePacketListenerImpl.handleClientCommand: a hardcore world's + // respawned player only watches. + if self.level.hardcore { + host.hub.set_game_mode(conn, SPECTATOR); + } if let Some(config) = elsewhere { self.respawn_trip(conn, config); } diff --git a/crates/horizon-client/src/game/screens.rs b/crates/horizon-client/src/game/screens.rs index cf1f3269f..5fa040665 100644 --- a/crates/horizon-client/src/game/screens.rs +++ b/crates/horizon-client/src/game/screens.rs @@ -2,6 +2,7 @@ use super::{Game, Screen}; use horizon_client::{ Result, + command::args::GameMode, hud::{HudBatch, rgba}, text::{self, Text}, ui, @@ -81,9 +82,15 @@ fn pause_icons(w: f32, h: f32) -> [(f32, f32, &'static str); 4] { (x0 + 72.0, y, "gui/sprites/pause_menu/player_reporting"), ] } -fn death_buttons(w: f32, h: f32) -> [(f32, f32, &'static str, u8); 2] { +/// DeathScreen.init: a hardcore player can only spectate. +fn death_buttons(w: f32, h: f32, hardcore: bool) -> [(f32, f32, &'static str, u8); 2] { + let respawn = if hardcore { + "deathScreen.spectate" + } else { + "deathScreen.respawn" + }; [ - (w / 2.0 - 100.0, h / 4.0 + 72.0, "deathScreen.respawn", 0), + (w / 2.0 - 100.0, h / 4.0 + 72.0, respawn, 0), ( w / 2.0 - 100.0, h / 4.0 + 96.0, @@ -223,7 +230,12 @@ impl Game<'_> { ), ); } - let title = Text::translate("deathScreen.title", &[]); + let title = if self.level.hardcore { + "deathScreen.title.hardcore" + } else { + "deathScreen.title" + }; + let title = Text::translate(title, &[]); let tw = atlas.font.width_text(&title); ui::text_scaled( batch, @@ -258,7 +270,7 @@ impl Game<'_> { .ui .died_at .is_none_or(|t| t.elapsed().as_millis() >= 1000); - for (x, y, key, _) in death_buttons(w, h) { + for (x, y, key, _) in death_buttons(w, h, self.level.hardcore) { let hover = active && ui::hovered(self.cursor, x, y, 200.0, 20.0); ui::button( batch, @@ -286,7 +298,7 @@ impl Game<'_> { .as_ref() .map_or([1, 1], |r| [r.config.width, r.config.height]); let b = HudBatch::new(size); - let hit = death_buttons(b.width, b.height) + let hit = death_buttons(b.width, b.height, self.level.hardcore) .into_iter() .find(|d| ui::hovered(self.cursor, d.0, d.1, 200.0, 20.0)) .map(|d| d.3); @@ -330,6 +342,11 @@ impl Game<'_> { Some(1) => self.close_world()?, _ => {} } + // ServerGamePacketListenerImpl.handleClientCommand: a hardcore world's + // respawned player only watches. + if hit == Some(0) && self.remote.is_none() && self.level.hardcore { + self.set_mode(GameMode::Spectator); + } if hit.is_some() { self.audio.play_button_click(); } diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java b/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java index 76a951894..3bed808f6 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java @@ -84,7 +84,9 @@ import net.minecraft.world.phys.Vec3; * still), {@code sync NAME} (runs {@code time query gametime}, whose reply is * not recorded, and prints {@code synced NAME} once it is back: the server has * handled everything this player sent before, and everything the server sent - * this player before the reply has been recorded), {@code quit NAME}, {@code + * this player before the reply has been recorded), {@code respawn NAME} (the + * death screen's button, ServerboundClientCommandPacket PERFORM_RESPAWN), + * {@code quit NAME}, {@code * players on|off} (whether each player also records the other players added * to and removed from its level, and its own respawns into a dimension), * {@code chat on|off} (whether the system chat each player receives is @@ -130,6 +132,8 @@ public final class LocatorBarClient { players.get(w[1]).send(new net.minecraft.network.protocol.game.ServerboundPlayerAbilitiesPacket(abilities)); } case "still" -> players.get(w[1]).still(); + case "respawn" -> players.get(w[1]).send(new net.minecraft.network.protocol.game.ServerboundClientCommandPacket( + net.minecraft.network.protocol.game.ServerboundClientCommandPacket.Action.PERFORM_RESPAWN)); case "sync" -> { try { players.get(w[1]).sync(); diff --git a/tools/test_hardcore_respawn_parity.py b/tools/test_hardcore_respawn_parity.py new file mode 100644 index 000000000..6e0669534 --- /dev/null +++ b/tools/test_hardcore_respawn_parity.py @@ -0,0 +1,139 @@ +#!/usr/bin/env python3 +"""Respawning in a hardcore world on a dedicated server, against vanilla's, seen by a vanilla client. + +A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one hardcore +flat world vanilla generated first, with Alice an operator in survival. LocatorBarClient (a player made of +vanilla's protocol code) joins, runs /kill on itself and presses the death screen's button +(ServerboundClientCommandPacket PERFORM_RESPAWN). Vanilla's ServerGamePacketListenerImpl.handleClientCommand +respawns the player, then makes it a spectator because the server is hardcore. The game mode changes and respawns +Alice is sent in the death and respawn steps are compared exactly. +""" +import json +import os +from pathlib import Path +import platform +import shutil +import subprocess +import sys +import time + +try: + from . import lab, test_rust_parity as base, test_locator_bar_parity as locator + from .test_dedicated_server_parity import Server +except ImportError: + import lab + import test_rust_parity as base + import test_locator_bar_parity as locator + from test_dedicated_server_parity import Server + +NAME = 'hardcore-respawn-parity' +# server_dir and generate_world write these server.properties. +locator.PROPERTIES = dict(locator.PROPERTIES, hardcore='true', gamemode='survival') +SCRIPT = [ + ('death', ['cmd Alice kill @s']), + ('respawn', ['respawn Alice']), +] +STEP_SECONDS = 4.0 +SHOWN = ('gamemode', 'respawn') + + +def run_side(label, server_command, client_command, build, world): + directory = build / label + port = locator.server_dir(directory, world) + out = build / f'{label}.txt' + out.unlink(missing_ok=True) + server = Server(server_command, directory, label) + try: + server.wait_for(r'Done \(') + with (build / f'{label}-client.log').open('w') as log: + client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), str(out)], + cwd=lab.ROOT, stdin=subprocess.PIPE, stdout=log, stderr=subprocess.STDOUT, + text=True) + + def tell(line): + client.stdin.write(line + '\n') + client.stdin.flush() + try: + tell('players on') + tell('state on') + tell('step joins') + tell('join Alice') + server.wait_for(r'Alice joined the game', timeout=120) + time.sleep(STEP_SECONDS) + for step, actions in SCRIPT: + tell(f'step {step}') + for action in actions: + tell(action) + time.sleep(STEP_SECONDS) + tell('end') + client.wait(timeout=60) + finally: + if client.poll() is None: + client.kill() + finally: + server.stop() + if not out.exists(): + raise AssertionError(f'{label}: the client wrote no transcript (see {build}/{label}-client.log)') + return out.read_text(encoding='utf-8').splitlines() + + +def shown(lines): + """Alice's game mode changes and respawns in the death and respawn steps.""" + keep = [] + for line in lines: + parts = line.split(' ', 3) + if len(parts) >= 3 and parts[1] in ('step=death', 'step=respawn') and parts[2] in SHOWN: + keep.append(line) + return keep + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java' + classes = build / 'classes' + shutil.rmtree(classes, ignore_errors=True) + classes.mkdir() + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-nowarn', '-sourcepath', + lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle], + cwd=lab.ROOT, check=True, capture_output=True) + subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, + check=True, capture_output=True) + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + client = ['java', '-Xmx1g', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.LocatorBarClient'] + + world = locator.generate_world(vanilla_server, build) + vanilla_all = run_side('vanilla', vanilla_server, client, build, world) + horizon_all = run_side('horizon', horizon_server, client, build, world) + errors = [l for l in vanilla_all + horizon_all if ' error ' in f' {l} ' or 'disconnected' in l] + vanilla, horizon = shown(vanilla_all), shown(horizon_all) + if errors or vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=1, lineterm='')) + raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' + + '\n'.join(errors[:10]) + '\n' + diff[:6000]) + if 'Alice step=respawn gamemode 3' not in vanilla: + raise AssertionError(f'vanilla did not make the respawned player a spectator: the script did not run ' + f'(see {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='A player dying and respawning in a hardcore world on a dedicated server: the respawn and the game ' + 'mode it is sent (spectator).')) + print(f'hardcore respawn: {len(vanilla)} lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') From f48cfb79661b9181ed62435e5009f1cd2bfc0543 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Sun, 25 Oct 2026 15:48:47 +0100 Subject: [PATCH 090/238] Let /ride mount and dismount mob riders /ride now looks up a mob's vehicle as well as the player's, so mobs can mount mobs, get the already-riding and loop errors, and dismount to the vehicle's dismount spot. execute on vehicle uses the same lookup. The command_exec corpus gets a mob riding group. --- .../src/game/commands/execute.rs | 5 +- .../src/game/commands/world_cmds.rs | 88 +++++++++++++++---- tools/command_exec_corpus.py | 26 ++++++ 3 files changed, 100 insertions(+), 19 deletions(-) diff --git a/crates/horizon-client/src/game/commands/execute.rs b/crates/horizon-client/src/game/commands/execute.rs index 463cd542e..3f7e4c2a0 100644 --- a/crates/horizon-client/src/game/commands/execute.rs +++ b/crates/horizon-client/src/game/commands/execute.rs @@ -606,10 +606,7 @@ impl Game<'_> { .collect(), _ => Vec::new(), }, - Some("vehicle") => match entity { - Target::Player => self.riding.map(Target::Mob).into_iter().collect(), - _ => Vec::new(), - }, + Some("vehicle") => self.ride_vehicle(entity).into_iter().collect(), Some("controller") | Some("passengers") => match entity { Target::Mob(id) if self.mobs.iter().any(|m| m.id == id && m.rider.is_some()) => { vec![Target::Player] diff --git a/crates/horizon-client/src/game/commands/world_cmds.rs b/crates/horizon-client/src/game/commands/world_cmds.rs index 2f3792a57..c8bd220be 100644 --- a/crates/horizon-client/src/game/commands/world_cmds.rs +++ b/crates/horizon-client/src/game/commands/world_cmds.rs @@ -1074,15 +1074,15 @@ impl Game<'_> { }; let name = self.target_name(target); if id == "ride.dismount" { - let vehicle = match target { - Target::Player => self.riding.map(Target::Mob), - _ => None, - }; - let Some(v) = vehicle else { + let Some(v) = self.ride_vehicle(target) else { return Err(err("commands.ride.not_riding", &[&name.plain()])); }; let vehicle_name = self.target_name(v); - self.dismount_mount(); + match target { + Target::Player => self.dismount_mount(), + Target::Mob(rider) => self.dismount_mob(rider), + _ => {} + } self.feedback(Text::translate( "commands.ride.dismount.success", &[name, vehicle_name], @@ -1094,11 +1094,8 @@ impl Game<'_> { return Ok(0); }; let vehicle_name = self.target_name(vehicle); - if target == Target::Player && self.riding.is_some() { - let current = self - .riding - .map(Target::Mob) - .map_or_else(Text::empty, |v| self.target_name(v)); + if let Some(current) = self.ride_vehicle(target) { + let current = self.target_name(current); return Err(err( "commands.ride.already_riding", &[&name.plain(), ¤t.plain()], @@ -1107,22 +1104,42 @@ impl Game<'_> { if vehicle == Target::Player { return Err(err("commands.ride.mount.failure.cant_ride_players", &[])); } - if target == vehicle { - return Err(err("commands.ride.mount.failure.loop", &[])); + // getSelfAndPassengers: the vehicle is the target or rides it. + let mut up = Some(vehicle); + while let Some(v) = up { + if v == target { + return Err(err("commands.ride.mount.failure.loop", &[])); + } + up = self.ride_vehicle(v); } - // The player can ride mobs that carry a rider. + // Players and mobs can ride mobs. let Target::Mob(mob_id) = vehicle else { return Err(err( "commands.ride.mount.failure.generic", &[&name.plain(), &vehicle_name.plain()], )); }; - if target != Target::Player || !self.mobs.iter().any(|m| m.id == mob_id && m.alive()) { + let rider = match target { + Target::Mob(id) => Some(id), + _ => None, + }; + if !(target == Target::Player || rider.is_some()) + || !self.mobs.iter().any(|m| m.id == mob_id && m.alive()) + { return Err(err( "commands.ride.mount.failure.generic", &[&name.plain(), &vehicle_name.plain()], )); } + if let Some(rider) = rider { + // Entity.startRiding. + self.mount_mob(rider, mob_id); + self.feedback(Text::translate( + "commands.ride.mount.success", + &[name, vehicle_name], + )); + return Ok(1); + } if let Some(m) = self.mobs.iter_mut().find(|m| m.id == mob_id) { m.extra.flag = false; m.extra.counter = 0; @@ -1138,6 +1155,47 @@ impl Game<'_> { Ok(1) } + /// Entity.getVehicle: the player's mount, or a mob's. + pub(super) fn ride_vehicle(&self, t: Target) -> Option { + let id = match t { + Target::Player => return self.riding.map(Target::Mob), + Target::Mob(id) => self.mobs.iter().find(|m| m.id == id)?.extra.mount?, + _ => return None, + }; + Some(if self.boats.iter().any(|b| b.id == id) { + Target::Boat(id) + } else { + Target::Mob(id) + }) + } + /// LivingEntity.stopRiding for a mob: off its vehicle + /// (Entity.removePassenger), then to where the vehicle lets it out + /// (LivingEntity.dismountVehicle). + fn dismount_mob(&mut self, rider: u32) { + let Some(i) = self.mobs.iter().position(|m| m.id == rider) else { + return; + }; + let Some(v) = self.mobs[i].extra.mount.take() else { + return; + }; + let (width, yaw) = (f64::from(self.mobs[i].width()), self.mobs[i].yaw); + let at = if let Some(b) = self.boats.iter().position(|b| b.id == v) { + self.boat_dismount_target(b, width, yaw) + } else if let Some(m) = self.mobs.iter_mut().find(|m| m.id == v) { + m.extra.rider = None; + // Entity.getDismountLocationForPassenger: on top of the vehicle. + let p = m.position; + horizon_core::geometry::Vec3::new(p.x, p.y + f64::from(m.height()), p.z) + } else { + self.mobs[i].position + }; + let m = &mut self.mobs[i]; + m.extra.passenger = false; + m.extra.boarding_cooldown = 60; + m.position = at; + m.previous = at; + } + // ---- /spreadplayers ----------------------------------------------------------------------- pub(super) fn cmd_spreadplayers(&mut self, ctx: &Context) -> R { diff --git a/tools/command_exec_corpus.py b/tools/command_exec_corpus.py index 0e376e260..ba03fc817 100644 --- a/tools/command_exec_corpus.py +++ b/tools/command_exec_corpus.py @@ -327,6 +327,31 @@ def riding_teleport(): '/tp @s 8.5 230 8.5 0 0') +def mob_riding(): + """Mobs mount and dismount mobs with /ride, NBT passengers included (#1520).""" + rider, vehicle = '@e[tag=ride_r,limit=1]', '@e[tag=ride_v,limit=1]' + return [ + *setup('/summon minecraft:cow 4.5 220 12.5 {NoAI:1b,NoGravity:1b,Tags:["ride_v"]}', + '/summon minecraft:pig 6.5 220 12.5 {NoAI:1b,NoGravity:1b,Tags:["ride_r"]}'), + cmd(f'/ride {rider} dismount'), + cmd(f'/ride {rider} mount @s'), + cmd(f'/ride {rider} mount {rider}'), + cmd(f'/ride {rider} mount {vehicle}'), + cmd(f'/ride {rider} mount {vehicle}'), + cmd(f'/ride {vehicle} mount {rider}'), + cmd('/execute as @e[tag=ride_r] on vehicle run say carrying'), + cmd(f'/ride {rider} dismount'), + cmd(f'/data get entity {rider} Pos'), + cmd('/execute as @e[tag=ride_r] on vehicle run say still carrying'), + *setup('/kill @e[tag=ride_r]', '/kill @e[tag=ride_v]'), + *setup('/summon minecraft:cow 4.5 220 12.5 {NoAI:1b,NoGravity:1b,Tags:["ride_v"],' + 'Passengers:[{id:"minecraft:pig",NoAI:1b,NoGravity:1b,Tags:["ride_r"]}]}'), + cmd(f'/ride {rider} dismount'), + cmd(f'/data get entity {rider} Pos'), + *setup('/kill @e[tag=ride_r]', '/kill @e[tag=ride_v]'), + ] + + def scores(): return [ cmd('/scoreboard objectives add harness dummy'), @@ -1017,6 +1042,7 @@ def groups(): ('blocks and border', world_blocks()), ('entities', entities()), ('riding teleport', riding_teleport()), + ('mob riding', mob_riding()), ('scores, teams and bossbars', scores()), ('execute', execute()), ('levels', levels()), From 436c2fefb47d16e33b0e13ad2218dc731ec43afe Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Sun, 25 Oct 2026 15:40:05 +0100 Subject: [PATCH 091/238] Freeze server-thread players in powder snow With the server thread, the host's copy of a player never set isInPowderSnow from the blocks along its moves and never ran LivingEntity.aiStep's freezing, so a frozen player took no freezing damage and the client's own damage was overwritten. Record powder snow's FREEZE on the server path and run the freezing countdown and its damage every 40 ticks there. The guest's freeze-immune armor now comes from its worn armor, as for the local player. --- crates/horizon-client/src/game.rs | 24 ++++++++++-------- crates/horizon-client/src/game/lan_actions.rs | 2 ++ crates/horizon-client/src/motion.rs | 25 ++++++++++++++++--- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 699468651..4e4865c7a 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1844,6 +1844,19 @@ impl Game<'_> { /// (decreaseAirSupply), the wings, leather boots on powder snow /// (PowderSnowBlock.canEntityWalkOnPowderSnow), freeze-immune wear /// (LivingEntity.canFreeze) and the dimension's FAST_LAVA. + /// `LivingEntity.canFreeze`'s armor half: any worn piece in + /// `freeze_immune_wearables`. + pub(crate) fn wears_freeze_immune(&self) -> bool { + let immune = self + .res + .items() + .item_tags + .get("minecraft:freeze_immune_wearables"); + self.armor + .iter() + .flatten() + .any(|s| immune.is_some_and(|t| t.contains(&*s.item))) + } pub(crate) fn sync_player_body(&mut self) { let glider = self.glider_worn(); let item_use_speed = self @@ -1863,16 +1876,7 @@ impl Game<'_> { let leather_boots = self.armor[3] .as_ref() .is_some_and(|s| &*s.item == "minecraft:leather_boots"); - let immune = self - .res - .items() - .item_tags - .get("minecraft:freeze_immune_wearables"); - let freeze_immune = self - .armor - .iter() - .flatten() - .any(|s| immune.is_some_and(|t| t.contains(&*s.item))); + let freeze_immune = self.wears_freeze_immune(); let Some(player) = self.player.as_mut() else { return; }; diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 2c7019c73..10307dca3 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -1942,6 +1942,7 @@ impl Game<'_> { let events = self.as_guest(host, conn, |game| { let (rules, motion) = (game.world.health_rules, game.motion); let health = &motion.health; + let freeze_immune = game.wears_freeze_immune(); let Some(player) = game.player.as_mut() else { return (Vec::new(), None); }; @@ -2005,6 +2006,7 @@ impl Game<'_> { // Entity.applyEffectsFromBlocks along those moves: magma, cactus, // lit campfires and grown sweet berry bushes hurt (#1305). w.player.shift_key_down = sneaking; + w.player.freeze_immune = freeze_immune; let _ = w.server_block_damage(start); let _ = w.tick_environment_damage(); let _ = w.apply_fire_contact(); diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index 560cadc80..e7809055d 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -1083,11 +1083,26 @@ impl MotionWorld<'_, '_, '_> { } /// What `applyEffectsFromBlocks` does to a server's player along the /// moves its client sent since `from` (in its history): the damage of - /// magma, cactus, lit campfires and grown sweet berry bushes. Its client - /// moves it, so the slowing and the rest are the client's. + /// magma, cactus, lit campfires and grown sweet berry bushes, and powder + /// snow's FREEZE, then `LivingEntity.aiStep`'s freezing and its damage. + /// Its client moves it, so the slowing and the rest are the client's. pub fn server_block_damage(&mut self, from: Vec3) -> Result<()> { self.player.old_position = from; - self.effects_from_blocks(true) + // Entity.baseTick + self.player.was_in_powder_snow = self.player.in_powder_snow; + self.player.in_powder_snow = false; + self.effects_from_blocks(true)?; + // LivingEntity.aiStep "freezing" (server). + if !self.player.in_powder_snow || !self.player.can_freeze() { + self.player.ticks_frozen = (self.player.ticks_frozen - 2).max(0); + } + if self.player.health.tick_count % 40 == 0 + && self.player.is_fully_frozen() + && self.player.can_freeze() + { + self.freeze_damage()?; + } + Ok(()) } fn effects_from_blocks(&mut self, damage_only: bool) -> Result<()> { use horizon_world::entity_move::MovementRecord; @@ -1191,7 +1206,7 @@ impl MotionWorld<'_, '_, '_> { groups += 1; last_step = step; } - if groups == 1 { + if groups == 1 && !damage_only { // A later group finds the fire already out. self.player.powder_snow_entered.push(p); } @@ -1316,6 +1331,8 @@ impl MotionWorld<'_, '_, '_> { "minecraft:sweet_berry_bush" if damage_only => { self.sweet_berry_bush_damage(state)?; } + // PowderSnowBlock.entityInside's FREEZE. + "minecraft:powder_snow" if damage_only => entered.push((step, p)), _ => {} } if damage_only { From e458e3724924360dfad1252358368ff090cc4dae Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:02:36 +0100 Subject: [PATCH 092/238] Hub: journal.json in the flat format Kira reads, at /data/journal.json One entry per merged pull request (with its roadmap item), game or launcher release, shipped roadmap item (post and announce) and weekly summary, newest first, ids stable forever: the feed kira's src/journal.js posts to #updates and X. The journal's item links are hub#; the board's and the journal's ids are reserved too, and the page-wide unique-id test covers every tab. Co-Authored-By: Claude Opus 5.5 --- roadmap/README.md | 36 ++++++++++++------ tools/roadmap.py | 2 +- website/README.md | 2 +- website/build.mjs | 2 +- website/src/hub.html | 2 +- website/src/hub.js | 79 ++++++++++++++++++++++++++++++++-------- website/src/hub.test.mjs | 51 +++++++++++++++++++++++--- 7 files changed, 136 insertions(+), 38 deletions(-) diff --git a/roadmap/README.md b/roadmap/README.md index f16e83f8c..55cf9d09f 100644 --- a/roadmap/README.md +++ b/roadmap/README.md @@ -53,7 +53,7 @@ Whoever moves the work (the orchestrator on each heartbeat, or anyone in their p 5. Once a week, write the week's summary in `roadmap/journal/-.md` (for example `2026-43.md`): `# Title`, a paragraph or two for players, then `## Technical` and the same week for contributors, with its pull requests. `build` puts it into website/data/roadmap.json; the hub's Journal tab - shows it among the releases, and `/hub/journal.json` carries it to Kira's #updates and to posts elsewhere. + shows it among the releases, and `/data/journal.json` carries it to Kira's #updates and to X. 6. Suggestions from the website and Discord are read and triaged by the maintainers. A good one becomes an item in a normal pull request; none is published automatically. @@ -62,18 +62,30 @@ or when the file breaks a rule; `python3 tools/roadmap.py check --offline` runs ## The journal -The hub's Journal tab and `https://playhorizon.dev/hub/journal.json` are built with each deploy of the website -(`website/src/hub.js`, `journalFrom`): every game and launcher release with its notes' changes, each placed under -the roadmap item it moves (the pull request's `roadmap:` label, else the item that links it); the merged pull -requests no release has yet; and the weekly summaries above. Its JSON, newest first: +The hub's Journal tab and `https://playhorizon.dev/data/journal.json` are built with each deploy of the website +(`website/src/hub.js`). The tab shows every game and launcher release with its notes' changes, each placed under +the roadmap item it moves (the pull request's `roadmap:` label, else the item that links it), the merged pull +requests no release has yet, and the weekly summaries above. + +`journal.json` is what Kira reads for `#updates` and its X posts (kira's `src/journal.js`): one flat entry per +merged pull request, release, shipped item and weekly summary, newest first, the latest 300. ```json -{ "format": "horizon-journal", "version": 1, "generated": "…", "url": "https://playhorizon.dev/hub#journal", - "entries": [ { "id": "v0.2.57", "kind": "release | launcher | merged | week", "date": "…", "title": "…", - "plain": "one line for players", "url": "…", "dev": "weeks only", - "changes": [ { "section": "new | fixed | merged", "text": "…", "prs": [1787], "item": "discord-presence" } ], - "items": [ { "id", "title", "plain", "post" } ], // roadmap items the entry moved - "shipped": [ { "id", "title", "plain", "post" } ] } ] } // items that shipped in this release +{ "format": "horizon-journal", "version": 2, "generated": "…", "url": "https://playhorizon.dev/hub#journal", + "entries": [ + { "id": "pr-1788", "kind": "merge", "date": "…", "title": "the pull request's title", "url": "…", + "number": 1788, "item": "discord-presence", "item_title": "Discord Rich Presence" }, + { "id": "release-v0.2.57", "kind": "release", "date": "…", "title": "…", "plain": "one line", "url": "…", + "tag": "v0.2.57" }, + { "id": "shipped-server-resource-packs", "kind": "shipped", "date": "the release's", "title": "…", + "plain": "…", "url": "https://playhorizon.dev/hub#server-resource-packs", "item": "…", "item_title": "…", + "shipped_in": "v0.2.48", "post": "at most 240 characters", "announce": false }, + { "id": "weekly-2026-43", "kind": "weekly", "date": "the week's last second", "title": "…", "plain": "…", + "dev": "…", "url": "https://playhorizon.dev/hub#week-2026-43" } ] } ``` -`post` is at most 240 characters, ready to announce as is. Readers should remember the ids they have posted. +- An `id` never changes: readers post each one once. `merge` entries are the pull requests players see (no + `skip-release` label), plus any that moves a roadmap item; `item` is absent when none does. +- `announce: true` (the item's `announce` key) asks Kira to post the shipped item on X too, with `post`. +- `https://playhorizon.dev/data/roadmap.json` is `roadmap.toml` as JSON (`areas`, `items` with every key above, + `overview`, the weekly summaries); an item's page is `https://playhorizon.dev/hub#`. diff --git a/tools/roadmap.py b/tools/roadmap.py index ab0147e70..fe97285ff 100644 --- a/tools/roadmap.py +++ b/tools/roadmap.py @@ -57,7 +57,7 @@ ISSUE = re.compile(r"#(\d+)$") ID = re.compile(r"[a-z0-9]+(-[a-z0-9]+)*$") # An item is the anchor playhorizon.dev/hub#: it can't take the name of a tab, a section or a heading. RESERVED = {"main", "overview", "roadmap", "board", "journal", "contribute", "hub-data", *STATUSES} -RESERVED_PREFIXES = ("h-", "tab-", "week-") +RESERVED_PREFIXES = ("h-", "tab-", "col-", "week-", "journal-") RELEASE = re.compile(r"(v\d[\w.]*|launcher@\d[\w.]*|launcher-v\d[\w.]*|website)$") MONTHS = ("January|February|March|April|June|July|August|September|October|November|December") # Kira's rule: no dates and no promises of when. "Coming soon" is the one allowed phrase. diff --git a/website/README.md b/website/README.md index 56fa515b9..d17bd0cd1 100644 --- a/website/README.md +++ b/website/README.md @@ -40,7 +40,7 @@ src/estimate.js estimates for unmeasured hardware from the nearest measured m src/markdown.js a small Markdown renderer (escapes everything), shared by build and page src/releases.js the home page's "Recently added" and the Releases page's markup, shared by build and page src/contributors.js the contributors' filtering and markup, shared by build and page -src/hub.js the hub's roadmap, board and journal from data/roadmap.json, GitHub's issues and the releases, shared by build and page; /hub/journal.json +src/hub.js the hub's roadmap, board and journal from data/roadmap.json, GitHub's issues and the releases, shared by build and page; /data/journal.json src/hub-page.js the hub page: tabs by #hash, the Simple / Technical switch (localStorage), the area filter, live states data/roadmap.json the roadmap, generated by tools/roadmap.py from roadmap/roadmap.toml (do not edit); served at /data/roadmap.json public/ fonts, screenshots, icons, og.png, robots.txt, benchmarks.json: copied as is (sitemap.xml is written by the build) diff --git a/website/build.mjs b/website/build.mjs index 17768e5fc..c3203891e 100644 --- a/website/build.mjs +++ b/website/build.mjs @@ -437,7 +437,7 @@ for (const { file, values: own } of docPages) { await mkdir(join(dist, "data"), { recursive: true }); await cp(join(site, "data", "roadmap.json"), join(dist, "data", "roadmap.json")); // The journal for Kira's #updates and its X posts: /data/journal.json, rebuilt with every deploy. -await writeFile(join(dist, "data", "journal.json"), JSON.stringify(journalJson(journal, roadmap), null, 1) + "\n"); +await writeFile(join(dist, "data", "journal.json"), JSON.stringify(journalJson(releases, hubLive, roadmap), null, 1) + "\n"); // The pages' inline scripts (they mark the page as scripted and apply the saved theme before CSS // applies) and the inlined stylesheet: the Content-Security-Policy in the Caddyfile allows them by hash. diff --git a/website/src/hub.html b/website/src/hub.html index a6e146080..d4ba4600c 100644 --- a/website/src/hub.html +++ b/website/src/hub.html @@ -71,7 +71,7 @@
  • -

    Every release with what it changed, grouped by roadmap item in Technical, and a summary of each week. Also as journal.json.

    +

    Every release with what it changed, grouped by roadmap item in Technical, and a summary of each week. Also as journal.json.

    {{HUB_JOURNAL}}
    diff --git a/website/src/hub.js b/website/src/hub.js index cda3b4e5b..d293b7250 100644 --- a/website/src/hub.js +++ b/website/src/hub.js @@ -319,19 +319,66 @@ export function journalFrom(releases, list, roadmap) { return entries.sort((a, b) => b.date.localeCompare(a.date)).slice(0, ENTRIES); } -// /hub/journal.json: the journal for Kira's #updates and posts elsewhere, with each shipped item's short text. -export function journalJson(entries, roadmap, generated = new Date().toISOString()) { - const item = (id) => { - const i = roadmap.items.find((x) => x.id === id); - return i && { id: i.id, title: i.title, plain: i.plain, post: i.post || i.plain }; - }; - return { - format: "horizon-journal", - version: 1, - generated, - url: JOURNAL_URL, - entries: entries.map((e) => ({ ...e, items: [...new Set(e.changes.map((c) => c.item).filter(Boolean))].map(item).filter(Boolean), shipped: e.shipped.map(item).filter(Boolean) })), - }; +const HUB = "https://playhorizon.dev/hub"; +const FEED = 300; + +// /data/journal.json, which Kira reads for #updates and X (kira's src/journal.js): one flat entry per merged pull +// request, release, shipped roadmap item and weekly summary, newest first. An entry's id never changes: Kira posts +// each id once. Shipped items carry `post` (at most 240 characters, tools/roadmap.py checks) and `announce`. +export function journalJson(releases, list, roadmap, generated = new Date().toISOString()) { + const items = new Map(roadmap.items.map((i) => [i.id, i])); + const ours = releases.filter((r) => !r.draft && (isGame(r.tag_name) || isLauncher(r.tag_name))); + const published = new Map(ours.map((r) => [r.tag_name, r.published_at || ""])); + const merges = list + .filter((i) => i.kind === "pr" && i.state === "merged") + .map((pr) => ({ pr, item: itemOf(pr, roadmap) })) + // What players see, and anything that moves a roadmap item. + .filter(({ pr, item }) => item || !pr.labels.includes("skip-release")) + .map(({ pr, item }) => ({ + id: `pr-${pr.n}`, + kind: "merge", + date: pr.at, + title: pr.title, + url: pr.url, + number: pr.n, + ...(item ? { item, item_title: items.get(item)?.title } : {}), + })); + const released = ours.map((r) => ({ + id: `release-${r.tag_name}`, + kind: "release", + date: r.published_at || "", + title: r.name || r.tag_name, + plain: parts(r).line, + url: r.html_url, + tag: r.tag_name, + })); + const shipped = roadmap.items + .filter((i) => i.status === "shipped") + .map((i) => ({ + id: `shipped-${i.id}`, + kind: "shipped", + date: published.get(i.shipped_in) || generated, + title: i.title, + plain: i.plain, + url: `${HUB}#${i.id}`, + item: i.id, + item_title: i.title, + shipped_in: i.shipped_in, + post: i.post || i.plain, + announce: Boolean(i.announce), + })); + // A week's summary is dated at the end of its week, after that week's releases. + const weekly = (roadmap.journal || []).map((w) => ({ + id: `weekly-${w.week}`, + kind: "weekly", + date: new Date(Date.parse(w.date) + 7 * 864e5 - 1000).toISOString(), + title: w.title, + plain: w.plain, + dev: w.dev, + url: `${HUB}#week-${w.week}`, + })); + const entries = [...merges, ...released, ...shipped, ...weekly].sort((a, b) => b.date.localeCompare(a.date) || a.id.localeCompare(b.id)); + return { format: "horizon-journal", version: 2, generated, url: `${HUB}#journal`, entries: entries.slice(0, FEED) }; } const KIND = { release: "Game", launcher: "Launcher", merged: "Merged", week: "This week" }; @@ -349,7 +396,7 @@ function changesHtml(entry, roadmap) { const dev = `
    ${order .map((id) => { const item = roadmap.items.find((i) => i.id === id); - return `

    ${item ? `${escape(item.title)}` : "Other changes"}

      ${groups.get(id).map(line).join("")}
    `; + return `

    ${item ? `${escape(item.title)}` : "Other changes"}

      ${groups.get(id).map(line).join("")}
    `; }) .join("")}
    `; return simple + dev; @@ -364,8 +411,8 @@ export function journalHtml(entries, roadmap) {

    ${e.url ? `${escape(e.title)}` : escape(e.title)}

    ${e.kind === "week" ? `

    ${inline(e.plain)}

    ${inline(e.dev.replace(/(^|[^\w[/`])#(\d+)\b/g, (_, b, n) => `${b}[#${n}](${issueUrl(n)})`))}

    ` : ""} - ${shipped.length ? `

    Shipped: ${shipped.map((i) => `${escape(i.title)}`).join(", ")}

    ` : ""} - ${items.length ? `

    ${items.map((i) => `${escape(i.title)}`).join("")}

    ` : ""} + ${shipped.length ? `

    Shipped: ${shipped.map((i) => `${escape(i.title)}`).join(", ")}

    ` : ""} + ${items.length ? `

    ${items.map((i) => `${escape(i.title)}`).join("")}

    ` : ""} ${changesHtml(e, roadmap)}
  • `; }) diff --git a/website/src/hub.test.mjs b/website/src/hub.test.mjs index a6337399b..d688489e9 100644 --- a/website/src/hub.test.mjs +++ b/website/src/hub.test.mjs @@ -1,7 +1,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; -import { boardFrom, fixesIn, itemOf, journalFrom, journalHtml, journalJson, overviewHtml, progress, roadmapHtml, slimIssue } from "./hub.js"; +import { boardFrom, boardHtml, fixesIn, itemOf, journalFrom, journalHtml, journalJson, overviewHtml, progress, roadmapHtml, slimIssue } from "./hub.js"; const roadmap = JSON.parse(readFileSync(new URL("../data/roadmap.json", import.meta.url), "utf8")); @@ -42,7 +42,11 @@ test("the roadmap renders every item once, with both texts, in its status's sect }); test("an item is its own anchor (hub#, as Kira links it) and takes no other id of the page", () => { - const page = readFileSync(new URL("./hub.html", import.meta.url), "utf8") + roadmapHtml(roadmap) + overviewHtml(roadmap); + const week = { week: "2030-01", date: "2029-12-31", title: "A week", plain: "p", dev: "d" }; + const release = { tag_name: "v0.2.2", name: "0.2.2", published_at: "2030-01-02T00:00:00Z", html_url: "u", body: "" }; + const journal = journalHtml(journalFrom([release], [], { ...roadmap, journal: [week] }), roadmap); + const board = boardHtml(boardFrom([], roadmap), roadmap); + const page = readFileSync(new URL("./hub.html", import.meta.url), "utf8") + roadmapHtml(roadmap) + overviewHtml(roadmap) + board + journal; const ids = [...page.matchAll(/ id="([^"]+)"/g)].map((m) => m[1]); assert.deepEqual(ids.filter((id, i) => ids.indexOf(id) !== i), []); for (const item of roadmap.items) assert.ok(ids.includes(item.id), item.id); @@ -91,10 +95,45 @@ test("the journal groups a release's changes by roadmap item and lists what awai assert.deepEqual(entries.map((e) => e.id), ["next-release", "v0.2.2", "launcher@0.2.1"]); assert.deepEqual(entries[0].changes.map((c) => [c.prs, c.item]), [[[1788], "discord-presence"]]); assert.deepEqual(entries[1].changes.map((c) => [c.section, c.text, c.item]), [["new", "Discord Rich Presence", "discord-presence"], ["fixed", "A fix", null]]); - const json = journalJson(entries, roadmap, "now"); - assert.equal(json.format, "horizon-journal"); - assert.equal(json.entries[1].items[0].post.length <= 240, true); const html = journalHtml(entries, roadmap); - assert.ok(html.includes('href="#item-discord-presence"')); + assert.ok(html.includes('href="#discord-presence"')); assert.ok(html.includes("And 1 fix.")); }); + +test("journal.json is the flat feed Kira reads: merges, releases, shipped items, weeks, ids that never change", () => { + const releases = [ + { tag_name: "v0.2.48", name: "Horizon 0.2.48", published_at: "2030-01-02T00:00:00Z", html_url: "https://github.com/ludovic111/horizon/releases/tag/v0.2.48", body: "### New\n- Packs (#1)\n" }, + { tag_name: "nightly-1", name: "Nightly", published_at: "2030-01-03T00:00:00Z", html_url: "u", body: "" }, + ]; + const list = [ + pr(1788, { state: "closed", pull_request: { merged_at: "2030-01-04T00:00:00Z" }, html_url: "https://github.com/ludovic111/horizon/pull/1788" }), + pr(7, { state: "closed", pull_request: { merged_at: "2030-01-04T00:00:00Z" }, labels: [{ name: "skip-release" }] }), + pr(8, { state: "closed", pull_request: { merged_at: "2030-01-04T00:00:00Z" }, labels: [{ name: "skip-release" }, { name: "roadmap:lean-code" }] }), + pr(9), + ].map(slimIssue); + const week = { week: "2030-01", date: "2029-12-31", title: "A week", plain: "p", dev: "d" }; + const feed = journalJson(releases, list, { ...roadmap, journal: [week] }, "2030-01-05T00:00:00Z"); + const byId = new Map(feed.entries.map((e) => [e.id, e])); + // A merged pull request, with its roadmap item; skip-release ones only when they move an item; open ones never. + assert.deepEqual(byId.get("pr-1788"), { id: "pr-1788", kind: "merge", date: "2030-01-04T00:00:00Z", title: "PR 1788", url: "https://github.com/ludovic111/horizon/pull/1788", number: 1788, item: "discord-presence", item_title: "Discord Rich Presence" }); + assert.ok(!byId.has("pr-7") && byId.get("pr-8").item === "lean-code" && !byId.has("pr-9")); + // Game and launcher releases only. + assert.equal(byId.get("release-v0.2.48").tag, "v0.2.48"); + assert.ok(!byId.has("release-nightly-1")); + // Every shipped item, dated by its release when the feed has it, with its post and the announce flag. + const shipped = roadmap.items.filter((i) => i.status === "shipped"); + for (const item of shipped) { + const e = byId.get(`shipped-${item.id}`); + assert.equal(e.kind, "shipped"); + assert.equal(e.url, `https://playhorizon.dev/hub#${item.id}`); + assert.ok(e.post.length <= 240 && typeof e.announce === "boolean", item.id); + } + assert.equal(byId.get("shipped-server-resource-packs").date, "2030-01-02T00:00:00Z"); + assert.equal(byId.get("weekly-2030-01").date, "2030-01-06T23:59:59.000Z"); + // Newest first; every entry has what Kira needs, and links only where Kira shows them. + assert.deepEqual(feed.entries.map((e) => e.date), feed.entries.map((e) => e.date).sort().reverse()); + for (const e of feed.entries) { + assert.ok(e.id && ["merge", "release", "weekly", "shipped"].includes(e.kind) && e.date && e.title, e.id); + assert.match(e.url, /^https:\/\/(github\.com|playhorizon\.dev)\//, e.id); + } +}); From 10efca9e6cd922ff7b9a4a3cc3f5a467e97f52f2 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Sun, 25 Oct 2026 15:40:29 +0100 Subject: [PATCH 093/238] Discard a dead player's ender pearl when the rule says so With ender_pearls_vanish_on_death on, a pearl thrown just before death kept flying and pulled the respawned player back to it. Vanilla's ThrownEnderpearl.tick discards the pearl instead of ticking it while its owner is dead. Add a harness that throws a pearl, dies and respawns on a dedicated server and compares the teleports with vanilla's. --- crates/horizon-client/src/game/creatures.rs | 19 +++ .../horizonlab/parity/LocatorBarClient.java | 15 +- tools/test_pearl_death_parity.py | 142 ++++++++++++++++++ 3 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 tools/test_pearl_death_parity.py diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 0bb7bbe85..ff07e9011 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -1694,7 +1694,26 @@ impl Game<'_> { targets: targets_guests, }; let guest_entities = self.lan_guest_entities(); + // ThrownEnderpearl.tick: a dead player's pearl is discarded in + // place of its tick while ender_pearls_vanish_on_death is on. + let vanish = self.ui.rules.bool("ender_pearls_vanish_on_death"); + let host_dead = self.player_view().is_some_and(|p| !p.alive); + let dead_guests: Vec = self.lan.as_ref().map_or_else(Vec::new, |h| { + h.hub + .guests + .iter() + .filter(|g| g.dead) + .map(|g| g.conn) + .collect() + }); projectiles.retain_mut(|p| { + if vanish + && matches!(p.kind, ProjectileKind::EnderPearl) + && p.owner == Some(Who::Player) + && p.guest.map_or(host_dead, |c| dead_guests.contains(&c)) + { + return false; + } let mob_shot = !matches!(p.owner, Some(Who::Player)); // A player's arrows, snowballs and eggs meet every player; a // guest's flies from that guest (Projectile.getOwner), not diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java b/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java index 76a951894..818746336 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java @@ -84,7 +84,10 @@ import net.minecraft.world.phys.Vec3; * still), {@code sync NAME} (runs {@code time query gametime}, whose reply is * not recorded, and prints {@code synced NAME} once it is back: the server has * handled everything this player sent before, and everything the server sent - * this player before the reply has been recorded), {@code quit NAME}, {@code + * this player before the reply has been recorded), {@code respawn NAME} (the + * death screen's button, ServerboundClientCommandPacket PERFORM_RESPAWN), + * {@code use NAME PITCH} (the main hand's item used looking that way, + * ServerboundUseItemPacket), {@code quit NAME}, {@code * players on|off} (whether each player also records the other players added * to and removed from its level, and its own respawns into a dimension), * {@code chat on|off} (whether the system chat each player receives is @@ -130,6 +133,9 @@ public final class LocatorBarClient { players.get(w[1]).send(new net.minecraft.network.protocol.game.ServerboundPlayerAbilitiesPacket(abilities)); } case "still" -> players.get(w[1]).still(); + case "respawn" -> players.get(w[1]).send(new net.minecraft.network.protocol.game.ServerboundClientCommandPacket( + net.minecraft.network.protocol.game.ServerboundClientCommandPacket.Action.PERFORM_RESPAWN)); + case "use" -> players.get(w[1]).use(Float.parseFloat(w[2])); case "sync" -> { try { players.get(w[1]).sync(); @@ -250,6 +256,13 @@ public final class LocatorBarClient { } } + private int sequence; + + void use(float pitch) throws Exception { + send(new net.minecraft.network.protocol.game.ServerboundUseItemPacket( + net.minecraft.world.InteractionHand.MAIN_HAND, ++sequence, position.yRot(), pitch)); + } + void still() throws Exception { PositionMoveRotation at = position; // A tick of its own: the server takes one position per client tick (receivedPositionThisTick). diff --git a/tools/test_pearl_death_parity.py b/tools/test_pearl_death_parity.py new file mode 100644 index 000000000..a6795c749 --- /dev/null +++ b/tools/test_pearl_death_parity.py @@ -0,0 +1,142 @@ +#!/usr/bin/env python3 +"""An ender pearl in flight when its thrower dies, on a dedicated server, against vanilla's, seen by a vanilla client. + +A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat world +vanilla generated first, with Alice an operator in survival. LocatorBarClient (a player made of vanilla's protocol +code) throws an ender pearl straight up (ServerboundUseItemPacket), runs /kill on itself while the pearl flies, +presses the death screen's button (PERFORM_RESPAWN) and waits for the pearl to come down, with the rule +ender_pearls_vanish_on_death true: ThrownEnderpearl.tick discards a dead owner's pearl, so the respawned player is +never teleported. The teleports, game mode changes and respawns Alice is sent from the throw on are compared +exactly. +""" +import json +import os +from pathlib import Path +import platform +import shutil +import subprocess +import sys +import time + +try: + from . import lab, test_rust_parity as base, test_locator_bar_parity as locator + from .test_dedicated_server_parity import Server +except ImportError: + import lab + import test_rust_parity as base + import test_locator_bar_parity as locator + from test_dedicated_server_parity import Server + +NAME = 'pearl-death-parity' +# server_dir and generate_world write these server.properties. +locator.PROPERTIES = dict(locator.PROPERTIES, gamemode='survival') +STEP_SECONDS = 4.0 +SHOWN = ('teleport', 'gamemode', 'respawn') + + +SCRIPT = [ + ('setup', ['cmd Alice gamerule ender_pearls_vanish_on_death true', 'cmd Alice give @s minecraft:ender_pearl']), + # Straight up: it comes down about four seconds later, near where it was thrown. + ('throw', ['use Alice -90', 'cmd Alice kill @s']), + ('respawn', ['respawn Alice']), + ('landed', []), +] + + +def run_side(label, server_command, client_command, build, world): + directory = build / label + port = locator.server_dir(directory, world) + out = build / f'{label}.txt' + out.unlink(missing_ok=True) + server = Server(server_command, directory, label) + try: + server.wait_for(r'Done \(') + with (build / f'{label}-client.log').open('w') as log: + client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), str(out)], + cwd=lab.ROOT, stdin=subprocess.PIPE, stdout=log, stderr=subprocess.STDOUT, + text=True) + + def tell(line): + client.stdin.write(line + '\n') + client.stdin.flush() + try: + tell('players on') + tell('state on') + tell('step joins') + tell('join Alice') + server.wait_for(r'Alice joined the game', timeout=120) + time.sleep(STEP_SECONDS) + for step, actions in SCRIPT: + tell(f'step {step}') + for action in actions: + tell(action) + time.sleep(STEP_SECONDS) + tell('end') + client.wait(timeout=60) + finally: + if client.poll() is None: + client.kill() + finally: + server.stop() + if not out.exists(): + raise AssertionError(f'{label}: the client wrote no transcript (see {build}/{label}-client.log)') + return out.read_text(encoding='utf-8').splitlines() + + +def shown(lines): + """Alice's teleports, game mode changes and respawns from the throws on.""" + steps = {f'step={step}' for step, _ in SCRIPT if step != 'setup'} + return [line for line in lines if len(parts := line.split(' ', 3)) >= 3 and parts[1] in steps + and parts[2] in SHOWN] + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java' + classes = build / 'classes' + shutil.rmtree(classes, ignore_errors=True) + classes.mkdir() + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-nowarn', '-sourcepath', + lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle], + cwd=lab.ROOT, check=True, capture_output=True) + subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, + check=True, capture_output=True) + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + client = ['java', '-Xmx1g', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.LocatorBarClient'] + + world = locator.generate_world(vanilla_server, build) + vanilla_all = run_side('vanilla', vanilla_server, client, build, world) + horizon_all = run_side('horizon', horizon_server, client, build, world) + errors = [l for l in vanilla_all + horizon_all if ' error ' in f' {l} ' or 'disconnected' in l] + vanilla, horizon = shown(vanilla_all), shown(horizon_all) + if errors or vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=1, lineterm='')) + raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' + + '\n'.join(errors[:10]) + '\n' + diff[:6000]) + killed = any(l.startswith('Alice step=throw killed') for l in vanilla_all) + if not killed or 'Alice step=respawn respawn minecraft:overworld' not in vanilla: + raise AssertionError(f'vanilla did not kill and respawn Alice: the script did not run (see {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='An ender pearl in flight when its thrower dies and respawns on a dedicated server, with ' + 'ender_pearls_vanish_on_death true: the pearl vanishes and never teleports the player.')) + print(f'pearl death: {len(vanilla)} lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') From 0c017678eb50c5819f0e2948f3f8432093e027ac Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Sun, 25 Oct 2026 15:44:16 +0100 Subject: [PATCH 094/238] Keep shulker boxes out of shulker boxes on the server ShulkerBoxSlot.mayPlace refuses items that cannot fit inside container items, which BlockItem says of every shulker box. The server's menus for guests treated a shulker box's slots like a chest's, so with the server thread a shulker box could be clicked, shift-clicked or swapped into another. Apply the slot rule when the open menu is a shulker box. --- crates/horizon-client/src/lan/containers.rs | 9 +++++++++ crates/horizon-client/src/lan/menu.rs | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/crates/horizon-client/src/lan/containers.rs b/crates/horizon-client/src/lan/containers.rs index d43cb5c27..3dc085a6d 100644 --- a/crates/horizon-client/src/lan/containers.rs +++ b/crates/horizon-client/src/lan/containers.rs @@ -344,6 +344,14 @@ impl Hub { return Vec::new(); } let source = self.guests[i].menu.as_ref().map(|m| m.slots); + let shulker_box = self + .sync + .builtin + .get("minecraft:menu") + .and_then(|m| m.id("minecraft:shulker_box")); + let shulker = in_menu + && shulker_box.is_some() + && self.guests[i].menu.as_ref().map(|m| m.kind) == shulker_box; let mut drag = if in_menu { self.guests[i].menu.as_mut().and_then(|m| m.drag.take()) } else { @@ -357,6 +365,7 @@ impl Hub { let mut view = View { guest, container: items.as_deref_mut(), + shulker, }; let (drops, changed) = menu::click( &mut view, diff --git a/crates/horizon-client/src/lan/menu.rs b/crates/horizon-client/src/lan/menu.rs index de9fb9608..fefcb1933 100644 --- a/crates/horizon-client/src/lan/menu.rs +++ b/crates/horizon-client/src/lan/menu.rs @@ -111,6 +111,8 @@ pub struct View<'a> { pub guest: &'a mut Guest, /// The container's slots, or None for the inventory menu. pub container: Option<&'a mut [Option]>, + /// The container is a shulker box (`ShulkerBoxSlot`). + pub shulker: bool, } impl View<'_> { @@ -153,6 +155,14 @@ impl View<'_> { if self.container.is_none() && (5..=8).contains(&i) { return rules.armor_index(&stack.item) == Some(i - 5); } + // ShulkerBoxSlot.mayPlace: Item.canFitInsideContainerItems, false + // for shulker boxes. + if self.shulker + && self.container.as_ref().is_some_and(|c| i < c.len()) + && stack.item.ends_with("shulker_box") + { + return false; + } self.enabled(i) } fn slot_max(&self, i: usize, stack: &ItemStack, rules: &dyn Rules) -> u32 { @@ -704,6 +714,7 @@ mod tests { let mut view = View { guest: &mut g, container: Some(&mut chest), + shulker: false, }; view.guest.carried = Some(ItemStack::new("minecraft:bundle", 1)); // Left click with the bundle: the stone goes in. From 5bfc649edd18aaf9738dc428bc24ae9e991cf87d Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Sun, 25 Oct 2026 15:52:29 +0100 Subject: [PATCH 095/238] Drop the cursor stack of a dead player A stack held on the inventory cursor at death stayed there through the respawn, even with keep_inventory off. In vanilla the dead player is removed 20 ticks after dying (or when it respawns sooner), and Player.remove's inventoryMenu.removed drops what the cursor held where the player died. Do the same for server players and the in-frame player, and add a harness that dies with a cursor stack on a dedicated server and compares with vanilla. --- crates/horizon-client/src/game.rs | 20 ++- crates/horizon-client/src/game/chat.rs | 2 + crates/horizon-client/src/game/lan_actions.rs | 27 +++- crates/horizon-client/src/lan/guest.rs | 3 + .../horizonlab/parity/LocatorBarClient.java | 10 +- tools/test_cursor_death_parity.py | 143 ++++++++++++++++++ 6 files changed, 202 insertions(+), 3 deletions(-) create mode 100644 tools/test_cursor_death_parity.py diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 699468651..667eff52a 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2173,6 +2173,11 @@ impl Game<'_> { // Player.drop: the drop statistics. self.stats.add("dropped", &thrown.item, thrown.count as i32); self.stats.custom("drop", 1); + self.throw_from_eyes(thrown, eye); + } + /// `Player.createItemStackToDrop`: an item entity thrown from the eyes + /// along the view direction. + fn throw_from_eyes(&mut self, thrown: ItemStack, eye: [f64; 3]) { let (yaw, pitch) = ( f64::from(self.yaw).to_radians(), f64::from(self.pitch).to_radians(), @@ -4335,7 +4340,8 @@ impl Game<'_> { // Out of the world. let _ = player.set_health(0.0, false); } - if player.health.dead_or_dying() && self.screen != Screen::Dead { + let dead = player.health.dead_or_dying(); + if dead && self.screen != Screen::Dead { // Inventory.dropAll: every stack thrown in a random direction. let feet = [player.position.x, player.position.y, player.position.z]; let at = [feet[0], feet[1] + 1.32, feet[2]]; @@ -4369,6 +4375,18 @@ impl Game<'_> { } self.screen = Screen::Dead; self.capture(false); + self.ui.death_time = 0; + } else if dead { + // LivingEntity.tickDeath: 20 ticks after dying the player is + // removed, and Player.remove's inventoryMenu.removed drops what + // the cursor held. + self.ui.death_time += 1; + if self.ui.death_time >= 20 + && let Some(stack) = self.carried.take() + && let Some(eye) = self.eye_position() + { + self.throw_from_eyes(stack, eye); + } } self.tick_world(running) } diff --git a/crates/horizon-client/src/game/chat.rs b/crates/horizon-client/src/game/chat.rs index ec85732ae..15e34bbb6 100644 --- a/crates/horizon-client/src/game/chat.rs +++ b/crates/horizon-client/src/game/chat.rs @@ -99,6 +99,8 @@ pub struct UiState { /// Death screen cause and when the player died (buttons wait 20 ticks). pub death_text: Option, pub died_at: Option, + /// `LivingEntity.deathTime` of the window's player: ticks since it died. + pub death_time: u32, } impl Game<'_> { diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 2c7019c73..de6ae51dc 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -1234,6 +1234,24 @@ impl Game<'_> { Some(out) } + /// `LivingEntity.tickDeath`: 20 ticks after dying the player is removed, + /// and `Player.remove`'s `inventoryMenu.removed` drops what the cursor + /// held (`dropOrPlaceInInventory` for a removed player). Respawning + /// sooner removes it too. + fn guest_tick_death(&mut self, host: &mut LanHost, gi: usize) { + let g = &mut host.hub.guests[gi]; + g.death_time += 1; + if g.death_time >= 20 { + self.guest_drop_carried(host, gi); + } + } + + fn guest_drop_carried(&mut self, host: &mut LanHost, gi: usize) { + if let Some(c) = host.hub.guests[gi].carried.take() { + self.drop_guest_items(host, gi, vec![c]); + } + } + /// `AbstractContainerMenu.removed`: what a closing player held on the /// cursor goes back into their inventory (`placeItemBackInInventory`, /// dropped only if it doesn't fit), and their client's cursor empties @@ -1573,6 +1591,8 @@ impl Game<'_> { if !host.hub.guests[i].dead { return; } + // PlayerList.respawn removes the dead player (Player.remove). + self.guest_drop_carried(host, i); // PlayerList.respawn: a server's player comes back in its respawn // point's level: the Overworld, where the world spawn and beds are, // or another one's anchor or forced point, which its block resolves @@ -1613,6 +1633,7 @@ impl Game<'_> { } let g = &mut host.hub.guests[i]; g.dead = false; + g.death_time = 0; g.health = 20.0; g.food = 20; g.saturation = 5.0; @@ -1694,7 +1715,11 @@ impl Game<'_> { continue; }; self.guest_sleep_tick(host, gi); - if host.hub.guests[gi].dead || self.on_server_trip(conn) { + if host.hub.guests[gi].dead { + self.guest_tick_death(host, gi); + continue; + } + if self.on_server_trip(conn) { continue; } let play = host.play.entry(conn).or_default(); diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index 440b58284..70510a5b4 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -223,6 +223,8 @@ pub struct Guest { /// Whether the guest was on the ground before the first of `moves`. pub moves_from_ground: bool, pub dead: bool, + /// `LivingEntity.deathTime`: ticks since the guest died. + pub death_time: u32, /// The join packets were sent (PlayerList.placeNewPlayer ran). pub placed: bool, /// The level the player is in (`ServerPlayer.level().dimension()`): the @@ -349,6 +351,7 @@ impl Guest { moves: Vec::new(), moves_from_ground: false, dead: false, + death_time: 0, placed: false, dimension: "minecraft:overworld".into(), menu: None, diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java b/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java index 76a951894..e4aeef25e 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java @@ -84,7 +84,10 @@ import net.minecraft.world.phys.Vec3; * still), {@code sync NAME} (runs {@code time query gametime}, whose reply is * not recorded, and prints {@code synced NAME} once it is back: the server has * handled everything this player sent before, and everything the server sent - * this player before the reply has been recorded), {@code quit NAME}, {@code + * this player before the reply has been recorded), {@code respawn NAME} (the + * death screen's button, ServerboundClientCommandPacket PERFORM_RESPAWN), + * {@code pickup NAME SLOT} (a left click on that slot of the player's + * inventory menu, claiming nothing), {@code quit NAME}, {@code * players on|off} (whether each player also records the other players added * to and removed from its level, and its own respawns into a dimension), * {@code chat on|off} (whether the system chat each player receives is @@ -130,6 +133,11 @@ public final class LocatorBarClient { players.get(w[1]).send(new net.minecraft.network.protocol.game.ServerboundPlayerAbilitiesPacket(abilities)); } case "still" -> players.get(w[1]).still(); + case "respawn" -> players.get(w[1]).send(new net.minecraft.network.protocol.game.ServerboundClientCommandPacket( + net.minecraft.network.protocol.game.ServerboundClientCommandPacket.Action.PERFORM_RESPAWN)); + case "pickup" -> players.get(w[1]).send(new net.minecraft.network.protocol.game.ServerboundContainerClickPacket( + 0, 0, Short.parseShort(w[2]), (byte) 0, net.minecraft.world.inventory.ContainerInput.PICKUP, + new it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap<>(), net.minecraft.network.HashedStack.EMPTY)); case "sync" -> { try { players.get(w[1]).sync(); diff --git a/tools/test_cursor_death_parity.py b/tools/test_cursor_death_parity.py new file mode 100644 index 000000000..8da944350 --- /dev/null +++ b/tools/test_cursor_death_parity.py @@ -0,0 +1,143 @@ +#!/usr/bin/env python3 +"""What a player holds on the cursor when they die, on a dedicated server, against vanilla's, seen by a vanilla client. + +A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat world +vanilla generated first, with Alice an operator in survival and keep_inventory off. LocatorBarClient (a player made +of vanilla's protocol code) is given seven diamonds, picks them up onto its inventory menu's cursor (a PICKUP click) +ten blocks from the spawn, runs /kill on itself, waits on the death screen and respawns (PERFORM_RESPAWN). Twenty +ticks after dying vanilla removes the dead player (LivingEntity.tickDeath), and Player.remove's +inventoryMenu.removed drops the cursor's stack where it died. Alice then asks whether an item lies around the death +site; the reply, and the inventory slots, respawns and teleports she is sent from the death on, are compared +exactly. +""" +import json +import os +from pathlib import Path +import platform +import shutil +import subprocess +import sys +import time + +try: + from . import lab, test_rust_parity as base, test_locator_bar_parity as locator + from .test_dedicated_server_parity import Server +except ImportError: + import lab + import test_rust_parity as base + import test_locator_bar_parity as locator + from test_dedicated_server_parity import Server + +NAME = 'cursor-death-parity' +# server_dir and generate_world write these server.properties. +locator.PROPERTIES = dict(locator.PROPERTIES, gamemode='survival') +SCRIPT = [ + ('setup', ['cmd Alice gamerule keep_inventory false', 'cmd Alice clear @s', + 'cmd Alice give @s minecraft:diamond 7', 'cmd Alice tp @s 10.5 -60 0.5']), + # Hotbar slot 0 is the inventory menu's slot 36. + ('pickup', ['pickup Alice 36']), + ('death', ['cmd Alice kill @s']), + ('respawn', ['respawn Alice']), + ('check', ['chat Alice on', + 'cmd Alice execute if entity @e[type=minecraft:item,x=10.5,y=-60,z=0.5,distance=..4]']), +] +STEP_SECONDS = 4.0 +SHOWN = ('slot', 'inventory', 'respawn', 'teleport', 'chat') + + +def run_side(label, server_command, client_command, build, world): + directory = build / label + port = locator.server_dir(directory, world) + out = build / f'{label}.txt' + out.unlink(missing_ok=True) + server = Server(server_command, directory, label) + try: + server.wait_for(r'Done \(') + with (build / f'{label}-client.log').open('w') as log: + client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), str(out)], + cwd=lab.ROOT, stdin=subprocess.PIPE, stdout=log, stderr=subprocess.STDOUT, + text=True) + + def tell(line): + client.stdin.write(line + '\n') + client.stdin.flush() + try: + tell('players on') + tell('state on') + tell('step joins') + tell('join Alice') + server.wait_for(r'Alice joined the game', timeout=120) + time.sleep(STEP_SECONDS) + for step, actions in SCRIPT: + tell(f'step {step}') + for action in actions: + tell(action.replace('chat Alice', 'chat')) + time.sleep(STEP_SECONDS) + tell('end') + client.wait(timeout=60) + finally: + if client.poll() is None: + client.kill() + finally: + server.stop() + if not out.exists(): + raise AssertionError(f'{label}: the client wrote no transcript (see {build}/{label}-client.log)') + return out.read_text(encoding='utf-8').splitlines() + + +def shown(lines): + """Alice's slots, respawns, teleports and chat from the death on.""" + steps = {'step=death', 'step=respawn', 'step=check'} + return [line for line in lines if len(parts := line.split(' ', 3)) >= 3 and parts[1] in steps + and parts[2] in SHOWN] + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java' + classes = build / 'classes' + shutil.rmtree(classes, ignore_errors=True) + classes.mkdir() + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-nowarn', '-sourcepath', + lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle], + cwd=lab.ROOT, check=True, capture_output=True) + subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, + check=True, capture_output=True) + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + client = ['java', '-Xmx1g', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.LocatorBarClient'] + + world = locator.generate_world(vanilla_server, build) + vanilla_all = run_side('vanilla', vanilla_server, client, build, world) + horizon_all = run_side('horizon', horizon_server, client, build, world) + errors = [l for l in vanilla_all + horizon_all if ' error ' in f' {l} ' or 'disconnected' in l] + vanilla, horizon = shown(vanilla_all), shown(horizon_all) + if errors or vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=1, lineterm='')) + raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' + + '\n'.join(errors[:10]) + '\n' + diff[:6000]) + if not any(l.startswith('Alice step=check chat') for l in vanilla): + raise AssertionError(f'vanilla answered no check command: the script did not run (see {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='A player dying with a stack on the inventory menu cursor (keep_inventory off) and respawning on a ' + 'dedicated server: the stack is dropped where it died, not kept.')) + print(f'cursor death: {len(vanilla)} lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') From 91148cdf74b18741253bfcc20313c0f239a4c3b6 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:09:48 +0100 Subject: [PATCH 096/238] Telemetry server: hub votes allow 600 an hour per address (bursts of 60) A visitor voting down the roadmap, or a school behind one address, ran out of the 12-request burst. Votes are cheap; the item cap and the voter id still bound them. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-telemetry-server/README.md | 2 +- crates/horizon-telemetry-server/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/horizon-telemetry-server/README.md b/crates/horizon-telemetry-server/README.md index 2b86bf99d..69095be73 100644 --- a/crates/horizon-telemetry-server/README.md +++ b/crates/horizon-telemetry-server/README.md @@ -77,7 +77,7 @@ All are environment variables: - `HORIZON_TELEMETRY_LIVE_PER_HOUR` per address, pings and reads (600, bursts of 60: ten players behind one address) - `HORIZON_TELEMETRY_LIVE_WINDOW_SECONDS` (120) - `HORIZON_TELEMETRY_LIVE_MAX_SESSIONS` (200 000) -- `HORIZON_TELEMETRY_HUB_PER_HOUR` votes and vote reads per address (120, bursts of 12) +- `HORIZON_TELEMETRY_HUB_PER_HOUR` votes and vote reads per address (600, bursts of 60: a page load is one read, and several players can share an address) ## Hosting diff --git a/crates/horizon-telemetry-server/src/lib.rs b/crates/horizon-telemetry-server/src/lib.rs index 19ba3e1dc..9c273727c 100644 --- a/crates/horizon-telemetry-server/src/lib.rs +++ b/crates/horizon-telemetry-server/src/lib.rs @@ -75,7 +75,7 @@ impl Default for Config { live_per_hour: 600.0, live_window: horizon_telemetry::live::WINDOW, live_max_sessions: 200_000, - hub_per_hour: 120.0, + hub_per_hour: 600.0, } } } From bac51e3fba6922c0a10eb4105cfc68886d6ae573 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 15:55:50 +0100 Subject: [PATCH 097/238] Telemetry server: the hub's bug reports and suggestions POST /hub/report opens a GitHub issue at once (labels bug and from-website, the every template field required, people's text made inert (no mentions, issue references, HTML or closing keywords) and the same report twice answered with the first issue. If GitHub fails it answers 202 and the upkeep tries again for a week. Off (503) without HORIZON_TELEMETRY_GITHUB_TOKEN. POST /hub/suggest queues a suggestion; GET /hub/suggestions?after= hands the queue to Kira with the HORIZON_TELEMETRY_HUB_TOKEN bearer (404 when unset, 401 for a wrong token). Nothing is published. Per-address limits (20 an hour, bursts of 2) on keyed hashes of the address, daily caps from everyone (30 reports, 200 suggestions), a honeypot field, entries kept 90 days. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-telemetry-server/Cargo.lock | 52 +++ crates/horizon-telemetry-server/Cargo.toml | 2 + crates/horizon-telemetry-server/README.md | 18 +- crates/horizon-telemetry-server/src/github.rs | 128 ++++++ crates/horizon-telemetry-server/src/hub.rs | 13 +- crates/horizon-telemetry-server/src/intake.rs | 370 ++++++++++++++++++ crates/horizon-telemetry-server/src/lib.rs | 80 +++- crates/horizon-telemetry-server/src/main.rs | 3 + crates/horizon-telemetry-server/src/store.rs | 1 + .../horizon-telemetry-server/tests/github.rs | 41 ++ crates/horizon-telemetry-server/tests/hub.rs | 304 +++++++++++++- 11 files changed, 990 insertions(+), 22 deletions(-) create mode 100644 crates/horizon-telemetry-server/src/github.rs create mode 100644 crates/horizon-telemetry-server/src/intake.rs create mode 100644 crates/horizon-telemetry-server/tests/github.rs diff --git a/crates/horizon-telemetry-server/Cargo.lock b/crates/horizon-telemetry-server/Cargo.lock index e4e7d060e..9b5fc960d 100644 --- a/crates/horizon-telemetry-server/Cargo.lock +++ b/crates/horizon-telemetry-server/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + [[package]] name = "atomic-waker" version = "1.1.2" @@ -92,6 +98,15 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" +[[package]] +name = "crc32fast" +version = "1.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ba106e844045ddc02c20768ac4b0b7cb45f2cc2ecbd833ac9f5dacb183c2055" +dependencies = [ + "cfg-if", +] + [[package]] name = "displaydoc" version = "0.2.7" @@ -131,6 +146,17 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aedcfb3409746eddb02b9e19ebda1c3394f759a152e48ee875a0844d1b955484" +[[package]] +name = "flate2" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e634e2e0ebac1ee034020da1ca582e17ffe4e0f5e985823721e168928136dcb" +dependencies = [ + "crc32fast", + "miniz_oxide", + "zlib-rs", +] + [[package]] name = "foldhash" version = "0.1.5" @@ -235,6 +261,7 @@ dependencies = [ "serde_json", "tokio", "tower", + "ureq", ] [[package]] @@ -473,6 +500,16 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "miniz_oxide" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c" +dependencies = [ + "adler2", + "simd-adler32", +] + [[package]] name = "mio" version = "1.2.4" @@ -701,6 +738,12 @@ dependencies = [ "libc", ] +[[package]] +name = "simd-adler32" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea" + [[package]] name = "slab" version = "0.4.12" @@ -851,10 +894,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "02d1a66277ed75f640d608235660df48c8e3c19f3b4edb6a263315626cc3c01d" dependencies = [ "base64", + "flate2", "log", "once_cell", "rustls", "rustls-pki-types", + "serde", + "serde_json", "url", "webpki-roots 0.26.11", ] @@ -1099,6 +1145,12 @@ dependencies = [ "syn", ] +[[package]] +name = "zlib-rs" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112" + [[package]] name = "zmij" version = "1.0.23" diff --git a/crates/horizon-telemetry-server/Cargo.toml b/crates/horizon-telemetry-server/Cargo.toml index 8dbb0b7fd..486648b5f 100644 --- a/crates/horizon-telemetry-server/Cargo.toml +++ b/crates/horizon-telemetry-server/Cargo.toml @@ -19,6 +19,8 @@ rusqlite = { version = "0.37", features = ["bundled"] } serde = { version = "1", features = ["derive"] } serde_json = "1" ruzstd = "0.9" +# Opens the hub's bug reports as GitHub issues (blocking, run off the async workers; rustls). +ureq = { version = "2.12", features = ["json"] } [dev-dependencies] # The uploader the launcher uses, against this server, end to end. diff --git a/crates/horizon-telemetry-server/README.md b/crates/horizon-telemetry-server/README.md index 69095be73..a401a72b0 100644 --- a/crates/horizon-telemetry-server/README.md +++ b/crates/horizon-telemetry-server/README.md @@ -1,6 +1,6 @@ # horizon-telemetry-server -This server receives what players choose to send (performance summaries, crash and bug reports, benchmark uploads: [docs/telemetry.md](../../docs/telemetry.md)) and stores it in one SQLite file. It serves anonymous totals to the website. It also keeps the live player count from the games' anonymous "I'm playing" pings, in memory only, and the 👍 players give roadmap items on the hub (playhorizon.dev/hub). It is one small Rust process, about 7 MB of memory when idle. +This server receives what players choose to send (performance summaries, crash and bug reports, benchmark uploads: [docs/telemetry.md](../../docs/telemetry.md)) and stores it in one SQLite file. It serves anonymous totals to the website. It also keeps the live player count from the games' anonymous "I'm playing" pings, in memory only, and takes what players send from the hub (playhorizon.dev/hub): votes, bug reports and suggestions. It is one small Rust process, about 7 MB of memory when idle. cargo run --release # in this folder: its own workspace, like the launcher's PORT=8080 HORIZON_TELEMETRY_DB=/data/telemetry.sqlite3 horizon-telemetry-server @@ -17,10 +17,15 @@ It runs at **https://telemetry.playhorizon.dev** (`DEFAULT_ENDPOINT` in `crates/ | `GET /v1/benchmarks/summary` | Players' benchmark uploads grouped by GPU, CPU, render distance and game, for the website's Benchmarks page (below). Cached 10 minutes, same `Access-Control-Allow-Origin`. | | `POST /v1/live` | A game's "I'm playing" ping (JSON, at most 512 bytes). Returns 204. Returns 400 for a bad ping, 413 for one too large, 429 over the address's limit, 503 when it holds `LIVE_MAX_SESSIONS` already. | | `GET /v1/live` | How many play now, rounded: `playing`, `lan`, `versions`, `platforms`. Made again at most every 15 seconds, with the same `Access-Control-Allow-Origin`. | -| `POST /hub/vote` | `{"item": "", "voter": "", "up": true}`: a 👍 on a roadmap item, one per voter; `"up": false` takes it back. Returns `{"item", "votes"}`. 400 for a bad id, 429 over the address's limit. `OPTIONS` answers the website's preflight. | +| `POST /hub/vote` | `{"item": "", "voter": "", "up": true}`: a 👍 on a roadmap item, one per voter; `"up": false` takes it back. Returns `{"item", "votes"}`. 400 for a bad id, 429 over the address's limit. | | `GET /hub/votes` | `{"votes": {"": n}}`. Made again at most every minute. | +| `POST /hub/report` | A bug report from the hub's form: `{"title", "did", "happened", "expected", "version"?, "system", "name"?}`. Opens a GitHub issue at once (labels `bug` and `from-website`, the `#horizon-bugs` template, "Reported on playhorizon.dev") and returns 201 `{"id", "issue": ""}`. If GitHub fails: 202 `{"id", "issue": null}`, and the upkeep tries again for a week. The same report twice in a day returns the first one's issue. 400 for an empty or too long field, 429 over the address's limit, 503 past the day's cap or without `GITHUB_TOKEN`. | +| `POST /hub/suggest` | `{"text", "item"?, "name"?}` into the suggestion queue. Returns 202 `{"id"}`. Never published: Kira relays it to the maintainers' private channel. | +| `GET /hub/suggestions?after=&limit=100` | The queue, oldest first, for Kira: `{"suggestions": [{"id", "text", "item"?, "name"?, "created_at"}]}`. Needs `Authorization: Bearer `: 401 without it, 404 when the server has none. | | `GET /healthz` | `ok` | +The hub's posts take JSON from the website (`OPTIONS` preflights answer with its origin). A form's hidden `website` field filled in (a bot) gets 202 and is dropped. + The totals are grouped per version, OS, GPU vendor and render distance. Each group has its sessions, install-days, hours played, the 14 frame-time buckets summed, and the mean p50 and p99 frame times. Groups with fewer than 5 install-days are left out. The totals also list crash counts per version. Benchmark uploads are published only through the summary below, never one by one. ## Benchmarks summary @@ -58,7 +63,7 @@ The live count is a table in memory: a session ID, when it last pinged, its vers The day stored is the day the server received an item. The sender's IP address is never written down. The per-address rate limit keeps addresses in memory for an hour at most. -A hub vote is stored as an item ID and the browser's random ID. The hub's rate limits key their buckets by a hash of the address with a key drawn at each start, in memory. +The hub keeps votes as an item ID and the browser's random ID, and reports and suggestions as their text, for `HORIZON_TELEMETRY_HUB_KEEP_DAYS` (90). Its rate limits key their buckets by a hash of the address with a key drawn at each start, in memory. Raw rows are deleted after `HORIZON_TELEMETRY_RETENTION_DAYS` (180). The daily totals stay; they hold no install ID. @@ -78,6 +83,13 @@ All are environment variables: - `HORIZON_TELEMETRY_LIVE_WINDOW_SECONDS` (120) - `HORIZON_TELEMETRY_LIVE_MAX_SESSIONS` (200 000) - `HORIZON_TELEMETRY_HUB_PER_HOUR` votes and vote reads per address (600, bursts of 60: a page load is one read, and several players can share an address) +- `HORIZON_TELEMETRY_HUB_POSTS_PER_HOUR` reports and suggestions per address (20, bursts of 2) +- `HORIZON_TELEMETRY_HUB_REPORTS_PER_DAY` from everyone (30): each opens an issue +- `HORIZON_TELEMETRY_HUB_SUGGESTIONS_PER_DAY` from everyone (200) +- `HORIZON_TELEMETRY_HUB_KEEP_DAYS` (90) +- `HORIZON_TELEMETRY_HUB_TOKEN`: the token Kira reads the suggestions with (unset: `GET /hub/suggestions` is off) +- `HORIZON_TELEMETRY_GITHUB_TOKEN`: a fine-grained token with Issues read and write on the repository only (unset: `POST /hub/report` answers 503 and the form points to GitHub and Discord) +- `HORIZON_TELEMETRY_GITHUB_REPO` (`ludovic111/horizon`) ## Hosting diff --git a/crates/horizon-telemetry-server/src/github.rs b/crates/horizon-telemetry-server/src/github.rs new file mode 100644 index 000000000..a800442e7 --- /dev/null +++ b/crates/horizon-telemetry-server/src/github.rs @@ -0,0 +1,128 @@ +//! A website bug report as a GitHub issue: the text made inert (no mentions, no cross-references, no HTML), +//! written as the template Kira's `/report` and `#horizon-bugs` use, and the call that opens it. +use std::time::Duration; + +use serde_json::{Map, Value, json}; + +/// Opens an issue; its URL, or why not. The server's is [`Filer::github`]; tests give their own. +pub type Filer = dyn Fn(&Issue) -> Result + Send + Sync; + +pub struct Issue { + pub title: String, + pub body: String, + pub labels: Vec, +} + +/// Labels every website report gets. +pub const LABELS: [&str; 2] = ["bug", "from-website"]; + +/// Text people wrote, safe in an issue: no HTML, no `@` mentions, no `#12` or issue-URL cross-references, +/// no closing keywords, no code spans to break out of. The same rules as Kira's `neutralise`. +pub fn neutralise(text: &str) -> String { + let mut out = String::with_capacity(text.len()); + let chars: Vec = text.chars().collect(); + let mut i = 0; + while i < chars.len() { + let c = chars[i]; + let next = chars.get(i + 1).copied(); + let word_start = i == 0 || !chars[i - 1].is_alphanumeric(); + match c { + '<' => out.push_str("<"), + '>' => out.push_str(">"), + '`' => out.push('\''), + // A zero-width space after `@` or `#` keeps the text and drops the mention or the link. + '@' | '#' if next.is_some_and(|n| n.is_alphanumeric()) => { + out.push(c); + out.push('\u{200b}'); + } + _ if word_start && closing_keyword(&chars[i..]) => { + out.push(c); + out.push('\u{200b}'); + } + _ => out.push(c), + } + i += 1; + } + // github.com/owner/repo/issues/12 links cross-reference the issue: shown, not linked. + out.replace("github.com/", "github.com\u{200b}/") +} + +fn closing_keyword(rest: &[char]) -> bool { + let word: String = rest + .iter() + .take_while(|c| c.is_alphabetic()) + .flat_map(|c| c.to_lowercase()) + .collect(); + [ + "close", "closes", "closed", "fix", "fixes", "fixed", "resolve", "resolves", "resolved", + ] + .contains(&word.as_str()) +} + +/// Text as a markdown quote, every line prefixed. +fn quote(text: &str) -> String { + text.lines() + .map(|l| format!("> {l}").trim_end().to_owned()) + .collect::>() + .join("\n") +} + +/// The issue for a report's fields (`hub::REPORT`'s names, already checked). +pub fn issue(fields: &Map) -> Issue { + let field = |name: &str| { + fields + .get(name) + .and_then(Value::as_str) + .map(neutralise) + .filter(|s| !s.is_empty()) + .unwrap_or_else(|| "not given".into()) + }; + let title = neutralise(&field("title").replace(['\n', '\r'], " ")); + let text = [ + format!("**What I did:**\n{}", field("did")), + format!("**What happened:**\n{}", field("happened")), + format!("**What I expected:**\n{}", field("expected")), + format!("**Horizon version:** {}", field("version")), + format!("**OS, CPU, GPU:** {}", field("system")), + ] + .join("\n"); + let by = fields + .get("name") + .and_then(Value::as_str) + .map(|n| neutralise(n).replace(['*', '_', '~', '|', '[', ']'], "")) + .filter(|n| !n.is_empty()) + .map(|n| format!(" by **{n}**")) + .unwrap_or_default(); + let body = format!( + "{}\n\nReported on playhorizon.dev{by}\n\nOpened by the bug form on playhorizon.dev/hub. The reporter has this issue's link; they don't get GitHub notifications.", + quote(&text) + ); + Issue { + title, + body, + labels: LABELS.iter().map(|l| (*l).to_owned()).collect(), + } +} + +/// Opens issues in `repo` (`owner/name`) with `token` (a fine-grained token with Issues: write on that repo +/// only). Errors never carry the token or GitHub's answer body. +pub fn github(repo: String, token: String) -> Box { + Box::new(move |issue: &Issue| { + let response = ureq::post(&format!("https://api.github.com/repos/{repo}/issues")) + .set("Authorization", &format!("Bearer {token}")) + .set("Accept", "application/vnd.github+json") + .set("X-GitHub-Api-Version", "2022-11-28") + .set("User-Agent", "horizon-telemetry-server") + .timeout(Duration::from_secs(20)) + .send_json(json!({ "title": issue.title, "body": issue.body, "labels": issue.labels })); + match response { + Ok(response) => response + .into_json::() + .ok() + .and_then(|v| v["html_url"].as_str().map(str::to_owned)) + .ok_or_else(|| "GitHub's answer had no issue URL".into()), + Err(ureq::Error::Status(code, _)) => Err(format!("GitHub answered {code}")), + Err(ureq::Error::Transport(e)) => Err(format!("could not reach GitHub: {}", e.kind())), + } + }) +} diff --git a/crates/horizon-telemetry-server/src/hub.rs b/crates/horizon-telemetry-server/src/hub.rs index ebf22610d..eb92cd692 100644 --- a/crates/horizon-telemetry-server/src/hub.rs +++ b/crates/horizon-telemetry-server/src/hub.rs @@ -1,4 +1,5 @@ -//! The hub's votes: a 👍 on a roadmap item at playhorizon.dev/hub. roadmap/README.md documents them for Kira and the website. +//! The hub's votes: a 👍 on a roadmap item at playhorizon.dev/hub, and the helpers the hub's other +//! endpoints (`intake`) share. roadmap/README.md documents them for Kira and the website. //! //! - `POST /hub/vote`: a 👍 on a roadmap item, one per browser (`voter`, a random id the page makes and //! keeps); `"up": false` takes it back. Answers the item's count. @@ -114,10 +115,12 @@ impl App { /// The hub's share of `maintain`: idle buckets are forgotten. pub(crate) fn forget_hub_buckets(&self) { - self.hub_buckets - .lock() - .unwrap_or_else(|e| e.into_inner()) - .retain(|_, (_, at)| at.elapsed() < Duration::from_secs(3600)); + for buckets in [&self.hub_buckets, &self.hub_post_buckets] { + buckets + .lock() + .unwrap_or_else(|e| e.into_inner()) + .retain(|_, (_, at)| at.elapsed() < Duration::from_secs(3600)); + } } } diff --git a/crates/horizon-telemetry-server/src/intake.rs b/crates/horizon-telemetry-server/src/intake.rs new file mode 100644 index 000000000..59ce57141 --- /dev/null +++ b/crates/horizon-telemetry-server/src/intake.rs @@ -0,0 +1,370 @@ +//! What players send from the hub (playhorizon.dev/hub) for the maintainers: bug reports and suggestions. +//! roadmap/README.md documents it for Kira and the website. +//! +//! - `POST /hub/report`: a bug report, opened at once as a GitHub issue (labels `bug`, `from-website`) in +//! the template Kira's `/report` uses (`github`). Kept and tried again by the upkeep if GitHub fails. +//! - `POST /hub/suggest`: a suggestion, into the queue. Never published: Kira relays it to the +//! maintainers' private channel. +//! - `GET /hub/suggestions?after=`: the queue for Kira, with the `HORIZON_TELEMETRY_HUB_TOKEN` bearer. +//! +//! Stored: reports and suggestions as their text, for `hub_keep_days`. Never an IP address. +use std::sync::Arc; + +use axum::extract::{Query, Request, State}; +use axum::http::{HeaderMap, HeaderValue, StatusCode, header}; +use axum::response::{IntoResponse, Response}; +use rusqlite::params; +use serde::Deserialize; +use serde_json::{Map, Value, json}; + +use crate::hub::{answer, item_id, json_body, refuse}; +use crate::store::Store; +use crate::{App, github, take, utc_now}; + +pub const SCHEMA: &str = " +CREATE TABLE IF NOT EXISTS hub_queue ( + id INTEGER PRIMARY KEY, kind TEXT NOT NULL, day INTEGER NOT NULL, created TEXT NOT NULL, + state TEXT NOT NULL DEFAULT 'new', issue TEXT NOT NULL DEFAULT '', body TEXT NOT NULL +); +CREATE INDEX IF NOT EXISTS hub_queue_kind ON hub_queue (kind, state, id); +"; + +/// Days the upkeep keeps trying to open a report's issue. +const RETRY_DAYS: u32 = 7; + +/// A field's rules: its name, whether it is needed, its length in characters, whether it may hold lines. +struct Field(&'static str, bool, std::ops::RangeInclusive, bool); + +/// A bug report: the `#horizon-bugs` template's fields. +const REPORT: &[Field] = &[ + Field("title", true, 5..=120, false), + Field("did", true, 3..=2000, true), + Field("happened", true, 3..=2000, true), + Field("expected", true, 3..=2000, true), + Field("version", false, 0..=60, false), + Field("system", true, 3..=200, false), + Field("name", false, 0..=64, false), +]; +const SUGGESTION: &[Field] = &[ + Field("text", true, 10..=1000, true), + Field("item", false, 0..=48, false), + Field("name", false, 0..=64, false), +]; + +/// Text as stored: no control characters but line breaks (where allowed), trimmed. +fn clean(text: &str, lines: bool) -> String { + let text = text.replace("\r\n", "\n"); + let kept: String = text + .chars() + .map(|c| if !lines && c == '\n' { ' ' } else { c }) + .filter(|c| !c.is_control() || (lines && *c == '\n')) + .collect(); + kept.trim().to_owned() +} + +/// The request's fields checked against `rules`: the cleaned object, or what is wrong. +fn fields(body: &Value, rules: &[Field]) -> Result, String> { + let object = body.as_object().ok_or("send a JSON object")?; + let mut out = Map::new(); + for Field(name, needed, length, lines) in rules { + let text = match object.get(*name) { + None | Some(Value::Null) => String::new(), + Some(Value::String(s)) => clean(s, *lines), + Some(_) => return Err(format!("{name} is text")), + }; + if text.is_empty() && !needed { + continue; + } + if !length.contains(&text.chars().count()) { + return Err(format!( + "{name}: {} to {} characters", + length.start(), + length.end() + )); + } + out.insert((*name).to_owned(), Value::String(text)); + } + Ok(out) +} + +impl Store { + /// How many entries of `kind` came in on `day`. + pub fn queued_on(&self, kind: &str, day: u32) -> rusqlite::Result { + self.db.query_row( + "SELECT count(*) FROM hub_queue WHERE kind = ?1 AND day = ?2", + params![kind, day], + |r| r.get(0), + ) + } + + /// Queues an entry: its id, and its issue when the same text already came in since `since` and was + /// opened (a double click, a form sent twice). + pub fn enqueue( + &mut self, + kind: &str, + day: u32, + since: u32, + body: &Map, + ) -> rusqlite::Result<(i64, Option)> { + let body = Value::Object(body.clone()).to_string(); + let same = self + .db + .prepare( + "SELECT id, issue FROM hub_queue WHERE kind = ?1 AND body = ?2 AND day >= ?3 LIMIT 1", + )? + .query_row(params![kind, body, since], |r| { + Ok((r.get::<_, i64>(0)?, r.get::<_, String>(1)?)) + }); + match same { + Ok((id, issue)) => return Ok((id, Some(issue).filter(|i| !i.is_empty()))), + Err(rusqlite::Error::QueryReturnedNoRows) => {} + Err(e) => return Err(e), + } + self.db.execute( + "INSERT INTO hub_queue (kind, day, created, body) VALUES (?1, ?2, ?3, ?4)", + params![kind, day, utc_now(), body], + )?; + Ok((self.db.last_insert_rowid(), None)) + } + + /// Suggestions after `after`, oldest first: `{id, text, item?, name?, created_at}`. + pub fn suggestions(&self, after: i64, limit: u32) -> rusqlite::Result> { + let mut statement = self.db.prepare( + "SELECT id, created, body FROM hub_queue WHERE kind = 'suggestion' AND id > ?1 ORDER BY id LIMIT ?2", + )?; + let rows = statement.query_map(params![after, limit], |r| { + let mut entry: Map = + serde_json::from_str(&r.get::<_, String>(2)?).unwrap_or_default(); + entry.insert("id".into(), r.get::<_, i64>(0)?.into()); + entry.insert("created_at".into(), r.get::<_, String>(1)?.into()); + Ok(Value::Object(entry)) + })?; + rows.collect() + } + + /// Reports whose issue isn't open yet, from `since` on: id and fields. + pub fn unfiled(&self, since: u32) -> rusqlite::Result)>> { + let mut statement = self.db.prepare( + "SELECT id, body FROM hub_queue WHERE kind = 'report' AND state = 'new' AND day >= ?1 ORDER BY id", + )?; + let rows = statement.query_map([since], |r| { + Ok(( + r.get::<_, i64>(0)?, + serde_json::from_str(&r.get::<_, String>(1)?).unwrap_or_default(), + )) + })?; + rows.collect() + } + + /// Marks a report as opened: `issue` is its URL. + pub fn filed(&mut self, id: i64, issue: &str) -> rusqlite::Result<()> { + self.db.execute( + "UPDATE hub_queue SET state = 'filed', issue = ?2 WHERE id = ?1", + params![id, issue], + )?; + Ok(()) + } + + /// Reports from before `retry_from` that never got an issue are given up; entries before `oldest_day` + /// go. + pub fn expire_queue(&mut self, oldest_day: u32, retry_from: u32) -> rusqlite::Result { + self.db.execute( + "UPDATE hub_queue SET state = 'failed' WHERE kind = 'report' AND state = 'new' AND day < ?1", + [retry_from], + )?; + self.db + .execute("DELETE FROM hub_queue WHERE day < ?1", [oldest_day]) + } +} + +impl App { + /// The same for reports and suggestions, a much smaller bucket. + fn allow_post(&self, req: &Request) -> bool { + take( + &self.hub_post_buckets, + self.config.hub_posts_per_hour, + self.hashed(req), + ) + } + + /// Opens the reports whose issue isn't open yet (the upkeep calls it off the async workers; it blocks). + /// Stops at the first failure: GitHub is down or the token is wrong, the next round tries again. + pub fn file_pending(&self) { + let Some(filer) = &self.filer else { return }; + let since = (self.today)().saturating_sub(RETRY_DAYS); + let pending = self.store().unfiled(since).unwrap_or_default(); + for (id, fields) in pending { + match filer(&github::issue(&fields)) { + Ok(url) => self.mark_filed(id, &url), + Err(e) => { + eprintln!("horizon-telemetry-server: report {id} still not opened: {e}"); + return; + } + } + } + } + + fn mark_filed(&self, id: i64, url: &str) { + let marked = self.store().filed(id, url); + if let Err(e) = marked { + eprintln!("horizon-telemetry-server: report {id} opened as {url}, not marked: {e}"); + } + } + + /// The intake's share of `maintain`: entries past `hub_keep_days` go, reports never opened after + /// [`RETRY_DAYS`] are given up. + pub(crate) fn expire_intake(&self) -> rusqlite::Result { + let today = (self.today)(); + self.store().expire_queue( + today.saturating_sub(self.config.hub_keep_days), + today.saturating_sub(RETRY_DAYS), + ) + } +} + +/// What `receive` keeps: the entry's id, its issue if already opened, its fields. +type Received = (i64, Option, Map); + +/// Checks a report or a suggestion and queues it. +async fn receive( + app: &App, + req: Request, + kind: &str, + rules: &[Field], + per_day: u32, +) -> Result> { + let no = |code: StatusCode, message: &str| Box::new(refuse(app, code, message)); + if !app.allow_post(&req) { + return Err(no(StatusCode::TOO_MANY_REQUESTS, "too many requests")); + } + let body = json_body(req) + .await + .map_err(|e| no(StatusCode::BAD_REQUEST, e))?; + // The form's hidden field, which people leave empty and form-filling bots don't: thanked, dropped. + if body["website"].as_str().is_some_and(|s| !s.is_empty()) { + let thanks = answer(app, StatusCode::ACCEPTED, json!({ "id": 0 })); + return Err(Box::new(thanks)); + } + let entry = fields(&body, rules).map_err(|e| no(StatusCode::BAD_REQUEST, &e))?; + if let Some(item) = entry.get("item").and_then(Value::as_str) + && !item_id(item) + { + return Err(no(StatusCode::BAD_REQUEST, "item: a roadmap item id")); + } + let today = (app.today)(); + let mut store = app.store(); + if store.queued_on(kind, today).unwrap_or(u32::MAX) >= per_day { + let full = "too many today: please try again tomorrow"; + return Err(no(StatusCode::SERVICE_UNAVAILABLE, full)); + } + match store.enqueue(kind, today, today.saturating_sub(1), &entry) { + Ok((id, issue)) => Ok((id, issue, entry)), + Err(e) => { + eprintln!("horizon-telemetry-server: could not queue a {kind}: {e}"); + Err(no(StatusCode::SERVICE_UNAVAILABLE, "try again later")) + } + } +} + +/// `POST /hub/report` `{"title", "did", "happened", "expected", "version"?, "system", "name"?}`: 201 +/// `{"id", "issue"}` once the issue is open, 202 `{"id", "issue": null}` when GitHub failed and the upkeep +/// will try again; 503 when reports are off (no GitHub token). +pub async fn report(State(app): State>, req: Request) -> Response { + let Some(filer) = app.filer.clone() else { + return refuse( + &app, + StatusCode::SERVICE_UNAVAILABLE, + "reports are closed here: use GitHub or Discord", + ); + }; + let per_day = app.config.hub_reports_per_day; + let (id, issue, fields) = match receive(&app, req, "report", REPORT, per_day).await { + Ok(received) => received, + Err(response) => return *response, + }; + if let Some(issue) = issue { + return answer( + &app, + StatusCode::CREATED, + json!({ "id": id, "issue": issue }), + ); + } + let opened = tokio::task::spawn_blocking(move || filer(&github::issue(&fields))) + .await + .map_err(|e| e.to_string()) + .and_then(|opened| opened); + match opened { + Ok(url) => { + app.mark_filed(id, &url); + answer(&app, StatusCode::CREATED, json!({ "id": id, "issue": url })) + } + Err(e) => { + eprintln!("horizon-telemetry-server: report {id} not opened yet: {e}"); + answer( + &app, + StatusCode::ACCEPTED, + json!({ "id": id, "issue": null }), + ) + } + } +} + +/// `POST /hub/suggest` `{"text", "item"?, "name"?}`: 202 `{"id"}`. +pub async fn suggest(State(app): State>, req: Request) -> Response { + let per_day = app.config.hub_suggestions_per_day; + match receive(&app, req, "suggestion", SUGGESTION, per_day).await { + Ok((id, ..)) => answer(&app, StatusCode::ACCEPTED, json!({ "id": id })), + Err(response) => *response, + } +} + +#[derive(Deserialize)] +pub struct After { + after: Option, + limit: Option, +} + +/// `GET /hub/suggestions?after=&limit=100` with `Authorization: Bearer `: +/// `{"suggestions": [{"id", "text", "item"?, "name"?, "created_at"}]}`, oldest first. 404 when no token is +/// set, 401 for a wrong one. +pub async fn suggestions( + State(app): State>, + headers: HeaderMap, + Query(after): Query, +) -> Response { + let Some(token) = app.config.hub_token.as_deref() else { + return StatusCode::NOT_FOUND.into_response(); + }; + let sent = headers + .get(header::AUTHORIZATION) + .and_then(|v| v.to_str().ok()) + .and_then(|v| v.strip_prefix("Bearer ")) + .unwrap_or(""); + if !same(sent.as_bytes(), token.as_bytes()) { + return StatusCode::UNAUTHORIZED.into_response(); + } + let limit = after.limit.unwrap_or(100).clamp(1, 500); + let listed = app.store().suggestions(after.after.unwrap_or(0), limit); + match listed { + Ok(list) => ( + [ + ( + header::CONTENT_TYPE, + HeaderValue::from_static("application/json"), + ), + (header::CACHE_CONTROL, HeaderValue::from_static("no-store")), + ], + json!({ "suggestions": list }).to_string(), + ) + .into_response(), + Err(e) => { + eprintln!("horizon-telemetry-server: could not read the suggestions: {e}"); + StatusCode::SERVICE_UNAVAILABLE.into_response() + } + } +} + +/// Equal, in a time that doesn't depend on where they differ. +fn same(a: &[u8], b: &[u8]) -> bool { + a.len() == b.len() && a.iter().zip(b).fold(0, |diff, (x, y)| diff | (x ^ y)) == 0 +} diff --git a/crates/horizon-telemetry-server/src/lib.rs b/crates/horizon-telemetry-server/src/lib.rs index 9c273727c..3ab923969 100644 --- a/crates/horizon-telemetry-server/src/lib.rs +++ b/crates/horizon-telemetry-server/src/lib.rs @@ -7,7 +7,7 @@ //! least 3 installs only (`summary`), cached; the website's Benchmarks page reads it. //! - `POST /v1/live`: a game's anonymous "I'm playing" ping (`live`, in memory only). //! - `GET /v1/live`: how many play now, rounded, cached briefly. -//! - `POST /hub/vote`, `GET /hub/votes`: the hub's 👍 on roadmap items (`hub`). +//! - `/hub/…`: the hub's votes (`hub`), bug reports and suggestions (`intake`). //! - `GET /healthz`. //! //! Abuse limits: a token bucket per IP address (kept in memory for an hour, never stored), a @@ -29,11 +29,14 @@ use axum::routing::{delete, get, post}; use horizon_telemetry::batch; pub mod classes; +pub mod github; pub mod hub; +pub mod intake; pub mod live; pub mod store; pub mod summary; +use github::Filer; use horizon_telemetry::live::{MAX_PING_BYTES, Ping}; use live::Live; use store::Store; @@ -61,6 +64,20 @@ pub struct Config { pub live_max_sessions: usize, /// The hub's votes and reads of their counts an IP address may make an hour. pub hub_per_hour: f64, + /// Bug reports and suggestions together an IP address may send an hour. + pub hub_posts_per_hour: f64, + /// Bug reports taken a day, from everyone: each one opens a GitHub issue. + pub hub_reports_per_day: u32, + /// Suggestions taken a day, from everyone. + pub hub_suggestions_per_day: u32, + /// Days reports and suggestions are kept (Kira has relayed them long before). + pub hub_keep_days: u32, + /// The bearer token `GET /hub/suggestions` wants (Kira's); unset, it answers 404. + pub hub_token: Option, + /// Opens the website's bug reports as issues; unset, `POST /hub/report` answers 503. + pub github_token: Option, + /// Where those issues go, `owner/name`. + pub github_repo: String, } impl Default for Config { @@ -76,6 +93,13 @@ impl Default for Config { live_window: horizon_telemetry::live::WINDOW, live_max_sessions: 200_000, hub_per_hour: 600.0, + hub_posts_per_hour: 20.0, + hub_reports_per_day: 30, + hub_suggestions_per_day: 200, + hub_keep_days: 90, + hub_token: None, + github_token: None, + github_repo: "ludovic111/horizon".into(), } } } @@ -83,9 +107,16 @@ impl Default for Config { impl Config { /// The defaults, with `HORIZON_TELEMETRY_RETENTION_DAYS`, `…_MIN_INSTALL_DAYS`, /// `…_REQUESTS_PER_HOUR`, `…_ITEMS_PER_DAY`, `…_TRUST_PROXY` (1), `…_ALLOW_ORIGIN`, - /// `…_LIVE_PER_HOUR`, `…_LIVE_WINDOW_SECONDS`, `…_LIVE_MAX_SESSIONS` and `…_HUB_PER_HOUR`. + /// `…_LIVE_PER_HOUR`, `…_LIVE_WINDOW_SECONDS`, `…_LIVE_MAX_SESSIONS`, `…_HUB_PER_HOUR`, + /// `…_HUB_POSTS_PER_HOUR`, `…_HUB_REPORTS_PER_DAY`, `…_HUB_SUGGESTIONS_PER_DAY`, `…_HUB_KEEP_DAYS`, + /// `…_HUB_TOKEN`, `…_GITHUB_TOKEN` and `…_GITHUB_REPO`. Empty tokens count as unset. pub fn from_env() -> Self { let var = |k: &str| std::env::var(format!("HORIZON_TELEMETRY_{k}")).ok(); + let secret = |k: &str| { + var(k) + .map(|v| v.trim().to_owned()) + .filter(|v| !v.is_empty()) + }; let d = Self::default(); Self { retention_days: var("RETENTION_DAYS") @@ -114,6 +145,21 @@ impl Config { hub_per_hour: var("HUB_PER_HOUR") .and_then(|v| v.parse().ok()) .unwrap_or(d.hub_per_hour), + hub_posts_per_hour: var("HUB_POSTS_PER_HOUR") + .and_then(|v| v.parse().ok()) + .unwrap_or(d.hub_posts_per_hour), + hub_reports_per_day: var("HUB_REPORTS_PER_DAY") + .and_then(|v| v.parse().ok()) + .unwrap_or(d.hub_reports_per_day), + hub_suggestions_per_day: var("HUB_SUGGESTIONS_PER_DAY") + .and_then(|v| v.parse().ok()) + .unwrap_or(d.hub_suggestions_per_day), + hub_keep_days: var("HUB_KEEP_DAYS") + .and_then(|v| v.parse().ok()) + .unwrap_or(d.hub_keep_days), + hub_token: secret("HUB_TOKEN"), + github_token: secret("GITHUB_TOKEN"), + github_repo: var("GITHUB_REPO").unwrap_or(d.github_repo), } } } @@ -139,8 +185,12 @@ pub struct App { hub_key: RandomState, /// The hub's votes and vote counts, per hashed address. hub_buckets: Mutex, + /// The hub's reports and suggestions, per hashed address. + hub_post_buckets: Mutex, /// `GET /hub/votes`'s answer and when it was made. votes_cache: Mutex>, + /// Opens bug reports as GitHub issues; None without a token. + filer: Option>, } type Buckets = HashMap; @@ -160,6 +210,20 @@ impl App { store: Store, config: Config, today: Box u32 + Send + Sync>, + ) -> Arc { + let filer = config + .github_token + .clone() + .map(|token| Arc::from(github::github(config.github_repo.clone(), token))); + Self::with_filer(store, config, today, filer) + } + + /// [`Self::with_clock`] with the hub's bug reports opened by `filer` (tests give their own). + pub fn with_filer( + store: Store, + config: Config, + today: Box u32 + Send + Sync>, + filer: Option>, ) -> Arc { let live = Live::new(config.live_window, config.live_max_sessions); Arc::new(Self { @@ -174,7 +238,9 @@ impl App { today, hub_key: RandomState::new(), hub_buckets: Mutex::default(), + hub_post_buckets: Mutex::default(), votes_cache: Mutex::default(), + filer, }) } @@ -235,10 +301,12 @@ impl App { } /// Daily upkeep: totals rebuilt, expired rows removed, idle rate-limit entries and live - /// sessions forgotten. + /// sessions forgotten, the hub's old reports and suggestions removed. Reports still waiting for + /// their issue are [`Self::file_pending`]'s. pub fn maintain(&self) -> rusqlite::Result { let oldest = (self.today)().saturating_sub(self.config.retention_days); let removed = self.store().expire(oldest)?; + self.expire_intake()?; self.forget_hub_buckets(); for buckets in [&self.buckets, &self.live_buckets] { buckets @@ -503,6 +571,12 @@ pub fn router(app: Arc) -> Router { .route("/v1/benchmarks/summary", get(benchmarks)) .route("/hub/vote", post(hub::vote).options(hub::preflight)) .route("/hub/votes", get(hub::votes)) + .route("/hub/report", post(intake::report).options(hub::preflight)) + .route( + "/hub/suggest", + post(intake::suggest).options(hub::preflight), + ) + .route("/hub/suggestions", get(intake::suggestions)) .route("/healthz", get(|| async { "ok" })) .with_state(app) } diff --git a/crates/horizon-telemetry-server/src/main.rs b/crates/horizon-telemetry-server/src/main.rs index 9b9c702c3..728846a0f 100644 --- a/crates/horizon-telemetry-server/src/main.rs +++ b/crates/horizon-telemetry-server/src/main.rs @@ -22,6 +22,9 @@ async fn main() { if let Err(e) = upkeep.maintain() { eprintln!("horizon-telemetry-server: upkeep failed: {e}"); } + // The hub's reports GitHub refused before: their issues, off the async workers. + let filing = upkeep.clone(); + let _ = tokio::task::spawn_blocking(move || filing.file_pending()).await; } }); let listener = tokio::net::TcpListener::bind(SocketAddr::from(([0, 0, 0, 0], port))) diff --git a/crates/horizon-telemetry-server/src/store.rs b/crates/horizon-telemetry-server/src/store.rs index d1137d197..9286dd36a 100644 --- a/crates/horizon-telemetry-server/src/store.rs +++ b/crates/horizon-telemetry-server/src/store.rs @@ -131,6 +131,7 @@ impl Store { let mut db = Connection::open(path)?; db.execute_batch(SCHEMA)?; db.execute_batch(crate::hub::SCHEMA)?; + db.execute_batch(crate::intake::SCHEMA)?; migrate(&mut db)?; Ok(Self { db }) } diff --git a/crates/horizon-telemetry-server/tests/github.rs b/crates/horizon-telemetry-server/tests/github.rs new file mode 100644 index 000000000..81f480b58 --- /dev/null +++ b/crates/horizon-telemetry-server/tests/github.rs @@ -0,0 +1,41 @@ +//! A website bug report as the issue GitHub gets: the template, and people's text made inert. +use horizon_telemetry_server::github::{issue, neutralise}; +use serde_json::{Map, Value, json}; + +#[test] +fn people_text_mentions_and_references_nothing() { + let text = + neutralise("@octocat see #12 and https://github.com/a/b/issues/3 `x` Fixes it"); + assert!(!text.contains("@o") && !text.contains("#1") && !text.contains("github.com/")); + assert!(!text.contains('<') && !text.contains('`')); + assert!(text.contains("F\u{200b}ixes")); + // Words that only start like a keyword stay as they are. + assert_eq!(neutralise("closet fixture"), "closet fixture"); +} + +#[test] +fn a_report_reads_as_the_template() { + let fields: Map = serde_json::from_value(json!({ + "title": "Water is black\non AMD", "did": "Opened a world", "happened": "Black water", + "expected": "Blue water", "system": "Windows 11, RX 6600", "name": "steve_" + })) + .unwrap(); + let issue = issue(&fields); + assert_eq!(issue.title, "Water is black on AMD"); + assert!( + issue + .body + .starts_with("> **What I did:**\n> Opened a world\n> **What happened:**") + ); + assert!( + issue + .body + .contains("> **Horizon version:** not given\n> **OS, CPU, GPU:** Windows 11, RX 6600") + ); + assert!( + issue + .body + .contains("Reported on playhorizon.dev by **steve**") + ); + assert_eq!(issue.labels, ["bug", "from-website"]); +} diff --git a/crates/horizon-telemetry-server/tests/hub.rs b/crates/horizon-telemetry-server/tests/hub.rs index 10ddc12c4..056978212 100644 --- a/crates/horizon-telemetry-server/tests/hub.rs +++ b/crates/horizon-telemetry-server/tests/hub.rs @@ -1,8 +1,11 @@ -//! The hub's votes through the HTTP interface. -use std::sync::Arc; +//! The hub's intake through its HTTP interface: votes, bug reports (opened as issues by a fake GitHub), +//! suggestions and the queue Kira reads. +use std::sync::atomic::{AtomicU32, Ordering}; +use std::sync::{Arc, Mutex}; use axum::body::Body; use axum::http::{Request, StatusCode}; +use horizon_telemetry_server::github::{Filer, Issue}; use horizon_telemetry_server::store::Store; use horizon_telemetry_server::{App, Config, router}; use http_body_util::BodyExt; @@ -12,17 +15,54 @@ use tower::ServiceExt; const DAY: u32 = 20_741; const VOTER: &str = "0b7c3c9e-3f0a-4d8e-9a39-4c1f5a6b7d21"; +/// The issues the fake GitHub opened, and whether it is up. +#[derive(Default)] +struct GitHub { + opened: Mutex>, + down: Mutex, +} + struct Hub { app: Arc, + github: Arc, + day: Arc, } fn hub(config: Config) -> Hub { - let app = App::with_clock(Store::open(":memory:").unwrap(), config, Box::new(|| DAY)); - Hub { app } + let github = Arc::new(GitHub::default()); + let fake = github.clone(); + let filer: Arc = Arc::new(move |issue: &Issue| { + if *fake.down.lock().unwrap() { + return Err("GitHub answered 502".into()); + } + let mut opened = fake.opened.lock().unwrap(); + opened.push(Issue { + title: issue.title.clone(), + body: issue.body.clone(), + labels: issue.labels.clone(), + }); + Ok(format!( + "https://github.com/ludovic111/horizon/issues/{}", + 1000 + opened.len() + )) + }); + let day = Arc::new(AtomicU32::new(DAY)); + let clock = day.clone(); + let app = App::with_filer( + Store::open(":memory:").unwrap(), + config, + Box::new(move || clock.load(Ordering::SeqCst)), + Some(filer), + ); + Hub { app, github, day } } fn config() -> Config { - Config::default() + Config { + hub_token: Some("kira-secret".into()), + hub_posts_per_hour: 1000.0, + ..Config::default() + } } impl Hub { @@ -43,12 +83,27 @@ impl Hub { .await } - async fn get(&self, path: &str) -> (StatusCode, Value) { - self.send(Request::get(path).body(Body::empty()).unwrap()) - .await + async fn get(&self, path: &str, token: Option<&str>) -> (StatusCode, Value) { + let mut req = Request::get(path); + if let Some(token) = token { + req = req.header("authorization", format!("Bearer {token}")); + } + self.send(req.body(Body::empty()).unwrap()).await } } +fn report(title: &str) -> Value { + json!({ + "title": title, + "did": "Opened a new world\nWalked to the sea", + "happened": "The water is black", + "expected": "Blue water, as in vanilla", + "version": "v0.2.57", + "system": "Windows 11, Ryzen 5 5600, RX 6600", + "website": "", + }) +} + #[tokio::test] async fn votes_count_once_per_browser_and_can_be_taken_back() { let hub = hub(config()); @@ -71,7 +126,7 @@ async fn votes_count_once_per_browser_and_can_be_taken_back() { 2 ); assert_eq!( - hub.get("/hub/votes").await, + hub.get("/hub/votes", None).await, (StatusCode::OK, json!({ "votes": { "smooth-frames": 2 } })) ); assert_eq!( @@ -79,7 +134,7 @@ async fn votes_count_once_per_browser_and_can_be_taken_back() { 1 ); assert_eq!( - hub.get("/hub/votes").await.1["votes"]["smooth-frames"], + hub.get("/hub/votes", None).await.1["votes"]["smooth-frames"], 1, "a vote drops the cached counts" ); @@ -92,12 +147,239 @@ async fn votes_count_once_per_browser_and_can_be_taken_back() { } } +#[tokio::test] +async fn a_report_becomes_an_issue_in_the_bug_template() { + let hub = hub(config()); + let (code, body) = hub + .post("/hub/report", report("Water is black on AMD")) + .await; + assert_eq!(code, StatusCode::CREATED); + assert_eq!( + body["issue"], + "https://github.com/ludovic111/horizon/issues/1001" + ); + let issue = hub.github.opened.lock().unwrap().remove(0); + assert_eq!(issue.title, "Water is black on AMD"); + assert_eq!(issue.labels, ["bug", "from-website"]); + for line in [ + "> **What I did:**\n> Opened a new world\n> Walked to the sea", + "> **What happened:**\n> The water is black", + "> **What I expected:**\n> Blue water, as in vanilla", + "> **Horizon version:** v0.2.57", + "> **OS, CPU, GPU:** Windows 11, Ryzen 5 5600, RX 6600", + "Reported on playhorizon.dev", + ] { + assert!(issue.body.contains(line), "{line}\n---\n{}", issue.body); + } + // The same report again (a double click) answers the same issue and opens nothing. + let (code, again) = hub + .post("/hub/report", report("Water is black on AMD")) + .await; + assert_eq!( + (code, &again["issue"]), + (StatusCode::CREATED, &body["issue"]) + ); + assert!(hub.github.opened.lock().unwrap().is_empty()); +} + +#[tokio::test] +async fn reports_need_every_template_field_and_skip_bots() { + let hub = hub(config()); + for empty in ["title", "did", "happened", "expected", "system"] { + let mut body = report("Water is black on AMD"); + body[empty] = json!(" "); + let (code, answer) = hub.post("/hub/report", body).await; + assert_eq!(code, StatusCode::BAD_REQUEST, "{empty}"); + assert!(answer["error"].as_str().unwrap().starts_with(empty)); + } + let mut bot = report("Cheap watches"); + bot["website"] = json!("https://spam.example"); + assert_eq!( + hub.post("/hub/report", bot).await, + (StatusCode::ACCEPTED, json!({ "id": 0 })) + ); + assert!(hub.github.opened.lock().unwrap().is_empty()); +} + +#[tokio::test] +async fn a_report_github_refused_is_opened_by_the_upkeep() { + let hub = hub(config()); + *hub.github.down.lock().unwrap() = true; + let (code, body) = hub + .post("/hub/report", report("Crash when opening a chest")) + .await; + assert_eq!((code, &body["issue"]), (StatusCode::ACCEPTED, &Value::Null)); + hub.app.file_pending(); + assert_eq!(hub.github.opened.lock().unwrap().len(), 0); + *hub.github.down.lock().unwrap() = false; + hub.app.file_pending(); + assert_eq!(hub.github.opened.lock().unwrap().len(), 1); + hub.app.file_pending(); + assert_eq!(hub.github.opened.lock().unwrap().len(), 1, "opened once"); +} + +#[tokio::test] +async fn reports_are_closed_without_a_github_token() { + let app = App::with_clock( + Store::open(":memory:").unwrap(), + Config::default(), + Box::new(|| DAY), + ); + let res = router(app) + .oneshot( + Request::post("/hub/report") + .header("content-type", "application/json") + .body(Body::from(report("Water is black").to_string())) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(res.status(), StatusCode::SERVICE_UNAVAILABLE); +} + +#[tokio::test] +async fn suggestions_wait_in_the_queue_for_kira() { + let hub = hub(config()); + let first = json!({ "text": " Add a waypoint mod\r\nto the\u{7} browser ", "item": "mod-registry", "name": "alex", "extra": 1 }); + let (code, body) = hub.post("/hub/suggest", first).await; + assert_eq!(code, StatusCode::ACCEPTED); + let first_id = body["id"].as_i64().unwrap(); + hub.post( + "/hub/suggest", + json!({ "text": "Bigger render distance on the Steam Deck" }), + ) + .await; + assert_eq!( + hub.post("/hub/suggest", json!({ "text": "too short" })) + .await + .0, + StatusCode::BAD_REQUEST + ); + assert_eq!( + hub.post( + "/hub/suggest", + json!({ "text": "A long enough idea", "item": "Bad Id" }) + ) + .await + .0, + StatusCode::BAD_REQUEST + ); + + assert_eq!( + hub.get("/hub/suggestions", None).await.0, + StatusCode::UNAUTHORIZED + ); + assert_eq!( + hub.get("/hub/suggestions", Some("wrong-secret")).await.0, + StatusCode::UNAUTHORIZED + ); + let (code, all) = hub.get("/hub/suggestions", Some("kira-secret")).await; + assert_eq!(code, StatusCode::OK); + let list = all["suggestions"].as_array().unwrap(); + assert_eq!(list.len(), 2); + assert_eq!( + list[0]["text"], "Add a waypoint mod\nto the browser", + "trimmed, no control characters" + ); + assert!(list[0].get("extra").is_none()); + assert_eq!(list[0]["item"], "mod-registry"); + assert_eq!(list[0]["name"], "alex"); + assert!(list[0]["created_at"].as_str().unwrap().ends_with('Z')); + assert!(list[1].get("name").is_none()); + let (_, after) = hub + .get( + &format!("/hub/suggestions?after={first_id}"), + Some("kira-secret"), + ) + .await; + assert_eq!(after["suggestions"].as_array().unwrap().len(), 1); + + let closed = hub_without_token(); + assert_eq!( + closed.get("/hub/suggestions", Some("anything")).await.0, + StatusCode::NOT_FOUND + ); +} + +fn hub_without_token() -> Hub { + hub(Config::default()) +} + +#[tokio::test] +async fn posts_are_rate_limited_per_address_and_capped_per_day() { + let hub = hub(Config { + hub_posts_per_hour: 20.0, + hub_suggestions_per_day: 3, + ..config() + }); + // A burst of a tenth of the hourly rate: two, then refused. + let idea = |n: u32| json!({ "text": format!("Idea number {n}, long enough") }); + assert_eq!( + hub.post("/hub/suggest", idea(1)).await.0, + StatusCode::ACCEPTED + ); + assert_eq!( + hub.post("/hub/suggest", idea(2)).await.0, + StatusCode::ACCEPTED + ); + assert_eq!( + hub.post("/hub/suggest", idea(3)).await.0, + StatusCode::TOO_MANY_REQUESTS + ); + + let busy = self::hub(Config { + hub_posts_per_hour: 1000.0, + hub_suggestions_per_day: 3, + ..config() + }); + for n in 0..3 { + assert_eq!( + busy.post("/hub/suggest", idea(n)).await.0, + StatusCode::ACCEPTED + ); + } + assert_eq!( + busy.post("/hub/suggest", idea(9)).await.0, + StatusCode::SERVICE_UNAVAILABLE + ); + busy.day.store(DAY + 1, Ordering::SeqCst); + assert_eq!( + busy.post("/hub/suggest", idea(9)).await.0, + StatusCode::ACCEPTED, + "a new day" + ); +} + +#[tokio::test] +async fn old_entries_go_and_reports_never_opened_are_given_up() { + let hub = hub(config()); + *hub.github.down.lock().unwrap() = true; + let (_, body) = hub + .post("/hub/report", report("Crash when opening a chest")) + .await; + assert!(body["id"].as_i64().unwrap() > 0); + hub.post("/hub/suggest", json!({ "text": "An idea from long ago" })) + .await; + hub.day.store(DAY + 8, Ordering::SeqCst); + hub.app.maintain().unwrap(); + *hub.github.down.lock().unwrap() = false; + hub.app.file_pending(); + assert!( + hub.github.opened.lock().unwrap().is_empty(), + "past a week a report is given up" + ); + hub.day.store(DAY + 91, Ordering::SeqCst); + hub.app.maintain().unwrap(); + let (_, left) = hub.get("/hub/suggestions", Some("kira-secret")).await; + assert!(left["suggestions"].as_array().unwrap().is_empty()); +} + #[tokio::test] async fn the_website_may_post_from_the_browser() { let hub = hub(config()); let res = router(hub.app.clone()) .oneshot( - Request::options("/hub/vote") + Request::options("/hub/report") .header("origin", "https://playhorizon.dev") .header("access-control-request-method", "POST") .body(Body::empty()) From 4a9ca2988732944ea794e23852132e02c2aec567 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 15:07:49 +0100 Subject: [PATCH 098/238] Website: the API portal at /docs/api, with Markdown tables The repository's docs/api/ (the portal, its surface pages and the generated mod and server references) is built into the docs section: /docs/api is its README and /docs/api/ each page, in two nav groups, "Build on Horizon" and "API reference". The pages are read from the checkout or from main on GitHub, like the README, so the site never holds a copy. Links between portal pages become the site's (and the build's link check covers them); other relative links go to the file on GitHub. The renderer gains tables: the "Ways in" table, the references and the other tables on the site (apis, performance) showed as raw pipes. A link whose label is a code span no longer throws. Caddy and the dev server route /docs/api/ and redirect /docs/api/ to /docs/api; Railway rebuilds when docs/api/ changes. /docs/mods links the Mod API, its reference and its ways in; the summary page is now "APIs at a glance". Co-Authored-By: Claude Opus 5.5 --- docs/api/README.md | 3 ++- website/Caddyfile | 4 ++++ website/README.md | 9 ++++++-- website/build.mjs | 34 +++++++++++++++++++++++------- website/docs/apis.md | 4 ++-- website/docs/mods.md | 2 +- website/docs/nav.json | 3 ++- website/railway.json | 2 +- website/serve.mjs | 6 +++++- website/src/docs.html | 2 +- website/src/markdown.js | 39 ++++++++++++++++++++++++++++++++--- website/src/markdown.test.mjs | 25 ++++++++++++++++++++++ website/src/style.css | 4 ++++ 13 files changed, 117 insertions(+), 20 deletions(-) create mode 100644 website/src/markdown.test.mjs diff --git a/docs/api/README.md b/docs/api/README.md index ad49ff609..ef3649105 100644 --- a/docs/api/README.md +++ b/docs/api/README.md @@ -51,5 +51,6 @@ Every page has the same parts: **Overview** (what you can build), **Guide** (whe **Examples**, **Stability** (the level of each part, per [stability.md](stability.md)), **Changelog** and **Not there yet** (what is planned, so nobody builds on a guess). -The website's summary for players and authors is [playhorizon.dev/docs/apis](https://playhorizon.dev/docs/apis) +The website shows these pages at [playhorizon.dev/docs/api](https://playhorizon.dev/docs/api), built from this +folder, and a summary for players and authors at [playhorizon.dev/docs/apis](https://playhorizon.dev/docs/apis) (`website/docs/apis.md`). diff --git a/website/Caddyfile b/website/Caddyfile index fb96e836d..4ef85d2fc 100644 --- a/website/Caddyfile +++ b/website/Caddyfile @@ -35,6 +35,10 @@ rewrite /benchmarks /benchmarks.html @doc path_regexp doc ^/docs/([a-z-]+)$ rewrite @doc /docs/{re.doc.1}.html + # The API portal: /docs/api is its index, /docs/api/ its pages. + redir /docs/api/ /docs/api permanent + @apidoc path_regexp apidoc ^/docs/api/([a-z-]+)$ + rewrite @apidoc /docs/api/{re.apidoc.1}.html # The registry's submission service (crates/horizon-mod-registry), over Railway's private network. Unset, the # form says it isn't open. handle /api/mods/* { diff --git a/website/README.md b/website/README.md index 8ced5a0aa..7339cf966 100644 --- a/website/README.md +++ b/website/README.md @@ -77,8 +77,13 @@ mark merged-but-unreleased things "next release". `{{HARNESSES}}` and `{{PERCENT the README. A line holding only `![caption](/media/...)` becomes a figure (the file must be in `public/media/`); links to other pages are `/docs/#heading`. The build checks every link between pages and every image, and fails with the list when one is broken. `/server` -redirects to `/docs/hosting`. The developer docs in the repository's `docs/` stay as they are and are linked from -the Contributing page. +redirects to `/docs/hosting`. + +The API portal is the repository's `docs/api/`, not a copy: `nav.json` lists its README as `api` (`/docs/api`) and +its pages as `api/` (`/docs/api/`), read from the checkout or from `main` on GitHub like the README. +Links between its pages become the site's, every other relative link goes to the file on GitHub, and the same link +check applies. Markdown tables render as tables. The other developer docs in `docs/` stay on GitHub and are linked +from the Contributing page. ## Run it locally diff --git a/website/build.mjs b/website/build.mjs index 12bba71cb..4045297fb 100644 --- a/website/build.mjs +++ b/website/build.mjs @@ -3,12 +3,13 @@ // docs/nav.json), plus public/ and fingerprinted CSS and JS. No dependencies. // // Railway builds only website/, so the README and links.json come from the repo when they -// are next to it (a local checkout) and from main on GitHub otherwise. The documentation lives in website/. The latest releases and +// are next to it (a local checkout) and from main on GitHub otherwise. The documentation lives in website/, except the +// API portal (docs/api/), read the same way as the README. The latest releases and // the contributors are baked in when GitHub answers, so the page reads well before (or without) JavaScript. import { readFile, writeFile, mkdir, rm, cp } from "node:fs/promises"; import { existsSync } from "node:fs"; import { createHash } from "node:crypto"; -import { join, dirname } from "node:path"; +import { join, dirname, posix } from "node:path"; import { fileURLToPath } from "node:url"; import { markdown, inline, section, escape } from "./src/markdown.js"; import { recentHtml, releasesPageHtml } from "./src/releases.js"; @@ -303,16 +304,35 @@ const values = { // --- The documentation --------------------------------------------------------------------- // website/docs/.md: "# Title", a one-paragraph lede, then the body. docs/nav.json orders them in groups. +// The API portal is the repository's docs/api/ (the pages and their generated references): nav.json lists it as +// `api` (its README, /docs/api) and `api/` (/docs/api/). Its links are written for GitHub, so links +// between its pages become the site's and every other relative link goes to the file on GitHub. const docsNav = JSON.parse(await readFile(join(site, "docs", "nav.json"), "utf8")); +const docSlugs = new Set(docsNav.flatMap((g) => g.pages)); +const apiSource = (slug) => `docs/api/${slug === "api" ? "README" : slug.slice(4)}.md`; +function apiLinks(text, from) { + return text.replace(/\]\(([^)\s]+)\)/g, (all, url) => { + if (url.startsWith("https://playhorizon.dev/")) return `](${url.slice(23)})`; + if (/^([a-z]+:|#|\/)/i.test(url)) return all; + const [file, id] = url.split("#"); + const path = posix.normalize(posix.join(posix.dirname(from), file)); + const page = path.match(/^docs\/api\/([\w-]+)\.md$/)?.[1]; + const slug = page === "README" ? "api" : page && `api/${page}`; + const to = docSlugs.has(slug) ? `/docs/${slug}` : `https://github.com/${REPO}/blob/main/${path}`; + return `](${to}${id ? `#${id}` : ""})`; + }); +} const docs = []; for (const { group, pages: slugs } of docsNav) { for (const slug of slugs) { - const file = `${slug}.md`; - const text = (await readFile(join(site, "docs", file), "utf8")).replace(/\r\n?/g, "\n"); + const api = slug === "api" || slug.startsWith("api/"); + const file = api ? apiSource(slug) : `website/docs/${slug}.md`; + let text = (api ? (await repoFile(file))[0] : await readFile(join(site, "docs", `${slug}.md`), "utf8")).replace(/\r\n?/g, "\n"); + if (api) text = apiLinks(text.replace(/^\n/, ""), file); const m = text.match(/^# (.+)\n\n((?:[^\n]+\n)+)\n/); - if (!m) throw new Error(`docs/${file}: needs "# Title", a blank line, a lede paragraph and a blank line`); - docs.push({ slug, file, group, title: m[1].trim(), lede: m[2].trim().replace(/\n/g, " "), body: text.slice(m[0].length), path: slug === "index" ? "/docs/" : `/docs/${slug}` }); + if (!m) throw new Error(`${file}: needs "# Title", a blank line, a lede paragraph and a blank line`); + docs.push({ slug, file, group, title: m[1].replace(/`/g, "").trim(), lede: m[2].trim().replace(/\n/g, " "), body: text.slice(m[0].length), path: slug === "index" ? "/docs/" : `/docs/${slug}` }); } } const plain = (md) => md.replace(/`/g, "").replace(/\[([^\]]+)\]\([^)]*\)/g, "$1").replace(/\*\*/g, ""); @@ -352,7 +372,7 @@ const docNav = (current) => .join("\n"); const pagerLink = (d, rel) => (d ? `${rel === "next" ? "Next" : "Previous"}${escape(d.title)}` : ""); -await mkdir(join(dist, "docs"), { recursive: true }); +await mkdir(join(dist, "docs", "api"), { recursive: true }); const docPageFiles = []; const docPages = renderedDocs.map((d, i) => { const toc = d.headings.filter((h) => h.level === 2); diff --git a/website/docs/apis.md b/website/docs/apis.md index f948b1d54..fbed5fe53 100644 --- a/website/docs/apis.md +++ b/website/docs/apis.md @@ -1,4 +1,4 @@ -# Horizon's APIs +# APIs at a glance Horizon is built to be built on. Mods, shaders, agents, server panels, launchers' scripts and tools all get a documented API with a version, a stability level and a reference generated from the code, so it never says something the game doesn't do. @@ -13,7 +13,7 @@ Horizon is built to be built on. Mods, shaders, agents, server panels, launchers | **Launcher** | your agent, pack makers | `horizon-launcher`'s 46 commands and its MCP server | preview | | **Data formats** | tools that read Horizon's files | benchmark reports, telemetry, settings, release checksums; worlds are vanilla's format | per format | -The full pages, one per surface, are in the repository: [docs/api](https://github.com/ludovic111/horizon/tree/main/docs/api). Mods for players: [Mods](/docs/mods). Agents: [Control Horizon with your AI agent](/docs/agents). Hosting: [Host a server](/docs/hosting). +The full pages, one per surface, with their references: [the API portal](/docs/api), starting with [the Mod API](/docs/api/mods). Mods for players: [Mods](/docs/mods). Agents: [Control Horizon with your AI agent](/docs/agents). Hosting: [Host a server](/docs/hosting). ## What the levels mean diff --git a/website/docs/mods.md b/website/docs/mods.md index 0c125cf73..77a1734fd 100644 --- a/website/docs/mods.md +++ b/website/docs/mods.md @@ -30,7 +30,7 @@ cd my-mod cargo horizon-mod run ``` -`run` builds the mod and starts the game with it. After an edit, `cargo horizon-mod build` rebuilds it and the running game reloads it. The full guide, with the API, the manifest and the roadmap, is [docs/mods.md](https://github.com/ludovic111/horizon/blob/main/docs/mods.md). Java mod jars don't run in Horizon: their code calls and patches Minecraft's Java classes, and Horizon has none. What carries over is the data and assets inside a jar ([modpacks](https://github.com/ludovic111/horizon/blob/main/docs/modpacks.md)). +`run` builds the mod and starts the game with it. After an edit, `cargo horizon-mod build` rebuilds it and the running game reloads it. The [Mod API](/docs/api/mods) lists what a mod can do today, its [reference](/docs/api/mods-reference), the [ways in](/docs/api/mods#ways-in) (Rust today, Lua scripts planned) and what comes next; the full guide, with the manifest and the runtime, is [docs/mods.md](https://github.com/ludovic111/horizon/blob/main/docs/mods.md). Java mod jars don't run in Horizon: their code calls and patches Minecraft's Java classes, and Horizon has none. What carries over is the data and assets inside a jar ([modpacks](https://github.com/ludovic111/horizon/blob/main/docs/modpacks.md)). ## Finding resource packs and modpacks diff --git a/website/docs/nav.json b/website/docs/nav.json index 3bc2ec8d2..18eab69fd 100644 --- a/website/docs/nav.json +++ b/website/docs/nav.json @@ -3,6 +3,7 @@ { "group": "Playing", "pages": ["settings", "multiplayer", "troubleshooting"] }, { "group": "Hosting", "pages": ["hosting"] }, { "group": "Going further", "pages": ["modpacks", "mods", "performance", "agents"] }, - { "group": "Build on Horizon", "pages": ["apis"] }, + { "group": "Build on Horizon", "pages": ["apis", "api", "api/stability"] }, + { "group": "API reference", "pages": ["api/mods", "api/mods-reference", "api/mod-index", "api/server", "api/server-reference", "api/agent-control", "api/launcher", "api/data-formats", "api/shaders"] }, { "group": "Project", "pages": ["contributing", "faq"] } ] diff --git a/website/railway.json b/website/railway.json index 0f84cf952..3bbe9dfb3 100644 --- a/website/railway.json +++ b/website/railway.json @@ -3,7 +3,7 @@ "build": { "builder": "DOCKERFILE", "dockerfilePath": "Dockerfile", - "watchPatterns": ["/website/**", "/README.md", "/launcher/links.json"] + "watchPatterns": ["/website/**", "/README.md", "/launcher/links.json", "/docs/api/**"] }, "deploy": { "healthcheckPath": "/", diff --git a/website/serve.mjs b/website/serve.mjs index 761eef48a..6159474a0 100644 --- a/website/serve.mjs +++ b/website/serve.mjs @@ -11,6 +11,10 @@ const port = Number(process.env.PORT) || 4173; createServer(async (req, res) => { let path = normalize(decodeURIComponent(new URL(req.url, "http://x").pathname)).replace(/^(\.\.[/\\])+/, ""); + if (path === "/docs/api/") { + res.writeHead(301, { Location: "/docs/api" }); + return res.end(); + } if (path.endsWith("/")) path += "index.html"; // The server guide is part of the docs now, as the Caddyfile redirects it. if (path === "/server" || path === "/server.html") { @@ -18,7 +22,7 @@ createServer(async (req, res) => { return res.end(); } if (path === "/docs") path = "/docs/index.html"; - // /privacy and /docs/ are .html files, as the Caddyfile rewrites them. + // /privacy, /docs/ and /docs/api/ are .html files, as the Caddyfile rewrites them. if (!extname(path)) path += ".html"; let file = join(root, path); let status = 200; diff --git a/website/src/docs.html b/website/src/docs.html index 1277803b5..56ba89c09 100644 --- a/website/src/docs.html +++ b/website/src/docs.html @@ -46,7 +46,7 @@ -

    Edit this page on GitHub · Spotted something wrong? Open an issue or ask on Discord.

    +

    Edit this page on GitHub · Spotted something wrong? Open an issue or ask on Discord.

    Not an official Minecraft product. Not approved by or associated with Mojang or Microsoft.

    diff --git a/website/src/markdown.js b/website/src/markdown.js index 252e554ec..1735f7304 100644 --- a/website/src/markdown.js +++ b/website/src/markdown.js @@ -1,5 +1,5 @@ -// A small Markdown renderer for release notes and the README: headings, paragraphs, -// lists (nested by indentation), fenced code, quotes, rules, and inline code, bold, +// A small Markdown renderer for release notes, the README and the docs: headings, paragraphs, +// lists (nested by indentation), fenced code, quotes, rules, tables, and inline code, bold, // italic and links. Everything is escaped first, so its output never carries raw HTML. // Shared by build.mjs (Node) and main.js (browser). @@ -38,12 +38,41 @@ export function inline(text, site = false) { out = out.replace(/\u0001(\d+)\u0001/g, (_, i) => { const [label, url] = links[i]; const target = href(url, site); - const body = inline(label, site); + // The label's code spans are already placeholders: put them back for its own pass. + const body = inline(label.replace(/\u0000(\d+)\u0000/g, (_, c) => `\`${codes[c]}\``), site); return target ? `${body}` : body; }); return out.replace(/\u0000(\d+)\u0000/g, (_, i) => `${escape(codes[i])}`); } +// A table: a row of cells, a rule row (`| --- | :-: |`), then body rows, until a line that isn't a row. +const ROW = /^\s*\|.*\|\s*$/; +const RULE = /^\s*\|(\s*:?-+:?\s*\|)+\s*$/; + +/** A row's cells: split on `|` outside code spans; `\|` is a literal bar. */ +function cells(row) { + const s = row.trim().slice(1, -1); + const out = [""]; + let code = false; + for (let i = 0; i < s.length; i++) { + if (s[i] === "\\" && s[i + 1] === "|") out[out.length - 1] += s[++i]; + else if (s[i] === "|" && !code) out.push(""); + else { + if (s[i] === "`") code = !code; + out[out.length - 1] += s[i]; + } + } + return out.map((c) => c.trim()); +} + +function table(lines, start, site) { + const row = (line, tag) => `${cells(line).map((c) => `<${tag}>${inline(c, site)}`).join("")}`; + let i = start + 2; + const body = []; + while (i < lines.length && ROW.test(lines[i])) body.push(row(lines[i++], "td")); + return [`
    ${row(lines[start], "th")}${body.join("")}
    `, i]; +} + const indentOf = (line) => line.match(/^ */)[0].length; const ITEM = /^(\s*)([-*+]|\d+[.)])\s+(.*)$/; @@ -131,6 +160,10 @@ export function markdown(source, { shift = 0, site = false, headings = null } = } else if (/^\s*([-*_])(\s*\1){2,}\s*$/.test(line)) { out.push("
    "); i++; + } else if (ROW.test(line) && RULE.test(lines[i + 1] || "")) { + const [html, end] = table(lines, i, site); + out.push(html); + i = end; } else if (ITEM.test(line)) { const [html, end] = list(lines, i, site); out.push(html); diff --git a/website/src/markdown.test.mjs b/website/src/markdown.test.mjs new file mode 100644 index 000000000..171348e45 --- /dev/null +++ b/website/src/markdown.test.mjs @@ -0,0 +1,25 @@ +// node --test src/: the Markdown the docs pages and release notes are rendered from. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { markdown } from "./markdown.js"; + +test("a table renders with its header, cells and inline Markdown", () => { + const html = markdown("| Way in | Status |\n| --- | :-: |\n| Rust | `cargo horizon-mod` |\n| Lua | planned \\| soon |\n\nAfter."); + assert.equal( + html, + '
    ' + + "
    Way inStatus
    Rustcargo horizon-mod
    Luaplanned | soon
    \n

    After.

    ", + ); +}); + +test("a bar inside a code span stays in its cell", () => { + assert.match(markdown("| a | b |\n|---|---|\n| `x | y` | z |"), /x \| y<\/code><\/td>z<\/td>/); +}); + +test("rows without a rule line are a paragraph", () => { + assert.equal(markdown("| a | b |\n| c | d |"), "

    | a | b | | c | d |

    "); +}); + +test("a link whose label is code keeps it", () => { + assert.equal(markdown("See [`AGENTS.md`](/docs/agents).", { site: true }), '

    See AGENTS.md.

    '); +}); diff --git a/website/src/style.css b/website/src/style.css index 4ce91791d..0279c38aa 100644 --- a/website/src/style.css +++ b/website/src/style.css @@ -687,6 +687,10 @@ code { border-radius: 0; background: var(--well); box-shadow: 0 0 0 1px var(--ou .bench-table td.num { font-variant-numeric: tabular-nums; white-space: nowrap; } .bench-table td.wins { color: var(--ink); font-weight: 600; } .bench-table td.vs { font-size: var(--text-caption); white-space: nowrap; } +.md-table { width: 100%; border-collapse: collapse; font-size: var(--text-ui); } +.md-table th, .md-table td { padding: 8px 12px 8px 0; text-align: left; border-bottom: 1px solid var(--rule); vertical-align: baseline; } +.md-table th { font-weight: 500; color: var(--ink); } +.md-table td code, .md-table th code { overflow-wrap: anywhere; } .vs-horizon { color: var(--success); } .vs-java { color: var(--danger); } .vs-tie, .vs-none { color: var(--muted); } From c1ffa17c87bcef7d2d564644123c3142c169b0d3 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:14:12 +0100 Subject: [PATCH 099/238] Hub: vote for roadmap items, report a bug and suggest something from the page Each item still to come gets a "Want it" button with its count; the Contribute tab gets a bug report form (the #horizon-bugs template, which the telemetry server opens as a GitHub issue and links back) and a suggestion form (to the private queue Kira relays), and a Contributors list that shows the Discord name of those who agreed on Kira (/contributors.json). The forms and buttons appear once the hub's server answers; until then the page points to GitHub and Discord. roadmap/README.md documents the intake API that Kira shares. Co-Authored-By: Claude Opus 5.5 --- roadmap/README.md | 29 ++++++++- tools/roadmap.py | 3 +- website/build.mjs | 19 +++++- website/src/contributors.js | 2 +- website/src/hub-page.js | 120 ++++++++++++++++++++++++++++++++++++ website/src/hub.html | 43 ++++++++++++- website/src/hub.js | 41 +++++++++++- website/src/hub.test.mjs | 22 ++++++- website/src/style.css | 25 +++++++- 9 files changed, 293 insertions(+), 11 deletions(-) diff --git a/roadmap/README.md b/roadmap/README.md index 55cf9d09f..22cde1a7d 100644 --- a/roadmap/README.md +++ b/roadmap/README.md @@ -54,8 +54,8 @@ Whoever moves the work (the orchestrator on each heartbeat, or anyone in their p `2026-43.md`): `# Title`, a paragraph or two for players, then `## Technical` and the same week for contributors, with its pull requests. `build` puts it into website/data/roadmap.json; the hub's Journal tab shows it among the releases, and `/data/journal.json` carries it to Kira's #updates and to X. -6. Suggestions from the website and Discord are read and triaged by the maintainers. A good one becomes an - item in a normal pull request; none is published automatically. +6. Suggestions from the website and Discord reach the maintainers' private channel through Kira (below). A good + one becomes an item in a normal pull request; none is published automatically. CI runs `tests/test_roadmap.py` (in the Python tests), which fails when ROADMAP.md is not what `build` writes or when the file breaks a rule; `python3 tools/roadmap.py check --offline` runs the same rules without GitHub. @@ -89,3 +89,28 @@ merged pull request, release, shipped item and weekly summary, newest first, the - `announce: true` (the item's `announce` key) asks Kira to post the shipped item on X too, with `post`. - `https://playhorizon.dev/data/roadmap.json` is `roadmap.toml` as JSON (`areas`, `items` with every key above, `overview`, the weekly summaries); an item's page is `https://playhorizon.dev/hub#`. + +## The intake: votes, bug reports, suggestions + +The hub's forms and vote buttons talk to the telemetry server (`https://telemetry.playhorizon.dev`, +`crates/horizon-telemetry-server`, `src/hub.rs` and `src/intake.rs`; its README lists the limits and settings). +Kira uses the same endpoints, so a bug or an idea goes the same way from the site and from Discord. + +| Endpoint | Body | Answer | +| --- | --- | --- | +| `POST /hub/vote` | `{"item": "", "voter": "", "up": true}` | `{"item", "votes"}`; `"up": false` takes the vote back | +| `GET /hub/votes` | | `{"votes": {"": n}}`, a minute old at most | +| `POST /hub/report` | `{"title", "did", "happened", "expected", "version"?, "system", "name"?}` | 201 `{"id", "issue": ""}`; 202 `{"id", "issue": null}` when GitHub was down (opened later) | +| `POST /hub/suggest` | `{"text", "item"?, "name"?}` | 202 `{"id"}` | +| `GET /hub/suggestions?after=` | `Authorization: Bearer ` | `{"suggestions": [{"id", "text", "item"?, "name"?, "created_at"}]}`, oldest first | + +- A report becomes an issue at once, labels `bug` and `from-website`, in the `#horizon-bugs` template (What I did, + What happened, What I expected, Horizon version, OS, CPU, GPU) with "Reported on playhorizon.dev". Every field + but the version and the name is required; mentions, `#123` references and HTML in people's text are made inert. +- Suggestions are never published. Kira polls `/hub/suggestions` with the last id it relayed and posts each one + to `#roadmap-suggestions`. +- Errors are `{"error": "…"}`: 400 names the field (`"did: 3 to 2000 characters"`), 429 means too many from that + address, 503 a full day or reports turned off (no GitHub token on the server). +- The page shows the forms and the buttons only once `GET /hub/votes` answers. Until then it points to GitHub and + Discord. It keeps a random voter id and the items it voted for in `localStorage` (`hub:voter`, `hub:voted`). + diff --git a/tools/roadmap.py b/tools/roadmap.py index fe97285ff..4bc439e4d 100644 --- a/tools/roadmap.py +++ b/tools/roadmap.py @@ -56,7 +56,8 @@ ITEM_KEYS = {"id", "title", "status", "area", "plain", "dev", "links", "focus", ISSUE = re.compile(r"#(\d+)$") ID = re.compile(r"[a-z0-9]+(-[a-z0-9]+)*$") # An item is the anchor playhorizon.dev/hub#: it can't take the name of a tab, a section or a heading. -RESERVED = {"main", "overview", "roadmap", "board", "journal", "contribute", "hub-data", *STATUSES} +RESERVED = {"main", "overview", "roadmap", "board", "journal", "contribute", "report", "suggest", "contributors", + "hub-data", *STATUSES} RESERVED_PREFIXES = ("h-", "tab-", "col-", "week-", "journal-") RELEASE = re.compile(r"(v\d[\w.]*|launcher@\d[\w.]*|launcher-v\d[\w.]*|website)$") MONTHS = ("January|February|March|April|June|July|August|September|October|November|December") diff --git a/website/build.mjs b/website/build.mjs index c3203891e..b766b3d79 100644 --- a/website/build.mjs +++ b/website/build.mjs @@ -16,7 +16,7 @@ import { machinesHtml, playersHtml } from "./src/bench.js"; import { CPUS, GPUS, singleThread } from "./src/hardware.js"; import { isGame, isLauncher, launcherVersion, newestFirst } from "./src/versions.js"; import { contributorsFrom, contributorsHtml } from "./src/contributors.js"; -import { areaFilterHtml, boardFilterHtml, boardFrom, boardHtml, helpWantedHtml, journalFrom, journalHtml, journalJson, overviewHtml, roadmapHtml, slimIssue } from "./src/hub.js"; +import { areaFilterHtml, boardFilterHtml, boardFrom, boardHtml, creditFrom, creditHtml, helpWantedHtml, journalFrom, journalHtml, journalJson, overviewHtml, roadmapHtml, slimIssue, suggestItemsHtml } from "./src/hub.js"; const site = dirname(fileURLToPath(import.meta.url)); const dist = join(site, "dist"); @@ -119,6 +119,18 @@ const hubLinked = new Map(hubLive.filter((i) => linked.has(i.n)).map((i) => [i.n const board = boardFrom(hubLive, roadmap); const journal = journalFrom(releases, hubLive, roadmap); +// The hub's server (crates/horizon-telemetry-server: votes, bug reports, suggestions), and Kira's list of the +// contributors who linked their Discord account and agreed to be credited (kira's /contributors.json). +const hubApi = process.env.HUB_API_URL || "https://telemetry.playhorizon.dev"; +let kiraCredit = {}; +try { + const res = await fetch(process.env.KIRA_CONTRIBUTORS_URL || "https://kira-production-500e.up.railway.app/contributors.json", { signal: AbortSignal.timeout(10000) }); + if (!res.ok) throw new Error(`${res.status} from Kira`); + kiraCredit = await res.json(); +} catch (e) { + console.warn(`hub: contributors' Discord names: ${e.message}`); +} + // --- The parity table ------------------------------------------------------------------ const status = section(readme, "Project status") ?? ""; @@ -332,7 +344,10 @@ const values = { HUB_BOARD: hubLive.length ? boardHtml(board, roadmap) : `

    The board could not be loaded here.

    `, HUB_BOARD_FILTER: boardFilterHtml(board, roadmap), HUB_JOURNAL: journalHtml(journal, roadmap) || unavailable, - HUB_DATA: JSON.stringify({ roadmap, live: [...hubLinked.values()] }).replace(/ { + try { + return JSON.parse(localStorage.getItem(key)) ?? fallback; + } catch { + return fallback; + } +}; +const save = (key, value) => { + try { + localStorage.setItem(key, JSON.stringify(value)); + } catch {} +}; +const post = (path, body) => fetch(`${data.api}${path}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); + +// This browser's random id (one vote per item per browser) and the items it voted for. Nothing else identifies it. +function voter() { + let id = saved("hub:voter", ""); + if (typeof id !== "string" || id.length < 16) { + id = crypto.randomUUID ? crypto.randomUUID() : [...crypto.getRandomValues(new Uint8Array(16))].map((b) => b.toString(16).padStart(2, "0")).join(""); + save("hub:voter", id); + } + return id; +} +const mine = new Set(saved("hub:voted", [])); +let counts = null; // { item: votes } once the server answered; until then the buttons stay hidden + +function showVotes() { + if (!counts) return; + for (const button of document.querySelectorAll("[data-vote]")) { + const id = button.dataset.vote; + button.hidden = false; + button.setAttribute("aria-pressed", String(mine.has(id))); + button.querySelector(".hub-vote-label").textContent = mine.has(id) ? "Wanted" : "Want it"; + button.querySelector("[data-votes]").textContent = counts[id] ? String(counts[id]) : ""; + } +} + +async function vote(id) { + const up = !mine.has(id); + const apply = (on, step) => { + if (on) mine.add(id); + else mine.delete(id); + counts[id] = Math.max(0, (counts[id] || 0) + step); + save("hub:voted", [...mine]); + showVotes(); + }; + apply(up, up ? 1 : -1); + try { + const res = await post("/hub/vote", { item: id, voter: voter(), up }); + if (!res.ok) throw new Error(res.status); + counts[id] = (await res.json()).votes; + showVotes(); + } catch { + apply(!up, up ? -1 : 1); + } +} +document.addEventListener("click", (e) => { + const button = e.target.closest("[data-vote]"); + if (button && counts) vote(button.dataset.vote); +}); + +const LABELS = { title: "the title", did: "what you did", happened: "what happened", expected: "what you expected", version: "the version", system: "OS, CPU, GPU", text: "your idea", name: "your name" }; +// The server's answer in words: "did: 3 to 2000 characters" becomes "Check what you did: 3 to 2000 characters." +function trouble(code, answer) { + if (code === 429) return "Too many sent from your connection: try again in a few minutes."; + const said = typeof answer.error === "string" ? answer.error : ""; + const field = said.match(/^(\w+): (.+)$/); + if (code === 400 && field) return `Check ${LABELS[field[1]] || field[1]}: ${field[2]}.`; + return said ? `${said[0].toUpperCase()}${said.slice(1)}.` : "That didn't go through: try again in a moment."; +} + +function setUpForm(form, path, thanks) { + const status = form.querySelector("[data-status]"); + form.hidden = false; + form.parentElement.querySelector("[data-closed]").hidden = true; + form.addEventListener("submit", async (e) => { + e.preventDefault(); + if (!form.reportValidity()) return; + const button = form.querySelector('[type="submit"]'); + button.disabled = true; + status.textContent = "Sending…"; + try { + const res = await post(path, Object.fromEntries(new FormData(form))); + const answer = await res.json().catch(() => ({})); + if (res.ok) { + thanks(status, answer); + // A new report starts with the version filled in again; the name stays for the next one. + for (const field of form.querySelectorAll("textarea, input:not([name='version']):not([name='name'])")) field.value = ""; + } else status.textContent = trouble(res.status, answer); + } catch { + status.textContent = "Couldn't reach the server: try again, or use GitHub or Discord."; + } + button.disabled = false; + }); +} + +// The forms and the votes show once the server answers; without it the page points to GitHub and Discord. +async function connect() { + try { + // Revalidated: the server keeps the counts a minute, a browser's copy would miss this browser's own vote. + const res = await fetch(`${data.api}/hub/votes`, { cache: "no-cache" }); + if (!res.ok) return; + counts = (await res.json()).votes || {}; + } catch { + return; + } + showVotes(); + setUpForm(document.querySelector('[data-form="report"]'), "/hub/report", (status, answer) => { + const issue = typeof answer.issue === "string" && answer.issue.match(/^https:\/\/github\.com\/ludovic111\/horizon\/issues\/(\d+)$/); + status.innerHTML = issue ? `Thanks! It's issue #${issue[1]} on GitHub: follow it there.` : "Thanks! It will be on GitHub in a few minutes."; + }); + setUpForm(document.querySelector('[data-form="suggest"]'), "/hub/suggest", (status) => { + status.textContent = "Thanks! The maintainers read every suggestion."; + }); +} + setUpTheme(); setUpMenu(); +connect(); follow(); refresh().then(() => data.roadmap.items.some((i) => `#${i.id}` === location.hash) && follow()); diff --git a/website/src/hub.html b/website/src/hub.html index d4ba4600c..167ffc15e 100644 --- a/website/src/hub.html +++ b/website/src/hub.html @@ -90,7 +90,7 @@

    Take a task

    @@ -109,6 +109,41 @@
    +
    +

    Report a bug

    +

    One bug per report: something that breaks, or doesn't play like vanilla. It becomes an issue on GitHub, public, and you get its link to follow it. Search the open bugs first.

    + +

    Report it on GitHub or in #horizon-bugs on Discord, where Kira turns your post into an issue.

    +
    +
    +

    Suggest something

    +

    An idea for Horizon, or about something on the roadmap. The maintainers read every one, and the good ones become roadmap items. Nothing you send here is published.

    + +

    Post it in #horizon-ideas on Discord, or send it to Kira with /suggest.

    +

    The rules

      @@ -119,6 +154,12 @@

    The contributing guideCONTRIBUTING.md

    +
    +

    Contributors

    +

    Everyone with commits in Horizon. On Discord, /link-github <your login> credit:True adds your Discord name next to yours.

    +
      {{HUB_CREDIT}}
    +

    All contributors on GitHub

    +

    Not an official Minecraft product. Not approved by or associated with Mojang or Microsoft.

    diff --git a/website/src/hub.js b/website/src/hub.js index d293b7250..f11668d8c 100644 --- a/website/src/hub.js +++ b/website/src/hub.js @@ -6,6 +6,7 @@ import { escape, inline } from "./markdown.js"; import { parts, plainItem, sections } from "./releases.js"; import { isGame, isLauncher } from "./versions.js"; +import { avatar } from "./contributors.js"; export const REPO = "ludovic111/horizon"; const issueUrl = (n) => `https://github.com/${REPO}/issues/${n}`; @@ -85,11 +86,16 @@ export function progress(item, live) { const areaName = (roadmap, id) => roadmap.areas.find((a) => a.id === id)?.name || id; +// An item's vote button: hidden until the page has the counts from the hub's server (hub-page.js). +export function voteHtml(item) { + return ``; +} + export function itemHtml(item, roadmap, live) { const done = item.status === "shipped"; const state = done ? "" : progress(item, live); return `
    -

    ${escape(areaName(roadmap, item.area))}${done ? `${escape(item.shipped_in)}` : ""}${item.ongoing ? `Ongoing` : ""}

    +

    ${escape(areaName(roadmap, item.area))}${done ? `${escape(item.shipped_in)}` : ""}${item.ongoing ? `Ongoing` : ""}${done ? "" : voteHtml(item)}

    ${escape(item.title)}

    ${inline(item.plain)}

    ${devHtml(item)}

    @@ -160,7 +166,7 @@ export function overviewHtml(roadmap, live = new Map()) {

    How to help

    ${inline(o.help_plain)}

    ${inline(o.help_dev)}

    -

    ContributeHelp wanted

    +

    ContributeHelp wantedReport a bugSuggest something

    `; } @@ -418,3 +424,34 @@ export function journalHtml(entries, roadmap) { }) .join("\n"); } + +// --- Contribute: the suggestion form's items and the contributors ------------------------------------------------ + +// The suggestion form's "About" choices: the items still to come. +export function suggestItemsHtml(roadmap) { + const open = roadmap.items.filter((i) => i.status !== "shipped"); + return [``] + .concat(open.map((i) => ``)) + .join(""); +} + +// The contributors, from GitHub (contributors.js's list), with the Discord name of those who linked their accounts +// on Kira and agreed to be credited (Kira's /contributors.json: { contributors: [{ github, discord }] }). +export function creditFrom(github, kira) { + const discord = new Map( + (Array.isArray(kira?.contributors) ? kira.contributors : []) + .filter((c) => c && typeof c.github === "string" && typeof c.discord === "string") + .map((c) => [c.github.toLowerCase(), c.discord]), + ); + return github.map((c) => ({ ...c, discord: discord.get(c.login.toLowerCase()) || "" })); +} + +export function creditHtml(list) { + return list + .map((c) => { + const src = avatar(c.avatar_url); + const count = `${c.contributions.toLocaleString("en-US")} ${c.contributions === 1 ? "commit" : "commits"}`; + return `
  • ${src ? `` : ``}${escape(c.login)}${count}${c.discord ? ` · ${escape(c.discord)} on Discord` : ""}
  • `; + }) + .join(""); +} diff --git a/website/src/hub.test.mjs b/website/src/hub.test.mjs index d688489e9..4213d8a87 100644 --- a/website/src/hub.test.mjs +++ b/website/src/hub.test.mjs @@ -1,7 +1,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; -import { boardFrom, boardHtml, fixesIn, itemOf, journalFrom, journalHtml, journalJson, overviewHtml, progress, roadmapHtml, slimIssue } from "./hub.js"; +import { boardFrom, boardHtml, creditFrom, creditHtml, fixesIn, itemOf, journalFrom, journalHtml, journalJson, overviewHtml, progress, roadmapHtml, slimIssue, suggestItemsHtml } from "./hub.js"; const roadmap = JSON.parse(readFileSync(new URL("../data/roadmap.json", import.meta.url), "utf8")); @@ -137,3 +137,23 @@ test("journal.json is the flat feed Kira reads: merges, releases, shipped items, assert.match(e.url, /^https:\/\/(github\.com|playhorizon\.dev)\//, e.id); } }); + +test("items still to come take votes and suggestions; shipped ones don't", () => { + const html = roadmapHtml(roadmap); + for (const item of roadmap.items) { + assert.equal(html.includes(`data-vote="${item.id}"`), item.status !== "shipped", item.id); + assert.equal(suggestItemsHtml(roadmap).includes(`value="${item.id}"`), item.status !== "shipped", item.id); + } + // Hidden until the hub's server answers. + assert.ok(html.includes('aria-pressed="false" hidden>')); +}); + +test("the contributors carry the Discord name of those who agreed on Kira", () => { + const github = [{ login: "Alex", html_url: "https://github.com/Alex", avatar_url: "", contributions: 3 }, { login: "sam", html_url: "https://github.com/sam", avatar_url: "", contributions: 1 }]; + const kira = { contributors: [{ github: "alex", discord: "Alex " }, { github: 5 }, null] }; + const credit = creditFrom(github, kira); + assert.deepEqual(credit.map((c) => c.discord), ["Alex ", ""]); + const html = creditHtml(credit); + assert.ok(html.includes("3 commits · Alex <b> on Discord") && html.includes("1 commit")); + assert.deepEqual(creditFrom(github, "not json").map((c) => c.discord), ["", ""]); +}); diff --git a/website/src/style.css b/website/src/style.css index f32e24a30..1a23e79c8 100644 --- a/website/src/style.css +++ b/website/src/style.css @@ -645,7 +645,7 @@ code { border-radius: 0; background: var(--well); box-shadow: 0 0 0 1px var(--ou /* Releases: a filter, then every release as a panel that opens. */ .release-filter { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 32px; } -.release-filter .button[aria-pressed="true"], .hub-detail .button[aria-pressed="true"] { background: var(--face); color: var(--on-face); box-shadow: inset 0 var(--block-bevel) 0 var(--face-bevel), inset 0 calc(-1 * var(--block-lip)) 0 var(--face-lip); } +.release-filter .button[aria-pressed="true"], .hub-detail .button[aria-pressed="true"], .button.hub-vote[aria-pressed="true"] { background: var(--face); color: var(--on-face); box-shadow: inset 0 var(--block-bevel) 0 var(--face-bevel), inset 0 calc(-1 * var(--block-lip)) 0 var(--face-lip); } .release-count { margin: 16px 0 12px; } .release-list { display: grid; gap: 8px; } .release-list .release + .release { margin-top: 0; } @@ -826,3 +826,26 @@ code { border-radius: 0; background: var(--well); box-shadow: 0 0 0 1px var(--ou .hub-rows .row > a { grid-template-columns: var(--index) minmax(0, 1fr); } .hub-rows .row-meta { grid-column: 2; text-align: left; } } + +/* --- Hub: votes, the report and suggestion forms, the contributors ---------------------- */ +.hub-vote { margin-left: auto; } +.hub-vote-count { margin-left: -2px; } +.hub-vote-count:empty { display: none; } +.hub-form { display: grid; gap: 16px; max-width: 46rem; margin-top: 8px; } +.hub-form-pair { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; } +.hub-field { display: grid; gap: 6px; font-size: var(--text-label); color: var(--ink-soft); } +.hub-field input, .hub-field textarea, .hub-field select { + width: 100%; min-height: 36px; padding: 8px 10px; font: var(--text-ui) / 1.4 var(--font); color: var(--ink); + background: var(--well); border: 0; box-shadow: 0 0 0 var(--block-outline) var(--outline); +} +.hub-field textarea { resize: vertical; } +.hub-field input:focus-visible, .hub-field textarea:focus-visible, .hub-field select:focus-visible { outline: 1px solid var(--accent); outline-offset: 2px; } +.hub-field input::placeholder, .hub-field textarea::placeholder { color: var(--muted); } +.hub-trap { position: absolute; left: -10000px; width: 1px; height: 1px; overflow: hidden; } +.hub-form-foot { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px; } +.hub-form-foot [data-status] { color: var(--ink); } +.hub-form-foot .button:disabled { opacity: 0.4; cursor: default; } +.hub-form-closed { color: var(--ink-soft); } +@media (max-width: 720px) { + .hub-form-pair { grid-template-columns: minmax(0, 1fr); } +} From 818b9638ed1a737d8ef09c16a3108215fc1d588e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:15:16 +0100 Subject: [PATCH 100/238] CONTRIBUTING.md and docs/README.md point to the hub Where to start is playhorizon.dev/hub: the roadmap, the work board, good first tasks and the forms; roadmap PRs carry their roadmap: label, ideas go to the suggestion queue and issues are for bugs. Co-Authored-By: Claude Opus 5.5 --- CONTRIBUTING.md | 5 +++++ docs/README.md | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e6e959186..21951c083 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -7,6 +7,11 @@ welcome on [Discord](https://discord.gg/HcTeMZxe5E). This repository holds the game, the launcher and the website, plus the tools, tests and docs that build, check and release them. Anything else (videos, Discord and other community material) lives elsewhere. +**Where to start: [playhorizon.dev/hub](https://playhorizon.dev/hub).** It shows what is being done now and next +([ROADMAP.md](ROADMAP.md) in the repository), the work board, good first tasks, and the forms to report a bug or +suggest something. A pull request for a roadmap item carries its `roadmap:` label; ideas go to the suggestion +queue (the hub, or `/suggest` on Discord) rather than to issues, which are for bugs. + ## Build it You need Rust (stable), Python 3.10+, JDK 25, and on Linux the ALSA and udev headers (`libasound2-dev libudev-dev`). diff --git a/docs/README.md b/docs/README.md index 1450ba388..f946ee10b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ # Horizon documentation -To play, use [the launcher](../launcher/README.md); the project site is [playhorizon.dev](https://playhorizon.dev). These pages are for developers. +To play, use [the launcher](../launcher/README.md); the project site is [playhorizon.dev](https://playhorizon.dev). These pages are for developers. What is being done now and next, the work board and how to help: [the hub](https://playhorizon.dev/hub) ([ROADMAP.md](../ROADMAP.md), [roadmap/README.md](../roadmap/README.md) for keeping it current). ## Build and run From a6f951e126f2dc210aa278531790334ff655e10c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:22:19 +0100 Subject: [PATCH 101/238] Website: Make a mod, the full guide from the first line to the registry Six pages under a "Make a mod" group of the docs: set up and see a mod in the game (the launcher's Game arguments or cargo horizon-mod run), the manifest and every capability, the hooks and calls with a worked example (Playtime, built and checked), content mods, testing and check, and publishing (minisign signing, the submit form, review, updates). /mods, the submit page, the docs index, Mods and data packs and the Mod API link to it. Co-Authored-By: Claude Opus 5.5 --- docs/api/mods.md | 3 + website/build.mjs | 2 +- website/docs/content-mods.md | 114 ++++++++++++++++++++ website/docs/index.md | 1 + website/docs/make-a-mod.md | 88 ++++++++++++++++ website/docs/mod-code.md | 190 ++++++++++++++++++++++++++++++++++ website/docs/mod-manifest.md | 60 +++++++++++ website/docs/mods.md | 8 +- website/docs/nav.json | 1 + website/docs/publish-a-mod.md | 57 ++++++++++ website/docs/test-a-mod.md | 59 +++++++++++ website/src/mods.js | 1 + 12 files changed, 580 insertions(+), 4 deletions(-) create mode 100644 website/docs/content-mods.md create mode 100644 website/docs/make-a-mod.md create mode 100644 website/docs/mod-code.md create mode 100644 website/docs/mod-manifest.md create mode 100644 website/docs/publish-a-mod.md create mode 100644 website/docs/test-a-mod.md diff --git a/docs/api/mods.md b/docs/api/mods.md index bec03e84e..3e6108c70 100644 --- a/docs/api/mods.md +++ b/docs/api/mods.md @@ -24,6 +24,9 @@ the player before the mod is turned on, and checked against the component's real ## Guide +The step-by-step guide, from installing the tools to publishing on the registry, is +[Make a mod](https://playhorizon.dev/docs/make-a-mod) on the website. In short: + 1. `rustup target add wasm32-unknown-unknown`, `cargo install --git https://github.com/ludovic111/horizon cargo-horizon-mod`, then `cargo horizon-mod new my-mod` ([Making a mod](../mods.md#making-a-mod)). 2. Implement `Mod` for your type, list the capabilities you import in `horizon-mod.toml` diff --git a/website/build.mjs b/website/build.mjs index 4045297fb..d92f452ed 100644 --- a/website/build.mjs +++ b/website/build.mjs @@ -411,7 +411,7 @@ const modsTemplate = await readFile(join(site, "src", "mods.html"), "utf8"); const modsList = `

    ModsMade for Horizon, reviewed

    Horizon mods

    Mods, modpacks and resource packs made for Horizon. Each one was reviewed before it was listed, and the launcher checks every file before it installs it. Mods run in a sandbox and can only do what their page says.

    -

    Submit yours Make a mod

    +

    Submit yours Make a mod

    diff --git a/website/docs/content-mods.md b/website/docs/content-mods.md new file mode 100644 index 000000000..1ad8cd5fb --- /dev/null +++ b/website/docs/content-mods.md @@ -0,0 +1,114 @@ +# Content mods + +A content mod adds blocks and items to the game with data files only: no Rust, no compiler. Each new block or item copies a vanilla one (its template) and changes what differs, and comes with its recipes, loot table, tags, model, textures and name, in the same formats as vanilla's data packs and resource packs. + +## What a content mod is + +A folder (while you work on it) or a `.hmod` zip (to share it) holding a manifest, `assets/` and `data/`. This is the SDK's [`ruby-content`](https://github.com/ludovic111/horizon/tree/main/crates/horizon-mod-sdk/examples/ruby-content) example, a ruby gem and a block of ruby: + +``` +ruby/ +├── horizon-mod.toml +├── data/ruby/horizon/block/ruby_block.json the block: a template and what differs +├── data/ruby/horizon/item/ruby.json the item +├── data/ruby/recipe/ruby_block.json recipes, as in a data pack +├── data/ruby/recipe/ruby_from_ruby_block.json +├── data/ruby/loot_table/blocks/ruby_block.json what the block drops +├── data/minecraft/tags/block/mineable/pickaxe.json added to vanilla's tags +├── data/minecraft/tags/block/needs_iron_tool.json +├── assets/ruby/blockstates/ruby_block.json models and textures, as in a resource pack +├── assets/ruby/models/block/ruby_block.json +├── assets/ruby/models/item/ruby.json +├── assets/ruby/items/ruby.json +├── assets/ruby/items/ruby_block.json +├── assets/ruby/textures/block/ruby_block.png +├── assets/ruby/textures/item/ruby.png +└── assets/ruby/lang/en_us.json the names players see +``` + +Its manifest asks for API `0.2` and the `registries` capability. It has no code, so it needs no `Cargo.toml` and no `mod.wasm`: + +```toml +id = "ruby" +name = "Ruby" +version = "0.1.0" +description = "A ruby gem and a block of ruby" +authors = ["You"] +api = "0.2" +side = "both" +capabilities = ["registries"] +``` + +## Blocks + +A block is a file `data//horizon/block/.json`. It becomes the block `:`, with the template's behaviour, states and shapes: + +```json +{ + "template": "minecraft:iron_block", + "properties": { "hardness": 4.0, "light": 3, "sounds": "minecraft:amethyst_block", "map_color": 28 } +} +``` + +| Key | Meaning | +| --- | --- | +| `template` | The vanilla block it copies, such as `minecraft:iron_block`, `minecraft:oak_door` or `minecraft:diamond_ore`. A door copies a door's states and shape, an ore an ore's. | +| `properties.hardness` | How long it takes to mine (vanilla's destroy time; `-1` cannot be broken). | +| `properties.explosion_resistance` | How well it stands up to explosions. | +| `properties.light` | The light it gives, 0 to 15. | +| `properties.requires_tool` | `true`: it drops nothing unless mined with the right tool (the tags say which). | +| `properties.friction` | How slippery it is (ice is `0.98`). | +| `properties.map_color` | Its colour on maps, a vanilla map colour id from 0 to 61. | +| `properties.sounds` | A vanilla block whose sounds it uses. | +| `item` | `false` for a block with no item. By default the block gets an item of the same id. | + +Every property you leave out is the template's. + +## Items + +An item is a file `data//horizon/item/.json`, copying a vanilla item: + +```json +{ "template": "minecraft:diamond", "rarity": "uncommon" } +``` + +It may change `max_stack_size` (1 to 99) and `rarity` (`common`, `uncommon`, `rare` or `epic`, the colour of its name). + +## Recipes, loot, tags, models and names + +Everything else uses vanilla 26.3's own formats, so the [Minecraft Wiki's](https://minecraft.wiki/w/Data_pack) pages on data packs and resource packs apply: + +- **Recipes** in `data//recipe/`, **loot tables** in `data//loot_table/`. +- **Tags**: your own in `data//tags/`. To add to vanilla's (which pickaxe mines the block, for example), put a tag file in `data/minecraft/tags/`; to add to the convention tags NeoForge and Fabric mods share, in `data/c/tags/`. They add to the tag as a data pack does; `"replace": true` drops what came before it, and the log says so. +- **Block states, models, item models and textures** in `assets//`, as in a resource pack. Models can use vanilla's parents, such as `minecraft:block/cube_all`. +- **Names** in `assets//lang/en_us.json`: `"block.ruby.ruby_block": "Block of Ruby"`, `"item.ruby.ruby": "Ruby"`. + +**Structures.** A content mod can also carry structures, template pools and structure sets in its namespace, and add its own pieces to vanilla's villages or to another mod's pools. The [mod design page](https://github.com/ludovic111/horizon/blob/main/docs/mods.md#content-mods-api-02) has the details. + +## Namespaces + +Your files go under your mod's id (`assets/ruby/`, `data/ruby/`), plus vanilla's and the convention tags (`data/minecraft/tags/`, `data/c/tags/`). Tags are the only files a mod may put under `minecraft`. + +A mod made from a Java mod whose files use other names lists them in its manifest: `namespaces = ["kaisyn", "towns_and_towers"]`. A listed namespace is the mod's own, as its id is, but blocks and items are still declared under `data//horizon/`. `minecraft`, `c` and `horizon` can't be listed, and two mods that are on can't own the same namespace: the game keeps the first and logs the clash. + +## Limits + +- A mod's files use vanilla's resource-name characters: at most 65,536 files, 16 MiB each and 512 MiB in all. +- A file the game can't read (a block model that doesn't parse, a recipe with a typo) is logged and skipped, as vanilla does: the rest of the mod still loads. Read the log while you work. +- Unknown keys in a block or item file are refused, and the mod stays off with the reason on its row. +- Content mods work in single player today. Playing them together on a server is planned. + +## Try it and share it + +While you work, give the game the folder as a dev mod ([how](/docs/make-a-mod#see-it-in-the-game)): `--dev-mod "/path/to/ruby"`. The first start with a new set of mods takes a little longer while the game adds their content to its tables; after a change to the files, start the game again. + +To share it, zip the folder's contents, with `horizon-mod.toml` at the top of the zip, and name the file `.hmod`: + +```sh +cd ruby +zip -r ../ruby.hmod horizon-mod.toml assets data +``` + +Players put that file in their `horizon-mods` folder and turn it on in **Options, Mods...**. The registry doesn't take content-only mods yet: its checks (`cargo horizon-mod check`) expect code in the file. That is coming next. + +Next: [test it](/docs/test-a-mod). diff --git a/website/docs/index.md b/website/docs/index.md index 2b16c4db9..24f969840 100644 --- a/website/docs/index.md +++ b/website/docs/index.md @@ -30,4 +30,5 @@ There is no build for Intel Macs, 32-bit systems, or ARM Linux and Windows. The - **Playing:** [install and first launch](/docs/install), [settings worth knowing](/docs/settings), [multiplayer](/docs/multiplayer) and [what to do when something breaks](/docs/troubleshooting). - **Hosting:** [host a server](/docs/hosting), from the launcher or the command line. - **Going further:** [modpacks](/docs/modpacks), [mods and data packs](/docs/mods) and [performance](/docs/performance). +- **Making mods:** [make a mod](/docs/make-a-mod), from the first line of code to the registry, and the [Mod API](/docs/api/mods). - **Helping out:** [contribute](/docs/contributing) and the [FAQ](/docs/faq). diff --git a/website/docs/make-a-mod.md b/website/docs/make-a-mod.md new file mode 100644 index 000000000..94c3deca2 --- /dev/null +++ b/website/docs/make-a-mod.md @@ -0,0 +1,88 @@ +# Make a mod + +This guide takes you from nothing to a mod that other players can install from [playhorizon.dev/mods](/mods). You write the mod in Rust. It is compiled to WebAssembly and runs in a sandbox, so a player can see what it may do before turning it on. You need no Minecraft modding experience. + +## The path + +1. **Set up** (this page): install Rust and `cargo horizon-mod`, make your first mod and see it in the game. +2. **[The manifest and capabilities](/docs/mod-manifest)**: the file that names your mod and lists what it may do. +3. **[Write the code](/docs/mod-code)**: the hooks the game calls, what your code can call, and a worked example. +4. **[Content mods](/docs/content-mods)**: blocks, items, recipes and textures from data files, with no code at all. +5. **[Test it](/docs/test-a-mod)**: unit tests, the game's dev mode and the registry's own checks. +6. **[Publish it](/docs/publish-a-mod)**: package, sign and submit it to the registry. + +The [Mod API](/docs/api/mods) lists everything a mod can do today, and its [reference](/docs/api/mods-reference) has every function and type. + +## What a mod can do today + +- **Client mods** run in the player's game. They read what the F3 screen shows (position, facing, health, food, dimension, the time), draw a text panel on the screen, show chat lines only that player sees, add chat commands, and keep small files of their own. They work in single player and on any server, and a server cannot see them. +- **Content mods** add blocks and items copied from vanilla templates, with their recipes, loot tables, tags, models, textures and names. They need no code. +- **Server mods** (rules, world events, server commands) are being built. The [Mod API](/docs/api/mods#not-there-yet) says what comes next. + +A Horizon mod is not a Java mod. Fabric, NeoForge and Forge jars don't run in Horizon, and Horizon mods don't run in Java Minecraft. + +## Install the tools + +You need [Rust](https://rustup.rs) (the stable toolchain), its WebAssembly target and `cargo horizon-mod`, the tool that makes, builds and checks mods. On Windows, macOS or Linux: + +```sh +rustup target add wasm32-unknown-unknown +cargo install --git https://github.com/ludovic111/horizon cargo-horizon-mod +``` + +The second command builds the tool from source, which takes a few minutes the first time. Run it again later to update the tool. + +You also need Horizon itself, installed with the launcher ([Install](/docs/install)), and prepared at least once so the game has its files. + +## Make your first mod + +```sh +cargo horizon-mod new my-mod +cd my-mod +``` + +`new` makes a folder with a working mod in it: a `/hello` command that answers in chat. `--name "My Mod"` sets the name players see. The folder holds: + +``` +my-mod/ +├── Cargo.toml the Rust crate (a cdylib depending on horizon-mod-sdk) +├── horizon-mod.toml the manifest: id, name, version, what the mod may do +├── .gitignore +└── src/lib.rs the code +``` + +Build it: + +```sh +cargo horizon-mod build +``` + +This compiles the crate, checks that the code uses nothing its manifest doesn't declare, and writes `target/horizon-mod/my_mod.hmod`. A `.hmod` is a zip file holding the manifest and the compiled code (`mod.wasm`). It is the one file you give players: the same file runs on Windows, macOS and Linux. + +## See it in the game + +A **dev mod** is a mod the game loads from a path you give it. It is always on, and the game looks at its file once a second: build again and the game reloads it, saying "Mods reloaded" in chat. Its memory starts over; what it saved to its storage stays. + +**From the launcher.** Open the launcher's **Settings** and put this in **Game arguments**, with the full path to your file: + +``` +--dev-mod "/path/to/my-mod/target/horizon-mod/my_mod.hmod" +``` + +Then press **Play**, open a world and type `/hello world` in chat. Leave the setting there while you work on the mod, and clear it when you are done. The launcher's command line does the same: `horizon-launcher play --arg --dev-mod --arg /path/to/my_mod.hmod`. + +**With `cargo horizon-mod run`.** If you have a `horizon-client` you built from source, `cargo horizon-mod run` builds the mod and starts that game with it. It looks for `horizon-client` on your `PATH`, or takes `--game ` or the `HORIZON_CLIENT` variable; anything after `--` goes to the game: + +```sh +cargo horizon-mod run --game ../horizon/target/fast/horizon-client -- --resources ../horizon/.local/horizon-client/26.3 +``` + +Either way, edit `src/lib.rs`, run `cargo horizon-mod build`, and the game picks up the new file. + +## Where things go + +- **Your mod's messages** go to the game log as `[mod my_mod] …` (the `logs/latest.log` file in the game folder), and the game prints them in its terminal too. +- **Mods players install** go in the `horizon-mods` folder of the game folder (Linux `~/.local/share/horizon`, macOS `~/Library/Application Support/horizon`, Windows `%APPDATA%\horizon`). **Options, Mods...** in the game lists them, each with what it may do and a switch. They start off. +- **A mod that fails** to load, or that breaks while running, is switched off with the reason in red on its row of the Mods screen. It never takes the game down with it. + +Next: [the manifest and capabilities](/docs/mod-manifest). diff --git a/website/docs/mod-code.md b/website/docs/mod-code.md new file mode 100644 index 000000000..42cc6f04b --- /dev/null +++ b/website/docs/mod-code.md @@ -0,0 +1,190 @@ +# Write the code + +A mod is a Rust type that implements the `Mod` trait from `horizon-mod-sdk`. The game calls its methods, the hooks, at fixed points, and the mod calls back into the game through the parts of the API its capabilities allow. This page explains both, then builds a complete mod step by step. + +## The shape of a mod + +```rust +use horizon_mod_sdk::{Mod, Registration, chat}; + +struct Hello { + greeted: u32, +} + +impl Mod for Hello { + fn init() -> Result<(Self, Registration), String> { + Ok((Hello { greeted: 0 }, Registration::new().command("hello"))) + } + + fn command(&mut self, _name: &str, args: &str) -> Result, String> { + self.greeted += 1; + chat::show(&format!("Hello {args}! ({} so far)", self.greeted)); + Ok(None) + } +} + +horizon_mod_sdk::export_mod!(Hello); +``` + +- **The state lives in your type.** `init` creates it, and every hook gets `&mut self`. +- **`export_mod!`** connects the type to the game. A mod has exactly one. +- **The game calls only the hooks you ask for.** `init` returns a `Registration` that lists them. The other methods keep their default, empty bodies, and a hook you didn't ask for costs nothing. + +## Hooks + +| Hook | Ask for it with | When the game calls it | +| --- | --- | --- | +| `init() -> Result<(Self, Registration), String>` | always called | Once, when the mod loads. An `Err` keeps the mod off and shows the message on its row. | +| `tick(&mut self, tick: u64)` | `.on_tick()` | Every game tick while the player is in a world: 20 times a second. | +| `frame(&mut self, seconds: f32)` | `.on_frame()` | Every frame the game draws, with the time since the last one. | +| `command(&mut self, name: &str, args: &str)` | `.command("name")` | When the player types `/name …` in chat. `args` is everything after the name. | +| `chat(&mut self, text: &str)` | `.on_chat()` | For each chat line the player receives, as plain text. | + +**Commands.** Ask for as many as you like with `.command("…")`, without the slash. The chat box colours them and completes them with Tab, like vanilla's commands. Vanilla's names win: a mod can't take `/give`, and when two mods ask for the same name, the first one loaded keeps it. The game logs both cases. The return value says what happens next: + +- `Ok(None)`: nothing more. Use it when the mod already answered with `chat::show`. +- `Ok(Some(text))`: the game shows `text` to the player. +- `Err(text)`: the game shows `text` in red, as a failed command. + +**Prefer ticks to frames.** A tick happens 20 times a second whatever the frame rate. The HUD panel is kept by the game and drawn every frame for you, so most mods never need `frame`. + +## What your code can call + +Each module of the SDK is one part of the API, and each needs its capability in the manifest ([the list](/docs/mod-manifest#capabilities)): + +| Module | Capability | Calls | +| --- | --- | --- | +| `game` | `game-read` | `get_player() -> Option` (`x`, `y`, `z`, `yaw`, `pitch`, `health`, `food`, `dimension`), `get_clock() -> Option` (`game_time`, `day_time`). `None` outside a world. | +| `chat` | `chat` | `show(text)` | +| `hud` | `hud` | `set_panel(Corner, &[Line])` (`Line { text, color }`, colour as `0xRRGGBB`; corners `TopLeft`, `TopRight`, `BottomLeft`, `BottomRight`), `clear()` | +| `storage` | `storage` | `get(key) -> Option>`, `set(key, &[u8]) -> Result<(), String>`, `delete(key)` | +| `log` | none | `horizon_mod_sdk::info(text)` and `warn(text)`: lines in the game log | + +What the game reads for you (`game`) is a snapshot taken once a tick, and again before a command runs. It never changes in the middle of a call. Everything else is plain Rust: your own types, maths and text, and the crates you add to `Cargo.toml`, as long as they build for `wasm32-unknown-unknown` without the operating system (no files, threads, network or clock). + +## A worked example: Playtime + +This mod counts how long you play, shows it in the top-right corner, and keeps the count when you quit. `/playtime` says it in chat; `/playtime hide`, `show` and `reset` do what they say. It uses three capabilities (`hud`, `chat`, `storage`) and two hooks (`tick`, `command`). + +**1. Make the mod.** + +```sh +cargo horizon-mod new playtime --name "Playtime" +cd playtime +``` + +**2. Declare what it may do.** In `horizon-mod.toml`, set the description and the capabilities: + +```toml +description = "How long you have played, in a corner of the screen" +capabilities = ["hud", "chat", "storage"] +``` + +**3. The state and a helper.** Replace `src/lib.rs` with the code below, in parts. First the imports, the state and a function that turns ticks into hours and minutes. It touches no API, so an ordinary unit test can check it (step 7). + +```rust +use horizon_mod_sdk::{ + Mod, Registration, chat, + hud::{self, Corner, Line}, + storage, +}; + +/// Ticks played, kept across sessions in the mod's storage. +struct Playtime { + ticks: u64, + shown: bool, +} + +/// `1h 05m` from a number of ticks (20 a second). +fn format(ticks: u64) -> String { + let minutes = ticks / 20 / 60; + format!("{}h {:02}m", minutes / 60, minutes % 60) +} +``` + +**4. Saving and drawing.** `storage::set` writes bytes under a key of your choice; it can fail when the mod is over its quota, so the error goes to the log. The panel is set once and the game draws it until it changes, so redraw only when there is something new to show. + +```rust +impl Playtime { + fn save(&self) { + if let Err(e) = storage::set("ticks", &self.ticks.to_le_bytes()) { + horizon_mod_sdk::warn(&format!("could not save: {e}")); + } + } + + fn draw(&self) { + if self.shown { + let line = Line { text: format!("Played {}", format(self.ticks)), color: 0xFFFF55 }; + hud::set_panel(Corner::TopRight, &[line]); + } else { + hud::clear(); + } + } +} +``` + +**5. The hooks.** `init` reads what an earlier session saved, draws the panel and asks for one command and the tick hook. `tick` counts, and once a minute redraws and saves. `command` reads its arguments. + +```rust +impl Mod for Playtime { + fn init() -> Result<(Self, Registration), String> { + // What an earlier session saved, or 0 the first time. + let ticks = storage::get("ticks") + .and_then(|bytes| bytes.try_into().ok()) + .map_or(0, u64::from_le_bytes); + let state = Playtime { ticks, shown: true }; + state.draw(); + Ok((state, Registration::new().command("playtime").on_tick())) + } + + fn tick(&mut self, _tick: u64) { + self.ticks += 1; + // Once a minute: redraw the panel and save. + if self.ticks % (20 * 60) == 0 { + self.draw(); + self.save(); + } + } + + fn command(&mut self, _name: &str, args: &str) -> Result, String> { + match args.trim() { + "" => chat::show(&format!("You have played {}", format(self.ticks))), + "hide" | "show" => { + self.shown = args.trim() == "show"; + self.draw(); + } + "reset" => { + self.ticks = 0; + self.save(); + self.draw(); + return Ok(Some("Playtime reset".into())); + } + other => return Err(format!("Unknown option {other}: use hide, show or reset")), + } + Ok(None) + } +} + +horizon_mod_sdk::export_mod!(Playtime); +``` + +**6. Try it.** `cargo horizon-mod build`, then start the game with it as a dev mod ([how](/docs/make-a-mod#see-it-in-the-game)). Open a world: "Played 0h 00m" sits in the top-right corner and goes up each minute. Type `/playtime`, then `/playtime hide`. `/playtime foo` answers in red. Quit and come back: the count goes on from where it was. + +**7. Test the helper.** At the end of `src/lib.rs`: + +```rust +#[cfg(test)] +mod tests { + #[test] + fn formats_hours_and_minutes() { + assert_eq!(super::format(0), "0h 00m"); + assert_eq!(super::format(20 * 60 * 65), "1h 05m"); + } +} +``` + +`cargo test` runs it on your computer, with no game. [Test it](/docs/test-a-mod) covers the rest. + +**Where to go from here.** The SDK's examples are complete mods to read: [`coords-hud`](https://github.com/ludovic111/horizon/tree/main/crates/horizon-mod-sdk/examples/coords-hud) (coordinates, facing and the time on the HUD, `/where` and `/coords`) and [`ruby-content`](https://github.com/ludovic111/horizon/tree/main/crates/horizon-mod-sdk/examples/ruby-content) (a content mod). The [reference](/docs/api/mods-reference) lists every type and function, with its limits. + +Next: [content mods](/docs/content-mods), or straight to [testing](/docs/test-a-mod) if your mod has no blocks or items. diff --git a/website/docs/mod-manifest.md b/website/docs/mod-manifest.md new file mode 100644 index 000000000..c0e0e4b51 --- /dev/null +++ b/website/docs/mod-manifest.md @@ -0,0 +1,60 @@ +# The manifest and capabilities + +Every mod has a `horizon-mod.toml` at its root. It names the mod, says which version of the mod API it was made for, and lists its capabilities: what it may do. Players read that list before they turn the mod on, and the game holds the mod to it. + +## The manifest + +```toml +id = "playtime" +name = "Playtime" +version = "0.1.0" +description = "How long you have played, in a corner of the screen" +authors = ["You"] +api = "0.1" +side = "client" +capabilities = ["hud", "chat", "storage"] +``` + +| Key | What it is | +| --- | --- | +| `id` | The mod's id: 1 to 64 characters of `a-z`, `0-9` and `_`. It names the mod's setting, its log lines, its storage folder and, for content, its namespace. Keep it once you have published the mod. | +| `name` | The name players see. | +| `version` | Your mod's version, such as `0.1.0`. Each version you publish needs a new one. | +| `description` | One or two sentences for the Mods screen. | +| `authors` | A list of names. | +| `api` | The mod API line the mod was made for: `"0.1"` for code, `"0.2"` for content. The game refuses a line it doesn't serve, with a message that says so. | +| `side` | `"client"` for a mod that runs in the player's game. Content mods use `"both"`. | +| `capabilities` | What the mod may do: the list below. | +| `namespaces` | Content mods only: extra namespaces the mod's files use (see [Content mods](/docs/content-mods#namespaces)). | + +Unknown keys are refused, so a typo is an error, not a permission that silently does nothing. + +## Capabilities + +A capability is a part of the API the mod may use. Each one shows on the Mods screen as a sentence that starts with "May". Writing to the game log needs no capability. + +| Capability | The player reads | What the code can do | +| --- | --- | --- | +| `game-read` | May see your position, health and the time | `game::get_player()`: position, facing (yaw and pitch), health, food and dimension. `game::get_clock()`: the game time and the time of day. | +| `chat` | May show you chat messages | `chat::show(text)`: a chat line only this player sees, after the mod's name. At most 8 per call into the mod. | +| `hud` | May draw text on your screen | `hud::set_panel(corner, lines)` and `hud::clear()`: a panel of up to 16 coloured lines of 256 characters in one corner. The game keeps drawing it until you change it. | +| `storage` | May save its own settings to disk | `storage::get`, `set` and `delete`: small files in a folder of the mod's own. Up to 64 files and 4 MiB in all, and at most 1 MiB written per call. Keys are 1 to 64 characters of `a-z`, `0-9`, `_`, `.` and `-`. | +| `registries` | May add blocks and items to your worlds | Content mods: blocks and items from the mod's data files. No code. | + +Ask only for what the mod uses: players are more likely to turn on a mod that asks for little. + +**The game checks.** When you build, and again when a game loads the mod, the compiled code's imports are compared with the manifest. A mod whose code calls `chat::show` without `chat` in its capabilities is refused (`uses capability chat, missing from horizon-mod.toml`). The list players read is what the code can do, not a promise. + +## What no mod can do + +A mod runs in a WebAssembly sandbox. Whatever its capabilities: + +- It cannot open files (apart from its own storage), the network, other programs or the clock. +- It sees the game only through the snapshot the game gives it once a tick, and changes it only through the calls above. It cannot patch the game's code. +- It has 64 MiB of memory and a time budget for each call. A mod that runs past its budget, runs out of memory or crashes is switched off, with the reason on its row of the Mods screen. The game goes on. + +## API versions + +The mod API is versioned apart from the game. A mod made for API `0.1` keeps loading on every game that serves `0.1`, and new functions arrive as additions. A change that would break mods comes as a new line, served beside the old one for at least one release cycle. The [stability page](/docs/api/stability) has the whole policy, and the [Mod API's changelog](/docs/api/mods#changelog) what each line added. + +Next: [write the code](/docs/mod-code). diff --git a/website/docs/mods.md b/website/docs/mods.md index 77a1734fd..405c62976 100644 --- a/website/docs/mods.md +++ b/website/docs/mods.md @@ -6,7 +6,7 @@ Horizon has its own mod system, written for Horizon and not compatible with Java Horizon mods are written in Rust, compiled to WebAssembly and run in a sandbox. A mod can only do what it asks for and you allow, and a mod that crashes is switched off without taking the game down. -> **Early.** The mod system is new. There is no store for Horizon mods yet: they are files to put in a folder. The launcher's **Mods** page (see below) finds resource packs and modpacks, not `.hmod` mods. +> **Early.** The mod system is new. Reviewed mods are listed at [playhorizon.dev/mods](/mods), and any `.hmod` file can be put in the folder by hand. **Playing with mods** @@ -22,15 +22,17 @@ Horizon mods are written in Rust, compiled to WebAssembly and run in a sandbox. **Making a mod** +[Make a mod](/docs/make-a-mod) is the full guide: the tools, your first mod in the game, the manifest and capabilities, the code with a worked example, content mods, testing, and publishing on [the registry](/mods). In short: + ```sh rustup target add wasm32-unknown-unknown cargo install --git https://github.com/ludovic111/horizon cargo-horizon-mod cargo horizon-mod new my-mod cd my-mod -cargo horizon-mod run +cargo horizon-mod build ``` -`run` builds the mod and starts the game with it. After an edit, `cargo horizon-mod build` rebuilds it and the running game reloads it. The [Mod API](/docs/api/mods) lists what a mod can do today, its [reference](/docs/api/mods-reference), the [ways in](/docs/api/mods#ways-in) (Rust today, Lua scripts planned) and what comes next; the full guide, with the manifest and the runtime, is [docs/mods.md](https://github.com/ludovic111/horizon/blob/main/docs/mods.md). Java mod jars don't run in Horizon: their code calls and patches Minecraft's Java classes, and Horizon has none. What carries over is the data and assets inside a jar ([modpacks](https://github.com/ludovic111/horizon/blob/main/docs/modpacks.md)). +The [Mod API](/docs/api/mods) lists what a mod can do today and what comes next, and its [reference](/docs/api/mods-reference) every function. Java mod jars don't run in Horizon: their code calls and patches Minecraft's Java classes, and Horizon has none. What carries over is the data and assets inside a jar ([modpacks](/docs/modpacks)). ## Finding resource packs and modpacks diff --git a/website/docs/nav.json b/website/docs/nav.json index 18eab69fd..76c06dc47 100644 --- a/website/docs/nav.json +++ b/website/docs/nav.json @@ -3,6 +3,7 @@ { "group": "Playing", "pages": ["settings", "multiplayer", "troubleshooting"] }, { "group": "Hosting", "pages": ["hosting"] }, { "group": "Going further", "pages": ["modpacks", "mods", "performance", "agents"] }, + { "group": "Make a mod", "pages": ["make-a-mod", "mod-manifest", "mod-code", "content-mods", "test-a-mod", "publish-a-mod"] }, { "group": "Build on Horizon", "pages": ["apis", "api", "api/stability"] }, { "group": "API reference", "pages": ["api/mods", "api/mods-reference", "api/mod-index", "api/server", "api/server-reference", "api/agent-control", "api/launcher", "api/data-formats", "api/shaders"] }, { "group": "Project", "pages": ["contributing", "faq"] } diff --git a/website/docs/publish-a-mod.md b/website/docs/publish-a-mod.md new file mode 100644 index 000000000..c24a77a2f --- /dev/null +++ b/website/docs/publish-a-mod.md @@ -0,0 +1,57 @@ +# Publish a mod + +Horizon's registry lists mods, modpacks and resource packs made for Horizon. Players find them at [playhorizon.dev/mods](/mods) and on the launcher's Mods page, which checks every file before it installs it. Each project is reviewed by a person before it is published. This page takes your mod from a file on your computer to the registry. + +## Before you submit + +1. **Choose a version.** Set `version` in `horizon-mod.toml` (such as `1.0.0`). Every version you publish needs a new one, and a published file never changes. +2. **Build it for release.** `cargo horizon-mod build` (release is the default; `--debug` is for quick local builds) writes `target/horizon-mod/.hmod`. That file is what you submit. +3. **Check it.** `cargo horizon-mod check target/horizon-mod/.hmod` must pass ([Test a mod](/docs/test-a-mod#check-it-as-the-registry-does)). +4. **Publish the source.** Put the mod's source in a public repository, under a licence that lets players use it (MIT, Apache-2.0, GPL and so on, as an [SPDX](https://spdx.org/licenses/) name). Reviewers read it, and the file must be built from it. + +## Sign it + +Signing is optional and recommended. When your project has a public key, every later version must be signed with the matching secret key, so nobody else can publish an update in your name, even with access to your GitHub account. + +Horizon uses [minisign](https://jedisct1.github.io/minisign/) signatures. Install minisign (`brew install minisign`, `scoop install minisign`, or your Linux distribution's package), then make a key pair once: + +```sh +minisign -G -p horizon-mods.pub -s horizon-mods.key +``` + +Keep `horizon-mods.key` and its password private and backed up: losing it means changing the project's key by hand, through the registry's maintainers. The public key is the last line of `horizon-mods.pub` (it starts with `RW`). + +Sign each file you publish, and check the signature as the registry will: + +```sh +minisign -S -s horizon-mods.key -m target/horizon-mod/playtime.hmod +cargo horizon-mod check target/horizon-mod/playtime.hmod --key "RW…your public key…" --signature target/horizon-mod/playtime.hmod.minisig +``` + +The report then says "signature verified". + +## Submit it + +Open [playhorizon.dev/mods/submit](/mods/submit) and sign in with GitHub. The page reads only your public profile. Then fill in: + +- **The file**: the kind (mod), the `.hmod`, its id (the manifest's `id`), its version, the Minecraft version (26.3) and what changed. +- **Its page**: the name, a one-line summary for the list, a description (Markdown), the authors, the licence, the source address and, optionally, a square PNG icon (at most 256 KiB). +- **Signature**: your public key (for a new project) and the file's `.minisig`. + +**Submit for review** checks the file again and opens a pull request in [ludovic111/horizon-mods](https://github.com/ludovic111/horizon-mods). It adds `mods/.toml`, with you as the project's owner. The repository's checks run the file in the sandbox and post their report on the pull request. + +## Review and publication + +A reviewer reads the pull request, your source and the checks' report. Questions and requests for changes come as comments on the pull request: answer them there, and submit a new version from the form when the file must change. + +When the reviewer merges it, the file becomes a release of horizon-mods within minutes, and the project gets its page at `playhorizon.dev/mods/` and shows in the launcher. The page lists what the mod may do, as the game's Mods screen does, with your source, licence and the file's SHA-256. + +## Updates + +An update is the same form with a new version: it adds the version to your project's entry. The project's owners publish updates without a reviewer's extra look, and if the project has a key, the new file must be signed with it. + +Without the form, you can open the pull request by hand: the [horizon-mods README](https://github.com/ludovic111/horizon-mods) says how. The registry's file formats are in [the mod index reference](/docs/api/mod-index). + +## Reports and takedowns + +Anyone can report a project from its page (**Report it**). A project that does harm is taken down: the launcher then refuses its files and turns installed copies off, telling players why. diff --git a/website/docs/test-a-mod.md b/website/docs/test-a-mod.md new file mode 100644 index 000000000..650810963 --- /dev/null +++ b/website/docs/test-a-mod.md @@ -0,0 +1,59 @@ +# Test a mod + +Three kinds of checks catch most mistakes before players see them: unit tests for your own logic, the game's dev mode for how the mod looks and feels, and `cargo horizon-mod check`, which runs the same checks as the registry. + +## Unit tests for your logic + +Code that doesn't call the game (formatting, maths, parsing a command's arguments) is ordinary Rust. Put it in plain functions and test it with `#[test]`, as the [Playtime example](/docs/mod-code#a-worked-example-playtime) does with its `format` function: + +```sh +cargo test +``` + +The tests run on your computer, with no game and no WebAssembly. Keeping the hooks thin and the logic in functions makes most of a mod testable this way. + +## In the game, as a dev mod + +Start the game with your mod as a dev mod ([how](/docs/make-a-mod#see-it-in-the-game)) and keep it running. After each edit: + +```sh +cargo horizon-mod build +``` + +The game sees the new file within a second, says "Mods reloaded" in chat and starts the mod over. What it saved with `storage` stays. Things to try each time: + +- Every command, with no arguments, with good ones and with wrong ones. An `Err` should say what to type instead. +- Leaving the world and opening another: `game::get_player()` is `None` on the title screen. +- Quitting and starting again, for a mod that saves. +- **Options, Mods...**: the mod's row lists what it may do. If it says the mod stopped, the reason is there. + +The game log (`logs/latest.log` in the game folder) has your mod's `info` and `warn` lines as `[mod ]`, and the reason when the game refused or stopped the mod. + +**Why a mod stops.** A mod that panics, runs past its time budget for one call (an endless loop, for example) or uses more than 64 MiB of memory is switched off. The rest of the game carries on. Fix the cause and build again: a dev mod starts over with the new file. + +## Check it as the registry does + +Before you publish, run what the registry runs on every submission: + +```sh +cargo horizon-mod check target/horizon-mod/playtime.hmod +``` + +``` +playtime 0.1.0 (mod, API 0.1, client) + 12424 bytes, SHA-256 6561…ac13, unsigned + may draw text on your screen + may show you chat messages + may save its own settings to disk + trial run: started, 40 ticks, 120 frames, a chat line, /playtime: ok +``` + +It reads the manifest, refuses code that uses a capability the manifest doesn't list, then plays the mod in the sandbox, as the game does: 40 ticks with a moving player, 120 frames, a chat line and each of its commands once with no arguments. The mod must start and must never crash or overrun its budget. It fails with the reason when one of those goes wrong. + +- `--json` prints the report as JSON, for scripts. +- `--key --signature .minisig` also checks your signature ([Publish](/docs/publish-a-mod#sign-it)). +- It checks modpacks (`.hpack`) and resource packs (`.zip`) too; `--kind mod|modpack|resourcepack` when the file's extension doesn't say which. + +A mod that passes `check` passes the registry's automatic checks. A person still reviews it (see [Publish](/docs/publish-a-mod)). + +Next: [publish it](/docs/publish-a-mod). diff --git a/website/src/mods.js b/website/src/mods.js index 4819af374..a597e2847 100644 --- a/website/src/mods.js +++ b/website/src/mods.js @@ -121,6 +121,7 @@ export function submitHtml() { return `

    ModsSubmit

    Submit to the registry

    A mod, a modpack of registry mods, or a resource pack made for Horizon. The form checks the file and opens a pull request; a reviewer reads it, its source and the checks' report, and merging publishes it to this site and the launcher.

    +

    First time? Publish a mod walks through building, checking and signing the file, and Make a mod starts from the beginning.

    The form needs JavaScript. You can also open a pull request by hand.

    From 5dcbe222c94261c7291659a15a06549f46134014 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 15:00:44 +0100 Subject: [PATCH 102/238] Shader packs, clean room 37: shadow settings and the shadow camera The stage files' shadow constants (shadowMapResolution, shadowDistance, shadowDistanceRenderMul, shadowIntervalSize, sunPathRotation, shadowMapFov, shadowNearPlane, shadowFarPlane, shadowHardwareFiltering[0|1] and the shadowcolor0/1 Format, Clear and ClearColor) are read into Config::shadow (spec 6.3, 7.1). The shadow matrices are no longer a placeholder (OQ-6): the camera looks from the shadow light, sits 128 blocks toward it from a point that moves in steps of shadowIntervalSize along the shadow view's own axes, and projects orthographically over shadowDistance each side (or in perspective with shadowMapFov), between shadowNearPlane and shadowFarPlane. sunPathRotation turns the sun and moon's path about the world's X axis: sunPosition, moonPosition, shadowLightPosition and the shadow camera follow it, and so do the sun, moon and stars a pack's gbuffers_skytextured and gbuffers_skybasic draw. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, sections 6.3 and 7.1; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx/pack_post.rs | 6 +- crates/horizon-client/src/gfx/pack_sky.rs | 10 +- .../horizon-client/src/gfx/pack_uniforms.rs | 121 +++++++++++++--- crates/horizon-shaders/src/config.rs | 136 ++++++++++++++++++ crates/horizon-shaders/src/lib.rs | 2 +- 5 files changed, 252 insertions(+), 23 deletions(-) diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index 57c24b1ed..2be557c17 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -879,6 +879,7 @@ impl PackPost { }); let mut notes = post.notes; notes.extend(post.config.notes); + let shadow_config = post.config.shadow.clone(); let mut custom = post.custom; let custom_names: Vec = custom.uniforms().map(|u| u.0.to_owned()).collect(); // A first run finds the names neither the frame nor the pack defines. @@ -1279,6 +1280,7 @@ impl PackPost { uniforms: { let mut u = FrameUniforms::default(); u.atlas = world.atlas.map_or([0; 2], |a| a.1); + u.shadow = shadow_config; u }, } @@ -1495,7 +1497,9 @@ impl PackPost { let Some(mut sky) = self.sky.take() else { return; }; - let parts = sky.geometry.prepare(queue, frame); + let parts = sky + .geometry + .prepare(queue, frame, self.uniforms.shadow.sun_path_rotation); let mut groups = [None, None]; for (i, pass) in [&mut sky.basic, &mut sky.textured].into_iter().enumerate() { if let Some(pass) = pass { diff --git a/crates/horizon-client/src/gfx/pack_sky.rs b/crates/horizon-client/src/gfx/pack_sky.rs index 694f3096f..4823da4a4 100644 --- a/crates/horizon-client/src/gfx/pack_sky.rs +++ b/crates/horizon-client/src/gfx/pack_sky.rs @@ -255,8 +255,10 @@ impl PackSky { /// Writes the frame's sky and returns its draws in vanilla's order: the disc, the sunset /// fan while the sun is near the horizon, the sun and the moon, the stars and, with the - /// camera low, the void. - pub fn prepare(&self, queue: &wgpu::Queue, frame: &super::Frame) -> Vec { + /// camera low, the void. `tilt` is the pack's `sunPathRotation` (degrees): the sun, moon + /// and stars turn with it (7.1). + pub fn prepare(&self, queue: &wgpu::Queue, frame: &super::Frame, tilt: f32) -> Vec { + let turn = super::pack_uniforms::tilt_matrix(tilt); let sun_angle = frame.sky.sun_angle.to_radians(); // The fan turns to the other horizon in the afternoon (terrain.wgsl's sunrise_weight). let flip = if sun_angle.sin() < 0.0 { 180f32 } else { 0.0 }; @@ -264,7 +266,7 @@ impl PackSky { let (rotation, brightness) = frame.stars.unwrap_or_default(); let [r, g, b] = frame.sky.sky_color; let uniform = Uniform { - stars: rotation.to_cols_array_2d(), + stars: (turn * rotation).to_cols_array_2d(), color: [r, g, b, 1.0], sunrise: frame.sky.sunrise_color, sun: [theta.cos(), theta.sin(), brightness.clamp(0.0, 1.0), 0.0], @@ -281,7 +283,7 @@ impl PackSky { .clone() .flat_map(|q| [0, 1, 2, 0, 2, 3].map(|i| q[i])) .map(|v| Celestial { - pos: v.pos, + pos: turn.transform_vector3(v.pos.into()).into(), uv: v.uv, color: v.color, }) diff --git a/crates/horizon-client/src/gfx/pack_uniforms.rs b/crates/horizon-client/src/gfx/pack_uniforms.rs index efa8d2e2c..d1a85a1ba 100644 --- a/crates/horizon-client/src/gfx/pack_uniforms.rs +++ b/crates/horizon-client/src/gfx/pack_uniforms.rs @@ -3,7 +3,8 @@ //! Matrices follow GL's conventions (5.5): `gbufferProjection` maps view depth to clip z in //! [-w, w], and the depth buffers packs read hold GL window depth. -use glam::{Mat4, Vec3, Vec4}; +use glam::{DMat4, DVec3, Mat4, Vec3, Vec4}; +use horizon_shaders::Shadow; use super::Frame; @@ -172,6 +173,8 @@ pub struct FrameUniforms { previous: Option<(Vec3, Mat4, Mat4)>, wetness: f32, eye_smooth: [f32; 2], + /// The pack's shadow settings (7.1). + pub shadow: Shadow, } impl Default for FrameUniforms { @@ -185,14 +188,72 @@ impl Default for FrameUniforms { previous: None, wetness: 0.0, eye_smooth: [0.0; 2], + shadow: Shadow::default(), } } } -/// `(0, 100, 0)` on the frame the sky draws a body on: Y -90°, then X by its angle in degrees. -fn body(angle: f32) -> Vec3 { - (Mat4::from_rotation_y((-90.0f32).to_radians()) * Mat4::from_rotation_x(angle.to_radians())) - .transform_vector3(Vec3::new(0.0, 100.0, 0.0)) +/// `(0, 100, 0)` on the frame the sky draws a body on: Y -90°, then X by its angle in degrees; +/// `sunPathRotation` (`tilt`, degrees) then turns the path about the world's X axis (7.1, OQ-6). +fn body(angle: f32, tilt: f32) -> Vec3 { + (tilt_matrix(tilt) + * Mat4::from_rotation_y((-90.0f32).to_radians()) + * Mat4::from_rotation_x(angle.to_radians())) + .transform_vector3(Vec3::new(0.0, 100.0, 0.0)) +} + +/// `sunPathRotation`'s turn of the sky's bodies: `tilt` degrees about the world's X axis, so a +/// positive angle moves the noon sun toward +Z (south). +pub fn tilt_matrix(tilt: f32) -> Mat4 { + Mat4::from_rotation_x(tilt.to_radians()) +} + +/// The world direction of the shadow light (7.1): the sun by day, the moon by night. +fn shadow_light(frame: &Frame, shadow: &Shadow) -> Vec3 { + let sun_angle = (frame.sky.sun_angle / 360.0 + 0.25).rem_euclid(1.0); + let angle = if sun_angle < 0.5 { + frame.sky.sun_angle + } else { + frame.sky.moon_angle + }; + body(angle, shadow.sun_path_rotation).normalize() +} + +/// Distance from the snapped point to the shadow camera, toward the light: the middle of the +/// default near and far planes (OQ-6). +const SHADOW_EYE: f64 = 128.0; + +/// The shadow camera (7.1; OQ-6, impl-plan.md): its view (player space to shadow view space) +/// and GL projection. The camera looks from the light, world +Z up (+Y when the light is near +/// Z); the point it centres moves in steps of `shadowIntervalSize` along the shadow view's +/// own axes, and it sits `SHADOW_EYE` blocks toward the light from it. Orthographic over +/// `shadowDistance` each side, or perspective with `shadowMapFov`. +pub fn shadow_camera(light: Vec3, camera: DVec3, shadow: &Shadow) -> (Mat4, Mat4) { + let light = light.as_dvec3(); + let up = if light.z.abs() > 0.99 { + DVec3::Y + } else { + DVec3::Z + }; + let rotation = DMat4::look_to_rh(DVec3::ZERO, -light, up); + let at = rotation.transform_vector3(camera); + let step = f64::from(shadow.interval); + let snapped = if step > 0.0 { + (at / step).floor() * step + } else { + at + }; + let offset = at - snapped - DVec3::new(0.0, 0.0, SHADOW_EYE); + let view = (DMat4::from_translation(offset) * rotation).as_mat4(); + let (near, far) = (shadow.near, shadow.far); + let projection = match shadow.fov { + Some(fov) => Mat4::perspective_rh_gl(fov.to_radians(), 1.0, near, far), + None => { + let d = shadow.distance; + Mat4::orthographic_rh_gl(-d, d, -d, d, near, far) + } + }; + (view, projection) } /// `value` moved toward `target` with a half-life of `half_life` seconds over `dt` (5.6). @@ -223,8 +284,9 @@ impl FrameUniforms { let to_view = |v: Vec3| view.transform_vector3(v); let sun_angle = (frame.sky.sun_angle / 360.0 + 0.25).rem_euclid(1.0); let day = sun_angle < 0.5; - let sun = to_view(body(frame.sky.sun_angle)); - let moon = to_view(body(frame.sky.moon_angle)); + let tilt = self.shadow.sun_path_rotation; + let sun = to_view(body(frame.sky.sun_angle, tilt)); + let moon = to_view(body(frame.sky.moon_angle, tilt)); // Half-lives are given in ticks (7.2); smoothing runs per frame in seconds (OQ-26). let half_life = if inputs.rain > self.wetness { 600.0 @@ -250,14 +312,8 @@ impl FrameUniforms { } None => (frame.sky.fog_color, frame.distance * 0.9, frame.distance), }; - // The shadow camera (OQ-6, impl-plan.md): orthographic, `shadowDistance` (160) blocks - // each side, 100 blocks from the player toward the shadow light, world +z up. - let light = view - .inverse() - .transform_vector3(if day { sun } else { moon }) - .normalize(); - let shadow_view = Mat4::look_at_rh(light * 100.0, Vec3::ZERO, Vec3::Z); - let shadow_projection = Mat4::orthographic_rh_gl(-160.0, 160.0, -160.0, 160.0, 0.05, 256.0); + let light = shadow_light(frame, &self.shadow); + let (shadow_view, shadow_projection) = shadow_camera(light, frame.camera, &self.shadow); let day_time = inputs.day_time.rem_euclid(24000) as i32; let world_day = inputs.day_time.div_euclid(24000); let seconds = (now - self.start).as_secs_f32() % 3600.0; @@ -413,7 +469,38 @@ mod tests { #[test] fn noon_is_straight_up_and_the_sun_sets_in_the_west() { - assert!(body(0.0).abs_diff_eq(Vec3::new(0.0, 100.0, 0.0), 1e-3)); - assert!(body(90.0).abs_diff_eq(Vec3::new(-100.0, 0.0, 0.0), 1e-3)); + assert!(body(0.0, 0.0).abs_diff_eq(Vec3::new(0.0, 100.0, 0.0), 1e-3)); + assert!(body(90.0, 0.0).abs_diff_eq(Vec3::new(-100.0, 0.0, 0.0), 1e-3)); + // sunPathRotation tilts noon toward +Z and leaves sunset on the horizon. + assert!(body(0.0, 30.0).abs_diff_eq(Vec3::new(0.0, 86.60254, 50.0), 1e-3)); + assert!(body(90.0, 30.0).abs_diff_eq(Vec3::new(-100.0, 0.0, 0.0), 1e-3)); + } + + #[test] + fn the_shadow_camera_looks_down_from_the_light_in_steps() { + let shadow = Shadow::default(); + let mut camera = DVec3::new(1000.3, 70.0, -20.7); + // Noon: the light is straight up. + let (view, projection) = shadow_camera(Vec3::Y, camera, &shadow); + let at = |p: Vec3| view.transform_point3(p); + // Straight down: a block below the player is farther from the light. + assert!(at(Vec3::new(0.0, -1.0, 0.0)).z < at(Vec3::ZERO).z); + // The player is less than a step from the centre, 128 blocks from the light. + let centre = at(Vec3::ZERO); + assert!(centre.x.abs() <= 2.0 && centre.y.abs() <= 2.0, "{centre}"); + assert!((centre.z + 128.0).abs() < 1e-3, "{centre}"); + // Moving within a step keeps the world where it was in the map. + camera.x = 1000.6; + let (moved, _) = shadow_camera(Vec3::Y, camera, &shadow); + let world = Vec3::new(5.0, -3.0, 9.0); + let shift = Vec3::new(0.3, 0.0, 0.0); + assert!( + moved + .transform_point3(world - shift) + .abs_diff_eq(at(world), 1e-3) + ); + // shadowDistance blocks reach the map's edge. + let edge = projection.project_point3(Vec3::new(160.0, 0.0, -128.0)); + assert!((edge.x.abs() - 1.0).abs() < 1e-4, "{edge}"); } } diff --git a/crates/horizon-shaders/src/config.rs b/crates/horizon-shaders/src/config.rs index 0f21e4900..042840354 100644 --- a/crates/horizon-shaders/src/config.rs +++ b/crates/horizon-shaders/src/config.rs @@ -118,9 +118,55 @@ impl Default for Buffer { } } +/// The shadow map's settings (6.3, 7.1), from `const` lines of the stage files. +#[derive(Clone, Debug, PartialEq)] +pub struct Shadow { + /// `shadowMapResolution`: the map's side in pixels. + pub resolution: u32, + /// `shadowDistance`: half the side of the area the map covers, in blocks. + pub distance: f32, + /// `shadowDistanceRenderMul`: when > 0, geometry beyond `distance × mul` is not drawn. + pub render_mul: f32, + /// `shadowIntervalSize`: the shadow camera moves in steps of this many blocks (0: none). + pub interval: f32, + /// `sunPathRotation`, degrees. + pub sun_path_rotation: f32, + /// `shadowMapFov`: a perspective projection of this many degrees instead of orthographic. + pub fov: Option, + /// `shadowNearPlane`, `shadowFarPlane`. + pub near: f32, + pub far: f32, + /// `shadowHardwareFiltering(0|1)`: shadowtex0/1 sampled with depth comparison. + pub hardware_filtering: [bool; 2], + /// `shadowcolor0/1`: format, cleared every frame, clear colour (default white). + pub colors: [Buffer; 2], +} + +impl Default for Shadow { + fn default() -> Self { + let white = Buffer { + clear_color: Some([1.0; 4]), + ..Buffer::default() + }; + Self { + resolution: 1024, + distance: 160.0, + render_mul: -1.0, + interval: 2.0, + sun_path_rotation: 0.0, + fov: None, + near: 0.05, + far: 256.0, + hardware_filtering: [false; 2], + colors: [white.clone(), white], + } + } +} + #[derive(Clone, Debug, PartialEq)] pub struct Config { pub buffers: [Buffer; BUFFERS], + pub shadow: Shadow, /// Conflicting or unreadable settings, as `file:line: message`. pub notes: Vec, format_set: [bool; BUFFERS], @@ -133,6 +179,7 @@ impl Default for Config { fn default() -> Self { Self { buffers: std::array::from_fn(|_| Buffer::default()), + shadow: Shadow::default(), notes: Vec::new(), format_set: [false; BUFFERS], seen: Vec::new(), @@ -197,6 +244,9 @@ impl Config { } fn constant(&mut self, place: &str, ty: &str, name: &str, value: &str) { + if self.shadow_constant(ty, name, value) { + return; + } let settings = [ ("Format", "int"), ("ClearColor", "vec4"), @@ -243,6 +293,70 @@ impl Config { } } +impl Config { + /// A shadow setting of 6.3 or 7.1; true when `name` is one (read or not). + fn shadow_constant(&mut self, ty: &str, name: &str, value: &str) -> bool { + let s = &mut self.shadow; + let float = |v: &mut f32| { + if let Some(n) = number(value) { + *v = n; + } + }; + match name { + "shadowMapResolution" => { + if let Some(n) = number(value).filter(|n| *n >= 1.0) { + s.resolution = (n as u32).min(16384); + } + } + "shadowDistance" => float(&mut s.distance), + "shadowDistanceRenderMul" => float(&mut s.render_mul), + "shadowIntervalSize" => float(&mut s.interval), + "sunPathRotation" => float(&mut s.sun_path_rotation), + "shadowNearPlane" => float(&mut s.near), + "shadowFarPlane" => float(&mut s.far), + "shadowMapFov" => s.fov = number(value).filter(|f| *f > 0.0), + "shadowHardwareFiltering" => { + if let Some(b) = boolean(value) { + s.hardware_filtering = [b; 2]; + } + } + "shadowHardwareFiltering0" | "shadowHardwareFiltering1" => { + if let Some(b) = boolean(value) { + s.hardware_filtering[usize::from(name.ends_with('1'))] = b; + } + } + _ => { + let color = ["shadowcolor0", "shadowcolor1", "shadowcolor"] + .iter() + .find_map(|b| Some((b, name.strip_prefix(b)?))); + let Some((buffer, setting)) = color else { + return false; + }; + let b = &mut s.colors[usize::from(buffer.ends_with('1'))]; + match (setting, ty) { + ("Format", "int") => { + if let Some(f) = Format::parse(value) { + b.format = f; + } + } + ("Clear", "bool") => { + if let Some(v) = boolean(value) { + b.clear = v; + } + } + ("ClearColor", "vec4") => { + if let Some(c) = vec4(value) { + b.clear_color = Some(c); + } + } + _ => return false, + } + } + } + true + } +} + /// The buffers a fragment shader writes, from the last `DRAWBUFFERS` or `RENDERTARGETS` comment /// of its live code, or `None` without one (then it writes buffers 0 to 7). 6.1.4. pub fn draw_buffers(pp: &Preprocessed) -> Option> { @@ -402,6 +516,28 @@ mod tests { assert!(config.notes.is_empty(), "{:?}", config.notes); } + #[test] + fn shadow_settings() { + let mut c = Config::default(); + c.read(&stage( + "const int shadowMapResolution = 2048;\nconst float shadowDistance = 128.0f;\n\ + const float sunPathRotation = -40.0;\nconst bool shadowHardwareFiltering = true;\n\ + /* const int shadowcolor1Format = RGBA16F; */\nconst bool shadowcolor0Clear = false;\n", + )); + let s = c.finish().shadow; + assert_eq!( + (s.resolution, s.distance, s.sun_path_rotation), + (2048, 128.0, -40.0) + ); + assert_eq!( + (s.interval, s.fov, s.hardware_filtering), + (2.0, None, [true; 2]) + ); + assert!(!s.colors[0].clear && s.colors[1].clear); + assert_eq!(s.colors[1].format, Format::parse("RGBA16F").unwrap()); + assert_eq!(s.colors[1].clear_color, Some([1.0; 4])); + } + #[test] fn no_directive_and_conflicts() { let pp = stage("void main() {}\n"); diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index 426ee5b35..9411f506c 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -30,7 +30,7 @@ mod translate; pub use biomes::{category as biome_category, precipitation as biome_precipitation}; pub use block_ids::BlockIds; pub use condition::eval_condition; -pub use config::{BUFFERS, Buffer, Config, Format, Kind, buffer_index, draw_buffers}; +pub use config::{BUFFERS, Buffer, Config, Format, Kind, Shadow, buffer_index, draw_buffers}; pub use custom::{Custom, Val}; pub use discover::{Found, discover}; pub use lang::Lang; From 86d8d9e3a19a0c6614b6b4fec537007d33759254 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 15:06:22 +0100 Subject: [PATCH 103/238] Shader packs, clean room 38: load the shadow programs The shadow pass runs when some program reads a shadow buffer (shadow, watershadow, shadowtex0/1, their HW forms, shadowcolor, shadowcolor0/1) or shadow.enabled=true; shadow.enabled=false keeps it off (spec 2.5). Then `shadow` (solid and cutout terrain), `shadow_water` falling back to `shadow` (translucent terrain) and shadowcomp..shadowcomp99 load, in the new Shadow and ShadowComp groups (2.1, 2.4). shadow_solid and shadow_cutout are not used: the game's solid and cutout terrain is one draw. Their output n writes shadowcolor n (6.3): draw buffer lists stop at the first index past 1. translate_shadow gives a shadow program the shadow camera's matrices as its model-view and projection (5.2). The game does not draw any of them yet. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, sections 2.1, 2.4, 2.5, 5.2 and 6.3; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-shaders/src/lib.rs | 4 +- crates/horizon-shaders/src/post.rs | 180 +++++++++++++++++++- crates/horizon-shaders/src/translate/mod.rs | 45 ++++- crates/horizon-shaders/tests/real_packs.rs | 6 +- 4 files changed, 223 insertions(+), 12 deletions(-) diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index 9411f506c..4aa481e81 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -7,7 +7,7 @@ //! - [`Properties`]: `shaders.properties` and the id files, with their conditional directives (3.1, 1.5). //! - [`Lang`]: the pack's translated names (8.4). //! - [`preprocess`]: a stage file through `#include` and the GLSL preprocessor, lines mapped back (1.4, 1.5). -//! - [`translate_composite`], [`translate_gbuffers`]: a program into naga modules and its interface (1.6, 2.2). +//! - [`translate_composite`], [`translate_gbuffers`], [`translate_shadow`]: a program into naga modules and its interface (1.6, 2.2). //! - [`Config`] and [`draw_buffers`]: the buffer settings and output lists stage files carry (1.7, 6.1). //! - [`load_post`]: a dimension's `prepare`, `deferred`, `composite` and `final` programs, in order (2.1). //! - [`Macros`] and [`eval_condition`]: the macros packs test, and the `#if` arithmetic (1.5). @@ -45,5 +45,5 @@ pub use preprocess::{Diagnostic, Origin, Preprocessed, preprocess}; pub use properties::{Entry, Note, Properties}; pub use translate::{ AlphaTest, DRAW_UNIFORMS, Error, GbuffersInputs, Interface, Program, Sampler, Target, Uniform, - translate_composite, translate_gbuffers, + translate_composite, translate_gbuffers, translate_shadow, }; diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs index 9beb7f06a..95b8c3663 100644 --- a/crates/horizon-shaders/src/post.rs +++ b/crates/horizon-shaders/src/post.rs @@ -15,7 +15,9 @@ use std::collections::BTreeSet; use crate::config::{self, Config}; use crate::custom::Custom; use crate::{GbuffersInputs, Macros, Pack, Preprocessed, Program, Properties, Target}; -use crate::{eval_condition, preprocess, translate_composite, translate_gbuffers}; +use crate::{ + eval_condition, preprocess, translate_composite, translate_gbuffers, translate_shadow, +}; /// Which group of 2.1 a program belongs to. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -28,8 +30,32 @@ pub enum Group { Gbuffers, /// A gbuffers program drawing translucent geometry, after deferred (2.1). Translucent, + /// A shadow program: the game's geometry from the shadow light, into the shadow buffers. + Shadow, + /// `shadowcomp`..`shadowcomp99`: composite-style programs over the shadow buffers. + ShadowComp, } +impl Group { + /// Whether the group writes the shadow buffers, where buffer n is shadowcolor n. + pub fn shadow(self) -> bool { + matches!(self, Group::Shadow | Group::ShadowComp) + } +} + +/// The samplers whose use turns the shadow pass on (2.5, 6.3). +const SHADOW_SAMPLERS: [&str; 9] = [ + "shadow", + "watershadow", + "shadowtex0", + "shadowtex1", + "shadowtex0HW", + "shadowtex1HW", + "shadowcolor", + "shadowcolor0", + "shadowcolor1", +]; + /// A blend factor of `blend.` (3.2), as GL names it. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Factor { @@ -216,6 +242,14 @@ pub struct Post { pub weather: Option, /// What draws the first-person hand and held items. pub hand: Option, + /// Some program reads a shadow buffer, or `shadow.enabled=true`: the shadow pass runs (2.5). + pub shadow_pass: bool, + /// When it runs: what draws solid and cutout terrain from the light (`shadow`), and + /// translucent terrain (`shadow_water` or `shadow`), into the shadow buffers (2.4). + pub shadow: Option, + pub shadow_water: Option, + /// `shadowcomp`..`shadowcomp99`, after the shadow pass (2.1). + pub shadow_comp: Vec, /// Translucent entities draw after deferred (`separateEntityDraws`, 3.2). pub translucent_late: bool, /// Particles draw after deferred (`particles.ordering`, `particles.before.deferred`, 3.2). @@ -280,6 +314,10 @@ pub fn load_post( clouds: None, weather: None, hand: None, + shadow_pass: false, + shadow: None, + shadow_water: None, + shadow_comp: Vec::new(), translucent_late: false, particles_late: false, custom: Custom::parse(properties), @@ -434,10 +472,77 @@ pub fn load_post( Some(_) => true, None => !flag("particles.before.deferred").unwrap_or(false), }; + load_shadow(&mut loader, inputs.terrain.as_ref()); post.config = std::mem::take(&mut post.config).finish(); post } +/// Whether the shadow pass runs (2.5) and, when it does, its programs: `shadow_solid` and +/// `shadow_cutout` are not used (2.4, Legacy); the game's solid and cutout terrain is one draw. +fn load_shadow(loader: &mut Loader<'_>, terrain: Option<&GbuffersInputs>) { + let post = &*loader.post; + let gbuffers = [ + &post.terrain, + &post.water, + &post.sky, + &post.sky_textured, + &post.entities, + &post.block, + &post.entities_translucent, + &post.particles, + &post.clouds, + &post.weather, + &post.hand, + ]; + let reads = (post.programs.iter()) + .chain(gbuffers.into_iter().flatten()) + .flat_map(|p| &p.program.interface.samplers) + .any(|s| SHADOW_SAMPLERS.contains(&s.name.as_str())); + let forced = match loader.properties.get("shadow.enabled").map(str::trim) { + Some("true") => Some(true), + Some("false") => Some(false), + _ => None, + }; + let on = forced.unwrap_or(reads); + loader.post.shadow_pass = on; + if !on { + return; + } + let names = + std::iter::once("shadowcomp".to_owned()).chain((1..=99).map(|n| format!("shadowcomp{n}"))); + for name in names { + let program = loader.load(name, Group::ShadowComp, |v, f| { + translate_composite(v, f, Target::Buffers) + }); + loader.post.shadow_comp.extend(program); + } + let Some(inputs) = terrain else { + return; + }; + let properties = loader.properties; + let mut load = |names: &[&str]| { + names.iter().find_map(|name| { + let p = loader.load(name.to_string(), Group::Shadow, |v, f| { + translate_shadow(v, f, inputs) + }); + p.map(|mut p| { + p.blend = blend(properties, &p.name).flatten(); + p + }) + }) + }; + let shadow = load(&["shadow"]); + let water = load(&["shadow_water", "shadow"]); + if shadow.is_none() { + loader + .post + .notes + .push("shadow: no program; the shadow map stays clear".to_owned()); + } + loader.post.shadow = shadow; + loader.post.shadow_water = water; +} + struct Loader<'a> { pack: &'a Pack, folder: &'a str, @@ -505,9 +610,17 @@ impl Loader<'_> { }; self.post.config.read(&vertex); self.post.config.read(&fragment); - let buffers = config::draw_buffers(&fragment).unwrap_or_else(|| (0..8).collect()); + let mut buffers = config::draw_buffers(&fragment).unwrap_or_else(|| (0..8).collect()); + if group.shadow() { + // Output n writes shadowcolor n (6.3); there are two. + let kept = buffers + .iter() + .position(|&b| b >= 2) + .unwrap_or(buffers.len()); + buffers.truncate(kept); + } let mipmaps = match group { - Group::Gbuffers | Group::Translucent => 0, + Group::Gbuffers | Group::Translucent | Group::Shadow | Group::ShadowComp => 0, _ => { let mut own = Config::default(); own.read(&fragment); @@ -696,6 +809,67 @@ mod tests { assert!(post.programs.iter().any(|p| p.name == "composite3")); } + /// The shadow pass runs when a program reads a shadow buffer or `shadow.enabled` says so; + /// then `shadow`, `shadow_water` and the `shadowcomp` series load, writing shadowcolor0/1. + #[test] + fn shadow_programs_load_when_a_shadow_buffer_is_read() { + let reads = "#version 120\nuniform sampler2D shadowtex0;\nvarying vec2 uv;\n\ + void main() { gl_FragColor = texture2D(shadowtex0, uv); }\n"; + let shadow = "#version 120\nvarying vec2 uv;\nvoid main() {\n/* RENDERTARGETS: 1,0,4 */\n\ + gl_FragData[0] = vec4(1.0); gl_FragData[1] = vec4(0.5); gl_FragData[2] = vec4(0.0);\n}\n"; + let files = [ + ("shaders/composite.vsh", VSH), + ("shaders/composite.fsh", reads), + ("shaders/shadow.vsh", VSH), + ("shaders/shadow.fsh", shadow), + ("shaders/shadowcomp1.vsh", VSH), + ("shaders/shadowcomp1.fsh", FSH), + ]; + let pack = Pack::from_zip_bytes("t", zip(&files)).unwrap(); + let inputs = GameInputs { + terrain: Some(crate::GbuffersInputs { + declarations: "layout(location = 0) in vec3 hz_v_pos;".into(), + body: " hz_Vertex = vec4(hz_v_pos, 1.0);\n".into(), + ..Default::default() + }), + ..Default::default() + }; + let load = |properties: &str| { + let properties = Properties::parse(properties, &Macros::default()); + load_post(&pack, "", &properties, &Macros::standard(), &inputs) + }; + let post = load(""); + assert!(post.shadow_pass); + let shadow = post.shadow.as_ref().expect("shadow"); + assert_eq!( + (shadow.group, &shadow.buffers[..]), + (Group::Shadow, &[1, 0][..]) + ); + // shadow_water falls back to shadow. + assert_eq!( + post.shadow_water.as_ref().map(|p| p.name.as_str()), + Some("shadow") + ); + let comp: Vec<_> = post + .shadow_comp + .iter() + .map(|p| (p.name.as_str(), p.group)) + .collect(); + assert_eq!(comp, [("shadowcomp1", Group::ShadowComp)]); + // DRAWBUFFERS:3 names no shadowcolor buffer. + assert!(post.shadow_comp[0].buffers.is_empty()); + let post = load("shadow.enabled = false"); + assert!(!post.shadow_pass && post.shadow.is_none() && post.shadow_comp.is_empty()); + // Nothing reads a shadow buffer. + let pack = Pack::from_zip_bytes("t", zip(&files[2..])).unwrap(); + let none = |properties: &str| { + let properties = Properties::parse(properties, &Macros::default()); + load_post(&pack, "", &properties, &Macros::standard(), &inputs) + }; + assert!(!none("").shadow_pass); + assert!(none("shadow.enabled=true").shadow.is_some()); + } + /// Water falls back to the terrain chain and blends by default, as the sky does; entities /// and block entities fall back along theirs; `blend.*` overrides. #[test] diff --git a/crates/horizon-shaders/src/translate/mod.rs b/crates/horizon-shaders/src/translate/mod.rs index 2f0163f94..6d2ae46c6 100644 --- a/crates/horizon-shaders/src/translate/mod.rs +++ b/crates/horizon-shaders/src/translate/mod.rs @@ -154,7 +154,7 @@ pub fn translate_composite( fragment: &Preprocessed, target: Target, ) -> Result> { - translate(vertex, fragment, target, None) + translate(vertex, fragment, target, None, false) } /// Translates a gbuffers-style program (`gbuffers_terrain`...) drawing the game's geometry @@ -165,7 +165,18 @@ pub fn translate_gbuffers( target: Target, inputs: &GbuffersInputs, ) -> Result> { - translate(vertex, fragment, target, Some(inputs)) + translate(vertex, fragment, target, Some(inputs), false) +} + +/// Translates a shadow program (`shadow`, `shadow_water`...) drawing the game's geometry +/// described by `inputs` from the shadow light: its model-view and projection matrices are the +/// shadow camera's (5.2). +pub fn translate_shadow( + vertex: &Preprocessed, + fragment: &Preprocessed, + inputs: &GbuffersInputs, +) -> Result> { + translate(vertex, fragment, Target::Buffers, Some(inputs), true) } /// The matrices of the compatibility profile in a gbuffers-style program, from the frame's. @@ -183,7 +194,13 @@ fn translate( fragment: &Preprocessed, target: Target, gbuffers: Option<&GbuffersInputs>, + shadow: bool, ) -> Result> { + let matrices = if shadow { + GBUFFERS_MATRICES.replace("gbuffer", "shadow") + } else { + GBUFFERS_MATRICES.to_owned() + }; let mut v = Stage::scan(vertex, ShaderStage::Vertex); let mut f = Stage::scan(fragment, ShaderStage::Fragment); v.gbuffers = gbuffers.is_some(); @@ -200,6 +217,9 @@ fn translate( "gbufferProjectionInverse", ] { uniforms.insert(name.to_owned(), "mat4".to_owned()); + if shadow { + uniforms.insert(name.replace("gbuffer", "shadow"), "mat4".to_owned()); + } } } let mut samplers: BTreeMap = BTreeMap::new(); @@ -327,7 +347,7 @@ fn translate( hz_MultiTexCoord0 = hz_Vertex;\n hz_Color = vec4(1.0);\n hz_Normal = vec3(0.0, 0.0, 1.0);\n", ), Some(inputs) => { - vertex_main.push_str(GBUFFERS_MATRICES); + vertex_main.push_str(&matrices); vertex_main.push_str(&inputs.body); } } @@ -361,7 +381,7 @@ fn translate( .unwrap(); let mut fragment_main = String::from("\nvoid main() {\n"); if gbuffers.is_some() { - fragment_main.push_str(GBUFFERS_MATRICES); + fragment_main.push_str(&matrices); } fragment_main.push_str(fog); if f.frag_coord { @@ -795,7 +815,7 @@ mod tests { let files: Vec<(&str, &str)> = files.iter().map(|(p, t)| (p.as_str(), *t)).collect(); let pack = Pack::from_zip_bytes("P.zip", zip(&files)).unwrap(); let m = Macros::standard(); - let inputs = GbuffersInputs { + let game = GbuffersInputs { declarations: "layout(location = 0) in vec3 game_pos;\nlayout(location = 1) in uvec2 game_uv;".into(), body: " hz_Vertex = vec4(game_pos, 1.0);\n hz_MultiTexCoord0 = vec4(vec2(game_uv) / 32768.0, 0.0, 1.0);\n \ hz_MultiTexCoord1 = vec4(240.0, 240.0, 0.0, 1.0);\n hz_Color = vec4(1.0);\n hz_Normal = vec3(0.0, 1.0, 0.0);\n" @@ -807,7 +827,7 @@ mod tests { &preprocess(&pack, "gbuffers_terrain.vsh", &m), &preprocess(&pack, "gbuffers_terrain.fsh", &m), Target::Buffers, - &inputs, + &game, ) .unwrap_or_else(|e| panic!("{e:?}")); let names: Vec<&str> = p @@ -842,6 +862,19 @@ mod tests { inputs.contains(&"game_pos") && inputs.contains(&"game_uv"), "{inputs:?}" ); + // From the light, the compatibility matrices are the shadow camera's. + let shadow = translate_shadow( + &preprocess(&pack, "gbuffers_terrain.vsh", &m), + &preprocess(&pack, "gbuffers_terrain.fsh", &m), + &game, + ) + .unwrap_or_else(|e| panic!("{e:?}")); + let names: Vec<&str> = (shadow.interface.uniforms.iter()) + .map(|u| u.name.as_str()) + .collect(); + for name in ["shadowModelView", "shadowProjection", "gbufferModelView"] { + assert!(names.contains(&name), "{name} missing from {names:?}"); + } } #[test] diff --git a/crates/horizon-shaders/tests/real_packs.rs b/crates/horizon-shaders/tests/real_packs.rs index 747b732d7..70fcaa55f 100644 --- a/crates/horizon-shaders/tests/real_packs.rs +++ b/crates/horizon-shaders/tests/real_packs.rs @@ -180,8 +180,12 @@ fn real_packs_read() { }; let post = load_post(&pack, &folder, &shaders, &Macros::standard(), &inputs); println!( - " dimension {id} (folder `{folder}`): {} post programs, terrain {:?}, water {:?}, sky {:?} {:?}, entities {:?} {:?}, {} notes, {:.0} ms", + " dimension {id} (folder `{folder}`): {} post programs, shadow pass {} ({:?}, {:?}, {} shadowcomp), terrain {:?}, water {:?}, sky {:?} {:?}, entities {:?} {:?}, {} notes, {:.0} ms", post.programs.len(), + post.shadow_pass, + post.shadow.as_ref().map(|t| &t.name), + post.shadow_water.as_ref().map(|t| &t.name), + post.shadow_comp.len(), post.terrain.as_ref().map(|t| &t.name), post.water.as_ref().map(|t| &t.name), post.sky.as_ref().map(|t| &t.name), From 632071849134f15723faba98d8fb76d155faa1ce Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:25:47 +0100 Subject: [PATCH 104/238] Shader packs, clean room 39: shadow buffers, their samplers and shadowcomp When the shadow pass runs, the pack gets real shadow buffers (spec 6.3): shadowtex0 (cleared to 1.0 every frame), shadowtex1 (a copy, made only when a program reads it) and shadowcolor0/1 (each with two sides, made only when used, cleared to their ShadowClear colour, white by default), square at shadowMapResolution within the device's limit. Samplers: shadowtex0/watershadow, shadowtex1, `shadow` (shadowtex1 beside watershadow) and the HW forms read the shadow map, through a linear depth comparison when declared sampler2DShadow; shadowcolor/shadowcolor0/1 read the colour buffers. In the shadow programs, buffer n is shadowcolor n and colortex reads a stand-in. Without the shadow pass nothing changes: the names read the stand-ins as before. shadowcomp..shadowcomp99 run after the shadow pass and before prepare, each drawing into the alt side of the shadowcolor buffers it writes and flipping them after, as composite programs do with colortex (6.1.6). The shadow pass itself draws nothing yet, so the map stays clear. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, sections 2.1, 5.2 (samplers), 6.1.6 and 6.3; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 1 + crates/horizon-client/src/gfx/pack_post.rs | 276 +++++++++++++++++-- crates/horizon-client/src/gfx/pack_shadow.rs | 150 ++++++++++ 3 files changed, 397 insertions(+), 30 deletions(-) create mode 100644 crates/horizon-client/src/gfx/pack_shadow.rs diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index df6bc33a5..bba07c647 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -73,6 +73,7 @@ pub mod pack_draw; mod pack_entities; mod pack_hand; mod pack_post; +mod pack_shadow; mod pack_sky; mod pack_timing; pub mod pack_uniforms; diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index 2be557c17..f17765a0f 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -37,6 +37,10 @@ enum Source { /// The block atlas (pack_atlas.rs), in gbuffers programs. Atlas, Lightmap, + /// `shadowtex0`/`shadowtex1` (6.3), with depth comparison (`sampler2DShadow`) or not. + ShadowDepth(usize, bool), + /// `shadowcolor0/1` read outside the shadow programs (in them they are `Color`). + ShadowColor(usize), } #[derive(Clone)] @@ -63,13 +67,17 @@ enum SamplerKind { impl SamplerKind { fn of(source: Source, filtering: bool) -> Self { match source { - Source::Color(_) | Source::Dummy if !filtering => Self::Nearest, - Source::Depth | Source::Depth1 => Self::Nearest, + Source::Color(_) | Source::ShadowColor(_) | Source::Dummy if !filtering => { + Self::Nearest + } + Source::Depth | Source::Depth1 | Source::ShadowDepth(_, false) => Self::Nearest, Source::Noise => Self::Repeat, - Source::Shadow => Self::Comparison, + Source::Shadow | Source::ShadowDepth(_, true) => Self::Comparison, // Blocks look as the game draws them: nearest texels. Source::Atlas => Self::NearestRepeat, - Source::Color(_) | Source::Dummy | Source::Lightmap => Self::Linear, + Source::Color(_) | Source::ShadowColor(_) | Source::Dummy | Source::Lightmap => { + Self::Linear + } } } @@ -158,6 +166,8 @@ struct Pass { flip: u16, /// Buffers sampled, for the bind group cache key. reads: u16, + /// shadowcolor buffers sampled by a program of the other buffers. + shadow_reads: u16, /// Buffers written. written: u16, /// Buffers whose mip chain is regenerated right before it (6.1.7). @@ -168,9 +178,9 @@ struct Pass { atlas: Option, /// Reads `mc_Entity`'s table (`PackPost::block_ids`). ids: bool, - /// Bind groups by the sides read (low 16 bits) and the buffers whose mip chain is current - /// (high 16 bits). - groups: HashMap, + /// Bind groups by the sides read (bits 0–15), the buffers whose mip chain is current (bits + /// 16–31) and the shadowcolor sides read (bits 32–33). + groups: HashMap, } /// One side of a buffer with mipmaps. @@ -258,6 +268,9 @@ pub struct PackPost { hand: Option<(Pass, super::pack_hand::PackHand)>, /// A program after translucent terrain reads `depthtex1`: it is copied before water. depth1: bool, + /// The shadow buffers, when the shadow pass runs (2.5), and the `shadowcomp` programs. + shadow_targets: Option, + shadow_comp: Vec, /// Buffers with mipmaps (filterable ones a program asks them for). mipped: u16, /// Buffers whose mip chain matches their first level, this frame. @@ -342,7 +355,37 @@ fn texture_format(f: Format, features: wgpu::Features) -> wgpu::TextureFormat { } } -fn source(name: &str) -> Source { +/// The texture behind sampler `name` in a program of the shadow buffers (`shadow_space`: buffer +/// n is shadowcolor n) or of the others, when the shadow pass runs (`shadow_pass`). `shadow` is +/// `shadowtex1` when the program also declares `watershadow` (6.3). +fn source(name: &str, shadow_space: bool, shadow_pass: bool, watershadow: bool) -> Source { + let shadow_color = match name { + "shadowcolor" | "shadowcolor0" => Some(0), + "shadowcolor1" => Some(1), + _ => None, + }; + let shadow_depth = match name { + "shadowtex0" | "watershadow" | "shadowtex0HW" => Some(0), + "shadowtex1" | "shadowtex1HW" => Some(1), + "shadow" => Some(usize::from(watershadow)), + _ => None, + }; + if shadow_pass { + if let Some(n) = shadow_depth { + return Source::ShadowDepth(n, name.ends_with("HW")); + } + if let Some(n) = shadow_color { + return if shadow_space { + Source::Color(n) + } else { + Source::ShadowColor(n) + }; + } + } + if shadow_space && horizon_shaders::buffer_index(name).is_some() { + // colortex buffers are not this pass's. + return Source::Dummy; + } match name { "depthtex0" | "gdepthtex" => Source::Depth, // The hand is drawn after the pack, so `depthtex2` (without it) is `depthtex1`. @@ -606,11 +649,15 @@ pub(super) struct Geometry<'a> { } /// The pass of program `p`: a quad, or `geometry` drawn by the game. `None` (with a note) when -/// wgpu refuses it or it writes nothing. +/// wgpu refuses it or it writes nothing. `formats` are its buffers' (for a program of the shadow +/// buffers, buffer n is shadowcolor n); `shadow` the shadowcolor formats when the shadow pass +/// runs. +#[allow(clippy::too_many_arguments)] fn build_pass( device: &wgpu::Device, p: horizon_shaders::PostProgram, formats: &[wgpu::TextureFormat; BUFFERS], + shadow: Option<&[wgpu::TextureFormat; 2]>, screen: wgpu::TextureFormat, notes: &mut Vec, geometry: Option<&Geometry<'_>>, @@ -639,6 +686,8 @@ fn build_pass( let mut samplers = Vec::new(); let (mut kinds, mut slots) = (HashMap::new(), HashMap::new()); let mut reads = 0u16; + let mut shadow_reads = 0u16; + let watershadow = interface.samplers.iter().any(|s| s.name == "watershadow"); // A gbuffers pass draws into its buffers and the depth buffer: reading them as well would // be a feedback loop (undefined in GL, refused by wgpu), so those read a blank texture. let in_place = match geometry { @@ -646,8 +695,10 @@ fn build_pass( None => 0, }; for s in &interface.samplers { - let mut src = source(&s.name); + let mut src = source(&s.name, p.group.shadow(), shadow.is_some(), watershadow); let blank = match src { + // The shadow pass writes the shadow map. + Source::ShadowDepth(..) => p.group == Group::Shadow, Source::Atlas => geometry.is_none(), Source::Depth => geometry.is_some(), // Translucent terrain reads the copy; opaque terrain would read itself. @@ -660,14 +711,23 @@ fn build_pass( Source::Color(n) => formats[n] .sample_type(None, Some(features)) .unwrap_or(float), - Source::Depth | Source::Depth1 => wgpu::TextureSampleType::Float { filterable: false }, + Source::ShadowColor(n) => shadow + .and_then(|f| f[n].sample_type(None, Some(features))) + .unwrap_or(float), + Source::Depth | Source::Depth1 | Source::ShadowDepth(..) => { + wgpu::TextureSampleType::Float { filterable: false } + } // Unfilterable on WebGPU-level devices; the lightmap is sampled at texel centres. Source::Lightmap => float, _ => float, }; let int = !matches!(sample_type, wgpu::TextureSampleType::Float { .. }); if s.ty == "sampler2DShadow" { - (src, sample_type) = (Source::Shadow, wgpu::TextureSampleType::Depth); + src = match src { + Source::ShadowDepth(n, _) => Source::ShadowDepth(n, true), + _ => Source::Shadow, + }; + sample_type = wgpu::TextureSampleType::Depth; } let wanted = match s.ty.as_str() { "sampler2DShadow" => true, @@ -683,7 +743,9 @@ fn build_pass( )); (src, sample_type) = (Source::Dummy, float); } - if blank { + if blank && sample_type == wgpu::TextureSampleType::Depth { + src = Source::Shadow; + } else if blank { // The blank texture is float: an integer sampler then fails the pipeline (a note). src = Source::Dummy; if int { @@ -691,8 +753,10 @@ fn build_pass( } } let filtering = sample_type == float; - if let Source::Color(n) = src { - reads |= 1 << n; + match src { + Source::Color(n) => reads |= 1 << n, + Source::ShadowColor(n) => shadow_reads |= 1 << n, + _ => {} } entries.push(texture_entry(s.binding, sample_type)); let kind = SamplerKind::of(src, filtering); @@ -849,6 +913,7 @@ fn build_pass( writes, flip, reads, + shadow_reads, written, mipmaps: p.mipmaps, draw, @@ -892,18 +957,32 @@ impl PackPost { |flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0); let mut used = 1u16; let draws = DrawUniforms::new(device); + // The shadow buffers' formats when the shadow pass runs; their programs see them as + // buffers 0 and 1. + let shadow_formats: Option<[wgpu::TextureFormat; 2]> = post.shadow_pass.then(|| { + std::array::from_fn(|n| texture_format(shadow_config.colors[n].format, features)) + }); + let shadow_space: [wgpu::TextureFormat; BUFFERS] = std::array::from_fn(|i| { + shadow_formats + .filter(|_| i < 2) + .map_or(formats[i], |f| f[i]) + }); let mut build = |p: horizon_shaders::PostProgram, geometry: Option<&Geometry<'_>>| { + let in_shadow = p.group.shadow(); let pass = build_pass( device, p, - &formats, + if in_shadow { &shadow_space } else { &formats }, + shadow_formats.as_ref(), screen, &mut notes, geometry, &custom_names, &draws.layout, )?; - used |= pass.reads | pass.written | pass.flip; + if !in_shadow { + used |= pass.reads | pass.written | pass.flip; + } Some(pass) }; // Flat terrain reads `mc_Entity` from a table of the sprite layers. @@ -1036,6 +1115,12 @@ impl PackPost { .filter(|p| !skip.split(',').any(|s| s == p.name)) .filter_map(|p| build(p, None)) .collect(); + let shadow_comp: Vec = post + .shadow_comp + .into_iter() + .filter(|p| !skip.split(',').any(|s| s == p.name)) + .filter_map(|p| build(p, None)) + .collect(); if let Some(t) = &terrain { notes.push(format!("terrain draws with {}", t.name)); } @@ -1077,6 +1162,39 @@ impl PackPost { .chain(&water) .chain(weather.iter().take(1).flatten()) .any(|p| p.samplers.iter().any(|s| s.source == Source::Depth1)); + let every_pass = || { + (passes + .iter() + .chain(&shadow_comp) + .chain(&terrain) + .chain(&water)) + .chain(sky.iter().flat_map(|s| s.basic.iter().chain(&s.textured))) + .chain(entities.iter().flat_map(|e| e.passes.iter().flatten())) + .chain(&clouds) + .chain(weather.iter().flatten()) + .chain(hand.iter().map(|h| &h.0)) + }; + let shadow_targets = shadow_formats.map(|f| { + let colors = every_pass().fold(0u16, |m, p| { + let own = if p.group.shadow() { + p.reads | p.written | p.flip + } else { + 0 + }; + m | own | p.shadow_reads + }); + let depth1 = every_pass() + .flat_map(|p| &p.samplers) + .any(|s| matches!(s.source, Source::ShadowDepth(1, _))); + let t = + super::pack_shadow::ShadowTargets::new(device, &shadow_config, f, colors, depth1); + notes.push(format!( + "shadow map {0}x{0}, shadowcolor buffers {colors:#b}{1}", + t.size, + if depth1 { ", shadowtex1" } else { "" } + )); + t + }); // A clear only matters when a program reads the buffer before one writes all of it // (composite passes cover their whole target, terrain does not); the others are skipped. let mut cleared = Vec::new(); @@ -1215,6 +1333,8 @@ impl PackPost { weather_generation: 0, hand, depth1, + shadow_targets, + shadow_comp, mipped, mips_valid: 0, down, @@ -1319,17 +1439,43 @@ impl PackPost { queue.write_buffer(&pass.ubo, 0, &pass.block); } let targets = self.targets.as_ref().expect("resized"); + let shadow = self.shadow_targets.as_ref(); + let shadow_main = shadow.map_or(0, |s| s.main); + let shadow_space = pass.group.shadow(); // A chain is sampled only while it is current: a buffer written since its last chain - // reads its first level, never stale levels (6.1.7). - let current = self.mips_valid & self.mipped & pass.reads; - let key = u32::from(self.main & pass.reads) | u32::from(current) << 16; + // reads its first level, never stale levels (6.1.7). The shadow buffers have none. + let (main, current) = if shadow_space { + (shadow_main, 0) + } else { + (self.main, self.mips_valid & self.mipped & pass.reads) + }; + let key = u64::from(main & pass.reads) + | u64::from(current) << 16 + | u64::from(shadow_main & pass.shadow_reads) << 32; let group = pass.groups.entry(key).or_insert_with(|| { let mut entries = vec![wgpu::BindGroupEntry { binding: 0, resource: pass.ubo.as_entire_binding(), }]; for s in &pass.samplers { + let shadow_color = |i: usize, side: usize| { + shadow.and_then(|t| t.color[i].as_ref()).map(|c| &c[side]) + }; let texture = match s.source { + Source::Color(i) if shadow_space => { + shadow_color(i, usize::from(key & 1 << i != 0)).unwrap_or(&self.dummy) + } + Source::ShadowColor(i) => { + shadow_color(i, usize::from(key & 1 << (32 + i) != 0)) + .unwrap_or(&self.dummy) + } + Source::ShadowDepth(i, _) => match shadow { + Some(t) => match (i, &t.depth1) { + (1, Some(d)) => &d.1, + _ => &t.depth.1, + }, + None => &self.shadow.0, + }, Source::Color(i) => { let side = usize::from(key & 1 << i != 0); match (&targets.mips[i], &targets.color[i]) { @@ -1366,7 +1512,7 @@ impl PackPost { SamplerKind::Nearest => &self.nearest, SamplerKind::Repeat => &self.repeat, SamplerKind::NearestRepeat => &self.nearest_repeat, - SamplerKind::Comparison => &self.shadow.1, + SamplerKind::Comparison => shadow.map_or(&self.shadow.1, |t| &t.comparison), }; entries.push(wgpu::BindGroupEntry { binding: s.binding, @@ -1399,15 +1545,20 @@ impl PackPost { load: wgpu::LoadOp::Load, store: wgpu::StoreOp::Store, }; + // The shadow pass draws into the shadow buffers and the shadow map. + let shadow = self.shadow_targets.as_ref().filter(|_| pass.group.shadow()); let mut attachments: Vec> = Vec::new(); for &(location, buffer) in &pass.writes { let l = location as usize; if attachments.len() <= l { attachments.resize(l + 1, None); } - attachments[l] = targets.color[buffer] - .as_ref() - .and_then(|t| attach(&t[usize::from(self.main & 1 << buffer != 0)], load)); + attachments[l] = match shadow { + Some(s) => s.color(buffer, false).and_then(|v| attach(v, load)), + None => targets.color[buffer] + .as_ref() + .and_then(|t| attach(&t[usize::from(self.main & 1 << buffer != 0)], load)), + }; } let timestamp_writes = self.timing.as_mut().and_then(|t| t.writes(&pass.name)); encoder.begin_render_pass(&wgpu::RenderPassDescriptor { @@ -1415,7 +1566,7 @@ impl PackPost { color_attachments: &attachments, timestamp_writes, depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { - view: &targets.depth, + view: shadow.map_or(&targets.depth, |s| &s.depth.1), depth_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Load, store: wgpu::StoreOp::Store, @@ -1933,6 +2084,55 @@ impl PackPost { /// Runs the pack over the world drawn into `world` (colour) and `world_depth`, then draws /// the result onto `screen`. #[allow(clippy::too_many_arguments)] + /// The shadow pass (2.1, passes 2 and 3): the shadow buffers cleared, then the `shadowcomp` + /// programs, each drawing into the "alt" side of what it writes and flipping it after. + fn run_shadow( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + ) { + let Some(t) = &self.shadow_targets else { + return; + }; + t.clear(encoder); + t.copy_depth1(encoder); + let size = [t.size; 2]; + let mut comp = std::mem::take(&mut self.shadow_comp); + for pass in &mut comp { + let group = self.bind(device, queue, pass, size); + let t = self.shadow_targets.as_ref().expect("shadow pass"); + let mut attachments: Vec> = Vec::new(); + for &(location, buffer) in &pass.writes { + let l = location as usize; + if attachments.len() <= l { + attachments.resize(l + 1, None); + } + // Overwritten whole. + let ops = wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), + store: wgpu::StoreOp::Store, + }; + attachments[l] = t.color(buffer, true).and_then(|v| attach(v, ops)); + } + { + let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some(&pass.name), + color_attachments: &attachments, + timestamp_writes: self.timing.as_mut().and_then(|t| t.writes(&pass.name)), + ..Default::default() + }); + rp.set_pipeline(&pass.pipeline); + rp.set_bind_group(0, &group, &[]); + rp.draw(0..6, 0..1); + } + if let Some(t) = &mut self.shadow_targets { + t.main ^= pass.flip; + } + } + self.shadow_comp = comp; + } + pub fn run( &mut self, device: &wgpu::Device, @@ -1978,6 +2178,10 @@ impl PackPost { } self.uniforms.update(frame, inputs, size); self.uniforms.eval_custom(&mut self.custom); + if self.targets.is_none() { + return; + } + self.run_shadow(device, queue, encoder); let Some(targets) = &self.targets else { return }; let main = |m: u16, i: usize| usize::from(m & 1 << i != 0); let view = |m: u16, i: usize, alt: bool| { @@ -2249,10 +2453,22 @@ mod tests { #[test] fn sampler_names() { - assert!(source("gaux1") == Source::Color(4)); - assert!(source("gdepthtex") == Source::Depth); - assert!(source("depthtex2") == Source::Depth1); - assert!(source("noisetex") == Source::Noise); - assert!(source("shadowcolor0") == Source::Dummy); + let main = |n| source(n, false, false, false); + assert!(main("gaux1") == Source::Color(4)); + assert!(main("gdepthtex") == Source::Depth); + assert!(main("depthtex2") == Source::Depth1); + assert!(main("noisetex") == Source::Noise); + // Without the shadow pass, the shadow buffers are stand-ins. + assert!(main("shadowcolor0") == Source::Dummy); + assert!(main("shadowtex0") == Source::Dummy); + // With it: shadowcolor n is buffer n of the shadow programs, `shadow` is shadowtex1 + // beside `watershadow` (6.3), and colortex is not theirs. + assert!(source("shadowcolor", false, true, false) == Source::ShadowColor(0)); + assert!(source("shadowcolor1", true, true, false) == Source::Color(1)); + assert!(source("colortex1", true, true, false) == Source::Dummy); + assert!(source("shadow", false, true, false) == Source::ShadowDepth(0, false)); + assert!(source("shadow", false, true, true) == Source::ShadowDepth(1, false)); + assert!(source("watershadow", false, true, true) == Source::ShadowDepth(0, false)); + assert!(source("shadowtex1HW", false, true, false) == Source::ShadowDepth(1, true)); } } diff --git a/crates/horizon-client/src/gfx/pack_shadow.rs b/crates/horizon-client/src/gfx/pack_shadow.rs new file mode 100644 index 000000000..7688d1250 --- /dev/null +++ b/crates/horizon-client/src/gfx/pack_shadow.rs @@ -0,0 +1,150 @@ +//! A shader pack's shadow buffers (spec 6.3): the shadow map's depth (`shadowtex0`, and +//! `shadowtex1` without translucent geometry), and the colour buffers `shadowcolor0/1`, each +//! with two sides that `shadowcomp` programs flip as composite programs flip colortex (6.1.6). +//! Square, `shadowMapResolution` pixels. + +/// What the shadow pass writes and the programs read. +pub(super) struct ShadowTargets { + pub size: u32, + /// `shadowtex0`, written by the shadow pass. + pub depth: (wgpu::Texture, wgpu::TextureView), + /// `shadowtex1`: a copy of `shadowtex0` taken before translucent terrain, when read. + pub depth1: Option<(wgpu::Texture, wgpu::TextureView)>, + /// Both sides of each shadowcolor buffer used. + pub color: [Option<[wgpu::TextureView; 2]>; 2], + /// The clear colour of each buffer cleared every frame. + pub clears: [Option<[f32; 4]>; 2], + /// Bit n set: B is shadowcolor n's "main". + pub main: u16, + /// Depth comparison for `sampler2DShadow` (hardware filtering): linear, as GL filters it. + pub comparison: wgpu::Sampler, +} + +fn target( + device: &wgpu::Device, + label: &str, + size: u32, + format: wgpu::TextureFormat, + copy: wgpu::TextureUsages, +) -> (wgpu::Texture, wgpu::TextureView) { + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some(label), + size: wgpu::Extent3d { + width: size, + height: size, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::RENDER_ATTACHMENT | copy, + view_formats: &[], + }); + let view = texture.create_view(&Default::default()); + (texture, view) +} + +impl ShadowTargets { + /// The buffers of `shadow` the pack uses: `colors` (bit n: shadowcolor n), `depth1` when + /// some program reads `shadowtex1`. The resolution is kept within the device's limit. + pub fn new( + device: &wgpu::Device, + shadow: &horizon_shaders::Shadow, + formats: [wgpu::TextureFormat; 2], + colors: u16, + depth1: bool, + ) -> Self { + let size = shadow + .resolution + .clamp(1, device.limits().max_texture_dimension_2d); + let copy = wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::COPY_DST; + let depth = wgpu::TextureFormat::Depth32Float; + let color = std::array::from_fn(|n| { + (colors & 1 << n != 0).then(|| { + [0, 1].map(|side| { + let label = format!("Shader pack shadowcolor{n} {}", ["A", "B"][side]); + target( + device, + &label, + size, + formats[n], + wgpu::TextureUsages::empty(), + ) + .1 + }) + }) + }); + Self { + size, + depth: target(device, "Shader pack shadowtex0", size, depth, copy), + depth1: depth1.then(|| target(device, "Shader pack shadowtex1", size, depth, copy)), + color, + clears: std::array::from_fn(|n| { + let b = &shadow.colors[n]; + b.clear.then(|| b.clear_color.unwrap_or([1.0; 4])) + }), + main: 0, + comparison: device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("Shader pack shadow comparison"), + compare: Some(wgpu::CompareFunction::LessEqual), + mag_filter: wgpu::FilterMode::Linear, + min_filter: wgpu::FilterMode::Linear, + ..Default::default() + }), + } + } + + /// Side `alt` (of "main" and "alt") of shadowcolor `n`. + pub fn color(&self, n: usize, alt: bool) -> Option<&wgpu::TextureView> { + let side = usize::from(self.main & 1 << n != 0) ^ usize::from(alt); + self.color.get(n)?.as_ref().map(|c| &c[side]) + } + + /// Clears the shadow map and the shadowcolor buffers cleared every frame (6.3). + pub fn clear(&self, encoder: &mut wgpu::CommandEncoder) { + let attachments: Vec<_> = (0..2) + .filter_map(|n| { + let c = self.clears[n]?; + Some(Some(wgpu::RenderPassColorAttachment { + view: self.color(n, false)?, + depth_slice: None, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color { + r: c[0].into(), + g: c[1].into(), + b: c[2].into(), + a: c[3].into(), + }), + store: wgpu::StoreOp::Store, + }, + })) + }) + .collect(); + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Shader pack shadow clear"), + color_attachments: &attachments, + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: &self.depth.1, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(1.0), + store: wgpu::StoreOp::Store, + }), + stencil_ops: None, + }), + ..Default::default() + }); + } + + /// `shadowtex1`: the shadow map as it is now (before translucent geometry). + pub fn copy_depth1(&self, encoder: &mut wgpu::CommandEncoder) { + if let Some((copy, _)) = &self.depth1 { + encoder.copy_texture_to_texture( + self.depth.0.as_image_copy(), + copy.as_image_copy(), + self.depth.0.size(), + ); + } + } +} From bf81ae6358638575fb14192f2a5f82495a0ce13f Mon Sep 17 00:00:00 2001 From: Alexander Krylov Date: Sun, 25 Oct 2026 18:28:48 +0300 Subject: [PATCH 105/238] Furnace: pay the experience a smelt earned RecipesUsed was written as an empty compound and never read, so a furnace paid nothing when its output was taken and nothing when it was broken. - Furnace keeps the per-recipe counter, saves and loads it, and floors each recipe on its own as AbstractFurnaceBlockEntity.createExperience does. - FurnaceResultSlot.checkTakeAchievements pays on a take: the counter is read and cleared when the output slot loses an item. - AbstractFurnaceBlockEntity.playerDestroy pays the same counter as orbs at the block when it is broken, whatever the tool. Fixes #1732 Co-Authored-By: Hermes Agent (Nous Research) --- .../examples/probe/menu_fixture.rs | 1 + crates/horizon-client/src/containers.rs | 47 +++++++++++++++++ crates/horizon-client/src/crafting.rs | 8 +++ crates/horizon-client/src/game.rs | 51 +++++++++++++++++++ crates/horizon-client/src/game/remote.rs | 1 + crates/horizon-client/src/save.rs | 35 ++++++++++++- 6 files changed, 142 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/examples/probe/menu_fixture.rs b/crates/horizon-client/examples/probe/menu_fixture.rs index 3c83b20bc..9ec558de3 100644 --- a/crates/horizon-client/examples/probe/menu_fixture.rs +++ b/crates/horizon-client/examples/probe/menu_fixture.rs @@ -169,6 +169,7 @@ pub fn main() -> horizon_client::Result<()> { cooking_timer: menu.data[2], cooking_total: menu.data[3], speed: 1.0, + recipes_used: Default::default(), }); menu.drops.clear(); menu.equipped.clear(); diff --git a/crates/horizon-client/src/containers.rs b/crates/horizon-client/src/containers.rs index de2e1b2e9..3701fefaf 100644 --- a/crates/horizon-client/src/containers.rs +++ b/crates/horizon-client/src/containers.rs @@ -44,6 +44,10 @@ pub struct Furnace { pub cooking_timer: i32, pub cooking_total: i32, pub speed: f32, + /// `AbstractFurnaceBlockEntity.recipesUsed`: how many of each recipe this furnace has + /// made since a player last took from its output. The experience is owed per recipe + /// rather than per item, and a save keeps it as the `RecipesUsed` compound. + pub recipes_used: HashMap, } /// A hopper's slots: the only plain container with five. pub const HOPPER_SLOTS: usize = 5; @@ -190,6 +194,7 @@ impl BlockEntity { cooking_timer: 0, cooking_total: 0, speed: 1.0, + recipes_used: HashMap::new(), })); } if name == "minecraft:creaking_heart" { @@ -483,6 +488,26 @@ impl Furnace { self.cooking_timer = 0; } } + /// `AbstractFurnaceBlockEntity.createExperience`: each recipe's own share is floored, + /// then the floors are added. Flooring the sum instead pays one point for two recipes + /// worth 0.6 each, which vanilla does not. + fn owed(used: &HashMap, experience: impl Fn(&str) -> f32) -> u32 { + used.iter() + .map(|(id, count)| (experience(id) * *count as f32).floor().max(0.0) as u32) + .sum() + } + /// What this furnace owes right now, without forgetting it: breaking the block reads + /// the same counter, and only a payout clears it. + pub fn experience_owed(&self, recipes: &Recipes) -> u32 { + Self::owed(&self.recipes_used, |id| recipes.cooking_experience(id)) + } + /// `AbstractFurnaceBlockEntity.awardUsedRecipesAndPopExperience`: the experience this + /// furnace owes, forgotten as it is paid. + pub fn take_experience(&mut self, recipes: &Recipes) -> u32 { + let owed = self.experience_owed(recipes); + self.recipes_used.clear(); + owed + } /// A menu click wrote its slots into `items`, and `before` was the input /// slot until then: the menu called `setItem` on it only if it changed /// (`Slot.setByPlayer`; partial takes and merges keep the stack). @@ -564,6 +589,7 @@ impl Furnace { Some(out) => out.count += recipe.result.count, None => self.items[2] = Some(recipe.result.clone()), } + *self.recipes_used.entry(recipe.id.clone()).or_insert(0) += 1; if let Some(i) = self.items[0].as_mut() { i.count -= 1; if i.count == 0 { @@ -594,3 +620,24 @@ fn total_time(time: u32, speed: f32) -> i32 { time as i32 } } + +#[cfg(test)] +mod tests { + use super::*; + + /// `createExperience` floors each recipe on its own. Two recipes worth 0.6 each pay + /// nothing, where flooring their sum would pay one point. + #[test] + fn experience_is_floored_per_recipe_and_not_on_the_sum() { + let mut used = HashMap::new(); + used.insert("a".to_owned(), 1); + used.insert("b".to_owned(), 1); + assert_eq!(Furnace::owed(&used, |_| 0.6), 0); + + used.insert("c".to_owned(), 10); + assert_eq!( + Furnace::owed(&used, |id| if id == "c" { 0.1 } else { 0.0 }), + 1 + ); + } +} diff --git a/crates/horizon-client/src/crafting.rs b/crates/horizon-client/src/crafting.rs index d0b8906ee..690383963 100644 --- a/crates/horizon-client/src/crafting.rs +++ b/crates/horizon-client/src/crafting.rs @@ -1610,6 +1610,14 @@ impl Recipes { } out } + /// The `experience` of the cooking recipe with this file name, and 0 for one this data + /// pack does not have (a save from another pack can name it). + pub fn cooking_experience(&self, id: &str) -> f32 { + self.cooking + .iter() + .find(|c| c.id == id) + .map_or(0.0, |c| c.experience) + } pub fn cook(&self, kind: &str, input: &ItemStack) -> Option<&Cooking> { let index = self.cook_index.get_or_init(|| { let mut index = CookIndex::new(); diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 699468651..7c0bbc917 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -3246,6 +3246,12 @@ impl Game<'_> { } // The block entity as it was, for what the drops copy from it. let key = (pos[0], pos[1], pos[2]); + // AbstractFurnaceBlockEntity.playerDestroy: the experience its recipes earned drops + // with the block. Read now, paid only if the block is really removed. + let furnace_xp = match self.world.block_entities.get(&key) { + Some(BlockEntity::Furnace(f)) => f.experience_owed(self.res.recipes()), + _ => 0, + }; if let Some(entity) = self.world.block_entities.get(&key).cloned() { let name = self.world.block_names.get(&key).cloned(); self.pending.record(pos, &entity, &facts.name, name); @@ -3315,6 +3321,18 @@ impl Game<'_> { ); } } + // Stored recipe experience is not a loot drop: it leaves with the block + // whatever the tool, as `AbstractFurnaceBlockEntity` does. + if furnace_xp > 0 { + self.spawn_orbs( + [ + f64::from(pos[0]) + 0.5, + f64::from(pos[1]) + 0.5, + f64::from(pos[2]) + 0.5, + ], + furnace_xp, + ); + } } self.pending.clear(pos); self.process_level_events()?; @@ -5936,6 +5954,12 @@ impl Game<'_> { // Attached after the slots were set: setting them is not a click. menu.merchant = session.map(|(s, _)| s); } + // `FurnaceResultSlot.checkTakeAchievements` pays out on what left the output slot. + let furnace_before = if matches!(kind, Kind::Furnace(_)) { + menu.slot(2).cloned().map_or(0, |s| s.count) + } else { + 0 + }; let outcome = run(&mut menu, &env, &to_menu); let made = maps.made.take(); match outcome { @@ -5953,6 +5977,12 @@ impl Game<'_> { self.merchant_sync_out(s); } let after: Vec> = (0..total).map(|i| menu.slot(i).cloned()).collect(); + // Only a furnace has a result slot to compare: an enchanting table has two slots. + let furnace_after = if matches!(kind, Kind::Furnace(_)) { + after[2].as_ref().map_or(0, |s| s.count) + } else { + 0 + }; let carried = menu.carried.take(); let dropped = std::mem::take(&mut menu.dropped); let sounds = std::mem::take(&mut menu.sounds); @@ -5977,6 +6007,15 @@ impl Game<'_> { self.player_drop(stack); } self.award_crafted(&crafted); + // shortcut: paid straight to the player instead of through `ExperienceOrb.award` + // (no orb flies at them), which a furnace's orbs reach within a tick anyway; + // upgrade if the visual matters or another block pays out far from the player. + if furnace_before > furnace_after { + let owed = self.take_furnace_experience(); + if owed > 0 { + self.give_orb_points(owed); + } + } } if kind == Kind::Loom { if let Some(s) = self.station.as_mut() { @@ -8768,6 +8807,18 @@ impl Game<'_> { self.give_orb_points(gained); } } + /// `AbstractFurnaceBlockEntity.awardUsedRecipesAndPopExperience`: what the open furnace + /// owes for everything it made since the last payout, cleared as it is read. + fn take_furnace_experience(&mut self) -> u32 { + let Some(pos) = self.container else { + return 0; + }; + let recipes = self.res.recipes(); + let Some(BlockEntity::Furnace(f)) = self.world.block_entities.get_mut(&pos) else { + return 0; + }; + f.take_experience(recipes) + } /// `Player.giveExperiencePoints`, and `giveExperienceLevels`' level-up /// sound every fifth level (at most every 100 ticks). pub(super) fn give_orb_points(&mut self, gained: u32) { diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 044e43835..12e012a06 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -4919,6 +4919,7 @@ impl Game<'_> { cooking_timer: 0, cooking_total: 0, speed: 1.0, + recipes_used: std::collections::HashMap::new(), })) } // HopperMenu, and DispenserMenu (MenuType.GENERIC_3x3: dispensers diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index da5735c17..937519da9 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -488,7 +488,12 @@ pub fn encode_block_entity( entries.push(("cooking_time_spent", Tag::Int(f.cooking_timer))); entries.push(("cooking_total_time", Tag::Int(f.cooking_total))); entries.push(("speed_multiplier", Tag::Float(f.speed.to_bits()))); - entries.push(("RecipesUsed", Tag::Compound(Compound::new()))); + let used: Compound = f + .recipes_used + .iter() + .map(|(id, count)| (id.as_str().into(), Tag::Int(*count as i32))) + .collect(); + entries.push(("RecipesUsed", Tag::Compound(used))); } BlockEntity::Brewing(b) => { entries.push(("Items", encode_items(&b.items))); @@ -640,6 +645,17 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain if let Some(speed) = float(tag.get("speed_multiplier")) { f.speed = speed as f32; } + // A save without it owes nothing: the field is newer than some worlds. + if let Some(Tag::Compound(used)) = tag.get("RecipesUsed") { + for (id, count) in used { + let count = match count { + Tag::Int(n) => *n, + _ => continue, + }; + f.recipes_used + .insert(id.to_string_lossy(), count.max(0) as u32); + } + } } BlockEntity::Brewing(b) => { let items = decode_items(tag.get("Items"), 5); @@ -4276,4 +4292,21 @@ mod tests { .unwrap_or(0); assert_eq!(flying_survival, 0); } + + /// `RecipesUsed` is what a furnace owes in experience, so a save that drops it pays the + /// player nothing after a reload. + #[test] + fn a_furnaces_recipes_used_survive_a_save() { + use crate::containers::BlockEntity; + let Some(BlockEntity::Furnace(mut f)) = BlockEntity::for_block("minecraft:furnace") else { + panic!("a furnace"); + }; + f.recipes_used + .insert("minecraft:cooked_porkchop".to_owned(), 3); + let tag = encode_block_entity((1, 64, 2), &BlockEntity::Furnace(f), "minecraft:furnace"); + let Some((_, BlockEntity::Furnace(back))) = decode_block_entity(&tag) else { + panic!("a furnace back"); + }; + assert_eq!(back.recipes_used.get("minecraft:cooked_porkchop"), Some(&3)); + } } From feec1b67e8446f5d81a4418f956b8b6b0f394e1e Mon Sep 17 00:00:00 2001 From: JasonGu <1456970218@qq.com> Date: Sun, 25 Oct 2026 23:29:23 +0800 Subject: [PATCH 106/238] Server deaths: build the message from the player's own combat record A guest's death only had `death_cause`, which `/kill` alone sets, so a server-thread, LAN or dedicated death fell back to "X died" or a generic cause. Give each guest its own CombatTracker, fed by the motion events the server already records, and take the message from CombatTracker.getDeathMessage with the dying player's own name. The wither's skull names the wither that launched it (projectile owner). The death screen now resolves the server's component in the client's current language instead of the session's en_us table, so it matches the chat line. Co-Authored-By: OpenCode DeepSeek V4.1 Flash --- crates/horizon-client/src/game.rs | 3 + crates/horizon-client/src/game/combat.rs | 6 +- crates/horizon-client/src/game/lan_actions.rs | 83 +++++++++++++------ crates/horizon-client/src/game/remote.rs | 10 +-- crates/horizon-client/src/game/wither.rs | 2 + 5 files changed, 72 insertions(+), 32 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 699468651..848061ff5 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1568,6 +1568,8 @@ struct Game<'a> { cmds: commands::extra::CommandExtras, ui: chat::UiState, combat: combat::CombatTracker, + /// Each guest's own `CombatTracker`, out of `combat` between turns. + guest_combat: std::collections::HashMap, mount: mounts::MountState, stats: stats::Stats, score: scoreboard::ScoreState, @@ -12001,6 +12003,7 @@ impl<'a> Game<'a> { cmds: Default::default(), ui: Default::default(), combat: Default::default(), + guest_combat: Default::default(), mount: Default::default(), stats: Default::default(), score: Default::default(), diff --git a/crates/horizon-client/src/game/combat.rs b/crates/horizon-client/src/game/combat.rs index 70ae082aa..227aa0332 100644 --- a/crates/horizon-client/src/game/combat.rs +++ b/crates/horizon-client/src/game/combat.rs @@ -191,8 +191,12 @@ impl Game<'_> { } /// `CombatTracker.getDeathMessage`. pub(super) fn combat_death_message(&mut self) -> Text { + self.combat_death_message_for(Text::literal(PLAYER_NAME)) + } + /// `CombatTracker.getDeathMessage` for a player whose display name is + /// `me`: a server's guest is not `PLAYER_NAME`. + pub(super) fn combat_death_message_for(&mut self, me: Text) -> Text { self.recheck_combat(); - let me = Text::literal(PLAYER_NAME); let entries = std::mem::take(&mut self.combat.entries); self.combat.in_combat = false; let Some(killing) = entries.last() else { diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 2c7019c73..f5d419357 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -143,6 +143,8 @@ pub(super) struct GuestSwap { adv_loaded: bool, last_inventory: u64, death_cause: Option, + /// The guest's own damage record, in place while they act. + combat: super::combat::CombatTracker, /// The guest whose turn this one nests in, if any. acting: (Option, Option, Option), /// The packets queued for that guest. @@ -256,6 +258,14 @@ impl Game<'_> { host.play.insert(conn, play); return None; }; + // The guest's own CombatTracker: damage to them is not the host's. + // A new body starts with no record of the last one. + let guest_combat = if fresh { + Default::default() + } else { + self.guest_combat.remove(&conn).unwrap_or_default() + }; + let host_combat = std::mem::replace(&mut self.combat, guest_combat); self.potions.swap_player(&mut g.effects, &mut play.potions); motion.position = Vec3::new(g.position[0], g.position[1], g.position[2]); motion.ground.on_ground = g.on_ground; @@ -418,6 +428,7 @@ impl Game<'_> { swings, projectiles, modifiers, + combat: host_combat, }) } @@ -468,12 +479,16 @@ impl Game<'_> { adv_loaded, last_inventory, death_cause, + combat, acting, packets, swings, projectiles, modifiers, } = swap; + // The guest's damage record back into their slot, the host's in place. + let guest_combat = std::mem::replace(&mut self.combat, combat); + self.guest_combat.insert(conn, guest_combat); play.modifiers = std::mem::replace(&mut self.applied_modifiers, modifiers); // What the guest shot is theirs (Projectile.setOwner). for p in self.creatures.projectiles.iter_mut().skip(projectiles) { @@ -1792,32 +1807,31 @@ impl Game<'_> { let drops = (g.game_mode != SPECTATOR) .then(|| [at[0], at[1] + g.eye_height() - 0.3, at[2]]); let cause = host.play.get_mut(&conn).and_then(|p| p.death_cause.take()); - let message = match cause { - // ServerPlayer.die: the death message, to everyone unless - // the player's team hides it. - Some(key) if self.ui.rules.bool("show_death_messages") => { - let text = horizon_client::text::Text::translate( - &key, - &[self.player_text(&name, uuid)], - ); - let message = horizon_client::lan::data::component_nbt(&text.component()); - let team = self.score.board.players_team(&name); - if horizon_client::scoreboard::own_death_message_shown(team) { - host.hub.system_message(message.clone()); - if self.options.dedicated.is_some() { - super::dedicated::log( - "INFO", - &format!("System chat: {}", text.plain()), - ); - } else if self.player.is_some() { - self.system_message(text); - } - } - message + let me = self.player_text(&name, uuid); + let text = match cause { + Some(key) => horizon_client::text::Text::translate(&key, &[me]), + None if at[1] < f64::from(horizon_client::chunk::MIN_Y - 64) => { + horizon_client::text::Text::translate("death.attack.outOfWorld", &[me]) } - Some(_) => literal(""), - None => literal(&format!("{name} died")), + None => self.guest_death_message(conn, me), }; + let message = horizon_client::lan::data::component_nbt(&text.component()); + // ServerPlayer.die: the message goes to everyone unless the + // player's team hides it, and always to the dying player. + if self.ui.rules.bool("show_death_messages") { + let team = self.score.board.players_team(&name); + if horizon_client::scoreboard::own_death_message_shown(team) { + host.hub.system_message(message.clone()); + if self.options.dedicated.is_some() { + super::dedicated::log( + "INFO", + &format!("System chat: {}", text.plain()), + ); + } else if self.player.is_some() { + self.system_message(text); + } + } + } host.hub.send_to( conn, &C::PlayerCombatKill(play::PlayerCombatKill { id, message }), @@ -1832,6 +1846,21 @@ impl Game<'_> { } } + /// `ServerPlayer.die`'s message from a guest's own `CombatTracker`, which + /// sits in `guest_combat` between turns. + fn guest_death_message( + &mut self, + conn: u32, + me: horizon_client::text::Text, + ) -> horizon_client::text::Text { + let mut tracker = self.guest_combat.remove(&conn).unwrap_or_default(); + std::mem::swap(&mut self.combat, &mut tracker); + let text = self.combat_death_message_for(me); + std::mem::swap(&mut self.combat, &mut tracker); + self.guest_combat.insert(conn, tracker); + text + } + /// `Player.tick`'s sleep counter, and `LivingEntity.tick`'s wake-up of a /// sleeper who died or whose bed is gone. fn guest_sleep_tick(&mut self, host: &mut LanHost, gi: usize) { @@ -2033,10 +2062,14 @@ impl Game<'_> { resources: game.motion, } .server_travel(); + // Landing damage from this travel reaches the tracker too. + events.append(&mut player.events); // A hurt since the last tick marked the velocity (Entity.markHurt): // ServerEntity.sendChanges sends it as it is after this travel. let marked = std::mem::take(&mut player.health.sync_velocity); - (events, marked.then_some(player.velocity)) + let velocity = marked.then_some(player.velocity); + game.record_combat(&events); + (events, velocity) }); let Some((events, marked)) = events else { return; diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 044e43835..53f7ea186 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -2285,12 +2285,10 @@ impl Game<'_> { } } C::PlayerCombatKill(p) => { - let lang = self - .remote - .as_ref() - .map(|r| r.lang.clone()) - .unwrap_or_default(); - let message = plain(&p.message, &lang); + // The component resolves in the client's current language, as + // the chat line does, not in the language the session joined + // with (`Remote.lang` is the vanilla server-language table). + let message = remote_text(&p.message).plain(); let me = self.remote.as_ref().is_some_and(|r| r.entity_id == p.id); if let Some(r) = self.remote.as_mut() { r.death_message = Some(message); diff --git a/crates/horizon-client/src/game/wither.rs b/crates/horizon-client/src/game/wither.rs index 91fe91b00..8084425ca 100644 --- a/crates/horizon-client/src/game/wither.rs +++ b/crates/horizon-client/src/game/wither.rs @@ -92,6 +92,8 @@ impl Game<'_> { match who { Some(Who::Player) => { if !self.creative { + // WitherSkull.onHitEntity's source: the wither launched it. + self.combat.projectile_owner = owner; self.mob_hits_player_kind( amount, [p.previous.x, p.previous.y, p.previous.z], From d9360338f655668351008f323e6181baf44ab7d0 Mon Sep 17 00:00:00 2001 From: Joansitoh Date: Sun, 25 Oct 2026 16:33:35 +0100 Subject: [PATCH 107/238] Show item lore in tooltips with vanilla purple italic styles. Stack tooltips skipped minecraft:lore entirely; draw the component lines as Text so colours and explicit non-italic spans survive. --- crates/horizon-client/src/game/beacon.rs | 7 +- crates/horizon-client/src/game/loom.rs | 9 +- crates/horizon-client/src/game/recipe_book.rs | 21 ++- crates/horizon-client/src/game/stations.rs | 134 ++++++++++++------ crates/horizon-client/src/item_tooltip.rs | 56 +++++++- 5 files changed, 167 insertions(+), 60 deletions(-) diff --git a/crates/horizon-client/src/game/beacon.rs b/crates/horizon-client/src/game/beacon.rs index c8a0bcba6..24ead3a3d 100644 --- a/crates/horizon-client/src/game/beacon.rs +++ b/crates/horizon-client/src/game/beacon.rs @@ -519,7 +519,12 @@ impl Game<'_> { self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1); } else if let Some(key) = tooltip { let text = self.translate(&key, &key); - self.draw_tooltip_lines(batch, &[(text, u32::MAX)], self.cursor.0, self.cursor.1); + self.draw_tooltip_lines( + batch, + &[(horizon_client::text::Text::literal(text), u32::MAX)], + self.cursor.0, + self.cursor.1, + ); } } /// `BeaconRenderer`: each section's rotating solid beam and its glow. diff --git a/crates/horizon-client/src/game/loom.rs b/crates/horizon-client/src/game/loom.rs index ef49c8cf1..3d69aaca6 100644 --- a/crates/horizon-client/src/game/loom.rs +++ b/crates/horizon-client/src/game/loom.rs @@ -218,7 +218,7 @@ impl Game<'_> { oy: f32, draws: &[ScreenDraw], background: bool, - ) -> Option> { + ) -> Option> { let atlas = &self.gui.atlas; let mut tooltip = None; let mut sheet: Option = None; @@ -261,7 +261,10 @@ impl Game<'_> { .. } => self.draw_banner_result(batch, ox + *x0 as f32, oy + *y0 as f32, base, layers), ScreenDraw::Tooltip { key } => { - tooltip = Some(vec![(self.translate(key, key), u32::MAX)]); + tooltip = Some(vec![( + horizon_client::text::Text::literal(self.translate(key, key)), + u32::MAX, + )]); } ScreenDraw::SpriteOf { id } => sheet = Some(id.clone()), ScreenDraw::Fill { @@ -314,7 +317,7 @@ impl Game<'_> { ox: f32, oy: f32, local: (f32, f32), - ) -> Option> { + ) -> Option> { let s = self.station.as_ref()?; let l = &s.loom; let dye = s.inputs[1] diff --git a/crates/horizon-client/src/game/recipe_book.rs b/crates/horizon-client/src/game/recipe_book.rs index d3c14ffc7..2536c3e03 100644 --- a/crates/horizon-client/src/game/recipe_book.rs +++ b/crates/horizon-client/src/game/recipe_book.rs @@ -1163,8 +1163,12 @@ impl Game<'_> { } /// Paints recipe book calls; returns the first tooltip set (vanilla keeps /// the first one a frame asks for). - fn paint_book(&mut self, batch: &mut HudBatch, calls: &[Drawn]) -> Option> { - let mut tooltip: Option> = None; + fn paint_book( + &mut self, + batch: &mut HudBatch, + calls: &[Drawn], + ) -> Option> { + let mut tooltip: Option> = None; for d in calls { let start = batch.vertices.len(); let atlas = &self.gui.atlas; @@ -1259,7 +1263,14 @@ impl Game<'_> { tooltip = Some( lines .iter() - .map(|l| (l.iter().map(|r| r.text.as_str()).collect(), u32::MAX)) + .map(|l| { + ( + horizon_client::text::Text::literal( + l.iter().map(|r| r.text.as_str()).collect::(), + ), + u32::MAX, + ) + }) .collect(), ); } @@ -1269,10 +1280,10 @@ impl Game<'_> { let mut lines = self.stack_tooltip(stack); if *more { lines.push(( - self.translate( + horizon_client::text::Text::literal(self.translate( "gui.recipebook.moreRecipes", "Right Click for More", - ), + )), u32::MAX, )); } diff --git a/crates/horizon-client/src/game/stations.rs b/crates/horizon-client/src/game/stations.rs index ffe56d699..fd7bd2642 100644 --- a/crates/horizon-client/src/game/stations.rs +++ b/crates/horizon-client/src/game/stations.rs @@ -1096,7 +1096,7 @@ impl Game<'_> { } let (ox, oy) = self.inventory_origin(batch); let local = (self.cursor.0 - ox, self.cursor.1 - oy); - let mut tooltip: Option> = None; + let mut tooltip: Option> = None; { let atlas = &self.gui.atlas; batch.fill( @@ -1345,16 +1345,19 @@ impl Game<'_> { let clue = self .translate("container.enchant.clue", "%s . . . ?") .replace("%s", &name); - let mut lines = vec![(clue, u32::MAX)]; + let mut lines = + vec![(horizon_client::text::Text::literal(clue), u32::MAX)]; if !self.creative { - lines.push((String::new(), u32::MAX)); + lines.push((horizon_client::text::Text::literal(""), u32::MAX)); if level < cost { lines.push(( - self.translate( - "container.enchant.level.requirement", - "Level Requirement: %s", - ) - .replace("%s", &cost.to_string()), + horizon_client::text::Text::literal( + self.translate( + "container.enchant.level.requirement", + "Level Requirement: %s", + ) + .replace("%s", &cost.to_string()), + ), rgba(255, 85, 85, 255), )); } else { @@ -1372,7 +1375,7 @@ impl Game<'_> { .replace("%s", &n.to_string()) }; lines.push(( - lapis, + horizon_client::text::Text::literal(lapis), if gold >= n as i32 { rgba(170, 170, 170, 255) } else { @@ -1391,7 +1394,10 @@ impl Game<'_> { ) .replace("%s", &n.to_string()) }; - lines.push((levels, rgba(170, 170, 170, 255))); + lines.push(( + horizon_client::text::Text::literal(levels), + rgba(170, 170, 170, 255), + )); } } tooltip = Some(lines); @@ -1500,10 +1506,10 @@ impl Game<'_> { ); if inside(local, 65.0, 46.0, 28.0, 21.0) { tooltip = Some(vec![( - self.translate( + horizon_client::text::Text::literal(self.translate( "container.upgrade.error_tooltip", "Item can't be upgraded this way", - ), + )), u32::MAX, )]); } @@ -1630,9 +1636,12 @@ impl Game<'_> { // ---------------------------------------------------------------- tooltips - /// `ItemStack.getTooltipLines` subset: name in rarity colour, potion - /// effects, enchantments (gray, curses red), stored enchantments, trims. - pub(super) fn stack_tooltip(&self, stack: &ItemStack) -> Vec<(String, u32)> { + /// `ItemStack.getTooltipLines` subset: name in rarity colour, lore, + /// potion effects, enchantments (gray, curses red), stored enchantments, trims. + pub(super) fn stack_tooltip( + &self, + stack: &ItemStack, + ) -> Vec<(horizon_client::text::Text, u32)> { let e: &Enchantments = &self.res.stations().enchantments; let props = e.props(&stack.item); let rarity = horizon_client::item_tooltip::rarity( @@ -1663,24 +1672,33 @@ impl Game<'_> { let display = horizon_client::item_tooltip::TooltipDisplay::for_stack(stack); let mut lines = Vec::new(); if display.name_visible(self.screen == Screen::Creative) { - lines.push((name, color)); + lines.push((horizon_client::text::Text::legacy(&name), color)); + } + // ItemLore.addToTooltip: after the name, before other component lines. + if display.shows("minecraft:lore") { + for text in horizon_client::item_tooltip::lore_texts(stack) { + lines.push((text, u32::MAX)); + } } let translate = |k: &str| e.translate(k).map(str::to_owned); + let push = |lines: &mut Vec<(horizon_client::text::Text, u32)>, s: String, c: u32| { + lines.push((horizon_client::text::Text::legacy(&s), c)); + }; // ItemContainerContents.addToTooltip. let hover_name = |s: &ItemStack| { s.custom_name() .map_or_else(|| self.pack.names.item(&s.item), str::to_owned) }; if display.shows("minecraft:container") { - for line in + for text in horizon_client::item_tooltip::container_lines(stack, &hover_name, &translate) { - lines.push((line, u32::MAX)); + push(&mut lines, text, u32::MAX); } } // Fireworks / FireworkExplosion.addToTooltip. - for line in horizon_client::item_tooltip::firework_lines(stack, &translate) { - lines.push((line, rgba(170, 170, 170, 255))); + for text in horizon_client::item_tooltip::firework_lines(stack, &translate) { + push(&mut lines, text, rgba(170, 170, 170, 255)); } if display.shows("minecraft:potion_contents") && (stack.item.ends_with("potion") || &*stack.item == "minecraft:tipped_arrow") @@ -1690,7 +1708,7 @@ impl Game<'_> { "minecraft:tipped_arrow" => 0.125, _ => 1.0, }; - for (line, cat) in brewing::tooltip(stack.potion(), scale, &|k| { + for (text, cat) in brewing::tooltip(stack.potion(), scale, &|k| { e.translate(k).map(str::to_owned) }) { let c = match cat { @@ -1698,18 +1716,19 @@ impl Game<'_> { brewing::Category::Harmful => rgba(255, 85, 85, 255), brewing::Category::Neutral => rgba(170, 170, 170, 255), }; - lines.push((line, c)); + push(&mut lines, text, c); } } if display.shows("minecraft:trim") && let Some((material, pattern)) = stack.data().and_then(|d| d.trim.clone()) { - lines.push(( + push( + &mut lines, e.translate("item.minecraft.smithing_template.upgrade") .unwrap_or("Upgrade:") .to_owned(), rgba(170, 170, 170, 255), - )); + ); let short = |s: &str| s.trim_start_matches("minecraft:").to_owned(); let p = e .translate(&format!("trim_pattern.minecraft.{}", short(&pattern))) @@ -1717,38 +1736,44 @@ impl Game<'_> { let m = e .translate(&format!("trim_material.minecraft.{}", short(&material))) .map_or_else(|| short(&material), str::to_owned); - lines.push((format!(" {p}"), rgba(170, 170, 170, 255))); - lines.push((format!(" {m}"), rgba(170, 170, 170, 255))); + push(&mut lines, format!(" {p}"), rgba(170, 170, 170, 255)); + push(&mut lines, format!(" {m}"), rgba(170, 170, 170, 255)); } if display.shows("minecraft:stored_enchantments") { for (text, curse) in e.tooltip(stack.stored_enchantments()) { - lines.push(( + push( + &mut lines, text, if curse { rgba(255, 85, 85, 255) } else { rgba(170, 170, 170, 255) }, - )); + ); } } if display.shows("minecraft:enchantments") { for (text, curse) in e.tooltip(stack.enchantments()) { - lines.push(( + push( + &mut lines, text, if curse { rgba(255, 85, 85, 255) } else { rgba(170, 170, 170, 255) }, - )); + ); } } // BannerPatternLayers.addToTooltip: up to six layer names. if display.shows("minecraft:banner_patterns") { for layer in horizon_client::banner::tooltip_layers(stack).iter().take(6) { let key = self.res.stations().banners.layer_key(layer); - lines.push((self.translate(&key, &key), rgba(170, 170, 170, 255))); + push( + &mut lines, + self.translate(&key, &key), + rgba(170, 170, 170, 255), + ); } } // Spawner.appendHoverText: what a spawner or trial spawner item spawns. @@ -1762,15 +1787,15 @@ impl Game<'_> { match block_entity_item::spawner_entity(stack) { Some(id) => { let key = format!("entity.{}", id.replacen(':', ".", 1)); - lines.push((self.translate(&key, &id), gray)); + push(&mut lines, self.translate(&key, &id), gray); } None => { - lines.push((String::new(), u32::MAX)); + push(&mut lines, String::new(), u32::MAX); let desc1 = self.translate("block.minecraft.spawner.desc1", "Interact with Spawn Egg:"); - lines.push((desc1, gray)); + push(&mut lines, desc1, gray); let desc2 = self.translate("block.minecraft.spawner.desc2", "Sets Mob Type"); - lines.push((format!(" {desc2}"), rgba(85, 85, 255, 255))); + push(&mut lines, format!(" {desc2}"), rgba(85, 85, 255, 255)); } } } @@ -1798,11 +1823,13 @@ impl Game<'_> { horizon_client::attack_strength::BASE_ATTACK_SPEED, ), ]; - lines.extend(horizon_client::item_tooltip::attribute_lines( + for (text, c) in horizon_client::item_tooltip::attribute_lines( &info.modifiers, Some(player_base), &translate, - )); + ) { + push(&mut lines, text, c); + } } if display.shows("minecraft:damage") && let Some(info) = self.res.items().get(&stack.item) @@ -1810,14 +1837,15 @@ impl Game<'_> { && stack.damage > 0 && self.options_advanced_tooltips() { - lines.push(( + push( + &mut lines, format!( "Durability: {} / {}", info.max_damage - stack.damage, info.max_damage ), u32::MAX, - )); + ); } // ItemStack.OP_NBT_WARNING, for a player with game master permission. if self.ui.registries.permission >= 2 && block_entity_item::op_warning(stack) { @@ -1832,7 +1860,11 @@ impl Game<'_> { "Do not use unless you know the exact contents!", ), ] { - lines.push((self.translate(key, fallback), rgba(255, 85, 85, 255))); + push( + &mut lines, + self.translate(key, fallback), + rgba(255, 85, 85, 255), + ); } } lines @@ -1863,20 +1895,27 @@ impl Game<'_> { self.settings.file.bool("advancedItemTooltips") } /// `TooltipRenderUtil`: dark box with the purple border, first line spaced. - pub(super) fn draw_tooltip_lines( + pub(super) fn draw_tooltip_lines>( &self, batch: &mut HudBatch, - lines: &[(String, u32)], + lines: &[(T, u32)], x: f32, y: f32, ) { if lines.is_empty() { return; } + let lines: Vec<_> = lines + .iter() + .map(|(text, color)| { + let text: horizon_client::text::Text = text.clone().into(); + (text.resolve_legacy(), *color) + }) + .collect(); let atlas = &self.gui.atlas; let w = lines .iter() - .map(|(t, _)| HudBatch::text_width(atlas, t)) + .map(|(t, _)| atlas.font.width_text(t)) .fold(0.0, f32::max); let h = if lines.len() == 1 { 8.0 @@ -1929,7 +1968,7 @@ impl Game<'_> { ); let mut ty = y; for (i, (text, color)) in lines.iter().enumerate() { - batch.text(atlas, text, x, ty, *color, true); + batch.text_styled(atlas, text, x, ty, *color, true); ty += if i == 0 { 12.0 } else { 10.0 }; } } @@ -1946,7 +1985,12 @@ impl Game<'_> { let lines = self.stack_tooltip(&stack); self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1); } else if let Some(text) = self.crafter_tooltip(slot) { - self.draw_tooltip_lines(batch, &[(text, u32::MAX)], self.cursor.0, self.cursor.1); + self.draw_tooltip_lines( + batch, + &[(horizon_client::text::Text::literal(text), u32::MAX)], + self.cursor.0, + self.cursor.1, + ); } } diff --git a/crates/horizon-client/src/item_tooltip.rs b/crates/horizon-client/src/item_tooltip.rs index ca6ff481b..6e51de391 100644 --- a/crates/horizon-client/src/item_tooltip.rs +++ b/crates/horizon-client/src/item_tooltip.rs @@ -487,23 +487,31 @@ pub fn lines( ) -> Vec { let mut lines = vec![name_line(stack, translate)]; if TooltipDisplay::for_stack(stack).shows("minecraft:lore") { - lines.extend(lore_lines(stack)); + lines.extend(lore_texts(stack).into_iter().map(|t| t.plain())); } lines.extend(detail_lines(stack, info, translate)); lines } -/// `LoreComponent.addToTooltip`: the saved component lines, as plain text. -fn lore_lines(stack: &ItemStack) -> Vec { +/// `ItemLore.addToTooltip`: each lore component under dark purple italic, with +/// the component's own styles (including explicit `false`) winning on children. +pub fn lore_texts(stack: &ItemStack) -> Vec { let Some(Tag::List(entries)) = stack.data().and_then(|d| d.other("minecraft:lore")) else { return Vec::new(); }; + let base = crate::text::Style { + color: Some(crate::text::DARK_PURPLE), + italic: Some(true), + ..Default::default() + }; entries .iter() .map(|entry| { - let json = crate::scoreboard::tag_to_json(entry); - let text = crate::component::to_text(&json, &mut |s| crate::text::Text::literal(s)); - text.plain() + let mut text = crate::entity_nbt::name_text(entry).resolve_legacy(); + for span in &mut text.spans { + span.style.inherit(&base); + } + text }) .collect() } @@ -512,6 +520,42 @@ fn lore_lines(stack: &ItemStack) -> Vec { mod tests { use super::*; + #[test] + fn lore_keeps_component_colours_under_dark_purple_italic() { + let mut stack = ItemStack::new("minecraft:stick", 1); + stack.data_mut().set_other( + "minecraft:lore", + Some(Tag::List(vec![ + Tag::compound([ + ("text", Tag::String("Gold line".into())), + ("color", Tag::String("gold".into())), + ("italic", Tag::Byte(0)), + ]), + Tag::String("Plain lore".into()), + ])), + ); + let lines = lore_texts(&stack); + assert_eq!(lines.len(), 2); + assert_eq!(lines[0].plain(), "Gold line"); + assert_eq!(lines[0].spans[0].style.color, Some(crate::text::GOLD)); + assert!(!lines[0].spans[0].style.is_italic()); + assert_eq!(lines[1].plain(), "Plain lore"); + assert_eq!( + lines[1].spans[0].style.color, + Some(crate::text::DARK_PURPLE) + ); + assert!(lines[1].spans[0].style.is_italic()); + assert!(TooltipDisplay::for_stack(&stack).shows("minecraft:lore")); + stack.data_mut().set_other( + "minecraft:tooltip_display", + Some(Tag::compound([( + "hidden_components", + Tag::List(vec![Tag::String("minecraft:lore".into())]), + )])), + ); + assert!(!TooltipDisplay::for_stack(&stack).shows("minecraft:lore")); + } + #[test] fn tooltip_display_controls_name_and_component_visibility() { let mut stack = ItemStack::new("minecraft:diamond_sword", 1); From ba4cf45aacb5564ae04bce109e466c48dea9f2eb Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:35:01 +0100 Subject: [PATCH 108/238] cargo horizon-mod: content mods (new --template content, build packs assets and data, check takes no code) - `new --template content`: a content mod with one block (Glowing Amethyst, copying vanilla's amethyst block) and its block state, model, item model, name, loot, recipe and pickaxe tag; `--template code` is the default. - `build` packs the mod's `assets/` and `data/` (read and checked as the game reads them) beside the component; a folder without `Cargo.toml` is a content mod and needs no compiler. - `check` takes a `.hmod` with no `mod.wasm`: "no code: content only, nothing to run" instead of the trial run, which refused it, so the registry can publish content mods. - The guide (content mods, make a mod) and docs/mods.md, docs/api/mods.md say so. Co-Authored-By: Claude Opus 5.5 --- crates/cargo-horizon-mod/src/check.rs | 16 +- crates/cargo-horizon-mod/src/main.rs | 168 +++++++++++++++--- .../template/content/block.json | 4 + .../template/content/blockstate.json | 5 + .../template/content/horizon-mod.toml | 10 ++ .../template/content/item.json | 3 + .../template/content/lang.json | 3 + .../template/content/loot.json | 11 ++ .../template/content/model.json | 4 + .../template/content/pickaxe.json | 3 + .../template/content/recipe.json | 6 + docs/api/mods.md | 3 +- docs/mods.md | 8 +- website/docs/content-mods.md | 20 ++- website/docs/make-a-mod.md | 2 +- 15 files changed, 233 insertions(+), 33 deletions(-) create mode 100644 crates/cargo-horizon-mod/template/content/block.json create mode 100644 crates/cargo-horizon-mod/template/content/blockstate.json create mode 100644 crates/cargo-horizon-mod/template/content/horizon-mod.toml create mode 100644 crates/cargo-horizon-mod/template/content/item.json create mode 100644 crates/cargo-horizon-mod/template/content/lang.json create mode 100644 crates/cargo-horizon-mod/template/content/loot.json create mode 100644 crates/cargo-horizon-mod/template/content/model.json create mode 100644 crates/cargo-horizon-mod/template/content/pickaxe.json create mode 100644 crates/cargo-horizon-mod/template/content/recipe.json diff --git a/crates/cargo-horizon-mod/src/check.rs b/crates/cargo-horizon-mod/src/check.rs index 0294bec98..8a25d1a24 100644 --- a/crates/cargo-horizon-mod/src/check.rs +++ b/crates/cargo-horizon-mod/src/check.rs @@ -21,6 +21,8 @@ pub struct Report { pub sha256: String, pub size: u64, pub signed: bool, + /// A mod has code to run (a content mod may have none). + pub code: bool, /// A mod's component imports. pub imports: Vec, /// A mod's commands, each run once. @@ -49,6 +51,7 @@ impl Report { "sha256": self.sha256, "size": self.size, "signed": self.signed, + "code": self.code, "imports": self.imports, "commands": self.commands, "output": self.output, @@ -87,7 +90,9 @@ impl Report { for r in &f.requires { out += &format!(" needs {} {} ({})\n", r.id, r.version, &r.sha256[..12]); } - if self.kind == Kind::Mod { + if self.kind == Kind::Mod && !self.code { + out += " no code: content only, nothing to run\n"; + } else if self.kind == Kind::Mod { let commands = if self.commands.is_empty() { String::new() } else { @@ -142,6 +147,7 @@ pub fn check( sha256: index::sha256_file(path)?, size, signed, + code: false, imports: Vec::new(), commands: Vec::new(), output: Vec::new(), @@ -153,10 +159,14 @@ pub fn check( } /// Runs a mod in the sandbox, as the game does, from a scratch copy so its -/// storage lands in a folder removed afterwards. +/// storage lands in a folder removed afterwards. A content mod has no code to +/// run: `inspect` already read its content as the game does. fn trial_run(path: &Path, report: &mut Report) -> Result<(), String> { let package = Package::read(path)?; - let wasm = package.wasm.as_deref().ok_or("the .hmod has no mod.wasm")?; + let Some(wasm) = package.wasm.as_deref() else { + return Ok(()); + }; + report.code = true; report.imports = component_imports(wasm)?; package .manifest diff --git a/crates/cargo-horizon-mod/src/main.rs b/crates/cargo-horizon-mod/src/main.rs index 925633930..a4585f680 100644 --- a/crates/cargo-horizon-mod/src/main.rs +++ b/crates/cargo-horizon-mod/src/main.rs @@ -1,14 +1,16 @@ //! `cargo horizon-mod`: make Horizon mods in Rust (`docs/mods.md`). //! -//! - `new [--name "Name"] [--sdk-path ]`: a mod crate from the template. -//! - `build [--debug]`: the `.hmod` in `target/horizon-mod/`. +//! - `new [--template code|content] [--name "Name"] [--sdk-path ]`: +//! a mod from a template: a Rust crate, or a content mod (no code). +//! - `build [--debug]`: the `.hmod` in `target/horizon-mod/`, with the mod's +//! `assets/` and `data/`; a folder without `Cargo.toml` is content only. //! - `run [--game ] [-- ]`: build, then play with //! the mod; run `build` again and the game reloads it. //! - `check [--json]`: what the registry checks of a submission //! (`.hmod`, `.hpack`, resource pack `.zip`), with a trial run for a mod. //! - `registry build|review|check|list`: the registry repository's CI. use horizon_mods::{ - Manifest, Package, + Capability, Content, Manifest, Package, manifest::{MANIFEST, component_imports, componentize}, }; mod check; @@ -36,7 +38,7 @@ fn main() -> ExitCode { Some("run") => run(&args[1..]), Some("check") => check(&args[1..]), Some("registry") => registry::main(&args[1..]), - _ => Err("usage: cargo horizon-mod new | build [--debug] | run [--game ] [-- ] | check [--kind ] [--key --signature ] [--json] | registry build|review|check|list".into()), + _ => Err("usage: cargo horizon-mod new [--template code|content] | build [--debug] | run [--game ] [-- ] | check [--kind ] [--key --signature ] [--json] | registry build|review|check|list".into()), }; match result { Ok(()) => ExitCode::SUCCESS, @@ -90,19 +92,68 @@ fn new(args: &[String]) -> Result<(), String> { .replace("{name}", &name) .replace("{sdk}", &sdk) }; - let files = [ + let code = [ ("Cargo.toml", include_str!("../template/Cargo.toml.in")), (MANIFEST, include_str!("../template/horizon-mod.toml")), ("src/lib.rs", include_str!("../template/src/lib.rs")), (".gitignore", include_str!("../template/gitignore")), ]; - for (file, text) in files { + // A block copying a vanilla one, with its model, name, loot, recipe and tag. + let content = [ + ( + MANIFEST, + include_str!("../template/content/horizon-mod.toml"), + ), + ( + "data/{id}/horizon/block/glowing_amethyst.json", + include_str!("../template/content/block.json"), + ), + ( + "assets/{id}/blockstates/glowing_amethyst.json", + include_str!("../template/content/blockstate.json"), + ), + ( + "assets/{id}/models/block/glowing_amethyst.json", + include_str!("../template/content/model.json"), + ), + ( + "assets/{id}/items/glowing_amethyst.json", + include_str!("../template/content/item.json"), + ), + ( + "assets/{id}/lang/en_us.json", + include_str!("../template/content/lang.json"), + ), + ( + "data/{id}/loot_table/blocks/glowing_amethyst.json", + include_str!("../template/content/loot.json"), + ), + ( + "data/{id}/recipe/glowing_amethyst.json", + include_str!("../template/content/recipe.json"), + ), + ( + "data/minecraft/tags/block/mineable/pickaxe.json", + include_str!("../template/content/pickaxe.json"), + ), + (".gitignore", include_str!("../template/gitignore")), + ]; + let files: &[(&str, &str)] = match flag(args, "--template").unwrap_or("code") { + "code" => &code, + "content" => &content, + other => return Err(format!("no template {other}: code or content")), + }; + for &(file, text) in files { + let file = fill(file); + let file = file.as_str(); let to = dir.join(file); std::fs::create_dir_all(to.parent().expect("a file in the mod")) .map_err(|e| e.to_string())?; std::fs::write(&to, fill(text)).map_err(|e| format!("{}: {e}", to.display()))?; } - Manifest::parse(&std::fs::read_to_string(dir.join(MANIFEST)).map_err(|e| e.to_string())?)?; + let manifest = + Manifest::parse(&std::fs::read_to_string(dir.join(MANIFEST)).map_err(|e| e.to_string())?)?; + Content::read(dir, &manifest)?; println!( "Created {} (mod id {id}). Next: cd {path} && cargo horizon-mod run", dir.display() @@ -110,14 +161,53 @@ fn new(args: &[String]) -> Result<(), String> { Ok(()) } -/// Builds the crate in the current folder and packs `target/horizon-mod/.hmod`. +/// Builds the mod in the current folder and packs `target/horizon-mod/.hmod`: +/// the manifest, the crate's component (none without a `Cargo.toml`: a +/// content mod) and the files in `assets/` and `data/`. fn build(release: bool) -> Result { - let manifest_text = std::fs::read_to_string(MANIFEST) + build_in(Path::new("."), release) +} + +fn build_in(root: &Path, release: bool) -> Result { + let manifest_text = std::fs::read_to_string(root.join(MANIFEST)) .map_err(|e| format!("{MANIFEST}: {e} (run this in a mod's folder)"))?; let manifest = Manifest::parse(&manifest_text)?; + let (wasm, target_dir) = if root.join("Cargo.toml").exists() { + let (wasm, target) = compile(root, release)?; + // The game refuses a component importing capabilities the manifest lacks. + manifest.check_imports(component_imports(&wasm)?.iter().map(String::as_str))?; + (Some(wasm), target) + } else if manifest.capabilities.contains(&Capability::Registries) { + (None, root.join("target")) + } else { + return Err(format!( + "no Cargo.toml: only a content mod (capability registries in {MANIFEST}) has no code" + )); + }; + // The same checks as the game's, and the files in the order it lists them. + let content = Content::read(root, &manifest)?; + let mut reader = content.reader()?; + let mut files = Vec::with_capacity(content.files.len()); + for name in &content.files { + files.push((name.as_str(), reader.read(name)?)); + } + let files: Vec<(&str, &[u8])> = files.iter().map(|(n, b)| (*n, b.as_slice())).collect(); + let dir = target_dir.join("horizon-mod"); + std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?; + let path = dir.join(format!("{}.hmod", manifest.id)); + // Write then rename, so a running game never reads half a file. + let partial = path.with_extension("hmod.partial"); + let bytes = Package::write_files(&manifest_text, wasm.as_deref(), &files)?; + std::fs::write(&partial, bytes).map_err(|e| e.to_string())?; + std::fs::rename(&partial, &path).map_err(|e| e.to_string())?; + Ok(path) +} + +/// Builds the crate for WebAssembly: its component, and Cargo's target folder. +fn compile(root: &Path, release: bool) -> Result<(Vec, PathBuf), String> { let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".into()); let mut cmd = Command::new(cargo); - cmd.args([ + cmd.current_dir(root).args([ "build", "--lib", "--target", @@ -145,17 +235,8 @@ fn build(release: bool) -> Result { .rfind(|f| f.extension().is_some_and(|e| e == "wasm")) .ok_or("the build made no .wasm: is the crate a cdylib?")?; let wasm = componentize(&std::fs::read(&core).map_err(|e| e.to_string())?)?; - // The game refuses a component importing capabilities the manifest lacks. - manifest.check_imports(component_imports(&wasm)?.iter().map(String::as_str))?; - let target_dir = core.ancestors().nth(3).ok_or("unexpected target folder")?; - let dir = target_dir.join("horizon-mod"); - std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?; - let path = dir.join(format!("{}.hmod", manifest.id)); - // Write then rename, so a running game never reads half a file. - let partial = path.with_extension("hmod.partial"); - std::fs::write(&partial, Package::write(&manifest_text, &wasm)?).map_err(|e| e.to_string())?; - std::fs::rename(&partial, &path).map_err(|e| e.to_string())?; - Ok(path) + let target = core.ancestors().nth(3).ok_or("unexpected target folder")?; + Ok((wasm, target.to_path_buf())) } /// `check `: the registry's checks of a submission, printed; fails @@ -237,4 +318,49 @@ mod tests { ); std::fs::remove_dir_all(&dir).unwrap(); } + + #[test] + fn the_content_template_builds_and_checks_with_no_code() { + let dir = std::env::temp_dir().join(format!("horizon-mod-content-{}", std::process::id())); + let path = dir.join("gems"); + let new_args = [ + path.display().to_string(), + "--template".into(), + "content".into(), + ]; + new(&new_args).unwrap(); + assert!(!path.join("Cargo.toml").exists()); + let hmod = build_in(&path, true).unwrap(); + assert_eq!(hmod, path.join("target/horizon-mod/gems.hmod")); + let package = Package::read(&hmod).unwrap(); + assert!(package.wasm.is_none()); + let content = Content::read(&hmod, &package.manifest).unwrap(); + assert!( + content + .files + .contains(&"data/gems/horizon/block/glowing_amethyst.json".into()) + ); + assert_eq!(content.files.len(), 8, "{:?}", content.files); + let report = check::check(&hmod, check::kind_of(&hmod).unwrap(), None, None).unwrap(); + assert!(!report.code); + assert!( + report.text().contains("no code: content only"), + "{}", + report.text() + ); + // Without registries, a mod needs code. + let manifest = std::fs::read_to_string(path.join(MANIFEST)).unwrap(); + let manifest = manifest.replace("[\"registries\"]", "[]"); + std::fs::write(path.join(MANIFEST), manifest).unwrap(); + assert!(build_in(&path, true).unwrap_err().contains("no Cargo.toml")); + assert!( + new(&[ + dir.join("x").display().to_string(), + "--template".into(), + "lua".into() + ]) + .is_err() + ); + std::fs::remove_dir_all(&dir).unwrap(); + } } diff --git a/crates/cargo-horizon-mod/template/content/block.json b/crates/cargo-horizon-mod/template/content/block.json new file mode 100644 index 000000000..ea68a1a27 --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/block.json @@ -0,0 +1,4 @@ +{ + "template": "minecraft:amethyst_block", + "properties": { "hardness": 2.0, "light": 7 } +} diff --git a/crates/cargo-horizon-mod/template/content/blockstate.json b/crates/cargo-horizon-mod/template/content/blockstate.json new file mode 100644 index 000000000..ea472666e --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/blockstate.json @@ -0,0 +1,5 @@ +{ + "variants": { + "": { "model": "{id}:block/glowing_amethyst" } + } +} diff --git a/crates/cargo-horizon-mod/template/content/horizon-mod.toml b/crates/cargo-horizon-mod/template/content/horizon-mod.toml new file mode 100644 index 000000000..4f133a905 --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/horizon-mod.toml @@ -0,0 +1,10 @@ +id = "{id}" +name = "{name}" +version = "0.1.0" +description = "" +authors = [] +# A content mod: blocks and items from data/ and assets/, no code (API 0.2). +api = "0.2" +side = "both" +# registries: "May add blocks and items to your worlds". +capabilities = ["registries"] diff --git a/crates/cargo-horizon-mod/template/content/item.json b/crates/cargo-horizon-mod/template/content/item.json new file mode 100644 index 000000000..411bc4499 --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/item.json @@ -0,0 +1,3 @@ +{ + "model": { "type": "minecraft:model", "model": "{id}:block/glowing_amethyst" } +} diff --git a/crates/cargo-horizon-mod/template/content/lang.json b/crates/cargo-horizon-mod/template/content/lang.json new file mode 100644 index 000000000..74baddab8 --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/lang.json @@ -0,0 +1,3 @@ +{ + "block.{id}.glowing_amethyst": "Glowing Amethyst" +} diff --git a/crates/cargo-horizon-mod/template/content/loot.json b/crates/cargo-horizon-mod/template/content/loot.json new file mode 100644 index 000000000..898111f1b --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/loot.json @@ -0,0 +1,11 @@ +{ + "type": "minecraft:block", + "pools": [ + { + "condition": { "type": "minecraft:survives_explosion" }, + "entries": [{ "type": "minecraft:item", "name": "{id}:glowing_amethyst" }], + "rolls": 1 + } + ], + "random_sequence": "{id}:blocks/glowing_amethyst" +} diff --git a/crates/cargo-horizon-mod/template/content/model.json b/crates/cargo-horizon-mod/template/content/model.json new file mode 100644 index 000000000..5710fecb1 --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/model.json @@ -0,0 +1,4 @@ +{ + "parent": "minecraft:block/cube_all", + "textures": { "all": "minecraft:block/amethyst_block" } +} diff --git a/crates/cargo-horizon-mod/template/content/pickaxe.json b/crates/cargo-horizon-mod/template/content/pickaxe.json new file mode 100644 index 000000000..8725edebd --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/pickaxe.json @@ -0,0 +1,3 @@ +{ + "values": ["{id}:glowing_amethyst"] +} diff --git a/crates/cargo-horizon-mod/template/content/recipe.json b/crates/cargo-horizon-mod/template/content/recipe.json new file mode 100644 index 000000000..5edb03d94 --- /dev/null +++ b/crates/cargo-horizon-mod/template/content/recipe.json @@ -0,0 +1,6 @@ +{ + "type": "minecraft:crafting_shapeless", + "category": "building", + "ingredients": ["minecraft:amethyst_block", "minecraft:glowstone_dust"], + "result": { "id": "{id}:glowing_amethyst" } +} diff --git a/docs/api/mods.md b/docs/api/mods.md index 3e6108c70..5c323a0e1 100644 --- a/docs/api/mods.md +++ b/docs/api/mods.md @@ -19,7 +19,7 @@ the player before the mod is turned on, and checked against the component's real | Capabilities | 5: `game-read`, `chat`, `hud`, `storage`, `registries` | | Manifest `api` lines served | 2: `0.1` (code), `0.2` (content) | | SDK | the `Mod` trait (5 methods), `Registration` (4 builders), `export_mod!`, log helpers | -| `cargo horizon-mod` | `new`, `build`, `run` | +| `cargo horizon-mod` | `new` (templates `code` and `content`), `build`, `run`, `check` | | Example mods | `coords-hud` (code), `ruby-content` (content) | ## Guide @@ -58,6 +58,7 @@ scripting runtime in the game. | Way in | For | Status | Template and example | | --- | --- | --- | --- | | Rust | most mods; the fastest | first-class: the SDK, `cargo horizon-mod` | `cargo horizon-mod new`, `coords-hud` | +| Data only | blocks and items with no code | first-class: content mods (API 0.2) | `cargo horizon-mod new --template content`, `ruby-content` | | Lua scripts | people who don't want a compiler | planned: a script mod whose code is a `.lua` file, run by Horizon's own interpreter mod | planned, with the same example as Rust | | C, C++, Zig | authors of those languages | to evaluate: bindings generated from the WIT, built without WASI | one each if it passes | | Go (TinyGo), AssemblyScript, JavaScript / TypeScript | authors of those languages | to evaluate: each needs its toolchain to emit a component without WASI imports, or an interpreter mod as for Lua | one each if it passes | diff --git a/docs/mods.md b/docs/mods.md index bac207f60..3a4473032 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -16,7 +16,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | SDK for mod authors | `crates/horizon-mod-sdk` | 0.1 | | Example mod | `crates/horizon-mod-sdk/examples/coords-hud` | coordinates, facing and time HUD, `/where`, `/coords` | | Runtime | `crates/horizon-mods` | wasmtime 36 LTS, component model | -| Authoring tool | `crates/cargo-horizon-mod` | `cargo horizon-mod new / build / run / check`, and the registry's CI (`registry build / review / list`) | +| Authoring tool | `crates/cargo-horizon-mod` | `cargo horizon-mod new [--template code\|content] / build / run / check`, and the registry's CI (`registry build / review / list`) | | In the game | `crates/horizon-client/src/game/mods.rs`, Options > Mods | client mods | | Launcher mod browser (Modrinth, CurseForge with a key) | [mod-browser.md](mod-browser.md) | resource packs, modpacks | | Registry index (`index.json`, `.hpack`), signatures | `horizon_mods::index`, [api/mod-index.md](api/mod-index.md) | schema 1; launcher and website next | @@ -215,6 +215,7 @@ Benchmarks (`--benchmark`) never load mods. rustup target add wasm32-unknown-unknown cargo install --git https://github.com/ludovic111/horizon cargo-horizon-mod cargo horizon-mod new my-mod # Cargo.toml, horizon-mod.toml, src/lib.rs + # (--template content: a content mod, no code) cd my-mod cargo horizon-mod run # build, then start the game with it cargo horizon-mod build # again after an edit: the running game reloads it @@ -292,7 +293,10 @@ permission. ### Content mods (API 0.2) A mod with `api = "0.2"` and capability `registries` may carry `assets/` and -`data/` in the `.hmod`, and needs no `mod.wasm` if it has no code. It adds +`data/` in the `.hmod`, and needs no `mod.wasm` if it has no code +(`cargo horizon-mod new --template content` starts one; `build` in a folder +with no `Cargo.toml` packs the manifest, `assets/` and `data/`, and `check` +reads it without a trial run). It adds blocks and items by declaring them in `data//horizon/block/*.json` and `data//horizon/item/*.json`, each copying a vanilla template ([modpacks.md](modpacks.md#registries-02-data-driven-blocks-and-items)): diff --git a/website/docs/content-mods.md b/website/docs/content-mods.md index 1ad8cd5fb..9504bceb6 100644 --- a/website/docs/content-mods.md +++ b/website/docs/content-mods.md @@ -4,7 +4,7 @@ A content mod adds blocks and items to the game with data files only: no Rust, n ## What a content mod is -A folder (while you work on it) or a `.hmod` zip (to share it) holding a manifest, `assets/` and `data/`. This is the SDK's [`ruby-content`](https://github.com/ludovic111/horizon/tree/main/crates/horizon-mod-sdk/examples/ruby-content) example, a ruby gem and a block of ruby: +A folder (while you work on it) or a `.hmod` file (to share it) holding a manifest, `assets/` and `data/`. This is the SDK's [`ruby-content`](https://github.com/ludovic111/horizon/tree/main/crates/horizon-mod-sdk/examples/ruby-content) example, a ruby gem and a block of ruby: ``` ruby/ @@ -98,17 +98,27 @@ A mod made from a Java mod whose files use other names lists them in its manifes - Unknown keys in a block or item file are refused, and the mod stays off with the reason on its row. - Content mods work in single player today. Playing them together on a server is planned. +## Start one + +```sh +cargo horizon-mod new my-blocks --template content +``` + +makes a content mod with one block, Glowing Amethyst: an amethyst block that gives light, with its block state, model (using vanilla's amethyst texture), item model, name, loot table, a recipe (an amethyst block and glowstone dust) and the pickaxe tag. Rename and copy its files to add your own. + ## Try it and share it While you work, give the game the folder as a dev mod ([how](/docs/make-a-mod#see-it-in-the-game)): `--dev-mod "/path/to/ruby"`. The first start with a new set of mods takes a little longer while the game adds their content to its tables; after a change to the files, start the game again. -To share it, zip the folder's contents, with `horizon-mod.toml` at the top of the zip, and name the file `.hmod`: +To share it, build the `.hmod` in the mod's folder: ```sh -cd ruby -zip -r ../ruby.hmod horizon-mod.toml assets data +cargo horizon-mod build +cargo horizon-mod check target/horizon-mod/ruby.hmod ``` -Players put that file in their `horizon-mods` folder and turn it on in **Options, Mods...**. The registry doesn't take content-only mods yet: its checks (`cargo horizon-mod check`) expect code in the file. That is coming next. +A folder without a `Cargo.toml` is a content mod: `build` checks its files as the game will and packs the manifest, `assets/` and `data/` into `target/horizon-mod/.hmod`, with no compiler involved. `check` reads it as the registry does and says "no code: content only, nothing to run". Players put that file in their `horizon-mods` folder and turn it on in **Options, Mods...**, and you can [publish it](/docs/publish-a-mod) like any mod. + +A mod can have both: a `Cargo.toml` with code, and `assets/` and `data/` with content. `build` packs both. Next: [test it](/docs/test-a-mod). diff --git a/website/docs/make-a-mod.md b/website/docs/make-a-mod.md index 94c3deca2..89f2d8bcd 100644 --- a/website/docs/make-a-mod.md +++ b/website/docs/make-a-mod.md @@ -41,7 +41,7 @@ cargo horizon-mod new my-mod cd my-mod ``` -`new` makes a folder with a working mod in it: a `/hello` command that answers in chat. `--name "My Mod"` sets the name players see. The folder holds: +`new` makes a folder with a working mod in it: a `/hello` command that answers in chat. `--name "My Mod"` sets the name players see, and `--template content` makes a [content mod](/docs/content-mods) instead, with no code. The folder holds: ``` my-mod/ From c6405610008b0c779ffe281a6cdab34c67f9199a Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:56:26 +0100 Subject: [PATCH 109/238] Launcher: the registry pack tests build file URLs with forward slashes, so they pass on Windows A Windows temp path's backslashes went into the test index's JSON as escapes, so the index failed to parse and the Windows launcher release stopped at its tests. Co-Authored-By: Claude Opus 5.5 --- launcher/src/browser/horizon/pack.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/launcher/src/browser/horizon/pack.rs b/launcher/src/browser/horizon/pack.rs index 73dcd0cc1..a2cc4d079 100644 --- a/launcher/src/browser/horizon/pack.rs +++ b/launcher/src/browser/horizon/pack.rs @@ -208,6 +208,8 @@ mod tests { zip.write_all(b"renderDistance:8\n").unwrap(); zip.finish().unwrap(); let size = |p: &Path| fs::metadata(p).unwrap().len(); + // Forward slashes: a Windows path's backslashes would be escapes in the JSON. + let url = |p: &Path| p.display().to_string().replace('\\', "/"); let text = format!( r#"{{"schema": 1, "projects": [ {{"id": "coords_hud", "kind": "mod", "name": "Coordinates HUD", "summary": "", "license": "MIT", @@ -219,9 +221,9 @@ mod tests { "requires": [{{"id": "coords_hud", "version": "0.1.0", "sha256": "{hmod_sha}"}}], "file": {{"name": "cozy-1.0.0.hpack", "url": "file://{}", "size": {}, "sha256": "{}"}}}}]}} ]{extra}}}"#, - hmod.display(), + url(&hmod), size(&hmod), - hpack.display(), + url(&hpack), size(&hpack), idx::sha256_file(&hpack).unwrap() ); From af7a685b5c5871764b9c7b16d948b04dd2cdb867 Mon Sep 17 00:00:00 2001 From: socksprox Date: Sun, 25 Oct 2026 17:09:25 +0100 Subject: [PATCH 110/238] =?UTF-8?q?Mod=20API:=20capability=20`ui`=20?= =?UTF-8?q?=E2=80=94=20mod-owned=20screens=20and=20settings=20pages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit New WIT interface `ui` (text, item and list widgets; open-screen, close-screen, set-settings) plus a `ui-events` export (select, closed, settings-opened), in a second world `horizon-ui-mod` so mods built before it keep loading. The game lays out and draws the widgets with its own menus' code: a screen the mod owns (Esc/Done to leave) and a Settings button on the mod's Options > Mods row that opens its page. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- AGENTS.md | 2 +- Cargo.lock | 7 + Cargo.toml | 2 +- crates/horizon-client/src/game.rs | 11 + crates/horizon-client/src/game/agent.rs | 1 + crates/horizon-client/src/game/control.rs | 1 + crates/horizon-client/src/game/mods.rs | 336 +++++++++++++++++- crates/horizon-client/src/game/options.rs | 1 + crates/horizon-client/src/game/screens.rs | 3 +- .../horizon-client/src/game/startup_menu.rs | 2 + crates/horizon-client/src/mod_list.rs | 4 + crates/horizon-client/src/options_ui.rs | 40 ++- crates/horizon-mod-sdk/src/lib.rs | 36 +- crates/horizon-mod-sdk/wit/horizon.wit | 92 +++++ crates/horizon-mods/src/manifest.rs | 9 +- crates/horizon-mods/src/runtime.rs | 200 ++++++++++- crates/horizon-mods/tests/sandbox.rs | 49 +++ crates/horizon-mods/tests/ui_guest/Cargo.toml | 16 + crates/horizon-mods/tests/ui_guest/src/lib.rs | 66 ++++ docs/api/mods-reference.md | 88 +++++ docs/mods.md | 4 +- 21 files changed, 936 insertions(+), 34 deletions(-) create mode 100644 crates/horizon-mods/tests/ui_guest/Cargo.toml create mode 100644 crates/horizon-mods/tests/ui_guest/src/lib.rs diff --git a/AGENTS.md b/AGENTS.md index a72c6723a..2d883b33b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -82,7 +82,7 @@ python3 tools/agent_control_e2e.py # horizon-ctl / MCP end to end ~25, a file of the client lib ~103, of `horizon-world` ~103–109, of `horizon-core` ~119. Plan for it. - Rust integration tests: `crates/horizon-client/tests/` (`lan_bot.rs`, `generation_saves.rs`, `streaming_meshes.rs`). - **macOS:** `cargo build/test --workspace` may fail to link the two wasm guest cdylibs (undefined - `horizon:mods/events` symbols) until #1446 lands: add `--exclude coords-hud --exclude horizon-mods-test-guest`. + `horizon:mods/events` symbols) until #1446 lands: add `--exclude coords-hud --exclude horizon-mods-test-guest --exclude horizon-mods-test-ui-guest`. Their real check is `cargo test -p horizon-mods` (builds them for `wasm32-unknown-unknown`). - Linux needs `libasound2-dev libudev-dev`; `--no-default-features` drops audio and controllers (the server build). - Run and drive the game (starts the `horizon-client` beside `horizon-ctl`; other arguments go to the game): diff --git a/Cargo.lock b/Cargo.lock index 70c3c831c..6945e09cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1352,6 +1352,13 @@ dependencies = [ "horizon-mod-sdk", ] +[[package]] +name = "horizon-mods-test-ui-guest" +version = "0.1.0" +dependencies = [ + "horizon-mod-sdk", +] + [[package]] name = "horizon-port-pack" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 294e93aa9..4076b9079 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] exclude = ["launcher", "crates/horizon-telemetry-server", "crates/horizon-mod-registry", "third_party/wgpu-hal"] -members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-shaders", "crates/horizon-shader-api", "crates/horizon-release-sign", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"] +members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/horizon-mods/tests/ui_guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-shaders", "crates/horizon-shader-api", "crates/horizon-release-sign", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"] resolver = "2" [workspace.package] diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 699468651..eabbc22b8 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -482,6 +482,9 @@ enum Screen { CommandBlock, /// WinScreen: the end poem and the credits (game/win.rs). Win, + /// A `ui` mod's own screen, or its settings page from Options > Mods + /// (game/mods.rs). + Mod, } struct Mining { pos: [i32; 3], @@ -5047,6 +5050,7 @@ impl Game<'_> { self.draw_creative(&mut batch); } Screen::Chat => self.draw_chat_screen(&mut batch), + Screen::Mod => self.draw_mod_screen(&mut batch), Screen::Playing | Screen::Options | Screen::Win => {} } self.draw_toasts(&mut batch); @@ -10363,6 +10367,7 @@ impl Game<'_> { Screen::CommandBlock => self.close_command_block(), Screen::Chat => self.close_chat(), Screen::Win => self.close_win_screen()?, + Screen::Mod => self.close_mod_screen(true), }, KeyCode::Digit1 | KeyCode::Digit2 @@ -11009,6 +11014,9 @@ impl ApplicationHandler for Game<'_> { if self.screen == Screen::Options && d != 0.0 { self.options_scroll(d); } + if self.screen == Screen::Mod && d != 0.0 { + self.mod_screen_scroll(d); + } if self.screen == Screen::Creative && d != 0.0 { self.creative_screen.wheel(f64::from(d), &self.pack.names); } @@ -11231,6 +11239,9 @@ impl ApplicationHandler for Game<'_> { .map_or([1, 1], |r| [r.config.width, r.config.height]); self.stats_click(size); } + Screen::Mod if pressed && button == MouseButton::Left => { + self.mod_screen_click() + } _ => {} } } diff --git a/crates/horizon-client/src/game/agent.rs b/crates/horizon-client/src/game/agent.rs index f6b68aa74..bed331c32 100644 --- a/crates/horizon-client/src/game/agent.rs +++ b/crates/horizon-client/src/game/agent.rs @@ -416,6 +416,7 @@ impl Game<'_> { Screen::Friends => "friends", Screen::CommandBlock => "command-block", Screen::Win => "win", + Screen::Mod => "mod", } } pub(super) fn inventory_counts(&self) -> BTreeMap { diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index a0d8987d8..ffe05b518 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -799,6 +799,7 @@ impl Game<'_> { Screen::Friends => "friends", Screen::CommandBlock => "command-block", Screen::Win => "win", + Screen::Mod => "mod", } } diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs index 572f9fe2b..688e29da1 100644 --- a/crates/horizon-client/src/game/mods.rs +++ b/crates/horizon-client/src/game/mods.rs @@ -20,10 +20,13 @@ use { horizon_client::{ extras::Corner, extras_hud, + items::ItemStack, mod_list::Installed, + options_ui::Page, text::{self, Text}, + ui, }, - horizon_mods::{Clock, Mods, Output, Player, Snapshot, Status}, + horizon_mods::{Clock, Mods, Output, Player, Snapshot, Status, UiScreen, UiWidget}, std::time::Instant, }; @@ -39,6 +42,23 @@ pub(super) struct ModsState { /// The chat's command tree while a running mod owns commands. #[cfg(feature = "mods")] tree: Option, + /// A `ui` mod's screen or settings page while it is up. + #[cfg(feature = "mods")] + screen: Option, +} + +/// A `ui` mod's own screen, or its settings page from Options > Mods: the +/// widgets the mod pushed, laid out and drawn with the menus' own widgets. +#[cfg(feature = "mods")] +struct ModScreen { + /// The running mod's index in `mods.mods.entries()`. + owner: usize, + /// Opened from Options > Mods: Done and Esc return there. + settings: bool, + /// What the mod's screen replaced (Playing, Chat, ...), to restore. + under: Screen, + /// One scroll per `list` widget, in widget order. + scrolls: Vec, } impl ModsState { /// The chat's command tree: vanilla's, plus the running mods' commands. @@ -166,6 +186,11 @@ impl Game<'_> { } pub(super) fn mods_chat(&mut self, _line: &str) {} pub(super) fn draw_mod_panels(&self, _batch: &mut HudBatch, _corners: &mut Corners) {} + pub(super) fn open_mod_screen(&mut self, _owner: usize, _settings: bool) {} + pub(super) fn close_mod_screen(&mut self, _tell: bool) {} + pub(super) fn draw_mod_screen(&mut self, _batch: &mut HudBatch) {} + pub(super) fn mod_screen_click(&mut self) {} + pub(super) fn mod_screen_scroll(&mut self, _delta: f32) {} } #[cfg(feature = "mods")] @@ -187,6 +212,8 @@ impl Game<'_> { let on = |id: &str| enabled.contains(id) || dev_ids.iter().any(|d| d == id); let mods = Mods::load(&paths, on, vanilla_command); self.mods.mods = mods; + // Entries were replaced wholesale: a screen's owner index is stale. + self.mods.screen = None; self.list_mods(); self.sync_mod_commands(); self.show_mod_output(); @@ -212,6 +239,7 @@ impl Game<'_> { description: String::new(), problem, dev: false, + has_settings: false, }; }; // What it may do first: the row cuts long descriptions. @@ -227,6 +255,7 @@ impl Game<'_> { description: format!("{may} {}", m.description).trim().to_owned(), problem, dev: dev.contains(&e.path), + has_settings: e.has_ui_settings(), } }) .collect(); @@ -343,10 +372,22 @@ impl Game<'_> { Output::Feedback { text, error: false } => Text::literal(text), Output::Feedback { text, error: true } => Text::literal(text).with_color(text::RED), Output::Stopped { name, reason } => { + // Its screen goes with it: the entry's run is gone, so + // `mod_ui_view` finds no widgets and the screen closes. self.list_mods(); self.sync_mod_commands(); Text::literal(format!("Mod {name} stopped: {reason}")).with_color(text::RED) } + Output::OpenScreen { owner } => { + self.open_mod_screen(owner, false); + continue; + } + Output::CloseScreen { owner } => { + if self.mods.screen.as_ref().is_some_and(|s| s.owner == owner) { + self.close_mod_screen(false); + } + continue; + } }; // Not through system_message: a mod must not hear its own lines. let focused = self.screen == Screen::Chat; @@ -376,4 +417,297 @@ impl Game<'_> { extras_hud::draw_lines(batch, &self.gui.atlas, corners, corner, &lines); } } + + /// A `ui` screen's title and widgets as the mod last pushed them; None + /// when the surface is gone (the mod stopped or closed it). + fn mod_ui_view(&self) -> Option<(String, Vec)> { + let ms = self.mods.screen.as_ref()?; + if ms.settings { + let widgets = self.mods.mods.settings_widgets(ms.owner); + if widgets.is_empty() { + return None; + } + let name = self + .mods + .mods + .entries() + .get(ms.owner) + .and_then(|e| e.manifest.as_ref()) + .map_or("Mod", |m| m.name.as_str()); + Some((name.to_string(), widgets.to_vec())) + } else { + let s: &UiScreen = self.mods.mods.screen(ms.owner)?; + Some((s.title.clone(), s.widgets.clone())) + } + } + /// The layout the mod screen's widgets get at `h`: (widget, top, + /// height), the column being `COLUMN` wide down the middle. + fn mod_ui_layout(&self, h: f32, widgets: &[UiWidget]) -> Vec<(f32, f32)> { + let mut out = Vec::with_capacity(widgets.len()); + let mut y = MOD_UI_TOP; + for w in widgets { + let wh = match w { + UiWidget::Text(_) => 12.0, + UiWidget::Item(_) => 20.0, + UiWidget::List(l) => (l.rows.len().min(6) as f32 * 12.0) + 8.0, + }; + // Nothing may cover Done; a crowded mod loses what won't fit. + if y + wh > h - 34.0 { + break; + } + out.push((y, wh)); + y += wh + 6.0; + } + out + } + /// A mod pushes its screen (`ui.open-screen`), or Options > Mods opens + /// its settings page (`settings`, the mod's `installed` index). + pub(super) fn open_mod_screen(&mut self, owner: usize, settings: bool) { + if self.title.is_some() { + return; + } + if settings { + self.mods.mods.ui_settings_opened(owner); + if self.mods.mods.settings_widgets(owner).is_empty() { + self.show_mod_output(); + return; + } + } + self.mods.screen = Some(ModScreen { + owner, + settings, + under: self.screen, + scrolls: Vec::new(), + }); + self.screen = Screen::Mod; + if self.player.is_some() { + self.capture(false); + } + self.show_mod_output(); + } + /// Leaves the mod screen: back to Options for a settings page, to what + /// it covered for a mod's own. `tell` is Esc/Done, heard as `closed`. + pub(super) fn close_mod_screen(&mut self, tell: bool) { + let Some(ms) = self.mods.screen.take() else { + return; + }; + if ms.settings { + self.open_options_at(self.options_from_title, Page::Mods); + } else { + self.screen = ms.under; + if self.screen == Screen::Playing && self.player.is_some() { + self.capture(true); + } + } + if tell { + self.mods.mods.ui_closed(ms.owner); + self.show_mod_output(); + } + } + /// The mod's screen or settings page: its title over widgets stacked + /// down the middle, a Done button, Esc to leave (vanilla's menus). + pub(super) fn draw_mod_screen(&mut self, batch: &mut HudBatch) { + if self.mods.screen.is_none() { + self.screen = Screen::Playing; + return; + } + let Some((title, widgets)) = self.mod_ui_view() else { + self.mods.screen = None; + self.screen = Screen::Playing; + return; + }; + // Item icons draw in a second pass: `draw_item` needs `&mut self`, + // so `atlas` may not stay borrowed across it. + let mut items: Vec<(String, f32, f32)> = Vec::new(); + let atlas = &self.gui.atlas; + let in_world = self.player.is_some(); + ui::menu_background(batch, &atlas, 0.0, 0.0, batch.width, batch.height, in_world); + ui::centered_text( + batch, + &atlas, + &Text::literal(title), + 0.0, + 15.0, + batch.width, + u32::MAX, + ); + let cx = (batch.width / 2.0).floor(); + let x = cx - MOD_UI_COLUMN / 2.0; + let layout = self.mod_ui_layout(batch.height, &widgets); + let mut list_i = 0usize; + for (w, &(wy, wh)) in widgets.iter().zip(&layout) { + match w { + UiWidget::Text(t) => { + let color = if t.color == 0 { + u32::MAX + } else { + t.color | 0xFF00_0000 + }; + ui::centered_text( + batch, + &atlas, + &Text::literal(t.text.clone()), + x, + wy + 1.0, + MOD_UI_COLUMN, + color, + ); + } + UiWidget::Item(i) => { + if self.res.items().get(&i.item).is_some() { + items.push((i.item.clone(), x + 2.0, wy + 2.0)); + } else { + batch.text(&atlas, "?", x + 6.0, wy + 6.0, u32::MAX, true); + } + let name = if i.name.is_empty() { + i.item.rsplit(':').next().unwrap_or(&i.item).to_string() + } else { + i.name.clone() + }; + batch.text_styled( + &atlas, + &Text::literal(name), + x + 22.0, + wy + 6.0, + u32::MAX, + true, + ); + } + UiWidget::List(l) => { + let scroll = { + let ms = self.mods.screen.as_mut().expect("checked"); + ms.scrolls.resize_with(list_i + 1, ui::Scroll::default); + ms.scrolls[list_i] + }; + list_i += 1; + let rows_top = wy + 4.0; + let rows_bottom = wy + wh - 4.0; + batch.fill(&atlas, x, wy, MOD_UI_COLUMN, wh, 0x8000_0000); + for (b, yy) in [(x, wy), (x, wy + wh - 1.0)] { + batch.fill(&atlas, b, yy, MOD_UI_COLUMN, 1.0, 0xFF50_5050); + } + for (xx, yy) in [(x, wy), (x + MOD_UI_COLUMN - 1.0, wy)] { + batch.fill(&atlas, xx, yy, 1.0, wh, 0xFF50_5050); + } + let content = l.rows.len() as f32 * 12.0; + let first = (scroll.amount / 12.0) as usize; + let last = (first + ((rows_bottom - rows_top) / 12.0).ceil() as usize + 1) + .min(l.rows.len()); + for (vi, ri) in (first..last).enumerate() { + let ry = rows_top + vi as f32 * 12.0 - (scroll.amount % 12.0); + if ui::hovered(self.cursor, x, ry, MOD_UI_COLUMN - 6.0, 12.0) { + batch.fill(&atlas, x + 1.0, ry, MOD_UI_COLUMN - 8.0, 12.0, 0x20FF_FFFF); + } + if l.selected == Some(ri as u32) { + batch.fill(&atlas, x + 1.0, ry, 3.0, 12.0, 0xFFFF_FFFF); + } + batch.text_styled( + &atlas, + &Text::literal(l.rows[ri].clone()), + x + 6.0, + ry + 2.0, + u32::MAX, + true, + ); + } + ui::scrollbar( + batch, + &atlas, + x + MOD_UI_COLUMN - 5.0, + rows_top, + rows_bottom, + content, + scroll.amount, + ); + } + } + } + let hover = ui::hovered(self.cursor, cx - 100.0, batch.height - 27.0, 200.0, 20.0); + ui::button( + batch, + &atlas, + cx - 100.0, + batch.height - 27.0, + 200.0, + 20.0, + &Text::literal("Done"), + true, + hover, + ); + for (id, ix, iy) in items { + let stack = ItemStack::new(id.as_str(), 1); + self.draw_item(batch, &stack, ix, iy); + } + } + /// A click on the mod screen: Done leaves it, a `list` row is the mod's + /// `ui-events::select`. + pub(super) fn mod_screen_click(&mut self) { + let (w, h) = self.gui_size(); + let cursor = self.cursor; + if ui::hovered(cursor, (w / 2.0).floor() - 100.0, h - 27.0, 200.0, 20.0) { + self.audio.play_button_click(); + self.close_mod_screen(true); + return; + } + let Some((_, widgets)) = self.mod_ui_view() else { + return; + }; + let x = (w / 2.0).floor() - MOD_UI_COLUMN / 2.0; + let layout = self.mod_ui_layout(h, &widgets); + let mut list_i = 0usize; + for (w, &(wy, wh)) in widgets.iter().zip(&layout) { + let UiWidget::List(l) = w else { continue }; + let scroll = self + .mods + .screen + .as_ref() + .and_then(|ms| ms.scrolls.get(list_i).copied()) + .unwrap_or_default(); + list_i += 1; + let row = ((cursor.1 - (wy + 4.0) + scroll.amount) / 12.0) as i64; + if ui::hovered(cursor, x + 1.0, wy + 1.0, MOD_UI_COLUMN - 7.0, wh - 2.0) + && row >= 0 + && (row as usize) < l.rows.len() + { + self.audio.play_button_click(); + self.mods.mods.ui_select( + self.mods.screen.as_ref().map_or(0, |ms| ms.owner), + l.id, + row as u32, + ); + self.show_mod_output(); + return; + } + } + } + /// The wheel over a `list` scrolls it. + pub(super) fn mod_screen_scroll(&mut self, delta: f32) { + let (w, h) = self.gui_size(); + let cursor = self.cursor; + let Some((_, widgets)) = self.mod_ui_view() else { + return; + }; + let x = (w / 2.0).floor() - MOD_UI_COLUMN / 2.0; + let layout = self.mod_ui_layout(h, &widgets); + let mut list_i = 0usize; + for (w, &(wy, wh)) in widgets.iter().zip(&layout) { + let UiWidget::List(l) = w else { continue }; + let over = ui::hovered(cursor, x, wy, MOD_UI_COLUMN, wh); + let Some(ms) = self.mods.screen.as_mut() else { + return; + }; + ms.scrolls.resize_with(list_i + 1, ui::Scroll::default); + if over { + let content = l.rows.len() as f32 * 12.0; + ms.scrolls[list_i].wheel(delta, 12.0, wy + 4.0, wy + wh - 4.0, content); + return; + } + list_i += 1; + } + } } + +#[cfg(feature = "mods")] +const MOD_UI_TOP: f32 = 40.0; +#[cfg(feature = "mods")] +const MOD_UI_COLUMN: f32 = 200.0; diff --git a/crates/horizon-client/src/game/options.rs b/crates/horizon-client/src/game/options.rs index d947f91d0..812c9ce07 100644 --- a/crates/horizon-client/src/game/options.rs +++ b/crates/horizon-client/src/game/options.rs @@ -237,6 +237,7 @@ impl Game<'_> { let _ = std::fs::create_dir_all(&dir); super::packs::open_folder(&dir); } + Event::ModSettings(i) => self.open_mod_screen(i, true), Event::Credits => self.open_win_screen(false, super::win::Finish::Options), } } diff --git a/crates/horizon-client/src/game/screens.rs b/crates/horizon-client/src/game/screens.rs index cf1f3269f..44f3fbd51 100644 --- a/crates/horizon-client/src/game/screens.rs +++ b/crates/horizon-client/src/game/screens.rs @@ -17,7 +17,8 @@ impl Screen { | Self::Stats | Self::Advancements | Self::WorldOptions - | Self::Friends => true, + | Self::Friends + | Self::Mod => true, Self::Playing | Self::GameModeSwitcher | Self::Chat diff --git a/crates/horizon-client/src/game/startup_menu.rs b/crates/horizon-client/src/game/startup_menu.rs index 96af99bbc..94771f9ac 100644 --- a/crates/horizon-client/src/game/startup_menu.rs +++ b/crates/horizon-client/src/game/startup_menu.rs @@ -381,6 +381,8 @@ impl Loading { self.menu.win = Some((screen, Instant::now())); } } + // Mods' settings pages open only in a running game. + Event::ModSettings(_) => {} // The startup menu runs before the game opens a control socket. Event::ButtonClick | Event::CopyAgentDetails => {} } diff --git a/crates/horizon-client/src/mod_list.rs b/crates/horizon-client/src/mod_list.rs index ba3d93cda..db2ea9194 100644 --- a/crates/horizon-client/src/mod_list.rs +++ b/crates/horizon-client/src/mod_list.rs @@ -26,6 +26,9 @@ pub struct Installed { pub problem: Option, /// Loaded with `--dev-mod`: always on, reloaded when rebuilt. pub dev: bool, + /// The running mod set a page of widgets (capability `ui`): its row + /// gains a Settings button. + pub has_settings: bool, } impl ModList { @@ -96,6 +99,7 @@ mod tests { description: String::new(), problem: None, dev, + has_settings: false, }; let mut list = ModList { installed: vec![installed("a", false), installed("b", true)], diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index 0bc1e933d..1d1f099eb 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -893,6 +893,9 @@ enum Act { ResetPadKey(&'static str), ResetPadKeys, ToggleMod(usize), + /// The installed mod's own settings page (capability `ui`), shown as a + /// screen: `Event::ModSettings` has the game open it. + ModSettings(usize), OpenModFolder, /// Credits & Attribution's Credits button (WinScreen without the poem). Credits, @@ -944,6 +947,9 @@ pub enum Event { Packs(Vec), OpenPackFolder, OpenModFolder, + /// Open the mod's own settings page (capability `ui`): the installed + /// mod's index in `mods.installed`. + ModSettings(usize), /// Roll the credits (`CreditsAndAttributionScreen.openCreditsScreen`). Credits, /// Copy how an agent connects (the AI Agent page). @@ -1503,15 +1509,26 @@ impl OptionsUi { } else if installed.dev { label = label.append(" (Dev)"); } - let widget = button(0.0, 0.0, 310.0, label, Act::ToggleMod(i)); - Row::Buttons( - if installed.dev || installed.id.is_empty() { - inactive(widget) - } else { - widget - }, - None, - ) + let dead = installed.dev || installed.id.is_empty(); + // A running mod with a settings page (capability + // `ui`) gets its own button beside the toggle. + if installed.has_settings { + let toggle = button(0.0, 0.0, 150.0, label, Act::ToggleMod(i)); + let settings = button( + 0.0, + 0.0, + 150.0, + Text::literal("Settings"), + Act::ModSettings(i), + ); + Row::Buttons( + if dead { inactive(toggle) } else { toggle }, + Some(settings), + ) + } else { + let widget = button(0.0, 0.0, 310.0, label, Act::ToggleMod(i)); + Row::Buttons(if dead { inactive(widget) } else { widget }, None) + } }) .collect() } @@ -2498,6 +2515,10 @@ impl OptionsUi { s.mods.toggle(i); events.push(Event::Changed); } + Act::ModSettings(i) => { + events.push(Event::ButtonClick); + events.push(Event::ModSettings(i)); + } Act::OpenModFolder => events.push(Event::OpenModFolder), Act::Credits => events.push(Event::Credits), Act::CopyAgentDetails => events.push(Event::CopyAgentDetails), @@ -2988,6 +3009,7 @@ mod tests { description: "May show HUD panels.".into(), problem: id.is_empty().then(|| "Invalid package".into()), dev, + has_settings: false, }); } let (widgets, ..) = ui.layout(&s, size.0, size.1); diff --git a/crates/horizon-mod-sdk/src/lib.rs b/crates/horizon-mod-sdk/src/lib.rs index 71c36cf80..aea52339f 100644 --- a/crates/horizon-mod-sdk/src/lib.rs +++ b/crates/horizon-mod-sdk/src/lib.rs @@ -21,18 +21,24 @@ //! ``` //! //! Every interface but `log` is a capability: list the ones the mod uses in -//! `horizon-mod.toml` (`game-read`, `chat`, `hud`, `storage`). +//! `horizon-mod.toml` (`game-read`, `chat`, `hud`, `storage`, `ui`). +//! +//! Widgets (capability `ui`): a mod shows a screen it owns with +//! [`ui::open_screen`] and a page in Options > Mods with +//! [`ui::set_settings`]; what the player does with them comes back as +//! [`Mod::ui_select`], [`Mod::ui_closed`] and [`Mod::ui_settings_opened`]. wit_bindgen::generate!({ path: "wit", - world: "horizon-mod", + world: "horizon-ui-mod", pub_export_macro: true, export_macro_name: "__export_horizon_mod", default_bindings_module: "horizon_mod_sdk", }); pub use exports::horizon::mods::events::{Guest as __Guest, Registration}; -pub use horizon::mods::{chat, game, hud, log, storage}; +pub use exports::horizon::mods::ui_events::Guest as __UiEventsGuest; +pub use horizon::mods::{chat, game, hud, log, storage, ui}; /// The game's calls into a mod. Only the hooks [`Registration`] asks for /// are called; the others may stay as their defaults. @@ -51,6 +57,14 @@ pub trait Mod: Sized + 'static { } /// A chat line the player received, when [`Registration::chat`] is set. fn chat(&mut self, _text: &str) {} + /// A row of one of the mod's `list` widgets was picked (capability + /// `ui`): the widget's `id` and the row. + fn ui_select(&mut self, _id: u32, _row: u32) {} + /// The player left the mod's screen (Esc or Done; capability `ui`). + fn ui_closed(&mut self) {} + /// The player opened the mod's settings page (capability `ui`): call + /// [`ui::set_settings`] for fresh widgets. + fn ui_settings_opened(&mut self) {} } impl Registration { @@ -135,13 +149,27 @@ macro_rules! export_mod { with(|m| $crate::Mod::chat(m, &text)) } } + impl $crate::__UiEventsGuest for Export { + fn select(id: u32, row: u32) { + with(|m| $crate::Mod::ui_select(m, id, row)) + } + fn closed() { + with(|m| $crate::Mod::ui_closed(m)) + } + fn settings_opened() { + with(|m| $crate::Mod::ui_settings_opened(m)) + } + } // Only WebAssembly has the component's exports: a host build (cargo test or // clippy on the workspace) checks the mod without them, which macOS // couldn't link (its exported-symbols list cuts names at the `#`). #[cfg(target_arch = "wasm32")] $crate::__export_horizon_mod!(Export); #[cfg(not(target_arch = "wasm32"))] - let _ = ::init; + let _ = ( + ::init, + ::select, + ); }; }; } diff --git a/crates/horizon-mod-sdk/wit/horizon.wit b/crates/horizon-mod-sdk/wit/horizon.wit index 9f00a12b1..1e49919c3 100644 --- a/crates/horizon-mod-sdk/wit/horizon.wit +++ b/crates/horizon-mod-sdk/wit/horizon.wit @@ -86,6 +86,83 @@ interface storage { delete: func(key: string); } +/// Capability `ui`: widgets the game draws with its own menus' widgets, for +/// a screen the mod owns and a settings page in Options > Mods. Layout and +/// drawing are the game's: a mod says what its widgets hold, never where +/// they sit or how they look, and the game clips everything it is handed. +interface ui { + /// A line of text. + record text-widget { + /// The mod's own name for the widget; `ui-events` calls echo it. + id: u32, + /// Up to 256 characters. + text: string, + /// 0xRRGGBB; 0 is the menus' white. + color: u32, + } + /// An item's icon and a name beside it, as recipe viewers show them. + record item-widget { + /// The mod's own name for the widget. + id: u32, + /// `minecraft:diamond`, `:`; an unknown id draws a "?". + item: string, + /// The name shown beside the icon (empty: the item's own). + name: string, + } + /// Rows the player picks one of, as an options page lists choices: a + /// pick is `ui-events::select`, `selected` is the row to mark now. + record list-widget { + /// The mod's own name for the widget; `ui-events::select` echoes it. + id: u32, + /// The rows' texts, at most 512, each up to 256 characters. + rows: list, + /// The row to mark as the current choice, if any. + selected: option, + } + /// A thing on a mod's screen or settings page. + variant widget { + /// Text the player reads. + text(text-widget), + /// An item's icon and name. + item(item-widget), + /// Rows the player picks from. + %list(list-widget), + } + /// A screen the mod owns: a title over widgets stacked down the middle, + /// the Done button the game adds, and Esc to leave it. + record screen { + /// Its title, up to 256 characters. + title: string, + /// Its widgets, in order, at most 64. + widgets: list, + } + /// Show `screen` over the game; a second call while it is open replaces + /// it. Only the last call of one hook counts. + open-screen: func(screen: screen); + /// Close the mod's screen when it is open (the mod hears no `closed` + /// for this: it was its own doing). + close-screen: func(); + /// The mod's page in Options > Mods, reached from a settings button on + /// its row: the widgets it shows, an empty list for no page. The player + /// opening the page is `ui-events::settings-opened`: push fresh widgets + /// then; later calls update the page while it shows. + set-settings: func(widgets: list); +} + +/// What the game calls on a mod that shows widgets (capability `ui`): +/// what the player did with them. +interface ui-events { + /// A row of one of the mod's `list` widgets was picked: the widget's + /// `id` and the row, on the screen or the settings page that showed it. + select: func(id: u32, row: u32); + /// The player left the mod's screen (Esc or Done); not for a screen the + /// mod closed itself or one that went away with the mod. + closed: func(); + /// The player opened the mod's settings page in Options > Mods: call + /// `ui::set-settings` now to fill it with fresh widgets. + settings-opened: func(); +} + /// What the game calls. Only the hooks `init` asks for are ever called. interface events { record registration { @@ -119,3 +196,18 @@ world horizon-mod { import storage; export events; } + +/// A mod with widgets (capability `ui`): the SDK builds every mod against +/// this world. Mods built for `horizon-mod` alone (no `ui-events` export) +/// still load and simply hear nothing of their widgets. +world horizon-ui-mod { + include horizon-mod; + import ui; + export ui-events; +} + +/// The game's side of `horizon-ui-mod` beyond `horizon-mod`. +world ui-host { + import ui; + export ui-events; +} diff --git a/crates/horizon-mods/src/manifest.rs b/crates/horizon-mods/src/manifest.rs index 3a93b2512..f1ac123b1 100644 --- a/crates/horizon-mods/src/manifest.rs +++ b/crates/horizon-mods/src/manifest.rs @@ -75,14 +75,18 @@ pub enum Capability { /// New blocks and items from the mod's `data/` (API 0.2): no WIT /// interface, the game reads them before the world loads. Registries, + /// A screen the mod owns and a settings page in Options > Mods, drawn + /// with the menus' own widgets (interface `ui`, calls `ui-events`). + Ui, } impl Capability { - pub const ALL: [Capability; 5] = [ + pub const ALL: [Capability; 6] = [ Capability::GameRead, Capability::Chat, Capability::Hud, Capability::Storage, Capability::Registries, + Capability::Ui, ]; /// The WIT interface it grants, in `horizon:mods`. pub fn interface(self) -> &'static str { @@ -92,6 +96,7 @@ impl Capability { Capability::Hud => "hud", Capability::Storage => "storage", Capability::Registries => "registries", + Capability::Ui => "ui", } } /// What the player reads before turning the mod on: "May ". @@ -102,6 +107,7 @@ impl Capability { Capability::Hud => "draw text on your screen", Capability::Storage => "save its own settings to disk", Capability::Registries => "add blocks and items to your worlds", + Capability::Ui => "show its own screens and a settings page", } } /// The capability a component import needs, `Ok(None)` for one every @@ -132,6 +138,7 @@ impl fmt::Display for Capability { Capability::Hud => "hud", Capability::Storage => "storage", Capability::Registries => "registries", + Capability::Ui => "ui", }; f.write_str(name) } diff --git a/crates/horizon-mods/src/runtime.rs b/crates/horizon-mods/src/runtime.rs index 52da641b4..1b1795037 100644 --- a/crates/horizon-mods/src/runtime.rs +++ b/crates/horizon-mods/src/runtime.rs @@ -16,6 +16,15 @@ mod bindings { world: "horizon-mod", }); } +/// Capability `ui`'s world beyond `horizon-mod` (docs/mods.md): a second +/// world, so mods built before it (no `ui-events` export) still instantiate +/// as `horizon-mod` and simply hear nothing of their widgets. +mod ui_bindings { + wasmtime::component::bindgen!({ + path: "../horizon-mod-sdk/wit", + world: "ui-host", + }); +} pub use bindings::horizon::mods::{ game::{Clock, Player}, hud::{Corner, Line}, @@ -25,6 +34,12 @@ use bindings::{ exports::horizon::mods::events::Registration, horizon::mods::{chat, game, hud, log as mod_log, storage}, }; +/// The widget model a `ui` screen or settings page shows (WIT `interface +/// ui`), for the game to draw with its menus' own widgets. +pub use ui_bindings::horizon::mods::ui::{ + ItemWidget, ListWidget, Screen as UiScreen, TextWidget, Widget as UiWidget, +}; +use ui_bindings::{UiHost, horizon::mods::ui}; /// A mod's linear memory cap. const MEMORY: usize = 64 << 20; @@ -66,6 +81,11 @@ pub enum Output { Feedback { text: String, error: bool }, /// The mod was turned off: it trapped, overran or failed to start. Stopped { name: String, reason: String }, + /// `ui.open-screen`: the mod's entry in `entries()`; what it shows is + /// [`Mods::screen`]. + OpenScreen { owner: usize }, + /// `ui.close-screen`: the mod closed its own screen (no `closed` back). + CloseScreen { owner: usize }, } /// A mod's text panel, drawn every frame until it changes. @@ -95,19 +115,44 @@ pub struct Entry { run: Option, modified: Option, } +impl Entry { + /// The running mod has a settings page (capability `ui` set widgets + /// with `ui.set-settings`): its row in Options > Mods gains a button. + pub fn has_ui_settings(&self) -> bool { + self.run + .as_ref() + .is_some_and(|r| !r.store.data().ui.settings.is_empty()) + } +} struct Running { store: Store, exports: HorizonMod, + /// The mod's `ui-events` export; none for a mod built before `ui`. + ui: Option, hooks: Registration, } +/// What capability `ui` pushed since the hooks last ran. +#[derive(Default)] +struct UiState { + /// The screen `ui.open-screen` last pushed, until `ui.close-screen`. + screen: Option, + /// The widgets `ui.set-settings` last pushed (the mod's page in + /// Options > Mods). + settings: Vec, + /// The calls since the last hook ended, for [`Output`]. + open: bool, + close: bool, +} + struct State { name: String, id: String, snapshot: Arc, out: Vec, panel: Option, + ui: UiState, /// Capability `storage`'s folder; made on first `set`. data_dir: PathBuf, /// Bytes `storage::set` may still write before this hook call ends @@ -184,6 +229,8 @@ impl Mods { let mut linker = Linker::new(&engine); HorizonMod::add_to_linker::>(&mut linker, |s| s) .map_err(|e| e.to_string())?; + UiHost::add_to_linker::>(&mut linker, |s| s) + .map_err(|e| e.to_string())?; self.engine = Some((engine, linker)); } Ok(self.engine.as_ref().expect("made above")) @@ -242,6 +289,7 @@ impl Mods { snapshot, out: Vec::new(), panel: None, + ui: UiState::default(), data_dir, storage_budget: STORAGE_WRITE_BUDGET, limits: StoreLimitsBuilder::new() @@ -252,8 +300,13 @@ impl Mods { let mut store = Store::new(engine, state); store.limiter(|s| &mut s.limits); store.set_fuel(FUEL_INIT).map_err(|e| e.to_string())?; - let exports = - HorizonMod::instantiate(&mut store, &component, linker).map_err(|e| reason(&e))?; + let instance = linker + .instantiate(&mut store, &component) + .map_err(|e| reason(&e))?; + let exports = HorizonMod::new(&mut store, &instance).map_err(|e| reason(&e))?; + // `ui-events` is a mod built since `ui`'s; older mods have no export + // and stay callable through `events` alone. + let ui = UiHost::new(&mut store, &instance).ok(); let mut hooks = exports .horizon_mods_events() .call_init(&mut store) @@ -269,6 +322,7 @@ impl Mods { Ok(Some(Running { store, exports, + ui, hooks, })) } @@ -290,7 +344,7 @@ impl Mods { &mut self, fuel: u64, wants: impl Fn(&Registration) -> bool, - mut f: impl FnMut(&HorizonMod, &mut Store) -> wasmtime::Result<()>, + mut f: impl FnMut(&mut Running) -> wasmtime::Result<()>, ) { for i in 0..self.entries.len() { if self.entries[i] @@ -308,20 +362,28 @@ impl Mods { &mut self, i: usize, fuel: u64, - f: impl FnOnce(&HorizonMod, &mut Store) -> wasmtime::Result, + f: impl FnOnce(&mut Running) -> wasmtime::Result, ) -> Option { let entry = &mut self.entries[i]; let run = entry.run.as_mut()?; run.store.data_mut().snapshot = self.snapshot.clone(); run.store.data_mut().storage_budget = STORAGE_WRITE_BUDGET; let started = Instant::now(); - let result = run - .store - .set_fuel(fuel) - .and_then(|()| f(&run.exports, &mut run.store)); + let result = run.store.set_fuel(fuel).and_then(|()| f(run)); // An average over about the last 64 calls. entry.cost = (entry.cost * 63 + started.elapsed()) / 64; - self.out.append(&mut run.store.data_mut().out); + let data = run.store.data_mut(); + let (open, close) = ( + std::mem::take(&mut data.ui.open), + std::mem::take(&mut data.ui.close), + ); + self.out.append(&mut data.out); + if open { + self.out.push(Output::OpenScreen { owner: i }); + } + if close { + self.out.push(Output::CloseScreen { owner: i }); + } match result { Ok(r) => Some(r), Err(e) => { @@ -344,7 +406,11 @@ impl Mods { self.each( FUEL_TICK, |h| h.tick, - |m, s| m.horizon_mods_events().call_tick(s, tick), + |run| { + run.exports + .horizon_mods_events() + .call_tick(&mut run.store, tick) + }, ); } /// A rendered frame, `seconds` after the last. @@ -352,7 +418,11 @@ impl Mods { self.each( FUEL_FRAME, |h| h.frame, - |m, s| m.horizon_mods_events().call_frame(s, seconds), + |run| { + run.exports + .horizon_mods_events() + .call_frame(&mut run.store, seconds) + }, ); } /// A chat line the player received, as plain text. @@ -360,7 +430,11 @@ impl Mods { self.each( FUEL_EVENT, |h| h.chat, - |m, s| m.horizon_mods_events().call_chat(s, text), + |run| { + run.exports + .horizon_mods_events() + .call_chat(&mut run.store, text) + }, ); } /// Whether a running mod owns `/name`. @@ -381,8 +455,10 @@ impl Mods { let Some(i) = self.command_owner(name) else { return false; }; - let result = self.call(i, FUEL_EVENT, |m, s| { - m.horizon_mods_events().call_command(s, name, args) + let result = self.call(i, FUEL_EVENT, |run| { + run.exports + .horizon_mods_events() + .call_command(&mut run.store, name, args) }); match result { Some(Ok(Some(text))) => self.out.push(Output::Feedback { text, error: false }), @@ -405,6 +481,60 @@ impl Mods { .iter() .filter_map(|e| e.run.as_ref()?.store.data().panel.as_ref()) } + /// The screen mod `i` last pushed with `ui.open-screen`, while it is up. + pub fn screen(&self, i: usize) -> Option<&UiScreen> { + self.entries + .get(i)? + .run + .as_ref()? + .store + .data() + .ui + .screen + .as_ref() + } + /// The widgets `ui.set-settings` pushed: mod `i`'s page in + /// Options > Mods. Empty while it has none. + pub fn settings_widgets(&self, i: usize) -> &[UiWidget] { + self.entries + .get(i) + .and_then(|e| e.run.as_ref()) + .map_or(&[], |r| &r.store.data().ui.settings) + } + /// A `list` widget's row was picked on a surface of mod `i`. + pub fn ui_select(&mut self, i: usize, id: u32, row: u32) { + self.ui_event(i, |ui, s| { + ui.horizon_mods_ui_events().call_select(s, id, row) + }); + } + /// The player left mod `i`'s screen (Esc or Done). + pub fn ui_closed(&mut self, i: usize) { + self.ui_event(i, |ui, s| ui.horizon_mods_ui_events().call_closed(s)); + } + /// The player opened mod `i`'s settings page. + pub fn ui_settings_opened(&mut self, i: usize) { + self.ui_event(i, |ui, s| { + ui.horizon_mods_ui_events().call_settings_opened(s) + }); + } + /// A `ui-events` call on mod `i`; no export, no call (mods built + /// before `ui` have none). + fn ui_event( + &mut self, + i: usize, + f: impl FnOnce(&UiHost, &mut Store) -> wasmtime::Result<()>, + ) { + let has_ui = self + .entries + .get(i) + .and_then(|e| e.run.as_ref()) + .is_some_and(|r| r.ui.is_some()); + if has_ui { + self.call(i, FUEL_EVENT, |run| { + f(run.ui.as_ref().expect("checked above"), &mut run.store) + }); + } + } /// What mods asked to show since the last call. pub fn drain(&mut self) -> Vec { std::mem::take(&mut self.out) @@ -476,6 +606,33 @@ fn clip(text: String) -> String { } } +/// Widgets on a `ui` screen or settings page, and rows on one `list`. +const WIDGETS: usize = 64; +const LIST_ROWS: usize = 512; + +fn clip_widgets(widgets: Vec) -> Vec { + widgets + .into_iter() + .take(WIDGETS) + .map(|w| match w { + UiWidget::Text(mut t) => { + t.text = clip(t.text); + UiWidget::Text(t) + } + UiWidget::Item(mut i) => { + i.item = clip(i.item); + i.name = clip(i.name); + UiWidget::Item(i) + } + UiWidget::List(mut l) => { + l.rows = l.rows.into_iter().take(LIST_ROWS).map(clip).collect(); + l.selected = l.selected.filter(|&s| (s as usize) < l.rows.len()); + UiWidget::List(l) + } + }) + .collect() +} + impl mod_log::Host for State { fn write(&mut self, level: mod_log::Level, message: String) { let level = match level { @@ -527,6 +684,21 @@ impl hud::Host for State { self.panel = None; } } +impl ui::Host for State { + fn open_screen(&mut self, mut screen: ui::Screen) { + screen.title = clip(screen.title); + screen.widgets = clip_widgets(screen.widgets); + self.ui.screen = Some(screen); + self.ui.open = true; + } + fn close_screen(&mut self) { + self.ui.screen = None; + self.ui.close = true; + } + fn set_settings(&mut self, widgets: Vec) { + self.ui.settings = clip_widgets(widgets); + } +} impl storage::Host for State { fn get(&mut self, key: String) -> Option> { storage_key_ok(&key) diff --git a/crates/horizon-mods/tests/sandbox.rs b/crates/horizon-mods/tests/sandbox.rs index a246c07b0..a229f748e 100644 --- a/crates/horizon-mods/tests/sandbox.rs +++ b/crates/horizon-mods/tests/sandbox.rs @@ -17,6 +17,13 @@ version = "0.1.0" api = "0.1" capabilities = ["chat", "storage"] "#; +const UI_MANIFEST: &str = r#" +id = "test_ui_guest" +name = "Test UI Guest" +version = "0.1.0" +api = "0.1" +capabilities = ["chat", "ui"] +"#; /// Builds both mods once and writes their `.hmod` files; returns the folder. fn mods_dir() -> &'static Path { @@ -32,6 +39,8 @@ fn mods_dir() -> &'static Path { "coords-hud", "-p", "horizon-mods-test-guest", + "-p", + "horizon-mods-test-ui-guest", "--target-dir", ]) .arg(&target) @@ -48,6 +57,7 @@ fn mods_dir() -> &'static Path { let mods = [ ("coords_hud", std::fs::read_to_string(example).unwrap()), ("horizon_mods_test_guest", GUEST_MANIFEST.to_string()), + ("horizon_mods_test_ui_guest", UI_MANIFEST.to_string()), ]; for (name, manifest) in mods { let core = std::fs::read(built.join(format!("{name}.wasm"))).unwrap(); @@ -366,6 +376,45 @@ fn hook_overhead() { ); } +#[test] +fn ui_screens_settings_and_events() { + let mut mods = load("horizon_mods_test_ui_guest.hmod"); + assert_eq!(mods.entries()[0].status, Status::Running); + assert!(!mods.entries()[0].has_ui_settings()); + // `/ui_screen` pushes the screen; the host clips nothing of this one. + assert!(mods.command("ui_screen")); + assert_eq!(mods.drain(), [Output::OpenScreen { owner: 0 }]); + let screen = mods.screen(0).expect("a pushed screen"); + assert_eq!(screen.title, "Guest's screen"); + assert_eq!(screen.widgets.len(), 3); + // What the player did comes back through `ui-events`. + mods.ui_select(0, 7, 2); + mods.ui_closed(0); + assert_eq!( + mods.drain(), + [ + Output::Chat { + name: "Test UI Guest".into(), + text: "selected 7:2".into(), + }, + Output::Chat { + name: "Test UI Guest".into(), + text: "closed".into(), + }, + ] + ); + // The settings page: empty until the mod fills it, refreshed on open. + assert!(mods.command("ui_settings")); + assert_eq!(mods.settings_widgets(0).len(), 3); + assert!(mods.entries()[0].has_ui_settings()); + mods.ui_settings_opened(0); + assert_eq!(mods.settings_widgets(0).len(), 1); + // `/ui_close` closes the mod's own screen. + assert!(mods.command("ui_close")); + assert!(mods.drain().contains(&Output::CloseScreen { owner: 0 })); + assert!(mods.screen(0).is_none()); +} + #[test] fn content_only_mods_load_without_an_engine() { let manifest = r#" diff --git a/crates/horizon-mods/tests/ui_guest/Cargo.toml b/crates/horizon-mods/tests/ui_guest/Cargo.toml new file mode 100644 index 000000000..ae6e0950e --- /dev/null +++ b/crates/horizon-mods/tests/ui_guest/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "horizon-mods-test-ui-guest" +version = "0.1.0" +edition.workspace = true +license.workspace = true +publish = false +description = "A mod that shows widgets, for horizon-mods' sandbox tests" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +horizon-mod-sdk = { path = "../../../horizon-mod-sdk" } + +[lints] +workspace = true diff --git a/crates/horizon-mods/tests/ui_guest/src/lib.rs b/crates/horizon-mods/tests/ui_guest/src/lib.rs new file mode 100644 index 000000000..1af3985a1 --- /dev/null +++ b/crates/horizon-mods/tests/ui_guest/src/lib.rs @@ -0,0 +1,66 @@ +//! A mod for the runtime's `ui` tests: `/ui_screen` opens a screen with one +//! of each widget, `/ui_settings` sets a settings page, `/ui_close` closes +//! the screen; the `ui-events` calls answer in chat so the host sees them. +use horizon_mod_sdk::{ + Mod, Registration, chat, + ui::{self, ItemWidget, ListWidget, Screen, TextWidget, Widget}, +}; + +struct Guest; + +fn widgets() -> Vec { + vec![ + Widget::Text(TextWidget { + id: 1, + text: "Pick a row".into(), + color: 0, + }), + Widget::Item(ItemWidget { + id: 2, + item: "minecraft:diamond".into(), + name: String::new(), + }), + Widget::List(ListWidget { + id: 7, + rows: vec!["first".into(), "second".into(), "third".into()], + selected: Some(1), + }), + ] +} + +impl Mod for Guest { + fn init() -> Result<(Self, Registration), String> { + let hooks = ["ui_screen", "ui_settings", "ui_close"] + .into_iter() + .fold(Registration::new(), Registration::command); + Ok((Guest, hooks)) + } + fn command(&mut self, name: &str, _args: &str) -> Result, String> { + match name { + "ui_screen" => ui::open_screen(&Screen { + title: "Guest's screen".into(), + widgets: widgets(), + }), + "ui_settings" => ui::set_settings(&widgets()), + "ui_close" => ui::close_screen(), + _ => {} + } + Ok(None) + } + fn ui_select(&mut self, id: u32, row: u32) { + chat::show(&format!("selected {id}:{row}")); + } + fn ui_closed(&mut self) { + chat::show("closed"); + } + fn ui_settings_opened(&mut self) { + ui::set_settings(&[Widget::Text(TextWidget { + id: 1, + text: "fresh".into(), + color: 0x33CC33, + })]); + chat::show("settings opened"); + } +} + +horizon_mod_sdk::export_mod!(Guest); diff --git a/docs/api/mods-reference.md b/docs/api/mods-reference.md index ca2fe9d7a..0812cde54 100644 --- a/docs/api/mods-reference.md +++ b/docs/api/mods-reference.md @@ -12,12 +12,27 @@ The contract between Horizon and its mods: the WIT package `horizon:mods@0.1.0`, | `hud` | [`hud`](#hud) | draw text on your screen | | `storage` | [`storage`](#storage) | save its own settings to disk | | `registries` | data files (`assets/`, `data/`), no interface | add blocks and items to your worlds | +| `ui` | [`ui`](#ui) | show its own screens and a settings page | ## World `horizon-mod` - Imports (what the game offers): `log`, `game`, `chat`, `hud`, `storage` - Exports (what the mod provides): `events` +## World `horizon-ui-mod` + +A mod with widgets (capability `ui`): the SDK builds every mod against this world. Mods built for `horizon-mod` alone (no `ui-events` export) still load and simply hear nothing of their widgets. + +- Imports (what the game offers): `ui`, `log`, `game`, `chat`, `hud`, `storage` +- Exports (what the mod provides): `ui-events`, `events` + +## World `ui-host` + +The game's side of `horizon-ui-mod` beyond `horizon-mod`. + +- Imports (what the game offers): `ui` +- Exports (what the mod provides): `ui-events` + ## `log` Access: always granted. @@ -117,6 +132,79 @@ Capability `storage`: a per-mod folder of small key/value files, quota'd (docs/m | `set` | `func(key: string, value: list) -> result<_, string>` | Errs on a malformed key or over quota (bytes for the whole mod). | | `delete` | `func(key: string)` | No error if `key` was already absent. | +## `ui` + +Access: capability `ui`. + +Capability `ui`: widgets the game draws with its own menus' widgets, for a screen the mod owns and a settings page in Options > Mods. Layout and drawing are the game's: a mod says what its widgets hold, never where they sit or how they look, and the game clips everything it is handed. + +### record `text-widget` + +A line of text. + +| Field | Type | | +| --- | --- | --- | +| `id` | `u32` | The mod's own name for the widget; `ui-events` calls echo it. | +| `text` | `string` | Up to 256 characters. | +| `color` | `u32` | 0xRRGGBB; 0 is the menus' white. | + +### record `item-widget` + +An item's icon and a name beside it, as recipe viewers show them. + +| Field | Type | | +| --- | --- | --- | +| `id` | `u32` | The mod's own name for the widget. | +| `item` | `string` | `minecraft:diamond`, `:`; an unknown id draws a "?". | +| `name` | `string` | The name shown beside the icon (empty: the item's own). | + +### record `list-widget` + +Rows the player picks one of, as an options page lists choices: a pick is `ui-events::select`, `selected` is the row to mark now. + +| Field | Type | | +| --- | --- | --- | +| `id` | `u32` | The mod's own name for the widget; `ui-events::select` echoes it. | +| `rows` | `list` | The rows' texts, at most 512, each up to 256 characters. | +| `selected` | `option` | The row to mark as the current choice, if any. | + +### variant `widget` + +A thing on a mod's screen or settings page. + +| Case | Payload | | +| --- | --- | --- | +| `text` | `text-widget` | Text the player reads. | +| `item` | `item-widget` | An item's icon and name. | +| `list` | `list-widget` | Rows the player picks from. | + +### record `screen` + +A screen the mod owns: a title over widgets stacked down the middle, the Done button the game adds, and Esc to leave it. + +| Field | Type | | +| --- | --- | --- | +| `title` | `string` | Its title, up to 256 characters. | +| `widgets` | `list` | Its widgets, in order, at most 64. | + +| Function | Signature | What it does | +| --- | --- | --- | +| `open-screen` | `func(screen: screen)` | Show `screen` over the game; a second call while it is open replaces it. Only the last call of one hook counts. | +| `close-screen` | `func()` | Close the mod's screen when it is open (the mod hears no `closed` for this: it was its own doing). | +| `set-settings` | `func(widgets: list)` | The mod's page in Options > Mods, reached from a settings button on its row: the widgets it shows, an empty list for no page. The player opening the page is `ui-events::settings-opened`: push fresh widgets then; later calls update the page while it shows. | + +## `ui-events` + +Access: exported by the mod: the game calls it. + +What the game calls on a mod that shows widgets (capability `ui`): what the player did with them. + +| Function | Signature | What it does | +| --- | --- | --- | +| `select` | `func(id: u32, row: u32)` | A row of one of the mod's `list` widgets was picked: the widget's `id` and the row, on the screen or the settings page that showed it. | +| `closed` | `func()` | The player left the mod's screen (Esc or Done); not for a screen the mod closed itself or one that went away with the mod. | +| `settings-opened` | `func()` | The player opened the mod's settings page in Options > Mods: call `ui::set-settings` now to fill it with fresh widgets. | + ## `events` Access: exported by the mod: the game calls it. diff --git a/docs/mods.md b/docs/mods.md index bac207f60..45e5ae02e 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -12,7 +12,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | Part | Where | State | |---|---|---| -| The contract (WIT) | `crates/horizon-mod-sdk/wit/horizon.wit` | 0.1: log, game-read, chat, hud, storage; tick, frame, chat and command hooks | +| The contract (WIT) | `crates/horizon-mod-sdk/wit/horizon.wit` | 0.1: log, game-read, chat, hud, storage, ui; tick, frame, chat and command hooks | | SDK for mod authors | `crates/horizon-mod-sdk` | 0.1 | | Example mod | `crates/horizon-mod-sdk/examples/coords-hud` | coordinates, facing and time HUD, `/where`, `/coords` | | Runtime | `crates/horizon-mods` | wasmtime 36 LTS, component model | @@ -86,6 +86,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | `chat` | `chat` | `show(text)`: a line only the local player sees, after the mod's name (8 per call) | | `hud` | `hud` | `set-panel(corner, lines)`, `clear()`: up to 16 lines of 256 characters | | `storage` | `storage` | `get(key)`, `set(key, value)`, `delete(key)`: a per-mod folder of up to 64 files, 4 MiB total, at most 1 MiB written per call | +| `ui` | `ui` | `open-screen`, `close-screen`, `set-settings`: a screen the mod owns and a page in Options > Mods, of text, item and list widgets the game lays out and draws with its own menus' code | | Hook (export) | When | |---|---| @@ -112,7 +113,6 @@ loading. Ordered by what the first native ports need: | `target` | the crosshair's block or entity, with its state and NBT summary | Jade/WTHIT-style tooltips | | `inventory-read` | the player's inventory, held item, open container | AppleSkin, recipe viewers | | `recipes` | the recipe registry and lookups | JEI/EMI-style viewers | -| `ui` | widgets: text, item icons, lists, a screen the mod owns, a settings page in Options > Mods | built from vanilla's widgets (`ui`) so it matches the menus | | `world-read` | blocks and biomes around the player, within the render distance | minimaps, schematics | | `render` | world-space lines, boxes and ghost blocks in a per-frame budget | Litematica-style placement, waypoints | | `registries` | new blocks, items, entities and recipes, mostly data: JSON models, textures, loot and recipes in the `.hmod`'s `assets/` and `data/` | server side; see [Multiplayer](#multiplayer) | From fd154dcbba30d8b4ad8320f6f93231013965391d Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:13:06 +0100 Subject: [PATCH 111/238] Website image: copy data/ so the build finds the roadmap build.mjs reads website/data/roadmap.json since the hub (#1819), but the Dockerfile never copied data/, so every site deploy since then failed and playhorizon.dev kept the old build. Co-Authored-By: Claude Opus 5.5 --- website/Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/website/Dockerfile b/website/Dockerfile index ea9cc9afc..b075f9b58 100644 --- a/website/Dockerfile +++ b/website/Dockerfile @@ -5,6 +5,7 @@ WORKDIR /site COPY build.mjs ./ COPY src ./src COPY docs ./docs +COPY data ./data COPY public ./public COPY LEGAL.md ./ ARG GITHUB_TOKEN From d41a3075aa86491e6ac23821480ff2a86b494289 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:13:26 +0100 Subject: [PATCH 112/238] Launcher: a local registry's file:/// URLs open on Windows file:///C:/x left /C:/x after the scheme, which Windows can't open; the pack tests now write real file:/// URLs (the index only accepts those), with forward slashes. Co-Authored-By: Claude Opus 5.5 --- launcher/src/browser/horizon.rs | 9 +++++++++ launcher/src/browser/horizon/pack.rs | 12 ++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/launcher/src/browser/horizon.rs b/launcher/src/browser/horizon.rs index e56f31e09..66553ab50 100644 --- a/launcher/src/browser/horizon.rs +++ b/launcher/src/browser/horizon.rs @@ -343,6 +343,7 @@ fn fetch( ) -> Result { match file.url.strip_prefix("file://").filter(|_| local) { Some(path) => { + let path = local_path(path); let reader = fs::File::open(path).map_err(|e| format!("{path}: {e}"))?; download::save_from_anywhere(file, reader, folder, progress) } @@ -350,6 +351,14 @@ fn fetch( } } +/// The path of a `file:///` URL: on Windows `/C:/x` is `C:/x`. +fn local_path(path: &str) -> &str { + match path.strip_prefix('/') { + Some(rest) if cfg!(windows) && rest.get(1..2) == Some(":") => rest, + _ => path, + } +} + /// What the registry adds to an installed entry. fn trust_label(trust: Trust) -> &'static str { match trust { diff --git a/launcher/src/browser/horizon/pack.rs b/launcher/src/browser/horizon/pack.rs index a2cc4d079..3cb6d7c9b 100644 --- a/launcher/src/browser/horizon/pack.rs +++ b/launcher/src/browser/horizon/pack.rs @@ -208,8 +208,16 @@ mod tests { zip.write_all(b"renderDistance:8\n").unwrap(); zip.finish().unwrap(); let size = |p: &Path| fs::metadata(p).unwrap().len(); - // Forward slashes: a Windows path's backslashes would be escapes in the JSON. - let url = |p: &Path| p.display().to_string().replace('\\', "/"); + // A `file:///` URL: forward slashes (a Windows path's backslashes would be escapes in + // the JSON) and a leading slash before a Windows drive. + let url = |p: &Path| { + let path = p.display().to_string().replace('\\', "/"); + if path.starts_with('/') { + path + } else { + format!("/{path}") + } + }; let text = format!( r#"{{"schema": 1, "projects": [ {{"id": "coords_hud", "kind": "mod", "name": "Coordinates HUD", "summary": "", "license": "MIT", From 204859a2cb893f71b14605ea689f91424ace0671 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:20:41 +0100 Subject: [PATCH 113/238] Launcher: a preparation reusing imported game files still finds the assets When an update's preparation reused the game files an earlier version imported, `preparer` handed the chosen folder as it was to the sound copy and the panorama, fonts, packs and languages step. A Prism Launcher instance or Modrinth App profile holds no asset store, so the game was silent with a grey title screen, and Prepare again did the same. It now resolves the folder (linking another launcher's files into the official layout) either way; with reused imports a missing one is a log line, not an error. Co-Authored-By: Claude Opus 5.5 --- launcher/src/backend.rs | 97 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 88 insertions(+), 9 deletions(-) diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index 9d1eeb111..5f631f8f8 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -1103,6 +1103,34 @@ fn with_java(cmd: &mut Command, home: &Path) { } } +/// The Minecraft folder a preparation whose game files are imported already reads: the sounds, +/// the title panorama, the fonts and the languages come from its asset store. It is the one +/// `resolve` found, linked into the official layout for another launcher's files; without one, the +/// chosen folder, and the preparation goes without them, saying so. Before, such a preparation +/// read the chosen folder as it was, which a Prism Launcher or Modrinth App install does not +/// keep its assets in: the game was silent and its title screen grey. +fn assets_folder( + resolved: Result, + chosen: &Path, + version: &str, + log: &dyn Fn(String), +) -> PathBuf { + match resolved { + Ok(sources::Resolved::Folder(dir)) => dir, + Ok(sources::Resolved::NotFound(_)) => { + log(format!( + "Minecraft {version}'s assets were not found in any launcher folder: the game \ + will be silent and the title screen plain" + )); + chosen.to_path_buf() + } + Err(e) => { + log(format!("Minecraft {version}'s assets: {e}")); + chosen.to_path_buf() + } + } +} + /// Checks what the preparation needs and picks how to run it. PLAY.txt describes the same /// steps: the player's own launcher install, the server jar from that version's own json, and /// Java (and Python, for versions before horizon-prepare). Also returns the Minecraft folder @@ -1117,9 +1145,8 @@ pub fn preparer( let dir = version_dir(tag); let reused = horizon_prepare::artifacts::reuse_imported(&dir, &|line| log(line.to_string()))?; let chosen = minecraft.to_path_buf(); - let minecraft = if reused { - minecraft.to_path_buf() - } else { + // Resolved even when the game files are reused: the assets still come from the install. + let minecraft = { let pins = read_json(&dir.join("research-lock.json")) .and_then(|lock| sources::Pins::read(&lock)) .ok_or("The version's research-lock.json doesn't pin the Minecraft files")?; @@ -1139,12 +1166,17 @@ pub fn preparer( Ok(body) }; let linked = data_dir().join("linked-minecraft"); - match sources::resolve(minecraft, &pins, &known, &linked, &fetch, log)? { - sources::Resolved::Folder(dir) => dir, - sources::Resolved::NotFound(checked) => { - let not_found = sources::not_found(&version, &checked, minecraft); - download_for_owner(&pins, &own, &fetch, log).map_err(|e| not_found + &e)?; - own + let resolved = sources::resolve(minecraft, &pins, &known, &linked, &fetch, log); + if reused { + assets_folder(resolved, &chosen, &version, log) + } else { + match resolved? { + sources::Resolved::Folder(dir) => dir, + sources::Resolved::NotFound(checked) => { + let not_found = sources::not_found(&version, &checked, minecraft); + download_for_owner(&pins, &own, &fetch, log).map_err(|e| not_found + &e)?; + own + } } } }; @@ -2353,6 +2385,53 @@ mod tests { use super::*; use std::cmp::Ordering::*; + /// A preparation that reuses the imported game files still reads the assets from the + /// launcher that has them: a Prism Launcher instance's are in the launcher root, linked into + /// the official layout (jan.sonki on Discord: silent, grey title screen since 0.2.48). + #[test] + fn reused_imports_take_the_assets_from_the_launcher_that_has_them() { + let dir = + std::env::temp_dir().join(format!("horizon-reused-assets-{}", std::process::id())); + let _ = fs::remove_dir_all(&dir); + let client = b"fake client jar"; + let manifest = br#"{"id": "26.3"}"#; + let write = |path: &str, data: &[u8]| { + let path = dir.join(path); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, data).unwrap(); + }; + let prism = "PrismLauncher"; + write( + &format!("{prism}/libraries/com/mojang/minecraft/26.3/minecraft-26.3-client.jar"), + client, + ); + write(&format!("{prism}/assets/indexes/34.json"), b"{}"); + let instance = dir.join(prism).join("instances/26.3/minecraft"); + fs::create_dir_all(&instance).unwrap(); + let pins = sources::Pins::read(&serde_json::json!({ "minecraft": { + "version": "26.3", + "client_sha1": sources::sha1_hex(client), + "manifest_sha1": sources::sha1_hex(manifest), + }})) + .unwrap(); + let linked = dir.join("linked"); + let fetch = |_: &str| Ok(manifest.to_vec()); + let lines = std::cell::RefCell::new(Vec::new()); + let log = |line: String| lines.borrow_mut().push(line); + let resolved = sources::resolve(&instance, &pins, &[], &linked, &fetch, &log); + let folder = assets_folder(resolved, &instance, "26.3", &log); + assert_eq!(folder, linked); + assert!(folder.join("assets/indexes/34.json").is_file()); + + // Nothing found: the chosen folder, and a line saying what the game will lack. + let empty = dir.join("empty"); + fs::create_dir_all(&empty).unwrap(); + let resolved = sources::resolve(&empty, &pins, &[], &linked, &fetch, &log); + assert_eq!(assets_folder(resolved, &empty, "26.3", &log), empty); + assert!(lines.borrow().last().unwrap().contains("will be silent")); + fs::remove_dir_all(&dir).unwrap(); + } + #[test] fn resources_without_sounds_are_reported() { let dir = std::env::temp_dir().join(format!("horizon-no-sounds-{}", std::process::id())); From 6d3cd330d1f9b24b560c19c1701f39b1ed9b87cd Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:25:02 +0100 Subject: [PATCH 114/238] One isWithinBlockInteractionRange for guests, the in-frame player, stations, lecterns and signs The reach test was written out four times (stations, lecterns, the in-frame container check and the guest's); they now share lan::guest::within_block_range. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/src/game/lan_stations.rs | 20 ++------------ crates/horizon-client/src/game/sign_edit.rs | 2 +- crates/horizon-client/src/game/storage.rs | 14 ++-------- crates/horizon-client/src/lan/guest.rs | 26 ++++++++++++------- 4 files changed, 21 insertions(+), 41 deletions(-) diff --git a/crates/horizon-client/src/game/lan_stations.rs b/crates/horizon-client/src/game/lan_stations.rs index 49a90b7b9..418ed9c2a 100644 --- a/crates/horizon-client/src/game/lan_stations.rs +++ b/crates/horizon-client/src/game/lan_stations.rs @@ -588,7 +588,7 @@ impl Game<'_> { let [x, y, z] = gs.pos; let name = &self.res.facts().get(self.world.block(x, y, z)).name; let block = StationKind::from_block(name) == Some(gs.kind); - if !(block && within_reach(g, gs.pos)) { + if !(block && g.within_block_range(gs.pos, 4.0)) { let window = gs.window; self.lan_station_closed(host, conn, Some(window)); host.hub.send_to( @@ -631,16 +631,7 @@ impl Game<'_> { Some(BlockEntity::Lectern(l)) if l.book.is_some() ); // Container.stillValidBlockEntity: the interaction range and its buffer. - let eye = [g.position[0], g.position[1] + g.eye_height(), g.position[2]]; - let reach = if g.creative() { 5.0 } else { 4.5 } + 4.0; - let pos = [key.0, key.1, key.2]; - let near = (0..3) - .map(|k| { - let lo = f64::from(pos[k]); - (lo - eye[k]).max(eye[k] - lo - 1.0).max(0.0).powi(2) - }) - .sum::() - < reach * reach; + let near = g.within_block_range([key.0, key.1, key.2], 4.0); if has_book && near { self.lan_send_lectern(host, conn, false); } else { @@ -660,10 +651,3 @@ pub(super) fn station_inputs(gs: &GuestStation) -> Vec { .map(|(_, _, inputs)| inputs.filter_map(|s| gs.menu.slot(s).cloned()).collect()) .unwrap_or_default() } - -/// `Player.isWithinBlockInteractionRange(pos, 4)`: the guest's eyes within -/// its block interaction range (4.5, creative 5) and a 4-block buffer of the -/// block's box. -pub(super) fn within_reach(g: &horizon_client::lan::guest::Guest, pos: [i32; 3]) -> bool { - g.within_block_range(pos, 4.0) -} diff --git a/crates/horizon-client/src/game/sign_edit.rs b/crates/horizon-client/src/game/sign_edit.rs index 5a26e3e69..5efc3dd0b 100644 --- a/crates/horizon-client/src/game/sign_edit.rs +++ b/crates/horizon-client/src/game/sign_edit.rs @@ -78,7 +78,7 @@ impl Game<'_> { let Some(g) = host.hub.guest(conn) else { return; }; - let near = super::lan_stations::within_reach(g, p.pos); + let near = g.within_block_range(p.pos, 4.0); let Some(play) = host.play.get_mut(&conn) else { return; }; diff --git a/crates/horizon-client/src/game/storage.rs b/crates/horizon-client/src/game/storage.rs index 9132c02a8..de7dfd6e7 100644 --- a/crates/horizon-client/src/game/storage.rs +++ b/crates/horizon-client/src/game/storage.rs @@ -144,18 +144,8 @@ impl Game<'_> { return; } let range = self.reach() + 4.0; - let near = self.container_parts().iter().all(|((x, y, z), _)| { - let d = [*x, *y, *z] - .iter() - .zip(eye) - .map(|(&lo, e)| { - (f64::from(lo) - e) - .max(e - f64::from(lo) - 1.0) - .max(0.0) - .powi(2) - }) - .sum::(); - d < range * range + let near = self.container_parts().iter().all(|&((x, y, z), _)| { + horizon_client::lan::guest::within_block_range(eye, range, [x, y, z]) }); if !near { self.close_container(); diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index dc10096e3..57fa7c636 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -404,22 +404,16 @@ impl Guest { pub fn eye_height(&self) -> f64 { if self.sneaking { 1.27 } else { 1.62 } } - /// `Player.isWithinBlockInteractionRange(pos, buffer)`: the eyes within the - /// block interaction range (4.5, creative 5) plus `buffer` of the block's box. + /// `Player.isWithinBlockInteractionRange(pos, buffer)` for the guest: + /// its block interaction range is 4.5, 5 in creative. pub fn within_block_range(&self, pos: [i32; 3], buffer: f64) -> bool { let eye = [ self.position[0], self.position[1] + self.eye_height(), self.position[2], ]; - let reach = if self.creative() { 5.0 } else { 4.5 } + buffer; - (0..3) - .map(|k| { - let lo = f64::from(pos[k]); - (lo - eye[k]).max(eye[k] - lo - 1.0).max(0.0).powi(2) - }) - .sum::() - < reach * reach + let range = if self.creative() { 5.0 } else { 4.5 }; + within_block_range(eye, range + buffer, pos) } /// The inventory menu's 46 slots (ServerPlayer.inventoryMenu): 0 crafting /// result, 1..=4 crafting grid, 5..=8 armor, 9..=35 main, 36..=44 hotbar, @@ -585,6 +579,18 @@ fn network_stack( Some(stack) } +/// `Player.isWithinBlockInteractionRange`: `eye` closer than `range` to the +/// box of the block at `pos` (`AABB.distanceToSqr`). +pub fn within_block_range(eye: [f64; 3], range: f64, pos: [i32; 3]) -> bool { + (0..3) + .map(|k| { + let lo = f64::from(pos[k]); + (lo - eye[k]).max(eye[k] - lo - 1.0).max(0.0).powi(2) + }) + .sum::() + < range * range +} + #[cfg(test)] mod tests { use super::*; From 65d3edfc3a1fc391f99dd091e41d031857a61583 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:26:03 +0100 Subject: [PATCH 115/238] Launcher: find the client jar of a Modrinth App mod loader profile (#1783) The Modrinth App keeps a Fabric or Quilt profile's vanilla client jar as `versions/-/-.jar`; a player who only played such a profile has no `versions//`, and Prepare said Minecraft was not found. Those jars are now candidates too, checked against the pinned sha1 like the rest. Co-Authored-By: Claude Opus 5.5 --- launcher/src/sources.rs | 18 ++++++++++++++++++ launcher/src/sources_tests.rs | 23 +++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/launcher/src/sources.rs b/launcher/src/sources.rs index b27b8417b..50953067e 100644 --- a/launcher/src/sources.rs +++ b/launcher/src/sources.rs @@ -191,6 +191,24 @@ fn client_jars(base: &Path, version: &str) -> Vec { jars.push(jar); } } + jars.extend(loader_jars(base, version)); + jars +} + +/// The client jars the Modrinth App keeps for a mod loader's profile: `versions/-/`, +/// the vanilla jar under the loader's name, when only a Fabric or Quilt profile of the version +/// was ever played (#1783). The jar is checked against the pin like the others. +fn loader_jars(base: &Path, version: &str) -> Vec { + let prefix = format!("{version}-"); + let mut jars: Vec = fs::read_dir(base.join("versions")) + .into_iter() + .flatten() + .flatten() + .filter_map(|entry| entry.file_name().into_string().ok()) + .filter(|name| name.starts_with(&prefix)) + .map(|name| format!("versions/{name}/{name}.jar")) + .collect(); + jars.sort(); jars } diff --git a/launcher/src/sources_tests.rs b/launcher/src/sources_tests.rs index 6d882f032..396f50881 100644 --- a/launcher/src/sources_tests.rs +++ b/launcher/src/sources_tests.rs @@ -289,6 +289,29 @@ fn the_modrinth_app_atlauncher_and_gdlauncher_are_found() { } } +/// A Modrinth App that only ever played a Fabric profile of 26.3 keeps the vanilla client jar +/// under the loader's name (#1783); a profile of another version beside it is not taken. +#[test] +fn a_modrinth_loader_profile_has_the_client_jar() { + let s = Scratch::new("modrinth-fabric"); + s.write( + "ModrinthApp/meta/versions/26.3.1-0.18.1/26.3.1-0.18.1.jar", + b"other", + ); + let jar = "versions/26.3-0.18.1/26.3-0.18.1.jar"; + let dir = s.launcher("ModrinthApp", "meta", jar); + s.write( + "ModrinthApp/meta/versions/26.3-0.18.1/26.3-0.18.1.json", + br#"{"id":"26.3-0.18.1","inheritsFrom":"26.3"}"#, + ); + let profile = dir.join("profiles/Fabric 26.3"); + fs::create_dir_all(&profile).unwrap(); + let linked = s.0.join("linked"); + let got = resolve_with(&profile, &[], &linked, &Mojang::new(MANIFEST)).unwrap(); + assert_eq!(got, linked); + assert_linked(&linked, &dir.join("meta"), jar); +} + #[test] fn an_instance_folder_may_be_chosen() { let s = Scratch::new("instance"); From 50727eb5c3d389628549c9237bbcca76029c3b79 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:26:43 +0100 Subject: [PATCH 116/238] Harnesses: one scaffold for one-player dedicated server scripts test_hardcore_respawn_parity.py copied test_locator_bar_parity.py's build and run code; both now share locator.sides, and one-player scripts run through locator.one_player. Co-Authored-By: Claude Opus 5.5 --- tools/test_hardcore_respawn_parity.py | 114 +++----------------------- tools/test_locator_bar_parity.py | 76 ++++++++++++++++- 2 files changed, 84 insertions(+), 106 deletions(-) diff --git a/tools/test_hardcore_respawn_parity.py b/tools/test_hardcore_respawn_parity.py index 6e0669534..8c7a5394d 100644 --- a/tools/test_hardcore_respawn_parity.py +++ b/tools/test_hardcore_respawn_parity.py @@ -8,23 +8,12 @@ vanilla's protocol code) joins, runs /kill on itself and presses the death scree respawns the player, then makes it a spectator because the server is hardcore. The game mode changes and respawns Alice is sent in the death and respawn steps are compared exactly. """ -import json -import os -from pathlib import Path -import platform -import shutil -import subprocess import sys -import time try: - from . import lab, test_rust_parity as base, test_locator_bar_parity as locator - from .test_dedicated_server_parity import Server + from . import test_locator_bar_parity as locator except ImportError: - import lab - import test_rust_parity as base import test_locator_bar_parity as locator - from test_dedicated_server_parity import Server NAME = 'hardcore-respawn-parity' # server_dir and generate_world write these server.properties. @@ -33,103 +22,24 @@ SCRIPT = [ ('death', ['cmd Alice kill @s']), ('respawn', ['respawn Alice']), ] -STEP_SECONDS = 4.0 -SHOWN = ('gamemode', 'respawn') - - -def run_side(label, server_command, client_command, build, world): - directory = build / label - port = locator.server_dir(directory, world) - out = build / f'{label}.txt' - out.unlink(missing_ok=True) - server = Server(server_command, directory, label) - try: - server.wait_for(r'Done \(') - with (build / f'{label}-client.log').open('w') as log: - client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), str(out)], - cwd=lab.ROOT, stdin=subprocess.PIPE, stdout=log, stderr=subprocess.STDOUT, - text=True) - - def tell(line): - client.stdin.write(line + '\n') - client.stdin.flush() - try: - tell('players on') - tell('state on') - tell('step joins') - tell('join Alice') - server.wait_for(r'Alice joined the game', timeout=120) - time.sleep(STEP_SECONDS) - for step, actions in SCRIPT: - tell(f'step {step}') - for action in actions: - tell(action) - time.sleep(STEP_SECONDS) - tell('end') - client.wait(timeout=60) - finally: - if client.poll() is None: - client.kill() - finally: - server.stop() - if not out.exists(): - raise AssertionError(f'{label}: the client wrote no transcript (see {build}/{label}-client.log)') - return out.read_text(encoding='utf-8').splitlines() def shown(lines): """Alice's game mode changes and respawns in the death and respawn steps.""" - keep = [] - for line in lines: - parts = line.split(' ', 3) - if len(parts) >= 3 and parts[1] in ('step=death', 'step=respawn') and parts[2] in SHOWN: - keep.append(line) - return keep + return [line for line in lines if len(parts := line.split(' ', 3)) >= 3 + and parts[1] in ('step=death', 'step=respawn') and parts[2] in ('gamemode', 'respawn')] + + +def check(vanilla, _): + if 'Alice step=respawn gamemode 3' not in vanilla: + return 'vanilla did not make the respawned player a spectator' + return None def main(): - lock, jars, libraries = base.verify_artifacts() - build = lab.LOCAL / NAME - build.mkdir(parents=True, exist_ok=True) - report = build / 'report.json' - report.unlink(missing_ok=True) - oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java' - classes = build / 'classes' - shutil.rmtree(classes, ignore_errors=True) - classes.mkdir() - dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) - subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-nowarn', '-sourcepath', - lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle], - cwd=lab.ROOT, check=True, capture_output=True) - subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, - check=True, capture_output=True) - metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], - cwd=lab.ROOT, text=True)) - binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') - vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] - horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] - client = ['java', '-Xmx1g', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.LocatorBarClient'] - - world = locator.generate_world(vanilla_server, build) - vanilla_all = run_side('vanilla', vanilla_server, client, build, world) - horizon_all = run_side('horizon', horizon_server, client, build, world) - errors = [l for l in vanilla_all + horizon_all if ' error ' in f' {l} ' or 'disconnected' in l] - vanilla, horizon = shown(vanilla_all), shown(horizon_all) - if errors or vanilla != horizon: - import difflib - diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=1, lineterm='')) - raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' - + '\n'.join(errors[:10]) + '\n' + diff[:6000]) - if 'Alice step=respawn gamemode 3' not in vanilla: - raise AssertionError(f'vanilla did not make the respawned player a spectator: the script did not run ' - f'(see {build})') - lab.write_json(report, dict( - schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), - java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], - lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), - scope='A player dying and respawning in a hardcore world on a dedicated server: the respawn and the game ' - 'mode it is sent (spectator).')) - print(f'hardcore respawn: {len(vanilla)} lines identical') + lines = locator.one_player(NAME, SCRIPT, shown, check, 'A player dying and respawning in a hardcore world on a ' + 'dedicated server: the respawn and the game mode it is sent (spectator).') + print(f'hardcore respawn: {len(lines)} lines identical') if __name__ == '__main__': diff --git a/tools/test_locator_bar_parity.py b/tools/test_locator_bar_parity.py index bc7d21750..985b87a35 100644 --- a/tools/test_locator_bar_parity.py +++ b/tools/test_locator_bar_parity.py @@ -205,12 +205,13 @@ def run_side(label, server_command, client_command, build, world): return out.read_text(encoding='utf-8').splitlines() -def main(): +def sides(name): + """The vanilla and Horizon dedicated servers and the LocatorBarClient that joins them, built for harness + `name`: (lock, build directory, vanilla server, Horizon server, client) commands.""" lock, jars, libraries = base.verify_artifacts() - build = lab.LOCAL / NAME + build = lab.LOCAL / name build.mkdir(parents=True, exist_ok=True) - report = build / 'report.json' - report.unlink(missing_ok=True) + (build / 'report.json').unlink(missing_ok=True) oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java' classes = build / 'classes' shutil.rmtree(classes, ignore_errors=True) @@ -228,7 +229,74 @@ def main(): vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] client = ['java', '-Xmx1g', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.LocatorBarClient'] + return lock, build, vanilla_server, horizon_server, client + +def one_player(name, script, shown, check, scope, step_seconds=4.0): + """A harness of one player, Alice: on each server she joins, then runs `script`, a list of (step, client + actions) `step_seconds` apart. The lines `shown(transcript)` keeps must match vanilla's exactly, and + `check(vanilla's shown lines, vanilla's transcript)` returns why vanilla's run is no proof (the script did + not run), or None.""" + lock, build, vanilla_server, horizon_server, client = sides(name) + + def run(label, server_command, world): + directory = build / label + port = server_dir(directory, world) + out = build / f'{label}.txt' + out.unlink(missing_ok=True) + server = Server(server_command, directory, label) + try: + server.wait_for(r'Done \(') + with (build / f'{label}-client.log').open('w') as log: + process = subprocess.Popen([*map(str, client), '127.0.0.1', str(port), str(out)], cwd=lab.ROOT, + stdin=subprocess.PIPE, stdout=log, stderr=subprocess.STDOUT, text=True) + + def tell(line): + process.stdin.write(line + '\n') + process.stdin.flush() + try: + for line in ['players on', 'state on', 'step joins', 'join Alice']: + tell(line) + server.wait_for(r'Alice joined the game', timeout=120) + time.sleep(step_seconds) + for step, actions in script: + tell(f'step {step}') + for action in actions: + tell(action) + time.sleep(step_seconds) + tell('end') + process.wait(timeout=60) + finally: + if process.poll() is None: + process.kill() + finally: + server.stop() + if not out.exists(): + raise AssertionError(f'{label}: the client wrote no transcript (see {build}/{label}-client.log)') + return out.read_text(encoding='utf-8').splitlines() + + world = generate_world(vanilla_server, build) + vanilla_all = run('vanilla', vanilla_server, world) + horizon_all = run('horizon', horizon_server, world) + errors = [l for l in vanilla_all + horizon_all if ' error ' in f' {l} ' or 'disconnected' in l] + vanilla, horizon = shown(vanilla_all), shown(horizon_all) + if errors or vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=1, lineterm='')) + raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' + + '\n'.join(errors[:10]) + '\n' + diff[:6000]) + if (why := check(vanilla, vanilla_all)) is not None: + raise AssertionError(f'{why}: the script did not run (see {build})') + lab.write_json(build / 'report.json', dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), scope=scope)) + return vanilla + + +def main(): + lock, build, vanilla_server, horizon_server, client = sides(NAME) + report = build / 'report.json' world = generate_world(vanilla_server, build) vanilla = run_side('vanilla', vanilla_server, client, build, world) horizon = run_side('horizon', horizon_server, client, build, world) From df23e1bfed0c2fde5d9c319ccae18fed3c6b036b Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:27:41 +0100 Subject: [PATCH 117/238] Keep vanilla's death screen message; resolve with the loaded language - Leave ServerPlayer.die's message as it was: with showDeathMessages off vanilla sends CommonComponents.EMPTY to the death screen, and #1858 reworks the death message from the combat record. - The effects panel and recipe book tooltips use Names::display_get, so a key missing from the language still falls back instead of showing the key. - net::load_language reuses the language lang already loaded (packs included) instead of reading it from disk again. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/lan_actions.rs | 48 +++++++++---------- crates/horizon-client/src/game/potions.rs | 2 +- crates/horizon-client/src/game/recipe_book.rs | 2 +- crates/horizon-client/src/net.rs | 18 ++++--- 4 files changed, 37 insertions(+), 33 deletions(-) diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 03bb15b65..2c7019c73 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -1791,33 +1791,33 @@ impl Game<'_> { // ServerPlayer.die: dropAllDeathLoot unless a spectator, from the eyes less 0.3. let drops = (g.game_mode != SPECTATOR) .then(|| [at[0], at[1] + g.eye_height() - 0.3, at[2]]); - // ServerPlayer.die: the death screen always shows the message; - // chat only when the rule allows and the team does not hide it. let cause = host.play.get_mut(&conn).and_then(|p| p.death_cause.take()); - let key = cause.unwrap_or_else(|| { - if at[1] < f64::from(horizon_client::chunk::MIN_Y - 64) { - "death.attack.outOfWorld".into() - } else { - "death.attack.generic".into() - } - }); - let text = - horizon_client::text::Text::translate(&key, &[self.player_text(&name, uuid)]); - let message = horizon_client::lan::data::component_nbt(&text.component()); - if self.ui.rules.bool("show_death_messages") { - let team = self.score.board.players_team(&name); - if horizon_client::scoreboard::own_death_message_shown(team) { - host.hub.system_message(message.clone()); - if self.options.dedicated.is_some() { - super::dedicated::log( - "INFO", - &format!("System chat: {}", text.plain()), - ); - } else if self.player.is_some() { - self.system_message(text); + let message = match cause { + // ServerPlayer.die: the death message, to everyone unless + // the player's team hides it. + Some(key) if self.ui.rules.bool("show_death_messages") => { + let text = horizon_client::text::Text::translate( + &key, + &[self.player_text(&name, uuid)], + ); + let message = horizon_client::lan::data::component_nbt(&text.component()); + let team = self.score.board.players_team(&name); + if horizon_client::scoreboard::own_death_message_shown(team) { + host.hub.system_message(message.clone()); + if self.options.dedicated.is_some() { + super::dedicated::log( + "INFO", + &format!("System chat: {}", text.plain()), + ); + } else if self.player.is_some() { + self.system_message(text); + } } + message } - } + Some(_) => literal(""), + None => literal(&format!("{name} died")), + }; host.hub.send_to( conn, &C::PlayerCombatKill(play::PlayerCombatKill { id, message }), diff --git a/crates/horizon-client/src/game/potions.rs b/crates/horizon-client/src/game/potions.rs index 49e171ce1..b28c2fb4d 100644 --- a/crates/horizon-client/src/game/potions.rs +++ b/crates/horizon-client/src/game/potions.rs @@ -755,7 +755,7 @@ impl Game<'_> { image_width, &self.potions.effects.list, self.clock.rate(), - &|key| Some(horizon_client::lang::get(key)), + &|key| self.pack.names.display_get(key), &|text| self.gui.atlas.font.width(text).ceil() as i32, ); if let Some(lines) = inventory_effects::paint( diff --git a/crates/horizon-client/src/game/recipe_book.rs b/crates/horizon-client/src/game/recipe_book.rs index f68460b7d..fd44041e5 100644 --- a/crates/horizon-client/src/game/recipe_book.rs +++ b/crates/horizon-client/src/game/recipe_book.rs @@ -502,7 +502,7 @@ impl Game<'_> { horizon_client::item_tooltip::lines( stack, self.res.items().get(&stack.item), - &|k| Some(horizon_client::lang::get(k)), + &|k| self.pack.names.display_get(k), ) }) { diff --git a/crates/horizon-client/src/net.rs b/crates/horizon-client/src/net.rs index 2b314f2a8..8acfa7ec9 100644 --- a/crates/horizon-client/src/net.rs +++ b/crates/horizon-client/src/net.rs @@ -597,14 +597,18 @@ pub fn json_text(v: &serde_json::Value, lang: &Language) -> String { } } -/// The language a session resolves server-sent components with: the client's -/// selected language, layered over `en_us` (`ClientLanguage.loadFrom`), so a -/// menu title the server sends as a key follows the language screen. +/// The language a session resolves server-sent components with: the selected +/// one as `lang` loaded it (`en_us` under it, packs included), so a menu title +/// the server sends as a key follows the language screen. A process with no +/// language set (a dedicated server's log) reads en_us.json. pub fn load_language(resources: &std::path::Path) -> Language { - let root = resources.to_path_buf(); - crate::lang::Language::load(&[&root], &crate::lang::code()) - .map() - .clone() + if let Some(lang) = crate::lang::current() { + return lang.map().clone(); + } + std::fs::read(resources.join("assets/minecraft/lang/en_us.json")) + .ok() + .and_then(|b| serde_json::from_slice(&b).ok()) + .unwrap_or_default() } /// Ask a server for its status (ServerStatusPinger): MOTD, players and the From 70aff6e90b1689bd2f365825fa34308aa46e65fa Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:30:01 +0100 Subject: [PATCH 118/238] Death screen: empty message with showDeathMessages off; forget a guest's record when they leave - ServerPlayer.die sends CommonComponents.EMPTY to the death screen when showDeathMessages is off, as the local player's death_message already does; the combat record is only read when the rule is on. - A guest's CombatTracker leaves guest_combat with them, so a long-running server doesn't keep one per connection. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/lan.rs | 1 + crates/horizon-client/src/game/lan_actions.rs | 30 +++++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 8460f54e6..4315cc56d 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -979,6 +979,7 @@ impl Game<'_> { self.save_guest(&host, &guest); host.models.remove(&guest.conn); host.play.remove(&guest.conn); + self.guest_combat.remove(&guest.conn); if self.options.dedicated.is_some() && guest.placed { super::dedicated::log( "INFO", diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index f5d419357..fec2f7251 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -1807,18 +1807,19 @@ impl Game<'_> { let drops = (g.game_mode != SPECTATOR) .then(|| [at[0], at[1] + g.eye_height() - 0.3, at[2]]); let cause = host.play.get_mut(&conn).and_then(|p| p.death_cause.take()); - let me = self.player_text(&name, uuid); - let text = match cause { - Some(key) => horizon_client::text::Text::translate(&key, &[me]), - None if at[1] < f64::from(horizon_client::chunk::MIN_Y - 64) => { - horizon_client::text::Text::translate("death.attack.outOfWorld", &[me]) - } - None => self.guest_death_message(conn, me), - }; - let message = horizon_client::lan::data::component_nbt(&text.component()); - // ServerPlayer.die: the message goes to everyone unless the - // player's team hides it, and always to the dying player. - if self.ui.rules.bool("show_death_messages") { + // ServerPlayer.die: with showDeathMessages, the message goes to + // the dying player's death screen and to everyone the team lets + // see it; without it, the death screen gets an empty one. + let message = if self.ui.rules.bool("show_death_messages") { + let me = self.player_text(&name, uuid); + let text = match cause { + Some(key) => horizon_client::text::Text::translate(&key, &[me]), + None if at[1] < f64::from(horizon_client::chunk::MIN_Y - 64) => { + horizon_client::text::Text::translate("death.attack.outOfWorld", &[me]) + } + None => self.guest_death_message(conn, me), + }; + let message = horizon_client::lan::data::component_nbt(&text.component()); let team = self.score.board.players_team(&name); if horizon_client::scoreboard::own_death_message_shown(team) { host.hub.system_message(message.clone()); @@ -1831,7 +1832,10 @@ impl Game<'_> { self.system_message(text); } } - } + message + } else { + literal("") + }; host.hub.send_to( conn, &C::PlayerCombatKill(play::PlayerCombatKill { id, message }), From 0609fb2bebdc082c335361c3da65a188a8f77ef0 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:30:37 +0100 Subject: [PATCH 119/238] Tooltip: lore after the enchantments, as ItemStack.addDetailsToTooltip Vanilla adds LORE after CONTAINER, BANNER_PATTERNS, FIREWORKS, POTION_CONTENTS, TRIM and the enchantments, and before the attribute modifiers, not right under the name. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/stations.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/horizon-client/src/game/stations.rs b/crates/horizon-client/src/game/stations.rs index fd7bd2642..5d8a2c3fe 100644 --- a/crates/horizon-client/src/game/stations.rs +++ b/crates/horizon-client/src/game/stations.rs @@ -1674,12 +1674,6 @@ impl Game<'_> { if display.name_visible(self.screen == Screen::Creative) { lines.push((horizon_client::text::Text::legacy(&name), color)); } - // ItemLore.addToTooltip: after the name, before other component lines. - if display.shows("minecraft:lore") { - for text in horizon_client::item_tooltip::lore_texts(stack) { - lines.push((text, u32::MAX)); - } - } let translate = |k: &str| e.translate(k).map(str::to_owned); let push = |lines: &mut Vec<(horizon_client::text::Text, u32)>, s: String, c: u32| { lines.push((horizon_client::text::Text::legacy(&s), c)); @@ -1776,6 +1770,13 @@ impl Game<'_> { ); } } + // ItemLore.addToTooltip: after the enchantments, before the attributes + // (ItemStack.addDetailsToTooltip). + if display.shows("minecraft:lore") { + for text in horizon_client::item_tooltip::lore_texts(stack) { + lines.push((text, u32::MAX)); + } + } // Spawner.appendHoverText: what a spawner or trial spawner item spawns. if display.shows("minecraft:block_entity_data") && matches!( From 0298d109886f0679f3a3ae11d001f0b81224026c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:31:37 +0100 Subject: [PATCH 120/238] Shulker boxes: one canFitInsideContainerItems for both menus, and a unit test The client's menu and the server's guest menus now share menu::fits_inside_container_items, and a resource-backed test checks that a guest's shulker box menu refuses shulker boxes by pickup, shift-click and number key while other containers take them. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/lan/menu.rs | 70 +++++++++++++++++++++------ crates/horizon-client/src/menu.rs | 13 ++--- 2 files changed, 61 insertions(+), 22 deletions(-) diff --git a/crates/horizon-client/src/lan/menu.rs b/crates/horizon-client/src/lan/menu.rs index fefcb1933..8e8772055 100644 --- a/crates/horizon-client/src/lan/menu.rs +++ b/crates/horizon-client/src/lan/menu.rs @@ -155,11 +155,9 @@ impl View<'_> { if self.container.is_none() && (5..=8).contains(&i) { return rules.armor_index(&stack.item) == Some(i - 5); } - // ShulkerBoxSlot.mayPlace: Item.canFitInsideContainerItems, false - // for shulker boxes. if self.shulker && self.container.as_ref().is_some_and(|c| i < c.len()) - && stack.item.ends_with("shulker_box") + && !crate::menu::fits_inside_container_items(stack) { return false; } @@ -675,6 +673,24 @@ mod tests { ) } + /// The prepared resources' items, or None (the test is skipped) without them. + fn rules() -> Option { + let root = std::env::var_os("HORIZON_RESOURCES") + .map(std::path::PathBuf::from) + .unwrap_or_else(|| { + std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../../.local/horizon-client") + .join(horizon_core::VANILLA_VERSION) + }); + if !root.join("items.json").exists() { + eprintln!("skipping resource-backed menu check: {}", root.display()); + return None; + } + let registry = horizon_world::level::data::load_registry(&root).unwrap(); + let facts = crate::blocks::BlockFacts::load(&root, ®istry).unwrap(); + Some(ItemRules(Items::load(&root, ®istry, &facts).unwrap())) + } + fn contents(stack: &Option) -> Vec<(String, u32)> { let inside = stack.as_ref().and_then(|s| s.data()); inside.map_or(Vec::new(), |d| { @@ -690,20 +706,9 @@ mod tests { /// slot; a stack clicked onto a bundle goes in, as far as it fits. #[test] fn guest_chest_clicks_fill_and_empty_bundles() { - let root = std::env::var_os("HORIZON_RESOURCES") - .map(std::path::PathBuf::from) - .unwrap_or_else(|| { - std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("../../.local/horizon-client") - .join(horizon_core::VANILLA_VERSION) - }); - if !root.join("items.json").exists() { - eprintln!("skipping resource-backed bundle check: {}", root.display()); + let Some(rules) = rules() else { return; - } - let registry = horizon_world::level::data::load_registry(&root).unwrap(); - let facts = crate::blocks::BlockFacts::load(&root, ®istry).unwrap(); - let rules = ItemRules(Items::load(&root, ®istry, &facts).unwrap()); + }; let mut g = guest(); let mut chest = vec![ Some(ItemStack::new("minecraft:stone", 48)), @@ -747,4 +752,37 @@ mod tests { ); assert_eq!(contents(&view.get(2)), []); } + + #[test] + fn guest_shulker_box_menu_refuses_shulker_boxes() { + let Some(rules) = rules() else { + return; + }; + for shulker in [true, false] { + let mut g = guest(); + let mut container = vec![None; 27]; + let mut drag = None; + let mut view = View { + guest: &mut g, + container: Some(&mut container), + shulker, + }; + // Carried onto an empty slot, shift-clicked from the hotbar + // (menu slot 27 + 27) and swapped in with the number key 1. + view.guest.carried = Some(ItemStack::new("minecraft:red_shulker_box", 1)); + click(&mut view, &mut drag, 0, 0, input::PICKUP, &rules); + assert_eq!(view.get(0).is_none(), shulker); + view.guest.carried = None; + view.guest.inventory[0] = Some(ItemStack::new("minecraft:shulker_box", 1)); + click(&mut view, &mut drag, 54, 0, input::QUICK_MOVE, &rules); + assert_eq!(view.guest.inventory[0].is_some(), shulker); + view.guest.inventory[0] = Some(ItemStack::new("minecraft:blue_shulker_box", 1)); + click(&mut view, &mut drag, 5, 0, input::SWAP, &rules); + assert_eq!(view.get(5).is_none(), shulker); + // Anything else goes in. + view.guest.carried = Some(ItemStack::new("minecraft:diamond", 1)); + click(&mut view, &mut drag, 9, 0, input::PICKUP, &rules); + assert!(view.get(9).is_some()); + } + } } diff --git a/crates/horizon-client/src/menu.rs b/crates/horizon-client/src/menu.rs index 07e4ba2b9..764abe0d9 100644 --- a/crates/horizon-client/src/menu.rs +++ b/crates/horizon-client/src/menu.rs @@ -247,6 +247,12 @@ pub fn beacon_required_levels(effect: Option<&str>) -> i32 { .map_or(i32::MAX, |i| i as i32 + 1) } +/// `Item.canFitInsideContainerItems` (`ShulkerBoxSlot.mayPlace`): shulker +/// boxes cannot nest. +pub fn fits_inside_container_items(stack: &ItemStack) -> bool { + !stack.item.ends_with("shulker_box") +} + /// The containers a menu spans. const PLAYER: usize = 0; const MAIN: usize = 1; @@ -1210,11 +1216,6 @@ impl Menu { env.items.get(&stack.item).is_some_and(|i| i.fuel.is_some()) } - /// `Item.canFitInsideContainerItems`: shulker boxes cannot nest. - fn fits_inside(stack: &ItemStack) -> bool { - !(stack.item.ends_with("shulker_box")) - } - // ---- Slot ---- fn may_place(&self, env: &Env, s: usize, stack: &ItemStack) -> bool { @@ -1247,7 +1248,7 @@ impl Menu { .as_deref() .is_some_and(|m| i.equip_allowed.as_ref().is_none_or(|a| a.contains(m))) }), - Role::Shulker => Self::fits_inside(stack), + Role::Shulker => fits_inside_container_items(stack), Role::Fuel => Self::is_fuel(env, stack) || Self::is_bucket(stack), Role::Output | Role::CraftResult | Role::Inert => false, Role::CrafterIn => self.data.get(self.slots[s].index) != Some(&1), From 71f0ffde4bf15a0d7b06a25a9bc4890ae0af344b Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:31:44 +0100 Subject: [PATCH 121/238] Prepare: a launcher's rewritten version file falls back to Mojang's (#1772) Launchers like HMCL rewrite versions//.json in the official layout (same jars and libraries, their own fields), and horizon-prepare refused it as a version Mojang may have republished. Mojang's copy saved as the imported manifest.json is now used instead, and without one the error says the launcher may have rewritten it and how to get Mojang's. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-prepare/src/artifacts.rs | 67 ++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 7 deletions(-) diff --git a/crates/horizon-prepare/src/artifacts.rs b/crates/horizon-prepare/src/artifacts.rs index 805dd80d4..bd22df079 100644 --- a/crates/horizon-prepare/src/artifacts.rs +++ b/crates/horizon-prepare/src/artifacts.rs @@ -199,13 +199,22 @@ pub(crate) fn import_manifest(lock: &Lock, minecraft: &Path) -> Result Date: Sun, 25 Oct 2026 17:36:22 +0100 Subject: [PATCH 122/238] /pregen: plan any radius at once, and skip a strip's rows outside the circle Pregen::new counted the area chunk by chunk and split the strips with an i32 product: /pregen start 30000000 overflowed, then hung the server thread for hours. Rows are now counted from the circle's half-width, the strip edges in i64, and each strip starts and ends at the rows the circle reaches, so a band never walks empty corner rows. Unit tests check that every chunk of squares and circles is swept once and that the largest radius plans at once. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/pregen.rs | 99 ++++++++++++++++++++++++----- 1 file changed, 84 insertions(+), 15 deletions(-) diff --git a/crates/horizon-client/src/pregen.rs b/crates/horizon-client/src/pregen.rs index e714bcda2..01215928a 100644 --- a/crates/horizon-client/src/pregen.rs +++ b/crates/horizon-client/src/pregen.rs @@ -39,48 +39,69 @@ impl Pregen { let radius = radius.max(0); let (x0, width) = (center.0 - radius, 2 * radius + 1); let count = (width + STRIP_WIDTH - 1) / STRIP_WIDTH; - let strips = (0..count) - .map(|i| (x0 + width * i / count, x0 + width * (i + 1) / count - 1)) - .collect(); + // The columns `i * width / count` on, in i64: the product overflows + // for a radius of a few hundred thousand chunks. + let edge = |i: i32| x0 + (i64::from(width) * i64::from(i) / i64::from(count)) as i32; + let strips = (0..count).map(|i| (edge(i), edge(i + 1) - 1)).collect(); let mut pregen = Self { center, radius, shape, strips, strip: 0, - row: center.1 - radius, + row: 0, done: 0, total: 0, }; pregen.total = (center.1 - radius..=center.1 + radius) .map(|z| pregen.row_chunks(x0, x0 + width - 1, z)) .sum(); + pregen.row = pregen.rows().map_or(0, |r| r.0); pregen } - fn contains(&self, x: i32, z: i32) -> bool { + /// How far from the centre column the area reaches at `d` chunks from + /// the centre row (or, the same, from the centre column), if it does. + fn half_width(&self, d: i32) -> Option { + let (r, d) = (i64::from(self.radius), i64::from(d)); match self.shape { - Shape::Square => true, - Shape::Circle => { - let (dx, dz) = (i64::from(x - self.center.0), i64::from(z - self.center.1)); - dx * dx + dz * dz <= i64::from(self.radius) * i64::from(self.radius) - } + Shape::Square => (d.abs() <= r).then_some(self.radius), + Shape::Circle => (d * d <= r * r).then(|| (r * r - d * d).isqrt() as i32), } } + fn contains(&self, x: i32, z: i32) -> bool { + self.half_width(z - self.center.1) + .is_some_and(|h| (x - self.center.0).abs() <= h) + } + + /// The area's chunks in columns `x0..=x1` of row `z`. fn row_chunks(&self, x0: i32, x1: i32, z: i32) -> u64 { - (x0..=x1).filter(|&x| self.contains(x, z)).count() as u64 + self.half_width(z - self.center.1).map_or(0, |h| { + let (lo, hi) = (x0.max(self.center.0 - h), x1.min(self.center.0 + h)); + (i64::from(hi) - i64::from(lo) + 1).max(0) as u64 + }) + } + + /// The first and last rows of the current strip that hold chunks of the + /// area. + fn rows(&self) -> Option<(i32, i32)> { + let &(x0, x1) = self.strips.get(self.strip)?; + let nearest = (x0 - self.center.0).max(self.center.0 - x1).max(0); + let h = self.half_width(nearest)?; + Some((self.center.1 - h, self.center.1 + h)) } /// The band streaming should hold now; none once every strip is done. pub fn band(&self) -> Option { let &(x0, x1) = self.strips.get(self.strip)?; + let (_, last) = self.rows()?; let rows = (BAND_CHUNKS + (x1 - x0)) / (x1 - x0 + 1); Some(Band { x0, x1, z0: self.row, - z1: (self.row + rows - 1).min(self.center.1 + self.radius), + z1: (self.row + rows - 1).min(last), circle: (self.shape == Shape::Circle).then_some((self.center, self.radius)), }) } @@ -89,16 +110,16 @@ impl Pregen { /// Returns whether the band moved. pub fn advance(&mut self, lit: impl Fn(i32, i32) -> bool) -> bool { let mut moved = false; - while let Some(&(x0, x1)) = self.strips.get(self.strip) { + while let (Some(&(x0, x1)), Some((_, last))) = (self.strips.get(self.strip), self.rows()) { let z = self.row; if !(x0..=x1).all(|x| !self.contains(x, z) || lit(x, z)) { break; } self.done += self.row_chunks(x0, x1, z); self.row += 1; - if self.row > self.center.1 + self.radius { + if self.row > last { self.strip += 1; - self.row = self.center.1 - self.radius; + self.row = self.rows().map_or(self.row, |r| r.0); } moved = true; } @@ -120,3 +141,51 @@ impl Pregen { self.strip >= self.strips.len() } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn sweeps_every_chunk_of_the_area_once() { + for shape in [Shape::Square, Shape::Circle] { + for (center, radius) in [((0, 0), 0), ((3, -7), 5), ((-40, 12), 130), ((0, 0), 300)] { + let mut p = Pregen::new(center, radius, shape); + let r = i64::from(radius); + let area = (-radius..=radius) + .flat_map(|dz| (-radius..=radius).map(move |dx| (dx, dz))) + .filter(|&(dx, dz)| { + shape == Shape::Square + || i64::from(dx).pow(2) + i64::from(dz).pow(2) <= r * r + }) + .count() as u64; + assert_eq!(p.total, area, "{shape:?} {radius}"); + let mut seen = std::collections::HashSet::new(); + while !p.finished() { + let band = p.band().unwrap(); + assert!(band.z0 <= band.z1); + let row = p.first_row(); + for &(x, z) in &row { + assert!(seen.insert((x, z)), "{x} {z} twice"); + } + assert!( + p.advance(|x, z| z == band.z0 && row.contains(&(x, z))) || row.is_empty() + ); + } + assert_eq!( + (p.done, seen.len() as u64), + (area, area), + "{shape:?} {radius}" + ); + } + } + } + + #[test] + fn a_huge_area_is_planned_at_once() { + let started = std::time::Instant::now(); + let p = Pregen::new((0, 0), 30_000_000 >> 4, Shape::Circle); + assert!(p.total > 11_000_000_000_000); + assert!(started.elapsed() < std::time::Duration::from_secs(5)); + } +} From b729cfc83361456ffcb3ff926dbb97ad617cf600 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:37:22 +0100 Subject: [PATCH 123/238] Furnace experience: createExperience's chance, orbs, every removal, guests - createExperience floors count * experience and adds one with the chance of the fraction (a level random nextFloat); flooring alone paid nothing for a single iron ore (0.7). - The payout pops ExperienceOrb.award's orbs at the player, recipe by recipe, instead of adding points directly. - A server's player (the default single-player path, LAN and dedicated) gets it too: lan_machine_click pays when the guest takes from the output. - AbstractFurnaceBlockEntity.preRemoveSideEffects pays at the block's centre on any removal (explosions, /setblock, creative breaking), after the contents spill, so it moves from break_block to the removal side effects. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/containers.rs | 69 ++++++++++--------- crates/horizon-client/src/game.rs | 64 ++++++++--------- .../horizon-client/src/game/lan_crafting.rs | 7 ++ crates/horizon-client/src/game/spill.rs | 19 ++++- 4 files changed, 90 insertions(+), 69 deletions(-) diff --git a/crates/horizon-client/src/containers.rs b/crates/horizon-client/src/containers.rs index 3701fefaf..26ca193a2 100644 --- a/crates/horizon-client/src/containers.rs +++ b/crates/horizon-client/src/containers.rs @@ -488,25 +488,13 @@ impl Furnace { self.cooking_timer = 0; } } - /// `AbstractFurnaceBlockEntity.createExperience`: each recipe's own share is floored, - /// then the floors are added. Flooring the sum instead pays one point for two recipes - /// worth 0.6 each, which vanilla does not. - fn owed(used: &HashMap, experience: impl Fn(&str) -> f32) -> u32 { - used.iter() - .map(|(id, count)| (experience(id) * *count as f32).floor().max(0.0) as u32) - .sum() - } - /// What this furnace owes right now, without forgetting it: breaking the block reads - /// the same counter, and only a payout clears it. - pub fn experience_owed(&self, recipes: &Recipes) -> u32 { - Self::owed(&self.recipes_used, |id| recipes.cooking_experience(id)) - } - /// `AbstractFurnaceBlockEntity.awardUsedRecipesAndPopExperience`: the experience this - /// furnace owes, forgotten as it is paid. - pub fn take_experience(&mut self, recipes: &Recipes) -> u32 { - let owed = self.experience_owed(recipes); - self.recipes_used.clear(); - owed + /// `recipesUsed`, each recipe with its `experience` (0 for one this data pack does + /// not have), in the order `getRecipesToAwardAndPopExperience` pays them. + pub fn recipe_experience(&self, recipes: &Recipes) -> Vec<(u32, f32)> { + self.recipes_used + .iter() + .map(|(id, count)| (*count, recipes.cooking_experience(id))) + .collect() } /// A menu click wrote its slots into `items`, and `before` was the input /// slot until then: the menu called `setItem` on it only if it changed @@ -621,23 +609,40 @@ fn total_time(time: u32, speed: f32) -> i32 { } } +/// `AbstractFurnaceBlockEntity.createExperience`: `count * experience`, floored, and one +/// more with the chance of what is left over (a level random draw only when there is some). +pub fn create_experience( + count: u32, + experience: f32, + random: &mut impl horizon_core::random::RandomSource, +) -> u32 { + let total = count as f32 * experience; + let whole = total.floor(); + let fraction = total - whole; + let mut xp = whole.max(0.0) as u32; + if fraction != 0.0 && random.next_float() < fraction { + xp += 1; + } + xp +} + #[cfg(test)] mod tests { use super::*; + use horizon_core::random::{LegacyRandom, RandomSource}; - /// `createExperience` floors each recipe on its own. Two recipes worth 0.6 each pay - /// nothing, where flooring their sum would pay one point. + /// A whole amount pays itself without a draw; a fraction is a chance of one more. #[test] - fn experience_is_floored_per_recipe_and_not_on_the_sum() { - let mut used = HashMap::new(); - used.insert("a".to_owned(), 1); - used.insert("b".to_owned(), 1); - assert_eq!(Furnace::owed(&used, |_| 0.6), 0); - - used.insert("c".to_owned(), 10); - assert_eq!( - Furnace::owed(&used, |id| if id == "c" { 0.1 } else { 0.0 }), - 1 - ); + fn create_experience_rounds_the_fraction_by_chance() { + let mut random = LegacyRandom::new(7); + let mut untouched = LegacyRandom::new(7); + assert_eq!(create_experience(4, 0.5, &mut random), 2); + assert_eq!(create_experience(3, 0.0, &mut random), 0); + assert_eq!(random.next_long(), untouched.next_long()); + let paid: u32 = (0..1000) + .map(|_| create_experience(1, 0.35, &mut random)) + .sum(); + assert!((250..450).contains(&paid), "{paid}"); + assert!((0..100).all(|_| (1..=2).contains(&create_experience(1, 1.7, &mut random)))); } } diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 7c0bbc917..808ca087f 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -537,6 +537,8 @@ struct GameLevel<'w> { seed: i64, behaviours: &'w Behaviours, items: &'w Items, + /// Cooking recipes' experience, which a removed furnace pays out. + recipes: &'w horizon_client::crafting::Recipes, /// Container loot tables (`unpackLootTable`). loot: Option>, registry: &'static horizon_world::registry::BlockRegistry, @@ -2420,6 +2422,7 @@ impl Game<'_> { seed: self.level.seed, behaviours: self.res.behaviours(), items: self.res.items(), + recipes: self.res.recipes(), loot: horizon_client::structure_blocks::loot_data(&self.options.resources), registry: self.pack.assets.registry, rules, @@ -3246,12 +3249,6 @@ impl Game<'_> { } // The block entity as it was, for what the drops copy from it. let key = (pos[0], pos[1], pos[2]); - // AbstractFurnaceBlockEntity.playerDestroy: the experience its recipes earned drops - // with the block. Read now, paid only if the block is really removed. - let furnace_xp = match self.world.block_entities.get(&key) { - Some(BlockEntity::Furnace(f)) => f.experience_owed(self.res.recipes()), - _ => 0, - }; if let Some(entity) = self.world.block_entities.get(&key).cloned() { let name = self.world.block_names.get(&key).cloned(); self.pending.record(pos, &entity, &facts.name, name); @@ -3321,18 +3318,6 @@ impl Game<'_> { ); } } - // Stored recipe experience is not a loot drop: it leaves with the block - // whatever the tool, as `AbstractFurnaceBlockEntity` does. - if furnace_xp > 0 { - self.spawn_orbs( - [ - f64::from(pos[0]) + 0.5, - f64::from(pos[1]) + 0.5, - f64::from(pos[2]) + 0.5, - ], - furnace_xp, - ); - } } self.pending.clear(pos); self.process_level_events()?; @@ -6007,14 +5992,12 @@ impl Game<'_> { self.player_drop(stack); } self.award_crafted(&crafted); - // shortcut: paid straight to the player instead of through `ExperienceOrb.award` - // (no orb flies at them), which a furnace's orbs reach within a tick anyway; - // upgrade if the visual matters or another block pays out far from the player. - if furnace_before > furnace_after { - let owed = self.take_furnace_experience(); - if owed > 0 { - self.give_orb_points(owed); - } + if furnace_before > furnace_after + && self.remote.is_none() + && let (Some(pos), Some(p)) = (self.container, self.player.as_ref()) + { + let at = [p.position.x, p.position.y, p.position.z]; + self.award_furnace_recipes(pos, at); } } if kind == Kind::Loom { @@ -8807,17 +8790,26 @@ impl Game<'_> { self.give_orb_points(gained); } } - /// `AbstractFurnaceBlockEntity.awardUsedRecipesAndPopExperience`: what the open furnace - /// owes for everything it made since the last payout, cleared as it is read. - fn take_furnace_experience(&mut self) -> u32 { - let Some(pos) = self.container else { - return 0; + /// `FurnaceResultSlot.checkTakeAchievements`: the player took from the output of the + /// furnace at `pos`, which pops the experience of what it made at them + /// (`awardUsedRecipesAndPopExperience`) and forgets it. + pub(super) fn award_furnace_recipes(&mut self, pos: (i32, i32, i32), at: [f64; 3]) { + let used = match self.world.block_entities.get_mut(&pos) { + Some(BlockEntity::Furnace(f)) => { + let used = f.recipe_experience(self.res.recipes()); + f.recipes_used.clear(); + used + } + _ => return, }; - let recipes = self.res.recipes(); - let Some(BlockEntity::Furnace(f)) = self.world.block_entities.get_mut(&pos) else { - return 0; - }; - f.take_experience(recipes) + for (count, experience) in used { + let xp = horizon_client::containers::create_experience( + count, + experience, + &mut self.world.random, + ); + self.spawn_orbs(at, xp); + } } /// `Player.giveExperiencePoints`, and `giveExperienceLevels`' level-up /// sound every fifth level (at most every 100 ticks). diff --git a/crates/horizon-client/src/game/lan_crafting.rs b/crates/horizon-client/src/game/lan_crafting.rs index cb3b0a2c9..3199894dd 100644 --- a/crates/horizon-client/src/game/lan_crafting.rs +++ b/crates/horizon-client/src/game/lan_crafting.rs @@ -225,6 +225,7 @@ impl Game<'_> { Some(BlockEntity::Crafter(c)) => (Kind::CrafterBlock, c.items.to_vec()), _ => return false, }; + let furnace = matches!(kind, Kind::Furnace(_)); // CrafterBlockEntity.containerData: the disabled slots, then triggered. let crafter_data = match self.world.block_entities.get(&pos) { Some(BlockEntity::Crafter(c)) => Some((c.disabled, c.triggered)), @@ -284,6 +285,12 @@ impl Game<'_> { } _ => {} } + // FurnaceResultSlot.checkTakeAchievements: the guest took from the output. + let count = |s: Option<&ItemStack>| s.map_or(0, |s| s.count); + if furnace && count(menu.slot(2)) < count(items[2].as_ref()) { + let at = host.hub.guests[gi].position; + self.award_furnace_recipes(pos, at); + } host.play.entry(conn).or_default().crafting.machine = Some((p.container, menu)); host.hub.inventory_clicked(conn, p, &self.world, &changed); true diff --git a/crates/horizon-client/src/game/spill.rs b/crates/horizon-client/src/game/spill.rs index 20dcdf0de..b7283c57b 100644 --- a/crates/horizon-client/src/game/spill.rs +++ b/crates/horizon-client/src/game/spill.rs @@ -373,12 +373,29 @@ impl GameLevel<'_> { let mut neighbour_updates = 0; if side_effects { match kind { - PreRemove::Contents { slots, .. } => { + PreRemove::Contents { slots, furnace } => { let items = entity.as_ref().map_or(&[][..], BlockEntity::slots); for slot in 0..slots { let stack = items.get(slot).cloned().flatten(); self.drop_item_stack(pos, stack); } + // AbstractFurnaceBlockEntity.preRemoveSideEffects: then the + // experience of what it made, at the block's centre. + if furnace && let Some(BlockEntity::Furnace(f)) = &entity { + let at = [ + f64::from(pos.x) + 0.5, + f64::from(pos.y) + 0.5, + f64::from(pos.z) + 0.5, + ]; + for (count, experience) in f.recipe_experience(self.recipes) { + let xp = horizon_client::containers::create_experience( + count, + experience, + &mut self.world.random, + ); + self.award_orbs(at, xp); + } + } } PreRemove::Campfire => { for _ in 0..4 { From a495fcf7a7af9ee0cc475bbccb810ae1c23d9755 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:37:51 +0100 Subject: [PATCH 124/238] End crystal: one blast call for both paths The respawn check is read before the blast, so the two copies of the explosion collapse into one. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/dragon.rs | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/crates/horizon-client/src/game/dragon.rs b/crates/horizon-client/src/game/dragon.rs index 701c09e64..ddd5a716b 100644 --- a/crates/horizon-client/src/game/dragon.rs +++ b/crates/horizon-client/src/game/dragon.rs @@ -880,30 +880,22 @@ impl Game<'_> { return; } let c = self.crystals.remove(index); - if self.fight.respawn.is_some() + // Read before the blast, which can take other respawn crystals. + let respawn_crystal = self.fight.respawn.is_some() && self .fight .respawn_crystals .iter() - .any(|p| p.distance_squared(c.position) < 0.01) - { - if damage == CrystalDamage::Direct { - let at = [c.position.x, c.position.y, c.position.z]; - let _ = self.server_explode( - at, - 6.0, - super::explosion::BlockInteraction::Destroy, - false, - ); - } - self.abort_respawn(); - return; - } + .any(|p| p.distance_squared(c.position) < 0.01); if damage == CrystalDamage::Direct { let at = [c.position.x, c.position.y, c.position.z]; let _ = self.server_explode(at, 6.0, super::explosion::BlockInteraction::Destroy, false); } + if respawn_crystal { + self.abort_respawn(); + return; + } // EnderDragonFight.onCrystalDestroyed. self.count_crystals(); if let Some(d) = self.dragon.as_mut() { From 0dfc6d8700554d0d0b8bfb93cc00cd14062daa49 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:38:28 +0100 Subject: [PATCH 125/238] command_exec harness: summoned mobs' saved attribute bases and health (#1483) A cow summoned with max_health and movement_speed bases, and a zombie with an attack_damage base and a saved Health, against vanilla. Co-Authored-By: Claude Opus 5.5 --- tools/command_exec_corpus.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tools/command_exec_corpus.py b/tools/command_exec_corpus.py index 0e376e260..be4e16a8a 100644 --- a/tools/command_exec_corpus.py +++ b/tools/command_exec_corpus.py @@ -291,6 +291,19 @@ def entities(): cmd('/data get entity @e[tag=root,limit=1] Passengers[0].Health'), cmd('/data get entity @e[tag=root,limit=1] Passengers[0].id'), cmd('/kill @e[tag=root]'), + # Saved attribute bases are read for every mob, then Health or the max + # health when there is none (LivingEntity.readAdditionalSaveData, #1483). + cmd('/summon minecraft:cow 4.5 210 8.5 {Tags:["attr"],NoAI:1b,NoGravity:1b,' + 'attributes:[{id:"minecraft:max_health",base:40.0d},{id:"minecraft:movement_speed",base:0.5d}]}'), + cmd('/data get entity @e[tag=attr,limit=1] Health'), + cmd('/attribute @e[tag=attr,limit=1] minecraft:max_health base get'), + cmd('/attribute @e[tag=attr,limit=1] minecraft:movement_speed base get'), + cmd('/kill @e[tag=attr]'), + cmd('/summon minecraft:zombie 4.5 210 8.5 {Tags:["attr"],NoAI:1b,NoGravity:1b,Health:7.0f,' + 'attributes:[{id:"minecraft:attack_damage",base:9.0d}]}'), + cmd('/data get entity @e[tag=attr,limit=1] Health'), + cmd('/attribute @e[tag=attr,limit=1] minecraft:attack_damage base get'), + cmd('/kill @e[tag=attr]'), *setup('/kill @e[type=minecraft:armor_stand]'), {'op': 'tick', 'n': 25}, cmd('/execute if entity @e[tag=h]'), From 8c54e1858741b47dcca050cd5dc35b6d32f2d23c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:40:41 +0100 Subject: [PATCH 126/238] /ride dismount: the boats' BOARDING_COOLDOWN, not a second 60 Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/boat_passengers.rs | 2 +- crates/horizon-client/src/game/commands/world_cmds.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/game/boat_passengers.rs b/crates/horizon-client/src/game/boat_passengers.rs index 96686f69b..7055cccfd 100644 --- a/crates/horizon-client/src/game/boat_passengers.rs +++ b/crates/horizon-client/src/game/boat_passengers.rs @@ -13,7 +13,7 @@ use horizon_client::{ use horizon_core::geometry::{Aabb, Vec3}; /// `Entity.boardingCooldown` once a mob leaves its boat. -const BOARDING_COOLDOWN: i32 = 60; +pub(super) const BOARDING_COOLDOWN: i32 = 60; /// `Player.DEFAULT_VEHICLE_ATTACHMENT`: how far below the seat a player's /// feet are. const PLAYER_RIDING_OFFSET: f64 = 0.6; diff --git a/crates/horizon-client/src/game/commands/world_cmds.rs b/crates/horizon-client/src/game/commands/world_cmds.rs index c8bd220be..30f019997 100644 --- a/crates/horizon-client/src/game/commands/world_cmds.rs +++ b/crates/horizon-client/src/game/commands/world_cmds.rs @@ -1191,7 +1191,7 @@ impl Game<'_> { }; let m = &mut self.mobs[i]; m.extra.passenger = false; - m.extra.boarding_cooldown = 60; + m.extra.boarding_cooldown = super::super::boat_passengers::BOARDING_COOLDOWN; m.position = at; m.previous = at; } From b873f30c29b66a551b6f3744be5efa7468795c9c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:41:04 +0100 Subject: [PATCH 127/238] wears_freeze_immune above sync_player_body's doc comment, which it had taken over Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 4e4865c7a..6dd5f4678 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1837,13 +1837,6 @@ impl Game<'_> { .is_some_and(|i| i.glider && (i.max_damage == 0 || s.damage + 1 < i.max_damage)) }) } - /// What the window decides for its player's body every tick, in frame and - /// on a server alike: the look (Player.travel steers sprint-swimming by - /// the pitch), Auto Jump (LocalPlayer.sendPosition), the slowdown of an - /// item in use (LocalPlayer.modifyInput), the helmet's Respiration - /// (decreaseAirSupply), the wings, leather boots on powder snow - /// (PowderSnowBlock.canEntityWalkOnPowderSnow), freeze-immune wear - /// (LivingEntity.canFreeze) and the dimension's FAST_LAVA. /// `LivingEntity.canFreeze`'s armor half: any worn piece in /// `freeze_immune_wearables`. pub(crate) fn wears_freeze_immune(&self) -> bool { @@ -1857,6 +1850,13 @@ impl Game<'_> { .flatten() .any(|s| immune.is_some_and(|t| t.contains(&*s.item))) } + /// What the window decides for its player's body every tick, in frame and + /// on a server alike: the look (Player.travel steers sprint-swimming by + /// the pitch), Auto Jump (LocalPlayer.sendPosition), the slowdown of an + /// item in use (LocalPlayer.modifyInput), the helmet's Respiration + /// (decreaseAirSupply), the wings, leather boots on powder snow + /// (PowderSnowBlock.canEntityWalkOnPowderSnow), freeze-immune wear + /// (LivingEntity.canFreeze) and the dimension's FAST_LAVA. pub(crate) fn sync_player_body(&mut self) { let glider = self.glider_worn(); let item_use_speed = self From 644384763338a5912405063d25ef52e60995d322 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:42:00 +0100 Subject: [PATCH 128/238] Launcher: add the panorama, Unifont, languages and sounds prepared resources lack Versions prepared while the asset store could not be read (Prism Launcher installs since 0.2.48) are silent with a grey title screen. Like the built-in packs (#1539), the launcher now copies each of these from the player's own asset store into prepared resources that lack it, at start, after an install and before Play, without preparing again. Co-Authored-By: Claude Opus 5.5 --- launcher/src/backend.rs | 241 +++++++++++++++++++++++++++++----------- launcher/src/main.rs | 7 +- 2 files changed, 178 insertions(+), 70 deletions(-) diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index 9d1eeb111..a7485468a 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -552,7 +552,7 @@ pub fn install(release: &Release, progress: impl Fn(f32)) -> Result<(), String> fs::write(target.join(COMPLETE), "").map_err(|e| e.to_string())?; let _ = fs::remove_dir_all(&partial); reuse_prepared(&root, tag); - complete_packs(tag); + complete_assets(tag); progress(1.0); Ok(()) } @@ -774,13 +774,13 @@ fn reuse_prepared(versions: &Path, tag: &str) -> Option { /// `reuse_prepared` for every installed version that is not prepared, oldest first, so a /// version prepared this way can serve the next. Launchers before 0.1.4 installed new versions /// without it; this catches those up. Returns each (version, where its resources came from). -/// Prepared versions also get the run-time reports (`complete_reports`) and built-in packs -/// (`complete_packs`) they lack. +/// Prepared versions also get the run-time reports (`complete_reports`) and the built-in packs, +/// panorama and sounds (`complete_assets`) they lack. pub fn reuse_prepared_everywhere() -> Vec<(String, String)> { let versions = versions_dir(); let reused = reuse_prepared_in(&versions); for tag in installed_in(&versions) { - complete_packs(&tag); + complete_assets(&tag); } reused } @@ -856,19 +856,77 @@ fn complete_reports(versions: &Path, tag: &str) -> bool { complete } -/// Adds vanilla's built-in Programmer Art and High Contrast packs to a prepared version that -/// lacks them, as `horizon-prepare packs` does: preparers before #1538 left them out, and a new -/// version reuses the resources of the one before (`reuse_prepared`). They come from the player's -/// own Minecraft asset store; without them there, one line in the log, once per launcher run, and -/// the version plays as before. Nothing is prepared again. -pub fn complete_packs(tag: &str) { - complete_packs_in(&versions_dir(), tag, minecraft_folders, &|line| { +/// What a prepared version may lack that the launcher adds without preparing again, each copied +/// from the player's own Minecraft asset store as `horizon-prepare ` does: what the +/// player misses without it, whether the resources lack it, and the copy. +type Completion = ( + &'static str, + fn(&Path) -> bool, + fn(&Path, Option<&Path>, horizon_prepare::Log) -> Result, +); + +const COMPLETIONS: [Completion; 5] = [ + // Preparers before #1538 left the built-in packs out. + ( + "Programmer Art and High Contrast", + lacks_packs, + horizon_prepare::packs, + ), + // A preparation that could not read the asset store keeps the client jar's 1x1 grey + // placeholders, its English alone, and no sounds (#1867: Prism Launcher installs prepared + // since 0.2.48). + ( + "The title panorama", + lacks_panorama, + horizon_prepare::panorama, + ), + ("The Unifont glyphs", lacks_unifont, horizon_prepare::fonts), + ("The languages", lacks_languages, horizon_prepare::languages), + ( + "The sounds", + |resources| !sounds_in(resources), + horizon_prepare::sounds, + ), +]; + +fn lacks_unifont(resources: &Path) -> bool { + !resources + .join("assets/minecraft/font/unifont.zip") + .is_file() +} + +/// Whether `en_us`, the one language the client jar has, is the only one. +fn lacks_languages(resources: &Path) -> bool { + fs::read_dir(resources.join("assets/minecraft/lang")).map_or(true, |dir| dir.count() <= 1) +} + +fn lacks_packs(resources: &Path) -> bool { + horizon_prepare::BUILT_IN_PACKS + .iter() + .any(|pack| !resources.join("assets").join(pack).is_file()) +} + +/// Whether the first panorama face is missing or the client jar's 1x1 placeholder (a PNG's +/// width is the big-endian word at byte 16, in the IHDR chunk). +fn lacks_panorama(resources: &Path) -> bool { + let face = resources.join("assets/minecraft/textures/gui/title/background/panorama_0.png"); + fs::read(face).map_or(true, |png| { + png.get(16..20).is_none_or(|width| width == [0, 0, 0, 1]) + }) +} + +/// Adds what a prepared version lacks of `COMPLETIONS`: a new version reuses the resources of the +/// one before (`reuse_prepared`), which may come from a preparer that left them out. Without them +/// in any Minecraft folder, one line each in the log, once per launcher run, and the version plays +/// as before. Nothing is prepared again. +pub fn complete_assets(tag: &str) { + complete_assets_in(&versions_dir(), tag, minecraft_folders, &|line| { eprintln!("{line}") }); } -/// `folders`: where to look, asked only when the version lacks a pack. -fn complete_packs_in( +/// `folders`: where to look, asked only when the version lacks something. +fn complete_assets_in( versions: &Path, tag: &str, folders: impl Fn() -> Vec, @@ -879,37 +937,41 @@ fn complete_packs_in( static TRIED: Mutex> = Mutex::new(Vec::new()); let root = versions.join(tag); let resources = resources_at(&root); - let lacking = || { - resources.join(RESOURCES_MANIFEST).exists() - && horizon_prepare::BUILT_IN_PACKS - .iter() - .any(|pack| !resources.join("assets").join(pack).is_file()) + let lacking = || -> Vec<&Completion> { + if !resources.join(RESOURCES_MANIFEST).exists() { + return Vec::new(); + } + COMPLETIONS + .iter() + .filter(|(_, lacks, _)| lacks(&resources)) + .collect() }; - if !lacking() { + if lacking().is_empty() { return; } let mut tried = TRIED.lock().unwrap_or_else(|e| e.into_inner()); - if tried.contains(&root) || !lacking() { + let lacking = lacking(); + if tried.contains(&root) || lacking.is_empty() { return; } tried.push(root.clone()); - let mut first_error = None; - for folder in folders() { - let copied = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { - horizon_prepare::packs(&root, Some(&folder), &|line| log(line.to_string())) - })) - .unwrap_or_else(|_| Err("the copy stopped unexpectedly".into())); - match copied { - Ok(_) => return, - Err(e) => { - first_error.get_or_insert(e); - } + let folders = folders(); + for (what, _, copy) in lacking { + let mut first_error = None; + let added = folders.iter().any(|folder| { + let copied = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + copy(&root, Some(folder), &|line| log(line.to_string())) + })) + .unwrap_or_else(|_| Err("the copy stopped unexpectedly".into())); + copied.map_err(|e| first_error.get_or_insert(e)).is_ok() + }); + if !added { + log(format!( + "{tag}: {what} not added: {}", + first_error.unwrap_or_else(|| "no Minecraft folder found".into()) + )); } } - log(format!( - "{tag}: Programmer Art and High Contrast not added: {}", - first_error.unwrap_or_else(|| "no Minecraft folder found".into()) - )); } /// The Minecraft folders whose asset store can hold what a prepared version lacks: the one @@ -1635,7 +1697,7 @@ pub fn need_prepared(tag: &str) -> Result<(), String> { /// The client's command, once its resources are prepared. pub fn client(tag: &str) -> Result { need_prepared(tag)?; - complete_packs(tag); + complete_assets(tag); crate::browser::horizon::before_play(&game_home()); let mut cmd = client_command(tag); // A sign-in that can't be renewed (no network, expired) still lets the game start, offline. @@ -3069,7 +3131,7 @@ mod tests { } #[test] - fn prepared_versions_get_the_built_in_packs_without_preparing_again() { + fn prepared_versions_get_what_the_asset_store_adds_without_preparing_again() { let dir = std::env::temp_dir().join(format!( "horizon-launcher-packs-test-{}", std::process::id() @@ -3080,17 +3142,42 @@ mod tests { fs::create_dir_all(path.parent().unwrap()).unwrap(); fs::write(path, data).unwrap(); }; - // An official launcher folder whose asset store holds the two packs. - let packs = horizon_prepare::BUILT_IN_PACKS.map(|name| (name, name.as_bytes())); + // An official launcher folder whose asset store holds the two packs, a panorama (fake + // 2x2 PNG headers), Unifont, a language and the sound list. + let background = "minecraft/textures/gui/title/background"; + let png = |n: u8| { + let mut data = b"\x89PNG\r\n\x1a\n\0\0\0\x0dIHDR\0\0\0\x02\0\0\0\x02".to_vec(); + data.push(n); + data + }; + let mut assets: Vec<(String, Vec)> = horizon_prepare::BUILT_IN_PACKS + .iter() + .map(|name| (name.to_string(), name.as_bytes().to_vec())) + .collect(); + for face in ["0", "1", "2", "3", "4", "5", "overlay"] { + assets.push(( + format!("{background}/panorama_{face}.png"), + png(face.as_bytes()[0]), + )); + } + for name in [ + "minecraft/font/include/unifont.json", + "minecraft/font/unifont.zip", + "pack.mcmeta", + "minecraft/lang/fr_fr.json", + "minecraft/sounds.json", + ] { + assets.push((name.into(), format!("{{\"{name}\": 1}}").into_bytes())); + } let mut index = serde_json::Map::new(); - for (name, data) in packs { + for (name, data) in &assets { let hash = sources::sha1_hex(data); write( store.join("assets/objects").join(&hash[..2]).join(&hash), data, ); index.insert( - name.into(), + name.clone(), serde_json::json!({"hash": hash, "size": data.len()}), ); } @@ -3105,6 +3192,8 @@ mod tests { .to_string(); write(store.join("versions/9.9/9.9.json"), manifest.as_bytes()); fs::create_dir_all(&empty).unwrap(); + // The client jar's 1x1 placeholder. + let placeholder = b"\x89PNG\r\n\x1a\n\0\0\0\x0dIHDR\0\0\0\x01\0\0\0\x01"; let release = |tag: &str, prepared: bool| { let root = versions.join(tag); write(root.join(COMPLETE), b""); @@ -3112,27 +3201,27 @@ mod tests { "client_sha1": "c", "server_sha1": "s", "manifest_sha1": sources::sha1_hex(manifest.as_bytes())}}); write(root.join("research-lock.json"), lock.to_string().as_bytes()); if prepared { - write(resources_at(&root).join(RESOURCES_MANIFEST), b"{}"); + let resources = resources_at(&root); + write(resources.join(RESOURCES_MANIFEST), b"{}"); + write(resources.join("assets/minecraft/lang/en_us.json"), b"{}"); write( - resources_at(&root).join("assets/minecraft/lang/en_us.json"), - b"{}", + resources + .join("assets") + .join(background) + .join("panorama_0.png"), + placeholder, ); } }; let lines = std::cell::RefCell::new(Vec::new()); let log = |line: String| lines.borrow_mut().push(line); - let pack = |tag: &str, i: usize| { - fs::read( - resources_at(&versions.join(tag)) - .join("assets") - .join(packs[i].0), - ) - .ok() + let asset = |tag: &str, name: &str| { + fs::read(resources_at(&versions.join(tag)).join("assets").join(name)).ok() }; - // Resources prepared before the packs were: a folder without an asset store is skipped. + // Resources prepared without any of them: a folder without an asset store is skipped. release("v0.2.1", true); - complete_packs_in( + complete_assets_in( &versions, "v0.2.1", || vec![empty.clone(), store.clone()], @@ -3140,37 +3229,55 @@ mod tests { ); assert_eq!( *lines.borrow(), - ["Built-in pack assets ready: 2 files, 0.0 MB"] + [ + "Built-in pack assets ready: 2 files, 0.0 MB", + "Panorama assets ready: 7 files, 0.0 MB", + "Font assets ready: 2 files, 0.0 MB", + "Language assets ready: 2 files, 0.0 MB", + "Sound assets ready: 1 files, 0.0 MB", + ] ); - for (i, (_, data)) in packs.iter().enumerate() { - assert_eq!(pack("v0.2.1", i).as_deref(), Some(*data)); + for (name, data) in &assets { + assert_eq!(asset("v0.2.1", name).as_ref(), Some(data), "{name}"); } // Nothing else is prepared again. let resources = resources_at(&versions.join("v0.2.1")); assert_eq!(fs::read(resources.join(RESOURCES_MANIFEST)).unwrap(), b"{}"); - assert!(!resources.join("assets/minecraft/sounds.json").exists()); - // No asset store anywhere: one line, the version stays prepared, and no second try. + // No asset store anywhere: one line each, the version stays prepared, and no second try. lines.borrow_mut().clear(); release("v0.2.2", true); - complete_packs_in(&versions, "v0.2.2", || vec![empty.clone()], &log); - complete_packs_in(&versions, "v0.2.2", || vec![empty.clone()], &log); - assert_eq!(lines.borrow().len(), 1); - assert!( - lines.borrow()[0].starts_with("v0.2.2: Programmer Art and High Contrast not added: "), - "{lines:?}" + complete_assets_in(&versions, "v0.2.2", || vec![empty.clone()], &log); + complete_assets_in(&versions, "v0.2.2", || vec![empty.clone()], &log); + let what = lines + .borrow() + .iter() + .map(|line| line.split(" not added: ").next().unwrap().to_string()) + .collect::>(); + assert_eq!( + what, + [ + "v0.2.2: Programmer Art and High Contrast", + "v0.2.2: The title panorama", + "v0.2.2: The Unifont glyphs", + "v0.2.2: The languages", + "v0.2.2: The sounds", + ] ); assert!(resources_at(&versions.join("v0.2.2")) .join(RESOURCES_MANIFEST) .exists()); - assert_eq!(pack("v0.2.2", 0), None); + assert_eq!( + asset("v0.2.2", &format!("{background}/panorama_0.png")).as_deref(), + Some(&placeholder[..]) + ); // Not prepared, or complete: nothing to do, without even looking for a Minecraft folder. lines.borrow_mut().clear(); release("v0.2.3", false); let unasked = || -> Vec { unreachable!("no folder is needed") }; - complete_packs_in(&versions, "v0.2.3", unasked, &log); - complete_packs_in(&versions, "v0.2.1", unasked, &log); + complete_assets_in(&versions, "v0.2.3", unasked, &log); + complete_assets_in(&versions, "v0.2.1", unasked, &log); assert!(lines.borrow().is_empty()); assert!(!resources_at(&versions.join("v0.2.3")).exists()); let _ = fs::remove_dir_all(&dir); diff --git a/launcher/src/main.rs b/launcher/src/main.rs index afc8dde45..9105f71a2 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -831,13 +831,14 @@ impl Launcher { // A little earlier than the spawn: file times can be coarse. self.client_started = Some(std::time::SystemTime::now() - std::time::Duration::from_secs(2)); - // The built-in packs the resources lack and the session (a network round trip when the - // token needs renewing) come first; `drain` starts the game when the session arrives. + // What the resources lack (built-in packs, panorama, sounds) and the session (a network + // round trip when the token needs renewing) come first; `drain` starts the game when the + // session arrives. self.starting_client = Some(tag.clone()); self.play_pressed = Some(Instant::now()); let tx = self.tx.clone(); std::thread::spawn(move || { - backend::complete_packs(&tag); + backend::complete_assets(&tag); let session = (|| { let mut accounts = Accounts::load(); let Some((a, secrets)) = account::session_for_launch( From 9fe77c7a56042ceb82ee634f0abc1610ede4f767 Mon Sep 17 00:00:00 2001 From: Alexander Krylov Date: Sun, 25 Oct 2026 19:49:02 +0300 Subject: [PATCH 129/238] Creative: a stack keeps its components when it crosses the wire `Game::to_network` wrote damage alone into SetCreativeModeSlot, and `item_from_network` read back damage and enchantments, so anything else a creative stack carried was gone once the server echoed the inventory: a named sword lost its name, trimmed armour its trim, an enchanted book its stored enchantments and its repair cost. `item_sync` is the pair for the components Horizon models: `encode` writes them with the synced registries' ids (an unknown name is left out rather than sent as id 0, which is a different entry) and `apply` reads them back into the same `StackData` fields `item_data::decode_components` fills. Potions are not carried yet: `minecraft:potion_contents` has no id or codec in horizon-protocol, so a creative potion still arrives as an uncraftable one (#1832). Nor are containers, charged projectiles, written books and a map id, which the packet models but this patch does not write. Refs #1832 Co-Authored-By: Hermes Agent (Nous Research) --- crates/horizon-client/src/game/lan_actions.rs | 19 +- crates/horizon-client/src/game/remote.rs | 12 +- crates/horizon-client/src/item_data.rs | 2 +- crates/horizon-client/src/item_sync.rs | 170 ++++++++++++++++++ crates/horizon-client/src/lan/hub.rs | 8 + crates/horizon-client/src/lib.rs | 1 + crates/horizon-protocol/src/registry.rs | 6 + 7 files changed, 204 insertions(+), 14 deletions(-) create mode 100644 crates/horizon-client/src/item_sync.rs diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 2c7019c73..1b4989055 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -1216,21 +1216,22 @@ impl Game<'_> { host: &LanHost, stack: &Option, ) -> Option { - use horizon_protocol::item::{Component, component}; let s = stack.as_ref()?; let name = host .hub .item_name(s.item) .unwrap_or_else(|| "minecraft:air".into()); let mut out = ItemStack::new(&name, s.count.max(1) as u32); - out.damage = s.damage().max(0) as u32; - if let Some(Component::Enchantments(list)) = s.component(component::ENCHANTMENTS) { - for (id, level) in list { - if let Some(e) = host.hub.enchantment_name(*id) { - out.enchant(&e, *level as u32); - } - } - } + // The same components the encoder wrote, so a creative slot keeps what + // the client held instead of just damage and enchantments. + horizon_client::item_sync::apply( + s, + &mut out, + |id| host.hub.enchantment_name(id).map(|n| n.into()), + |id| host.hub.trim_name("minecraft:trim_material", id), + |id| host.hub.trim_name("minecraft:trim_pattern", id), + ); + out.tidy(); Some(out) } diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index a3ab8d281..bbd688eef 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -761,10 +761,14 @@ impl Game<'_> { fn to_network(&self, s: &Option) -> Option { let s = s.as_ref()?; let mut stack = Stack::new(self.item_id(&s.item), s.count as i32); - if s.damage > 0 { - stack.added.push(( - horizon_protocol::item::component::DAMAGE, - horizon_protocol::item::Component::Int(s.damage as i32), + // The components the client models, with the synced registries' ids: + // without them the server's echo hands back a bare item. + if let Some(r) = self.remote.as_ref() { + stack.added.extend(horizon_client::item_sync::encode( + s, + |name| r.registries.id("minecraft:enchantment", name), + |name| r.registries.id("minecraft:trim_material", name), + |name| r.registries.id("minecraft:trim_pattern", name), )); } Some(stack) diff --git a/crates/horizon-client/src/item_data.rs b/crates/horizon-client/src/item_data.rs index c07e2f352..00fd0f074 100644 --- a/crates/horizon-client/src/item_data.rs +++ b/crates/horizon-client/src/item_data.rs @@ -291,7 +291,7 @@ fn filterable_text(t: &Tag) -> Option { None => read_text(t), } } -fn read_text(t: &Tag) -> Option { +pub(crate) fn read_text(t: &Tag) -> Option { match t { Tag::String(s) => Some(s.to_string_lossy()), // A component's root `text` may be empty with the visible words in diff --git a/crates/horizon-client/src/item_sync.rs b/crates/horizon-client/src/item_sync.rs new file mode 100644 index 000000000..4e48498ac --- /dev/null +++ b/crates/horizon-client/src/item_sync.rs @@ -0,0 +1,170 @@ +//! The data components a stack keeps when it crosses the wire, and how the +//! other side reads them back. A creative slot's packet carries a component +//! patch, and before this the encoder wrote damage alone, so everything else +//! (enchantments, a custom name, trim, repair cost) was gone once the server +//! echoed the inventory back. +use crate::item_data::{Levels, StackData}; +use crate::items::ItemStack; +use horizon_protocol::item::{Component, Stack, component}; +use horizon_world::nbt::Tag; +use std::sync::Arc; + +/// `Slot.STREAM_CODEC`'s patch for the components Horizon models, with the +/// registries' ids for enchantments and trim. A name the registry does not +/// know is left out: id 0 is a different entry, not a missing one. +pub fn encode( + stack: &ItemStack, + enchantment: impl Fn(&str) -> Option, + trim_material: impl Fn(&str) -> Option, + trim_pattern: impl Fn(&str) -> Option, +) -> Vec<(i32, Component)> { + let mut out = Vec::new(); + if stack.damage > 0 { + out.push((component::DAMAGE, Component::Int(stack.damage as i32))); + } + let Some(d) = stack.data() else { + return out; + }; + let levels = |levels: &Levels| -> Option { + let list: Vec<(i32, i32)> = levels + .iter() + .filter_map(|(name, level)| Some((enchantment(name)?, *level as i32))) + .collect(); + (!list.is_empty()).then_some(Component::Enchantments(list)) + }; + if let Some(c) = levels(&d.enchantments) { + out.push((component::ENCHANTMENTS, c)); + } + if let Some(c) = levels(&d.stored_enchantments) { + out.push((component::STORED_ENCHANTMENTS, c)); + } + if d.repair_cost > 0 { + out.push((component::REPAIR_COST, Component::Int(d.repair_cost as i32))); + } + if let Some(name) = &d.custom_name { + // A plain string is a valid text component, and `decode_components` + // reads it back as one. + out.push(( + component::CUSTOM_NAME, + Component::Text(Tag::String(name.as_str().into())), + )); + } + if let Some((material, pattern)) = &d.trim + && let (Some(material), Some(pattern)) = (trim_material(material), trim_pattern(pattern)) + { + out.push((component::TRIM, Component::Trim { material, pattern })); + } + out +} + +/// The same patch read back into `out`, the fields `item_data::decode_components` +/// fills from the equivalent `components` compound. +pub fn apply( + stack: &Stack, + out: &mut ItemStack, + enchantment: impl Fn(i32) -> Option>, + trim_material: impl Fn(i32) -> Option>, + trim_pattern: impl Fn(i32) -> Option>, +) { + out.damage = stack.damage().max(0) as u32; + let read_levels = |kind: i32, into: &mut Levels| { + if let Some(Component::Enchantments(list)) = stack.component(kind) { + for (id, level) in list { + if let Some(name) = enchantment(*id) { + into.push((name, (*level).max(0) as u32)); + } + } + } + }; + let data: &mut StackData = out.data_mut(); + read_levels(component::ENCHANTMENTS, &mut data.enchantments); + read_levels( + component::STORED_ENCHANTMENTS, + &mut data.stored_enchantments, + ); + if let Some(Component::Int(v)) = stack.component(component::REPAIR_COST) { + data.repair_cost = (*v).max(0) as u32; + } + if let Some(Component::Text(t)) = stack.component(component::CUSTOM_NAME) { + data.custom_name = crate::item_data::read_text(t); + } + if let Some(Component::Trim { material, pattern }) = stack.component(component::TRIM) + && let (Some(material), Some(pattern)) = (trim_material(*material), trim_pattern(*pattern)) + { + data.trim = Some((material, pattern)); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn named(name: &str) -> Option { + Some(match name { + "minecraft:sharpness" => 3, + "minecraft:gold" => 7, + "minecraft:sentry" => 2, + _ => return None, + }) + } + fn name_of(id: i32) -> Option> { + Some(Arc::from(match id { + 3 => "minecraft:sharpness", + 7 => "minecraft:gold", + 2 => "minecraft:sentry", + _ => return None, + })) + } + + fn stack_with_data() -> ItemStack { + let mut stack = ItemStack::new("minecraft:diamond_sword", 1); + stack.damage = 5; + let d = stack.data_mut(); + d.enchantments.push((Arc::from("minecraft:sharpness"), 4)); + d.stored_enchantments + .push((Arc::from("minecraft:sharpness"), 2)); + d.repair_cost = 9; + d.custom_name = Some("Keeper".to_owned()); + d.trim = Some((Arc::from("minecraft:gold"), Arc::from("minecraft:sentry"))); + stack + } + + /// Every modelled component survives the packet, so the server's echo does + /// not hand back a bare item. + #[test] + fn a_stack_keeps_its_components_across_the_wire() { + let stack = stack_with_data(); + let mut packet = Stack::new(1, 1); + packet.added = encode(&stack, named, named, named); + + let mut back = ItemStack::new("minecraft:diamond_sword", 1); + apply(&packet, &mut back, name_of, name_of, name_of); + back.tidy(); + + assert_eq!(back.damage, 5); + let d = back.data().expect("components"); + assert_eq!(d.enchantments, vec![(Arc::from("minecraft:sharpness"), 4)]); + assert_eq!( + d.stored_enchantments, + vec![(Arc::from("minecraft:sharpness"), 2)] + ); + assert_eq!(d.repair_cost, 9); + assert_eq!(d.custom_name.as_deref(), Some("Keeper")); + assert_eq!( + d.trim, + Some((Arc::from("minecraft:gold"), Arc::from("minecraft:sentry"))) + ); + } + + /// A name the registry does not know is a missing id, not id 0, which would + /// name the registry's first entry instead. + #[test] + fn an_unknown_enchantment_is_left_out_rather_than_sent_as_zero() { + let mut stack = ItemStack::new("minecraft:book", 1); + stack + .data_mut() + .enchantments + .push((Arc::from("mod:unknown"), 1)); + assert!(encode(&stack, named, named, named).is_empty()); + } +} diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index dd0a32f22..2667b1fed 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -2293,6 +2293,14 @@ impl Hub { .name("minecraft:enchantment", id) .map(str::to_owned) } + /// An entry of a synced registry by id, for the components a packet + /// carries by id (`minecraft:trim_material`, `minecraft:trim_pattern`). + pub fn trim_name(&self, registry: &str, id: i32) -> Option> { + self.sync + .dynamic + .name(registry, id) + .map(std::sync::Arc::from) + } /// ServerLevel.removePlayerImmediately, then addRespawnedPlayer: the other players /// lose the dead player's entity (ServerEntity.removePairing) and are sent the diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index ae95e1bdb..ad05153eb 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -96,6 +96,7 @@ pub mod inv_sort; pub mod inventory_effects; pub mod item_data; pub mod item_render; +pub mod item_sync; pub mod item_tooltip; pub mod items; pub mod java_text; diff --git a/crates/horizon-protocol/src/registry.rs b/crates/horizon-protocol/src/registry.rs index f97401670..400c21a37 100644 --- a/crates/horizon-protocol/src/registry.rs +++ b/crates/horizon-protocol/src/registry.rs @@ -102,6 +102,12 @@ impl Dynamic { .get(i) .map(|(n, _)| n.as_str()) } + /// The id of an entry by name, the reverse of `name`. A registry has one + /// entry per id, so the first match is the only one. + pub fn id(&self, registry: &str, name: &str) -> Option { + let list = self.registries.get(registry)?; + list.iter().position(|(n, _)| n == name).map(|i| i as i32) + } pub fn data(&self, registry: &str, id: i32) -> Option<&Tag> { let i = usize::try_from(id).ok()?; self.registries.get(registry)?.get(i)?.1.as_ref() From 5ee1130719904c4101a0b13748fc53b1adaab1ee Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:53:13 +0100 Subject: [PATCH 130/238] horizon-dualsense: iOS comments name only what exists The repo has no horizon-gamecontroller crate, and on iOS the Link never opens, so no rumble covers vibration there. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-dualsense/Cargo.toml | 5 ++--- crates/horizon-dualsense/src/device.rs | 3 +-- crates/horizon-dualsense/src/haptics_stub.rs | 5 ++--- crates/horizon-dualsense/src/lib.rs | 3 +-- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/crates/horizon-dualsense/Cargo.toml b/crates/horizon-dualsense/Cargo.toml index 1a41e5703..feb25026b 100644 --- a/crates/horizon-dualsense/Cargo.toml +++ b/crates/horizon-dualsense/Cargo.toml @@ -10,9 +10,8 @@ description = "The DualSense's output over HID: rumble, adaptive triggers, light log = "0.4" # hidapi has no iOS backend and its build script stops there, so the output's two -# dependencies go only to the targets that have one. On iOS the crate builds empty: pads -# are read through GameController (horizon-gamecontroller), which only reads, and the -# DualSense's own output waits for a desktop. +# dependencies go only to the targets that have one. On iOS the crate builds without +# output: the Link never opens. [target.'cfg(not(target_os = "ios"))'.dependencies] # MIT; bundles the C hidapi under its BSD licence option. hidraw on Linux (libudev, as gilrs), # IOKit on macOS (opened shared, so gilrs keeps reading the pad), the HID API on Windows. diff --git a/crates/horizon-dualsense/src/device.rs b/crates/horizon-dualsense/src/device.rs index 0b11ac415..f8e544ea6 100644 --- a/crates/horizon-dualsense/src/device.rs +++ b/crates/horizon-dualsense/src/device.rs @@ -165,8 +165,7 @@ impl Link { } } - /// No output on iOS: hidapi has no backend there, and iOS reads pads through - /// GameController (horizon-gamecontroller), which only reads. The Link never opens. + /// No output on iOS: hidapi has no backend there. The Link never opens. #[cfg(target_os = "ios")] pub fn start() -> Link { Link { diff --git a/crates/horizon-dualsense/src/haptics_stub.rs b/crates/horizon-dualsense/src/haptics_stub.rs index 7afc944ca..dc4e61667 100644 --- a/crates/horizon-dualsense/src/haptics_stub.rs +++ b/crates/horizon-dualsense/src/haptics_stub.rs @@ -1,6 +1,5 @@ -//! No HD haptics on iOS: the trick plays vibrations through the pad's own four-channel audio -//! device over USB, and a phone has neither that device nor the audio stack the real module -//! needs. The motors' rumble through [`crate::Link`] covers vibration instead. +//! No HD haptics on iOS: they play through the pad's own four-channel audio device over USB, +//! which needs cpal, and cpal is left out there with hidapi (see Cargo.toml). /// The pad's audio output, playing the queued samples (left, right actuator). pub struct Haptics; diff --git a/crates/horizon-dualsense/src/lib.rs b/crates/horizon-dualsense/src/lib.rs index 80831ee00..4ae55de9a 100644 --- a/crates/horizon-dualsense/src/lib.rs +++ b/crates/horizon-dualsense/src/lib.rs @@ -1,8 +1,7 @@ //! The DualSense's output report, built by hand (docs/controllers.md "DualSense output"): the //! compatible-vibration motors, the adaptive triggers, the lightbar and the player LEDs. Gilrs //! keeps reading the pad; this crate only writes to it, over hidapi ([`device`]). -//! On iOS the crate builds without hidapi, which has no backend there: [`Link`] never opens, -//! and the pad is read through GameController, which only reads. +//! On iOS the crate builds without hidapi, which has no backend there: [`Link`] never opens. //! //! The byte layout is the one documented by the Linux kernel's `hid-playstation.c` and SDL's //! `SDL_hidapi_ps5.c`: a 47-byte block shared by both transports, sent as report 0x02 over USB From f2832f83ef4e811e744312ac01117114a098f746 Mon Sep 17 00:00:00 2001 From: socksprox Date: Sun, 25 Oct 2026 17:53:13 +0100 Subject: [PATCH 131/238] check: cover the ui open/close-screen outputs Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- crates/cargo-horizon-mod/src/check.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/cargo-horizon-mod/src/check.rs b/crates/cargo-horizon-mod/src/check.rs index 0294bec98..5d4150ab9 100644 --- a/crates/cargo-horizon-mod/src/check.rs +++ b/crates/cargo-horizon-mod/src/check.rs @@ -215,6 +215,8 @@ fn play(path: &Path, report: &mut Report) -> Result<(), String> { format!("{}: {text}", if error { "error" } else { "feedback" }) } Output::Stopped { reason, .. } => return Err(format!("it stopped: {reason}")), + Output::OpenScreen { .. } => "opened a screen".to_string(), + Output::CloseScreen { .. } => "closed its screen".to_string(), }); } match failed(&mods) { From a276f81abacc7d7503a3098c986b1c370f294912 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:55:23 +0100 Subject: [PATCH 132/238] Shader packs, clean room 39: the run doc comment back on run Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx/pack_post.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index f17765a0f..fdc4563ef 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -2081,9 +2081,6 @@ impl PackPost { } } - /// Runs the pack over the world drawn into `world` (colour) and `world_depth`, then draws - /// the result onto `screen`. - #[allow(clippy::too_many_arguments)] /// The shadow pass (2.1, passes 2 and 3): the shadow buffers cleared, then the `shadowcomp` /// programs, each drawing into the "alt" side of what it writes and flipping it after. fn run_shadow( @@ -2133,6 +2130,9 @@ impl PackPost { self.shadow_comp = comp; } + /// Runs the pack over the world drawn into `world` (colour) and `world_depth`, then draws + /// the result onto `screen`. + #[allow(clippy::too_many_arguments)] pub fn run( &mut self, device: &wgpu::Device, From 2ec2964962790c292180cd228890329285bf5ea9 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 18:09:13 +0100 Subject: [PATCH 133/238] Roadmap: the items Ludovic decided on (55 added, 4 updated, 3 areas) Adds the ideas he said yes to, as roadmap items with plain and dev text, and extends ai-companion, new-versions, benchmarks and intel-mac. New areas: platforms, ai, community. Co-Authored-By: Claude Sonnet 5.5 --- ROADMAP.md | 632 +++++++++++++++++++++++++++++++- roadmap/roadmap.toml | 739 +++++++++++++++++++++++++++++++++++++- website/data/roadmap.json | 663 +++++++++++++++++++++++++++++++++- 3 files changed, 1996 insertions(+), 38 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index b433fad3a..7e4cc9937 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -16,7 +16,7 @@ Horizon is Minecraft: Java Edition 26.3 rewritten from scratch in Rust, so it ru - **Single player as a server: the last differences**: Single player now runs a small server in the background, as vanilla does. We are fixing what still behaves differently there; every fix also works for LAN and server players. - **Horizon's mod registry**: One place for mods, modpacks and resource packs made for Horizon: browse them on the website and install them from the launcher in one click. -**Next**: Microsoft sign-in (coming soon); CurseForge in the mod browser (coming soon); Fresh benchmarks against vanilla; Pistons push players and mobs; Horizon on Intel Macs. +**Next**: Microsoft sign-in (coming soon); CurseForge in the mod browser (coming soon); Fresh benchmarks against vanilla; Pistons push players and mobs; Horizon on Intel Macs; Faster world creation and entry on small CPUs; Servers that hold 20 TPS with 50 to 100 players; Low-end computers: Steam Deck, old Macs, 8 GB; Native upscaling; A batch of targeted optimisations; A Convert to Rust button for what isn't rewritten yet; Mods with code: machines, magic, mobs; Data packs in the mod browser; Prepare a version without Java; Server console, bans and a full admin API; Hosting polish: Windows service, safe updates; Guided hosting with Tailscale and an invite button; An official Docker image for servers; One single-player path: remove the in-frame world; Villages and combat, exactly as in vanilla; Choose the audio output device; Mods that read and change the world; Machines and blocks with inventories (API 0.4); Lua and other languages for mods; Well-known shader looks, rewritten for Horizon; A visible beta label; Wayland and Hyprland polish; A versioned agent-control protocol; Port mods with your own AI agent; Horizon on X (coming soon); Pipeline protection and a GitHub App for agents; Signed updates everywhere, installers included. **How to help**: Play it and tell us what differs from vanilla (Discord or GitHub), run the benchmark on your computer, vote for what you want next, or pick one of the Help wanted items. @@ -80,6 +80,16 @@ Servers that show dialogs, such as the register and login screens of AuthMe, wil +#### Pre-generate an area with /pregen + +A /pregen command that builds an area of the world ahead of time, so players don't wait for new terrain when they explore. + +
    Technical + +Generates, lights and saves an area before anyone walks there ([#1805](https://github.com/ludovic111/horizon/issues/1805), a contributor's pull request, in review). Useful on servers, and it moves the generation cost away from the first visit; the generation speed-ups are under 'Steady frames at long render distances'. + +
    + ### Mods and API #### Horizon's mod registry @@ -94,6 +104,40 @@ More: [docs/mods.md](docs/mods.md) +#### Mods that run on the server + +Mods that run on the server side of a world, so a dedicated server or a LAN world can have mods that react to players joining, chat and blocks being broken or placed, and that add commands. + +
    Technical + +API 0.3, experimental first ([#1833](https://github.com/ludovic111/horizon/issues/1833), in review): the same sandboxed component runs on the server thread and the dedicated server, with world events (block broken or placed, entity spawned or killed, player joined or left), game rules and server commands with typed arguments. The world, item and entity interfaces come after (see 'Mods that read and change the world'). [docs/api/mods.md](docs/api/mods.md), [docs/mods.md](docs/mods.md). + +
    + +#### Screens, input and drawing for mods + +Mods will be able to open their own screens and settings pages and react to keys and item use, so bigger mods, such as the Orbital Laser, can run on the normal game. + +
    Technical + +Capability `ui` is in review ([#1864](https://github.com/ludovic111/horizon/issues/1864)): mod-owned screens and a settings page under Options > Mods, built from vanilla's widgets. Still to design: `input` (an item's use, key binds, the crosshair target), `render` (a budgeted draw list on the native shader API) and `world` (changing blocks). Orbital Laser is the acceptance test: it needs all four today through its author's fork (see 'Orbital Laser on the normal game'). + +More: [docs/mods.md](docs/mods.md) + +
    + +#### Tools for people who make mods + +Better tools for mod makers: a test runner that needs no game window, templates for each kind of mod, and clear promises about what keeps working from one version to the next. + +
    Technical + +`cargo horizon-mod test` runs a mod's test scripts against a fake game snapshot ([#1816](https://github.com/ludovic111/horizon/issues/1816)) and, for server mods, scripted server events ([#1837](https://github.com/ludovic111/horizon/issues/1837)); the content template and the guides are merged ([#1859](https://github.com/ludovic111/horizon/issues/1859), [#1810](https://github.com/ludovic111/horizon/issues/1810), [docs/api/stability.md](docs/api/stability.md)). Left: templates for hud, server and machine mods, typed wrappers in the SDK, a frozen 0.1 compatibility mod that every runtime must load, a CI check that fails when a stable reference entry is removed without a new major version, and the SDK on crates.io once the API is stable. + +More: [website/docs/test-a-mod.md](website/docs/test-a-mod.md) + +
    + ### Gameplay parity #### Single player as a server: the last differences @@ -130,6 +174,16 @@ More: [#1561](https://github.com/ludovic111/horizon/issues/1561) · [#1771](http +#### Horizon's own shader format and kit + +Horizon's own way to make shader packs, with tools to write and test them. Every pack is checked before it runs, and the registry will list them. + +
    Technical + +`horizon-shader-api` (API 0.1, experimental): WGSL stages with typed inputs inside the renderer's passes, options as WGSL override constants, and the `horizon-shader new|check|pack` tool with templates and the example pack Golden Hour. Merged: the check every pack passes ([#1820](https://github.com/ludovic111/horizon/issues/1820)) and the tool, templates and guide ([#1822](https://github.com/ludovic111/horizon/issues/1822)). In review: the post stage running in the game with hot reload ([#1861](https://github.com/ludovic111/horizon/issues/1861)), the sky stage ([#1863](https://github.com/ludovic111/horizon/issues/1863)) and registry checks that use the game's own check ([#1855](https://github.com/ludovic111/horizon/issues/1855)). Left: the material, lighting and shadow stages, a last-good fallback on reload, a GPU-time overlay per stage and budgets ([docs/shaders.md](docs/shaders.md), [docs/api/shaders.md](docs/api/shaders.md)). The Iris/OptiFine loader is a separate item, 'Shader packs, written again from scratch'. + +
    + ### Horizon extras #### Discord Rich Presence @@ -160,11 +214,65 @@ A review of the whole tree: dead code and stubs removed ([#1432](https://github. #### Fresh benchmarks against vanilla -New public numbers comparing Horizon with vanilla on many kinds of computers, measured the same way every time, on the website's Benchmarks page. +New public numbers comparing Horizon with vanilla on many kinds of computers, measured the same way every time, with a history that shows what each release changed.
    Technical -Redo the comparison after the current performance work ([#1073](https://github.com/ludovic111/horizon/issues/1073)); reliability, coverage and presentation ([#1346](https://github.com/ludovic111/horizon/issues/1346)); a scene must count as loaded only once its first chunk arrives ([#1749](https://github.com/ludovic111/horizon/issues/1749)). Players' reports come in through `--benchmark` and `horizon-launcher data benchmark` ([docs/benchmark.md](docs/benchmark.md)). +Redo the comparison after the current performance work ([#1073](https://github.com/ludovic111/horizon/issues/1073)); reliability, coverage and presentation ([#1346](https://github.com/ludovic111/horizon/issues/1346)); a scene must count as loaded only once its first chunk arrives ([#1749](https://github.com/ludovic111/horizon/issues/1749)). Next: a performance history across releases (an automatic comparison of each release with the last, on a timeline), more report fields (server TPS and MSPT, power, battery drain, temperature, throttling) and more hardware (a MacBook on battery, low-end laptops, community reports). Players' reports come in through `--benchmark` and `horizon-launcher data benchmark` ([docs/benchmark.md](docs/benchmark.md)). + +
    + +#### Faster world creation and entry on small CPUs + +Creating a world and joining it should be quick even on a four-thread laptop, where a fresh world at 32 chunks can make you wait minutes for the first terrain. + +
    Technical + +Cut the per-column generation cost (noise sampling, surface rules, aquifers) and the lock convoy on structure starts, and leave one core free for the rest of the game as vanilla does ([#1760](https://github.com/ludovic111/horizon/issues/1760) and [#1778](https://github.com/ludovic111/horizon/issues/1778), in review, are listed under 'Steady frames at long render distances'). Still to profile on small CPUs: the first seconds of a fresh world deliver no chunk, and fluids remesh for about a minute after loading. Measured with `tools/perf_bench.py` ([docs/benchmark.md](docs/benchmark.md), [docs/profiling.md](docs/profiling.md)). + +
    + +#### Servers that hold 20 TPS with 50 to 100 players + +Servers that keep 20 ticks per second with 50 to 100 players. Today Horizon holds it up to 50 bots and drops to about 10 at 100. + +
    Technical + +Measured against vanilla on equal work ([docs/server.md](docs/server.md), Scale): Horizon is ahead with few players and when exploring, but vanilla's tick is shorter with 25 to 50 players near spawn. Ideas on the list: send entity changes instead of rebuilding every entity each tick (about 15 percent), fewer memory reads in random ticks and collision scans, cheaper item collision checks, mob AI and natural spawning. The bot benchmark (`horizon-bots`) is the proof. + +
    + +#### Low-end computers: Steam Deck, old Macs, 8 GB + +Horizon should run well on small machines: integrated graphics, the Steam Deck, older Macs and 8 GB of memory, with a battery mode that saves power on laptops. + +
    Technical + +Partly done: an opt-in 60 FPS cap with fewer workers on battery, Far Detail Reduced for integrated GPUs and animated textures on the GPU (both shipped). Left: measured targets and fixes per class of machine (memory use, worker counts, startup) on a Steam Deck, old Intel and Apple Silicon Macs and 8 GB computers, reported on the Benchmarks page. + +More: [docs/benchmark.md](docs/benchmark.md) · [website/docs/performance.md](website/docs/performance.md) + +
    + +#### Native upscaling + +Draw the game at a lower resolution and sharpen it to your screen's size, in the way of FSR or DLSS, to gain frames on weaker graphics cards. An optional graphics setting. + +
    Technical + +A render-scale setting below the window size with an upscaler of our own that runs on every wgpu backend. Proof: frame time against native resolution on integrated GPUs, and screenshots for image quality. Issue [#1403](https://github.com/ludovic111/horizon/issues/1403). + +
    + +#### A batch of targeted optimisations + +Several smaller speed-ups, each measured: idle block entities that sleep, cheaper particles, faster structure placement, high-rate mice and very large texture packs. + +
    Technical + +Block entities and hoppers that skip work when idle (the hopper item scan is already skipped when no item is near); a particle frustum test first (also a parity gap), then particle quads built on the GPU; jigsaw shortcuts that skip blocked connectors and pool elements known not to fit, with layouts unchanged (trial chambers and ancient cities first); batched raw mouse input on Windows for 8000 Hz mice; a better atlas packer for huge resource packs (a 64x pack builds a 704 MiB atlas for 452 MiB of textures); sign-heavy scenes ([#1461](https://github.com/ludovic111/horizon/issues/1461)). Profile first, keep what pays, show before and after numbers. + +More: [docs/profiling.md](docs/profiling.md)
    @@ -180,6 +288,126 @@ The mod browser lists CurseForge as coming soon ([docs/mod-browser.md](docs/mod- +#### A Convert to Rust button for what isn't rewritten yet + +When a mod, shader pack or modpack hasn't been rewritten for Horizon, one button converts what can be converted, so you don't have to wait for the rewrite. + +
    Technical + +Generalise the mod browser's Port step (`horizon-port-pack`: the data, assets, worldgen and block and item definitions of Java mods, .mrpack and CurseForge zips) to every kind of content: code mods where the API allows it (see 'Mods with code') and shader packs, converting an Iris/OptiFine pack into Horizon's format. The browser says what works before the install. Java code itself does not run ([docs/modpacks.md](docs/modpacks.md), [docs/mod-browser.md](docs/mod-browser.md)). + +
    + +#### Mods with code: machines, magic, mobs + +Making mods that add machines, magic and mobs work in Horizon, in steps, starting with machines. They are most of what a big modpack is made of. + +
    Technical + +333 of the 496 mods in a big pack (ATM10) contain code. Issue [#1854](https://github.com/ludovic111/horizon/issues/1854) ranks the API gaps (ticking block entities, networking, rendering, menus, inventories) that unlock them in tiers, with machines first as the recommended path and a furnace ported tick for tick as proof. The first API step is a draft ([#1189](https://github.com/ludovic111/horizon/issues/1189), see 'Machines and blocks with inventories'). The path isn't started yet ([docs/modpacks.md](docs/modpacks.md)). + +
    + +#### Data packs in the mod browser + +Search and install data packs from the launcher's mod browser, for the world you choose, as you do mods and resource packs. + +
    Technical + +A Datapacks tab beside Mods and Resource packs: install into a chosen world's datapacks folder, with the same checks and 'works in Horizon' notes as the other kinds ([docs/mod-browser.md](docs/mod-browser.md)). + +
    + +### Servers and multiplayer + +#### Prepare a version without Java + +Set up Horizon, a server first, without Java on your computer: published tables are combined with your own copy of the game. + +
    Technical + +Today `horizon-prepare` runs Mojang's exporters with Java. Issue [#1750](https://github.com/ludovic111/horizon/issues/1750) proposes publishing the computed version tables once from CI and assembling the rest locally from the player's own jar and assets, provided the tables hold no Mojang content; a contributor offered a pull request, and it would help phones too. A server-only prepare from `server.jar` alone is the first step ([docs/server.md](docs/server.md)). + +
    + +#### Server console, bans and a full admin API + +Manage a server from a console, ban and pardon players, and drive it from other tools through a full admin API and an MCP server for AI agents. + +
    Technical + +`horizon-server console` (a text interface), `ban`, `pardon`, `ban-ip` and `mcp` over stdio. Admin API additions: the player list, kick, ban, op and whitelist, an SSE stream of logs and events, a text metrics endpoint, config GET and PATCH, backup delete and download. The panel's later features (bans, worlds, server.properties import, logs and crash reports, modpacks) and vanilla's management protocol (the JSON-RPC over WebSocket that the `management-server-*` keys configure), so vanilla's tools can manage a Horizon server. [docs/server.md](docs/server.md), [docs/api/server.md](docs/api/server.md). + +
    + +#### Hosting polish: Windows service, safe updates + +Smoother hosting: a real Windows service, a backup before every update, backups that keep the world and each player's state, and a changed view distance or message without a restart. + +
    Technical + +A Windows service wrapper instead of the logon scheduled task; `backups.before_update`; hot reload of config for view distance and the message of the day (needs engine support); backups that keep world and player state ([#1808](https://github.com/ludovic111/horizon/issues/1808), a contributor's draft, in review). [docs/hosting.md](docs/hosting.md), docs/server.md. + +
    + +#### Guided hosting with Tailscale and an invite button + +In the launcher, a guide and a button to host a server and invite friends over Tailscale, so nobody has to open ports on a router. + +
    Technical + +On the Servers page (create, start, back up, update and invite are shipped) add a Tailscale setup that explains and checks each step, and an invite button that hands friends what they need to join over the tailnet. Buttons first, no docs needed, with the details in an Advanced section. Remote access over Tailscale is already described in [docs/server.md](docs/server.md). + +More: [docs/hosting.md](docs/hosting.md) + +
    + +#### An official Docker image for servers + +An official image to run a Horizon dedicated server in Docker with one command, your own copy of the game files mounted in. + +
    Technical + +The repository has the Dockerfile (`packaging/server/Dockerfile`: a multi-stage build on Debian bookworm-slim, user `horizon`, volume `/data`, port 25565, game files mounted read-only, `HORIZON_EULA` and friends answering the first-start questions), and anyone can build it ([docs/hosting.md](docs/hosting.md), section 9). Left: publish the image to a public registry from the release workflow, tagged per release, so it follows the same checks, and update the docs and the launcher. The image never holds Mojang's files. + +More: [docs/server.md](docs/server.md) + +
    + +### Mods and API + +#### Mods that read and change the world + +Mods that can see and change blocks, read inventories and items, and find and affect entities, all through the server so every player sees the same thing. + +
    Technical + +After server mods: read blocks, states and biomes in loaded chunks and change them through the server's block log, so clients and the parity harnesses see vanilla's updates; the player's inventory, held item, open container and item stacks with components; entity queries near a position, a safe subset of entity data and data-driven mod entity types. Snapshots and batched calls per tick, never per-block host calls in hot paths. [docs/api/mods.md](docs/api/mods.md) ('Not there yet'). + +More: [docs/modpacks.md](docs/modpacks.md) + +
    + +#### Machines and blocks with inventories (API 0.4) + +Mods that add machines: blocks that tick, hold items and have their own menus. This unlocks the technology mods of big modpacks. + +
    Technical + +Block entities and menus: ticking machines with slots, server menu logic and the menu protocol to clients, energy, item and fluid transfer, data attachments, accessory slots and custom recipe types, with a typed client and server channel and typed config beside them. A draft ([#1189](https://github.com/ludovic111/horizon/issues/1189)) proves a furnace ported tick for tick and holds the ATM10 code-mods plan; the design is redone through the API policy before it merges ([docs/modpacks.md](docs/modpacks.md), docs/api/stability.md). + +
    + +#### Lua and other languages for mods + +Write mods as Lua scripts, in the style of KubeJS, with no compiler, and later in other languages that pass the tests. Every language runs in the same sandbox. + +
    Technical + +Lua first: Horizon ships a Lua interpreter that is itself a WebAssembly mod, and a script mod's manifest names it and the capabilities it uses, so a script can do exactly what a compiled mod with the same manifest can, with hot reload of `main.lua`. Then C, C++, Zig, Go (TinyGo), AssemblyScript and JavaScript or TypeScript are measured against Rust on the same tick-heavy example, and each is supported only with a template, an example and a CI test. [docs/api/mods.md](docs/api/mods.md) ('Ways in'). + +
    + ### Gameplay parity #### Pistons push players and mobs @@ -192,6 +420,50 @@ Port `PistonMovingBlockEntity.moveCollidedEntities` and `moveStuckEntities` (hon +#### One single-player path: remove the in-frame world + +Single player will have one way to run, the server thread, so every fix works everywhere and there are fewer bugs. The older in-frame mode goes once everything passes without it. + +
    Technical + +`--in-frame` (also `--no-server-thread` and `HORIZON_SERVER_THREAD=0`) still ticks the world inside the window's `Game`, and most harnesses test that path. Delete it once `lan_e2e --server-thread` and the parity harnesses pass on the server thread, which also halves the bug surface. See also 'Single player as a server' and [docs/server-levels.md](docs/server-levels.md). + +
    + +#### Villages and combat, exactly as in vanilla + +Villages and fights behaving as in vanilla: iron golems defending villages, villagers remembering their homes, mace smashes and knockback. + +
    Technical + +Villages: iron golem village goals, villager memories across saves, villager age, job-site claiming on dedicated servers, flower offers ([#1639](https://github.com/ludovic111/horizon/issues/1639) is one report). Combat: the mobs' `attack_knockback` attribute, mace smash logic, the hit knockback source on the host, the fire burnout biome tag. Each is checked against vanilla by a harness ([docs/rust-parity.md](docs/rust-parity.md)). + +
    + +#### Choose the audio output device + +Pick the speakers or headphones Horizon plays through, in Music & Sounds. + +
    Technical + +An output device list in Music & Sounds, falling back to the system default. [#1035](https://github.com/ludovic111/horizon/issues/1035) started it: a player on an older Windows computer had no sound at all, and a device choice is how such cases are found and fixed. + +
    + +### Rendering + +#### Well-known shader looks, rewritten for Horizon + +Popular shader packs rewritten in Horizon's format, so good packs exist from the start. Only packs whose licence is open source and compatible with ours are rewritten, never anything else. + +
    Technical + +The first candidates are Photon, Chocapic13 and MakeUp Ultra Fast, each only after its licence is read and recorded: a pack is rewritten only if it is open source and compatible with Horizon's licence, and the rewrite is written from the shader format's documentation and the look on screen, not from anyone's code. Today only the Golden Hour example exists (`[crates/horizon-shader-api/packs/golden_hour](crates/horizon-shader-api/packs/golden_hour)`). + +More: [docs/shaders.md](docs/shaders.md) + +
    + ### Launcher #### Microsoft sign-in (coming soon) @@ -206,6 +478,18 @@ More: [#1270](https://github.com/ludovic111/horizon/issues/1270) +#### A visible beta label + +A clear 'beta' label in the launcher and the game, so nobody is surprised that Horizon is still being built. + +
    Technical + +Show Beta in the launcher's header and about page, on the game's title screen and on the website, beside the version number ([docs/versioning.md](docs/versioning.md)). + +
    + +### Platforms + #### Horizon on Intel Macs Builds of the game and the launcher for Macs with an Intel processor. @@ -216,29 +500,351 @@ x86_64 game and launcher builds, picked per processor ([#1171](https://github.co -## Later +#### Wayland and Hyprland polish -### Gameplay parity - -#### Following new Minecraft versions - -When Mojang releases a new version of the game, Horizon follows it, and every system is checked against the new version again. +Fixes for Linux desktops that use Wayland and Hyprland: fullscreen, the clipboard, the GUI scale after a restart and the like.
    Technical -The port pipeline detects a release, diffs its classes against 26.3 and writes a port report ([docs/version-ports.md](docs/version-ports.md)); the harnesses then run against the new version. +Open: the GUI Scale is clamped to 2 after a restart under Hyprland, while XWayland keeps it ([#1866](https://github.com/ludovic111/horizon/issues/1866)). Earlier reports covered fullscreen dropping on menu clicks, the clipboard and a white overlay ([#1007](https://github.com/ludovic111/horizon/issues/1007), closed). Checked on a real Wayland session. + +
    + +### AI and agents + +#### A versioned agent-control protocol + +A stable, documented way for AI agents and scripts to drive the game and the launcher, with versions and an event stream so tools keep working as Horizon changes. + +
    Technical + +`hello` answers with the API version, the release and the features; `horizon-ctl --api`; MCP serverInfo; reply schemas generated into the reference; a `test.` namespace for hooks; transcript compatibility tests; `subscribe` as a stream of events on one connection; Horizon option files by key and saved-server commands. Agent control itself (`horizon-ctl`, both MCP servers, the No AI switch) is done: [docs/agent-control.md](docs/agent-control.md), [docs/api/agent-control.md](docs/api/agent-control.md). + +More: [docs/api/stability.md](docs/api/stability.md) + +
    + +#### Port mods with your own AI agent + +You can port a mod to Horizon with your own AI agent on your own computer. Horizon documents how and gives you the tools, but provides no hosted agents. + +
    Technical + +A player runs their own agent locally with `horizon-ctl` and the MCP servers ([docs/agent-control.md](docs/agent-control.md)), `cargo horizon-mod`, `horizon-port-pack` and the parity harnesses as the check. Horizon writes the guide ([website/docs/agents.md](website/docs/agents.md), [docs/modpacks.md](docs/modpacks.md)) and runs no agents, accounts or keys for players. The No AI switch turns all of it off. + +
    + +### Community + +#### Horizon on X (coming soon) + +Coming soon: an official Horizon account on X, used to announce releases and the weekly summary. + +
    Technical + +Kira already posts the roadmap items marked `announce = true` when they ship, and the weekly summary, as soon as the account's credentials exist ([roadmap/README.md](roadmap/README.md), 'The journal'). Waiting for the account itself. + +
    + +### Code and tools + +#### Pipeline protection and a GitHub App for agents + +Safer releases: changes to the release machinery need the owner's review, and AI agents use a smaller identity of their own instead of a personal account. + +
    Technical + +`.github/CODEOWNERS` lists the workflows, the release and signing tools, the update-checking code and the installers ([#1015](https://github.com/ludovic111/horizon/issues/1015)); the branch protection that makes it count waits for the `horizon-agent` GitHub App, which reads and writes code, pull requests and issues but can't change settings, workflows, secrets or members, and whose tokens last an hour. It needs the owner to create the App and switch the protection on ([docs/security.md](docs/security.md)). + +
    + +#### Signed updates everywhere, installers included + +Every download Horizon makes is checked against the project's signature, including the first install with install.sh and install.ps1. + +
    Technical + +Releases carry signed checksums, and the launcher and `horizon-server update` refuse anything unsigned ([#1011](https://github.com/ludovic111/horizon/issues/1011), [#1014](https://github.com/ludovic111/horizon/issues/1014)). Left: the first install through `install.sh` and `install.ps1` still trusts only the checksum GitHub publishes, because verifying Ed25519 signatures needs OpenSSL 3, which macOS doesn't ship ([docs/security.md](docs/security.md)). + +
    + +## Later + +### Performance + +#### Exact far terrain from real chunks + +Far terrain drawn from the world you really built and explored, caves and builds included, not only a preview from the seed. + +
    Technical + +Keep a 3D mip store on disk built from real chunks, drawn by the GPU beside the seed preview for areas nobody has visited. Written from scratch and measured before it is kept. Builds on Far Detail and the far-terrain levels of detail already shipped; the open work on them is [#718](https://github.com/ludovic111/horizon/issues/718). + +
    + +### Modpacks + +#### Older Fabric and Forge mods + +Recognise mods built for versions before 26.1 in the mod browser, so they can be checked and carried over like newer ones. + +
    Technical + +A mapping table from intermediary and SRG names so pre-26.1 jars can be classified, and a rename for mod ids with dashes (`fabric-api`); the port step then treats them like newer jars ([docs/mod-browser.md](docs/mod-browser.md)). + +
    + +#### A modpack's own resource and shader packs + +Importing a modpack also brings the resource packs and shader packs it ships with, where Horizon can use them. + +
    Technical + +Porting a pack carries data, assets and worldgen today; the resource packs and shader packs bundled with it are left out. Resource packs can be carried as they are; shader packs go through the converter ([docs/modpacks.md](docs/modpacks.md)). + +
    + +#### Whole modpacks, such as ATM10, rewritten + +Rewriting a complete big modpack for Horizon so it runs far better than the original. Not a priority now: the work above comes first. + +
    Technical + +The project's first goal for modpacks: support them ten times better than vanilla and port any of them. It was set aside in favour of compatibility through the browser. What exists: ATM10's world generates and 42 of its 496 mods are carried at least in part ([docs/modpacks.md](docs/modpacks.md)). It depends on 'Mods with code' and the native mod API. + +
    + +#### Running Java mods that aren't rewritten + +Run Java mods that nobody has rewritten for Horizon yet, through a bridge or a converter. Hard and not started: the Convert button and native rewrites come first. + +
    Technical + +Earlier research ([#527](https://github.com/ludovic111/horizon/issues/527), closed) found that Java mods can't simply run, because they call Minecraft's own classes. Options to look at again: a jar-to-WebAssembly backend, or a bridge for simple Fabric mods. Whatever is chosen has to run inside the same sandbox and capability checks as native mods. + +
    + +### Servers and multiplayer + +#### Ely.by and other sign-in providers + +Sign in with other account providers, such as Ely.by or any Yggdrasil-compatible one, beside Microsoft. + +
    Technical + +Issue [#1270](https://github.com/ludovic111/horizon/issues/1270) asks for Ely.by and Yggdrasil-compatible servers with an account selector in the launcher. It builds on the Microsoft sign-in ([docs/microsoft-account.md](docs/microsoft-account.md)). + +
    + +#### Online-mode checks for Play with Friends + +Friends who join through an invite code could prove who they are with their Microsoft account, as on online-mode servers. + +
    Technical + +Play with Friends joins over an encrypted tunnel with an invite code ([docs/play-with-friends.md](docs/play-with-friends.md), 'Identity'). Online-mode checks against Mojang's session servers can be added on top of the same tunnel; they need Microsoft sign-in first, which is coming soon. Play with Friends stays self-hosted between players, with no relay run by the project. + +
    + +### Mods and API + +#### Orbital Laser on the normal game + +The community mod Orbital Laser, made for a fork of Horizon, running on the normal game and listed in the registry as the showcase of the mod API. + +
    Technical + +A pull request on [https://github.com/Stink-O/orbital-laser](https://github.com/Stink-O/orbital-laser) (MIT) once the input, ui, render and world capabilities exist (see 'Screens, input and drawing for mods'): today the mod needs them through its author's fork. Tested in the game, and listed in the registry. + +
    + +#### Native versions of popular client mods + +Built-in versions of the client mods players ask for: block info on hover, a recipe viewer like JEI, a world map and schematic overlays. + +
    Technical + +Done as extras: a mod list and menu, a minimap and a zoom ([docs/extras.md](docs/extras.md)). Left: block info tooltips, a recipe viewer (a contributor offered to port JEI if it becomes a native feature), a world map and a schematic overlay. They need the read-only interfaces `inventory-read`, `recipes`, `world-read` and `render` in the mod API, or they go into the game directly. + +More: [docs/mods.md](docs/mods.md) + +
    + +#### Create-style physics as a native mod + +Contraptions and physics like the Create and Aeronautics mods, as a mod for Horizon rather than part of the base game. + +
    Technical + +Issue [#805](https://github.com/ludovic111/horizon/issues/805) (porting QOL mods) closed with this answer: it would come as a native Horizon mod on the mod API. It needs server mods, entities and rendering first. + +
    + +#### Proximity voice chat as a native mod + +Hear nearby players talk, as with the voice chat mods of other Minecraft versions, as a Horizon mod. + +
    Technical + +Needs server mods, mod networking and an audio interface in the mod API before anything can be built ([docs/api/mods.md](docs/api/mods.md)). + +
    + +### Gameplay parity + +#### Every Minecraft version, prepared automatically + +Every past and future Minecraft version prepared for Horizon automatically. When Mojang releases one, Horizon follows it, and every system is checked against it again. + +
    Technical + +The port pipeline ([docs/version-ports.md](docs/version-ports.md), `tools/version_port.py`) detects a release, prepares it, diffs its classes against the pinned 26.3, regenerates the data-driven tables, runs every parity harness on it and writes a port report that becomes issues. The goal is as little human work as possible for any version, past or future. Detection and the first slices are merged; the scheduled agent that runs it every day isn't installed yet. Two versions in one build are out of scope. + +
    + +#### Controllers: tested on real pads, and in the launcher + +Make controller support solid: tested with real PlayStation and Xbox pads and a Steam Deck, and usable in the launcher too. + +
    Technical + +The game side is merged ([docs/controllers.md](docs/controllers.md)): pads read with gilrs, menus, rumble, glyphs, remapping and DualSense adaptive triggers, lightbar and haptics, but not yet confirmed on real pads or a Steam Deck (DualSense haptics stay behind a switch until then). Left: a test pass on real hardware, and the same focus navigation in the GPUI launcher. + +
    + +#### The full debug screen + +Vanilla's complete F3 screen: debug options, a profiler chart, ten-second captures and an atlas dump. + +
    Technical + +Today F3 shows position, chunk, light, biome and FPS ([docs/native-client.md](docs/native-client.md)). Vanilla also has F3+F6 debug options with profiles, F3+1 the profiler pie, F3+L a ten-second capture and F3+S the atlas dump ([#562](https://github.com/ludovic111/horizon/issues/562) asked for the F3 editor). + +
    + +### Rendering + +#### A light default shader pack + +A small built-in look (tone mapping, colour grade, a cheap vignette) that costs almost nothing, for players who want a nicer picture without a heavy pack. + +
    Technical + +An opt-in, composite-only pack on the native shader API, under about 0.3 ms at 1080p, shown on the Shader Packs page. Pack option sliders and profiles, and a Failed row with the full error, belong with it. + +More: [docs/shaders.md](docs/shaders.md) + +
    + +#### Optional ray tracing + +An optional ray-tracing mode for graphics cards that support it (RTX and recent AMD): shadows, water reflections and global lighting on top of the vanilla look. + +
    Technical + +A spike with ray-traced sun shadows, off by default, is a draft ([#1180](https://github.com/ludovic111/horizon/issues/1180)). Reflections, global illumination, Apple M3 and newer if it is cheap, and a performance report per card come after. + +
    + +#### HDR monitors + +Output for HDR monitors: brighter highlights and richer colour where your screen supports it. + +
    Technical + +Issue [#1441](https://github.com/ludovic111/horizon/issues/1441). Needs a surface format and a tone-mapping path per backend, and tests on real HDR screens on Windows, macOS and Linux. + +
    + +#### MSAA, anisotropic filtering, exclusive fullscreen + +More graphics options: multisample anti-aliasing, anisotropic filtering and an exclusive fullscreen mode. Today Horizon has FXAA and borderless fullscreen. + +
    Technical + +Not implemented ([docs/graphics.md](docs/graphics.md)). Anisotropic filtering needs linear magnification in wgpu, which blurs close blocks, so close-up sharpness has to be kept first; MSAA would sit beside FXAA; exclusive fullscreen where the backend supports it. + +
    + +### Launcher + +#### Lean installs that use less disk + +Smaller installs: Horizon keeps one shared copy of the prepared game files and removes what only the preparation needed. + +
    Technical + +A shared resource store keyed by the preparer's digest, no prepare intermediates left behind, the current and the previous version kept, and an automatic clean-up. Not started. + +
    + +### Platforms + +#### Horizon on Linux ARM64 + +Game and launcher builds for 64-bit ARM Linux computers. + +
    Technical + +Issue [#1709](https://github.com/ludovic111/horizon/issues/1709): aarch64 builds of the game and the launcher in the release workflow, tested on a real ARM machine with its Vulkan driver. The server build already drops audio and controllers (`--no-default-features`). + +
    + +#### OpenGL for old computers + +A way to play on old PCs and Linux machines that have no Vulkan or DirectX 12, through OpenGL. + +
    Technical + +Players on older hardware ask whether Vulkan is the only way. Horizon draws with wgpu, which has an OpenGL backend; what is needed is to see which features hold up on it, how fast it runs, and a launcher switch like the DirectX 12 fallback that already ships. + +More: [docs/graphics.md](docs/graphics.md) + +
    + +#### Android, iOS and the browser + +Versions of Horizon for Android, iOS and web browsers. Nothing official yet; contributors have started the first pieces. + +
    Technical + +No official build. Android was first closed as not planned, with touch controls, a mobile interface, packaging and loading the player's own assets all missing; the roadmap now includes it, iOS and a WebAssembly build for the browser as long-term goals. First pieces from contributors: `horizon-dualsense` compiling on iOS ([#1813](https://github.com/ludovic111/horizon/issues/1813)) and helper-thread limits on Android ([#1751](https://github.com/ludovic111/horizon/issues/1751)). Preparing without Java ([#1750](https://github.com/ludovic111/horizon/issues/1750)) matters here too. Horizon ships none of Mojang's files on any platform.
    ### Horizon extras -#### An optional AI teammate +#### A complete video studio inside the game -An AI teammate that plays beside you and that you can talk to. It is off unless you turn it on, and the No AI switch hides it completely. +Record, replay and edit videos without leaving the game: record in a keypress, replay a session, cut and finish the video. One of Horizon's original goals.
    Technical -Draft pull requests: the companion ([#1068](https://github.com/ludovic111/horizon/issues/1068)), its safety modes and their evaluation ([#1069](https://github.com/ludovic111/horizon/issues/1069)), and a route that finishes the game ([#1070](https://github.com/ludovic111/horizon/issues/1070)). Local models first; it ships only once the safety evaluation passes. +Three stages. Recording: capture gameplay in a keypress with an encoder thread and little frame-time cost. Replay: a replay mode for recorded sessions. Editing: cuts, camera paths, titles and export. None of it exists yet; the agent-control socket ([docs/agent-control.md](docs/agent-control.md)) already lets tools drive the game for scripted shots. + +
    + +### AI and agents + +#### An optional AI teammate + +An optional AI teammate you can talk to by voice. It plays beside you, helps or finishes the game with you, runs on your computer first, and exists only if you turn it on. The No AI switch hides it. + +
    Technical + +Draft pull requests: the companion ([#1068](https://github.com/ludovic111/horizon/issues/1068)), its safety modes and their evaluation ([#1069](https://github.com/ludovic111/horizon/issues/1069)) and a route that finishes the game ([#1070](https://github.com/ludovic111/horizon/issues/1070)). In the drafts: a companion that follows, fights, mines, crafts, sleeps and respawns; chat and push-to-talk voice, with speech recognition and a choice of voices; presets (very strong, helper, beat the game); a guard model with Clean, Kid and Salty modes; perception levels from Fair to All-knowing; hardware detection with a simple mode and an Advanced tab; local models first, cloud keys optional. The No AI switch is already in the game. Left: the speedrun route doesn't finish the game reliably yet, voices for more languages, and the safety evaluation, which must pass before it ships. + +
    + +### Community + +#### A video series about Horizon + +A series of videos about how Horizon is made and what is new, with each upload announced on Discord. + +
    Technical + +Kira already shows new uploads in a read-only Discord channel (done). The series follows each stage of the project: a script, edits, titles and a thumbnail per video.
    diff --git a/roadmap/roadmap.toml b/roadmap/roadmap.toml index 5ad4d8846..34ae8a955 100644 --- a/roadmap/roadmap.toml +++ b/roadmap/roadmap.toml @@ -49,10 +49,22 @@ name = "Rendering" id = "launcher" name = "Launcher" +[[area]] +id = "platforms" +name = "Platforms" + [[area]] id = "extras" name = "Horizon extras" +[[area]] +id = "ai" +name = "AI and agents" + +[[area]] +id = "community" +name = "Community" + [[area]] id = "project" name = "Code and tools" @@ -206,6 +218,75 @@ dev = """A review of the whole tree: dead code and stubs removed (#1432, #1658), feature to its files and harnesses.""" links = ["#1432", "#1658", "#1453", "#1454", "#1448", "#1707", "docs/architecture.md"] +[[item]] +id = "native-shaders" +title = "Horizon's own shader format and kit" +status = "now" +area = "rendering" +plain = """Horizon's own way to make shader packs, with tools to write and test them. Every pack is checked before it \ +runs, and the registry will list them.""" +dev = """`horizon-shader-api` (API 0.1, experimental): WGSL stages with typed inputs inside the renderer's passes, \ +options as WGSL override constants, and the `horizon-shader new|check|pack` tool with templates and the \ +example pack Golden Hour. Merged: the check every pack passes (#1820) and the tool, templates and guide \ +(#1822). In review: the post stage running in the game with hot reload (#1861), the sky stage (#1863) and \ +registry checks that use the game's own check (#1855). Left: the material, lighting and shadow stages, a \ +last-good fallback on reload, a GPU-time overlay per stage and budgets (docs/shaders.md, \ +docs/api/shaders.md). The Iris/OptiFine loader is a separate item, 'Shader packs, written again from \ +scratch'.""" +links = ["#1820", "#1822", "#1861", "#1863", "#1855", "docs/shaders.md", "docs/api/shaders.md"] + +[[item]] +id = "server-mods" +title = "Mods that run on the server" +status = "now" +area = "mods" +plain = """Mods that run on the server side of a world, so a dedicated server or a LAN world can have mods that react \ +to players joining, chat and blocks being broken or placed, and that add commands.""" +dev = """API 0.3, experimental first (#1833, in review): the same sandboxed component runs on the server thread and \ +the dedicated server, with world events (block broken or placed, entity spawned or killed, player joined or \ +left), game rules and server commands with typed arguments. The world, item and entity interfaces come after \ +(see 'Mods that read and change the world'). docs/api/mods.md, docs/mods.md.""" +links = ["#1833", "docs/api/mods.md", "docs/mods.md"] + +[[item]] +id = "mod-screens" +title = "Screens, input and drawing for mods" +status = "now" +area = "mods" +plain = """Mods will be able to open their own screens and settings pages and react to keys and item use, so bigger \ +mods, such as the Orbital Laser, can run on the normal game.""" +dev = """Capability `ui` is in review (#1864): mod-owned screens and a settings page under Options > Mods, built from \ +vanilla's widgets. Still to design: `input` (an item's use, key binds, the crosshair target), `render` (a \ +budgeted draw list on the native shader API) and `world` (changing blocks). Orbital Laser is the acceptance \ +test: it needs all four today through its author's fork (see 'Orbital Laser on the normal game').""" +links = ["#1864", "docs/mods.md"] + +[[item]] +id = "modder-tools" +title = "Tools for people who make mods" +status = "now" +area = "mods" +plain = """Better tools for mod makers: a test runner that needs no game window, templates for each kind of mod, and \ +clear promises about what keeps working from one version to the next.""" +dev = """`cargo horizon-mod test` runs a mod's test scripts against a fake game snapshot (#1816) and, for server \ +mods, scripted server events (#1837); the content template and the guides are merged (#1859, #1810, \ +docs/api/stability.md). Left: templates for hud, server and machine mods, typed wrappers in the SDK, a \ +frozen 0.1 compatibility mod that every runtime must load, a CI check that fails when a stable reference \ +entry is removed without a new major version, and the SDK on crates.io once the API is stable.""" +links = ["#1816", "#1837", "docs/api/stability.md", "website/docs/test-a-mod.md"] + +[[item]] +id = "pregen" +title = "Pre-generate an area with /pregen" +status = "now" +area = "servers" +plain = """A /pregen command that builds an area of the world ahead of time, so players don't wait for new terrain when \ +they explore.""" +dev = """Generates, lights and saves an area before anyone walks there (#1805, a contributor's pull request, in \ +review). Useful on servers, and it moves the generation cost away from the first visit; the generation \ +speed-ups are under 'Steady frames at long render distances'.""" +links = ["#1805"] + # ---------------------------------------------------------------------------------------------------- Next [[item]] @@ -239,11 +320,14 @@ id = "benchmarks" title = "Fresh benchmarks against vanilla" status = "next" area = "performance" -plain = """New public numbers comparing Horizon with vanilla on many kinds of computers, measured the same way \ -every time, on the website's Benchmarks page.""" +plain = """New public numbers comparing Horizon with vanilla on many kinds of computers, measured the same way every \ +time, with a history that shows what each release changed.""" dev = """Redo the comparison after the current performance work (#1073); reliability, coverage and presentation \ -(#1346); a scene must count as loaded only once its first chunk arrives (#1749). Players' reports come in through \ -`--benchmark` and `horizon-launcher data benchmark` (docs/benchmark.md).""" +(#1346); a scene must count as loaded only once its first chunk arrives (#1749). Next: a performance history \ +across releases (an automatic comparison of each release with the last, on a timeline), more report fields \ +(server TPS and MSPT, power, battery drain, temperature, throttling) and more hardware (a MacBook on \ +battery, low-end laptops, community reports). Players' reports come in through `--benchmark` and \ +`horizon-launcher data benchmark` (docs/benchmark.md).""" links = ["#1073", "#1346", "#1749", "docs/benchmark.md"] [[item]] @@ -262,36 +346,659 @@ links = ["#1496", "#1609", "#1608", "#1704", "#1758"] id = "intel-mac" title = "Horizon on Intel Macs" status = "next" -area = "launcher" -plain = """Builds of the game and the launcher for Macs with an Intel processor.""" +area = "platforms" +plain = "Builds of the game and the launcher for Macs with an Intel processor." dev = """x86_64 game and launcher builds, picked per processor (#1171). Before it ships it needs a run on a real \ Intel Mac: Rosetta on Apple Silicon only proves the CPU code. Requests: #1161, #1792.""" links = ["#1171", "#1161", "#1792"] +[[item]] +id = "world-entry" +title = "Faster world creation and entry on small CPUs" +status = "next" +area = "performance" +plain = """Creating a world and joining it should be quick even on a four-thread laptop, where a fresh world at 32 \ +chunks can make you wait minutes for the first terrain.""" +dev = """Cut the per-column generation cost (noise sampling, surface rules, aquifers) and the lock convoy on \ +structure starts, and leave one core free for the rest of the game as vanilla does (#1760 and #1778, in \ +review, are listed under 'Steady frames at long render distances'). Still to profile on small CPUs: the \ +first seconds of a fresh world deliver no chunk, and fluids remesh for about a minute after loading. \ +Measured with `tools/perf_bench.py` (docs/benchmark.md, docs/profiling.md).""" +links = ["docs/benchmark.md", "docs/profiling.md"] + +[[item]] +id = "server-tick" +title = "Servers that hold 20 TPS with 50 to 100 players" +status = "next" +area = "performance" +plain = """Servers that keep 20 ticks per second with 50 to 100 players. Today Horizon holds it up to 50 bots and drops \ +to about 10 at 100.""" +dev = """Measured against vanilla on equal work (docs/server.md, Scale): Horizon is ahead with few players and when \ +exploring, but vanilla's tick is shorter with 25 to 50 players near spawn. Ideas on the list: send entity \ +changes instead of rebuilding every entity each tick (about 15 percent), fewer memory reads in random ticks \ +and collision scans, cheaper item collision checks, mob AI and natural spawning. The bot benchmark \ +(`horizon-bots`) is the proof.""" +links = ["docs/server.md"] + +[[item]] +id = "low-end-mode" +title = "Low-end computers: Steam Deck, old Macs, 8 GB" +status = "next" +area = "performance" +plain = """Horizon should run well on small machines: integrated graphics, the Steam Deck, older Macs and 8 GB of \ +memory, with a battery mode that saves power on laptops.""" +dev = """Partly done: an opt-in 60 FPS cap with fewer workers on battery, Far Detail Reduced for integrated GPUs and \ +animated textures on the GPU (both shipped). Left: measured targets and fixes per class of machine (memory \ +use, worker counts, startup) on a Steam Deck, old Intel and Apple Silicon Macs and 8 GB computers, reported \ +on the Benchmarks page.""" +links = ["docs/benchmark.md", "website/docs/performance.md"] + +[[item]] +id = "upscaling" +title = "Native upscaling" +status = "next" +area = "performance" +plain = """Draw the game at a lower resolution and sharpen it to your screen's size, in the way of FSR or DLSS, to gain \ +frames on weaker graphics cards. An optional graphics setting.""" +dev = """A render-scale setting below the window size with an upscaler of our own that runs on every wgpu backend. \ +Proof: frame time against native resolution on integrated GPUs, and screenshots for image quality. Issue \ +#1403.""" +links = ["#1403"] + +[[item]] +id = "targeted-optimisations" +title = "A batch of targeted optimisations" +status = "next" +area = "performance" +plain = """Several smaller speed-ups, each measured: idle block entities that sleep, cheaper particles, faster \ +structure placement, high-rate mice and very large texture packs.""" +dev = """Block entities and hoppers that skip work when idle (the hopper item scan is already skipped when no item is \ +near); a particle frustum test first (also a parity gap), then particle quads built on the GPU; jigsaw \ +shortcuts that skip blocked connectors and pool elements known not to fit, with layouts unchanged (trial \ +chambers and ancient cities first); batched raw mouse input on Windows for 8000 Hz mice; a better atlas \ +packer for huge resource packs (a 64x pack builds a 704 MiB atlas for 452 MiB of textures); sign-heavy \ +scenes (#1461). Profile first, keep what pays, show before and after numbers.""" +links = ["#1461", "docs/profiling.md"] + +[[item]] +id = "convert-to-rust" +title = "A Convert to Rust button for what isn't rewritten yet" +status = "next" +area = "modpacks" +plain = """When a mod, shader pack or modpack hasn't been rewritten for Horizon, one button converts what can be \ +converted, so you don't have to wait for the rewrite.""" +dev = """Generalise the mod browser's Port step (`horizon-port-pack`: the data, assets, worldgen and block and item \ +definitions of Java mods, .mrpack and CurseForge zips) to every kind of content: code mods where the API \ +allows it (see 'Mods with code') and shader packs, converting an Iris/OptiFine pack into Horizon's format. \ +The browser says what works before the install. Java code itself does not run (docs/modpacks.md, \ +docs/mod-browser.md).""" +links = ["docs/modpacks.md", "docs/mod-browser.md"] + +[[item]] +id = "code-mods" +title = "Mods with code: machines, magic, mobs" +status = "next" +area = "modpacks" +plain = """Making mods that add machines, magic and mobs work in Horizon, in steps, starting with machines. They are \ +most of what a big modpack is made of.""" +dev = """333 of the 496 mods in a big pack (ATM10) contain code. Issue #1854 ranks the API gaps (ticking block \ +entities, networking, rendering, menus, inventories) that unlock them in tiers, with machines first as the \ +recommended path and a furnace ported tick for tick as proof. The first API step is a draft (#1189, see \ +'Machines and blocks with inventories'). The path isn't started yet (docs/modpacks.md).""" +links = ["#1854", "docs/modpacks.md"] + +[[item]] +id = "datapack-browser" +title = "Data packs in the mod browser" +status = "next" +area = "modpacks" +plain = """Search and install data packs from the launcher's mod browser, for the world you choose, as you do mods and \ +resource packs.""" +dev = """A Datapacks tab beside Mods and Resource packs: install into a chosen world's datapacks folder, with the \ +same checks and 'works in Horizon' notes as the other kinds (docs/mod-browser.md).""" +links = ["docs/mod-browser.md"] + +[[item]] +id = "prepare-without-java" +title = "Prepare a version without Java" +status = "next" +area = "servers" +plain = """Set up Horizon, a server first, without Java on your computer: published tables are combined with your own \ +copy of the game.""" +dev = """Today `horizon-prepare` runs Mojang's exporters with Java. Issue #1750 proposes publishing the computed \ +version tables once from CI and assembling the rest locally from the player's own jar and assets, provided \ +the tables hold no Mojang content; a contributor offered a pull request, and it would help phones too. A \ +server-only prepare from `server.jar` alone is the first step (docs/server.md).""" +links = ["#1750", "docs/server.md"] + +[[item]] +id = "server-admin" +title = "Server console, bans and a full admin API" +status = "next" +area = "servers" +plain = """Manage a server from a console, ban and pardon players, and drive it from other tools through a full admin \ +API and an MCP server for AI agents.""" +dev = """`horizon-server console` (a text interface), `ban`, `pardon`, `ban-ip` and `mcp` over stdio. Admin API \ +additions: the player list, kick, ban, op and whitelist, an SSE stream of logs and events, a text metrics \ +endpoint, config GET and PATCH, backup delete and download. The panel's later features (bans, worlds, \ +server.properties import, logs and crash reports, modpacks) and vanilla's management protocol (the JSON-RPC \ +over WebSocket that the `management-server-*` keys configure), so vanilla's tools can manage a Horizon \ +server. docs/server.md, docs/api/server.md.""" +links = ["docs/server.md", "docs/api/server.md"] + +[[item]] +id = "hosting-polish" +title = "Hosting polish: Windows service, safe updates" +status = "next" +area = "servers" +plain = """Smoother hosting: a real Windows service, a backup before every update, backups that keep the world and each \ +player's state, and a changed view distance or message without a restart.""" +dev = """A Windows service wrapper instead of the logon scheduled task; `backups.before_update`; hot reload of config \ +for view distance and the message of the day (needs engine support); backups that keep world and player \ +state (#1808, a contributor's draft, in review). docs/hosting.md, docs/server.md.""" +links = ["#1808", "docs/hosting.md"] + +[[item]] +id = "tailscale-hosting" +title = "Guided hosting with Tailscale and an invite button" +status = "next" +area = "servers" +plain = """In the launcher, a guide and a button to host a server and invite friends over Tailscale, so nobody has to \ +open ports on a router.""" +dev = """On the Servers page (create, start, back up, update and invite are shipped) add a Tailscale setup that \ +explains and checks each step, and an invite button that hands friends what they need to join over the \ +tailnet. Buttons first, no docs needed, with the details in an Advanced section. Remote access over \ +Tailscale is already described in docs/server.md.""" +links = ["docs/hosting.md", "docs/server.md"] + +[[item]] +id = "docker-image" +title = "An official Docker image for servers" +status = "next" +area = "servers" +plain = """An official image to run a Horizon dedicated server in Docker with one command, your own copy of the game \ +files mounted in.""" +dev = """The repository has the Dockerfile (`packaging/server/Dockerfile`: a multi-stage build on Debian \ +bookworm-slim, user `horizon`, volume `/data`, port 25565, game files mounted read-only, `HORIZON_EULA` and \ +friends answering the first-start questions), and anyone can build it (docs/hosting.md, section 9). Left: \ +publish the image to a public registry from the release workflow, tagged per release, so it follows the same \ +checks, and update the docs and the launcher. The image never holds Mojang's files.""" +links = ["docs/hosting.md", "docs/server.md"] + +[[item]] +id = "one-single-player-path" +title = "One single-player path: remove the in-frame world" +status = "next" +area = "gameplay" +plain = """Single player will have one way to run, the server thread, so every fix works everywhere and there are fewer \ +bugs. The older in-frame mode goes once everything passes without it.""" +dev = """`--in-frame` (also `--no-server-thread` and `HORIZON_SERVER_THREAD=0`) still ticks the world inside the \ +window's `Game`, and most harnesses test that path. Delete it once `lan_e2e --server-thread` and the parity \ +harnesses pass on the server thread, which also halves the bug surface. See also 'Single player as a server' \ +and docs/server-levels.md.""" +links = ["docs/server-levels.md"] + +[[item]] +id = "villages-combat-parity" +title = "Villages and combat, exactly as in vanilla" +status = "next" +area = "gameplay" +plain = """Villages and fights behaving as in vanilla: iron golems defending villages, villagers remembering their \ +homes, mace smashes and knockback.""" +dev = """Villages: iron golem village goals, villager memories across saves, villager age, job-site claiming on \ +dedicated servers, flower offers (#1639 is one report). Combat: the mobs' `attack_knockback` attribute, mace \ +smash logic, the hit knockback source on the host, the fire burnout biome tag. Each is checked against \ +vanilla by a harness (docs/rust-parity.md).""" +links = ["#1639", "docs/rust-parity.md"] + +[[item]] +id = "audio-device" +title = "Choose the audio output device" +status = "next" +area = "gameplay" +plain = "Pick the speakers or headphones Horizon plays through, in Music & Sounds." +dev = """An output device list in Music & Sounds, falling back to the system default. #1035 started it: a player on \ +an older Windows computer had no sound at all, and a device choice is how such cases are found and fixed.""" +links = ["#1035"] + +[[item]] +id = "mod-world-items" +title = "Mods that read and change the world" +status = "next" +area = "mods" +plain = """Mods that can see and change blocks, read inventories and items, and find and affect entities, all through \ +the server so every player sees the same thing.""" +dev = """After server mods: read blocks, states and biomes in loaded chunks and change them through the server's \ +block log, so clients and the parity harnesses see vanilla's updates; the player's inventory, held item, \ +open container and item stacks with components; entity queries near a position, a safe subset of entity data \ +and data-driven mod entity types. Snapshots and batched calls per tick, never per-block host calls in hot \ +paths. docs/api/mods.md ('Not there yet').""" +links = ["docs/api/mods.md", "docs/modpacks.md"] + +[[item]] +id = "machines-api" +title = "Machines and blocks with inventories (API 0.4)" +status = "next" +area = "mods" +plain = """Mods that add machines: blocks that tick, hold items and have their own menus. This unlocks the technology \ +mods of big modpacks.""" +dev = """Block entities and menus: ticking machines with slots, server menu logic and the menu protocol to clients, \ +energy, item and fluid transfer, data attachments, accessory slots and custom recipe types, with a typed \ +client and server channel and typed config beside them. A draft (#1189) proves a furnace ported tick for \ +tick and holds the ATM10 code-mods plan; the design is redone through the API policy before it merges \ +(docs/modpacks.md, docs/api/stability.md).""" +links = ["#1189", "docs/modpacks.md"] + +[[item]] +id = "mod-languages" +title = "Lua and other languages for mods" +status = "next" +area = "mods" +plain = """Write mods as Lua scripts, in the style of KubeJS, with no compiler, and later in other languages that pass \ +the tests. Every language runs in the same sandbox.""" +dev = """Lua first: Horizon ships a Lua interpreter that is itself a WebAssembly mod, and a script mod's manifest \ +names it and the capabilities it uses, so a script can do exactly what a compiled mod with the same manifest \ +can, with hot reload of `main.lua`. Then C, C++, Zig, Go (TinyGo), AssemblyScript and JavaScript or \ +TypeScript are measured against Rust on the same tick-heavy example, and each is supported only with a \ +template, an example and a CI test. docs/api/mods.md ('Ways in').""" +links = ["docs/api/mods.md"] + +[[item]] +id = "shader-pack-examples" +title = "Well-known shader looks, rewritten for Horizon" +status = "next" +area = "rendering" +plain = """Popular shader packs rewritten in Horizon's format, so good packs exist from the start. Only packs whose \ +licence is open source and compatible with ours are rewritten, never anything else.""" +dev = """The first candidates are Photon, Chocapic13 and MakeUp Ultra Fast, each only after its licence is read and \ +recorded: a pack is rewritten only if it is open source and compatible with Horizon's licence, and the \ +rewrite is written from the shader format's documentation and the look on screen, not from anyone's code. \ +Today only the Golden Hour example exists (`crates/horizon-shader-api/packs/golden_hour`).""" +links = ["docs/shaders.md", "crates/horizon-shader-api/packs/golden_hour"] + +[[item]] +id = "beta-label" +title = "A visible beta label" +status = "next" +area = "launcher" +plain = """A clear 'beta' label in the launcher and the game, so nobody is surprised that Horizon is still being built.""" +dev = """Show Beta in the launcher's header and about page, on the game's title screen and on the website, beside the \ +version number (docs/versioning.md).""" +links = ["docs/versioning.md"] + +[[item]] +id = "wayland-polish" +title = "Wayland and Hyprland polish" +status = "next" +area = "platforms" +plain = """Fixes for Linux desktops that use Wayland and Hyprland: fullscreen, the clipboard, the GUI scale after a \ +restart and the like.""" +dev = """Open: the GUI Scale is clamped to 2 after a restart under Hyprland, while XWayland keeps it (#1866). Earlier \ +reports covered fullscreen dropping on menu clicks, the clipboard and a white overlay (#1007, closed). \ +Checked on a real Wayland session.""" +links = ["#1866"] + +[[item]] +id = "agent-control-protocol" +title = "A versioned agent-control protocol" +status = "next" +area = "ai" +plain = """A stable, documented way for AI agents and scripts to drive the game and the launcher, with versions and an \ +event stream so tools keep working as Horizon changes.""" +dev = """`hello` answers with the API version, the release and the features; `horizon-ctl --api`; MCP serverInfo; \ +reply schemas generated into the reference; a `test.` namespace for hooks; transcript compatibility tests; \ +`subscribe` as a stream of events on one connection; Horizon option files by key and saved-server commands. \ +Agent control itself (`horizon-ctl`, both MCP servers, the No AI switch) is done: docs/agent-control.md, \ +docs/api/agent-control.md.""" +links = ["docs/agent-control.md", "docs/api/agent-control.md", "docs/api/stability.md"] + +[[item]] +id = "own-agents-porting" +title = "Port mods with your own AI agent" +status = "next" +area = "ai" +plain = """You can port a mod to Horizon with your own AI agent on your own computer. Horizon documents how and gives \ +you the tools, but provides no hosted agents.""" +dev = """A player runs their own agent locally with `horizon-ctl` and the MCP servers (docs/agent-control.md), `cargo \ +horizon-mod`, `horizon-port-pack` and the parity harnesses as the check. Horizon writes the guide \ +(website/docs/agents.md, docs/modpacks.md) and runs no agents, accounts or keys for players. The No AI \ +switch turns all of it off.""" +links = ["website/docs/agents.md", "docs/modpacks.md", "docs/agent-control.md"] + +[[item]] +id = "x-account" +title = "Horizon on X (coming soon)" +status = "next" +area = "community" +plain = "Coming soon: an official Horizon account on X, used to announce releases and the weekly summary." +dev = """Kira already posts the roadmap items marked `announce = true` when they ship, and the weekly summary, as \ +soon as the account's credentials exist (roadmap/README.md, 'The journal'). Waiting for the account itself.""" +links = ["roadmap/README.md"] + +[[item]] +id = "agents-app" +title = "Pipeline protection and a GitHub App for agents" +status = "next" +area = "project" +plain = """Safer releases: changes to the release machinery need the owner's review, and AI agents use a smaller \ +identity of their own instead of a personal account.""" +dev = """`.github/CODEOWNERS` lists the workflows, the release and signing tools, the update-checking code and the \ +installers (#1015); the branch protection that makes it count waits for the `horizon-agent` GitHub App, \ +which reads and writes code, pull requests and issues but can't change settings, workflows, secrets or \ +members, and whose tokens last an hour. It needs the owner to create the App and switch the protection on \ +(docs/security.md).""" +links = ["docs/security.md"] + +[[item]] +id = "signed-installers" +title = "Signed updates everywhere, installers included" +status = "next" +area = "project" +plain = """Every download Horizon makes is checked against the project's signature, including the first install with \ +install.sh and install.ps1.""" +dev = """Releases carry signed checksums, and the launcher and `horizon-server update` refuse anything unsigned \ +(#1011, #1014). Left: the first install through `install.sh` and `install.ps1` still trusts only the \ +checksum GitHub publishes, because verifying Ed25519 signatures needs OpenSSL 3, which macOS doesn't ship \ +(docs/security.md).""" +links = ["docs/security.md"] + # ---------------------------------------------------------------------------------------------------- Later [[item]] id = "ai-companion" title = "An optional AI teammate" status = "later" -area = "extras" -plain = """An AI teammate that plays beside you and that you can talk to. It is off unless you turn it on, and the \ -No AI switch hides it completely.""" -dev = """Draft pull requests: the companion (#1068), its safety modes and their evaluation (#1069), and a route \ -that finishes the game (#1070). Local models first; it ships only once the safety evaluation passes.""" +area = "ai" +plain = """An optional AI teammate you can talk to by voice. It plays beside you, helps or finishes the game with you, \ +runs on your computer first, and exists only if you turn it on. The No AI switch hides it.""" +dev = """Draft pull requests: the companion (#1068), its safety modes and their evaluation (#1069) and a route that \ +finishes the game (#1070). In the drafts: a companion that follows, fights, mines, crafts, sleeps and \ +respawns; chat and push-to-talk voice, with speech recognition and a choice of voices; presets (very strong, \ +helper, beat the game); a guard model with Clean, Kid and Salty modes; perception levels from Fair to \ +All-knowing; hardware detection with a simple mode and an Advanced tab; local models first, cloud keys \ +optional. The No AI switch is already in the game. Left: the speedrun route doesn't finish the game reliably \ +yet, voices for more languages, and the safety evaluation, which must pass before it ships.""" links = ["#1068", "#1069", "#1070"] [[item]] id = "new-versions" -title = "Following new Minecraft versions" +title = "Every Minecraft version, prepared automatically" status = "later" area = "gameplay" -plain = """When Mojang releases a new version of the game, Horizon follows it, and every system is checked against \ -the new version again.""" -dev = """The port pipeline detects a release, diffs its classes against 26.3 and writes a port report \ -(docs/version-ports.md); the harnesses then run against the new version.""" +plain = """Every past and future Minecraft version prepared for Horizon automatically. When Mojang releases one, \ +Horizon follows it, and every system is checked against it again.""" +dev = """The port pipeline (docs/version-ports.md, `tools/version_port.py`) detects a release, prepares it, diffs its \ +classes against the pinned 26.3, regenerates the data-driven tables, runs every parity harness on it and \ +writes a port report that becomes issues. The goal is as little human work as possible for any version, past \ +or future. Detection and the first slices are merged; the scheduled agent that runs it every day isn't \ +installed yet. Two versions in one build are out of scope.""" links = ["docs/version-ports.md"] +[[item]] +id = "exact-far-terrain" +title = "Exact far terrain from real chunks" +status = "later" +area = "performance" +plain = """Far terrain drawn from the world you really built and explored, caves and builds included, not only a \ +preview from the seed.""" +dev = """Keep a 3D mip store on disk built from real chunks, drawn by the GPU beside the seed preview for areas \ +nobody has visited. Written from scratch and measured before it is kept. Builds on Far Detail and the \ +far-terrain levels of detail already shipped; the open work on them is #718.""" +links = ["#718"] + +[[item]] +id = "older-jars" +title = "Older Fabric and Forge mods" +status = "later" +area = "modpacks" +plain = """Recognise mods built for versions before 26.1 in the mod browser, so they can be checked and carried over \ +like newer ones.""" +dev = """A mapping table from intermediary and SRG names so pre-26.1 jars can be classified, and a rename for mod ids \ +with dashes (`fabric-api`); the port step then treats them like newer jars (docs/mod-browser.md).""" +links = ["docs/mod-browser.md"] + +[[item]] +id = "modpack-own-packs" +title = "A modpack's own resource and shader packs" +status = "later" +area = "modpacks" +plain = """Importing a modpack also brings the resource packs and shader packs it ships with, where Horizon can use \ +them.""" +dev = """Porting a pack carries data, assets and worldgen today; the resource packs and shader packs bundled with it \ +are left out. Resource packs can be carried as they are; shader packs go through the converter \ +(docs/modpacks.md).""" +links = ["docs/modpacks.md"] + +[[item]] +id = "whole-modpacks" +title = "Whole modpacks, such as ATM10, rewritten" +status = "later" +area = "modpacks" +plain = """Rewriting a complete big modpack for Horizon so it runs far better than the original. Not a priority now: \ +the work above comes first.""" +dev = """The project's first goal for modpacks: support them ten times better than vanilla and port any of them. It \ +was set aside in favour of compatibility through the browser. What exists: ATM10's world generates and 42 of \ +its 496 mods are carried at least in part (docs/modpacks.md). It depends on 'Mods with code' and the native \ +mod API.""" +links = ["docs/modpacks.md"] + +[[item]] +id = "java-mod-bridge" +title = "Running Java mods that aren't rewritten" +status = "later" +area = "modpacks" +plain = """Run Java mods that nobody has rewritten for Horizon yet, through a bridge or a converter. Hard and not \ +started: the Convert button and native rewrites come first.""" +dev = """Earlier research (#527, closed) found that Java mods can't simply run, because they call Minecraft's own \ +classes. Options to look at again: a jar-to-WebAssembly backend, or a bridge for simple Fabric mods. \ +Whatever is chosen has to run inside the same sandbox and capability checks as native mods.""" +links = ["#527"] + +[[item]] +id = "alt-accounts" +title = "Ely.by and other sign-in providers" +status = "later" +area = "servers" +plain = """Sign in with other account providers, such as Ely.by or any Yggdrasil-compatible one, beside Microsoft.""" +dev = """Issue #1270 asks for Ely.by and Yggdrasil-compatible servers with an account selector in the launcher. It \ +builds on the Microsoft sign-in (docs/microsoft-account.md).""" +links = ["#1270", "docs/microsoft-account.md"] + +[[item]] +id = "friends-online-mode" +title = "Online-mode checks for Play with Friends" +status = "later" +area = "servers" +plain = """Friends who join through an invite code could prove who they are with their Microsoft account, as on \ +online-mode servers.""" +dev = """Play with Friends joins over an encrypted tunnel with an invite code (docs/play-with-friends.md, \ +'Identity'). Online-mode checks against Mojang's session servers can be added on top of the same tunnel; \ +they need Microsoft sign-in first, which is coming soon. Play with Friends stays self-hosted between \ +players, with no relay run by the project.""" +links = ["docs/play-with-friends.md"] + +[[item]] +id = "orbital-laser" +title = "Orbital Laser on the normal game" +status = "later" +area = "mods" +plain = """The community mod Orbital Laser, made for a fork of Horizon, running on the normal game and listed in the \ +registry as the showcase of the mod API.""" +dev = """A pull request on https://github.com/Stink-O/orbital-laser (MIT) once the input, ui, render and world \ +capabilities exist (see 'Screens, input and drawing for mods'): today the mod needs them through its \ +author's fork. Tested in the game, and listed in the registry.""" +links = ["https://github.com/Stink-O/orbital-laser"] + +[[item]] +id = "client-mod-natives" +title = "Native versions of popular client mods" +status = "later" +area = "mods" +plain = """Built-in versions of the client mods players ask for: block info on hover, a recipe viewer like JEI, a world \ +map and schematic overlays.""" +dev = """Done as extras: a mod list and menu, a minimap and a zoom (docs/extras.md). Left: block info tooltips, a \ +recipe viewer (a contributor offered to port JEI if it becomes a native feature), a world map and a \ +schematic overlay. They need the read-only interfaces `inventory-read`, `recipes`, `world-read` and `render` \ +in the mod API, or they go into the game directly.""" +links = ["docs/extras.md", "docs/mods.md"] + +[[item]] +id = "create-physics" +title = "Create-style physics as a native mod" +status = "later" +area = "mods" +plain = """Contraptions and physics like the Create and Aeronautics mods, as a mod for Horizon rather than part of the \ +base game.""" +dev = """Issue #805 (porting QOL mods) closed with this answer: it would come as a native Horizon mod on the mod API. \ +It needs server mods, entities and rendering first.""" +links = ["#805"] + +[[item]] +id = "proximity-voice" +title = "Proximity voice chat as a native mod" +status = "later" +area = "mods" +plain = """Hear nearby players talk, as with the voice chat mods of other Minecraft versions, as a Horizon mod.""" +dev = """Needs server mods, mod networking and an audio interface in the mod API before anything can be built \ +(docs/api/mods.md).""" +links = ["docs/api/mods.md"] + +[[item]] +id = "light-default-pack" +title = "A light default shader pack" +status = "later" +area = "rendering" +plain = """A small built-in look (tone mapping, colour grade, a cheap vignette) that costs almost nothing, for players \ +who want a nicer picture without a heavy pack.""" +dev = """An opt-in, composite-only pack on the native shader API, under about 0.3 ms at 1080p, shown on the Shader \ +Packs page. Pack option sliders and profiles, and a Failed row with the full error, belong with it.""" +links = ["docs/shaders.md"] + +[[item]] +id = "ray-tracing" +title = "Optional ray tracing" +status = "later" +area = "rendering" +plain = """An optional ray-tracing mode for graphics cards that support it (RTX and recent AMD): shadows, water \ +reflections and global lighting on top of the vanilla look.""" +dev = """A spike with ray-traced sun shadows, off by default, is a draft (#1180). Reflections, global illumination, \ +Apple M3 and newer if it is cheap, and a performance report per card come after.""" +links = ["#1180"] + +[[item]] +id = "hdr" +title = "HDR monitors" +status = "later" +area = "rendering" +plain = "Output for HDR monitors: brighter highlights and richer colour where your screen supports it." +dev = """Issue #1441. Needs a surface format and a tone-mapping path per backend, and tests on real HDR screens on \ +Windows, macOS and Linux.""" +links = ["#1441"] + +[[item]] +id = "msaa-fullscreen" +title = "MSAA, anisotropic filtering, exclusive fullscreen" +status = "later" +area = "rendering" +plain = """More graphics options: multisample anti-aliasing, anisotropic filtering and an exclusive fullscreen mode. \ +Today Horizon has FXAA and borderless fullscreen.""" +dev = """Not implemented (docs/graphics.md). Anisotropic filtering needs linear magnification in wgpu, which blurs \ +close blocks, so close-up sharpness has to be kept first; MSAA would sit beside FXAA; exclusive fullscreen \ +where the backend supports it.""" +links = ["docs/graphics.md"] + +[[item]] +id = "video-studio" +title = "A complete video studio inside the game" +status = "later" +area = "extras" +plain = """Record, replay and edit videos without leaving the game: record in a keypress, replay a session, cut and \ +finish the video. One of Horizon's original goals.""" +dev = """Three stages. Recording: capture gameplay in a keypress with an encoder thread and little frame-time cost. \ +Replay: a replay mode for recorded sessions. Editing: cuts, camera paths, titles and export. None of it \ +exists yet; the agent-control socket (docs/agent-control.md) already lets tools drive the game for scripted \ +shots.""" +links = ["docs/agent-control.md"] + +[[item]] +id = "controllers" +title = "Controllers: tested on real pads, and in the launcher" +status = "later" +area = "gameplay" +plain = """Make controller support solid: tested with real PlayStation and Xbox pads and a Steam Deck, and usable in \ +the launcher too.""" +dev = """The game side is merged (docs/controllers.md): pads read with gilrs, menus, rumble, glyphs, remapping and \ +DualSense adaptive triggers, lightbar and haptics, but not yet confirmed on real pads or a Steam Deck \ +(DualSense haptics stay behind a switch until then). Left: a test pass on real hardware, and the same focus \ +navigation in the GPUI launcher.""" +links = ["docs/controllers.md"] + +[[item]] +id = "debug-screen" +title = "The full debug screen" +status = "later" +area = "gameplay" +plain = """Vanilla's complete F3 screen: debug options, a profiler chart, ten-second captures and an atlas dump.""" +dev = """Today F3 shows position, chunk, light, biome and FPS (docs/native-client.md). Vanilla also has F3+F6 debug \ +options with profiles, F3+1 the profiler pie, F3+L a ten-second capture and F3+S the atlas dump (#562 asked \ +for the F3 editor).""" +links = ["#562", "docs/native-client.md"] + +[[item]] +id = "lean-installs" +title = "Lean installs that use less disk" +status = "later" +area = "launcher" +plain = """Smaller installs: Horizon keeps one shared copy of the prepared game files and removes what only the \ +preparation needed.""" +dev = """A shared resource store keyed by the preparer's digest, no prepare intermediates left behind, the current \ +and the previous version kept, and an automatic clean-up. Not started.""" + +[[item]] +id = "linux-arm64" +title = "Horizon on Linux ARM64" +status = "later" +area = "platforms" +plain = "Game and launcher builds for 64-bit ARM Linux computers." +dev = """Issue #1709: aarch64 builds of the game and the launcher in the release workflow, tested on a real ARM \ +machine with its Vulkan driver. The server build already drops audio and controllers \ +(`--no-default-features`).""" +links = ["#1709"] + +[[item]] +id = "opengl-fallback" +title = "OpenGL for old computers" +status = "later" +area = "platforms" +plain = "A way to play on old PCs and Linux machines that have no Vulkan or DirectX 12, through OpenGL." +dev = """Players on older hardware ask whether Vulkan is the only way. Horizon draws with wgpu, which has an OpenGL \ +backend; what is needed is to see which features hold up on it, how fast it runs, and a launcher switch like \ +the DirectX 12 fallback that already ships.""" +links = ["docs/graphics.md"] + +[[item]] +id = "phones-and-browser" +title = "Android, iOS and the browser" +status = "later" +area = "platforms" +plain = """Versions of Horizon for Android, iOS and web browsers. Nothing official yet; contributors have started the \ +first pieces.""" +dev = """No official build. Android was first closed as not planned, with touch controls, a mobile interface, \ +packaging and loading the player's own assets all missing; the roadmap now includes it, iOS and a \ +WebAssembly build for the browser as long-term goals. First pieces from contributors: `horizon-dualsense` \ +compiling on iOS (#1813) and helper-thread limits on Android (#1751). Preparing without Java (#1750) matters \ +here too. Horizon ships none of Mojang's files on any platform.""" +links = ["#1813", "#1751"] + +[[item]] +id = "video-series" +title = "A video series about Horizon" +status = "later" +area = "community" +plain = """A series of videos about how Horizon is made and what is new, with each upload announced on Discord.""" +dev = """Kira already shows new uploads in a read-only Discord channel (done). The series follows each stage of the \ +project: a script, edits, titles and a thumbnail per video.""" + # ---------------------------------------------------------------------------------------------------- Help wanted [[item]] diff --git a/website/data/roadmap.json b/website/data/roadmap.json index 1fcbdd773..4af2a460f 100644 --- a/website/data/roadmap.json +++ b/website/data/roadmap.json @@ -59,10 +59,22 @@ "id": "launcher", "name": "Launcher" }, + { + "id": "platforms", + "name": "Platforms" + }, { "id": "extras", "name": "Horizon extras" }, + { + "id": "ai", + "name": "AI and agents" + }, + { + "id": "community", + "name": "Community" + }, { "id": "project", "name": "Code and tools" @@ -255,6 +267,73 @@ ], "ongoing": true }, + { + "id": "native-shaders", + "title": "Horizon's own shader format and kit", + "status": "now", + "area": "rendering", + "plain": "Horizon's own way to make shader packs, with tools to write and test them. Every pack is checked before it runs, and the registry will list them.", + "dev": "`horizon-shader-api` (API 0.1, experimental): WGSL stages with typed inputs inside the renderer's passes, options as WGSL override constants, and the `horizon-shader new|check|pack` tool with templates and the example pack Golden Hour. Merged: the check every pack passes (#1820) and the tool, templates and guide (#1822). In review: the post stage running in the game with hot reload (#1861), the sky stage (#1863) and registry checks that use the game's own check (#1855). Left: the material, lighting and shadow stages, a last-good fallback on reload, a GPU-time overlay per stage and budgets (docs/shaders.md, docs/api/shaders.md). The Iris/OptiFine loader is a separate item, 'Shader packs, written again from scratch'.", + "links": [ + "#1820", + "#1822", + "#1861", + "#1863", + "#1855", + "docs/shaders.md", + "docs/api/shaders.md" + ] + }, + { + "id": "server-mods", + "title": "Mods that run on the server", + "status": "now", + "area": "mods", + "plain": "Mods that run on the server side of a world, so a dedicated server or a LAN world can have mods that react to players joining, chat and blocks being broken or placed, and that add commands.", + "dev": "API 0.3, experimental first (#1833, in review): the same sandboxed component runs on the server thread and the dedicated server, with world events (block broken or placed, entity spawned or killed, player joined or left), game rules and server commands with typed arguments. The world, item and entity interfaces come after (see 'Mods that read and change the world'). docs/api/mods.md, docs/mods.md.", + "links": [ + "#1833", + "docs/api/mods.md", + "docs/mods.md" + ] + }, + { + "id": "mod-screens", + "title": "Screens, input and drawing for mods", + "status": "now", + "area": "mods", + "plain": "Mods will be able to open their own screens and settings pages and react to keys and item use, so bigger mods, such as the Orbital Laser, can run on the normal game.", + "dev": "Capability `ui` is in review (#1864): mod-owned screens and a settings page under Options > Mods, built from vanilla's widgets. Still to design: `input` (an item's use, key binds, the crosshair target), `render` (a budgeted draw list on the native shader API) and `world` (changing blocks). Orbital Laser is the acceptance test: it needs all four today through its author's fork (see 'Orbital Laser on the normal game').", + "links": [ + "#1864", + "docs/mods.md" + ] + }, + { + "id": "modder-tools", + "title": "Tools for people who make mods", + "status": "now", + "area": "mods", + "plain": "Better tools for mod makers: a test runner that needs no game window, templates for each kind of mod, and clear promises about what keeps working from one version to the next.", + "dev": "`cargo horizon-mod test` runs a mod's test scripts against a fake game snapshot (#1816) and, for server mods, scripted server events (#1837); the content template and the guides are merged (#1859, #1810, docs/api/stability.md). Left: templates for hud, server and machine mods, typed wrappers in the SDK, a frozen 0.1 compatibility mod that every runtime must load, a CI check that fails when a stable reference entry is removed without a new major version, and the SDK on crates.io once the API is stable.", + "links": [ + "#1816", + "#1837", + "docs/api/stability.md", + "website/docs/test-a-mod.md" + ] + }, + { + "id": "pregen", + "title": "Pre-generate an area with /pregen", + "status": "now", + "area": "servers", + "plain": "A /pregen command that builds an area of the world ahead of time, so players don't wait for new terrain when they explore.", + "dev": "Generates, lights and saves an area before anyone walks there (#1805, a contributor's pull request, in review). Useful on servers, and it moves the generation cost away from the first visit; the generation speed-ups are under 'Steady frames at long render distances'.", + "links": [ + "#1805" + ] + }, { "id": "microsoft-sign-in", "title": "Microsoft sign-in (coming soon)", @@ -286,8 +365,8 @@ "title": "Fresh benchmarks against vanilla", "status": "next", "area": "performance", - "plain": "New public numbers comparing Horizon with vanilla on many kinds of computers, measured the same way every time, on the website's Benchmarks page.", - "dev": "Redo the comparison after the current performance work (#1073); reliability, coverage and presentation (#1346); a scene must count as loaded only once its first chunk arrives (#1749). Players' reports come in through `--benchmark` and `horizon-launcher data benchmark` (docs/benchmark.md).", + "plain": "New public numbers comparing Horizon with vanilla on many kinds of computers, measured the same way every time, with a history that shows what each release changed.", + "dev": "Redo the comparison after the current performance work (#1073); reliability, coverage and presentation (#1346); a scene must count as loaded only once its first chunk arrives (#1749). Next: a performance history across releases (an automatic comparison of each release with the last, on a timeline), more report fields (server TPS and MSPT, power, battery drain, temperature, throttling) and more hardware (a MacBook on battery, low-end laptops, community reports). Players' reports come in through `--benchmark` and `horizon-launcher data benchmark` (docs/benchmark.md).", "links": [ "#1073", "#1346", @@ -314,7 +393,7 @@ "id": "intel-mac", "title": "Horizon on Intel Macs", "status": "next", - "area": "launcher", + "area": "platforms", "plain": "Builds of the game and the launcher for Macs with an Intel processor.", "dev": "x86_64 game and launcher builds, picked per processor (#1171). Before it ships it needs a run on a real Intel Mac: Rosetta on Apple Silicon only proves the CPU code. Requests: #1161, #1792.", "links": [ @@ -323,13 +402,328 @@ "#1792" ] }, + { + "id": "world-entry", + "title": "Faster world creation and entry on small CPUs", + "status": "next", + "area": "performance", + "plain": "Creating a world and joining it should be quick even on a four-thread laptop, where a fresh world at 32 chunks can make you wait minutes for the first terrain.", + "dev": "Cut the per-column generation cost (noise sampling, surface rules, aquifers) and the lock convoy on structure starts, and leave one core free for the rest of the game as vanilla does (#1760 and #1778, in review, are listed under 'Steady frames at long render distances'). Still to profile on small CPUs: the first seconds of a fresh world deliver no chunk, and fluids remesh for about a minute after loading. Measured with `tools/perf_bench.py` (docs/benchmark.md, docs/profiling.md).", + "links": [ + "docs/benchmark.md", + "docs/profiling.md" + ] + }, + { + "id": "server-tick", + "title": "Servers that hold 20 TPS with 50 to 100 players", + "status": "next", + "area": "performance", + "plain": "Servers that keep 20 ticks per second with 50 to 100 players. Today Horizon holds it up to 50 bots and drops to about 10 at 100.", + "dev": "Measured against vanilla on equal work (docs/server.md, Scale): Horizon is ahead with few players and when exploring, but vanilla's tick is shorter with 25 to 50 players near spawn. Ideas on the list: send entity changes instead of rebuilding every entity each tick (about 15 percent), fewer memory reads in random ticks and collision scans, cheaper item collision checks, mob AI and natural spawning. The bot benchmark (`horizon-bots`) is the proof.", + "links": [ + "docs/server.md" + ] + }, + { + "id": "low-end-mode", + "title": "Low-end computers: Steam Deck, old Macs, 8 GB", + "status": "next", + "area": "performance", + "plain": "Horizon should run well on small machines: integrated graphics, the Steam Deck, older Macs and 8 GB of memory, with a battery mode that saves power on laptops.", + "dev": "Partly done: an opt-in 60 FPS cap with fewer workers on battery, Far Detail Reduced for integrated GPUs and animated textures on the GPU (both shipped). Left: measured targets and fixes per class of machine (memory use, worker counts, startup) on a Steam Deck, old Intel and Apple Silicon Macs and 8 GB computers, reported on the Benchmarks page.", + "links": [ + "docs/benchmark.md", + "website/docs/performance.md" + ] + }, + { + "id": "upscaling", + "title": "Native upscaling", + "status": "next", + "area": "performance", + "plain": "Draw the game at a lower resolution and sharpen it to your screen's size, in the way of FSR or DLSS, to gain frames on weaker graphics cards. An optional graphics setting.", + "dev": "A render-scale setting below the window size with an upscaler of our own that runs on every wgpu backend. Proof: frame time against native resolution on integrated GPUs, and screenshots for image quality. Issue #1403.", + "links": [ + "#1403" + ] + }, + { + "id": "targeted-optimisations", + "title": "A batch of targeted optimisations", + "status": "next", + "area": "performance", + "plain": "Several smaller speed-ups, each measured: idle block entities that sleep, cheaper particles, faster structure placement, high-rate mice and very large texture packs.", + "dev": "Block entities and hoppers that skip work when idle (the hopper item scan is already skipped when no item is near); a particle frustum test first (also a parity gap), then particle quads built on the GPU; jigsaw shortcuts that skip blocked connectors and pool elements known not to fit, with layouts unchanged (trial chambers and ancient cities first); batched raw mouse input on Windows for 8000 Hz mice; a better atlas packer for huge resource packs (a 64x pack builds a 704 MiB atlas for 452 MiB of textures); sign-heavy scenes (#1461). Profile first, keep what pays, show before and after numbers.", + "links": [ + "#1461", + "docs/profiling.md" + ] + }, + { + "id": "convert-to-rust", + "title": "A Convert to Rust button for what isn't rewritten yet", + "status": "next", + "area": "modpacks", + "plain": "When a mod, shader pack or modpack hasn't been rewritten for Horizon, one button converts what can be converted, so you don't have to wait for the rewrite.", + "dev": "Generalise the mod browser's Port step (`horizon-port-pack`: the data, assets, worldgen and block and item definitions of Java mods, .mrpack and CurseForge zips) to every kind of content: code mods where the API allows it (see 'Mods with code') and shader packs, converting an Iris/OptiFine pack into Horizon's format. The browser says what works before the install. Java code itself does not run (docs/modpacks.md, docs/mod-browser.md).", + "links": [ + "docs/modpacks.md", + "docs/mod-browser.md" + ] + }, + { + "id": "code-mods", + "title": "Mods with code: machines, magic, mobs", + "status": "next", + "area": "modpacks", + "plain": "Making mods that add machines, magic and mobs work in Horizon, in steps, starting with machines. They are most of what a big modpack is made of.", + "dev": "333 of the 496 mods in a big pack (ATM10) contain code. Issue #1854 ranks the API gaps (ticking block entities, networking, rendering, menus, inventories) that unlock them in tiers, with machines first as the recommended path and a furnace ported tick for tick as proof. The first API step is a draft (#1189, see 'Machines and blocks with inventories'). The path isn't started yet (docs/modpacks.md).", + "links": [ + "#1854", + "docs/modpacks.md" + ] + }, + { + "id": "datapack-browser", + "title": "Data packs in the mod browser", + "status": "next", + "area": "modpacks", + "plain": "Search and install data packs from the launcher's mod browser, for the world you choose, as you do mods and resource packs.", + "dev": "A Datapacks tab beside Mods and Resource packs: install into a chosen world's datapacks folder, with the same checks and 'works in Horizon' notes as the other kinds (docs/mod-browser.md).", + "links": [ + "docs/mod-browser.md" + ] + }, + { + "id": "prepare-without-java", + "title": "Prepare a version without Java", + "status": "next", + "area": "servers", + "plain": "Set up Horizon, a server first, without Java on your computer: published tables are combined with your own copy of the game.", + "dev": "Today `horizon-prepare` runs Mojang's exporters with Java. Issue #1750 proposes publishing the computed version tables once from CI and assembling the rest locally from the player's own jar and assets, provided the tables hold no Mojang content; a contributor offered a pull request, and it would help phones too. A server-only prepare from `server.jar` alone is the first step (docs/server.md).", + "links": [ + "#1750", + "docs/server.md" + ] + }, + { + "id": "server-admin", + "title": "Server console, bans and a full admin API", + "status": "next", + "area": "servers", + "plain": "Manage a server from a console, ban and pardon players, and drive it from other tools through a full admin API and an MCP server for AI agents.", + "dev": "`horizon-server console` (a text interface), `ban`, `pardon`, `ban-ip` and `mcp` over stdio. Admin API additions: the player list, kick, ban, op and whitelist, an SSE stream of logs and events, a text metrics endpoint, config GET and PATCH, backup delete and download. The panel's later features (bans, worlds, server.properties import, logs and crash reports, modpacks) and vanilla's management protocol (the JSON-RPC over WebSocket that the `management-server-*` keys configure), so vanilla's tools can manage a Horizon server. docs/server.md, docs/api/server.md.", + "links": [ + "docs/server.md", + "docs/api/server.md" + ] + }, + { + "id": "hosting-polish", + "title": "Hosting polish: Windows service, safe updates", + "status": "next", + "area": "servers", + "plain": "Smoother hosting: a real Windows service, a backup before every update, backups that keep the world and each player's state, and a changed view distance or message without a restart.", + "dev": "A Windows service wrapper instead of the logon scheduled task; `backups.before_update`; hot reload of config for view distance and the message of the day (needs engine support); backups that keep world and player state (#1808, a contributor's draft, in review). docs/hosting.md, docs/server.md.", + "links": [ + "#1808", + "docs/hosting.md" + ] + }, + { + "id": "tailscale-hosting", + "title": "Guided hosting with Tailscale and an invite button", + "status": "next", + "area": "servers", + "plain": "In the launcher, a guide and a button to host a server and invite friends over Tailscale, so nobody has to open ports on a router.", + "dev": "On the Servers page (create, start, back up, update and invite are shipped) add a Tailscale setup that explains and checks each step, and an invite button that hands friends what they need to join over the tailnet. Buttons first, no docs needed, with the details in an Advanced section. Remote access over Tailscale is already described in docs/server.md.", + "links": [ + "docs/hosting.md", + "docs/server.md" + ] + }, + { + "id": "docker-image", + "title": "An official Docker image for servers", + "status": "next", + "area": "servers", + "plain": "An official image to run a Horizon dedicated server in Docker with one command, your own copy of the game files mounted in.", + "dev": "The repository has the Dockerfile (`packaging/server/Dockerfile`: a multi-stage build on Debian bookworm-slim, user `horizon`, volume `/data`, port 25565, game files mounted read-only, `HORIZON_EULA` and friends answering the first-start questions), and anyone can build it (docs/hosting.md, section 9). Left: publish the image to a public registry from the release workflow, tagged per release, so it follows the same checks, and update the docs and the launcher. The image never holds Mojang's files.", + "links": [ + "docs/hosting.md", + "docs/server.md" + ] + }, + { + "id": "one-single-player-path", + "title": "One single-player path: remove the in-frame world", + "status": "next", + "area": "gameplay", + "plain": "Single player will have one way to run, the server thread, so every fix works everywhere and there are fewer bugs. The older in-frame mode goes once everything passes without it.", + "dev": "`--in-frame` (also `--no-server-thread` and `HORIZON_SERVER_THREAD=0`) still ticks the world inside the window's `Game`, and most harnesses test that path. Delete it once `lan_e2e --server-thread` and the parity harnesses pass on the server thread, which also halves the bug surface. See also 'Single player as a server' and docs/server-levels.md.", + "links": [ + "docs/server-levels.md" + ] + }, + { + "id": "villages-combat-parity", + "title": "Villages and combat, exactly as in vanilla", + "status": "next", + "area": "gameplay", + "plain": "Villages and fights behaving as in vanilla: iron golems defending villages, villagers remembering their homes, mace smashes and knockback.", + "dev": "Villages: iron golem village goals, villager memories across saves, villager age, job-site claiming on dedicated servers, flower offers (#1639 is one report). Combat: the mobs' `attack_knockback` attribute, mace smash logic, the hit knockback source on the host, the fire burnout biome tag. Each is checked against vanilla by a harness (docs/rust-parity.md).", + "links": [ + "#1639", + "docs/rust-parity.md" + ] + }, + { + "id": "audio-device", + "title": "Choose the audio output device", + "status": "next", + "area": "gameplay", + "plain": "Pick the speakers or headphones Horizon plays through, in Music & Sounds.", + "dev": "An output device list in Music & Sounds, falling back to the system default. #1035 started it: a player on an older Windows computer had no sound at all, and a device choice is how such cases are found and fixed.", + "links": [ + "#1035" + ] + }, + { + "id": "mod-world-items", + "title": "Mods that read and change the world", + "status": "next", + "area": "mods", + "plain": "Mods that can see and change blocks, read inventories and items, and find and affect entities, all through the server so every player sees the same thing.", + "dev": "After server mods: read blocks, states and biomes in loaded chunks and change them through the server's block log, so clients and the parity harnesses see vanilla's updates; the player's inventory, held item, open container and item stacks with components; entity queries near a position, a safe subset of entity data and data-driven mod entity types. Snapshots and batched calls per tick, never per-block host calls in hot paths. docs/api/mods.md ('Not there yet').", + "links": [ + "docs/api/mods.md", + "docs/modpacks.md" + ] + }, + { + "id": "machines-api", + "title": "Machines and blocks with inventories (API 0.4)", + "status": "next", + "area": "mods", + "plain": "Mods that add machines: blocks that tick, hold items and have their own menus. This unlocks the technology mods of big modpacks.", + "dev": "Block entities and menus: ticking machines with slots, server menu logic and the menu protocol to clients, energy, item and fluid transfer, data attachments, accessory slots and custom recipe types, with a typed client and server channel and typed config beside them. A draft (#1189) proves a furnace ported tick for tick and holds the ATM10 code-mods plan; the design is redone through the API policy before it merges (docs/modpacks.md, docs/api/stability.md).", + "links": [ + "#1189", + "docs/modpacks.md" + ] + }, + { + "id": "mod-languages", + "title": "Lua and other languages for mods", + "status": "next", + "area": "mods", + "plain": "Write mods as Lua scripts, in the style of KubeJS, with no compiler, and later in other languages that pass the tests. Every language runs in the same sandbox.", + "dev": "Lua first: Horizon ships a Lua interpreter that is itself a WebAssembly mod, and a script mod's manifest names it and the capabilities it uses, so a script can do exactly what a compiled mod with the same manifest can, with hot reload of `main.lua`. Then C, C++, Zig, Go (TinyGo), AssemblyScript and JavaScript or TypeScript are measured against Rust on the same tick-heavy example, and each is supported only with a template, an example and a CI test. docs/api/mods.md ('Ways in').", + "links": [ + "docs/api/mods.md" + ] + }, + { + "id": "shader-pack-examples", + "title": "Well-known shader looks, rewritten for Horizon", + "status": "next", + "area": "rendering", + "plain": "Popular shader packs rewritten in Horizon's format, so good packs exist from the start. Only packs whose licence is open source and compatible with ours are rewritten, never anything else.", + "dev": "The first candidates are Photon, Chocapic13 and MakeUp Ultra Fast, each only after its licence is read and recorded: a pack is rewritten only if it is open source and compatible with Horizon's licence, and the rewrite is written from the shader format's documentation and the look on screen, not from anyone's code. Today only the Golden Hour example exists (`crates/horizon-shader-api/packs/golden_hour`).", + "links": [ + "docs/shaders.md", + "crates/horizon-shader-api/packs/golden_hour" + ] + }, + { + "id": "beta-label", + "title": "A visible beta label", + "status": "next", + "area": "launcher", + "plain": "A clear 'beta' label in the launcher and the game, so nobody is surprised that Horizon is still being built.", + "dev": "Show Beta in the launcher's header and about page, on the game's title screen and on the website, beside the version number (docs/versioning.md).", + "links": [ + "docs/versioning.md" + ] + }, + { + "id": "wayland-polish", + "title": "Wayland and Hyprland polish", + "status": "next", + "area": "platforms", + "plain": "Fixes for Linux desktops that use Wayland and Hyprland: fullscreen, the clipboard, the GUI scale after a restart and the like.", + "dev": "Open: the GUI Scale is clamped to 2 after a restart under Hyprland, while XWayland keeps it (#1866). Earlier reports covered fullscreen dropping on menu clicks, the clipboard and a white overlay (#1007, closed). Checked on a real Wayland session.", + "links": [ + "#1866" + ] + }, + { + "id": "agent-control-protocol", + "title": "A versioned agent-control protocol", + "status": "next", + "area": "ai", + "plain": "A stable, documented way for AI agents and scripts to drive the game and the launcher, with versions and an event stream so tools keep working as Horizon changes.", + "dev": "`hello` answers with the API version, the release and the features; `horizon-ctl --api`; MCP serverInfo; reply schemas generated into the reference; a `test.` namespace for hooks; transcript compatibility tests; `subscribe` as a stream of events on one connection; Horizon option files by key and saved-server commands. Agent control itself (`horizon-ctl`, both MCP servers, the No AI switch) is done: docs/agent-control.md, docs/api/agent-control.md.", + "links": [ + "docs/agent-control.md", + "docs/api/agent-control.md", + "docs/api/stability.md" + ] + }, + { + "id": "own-agents-porting", + "title": "Port mods with your own AI agent", + "status": "next", + "area": "ai", + "plain": "You can port a mod to Horizon with your own AI agent on your own computer. Horizon documents how and gives you the tools, but provides no hosted agents.", + "dev": "A player runs their own agent locally with `horizon-ctl` and the MCP servers (docs/agent-control.md), `cargo horizon-mod`, `horizon-port-pack` and the parity harnesses as the check. Horizon writes the guide (website/docs/agents.md, docs/modpacks.md) and runs no agents, accounts or keys for players. The No AI switch turns all of it off.", + "links": [ + "website/docs/agents.md", + "docs/modpacks.md", + "docs/agent-control.md" + ] + }, + { + "id": "x-account", + "title": "Horizon on X (coming soon)", + "status": "next", + "area": "community", + "plain": "Coming soon: an official Horizon account on X, used to announce releases and the weekly summary.", + "dev": "Kira already posts the roadmap items marked `announce = true` when they ship, and the weekly summary, as soon as the account's credentials exist (roadmap/README.md, 'The journal'). Waiting for the account itself.", + "links": [ + "roadmap/README.md" + ] + }, + { + "id": "agents-app", + "title": "Pipeline protection and a GitHub App for agents", + "status": "next", + "area": "project", + "plain": "Safer releases: changes to the release machinery need the owner's review, and AI agents use a smaller identity of their own instead of a personal account.", + "dev": "`.github/CODEOWNERS` lists the workflows, the release and signing tools, the update-checking code and the installers (#1015); the branch protection that makes it count waits for the `horizon-agent` GitHub App, which reads and writes code, pull requests and issues but can't change settings, workflows, secrets or members, and whose tokens last an hour. It needs the owner to create the App and switch the protection on (docs/security.md).", + "links": [ + "docs/security.md" + ] + }, + { + "id": "signed-installers", + "title": "Signed updates everywhere, installers included", + "status": "next", + "area": "project", + "plain": "Every download Horizon makes is checked against the project's signature, including the first install with install.sh and install.ps1.", + "dev": "Releases carry signed checksums, and the launcher and `horizon-server update` refuse anything unsigned (#1011, #1014). Left: the first install through `install.sh` and `install.ps1` still trusts only the checksum GitHub publishes, because verifying Ed25519 signatures needs OpenSSL 3, which macOS doesn't ship (docs/security.md).", + "links": [ + "docs/security.md" + ] + }, { "id": "ai-companion", "title": "An optional AI teammate", "status": "later", - "area": "extras", - "plain": "An AI teammate that plays beside you and that you can talk to. It is off unless you turn it on, and the No AI switch hides it completely.", - "dev": "Draft pull requests: the companion (#1068), its safety modes and their evaluation (#1069), and a route that finishes the game (#1070). Local models first; it ships only once the safety evaluation passes.", + "area": "ai", + "plain": "An optional AI teammate you can talk to by voice. It plays beside you, helps or finishes the game with you, runs on your computer first, and exists only if you turn it on. The No AI switch hides it.", + "dev": "Draft pull requests: the companion (#1068), its safety modes and their evaluation (#1069) and a route that finishes the game (#1070). In the drafts: a companion that follows, fights, mines, crafts, sleeps and respawns; chat and push-to-talk voice, with speech recognition and a choice of voices; presets (very strong, helper, beat the game); a guard model with Clean, Kid and Salty modes; perception levels from Fair to All-knowing; hardware detection with a simple mode and an Advanced tab; local models first, cloud keys optional. The No AI switch is already in the game. Left: the speedrun route doesn't finish the game reliably yet, voices for more languages, and the safety evaluation, which must pass before it ships.", "links": [ "#1068", "#1069", @@ -338,15 +732,266 @@ }, { "id": "new-versions", - "title": "Following new Minecraft versions", + "title": "Every Minecraft version, prepared automatically", "status": "later", "area": "gameplay", - "plain": "When Mojang releases a new version of the game, Horizon follows it, and every system is checked against the new version again.", - "dev": "The port pipeline detects a release, diffs its classes against 26.3 and writes a port report (docs/version-ports.md); the harnesses then run against the new version.", + "plain": "Every past and future Minecraft version prepared for Horizon automatically. When Mojang releases one, Horizon follows it, and every system is checked against it again.", + "dev": "The port pipeline (docs/version-ports.md, `tools/version_port.py`) detects a release, prepares it, diffs its classes against the pinned 26.3, regenerates the data-driven tables, runs every parity harness on it and writes a port report that becomes issues. The goal is as little human work as possible for any version, past or future. Detection and the first slices are merged; the scheduled agent that runs it every day isn't installed yet. Two versions in one build are out of scope.", "links": [ "docs/version-ports.md" ] }, + { + "id": "exact-far-terrain", + "title": "Exact far terrain from real chunks", + "status": "later", + "area": "performance", + "plain": "Far terrain drawn from the world you really built and explored, caves and builds included, not only a preview from the seed.", + "dev": "Keep a 3D mip store on disk built from real chunks, drawn by the GPU beside the seed preview for areas nobody has visited. Written from scratch and measured before it is kept. Builds on Far Detail and the far-terrain levels of detail already shipped; the open work on them is #718.", + "links": [ + "#718" + ] + }, + { + "id": "older-jars", + "title": "Older Fabric and Forge mods", + "status": "later", + "area": "modpacks", + "plain": "Recognise mods built for versions before 26.1 in the mod browser, so they can be checked and carried over like newer ones.", + "dev": "A mapping table from intermediary and SRG names so pre-26.1 jars can be classified, and a rename for mod ids with dashes (`fabric-api`); the port step then treats them like newer jars (docs/mod-browser.md).", + "links": [ + "docs/mod-browser.md" + ] + }, + { + "id": "modpack-own-packs", + "title": "A modpack's own resource and shader packs", + "status": "later", + "area": "modpacks", + "plain": "Importing a modpack also brings the resource packs and shader packs it ships with, where Horizon can use them.", + "dev": "Porting a pack carries data, assets and worldgen today; the resource packs and shader packs bundled with it are left out. Resource packs can be carried as they are; shader packs go through the converter (docs/modpacks.md).", + "links": [ + "docs/modpacks.md" + ] + }, + { + "id": "whole-modpacks", + "title": "Whole modpacks, such as ATM10, rewritten", + "status": "later", + "area": "modpacks", + "plain": "Rewriting a complete big modpack for Horizon so it runs far better than the original. Not a priority now: the work above comes first.", + "dev": "The project's first goal for modpacks: support them ten times better than vanilla and port any of them. It was set aside in favour of compatibility through the browser. What exists: ATM10's world generates and 42 of its 496 mods are carried at least in part (docs/modpacks.md). It depends on 'Mods with code' and the native mod API.", + "links": [ + "docs/modpacks.md" + ] + }, + { + "id": "java-mod-bridge", + "title": "Running Java mods that aren't rewritten", + "status": "later", + "area": "modpacks", + "plain": "Run Java mods that nobody has rewritten for Horizon yet, through a bridge or a converter. Hard and not started: the Convert button and native rewrites come first.", + "dev": "Earlier research (#527, closed) found that Java mods can't simply run, because they call Minecraft's own classes. Options to look at again: a jar-to-WebAssembly backend, or a bridge for simple Fabric mods. Whatever is chosen has to run inside the same sandbox and capability checks as native mods.", + "links": [ + "#527" + ] + }, + { + "id": "alt-accounts", + "title": "Ely.by and other sign-in providers", + "status": "later", + "area": "servers", + "plain": "Sign in with other account providers, such as Ely.by or any Yggdrasil-compatible one, beside Microsoft.", + "dev": "Issue #1270 asks for Ely.by and Yggdrasil-compatible servers with an account selector in the launcher. It builds on the Microsoft sign-in (docs/microsoft-account.md).", + "links": [ + "#1270", + "docs/microsoft-account.md" + ] + }, + { + "id": "friends-online-mode", + "title": "Online-mode checks for Play with Friends", + "status": "later", + "area": "servers", + "plain": "Friends who join through an invite code could prove who they are with their Microsoft account, as on online-mode servers.", + "dev": "Play with Friends joins over an encrypted tunnel with an invite code (docs/play-with-friends.md, 'Identity'). Online-mode checks against Mojang's session servers can be added on top of the same tunnel; they need Microsoft sign-in first, which is coming soon. Play with Friends stays self-hosted between players, with no relay run by the project.", + "links": [ + "docs/play-with-friends.md" + ] + }, + { + "id": "orbital-laser", + "title": "Orbital Laser on the normal game", + "status": "later", + "area": "mods", + "plain": "The community mod Orbital Laser, made for a fork of Horizon, running on the normal game and listed in the registry as the showcase of the mod API.", + "dev": "A pull request on https://github.com/Stink-O/orbital-laser (MIT) once the input, ui, render and world capabilities exist (see 'Screens, input and drawing for mods'): today the mod needs them through its author's fork. Tested in the game, and listed in the registry.", + "links": [ + "https://github.com/Stink-O/orbital-laser" + ] + }, + { + "id": "client-mod-natives", + "title": "Native versions of popular client mods", + "status": "later", + "area": "mods", + "plain": "Built-in versions of the client mods players ask for: block info on hover, a recipe viewer like JEI, a world map and schematic overlays.", + "dev": "Done as extras: a mod list and menu, a minimap and a zoom (docs/extras.md). Left: block info tooltips, a recipe viewer (a contributor offered to port JEI if it becomes a native feature), a world map and a schematic overlay. They need the read-only interfaces `inventory-read`, `recipes`, `world-read` and `render` in the mod API, or they go into the game directly.", + "links": [ + "docs/extras.md", + "docs/mods.md" + ] + }, + { + "id": "create-physics", + "title": "Create-style physics as a native mod", + "status": "later", + "area": "mods", + "plain": "Contraptions and physics like the Create and Aeronautics mods, as a mod for Horizon rather than part of the base game.", + "dev": "Issue #805 (porting QOL mods) closed with this answer: it would come as a native Horizon mod on the mod API. It needs server mods, entities and rendering first.", + "links": [ + "#805" + ] + }, + { + "id": "proximity-voice", + "title": "Proximity voice chat as a native mod", + "status": "later", + "area": "mods", + "plain": "Hear nearby players talk, as with the voice chat mods of other Minecraft versions, as a Horizon mod.", + "dev": "Needs server mods, mod networking and an audio interface in the mod API before anything can be built (docs/api/mods.md).", + "links": [ + "docs/api/mods.md" + ] + }, + { + "id": "light-default-pack", + "title": "A light default shader pack", + "status": "later", + "area": "rendering", + "plain": "A small built-in look (tone mapping, colour grade, a cheap vignette) that costs almost nothing, for players who want a nicer picture without a heavy pack.", + "dev": "An opt-in, composite-only pack on the native shader API, under about 0.3 ms at 1080p, shown on the Shader Packs page. Pack option sliders and profiles, and a Failed row with the full error, belong with it.", + "links": [ + "docs/shaders.md" + ] + }, + { + "id": "ray-tracing", + "title": "Optional ray tracing", + "status": "later", + "area": "rendering", + "plain": "An optional ray-tracing mode for graphics cards that support it (RTX and recent AMD): shadows, water reflections and global lighting on top of the vanilla look.", + "dev": "A spike with ray-traced sun shadows, off by default, is a draft (#1180). Reflections, global illumination, Apple M3 and newer if it is cheap, and a performance report per card come after.", + "links": [ + "#1180" + ] + }, + { + "id": "hdr", + "title": "HDR monitors", + "status": "later", + "area": "rendering", + "plain": "Output for HDR monitors: brighter highlights and richer colour where your screen supports it.", + "dev": "Issue #1441. Needs a surface format and a tone-mapping path per backend, and tests on real HDR screens on Windows, macOS and Linux.", + "links": [ + "#1441" + ] + }, + { + "id": "msaa-fullscreen", + "title": "MSAA, anisotropic filtering, exclusive fullscreen", + "status": "later", + "area": "rendering", + "plain": "More graphics options: multisample anti-aliasing, anisotropic filtering and an exclusive fullscreen mode. Today Horizon has FXAA and borderless fullscreen.", + "dev": "Not implemented (docs/graphics.md). Anisotropic filtering needs linear magnification in wgpu, which blurs close blocks, so close-up sharpness has to be kept first; MSAA would sit beside FXAA; exclusive fullscreen where the backend supports it.", + "links": [ + "docs/graphics.md" + ] + }, + { + "id": "video-studio", + "title": "A complete video studio inside the game", + "status": "later", + "area": "extras", + "plain": "Record, replay and edit videos without leaving the game: record in a keypress, replay a session, cut and finish the video. One of Horizon's original goals.", + "dev": "Three stages. Recording: capture gameplay in a keypress with an encoder thread and little frame-time cost. Replay: a replay mode for recorded sessions. Editing: cuts, camera paths, titles and export. None of it exists yet; the agent-control socket (docs/agent-control.md) already lets tools drive the game for scripted shots.", + "links": [ + "docs/agent-control.md" + ] + }, + { + "id": "controllers", + "title": "Controllers: tested on real pads, and in the launcher", + "status": "later", + "area": "gameplay", + "plain": "Make controller support solid: tested with real PlayStation and Xbox pads and a Steam Deck, and usable in the launcher too.", + "dev": "The game side is merged (docs/controllers.md): pads read with gilrs, menus, rumble, glyphs, remapping and DualSense adaptive triggers, lightbar and haptics, but not yet confirmed on real pads or a Steam Deck (DualSense haptics stay behind a switch until then). Left: a test pass on real hardware, and the same focus navigation in the GPUI launcher.", + "links": [ + "docs/controllers.md" + ] + }, + { + "id": "debug-screen", + "title": "The full debug screen", + "status": "later", + "area": "gameplay", + "plain": "Vanilla's complete F3 screen: debug options, a profiler chart, ten-second captures and an atlas dump.", + "dev": "Today F3 shows position, chunk, light, biome and FPS (docs/native-client.md). Vanilla also has F3+F6 debug options with profiles, F3+1 the profiler pie, F3+L a ten-second capture and F3+S the atlas dump (#562 asked for the F3 editor).", + "links": [ + "#562", + "docs/native-client.md" + ] + }, + { + "id": "lean-installs", + "title": "Lean installs that use less disk", + "status": "later", + "area": "launcher", + "plain": "Smaller installs: Horizon keeps one shared copy of the prepared game files and removes what only the preparation needed.", + "dev": "A shared resource store keyed by the preparer's digest, no prepare intermediates left behind, the current and the previous version kept, and an automatic clean-up. Not started." + }, + { + "id": "linux-arm64", + "title": "Horizon on Linux ARM64", + "status": "later", + "area": "platforms", + "plain": "Game and launcher builds for 64-bit ARM Linux computers.", + "dev": "Issue #1709: aarch64 builds of the game and the launcher in the release workflow, tested on a real ARM machine with its Vulkan driver. The server build already drops audio and controllers (`--no-default-features`).", + "links": [ + "#1709" + ] + }, + { + "id": "opengl-fallback", + "title": "OpenGL for old computers", + "status": "later", + "area": "platforms", + "plain": "A way to play on old PCs and Linux machines that have no Vulkan or DirectX 12, through OpenGL.", + "dev": "Players on older hardware ask whether Vulkan is the only way. Horizon draws with wgpu, which has an OpenGL backend; what is needed is to see which features hold up on it, how fast it runs, and a launcher switch like the DirectX 12 fallback that already ships.", + "links": [ + "docs/graphics.md" + ] + }, + { + "id": "phones-and-browser", + "title": "Android, iOS and the browser", + "status": "later", + "area": "platforms", + "plain": "Versions of Horizon for Android, iOS and web browsers. Nothing official yet; contributors have started the first pieces.", + "dev": "No official build. Android was first closed as not planned, with touch controls, a mobile interface, packaging and loading the player's own assets all missing; the roadmap now includes it, iOS and a WebAssembly build for the browser as long-term goals. First pieces from contributors: `horizon-dualsense` compiling on iOS (#1813) and helper-thread limits on Android (#1751). Preparing without Java (#1750) matters here too. Horizon ships none of Mojang's files on any platform.", + "links": [ + "#1813", + "#1751" + ] + }, + { + "id": "video-series", + "title": "A video series about Horizon", + "status": "later", + "area": "community", + "plain": "A series of videos about how Horizon is made and what is new, with each upload announced on Discord.", + "dev": "Kira already shows new uploads in a read-only Discord channel (done). The series follows each stage of the project: a script, edits, titles and a thumbnail per video." + }, { "id": "benchmark-your-pc", "title": "Benchmark your computer", From 72b9daabf01ee530be04b6dba5f3f58fe9ef2410 Mon Sep 17 00:00:00 2001 From: socksprox Date: Sun, 25 Oct 2026 18:24:46 +0100 Subject: [PATCH 134/238] =?UTF-8?q?Resolve=20legacy=20=C2=A7=20codes=20whe?= =?UTF-8?q?re=20the=20font=20draws=20text?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Name tags and styled HUD text drew a span's § codes as glyphs instead of restyling what follows, as StringDecomposer.iterateFormatted does. A team prefix like "§c" showed literally over players' heads, and a resource pack's styled menu.disconnect kept its codes on the leave-server button. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- crates/horizon-client/src/font.rs | 5 ++++- crates/horizon-client/src/game/names.rs | 12 ++++++++---- crates/horizon-client/src/hud.rs | 3 +++ crates/horizon-client/src/text.rs | 25 +++++++++++++++++++++++++ crates/horizon-client/src/ui.rs | 12 ++++++++++-- 5 files changed, 50 insertions(+), 7 deletions(-) diff --git a/crates/horizon-client/src/font.rs b/crates/horizon-client/src/font.rs index 801a892d5..1903cab7e 100644 --- a/crates/horizon-client/src/font.rs +++ b/crates/horizon-client/src/font.rs @@ -891,8 +891,11 @@ impl Font { } s.chars().map(|c| self.glyph(c).advance).sum::().ceil() } + /// Font.width of a component: `§` codes in the spans are formatting, + /// not glyphs. pub fn width_text(&self, t: &Text) -> f32 { - t.chars() + t.resolved() + .chars() .map(|(c, s)| self.advance(c, s)) .sum::() .ceil() diff --git a/crates/horizon-client/src/game/names.rs b/crates/horizon-client/src/game/names.rs index be829f1bb..7655fb489 100644 --- a/crates/horizon-client/src/game/names.rs +++ b/crates/horizon-client/src/game/names.rs @@ -151,10 +151,13 @@ impl Game<'_> { .clone() .unwrap_or_else(|| Text::literal(&name)) }; + // StringDecomposer.iterateFormatted: `§` codes a server leaves + // inside the team prefix or the name itself apply at draw time. let shown = match team { Some(t) => t.formatted_name(member), None => member, - }; + } + .resolve_legacy(); let mut lines = Vec::new(); // EntityRenderer.extractNameTags: the score shows within the // below-name distance (10), under the name. @@ -162,7 +165,7 @@ impl Game<'_> { if distance_sqr < 10.0 * 10.0 && let Some(score) = holder.and_then(|h| self.below_name_text(&h)) { - lines.push((score, 0.0)); + lines.push((score.resolve_legacy(), 0.0)); lift = f64::from(9.0f32 * 1.15 * 0.025); } lines.push((shown, lift)); @@ -213,14 +216,15 @@ impl Game<'_> { let shown = match team { Some(t) => t.formatted_name(name.clone()), None => name.clone(), - }; + } + .resolve_legacy(); let mut lines = Vec::new(); let mut lift = 0.0; let holder = self.persist.uuids.get(&a.id).map(|u| uuid_text(*u)); if distance_sqr < 10.0 * 10.0 && let Some(score) = holder.and_then(|h| self.below_name_text(&h)) { - lines.push((score, 0.0)); + lines.push((score.resolve_legacy(), 0.0)); lift = f64::from(9.0f32 * 1.15 * 0.025); } lines.push((shown, lift)); diff --git a/crates/horizon-client/src/hud.rs b/crates/horizon-client/src/hud.rs index 617126c2e..4e3290f15 100644 --- a/crates/horizon-client/src/hud.rs +++ b/crates/horizon-client/src/hud.rs @@ -890,6 +890,9 @@ impl HudBatch { color: u32, shadow: bool, ) -> f32 { + // StringDecomposer.iterateFormatted inside Font.drawInternal: + // `§` codes inside a span restyle what follows and are not drawn. + let text = text.resolved(); let font = &atlas.font; let alpha = (color >> 24) as u8; let mut glyphs = Vec::new(); diff --git a/crates/horizon-client/src/text.rs b/crates/horizon-client/src/text.rs index 1254d32f6..4c04c9da5 100644 --- a/crates/horizon-client/src/text.rs +++ b/crates/horizon-client/src/text.rs @@ -428,6 +428,15 @@ impl Text { } out } + /// `resolve_legacy` borrowing `self` when no span holds a `§` code (the + /// common case). + pub fn resolved(&self) -> std::borrow::Cow<'_, Text> { + if self.spans.iter().any(|s| s.text.contains('§')) { + std::borrow::Cow::Owned(self.resolve_legacy()) + } else { + std::borrow::Cow::Borrowed(self) + } + } /// The text without its legacy `§` codes (ChatFormatting.stripFormatting: /// a `§` and a code character, `0`-`9`, `a`-`f`, `k`-`o` or `r`). pub fn strip_legacy(&self) -> Text { @@ -695,4 +704,20 @@ mod tests { assert_eq!(t.spans[1].style.color, Some(GOLD)); assert_eq!(t.slice(1, 2).plain(), "y"); } + #[test] + fn resolved_borrows_clean_text_and_restyles_codes() { + let clean = Text::colored("Steve", GOLD); + assert!(matches!(clean.resolved(), std::borrow::Cow::Borrowed(_))); + // A colour code inside a span clears its decorations (a bold + // "§cSteve" draws "Steve" red, not bold). + let styled = Style { + bold: Some(true), + ..Style::default() + }; + let styled = Text::styled("§cSteve", styled); + let resolved = styled.resolved(); + assert_eq!(resolved.plain(), "Steve"); + assert_eq!(resolved.spans[0].style.color, Some(RED)); + assert_eq!(resolved.spans[0].style.bold, Some(false)); + } } diff --git a/crates/horizon-client/src/ui.rs b/crates/horizon-client/src/ui.rs index 1c6e93067..3cf7af9cb 100644 --- a/crates/horizon-client/src/ui.rs +++ b/crates/horizon-client/src/ui.rs @@ -279,9 +279,17 @@ pub fn centered_text( w: f32, color: u32, ) { - let width = atlas.font.width_text(text); + let text = text.resolved(); + let width = atlas.font.width_text(&text); if width <= w { - batch.text_styled(atlas, text, x + ((w - width) / 2.0).floor(), y, color, true); + batch.text_styled( + atlas, + &text, + x + ((w - width) / 2.0).floor(), + y, + color, + true, + ); } else { // Clip by characters (vanilla scrolls; we show the part that fits). let mut shown = Text::empty(); From c965aad8d3ad6a837149d6ae5a6e7100c168232d Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:13:55 +0100 Subject: [PATCH 135/238] Shader packs, clean room 40: terrain in the shadow map, culled from the light The shadow program now draws the world's terrain from the light's view. The shadow pass culls its own section list against the shadow camera's frustum (render distance, capped by shadowDistanceRenderMul), nearest the light first, and draws only the faces toward the light. Its draws go by arena page within 64-block bands from the light, as the camera's solid draws do, so the flat path draws them in about ten calls per list instead of one per page change (over a thousand with BSL). Its draws follow the camera's translucent draws in the same instance and indirect buffers, so the flat path gives them calls of their own. Sections never sorted for the camera are sorted once so their translucent indices are valid. The frame's first shadow pass clears the shadow map and shadowcolor buffers as it begins, instead of a separate pass that loads and stores them all; the translucent shadow pass is timed under its own label. Without a pack nothing changes: the shadow lists are empty and the terrain stays one draw call. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 174 ++++++++++++++++-- crates/horizon-client/src/gfx/flat.rs | 70 ++++++- crates/horizon-client/src/gfx/pack_post.rs | 161 +++++++++++++--- crates/horizon-client/src/gfx/pack_shadow.rs | 84 +++++++-- .../horizon-client/src/gfx/pack_uniforms.rs | 15 +- crates/horizon-shaders/src/post.rs | 10 +- 6 files changed, 447 insertions(+), 67 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index bba07c647..b21c846a5 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -471,6 +471,8 @@ pub struct Renderer { /// Per-frame scratch, kept to avoid allocating every frame. visible: Vec, visible_lods: Vec, + /// The sections a shader pack's shadow camera sees (`pack_post::ShadowView`). + shadow_visible: Vec, translucent_draws: Vec<(u64, usize, [u32; 5], (u32, Option))>, origins: Vec<[f32; 4]>, /// `DrawIndexedIndirectArgs` words: index count, instance count, first @@ -485,6 +487,8 @@ pub struct Renderer { /// Each section's solid draws, to group them by arena page. solid_runs: page_runs::PageRuns, phase2_runs: page_runs::PageRuns, + /// The shadow camera's draws by page, in bands of distance from the light. + shadow_runs: page_runs::PageRuns, /// Block-entity blocks per meshed section (world position, state id). block_entities: HashMap>, /// Changes whenever `block_entities` does (unique across renderers). @@ -2351,6 +2355,7 @@ impl Renderer { multi_draw, visible: Vec::new(), visible_lods: Vec::new(), + shadow_visible: Vec::new(), translucent_draws: Vec::new(), origins: Vec::new(), draws: Vec::new(), @@ -2359,6 +2364,7 @@ impl Renderer { phase2_pages: Vec::new(), solid_runs: page_runs::PageRuns::default(), phase2_runs: page_runs::PageRuns::default(), + shadow_runs: page_runs::PageRuns::default(), block_entities: HashMap::new(), block_entity_revision: next_block_entity_revision(), occlusion: occlusion::Occlusion::new(crate::chunk::MIN_Y, crate::chunk::HEIGHT / 16), @@ -3275,8 +3281,43 @@ impl Renderer { ); visible_lods.sort_unstable_by_key(|v| (v.distance, v.key)); } + // A shader pack's shadow pass draws the sections in its camera's box, all of them (the + // occlusion graph follows the eye, not the light), within shadowDistanceRenderMul. + let shadow_view = self + .pack_post + .as_ref() + .and_then(|p| p.1.as_ref()) + .filter(|_| !frame.title) + .and_then(|p| p.shadow_view(frame)); + let mut shadow_visible = std::mem::take(&mut self.shadow_visible); + shadow_visible.clear(); + if let Some(v) = &shadow_view { + let radius = (frame.detail_distance / 16.0) as i32; + let radius = v + .distance + .map_or(radius, |d| radius.min((d / 16.0).ceil() as i32)); + let light = Frustum { + planes: frustum(v.cull), + }; + crate::spans::time("shadow visibility", || { + self.sections.visible( + camera, + radius, + &light, + |_| true, + |_, _| false, + &mut shadow_visible, + ) + }); + // Nearest the light first, so the depth test rejects what lies under it. + let light = v.light; + shadow_visible.sort_unstable_by(|a, b| { + let height = |s: &resident::Visible| glam::Vec3::from(s.origin).dot(light); + height(b).total_cmp(&height(a)) + }); + } self.drawn_sections = visible.len() + visible_lods.len(); - let instances = self.drawn_sections; + let instances = self.drawn_sections + shadow_visible.len(); if instances as u64 > self.instance_capacity { self.instance_capacity = (instances as u64).next_power_of_two(); self.instances = self.device.create_buffer(&wgpu::BufferDescriptor { @@ -3293,7 +3334,8 @@ impl Renderer { visible .iter() .map(|v| section_instance(v.origin)) - .chain(visible_lods.iter().map(|v| lod::instance(v.origin))), + .chain(visible_lods.iter().map(|v| lod::instance(v.origin))) + .chain(shadow_visible.iter().map(|v| section_instance(v.origin))), ); if !self.origins.is_empty() { self.uploads.buffer( @@ -3497,9 +3539,87 @@ impl Renderer { self.translucency_cursor += budget; let translucent_draws = self.draws.len() as u32 - solid_draws; self.section_draws = self.draws.len(); + // The shadow camera's sections after the camera's: quads that can face the light, then + // translucent ones (in their last sorted order; depth decides there). + let shadow_first = self.draws.len() as u32; + let mut shadow_translucent = Vec::new(); + self.shadow_runs.clear(); + if let Some(view) = &shadow_view { + let base = self.drawn_sections; + let toward = view.light * 1.0e5; + // How far below the nearest section to the light (squared, as bits, as `PageRuns` + // takes distances): each band's draws go by page, a call per page and band. + let height = |v: &resident::Visible| glam::Vec3::from(v.origin).dot(view.light); + let top = shadow_visible.first().map_or(0.0, height); + for (k, v) in shadow_visible.iter().enumerate() { + let s = self.sections.get_mut(v.slot); + let instance = (base + k) as u32; + let depth = f64::from(top - height(v)).max(0.0); + let distance = (depth * depth).to_bits(); + // A section the camera never saw has no sorted indices yet: sorted once now. + if let Some((index_page, index_start)) = s.sorted_indices + && !s.sort.is_sorted() + { + let origin = [ + v.key.0 * 16, + crate::chunk::MIN_Y + v.key.1 * 16, + v.key.2 * 16, + ]; + if let Some(indices) = + s.sort + .update(camera.to_array(), origin, &mut self.translucent_sorter) + { + self.uploads.buffer( + &mut encoder, + self.sorted_indices.buffer(index_page), + index_start * 4, + bytemuck::cast_slice(indices), + ); + } + } + let before = self.draws.len(); + facing_runs(&s.sides, Some(toward), v.origin, |first, quads| { + let start = s.start as u32 + first * 4; + self.draws.push([quads * 6, 1, 0, start, instance]); + self.draw_pages.push((s.page, None)); + }); + let len = self.draws.len() - before; + self.shadow_runs.push(distance, s.page, before, len, [0; 3]); + if let Some((index_page, index_start)) = s.sorted_indices { + let start = s.start + u64::from(s.solid) * 4; + shadow_translucent.push(( + distance, + [ + s.translucent * 6, + 1, + index_start as u32, + start as u32, + instance, + ], + (s.page, Some(index_page)), + )); + } + } + } + let shadow_solid = self.draws.len() as u32 - shadow_first; + self.shadow_runs + .regroup(&mut self.draws, &mut self.draw_pages, shadow_first as usize); + self.shadow_runs.clear(); + for (distance, draw, pages) in shadow_translucent { + self.shadow_runs + .push(distance, pages.0, self.draws.len(), 1, [0; 3]); + self.draws.push(draw); + self.draw_pages.push(pages); + } + self.shadow_runs.regroup( + &mut self.draws, + &mut self.draw_pages, + (shadow_first + shadow_solid) as usize, + ); + let shadow_translucent = self.draws.len() as u32 - shadow_first - shadow_solid; // Metal replays a multi-draw as one draw call per entry. self.section_draw_calls = if self.multi_draw && self.info.backend != wgpu::Backend::Metal { - (0..self.draws.len()) + (0..self.section_draws) .filter(|&i| { i == 0 || i == phase1_draws as usize @@ -3508,7 +3628,7 @@ impl Renderer { }) .count() } else { - self.draws.len() + self.section_draws }; let debug_depth = frame.debug_lines.len() as u32; let debug_top = frame.debug_lines_on_top.len() as u32; @@ -3743,7 +3863,9 @@ impl Renderer { Some(flat) if self.flat_on && !self.draws.is_empty() - && (self.drawn_sections >= flat.min_sections || pack_terrain) => + && (self.drawn_sections >= flat.min_sections + || pack_terrain + || shadow_view.is_some()) => { let sections = &self.sections; let lods = &self.lods; @@ -3758,9 +3880,11 @@ impl Renderer { &self.draw_pages, phase1_draws as usize, solid_draws as usize, + [shadow_first, shadow_first + shadow_solid].map(|d| d as usize), phase2, visible .iter() + .chain(&shadow_visible) .filter_map(|v| { let id = sections.get(v.slot).flat_id?; Some((id, section_instance(v.origin))) @@ -3776,7 +3900,7 @@ impl Renderer { _ => false, }; if flat_frame { - self.section_draw_calls = self.flat.as_ref().map_or(0, |f| f.calls.len()); + self.section_draw_calls = self.flat.as_ref().map_or(0, |f| f.shadow_calls[0]); } else if self.multi_draw && !self.draws.is_empty() { if self.draws.len() as u64 > self.indirect_capacity { self.indirect_capacity = (self.draws.len() as u64).next_power_of_two(); @@ -3805,6 +3929,8 @@ impl Renderer { let pack_post = !frame.title && self.pack_post.as_ref().is_some_and(|p| p.1.is_some()); // The pack's program reads the vertices this frame's draws bind. let pack_draws_terrain = pack_terrain && flat_frame == self.pack_terrain_flat; + // The shadow camera's lists, drawn by the pack's shadow programs. + let pack_draws_shadow = shadow_view.is_some() && flat_frame == self.pack_terrain_flat; let pack_draws_sky = pack_post && self .pack_post @@ -4074,7 +4200,7 @@ impl Renderer { _ if pack_draws_water => {} Some(flat) => { pass.set_pipeline(&flat.translucent); - flat.draw(&mut pass, &flat.calls[flat.solid_calls..], |p| { + flat.draw(&mut pass, flat.translucent_calls(), |p| { self.arena.buffer(p) }); pass.set_bind_group(0, &self.group, &[]); @@ -4165,7 +4291,7 @@ impl Renderer { post.set_weather(weather); let flat = self.flat.as_ref().filter(|_| flat_frame); let pages: Vec<(u32, wgpu::Buffer)> = match flat { - Some(_) if pack_draws_terrain => self + Some(_) if pack_draws_terrain || pack_draws_shadow => self .arena .page_buffers() .map(|(p, b)| (p, b.clone())) @@ -4174,27 +4300,38 @@ impl Renderer { }; let mut terrain = |rp: &mut wgpu::RenderPass<'_>, - translucent: bool, + list: pack_post::TerrainList, page: &dyn Fn(&mut wgpu::RenderPass<'_>, u32)| { - if !pack_draws_terrain { + use pack_post::TerrainList as L; + let shadow = matches!(list, L::ShadowSolid | L::ShadowTranslucent); + if !(if shadow { + pack_draws_shadow + } else { + pack_draws_terrain + }) { return; } match flat { Some(flat) => { - let calls = if translucent { - &flat.calls[flat.solid_calls..] - } else { - &flat.calls[..flat.solid_calls] + let [shadow, shadow_translucent] = flat.shadow_calls; + let calls = match list { + L::Solid => &flat.calls[..flat.solid_calls], + L::Translucent => flat.translucent_calls(), + L::ShadowSolid => &flat.calls[shadow..shadow_translucent], + L::ShadowTranslucent => &flat.calls[shadow_translucent..], }; flat.draw_with(rp, calls, |p| self.arena.buffer(p), page); } None => { rp.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); rp.set_vertex_buffer(1, self.instances.slice(..)); - let (first, count) = if translucent { - (solid_draws, translucent_draws) - } else { - (0, solid_draws) + let (first, count) = match list { + L::Solid => (0, solid_draws), + L::Translucent => (solid_draws, translucent_draws), + L::ShadowSolid => (shadow_first, shadow_solid), + L::ShadowTranslucent => { + (shadow_first + shadow_solid, shadow_translucent) + } }; self.draw_sections(rp, &self.indirect, 0, first, count); } @@ -4429,6 +4566,7 @@ impl Renderer { let _ = self.started; self.visible = visible; self.visible_lods = visible_lods; + self.shadow_visible = shadow_visible; Ok(true) } } diff --git a/crates/horizon-client/src/gfx/flat.rs b/crates/horizon-client/src/gfx/flat.rs index 6b71a16f7..5d86888da 100644 --- a/crates/horizon-client/src/gfx/flat.rs +++ b/crates/horizon-client/src/gfx/flat.rs @@ -40,6 +40,8 @@ enum Source { struct Plan { phase1_calls: usize, solid_calls: usize, + /// Where the shadow camera's solid and translucent calls begin. + shadow_calls: [usize; 2], total: u64, } const QUADS: u32 = u32::MAX; @@ -97,6 +99,10 @@ pub struct Flat { pub calls: Vec, pub phase1_calls: usize, pub solid_calls: usize, + /// Where a shader pack's shadow camera's solid and translucent calls + /// begin (`calls.len()` without them): the camera's translucent calls + /// end at the first. + pub shadow_calls: [usize; 2], /// Frames with fewer sections in view keep the per-section draws. pub min_sections: usize, range_align: u64, @@ -164,12 +170,16 @@ fn buffer( /// draw calls, one per run of draws in the same vertex page, phase 1's solid /// ones, phase 2's (draws `phase1_draws..solid_draws`) and translucent ones /// apart. A phase 2 range names its draw in phase 2 and its call in phase 2 -/// instead of a destination: the call's room holds all of its draws. +/// instead of a destination: the call's room holds all of its draws. Draws +/// from `shadow[0]` (solid) and `shadow[1]` (translucent) on are a shader +/// pack's shadow camera's, with calls of their own. +#[allow(clippy::too_many_arguments)] fn plan( draws: &[[u32; 5]], pages: &[(u32, Option)], phase1_draws: usize, solid_draws: usize, + shadow: [usize; 2], align: u64, calls: &mut Vec, ranges: &mut Vec, @@ -179,6 +189,7 @@ fn plan( ranges.clear(); segments.clear(); let (mut phase1_calls, mut solid_calls) = (None, None); + let mut shadow_calls = [None; 2]; let mut total = 0u64; let mut by_source: Vec<(Source, Vec)> = Vec::new(); for (i, (d, &(page, sorted))) in draws.iter().zip(pages).enumerate() { @@ -188,10 +199,14 @@ fn plan( if i == solid_draws { solid_calls = Some(calls.len()); } - match calls.last_mut() { - Some(last) if last.page == page && i != phase1_draws && i != solid_draws => { - last.count += d[0] + for (k, &from) in shadow.iter().enumerate() { + if i == from { + shadow_calls[k].get_or_insert(calls.len()); } + } + let starts = i == phase1_draws || i == solid_draws || shadow.contains(&i); + match calls.last_mut() { + Some(last) if last.page == page && !starts => last.count += d[0], _ => calls.push(Call { page, first: total as u32, @@ -227,6 +242,7 @@ fn plan( Plan { phase1_calls: phase1_calls.unwrap_or(calls.len()).min(solid_calls), solid_calls, + shadow_calls: shadow_calls.map(|c| c.unwrap_or(calls.len())), total, } } @@ -340,6 +356,7 @@ impl Flat { calls: Vec::new(), phase1_calls: 0, solid_calls: 0, + shadow_calls: [0; 2], min_sections: if std::env::var("HORIZON_FLAT_DRAWS").as_deref() == Ok("1") { 0 } else { @@ -412,6 +429,7 @@ impl Flat { /// /// When culling, the solid draws from `phase1_draws` are phase 2's, which /// `phase2` holds as the test leaves them: `flatten_phase2` copies them. + /// Draws from `shadow` on are a shader pack's shadow camera's (`plan`). #[allow(clippy::too_many_arguments)] pub fn prepare( &mut self, @@ -422,6 +440,7 @@ impl Flat { pages: &[(u32, Option)], phase1_draws: usize, solid_draws: usize, + shadow: [usize; 2], phase2: Option<&wgpu::Buffer>, origins: impl Iterator, sorted_page: impl Fn(u32) -> wgpu::Buffer, @@ -464,12 +483,14 @@ impl Flat { let Plan { phase1_calls, solid_calls, + shadow_calls, total, } = plan( draws, pages, phase1_draws, solid_draws, + shadow, self.range_align, &mut self.calls, &mut self.range_data, @@ -477,10 +498,12 @@ impl Flat { ); self.phase1_calls = phase1_calls; self.solid_calls = solid_calls; + self.shadow_calls = shadow_calls; if total == 0 || total * 4 > device.limits().max_storage_buffer_binding_size { self.calls.clear(); self.phase1_calls = 0; self.solid_calls = 0; + self.shadow_calls = [0; 2]; return false; } if self.range_data.len() as u64 > self.range_capacity { @@ -644,6 +667,11 @@ impl Flat { pass.dispatch_workgroups((*count).min(GROUP_LIMIT), count.div_ceil(GROUP_LIMIT), 1); } + /// The camera's translucent calls. + pub fn translucent_calls(&self) -> &[Call] { + &self.calls[self.solid_calls..self.shadow_calls[0]] + } + /// Draws `calls` with a flat pipeline set and `vertices` giving each /// page's vertex buffer. pub fn draw<'a>( @@ -735,11 +763,13 @@ mod tests { phase1_calls, solid_calls, total, + .. } = plan( &draws, &pages, solid, solid, + [draws.len(); 2], 4, &mut calls, &mut ranges, @@ -774,6 +804,35 @@ mod tests { assert_eq!(ranges.len(), 12); } + /// A shader pack's shadow draws follow the camera's, in calls of their + /// own even from the same page: solid ones, then translucent ones. + #[test] + fn plan_gives_the_shadow_lists_calls_of_their_own() { + let (mut draws, mut pages, solid) = sample(); + let camera = draws.len(); + draws.extend([[6, 1, 0, 40, 6], [6 * 2, 1, 0, 64, 7], [6, 1, 6, 80, 7]]); + pages.extend([(0, None), (0, None), (0, Some(0))]); + let (mut calls, mut ranges, mut segments) = (Vec::new(), Vec::new(), Vec::new()); + let plan = plan( + &draws, + &pages, + solid, + solid, + [camera, camera + 2], + 4, + &mut calls, + &mut ranges, + &mut segments, + ); + assert_eq!((plan.solid_calls, plan.shadow_calls), (3, [4, 5])); + let call = |page, first, count| Call { page, first, count }; + assert_eq!( + &calls[3..], + [call(0, 60, 18), call(0, 78, 18), call(0, 96, 6)] + ); + assert_eq!(plan.total, 6 * 17); + } + /// Phase 2's draws (here the third and fourth) get calls of their own, /// each with room for all of its draws, and ranges naming their draw /// and call in phase 2. @@ -786,6 +845,7 @@ mod tests { &pages, 2, solid, + [draws.len(); 2], 4, &mut calls, &mut ranges, @@ -796,6 +856,7 @@ mod tests { Plan { phase1_calls: 1, solid_calls: 3, + shadow_calls: [4, 4], total: 6 * 13 } ); @@ -899,6 +960,7 @@ mod tests { &pages, phase1, solid, + [draws.len(); 2], align, &mut calls, &mut ranges, diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index fdc4563ef..57ce0b147 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -270,6 +270,9 @@ pub struct PackPost { depth1: bool, /// The shadow buffers, when the shadow pass runs (2.5), and the `shadowcomp` programs. shadow_targets: Option, + /// What draws terrain from the light (`shadow`), and translucent terrain (`shadow_water`). + shadow_terrain: Option, + shadow_water: Option, shadow_comp: Vec, /// Buffers with mipmaps (filterable ones a program asks them for). mipped: u16, @@ -598,10 +601,35 @@ fn attach( }) } -/// Draws the game's terrain (`true`: translucent) into a pass; calls its third argument with -/// each arena page before drawing from it. +/// Which of the frame's terrain draws: the camera's solid or translucent ones, or the shadow +/// camera's. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum TerrainList { + Solid, + Translucent, + ShadowSolid, + ShadowTranslucent, +} + +impl TerrainList { + fn translucent(self) -> bool { + matches!(self, Self::Translucent | Self::ShadowTranslucent) + } +} + +/// Draws the game's terrain of one list into a pass; calls its third argument with each arena +/// page before drawing from it. pub(super) type TerrainDraw<'a> = - dyn FnMut(&mut wgpu::RenderPass<'_>, bool, &dyn Fn(&mut wgpu::RenderPass<'_>, u32)) + 'a; + dyn FnMut(&mut wgpu::RenderPass<'_>, TerrainList, &dyn Fn(&mut wgpu::RenderPass<'_>, u32)) + 'a; + +/// What the shadow camera sees of the terrain, for the game's draw lists: its view and +/// projection in wgpu's clip space, the direction toward the light, and how far from the camera +/// terrain is drawn (`shadowDistanceRenderMul`). +pub(super) struct ShadowView { + pub cull: glam::Mat4, + pub light: glam::Vec3, + pub distance: Option, +} /// Set 3 of flat terrain, read by the vertex stage: `mc_Entity`'s table (-1 pairs when empty) /// and, per arena page, the page's vertices (a vertex reads its quad's corners for its normal). @@ -1007,6 +1035,14 @@ impl PackPost { .water .zip(terrain_geometry.as_ref()) .and_then(|(p, g)| build(p, Some(g))); + let shadow_terrain = post + .shadow + .zip(terrain_geometry.as_ref()) + .and_then(|(p, g)| build(p, Some(g))); + let shadow_water = post + .shadow_water + .zip(terrain_geometry.as_ref()) + .and_then(|(p, g)| build(p, Some(g))); let sky = (post.sky.is_some() || post.sky_textured.is_some()).then(|| { let geometry = PackSky::new(device); let basic = Geometry { @@ -1124,9 +1160,22 @@ impl PackPost { if let Some(t) = &terrain { notes.push(format!("terrain draws with {}", t.name)); } + for (part, pass) in [ + ("terrain", &shadow_terrain), + ("translucent terrain", &shadow_water), + ] { + if let Some(p) = pass { + notes.push(format!("{part} casts shadows with {}", p.name)); + } + } if let Some(w) = &water { notes.push(format!("translucent terrain draws with {}", w.name)); } + // Timed (and labelled) apart from the solid shadow pass that runs the same program. + let mut shadow_water = shadow_water; + if let Some(p) = &mut shadow_water { + p.name.push_str(" (translucent)"); + } for (part, pass) in sky .iter() .flat_map(|s| [("sky", &s.basic), ("sun and moon", &s.textured)]) @@ -1168,6 +1217,7 @@ impl PackPost { .chain(&shadow_comp) .chain(&terrain) .chain(&water)) + .chain(shadow_terrain.iter().chain(&shadow_water)) .chain(sky.iter().flat_map(|s| s.basic.iter().chain(&s.textured))) .chain(entities.iter().flat_map(|e| e.passes.iter().flatten())) .chain(&clouds) @@ -1189,9 +1239,11 @@ impl PackPost { let t = super::pack_shadow::ShadowTargets::new(device, &shadow_config, f, colors, depth1); notes.push(format!( - "shadow map {0}x{0}, shadowcolor buffers {colors:#b}{1}", + "shadow map {0}x{0} over {2} blocks, sunPathRotation {3}, shadowcolor buffers {colors:#b}{1}", t.size, - if depth1 { ", shadowtex1" } else { "" } + if depth1 { ", shadowtex1" } else { "" }, + shadow_config.distance, + shadow_config.sun_path_rotation, )); t }); @@ -1334,6 +1386,8 @@ impl PackPost { hand, depth1, shadow_targets, + shadow_terrain, + shadow_water, shadow_comp, mipped, mips_valid: 0, @@ -1545,7 +1599,12 @@ impl PackPost { load: wgpu::LoadOp::Load, store: wgpu::StoreOp::Store, }; - // The shadow pass draws into the shadow buffers and the shadow map. + // The shadow pass draws into the shadow buffers and the shadow map, the frame's first + // one clearing them. + let (depth_ops, shadow_ops) = match &mut self.shadow_targets { + Some(s) if pass.group.shadow() => s.clear_ops(pass.writes.iter().map(|w| w.1)), + _ => (super::pack_shadow::load(), [load; 2]), + }; let shadow = self.shadow_targets.as_ref().filter(|_| pass.group.shadow()); let mut attachments: Vec> = Vec::new(); for &(location, buffer) in &pass.writes { @@ -1554,7 +1613,9 @@ impl PackPost { attachments.resize(l + 1, None); } attachments[l] = match shadow { - Some(s) => s.color(buffer, false).and_then(|v| attach(v, load)), + Some(s) => s + .color(buffer, false) + .and_then(|v| attach(v, shadow_ops.get(buffer).copied().unwrap_or(load))), None => targets.color[buffer] .as_ref() .and_then(|t| attach(&t[usize::from(self.main & 1 << buffer != 0)], load)), @@ -1567,10 +1628,7 @@ impl PackPost { timestamp_writes, depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { view: shadow.map_or(&targets.depth, |s| &s.depth.1), - depth_ops: Some(wgpu::Operations { - load: wgpu::LoadOp::Load, - store: wgpu::StoreOp::Store, - }), + depth_ops: Some(depth_ops), stencil_ops: None, }), ..Default::default() @@ -1585,18 +1643,20 @@ impl PackPost { queue: &wgpu::Queue, encoder: &mut wgpu::CommandEncoder, size: [u32; 2], - translucent: bool, + list: TerrainList, draw: &mut TerrainDraw<'_>, ) { - let slot = if translucent { - &mut self.water - } else { - &mut self.terrain + let slot = match list { + TerrainList::Solid => &mut self.terrain, + TerrainList::Translucent => &mut self.water, + TerrainList::ShadowSolid => &mut self.shadow_terrain, + TerrainList::ShadowTranslucent => &mut self.shadow_water, }; let Some(mut pass) = slot.take() else { return; }; - if translucent + let translucent = list.translucent(); + if list == TerrainList::Translucent && let Some(t) = &self.targets && let Some((copy, _)) = &t.depth1 { @@ -1625,14 +1685,17 @@ impl PackPost { rp.set_bind_group(3, group, &[]); } }; - draw(&mut rp, translucent, &page); + draw(&mut rp, list, &page); } - self.mips_valid &= !pass.written; - if translucent { - self.water = Some(pass); - } else { - self.terrain = Some(pass); + if !pass.group.shadow() { + self.mips_valid &= !pass.written; } + *match list { + TerrainList::Solid => &mut self.terrain, + TerrainList::Translucent => &mut self.water, + TerrainList::ShadowSolid => &mut self.shadow_terrain, + TerrainList::ShadowTranslucent => &mut self.shadow_water, + } = Some(pass); } /// The sky's parts in vanilla's order, each with the program that draws it; one render pass @@ -1782,7 +1845,7 @@ impl PackPost { if super::sky_visible(frame) { self.draw_sky(device, queue, encoder, size, frame); } - self.draw_terrain(device, queue, encoder, size, false, terrain); + self.draw_terrain(device, queue, encoder, size, TerrainList::Solid, terrain); self.draw_entities(device, queue, encoder, size, false); self.draw_clouds(device, queue, encoder, size); self.draw_hand(device, queue, encoder, size, frame); @@ -1878,7 +1941,14 @@ impl PackPost { size: [u32; 2], terrain: &mut TerrainDraw<'_>, ) { - self.draw_terrain(device, queue, encoder, size, true, terrain); + self.draw_terrain( + device, + queue, + encoder, + size, + TerrainList::Translucent, + terrain, + ); self.draw_entities(device, queue, encoder, size, true); self.draw_weather(device, queue, encoder, size); } @@ -1960,6 +2030,20 @@ impl PackPost { } /// Whether the pack draws terrain itself. + /// What the shadow camera draws of the terrain this frame, when the pack draws it. + pub fn shadow_view(&self, frame: &super::Frame) -> Option { + self.shadow_targets.as_ref()?; + if self.shadow_terrain.is_none() && self.shadow_water.is_none() { + return None; + } + let shadow = &self.uniforms.shadow; + Some(ShadowView { + cull: pack_uniforms::shadow_culling(frame, shadow), + light: pack_uniforms::shadow_light(frame, shadow), + distance: (shadow.render_mul > 0.0).then_some(shadow.distance * shadow.render_mul), + }) + } + pub fn has_terrain(&self) -> bool { self.terrain.is_some() } @@ -2088,13 +2172,32 @@ impl PackPost { device: &wgpu::Device, queue: &wgpu::Queue, encoder: &mut wgpu::CommandEncoder, + terrain: &mut TerrainDraw<'_>, ) { - let Some(t) = &self.shadow_targets else { + let Some(t) = &mut self.shadow_targets else { return; }; - t.clear(encoder); - t.copy_depth1(encoder); + t.begin_frame(); let size = [t.size; 2]; + self.draw_terrain( + device, + queue, + encoder, + size, + TerrainList::ShadowSolid, + terrain, + ); + let t = self.shadow_targets.as_mut().expect("shadow pass"); + t.clear_rest(encoder); + t.copy_depth1(encoder); + self.draw_terrain( + device, + queue, + encoder, + size, + TerrainList::ShadowTranslucent, + terrain, + ); let mut comp = std::mem::take(&mut self.shadow_comp); for pass in &mut comp { let group = self.bind(device, queue, pass, size); @@ -2181,7 +2284,7 @@ impl PackPost { if self.targets.is_none() { return; } - self.run_shadow(device, queue, encoder); + self.run_shadow(device, queue, encoder, terrain); let Some(targets) = &self.targets else { return }; let main = |m: u16, i: usize| usize::from(m & 1 << i != 0); let view = |m: u16, i: usize, alt: bool| { diff --git a/crates/horizon-client/src/gfx/pack_shadow.rs b/crates/horizon-client/src/gfx/pack_shadow.rs index 7688d1250..92f0aeda0 100644 --- a/crates/horizon-client/src/gfx/pack_shadow.rs +++ b/crates/horizon-client/src/gfx/pack_shadow.rs @@ -3,6 +3,29 @@ //! with two sides that `shadowcomp` programs flip as composite programs flip colortex (6.1.6). //! Square, `shadowMapResolution` pixels. +type Ops = wgpu::Operations; +type Color = wgpu::Color; + +/// Loaded and stored. +pub(super) fn load() -> Ops { + wgpu::Operations { + load: wgpu::LoadOp::Load, + store: wgpu::StoreOp::Store, + } +} + +fn clear(c: [f32; 4]) -> Ops { + wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color { + r: c[0].into(), + g: c[1].into(), + b: c[2].into(), + a: c[3].into(), + }), + store: wgpu::StoreOp::Store, + } +} + /// What the shadow pass writes and the programs read. pub(super) struct ShadowTargets { pub size: u32, @@ -16,6 +39,11 @@ pub(super) struct ShadowTargets { pub clears: [Option<[f32; 4]>; 2], /// Bit n set: B is shadowcolor n's "main". pub main: u16, + /// Nothing cleared yet this frame: the first pass into the shadow buffers clears them as it + /// begins (`clear_ops`), which saves a pass that loads and stores them all on tiled GPUs. + fresh: bool, + /// Bit n set: shadowcolor n was cleared that way. + cleared: u8, /// Depth comparison for `sampler2DShadow` (hardware filtering): linear, as GL filters it. pub comparison: wgpu::Sampler, } @@ -85,6 +113,8 @@ impl ShadowTargets { b.clear.then(|| b.clear_color.unwrap_or([1.0; 4])) }), main: 0, + fresh: false, + cleared: 0, comparison: device.create_sampler(&wgpu::SamplerDescriptor { label: Some("Shader pack shadow comparison"), compare: Some(wgpu::CompareFunction::LessEqual), @@ -101,31 +131,59 @@ impl ShadowTargets { self.color.get(n)?.as_ref().map(|c| &c[side]) } - /// Clears the shadow map and the shadowcolor buffers cleared every frame (6.3). - pub fn clear(&self, encoder: &mut wgpu::CommandEncoder) { + /// A new frame: the shadow map and the shadowcolor buffers cleared every frame (6.3) are + /// cleared by the first pass that begins (`clear_ops`), or else by `clear_rest`. + pub fn begin_frame(&mut self) { + self.fresh = true; + self.cleared = 0; + } + + /// The load operations of a pass into the shadow map and shadowcolor buffers `writes`: + /// clears in the frame's first one, loads after. + pub fn clear_ops( + &mut self, + writes: impl Iterator, + ) -> (Ops, [Ops; 2]) { + if !std::mem::take(&mut self.fresh) { + return (load(), [load(); 2]); + } + let mut colors = [load(); 2]; + for n in writes.filter(|&n| n < 2) { + if let Some(c) = self.clears[n] { + colors[n] = clear(c); + self.cleared |= 1 << n; + } + } + let depth = wgpu::Operations { + load: wgpu::LoadOp::Clear(1.0), + store: wgpu::StoreOp::Store, + }; + (depth, colors) + } + + /// Clears what no pass has cleared this frame: all of it when no shadow program drew, the + /// cleared shadowcolor buffers the first one did not write. + pub fn clear_rest(&mut self, encoder: &mut wgpu::CommandEncoder) { + let depth = std::mem::take(&mut self.fresh); + let cleared = std::mem::replace(&mut self.cleared, 0b11); let attachments: Vec<_> = (0..2) + .filter(|n| cleared & 1 << n == 0) .filter_map(|n| { - let c = self.clears[n]?; Some(Some(wgpu::RenderPassColorAttachment { view: self.color(n, false)?, depth_slice: None, resolve_target: None, - ops: wgpu::Operations { - load: wgpu::LoadOp::Clear(wgpu::Color { - r: c[0].into(), - g: c[1].into(), - b: c[2].into(), - a: c[3].into(), - }), - store: wgpu::StoreOp::Store, - }, + ops: clear(self.clears[n]?), })) }) .collect(); + if !depth && attachments.is_empty() { + return; + } encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("Shader pack shadow clear"), color_attachments: &attachments, - depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + depth_stencil_attachment: depth.then_some(wgpu::RenderPassDepthStencilAttachment { view: &self.depth.1, depth_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Clear(1.0), diff --git a/crates/horizon-client/src/gfx/pack_uniforms.rs b/crates/horizon-client/src/gfx/pack_uniforms.rs index d1a85a1ba..c4df4377b 100644 --- a/crates/horizon-client/src/gfx/pack_uniforms.rs +++ b/crates/horizon-client/src/gfx/pack_uniforms.rs @@ -209,7 +209,7 @@ pub fn tilt_matrix(tilt: f32) -> Mat4 { } /// The world direction of the shadow light (7.1): the sun by day, the moon by night. -fn shadow_light(frame: &Frame, shadow: &Shadow) -> Vec3 { +pub fn shadow_light(frame: &Frame, shadow: &Shadow) -> Vec3 { let sun_angle = (frame.sky.sun_angle / 360.0 + 0.25).rem_euclid(1.0); let angle = if sun_angle < 0.5 { frame.sky.sun_angle @@ -219,6 +219,19 @@ fn shadow_light(frame: &Frame, shadow: &Shadow) -> Vec3 { body(angle, shadow.sun_path_rotation).normalize() } +/// `shadow_camera`'s view and projection in wgpu's clip space (depth 0..1), for culling. +pub fn shadow_culling(frame: &Frame, shadow: &Shadow) -> Mat4 { + let (view, gl) = shadow_camera(shadow_light(frame, shadow), frame.camera, shadow); + // GL's depth -w..w to wgpu's 0..w. + let depth = Mat4::from_cols( + Vec4::X, + Vec4::Y, + Vec4::new(0.0, 0.0, 0.5, 0.0), + Vec4::new(0.0, 0.0, 0.5, 1.0), + ); + depth * gl * view +} + /// Distance from the snapped point to the shadow camera, toward the light: the middle of the /// default near and far planes (OQ-6). const SHADOW_EYE: f64 = 128.0; diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs index 95b8c3663..dd062b508 100644 --- a/crates/horizon-shaders/src/post.rs +++ b/crates/horizon-shaders/src/post.rs @@ -531,8 +531,12 @@ fn load_shadow(loader: &mut Loader<'_>, terrain: Option<&GbuffersInputs>) { }) }) }; - let shadow = load(&["shadow"]); - let water = load(&["shadow_water", "shadow"]); + // `shadowTerrain` and `shadowTranslucent` (3.2) leave terrain out of the shadow pass. + let keep = |key: &str| properties.get(key).is_none_or(|v| v.trim() != "false"); + let shadow = keep("shadowTerrain").then(|| load(&["shadow"])).flatten(); + let water = keep("shadowTranslucent") + .then(|| load(&["shadow_water", "shadow"])) + .flatten(); if shadow.is_none() { loader .post @@ -868,6 +872,8 @@ mod tests { }; assert!(!none("").shadow_pass); assert!(none("shadow.enabled=true").shadow.is_some()); + let post = none("shadow.enabled=true\nshadowTranslucent=false"); + assert!(post.shadow.is_some() && post.shadow_water.is_none()); } /// Water falls back to the terrain chain and blends by default, as the sky does; entities From 13508cbd9c6ee7122694dc3f1482a10683f408ca Mon Sep 17 00:00:00 2001 From: Jan Pres Date: Sun, 25 Oct 2026 17:41:07 +0000 Subject: [PATCH 136/238] Creative: Ctrl+Backspace deletes a word in the search The creative inventory's search box is an append-only EditBox of 50 characters, but its editing was a plain String pop, so Ctrl+Backspace removed one character instead of the word before the caret, unlike every other text field and vanilla's EditBox.keyPressed. Move EditBox.getWordPosition and deleteWords into one shared helper in ui.rs (the UI and chat text fields had two copies of it), and route a Ctrl+Backspace in the creative search through it. A Select All selection is still what backspace deletes first, as in vanilla. Fixes #1764. Co-Authored-By: OpenCode (DeepSeek V4.1 Flash Free) --- crates/horizon-client/src/game.rs | 5 +- crates/horizon-client/src/game/clipboard.rs | 22 ++++-- crates/horizon-client/src/game/control.rs | 2 +- crates/horizon-client/src/game/pad_menu.rs | 6 +- crates/horizon-client/src/screen_draw.rs | 21 +---- crates/horizon-client/src/ui.rs | 86 ++++++++++++++++----- 6 files changed, 92 insertions(+), 50 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 7fcb3be89..6df6e531c 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -11272,9 +11272,10 @@ impl ApplicationHandler for Game<'_> { // The hovered slot took the key (`ignoreTextInput`). } else if let Some(shortcut) = self.edit_shortcut(&event) { self.creative_search_shortcut(shortcut); + } else if event.physical_key == PhysicalKey::Code(KeyCode::Backspace) { + self.creative_search_backspace(self.has_control_down()); } else { - let backspace = event.physical_key == PhysicalKey::Code(KeyCode::Backspace); - self.creative_search_edit(event.text.as_deref(), backspace); + self.creative_search_edit(event.text.as_deref()); } } WindowEvent::KeyboardInput { event, .. } diff --git a/crates/horizon-client/src/game/clipboard.rs b/crates/horizon-client/src/game/clipboard.rs index b9558ddfd..c87aa3681 100644 --- a/crates/horizon-client/src/game/clipboard.rs +++ b/crates/horizon-client/src/game/clipboard.rs @@ -97,15 +97,25 @@ impl Game<'_> { self.creative_screen.scroll = 0.0; } } - /// Typing or a backspace in the creative search box. - pub(super) fn creative_search_edit(&mut self, typed: Option<&str>, backspace: bool) { + /// A backspace in the creative search box: with Ctrl, the whole word + /// before the caret (`EditBox.deleteWords(-1)`), otherwise one character. + /// A Select All selection is what it deletes, as in `EditBox.keyPressed`. + pub(super) fn creative_search_backspace(&mut self, ctrl: bool) { let search = &mut self.creative_screen.search; - let cleared = self.text_selection.take(CREATIVE_SEARCH, search); - if backspace { - if !cleared { + if !self.text_selection.take(CREATIVE_SEARCH, search) { + if ctrl { + horizon_client::ui::delete_words(search, -1); + } else { search.pop(); } - } else if let Some(text) = typed { + } + self.creative_screen.scroll = 0.0; + } + /// Typing in the creative search box. + pub(super) fn creative_search_edit(&mut self, typed: Option<&str>) { + let search = &mut self.creative_screen.search; + self.text_selection.take(CREATIVE_SEARCH, search); + if let Some(text) = typed { clipboard::append_limited(search, text, CREATIVE_SEARCH_CHARS); } self.creative_screen.scroll = 0.0; diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index a0d8987d8..987937066 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -569,7 +569,7 @@ impl Game<'_> { } Screen::Creative if self.creative_screen.tab().search => { // As typed: replaces a selected query, keeps the length limit. - self.creative_search_edit(Some(text), false) + self.creative_search_edit(Some(text)) } _ => { return Err( diff --git a/crates/horizon-client/src/game/pad_menu.rs b/crates/horizon-client/src/game/pad_menu.rs index 32acdd256..30f2e7337 100644 --- a/crates/horizon-client/src/game/pad_menu.rs +++ b/crates/horizon-client/src/game/pad_menu.rs @@ -921,8 +921,10 @@ impl Game<'_> { } else if self.book.is_some() { self.book_input(key, text); } else if self.screen == Screen::Creative && self.creative_screen.tab().search { - if key != Some(KeyCode::Enter) { - self.creative_search_edit(text, backspace); + if backspace { + self.creative_search_backspace(false); + } else if key != Some(KeyCode::Enter) { + self.creative_search_edit(text); } } else if self.recipe_search_focused() { if key != Some(KeyCode::Enter) { diff --git a/crates/horizon-client/src/screen_draw.rs b/crates/horizon-client/src/screen_draw.rs index 572ee202f..03f979a68 100644 --- a/crates/horizon-client/src/screen_draw.rs +++ b/crates/horizon-client/src/screen_draw.rs @@ -619,26 +619,7 @@ impl TextField { /// `getWordPosition(dir, from)`: past the next space run, or back to the /// start of the word. pub fn word_position(&self, dir: isize, from: usize) -> usize { - let v: Vec = self.value.chars().collect(); - let mut at = from; - if dir > 0 { - at = v - .iter() - .skip(at) - .position(|c| *c == ' ') - .map_or(v.len(), |p| p + at); - while at < v.len() && v[at] == ' ' { - at += 1; - } - } else { - while at > 0 && v[at - 1] == ' ' { - at -= 1; - } - while at > 0 && v[at - 1] != ' ' { - at -= 1; - } - } - at + crate::ui::word_position(&self.value, dir, from) } /// `keyPressed` of a focused, editable box: `Some(changed)` when handled. pub fn key( diff --git a/crates/horizon-client/src/ui.rs b/crates/horizon-client/src/ui.rs index 1c6e93067..5f8f7b9f9 100644 --- a/crates/horizon-client/src/ui.rs +++ b/crates/horizon-client/src/ui.rs @@ -821,6 +821,40 @@ fn byte_index(s: &str, chars: usize) -> usize { pub fn char_len(s: &str) -> usize { s.chars().count() } +/// `EditBox.getWordPosition(dir, from)`: the character index `dir` words away +/// from `from`. Forward, past the rest of the word and its spaces; backward, +/// over any spaces to the start of the word. +pub fn word_position(value: &str, dir: isize, from: usize) -> usize { + let chars: Vec = value.chars().collect(); + let mut pos = from.min(chars.len()); + if dir < 0 { + while pos > 0 && chars[pos - 1] == ' ' { + pos -= 1; + } + while pos > 0 && chars[pos - 1] != ' ' { + pos -= 1; + } + } else { + while pos < chars.len() && chars[pos] != ' ' { + pos += 1; + } + while pos < chars.len() && chars[pos] == ' ' { + pos += 1; + } + } + pos +} +/// `EditBox.deleteWords(dir)` for an append-only field, whose caret is at the +/// end: removes the word in that direction. True when the value changed. +pub fn delete_words(value: &mut String, dir: isize) -> bool { + let len = char_len(value); + let to = word_position(value, dir, len); + if to == len { + return false; + } + value.truncate(byte_index(value, to)); + true +} impl crate::clipboard::Editable for EditBox { fn select_all(&mut self) { self.move_cursor_to_end(false); @@ -919,25 +953,7 @@ impl EditBox { } /// EditBox.getWordPosition(dir, from). fn word_position_from(&self, dir: isize, from: usize) -> usize { - let chars: Vec = self.value.chars().collect(); - let mut pos = from.min(chars.len()); - if dir < 0 { - while pos > 0 && chars[pos - 1] == ' ' { - pos -= 1; - } - while pos > 0 && chars[pos - 1] != ' ' { - pos -= 1; - } - } else { - let len = chars.len(); - while pos < len && chars[pos] != ' ' { - pos += 1; - } - while pos < len && chars[pos] == ' ' { - pos += 1; - } - } - pos + word_position(&self.value, dir, from) } pub fn set_cursor(&mut self, pos: usize, select: bool) { self.cursor = pos.min(char_len(&self.value)); @@ -1451,4 +1467,36 @@ mod tests { assert_eq!(e.value, "éx worl"); assert_eq!(e.cursor, 2); } + #[test] + fn word_position_steps_over_a_word_and_its_spaces() { + assert_eq!(word_position("hello world", -1, 11), 6); + assert_eq!(word_position("hello world", -1, 6), 0); + assert_eq!(word_position("hello world", -1, 13), 8); + assert_eq!(word_position("hello world", 1, 5), 8); + // From inside a word: back to its start, forward past it and its spaces. + assert_eq!(word_position("hello world", -1, 8), 6); + assert_eq!(word_position("hello world", 1, 2), 6); + // The ends, and a position past the end. + assert_eq!(word_position("hello", -1, 0), 0); + assert_eq!(word_position("hello", 1, 5), 5); + assert_eq!(word_position("hi", -1, 99), 0); + assert_eq!(word_position("hi", 1, 99), 2); + } + #[test] + fn delete_words_removes_the_word_before_the_end() { + let mut v = "stone bricks".to_string(); + assert!(delete_words(&mut v, -1)); + assert_eq!(v, "stone "); + assert!(delete_words(&mut v, -1)); + assert_eq!(v, ""); + assert!(!delete_words(&mut v, -1)); + // Characters, not bytes. + let mut v = "a é§x".to_string(); + assert!(delete_words(&mut v, -1)); + assert_eq!(v, "a "); + // Nothing after the end to delete. + let mut v = "stone".to_string(); + assert!(!delete_words(&mut v, 1)); + assert_eq!(v, "stone"); + } } From aa2f60b7bd78179f1e7723c866212fd4e1ec548e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 18:43:03 +0100 Subject: [PATCH 137/238] Copper golem statues turn to their next pose when right-clicked CopperGolemStatueBlock.useItemOn: anything but an axe, an empty hand too, cycles copper_golem_pose with its sound and block_change game event; honeycomb on a weathering statue passes on to waxing. Axe scraping, unwaxing and stripping and honeycomb waxing now count the item as used, and waxing sends its block_change game event, as vanilla's useOn does. The use_item_on harness gains statue cases. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 10 ++++++ crates/horizon-world/src/level/interact.rs | 31 ++++++++++++++++++ tools/test_use_item_on_parity.py | 37 ++++++++++++++++++++-- 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 7fcb3be89..ee6df4c38 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -8332,6 +8332,8 @@ impl Game<'_> { 1.0, 1.0, ); + // ItemStack.useOn: a use that acted counts as one of the item. + self.stats.add("used", &stack.item, 1); self.damage_held(damage); self.post_block_game_event("minecraft:block_change", pos, self.sculk_actor(), Some(new)); Ok(true) @@ -8536,6 +8538,14 @@ impl Game<'_> { 1.0, 1.0, ); + // HoneycombItem.useOn's game event, and ItemStack.useOn's used statistic. + self.post_block_game_event( + "minecraft:block_change", + hit.pos, + self.sculk_actor(), + Some(state), + ); + self.stats.add("used", &stack.item, 1); if !self.creative { self.consume_held(); } diff --git a/crates/horizon-world/src/level/interact.rs b/crates/horizon-world/src/level/interact.rs index b8251ba46..7b2195c34 100644 --- a/crates/horizon-world/src/level/interact.rs +++ b/crates/horizon-world/src/level/interact.rs @@ -592,6 +592,37 @@ impl Behaviours { { return InteractionResult::Pass; } + if self.data(state).chain.contains(&Class::CopperGolemStatue) { + return self.turn_statue(level, ticks, pos, state, item); + } self.use_without_item(level, ticks, pos, face, hit, player) } + + /// `CopperGolemStatueBlock.useItemOn` (and its weathering subclass's): anything but an axe + /// (`#minecraft:axes`, every one named `*_axe`), even an empty hand, turns the statue to its + /// next pose; honeycomb waxes a statue that still weathers instead. + fn turn_statue( + &self, + level: &mut L, + ticks: &mut Ticks, + pos: BlockPos, + state: BlockStateId, + item: Option<&str>, + ) -> InteractionResult { + let item = item.unwrap_or("minecraft:air"); + let waxed = self.table.name(state).starts_with("minecraft:waxed_"); + if item.ends_with("_axe") || (item == "minecraft:honeycomb" && !waxed) { + return InteractionResult::Pass; + } + let mut sim = self.sim(level, ticks); + sim.sound( + pos, + &"minecraft:entity.copper_golem_become_statue".into(), + 1.0, + 1.0, + ); + sim.set_block_and_update(pos, self.table.cycle(state, self.props.copper_golem_pose)); + sim.game_event(pos, "minecraft:block_change"); + InteractionResult::Success + } } diff --git a/tools/test_use_item_on_parity.py b/tools/test_use_item_on_parity.py index 89b463116..eabdd04eb 100644 --- a/tools/test_use_item_on_parity.py +++ b/tools/test_use_item_on_parity.py @@ -2,8 +2,9 @@ """Compare a player's right click on a block with vanilla's, for items that are both food and blocks (sweet and glow berries, carrots, potatoes), for sweet berry bushes, for spawn eggs used on a monster spawner, for shears carving -pumpkins, for blocks placed where mobs and armor stands stand, for respawn -anchors and for beds in the Nether and the End. +pumpkins, for copper golem statues turned to their next pose, for blocks +placed where mobs and armor stands stand, for respawn anchors and for beds in +the Nether and the End. Every case has a block at the origin, something above it, and a player three blocks south, survival or creative, hungry or full, sneaking or not, who @@ -228,6 +229,32 @@ def pumpkin_case(rng, states, case): case['cells'] = cells +STATUES = [f'{wax}{age}copper_golem_statue' for wax in ('', 'waxed_') + for age in ('', 'exposed_', 'weathered_', 'oxidized_')] + + +def statue_case(rng, states, case): + """A copper golem statue right-clicked (`CopperGolemStatueBlock.useItemOn`, the weathering + one's): anything but an axe, an empty hand too, turns it to its next pose; honeycomb waxes a + statue that still weathers instead (`HoneycombItem.useOn`), and an axe scrapes or unwaxes it.""" + statue = rng.choice(STATUES) + case['cells'] = [[0, 0, 0, states.any(rng, statue)]] + items = ['-', '-', 'stick', 'honeycomb', 'honeycomb', 'stone', 'copper_ingot', 'diamond_pickaxe', + 'copper_golem_statue'] + if statue != 'copper_golem_statue': + # An axe scrapes or unwaxes the others; on the unaffected one it brings the golem back + # (`CopperGolemStatueBlockEntity.removeStatue`), which Horizon does not do yet. + items.append('iron_axe') + item = rng.choice(items) + use = {'item': '-' if item == '-' else d_plain(item, rng.choice([1, 16])), 'creative': rng.random() < 0.2, + 'sneaking': rng.random() < 0.15, 'food': 20, + 'face': rng.choice(['up', 'up', 'north', 'south', 'west', 'east'])} + if rng.random() < 0.2: + # The item in the off hand, nothing in the main hand. + use.update(offhand=use['item'], item='-') + case['use_item'] = use + + # Entities a block may be placed into (`Entity.blocksBuilding`), and blocks to place. BLOCKERS = ['skeleton', 'zombie', 'pig', 'chicken', 'cow', 'armor_stand', 'slime', 'bat'] PLACED = ['obsidian', 'stone', 'oak_slab', 'oak_fence', 'white_carpet', 'torch', 'glass', 'oak_trapdoor', 'snow'] @@ -290,6 +317,12 @@ def cases(facts, count, seed): case['mode'] = 'dispense' yield case continue + if rng.random() < 0.08: + statue_case(rng, states, case) + case['items'] = [] + case['mode'] = 'dispense' + yield case + continue if rng.random() < 0.12: pumpkin_case(rng, states, case) case['items'] = [] From bdbc816486ea2bc8aa3362a8547be73af897a9a1 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 18:53:20 +0100 Subject: [PATCH 138/238] Creative search: match the localized hover name, not the item's base name The base name (Tipped Arrow, Potion, White Banner) matched where vanilla's first tooltip line (Arrow of Poison, Water Bottle, Ominous Banner) doesn't: the creative harness found extra results for 'e', 'potion', 'tipped' and 'white'. name_line builds the full hover name through display_get. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/creative.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/crates/horizon-client/src/creative.rs b/crates/horizon-client/src/creative.rs index 593aad61d..249e40b11 100644 --- a/crates/horizon-client/src/creative.rs +++ b/crates/horizon-client/src/creative.rs @@ -346,10 +346,16 @@ impl Creative { } // FullTextSearchTree: tooltip lines, and the item id after a colon. // The export's lines are English (they come from the vanilla jars); - // the display name in the selected language is matched too, so a - // localized search finds the item. + // the hover name in the selected language is matched too, so a + // localized search finds the item. It is the whole name (a tipped + // arrow's potion, the ominous banner), as the first line is. let text = java_lower(term); - let localized = |i: usize| java_lower(&names.display_item(&self.search_items[i].item)); + let localized = |i: usize| { + java_lower(&crate::item_tooltip::name_line( + &self.search_items[i], + &|k| names.display_get(k), + )) + }; all.filter(|&i| { let e = &self.search_index[i]; let fallback; From 90eda915d5dec7d02c929ec867262bfe867d396f Mon Sep 17 00:00:00 2001 From: Joansitoh Date: Sun, 25 Oct 2026 18:58:52 +0100 Subject: [PATCH 139/238] Add a lore tooltip harness against ItemLore.addToTooltip. The styled lines have to keep matching vanilla, including an explicit non-italic span and a lore component hidden by tooltip_display. --- .../examples/probe/lore_tooltip_fixture.rs | 53 +++++++++++++++++ crates/horizon-client/examples/probe/main.rs | 2 + .../parity/VanillaLoreTooltipOracle.java | 58 +++++++++++++++++++ tools/test_lore_tooltip_parity.py | 41 +++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 crates/horizon-client/examples/probe/lore_tooltip_fixture.rs create mode 100644 mods/horizon/src/test/java/dev/horizonlab/parity/VanillaLoreTooltipOracle.java create mode 100644 tools/test_lore_tooltip_parity.py diff --git a/crates/horizon-client/examples/probe/lore_tooltip_fixture.rs b/crates/horizon-client/examples/probe/lore_tooltip_fixture.rs new file mode 100644 index 000000000..350d9be81 --- /dev/null +++ b/crates/horizon-client/examples/probe/lore_tooltip_fixture.rs @@ -0,0 +1,53 @@ +//! Lore tooltip companion for tools/test_lore_tooltip_parity.py: the spans +//! `item_tooltip::lore_texts` gives each stack (SNBT, one per line). +use horizon_client::{ + item_data::parse_stack, + item_tooltip::{TooltipDisplay, lore_texts}, +}; +use std::{error::Error, fmt::Write}; + +pub fn main() -> Result<(), Box> { + let args: Vec = crate::args().collect(); + let input = std::fs::read_to_string(&args[1])?; + let mut output = String::new(); + for line in input + .lines() + .filter(|l| !l.is_empty() && !l.starts_with('#')) + { + let stack = parse_stack(line)?.ok_or("empty stack")?; + let display = TooltipDisplay::for_stack(&stack); + if !display.shows("minecraft:lore") { + writeln!(output, "-")?; + continue; + } + let lines = lore_texts(&stack); + if lines.is_empty() { + writeln!(output, "-")?; + continue; + } + let mut rendered = Vec::new(); + for text in lines { + let mut spans = Vec::new(); + for span in &text.spans { + if span.text.is_empty() { + continue; + } + let style = &span.style; + spans.push(format!( + "{}\t{}\t{}\t{}\t{}\t{}\t{}", + span.text, + style.color.map(|c| c as i32).unwrap_or(-1), + style.bold == Some(true), + style.italic == Some(true), + style.underlined == Some(true), + style.strikethrough == Some(true), + style.obfuscated == Some(true), + )); + } + rendered.push(spans.join(" | ")); + } + writeln!(output, "{}", rendered.join(" || "))?; + } + std::fs::write(&args[2], output)?; + Ok(()) +} diff --git a/crates/horizon-client/examples/probe/main.rs b/crates/horizon-client/examples/probe/main.rs index f05c2a662..e80088fd0 100644 --- a/crates/horizon-client/examples/probe/main.rs +++ b/crates/horizon-client/examples/probe/main.rs @@ -49,6 +49,7 @@ mod input_probe; mod interaction_input_fixture; mod lan_probe; mod light_parity; +mod lore_tooltip_fixture; mod marker_particles_probe; mod menu_fixture; mod mipmap_probe; @@ -133,6 +134,7 @@ probes! { interaction_input_fixture, lan_probe, light_parity, + lore_tooltip_fixture, marker_particles_probe, menu_fixture, mipmap_probe, diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaLoreTooltipOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaLoreTooltipOracle.java new file mode 100644 index 000000000..462a8016a --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaLoreTooltipOracle.java @@ -0,0 +1,58 @@ +package dev.horizonlab.parity; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Optional; +import net.minecraft.core.component.DataComponents; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.Style; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.TooltipFlag; +import net.minecraft.world.item.component.ItemLore; +import net.minecraft.world.item.component.TooltipDisplay; + +/** + * The styled lines {@code ItemLore.addToTooltip} adds, one stack (SNBT) per input + * line. Each line is its visited spans ({@code text, colour, bold, italic, + * underlined, strikethrough, obfuscated}); lines are separated by {@code ||}. + */ +public final class VanillaLoreTooltipOracle { + public static void main(String[] args) throws Exception { + ParityData.init(); + var context = Item.TooltipContext.of(ParityData.lookup); + var output = new ArrayList(); + for (String line : Files.readAllLines(Path.of(args[0]))) { + if (line.isBlank() || line.startsWith("#")) continue; + var stack = ParityData.stack(line); + var display = stack.getOrDefault(DataComponents.TOOLTIP_DISPLAY, TooltipDisplay.DEFAULT); + ItemLore lore = stack.get(DataComponents.LORE); + if (lore == null || !display.shows(DataComponents.LORE)) { + output.add("-"); + continue; + } + var lines = new ArrayList(); + lore.addToTooltip(context, component -> lines.add(spans(component)), TooltipFlag.NORMAL, stack); + output.add(lines.isEmpty() ? "-" : String.join(" || ", lines)); + } + Files.write(Path.of(args[1]), output); + } + + /** {@code Component.visit} from {@code Style.EMPTY}: the style a tooltip draws. */ + static String spans(Component component) { + var out = new StringBuilder(); + component.visit((style, text) -> { + if (text.isEmpty()) return Optional.empty(); + if (out.length() > 0) out.append(" | "); + int color = style.getColor() == null ? -1 : style.getColor().getValue(); + out.append(text).append('\t').append(color).append('\t') + .append(style.isBold()).append('\t') + .append(style.isItalic()).append('\t') + .append(style.isUnderlined()).append('\t') + .append(style.isStrikethrough()).append('\t') + .append(style.isObfuscated()); + return Optional.empty(); + }, Style.EMPTY); + return out.toString(); + } +} diff --git a/tools/test_lore_tooltip_parity.py b/tools/test_lore_tooltip_parity.py new file mode 100644 index 000000000..d262a3542 --- /dev/null +++ b/tools/test_lore_tooltip_parity.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +"""Compare item lore tooltip styles with the pinned vanilla client. + +Vanilla prints what ``ItemLore.addToTooltip`` adds after ``ComponentUtils.mergeStyles`` +with dark purple italic (VanillaLoreTooltipOracle). Horizon prints what +``item_tooltip::lore_texts`` gives (examples/probe/lore_tooltip_fixture). +""" +try: + from . import lab, oracle_harness as harness +except ImportError: + import lab, oracle_harness as harness + +# SNBT stacks. A lore line is a text component; italic:0b is an explicit false. +CASES = [ + '{id:"minecraft:stick",count:1}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'"Plain"\']}}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'{"text":"Gold","color":"gold","italic":0b}\']}}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'{"text":"A","extra":[{"text":"B","color":"aqua","italic":0b}]}\']}}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'"One"\',\'{"text":"Two","bold":1b}\']}}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'"Hidden"\'],"minecraft:tooltip_display":{hidden_components:["minecraft:lore"]}}}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'{"text":"Line","underlined":1b,"strikethrough":1b}\']}}', +] + + +def cases(build): + path = build / 'cases.txt' + path.write_text(''.join(line + '\n' for line in CASES)) + return path, len(CASES) + + +if __name__ == '__main__': + harness.differential( + 'lore-tooltip-parity', 'VanillaLoreTooltipOracle', 'lore_tooltip_fixture', cases, + 'Item lore tooltip lines: dark purple italic, child colours, explicit non-italic, and tooltip_display', + sources=[ + lab.ROOT / 'crates/horizon-client/src/item_tooltip.rs', + lab.ROOT / 'crates/horizon-client/src/item_data.rs', + lab.ROOT / 'crates/horizon-client/src/text.rs', + lab.ROOT / 'crates/horizon-client/src/component.rs', + ], + expected_lines=len(CASES)) From b3840adccdaf48f9c04f9e67f5b4decf74fd2545 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 25 Oct 2026 18:08:03 +0000 Subject: [PATCH 140/238] Fix renderer backend fallback after surface creation failure Keep surface creation errors in the backend failure list so Windows Auto can try DirectX 12 after Vulkan fails. Co-Authored-By: OpenAI Codex --- crates/horizon-client/src/gfx.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 31853ad92..2c54a0e53 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -685,7 +685,7 @@ fn instance_descriptor(backends: wgpu::Backends) -> wgpu::InstanceDescriptor { /// device. `api` is the player's choice (`GraphicsApi::with_env` applied); /// `wgpu_backend`: whether `WGPU_BACKEND` is set, which picks the backends /// itself. Only Windows has two: Auto tries Vulkan, then DirectX 12 when -/// Vulkan has no adapter (no Vulkan driver) or no device. A native crash in +/// Vulkan has no surface, adapter (no Vulkan driver) or device. A native crash in /// the driver can't be caught in the game; the launcher switches the setting /// to DirectX 12 after one at the renderer's start (#746). fn backend_order(api: GraphicsApi, windows: bool, wgpu_backend: bool) -> Vec { @@ -793,7 +793,13 @@ async fn open_device( let surface = if offscreen { None } else { - Some(instance.create_surface(window.clone())?) + match instance.create_surface(window.clone()) { + Ok(surface) => Some(surface), + Err(e) => { + failures.push(format!("{name} surface: {e}")); + continue; + } + } }; let adapter = match instance .request_adapter(&wgpu::RequestAdapterOptions { From 2c8930c48da0ba6c17db31dcaf09dea9a1465b4f Mon Sep 17 00:00:00 2001 From: vlesmentie <129341611+vlesmentie@users.noreply.github.com> Date: Sun, 25 Oct 2026 15:16:32 -0300 Subject: [PATCH 141/238] Chickens flap while they fall on a client of a server Chicken.aiStep updates flap and flapSpeed on the client too, for the chickens it only displays. tick_remote_entities never did, so with single player on its server thread (and on servers) a falling chicken kept its wings folded. The wing part of chicken_ai_step is now chicken_flap, and the remote tick runs it from the server's on-ground flag; the falling slowdown and egg laying stay the server's. Fixes #1706. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/remote.rs | 4 + crates/horizon-client/src/mobs/farm.rs | 111 +++++++++++++++++++++-- 2 files changed, 106 insertions(+), 9 deletions(-) diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 620d145bf..b3a2b73ce 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -3572,6 +3572,10 @@ impl Game<'_> { m.head_yaw += wrap_degrees(*yaw - m.head_yaw) / *steps as f32; *steps -= 1; } + // Chicken.aiStep: the wings follow the server's on-ground flag. + if m.species() == horizon_client::mobs::Species::Chicken { + horizon_client::mobs::farm::chicken_flap(m); + } // LivingEntity.updatingUsingItem for another player: the ticks the // item has been used before this one. if player && m.extra.using_item > 0 { diff --git a/crates/horizon-client/src/mobs/farm.rs b/crates/horizon-client/src/mobs/farm.rs index bf886cb5a..aa02c901d 100644 --- a/crates/horizon-client/src/mobs/farm.rs +++ b/crates/horizon-client/src/mobs/farm.rs @@ -69,18 +69,10 @@ pub fn register(m: &mut Mob) { } /// Chicken.aiStep: wing flapping, slowed falling and laying eggs. pub fn chicken_ai_step(m: &mut Mob, _w: &MobWorld, events: &mut Vec) { - let a = &mut m.anim; - a.flap_o = a.flap; - a.flap_speed_o = a.flap_speed; - a.flap_speed = (a.flap_speed + if m.on_ground { -1.0 } else { 4.0 } * 0.3).clamp(0.0, 1.0); - if !m.on_ground && a.flapping < 1.0 { - a.flapping = 1.0; - } - a.flapping *= 0.9; + chicken_flap(m); if !m.on_ground && m.velocity.y < 0.0 { m.velocity.y *= 0.6; } - a.flap += a.flapping * 2.0; if m.alive() && !m.is_baby() && !m.extra.jockey { m.extra.egg_time -= 1; if m.extra.egg_time <= 0 { @@ -95,6 +87,21 @@ pub fn chicken_ai_step(m: &mut Mob, _w: &MobWorld, events: &mut Vec) { } } } +/// Chicken.aiStep's wings: `flapSpeed` rises in the air and falls on the +/// ground, `flapping` restarts at 1 in the air and decays, and `flap` turns +/// by it. It runs on a client too, for the chickens it only displays (the +/// falling slowdown and the eggs are the server's). +pub fn chicken_flap(m: &mut Mob) { + let a = &mut m.anim; + a.flap_o = a.flap; + a.flap_speed_o = a.flap_speed; + a.flap_speed = (a.flap_speed + if m.on_ground { -1.0 } else { 4.0 } * 0.3).clamp(0.0, 1.0); + if !m.on_ground && a.flapping < 1.0 { + a.flapping = 1.0; + } + a.flapping *= 0.9; + a.flap += a.flapping * 2.0; +} /// Farm animal climate from the biome tags (`spawns_warm/cold_variant_farm_animals`). #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] pub enum Climate { @@ -125,3 +132,89 @@ pub fn sheep_color(climate: Climate, random: &mut impl RandomSource) -> u8 { 6 } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::mobs::{Anim, Brain, Flags, MobType, Part, Species}; + use horizon_core::geometry::Vec3; + use serde_json::json; + use std::collections::HashMap; + + fn chicken() -> Mob { + let model = Arc::new(Part::parse( + "root", + &json!({"children": {"right_wing": {}, "left_wing": {}}}), + )); + let kind = Arc::new(MobType { + id: "minecraft:chicken".into(), + species: Species::Chicken, + width: 0.4, + height: 0.7, + eye_height: 0.644, + category: "creature".into(), + max_health: 4.0, + speed: 0.25, + 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, + layers: HashMap::new(), + texture: String::new(), + brain: Brain::Passive, + anim: Anim::Chicken, + sounds: [""; 3], + step_sound: None, + food: Arc::default(), + tempt: Arc::default(), + }); + Mob::new(1, kind, Vec3::ZERO, 0.0) + } + + #[test] + fn a_falling_chicken_flaps_and_a_landed_one_folds_its_wings() { + let mut m = chicken(); + m.on_ground = true; + chicken_flap(&mut m); + assert_eq!(m.anim.flap_speed, 0.0); + assert_eq!(m.anim.flap, 0.0); + // In the air flapSpeed rises by 1.2 (clamped to 1), flapping restarts + // at 1 and decays by 0.9, and flap turns by twice flapping. + m.on_ground = false; + chicken_flap(&mut m); + assert_eq!(m.anim.flap_speed, 1.0); + assert_eq!(m.anim.flapping, 0.9); + assert_eq!(m.anim.flap, 1.8); + chicken_flap(&mut m); + assert_eq!(m.anim.flap_o, 1.8); + assert_eq!(m.anim.flap_speed_o, 1.0); + assert_eq!(m.anim.flap, 3.6); + // Landed, flapSpeed falls by 0.3 a tick and flapping decays. + m.on_ground = true; + chicken_flap(&mut m); + assert!((m.anim.flap_speed - 0.7).abs() < 1.0e-6); + assert!((m.anim.flapping - 0.81).abs() < 1.0e-6); + } +} From 96199c23e7ba28ec666bff1309ce6a075a058c17 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 19:20:17 +0100 Subject: [PATCH 142/238] Shader packs, clean room 41: entities and block entities in the shadow pass shadow_entities and shadow_block (each falling back to shadow) load with the entities' vertex inputs unless shadowEntities or shadowBlockEntities is false (spec 2.4, 3.2; both on by default, OQ-19). They draw the same entity and block-entity runs the gbuffers programs would, with the same per-draw uniforms, into the shadow map after the solid terrain and before the shadowtex1 copy. A run is uploaded when a gbuffers program or a shadow program draws it; Horizon still draws the runs no gbuffers program covers. Without a pack nothing changes: only the pack's entity code is touched. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/src/gfx/pack_entities.rs | 35 +++++--- crates/horizon-client/src/gfx/pack_post.rs | 85 +++++++++++++++++-- crates/horizon-shaders/src/post.rs | 70 +++++++++++---- crates/horizon-shaders/tests/real_packs.rs | 4 +- 4 files changed, 160 insertions(+), 34 deletions(-) diff --git a/crates/horizon-client/src/gfx/pack_entities.rs b/crates/horizon-client/src/gfx/pack_entities.rs index ffc55149f..30fa70880 100644 --- a/crates/horizon-client/src/gfx/pack_entities.rs +++ b/crates/horizon-client/src/gfx/pack_entities.rs @@ -161,8 +161,12 @@ pub(super) struct Draw { /// The first vertex. pub base: u32, pub quads: u32, - /// The program that draws it (`ENTITY`, `BLOCK`, `TRANSLUCENT`, `PARTICLE`). + /// The kind of program that draws it (`ENTITY`, `BLOCK`, `TRANSLUCENT`, `PARTICLE`). pub program: usize, + /// The pack's gbuffers program draws it (else Horizon does). + pub pack: bool, + /// The pack's shadow program draws it into the shadow map too. + pub shadow: bool, pub slot: u32, } @@ -280,7 +284,8 @@ impl PackEntities { /// Splits `entities` between the pack, which draws the runs of `spans` its programs cover /// (`drawn`: whether it has `gbuffers_entities`, `gbuffers_block` or their fallbacks), and - /// Horizon, which draws the rest (`rest`). Uploads the pack's part. + /// Horizon, which draws the rest (`rest`). Uploads the pack's part, and the runs its shadow + /// programs draw (`shadow`: entities, block entities). #[allow(clippy::too_many_arguments)] pub fn prepare( &mut self, @@ -290,6 +295,7 @@ impl PackEntities { entities: &[Vertex], spans: &[EntitySpan], drawn: [bool; PROGRAMS], + shadow: [bool; 2], rest: &mut Vec, ) { self.draws.clear(); @@ -300,13 +306,15 @@ impl PackEntities { rest.extend_from_slice(&entities[..first.min(entities.len())]); for (span, range) in runs(entities.len(), spans) { let run = &entities[range]; - let Some(program) = drawn_by_pack(&span, drawn).filter(|_| plain(run)) else { + let program = program_of(&span).filter(|_| plain(run)); + let pack = program.is_some_and(|p| drawn[p]); + let casts = program.is_some_and(|p| shadow.get(p).copied().unwrap_or(false)); + if !pack { rest.extend_from_slice(run); + } + let Some(program) = program.filter(|_| (pack || casts) && !run.is_empty()) else { continue; }; - if run.is_empty() { - continue; - } let start = self.scratch.len() as u32; self.scratch.extend_from_slice(run); let (entity_id, block_id) = match program { @@ -327,6 +335,8 @@ impl PackEntities { base: start, quads: run.len() as u32 / 4, program, + pack, + shadow: casts, slot: self.draws.len() as u32, }); } @@ -388,8 +398,14 @@ pub const PROGRAMS: usize = 4; /// Which program draws `span`'s run, given which of them (or their fallbacks) the pack has; /// `None`: Horizon draws it. -pub fn drawn_by_pack(span: &EntitySpan, drawn: [bool; PROGRAMS]) -> Option { - let program = if span.stage == render_stage("ENTITIES") { +#[cfg(test)] +fn drawn_by_pack(span: &EntitySpan, drawn: [bool; PROGRAMS]) -> Option { + program_of(span).filter(|&p| drawn[p]) +} + +/// The kind of program that draws `span`'s run, when a pack may. +pub fn program_of(span: &EntitySpan) -> Option { + Some(if span.stage == render_stage("ENTITIES") { if span.translucent { TRANSLUCENT } else { @@ -401,8 +417,7 @@ pub fn drawn_by_pack(span: &EntitySpan, drawn: [bool; PROGRAMS]) -> Option; PROGRAMS], late: [bool; PROGRAMS], + /// What draws entities and block entities into the shadow map (2.4). + shadow: [Option; 2], +} + +impl Entities { + /// Whether a gbuffers program draws some of them (else only the shadow pass does). + fn drawn(&self) -> bool { + self.passes.iter().any(Option::is_some) + } } #[derive(Clone)] @@ -1077,8 +1086,8 @@ impl PackPost { post.particles, ]; let late = [false, false, post.translucent_late, post.particles_late]; - let entity_vertices = programs - .iter() + let shadow_programs = [post.shadow_entities, post.shadow_block]; + let entity_vertices = (programs.iter().chain(&shadow_programs)) .any(Option::is_some) .then(|| PackEntities::new(device, &draws.layout)); let entity_buffers = super::pack_entities::buffers(); @@ -1094,12 +1103,17 @@ impl PackPost { p.zip(entity_geometry.as_ref()) .and_then(|(p, g)| build(p, Some(g))) }); + let shadow_entities = shadow_programs.map(|p| { + p.zip(entity_geometry.as_ref()) + .and_then(|(p, g)| build(p, Some(g))) + }); let entities = entity_vertices - .filter(|_| entity_passes.iter().any(Option::is_some)) + .filter(|_| (entity_passes.iter().chain(&shadow_entities)).any(Option::is_some)) .map(|geometry| Entities { geometry, passes: entity_passes, late, + shadow: shadow_entities, }); let cloud_geometry = world.clouds.map(|layout| Geometry { buffers: &entity_buffers, @@ -1204,6 +1218,15 @@ impl PackPost { notes.push(format!("{part} draw with {}", p.name)); } } + // Timed (and labelled) apart from the terrain's shadow pass, which may run the same + // program. + let mut entities = entities; + for (part, pass) in (entities.iter_mut()).flat_map(|e| parts.iter().zip(&mut e.shadow)) { + if let Some(p) = pass { + notes.push(format!("{part} cast shadows with {}", p.name)); + p.name.push_str(&format!(" ({part})")); + } + } let depth1 = water.is_some() && passes .iter() @@ -1220,6 +1243,7 @@ impl PackPost { .chain(shadow_terrain.iter().chain(&shadow_water)) .chain(sky.iter().flat_map(|s| s.basic.iter().chain(&s.textured))) .chain(entities.iter().flat_map(|e| e.passes.iter().flatten())) + .chain(entities.iter().flat_map(|e| e.shadow.iter().flatten())) .chain(&clouds) .chain(weather.iter().flatten()) .chain(hand.iter().map(|h| &h.0)) @@ -1277,7 +1301,7 @@ impl PackPost { let blit_depth = terrain.is_some() || water.is_some() || sky.is_some() - || entities.is_some() + || entities.as_ref().is_some_and(Entities::drawn) || clouds.is_some() || weather.is_some() || hand.is_some() @@ -1775,7 +1799,7 @@ impl PackPost { let mut draws = geometry .draws .iter() - .filter(|d| d.program == program) + .filter(|d| d.pack && d.program == program) .peekable(); if draws.peek().is_none() { continue; @@ -1800,6 +1824,46 @@ impl PackPost { self.entities = Some(entities); } + /// Entities and block entities into the shadow map, each run with the per-draw uniforms + /// of its gbuffers draw (2.4): opaque, so before the `shadowtex1` copy. + fn draw_shadow_entities( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + size: [u32; 2], + ) { + let Some(mut entities) = self.entities.take() else { + return; + }; + let geometry = &entities.geometry; + for (program, pass) in entities.shadow.iter_mut().enumerate() { + let Some(pass) = pass else { + continue; + }; + let mut draws = (geometry.draws.iter()) + .filter(|d| d.shadow && d.program == program) + .peekable(); + if draws.peek().is_none() { + continue; + } + let group = self.bind(device, queue, pass, size); + let mut rp = self.begin(encoder, pass); + rp.set_pipeline(&pass.pipeline); + rp.set_bind_group(0, &group, &[]); + rp.set_bind_group(1, &geometry.table_group, &[]); + rp.set_vertex_buffer(0, geometry.vertices()); + rp.set_index_buffer(geometry.indices.slice(..), wgpu::IndexFormat::Uint32); + for d in draws { + if pass.draw { + rp.set_bind_group(2, &geometry.group, &[geometry.offset(d.slot)]); + } + rp.draw_indexed(0..d.quads * 6, d.base as i32, 0..1); + } + } + self.entities = Some(entities); + } + /// Splits `entities` (`Frame::entities`) between the pack and Horizon by its runs /// (`spans`): the pack's part is uploaded for this frame's draw, Horizon's goes to `rest`. pub fn prepare_entities( @@ -1816,6 +1880,11 @@ impl PackPost { return; }; let drawn = e.passes.each_ref().map(Option::is_some); + // Only while the shadow pass draws this frame. + let shadow = e + .shadow + .each_ref() + .map(|p| p.is_some() && self.shadow_targets.is_some()); e.geometry.prepare( device, queue, @@ -1823,6 +1892,7 @@ impl PackPost { entities, spans, drawn, + shadow, rest, ); } @@ -2029,7 +2099,6 @@ impl PackPost { } } - /// Whether the pack draws terrain itself. /// What the shadow camera draws of the terrain this frame, when the pack draws it. pub fn shadow_view(&self, frame: &super::Frame) -> Option { self.shadow_targets.as_ref()?; @@ -2044,6 +2113,7 @@ impl PackPost { }) } + /// Whether the pack draws terrain itself. pub fn has_terrain(&self) -> bool { self.terrain.is_some() } @@ -2187,6 +2257,7 @@ impl PackPost { TerrainList::ShadowSolid, terrain, ); + self.draw_shadow_entities(device, queue, encoder, size); let t = self.shadow_targets.as_mut().expect("shadow pass"); t.clear_rest(encoder); t.copy_depth1(encoder); @@ -2395,7 +2466,7 @@ impl PackPost { let mut shown = false; let mut terrain_drawn = self.terrain.is_none() && self.sky.is_none() - && self.entities.is_none() + && !self.entities.as_ref().is_some_and(Entities::drawn) && self.clouds.is_none() && self.hand.is_none(); let late_entities = self.entities.as_ref().is_some_and(|e| { diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs index dd062b508..715dca257 100644 --- a/crates/horizon-shaders/src/post.rs +++ b/crates/horizon-shaders/src/post.rs @@ -248,6 +248,10 @@ pub struct Post { /// translucent terrain (`shadow_water` or `shadow`), into the shadow buffers (2.4). pub shadow: Option, pub shadow_water: Option, + /// What draws entities (`shadow_entities` or `shadow`) and block entities (`shadow_block` or + /// `shadow`) from the light, unless `shadowEntities`/`shadowBlockEntities` is false. + pub shadow_entities: Option, + pub shadow_block: Option, /// `shadowcomp`..`shadowcomp99`, after the shadow pass (2.1). pub shadow_comp: Vec, /// Translucent entities draw after deferred (`separateEntityDraws`, 3.2). @@ -317,6 +321,8 @@ pub fn load_post( shadow_pass: false, shadow: None, shadow_water: None, + shadow_entities: None, + shadow_block: None, shadow_comp: Vec::new(), translucent_late: false, particles_late: false, @@ -472,14 +478,23 @@ pub fn load_post( Some(_) => true, None => !flag("particles.before.deferred").unwrap_or(false), }; - load_shadow(&mut loader, inputs.terrain.as_ref()); + load_shadow( + &mut loader, + inputs.terrain.as_ref(), + inputs.entities.as_ref(), + ); post.config = std::mem::take(&mut post.config).finish(); post } /// Whether the shadow pass runs (2.5) and, when it does, its programs: `shadow_solid` and /// `shadow_cutout` are not used (2.4, Legacy); the game's solid and cutout terrain is one draw. -fn load_shadow(loader: &mut Loader<'_>, terrain: Option<&GbuffersInputs>) { +/// `shadow_entities` and `shadow_block` read the entities' vertices (`entities`). +fn load_shadow( + loader: &mut Loader<'_>, + terrain: Option<&GbuffersInputs>, + entities: Option<&GbuffersInputs>, +) { let post = &*loader.post; let gbuffers = [ &post.terrain, @@ -516,11 +531,9 @@ fn load_shadow(loader: &mut Loader<'_>, terrain: Option<&GbuffersInputs>) { }); loader.post.shadow_comp.extend(program); } - let Some(inputs) = terrain else { - return; - }; let properties = loader.properties; - let mut load = |names: &[&str]| { + let mut load = |names: &[&str], inputs: Option<&GbuffersInputs>| { + let inputs = inputs?; names.iter().find_map(|name| { let p = loader.load(name.to_string(), Group::Shadow, |v, f| { translate_shadow(v, f, inputs) @@ -531,13 +544,22 @@ fn load_shadow(loader: &mut Loader<'_>, terrain: Option<&GbuffersInputs>) { }) }) }; - // `shadowTerrain` and `shadowTranslucent` (3.2) leave terrain out of the shadow pass. + // `shadowTerrain`, `shadowTranslucent`, `shadowEntities` and `shadowBlockEntities` (3.2) + // leave their part out of the shadow pass; all are on by default (OQ-19). let keep = |key: &str| properties.get(key).is_none_or(|v| v.trim() != "false"); - let shadow = keep("shadowTerrain").then(|| load(&["shadow"])).flatten(); - let water = keep("shadowTranslucent") - .then(|| load(&["shadow_water", "shadow"])) + let shadow = keep("shadowTerrain") + .then(|| load(&["shadow"], terrain)) .flatten(); - if shadow.is_none() { + let water = keep("shadowTranslucent") + .then(|| load(&["shadow_water", "shadow"], terrain)) + .flatten(); + let shadow_entities = keep("shadowEntities") + .then(|| load(&["shadow_entities", "shadow"], entities)) + .flatten(); + let shadow_block = keep("shadowBlockEntities") + .then(|| load(&["shadow_block", "shadow"], entities)) + .flatten(); + if shadow.is_none() && terrain.is_some() { loader .post .notes @@ -545,6 +567,8 @@ fn load_shadow(loader: &mut Loader<'_>, terrain: Option<&GbuffersInputs>) { } loader.post.shadow = shadow; loader.post.shadow_water = water; + loader.post.shadow_entities = shadow_entities; + loader.post.shadow_block = shadow_block; } struct Loader<'a> { @@ -828,14 +852,18 @@ mod tests { ("shaders/shadow.fsh", shadow), ("shaders/shadowcomp1.vsh", VSH), ("shaders/shadowcomp1.fsh", FSH), + ("shaders/shadow_block.vsh", VSH), + ("shaders/shadow_block.fsh", FSH), ]; let pack = Pack::from_zip_bytes("t", zip(&files)).unwrap(); + let game = crate::GbuffersInputs { + declarations: "layout(location = 0) in vec3 hz_v_pos;".into(), + body: " hz_Vertex = vec4(hz_v_pos, 1.0);\n".into(), + ..Default::default() + }; let inputs = GameInputs { - terrain: Some(crate::GbuffersInputs { - declarations: "layout(location = 0) in vec3 hz_v_pos;".into(), - body: " hz_Vertex = vec4(hz_v_pos, 1.0);\n".into(), - ..Default::default() - }), + terrain: Some(game.clone()), + entities: Some(game), ..Default::default() }; let load = |properties: &str| { @@ -854,6 +882,16 @@ mod tests { post.shadow_water.as_ref().map(|p| p.name.as_str()), Some("shadow") ); + // Entities fall back to shadow; block entities have their own program. + let name = |p: &Option| p.as_ref().map(|p| p.name.clone()); + assert_eq!( + (name(&post.shadow_entities), name(&post.shadow_block)), + (Some("shadow".into()), Some("shadow_block".into())) + ); + let off = load("shadowEntities=false\nshadowBlockEntities = false"); + assert!( + off.shadow.is_some() && off.shadow_entities.is_none() && off.shadow_block.is_none() + ); let comp: Vec<_> = post .shadow_comp .iter() diff --git a/crates/horizon-shaders/tests/real_packs.rs b/crates/horizon-shaders/tests/real_packs.rs index 70fcaa55f..52d31fd0e 100644 --- a/crates/horizon-shaders/tests/real_packs.rs +++ b/crates/horizon-shaders/tests/real_packs.rs @@ -180,11 +180,13 @@ fn real_packs_read() { }; let post = load_post(&pack, &folder, &shaders, &Macros::standard(), &inputs); println!( - " dimension {id} (folder `{folder}`): {} post programs, shadow pass {} ({:?}, {:?}, {} shadowcomp), terrain {:?}, water {:?}, sky {:?} {:?}, entities {:?} {:?}, {} notes, {:.0} ms", + " dimension {id} (folder `{folder}`): {} post programs, shadow pass {} ({:?}, {:?}, {:?}, {:?}, {} shadowcomp), terrain {:?}, water {:?}, sky {:?} {:?}, entities {:?} {:?}, {} notes, {:.0} ms", post.programs.len(), post.shadow_pass, post.shadow.as_ref().map(|t| &t.name), post.shadow_water.as_ref().map(|t| &t.name), + post.shadow_entities.as_ref().map(|t| &t.name), + post.shadow_block.as_ref().map(|t| &t.name), post.shadow_comp.len(), post.terrain.as_ref().map(|t| &t.name), post.water.as_ref().map(|t| &t.name), From f969415c7a91ab19c83c6b3d98d1d6c9950eca6c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 19:36:52 +0100 Subject: [PATCH 143/238] Creative stacks: reuse the wire codec both sides already had Horizon already encoded stacks for the wire (lan::guest::stack_to_network, what a Horizon server sends its clients: damage, enchantments, stored enchantments, repair cost, custom name, dyed colour, trim, maps, charged projectiles, books) and decoded them on the client (remote.rs's protocol_components + item_data::decode_components, every component it has a codec for). The new encode/apply pair covered six components and duplicated both. - The client's SetCreativeModeSlot stack goes through stack_to_network. - The decoder moves into item_sync::from_network, used by the client for what its server sends and by the server (Hub::stack_from_network) for a creative client's slot, so the server now keeps every component the client's decoder reads, not six. - One round-trip test covers encode then decode with registries. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/lan_actions.rs | 17 +- crates/horizon-client/src/game/remote.rs | 198 +-------- crates/horizon-client/src/item_data.rs | 2 +- crates/horizon-client/src/item_sync.rs | 378 ++++++++++++------ crates/horizon-client/src/lan/hub.rs | 11 +- 5 files changed, 270 insertions(+), 336 deletions(-) diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 1b4989055..84633836c 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -1217,21 +1217,8 @@ impl Game<'_> { stack: &Option, ) -> Option { let s = stack.as_ref()?; - let name = host - .hub - .item_name(s.item) - .unwrap_or_else(|| "minecraft:air".into()); - let mut out = ItemStack::new(&name, s.count.max(1) as u32); - // The same components the encoder wrote, so a creative slot keeps what - // the client held instead of just damage and enchantments. - horizon_client::item_sync::apply( - s, - &mut out, - |id| host.hub.enchantment_name(id).map(|n| n.into()), - |id| host.hub.trim_name("minecraft:trim_material", id), - |id| host.hub.trim_name("minecraft:trim_pattern", id), - ); - out.tidy(); + let mut out = host.hub.stack_from_network(s); + out.count = out.count.max(1); Some(out) } diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 55d259c1a..2c6ab4a26 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -223,30 +223,6 @@ fn face_direction(face: [i32; 3]) -> u8 { } } -/// A `minecraft:profile` component as the item data JSON the model reads. -fn profile_tag(p: &horizon_protocol::item::Profile) -> Tag { - let properties = p - .properties - .iter() - .map(|(name, value, signature)| { - let mut entries: Vec<(&'static str, Tag)> = vec![ - ("name", Tag::String(name.as_str().into())), - ("value", Tag::String(value.as_str().into())), - ]; - if let Some(signature) = signature { - entries.push(("signature", Tag::String(signature.as_str().into()))); - } - Tag::compound(entries) - }) - .collect::>(); - let mut entries: Vec<(&'static str, Tag)> = Vec::new(); - if let Some(name) = &p.name { - entries.push(("name", Tag::String(name.as_str().into()))); - } - entries.push(("properties", Tag::List(properties))); - Tag::compound(entries) -} - /// A protocol number format as the scoreboard's. fn number_format(f: &play::NumberFormat) -> horizon_client::scoreboard::NumberFormat { use horizon_client::scoreboard::{NumberFormat, tag_to_json}; @@ -610,168 +586,26 @@ impl Game<'_> { .and_then(|i| i.id(name)) .unwrap_or(0) } + /// A stack from the server, decoded as the saved and command item NBT + /// are, so tooltips and models see every field. pub(super) fn to_stack(&self, s: &Option) -> Option { - let s = s.as_ref()?; - let mut out = ItemStack { - item: self.item_name(s.item), - count: s.count.max(0) as u32, - damage: 0, - extra: None, - }; - // A vanilla `components` compound, then the same decoder the saved - // and command item NBT use, so tooltips and models see every field. - let components = self.protocol_components(s); - let (damage, data) = horizon_client::item_data::decode_components(components.as_ref()); - out.damage = damage; - if let Some(data) = data { - *out.data_mut() = *data; - } - out.tidy(); - Some(out) - } - /// The item's patched components as vanilla's `components` compound. - fn protocol_components(&self, s: &Stack) -> Option { - use horizon_protocol::item::{Component, component}; let r = self.remote.as_ref()?; - let name = |id: i32| r.builtin.get("minecraft:data_component_type")?.name(id); - let mut entries: Vec<(&str, Tag)> = Vec::new(); - for (kind, c) in &s.added { - let Some(key) = name(*kind) else { - continue; - }; - let value = match c { - // Read past, not decoded: the item keeps its prototype's value. - Component::Raw(_) => continue, - // Trim materials and patterns are synced registries, as enchantments are. - Component::Trim { material, pattern } => { - let (Some(material), Some(pattern)) = ( - r.registries.name("minecraft:trim_material", *material), - r.registries.name("minecraft:trim_pattern", *pattern), - ) else { - continue; - }; - Tag::compound([ - ("material", Tag::String(material.into())), - ("pattern", Tag::String(pattern.into())), - ]) - } - Component::Int(v) if *kind == component::RARITY => { - let rarity = ["common", "uncommon", "rare", "epic"]; - Tag::String(rarity.get(*v as usize).copied().unwrap_or("common").into()) - } - Component::Int(v) => Tag::Int(*v), - Component::Unit => Tag::Compound(Default::default()), - Component::Bool(v) => Tag::Byte(i8::from(*v)), - Component::Text(t) | Component::Nbt(t) => t.clone(), - Component::Ident(v) => Tag::String(v.as_str().into()), - Component::Lore(lines) => Tag::List(lines.clone()), - Component::WritableBook(pages) => Tag::compound([( - "pages", - Tag::List( - pages - .iter() - .map(|p| Tag::String(p.as_str().into())) - .collect(), - ), - )]), - Component::WrittenBook(b) => Tag::compound([ - ("title", Tag::String(b.title.as_str().into())), - ("author", Tag::String(b.author.as_str().into())), - ("generation", Tag::Int(b.generation)), - ("pages", Tag::List(b.pages.clone())), - ("resolved", Tag::Byte(i8::from(b.resolved))), - ]), - Component::Enchantments(list) => { - // Enchantments are a synced (data-driven) registry: the - // ids index the server's RegistryData, not the builtin - // report, so a datapack enchantment names correctly. - let levels = list.iter().filter_map(|(id, level)| { - let name = r.registries.name("minecraft:enchantment", *id)?; - Some((name, Tag::Int(*level))) - }); - Tag::compound(levels) - } - Component::AttackRange(v) => Tag::compound([ - ("min_reach", Tag::Float(v[0].to_bits())), - ("max_reach", Tag::Float(v[1].to_bits())), - ("min_attack_momentum", Tag::Float(v[2].to_bits())), - ("max_attack_momentum", Tag::Float(v[3].to_bits())), - ("hitbox_margin", Tag::Float(v[4].to_bits())), - ("mob_factor", Tag::Float(v[5].to_bits())), - ]), - Component::Profile(p) => profile_tag(p), - // ItemStackTemplate.CODEC: id, count and components. - Component::Stacks(stacks) => Tag::List( - stacks - .iter() - .map(|t| { - let mut entries = vec![ - ("id", Tag::String(self.item_name(t.item).as_ref().into())), - ("count", Tag::Int(t.count)), - ]; - if let Some(c) = self.protocol_components(t) { - entries.push(("components", c)); - } - Tag::compound(entries) - }) - .collect(), - ), - Component::CustomModelData { - floats, - flags, - strings, - colors, - } => Tag::compound([ - ( - "floats", - Tag::List(floats.iter().map(|f| Tag::Float(f.to_bits())).collect()), - ), - ( - "flags", - Tag::List(flags.iter().map(|b| Tag::Byte(i8::from(*b))).collect()), - ), - ( - "strings", - Tag::List( - strings - .iter() - .map(|s| Tag::String(s.as_str().into())) - .collect(), - ), - ), - ( - "colors", - Tag::List(colors.iter().map(|c| Tag::Int(*c)).collect()), - ), - ]), - Component::TooltipDisplay { hide, hidden } => { - let hidden = hidden - .iter() - .filter_map(|id| Some(Tag::String(name(*id)?.into()))); - Tag::compound([ - ("hide_tooltip", Tag::Byte(i8::from(*hide))), - ("hidden_components", Tag::List(hidden.collect())), - ]) - } - }; - entries.push((key, value)); - } - Some(Tag::compound(entries)) + Some(horizon_client::item_sync::from_network( + s.as_ref()?, + &r.builtin, + &r.registries, + )) } + /// A stack to the server (`SetCreativeModeSlot`): the same component + /// patch a Horizon server sends its clients, with the synced registries' + /// ids, so the server's echo doesn't hand back a bare item. fn to_network(&self, s: &Option) -> Option { - let s = s.as_ref()?; - let mut stack = Stack::new(self.item_id(&s.item), s.count as i32); - // The components the client models, with the synced registries' ids: - // without them the server's echo hands back a bare item. - if let Some(r) = self.remote.as_ref() { - stack.added.extend(horizon_client::item_sync::encode( - s, - |name| r.registries.id("minecraft:enchantment", name), - |name| r.registries.id("minecraft:trim_material", name), - |name| r.registries.id("minecraft:trim_pattern", name), - )); - } - Some(stack) + let r = self.remote.as_ref()?; + horizon_client::lan::guest::stack_to_network( + |name| self.item_id(name), + |registry, name| r.registries.id(registry, name), + s, + ) } /// A line in the connection's chat, with its styles, click and hover /// events (ChatComponent.addMessage). diff --git a/crates/horizon-client/src/item_data.rs b/crates/horizon-client/src/item_data.rs index 00fd0f074..c07e2f352 100644 --- a/crates/horizon-client/src/item_data.rs +++ b/crates/horizon-client/src/item_data.rs @@ -291,7 +291,7 @@ fn filterable_text(t: &Tag) -> Option { None => read_text(t), } } -pub(crate) fn read_text(t: &Tag) -> Option { +fn read_text(t: &Tag) -> Option { match t { Tag::String(s) => Some(s.to_string_lossy()), // A component's root `text` may be empty with the visible words in diff --git a/crates/horizon-client/src/item_sync.rs b/crates/horizon-client/src/item_sync.rs index 4e48498ac..6ac2e894e 100644 --- a/crates/horizon-client/src/item_sync.rs +++ b/crates/horizon-client/src/item_sync.rs @@ -1,149 +1,277 @@ -//! The data components a stack keeps when it crosses the wire, and how the -//! other side reads them back. A creative slot's packet carries a component -//! patch, and before this the encoder wrote damage alone, so everything else -//! (enchantments, a custom name, trim, repair cost) was gone once the server -//! echoed the inventory back. -use crate::item_data::{Levels, StackData}; +//! Item stacks on the wire (`ItemStack.OPTIONAL_STREAM_CODEC`): the component +//! patch read back as vanilla's `components` compound, so the decoder the saved +//! and command item NBT use fills every field. Both sides use it: a client for +//! what its server sends, a server for a creative client's +//! `SetCreativeModeSlot`. The other direction is `lan::guest::stack_to_network`. use crate::items::ItemStack; -use horizon_protocol::item::{Component, Stack, component}; +use horizon_protocol::item::{Component, Profile, Stack, component}; +use horizon_protocol::registry::{Builtin, Dynamic}; use horizon_world::nbt::Tag; use std::sync::Arc; -/// `Slot.STREAM_CODEC`'s patch for the components Horizon models, with the -/// registries' ids for enchantments and trim. A name the registry does not -/// know is left out: id 0 is a different entry, not a missing one. -pub fn encode( - stack: &ItemStack, - enchantment: impl Fn(&str) -> Option, - trim_material: impl Fn(&str) -> Option, - trim_pattern: impl Fn(&str) -> Option, -) -> Vec<(i32, Component)> { - let mut out = Vec::new(); - if stack.damage > 0 { - out.push((component::DAMAGE, Component::Int(stack.damage as i32))); - } - let Some(d) = stack.data() else { - return out; +/// A stack from the wire: its item by the built-in registry's id, its +/// components by the synced registries' (enchantments, trims). +pub fn from_network(s: &Stack, builtin: &Builtin, synced: &Dynamic) -> ItemStack { + let mut out = ItemStack { + item: item_name(builtin, s.item), + count: s.count.max(0) as u32, + damage: 0, + extra: None, }; - let levels = |levels: &Levels| -> Option { - let list: Vec<(i32, i32)> = levels - .iter() - .filter_map(|(name, level)| Some((enchantment(name)?, *level as i32))) - .collect(); - (!list.is_empty()).then_some(Component::Enchantments(list)) - }; - if let Some(c) = levels(&d.enchantments) { - out.push((component::ENCHANTMENTS, c)); - } - if let Some(c) = levels(&d.stored_enchantments) { - out.push((component::STORED_ENCHANTMENTS, c)); - } - if d.repair_cost > 0 { - out.push((component::REPAIR_COST, Component::Int(d.repair_cost as i32))); - } - if let Some(name) = &d.custom_name { - // A plain string is a valid text component, and `decode_components` - // reads it back as one. - out.push(( - component::CUSTOM_NAME, - Component::Text(Tag::String(name.as_str().into())), - )); - } - if let Some((material, pattern)) = &d.trim - && let (Some(material), Some(pattern)) = (trim_material(material), trim_pattern(pattern)) - { - out.push((component::TRIM, Component::Trim { material, pattern })); + let components = components_tag(s, builtin, synced); + let (damage, data) = crate::item_data::decode_components(components.as_ref()); + out.damage = damage; + if let Some(data) = data { + *out.data_mut() = *data; } + out.tidy(); out } -/// The same patch read back into `out`, the fields `item_data::decode_components` -/// fills from the equivalent `components` compound. -pub fn apply( - stack: &Stack, - out: &mut ItemStack, - enchantment: impl Fn(i32) -> Option>, - trim_material: impl Fn(i32) -> Option>, - trim_pattern: impl Fn(i32) -> Option>, -) { - out.damage = stack.damage().max(0) as u32; - let read_levels = |kind: i32, into: &mut Levels| { - if let Some(Component::Enchantments(list)) = stack.component(kind) { - for (id, level) in list { - if let Some(name) = enchantment(*id) { - into.push((name, (*level).max(0) as u32)); - } +fn item_name(builtin: &Builtin, id: i32) -> Arc { + Arc::from( + builtin + .get("minecraft:item") + .and_then(|i| i.name(id)) + .unwrap_or("minecraft:air"), + ) +} + +/// The item's patched components as vanilla's `components` compound. +fn components_tag(s: &Stack, builtin: &Builtin, synced: &Dynamic) -> Option { + let name = |id: i32| builtin.get("minecraft:data_component_type")?.name(id); + let mut entries: Vec<(&str, Tag)> = Vec::new(); + for (kind, c) in &s.added { + let Some(key) = name(*kind) else { + continue; + }; + let value = match c { + // Read past, not decoded: the item keeps its prototype's value. + Component::Raw(_) => continue, + // Trim materials and patterns are synced registries, as enchantments are. + Component::Trim { material, pattern } => { + let (Some(material), Some(pattern)) = ( + synced.name("minecraft:trim_material", *material), + synced.name("minecraft:trim_pattern", *pattern), + ) else { + continue; + }; + Tag::compound([ + ("material", Tag::String(material.into())), + ("pattern", Tag::String(pattern.into())), + ]) } - } - }; - let data: &mut StackData = out.data_mut(); - read_levels(component::ENCHANTMENTS, &mut data.enchantments); - read_levels( - component::STORED_ENCHANTMENTS, - &mut data.stored_enchantments, - ); - if let Some(Component::Int(v)) = stack.component(component::REPAIR_COST) { - data.repair_cost = (*v).max(0) as u32; + Component::Int(v) if *kind == component::RARITY => { + let rarity = ["common", "uncommon", "rare", "epic"]; + Tag::String(rarity.get(*v as usize).copied().unwrap_or("common").into()) + } + Component::Int(v) => Tag::Int(*v), + Component::Unit => Tag::Compound(Default::default()), + Component::Bool(v) => Tag::Byte(i8::from(*v)), + Component::Text(t) | Component::Nbt(t) => t.clone(), + Component::Ident(v) => Tag::String(v.as_str().into()), + Component::Lore(lines) => Tag::List(lines.clone()), + Component::WritableBook(pages) => Tag::compound([( + "pages", + Tag::List( + pages + .iter() + .map(|p| Tag::String(p.as_str().into())) + .collect(), + ), + )]), + Component::WrittenBook(b) => Tag::compound([ + ("title", Tag::String(b.title.as_str().into())), + ("author", Tag::String(b.author.as_str().into())), + ("generation", Tag::Int(b.generation)), + ("pages", Tag::List(b.pages.clone())), + ("resolved", Tag::Byte(i8::from(b.resolved))), + ]), + Component::Enchantments(list) => { + // Enchantments are a synced (data-driven) registry: the + // ids index the server's RegistryData, not the builtin + // report, so a datapack enchantment names correctly. + let levels = list.iter().filter_map(|(id, level)| { + let name = synced.name("minecraft:enchantment", *id)?; + Some((name, Tag::Int(*level))) + }); + Tag::compound(levels) + } + Component::AttackRange(v) => Tag::compound([ + ("min_reach", Tag::Float(v[0].to_bits())), + ("max_reach", Tag::Float(v[1].to_bits())), + ("min_attack_momentum", Tag::Float(v[2].to_bits())), + ("max_attack_momentum", Tag::Float(v[3].to_bits())), + ("hitbox_margin", Tag::Float(v[4].to_bits())), + ("mob_factor", Tag::Float(v[5].to_bits())), + ]), + Component::Profile(p) => profile_tag(p), + // ItemStackTemplate.CODEC: id, count and components. + Component::Stacks(stacks) => Tag::List( + stacks + .iter() + .map(|t| { + let mut entries = vec![ + ( + "id", + Tag::String(item_name(builtin, t.item).as_ref().into()), + ), + ("count", Tag::Int(t.count)), + ]; + if let Some(c) = components_tag(t, builtin, synced) { + entries.push(("components", c)); + } + Tag::compound(entries) + }) + .collect(), + ), + Component::CustomModelData { + floats, + flags, + strings, + colors, + } => Tag::compound([ + ( + "floats", + Tag::List(floats.iter().map(|f| Tag::Float(f.to_bits())).collect()), + ), + ( + "flags", + Tag::List(flags.iter().map(|b| Tag::Byte(i8::from(*b))).collect()), + ), + ( + "strings", + Tag::List( + strings + .iter() + .map(|s| Tag::String(s.as_str().into())) + .collect(), + ), + ), + ( + "colors", + Tag::List(colors.iter().map(|c| Tag::Int(*c)).collect()), + ), + ]), + Component::TooltipDisplay { hide, hidden } => { + let hidden = hidden + .iter() + .filter_map(|id| Some(Tag::String(name(*id)?.into()))); + Tag::compound([ + ("hide_tooltip", Tag::Byte(i8::from(*hide))), + ("hidden_components", Tag::List(hidden.collect())), + ]) + } + }; + entries.push((key, value)); } - if let Some(Component::Text(t)) = stack.component(component::CUSTOM_NAME) { - data.custom_name = crate::item_data::read_text(t); - } - if let Some(Component::Trim { material, pattern }) = stack.component(component::TRIM) - && let (Some(material), Some(pattern)) = (trim_material(*material), trim_pattern(*pattern)) - { - data.trim = Some((material, pattern)); + Some(Tag::compound(entries)) +} + +/// A `minecraft:profile` component as the item data JSON the model reads. +fn profile_tag(p: &Profile) -> Tag { + let properties = p + .properties + .iter() + .map(|(name, value, signature)| { + let mut entries: Vec<(&'static str, Tag)> = vec![ + ("name", Tag::String(name.as_str().into())), + ("value", Tag::String(value.as_str().into())), + ]; + if let Some(signature) = signature { + entries.push(("signature", Tag::String(signature.as_str().into()))); + } + Tag::compound(entries) + }) + .collect::>(); + let mut entries: Vec<(&'static str, Tag)> = Vec::new(); + if let Some(name) = &p.name { + entries.push(("name", Tag::String(name.as_str().into()))); } + entries.push(("properties", Tag::List(properties))); + Tag::compound(entries) } #[cfg(test)] mod tests { use super::*; + use horizon_protocol::registry::Ids; - fn named(name: &str) -> Option { - Some(match name { - "minecraft:sharpness" => 3, - "minecraft:gold" => 7, - "minecraft:sentry" => 2, - _ => return None, - }) - } - fn name_of(id: i32) -> Option> { - Some(Arc::from(match id { - 3 => "minecraft:sharpness", - 7 => "minecraft:gold", - 2 => "minecraft:sentry", - _ => return None, - })) - } - - fn stack_with_data() -> ItemStack { - let mut stack = ItemStack::new("minecraft:diamond_sword", 1); + /// What a creative client sends (`stack_to_network`) comes back whole on + /// the server: damage, both enchantment lists, repair cost, name, trim. + #[test] + fn a_creative_stack_keeps_its_components_across_the_wire() { + let mut components = vec![String::new(); 60]; + for (id, name) in [ + (component::DAMAGE, "minecraft:damage"), + (component::CUSTOM_NAME, "minecraft:custom_name"), + (component::ENCHANTMENTS, "minecraft:enchantments"), + (component::REPAIR_COST, "minecraft:repair_cost"), + ( + component::STORED_ENCHANTMENTS, + "minecraft:stored_enchantments", + ), + (component::TRIM, "minecraft:trim"), + ] { + components[id as usize] = name.to_owned(); + } + let names = |n: &[&str]| n.iter().map(|s| s.to_string()).collect::>(); + let builtin = Builtin { + registries: [ + ( + "minecraft:item".to_owned(), + Ids::from_names(names(&["minecraft:air", "minecraft:diamond_chestplate"])), + ), + ( + "minecraft:data_component_type".to_owned(), + Ids::from_names(components), + ), + ] + .into(), + }; + let synced = |n: &[&str]| n.iter().map(|s| (s.to_string(), None)).collect::>(); + let dynamic = Dynamic { + registries: [ + ( + "minecraft:enchantment".to_owned(), + synced(&["minecraft:protection", "minecraft:sharpness"]), + ), + ( + "minecraft:trim_material".to_owned(), + synced(&["minecraft:gold"]), + ), + ( + "minecraft:trim_pattern".to_owned(), + synced(&["minecraft:sentry"]), + ), + ] + .into(), + }; + let mut stack = ItemStack::new("minecraft:diamond_chestplate", 1); stack.damage = 5; + stack.enchant("minecraft:protection", 4); let d = stack.data_mut(); - d.enchantments.push((Arc::from("minecraft:sharpness"), 4)); d.stored_enchantments .push((Arc::from("minecraft:sharpness"), 2)); d.repair_cost = 9; d.custom_name = Some("Keeper".to_owned()); d.trim = Some((Arc::from("minecraft:gold"), Arc::from("minecraft:sentry"))); - stack - } - - /// Every modelled component survives the packet, so the server's echo does - /// not hand back a bare item. - #[test] - fn a_stack_keeps_its_components_across_the_wire() { - let stack = stack_with_data(); - let mut packet = Stack::new(1, 1); - packet.added = encode(&stack, named, named, named); - - let mut back = ItemStack::new("minecraft:diamond_sword", 1); - apply(&packet, &mut back, name_of, name_of, name_of); - back.tidy(); - + let wire = crate::lan::guest::stack_to_network( + |name| { + builtin + .get("minecraft:item") + .and_then(|i| i.id(name)) + .unwrap_or(0) + }, + |registry, name| dynamic.id(registry, name), + &Some(stack.clone()), + ) + .unwrap(); + let back = from_network(&wire, &builtin, &dynamic); + assert_eq!(back.item, stack.item); assert_eq!(back.damage, 5); let d = back.data().expect("components"); - assert_eq!(d.enchantments, vec![(Arc::from("minecraft:sharpness"), 4)]); + assert_eq!(d.enchantments, vec![(Arc::from("minecraft:protection"), 4)]); assert_eq!( d.stored_enchantments, vec![(Arc::from("minecraft:sharpness"), 2)] @@ -155,16 +283,4 @@ mod tests { Some((Arc::from("minecraft:gold"), Arc::from("minecraft:sentry"))) ); } - - /// A name the registry does not know is a missing id, not id 0, which would - /// name the registry's first entry instead. - #[test] - fn an_unknown_enchantment_is_left_out_rather_than_sent_as_zero() { - let mut stack = ItemStack::new("minecraft:book", 1); - stack - .data_mut() - .enchantments - .push((Arc::from("mod:unknown"), 1)); - assert!(encode(&stack, named, named, named).is_empty()); - } } diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index 2667b1fed..dddecf0ca 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -2293,13 +2293,10 @@ impl Hub { .name("minecraft:enchantment", id) .map(str::to_owned) } - /// An entry of a synced registry by id, for the components a packet - /// carries by id (`minecraft:trim_material`, `minecraft:trim_pattern`). - pub fn trim_name(&self, registry: &str, id: i32) -> Option> { - self.sync - .dynamic - .name(registry, id) - .map(std::sync::Arc::from) + /// A stack a client sent (`SetCreativeModeSlot`), read with this + /// server's registries. + pub fn stack_from_network(&self, s: &horizon_protocol::item::Stack) -> crate::items::ItemStack { + crate::item_sync::from_network(s, &self.sync.builtin, &self.sync.dynamic) } /// ServerLevel.removePlayerImmediately, then addRespawnedPlayer: the other players From e4a151c2e9dd78b09d5f76962df1ee6992061bf1 Mon Sep 17 00:00:00 2001 From: vlesmentie <129341611+vlesmentie@users.noreply.github.com> Date: Sun, 25 Oct 2026 15:49:15 -0300 Subject: [PATCH 144/238] Menu buttons show the pointing hand under the mouse 26.3's AbstractWidget.handleCursor asks for CursorTypes.POINTING_HAND when a widget is hovered, or NOT_ALLOWED when it is inactive, and the frame applies the last request to the window (GuiGraphicsExtractor requestCursor and applyCursor, Window.selectCursor). Horizon never changed the cursor, so it stayed an arrow over buttons. widgets.rs keeps the frame's request, ui::button and the resource pack screen's buttons make it from the mouse hover (not keyboard focus), and the game sets winit's Pointer, NotAllowed or Default cursor after each frame when it changes. Fixes #1347. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 22 ++++++++ crates/horizon-client/src/pack_screen.rs | 3 ++ crates/horizon-client/src/ui.rs | 1 + crates/horizon-client/src/widgets.rs | 66 +++++++++++++++++++++++- 4 files changed, 90 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 0502753db..1a862193d 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1203,6 +1203,8 @@ struct Game<'a> { left: bool, right: bool, cursor: (f32, f32), + /// The cursor icon the window shows (`widgets::take_cursor` each frame). + cursor_icon: horizon_client::widgets::Cursor, captured: bool, focused: bool, /// The keyboard's modifier state (`KeyEvent.modifiers`). @@ -4659,6 +4661,24 @@ impl Game<'_> { } window.set_cursor_visible(!self.captured); } + /// `GuiGraphicsExtractor.applyCursor` -> `Window.selectCursor` at the end + /// of a frame: the cursor the frame's widgets asked for, set on the window + /// only when it changes. + fn apply_cursor_icon(&mut self) { + use horizon_client::widgets::Cursor; + let wanted = horizon_client::widgets::take_cursor(); + if wanted == self.cursor_icon { + return; + } + if let Some(window) = &self.window { + window.set_cursor(match wanted { + Cursor::Arrow => winit::window::CursorIcon::Default, + Cursor::PointingHand => winit::window::CursorIcon::Pointer, + Cursor::NotAllowed => winit::window::CursorIcon::NotAllowed, + }); + } + self.cursor_icon = wanted; + } /// LevelLoadingScreen.onClose -> Gui.setScreen(null) -> grabMouse. fn capture_loaded_world(&mut self) { // Loading may finish after focus was lost or another screen opened. @@ -11472,6 +11492,7 @@ impl ApplicationHandler for Game<'_> { self.advance_flight(); let ticked = Instant::now(); let rendered = self.render()?; + self.apply_cursor_icon(); if let Some(threshold) = self.options.spikes && let Some(r) = self.renderer.as_ref() { @@ -11779,6 +11800,7 @@ impl<'a> Game<'a> { left: false, right: false, cursor: (0.0, 0.0), + cursor_icon: horizon_client::widgets::Cursor::Arrow, captured: false, focused: true, modifiers: winit::keyboard::ModifiersState::empty(), diff --git a/crates/horizon-client/src/pack_screen.rs b/crates/horizon-client/src/pack_screen.rs index 2929fd663..03906030a 100644 --- a/crates/horizon-client/src/pack_screen.rs +++ b/crates/horizon-client/src/pack_screen.rs @@ -831,6 +831,7 @@ impl PackScreen { d::label(&mut out, &lines, mx, my, self.width - 50, true); for ((bx, by), key) in buttons.into_iter().zip(["gui.yes", "gui.no"]) { let hover = d::inside(mouse, bx, by, 150, 20); + crate::widgets::widget_cursor(hover, true); d::button(&mut out, m, &tr(key), bx, by, 150, 20, true, hover); } return out; @@ -856,6 +857,8 @@ impl PackScreen { } else { "gui.done" }); + // The cursor follows the mouse; the highlight also keyboard focus. + crate::widgets::widget_cursor(hover, true); d::button( &mut out, m, diff --git a/crates/horizon-client/src/ui.rs b/crates/horizon-client/src/ui.rs index 1c6e93067..df72ba45b 100644 --- a/crates/horizon-client/src/ui.rs +++ b/crates/horizon-client/src/ui.rs @@ -309,6 +309,7 @@ pub fn button( hover: bool, ) { crate::widgets::record(&label.plain(), [x, y, w, h], active); + crate::widgets::widget_cursor(hover, active); let sprite = if !active { "gui/sprites/widget/button_disabled" } else if hover { diff --git a/crates/horizon-client/src/widgets.rs b/crates/horizon-client/src/widgets.rs index 8bd80d3d1..c7c7eb2db 100644 --- a/crates/horizon-client/src/widgets.rs +++ b/crates/horizon-client/src/widgets.rs @@ -1,8 +1,47 @@ //! The labelled widgets and focus targets drawn in the last frame, for the //! control socket: a script clicks a button by its label (`horizon-ctl //! click-button Respawn`) or any widget by its id in [`tree`] (`horizon-ctl -//! click done`), whatever screen drew it. -use std::cell::RefCell; +//! click done`), whatever screen drew it. Also the mouse cursor the widget +//! under the mouse asks for. +use std::cell::{Cell, RefCell}; + +/// The mouse cursor a frame's widgets asked for (`CursorTypes`; the others +/// aren't asked for yet). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum Cursor { + #[default] + Arrow, + PointingHand, + NotAllowed, +} + +thread_local! { + /// `GuiGraphicsExtractor.requestCursor`: the last request since [`take_cursor`]. + static CURSOR: Cell = const { Cell::new(Cursor::Arrow) }; +} + +/// `GuiGraphicsExtractor.requestCursor`: the last request of a frame wins. +pub fn request_cursor(cursor: Cursor) { + CURSOR.set(cursor); +} + +/// `AbstractWidget.handleCursor`: a hovered widget asks for the pointing +/// hand, or for not-allowed when it is inactive. +pub fn widget_cursor(hovered: bool, active: bool) { + if hovered { + request_cursor(if active { + Cursor::PointingHand + } else { + Cursor::NotAllowed + }); + } +} + +/// The cursor the frame asked for, and back to the arrow for the next one +/// (the window shows the arrow unless a widget asks). +pub fn take_cursor() -> Cursor { + CURSOR.replace(Cursor::Arrow) +} /// A labelled widget in GUI pixels (the coordinates screens take input in). #[derive(Clone, Debug)] @@ -133,6 +172,29 @@ pub fn record_as(label: &str, [x, y, w, h]: [f32; 4], enabled: bool, kind: Kind) }); } +#[cfg(test)] +mod cursor_tests { + use super::*; + + #[test] + fn hovered_widgets_ask_for_the_hand_or_not_allowed() { + let _ = take_cursor(); + widget_cursor(false, true); + assert_eq!(take_cursor(), Cursor::Arrow); + widget_cursor(true, true); + assert_eq!(take_cursor(), Cursor::PointingHand); + // Taken: the next frame starts from the arrow. + assert_eq!(take_cursor(), Cursor::Arrow); + widget_cursor(true, false); + assert_eq!(take_cursor(), Cursor::NotAllowed); + // The last request of a frame wins. + widget_cursor(true, false); + widget_cursor(false, true); + widget_cursor(true, true); + assert_eq!(take_cursor(), Cursor::PointingHand); + } +} + /// Once per event loop turn with the count of rendered frames: when a new /// frame was drawn since the last call, its buttons become the last frame's. pub fn end_frame(frames: u64) { From 9ec5f1ddbc31908eaa27735a01ecd8ba9cf44182 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 19:51:09 +0100 Subject: [PATCH 145/238] Rockets: a guest's boost rocket hurts them; fireworks sent to a server's clients FireworkRocketEntity.dealExplosionDamage hurts attachedToEntity with the full 5 + 2 per explosion: a guest's boost rocket now hurts that guest instead of nobody. The host's encoder (network_stack) now sends minecraft:fireworks and minecraft:firework_explosion too, so a rocket's flight duration and a star's explosion reach the server thread's window, LAN guests and a dedicated server's players; the creative slot encoder shares it. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/fireworks.rs | 15 ++++++-- crates/horizon-client/src/game/remote.rs | 21 +++--------- crates/horizon-client/src/lan/guest.rs | 38 +++++++++++++++++++++ 3 files changed, 55 insertions(+), 19 deletions(-) diff --git a/crates/horizon-client/src/game/fireworks.rs b/crates/horizon-client/src/game/fireworks.rs index 623e0e147..7838c9590 100644 --- a/crates/horizon-client/src/game/fireworks.rs +++ b/crates/horizon-client/src/game/fireworks.rs @@ -251,8 +251,19 @@ impl Game<'_> { let at = [r.position.x, r.position.y, r.position.z]; if !r.explosions.is_empty() { let damage = 5.0 + r.explosions.len() as f32 * 2.0; - if r.attached && r.guest.is_none() { - self.hurt_player_by("minecraft:fireworks", damage); + // The player it boosts takes the full damage, a guest as well. + if r.attached { + match r.guest { + None => self.hurt_player_by("minecraft:fireworks", damage), + Some(conn) => { + if let Some(mut host) = self.lan.take() { + self.as_guest(&mut host, conn, |g| { + g.hurt_player_by("minecraft:fireworks", damage); + }); + self.lan = Some(host); + } + } + } } let from = r.position; let hits: Vec<(u32, f32)> = { diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 5a8582ac9..d8b370af4 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -769,23 +769,10 @@ impl Game<'_> { horizon_protocol::item::Component::Int(s.damage as i32), )); } - if let Some((flight, explosions)) = s.data().and_then(|d| d.fireworks.clone()) { - stack.added.push(( - horizon_protocol::item::component::FIREWORKS, - horizon_protocol::item::Component::Fireworks { - flight: i32::from(flight), - explosions: explosions - .iter() - .map(|e| horizon_protocol::item::FireworkExplosion { - shape: e.shape as i32, - colors: e.colors.iter().map(|c| *c as i32).collect(), - fade: e.fade.iter().map(|c| *c as i32).collect(), - trail: e.trail, - twinkle: e.twinkle, - }) - .collect(), - }, - )); + if let Some(d) = s.data() { + stack + .added + .extend(horizon_client::lan::guest::firework_components(d)); } Some(stack) } diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index 440b58284..4bafe7866 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -431,6 +431,43 @@ impl Guest { } } +/// `FireworkExplosion.STREAM_CODEC`'s fields (`Shape` by its BY_ID order). +fn network_explosion( + e: &crate::particles::FireworkExplosion, +) -> horizon_protocol::item::FireworkExplosion { + horizon_protocol::item::FireworkExplosion { + shape: e.shape as i32, + colors: e.colors.iter().map(|c| *c as i32).collect(), + fade: e.fade.iter().map(|c| *c as i32).collect(), + trail: e.trail, + twinkle: e.twinkle, + } +} + +/// A rocket's `minecraft:fireworks` and a star's `minecraft:firework_explosion`. +pub fn firework_components( + d: &crate::item_data::StackData, +) -> Vec<(i32, horizon_protocol::item::Component)> { + use horizon_protocol::item::{Component, component}; + let mut out = Vec::new(); + if let Some((flight, explosions)) = &d.fireworks { + out.push(( + component::FIREWORKS, + Component::Fireworks { + flight: i32::from(*flight), + explosions: explosions.iter().map(network_explosion).collect(), + }, + )); + } + if let Some(e) = &d.firework_explosion { + out.push(( + component::FIREWORK_EXPLOSION, + Component::FireworkExplosion(network_explosion(e)), + )); + } + out +} + /// The off hand slot is conventionally 40 in saves and 45 in the menu. /// `synced_id` names an entry of a synced registry (enchantments, trim /// materials and patterns) by its id on this connection. @@ -521,6 +558,7 @@ fn network_stack( horizon_protocol::item::Component::Int(i32::from(mode)), )); } + stack.added.extend(firework_components(d)); // A loaded crossbow's ammunition. if !d.charged.is_empty() { let charged = d From 4513af89954872409e1f02909633789c6293a5a9 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 20:13:36 +0100 Subject: [PATCH 146/238] horizon-server: the lock test waits for a fork-held descriptor to close Tests in the same binary spawn processes (tar, the engine); between fork and exec the child holds a copy of every open descriptor, the lock's too, so relocking right after drop could see WouldBlock. Seen twice on CI (#1833, #1895). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-server/src/supervisor.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/horizon-server/src/supervisor.rs b/crates/horizon-server/src/supervisor.rs index 4de011362..f2e811700 100644 --- a/crates/horizon-server/src/supervisor.rs +++ b/crates/horizon-server/src/supervisor.rs @@ -849,7 +849,17 @@ mod tests { let first = lock(&dir).unwrap(); assert!(lock(&dir).unwrap_err().contains("already running")); drop(first); - lock(&dir).unwrap(); + // A process another test spawns holds a copy of the lock's descriptor between its fork and exec: + // the lock comes back once that child has exec'd. + let mut again = lock(&dir); + for _ in 0..50 { + if again.is_ok() { + break; + } + std::thread::sleep(std::time::Duration::from_millis(20)); + again = lock(&dir); + } + again.unwrap(); std::fs::remove_dir_all(&dir).unwrap(); } } From 481e75bc33bea120cad48a40294fdaf3df2b19aa Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 25 Oct 2026 19:16:36 +0000 Subject: [PATCH 147/238] Clear off-hand items in vanilla inventory slot order Co-Authored-By: OpenAI Codex GPT-6.1 --- crates/horizon-client/src/game/commands.rs | 9 ++++++--- tools/command_exec_corpus.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index 19a16e261..1f24c8a4f 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -2499,12 +2499,14 @@ impl Game<'_> { }; let max = Self::int(ctx, "maxCount").unwrap_or(-1); let counting = max == 0; - // Inventory.clearOrCountMatchingItems: main, armor, crafting grid, carried. + // Inventory slots 0..40: main, armor feet to head, off hand; + // then clearOrCountMatchingItems visits the crafting grid and carried stack. let mut removed = 0i32; let mut budget = if max < 0 { i32::MAX } else { max }; let all = |g: &Self| -> Vec> { let mut v: Vec> = g.inventory.clone(); - v.extend(g.armor.iter().cloned()); + v.extend(g.armor.iter().rev().cloned()); + v.push(g.offhand.clone()); v.extend(g.craft_grid.iter().cloned()); v.push(g.carried.clone()); v @@ -2518,7 +2520,8 @@ impl Game<'_> { .collect(); let mut slots: Vec<&mut Option> = Vec::new(); slots.extend(self.inventory.iter_mut()); - slots.extend(self.armor.iter_mut()); + slots.extend(self.armor.iter_mut().rev()); + slots.push(&mut self.offhand); slots.extend(self.craft_grid.iter_mut()); slots.push(&mut self.carried); for (slot, matches) in slots.into_iter().zip(matching) { diff --git a/tools/command_exec_corpus.py b/tools/command_exec_corpus.py index 0e376e260..4b24ca80f 100644 --- a/tools/command_exec_corpus.py +++ b/tools/command_exec_corpus.py @@ -182,6 +182,23 @@ def player_state(): cmd('/item modify entity @s weapon.mainhand {type:"minecraft:set_count",count:1}'), cmd('/clear @s'), cmd('/clear @s'), + # Inventory.clearOrCountMatchingItems visits the off hand as well as + # the main inventory, and consumes limited clears in inventory-slot order. + cmd('/item replace entity @s weapon.offhand with minecraft:shield'), + cmd('/clear @s minecraft:shield 0'), + cmd('/clear @s minecraft:shield 1'), + cmd('/clear @s minecraft:shield 0'), + cmd('/item replace entity @s weapon.offhand with minecraft:stone 5'), + cmd('/clear @s minecraft:stone 2'), + cmd('/clear @s minecraft:stone 0'), + cmd('/clear @s'), + cmd('/clear @s minecraft:stone 0'), + cmd('/item replace entity @s armor.head with minecraft:iron_helmet'), + cmd('/item replace entity @s armor.feet with minecraft:iron_boots'), + cmd('/clear @s * 1'), + cmd('/clear @s minecraft:iron_boots 0'), + cmd('/clear @s minecraft:iron_helmet 0'), + cmd('/clear @s'), cmd('/attribute @s minecraft:max_health get'), cmd('/attribute @s minecraft:max_health base get'), cmd('/attribute @s minecraft:max_health base set 30'), From 9c7909f77006d76fda223ecb8396039f2a108188 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 25 Oct 2026 19:23:55 +0000 Subject: [PATCH 148/238] Advance remote fireballs between server tracker updates Co-Authored-By: OpenAI Codex GPT-6.1 --- crates/horizon-client/src/game/remote.rs | 23 +++++++ .../src/game/remote_projectiles.rs | 20 +++++- crates/horizon-client/src/projectiles.rs | 46 ++++++++++---- tools/test_remote_fireball_parity.py | 62 +++++++++++++++++++ 4 files changed, 137 insertions(+), 14 deletions(-) create mode 100644 tools/test_remote_fireball_parity.py diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index ba069522c..662a29d18 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -1566,6 +1566,22 @@ impl Game<'_> { m.on_ground = on_ground; return; } + if let Some(p) = self.creatures.projectiles.iter_mut().find(|p| { + p.id == id as u32 + && matches!( + p.kind, + horizon_client::projectiles::ProjectileKind::LargeFireball { .. } + ) + }) { + if path.is_some() { + p.position = Vec3::new(position[0], position[1], position[2]); + } + if let Some((yaw, pitch)) = rotation { + p.yaw = yaw; + p.pitch = pitch; + } + return; + } if let Some(p) = r.pending_items.get_mut(&id) { p.0 = position; } @@ -3104,6 +3120,13 @@ impl Game<'_> { d.velocity = v; } else if let Some(f) = self.falling.iter_mut().find(|f| f.id == id as u32) { f.velocity = v; + } else if let Some(p) = self + .creatures + .projectiles + .iter_mut() + .find(|p| p.id == id as u32) + { + p.velocity = v; } else if let Some(p) = self .remote .as_mut() diff --git a/crates/horizon-client/src/game/remote_projectiles.rs b/crates/horizon-client/src/game/remote_projectiles.rs index bc6d0b13e..c7f80ae8e 100644 --- a/crates/horizon-client/src/game/remote_projectiles.rs +++ b/crates/horizon-client/src/game/remote_projectiles.rs @@ -207,7 +207,25 @@ impl Game<'_> { for p in &mut self.creatures.projectiles { p.previous = p.position; p.age += 1; - p.position = step(p.id, p.position); + if matches!(p.kind, ProjectileKind::LargeFireball { .. }) { + // AbstractHurtingProjectile.tick advances on the client between + // the tracker updates (ten ticks apart for a ghast's fireball). + let at = p.position; + let water = self + .res + .facts() + .get(self.world.block( + at.x.floor() as i32, + at.y.floor() as i32, + at.z.floor() as i32, + )) + .fluid + == horizon_client::blocks::Fluid::Water; + p.apply_hurting_inertia(water); + p.position = p.position.add_vector(p.velocity); + } else { + p.position = step(p.id, p.position); + } } for t in &mut self.tridents { let id = t.id; diff --git a/crates/horizon-client/src/projectiles.rs b/crates/horizon-client/src/projectiles.rs index 8244da4d9..219d59b6b 100644 --- a/crates/horizon-client/src/projectiles.rs +++ b/crates/horizon-client/src/projectiles.rs @@ -572,6 +572,23 @@ impl Projectile { } (self.position.y > -128.0, Hit::None) } + /// AbstractHurtingProjectile.applyInertia, on both server and client. + pub fn apply_hurting_inertia(&mut self, water: bool) { + // WitherSkull.getInertia: 0.73 for dangerous skulls. + let inertia = match self.kind { + ProjectileKind::WitherSkull { dangerous: true } => 0.73, + _ if water => 0.8, + _ => 0.95, + }; + let v = self.velocity; + let len = (v.x * v.x + v.y * v.y + v.z * v.z).sqrt().max(1.0e-9); + self.velocity = Vec3::new( + (v.x + v.x / len * self.acceleration) * inertia, + (v.y + v.y / len * self.acceleration) * inertia, + (v.z + v.z / len * self.acceleration) * inertia, + ); + } + /// One tick: returns whether to keep the projectile and what it hit. pub fn tick(&mut self, w: &impl ProjectileWorld) -> (bool, Hit) { self.previous = self.position; @@ -584,19 +601,7 @@ impl Projectile { ProjectileKind::SmallFireball | ProjectileKind::LargeFireball { .. } | ProjectileKind::WitherSkull { .. } => { - // WitherSkull.getInertia: 0.73 for dangerous skulls. - let inertia = match self.kind { - ProjectileKind::WitherSkull { dangerous: true } => 0.73, - _ if water => 0.8, - _ => 0.95, - }; - let v = self.velocity; - let len = (v.x * v.x + v.y * v.y + v.z * v.z).sqrt().max(1.0e-9); - self.velocity = Vec3::new( - (v.x + v.x / len * self.acceleration) * inertia, - (v.y + v.y / len * self.acceleration) * inertia, - (v.z + v.z / len * self.acceleration) * inertia, - ); + self.apply_hurting_inertia(water); } // Arrows tick with `arrows::tick`. ProjectileKind::Arrow { .. } => {} @@ -819,6 +824,21 @@ mod tests { // (0.1 + 0.1) * 0.95 assert!((p.velocity.x - 0.19).abs() < 1e-9); } + + #[test] + fn fireball_inertia_preserves_custom_acceleration() { + let mut p = Projectile::fireball( + ProjectileKind::SmallFireball, + Vec3::new(0.0, 100.0, 0.0), + None, + [1.0, 0.0, 0.0], + ); + // A redirected projectile can carry a different acceleration power. + p.acceleration = 0.2; + p.tick(&Empty(vec![])); + assert!((p.velocity.x - 0.285).abs() < 1e-9); + assert!((p.position.x - 0.285).abs() < 1e-9); + } } #[cfg(test)] diff --git a/tools/test_remote_fireball_parity.py b/tools/test_remote_fireball_parity.py new file mode 100644 index 000000000..7e60ff30a --- /dev/null +++ b/tools/test_remote_fireball_parity.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 +"""A connected client advances ghast fireballs between tracker packets. + +AbstractHurtingProjectile.tick/applyInertia run on clients as well as servers. +Like lan_e2e's falling-entity check, sample motion more often than the tracker +interval; an interpolation-only client stalls for most of that interval. +""" +import json +import math +import subprocess + +import lab +import lan_e2e as lan + + +def check_motion(host, guest, out): + x, y, z = (math.floor(v) for v in lan.pos_of(guest)) + for game in (host, guest): + game('command', command='gamemode creative') + tag = 'remote_fireball_parity' + host('command', command=f'fill {x + 2} {y + 18} {z - 1} {x + 48} {y + 23} {z + 1} minecraft:air') + host('command', command=f'summon minecraft:fireball {x + 3.5} {y + 20} {z + 0.5} ' + f'{{Tags:["{tag}"],Motion:[0.1d,0.0d,0.0d]}}') + samples = [] + try: + for _ in range(8): + balls = [p for p in guest('status')['projectiles'] if p['kind'] == 'LargeFireball'] + samples.append(balls[0]['position'][0] if balls else None) + guest.wait(2) + finally: + host('command', command=f'kill @e[tag={tag}]') + seen = [p for p in samples if p is not None] + assert len(seen) >= 6, f'fireball visible in only {len(seen)} samples: {samples}' + moves = sum(b != a for a, b in zip(seen, seen[1:])) + assert moves >= len(seen) - 2, f'fireball stalled between tracker updates: {samples}' + return {'positions': samples} + + +def main(): + subprocess.run(['cargo', 'build', '--locked', '--profile', 'fast', '-p', 'horizon-client', + '--bin', 'horizon-client', '--bin', 'horizon-ctl'], cwd=lab.ROOT, check=True) + metadata = json.loads(subprocess.check_output( + ['cargo', 'metadata', '--format-version', '1', '--no-deps'], cwd=lab.ROOT, text=True)) + ctl = lab.ROOT / metadata['target_directory'] / 'fast' / 'horizon-ctl' + lan.CHECK_FNS['remote-fireball'] = check_motion + # Use the runner's current display (xvfb on Linux), with fresh isolated homes. + lan.game_env = lambda _screen, home: lab.game_env( + HORIZON_HOME=home, HORIZON_TELEMETRY_URL='off') + reports = {} + for threaded in (True, False): + lan.SERVER_THREAD = threaded + mode = 'server-thread' if threaded else 'in-frame' + out = lab.LOCAL / 'remote-fireball-parity' / mode + out.mkdir(parents=True, exist_ok=True) + # lan.Game launches each client with lab.game_env and private scratch homes. + reports[mode] = lan.run(ctl, out, lab.RESOURCES.resolve(), ['remote-fireball']) + lab.write_json(lab.LOCAL / 'remote-fireball-parity' / 'report.json', reports) + assert all(r['pass'] for mode in reports.values() for r in mode.values()), reports + + +if __name__ == '__main__': + main() From 84a3f1ee1832967b8d1eed3b55dfae767efaeee9 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 25 Oct 2026 19:30:34 +0000 Subject: [PATCH 149/238] Emit sculk movement events for each server player Co-Authored-By: OpenAI Codex GPT-6.1 --- crates/horizon-client/src/game/lan_actions.rs | 5 ++ crates/horizon-client/src/game/sculk.rs | 25 +++++- tools/test_server_player_sculk_parity.py | 87 +++++++++++++++++++ 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 tools/test_server_player_sculk_parity.py diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 18f529184..b8c161428 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -84,6 +84,7 @@ pub(super) struct GuestPlay { equipped: Option>>, /// The guest's cast fishing hook (`Player.fishing`). pub(super) hook: Option, + sculk: super::sculk::PlayerEvents, } /// A guest's command waiting for the hub (`LanHost::commands`). @@ -250,6 +251,7 @@ impl Game<'_> { if fresh { // A new body (joined, respawned, changed level): nothing on it yet. play.equipped = None; + play.sculk = Default::default(); } let g = &mut host.hub.guests[i]; // The guest's body. @@ -257,6 +259,7 @@ impl Game<'_> { host.play.insert(conn, play); return None; }; + self.sculk.swap_player(&mut play.sculk); self.potions.swap_player(&mut g.effects, &mut play.potions); motion.position = Vec3::new(g.position[0], g.position[1], g.position[2]); motion.ground.on_ground = g.on_ground; @@ -478,6 +481,7 @@ impl Game<'_> { projectiles, modifiers, } = swap; + self.sculk.swap_player(&mut play.sculk); play.modifiers = std::mem::replace(&mut self.applied_modifiers, modifiers); // What the guest shot is theirs (Projectile.setOwner). for p in self.creatures.projectiles.iter_mut().skip(projectiles) { @@ -2095,6 +2099,7 @@ impl Game<'_> { let Some((events, marked)) = events else { return; }; + self.as_guest(host, conn, |game| game.tick_player_sculk_events()); for event in events { match event { MotionEvent::HurtSound(sound) => self.player_hurt_sound(sound, at), diff --git a/crates/horizon-client/src/game/sculk.rs b/crates/horizon-client/src/game/sculk.rs index a11a643c1..84e3d5cb2 100644 --- a/crates/horizon-client/src/game/sculk.rs +++ b/crates/horizon-client/src/game/sculk.rs @@ -63,6 +63,12 @@ struct Steps { on_ground: bool, in_water: bool, } +#[derive(Default)] +pub(super) struct PlayerEvents { + steps: Option, + last_health: Option, +} + #[derive(Default)] pub(super) struct SculkState { /// The player's warning level toward a warden. @@ -86,6 +92,15 @@ pub(super) struct SculkState { } impl SculkState { + pub(super) fn swap_player(&mut self, other: &mut PlayerEvents) { + let steps = self.steps.remove(&Who::Player); + if let Some(guest) = other.steps.take() { + self.steps.insert(Who::Player, guest); + } + other.steps = steps; + std::mem::swap(&mut self.last_health, &mut other.last_health); + } + /// Exchange the level-held parts (its sculk block entities and the /// chunks scanned for them) with another level's (game/levels.rs). pub(super) fn swap_level(&mut self, other: &mut Self) { @@ -1491,6 +1506,12 @@ impl Game<'_> { /// Events from entities moving, getting hurt and dying, derived from what /// changed this tick. fn tick_entity_events(&mut self) { + self.tick_player_sculk_events(); + self.tick_mob_sculk_events(); + } + + /// Entity.move emits on the authoritative server too, for each ServerPlayer. + pub(super) fn tick_player_sculk_events(&mut self) { // The player. let player = self.player.as_ref().map(|p| { ( @@ -1518,7 +1539,9 @@ impl Game<'_> { } self.sculk.last_health = Some(health); } - // Mobs. + } + + fn tick_mob_sculk_events(&mut self) { let snapshot: Vec<(u32, [f64; 3], bool, bool, Species, Option)> = self .mobs .iter() diff --git a/tools/test_server_player_sculk_parity.py b/tools/test_server_player_sculk_parity.py new file mode 100644 index 000000000..33489ad5a --- /dev/null +++ b/tools/test_server_player_sculk_parity.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 +"""Both players' ordinary walking activates a sculk sensor on the server. + +Exercise Entity.move movement emission through real client input, alongside +sculk's deterministic vanilla oracle. Check the integrated-server and in-frame +hosts: the guest always uses the server-player path. +""" +import json +import math +import subprocess + +import lab +import lan_e2e as lan + + +def until(game, observe, predicate, ticks=100): + value = observe() + for _ in range(ticks): + if predicate(value): + return value + game.wait(1) + value = observe() + assert predicate(value), value + return value + + +def check_walk(host, guest, out, role): + player = host if role == 'host' else guest + hx, y, hz = (math.floor(v) for v in lan.pos_of(host)) + x, z = hx + 6, hz + (-8 if role == 'host' else 8) + sensor = (x + 3, y, z + 1) + player('command', command='gamemode survival') + host('command', command=f'fill {x - 2} {y - 1} {z - 2} {x + 5} {y - 1} {z + 5} minecraft:stone') + host('command', command=f'fill {x - 2} {y} {z - 2} {x + 5} {y + 3} {z + 5} minecraft:air') + start = (x + 0.5, y, z + 0.5) + player('command', command=f'tp @s {start[0]} {start[1]} {start[2]} 0 0') + until(player, lambda: lan.pos_of(player), lambda p: math.dist(p, start) < 0.1) + player.raw('look', 0, 0) + player.wait(2) + host('command', command=f'setblock {sensor[0]} {sensor[1]} {sensor[2]} minecraft:sculk_sensor') + phase = lambda game: game.raw('getblock', *sensor)['block'] + try: + until(host, lambda: phase(host), lambda s: 'sculk_sensor_phase=inactive' in s) + player.wait(10) + assert 'sculk_sensor_phase=inactive' in phase(host), 'setup activated the sensor' + player.raw('key', 'w', 'down') + try: + player.wait(15) + finally: + player.raw('key', 'w', 'up') + moved = {'client': math.dist(lan.pos_of(player), start), + 'server': math.dist(lan.server_player(host, player)['position'], start)} + assert all(d > 1 for d in moved.values()), moved + for viewer in (host, guest): + until(viewer, lambda: phase(viewer), lambda s: 'sculk_sensor_phase=active' in s, ticks=85) + return {'moved': moved, 'phase': phase(host)} + finally: + player.raw('key', 'w', 'up') + host('command', command=f'fill {x - 2} {y} {z - 2} {x + 5} {y + 3} {z + 5} minecraft:air') + + +def main(): + subprocess.run(['cargo', 'build', '--locked', '--profile', 'fast', '-p', 'horizon-client', + '--bin', 'horizon-client', '--bin', 'horizon-ctl'], cwd=lab.ROOT, check=True) + metadata = json.loads(subprocess.check_output( + ['cargo', 'metadata', '--format-version', '1', '--no-deps'], cwd=lab.ROOT, text=True)) + ctl = lab.ROOT / metadata['target_directory'] / 'fast' / 'horizon-ctl' + checks = ['sculk-walk-host', 'sculk-walk-guest'] + for role, name in zip(('host', 'guest'), checks): + lan.CHECK_FNS[name] = lambda h, g, o, role=role: check_walk(h, g, o, role) + # Use the runner's current display (xvfb on Linux), with fresh isolated homes. + lan.game_env = lambda _screen, home: lab.game_env( + HORIZON_HOME=home, HORIZON_TELEMETRY_URL='off') + reports = {} + for threaded in (True, False): + lan.SERVER_THREAD = threaded + mode = 'server-thread' if threaded else 'in-frame' + out = lab.LOCAL / 'server-player-sculk-parity' / mode + out.mkdir(parents=True, exist_ok=True) + # lan.Game launches each client with lab.game_env and private scratch homes. + reports[mode] = lan.run(ctl, out, lab.RESOURCES.resolve(), checks) + lab.write_json(lab.LOCAL / 'server-player-sculk-parity' / 'report.json', reports) + assert all(r['pass'] for mode in reports.values() for r in mode.values()), reports + + +if __name__ == '__main__': + main() From 89d6e1c2a50112cb6d65de4cbfbe8af094ea5952 Mon Sep 17 00:00:00 2001 From: vlesmentie <129341611+vlesmentie@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:34:39 -0300 Subject: [PATCH 150/238] Structure chests keep the enchantments their loot rolled The loot rolls carry enchantments and potions (horizon_world::loot runs enchant_with_levels, enchant_randomly and set_potion), but chests filled when a structure is placed, and mineshaft chest minecarts, turned each LootStack into an item with loot_item, which keeps only the item, count, damage and exploration map. End city gear came out bare. Both now use loot_item_with_components, as vaults, trial spawners and decorated pots already do. Fixes #1437. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/structure_blocks.rs | 53 +++++++++++++++---- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/crates/horizon-client/src/structure_blocks.rs b/crates/horizon-client/src/structure_blocks.rs index 1e1906f6d..427323c23 100644 --- a/crates/horizon-client/src/structure_blocks.rs +++ b/crates/horizon-client/src/structure_blocks.rs @@ -165,14 +165,10 @@ pub fn block_entities( .iter_mut() .zip(loot.fill(&table, seed, 27, Some(biome))) { - *slot = stack.map(|s| { - let mut item = loot_item(&s); - // An ominous bottle's level (the default, 0, is no component). - if let Some(level) = s.ominous_amplifier.filter(|l| *l > 0) { - item.data_mut().ominous_amplifier = Some(level.clamp(0, 4) as u8); - } - item - }); + // With what the rolls put on them: enchantments + // (enchant_with_levels in End city chests), potions and an + // ominous bottle's level. + *slot = stack.map(|s| loot_item_with_components(&s)); } } out.push(((x, y, z), entity)); @@ -211,7 +207,7 @@ pub fn unpack_entity_loot( let slots: Vec> = loot .fill(&table, seed, 27, Some(biome)) .into_iter() - .map(|s| s.map(|s| loot_item(&s))) + .map(|s| s.map(|s| loot_item_with_components(&s))) .collect(); nbt.remove(&JavaString::from("LootTable")); nbt.remove(&JavaString::from("LootTableSeed")); @@ -224,6 +220,45 @@ mod tests { use super::*; use horizon_world::loot::{ExplorationMap, LootStack}; + #[test] + fn placed_chest_loot_keeps_its_enchantments_and_potions() { + let stack = + |item: &str, enchantments: Vec<(String, i32)>, potion: Option<&str>| LootStack { + item: item.into(), + count: 1, + damage: 0, + enchantments, + potion: potion.map(str::to_owned), + map: false, + exploration: None, + ominous_amplifier: None, + }; + // An End city chest's enchant_with_levels roll. + let boots = loot_item_with_components(&stack( + "minecraft:diamond_boots", + vec![ + ("minecraft:protection".into(), 3), + ("minecraft:unbreaking".into(), 2), + ], + None, + )); + assert_eq!(boots.enchantments().len(), 2); + // A book's go into stored_enchantments. + let book = loot_item_with_components(&stack( + "minecraft:enchanted_book", + vec![("minecraft:mending".into(), 1)], + None, + )); + assert!(book.enchantments().is_empty()); + assert_eq!(book.stored_enchantments().len(), 1); + let arrow = loot_item_with_components(&stack( + "minecraft:tipped_arrow", + Vec::new(), + Some("minecraft:slowness"), + )); + assert_eq!(arrow.potion(), Some("minecraft:slowness")); + } + #[test] fn exploration_maps_wait_in_custom_data_until_searched() { let mut stack = LootStack { From 4459f132fe481be02ed0c2a2e2e41b7666ba3dbe Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 20:44:09 +0100 Subject: [PATCH 151/238] Collision scans: skip the outer cells of sections no block reaches out of BlockCollisions keeps a block on the cells around the box (Cursor3D types 1 and 2) only if it has a large collision shape or is a moving piston. Most sections hold neither in their palette, so the scan now asks the provider once per section and passes those cells without reading them: a mob's box reads about a fifth of the cells it did. Same hits, same order. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/collisions.rs | 18 +++ crates/horizon-world/src/block_collision.rs | 7 ++ .../horizon-world/src/block_collision_scan.rs | 31 +++++ .../tests/block_collision_scan.rs | 107 +++++++++++++----- 4 files changed, 136 insertions(+), 27 deletions(-) diff --git a/crates/horizon-client/src/collisions.rs b/crates/horizon-client/src/collisions.rs index 3ff2cfdd6..7c32bd829 100644 --- a/crates/horizon-client/src/collisions.rs +++ b/crates/horizon-client/src/collisions.rs @@ -92,6 +92,13 @@ impl BlockCollisionWorld for EntityCollisions<'_, W> { ) -> std::result::Result { self.blocks.is_suffocating(chunk, block, p) } + fn may_reach_out( + &mut self, + chunk: Self::Chunk, + section_y: i32, + ) -> std::result::Result { + self.blocks.may_reach_out(chunk, section_y) + } fn context_shape( &mut self, block: CollisionBlock, @@ -262,6 +269,17 @@ impl<'w> BlockCollisionWorld for SceneCollisions<'w, '_> { fn collision_block(&mut self, chunk: Self::Chunk, p: BlockPos) -> io::Result { self.binding.collision_block(chunk.block(p.x, p.y, p.z)) } + /// From the section's palette, without reading its blocks; a palette + /// of the whole registry reads every cell. + fn may_reach_out(&mut self, chunk: Self::Chunk, section_y: i32) -> io::Result { + let blocks = usize::try_from(section_y - (crate::chunk::MIN_Y >> 4)) + .ok() + .and_then(|s| chunk.sections.get(s)) + .map(|section| §ion.blocks); + Ok(blocks.is_none_or(|blocks| { + blocks.is_global() || blocks.maybe_has(|v| self.binding.reaches_out(BlockStateId(v))) + })) + } fn is_suffocating( &mut self, _: Self::Chunk, diff --git a/crates/horizon-world/src/block_collision.rs b/crates/horizon-world/src/block_collision.rs index 3770a1a0f..9e2d81b78 100644 --- a/crates/horizon-world/src/block_collision.rs +++ b/crates/horizon-world/src/block_collision.rs @@ -158,6 +158,13 @@ impl<'r> BlockCollisionRegistry<'r> { empty_shape: flags & 4 != 0, }) } + /// Whether a scan keeps `state` in the cells around its box: a large + /// collision shape or a moving piston. States outside the registry count. + pub fn reaches_out(&self, state: BlockStateId) -> bool { + self.scan_flags + .get(state.0 as usize) + .is_none_or(|flags| flags & 3 != 0) + } pub fn collision_shape( &self, world: &mut W, diff --git a/crates/horizon-world/src/block_collision_scan.rs b/crates/horizon-world/src/block_collision_scan.rs index 6c26c6631..391c3e96e 100644 --- a/crates/horizon-world/src/block_collision_scan.rs +++ b/crates/horizon-world/src/block_collision_scan.rs @@ -41,6 +41,13 @@ pub trait BlockCollisionWorld { position: BlockPos, context: &CollisionContext<'_>, ) -> Result; + /// Whether a block of the chunk's section `section_y` (block y >> 4) may + /// have a large collision shape or be a moving piston: the only blocks + /// the scan keeps outside the box's own cells. False lets the scan pass + /// that section's outer cells without reading them; true reads them all. + fn may_reach_out(&mut self, _chunk: Self::Chunk, _section_y: i32) -> Result { + Ok(true) + } } #[derive(Debug)] @@ -88,6 +95,8 @@ pub struct BlockCollisionScan<'w, 'c, W: BlockCollisionWorld> { count: usize, budget: usize, chunk: Option<(ChunkPos, W::Chunk)>, + /// `may_reach_out` of the last section an outer cell was in. + reach: Option<(ChunkPos, i32, bool)>, failed: bool, } impl<'w, 'c, W: BlockCollisionWorld> BlockCollisionScan<'w, 'c, W> { @@ -137,6 +146,7 @@ impl<'w, 'c, W: BlockCollisionWorld> BlockCollisionScan<'w, 'c, W> { count, budget: candidate_budget, chunk: None, + reach: None, failed: false, }) } @@ -184,6 +194,27 @@ impl<'w, 'c, W: BlockCollisionWorld> BlockCollisionScan<'w, 'c, W> { let Some(chunk) = chunk else { continue; }; + // An outer cell only counts for a block reaching out of its own + // cell: most sections hold none, so their outer cells go unread + // (the result is the same, with a fifth of the block reads for a + // mob's box). + if kind != 0 { + let section_y = p.y >> 4; + let reaches = match self.reach { + Some((at, y, reaches)) if at == chunk_position && y == section_y => reaches, + _ => { + let reaches = self + .world + .may_reach_out(chunk, section_y) + .map_err(CollisionScanError::World)?; + self.reach = Some((chunk_position, section_y, reaches)); + reaches + } + }; + if !reaches { + continue; + } + } let block = self .world .collision_block(chunk, p) diff --git a/crates/horizon-world/tests/block_collision_scan.rs b/crates/horizon-world/tests/block_collision_scan.rs index 32735f826..adf530069 100644 --- a/crates/horizon-world/tests/block_collision_scan.rs +++ b/crates/horizon-world/tests/block_collision_scan.rs @@ -112,6 +112,8 @@ fn invalid_or_excessive_domains_fail_before_world_queries() { /// and has an empty shape. struct Recording { cells: Vec, + /// Sections (block y >> 4) whose blocks never reach out of their cell. + contained: Vec, } impl BlockCollisionWorld for Recording { type Error = io::Error; @@ -139,6 +141,40 @@ impl BlockCollisionWorld for Recording { ) -> io::Result { unreachable!() } + fn may_reach_out(&mut self, _: (), section_y: i32) -> io::Result { + Ok(!self.contained.contains(§ion_y)) + } +} + +/// The cells the scan reads for `bounds` (Cursor3D order: x fastest, then +/// y, then z, without the 8 corners), less the outer cells of `contained`. +fn expected_cells(bounds: Aabb, contained: &[i32]) -> Vec { + let (min, max) = (bounds.min_position(), bounds.max_position()); + let start = BlockPos::containing(Vec3::new(min.x - 1.0e-7, min.y - 1.0e-7, min.z - 1.0e-7)) + .offset(-1, -1, -1); + let end = BlockPos::containing(Vec3::new(max.x + 1.0e-7, max.y + 1.0e-7, max.z + 1.0e-7)) + .offset(1, 1, 1); + let sizes = [ + end.x - start.x + 1, + end.y - start.y + 1, + end.z - start.z + 1, + ]; + let mut expected = Vec::new(); + for i in 0..sizes[0] * sizes[1] * sizes[2] { + let r = [ + i % sizes[0], + i / sizes[0] % sizes[1], + i / sizes[0] / sizes[1], + ]; + let edges = (0..3) + .filter(|&a| r[a] == 0 || r[a] == sizes[a] - 1) + .count(); + let p = start.offset(r[0], r[1], r[2]); + if edges < 3 && (edges == 0 || !contained.contains(&(p.y >> 4))) { + expected.push(p); + } + } + expected } #[test] @@ -150,7 +186,10 @@ fn cells_follow_the_cursor_order_without_corners() { Aabb::new(Vec3::new(-0.3, 0.0, -0.3), Vec3::new(0.3, 0.9, 0.3)), ]; for bounds in boxes { - let mut world = Recording { cells: Vec::new() }; + let mut world = Recording { + cells: Vec::new(), + contained: Vec::new(), + }; let hits = BlockCollisionScan::new( &mut world, CollisionContext::empty(false), @@ -161,31 +200,45 @@ fn cells_follow_the_cursor_order_without_corners() { .unwrap() .count(); assert_eq!(hits, 0); - // Cursor3D: x fastest, then y, then z, skipping the 8 corners. - let (min, max) = (bounds.min_position(), bounds.max_position()); - let start = BlockPos::containing(Vec3::new(min.x - 1.0e-7, min.y - 1.0e-7, min.z - 1.0e-7)) - .offset(-1, -1, -1); - let end = BlockPos::containing(Vec3::new(max.x + 1.0e-7, max.y + 1.0e-7, max.z + 1.0e-7)) - .offset(1, 1, 1); - let sizes = [ - end.x - start.x + 1, - end.y - start.y + 1, - end.z - start.z + 1, - ]; - let mut expected = Vec::new(); - for i in 0..sizes[0] * sizes[1] * sizes[2] { - let r = [ - i % sizes[0], - i / sizes[0] % sizes[1], - i / sizes[0] / sizes[1], - ]; - let edges = (0..3) - .filter(|&a| r[a] == 0 || r[a] == sizes[a] - 1) - .count(); - if edges < 3 { - expected.push(start.offset(r[0], r[1], r[2])); - } - } - assert_eq!(world.cells, expected); + assert_eq!(world.cells, expected_cells(bounds, &[])); + } +} + +/// Outer cells are read only in sections that may hold a block reaching +/// out of its cell; the box's own cells always are. +#[test] +fn outer_cells_of_contained_sections_are_not_read() { + // A mob crossing from section 3 into section 4, and an item in section 0. + let boxes = [ + ( + Aabb::new(Vec3::new(0.2, 63.5, 0.3), Vec3::new(0.8, 65.3, 0.9)), + vec![3], + ), + ( + Aabb::new(Vec3::new(0.2, 63.5, 0.3), Vec3::new(0.8, 65.3, 0.9)), + vec![3, 4], + ), + ( + Aabb::new(Vec3::new(4.4, 2.0, -7.6), Vec3::new(4.65, 2.25, -7.35)), + vec![0], + ), + ]; + for (bounds, contained) in boxes { + let mut world = Recording { + cells: Vec::new(), + contained: contained.clone(), + }; + let hits = BlockCollisionScan::new( + &mut world, + CollisionContext::empty(false), + bounds, + false, + 1_000_000, + ) + .unwrap() + .count(); + assert_eq!(hits, 0); + assert_eq!(world.cells, expected_cells(bounds, &contained)); + assert!(world.cells.len() < expected_cells(bounds, &[]).len()); } } From bb9c474c643f45ba551899bfe24ea360daeb2e6e Mon Sep 17 00:00:00 2001 From: levzzz <86072227+levzzz5154@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:48:58 +0500 Subject: [PATCH 152/238] End city banners keep their template patterns A structure template saves a banner's layers in its block entity NBT, and StructureTemplate.placeInWorld hands that NBT to the level, where BannerBlockEntity.loadAdditional reads `patterns`. Horizon built the client block entity from the block alone (BlockEntity::for_block), so the End cities' magenta banners came out bare. Fixes #1438. Co-Authored-By: [CC] --- crates/horizon-client/src/structure_blocks.rs | 89 ++++++++++++++++--- 1 file changed, 78 insertions(+), 11 deletions(-) diff --git a/crates/horizon-client/src/structure_blocks.rs b/crates/horizon-client/src/structure_blocks.rs index 1e1906f6d..da5b75258 100644 --- a/crates/horizon-client/src/structure_blocks.rs +++ b/crates/horizon-client/src/structure_blocks.rs @@ -128,6 +128,44 @@ fn text(nbt: &Compound, key: &str) -> Option { } } +/// `BannerBlockEntity.loadAdditional`: the `patterns` layers and the custom +/// name a structure template saves with its banner (the End cities' tower +/// tops place black triangles this way). +fn banner_from_nbt(nbt: &Compound) -> crate::containers::Banner { + crate::containers::Banner { + layers: crate::banner::layers_from_nbt(nbt.get(&JavaString::from("patterns"))), + name: text(nbt, "CustomName"), + components: match nbt.get(&JavaString::from("components")) { + Some(Tag::Compound(c)) => c.clone(), + _ => Compound::new(), + }, + } +} + +/// The client block entity for one of a chunk's generated block entities, +/// before a container's loot is unpacked. +fn generated_block_entity(block: &str, nbt: &Compound) -> Option { + let mut entity = BlockEntity::for_block(block)?; + let tag = Tag::Compound(nbt.clone()); + // MonsterRoomFeature and the structures' setEntityId. + if let BlockEntity::Spawner(s) = &mut entity { + **s = crate::mobs::spawner::Spawner::load(&tag); + } + if let BlockEntity::TrialSpawner(s) = &mut entity { + **s = crate::mobs::trial_spawner::TrialSpawner::load(&tag); + } + if let BlockEntity::Vault(v) = &mut entity { + **v = crate::vault::Vault::load(&tag); + } + // StructureTemplate.placeInWorld hands the template's block entity NBT to + // the level, so `BannerBlockEntity.loadAdditional` keeps the banner's + // patterns (the End cities' black triangles) and its name. + if let BlockEntity::Banner(b) = &mut entity { + *b = banner_from_nbt(nbt); + } + Some(entity) +} + /// Client block entities for the generated block entities of a chunk. pub fn block_entities( chunk: &ProtoChunk, @@ -137,19 +175,9 @@ pub fn block_entities( for (pos, nbt) in &chunk.block_entities { let [x, y, z] = *pos; let block = chunk.block_name(x, y, z); - let Some(mut entity) = BlockEntity::for_block(block) else { + let Some(mut entity) = generated_block_entity(block, nbt) else { continue; }; - // MonsterRoomFeature and the structures' setEntityId. - if let BlockEntity::Spawner(s) = &mut entity { - **s = crate::mobs::spawner::Spawner::load(&Tag::Compound(nbt.clone())); - } - if let BlockEntity::TrialSpawner(s) = &mut entity { - **s = crate::mobs::trial_spawner::TrialSpawner::load(&Tag::Compound(nbt.clone())); - } - if let BlockEntity::Vault(v) = &mut entity { - **v = crate::vault::Vault::load(&Tag::Compound(nbt.clone())); - } if let (BlockEntity::Chest(slots), Some(table), Some(loot)) = (&mut entity, text(nbt, "LootTable"), loot) { @@ -278,4 +306,43 @@ mod tests { stack.exploration = None; assert!(!has_pending_map(&loot_item(&stack))); } + + /// The End cities' tower tops save their banners' patterns in the + /// template; the placed banner must keep them (`EndCityPieces`). + #[test] + fn a_generated_banner_keeps_its_template_patterns() { + let patterns = Tag::List(vec![ + Tag::compound([ + ("color", Tag::String("black".into())), + ("pattern", Tag::String("minecraft:triangle_top".into())), + ]), + Tag::compound([ + ("color", Tag::String("black".into())), + ("pattern", Tag::String("minecraft:triangle_bottom".into())), + ]), + ]); + let nbt = Tag::compound([ + ("id", Tag::String("minecraft:banner".into())), + ("patterns", patterns), + ]); + let Tag::Compound(nbt) = nbt else { + unreachable!() + }; + let Some(BlockEntity::Banner(banner)) = + generated_block_entity("minecraft:magenta_banner", &nbt) + else { + panic!("a banner block makes a banner block entity"); + }; + assert_eq!(banner.layers.len(), 2); + assert_eq!(&*banner.layers[0].pattern, "minecraft:triangle_top"); + assert_eq!(&*banner.layers[1].pattern, "minecraft:triangle_bottom"); + assert_eq!(&*banner.layers[0].color, "black"); + // A banner the template saved without patterns stays bare. + let Some(BlockEntity::Banner(bare)) = + generated_block_entity("minecraft:white_banner", &Compound::new()) + else { + panic!("a banner block makes a banner block entity"); + }; + assert!(bare.layers.is_empty()); + } } From 200b756b4ae2cbd0cce186d1b4db8041805c9ac1 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 20:53:31 +0100 Subject: [PATCH 153/238] Sculk: pass over idle sensors and catalysts in the block entity tick A sculk sensor, shrieker or catalyst with no vibration chosen, travelling or waiting, and no charge cursors, ticks without changing anything (vanilla's VibrationSystem.Ticker.tick and SculkSpreader.updateCursors return at once). The tick now leaves those out before asking whether the chunks around them tick (nine lookups each) and before looking them up a second time. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/sculk.rs | 17 ++++++++++++----- crates/horizon-client/src/sculk.rs | 7 +++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/crates/horizon-client/src/game/sculk.rs b/crates/horizon-client/src/game/sculk.rs index a11a643c1..63909c592 100644 --- a/crates/horizon-client/src/game/sculk.rs +++ b/crates/horizon-client/src/game/sculk.rs @@ -718,17 +718,24 @@ impl Game<'_> { }) }; // The ticking ones in the index's order, with whether the chunks - // around them tick too. + // around them tick too. Most sensors and catalysts wait for a + // vibration or a death: their tick would change nothing, so they + // are passed over here (a vibration scheduled during these ticks is + // chosen on the next tick at the earliest, as in vanilla). + let entities = &self.world.block_entities; let due: Vec<((i32, i32, i32), bool)> = self .sculk .index .iter() .filter_map(|&key| { let (cx, cz) = (key.0 >> 4, key.2 >> 4); - ticks(cx, cz).then(|| { - let adjacent = (-1..=1).all(|dx| (-1..=1).all(|dz| ticks(cx + dx, cz + dz))); - (key, adjacent) - }) + if !ticks(cx, cz) + || matches!(entities.get(&key), Some(BlockEntity::Sculk(e)) if e.idle()) + { + return None; + } + let adjacent = (-1..=1).all(|dx| (-1..=1).all(|dz| ticks(cx + dx, cz + dz))); + Some((key, adjacent)) }) .collect(); for (key, adjacent) in due { diff --git a/crates/horizon-client/src/sculk.rs b/crates/horizon-client/src/sculk.rs index 3affbda77..eefedaa98 100644 --- a/crates/horizon-client/src/sculk.rs +++ b/crates/horizon-client/src/sculk.rs @@ -413,6 +413,13 @@ impl SculkEntity { spreader: SculkSpreader::default(), } } + /// Whether its block entity tick does nothing: no vibration chosen + /// or on its way, none waiting to be chosen, and no charge to spread. + pub fn idle(&self) -> bool { + self.vibration.current.is_none() + && self.vibration.selector.current().is_none() + && self.spreader.cursors.is_empty() + } pub fn for_block(name: &str) -> Option { Some(Self::new(match name { "minecraft:sculk_sensor" => SculkKind::Sensor, From 05396b4edfd63209708ecbf767e0b93eea272272 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 20:57:10 +0100 Subject: [PATCH 154/238] ui: experimental, opted into with experimental = ["ui"] in the manifest docs/api/stability.md: a new API starts experimental, usable only by opting in with a manifest field naming it. Capability ui now needs experimental = ["ui"] beside capabilities; without it the manifest is refused with that line to add. A capability promoted later stays accepted in the list, so such manifests keep loading. The ui test guest opts in; stability.md gets ui's row; docs/mods.md shows the key. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-mods/src/manifest.rs | 38 ++++++++++++++++++++++++++++ crates/horizon-mods/tests/sandbox.rs | 1 + docs/api/stability.md | 1 + docs/mods.md | 7 +++-- 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/crates/horizon-mods/src/manifest.rs b/crates/horizon-mods/src/manifest.rs index f1ac123b1..6070f99bd 100644 --- a/crates/horizon-mods/src/manifest.rs +++ b/crates/horizon-mods/src/manifest.rs @@ -49,6 +49,12 @@ pub struct Manifest { /// that are on may share one (`content::clash`). #[serde(default)] pub namespaces: Vec, + /// The experimental capabilities the mod opts into (`["ui"]`): each one + /// it lists in `capabilities` must be named here too, since it may change + /// in any release (docs/api/stability.md). One no longer experimental is + /// accepted here, so the manifest keeps loading once it is promoted. + #[serde(default)] + pub experimental: Vec, } #[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq)] @@ -99,6 +105,11 @@ impl Capability { Capability::Ui => "ui", } } + /// May change in any release: a mod uses it only with + /// `experimental = [""]` in its manifest. + pub fn is_experimental(self) -> bool { + matches!(self, Capability::Ui) + } /// What the player reads before turning the mod on: "May ". pub fn describe(self) -> &'static str { match self { @@ -169,6 +180,16 @@ impl Manifest { "{MANIFEST}: capability registries needs api = \"0.2\"" )); } + if let Some(c) = m + .capabilities + .iter() + .find(|c| c.is_experimental() && !m.experimental.contains(c)) + { + return Err(format!( + "{MANIFEST}: capability {c} is experimental and may change in any release: \ + add experimental = [\"{c}\"] to use it" + )); + } if !m.namespaces.is_empty() && !m.capabilities.contains(&Capability::Registries) { return Err(format!( "{MANIFEST}: namespaces are for content, which needs capability registries" @@ -421,4 +442,21 @@ capabilities = ["game-read", "hud"] assert_eq!(p.wasm.as_deref(), Some(&b"\0asm"[..])); std::fs::remove_dir_all(&dir).unwrap(); } + + #[test] + fn experimental_capabilities_need_the_opt_in() { + let base = "id = \"m\"\nname = \"M\"\nversion = \"1\"\napi = \"0.1\"\n"; + let err = Manifest::parse(&format!("{base}capabilities = [\"ui\"]\n")).unwrap_err(); + assert!(err.contains("experimental = [\"ui\"]"), "{err}"); + let m = Manifest::parse(&format!( + "{base}capabilities = [\"ui\"]\nexperimental = [\"ui\"]\n" + )) + .unwrap(); + assert_eq!(m.experimental, [Capability::Ui]); + // A capability that is (or became) stable is fine in the list. + Manifest::parse(&format!( + "{base}capabilities = [\"hud\"]\nexperimental = [\"hud\"]\n" + )) + .unwrap(); + } } diff --git a/crates/horizon-mods/tests/sandbox.rs b/crates/horizon-mods/tests/sandbox.rs index a229f748e..77f0975f3 100644 --- a/crates/horizon-mods/tests/sandbox.rs +++ b/crates/horizon-mods/tests/sandbox.rs @@ -23,6 +23,7 @@ name = "Test UI Guest" version = "0.1.0" api = "0.1" capabilities = ["chat", "ui"] +experimental = ["ui"] "#; /// Builds both mods once and writes their `.hmod` files; returns the folder. diff --git a/docs/api/stability.md b/docs/api/stability.md index 2d4476fc7..ffa9d9b11 100644 --- a/docs/api/stability.md +++ b/docs/api/stability.md @@ -87,6 +87,7 @@ Deprecations and breaking changes are in bold. The release notes of the game rel | Part | Level | Version | | --- | --- | --- | | Mod API: `log`, `game`, `chat`, `hud`, `storage`, `events` | preview | 0.1.0 | +| Mod API: `ui`, `ui-events` (a mod's screens and settings page) | experimental | manifest `experimental = ["ui"]` | | Content mods (`registries`: data-driven blocks and items) | preview | manifest `api = "0.2"` | | Server mods (events, world, server commands) | not started | manifest `api = "0.3"`, experimental first | | Script mods (Lua run by an interpreter mod, [mods.md](mods.md#ways-in)) | not started | | diff --git a/docs/mods.md b/docs/mods.md index 45e5ae02e..9b3b8aa94 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -86,7 +86,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | `chat` | `chat` | `show(text)`: a line only the local player sees, after the mod's name (8 per call) | | `hud` | `hud` | `set-panel(corner, lines)`, `clear()`: up to 16 lines of 256 characters | | `storage` | `storage` | `get(key)`, `set(key, value)`, `delete(key)`: a per-mod folder of up to 64 files, 4 MiB total, at most 1 MiB written per call | -| `ui` | `ui` | `open-screen`, `close-screen`, `set-settings`: a screen the mod owns and a page in Options > Mods, of text, item and list widgets the game lays out and draws with its own menus' code | +| `ui` | `ui` | `open-screen`, `close-screen`, `set-settings`: a screen the mod owns and a page in Options > Mods, of text, item and list widgets the game lays out and draws with its own menus' code. **Experimental:** the manifest needs `experimental = ["ui"]` | | Hook (export) | When | |---|---| @@ -284,10 +284,13 @@ authors = ["..."] api = "0.1" # the API the SDK targets side = "client" # client | server | both capabilities = ["game-read", "hud", "chat"] +experimental = [] # experimental capabilities it uses, e.g. ["ui"] ``` Unknown keys are refused, so a typo is an error, not a silently ignored -permission. +permission. An experimental capability (today `ui`) may change in any release +([stability](api/stability.md)): a mod uses it only by naming it in +`experimental` as well, so nobody depends on it by accident. ### Content mods (API 0.2) From befec1995f0cb89d11b4fd07354116805f6796a5 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 20:57:10 +0100 Subject: [PATCH 155/238] ui: a Settings button appears when the mod sets its page later has_settings was read once, when the mods loaded, so a mod that called set-settings from a command or a tick never got its Settings button until a reload. The Mods page's rows now follow each mod call's output. Also clippy with --features mods: needless borrows of the atlas in draw_mod_screen. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/mods.rs | 32 +++++++++++++++----------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs index 688e29da1..47a3ce07b 100644 --- a/crates/horizon-client/src/game/mods.rs +++ b/crates/horizon-client/src/game/mods.rs @@ -395,6 +395,12 @@ impl Game<'_> { .chat .add(&self.gui.atlas, line, None, self.ticks, focused); } + // A mod may set or clear its settings page in any call, not only + // while loading: its row's Settings button follows. + let rows = self.settings.mods.installed.iter_mut(); + for (row, e) in rows.zip(self.mods.mods.entries()) { + row.has_settings = e.has_ui_settings(); + } } /// The mods' HUD panels, stacked with the extras in their corners. pub(super) fn draw_mod_panels(&self, batch: &mut HudBatch, corners: &mut Corners) { @@ -521,10 +527,10 @@ impl Game<'_> { let mut items: Vec<(String, f32, f32)> = Vec::new(); let atlas = &self.gui.atlas; let in_world = self.player.is_some(); - ui::menu_background(batch, &atlas, 0.0, 0.0, batch.width, batch.height, in_world); + ui::menu_background(batch, atlas, 0.0, 0.0, batch.width, batch.height, in_world); ui::centered_text( batch, - &atlas, + atlas, &Text::literal(title), 0.0, 15.0, @@ -545,7 +551,7 @@ impl Game<'_> { }; ui::centered_text( batch, - &atlas, + atlas, &Text::literal(t.text.clone()), x, wy + 1.0, @@ -557,7 +563,7 @@ impl Game<'_> { if self.res.items().get(&i.item).is_some() { items.push((i.item.clone(), x + 2.0, wy + 2.0)); } else { - batch.text(&atlas, "?", x + 6.0, wy + 6.0, u32::MAX, true); + batch.text(atlas, "?", x + 6.0, wy + 6.0, u32::MAX, true); } let name = if i.name.is_empty() { i.item.rsplit(':').next().unwrap_or(&i.item).to_string() @@ -565,7 +571,7 @@ impl Game<'_> { i.name.clone() }; batch.text_styled( - &atlas, + atlas, &Text::literal(name), x + 22.0, wy + 6.0, @@ -582,12 +588,12 @@ impl Game<'_> { list_i += 1; let rows_top = wy + 4.0; let rows_bottom = wy + wh - 4.0; - batch.fill(&atlas, x, wy, MOD_UI_COLUMN, wh, 0x8000_0000); + batch.fill(atlas, x, wy, MOD_UI_COLUMN, wh, 0x8000_0000); for (b, yy) in [(x, wy), (x, wy + wh - 1.0)] { - batch.fill(&atlas, b, yy, MOD_UI_COLUMN, 1.0, 0xFF50_5050); + batch.fill(atlas, b, yy, MOD_UI_COLUMN, 1.0, 0xFF50_5050); } for (xx, yy) in [(x, wy), (x + MOD_UI_COLUMN - 1.0, wy)] { - batch.fill(&atlas, xx, yy, 1.0, wh, 0xFF50_5050); + batch.fill(atlas, xx, yy, 1.0, wh, 0xFF50_5050); } let content = l.rows.len() as f32 * 12.0; let first = (scroll.amount / 12.0) as usize; @@ -596,13 +602,13 @@ impl Game<'_> { for (vi, ri) in (first..last).enumerate() { let ry = rows_top + vi as f32 * 12.0 - (scroll.amount % 12.0); if ui::hovered(self.cursor, x, ry, MOD_UI_COLUMN - 6.0, 12.0) { - batch.fill(&atlas, x + 1.0, ry, MOD_UI_COLUMN - 8.0, 12.0, 0x20FF_FFFF); + batch.fill(atlas, x + 1.0, ry, MOD_UI_COLUMN - 8.0, 12.0, 0x20FF_FFFF); } if l.selected == Some(ri as u32) { - batch.fill(&atlas, x + 1.0, ry, 3.0, 12.0, 0xFFFF_FFFF); + batch.fill(atlas, x + 1.0, ry, 3.0, 12.0, 0xFFFF_FFFF); } batch.text_styled( - &atlas, + atlas, &Text::literal(l.rows[ri].clone()), x + 6.0, ry + 2.0, @@ -612,7 +618,7 @@ impl Game<'_> { } ui::scrollbar( batch, - &atlas, + atlas, x + MOD_UI_COLUMN - 5.0, rows_top, rows_bottom, @@ -625,7 +631,7 @@ impl Game<'_> { let hover = ui::hovered(self.cursor, cx - 100.0, batch.height - 27.0, 200.0, 20.0); ui::button( batch, - &atlas, + atlas, cx - 100.0, batch.height - 27.0, 200.0, From de0f76476f1ee603c7d86d167b669d9680acfdb7 Mon Sep 17 00:00:00 2001 From: vlesmentie <129341611+vlesmentie@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:10:26 -0300 Subject: [PATCH 156/238] Big dripleaves tilt under a player standing on them The leaf's tilt ticks, its reset and its redstone reset were already in horizon-world (big_dripleaf_tick, big_dripleaf_reset_tilt), but nothing started them: BigDripleafBlock.entityInside was missing. An entity on the ground whose feet are above the leaf's y + 0.6875 (canEntityTilt) tilts a flat, unpowered leaf UNSTABLE and schedules its tick in 10. The block tick's inside scan now also passes the host's and the LAN guests' feet and on-ground flag to Behaviours::entity_on_big_dripleaf. Fixes #1683. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 31 ++++++++++++++++ crates/horizon-client/src/game/lan_mobs.rs | 13 +++++++ crates/horizon-world/src/level/hooks.rs | 23 ++++++++++++ crates/horizon-world/src/level/mod.rs | 13 +++++++ crates/horizon-world/src/level/tests.rs | 41 ++++++++++++++++++++++ 5 files changed, 121 insertions(+) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index f05ee41e4..9edb3c17d 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2575,10 +2575,41 @@ impl Game<'_> { } } } + // BigDripleafBlock.entityInside also asks whether the player stands on + // the ground and how high its feet are: the leaves its box's feet + // layer is inside (the host and the LAN guests). + let mut standing = Vec::new(); + let feet = self + .player + .iter() + .filter(|_| !self.spectator()) + .map(|p| { + ( + [p.position.x, p.position.y, p.position.z], + p.ground.on_ground, + f64::from(p.dimensions.width) / 2.0, + ) + }) + .chain( + self.lan_guest_feet() + .into_iter() + .map(|(at, on_ground)| (at, on_ground, 0.3)), + ); + for (at, on_ground, w) in feet { + let y = (at[1] + 1e-7).floor() as i32; + for x in (at[0] - w + 1e-7).floor() as i32..=(at[0] + w - 1e-7).floor() as i32 { + for z in (at[2] - w + 1e-7).floor() as i32..=(at[2] + w - 1e-7).floor() as i32 { + standing.push((BlockPos::new(x, y, z), on_ground, at[1])); + } + } + } self.with_level(|b, level, ticks| { for pos in inside { b.entity_inside(level, ticks, pos); } + for (pos, on_ground, feet_y) in standing { + b.entity_on_big_dripleaf(level, ticks, pos, on_ground, feet_y); + } }); self.process_level_events()?; self.tick_scheduled()?; diff --git a/crates/horizon-client/src/game/lan_mobs.rs b/crates/horizon-client/src/game/lan_mobs.rs index be277b6e0..dc4d1eb42 100644 --- a/crates/horizon-client/src/game/lan_mobs.rs +++ b/crates/horizon-client/src/game/lan_mobs.rs @@ -171,6 +171,19 @@ impl Game<'_> { }) .collect() } + /// The same guests' feet and whether they stand on the ground, for the + /// blocks that also ask that (`BigDripleafBlock.canEntityTilt`). + pub(super) fn lan_guest_feet(&self) -> Vec<([f64; 3], bool)> { + let Some(host) = self.lan.as_ref() else { + return Vec::new(); + }; + host.hub + .guests + .iter() + .filter(|g| host.hub.here(g) && g.loaded && !g.dead && !g.spectator()) + .map(|g| (g.position, g.on_ground)) + .collect() + } /// The guests a projectile can hit, under their entity ids. pub(super) fn lan_projectile_targets(&self) -> Vec<(Who, Aabb)> { diff --git a/crates/horizon-world/src/level/hooks.rs b/crates/horizon-world/src/level/hooks.rs index 0695a92b3..7f9e8d19a 100644 --- a/crates/horizon-world/src/level/hooks.rs +++ b/crates/horizon-world/src/level/hooks.rs @@ -573,6 +573,29 @@ impl Sim<'_, L> { self.game_event_state(pos, "minecraft:block_destroy", state); } } + /// `BigDripleafBlock.entityInside` on a server level: an entity standing on + /// a flat leaf (`canEntityTilt`: on the ground, its feet above the leaf's + /// lowest top, 11/16, less 4/16: 0.6875) tilts it UNSTABLE unless the leaf + /// is powered; `setTiltAndScheduleTick` with no sound (UNSTABLE causes no + /// vibration) and the next state in 10 ticks. + pub(crate) fn big_dripleaf_entity_inside( + &mut self, + pos: BlockPos, + on_ground: bool, + feet_y: f64, + ) { + let b = self.b; + let state = self.state(pos); + if !b.data(state).is(Class::BigDripleaf) + || b.table.text(state, b.props.tilt) != "none" + || !(on_ground && feet_y > f64::from(pos.y) + f64::from(0.6875_f32)) + || self.has_neighbor_signal(pos) + { + return; + } + self.set_block(pos, b.table.set_text(state, b.props.tilt, "unstable"), 2); + self.schedule_tick(pos, b.block(state), 10); + } /// `BigDripleafBlock.resetTilt`. pub(crate) fn big_dripleaf_reset_tilt(&mut self, pos: BlockPos, state: BlockStateId) { let b = self.b; diff --git a/crates/horizon-world/src/level/mod.rs b/crates/horizon-world/src/level/mod.rs index 696777386..3831efbd6 100644 --- a/crates/horizon-world/src/level/mod.rs +++ b/crates/horizon-world/src/level/mod.rs @@ -749,6 +749,19 @@ impl Behaviours { ) { self.sim(level, ticks).entity_inside(pos); } + /// `BigDripleafBlock.entityInside` for an entity whose box is inside + /// `pos`: whether it is on the ground and its feet's y (`canEntityTilt`). + pub fn entity_on_big_dripleaf( + &self, + level: &mut L, + ticks: &mut Ticks, + pos: BlockPos, + on_ground: bool, + feet_y: f64, + ) { + self.sim(level, ticks) + .big_dripleaf_entity_inside(pos, on_ground, feet_y); + } /// Redstone signal queries (`SignalGetter`). pub fn has_neighbor_signal( &self, diff --git a/crates/horizon-world/src/level/tests.rs b/crates/horizon-world/src/level/tests.rs index 1a1b9ebae..8a4b6c34f 100644 --- a/crates/horizon-world/src/level/tests.rs +++ b/crates/horizon-world/src/level/tests.rs @@ -1283,3 +1283,44 @@ fn player_breaks_fire_with_extinguish_not_generic_break_event() { assert_eq!(w.get(pos), w.b().air()); } } + +#[test] +fn big_dripleaf_tilts_under_an_entity_standing_on_it() { + let mut w = world!(); + w.floor(0, 2, "clay"); + let pos = BlockPos::new(0, 1, 0); + let leaf = w.id("big_dripleaf", &[("facing", "north")]); + w.put(pos, leaf); + let stand = |w: &mut World, on_ground: bool, feet: f64| { + w.f.behaviours + .entity_on_big_dripleaf(&mut w.level, &mut w.ticks, pos, on_ground, feet); + }; + // canEntityTilt: in the air, or with its feet at or under 0.6875, nothing. + stand(&mut w, false, 1.9375); + stand(&mut w, true, 1.6875); + assert_eq!(w.prop(pos, "tilt"), "none"); + // Standing on the leaf's top (15/16): UNSTABLE, then PARTIAL and FULL + // 10 ticks apart, and back to NONE 100 ticks later. + stand(&mut w, true, 1.9375); + assert_eq!(w.prop(pos, "tilt"), "unstable"); + for _ in 0..10 { + w.tick(); + } + assert_eq!(w.prop(pos, "tilt"), "partial"); + for _ in 0..10 { + w.tick(); + } + assert_eq!(w.prop(pos, "tilt"), "full"); + // A tilted leaf doesn't restart under the entity. + stand(&mut w, true, 1.9375); + assert_eq!(w.prop(pos, "tilt"), "full"); + for _ in 0..100 { + w.tick(); + } + assert_eq!(w.prop(pos, "tilt"), "none"); + // A powered leaf doesn't tilt. + let power = w.id("redstone_block", &[]); + w.put(BlockPos::new(1, 1, 0), power); + stand(&mut w, true, 1.9375); + assert_eq!(w.prop(pos, "tilt"), "none"); +} From 74e3b0adf6937575dd5c5a3d2ea99ed0cb490eec Mon Sep 17 00:00:00 2001 From: socksprox Date: Sun, 25 Oct 2026 21:10:52 +0100 Subject: [PATCH 157/238] ui: recapture the mouse when a mod's screen vanishes, borrow its widgets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review on #1864: when a mod stops while its screen or settings page is up, draw_mod_screen dropped the player back to Playing without capture(true), leaving the mouse free until the next click into the world. The dead surface now leaves through close_mod_screen, which puts back the screen it covered and holds the mouse on a return to Playing; the same goes for a screen dropped while it was up. mod_ui_view also cloned the title and every widget (up to 64, lists to 512 rows) on each frame, click and scroll. It now borrows the mod's stored widgets — the screen's scrolls live in a disjoint field — and the click finds the picked row first so the &mut ui_select runs after. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- crates/horizon-client/src/game/mods.rs | 87 +++++++++++++------------- 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs index 47a3ce07b..d2e9627e6 100644 --- a/crates/horizon-client/src/game/mods.rs +++ b/crates/horizon-client/src/game/mods.rs @@ -425,30 +425,28 @@ impl Game<'_> { } /// A `ui` screen's title and widgets as the mod last pushed them; None - /// when the surface is gone (the mod stopped or closed it). - fn mod_ui_view(&self) -> Option<(String, Vec)> { - let ms = self.mods.screen.as_ref()?; + /// when the surface is gone (the mod stopped or closed it). Borrows + /// `mods` only, so `self.mods.screen` may stay borrowed for the scrolls. + fn mod_ui_view<'a>(mods: &'a Mods, ms: &ModScreen) -> Option<(&'a str, &'a [UiWidget])> { if ms.settings { - let widgets = self.mods.mods.settings_widgets(ms.owner); + let widgets = mods.settings_widgets(ms.owner); if widgets.is_empty() { return None; } - let name = self - .mods - .mods + let name = mods .entries() .get(ms.owner) .and_then(|e| e.manifest.as_ref()) .map_or("Mod", |m| m.name.as_str()); - Some((name.to_string(), widgets.to_vec())) + Some((name, widgets)) } else { - let s: &UiScreen = self.mods.mods.screen(ms.owner)?; - Some((s.title.clone(), s.widgets.clone())) + let s: &UiScreen = mods.screen(ms.owner)?; + Some((s.title.as_str(), s.widgets.as_slice())) } } /// The layout the mod screen's widgets get at `h`: (widget, top, /// height), the column being `COLUMN` wide down the middle. - fn mod_ui_layout(&self, h: f32, widgets: &[UiWidget]) -> Vec<(f32, f32)> { + fn mod_ui_layout(h: f32, widgets: &[UiWidget]) -> Vec<(f32, f32)> { let mut out = Vec::with_capacity(widgets.len()); let mut y = MOD_UI_TOP; for w in widgets { @@ -513,13 +511,19 @@ impl Game<'_> { /// The mod's screen or settings page: its title over widgets stacked /// down the middle, a Done button, Esc to leave (vanilla's menus). pub(super) fn draw_mod_screen(&mut self, batch: &mut HudBatch) { - if self.mods.screen.is_none() { + let Some(ms) = self.mods.screen.as_mut() else { + // The screen was dropped while it was up: back to the world, + // mouse held again like any return to Playing. self.screen = Screen::Playing; + if self.player.is_some() { + self.capture(true); + } return; - } - let Some((title, widgets)) = self.mod_ui_view() else { - self.mods.screen = None; - self.screen = Screen::Playing; + }; + let Some((title, widgets)) = Self::mod_ui_view(&self.mods.mods, ms) else { + // The mod's surface is gone (it stopped or cleared it): leave + // the screen like Done did, without `closed` — the mod ended it. + self.close_mod_screen(false); return; }; // Item icons draw in a second pass: `draw_item` needs `&mut self`, @@ -539,7 +543,7 @@ impl Game<'_> { ); let cx = (batch.width / 2.0).floor(); let x = cx - MOD_UI_COLUMN / 2.0; - let layout = self.mod_ui_layout(batch.height, &widgets); + let layout = Self::mod_ui_layout(batch.height, widgets); let mut list_i = 0usize; for (w, &(wy, wh)) in widgets.iter().zip(&layout) { match w { @@ -580,11 +584,8 @@ impl Game<'_> { ); } UiWidget::List(l) => { - let scroll = { - let ms = self.mods.screen.as_mut().expect("checked"); - ms.scrolls.resize_with(list_i + 1, ui::Scroll::default); - ms.scrolls[list_i] - }; + ms.scrolls.resize_with(list_i + 1, ui::Scroll::default); + let scroll = ms.scrolls[list_i]; list_i += 1; let rows_top = wy + 4.0; let rows_bottom = wy + wh - 4.0; @@ -655,53 +656,53 @@ impl Game<'_> { self.close_mod_screen(true); return; } - let Some((_, widgets)) = self.mod_ui_view() else { + let Some(ms) = self.mods.screen.as_ref() else { return; }; + let Some((_, widgets)) = Self::mod_ui_view(&self.mods.mods, ms) else { + return; + }; + let owner = ms.owner; let x = (w / 2.0).floor() - MOD_UI_COLUMN / 2.0; - let layout = self.mod_ui_layout(h, &widgets); + let layout = Self::mod_ui_layout(h, widgets); let mut list_i = 0usize; + // The `ui_select` call needs `&mut`: find the pick first. + let mut hit = None; for (w, &(wy, wh)) in widgets.iter().zip(&layout) { let UiWidget::List(l) = w else { continue }; - let scroll = self - .mods - .screen - .as_ref() - .and_then(|ms| ms.scrolls.get(list_i).copied()) - .unwrap_or_default(); + let scroll = ms.scrolls.get(list_i).copied().unwrap_or_default(); list_i += 1; let row = ((cursor.1 - (wy + 4.0) + scroll.amount) / 12.0) as i64; if ui::hovered(cursor, x + 1.0, wy + 1.0, MOD_UI_COLUMN - 7.0, wh - 2.0) && row >= 0 && (row as usize) < l.rows.len() { - self.audio.play_button_click(); - self.mods.mods.ui_select( - self.mods.screen.as_ref().map_or(0, |ms| ms.owner), - l.id, - row as u32, - ); - self.show_mod_output(); - return; + hit = Some((l.id, row as u32)); + break; } } + if let Some((id, row)) = hit { + self.audio.play_button_click(); + self.mods.mods.ui_select(owner, id, row); + self.show_mod_output(); + } } /// The wheel over a `list` scrolls it. pub(super) fn mod_screen_scroll(&mut self, delta: f32) { let (w, h) = self.gui_size(); let cursor = self.cursor; - let Some((_, widgets)) = self.mod_ui_view() else { + let Some(ms) = self.mods.screen.as_mut() else { + return; + }; + let Some((_, widgets)) = Self::mod_ui_view(&self.mods.mods, ms) else { return; }; let x = (w / 2.0).floor() - MOD_UI_COLUMN / 2.0; - let layout = self.mod_ui_layout(h, &widgets); + let layout = Self::mod_ui_layout(h, widgets); let mut list_i = 0usize; for (w, &(wy, wh)) in widgets.iter().zip(&layout) { let UiWidget::List(l) = w else { continue }; let over = ui::hovered(cursor, x, wy, MOD_UI_COLUMN, wh); - let Some(ms) = self.mods.screen.as_mut() else { - return; - }; ms.scrolls.resize_with(list_i + 1, ui::Scroll::default); if over { let content = l.rows.len() as f32 * 12.0; From 03a5cbdcd800bb6d41c3f6ffb88e875ce25e9f07 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 21:11:47 +0100 Subject: [PATCH 158/238] Renderer: OpenGL as the last fallback, for GPUs with no Vulkan or DirectX 12 driver wgpu's GL backend (WGL on Windows, EGL on Linux). Auto tries Vulkan, then DirectX 12 on Windows, then OpenGL; graphicsApi:opengl (or HORIZON_GRAPHICS_API=opengl) picks it. A Radeon HD 7400M (TeraScale 2: DirectX 11, OpenGL 4.x) could not start Horizon at all. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/Cargo.toml | 5 +++ crates/horizon-client/src/gfx.rs | 64 +++++++++++++++------------ crates/horizon-client/src/graphics.rs | 15 +++++-- 3 files changed, 53 insertions(+), 31 deletions(-) diff --git a/crates/horizon-client/Cargo.toml b/crates/horizon-client/Cargo.toml index ea5859c23..4877c18f2 100644 --- a/crates/horizon-client/Cargo.toml +++ b/crates/horizon-client/Cargo.toml @@ -68,6 +68,11 @@ rustix = { version = "1", default-features = false, features = ["std", "time"] } # on the PATH. wgpu = { version = "30.0.1", default-features = false, features = ["dx12"] } +[target.'cfg(not(target_os = "macos"))'.dependencies] +# OpenGL (WGL on Windows, EGL on Linux), the last fallback, for GPUs with no Vulkan or +# DirectX 12 driver (`graphicsApi`, graphics.rs). +wgpu = { version = "30.0.1", default-features = false, features = ["gles"] } + [target.'cfg(windows)'.build-dependencies] # The icon resource in the .exe (build.rs). winresource = "0.1" diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index b43202074..f2fdfb6b3 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -683,25 +683,31 @@ fn instance_descriptor(backends: wgpu::Backends) -> wgpu::InstanceDescriptor { } /// The backends to open a device on, each tried in turn until one gives a /// device. `api` is the player's choice (`GraphicsApi::with_env` applied); -/// `wgpu_backend`: whether `WGPU_BACKEND` is set, which picks the backends -/// itself. Only Windows has two: Auto tries Vulkan, then DirectX 12 when -/// Vulkan has no surface, adapter (no Vulkan driver) or device. A native crash in -/// the driver can't be caught in the game; the launcher switches the setting -/// to DirectX 12 after one at the renderer's start (#746). -fn backend_order(api: GraphicsApi, windows: bool, wgpu_backend: bool) -> Vec { - if !windows || wgpu_backend { - return vec![wgpu::Backends::default()]; +/// `os` is `std::env::consts::OS`; `wgpu_backend`: whether `WGPU_BACKEND` is set, +/// which picks the backends itself. Auto tries Vulkan, then DirectX 12 on Windows, +/// then OpenGL (Windows and Linux) for GPUs with neither driver, each when the one +/// before has no surface, adapter or device. A native crash in the driver can't be +/// caught in the game; the launcher switches the setting to DirectX 12 after one at +/// the renderer's start (#746). macOS has Metal only. +fn backend_order(api: GraphicsApi, os: &str, wgpu_backend: bool) -> Vec { + use wgpu::Backends; + if os == "macos" || wgpu_backend { + return vec![Backends::default()]; } + let windows = os == "windows"; match api { - GraphicsApi::Auto => vec![wgpu::Backends::VULKAN, wgpu::Backends::DX12], - GraphicsApi::Vulkan => vec![wgpu::Backends::VULKAN], - GraphicsApi::Dx12 => vec![wgpu::Backends::DX12], + GraphicsApi::Auto if windows => vec![Backends::VULKAN, Backends::DX12, Backends::GL], + GraphicsApi::Auto => vec![Backends::VULKAN, Backends::GL], + GraphicsApi::Dx12 if windows => vec![Backends::DX12], + GraphicsApi::Vulkan | GraphicsApi::Dx12 => vec![Backends::VULKAN], + GraphicsApi::OpenGl => vec![Backends::GL], } } fn backends_name(backends: wgpu::Backends) -> &'static str { match backends { wgpu::Backends::VULKAN => "Vulkan", wgpu::Backends::DX12 => "DirectX 12", + wgpu::Backends::GL => "OpenGL", _ => "graphics", } } @@ -780,7 +786,7 @@ async fn open_device( ) -> Result { let order = backend_order( api.with_env(), - cfg!(windows), + std::env::consts::OS, std::env::var_os("WGPU_BACKEND").is_some(), ); let mut failures = Vec::new(); @@ -869,14 +875,14 @@ async fn open_device( /// Vulkan is loaded at run time (the game links no graphics library), so a /// missing driver shows here rather than as a missing-DLL dialog. #[cfg(target_os = "windows")] -const NO_GPU_DRIVER: &str = "No graphics driver with Vulkan or DirectX 12 found: Horizon needs a GPU \ - driver with one of them. Install the latest driver for your NVIDIA, AMD or Intel GPU and \ +const NO_GPU_DRIVER: &str = "No graphics driver with Vulkan, DirectX 12 or OpenGL found: Horizon needs \ + a GPU driver with one of them. Install the latest driver for your NVIDIA, AMD or Intel GPU and \ start the game again"; #[cfg(target_os = "macos")] const NO_GPU_DRIVER: &str = "No Metal graphics device found"; #[cfg(not(any(target_os = "windows", target_os = "macos")))] -const NO_GPU_DRIVER: &str = "No Vulkan graphics driver found: Horizon needs the Vulkan loader \ - (libvulkan.so.1) and a Vulkan driver for your GPU (Mesa's, or NVIDIA's)"; +const NO_GPU_DRIVER: &str = "No Vulkan or OpenGL graphics driver found: Horizon needs the Vulkan loader \ + (libvulkan.so.1) and a Vulkan driver for your GPU (Mesa's, or NVIDIA's), or OpenGL through EGL"; /// Logs a step of the renderer's start, for a native crash in the graphics /// driver: it leaves no panic or crash report, so the log's last line is the /// only sign of where the game stopped. The log file is written unbuffered; @@ -4475,25 +4481,27 @@ mod tests { ); } #[test] - fn windows_tries_vulkan_then_directx_12() { + fn auto_tries_vulkan_then_directx_12_then_opengl() { use super::{GraphicsApi, backend_order}; use wgpu::Backends; + let order = |api, os| backend_order(api, os, false); assert_eq!( - backend_order(GraphicsApi::Auto, true, false), - [Backends::VULKAN, Backends::DX12] + order(GraphicsApi::Auto, "windows"), + [Backends::VULKAN, Backends::DX12, Backends::GL] ); + assert_eq!(order(GraphicsApi::Vulkan, "windows"), [Backends::VULKAN]); + assert_eq!(order(GraphicsApi::Dx12, "windows"), [Backends::DX12]); + assert_eq!(order(GraphicsApi::OpenGl, "windows"), [Backends::GL]); assert_eq!( - backend_order(GraphicsApi::Vulkan, true, false), - [Backends::VULKAN] + order(GraphicsApi::Auto, "linux"), + [Backends::VULKAN, Backends::GL] ); - assert_eq!( - backend_order(GraphicsApi::Dx12, true, false), - [Backends::DX12] - ); - // WGPU_BACKEND picks the backends itself; other systems have one. + assert_eq!(order(GraphicsApi::Dx12, "linux"), [Backends::VULKAN]); + assert_eq!(order(GraphicsApi::OpenGl, "linux"), [Backends::GL]); + // WGPU_BACKEND picks the backends itself; macOS has Metal only. for api in GraphicsApi::ALL { - assert_eq!(backend_order(api, true, true), [Backends::default()]); - assert_eq!(backend_order(api, false, false), [Backends::default()]); + assert_eq!(backend_order(api, "windows", true), [Backends::default()]); + assert_eq!(order(api, "macos"), [Backends::default()]); } } /// Every WGSL shader file translates to HLSL as wgpu's DirectX 12 backend diff --git a/crates/horizon-client/src/graphics.rs b/crates/horizon-client/src/graphics.rs index 3cf1402d2..21d38539e 100644 --- a/crates/horizon-client/src/graphics.rs +++ b/crates/horizon-client/src/graphics.rs @@ -98,19 +98,27 @@ impl PresentMode { /// macOS). Applies at the next start. #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] pub enum GraphicsApi { - /// Vulkan, and DirectX 12 when Vulkan has no adapter or device. + /// Vulkan, then DirectX 12 (Windows), then OpenGL when neither has a surface, adapter or device. #[default] Auto, Vulkan, Dx12, + /// OpenGL, for GPUs with no Vulkan or DirectX 12 driver (Windows and Linux). + OpenGl, } impl GraphicsApi { - pub const ALL: [GraphicsApi; 3] = [GraphicsApi::Auto, GraphicsApi::Vulkan, GraphicsApi::Dx12]; + pub const ALL: [GraphicsApi; 4] = [ + GraphicsApi::Auto, + GraphicsApi::Vulkan, + GraphicsApi::Dx12, + GraphicsApi::OpenGl, + ]; pub fn name(self) -> &'static str { match self { GraphicsApi::Auto => "auto", GraphicsApi::Vulkan => "vulkan", GraphicsApi::Dx12 => "dx12", + GraphicsApi::OpenGl => "opengl", } } pub fn label(self) -> &'static str { @@ -118,6 +126,7 @@ impl GraphicsApi { GraphicsApi::Auto => "Auto", GraphicsApi::Vulkan => "Vulkan", GraphicsApi::Dx12 => "DirectX 12", + GraphicsApi::OpenGl => "OpenGL", } } /// The API a name stands for, ignoring case; `None` for anything else. @@ -126,7 +135,7 @@ impl GraphicsApi { .into_iter() .find(|api| api.name().eq_ignore_ascii_case(s.trim())) } - /// `HORIZON_GRAPHICS_API` (auto, vulkan or dx12) if set, else the setting. + /// `HORIZON_GRAPHICS_API` (auto, vulkan, dx12 or opengl) if set, else the setting. pub fn with_env(self) -> Self { std::env::var("HORIZON_GRAPHICS_API") .ok() From a013e35a61f3d888f141364ab5e04f13fbb1a842 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 21:17:59 +0100 Subject: [PATCH 159/238] Collision scans: pass the rest of an outer row in a contained section at once On the rows along a box's bottom, top and sides every cell is outer, so in a section no block reaches out of, the rest of the row up to the chunk's edge is passed in one step instead of cell by cell. The candidate budget still ends the scan where it did. Co-Authored-By: Claude Opus 5.5 --- .../horizon-world/src/block_collision_scan.rs | 35 ++++++++++++++++++- .../tests/block_collision_scan.rs | 31 ++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/crates/horizon-world/src/block_collision_scan.rs b/crates/horizon-world/src/block_collision_scan.rs index 391c3e96e..172e6a79c 100644 --- a/crates/horizon-world/src/block_collision_scan.rs +++ b/crates/horizon-world/src/block_collision_scan.rs @@ -151,12 +151,31 @@ impl<'w, 'c, W: BlockCollisionWorld> BlockCollisionScan<'w, 'c, W> { }) } + /// Move the cursor `n` cells further along its row; `n` must not pass + /// the row's end. + fn skip(&mut self, n: usize) { + if n == 0 { + return; + } + let [width, height, _] = self.sizes; + self.index += n; + self.cursor[0] += n; + if self.cursor[0] == width { + self.cursor[0] = 0; + self.cursor[1] += 1; + if self.cursor[1] == height { + self.cursor[1] = 0; + self.cursor[2] += 1; + } + } + } + fn find_next(&mut self) -> Result, CollisionScanError> { while self.index < self.count { if self.index >= self.budget { return Err(CollisionScanError::VisitBudget); } - let [width, height, _] = self.sizes; + let [width, height, depth] = self.sizes; let relative = self.cursor; self.index += 1; self.cursor[0] += 1; @@ -212,6 +231,20 @@ impl<'w, 'c, W: BlockCollisionWorld> BlockCollisionScan<'w, 'c, W> { } }; if !reaches { + // On a row along the box's bottom, top or sides every + // cell is outer: the rest of it in this chunk is passed + // at once (never beyond the budget, which still ends the + // scan where it would have). + if relative[1] == 0 + || relative[1] == height - 1 + || relative[2] == 0 + || relative[2] == depth - 1 + { + let rest = (width - 1 - relative[0]) + .min(15 - (p.x & 15) as usize) + .min(self.budget.saturating_sub(self.index)); + self.skip(rest); + } continue; } } diff --git a/crates/horizon-world/tests/block_collision_scan.rs b/crates/horizon-world/tests/block_collision_scan.rs index adf530069..e3f61e1ae 100644 --- a/crates/horizon-world/tests/block_collision_scan.rs +++ b/crates/horizon-world/tests/block_collision_scan.rs @@ -222,6 +222,11 @@ fn outer_cells_of_contained_sections_are_not_read() { Aabb::new(Vec3::new(4.4, 2.0, -7.6), Vec3::new(4.65, 2.25, -7.35)), vec![0], ), + // Across a chunk's edge: the rest of a row is passed only up to it. + ( + Aabb::new(Vec3::new(14.2, 70.5, -0.4), Vec3::new(17.8, 71.5, 0.4)), + vec![4], + ), ]; for (bounds, contained) in boxes { let mut world = Recording { @@ -242,3 +247,29 @@ fn outer_cells_of_contained_sections_are_not_read() { assert!(world.cells.len() < expected_cells(bounds, &[]).len()); } } + +/// Passing outer cells at once still stops at the candidate budget, as +/// visiting them one by one does. +#[test] +fn passed_cells_count_toward_the_budget() { + let bounds = Aabb::new(Vec3::new(0.2, 63.5, 0.3), Vec3::new(0.8, 65.3, 0.9)); + for budget in 0..80 { + let scan = |contained: Vec| { + let mut world = Recording { + cells: Vec::new(), + contained, + }; + BlockCollisionScan::new( + &mut world, + CollisionContext::empty(false), + bounds, + false, + budget, + ) + .unwrap() + .map(|hit| hit.map(|_| ()).map_err(|e| e.to_string())) + .collect::>() + }; + assert_eq!(scan(vec![3, 4]), scan(Vec::new()), "budget {budget}"); + } +} From 54bb3e4fbe869bfc223d0daa2ccf2fa329c721e8 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 21:19:47 +0100 Subject: [PATCH 160/238] Screen effects: one sampler per entry point, so the shader compiles for OpenGL GLSL combines a texture with its sampler, and naga refuses an entry point that reads one texture through two (ImageMultipleSamplers): the BLUR override left both branches in fs_screen and fs_vignette, so creating the screen effect pipelines failed on wgpu's GL backend at startup. Blurred sprites now have their own entry points (fs_screen_blur, fs_vignette_blur); each pipeline still samples once per pixel. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 17 +++++++------- crates/horizon-client/src/screen.wgsl | 34 +++++++++++++++++---------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index f2fdfb6b3..b55022510 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -2069,16 +2069,15 @@ impl Renderer { // One per blend and `.png.mcmeta` sampling (screen_effects::SAMPLE_*): // the shader is specialised, so a pixel pays for none of the others. let screen_pipelines: [wgpu::RenderPipeline; 8] = std::array::from_fn(|i| { - let (entry, blend) = if i & 4 == 0 { - ("fs_screen", alpha_blend) - } else { - ("fs_vignette", vignette_blend) + let blur = i as u32 & crate::screen_effects::SAMPLE_BLUR != 0; + let (entry, blend) = match (i & 4 == 0, blur) { + (true, false) => ("fs_screen", alpha_blend), + (true, true) => ("fs_screen_blur", alpha_blend), + (false, false) => ("fs_vignette", vignette_blend), + (false, true) => ("fs_vignette_blur", vignette_blend), }; - let on = |bit: u32| f64::from(u8::from(i as u32 & bit != 0)); - let constants = [ - ("BLUR", on(crate::screen_effects::SAMPLE_BLUR)), - ("CLAMP", on(crate::screen_effects::SAMPLE_CLAMP)), - ]; + let clamp = i as u32 & crate::screen_effects::SAMPLE_CLAMP != 0; + let constants = [("CLAMP", f64::from(u8::from(clamp)))]; let compilation_options = wgpu::PipelineCompilationOptions { constants: &constants, ..Default::default() diff --git a/crates/horizon-client/src/screen.wgsl b/crates/horizon-client/src/screen.wgsl index e70aeb896..1497b36fa 100644 --- a/crates/horizon-client/src/screen.wgsl +++ b/crates/horizon-client/src/screen.wgsl @@ -8,10 +8,12 @@ // atlas, wrapped as repeat reads it (hud.rs), so the hardware's bilinear // sample is GL_LINEAR with repeat; clamp-to-edge keeps the sample point half // a texel inside. Each sampling has its own pipeline (gfx.rs), so a pixel -// costs what it did with nearest and repeat only: one sample. +// costs what it did with nearest and repeat only: one sample. Each entry +// point reads `atlas` through one sampler only: GLSL (wgpu's OpenGL backend) +// combines a texture with its sampler, and an override would not remove the +// other sampler's branch. @group(0) @binding(2) var nearest_sampler: sampler; @group(0) @binding(4) var linear_sampler: sampler; -override BLUR: bool = false; override CLAMP: bool = false; struct Out { @builtin(position) clip: vec4, @@ -32,28 +34,36 @@ struct Out { out.edges = vec4(rect.xy + half, rect.zw - half); return out; } -fn sample(in: Out) -> vec4 { +// The sprite's atlas coordinates of this pixel. +fn atlas_uv(in: Out) -> vec2 { var f = in.uv; if !CLAMP { // Repeat: one period of the sprite. f = fract(f); } - var uv = in.rect.xy + f * (in.rect.zw - in.rect.xy); + let uv = in.rect.xy + f * (in.rect.zw - in.rect.xy); if CLAMP { - uv = clamp(uv, in.edges.xy, in.edges.zw); + return clamp(uv, in.edges.xy, in.edges.zw); } - if BLUR { - return textureSampleLevel(atlas, linear_sampler, uv, 0.0) * in.color; - } - return textureSampleLevel(atlas, nearest_sampler, uv, 0.0) * in.color; + return uv; } -@fragment fn fs_screen(in: Out) -> @location(0) vec4 { - let c = sample(in); +fn screen(c: vec4) -> vec4 { if c.a <= 0.0 { discard; } return c; } +@fragment fn fs_screen(in: Out) -> @location(0) vec4 { + return screen(textureSampleLevel(atlas, nearest_sampler, atlas_uv(in), 0.0) * in.color); +} +@fragment fn fs_screen_blur(in: Out) -> @location(0) vec4 { + return screen(textureSampleLevel(atlas, linear_sampler, atlas_uv(in), 0.0) * in.color); +} // RenderPipelines.VIGNETTE blends ZERO, ONE_MINUS_SRC_COLOR: output the // texture colour; the blend state darkens the frame by it. @fragment fn fs_vignette(in: Out) -> @location(0) vec4 { - return vec4(sample(in).rgb, 1.0); + let c = textureSampleLevel(atlas, nearest_sampler, atlas_uv(in), 0.0) * in.color; + return vec4(c.rgb, 1.0); +} +@fragment fn fs_vignette_blur(in: Out) -> @location(0) vec4 { + let c = textureSampleLevel(atlas, linear_sampler, atlas_uv(in), 0.0) * in.color; + return vec4(c.rgb, 1.0); } From 903da4f1ab8f6735a8d60734a63bcecdd4edbe9e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 21:20:02 +0100 Subject: [PATCH 161/238] Cargo.lock: wgpu's GL backend dependencies (glow, khronos-egl, glutin_wgl_sys) Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 6945e09cf..4d749add6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1116,12 +1116,44 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "gl_generator" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a95dfc23a2b4a9a2f5ab41d194f8bfda3cabec42af4e39f08c339eb2a0c124d" +dependencies = [ + "khronos_api", + "log", + "xml-rs", +] + [[package]] name = "glam" version = "0.30.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19fc433e8437a212d1b6f1e68c7824af3aed907da60afa994e7f542d18d12aa9" +[[package]] +name = "glow" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29038e1c483364cc6bb3cf78feee1816002e127c331a1eec55a4d202b9e1adb5" +dependencies = [ + "js-sys", + "slotmap", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "glutin_wgl_sys" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c4ee00b289aba7a9e5306d57c2d05499b2e5dc427f84ac708bd2c090212cf3e" +dependencies = [ + "gl_generator", +] + [[package]] name = "gpu-allocator" version = "0.28.0" @@ -1812,6 +1844,23 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "khronos-egl" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6aae1df220ece3c0ada96b8153459b67eebe9ae9212258bb0134ae60416fdf76" +dependencies = [ + "libc", + "libloading", + "pkg-config", +] + +[[package]] +name = "khronos_api" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" + [[package]] name = "lazy_static" version = "1.5.1" @@ -3149,6 +3198,15 @@ version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" +[[package]] +name = "slotmap" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdd58c3c93c3d278ca835519292445cb4b0d4dc59ccfdf7ceadaab3f8aeb4038" +dependencies = [ + "version_check", +] + [[package]] name = "smallvec" version = "1.16.2" @@ -4274,6 +4332,7 @@ dependencies = [ "smallvec", "thiserror 2.0.21", "wgpu-core-deps-apple", + "wgpu-core-deps-emscripten", "wgpu-core-deps-windows-linux-android", "wgpu-hal", "wgpu-naga-bridge", @@ -4289,6 +4348,15 @@ dependencies = [ "wgpu-hal", ] +[[package]] +name = "wgpu-core-deps-emscripten" +version = "30.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d98b86cf4abf524a902dd35f18ca6a3f08fc2ae9847c8f10b48e30491b1f0b86" +dependencies = [ + "wgpu-hal", +] + [[package]] name = "wgpu-core-deps-windows-linux-android" version = "30.0.1" @@ -4311,13 +4379,18 @@ dependencies = [ "bytemuck", "cfg-if", "cfg_aliases", + "glow", + "glutin_wgl_sys", "gpu-allocator", "hashbrown 0.17.1", + "js-sys", + "khronos-egl", "libc", "libloading", "log", "naga", "naga-types", + "ndk-sys", "objc2 0.6.5", "objc2-core-foundation", "objc2-core-graphics", @@ -4337,10 +4410,14 @@ dependencies = [ "smallvec", "static_assertions", "thiserror 2.0.21", + "wasm-bindgen", + "wayland-sys", + "web-sys", "wgpu-naga-bridge", "wgpu-types", "windows 0.62.2", "windows-core 0.62.2", + "windows-result 0.4.1", ] [[package]] @@ -5081,6 +5158,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9cc00251562a284751c9973bace760d86c0276c471b4be569fe6b068ee97a56" +[[package]] +name = "xml-rs" +version = "0.8.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7" + [[package]] name = "xtask" version = "0.1.0" From ac47b988c9add9ceb575a23a24751084ec43abc6 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 21:21:05 +0100 Subject: [PATCH 162/238] Shader packs, clean room 42: culled entities, the first-person player, shadowPlayer and shadowLightBlockEntities in the shadow pass MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Entities Horizon's entity culling hides from the camera keep their vertices in a hidden span that only the pack's shadow pass draws; the local player's model is built in first person, hidden from the camera, while the pack casts entity shadows. shadowPlayer draws the player alone when shadowEntities is off, shadowLightBlockEntities the light-emitting block entities when shadowBlockEntities is off. Without a shader pack nothing changes. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, §2.4, §3.2; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/entity_culling.rs | 20 ++++-- crates/horizon-client/src/game.rs | 8 ++- .../horizon-client/src/game/entity_culling.rs | 46 +++++++----- crates/horizon-client/src/game/perspective.rs | 3 +- .../horizon-client/src/game/shader_packs.rs | 28 +++++++- crates/horizon-client/src/gfx.rs | 12 ++++ crates/horizon-client/src/gfx/pack_draw.rs | 71 ++++++++++++++++++- .../horizon-client/src/gfx/pack_entities.rs | 59 +++++++++++++-- crates/horizon-client/src/gfx/pack_post.rs | 42 ++++++++--- crates/horizon-shaders/src/post.rs | 27 ++++++- 10 files changed, 271 insertions(+), 45 deletions(-) diff --git a/crates/horizon-client/src/entity_culling.rs b/crates/horizon-client/src/entity_culling.rs index 611eff1af..16de17465 100644 --- a/crates/horizon-client/src/entity_culling.rs +++ b/crates/horizon-client/src/entity_culling.rs @@ -25,6 +25,20 @@ impl Culler { vertices: &mut Vec, start: usize, opaque: impl FnMut([i32; 3]) -> bool, + ) -> bool { + let hidden = self.hidden(vertices, start, opaque); + if hidden { + vertices.truncate(start); + } + hidden + } + + /// Whether this entity's completed camera-relative mesh (`vertices[start..]`) is hidden. + pub fn hidden( + &self, + vertices: &[crate::mesher::Vertex], + start: usize, + opaque: impl FnMut([i32; 3]) -> bool, ) -> bool { if start >= vertices.len() || self.remaining.get() == 0 { return false; @@ -42,11 +56,7 @@ impl Culler { } } // Cover floating-point and rasterization edges as well as the vertices. - if self.occluded(min.map(|v| v - 0.05), max.map(|v| v + 0.05), opaque) { - vertices.truncate(start); - return true; - } - false + self.occluded(min.map(|v| v - 0.05), max.map(|v| v + 0.05), opaque) } /// Returns true only when a complete opaque block plane covers the bounds. diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 97dff6bbc..49d2a75c0 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1495,6 +1495,9 @@ struct Game<'a> { /// This frame's view frustum, for culling entities and block entities. view_frustum: Option, entity_culler: Option, + /// `Renderer::pack_shadow_casters` for this frame's entities: (keep what the culler hides, + /// build the player's model in first person); (false, false) without a shader pack. + pack_shadow_casters: (bool, bool), /// A scripted flight from the control socket (`fly`). flight: Option, /// The running agent action from the control socket (game/agent.rs). @@ -6792,8 +6795,10 @@ impl Game<'_> { self.ominous_item_spawner_geometry(camera, alpha, &mut out); // Mobs and their projectiles (a span each). self.mob_geometry(camera, alpha, &mut out); - self.mark_entity(out.len(), "minecraft:player"); + // In first person only a shader pack's shadow pass draws the player. + self.mark_player(out.len(), self.camera_type.first_person()); self.player_model_geometry(camera, self.clock.camera_fraction(), &mut out); + self.mark_entity(out.len(), "minecraft:player"); self.guest_model_geometry(camera, alpha, &mut out); self.mark_entity(out.len(), "minecraft:evoker_fangs"); self.fangs_geometry(camera, alpha, &mut out); @@ -11998,6 +12003,7 @@ impl<'a> Game<'a> { agent: agent::Agent::default(), view_frustum: None, entity_culler: None, + pack_shadow_casters: (false, false), pending_uploads: Vec::new(), frame_graph: Default::default(), extras: Default::default(), diff --git a/crates/horizon-client/src/game/entity_culling.rs b/crates/horizon-client/src/game/entity_culling.rs index 3abf92835..66fbaa3e4 100644 --- a/crates/horizon-client/src/game/entity_culling.rs +++ b/crates/horizon-client/src/game/entity_culling.rs @@ -15,6 +15,11 @@ fn opaque(facts: &StateFacts) -> bool { impl Game<'_> { pub(super) fn prepare_entity_culling(&mut self, camera: glam::DVec3) { + // Spans tell the pack's passes which entities are hidden; without them, nothing changes. + self.pack_shadow_casters = match (&self.renderer, &self.pack_ids) { + (Some(r), Some(_)) => r.pack_shadow_casters(), + _ => (false, false), + }; let at = camera.to_array().map(|v| v.floor() as i32); self.entity_culler = (self.settings.horizon.entity_culling // Packs can replace an opaque block's model or texture. @@ -28,24 +33,29 @@ impl Game<'_> { /// Use complete generated geometry so equipment, animation and custom /// models cannot stick out of an assumed collision-sized culling box. pub(super) fn cull_entity_vertices(&self, vertices: &mut Vec, start: usize) { - if let Some(culler) = &self.entity_culler { - culler.cull_vertices(vertices, start, |pos| { - let state = self.world.block(pos[0], pos[1], pos[2]); - if !opaque(self.res.facts().get(state)) { - return false; - } - let Some(y) = pos[1].checked_sub(MIN_Y) else { - return false; - }; - let section = (pos[0] >> 4, y.div_euclid(16), pos[2] >> 4); - // Newly edited or not-yet-uploaded terrain is not an occluder. - self.renderer - .as_ref() - .is_some_and(|r| r.has_section(section)) - && !self.world.dirty_sections.contains(§ion) - && !self.streaming.is_meshing(section) - && !self.pending_uploads.iter().any(|(key, _)| *key == section) - }); + let Some(culler) = &self.entity_culler else { + return; + }; + let hidden = culler.hidden(vertices, start, |pos| { + let state = self.world.block(pos[0], pos[1], pos[2]); + if !opaque(self.res.facts().get(state)) { + return false; + } + let Some(y) = pos[1].checked_sub(MIN_Y) else { + return false; + }; + let section = (pos[0] >> 4, y.div_euclid(16), pos[2] >> 4); + // Newly edited or not-yet-uploaded terrain is not an occluder. + self.renderer + .as_ref() + .is_some_and(|r| r.has_section(section)) + && !self.world.dirty_sections.contains(§ion) + && !self.streaming.is_meshing(section) + && !self.pending_uploads.iter().any(|(key, _)| *key == section) + }); + // A shader pack's shadow pass still draws what the camera cannot see. + if hidden && !(self.pack_shadow_casters.0 && self.hide_spans_from(start, vertices.len())) { + vertices.truncate(start); } } } diff --git a/crates/horizon-client/src/game/perspective.rs b/crates/horizon-client/src/game/perspective.rs index bd2721648..caf49883b 100644 --- a/crates/horizon-client/src/game/perspective.rs +++ b/crates/horizon-client/src/game/perspective.rs @@ -412,7 +412,8 @@ impl Game<'_> { } /// Whether the player's own model shows in the world view. fn player_model_shown(&self) -> bool { - !self.camera_type.first_person() + // A shader pack's shadow pass draws the player in first person too (shadowPlayer). + (!self.camera_type.first_person() || self.pack_shadow_casters.1) && self.sleeping.is_none() && !self.spectator() && self.potions.effects.get("minecraft:invisibility").is_none() diff --git a/crates/horizon-client/src/game/shader_packs.rs b/crates/horizon-client/src/game/shader_packs.rs index f0087c4b0..f534e8391 100644 --- a/crates/horizon-client/src/game/shader_packs.rs +++ b/crates/horizon-client/src/game/shader_packs.rs @@ -232,6 +232,32 @@ impl Game<'_> { .unwrap_or(ids.missing_block) }); self.push_span(start, "BLOCK_ENTITIES", id); + let emission = self + .res + .facts() + .get(horizon_world::registry::BlockStateId(state)) + .emission; + if let Some(last) = self.entity_spans.borrow_mut().last_mut() { + last.emissive = emission > 0; + } + } + + /// Starts the span of the local player's model; `hidden` in first person, where only a + /// pack's shadow pass draws it. + pub(super) fn mark_player(&self, start: usize, hidden: bool) { + self.mark_entity(start, "minecraft:player"); + if self.pack_ids.is_some() + && let Some(last) = self.entity_spans.borrow_mut().last_mut() + { + last.player = true; + last.hidden = hidden; + } + } + + /// Marks vertices `start..end` (one entity) as hidden from the camera, for an entity + /// Horizon's culling would drop but a pack's shadow pass still draws (`EntitySpan::hide`). + pub(super) fn hide_spans_from(&self, start: usize, end: usize) -> bool { + EntitySpan::hide(&mut self.entity_spans.borrow_mut(), start, end) } /// Starts a span of geometry that is not an entity (`PARTICLES`, `DESTROY`...). @@ -247,7 +273,7 @@ impl Game<'_> { start: start as u32, stage: horizon_shaders::render_stage(stage), id, - translucent: false, + ..Default::default() }; // A span that drew nothing gives way to the next. match spans.last_mut() { diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index b21c846a5..523a2801b 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -2560,6 +2560,18 @@ impl Renderer { pub fn section_count(&self) -> usize { self.sections.len() } + /// What a shader pack's shadow pass draws of the entities this frame: (entities or block + /// entities other than the player, so those Horizon's culling hides from the camera are + /// kept for it; the local player, so its model is built in first person too). (false, + /// false) without a pack or its shadow pass. + pub fn pack_shadow_casters(&self) -> (bool, bool) { + let Some((_, Some(post))) = &self.pack_post else { + return (false, false); + }; + let c = post.shadow_casters(); + ((c.entities && !c.only_player) || c.blocks, c.entities) + } + pub fn has_section(&self, key: SectionKey) -> bool { self.sections.contains(key) } diff --git a/crates/horizon-client/src/gfx/pack_draw.rs b/crates/horizon-client/src/gfx/pack_draw.rs index d19c3b4db..a558084f4 100644 --- a/crates/horizon-client/src/gfx/pack_draw.rs +++ b/crates/horizon-client/src/gfx/pack_draw.rs @@ -7,7 +7,7 @@ use horizon_shaders::{RENDER_STAGES, render_stage}; /// Where a run of `Frame::entities` begins, and what it is (slice 7): the run lasts until the /// next span. Recorded only while a shader pack draws entities, so Horizon's own frames carry /// none; Horizon itself still draws the whole list at once. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct EntitySpan { /// The first vertex. pub start: u32, @@ -18,6 +18,44 @@ pub struct EntitySpan { pub id: i32, /// An entity part that blends (a name tag, a shadow): `gbuffers_entities_translucent`. pub translucent: bool, + /// Hidden from the camera (Horizon's entity culling, the player in first person): only the + /// shadow pass draws it. + pub hidden: bool, + /// The local player (`shadowPlayer`). + pub player: bool, + /// A block entity whose block emits light (`shadowLightBlockEntities`). + pub emissive: bool, +} + +impl EntitySpan { + /// Marks vertices `start..end` of `spans`' list (one entity) as hidden: the span covering + /// `start` is split there, the spans inside are hidden, and the geometry after `end` goes + /// on in the last span's kind. False when no span covers `start`. + pub fn hide(spans: &mut Vec, start: usize, end: usize) -> bool { + let Some(i) = spans.iter().rposition(|s| s.start as usize <= start) else { + return false; + }; + let after = spans[spans.len() - 1]; + if spans[i].start as usize != start { + let split = EntitySpan { + start: start as u32, + ..spans[i] + }; + spans.insert(i + 1, split); + } + for s in spans.iter_mut().skip(i) { + if s.start as usize >= start { + s.hidden = true; + } + } + if end > start { + spans.push(EntitySpan { + start: end as u32, + ..after + }); + } + true + } } /// `HzDraw`'s size: `renderStage`, `entityId`, `blockEntityId`, a pad, `entityColor`. @@ -81,3 +119,34 @@ impl DrawUniforms { pass.set_bind_group(2, &self.group, &[self.stride * render_stage(stage)]); } } + +#[cfg(test)] +mod tests { + use super::EntitySpan; + + fn span(start: u32, id: i32) -> EntitySpan { + EntitySpan { + start, + id, + ..Default::default() + } + } + + #[test] + fn hide_splits_the_span_around_one_entity() { + // Two cows in one span: the second (vertices 8..16) is hidden. + let mut spans = vec![span(0, 7)]; + assert!(EntitySpan::hide(&mut spans, 8, 16)); + let hidden = EntitySpan { + hidden: true, + ..span(8, 7) + }; + assert_eq!(spans, [span(0, 7), hidden, span(16, 7)]); + // An entity with its own span and a part after it (armour) is hidden whole. + let mut spans = vec![span(0, 1), span(4, 2), span(6, 3)]; + assert!(EntitySpan::hide(&mut spans, 4, 10)); + assert!(!spans[0].hidden && spans[1].hidden && spans[2].hidden); + assert_eq!(spans[3], span(10, 3)); + assert!(!EntitySpan::hide(&mut Vec::new(), 0, 4)); + } +} diff --git a/crates/horizon-client/src/gfx/pack_entities.rs b/crates/horizon-client/src/gfx/pack_entities.rs index 30fa70880..ab3c66c69 100644 --- a/crates/horizon-client/src/gfx/pack_entities.rs +++ b/crates/horizon-client/src/gfx/pack_entities.rs @@ -170,6 +170,30 @@ pub(super) struct Draw { pub slot: u32, } +/// Which entity runs a pack's shadow pass draws (2.4, 3.2). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct Casters { + /// `shadow_entities` (or `shadow`) draws entities... + pub entities: bool, + /// ...only the local player (`shadowPlayer` without `shadowEntities`). + pub only_player: bool, + /// `shadow_block` (or `shadow`) draws block entities... + pub blocks: bool, + /// ...only those whose block emits light (`shadowLightBlockEntities`). + pub only_lit: bool, +} + +impl Casters { + /// Whether the shadow pass draws `span`'s run, of kind `program`. + pub fn cast(&self, span: &EntitySpan, program: usize) -> bool { + match program { + ENTITY => self.entities && (!self.only_player || span.player), + BLOCK => self.blocks && (!self.only_lit || span.emissive), + _ => false, + } + } +} + /// The vertices and per-draw uniforms of the runs a pack draws, rebuilt each frame. pub(super) struct PackEntities { vertices: wgpu::Buffer, @@ -285,7 +309,7 @@ impl PackEntities { /// Splits `entities` between the pack, which draws the runs of `spans` its programs cover /// (`drawn`: whether it has `gbuffers_entities`, `gbuffers_block` or their fallbacks), and /// Horizon, which draws the rest (`rest`). Uploads the pack's part, and the runs its shadow - /// programs draw (`shadow`: entities, block entities). + /// programs draw (`shadow`). Runs hidden from the camera only ever cast shadows. #[allow(clippy::too_many_arguments)] pub fn prepare( &mut self, @@ -295,7 +319,7 @@ impl PackEntities { entities: &[Vertex], spans: &[EntitySpan], drawn: [bool; PROGRAMS], - shadow: [bool; 2], + shadow: Casters, rest: &mut Vec, ) { self.draws.clear(); @@ -307,9 +331,9 @@ impl PackEntities { for (span, range) in runs(entities.len(), spans) { let run = &entities[range]; let program = program_of(&span).filter(|_| plain(run)); - let pack = program.is_some_and(|p| drawn[p]); - let casts = program.is_some_and(|p| shadow.get(p).copied().unwrap_or(false)); - if !pack { + let pack = program.is_some_and(|p| drawn[p]) && !span.hidden; + let casts = program.is_some_and(|p| shadow.cast(&span, p)); + if !pack && !span.hidden { rest.extend_from_slice(run); } let Some(program) = program.filter(|_| (pack || casts) && !run.is_empty()) else { @@ -446,7 +470,7 @@ mod tests { start, stage: render_stage(stage), id, - translucent: false, + ..Default::default() }; let spans = [ span(2, "ENTITIES", 7), @@ -473,5 +497,28 @@ mod tests { assert_eq!(entity_color(1)[0], 1.0); assert!((entity_color(1)[3] - (1.0 - 178.0 / 255.0)).abs() < 1e-6); assert_eq!(entity_color(0), [0.0; 4]); + // The shadow pass: entities, the player alone, block entities, those that emit light. + let all = Casters { + entities: true, + blocks: true, + ..Default::default() + }; + let player = EntitySpan { + player: true, + ..spans[0] + }; + let lit = EntitySpan { + emissive: true, + ..spans[2] + }; + assert!(all.cast(&spans[0], ENTITY) && all.cast(&spans[2], BLOCK)); + assert!(!all.cast(&spans[1], PARTICLE)); + let some = Casters { + only_player: true, + only_lit: true, + ..all + }; + assert!(!some.cast(&spans[0], ENTITY) && some.cast(&player, ENTITY)); + assert!(!some.cast(&spans[2], BLOCK) && some.cast(&lit, BLOCK)); } } diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index da174b022..19f0d7349 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -149,6 +149,8 @@ struct Entities { late: [bool; PROGRAMS], /// What draws entities and block entities into the shadow map (2.4). shadow: [Option; 2], + /// Only the player, only block entities that emit light (3.2). + only: (bool, bool), } impl Entities { @@ -1086,6 +1088,7 @@ impl PackPost { post.particles, ]; let late = [false, false, post.translucent_late, post.particles_late]; + let shadow_only = (post.shadow_only_player, post.shadow_only_lit_blocks); let shadow_programs = [post.shadow_entities, post.shadow_block]; let entity_vertices = (programs.iter().chain(&shadow_programs)) .any(Option::is_some) @@ -1114,6 +1117,7 @@ impl PackPost { passes: entity_passes, late, shadow: shadow_entities, + only: shadow_only, }); let cloud_geometry = world.clouds.map(|layout| Geometry { buffers: &entity_buffers, @@ -1221,10 +1225,17 @@ impl PackPost { // Timed (and labelled) apart from the terrain's shadow pass, which may run the same // program. let mut entities = entities; - for (part, pass) in (entities.iter_mut()).flat_map(|e| parts.iter().zip(&mut e.shadow)) { - if let Some(p) = pass { - notes.push(format!("{part} cast shadows with {}", p.name)); - p.name.push_str(&format!(" ({part})")); + for e in entities.iter_mut() { + let only = [ + (e.only.0, " (only the player)"), + (e.only.1, " (only those that emit light)"), + ]; + for ((part, pass), (only, which)) in parts.iter().zip(&mut e.shadow).zip(only) { + if let Some(p) = pass { + let which = if only { which } else { "" }; + notes.push(format!("{part}{which} cast shadows with {}", p.name)); + p.name.push_str(&format!(" ({part})")); + } } } let depth1 = water.is_some() @@ -1874,17 +1885,13 @@ impl PackPost { spans: &[super::pack_draw::EntitySpan], rest: &mut Vec, ) { + let shadow = self.shadow_casters(); let Some(e) = &mut self.entities else { rest.clear(); rest.extend_from_slice(entities); return; }; let drawn = e.passes.each_ref().map(Option::is_some); - // Only while the shadow pass draws this frame. - let shadow = e - .shadow - .each_ref() - .map(|p| p.is_some() && self.shadow_targets.is_some()); e.geometry.prepare( device, queue, @@ -1897,6 +1904,23 @@ impl PackPost { ); } + /// Which entity runs the shadow pass draws: none without it. + pub fn shadow_casters(&self) -> super::pack_entities::Casters { + let Some(e) = self + .entities + .as_ref() + .filter(|_| self.shadow_targets.is_some()) + else { + return Default::default(); + }; + super::pack_entities::Casters { + entities: e.shadow[0].is_some(), + only_player: e.only.0, + blocks: e.shadow[1].is_some(), + only_lit: e.only.1, + } + } + /// Whether the pack draws entities or block entities itself. pub fn has_entities(&self) -> bool { self.entities.is_some() diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs index 715dca257..fe5b1c63f 100644 --- a/crates/horizon-shaders/src/post.rs +++ b/crates/horizon-shaders/src/post.rs @@ -252,6 +252,11 @@ pub struct Post { /// `shadow`) from the light, unless `shadowEntities`/`shadowBlockEntities` is false. pub shadow_entities: Option, pub shadow_block: Option, + /// `shadowEntities=false` with `shadowPlayer=true`: of the entities, only the player casts. + pub shadow_only_player: bool, + /// `shadowBlockEntities=false` with `shadowLightBlockEntities=true`: only block entities + /// whose block emits light cast. + pub shadow_only_lit_blocks: bool, /// `shadowcomp`..`shadowcomp99`, after the shadow pass (2.1). pub shadow_comp: Vec, /// Translucent entities draw after deferred (`separateEntityDraws`, 3.2). @@ -323,6 +328,8 @@ pub fn load_post( shadow_water: None, shadow_entities: None, shadow_block: None, + shadow_only_player: false, + shadow_only_lit_blocks: false, shadow_comp: Vec::new(), translucent_late: false, particles_late: false, @@ -545,18 +552,23 @@ fn load_shadow( }) }; // `shadowTerrain`, `shadowTranslucent`, `shadowEntities` and `shadowBlockEntities` (3.2) - // leave their part out of the shadow pass; all are on by default (OQ-19). + // leave their part out of the shadow pass; all are on by default (OQ-19). `shadowPlayer` + // and `shadowLightBlockEntities` (E, off by default) keep the player, and block entities + // that emit light, when the first two are off. let keep = |key: &str| properties.get(key).is_none_or(|v| v.trim() != "false"); + let set = |key: &str| properties.get(key).is_some_and(|v| v.trim() == "true"); + let only_player = !keep("shadowEntities") && set("shadowPlayer"); + let only_lit = !keep("shadowBlockEntities") && set("shadowLightBlockEntities"); let shadow = keep("shadowTerrain") .then(|| load(&["shadow"], terrain)) .flatten(); let water = keep("shadowTranslucent") .then(|| load(&["shadow_water", "shadow"], terrain)) .flatten(); - let shadow_entities = keep("shadowEntities") + let shadow_entities = (keep("shadowEntities") || only_player) .then(|| load(&["shadow_entities", "shadow"], entities)) .flatten(); - let shadow_block = keep("shadowBlockEntities") + let shadow_block = (keep("shadowBlockEntities") || only_lit) .then(|| load(&["shadow_block", "shadow"], entities)) .flatten(); if shadow.is_none() && terrain.is_some() { @@ -569,6 +581,8 @@ fn load_shadow( loader.post.shadow_water = water; loader.post.shadow_entities = shadow_entities; loader.post.shadow_block = shadow_block; + loader.post.shadow_only_player = only_player; + loader.post.shadow_only_lit_blocks = only_lit; } struct Loader<'a> { @@ -892,6 +906,13 @@ mod tests { assert!( off.shadow.is_some() && off.shadow_entities.is_none() && off.shadow_block.is_none() ); + // shadowPlayer and shadowLightBlockEntities keep a part of each. + let some = load( + "shadowEntities=false\nshadowPlayer=true\nshadowBlockEntities=false\nshadowLightBlockEntities=true", + ); + assert!(some.shadow_entities.is_some() && some.shadow_only_player); + assert!(some.shadow_block.is_some() && some.shadow_only_lit_blocks); + assert!(!post.shadow_only_player && !post.shadow_only_lit_blocks); let comp: Vec<_> = post .shadow_comp .iter() From b3e400a52673862fa237ef4341ba21d4440d3c52 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 21:25:37 +0100 Subject: [PATCH 163/238] Renderer: sprite arrays OpenGL can view as arrays wgpu's GL backend picks a texture's target from its layer count when it creates it: one layer makes a 2D texture and a multiple of six on a square texture a cube map, and neither can be bound as terrain.wgsl's texture_2d_array. On GL, the 1-layer stand-in for unused sprite arrays gets two layers and a sprite array of 1 or 6n layers one empty layer more. Other backends are unchanged. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index b55022510..a02e6eb7f 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -516,6 +516,18 @@ fn sprite_split() -> u32 { } /// Sprite arrays terrain.wgsl binds (`sprites`, `sprites_1`...). const SPRITE_ARRAYS: u32 = 8; +/// The layers to give a square texture of `count` layers viewed as a 2D +/// array. wgpu's OpenGL backend picks a texture's target from its layer +/// count when it is created: one layer makes a 2D texture and a multiple of +/// six a cube map, neither of which can be viewed as an array. +fn array_layers(count: u32, gl: bool) -> u32 { + match count { + _ if !gl => count, + 0 | 1 => 2, + n if n % 6 == 0 => n + 1, + n => n, + } +} /// The views of terrain.wgsl's sprite arrays, the unused ones a 1-layer /// stand-in. fn sprite_views( @@ -1312,13 +1324,17 @@ impl Renderer { PreparedTextures::new(sprite_pixels, sprite_size, layers, sprite_mips, forcefield) }); let mut mip_levels = 1; + let gl = adapter.get_info().backend == wgpu::Backend::Gl; let sprite_textures: Vec = prepared .sprites .into_iter() .enumerate() - .map(|(array, (mips, levels))| { + .map(|(array, (mut mips, levels))| { let first = array as u32 * split; - let count = layers.saturating_sub(first).clamp(1, split); + let used = layers.saturating_sub(first).clamp(1, split); + let count = array_layers(used, gl); + // The extra layers are empty. + mips.resize(mips.len() / used as usize * count as usize, 0); mip_levels = levels; device.create_texture_with_data( &queue, @@ -1359,7 +1375,7 @@ impl Renderer { size: wgpu::Extent3d { width: 1, height: 1, - depth_or_array_layers: 1, + depth_or_array_layers: array_layers(1, gl), }, mip_level_count: 1, sample_count: 1, @@ -4560,6 +4576,13 @@ mod tests { } } #[test] + fn gl_sprite_arrays_are_neither_2d_textures_nor_cube_maps() { + for (count, gl) in [(1, 2), (2, 2), (5, 5), (6, 7), (12, 13), (2048, 2048)] { + assert_eq!(super::array_layers(count, true), gl); + assert_eq!(super::array_layers(count, false), count); + } + } + #[test] fn entity_shader_validates_with_its_overlay_constants() { let module = naga::front::wgsl::parse_str(&super::entity_shader()).unwrap(); naga::valid::Validator::new( From 1a16e35aaa45800285fca7d5be2a07e48f0421c0 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 21:25:37 +0100 Subject: [PATCH 164/238] Animated sprites without compute shaders (OpenGL before 4.3) SpriteAnimator wrote every changed sprite with a compute pass, which OpenGL 3.3 to 4.2 has no equivalent for in wgpu: creating the pipeline failed. Without compute shaders and storage, the same jobs now run on the CPU with animate.wgsl's integer mix, and each level is copied into its layer through the upload belt. The GPU test runs both paths against the same CPU reference. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 1 + crates/horizon-client/src/gfx/animate.rs | 162 +++++++++++++++++++++-- 2 files changed, 150 insertions(+), 13 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index a02e6eb7f..baa8c5099 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -1368,6 +1368,7 @@ impl Renderer { sprite_mips, &sprite_textures, split, + animate::SpriteAnimator::compute(&adapter, &device), ); let sprite_unused = device .create_texture(&wgpu::TextureDescriptor { diff --git a/crates/horizon-client/src/gfx/animate.rs b/crates/horizon-client/src/gfx/animate.rs index b7c44429b..c94d8034e 100644 --- a/crates/horizon-client/src/gfx/animate.rs +++ b/crates/horizon-client/src/gfx/animate.rs @@ -10,6 +10,10 @@ //! frames. Blending on the CPU at the array's size and copying every changed //! sprite level by level (a copy command each, a blit each in Mesa) was about //! a fifth of the render thread on an integrated GPU. +//! +//! Without compute shaders (OpenGL before 4.3, the last fallback) the same +//! jobs run on the CPU, the same integer mix as `animate.wgsl`, and each +//! changed level is copied into its layer. use super::upload::Uploads; use crate::mipmap::Settings; use crate::sprites::{FrameBlend, SpriteAnimation}; @@ -38,17 +42,32 @@ struct Sprite { struct Group { /// Where its jobs start in `SpriteAnimator::jobs`, in bytes. offset: u64, - /// One bind group per mip level of the array. + /// One bind group per mip level of the array (none on the CPU). levels: Vec, /// This tick's jobs. jobs: Vec, + /// Its sprite array and its frames, for the CPU. + array: usize, + buffer: usize, +} + +/// Where the jobs run. +enum Writer { + Gpu { + jobs: wgpu::Buffer, + pipeline: wgpu::ComputePipeline, + }, + /// Every frames buffer's words, and the sprite arrays to copy into. + Cpu { + frames: Vec>, + textures: Vec, + }, } pub(super) struct SpriteAnimator { sprites: Vec, groups: Vec, - jobs: wgpu::Buffer, - pipeline: wgpu::ComputePipeline, + writer: Writer, /// Edge of the arrays' layers. size: u32, /// The layers the last `animate` rewrote, counted over all arrays. @@ -57,7 +76,8 @@ pub(super) struct SpriteAnimator { impl SpriteAnimator { /// `textures` are the sprite arrays, `split` layers each, `size` pixels - /// wide; `animations` hold their frames at that size. + /// wide; `animations` hold their frames at that size. `compute`: whether + /// the device runs compute shaders writing storage textures. pub fn new( device: &wgpu::Device, animations: &[SpriteAnimation], @@ -65,11 +85,17 @@ impl SpriteAnimator { settings: &[Settings], textures: &[wgpu::Texture], split: u32, + compute: bool, ) -> Self { let limits = device.limits(); - let limit = limits - .max_storage_buffer_binding_size - .min(limits.max_buffer_size); + let limit = if compute { + limits + .max_storage_buffer_binding_size + .min(limits.max_buffer_size) + } else { + // Word offsets. + u64::from(u32::MAX) * 4 + }; let mut frames: Vec> = Vec::new(); // Each group's sprite array and frames buffer. let mut keys: Vec<(u32, usize)> = Vec::new(); @@ -119,6 +145,29 @@ impl SpriteAnimator { group, }); } + if !compute { + let groups = keys + .iter() + .zip(&counts) + .map(|(&(array, buffer), &count)| Group { + offset: 0, + levels: Vec::new(), + jobs: Vec::with_capacity(count * JOB_BYTES), + array: array as usize, + buffer, + }) + .collect(); + return Self { + sprites, + groups, + writer: Writer::Cpu { + frames, + textures: textures.to_vec(), + }, + size, + changed: Vec::new(), + }; + } let frames: Vec<_> = frames .iter() .map(|data| { @@ -198,19 +247,33 @@ impl SpriteAnimator { offset, levels, jobs: Vec::with_capacity(count * JOB_BYTES), + array: array as usize, + buffer, } }) .collect(); Self { sprites, groups, - jobs, - pipeline, + writer: Writer::Gpu { jobs, pipeline }, size, changed: Vec::new(), } } + /// Whether the jobs can run on the GPU: compute shaders writing a + /// storage texture and reading two storage buffers (not OpenGL before + /// 4.3). + pub fn compute(adapter: &wgpu::Adapter, device: &wgpu::Device) -> bool { + let limits = device.limits(); + adapter + .get_downlevel_capabilities() + .flags + .contains(wgpu::DownlevelFlags::COMPUTE_SHADERS) + && limits.max_storage_textures_per_shader_stage >= 1 + && limits.max_storage_buffers_per_shader_stage >= 2 + } + /// The layers the last `animate` rewrote (for the shader pack atlas). pub fn changed(&self) -> &[u32] { &self.changed @@ -227,14 +290,47 @@ impl SpriteAnimator { if self.collect(tick) == 0 { return; } + let (jobs, pipeline) = match &self.writer { + Writer::Gpu { jobs, pipeline } => (jobs, pipeline), + Writer::Cpu { frames, textures } => { + for group in &mut self.groups { + let (frames, texture) = (&frames[group.buffer], &textures[group.array]); + for job in group.jobs.as_chunks::().0 { + let (words, _) = job.as_chunks::<4>(); + let job: [u32; 5] = std::array::from_fn(|i| u32::from_le_bytes(words[i])); + for level in 0..texture.mip_level_count() { + let width = self.size >> level; + uploads.texture( + encoder, + wgpu::TexelCopyTextureInfo { + texture, + mip_level: level, + origin: wgpu::Origin3d { + x: 0, + y: 0, + z: job[0], + }, + aspect: wgpu::TextureAspect::All, + }, + width, + width, + &mix_level(frames, job, width), + ); + } + } + group.jobs.clear(); + } + return; + } + }; for group in &self.groups { - uploads.buffer(encoder, &self.jobs, group.offset, &group.jobs); + uploads.buffer(encoder, jobs, group.offset, &group.jobs); } let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor { label: Some("Animated sprites"), timestamp_writes: None, }); - pass.set_pipeline(&self.pipeline); + pass.set_pipeline(pipeline); for group in &mut self.groups { let count = (group.jobs.len() / JOB_BYTES) as u32; if count == 0 { @@ -278,6 +374,33 @@ impl SpriteAnimator { } } +/// `animate.wgsl` on the CPU: one `width` wide level of a job +/// (`[layer, current, next, millis, edge]`) from `frames`' chains. +fn mix_level(frames: &[u8], [_, current, next, millis, edge]: [u32; 5], width: u32) -> Vec { + let mut out = Vec::with_capacity((width * width * 4) as usize); + for y in 0..width { + for x in 0..width { + let texel = if width >= edge { + let scale = width / edge; + y / scale * edge + x / scale + } else { + 4 * (edge * edge - width * width) / 3 + y * width + x + }; + let at = |chain: u32| ((chain + texel) * 4) as usize; + let a = &frames[at(current)..at(current) + 4]; + if millis == 0 { + out.extend_from_slice(a); + continue; + } + let b = &frames[at(next)..at(next) + 4]; + out.extend(a.iter().zip(b).map(|(&a, &b)| { + ((u32::from(a) * (1000 - millis) + u32::from(b) * millis + 500) / 1000) as u8 + })); + } + } + out +} + /// The mip chain a frame's layer is made of, and its first level's edge. /// `mipmap::generate` makes a sprite scaled up by whole pixels to the /// array's size from its own size's chain, its first level repeated into the @@ -512,6 +635,16 @@ mod tests { #[test] fn gpu_layers_match_the_cpu_chains_every_tick() { + layers_match_the_cpu_chains_every_tick(true); + } + + /// Without compute shaders (OpenGL before 4.3): the same layers. + #[test] + fn copied_layers_match_the_cpu_chains_every_tick() { + layers_match_the_cpu_chains_every_tick(false); + } + + fn layers_match_the_cpu_chains_every_tick(compute: bool) { let Some((device, queue)) = device() else { return; }; @@ -552,12 +685,15 @@ mod tests { sample_count: 1, dimension: wgpu::TextureDimension::D2, format: wgpu::TextureFormat::Rgba8Unorm, - usage: wgpu::TextureUsages::STORAGE_BINDING | wgpu::TextureUsages::COPY_SRC, + usage: wgpu::TextureUsages::STORAGE_BINDING + | wgpu::TextureUsages::COPY_SRC + | wgpu::TextureUsages::COPY_DST, view_formats: &[], }) }) .collect(); - let mut animator = SpriteAnimator::new(&device, &animations, size, &mips, &textures, split); + let mut animator = + SpriteAnimator::new(&device, &animations, size, &mips, &textures, split, compute); let mut uploads = Uploads::new(&device); let mut writes = Vec::new(); for tick in [0, 0, 1, 2, 3, 4, 7, 7, 12, 30] { From 72de6e0b70aece450db738b421222b4ec9b6bd78 Mon Sep 17 00:00:00 2001 From: vlesmentie <129341611+vlesmentie@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:25:48 -0300 Subject: [PATCH 165/238] Water cauldrons put out a burning player and lose a level On top of the lava cauldron's inside test: water and powder snow cauldrons are no longer skipped. Their entity-inside shape is the cauldron's or their content, Block.column(12, 4, 6 + 3 per level), and LayeredCauldronBlock.entityInside applies: on a server, a burning player first asks for handleEntityOnFireInside (runBefore EXTINGUISH), which lowers the fill level (lowerFillLevel, a powder snow cauldron turning to water of its level first, block_change with no source); then EXTINGUISH (Entity.clearFire) puts the fire out. Fixes #1330. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/cauldron.rs | 34 +++++ crates/horizon-client/src/game.rs | 30 +++++ crates/horizon-client/src/game/lan_actions.rs | 6 + crates/horizon-client/src/motion.rs | 120 +++++++++++++++--- 4 files changed, 173 insertions(+), 17 deletions(-) diff --git a/crates/horizon-client/src/cauldron.rs b/crates/horizon-client/src/cauldron.rs index 9fbe1179f..03fb0676d 100644 --- a/crates/horizon-client/src/cauldron.rs +++ b/crates/horizon-client/src/cauldron.rs @@ -87,6 +87,19 @@ fn filled(player: &mut dyn Player, new: ItemStack, limit_creative: bool) { } } +/// `LayeredCauldronBlock.handleEntityOnFireInside`: a burning entity in a +/// water cauldron lowers it a level; a powder snow cauldron becomes a water +/// cauldron of its level first, then lowers. +pub fn extinguished(cauldron: &BlockState) -> BlockState { + if cauldron.name == "minecraft:powder_snow_cauldron" { + let mut water = cauldron.clone(); + water.name = "minecraft:water_cauldron".into(); + lowered(&water) + } else { + lowered(cauldron) + } +} + /// `LayeredCauldronBlock.lowerFillLevel`. fn lowered(cauldron: &BlockState) -> BlockState { let level: u8 = cauldron @@ -296,3 +309,24 @@ pub fn interact( _ => pass(), } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_burning_entity_lowers_water_and_melts_powder_snow() { + let water = |l| state("minecraft:water_cauldron", Some(l)); + assert_eq!(extinguished(&water(3)), water(2)); + assert_eq!(extinguished(&water(1)), state("minecraft:cauldron", None)); + // Powder snow turns to water of its level, then lowers. + assert_eq!( + extinguished(&state("minecraft:powder_snow_cauldron", Some(3))), + water(2) + ); + assert_eq!( + extinguished(&state("minecraft:powder_snow_cauldron", Some(1))), + state("minecraft:cauldron", None) + ); + } +} diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 699468651..f75064d76 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -4289,6 +4289,7 @@ impl Game<'_> { MotionEvent::Snowflake { position, velocity } => self.snowflake(position, velocity), MotionEvent::EnterBlock { state } => self.trigger_enter_block(state), MotionEvent::InsidePortal { pos } => self.player_inside_nether_portal(pos), + MotionEvent::CauldronExtinguish { pos } => self.cauldron_extinguish(pos), MotionEvent::SmashLanding { state, position, @@ -8354,6 +8355,35 @@ impl Game<'_> { self.level_set(pos, id)?; Ok(true) } + /// `LayeredCauldronBlock.handleEntityOnFireInside` for a burning player + /// in the cauldron at `pos` (`mayInteract`: not a spectator): the fill + /// level drops (`lowerFillLevel`, a `block_change` with no source). A + /// server's cauldron is the server's. + pub(super) fn cauldron_extinguish(&mut self, pos: BlockPos) { + if self.remote.is_some() { + return; + } + let at = [pos.x, pos.y, pos.z]; + let registry = self.pack.assets.registry; + let Some(old) = registry + .state(self.world.block(at[0], at[1], at[2])) + .filter(|s| { + matches!( + s.name.as_str(), + "minecraft:water_cauldron" | "minecraft:powder_snow_cauldron" + ) + }) + .cloned() + else { + return; + }; + let Ok(new) = registry.resolve(&horizon_client::cauldron::extinguished(&old)) else { + return; + }; + if self.level_set(at, new).is_ok() { + self.post_block_game_event("minecraft:block_change", at, None, Some(new)); + } + } fn damage_held(&mut self, amount: u32) { if self.creative { return; diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 2c7019c73..6090fb896 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -2010,6 +2010,12 @@ impl Game<'_> { let _ = w.apply_fire_contact(); let _ = w.server_hazards_tick(in_wall); let mut events = std::mem::take(&mut w.player.events); + // LayeredCauldronBlock.handleEntityOnFireInside along those moves. + for event in &events { + if let MotionEvent::CauldronExtinguish { pos } = event { + game.cauldron_extinguish(*pos); + } + } // ServerGamePacketListenerImpl.handleMovePlayer: // Player.checkMovementStatistics for each move. for d in travelled { diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index 8637ead1f..5835c4629 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -167,6 +167,11 @@ pub enum MotionEvent { InsidePortal { pos: BlockPos, }, + /// `LayeredCauldronBlock.handleEntityOnFireInside`: a burning player in + /// a water or powder snow cauldron; the server lowers its fill level. + CauldronExtinguish { + pos: BlockPos, + }, /// `ServerPlayer.checkFallDamage` after a mace smash: block particles /// from the landing block, more the further the fall. SmashLanding { @@ -1259,22 +1264,31 @@ impl MotionWorld<'_, '_, '_> { continue; } let powder_snow = name == "minecraft:powder_snow"; - let lava_cauldron = name == "minecraft:lava_cauldron"; + // A filled cauldron's content top in blocks: the lava's 15/16 + // (LavaCauldronBlock.FILLED_SHAPE), or a layered cauldron's + // getPixelContentHeight, 6 + 3 per level (FILLED_SHAPES). + let content_top = match name { + "minecraft:lava_cauldron" => Some(15.0 / 16.0), + "minecraft:water_cauldron" | "minecraft:powder_snow_cauldron" => { + let level = self + .assets + .registry + .state(state) + .and_then(|s| s.properties.get("level")) + .and_then(|l| l.parse::().ok()) + .unwrap_or(1); + Some((6.0 + 3.0 * f64::from(level)) / 16.0) + } + _ => None, + }; // getEntityInsideCollisionShape is the whole block but for powder // snow (its collision shape, the whole block when it is empty), // filled cauldrons, end portals and tripwires; Horizon doesn't - // follow the player into the water and powder snow cauldrons, end - // portals or tripwires yet. - if matches!( - name, - "minecraft:water_cauldron" - | "minecraft:powder_snow_cauldron" - | "minecraft:end_portal" - | "minecraft:tripwire" - ) { + // follow the player into those last two. + if matches!(name, "minecraft:end_portal" | "minecraft:tripwire") { continue; } - let inside = !(powder_snow || lava_cauldron) || { + let inside = !(powder_snow || content_top.is_some()) || { let context = self.context(); let binding = self.collision_binding; let shape = binding.collision_shape_with_context( @@ -1287,12 +1301,12 @@ impl MotionWorld<'_, '_, '_> { || { let offset = Vec3::new(f64::from(p.x), f64::from(p.y), f64::from(p.z)); let mut boxes: Vec<_> = shape.moved(offset).boxes().to_vec(); - if lava_cauldron { - // LavaCauldronBlock.FILLED_SHAPE: the cauldron's - // shape or its lava, Block.column(12, 4, 15). + if let Some(top) = content_top { + // The cauldron's shape or its content, + // Block.column(12, 4, top). boxes.push(Aabb::new( Vec3::new(2.0 / 16.0, 4.0 / 16.0, 2.0 / 16.0).add_vector(offset), - Vec3::new(14.0 / 16.0, 15.0 / 16.0, 14.0 / 16.0).add_vector(offset), + Vec3::new(14.0 / 16.0, top, 14.0 / 16.0).add_vector(offset), )); } self.player @@ -1302,11 +1316,14 @@ impl MotionWorld<'_, '_, '_> { } }; if inside && visited.insert(p.pack()) { - // CactusBlock, CampfireBlock and LavaCauldronBlock.entityInside - // (`entity.hurt`). + // CactusBlock, CampfireBlock and the cauldrons' entityInside + // (`entity.hurt`, the fire and the fill level). match name { "minecraft:cactus" => self.environment_hurt("minecraft:cactus", 1.0)?, "minecraft:lava_cauldron" => self.lava_cauldron_inside()?, + "minecraft:water_cauldron" | "minecraft:powder_snow_cauldron" => { + self.layered_cauldron_inside(p); + } "minecraft:campfire" | "minecraft:soul_campfire" => { let lit = self .assets @@ -1372,6 +1389,18 @@ impl MotionWorld<'_, '_, '_> { self.player.ignite_for_ticks(300)?; self.environment_hurt("minecraft:lava", 4.0) } + /// `LayeredCauldronBlock.entityInside`: on a server, a burning player + /// asks for the cauldron's `handleEntityOnFireInside` (runBefore + /// EXTINGUISH, so while still burning), then EXTINGUISH + /// (`Entity.clearFire`) puts the fire out. + fn layered_cauldron_inside(&mut self, pos: BlockPos) { + if self.player.fire_ticks > 0 && !self.player.spectator { + self.player + .events + .push(MotionEvent::CauldronExtinguish { pos }); + } + self.player.fire_ticks = self.player.fire_ticks.min(0); + } /// `WebBlock.entityInside`: stuck at a quarter of the speed, half with /// the Weaving effect. fn web_inside(&mut self) { @@ -2434,6 +2463,63 @@ mod tests { assert!(away.fire_ticks <= 0); } + #[test] + fn a_water_cauldron_puts_out_a_burning_player() { + let root = std::env::var_os("HORIZON_TEST_RESOURCES") + .map(std::path::PathBuf::from) + .unwrap_or_else(|| { + 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, 1).unwrap(); + let water = assets + .registry + .resolve(&horizon_world::chunk::BlockState { + name: "minecraft:water_cauldron".into(), + properties: [("level".to_owned(), "3".to_owned())].into(), + }) + .unwrap(); + scene.set(20, 4, 20, water).unwrap(); + let mut tick = |player: &mut PlayerMotion| { + MotionWorld { + entities: &[], + player, + scene: &mut scene, + assets: &assets, + collision_binding: &binding, + resources: &resources, + } + .controlled_simulation_tick(Default::default()) + .unwrap(); + }; + let asks = |player: &PlayerMotion| { + player + .events + .iter() + .filter(|e| matches!(e, MotionEvent::CauldronExtinguish { .. })) + .count() + }; + // Burning, on the cauldron's floor in its water: put out, and the + // server is asked to lower the water. + let mut burning = PlayerMotion::new(&resources, Vec3::new(20.5, 4.25, 20.5)).unwrap(); + burning.fire_ticks = 100; + tick(&mut burning); + assert!(burning.fire_ticks <= 0, "the water must put the fire out"); + assert_eq!(asks(&burning), 1); + // Not burning: nothing to lower. + let mut dry = PlayerMotion::new(&resources, Vec3::new(20.5, 4.25, 20.5)).unwrap(); + tick(&mut dry); + assert_eq!(asks(&dry), 0); + } + #[test] fn server_travel_moves_only_the_velocity() { let root = Path::new(env!("CARGO_MANIFEST_DIR")) From 386912a76d4046a6026e6caa331b5ee5576e217b Mon Sep 17 00:00:00 2001 From: sl4ppleware <66299946+sl4ppleware@users.noreply.github.com> Date: Sun, 25 Oct 2026 23:32:27 +0300 Subject: [PATCH 166/238] Hearts blink again when the server sets health: LocalPlayer.hurtTo's damageCooldownTime --- crates/horizon-client/src/health.rs | 10 ++++++++-- crates/horizon-client/src/motion.rs | 5 +++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/health.rs b/crates/horizon-client/src/health.rs index da40f41fc..25c719bfb 100644 --- a/crates/horizon-client/src/health.rs +++ b/crates/horizon-client/src/health.rs @@ -261,11 +261,17 @@ impl PlayerHealth { self.hurt_animation_time = 10; } /// `LocalPlayer.hurtTo` with `flashOnSetHealth`: a drop below the current - /// health starts the hurt animation. + /// health starts the hurt animation; `damageCooldownTime` (20 on a drop, + /// 10 on a gain) is what lets `Hud.extractPlayerHealth` blink the hearts. pub fn flash_health_change(&mut self, health: f32) { let damage = self.health() - health; - if damage > 0.0 { + if damage <= 0.0 { + if damage < 0.0 { + self.hurt.cooldown = 10; + } + } else { self.hurt.last_hurt = damage; + self.hurt.cooldown = 20; self.animate_hurt(); } } diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index e7809055d..a5e3813b1 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -2934,8 +2934,13 @@ mod tests { if client_side { w.player.health.flash_health_change(20.0); assert_eq!(w.player.health.hurt_animation_time, 0); + assert_eq!(w.player.health.hurt.cooldown, 0); w.player.health.flash_health_change(12.0); assert_eq!(w.player.health.hurt_animation_time, 10); + // LocalPlayer.hurtTo's damageCooldownTime: the HUD's hearts blink. + assert_eq!(w.player.health.hurt.cooldown, 20); + w.player.health.flash_health_change(25.0); + assert_eq!(w.player.health.hurt.cooldown, 10); } } } From 21203de7f07aec02ac1bb325462c729d9ae8fc38 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 21:33:05 +0100 Subject: [PATCH 167/238] Animated sprites: the CPU path also when the driver cannot build the compute pipeline An OpenGL driver can list compute shaders among its extensions while wgpu writes GLSL of its core version, which has none (seen under MESA_GL_VERSION_OVERRIDE=3.3: the pipeline failed and the game panicked). The pipeline is now created inside error scopes, and a failure falls back to the CPU path with one warning. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx/animate.rs | 50 ++++++++++++++++-------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/crates/horizon-client/src/gfx/animate.rs b/crates/horizon-client/src/gfx/animate.rs index c94d8034e..5a70f3b0f 100644 --- a/crates/horizon-client/src/gfx/animate.rs +++ b/crates/horizon-client/src/gfx/animate.rs @@ -77,7 +77,7 @@ pub(super) struct SpriteAnimator { impl SpriteAnimator { /// `textures` are the sprite arrays, `split` layers each, `size` pixels /// wide; `animations` hold their frames at that size. `compute`: whether - /// the device runs compute shaders writing storage textures. + /// the device says it runs compute shaders writing storage textures. pub fn new( device: &wgpu::Device, animations: &[SpriteAnimation], @@ -87,8 +87,9 @@ impl SpriteAnimator { split: u32, compute: bool, ) -> Self { + let pipeline = if compute { pipeline(device) } else { None }; let limits = device.limits(); - let limit = if compute { + let limit = if pipeline.is_some() { limits .max_storage_buffer_binding_size .min(limits.max_buffer_size) @@ -145,7 +146,7 @@ impl SpriteAnimator { group, }); } - if !compute { + let Some(pipeline) = pipeline else { let groups = keys .iter() .zip(&counts) @@ -167,7 +168,7 @@ impl SpriteAnimator { size, changed: Vec::new(), }; - } + }; let frames: Vec<_> = frames .iter() .map(|data| { @@ -191,18 +192,6 @@ impl SpriteAnimator { usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); - let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { - label: Some("Animated sprites"), - source: wgpu::ShaderSource::Wgsl(include_str!("animate.wgsl").into()), - }); - let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { - label: Some("Animated sprites"), - layout: None, - module: &module, - entry_point: Some("animate"), - compilation_options: Default::default(), - cache: None, - }); let layout = pipeline.get_bind_group_layout(0); let groups = keys .iter() @@ -374,6 +363,35 @@ impl SpriteAnimator { } } +/// `animate.wgsl`'s pipeline, unless the device fails to make it: an OpenGL +/// driver can list compute shaders among its extensions while wgpu writes +/// the GLSL version of its core version, which has none (Mesa's +/// `MESA_GL_VERSION_OVERRIDE`, older drivers). +fn pipeline(device: &wgpu::Device) -> Option { + let validation = device.push_error_scope(wgpu::ErrorFilter::Validation); + let internal = device.push_error_scope(wgpu::ErrorFilter::Internal); + let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("Animated sprites"), + source: wgpu::ShaderSource::Wgsl(include_str!("animate.wgsl").into()), + }); + let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { + label: Some("Animated sprites"), + layout: None, + module: &module, + entry_point: Some("animate"), + compilation_options: Default::default(), + cache: None, + }); + let internal = pollster::block_on(internal.pop()); + match internal.or(pollster::block_on(validation.pop())) { + None => Some(pipeline), + Some(e) => { + log::warn!("Horizon renderer: animated sprites drawn on the CPU ({e})"); + None + } + } +} + /// `animate.wgsl` on the CPU: one `width` wide level of a job /// (`[layer, current, next, millis, edge]`) from `frames`' chains. fn mix_level(frames: &[u8], [_, current, next, millis, edge]: [u32; 5], width: u32) -> Vec { From 61c44c22efe23f9cbc4ac00ef8c8f20a35b00aa4 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 21:35:12 +0100 Subject: [PATCH 168/238] Mod API: capability render (experimental): HUD shapes and camera shake Mods draw through the game's own renderer, never their own shaders: - WIT `render`: set-hud(shapes) with fill, quad (any four corners), sprite (the game's GUI sprites), item (scaled) and text shapes, up to 1024, kept until replaced; gui-size(); shake(strength, seconds). - Drawn over the HUD after the extras, under chat and screens, hidden with F1 (game/mod_render.rs). Colours are 0xAARRGGBB. - shake turns the view, not the player: jolts fade out squared, add up to 1, at most 3 degrees of yaw and 2 of pitch, scaled by Distortion Effects (screenEffectScale; 0 turns it off). - Experimental: the manifest needs experimental = ["render"]. - No mod drawing, no cost: an empty iterator and an is_empty check. - cargo horizon-mod check reports shakes and HUD shapes. The ui test guest gains /render_hud, /render_clear and /render_shake, with a sandbox test; HudBatch gets fill_quad. - Docs: mods.md, stability.md, the changelog, the regenerated reference. Co-Authored-By: Claude Opus 5.5 --- crates/cargo-horizon-mod/src/check.rs | 9 + crates/horizon-client/src/game.rs | 3 + crates/horizon-client/src/game/mod_render.rs | 176 ++++++++++++++++++ crates/horizon-client/src/game/mods.rs | 15 +- crates/horizon-client/src/hud.rs | 6 + crates/horizon-mod-sdk/src/lib.rs | 2 +- crates/horizon-mod-sdk/wit/horizon.wit | 82 ++++++++ crates/horizon-mods/src/manifest.rs | 11 +- crates/horizon-mods/src/runtime.rs | 81 ++++++++ crates/horizon-mods/tests/sandbox.rs | 38 +++- crates/horizon-mods/tests/ui_guest/src/lib.rs | 71 ++++++- docs/api/mods-reference.md | 94 +++++++++- docs/api/mods.md | 2 + docs/api/stability.md | 1 + docs/mods.md | 5 +- 15 files changed, 582 insertions(+), 14 deletions(-) create mode 100644 crates/horizon-client/src/game/mod_render.rs diff --git a/crates/cargo-horizon-mod/src/check.rs b/crates/cargo-horizon-mod/src/check.rs index 5d4150ab9..dfb9820a2 100644 --- a/crates/cargo-horizon-mod/src/check.rs +++ b/crates/cargo-horizon-mod/src/check.rs @@ -198,6 +198,8 @@ fn play(path: &Path, report: &mut Report) -> Result<(), String> { game_time: t, day_time: 1000 + t, }), + // The GUI of a 1280 x 720 window at scale 3. + gui_size: (427.0, 240.0), }); mods.tick(t); for _ in 0..FRAMES_PER_TICK { @@ -217,8 +219,15 @@ fn play(path: &Path, report: &mut Report) -> Result<(), String> { Output::Stopped { reason, .. } => return Err(format!("it stopped: {reason}")), Output::OpenScreen { .. } => "opened a screen".to_string(), Output::CloseScreen { .. } => "closed its screen".to_string(), + Output::Shake { strength, seconds } => { + format!("shook the camera: {strength:.2} for {seconds:.1} s") + } }); } + let shapes = mods.hud_shapes().count(); + if shapes > 0 { + report.output.push(format!("drew {shapes} HUD shape(s)")); + } match failed(&mods) { Some(reason) => Err(format!("it stopped: {reason}")), None => Ok(()), diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index eabbc22b8..31bfe74b6 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -96,6 +96,7 @@ mod maps; mod minecarts; mod mob_blocks; mod mob_gear; +mod mod_render; mod mods; mod mount_menu; mod mounts; @@ -4894,6 +4895,7 @@ impl Game<'_> { // Hud: the action bar and titles draw over the hotbar (nextStratum). self.draw_boss_bars(&mut batch); self.draw_extras(&mut batch); + self.draw_mod_shapes(&mut batch); } if show_hud { // Hud.nextContextualInfoState: the jump bar, the experience bar or @@ -9865,6 +9867,7 @@ impl Game<'_> { self.pitch, ), }; + let (view_yaw, view_pitch) = self.shaken_view(view_yaw, view_pitch); self.audio .set_listener([camera.x, camera.y, camera.z], view_yaw, view_pitch); let block = [ diff --git a/crates/horizon-client/src/game/mod_render.rs b/crates/horizon-client/src/game/mod_render.rs new file mode 100644 index 000000000..1c3a20621 --- /dev/null +++ b/crates/horizon-client/src/game/mod_render.rs @@ -0,0 +1,176 @@ +//! Capability `render` (docs/mods.md): the shapes mods keep on the HUD, +//! drawn by the game's own HUD renderer, and their camera shakes. Mods +//! never supply shader code. With no mod drawing, each hook here returns +//! after one check. +use super::Game; +use horizon_client::hud::HudBatch; +#[cfg(feature = "mods")] +use { + horizon_client::{hud::rgba, items::ItemStack, text::Text}, + horizon_mods::Shape, + std::time::Instant, +}; + +/// The camera's jolts from `render.shake`, while any is fading. +#[cfg(feature = "mods")] +#[derive(Default)] +pub(super) struct Shake { + jolts: Vec, +} + +#[cfg(feature = "mods")] +struct Jolt { + start: Instant, + strength: f32, + seconds: f32, +} + +/// The HUD batch's colour (ABGR) from a mod's 0xAARRGGBB. +#[cfg(feature = "mods")] +fn argb(color: u32) -> u32 { + let [a, r, g, b] = color.to_be_bytes(); + rgba(r, g, b, a) +} + +/// The view's turn at full strength, in degrees: yaw, then pitch. +#[cfg(feature = "mods")] +const SHAKE_DEGREES: (f32, f32) = (3.0, 2.0); + +#[cfg(feature = "mods")] +impl Shake { + pub(super) fn add(&mut self, strength: f32, seconds: f32) { + self.jolts.push(Jolt { + start: Instant::now(), + strength, + seconds, + }); + } + /// How far the view turns now (yaw, pitch degrees), `scale` being the + /// player's Distortion Effects; fades jolts out and forgets ended ones. + fn offset(&mut self, now: Instant, scale: f32) -> (f32, f32) { + self.jolts + .retain(|j| now.duration_since(j.start).as_secs_f32() < j.seconds); + let mut strength = 0.0; + for j in &self.jolts { + let left = 1.0 - now.duration_since(j.start).as_secs_f32() / j.seconds; + strength += j.strength * left * left; + } + let strength = strength.min(1.0) * scale.clamp(0.0, 1.0); + if strength <= 0.0 { + return (0.0, 0.0); + } + // Smooth noise: sines at unrelated rates, so it never repeats visibly. + let t = self + .jolts + .first() + .map_or(0.0, |j| now.duration_since(j.start).as_secs_f32()); + let wave = |a: f32, b: f32, c: f32| { + ((t * a).sin() * 0.5 + (t * b).sin() * 0.3 + (t * c).sin() * 0.2).clamp(-1.0, 1.0) + }; + ( + strength * SHAKE_DEGREES.0 * wave(23.0, 37.3, 51.7), + strength * SHAKE_DEGREES.1 * wave(29.1, 43.9, 61.3), + ) + } +} + +#[cfg(not(feature = "mods"))] +impl Game<'_> { + pub(super) fn draw_mod_shapes(&self, _batch: &mut HudBatch) {} + pub(super) fn shaken_view(&mut self, yaw: f32, pitch: f32) -> (f32, f32) { + (yaw, pitch) + } +} + +#[cfg(feature = "mods")] +impl Game<'_> { + /// The running mods' HUD shapes, in order, over the HUD. + pub(super) fn draw_mod_shapes(&self, batch: &mut HudBatch) { + let atlas = &self.gui.atlas; + for shape in self.mods.mods.hud_shapes() { + match shape { + Shape::Fill(f) => { + let r = f.rect; + batch.fill(atlas, r.x, r.y, r.w, r.h, argb(f.color)); + } + Shape::Quad(q) => { + let (a, b, c, d) = q.corners; + let corners = [[a.x, a.y], [b.x, b.y], [c.x, c.y], [d.x, d.y]]; + batch.fill_quad(atlas, corners, argb(q.color)); + } + Shape::Sprite(s) => { + let r = s.rect; + let name = s.sprite.strip_prefix("minecraft:").unwrap_or(&s.sprite); + let name = format!("gui/sprites/{name}"); + batch.sprite_scaled(atlas, &name, r.x, r.y, r.w, r.h, argb(s.color)); + } + Shape::Item(i) => { + if self.res.items().get(&i.item).is_none() { + continue; + } + let start = batch.vertices.len(); + let stack = ItemStack::new(i.item.as_str(), 1); + self.draw_item_model(batch, &stack, i.at.x, i.at.y); + batch.scale_since(start, i.at.x, i.at.y, i.scale, i.scale); + } + Shape::Text(t) => { + let text = Text::literal(t.text.clone()); + let x = if t.centered { + t.at.x - (HudBatch::text_width(atlas, &t.text) / 2.0).floor() + } else { + t.at.x + }; + batch.text_styled( + atlas, + &text, + x, + t.at.y, + argb(t.color | 0xFF00_0000), + t.shadow, + ); + } + } + } + } + /// The view's yaw and pitch after the mods' camera shakes; unchanged + /// while none is fading. + pub(super) fn shaken_view(&mut self, yaw: f32, pitch: f32) -> (f32, f32) { + if self.mods.shake.jolts.is_empty() { + return (yaw, pitch); + } + let scale = self.settings.file.double("screenEffectScale") as f32; + let (dy, dp) = self.mods.shake.offset(Instant::now(), scale); + (yaw + dy, (pitch + dp).clamp(-90.0, 90.0)) + } +} + +#[cfg(all(test, feature = "mods"))] +mod tests { + use super::*; + use std::time::Duration; + + #[test] + fn a_jolt_fades_out_and_distortion_scales_it() { + let mut shake = Shake::default(); + shake.add(1.0, 2.0); + let start = shake.jolts[0].start; + let at = |s: f32| start + Duration::from_secs_f32(s); + let size = |(y, p): (f32, f32)| y.abs() + p.abs(); + // Off with Distortion Effects at 0, inside the limits otherwise. + assert_eq!(shake.offset(at(0.3), 0.0), (0.0, 0.0)); + let (y, p) = shake.offset(at(0.3), 1.0); + assert!(y.abs() <= SHAKE_DEGREES.0 && p.abs() <= SHAKE_DEGREES.1); + // Fading: the largest turn over a window shrinks. + let mut peak = |from: f32| { + (0..50) + .map(|i| size(shake.offset(at(from + i as f32 * 0.002), 1.0))) + .fold(0.0, f32::max) + }; + let early = peak(0.1); + let late = peak(1.6); + assert!(late < early, "{late} !< {early}"); + // Gone once it ends. + assert_eq!(shake.offset(at(2.1), 1.0), (0.0, 0.0)); + assert!(shake.jolts.is_empty()); + } +} diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs index 47a3ce07b..2a07cb940 100644 --- a/crates/horizon-client/src/game/mods.rs +++ b/crates/horizon-client/src/game/mods.rs @@ -33,7 +33,10 @@ use { #[derive(Default)] pub(super) struct ModsState { #[cfg(feature = "mods")] - mods: Mods, + pub(super) mods: Mods, + /// Capability `render`'s camera shakes. + #[cfg(feature = "mods")] + pub(super) shake: super::mod_render::Shake, /// `--dev-mod PATH`: always on, reloaded when rebuilt. pub(super) dev: Vec, /// When dev mods were last checked for a rebuild. @@ -308,7 +311,11 @@ impl Game<'_> { game_time: self.world.game_time.max(0) as u64, day_time: self.world.day_time.max(0) as u64, }); - Snapshot { player, clock } + Snapshot { + player, + clock, + gui_size: self.gui_size(), + } } /// A game tick. pub(super) fn tick_mods(&mut self) { @@ -382,6 +389,10 @@ impl Game<'_> { self.open_mod_screen(owner, false); continue; } + Output::Shake { strength, seconds } => { + self.mods.shake.add(strength, seconds); + continue; + } Output::CloseScreen { owner } => { if self.mods.screen.as_ref().is_some_and(|s| s.owner == owner) { self.close_mod_screen(false); diff --git a/crates/horizon-client/src/hud.rs b/crates/horizon-client/src/hud.rs index 89d1b1c3c..c3a165792 100644 --- a/crates/horizon-client/src/hud.rs +++ b/crates/horizon-client/src/hud.rs @@ -835,6 +835,12 @@ impl HudBatch { r.v1 = cv; self.rect_uv(x, y, w, h, r, color); } + /// Any four-cornered shape in one colour, corners in order round it. + pub fn fill_quad(&mut self, atlas: &HudAtlas, corners: [[f32; 2]; 4], color: u32) { + let w = atlas.white; + let uv = [(w.u0 + w.u1) / 2.0, (w.v0 + w.v1) / 2.0]; + self.quad(corners, [uv; 4], color); + } /// GuiGraphics.fillGradient: `top` to `bottom`, down the rectangle. pub fn fill_gradient( &mut self, diff --git a/crates/horizon-mod-sdk/src/lib.rs b/crates/horizon-mod-sdk/src/lib.rs index aea52339f..8708fb5be 100644 --- a/crates/horizon-mod-sdk/src/lib.rs +++ b/crates/horizon-mod-sdk/src/lib.rs @@ -38,7 +38,7 @@ wit_bindgen::generate!({ pub use exports::horizon::mods::events::{Guest as __Guest, Registration}; pub use exports::horizon::mods::ui_events::Guest as __UiEventsGuest; -pub use horizon::mods::{chat, game, hud, log, storage, ui}; +pub use horizon::mods::{chat, game, hud, log, render, storage, ui}; /// The game's calls into a mod. Only the hooks [`Registration`] asks for /// are called; the others may stay as their defaults. diff --git a/crates/horizon-mod-sdk/wit/horizon.wit b/crates/horizon-mod-sdk/wit/horizon.wit index 1e49919c3..64bbac592 100644 --- a/crates/horizon-mod-sdk/wit/horizon.wit +++ b/crates/horizon-mod-sdk/wit/horizon.wit @@ -163,6 +163,82 @@ interface ui-events { settings-opened: func(); } +/// Capability `render` (experimental: the manifest needs +/// `experimental = ["render"]`): shapes the game draws for the mod with its +/// own renderer, kept until the mod replaces them (no call per frame). A mod +/// never supplies shader code. +interface render { + /// A rectangle in GUI pixels: the top left corner and the size. + record rect { + x: f32, + y: f32, + w: f32, + h: f32, + } + /// A point in GUI pixels from the screen's top left. + record point { + x: f32, + y: f32, + } + record fill-shape { + rect: rect, + /// 0xAARRGGBB. + color: u32, + } + /// Any four-cornered shape (a wheel's segment, an arrow): the corners + /// in order round it. + record quad-shape { + corners: tuple, + /// 0xAARRGGBB. + color: u32, + } + record sprite-shape { + /// One of the game's GUI sprites, as `gui/sprites/` names them + /// (`hud/heart/full`, `widget/button`), the resource packs' versions + /// included. An unknown one draws nothing. + sprite: string, + rect: rect, + /// A tint, 0xAARRGGBB: 0xFFFFFFFF draws it as it is. + color: u32, + } + record item-shape { + /// `minecraft:diamond`, `:`; an unknown one draws nothing. + item: string, + /// The icon's top left. + at: point, + /// 1.0 is the hotbar's 16 × 16. + scale: f32, + } + record text-shape { + /// Up to 256 characters, one line. + text: string, + at: point, + /// 0xRRGGBB, as `hud`'s lines. + color: u32, + /// Centred on `at.x` instead of starting there. + centered: bool, + shadow: bool, + } + variant shape { + fill(fill-shape), + quad(quad-shape), + sprite(sprite-shape), + item(item-shape), + text(text-shape), + } + /// Replaces the mod's HUD shapes: up to 1024, drawn every frame in + /// order over the HUD (after the hotbar, under chat and screens), hidden + /// with the HUD (F1). An empty list removes them. + set-hud: func(shapes: list); + /// The screen's size in GUI pixels (width, height), as of this tick. + gui-size: func() -> tuple; + /// Shakes the camera: a jolt of `strength` (0 to 1, 1 the hardest) that + /// fades out over `seconds` (at most 10). Jolts add up to 1. The view + /// turns, the player doesn't, and the player's Distortion Effects option + /// scales it (0 turns it off). + shake: func(strength: f32, seconds: f32); +} + /// What the game calls. Only the hooks `init` asks for are ever called. interface events { record registration { @@ -203,6 +279,7 @@ world horizon-mod { world horizon-ui-mod { include horizon-mod; import ui; + import render; export ui-events; } @@ -211,3 +288,8 @@ world ui-host { import ui; export ui-events; } + +/// The game's side of capability `render` (imports only). +world render-host { + import render; +} diff --git a/crates/horizon-mods/src/manifest.rs b/crates/horizon-mods/src/manifest.rs index 6070f99bd..1880b9f3e 100644 --- a/crates/horizon-mods/src/manifest.rs +++ b/crates/horizon-mods/src/manifest.rs @@ -84,15 +84,19 @@ pub enum Capability { /// A screen the mod owns and a settings page in Options > Mods, drawn /// with the menus' own widgets (interface `ui`, calls `ui-events`). Ui, + /// Shapes on the HUD and the camera's shake, drawn by the game's own + /// renderer (interface `render`). + Render, } impl Capability { - pub const ALL: [Capability; 6] = [ + pub const ALL: [Capability; 7] = [ Capability::GameRead, Capability::Chat, Capability::Hud, Capability::Storage, Capability::Registries, Capability::Ui, + Capability::Render, ]; /// The WIT interface it grants, in `horizon:mods`. pub fn interface(self) -> &'static str { @@ -103,12 +107,13 @@ impl Capability { Capability::Storage => "storage", Capability::Registries => "registries", Capability::Ui => "ui", + Capability::Render => "render", } } /// May change in any release: a mod uses it only with /// `experimental = [""]` in its manifest. pub fn is_experimental(self) -> bool { - matches!(self, Capability::Ui) + matches!(self, Capability::Ui | Capability::Render) } /// What the player reads before turning the mod on: "May ". pub fn describe(self) -> &'static str { @@ -119,6 +124,7 @@ impl Capability { Capability::Storage => "save its own settings to disk", Capability::Registries => "add blocks and items to your worlds", Capability::Ui => "show its own screens and a settings page", + Capability::Render => "draw on your screen and shake the camera", } } /// The capability a component import needs, `Ok(None)` for one every @@ -150,6 +156,7 @@ impl fmt::Display for Capability { Capability::Storage => "storage", Capability::Registries => "registries", Capability::Ui => "ui", + Capability::Render => "render", }; f.write_str(name) } diff --git a/crates/horizon-mods/src/runtime.rs b/crates/horizon-mods/src/runtime.rs index 1b1795037..e19bfbe3f 100644 --- a/crates/horizon-mods/src/runtime.rs +++ b/crates/horizon-mods/src/runtime.rs @@ -40,6 +40,19 @@ pub use ui_bindings::horizon::mods::ui::{ ItemWidget, ListWidget, Screen as UiScreen, TextWidget, Widget as UiWidget, }; use ui_bindings::{UiHost, horizon::mods::ui}; +/// Capability `render`'s imports (docs/mods.md): the game draws the shapes. +mod render_bindings { + wasmtime::component::bindgen!({ + path: "../horizon-mod-sdk/wit", + world: "render-host", + }); +} +/// What capability `render` asks the game to draw (WIT `interface render`). +pub use render_bindings::horizon::mods::render::{ + FillShape, ItemShape, Point as RenderPoint, QuadShape, Rect as RenderRect, Shape, SpriteShape, + TextShape, +}; +use render_bindings::{RenderHost, horizon::mods::render}; /// A mod's linear memory cap. const MEMORY: usize = 64 << 20; @@ -70,6 +83,8 @@ const STORAGE_WRITE_BUDGET: u64 = 1 << 20; pub struct Snapshot { pub player: Option, pub clock: Option, + /// The screen in GUI pixels (capability `render`'s `gui-size`). + pub gui_size: (f32, f32), } /// What mods asked the game to show since the last [`Mods::drain`]. @@ -86,6 +101,8 @@ pub enum Output { OpenScreen { owner: usize }, /// `ui.close-screen`: the mod closed its own screen (no `closed` back). CloseScreen { owner: usize }, + /// `render.shake`: strength 0 to 1, fading out over `seconds` (≤ 10). + Shake { strength: f32, seconds: f32 }, } /// A mod's text panel, drawn every frame until it changes. @@ -153,6 +170,8 @@ struct State { out: Vec, panel: Option, ui: UiState, + /// Capability `render`'s HUD shapes, drawn until replaced. + shapes: Vec, /// Capability `storage`'s folder; made on first `set`. data_dir: PathBuf, /// Bytes `storage::set` may still write before this hook call ends @@ -231,6 +250,8 @@ impl Mods { .map_err(|e| e.to_string())?; UiHost::add_to_linker::>(&mut linker, |s| s) .map_err(|e| e.to_string())?; + RenderHost::add_to_linker::>(&mut linker, |s| s) + .map_err(|e| e.to_string())?; self.engine = Some((engine, linker)); } Ok(self.engine.as_ref().expect("made above")) @@ -290,6 +311,7 @@ impl Mods { out: Vec::new(), panel: None, ui: UiState::default(), + shapes: Vec::new(), data_dir, storage_budget: STORAGE_WRITE_BUDGET, limits: StoreLimitsBuilder::new() @@ -481,6 +503,14 @@ impl Mods { .iter() .filter_map(|e| e.run.as_ref()?.store.data().panel.as_ref()) } + /// The running mods' HUD shapes (capability `render`), each mod's in + /// its own order, mods in load order. + pub fn hud_shapes(&self) -> impl Iterator { + self.entries + .iter() + .filter_map(|e| e.run.as_ref()) + .flat_map(|r| &r.store.data().shapes) + } /// The screen mod `i` last pushed with `ui.open-screen`, while it is up. pub fn screen(&self, i: usize) -> Option<&UiScreen> { self.entries @@ -668,6 +698,57 @@ impl chat::Host for State { } } } +/// HUD shapes one mod may keep (capability `render`). +const SHAPES: usize = 1024; +/// The longest jolt `render.shake` makes, in seconds. +const SHAKE_SECONDS: f32 = 10.0; + +impl render::Host for State { + fn set_hud(&mut self, shapes: Vec) { + self.shapes = shapes + .into_iter() + .take(SHAPES) + .map(|shape| match shape { + Shape::Sprite(mut s) => { + s.sprite = clip(s.sprite); + Shape::Sprite(s) + } + Shape::Item(mut i) => { + i.item = clip(i.item); + i.scale = if i.scale.is_finite() { + i.scale.clamp(0.0, 16.0) + } else { + 1.0 + }; + Shape::Item(i) + } + Shape::Text(mut t) => { + t.text = clip(t.text); + t.color &= 0xFF_FFFF; + Shape::Text(t) + } + other => other, + }) + .collect(); + } + fn gui_size(&mut self) -> (f32, f32) { + self.snapshot.gui_size + } + fn shake(&mut self, strength: f32, seconds: f32) { + let ok = |v: f32, max: f32| { + if v.is_finite() { + v.clamp(0.0, max) + } else { + 0.0 + } + }; + let (strength, seconds) = (ok(strength, 1.0), ok(seconds, SHAKE_SECONDS)); + if strength > 0.0 && seconds > 0.0 { + self.out.push(Output::Shake { strength, seconds }); + } + } +} + impl hud::Host for State { fn set_panel(&mut self, corner: Corner, lines: Vec) { let lines = lines diff --git a/crates/horizon-mods/tests/sandbox.rs b/crates/horizon-mods/tests/sandbox.rs index 77f0975f3..dac333fe6 100644 --- a/crates/horizon-mods/tests/sandbox.rs +++ b/crates/horizon-mods/tests/sandbox.rs @@ -1,7 +1,7 @@ //! Loads real mods built with the SDK: the coords-hud example and a guest //! that misbehaves on command. Needs `rustup target add wasm32-unknown-unknown`. use horizon_mods::{ - Clock, Corner, Mods, Output, Package, Player, Snapshot, Status, manifest::componentize, + Clock, Corner, Mods, Output, Package, Player, Shape, Snapshot, Status, manifest::componentize, }; use std::{ path::{Path, PathBuf}, @@ -22,8 +22,8 @@ id = "test_ui_guest" name = "Test UI Guest" version = "0.1.0" api = "0.1" -capabilities = ["chat", "ui"] -experimental = ["ui"] +capabilities = ["chat", "ui", "render"] +experimental = ["ui", "render"] "#; /// Builds both mods once and writes their `.hmod` files; returns the folder. @@ -89,6 +89,7 @@ fn player() -> Snapshot { game_time: 100, day_time: 24000 + 6000, }), + gui_size: (427.0, 240.0), } } @@ -416,6 +417,37 @@ fn ui_screens_settings_and_events() { assert!(mods.screen(0).is_none()); } +#[test] +fn render_shapes_and_shakes() { + let mut mods = load("horizon_mods_test_ui_guest.hmod"); + mods.set_snapshot(Snapshot { + gui_size: (427.0, 240.0), + ..Snapshot::default() + }); + assert_eq!(mods.hud_shapes().count(), 0); + assert!(mods.command("render_hud")); + let shapes: Vec<_> = mods.hud_shapes().cloned().collect(); + assert_eq!(shapes.len(), 5); + // The guest read `gui-size`; text is clipped to 256 characters. + let Shape::Text(t) = &shapes[4] else { + panic!("{:?}", shapes[4]) + }; + assert!(t.text.starts_with("gui 427x240 "), "{}", t.text); + assert_eq!(t.text.chars().count(), 256); + assert!(mods.command("render_clear")); + assert_eq!(mods.hud_shapes().count(), 0); + // One jolt, clamped; zero and NaN shake nothing. + mods.drain(); + assert!(mods.command("render_shake")); + assert_eq!( + mods.drain(), + [Output::Shake { + strength: 1.0, + seconds: 10.0 + }] + ); +} + #[test] fn content_only_mods_load_without_an_engine() { let manifest = r#" diff --git a/crates/horizon-mods/tests/ui_guest/src/lib.rs b/crates/horizon-mods/tests/ui_guest/src/lib.rs index 1af3985a1..146ab6f40 100644 --- a/crates/horizon-mods/tests/ui_guest/src/lib.rs +++ b/crates/horizon-mods/tests/ui_guest/src/lib.rs @@ -1,11 +1,61 @@ //! A mod for the runtime's `ui` tests: `/ui_screen` opens a screen with one //! of each widget, `/ui_settings` sets a settings page, `/ui_close` closes //! the screen; the `ui-events` calls answer in chat so the host sees them. +//! `/render_hud`, `/render_clear` and `/render_shake` test `render`. use horizon_mod_sdk::{ Mod, Registration, chat, + render::{self, FillShape, ItemShape, Point, QuadShape, Rect, Shape, SpriteShape, TextShape}, ui::{self, ItemWidget, ListWidget, Screen, TextWidget, Widget}, }; +/// One of each shape, the text too long by 44 characters. +fn shapes() -> Vec { + let (w, h) = render::gui_size(); + let p = |x, y| Point { x, y }; + vec![ + Shape::Fill(FillShape { + rect: Rect { + x: 4.0, + y: 4.0, + w: 60.0, + h: 20.0, + }, + color: 0x80FF0000, + }), + Shape::Quad(QuadShape { + corners: ( + p(w / 2.0, 10.0), + p(w / 2.0 + 20.0, 30.0), + p(w / 2.0, 50.0), + p(w / 2.0 - 20.0, 30.0), + ), + color: 0xC000FF00, + }), + Shape::Sprite(SpriteShape { + sprite: "hud/heart/full".into(), + rect: Rect { + x: 70.0, + y: 4.0, + w: 18.0, + h: 18.0, + }, + color: 0xFFFFFFFF, + }), + Shape::Item(ItemShape { + item: "minecraft:diamond".into(), + at: p(92.0, 4.0), + scale: 2.0, + }), + Shape::Text(TextShape { + text: format!("gui {w}x{h} {}", "x".repeat(300)), + at: p(w / 2.0, h / 2.0 - 30.0), + color: 0xFFFF55, + centered: true, + shadow: true, + }), + ] +} + struct Guest; fn widgets() -> Vec { @@ -30,9 +80,16 @@ fn widgets() -> Vec { impl Mod for Guest { fn init() -> Result<(Self, Registration), String> { - let hooks = ["ui_screen", "ui_settings", "ui_close"] - .into_iter() - .fold(Registration::new(), Registration::command); + let hooks = [ + "ui_screen", + "ui_settings", + "ui_close", + "render_hud", + "render_clear", + "render_shake", + ] + .into_iter() + .fold(Registration::new(), Registration::command); Ok((Guest, hooks)) } fn command(&mut self, name: &str, _args: &str) -> Result, String> { @@ -43,6 +100,14 @@ impl Mod for Guest { }), "ui_settings" => ui::set_settings(&widgets()), "ui_close" => ui::close_screen(), + "render_hud" => render::set_hud(&shapes()), + "render_clear" => render::set_hud(&[]), + // Clamped to (1, 10); the two after it shake nothing. + "render_shake" => { + render::shake(2.0, 99.0); + render::shake(0.0, 1.0); + render::shake(f32::NAN, 1.0); + } _ => {} } Ok(None) diff --git a/docs/api/mods-reference.md b/docs/api/mods-reference.md index 0812cde54..ef889d599 100644 --- a/docs/api/mods-reference.md +++ b/docs/api/mods-reference.md @@ -13,6 +13,7 @@ The contract between Horizon and its mods: the WIT package `horizon:mods@0.1.0`, | `storage` | [`storage`](#storage) | save its own settings to disk | | `registries` | data files (`assets/`, `data/`), no interface | add blocks and items to your worlds | | `ui` | [`ui`](#ui) | show its own screens and a settings page | +| `render` | [`render`](#render) | draw on your screen and shake the camera | ## World `horizon-mod` @@ -23,7 +24,7 @@ The contract between Horizon and its mods: the WIT package `horizon:mods@0.1.0`, A mod with widgets (capability `ui`): the SDK builds every mod against this world. Mods built for `horizon-mod` alone (no `ui-events` export) still load and simply hear nothing of their widgets. -- Imports (what the game offers): `ui`, `log`, `game`, `chat`, `hud`, `storage` +- Imports (what the game offers): `ui`, `render`, `log`, `game`, `chat`, `hud`, `storage` - Exports (what the mod provides): `ui-events`, `events` ## World `ui-host` @@ -33,6 +34,13 @@ The game's side of `horizon-ui-mod` beyond `horizon-mod`. - Imports (what the game offers): `ui` - Exports (what the mod provides): `ui-events` +## World `render-host` + +The game's side of capability `render` (imports only). + +- Imports (what the game offers): `render` +- Exports (what the mod provides): + ## `log` Access: always granted. @@ -205,6 +213,90 @@ What the game calls on a mod that shows widgets (capability `ui`): what the play | `closed` | `func()` | The player left the mod's screen (Esc or Done); not for a screen the mod closed itself or one that went away with the mod. | | `settings-opened` | `func()` | The player opened the mod's settings page in Options > Mods: call `ui::set-settings` now to fill it with fresh widgets. | +## `render` + +Access: capability `render`. + +Capability `render` (experimental: the manifest needs `experimental = ["render"]`): shapes the game draws for the mod with its own renderer, kept until the mod replaces them (no call per frame). A mod never supplies shader code. + +### record `rect` + +A rectangle in GUI pixels: the top left corner and the size. + +| Field | Type | | +| --- | --- | --- | +| `x` | `f32` | | +| `y` | `f32` | | +| `w` | `f32` | | +| `h` | `f32` | | + +### record `point` + +A point in GUI pixels from the screen's top left. + +| Field | Type | | +| --- | --- | --- | +| `x` | `f32` | | +| `y` | `f32` | | + +### record `fill-shape` + +| Field | Type | | +| --- | --- | --- | +| `rect` | `rect` | | +| `color` | `u32` | 0xAARRGGBB. | + +### record `quad-shape` + +Any four-cornered shape (a wheel's segment, an arrow): the corners in order round it. + +| Field | Type | | +| --- | --- | --- | +| `corners` | `tuple` | | +| `color` | `u32` | 0xAARRGGBB. | + +### record `sprite-shape` + +| Field | Type | | +| --- | --- | --- | +| `sprite` | `string` | One of the game's GUI sprites, as `gui/sprites/` names them (`hud/heart/full`, `widget/button`), the resource packs' versions included. An unknown one draws nothing. | +| `rect` | `rect` | | +| `color` | `u32` | A tint, 0xAARRGGBB: 0xFFFFFFFF draws it as it is. | + +### record `item-shape` + +| Field | Type | | +| --- | --- | --- | +| `item` | `string` | `minecraft:diamond`, `:`; an unknown one draws nothing. | +| `at` | `point` | The icon's top left. | +| `scale` | `f32` | 1.0 is the hotbar's 16 × 16. | + +### record `text-shape` + +| Field | Type | | +| --- | --- | --- | +| `text` | `string` | Up to 256 characters, one line. | +| `at` | `point` | | +| `color` | `u32` | 0xRRGGBB, as `hud`'s lines. | +| `centered` | `bool` | Centred on `at.x` instead of starting there. | +| `shadow` | `bool` | | + +### variant `shape` + +| Case | Payload | | +| --- | --- | --- | +| `fill` | `fill-shape` | | +| `quad` | `quad-shape` | | +| `sprite` | `sprite-shape` | | +| `item` | `item-shape` | | +| `text` | `text-shape` | | + +| Function | Signature | What it does | +| --- | --- | --- | +| `set-hud` | `func(shapes: list)` | Replaces the mod's HUD shapes: up to 1024, drawn every frame in order over the HUD (after the hotbar, under chat and screens), hidden with the HUD (F1). An empty list removes them. | +| `gui-size` | `func() -> tuple` | The screen's size in GUI pixels (width, height), as of this tick. | +| `shake` | `func(strength: f32, seconds: f32)` | Shakes the camera: a jolt of `strength` (0 to 1, 1 the hardest) that fades out over `seconds` (at most 10). Jolts add up to 1. The view turns, the player doesn't, and the player's Distortion Effects option scales it (0 turns it off). | + ## `events` Access: exported by the mod: the game calls it. diff --git a/docs/api/mods.md b/docs/api/mods.md index bec03e84e..cda484c31 100644 --- a/docs/api/mods.md +++ b/docs/api/mods.md @@ -95,5 +95,7 @@ What real mods need next, in the order modpacks and servers need it: ## Changelog +- Experimental `render` (`experimental = ["render"]`): HUD shapes (fill, quad, sprite, item, text), `gui-size`, camera + `shake`, drawn by the game's own renderer. - API 0.2 (content): data-driven blocks and items, tags, `horizon:add_to`, terrain adaptation. - API 0.1.0: `log`, `game`, `chat`, `hud`, `storage`; `init`, `tick`, `frame`, `command`, `chat`. diff --git a/docs/api/stability.md b/docs/api/stability.md index ffa9d9b11..76c3c4e51 100644 --- a/docs/api/stability.md +++ b/docs/api/stability.md @@ -88,6 +88,7 @@ Deprecations and breaking changes are in bold. The release notes of the game rel | --- | --- | --- | | Mod API: `log`, `game`, `chat`, `hud`, `storage`, `events` | preview | 0.1.0 | | Mod API: `ui`, `ui-events` (a mod's screens and settings page) | experimental | manifest `experimental = ["ui"]` | +| Mod API: `render` (HUD shapes, camera shake; drawn by the game, no mod shaders) | experimental | manifest `experimental = ["render"]` | | Content mods (`registries`: data-driven blocks and items) | preview | manifest `api = "0.2"` | | Server mods (events, world, server commands) | not started | manifest `api = "0.3"`, experimental first | | Script mods (Lua run by an interpreter mod, [mods.md](mods.md#ways-in)) | not started | | diff --git a/docs/mods.md b/docs/mods.md index 9b3b8aa94..672d6c7fd 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -87,6 +87,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | `hud` | `hud` | `set-panel(corner, lines)`, `clear()`: up to 16 lines of 256 characters | | `storage` | `storage` | `get(key)`, `set(key, value)`, `delete(key)`: a per-mod folder of up to 64 files, 4 MiB total, at most 1 MiB written per call | | `ui` | `ui` | `open-screen`, `close-screen`, `set-settings`: a screen the mod owns and a page in Options > Mods, of text, item and list widgets the game lays out and draws with its own menus' code. **Experimental:** the manifest needs `experimental = ["ui"]` | +| `render` | `render` | `set-hud(shapes)`: up to 1024 fills, four-cornered shapes, GUI sprites, items and text, kept on the HUD (over the hotbar, under chat and screens, hidden with F1) until replaced; `gui-size()`; `shake(strength, seconds)`: the view turns, the player doesn't, scaled by Distortion Effects. The game's own HUD renderer draws them: a mod never supplies shader code. **Experimental:** `experimental = ["render"]` | | Hook (export) | When | |---|---| @@ -114,7 +115,7 @@ loading. Ordered by what the first native ports need: | `inventory-read` | the player's inventory, held item, open container | AppleSkin, recipe viewers | | `recipes` | the recipe registry and lookups | JEI/EMI-style viewers | | `world-read` | blocks and biomes around the player, within the render distance | minimaps, schematics | -| `render` | world-space lines, boxes and ghost blocks in a per-frame budget | Litematica-style placement, waypoints | +| `render` (world) | world-space lines, boxes and billboards, beside the HUD shapes `render` has | Litematica-style placement, waypoints | | `registries` | new blocks, items, entities and recipes, mostly data: JSON models, textures, loot and recipes in the `.hmod`'s `assets/` and `data/` | server side; see [Multiplayer](#multiplayer) | | `server` | world events (block break/place, entity spawn/death, player join), game rules, server commands | runs where the world is simulated | | `net` | a mod's own client↔server channel (custom payloads) | only between the two halves of the same mod | @@ -288,7 +289,7 @@ experimental = [] # experimental capabilities it uses, e.g. ["ui"] ``` Unknown keys are refused, so a typo is an error, not a silently ignored -permission. An experimental capability (today `ui`) may change in any release +permission. An experimental capability (today `ui` and `render`) may change in any release ([stability](api/stability.md)): a mod uses it only by naming it in `experimental` as well, so nobody depends on it by accident. From 09ff66bfeee243b10396a77381095eaa31670327 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 25 Oct 2026 20:39:44 +0000 Subject: [PATCH 169/238] Fix stale server digging after instant survival breaks Clear completed guest digging so replanted flowers and crops are not broken again after attack is released. Add a LAN replanting regression for both single-player paths. Co-Authored-By: Codex (gpt-6.1-sol) --- crates/horizon-client/src/game/lan_actions.rs | 11 ++- tools/lan_e2e.py | 68 +++++++++++++++++++ 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 02434d793..ab7803ddd 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -1810,14 +1810,13 @@ impl Game<'_> { g.use_delay -= 1; } }); - // ServerPlayerGameMode.handleBlockBreakAction: a creative - // player's START breaks the block once (destroyAndAck) and - // leaves nothing being destroyed; its client sends no STOP. - // Kept, the dig broke whatever was put there next, every - // destroy delay, for as long as the player broke nothing else. + // ServerPlayerGameMode.handleBlockBreakAction: creative and + // instant survival STARTs finish without a STOP from the client. + // tick_mining clears mining on completion; forget the START too, + // so a flower or crop planted here later is not broken again. if digging.is_some() - && host.hub.guests[gi].creative() && let Some(play) = host.play.get_mut(&conn) + && (host.hub.guests[gi].creative() || play.mining.is_none()) { play.digging = None; } diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index f79e07033..f0333c87b 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -250,6 +250,8 @@ address. The script then checks, from both sides, what a guest's game used to ge - falling: an item and a falling block the host summons high up fall smoothly on the guest, a little further every tick between the server's updates (every 20 ticks), as their own client tick moves them (they used to hang, then jump down once a second, #1568). +- replant: flowers and crops replanted after a short harvest click stay planted for both players; + the server forgets the completed instant break (#1880). Each check is reported on its own (a failing one doesn't stop the others) and the screenshots of both sides go to --out (host-*.png, guest-*.png). @@ -6348,5 +6350,71 @@ CHECKS += ['sprint-fov'] CHECK_FNS['sprint-fov'] = check_sprint_fov +def check_replant(host, guest, out): + """Instant breaks leave no server dig that can destroy a replacement (#1880). + + ServerPlayerGameMode.handleBlockBreakAction's insta-mine branch finishes on + START; MultiPlayerGameMode sends no STOP or ABORT when attack is released. + Exercise actual flower placement and seed planting after a short click. + """ + spots = apart(host, guest) + seen = {} + host('command', command='gamerule random_tick_speed 0') + try: + for label, player in (('host', host), ('guest', guest)): + x, y, z = spots[label] + cell = (x, y, z + 2) + + def observe(block): + # The integrated host is itself a client: wait for both views. + for _ in range(40): + blocks = [g.raw('block', *cell).get('block') for g in (host, guest)] + if blocks == [f'minecraft:{block}'] * 2: + return blocks + host.wait(2) + raise AssertionError(f'{label} expected {block} on both clients: {blocks}') + + for mode, block, item, soil in ( + ('survival', 'dandelion', 'dandelion', 'dirt'), + ('survival', 'wheat', 'wheat_seeds', 'farmland[moisture=7]'), + ('survival', 'carrots', 'carrot', 'farmland[moisture=7]'), + ('creative', 'dandelion', 'dandelion', 'dirt'), + ): + player('command', command=f'gamemode {mode}') + player('command', command='clear @s') + player('command', command=f'item replace entity @s weapon.mainhand with minecraft:{item} 8') + player('command', command=f'setblock {x} {y - 1} {z + 2} minecraft:{soil}') + player('command', command=f'setblock {x} {y} {z + 2} minecraft:{block}') + player.wait(5) + observe(block) + # Flowers' narrow outlines have a position-dependent offset. + for dx in (0.0, -0.2, 0.2): + player.raw('look-at', x + 0.5 + dx, y + (0.3 if block == 'dandelion' else 0.1), z + 2.5) + target = player('looking_at').get('block') or {} + if target.get('pos') == list(cell): + break + assert target.get('pos') == list(cell), f'{label} does not aim at {block}: {target}' + player.raw('mouse', 'left', 'click') + player.wait(5) + observe('air') + # Keep aiming at the supporting soil and use the held flower/seeds. + player.raw('mouse', 'right', 'click') + player.wait(3) + observe(block) + player.wait(20) # past the stale dig's five-tick destroy delay + key = f'{label} {mode} {block}' + seen[key] = [g.raw('block', *cell).get('block') for g in (host, guest)] + player.shot(out, f'replant-{mode}-{block}') + assert seen[key] == [f'minecraft:{block}'] * 2, f'{key}: {seen}' + player('command', command=f'setblock {x} {y} {z + 2} minecraft:air') + finally: + host('command', command='gamerule random_tick_speed 3') + return seen + + +CHECKS += ['replant'] +CHECK_FNS['replant'] = check_replant + + if __name__ == '__main__': main() From b3807d6e505de48df82e5861de400e559cbd2988 Mon Sep 17 00:00:00 2001 From: levzzz <86072227+levzzz5154@users.noreply.github.com> Date: Mon, 26 Oct 2026 01:45:10 +0500 Subject: [PATCH 170/238] Chat: the Chat option hides what it says it hides The Chat Settings option was saved and shown but never applied: Hidden and Commands Only still sent and displayed ordinary chat, and Hidden still displayed the server's messages. Minecraft.computeChatAbilities turns the option into ChatRestriction's permissions, and they are now honoured where vanilla honours them: ChatScreen refuses to submit input they take (hasAllowedInput) and says why in a red usage line, ClientPacketListener.sendChat/sendCommand stop it before the packet, ChatListener drops the player, server and client sources the option hides, and ClientInformation carries the visibility to a server (at login, and again when it changes, as Options.broadcastOptions does). Fixes #1376 Co-Authored-By: [CC] --- .../examples/probe/chat_screen_fixture.rs | 3 +- crates/horizon-client/src/chat.rs | 105 +++++++++++++++++- crates/horizon-client/src/chat_tests.rs | 95 +++++++++++++++- crates/horizon-client/src/game/chat.rs | 22 +++- crates/horizon-client/src/game/remote.rs | 63 ++++++++--- 5 files changed, 260 insertions(+), 28 deletions(-) diff --git a/crates/horizon-client/examples/probe/chat_screen_fixture.rs b/crates/horizon-client/examples/probe/chat_screen_fixture.rs index 229d8d861..db334797d 100644 --- a/crates/horizon-client/examples/probe/chat_screen_fixture.rs +++ b/crates/horizon-client/examples/probe/chat_screen_fixture.rs @@ -7,7 +7,7 @@ //! usage: chat_screen_fixture RESOURCES REGISTRIES CASES OUTPUT use crate::pack_screen_fixture; use horizon_client::{ - chat::{ChatAction, ChatScreen, Commands}, + chat::{ChatAbilities, ChatAction, ChatScreen, ChatVisibility, Commands}, command::vanilla, hud::HudAtlas, lang, @@ -53,6 +53,7 @@ pub fn main() -> horizon_client::Result<()> { history.len(), w, h, + ChatAbilities::new(ChatVisibility::Full), ); screen.auto_suggestions = case["autoSuggest"] == true; let mut closed = false; diff --git a/crates/horizon-client/src/chat.rs b/crates/horizon-client/src/chat.rs index 273b87153..e14178c8d 100644 --- a/crates/horizon-client/src/chat.rs +++ b/crates/horizon-client/src/chat.rs @@ -19,6 +19,78 @@ mod tests; /// GuiMessageTag.systemSinglePlayer's indicator colour. pub const SYSTEM_INDICATOR: u32 = 0xD0D0D0; +/// `ChatVisiblity`: the Chat option's three values. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ChatVisibility { + Full, + System, + Hidden, +} +impl ChatVisibility { + /// Options.chatVisibility's saved value (0 full, 1 system, 2 hidden). + pub fn from_option(value: u8) -> Self { + match value { + 0 => Self::Full, + 1 => Self::System, + _ => Self::Hidden, + } + } +} + +/// `ChatAbilities`: what the Chat option allows, as `Minecraft.computeChatAbilities` +/// derives it from the visibility alone (`ChatRestriction`). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct ChatAbilities { + pub send_messages: bool, + pub send_commands: bool, + pub receive_player_messages: bool, + pub receive_system_messages: bool, +} +impl ChatAbilities { + /// `ChatRestriction.CHAT_AND_COMMANDS_DISABLED_BY_OPTIONS` (Hidden) takes + /// chat, commands, player messages and system messages; + /// `CHAT_DISABLED_BY_OPTIONS` (System) takes the chat and player messages. + pub fn new(visibility: ChatVisibility) -> Self { + match visibility { + ChatVisibility::Full => Self { + send_messages: true, + send_commands: true, + receive_player_messages: true, + receive_system_messages: true, + }, + ChatVisibility::System => Self { + send_messages: false, + send_commands: true, + receive_player_messages: false, + receive_system_messages: true, + }, + ChatVisibility::Hidden => Self { + send_messages: false, + send_commands: false, + receive_player_messages: false, + receive_system_messages: false, + }, + } + } + /// Whether the chat screen may send `input` (`hasAllowedInput`). + pub fn allows(&self, input: &str) -> bool { + if input.starts_with('/') { + self.send_commands + } else { + self.send_messages + } + } +} + +/// Where a chat line came from (`GuiMessageSource`): the visibility filters +/// player messages and the server's, while the client's own always show. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ChatSource { + Player, + Server, + Client, +} + /// The chat options (doubles, as vanilla keeps them). #[derive(Clone, Copy, Debug)] pub struct ChatOptions { @@ -437,6 +509,8 @@ pub struct ChatScreen { allow_suggestions: bool, keep_suggestions: bool, pub auto_suggestions: bool, + /// `CommandSuggestions.setRestrictions`: what the Chat option allows. + pub abilities: ChatAbilities, } /// Local completions waiting for the server's reply to request `id`. struct Awaiting { @@ -482,6 +556,7 @@ impl ChatScreen { history_len: usize, width: i32, height: i32, + abilities: ChatAbilities, ) -> Self { let mut input = TextField::new(4, height - 12, width - 4, 12); input.max_length = 256; @@ -505,6 +580,7 @@ impl ChatScreen { allow_suggestions: false, keep_suggestions: false, auto_suggestions: true, + abilities, }; s.update_command_info(m, c); s @@ -522,7 +598,8 @@ impl ChatScreen { if (width, height) != (self.width, self.height) { let value = self.input.value.clone(); let auto = self.auto_suggestions; - *self = Self::new(m, c, &value, self.history_pos, width, height); + let abilities = self.abilities; + *self = Self::new(m, c, &value, self.history_pos, width, height, abilities); self.auto_suggestions = auto; } } @@ -604,6 +681,14 @@ impl ChatScreen { self.pending = Some((range.0, list)); self.usage_width = 0; self.usage_position = 0; + // CommandSuggestions.updateCommandInfo: the Chat option may take + // the messages. + if !self.abilities.send_messages { + let line = + Text::translate("chat_screen.messages_not_allowed", &[]).with_color(text::RED); + self.usage_width = m.width(&line); + self.usage.push(line); + } } else { self.awaiting = None; self.pending = None; @@ -708,6 +793,15 @@ impl ChatScreen { } self.usage.extend(entries); } + // CommandSuggestions.updateUsageInfo: the Chat option may take the + // commands, under the usage. (Vanilla also counts a command with a + // message argument, `/say hi`, as a message; Horizon's parse keeps no + // argument kinds, so only a bare message does.) + if !self.abilities.send_commands { + self.usage.push( + Text::translate("chat_screen.commands_not_allowed", &[]).with_color(text::RED), + ); + } self.usage_width = self.usage.iter().map(|u| m.width(u)).max().unwrap_or(0); self.usage_position = if self.usage.is_empty() { 0 @@ -842,7 +936,14 @@ impl ChatScreen { } match key { K::Escape => return ChatAction::Close, - K::Enter | K::NumpadEnter => return ChatAction::Submit(self.normalized()), + K::Enter | K::NumpadEnter => { + // ChatScreen.keyPressed: without `hasAllowedInput` the input + // stays, unsent, and the screen stays open. + if !self.abilities.allows(&self.input.value) { + return ChatAction::None; + } + return ChatAction::Submit(self.normalized()); + } K::ArrowUp => self.move_in_history(m, c, history, -1), K::ArrowDown => self.move_in_history(m, c, history, 1), _ => { diff --git a/crates/horizon-client/src/chat_tests.rs b/crates/horizon-client/src/chat_tests.rs index d0ce3f458..cd5860bfb 100644 --- a/crates/horizon-client/src/chat_tests.rs +++ b/crates/horizon-client/src/chat_tests.rs @@ -2,6 +2,11 @@ use super::*; use crate::{font::Font, hud::Rect}; use std::{collections::HashMap, path::Path}; +/// The Chat option's default: no restriction. +fn full() -> ChatAbilities { + ChatAbilities::new(ChatVisibility::Full) +} + fn font() -> Font { // The missing-glyph metrics let these tests run without game assets. Font::load( @@ -139,7 +144,7 @@ fn locate_coordinates_fill_chat_with_teleport_without_submitting() { assert_eq!(link.component()["click_event"]["command"], expected); let mut chat = Chat::default(); add(&mut chat, &font, Text::literal("Found at ").append(link)); - let mut screen = ChatScreen::new(&m, &commands, "draft", 0, 640, 360); + let mut screen = ChatScreen::new(&m, &commands, "draft", 0, 640, 360, full()); let x = 4.0 + f64::from(font.width("Found at ")); assert!(!screen.click(&m, &commands, x, 315.0)); let Some(ClickEvent::SuggestCommand(command)) = chat.click_at(&font, 360, x, 315.0) else { @@ -176,7 +181,15 @@ fn history_arrows_recall_messages_and_restore_the_draft() { registries: ®istries, }; let history = vec!["first message".to_owned(), "latest message".to_owned()]; - let mut screen = ChatScreen::new(&m, &commands, "unfinished draft", history.len(), 640, 360); + let mut screen = ChatScreen::new( + &m, + &commands, + "unfinished draft", + history.len(), + 640, + 360, + full(), + ); for (key, expected) in [ (ArrowUp, "latest message"), (ArrowUp, "first message"), @@ -213,7 +226,7 @@ fn mod_commands_are_coloured_and_complete_like_vanilla_ones() { dispatcher, registries: ®istries, }; - let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360); + let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360, full()); screen.type_text(&m, &c, line); screen }; @@ -259,7 +272,7 @@ fn ask_server_nodes_wait_for_the_servers_completions() { let tab = |screen: &mut ChatScreen| { screen.key(&m, &c, &[], winit::keyboard::KeyCode::Tab, false, false); }; - let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360); + let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360, full()); screen.type_text(&m, &c, "/bossbar get pt"); let (request, sent) = screen.take_request().expect("the server is asked"); assert_eq!(sent, "/bossbar get pt"); @@ -281,7 +294,7 @@ fn ask_server_nodes_wait_for_the_servers_completions() { dispatcher: local, registries: ®istries, }; - let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360); + let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360, full()); screen.type_text(&m, &c, "/bossbar get pt"); assert_eq!(screen.take_request(), None); } @@ -327,3 +340,75 @@ fn a_server_component_keeps_its_click_and_hover_events() { let past = 4.0 + f64::from(font.width("CLICK")) + 20.0; assert!(chat.hover_at(&font, 360, past, 315.0).is_none()); } + +/// `ChatVisiblity`: Commands Only keeps the chat out, Hidden takes the +/// commands too (`ChatAbilities`), and Enter sends nothing the option hides. +#[test] +fn the_chat_option_decides_what_may_be_sent() { + let font = font(); + let m = Measure(&font); + let registries = Registries::default(); + let c = Commands { + dispatcher: crate::command::vanilla::game_dispatcher(), + registries: ®istries, + }; + let enter = |screen: &mut ChatScreen| { + screen.key(&m, &c, &[], winit::keyboard::KeyCode::Enter, false, false) + }; + for (visibility, chat, command) in [ + (ChatVisibility::Full, true, true), + (ChatVisibility::System, false, true), + (ChatVisibility::Hidden, false, false), + ] { + let abilities = ChatAbilities::new(visibility); + assert_eq!(abilities.allows("hi"), chat, "{visibility:?}"); + assert_eq!(abilities.allows("/time set day"), command, "{visibility:?}"); + for (line, allowed) in [("hi", chat), ("/time set day", command)] { + let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360, abilities); + screen.type_text(&m, &c, line); + match enter(&mut screen) { + ChatAction::Submit(sent) => { + assert!(allowed, "{visibility:?} sent {sent}"); + assert_eq!(sent, line); + } + ChatAction::None => assert!(!allowed, "{visibility:?} kept {line}"), + ChatAction::Close => panic!("Enter does not close the chat"), + } + } + } +} + +/// The restriction the option sets is a usage line: red, under the input, for +/// a command the Chat option takes and for a message it takes. +#[test] +fn restricted_input_says_why_it_will_not_send() { + let font = font(); + let m = Measure(&font); + let registries = Registries::default(); + let c = Commands { + dispatcher: crate::command::vanilla::game_dispatcher(), + registries: ®istries, + }; + let usage = |visibility, line: &str| { + let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360, ChatAbilities::new(visibility)); + screen.type_text(&m, &c, line); + screen.usage.iter().map(|t| t.plain()).collect::>() + }; + assert_eq!(usage(ChatVisibility::Full, "hi"), Vec::::new()); + // The font has no language files here, so the line is its key. + assert_eq!( + usage(ChatVisibility::System, "hi"), + ["chat_screen.messages_not_allowed"] + ); + // A command the option allows says nothing about the restriction. + assert!( + !usage(ChatVisibility::System, "/time set day") + .iter() + .any(|line| line == "chat_screen.commands_not_allowed") + ); + assert!( + usage(ChatVisibility::Hidden, "/time set day") + .iter() + .any(|line| line == "chat_screen.commands_not_allowed") + ); +} diff --git a/crates/horizon-client/src/game/chat.rs b/crates/horizon-client/src/game/chat.rs index 23a0f0965..fafe88f26 100644 --- a/crates/horizon-client/src/game/chat.rs +++ b/crates/horizon-client/src/game/chat.rs @@ -2,7 +2,10 @@ //! (game rules, the world clock, difficulty), bound to the game. use super::{Game, Screen}; use horizon_client::{ - chat::{Chat, ChatAction, ChatScreen, Commands, SYSTEM_INDICATOR}, + chat::{ + Chat, ChatAbilities, ChatAction, ChatScreen, ChatSource, ChatVisibility, Commands, + SYSTEM_INDICATOR, + }, command::Registries, gamerules::GameRules, hud::HudBatch, @@ -425,6 +428,7 @@ impl Game<'_> { self.ui.chat.recent.len(), b.width as i32, b.height as i32, + self.chat_abilities(), ); screen.auto_suggestions = self.settings.auto_suggestions; self.ui.chat_screen = Some(screen); @@ -443,6 +447,10 @@ impl Game<'_> { self.capture(true); } } + /// `Minecraft.computeChatAbilities` from the Chat option. + pub(super) fn chat_abilities(&self) -> ChatAbilities { + ChatAbilities::new(ChatVisibility::from_option(self.settings.chat_visibility)) + } /// A chat message from the game (ChatComponent.addServerSystemMessage). pub(super) fn system_message(&mut self, text: Text) { if let Some(capture) = self.chat_capture.as_mut() { @@ -452,7 +460,12 @@ impl Game<'_> { // Connected to a server (single player's own included), the chat on // screen is the connection's: the game's own messages join it. if self.remote.is_some() { - self.chat_line(text); + self.chat_line(text, ChatSource::Server); + return; + } + // ChatListener.handleSystemMessage: the Chat option's Hidden level + // takes the server's messages too. + if !self.chat_abilities().receive_system_messages { return; } let focused = self.screen == Screen::Chat; @@ -570,7 +583,12 @@ impl Game<'_> { } } /// ChatScreen.handleChatInput: history, then a command or a chat line. + /// The Chat option's restrictions stop the message here too, so a + /// control-socket `chat` cannot send what the player's setting hides. pub(super) fn handle_chat_input(&mut self, message: &str) { + if !self.chat_abilities().allows(message) { + return; + } self.ui.chat.add_recent(message); // On a server (single player's own included) the server runs it, // unless a client mod owns the command. diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index ba069522c..80adb5ff6 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -123,6 +123,9 @@ pub(super) struct Remote { loaded_sent: bool, /// The view distance last sent in ClientInformation. sent_view: i8, + /// The Chat option's visibility last sent in ClientInformation + /// (`Options.broadcastOptions` sends it again when it changes). + sent_chat_visibility: i32, /// BlockStatePredictionHandler: blocks changed ahead of the server (a predicted break), /// with the sequence of the last action that changed them and the state the server /// last said they have. Until the server acknowledges that sequence its block updates @@ -430,6 +433,7 @@ impl Game<'_> { abilities: None, loaded_sent: false, sent_view: view, + sent_chat_visibility: self.settings.chat_visibility as i32, predictions: HashMap::new(), menu: None, menu_title: None, @@ -794,8 +798,22 @@ impl Game<'_> { Some(stack) } /// A line in the connection's chat, with its styles, click and hover - /// events (ChatComponent.addMessage). - pub(super) fn chat_line(&mut self, content: horizon_client::text::Text) { + /// events (ChatComponent.addMessage). `ChatListener` drops the lines the + /// Chat option hides before they reach the chat. + pub(super) fn chat_line( + &mut self, + content: horizon_client::text::Text, + source: horizon_client::chat::ChatSource, + ) { + let abilities = self.chat_abilities(); + let allowed = match source { + horizon_client::chat::ChatSource::Player => abilities.receive_player_messages, + horizon_client::chat::ChatSource::Server => abilities.receive_system_messages, + horizon_client::chat::ChatSource::Client => true, + }; + if !allowed { + return; + } let text = content.plain(); log::info!("[chat] {text}"); self.ui.chat.add( @@ -1776,9 +1794,10 @@ impl Game<'_> { if let Some(r) = self.remote.as_mut() { r.sent_view = view; } - self.send(S::ClientInformation( - horizon_protocol::packets::configuration::ClientInformation::new(view), - )); + let mut info = + horizon_protocol::packets::configuration::ClientInformation::new(view); + info.chat_visibility = self.settings.chat_visibility as i32; + self.send(S::ClientInformation(info)); } C::Respawn(p) => { // A new player, without effects: the server sends them again. @@ -2568,7 +2587,7 @@ impl Game<'_> { if p.overlay { self.titles.set_overlay(text, false); } else { - self.chat_line(text); + self.chat_line(text, horizon_client::chat::ChatSource::Server); } } C::PlayerChat(p) => { @@ -2578,12 +2597,12 @@ impl Game<'_> { .as_ref() .map_or_else(|| serde_json::Value::from(p.content.as_str()), tag_to_json); let line = self.format_chat(&p.chat_type, content); - self.chat_line(line); + self.chat_line(line, horizon_client::chat::ChatSource::Player); } C::DisguisedChat(p) => { let content = horizon_client::scoreboard::tag_to_json(&p.message); let line = self.format_chat(&p.chat_type, content); - self.chat_line(line); + self.chat_line(line, horizon_client::chat::ChatSource::Player); } C::PlayerInfoUpdate(p) => { if let Some(r) = self.remote.as_mut() { @@ -3216,16 +3235,19 @@ impl Game<'_> { fn tick_remote_level(&mut self) -> Result<()> { self.ticks += 1; self.track_experience_display(); - // Options.broadcastOptions: a changed render distance is sent again. + // Options.broadcastOptions: a changed render distance or Chat option + // is sent again. let view = client_view_distance(self.settings.render_distance); + let visibility = self.settings.chat_visibility as i32; if let Some(r) = self.remote.as_mut() && r.joined - && r.sent_view != view + && (r.sent_view != view || r.sent_chat_visibility != visibility) { r.sent_view = view; - self.send(S::ClientInformation( - horizon_protocol::packets::configuration::ClientInformation::new(view), - )); + r.sent_chat_visibility = visibility; + let mut info = horizon_protocol::packets::configuration::ClientInformation::new(view); + info.chat_visibility = visibility; + self.send(S::ClientInformation(info)); } // Minecraft.tick: TickRateManager.tick; ClientLevel.tickTime advances // the time only while the level runs normally. @@ -4643,10 +4665,12 @@ impl Game<'_> { _ => Ok(false), } } - /// ClientPacketListener.sendChat / sendCommand (unsigned). + /// ClientPacketListener.sendChat / sendCommand (unsigned). The Chat + /// option's Hidden and System levels keep chat out and Hidden keeps the + /// commands out (ChatAbilities). pub(super) fn send_chat(&mut self, message: &str) { let message = message.trim(); - if message.is_empty() { + if message.is_empty() || !self.chat_abilities().allows(message) { return; } if let Some(command) = message.strip_prefix('/') { @@ -5026,9 +5050,12 @@ impl Game<'_> { self.send(S::ContainerClose(play::ContainerClose { container: p.container, })); - self.chat_line(horizon_client::text::Text::literal(format!( - "(Horizon cannot show {menu} screens yet)" - ))); + self.chat_line( + horizon_client::text::Text::literal(format!( + "(Horizon cannot show {menu} screens yet)" + )), + horizon_client::chat::ChatSource::Client, + ); return; } }; From 3762ab7cdaa0f67c8cd473a88347098ebc982f92 Mon Sep 17 00:00:00 2001 From: Alexander Krylov Date: Sun, 25 Oct 2026 23:46:43 +0300 Subject: [PATCH 171/238] Enchantments: the name and the level follow the selected language The registry resolved every `description` from the pinned `en_us` map at load and `full_name` took `enchantment.level.` from the same table, so a tooltip showed an English enchantment line whatever language the player picked. Keep the description's translation key and resolve it, and the level, through the current language at display time, with the English text as the fallback for the server thread, the probes and an old resource root. Refs #1774. Co-Authored-By: Hermes Agent (Nous Research) --- crates/horizon-client/src/enchant.rs | 55 +++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/crates/horizon-client/src/enchant.rs b/crates/horizon-client/src/enchant.rs index 2fc9ef2fb..f5ddc84c2 100644 --- a/crates/horizon-client/src/enchant.rs +++ b/crates/horizon-client/src/enchant.rs @@ -33,7 +33,11 @@ impl Cost { #[derive(Clone, Debug)] pub struct Enchantment { pub id: Arc, + /// The name the pinned English map gave at load: the fallback for a + /// language that has no entry for the key below. pub description: String, + /// `description`'s translation key (`enchantment.minecraft.`). + pub description_key: String, pub max_level: u32, pub weight: u32, pub anvil_cost: u32, @@ -224,6 +228,7 @@ impl Enchantments { .get(key) .cloned() .unwrap_or_else(|| id.trim_start_matches("minecraft:").replace('_', " ")), + description_key: key.to_owned(), max_level: v["max_level"].as_u64().unwrap_or(1) as u32, weight: v["weight"].as_u64().unwrap_or(1) as u32, anvil_cost: v["anvil_cost"].as_u64().unwrap_or(0) as u32, @@ -329,16 +334,26 @@ impl Enchantments { } /// `Enchantment.getFullname` as plain text and whether it is a curse (red). pub fn full_name(&self, id: &str, level: u32) -> (String, bool) { + let language = crate::lang::current(); + self.full_name_in(language.as_deref(), id, level) + } + /// `full_name` against `language`; `None` keeps the English names loaded + /// above, which is what the server thread and the probes see. + pub fn full_name_in( + &self, + language: Option<&crate::lang::Language>, + id: &str, + level: u32, + ) -> (String, bool) { + let tr = |key: &str| language.and_then(|l| l.get(key).map(str::to_owned)); let Some(e) = self.get(id) else { return (format!("{id} {}", level_name(level)), false); }; - let mut name = e.description.clone(); + let mut name = tr(&e.description_key).unwrap_or_else(|| e.description.clone()); if level != 1 || e.max_level != 1 { name.push(' '); name.push_str( - self.lang - .get(&format!("enchantment.level.{level}")) - .map_or(&level_name(level), |s| s), + &tr(&format!("enchantment.level.{level}")).unwrap_or_else(|| level_name(level)), ); } (name, self.is_curse(id)) @@ -771,7 +786,7 @@ pub(crate) mod tests { let defs = [ ( "minecraft:sharpness", - json!({"anvil_cost":1,"exclusive_set":"#minecraft:exclusive_set/damage","max_cost":{"base":21,"per_level_above_first":11},"max_level":5,"min_cost":{"base":1,"per_level_above_first":11},"primary_items":"#minecraft:enchantable/melee_weapon","supported_items":"#minecraft:enchantable/sharp_weapon","weight":10,"slots":["mainhand"],"effects":{"minecraft:damage":[{"effect":{"type":"minecraft:add","value":{"type":"minecraft:linear","base":1.0,"per_level_above_first":0.5}}}]}}), + json!({"anvil_cost":1,"description":{"translate":"enchantment.minecraft.sharpness"},"exclusive_set":"#minecraft:exclusive_set/damage","max_cost":{"base":21,"per_level_above_first":11},"max_level":5,"min_cost":{"base":1,"per_level_above_first":11},"primary_items":"#minecraft:enchantable/melee_weapon","supported_items":"#minecraft:enchantable/sharp_weapon","weight":10,"slots":["mainhand"],"effects":{"minecraft:damage":[{"effect":{"type":"minecraft:add","value":{"type":"minecraft:linear","base":1.0,"per_level_above_first":0.5}}}]}}), ), ( "minecraft:smite", @@ -795,6 +810,7 @@ pub(crate) mod tests { .map(|(id, v)| Enchantment { id: Arc::from(*id), description: id.trim_start_matches("minecraft:").into(), + description_key: v["description"]["translate"].as_str().unwrap_or("").into(), max_level: v["max_level"].as_u64().unwrap() as u32, weight: v["weight"].as_u64().unwrap() as u32, anvil_cost: v["anvil_cost"].as_u64().unwrap() as u32, @@ -1088,4 +1104,33 @@ pub(crate) mod tests { assert!(!e.compatible("minecraft:sharpness", "minecraft:smite")); assert!(e.compatible("minecraft:sharpness", "minecraft:unbreaking")); } + + /// The enchantment's name and its level come from the selected language, + /// with the English name loaded above as the fallback. + #[test] + fn names_follow_the_language() { + struct Mem(HashMap>); + impl crate::lang::ResourceSource for Mem { + fn read(&self, path: &str) -> Option> { + self.0.get(path).cloned() + } + } + let ru = Mem(HashMap::from([( + "assets/minecraft/lang/ru_ru.json".to_owned(), + r#"{"enchantment.minecraft.sharpness":"Острота","enchantment.level.5":"V"}"# + .as_bytes() + .to_vec(), + )])); + let ru = crate::lang::Language::load(&[&ru], "ru_ru"); + let e = sample(); + assert_eq!( + e.full_name_in(Some(&ru), "minecraft:sharpness", 5).0, + "Острота V" + ); + // mending carries no key in the fixture: the English name stays. + assert_eq!( + e.full_name_in(Some(&ru), "minecraft:mending", 1).0, + "mending" + ); + } } From b71623dfd7a7b402b40bf6424efbcfae8f8e8c64 Mon Sep 17 00:00:00 2001 From: levzzz <86072227+levzzz5154@users.noreply.github.com> Date: Mon, 26 Oct 2026 01:50:12 +0500 Subject: [PATCH 172/238] Item cooldowns: the sweep on a slot, and no use while one runs Nothing tracked `ItemCooldowns`: `draw_item_decorations` passed a cooldown of 0.0 with a note that there was none to shade, and ender pearls were held back by an ad-hoc `pearl_cooldown` counter. `horizon_client::cooldowns` keeps the `use_cooldown` component's entries by cooldown group the way `Player.getCooldowns` does (start and end ticks, the percent `getCooldownPercent` returns), `ItemInfo` now carries the item's `use_cooldown`, and the game uses it where vanilla does: `ServerPlayerGameMode.useItem`/useItemOn refuse a stack on cooldown, `ItemStack.applyAfterUseComponentSideEffects` starts the entry when a use finishes (a thrown pearl, a wind charge, an eaten chorus fruit), `GuiGraphicsExtractor.itemCooldown` shades the slot, and a server's guests keep their own and are sent `ClientboundCooldownPacket` (started, and 0 when it ends), which a client applies. Fixes #1763 Co-Authored-By: [CC] --- crates/horizon-client/src/cooldowns.rs | 174 ++++++++++++++++++ crates/horizon-client/src/game.rs | 56 +++++- crates/horizon-client/src/game/creatures.rs | 24 +-- crates/horizon-client/src/game/lan_actions.rs | 31 +++- crates/horizon-client/src/game/remote.rs | 18 +- crates/horizon-client/src/game/wind.rs | 4 - crates/horizon-client/src/items.rs | 5 + crates/horizon-client/src/lib.rs | 1 + crates/horizon-protocol/src/packets/play.rs | 22 +++ 9 files changed, 306 insertions(+), 29 deletions(-) create mode 100644 crates/horizon-client/src/cooldowns.rs diff --git a/crates/horizon-client/src/cooldowns.rs b/crates/horizon-client/src/cooldowns.rs new file mode 100644 index 000000000..31e882712 --- /dev/null +++ b/crates/horizon-client/src/cooldowns.rs @@ -0,0 +1,174 @@ +//! `Player.getCooldowns()` (`ItemCooldowns`) as the game keeps it: the +//! `use_cooldown` component's entries by cooldown group, counted down once a +//! tick, and the percent `GuiGraphicsExtractor.itemCooldown` shades a slot +//! by. The world crate's `item_cooldowns` models vanilla's hash map and its +//! observers for the parity harness; this is the same data with the lookup a +//! slot needs, and no observer (vanilla's own player uses the plain class). +use crate::items::ItemStack; +use horizon_world::nbt::Tag; +use std::collections::HashMap; +use std::sync::Arc; + +/// A stack's `minecraft:use_cooldown` component. +#[derive(Clone, Debug, PartialEq)] +pub struct UseCooldown { + pub seconds: f32, + /// `cooldown_group`: the group the entry is filed under, else the item. + pub group: Option>, +} +impl UseCooldown { + /// `UseCooldown.ticks`: the seconds in ticks, truncated. + pub fn ticks(&self) -> i32 { + (self.seconds * 20.0) as i32 + } + /// The component as `items.json` holds it. + pub fn from_json(v: &serde_json::Value) -> Option { + let seconds = v.get("seconds")?.as_f64()? as f32; + Some(Self { + seconds, + group: v + .get("cooldown_group") + .and_then(|g| g.as_str()) + .map(Arc::from), + }) + } + /// The component as a stack's saved `components` holds it. + pub fn from_tag(t: &Tag) -> Option { + let Tag::Compound(c) = t else { + return None; + }; + let text = |k: &str| match c.get(&horizon_world::nbt::JavaString::from(k)) { + Some(Tag::String(s)) => Some(s.to_string_lossy()), + _ => None, + }; + let seconds = match c.get(&horizon_world::nbt::JavaString::from("seconds")) { + Some(Tag::Float(bits)) => f32::from_bits(*bits), + Some(Tag::Double(bits)) => f64::from_bits(*bits) as f32, + _ => return None, + }; + Some(Self { + seconds, + group: text("cooldown_group").map(Arc::from), + }) + } +} + +/// The component a stack carries: its own patch first (`ItemStack.get`), then +/// the item's default from `items.json` (`ItemInfo::use_cooldown`). +pub fn component(stack: &ItemStack, default: Option<&UseCooldown>) -> Option { + stack + .data() + .and_then(|d| d.other("minecraft:use_cooldown")) + .and_then(UseCooldown::from_tag) + .or_else(|| default.cloned()) +} + +/// `ItemCooldowns.getCooldownGroup`: the component's group, else the item id. +pub fn group(stack: &ItemStack, default: Option<&UseCooldown>) -> Arc { + component(stack, default) + .and_then(|c| c.group) + .unwrap_or_else(|| Arc::clone(&stack.item)) +} + +/// `ItemCooldowns`: start and end ticks by group, and the tick counter. +#[derive(Clone, Debug, Default)] +pub struct Cooldowns { + tick_count: i32, + entries: HashMap, (i32, i32)>, +} +impl Cooldowns { + /// `getCooldownPercent`: what is left of the entry, from 1 to 0. + pub fn percent(&self, group: &str, partial_tick: f32) -> f32 { + let Some((start, end)) = self.entries.get(group) else { + return 0.0; + }; + let duration = end.wrapping_sub(*start) as f32; + let remaining = *end as f32 - (self.tick_count as f32 + partial_tick); + (remaining / duration).clamp(0.0, 1.0) + } + /// `isOnCooldown`: whether the group still has time to run. + pub fn is_on_cooldown(&self, group: &str) -> bool { + self.percent(group, 0.0) > 0.0 + } + /// `addCooldown`. + pub fn add(&mut self, group: Arc, ticks: i32) { + self.entries.insert( + group, + (self.tick_count, self.tick_count.wrapping_add(ticks)), + ); + } + /// `ItemCooldowns.tick`: the counter, then every entry that has run out, + /// which it hands back (`onCooldownEnded`). + pub fn tick(&mut self) -> Vec> { + self.tick_count = self.tick_count.wrapping_add(1); + let done: Vec> = self + .entries + .iter() + .filter(|(_, (_, end))| *end <= self.tick_count) + .map(|(group, _)| Arc::clone(group)) + .collect(); + self.entries.retain(|_, (_, end)| *end > self.tick_count); + done + } + /// `removeCooldown`. + pub fn remove(&mut self, group: &str) { + self.entries.remove(group); + } + pub fn is_empty(&self) -> bool { + self.entries.is_empty() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + fn stack(item: &str) -> ItemStack { + ItemStack::new(item, 1) + } + + #[test] + fn the_percent_falls_from_one_to_zero_over_the_entry() { + let mut c = Cooldowns::default(); + c.add(Arc::from("minecraft:ender_pearl"), 20); + assert!(c.is_on_cooldown("minecraft:ender_pearl")); + assert_eq!(c.percent("minecraft:ender_pearl", 0.0), 1.0); + for _ in 0..10 { + assert!(c.tick().is_empty()); + } + assert_eq!(c.percent("minecraft:ender_pearl", 0.0), 0.5); + // The partial tick moves it within the tick. + assert_eq!(c.percent("minecraft:ender_pearl", 0.5), 0.475); + for _ in 0..9 { + c.tick(); + } + assert!(c.is_on_cooldown("minecraft:ender_pearl")); + // ItemCooldowns.tick drops the entry at its end tick, and says so. + assert_eq!(c.tick(), [Arc::::from("minecraft:ender_pearl")]); + assert!(!c.is_on_cooldown("minecraft:ender_pearl")); + assert!(c.is_empty()); + assert_eq!(c.percent("minecraft:ender_pearl", 0.0), 0.0); + } + + #[test] + fn the_group_comes_from_the_stack_then_the_item() { + let pearl = stack("minecraft:ender_pearl"); + let default = UseCooldown::from_json(&json!({"seconds": 1.0})).expect("the component"); + assert_eq!(&*group(&pearl, Some(&default)), "minecraft:ender_pearl"); + // A stack's own component overrides the item's, group included. + let mut grouped = stack("minecraft:ender_pearl"); + grouped.data_mut().set_other( + "minecraft:use_cooldown", + Some(Tag::compound([ + ("seconds", Tag::Float(2.0f32.to_bits())), + ("cooldown_group", Tag::String("minecraft:pearls".into())), + ])), + ); + let c = component(&grouped, Some(&default)).expect("the stack's component"); + assert_eq!(c.ticks(), 40); + assert_eq!(&*group(&grouped, Some(&default)), "minecraft:pearls"); + // An item without the component has no cooldown. + assert!(component(&stack("minecraft:dirt"), None).is_none()); + } +} diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index f05ee41e4..d7ec53eea 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1320,6 +1320,10 @@ struct Game<'a> { hand: Hand, /// The shield's item cooldown after an axe disabled it (ticks). shield_cooldown: u32, + /// `Player.getCooldowns()`: the `use_cooldown` component's entries, what + /// `itemDecorations` shades a slot by. A server's players have their own + /// (`GuestPlay::cooldowns`), swapped in for their turn. + cooldowns: horizon_client::cooldowns::Cooldowns, /// The source of a command run through /execute (the player otherwise). command_source: Option, titles: titles::Titles, @@ -1558,6 +1562,9 @@ struct Game<'a> { chat_capture: Option>, /// Packets for the guest acting now, sent once its action returns. guest_packets: Vec, + /// The acting guest's cooldowns, as (item, ticks): their packet needs the + /// hub's item registry, which the guest's turn does not hold. + guest_cooldowns: Vec<(Arc, i32)>, /// `broadcastToAdmins` lines a guest's command queued, sent once it /// returns: the line, whether operators get it and whether the console /// logs it. @@ -3394,6 +3401,25 @@ impl Game<'_> { || self.interact_mob()? || self.interact_knot()) } + /// `ItemStack.applyAfterUseComponentSideEffects`: a used stack with a + /// `use_cooldown` component starts its entry (`UseCooldown.apply`). A + /// server's guest also gets `ClientboundCooldownPacket` + /// (`ServerItemCooldowns.onCooldownStarted`), queued for their turn's end. + pub(super) fn apply_use_cooldown(&mut self, stack: &ItemStack) { + let default = self + .res + .items() + .get(&stack.item) + .and_then(|i| i.use_cooldown.clone()); + let Some(component) = horizon_client::cooldowns::component(stack, default.as_ref()) else { + return; + }; + let group = horizon_client::cooldowns::group(stack, default.as_ref()); + self.cooldowns.add(Arc::clone(&group), component.ticks()); + if self.lan_acting.is_some() { + self.guest_cooldowns.push((group, component.ticks())); + } + } /// One hand's turn in `startUseItem`: `useItemOn` the block aimed at, /// then `useItem`. Whether the click ends here (it acted or failed). fn use_hand(&mut self, block_hit: Option<&Hit>) -> Result { @@ -3478,6 +3504,17 @@ impl Game<'_> { let Some(info) = self.res.items().get(&stack.item).cloned() else { return Ok(false); }; + // ServerPlayerGameMode.useItem and useItemOn: an item on cooldown is + // not used (PASS, so the other hand may still act). + if self + .cooldowns + .is_on_cooldown(&horizon_client::cooldowns::group( + &stack, + info.use_cooldown.as_ref(), + )) + { + return Ok(false); + } if block_hit.is_some() && (self.use_bone_meal(&stack)? || self.ignite_tnt(&stack)?) { return Ok(true); } @@ -3804,6 +3841,8 @@ impl Game<'_> { } if ticks >= duration { self.using = None; + // ItemStack.finishUsingItem's applyAfterUseComponentSideEffects. + self.apply_use_cooldown(&stack); self.finish_consuming(&info, consumable, &stack); } else { self.using = Some((slot, ticks)); @@ -4405,6 +4444,7 @@ impl Game<'_> { // Player.tick's ItemCooldowns.tick for the window's player (a // server's players count theirs down in `tick_guests`). self.shield_cooldown = self.shield_cooldown.saturating_sub(1); + self.cooldowns.tick(); horizon_client::spans::time("tick_using", || self.tick_using()); horizon_client::spans::time("tick_hud", || self.tick_hud()); // The player's digging comes in with its packets, before the level @@ -4786,17 +4826,17 @@ impl Game<'_> { count_text: Option, ) { // GuiGraphicsExtractor.itemDecorations, laid out at the origin and - // moved to (x, y). Horizon tracks no item cooldowns to shade yet. - let max_damage = self - .res - .items() - .get(&stack.item) - .map_or(0, |info| info.max_damage as i32); + // moved to (x, y): the damage bar, the cooldown shade and the count. + let info = self.res.items().get(&stack.item); + let max_damage = info.map_or(0, |info| info.max_damage as i32); + let cooldown = + horizon_client::cooldowns::group(stack, info.and_then(|i| i.use_cooldown.as_ref())); + let partial = self.clock.fraction() as f32; let decorations = hud_overlays::ItemDecorations { count: stack.count, count_text, bar: hud_overlays::damage_bar(stack.damage as i32, max_damage), - cooldown: 0.0, + cooldown: self.cooldowns.percent(&cooldown, partial), }; let mut draws = vec![hud_layout::GuiDraw::Push(vec![ hud_layout::PoseOp::Translate(x, y), @@ -11880,6 +11920,7 @@ impl<'a> Game<'a> { offhand: None, hand: Hand::Main, shield_cooldown: 0, + cooldowns: Default::default(), command_source: None, titles: Default::default(), tool_highlight: Default::default(), @@ -12028,6 +12069,7 @@ impl<'a> Game<'a> { lid_openers: HashMap::new(), lan_acting: None, guest_packets: Vec::new(), + guest_cooldowns: Vec::new(), lan_acting_name: None, lan_acting_uuid: None, chat_capture: None, diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 5cc6d7db7..9b2044d62 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -44,9 +44,7 @@ fn arrow_damage(arrow: &mut Projectile, fallback: &mut impl RandomSource) -> f32 pub(super) struct Creatures { pub projectiles: Vec, /// Player status effects applied by mobs: (effect, ticks left, amplifier). - pub pearl_cooldown: u32, /// The wind charge's use cooldown (ticks). - pub wind_cooldown: u32, /// Wind bursts from a guest's turn, waiting for it to end (game/wind.rs). pub wind_bursts: Vec, /// Ticks left of the elder guardian's apparition (MobAppearanceParticle). @@ -642,7 +640,6 @@ impl Game<'_> { self.tick_allays(); self.tick_breeze_effects(); self.tick_sniffer_effects(); - self.tick_player_effects(); self.piglin_pickups(); self.sulfur_cube_pickups(); // Hurt and death sounds of damage dealt outside the mob ticks. @@ -1563,12 +1560,6 @@ impl Game<'_> { )); } /// Burning applied to the player by mobs. - fn tick_player_effects(&mut self) { - self.creatures.wind_cooldown = self.creatures.wind_cooldown.saturating_sub(1); - if self.creatures.pearl_cooldown > 0 { - self.creatures.pearl_cooldown -= 1; - } - } pub(super) fn hurt_player_by(&mut self, damage_type: &str, amount: f32) { if self.creative { return; @@ -2869,14 +2860,14 @@ impl Game<'_> { let kind = match item { "minecraft:snowball" => ProjectileKind::Snowball, "minecraft:egg" | "minecraft:blue_egg" | "minecraft:brown_egg" => ProjectileKind::Egg, - "minecraft:ender_pearl" => { - if self.creatures.pearl_cooldown > 0 { - return Ok(true); + "minecraft:ender_pearl" => ProjectileKind::EnderPearl, + "minecraft:wind_charge" => { + let thrown = self.throw_wind_charge()?; + if thrown { + self.apply_use_cooldown(stack); } - self.creatures.pearl_cooldown = 20; - ProjectileKind::EnderPearl + return Ok(thrown); } - "minecraft:wind_charge" => return self.throw_wind_charge(), _ if item.ends_with("_spawn_egg") => return self.use_spawn_egg(stack), _ => return Ok(false), }; @@ -2901,6 +2892,9 @@ impl Game<'_> { ); self.creatures.projectiles.push(proj); self.player_projectile_shot(); + // ItemStack.use's applyAfterUseComponentSideEffects: the pearl's + // second is the `use_cooldown` component, as for any thrown item. + self.apply_use_cooldown(stack); let sound = match kind { ProjectileKind::Snowball => "minecraft:entity.snowball.throw", ProjectileKind::Egg => "minecraft:entity.egg.throw", diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 02434d793..2b033ba2d 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -38,6 +38,8 @@ pub(super) struct GuestPlay { use_delay: u32, /// The shield's ItemCooldowns entry: an axe disabled it for this many ticks. pub(super) shield_cooldown: u32, + /// The guest's `Player.getCooldowns()` (`Game::cooldowns` for its turn). + pub(super) cooldowns: horizon_client::cooldowns::Cooldowns, attack_ticks: u32, /// The main hand's item at the guest's last tick (`Game::attack_item`). attack_item: Option>, @@ -125,6 +127,8 @@ pub(super) struct GuestSwap { using: Option<(usize, u32)>, use_delay: u32, pub(super) shield_cooldown: u32, + /// The guest's item cooldowns (`Game::cooldowns` for its turn). + cooldowns: horizon_client::cooldowns::Cooldowns, attack_ticks: u32, attack_item: Option>, swing: horizon_client::first_person::SwingState, @@ -316,6 +320,7 @@ impl Game<'_> { let using = std::mem::replace(&mut self.using, play.using.take()); let use_delay = std::mem::replace(&mut self.use_delay, play.use_delay); let shield_cooldown = std::mem::replace(&mut self.shield_cooldown, play.shield_cooldown); + let cooldowns = std::mem::replace(&mut self.cooldowns, std::mem::take(&mut play.cooldowns)); let attack_ticks = std::mem::replace(&mut self.attack_ticks, play.attack_ticks); let attack_item = std::mem::replace(&mut self.attack_item, play.attack_item.take()); let swing = std::mem::replace(&mut self.swing, play.swing); @@ -400,6 +405,7 @@ impl Game<'_> { using, use_delay, shield_cooldown, + cooldowns, attack_ticks, attack_item, swing, @@ -456,6 +462,7 @@ impl Game<'_> { using, use_delay, shield_cooldown, + cooldowns, attack_ticks, attack_item, swing, @@ -491,6 +498,16 @@ impl Game<'_> { for packet in std::mem::replace(&mut self.guest_packets, packets) { host.hub.send_to(conn, &packet); } + // ServerItemCooldowns.onCooldownStarted: the guest's own cooldown. + for (group, duration) in std::mem::take(&mut self.guest_cooldowns) { + host.hub.send_to( + conn, + &C::Cooldown(play::Cooldown { + group: group.to_string(), + duration, + }), + ); + } (self.lan_acting, self.lan_acting_name, self.lan_acting_uuid) = acting; play.death_cause = std::mem::replace(&mut self.ui.death_cause, death_cause); // An ender chest the guest opened: the menu shows their inventory @@ -520,6 +537,7 @@ impl Game<'_> { play.using = std::mem::replace(&mut self.using, using); play.use_delay = std::mem::replace(&mut self.use_delay, use_delay); play.shield_cooldown = std::mem::replace(&mut self.shield_cooldown, shield_cooldown); + play.cooldowns = std::mem::replace(&mut self.cooldowns, cooldowns); play.attack_ticks = std::mem::replace(&mut self.attack_ticks, attack_ticks); play.attack_item = std::mem::replace(&mut self.attack_item, attack_item); play.swing = std::mem::replace(&mut self.swing, swing); @@ -1783,8 +1801,19 @@ impl Game<'_> { } let play = host.play.entry(conn).or_default(); // Player.tick's ItemCooldowns.tick, once a tick for each player - // (the shield an axe disabled). + // (the shield an axe disabled, a thrown pearl). play.shield_cooldown = play.shield_cooldown.saturating_sub(1); + // ServerItemCooldowns.onCooldownEnded: a finished entry is told to + // the guest (a duration of 0 removes it). + for group in play.cooldowns.tick() { + host.hub.send_to( + conn, + &C::Cooldown(play::Cooldown { + group: group.to_string(), + duration: 0, + }), + ); + } let digging = play.digging; let using = play.using.is_some(); let delayed = play.destroy_delay > 0 || play.use_delay > 0 || play.attack_ticks < 100; diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index ba069522c..0f6bad14a 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -2561,6 +2561,16 @@ impl Game<'_> { r.commands = Some(tree); } } + C::Cooldown(p) => { + // ClientPacketListener.handleItemCooldown: the server starts + // (or confirms) a group's cooldown; 0 ends it. + let group: Arc = Arc::from(p.group.as_str()); + if p.duration == 0 { + self.cooldowns.remove(&group); + } else { + self.cooldowns.add(group, p.duration); + } + } C::SystemChat(p) => { // The component as sent, translated in the client's own // language. ChatListener.handleOverlay for the action bar. @@ -4303,8 +4313,12 @@ impl Game<'_> { let Some(info) = self.res.items().get(&held.item).cloned() else { return false; }; - // MultiPlayerGameMode.useItem runs the use here too: food starts - // being eaten (Consumable.startConsuming), a book and quill opens. + // MultiPlayerGameMode.useItem runs the use here too (inside + // startPrediction): the `use_cooldown` component starts its entry, and + // the server's ClientboundCooldownPacket confirms it. + self.apply_use_cooldown(&held); + // Food starts being eaten (Consumable.startConsuming), a book and + // quill opens. self.with_hand(hand, |g| g.start_eating(&info)); if self.with_hand(hand, |g| g.remote_use_book(&held)) { return true; diff --git a/crates/horizon-client/src/game/wind.rs b/crates/horizon-client/src/game/wind.rs index 0754bca8b..fb24102ba 100644 --- a/crates/horizon-client/src/game/wind.rs +++ b/crates/horizon-client/src/game/wind.rs @@ -52,10 +52,6 @@ fn triggers(name: &str) -> bool { impl Game<'_> { /// `WindChargeItem.use`. pub(super) fn throw_wind_charge(&mut self) -> Result { - if self.creatures.wind_cooldown > 0 { - return Ok(true); - } - self.creatures.wind_cooldown = 10; let (Some(eye), Some(p)) = (self.eye_position(), self.player.as_ref()) else { return Ok(false); }; diff --git a/crates/horizon-client/src/items.rs b/crates/horizon-client/src/items.rs index 95e7e2c42..8ac450bcb 100644 --- a/crates/horizon-client/src/items.rs +++ b/crates/horizon-client/src/items.rs @@ -77,6 +77,8 @@ pub struct ItemInfo { pub consumable: Option>, /// `use_remainder`: what a finished use leaves (`UseRemainder`). pub use_remainder: Option, + /// `use_cooldown`: the cooldown a use starts (`UseCooldown`). + pub use_cooldown: Option, pub model: String, /// Main-hand `attack_damage` / `attack_speed` modifier amounts. pub attack_damage: f32, @@ -568,6 +570,9 @@ impl Items { use_remainder: c["minecraft:use_remainder"]["id"] .as_str() .map(str::to_owned), + use_cooldown: c + .get("minecraft:use_cooldown") + .and_then(crate::cooldowns::UseCooldown::from_json), model: c["minecraft:item_model"] .as_str() .unwrap_or(name) diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index ae95e1bdb..722a4dab0 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -46,6 +46,7 @@ pub mod confirm_screen; pub mod container_screen; pub mod containers; pub mod control; +pub mod cooldowns; pub mod crafting; pub mod crash; pub mod creative; diff --git a/crates/horizon-protocol/src/packets/play.rs b/crates/horizon-protocol/src/packets/play.rs index 67373cf4b..9f7ec4c6a 100644 --- a/crates/horizon-protocol/src/packets/play.rs +++ b/crates/horizon-protocol/src/packets/play.rs @@ -176,6 +176,9 @@ packet!( } ); packet!(ContainerSetData { container: i32 => VarInt, id: i16 => Short, value: i16 => Short }); +// ClientboundCooldownPacket: the cooldown group (an identifier) and the ticks +// its entry lasts (0 ends it). +packet!(Cooldown { group: String => Ident, duration: i32 => VarInt }); packet!(ContainerSetSlot { container: i32 => VarInt, state: i32 => VarInt, slot: i16 => Short, item: Option => Slot }); /// A holder reference: registry id + 1, or 0 for an inline value (unsupported). pub struct HolderId; @@ -1761,6 +1764,7 @@ packet_enum!(Clientbound, crate::ids::play::clientbound { ContainerSetData(ContainerSetData) = CONTAINER_SET_DATA, ContainerSetSlot(ContainerSetSlot) = CONTAINER_SET_SLOT, CookieRequest(CookieRequest) = COOKIE_REQUEST, + Cooldown(Cooldown) = COOLDOWN, CustomPayload(CustomPayload) = CUSTOM_PAYLOAD, Disconnect(Disconnect) = DISCONNECT, DisguisedChat(DisguisedChat) = DISGUISED_CHAT, @@ -2894,6 +2898,24 @@ mod tests { round_trip(beacon, Serverbound::encode, Serverbound::decode); } + #[test] + fn a_cooldown_packet_carries_its_group_and_ticks() { + let packet = Clientbound::Cooldown(Cooldown { + group: "minecraft:ender_pearl".into(), + duration: 20, + }); + round_trip(packet, Clientbound::encode, Clientbound::decode); + // The group is an identifier, the ticks a VarInt; a duration of 0 + // removes the entry. + let body = Clientbound::encode(&Clientbound::Cooldown(Cooldown { + group: "minecraft:ender_pearl".into(), + duration: 0, + })); + assert_eq!(body[0], crate::ids::play::clientbound::COOLDOWN as u8); + assert_eq!(&body[1..3], [21, b'm']); + assert_eq!(body[body.len() - 1], 0); + } + #[test] fn change_game_mode_uses_the_game_type_varint() { for mode in 0..=3 { From 2761349d98199a2695e629bf2f911c2d0db7a6c8 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 21:50:49 +0100 Subject: [PATCH 173/238] Renderer: no indirect buffer usage without indirect draws (OpenGL before 4.3) wgpu refuses to create a buffer with INDIRECT usage on a device without DownlevelFlags::INDIRECT_EXECUTION (OpenGL before 4.3 without compute shaders), so the game panicked creating 'Section draws'. Multi-draw now also needs indirect execution, and without it the buffer has no INDIRECT usage: sections are drawn one direct draw each, as they already were without INDIRECT_FIRST_INSTANCE. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index baa8c5099..cd40ea22e 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -2182,15 +2182,26 @@ impl Renderer { ) }); let sorted_indices = arena::Arena::indices(&device, 1 << 16, flat.is_some()); + let multi_draw = device + .features() + .contains(wgpu::Features::INDIRECT_FIRST_INSTANCE) + && adapter + .get_downlevel_capabilities() + .flags + .contains(wgpu::DownlevelFlags::INDIRECT_EXECUTION); + // Without indirect draws (OpenGL before 4.3 without compute shaders) + // even creating an indirect buffer fails; one draw per section reads + // `draws` on the CPU instead. let indirect = device.create_buffer(&wgpu::BufferDescriptor { label: Some("Section draws"), size: 1024 * 20, - usage: wgpu::BufferUsages::INDIRECT | wgpu::BufferUsages::COPY_DST, + usage: if multi_draw { + wgpu::BufferUsages::INDIRECT | wgpu::BufferUsages::COPY_DST + } else { + wgpu::BufferUsages::COPY_DST + }, mapped_at_creation: false, }); - let multi_draw = device - .features() - .contains(wgpu::Features::INDIRECT_FIRST_INSTANCE); // HORIZON_GPU_CULL=0 turns occlusion culling off, `all` culls every // frame however few its draws and little it hides, `shader` tests in // the shader as on Metal (A/B checks). From 95c336a3870b003b6da5b7baae01d87e8cddee4b Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 25 Oct 2026 20:50:57 +0000 Subject: [PATCH 174/238] fix(threads): lower helper thread priority on Android Co-Authored-By: Codex (gpt-6.1-sol) --- crates/horizon-threads/Cargo.toml | 2 +- crates/horizon-threads/src/lib.rs | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/horizon-threads/Cargo.toml b/crates/horizon-threads/Cargo.toml index 3d7581fe9..d43b3e4fc 100644 --- a/crates/horizon-threads/Cargo.toml +++ b/crates/horizon-threads/Cargo.toml @@ -6,7 +6,7 @@ license.workspace = true rust-version = "1.85" description = "Scheduling hints for the game's threads: the OS calls the crates that forbid unsafe code can't make" -[target.'cfg(any(target_vendor = "apple", target_os = "linux"))'.dependencies] +[target.'cfg(any(target_vendor = "apple", target_os = "linux", target_os = "android"))'.dependencies] libc = "0.2" [target.'cfg(windows)'.dependencies] diff --git a/crates/horizon-threads/src/lib.rs b/crates/horizon-threads/src/lib.rs index ae4cc753f..7e9146ce7 100644 --- a/crates/horizon-threads/src/lib.rs +++ b/crates/horizon-threads/src/lib.rs @@ -45,8 +45,8 @@ fn favour() -> bool { /// behind the server thread and the render thread (`behind_server`). static HELPERS_BEHIND: AtomicBool = AtomicBool::new(false); -/// How many nice levels a helper thread goes below the process on Linux. -#[cfg(target_os = "linux")] +/// How many nice levels a helper thread goes below the process on Linux and Android. +#[cfg(any(target_os = "linux", target_os = "android"))] const BEHIND: i32 = 10; /// From now on, the helper threads that call `behind_server` as they start @@ -58,7 +58,7 @@ pub fn put_helpers_behind() { /// Called by a helper thread as it starts (world generation, lighting and /// meshing, a connection's packet writer): work no tick or frame waits for. -/// Once `put_helpers_behind` was called, on Linux, where the server and +/// Once `put_helpers_behind` was called, on Linux and Android, where the server and /// render threads can't be raised (`favour_current`), the helper lowers /// itself instead, which an unprivileged thread may: 10 nice levels below /// the process, or, where nice runs out (a game started under nice 10 or @@ -69,10 +69,10 @@ pub fn behind_server() -> bool { HELPERS_BEHIND.load(Ordering::Relaxed) && behind() } -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", target_os = "android"))] fn behind() -> bool { // SAFETY: these read and set the calling thread's own nice value or - // policy (who 0, pid 0 is the calling thread on Linux); `param` lives + // policy (who 0, pid 0 is the calling thread on Linux and Android); `param` lives // across the call. unsafe { let nice = libc::getpriority(libc::PRIO_PROCESS as _, 0); @@ -85,7 +85,7 @@ fn behind() -> bool { } } -#[cfg(not(target_os = "linux"))] +#[cfg(not(any(target_os = "linux", target_os = "android")))] fn behind() -> bool { false } @@ -117,12 +117,12 @@ mod tests { } /// A helper goes behind only once a dedicated server asks for it, and - /// then, on Linux, 10 nice levels below the process or in the idle class. + /// then, on Linux and Android, 10 nice levels below the process or in the idle class. #[test] fn helpers_go_behind_only_when_asked() { assert!(!std::thread::spawn(super::behind_server).join().unwrap()); super::put_helpers_behind(); - #[cfg(target_os = "linux")] + #[cfg(any(target_os = "linux", target_os = "android"))] std::thread::spawn(|| { // SAFETY: reads the calling thread's own nice value. let before = unsafe { libc::getpriority(libc::PRIO_PROCESS as _, 0) }; @@ -142,7 +142,7 @@ mod tests { }) .join() .unwrap(); - #[cfg(not(target_os = "linux"))] + #[cfg(not(any(target_os = "linux", target_os = "android")))] assert!(!std::thread::spawn(super::behind_server).join().unwrap()); } } From 8dcd00fc623d0c5c0e75843fe24839028ed15f96 Mon Sep 17 00:00:00 2001 From: socksprox Date: Sun, 25 Oct 2026 21:54:47 +0100 Subject: [PATCH 175/238] AGENTS.md: the feature-request loop When an agent is asked for a feature it runs the whole loop itself: check open PRs for the area first, work in a new worktree on the latest upstream main, build and run pr_ready, then push to the fork and open the pull request against upstream, never the fork. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- AGENTS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/AGENTS.md b/AGENTS.md index 2d883b33b..ed76c08dd 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -27,6 +27,10 @@ Feature → files → harness for the most-changed areas: [docs/architecture.md] change), none for a fix. Agents never add `release-now`: the maintainer decides it. Merges ship in batches of 5, or after 6 hours ([docs/release-flow.md](docs/release-flow.md)). - Contributors first: `gh pr list --state open --search ` before you start; never duplicate theirs. +- Asked for a feature? Run the whole loop: check open PRs for the area (above), work in a new worktree, + `git fetch` the upstream remote (`ludovic111/horizon`) and start from its latest `main`, build, run + `tools/pr_ready.py`, then push to your fork and open the pull request against upstream's `main` — + never against the fork itself. ## Crates From 820cc211d32d6bf0c7fa677a9c99a60f4cfc0aa8 Mon Sep 17 00:00:00 2001 From: socksprox Date: Sun, 25 Oct 2026 21:55:02 +0100 Subject: [PATCH 176/238] Player heads render their owner's skin Placed player_head blocks kept no profile: skulls had no block entity, so the renderer fell back to a hardcoded steve for every head. Give skulls a real BlockEntity carrying vanilla's SkullBlockEntity fields (profile, note_block_sound, custom_name) so the placement pipeline lands minecraft:profile in it the way block_entity_item already maps, encode and decode it through save so regions keep it, send its update tag to remote clients, and resolve the profile through the skin downloader the head items and the tab list share: the downloaded skin slot once it lands, the profile's DefaultPlayerSkin uuid pick while it fetches, SKIN_BY_TYPE steve without a profile. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../probe/block_entity_render_fixture.rs | 1 + .../horizon-client/src/block_entity_render.rs | 16 +++- crates/horizon-client/src/containers.rs | 24 +++++- crates/horizon-client/src/game.rs | 7 +- .../src/game/block_entity_anims.rs | 32 ++++++++ crates/horizon-client/src/game/minecarts.rs | 1 + crates/horizon-client/src/lan/chunks.rs | 14 ++++ crates/horizon-client/src/save.rs | 68 +++++++++++++++- crates/horizon-client/src/skins.rs | 77 +++++++++++++++++++ 9 files changed, 233 insertions(+), 7 deletions(-) diff --git a/crates/horizon-client/examples/probe/block_entity_render_fixture.rs b/crates/horizon-client/examples/probe/block_entity_render_fixture.rs index a0e3471bc..1db978b77 100644 --- a/crates/horizon-client/examples/probe/block_entity_render_fixture.rs +++ b/crates/horizon-client/examples/probe/block_entity_render_fixture.rs @@ -367,6 +367,7 @@ pub fn main() -> Result<()> { lid: &|_| open, sign: &|_| sign.as_ref(), banner: &|_| layers.clone(), + skull: &|_| None, glyph: &|c| atlas.glyph_cell(c), }; let mut verts = Vec::new(); diff --git a/crates/horizon-client/src/block_entity_render.rs b/crates/horizon-client/src/block_entity_render.rs index af4168307..a863d6214 100644 --- a/crates/horizon-client/src/block_entity_render.rs +++ b/crates/horizon-client/src/block_entity_render.rs @@ -730,6 +730,10 @@ pub struct Scene<'a> { pub sign: &'a dyn Fn([i32; 3]) -> Option<&'a crate::signs::SignData>, /// Banner layers at a position: pattern texture and RGB tint, bottom first. pub banner: &'a dyn Fn([i32; 3]) -> Vec<(String, u32)>, + /// The skin sprite a `player_head` shows at a position (`SkullBlockRenderer` + /// profile → skin): a downloaded `skins/` slot or the profile's + /// `DefaultPlayerSkin` fallback; None keeps `SKIN_BY_TYPE` (steve). + pub skull: &'a dyn Fn([i32; 3]) -> Option, /// Font glyph cell (none for blank glyphs such as the space) and advance. pub glyph: &'a dyn Fn(char) -> (Option, f32), } @@ -1277,7 +1281,16 @@ impl BlockEntityRenderer { SkullType::Creeper => ("creeper_head", "entity/creeper/creeper"), SkullType::Dragon => ("dragon_skull", "entity/enderdragon/dragon"), SkullType::Piglin => ("piglin_head", "entity/piglin/piglin"), - SkullType::Player => ("player_head", "entity/player/slim/steve"), + SkullType::Player => ("player_head", "entity/player/wide/steve"), + }; + // SkullBlockRenderer.getRenderType: the profile's skin when it + // resolves, `SKIN_BY_TYPE` (steve) otherwise. + let skin; + let texture = if *kind == SkullType::Player { + skin = (scene.skull)(pos); + skin.as_deref().unwrap_or(texture) + } else { + texture }; let (Some(model), Some(paint)) = (self.models.get(model), paint(texture, 0xFFFFFF, packed)) @@ -1886,6 +1899,7 @@ mod tests { lid: &|_| 0.0, sign: &|_| None, banner: &|_| Vec::new(), + skull: &|_| None, glyph: &|_| (None, 0.0), }; // Compare the cached world path with the original model renderer, diff --git a/crates/horizon-client/src/containers.rs b/crates/horizon-client/src/containers.rs index de2e1b2e9..7c9da6862 100644 --- a/crates/horizon-client/src/containers.rs +++ b/crates/horizon-client/src/containers.rs @@ -79,6 +79,17 @@ pub enum BlockEntity { Beehive(Box), /// `CommandBlockEntity` of the three command blocks (`crate::command_block`). CommandBlock(Box), + /// `SkullBlockEntity` of every skull and head block. + Skull(Skull), +} +/// `SkullBlockEntity`: the player profile the head shows (kept as loaded — +/// `SkullOwner` is a compound, or a string when it names a player only), +/// the note block sound and `custom_name`. +#[derive(Clone, Debug, Default)] +pub struct Skull { + pub profile: Option, + pub note_block_sound: Option, + pub custom_name: Option, } /// `CrafterBlockEntity`: nine slots that can be disabled, the powered flag /// and the ticks left in the crafting animation. @@ -233,6 +244,9 @@ impl BlockEntity { if name.ends_with("_banner") { return Some(Self::Banner(Banner::default())); } + if name.ends_with("_head") || name.ends_with("_skull") { + return Some(Self::Skull(Skull::default())); + } if name == "minecraft:beacon" { return Some(Self::Beacon(Box::new(Beacon { last_check_y: i32::MIN / 2, @@ -278,6 +292,7 @@ impl BlockEntity { Self::Vault(_) => "minecraft:vault", Self::Beehive(_) => "minecraft:beehive", Self::CommandBlock(_) => "minecraft:command_block", + Self::Skull(_) => "minecraft:skull", } } pub fn contents(&self) -> Vec { @@ -293,7 +308,8 @@ impl BlockEntity { | Self::TrialSpawner(_) | Self::Vault(_) | Self::Beehive(_) - | Self::CommandBlock(_) => Vec::new(), + | Self::CommandBlock(_) + | Self::Skull(_) => Vec::new(), Self::Lectern(l) => l.book.iter().cloned().collect(), Self::Sculk(_) => Vec::new(), Self::Banner(_) => Vec::new(), @@ -310,7 +326,8 @@ impl BlockEntity { | Self::Beacon(_) | Self::Lectern(_) | Self::Sculk(_) - | Self::Banner(_) => &[], + | Self::Banner(_) + | Self::Skull(_) => &[], Self::Crafter(c) => &c.items, Self::CreakingHeart(_) | Self::Spawner(_) @@ -330,7 +347,8 @@ impl BlockEntity { | Self::Beacon(_) | Self::Lectern(_) | Self::Sculk(_) - | Self::Banner(_) => &mut [], + | Self::Banner(_) + | Self::Skull(_) => &mut [], Self::Crafter(c) => &mut c.items, Self::CreakingHeart(_) | Self::Spawner(_) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 52d7ce8dd..bfdebe44a 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -5133,7 +5133,8 @@ impl Game<'_> { | BlockEntity::TrialSpawner(_) | BlockEntity::Vault(_) | BlockEntity::Beehive(_) - | BlockEntity::CommandBlock(_), + | BlockEntity::CommandBlock(_) + | BlockEntity::Skull(_), ) => {} None => { v.extend((0..self.craft_size * self.craft_size).map(|i| 100 + i)); @@ -5392,7 +5393,8 @@ impl Game<'_> { | BlockEntity::TrialSpawner(_) | BlockEntity::Vault(_) | BlockEntity::Beehive(_) - | BlockEntity::CommandBlock(_), + | BlockEntity::CommandBlock(_) + | BlockEntity::Skull(_), ) => {} None if self.mount_active() => self.draw_mount_background(batch, ox, oy), None => { @@ -6884,6 +6886,7 @@ impl Game<'_> { _ => None, }, banner: &|p| self.banner_layers_at(p), + skull: &|p| self.skull_skin_at(p), glyph: &|c| self.gui.atlas.glyph_cell(c), }; // Block entities outside the view frustum are skipped (with a diff --git a/crates/horizon-client/src/game/block_entity_anims.rs b/crates/horizon-client/src/game/block_entity_anims.rs index 619fcd009..136738350 100644 --- a/crates/horizon-client/src/game/block_entity_anims.rs +++ b/crates/horizon-client/src/game/block_entity_anims.rs @@ -8,8 +8,10 @@ use horizon_client::{ block_entity_anim::{BellEntity, EndGateway, Shulker, ShulkerStatus, Ticker}, block_entity_render::BlockAnim, collisions::{SceneCollisions, no_collision_empty}, + containers::BlockEntity, particles::{ParticleOptions, ParticleType}, save::number, + skins, }; use horizon_core::geometry::{Aabb, Vec3}; use horizon_world::nbt::Tag; @@ -392,4 +394,34 @@ impl Game<'_> { pub(super) fn chest_lid(&self, pos: [i32; 3], partial: f32) -> f32 { self.be_anims.lid(pos, partial) } + /// `SkullBlockRenderer.getPlayerSkin`: the skin sprite of the `profile` of + /// the player head at `pos`. The skin download is started here; while it + /// is in flight (or when it cannot be got) the profile's uuid picks the + /// `DefaultPlayerSkin` for it, and a profile with no id keeps steve. A + /// head with no profile answers None so `SKIN_BY_TYPE` applies. + pub(super) fn skull_skin_at(&self, pos: [i32; 3]) -> Option { + let key = (pos[0], pos[1], pos[2]); + let profile = match self.world.block_entities.get(&key) { + Some(BlockEntity::Skull(s)) => s.profile.clone(), + _ => None, + } + .or_else(|| self.saved_tag(pos).and_then(|t| t.get("profile").cloned())); + let profile = profile?; + if let Some(url) = skins::profile_skin_url_nbt(&profile) + && let Some(location) = + skins::texture_hash(&url).map(|hash| skins::skin_location(&hash)) + { + self.skin_downloads + .borrow_mut() + .state(skins::Kind::Skin, &url); + if self.gui.atlas.sprites.contains_key(&location) { + return Some(location); + } + } + Some( + skins::profile_uuid_nbt(&profile) + .map(|uuid| horizon_client::tab_list::default_skin(uuid).to_owned()) + .unwrap_or_else(|| "entity/player/wide/steve".to_owned()), + ) + } } diff --git a/crates/horizon-client/src/game/minecarts.rs b/crates/horizon-client/src/game/minecarts.rs index 6b8b51dde..8d4920b48 100644 --- a/crates/horizon-client/src/game/minecarts.rs +++ b/crates/horizon-client/src/game/minecarts.rs @@ -1463,6 +1463,7 @@ impl Game<'_> { lid: &lid, sign: &|_| None, banner: &|_| Vec::new(), + skull: &|_| None, glyph: &|c| self.gui.atlas.glyph_cell(c), }; self.res diff --git a/crates/horizon-client/src/lan/chunks.rs b/crates/horizon-client/src/lan/chunks.rs index 65b092881..d7dcc6cd8 100644 --- a/crates/horizon-client/src/lan/chunks.rs +++ b/crates/horizon-client/src/lan/chunks.rs @@ -937,6 +937,20 @@ pub fn entity_tag(entity: &BlockEntity) -> Option { match entity { BlockEntity::Sign(sign) => Some(Tag::compound(sign.encode())), BlockEntity::Spawner(s) => Some(s.update_tag()), + // SkullBlockEntity: the profile is the whole reason a head differs. + BlockEntity::Skull(s) => { + let mut entries = Vec::new(); + if let Some(profile) = &s.profile { + entries.push(("profile", profile.clone())); + } + if let Some(sound) = &s.note_block_sound { + entries.push(("note_block_sound", Tag::String(sound.as_str().into()))); + } + if let Some(name) = &s.custom_name { + entries.push(("custom_name", name.clone())); + } + Some(Tag::compound(entries)) + } BlockEntity::Jukebox(_) | BlockEntity::Lectern(_) => None, _ => None, } diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index da5735c17..c59c3c974 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -548,6 +548,17 @@ pub fn encode_block_entity( entries.push(("creaking", uuid_tag(uuid))); } } + BlockEntity::Skull(s) => { + if let Some(profile) = &s.profile { + entries.push(("profile", profile.clone())); + } + if let Some(sound) = &s.note_block_sound { + entries.push(("note_block_sound", string(sound))); + } + if let Some(name) = &s.custom_name { + entries.push(("custom_name", name.clone())); + } + } BlockEntity::Spawner(s) => entries.extend(s.save()), BlockEntity::TrialSpawner(s) => entries.extend(s.save()), BlockEntity::Vault(v) => entries.extend(v.save()), @@ -622,6 +633,19 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain }; return Some((pos, BlockEntity::Beacon(Box::new(beacon)))); } + // A skull's block entity id is not its block's name (every head block). + if id == "minecraft:skull" { + return Some(( + pos, + BlockEntity::Skull(crate::containers::Skull { + profile: tag.get("profile").cloned(), + note_block_sound: text(tag.get("note_block_sound")), + custom_name: tag + .get("custom_name") + .and_then(crate::block_entity_nbt::component), + }), + )); + } // A spawner's block entity id is not its block's name. let block = match id.as_str() { "minecraft:mob_spawner" => "minecraft:spawner", @@ -683,7 +707,8 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain | BlockEntity::Beacon(_) | BlockEntity::Lectern(_) | BlockEntity::Banner(_) - | BlockEntity::CreakingHeart(_) => {} + | BlockEntity::CreakingHeart(_) + | BlockEntity::Skull(_) => {} BlockEntity::Spawner(s) => **s = crate::mobs::spawner::Spawner::load(tag), BlockEntity::TrialSpawner(s) => **s = crate::mobs::trial_spawner::TrialSpawner::load(tag), BlockEntity::Vault(v) => **v = crate::vault::Vault::load(tag), @@ -3876,6 +3901,47 @@ mod tests { } use super::*; + #[test] + fn skull_block_entity_keeps_its_profile() { + // A placed `player_head` as vanilla saves it: the owning player's + // `profile` survives load, encode and `canonical`. + let saved = horizon_world::nbt::parse_snbt( + r#"{id:"minecraft:skull",x:10,y:64,z:20,profile:{name:"Steve",id:[I;1,2,3,4],properties:[{name:"textures",value:"abc"}]},note_block_sound:"minecraft:entity.player.hurt",custom_name:"Trophy"}"#, + ) + .unwrap() + .0; + let (pos, back) = decode_block_entity(&saved).expect("skull"); + assert_eq!(pos, (10, 64, 20)); + let crate::containers::BlockEntity::Skull(skull) = &back else { + panic!("not a skull"); + }; + assert_eq!(skull.profile, saved.get("profile").cloned()); + assert_eq!( + skull.note_block_sound.as_deref(), + Some("minecraft:entity.player.hurt") + ); + assert_eq!(back.id(), "minecraft:skull"); + let tag = encode_block_entity(pos, &back, "minecraft:player_head"); + assert_eq!( + crate::block_entity_nbt::id(&tag).as_deref(), + Some("minecraft:skull") + ); + assert_eq!(tag.get("profile"), saved.get("profile")); + let (_, again) = decode_block_entity(&tag).expect("skull again"); + let crate::containers::BlockEntity::Skull(again) = again else { + panic!("not a skull"); + }; + assert_eq!(again.profile, skull.profile); + // A profile-less head (skeleton, creeper) round-trips too. + let plain = encode_block_entity( + pos, + &crate::containers::BlockEntity::Skull(crate::containers::Skull::default()), + "minecraft:creeper_head", + ); + let (_, decoded) = decode_block_entity(&plain).expect("plain skull"); + assert!(matches!(decoded, crate::containers::BlockEntity::Skull(_))); + } + #[test] fn uuid_text_round_trips() { let u = [-158327916, 1572358581, -2125825222, -487948715]; diff --git a/crates/horizon-client/src/skins.rs b/crates/horizon-client/src/skins.rs index 9014032c4..ee6851e00 100644 --- a/crates/horizon-client/src/skins.rs +++ b/crates/horizon-client/src/skins.rs @@ -130,6 +130,47 @@ pub fn profile_skin_url(profile: &serde_json::Value) -> Option { unpack(value, Signature::Unsigned, &TEXTURE_URIS).skin } +/// `profile_skin_url` on a block entity's `profile` NBT (`SkullBlockEntity`): +/// the same `properties` list, held as a compound. A name-only profile (a +/// string tag) has no properties and no skin URL. +pub fn profile_skin_url_nbt(profile: &horizon_world::nbt::Tag) -> Option { + let properties = match profile.get("properties")? { + horizon_world::nbt::Tag::List(list) => list, + _ => return None, + }; + let value = properties.iter().find_map(|p| { + let name = nbt_text(p.get("name"))?; + (name == "textures") + .then(|| nbt_text(p.get("value"))) + .flatten() + })?; + unpack(&value, Signature::Unsigned, &TEXTURE_URIS).skin +} + +/// The profile's uuid as u128: `id` as an int array (block entity NBT) or a +/// uuid string (older and component forms). Used for `DefaultPlayerSkin.get` +/// when a head's skin is not downloaded yet. +pub fn profile_uuid_nbt(profile: &horizon_world::nbt::Tag) -> Option { + match profile.get("id")? { + horizon_world::nbt::Tag::IntArray(v) if v.len() == 4 => Some( + v.iter() + .fold(0u128, |a, &w| (a << 32) | u128::from(w as u32)), + ), + horizon_world::nbt::Tag::String(s) => { + let digits: String = s.to_string_lossy().chars().filter(|c| *c != '-').collect(); + u128::from_str_radix(&digits, 16).ok() + } + _ => None, + } +} + +fn nbt_text(tag: Option<&horizon_world::nbt::Tag>) -> Option { + match tag { + Some(horizon_world::nbt::Tag::String(s)) => Some(s.to_string_lossy()), + _ => None, + } +} + /// `MinecraftProfileTexture.getHash`: the base name of the URL's path. pub fn texture_hash(url: &str) -> Option { let rest = url.split_once("://")?.1; @@ -457,6 +498,42 @@ mod tests { use super::*; + #[test] + fn skull_block_entity_profiles_supply_head_skins() { + let value = "eyJ0ZXh0dXJlcyI6IHsiU0tJTiI6IHsidXJsIjogImh0dHBzOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlL2FiYzEyMyJ9fX0="; + let profile = horizon_world::nbt::parse_snbt(&format!( + r#"{{name:"T",id:[I;-158327916,1572358581,-2125825222,-487948715],properties:[{{name:"textures",value:"{value}"}}]}}"# + )) + .unwrap() + .0; + assert_eq!( + super::profile_skin_url_nbt(&profile).as_deref(), + Some("https://textures.minecraft.net/texture/abc123") + ); + assert_eq!( + super::profile_uuid_nbt(&profile), + Some( + [-158327916, 1572358581, -2125825222, -487948715] + .iter() + .fold(0u128, |a, &w: &i32| (a << 32) | u128::from(w as u32)) + ) + ); + // A name-only profile (a string tag) has no properties: no url, no id. + let named = horizon_world::nbt::Tag::String("MHF_Blaze".into()); + assert_eq!(super::profile_skin_url_nbt(&named), None); + assert_eq!(super::profile_uuid_nbt(&named), None); + // A uuid in string form decodes too. + let text_id = horizon_world::nbt::parse_snbt( + r#"{name:"T",id:"e843a1f4-0000-0001-0000-000200000003"}"#, + ) + .unwrap() + .0; + assert_eq!( + super::profile_uuid_nbt(&text_id), + Some(u128::from_str_radix("e843a1f4000000010000000200000003", 16).unwrap()) + ); + } + #[test] fn sha1_and_base64() { let hex: String = sha1(b"abc").iter().map(|b| format!("{b:02x}")).collect(); From 4a2b262343581332c58998410c8884c9d82c6558 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 22:00:07 +0100 Subject: [PATCH 177/238] Structure starts: decide chunks no ring search can reach without the rings The 128 stronghold biome searches (two seconds of CPU on a 4-thread laptop) ran when the first chunk of a new world looked up its structure starts, so nothing near spawn was generated until they were done. Each search ends within 8 chunks of its ring chunk, which is known before searching, and strongholds lie 1,400 blocks out or more: a chunk with no ring search in reach (exclusion zones included) is now decided without them, which is what they would say. They run when a chunk near a ring, /locate or an eye of ender needs them (ring_positions now searches on demand instead of returning nothing before a lookup did). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/vanillagen.rs | 7 +- .../horizon-world/src/worldgen/generator.rs | 59 +++++++++++-- .../src/worldgen/generator_structures.rs | 85 ++++++++++++++++--- .../src/worldgen/structure/start.rs | 12 +++ .../src/worldgen/structure/stronghold.rs | 15 +++- 5 files changed, 154 insertions(+), 24 deletions(-) diff --git a/crates/horizon-client/src/vanillagen.rs b/crates/horizon-client/src/vanillagen.rs index c44e1f542..a17bb22d3 100644 --- a/crates/horizon-client/src/vanillagen.rs +++ b/crates/horizon-client/src/vanillagen.rs @@ -272,9 +272,12 @@ impl VanillaTerrain { pub fn origin(&self) -> crate::Result { Ok(self.generator.origin()?) } - /// Concentric ring positions of a structure set (strongholds), once known. + /// Concentric ring positions of a structure set (strongholds), searched + /// for now if no chunk needed them yet. pub fn ring_positions(&self, set: &str) -> Option> { - self.generator.ring_positions(set) + let mut worker = self.query.lock().unwrap_or_else(|e| e.into_inner()); + let worker = worker.get_or_insert_with(|| self.generator.worker()); + worker.ring_positions(set) } } /// The worldgen bundle at `dir`, loaded once and shared by every generator diff --git a/crates/horizon-world/src/worldgen/generator.rs b/crates/horizon-world/src/worldgen/generator.rs index 2595173ad..b2daae61c 100644 --- a/crates/horizon-world/src/worldgen/generator.rs +++ b/crates/horizon-world/src/worldgen/generator.rs @@ -1652,7 +1652,7 @@ trait SeededPipeline: Send + Sync { ) -> Vec>; fn unsupported_features(&self) -> Vec<(String, String)>; fn forget_structure_starts(&self); - fn ring_positions(&self, set: &str) -> Option>; + fn ring_positions(&self, worker: &mut WorkerState, set: &str) -> Option>; fn spawn_position(&self) -> Result<[i32; 2], GeneratorError>; fn locate( &self, @@ -1752,8 +1752,15 @@ where structures.forget_starts(); } } - fn ring_positions(&self, set: &str) -> Option> { - self.structures.as_ref()?.cached_rings(set) + fn ring_positions(&self, worker: &mut WorkerState, set: &str) -> Option> { + self.structures.as_ref()?.ring_positions( + &mut StructureQueries { + pipeline: self, + worker, + valid: BTreeSet::new(), + }, + set, + ) } fn spawn_position(&self) -> Result<[i32; 2], GeneratorError> { if let Some(position) = self.spawn_position.get() { @@ -2375,11 +2382,6 @@ impl OverworldGenerator { pub fn unsupported_features(&self) -> Vec<(String, String)> { self.pipeline.unsupported_features() } - /// ConcentricRingsStructurePlacement chunks of a structure set (such as - /// `minecraft:strongholds`), available once structure starts were looked up. - pub fn ring_positions(&self, set: &str) -> Option> { - self.pipeline.ring_positions(set) - } /// NoiseBasedChunkGenerator.getOrigin: the chunk a new world's spawn /// search starts from (NoiseSpawnFinder over the `spawn_target` points). pub fn origin(&self) -> Result { @@ -2487,6 +2489,11 @@ impl GeneratorWorker { pub fn jigsaw_pieces(&mut self, query: &JigsawQuery<'_>) -> Option { self.pipeline.jigsaw_pieces(&mut self.state, query) } + /// ConcentricRingsStructurePlacement chunks of a structure set (such as + /// `minecraft:strongholds`), searched for now if nothing needed them yet. + pub fn ring_positions(&mut self, set: &str) -> Option> { + self.pipeline.ring_positions(&mut self.state, set) + } /// STRUCTURE_STARTS: the structure starts owned by a chunk (cached). pub fn structure_starts( &mut self, @@ -2887,8 +2894,8 @@ mod tests { return; }; let generator = OverworldGenerator::new(data, 1234).expect("generator"); - generator.worker().structure_starts(ChunkPos::new(0, 0)); let rings = generator + .worker() .ring_positions("minecraft:strongholds") .expect("ring positions"); assert_eq!(rings.len(), 128); @@ -2902,6 +2909,40 @@ mod tests { } } + /// Chunks no ring search can end near are decided without the rings, + /// which stay unsearched, and get the starts they get with them. + #[test] + fn chunks_far_from_the_rings_do_not_search_them() { + let Some(data) = prepared_data() else { + return; + }; + let generator = OverworldGenerator::new(data.clone(), 4321).expect("generator"); + let mut worker = generator.worker(); + let summary = |worker: &mut GeneratorWorker, c: ChunkPos| -> Vec<(String, i32, i32)> { + worker + .structure_starts(c) + .iter() + .map(|s| (s.name.clone(), s.chunk.x, s.chunk.z)) + .collect() + }; + let chunks: Vec = (-24..=24) + .step_by(3) + .flat_map(|x| (-24..=24).step_by(3).map(move |z| ChunkPos::new(x, z))) + .collect(); + let before: Vec<_> = chunks.iter().map(|&c| summary(&mut worker, c)).collect(); + assert!(before.iter().any(|s| !s.is_empty()), "no starts to compare"); + let searched = || data.ring_cache.lock().unwrap()[0].1.get().is_some(); + assert!(!searched()); + let rings = worker + .ring_positions("minecraft:strongholds") + .expect("ring positions"); + assert_eq!(rings.len(), 128); + assert!(searched()); + worker.forget_structure_starts(); + let after: Vec<_> = chunks.iter().map(|&c| summary(&mut worker, c)).collect(); + assert_eq!(before, after); + } + /// Generators of one seed and dimension share their ring positions; /// another seed or generator gets its own, and old entries go. #[test] diff --git a/crates/horizon-world/src/worldgen/generator_structures.rs b/crates/horizon-world/src/worldgen/generator_structures.rs index 1e4913c95..8f8817f11 100644 --- a/crates/horizon-world/src/worldgen/generator_structures.rs +++ b/crates/horizon-world/src/worldgen/generator_structures.rs @@ -37,6 +37,8 @@ pub(super) struct StructureState { rings: Arc>, /// Their biome searches while they are computed. ring_work: OnceLock, + /// `StructureRegistry::zone_reach`. + zone_reach: i32, } /// generateRingPositions' biome searches, shared out. Vanilla runs each as @@ -209,7 +211,9 @@ impl StructureState { { bindings = bindings.with_block_properties(&blocks.properties)?; } + let zone_reach = registry.zone_reach(); Ok(Some(Self { + zone_reach, registry, resources: resources.clone(), bindings, @@ -221,10 +225,22 @@ impl StructureState { ring_work: OnceLock::new(), })) } - /// Concentric ring positions of a structure set, once the first start - /// lookup has computed them. - pub(super) fn cached_rings(&self, set: &str) -> Option> { - self.rings.get().and_then(|rings| rings.get(set).cloned()) + /// Concentric ring positions of a structure set (vanilla's + /// getRingPositionsFor), computed now if no lookup needed them yet. + pub(super) fn ring_positions( + &self, + context: &mut impl StartContext, + set: &str, + ) -> Option> { + self.rings(context).get(set).cloned() + } + fn ring_work(&self, context: &impl StartContext) -> &RingWork { + self.ring_work.get_or_init(|| { + RingWork::new( + self.registry + .ring_searches(&self.possible_sets, context.seed()), + ) + }) } /// The concentric ring positions, computing them with the threads that /// need them first (see `RingWork`). @@ -232,12 +248,7 @@ impl StructureState { if let Some(rings) = self.rings.get() { return rings; } - let work = self.ring_work.get_or_init(|| { - RingWork::new( - self.registry - .ring_searches(&self.possible_sets, context.seed()), - ) - }); + let work = self.ring_work(context); let registry = &self.registry; work.help(|set| registry.ring_biomes(set), &mut |quarts| { context.noise_biomes(quarts) @@ -245,6 +256,24 @@ impl StructureState { self.rings .get_or_init(|| work.rings(|set| registry.sets[set].name.clone())) } + /// The rings a chunk's starts are decided with. Before they are known, + /// a chunk no ring search can end near (with the exclusion zones' reach) + /// is decided without them: no ring chunk is near it, which is what the + /// rings would say. Strongholds lie 1,400 blocks out or more, so a new + /// world's first chunks don't wait for the 128 biome searches (seconds + /// on four threads); they run when a chunk near a ring needs them. + fn rings_for(&self, context: &mut impl StartContext, chunk: ChunkPos) -> &Rings { + static NONE: Rings = Rings::new(); + if let Some(rings) = self.rings.get() { + return rings; + } + let near = self + .ring_work(context) + .searches + .iter() + .any(|(_, search)| search.may_land_near(chunk.x, chunk.z, self.zone_reach)); + if near { self.rings(context) } else { &NONE } + } /// Drop the cached starts, so that later chunks build fresh pieces (the /// pieces keep per-start placement state such as a shipwreck's height). pub(super) fn forget_starts(&self) { @@ -263,7 +292,7 @@ impl StructureState { } // Ring searches enlist other workers and wait for them: never hold a // chunk-work lock while helping, even when this chunk has no starts. - let rings = self.rings(context); + let rings = self.rings_for(context, chunk); let work = self.start_work.at((chunk.x, chunk.z)); let _work = work.lock().unwrap(); if let Some(found) = self.starts.get((chunk.x, chunk.z)) { @@ -470,6 +499,40 @@ mod tests { assert!(cache.get((3, -4)).is_none()); } + /// A ring search ends where `may_land_near` allows: on its ring chunk + /// or in the square it samples, never further. + #[test] + fn ring_searches_end_where_they_may() { + let none = BTreeSet::new(); + for seed in [0, 1234, -77, i64::MAX] { + for (b, search) in ring_searches(seed, 32, 3, 128).iter().enumerate() { + // Preferred biomes on one quart at a time, in turn at each + // corner of the square, else nowhere (the ring chunk). + let corner = (b % 5) as i32; + let biomes = |quarts: &[[i32; 3]]| -> Vec { + let (first, last) = (quarts[0], quarts[quarts.len() - 1]); + let pick = match corner { + 0 => first, + 1 => [last[0], 0, first[2]], + 2 => [first[0], 0, last[2]], + 3 => last, + _ => [i32::MIN, 0, i32::MIN], + }; + quarts + .iter() + .map(|q| BiomeId(u32::from(*q == pick))) + .collect() + }; + let preferred: BTreeSet = [BiomeId(1)].into(); + for wanted in [&preferred, &none] { + let found = search.find(wanted, &mut { biomes }); + assert!(search.may_land_near(found.x, found.z, 0)); + assert!(!search.may_land_near(found.x + 17, found.z, 0)); + } + } + } + } + /// Searches shared between threads give the positions of one thread /// running them in order, set by set. #[test] diff --git a/crates/horizon-world/src/worldgen/structure/start.rs b/crates/horizon-world/src/worldgen/structure/start.rs index 44f4b70f4..7919751aa 100644 --- a/crates/horizon-world/src/worldgen/structure/start.rs +++ b/crates/horizon-world/src/worldgen/structure/start.rs @@ -511,6 +511,18 @@ impl StructureRegistry { }) .collect() } + /// How far from a chunk, in chunks, deciding its starts can look at + /// other sets' start chunks through exclusion zones: every set's zones + /// summed, a bound on any chain of them. + pub fn zone_reach(&self) -> i32 { + self.sets + .iter() + .map(|s| { + s.exclusion.as_ref().map_or(0, |e| e.1.max(0)) + + s.avoid.as_ref().map_or(0, |a| a.chunk_count.max(0)) + }) + .sum() + } /// The preferred biomes of a concentric-ring set (empty for others). pub fn ring_biomes(&self, set: usize) -> &BTreeSet { static NONE: BTreeSet = BTreeSet::new(); diff --git a/crates/horizon-world/src/worldgen/structure/stronghold.rs b/crates/horizon-world/src/worldgen/structure/stronghold.rs index e1a10808f..94042ba6f 100644 --- a/crates/horizon-world/src/worldgen/structure/stronghold.rs +++ b/crates/horizon-world/src/worldgen/structure/stronghold.rs @@ -29,6 +29,9 @@ pub fn ring_positions( .collect() } +/// findBiomeHorizontal's radius, 112 blocks, in quarts. +const SEARCH_QUARTS: i32 = 28; + /// One ring position before its biome search: the chunk on the ring and the /// random its search uses. Vanilla runs each search as its own task. #[derive(Clone, Debug)] @@ -38,6 +41,13 @@ pub struct RingSearch { random: LegacyRandom, } impl RingSearch { + /// Whether the search can end within `margin` chunks of chunk (`x`, + /// `z`): it ends on its ring chunk or in the square it samples, at most + /// 120 blocks from the ring chunk's corner (8 chunks, rounded up). + pub fn may_land_near(&self, x: i32, z: i32, margin: i32) -> bool { + let reach = (SEARCH_QUARTS * 4 + 8 + 15) / 16 + margin; + (self.x - x).abs() <= reach && (self.z - z).abs() <= reach + } /// BiomeSource.findBiomeHorizontal around the ring chunk's centre: a /// random preferred biome within 112 blocks, else the ring chunk itself. /// @@ -53,8 +63,9 @@ impl RingSearch { ) -> ChunkPos { let mut search = self.random.clone(); let (cx, cz) = ((self.x * 16 + 8) >> 2, (self.z * 16 + 8) >> 2); - let quarts: Vec<[i32; 3]> = (-28..=28) - .flat_map(|dz| (-28..=28).map(move |dx| [cx + dx, 0, cz + dz])) + let r = SEARCH_QUARTS; + let quarts: Vec<[i32; 3]> = (-r..=r) + .flat_map(|dz| (-r..=r).map(move |dx| [cx + dx, 0, cz + dz])) .collect(); let mut found = 0; let mut result = None; From 2fb0ac186a65b1171d0893c409e040dd094a95b6 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 22:04:13 +0100 Subject: [PATCH 178/238] Behaviour cache: hash the source files once per process The snapshot's key hashes the 70 MB of source tables it was resolved from. The client's tables and the world generator's are loaded from the same folder, so a world opening hashed them a second time: a third of a second of the server thread's start on a 4-thread laptop, before any chunk. The hash is now kept for the process, by each file's length and modification time; a changed file is read again. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-world/src/level/data/cache.rs | 96 ++++++++++++++++---- 1 file changed, 78 insertions(+), 18 deletions(-) diff --git a/crates/horizon-world/src/level/data/cache.rs b/crates/horizon-world/src/level/data/cache.rs index 59bc1f8d8..54682ab8c 100644 --- a/crates/horizon-world/src/level/data/cache.rs +++ b/crates/horizon-world/src/level/data/cache.rs @@ -13,10 +13,10 @@ use std::{ io::Write, path::PathBuf, sync::{ - OnceLock, + Mutex, OnceLock, atomic::{AtomicU64, Ordering}, }, - time::UNIX_EPOCH, + time::{SystemTime, UNIX_EPOCH}, }; const MAGIC: &[u8; 8] = b"HZBEHAV\0"; @@ -149,22 +149,7 @@ fn fingerprint(registry: &BlockRegistry, root: &Path) -> io::Result<[u8; 32]> { .duration_since(UNIX_EPOCH) .map_err(|e| invalid(e.to_string()))?; hasher.update(modified.as_nanos().to_le_bytes()); - for relative in [ - "facts.json", - "block-behaviours.json", - "gameplay-facts.json", - "block-tags.json", - "cached-collision.nbt", - ] { - hash_file(&mut hasher, root, &root.join(relative))?; - } - let mut fluids: Vec = fs::read_dir(root.join("data/minecraft/tags/fluid"))? - .map(|entry| entry.map(|entry| entry.path())) - .collect::>()?; - fluids.sort(); - for path in fluids { - hash_file(&mut hasher, root, &path)?; - } + hasher.update(sources_digest(root)?); // The registry argument may be modified independently of facts.json. hasher.update((registry.len() as u64).to_le_bytes()); let mut seen = std::collections::HashSet::new(); @@ -190,6 +175,55 @@ fn fingerprint(registry: &BlockRegistry, root: &Path) -> io::Result<[u8; 32]> { Ok(hasher.finalize().into()) } +/// The source files the snapshot is resolved from, as their lengths and +/// modification times, and the hash of their contents. +type Sources = (Vec<(PathBuf, u64, Option)>, [u8; 32]); +/// Source hashes already computed by this process: the client's tables and +/// the world generator's are loaded from the same folder, and hashing its +/// 70 MB again took a third of a second on a 4-thread laptop. A file whose +/// length or modification time changed is read again. +static SOURCES: Mutex> = Mutex::new(Vec::new()); + +fn sources_digest(root: &Path) -> io::Result<[u8; 32]> { + let mut paths: Vec = [ + "facts.json", + "block-behaviours.json", + "gameplay-facts.json", + "block-tags.json", + "cached-collision.nbt", + ] + .iter() + .map(|relative| root.join(relative)) + .collect(); + let mut fluids: Vec = fs::read_dir(root.join("data/minecraft/tags/fluid"))? + .map(|entry| entry.map(|entry| entry.path())) + .collect::>()?; + fluids.sort(); + paths.extend(fluids); + let stamps = paths + .iter() + .map(|path| { + let meta = fs::metadata(path)?; + Ok((path.clone(), meta.len(), meta.modified().ok())) + }) + .collect::>>()?; + let known = SOURCES.lock().unwrap_or_else(|e| e.into_inner()); + if let Some((_, digest)) = known.iter().find(|(s, _)| *s == stamps) { + return Ok(*digest); + } + drop(known); + let mut hasher = Sha256::new(); + for path in &paths { + hash_file(&mut hasher, root, path)?; + } + let digest: [u8; 32] = hasher.finalize().into(); + SOURCES + .lock() + .unwrap_or_else(|e| e.into_inner()) + .push((stamps, digest)); + Ok(digest) +} + fn hash_str(hasher: &mut Sha256, text: &str) { hasher.update((text.len() as u64).to_le_bytes()); hasher.update(text.as_bytes()); @@ -806,6 +840,32 @@ mod tests { ); } + /// A folder's sources are hashed once while they stay as they are; a + /// file rewritten at the same length is hashed again. + #[test] + fn sources_are_hashed_once_until_a_file_changes() { + let Some(root) = fixture() else { + return; + }; + let entries = || { + SOURCES + .lock() + .unwrap() + .iter() + .filter(|(stamps, _)| stamps[0].0.starts_with(&root.0)) + .count() + }; + let first = sources_digest(&root.0).unwrap(); + assert_eq!(sources_digest(&root.0).unwrap(), first); + assert_eq!(entries(), 1); + let tags = root.0.join("block-tags.json"); + let mut bytes = fs::read(&tags).unwrap(); + bytes[0] ^= 1; + fs::write(&tags, bytes).unwrap(); + assert_ne!(sources_digest(&root.0).unwrap(), first); + assert_eq!(entries(), 2); + } + #[test] fn cache_invalidates_sources_and_recovers_from_corruption() { let Some(root) = fixture() else { From 40b3c1e3d68bf0c96d9eeb5b7c75cd207d0cf51d Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 25 Oct 2026 21:08:55 +0000 Subject: [PATCH 179/238] Preserve requested GUI scale across temporary small surfaces Keep framebuffer limits in rendering and explicit option cycling rather than overwriting the saved choice during startup or settings application. Co-Authored-By: Codex gpt-6.1-sol --- crates/horizon-client/src/game.rs | 10 ------ .../horizon-client/src/game/startup_menu.rs | 2 -- crates/horizon-client/src/options_ui.rs | 3 -- crates/horizon-client/src/settings.rs | 33 +++++++++++++++---- 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index f5803ad08..7b69a2ee3 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -9194,10 +9194,6 @@ impl Game<'_> { ) } fn apply_settings(&mut self) { - if let Some(r) = &self.renderer { - self.settings - .clamp_gui_scale([r.config.width as i32, r.config.height as i32]); - } self.streaming.radius = self.settings.render_distance; // Options.biomeBlendRadius, ambientOcclusion and cutoutLeaves: // LevelExtractor.allChanged when they change. @@ -13094,10 +13090,6 @@ impl Loading { return; } } - if let Some(r) = &self.renderer { - self.settings - .clamp_gui_scale([r.config.width as i32, r.config.height as i32]); - } horizon_client::hud::GUI_SCALE.store(self.settings.gui_scale, Ordering::Relaxed); self.window = Some(window); } @@ -13136,8 +13128,6 @@ impl Loading { self.settings.horizon.api, ) { Ok(r) => { - self.settings - .clamp_gui_scale([r.config.width as i32, r.config.height as i32]); horizon_client::hud::GUI_SCALE.store(self.settings.gui_scale, Ordering::Relaxed); self.renderer = Some(r); } diff --git a/crates/horizon-client/src/game/startup_menu.rs b/crates/horizon-client/src/game/startup_menu.rs index a5d658a4d..f7be6241f 100644 --- a/crates/horizon-client/src/game/startup_menu.rs +++ b/crates/horizon-client/src/game/startup_menu.rs @@ -272,8 +272,6 @@ impl Loading { } pub(super) fn apply_menu_settings(&mut self) { if let Some(renderer) = &mut self.renderer { - self.settings - .clamp_gui_scale([renderer.config.width as i32, renderer.config.height as i32]); renderer.gamma = self.settings.gamma; renderer.set_present_mode(self.settings.vsync, self.settings.horizon.present_mode); } diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index 1d1f099eb..9f91254bd 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -3403,9 +3403,6 @@ mod tests { assert_eq!(s.gui_scale, expected); } s.gui_scale = u32::MAX; - s.clamp_gui_scale(framebuffer); - assert_eq!(s.gui_scale, max); - s.gui_scale = u32::MAX; Opt::GuiScale.cycle(&mut s, false, framebuffer); assert_eq!(s.gui_scale, 0); s.gui_scale = u32::MAX; diff --git a/crates/horizon-client/src/settings.rs b/crates/horizon-client/src/settings.rs index 5fc09e35b..2a53e1702 100644 --- a/crates/horizon-client/src/settings.rs +++ b/crates/horizon-client/src/settings.rs @@ -104,7 +104,7 @@ pub struct Settings { pub master_volume: f32, /// Volumes of every other sound source, indexed like SOUND_SOURCES. pub volumes: [f32; 11], - /// 0 = auto. + /// Requested scale (0 = auto); rendering caps it to the current framebuffer. pub gui_scale: u32, pub view_bobbing: bool, /// Brightness 0..1 (gamma). @@ -284,12 +284,6 @@ fn put_s(f: &mut OptionsFile, key: &str, v: &str) { } } impl Settings { - /// `ClampingLazyMaxIntRange.validateValue`: clamp once the framebuffer is known. - pub fn clamp_gui_scale(&mut self, framebuffer: [i32; 2]) { - let max = crate::window::calculate_scale(framebuffer, 0, self.force_unicode) as u32; - self.gui_scale = self.gui_scale.min(max); - } - pub fn load(path: &Path) -> Self { let horizon = path .parent() @@ -597,6 +591,31 @@ impl Settings { #[cfg(test)] mod tests { use super::*; + #[test] + fn gui_scale_survives_temporary_small_framebuffer() { + for requested in [0, 1, 2, 6] { + let settings = Settings::parse(&format!("guiScale:{requested}\n")); + // Hyprland's initial surface can be smaller than the fullscreen one. + for (framebuffer, maximum) in [([715, 860], 2), ([3400, 1913], 7)] { + let effective = crate::window::calculate_scale( + framebuffer, + settings.gui_scale as i32, + settings.force_unicode, + ); + assert_eq!( + effective, + if requested == 0 { + maximum + } else { + (requested as i32).min(maximum) + } + ); + let saved = Settings::parse(&settings.serialize()); + assert_eq!(saved.gui_scale, requested); + } + } + } + #[test] fn round_trips_vanilla_options_txt() { let text = "version:4000\nfov:0.5\nrenderDistance:16\nmaxFps:260\nrenderClouds:\"fast\"\nkey_key.jump:key.keyboard.j\nsoundCategory_music:0.25\nresourcePacks:[\"vanilla\",\"file/x.zip\"]\nlang:fr_fr\nsomeFutureOption:42\n"; From fada660310d4dcc8b407219efef987a040b3805c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 22:10:44 +0100 Subject: [PATCH 180/238] Mod API: capability input (experimental): claimed items' use, mod keys - WIT `input`: claim-items(ids) makes the use button on those items the mod's: use-start, then use-stop with the ticks held (button up, item out of the hand, or a screen opening); the game does nothing else with the click. register-key(name, label, key) for up to 16 keys (key.keyboard.r), heard as key-down / key-up while playing; the game's own mapping on the key still runs. held(), poll() (256 kept). - game/mod_input.rs: play_mouse asks mod_use first (local and remote windows), the tick ends a use whose item left the hand, the keyboard and horizon-ctl's key send the physical key to mod_key. - No claim and no key, no cost: one check each. - Experimental: the manifest needs experimental = ["input"]. - The ui test guest gains /input_claim, /input_poll and /input_held, with a sandbox test. Docs: mods.md, stability.md, the changelog, the regenerated reference. Co-Authored-By: Claude Opus 5.5 --- crates/cargo-horizon-mod/src/check.rs | 1 + crates/horizon-client/src/game.rs | 6 + crates/horizon-client/src/game/control.rs | 5 + crates/horizon-client/src/game/mod_input.rs | 92 +++++++++++ crates/horizon-client/src/game/mods.rs | 5 + crates/horizon-mod-sdk/src/lib.rs | 2 +- crates/horizon-mod-sdk/wit/horizon.wit | 44 +++++ crates/horizon-mods/src/manifest.rs | 14 +- crates/horizon-mods/src/runtime.rs | 153 ++++++++++++++++++ crates/horizon-mods/tests/sandbox.rs | 67 +++++++- crates/horizon-mods/tests/ui_guest/src/lib.rs | 30 +++- docs/api/mods-reference.md | 44 ++++- docs/api/mods.md | 2 + docs/api/stability.md | 1 + docs/mods.md | 3 +- 15 files changed, 459 insertions(+), 10 deletions(-) create mode 100644 crates/horizon-client/src/game/mod_input.rs diff --git a/crates/cargo-horizon-mod/src/check.rs b/crates/cargo-horizon-mod/src/check.rs index dfb9820a2..87ae3c982 100644 --- a/crates/cargo-horizon-mod/src/check.rs +++ b/crates/cargo-horizon-mod/src/check.rs @@ -200,6 +200,7 @@ fn play(path: &Path, report: &mut Report) -> Result<(), String> { }), // The GUI of a 1280 x 720 window at scale 3. gui_size: (427.0, 240.0), + held: None, }); mods.tick(t); for _ in 0..FRAMES_PER_TICK { diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 31bfe74b6..5b04d8716 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -96,6 +96,7 @@ mod maps; mod minecarts; mod mob_blocks; mod mob_gear; +mod mod_input; mod mod_render; mod mods; mod mount_menu; @@ -10479,6 +10480,10 @@ impl Game<'_> { } /// A mouse button while playing (window or control socket). fn play_mouse(&mut self, button: MouseButton, pressed: bool) -> Result<()> { + // A mod's claimed item takes the use button (capability `input`). + if self.mod_use(button, pressed) { + return Ok(()); + } if self.sleeping.is_some() { return Ok(()); } @@ -11385,6 +11390,7 @@ impl ApplicationHandler for Game<'_> { return Ok(()); }; self.extras_key(key, event.state == ElementState::Pressed, event.repeat); + self.mod_key(key, event.state == ElementState::Pressed, event.repeat); // key.smoothCamera toggles the cinematic camera. if self.screen == Screen::Playing && event.state == ElementState::Pressed diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index ffe05b518..89ce7c30e 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -294,6 +294,11 @@ impl Game<'_> { if action == "tap" { self.extras_key(physical, false, false); } + // So do mods' keys (capability `input`). + self.mod_key(physical, action != "up", action == "repeat"); + if action == "tap" { + self.mod_key(physical, false, false); + } // Text screens consume raw presses; releases follow key binds. let typing = matches!(self.screen, Screen::Chat | Screen::Options); let key = if typing && arg(2)? != "up" { diff --git a/crates/horizon-client/src/game/mod_input.rs b/crates/horizon-client/src/game/mod_input.rs new file mode 100644 index 000000000..ac80e8e42 --- /dev/null +++ b/crates/horizon-client/src/game/mod_input.rs @@ -0,0 +1,92 @@ +//! Capability `input` (docs/mods.md): the use button on items a mod claims, +//! and the mod's own keys. With no mod claiming items or keys, each hook +//! here returns after one check. +use super::Game; +use winit::{event::MouseButton, keyboard::KeyCode}; +#[cfg(feature = "mods")] +use {super::Screen, horizon_mods::HeldItem}; + +/// A claimed item's use while the button is down. +#[cfg(feature = "mods")] +pub(super) struct ModUse { + item: HeldItem, + /// `Game::ticks` when it started. + since: u64, +} + +#[cfg(not(feature = "mods"))] +impl Game<'_> { + pub(super) fn mod_use(&mut self, _button: MouseButton, _pressed: bool) -> bool { + false + } + pub(super) fn mod_key(&mut self, _key: KeyCode, _pressed: bool, _repeat: bool) {} +} + +#[cfg(feature = "mods")] +impl Game<'_> { + /// The main hand's stack as `input` shows it; none when it is empty. + pub(super) fn mod_held(&self) -> Option { + let stack = self.inventory.get(self.selected)?.as_ref()?; + Some(HeldItem { + id: stack.item.to_string(), + count: stack.count, + slot: self.selected as u32, + }) + } + /// The use button while playing: true when a mod claimed the held item, + /// so the game does nothing else with it (no placing, eating, swing). + pub(super) fn mod_use(&mut self, button: MouseButton, pressed: bool) -> bool { + if button != MouseButton::Right || !self.mods.mods.claims_any() { + return false; + } + if !pressed { + return self.stop_mod_use(); + } + let Some(item) = self.mod_held().filter(|h| self.mods.mods.claims(&h.id)) else { + return false; + }; + self.mods.mods.use_start(&item); + self.mods.using = Some(ModUse { + item, + since: self.ticks, + }); + true + } + /// Ends a claimed use: the button came up, the player switched slots, + /// or a screen opened. False when none was going. + pub(super) fn stop_mod_use(&mut self) -> bool { + let Some(u) = self.mods.using.take() else { + return false; + }; + let ticks = self.ticks.saturating_sub(u.since).min(u64::from(u32::MAX)) as u32; + self.mods.mods.use_stop(&u.item, ticks); + true + } + /// Once a tick: a claimed use stops when its item leaves the hand or a + /// screen opens, as vanilla's `releaseUsingItem`. + pub(super) fn tick_mod_use(&mut self) { + let Some(u) = &self.mods.using else { + return; + }; + let still = self + .mod_held() + .is_some_and(|h| h.id == u.item.id && h.slot == u.item.slot); + if !still || self.screen != Screen::Playing { + self.stop_mod_use(); + } + } + /// A key went down or up: a mod's registered key while playing (no + /// screen) is its `key-down` / `key-up`. Vanilla's mapping on the same + /// key still runs. + pub(super) fn mod_key(&mut self, key: KeyCode, pressed: bool, repeat: bool) { + if repeat || !self.mods.mods.has_keys() { + return; + } + if pressed && (self.screen != Screen::Playing || self.title.is_some()) { + return; + } + if let Some(name) = horizon_client::keybinds::key_name(key) { + self.mods.mods.key(name, pressed); + } + } +} diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs index 2a07cb940..1c62046ea 100644 --- a/crates/horizon-client/src/game/mods.rs +++ b/crates/horizon-client/src/game/mods.rs @@ -34,6 +34,9 @@ use { pub(super) struct ModsState { #[cfg(feature = "mods")] pub(super) mods: Mods, + /// Capability `input`: a claimed item's use while the button is down. + #[cfg(feature = "mods")] + pub(super) using: Option, /// Capability `render`'s camera shakes. #[cfg(feature = "mods")] pub(super) shake: super::mod_render::Shake, @@ -315,6 +318,7 @@ impl Game<'_> { player, clock, gui_size: self.gui_size(), + held: self.mod_held(), } } /// A game tick. @@ -322,6 +326,7 @@ impl Game<'_> { if !self.mods.mods.is_running() { return; } + self.tick_mod_use(); let snapshot = self.mod_snapshot(); self.mods.mods.set_snapshot(snapshot); self.mods.mods.tick(self.ticks); diff --git a/crates/horizon-mod-sdk/src/lib.rs b/crates/horizon-mod-sdk/src/lib.rs index 8708fb5be..43a6c4263 100644 --- a/crates/horizon-mod-sdk/src/lib.rs +++ b/crates/horizon-mod-sdk/src/lib.rs @@ -38,7 +38,7 @@ wit_bindgen::generate!({ pub use exports::horizon::mods::events::{Guest as __Guest, Registration}; pub use exports::horizon::mods::ui_events::Guest as __UiEventsGuest; -pub use horizon::mods::{chat, game, hud, log, render, storage, ui}; +pub use horizon::mods::{chat, game, hud, input, log, render, storage, ui}; /// The game's calls into a mod. Only the hooks [`Registration`] asks for /// are called; the others may stay as their defaults. diff --git a/crates/horizon-mod-sdk/wit/horizon.wit b/crates/horizon-mod-sdk/wit/horizon.wit index 64bbac592..1560ec3a7 100644 --- a/crates/horizon-mod-sdk/wit/horizon.wit +++ b/crates/horizon-mod-sdk/wit/horizon.wit @@ -163,6 +163,48 @@ interface ui-events { settings-opened: func(); } +/// Capability `input` (experimental: the manifest needs +/// `experimental = ["input"]`): the use button on items the mod claims, and +/// the mod's own keys. Events wait for `poll`, at most 256. +interface input { + /// The main hand's stack. + record held-item { + /// `minecraft:spyglass`, `:`. + id: string, + count: u32, + /// The hotbar slot, 0 to 8. + slot: u32, + } + record item-use { + item: held-item, + /// Game ticks the button was held (0 on `use-start`). + ticks: u32, + } + variant event { + /// The use button went down with a claimed item in the main hand. + /// The game does nothing else with the click: no placing, no eating, + /// no swing. + use-start(item-use), + /// It came up again, or the item left the hand, or a screen opened. + use-stop(item-use), + /// A key `register-key` named went down or up while playing (no + /// screen open). The game's own mapping on that key still runs. + key-down(string), + key-up(string), + } + /// The items whose use the mod handles instead of the game, by id (at + /// most 64). Replaces the previous list. + claim-items: func(ids: list); + /// A key of the mod's: `name` comes back in `key-down` and `key-up`, + /// `label` says what it does, `key` is the key as options.txt writes + /// it (`key.keyboard.r`). At most 16; an unknown key is an error. + register-key: func(name: string, label: string, key: string) -> result<_, string>; + /// The main hand's stack, as of this tick; none when it is empty. + held: func() -> option; + /// The events since the last call, oldest first. + poll: func() -> list; +} + /// Capability `render` (experimental: the manifest needs /// `experimental = ["render"]`): shapes the game draws for the mod with its /// own renderer, kept until the mod replaces them (no call per frame). A mod @@ -280,6 +322,7 @@ world horizon-ui-mod { include horizon-mod; import ui; import render; + import input; export ui-events; } @@ -292,4 +335,5 @@ world ui-host { /// The game's side of capability `render` (imports only). world render-host { import render; + import input; } diff --git a/crates/horizon-mods/src/manifest.rs b/crates/horizon-mods/src/manifest.rs index 1880b9f3e..e9535bc78 100644 --- a/crates/horizon-mods/src/manifest.rs +++ b/crates/horizon-mods/src/manifest.rs @@ -87,9 +87,12 @@ pub enum Capability { /// Shapes on the HUD and the camera's shake, drawn by the game's own /// renderer (interface `render`). Render, + /// The use button on items the mod claims, and its own keys + /// (interface `input`). + Input, } impl Capability { - pub const ALL: [Capability; 7] = [ + pub const ALL: [Capability; 8] = [ Capability::GameRead, Capability::Chat, Capability::Hud, @@ -97,6 +100,7 @@ impl Capability { Capability::Registries, Capability::Ui, Capability::Render, + Capability::Input, ]; /// The WIT interface it grants, in `horizon:mods`. pub fn interface(self) -> &'static str { @@ -108,12 +112,16 @@ impl Capability { Capability::Registries => "registries", Capability::Ui => "ui", Capability::Render => "render", + Capability::Input => "input", } } /// May change in any release: a mod uses it only with /// `experimental = [""]` in its manifest. pub fn is_experimental(self) -> bool { - matches!(self, Capability::Ui | Capability::Render) + matches!( + self, + Capability::Ui | Capability::Render | Capability::Input + ) } /// What the player reads before turning the mod on: "May ". pub fn describe(self) -> &'static str { @@ -125,6 +133,7 @@ impl Capability { Capability::Registries => "add blocks and items to your worlds", Capability::Ui => "show its own screens and a settings page", Capability::Render => "draw on your screen and shake the camera", + Capability::Input => "take over the use button on its own items, and add keys", } } /// The capability a component import needs, `Ok(None)` for one every @@ -157,6 +166,7 @@ impl fmt::Display for Capability { Capability::Registries => "registries", Capability::Ui => "ui", Capability::Render => "render", + Capability::Input => "input", }; f.write_str(name) } diff --git a/crates/horizon-mods/src/runtime.rs b/crates/horizon-mods/src/runtime.rs index e19bfbe3f..881eaf0ae 100644 --- a/crates/horizon-mods/src/runtime.rs +++ b/crates/horizon-mods/src/runtime.rs @@ -47,6 +47,9 @@ mod render_bindings { world: "render-host", }); } +use render_bindings::horizon::mods::input; +/// Capability `input`'s records (WIT `interface input`). +pub use render_bindings::horizon::mods::input::{Event as InputEvent, HeldItem, ItemUse}; /// What capability `render` asks the game to draw (WIT `interface render`). pub use render_bindings::horizon::mods::render::{ FillShape, ItemShape, Point as RenderPoint, QuadShape, Rect as RenderRect, Shape, SpriteShape, @@ -85,6 +88,8 @@ pub struct Snapshot { pub clock: Option, /// The screen in GUI pixels (capability `render`'s `gui-size`). pub gui_size: (f32, f32), + /// The main hand's stack (capability `input`'s `held`). + pub held: Option, } /// What mods asked the game to show since the last [`Mods::drain`]. @@ -172,6 +177,7 @@ struct State { ui: UiState, /// Capability `render`'s HUD shapes, drawn until replaced. shapes: Vec, + input: InputState, /// Capability `storage`'s folder; made on first `set`. data_dir: PathBuf, /// Bytes `storage::set` may still write before this hook call ends @@ -312,6 +318,7 @@ impl Mods { panel: None, ui: UiState::default(), shapes: Vec::new(), + input: InputState::default(), data_dir, storage_budget: STORAGE_WRITE_BUDGET, limits: StoreLimitsBuilder::new() @@ -503,6 +510,78 @@ impl Mods { .iter() .filter_map(|e| e.run.as_ref()?.store.data().panel.as_ref()) } + /// Some running mod claimed items (capability `input`). + pub fn claims_any(&self) -> bool { + self.inputs().any(|i| !i.claimed.is_empty()) + } + /// Some running mod claimed the item `id`. + pub fn claims(&self, id: &str) -> bool { + self.inputs().any(|i| i.claimed.iter().any(|c| c == id)) + } + /// Some running mod registered a key. + pub fn has_keys(&self) -> bool { + self.inputs().any(|i| !i.keys.is_empty()) + } + /// The mods' keys: (mod name, key name, label, key), for Controls. + pub fn mod_keys(&self) -> impl Iterator { + self.entries + .iter() + .filter_map(|e| e.run.as_ref()) + .flat_map(|r| { + let d = r.store.data(); + d.input.keys.iter().map(move |k| (d.name.as_str(), k)) + }) + } + /// The use button went down with `item`: `use-start` to each mod that + /// claimed it. + pub fn use_start(&mut self, item: &HeldItem) { + let event = InputEvent::UseStart(ItemUse { + item: item.clone(), + ticks: 0, + }); + self.push_input(|i| i.claimed.contains(&item.id), &event); + } + /// The claimed use ended after `ticks`. + pub fn use_stop(&mut self, item: &HeldItem, ticks: u32) { + let event = InputEvent::UseStop(ItemUse { + item: item.clone(), + ticks, + }); + self.push_input(|i| i.claimed.contains(&item.id), &event); + } + /// The key `key` (`key.keyboard.r`) went down or up: each mod with a + /// key on it hears its own name for it. + pub fn key(&mut self, key: &str, down: bool) { + for run in self.entries.iter_mut().filter_map(|e| e.run.as_mut()) { + let input = &mut run.store.data_mut().input; + let names: Vec = input + .keys + .iter() + .filter(|k| k.key == key) + .map(|k| k.name.clone()) + .collect(); + for name in names { + input.push(if down { + InputEvent::KeyDown(name) + } else { + InputEvent::KeyUp(name) + }); + } + } + } + fn inputs(&self) -> impl Iterator { + self.entries + .iter() + .filter_map(|e| Some(&e.run.as_ref()?.store.data().input)) + } + fn push_input(&mut self, to: impl Fn(&InputState) -> bool, event: &InputEvent) { + for run in self.entries.iter_mut().filter_map(|e| e.run.as_mut()) { + let input = &mut run.store.data_mut().input; + if to(input) { + input.push(event.clone()); + } + } + } /// The running mods' HUD shapes (capability `render`), each mod's in /// its own order, mods in load order. pub fn hud_shapes(&self) -> impl Iterator { @@ -698,6 +777,80 @@ impl chat::Host for State { } } } +/// A key a mod registered (capability `input`). +#[derive(Clone, Debug, PartialEq)] +pub struct ModKey { + /// The mod's own name for it, echoed in `key-down`. + pub name: String, + /// What it does, for Controls. + pub label: String, + /// `key.keyboard.r`. + pub key: String, +} + +/// What capability `input` holds for one mod. +#[derive(Default)] +struct InputState { + claimed: Vec, + keys: Vec, + events: std::collections::VecDeque, +} +/// Events kept for `poll`, items claimed and keys, per mod. +const INPUT_EVENTS: usize = 256; +const CLAIMS: usize = 64; +const KEYS: usize = 16; + +/// `key.keyboard.` or `key.mouse.`, as options.txt writes keys; +/// the game knows which names exist and ignores a key it has none for. +fn key_ok(key: &str) -> bool { + let rest = key + .strip_prefix("key.keyboard.") + .or_else(|| key.strip_prefix("key.mouse.")); + rest.is_some_and(|r| { + (1..=32).contains(&r.len()) + && r.bytes() + .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'.') + }) +} + +impl InputState { + fn push(&mut self, event: InputEvent) { + if self.events.len() == INPUT_EVENTS { + self.events.pop_front(); + } + self.events.push_back(event); + } +} + +impl input::Host for State { + fn claim_items(&mut self, ids: Vec) { + self.input.claimed = ids.into_iter().take(CLAIMS).map(clip).collect(); + } + fn register_key(&mut self, name: String, label: String, key: String) -> Result<(), String> { + if !key_ok(&key) { + return Err(format!( + "{key:?} is not a key as options.txt writes them (key.keyboard.r)" + )); + } + let (name, label) = (clip(name), clip(label)); + if let Some(k) = self.input.keys.iter_mut().find(|k| k.name == name) { + (k.label, k.key) = (label, key); + return Ok(()); + } + if self.input.keys.len() == KEYS { + return Err(format!("a mod may register {KEYS} keys")); + } + self.input.keys.push(ModKey { name, label, key }); + Ok(()) + } + fn held(&mut self) -> Option { + self.snapshot.held.clone() + } + fn poll(&mut self) -> Vec { + self.input.events.drain(..).collect() + } +} + /// HUD shapes one mod may keep (capability `render`). const SHAPES: usize = 1024; /// The longest jolt `render.shake` makes, in seconds. diff --git a/crates/horizon-mods/tests/sandbox.rs b/crates/horizon-mods/tests/sandbox.rs index dac333fe6..ba1736e5e 100644 --- a/crates/horizon-mods/tests/sandbox.rs +++ b/crates/horizon-mods/tests/sandbox.rs @@ -1,7 +1,8 @@ //! Loads real mods built with the SDK: the coords-hud example and a guest //! that misbehaves on command. Needs `rustup target add wasm32-unknown-unknown`. use horizon_mods::{ - Clock, Corner, Mods, Output, Package, Player, Shape, Snapshot, Status, manifest::componentize, + Clock, Corner, HeldItem, Mods, Output, Package, Player, Shape, Snapshot, Status, + manifest::componentize, }; use std::{ path::{Path, PathBuf}, @@ -22,8 +23,8 @@ id = "test_ui_guest" name = "Test UI Guest" version = "0.1.0" api = "0.1" -capabilities = ["chat", "ui", "render"] -experimental = ["ui", "render"] +capabilities = ["chat", "ui", "render", "input"] +experimental = ["ui", "render", "input"] "#; /// Builds both mods once and writes their `.hmod` files; returns the folder. @@ -90,6 +91,7 @@ fn player() -> Snapshot { day_time: 24000 + 6000, }), gui_size: (427.0, 240.0), + held: None, } } @@ -422,6 +424,7 @@ fn render_shapes_and_shakes() { let mut mods = load("horizon_mods_test_ui_guest.hmod"); mods.set_snapshot(Snapshot { gui_size: (427.0, 240.0), + held: None, ..Snapshot::default() }); assert_eq!(mods.hud_shapes().count(), 0); @@ -448,6 +451,64 @@ fn render_shapes_and_shakes() { ); } +#[test] +fn input_claims_keys_and_events() { + let mut mods = load("horizon_mods_test_ui_guest.hmod"); + let chat = |mods: &mut Mods| -> Vec { + mods.drain() + .into_iter() + .filter_map(|o| match o { + Output::Chat { text, .. } => Some(text), + _ => None, + }) + .collect() + }; + assert!(!mods.claims_any() && !mods.has_keys()); + assert!(mods.command("input_claim")); + let said = chat(&mut mods); + assert!( + said[0].starts_with("claimed; \"R\" is not a key"), + "{said:?}" + ); + assert!(mods.claims("minecraft:stick") && !mods.claims("minecraft:stone")); + let keys: Vec<_> = mods + .mod_keys() + .map(|(m, k)| (m.to_owned(), k.key.clone())) + .collect(); + assert_eq!( + keys, + [("Test UI Guest".to_owned(), "key.keyboard.r".to_owned())] + ); + // The game feeds the events; the mod reads them in order with `poll`. + let stick = HeldItem { + id: "minecraft:stick".into(), + count: 3, + slot: 2, + }; + mods.use_start(&stick); + mods.key("key.keyboard.r", true); + mods.key("key.keyboard.t", true); + mods.key("key.keyboard.r", false); + mods.use_stop(&stick, 12); + assert!(mods.command("input_poll")); + assert_eq!( + chat(&mut mods), + [ + "use-start minecraft:stick 0", + "key-down wheel", + "key-up wheel", + "use-stop minecraft:stick 12" + ] + ); + // `held` reads the snapshot. + mods.set_snapshot(Snapshot { + held: Some(stick), + ..Snapshot::default() + }); + assert!(mods.command("input_held")); + assert_eq!(chat(&mut mods), ["holding minecraft:stick x3 in 2"]); +} + #[test] fn content_only_mods_load_without_an_engine() { let manifest = r#" diff --git a/crates/horizon-mods/tests/ui_guest/src/lib.rs b/crates/horizon-mods/tests/ui_guest/src/lib.rs index 146ab6f40..be2b8fcc4 100644 --- a/crates/horizon-mods/tests/ui_guest/src/lib.rs +++ b/crates/horizon-mods/tests/ui_guest/src/lib.rs @@ -1,9 +1,11 @@ //! A mod for the runtime's `ui` tests: `/ui_screen` opens a screen with one //! of each widget, `/ui_settings` sets a settings page, `/ui_close` closes //! the screen; the `ui-events` calls answer in chat so the host sees them. -//! `/render_hud`, `/render_clear` and `/render_shake` test `render`. +//! `/render_hud`, `/render_clear` and `/render_shake` test `render`; +//! `/input_claim`, `/input_poll` and `/input_held` test `input`. use horizon_mod_sdk::{ Mod, Registration, chat, + input::{self, Event}, render::{self, FillShape, ItemShape, Point, QuadShape, Rect, Shape, SpriteShape, TextShape}, ui::{self, ItemWidget, ListWidget, Screen, TextWidget, Widget}, }; @@ -87,6 +89,9 @@ impl Mod for Guest { "render_hud", "render_clear", "render_shake", + "input_claim", + "input_poll", + "input_held", ] .into_iter() .fold(Registration::new(), Registration::command); @@ -102,6 +107,29 @@ impl Mod for Guest { "ui_close" => ui::close_screen(), "render_hud" => render::set_hud(&shapes()), "render_clear" => render::set_hud(&[]), + "input_claim" => { + input::claim_items(&["minecraft:stick".into()]); + input::register_key("wheel", "Open the wheel", "key.keyboard.r")?; + // Not a key: refused. + let bad = input::register_key("bad", "Bad", "R").unwrap_err(); + chat::show(&format!("claimed; {bad}")); + } + "input_poll" => { + for event in input::poll() { + chat::show(&match event { + Event::UseStart(u) => format!("use-start {} {}", u.item.id, u.ticks), + Event::UseStop(u) => format!("use-stop {} {}", u.item.id, u.ticks), + Event::KeyDown(k) => format!("key-down {k}"), + Event::KeyUp(k) => format!("key-up {k}"), + }); + } + } + "input_held" => { + let held = input::held().map_or("nothing".into(), |h| { + format!("{} x{} in {}", h.id, h.count, h.slot) + }); + chat::show(&format!("holding {held}")); + } // Clamped to (1, 10); the two after it shake nothing. "render_shake" => { render::shake(2.0, 99.0); diff --git a/docs/api/mods-reference.md b/docs/api/mods-reference.md index ef889d599..b1b52164a 100644 --- a/docs/api/mods-reference.md +++ b/docs/api/mods-reference.md @@ -14,6 +14,7 @@ The contract between Horizon and its mods: the WIT package `horizon:mods@0.1.0`, | `registries` | data files (`assets/`, `data/`), no interface | add blocks and items to your worlds | | `ui` | [`ui`](#ui) | show its own screens and a settings page | | `render` | [`render`](#render) | draw on your screen and shake the camera | +| `input` | [`input`](#input) | take over the use button on its own items, and add keys | ## World `horizon-mod` @@ -24,7 +25,7 @@ The contract between Horizon and its mods: the WIT package `horizon:mods@0.1.0`, A mod with widgets (capability `ui`): the SDK builds every mod against this world. Mods built for `horizon-mod` alone (no `ui-events` export) still load and simply hear nothing of their widgets. -- Imports (what the game offers): `ui`, `render`, `log`, `game`, `chat`, `hud`, `storage` +- Imports (what the game offers): `ui`, `render`, `input`, `log`, `game`, `chat`, `hud`, `storage` - Exports (what the mod provides): `ui-events`, `events` ## World `ui-host` @@ -38,7 +39,7 @@ The game's side of `horizon-ui-mod` beyond `horizon-mod`. The game's side of capability `render` (imports only). -- Imports (what the game offers): `render` +- Imports (what the game offers): `render`, `input` - Exports (what the mod provides): ## `log` @@ -213,6 +214,45 @@ What the game calls on a mod that shows widgets (capability `ui`): what the play | `closed` | `func()` | The player left the mod's screen (Esc or Done); not for a screen the mod closed itself or one that went away with the mod. | | `settings-opened` | `func()` | The player opened the mod's settings page in Options > Mods: call `ui::set-settings` now to fill it with fresh widgets. | +## `input` + +Access: capability `input`. + +Capability `input` (experimental: the manifest needs `experimental = ["input"]`): the use button on items the mod claims, and the mod's own keys. Events wait for `poll`, at most 256. + +### record `held-item` + +The main hand's stack. + +| Field | Type | | +| --- | --- | --- | +| `id` | `string` | `minecraft:spyglass`, `:`. | +| `count` | `u32` | | +| `slot` | `u32` | The hotbar slot, 0 to 8. | + +### record `item-use` + +| Field | Type | | +| --- | --- | --- | +| `item` | `held-item` | | +| `ticks` | `u32` | Game ticks the button was held (0 on `use-start`). | + +### variant `event` + +| Case | Payload | | +| --- | --- | --- | +| `use-start` | `item-use` | The use button went down with a claimed item in the main hand. The game does nothing else with the click: no placing, no eating, no swing. | +| `use-stop` | `item-use` | It came up again, or the item left the hand, or a screen opened. | +| `key-down` | `string` | A key `register-key` named went down or up while playing (no screen open). The game's own mapping on that key still runs. | +| `key-up` | `string` | | + +| Function | Signature | What it does | +| --- | --- | --- | +| `claim-items` | `func(ids: list)` | The items whose use the mod handles instead of the game, by id (at most 64). Replaces the previous list. | +| `register-key` | `func(name: string, label: string, key: string) -> result<_, string>` | A key of the mod's: `name` comes back in `key-down` and `key-up`, `label` says what it does, `key` is the key as options.txt writes it (`key.keyboard.r`). At most 16; an unknown key is an error. | +| `held` | `func() -> option` | The main hand's stack, as of this tick; none when it is empty. | +| `poll` | `func() -> list` | The events since the last call, oldest first. | + ## `render` Access: capability `render`. diff --git a/docs/api/mods.md b/docs/api/mods.md index cda484c31..29da98122 100644 --- a/docs/api/mods.md +++ b/docs/api/mods.md @@ -95,6 +95,8 @@ What real mods need next, in the order modpacks and servers need it: ## Changelog +- Experimental `input` (`experimental = ["input"]`): `claim-items`, `register-key`, `held`, `poll` (use-start, + use-stop, key-down, key-up). - Experimental `render` (`experimental = ["render"]`): HUD shapes (fill, quad, sprite, item, text), `gui-size`, camera `shake`, drawn by the game's own renderer. - API 0.2 (content): data-driven blocks and items, tags, `horizon:add_to`, terrain adaptation. diff --git a/docs/api/stability.md b/docs/api/stability.md index 76c3c4e51..b513d5ddf 100644 --- a/docs/api/stability.md +++ b/docs/api/stability.md @@ -89,6 +89,7 @@ Deprecations and breaking changes are in bold. The release notes of the game rel | Mod API: `log`, `game`, `chat`, `hud`, `storage`, `events` | preview | 0.1.0 | | Mod API: `ui`, `ui-events` (a mod's screens and settings page) | experimental | manifest `experimental = ["ui"]` | | Mod API: `render` (HUD shapes, camera shake; drawn by the game, no mod shaders) | experimental | manifest `experimental = ["render"]` | +| Mod API: `input` (claimed items' use, the mod's keys) | experimental | manifest `experimental = ["input"]` | | Content mods (`registries`: data-driven blocks and items) | preview | manifest `api = "0.2"` | | Server mods (events, world, server commands) | not started | manifest `api = "0.3"`, experimental first | | Script mods (Lua run by an interpreter mod, [mods.md](mods.md#ways-in)) | not started | | diff --git a/docs/mods.md b/docs/mods.md index 672d6c7fd..01de73fdb 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -88,6 +88,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | `storage` | `storage` | `get(key)`, `set(key, value)`, `delete(key)`: a per-mod folder of up to 64 files, 4 MiB total, at most 1 MiB written per call | | `ui` | `ui` | `open-screen`, `close-screen`, `set-settings`: a screen the mod owns and a page in Options > Mods, of text, item and list widgets the game lays out and draws with its own menus' code. **Experimental:** the manifest needs `experimental = ["ui"]` | | `render` | `render` | `set-hud(shapes)`: up to 1024 fills, four-cornered shapes, GUI sprites, items and text, kept on the HUD (over the hotbar, under chat and screens, hidden with F1) until replaced; `gui-size()`; `shake(strength, seconds)`: the view turns, the player doesn't, scaled by Distortion Effects. The game's own HUD renderer draws them: a mod never supplies shader code. **Experimental:** `experimental = ["render"]` | +| `input` | `input` | `claim-items(ids)`: the use button on those items is the mod's (`use-start`, `use-stop` with the ticks held; the game doesn't place, eat or swing); `register-key(name, label, key)`: up to 16 keys (`key.keyboard.r`), heard while playing as `key-down` / `key-up` (the game's own mapping on that key still runs); `held()`; `poll()` returns the events, at most 256 kept. **Experimental:** `experimental = ["input"]` | | Hook (export) | When | |---|---| @@ -289,7 +290,7 @@ experimental = [] # experimental capabilities it uses, e.g. ["ui"] ``` Unknown keys are refused, so a typo is an error, not a silently ignored -permission. An experimental capability (today `ui` and `render`) may change in any release +permission. An experimental capability (today `ui`, `render` and `input`) may change in any release ([stability](api/stability.md)): a mod uses it only by naming it in `experimental` as well, so nobody depends on it by accident. From 61c5f29ab772d04d4cf55c06f59870c27205c16b Mon Sep 17 00:00:00 2001 From: levzzz <86072227+levzzz5154@users.noreply.github.com> Date: Mon, 26 Oct 2026 01:04:47 +0500 Subject: [PATCH 181/238] Game Rules and Statistics: the scroll bars can be dragged Both lists drew a scroll bar but only listened to the mouse wheel: a press inside the bar column, the drag that follows and the release were dropped. `ui::Scroll` already models AbstractSelectionList's press/drag/release (the server list uses it); the two screens now keep their scrolling in it and the window passes the press, the pointer's moves while it is held and the release along. Fixes #1782 --- crates/horizon-client/src/game.rs | 10 +++ .../src/game/game_rules_screen.rs | 56 +++++++++++---- crates/horizon-client/src/game/lan_screen.rs | 21 ++++++ .../horizon-client/src/game/stats_screen.rs | 68 ++++++++++++++++--- 4 files changed, 132 insertions(+), 23 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index f05ee41e4..f4b2fc192 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -10962,6 +10962,12 @@ impl ApplicationHandler for Game<'_> { if self.screen == Screen::Advancements { self.advancements_drag(); } + if self.screen == Screen::Stats { + self.stats_drag(); + } + if self.screen == Screen::WorldOptions { + self.world_options_drag(); + } if self.screen == Screen::Creative { let (_, oy) = Self::creative_origin(&b); self.creative_screen.drag_scrollbar(self.cursor.1 - oy); @@ -11238,6 +11244,9 @@ impl ApplicationHandler for Game<'_> { Screen::WorldOptions if pressed && button == MouseButton::Left => { self.world_options_click() } + Screen::WorldOptions if button == MouseButton::Left => { + self.world_options_release() + } Screen::Friends if pressed && button == MouseButton::Left => { self.friends_screen_click() } @@ -11261,6 +11270,7 @@ impl ApplicationHandler for Game<'_> { .map_or([1, 1], |r| [r.config.width, r.config.height]); self.stats_click(size); } + Screen::Stats if button == MouseButton::Left => self.stats_release(), _ => {} } } diff --git a/crates/horizon-client/src/game/game_rules_screen.rs b/crates/horizon-client/src/game/game_rules_screen.rs index d925c5081..126bc8804 100644 --- a/crates/horizon-client/src/game/game_rules_screen.rs +++ b/crates/horizon-client/src/game/game_rules_screen.rs @@ -45,7 +45,8 @@ pub(super) struct RulesScreen { initial: GameRules, /// IntegerRuleEntry's boxes, by rule. numbers: Vec<(&'static str, EditBox)>, - scroll: f32, + /// RuleList's scrolling: the wheel and the held scrollbar. + scroll: ui::Scroll, /// ConfirmScreen "Game Rule Changes" in its place. confirm: bool, } @@ -120,7 +121,7 @@ impl RulesScreen { values: rules.clone(), initial: rules.clone(), numbers, - scroll: 0.0, + scroll: ui::Scroll::default(), confirm: false, } } @@ -156,12 +157,17 @@ impl RulesScreen { }) .collect() } - fn max_scroll(rows: usize, h: f32) -> f32 { - (ROW * rows as f32 + 4.0 - (h - HEADER - FOOTER)).max(0.0) - } /// Entry `i`'s top: 2 under the list's top, less the scroll. fn row_top(&self, i: usize) -> f32 { - HEADER + 2.0 - self.scroll.trunc() + ROW * i as f32 + HEADER + 2.0 - self.scroll.amount.trunc() + ROW * i as f32 + } + /// The list's scrollbar column (AbstractSelectionList: 6 pixels right of + /// the entries) and the pixels it scrolls. + fn bar(w: f32) -> f32 { + row_left(w) + ROW_WIDTH + 6.0 + 2.0 + } + fn content(&self) -> f32 { + ROW * self.rows().len() as f32 + 4.0 } /// getEntryAtPosition: the row under the pointer, inside the list. fn hovered(&self, rows: usize, (x, y): (f32, f32), (w, h): (f32, f32)) -> Option { @@ -212,10 +218,28 @@ impl RulesScreen { /// The mouse wheel (AbstractSelectionList: half an entry a step). pub(super) fn wheel(&mut self, delta: f32, h: f32) { if !self.confirm { - let max = Self::max_scroll(self.rows().len(), h); - self.scroll = (self.scroll - delta * ROW / 2.0).clamp(0.0, max); + let content = self.content(); + self.scroll + .wheel(delta, ROW / 2.0, HEADER, h - FOOTER, content); } } + /// A press on the scrollbar holds it (AbstractSelectionList.updateScrolling). + pub(super) fn press_scrollbar(&mut self, cursor: (f32, f32), (w, h): (f32, f32)) -> bool { + let content = self.content(); + self.scroll + .press(cursor, Self::bar(w), HEADER, h - FOOTER, content) + } + /// The pointer moved while the scrollbar is held. + pub(super) fn drag(&mut self, y: f32, h: f32) { + if self.scroll.held() { + let content = self.content(); + self.scroll.drag(y, HEADER, h - FOOTER, content); + } + } + /// The button came up: the scrollbar lets go. + pub(super) fn release(&mut self) { + self.scroll.release(); + } fn focused(&mut self) -> Option<(Option<&'static str>, &mut EditBox)> { if self.search.focused { return Some((None, &mut self.search)); @@ -229,7 +253,7 @@ impl RulesScreen { /// number sets its rule when valid (IntegerRuleEntry's responder). fn edited(&mut self, rule: Option<&'static str>) { let Some(name) = rule else { - self.scroll = 0.0; + self.scroll.amount = 0.0; return; }; let Some(r) = gamerules::rule(name) else { @@ -286,6 +310,11 @@ impl RulesScreen { if on(cx, cy, 150.0, 20.0) { return self.close(); } + // A press on the scrollbar holds it and clicks nothing else. + if self.press_scrollbar(cursor, size) { + self.unfocus(); + return None; + } // A click moves the focus to the widget under it. let (sx, sy) = search_at(w); if on(sx, sy, SEARCH_WIDTH, 15.0) { @@ -360,7 +389,8 @@ impl RulesScreen { return; } let rows = self.rows(); - self.scroll = self.scroll.clamp(0.0, Self::max_scroll(rows.len(), h)); + let content = ROW * rows.len() as f32 + 4.0; + self.scroll.clamp(HEADER, h - FOOTER, content); // RuleList: the in-world list background, its entries clipped to it, // then the separators and scrollbar. ui::list_background(batch, atlas, HEADER, h - FOOTER, true); @@ -421,16 +451,14 @@ impl RulesScreen { } batch.clip_since(first, 0.0, HEADER, w, h - FOOTER); ui::separators(batch, atlas, HEADER, h - FOOTER, true); - let content = ROW * rows.len() as f32 + 4.0; - let bar_x = left + ROW_WIDTH + 6.0 + 2.0; ui::scrollbar( batch, atlas, - bar_x, + Self::bar(w), HEADER, h - FOOTER, content, - self.scroll.trunc(), + self.scroll.amount.trunc(), ); // The header: the title centred over the search box. let title = Text::translate("editGamerule.title", &[]); diff --git a/crates/horizon-client/src/game/lan_screen.rs b/crates/horizon-client/src/game/lan_screen.rs index 335b5fbae..2d0416d72 100644 --- a/crates/horizon-client/src/game/lan_screen.rs +++ b/crates/horizon-client/src/game/lan_screen.rs @@ -802,6 +802,27 @@ impl Game<'_> { } } + /// The pointer moved while a list's scrollbar is held: the game rules' + /// list follows it (AbstractSelectionList.mouseDragged). + pub(super) fn world_options_drag(&mut self) { + let h = self.gui_size().1; + if let Some(o) = self.world_options.as_mut() + && o.view == View::GameRules + && let Some(rules) = o.rules.as_mut() + { + rules.drag(self.cursor.1, h); + } + } + + /// The button came up: a held scrollbar lets go (onRelease). + pub(super) fn world_options_release(&mut self) { + if let Some(o) = self.world_options.as_mut() + && let Some(rules) = o.rules.as_mut() + { + rules.release(); + } + } + pub(super) fn world_options_click(&mut self) { let Some(mut o) = self.world_options.take() else { return; diff --git a/crates/horizon-client/src/game/stats_screen.rs b/crates/horizon-client/src/game/stats_screen.rs index 5505d423d..e86b29aeb 100644 --- a/crates/horizon-client/src/game/stats_screen.rs +++ b/crates/horizon-client/src/game/stats_screen.rs @@ -158,7 +158,8 @@ const COLUMNS: [(&str, &str); 6] = [ #[derive(Default)] pub(super) struct StatsUi { tab: usize, - scroll: f32, + /// The list's scrolling: the wheel and the held scrollbar. + scroll: horizon_client::ui::Scroll, sort: Option<(usize, i32)>, } /// `MenuTabBar` geometry: three tabs over at most 400 pixels. @@ -182,6 +183,15 @@ impl Game<'_> { self.stats_ui = StatsUi::default(); self.screen = Screen::Stats; } + /// The list's scrollbar column (right of the 280 pixel list) and the + /// pixels it scrolls. + fn stats_bar(w: f32) -> f32 { + ((w - 280.0) / 2.0).floor() + 280.0 + 6.0 + } + fn stats_content(&self) -> f32 { + let (row_h, rows) = self.stats_rows(); + row_h * rows as f32 + 4.0 + } fn stats_rows(&self) -> (f32, usize) { match self.stats_ui.tab { 0 => (14.0, CUSTOM.len()), @@ -260,10 +270,9 @@ impl Game<'_> { // The list. let (row_h, rows) = self.stats_rows(); let content = row_h * rows as f32 + 4.0; - let max_scroll = (content - (bottom - top)).max(0.0); - self.stats_ui.scroll = self.stats_ui.scroll.clamp(0.0, max_scroll); + self.stats_ui.scroll.clamp(top, bottom, content); let left = ((w - 280.0) / 2.0).floor(); - let scroll = self.stats_ui.scroll; + let scroll = self.stats_ui.scroll.amount; let row_y = |i: usize| top + 4.0 + i as f32 * row_h - scroll; let visible = |y: f32| y >= top && y + row_h <= bottom; match self.stats_ui.tab { @@ -408,11 +417,11 @@ impl Game<'_> { ui::scrollbar( batch, atlas, - left + 280.0 + 6.0, + Self::stats_bar(w), top, bottom, content, - self.stats_ui.scroll, + self.stats_ui.scroll.amount, ); self.draw_stats_footer(batch, w, h); } @@ -433,6 +442,10 @@ impl Game<'_> { ); } pub(super) fn stats_click(&mut self, size: [u32; 2]) { + // A press on the scrollbar holds it and clicks nothing else. + if self.stats_press_scrollbar(size) { + return; + } let b = HudBatch::new(size); let (w, h) = (b.width, b.height); let (x0, tw) = tabs(w); @@ -441,7 +454,7 @@ impl Game<'_> { && self.stats_ui.tab != i { self.stats_ui.tab = i; - self.stats_ui.scroll = 0.0; + self.stats_ui.scroll.amount = 0.0; self.audio .play("minecraft:ui.button.click", None, 0.25, 1.0); return; @@ -461,7 +474,7 @@ impl Game<'_> { } if self.stats_ui.tab == 1 { let left = ((w - 280.0) / 2.0).floor(); - let y = TAB_H + 4.0 - self.stats_ui.scroll; + let y = TAB_H + 4.0 - self.stats_ui.scroll.amount; for c in 0..COLUMNS.len() { let x = left + 75.0 + 40.0 * c as f32 - 18.0; if ui::hovered(self.cursor, x, y + 1.0, 18.0, 18.0) { @@ -479,6 +492,43 @@ impl Game<'_> { } pub(super) fn stats_scroll(&mut self, amount: f32) { let (row_h, _) = self.stats_rows(); - self.stats_ui.scroll -= amount * row_h / 2.0; + let size = self + .renderer + .as_ref() + .map_or([1280, 720], |r| [r.config.width, r.config.height]); + let h = HudBatch::new(size).height; + let content = self.stats_content(); + self.stats_ui + .scroll + .wheel(amount, row_h / 2.0, TAB_H, h - FOOTER, content); + } + /// A press on the statistics list's scrollbar holds it + /// (`AbstractSelectionList.updateScrolling`). + pub(super) fn stats_press_scrollbar(&mut self, size: [u32; 2]) -> bool { + let b = HudBatch::new(size); + let (w, h) = (b.width, b.height); + let content = self.stats_content(); + self.stats_ui + .scroll + .press(self.cursor, Self::stats_bar(w), TAB_H, h - FOOTER, content) + } + /// The pointer moved while the scrollbar is held. + pub(super) fn stats_drag(&mut self) { + if !self.stats_ui.scroll.held() { + return; + } + let size = self + .renderer + .as_ref() + .map_or([1280, 720], |r| [r.config.width, r.config.height]); + let h = HudBatch::new(size).height; + let content = self.stats_content(); + self.stats_ui + .scroll + .drag(self.cursor.1, TAB_H, h - FOOTER, content); + } + /// The button came up: the scrollbar lets go. + pub(super) fn stats_release(&mut self) { + self.stats_ui.scroll.release(); } } From a4a709c2d698788c30af6612d1870be9fc5e5763 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 22:16:18 +0100 Subject: [PATCH 182/238] Roadmap: OpenGL shipped in v0.2.63; three players' ideas kept by Ludovic One-command server install, launcher-style game menus (off by default), AppImage and a Nix flake. Co-Authored-By: Claude Opus 5.5 --- ROADMAP.md | 45 ++++++++++++++++++++++++----------- roadmap/roadmap.toml | 49 ++++++++++++++++++++++++++++++++++----- website/data/roadmap.json | 44 +++++++++++++++++++++++++++++++---- 3 files changed, 114 insertions(+), 24 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 7e4cc9937..a04eca2ce 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -16,7 +16,7 @@ Horizon is Minecraft: Java Edition 26.3 rewritten from scratch in Rust, so it ru - **Single player as a server: the last differences**: Single player now runs a small server in the background, as vanilla does. We are fixing what still behaves differently there; every fix also works for LAN and server players. - **Horizon's mod registry**: One place for mods, modpacks and resource packs made for Horizon: browse them on the website and install them from the launcher in one click. -**Next**: Microsoft sign-in (coming soon); CurseForge in the mod browser (coming soon); Fresh benchmarks against vanilla; Pistons push players and mobs; Horizon on Intel Macs; Faster world creation and entry on small CPUs; Servers that hold 20 TPS with 50 to 100 players; Low-end computers: Steam Deck, old Macs, 8 GB; Native upscaling; A batch of targeted optimisations; A Convert to Rust button for what isn't rewritten yet; Mods with code: machines, magic, mobs; Data packs in the mod browser; Prepare a version without Java; Server console, bans and a full admin API; Hosting polish: Windows service, safe updates; Guided hosting with Tailscale and an invite button; An official Docker image for servers; One single-player path: remove the in-frame world; Villages and combat, exactly as in vanilla; Choose the audio output device; Mods that read and change the world; Machines and blocks with inventories (API 0.4); Lua and other languages for mods; Well-known shader looks, rewritten for Horizon; A visible beta label; Wayland and Hyprland polish; A versioned agent-control protocol; Port mods with your own AI agent; Horizon on X (coming soon); Pipeline protection and a GitHub App for agents; Signed updates everywhere, installers included. +**Next**: Microsoft sign-in (coming soon); CurseForge in the mod browser (coming soon); Fresh benchmarks against vanilla; Pistons push players and mobs; Horizon on Intel Macs; Faster world creation and entry on small CPUs; Servers that hold 20 TPS with 50 to 100 players; Low-end computers: Steam Deck, old Macs, 8 GB; Native upscaling; A batch of targeted optimisations; A Convert to Rust button for what isn't rewritten yet; Mods with code: machines, magic, mobs; Data packs in the mod browser; Prepare a version without Java; Server console, bans and a full admin API; Hosting polish: Windows service, safe updates; Guided hosting with Tailscale and an invite button; An official Docker image for servers; One single-player path: remove the in-frame world; Villages and combat, exactly as in vanilla; Choose the audio output device; Mods that read and change the world; Machines and blocks with inventories (API 0.4); Lua and other languages for mods; Well-known shader looks, rewritten for Horizon; A visible beta label; Wayland and Hyprland polish; A versioned agent-control protocol; Port mods with your own AI agent; Horizon on X (coming soon); Pipeline protection and a GitHub App for agents; Signed updates everywhere, installers included; A server you install with one command. **How to help**: Play it and tell us what differs from vanilla (Discord or GitHub), run the benchmark on your computer, vote for what you want next, or pick one of the Help wanted items. @@ -374,6 +374,16 @@ More: [docs/server.md](docs/server.md) +#### A server you install with one command + +Install and run a Horizon dedicated server from a terminal with a single command, no launcher needed, for VPSes and headless machines. + +
    Technical + +A player's idea (#horizon-ideas). `horizon-server` already runs headless; left: an install script (Linux/macOS shell, Windows PowerShell) that fetches the signed release, checks it, asks for or points at the player's own game files and starts the server, documented in [docs/hosting.md](docs/hosting.md). + +
    + ### Mods and API #### Mods that read and change the world @@ -790,18 +800,6 @@ Issue [#1709](https://github.com/ludovic111/horizon/issues/1709): aarch64 builds -#### OpenGL for old computers - -A way to play on old PCs and Linux machines that have no Vulkan or DirectX 12, through OpenGL. - -
    Technical - -Players on older hardware ask whether Vulkan is the only way. Horizon draws with wgpu, which has an OpenGL backend; what is needed is to see which features hold up on it, how fast it runs, and a launcher switch like the DirectX 12 fallback that already ships. - -More: [docs/graphics.md](docs/graphics.md) - -
    - #### Android, iOS and the browser Versions of Horizon for Android, iOS and web browsers. Nothing official yet; contributors have started the first pieces. @@ -812,6 +810,16 @@ No official build. Android was first closed as not planned, with touch controls, +#### AppImage and a Nix flake + +Install Horizon on any Linux distribution as an AppImage, and on NixOS through a Nix flake. + +
    Technical + +A player's idea (#horizon-ideas). An AppImage of the launcher and game from the release workflow, and a flake.nix packaging the release binaries with their runtime libraries (Vulkan loader, X11/Wayland, ALSA), the way horizon-parity's NixOS setup runs them today. + +
    + ### Horizon extras #### A complete video studio inside the game @@ -824,6 +832,16 @@ Three stages. Recording: capture gameplay in a keypress with an encoder thread a +#### Game menus in the launcher's style + +An optional look for the game's own menus and options that matches the Horizon launcher. Off by default: the vanilla menus stay as they are. + +
    Technical + +A player's idea (#horizon-ideas). A built-in resource pack or a Horizon setting that restyles the title, pause and options screens with the launcher's design tokens (crates/horizon-theme), following [docs/design-system.md](docs/design-system.md); off by default because it changes the vanilla look. + +
    + ### AI and agents #### An optional AI teammate @@ -936,6 +954,7 @@ Vanilla's `Chicken.aiStep` updates `flap` and `flapSpeed` when the chicken isn't ## Recently shipped +- **OpenGL for old computers** (v0.2.63): Horizon now starts on old PCs whose graphics card has no Vulkan or DirectX 12 driver: it switches to OpenGL by itself, and Options > Graphics can pick OpenGL by hand. [#1917](https://github.com/ludovic111/horizon/issues/1917) · [#1920](https://github.com/ludovic111/horizon/issues/1920) · [#1936](https://github.com/ludovic111/horizon/issues/1936) - **Server resource packs** (v0.2.48): Servers can send you their resource pack: Horizon asks, downloads it and applies it, like vanilla. [#1562](https://github.com/ludovic111/horizon/issues/1562) · [#1569](https://github.com/ludovic111/horizon/issues/1569) · [#1570](https://github.com/ludovic111/horizon/issues/1570) - **Single player on its own server thread** (v0.2.39): Single player runs on a server thread, as vanilla does, so the world keeps ticking smoothly while the window draws. [#1514](https://github.com/ludovic111/horizon/issues/1514) · [docs/server-levels.md](docs/server-levels.md) - **GPU occlusion culling on Macs** (v0.2.17): On Macs the GPU now skips terrain hidden behind hills and caves: 8 to 48% more frames per second at 32 chunks on an M4 Pro. [#1434](https://github.com/ludovic111/horizon/issues/1434) diff --git a/roadmap/roadmap.toml b/roadmap/roadmap.toml index 34ae8a955..669596fba 100644 --- a/roadmap/roadmap.toml +++ b/roadmap/roadmap.toml @@ -968,13 +968,15 @@ links = ["#1709"] [[item]] id = "opengl-fallback" title = "OpenGL for old computers" -status = "later" +status = "shipped" +shipped_in = "v0.2.63" area = "platforms" -plain = "A way to play on old PCs and Linux machines that have no Vulkan or DirectX 12, through OpenGL." -dev = """Players on older hardware ask whether Vulkan is the only way. Horizon draws with wgpu, which has an OpenGL \ -backend; what is needed is to see which features hold up on it, how fast it runs, and a launcher switch like \ -the DirectX 12 fallback that already ships.""" -links = ["docs/graphics.md"] +plain = """Horizon now starts on old PCs whose graphics card has no Vulkan or DirectX 12 driver: it switches to OpenGL \ +by itself, and Options > Graphics can pick OpenGL by hand.""" +dev = """wgpu's GL backend (WGL on Windows, EGL on Linux) as the last fallback after Vulkan and DirectX 12 (#1917), \ +sprite arrays and CPU sprite animation for drivers without compute (#1920), no indirect draws below GL 4.3 \ +(#1936). Minimum OpenGL 3.3 core. Shader packs don't run on it yet.""" +links = ["#1917", "#1920", "#1936"] [[item]] id = "phones-and-browser" @@ -1187,3 +1189,38 @@ plain = """The launcher's Servers page creates, starts, backs up and updates a H invites friends.""" dev = """The launcher's Servers page (#1026) over `horizon-server` (docs/hosting.md).""" links = ["#1026", "docs/hosting.md"] + +[[item]] +id = "cli-server-install" +title = "A server you install with one command" +status = "next" +area = "servers" +plain = """Install and run a Horizon dedicated server from a terminal with a single command, no launcher needed, for \ +VPSes and headless machines.""" +dev = """A player's idea (#horizon-ideas). `horizon-server` already runs headless; left: an install script \ +(Linux/macOS shell, Windows PowerShell) that fetches the signed release, checks it, asks for or points at the \ +player's own game files and starts the server, documented in docs/hosting.md.""" +links = ["docs/hosting.md"] + +[[item]] +id = "launcher-look-menus" +title = "Game menus in the launcher's style" +status = "later" +area = "extras" +plain = """An optional look for the game's own menus and options that matches the Horizon launcher. Off by default: \ +the vanilla menus stay as they are.""" +dev = """A player's idea (#horizon-ideas). A built-in resource pack or a Horizon setting that restyles the title, \ +pause and options screens with the launcher's design tokens (crates/horizon-theme), following \ +docs/design-system.md; off by default because it changes the vanilla look.""" +links = ["docs/design-system.md"] + +[[item]] +id = "linux-packages" +title = "AppImage and a Nix flake" +status = "later" +area = "platforms" +plain = """Install Horizon on any Linux distribution as an AppImage, and on NixOS through a Nix flake.""" +dev = """A player's idea (#horizon-ideas). An AppImage of the launcher and game from the release workflow, and a \ +flake.nix packaging the release binaries with their runtime libraries (Vulkan loader, X11/Wayland, ALSA), \ +the way horizon-parity's NixOS setup runs them today.""" +links = [] diff --git a/website/data/roadmap.json b/website/data/roadmap.json index 4af2a460f..58d697257 100644 --- a/website/data/roadmap.json +++ b/website/data/roadmap.json @@ -964,13 +964,16 @@ { "id": "opengl-fallback", "title": "OpenGL for old computers", - "status": "later", + "status": "shipped", "area": "platforms", - "plain": "A way to play on old PCs and Linux machines that have no Vulkan or DirectX 12, through OpenGL.", - "dev": "Players on older hardware ask whether Vulkan is the only way. Horizon draws with wgpu, which has an OpenGL backend; what is needed is to see which features hold up on it, how fast it runs, and a launcher switch like the DirectX 12 fallback that already ships.", + "plain": "Horizon now starts on old PCs whose graphics card has no Vulkan or DirectX 12 driver: it switches to OpenGL by itself, and Options > Graphics can pick OpenGL by hand.", + "dev": "wgpu's GL backend (WGL on Windows, EGL on Linux) as the last fallback after Vulkan and DirectX 12 (#1917), sprite arrays and CPU sprite animation for drivers without compute (#1920), no indirect draws below GL 4.3 (#1936). Minimum OpenGL 3.3 core. Shader packs don't run on it yet.", "links": [ - "docs/graphics.md" - ] + "#1917", + "#1920", + "#1936" + ], + "shipped_in": "v0.2.63" }, { "id": "phones-and-browser", @@ -1202,6 +1205,37 @@ "docs/hosting.md" ], "shipped_in": "launcher-v0.15.0" + }, + { + "id": "cli-server-install", + "title": "A server you install with one command", + "status": "next", + "area": "servers", + "plain": "Install and run a Horizon dedicated server from a terminal with a single command, no launcher needed, for VPSes and headless machines.", + "dev": "A player's idea (#horizon-ideas). `horizon-server` already runs headless; left: an install script (Linux/macOS shell, Windows PowerShell) that fetches the signed release, checks it, asks for or points at the player's own game files and starts the server, documented in docs/hosting.md.", + "links": [ + "docs/hosting.md" + ] + }, + { + "id": "launcher-look-menus", + "title": "Game menus in the launcher's style", + "status": "later", + "area": "extras", + "plain": "An optional look for the game's own menus and options that matches the Horizon launcher. Off by default: the vanilla menus stay as they are.", + "dev": "A player's idea (#horizon-ideas). A built-in resource pack or a Horizon setting that restyles the title, pause and options screens with the launcher's design tokens (crates/horizon-theme), following docs/design-system.md; off by default because it changes the vanilla look.", + "links": [ + "docs/design-system.md" + ] + }, + { + "id": "linux-packages", + "title": "AppImage and a Nix flake", + "status": "later", + "area": "platforms", + "plain": "Install Horizon on any Linux distribution as an AppImage, and on NixOS through a Nix flake.", + "dev": "A player's idea (#horizon-ideas). An AppImage of the launcher and game from the release workflow, and a flake.nix packaging the release binaries with their runtime libraries (Vulkan loader, X11/Wayland, ALSA), the way horizon-parity's NixOS setup runs them today.", + "links": [] } ], "journal": [ From 2cdb6362cae1e4d88894f4fda3a4dc3b49ada9bb Mon Sep 17 00:00:00 2001 From: socksprox Date: Sun, 25 Oct 2026 22:38:40 +0100 Subject: [PATCH 183/238] Particles keep their sprites over a resource reload MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The particle engine copied the GUI atlas's sprite rects once at start; a pack or server-pack change rebuilt the atlas but not the engine, so every named and set sprite then drew a stale region — break crumbs showed other blocks and campfire smoke showed random items (#1923). The engine now re-reads the rects and the packs' sprite sets on reload, and a particle whose sprite-set index outlived the rebuild keeps its sprite instead of indexing past the new list. The reload also layers content mods over the root again, which startup does but reload forgot, so the first reload silently dropped their assets and data. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- crates/horizon-client/src/game/packs.rs | 6 ++++ crates/horizon-client/src/particles.rs | 46 +++++++++++++++++++------ 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/crates/horizon-client/src/game/packs.rs b/crates/horizon-client/src/game/packs.rs index 7629a5d25..b35eff564 100644 --- a/crates/horizon-client/src/game/packs.rs +++ b/crates/horizon-client/src/game/packs.rs @@ -142,6 +142,8 @@ impl Game<'_> { } let server = &self.server_packs.active; let root = startup_root(&vanilla, &self.settings.resource_packs, server)?; + let root = + super::mods::content_root(&root, &self.settings.mods.enabled, &self.options.dev_mods); self.reload_resources(&root) } fn reload_resources(&mut self, root: &Path) -> horizon_client::Result<()> { @@ -192,6 +194,10 @@ impl Game<'_> { let context = pack.context.clone(); self.pack = pack; self.gui = gui; + // The atlas's sprites moved: the particle engine re-reads every rect + // and the packs' sprite sets, or every particle draws a stale region. + self.particles + .reload(root, &self.gui.atlas.sprites, &self.gui.atlas.translucent); horizon_client::ui_theme::reset_atlas(); // Villager hats come from the pack's texture metadata (shared data, swapped in place). *self diff --git a/crates/horizon-client/src/particles.rs b/crates/horizon-client/src/particles.rs index be7f32551..fa9dc9c6e 100644 --- a/crates/horizon-client/src/particles.rs +++ b/crates/horizon-client/src/particles.rs @@ -1212,10 +1212,7 @@ impl ParticleEngine { requested: None, names: Vec::new(), rects: Vec::new(), - atlas: atlas - .iter() - .map(|(name, &rect)| (name.clone(), (rect, translucent.contains(name)))) - .collect(), + atlas: HashMap::new(), sets: HashMap::new(), set_sprites: Vec::new(), emitters: Vec::new(), @@ -1225,6 +1222,34 @@ impl ParticleEngine { made: Default::default(), item_sprites: HashMap::new(), }; + engine.reload(root, atlas, translucent); + engine + } + /// `onResourceManagerReload`: sprite rects and sets come from the + /// reloaded atlas and pack; a live particle keeps drawing its sprite by + /// name or index. + pub fn reload( + &mut self, + root: &Path, + atlas: &HashMap, + translucent: &HashSet, + ) { + self.atlas = atlas + .iter() + .map(|(name, &rect)| (name.clone(), (rect, translucent.contains(name)))) + .collect(); + self.rects = self + .names + .iter() + .map(|name| self.atlas.get(name).map(|&(rect, _)| rect)) + .collect(); + self.sets.clear(); + self.set_sprites.clear(); + self.item_sprites.clear(); + self.load_sprites(root); + } + /// The sprite sets of every `particles/*.json` under `root`. + fn load_sprites(&mut self, root: &Path) { let dir = root.join("assets/minecraft/particles"); for &ty in ParticleType::ALL { let Ok(bytes) = std::fs::read(dir.join(format!("{}.json", ty.name()))) else { @@ -1239,18 +1264,16 @@ impl ParticleEngine { a.iter() .filter_map(|t| t.as_str()) .map(|t| { - engine - .intern(&format!("particle/{}", t.trim_start_matches("minecraft:"))) + self.intern(&format!("particle/{}", t.trim_start_matches("minecraft:"))) }) .collect() }) .unwrap_or_default(); if !sprites.is_empty() { - engine.sets.insert(ty, engine.set_sprites.len() as u32); - engine.set_sprites.push(sprites); + self.sets.insert(ty, self.set_sprites.len() as u32); + self.set_sprites.push(sprites); } } - engine } fn intern(&mut self, name: &str) -> u32 { if let Some(i) = self.names.iter().position(|n| n == name) { @@ -1601,7 +1624,10 @@ impl ParticleEngine { return; } if let Some(set) = p.set { - let sprites = &self.set_sprites[set as usize]; + // A sprite set's index can outlive the reload that rebuilt them. + let Some(sprites) = self.set_sprites.get(set as usize) else { + return; + }; let lifetime = p.lifetime.max(1); let index = (p.age.max(0) as i64 * (sprites.len() as i64 - 1) / i64::from(lifetime)) .clamp(0, sprites.len() as i64 - 1); From b6efebc87908e113cbf555496e61e61f1c685d16 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 25 Oct 2026 21:39:14 +0000 Subject: [PATCH 184/238] Play anvil result sounds once for every player MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Deliver anvil level events to the acting player and keep their local sound out of positional sound capture. Extend the anvil LAN check to count sounds for both players using either player’s anvil result. Co-Authored-By: OpenAI Codex --- crates/horizon-client/src/game.rs | 8 +++++++- crates/horizon-client/src/game/lan.rs | 6 +++--- tools/lan_e2e.py | 20 +++++++++++++++----- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index f5803ad08..08abb2d2b 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2764,7 +2764,13 @@ impl Game<'_> { } _ => 1.0, }; - self.audio.play(name, c, volume, pitch); + if matches!(id, 1029 | 1030) { + // Clients play the anvil sound from its level event; + // capturing it also sends a duplicate sound packet. + self.audio.play_unshared(name, c, volume, pitch); + } else { + self.audio.play(name, c, volume, pitch); + } } } LevelEvent::Sound { diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index ddb959cf3..9b6754661 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -1114,9 +1114,9 @@ impl Game<'_> { for e in events { if let horizon_world::level::LevelEvent::LevelEvent { id, pos, data } = e { // The acting guest does not predict the egg's teleport - // particles, nor bone meal's growth (BoneMealItem.useOn's - // levelEvent names no player). - let except = if matches!(*id, 2015 | 1505) { + // particles, bone meal's growth, or anvil result sounds + // (these levelEvent calls name no excluded player). + let except = if matches!(*id, 2015 | 1505 | 1029 | 1030) { None } else { self.lan_acting diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 0ec7218d8..8e7985862 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -52,9 +52,9 @@ address. The script then checks, from both sides, what a guest's game used to ge - grindstone: a grindstone opens on the guest's screen; the enchanted sword the guest shift-clicks in shows the server's result, and the guest takes it with only its curse left and the experience orbs the server awards (#868); -- anvil: an anvil opens on the guest's screen; the guest repairs a sword with iron ingots and - renames it (ServerboundRenameItemPacket), takes it for the server's level cost, and the - server has the repaired, renamed sword (#868); +- anvil: each player repairs and renames a sword (ServerboundRenameItemPacket), takes it for + the server's level cost, and the server has the repaired, renamed sword (#868); + both players hear exactly one anvil sound for each result (#1907); - enchanting: an enchanting table opens on the guest's screen with the server's offers for the sword the guest puts in; the guest clicks the first offer and the server enchants the sword for a level and a lapis, and counts it in the guest's enchant_item stat (#868); @@ -1620,6 +1620,12 @@ def check_grindstone(host, guest, out): def check_anvil(host, guest, out): + # AnvilMenu.onTake emits one level event to everyone, including the user. + # Exercise both the in-frame host and a remote user (both remote with --server-thread). + return {actor.name: check_anvil_user(actor, (host, guest), out) for actor in (guest, host)} + + +def check_anvil_user(guest, listeners, out): guest('command', command='gamemode survival') guest('command', command='clear @s') guest('command', command='xp set @s 5 levels') @@ -1638,8 +1644,12 @@ def check_anvil(host, guest, out): menu = guest.raw('menu') offered = (menu['info']['anvil_cost'], station_slots(guest).get(650)) guest.shot(out, 'anvil-rename') + sounds = ('minecraft:block.anvil.use', 'minecraft:block.anvil.destroy') + before = {g.name: sum(heard(g, s) for s in sounds) for g in listeners} guest.raw('click-slot', 650, 'shift') - guest.wait(10) + guest.wait(20) + sound_counts = {g.name: sum(heard(g, s) for s in sounds) - before[g.name] for g in listeners} + assert sound_counts == {g.name: 1 for g in listeners}, f'anvil sounds: {sound_counts}' left = station_slots(guest) close_station(guest, at) sword = server_reply(guest, 'data get entity @s Inventory[{id:"minecraft:iron_sword"}].components', @@ -1653,7 +1663,7 @@ def check_anvil(host, guest, out): want = {'offered': (4, ('minecraft:iron_sword', 1)), 'left': {}, 'name': True, 'damage': True, 'levels': 1, 'ingots': 0} assert seen == want, f'the anvil on the guest: {seen} ({sword!r})' - return seen + return {**seen, 'heard': sound_counts} def check_smithing(host, guest, out): From acb68ed163deb929e1e64b8abf6f39aa9caa99bf Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 22:44:56 +0100 Subject: [PATCH 185/238] launcher: reuse prepared resources while the preparer's OUTPUT_VERSION is the same A version's resources were reused only when the preparer's sources and the exporters were byte for byte the same, so any change to crates/horizon-prepare (even one that prepares the same files) made every player prepare again. The launcher now compares the number the preparer declares for what it makes (`pub const OUTPUT_VERSION: u32` in crates/horizon-prepare/src/lib.rs, bumped by hand when the output changes and checked by CI in the follow-up). Preparers from before the number are known by a fingerprint of their sources and exporters: v0.2.49 to v0.2.64 prepare version 1's output (measured on the CI runners' preparations). Older versions are compared as before. Co-Authored-By: Claude Opus 5.5 --- launcher/src/backend.rs | 216 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 205 insertions(+), 11 deletions(-) diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index 7e5c4babb..ce9007868 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -628,17 +628,13 @@ pub fn resources_summary(prepared: bool, sounds: bool, version: &str) -> String } } -/// Whether the resources `from` prepared are the ones preparing `to` would make. The output -/// depends on the Horizon version, not only on Minecraft's: the manifest records the hashes of -/// the vanilla exporters it was made with (mods/horizon/src/test/java/.../parity), and the -/// preparer writes every file and the manifest's `schema`. So both need the same pinned -/// Minecraft jars (research-lock.json), the same preparer and the same exporter sources. The -/// preparer is crates/horizon-prepare (its sources ship in the release folder beside the -/// binary) and tools/prepare_native_client.py (the Python preparer of older versions, a wrapper -/// of the binary since); a file only one of the two versions has is a different preparer. The -/// helpers the Python preparer imported (lab.py and the test_*_parity.py modules) change -/// between releases in how they find and check the player's files, which the output does not -/// depend on; they are not compared. +/// Whether the resources `from` prepared are the ones preparing `to` would make: both pin the same +/// Minecraft jars (research-lock.json) and their preparers make the same output. A preparer +/// declares what it makes with `OUTPUT_VERSION` (crates/horizon-prepare/src/lib.rs, whose sources +/// ship in the release folder), bumped by hand when the output changes and checked by CI +/// (tools/prepare_output.py), so a release that changes the preparer's code but not its output +/// keeps the resources. Preparers from before the number are known by their sources +/// (`LEGACY_OUTPUT_VERSIONS`); older ones still are compared file by file (`same_preparer`). fn resources_compatible(from: &Path, to: &Path, manifest: &serde_json::Value) -> bool { let pins = |dir: &Path| { read_json(&dir.join("research-lock.json")).map(|lock| { @@ -651,6 +647,108 @@ fn resources_compatible(from: &Path, to: &Path, manifest: &serde_json::Value) -> if a != b || a.iter().any(|v| !v.is_string()) { return false; } + match (output_version_at(from), output_version_at(to)) { + (Some(a), Some(b)) => a == b, + (None, None) => same_preparer(from, to, manifest), + _ => false, + } +} + +/// The `OUTPUT_VERSION` of a version folder's preparer: the constant in its sources, or for a +/// preparer from before it, the number `LEGACY_OUTPUT_VERSIONS` gives its fingerprint. None for +/// an unknown older preparer. +fn output_version_at(dir: &Path) -> Option { + output_version_in(dir, &LEGACY_OUTPUT_VERSIONS) +} + +fn output_version_in(dir: &Path, legacy: &[(&str, u32)]) -> Option { + let crate_dir = dir.join(PREPARER_CRATE); + let lib = fs::read_to_string(crate_dir.join("src/lib.rs")).ok()?; + let declared = lib.lines().find_map(|line| { + line.strip_prefix("pub const OUTPUT_VERSION: u32 = ")? + .strip_suffix(';')? + .parse() + .ok() + }); + if declared.is_some() { + return declared; + } + let fingerprint = legacy_fingerprint(dir)?; + legacy + .iter() + .find(|(known, _)| *known == fingerprint) + .map(|&(_, version)| version) +} + +/// Preparers from before `OUTPUT_VERSION`, by `legacy_fingerprint`, whose output +/// tools/prepare_output.py measured: what they prepare is version 1's. +const LEGACY_OUTPUT_VERSIONS: [(&str, u32); 2] = [ + // v0.2.52 to v0.2.64 + ( + "54218ade1c058b1f15ea4094bf8c1a7893adb37b1ed5cc803ddae8e950c31704", + 1, + ), + // v0.2.49 to v0.2.51 + ( + "da9ffe7558129c38b44abf43289e81946fe9f2512e5e07272eeae0cf0b4f1254", + 1, + ), +]; + +/// The vanilla exporters those preparers ran (the `*_exporters_sha256` of their manifests), in +/// mods/horizon/src/test/java/dev/horizonlab/parity. +const LEGACY_EXPORTERS: [&str; 18] = [ + "ParityData.java", + "VanillaBlockBehaviourExporter.java", + "VanillaBlockLandingOracle.java", + "VanillaBlockStateBindingOracle.java", + "VanillaCollisionOracle.java", + "VanillaCommandParseOracle.java", + "VanillaCreativeTabsExporter.java", + "VanillaCuboidModelOracle.java", + "VanillaDamageSourceOracle.java", + "VanillaEntityAttributeOracle.java", + "VanillaEntityModelExporter.java", + "VanillaEntityTypesExporter.java", + "VanillaFaceVisibilityOracle.java", + "VanillaGameplayFactsExporter.java", + "VanillaHeightmapOracle.java", + "VanillaMobDataExporter.java", + "VanillaMovementRegistryOracle.java", + "VanillaPlayerHealthOracle.java", +]; + +/// A version folder's preparer as the launchers before `OUTPUT_VERSION` compared it: the digest +/// of its sources (`horizon_prepare::sources_sha256`) and of the exporters it ran, line endings +/// as checked out ignored. +fn legacy_fingerprint(dir: &Path) -> Option { + use sha2::Digest; + let sources = horizon_prepare::sources_sha256(&dir.join(PREPARER_CRATE))?; + let mut hash = sha2::Sha256::new(); + hash.update(sources.as_bytes()); + for name in LEGACY_EXPORTERS { + let path = format!("mods/horizon/src/test/java/dev/horizonlab/parity/{name}"); + let data: Vec = fs::read(dir.join(&path)) + .ok()? + .into_iter() + .filter(|b| *b != b'\r') + .collect(); + hash.update(format!("{path}\0{}\0", data.len())); + hash.update(&data); + } + Some(hash.finalize().iter().map(|b| format!("{b:02x}")).collect()) +} + +/// How launchers before `OUTPUT_VERSION` compared two preparers, for versions older than the +/// ones `LEGACY_OUTPUT_VERSIONS` knows. The manifest records the hashes of the vanilla exporters +/// the resources were made with (mods/horizon/src/test/java/.../parity), and the preparer writes +/// every file and the manifest's `schema`, so both need the same preparer and the same exporter +/// sources. The preparer is crates/horizon-prepare and tools/prepare_native_client.py (the +/// Python preparer of older versions, a wrapper of the binary since); a file only one of the two +/// versions has is a different preparer. The helpers the Python preparer imported (lab.py and +/// the test_*_parity.py modules) change between releases in how they find and check the +/// player's files, which the output does not depend on; they are not compared. +fn same_preparer(from: &Path, to: &Path, manifest: &serde_json::Value) -> bool { let Some(fields) = manifest.as_object() else { return false; }; @@ -3150,6 +3248,102 @@ mod tests { let _ = fs::remove_dir_all(&versions); } + #[test] + fn prepared_resources_follow_the_preparers_output_version() { + let versions = std::env::temp_dir().join(format!( + "horizon-launcher-output-test-{}", + std::process::id() + )); + let _ = fs::remove_dir_all(&versions); + let write = |tag: &str, path: &str, text: &str| { + let path = versions.join(tag).join(path); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, text).unwrap(); + }; + let lib = "crates/horizon-prepare/src/lib.rs"; + // A preparer with its sources (as releases ship them) and the exporters it runs. + let release = |tag: &str, code: &str| { + write(tag, COMPLETE, ""); + let lock = serde_json::json!({"minecraft": {"version": "26.3", "client_sha1": "c", "server_sha1": "s"}}); + write(tag, "research-lock.json", &lock.to_string()); + write(tag, "crates/horizon-prepare/Cargo.toml", "[package]"); + write(tag, "crates/horizon-prepare/build.rs", "fn main() {}"); + write(tag, lib, code); + for name in LEGACY_EXPORTERS { + write( + tag, + &format!("mods/horizon/src/test/java/dev/horizonlab/parity/{name}"), + name, + ); + } + }; + let prepared = |tag: &str| { + write( + tag, + &format!(".local/horizon-client/26.3/{RESOURCES_MANIFEST}"), + r#"{"schema": 7}"#, + ); + write(tag, ".local/horizon-client/26.3/facts.json", tag); + }; + let facts = |tag: &str| { + fs::read_to_string(resources_at(&versions.join(tag)).join("facts.json")).ok() + }; + + // The preparer's code changed, not what it makes: the resources are kept. + release( + "v0.3.0", + "pub const OUTPUT_VERSION: u32 = 1;\nfn prepare() {}", + ); + prepared("v0.3.0"); + release( + "v0.3.1", + "pub const OUTPUT_VERSION: u32 = 1;\nfn prepare() { faster() }", + ); + assert_eq!(reuse_prepared(&versions, "v0.3.1"), Some("v0.3.0".into())); + assert_eq!(facts("v0.3.1").as_deref(), Some("v0.3.0")); + // Its output changed, so the number did: prepare again. + release( + "v0.3.2", + "pub const OUTPUT_VERSION: u32 = 2;\nfn prepare() {}", + ); + assert_eq!(reuse_prepared(&versions, "v0.3.2"), None); + assert_eq!(facts("v0.3.2"), None); + // Another Minecraft jar, same number: prepare again. + release( + "v0.3.3", + "pub const OUTPUT_VERSION: u32 = 1;\nfn prepare() {}", + ); + let lock = serde_json::json!({"minecraft": {"version": "26.3", "client_sha1": "other", "server_sha1": "s"}}); + write("v0.3.3", "research-lock.json", &lock.to_string()); + assert_eq!(reuse_prepared(&versions, "v0.3.3"), None); + + // A preparer from before the number is known by its fingerprint. + release("v0.2.64", "fn prepare() {}"); + let fingerprint = legacy_fingerprint(&versions.join("v0.2.64")).unwrap(); + let legacy = [(fingerprint.as_str(), 1)]; + let at = |tag: &str| output_version_in(&versions.join(tag), &legacy); + assert_eq!(at("v0.2.64"), Some(1)); + assert_eq!(at("v0.3.2"), Some(2)); + // Line endings as checked out don't count; an exporter's or the sources' change does. + write( + "v0.2.64", + "mods/horizon/src/test/java/dev/horizonlab/parity/ParityData.java", + "ParityData.java\r", + ); + assert_eq!(at("v0.2.64"), Some(1)); + write( + "v0.2.64", + "mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCollisionOracle.java", + "changed", + ); + assert_eq!(at("v0.2.64"), None); + release("v0.2.65", "fn prepare() { changed() }"); + assert_eq!(at("v0.2.65"), None); + // A folder without the preparer's sources has no number. + assert_eq!(output_version_in(&versions.join("missing"), &legacy), None); + let _ = fs::remove_dir_all(&versions); + } + #[test] fn prepared_versions_get_the_run_time_reports_without_preparing_again() { let versions = std::env::temp_dir().join(format!( From 116a7f7dcafc4e36d14b75600f402446ec8056ac Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 22:48:43 +0100 Subject: [PATCH 186/238] horizon-prepare: OUTPUT_VERSION, the number of what it prepares, checked by CI Launchers copy a player's prepared resources to a new release while its preparer declares the same OUTPUT_VERSION (the launcher side is its own pull request). The number is bumped by hand; CI keeps it honest: the parity job already prepares with every change to the preparer, and tools/prepare_output.py check now fails when that preparation's digest is not the one crates/horizon-prepare/output-versions.txt gives the current number, or when the file changed other than by appending a version. The digest canonicalises what differs between two runs of the same preparer (JSON key order, creative tab tag order, lambda class addresses, logs, the sounds): 10 preparations by 5 preparer versions on the three Mac runners give one digest, and a Linux preparation by an older preparer gives the Mac runners' digest for it. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/parity.yml | 4 + crates/horizon-prepare/output-versions.txt | 3 + crates/horizon-prepare/src/lib.rs | 32 ++++ docs/release-flow.md | 4 + tests/test_prepare_output.py | 64 ++++++++ tools/prepare_output.py | 176 +++++++++++++++++++++ 6 files changed, 283 insertions(+) create mode 100644 crates/horizon-prepare/output-versions.txt create mode 100644 tests/test_prepare_output.py create mode 100755 tools/prepare_output.py diff --git a/.github/workflows/parity.yml b/.github/workflows/parity.yml index a18fc5019..0588c12a3 100644 --- a/.github/workflows/parity.yml +++ b/.github/workflows/parity.yml @@ -37,6 +37,10 @@ jobs: run: bash tools/ci/runner-env.sh - name: Vanilla data, and resources prepared by this preparer run: bash tools/ci/vanilla-data.sh + - name: The prepared output is the one the preparer's OUTPUT_VERSION says + env: + BASE_SHA: ${{ inputs.base }} + run: python3 tools/prepare_output.py check --cache --base "$BASE_SHA" - name: The version constants are the pinned jar's run: python3 tools/gen_version_consts.py --check - name: The harnesses this change can affect diff --git a/crates/horizon-prepare/output-versions.txt b/crates/horizon-prepare/output-versions.txt new file mode 100644 index 000000000..71a531fab --- /dev/null +++ b/crates/horizon-prepare/output-versions.txt @@ -0,0 +1,3 @@ +# OUTPUT_VERSION (src/lib.rs) and the digest of what it prepares (python3 tools/prepare_output.py digest), +# one line per version, never edited: a change to the prepared files appends the next version. +1 0a608bf4115a8ed2e2c4f5f2b2609dc10c54e63479af8e56fabaa10af1060389 diff --git a/crates/horizon-prepare/src/lib.rs b/crates/horizon-prepare/src/lib.rs index c7e443c7d..5ea1121df 100644 --- a/crates/horizon-prepare/src/lib.rs +++ b/crates/horizon-prepare/src/lib.rs @@ -31,6 +31,15 @@ pub type Log<'a> = &'a dyn Fn(&str); /// The manifest's `schema`: the prepared files' layout, which the launcher compares. pub const SCHEMA: i64 = 7; +/// What this preparer makes. The launcher copies a prepared version's resources to a newly installed +/// version whose preparer has the same number (and the same pinned jars), so a release that changes +/// this crate without changing its output asks nobody to prepare again. Bump it, and append the new +/// digest to output-versions.txt, when the prepared files change: CI prepares with every change to +/// the preparer and fails when the output is not the one output-versions.txt gives this number +/// (`python3 tools/prepare_output.py check`). The launcher reads this line from the version +/// folder's sources: keep it on one line, as it is. +pub const OUTPUT_VERSION: u32 = 1; + /// Digest of this crate's sources (build.rs): the launcher prepares a version in-process when /// that version ships these same sources, and runs the version's own binary otherwise. pub const SOURCES_SHA256: &str = env!("HORIZON_PREPARE_SOURCES_SHA256"); @@ -1209,6 +1218,29 @@ mod tests { fs::remove_dir_all(dir).unwrap(); } + #[test] + fn output_versions_end_at_the_output_version() { + let text = include_str!("../output-versions.txt"); + let versions: Vec<(u32, &str)> = text + .lines() + .filter(|l| !l.trim().is_empty() && !l.starts_with('#')) + .map(|l| { + let (n, digest) = l.split_once(' ').unwrap(); + (n.parse().unwrap(), digest.trim()) + }) + .collect(); + let numbers: Vec = versions.iter().map(|v| v.0).collect(); + assert_eq!(numbers, (1..=OUTPUT_VERSION).collect::>()); + for (i, (_, digest)) in versions.iter().enumerate() { + assert_eq!(digest.len(), 64); + assert!(!versions[..i].iter().any(|(_, d)| d == digest)); + } + // The launcher finds the number with this line in a version folder's sources. + let lib = include_str!("lib.rs"); + let line = format!("pub const OUTPUT_VERSION: u32 = {OUTPUT_VERSION};"); + assert!(lib.lines().any(|l| l == line)); + } + #[test] fn the_source_digest_is_the_one_build_rs_computed() { assert_eq!( diff --git a/docs/release-flow.md b/docs/release-flow.md index 4fb0209d9..1413cde01 100644 --- a/docs/release-flow.md +++ b/docs/release-flow.md @@ -39,6 +39,10 @@ copy is: the Mac mini. Its runner runs as the maintainer's account and keeps its depend only on the Minecraft version, are kept once in `shared-local/sounds//` (copied from a folder `horizon-prepare sounds` made from the maintainer's own game; the Mac mini has no official launcher of its own) and cloned into each preparation (not linked: vanilla's packs refuse symbolic links), for the audio harnesses. + `tools/prepare_output.py check` then compares what was prepared with `crates/horizon-prepare/output-versions.txt`: + launchers keep a player's prepared resources across releases whose preparer declares the same `OUTPUT_VERSION` + (`crates/horizon-prepare/src/lib.rs`), so a change to the prepared files fails here until it bumps the number + and appends the new digest (the error gives both lines). 3. `tools/parity.py --changed ` picks the harnesses whose traced dependencies (their script, Python modules, Java oracles and every Rust source their probes compile) changed since the merge base, plus any harness nothing is recorded about yet, and runs those that have no pass for their current inputs. A change to diff --git a/tests/test_prepare_output.py b/tests/test_prepare_output.py new file mode 100644 index 000000000..00c489fbb --- /dev/null +++ b/tests/test_prepare_output.py @@ -0,0 +1,64 @@ +import json +import tempfile +import unittest +from pathlib import Path + +from tools import prepare_output + + +class PrepareOutputTests(unittest.TestCase): + def preparation(self, root: Path, files: dict[str, bytes]) -> Path: + for rel, data in files.items(): + path = root / rel + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(data) + return root + + def test_what_varies_between_runs_does_not_count(self): + manifest = {'schema': 7, 'data_version': 5023, 'render_files_sha256': {'a.json': 'x'}} + a = { + 'horizon-resources.json': json.dumps(manifest).encode(), + 'render-definitions.json': b'{"a": 1, "b": [1, 2]}', + 'creative-inventory.json': b'{"tags": [["b", "a"]]}', + 'block-behaviours.json': b'{"c": "Foo$$Lambda/0x0000012a"}', + 'export.log': b'run 1', + 'assets/minecraft/sounds/a.ogg': b'one', + } + manifest['render_files_sha256'] = {'a.json': 'y'} + b = { + 'horizon-resources.json': json.dumps(manifest).encode(), + 'render-definitions.json': b'{"b": [1, 2], "a": 1}', + 'creative-inventory.json': b'{"tags": [["a", "b"]]}', + 'block-behaviours.json': b'{"c": "Foo$$Lambda/0x00000ff0"}', + 'export.log': b'run 2', + 'assets/minecraft/sounds/a.ogg': b'two', + } + with tempfile.TemporaryDirectory() as tmp: + first = prepare_output.digest(self.preparation(Path(tmp) / 'a', a)) + second = prepare_output.digest(self.preparation(Path(tmp) / 'b', b)) + self.assertEqual(first, second) + # The data does, and so do the manifest's schema and the files there are. + (Path(tmp) / 'b/render-definitions.json').write_bytes(b'{"b": [2, 1], "a": 1}') + self.assertNotEqual(first, prepare_output.digest(Path(tmp) / 'b')) + (Path(tmp) / 'b/render-definitions.json').write_bytes(b'{"b": [1, 2], "a": 1}') + (Path(tmp) / 'b/horizon-resources.json').write_text(json.dumps({**manifest, 'schema': 8})) + self.assertNotEqual(first, prepare_output.digest(Path(tmp) / 'b')) + (Path(tmp) / 'b/horizon-resources.json').write_text(json.dumps(manifest)) + (Path(tmp) / 'b/new.nbt').write_bytes(b'') + self.assertNotEqual(first, prepare_output.digest(Path(tmp) / 'b')) + + def test_the_versions_file_ends_at_the_constant(self): + versions = prepare_output.parse_versions('# comment\n1 aa\n2 bb\n') + self.assertEqual(versions, [(1, 'aa'), (2, 'bb')]) + self.assertEqual(prepare_output.problems(versions, 2), []) + self.assertTrue(prepare_output.problems(versions, 1)) + self.assertTrue(prepare_output.problems([(1, 'aa'), (3, 'bb')], 3)) + self.assertTrue(prepare_output.problems([(1, 'aa'), (2, 'aa')], 2)) + root = Path(prepare_output.__file__).resolve().parent.parent + current = prepare_output.parse_versions((root / prepare_output.VERSIONS).read_text()) + constant = prepare_output.CONSTANT.search((root / prepare_output.LIB).read_text()) + self.assertEqual(prepare_output.problems(current, int(constant.group(1))), []) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/prepare_output.py b/tools/prepare_output.py new file mode 100755 index 000000000..81ed156cb --- /dev/null +++ b/tools/prepare_output.py @@ -0,0 +1,176 @@ +#!/usr/bin/env python3 +"""The prepared output's version: what keeps `OUTPUT_VERSION` (crates/horizon-prepare/src/lib.rs) honest. + +The launcher copies a prepared version's resources (`.local/horizon-client/`) to a newly installed +one when both pin the same Minecraft jars and their preparers declare the same `OUTPUT_VERSION`, so a release +that changes the preparer's code without changing what it makes asks nobody to prepare again. The number is +bumped by hand; this tool catches a change to the output without a bump. + +crates/horizon-prepare/output-versions.txt lists every `OUTPUT_VERSION` with the digest of what it prepares. +It only grows: the last line is the current version, and a change to the output appends a line. + + python3 tools/prepare_output.py digest [RESOURCES] the digest of a preparation + python3 tools/prepare_output.py check [--base SHA] [--cache] [RESOURCES] + fails when RESOURCES (default: this checkout's .local/horizon-client/, as CI prepares it) is + not what the last line says, or when the file changed other than by appending since SHA. --cache keeps + the digest beside RESOURCES (CI: a preparation never changes once made; it takes ~30 s to compute) + +The digest covers every file of the preparation, canonicalised where the preparer is not deterministic between +runs (verified across the CI runners): JSON is compared as data (key order, the order of each `tags` list of +creative-inventory.json, and the addresses in `$$Lambda/0x...` class names don't count); logs, compiled classes, +the sounds (copied from the player's launcher by another step) and the manifest's file hashes (raw bytes) don't +count. The manifest's own scalar fields (schema, data version, jar hashes, file count) do. +""" + +from __future__ import annotations + +import argparse +import hashlib +import json +import os +import re +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +VERSIONS = 'crates/horizon-prepare/output-versions.txt' +LIB = 'crates/horizon-prepare/src/lib.rs' +MANIFEST = 'horizon-resources.json' +LAMBDA = re.compile(r'\$\$Lambda/0x[0-9a-fA-F]+') +CONSTANT = re.compile(r'^pub const OUTPUT_VERSION: u32 = (\d+);', re.M) + + +def canonical_json(value, key=''): + if isinstance(value, dict): + return {k: canonical_json(v, k) for k, v in value.items()} + if isinstance(value, list): + items = [canonical_json(v, key) for v in value] + if key == 'tags' and all(isinstance(x, list) and all(isinstance(t, str) for t in x) for x in items): + items = [sorted(x) for x in items] + return items + if isinstance(value, str): + return LAMBDA.sub('$$Lambda', value) + return value + + +def canonical(rel: str, data: bytes) -> bytes: + if rel == MANIFEST: + manifest = json.loads(data) + scalars = {k: v for k, v in manifest.items() if not isinstance(v, (dict, list))} + return json.dumps(scalars, sort_keys=True).encode() + if rel.endswith('.json'): + try: + value = json.loads(data) + except ValueError: + return data + return json.dumps(canonical_json(value), sort_keys=True).encode() + return data + + +def skipped(rel: str) -> bool: + parts = rel.split('/') + return rel.endswith(('.log', '.class')) or parts[:3] == ['assets', 'minecraft', 'sounds'] \ + or rel == 'assets/minecraft/sounds.json' + + +def digest(resources: Path) -> tuple[str, int]: + """The digest of a preparation, and how many files it covers.""" + if not (resources / MANIFEST).is_file(): + raise SystemExit(f'{resources} is not a preparation: no {MANIFEST}') + files = [] + for dirpath, dirs, names in os.walk(resources): + dirs.sort() + for name in names: + path = Path(dirpath) / name + rel = path.relative_to(resources).as_posix() + if path.is_symlink() or skipped(rel): + continue + files.append(rel) + total = hashlib.sha256() + for rel in sorted(files): + data = hashlib.sha256(canonical(rel, (resources / rel).read_bytes())).hexdigest() + total.update(f'{rel}\0{data}\n'.encode()) + return total.hexdigest(), len(files) + + +def parse_versions(text: str) -> list[tuple[int, str]]: + out = [] + for line in text.splitlines(): + line = line.strip() + if not line or line.startswith('#'): + continue + number, value = line.split() + out.append((int(number), value)) + return out + + +def problems(versions: list[tuple[int, str]], constant: int | None) -> list[str]: + found = [] + if not versions: + return [f'{VERSIONS} lists no version'] + if [n for n, _ in versions] != list(range(1, len(versions) + 1)): + found.append(f'{VERSIONS}: the versions must be 1, 2, 3... in order') + if len({d for _, d in versions}) != len(versions): + found.append(f'{VERSIONS}: two versions with the same digest') + if constant != versions[-1][0]: + found.append(f'{LIB} says OUTPUT_VERSION = {constant}, {VERSIONS} ends at {versions[-1][0]}') + return found + + +def default_resources() -> Path: + lock = json.loads((ROOT / 'research-lock.json').read_text()) + return ROOT / '.local/horizon-client' / lock['minecraft']['version'] + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__.split('\n\n')[0]) + parser.add_argument('command', choices=['digest', 'check']) + parser.add_argument('resources', nargs='?', type=Path) + parser.add_argument('--base', help='the commit the change is based on (check: the file only grew since)') + parser.add_argument('--cache', action='store_true', help='keep the digest beside the preparation') + args = parser.parse_args() + resources = (args.resources or default_resources()).resolve() + tool = hashlib.sha256(Path(__file__).read_bytes()).hexdigest()[:16] + cache = resources.parent / f'.output-digest-{resources.name}-{tool}' + if args.cache and cache.is_file(): + value, count = cache.read_text().split() + else: + value, count = digest(resources) + if args.cache: + cache.write_text(f'{value} {count}\n') + if args.command == 'digest': + print(value) + return 0 + + versions = parse_versions((ROOT / VERSIONS).read_text()) + match = CONSTANT.search((ROOT / LIB).read_text()) + found = problems(versions, int(match.group(1)) if match else None) + if args.base: + shown = subprocess.run(['git', 'show', f'{args.base}:{VERSIONS}'], cwd=ROOT, capture_output=True, text=True) + if shown.returncode == 0: + before = parse_versions(shown.stdout) + if versions[:len(before)] != before: + found.append(f'{VERSIONS} changed lines it already had (since {args.base[:12]}): ' + 'a new output gets a new version, appended') + current = versions[-1] if versions else (0, '') + if value != current[1]: + known = [n for n, d in versions if d == value] + if known: + found.append(f'the preparation is OUTPUT_VERSION {known[0]}\'s output, but OUTPUT_VERSION is {current[0]}') + else: + found.append( + f'the prepared output changed ({count} files, digest {value}) but OUTPUT_VERSION is still ' + f'{current[0]}. Players would keep resources prepared by the old preparer. Bump it:\n' + f' set `pub const OUTPUT_VERSION: u32 = {current[0] + 1};` in {LIB}\n' + f' append `{current[0] + 1} {value}` to {VERSIONS}\n' + ' (if the change is only a nondeterminism this tool should ignore, teach canonical() instead)') + for problem in found: + print(f'::error::{problem}' if os.environ.get('GITHUB_ACTIONS') else f'error: {problem}', file=sys.stderr) + if not found: + print(f'prepared output: OUTPUT_VERSION {current[0]} ({count} files, {value[:16]})') + return 1 if found else 0 + + +if __name__ == '__main__': + sys.exit(main()) From 0b49da246674bef76848ffe3ecb7517db48f0faf Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 25 Oct 2026 22:07:58 +0000 Subject: [PATCH 187/238] Fix lead drops when leashed mobs die Follow Leashable.tickLeash for dying mobs and cover trader/player-held llama leads through the death animation. Co-Authored-By: Codex --- .../examples/probe/mob_ai_probe.rs | 16 ++++++++++++++++ crates/horizon-client/src/game/leads.rs | 2 ++ crates/horizon-client/src/mobs/leash.rs | 9 +++++++++ .../horizonlab/parity/VanillaMobAiOracle.java | 8 ++++++++ tools/test_mob_ai_parity.py | 15 ++++++++++++++- 5 files changed, 49 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/examples/probe/mob_ai_probe.rs b/crates/horizon-client/examples/probe/mob_ai_probe.rs index 157baa74c..49f56c250 100644 --- a/crates/horizon-client/examples/probe/mob_ai_probe.rs +++ b/crates/horizon-client/examples/probe/mob_ai_probe.rs @@ -598,6 +598,7 @@ fn print(out: &mut String, t: i32, i: usize, m: &Mob, player_down: bool, now: i6 let _ = writeln!(out, " removed {reason}"); return; } + let _ = write!(out, "\tleashed {}", u8::from(m.extra.leash.is_some())); let p = m.position; let v = m.velocity; let _ = write!( @@ -977,6 +978,7 @@ pub fn main() -> horizon_client::Result<()> { } // Set once the brain exists (below). x if x.starts_with("cooldown=") => {} + "dead" => m.health = 0.0, "leashed" => m.extra.leash = Some(horizon_client::mobs::leash::Holder::Player), x if x.starts_with("leashto=") => { let n: u32 = x[8..].parse().unwrap(); @@ -1097,6 +1099,7 @@ pub fn main() -> horizon_client::Result<()> { } }; } + let mut dropped_leads = 0; for t in 1..=s.ticks { world.game_time += 1; if s.daycycle { @@ -1208,6 +1211,16 @@ pub fn main() -> horizon_client::Result<()> { if !alive { m.removed = true; } + // Apply the same detach/drop event as Game::break_leash. + for event in &events[from..] { + if let MobEvent::LeashBreak { mob, drop, .. } = event + && let Some(m) = mobs.iter_mut().find(|m| m.id == *mob) + && horizon_client::mobs::leash::remove(m).is_some() + && *drop + { + dropped_leads += 1; + } + } horizon_client::mobs::apply_brain_events(&mut mobs, &mut events, from); villager_events( &mut mobs, @@ -1427,6 +1440,9 @@ pub fn main() -> horizon_client::Result<()> { world.game_time, ); } + if s.name.starts_with("leash_death_") { + let _ = writeln!(out, "{t} leads {dropped_leads}"); + } for e in &events { if let MobEvent::Sound(name, _, volume, pitch) = e { let _ = writeln!( diff --git a/crates/horizon-client/src/game/leads.rs b/crates/horizon-client/src/game/leads.rs index 3a5112967..8867157ee 100644 --- a/crates/horizon-client/src/game/leads.rs +++ b/crates/horizon-client/src/game/leads.rs @@ -79,6 +79,8 @@ impl Game<'_> { if horizon_client::mobs::leash::remove(m).is_none() { return; } + // Leashable.tickLeash gates a dying entity's lead drop on entity_drops. + let drop = drop && (m.alive() || self.ui.rules.bool("entity_drops")); let at = [m.position.x, m.position.y, m.position.z]; if sound { self.lead_sound("break", at); diff --git a/crates/horizon-client/src/mobs/leash.rs b/crates/horizon-client/src/mobs/leash.rs index b7c5011ba..3bd0513d7 100644 --- a/crates/horizon-client/src/mobs/leash.rs +++ b/crates/horizon-client/src/mobs/leash.rs @@ -198,6 +198,15 @@ pub fn tick(m: &mut Mob, w: &MobWorld, events: &mut Vec) { m.extra.leash_spin = 0.0; return; }; + // Leashable.tickLeash: a dying entity cannot interact with the level. + if !m.alive() { + events.push(MobEvent::LeashBreak { + mob: m.id, + drop: true, + sound: false, + }); + return; + } let Some(state) = holder_state(w, holder) else { // The holder is gone: the lead drops. if !matches!(holder, Holder::Player) { diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaMobAiOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaMobAiOracle.java index 5808fec0d..eb5235adf 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaMobAiOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaMobAiOracle.java @@ -604,6 +604,7 @@ public final class VanillaMobAiOracle { case "tame" -> { if (mob instanceof net.minecraft.world.entity.animal.equine.AbstractHorse horse) horse.setTamed(true); } case "strength" -> method(net.minecraft.world.entity.animal.equine.Llama.class, "setStrength", int.class) .invoke(mob, Integer.parseInt(v)); + case "dead" -> mob.setHealth(0.0F); case "leashed", "leashto" -> { // Leashed once every mob exists (below). } @@ -807,6 +808,12 @@ public final class VanillaMobAiOracle { for (BlockPos h : heartPositions) printHeart(t, level, h); for (String[] b : s.blocks) if (b[0].equals("chest")) printChest(t, s, level, b); for (String[] b : s.blocks) if (isHive(b)) printHive(t, level, b); + if (s.name.startsWith("leash_death_")) { + int leads = level.getEntitiesOfClass(ItemEntity.class, ENTS.get(0).getBoundingBox().inflate(32.0), + item -> item.getItem().is(net.minecraft.world.item.Items.LEAD)) + .stream().mapToInt(item -> item.getItem().getCount()).sum(); + out.println(t + " leads " + leads); + } for (String sound : SOUNDS) out.println(t + " sound " + sound); for (var w : watched.entrySet()) { BlockState now = level.getBlockState(w.getKey()); @@ -1008,6 +1015,7 @@ public final class VanillaMobAiOracle { return; } Mob m = (Mob) e; + sb.append("\tleashed ").append(((net.minecraft.world.entity.Leashable) m).isLeashed() ? 1 : 0); sb.append("\tp ").append(d(m.getX())).append(',').append(d(m.getY())).append(',').append(d(m.getZ())); Vec3 v = m.getDeltaMovement(); sb.append("\tv ").append(d(v.x)).append(',').append(d(v.y)).append(',').append(d(v.z)); diff --git a/tools/test_mob_ai_parity.py b/tools/test_mob_ai_parity.py index 3e6ce74df..87a7a1403 100644 --- a/tools/test_mob_ai_parity.py +++ b/tools/test_mob_ai_parity.py @@ -191,6 +191,16 @@ def scenarios(): 'mob minecraft:wandering_trader 0.5 -60 0.5 0 104 persistent', 'mob minecraft:trader_llama 3.5 -60 1.5 0 105 persistent leashto=0 strength=2', 'mob minecraft:trader_llama 3.5 -60 -1.5 0 106 persistent leashto=0 strength=4']) + # Death must detach immediately and drop exactly one lead throughout the corpse animation. + add('leash_death_trader_llama', 10, [ + 'player 14.5 -60 0.5', + 'mob minecraft:wandering_trader 0.5 -60 0.5 0 104 persistent', + 'mob minecraft:trader_llama 3.5 -60 1.5 0 105 persistent leashto=0 strength=2 dead', + ]) + add('leash_death_player_llama', 10, [ + 'player 0.5 -60 0.5', + 'mob minecraft:llama 3.5 -60 0.5 0 101 persistent leashed strength=3 dead', + ]) # Copper golems sorting chests (TransportItemsBetweenContainers): from a copper chest to a chest, # skipping a chest without the carried item, queueing at a chest another golem has open, a double # chest, a blocked copper chest, a part-full chest, and idling with nothing to carry. @@ -358,6 +368,9 @@ def parse(path): if parts[1] == 'hive': cur.setdefault('hives', {}).setdefault(t, []).append(parts[2]) continue + if parts[1] == 'leads': + cur.setdefault('leads', {}).setdefault(t, []).append(parts[2]) + continue if parts[1] == 'chest': cur.setdefault('chests', {}).setdefault(t, []).append(parts[2]) continue @@ -455,7 +468,7 @@ def compare(vanilla, rust, fields=None): clean = False break # Sounds, watched block changes, chest contents and hives: first differing tick. - for kind in ('sounds', 'blocks', 'chests', 'hives'): + for kind in ('sounds', 'blocks', 'chests', 'hives', 'leads'): va, ra = v.get(kind, {}), r.get(kind, {}) for t in sorted(set(va) | set(ra)): if va.get(t, []) != ra.get(t, []): From e3bd6266884838537ed7579f3a9b58c7adbc1127 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 23:08:15 +0100 Subject: [PATCH 188/238] Mod API: what the extras need as mods: corner panels, frame stats, mappings, buttons The built-in extras become official mods; these are the calls the FPS counter and keystrokes overlay need to look and behave the same: - render.set-corner(corner, w, h, shapes): a box the game places with its own corner stacking (under the status effects, with the overlays and hud panels), clear-corner; text-width(text) in the HUD font (advances for U+0000..U+024F, 6 otherwise); frame-stats(): the game's FPS counter (fps, min, 1% low). - input.mapping(name): a movement or click mapping's key label and whether it is held; clicks-per-second() for attack and use. - ui: a button widget (200 x 20, select(id, 0)) for settings pages. - The game keeps its FPS and click counters running while a mod runs, and refreshes the snapshot every frame for mods with a frame hook. - The ui test guest gains /corner and /readings, with a sandbox test. Co-Authored-By: Claude Opus 5.5 --- crates/cargo-horizon-mod/src/check.rs | 2 +- crates/horizon-client/src/game/extras.rs | 20 ++- crates/horizon-client/src/game/mod_render.rs | 28 ++-- crates/horizon-client/src/game/mods.rs | 102 ++++++++++++- crates/horizon-mod-sdk/wit/horizon.wit | 52 +++++++ crates/horizon-mods/src/runtime.rs | 139 ++++++++++++++---- crates/horizon-mods/tests/sandbox.rs | 46 +++++- crates/horizon-mods/tests/ui_guest/src/lib.rs | 64 +++++++- docs/api/mods-reference.md | 46 +++++- docs/api/mods.md | 2 + docs/mods.md | 6 +- 11 files changed, 447 insertions(+), 60 deletions(-) diff --git a/crates/cargo-horizon-mod/src/check.rs b/crates/cargo-horizon-mod/src/check.rs index 87ae3c982..b22c0eeaf 100644 --- a/crates/cargo-horizon-mod/src/check.rs +++ b/crates/cargo-horizon-mod/src/check.rs @@ -200,7 +200,7 @@ fn play(path: &Path, report: &mut Report) -> Result<(), String> { }), // The GUI of a 1280 x 720 window at scale 3. gui_size: (427.0, 240.0), - held: None, + ..Snapshot::default() }); mods.tick(t); for _ in 0..FRAMES_PER_TICK { diff --git a/crates/horizon-client/src/game/extras.rs b/crates/horizon-client/src/game/extras.rs index cee173fda..4664f0c6f 100644 --- a/crates/horizon-client/src/game/extras.rs +++ b/crates/horizon-client/src/game/extras.rs @@ -70,7 +70,8 @@ impl Game<'_> { _ => return, }; self.extras.mouse[i] = pressed; - if pressed && self.settings.extras.keystrokes { + // Mods read the clicks too (`input.clicks-per-second`). + if pressed && (self.settings.extras.keystrokes || self.mods.running()) { let now = self.extras_now(); self.extras.cps[i].click(now); } @@ -87,13 +88,28 @@ impl Game<'_> { self.extras.mouse = [false; 2]; } self.update_minimap(); - if self.settings.extras.fps { + // Mods read the counter too (`render.frame-stats`). + if self.settings.extras.fps || self.mods.running() { let now = self.extras_now(); self.extras.fps.record(now, elapsed.as_secs_f32() * 1000.0); } else { self.extras.fps.clear(); } } + /// The frame counter for mods: fps, min, 1% low. + #[cfg(feature = "mods")] + pub(super) fn frame_reading(&self) -> (u32, u32, u32) { + let r = self.extras.fps.reading(); + (r.fps, r.min, r.low) + } + /// Attack and use clicks in the last second, and whether each is held. + #[cfg(feature = "mods")] + pub(super) fn mouse_reading(&mut self) -> ((u32, u32), [bool; 2]) { + let now = self.extras_now(); + let [l, r] = &mut self.extras.cps; + let cps = (l.per_second(now) as u32, r.per_second(now) as u32); + (cps, self.extras.mouse) + } /// Auto-reconnect: a connection to `address` as `name` starts. pub(super) fn extras_connecting(&mut self, address: &str, name: &str) { self.extras.reconnect.connecting(address, name); diff --git a/crates/horizon-client/src/game/mod_render.rs b/crates/horizon-client/src/game/mod_render.rs index 1c3a20621..931aec641 100644 --- a/crates/horizon-client/src/game/mod_render.rs +++ b/crates/horizon-client/src/game/mod_render.rs @@ -86,23 +86,29 @@ impl Game<'_> { impl Game<'_> { /// The running mods' HUD shapes, in order, over the HUD. pub(super) fn draw_mod_shapes(&self, batch: &mut HudBatch) { - let atlas = &self.gui.atlas; for shape in self.mods.mods.hud_shapes() { + self.draw_shapes(batch, std::slice::from_ref(shape), (0.0, 0.0)); + } + } + /// Shapes with their GUI coordinates moved by `(ox, oy)`. + pub(super) fn draw_shapes(&self, batch: &mut HudBatch, shapes: &[Shape], (ox, oy): (f32, f32)) { + let atlas = &self.gui.atlas; + for shape in shapes { match shape { Shape::Fill(f) => { let r = f.rect; - batch.fill(atlas, r.x, r.y, r.w, r.h, argb(f.color)); + batch.fill(atlas, ox + r.x, oy + r.y, r.w, r.h, argb(f.color)); } Shape::Quad(q) => { let (a, b, c, d) = q.corners; - let corners = [[a.x, a.y], [b.x, b.y], [c.x, c.y], [d.x, d.y]]; + let corners = [a, b, c, d].map(|p| [ox + p.x, oy + p.y]); batch.fill_quad(atlas, corners, argb(q.color)); } Shape::Sprite(s) => { let r = s.rect; let name = s.sprite.strip_prefix("minecraft:").unwrap_or(&s.sprite); let name = format!("gui/sprites/{name}"); - batch.sprite_scaled(atlas, &name, r.x, r.y, r.w, r.h, argb(s.color)); + batch.sprite_scaled(atlas, &name, ox + r.x, oy + r.y, r.w, r.h, argb(s.color)); } Shape::Item(i) => { if self.res.items().get(&i.item).is_none() { @@ -110,21 +116,23 @@ impl Game<'_> { } let start = batch.vertices.len(); let stack = ItemStack::new(i.item.as_str(), 1); - self.draw_item_model(batch, &stack, i.at.x, i.at.y); - batch.scale_since(start, i.at.x, i.at.y, i.scale, i.scale); + let (x, y) = (ox + i.at.x, oy + i.at.y); + self.draw_item_model(batch, &stack, x, y); + batch.scale_since(start, x, y, i.scale, i.scale); } Shape::Text(t) => { let text = Text::literal(t.text.clone()); - let x = if t.centered { - t.at.x - (HudBatch::text_width(atlas, &t.text) / 2.0).floor() + let centre = if t.centered { + (HudBatch::text_width(atlas, &t.text) / 2.0).floor() } else { - t.at.x + 0.0 }; + let x = ox + t.at.x - centre; batch.text_styled( atlas, &text, x, - t.at.y, + oy + t.at.y, argb(t.color | 0xFF00_0000), t.shadow, ); diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs index 1c62046ea..9d6015ad7 100644 --- a/crates/horizon-client/src/game/mods.rs +++ b/crates/horizon-client/src/game/mods.rs @@ -21,6 +21,7 @@ use { extras::Corner, extras_hud, items::ItemStack, + keybinds, mod_list::Installed, options_ui::Page, text::{self, Text}, @@ -51,8 +52,32 @@ pub(super) struct ModsState { /// A `ui` mod's screen or settings page while it is up. #[cfg(feature = "mods")] screen: Option, + /// The HUD font's advances for `render.text-width`, made once. + #[cfg(feature = "mods")] + font_widths: std::sync::Arc<[f32]>, } +/// The mappings `input.mapping` answers for, with the default key their +/// binding is translated to (none: a mouse button, attack or use). +#[cfg(feature = "mods")] +const MAPPINGS: [(&str, Option); 9] = { + use winit::keyboard::KeyCode as K; + [ + ("key.forward", Some(K::KeyW)), + ("key.left", Some(K::KeyA)), + ("key.back", Some(K::KeyS)), + ("key.right", Some(K::KeyD)), + ("key.jump", Some(K::Space)), + ("key.sneak", Some(K::ShiftLeft)), + ("key.sprint", Some(K::ControlLeft)), + ("key.attack", None), + ("key.use", None), + ] +}; +/// Characters `render.text-width` knows exactly: U+0000 to U+024F. +#[cfg(feature = "mods")] +const FONT_CHARS: u32 = 0x250; + /// A `ui` mod's own screen, or its settings page from Options > Mods: the /// widgets the mod pushed, laid out and drawn with the menus' own widgets. #[cfg(feature = "mods")] @@ -83,10 +108,10 @@ impl ModsState { } false } - /// Whether a mod draws a HUD panel. + /// Whether a mod draws a HUD panel or a corner panel. pub(super) fn has_panels(&self) -> bool { #[cfg(feature = "mods")] - if self.mods.panels().next().is_some() { + if self.mods.panels().next().is_some() || self.mods.corner_panels().next().is_some() { return true; } false @@ -298,8 +323,8 @@ impl Game<'_> { self.start_mods(); } } - /// What mods read this tick. - fn mod_snapshot(&self) -> Snapshot { + /// What mods read this tick (or frame, for mods with a `frame` hook). + fn mod_snapshot(&mut self) -> Snapshot { let player = self.player.as_ref().map(|p| Player { x: p.position.x, y: p.position.y, @@ -314,11 +339,35 @@ impl Game<'_> { game_time: self.world.game_time.max(0) as u64, day_time: self.world.day_time.max(0) as u64, }); + let (cps, mouse) = self.mouse_reading(); + // The movement and click mappings, as the keystroke overlay reads them: + // keys by the default key their binding is translated to. + let mappings = MAPPINGS + .iter() + .map(|&(name, key)| { + let label = keybinds::display_name(self.settings.keys.get(name)).plain(); + let down = match key { + Some(k) => self.keys.contains(&k), + None => mouse[usize::from(name == "key.use")], + }; + (name, label, down) + }) + .collect(); + if self.mods.font_widths.is_empty() { + let atlas = &self.gui.atlas; + self.mods.font_widths = (0..FONT_CHARS) + .map(|c| char::from_u32(c).map_or(6.0, |c| atlas.glyph_cell(c).1)) + .collect(); + } Snapshot { player, clock, gui_size: self.gui_size(), held: self.mod_held(), + frame: self.frame_reading(), + mappings, + cps, + font_widths: self.mods.font_widths.clone(), } } /// A game tick. @@ -350,6 +399,10 @@ impl Game<'_> { if !self.mods.mods.is_running() { return; } + if self.mods.mods.wants_frames() { + let snapshot = self.mod_snapshot(); + self.mods.mods.set_snapshot(snapshot); + } self.mods.mods.frame(elapsed.as_secs_f32()); self.show_mod_output(); } @@ -438,6 +491,20 @@ impl Game<'_> { .collect(); extras_hud::draw_lines(batch, &self.gui.atlas, corners, corner, &lines); } + // Capability `render`'s corner panels, placed the same way. + for panel in self.mods.mods.corner_panels() { + let corner = match panel.corner { + horizon_mods::RenderCorner::TopLeft => Corner::TopLeft, + horizon_mods::RenderCorner::TopRight => Corner::TopRight, + horizon_mods::RenderCorner::BottomLeft => Corner::BottomLeft, + horizon_mods::RenderCorner::BottomRight => Corner::BottomRight, + }; + if self.debug && corner == Corner::TopLeft { + continue; + } + let at = corners.place(corner, panel.width, panel.height); + self.draw_shapes(batch, &panel.shapes, at); + } } /// A `ui` screen's title and widgets as the mod last pushed them; None @@ -470,7 +537,7 @@ impl Game<'_> { for w in widgets { let wh = match w { UiWidget::Text(_) => 12.0, - UiWidget::Item(_) => 20.0, + UiWidget::Item(_) | UiWidget::Button(_) => 20.0, UiWidget::List(l) => (l.rows.len().min(6) as f32 * 12.0) + 8.0, }; // Nothing may cover Done; a crowded mod loses what won't fit. @@ -559,6 +626,21 @@ impl Game<'_> { let mut list_i = 0usize; for (w, &(wy, wh)) in widgets.iter().zip(&layout) { match w { + UiWidget::Button(b) => { + let hover = ui::hovered(self.cursor, x, wy, MOD_UI_COLUMN, 20.0); + let label = Text::literal(b.label.clone()); + ui::button( + batch, + atlas, + x, + wy, + MOD_UI_COLUMN, + 20.0, + &label, + true, + hover, + ); + } UiWidget::Text(t) => { let color = if t.color == 0 { u32::MAX @@ -678,6 +760,16 @@ impl Game<'_> { let layout = self.mod_ui_layout(h, &widgets); let mut list_i = 0usize; for (w, &(wy, wh)) in widgets.iter().zip(&layout) { + // A button is `select(id, 0)`. + if let UiWidget::Button(b) = w + && ui::hovered(cursor, x, wy, MOD_UI_COLUMN, 20.0) + { + self.audio.play_button_click(); + let owner = self.mods.screen.as_ref().map_or(0, |ms| ms.owner); + self.mods.mods.ui_select(owner, b.id, 0); + self.show_mod_output(); + return; + } let UiWidget::List(l) = w else { continue }; let scroll = self .mods diff --git a/crates/horizon-mod-sdk/wit/horizon.wit b/crates/horizon-mod-sdk/wit/horizon.wit index 1560ec3a7..b787ff675 100644 --- a/crates/horizon-mod-sdk/wit/horizon.wit +++ b/crates/horizon-mod-sdk/wit/horizon.wit @@ -120,9 +120,20 @@ interface ui { selected: option, } /// A thing on a mod's screen or settings page. + /// A button as the options pages have them (200 × 20): a click is + /// `ui-events::select(id, 0)`. Mods cycle its label for a choice + /// ("Corner: Top Left"). + record button-widget { + /// The mod's own name for the widget. + id: u32, + /// Up to 256 characters. + label: string, + } variant widget { /// Text the player reads. text(text-widget), + /// A button. + button(button-widget), /// An item's icon and name. item(item-widget), /// Rows the player picks from. @@ -203,6 +214,20 @@ interface input { held: func() -> option; /// The events since the last call, oldest first. poll: func() -> list; + /// A mapping as the player has bound it. + record mapping-state { + /// The bound key's name as Controls shows it: `W`, `Space`, `LMB`. + label: string, + /// Held down now (as of this frame for a mod with a `frame` hook, + /// else this tick). + down: bool, + } + /// A movement or click mapping as the player has bound it: `key.forward`, + /// `key.left`, `key.back`, `key.right`, `key.jump`, `key.sneak`, + /// `key.sprint`, `key.attack` or `key.use`. None for another name. + mapping: func(name: string) -> option; + /// Clicks of the attack and use buttons in the last second. + clicks-per-second: func() -> tuple; } /// Capability `render` (experimental: the manifest needs @@ -210,6 +235,13 @@ interface input { /// own renderer, kept until the mod replaces them (no call per frame). A mod /// never supplies shader code. interface render { + /// A corner of the screen, as `hud`'s panels use them. + enum corner { + top-left, + top-right, + bottom-left, + bottom-right, + } /// A rectangle in GUI pixels: the top left corner and the size. record rect { x: f32, @@ -279,6 +311,26 @@ interface render { /// turns, the player doesn't, and the player's Distortion Effects option /// scales it (0 turns it off). shake: func(strength: f32, seconds: f32); + /// Replaces the mod's corner panel: shapes in GUI pixels from the top + /// left of a `width` × `height` box that the game places in `corner`, + /// stacked with the game's overlays and the other mods' panels (under + /// the status effects at the top right), as `hud`'s panels are. + set-corner: func(corner: corner, width: f32, height: f32, shapes: list); + /// Removes the mod's corner panel. + clear-corner: func(); + /// The width of one line of text in GUI pixels, in the HUD's font: exact + /// for Latin letters (U+0000 to U+024F), 6 per other character. + text-width: func(text: string) -> f32; + record frame-rate { + /// Frames drawn in the last second. + fps: u32, + /// The slowest frame's rate, and the 1% low (the 99th percentile + /// frame time's rate), over the last 5 seconds. + min: u32, + low: u32, + } + /// The game's frame counter, recomputed twice a second. + frame-stats: func() -> frame-rate; } /// What the game calls. Only the hooks `init` asks for are ever called. diff --git a/crates/horizon-mods/src/runtime.rs b/crates/horizon-mods/src/runtime.rs index 881eaf0ae..f33839fe7 100644 --- a/crates/horizon-mods/src/runtime.rs +++ b/crates/horizon-mods/src/runtime.rs @@ -37,7 +37,7 @@ use bindings::{ /// The widget model a `ui` screen or settings page shows (WIT `interface /// ui`), for the game to draw with its menus' own widgets. pub use ui_bindings::horizon::mods::ui::{ - ItemWidget, ListWidget, Screen as UiScreen, TextWidget, Widget as UiWidget, + ButtonWidget, ItemWidget, ListWidget, Screen as UiScreen, TextWidget, Widget as UiWidget, }; use ui_bindings::{UiHost, horizon::mods::ui}; /// Capability `render`'s imports (docs/mods.md): the game draws the shapes. @@ -52,8 +52,8 @@ use render_bindings::horizon::mods::input; pub use render_bindings::horizon::mods::input::{Event as InputEvent, HeldItem, ItemUse}; /// What capability `render` asks the game to draw (WIT `interface render`). pub use render_bindings::horizon::mods::render::{ - FillShape, ItemShape, Point as RenderPoint, QuadShape, Rect as RenderRect, Shape, SpriteShape, - TextShape, + Corner as RenderCorner, FillShape, FrameRate, ItemShape, Point as RenderPoint, QuadShape, + Rect as RenderRect, Shape, SpriteShape, TextShape, }; use render_bindings::{RenderHost, horizon::mods::render}; @@ -90,6 +90,16 @@ pub struct Snapshot { pub gui_size: (f32, f32), /// The main hand's stack (capability `input`'s `held`). pub held: Option, + /// The game's frame counter: fps, min, 1% low (`render.frame-stats`). + pub frame: (u32, u32, u32), + /// The movement and click mappings `input.mapping` answers for: + /// (mapping, the bound key's label, held). + pub mappings: Vec<(&'static str, String, bool)>, + /// Attack and use clicks in the last second. + pub cps: (u32, u32), + /// The HUD font's advance per character from U+0000 + /// (`render.text-width`); empty: 6 each. + pub font_widths: Arc<[f32]>, } /// What mods asked the game to show since the last [`Mods::drain`]. @@ -177,6 +187,8 @@ struct State { ui: UiState, /// Capability `render`'s HUD shapes, drawn until replaced. shapes: Vec, + /// Capability `render`'s corner panel. + corner: Option, input: InputState, /// Capability `storage`'s folder; made on first `set`. data_dir: PathBuf, @@ -318,6 +330,7 @@ impl Mods { panel: None, ui: UiState::default(), shapes: Vec::new(), + corner: None, input: InputState::default(), data_dir, storage_budget: STORAGE_WRITE_BUDGET, @@ -510,6 +523,19 @@ impl Mods { .iter() .filter_map(|e| e.run.as_ref()?.store.data().panel.as_ref()) } + /// The running mods' corner panels (capability `render`), in load order. + pub fn corner_panels(&self) -> impl Iterator { + self.entries + .iter() + .filter_map(|e| e.run.as_ref()?.store.data().corner.as_ref()) + } + /// Some running mod has a `frame` hook: the game refreshes the snapshot + /// every frame for it. + pub fn wants_frames(&self) -> bool { + self.entries + .iter() + .any(|e| e.run.as_ref().is_some_and(|r| r.hooks.frame)) + } /// Some running mod claimed items (capability `input`). pub fn claims_any(&self) -> bool { self.inputs().any(|i| !i.claimed.is_empty()) @@ -728,6 +754,10 @@ fn clip_widgets(widgets: Vec) -> Vec { t.text = clip(t.text); UiWidget::Text(t) } + UiWidget::Button(mut b) => { + b.label = clip(b.label); + UiWidget::Button(b) + } UiWidget::Item(mut i) => { i.item = clip(i.item); i.name = clip(i.name); @@ -849,6 +879,16 @@ impl input::Host for State { fn poll(&mut self) -> Vec { self.input.events.drain(..).collect() } + fn mapping(&mut self, name: String) -> Option { + let (_, label, down) = self.snapshot.mappings.iter().find(|m| m.0 == name)?; + Some(input::MappingState { + label: label.clone(), + down: *down, + }) + } + fn clicks_per_second(&mut self) -> (u32, u32) { + self.snapshot.cps + } } /// HUD shapes one mod may keep (capability `render`). @@ -856,33 +896,76 @@ const SHAPES: usize = 1024; /// The longest jolt `render.shake` makes, in seconds. const SHAKE_SECONDS: f32 = 10.0; +/// A mod's corner panel: shapes in a `width` × `height` box the game +/// places in `corner`, stacked with the other overlays. +#[derive(Clone, Debug)] +pub struct CornerPanel { + pub corner: RenderCorner, + pub width: f32, + pub height: f32, + pub shapes: Vec, +} + +fn clip_shapes(shapes: Vec) -> Vec { + shapes + .into_iter() + .take(SHAPES) + .map(|shape| match shape { + Shape::Sprite(mut s) => { + s.sprite = clip(s.sprite); + Shape::Sprite(s) + } + Shape::Item(mut i) => { + i.item = clip(i.item); + i.scale = if i.scale.is_finite() { + i.scale.clamp(0.0, 16.0) + } else { + 1.0 + }; + Shape::Item(i) + } + Shape::Text(mut t) => { + t.text = clip(t.text); + t.color &= 0xFF_FFFF; + Shape::Text(t) + } + other => other, + }) + .collect() +} + impl render::Host for State { fn set_hud(&mut self, shapes: Vec) { - self.shapes = shapes - .into_iter() - .take(SHAPES) - .map(|shape| match shape { - Shape::Sprite(mut s) => { - s.sprite = clip(s.sprite); - Shape::Sprite(s) - } - Shape::Item(mut i) => { - i.item = clip(i.item); - i.scale = if i.scale.is_finite() { - i.scale.clamp(0.0, 16.0) - } else { - 1.0 - }; - Shape::Item(i) - } - Shape::Text(mut t) => { - t.text = clip(t.text); - t.color &= 0xFF_FFFF; - Shape::Text(t) - } - other => other, - }) - .collect(); + self.shapes = clip_shapes(shapes); + } + fn set_corner(&mut self, corner: RenderCorner, width: f32, height: f32, shapes: Vec) { + let size = |v: f32| { + if v.is_finite() { + v.clamp(0.0, 1024.0) + } else { + 0.0 + } + }; + self.corner = Some(CornerPanel { + corner, + width: size(width), + height: size(height), + shapes: clip_shapes(shapes), + }); + } + fn clear_corner(&mut self) { + self.corner = None; + } + fn text_width(&mut self, text: String) -> f32 { + let widths = &self.snapshot.font_widths; + text.chars() + .map(|c| widths.get(c as usize).copied().unwrap_or(6.0)) + .sum::() + .ceil() + } + fn frame_stats(&mut self) -> FrameRate { + let (fps, min, low) = self.snapshot.frame; + FrameRate { fps, min, low } } fn gui_size(&mut self) -> (f32, f32) { self.snapshot.gui_size diff --git a/crates/horizon-mods/tests/sandbox.rs b/crates/horizon-mods/tests/sandbox.rs index ba1736e5e..a9de4f55b 100644 --- a/crates/horizon-mods/tests/sandbox.rs +++ b/crates/horizon-mods/tests/sandbox.rs @@ -91,7 +91,7 @@ fn player() -> Snapshot { day_time: 24000 + 6000, }), gui_size: (427.0, 240.0), - held: None, + ..Snapshot::default() } } @@ -412,7 +412,7 @@ fn ui_screens_settings_and_events() { assert_eq!(mods.settings_widgets(0).len(), 3); assert!(mods.entries()[0].has_ui_settings()); mods.ui_settings_opened(0); - assert_eq!(mods.settings_widgets(0).len(), 1); + assert_eq!(mods.settings_widgets(0).len(), 2); // `/ui_close` closes the mod's own screen. assert!(mods.command("ui_close")); assert!(mods.drain().contains(&Output::CloseScreen { owner: 0 })); @@ -424,7 +424,6 @@ fn render_shapes_and_shakes() { let mut mods = load("horizon_mods_test_ui_guest.hmod"); mods.set_snapshot(Snapshot { gui_size: (427.0, 240.0), - held: None, ..Snapshot::default() }); assert_eq!(mods.hud_shapes().count(), 0); @@ -509,6 +508,47 @@ fn input_claims_keys_and_events() { assert_eq!(chat(&mut mods), ["holding minecraft:stick x3 in 2"]); } +#[test] +fn corner_panels_text_width_and_readings() { + let mut mods = load("horizon_mods_test_ui_guest.hmod"); + // A font of 5-pixel glyphs, 3-pixel spaces. + let mut widths = vec![5.0f32; 0x250]; + widths[' ' as usize] = 3.0; + mods.set_snapshot(Snapshot { + frame: (60, 41, 52), + mappings: vec![("key.forward", "W".into(), true)], + cps: (7, 2), + font_widths: widths.into(), + ..Snapshot::default() + }); + assert!(mods.command("corner")); + let panels: Vec<_> = mods.corner_panels().cloned().collect(); + assert_eq!(panels.len(), 1); + // "60 fps": five glyphs and a space, plus the box's 4. + assert_eq!( + (panels[0].width, panels[0].height), + (5.0 * 5.0 + 3.0 + 4.0, 12.0) + ); + assert_eq!(panels[0].shapes.len(), 2); + assert!(matches!( + panels[0].corner, + horizon_mods::RenderCorner::TopRight + )); + assert!(matches!( + &mods.settings_widgets(0)[0], + horizon_mods::UiWidget::Button(b) if b.id == 3 && b.label == "Corner: Top Right" + )); + mods.drain(); + assert!(mods.command("readings")); + assert_eq!( + mods.drain(), + [Output::Chat { + name: "Test UI Guest".into(), + text: "60 41 52; forward W true; cps 7 2; other true".into(), + }] + ); +} + #[test] fn content_only_mods_load_without_an_engine() { let manifest = r#" diff --git a/crates/horizon-mods/tests/ui_guest/src/lib.rs b/crates/horizon-mods/tests/ui_guest/src/lib.rs index be2b8fcc4..ae6e4dfc4 100644 --- a/crates/horizon-mods/tests/ui_guest/src/lib.rs +++ b/crates/horizon-mods/tests/ui_guest/src/lib.rs @@ -2,7 +2,8 @@ //! of each widget, `/ui_settings` sets a settings page, `/ui_close` closes //! the screen; the `ui-events` calls answer in chat so the host sees them. //! `/render_hud`, `/render_clear` and `/render_shake` test `render`; -//! `/input_claim`, `/input_poll` and `/input_held` test `input`. +//! `/input_claim`, `/input_poll` and `/input_held` test `input`; +//! `/corner` and `/readings` the calls the official extras mods use. use horizon_mod_sdk::{ Mod, Registration, chat, input::{self, Event}, @@ -92,6 +93,8 @@ impl Mod for Guest { "input_claim", "input_poll", "input_held", + "corner", + "readings", ] .into_iter() .fold(Registration::new(), Registration::command); @@ -107,6 +110,49 @@ impl Mod for Guest { "ui_close" => ui::close_screen(), "render_hud" => render::set_hud(&shapes()), "render_clear" => render::set_hud(&[]), + // An FPS-counter-like box: as wide as its text plus 4. + "corner" => { + let text = "60 fps"; + let w = render::text_width(text) + 4.0; + render::set_corner( + render::Corner::TopRight, + w, + 12.0, + &[ + Shape::Fill(FillShape { + rect: Rect { + x: 0.0, + y: 0.0, + w, + h: 12.0, + }, + color: 0x78000000, + }), + Shape::Text(TextShape { + text: text.into(), + at: Point { x: 2.0, y: 2.0 }, + color: 0xFFFFFF, + centered: false, + shadow: true, + }), + ], + ); + ui::set_settings(&[Widget::Button(ui::ButtonWidget { + id: 3, + label: "Corner: Top Right".into(), + })]); + } + "readings" => { + let f = render::frame_stats(); + let fwd = input::mapping("key.forward") + .map_or("none".into(), |m| format!("{} {}", m.label, m.down)); + let (l, r) = input::clicks_per_second(); + let none = input::mapping("key.inventory").is_none(); + chat::show(&format!( + "{} {} {}; forward {fwd}; cps {l} {r}; other {none}", + f.fps, f.min, f.low + )); + } "input_claim" => { input::claim_items(&["minecraft:stick".into()]); input::register_key("wheel", "Open the wheel", "key.keyboard.r")?; @@ -147,11 +193,17 @@ impl Mod for Guest { chat::show("closed"); } fn ui_settings_opened(&mut self) { - ui::set_settings(&[Widget::Text(TextWidget { - id: 1, - text: "fresh".into(), - color: 0x33CC33, - })]); + ui::set_settings(&[ + Widget::Text(TextWidget { + id: 1, + text: "fresh".into(), + color: 0x33CC33, + }), + Widget::Button(ui::ButtonWidget { + id: 3, + label: "Corner: Top Right".into(), + }), + ]); chat::show("settings opened"); } } diff --git a/docs/api/mods-reference.md b/docs/api/mods-reference.md index b1b52164a..e371f3975 100644 --- a/docs/api/mods-reference.md +++ b/docs/api/mods-reference.md @@ -177,13 +177,21 @@ Rows the player picks one of, as an options page lists choices: a pick is `ui-ev | `rows` | `list` | The rows' texts, at most 512, each up to 256 characters. | | `selected` | `option` | The row to mark as the current choice, if any. | -### variant `widget` +### record `button-widget` -A thing on a mod's screen or settings page. +A thing on a mod's screen or settings page. A button as the options pages have them (200 × 20): a click is `ui-events::select(id, 0)`. Mods cycle its label for a choice ("Corner: Top Left"). + +| Field | Type | | +| --- | --- | --- | +| `id` | `u32` | The mod's own name for the widget. | +| `label` | `string` | Up to 256 characters. | + +### variant `widget` | Case | Payload | | | --- | --- | --- | | `text` | `text-widget` | Text the player reads. | +| `button` | `button-widget` | A button. | | `item` | `item-widget` | An item's icon and name. | | `list` | `list-widget` | Rows the player picks from. | @@ -246,12 +254,23 @@ The main hand's stack. | `key-down` | `string` | A key `register-key` named went down or up while playing (no screen open). The game's own mapping on that key still runs. | | `key-up` | `string` | | +### record `mapping-state` + +A mapping as the player has bound it. + +| Field | Type | | +| --- | --- | --- | +| `label` | `string` | The bound key's name as Controls shows it: `W`, `Space`, `LMB`. | +| `down` | `bool` | Held down now (as of this frame for a mod with a `frame` hook, else this tick). | + | Function | Signature | What it does | | --- | --- | --- | | `claim-items` | `func(ids: list)` | The items whose use the mod handles instead of the game, by id (at most 64). Replaces the previous list. | | `register-key` | `func(name: string, label: string, key: string) -> result<_, string>` | A key of the mod's: `name` comes back in `key-down` and `key-up`, `label` says what it does, `key` is the key as options.txt writes it (`key.keyboard.r`). At most 16; an unknown key is an error. | | `held` | `func() -> option` | The main hand's stack, as of this tick; none when it is empty. | | `poll` | `func() -> list` | The events since the last call, oldest first. | +| `mapping` | `func(name: string) -> option` | A movement or click mapping as the player has bound it: `key.forward`, `key.left`, `key.back`, `key.right`, `key.jump`, `key.sneak`, `key.sprint`, `key.attack` or `key.use`. None for another name. | +| `clicks-per-second` | `func() -> tuple` | Clicks of the attack and use buttons in the last second. | ## `render` @@ -259,6 +278,17 @@ Access: capability `render`. Capability `render` (experimental: the manifest needs `experimental = ["render"]`): shapes the game draws for the mod with its own renderer, kept until the mod replaces them (no call per frame). A mod never supplies shader code. +### enum `corner` + +A corner of the screen, as `hud`'s panels use them. + +| Case | | +| --- | --- | +| `top-left` | | +| `top-right` | | +| `bottom-left` | | +| `bottom-right` | | + ### record `rect` A rectangle in GUI pixels: the top left corner and the size. @@ -331,11 +361,23 @@ Any four-cornered shape (a wheel's segment, an arrow): the corners in order roun | `item` | `item-shape` | | | `text` | `text-shape` | | +### record `frame-rate` + +| Field | Type | | +| --- | --- | --- | +| `fps` | `u32` | Frames drawn in the last second. | +| `min` | `u32` | The slowest frame's rate, and the 1% low (the 99th percentile frame time's rate), over the last 5 seconds. | +| `low` | `u32` | | + | Function | Signature | What it does | | --- | --- | --- | | `set-hud` | `func(shapes: list)` | Replaces the mod's HUD shapes: up to 1024, drawn every frame in order over the HUD (after the hotbar, under chat and screens), hidden with the HUD (F1). An empty list removes them. | | `gui-size` | `func() -> tuple` | The screen's size in GUI pixels (width, height), as of this tick. | | `shake` | `func(strength: f32, seconds: f32)` | Shakes the camera: a jolt of `strength` (0 to 1, 1 the hardest) that fades out over `seconds` (at most 10). Jolts add up to 1. The view turns, the player doesn't, and the player's Distortion Effects option scales it (0 turns it off). | +| `set-corner` | `func(corner: corner, width: f32, height: f32, shapes: list)` | Replaces the mod's corner panel: shapes in GUI pixels from the top left of a `width` × `height` box that the game places in `corner`, stacked with the game's overlays and the other mods' panels (under the status effects at the top right), as `hud`'s panels are. | +| `clear-corner` | `func()` | Removes the mod's corner panel. | +| `text-width` | `func(text: string) -> f32` | The width of one line of text in GUI pixels, in the HUD's font: exact for Latin letters (U+0000 to U+024F), 6 per other character. | +| `frame-stats` | `func() -> frame-rate` | The game's frame counter, recomputed twice a second. | ## `events` diff --git a/docs/api/mods.md b/docs/api/mods.md index 29da98122..356ed353c 100644 --- a/docs/api/mods.md +++ b/docs/api/mods.md @@ -95,6 +95,8 @@ What real mods need next, in the order modpacks and servers need it: ## Changelog +- Experimental: `ui` button widget; `render.set-corner`, `text-width`, `frame-stats`; `input.mapping`, + `clicks-per-second` (what the official FPS, keystrokes and zoom mods need). - Experimental `input` (`experimental = ["input"]`): `claim-items`, `register-key`, `held`, `poll` (use-start, use-stop, key-down, key-up). - Experimental `render` (`experimental = ["render"]`): HUD shapes (fill, quad, sprite, item, text), `gui-size`, camera diff --git a/docs/mods.md b/docs/mods.md index 01de73fdb..d92c88fa6 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -86,9 +86,9 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | `chat` | `chat` | `show(text)`: a line only the local player sees, after the mod's name (8 per call) | | `hud` | `hud` | `set-panel(corner, lines)`, `clear()`: up to 16 lines of 256 characters | | `storage` | `storage` | `get(key)`, `set(key, value)`, `delete(key)`: a per-mod folder of up to 64 files, 4 MiB total, at most 1 MiB written per call | -| `ui` | `ui` | `open-screen`, `close-screen`, `set-settings`: a screen the mod owns and a page in Options > Mods, of text, item and list widgets the game lays out and draws with its own menus' code. **Experimental:** the manifest needs `experimental = ["ui"]` | -| `render` | `render` | `set-hud(shapes)`: up to 1024 fills, four-cornered shapes, GUI sprites, items and text, kept on the HUD (over the hotbar, under chat and screens, hidden with F1) until replaced; `gui-size()`; `shake(strength, seconds)`: the view turns, the player doesn't, scaled by Distortion Effects. The game's own HUD renderer draws them: a mod never supplies shader code. **Experimental:** `experimental = ["render"]` | -| `input` | `input` | `claim-items(ids)`: the use button on those items is the mod's (`use-start`, `use-stop` with the ticks held; the game doesn't place, eat or swing); `register-key(name, label, key)`: up to 16 keys (`key.keyboard.r`), heard while playing as `key-down` / `key-up` (the game's own mapping on that key still runs); `held()`; `poll()` returns the events, at most 256 kept. **Experimental:** `experimental = ["input"]` | +| `ui` | `ui` | `open-screen`, `close-screen`, `set-settings`: a screen the mod owns and a page in Options > Mods, of text, button, item and list widgets the game lays out and draws with its own menus' code. **Experimental:** the manifest needs `experimental = ["ui"]` | +| `render` | `render` | `set-hud(shapes)`: up to 1024 fills, four-cornered shapes, GUI sprites, items and text, kept on the HUD (over the hotbar, under chat and screens, hidden with F1) until replaced; `set-corner(corner, w, h, shapes)`: a box the game places in a corner, stacked with its overlays and other panels, as `hud`'s panels are; `text-width(text)`; `frame-stats()`: the game's FPS, min and 1% low; `gui-size()`; `shake(strength, seconds)`: the view turns, the player doesn't, scaled by Distortion Effects. The game's own HUD renderer draws them: a mod never supplies shader code. **Experimental:** `experimental = ["render"]` | +| `input` | `input` | `claim-items(ids)`: the use button on those items is the mod's (`use-start`, `use-stop` with the ticks held; the game doesn't place, eat or swing); `register-key(name, label, key)`: up to 16 keys (`key.keyboard.r`), heard while playing as `key-down` / `key-up` (the game's own mapping on that key still runs); `held()`; `mapping(name)`: a movement or click mapping's key label and whether it is held; `clicks-per-second()`; `poll()` returns the events, at most 256 kept. **Experimental:** `experimental = ["input"]` | | Hook (export) | When | |---|---| From 8463325648f90045f83808dd201c07bfad2a264f Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 22:28:03 +0100 Subject: [PATCH 189/238] CI: Linux runners for the parity job (desktop, ThinkPad), paused while a game runs runner-env.sh and vanilla-data.sh find Python 3.13, the pinned JDK and sccache on NixOS; linux-runner.sh installs a runner as a systemd user service with its own screen; runner-pause.py stops it from taking jobs while a game runs. Co-Authored-By: Claude Opus 5.5 --- tools/ci/linux-runner.sh | 191 +++++++++++++++++++++++++++++++++++++++ tools/ci/runner-env.sh | 32 ++++++- tools/ci/runner-pause.py | 167 ++++++++++++++++++++++++++++++++++ tools/ci/vanilla-data.sh | 8 +- 4 files changed, 392 insertions(+), 6 deletions(-) create mode 100755 tools/ci/linux-runner.sh create mode 100755 tools/ci/runner-pause.py diff --git a/tools/ci/linux-runner.sh b/tools/ci/linux-runner.sh new file mode 100755 index 000000000..0cb677c87 --- /dev/null +++ b/tools/ci/linux-runner.sh @@ -0,0 +1,191 @@ +#!/usr/bin/env bash +# A Horizon CI runner (labels self-hosted, Linux, X64, horizon-game and the host's name) on a Linux machine of the +# maintainer's (NixOS: ludodesktop, ludothinkpad), as a systemd user service of the account that holds his +# Minecraft files, like the Mac mini's runners (docs/release-flow.md). Run it on that machine, as that user: +# +# bash tools/ci/linux-runner.sh install # register (gh login), install, start; again to update the scripts +# bash tools/ci/linux-runner.sh status # the services, the runner's state, the pause watcher's last lines +# bash tools/ci/linux-runner.sh uninstall # stop and unregister +# +# Everything lives in ~/.actions-runners/horizon (never in ~/code): the runner, its own rustup and cargo homes, +# the work folder, and copies of this script and runner-pause.py taken at install time (a pull request's checkout +# never runs as the service). The service runs nice, in horizon-ci.slice (low CPU and I/O weight, memory capped), +# on its own virtual screen: vscreen (a headless sway) where it is installed, else Xvfb from nixpkgs with Mesa's +# Vulkan drivers. runner-pause.py pauses it while a game runs (horizon-runner-pause.service). +# HORIZON_SHARED_SOURCE: the imported vanilla data (default ~/horizon-local/shared-local, else +# ~/code/horizon/.local); RUNNER_JOBS: cargo jobs and harnesses at once (default 2/3 of the threads). +set -euo pipefail +repo=ludovic111/horizon +dir=$HOME/.actions-runners/horizon +units=${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user +version=2.338.0 +sha=af4b794c1bc41d73d40535e3fe092a39f9679cd8d965954c2aca25a05ca41d32 +here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) + +# Nix packages the jobs need, kept alive by GC roots in $dir/nix. +nix_out() { nix-build '' -A "$1" -o "$dir/nix/$2" > /dev/null && readlink -f "$dir/nix/$2"; } + +# The runner is a .NET program: on NixOS its native libraries (libstdc++, ICU, OpenSSL) come from nix-ld. +export LD_LIBRARY_PATH=/run/current-system/sw/share/nix-ld/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} +# NixOS has no /bin/bash: the runner's scripts (again after each of its self-updates) get a portable one. +fix_shebangs() { sed -i '1s|^#!/bin/bash|#!/usr/bin/env bash|' "$dir"/*.sh "$dir"/bin/*.sh; } + +display_env() { + # The jobs' screen. Never the maintainer's: the user manager hands every service his DISPLAY and WAYLAND_DISPLAY. + if command -v vscreen > /dev/null; then + VSCREEN=ci-horizon vscreen env + else + echo "unset WAYLAND_DISPLAY; export DISPLAY=:$(cat "$dir/display")" + fi +} + +install() { + local name=${RUNNER_NAME:-$(hostname)-horizon} jobs=${RUNNER_JOBS:-$(( $(nproc) * 2 / 3 ))} + local source=${HORIZON_SHARED_SOURCE:-} + if [ -z "$source" ]; then + for candidate in "$HOME/horizon-local/shared-local" "$HOME/code/horizon/.local"; do + [ -d "$candidate/minecraft" ] && { source=$candidate; break; } + done + fi + [ -n "$source" ] && [ -d "$source/minecraft" ] || { echo "no imported Minecraft (HORIZON_SHARED_SOURCE)"; exit 1; } + mkdir -p "$dir/nix" "$units" + cp "$here/linux-runner.sh" "$here/runner-pause.py" "$dir/" + + if [ ! -f "$dir/.runner" ]; then + local cache=$HOME/.cache/gha-runner/actions-runner-linux-x64-$version.tar.gz + if ! echo "$sha $cache" | sha256sum -c --quiet - > /dev/null 2>&1; then + mkdir -p "$(dirname "$cache")" + curl -fsSL -o "$cache" "https://github.com/actions/runner/releases/download/v$version/actions-runner-linux-x64-$version.tar.gz" + echo "$sha $cache" | sha256sum -c --quiet - + fi + tar xzf "$cache" -C "$dir" + fix_shebangs + gh api -X POST "repos/$repo/actions/runners/registration-token" --jq .token | + (cd "$dir" && bash ./config.sh --unattended --replace --url "https://github.com/$repo" --token "$(cat)" \ + --name "$name" --labels "${RUNNER_LABELS:-horizon-game,$(hostname)}" --work _work > /dev/null) + fi + + # Libraries for building (pkg-config) and running (dlopen) the game, the JDK pinned by research-lock.json, sccache. + local alsa udev java sccache + alsa=$(nix_out alsa-lib.dev alsa-dev) + udev=$(nix_out systemd.dev systemd-dev) + java=$(nix_out "jdk$(sed -n 's/.*"java_major": *\([0-9]*\).*/\1/p' "$here/../../research-lock.json" | head -1)_headless" jdk)/lib/openjdk + sccache=$(nix_out sccache sccache) + local libs=/run/opengl-driver/lib:/run/current-system/sw/share/nix-ld/lib + if ! command -v vscreen > /dev/null; then + local mesa x + mesa=$(nix_out mesa mesa) + x=$(nix_out xvfb-run xvfb-run) + for lib in libx11 libxcursor libxrandr libxi libxext libxinerama libxrender libxxf86vm libxcb libxkbcommon \ + vulkan-loader libGL; do + libs=$libs:$(nix_out "$lib" "$lib")/lib + done + libs=$mesa/lib:$libs + echo "VK_DRIVER_FILES=$mesa/share/vulkan/icd.d/intel_icd.x86_64.json:$mesa/share/vulkan/icd.d/lvp_icd.x86_64.json" \ + > "$dir/.env.display" + echo "$x/bin" > "$dir/.path.display" + fi + cat > "$dir/.env" <> "$dir/.env"; else echo "VSCREEN=ci-horizon" >> "$dir/.env"; fi + echo "$dir/cargo/bin:$(cat "$dir/.path.display" 2>/dev/null || true)${dir}/nix/sccache/bin:$java/bin:$HOME/.local/bin:/run/wrappers/bin:/etc/profiles/per-user/$USER/bin:/run/current-system/sw/bin" \ + | sed 's/::/:/g' > "$dir/.path" + + cat > "$units/horizon-ci.slice" < "$units/horizon-runner.service" < "$units/horizon-runner-pause.service" </dev/null)" != yes ]; then + sudo -n loginctl enable-linger "$USER" || echo "Run once: sudo loginctl enable-linger $USER" + fi + systemctl --user daemon-reload + systemctl --user enable horizon-runner.service horizon-runner-pause.service > /dev/null + systemctl --user restart horizon-runner-pause.service + [ -e "$dir/paused" ] || systemctl --user start horizon-runner.service + status +} + +run() { + cd "$dir" + fix_shebangs + if command -v vscreen > /dev/null; then + eval "$(display_env)" + else + # Xvfb on the first free display from :90, for this service's lifetime. + local n=90 + while [ -e "/tmp/.X$n-lock" ]; do n=$((n + 1)); done + echo "$n" > "$dir/display" + Xvfb ":$n" -screen 0 1280x720x24 -nolisten tcp & + eval "$(display_env)" + fi + exec bash ./bin/runsvc.sh +} + +status() { + systemctl --user --no-pager list-units 'horizon-runner*' 'vscreen-ci-horizon*' + [ -e "$dir/paused" ] && echo "paused: $(cat "$dir/paused")" + tail -5 "$dir/pause.log" 2>/dev/null || true +} + +uninstall() { + systemctl --user disable --now horizon-runner-pause.service horizon-runner.service 2>/dev/null || true + gh api -X POST "repos/$repo/actions/runners/remove-token" --jq .token | + (cd "$dir" && bash ./config.sh remove --token "$(cat)") + rm -f "$units/horizon-runner.service" "$units/horizon-runner-pause.service" "$units/horizon-ci.slice" + systemctl --user daemon-reload +} + +case ${1:-} in + install|run|status|uninstall) "$1" ;; + *) sed -n '2,10p' "$0"; exit 2 ;; +esac diff --git a/tools/ci/runner-env.sh b/tools/ci/runner-env.sh index 49158ac91..39828b7d7 100755 --- a/tools/ci/runner-env.sh +++ b/tools/ci/runner-env.sh @@ -1,7 +1,8 @@ #!/usr/bin/env bash -# The tools the vanilla harnesses need, on the self-hosted Mac mini runner (label horizon-game): its service -# starts with a bare PATH. Python 3.13 and the JDK pinned by research-lock.json come from the machine, never -# from a download. Writes them to $GITHUB_PATH and $GITHUB_ENV for the next steps. +# The tools the vanilla harnesses need, on a self-hosted runner (label horizon-game): the Mac mini's, whose service +# starts with a bare PATH, and the Linux ones (tools/ci/linux-runner.sh, NixOS). Python 3.13 and the JDK pinned by +# research-lock.json come from the machine, never from a download. Writes them to $GITHUB_PATH and $GITHUB_ENV for +# the next steps. set -euo pipefail java_major=$(sed -n 's/.*"java_major": *\([0-9]*\).*/\1/p' research-lock.json | head -1) bin="$RUNNER_TEMP/horizon-bin" @@ -17,5 +18,30 @@ if [ "$(uname)" = Darwin ]; then if [ -x /opt/homebrew/bin/sccache ]; then { echo "RUSTC_WRAPPER=/opt/homebrew/bin/sccache"; echo "CARGO_INCREMENTAL=0"; } >> "$GITHUB_ENV" fi +else + python=$(command -v python3.13 || true) + [ -n "$python" ] || { echo "::error::no python3.13 on this runner"; exit 1; } + ln -sfn "$python" "$bin/python3" + ln -sfn "$python" "$bin/python" + # The JDK: the runner's JAVA_HOME (linux-runner.sh pins one), else the java on PATH, else nixpkgs' jdk. + major_of() { "$1/bin/java" -version 2>&1 | sed -n 's/.*version "\([0-9]*\).*/\1/p' | head -1; } + java_home=${JAVA_HOME:-} + if [ -z "$java_home" ] || [ "$(major_of "$java_home")" != "$java_major" ]; then + java_home="" + if command -v java > /dev/null; then + candidate=$(dirname "$(dirname "$(readlink -f "$(command -v java)")")") + [ "$(major_of "$candidate")" = "$java_major" ] && java_home=$candidate + fi + fi + if [ -z "$java_home" ] && command -v nix-build > /dev/null; then + out=$(nix-build '' --no-out-link -A "jdk${java_major}_headless" 2>/dev/null || true) + [ -n "$out" ] && java_home=$out/lib/openjdk + fi + [ -n "$java_home" ] || { echo "::error::no JDK $java_major on this runner"; exit 1; } + { echo "$bin"; echo "$java_home/bin"; } >> "$GITHUB_PATH" + echo "JAVA_HOME=$java_home" >> "$GITHUB_ENV" + if sccache=$(command -v sccache); then + { echo "RUSTC_WRAPPER=$sccache"; echo "CARGO_INCREMENTAL=0"; } >> "$GITHUB_ENV" + fi fi echo "HORIZON_NO_AUDIO=1" >> "$GITHUB_ENV" diff --git a/tools/ci/runner-pause.py b/tools/ci/runner-pause.py new file mode 100755 index 000000000..4a88c6ae4 --- /dev/null +++ b/tools/ci/runner-pause.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +"""Pauses the Horizon CI runner (tools/ci/linux-runner.sh) while the maintainer plays a game on this machine. + +A game is running when any of these holds: + - fleet-load says so (~/fleet/load: Steam, gamescope, Wine or Proton processes); + - a process of his matches GAME below (the same, plus Minecraft and a Horizon game he started himself); + - a process outside horizon-ci.slice keeps the GPU busy (nvidia-smi: SM use >= GPU_BUSY % twice in a row); + - ~/.actions-runners/horizon/pause exists (pause by hand: `touch` it; `rm` it to let the watcher decide). + +Paused, the runner takes no new job: while it is idle its service is stopped, so GitHub sends the jobs to the +Mac mini. A job already running is throttled, not stopped: horizon-ci.slice (the job, and the parity workers other machines +send here) drops to CPU weight 1, I/O weight 1 +and a quota of THROTTLE_CPUS threads, and the service stops once the job ends. Freezing it (SIGSTOP or the cgroup +freezer) would stop the runner's heartbeat, so GitHub would drop the job as lost, and the harnesses' wall-clock +timeouts would fail runs that were only waiting. The runner comes back RESUME_AFTER seconds after the last sign +of a game. Every change is written to ~/.actions-runners/horizon/pause.log. +""" +import json +import os +import re +import subprocess +import time +from pathlib import Path + +DIR = Path.home() / ".actions-runners" / "horizon" +SERVICE, SLICE = "horizon-runner.service", "horizon-ci.slice" +MANUAL, STATE, LOG = DIR / "pause", DIR / "paused", DIR / "pause.log" +GAME = re.compile(r"(SteamLaunch|gamescope|wine64-preloader|wineserver|wine-preloader|\.exe(\s|$)|proton\b" + r"|net\.minecraft\.client\.main\.Main|minecraft-launcher|prismlauncher)", re.I) +GPU_BUSY = int(os.environ.get("GPU_BUSY", 30)) +INTERVAL = int(os.environ.get("INTERVAL", 15)) +RESUME_AFTER = int(os.environ.get("RESUME_AFTER", 300)) +THROTTLE_CPUS = int(os.environ.get("THROTTLE_CPUS", 2)) + + +def log(text): + with LOG.open("a") as handle: + handle.write(f"{time.strftime('%Y-%m-%dT%H:%M:%S')} {text}\n") + + +def systemctl(*arguments): + return subprocess.run(["systemctl", "--user", *arguments], capture_output=True, text=True) + + +def ci_process(pid): + """A process of the runner's slice (its jobs, the parity workers other machines start here).""" + try: + return SLICE in Path(f"/proc/{pid}/cgroup").read_text() + except OSError: + return True # gone + + +def fleet_load_gaming(): + status = Path(os.environ.get("XDG_RUNTIME_DIR", f"/run/user/{os.getuid()}")) / "fleet-load" / "status.json" + try: + if time.time() - status.stat().st_mtime > 120: + return None + return "fleet-load: playing a game" if json.loads(status.read_text())["human"]["gaming"] else None + except (OSError, ValueError, KeyError, TypeError): + return None + + +def game_process(): + uid = os.getuid() + for entry in Path("/proc").iterdir(): + if not entry.name.isdigit(): + continue + try: + if entry.stat().st_uid != uid: + continue + command = (entry / "cmdline").read_bytes().replace(b"\0", b" ").decode(errors="replace") + except OSError: + continue + # A Horizon game counts when he started it (its parent is his launcher), not when a harness or agent did. + if GAME.search(command) or (" --offscreen" not in command and "/horizon-launcher/" in command + and "horizon-client" in command.split(" ")[0]): + if not ci_process(int(entry.name)): + return f"process {entry.name}: {command[:120]}" + return None + + +def gpu_users(): + """Processes outside the CI slice using the GPU's shader cores, as {pid: SM %}.""" + try: + output = subprocess.run(["nvidia-smi", "pmon", "-c", "1", "-s", "u"], capture_output=True, text=True, + timeout=20).stdout + except (OSError, subprocess.TimeoutExpired): + return {} + users = {} + for line in output.splitlines(): + fields = line.split() + if line.startswith("#") or len(fields) < 4 or not fields[1].isdigit() or not fields[3].isdigit(): + continue + if not ci_process(int(fields[1])): + users[int(fields[1])] = int(fields[3]) + return users + + +def runner_busy(): + """The runner is in a job: a Runner.Worker runs in its service.""" + group = systemctl("show", "-p", "ControlGroup", "--value", SERVICE).stdout.strip() + try: + pids = Path(f"/sys/fs/cgroup{group}/cgroup.procs").read_text().split() if group else [] + except OSError: + return False + for pid in pids: + try: + if Path(f"/proc/{pid}/comm").read_text().strip() == "Runner.Worker": + return True + except OSError: + pass + return False + + +def throttle(on): + if on: + systemctl("set-property", "--runtime", SLICE, "CPUWeight=1", "IOWeight=1", f"CPUQuota={THROTTLE_CPUS * 100}%") + else: + systemctl("set-property", "--runtime", SLICE, "CPUWeight=20", "IOWeight=20", "CPUQuota=") + + +def main(): + hot = {} # GPU users seen busy at the last sample + last_game = 0.0 + paused = STATE.exists() + while True: + reason = None + if MANUAL.exists(): + reason = "paused by hand (" + str(MANUAL) + ")" + reason = reason or fleet_load_gaming() or game_process() + if not reason: + busy = {pid for pid, sm in gpu_users().items() if sm >= GPU_BUSY} + twice = busy & set(hot) + hot = dict.fromkeys(busy) + if twice: + pid = min(twice) + try: + name = Path(f"/proc/{pid}/comm").read_text().strip() + except OSError: + name = "?" + reason = f"GPU busy: process {pid} ({name})" + now = time.monotonic() + if reason: + last_game = now + if reason and not paused: + paused = True + STATE.write_text(reason + "\n") + throttle(True) # also what other machines' parity workers run here meanwhile + if runner_busy(): + log(f"pause: {reason}; a job is running: throttled to {THROTTLE_CPUS} threads, stops after it") + else: + systemctl("stop", SERVICE) + log(f"pause: {reason}; runner stopped") + elif paused and reason is None and now - last_game >= RESUME_AFTER: + paused = False + STATE.unlink(missing_ok=True) + throttle(False) + systemctl("start", SERVICE) + log(f"resume: no game for {RESUME_AFTER} s; runner started") + elif paused and systemctl("is-active", "--quiet", SERVICE).returncode == 0 and not runner_busy(): + systemctl("stop", SERVICE) + log("the job ended: runner stopped until the game ends") + time.sleep(INTERVAL) + + +if __name__ == "__main__": + main() diff --git a/tools/ci/vanilla-data.sh b/tools/ci/vanilla-data.sh index 5276c54e9..a7bd4a407 100755 --- a/tools/ci/vanilla-data.sh +++ b/tools/ci/vanilla-data.sh @@ -1,8 +1,9 @@ #!/usr/bin/env bash # The vanilla data for this checkout on the self-hosted runner. Horizon never downloads the game: the runner # holds the maintainer's own copy, imported and decompiled once (HORIZON_SHARED_SOURCE, default -# ~/code/horizon-parity/shared-local). It is cloned once into the runner's work folder ($RUNNER_WORKSPACE/ -# shared-local; APFS clones, so no second copy of the bytes), and .local/ links to it as in a parity worktree. +# ~/code/horizon-parity/shared-local; tools/ci/linux-runner.sh sets it on Linux). It is cloned once into the runner's +# work folder ($RUNNER_WORKSPACE/shared-local; APFS clones, or reflinks where the file system has them, so no second +# copy of the bytes), and .local/ links to it as in a parity worktree. # .local/horizon-client and the exporter outputs beside it, what horizon-prepare makes, are prepared by this # checkout's own preparer once per preparer version (its sources, the exporters and research-lock.json), so a # change to the preparer is checked with what it prepares; the four newest preparations are kept. @@ -31,7 +32,8 @@ for entry in "$shared"/*; do done # HORIZON_PREPARE_KEY: the same key, computed where the Git history is (tools/pr_ready.py on another machine). -key=${HORIZON_PREPARE_KEY:-$(git ls-files -s crates/horizon-prepare mods/horizon/src/test/java research-lock.json | shasum -a 256 | cut -c1-16)} +sha256() { if command -v shasum > /dev/null; then shasum -a 256; else sha256sum; fi; } +key=${HORIZON_PREPARE_KEY:-$(git ls-files -s crates/horizon-prepare mods/horizon/src/test/java research-lock.json | sha256 | cut -c1-16)} prepared="$shared/prepared/$key" # Two runners share the vanilla data (~/.actions-runners/horizon-2/_work/horizon/shared-local links to the first # one's): one preparation at a time. A lock older than 30 minutes belongs to a job that died. From d81aa97e842a753e231ae34b181bc1bd2654c589 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 23:12:08 +0100 Subject: [PATCH 190/238] CI: the desktop runner works only while nobody is at the screen; desktop as a parity host runner_pause.py pauses on any seat0 session (locked too), games, GPU use or a pause file, and is the only thing that starts the runner; a job under way is throttled, then the runner stops. The desktop host in parity_hosts.json refuses work while paused and its workers stop on a pause. The nightly audit stays on the Mac mini. Docs and a unit test of the watcher. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/nightly.yml | 3 +- docs/parity-workflow.md | 2 +- docs/release-flow.md | 28 +++- tests/test_runner_pause.py | 82 ++++++++++++ tools/ci/linux-runner.sh | 52 +++++--- tools/ci/{runner-pause.py => runner_pause.py} | 125 +++++++++++------- tools/parity_hosts.json | 9 ++ 7 files changed, 230 insertions(+), 71 deletions(-) create mode 100644 tests/test_runner_pause.py rename tools/ci/{runner-pause.py => runner_pause.py} (51%) diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 51f9e89cd..60bbbf057 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -29,7 +29,8 @@ env: jobs: audit: - runs-on: [self-hosted, horizon-game] + # The Mac mini's runners only: the audit compares with the previous one, so it stays on one platform. + runs-on: [self-hosted, horizon-game, macOS] timeout-minutes: 150 steps: - name: 03:00 in Paris diff --git a/docs/parity-workflow.md b/docs/parity-workflow.md index f9f222071..80426fa59 100644 --- a/docs/parity-workflow.md +++ b/docs/parity-workflow.md @@ -38,7 +38,7 @@ The game logs into `HORIZON_HOME` and falls back to the player's own game folder ### Other machines -Harnesses can also run on the Mac mini and the ThinkPad (`tools/parity_hosts.json` describes them; its `default` list is empty, so pass `--hosts macmini,thinkpad` or set `PARITY_HOSTS`, after copying `shared-local/` to `~/code/horizon-parity/shared-local` on each host). The runner mirrors the worktree there with rsync (not `.git`, `.local`, `target` or ignored files) into `~/code/horizon-parity/remote/`, whose `.local` links to the vanilla data in `~/code/horizon-parity/shared-local`, and starts `tools/parity.py --worker` over ssh. The worker builds the probes (about 80 s the first time) and runs what it is sent under the same tracer, admitting harnesses against its own free memory. One queue, longest first, feeds every machine, remote ones first: the Mac takes up to 14 harnesses, the desktop 3 and only those under 800 MB. Each pass comes back with the content hash of every file it depended on, so it is keyed on exactly what ran, and logs come back to `.local/parity-runner/logs/`; state, sharing and `gate.py` work as before. If a host can't be reached, or drops out, its harnesses run here. +Harnesses can also run on the Mac mini, the ThinkPad and the desktop (`tools/parity_hosts.json` describes them; `desktop` uses its CI runner's environment and vanilla data, and refuses work, or stops it, while someone is at it: docs/release-flow.md, "The desktop's runner"; its `default` list is empty, so pass `--hosts macmini,thinkpad` or set `PARITY_HOSTS`, after copying `shared-local/` to `~/code/horizon-parity/shared-local` on each host). The runner mirrors the worktree there with rsync (not `.git`, `.local`, `target` or ignored files) into `~/code/horizon-parity/remote/`, whose `.local` links to the vanilla data in `~/code/horizon-parity/shared-local`, and starts `tools/parity.py --worker` over ssh. The worker builds the probes (about 80 s the first time) and runs what it is sent under the same tracer, admitting harnesses against its own free memory. One queue, longest first, feeds every machine, remote ones first: the Mac takes up to 14 harnesses, the desktop 3 and only those under 800 MB. Each pass comes back with the content hash of every file it depended on, so it is keyed on exactly what ran, and logs come back to `.local/parity-runner/logs/`; state, sharing and `gate.py` work as before. If a host can't be reached, or drops out, its harnesses run here. Harnesses a host can't run are listed in the file: `local_only` (`save_roundtrip` runs the game on a virtual screen) and each host's `exclude`. The Mac excludes nothing: vanilla's `Math.sin`, `pow`, `log` and the like don't round the same on an Apple silicon Mac as on Linux (HotSpot uses fdlibm there, Intel's stubs on x86-64), and Rust's float functions round like neither (they call the platform libm), so code that mirrors a vanilla `Math` call uses `horizon_core::jmath`, which computes what HotSpot computes on the host architecture. The ThinkPad helps too, gently: Linux, 2 harnesses at `nice 19`, since it hosts the Claude account pool; it finds ALSA through `~/code/horizon-parity/alsa-dev` like the desktop. diff --git a/docs/release-flow.md b/docs/release-flow.md index 4fb0209d9..e72bebc73 100644 --- a/docs/release-flow.md +++ b/docs/release-flow.md @@ -13,7 +13,7 @@ reopened; labels never start it): | Job | Runs on | What | Time | | --- | --- | --- | --- | | `verify` | GitHub `ubuntu-latest` | Public-tree check, Python tests, rustfmt, clippy with warnings denied, Rust tests. Dependencies and the workspace build come from a cache saved by main's runs | ~4 min cold, less warm | -| `parity / parity` | The Mac mini's runner (`ludovics-mac-mini`, label `horizon-game`) | The vanilla harnesses the change can affect (`tools/parity.py --changed `) | seconds when nothing is affected; minutes per affected harness | +| `parity / parity` | A runner with the label `horizon-game`: the Mac mini's two, and the desktop's when no game runs there (below) | The vanilla harnesses the change can affect (`tools/parity.py --changed `) | seconds when nothing is affected; minutes per affected harness | Pushes to main run `verify` only; the nightly audit covers main's harnesses. @@ -28,10 +28,13 @@ The job is `.github/workflows/parity.yml`, a reusable workflow: `checks` calls i repository, `run-parity.yml` for a fork's pull request a maintainer labels `run-parity`. Horizon never downloads the game, so harnesses that compare with vanilla run only where the maintainer's own -copy is: the Mac mini. Its runner runs as the maintainer's account and keeps its checkout between runs -(`clean: false`), so the probes build incrementally into `.local/parity-target` and earlier passes stand. +copy is: the Mac mini and his Linux desktop. Their runners run as the maintainer's account and keep their checkout +between runs (`clean: false`), so the probes build incrementally into `.local/parity-target` and earlier passes +stand. -1. `tools/ci/runner-env.sh` puts the machine's Python 3.13 and the JDK `research-lock.json` pins on the PATH. +1. `tools/ci/runner-env.sh` puts the machine's Python 3.13 and the JDK `research-lock.json` pins on the PATH + (Homebrew and `java_home` on the Mac; on NixOS what `linux-runner.sh` pinned, else nixpkgs), and sccache when + the machine has it. 2. `tools/ci/vanilla-data.sh` links `.local/` to the runner's copy of the vanilla data, cloned once from `~/code/horizon-parity/shared-local` into `~/.actions-runners/horizon/_work/horizon/shared-local`, and prepares `.local/horizon-client` with the pull request's own `horizon-prepare` once per preparer version @@ -64,6 +67,23 @@ release's macOS build back. Each keeps its own checkout, `target/` and Rust inst `_work/horizon/shared-local` links to the first's, so they share the vanilla data, the preparations (one at a time, under a lock) and the parity passes and slots. +**The desktop's runner.** `ludodesktop-horizon` (labels `self-hosted`, `Linux`, `X64`, `ludodesktop`, and +`horizon-game` once a full trial has passed there) runs on the maintainer's Linux desktop, only while nobody is +at it. `tools/ci/linux-runner.sh install`, run there as him, registers it and installs two systemd user services: +`horizon-runner` (the runner, in `~/.actions-runners/horizon`, nice 19, in `horizon-ci.slice` with CPU and I/O +weight 20 and memory capped at two thirds of the RAM; `CARGO_BUILD_JOBS` and the harness slots at 8, after the +machine froze under ~7 cold cargo builds on 2026-10-14; its own Rust install; the JDK, sccache and the build +libraries from nixpkgs; its own `vscreen` so no game it starts reaches his screen) and `horizon-runner-pause` +(`tools/ci/runner_pause.py`), the only thing that starts the runner. The watcher stops it while a graphical +session is open on the machine, locked or not (`~/fleet/power status` says "IN USE"), while a game runs (Steam, +gamescope, Wine, Proton or Minecraft processes, fleet-load's own detection, or a process outside the CI slice +using the GPU at 50 % or more), or while `~/.actions-runners/horizon/pause` exists, and starts it again 5 minutes +after the last such sign. A job already under way is throttled to 2 threads at the lowest weight and the runner +stops after it: freezing the job would make GitHub drop it as lost and the harnesses time out. Old pull requests' +`runner-env.sh` know only the Mac: the runner's `.env` and `.path` give their jobs Python, the JDK and the +vanilla data (`HORIZON_SHARED_SOURCE`) all the same. The nightly audit stays on the Mac mini's runners, so its +comparison with the previous night is always made on one platform. + ## Merging Pull requests merge with a merge commit. The orchestrator sessions review a pull request, then turn on diff --git a/tests/test_runner_pause.py b/tests/test_runner_pause.py new file mode 100644 index 000000000..c6df08b82 --- /dev/null +++ b/tests/test_runner_pause.py @@ -0,0 +1,82 @@ +"""tools/ci/runner_pause.py: the Linux CI runner works only while nobody is at the screen or playing.""" +import tempfile +import unittest +from pathlib import Path +from unittest import mock + +from tools.ci import runner_pause + + +class Machine: + """A fake desktop: a clock, who is there, and the runner service. The watcher samples every 15 s.""" + + def __init__(self, signs): + self.signs = signs # (from, until, reason): a sign of someone at the screen or playing + self.clock = 0.0 + self.active = False + self.job_until = None # the runner is in a job until then + self.throttles = [] # (clock, on) + self.calls = [] + + def reason(self, hot): + return next((reason for start, end, reason in self.signs if start <= self.clock < end), None) + + def systemctl(self, *arguments): + self.calls.append((self.clock, arguments)) + if arguments[1:] == (runner_pause.SERVICE,): + self.active = {"start": True, "stop": False}.get(arguments[0], self.active) + return mock.Mock(returncode=0 if arguments[:2] != ("is-active", "--quiet") or self.active else 3) + + def busy(self): + return self.job_until is not None and self.clock < self.job_until + + def sleep(self, seconds): + self.clock += seconds + if self.clock > 2000: + raise StopIteration + + def run(self): + with tempfile.TemporaryDirectory() as directory, \ + mock.patch.multiple(runner_pause, MANUAL=Path(directory, "pause"), STATE=Path(directory, "paused"), + LOG=Path(directory, "pause.log"), INTERVAL=15, RESUME_AFTER=300, + pause_reason=self.reason, systemctl=self.systemctl, runner_busy=self.busy, + throttle=lambda on: self.throttles.append((self.clock, on))), \ + mock.patch.object(runner_pause.time, "monotonic", lambda: self.clock), \ + mock.patch.object(runner_pause.time, "sleep", self.sleep): + try: + runner_pause.main() + except StopIteration: # the clock ran out + pass + + def first(self, verb, after=0): + return next(clock for clock, arguments in self.calls + if arguments == (verb, runner_pause.SERVICE) and clock >= after) + + +class RunnerPauseTests(unittest.TestCase): + def test_starts_only_after_nobody_was_there_for_resume_after(self): + machine = Machine([(0, 600, "session 3 of ludovic at the screen (seat0)")]) + machine.run() + self.assertEqual(machine.first("start"), 585 + 300) # the last sample that saw him + + def test_a_game_stops_an_idle_runner_at_once(self): + machine = Machine([(1000, 1300, "process 42: wine64-preloader game.exe")]) + machine.run() + self.assertEqual(machine.first("start"), 300) + self.assertEqual(machine.first("stop", after=300), 1005) + self.assertEqual(machine.first("start", after=1005), 1290 + 300) + + def test_a_job_under_way_is_throttled_then_stopped_when_it_ends(self): + machine = Machine([(1000, 1300, "GPU busy: process 7 (game)")]) + machine.job_until = 1200 + machine.run() + self.assertIn((1005, True), machine.throttles) # the game started: the job slows down at once, + self.assertIn((1005, ("stop", "horizon-parity-worker-*.scope")), machine.calls) # other machines' work goes + stop = machine.first("stop", after=300) + self.assertGreaterEqual(stop, 1200) # and the runner stops once it ends, not before + self.assertLess(stop, 1215) + self.assertEqual(machine.throttles[-1], (1290 + 300, False)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/ci/linux-runner.sh b/tools/ci/linux-runner.sh index 0cb677c87..adc2b0130 100755 --- a/tools/ci/linux-runner.sh +++ b/tools/ci/linux-runner.sh @@ -8,12 +8,14 @@ # bash tools/ci/linux-runner.sh uninstall # stop and unregister # # Everything lives in ~/.actions-runners/horizon (never in ~/code): the runner, its own rustup and cargo homes, -# the work folder, and copies of this script and runner-pause.py taken at install time (a pull request's checkout -# never runs as the service). The service runs nice, in horizon-ci.slice (low CPU and I/O weight, memory capped), +# the work folder, and copies of this script and runner_pause.py taken at install time (a pull request's checkout +# never runs as the service). horizon-runner-pause.service (runner_pause.py) starts the runner only while nobody is +# at the screen or playing, and stops it otherwise. The runner runs nice, in horizon-ci.slice (low CPU and I/O weight, memory capped), # on its own virtual screen: vscreen (a headless sway) where it is installed, else Xvfb from nixpkgs with Mesa's -# Vulkan drivers. runner-pause.py pauses it while a game runs (horizon-runner-pause.service). +# Vulkan drivers. # HORIZON_SHARED_SOURCE: the imported vanilla data (default ~/horizon-local/shared-local, else -# ~/code/horizon/.local); RUNNER_JOBS: cargo jobs and harnesses at once (default 2/3 of the threads). +# ~/code/horizon/.local); RUNNER_JOBS: cargo jobs and harnesses at once (default a third of the threads, at most 8: +# ludodesktop froze under ~7 cold cargo builds on 2026-10-14, and it is the maintainer's own machine). set -euo pipefail repo=ludovic111/horizon dir=$HOME/.actions-runners/horizon @@ -30,6 +32,18 @@ export LD_LIBRARY_PATH=/run/current-system/sw/share/nix-ld/lib${LD_LIBRARY_PATH: # NixOS has no /bin/bash: the runner's scripts (again after each of its self-updates) get a portable one. fix_shebangs() { sed -i '1s|^#!/bin/bash|#!/usr/bin/env bash|' "$dir"/*.sh "$dir"/bin/*.sh; } +# Other machines' parity runs can send harnesses here (tools/parity_hosts.json "desktop": parity.py --worker over +# ssh, in horizon-ci.slice). Their vanilla data is the runner checkout's: parity-host/shared-local links to its +# .local links, so it follows the preparation of the runner's last job. +parity_host_links() { + local checkout=$dir/_work/horizon/horizon/.local shared=$dir/parity-host/shared-local entry + [ -d "$checkout" ] || return 0 + mkdir -p "$shared" + for entry in "$checkout"/*; do + [ -L "$entry" ] && ln -sfn "$entry" "$shared/$(basename "$entry")" + done +} + display_env() { # The jobs' screen. Never the maintainer's: the user manager hands every service his DISPLAY and WAYLAND_DISPLAY. if command -v vscreen > /dev/null; then @@ -40,7 +54,9 @@ display_env() { } install() { - local name=${RUNNER_NAME:-$(hostname)-horizon} jobs=${RUNNER_JOBS:-$(( $(nproc) * 2 / 3 ))} + local name=${RUNNER_NAME:-$(hostname)-horizon} threads jobs + threads=$(nproc) + jobs=${RUNNER_JOBS:-$(( threads / 3 > 8 ? 8 : threads / 3 > 1 ? threads / 3 : 1 ))} local source=${HORIZON_SHARED_SOURCE:-} if [ -z "$source" ]; then for candidate in "$HOME/horizon-local/shared-local" "$HOME/code/horizon/.local"; do @@ -49,7 +65,7 @@ install() { fi [ -n "$source" ] && [ -d "$source/minecraft" ] || { echo "no imported Minecraft (HORIZON_SHARED_SOURCE)"; exit 1; } mkdir -p "$dir/nix" "$units" - cp "$here/linux-runner.sh" "$here/runner-pause.py" "$dir/" + cp "$here/linux-runner.sh" "$here/runner_pause.py" "$dir/" if [ ! -f "$dir/.runner" ]; then local cache=$HOME/.cache/gha-runner/actions-runner-linux-x64-$version.tar.gz @@ -66,11 +82,11 @@ install() { fi # Libraries for building (pkg-config) and running (dlopen) the game, the JDK pinned by research-lock.json, sccache. - local alsa udev java sccache + local alsa udev java alsa=$(nix_out alsa-lib.dev alsa-dev) udev=$(nix_out systemd.dev systemd-dev) java=$(nix_out "jdk$(sed -n 's/.*"java_major": *\([0-9]*\).*/\1/p' "$here/../../research-lock.json" | head -1)_headless" jdk)/lib/openjdk - sccache=$(nix_out sccache sccache) + nix_out sccache sccache > /dev/null # on the PATH below: runner-env.sh hands it to cargo local libs=/run/opengl-driver/lib:/run/current-system/sw/share/nix-ld/lib if ! command -v vscreen > /dev/null; then local mesa x @@ -91,6 +107,7 @@ RUSTUP_HOME=$dir/rustup CARGO_HOME=$dir/cargo CARGO_BUILD_JOBS=$jobs PARITY_MACHINE_JOBS=$jobs +PARITY_LOCAL_JOBS=$jobs JAVA_HOME=$java HORIZON_SHARED_SOURCE=$source HORIZON_NO_AUDIO=1 @@ -118,27 +135,25 @@ MemoryMax=$(( $(awk '/MemTotal/ {print int($2 / 1048576)}' /proc/meminfo) * 2 / EOF cat > "$units/horizon-runner.service" < "$units/horizon-runner-pause.service" < /dev/null + # Only the watcher starts the runner (never the boot itself): it decides whether anyone is at the screen. + systemctl --user enable horizon-runner-pause.service > /dev/null + # The runner reads .env when it starts: stop it, unless it is in a job (that would cancel the job); the watcher + # starts it again when it may run. + if pgrep -f "$dir/bin/Runner.Worker" > /dev/null; then echo "In a job: stop horizon-runner.service after it" + else systemctl --user stop horizon-runner.service; rm -f "$dir/paused"; fi systemctl --user restart horizon-runner-pause.service - [ -e "$dir/paused" ] || systemctl --user start horizon-runner.service status } run() { cd "$dir" fix_shebangs + parity_host_links if command -v vscreen > /dev/null; then eval "$(display_env)" else diff --git a/tools/ci/runner-pause.py b/tools/ci/runner_pause.py similarity index 51% rename from tools/ci/runner-pause.py rename to tools/ci/runner_pause.py index 4a88c6ae4..57f304f1f 100755 --- a/tools/ci/runner-pause.py +++ b/tools/ci/runner_pause.py @@ -1,19 +1,22 @@ #!/usr/bin/env python3 -"""Pauses the Horizon CI runner (tools/ci/linux-runner.sh) while the maintainer plays a game on this machine. +"""Pauses the Horizon CI runner (tools/ci/linux-runner.sh) while the maintainer is at this machine or plays on it. -A game is running when any of these holds: - - fleet-load says so (~/fleet/load: Steam, gamescope, Wine or Proton processes); +The runner works only while nobody is at the screen (rule of 2026-10-25, after ludodesktop was restarted under a +CI load while he used it). It pauses when any of these holds: + - a graphical session is open on seat0, locked or not: what `~/fleet/power status` calls "IN USE"; + - fleet-load says a game runs (~/fleet/load: Steam, gamescope, Wine or Proton processes); - a process of his matches GAME below (the same, plus Minecraft and a Horizon game he started himself); - - a process outside horizon-ci.slice keeps the GPU busy (nvidia-smi: SM use >= GPU_BUSY % twice in a row); + - a process outside horizon-ci.slice keeps the GPU busy (nvidia-smi: SM use >= GPU_BUSY %, default 50, in two + samples in a row; the browser, Discord and the compositor stay under 20 %); - ~/.actions-runners/horizon/pause exists (pause by hand: `touch` it; `rm` it to let the watcher decide). Paused, the runner takes no new job: while it is idle its service is stopped, so GitHub sends the jobs to the -Mac mini. A job already running is throttled, not stopped: horizon-ci.slice (the job, and the parity workers other machines -send here) drops to CPU weight 1, I/O weight 1 -and a quota of THROTTLE_CPUS threads, and the service stops once the job ends. Freezing it (SIGSTOP or the cgroup -freezer) would stop the runner's heartbeat, so GitHub would drop the job as lost, and the harnesses' wall-clock -timeouts would fail runs that were only waiting. The runner comes back RESUME_AFTER seconds after the last sign -of a game. Every change is written to ~/.actions-runners/horizon/pause.log. +Mac mini, and the parity workers other machines started here stop (their harnesses go back to those machines). A +job already running is throttled, not stopped: horizon-ci.slice drops to CPU weight 1, I/O weight 1 and a quota of +THROTTLE_CPUS threads, and the service stops once the job ends. Freezing it (SIGSTOP or the cgroup freezer) +would stop the runner's heartbeat, so GitHub would drop the job as lost, and the harnesses' wall-clock timeouts +would fail runs that were only waiting. The runner comes back RESUME_AFTER seconds after the last of these signs. +Every change is written to ~/.actions-runners/horizon/pause.log. """ import json import os @@ -27,7 +30,7 @@ SERVICE, SLICE = "horizon-runner.service", "horizon-ci.slice" MANUAL, STATE, LOG = DIR / "pause", DIR / "paused", DIR / "pause.log" GAME = re.compile(r"(SteamLaunch|gamescope|wine64-preloader|wineserver|wine-preloader|\.exe(\s|$)|proton\b" r"|net\.minecraft\.client\.main\.Main|minecraft-launcher|prismlauncher)", re.I) -GPU_BUSY = int(os.environ.get("GPU_BUSY", 30)) +GPU_BUSY = int(os.environ.get("GPU_BUSY", 50)) INTERVAL = int(os.environ.get("INTERVAL", 15)) RESUME_AFTER = int(os.environ.get("RESUME_AFTER", 300)) THROTTLE_CPUS = int(os.environ.get("THROTTLE_CPUS", 2)) @@ -50,6 +53,16 @@ def ci_process(pid): return True # gone +def at_the_screen(): + """A graphical login on seat0, as ~/fleet/power status counts it (a locked screen counts too).""" + sessions = subprocess.run(["loginctl", "list-sessions", "--no-legend"], capture_output=True, text=True).stdout + for line in sessions.splitlines(): + fields = line.split() + if "seat0" in fields: + return f"session {fields[0]} of {fields[2] if len(fields) > 2 else '?'} at the screen (seat0)" + return None + + def fleet_load_gaming(): status = Path(os.environ.get("XDG_RUNTIME_DIR", f"/run/user/{os.getuid()}")) / "fleet-load" / "status.json" try: @@ -82,7 +95,8 @@ def game_process(): def gpu_users(): """Processes outside the CI slice using the GPU's shader cores, as {pid: SM %}.""" try: - output = subprocess.run(["nvidia-smi", "pmon", "-c", "1", "-s", "u"], capture_output=True, text=True, + # Two samples: the first one nvidia-smi prints has no utilisation yet. + output = subprocess.run(["nvidia-smi", "pmon", "-c", "2", "-s", "u"], capture_output=True, text=True, timeout=20).stdout except (OSError, subprocess.TimeoutExpired): return {} @@ -92,7 +106,7 @@ def gpu_users(): if line.startswith("#") or len(fields) < 4 or not fields[1].isdigit() or not fields[3].isdigit(): continue if not ci_process(int(fields[1])): - users[int(fields[1])] = int(fields[3]) + users[int(fields[1])] = max(users.get(int(fields[1]), 0), int(fields[3])) return users @@ -119,47 +133,60 @@ def throttle(on): systemctl("set-property", "--runtime", SLICE, "CPUWeight=20", "IOWeight=20", "CPUQuota=") +def pause_reason(hot): + """Why the runner may not work now, or None. hot: the GPU users seen busy at the last sample (updated).""" + if MANUAL.exists(): + return f"paused by hand ({MANUAL})" + reason = at_the_screen() or fleet_load_gaming() or game_process() + if reason: + return reason + busy = {pid for pid, sm in gpu_users().items() if sm >= GPU_BUSY} + twice = sorted(busy & set(hot)) + hot.clear() + hot.update(dict.fromkeys(busy)) + if twice: + try: + name = Path(f"/proc/{twice[0]}/comm").read_text().strip() + except OSError: + name = "?" + return f"GPU busy: process {twice[0]} ({name})" + return None + + def main(): - hot = {} # GPU users seen busy at the last sample - last_game = 0.0 - paused = STATE.exists() + hot = {} + # A boot (or a restart of this watcher) counts as a sign: the runner starts RESUME_AFTER seconds later at the + # earliest, which leaves time to log in after switching the machine on. + last_sign = time.monotonic() + paused = None while True: - reason = None - if MANUAL.exists(): - reason = "paused by hand (" + str(MANUAL) + ")" - reason = reason or fleet_load_gaming() or game_process() - if not reason: - busy = {pid for pid, sm in gpu_users().items() if sm >= GPU_BUSY} - twice = busy & set(hot) - hot = dict.fromkeys(busy) - if twice: - pid = min(twice) - try: - name = Path(f"/proc/{pid}/comm").read_text().strip() - except OSError: - name = "?" - reason = f"GPU busy: process {pid} ({name})" + reason = pause_reason(hot) now = time.monotonic() if reason: - last_game = now - if reason and not paused: - paused = True - STATE.write_text(reason + "\n") - throttle(True) # also what other machines' parity workers run here meanwhile - if runner_busy(): - log(f"pause: {reason}; a job is running: throttled to {THROTTLE_CPUS} threads, stops after it") - else: + last_sign = now + active = systemctl("is-active", "--quiet", SERVICE).returncode == 0 + if reason or now - last_sign < RESUME_AFTER: + if paused is not True: + paused = True + STATE.write_text((reason or "starting") + "\n") + throttle(True) + # Parity workers other machines started here (tools/parity_hosts.json): they stop, and those + # machines run the harnesses themselves. + systemctl("stop", "horizon-parity-worker-*.scope") + log(f"pause: {reason or 'waiting ' + str(RESUME_AFTER) + ' s after starting'}" + + (f"; a job is running: throttled to {THROTTLE_CPUS} threads, stops after it" if active else "")) + if active and not runner_busy(): systemctl("stop", SERVICE) - log(f"pause: {reason}; runner stopped") - elif paused and reason is None and now - last_game >= RESUME_AFTER: - paused = False - STATE.unlink(missing_ok=True) - throttle(False) - systemctl("start", SERVICE) - log(f"resume: no game for {RESUME_AFTER} s; runner started") - elif paused and systemctl("is-active", "--quiet", SERVICE).returncode == 0 and not runner_busy(): - systemctl("stop", SERVICE) - log("the job ended: runner stopped until the game ends") + log("runner stopped") + else: + if paused is not False: + paused = False + STATE.unlink(missing_ok=True) + throttle(False) + log(f"resume: nobody at the screen and no game for {RESUME_AFTER} s") + if not active: + systemctl("start", SERVICE) + log("runner started") time.sleep(INTERVAL) diff --git a/tools/parity_hosts.json b/tools/parity_hosts.json index 0aa013a5b..5d561175d 100644 --- a/tools/parity_hosts.json +++ b/tools/parity_hosts.json @@ -11,6 +11,15 @@ "jobs": 14, "exclude": {} }, + "desktop": { + "ssh": "desktop", + "root": ".actions-runners/horizon/parity-host", + "shell": "systemd-run --user --scope --quiet --slice=horizon-ci.slice --unit=horizon-parity-worker-$$ bash -c", + "setup": "test ! -e ~/.actions-runners/horizon/paused && set -a && . ~/.actions-runners/horizon/.env && set +a && export PATH=$(cat ~/.actions-runners/horizon/.path) && eval \"$(vscreen env)\"", + "jobs": 8, + "nice": 19, + "note": "ludodesktop, the maintainer's own machine: only while nobody is at the screen (tools/ci/runner_pause.py: a paused desktop refuses workers and stops the running ones, whose harnesses then run here); 8 jobs at most, in the CI runner's environment and slice" + }, "thinkpad": { "ssh": "thinkpad", "root": "code/horizon-parity", From 28206976d8f301e834f47d85071820214a732bb7 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 25 Oct 2026 22:04:23 +0000 Subject: [PATCH 191/238] Move parity fixtures onto server execution Co-Authored-By: OpenAI Codex (GPT-6.1) --- crates/horizon-client/src/game.rs | 22 ++- .../src/game/command_harness.rs | 141 ++++++++++++++---- crates/horizon-client/src/game/commands.rs | 3 +- .../horizon-client/src/game/commands/extra.rs | 28 +++- .../src/game/dispense_fixture.rs | 4 + crates/horizon-client/src/game/integrated.rs | 57 ++++++- crates/horizon-client/src/game/levels.rs | 18 ++- crates/horizon-client/src/game/remote.rs | 17 +++ .../horizon-client/src/game/sculk_fixture.rs | 4 + .../horizon-client/src/game/server_travel.rs | 2 +- docs/parity-workflow.md | 28 ++++ tools/command_exec_corpus.py | 23 ++- tools/lan_e2e.py | 39 +++++ tools/test_command_exec_parity.py | 4 +- tools/test_dispenser_parity.py | 2 +- tools/test_sculk_parity.py | 2 +- 16 files changed, 338 insertions(+), 56 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index f5803ad08..30c1cb5b1 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -348,10 +348,7 @@ impl Options { dedicated: None, benchmark: None, dev_mods: Vec::new(), - // 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(), + server_thread: std::env::var("HORIZON_SERVER_THREAD").map_or(true, |v| v != "0"), execution_role: execution_role::ExecutionRole::Client, }; let mut session_file = std::env::var("HORIZON_SESSION_FILE").ok(); @@ -12935,11 +12932,11 @@ fn start_game<'a>( ); } if let Ok(spec) = std::env::var("HORIZON_DISPENSE_FIXTURE") { - game.dispense_fixture(&spec)?; + game.run_component_harness(integrated::ComponentHarness::Dispenser(spec))?; return Ok(None); } if let Ok(spec) = std::env::var("HORIZON_SCULK_FIXTURE") { - game.sculk_fixture(&spec)?; + game.run_component_harness(integrated::ComponentHarness::Sculk(spec))?; return Ok(None); } if game.options.execution_role == execution_role::ExecutionRole::DedicatedServer { @@ -12961,8 +12958,8 @@ fn start_game<'a>( Some(folder) => { let (seed, creative) = (game.options.seed, game.options.creative); if std::env::var_os("HORIZON_COMMAND_SCRIPT").is_some() { - // The command harness runs before entering the window loop. - game.open_world(&folder, None, Some(seed), creative, None)?; + // Headless, but with the same server and memory connection as play. + game.open_integrated(&folder, None, Some(seed), creative, None)?; } else { // Start after the window is handed over, so --world uses the // same responsive initialization path as the title screen. @@ -12989,11 +12986,10 @@ fn start_game<'a>( game.title = Some(title); } } - if let (Some(script), Some(output)) = ( - std::env::var_os("HORIZON_COMMAND_SCRIPT"), - std::env::var_os("HORIZON_COMMAND_OUTPUT"), - ) { - game.run_command_harness(script.as_ref(), output.as_ref())?; + if std::env::var_os("HORIZON_COMMAND_SCRIPT").is_some() + && std::env::var_os("HORIZON_COMMAND_OUTPUT").is_some() + { + game.wait_command_harness()?; return Ok(None); } Ok(Some(game)) diff --git a/crates/horizon-client/src/game/command_harness.rs b/crates/horizon-client/src/game/command_harness.rs index 096ba1c74..33b99cc14 100644 --- a/crates/horizon-client/src/game/command_harness.rs +++ b/crates/horizon-client/src/game/command_harness.rs @@ -1,6 +1,7 @@ //! Headless command runner for `tools/test_command_exec_parity.py`: with //! `HORIZON_COMMAND_SCRIPT` and `HORIZON_COMMAND_OUTPUT` set, the opened world -//! runs without a window. Each script line is a JSON operation: `cmd` runs a +//! runs on its server thread with a connected, headless client. Each script +//! line is a JSON operation: `cmd` runs a //! command as the player (Commands.performPrefixedCommand) and records every //! system message it produced, as styled spans, with the results reported to //! the player's `CommandResultCallback`; `tick` advances the game; `settle` @@ -41,30 +42,100 @@ pub(super) fn spans(text: &Text) -> Value { } impl Game<'_> { + /// Pump the real client's connection, without predicting movement: the + /// oracle's player stays where commands put it. Errors cross the server + /// boundary through Integrated::stop, including script and save failures. + pub(super) fn wait_command_harness(&mut self) -> Result<()> { + let server = self.integrated.take().ok_or("command harness: no server")?; + while !server.stopped() { + self.poll_stationary_client()?; + std::thread::sleep(Duration::from_millis(2)); + } + server.stop() + } + + /// The owner's authoritative body and level, using the same swaps as a + /// server command. Always put them back, also when a script operation fails. + fn harness_owner(&mut self, f: impl FnOnce(&mut Self) -> Result) -> Result { + let owner = self + .serving + .as_ref() + .ok_or("command harness: not a server")? + .owner; + let mut host = self.lan.take().ok_or("command harness: no hub")?; + let guest = host.hub.guests.iter().find(|g| g.uuid == owner && g.placed); + let Some(guest) = guest else { + self.lan = Some(host); + return Err("command harness: owner is not connected".into()); + }; + let (conn, dim, back) = (guest.conn, super::Dim::from_id(&guest.dimension), self.dim); + if let Err(error) = self.enter_level(Some(&mut host), dim) { + self.lan = Some(host); + return Err(error); + } + let swap = self + .enter_guest(&mut host, conn) + .expect("the owner is not already acting"); + self.lan = Some(host); + let result = f(self); + let mut host = self.lan.take().expect("the operation puts the hub back"); + self.leave_guest(&mut host, swap); + host.hub.sync_all_effects(); + self.run_level_teleports(&mut host); + let restored = self.enter_level(Some(&mut host), back); + self.lan = Some(host); + restored?; + result + } + /// The player's portal process finished in the nether portal it stands /// in (the oracle's player is not ticked, so neither is the wait). fn harness_portal(&mut self) -> Result<()> { - let Some(p) = &self.player else { - return Err("command harness: portal without a player".into()); - }; - let entry = horizon_core::position::BlockPos::containing(p.position); - let block = self.world.block(entry.x, entry.y, entry.z); - if &*self.res.facts().get(block).name != "minecraft:nether_portal" { - return Err("command harness: the player is not in a nether portal".into()); - } - self.player_through_nether_portal(entry) + let (conn, plan) = self.harness_owner(|g| { + let p = g + .player + .as_ref() + .ok_or("command harness: portal without a player")?; + let entry = horizon_core::position::BlockPos::containing(p.position); + let block = g.world.block(entry.x, entry.y, entry.z); + if &*g.res.facts().get(block).name != "minecraft:nether_portal" { + return Err("command harness: the player is not in a nether portal".into()); + } + let plan = g.portal_plan( + entry, + p.position, + p.dimensions.width, + p.dimensions.height, + true, + ); + Ok(( + g.lan_acting.ok_or("command harness: no acting owner")?, + plan, + )) + })?; + // Start travel after restoring the owner's body: change_level updates + // its hub record, which leave_guest would otherwise overwrite. + self.in_owner_level(|g| { + let mut host = g.lan.take().ok_or("command harness: no hub")?; + g.start_server_trip(&mut host, conn, plan.dim, Some(plan)); + g.lan = Some(host); + Ok(()) + }) } /// Load terrain until the player stands in lit chunks. fn harness_settle(&mut self, deadline: Instant) -> Result<()> { loop { self.update_world(); - self.spawn_player()?; if let Some(p) = &self.player { let (cx, cz) = ( (p.position.x.floor() as i32) >> 4, (p.position.z.floor() as i32) >> 4, ); - if (-1..=1).all(|dz| (-1..=1).all(|dx| self.streaming.is_lit(cx + dx, cz + dz))) { + // Finish requested terrain before inspecting it, so worker + // timing cannot change which command positions are loaded. + if !self.streaming.busy() + && (-1..=1).all(|dz| (-1..=1).all(|dx| self.streaming.is_lit(cx + dx, cz + dz))) + { // The oracle's settle also waits for the entities saved // in those chunks (areEntitiesActuallyLoadedAndTicking): // after a dimension change they come back from the save. @@ -95,7 +166,19 @@ impl Game<'_> { .and_then(|v| v.parse().ok()) .unwrap_or(600), ); - self.harness_settle(deadline)?; + // Join/configuration uses the normal hub, but never advances the world. + while !self + .lan + .as_ref() + .is_some_and(|h| h.hub.guests.iter().any(|g| g.placed && g.loaded)) + { + self.tick_server(true)?; + if Instant::now() > deadline { + return Err("command harness: owner did not connect".into()); + } + std::thread::sleep(Duration::from_millis(2)); + } + self.harness_owner(|g| g.harness_settle(deadline))?; let mut out = String::new(); for line in std::fs::read_to_string(script)?.lines() { if line.trim().is_empty() { @@ -107,9 +190,12 @@ impl Game<'_> { let command = op["cmd"].as_str().unwrap_or(""); self.chat_capture = Some(Vec::new()); self.cmds.results = Some(Vec::new()); - self.as_console(|g| { - g.run_command(command.strip_prefix('/').unwrap_or(command)) - }); + self.harness_owner(|g| { + g.as_console(|g| { + g.run_command(command.strip_prefix('/').unwrap_or(command)) + }); + Ok(()) + })?; let messages = self.chat_capture.take().unwrap_or_default(); let results = self.cmds.results.take().unwrap_or_default(); if op["record"].as_bool() != Some(false) { @@ -124,17 +210,8 @@ impl Game<'_> { } "tick" => { self.chat_capture = Some(Vec::new()); - // The oracle's player has no client moving it: hold the - // player where commands put it while the game ticks. - let held = self.player.as_ref().map(|p| p.position); for _ in 0..op["n"].as_u64().unwrap_or(1) { - self.update_world(); - self.tick()?; - if let (Some(p), Some(at)) = (self.player.as_mut(), held) { - p.position = at; - p.velocity = horizon_core::geometry::Vec3::new(0.0, 0.0, 0.0); - p.fall_distance = 0.0; - } + self.tick_server(false)?; } let messages = self.chat_capture.take().unwrap_or_default(); if op["record"].as_bool() == Some(true) { @@ -147,7 +224,17 @@ impl Game<'_> { out.push('\n'); } } - "settle" => self.harness_settle(deadline)?, + "settle" => { + // Finish portal arrivals using connection-only passes too. + while !self.server_trips.is_empty() { + self.tick_server(true)?; + if Instant::now() > deadline { + return Err("command harness: portal did not arrive".into()); + } + std::thread::sleep(Duration::from_millis(2)); + } + self.harness_owner(|g| g.harness_settle(deadline))?; + } "portal" => self.harness_portal()?, other => return Err(format!("command harness: unknown op {other}").into()), } diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index 69c06d97e..c456c9743 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -751,7 +751,8 @@ impl Game<'_> { && !id.ends_with("get") && let Ok(t) = self.entities(ctx, "target", false) { - self.cmds.waypoint_dirty.push(t[0]); + let target = self.waypoint_target(t[0]); + self.cmds.waypoint_dirty.push(target); } result } diff --git a/crates/horizon-client/src/game/commands/extra.rs b/crates/horizon-client/src/game/commands/extra.rs index ef69be6d9..7536c9503 100644 --- a/crates/horizon-client/src/game/commands/extra.rs +++ b/crates/horizon-client/src/game/commands/extra.rs @@ -212,8 +212,22 @@ impl Game<'_> { // ---- /waypoint ------------------------------------------------------ + /// A guest's body is only `Player` during its turn; retained command state + /// must use its connection so the next server tick still finds that body. + pub(super) fn waypoint_target(&self, t: Target) -> Target { + match t { + Target::Player => self.lan_acting.map_or(t, Target::Guest), + _ => t, + } + } + /// A living entity's `waypoint_transmit_range` (`isTransmittingWaypoint`). fn transmit_range(&mut self, t: Target) -> f64 { + if matches!(t, Target::Guest(_)) { + return self + .for_player(t, |g| g.transmit_range(Target::Player)) + .unwrap_or(0.0); + } if t == Target::Player { return self .player @@ -242,7 +256,7 @@ impl Game<'_> { fn waypoint_transmitters(&mut self) -> Vec { if self.cmds.waypoints.is_none() { let tracked = if self.transmit_range(Target::Player) > 0.0 { - vec![Target::Player] + vec![self.waypoint_target(Target::Player)] } else { Vec::new() }; @@ -255,6 +269,12 @@ impl Game<'_> { .collect(); self.cmds.waypoints = Some(alive.clone()); alive + .into_iter() + .map(|t| match t { + Target::Guest(conn) if self.lan_acting == Some(conn) => Target::Player, + _ => t, + }) + .collect() } /// The waypoint part of a living entity's tick: dirty transmit ranges /// track or untrack it. @@ -277,7 +297,7 @@ impl Game<'_> { fn waypoint_icon(&self, t: Target) -> WaypointIcon { self.cmds .waypoint_icons - .get(&t) + .get(&self.waypoint_target(t)) .cloned() .unwrap_or_default() } @@ -387,6 +407,7 @@ impl Game<'_> { &[Text::colored(name, rgb)], ) }; + let t = self.waypoint_target(t); self.cmds.waypoint_icons.insert(t, icon); // WaypointCommand.mutateIcon: untrack, change, track again. self.waypoint_transmitters(); @@ -606,6 +627,7 @@ impl Game<'_> { .hub .guests .iter() + .filter(|g| lan.hub.singleplayer_owner != Some(g.uuid)) .filter(|g| match &s.player_name { Some(name) => g.name.eq_ignore_ascii_case(name), None => s.kind == 'a' || s.kind == 'e', @@ -617,7 +639,7 @@ impl Game<'_> { if host.is_empty() && guests.is_empty() { return Err(err("argument.entity.notfound.player", &[])); } - if self.lan.is_none() { + if self.lan.is_none() || (self.serving() && self.published_port().is_none()) { return Err(err("commands.kick.singleplayer.failed", &[])); } let reason_raw = match ctx.get("reason") { diff --git a/crates/horizon-client/src/game/dispense_fixture.rs b/crates/horizon-client/src/game/dispense_fixture.rs index 752785084..267f323c3 100644 --- a/crates/horizon-client/src/game/dispense_fixture.rs +++ b/crates/horizon-client/src/game/dispense_fixture.rs @@ -90,6 +90,10 @@ enum Target { impl Game<'_> { /// Run every case of `spec` ("CASES,OUTPUT") and write the results. pub(super) fn dispense_fixture(&mut self, spec: &str) -> Result<()> { + assert!( + !self.options.execution_role.is_client(), + "fixture requires a server Game" + ); let (input, output) = spec .split_once(',') .ok_or("HORIZON_DISPENSE_FIXTURE=CASES,OUTPUT")?; diff --git a/crates/horizon-client/src/game/integrated.rs b/crates/horizon-client/src/game/integrated.rs index d4726f2f1..abe6df9b3 100644 --- a/crates/horizon-client/src/game/integrated.rs +++ b/crates/horizon-client/src/game/integrated.rs @@ -143,7 +143,7 @@ pub(super) struct Serving { flags: Arc, tasks: Receiver, /// The window's profile. - owner: u128, + pub(super) owner: u128, /// Open to LAN or closed from inside a player's command, while the hub /// is out of its place: applied after the tick. pending: Option, @@ -357,6 +357,16 @@ impl Game<'_> { }); let _ = ready.send(Ok((pipe, self.level.name.clone()))); self.settle_spawn()?; + if let (Some(script), Some(output)) = ( + std::env::var_os("HORIZON_COMMAND_SCRIPT"), + std::env::var_os("HORIZON_COMMAND_OUTPUT"), + ) { + // The script owns the clock: connection setup and terrain loading + // cannot add wall-clock-dependent world ticks to oracle output. + let result = self.run_command_harness(script.as_ref(), output.as_ref()); + let stopped = self.stop_integrated(); + return result.and(stopped); + } let mut next = Instant::now(); while !flags.stop.load(Ordering::Relaxed) { self.run_tasks(); @@ -692,6 +702,48 @@ fn serve( tasks: Receiver, ready: &Sender, ) -> Result<()> { + let mut server = server_game(res, pack, options, settings)?; + server.run_integrated(launch, flags, tasks, ready) +} + +/// Component oracles supply their own synthetic levels and entity phases. +/// They still execute on a server Game, never the client's world. +pub(super) enum ComponentHarness { + Dispenser(String), + Sculk(String), +} + +impl Game<'_> { + pub(super) fn run_component_harness(&self, fixture: ComponentHarness) -> Result<()> { + let (res, pack, settings) = (self.res.clone(), self.pack.clone(), self.settings.clone()); + let mut options = self.options.clone(); + options.execution_role = ExecutionRole::IntegratedServer; + options.dedicated = Some(Vec::new()); + options.server_thread = false; + let result = std::thread::Builder::new() + .name("Server thread".into()) + .spawn(move || { + let run = || -> Result<()> { + let mut server = server_game(&res, pack, options, settings)?; + match fixture { + ComponentHarness::Dispenser(spec) => server.dispense_fixture(&spec), + ComponentHarness::Sculk(spec) => server.sculk_fixture(&spec), + } + }; + run().map_err(|e| e.to_string()) + })? + .join() + .map_err(|_| "the component harness server panicked")?; + result.map_err(Into::into) + } +} + +fn server_game( + res: &Arc, + pack: PackResources, + options: Options, + settings: Settings, +) -> Result> { // It draws nothing: only the font, which books and chat measure with. let atlas = HudAtlas::fonts(&options.resources)?; let gui = UiResources { @@ -702,8 +754,7 @@ fn serve( }), }; let audio = Audio::silent(&options.resources); - let mut server = Game::new(res, pack, gui, options, settings, audio, None)?; - server.run_integrated(launch, flags, tasks, ready) + Game::new(res, pack, gui, options, settings, audio, None) } #[cfg(test)] diff --git a/crates/horizon-client/src/game/levels.rs b/crates/horizon-client/src/game/levels.rs index f685200f2..6f29f9371 100644 --- a/crates/horizon-client/src/game/levels.rs +++ b/crates/horizon-client/src/game/levels.rs @@ -21,7 +21,7 @@ use super::{ use horizon_client::{ Result, block_entity_anim::BlockEntityAnims, random_tick_sections::RandomTickSections, save::SaveDir, streaming::Streaming, tick_chunk_membership::TickChunkMembership, - ticket_levels::ChunkTickets, world::World, + ticket_levels::ChunkTickets, vanillagen::VanillaTerrain, world::World, }; use horizon_world::level::{LevelEvent, Ticks}; use std::{ @@ -39,6 +39,7 @@ const IDLE_TICKS: u32 = 300; pub(super) struct Parked { world: World, streaming: Streaming, + locator: Option>, save: Arc, block_ticks: Ticks, tick_chunks: TickChunkMembership, @@ -109,13 +110,21 @@ pub(super) struct Levels { impl Game<'_> { /// A new level for `dim`, opened from the save (`MinecraftServer.createLevels`). fn open_level(&self, dim: Dim) -> Result { - let (streaming, save) = - self.make_streaming(&self.world_root.clone(), dim, self.level.seed)?; + let (streaming, save, locator) = super::world_loading::make_streaming( + &self.res, + &self.pack, + &self.options, + &self.settings, + &self.world_root, + dim, + self.level.seed, + )?; let mut world = World::new(self.res.facts().air, self.level.seed); world.fast_lava = dim == Dim::Nether; Ok(Parked { world, streaming, + locator, save, block_ticks: Ticks::new(self.level.seed as i32), tick_chunks: Default::default(), @@ -163,6 +172,9 @@ impl Game<'_> { use std::mem::swap; swap(&mut self.world, &mut p.world); swap(&mut self.streaming, &mut p.streaming); + let mut locator = self.end.locator(); + swap(&mut locator, &mut p.locator); + self.end.set_locator(locator); swap(&mut self.save, &mut p.save); swap(&mut self.block_ticks, &mut p.block_ticks); swap(&mut self.tick_chunks, &mut p.tick_chunks); diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index c89773a5d..6ee8ed278 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -684,6 +684,23 @@ impl Game<'_> { pub(super) fn remote_backlog(&self) -> usize { self.remote.as_ref().map_or(0, |r| r.backlog.len()) } + /// A headless command oracle has a real connection but no movement tick. + /// Acknowledge loaded terrain without predicting or sending movement. + pub(super) fn poll_stationary_client(&mut self) -> Result<()> { + self.poll_remote()?; + let loaded = self.player.as_ref().is_some_and(|p| { + self.streaming.is_lit( + (p.position.x.floor() as i32) >> 4, + (p.position.z.floor() as i32) >> 4, + ) + }); + if loaded && self.remote.as_ref().is_some_and(|r| !r.loaded_sent) { + self.send(S::PlayerLoaded(Empty)); + self.remote.as_mut().unwrap().loaded_sent = true; + } + Ok(()) + } + /// Process what the network thread received (once per frame, like the /// client's packet processor), in order. Chunk packets get /// `CHUNK_PACKETS_PER_FRAME`: once a frame has spent it, the events from diff --git a/crates/horizon-client/src/game/sculk_fixture.rs b/crates/horizon-client/src/game/sculk_fixture.rs index b02dfe998..58e29066d 100644 --- a/crates/horizon-client/src/game/sculk_fixture.rs +++ b/crates/horizon-client/src/game/sculk_fixture.rs @@ -156,6 +156,10 @@ const CLIENT_SOUNDS: &[&str] = &[ impl Game<'_> { /// Run every scenario of `spec` ("SCENARIOS,OUTPUT") and write the results. pub(super) fn sculk_fixture(&mut self, spec: &str) -> Result<()> { + assert!( + !self.options.execution_role.is_client(), + "fixture requires a server Game" + ); let (input, output) = spec .split_once(',') .ok_or("HORIZON_SCULK_FIXTURE=SCENARIOS,OUTPUT")?; diff --git a/crates/horizon-client/src/game/server_travel.rs b/crates/horizon-client/src/game/server_travel.rs index 02b94c0f6..158b2301e 100644 --- a/crates/horizon-client/src/game/server_travel.rs +++ b/crates/horizon-client/src/game/server_travel.rs @@ -170,7 +170,7 @@ impl Game<'_> { /// The player moves to `target`'s level (opened if nobody was there) /// and is told it is there, at the approximate exit, while it loads. The /// level whose tick is running is put back in place afterwards. - fn start_server_trip( + pub(super) fn start_server_trip( &mut self, host: &mut LanHost, conn: u32, diff --git a/docs/parity-workflow.md b/docs/parity-workflow.md index f9f222071..52a5a030f 100644 --- a/docs/parity-workflow.md +++ b/docs/parity-workflow.md @@ -74,6 +74,34 @@ An agent working an area merges the integration branch first, uses `tools/parity On ludodesktop, source `~/code/horizon-parity/env.sh` first: it sets the ALSA build path and the Cargo job cap. Debug builds carry line tables only (`[profile.dev]` in `Cargo.toml`), which keeps backtraces readable while keeping each worktree's build small; a fresh worktree builds `horizon-client` and its tests in under a minute. +## Server execution in game fixtures + +`HORIZON_COMMAND_SCRIPT` runs on the integrated server's `Server thread`, with the +headless client joined through the normal memory connection. Commands use the connected +owner's body and dimension. The script owns the clock: each `tick` calls `tick_server`, +while login and portal arrival pump connection-only passes. The client acknowledges +terrain and teleports without predicting movement, matching the command oracle's stationary +player. Script and save errors return through server shutdown; output keeps the existing +styled messages and `CommandResultCallback` schema. `--in-frame` is not needed. + +`HORIZON_DISPENSE_FIXTURE` and `HORIZON_SCULK_FIXTURE` are component probes. They construct +a server `Game` on a server thread and retain the oracles' synthetic levels, seeded random +streams and explicit entity/block phases. A synthetic sculk player controls warning levels, +not client input; dispenser probes call one activation without running unrelated world ticks. +Keep those controls when extending the fixtures. Player interactions belong in connected-client +coverage alongside the components: + +```sh +python3 tools/test_command_exec_parity.py +python3 tools/test_dispenser_parity.py +python3 tools/test_sculk_parity.py +python3 tools/lan_e2e.py --server-thread --check machines --check fireball --check sculk_container +``` + +The LAN checks open and fill machines through the guest's inventory packets, observe a +powered dispenser's projectile on both clients, and open a chest to activate a sculk sensor +and shrieker. They supplement the deterministic component comparisons. + ## Reading vanilla Besides the Vineflower sources in `.local/minecraft//sources/`, `tools/rea_vanilla.py` queries the shipped bytecode through REA (callers through lambdas, overloads, a second decompiler); see [decompilation status](decompilation-status.md#querying-the-shipped-bytecode-with-rea). diff --git a/tools/command_exec_corpus.py b/tools/command_exec_corpus.py index 4bc202f78..230cf9440 100644 --- a/tools/command_exec_corpus.py +++ b/tools/command_exec_corpus.py @@ -611,7 +611,9 @@ def world_misc(): cmd('/fill 0 210 0 2 212 2 minecraft:glass keep'), cmd('/fill 0 210 0 2 212 2 minecraft:dirt replace #minecraft:base_stone_overworld'), cmd('/fill 0 210 0 2 212 2 minecraft:air strict'), - cmd('/fill 0 210 0 100 300 100 minecraft:stone'), + # A connected server loads more terrain than the oracle's 5x5 core. + # Keep the far corner unambiguously unloaded in both executions. + cmd('/fill 0 210 0 1000 300 1000 minecraft:stone'), cmd('/setblock 0 211 0 minecraft:oak_sign{front_text:{messages:["a","b","c","d"]}}'), cmd('/data get block 0 211 0 front_text.messages'), cmd('/clone 0 211 0 0 211 0 1 211 1 replace move'), @@ -814,6 +816,9 @@ def functions_and_packs(): def ticking(): return [ + # Isolate named-stream commands from earlier loot rolls, including + # ocean vegetation updated while asynchronous terrain loads settle. + *setup('/random reset *'), cmd('/tick query'), cmd('/tick rate 40'), cmd('/tick query'), @@ -1046,9 +1051,25 @@ def test_command(): ] +def server_clock(): + """Only explicit tick operations advance the script's authoritative clock.""" + return [ + *setup('/scoreboard objectives add harness_clock dummy', + '/execute store result score before harness_clock run time query gametime'), + {'op': 'tick', 'n': 3}, + *setup('/execute store result score elapsed harness_clock run time query gametime', + '/scoreboard players operation elapsed harness_clock -= before harness_clock'), + cmd('/scoreboard players get elapsed harness_clock'), + # The connected owner is the only player; @s and @a use the same body. + cmd('/execute as @a run say connected owner'), + *setup('/scoreboard objectives remove harness_clock'), + ] + + def groups(): return [ ('setup', group_setup()), + ('server clock', server_clock()), ('client commands', client_commands()), ('game state', game_state()), ('player state', player_state()), diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 0ec7218d8..0259624e0 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -6398,5 +6398,44 @@ CHECKS += ['sprint-fov'] CHECK_FNS['sprint-fov'] = check_sprint_fov +def check_sculk_container(host, guest, out): + """A real guest's container interaction emits a server game event. Both + clients see the sensor and the non-summoning shrieker activate and reset. + The component oracle separately controls warning levels and seeded mobs. + """ + x, y, z = (int(v // 1) for v in pos_of(guest)) + chest, sensor, shrieker = (x + 2, y, z), (x + 3, y, z), (x + 4, y, z) + guest('command', command='gamemode survival') + for at, block in ((chest, 'chest'), (shrieker, 'sculk_shrieker'), (sensor, 'sculk_sensor')): + guest('command', command=f'setblock {at[0]} {at[1]} {at[2]} minecraft:{block}') + guest.wait(60) + seen = {} + # Keep the sensor's 30-tick active window observable while both clients + # acknowledge the container and the control socket inspects them. + host('command', command='tick rate 4') + try: + guest.raw('interact', *chest) + guest.raw('wait-for', 'screen', 'container', '--timeout', 10) + for game in (host, guest): + active = wait_state(game, sensor, 'sculk_sensor_phase', 'active', 40) + shrieking = wait_state(game, shrieker, 'shrieking', 'true', 40) + seen[game.name] = (active, shrieking) + assert all(s == ('active', 'true') for s in seen.values()), seen + guest.raw('key', 'esc', 'tap') + for game in (host, guest): + assert wait_state(game, sensor, 'sculk_sensor_phase', 'inactive', 160) == 'inactive', seen + assert wait_state(game, shrieker, 'shrieking', 'false', 160) == 'false', seen + finally: + host('command', command='tick rate 20') + if guest('state').get('screen') == 'container': + guest.raw('key', 'esc', 'tap') + for at in (sensor, shrieker, chest): + guest('command', command=f'setblock {at[0]} {at[1]} {at[2]} minecraft:air') + return seen + + +CHECKS += ['sculk_container'] +CHECK_FNS['sculk_container'] = check_sculk_container + if __name__ == '__main__': main() diff --git a/tools/test_command_exec_parity.py b/tools/test_command_exec_parity.py index 877bd0b38..75f38c351 100644 --- a/tools/test_command_exec_parity.py +++ b/tools/test_command_exec_parity.py @@ -4,7 +4,7 @@ Java (VanillaCommandExecOracle) starts a stand-in integrated server: a real MinecraftServer with a generated world, Commands.CommandSelection.INTEGRATED and one single-player owner named Player. Rust runs the same script through -the Horizon client headless (HORIZON_COMMAND_SCRIPT). Both run each command as +Horizon's integrated server with a connected headless client (HORIZON_COMMAND_SCRIPT). Both run each command as the player through Commands.performPrefixedCommand and record every system or chat message the player receives (text and resolved style: colour, bold, italic, underlined, strikethrough, obfuscated) together with the results the @@ -82,7 +82,7 @@ def main(): rust_dir = build / 'rust' shutil.rmtree(rust_dir, ignore_errors=True) rust_dir.mkdir() - env = lab.game_env(HORIZON_COMMAND_SCRIPT=script_path, HORIZON_COMMAND_OUTPUT=build / 'rust.jsonl') + env = lab.game_env(HORIZON_SERVER_THREAD=1, HORIZON_PLAYER_NAME="Player", HORIZON_COMMAND_SCRIPT=script_path, HORIZON_COMMAND_OUTPUT=build / 'rust.jsonl') run([binary, '--resources', lab.RESOURCES, '--world', 'harness', '--seed', SEED, '--radius', 4], 'rust', cwd=rust_dir, env=env) vanilla = [json.loads(l) for l in (build / 'vanilla.jsonl').open(encoding='utf-8')] diff --git a/tools/test_dispenser_parity.py b/tools/test_dispenser_parity.py index a6d1f429a..7ca48cb7f 100644 --- a/tools/test_dispenser_parity.py +++ b/tools/test_dispenser_parity.py @@ -5,7 +5,7 @@ A dispenser or dropper at the origin faces a random direction and holds the case's items; the block in front (and below it) comes from a palette of blocks the behaviours care about, and some cases put a target entity in front. Vanilla `DispenserBlock.dispenseFrom` runs over the headless level of the block update -oracle (`VanillaDispenserOracle`); Horizon runs its game glue in a headless game +oracle (`VanillaDispenserOracle`); Horizon runs its game glue as a component probe on a server thread (`HORIZON_DISPENSE_FIXTURE`). Both print block changes, container contents, spawned entities (type, position, motion), the targets afterwards, level events, sounds, game events and the level random. Motion drawn from an entity's own diff --git a/tools/test_sculk_parity.py b/tools/test_sculk_parity.py index ae2c1c6aa..bff5ad5ba 100644 --- a/tools/test_sculk_parity.py +++ b/tools/test_sculk_parity.py @@ -5,7 +5,7 @@ The Java oracle (VanillaSculkOracle) runs each scenario in a real, headless, fla level: it builds the blocks, places the player (with a warning level) and entities, and ticks the level in ServerLevel.tick's order while scripted game events and block changes happen at given ticks. Horizon runs the same scenarios through its game code (`HORIZON_SCULK_FIXTURE`, the -same functions the game ticks). Both print, per tick, what changed at the watched blocks (state, +same server phases, with synthetic player and entity state). Both print, per tick, what changed at the watched blocks (state, vibration, selector, last frequency, warning level, charge cursors), sounds, level events, the player's warden warning level and darkness, new entities, and the level random. The harness reports each scenario's first differing tick. From 401ac1322ffdd9aa72f2fffd927f135176a0ec98 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 23:43:03 +0100 Subject: [PATCH 192/238] Banners: one loader for saved and generated banners The generated banner reuses save.rs's BannerBlockEntity.loadAdditional (now decode_banner) instead of a copy of it, and the generated block entity clones its NBT only for the kinds that read it. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/save.rs | 23 +++++----- crates/horizon-client/src/structure_blocks.rs | 45 ++++++------------- 2 files changed, 26 insertions(+), 42 deletions(-) diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index da5735c17..69b6a44f5 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -567,6 +567,17 @@ fn merge_block_entity(original: &Tag, loaded: &Tag, current: &Tag) -> Tag { } merged } +/// `BannerBlockEntity.loadAdditional` (and the `components` a block entity keeps). +pub fn decode_banner(tag: &Tag) -> crate::containers::Banner { + crate::containers::Banner { + layers: crate::banner::layers_from_nbt(tag.get("patterns")), + name: text(tag.get("CustomName")), + components: match tag.get("components") { + Some(Tag::Compound(c)) => c.clone(), + _ => Compound::new(), + }, + } +} pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::containers::BlockEntity)> { use crate::containers::BlockEntity; let int = |k: &str| number(tag.get(k)).map(|v| v as i32); @@ -600,17 +611,7 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain return Some((pos, BlockEntity::Lectern(lectern))); } if id == "minecraft:banner" { - return Some(( - pos, - BlockEntity::Banner(crate::containers::Banner { - layers: crate::banner::layers_from_nbt(tag.get("patterns")), - name: text(tag.get("CustomName")), - components: match tag.get("components") { - Some(Tag::Compound(c)) => c.clone(), - _ => Compound::new(), - }, - }), - )); + return Some((pos, BlockEntity::Banner(decode_banner(tag)))); } if id == "minecraft:beacon" { let beacon = crate::containers::Beacon { diff --git a/crates/horizon-client/src/structure_blocks.rs b/crates/horizon-client/src/structure_blocks.rs index da5b75258..ff51116bc 100644 --- a/crates/horizon-client/src/structure_blocks.rs +++ b/crates/horizon-client/src/structure_blocks.rs @@ -128,40 +128,23 @@ fn text(nbt: &Compound, key: &str) -> Option { } } -/// `BannerBlockEntity.loadAdditional`: the `patterns` layers and the custom -/// name a structure template saves with its banner (the End cities' tower -/// tops place black triangles this way). -fn banner_from_nbt(nbt: &Compound) -> crate::containers::Banner { - crate::containers::Banner { - layers: crate::banner::layers_from_nbt(nbt.get(&JavaString::from("patterns"))), - name: text(nbt, "CustomName"), - components: match nbt.get(&JavaString::from("components")) { - Some(Tag::Compound(c)) => c.clone(), - _ => Compound::new(), - }, - } -} - /// The client block entity for one of a chunk's generated block entities, -/// before a container's loot is unpacked. +/// before a container's loot is unpacked: `StructureTemplate.placeInWorld` +/// hands the template's block entity NBT to the level's block entity. fn generated_block_entity(block: &str, nbt: &Compound) -> Option { let mut entity = BlockEntity::for_block(block)?; - let tag = Tag::Compound(nbt.clone()); - // MonsterRoomFeature and the structures' setEntityId. - if let BlockEntity::Spawner(s) = &mut entity { - **s = crate::mobs::spawner::Spawner::load(&tag); - } - if let BlockEntity::TrialSpawner(s) = &mut entity { - **s = crate::mobs::trial_spawner::TrialSpawner::load(&tag); - } - if let BlockEntity::Vault(v) = &mut entity { - **v = crate::vault::Vault::load(&tag); - } - // StructureTemplate.placeInWorld hands the template's block entity NBT to - // the level, so `BannerBlockEntity.loadAdditional` keeps the banner's - // patterns (the End cities' black triangles) and its name. - if let BlockEntity::Banner(b) = &mut entity { - *b = banner_from_nbt(nbt); + match &mut entity { + // MonsterRoomFeature and the structures' setEntityId. + BlockEntity::Spawner(s) => { + **s = crate::mobs::spawner::Spawner::load(&Tag::Compound(nbt.clone())); + } + BlockEntity::TrialSpawner(s) => { + **s = crate::mobs::trial_spawner::TrialSpawner::load(&Tag::Compound(nbt.clone())); + } + BlockEntity::Vault(v) => **v = crate::vault::Vault::load(&Tag::Compound(nbt.clone())), + // BannerBlockEntity.loadAdditional: the End cities' black triangles. + BlockEntity::Banner(b) => *b = crate::save::decode_banner(&Tag::Compound(nbt.clone())), + _ => {} } Some(entity) } From 072f5ce2ee412f735e9b2ec70c29126695dc15b0 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 25 Oct 2026 22:43:07 +0000 Subject: [PATCH 193/238] Fix custom item names above targeted item frames Show the framed item's custom name through the existing world name-label renderer, matching vanilla's HUD and crosshair conditions. Extend the LAN frame check for normal and glow frames. Co-Authored-By: Codex --- crates/horizon-client/src/game/names.rs | 36 +++++++++++++++++++++++++ tools/lan_e2e.py | 17 ++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/game/names.rs b/crates/horizon-client/src/game/names.rs index 7655fb489..d5e8446c3 100644 --- a/crates/horizon-client/src/game/names.rs +++ b/crates/horizon-client/src/game/names.rs @@ -240,6 +240,42 @@ impl Game<'_> { discrete: false, }); } + // ItemFrameRenderer.shouldShowName/getNameTag: only the targeted + // frame's custom-named item, with the HUD visible (also glow frames). + if !self.ui.hide_gui + && let Some((i, distance)) = self.frame_hit() + && self.before_block(distance) + && self.mob_hit().is_none_or(|(_, d)| d > distance) + && let Some(name) = self.item_frames[i] + .item + .as_ref() + .and_then(|item| item.custom_name()) + { + let frame = &self.item_frames[i]; + let (lo, hi) = frame.bounds(); + let center = glam::DVec3::from_array(std::array::from_fn(|a| (lo[a] + hi[a]) / 2.0)); + if center.distance_squared(camera) < 64.0 * 64.0 { + // NAME_TAG is at the entity height (0.5), then the name + // submitter lifts it 0.5. ItemFrameRenderer offsets rendering + // 0.3 toward its facing and -0.25 vertically. + let step = frame.step(); + tags.push(NameTag { + at: center + + glam::DVec3::new( + f64::from(step[0] as f32 * 0.3), + 0.75, + f64::from(step[2] as f32 * 0.3), + ), + lines: vec![(Text::literal(name).resolve_legacy(), 0.0)], + light: self.world.light( + center.x.floor() as i32, + center.y.floor() as i32, + center.z.floor() as i32, + ), + discrete: false, + }); + } + } tags } /// `SubmitNodeCollection.submitNameTag`: a discrete name is drawn once, diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 0ec7218d8..df78489f9 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -3762,17 +3762,21 @@ def check_frames(host, guest, out): # Within a survival player's 3 blocks of reach: a frame on a wall, an armor stand beside it. player('command', command=f'fill {x - 2} {y} {z + 1} {x + 2} {y + 2} {z + 2} minecraft:air') player('command', command=f'fill {x - 2} {y} {z + 3} {x + 2} {y + 2} {z + 3} minecraft:stone') - player('command', command=f'summon minecraft:item_frame {x + 0.5} {y + 1.5} {z + 2.97} ' + frame_kind = 'item_frame' if label == 'host' else 'glow_item_frame' + player('command', command=f'summon minecraft:{frame_kind} {x + 0.5} {y + 1.5} {z + 2.97} ' f'{{block_pos:[I;{x},{y + 1},{z + 2}],Facing:2b}}') player('command', command=f'summon minecraft:armor_stand {x - 1.5} {y} {z + 1.5}') player('command', command='clear @s') - player('command', command='item replace entity @s weapon.mainhand with minecraft:diamond') + frame_name = f'{label} framed diamond' + player('command', command='item replace entity @s weapon.mainhand with ' + f"minecraft:diamond[minecraft:custom_name='{frame_name}']") player.wait(30) try: frame = [o for o in player('status')['objects'] if o['kind'].endswith('item_frame') and o['position'] == [x, y + 1, z + 2]] assert frame, f'the {label} sees no item frame at {[x, y + 1, z + 2]}' player.raw('look-at', f'{x + 0.5:.2f}', f'{y + 1.5:.2f}', f'{z + 2.95:.2f}') + assert frame_name not in (player('status').get('name_tags') or []), 'empty frame name visible' player.raw('mouse', 'right', 'click') player.wait(10) player.raw('mouse', 'right', 'click') @@ -3780,6 +3784,15 @@ def check_frames(host, guest, out): frame = [o for o in player('status')['objects'] if o['id'] == frame[0]['id']][0] assert frame['item'] == 'minecraft:diamond', f'the {label}\'s frame holds {frame["item"]}' assert frame['rotation'] == 1, f'the {label}\'s frame turned to {frame["rotation"]}' + assert frame_name in (player('status').get('name_tags') or []), \ + f"the {label}'s targeted frame has no item name" + player.shot(out, f'{label}-framed-item-name') + player.raw('key', 'f1', 'tap') + player.wait(2) + assert frame_name not in (player('status').get('name_tags') or []), 'F1 left the frame name visible' + player.raw('key', 'f1', 'tap') + player.raw('look-at', f'{x + 2:.2f}', f'{y + 1.5:.2f}', f'{z + 2.95:.2f}') + assert frame_name not in (player('status').get('name_tags') or []), 'untargeted frame name visible' theirs = [o for o in other('status')['objects'] if o['kind'].endswith('item_frame') and o['position'] == [x, y + 1, z + 2]] assert theirs and theirs[0]['item'] == 'minecraft:diamond' and theirs[0]['rotation'] == 1, \ From a41381236362cca38a951ed652adeaef876e0b4a Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 23:46:15 +0100 Subject: [PATCH 194/238] Scroll bars: the screen's GUI size, and a test of the drag stats_scroll and stats_drag take the GUI height from gui_size() instead of a copy of it. The Game Rules list's press, drag and release get the test from #1913 (vlesmentie's take on the same fix). Co-Authored-By: Claude Opus 5.5 --- .../src/game/game_rules_screen.rs | 23 +++++++++++++++++++ .../horizon-client/src/game/stats_screen.rs | 12 ++-------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/crates/horizon-client/src/game/game_rules_screen.rs b/crates/horizon-client/src/game/game_rules_screen.rs index 126bc8804..0992f7689 100644 --- a/crates/horizon-client/src/game/game_rules_screen.rs +++ b/crates/horizon-client/src/game/game_rules_screen.rs @@ -564,6 +564,29 @@ mod tests { ); } + /// AbstractScrollArea: a press on the bar holds it, a drag moves the + /// list (to the ends past them), a release lets go. + #[test] + fn the_scroll_bar_can_be_held_dragged_and_let_go() { + let mut s = RulesScreen::new(&GameRules::default()); + let (w, h) = (427.0, 240.0); + let (top, bottom, content) = (HEADER, h - FOOTER, s.content()); + assert!( + content > bottom - top, + "the rules overflow a 240-high screen" + ); + let bar = RulesScreen::bar(w); + assert!(!s.press_scrollbar((bar - 4.0, top + 5.0), (w, h))); + assert!(s.press_scrollbar((bar + 3.0, top + 5.0), (w, h))); + s.drag(top + 40.0, h); + assert!(s.scroll.amount > 0.0); + s.drag(bottom + 10.0, h); + assert_eq!(s.scroll.amount, content - (bottom - top)); + s.release(); + s.drag(top, h); + assert_eq!(s.scroll.amount, content - (bottom - top)); + } + #[test] fn edits_change_rules_and_bad_numbers_hold_done() { let mut s = RulesScreen::new(&GameRules::default()); diff --git a/crates/horizon-client/src/game/stats_screen.rs b/crates/horizon-client/src/game/stats_screen.rs index e86b29aeb..bd7663e49 100644 --- a/crates/horizon-client/src/game/stats_screen.rs +++ b/crates/horizon-client/src/game/stats_screen.rs @@ -492,11 +492,7 @@ impl Game<'_> { } pub(super) fn stats_scroll(&mut self, amount: f32) { let (row_h, _) = self.stats_rows(); - let size = self - .renderer - .as_ref() - .map_or([1280, 720], |r| [r.config.width, r.config.height]); - let h = HudBatch::new(size).height; + let h = self.gui_size().1; let content = self.stats_content(); self.stats_ui .scroll @@ -517,11 +513,7 @@ impl Game<'_> { if !self.stats_ui.scroll.held() { return; } - let size = self - .renderer - .as_ref() - .map_or([1280, 720], |r| [r.config.width, r.config.height]); - let h = HudBatch::new(size).height; + let h = self.gui_size().1; let content = self.stats_content(); self.stats_ui .scroll From b7629cf6560ac31557d751638910acdc8bf7e0f8 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 23:46:36 +0100 Subject: [PATCH 195/238] Enchantments: say which callers have no language full_name reads the process's language, which the server thread shares with its window; only a process with no language loaded (a dedicated server, the probes) keeps the English names. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/enchant.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/enchant.rs b/crates/horizon-client/src/enchant.rs index f5ddc84c2..2f418ae82 100644 --- a/crates/horizon-client/src/enchant.rs +++ b/crates/horizon-client/src/enchant.rs @@ -337,8 +337,8 @@ impl Enchantments { let language = crate::lang::current(); self.full_name_in(language.as_deref(), id, level) } - /// `full_name` against `language`; `None` keeps the English names loaded - /// above, which is what the server thread and the probes see. + /// `full_name` against `language`; `None` (no language loaded: a + /// dedicated server, the probes) keeps the English names loaded above. pub fn full_name_in( &self, language: Option<&crate::lang::Language>, From 9d5301627bbb81bf46113a9c41dff841d2d4f2e1 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 23:55:43 +0100 Subject: [PATCH 196/238] Chickens: the flap test uses the shared test mob type chicken_flap reads only the ground flag, so the test builds its mob from mobs::sensor_scan_tests::kind() instead of a 50-line copy of a MobType. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/mobs/farm.rs | 54 +------------------------- 1 file changed, 2 insertions(+), 52 deletions(-) diff --git a/crates/horizon-client/src/mobs/farm.rs b/crates/horizon-client/src/mobs/farm.rs index aa02c901d..83d5ac1f5 100644 --- a/crates/horizon-client/src/mobs/farm.rs +++ b/crates/horizon-client/src/mobs/farm.rs @@ -136,61 +136,11 @@ pub fn sheep_color(climate: Climate, random: &mut impl RandomSource) -> u8 { #[cfg(test)] mod tests { use super::*; - use crate::mobs::{Anim, Brain, Flags, MobType, Part, Species}; use horizon_core::geometry::Vec3; - use serde_json::json; - use std::collections::HashMap; + /// `chicken_flap` reads only the ground flag: any mob type will do. fn chicken() -> Mob { - let model = Arc::new(Part::parse( - "root", - &json!({"children": {"right_wing": {}, "left_wing": {}}}), - )); - let kind = Arc::new(MobType { - id: "minecraft:chicken".into(), - species: Species::Chicken, - width: 0.4, - height: 0.7, - eye_height: 0.644, - category: "creature".into(), - max_health: 4.0, - speed: 0.25, - 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, - layers: HashMap::new(), - texture: String::new(), - brain: Brain::Passive, - anim: Anim::Chicken, - sounds: [""; 3], - step_sound: None, - food: Arc::default(), - tempt: Arc::default(), - }); - Mob::new(1, kind, Vec3::ZERO, 0.0) + Mob::new(1, crate::mobs::sensor_scan_tests::kind(), Vec3::ZERO, 0.0) } #[test] From 4be53ee28a64f126a1ab08b929eae354cba138c4 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 25 Oct 2026 22:58:00 +0000 Subject: [PATCH 197/238] Reduce resource preparation filesystem work Share the asset index and verification state, stage checked imports while hashing, preserve indexed replacements, and clone independent resource copies where supported. Co-Authored-By: Codex (GPT-6.1) --- Cargo.lock | 2 + crates/horizon-prepare/Cargo.toml | 4 + crates/horizon-prepare/src/artifacts.rs | 78 +++-- crates/horizon-prepare/src/files.rs | 180 ++++++++++++ crates/horizon-prepare/src/lib.rs | 105 +++++-- crates/horizon-prepare/src/sounds.rs | 362 +++++++++++++++++++----- docs/prepare-performance.md | 44 +++ launcher/Cargo.lock | 2 + launcher/src/backend.rs | 2 +- 9 files changed, 637 insertions(+), 142 deletions(-) create mode 100644 crates/horizon-prepare/src/files.rs create mode 100644 docs/prepare-performance.md diff --git a/Cargo.lock b/Cargo.lock index 4d749add6..bb4e72446 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1408,6 +1408,8 @@ dependencies = [ name = "horizon-prepare" version = "0.1.0" dependencies = [ + "crc32fast", + "libc", "regex", "sha1", "sha2", diff --git a/crates/horizon-prepare/Cargo.toml b/crates/horizon-prepare/Cargo.toml index 3d62202bd..9bcf5464d 100644 --- a/crates/horizon-prepare/Cargo.toml +++ b/crates/horizon-prepare/Cargo.toml @@ -6,11 +6,15 @@ license = "MIT" description = "Prepares Horizon's resources from the player's own Minecraft: imports and checks the pinned jars, runs the vanilla exporters, writes the resource folder" [dependencies] +crc32fast = "1" regex = { version = "1", default-features = false, features = ["std", "unicode-perl"] } sha1 = "0.10" sha2 = "0.10" zip = { version = "2", default-features = false, features = ["deflate"] } +[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies] +libc = "0.2" + [build-dependencies] sha2 = "0.10" diff --git a/crates/horizon-prepare/src/artifacts.rs b/crates/horizon-prepare/src/artifacts.rs index 805dd80d4..a3bf945d5 100644 --- a/crates/horizon-prepare/src/artifacts.rs +++ b/crates/horizon-prepare/src/artifacts.rs @@ -143,31 +143,17 @@ pub(crate) fn import_copy( algorithm: &str, hint: &str, ) -> Result { - fs::create_dir_all(destination.parent().unwrap()).map_err(|e| e.to_string())?; - if destination.exists() && digest(destination, algorithm)? == checksum { - return Ok(destination.to_path_buf()); - } - if !source.is_file() { - return Err(format!("Missing {}. {hint}", source.display()) - .trim() - .to_string()); - } - let actual = digest(source, algorithm)?; - if actual != checksum { - return Err(format!( - "{} is not the pinned file: expected {algorithm} {checksum}, got {actual}", - source.display() - )); - } - let temporary = destination.with_file_name(format!( - ".{}.partial-{}", - destination.file_name().unwrap().to_string_lossy(), - std::process::id() - )); - let copied = fs::copy(source, &temporary).and_then(|_| fs::rename(&temporary, destination)); - let _ = fs::remove_file(&temporary); - copied.map_err(|e| format!("{}: {e}", destination.display()))?; - Ok(destination.to_path_buf()) + crate::files::Import::stage(source, destination, checksum, algorithm, false) + .map_err(|error| { + if !source.is_file() { + format!("Missing {}. {hint}", source.display()) + .trim() + .to_string() + } else { + error + } + })? + .commit() } /// The pinned version JSON from the launcher's versions/ directory. The launcher's copy wins @@ -653,9 +639,17 @@ pub struct Artifacts { /// Reuses checked imports from this root or another installed version beside it. pub fn reuse_imported(root: &Path, log: Log) -> Result { + Ok(reuse_verified(root, log)?.is_some()) +} + +/// Keep this preparation's verified artifacts and client libraries for the exporters. +pub(crate) fn reuse_verified( + root: &Path, + log: Log, +) -> Result)>, String> { let root = fs::canonicalize(root).map_err(|e| format!("{}: {e}", root.display()))?; let lock = Lock::read(&root)?; - let checked = |directory: PathBuf| -> Result, String> { + let checked = |directory: PathBuf| -> Result<(Artifacts, Vec), String> { let candidate = Lock { json: lock.json.clone(), version: lock.version.clone(), @@ -664,17 +658,9 @@ pub fn reuse_imported(root: &Path, log: Log) -> Result { }; let found = verify(&candidate)?; let client = client_libraries(&candidate, &found.manifest)?; - let mut files = vec![ - candidate.directory.join("manifest.json"), - candidate.directory.join("jars/client.jar"), - candidate.directory.join("jars/server.jar"), - found.jars["server"].clone(), - ]; - files.extend(found.libraries); - files.extend(client); - Ok(files) + Ok((found, client)) }; - if checked(lock.directory.clone()).is_ok() { + if let Ok((found, client_libraries)) = checked(lock.directory.clone()) { if [ "prepared.json", "reports/client-inventory.json", @@ -683,8 +669,6 @@ pub fn reuse_imported(root: &Path, log: Log) -> Result { .iter() .any(|path| !lock.directory.join(path).is_file()) { - let found = verify(&lock)?; - let client_libraries = client_libraries(&lock, &found.manifest)?; write_import_metadata( &root, &lock, @@ -696,10 +680,10 @@ pub fn reuse_imported(root: &Path, log: Log) -> Result { )?; } log(&format!("Using imported Minecraft {}", lock.version)); - return Ok(true); + return Ok(Some((found, client_libraries))); } let Some(versions) = root.parent() else { - return Ok(false); + return Ok(None); }; // Sibling discovery is intended for the launcher's versions/ folder. let mut siblings: Vec<_> = fs::read_dir(versions) @@ -711,9 +695,17 @@ pub fn reuse_imported(root: &Path, log: Log) -> Result { siblings.sort(); for sibling in siblings { let source = sibling.join(".local/minecraft").join(&lock.version); - let Ok(files) = checked(source.clone()) else { + let Ok((found, client)) = checked(source.clone()) else { continue; }; + let mut files = vec![ + source.join("manifest.json"), + source.join("jars/client.jar"), + source.join("jars/server.jar"), + found.jars["server"].clone(), + ]; + files.extend(found.libraries); + files.extend(client); for file in files { let relative = file.strip_prefix(&source).map_err(|e| e.to_string())?; import_copy( @@ -740,9 +732,9 @@ pub fn reuse_imported(root: &Path, log: Log) -> Result { lock.version, sibling.display() )); - return Ok(true); + return Ok(Some((found, client_libraries))); } - Ok(false) + Ok(None) } pub fn verify(lock: &Lock) -> Result { diff --git a/crates/horizon-prepare/src/files.rs b/crates/horizon-prepare/src/files.rs new file mode 100644 index 000000000..484c7bf7b --- /dev/null +++ b/crates/horizon-prepare/src/files.rs @@ -0,0 +1,180 @@ +//! Independent copies, using filesystem cloning when available. Never hard links: exporters +//! rewrite prepared resources in place. Checked imports publish only after verifying the copy. + +use crate::{digest, hex}; +use sha2::Digest; +use std::fs; +use std::io::{self, Read, Write}; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicUsize, Ordering}; + +fn clone_file(from: &Path, to: &Path) -> io::Result<()> { + #[cfg(target_os = "linux")] + { + use std::os::fd::AsRawFd; + let source = fs::File::open(from)?; + let target = fs::File::create(to)?; + // SAFETY: FICLONE takes a source fd, both descriptors stay open for the call. + if unsafe { libc::ioctl(target.as_raw_fd(), libc::FICLONE, source.as_raw_fd()) } == 0 { + return Ok(()); + } + Err(io::Error::last_os_error()) + } + #[cfg(target_os = "macos")] + { + use std::ffi::CString; + use std::os::unix::ffi::OsStrExt; + let path = |p: &Path| { + CString::new(p.as_os_str().as_bytes()) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e)) + }; + let (source, target) = (path(from)?, path(to)?); + // SAFETY: both paths are valid NUL-terminated strings for the duration of the call. + if unsafe { libc::clonefile(source.as_ptr(), target.as_ptr(), 0) } == 0 { + return Ok(()); + } + Err(io::Error::last_os_error()) + } + #[cfg(not(any(target_os = "linux", target_os = "macos")))] + { + let _ = (from, to); + Err(io::ErrorKind::Unsupported.into()) + } +} + +/// Copies an independent file, attempting a reflink first. The destination may be replaced. +pub fn clone_or_copy(from: &Path, to: &Path) -> io::Result<()> { + clone_file(from, to).or_else(|_| fs::copy(from, to).map(|_| ())) +} + +fn copy_hash(from: &Path, to: &Path) -> io::Result { + let mut source = fs::File::open(from)?; + let mut target = fs::File::create(to)?; + let mut hash = D::new(); + let mut buffer = vec![0; 64 * 1024]; + loop { + let n = source.read(&mut buffer)?; + if n == 0 { + return Ok(hex(&hash.finalize())); + } + target.write_all(&buffer[..n])?; + hash.update(&buffer[..n]); + } +} + +/// A verified replacement staged beside its destination. Dropping an unfinished set cleans +/// every temporary file, so a corrupt late panorama face cannot replace earlier placeholders. +pub(crate) struct Import { + temporary: Option, + destination: PathBuf, +} + +impl Import { + pub(crate) fn stage( + source: &Path, + destination: &Path, + checksum: &str, + algorithm: &str, + verified: bool, + ) -> Result { + let mut import = Self { + temporary: None, + destination: destination.into(), + }; + if verified || (destination.is_file() && digest(destination, algorithm)? == checksum) { + return Ok(import); + } + fs::create_dir_all(destination.parent().unwrap()).map_err(|e| e.to_string())?; + static COPIES: AtomicUsize = AtomicUsize::new(0); + let temporary = destination.with_file_name(format!( + ".{}.partial-{}-{}", + destination.file_name().unwrap().to_string_lossy(), + std::process::id(), + COPIES.fetch_add(1, Ordering::Relaxed) + )); + import.temporary = Some(temporary.clone()); + let actual = if clone_file(source, &temporary).is_ok() { + digest(&temporary, algorithm)? + } else { + match algorithm { + "sha1" => copy_hash::(source, &temporary), + _ => copy_hash::(source, &temporary), + } + .map_err(|e| format!("{}: {e}", source.display()))? + }; + if actual != checksum { + return Err(format!( + "{} is not the pinned file: expected {algorithm} {checksum}, got {actual}", + source.display() + )); + } + Ok(import) + } + + pub(crate) fn commit(mut self) -> Result { + if let Some(temporary) = &self.temporary { + fs::rename(temporary, &self.destination) + .map_err(|e| format!("{}: {e}", self.destination.display()))?; + } + self.temporary = None; + Ok(self.destination.clone()) + } +} + +impl Drop for Import { + fn drop(&mut self) { + if let Some(temporary) = &self.temporary { + let _ = fs::remove_file(temporary); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn copies_are_independent_and_checked_before_publication() { + let dir = std::env::temp_dir().join(format!("horizon-file-copy-{}", std::process::id())); + fs::create_dir_all(&dir).unwrap(); + let source = dir.join("source"); + let target = dir.join("target"); + let data = vec![42; 200_000]; // More than one streaming buffer. + fs::write(&source, &data).unwrap(); + for algorithm in ["sha1", "sha256"] { + let checksum = digest(&source, algorithm).unwrap(); + Import::stage(&source, &target, &checksum, algorithm, false) + .unwrap() + .commit() + .unwrap(); + assert_eq!(digest(&target, algorithm).unwrap(), checksum); + fs::write(&target, b"old destination").unwrap(); + assert_eq!(fs::read(&source).unwrap(), data); + assert!(Import::stage(&source, &target, "bad hash", algorithm, false).is_err()); + assert_eq!(fs::read(&target).unwrap(), b"old destination"); + // Explicitly exercise ordinary streaming copies even on a reflink filesystem. + let streamed = if algorithm == "sha1" { + copy_hash::(&source, &target).unwrap() + } else { + copy_hash::(&source, &target).unwrap() + }; + assert_eq!(streamed, checksum); + assert_eq!(fs::read(&target).unwrap(), data); + } + clone_or_copy(&source, &target).unwrap(); + fs::write(&target, b"rewrite").unwrap(); + assert_eq!(fs::read(&source).unwrap(), data); + let staged = Import::stage( + &source, + &target, + &digest(&source, "sha1").unwrap(), + "sha1", + false, + ) + .unwrap(); + assert_eq!(fs::read_dir(&dir).unwrap().count(), 3); + drop(staged); + assert_eq!(fs::read_dir(&dir).unwrap().count(), 2); + fs::remove_dir_all(dir).unwrap(); + } +} diff --git a/crates/horizon-prepare/src/lib.rs b/crates/horizon-prepare/src/lib.rs index c7e443c7d..daee30444 100644 --- a/crates/horizon-prepare/src/lib.rs +++ b/crates/horizon-prepare/src/lib.rs @@ -11,12 +11,14 @@ //! run, keyed by the sha256 of their inputs and outputs recorded in the manifest, as before. pub mod artifacts; +mod files; mod inventory; pub mod java; pub mod pyjson; mod sounds; mod survival; +pub use files::clone_or_copy; pub use sounds::{fonts, languages, packs, panorama, sounds, BUILT_IN_PACKS}; use pyjson::Value; @@ -410,16 +412,28 @@ pub fn prepare(options: &Options, log: Log) -> Result { &artifacts::minecraft_dir(options.minecraft_dir.as_deref()).join("runtime"), )); let jdk = java::find(lock.java_major, &homes)?; - if options.import && !artifacts::reuse_imported(root, log)? { - artifacts::import( - root, - &lock, - options.minecraft_dir.as_deref(), - options.server_jar.as_deref(), - log, - )?; - } - let found = artifacts::verify(&lock)?; + let reused = if options.import { + artifacts::reuse_verified(root, log)? + } else { + None + }; + let (found, client_libraries) = match reused { + Some(checked) => checked, + None => { + if options.import { + artifacts::import( + root, + &lock, + options.minecraft_dir.as_deref(), + options.server_jar.as_deref(), + log, + )?; + } + let found = artifacts::verify(&lock)?; + let libraries = artifacts::client_libraries(&lock, &found.manifest)?; + (found, libraries) + } + }; let local = root.join(".local"); let resources = local.join("horizon-client").join(&lock.version); mkdir(&resources)?; @@ -445,7 +459,6 @@ pub fn prepare(options: &Options, log: Log) -> Result { } else { Value::Object(Vec::new()) }; - let client_libraries = artifacts::client_libraries(&lock, &found.manifest)?; let run = Run { root: root.clone(), local, @@ -679,25 +692,9 @@ fn steps( // and every data-pack resource (worldgen, recipes, loot tables, tags), and // the vanilla pack's icon (`pack.png`) for the pack screen. (run.log)("Copying the client's assets and data"); - let mut count = 0usize; - let mut client = open_zip(&run.client)?; - let version = pyjson::loads(&zip_text(&mut client, "version.json")?)?; - for name in zip_names(&client) { - let wanted = name.starts_with("assets/") || name.starts_with("data/") || name == "pack.png"; - if !wanted || name.ends_with('/') { - continue; - } - let relative = member_path(&name, "Invalid resource path")?; - let data = zip_bytes(&mut client, &name)?; - write_if_changed(&root.join(relative), &data)?; - count += 1; - } - - // The JAR holds only gray 1x1 panorama placeholders and an empty Unifont - // include, and none of the built-in packs. Import the indexed ones afterwards, including on reruns that just - // extracted the JAR again. They are optional when preparing from - // already-imported JARs. - sounds::optional_assets(repo, minecraft, run.log); + let mut indexed = sounds::Assets::load(repo, minecraft, run.log); + let (version, count) = extract_client_resources(&run.client, root, indexed.as_mut().ok())?; + sounds::optional_imports(indexed.as_mut().map_err(|e| e.as_str()), run.log); // One file each, exported again when the exporter or the jar it reads changed. let single = |what: &str, @@ -931,6 +928,54 @@ fn steps( ) } +/// Check existing JAR assets by their ZIP size/CRC without inflating every member again. +/// Indexed panorama/font/language replacements have stronger SHA-1 checks and take precedence. +fn extract_client_resources( + jar: &Path, + root: &Path, + mut indexed: Option<&mut sounds::Assets>, +) -> Result<(Value, usize), String> { + let mut client = open_zip(jar)?; + let version = pyjson::loads(&zip_text(&mut client, "version.json")?)?; + let mut count = 0; + for name in zip_names(&client) { + if !(name.starts_with("assets/") || name.starts_with("data/") || name == "pack.png") + || name.ends_with('/') + { + continue; + } + let relative = member_path(&name, "Invalid resource path")?; + count += 1; + if let Some(assets) = &mut indexed { + if assets.imported(&name)? { + continue; + } + } + let target = root.join(relative); + let member = client.by_name(&name).map_err(|e| e.to_string())?; + let (size, crc) = (member.size(), member.crc32()); + drop(member); + if target.is_file() && fs::metadata(&target).map_err(|e| e.to_string())?.len() == size { + let mut file = fs::File::open(&target).map_err(|e| e.to_string())?; + let mut hash = crc32fast::Hasher::new(); + let mut buffer = [0; 16 * 1024]; + loop { + let n = file.read(&mut buffer).map_err(|e| e.to_string())?; + if n == 0 { + break; + } + hash.update(&buffer[..n]); + } + if hash.finalize() == crc { + continue; + } + } + mkdir(target.parent().unwrap())?; + write(&target, &zip_bytes(&mut client, &name)?)?; + } + Ok((version, count)) +} + fn datagen_reports_missing(root: &Path) -> bool { ["items.json", "registries.json", "commands.json"] .iter() diff --git a/crates/horizon-prepare/src/sounds.rs b/crates/horizon-prepare/src/sounds.rs index 15d4a7a08..fd171ec12 100644 --- a/crates/horizon-prepare/src/sounds.rs +++ b/crates/horizon-prepare/src/sounds.rs @@ -5,7 +5,8 @@ use crate::artifacts::{self, Lock}; use crate::pyjson::Value; -use crate::{digest, read_json, Log}; +use crate::{digest, Log}; +use std::collections::{HashMap, HashSet}; use std::fs; use std::path::{Path, PathBuf}; @@ -18,8 +19,9 @@ fn launcher_assets( lock: &Lock, manifest: &Value, minecraft: &Path, + cached_index: &Path, log: Log, -) -> Result<(PathBuf, PathBuf), String> { +) -> Result<(PathBuf, PathBuf, Vec), String> { let assets = minecraft.join("assets"); let info = manifest .get("assetIndex") @@ -33,7 +35,10 @@ fn launcher_assets( if artifacts::safe_relative(id).is_none_or(|p| p.components().count() != 1) { return Err(format!("Unsafe asset index id {id:?}")); } - let index = assets.join("indexes").join(format!("{id}.json")); + let mut index = assets.join("indexes").join(format!("{id}.json")); + if !index.is_file() && cached_index.is_file() { + index = cached_index.to_path_buf(); + } if !index.is_file() { let version = manifest.get("id").and_then(Value::as_str).unwrap_or(""); return Err(format!( @@ -42,7 +47,8 @@ fn launcher_assets( artifacts::launcher_hint(version) )); } - let checksum = digest(&index, "sha1")?; + let bytes = fs::read(&index).map_err(|e| format!("{}: {e}", index.display()))?; + let checksum = crate::hex(&::digest(&bytes)); if checksum != sha1 && !lock.asset_index_accepted(&checksum) { log(&format!( "{} is an asset index Horizon does not know yet (sha1 {checksum}); Mojang may have \ @@ -50,7 +56,7 @@ fn launcher_assets( index.display() )); } - Ok((assets, index)) + Ok((assets, index, bytes)) } /// Where the launcher's asset store keeps the object with this SHA-1. @@ -61,24 +67,6 @@ fn object_path(assets: &Path, checksum: &str) -> Result { Ok(assets.join("objects").join(&checksum[..2]).join(checksum)) } -/// One verified object from the launcher's asset store (tools/lab.py `launcher_object`). -fn launcher_object(assets: &Path, checksum: &str) -> Result { - let path = object_path(assets, checksum)?; - if !path.is_file() { - return Err(format!( - "Missing asset object {}. Play the pinned version once in your launcher.", - path.display() - )); - } - if digest(&path, "sha1")? != checksum { - return Err(format!( - "Corrupt asset object {}; let the launcher repair it", - path.display() - )); - } - Ok(path) -} - /// Whether the game plays this asset: sounds.json and everything under sounds/. fn is_sound(name: &str) -> bool { name == "minecraft/sounds.json" || name.starts_with("minecraft/sounds/") @@ -127,26 +115,56 @@ fn is_language(name: &str) -> bool { } /// Full preparation can run from cached JARs without an official launcher install. -pub(crate) fn optional_assets(root: &Path, minecraft: Option<&Path>, log: Log) { - if let Err(error) = panorama(root, minecraft, log) { - log(&format!( - "Title panorama not imported: {error}; run horizon-prepare panorama later" - )); - } - if let Err(error) = fonts(root, minecraft, log) { - log(&format!( - "Unifont glyphs not imported: {error}; run horizon-prepare fonts later" - )); - } - if let Err(error) = packs(root, minecraft, log) { - log(&format!( - "Programmer Art and High Contrast not imported: {error}; run horizon-prepare packs later" - )); - } - if let Err(error) = languages(root, minecraft, log) { - log(&format!( - "Languages not imported: {error}; run horizon-prepare languages later" - )); +#[cfg(test)] +fn optional_assets(root: &Path, minecraft: Option<&Path>, log: Log) { + optional_imports( + Assets::load(root, minecraft, log) + .as_mut() + .map_err(|e| e.as_str()), + log, + ); +} + +pub(crate) fn optional_imports(mut assets: Result<&mut Assets, &str>, log: Log) { + for (wanted, label, required, failure, command) in [ + ( + is_panorama as fn(&str) -> bool, + "Panorama", + PANORAMA.as_slice(), + "Title panorama", + "panorama", + ), + ( + is_font, + "Font", + UNIFONT.as_slice(), + "Unifont glyphs", + "fonts", + ), + ( + is_built_in_pack, + "Built-in pack", + BUILT_IN_PACKS.as_slice(), + "Programmer Art and High Contrast", + "packs", + ), + ( + is_language, + "Language", + LANGUAGES.as_slice(), + "Languages", + "languages", + ), + ] { + let result = match &mut assets { + Ok(assets) => assets.copy(wanted, label, required, log).map(|_| ()), + Err(error) => Err(error.to_string()), + }; + if let Err(error) = result { + log(&format!( + "{failure} not imported: {error}; run horizon-prepare {command} later" + )); + } } } @@ -207,29 +225,98 @@ fn copy_assets( label: &str, required: &[&str], ) -> Result { - let lock = Lock::read(root)?; - let minecraft = artifacts::minecraft_dir(minecraft); - let manifest = artifacts::import_manifest(&lock, &minecraft)?; - let (store, index) = launcher_assets(&lock, &manifest, &minecraft, log)?; - let id = manifest - .get("id") - .and_then(Value::as_str) - .ok_or("The version JSON has no id")?; - let target = root.join(".local/horizon-client").join(id); - fs::create_dir_all(&target).map_err(|e| format!("{}: {e}", target.display()))?; - fs::copy(&index, target.join("asset-index.json")) - .map_err(|e| format!("{}: {e}", target.join("asset-index.json").display()))?; - let objects = read_json(&index)?; - let objects = objects - .get("objects") - .and_then(Value::as_object) - .ok_or_else(|| format!("{} has no objects", index.display()))?; + Assets::load(root, minecraft, log)?.copy(wanted_asset, label, required, log) +} + +/// A preparation's index and verified destinations. Never persisted: a later invocation +/// checks the bytes again, including when repairing a corrupt prepared file. +pub(crate) struct Assets { + store: PathBuf, + target: PathBuf, + objects: Vec<(String, Value)>, + verified: HashSet, +} + +impl Assets { + pub(crate) fn load(root: &Path, minecraft: Option<&Path>, log: Log) -> Result { + let lock = Lock::read(root)?; + let minecraft = artifacts::minecraft_dir(minecraft); + let manifest = artifacts::import_manifest(&lock, &minecraft)?; + let target = root.join(".local/horizon-client").join(&lock.version); + let (store, index, bytes) = launcher_assets( + &lock, + &manifest, + &minecraft, + &target.join("asset-index.json"), + log, + )?; + let text = std::str::from_utf8(&bytes).map_err(|e| e.to_string())?; + let objects = crate::pyjson::loads(text)?; + let objects = objects + .get("objects") + .and_then(Value::as_object) + .ok_or_else(|| format!("{} has no objects", index.display()))? + .to_vec(); + fs::create_dir_all(&target).map_err(|e| e.to_string())?; + crate::write_if_changed(&target.join("asset-index.json"), &bytes)?; + Ok(Self { + store, + target, + objects, + verified: HashSet::new(), + }) + } + + /// Preserve indexed replacements instead of overwriting them with JAR placeholders. + pub(crate) fn imported(&mut self, name: &str) -> Result { + let Some(name) = name.strip_prefix("assets/") else { + return Ok(false); + }; + if !(is_panorama(name) || is_font(name) || is_language(name)) { + return Ok(false); + } + let Some((_, info)) = self.objects.iter().rev().find(|(n, _)| n == name) else { + return Ok(false); + }; + let Some(hash) = info.get("hash").and_then(Value::as_str) else { + return Ok(false); + }; + let destination = self.target.join("assets").join(name); + if destination.is_file() && digest(&destination, "sha1")? == hash { + self.verified.insert(destination); + return Ok(true); + } + Ok(false) + } + + fn copy( + &mut self, + wanted_asset: fn(&str) -> bool, + label: &str, + required: &[&str], + log: Log, + ) -> Result { + copy_indexed(self, wanted_asset, label, required, log) + } +} + +fn copy_indexed( + assets: &mut Assets, + wanted_asset: fn(&str) -> bool, + label: &str, + required: &[&str], + log: Log, +) -> Result { + let objects = &assets.objects; // A repeated name keeps its first place and its last value, as a Python dict does. let mut wanted: Vec<(&str, &Value)> = Vec::new(); + let mut positions = HashMap::new(); for (name, info) in objects.iter().filter(|(name, _)| wanted_asset(name)) { - match wanted.iter_mut().find(|(n, _)| n == name) { - Some(entry) => entry.1 = info, - None => wanted.push((name, info)), + if let Some(&position) = positions.get(name.as_str()) { + wanted[position] = (name.as_str(), info); + } else { + positions.insert(name.as_str(), wanted.len()); + wanted.push((name, info)); } } for name in required { @@ -251,21 +338,45 @@ fn copy_assets( .ok_or_else(|| format!("{name} has no size in the asset index"))?; let relative = artifacts::safe_relative(name) .ok_or_else(|| format!("Unsafe asset name {name:?} in the asset index"))?; - let source = launcher_object(&store, hash)?; - copies.push((source, target.join("assets").join(relative), hash)); + let source = object_path(&assets.store, hash)?; + let destination = assets.target.join("assets").join(relative); + let import = crate::files::Import::stage( + &source, + &destination, + hash, + "sha1", + assets.verified.contains(&destination), + ) + .map_err(|error| { + if !source.is_file() { + format!( + "Missing asset object {}. Play the pinned version once in your launcher.", + source.display() + ) + } else if error.contains(" is not the pinned file:") { + format!( + "Corrupt asset object {}; let the launcher repair it: {error}", + source.display() + ) + } else { + error + } + })?; + copies.push((import, destination)); total += size; } // Check the entire set before replacing any placeholders: a missing late face // must not leave a mixture of real textures and gray faces in either entry point. - for (source, destination, hash) in copies { - artifacts::import_copy(&source, &destination, hash, "sha1", "")?; + for (import, destination) in copies { + import.commit()?; + assets.verified.insert(destination); } log(&format!( "{label} assets ready: {} files, {:.1} MB", wanted.len(), total as f64 / 1e6 )); - Ok(target) + Ok(assets.target.clone()) } #[cfg(test)] @@ -680,6 +791,7 @@ mod tests { "minecraft/sounds.json is missing from the asset index" ); fs::remove_file(&index).unwrap(); + fs::remove_file(f.root.join(".local/horizon-client/9.9/asset-index.json")).unwrap(); let error = f.run().unwrap_err(); assert!( error.starts_with("Missing ") && error.contains("play 9.9 once"), @@ -687,6 +799,120 @@ mod tests { ); } + #[test] + fn late_corruption_discards_all_staged_replacements() { + let f = Fixture::new("late-corruption"); + let target = f.root.join(".local/horizon-client/9.9/assets"); + for name in PANORAMA { + let path = target.join(name); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, b"placeholder").unwrap(); + } + fs::write(f.object(FIRST_FACE + 6), b"bad overlay").unwrap(); + assert!(panorama(&f.root, Some(&f.minecraft), &|_| {}).is_err()); + for name in PANORAMA { + assert_eq!(fs::read(target.join(name)).unwrap(), b"placeholder"); + } + assert_eq!( + fs::read_dir(target.join("minecraft/textures/gui/title/background")) + .unwrap() + .count(), + 7 + ); + } + + #[test] + fn one_index_snapshot_serves_every_import_and_reruns_repair_corruption() { + let f = Fixture::with_extras("snapshot", &PANORAMA, &[("pack.mcmeta", b"{}")]); + let mut assets = Assets::load(&f.root, Some(&f.minecraft), &|_| {}).unwrap(); + fs::remove_file(f.minecraft.join("assets/indexes/7.json")).unwrap(); + let lines = std::cell::RefCell::new(Vec::new()); + optional_imports(Ok(&mut assets), &|line| { + lines.borrow_mut().push(line.to_string()) + }); + assert_eq!(lines.borrow().len(), 4); + assert!(lines + .borrow() + .iter() + .all(|line| line.contains("assets ready"))); + assets + .copy(is_sound, "Sound", &["minecraft/sounds.json"], &|_| {}) + .unwrap(); + let sound = assets + .target + .join("assets/minecraft/sounds/ambient/cave1.ogg"); + let time = fs::metadata(&sound).unwrap().modified().unwrap(); + fs::remove_file(f.object(1)).unwrap(); + f.run().unwrap(); // A verified destination works even without the source object/index. + assert_eq!(fs::metadata(&sound).unwrap().modified().unwrap(), time); + fs::write(&sound, b"corrupt").unwrap(); + assert!(f.run().is_err()); // Verification never carries over to another invocation. + fs::write(f.object(1), &f.objects[1].1).unwrap(); + f.run().unwrap(); + assert_eq!(fs::read(sound).unwrap(), f.objects[1].1); + } + + #[test] + fn jar_reruns_preserve_indexed_assets_and_repair_missing_or_corrupt_members() { + use std::io::Write; + let f = Fixture::with_extras( + "extract", + &PANORAMA, + &[ + ("pack.mcmeta", b"{}"), + ("minecraft/lang/en_us.json", b"{\"indexed\":true}"), + ], + ); + optional_assets(&f.root, Some(&f.minecraft), &|_| {}); + let target = f.root.join(".local/horizon-client/9.9"); + let jar = f.root.join("client.jar"); + let mut zip = zip::ZipWriter::new(fs::File::create(&jar).unwrap()); + for (name, data) in [ + ("version.json", "{\"world_version\": 1}"), + ( + "assets/minecraft/textures/gui/title/background/panorama_0.png", + "gray", + ), + ("assets/minecraft/font/include/unifont.json", "{}"), + ("assets/minecraft/lang/en_us.json", "{}"), + ("data/minecraft/test.json", "data"), + ("pack.png", "icon"), + ] { + zip.start_file(name, zip::write::SimpleFileOptions::default()) + .unwrap(); + zip.write_all(data.as_bytes()).unwrap(); + } + zip.finish().unwrap(); + let face = target.join("assets").join(PANORAMA[0]); + let time = fs::metadata(&face).unwrap().modified().unwrap(); + // Preparing from cached JARs must also preserve full imports without the launcher. + fs::remove_dir_all(f.minecraft.join("assets")).unwrap(); + for _ in 0..2 { + let mut assets = Assets::load(&f.root, Some(&f.minecraft), &|_| {}).unwrap(); + let (_, count) = + crate::extract_client_resources(&jar, &target, Some(&mut assets)).unwrap(); + assert_eq!(count, 5); + optional_imports(Ok(&mut assets), &|_| {}); + assert_eq!(fs::read(&face).unwrap(), PANORAMA[0].as_bytes()); + assert_eq!(fs::metadata(&face).unwrap().modified().unwrap(), time); + assert_eq!( + fs::read(target.join("assets").join(UNIFONT[0])).unwrap(), + f.objects[4].1 + ); + assert_eq!( + fs::read(target.join("assets/minecraft/lang/en_us.json")).unwrap(), + b"{\"indexed\":true}" + ); + assert_eq!( + fs::read(target.join("data/minecraft/test.json")).unwrap(), + b"data" + ); + assert_eq!(fs::read(target.join("pack.png")).unwrap(), b"icon"); + fs::write(target.join("data/minecraft/test.json"), b"oops").unwrap(); + fs::remove_file(target.join("pack.png")).unwrap(); + } + } + #[test] fn object_paths_come_from_real_hashes() { let store = Path::new("assets"); diff --git a/docs/prepare-performance.md b/docs/prepare-performance.md new file mode 100644 index 000000000..3a3587111 --- /dev/null +++ b/docs/prepare-performance.md @@ -0,0 +1,44 @@ +# Resource preparation and filesystem work + +The reported preparation timings put sounds at 20–29 seconds with little CPU use, versus +0.9–1.4 seconds for fresh client asset extraction. Prefer fewer file opens, reads and writes +over parallelizing decompression. + +Preparation now shares one parsed asset index between panorama, font, pack and language imports. +Its destination verification results live only for that preparation. A subsequent invocation +checks bytes again, so it can repair corrupt resources. Existing valid destination files do not +require the original asset objects to remain installed. A saved asset index also lets preparation +preserve full imports when only cached Minecraft JARs remain. + +New imports are staged beside their destination. On Linux, FICLONE is attempted first; other +filesystems fall back to copying while hashing. Cloned copies are hashed before publication. +macOS attempts clonefile with the same checked streaming fallback. A whole asset group is staged and +verified before any replacement is published; corrupt late panorama faces leave placeholders +alone. Temporary files are cleaned up on errors. This is not a transaction for rename failures +or process termination during publication: a later run repairs incomplete groups. + +Unchanged JAR resources are checked by the ZIP member's size and CRC, avoiding repeated +decompression. Valid indexed replacements take precedence over JAR placeholders. Reusing checked +Minecraft imports also passes the verified artifact/library results directly to the exporters. + +## An immutable store across releases + +The official launcher's SHA-1 object store already provides an immutable source for indexed +assets. Independent filesystem clones let installed Horizon releases share physical blocks +where supported, without sharing writable inodes. Ordinary copies remain the fallback. + +A separate Horizon store could additionally share complete preparations on filesystems without +cloning. Its key would need the pinned client/server hashes, the preparer's output version (or +the legacy source/exporter fingerprint), and the actual asset-index digest: Mojang republishes +translations without changing the Minecraft release. Two preparations with different optional +asset availability must not claim the same complete snapshot. + +Before adopting that store, separate immutable runtime resources from mutable exporter scratch, +classes and logs. Publish snapshots through a temporary directory and atomic rename, give each +release an explicit reference, and define locking, corruption repair, migration and garbage +collection. Exporters must never receive a shared writable directory or hard-linked files. +The existing launcher compatibility check should choose reuse; the store should only supply its +bytes. Keep this separate from the open work on output-version compatibility (#1946/#1947). + +For now, retain independent release directories and clone their resource files. This reduces +copying where the filesystem supports it without introducing a second ownership/cleanup scheme. diff --git a/launcher/Cargo.lock b/launcher/Cargo.lock index 1e8a5aee2..ee0cb882a 100644 --- a/launcher/Cargo.lock +++ b/launcher/Cargo.lock @@ -2726,6 +2726,8 @@ dependencies = [ name = "horizon-prepare" version = "0.1.0" dependencies = [ + "crc32fast", + "libc", "regex", "sha1", "sha2 0.10.9", diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index 7e5c4babb..dadef73d3 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -719,7 +719,7 @@ fn copy_dir(from: &Path, to: &Path) -> std::io::Result<()> { if entry.file_type()?.is_dir() { copy_dir(&entry.path(), &target)?; } else { - fs::copy(entry.path(), target)?; + horizon_prepare::clone_or_copy(&entry.path(), &target)?; } } Ok(()) From c28bea8e96cdf9ec7db298295664626926cb63f1 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:06:16 +0100 Subject: [PATCH 198/238] Mod API: input's permission line says it sees keys and clicks Co-Authored-By: Claude Opus 5.5 --- crates/horizon-mods/src/manifest.rs | 4 +++- docs/api/mods-reference.md | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/horizon-mods/src/manifest.rs b/crates/horizon-mods/src/manifest.rs index e9535bc78..9e998dae1 100644 --- a/crates/horizon-mods/src/manifest.rs +++ b/crates/horizon-mods/src/manifest.rs @@ -133,7 +133,9 @@ impl Capability { Capability::Registries => "add blocks and items to your worlds", Capability::Ui => "show its own screens and a settings page", Capability::Render => "draw on your screen and shake the camera", - Capability::Input => "take over the use button on its own items, and add keys", + Capability::Input => { + "see the keys you hold and your clicks, take over the use button on its own items, and add keys" + } } } /// The capability a component import needs, `Ok(None)` for one every diff --git a/docs/api/mods-reference.md b/docs/api/mods-reference.md index e371f3975..333b65bd0 100644 --- a/docs/api/mods-reference.md +++ b/docs/api/mods-reference.md @@ -14,7 +14,7 @@ The contract between Horizon and its mods: the WIT package `horizon:mods@0.1.0`, | `registries` | data files (`assets/`, `data/`), no interface | add blocks and items to your worlds | | `ui` | [`ui`](#ui) | show its own screens and a settings page | | `render` | [`render`](#render) | draw on your screen and shake the camera | -| `input` | [`input`](#input) | take over the use button on its own items, and add keys | +| `input` | [`input`](#input) | see the keys you hold and your clicks, take over the use button on its own items, and add keys | ## World `horizon-mod` From 6824224a333df0771dcf085566cb898416463be3 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:06:22 +0100 Subject: [PATCH 199/238] Player heads: read the profile's skin once, and no skull on a piston head - Skull keeps what its profile draws (the skin URL, its skins/ sprite and the default skin), computed when the block entity loads, instead of decoding base64 JSON and hashing it for every head every frame; skull_skin_at no longer scans the chunk's saved block entities. - BlockEntity::for_block gave minecraft:piston_head a skull block entity (it ends in _head), which would have been saved as minecraft:skull. - AGENTS.md is left as it is on main: the workflow note is not part of this fix. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 4 -- .../horizon-client/src/block_entity_render.rs | 6 +-- crates/horizon-client/src/containers.rs | 50 +++++++++++++++++-- .../src/game/block_entity_anims.rs | 37 ++++++-------- crates/horizon-client/src/save.rs | 18 ++++--- 5 files changed, 74 insertions(+), 41 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index ed76c08dd..2d883b33b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -27,10 +27,6 @@ Feature → files → harness for the most-changed areas: [docs/architecture.md] change), none for a fix. Agents never add `release-now`: the maintainer decides it. Merges ship in batches of 5, or after 6 hours ([docs/release-flow.md](docs/release-flow.md)). - Contributors first: `gh pr list --state open --search ` before you start; never duplicate theirs. -- Asked for a feature? Run the whole loop: check open PRs for the area (above), work in a new worktree, - `git fetch` the upstream remote (`ludovic111/horizon`) and start from its latest `main`, build, run - `tools/pr_ready.py`, then push to your fork and open the pull request against upstream's `main` — - never against the fork itself. ## Crates diff --git a/crates/horizon-client/src/block_entity_render.rs b/crates/horizon-client/src/block_entity_render.rs index a863d6214..620640188 100644 --- a/crates/horizon-client/src/block_entity_render.rs +++ b/crates/horizon-client/src/block_entity_render.rs @@ -733,7 +733,7 @@ pub struct Scene<'a> { /// The skin sprite a `player_head` shows at a position (`SkullBlockRenderer` /// profile → skin): a downloaded `skins/` slot or the profile's /// `DefaultPlayerSkin` fallback; None keeps `SKIN_BY_TYPE` (steve). - pub skull: &'a dyn Fn([i32; 3]) -> Option, + pub skull: &'a dyn Fn([i32; 3]) -> Option<&'a str>, /// Font glyph cell (none for blank glyphs such as the space) and advance. pub glyph: &'a dyn Fn(char) -> (Option, f32), } @@ -1285,10 +1285,8 @@ impl BlockEntityRenderer { }; // SkullBlockRenderer.getRenderType: the profile's skin when it // resolves, `SKIN_BY_TYPE` (steve) otherwise. - let skin; let texture = if *kind == SkullType::Player { - skin = (scene.skull)(pos); - skin.as_deref().unwrap_or(texture) + (scene.skull)(pos).unwrap_or(texture) } else { texture }; diff --git a/crates/horizon-client/src/containers.rs b/crates/horizon-client/src/containers.rs index 7c9da6862..1f8fad151 100644 --- a/crates/horizon-client/src/containers.rs +++ b/crates/horizon-client/src/containers.rs @@ -82,14 +82,56 @@ pub enum BlockEntity { /// `SkullBlockEntity` of every skull and head block. Skull(Skull), } -/// `SkullBlockEntity`: the player profile the head shows (kept as loaded — -/// `SkullOwner` is a compound, or a string when it names a player only), -/// the note block sound and `custom_name`. +/// `SkullBlockEntity`: the player profile the head shows (kept as loaded: +/// a compound, or a string when it names a player only), the note block +/// sound and `custom_name`. #[derive(Clone, Debug, Default)] pub struct Skull { pub profile: Option, pub note_block_sound: Option, pub custom_name: Option, + /// What `profile` draws, read from it once rather than every frame. + pub skin: Option, +} +impl Skull { + pub fn new( + profile: Option, + note_block_sound: Option, + custom_name: Option, + ) -> Self { + let skin = profile.as_ref().map(SkullSkin::of); + Self { + profile, + note_block_sound, + custom_name, + skin, + } + } +} +/// `SkullBlockRenderer.getPlayerSkin` of a head's profile: its skin's URL +/// with the `skins/` sprite it downloads to, and the +/// `DefaultPlayerSkin` drawn until that sprite is there (steve for a +/// profile with no id). +#[derive(Clone, Debug)] +pub struct SkullSkin { + pub download: Option<(String, String)>, + pub fallback: &'static str, +} +impl SkullSkin { + pub fn of(profile: &horizon_world::nbt::Tag) -> Self { + let download = crate::skins::profile_skin_url_nbt(profile).and_then(|url| { + let location = crate::skins::skin_location(&crate::skins::texture_hash(&url)?); + Some((url, location)) + }); + let fallback = crate::skins::profile_uuid_nbt(profile) + .map_or("entity/player/wide/steve", crate::tab_list::default_skin); + Self { download, fallback } + } +} +/// The skull and head blocks (`SkullBlock`, `WallSkullBlock`): every +/// `*_skull` and `*_head` but the piston's head. +fn is_skull(name: &str) -> bool { + name.ends_with("_skull") || (name.ends_with("_head") && name != "minecraft:piston_head") } /// `CrafterBlockEntity`: nine slots that can be disabled, the powered flag /// and the ticks left in the crafting animation. @@ -244,7 +286,7 @@ impl BlockEntity { if name.ends_with("_banner") { return Some(Self::Banner(Banner::default())); } - if name.ends_with("_head") || name.ends_with("_skull") { + if is_skull(name) { return Some(Self::Skull(Skull::default())); } if name == "minecraft:beacon" { diff --git a/crates/horizon-client/src/game/block_entity_anims.rs b/crates/horizon-client/src/game/block_entity_anims.rs index 136738350..55f9c42ba 100644 --- a/crates/horizon-client/src/game/block_entity_anims.rs +++ b/crates/horizon-client/src/game/block_entity_anims.rs @@ -394,34 +394,25 @@ impl Game<'_> { pub(super) fn chest_lid(&self, pos: [i32; 3], partial: f32) -> f32 { self.be_anims.lid(pos, partial) } - /// `SkullBlockRenderer.getPlayerSkin`: the skin sprite of the `profile` of + /// `SkullBlockRenderer.getPlayerSkin`: the skin sprite of the profile of /// the player head at `pos`. The skin download is started here; while it - /// is in flight (or when it cannot be got) the profile's uuid picks the - /// `DefaultPlayerSkin` for it, and a profile with no id keeps steve. A - /// head with no profile answers None so `SKIN_BY_TYPE` applies. - pub(super) fn skull_skin_at(&self, pos: [i32; 3]) -> Option { - let key = (pos[0], pos[1], pos[2]); - let profile = match self.world.block_entities.get(&key) { - Some(BlockEntity::Skull(s)) => s.profile.clone(), - _ => None, - } - .or_else(|| self.saved_tag(pos).and_then(|t| t.get("profile").cloned())); - let profile = profile?; - if let Some(url) = skins::profile_skin_url_nbt(&profile) - && let Some(location) = - skins::texture_hash(&url).map(|hash| skins::skin_location(&hash)) - { + /// is in flight (or when it cannot be got) the profile's default skin + /// stands in. A head with no profile answers None: `SKIN_BY_TYPE`. + pub(super) fn skull_skin_at(&self, pos: [i32; 3]) -> Option<&str> { + let Some(BlockEntity::Skull(skull)) = + self.world.block_entities.get(&(pos[0], pos[1], pos[2])) + else { + return None; + }; + let skin = skull.skin.as_ref()?; + if let Some((url, location)) = &skin.download { self.skin_downloads .borrow_mut() - .state(skins::Kind::Skin, &url); - if self.gui.atlas.sprites.contains_key(&location) { + .state(skins::Kind::Skin, url); + if self.gui.atlas.sprites.contains_key(location) { return Some(location); } } - Some( - skins::profile_uuid_nbt(&profile) - .map(|uuid| horizon_client::tab_list::default_skin(uuid).to_owned()) - .unwrap_or_else(|| "entity/player/wide/steve".to_owned()), - ) + Some(skin.fallback) } } diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index c59c3c974..964a32cb4 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -637,13 +637,12 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain if id == "minecraft:skull" { return Some(( pos, - BlockEntity::Skull(crate::containers::Skull { - profile: tag.get("profile").cloned(), - note_block_sound: text(tag.get("note_block_sound")), - custom_name: tag - .get("custom_name") + BlockEntity::Skull(crate::containers::Skull::new( + tag.get("profile").cloned(), + text(tag.get("note_block_sound")), + tag.get("custom_name") .and_then(crate::block_entity_nbt::component), - }), + )), )); } // A spawner's block entity id is not its block's name. @@ -3940,6 +3939,13 @@ mod tests { ); let (_, decoded) = decode_block_entity(&plain).expect("plain skull"); assert!(matches!(decoded, crate::containers::BlockEntity::Skull(_))); + // The heads and skulls have one; the piston's head is no skull. + use crate::containers::BlockEntity; + assert!(matches!( + BlockEntity::for_block("minecraft:player_wall_head"), + Some(BlockEntity::Skull(_)) + )); + assert!(BlockEntity::for_block("minecraft:piston_head").is_none()); } #[test] From fbd6bee3e3adc4f2d64b2e5e3520899fdc0f5239 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:03:38 +0100 Subject: [PATCH 200/238] Official mods: FPS Counter and Keystrokes, the first extras as mods The FPS counter and keystrokes extras rewritten as mods on the public API (render, input, ui, storage), in mods/official/, with the extras' options on their settings pages and the extras' look: the same boxes, colours and text positions, pixel for pixel. They are workspace members, so clippy and pr_ready cover them; a maintainer publishes them in horizon-mods. The built-in extras stay until the removal PR, which installs these for the players who had them on. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 14 ++ Cargo.toml | 2 +- docs/mods.md | 16 ++ mods/official/fps-overlay/Cargo.toml | 16 ++ mods/official/fps-overlay/horizon-mod.toml | 9 + mods/official/fps-overlay/src/lib.rs | 131 ++++++++++++++ mods/official/keystrokes/Cargo.toml | 16 ++ mods/official/keystrokes/horizon-mod.toml | 9 + mods/official/keystrokes/src/lib.rs | 192 +++++++++++++++++++++ 9 files changed, 404 insertions(+), 1 deletion(-) create mode 100644 mods/official/fps-overlay/Cargo.toml create mode 100644 mods/official/fps-overlay/horizon-mod.toml create mode 100644 mods/official/fps-overlay/src/lib.rs create mode 100644 mods/official/keystrokes/Cargo.toml create mode 100644 mods/official/keystrokes/horizon-mod.toml create mode 100644 mods/official/keystrokes/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 6945e09cf..ecea2e04e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -993,6 +993,13 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fps-overlay" +version = "1.0.0" +dependencies = [ + "horizon-mod-sdk", +] + [[package]] name = "futures-core" version = "0.3.34" @@ -1812,6 +1819,13 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "keystrokes" +version = "1.0.0" +dependencies = [ + "horizon-mod-sdk", +] + [[package]] name = "lazy_static" version = "1.5.1" diff --git a/Cargo.toml b/Cargo.toml index 4076b9079..57991c185 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] exclude = ["launcher", "crates/horizon-telemetry-server", "crates/horizon-mod-registry", "third_party/wgpu-hal"] -members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/horizon-mods/tests/ui_guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-shaders", "crates/horizon-shader-api", "crates/horizon-release-sign", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"] +members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/horizon-mods/tests/ui_guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-shaders", "crates/horizon-shader-api", "crates/horizon-release-sign", "crates/horizon-mod-sdk/examples/coords-hud", "mods/official/fps-overlay", "mods/official/keystrokes", "xtask"] resolver = "2" [workspace.package] diff --git a/docs/mods.md b/docs/mods.md index d92c88fa6..44cd24122 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -15,6 +15,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | The contract (WIT) | `crates/horizon-mod-sdk/wit/horizon.wit` | 0.1: log, game-read, chat, hud, storage, ui; tick, frame, chat and command hooks | | SDK for mod authors | `crates/horizon-mod-sdk` | 0.1 | | Example mod | `crates/horizon-mod-sdk/examples/coords-hud` | coordinates, facing and time HUD, `/where`, `/coords` | +| Horizon's official mods | `mods/official/` | the extras moving to the registry: FPS Counter (`fps-overlay`), Keystrokes | | Runtime | `crates/horizon-mods` | wasmtime 36 LTS, component model | | Authoring tool | `crates/cargo-horizon-mod` | `cargo horizon-mod new / build / run / check`, and the registry's CI (`registry build / review / list`) | | In the game | `crates/horizon-client/src/game/mods.rs`, Options > Mods | client mods | @@ -406,6 +407,21 @@ An update is the same form with a new version; only the project's `owners` can p has one. A published file never changes. Without the form, open the pull request by hand (the repository's README says how). The formats: [api/mod-index.md](api/mod-index.md). +### Horizon's official mods + +The Horizon extras that are mods now live in `mods/official//`, one crate each in the game's workspace, so +`cargo clippy --workspace` and pr_ready cover them. They use only the public API, like any other mod, with the same +options on their settings page as the extra had and the same look. Each is published by a maintainer: build it on +`main` with the toolchain CI uses (`cd mods/official/ && cargo horizon-mod build`), then open a pull request in +horizon-mods with its `mods/.toml` (owners `ludovic111`, `source` pointing at the folder) and the file's +SHA-256. The reviewer builds it the same way and merges only when the hashes match: the `.wasm` holds no local paths, +so the same rustc and `Cargo.lock` give the same file. + +| Mod | Id | Replaces | Settings (storage key: values) | +|---|---|---|---| +| FPS Counter | `fps_overlay` | Extras > FPS counter | `corner`: `top_left` (default), `top_right`, `bottom_left`, `bottom_right`; `lows`: `false` (default), `true` | +| Keystrokes | `keystrokes` | Extras > Keystrokes | `corner`: as above, default `top_left`; `cps`: `true` (default), `false` | + ## Versions and compatibility - The API is the WIT package `horizon:mods@0.1.0`. New functions and diff --git a/mods/official/fps-overlay/Cargo.toml b/mods/official/fps-overlay/Cargo.toml new file mode 100644 index 000000000..7af17ea0d --- /dev/null +++ b/mods/official/fps-overlay/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "fps-overlay" +version = "1.0.0" +edition.workspace = true +license.workspace = true +publish = false +description = "Horizon's official FPS counter: frames per second in a corner, with the minimum and the 1% low." + +[lib] +crate-type = ["cdylib"] + +[dependencies] +horizon-mod-sdk = { path = "../../../crates/horizon-mod-sdk" } + +[lints] +workspace = true diff --git a/mods/official/fps-overlay/horizon-mod.toml b/mods/official/fps-overlay/horizon-mod.toml new file mode 100644 index 000000000..b18e79f12 --- /dev/null +++ b/mods/official/fps-overlay/horizon-mod.toml @@ -0,0 +1,9 @@ +id = "fps_overlay" +name = "FPS Counter" +version = "1.0.0" +description = "Frames per second in a corner of the screen, with the slowest frame and the 1% low if you like. Horizon's own, formerly an extra." +authors = ["Horizon"] +api = "0.1" +side = "client" +capabilities = ["render", "ui", "storage"] +experimental = ["render", "ui"] diff --git a/mods/official/fps-overlay/src/lib.rs b/mods/official/fps-overlay/src/lib.rs new file mode 100644 index 000000000..897402d02 --- /dev/null +++ b/mods/official/fps-overlay/src/lib.rs @@ -0,0 +1,131 @@ +//! Horizon's official FPS counter, formerly the built-in extra: the game's +//! frame counter (frames in the last second, the slowest frame and the 1% +//! low of the last 5 seconds) in a box in a corner, drawn as the extra drew +//! it. Its settings page has the extra's two options; they are kept in the +//! mod's storage (`corner`, `lows`), where the game writes a player's old +//! extra settings when it installs the mod for them. +use horizon_mod_sdk::{ + Mod, Registration, + render::{self, Corner, FillShape, Point, Rect, Shape, TextShape}, + storage, + ui::{self, ButtonWidget, Widget}, +}; + +/// The extra's box: translucent black, white text with a shadow. +const BACKGROUND: u32 = 0x7800_0000; +const CORNERS: [(Corner, &str, &str); 4] = [ + (Corner::TopLeft, "top_left", "Top left"), + (Corner::TopRight, "top_right", "Top right"), + (Corner::BottomLeft, "bottom_left", "Bottom left"), + (Corner::BottomRight, "bottom_right", "Bottom right"), +]; +/// Settings page buttons. +const CORNER_BUTTON: u32 = 1; +const LOWS_BUTTON: u32 = 2; + +struct Fps { + /// Index in `CORNERS`. + corner: usize, + lows: bool, + /// What the box shows now, to redraw only on a change. + shown: String, +} + +impl Fps { + fn text(&self, f: render::FrameRate) -> String { + if self.lows { + format!("{} fps min {} 1% {}", f.fps, f.min, f.low) + } else { + format!("{} fps", f.fps) + } + } + fn draw(&mut self) { + let text = self.text(render::frame_stats()); + if text == self.shown { + return; + } + let w = render::text_width(&text) + 4.0; + render::set_corner( + CORNERS[self.corner].0, + w, + 12.0, + &[ + Shape::Fill(FillShape { + rect: Rect { + x: 0.0, + y: 0.0, + w, + h: 12.0, + }, + color: BACKGROUND, + }), + Shape::Text(TextShape { + text: text.clone(), + at: Point { x: 2.0, y: 2.0 }, + color: 0xFF_FFFF, + centered: false, + shadow: true, + }), + ], + ); + self.shown = text; + } + /// The settings page: the screen's title is the mod's name already. + fn page(&self) { + let on = |b: bool| if b { "ON" } else { "OFF" }; + ui::set_settings(&[ + Widget::Button(ButtonWidget { + id: CORNER_BUTTON, + label: format!("Corner: {}", CORNERS[self.corner].2), + }), + Widget::Button(ButtonWidget { + id: LOWS_BUTTON, + label: format!("Minimum and 1% low: {}", on(self.lows)), + }), + ]); + } + fn save(&self) { + let _ = storage::set("corner", CORNERS[self.corner].1.as_bytes()); + let _ = storage::set("lows", if self.lows { b"true" } else { b"false" }); + } +} + +fn stored(key: &str) -> Option { + storage::get(key).and_then(|v| String::from_utf8(v).ok()) +} + +impl Mod for Fps { + fn init() -> Result<(Self, Registration), String> { + // The extra's defaults: top left, no lows. + let corner = stored("corner") + .and_then(|c| CORNERS.iter().position(|k| k.1 == c.trim())) + .unwrap_or(0); + let lows = stored("lows").is_some_and(|v| v.trim() == "true"); + let fps = Fps { + corner, + lows, + shown: String::new(), + }; + fps.page(); + Ok((fps, Registration::new().on_tick())) + } + fn tick(&mut self, _tick: u64) { + self.draw(); + } + fn ui_select(&mut self, id: u32, _row: u32) { + match id { + CORNER_BUTTON => self.corner = (self.corner + 1) % CORNERS.len(), + LOWS_BUTTON => self.lows = !self.lows, + _ => return, + } + self.save(); + self.page(); + self.shown.clear(); + self.draw(); + } + fn ui_settings_opened(&mut self) { + self.page(); + } +} + +horizon_mod_sdk::export_mod!(Fps); diff --git a/mods/official/keystrokes/Cargo.toml b/mods/official/keystrokes/Cargo.toml new file mode 100644 index 000000000..36982a12f --- /dev/null +++ b/mods/official/keystrokes/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "keystrokes" +version = "1.0.0" +edition.workspace = true +license.workspace = true +publish = false +description = "Horizon's official keystrokes overlay: movement keys, mouse buttons and clicks per second." + +[lib] +crate-type = ["cdylib"] + +[dependencies] +horizon-mod-sdk = { path = "../../../crates/horizon-mod-sdk" } + +[lints] +workspace = true diff --git a/mods/official/keystrokes/horizon-mod.toml b/mods/official/keystrokes/horizon-mod.toml new file mode 100644 index 000000000..ffda6cd3b --- /dev/null +++ b/mods/official/keystrokes/horizon-mod.toml @@ -0,0 +1,9 @@ +id = "keystrokes" +name = "Keystrokes" +version = "1.0.0" +description = "Your movement keys, mouse buttons and space bar in a corner, lit while held, with clicks per second. Horizon's own, formerly an extra." +authors = ["Horizon"] +api = "0.1" +side = "client" +capabilities = ["render", "input", "ui", "storage"] +experimental = ["render", "input", "ui"] diff --git a/mods/official/keystrokes/src/lib.rs b/mods/official/keystrokes/src/lib.rs new file mode 100644 index 000000000..29866ffbf --- /dev/null +++ b/mods/official/keystrokes/src/lib.rs @@ -0,0 +1,192 @@ +//! Horizon's official keystrokes overlay, formerly the built-in extra: +//! forward, left, back and right as the player bound them, the two mouse +//! buttons (with clicks per second) and the space bar, lit while held, +//! drawn as the extra drew them. It redraws in the frame hook only when +//! something changed. Settings (`corner`, `cps`) live in the mod's storage. +use horizon_mod_sdk::{ + Mod, Registration, input, + render::{self, Corner, FillShape, Rect, Shape, TextShape}, + storage, + ui::{self, ButtonWidget, Widget}, +}; + +const BACKGROUND: u32 = 0x7800_0000; +/// A held key: light fill, dark text without a shadow. +const HELD: u32 = 0xB0FF_FFFF; +const INK_HELD: u32 = 0x14_1414; +const INK: u32 = 0xFF_FFFF; +const KEY: f32 = 22.0; +const GAP: f32 = 2.0; +const CORNERS: [(Corner, &str, &str); 4] = [ + (Corner::TopLeft, "top_left", "Top left"), + (Corner::TopRight, "top_right", "Top right"), + (Corner::BottomLeft, "bottom_left", "Bottom left"), + (Corner::BottomRight, "bottom_right", "Bottom right"), +]; +const CORNER_BUTTON: u32 = 1; +const CPS_BUTTON: u32 = 2; +const MOVES: [&str; 4] = ["key.forward", "key.left", "key.back", "key.right"]; + +/// What the overlay shows; a change redraws it. +#[derive(Clone, PartialEq, Default)] +struct View { + moves: [(bool, String); 4], + jump: bool, + attack: bool, + use_: bool, + cps: Option<(u32, u32)>, +} + +struct Keystrokes { + corner: usize, + cps: bool, + shown: Option, +} + +fn down(name: &str) -> (bool, String) { + input::mapping(name).map_or((false, String::new()), |m| (m.down, m.label)) +} + +/// The extra's key box: a fill and its lines centred, 9 pixels apart. +fn key_box(out: &mut Vec, [x, y, w, h]: [f32; 4], held: bool, lines: &[&str]) { + let (fill, ink) = if held { + (HELD, INK_HELD) + } else { + (BACKGROUND, INK) + }; + out.push(Shape::Fill(FillShape { + rect: Rect { x, y, w, h }, + color: fill, + })); + let total = lines.len() as f32 * 9.0 - 1.0; + for (i, line) in lines.iter().enumerate() { + let tw = render::text_width(line); + out.push(Shape::Text(TextShape { + text: (*line).into(), + at: render::Point { + x: (x + (w - tw) / 2.0).round(), + y: (y + (h - total) / 2.0).round() + i as f32 * 9.0, + }, + color: ink, + centered: false, + shadow: !held, + })); + } +} + +impl Keystrokes { + fn view(&self) -> View { + let (l, r) = input::clicks_per_second(); + View { + moves: MOVES.map(down), + jump: down("key.jump").0, + attack: down("key.attack").0, + use_: down("key.use").0, + cps: self.cps.then_some((l, r)), + } + } + fn draw(&mut self) { + let v = self.view(); + if self.shown.as_ref() == Some(&v) { + return; + } + let (w, h) = (3.0 * KEY + 2.0 * GAP, 3.0 * KEY + 3.0 * GAP + 10.0); + let step = KEY + GAP; + let mut out = Vec::with_capacity(24); + let [forward, left, back, right] = &v.moves; + key_box(&mut out, [step, 0.0, KEY, KEY], forward.0, &[&forward.1]); + for (i, key) in [left, back, right].into_iter().enumerate() { + key_box( + &mut out, + [i as f32 * step, step, KEY, KEY], + key.0, + &[&key.1], + ); + } + let mw = (w - GAP) / 2.0; + let my = 2.0 * step; + for (i, (held, label, cps)) in [ + (v.attack, "LMB", v.cps.map(|c| c.0)), + (v.use_, "RMB", v.cps.map(|c| c.1)), + ] + .into_iter() + .enumerate() + { + let rate = cps.map(|n| format!("{n} CPS")); + let lines: Vec<&str> = std::iter::once(label).chain(rate.as_deref()).collect(); + key_box(&mut out, [i as f32 * (mw + GAP), my, mw, KEY], held, &lines); + } + // The space bar: a line in a short key. + let sy = my + step; + key_box(&mut out, [0.0, sy, w, 10.0], v.jump, &[]); + out.push(Shape::Fill(FillShape { + rect: Rect { + x: w / 2.0 - 12.0, + y: sy + 4.0, + w: 24.0, + h: 2.0, + }, + color: 0xFF00_0000 | if v.jump { INK_HELD } else { INK }, + })); + render::set_corner(CORNERS[self.corner].0, w, h, &out); + self.shown = Some(v); + } + /// The settings page: the screen's title is the mod's name already. + fn page(&self) { + let on = |b: bool| if b { "ON" } else { "OFF" }; + ui::set_settings(&[ + Widget::Button(ButtonWidget { + id: CORNER_BUTTON, + label: format!("Corner: {}", CORNERS[self.corner].2), + }), + Widget::Button(ButtonWidget { + id: CPS_BUTTON, + label: format!("Clicks per second: {}", on(self.cps)), + }), + ]); + } + fn save(&self) { + let _ = storage::set("corner", CORNERS[self.corner].1.as_bytes()); + let _ = storage::set("cps", if self.cps { b"true" } else { b"false" }); + } +} + +fn stored(key: &str) -> Option { + storage::get(key).and_then(|v| String::from_utf8(v).ok()) +} + +impl Mod for Keystrokes { + fn init() -> Result<(Self, Registration), String> { + // The extra's defaults: top left, with clicks per second. + let corner = stored("corner") + .and_then(|c| CORNERS.iter().position(|k| k.1 == c.trim())) + .unwrap_or(0); + let cps = stored("cps").is_none_or(|v| v.trim() != "false"); + let k = Keystrokes { + corner, + cps, + shown: None, + }; + k.page(); + Ok((k, Registration::new().on_frame())) + } + fn frame(&mut self, _seconds: f32) { + self.draw(); + } + fn ui_select(&mut self, id: u32, _row: u32) { + match id { + CORNER_BUTTON => self.corner = (self.corner + 1) % CORNERS.len(), + CPS_BUTTON => self.cps = !self.cps, + _ => return, + } + self.save(); + self.page(); + self.shown = None; + self.draw(); + } + fn ui_settings_opened(&mut self) { + self.page(); + } +} + +horizon_mod_sdk::export_mod!(Keystrokes); From f45e3f1237edbf3fc665ccec11a178f85757d5d7 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 25 Oct 2026 23:11:23 +0000 Subject: [PATCH 201/238] Optimize launcher preparation with precompiled, batched exporters Compile and validate original exporter bundles during release builds, cache development compilation, and reuse initialized JVMs and collision data during preparation. Keep legacy preparers compatible while current releases need only a Java runtime. Co-Authored-By: Codex --- .github/workflows/release.yml | 36 ++- .gitignore | 3 + README.md | 2 +- crates/horizon-prepare/src/exporters.rs | 218 ++++++++++++++++++ crates/horizon-prepare/src/java.rs | 45 ++-- crates/horizon-prepare/src/lib.rs | 85 ++++--- crates/horizon-prepare/src/main.rs | 10 +- crates/horizon-prepare/src/worker.rs | 79 +++++++ .../horizon-prepare/tests/java_selection.rs | 16 +- docs/hosting.md | 2 +- docs/launcher-preparation.md | 53 +++++ launcher/src/backend.rs | 30 ++- launcher/src/backend/java_tests.rs | 2 +- launcher/src/mojang.rs | 4 +- .../dev/horizonlab/parity/PrepareWorker.java | 69 ++++++ .../parity/VanillaCollisionOracle.java | 24 +- packaging/PLAY.txt | 2 +- tests/test_prepare_exporters.py | 37 +++ tools/check_prepare_exporters.py | 145 ++++++++++++ xtask/src/main.rs | 26 ++- 20 files changed, 810 insertions(+), 78 deletions(-) create mode 100644 crates/horizon-prepare/src/exporters.rs create mode 100644 crates/horizon-prepare/src/worker.rs create mode 100644 docs/launcher-preparation.md create mode 100644 mods/horizon/src/test/java/dev/horizonlab/parity/PrepareWorker.java create mode 100644 tests/test_prepare_exporters.py create mode 100644 tools/check_prepare_exporters.py diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4cba851fa..4d20941d4 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -77,9 +77,39 @@ jobs: # 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 - build: + exporters: needs: decide if: needs.decide.outputs.release == 'true' + # This runner holds the maintainer's own pinned Minecraft jars. Only our classes leave it. + runs-on: ${{ vars.MAC_RUNNER || 'ludovics-mac-mini' }} + timeout-minutes: 30 + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ needs.decide.outputs.head }} + persist-credentials: false + clean: true + - uses: dtolnay/rust-toolchain@stable + - run: bash tools/ci/runner-env.sh + - run: bash tools/ci/vanilla-data.sh + - name: Compile and validate original exporters against the pinned jars + shell: bash + run: | + set -euo pipefail + bundle=$(target/release/horizon-prepare compile-exporters --root "$PWD" | tail -1) + mkdir -p exporters + cp -R "$bundle/server" "$bundle/client" "$bundle/manifest.json" exporters/ + python3 tools/check_prepare_exporters.py --binary target/release/horizon-prepare + - uses: actions/upload-artifact@v4 + with: + name: original-exporters + path: exporters/ + if-no-files-found: error + retention-days: 3 + + build: + needs: [decide, exporters] + if: needs.decide.outputs.release == 'true' name: build ${{ matrix.name }} strategy: fail-fast: false @@ -218,6 +248,10 @@ jobs: path: ${{ runner.temp }}/smoke-test.json if-no-files-found: error retention-days: 3 + - uses: actions/download-artifact@v4 + with: + name: original-exporters + path: exporters - name: Package run: | set -euo pipefail diff --git a/.gitignore b/.gitignore index 7fffd6a99..6bdae649a 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,6 @@ /.horizon-cache/ /crash-reports/ /debug/ + +# Release-only compiled original exporter bundle. +/exporters/ diff --git a/README.md b/README.md index ceee29249..65078464e 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Horizon is Minecraft: Java Edition 26.3 rewritten in Rust: a native game that re Horizon is installed through **the Horizon launcher**. Get it from [playhorizon.dev](https://playhorizon.dev) (the Download button picks your system) or from [the launcher releases](https://github.com/ludovic111/horizon/releases?q=launcher-v&expanded=true), for Linux x86-64, macOS on Apple Silicon or Windows x64. The launcher installs and updates the game, prepares its files from your own copy of Minecraft, and updates itself. -**You need** Minecraft: Java Edition 26.3, played once (the official launcher, Prism, MultiMC and other launchers all work); a Java 25 JDK, for the one-time preparation; and a GPU with Vulkan on Linux and Windows, or any Apple Silicon Mac. Step by step, with macOS's first-open prompt and the first launch: [Install and first launch](https://playhorizon.dev/docs/install). +**You need** Minecraft: Java Edition 26.3, played once (the official launcher, Prism, MultiMC and other launchers all work); a Java 25 runtime, for the one-time preparation; and a GPU with Vulkan on Linux and Windows, or any Apple Silicon Mac. Step by step, with macOS's first-open prompt and the first launch: [Install and first launch](https://playhorizon.dev/docs/install). diff --git a/crates/horizon-prepare/src/exporters.rs b/crates/horizon-prepare/src/exporters.rs new file mode 100644 index 000000000..35992c8b5 --- /dev/null +++ b/crates/horizon-prepare/src/exporters.rs @@ -0,0 +1,218 @@ +//! Only original Horizon classes are packaged. The pinned jars remain player supplied. +use crate::*; + +const SETTINGS: &str = "exporters-v1 --release -proc:none -encoding UTF-8"; +const SERVER: &[&str] = &[ + "VanillaHeightmapOracle", + "VanillaBlockLandingOracle", + "VanillaEntityAttributeOracle", + "VanillaPlayerHealthOracle", + "VanillaDamageSourceOracle", + "VanillaCollisionOracle", + "VanillaFaceVisibilityOracle", + "VanillaGameplayFactsExporter", + "VanillaEntityTypesExporter", + "VanillaBlockBehaviourExporter", + "VanillaCommandParseOracle", + "VanillaCreativeTabsExporter", + "VanillaSimpleBlockOracle", + "VanillaStructureBlockExporter", + "PrepareWorker", +]; +const CLIENT: &[&str] = &[ + "VanillaBlockStateBindingOracle", + "VanillaEntityModelExporter", + "VanillaMobDataExporter", + "PrepareWorker", +]; + +fn files(dir: &Path) -> Result, String> { + let mut result = Vec::new(); + for entry in fs::read_dir(dir).map_err(|e| format!("{}: {e}", dir.display()))? { + let path = entry.map_err(|e| e.to_string())?.path(); + if path.is_dir() { + result.extend(files(&path)?); + } else { + result.push(path); + } + } + result.sort(); + Ok(result) +} + +// research-lock pins both jars and every accepted client manifest (which pins its libraries). +// artifacts::verify/client_libraries check those dependencies before any bundle is used. +fn key(root: &Path) -> Result { + let mut hash = sha2::Sha256::new(); + hash.update(SETTINGS); + // The launcher's bundled preparer may differ from the selected game's. Hash that + // version's compiler settings so a newer release still selects a runtime correctly. + let compiler = fs::read_to_string(root.join("crates/horizon-prepare/src/exporters.rs")) + .unwrap_or_else(|_| include_str!("exporters.rs").into()); + hash.update(compiler.replace('\r', "")); + let mut sources = files(&root.join(PARITY))?; + sources.retain(|p| p.extension().is_some_and(|e| e == "java")); + sources.push(root.join("research-lock.json")); + for path in sources { + let data: Vec<_> = read(&path)?.into_iter().filter(|b| *b != b'\r').collect(); + hash.update(format!( + "{}\0{}\0", + relative_display(&path, root)?.replace('\\', "/"), + data.len() + )); + hash.update(data); + } + Ok(hex(&hash.finalize())) +} + +fn outputs(dir: &Path) -> Result { + let mut hashes = Value::Object(Vec::new()); + for path in files(dir)? { + if path.extension().is_some_and(|e| e == "class") { + let name = relative_display(&path, dir)?.replace('\\', "/"); + // javac's sourcepath contains our sources only; enforce that at packaging too. + if !name.starts_with("server/dev/horizonlab/parity/") + && !name.starts_with("client/dev/horizonlab/parity/") + { + return Err(format!("Non-Horizon class in exporter bundle: {name}")); + } + hashes.insert(&name, Value::Str(digest(&path, "sha256")?)); + } + } + Ok(hashes) +} + +fn valid(dir: &Path, key: &str) -> bool { + let Ok(manifest) = read_json(&dir.join("manifest.json")) else { + return false; + }; + manifest.get("key").and_then(Value::as_str) == Some(key) + && outputs(dir).is_ok_and(|hashes| { + !hashes.as_object().unwrap().is_empty() && manifest.get("classes") == Some(&hashes) + }) +} + +/// Whether the version ships a matching, intact bundle and needs only a Java runtime. +pub fn available(root: &Path) -> bool { + key(root).is_ok_and(|key| valid(&root.join("exporters"), &key)) +} + +pub(crate) fn classes(run: &Run) -> Result { + let key = key(&run.root)?; + let shipped = run.root.join("exporters"); + if valid(&shipped, &key) { + return Ok(shipped); + } + let cached = run.local.join("exporters").join(&key); + if valid(&cached, &key) { + return Ok(cached); + } + // Build privately: an interrupted compiler or another preparer cannot publish partial classes. + let destination = cached; + let cached = run + .local + .join("exporters") + .join(format!(".{key}-{}", std::process::id())); + if cached.exists() { + fs::remove_dir_all(&cached).map_err(|e| e.to_string())?; + } + mkdir(&cached)?; + let jdk = java::find(run.java_major, &[run.jdk.home()])?; + for (side, mains, cp) in [ + ("server", SERVER, &run.server_classpath), + ("client", CLIENT, &run.client_classpath), + ] { + (run.log)(&format!("Compiling {side} exporters (shared helpers once)")); + let classes = cached.join(side); + mkdir(&classes)?; + let log = run.log_file(&cached.join(format!("{side}.log")))?; + let mut args = vec![ + "-encoding".into(), + "UTF-8".into(), + "-cp".into(), + cp.clone(), + "-sourcepath".into(), + arg(&run.root.join("mods/horizon/src/test/java")), + "-d".into(), + arg(&classes), + ]; + args.extend(mains.iter().map(|n| arg(&run.oracle(&format!("{n}.java"))))); + jdk.javac(run.java_major, &args, &log)?; + let validate = vec![ + "-Xverify:all".into(), + "-cp".into(), + format!( + "{}{}{}", + classes.display(), + if cfg!(windows) { ";" } else { ":" }, + cp + ), + "dev.horizonlab.parity.PrepareWorker".into(), + "validate".into(), + arg(&classes), + ]; + // No exporter executes here: resolve every class and its signatures against pinned jars. + run.jdk.java(&validate, None, &log)?; + } + let manifest = Value::Object(vec![ + ("key".into(), Value::Str(key.clone())), + ("classes".into(), outputs(&cached)?), + ]); + write_json(&cached.join("manifest.json"), &manifest)?; + if valid(&destination, &key) { + fs::remove_dir_all(&cached).map_err(|e| e.to_string())?; + } else { + if destination.exists() { + fs::remove_dir_all(&destination).map_err(|e| e.to_string())?; + } + fs::rename(&cached, &destination).map_err(|e| e.to_string())?; + } + Ok(destination) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn bundles_invalidate_sources_pins_and_damaged_classes() { + let root = std::env::temp_dir().join(format!("prepare-bundle-{}", std::process::id())); + let source = root.join(PARITY).join("VanillaHeightmapOracle.java"); + mkdir(source.parent().unwrap()).unwrap(); + write(&source, b"original\n").unwrap(); + let compiler = root.join("crates/horizon-prepare/src/exporters.rs"); + mkdir(compiler.parent().unwrap()).unwrap(); + write(&compiler, b"--release 25 -proc:none").unwrap(); + write(&root.join("research-lock.json"), b"pins").unwrap(); + let original = key(&root).unwrap(); + write(&compiler, b"--release 26 -proc:none").unwrap(); + assert_ne!(key(&root).unwrap(), original); + write(&compiler, b"--release 25 -proc:none").unwrap(); + write(&source, b"original\r\n").unwrap(); + assert_eq!(key(&root).unwrap(), original); + write(&source, b"changed").unwrap(); + assert_ne!(key(&root).unwrap(), original); + write(&source, b"original\n").unwrap(); + write(&root.join("research-lock.json"), b"new dependencies").unwrap(); + assert_ne!(key(&root).unwrap(), original); + write(&root.join("research-lock.json"), b"pins").unwrap(); + let bundle = root.join("exporters"); + let class = bundle.join("server/dev/horizonlab/parity/Helper.class"); + mkdir(class.parent().unwrap()).unwrap(); + write(&class, b"compiled").unwrap(); + write_json( + &bundle.join("manifest.json"), + &Value::Object(vec![ + ("key".into(), Value::Str(original)), + ("classes".into(), outputs(&bundle).unwrap()), + ]), + ) + .unwrap(); + assert!(available(&root)); + write(&class, b"damaged").unwrap(); + assert!(!available(&root)); + fs::remove_file(&class).unwrap(); + assert!(!available(&root)); + fs::remove_dir_all(root).unwrap(); + } +} diff --git a/crates/horizon-prepare/src/java.rs b/crates/horizon-prepare/src/java.rs index d46cb9428..6343d8433 100644 --- a/crates/horizon-prepare/src/java.rs +++ b/crates/horizon-prepare/src/java.rs @@ -1,4 +1,4 @@ -//! The JDK that compiles and runs the vanilla exporters. They call Mojang's own code, so they +//! Java discovery and execution for the vanilla exporters. They call Mojang's own code, so they //! stay Java; everything around them is here. use std::ffi::OsStr; @@ -15,7 +15,7 @@ pub struct Jdk { const TIMEOUT: Duration = Duration::from_secs(3600); /// Exporters and JDK probes write to pipes or log files: on Windows they open no terminal. -fn command(program: impl AsRef) -> Command { +pub(crate) fn command(program: impl AsRef) -> Command { let command = Command::new(program); #[cfg(windows)] let command = { @@ -56,9 +56,9 @@ fn without_verbatim(path: PathBuf) -> PathBuf { /// Resolve compiler symlinks before picking its sibling runtime. The first `java` on the /// PATH may be an older, runtime-only install; it must not be paired with a newer `javac`. -fn path_jdks(path: &OsStr) -> Vec { +fn path_homes(path: &OsStr, tool: &str) -> Vec { std::env::split_paths(path) - .map(|dir| executable(&dir, "javac")) + .map(|dir| executable(&dir, tool)) .filter(|javac| javac.is_file()) .map(real_path) .filter_map(|javac| { @@ -162,7 +162,7 @@ fn jdk_parents() -> Vec { /// The JDKs (folders with `bin/javac`) one level below `parents`; a macOS bundle's home is /// its `Contents/Home`. Those named for `major` come first, then the others newest name first. -fn installed_jdks(parents: &[PathBuf], major: i64) -> Vec { +fn installed_jdks(parents: &[PathBuf], major: i64, tool: &str) -> Vec { let mut homes: Vec = parents .iter() .filter_map(|parent| fs::read_dir(parent).ok()) @@ -176,7 +176,7 @@ fn installed_jdks(parents: &[PathBuf], major: i64) -> Vec { entry.path() } }) - .filter(|home| executable(&home.join("bin"), "javac").is_file()) + .filter(|home| executable(&home.join("bin"), tool).is_file()) .collect(); let named = |home: &Path| { home.iter().any(|part| { @@ -191,8 +191,7 @@ fn installed_jdks(parents: &[PathBuf], major: i64) -> Vec { } /// The Java runtimes the official launcher downloaded into `runtime` (a game directory's -/// `runtime` folder: `///`, a macOS one in `jre.bundle`). Mojang's -/// runtimes are whole JDKs, javac included, so a player who has run Minecraft has one already. +/// `runtime` folder: `///`, a macOS one in `jre.bundle`). pub fn minecraft_runtimes(runtime: &Path) -> Vec { let children = |dir: &Path| -> Vec { let mut paths: Vec = fs::read_dir(dir) @@ -206,7 +205,7 @@ pub fn minecraft_runtimes(runtime: &Path) -> Vec { for platform in children(&component) { let home = platform.join(component.file_name().unwrap_or_default()); for home in [home.join("jre.bundle/Contents/Home"), home] { - if executable(&home.join("bin"), "javac").is_file() { + if executable(&home.join("bin"), "java").is_file() { homes.push(home); break; } @@ -221,8 +220,18 @@ pub fn minecraft_runtimes(runtime: &Path) -> Vec { /// `/usr/libexec/java_home -v ` reports, else the homes in `more` (the launcher's pick, /// Minecraft's own runtimes), else the folders installers use (`jdk_parents`). pub fn find(major: i64, more: &[PathBuf]) -> Result { + find_tools(major, more, true) +} + +/// Finds a compatible runtime; precompiled exporters do not need javac. +pub fn find_runtime(major: i64, more: &[PathBuf]) -> Result { + find_tools(major, more, false) +} + +fn find_tools(major: i64, more: &[PathBuf], compiler: bool) -> Result { + let tool = if compiler { "javac" } else { "java" }; let need = format!( - "Java {major} is needed (a JDK with javac, e.g. Eclipse Temurin {major}). \ + "Java {major} is needed ({tool}, e.g. Eclipse Temurin {major}). \ Install it, or set JAVA_HOME to it, and try again." ); let mut candidates = Vec::new(); @@ -235,7 +244,7 @@ pub fn find(major: i64, more: &[PathBuf]) -> Result { )); } if let Some(path) = std::env::var_os("PATH") { - for home in path_jdks(&path) { + for home in path_homes(&path, tool) { let bin = home.join("bin"); candidates.push((executable(&bin, "javac"), executable(&bin, "java"), "PATH")); } @@ -265,7 +274,7 @@ pub fn find(major: i64, more: &[PathBuf]) -> Result { "Minecraft's Java", )); } - for home in installed_jdks(&jdk_parents(), major) { + for home in installed_jdks(&jdk_parents(), major, tool) { let bin = home.join("bin"); candidates.push(( executable(&bin, "javac"), @@ -275,15 +284,16 @@ pub fn find(major: i64, more: &[PathBuf]) -> Result { } let mut problems = Vec::new(); for (javac, java, from) in candidates { - if !javac.is_file() || !java.is_file() { + if (compiler && !javac.is_file()) || !java.is_file() { problems.push(format!( - "{from} ({}) has no javac and java", + "{from} ({}) has no compatible Java tools", javac.parent().unwrap().display() )); continue; } let checked = [(&javac, "javac"), (&java, "java")] .into_iter() + .filter(|(_, name)| compiler || *name == "java") .try_for_each(|(program, name)| { let found = tool_major(program, name)?; if i64::from(found) >= major { @@ -298,7 +308,7 @@ pub fn find(major: i64, more: &[PathBuf]) -> Result { } } if problems.is_empty() { - Err(format!("{need} No javac was found.")) + Err(format!("{need} No {tool} was found.")) } else { Err(format!("{need} Found: {}.", problems.join("; "))) } @@ -317,7 +327,7 @@ pub fn classpath<'a>(paths: impl IntoIterator) -> String { impl Jdk { /// The JDK's home, the folder above `bin` (what `JAVA_HOME` names). pub fn home(&self) -> PathBuf { - self.javac + self.java .parent() .and_then(Path::parent) .map(Path::to_path_buf) @@ -424,6 +434,7 @@ mod tests { dir.join("missing"), ], 25, + "javac", ); assert_eq!( found, @@ -445,7 +456,7 @@ mod tests { let make = |home: PathBuf, javac: bool| { fs::create_dir_all(home.join("bin")).unwrap(); if javac { - fs::write(executable(&home.join("bin"), "javac"), b"").unwrap(); + fs::write(executable(&home.join("bin"), "java"), b"").unwrap(); } home }; diff --git a/crates/horizon-prepare/src/lib.rs b/crates/horizon-prepare/src/lib.rs index c7e443c7d..09787e476 100644 --- a/crates/horizon-prepare/src/lib.rs +++ b/crates/horizon-prepare/src/lib.rs @@ -11,11 +11,13 @@ //! run, keyed by the sha256 of their inputs and outputs recorded in the manifest, as before. pub mod artifacts; +pub mod exporters; mod inventory; pub mod java; pub mod pyjson; mod sounds; mod survival; +mod worker; pub use sounds::{fonts, languages, packs, panorama, sounds, BUILT_IN_PACKS}; @@ -65,7 +67,7 @@ pub fn sources_sha256(crate_dir: &Path) -> Option { pub struct Options { /// The version folder (a release folder, or a checkout): research-lock.json, the exporters' - /// Java sources, and `.local/` where everything is written. + /// Java sources, optional precompiled `exporters/`, and `.local/` where everything is written. pub root: PathBuf, /// Import the pinned game files from the player's own copy first (`--prepare`). pub import: bool, @@ -74,7 +76,7 @@ pub struct Options { pub minecraft_dir: Option, /// The pinned server jar the player downloaded (`--server-jar`, else HORIZON_SERVER_JAR). pub server_jar: Option, - /// The JDK to try after JAVA_HOME and the PATH (the launcher's pick, e.g. Minecraft's + /// The Java runtime to try after JAVA_HOME and the PATH (the launcher's pick, e.g. Minecraft's /// own Java it downloaded from Mojang). pub java_home: Option, } @@ -293,6 +295,8 @@ struct Run<'a> { server_classpath: String, /// The client jar and its libraries: the classpath of the client-side exporters. client_classpath: String, + bundles: std::cell::OnceCell, + workers: std::cell::RefCell<[Option; 2]>, previous: Value, origin: Value, log: Log<'a>, @@ -301,6 +305,13 @@ struct Run<'a> { const PARITY: &str = "mods/horizon/src/test/java/dev/horizonlab/parity"; impl Run<'_> { + fn bundle(&self) -> Result<&Path, String> { + if self.bundles.get().is_none() { + self.bundles.set(exporters::classes(self)?).unwrap(); + } + Ok(self.bundles.get().unwrap()) + } + fn oracle(&self, name: &str) -> PathBuf { self.root.join(PARITY).join(name) } @@ -333,33 +344,31 @@ impl Run<'_> { fs::File::create(path).map_err(|e| format!("{}: {e}", path.display())) } - /// `javac --release -proc:none -cp [extra] -d `. - fn compile( - &self, - classpath: &str, - extra: &[String], - classes: &Path, - sources: &[&Path], - log: &fs::File, - ) -> Result<(), String> { - let mut args = vec!["-cp".to_string(), classpath.to_string()]; - args.extend_from_slice(extra); - args.push("-d".into()); - args.push(classes.display().to_string()); - args.extend(sources.iter().map(|s| s.display().to_string())); - self.jdk.javac(self.java_major, &args, log) - } - /// `java -Xmx -cp : dev.horizonlab.parity.
    `. fn run( &self, heap: &str, - classes: &Path, classpath: &str, main: &str, args: &[String], log: &fs::File, + log_path: &Path, ) -> Result<(), String> { + let side = usize::from(classpath == self.client_classpath); + let classes = self + .bundle()? + .join(if side == 1 { "client" } else { "server" }); + if !matches!( + main, + "VanillaCommandParseOracle" | "VanillaCreativeTabsExporter" + ) && std::env::var_os("HORIZON_PREPARE_ISOLATED").is_none() + { + let mut workers = self.workers.borrow_mut(); + if workers[side].is_none() { + workers[side] = Some(worker::Worker::new(self, &classes, classpath)?); + } + return workers[side].as_mut().unwrap().run(main, args, log_path); + } let separator = if cfg!(windows) { ";" } else { ":" }; let mut all = vec![ format!("-Xmx{heap}"), @@ -371,7 +380,7 @@ impl Run<'_> { self.jdk.java(&all, None, log) } - /// Compiles `sources` into `classes` and runs each of `mains` with `args`, their output in + /// Runs each exporter from the verified shared bundle, with output in /// the log at `log`: the shape of every exporter step. #[allow(clippy::too_many_arguments)] fn export( @@ -379,7 +388,7 @@ impl Run<'_> { what: &str, classpath: &str, classes: &Path, - sources: &[&Path], + _sources: &[&Path], mains: &[&str], heap: &str, args: &[String], @@ -389,10 +398,8 @@ impl Run<'_> { mkdir(classes)?; let file = self.log_file(log)?; let fail = |e: String| format!("{e} while exporting {what}; see {}", log.display()); - self.compile(classpath, &[], classes, sources, &file) - .map_err(fail)?; for main in mains { - self.run(heap, classes, classpath, main, args, &file) + self.run(heap, classpath, main, args, &file, log) .map_err(fail)?; } Ok(()) @@ -401,6 +408,15 @@ impl Run<'_> { /// Prepares the resources; returns their folder (`.local/horizon-client/`). pub fn prepare(options: &Options, log: Log) -> Result { + prepare_impl(options, log, false) +} + +/// Release build tool: compile and link-check only Horizon's original exporter classes. +pub fn compile_exporters(options: &Options, log: Log) -> Result { + prepare_impl(options, log, true) +} + +fn prepare_impl(options: &Options, log: Log, compile_only: bool) -> Result { let root = &std::path::absolute(&options.root) .map_err(|e| format!("{}: {e}", options.root.display()))?; let lock = artifacts::Lock::read(root)?; @@ -409,7 +425,7 @@ pub fn prepare(options: &Options, log: Log) -> Result { homes.extend(java::minecraft_runtimes( &artifacts::minecraft_dir(options.minecraft_dir.as_deref()).join("runtime"), )); - let jdk = java::find(lock.java_major, &homes)?; + let jdk = java::find_runtime(lock.java_major, &homes)?; if options.import && !artifacts::reuse_imported(root, log)? { artifacts::import( root, @@ -456,10 +472,15 @@ pub fn prepare(options: &Options, log: Log) -> Result { client: found.jars["client"].clone(), server_classpath: classpath_join(&found.jars["server"], &found.libraries), client_classpath: classpath_join(&found.jars["client"], &client_libraries), + bundles: std::cell::OnceCell::new(), + workers: std::cell::RefCell::new([None, None]), previous, origin, log, }; + if compile_only { + return Ok(run.bundle()?.to_path_buf()); + } steps(&run, &lock, &oracle, options.minecraft_dir.as_deref())?; log(&format!( "Prepared Horizon native-client resources: {}", @@ -1117,7 +1138,6 @@ fn prepare_worldgen(run: &Run) -> Result<(), String> { /// whose blocks are checked for survival or support (trees, flowers, grass, kelp, ...). fn export_block_behavior(run: &Run, export: &Path) -> Result<(), String> { (run.log)("Exporting block behavior (VanillaSimpleBlockOracle)"); - let tests = run.root.join("mods/horizon/src/test/java"); let classes = export.join("classes"); mkdir(&classes)?; let log_path = export.join("export.log"); @@ -1128,22 +1148,13 @@ fn export_block_behavior(run: &Run, export: &Path) -> Result<(), String> { log_path.display() ) }; - // The oracle's helper classes are compiled on demand from the source path. - run.compile( - &run.server_classpath, - &["-sourcepath".into(), arg(&tests)], - &classes, - &[&tests.join("dev/horizonlab/parity/VanillaSimpleBlockOracle.java")], - &log, - ) - .map_err(fail)?; run.run( "1G", - &classes, &run.server_classpath, "VanillaSimpleBlockOracle", &["export".into(), arg(export)], &log, + &log_path, ) .map_err(fail)?; let rules = survival::bind_definitions( diff --git a/crates/horizon-prepare/src/main.rs b/crates/horizon-prepare/src/main.rs index db64ca19d..10e153a04 100644 --- a/crates/horizon-prepare/src/main.rs +++ b/crates/horizon-prepare/src/main.rs @@ -16,6 +16,7 @@ const HELP: &str = "\ horizon-prepare: prepare Horizon's resources from your own Minecraft Java Edition Usage: horizon-prepare [--prepare] [--server-jar PATH] [--minecraft-dir PATH] [--root PATH] + horizon-prepare compile-exporters [--root PATH] horizon-prepare sounds [--minecraft-dir PATH] [--root PATH] horizon-prepare panorama [--minecraft-dir PATH] [--root PATH] horizon-prepare fonts [--minecraft-dir PATH] [--root PATH] @@ -30,8 +31,9 @@ Usage: horizon-prepare [--prepare] [--server-jar PATH] [--minecraft-dir PATH] [- --root PATH The Horizon folder to prepare (default: the folder this program is in, else the nearest folder above the current one with research-lock.json) -Needs Minecraft played once from the official launcher, and Java (a JDK) of the version -research-lock.json pins: JAVA_HOME, else javac and java on the PATH. +Needs Minecraft played once from the official launcher, and Java of the version +research-lock.json pins. Releases use a runtime; development or changed exporters need a JDK. +compile-exporters builds and validates a release bundle from the already imported pinned jars. The resources go to /.local/horizon-client/; pass that to horizon-client --resources. horizon-prepare panorama repairs the classic menu background in existing resources without Java. @@ -73,7 +75,7 @@ fn main() -> ExitCode { let assets = args.next_if(|a| { matches!( a.to_str(), - Some("sounds" | "panorama" | "fonts" | "packs" | "languages") + Some("sounds" | "panorama" | "fonts" | "packs" | "languages" | "compile-exporters") ) }); while let Some(arg) = args.next() { @@ -132,6 +134,8 @@ fn main() -> ExitCode { let _ = out.flush(); }; let result = match assets.as_deref().and_then(|a| a.to_str()) { + Some("compile-exporters") => horizon_prepare::compile_exporters(&options, &log) + .inspect(|dir| log(&dir.display().to_string())), Some("sounds") => { horizon_prepare::sounds(&options.root, options.minecraft_dir.as_deref(), &log) } diff --git a/crates/horizon-prepare/src/worker.rs b/crates/horizon-prepare/src/worker.rs new file mode 100644 index 000000000..564b11746 --- /dev/null +++ b/crates/horizon-prepare/src/worker.rs @@ -0,0 +1,79 @@ +//! A bounded request/reply channel to one initialized exporter JVM. +use crate::*; +use std::io::{BufRead, BufReader, Write}; +use std::process::{Child, ChildStdin, Stdio}; +use std::sync::mpsc::{self, Receiver}; + +pub(crate) struct Worker { + child: Child, + input: ChildStdin, + replies: Receiver>, +} + +impl Worker { + pub(crate) fn new(run: &Run, classes: &Path, cp: &str) -> Result { + let log = run.log_file(&run.local.join(format!( + "{}-worker.log", + classes.file_name().unwrap().to_string_lossy() + )))?; + let mut child = java::command(&run.jdk.java) + .args([ + "-Xmx2G", + "-cp", + &format!( + "{}{}{}", + classes.display(), + if cfg!(windows) { ";" } else { ":" }, + cp + ), + "dev.horizonlab.parity.PrepareWorker", + ]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(log) + .spawn() + .map_err(|e| e.to_string())?; + let input = child.stdin.take().unwrap(); + let output = child.stdout.take().unwrap(); + let (send, replies) = mpsc::channel(); + std::thread::spawn(move || { + for line in BufReader::new(output).lines() { + if send.send(line).is_err() { + break; + } + } + }); + Ok(Self { + child, + input, + replies, + }) + } + + pub(crate) fn run(&mut self, main: &str, args: &[String], log: &Path) -> Result<(), String> { + let task = Value::Array(vec![ + Value::Str(arg(log)), + Value::Str(main.into()), + Value::Array(args.iter().cloned().map(Value::Str).collect()), + ]); + writeln!(self.input, "{}", pyjson::dumps(&task, &pyjson::COMPACT)) + .map_err(|e| e.to_string())?; + self.input.flush().map_err(|e| e.to_string())?; + let reply = self + .replies + .recv_timeout(std::time::Duration::from_secs(3600)) + .map_err(|e| format!("Exporter JVM stopped or timed out: {e}"))? + .map_err(|e| e.to_string())?; + match pyjson::loads(&reply)?.as_str() { + Some("") => Ok(()), + _ => Err(format!("Exporter {main} failed: {reply}")), + } + } +} + +impl Drop for Worker { + fn drop(&mut self) { + let _ = self.child.kill(); + let _ = self.child.wait(); + } +} diff --git a/crates/horizon-prepare/tests/java_selection.rs b/crates/horizon-prepare/tests/java_selection.rs index ba15304e6..4e8d60f55 100644 --- a/crates/horizon-prepare/tests/java_selection.rs +++ b/crates/horizon-prepare/tests/java_selection.rs @@ -25,13 +25,23 @@ fn selects_a_compatible_compiler_and_runtime() { assert!(error.contains(&format!("java {runtime} (")), "{error}"); return; } - let jdk = java::find(25, &[]).unwrap(); - let expected = root.join(if scenario == "target-release" { + let jdk = if scenario == "runtime-only" { + java::find_runtime(25, &[]) + } else { + java::find(25, &[]) + } + .unwrap(); + let expected = root.join(if scenario == "runtime-only" { + "jre25" + } else if scenario == "target-release" { "newer" } else { "jdk25" }); for (actual, name) in [(&jdk.javac, "javac"), (&jdk.java, "java")] { + if scenario == "runtime-only" && name == "javac" { + continue; + } assert_eq!( fs::canonicalize(actual).unwrap(), fs::canonicalize(tool(&expected.join("bin"), name)).unwrap() @@ -64,6 +74,7 @@ fn selects_a_compatible_compiler_and_runtime() { ); for (home, compiler, runtime) in [ ("jre21", None, "21.0.1"), + ("jre25", None, "25.0.1"), ("jdk21", Some("21.0.1"), "21.0.1"), ("jdk25", Some("25.0.1"), "25.0.1"), ("mixed", Some("25.0.1"), "21.0.1"), @@ -83,6 +94,7 @@ fn selects_a_compatible_compiler_and_runtime() { } fs::write(root.join("failing/bin/java.exit"), "7").unwrap(); let scenarios = vec![ + ("runtime-only", Some("jre25"), vec!["jre21", "jre25"]), ("older-runtime", None, vec!["jre21", "jdk25"]), ("older-jdk", None, vec!["jdk21", "jdk25"]), ("incompatible-home", Some("mixed"), vec!["jdk25"]), diff --git a/docs/hosting.md b/docs/hosting.md index bf431cada..778dcbaab 100644 --- a/docs/hosting.md +++ b/docs/hosting.md @@ -71,7 +71,7 @@ from there too. ## 2. Prepare the game files Horizon reads Minecraft's data from your own copy of the game. You need Minecraft Java Edition 26.3, played -once from the official launcher on this computer, Java 25 (a JDK), and the 26.3 `server.jar`: it's the +once from the official launcher on this computer, a Java 25 runtime, and the 26.3 `server.jar`: it's the `downloads.server.url` in your launcher's `versions/26.3/26.3.json`. Then: ```sh diff --git a/docs/launcher-preparation.md b/docs/launcher-preparation.md new file mode 100644 index 000000000..7df5b9131 --- /dev/null +++ b/docs/launcher-preparation.md @@ -0,0 +1,53 @@ +# Launcher resource preparation + +Release folders include `exporters/server`, `exporters/client` and an integrity manifest. +These contain only Horizon's original Java exporter classes. Minecraft jars, libraries, +assets and generated resources remain local and are verified against `research-lock.json`. +The same platform-independent class bundle is used in game and dedicated-server releases. + +The release runner compiles the server and client source sets separately with +`javac --release -proc:none -encoding UTF-8`, resolving shared helpers once +per classpath. It checks every class and its signatures with `-Xverify:all` against the +pinned jars. The release job also compares two fresh isolated preparations with a fresh +batched preparation using a runtime-only Java home: + +```sh +cargo build --release -p horizon-prepare +# Requires already imported Minecraft files in .local/minecraft/. +target/release/horizon-prepare compile-exporters +# Copy server/, client/ and manifest.json from the printed folder into exporters/. +python3 tools/check_prepare_exporters.py --binary target/release/horizon-prepare +``` + +Development checkouts and changed sources compile on demand, into +`.local/exporters/`. The key covers Java sources (including implicit helpers), +pinned dependency checksums and compiler settings. Class checksums detect missing or damaged +outputs. Compilation happens in a private staging folder before publishing a complete cache +entry. Sources and paths are normalized so one release bundle works on all three platforms. + +Compatible exporters run sequentially through one server JVM and one client JVM. Both keep +Minecraft's initialized built-in registries. Every task restores the original registry tags +before replying; failures stop preparation. Collision exports share the first generated +registry and collision files, encoded block states and shape grids within the server worker. +Ordinary parity oracle processes keep their existing behavior. + +Command-registry and creative-tab exporters remain separate processes: they install data +components as well as tags. The data generator also remains separate. Inspection of pinned +`net.minecraft.data.Main.main` showed that it calls `Util.shutdownExecutors` after running +its providers. Calling it inside a worker would shut down shared executors; incorporating +its providers directly needs a separate comparison of registry and executor lifetimes. +Fresh release preparation therefore starts five exporter/data-generator JVMs, down from 18, +and invokes no compiler. `HORIZON_PREPARE_ISOLATED=1` retains separate exporter JVMs for +output comparisons and diagnosis. + +`tools/check_prepare_exporters.py` requires identical resource file sets and identical bytes +for every file stable across the two isolated runs. For the three known noisy JSON files, +it also requires semantic equality after normalizing object key order, opaque JVM lambda +addresses and creative-tab tag set order. Other variation fails the release check. + +On Linux with Java 25, two comparisons produced 23,204 matching resource files each. +The first isolated runs took 178.95 s and 104.25 s, versus 45.05 s batched; the second took +100.03 s and 176.06 s, versus 120.09 s batched. All used precompiled exporters. These are fresh output directories with a warm OS file cache, on a +machine with other workloads; the spread between isolated runs prevents treating this as a +controlled speedup estimate. The compiler saving is separate: release preparation removes +the previously measured 15 compiler invocations (29–52 s). diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index 7e5c4babb..dfe264300 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -1096,17 +1096,23 @@ fn download_for_owner( } } -/// The home of a JDK whose compiler and runtime support `major`. Forward even an installed -/// JDK to external preparers: older versions may otherwise rediscover incompatible PATH tools. +/// The Java home for this version: a runtime for precompiled exporters, a JDK for older +/// preparers or changed sources. Forward the selection to external preparers too. /// Else try the official launcher's Java in `games`, then download the runtime named by /// `manifest` into Horizon's game folder (Mojang's runtimes are whole JDKs). fn java( major: i64, + compiler: bool, games: &[PathBuf], manifest: &Path, log: &dyn Fn(String), ) -> Result { - if let Ok(jdk) = horizon_prepare::java::find(major, &[]) { + let find = if compiler { + horizon_prepare::java::find + } else { + horizon_prepare::java::find_runtime + }; + if let Ok(jdk) = find(major, &[]) { return Ok(jdk.home()); } let mut runtimes: Vec = Vec::new(); @@ -1118,7 +1124,7 @@ fn java( runtimes.push(home); } } - let missing = match horizon_prepare::java::find(major, &runtimes) { + let missing = match find(major, &runtimes) { Ok(jdk) => return Ok(jdk.home()), Err(e) => e, }; @@ -1129,7 +1135,7 @@ fn java( // musl distributions) it cannot start, so don't download it. if cfg!(all(target_os = "linux", target_arch = "x86_64")) && !Path::new(GLIBC_LOADER).exists() { return Err(format!( - "{missing}\n • Minecraft's own Java from Mojang needs glibc ({GLIBC_LOADER} is missing; on NixOS, enable programs.nix-ld), so install a JDK instead." + "{missing}\n • Minecraft's own Java from Mojang needs glibc ({GLIBC_LOADER} is missing; on NixOS, enable programs.nix-ld), so install compatible Java instead." )); } let fetch = |url: &str| -> Result, String> { @@ -1144,13 +1150,13 @@ fn java( let home = mojang::java_runtime(&component, &runtime, 8, &fetch, log).map_err(|e| { format!("{missing}\n • Horizon also tried to download Minecraft's own Java from Mojang, but: {e}") })?; - horizon_prepare::java::find(major, &[home]).map(|jdk| jdk.home()) + find(major, &[home]).map(|jdk| jdk.home()) } /// The dynamic loader glibc programs on x86-64 Linux start through. const GLIBC_LOADER: &str = "/lib64/ld-linux-x86-64.so.2"; -/// Runs `cmd` with the JDK at `home`: JAVA_HOME names it and its `bin` leads the PATH, as the +/// Runs `cmd` with Java at `home`: JAVA_HOME names it and its `bin` leads the PATH, as the /// versions' own preparers look for Java. fn with_java(cmd: &mut Command, home: &Path) { let mut path = vec![home.join("bin")]; @@ -1253,7 +1259,7 @@ pub fn preparer( "This version prepares with Python: Python 3.10 or newer is needed. Install it, or pick a newer version.", )?), }; - // The JDK the version pins, found as horizon-prepare finds it, so a missing one is + // The Java the version pins, found as horizon-prepare finds it, so a missing one is // reported (or downloaded) before the server jar is downloaded. Versions from before // java_major ran Java 25. let major = read_json(&dir.join("research-lock.json")) @@ -1267,7 +1273,13 @@ pub fn preparer( .into_iter() .map(|(_, dir)| dir), ); - let java_home = java(major, &games, &manifest, log)?; + let java_home = java( + major, + !horizon_prepare::exporters::available(&dir), + &games, + &manifest, + log, + )?; let jar = data_dir() .join("server-jars") .join(format!("{version}.jar")); diff --git a/launcher/src/backend/java_tests.rs b/launcher/src/backend/java_tests.rs index 75914a1b0..3f46123b8 100644 --- a/launcher/src/backend/java_tests.rs +++ b/launcher/src/backend/java_tests.rs @@ -5,7 +5,7 @@ fn selected_jdk_reaches_external_preparers() { const CHILD: &str = "HORIZON_LAUNCHER_JAVA_TEST"; if let Some(root) = std::env::var_os(CHILD) { let root = PathBuf::from(root); - let home = java(25, &[], &root.join("missing.json"), &|_| {}).unwrap(); + let home = java(25, true, &[], &root.join("missing.json"), &|_| {}).unwrap(); assert_eq!( fs::canonicalize(&home).unwrap(), fs::canonicalize(root.join("jdk25")).unwrap() diff --git a/launcher/src/mojang.rs b/launcher/src/mojang.rs index 27f9c3f0b..f242672b9 100644 --- a/launcher/src/mojang.rs +++ b/launcher/src/mojang.rs @@ -103,8 +103,8 @@ fn java_platform() -> Option<&'static str> { /// Downloads Mojang's Java runtime `component` (the version JSON's `javaVersion.component`, e.g. /// `java-runtime-epsilon`) into the game directory's `runtime` folder, as the official launcher /// lays it out: `///`. Each file is checked against its sha1; -/// files already there and whole are kept. Mojang's runtimes are whole JDKs, javac included, -/// which is what the exporters need. +/// files already there and whole are kept. These also include javac for older release preparers; +/// current releases can use an already installed runtime without it. pub fn java_runtime( component: &str, runtime: &Path, diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/PrepareWorker.java b/mods/horizon/src/test/java/dev/horizonlab/parity/PrepareWorker.java new file mode 100644 index 000000000..26104bbe1 --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/PrepareWorker.java @@ -0,0 +1,69 @@ +package dev.horizonlab.parity; + +import com.google.gson.*; +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.nio.file.*; +import java.util.*; +import net.minecraft.core.*; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.tags.*; + +/** Original launcher transport. Each worker owns one server OR client classpath. */ +public final class PrepareWorker { + private static Runnable restoreTags(Registry registry) { + var tags = new HashMap, List>>(); + registry.getTags().forEach(tag -> tags.put(tag.key(), tag.stream().toList())); + return () -> registry.prepareTagReload(new TagLoader.LoadResult<>(registry.key(), tags)).apply(); + } + + public static void main(String[] args) throws Exception { + if (args.length > 0 && args[0].equals("validate")) { + try (var paths = Files.walk(Path.of(args[1]))) { + for (var path : paths.filter(p -> p.toString().endsWith(".class")).toList()) { + String name = Path.of(args[1]).relativize(path).toString() + .replace(File.separatorChar, '.').replaceAll("\\.class$", ""); + var type = Class.forName(name, false, PrepareWorker.class.getClassLoader()); + type.getDeclaredMethods(); type.getDeclaredConstructors(); type.getDeclaredFields(); + } + } + return; + } + // Bootstrap replaces System.out. Keep the protocol on the original descriptor. + var replies = new PrintWriter(new OutputStreamWriter(new FileOutputStream(FileDescriptor.out), StandardCharsets.UTF_8), true); + System.setOut(System.err); + net.minecraft.SharedConstants.tryDetectVersion(); + net.minecraft.server.Bootstrap.bootStrap(); + var resets = new ArrayList(); + for (var registry : BuiltInRegistries.REGISTRY) resets.add(restoreTags(registry)); + System.setProperty("horizon.prepare.reuse", "true"); + try (var input = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8))) { + for (String line; (line = input.readLine()) != null;) { + var task = JsonParser.parseString(line).getAsJsonArray(); + var previousOut = System.out; var previousErr = System.err; + String error = ""; + try (var log = new PrintStream(Files.newOutputStream(Path.of(task.get(0).getAsString()), + StandardOpenOption.CREATE, StandardOpenOption.APPEND), true, StandardCharsets.UTF_8)) { + System.setOut(log); System.setErr(log); + try { + var values = task.get(2).getAsJsonArray(); + var argv = new String[values.size()]; + for (int i = 0; i < argv.length; i++) argv[i] = values.get(i).getAsString(); + Class.forName("dev.horizonlab.parity." + task.get(1).getAsString()) + .getMethod("main", String[].class).invoke(null, (Object) argv); + } catch (Throwable failure) { + failure.printStackTrace(log); error = failure.toString(); + } finally { + for (var reset : resets) reset.run(); + } + } finally { + System.setOut(previousOut); System.setErr(previousErr); + } + replies.println(new Gson().toJson(error)); + if (!error.isEmpty()) System.exit(1); + } + } + // Vanilla may have started non-daemon executors. All task outputs are closed now. + System.exit(0); + } +} diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCollisionOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCollisionOracle.java index 3466606cd..6ac41ff47 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCollisionOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCollisionOracle.java @@ -19,6 +19,14 @@ import net.minecraft.world.phys.shapes.*; /** Exports private runtime data locally and invokes actual vanilla cached-shape queries. */ public final class VanillaCollisionOracle { static { SharedConstants.tryDetectVersion(); Bootstrap.bootStrap(); } + private static final boolean REUSE = Boolean.getBoolean("horizon.prepare.reuse"); + private static Map generated; + private static final Map encodedStates = new IdentityHashMap<>(); + private static final Map encodedGrids = new IdentityHashMap<>(); + private static Tag encode(BlockState state) { + if (!REUSE) return BlockState.FULL_CODEC.encodeStart(NbtOps.INSTANCE,state).getOrThrow(); + return encodedStates.computeIfAbsent(state, s -> BlockState.FULL_CODEC.encodeStart(NbtOps.INSTANCE,s).getOrThrow()).copy(); + } private static final Field CACHE, COLLISION, DISCRETE; private static final Method INDEX; static { @@ -37,6 +45,7 @@ public final class VanillaCollisionOracle { return shape; } private static CompoundTag grid(VoxelShape shape) throws ReflectiveOperationException { + if (REUSE && encodedGrids.containsKey(shape)) return encodedGrids.get(shape).copy(); CompoundTag tag = new CompoundTag(); tag.putByte("canonical",(byte)(shape==Shapes.empty()?1:shape==Shapes.block()?2:0)); tag.putByte("cube", (byte)(shape instanceof CubeVoxelShape ? 1 : 0)); @@ -52,13 +61,14 @@ public final class VanillaCollisionOracle { byte[] bits = new byte[discrete.getXSize() * discrete.getYSize() * discrete.getZSize()]; for (int x = 0; x < discrete.getXSize(); x++) for (int y = 0; y < discrete.getYSize(); y++) for (int z = 0; z < discrete.getZSize(); z++) bits[(x * discrete.getYSize() + y) * discrete.getZSize() + z] = (byte)(discrete.isFull(x, y, z) ? 1 : 0); tag.putByteArray("occupied", bits); + if (REUSE) encodedGrids.put(shape, tag.copy()); return tag; } public static CompoundTag occlusionDefinitions() throws Exception { var method=BlockBehaviour.class.getDeclaredMethod("getOcclusionShape",BlockState.class);method.setAccessible(true); var states=new ListTag();var shapes=new ListTag();var ids=new IdentityHashMap();int[] mapping=new int[Block.BLOCK_STATE_REGISTRY.size()];int index=0; for(var state:Block.BLOCK_STATE_REGISTRY) { - states.add(BlockState.FULL_CODEC.encodeStart(NbtOps.INSTANCE,state).getOrThrow()); + states.add(encode(state)); var shape=(VoxelShape)method.invoke(state.getBlock(),state);Integer id=ids.get(shape); if(id==null){id=shapes.size();ids.put(shape,id);shapes.add(grid(shape));} mapping[index++]=id; @@ -70,12 +80,16 @@ public final class VanillaCollisionOracle { } private static void inputValues(PrintWriter out, double... values) { for (double value : values) out.printf(" %016x", Double.doubleToRawLongBits(value)); } private static void generate(Path base) throws Exception { + if (REUSE && generated != null) { + for (var file : generated.entrySet()) Files.write(base.resolve(file.getKey()), file.getValue()); + return; + } ListTag states = new ListTag(), shapes = new ListTag(); CompoundTag defaults = new CompoundTag(); Map ids = new IdentityHashMap<>();Set grids=new HashSet<>(); int[] mapping = new int[Block.BLOCK_STATE_REGISTRY.size()]; int cachedCount = 0; for (BlockState state : Block.BLOCK_STATE_REGISTRY) { int id = states.size(); if (Block.BLOCK_STATE_REGISTRY.getId(state) != id) throw new AssertionError("non-dense states"); - Tag encoded = BlockState.FULL_CODEC.encodeStart(NbtOps.INSTANCE, state).getOrThrow(); states.add(encoded); + Tag encoded = encode(state); states.add(encoded); if (state == state.getBlock().defaultBlockState()) defaults.putInt(((CompoundTag)encoded).getStringOr("id", ""), id); VoxelShape shape = cached(state); if (shape == null) mapping[id] = -1; @@ -109,6 +123,12 @@ public final class VanillaCollisionOracle { } } } + if (REUSE) { + // Consumers can overwrite block-registry.nbt; preserve the original bytes. + generated = new HashMap<>(); + for (String file : List.of("block-registry.nbt", "cached-collision.nbt", "inputs.txt")) + generated.put(file, Files.readAllBytes(base.resolve(file))); + } System.out.printf("HORIZON_COLLISION_RESULT {\"registry_states\":%d,\"cached_states\":%d,\"uncached_states\":%d,\"unique_shapes\":%d,\"unique_grids\":%d,\"queries\":%d}%n", mapping.length,cachedCount,mapping.length-cachedCount,shapes.size(),grids.size(),cachedCount*6); } private static void values(PrintWriter out, String label, double... values) { out.print(label); for (double value : values) out.printf(" %016x",Double.doubleToLongBits(value)); out.println(); } diff --git a/packaging/PLAY.txt b/packaging/PLAY.txt index 20ea402fb..6d696b16c 100644 --- a/packaging/PLAY.txt +++ b/packaging/PLAY.txt @@ -17,7 +17,7 @@ Downloaded this archive directly? More about Horizon: https://playhorizon.dev To prepare this folder by hand you need Minecraft Java Edition 26.3, played - once from the official launcher, and Java 25 (a JDK). Then, in this folder: + once from the official launcher, and a Java 25 runtime. Then, in this folder: ./horizon-prepare --prepare --server-jar PATH/TO/server.jar ./horizon-prepare sounds (optional, for sound) (Windows: horizon-prepare.exe.) The 26.3 server jar is the downloads.server.url diff --git a/tests/test_prepare_exporters.py b/tests/test_prepare_exporters.py new file mode 100644 index 000000000..56416100a --- /dev/null +++ b/tests/test_prepare_exporters.py @@ -0,0 +1,37 @@ +"""The release comparison must tolerate only known variation and still catch regressions.""" +import contextlib +import io +import json +from pathlib import Path +import tempfile +import unittest + +from tools.check_prepare_exporters import verify + + +class PrepareExportersTests(unittest.TestCase): + def test_known_noise_does_not_mask_changed_resources(self): + with tempfile.TemporaryDirectory() as temporary: + folders = [Path(temporary) / str(i) for i in range(3)] + for i, folder in enumerate(folders): + folder.mkdir() + (folder / 'stable.nbt').write_bytes(b'unchanged') + (folder / 'horizon-resources.json').write_text('{"schema":7}') + (folder / 'block-behaviours.json').write_text(json.dumps({ + 'fields': {'shape': f'', 'solid': True}})) + (folder / 'creative-inventory.json').write_text(json.dumps({ + 'tabs': [{}, {'tags': [['a', 'b'] if i < 2 else ['b', 'a']], 'items': ['stone']}]})) + with contextlib.redirect_stdout(io.StringIO()): + verify(folders) + for name, changed in [ + ('stable.nbt', b'changed'), + ('block-behaviours.json', b'{"fields":{"shape":"","solid":false}}'), + ('creative-inventory.json', b'{"tabs":[{},{"tags":[["a","b"]],"items":["dirt"]}]}'), + ('horizon-resources.json', b'{"schema":8}'), + ]: + path = folders[2] / name + original = path.read_bytes() + path.write_bytes(changed) + with self.subTest(resource=name), self.assertRaises(SystemExit): + verify(folders) + path.write_bytes(original) diff --git a/tools/check_prepare_exporters.py b/tools/check_prepare_exporters.py new file mode 100644 index 000000000..6d37dfe93 --- /dev/null +++ b/tools/check_prepare_exporters.py @@ -0,0 +1,145 @@ +#!/usr/bin/env python3 +"""Compare two isolated preparations with a batched, runtime-only release preparation. + +Uses only locally imported pinned Minecraft files. Run after compile-exporters and copy its +server/, client/ and manifest.json to exporters/. Byte differences are accepted only where +both isolated runs also differ and normalize identically (known exporter noise). +""" +import argparse +import hashlib +import json +import os +import re +from pathlib import Path +import shlex +import shutil +import subprocess +import tempfile +import time + +ROOT = Path(__file__).resolve().parents[1] + + +def hashes(folder): + return {str(p.relative_to(folder)): hashlib.sha256(p.read_bytes()).hexdigest() + for p in folder.rglob('*') if p.is_file() and p.suffix != '.log' + and not {'classes', 'client-classes'} & set(p.relative_to(folder).parts) + and p.name != 'horizon-resources.json'} + + +def normalize(path): + value = json.loads(path.read_text()) + if path.name == 'block-behaviours.json': + # JVM-assigned lambda class addresses are printed as opaque field values. + def clean(node): + if isinstance(node, str): + return re.sub(r'\$\$Lambda/0x[0-9a-fA-F]+', '$$Lambda/
    ', node) + if isinstance(node, list): + return [clean(item) for item in node] + if isinstance(node, dict): + return {key: clean(item) for key, item in node.items()} + return node + value = clean(value) + elif path.name == 'creative-inventory.json': + for tab in value['tabs']: + if 'tags' in tab: + tab['tags'] = [sorted(tags) for tags in tab['tags']] + return value + + +def verify(folders): + a, b, batched = [hashes(folder) for folder in folders] + if a.keys() != b.keys() or a.keys() != batched.keys(): + raise SystemExit('Resource file sets differ') + known = {'render-definitions.json', 'block-behaviours.json', 'creative-inventory.json'} + noise = [name for name in a if a[name] != b[name] or a[name] != batched[name]] + differences = [] + for name in noise: + if name not in known: + differences.append(name) + else: + x, y, z = [normalize(folder / name) for folder in folders] + if x != y or x != z: + differences.append(name) + manifests = [] + for folder, raw in zip(folders, [a, b, batched]): + replacements = {raw[name]: f'normalized:{name}' for name in noise} + def clean(node): + if isinstance(node, str): + return replacements.get(node, node) + if isinstance(node, list): + return [clean(item) for item in node] + if isinstance(node, dict): + return {key: clean(item) for key, item in node.items()} + return node + manifests.append(clean(json.loads((folder / 'horizon-resources.json').read_text()))) + if manifests[0] != manifests[1] or manifests[0] != manifests[2]: + differences.append('horizon-resources.json') + print(f'Normalized exporter noise: {noise}') + if differences: + raise SystemExit(f'Resources differ: {differences}') + print(f'PASS: all {len(a)} resource files match, with only known noise normalized') + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--binary', type=Path, required=True) + parser.add_argument('--work', type=Path) + args = parser.parse_args() + binary = args.binary.resolve() + with tempfile.TemporaryDirectory(prefix='prepare-exporters-') as temporary: + work = args.work.resolve() if args.work else Path(temporary) + work.mkdir(parents=True, exist_ok=True) + # Freeze the tested executable even if a developer rebuilds concurrently. + frozen = work / binary.name + shutil.copy2(binary, frozen) + binary = frozen + results = [] + times = [] + for name in ['isolated-1', 'isolated-2', 'batched']: + folder = work / name + folder.mkdir() + shutil.copyfile(ROOT / 'research-lock.json', folder / 'research-lock.json') + shutil.copytree(ROOT / 'mods/horizon/src/test/java', folder / 'mods/horizon/src/test/java') + shutil.copytree(ROOT / 'exporters', folder / 'exporters') + (folder / '.local').mkdir() + (folder / '.local/minecraft').symlink_to((ROOT / '.local/minecraft').resolve(), target_is_directory=True) + env = dict(os.environ) + env.pop('HORIZON_PREPARE_ISOLATED', None) + if name.startswith('isolated'): + env['HORIZON_PREPARE_ISOLATED'] = '1' + else: + # An actual runtime-only home: no javac in bin. Java resolves its own libraries. + java = Path(shutil.which('java')).resolve() + runtime = folder / 'runtime' + (runtime / 'bin').mkdir(parents=True) + trace = folder / 'java-invocations.txt' + wrapper = runtime / 'bin' / java.name + wrapper.write_text('#!/bin/sh\nprintf "%s\\n" "$*" >> ' + + shlex.quote(str(trace)) + '\nexec ' + + shlex.quote(str(java)) + ' "$@"\n') + wrapper.chmod(0o755) + env['JAVA_HOME'] = str(runtime) + env['PATH'] = str(runtime / 'bin') + started = time.monotonic() + with (work / f'{name}.log').open('w') as log: + subprocess.run([str(binary), '--root', str(folder)], cwd=folder, env=env, + stdout=log, stderr=subprocess.STDOUT, check=True, timeout=1200) + if name == 'batched': + launches = [line for line in trace.read_text().splitlines() if line != '-version'] + if len(launches) != 5: + raise SystemExit(f'Expected 5 exporter/data-generator JVMs, got {len(launches)}') + print(f'Runtime-only preparation: {len(launches)} JVM launches', flush=True) + text = (work / f'{name}.log').read_text() + if 'Compiling ' in text: + raise SystemExit(f'{name} unexpectedly needed a compiler') + resources = next((folder / '.local/horizon-client').iterdir()) + results.append(resources) + times.append(round(time.monotonic() - started, 2)) + print(f'{name}: {times[-1]}s, prepared', flush=True) + verify(results) + print(f'Isolated {times[:2]}s, batched {times[2]}s') + + +if __name__ == '__main__': + main() diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 28b591626..2682b1f82 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -222,16 +222,40 @@ fn package(binary: &Path, out: &Path) -> Result<(), String> { let repo = new_folder(out)?; copy_into(binary, out)?; copy_beside(binary, &TOOLS, out)?; + copy_exporters(&repo.join("exporters"), &out.join("exporters"))?; let play = repo.join("packaging/PLAY.txt"); copy(&play, &out.join("PLAY.txt")) } fn package_server(engine: &Path, out: &Path) -> Result<(), String> { - new_folder(out)?; + let repo = new_folder(out)?; + copy_exporters(&repo.join("exporters"), &out.join("exporters"))?; copy(engine, &out.join(program_name(engine, "horizon-engine")))?; copy_beside(engine, &SERVER_TOOLS, out) } +/// Release builds supply only our compiled classes and their integrity manifest. +fn copy_exporters(from: &Path, to: &Path) -> Result<(), String> { + if !from.join("manifest.json").is_file() { + return Err("Missing exporters/manifest.json; run horizon-prepare compile-exporters and copy its bundle to exporters/".into()); + } + copy_exporter_tree(from, to) +} + +fn copy_exporter_tree(from: &Path, to: &Path) -> Result<(), String> { + fs::create_dir_all(to).map_err(|e| e.to_string())?; + for entry in fs::read_dir(from).map_err(|e| e.to_string())? { + let path = entry.map_err(|e| e.to_string())?.path(); + let target = to.join(path.file_name().unwrap()); + if path.is_dir() { + copy_exporter_tree(&path, &target)?; + } else { + copy(&path, &target)?; + } + } + Ok(()) +} + /// `OUT`, emptied, with the sources the release folders take (`ARCHIVED`); the repository's path. fn new_folder(out: &Path) -> Result { let top = git( From 5e9d45bc9afe0bf91feb22e49a5232cb00f38287 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:11:39 +0100 Subject: [PATCH 202/238] /pregen: off unless the server turns it on Vanilla has no /pregen, so with it always registered /help and the command tree differed from vanilla's (the command_exec harness failed on /help). Horizon's own commands are now added to the game's tree only when the owner asks: `horizon-pregen=true` in server.properties (`horizon-pregen = true` under [properties] in server.toml) for a dedicated server, `--pregen` for the game (single player, LAN). Off, the trees are vanilla's own, unchanged. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/command/tests.rs | 18 ++++++++ crates/horizon-client/src/command/vanilla.rs | 45 ++++++++++++++------ crates/horizon-client/src/game.rs | 4 +- crates/horizon-client/src/game/dedicated.rs | 8 ++++ docs/hosting.md | 5 ++- docs/pregeneration.md | 6 +++ 6 files changed, 70 insertions(+), 16 deletions(-) diff --git a/crates/horizon-client/src/command/tests.rs b/crates/horizon-client/src/command/tests.rs index 2bdea1aba..d61fd8ae1 100644 --- a/crates/horizon-client/src/command/tests.rs +++ b/crates/horizon-client/src/command/tests.rs @@ -592,3 +592,21 @@ fn mod_commands_join_the_tree_for_every_player() { // The game's own tree is left as it was. assert!(vanilla::game_dispatcher().find("where").is_none()); } + +#[test] +fn horizon_commands_are_off_unless_the_server_turns_them_on() { + // By default the game runs vanilla's tree: /help lists no /pregen. + assert!(vanilla::game_dispatcher().find("pregen").is_none()); + for dev in [false, true] { + let vanilla_tree = vanilla::game_tree(dev, false); + assert!(vanilla_tree.find("pregen").is_none()); + let d = vanilla::game_tree(dev, true); + let parse = d.parse("pregen start 100 circle", ®()); + assert_eq!(parse.executable().unwrap().1, "pregen.start"); + // Vanilla's commands are the same with or without it. + assert_eq!( + d.nodes[0].children.len(), + vanilla_tree.nodes[0].children.len() + 1 + ); + } +} diff --git a/crates/horizon-client/src/command/vanilla.rs b/crates/horizon-client/src/command/vanilla.rs index cba5a5d5b..2ce892077 100644 --- a/crates/horizon-client/src/command/vanilla.rs +++ b/crates/horizon-client/src/command/vanilla.rs @@ -538,20 +538,39 @@ fn apply_vanilla_order(d: &mut Dispatcher) { } /// The tree the game runs: a release integrated server's, or with /// `HORIZON_DEV_COMMANDS` set the development one -/// (SharedConstants.DEBUG_DEV_COMMANDS), plus Horizon's own commands. +/// (SharedConstants.DEBUG_DEV_COMMANDS), plus Horizon's own commands when +/// the server turned them on (`set_horizon_commands`). pub fn game_dispatcher() -> &'static Dispatcher { - static DEV: std::sync::OnceLock = std::sync::OnceLock::new(); - static RELEASE: std::sync::OnceLock = std::sync::OnceLock::new(); - let (tree, dev) = if std::env::var_os("HORIZON_DEV_COMMANDS").is_some() { - (&DEV, true) - } else { - (&RELEASE, false) - }; - tree.get_or_init(|| { - let mut d = build_with(dev); - horizon_commands(&mut d, 0); - d - }) + game_tree( + std::env::var_os("HORIZON_DEV_COMMANDS").is_some(), + HORIZON_COMMANDS.load(std::sync::atomic::Ordering::Relaxed), + ) +} +/// Whether this process's server has Horizon's own commands (`/pregen`). Off +/// by default, so `/help` and the command tree are vanilla's; a dedicated +/// server turns them on with `horizon-pregen=true` in server.properties, the +/// game with `--pregen` (docs/pregeneration.md). +static HORIZON_COMMANDS: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); +/// Turns Horizon's own commands on or off for the trees `game_dispatcher` +/// returns from now on. +pub fn set_horizon_commands(on: bool) { + HORIZON_COMMANDS.store(on, std::sync::atomic::Ordering::Relaxed); +} +/// The server.properties key that turns `/pregen` on (Horizon's own; vanilla +/// ignores keys it doesn't know). +pub const PREGEN_PROPERTY: &str = "horizon-pregen"; +pub(crate) fn game_tree(dev: bool, horizon: bool) -> &'static Dispatcher { + static HORIZON: [std::sync::OnceLock; 2] = + [const { std::sync::OnceLock::new() }; 2]; + match (dev, horizon) { + (true, false) => dispatcher(), + (false, false) => release_dispatcher(), + (dev, true) => HORIZON[usize::from(dev)].get_or_init(|| { + let mut d = build_with(dev); + horizon_commands(&mut d, 0); + d + }), + } } /// Commands vanilla doesn't have, after its own: `/pregen` /// (docs/pregeneration.md). diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index fa00e3804..08e1ff68b 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -360,7 +360,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 --width N, --height N Window size (default 1280 x 720)\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 --in-frame 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 — 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 --width N, --height N Window size (default 1280 x 720)\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 --in-frame Run single player's world in the frame, not on its server thread\n --pregen Add Horizon's /pregen command (docs/pregeneration.md)\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); @@ -377,6 +377,8 @@ impl Options { "--control" => o.control = Some(horizon_client::control::default_path()), "--server-thread" => o.server_thread = true, "--in-frame" | "--no-server-thread" => o.server_thread = false, + // Horizon's own commands (/pregen), off by default as on vanilla. + "--pregen" => horizon_client::command::vanilla::set_horizon_commands(true), _ => { let value = args .next() diff --git a/crates/horizon-client/src/game/dedicated.rs b/crates/horizon-client/src/game/dedicated.rs index edea0e800..fa5365195 100644 --- a/crates/horizon-client/src/game/dedicated.rs +++ b/crates/horizon-client/src/game/dedicated.rs @@ -155,6 +155,14 @@ pub(super) fn preflight(options: &mut Options) -> Result { for w in &props.warnings { log("WARN", w); } + // Horizon's own commands are the owner's choice, off as on vanilla. + if props + .raw + .get(horizon_client::command::vanilla::PREGEN_PROPERTY) + .is_some_and(|v| v.trim().eq_ignore_ascii_case("true")) + { + horizon_client::command::vanilla::set_horizon_commands(true); + } props.store(&settings)?; let eula_file = PathBuf::from("eula.txt"); let agreed = match eula::agreed(&eula_file) { diff --git a/docs/hosting.md b/docs/hosting.md index 4c4c954bc..a9624dc2c 100644 --- a/docs/hosting.md +++ b/docs/hosting.md @@ -218,8 +218,9 @@ horizon-server rollback back to the release before While the server runs, `op`, `whitelist` and `cmd` go to the game's console, so they act exactly as vanilla's console commands do. While it's stopped, they edit vanilla's files directly. Any console command works through `cmd`: `horizon-server cmd ban Griefer`, `horizon-server cmd say Restarting in 5 minutes`. -To generate the world around spawn ahead of the players, Horizon has its own `pregen` command: -`horizon-server cmd pregen start 2500` ([pregeneration](pregeneration.md)). +To generate the world around spawn ahead of the players, Horizon has its own `pregen` command, off until +`server.toml` has `horizon-pregen = true` under `[properties]`: `horizon-server cmd pregen start 2500` +([pregeneration](pregeneration.md)). `config set` changes `server.toml` without opening it: `horizon-server config set world.difficulty=hard server.max_players=10`. Each key is a section and a name from the file (`properties.spawn-protection=0` for any diff --git a/docs/pregeneration.md b/docs/pregeneration.md index a8ca228b3..706527661 100644 --- a/docs/pregeneration.md +++ b/docs/pregeneration.md @@ -5,6 +5,12 @@ Chunky does on a Java server. It is Horizon's own command (vanilla has none), av with cheats on and to operators (level 2) on a server, from the chat or the server console (`horizon-server cmd pregen start 2500`). +It is off until you turn it on, so `/help` and the command tree stay vanilla's: + +- a server: `horizon-pregen = true` under `[properties]` in `server.toml` (or `horizon-pregen=true` in a + vanilla folder's `server.properties`), then restart it; +- single player and LAN: start the game with `--pregen`. + ```text /pregen start [square|circle] [ ] radius in blocks; the centre defaults to where you stand (the world spawn from the console) From 03190ec724589fdaa639501640397b75447880ec Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 25 Oct 2026 23:13:02 +0000 Subject: [PATCH 203/238] Update the runtime download fixture for runtime-only discovery Co-Authored-By: Codex --- launcher/src/mojang_tests.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/launcher/src/mojang_tests.rs b/launcher/src/mojang_tests.rs index 24ed80bca..cd32e4c79 100644 --- a/launcher/src/mojang_tests.rs +++ b/launcher/src/mojang_tests.rs @@ -247,9 +247,12 @@ fn links_stay_inside_the_runtime() { #[test] fn java_runtime_lays_out_mojangs_runtime_with_its_executables_and_links() { const JAVAC: &[u8] = b"#!/bin/sh\necho javac 25.0.1\n"; + const JAVA: &[u8] = b"#!/bin/sh\necho java 25.0.1\n"; const LICENSE: &[u8] = b"a fake license"; let manifest = serde_json::json!({"files": { "bin": {"type": "directory"}, + "bin/java": {"type": "file", "executable": true, "downloads": {"raw": + {"sha1": sha1_hex(JAVA), "size": JAVA.len(), "url": "https://piston-data.mojang.com/java"}}}, "bin/javac": {"type": "file", "executable": true, "downloads": {"raw": {"sha1": sha1_hex(JAVAC), "size": JAVAC.len(), "url": "https://piston-data.mojang.com/javac"}}}, "legal/java.base/LICENSE": {"type": "file", "executable": false, "downloads": {"raw": @@ -269,6 +272,7 @@ fn java_runtime_lays_out_mojangs_runtime_with_its_executables_and_links() { "https://piston-meta.mojang.com/m.json".into(), manifest.into_bytes(), ), + ("https://piston-data.mojang.com/java".into(), JAVA.to_vec()), ( "https://piston-data.mojang.com/javac".into(), JAVAC.to_vec(), From 21d8e530346f3e05c0256e2c085bceb0103f823d Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:14:51 +0100 Subject: [PATCH 204/238] parity: pick harnesses by the Rust items their probes reach, not whole crates `--changed` / `--changed-files` now narrow each harness's recorded Rust sources to those its probes can reach (tools/parity_reach.py): an item-level reachability walk from the probe modules a harness ran (or the binary it built) over free items, inherent methods, trait impls and macros, matched by name so it only over-approximates. Parent modules count through their `mod` declarations, compared with the merge base. A harness whose probe can't be placed keeps every source; `--by-crate` turns the narrowing off. On the last 20 PRs merged on 2026-10-25: 2255 -> 1061 harnesses picked, 2384 -> 1592 harness-minutes. Replaying the week's 61 failed CI parity runs, every failed harness the old selection picked is still picked; LLVM coverage of six harnesses' probes executes only kept files. Co-Authored-By: Claude Opus 5.5 --- docs/parity-workflow.md | 4 + tests/test_parity_reach.py | 111 ++++++ tools/parity.py | 49 ++- tools/parity_reach.py | 768 +++++++++++++++++++++++++++++++++++++ 4 files changed, 925 insertions(+), 7 deletions(-) create mode 100644 tests/test_parity_reach.py create mode 100644 tools/parity_reach.py diff --git a/docs/parity-workflow.md b/docs/parity-workflow.md index f9f222071..d794b9c71 100644 --- a/docs/parity-workflow.md +++ b/docs/parity-workflow.md @@ -49,6 +49,10 @@ A harness can declare, at the top level of its script: - `PARITY_SHARDS = N`: it runs as `NAME#1` to `NAME#N`, in parallel, each told its share in `PARITY_SHARD=i/N` (`test_datafix_parity.py` upgrades every sixth world). `NAME` on the command line or in `--known-failures` means all of them. - `PARITY_RUST_SCOPE = [...]`: the Rust sources its probes can reach, as path prefixes. Cargo's dep-info names every file of every crate a probe links, so without it a probe of `horizon-world` depends on all 350 of its files. The runner keeps only the scope's files if the scope is closed: nothing in it names a module, crate-root re-export or macro outside it (`crate::`, `super::`, other workspace crates; comments and strings ignored), declares a module it doesn't cover, or includes source, and nothing outside it implements anything for a type it defines. Crate roots then count without their declarations of other modules, so adding a module elsewhere doesn't rerun the harness. If the scope isn't closed the runner says why and keeps every file. `datafix` depends on 41 files this way, not 354. +### Which harnesses a change affects + +`--changed` and `--changed-files` narrow each harness's Rust sources to those its probes can reach (`tools/parity_reach.py`), so a change to one module of `horizon-client` no longer reruns every harness whose probe links the crate. The narrowing works on items, not files: from the probe modules a harness ran (or the binary it built), a free item (fn, type, const, static) is reached when a reached item names it in its own module, through a path or an import of a module that is named, or through a glob import; an inherent impl's methods count one by one once their type is reached and their name is mentioned; a trait impl counts once its trait and its type are reached (std's traits and types always are); a macro counts once invoked. Names match by spelling, so the result only over-approximates. A reached file's parent modules count through their `mod` declarations of it, compared with the merge base; Rust files a change adds take part, a dependency that was deleted counts as changed, code under `#[cfg(test)]` never counts, and a harness whose probe can't be found keeps every source. Parses are cached by content in `.local/parity-runner/reach-cache.json`. `--by-crate` turns the narrowing off. On the last 20 pull requests merged on 2026-10-25, the harnesses picked went from 2255 to 1061 (2384 to 1592 harness-minutes; game-binary harnesses, which link all of `game/`, set the floor), and every harness a CI parity run failed in the week before was still picked; every source file a sample of probes executed (LLVM `-C instrument-coverage`) is among those kept. Building the graph takes 15 s cold, under a second warm, and up to a minute and a half on a change that reaches many harnesses. + ## The gate `tools/gate.py` is what a branch must pass before it is integrated: the public-tree check, rustfmt, clippy with warnings denied, the Rust and Python tests, then the harnesses whose inputs changed. It stops at the first failure and writes `.local/gate.json`. `--known-failures a,b` lets harnesses that already fail on the integration branch keep failing. `--quick` checks formatting, lints and Python tests only; `--no-parity` skips the harnesses. diff --git a/tests/test_parity_reach.py b/tests/test_parity_reach.py new file mode 100644 index 000000000..9b7464b47 --- /dev/null +++ b/tests/test_parity_reach.py @@ -0,0 +1,111 @@ +"""tools/parity_reach.py: which workspace sources a harness's probe can reach.""" +from pathlib import Path +import tempfile +import textwrap +import unittest + +from tools import parity, parity_reach + +PROBE = [["example", "world_probe", "release", ["-p", "a"], True]] + + +def workspace(root, files): + for path, text in files.items(): + (root / path).parent.mkdir(parents=True, exist_ok=True) + (root / path).write_text(textwrap.dedent(text), encoding="utf-8") + + +class ReachTests(unittest.TestCase): + def setUp(self): + self.directory = tempfile.TemporaryDirectory() + self.root = Path(self.directory.name) + workspace(self.root, { + "crates/a/Cargo.toml": '[package]\nname = "a"\n\n[[example]]\nname = "world_probe"\npath = "examples/probe/main.rs"\n', + "crates/a/src/lib.rs": """ + pub mod used; + pub mod unused; + mod methods; + mod display; + mod table; + #[cfg(test)] + mod tests; + """, + "crates/a/src/used.rs": """ + pub struct Thing; + pub fn helper() -> u32 { 1 } + """, + # Same names as used.rs, but nothing names this module. + "crates/a/src/unused.rs": "pub struct Thing;\npub fn helper() -> u32 { 2 }\n", + "crates/a/src/methods.rs": """ + use crate::used::Thing; + impl Thing { + pub fn method(&self) -> u32 { 3 } + pub fn never_called(&self) -> u32 { 4 } + } + """, + "crates/a/src/display.rs": """ + impl std::fmt::Display for crate::used::Thing { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "thing") } + } + """, + "crates/a/src/table.rs": """ + macro_rules! table { () => { pub struct Table; }; } + table!(); + """, + "crates/a/src/tests.rs": "use crate::used::Thing;\nimpl Thing { pub fn method_in_tests(&self) {} }\n", + "crates/a/examples/probe/main.rs": "mod dispatch;\nmod p;\n", + "crates/a/examples/probe/dispatch.rs": "pub fn args() {}\n", + "crates/a/examples/probe/p.rs": """ + use a::used::{helper, Thing}; + pub fn main() { println!("{} {} {}", helper(), Thing.method(), Thing); } + """, + }) + self.dependencies = sorted(str(path.relative_to(self.root)) for path in self.root.rglob("*") if path.is_file()) + self.dependencies.remove("crates/a/examples/probe/main.rs") # parity.only_probes_run leaves it out + + def tearDown(self): + self.directory.cleanup() + + def narrowed(self, changed=(), artifacts=PROBE): + graph = parity_reach.SourceGraph(self.root, parity.rust_code) + return parity_reach.narrow(self.dependencies, artifacts, graph, set(changed)) + + def test_keeps_what_the_probe_reaches(self): + kept = {path for path, (whole, _) in self.narrowed().items() if whole} + self.assertIn("crates/a/src/used.rs", kept) + self.assertIn("crates/a/src/methods.rs", kept) # Thing.method() + self.assertIn("crates/a/src/display.rs", kept) # std's Display for a reached type + self.assertIn("crates/a/Cargo.toml", kept) + self.assertNotIn("crates/a/src/unused.rs", kept) # its names match, but nothing names its module + self.assertNotIn("crates/a/src/table.rs", kept) + self.assertNotIn("crates/a/src/tests.rs", kept) # compiled into tests only + + def test_module_parents_count_through_their_declarations(self): + self.assertEqual(self.narrowed()["crates/a/src/lib.rs"], (False, {"used", "methods", "display"})) + text = (self.root / "crates/a/src/lib.rs").read_text() + self.assertEqual(parity_reach.declarations(text, {"used"}, False), "pub mod used;") + self.assertEqual(parity_reach.declarations(text, {"tests"}, False), "#[cfg(test)]\nmod tests;") + # Adding or dropping a module beside the reached ones changes neither view. + other = text.replace("mod table;", "mod table2;\nmod more;") + self.assertEqual(parity_reach.declarations(other, {"used"}, False), "pub mod used;") + self.assertEqual(parity_reach.declarations(other, {"used"}, True), parity_reach.declarations(text, {"used"}, True)) + self.assertNotEqual(parity_reach.declarations(text + "pub fn f() {}\n", {"used"}, True), + parity_reach.declarations(text, {"used"}, True)) + + def test_a_new_file_counts_once_it_is_reached(self): + (self.root / "crates/a/src/lib.rs").write_text("pub mod used;\nmod extra;\n") + workspace(self.root, {"crates/a/src/extra.rs": "impl crate::used::Thing { pub fn method(&self) -> u32 { 5 } }\n"}) + self.assertIn("crates/a/src/extra.rs", self.narrowed(changed={"crates/a/src/extra.rs"})) + + def test_a_deleted_dependency_counts(self): + (self.root / "crates/a/src/unused.rs").unlink() + self.assertIn("crates/a/src/unused.rs", self.narrowed()) + + def test_unknown_probes_keep_everything(self): + self.assertEqual(set(self.narrowed(artifacts=[])), set(self.dependencies)) + self.assertEqual(set(self.narrowed(artifacts=[["example", "missing", "release", ["-p", "a"], True]])), + set(self.dependencies)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/parity.py b/tools/parity.py index 503a5f3bf..dc5fdb1c9 100644 --- a/tools/parity.py +++ b/tools/parity.py @@ -45,6 +45,11 @@ import subprocess import sys import time +try: + from . import parity_reach +except ImportError: + import parity_reach + ROOT = Path(__file__).resolve().parents[1] LOCAL = ROOT / ".local" STATE_DIR = LOCAL / "parity-runner" @@ -1224,20 +1229,44 @@ def changed_harnesses(found, records, ref): return found_changed -def affected_harnesses(found, records, changed): +def affected_harnesses(found, records, changed, reach=True, base=None): """Harnesses the changed files can affect: their script or a recorded dependency (here, or in the shared - dependency lists) changed, a file every harness depends on changed, or nothing is recorded about them yet.""" + dependency lists) changed, a file every harness depends on changed, or nothing is recorded about them yet. + With reach, a harness's workspace Rust sources count only if its probes can reach them (tools/parity_reach.py); + a harness whose probes can't be placed keeps them all. A changed file counts unless, compared with base (a + commit), only declarations of modules the harness doesn't reach changed (tools/parity_reach.py).""" if changed & SHARED_INPUTS: return set(found) + graph = None + if reach and any(path.startswith("crates/") for path in changed): + cache_path = STATE_DIR / "reach-cache.json" + try: + cache = json.loads(cache_path.read_text(encoding="utf-8")) + except (OSError, ValueError): + cache = {} + graph = parity_reach.SourceGraph(ROOT, rust_code, cache) affected = set() for name, script in found.items(): - dependencies = records.get(name, {}).get("dependencies") + record = records.get(name) or {} + dependencies = record.get("dependencies") template = SHARED / "dependencies" / f"{name}.json" if dependencies is None and template.exists(): - dependencies = json.loads(template.read_text(encoding="utf-8")).get("dependencies") - if dependencies is None or Path(script).relative_to(ROOT).as_posix() in changed or \ - any(path.split(KEEP)[0] in changed for path in dependencies): + record = json.loads(template.read_text(encoding="utf-8")) + dependencies = record.get("dependencies") + if dependencies is None or Path(script).relative_to(ROOT).as_posix() in changed: affected.add(name) + continue + plain = {path.split(KEEP)[0] for path in dependencies} + if graph is not None and plain & changed: + narrowed = parity_reach.narrow(dependencies, recorded_artifacts(record), graph, changed) + if any(path.split(KEEP)[0] in changed and (children is None or parity_reach.declarations_changed( + ROOT, path.split(KEEP)[0], children, base, whole)) for path, (whole, children) in narrowed.items()): + affected.add(name) + elif plain & changed: + affected.add(name) + if graph is not None: + STATE_DIR.mkdir(parents=True, exist_ok=True) + cache_path.write_text(json.dumps(graph.saved_cache()), encoding="utf-8") return affected @@ -1280,6 +1309,8 @@ def main(): parser.add_argument("--flake-rounds", type=int, default=3, help="runs required by --flake-check (default 3)") parser.add_argument("--changed", metavar="REF", help="only harnesses the changes since the merge base with REF can " "affect (and that have not passed with their current inputs): the pull-request check") + parser.add_argument("--by-crate", action="store_true", help="with --changed: count every Rust source a probe " + "links, not only those it can reach (tools/parity_reach.py)") parser.add_argument("--changed-files", metavar="FILE", help="like --changed, for the changed paths listed in FILE, " "one per line: for a copy without Git history (tools/pr_ready.py on another machine)") args = parser.parse_args() @@ -1316,7 +1347,11 @@ def main(): changed, since = changed_files(args.changed), f" since the merge base with {args.changed}" if changed is None: parser.error(f"no merge base with {args.changed}") - affected = affected_harnesses(found, records, changed) + base = None + if args.changed: + merge_base = subprocess.run(["git", "merge-base", "HEAD", args.changed], cwd=ROOT, capture_output=True, text=True) + base = merge_base.stdout.strip() or None + affected = affected_harnesses(found, records, changed, reach=not args.by_crate, base=base) chosen = [name for name in found if name in affected and stale(name, state, hasher, environment)] print(f"{len(changed)} files changed{since}: {len(affected)} harnesses " f"depend on them, {len(chosen)} without a pass for their current inputs.", flush=True) diff --git a/tools/parity_reach.py b/tools/parity_reach.py new file mode 100644 index 000000000..0c3051b86 --- /dev/null +++ b/tools/parity_reach.py @@ -0,0 +1,768 @@ +"""Which of a harness's Rust sources its probes can reach, for `parity.py --changed`. + +Cargo's dep-info names every source file of every crate a probe links, so a change anywhere in horizon-client +selected every harness whose probe links horizon-client. This module narrows a harness's recorded dependencies to +the files holding an item its entry points (the probe modules it ran, the binaries it built) can reach, over a graph +of the workspace's current sources whose nodes are items: + +- everything in an entry is reached, and so is everything in a Rust file the module tree doesn't place (an + `include!`d source); +- a module is open when it is a crate root, holds reached code, or is named in a path (`name::`, `use ...name`) by + reached code while its parent is open; +- a free item (fn, struct, enum, union, trait, type, const, static, an item-level macro call's items) is reached when + reached code in its own module or below it mentions its name, or when its module is open and reached code names + it in a path or an import, or when its module is glob-imported (`use m::*`) and its name is mentioned; +- an inherent impl's methods and constants are reached one by one, by name, once their type is; a trait impl once + its trait and its type are (a std or outside name always is, a generic parameter needs nothing); a macro once + invoked; a `pub use` like a free item named by everything it imports; +- "a crate's reached code" covers the crates it names (`horizon_world::`), directly or through one that does, and + code under `#[cfg(test)]` or `#[test]` is left out (probes are never test builds). + +Names match by spelling, so a file can be kept for a name it never meant: the graph only over-approximates. A +reached file's parent modules count through their `mod` declarations of it (parity.py compares those with the merge +base). Everything that isn't a workspace Rust source stays a dependency, except a crate's non-Rust files (shaders, +assets, fonts) that only unreached sources name; a dependency that no longer exists counts as changed, and Rust files +a change adds take part. +""" + +import hashlib +from pathlib import Path, PurePosixPath +import re +import subprocess + +IDENT = r"[A-Za-z_]\w*" +WORD = re.compile(rf"\b({IDENT})\b") +MOD_DECLARATION = re.compile(rf"\bmod\s+({IDENT})\s*;") +INLINE_MOD = re.compile(rf"\bmod\s+({IDENT})\s*\{{") +PATH_ATTRIBUTE = re.compile(r'#\[\s*path\s*=\s*"([^"]+)"\s*\]\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+\w+\s*;$') +BRACE = re.compile(r"[{}]") +TEST_ONLY = re.compile(r"#\[\s*(?:cfg\s*\(\s*test\s*\)|test)\s*\]") +TEST_MODULE = re.compile(r"#\[\s*cfg\s*\(\s*test\s*\)\s*\]\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+\w+\s*;$") +SPLIT = re.compile(r"[{}()\[\];]") +ITEM = re.compile(rf"(?:pub(?:\s*\([^)]*\))?\s+)?(?:(?:unsafe|async|const|extern|default|auto)\s+)*" + rf"(fn|struct|enum|union|trait|type|const|static|impl|mod|use|macro_rules\s*!|extern\s+crate)" + rf"(?!\w)\s*(?:mut\s+)?({IDENT})?") +PATH_BEFORE = re.compile(rf"\b({IDENT})\s*::") +PATH_AFTER = re.compile(rf"::\s*({IDENT})") +MACRO_USE = re.compile(rf"\b({IDENT})\s*!\s*[(\[{{]") +MACRO_CALL = re.compile(rf"({IDENT}(?:\s*::\s*{IDENT})*)\s*!") +DEFINED_INSIDE = re.compile(rf"\b(?:fn|struct|enum|union|trait|type|const|static)\s+({IDENT})") +NESTED_IMPL = re.compile(r"^[ \t]*(?:unsafe\s+)?impl\b([^{;]*)\{", re.MULTILINE) +QUALIFIED = re.compile(rf"{IDENT}(?:\s*::\s*{IDENT})*") +GLOB_IMPORT = re.compile(r"\buse\s[^;]*::\s*\*") +# File names a source may name in a literal (include_str!, include_bytes!, a path it reads). A file of another kind +# is never "named", so it always counts. +FILE_NAME = re.compile(r"[\w-]+\.(?:wgsl|glsl|json|png|ttf|otf|svg|txt|toml|ogg|wav|nbt|bin|dat|csv|mcmeta|wit|wasm)\b") +TYPE_KINDS = {"struct", "enum", "union", "trait", "type", "macro_call"} +# Names an impl header can take from outside the workspace: std's prelude and primitive types, and the std and serde +# traits a glob import may bring. A glob-imported name outside this list is taken to be the workspace's. +STD_NAMES = {"Display", "Error", "Read", "Write", "Seek", "BufRead", "Deref", "DerefMut", "Index", "IndexMut", "Add", + "Sub", "Mul", "Div", "Rem", "Neg", "Not", "BitAnd", "BitOr", "BitXor", "Shl", "Shr", "AddAssign", + "SubAssign", "MulAssign", "DivAssign", "RemAssign", "BitAndAssign", "BitOrAssign", "BitXorAssign", + "Borrow", "BorrowMut", "Future", "Any", "FromStr", "Hasher", "BuildHasher", "Formatter", "Ordering", + "Serialize", "Deserialize", "Serializer", "Deserializer", "Visitor", "Termination", "Sum", "Product", + "Wake", "Cow", "Rc", "Arc", "Mutex", "RwLock", "Cell", "RefCell", "HashMap", "HashSet", "BTreeMap", + "BTreeSet", "VecDeque"} +PRELUDE = {"Option", "Some", "None", "Result", "Ok", "Err", "Vec", "String", "Box", "ToString", "ToOwned", "Clone", + "Copy", "Send", "Sync", "Sized", "Unpin", "Drop", "Fn", "FnMut", "FnOnce", "From", "Into", "TryFrom", + "TryInto", "AsRef", "AsMut", "Default", "Eq", "PartialEq", "Ord", "PartialOrd", "Iterator", "Extend", + "IntoIterator", "DoubleEndedIterator", "ExactSizeIterator", "FromIterator", "Debug", "Hash", + "bool", "char", "str", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128", + "isize", "f32", "f64"} +STD_NAMES |= PRELUDE +KEYWORDS = {"crate", "self", "super", "Self", "pub", "in", "as", "use", "for", "where", "dyn", "mut", "const", "fn", + "unsafe", "impl", "std", "core", "alloc", "let", "if", "else", "match", "return", "struct", "enum", "mod", + "type", "static", "trait", "true", "false", "loop", "while", "move", "ref", "break", "continue", "async", + "await", "extern", "union"} + + +def segments(code, start, end): + """The item-level pieces of code[start:end]: each ends at a `;` or a `}` outside any bracket.""" + found, begin, depth = [], start, 0 + for match in SPLIT.finditer(code, start, end): + character = match.group() + if character in "{([": + depth += 1 + elif character in "})]": + depth -= 1 + if character == "}" and depth == 0: + found.append((begin, match.end())) + begin = match.end() + elif depth == 0: + found.append((begin, match.end())) + begin = match.end() + if code[begin:end].strip(): + found.append((begin, end)) + return found + + +def skip_attributes(code, index, end): + """The offset of the first non-space character after the attributes (`#[...]`, `#![...]`) starting at index.""" + while True: + while index < end and code[index].isspace(): + index += 1 + if not code.startswith("#", index) or not re.match(r"#!?\[", code[index:index + 3]): + return index + depth = 0 + for position in range(code.index("[", index), end): + depth += {"[": 1, "]": -1}.get(code[position], 0) + if depth == 0: + index = position + 1 + break + else: + return end + + +def body_of(code, start, end): + """The range inside an item's outermost `{ }` block, or None.""" + opening = code.find("{", start, end) + closing = code.rfind("}", start, end) + return (opening + 1, closing) if 0 <= opening < closing else None + + +def words(text): + return set(WORD.findall(text)) - KEYWORDS + + +def qualified_words(text): + """Names a piece of code reaches through a path (`a::b`: b), the way items of other modules are named.""" + return set(PATH_AFTER.findall(text)) - KEYWORDS + + +def path_words(text): + """Names a piece of code uses as path segments (`a::`, `::b`), where module names appear.""" + return set(PATH_BEFORE.findall(text)) | set(PATH_AFTER.findall(text)) + + +def parse_items(code, start, end, items): + """Append the items of code[start:end] (a file or an inline module's body) to items, as dicts.""" + for begin, finish in segments(code, start, end): + head = skip_attributes(code, begin, finish) + text = code[head:finish] + if not text.strip(" \n\t;") or TEST_ONLY.search(code[begin:head]): + continue # compiled into tests only, never into a probe + match = ITEM.match(text) + kind = re.sub(r"\s+", "", match.group(1)) if match else None + name = match.group(2) if match else None + if kind == "mod": + body = body_of(code, head, finish) + if body: + parse_items(code, body[0], body[1], items) + continue + if kind in ("externcrate", None) and not MACRO_CALL.match(text): + if kind is None: + items.append({"kind": "glue", "mentions": sorted(words(text)), "paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))}) + continue + if kind is None: # an item-level macro call: it may define items + items.append({"kind": "macro_call", "names": sorted(set(DEFINED_INSIDE.findall(text))), + "macro": MACRO_CALL.match(text).group(1).split("::")[-1].strip(), + "mentions": sorted(words(text)), "paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))}) + elif kind == "use": + head_word = re.match(rf"\s*(?:::\s*)?({IDENT})", text[match.end(1):]) + items.append({"kind": "use", "public": text.startswith("pub"), "names": sorted(words(text)), + "head": head_word.group(1) if head_word else "", "glob": bool(re.search(r"::\s*\*", text)), + "mentions": sorted(words(text)), "paths": sorted(words(text)), "qualified": sorted(words(text)), + "invokes": []}) + elif kind == "impl": + body = body_of(code, head, finish) + subject = code[head + match.end(1) - match.start(): body[0] - 1 if body else finish] + inherent = not re.search(r"\bfor\b", impl_subject(subject)[1]) + if inherent and body: + members = [] + for member_begin, member_end in segments(code, body[0], body[1]): + member = code[skip_attributes(code, member_begin, member_end):member_end] + found = ITEM.match(member) + members.append({"name": found.group(2) if found else None, "mentions": sorted(words(member)), + "paths": sorted(path_words(member)), "qualified": sorted(qualified_words(member)), "invokes": sorted(set(MACRO_USE.findall(member)))}) + items.append({"kind": "impl", "subject": subject, "inherent": True, "members": members, + "mentions": sorted(words(subject)), "paths": sorted(path_words(subject)), "qualified": sorted(qualified_words(subject)), "invokes": sorted(set(MACRO_USE.findall(subject)))}) + else: + items.append({"kind": "impl", "subject": subject, "inherent": False, "mentions": sorted(words(text)), + "paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))}) + else: + kind = "macro" if kind.startswith("macro_rules") else kind + items.append({"kind": kind, "names": [name] if name else [], "mentions": sorted(words(text)), + "defines": sorted(set(DEFINED_INSIDE.findall(text))) if kind == "macro" else [], + "paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))}) + if kind in ("fn", "const", "static"): + # An impl inside a function body still holds for the whole program. + for header in NESTED_IMPL.findall(text): + items.append({"kind": "impl", "subject": header, "inherent": False, + "mentions": sorted(words(text)), "paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))}) + + +def crate_roots(crate_dir): + """A crate's root files: its library, binaries and examples.""" + roots = [crate_dir / "src" / "lib.rs", crate_dir / "src" / "main.rs"] + for folder in (crate_dir / "src" / "bin", crate_dir / "examples"): + if folder.is_dir(): + roots += sorted(folder.glob("*.rs")) + sorted(folder.glob("*/main.rs")) + return [root for root in roots if root.is_file()] + + +class SourceGraph: + """The workspace's Rust sources, parsed once: what each file names, defines and declares.""" + + def __init__(self, root, rust_code, cache=None): + """cache: a dict of earlier parses by content digest, which the graph reuses and adds to (parity.py keeps it + in .local/parity-runner/reach-cache.json).""" + self.root = Path(root) + self.rust_code = rust_code + self.cache = {} if cache is None else cache + self.used = set() + self.files = {} + self.parent = {} # module file -> the file declaring it + self.module_name = {} + self.roots = set() + self.target_cache = {} + self.workspace_heads = None + self.test_only = set() # modules compiled into tests only + self.tables = None + self.root_of = {} # source -> the root of its target (lib.rs, a bin's or an example's main file) + self.results = {} # (entries, universe) -> reachable files + for manifest in sorted((self.root / "crates").glob("*/Cargo.toml")): + for crate_root in crate_roots(manifest.parent): + path = self.relative(crate_root) + self.roots.add(path) + self.root_of[path] = path + self.place(path, crate_root.parent, set()) + + def entries(self, artifacts, rust): + """The root files of the probes and binaries a harness built (its record's artifacts), among its Rust + dependencies: for a crate's probe binary, the probe modules it ran (parity.only_probes_run kept only those). + None when an artifact can't be placed.""" + found = set() + for kind, name, *rest in artifacts: + selection = rest[1] if len(rest) > 1 else [] + packages = [value for flag, value in zip(selection, selection[1:]) if flag == "-p"] or \ + [manifest.parent.name for manifest in sorted((self.root / "crates").glob("*/Cargo.toml"))] + roots = set() + for package in packages: + crate = self.root / "crates" / package + targets = self.targets(crate) + if kind in ("example", "examples"): + candidates = [path for target, (target_kind, path) in targets.items() + if target_kind == "example" and (not name or target == name)] + else: + candidates = [path for target, (target_kind, path) in targets.items() + if target_kind == "bin" and (not name or target == name)] + for path in candidates: + if path.endswith("/examples/probe/main.rs"): + # The probes it ran, and the dispatch and support modules they use (only_probes_run kept + # those); the binary's root, which only declares them, so their own submodules are placed. + folder = path.removesuffix("main.rs") + roots |= {dependency for dependency in rust if dependency.startswith(folder) + and "/" not in dependency.removeprefix(folder).removeprefix("support/")} + if roots: + roots.add(path) + elif path in rust: + roots.add(path) + if not roots: + return None + found |= roots + return found or None + + def targets(self, crate): + """A crate's bins and examples: {target name: (kind, root file)}, from its Cargo.toml and Cargo's defaults.""" + if crate in self.target_cache: + return self.target_cache[crate] + found = {} + manifest = (crate / "Cargo.toml").read_text(encoding="utf-8") if (crate / "Cargo.toml").is_file() else "" + package = re.search(r'^\[package\][^\[]*?^name\s*=\s*"([^"]+)"', manifest, re.MULTILINE | re.DOTALL) + if (crate / "src" / "main.rs").is_file() and package: + found[package.group(1)] = ("bin", self.relative(crate / "src" / "main.rs")) + for kind, folder in (("bin", crate / "src" / "bin"), ("example", crate / "examples")): + for path in sorted(folder.glob("*.rs")) + sorted(folder.glob("*/main.rs")): + found[path.stem if path.name != "main.rs" else path.parent.name] = (kind, self.relative(path)) + for kind, table in re.findall(r"^\[\[(bin|example)\]\]([^\[]*)", manifest, re.MULTILINE): + name = re.search(r'^name\s*=\s*"([^"]+)"', table, re.MULTILINE) + path = re.search(r'^path\s*=\s*"([^"]+)"', table, re.MULTILINE) + if name and path and (crate / path.group(1)).is_file(): + found[name.group(1)] = (kind, self.relative(crate / path.group(1))) + self.target_cache[crate] = found + return found + + def target(self, path): + """The Cargo target a source belongs to: the root file of the library, bin or example whose module tree + holds it (a file nothing places is its own).""" + return self.root_of.get(path, path) + + def is_library(self, path): + return self.target(path).endswith("/src/lib.rs") + + def relative(self, path): + return Path(path).resolve().relative_to(self.root.resolve()).as_posix() + + def place(self, path, module_dir, seen): + """Walk the module tree from a file: record each declared module's file, parent and name.""" + if path in seen: + return + seen.add(path) + parsed = self.parse(path) + if parsed is None: + return + for name, nesting, attribute, test in parsed["declarations"]: + if attribute is not None: + if nesting: + continue # a #[path] inside an inline module: unplaced, so always kept + candidates = [(self.root / path).parent / attribute] + else: + folder = module_dir.joinpath(*nesting) + candidates = [folder / f"{name}.rs", folder / name / "mod.rs"] + for candidate in candidates: + if candidate.is_file(): + child = self.relative(candidate) + self.parent.setdefault(child, path) + self.module_name.setdefault(child, name) + self.root_of.setdefault(child, self.root_of[path]) + if test or path in self.test_only: + self.test_only.add(child) + child_dir = candidate.parent if candidate.name == "mod.rs" or attribute is not None \ + else candidate.parent / candidate.stem + self.place(child, child_dir, seen) + break + + def parse(self, path): + if path in self.files: + return self.files[path] + try: + data = (self.root / path).read_bytes() + except OSError: + self.files[path] = None + return None + digest = hashlib.sha256(data).hexdigest() + self.used.add(digest) + if digest in self.cache: + self.files[path] = parsed = self.cache[digest] + return parsed + raw = data.decode("utf-8", errors="replace") + code = self.rust_code(raw) # same length as raw: literals and comments blanked + opening = {match.end() - 1: match.group(1) for match in INLINE_MOD.finditer(code)} + events = sorted([(match.start(), match) for match in BRACE.finditer(code)] + + [(match.start(), match) for match in MOD_DECLARATION.finditer(code)], key=lambda event: event[0]) + declarations, stack, depth = [], [], 0 + for index, match in events: + if match.group() == "{": + depth += 1 + if index in opening: + stack.append((opening[index], depth)) + elif match.group() == "}": + if stack and stack[-1][1] == depth: + stack.pop() + depth -= 1 + else: + window = " ".join(raw[max(0, index - 300):match.end()].split()) + attribute = PATH_ATTRIBUTE.search(window) + declarations.append([match.group(1), [name for name, _ in stack], attribute.group(1) if attribute else None, + bool(TEST_MODULE.search(window))]) + items = [] + parse_items(code, 0, len(code), items) + self.files[path] = parsed = { + "declarations": declarations, + "items": items, + "glob": bool(GLOB_IMPORT.search(code)), + "file_names": sorted(set(FILE_NAME.findall(raw))), + } + self.cache[digest] = parsed + return parsed + + def saved_cache(self): + """The parses of the files this graph read, to keep for the next run.""" + return {digest: self.cache[digest] for digest in self.used if digest in self.cache} + + def requirement(self, path, item): + """For an impl: for its trait and for its type, [names the file defines, other workspace names], one of + which must be a reached type or trait (of this file, of a crate it sees) before the impl can matter; both + empty: nothing of the workspace is needed. A name is the workspace's when the path naming it starts in the + workspace, or the file defines it, imports it from the workspace or may glob-import it; otherwise (std's, + another crate's, the prelude's) it needs nothing.""" + if "requires" in item: + return item["requires"] + if self.workspace_heads is None: + self.workspace_heads = {"crate", "self", "super"} | set(self.module_name.values()) | { + manifest.parent.name.replace("-", "_") for manifest in (self.root / "crates").glob("*/Cargo.toml")} + parsed = self.parse(path) + defined = {name for other in parsed["items"] for name in other.get("names", ()) if other["kind"] != "use"} + workspace_imports = {name for other in parsed["items"] if other["kind"] == "use" + and other["head"] in self.workspace_heads for name in other["names"]} + parameters, subject = impl_subject(item["subject"]) + parts = [] + for part in re.split(r"\bfor\b", subject, maxsplit=1): + names, own = set(), set() + for match in QUALIFIED.finditer(part): + segments_ = [segment.strip() for segment in match.group().split("::")] + name = segments_[-1] + if name in parameters or name in KEYWORDS: + continue + if len(segments_) > 1: + if segments_[0] in self.workspace_heads: + names.add(name) + elif name in defined: + own.add(name) + elif name in workspace_imports or (parsed["glob"] and name not in STD_NAMES): + names.add(name) + parts.append([sorted(own), sorted(names)]) + item["requires"] = parts + return parts + + def index(self): + """Lookup tables over every placed source, built once: which crates each crate sees, the nodes (items, an + inherent impl's members) by the names they define and the types they need, the modules by name.""" + if self.tables is not None: + return self.tables + idents = {manifest.parent.name.replace("-", "_"): manifest.parent.name + for manifest in (self.root / "crates").glob("*/Cargo.toml")} + paths = [path for path in self.files if self.files[path] is not None and path not in self.test_only] + sees = {} + for path in paths: + named = {word for item in self.files[path]["items"] for word in item["mentions"]} & idents.keys() + sees.setdefault(crate_of(path), {crate_of(path)}).update(idents[name] for name in named) + changed = True + while changed: + changed = False + for crate, seen in sees.items(): + wider = set().union(*(sees.get(other, {other}) for other in seen)) + if not wider <= seen: + seen |= wider + changed = True + seen_by = {} # crate -> the crates that see it + for crate, seen in sees.items(): + for other in seen: + seen_by.setdefault(other, set()).add(crate) + # What a call of a workspace macro defines: what its definition's body defines. + expands = {} + for path in paths: + for item in self.files[path]["items"]: + if item["kind"] == "macro": + for name in item["names"]: + expands.setdefault(name, set()).update(item["defines"]) + names_of, by_name, by_file_name, by_type, by_module, children, seeds = {}, {}, {}, {}, {}, {}, [] + for path in paths: + crate = crate_of(path) + if path in self.module_name: + by_module.setdefault((crate, self.module_name[path]), []).append(path) + children.setdefault(self.parent[path], []).append(path) + for index, item in enumerate(self.files[path]["items"]): + node = (path, index, None) + names = set(item.get("names", ())) + if item["kind"] == "macro_call": + names |= expands.get(item["macro"], set()) + names_of[node] = names + if item["kind"] == "glue": + seeds.append(node) + elif item["kind"] == "macro_call" and not names: + # A macro call defining nothing by name may still expand to impls: it counts once a type or + # trait it mentions is reached. + for name in item["mentions"]: + by_type.setdefault(name, []).append(node) + elif item["kind"] == "impl": + required = {name for own, foreign in self.requirement(path, item) for name in own + foreign} + members = [(path, index, member) for member in range(len(item["members"]))] if item["inherent"] else [] + for member_node in members: + name = item["members"][member_node[2]]["name"] + names_of[member_node] = set() + if name: + by_name.setdefault(name, []).append(member_node) + for name in required: + for other in [node] + members: + by_type.setdefault(name, []).append(other) + if not required: + seeds.append(node) + seeds += [member_node for member_node in members if not item["members"][member_node[2]]["name"]] + else: + for name in names: + by_name.setdefault(name, []).append(node) + by_file_name.setdefault((path, name), []).append(node) + self.tables = dict(idents=idents, sees=sees, seen_by=seen_by, names_of=names_of, by_name=by_name, + by_file_name=by_file_name, by_type=by_type, by_module=by_module, children=children, + seeds=seeds, node_sets={}) + return self.tables + + def reachable(self, entries, universe, reasons=None): + """The files of universe (workspace Rust sources) holding an item reachable from entries (by the rules in + this module's doc), and the entries. reasons, a dict, gets why each file was reached (for debugging).""" + # Searching every source of the crates involved (not only those Cargo listed) only adds reach, and lets + # harnesses with the same entries (the game binary's) share one search. + crates = frozenset(crate_of(path) for path in universe) + key = (frozenset(entries), crates) + if reasons is None and key in self.results: + return self.results[key] & set(universe) + everything = {path for path in self.files if crate_of(path) in crates} | set(universe) + files = self.search(entries, everything, reasons) + self.results[key] = files + return files & set(universe) + + def search(self, entries, universe, reasons): + targets = {self.target(path) for path in entries} + universe = {path for path in universe if (self.is_library(path) or self.target(path) in targets) + and path not in self.test_only} + tables = self.index() + idents, sees, seen_by, names_of = tables["idents"], tables["sees"], tables["seen_by"], tables["names_of"] + node_sets = tables["node_sets"] + by_name, by_file_name, by_type = tables["by_name"], tables["by_file_name"], tables["by_type"] + by_module, children = tables["by_module"], tables["children"] + seeds = [node for node in tables["seeds"] if node[0] in universe] + for path in universe: + if (path in entries or (path not in self.parent and path not in self.roots)) and self.parse(path): + seeds += [(path, index, None) for index in range(len(self.parse(path)["items"]))] + + def names(node): + return names_of.get(node, set()) + + def sets_of(node, item): + """A node's mentions, path words, qualified names and macro calls, as sets (parses keep lists).""" + if node not in node_sets: + source = item["members"][node[2]] if node[2] is not None else item + node_sets[node] = {key: set(source.get(key, ())) for key in ("mentions", "paths", "qualified", "invokes")} + return node_sets[node] + + def item_of(node): + return self.parse(node[0])["items"][node[1]] + + reached, opened, globbed, files, queue = set(), set(), set(), set(), [] + # name -> the crates whose reached code mentions it plainly, names it in a path or an import, invokes it. + mentioned, qualified, invoked = {}, {}, {} + done = {"mentions": {}, "qualified": {}, "invokes": {}, "paths": {}} # crate -> names already handled + path_names, types = {}, {} # by crate + local = {} # file -> plain mentions by reached code in it or below it (child modules may `use super::*`) + local_types = {} # file -> its reached types and traits + + def heard(table, name, crate): + """Whether a crate that can see crate's items has name in table.""" + sources = table.get(name) + return bool(sources) and not sources.isdisjoint(seen_by.get(crate, {crate})) + + def ready(node): + path, index, member = node + item = item_of(node) + crate = crate_of(path) + if item["kind"] == "impl": + visible = visible_types(crate) + here = local_types.get(path, set()) + if not all((not own and not foreign) or here.intersection(own) or visible.intersection(foreign) + for own, foreign in self.requirement(path, item)): + return False + name = item["members"][member]["name"] if member is not None else None + return name is None or heard(mentioned, name, crate) # a method: called by its name + if item["kind"] == "macro_call" and not names(node): + return not visible_types(crate).isdisjoint(item["mentions"]) + names_ = names(node) + if item["kind"] == "macro": + return any(heard(invoked, name, crate) for name in names_) + if item["kind"] == "use" and item["glob"]: + return path in opened + nearby = local.get(path, ()) + if any(name in nearby for name in names_): + return True # named in its own module, or below it + if item["kind"] == "use" and not item["public"]: + return False + if path in opened and any(heard(qualified, name, crate) for name in names_): + return True # named through a path, or imported + return path in globbed and any(heard(mentioned, name, crate) for name in names_) + + visible_cache = {} + + def visible_types(crate): + """The reached types and traits of the crates a crate sees (cached until a type is reached).""" + if crate not in visible_cache: + visible_cache[crate] = set().union(*(types.get(other, set()) for other in sees.get(crate, {crate}))) + return visible_cache[crate] + + def wake(nodes, why): + for other in nodes: + if other not in reached and other[0] in universe and ready(other): + reach(other, why) + + def open_module(path, why): + pending = [path] + while pending: + current = pending.pop() + if current in opened: + continue + opened.add(current) + crate = crate_of(current) + pending += [child for child in children.get(current, ()) + if child in universe and self.module_name[child] in path_names.get(crate, ())] + parsed = self.parse(current) + wake([(current, index, None) for index, item in enumerate(parsed["items"] if parsed else ()) + if item["kind"] != "impl"], f"{current} opened ({why})") + + def reach(node, why): + if node in reached: + return + reached.add(node) + queue.append(node) + path, _, member = node + item = item_of(node) + if (item["kind"] == "use" and not item["public"]) or (item["kind"] == "impl" and item["inherent"] and member is None): + return + if path not in files: + files.add(path) + if reasons is not None: + reasons[path] = why + + for path in universe: + if path.endswith("/src/lib.rs") or path in entries or path not in self.parent: + open_module(path, "crate root or entry") + for node in seeds: + reach(node, "entry" if node[0] in entries else "always counted") + for path in entries & universe: + files.add(path) + if reasons is not None: + reasons.setdefault(path, "entry") + while queue: + node = queue.pop() + path, index, member = node + item = item_of(node) + crate = crate_of(path) + label = (item.get("names") or [item["kind"]])[0] if member is None else item["members"][member]["name"] + source = sets_of(node, item) + why = f"{path} ({label})" + # Plain mentions reach items of this file and of the modules above it. + ancestor, new = path, source["mentions"] + while ancestor is not None and new: + known = local.setdefault(ancestor, set()) + new = new - known + known |= new + for name in new: + wake(by_file_name.get((ancestor, name), ()), f"{why} mentions {name}") + ancestor = self.parent.get(ancestor) + if item["kind"] == "use" and item["glob"]: + for name in item["names"]: + for target in sees.get(crate, {crate}): + for module in list(by_module.get((target, name), ())) + \ + ([f"crates/{idents[name]}/src/lib.rs"] if name in idents else []): + if module not in globbed and module in universe: + globbed.add(module) + wake([(module, index, None) for index in range(len(self.parse(module)["items"]))] + if self.parse(module) else [], f"{why} glob-imports {name}") + visible_to = sees.get(crate, {crate}) + new = source["paths"] - done["paths"].setdefault(crate, set()) + done["paths"][crate] |= new + for name in new: + for target in visible_to: + path_names.setdefault(target, set()).add(name) + for module in by_module.get((target, name), ()): + if module in universe and self.parent[module] in opened: + open_module(module, f"{why} names {name}") + for key, table, wanted in (("qualified", qualified, lambda other: True), + ("invokes", invoked, lambda other: item_of(other)["kind"] == "macro"), + ("mentions", mentioned, lambda other: other[2] is not None or other[0] in globbed)): + new = source[key] - done[key].setdefault(crate, set()) + done[key][crate] |= new + for name in new: + table.setdefault(name, set()).add(crate) + wake([other for other in by_name.get(name, ()) if crate_of(other[0]) in visible_to and wanted(other)], + f"{why} {key} {name}") + if item["kind"] in TYPE_KINDS and member is None: + own = types.setdefault(crate, set()) + local_types.setdefault(path, set()).update(names(node)) + if not names(node) <= own: + own |= names(node) + visible_cache.clear() + seeing = seen_by.get(crate, {crate}) + for name in names(node): # also when the crate has one by that name already: impls of this one + wake([other for other in by_type.get(name, ()) if crate_of(other[0]) in seeing], + f"implements for {name}, defined in {path}") + return files + + +def impl_subject(header): + """An impl header (the text after `impl`): its generic parameters' names, and the rest without its where clause + (the trait and the type it is for).""" + header, parameters = header.strip(), set() + if header.startswith("<"): + depth = 0 + for index, character in enumerate(header): + depth += {"<": 1, ">": -1}.get(character, 0) + if depth == 0: + generics, header = header[1:index], header[index + 1:] + parameters = {match.group(1) for match in re.finditer(rf"(?:^|,)\s*(?:const\s+)?({IDENT})", generics)} + break + return parameters, re.split(r"\bwhere\b", header)[0] + + +def crate_of(path): + return path.split("/", 2)[1] + + +def is_workspace_rust(path): + parts = PurePosixPath(path).parts + return len(parts) > 2 and parts[0] == "crates" and path.endswith(".rs") + + +DECLARATION_LINE = re.compile(rf"\s*(?:pub(?:\([^)]*\))?\s+)?mod\s+({IDENT})\s*;") + + +def declarations(text, names, keep_rest): + """A Rust file's `mod` declarations of names, each with the attribute lines just above it; with keep_rest, the + whole text except the declarations of other modules instead.""" + lines, found, attributes = text.splitlines(), [], [] + for line in lines: + if line.strip().startswith("#[") and not line.strip().startswith("#!["): + attributes.append(line.strip()) + continue + match = DECLARATION_LINE.match(line) + if match and match.group(1) in names: + found += attributes + [line.strip()] + elif keep_rest and not match: + found += attributes + [line] + attributes = [] + return "\n".join(found + attributes) + + +def declarations_changed(root, path, children, base, whole): + """Whether a changed file differs from the commit base in what a harness depends on: for a module parent, its + `mod` declarations of the reached children; for a whole file (whole), everything but its declarations of other + modules (adding a module beside reached ones changes nothing). True without a base, or if either side is + missing.""" + if base is None: + return True + try: + head = (Path(root) / path).read_text(encoding="utf-8", errors="replace") + before = subprocess.run(["git", "show", f"{base}:{path}"], cwd=root, capture_output=True, text=True, check=True).stdout + except (OSError, subprocess.CalledProcessError): + return True + return declarations(head, children, whole) != declarations(before, children, whole) + + +def narrow(dependencies, artifacts, graph, changed=()): + """A harness's dependencies narrowed to what its entry points can reach: {dependency: (whole, children)}, where + whole says the file counts for its own content (else only through its `mod` declarations of children, as a + module ancestor of a reached file) and children are its reached child modules. Without known entry points (a + probe or binary the harness built that isn't found here), every dependency is kept whole.""" + plain = [dependency.split("#keep:")[0] for dependency in dependencies] + crates = {PurePosixPath(path).parts[1] for path in plain if path.startswith("crates/")} + rust = {path for path in plain if is_workspace_rust(path)} + added = {path for path in changed if is_workspace_rust(path) and PurePosixPath(path).parts[1] in crates + and path not in rust and (graph.root / path).is_file()} + entries = graph.entries(artifacts, rust) + if entries is None: + return {dependency: (True, None) for dependency in dependencies} + missing = {path for path in rust if graph.parse(path) is None} + reached = graph.reachable(entries, rust | added | entries) | missing + named, named_in_crate = set(), {} + for path in rust | added: + parsed = graph.parse(path) + file_names = set(parsed["file_names"]) if parsed else set() + if path in reached: + named |= file_names + named_in_crate.setdefault(PurePosixPath(path).parts[1], set()).update(file_names) + kept = {} + for dependency, path in zip(dependencies, plain): + if is_workspace_rust(path): + if path in reached: + kept[dependency] = (True, set()) + elif not path.startswith("crates/") or path.endswith(("Cargo.toml", "build.rs")) \ + or PurePosixPath(path).name in named \ + or PurePosixPath(path).name not in named_in_crate.get(PurePosixPath(path).parts[1], ()): + kept[dependency] = (True, None) + for path in sorted(added & reached): + kept[path] = (True, set()) + for path in sorted(reached): + child = path + while child in graph.parent: + parent = graph.parent[child] + kept.setdefault(parent, (False, set()))[1].add(graph.module_name[child]) + child = parent + return kept From 1e6f05ed5a192f6ccf483f8cb8877448b6eb6a01 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:15:37 +0100 Subject: [PATCH 205/238] Chat option: filter the chat's lines both ways, as setVisibleMessageFilter The chat keeps where each message came from and the Chat option's abilities. A message the option hides is still dropped on arrival, but a change of the option now also hides the lines already in the chat that it no longer shows, and brings them back when it shows them again (ChatComponent.setVisibleMessageFilter -> refreshTrimmedMessages), on apply_settings. Every Chat::add names its source. The Restrictions screen reads its two flags from ChatAbilities instead of its own copy. Co-Authored-By: Claude Opus 5.5 --- .../examples/probe/hud_fixture/kinds.rs | 1 + crates/horizon-client/src/chat.rs | 72 ++++++++++++++++--- crates/horizon-client/src/chat_tests.rs | 24 +++++++ crates/horizon-client/src/game.rs | 3 + crates/horizon-client/src/game/chat.rs | 28 +++++--- crates/horizon-client/src/game/commands.rs | 11 ++- crates/horizon-client/src/game/lan_screen.rs | 5 +- crates/horizon-client/src/game/remote.rs | 13 ++-- 8 files changed, 127 insertions(+), 30 deletions(-) diff --git a/crates/horizon-client/examples/probe/hud_fixture/kinds.rs b/crates/horizon-client/examples/probe/hud_fixture/kinds.rs index d1450f2e1..9ba66abe1 100644 --- a/crates/horizon-client/examples/probe/hud_fixture/kinds.rs +++ b/crates/horizon-client/examples/probe/hud_fixture/kinds.rs @@ -319,6 +319,7 @@ fn chat(c: &Value, h: i32, atlas: &HudAtlas) -> Vec { atlas, comp(&m["text"]), indicator, + horizon_client::chat::ChatSource::Client, m["at"].as_u64().unwrap_or(0), false, ); diff --git a/crates/horizon-client/src/chat.rs b/crates/horizon-client/src/chat.rs index e14178c8d..04b9d486c 100644 --- a/crates/horizon-client/src/chat.rs +++ b/crates/horizon-client/src/chat.rs @@ -72,6 +72,14 @@ impl ChatAbilities { }, } } + /// `visibleMessagesFilter`: the client's own lines always show. + pub fn shows(&self, source: ChatSource) -> bool { + match source { + ChatSource::Player => self.receive_player_messages, + ChatSource::Server => self.receive_system_messages, + ChatSource::Client => true, + } + } /// Whether the chat screen may send `input` (`hasAllowedInput`). pub fn allows(&self, input: &str) -> bool { if input.starts_with('/') { @@ -82,6 +90,12 @@ impl ChatAbilities { } } +impl Default for ChatAbilities { + fn default() -> Self { + Self::new(ChatVisibility::Full) + } +} + /// Where a chat line came from (`GuiMessageSource`): the visibility filters /// player messages and the server's, while the client's own always show. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -143,6 +157,7 @@ struct Message { added: u64, content: Text, indicator: Option, + source: ChatSource, } /// A wrapped line (`GuiMessage.Line`). #[derive(Clone, Debug)] @@ -177,6 +192,8 @@ pub struct Chat { pub log: bool, /// Messages added since the start, for the control socket's events. pub received: u64, + /// The Chat option's `visibleMessageFilter` (`set_abilities`). + abilities: ChatAbilities, } impl Chat { pub fn lines_per_page(&self, focused: bool) -> usize { @@ -186,16 +203,32 @@ impl Chat { fn max_width(&self) -> i32 { (f64::from(self.options.width_px()) / self.options.scale).floor() as i32 } - /// ChatComponent.addMessage: wrap, prepend and trim to 100 lines. + /// ChatComponent.addMessage: wrap, prepend and trim to 100 lines. A + /// message the Chat option hides is dropped, unlogged. pub fn add( &mut self, atlas: &HudAtlas, content: Text, indicator: Option, + source: ChatSource, ticks: u64, focused: bool, ) { - // ChatComponent.logChatMessage: every message is in the log. + self.add_message(&atlas.font, content, indicator, source, ticks, focused); + } + fn add_message( + &mut self, + font: &crate::font::Font, + content: Text, + indicator: Option, + source: ChatSource, + ticks: u64, + focused: bool, + ) { + if !self.abilities.shows(source) { + return; + } + // ChatComponent.logChatMessage: every message shown is in the log. crate::logging::write_only(log::Level::Info, &format!("[CHAT] {}", content.plain())); if self.log { println!("[CHAT] {}", content.plain()); @@ -204,16 +237,17 @@ impl Chat { added: ticks, content, indicator, + source, }; - self.add_lines(atlas, &message, focused); + self.add_lines(font, &message, focused); self.messages.insert(0, message); self.messages.truncate(100); self.received += 1; } - fn add_lines(&mut self, atlas: &HudAtlas, message: &Message, focused: bool) { + fn add_lines(&mut self, font: &crate::font::Font, message: &Message, focused: bool) { let width = self.max_width(); let content = self.displayed(&message.content); - for line in atlas.font.wrap_components(&content, width) { + for line in font.wrap_components(&content, width) { if focused && self.scroll > 0 { self.new_since_scroll = true; self.scroll_by(1, focused); @@ -239,16 +273,38 @@ impl Chat { content.strip_legacy() } } - /// Re-wrap after a width or scale change (ChatComponent.rescaleChat). + /// Re-wrap after a width or scale change (ChatComponent.rescaleChat), + /// keeping only the messages the Chat option shows. pub fn rescale(&mut self, atlas: &HudAtlas) { + self.rewrap(&atlas.font); + } + fn rewrap(&mut self, font: &crate::font::Font) { self.scroll = 0; self.new_since_scroll = false; self.lines.clear(); - let messages: Vec = self.messages.iter().rev().cloned().collect(); + let messages: Vec = self + .messages + .iter() + .rev() + .filter(|m| self.abilities.shows(m.source)) + .cloned() + .collect(); for m in &messages { - self.add_lines(atlas, m, false); + self.add_lines(font, m, false); } } + /// `ChatComponent.setVisibleMessageFilter`: the Chat option changed; + /// the messages it now hides leave the chat, those it shows again return. + pub fn set_abilities(&mut self, font: &crate::font::Font, abilities: ChatAbilities) { + if self.abilities != abilities { + self.abilities = abilities; + self.rewrap(font); + } + } + /// Whether a message from `source` would show (`visibleMessagesFilter`). + pub fn shows(&self, source: ChatSource) -> bool { + self.abilities.shows(source) + } pub fn clear(&mut self) { self.messages.clear(); self.lines.clear(); diff --git a/crates/horizon-client/src/chat_tests.rs b/crates/horizon-client/src/chat_tests.rs index cd5860bfb..6b058c0fb 100644 --- a/crates/horizon-client/src/chat_tests.rs +++ b/crates/horizon-client/src/chat_tests.rs @@ -412,3 +412,27 @@ fn restricted_input_says_why_it_will_not_send() { .any(|line| line == "chat_screen.commands_not_allowed") ); } + +/// `ChatComponent.setVisibleMessageFilter`: a message the Chat option hides +/// on arrival is dropped; one already in the chat leaves it while the option +/// hides its kind and comes back when it shows it again. +#[test] +fn the_chat_option_filters_the_lines_both_ways() { + let font = font(); + let mut chat = Chat::default(); + let says = |chat: &mut Chat, text: &str, source| { + chat.add_message(&font, Text::literal(text), None, source, 0, false); + }; + let shown = + |chat: &Chat| -> Vec { chat.lines.iter().map(|l| l.content.plain()).collect() }; + says(&mut chat, "player", ChatSource::Player); + says(&mut chat, "server", ChatSource::Server); + chat.set_abilities(&font, ChatAbilities::new(ChatVisibility::System)); + assert_eq!(shown(&chat), ["server"]); + says(&mut chat, "dropped", ChatSource::Player); + chat.set_abilities(&font, ChatAbilities::new(ChatVisibility::Hidden)); + says(&mut chat, "client", ChatSource::Client); + assert_eq!(shown(&chat), ["client"]); + chat.set_abilities(&font, ChatAbilities::new(ChatVisibility::Full)); + assert_eq!(shown(&chat), ["client", "server", "player"]); +} diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index f05ee41e4..2f337d16b 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -9136,6 +9136,9 @@ impl Game<'_> { ) } fn apply_settings(&mut self) { + // LocalPlayer.refreshChatAbilities: the chat shows what the Chat option allows. + let abilities = self.chat_abilities(); + self.ui.chat.set_abilities(&self.gui.atlas.font, abilities); if let Some(r) = &self.renderer { self.settings .clamp_gui_scale([r.config.width as i32, r.config.height as i32]); diff --git a/crates/horizon-client/src/game/chat.rs b/crates/horizon-client/src/game/chat.rs index fafe88f26..1319b1192 100644 --- a/crates/horizon-client/src/game/chat.rs +++ b/crates/horizon-client/src/game/chat.rs @@ -463,9 +463,8 @@ impl Game<'_> { self.chat_line(text, ChatSource::Server); return; } - // ChatListener.handleSystemMessage: the Chat option's Hidden level - // takes the server's messages too. - if !self.chat_abilities().receive_system_messages { + // The Chat option's Hidden level takes the server's messages too. + if !self.ui.chat.shows(ChatSource::Server) { return; } let focused = self.screen == Screen::Chat; @@ -474,6 +473,7 @@ impl Game<'_> { &self.gui.atlas, text, Some(SYSTEM_INDICATOR), + ChatSource::Server, self.ticks, focused, ); @@ -610,9 +610,14 @@ impl Game<'_> { ); let focused = self.screen == Screen::Chat; let heard = self.mods.running().then(|| line.plain()); - self.ui - .chat - .add(&self.gui.atlas, line, None, self.ticks, focused); + self.ui.chat.add( + &self.gui.atlas, + line, + None, + ChatSource::Player, + self.ticks, + focused, + ); if let Some(line) = heard { self.mods_chat(&line); } @@ -916,9 +921,14 @@ impl Game<'_> { Err(e) => Text::translate("screenshot.failure", &[Text::literal(e.clone())]), }; let focused = self.screen == Screen::Chat; - self.ui - .chat - .add(&self.gui.atlas, message, None, self.ticks, focused); + self.ui.chat.add( + &self.gui.atlas, + message, + None, + ChatSource::Client, + self.ticks, + focused, + ); } } /// `yyyy-MM-dd_HH.mm.ss[_n].png` (Util.getFilenameFormattedDateTime, local time diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index b84f13e4a..41a4b7a15 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -2836,9 +2836,14 @@ impl Game<'_> { capture.push(line.clone()); } let focused = self.screen == Screen::Chat; - self.ui - .chat - .add(&self.gui.atlas, line, None, self.ticks, focused); + self.ui.chat.add( + &self.gui.atlas, + line, + None, + horizon_client::chat::ChatSource::Player, + self.ticks, + focused, + ); Ok(1) } /// MessageArgument: `@p`-style selectors in a message become names. diff --git a/crates/horizon-client/src/game/lan_screen.rs b/crates/horizon-client/src/game/lan_screen.rs index 335b5fbae..0f2bec30d 100644 --- a/crates/horizon-client/src/game/lan_screen.rs +++ b/crates/horizon-client/src/game/lan_screen.rs @@ -388,7 +388,10 @@ impl Restrictions { 1 => Some("chat_restriction.chat_disabled_by_options"), _ => None, }; - let (chat, commands) = (chat_visibility == 0, chat_visibility < 2); + let abilities = horizon_client::chat::ChatAbilities::new( + horizon_client::chat::ChatVisibility::from_option(chat_visibility), + ); + let (chat, commands) = (abilities.send_messages, abilities.send_commands); // The body's widgets: text blocks (lines, top) and the action's top. let mut blocks = Vec::new(); let mut action = None; diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 80adb5ff6..a50fdaa61 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -798,20 +798,14 @@ impl Game<'_> { Some(stack) } /// A line in the connection's chat, with its styles, click and hover - /// events (ChatComponent.addMessage). `ChatListener` drops the lines the - /// Chat option hides before they reach the chat. + /// events (ChatComponent.addMessage). The lines the Chat option hides + /// are dropped there (its `visibleMessageFilter`). pub(super) fn chat_line( &mut self, content: horizon_client::text::Text, source: horizon_client::chat::ChatSource, ) { - let abilities = self.chat_abilities(); - let allowed = match source { - horizon_client::chat::ChatSource::Player => abilities.receive_player_messages, - horizon_client::chat::ChatSource::Server => abilities.receive_system_messages, - horizon_client::chat::ChatSource::Client => true, - }; - if !allowed { + if !self.ui.chat.shows(source) { return; } let text = content.plain(); @@ -820,6 +814,7 @@ impl Game<'_> { &self.gui.atlas, content, None, + source, self.ticks, self.screen == Screen::Chat, ); From 3adff150cf607a90675892ebe5559dee60a55669 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 25 Oct 2026 23:15:40 +0000 Subject: [PATCH 206/238] Record preparation validation timings and measurement limits Co-Authored-By: Codex --- docs/launcher-preparation.md | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/docs/launcher-preparation.md b/docs/launcher-preparation.md index 7df5b9131..5381106a1 100644 --- a/docs/launcher-preparation.md +++ b/docs/launcher-preparation.md @@ -45,9 +45,17 @@ for every file stable across the two isolated runs. For the three known noisy JS it also requires semantic equality after normalizing object key order, opaque JVM lambda addresses and creative-tab tag set order. Other variation fails the release check. -On Linux with Java 25, two comparisons produced 23,204 matching resource files each. -The first isolated runs took 178.95 s and 104.25 s, versus 45.05 s batched; the second took -100.03 s and 176.06 s, versus 120.09 s batched. All used precompiled exporters. These are fresh output directories with a warm OS file cache, on a -machine with other workloads; the spread between isolated runs prevents treating this as a -controlled speedup estimate. The compiler saving is separate: release preparation removes -the previously measured 15 compiler invocations (29–52 s). +On Linux with Java 25, the output comparisons used 23,204 resource files per run. +All runs used precompiled exporters: + +| Comparison | Isolated A | Isolated B | Batched | +|---|---:|---:|---:| +| Initial | 178.95 s | 104.25 s | 45.05 s | +| Repeat | 100.03 s | 176.06 s | 120.09 s | +| Final, immutable collision cache | 133.80 s | 123.79 s | 56.19 s | + +These use fresh output directories with a warm OS file cache, on a machine with other +workloads. The first two rows include output hashing; the final row measures preparation +before comparison. The variability prevents treating these as a controlled speedup estimate. +The compiler saving is separate: release preparation removes the previously measured +15 compiler invocations (29–52 s). From ea1c0a4490909d5af7beca6103642034eb463435 Mon Sep 17 00:00:00 2001 From: socksprox Date: Mon, 26 Oct 2026 00:19:29 +0100 Subject: [PATCH 207/238] Render mannequin entities (server NPCs) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Servers on 26.3 spawn NPCs as minecraft:mannequin — the avatar entity vanilla added for exactly this — instead of fake players (gommehd.net's "Gommunity" lobby NPCs are invisible today). The client never registered the type: MobTypes.load only builds species::ALL, so AddEntity produced a tracked entity but nothing to draw. - EntityDataSerializers.RESOLVABLE_PROFILE (41) decodes into Value::Profile. Before, the entry list ended at it and the mannequin's DATA_IMMOVABLE and DATA_DESCRIPTION after it never decoded. - species::ALL gains mannequin (player#main model, Humanoid anim), and the player-only render paths — skin texture, sleep pose, AvatarRenderer's 0.9375 scale — take an avatar() check instead of the id string. - SetEntityData's apply stores DATA_PROFILE on the mob, and update_remote_skins resolves its textures property through PlayerSkinRenderCache's rules (unsigned allowed, SkinManager's insecure path) the way a tab player's skin resolves. - Mannequins get the shared-flags crouch and sleeping-position handling other players already had. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- crates/horizon-client/src/game/remote.rs | 71 +++++++-- .../src/game/remote_mob_data.rs | 23 +++ crates/horizon-client/src/mobs.rs | 4 + crates/horizon-client/src/mobs/render.rs | 13 +- crates/horizon-client/src/mobs/species.rs | 15 +- crates/horizon-protocol/src/entity_data.rs | 143 ++++++++++++++++++ 6 files changed, 255 insertions(+), 14 deletions(-) diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 03b48df3d..d64449f55 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -1906,10 +1906,14 @@ impl Game<'_> { } // LivingEntity.DATA_LIVING_ENTITY_FLAGS: for another player // too, remote_mob_data's apply reads index 8 above. - let other = self - .remote - .as_ref() - .is_some_and(|r| r.level_players.contains_key(&p.id) && r.entity_id != p.id); + let mannequin = self + .mobs + .iter() + .any(|m| m.id == p.id as u32 && &*m.kind.id == "minecraft:mannequin"); + let other = mannequin + || self.remote.as_ref().is_some_and(|r| { + r.level_players.contains_key(&p.id) && r.entity_id != p.id + }); // LivingEntity.onSyncedDataUpdated(SLEEPING_POS_ID): another // player lies down in their bed (drawn lying in it), or gets up. if other { @@ -1960,11 +1964,13 @@ impl Game<'_> { } } // Entity.DATA_SHARED_FLAGS_ID's crouching bit: another - // player's crouch (their model and Entity.isDiscrete's name tag). - if self - .remote - .as_ref() - .is_some_and(|r| r.level_players.contains_key(&p.id)) + // player's or a mannequin's crouch (their model and + // Entity.isDiscrete's name tag). + if (mannequin + || self + .remote + .as_ref() + .is_some_and(|r| r.level_players.contains_key(&p.id))) && let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) { for (index, _, value) in &p.values { @@ -2781,6 +2787,53 @@ impl Game<'_> { { mob.skin = Some(skin); } + // ClientMannequin.updateSkin (PlayerSkinRenderCache.lookup): a + // mannequin's DATA_PROFILE `textures` property resolves like a + // player's, never required to be signed. + let mannequins: Vec<(u32, horizon_client::skins::Textures)> = self + .mobs + .iter() + .filter(|m| &*m.kind.id == "minecraft:mannequin" && m.profile.is_some()) + .map(|m| { + let t = m + .profile + .as_ref() + .and_then(|p| p.properties.iter().find(|p| p.name == "textures")); + let textures = t.map_or_else(Default::default, |t| { + let signature = if t.signature.is_some() { + horizon_client::skins::Signature::Signed + } else { + horizon_client::skins::Signature::Unsigned + }; + horizon_client::skins::unpack( + &t.value, + signature, + &horizon_client::skins::TEXTURE_URIS, + ) + }); + (m.id, textures) + }) + .collect(); + let mut resolved: Vec<(u32, Option)> = Vec::new(); + for (id, textures) in mannequins { + let skin = { + let mut downloads = self.skin_downloads.borrow_mut(); + horizon_client::skins::lookup(&textures, false, |kind, url| { + downloads.state(kind, url) + }) + }; + resolved.push((id, skin)); + } + for (id, skin) in resolved { + let skin = skin.filter(|s| !s.starts_with("skins/") || self.show_skin(s)); + if let Some(mob) = self + .mobs + .iter_mut() + .find(|m| m.id == id && &*m.kind.id == "minecraft:mannequin") + { + mob.skin = skin; + } + } } /// Puts a downloaded skin in an atlas skin slot: a free one, else the /// least recently shown. diff --git a/crates/horizon-client/src/game/remote_mob_data.rs b/crates/horizon-client/src/game/remote_mob_data.rs index da786c1b9..fbfc5a4fb 100644 --- a/crates/horizon-client/src/game/remote_mob_data.rs +++ b/crates/horizon-client/src/game/remote_mob_data.rs @@ -75,6 +75,11 @@ fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin) { // Zombie's three): what VillagerProfessionLayer draws. (Species::Villager, 19, Value::Ids(data)) | (Species::ZombieVillager, 20, Value::Ids(data)) => villager_data(m, data, builtin), + // Mannequin.DATA_PROFILE (after Avatar's two): whose skin it + // wears; update_remote_skins resolves it like a tab player's. + (Species::Mannequin, 17, Value::Profile(profile)) => { + m.profile = Some(profile.clone()); + } _ => {} } } @@ -295,4 +300,22 @@ mod tests { apply(&mut e, &[(17, s::BYTE, Value::Byte(2))]); assert_eq!(e.extra.variant2, 2); } + + #[test] + fn a_mannequin_exists_and_takes_its_profile() { + let Some(mut m) = mob("minecraft:mannequin") else { + return; + }; + assert_eq!(&*m.kind.id, "minecraft:mannequin"); + let profile = horizon_protocol::entity_data::Profile { + name: Some("Gommunity".into()), + uuid: Some(1), + ..Default::default() + }; + apply( + &mut m, + &[(17, s::RESOLVABLE_PROFILE, Value::Profile(profile.clone()))], + ); + assert_eq!(m.profile, Some(profile)); + } } diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 46834e9d1..1b5dddbe7 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -861,6 +861,9 @@ pub struct Mob { /// The skin texture of a player mob (`PlayerInfo.getSkin`), which /// replaces the type's default when its download is ready. pub skin: Option, + /// A mannequin's `DATA_PROFILE` (ResolvableProfile): the profile's + /// `textures` property resolves to `skin` like a player's. + pub profile: Option, } /// Events a mob tick asks the game to act on. #[derive(Clone, Debug)] @@ -1586,6 +1589,7 @@ impl Mob { rider: None, outbox: Vec::new(), skin: None, + profile: None, attrs: Attrs { speed: kind.speed, attack_damage: kind.attack_damage, diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs index cb21cec8c..bbbf26738 100644 --- a/crates/horizon-client/src/mobs/render.rs +++ b/crates/horizon-client/src/mobs/render.rs @@ -92,6 +92,11 @@ fn climate(m: &Mob) -> &'static str { _ => "temperate", } } +/// `AvatarRenderer` entities: a mannequin draws exactly like a player — +/// the player model, its resolved skin and the same scale. +fn avatar(m: &Mob) -> bool { + matches!(&*m.kind.id, "minecraft:player" | "minecraft:mannequin") +} /// The model layer key and texture for the mob's body. pub fn body(m: &Mob) -> (String, String) { use Species as S; @@ -103,7 +108,7 @@ pub fn body(m: &Mob) -> (String, String) { s.to_owned() } }; - if &*m.kind.id == "minecraft:player" { + if avatar(m) { return ( "main".into(), m.skin.clone().unwrap_or_else(|| m.kind.texture.clone()), @@ -334,7 +339,7 @@ pub fn body_color(m: &Mob, alpha: f32) -> u32 { pub fn layers(m: &Mob) -> Vec { use Species as S; let mut v = Vec::new(); - if &*m.kind.id == "minecraft:player" { + if avatar(m) { return v; } let white = 0xFFFF_FFFF; @@ -754,7 +759,7 @@ pub fn base_transform_at(m: &Mob, alpha: f32, origin: glam::Mat4) -> glam::Mat4 body += (f64::from(ticks * 3.25).cos() * std::f64::consts::PI * 0.4) as f32; } // A sleeping villager, or another player lying in their bed. - let sleep = (m.species() == Species::Villager || m.kind.id == "minecraft:player") + let sleep = (m.species() == Species::Villager || avatar(m)) .then(|| super::villager::sleep_pose(m)) .flatten(); let rotation = match sleep { @@ -873,7 +878,7 @@ fn red_overlay(m: &Mob) -> bool { m.hurt_time > 0 || (m.death_time > 0 && !super::creaking::tearing_down(m)) } fn species_scale(m: &Mob, alpha: f32) -> glam::Mat4 { - if m.kind.id == "minecraft:player" { + if avatar(m) { // AvatarRenderer.scale, shared by the inventory and world renderer. return glam::Mat4::from_scale(glam::Vec3::splat(0.9375)); } diff --git a/crates/horizon-client/src/mobs/species.rs b/crates/horizon-client/src/mobs/species.rs index 28ea729b3..f48a21fe9 100644 --- a/crates/horizon-client/src/mobs/species.rs +++ b/crates/horizon-client/src/mobs/species.rs @@ -94,6 +94,7 @@ pub enum Species { Nautilus, ZombieNautilus, SulfurCube, + Mannequin, } /// Baby bounding boxes (each class's BABY_DIMENSIONS). #[derive(Clone, Copy, Debug)] @@ -1307,6 +1308,18 @@ pub const ALL: &[Def] = &[ None, Baby::None ), + // Mannequin: the player model; the DATA_PROFILE ResolvableProfile picks + // its skin, this texture only while that has none. + def!( + Mannequin, + "mannequin", + "player#main", + "entity/player/wide/steve", + A::Humanoid { zombie_arms: false }, + ["", "entity.generic.hurt", "entity.generic.death"], + None, + Baby::None + ), ]; pub fn def(s: Species) -> &'static Def { ALL.iter().find(|d| d.species == s).unwrap_or(&ALL[0]) @@ -1488,7 +1501,7 @@ impl Def { // Nautilus.handleAirSupply: it dries out on land. needs_water: aquatic || s == S::Nautilus, water_sensitive: matches!(s, S::Enderman | S::Blaze | S::SnowGolem), - animal: !matches!(s, S::Bat | S::SulfurCube) + animal: !matches!(s, S::Bat | S::SulfurCube | S::Mannequin) && !self.hostile_class() && !aquatic && !matches!( diff --git a/crates/horizon-protocol/src/entity_data.rs b/crates/horizon-protocol/src/entity_data.rs index 574afd300..47467de4a 100644 --- a/crates/horizon-protocol/src/entity_data.rs +++ b/crates/horizon-protocol/src/entity_data.rs @@ -34,6 +34,98 @@ pub enum Value { Particle(Particle), /// A particle list (PARTICLES: a VarInt count, then each particle). Particles(Vec), + /// A `ResolvableProfile` (a mannequin's DATA_PROFILE). + Profile(Profile), +} +/// `ResolvableProfile.STREAM_CODEC`: the GameProfile a mannequin or player +/// head resolves — either a full profile (id, name and properties on the +/// wire) or a partial one (a name and/or id to look up) — followed by a +/// `PlayerSkin.Patch` of texture overrides. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Profile { + pub name: Option, + pub uuid: Option, + /// `PropertyMap`: its `textures` property carries the skin. + pub properties: Vec, + /// `PlayerSkin.Patch`: texture asset ids overriding the profile's. + pub body: Option, + pub cape: Option, + pub elytra: Option, + /// `PlayerModelType`: true is the slim (4px arms) model. + pub slim: Option, +} +/// `ByteBufCodecs.GAME_PROFILE_PROPERTIES`. +fn read_properties(r: &mut Reader) -> Result> { + use crate::buf::Codec; + let n = r.count(16)?; + (0..n) + .map(|_| crate::packets::login::Property::read(r)) + .collect() +} +fn write_properties(p: &[crate::packets::login::Property], w: &mut Writer) { + use crate::buf::Codec; + w.varint(p.len() as i32); + p.iter() + .for_each(|p| crate::packets::login::Property::write(p, w)); +} +fn read_profile(r: &mut Reader) -> Result { + let mut p = Profile::default(); + // ByteBufCodecs.either: true is the full GameProfile, false a Partial. + if r.bool()? { + p.uuid = Some(r.uuid()?); + p.name = Some(r.string(16)?); + p.properties = read_properties(r)?; + } else { + if r.bool()? { + p.name = Some(r.string(16)?); + } + if r.bool()? { + p.uuid = Some(r.uuid()?); + } + p.properties = read_properties(r)?; + } + // PlayerSkin.Patch: three optional texture ids, then the model type. + if r.bool()? { + p.body = Some(r.string(32767)?); + } + if r.bool()? { + p.cape = Some(r.string(32767)?); + } + if r.bool()? { + p.elytra = Some(r.string(32767)?); + } + if r.bool()? { + p.slim = Some(r.bool()?); + } + Ok(p) +} +fn write_profile(p: &Profile, w: &mut Writer) { + let full = p.uuid.is_some() && p.name.is_some(); + w.bool(full); + if full { + w.uuid(p.uuid.unwrap_or(0)); + w.string(p.name.as_deref().unwrap_or("")); + } else { + w.bool(p.name.is_some()); + if let Some(name) = &p.name { + w.string(name); + } + w.bool(p.uuid.is_some()); + if let Some(uuid) = p.uuid { + w.uuid(uuid); + } + } + write_properties(&p.properties, w); + for texture in [&p.body, &p.cape, &p.elytra] { + w.bool(texture.is_some()); + if let Some(texture) = texture { + w.string(texture); + } + } + w.bool(p.slim.is_some()); + if let Some(slim) = p.slim { + w.bool(slim); + } } /// One `ParticleTypes.STREAM_CODEC` value: the `minecraft:particle_type` id, /// then the data its type carries (26.3 registry order). @@ -258,6 +350,7 @@ fn read_value(kind: i32, r: &mut Reader) -> Result> { | HUMANOID_ARM | DYE_COLOR => Value::Id(r.varint()?), PARTICLE => Value::Particle(Particle::read(r)?), + RESOLVABLE_PROFILE => Value::Profile(read_profile(r)?), PARTICLES => { let n = r.count(65536)?; let mut list = Vec::with_capacity(n.min(64)); @@ -305,6 +398,7 @@ fn write_value(kind: i32, v: &Value, w: &mut Writer) { w.varint(list.len() as i32); list.iter().for_each(|p| p.write(w)); } + Value::Profile(p) => write_profile(p, w), } let _ = kind; } @@ -455,4 +549,53 @@ mod tests { ] ); } + + /// RESOLVABLE_PROFILE (a mannequin's DATA_PROFILE): both ResolvableProfile + /// contents round-trip, and an entry after it still decodes. + #[test] + fn resolvable_profile_round_trips() { + use crate::packets::login::Property; + let full = Profile { + name: Some("Gommunity".into()), + uuid: Some(0x0123_4567_89AB_CDEF_0123_4567_89AB_CDEF), + properties: vec![Property { + name: "textures".into(), + value: "e30=".into(), + signature: Some("sig".into()), + }], + body: Some("minecraft:entity/player/wide/steve".into()), + cape: None, + elytra: None, + slim: Some(false), + }; + let partial = Profile { + name: Some("Gommunity".into()), + ..Profile::default() + }; + let mut w = Writer::new(); + EntityData::write( + &vec![ + ( + 17, + serializer::RESOLVABLE_PROFILE, + Value::Profile(full.clone()), + ), + ( + 17, + serializer::RESOLVABLE_PROFILE, + Value::Profile(partial.clone()), + ), + (18, serializer::BOOLEAN, Value::Bool(true)), + ], + &mut w, + ); + assert_eq!( + EntityData::read(&mut Reader::new(&w.data)).unwrap(), + vec![ + (17, serializer::RESOLVABLE_PROFILE, Value::Profile(full)), + (17, serializer::RESOLVABLE_PROFILE, Value::Profile(partial)), + (18, serializer::BOOLEAN, Value::Bool(true)), + ] + ); + } } From ef5361e4c25c223c6607af9efad9ca0d9348978d Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:26:17 +0100 Subject: [PATCH 208/238] Creatures: drop the doc comments the removed cooldown fields left behind Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/creatures.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 9b2044d62..addb24d78 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -43,8 +43,6 @@ fn arrow_damage(arrow: &mut Projectile, fallback: &mut impl RandomSource) -> f32 #[derive(Default)] pub(super) struct Creatures { pub projectiles: Vec, - /// Player status effects applied by mobs: (effect, ticks left, amplifier). - /// The wind charge's use cooldown (ticks). /// Wind bursts from a guest's turn, waiting for it to end (game/wind.rs). pub wind_bursts: Vec, /// Ticks left of the elder guardian's apparition (MobAppearanceParticle). @@ -1559,7 +1557,6 @@ impl Game<'_> { id, ticks, amplifier, )); } - /// Burning applied to the player by mobs. pub(super) fn hurt_player_by(&mut self, damage_type: &str, amount: f32) { if self.creative { return; From 0baf6cbd62e5dc053732354b5ebd356aae3ffd83 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:27:22 +0100 Subject: [PATCH 209/238] CI: the desktop runner works while its screen is locked or untouched for 15 min MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ludovic's rule (2026-10-26): runner_pause.py pauses at once on keyboard, mouse or gamepad input (read from /dev/input, only whether bytes came), on an unlocked session touched in the last 15 min, at 90 °C, on a game or the GPU. Fixes the service start: the link loop's last test failed the script under set -e. Co-Authored-By: Claude Opus 5.5 --- docs/parity-workflow.md | 2 +- docs/release-flow.md | 32 +++++++------- tests/test_runner_pause.py | 13 +++++- tools/ci/linux-runner.sh | 12 +++--- tools/ci/runner_pause.py | 88 ++++++++++++++++++++++++++++++++------ tools/parity_hosts.json | 2 +- 6 files changed, 113 insertions(+), 36 deletions(-) diff --git a/docs/parity-workflow.md b/docs/parity-workflow.md index 80426fa59..7eecdd8bb 100644 --- a/docs/parity-workflow.md +++ b/docs/parity-workflow.md @@ -38,7 +38,7 @@ The game logs into `HORIZON_HOME` and falls back to the player's own game folder ### Other machines -Harnesses can also run on the Mac mini, the ThinkPad and the desktop (`tools/parity_hosts.json` describes them; `desktop` uses its CI runner's environment and vanilla data, and refuses work, or stops it, while someone is at it: docs/release-flow.md, "The desktop's runner"; its `default` list is empty, so pass `--hosts macmini,thinkpad` or set `PARITY_HOSTS`, after copying `shared-local/` to `~/code/horizon-parity/shared-local` on each host). The runner mirrors the worktree there with rsync (not `.git`, `.local`, `target` or ignored files) into `~/code/horizon-parity/remote/`, whose `.local` links to the vanilla data in `~/code/horizon-parity/shared-local`, and starts `tools/parity.py --worker` over ssh. The worker builds the probes (about 80 s the first time) and runs what it is sent under the same tracer, admitting harnesses against its own free memory. One queue, longest first, feeds every machine, remote ones first: the Mac takes up to 14 harnesses, the desktop 3 and only those under 800 MB. Each pass comes back with the content hash of every file it depended on, so it is keyed on exactly what ran, and logs come back to `.local/parity-runner/logs/`; state, sharing and `gate.py` work as before. If a host can't be reached, or drops out, its harnesses run here. +Harnesses can also run on the Mac mini, the ThinkPad and the desktop (`tools/parity_hosts.json` describes them; `desktop` uses its CI runner's environment and vanilla data, and refuses work, or stops it, while someone uses it: docs/release-flow.md, "The desktop's runner"; its `default` list is empty, so pass `--hosts macmini,thinkpad` or set `PARITY_HOSTS`, after copying `shared-local/` to `~/code/horizon-parity/shared-local` on each host). The runner mirrors the worktree there with rsync (not `.git`, `.local`, `target` or ignored files) into `~/code/horizon-parity/remote/`, whose `.local` links to the vanilla data in `~/code/horizon-parity/shared-local`, and starts `tools/parity.py --worker` over ssh. The worker builds the probes (about 80 s the first time) and runs what it is sent under the same tracer, admitting harnesses against its own free memory. One queue, longest first, feeds every machine, remote ones first: the Mac takes up to 14 harnesses, the desktop 8. Each pass comes back with the content hash of every file it depended on, so it is keyed on exactly what ran, and logs come back to `.local/parity-runner/logs/`; state, sharing and `gate.py` work as before. If a host can't be reached, or drops out, its harnesses run here. Harnesses a host can't run are listed in the file: `local_only` (`save_roundtrip` runs the game on a virtual screen) and each host's `exclude`. The Mac excludes nothing: vanilla's `Math.sin`, `pow`, `log` and the like don't round the same on an Apple silicon Mac as on Linux (HotSpot uses fdlibm there, Intel's stubs on x86-64), and Rust's float functions round like neither (they call the platform libm), so code that mirrors a vanilla `Math` call uses `horizon_core::jmath`, which computes what HotSpot computes on the host architecture. The ThinkPad helps too, gently: Linux, 2 harnesses at `nice 19`, since it hosts the Claude account pool; it finds ALSA through `~/code/horizon-parity/alsa-dev` like the desktop. diff --git a/docs/release-flow.md b/docs/release-flow.md index e72bebc73..d9d1801aa 100644 --- a/docs/release-flow.md +++ b/docs/release-flow.md @@ -68,21 +68,23 @@ release's macOS build back. Each keeps its own checkout, `target/` and Rust inst time, under a lock) and the parity passes and slots. **The desktop's runner.** `ludodesktop-horizon` (labels `self-hosted`, `Linux`, `X64`, `ludodesktop`, and -`horizon-game` once a full trial has passed there) runs on the maintainer's Linux desktop, only while nobody is -at it. `tools/ci/linux-runner.sh install`, run there as him, registers it and installs two systemd user services: -`horizon-runner` (the runner, in `~/.actions-runners/horizon`, nice 19, in `horizon-ci.slice` with CPU and I/O -weight 20 and memory capped at two thirds of the RAM; `CARGO_BUILD_JOBS` and the harness slots at 8, after the -machine froze under ~7 cold cargo builds on 2026-10-14; its own Rust install; the JDK, sccache and the build -libraries from nixpkgs; its own `vscreen` so no game it starts reaches his screen) and `horizon-runner-pause` -(`tools/ci/runner_pause.py`), the only thing that starts the runner. The watcher stops it while a graphical -session is open on the machine, locked or not (`~/fleet/power status` says "IN USE"), while a game runs (Steam, -gamescope, Wine, Proton or Minecraft processes, fleet-load's own detection, or a process outside the CI slice -using the GPU at 50 % or more), or while `~/.actions-runners/horizon/pause` exists, and starts it again 5 minutes -after the last such sign. A job already under way is throttled to 2 threads at the lowest weight and the runner -stops after it: freezing the job would make GitHub drop it as lost and the harnesses time out. Old pull requests' -`runner-env.sh` know only the Mac: the runner's `.env` and `.path` give their jobs Python, the JDK and the -vanilla data (`HORIZON_SHARED_SOURCE`) all the same. The nightly audit stays on the Mac mini's runners, so its -comparison with the previous night is always made on one platform. +`horizon-game` once a full trial has passed there) runs on the maintainer's Linux desktop while nobody uses it: its +screen locked, or untouched for 15 minutes (his rule, 2026-10-26). `tools/ci/linux-runner.sh install`, run there as +him, registers it and installs two systemd user services: `horizon-runner` (the runner, in +`~/.actions-runners/horizon`, nice 19, in `horizon-ci.slice` with CPU and I/O weight 20 and memory capped at two +thirds of the RAM; `CARGO_BUILD_JOBS` and the harness slots at 8, after the machine froze under ~7 cold cargo +builds on 2026-10-14; its own Rust install; the JDK, sccache and the build libraries from nixpkgs; its own +`vscreen` so no game it starts reaches his screen) and `horizon-runner-pause` (`tools/ci/runner_pause.py`), the +only thing that starts the runner. The watcher stops it at once on keyboard, mouse or gamepad input (read from +`/dev/input`: only whether bytes came, never what), while an unlocked session has had input in the last 15 minutes, +while the CPU package is at 90 °C or more, while a game runs (Steam, gamescope, Wine, Proton or Minecraft +processes, fleet-load's own detection, or a process outside the CI slice using the GPU at 50 % or more), or while +`~/.actions-runners/horizon/pause` exists, and starts it again 5 minutes after the last such sign. A job already +under way is throttled to 2 threads at the lowest weight and the runner stops after it: freezing the job would make +GitHub drop it as lost and the harnesses time out. Old pull requests' `runner-env.sh` know only the Mac: the +runner's `.env` and `.path` give their jobs Python, the JDK and the vanilla data (`HORIZON_SHARED_SOURCE`) all the +same. The nightly audit stays on the Mac mini's runners, so its comparison with the previous night is always made +on one platform. ## Merging diff --git a/tests/test_runner_pause.py b/tests/test_runner_pause.py index c6df08b82..38c4affd8 100644 --- a/tests/test_runner_pause.py +++ b/tests/test_runner_pause.py @@ -18,7 +18,7 @@ class Machine: self.throttles = [] # (clock, on) self.calls = [] - def reason(self, hot): + def reason(self, hot, keys): return next((reason for start, end, reason in self.signs if start <= self.clock < end), None) def systemctl(self, *arguments): @@ -77,6 +77,17 @@ class RunnerPauseTests(unittest.TestCase): self.assertLess(stop, 1215) self.assertEqual(machine.throttles[-1], (1290 + 300, False)) + def test_the_desktop_is_free_when_locked_or_untouched_for_15_minutes(self): + cases = [ # (session, seconds since the last input, someone there) + (("3", False), 30, True), (("3", True), 30, True), # any input pauses at once, even on the lock screen + (("3", False), 120, True), (("3", False), 1000, False), # unlocked: free after 15 min untouched + (("3", True), 120, False), (None, 120, False), # locked, or nobody logged in + ] + for session, idle, there in cases: + with self.subTest(session=session, idle=idle), \ + mock.patch.object(runner_pause, "seat_session", lambda: session): + self.assertEqual(runner_pause.at_the_screen(idle) is not None, there) + if __name__ == "__main__": unittest.main() diff --git a/tools/ci/linux-runner.sh b/tools/ci/linux-runner.sh index adc2b0130..1bd587e79 100755 --- a/tools/ci/linux-runner.sh +++ b/tools/ci/linux-runner.sh @@ -9,10 +9,10 @@ # # Everything lives in ~/.actions-runners/horizon (never in ~/code): the runner, its own rustup and cargo homes, # the work folder, and copies of this script and runner_pause.py taken at install time (a pull request's checkout -# never runs as the service). horizon-runner-pause.service (runner_pause.py) starts the runner only while nobody is -# at the screen or playing, and stops it otherwise. The runner runs nice, in horizon-ci.slice (low CPU and I/O weight, memory capped), -# on its own virtual screen: vscreen (a headless sway) where it is installed, else Xvfb from nixpkgs with Mesa's -# Vulkan drivers. +# never runs as the service). horizon-runner-pause.service (runner_pause.py) starts the runner only while nobody +# uses the machine (screen locked or untouched for 15 min, no game), and stops it otherwise. The runner runs nice, +# in horizon-ci.slice (low CPU and I/O weight, memory capped), on its own virtual screen: vscreen (a headless sway) +# where it is installed, else Xvfb from nixpkgs with Mesa's Vulkan drivers. # HORIZON_SHARED_SOURCE: the imported vanilla data (default ~/horizon-local/shared-local, else # ~/code/horizon/.local); RUNNER_JOBS: cargo jobs and harnesses at once (default a third of the threads, at most 8: # ludodesktop froze under ~7 cold cargo builds on 2026-10-14, and it is the maintainer's own machine). @@ -40,7 +40,7 @@ parity_host_links() { [ -d "$checkout" ] || return 0 mkdir -p "$shared" for entry in "$checkout"/*; do - [ -L "$entry" ] && ln -sfn "$entry" "$shared/$(basename "$entry")" + if [ -L "$entry" ]; then ln -sfn "$entry" "$shared/$(basename "$entry")"; fi done } @@ -151,7 +151,7 @@ RestartSec=30 EOF cat > "$units/horizon-runner-pause.service" <= GPU_BUSY %, default 50, in two @@ -34,6 +36,9 @@ GPU_BUSY = int(os.environ.get("GPU_BUSY", 50)) INTERVAL = int(os.environ.get("INTERVAL", 15)) RESUME_AFTER = int(os.environ.get("RESUME_AFTER", 300)) THROTTLE_CPUS = int(os.environ.get("THROTTLE_CPUS", 2)) +INPUT_PAUSE = int(os.environ.get("INPUT_PAUSE", 60)) +IDLE_AFTER = int(os.environ.get("IDLE_AFTER", 900)) +TEMP_LIMIT = int(os.environ.get("TEMP_LIMIT", 90)) def log(text): @@ -53,13 +58,71 @@ def ci_process(pid): return True # gone -def at_the_screen(): - """A graphical login on seat0, as ~/fleet/power status counts it (a locked screen counts too).""" +class Input: + """Keyboard, mouse and gamepad activity, from /dev/input (the input group): only whether bytes came, never what.""" + + def __init__(self): + self.devices = {} # path -> file descriptor + self.last = time.monotonic() # a start counts as input: nothing is known yet + + def poll(self): + try: + text = Path("/proc/bus/input/devices").read_text() + except OSError: + text = "" + wanted = {f"/dev/input/{handler}" for block in text.split("\n\n") for line in block.splitlines() + if line.startswith("H: Handlers=") and re.search(r"\b(kbd|mouse\d*|js\d*)\b", line) + for handler in line.split("=", 1)[1].split() if handler.startswith("event")} + for path in set(self.devices) - wanted: + os.close(self.devices.pop(path)) + for path in wanted - set(self.devices): + try: + self.devices[path] = os.open(path, os.O_RDONLY | os.O_NONBLOCK) + except OSError: + pass + for path, descriptor in list(self.devices.items()): + try: + while os.read(descriptor, 4096): + self.last = time.monotonic() + except BlockingIOError: + pass + except OSError: # unplugged + os.close(self.devices.pop(path)) + return time.monotonic() - self.last + + +def seat_session(): + """The graphical session on seat0 and whether its screen is locked, or None.""" sessions = subprocess.run(["loginctl", "list-sessions", "--no-legend"], capture_output=True, text=True).stdout for line in sessions.splitlines(): fields = line.split() if "seat0" in fields: - return f"session {fields[0]} of {fields[2] if len(fields) > 2 else '?'} at the screen (seat0)" + locked = subprocess.run(["loginctl", "show-session", fields[0], "-p", "LockedHint", "--value"], + capture_output=True, text=True).stdout.strip() == "yes" + return fields[0], locked + return None + + +def at_the_screen(input_idle): + """Someone is at the machine: input in the last INPUT_PAUSE seconds, or an unlocked session with input in the last + IDLE_AFTER seconds. A locked screen, or one nobody touched for IDLE_AFTER, leaves the machine to CI.""" + if input_idle < INPUT_PAUSE: + return f"input {input_idle:.0f} s ago" + session = seat_session() + if session and not session[1] and input_idle < IDLE_AFTER: + return f"session {session[0]} unlocked, input {input_idle:.0f} s ago" + return None + + +def too_hot(): + """The CPU package above TEMP_LIMIT °C (coretemp or k10temp).""" + for monitor in Path("/sys/class/hwmon").glob("hwmon*"): + try: + if (monitor / "name").read_text().strip() in ("coretemp", "k10temp"): + celsius = int((monitor / "temp1_input").read_text()) / 1000 + return f"CPU at {celsius:.0f} °C" if celsius >= TEMP_LIMIT else None + except (OSError, ValueError): + pass return None @@ -133,11 +196,12 @@ def throttle(on): systemctl("set-property", "--runtime", SLICE, "CPUWeight=20", "IOWeight=20", "CPUQuota=") -def pause_reason(hot): - """Why the runner may not work now, or None. hot: the GPU users seen busy at the last sample (updated).""" +def pause_reason(hot, keys): + """Why the runner may not work now, or None. hot: the GPU users seen busy at the last sample (updated); + keys: the Input watcher.""" if MANUAL.exists(): return f"paused by hand ({MANUAL})" - reason = at_the_screen() or fleet_load_gaming() or game_process() + reason = at_the_screen(keys.poll()) or too_hot() or fleet_load_gaming() or game_process() if reason: return reason busy = {pid for pid, sm in gpu_users().items() if sm >= GPU_BUSY} @@ -154,13 +218,13 @@ def pause_reason(hot): def main(): - hot = {} + hot, keys = {}, Input() # A boot (or a restart of this watcher) counts as a sign: the runner starts RESUME_AFTER seconds later at the # earliest, which leaves time to log in after switching the machine on. last_sign = time.monotonic() paused = None while True: - reason = pause_reason(hot) + reason = pause_reason(hot, keys) now = time.monotonic() if reason: last_sign = now diff --git a/tools/parity_hosts.json b/tools/parity_hosts.json index 5d561175d..f2ba65c68 100644 --- a/tools/parity_hosts.json +++ b/tools/parity_hosts.json @@ -18,7 +18,7 @@ "setup": "test ! -e ~/.actions-runners/horizon/paused && set -a && . ~/.actions-runners/horizon/.env && set +a && export PATH=$(cat ~/.actions-runners/horizon/.path) && eval \"$(vscreen env)\"", "jobs": 8, "nice": 19, - "note": "ludodesktop, the maintainer's own machine: only while nobody is at the screen (tools/ci/runner_pause.py: a paused desktop refuses workers and stops the running ones, whose harnesses then run here); 8 jobs at most, in the CI runner's environment and slice" + "note": "ludodesktop, the maintainer's own machine: only while nobody uses it (tools/ci/runner_pause.py: screen locked or untouched for 15 min, no game; a paused desktop refuses workers and stops the running ones, whose harnesses then run here); 8 jobs at most, in the CI runner's environment and slice" }, "thinkpad": { "ssh": "thinkpad", From 836c1ee3b999823ab5d0db767ffbb6b71db4ab1e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:27:32 +0100 Subject: [PATCH 210/238] CI parity: pick harnesses with main's selection code, run them at the head The parity job checks out the PR head, so every branch made before #1964 still selected with the old per-crate rule. The step now runs main's tools/parity.py and tools/parity_reach.py with --changed --list, restores the head's files, and runs the named harnesses with the head's parity.py. A branch that edits either file, or a failure of main's selection, falls back to the head's own `--changed` run. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/parity.yml | 32 +++++++++++++++++++++++++++++++- docs/release-flow.md | 3 +++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/.github/workflows/parity.yml b/.github/workflows/parity.yml index a18fc5019..be87bc965 100644 --- a/.github/workflows/parity.yml +++ b/.github/workflows/parity.yml @@ -43,7 +43,37 @@ jobs: env: PARITY_HOSTS: '' BASE_SHA: ${{ inputs.base }} - run: python3 tools/parity.py --changed "$BASE_SHA" --timeout 2400 # world_spawn takes ~1480 s on a busy runner + run: | + # main's selection (tools/parity.py --changed --list with main's parity.py and parity_reach.py), so a + # branch made before a better selection landed gets it too; the head's own runner then runs those + # harnesses. A branch that changes the selection itself selects with its own code. + set -euo pipefail + own() { python3 tools/parity.py --changed "$BASE_SHA" --timeout 2400; } # world_spawn takes ~1480 s on a busy runner + fork_point=$(git merge-base "$BASE_SHA" HEAD) + if ! git rev-parse --verify -q origin/main >/dev/null || ! git diff --quiet "$fork_point" HEAD -- tools/parity.py tools/parity_reach.py; then + own + exit + fi + had_reach=$(git ls-files tools/parity_reach.py) + git show origin/main:tools/parity.py > tools/parity.py + git show origin/main:tools/parity_reach.py > tools/parity_reach.py + status=0 + python3 tools/parity.py --changed "$BASE_SHA" --list > .local/parity-selection.txt || status=$? + git checkout -- tools/parity.py + if [ -n "$had_reach" ]; then git checkout -- tools/parity_reach.py; else rm -f tools/parity_reach.py; fi + cat .local/parity-selection.txt + if [ "$status" != 0 ]; then + echo "main's selection failed: the head's own selection runs" + own + exit + fi + names=$(sed '$d' .local/parity-selection.txt | grep -E '^[A-Za-z0-9_#]+$' || true) + if [ -z "$names" ]; then + echo "Nothing to run." + exit 0 + fi + # shellcheck disable=SC2086 + python3 tools/parity.py $names --timeout 2400 - name: Logs of the failed harnesses if: failure() uses: actions/upload-artifact@v4 diff --git a/docs/release-flow.md b/docs/release-flow.md index 4fb0209d9..3024e0f53 100644 --- a/docs/release-flow.md +++ b/docs/release-flow.md @@ -43,6 +43,9 @@ copy is: the Mac mini. Its runner runs as the maintainer's account and keeps its modules, Java oracles and every Rust source their probes compile) changed since the merge base, plus any harness nothing is recorded about yet, and runs those that have no pass for their current inputs. A change to `Cargo.lock` or `research-lock.json` affects every harness. Each harness has 25 minutes, the job 45. + The selection runs with main's `tools/parity.py` and `tools/parity_reach.py` (`--changed --list`), so a + branch made before a better selection landed gets it too, and the head's own `parity.py` then runs the named + harnesses. A branch that changes either file selects with its own code. Failed harnesses' logs are uploaded as the `parity-logs` artifact. From 0b970c0c9a0eb259652f5bee756073563e471faf Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 25 Oct 2026 23:35:22 +0000 Subject: [PATCH 211/238] Cover instant-break sounds alongside replanting Move the instant_digging regression from redundant PR #1966 into the existing stale-dig fix for #1880 and #1908. Co-Authored-By: Codex (gpt-6.1-sol) --- tools/lan_e2e.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index f0333c87b..11354b970 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -250,6 +250,7 @@ address. The script then checks, from both sides, what a guest's game used to ge - falling: an item and a falling block the host summons high up fall smoothly on the guest, a little further every tick between the server's updates (every 20 ticks), as their own client tick moves them (they used to hang, then jump down once a second, #1568). +- instant_digging: an instant survival break leaves no repeated stone hit sounds or stale server dig (#1908). - replant: flowers and crops replanted after a short harvest click stay planted for both players; the server forgets the completed instant break (#1880). @@ -5398,6 +5399,42 @@ CHECKS += ['digging'] CHECK_FNS.update(digging=check_digging) +def check_instant_digging(host, guest, out): + """#1908: an instant survival START has no STOP. ServerPlayerGameMode.tick + forgets a dig once its block is air; it must neither hit air nor break a replacement. + Exercise the in-frame host and network guest (both are clients with --server-thread). + """ + result = {} + for label, player in (('host', host), ('guest', guest)): + x, y, z = (int(v // 1) for v in pos_of(player)) + cell = (x, y + 1, z + 2) + player('command', command='gamemode survival') + player('command', command='clear @s') + host('command', command=f'fill {x - 1} {y} {z + 1} {x + 1} {y + 3} {z + 4} minecraft:air') + host('command', command=f'setblock {x} {y} {z + 2} minecraft:sand') + host('command', command=f'setblock {cell[0]} {cell[1]} {cell[2]} minecraft:dead_bush') + player.wait(10) + before = heard(player, 'minecraft:block.stone.hit') + player.raw('look-at', x + 0.5, y + 1.4, z + 2.5) + player.raw('mouse', 'left', 'click') + player.wait(20) + broken = host.raw('block', *cell).get('block') + hits = heard(player, 'minecraft:block.stone.hit') - before + host('command', command=f'setblock {cell[0]} {cell[1]} {cell[2]} minecraft:dead_bush') + player.wait(20) + replacement = host.raw('block', *cell).get('block') + result[label] = {'broken': broken, 'stone_hits': hits, 'replacement': replacement} + assert broken == 'minecraft:air', f'{label}: the bush did not break: {result}' + assert hits == 0, f'{label}: hitting air after an instant break: {result}' + assert replacement == 'minecraft:dead_bush', f'{label}: the server kept digging: {result}' + host('command', command=f'setblock {cell[0]} {cell[1]} {cell[2]} minecraft:air') + return result + + +CHECKS += ['instant_digging'] +CHECK_FNS.update(instant_digging=check_instant_digging) + + def check_vehicles(host, guest, out): """The host and the guest each (#1551): - in creative, break a block and have it put back: the server's dig ends with the break From 73da2f04ddd56cb2ca1f480f54ab0aad31478223 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:35:28 +0100 Subject: [PATCH 212/238] CI: parity on main, a batch of merges at a time; releases wait for it parity-main runs the vanilla harnesses on every push to main, one run at a time: merges that land during a run wait in one pending run, which checks every merge since main's last green parity-main (tools/parity_main.py base, then parity.py --changed). A failure is bisected over the batch's first-parent merges, running only the failing harnesses, and the breaking merge's pull request gets a comment. Game releases ship only a commit both checks and parity-main passed on (release_train.py; release.yml also starts when parity-main finishes; a forced release doesn't wait). The per-PR parity job now runs only for a PR that changes a harness, an oracle, a probe or the runner, or carries run-parity. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 34 +++++++- .github/workflows/parity-main.yml | 70 ++++++++++++++++ .github/workflows/release.yml | 10 ++- docs/parity-workflow.md | 2 + docs/release-flow.md | 30 ++++++- tests/test_parity_main.py | 43 ++++++++++ tests/test_release_train.py | 21 +++++ tools/parity_main.py | 129 ++++++++++++++++++++++++++++++ tools/release_train.py | 34 ++++++-- 9 files changed, 356 insertions(+), 17 deletions(-) create mode 100644 .github/workflows/parity-main.yml create mode 100644 tests/test_parity_main.py create mode 100644 tools/parity_main.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 98d5b9093..eff82fa99 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,7 +3,8 @@ name: checks # check main requires, so it never has a condition: a skipped job counts as passed, and every run of this workflow # must report a real verify (a pull request that changes no Rust finishes it in seconds instead). # parity: the vanilla harnesses a pull request's changes can affect, on the Mac mini's runner, which holds the -# maintainer's own Minecraft files (Horizon never downloads the game); .github/workflows/parity.yml. A fork's pull +# maintainer's own Minecraft files (Horizon never downloads the game); .github/workflows/parity.yml. Only for a pull +# request that changes a harness, oracle, probe or the runner: the rest are checked on main by parity-main. A fork's pull # request runs it only when a maintainer adds the run-parity label (.github/workflows/run-parity.yml), so labels # never start this workflow. See docs/release-flow.md. on: @@ -72,8 +73,37 @@ jobs: run: | cargo +stable test --workspace --lib --bins --tests --features horizon-client/mods,horizon-mods/pack cargo +stable test --workspace --doc --features horizon-client/mods,horizon-mods/pack - parity: + # Most pull requests leave the harnesses to parity-main, which runs them on main a batch of merges at a time + # (.github/workflows/parity-main.yml; releases wait for it). A pull request still runs them itself when it changes + # a harness, an oracle, a probe or the runner, or carries the run-parity label (run-parity.yml, when it is added). + parity-scope: if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository + runs-on: ubuntu-latest + outputs: + run: ${{ steps.scope.outputs.run }} + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + fetch-depth: 0 + persist-credentials: false + - id: scope + env: + BASE_SHA: ${{ github.event.pull_request.base.sha }} + LABELS: ${{ join(github.event.pull_request.labels.*.name, ' ') }} + run: | + changed=$(git diff --name-only "$(git merge-base "$BASE_SHA" HEAD)" HEAD) + harness=$(grep -E '^(tools/test_[a-z0-9_]+_parity\.py|tools/(parity[a-z_]*|lab)\.py|tools/parity_hosts\.json|mods/horizon/src/test/|crates/[^/]+/examples/|crates/horizon-client/src/game/[a-z_]*(fixture|harness)\.rs|\.github/workflows/parity[a-z-]*\.yml)' <<< "$changed" || true) + if [ -n "$harness" ] || [[ " $LABELS " == *" run-parity "* ]]; then + echo "run=true" >> "$GITHUB_OUTPUT" + echo "The pull request runs its harnesses: ${harness:-label run-parity}" + else + echo "run=false" >> "$GITHUB_OUTPUT" + echo "No harness, oracle, probe or runner change: parity-main checks this on main" + fi + parity: + needs: parity-scope + if: needs.parity-scope.outputs.run == 'true' uses: ./.github/workflows/parity.yml with: base: ${{ github.event.pull_request.base.sha }} diff --git a/.github/workflows/parity-main.yml b/.github/workflows/parity-main.yml new file mode 100644 index 000000000..1ddfb24bb --- /dev/null +++ b/.github/workflows/parity-main.yml @@ -0,0 +1,70 @@ +name: parity-main +# The vanilla harnesses on main, a batch of merges at a time (docs/release-flow.md#parity-on-main), on the Mac mini's +# runner. One run at a time: merges that land while a run goes on wait in one pending run (GitHub keeps only the +# newest), which checks every merge since main's last green parity-main (tools/parity_main.py base). On a failure it +# bisects the failing harnesses over those merges and comments on the pull request that broke them. Releases ship +# only a commit whose parity-main passed (tools/release_train.py), so nothing a harness catches reaches players. +on: + push: + branches: [main] + workflow_dispatch: +permissions: + contents: read + # The comment on the merge that broke a harness. + pull-requests: write + # tools/parity_main.py base reads earlier runs. + actions: read +concurrency: + group: parity-main + cancel-in-progress: false +env: + # Test runs never reach the telemetry server: no live-count ping, no uploads (docs/telemetry.md). + HORIZON_TELEMETRY_URL: "off" + +jobs: + parity-main: + runs-on: [self-hosted, horizon-game] + timeout-minutes: 240 # a core-crate batch reruns ~300 harnesses (~60-90 min), a bisect a few rounds of the failures + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ github.sha }} + fetch-depth: 0 + persist-credentials: false + # Keeps .local/ (parity state, probe builds, oracle results) and target/ between runs. + clean: false + - uses: dtolnay/rust-toolchain@stable + - name: Tools from the runner + run: bash tools/ci/runner-env.sh + - name: Vanilla data, and resources prepared by this preparer + run: bash tools/ci/vanilla-data.sh + - name: Main's last green parity-main + id: base + env: + GH_TOKEN: ${{ github.token }} + run: echo "sha=$(python3 tools/parity_main.py base)" >> "$GITHUB_OUTPUT" + - name: The harnesses the merges since then can affect + env: + PARITY_HOSTS: '' + BASE_SHA: ${{ steps.base.outputs.sha }} + run: | + echo "Merges since ${BASE_SHA}:" + git log --first-parent --format=' %h %s' "$BASE_SHA..HEAD" + python3 tools/parity.py --changed "$BASE_SHA" --timeout 2400 + - name: The merge that broke them + if: failure() + env: + GH_TOKEN: ${{ github.token }} + PARITY_HOSTS: '' + BASE_SHA: ${{ steps.base.outputs.sha }} + run: | + rm -rf .local/parity-main-logs && cp -R .local/parity-runner/logs .local/parity-main-logs 2>/dev/null || true + python3 tools/parity_main.py bisect "$BASE_SHA" --comment + - name: Logs of the failed harnesses + if: failure() + uses: actions/upload-artifact@v4 + with: + name: parity-main-logs + path: .local/parity-main-logs/ + if-no-files-found: ignore + retention-days: 7 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4cba851fa..05f590096 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,6 +1,7 @@ name: release # Automatic game releases (docs/release-flow.md). After every green `checks` run on main, every 10 minutes (so a -# release waiting for merges to settle goes out without another merge) and by hand, tools/release_train.py decides: +# release waiting for merges to settle goes out without another merge) and by hand, tools/release_train.py decides, +# for a commit whose checks and parity-main (the vanilla harnesses on main) both passed: # a release when 5 pull requests merged since the last one changed what players run and the newest merged 5 minutes # ago, or when the oldest has waited 6 hours, or at once for a pull request labelled release-now. # RELEASES_PAUSED=1 (repository variable) stops them; so does an open issue labelled release-blocker, forced runs too. @@ -18,7 +19,8 @@ name: release # gh workflow run release -f dry_run=false -f force=true # on main: release now, whatever the rules say on: workflow_run: - workflows: [checks] + # A game release ships a commit both passed on (parity-main runs on batches: the newest commit it passed on). + workflows: [checks, parity-main] types: [completed] branches: [main] schedule: @@ -64,8 +66,8 @@ jobs: env: GH_TOKEN: ${{ github.token }} RELEASES_PAUSED: ${{ vars.RELEASES_PAUSED }} - # The commit whose checks just passed; a dry run builds the branch it was started on; otherwise - # main's newest commit with green checks. + # The commit whose checks or parity-main just passed; a dry run builds the branch it was started on; + # otherwise main's newest commit with green checks and parity-main. HEAD_SHA: ${{ github.event.workflow_run.head_sha || (steps.mode.outputs.dry_run == 'true' && github.sha) || '' }} FORCE: ${{ inputs.force && 'yes' || '' }} run: | diff --git a/docs/parity-workflow.md b/docs/parity-workflow.md index d794b9c71..d64ec3087 100644 --- a/docs/parity-workflow.md +++ b/docs/parity-workflow.md @@ -20,6 +20,8 @@ python3 tools/parity.py --changed origin/main # those a branch's changes ca python3 tools/parity.py --changed-files paths.txt # the same for listed paths, in a copy without Git (tools/pr_ready.py) ``` +On CI, a pull request runs the parity job only when it changes a harness, an oracle, a probe or the runner; everything else is checked on main by `parity-main`, a batch of merges at a time, which bisects a failure to the merge that caused it (`tools/parity_main.py`; [release-flow.md](release-flow.md#parity-on-main)). Game releases wait for it. + Each harness runs unmodified under a tracer that records the files it depended on: its Python modules, its Java oracle and the oracle sources javac pulled in, other repository files it read, and the Rust sources of the probes it ran (from Cargo's `.d` dep-info files). A pass stands until one of those files, the pinned vanilla artifacts, `Cargo.lock` or the Rust/Java toolchains change. State and logs are in `.local/parity-runner/`; each harness's own outputs stay in its `.local//` directory. Vanilla oracle runs are reused as well. `tools/parity_shim.py` stands in for `java` and `javac`: it records each oracle run's outputs and the content of every file under the paths its arguments name, and replays the run while those are unchanged. After a Rust-only change a harness pays only for its Rust side (`block_update`: 53 s to 5 s). Files an oracle writes outside its named paths aren't tracked, so a harness that fails while reusing oracle results is rerun fresh, and stops reusing them if it then passes. `--all` and `--fresh` never reuse them. diff --git a/docs/release-flow.md b/docs/release-flow.md index 4fb0209d9..b551bad21 100644 --- a/docs/release-flow.md +++ b/docs/release-flow.md @@ -13,7 +13,7 @@ reopened; labels never start it): | Job | Runs on | What | Time | | --- | --- | --- | --- | | `verify` | GitHub `ubuntu-latest` | Public-tree check, Python tests, rustfmt, clippy with warnings denied, Rust tests. Dependencies and the workspace build come from a cache saved by main's runs | ~4 min cold, less warm | -| `parity / parity` | The Mac mini's runner (`ludovics-mac-mini`, label `horizon-game`) | The vanilla harnesses the change can affect (`tools/parity.py --changed `) | seconds when nothing is affected; minutes per affected harness | +| `parity / parity` | The Mac mini's runner (`ludovics-mac-mini`, label `horizon-game`) | Only for a change to a harness, an oracle, a probe or the runner (or the `run-parity` label): the vanilla harnesses the change can affect (`tools/parity.py --changed `). Other pull requests skip it: [parity-main](#parity-on-main) checks them on main | seconds when nothing is affected; minutes per affected harness | Pushes to main run `verify` only; the nightly audit covers main's harnesses. @@ -80,10 +80,34 @@ gh api -X POST repos/ludovic111/horizon/branches/main/protection/required_status A skipped `parity` (a fork's pull request without the label) counts as passed, so requiring it never blocks a fork's pull request; the maintainer decides whether to run it. +## Parity on main + +Most pull requests merge on `verify` alone and leave the vanilla harnesses to `.github/workflows/parity-main.yml`, +which runs them on main a batch of merges at a time, on the Mac mini's runner: + +- It starts on every push to main, one run at a time (`concurrency: parity-main`). Merges that land while a run + goes on wait in one pending run: GitHub keeps only the newest and cancels the others, so the next run takes them + all. +- `tools/parity_main.py base` gives the newest commit main contains whose parity-main passed (else the last + merge's parent), and the run is `tools/parity.py --changed `: the union of the harnesses every merge + since then can affect, once, with every earlier pass reused. +- On a failure, `tools/parity_main.py bisect` checks out the batch's first-parent merges by halves and runs only + the failing harnesses, names the first merge they fail on in the run's summary and comments on its pull request + (`🤖 Agent: parity-main found that this merge breaks ...`). Fix it with a pull request as usual; the next + run checks from the last green commit again. +- Game releases ship only a commit both `checks` and `parity-main` passed on (`tools/release_train.py`, see + below), so a broken merge holds releases instead of reaching players. `release.yml` also starts when a + parity-main run finishes. A forced release (`force=true`) doesn't wait for parity-main. + +A pull request still runs its own parity job when it changes a harness script, an oracle +(`mods/horizon/src/test/`), a probe (`crates/*/examples/`, `game/*_fixture.rs`, `game/*_harness.rs`), the runner +(`tools/parity*.py`, `tools/lab.py`, `tools/parity_hosts.json`, `parity*.yml`), or carries the `run-parity` label +(`run-parity.yml` runs it when the label is added). + ## Game releases -`.github/workflows/release.yml` releases the game by itself. It starts after every green `checks` run of a push -to main, every 10 minutes (so a release waiting for merges to settle goes out without another merge), and by hand. +`.github/workflows/release.yml` releases the game by itself. It starts after every green `checks` or `parity-main` +run of a push to main (it releases a commit only once both passed on it), every 10 minutes (so a release waiting for merges to settle goes out without another merge), and by hand. ```mermaid flowchart LR diff --git a/tests/test_parity_main.py b/tests/test_parity_main.py new file mode 100644 index 000000000..7a72ecae5 --- /dev/null +++ b/tests/test_parity_main.py @@ -0,0 +1,43 @@ +"""tools/parity_main.py: the base of a parity-main run, and the bisect after a failure.""" +from pathlib import Path +import tempfile +import unittest +from unittest import mock + +from tools import parity_main + + +class ParityMainTests(unittest.TestCase): + def test_first_bad_finds_the_first_failing_merge(self): + commits = list("abcdefgh") # h failed; the base before a passed + for culprit in commits: + tried = [] + fails = lambda commit: tried.append(commit) or commit >= culprit + self.assertEqual(parity_main.first_bad(commits, fails), culprit) + self.assertLessEqual(len(tried), 3, "log2 of 8 merges") + self.assertEqual(parity_main.first_bad(["a"], lambda commit: self.fail("nothing to try")), "a") + + def test_base_is_the_newest_green_run_head_contains(self): + with tempfile.TemporaryDirectory() as tmp, mock.patch.object(parity_main, "ROOT", Path(tmp)): + git = lambda *arguments: parity_main.git(*arguments).stdout.strip() + git("init", "-q") + for n in range(3): + git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", str(n)) + head, parent, grandparent = git("rev-parse", "HEAD"), git("rev-parse", "HEAD^1"), git("rev-parse", "HEAD~2") + git("checkout", "-q", "-b", "side", grandparent) + git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "side") + side = git("rev-parse", "HEAD") + git("checkout", "-q", head) + self.assertEqual(parity_main.base(runs=[side, grandparent, parent]), grandparent, + "a run on a commit HEAD doesn't contain is skipped") + self.assertEqual(parity_main.base(runs=[]), parent, "no green run yet: the last merge") + self.assertEqual(parity_main.base(runs=[head]), head) + + def test_a_harness_a_commit_does_not_have_cannot_fail_there(self): + with mock.patch.object(parity_main.subprocess, "run") as run: + self.assertFalse(parity_main.run_harnesses(["not_a_harness_yet"], 60)) + run.assert_not_called() + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_release_train.py b/tests/test_release_train.py index 56064fa42..ee1fbcae5 100644 --- a/tests/test_release_train.py +++ b/tests/test_release_train.py @@ -2,6 +2,7 @@ import datetime as dt from pathlib import Path import tempfile import unittest +from unittest import mock from tools import release_train as train @@ -147,6 +148,26 @@ class ReleaseTrainTests(unittest.TestCase): self.assertFalse(train.decision("game", [], NOW, new_line="0.3", blockers=[7])[0]) self.assertTrue(train.decision("game", [pr(1)], NOW, blockers=[])[0]) + def test_game_releases_ship_only_what_parity_main_passed(self): + def runs(parity, checks): + def gh_json(*arguments): + table = parity if "parity-main" in arguments else checks + if "--commit" in arguments: + sha = arguments[arguments.index("--commit") + 1] + return [{"status": s, "conclusion": c} for h, s, c in table if h == sha] + return [{"headSha": h, "status": s, "conclusion": c} for h, s, c in table] + return mock.patch.object(train, "gh_json", gh_json) + checks = [("c", "in_progress", ""), ("b", "completed", "success"), ("a", "completed", "success")] + with runs([("c", "queued", ""), ("b", "completed", "cancelled"), ("a", "completed", "success")], checks): + self.assertEqual(train.green_main_head(), "b", "checks alone: the newest green checks") + self.assertEqual(train.green_main_head(parity=True), "a", "a superseded parity-main run counts for nothing") + self.assertTrue(train.passed_on("parity-main", "a")) + self.assertFalse(train.passed_on("parity-main", "b")) + with runs([("b", "completed", "failure"), ("a", "completed", "success")], checks): + self.assertIsNone(train.green_main_head(parity=True), "a failed batch holds releases until fixed") + with runs([("c", "completed", "success")], checks): + self.assertIsNone(train.green_main_head(parity=True), "its checks must pass too") + def test_a_release_is_published_only_after_its_smoke_test_and_with_no_blocker(self): drew = {"error": None, "frames": 12710, "chunks": 81, "meshed_sections": 550, "player": [160.5, 71.0, 156.5]} self.assertIsNone(train.gate(drew, [])) diff --git a/tools/parity_main.py b/tools/parity_main.py new file mode 100644 index 000000000..ff319aed0 --- /dev/null +++ b/tools/parity_main.py @@ -0,0 +1,129 @@ +#!/usr/bin/env python3 +"""parity-main (.github/workflows/parity-main.yml): the vanilla harnesses run on main, a batch of merges at a time. + + python3 tools/parity_main.py base # the commit this run checks from: main's last green parity-main + python3 tools/parity_main.py bisect BASE # after a failure: the first merge since BASE that broke a harness + +`base` prints the newest commit on main whose parity-main run passed and that HEAD contains, else HEAD's first parent, +so a run tests every merge since the last green one (`tools/parity.py --changed `), however many landed while +the previous run went on. `bisect` reads the harnesses that failed (.local/parity-runner/last-run.json), checks out +the first-parent merges between BASE and HEAD by halves and runs only those harnesses, then names the first merge they +fail on (written to $GITHUB_STEP_SUMMARY and, with --comment, on its pull request). HEAD is checked out again at the end. +""" + +import argparse +import json +import os +from pathlib import Path +import re +import subprocess +import sys + +ROOT = Path(__file__).resolve().parents[1] +REPO = os.environ.get("GITHUB_REPOSITORY", "ludovic111/horizon") +LAST_RUN = ROOT / ".local" / "parity-runner" / "last-run.json" + + +def git(*arguments, check=True): + result = subprocess.run(["git", *arguments], cwd=ROOT, capture_output=True, text=True) + if check and result.returncode: + sys.exit(f"git {' '.join(arguments)}: {result.stderr.strip()}") + return result + + +def green_runs(): + """Head commits of main's passing parity-main runs, newest first.""" + result = subprocess.run(["gh", "run", "list", "--repo", REPO, "--workflow", "parity-main", "--branch", "main", + "--status", "success", "--limit", "30", "--json", "headSha"], capture_output=True, text=True) + if result.returncode: + print(f"note: no parity-main history ({result.stderr.strip()})", file=sys.stderr) + return [] + return [run["headSha"] for run in json.loads(result.stdout or "[]")] + + +def base(head="HEAD", runs=None): + for sha in green_runs() if runs is None else runs: + if git("merge-base", "--is-ancestor", sha, head, check=False).returncode == 0: + return git("rev-parse", sha).stdout.strip() + return git("rev-parse", f"{head}^1").stdout.strip() + + +def first_bad(commits, fails): + """The first of commits (oldest first, the last known to fail; the one before them known to pass) where fails().""" + good, bad = -1, len(commits) - 1 + while bad - good > 1: + middle = (good + bad) // 2 + if fails(commits[middle]): + bad = middle + else: + good = middle + return commits[bad] + + +def harness_exists(name): + return (ROOT / "tools" / f"test_{name.split('#')[0]}_parity.py").exists() + + +def run_harnesses(names, timeout): + """Whether any of the harnesses fails at the checked-out commit (those it doesn't have yet can't).""" + present = [name for name in names if harness_exists(name)] + if not present: + return False + return subprocess.run([sys.executable, "tools/parity.py", *present, "--timeout", str(timeout)], cwd=ROOT).returncode != 0 + + +def bisect(since, timeout, comment): + failed = [] + if LAST_RUN.exists(): + failed = json.loads(LAST_RUN.read_text(encoding="utf-8")).get("failed") or [] + if not failed: + print("No failed harness recorded: nothing to bisect.") + return 0 + head = git("rev-parse", "HEAD").stdout.strip() + commits = git("rev-list", "--first-parent", "--reverse", f"{since}..{head}").stdout.split() + if not commits: + print("No merge since the base.") + return 0 + + def fails(commit): + print(f"--- {commit[:9]} {git('log', '-1', '--format=%s', commit).stdout.strip()}", flush=True) + git("checkout", "-q", "--detach", commit) + return run_harnesses(failed, timeout) + + try: + culprit = first_bad(commits, fails) if len(commits) > 1 else commits[0] + finally: + git("checkout", "-q", "--detach", head) + subject = git("log", "-1", "--format=%s", culprit).stdout.strip() + number = re.search(r"Merge pull request #(\d+)", subject) + line = (f"parity-main: {', '.join(failed)} first fail at {culprit[:9]} ({subject}), " + f"of {len(commits)} merges since {since[:9]}.") + print(line) + if os.environ.get("GITHUB_STEP_SUMMARY"): + with open(os.environ["GITHUB_STEP_SUMMARY"], "a", encoding="utf-8") as summary: + summary.write(f"### {line}\n") + if comment and number: + url = f"{os.environ.get('GITHUB_SERVER_URL', 'https://github.com')}/{REPO}/actions/runs/{os.environ.get('GITHUB_RUN_ID', '')}" + body = (f"🤖 Agent: parity-main found that this merge breaks {', '.join(f'`{name}`' for name in failed)} on main " + f"(bisected over {len(commits)} merges: {url}). Releases wait until parity-main passes again.") + subprocess.run(["gh", "pr", "comment", number.group(1), "--repo", REPO, "--body", body]) + return 0 + + +def main(): + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + commands = parser.add_subparsers(dest="command", required=True) + commands.add_parser("base") + bisecting = commands.add_parser("bisect") + bisecting.add_argument("since", help="the last commit known to pass (the run's base)") + bisecting.add_argument("--timeout", type=int, default=2400, help="seconds per harness") + bisecting.add_argument("--comment", action="store_true", help="comment on the breaking merge's pull request") + args = parser.parse_args() + if args.command == "base": + print(base()) + return 0 + return bisect(args.since, args.timeout, args.comment) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/release_train.py b/tools/release_train.py index 48d4d46cf..72d81ce57 100644 --- a/tools/release_train.py +++ b/tools/release_train.py @@ -414,13 +414,26 @@ def gather(kind, head, releases=None): return last, prs -def green_main_head(): - """The newest commit on main whose checks passed: what an automatic release builds.""" - runs = gh_json("run", "list", "--repo", REPO, "--workflow", "checks", "--branch", "main", "--event", "push", +def passed_on(workflow, sha): + """Whether the newest finished push run of workflow on main for commit sha passed.""" + runs = gh_json("run", "list", "--repo", REPO, "--workflow", workflow, "--branch", "main", "--event", "push", + "--commit", sha, "--limit", "5", "--json", "status,conclusion") + done = [entry for entry in runs if entry["status"] == "completed" and entry["conclusion"] != "cancelled"] + return bool(done) and done[0]["conclusion"] == "success" + + +def green_main_head(parity=False): + """The newest commit on main whose checks passed: what an automatic release builds. With parity (game + releases), the newest commit parity-main passed on, if its checks passed too: parity-main runs on batches of + merges, so the commits a run skipped are covered by the next one, and a superseded run counts for nothing.""" + workflow = "parity-main" if parity else "checks" + runs = gh_json("run", "list", "--repo", REPO, "--workflow", workflow, "--branch", "main", "--event", "push", "--limit", "10", "--json", "headSha,status,conclusion") for entry in runs: - if entry["status"] == "completed": - return entry["headSha"] if entry["conclusion"] == "success" else None + if entry["status"] == "completed" and entry["conclusion"] != "cancelled": + if entry["conclusion"] != "success": + return None + return entry["headSha"] if not parity or passed_on("checks", entry["headSha"]) else None return None @@ -437,7 +450,7 @@ def main(): parser.add_argument("--kind", choices=["game", "launcher"], required=True) parser.add_argument("--head", help="the commit to release (default: the newest commit on main whose checks passed)") parser.add_argument("--tag", help="notes: the release's tag") - parser.add_argument("--force", action="store_true", help="release even without player-facing changes, before merges settle or while paused") + parser.add_argument("--force", action="store_true", help="release even without player-facing changes, before merges settle, while paused or before parity-main passed") parser.add_argument("--out", type=Path, help="notes: write title.txt and body.md here") parser.add_argument("--now", help="the time to decide at (ISO 8601; default now)") parser.add_argument("--smoke", type=Path, help="gate: the release smoke test's report (smoke-test.json)") @@ -456,9 +469,12 @@ def main(): summary.write(f"### {line}\n") return 1 if problem else 0 - head = args.head or green_main_head() + # Game releases ship only a commit the vanilla harnesses passed on (parity-main, docs/release-flow.md). + parity = args.kind == "game" and args.command == "decide" and not args.force + head = args.head or green_main_head(parity) if not head: - result = {"release": False, "reason": "main's latest checks did not pass"} + result = {"release": False, "reason": "main's latest checks did not pass" + ( + " or parity-main did not pass on them" if parity else "")} print(json.dumps(result, indent=1)) write_outputs({"release": "false", "reason": result["reason"]}) return 0 @@ -482,6 +498,8 @@ def main(): blockers = open_blockers() if args.kind == "game" else [] release, reason = decision(args.kind, prs, now, os.environ.get("RELEASES_PAUSED") == "1", args.force, new_line, blockers) + if release and args.head and parity and not (passed_on("parity-main", head) and passed_on("checks", head)): + release, reason = False, f"waiting for checks and parity-main to pass on {head[:9]}" minor = any("feature" in pr["labels"] for pr in prs if pr["paths"]) tag = next_game_tag(tags, now.date(), line) if args.kind == "game" else next_launcher_tag(tags, minor, line) result = { From 40c08c2d12ebfb8d0a56e9bc2f71b72460cfff0c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:38:47 +0100 Subject: [PATCH 213/238] streaming: check every fourth cell of the cover test in debug builds the_grid_answers_as_the_scan compared the grid with the scan on every cell of 60 cases and took 20 of the ~30 s of `cargo test -p horizon-client` on the ThinkPad. A debug build now checks every fourth cell of each row, shifted by row and case, so the cases together still meet every cell position relative to the areas; a release build checks every cell. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/streaming.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 55a2f0086..13b19fefc 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -2148,8 +2148,13 @@ mod cover_tests { } /// Every question streaming asks gets the same answer with the grid. + /// + /// A debug build checks every fourth cell of each row, shifted by row and case so the cases still cover every + /// cell's position relative to the areas: the full check took 20 of the ~30 s of the crate's tests. + /// `cargo test --release -p horizon-client --lib the_grid_answers_as_the_scan` checks every cell. #[test] fn the_grid_answers_as_the_scan() { + let stride = if cfg!(debug_assertions) { 4 } else { 1 }; let mut seed = 0x9E37_79B9_7F4A_7C15u64; let forced: HashSet<(i32, i32)> = [(40, -40)].into(); for case in 0..60 { @@ -2165,7 +2170,8 @@ mod cover_tests { 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 { + let first = -span + (z + case as i32).rem_euclid(stride); + for x in (first..=span).step_by(stride as usize) { for m in 0..=ExtraCover::REACH + 2 { assert_eq!( scan.in_extra(x, z, m), From 0a8af38ac4ed1094a871eacae8338c4b95c636d5 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:39:18 +0100 Subject: [PATCH 214/238] Rings: search them on a thread behind the server, as the world opens The previous commit decides chunks far from the rings without them. The 128 biome searches now start on a helper thread as an overworld with structures opens, behind the server and render threads, so an eye of ender or a chunk near a ring finds them done or shares what is left, instead of running them all on the thread that asked. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/src/game/world_loading.rs | 3 +++ crates/horizon-client/src/vanillagen.rs | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/crates/horizon-client/src/game/world_loading.rs b/crates/horizon-client/src/game/world_loading.rs index 998788960..0cea31fd0 100644 --- a/crates/horizon-client/src/game/world_loading.rs +++ b/crates/horizon-client/src/game/world_loading.rs @@ -246,6 +246,9 @@ pub(super) fn make_streaming( let inner = Arc::new(VanillaTerrain::for_world(&options.resources.join("worldgen"), pack.assets.registry, res.biomes(), seed, dim.id(), structures).map_err(|e| { format!("{e}\nPrepare the worldgen bundle with the launcher, or: horizon-prepare --prepare") })?); + if dim == Dim::Overworld && structures { + inner.search_rings_behind(); + } locator = Some(inner.clone()); Streaming::new( Arc::new(Persistent { diff --git a/crates/horizon-client/src/vanillagen.rs b/crates/horizon-client/src/vanillagen.rs index a17bb22d3..046de6551 100644 --- a/crates/horizon-client/src/vanillagen.rs +++ b/crates/horizon-client/src/vanillagen.rs @@ -279,6 +279,26 @@ impl VanillaTerrain { let worker = worker.get_or_insert_with(|| self.generator.worker()); worker.ring_positions(set) } + /// Searches the concentric rings on a thread of their own, behind the + /// server and render threads, as vanilla's background executor does: + /// the first chunks don't wait for them, and an eye of ender thrown + /// later finds them ready. A chunk or a query that needs them first + /// shares the remaining searches with this thread. + pub fn search_rings_behind(self: &Arc) { + let terrain = Arc::clone(self); + let spawned = std::thread::Builder::new() + .name("Ring search".into()) + .spawn(move || { + horizon_threads::behind_server(); + let _ = terrain + .generator + .worker() + .ring_positions("minecraft:strongholds"); + }); + if let Err(e) = spawned { + log::warn!("ring search thread: {e}"); + } + } } /// The worldgen bundle at `dir`, loaded once and shared by every generator /// and by live feature placement (sapling growth, bone meal). From 40f79354d615be286d3ba761b194f22928f57c79 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:40:43 +0100 Subject: [PATCH 215/238] release: build the preparer and compile the exporters fresh in the exporters job The clean checkout removes target/, and vanilla-data.sh builds horizon-prepare only when it prepares afresh, so compile-exporters had no binary on a cached preparation. The bundle is also compiled in the job instead of reused from .local/exporters, which vanilla-data.sh links to preparations that pull requests' parity jobs write, and the job fails if anything but our classes and the manifest would leave the runner. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/release.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4d20941d4..52eef26da 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -96,9 +96,19 @@ jobs: shell: bash run: | set -euo pipefail + # target/ is gone after the clean checkout, and vanilla-data.sh builds the preparer only when it + # prepares afresh. + cargo build --release --locked -p horizon-prepare + # Compiled here, never taken from the runner's shared preparations: pull requests' parity jobs + # write those (vanilla-data.sh links .local/exporters to them). + rm -rf .local/exporters bundle=$(target/release/horizon-prepare compile-exporters --root "$PWD" | tail -1) mkdir -p exporters cp -R "$bundle/server" "$bundle/client" "$bundle/manifest.json" exporters/ + # Only our classes and their manifest leave this runner. + stray=$(find exporters -type f ! -path exporters/manifest.json \ + ! \( -name '*.class' \( -path 'exporters/server/dev/horizonlab/parity/*' -o -path 'exporters/client/dev/horizonlab/parity/*' \) \)) + [ -z "$stray" ] || { echo "::error::not one of our classes: $stray"; exit 1; } python3 tools/check_prepare_exporters.py --binary target/release/horizon-prepare - uses: actions/upload-artifact@v4 with: From 5034e4557dba8c54ebd406a50ef4449b8d7ec2ea Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:44:22 +0100 Subject: [PATCH 216/238] parity: clear rust--parity outputs after a pass, reuse oracles on CI Disk: clear_large_outputs only looked at .local directories named after the harness, so the 174 harnesses writing to rust--parity kept every dump (86 GB in one runner checkout, 13 GB for state_provider alone). It now clears those too, keeps vanilla outputs only up to 256 MB (larger ones cost more disk than rerunning their oracle), and handles shard names. collision, world and geometry dropped one mkdtemp run directory per run (11 GB): each run now removes the earlier ones. Time: harnesses inherited the runner's stdin, a pipe on GitHub's runner, and parity_shim never reuses an oracle run whose stdin is a pipe, so oracle reuse never worked on CI. Harnesses now get /dev/null. CI: parity.yml falls back to the head's own selection when the names main's `--list` printed don't match its summary count. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/parity.yml | 11 ++++++++++- tests/test_parity_runner.py | 14 ++++++++++++++ tools/parity.py | 20 ++++++++++++++------ tools/test_collision_parity.py | 3 +++ tools/test_geometry_parity.py | 3 +++ tools/test_world_parity.py | 3 +++ 6 files changed, 47 insertions(+), 7 deletions(-) diff --git a/.github/workflows/parity.yml b/.github/workflows/parity.yml index be87bc965..9dcb21775 100644 --- a/.github/workflows/parity.yml +++ b/.github/workflows/parity.yml @@ -68,7 +68,16 @@ jobs: exit fi names=$(sed '$d' .local/parity-selection.txt | grep -E '^[A-Za-z0-9_#]+$' || true) - if [ -z "$names" ]; then + # The summary line says how many would run: a count that doesn't match the names read (a format change) + # must not let the job pass untested. + expected=$(tail -1 .local/parity-selection.txt | sed -nE 's/^([0-9]+) of [0-9]+ harnesses would run$/\1/p') + count=$(printf '%s' "$names" | grep -c . || true) + if [ -z "$expected" ] || [ "$expected" != "$count" ]; then + echo "main's selection listed $count names, its summary says '${expected:-?}': the head's own selection runs" + own + exit + fi + if [ "$count" = 0 ]; then echo "Nothing to run." exit 0 fi diff --git a/tests/test_parity_runner.py b/tests/test_parity_runner.py index d10fde854..2d16e7179 100644 --- a/tests/test_parity_runner.py +++ b/tests/test_parity_runner.py @@ -300,3 +300,17 @@ class LargeOutputTests(unittest.TestCase): self.assertFalse((own / "dump.txt").exists()) # chunk_generation may still be writing there. self.assertTrue((shared / "dump.txt").exists()) + + def test_rust_named_directories_and_big_oracle_outputs_go_too(self): + with tempfile.TemporaryDirectory() as tmp: + local, state = Path(tmp) / ".local", Path(tmp) / "state" + own = local / "rust-state-provider-parity" + own.mkdir(parents=True) + for name, size in (("rust.txt", 16), ("server.txt", 16), ("client.txt", 64), ("report.json", 4)): + (own / name).write_bytes(b"x" * size) + trace = {"commands": [["java", "Oracle", str(own)], ["probe", "fixture", str(own)]]} + with mock.patch.multiple(parity, LOCAL=local, STATE_DIR=state, LARGE_OUTPUT=8, KEPT_ORACLE_OUTPUT=32): + listing = parity.outputs_listing("state_provider#2", fresh=True) + listing.write_text(f"{own / 'server.txt'}\n{own / 'client.txt'}\n") + parity.clear_large_outputs("state_provider#2", trace) + self.assertEqual(sorted(path.name for path in own.iterdir()), ["report.json", "server.txt"]) diff --git a/tools/parity.py b/tools/parity.py index dc5fdb1c9..3f18b922d 100644 --- a/tools/parity.py +++ b/tools/parity.py @@ -657,6 +657,9 @@ def prebuild(artifacts, jobs): LARGE_OUTPUT = 64 << 20 +# Vanilla outputs above this are deleted after a pass too: rerunning those few oracles (12 harnesses on the Mac +# mini's runner) costs less than the 26 GB they held in every checkout; smaller ones stay for oracle reuse. +KEPT_ORACLE_OUTPUT = 256 << 20 OWN_DIRECTORIES = {"parity-target", "parity-runner", "rea", "logs", "gate"} @@ -672,11 +675,12 @@ def clear_large_outputs(name, trace): """After a pass, delete the large files a harness regenerates on every run. Harness outputs reach gigabytes and used to stay in every worktree. Vanilla - oracle outputs are kept, since reusing an oracle run needs them in place; - everything else large under the harness's .local directories is rebuilt on - its next run.""" + oracle outputs under KEPT_ORACLE_OUTPUT are kept, since reusing an oracle + run needs them in place; everything else large under the harness's .local + directories (`...` or `rust--parity`) is rebuilt on its next run.""" local = LOCAL.resolve() keep = {os.path.realpath(line) for line in outputs_listing(name).read_text(encoding="utf-8").splitlines() if line} + own = name.split("#")[0].replace("_", "-") directories = set() for command in trace["commands"]: for argument in command[1:]: @@ -688,12 +692,13 @@ def clear_large_outputs(name, trace): # Only the harness's own directory: another harness running beside it may be writing # large files in a directory this one merely read (decorated_region compiles its oracle # into chunk-generation-parity while chunk_generation writes its dumps there). - if top not in OWN_DIRECTORIES and top.startswith(name.replace("_", "-")): + if top not in OWN_DIRECTORIES and (top.startswith(own) or top == f"rust-{own}-parity"): directories.add(local / top) for directory in directories: for file in (directory.rglob("*") if directory.is_dir() else []): try: - if file.is_file() and not file.is_symlink() and file.stat().st_size >= LARGE_OUTPUT and str(file) not in keep: + size = file.stat().st_size if file.is_file() and not file.is_symlink() else 0 + if size >= LARGE_OUTPUT and (str(file) not in keep or size > KEPT_ORACLE_OUTPUT): file.unlink() except OSError: pass @@ -828,8 +833,11 @@ class LocalExecutor: environment.pop("PARITY_SHARD", None) if shard: environment["PARITY_SHARD"] = shard + # stdin from /dev/null: tools/parity_shim.py never reuses an oracle run that reads a pipe, and CI runners + # (and --worker's ssh) give the runner one, which kept oracle reuse off on CI. process = subprocess.Popen([sys.executable, str(Path(__file__)), "--trace", str(trace), str(script)], - cwd=ROOT, env=environment, stdout=log, stderr=subprocess.STDOUT, start_new_session=True) + cwd=ROOT, env=environment, stdin=subprocess.DEVNULL, stdout=log, + stderr=subprocess.STDOUT, start_new_session=True) self.running[name] = {"process": process, "started": time.monotonic(), "log": log, "expected": expected, "slot": slot, "script": script} return True diff --git a/tools/test_collision_parity.py b/tools/test_collision_parity.py index ad99ab960..26f7f25df 100644 --- a/tools/test_collision_parity.py +++ b/tools/test_collision_parity.py @@ -5,6 +5,7 @@ import json import os from pathlib import Path import platform +import shutil import subprocess import tempfile @@ -37,6 +38,8 @@ def main(): if args.prepare: lab.prepare() lock, jars, libraries = test_rust_parity.verify_artifacts() + for earlier in directory.glob("run-*"): # one run per run used to stay behind: 5 GB here after a few weeks + shutil.rmtree(earlier, ignore_errors=True) base = Path(tempfile.mkdtemp(prefix="run-", dir=directory)) oracle = lab.ROOT / "mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCollisionOracle.java" sources = [oracle, Path(__file__).resolve(), lab.ROOT / "tools/test_rust_parity.py", lab.ROOT / "Cargo.toml", lab.ROOT / "Cargo.lock", lab.ROOT / "crates/horizon-core/Cargo.toml", lab.ROOT / "crates/horizon-world/Cargo.toml", *sorted((lab.ROOT / "crates/horizon-core/src").rglob("*.rs")), *sorted((lab.ROOT / "crates/horizon-world/src").rglob("*.rs")), lab.ROOT / "crates/horizon-world/examples/probe/collision_fixture.rs"] diff --git a/tools/test_geometry_parity.py b/tools/test_geometry_parity.py index 54ebc21ae..fa58bf66f 100644 --- a/tools/test_geometry_parity.py +++ b/tools/test_geometry_parity.py @@ -9,6 +9,7 @@ from pathlib import Path import platform import random import struct +import shutil import subprocess import tempfile @@ -273,6 +274,8 @@ def main(): if args.prepare: lab.prepare() lock, jars, libraries = test_rust_parity.verify_artifacts() + for earlier in directory.glob("run-*"): # one run per run used to stay behind: 5 GB here after a few weeks + shutil.rmtree(earlier, ignore_errors=True) base = Path(tempfile.mkdtemp(prefix="run-", dir=directory)) commands = list(corpus()) inputs = base / "inputs.txt" diff --git a/tools/test_world_parity.py b/tools/test_world_parity.py index 0b4a3deb7..9344a47b0 100644 --- a/tools/test_world_parity.py +++ b/tools/test_world_parity.py @@ -5,6 +5,7 @@ import json import os from pathlib import Path import platform +import shutil import subprocess import tempfile @@ -35,6 +36,8 @@ def main(): if args.prepare: lab.prepare() lock, jars, libraries = test_rust_parity.verify_artifacts() + for earlier in directory.glob("run-*"): # one run per run used to stay behind: 5 GB here after a few weeks + shutil.rmtree(earlier, ignore_errors=True) base = Path(tempfile.mkdtemp(prefix="run-", dir=directory)) classes = base / "classes" classes.mkdir() From 1dd47581de0fece92a193888e0a9518632edc787 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:59:15 +0100 Subject: [PATCH 217/238] Ring search thread: the idle class, not behind the server On a 4-thread laptop the background search at nice +10 took a share of the cores from the chunk workers: 600 chunks arrived 3-4 s later. The thread now runs only on cores nothing else wants (Linux SCHED_IDLE, macOS background QoS, Windows idle priority): horizon_threads::idle_current. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/vanillagen.rs | 12 +++--- crates/horizon-threads/src/lib.rs | 54 +++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/crates/horizon-client/src/vanillagen.rs b/crates/horizon-client/src/vanillagen.rs index 046de6551..7623d720f 100644 --- a/crates/horizon-client/src/vanillagen.rs +++ b/crates/horizon-client/src/vanillagen.rs @@ -279,17 +279,17 @@ impl VanillaTerrain { let worker = worker.get_or_insert_with(|| self.generator.worker()); worker.ring_positions(set) } - /// Searches the concentric rings on a thread of their own, behind the - /// server and render threads, as vanilla's background executor does: - /// the first chunks don't wait for them, and an eye of ender thrown - /// later finds them ready. A chunk or a query that needs them first - /// shares the remaining searches with this thread. + /// Searches the concentric rings on a thread of their own, as vanilla's + /// background executor does, in the idle class: on a few cores it would + /// otherwise slow the first chunks, which don't need the rings. An eye + /// of ender thrown later finds them ready, and a chunk or a query that + /// needs them first shares the remaining searches with this thread. pub fn search_rings_behind(self: &Arc) { let terrain = Arc::clone(self); let spawned = std::thread::Builder::new() .name("Ring search".into()) .spawn(move || { - horizon_threads::behind_server(); + horizon_threads::idle_current(); let _ = terrain .generator .worker() diff --git a/crates/horizon-threads/src/lib.rs b/crates/horizon-threads/src/lib.rs index ae4cc753f..27ee32cc4 100644 --- a/crates/horizon-threads/src/lib.rs +++ b/crates/horizon-threads/src/lib.rs @@ -90,6 +90,42 @@ fn behind() -> bool { false } +/// Run the calling thread only on a core nothing else wants: work that may +/// take seconds and that nothing waits for yet (the stronghold rings while a +/// world opens). The idle class on Linux, background quality of service on +/// macOS, idle priority on Windows. Whether the OS took it. +pub fn idle_current() -> bool { + idle() +} + +#[cfg(target_os = "linux")] +fn idle() -> bool { + let param = libc::sched_param { sched_priority: 0 }; + // SAFETY: sets the calling thread's own policy (pid 0); `param` lives + // across the call. + unsafe { libc::sched_setscheduler(0, libc::SCHED_IDLE, ¶m) == 0 } +} + +#[cfg(target_vendor = "apple")] +fn idle() -> bool { + // SAFETY: sets the calling thread's own QoS class; no pointers. + unsafe { libc::pthread_set_qos_class_self_np(libc::qos_class_t::QOS_CLASS_BACKGROUND, 0) == 0 } +} + +#[cfg(windows)] +fn idle() -> bool { + use windows_sys::Win32::System::Threading::{ + GetCurrentThread, SetThreadPriority, THREAD_PRIORITY_IDLE, + }; + // SAFETY: GetCurrentThread's pseudo handle needs no closing. + unsafe { SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE) != 0 } +} + +#[cfg(not(any(target_os = "linux", target_vendor = "apple", windows)))] +fn idle() -> bool { + false +} + #[cfg(test)] mod tests { /// The hint holds on the platforms that take it. @@ -145,4 +181,22 @@ mod tests { #[cfg(not(target_os = "linux"))] assert!(!std::thread::spawn(super::behind_server).join().unwrap()); } + + /// The idle hint holds on the three platforms. + #[test] + fn idling_a_thread_takes() { + let took = std::thread::spawn(super::idle_current).join().unwrap(); + assert_eq!( + took, + cfg!(any(target_os = "linux", target_vendor = "apple", windows)) + ); + #[cfg(target_os = "linux")] + std::thread::spawn(|| { + assert!(super::idle_current()); + // SAFETY: reads the calling thread's own policy. + assert_eq!(unsafe { libc::sched_getscheduler(0) }, libc::SCHED_IDLE); + }) + .join() + .unwrap(); + } } From 8b26c4cf348e48ccf9d47acdb08984665ed45a84 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 01:14:39 +0100 Subject: [PATCH 218/238] Ring search thread: start once generation pauses In the idle class the thread still slowed a fresh world's first 600 chunks by about 3 s on a 4-thread laptop (it shares a physical core with a worker). It now waits until no proto chunk was generated for 2 s, or a minute after the world opened, and gives up if the world closes first. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/vanillagen.rs | 47 ++++++++++++++++++++----- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/crates/horizon-client/src/vanillagen.rs b/crates/horizon-client/src/vanillagen.rs index 7623d720f..c890213c5 100644 --- a/crates/horizon-client/src/vanillagen.rs +++ b/crates/horizon-client/src/vanillagen.rs @@ -41,6 +41,10 @@ pub struct VanillaTerrain { tick_states: Vec, seed: i64, dimension: String, + /// When the last proto chunk was generated, in milliseconds since + /// `created`: the ring search waits for a pause in generation. + last_proto: std::sync::atomic::AtomicU64, + created: std::time::Instant, } /// A proto chunk, or the position of one whose generation failed. enum VanillaProto { @@ -139,6 +143,8 @@ impl VanillaTerrain { tick_states, seed, dimension: dimension.to_owned(), + last_proto: std::sync::atomic::AtomicU64::new(0), + created: std::time::Instant::now(), }) } /// A proto chunk's blocks, biomes, scheduled ticks and post-processing @@ -280,25 +286,46 @@ impl VanillaTerrain { worker.ring_positions(set) } /// Searches the concentric rings on a thread of their own, as vanilla's - /// background executor does, in the idle class: on a few cores it would - /// otherwise slow the first chunks, which don't need the rings. An eye - /// of ender thrown later finds them ready, and a chunk or a query that + /// background executor does, once generation pauses for two seconds + /// (at the latest a minute after the world opens) and in the idle + /// class: on a laptop's few cores, sharing them with the first chunks, + /// which don't need the rings, made those 3 s slower. An eye of ender + /// thrown later finds the rings ready, and a chunk or a query that /// needs them first shares the remaining searches with this thread. pub fn search_rings_behind(self: &Arc) { - let terrain = Arc::clone(self); + const QUIET: u64 = 2_000; + const LATEST: u64 = 60_000; + let terrain = Arc::downgrade(self); let spawned = std::thread::Builder::new() .name("Ring search".into()) .spawn(move || { horizon_threads::idle_current(); - let _ = terrain - .generator - .worker() - .ring_positions("minecraft:strongholds"); + loop { + std::thread::sleep(std::time::Duration::from_millis(500)); + // The world closed: nothing to search for. + let Some(terrain) = terrain.upgrade() else { + return; + }; + let now = terrain.elapsed_ms(); + let last = terrain + .last_proto + .load(std::sync::atomic::Ordering::Relaxed); + if now >= LATEST || (last > 0 && now - last >= QUIET) { + let _ = terrain + .generator + .worker() + .ring_positions("minecraft:strongholds"); + return; + } + } }); if let Err(e) = spawned { log::warn!("ring search thread: {e}"); } } + fn elapsed_ms(&self) -> u64 { + u64::try_from(self.created.elapsed().as_millis()).unwrap_or(u64::MAX) + } } /// The worldgen bundle at `dir`, loaded once and shared by every generator /// and by live feature placement (sapling growth, bone meal). @@ -346,6 +373,10 @@ impl ChunkGenerator for VanillaTerrain { } } fn proto(&self, worker: &mut GeneratorWorker, x: i32, z: i32) -> Proto { + self.last_proto.store( + self.elapsed_ms().max(1), + std::sync::atomic::Ordering::Relaxed, + ); match worker.generate_proto(ChunkPos::new(x, z)) { Ok(chunk) => Box::new(VanillaProto::Ready(Box::new(chunk))), Err(e) => { From 045bda8bfcb53c332af600d359fb662ac87ce0f3 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 01:21:20 +0100 Subject: [PATCH 219/238] release: give the exporters job an hour, as the Mac builds Co-Authored-By: Claude Opus 5.5 --- .github/workflows/release.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 52eef26da..5d9801a32 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -82,7 +82,8 @@ jobs: if: needs.decide.outputs.release == 'true' # This runner holds the maintainer's own pinned Minecraft jars. Only our classes leave it. runs-on: ${{ vars.MAC_RUNNER || 'ludovics-mac-mini' }} - timeout-minutes: 30 + # Three full preparations (two isolated, one batched) besides the compile. + timeout-minutes: 60 steps: - uses: actions/checkout@v4 with: From 9db6a0ee48a6862df8e27fe0e5cb587a61c67e0f Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 01:23:27 +0100 Subject: [PATCH 220/238] CI: parity-main stays on the Mac; heat check on a median, boot as the first sign; nix roots fixed nix-build names a dev output's GC root -dev, so the runner's PKG_CONFIG_PATH pointed at links that didn't exist and every client build failed on libudev; it now uses the store path nix-build prints. The package temperature spikes for a second under load, so the heat check takes the median of 5 readings. Restarting the watcher no longer throttles a running job. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/parity-main.yml | 4 +++- docs/release-flow.md | 5 +++-- tools/ci/linux-runner.sh | 5 +++-- tools/ci/runner_pause.py | 26 ++++++++++++++++---------- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/.github/workflows/parity-main.yml b/.github/workflows/parity-main.yml index 1ddfb24bb..1debcc694 100644 --- a/.github/workflows/parity-main.yml +++ b/.github/workflows/parity-main.yml @@ -23,7 +23,9 @@ env: jobs: parity-main: - runs-on: [self-hosted, horizon-game] + # The Mac mini's runners only: a desktop job slowed down because the maintainer came back could time out, and a + # failure here bisects and comments on merges. + runs-on: [self-hosted, horizon-game, macOS] timeout-minutes: 240 # a core-crate batch reruns ~300 harnesses (~60-90 min), a bisect a few rounds of the failures steps: - uses: actions/checkout@v4 diff --git a/docs/release-flow.md b/docs/release-flow.md index 28d1feef9..dc96498a0 100644 --- a/docs/release-flow.md +++ b/docs/release-flow.md @@ -90,8 +90,9 @@ processes, fleet-load's own detection, or a process outside the CI slice using t under way is throttled to 2 threads at the lowest weight and the runner stops after it: freezing the job would make GitHub drop it as lost and the harnesses time out. Old pull requests' `runner-env.sh` know only the Mac: the runner's `.env` and `.path` give their jobs Python, the JDK and the vanilla data (`HORIZON_SHARED_SOURCE`) all the -same. The nightly audit stays on the Mac mini's runners, so its comparison with the previous night is always made -on one platform. +same. The nightly audit and parity-main stay on the Mac mini's runners: the audit compares with the previous night, +so it stays on one platform, and a parity-main job slowed down on the desktop because he came back could time out, +and its failures bisect and comment on merges. ## Merging diff --git a/tools/ci/linux-runner.sh b/tools/ci/linux-runner.sh index 1bd587e79..c7235f5c1 100755 --- a/tools/ci/linux-runner.sh +++ b/tools/ci/linux-runner.sh @@ -24,8 +24,9 @@ version=2.338.0 sha=af4b794c1bc41d73d40535e3fe092a39f9679cd8d965954c2aca25a05ca41d32 here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) -# Nix packages the jobs need, kept alive by GC roots in $dir/nix. -nix_out() { nix-build '' -A "$1" -o "$dir/nix/$2" > /dev/null && readlink -f "$dir/nix/$2"; } +# Nix packages the jobs need, kept alive by GC roots in $dir/nix. nix-build prints the store path; the root's name +# gets the output's suffix (alsa-dev-dev for alsa-lib.dev), so it is not read back from the link. +nix_out() { nix-build '' -A "$1" -o "$dir/nix/$2" | tail -1; } # The runner is a .NET program: on NixOS its native libraries (libstdc++, ICU, OpenSSL) come from nix-ld. export LD_LIBRARY_PATH=/run/current-system/sw/share/nix-ld/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} diff --git a/tools/ci/runner_pause.py b/tools/ci/runner_pause.py index 35476a0b2..8fa0a4c5b 100755 --- a/tools/ci/runner_pause.py +++ b/tools/ci/runner_pause.py @@ -5,7 +5,7 @@ The runner works while nobody uses the machine: its screen locked, or untouched It pauses when any of these holds: - keyboard, mouse or gamepad input in the last INPUT_PAUSE seconds (60), or an unlocked session on seat0 with input in the last IDLE_AFTER seconds; - - the CPU package at TEMP_LIMIT °C (90) or more; + - the CPU package at TEMP_LIMIT °C (90) or more (the median of 5 readings); - fleet-load says a game runs (~/fleet/load: Steam, gamescope, Wine or Proton processes); - a process of his matches GAME below (the same, plus Minecraft and a Horizon game he started himself); - a process outside horizon-ci.slice keeps the GPU busy (nvidia-smi: SM use >= GPU_BUSY %, default 50, in two @@ -63,7 +63,7 @@ class Input: def __init__(self): self.devices = {} # path -> file descriptor - self.last = time.monotonic() # a start counts as input: nothing is known yet + self.last = 0.0 # the boot (the monotonic clock's zero on Linux) counts as input def poll(self): try: @@ -115,14 +115,20 @@ def at_the_screen(input_idle): def too_hot(): - """The CPU package above TEMP_LIMIT °C (coretemp or k10temp).""" + """The CPU package at TEMP_LIMIT °C or more (coretemp or k10temp), as the median of 5 readings over 2 s: under + load it jumps for a second at a time (to 96 °C on ludodesktop at a median of 68 °C), which is no reason to stop.""" for monitor in Path("/sys/class/hwmon").glob("hwmon*"): try: - if (monitor / "name").read_text().strip() in ("coretemp", "k10temp"): - celsius = int((monitor / "temp1_input").read_text()) / 1000 - return f"CPU at {celsius:.0f} °C" if celsius >= TEMP_LIMIT else None + if (monitor / "name").read_text().strip() not in ("coretemp", "k10temp"): + continue + readings = [] + for _ in range(5): + readings.append(int((monitor / "temp1_input").read_text()) / 1000) + time.sleep(0.5) except (OSError, ValueError): - pass + continue + celsius = sorted(readings)[2] + return f"CPU at {celsius:.0f} °C" if celsius >= TEMP_LIMIT else None return None @@ -219,9 +225,9 @@ def pause_reason(hot, keys): def main(): hot, keys = {}, Input() - # A boot (or a restart of this watcher) counts as a sign: the runner starts RESUME_AFTER seconds later at the - # earliest, which leaves time to log in after switching the machine on. - last_sign = time.monotonic() + # The boot counts as a sign: the runner starts RESUME_AFTER seconds after it at the earliest, which leaves time + # to log in after switching the machine on. Restarting this watcher later changes nothing for a running job. + last_sign = 0.0 paused = None while True: reason = pause_reason(hot, keys) From bdbf67ef9399a05e741c64c85e46e98fec69964e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 01:31:38 +0100 Subject: [PATCH 221/238] pr_ready: work out the affected harnesses once, not once per harness affected_here called parity.affected_harnesses inside the filter, once for each of the 343 harnesses. Since the selection reads the Rust items each probe reaches (#1964) one call takes about a minute and a half, so pr_ready sat for hours before its first step. Co-Authored-By: Claude Opus 5.5 --- tools/pr_ready.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/pr_ready.py b/tools/pr_ready.py index 505de28c1..4a3da6bae 100755 --- a/tools/pr_ready.py +++ b/tools/pr_ready.py @@ -164,7 +164,8 @@ def affected_here(changed): (parity.SHARED / "dependencies" / f"{name}.json").exists() for name in found) if not known and not changed & parity.SHARED_INPUTS: return None - return harness_names(name for name in found if name in parity.affected_harnesses(found, records, changed)) + affected = parity.affected_harnesses(found, records, changed) + return harness_names(name for name in found if name in affected) def short_list(names, limit=12): From 49bbdf8954bdc8958bc6f8a2e7917bb8035f710c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 01:38:23 +0100 Subject: [PATCH 222/238] CI: the desktop's CI slice gets half the threads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On all 24, a cargo build held ludodesktop's CPU at 92 °C and the watcher kept pausing; at half, harnesses run at a median of 80 °C. Co-Authored-By: Claude Opus 5.5 --- tools/ci/linux-runner.sh | 1 + tools/ci/runner_pause.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/ci/linux-runner.sh b/tools/ci/linux-runner.sh index c7235f5c1..699742bf1 100755 --- a/tools/ci/linux-runner.sh +++ b/tools/ci/linux-runner.sh @@ -131,6 +131,7 @@ Description=Horizon CI: the runner's jobs and parity workers sent from other mac [Slice] CPUWeight=20 IOWeight=20 +CPUQuota=$(( threads * 50 ))% MemoryHigh=$(( $(awk '/MemTotal/ {print int($2 / 1048576)}' /proc/meminfo) / 2 ))G MemoryMax=$(( $(awk '/MemTotal/ {print int($2 / 1048576)}' /proc/meminfo) * 2 / 3 ))G EOF diff --git a/tools/ci/runner_pause.py b/tools/ci/runner_pause.py index 8fa0a4c5b..845627243 100755 --- a/tools/ci/runner_pause.py +++ b/tools/ci/runner_pause.py @@ -36,6 +36,8 @@ GPU_BUSY = int(os.environ.get("GPU_BUSY", 50)) INTERVAL = int(os.environ.get("INTERVAL", 15)) RESUME_AFTER = int(os.environ.get("RESUME_AFTER", 300)) THROTTLE_CPUS = int(os.environ.get("THROTTLE_CPUS", 2)) +# Working, the slice gets half the threads: on all of them ludodesktop's CPU sits at 92 °C through a cargo build. +NORMAL_CPUS = int(os.environ.get("NORMAL_CPUS", max(1, (os.cpu_count() or 2) // 2))) INPUT_PAUSE = int(os.environ.get("INPUT_PAUSE", 60)) IDLE_AFTER = int(os.environ.get("IDLE_AFTER", 900)) TEMP_LIMIT = int(os.environ.get("TEMP_LIMIT", 90)) @@ -199,7 +201,7 @@ def throttle(on): if on: systemctl("set-property", "--runtime", SLICE, "CPUWeight=1", "IOWeight=1", f"CPUQuota={THROTTLE_CPUS * 100}%") else: - systemctl("set-property", "--runtime", SLICE, "CPUWeight=20", "IOWeight=20", "CPUQuota=") + systemctl("set-property", "--runtime", SLICE, "CPUWeight=20", "IOWeight=20", f"CPUQuota={NORMAL_CPUS * 100}%") def pause_reason(hot, keys): From 160c1e8b7a3bebcd7157feff5c4cf17ada0198c2 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 01:54:02 +0100 Subject: [PATCH 223/238] CI: a failed preparation leaves nothing in .local When horizon-prepare failed, vanilla-data.sh deleted the preparation folder but kept what it had already written to .local/ (horizon-client, horizon-client-motion, ...). The next job then saw those real folders as already there: its preparation listed only horizon-client-structures in .entries, and the checkout kept using the stale resources outside the per-key cache. Now a failed preparation removes the .local/ entries it created; a job first removes real folders named like preparation outputs, and makes again a preparation whose .entries lacks horizon-client. Co-Authored-By: Claude Opus 5.5 --- tools/ci/vanilla-data.sh | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tools/ci/vanilla-data.sh b/tools/ci/vanilla-data.sh index 5276c54e9..f548e1f14 100755 --- a/tools/ci/vanilla-data.sh +++ b/tools/ci/vanilla-data.sh @@ -42,7 +42,21 @@ until mkdir "$lock" 2>/dev/null; do done trap 'rmdir "$lock" 2>/dev/null || true' EXIT # horizon-prepare writes .local/horizon-client and its exporters' outputs beside it: all of them live in -# $prepared, listed in $prepared/.entries, and .local/ links to them. +# $prepared, listed in $prepared/.entries, and .local/ links to them. A real folder of that kind in .local/ is +# what an older failed preparation left: it would hide the preparations (and a new one would not count it as its +# own output), so it goes. +outputs=$({ cat "$shared"/prepared/*/.entries; cd .local && ls -d horizon-client*; } 2>/dev/null || true) +for name in $(echo "$outputs" | LC_ALL=C sort -u); do + if [ -e ".local/$name" ] && [ ! -L ".local/$name" ]; then + rm -rf ".local/${name:?}" + echo "Removed .local/$name, left by a failed preparation" + fi +done +# A preparation made while such a folder was there did not list it (horizon-client above all), so it is made again. +if [ -f "$prepared/.entries" ] && ! grep -qx horizon-client "$prepared/.entries"; then + echo "Preparation $key lacks horizon-client: preparing again" + rm -rf "$prepared" +fi if [ ! -f "$prepared/.entries" ]; then echo "Preparing resources with this checkout's horizon-prepare ($key)" rm -rf "$prepared" && mkdir -p "$prepared" @@ -55,6 +69,9 @@ if [ ! -f "$prepared/.entries" ]; then cargo build --release --locked -p horizon-prepare if ! target/release/horizon-prepare --root "$PWD" > "$RUNNER_TEMP/prepare.log" 2>&1; then tail -40 "$RUNNER_TEMP/prepare.log" + # Nothing of a failed preparation stays in .local/: the next job prepares again. + LC_ALL=C comm -13 <(echo "$before") <(ls -1A .local | LC_ALL=C sort) | + while read -r name; do rm -rf ".local/${name:?}"; done rm -rf "$prepared" exit 1 fi From d782e455a41f21e79675347c4314c9e8e2bdb535 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 01:55:57 +0100 Subject: [PATCH 224/238] docs: the desktop's CI slice gets half the threads Co-Authored-By: Claude Opus 5.5 --- docs/release-flow.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/release-flow.md b/docs/release-flow.md index dc96498a0..7bb64d21e 100644 --- a/docs/release-flow.md +++ b/docs/release-flow.md @@ -78,8 +78,8 @@ time, under a lock) and the parity passes and slots. `horizon-game` once a full trial has passed there) runs on the maintainer's Linux desktop while nobody uses it: its screen locked, or untouched for 15 minutes (his rule, 2026-10-26). `tools/ci/linux-runner.sh install`, run there as him, registers it and installs two systemd user services: `horizon-runner` (the runner, in -`~/.actions-runners/horizon`, nice 19, in `horizon-ci.slice` with CPU and I/O weight 20 and memory capped at two -thirds of the RAM; `CARGO_BUILD_JOBS` and the harness slots at 8, after the machine froze under ~7 cold cargo +`~/.actions-runners/horizon`, nice 19, in `horizon-ci.slice` with CPU and I/O weight 20, half the threads (on all of +them the CPU package reached 92–96 °C) and memory capped at two thirds of the RAM; `CARGO_BUILD_JOBS` and the harness slots at 8, after the machine froze under ~7 cold cargo builds on 2026-10-14; its own Rust install; the JDK, sccache and the build libraries from nixpkgs; its own `vscreen` so no game it starts reaches his screen) and `horizon-runner-pause` (`tools/ci/runner_pause.py`), the only thing that starts the runner. The watcher stops it at once on keyboard, mouse or gamepad input (read from From 24b26da5d6a2d9fcce961ef8fa289a5dda0bff10 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 02:19:17 +0100 Subject: [PATCH 225/238] server_ops_feedback: wait for the save's reply before the next step On the desktop runner (8 harnesses at a time) vanilla's save-all took 2 s, the whole step: the transcript ended before "Saved the game" and the harness failed with Horizon right. The save step now waits for that console line, on both sides, before its usual 2 s. Co-Authored-By: Claude Opus 5.5 --- tools/test_server_ops_feedback_parity.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/test_server_ops_feedback_parity.py b/tools/test_server_ops_feedback_parity.py index 2dd405517..f7b7a0e73 100644 --- a/tools/test_server_ops_feedback_parity.py +++ b/tools/test_server_ops_feedback_parity.py @@ -54,6 +54,9 @@ QUIET = [ ] CASES = {'broadcast': ({}, BROADCAST), 'quiet': ({'broadcast-console-to-ops': 'false'}, QUIET)} STEP_SECONDS = 2.0 +# Steps whose reply can take longer than a step: the next step waits for this console line first. A loaded machine +# took 2 s to save vanilla's world, and the transcript ended before "Saved the game". +WAIT_FOR = {'console-save': r'System chat: Saved the game'} def run_side(label, case, server_command, client_command, build, world): @@ -88,6 +91,8 @@ def run_side(label, case, server_command, client_command, build, world): server.console(command) for action in actions: tell(action) + if step in WAIT_FOR: + server.wait_for(WAIT_FOR[step], timeout=120) time.sleep(STEP_SECONDS) tell('end') client.wait(timeout=60) From d3af1e04e01c1122c0647447dbb918dbf53277cf Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 02:27:37 +0100 Subject: [PATCH 226/238] Mannequins run no mob goals and never despawn A mannequin is an Avatar (a LivingEntity), not a Mob: its serverAiStep has no goals and LivingEntity has no checkDespawn. With the passive brain a summoned mannequin wandered off (14 blocks in 400 ticks on the server thread) and could despawn far from players. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/mobs/ai.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 97da5ea06..c9bc15fda 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -664,8 +664,10 @@ fn ai_step(m: &mut Mob, w: &MobWorld, level_random: &mut LegacyRandom, events: & m.ai.xxa *= 0.98; m.ai.zza *= 0.98; // LivingEntity.isImmobile: no goals or input (a biting, stunned or - // roaring ravager). - let immobile = m.species() == Species::Ravager && super::ravager::immobile(m); + // roaring ravager). A mannequin is an Avatar, not a Mob: LivingEntity's + // serverAiStep has no goals, so it only falls and is pushed. + let immobile = (m.species() == Species::Ravager && super::ravager::immobile(m)) + || m.species() == Species::Mannequin; if m.alive() && !immobile { server_ai_step(m, w, level_random, events); } else { @@ -1018,6 +1020,10 @@ pub fn despawn_check(m: &mut Mob, w: &MobWorld) -> bool { /// Mob.checkDespawn, before the tick (players are the only despawn /// reference). Returns false when the mob is discarded. fn check_despawn(m: &mut Mob, w: &MobWorld) -> bool { + // A mannequin is a LivingEntity, which never despawns. + if m.species() == Species::Mannequin { + return true; + } let Some(p) = w.nearest_player else { return true; }; From 48db367d4a32e7ad5ee6bfa51765052c375d25d9 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 03:09:29 +0100 Subject: [PATCH 227/238] Mod API: zoom the camera, take the mouse wheel (render, input) render.set-zoom divides the field of view by up to 10, eased through the game's FOV easing or at once, with a turning scale (at least 0.05); only while playing, and the spyglass's own view wins. The strongest of several mods' zooms counts, and the built-in zoom keeps working beside it. input.claim-wheel takes the wheel from the hotbar while playing: its steps are the mod's wheel events. That is the one input a mod consumes; its keys never do (docs/api/stability.md). A screen opening now lets the mod's held keys up, and mod keys bound to mouse buttons (key.mouse.4) are heard. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 2 + crates/horizon-client/src/game/extras.rs | 27 +++++--- crates/horizon-client/src/game/mod_input.rs | 62 ++++++++++++++++- crates/horizon-client/src/game/mods.rs | 4 ++ crates/horizon-mod-sdk/wit/horizon.wit | 25 ++++++- crates/horizon-mods/src/manifest.rs | 4 +- crates/horizon-mods/src/runtime.rs | 68 ++++++++++++++++++- crates/horizon-mods/tests/sandbox.rs | 50 ++++++++++++++ crates/horizon-mods/tests/ui_guest/src/lib.rs | 21 +++++- docs/api/mods-reference.md | 17 ++++- docs/api/stability.md | 13 +++- docs/mods.md | 4 +- 12 files changed, 273 insertions(+), 24 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 5b04d8716..508122bca 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -10995,6 +10995,7 @@ impl ApplicationHandler for Game<'_> { if self.screen == Screen::Playing && self.sleeping.is_none() && self.player.is_some() + && !self.mod_wheel(d) && !self.extras_scroll(d) && let Some((wheel, (_, vertical_steps))) = self.mouse @@ -11165,6 +11166,7 @@ impl ApplicationHandler for Game<'_> { return Ok(()); } self.extras_mouse(button, pressed); + self.mod_mouse_key(button, pressed); match self.settings.keys.translate_mouse(button) { Some(Err(b)) => self.play_mouse(b, pressed)?, Some(Ok(k)) if pressed => { diff --git a/crates/horizon-client/src/game/extras.rs b/crates/horizon-client/src/game/extras.rs index 4664f0c6f..1978d003f 100644 --- a/crates/horizon-client/src/game/extras.rs +++ b/crates/horizon-client/src/game/extras.rs @@ -229,25 +229,34 @@ impl Game<'_> { fn zoom_factor(&self) -> f32 { self.extras.zoom.factor(self.scoping()) } + /// The zoom in effect, the extra's or a mod's (`render.set-zoom`), + /// whichever is stronger: (factor, smooth, turning scale). + fn zoom_now(&self) -> (f32, bool, f64) { + let factor = self.zoom_factor(); + let e = &self.settings.extras; + let own = (factor, e.zoom_smooth, turn_scale(factor, e.zoom_sensitivity)); + match self.mod_zoom() { + Some(z) if z.factor > factor => (z.factor, z.smooth, f64::from(z.turn)), + _ => own, + } + } /// The field-of-view modifier target, divided by a smooth zoom. pub(super) fn zoomed_fov_target(&self, vanilla: f32) -> f32 { - if self.settings.extras.zoom_smooth { - fov_target(vanilla, self.zoom_factor()) - } else { - vanilla + match self.zoom_now() { + (factor, true, _) => fov_target(vanilla, factor), + _ => vanilla, } } /// The field of view in degrees, divided by an instant zoom. pub(super) fn zoomed_fov_degrees(&self, fov: f32) -> f32 { - if self.settings.extras.zoom_smooth { - fov - } else { - fov / self.zoom_factor() + match self.zoom_now() { + (factor, false, _) => fov / factor, + _ => fov, } } /// Mouse turning while zoomed. pub(super) fn zoom_turn_scale(&self) -> f64 { - turn_scale(self.zoom_factor(), self.settings.extras.zoom_sensitivity) + self.zoom_now().2 } /// The FPS counter and keystrokes, in the HUD layer (hidden with F1 by /// the caller). The top left corner gives way to the debug screen. diff --git a/crates/horizon-client/src/game/mod_input.rs b/crates/horizon-client/src/game/mod_input.rs index ac80e8e42..199669957 100644 --- a/crates/horizon-client/src/game/mod_input.rs +++ b/crates/horizon-client/src/game/mod_input.rs @@ -1,6 +1,7 @@ //! Capability `input` (docs/mods.md): the use button on items a mod claims, -//! and the mod's own keys. With no mod claiming items or keys, each hook -//! here returns after one check. +//! the mod's own keys and the mouse wheel; and `render`'s zoom. With no mod +//! claiming items, keys or the wheel, each hook here returns after one +//! check. use super::Game; use winit::{event::MouseButton, keyboard::KeyCode}; #[cfg(feature = "mods")] @@ -20,6 +21,21 @@ impl Game<'_> { false } pub(super) fn mod_key(&mut self, _key: KeyCode, _pressed: bool, _repeat: bool) {} + pub(super) fn mod_mouse_key(&mut self, _button: MouseButton, _pressed: bool) {} + pub(super) fn mod_wheel(&mut self, _steps: f32) -> bool { + false + } + pub(super) fn mod_zoom(&self) -> Option { + None + } +} + +/// A mod's zoom (`render.set-zoom`), clamped by the runtime. +#[derive(Clone, Copy)] +pub(super) struct ModZoom { + pub(super) factor: f32, + pub(super) smooth: bool, + pub(super) turn: f32, } #[cfg(feature = "mods")] @@ -89,4 +105,46 @@ impl Game<'_> { self.mods.mods.key(name, pressed); } } + /// A mouse button while playing: a mod's key bound to it + /// (`key.mouse.4`...), as `mod_key`. + pub(super) fn mod_mouse_key(&mut self, button: MouseButton, pressed: bool) { + if self.mods.mods.has_keys() { + let name = horizon_client::keybinds::mouse_name(button); + self.mods.mods.key(&name, pressed); + } + } + /// Wheel steps while playing: true when a mod holds the wheel, so the + /// hotbar doesn't move. + pub(super) fn mod_wheel(&mut self, steps: f32) -> bool { + self.mods.mods.wheel(steps) + } + /// Playing, with nothing over the world: mods' keys and zoom apply. + fn mod_playing(&self) -> bool { + self.screen == Screen::Playing + && self.title.is_none() + && self.sign_edit.is_none() + && self.book.is_none() + } + /// Once a frame, before the mods run: a screen that opened lets their + /// held keys up. + pub(super) fn mod_input_frame(&mut self) { + let playing = self.mod_playing(); + if self.mods.playing && !playing { + self.mods.mods.release_keys(); + } + self.mods.playing = playing; + } + /// The strongest zoom a mod asks for, while playing; the spyglass's + /// own view wins. + pub(super) fn mod_zoom(&self) -> Option { + if !self.mod_playing() || self.scoping() { + return None; + } + let z = self.mods.mods.zoom()?; + Some(ModZoom { + factor: z.factor, + smooth: z.smooth, + turn: z.turn, + }) + } } diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs index 9d6015ad7..63e6314fd 100644 --- a/crates/horizon-client/src/game/mods.rs +++ b/crates/horizon-client/src/game/mods.rs @@ -55,6 +55,9 @@ pub(super) struct ModsState { /// The HUD font's advances for `render.text-width`, made once. #[cfg(feature = "mods")] font_widths: std::sync::Arc<[f32]>, + /// Playing at the last frame, for `mod_input_frame`. + #[cfg(feature = "mods")] + pub(super) playing: bool, } /// The mappings `input.mapping` answers for, with the default key their @@ -399,6 +402,7 @@ impl Game<'_> { if !self.mods.mods.is_running() { return; } + self.mod_input_frame(); if self.mods.mods.wants_frames() { let snapshot = self.mod_snapshot(); self.mods.mods.set_snapshot(snapshot); diff --git a/crates/horizon-mod-sdk/wit/horizon.wit b/crates/horizon-mod-sdk/wit/horizon.wit index b787ff675..5cee9ef51 100644 --- a/crates/horizon-mod-sdk/wit/horizon.wit +++ b/crates/horizon-mod-sdk/wit/horizon.wit @@ -199,9 +199,13 @@ interface input { /// It came up again, or the item left the hand, or a screen opened. use-stop(item-use), /// A key `register-key` named went down or up while playing (no - /// screen open). The game's own mapping on that key still runs. + /// screen open). The game's own mapping on that key still runs. A + /// screen opening lets every held key up. key-down(string), key-up(string), + /// Mouse wheel steps while the mod holds the wheel (`claim-wheel`): + /// positive away from the player, as the hotbar reads them. + wheel(f32), } /// The items whose use the mod handles instead of the game, by id (at /// most 64). Replaces the previous list. @@ -214,6 +218,10 @@ interface input { held: func() -> option; /// The events since the last call, oldest first. poll: func() -> list; + /// Takes the mouse wheel while playing (true) or gives it back: while + /// taken, its steps are the mod's `wheel` events and the hotbar doesn't + /// move. The game gives it back when the mod stops. + claim-wheel: func(on: bool); /// A mapping as the player has bound it. record mapping-state { /// The bound key's name as Controls shows it: `W`, `Space`, `LMB`. @@ -331,6 +339,21 @@ interface render { } /// The game's frame counter, recomputed twice a second. frame-stats: func() -> frame-rate; + record zoom { + /// The field of view is divided by it: 1 is none, at most 10. + factor: f32, + /// Ease in and out through the game's field-of-view easing, as the + /// spyglass does, instead of at once. + smooth: bool, + /// Mouse turning is multiplied by it while zoomed: 1 keeps the + /// player's sensitivity, at least 0.05. + turn: f32, + } + /// Zooms the camera until the next call (`factor` 1 ends it), only + /// while playing: a screen opening or the spyglass's own view (which + /// wins) leave the camera vanilla's. With several mods, the strongest + /// zoom counts. + set-zoom: func(zoom: zoom); } /// What the game calls. Only the hooks `init` asks for are ever called. diff --git a/crates/horizon-mods/src/manifest.rs b/crates/horizon-mods/src/manifest.rs index 9e998dae1..4dfd9af51 100644 --- a/crates/horizon-mods/src/manifest.rs +++ b/crates/horizon-mods/src/manifest.rs @@ -132,9 +132,9 @@ impl Capability { Capability::Storage => "save its own settings to disk", Capability::Registries => "add blocks and items to your worlds", Capability::Ui => "show its own screens and a settings page", - Capability::Render => "draw on your screen and shake the camera", + Capability::Render => "draw on your screen, shake and zoom the camera", Capability::Input => { - "see the keys you hold and your clicks, take over the use button on its own items, and add keys" + "see the keys you hold and your clicks, take over the use button on its own items and the mouse wheel, and add keys" } } } diff --git a/crates/horizon-mods/src/runtime.rs b/crates/horizon-mods/src/runtime.rs index f33839fe7..538b7edd5 100644 --- a/crates/horizon-mods/src/runtime.rs +++ b/crates/horizon-mods/src/runtime.rs @@ -53,7 +53,7 @@ pub use render_bindings::horizon::mods::input::{Event as InputEvent, HeldItem, I /// What capability `render` asks the game to draw (WIT `interface render`). pub use render_bindings::horizon::mods::render::{ Corner as RenderCorner, FillShape, FrameRate, ItemShape, Point as RenderPoint, QuadShape, - Rect as RenderRect, Shape, SpriteShape, TextShape, + Rect as RenderRect, Shape, SpriteShape, TextShape, Zoom as ModZoom, }; use render_bindings::{RenderHost, horizon::mods::render}; @@ -189,6 +189,8 @@ struct State { shapes: Vec, /// Capability `render`'s corner panel. corner: Option, + /// Capability `render`'s zoom, when the mod zooms. + zoom: Option, input: InputState, /// Capability `storage`'s folder; made on first `set`. data_dir: PathBuf, @@ -331,6 +333,7 @@ impl Mods { ui: UiState::default(), shapes: Vec::new(), corner: None, + zoom: None, input: InputState::default(), data_dir, storage_budget: STORAGE_WRITE_BUDGET, @@ -587,6 +590,16 @@ impl Mods { .map(|k| k.name.clone()) .collect(); for name in names { + let held = input.down.iter().position(|n| *n == name); + match (down, held) { + (true, None) => input.down.push(name.clone()), + (false, Some(i)) => { + input.down.swap_remove(i); + } + // Already let go when a screen opened. + (false, None) => continue, + (true, Some(_)) => {} + } input.push(if down { InputEvent::KeyDown(name) } else { @@ -595,6 +608,35 @@ impl Mods { } } } + /// A screen opened, or the game stopped playing: every mod key held + /// lets go (`key-up`), so a key needs a new press afterwards. + pub fn release_keys(&mut self) { + for run in self.entries.iter_mut().filter_map(|e| e.run.as_mut()) { + let input = &mut run.store.data_mut().input; + for name in std::mem::take(&mut input.down) { + input.push(InputEvent::KeyUp(name)); + } + } + } + /// Some running mod holds the mouse wheel. + pub fn wheel_claimed(&self) -> bool { + self.inputs().any(|i| i.wheel) + } + /// Wheel steps while playing: `wheel` to the mods holding it; false + /// when none does (the hotbar takes them). + pub fn wheel(&mut self, steps: f32) -> bool { + let event = InputEvent::Wheel(steps); + let claimed = self.wheel_claimed(); + self.push_input(|i| i.wheel, &event); + claimed + } + /// The strongest zoom a running mod asks for (capability `render`). + pub fn zoom(&self) -> Option { + self.entries + .iter() + .filter_map(|e| e.run.as_ref()?.store.data().zoom) + .max_by(|a, b| a.factor.total_cmp(&b.factor)) + } fn inputs(&self) -> impl Iterator { self.entries .iter() @@ -824,6 +866,10 @@ struct InputState { claimed: Vec, keys: Vec, events: std::collections::VecDeque, + /// The mod's keys held now, by name. + down: Vec, + /// It holds the mouse wheel. + wheel: bool, } /// Events kept for `poll`, items claimed and keys, per mod. const INPUT_EVENTS: usize = 256; @@ -889,12 +935,19 @@ impl input::Host for State { fn clicks_per_second(&mut self) -> (u32, u32) { self.snapshot.cps } + fn claim_wheel(&mut self, on: bool) { + self.input.wheel = on; + } } /// HUD shapes one mod may keep (capability `render`). const SHAPES: usize = 1024; /// The longest jolt `render.shake` makes, in seconds. const SHAKE_SECONDS: f32 = 10.0; +/// `render.set-zoom`'s limits: `Camera.tickFov` keeps the modifier at 0.1 +/// or more, and turning never stops. +const ZOOM_MAX: f32 = 10.0; +const TURN_MIN: f32 = 0.05; /// A mod's corner panel: shapes in a `width` × `height` box the game /// places in `corner`, stacked with the other overlays. @@ -963,6 +1016,19 @@ impl render::Host for State { .sum::() .ceil() } + fn set_zoom(&mut self, zoom: ModZoom) { + let finite = |v: f32, lo: f32, hi: f32| if v.is_finite() { v.clamp(lo, hi) } else { lo }; + let factor = finite(zoom.factor, 1.0, ZOOM_MAX); + self.zoom = (factor > 1.0).then(|| ModZoom { + factor, + smooth: zoom.smooth, + turn: if zoom.turn.is_finite() { + zoom.turn.clamp(TURN_MIN, 1.0) + } else { + 1.0 + }, + }); + } fn frame_stats(&mut self) -> FrameRate { let (fps, min, low) = self.snapshot.frame; FrameRate { fps, min, low } diff --git a/crates/horizon-mods/tests/sandbox.rs b/crates/horizon-mods/tests/sandbox.rs index a9de4f55b..780899289 100644 --- a/crates/horizon-mods/tests/sandbox.rs +++ b/crates/horizon-mods/tests/sandbox.rs @@ -506,6 +506,56 @@ fn input_claims_keys_and_events() { }); assert!(mods.command("input_held")); assert_eq!(chat(&mut mods), ["holding minecraft:stick x3 in 2"]); + // A screen opening lets held keys up, once; the release that follows + // is not sent again. + mods.key("key.keyboard.r", true); + mods.release_keys(); + mods.release_keys(); + mods.key("key.keyboard.r", false); + assert!(mods.command("input_poll")); + assert_eq!(chat(&mut mods), ["key-down wheel", "key-up wheel"]); +} + +#[test] +fn input_wheel_is_the_mods_while_claimed() { + let mut mods = load("horizon_mods_test_ui_guest.hmod"); + let chat = |mods: &mut Mods| -> Vec { + mods.drain() + .into_iter() + .filter_map(|o| match o { + Output::Chat { text, .. } => Some(text), + _ => None, + }) + .collect() + }; + // Unclaimed, the hotbar keeps the wheel and the mod hears nothing. + assert!(!mods.wheel_claimed() && !mods.wheel(1.0)); + assert!(mods.command("input_wheel")); + assert!(mods.wheel_claimed()); + assert!(mods.wheel(1.0) && mods.wheel(-2.0)); + assert!(mods.command("input_poll")); + assert_eq!(chat(&mut mods), ["wheel 1", "wheel -2"]); + assert!(mods.command("input_wheel off")); + assert!(!mods.wheel(1.0)); +} + +#[test] +fn render_zoom_is_clamped_and_the_strongest_counts() { + let mut mods = load("horizon_mods_test_ui_guest.hmod"); + let zoom = |mods: &mut Mods, args: &str| { + assert!(mods.command(&format!("zoom {args}"))); + mods.zoom().map(|z| (z.factor, z.smooth, z.turn)) + }; + assert!(mods.zoom().is_none()); + assert_eq!(zoom(&mut mods, "4 1 0.25"), Some((4.0, true, 0.25))); + // Over 10 and a turn under 0.05 are clamped. + assert_eq!(zoom(&mut mods, "50 0 0"), Some((10.0, false, 0.05))); + // A NaN turn keeps the player's sensitivity. + assert_eq!(zoom(&mut mods, "2 0 x"), Some((2.0, false, 1.0))); + // 1, under 1 and NaN end the zoom. + assert_eq!(zoom(&mut mods, "1 0 1"), None); + assert_eq!(zoom(&mut mods, "0.5 0 1"), None); + assert_eq!(zoom(&mut mods, "x 0 1"), None); } #[test] diff --git a/crates/horizon-mods/tests/ui_guest/src/lib.rs b/crates/horizon-mods/tests/ui_guest/src/lib.rs index ae6e4dfc4..1555b44f0 100644 --- a/crates/horizon-mods/tests/ui_guest/src/lib.rs +++ b/crates/horizon-mods/tests/ui_guest/src/lib.rs @@ -2,7 +2,8 @@ //! of each widget, `/ui_settings` sets a settings page, `/ui_close` closes //! the screen; the `ui-events` calls answer in chat so the host sees them. //! `/render_hud`, `/render_clear` and `/render_shake` test `render`; -//! `/input_claim`, `/input_poll` and `/input_held` test `input`; +//! `/input_claim`, `/input_poll`, `/input_held` and `/input_wheel` test +//! `input`; `/zoom FACTOR SMOOTH TURN` tests `render.set-zoom`; //! `/corner` and `/readings` the calls the official extras mods use. use horizon_mod_sdk::{ Mod, Registration, chat, @@ -93,6 +94,8 @@ impl Mod for Guest { "input_claim", "input_poll", "input_held", + "input_wheel", + "zoom", "corner", "readings", ] @@ -100,7 +103,7 @@ impl Mod for Guest { .fold(Registration::new(), Registration::command); Ok((Guest, hooks)) } - fn command(&mut self, name: &str, _args: &str) -> Result, String> { + fn command(&mut self, name: &str, args: &str) -> Result, String> { match name { "ui_screen" => ui::open_screen(&Screen { title: "Guest's screen".into(), @@ -167,6 +170,7 @@ impl Mod for Guest { Event::UseStop(u) => format!("use-stop {} {}", u.item.id, u.ticks), Event::KeyDown(k) => format!("key-down {k}"), Event::KeyUp(k) => format!("key-up {k}"), + Event::Wheel(steps) => format!("wheel {steps}"), }); } } @@ -176,6 +180,19 @@ impl Mod for Guest { }); chat::show(&format!("holding {held}")); } + "input_wheel" => input::claim_wheel(args != "off"), + "zoom" => { + let mut words = args.split(' '); + let mut number = || words.next().and_then(|w| w.parse().ok()).unwrap_or(f32::NAN); + let factor = number(); + let smooth = number() == 1.0; + let turn = number(); + render::set_zoom(render::Zoom { + factor, + smooth, + turn, + }); + } // Clamped to (1, 10); the two after it shake nothing. "render_shake" => { render::shake(2.0, 99.0); diff --git a/docs/api/mods-reference.md b/docs/api/mods-reference.md index 333b65bd0..209a8298d 100644 --- a/docs/api/mods-reference.md +++ b/docs/api/mods-reference.md @@ -13,8 +13,8 @@ The contract between Horizon and its mods: the WIT package `horizon:mods@0.1.0`, | `storage` | [`storage`](#storage) | save its own settings to disk | | `registries` | data files (`assets/`, `data/`), no interface | add blocks and items to your worlds | | `ui` | [`ui`](#ui) | show its own screens and a settings page | -| `render` | [`render`](#render) | draw on your screen and shake the camera | -| `input` | [`input`](#input) | see the keys you hold and your clicks, take over the use button on its own items, and add keys | +| `render` | [`render`](#render) | draw on your screen, shake and zoom the camera | +| `input` | [`input`](#input) | see the keys you hold and your clicks, take over the use button on its own items and the mouse wheel, and add keys | ## World `horizon-mod` @@ -251,8 +251,9 @@ The main hand's stack. | --- | --- | --- | | `use-start` | `item-use` | The use button went down with a claimed item in the main hand. The game does nothing else with the click: no placing, no eating, no swing. | | `use-stop` | `item-use` | It came up again, or the item left the hand, or a screen opened. | -| `key-down` | `string` | A key `register-key` named went down or up while playing (no screen open). The game's own mapping on that key still runs. | +| `key-down` | `string` | A key `register-key` named went down or up while playing (no screen open). The game's own mapping on that key still runs. A screen opening lets every held key up. | | `key-up` | `string` | | +| `wheel` | `f32` | Mouse wheel steps while the mod holds the wheel (`claim-wheel`): positive away from the player, as the hotbar reads them. | ### record `mapping-state` @@ -269,6 +270,7 @@ A mapping as the player has bound it. | `register-key` | `func(name: string, label: string, key: string) -> result<_, string>` | A key of the mod's: `name` comes back in `key-down` and `key-up`, `label` says what it does, `key` is the key as options.txt writes it (`key.keyboard.r`). At most 16; an unknown key is an error. | | `held` | `func() -> option` | The main hand's stack, as of this tick; none when it is empty. | | `poll` | `func() -> list` | The events since the last call, oldest first. | +| `claim-wheel` | `func(on: bool)` | Takes the mouse wheel while playing (true) or gives it back: while taken, its steps are the mod's `wheel` events and the hotbar doesn't move. The game gives it back when the mod stops. | | `mapping` | `func(name: string) -> option` | A movement or click mapping as the player has bound it: `key.forward`, `key.left`, `key.back`, `key.right`, `key.jump`, `key.sneak`, `key.sprint`, `key.attack` or `key.use`. None for another name. | | `clicks-per-second` | `func() -> tuple` | Clicks of the attack and use buttons in the last second. | @@ -369,6 +371,14 @@ Any four-cornered shape (a wheel's segment, an arrow): the corners in order roun | `min` | `u32` | The slowest frame's rate, and the 1% low (the 99th percentile frame time's rate), over the last 5 seconds. | | `low` | `u32` | | +### record `zoom` + +| Field | Type | | +| --- | --- | --- | +| `factor` | `f32` | The field of view is divided by it: 1 is none, at most 10. | +| `smooth` | `bool` | Ease in and out through the game's field-of-view easing, as the spyglass does, instead of at once. | +| `turn` | `f32` | Mouse turning is multiplied by it while zoomed: 1 keeps the player's sensitivity, at least 0.05. | + | Function | Signature | What it does | | --- | --- | --- | | `set-hud` | `func(shapes: list)` | Replaces the mod's HUD shapes: up to 1024, drawn every frame in order over the HUD (after the hotbar, under chat and screens), hidden with the HUD (F1). An empty list removes them. | @@ -378,6 +388,7 @@ Any four-cornered shape (a wheel's segment, an arrow): the corners in order roun | `clear-corner` | `func()` | Removes the mod's corner panel. | | `text-width` | `func(text: string) -> f32` | The width of one line of text in GUI pixels, in the HUD's font: exact for Latin letters (U+0000 to U+024F), 6 per other character. | | `frame-stats` | `func() -> frame-rate` | The game's frame counter, recomputed twice a second. | +| `set-zoom` | `func(zoom: zoom)` | Zooms the camera until the next call (`factor` 1 ends it), only while playing: a screen opening or the spyglass's own view (which wins) leave the camera vanilla's. With several mods, the strongest zoom counts. | ## `events` diff --git a/docs/api/stability.md b/docs/api/stability.md index b513d5ddf..485d805af 100644 --- a/docs/api/stability.md +++ b/docs/api/stability.md @@ -88,8 +88,8 @@ Deprecations and breaking changes are in bold. The release notes of the game rel | --- | --- | --- | | Mod API: `log`, `game`, `chat`, `hud`, `storage`, `events` | preview | 0.1.0 | | Mod API: `ui`, `ui-events` (a mod's screens and settings page) | experimental | manifest `experimental = ["ui"]` | -| Mod API: `render` (HUD shapes, camera shake; drawn by the game, no mod shaders) | experimental | manifest `experimental = ["render"]` | -| Mod API: `input` (claimed items' use, the mod's keys) | experimental | manifest `experimental = ["input"]` | +| Mod API: `render` (HUD shapes, camera shake and zoom; drawn by the game, no mod shaders) | experimental | manifest `experimental = ["render"]` | +| Mod API: `input` (claimed items' use, the mod's keys, the mouse wheel) | experimental | manifest `experimental = ["input"]` | | Content mods (`registries`: data-driven blocks and items) | preview | manifest `api = "0.2"` | | Server mods (events, world, server commands) | not started | manifest `api = "0.3"`, experimental first | | Script mods (Lua run by an interpreter mod, [mods.md](mods.md#ways-in)) | not started | | @@ -104,3 +104,12 @@ Deprecations and breaking changes are in bold. The release notes of the game rel | Telemetry batch and consent | stable | `"schema": 1`, consent `2` | | `horizon-graphics.txt`, `horizon-extras.txt`, `horizon-mods.txt` | preview | none | | `SHA256SUMS` and `SHA256SUMS.sig` | stable | | + +### The experimental mod APIs' choices + +- **What a mod may take from the game.** `input.claim-wheel` is the one input a mod consumes: while it holds the + wheel, the steps are its `wheel` events and the hotbar doesn't move. `claim-items` takes the use button only on the + items it names. A mod's keys (`register-key`) never consume: vanilla's mapping on the same key still runs. Whether + keys may consume too is decided before `input` becomes preview. +- **`input`'s `wheel` event** is a new case in a variant the game sends, so it breaks mods built before it (allowed + because `input` is experimental): an `input` mod that calls `poll` needs a rebuild against the new WIT. diff --git a/docs/mods.md b/docs/mods.md index d92c88fa6..9a75badb6 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -87,8 +87,8 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | `hud` | `hud` | `set-panel(corner, lines)`, `clear()`: up to 16 lines of 256 characters | | `storage` | `storage` | `get(key)`, `set(key, value)`, `delete(key)`: a per-mod folder of up to 64 files, 4 MiB total, at most 1 MiB written per call | | `ui` | `ui` | `open-screen`, `close-screen`, `set-settings`: a screen the mod owns and a page in Options > Mods, of text, button, item and list widgets the game lays out and draws with its own menus' code. **Experimental:** the manifest needs `experimental = ["ui"]` | -| `render` | `render` | `set-hud(shapes)`: up to 1024 fills, four-cornered shapes, GUI sprites, items and text, kept on the HUD (over the hotbar, under chat and screens, hidden with F1) until replaced; `set-corner(corner, w, h, shapes)`: a box the game places in a corner, stacked with its overlays and other panels, as `hud`'s panels are; `text-width(text)`; `frame-stats()`: the game's FPS, min and 1% low; `gui-size()`; `shake(strength, seconds)`: the view turns, the player doesn't, scaled by Distortion Effects. The game's own HUD renderer draws them: a mod never supplies shader code. **Experimental:** `experimental = ["render"]` | -| `input` | `input` | `claim-items(ids)`: the use button on those items is the mod's (`use-start`, `use-stop` with the ticks held; the game doesn't place, eat or swing); `register-key(name, label, key)`: up to 16 keys (`key.keyboard.r`), heard while playing as `key-down` / `key-up` (the game's own mapping on that key still runs); `held()`; `mapping(name)`: a movement or click mapping's key label and whether it is held; `clicks-per-second()`; `poll()` returns the events, at most 256 kept. **Experimental:** `experimental = ["input"]` | +| `render` | `render` | `set-hud(shapes)`: up to 1024 fills, four-cornered shapes, GUI sprites, items and text, kept on the HUD (over the hotbar, under chat and screens, hidden with F1) until replaced; `set-corner(corner, w, h, shapes)`: a box the game places in a corner, stacked with its overlays and other panels, as `hud`'s panels are; `text-width(text)`; `frame-stats()`: the game's FPS, min and 1% low; `gui-size()`; `shake(strength, seconds)`: the view turns, the player doesn't, scaled by Distortion Effects; `set-zoom(zoom)`: the field of view divided by up to 10, eased or at once, with slower turning, while playing and not through a spyglass (the strongest of several mods' zooms counts). The game's own HUD renderer draws them: a mod never supplies shader code. **Experimental:** `experimental = ["render"]` | +| `input` | `input` | `claim-items(ids)`: the use button on those items is the mod's (`use-start`, `use-stop` with the ticks held; the game doesn't place, eat or swing); `register-key(name, label, key)`: up to 16 keys (`key.keyboard.r`), heard while playing as `key-down` / `key-up` (the game's own mapping on that key still runs; a screen opening lets held keys up), mouse buttons as `key.mouse.4`; `claim-wheel(on)`: the wheel's steps become `wheel` events and the hotbar doesn't move (the one input a mod takes from the game; a mod's keys never do); `held()`; `mapping(name)`: a movement or click mapping's key label and whether it is held; `clicks-per-second()`; `poll()` returns the events, at most 256 kept. **Experimental:** `experimental = ["input"]` | | Hook (export) | When | |---|---| From 2c612fd791c787ca4415439dfa9fc696798395af Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 03:10:55 +0100 Subject: [PATCH 228/238] ci: parity-main on the release runner, not behind PR parity jobs The queued parity-main run waited 2 h behind PR parity jobs on the two horizon-game runners, each merge replacing the pending one, and releases need it green. The release runner gets a horizon-parity-main label; a release only starts after parity-main passed, so the two never compete. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/parity-main.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/parity-main.yml b/.github/workflows/parity-main.yml index 1debcc694..13be3c36d 100644 --- a/.github/workflows/parity-main.yml +++ b/.github/workflows/parity-main.yml @@ -23,9 +23,11 @@ env: jobs: parity-main: - # The Mac mini's runners only: a desktop job slowed down because the maintainer came back could time out, and a - # failure here bisects and comments on merges. - runs-on: [self-hosted, horizon-game, macOS] + # The Mac mini's release runner (label added 2026-10-26), not the two `horizon-game` runners: behind PR parity jobs + # this run waited 2 h while every merge replaced the pending one, and releases wait for it. Releases need it green + # first, so the two never compete. Not the desktop: a job slowed down because the maintainer came back could time + # out, and a failure here bisects and comments on merges. + runs-on: [self-hosted, macOS, horizon-parity-main] timeout-minutes: 240 # a core-crate batch reruns ~300 harnesses (~60-90 min), a bisect a few rounds of the failures steps: - uses: actions/checkout@v4 From 0c84eb30793b461072407eed9bb8c60db480fe19 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 03:18:39 +0100 Subject: [PATCH 229/238] ci: PR parity jobs prepare vanilla data with main's script A PR head older than the fix in vanilla-data.sh left a real .local/horizon-client without sounds.json on runner ludovics-mac-mini; every later job there failed subtitle. Like the harness selection, the job now runs main's script unless the branch changes it. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/parity.yml | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/.github/workflows/parity.yml b/.github/workflows/parity.yml index 577380338..b48be1d05 100644 --- a/.github/workflows/parity.yml +++ b/.github/workflows/parity.yml @@ -36,7 +36,19 @@ jobs: - name: Tools from the runner run: bash tools/ci/runner-env.sh - name: Vanilla data, and resources prepared by this preparer - run: bash tools/ci/vanilla-data.sh + env: + BASE_SHA: ${{ inputs.base }} + run: | + # main's script: one written before a fix to it left a real .local/horizon-client without sounds.json on + # the runner, and every later job there failed `subtitle`. A branch that changes the script runs its own. + set -euo pipefail + if git rev-parse --verify -q origin/main >/dev/null \ + && git diff --quiet "$(git merge-base "$BASE_SHA" HEAD)" HEAD -- tools/ci/vanilla-data.sh; then + git show origin/main:tools/ci/vanilla-data.sh > "$RUNNER_TEMP/vanilla-data.sh" + bash "$RUNNER_TEMP/vanilla-data.sh" + else + bash tools/ci/vanilla-data.sh + fi - name: The prepared output is the one the preparer's OUTPUT_VERSION says env: BASE_SHA: ${{ inputs.base }} From 7d4792fb3166b83674b2abf048293908deb7cd9b Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 04:03:03 +0100 Subject: [PATCH 230/238] Mods API reference test: follow wit-parser 0.254 (FixedLengthList, Param) Co-Authored-By: Claude Sonnet 5.5 --- crates/horizon-mods/tests/api_reference.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/horizon-mods/tests/api_reference.rs b/crates/horizon-mods/tests/api_reference.rs index 5e6a0a069..b3d2363a9 100644 --- a/crates/horizon-mods/tests/api_reference.rs +++ b/crates/horizon-mods/tests/api_reference.rs @@ -52,7 +52,7 @@ fn definition(resolve: &Resolve, kind: &TypeDefKind) -> String { match kind { TypeDefKind::Option(t) => format!("option<{}>", ty(resolve, t)), TypeDefKind::List(t) => format!("list<{}>", ty(resolve, t)), - TypeDefKind::FixedSizeList(t, n) => format!("list<{}, {n}>", ty(resolve, t)), + TypeDefKind::FixedLengthList(t, n) => format!("list<{}, {n}>", ty(resolve, t)), TypeDefKind::Result(r) => match (&r.ok, &r.err) { (None, None) => "result".into(), (ok, None) => format!("result<{}>", of(ok)), @@ -82,7 +82,7 @@ fn signature(resolve: &Resolve, f: &Function) -> String { let params: Vec<_> = f .params .iter() - .map(|(name, t)| format!("{name}: {}", ty(resolve, t))) + .map(|p| format!("{}: {}", p.name, ty(resolve, &p.ty))) .collect(); let result = f .result From c89434557b2a60283485d28fea0ed0b6806c6fc3 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 04:09:25 +0100 Subject: [PATCH 231/238] Player heads without a skin show slim steve, as vanilla SkullBlockRenderer's SKIN_BY_TYPE is DefaultPlayerSkin.getDefaultTexture(), DEFAULT_SKINS[6] = entity/player/slim/steve; the block_entity_render harness found Horizon drawing nothing for heads without a profile. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/block_entity_render.rs | 4 ++-- crates/horizon-client/src/containers.rs | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/horizon-client/src/block_entity_render.rs b/crates/horizon-client/src/block_entity_render.rs index 620640188..10c5e2509 100644 --- a/crates/horizon-client/src/block_entity_render.rs +++ b/crates/horizon-client/src/block_entity_render.rs @@ -732,7 +732,7 @@ pub struct Scene<'a> { pub banner: &'a dyn Fn([i32; 3]) -> Vec<(String, u32)>, /// The skin sprite a `player_head` shows at a position (`SkullBlockRenderer` /// profile → skin): a downloaded `skins/` slot or the profile's - /// `DefaultPlayerSkin` fallback; None keeps `SKIN_BY_TYPE` (steve). + /// `DefaultPlayerSkin` fallback; None keeps `SKIN_BY_TYPE` (`DefaultPlayerSkin.getDefaultTexture`, slim steve). pub skull: &'a dyn Fn([i32; 3]) -> Option<&'a str>, /// Font glyph cell (none for blank glyphs such as the space) and advance. pub glyph: &'a dyn Fn(char) -> (Option, f32), @@ -1281,7 +1281,7 @@ impl BlockEntityRenderer { SkullType::Creeper => ("creeper_head", "entity/creeper/creeper"), SkullType::Dragon => ("dragon_skull", "entity/enderdragon/dragon"), SkullType::Piglin => ("piglin_head", "entity/piglin/piglin"), - SkullType::Player => ("player_head", "entity/player/wide/steve"), + SkullType::Player => ("player_head", "entity/player/slim/steve"), }; // SkullBlockRenderer.getRenderType: the profile's skin when it // resolves, `SKIN_BY_TYPE` (steve) otherwise. diff --git a/crates/horizon-client/src/containers.rs b/crates/horizon-client/src/containers.rs index 1f8fad151..a968408db 100644 --- a/crates/horizon-client/src/containers.rs +++ b/crates/horizon-client/src/containers.rs @@ -123,8 +123,9 @@ impl SkullSkin { let location = crate::skins::skin_location(&crate::skins::texture_hash(&url)?); Some((url, location)) }); + // DefaultPlayerSkin.get(id), or getDefaultSkin (slim steve) without an id. let fallback = crate::skins::profile_uuid_nbt(profile) - .map_or("entity/player/wide/steve", crate::tab_list::default_skin); + .map_or("entity/player/slim/steve", crate::tab_list::default_skin); Self { download, fallback } } } From de294e34bbddb7bb2e8dc48c86efe1c662421e53 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 04:19:07 +0100 Subject: [PATCH 232/238] rustfmt Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/extras.rs | 6 +++++- crates/horizon-mods/tests/ui_guest/src/lib.rs | 7 ++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/game/extras.rs b/crates/horizon-client/src/game/extras.rs index 1978d003f..a04196b69 100644 --- a/crates/horizon-client/src/game/extras.rs +++ b/crates/horizon-client/src/game/extras.rs @@ -234,7 +234,11 @@ impl Game<'_> { fn zoom_now(&self) -> (f32, bool, f64) { let factor = self.zoom_factor(); let e = &self.settings.extras; - let own = (factor, e.zoom_smooth, turn_scale(factor, e.zoom_sensitivity)); + let own = ( + factor, + e.zoom_smooth, + turn_scale(factor, e.zoom_sensitivity), + ); match self.mod_zoom() { Some(z) if z.factor > factor => (z.factor, z.smooth, f64::from(z.turn)), _ => own, diff --git a/crates/horizon-mods/tests/ui_guest/src/lib.rs b/crates/horizon-mods/tests/ui_guest/src/lib.rs index 1555b44f0..9bede11f9 100644 --- a/crates/horizon-mods/tests/ui_guest/src/lib.rs +++ b/crates/horizon-mods/tests/ui_guest/src/lib.rs @@ -183,7 +183,12 @@ impl Mod for Guest { "input_wheel" => input::claim_wheel(args != "off"), "zoom" => { let mut words = args.split(' '); - let mut number = || words.next().and_then(|w| w.parse().ok()).unwrap_or(f32::NAN); + let mut number = || { + words + .next() + .and_then(|w| w.parse().ok()) + .unwrap_or(f32::NAN) + }; let factor = number(); let smooth = number() == 1.0; let turn = number(); From 2256941af73b77d540de4b3fa74351f45ac0deee Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 04:23:25 +0100 Subject: [PATCH 233/238] Chat: mod output lines are the client's own Main's mod output calls Chat::add without the new source; a mod's line is written by this client, so the Chat option keeps it. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/mods.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs index 667c06335..fbf92237d 100644 --- a/crates/horizon-client/src/game/mods.rs +++ b/crates/horizon-client/src/game/mods.rs @@ -454,10 +454,16 @@ impl Game<'_> { } }; // Not through system_message: a mod must not hear its own lines. + // Written by this client, so the Chat option doesn't hide it. let focused = self.screen == Screen::Chat; - self.ui - .chat - .add(&self.gui.atlas, line, None, self.ticks, focused); + self.ui.chat.add( + &self.gui.atlas, + line, + None, + horizon_client::chat::ChatSource::Client, + self.ticks, + focused, + ); } // A mod may set or clear its settings page in any call, not only // while loading: its row's Settings button follows. From 7e4012c83ec859b5fa18f6f5975e49f97719854a Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 04:41:12 +0100 Subject: [PATCH 234/238] Mod API: a key widget, so players bind a mod's keys (ui, input) A ui page's key widget shows one of the mod's keys as Key Binds does ("Zoom: Z"). A click waits for the next key or mouse button, which becomes the key; Esc binds none. The game keeps the choice in horizon-mods.txt (key..:key.keyboard.z), applies it when the mods load and to keys registered later, and lets a held key go when it is rebound. register-key's key is now the default. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 7 +- crates/horizon-client/src/game/mods.rs | 123 +++++++++++++++++- crates/horizon-client/src/mod_list.rs | 52 +++++++- crates/horizon-mod-sdk/wit/horizon.wit | 15 ++- crates/horizon-mods/src/runtime.rs | 67 +++++++++- crates/horizon-mods/tests/sandbox.rs | 41 +++++- crates/horizon-mods/tests/ui_guest/src/lib.rs | 11 +- docs/api/mods-reference.md | 12 +- docs/mods.md | 2 +- 9 files changed, 312 insertions(+), 18 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 3f7450501..bb68dc590 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -10451,6 +10451,9 @@ impl Game<'_> { } return Ok(()); } + if self.screen == Screen::Mod && self.mod_screen_key(key) { + return Ok(()); + } if self.sort_key_press(key) { return Ok(()); } @@ -11363,9 +11366,7 @@ impl ApplicationHandler for Game<'_> { .map_or([1, 1], |r| [r.config.width, r.config.height]); self.stats_click(size); } - Screen::Mod if pressed && button == MouseButton::Left => { - self.mod_screen_click() - } + Screen::Mod if pressed => self.mod_screen_click(button), _ => {} } } diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs index 6211f43b1..66d00b233 100644 --- a/crates/horizon-client/src/game/mods.rs +++ b/crates/horizon-client/src/game/mods.rs @@ -29,6 +29,7 @@ use { }, horizon_mods::{Clock, Mods, Output, Player, Snapshot, Status, UiScreen, UiWidget}, std::time::Instant, + winit::{event::MouseButton, keyboard::KeyCode}, }; #[derive(Default)] @@ -101,6 +102,8 @@ struct ModScreen { under: Screen, /// One scroll per `list` widget, in widget order. scrolls: Vec, + /// A key widget waiting for the key to bind: the mod key's name. + binding: Option, } impl ModsState { /// The chat's command tree: vanilla's, plus the running mods' commands. @@ -240,7 +243,10 @@ impl Game<'_> { pub(super) fn open_mod_screen(&mut self, _owner: usize, _settings: bool) {} pub(super) fn close_mod_screen(&mut self, _tell: bool) {} pub(super) fn draw_mod_screen(&mut self, _batch: &mut HudBatch) {} - pub(super) fn mod_screen_click(&mut self) {} + pub(super) fn mod_screen_click(&mut self, _button: winit::event::MouseButton) {} + pub(super) fn mod_screen_key(&mut self, _key: winit::keyboard::KeyCode) -> bool { + false + } pub(super) fn mod_screen_scroll(&mut self, _delta: f32) {} } @@ -263,6 +269,8 @@ impl Game<'_> { let on = |id: &str| enabled.contains(id) || dev_ids.iter().any(|d| d == id); let mods = Mods::load(&paths, on, vanilla_command); self.mods.mods = mods; + let chosen = &self.settings.mods; + self.mods.mods.set_bindings(|id| chosen.keys_of(id)); // Entries were replaced wholesale: a screen's owner index is stale. self.mods.screen = None; self.list_mods(); @@ -429,6 +437,8 @@ impl Game<'_> { { self.mods.checked = Some(Instant::now()); if self.mods.mods.reload_changed(vanilla_command) { + let chosen = &self.settings.mods; + self.mods.mods.set_bindings(|id| chosen.keys_of(id)); self.list_mods(); self.sync_mod_commands(); self.system_message(Text::literal("Mods reloaded").with_color(text::GRAY)); @@ -579,6 +589,72 @@ impl Game<'_> { Some((s.title.as_str(), s.widgets.as_slice())) } } + /// A key widget's label as Key Binds shows one, "Zoom: Z", marked + /// "> Zoom: Z <" in yellow while it waits for the new key. + fn mod_key_label(mods: &Mods, owner: usize, name: &str, waiting: bool) -> Text { + let (label, key) = mods + .keys_of(owner) + .iter() + .find(|k| k.name == name) + .map_or((name, "key.keyboard.unknown"), |k| { + (k.label.as_str(), k.key.as_str()) + }); + let value = format!("{label}: {}", keybinds::display_name(key).plain()); + if waiting { + Text::literal(format!("> {value} <")).with_color(text::YELLOW) + } else { + Text::literal(value) + } + } + /// The player bound the key widget that waited to `key`: the mod's key + /// follows it now and at the next start (`horizon-mods.txt`). + fn bind_mod_key(&mut self, key: String) { + let Some(ms) = self.mods.screen.as_mut() else { + return; + }; + let Some(name) = ms.binding.take() else { + return; + }; + let owner = ms.owner; + if !self.mods.mods.rebind(owner, &name, &key) { + return; + } + let Some(id) = self + .mods + .mods + .entries() + .get(owner) + .and_then(|e| e.manifest.as_ref()) + .map(|m| m.id.clone()) + else { + return; + }; + self.settings + .mods + .keys + .entry(id) + .or_default() + .insert(name, key); + self.settings.save(&Self::options_path()); + } + /// A key while a mod screen is up: true when a key widget waited for + /// it (Key Binds: Escape binds none). + pub(super) fn mod_screen_key(&mut self, key: KeyCode) -> bool { + if self + .mods + .screen + .as_ref() + .is_none_or(|ms| ms.binding.is_none()) + { + return false; + } + let value = match key { + KeyCode::Escape => None, + k => keybinds::key_name(k), + }; + self.bind_mod_key(value.unwrap_or("key.keyboard.unknown").to_owned()); + true + } /// The layout the mod screen's widgets get at `h`: (widget, top, /// height), the column being `COLUMN` wide down the middle. fn mod_ui_layout(h: f32, widgets: &[UiWidget]) -> Vec<(f32, f32)> { @@ -587,7 +663,7 @@ impl Game<'_> { for w in widgets { let wh = match w { UiWidget::Text(_) => 12.0, - UiWidget::Item(_) | UiWidget::Button(_) => 20.0, + UiWidget::Item(_) | UiWidget::Button(_) | UiWidget::Key(_) => 20.0, UiWidget::List(l) => (l.rows.len().min(6) as f32 * 12.0) + 8.0, }; // Nothing may cover Done; a crowded mod loses what won't fit. @@ -617,6 +693,7 @@ impl Game<'_> { settings, under: self.screen, scrolls: Vec::new(), + binding: None, }); self.screen = Screen::Mod; if self.player.is_some() { @@ -697,6 +774,22 @@ impl Game<'_> { hover, ); } + UiWidget::Key(k) => { + let hover = ui::hovered(self.cursor, x, wy, MOD_UI_COLUMN, 20.0); + let waiting = ms.binding.as_deref() == Some(k.key.as_str()); + let label = Self::mod_key_label(&self.mods.mods, ms.owner, &k.key, waiting); + ui::button( + batch, + atlas, + x, + wy, + MOD_UI_COLUMN, + 20.0, + &label, + true, + hover, + ); + } UiWidget::Text(t) => { let color = if t.color == 0 { u32::MAX @@ -798,7 +891,20 @@ impl Game<'_> { } /// A click on the mod screen: Done leaves it, a `list` row is the mod's /// `ui-events::select`. - pub(super) fn mod_screen_click(&mut self) { + pub(super) fn mod_screen_click(&mut self, button: MouseButton) { + // Key Binds: a mouse button pressed while waiting binds it. + if self + .mods + .screen + .as_ref() + .is_some_and(|ms| ms.binding.is_some()) + { + self.bind_mod_key(keybinds::mouse_name(button)); + return; + } + if button != MouseButton::Left { + return; + } let (w, h) = self.gui_size(); let cursor = self.cursor; if ui::hovered(cursor, (w / 2.0).floor() - 100.0, h - 27.0, 200.0, 20.0) { @@ -829,6 +935,17 @@ impl Game<'_> { self.show_mod_output(); return; } + // A key widget waits for the key to bind. + if let UiWidget::Key(k) = w + && ui::hovered(cursor, x, wy, MOD_UI_COLUMN, 20.0) + { + self.audio.play_button_click(); + let name = k.key.clone(); + if let Some(ms) = self.mods.screen.as_mut() { + ms.binding = Some(name); + } + return; + } let UiWidget::List(l) = w else { continue }; let scroll = ms.scrolls.get(list_i).copied().unwrap_or_default(); list_i += 1; diff --git a/crates/horizon-client/src/mod_list.rs b/crates/horizon-client/src/mod_list.rs index db2ea9194..325839738 100644 --- a/crates/horizon-client/src/mod_list.rs +++ b/crates/horizon-client/src/mod_list.rs @@ -1,7 +1,11 @@ //! Which of the installed Horizon mods are on (`docs/mods.md`), kept in //! `horizon-mods.txt` beside `options.txt` as `mod.:true` lines. Every //! mod is off until the player turns it on in Options > Mods. -use std::{collections::BTreeSet, fs, path::Path}; +use std::{ + collections::{BTreeMap, BTreeSet}, + fs, + path::Path, +}; pub const FILE: &str = "horizon-mods.txt"; /// The folder holding `.hmod` files, beside `options.txt`. @@ -11,6 +15,9 @@ pub const DIR: &str = "horizon-mods"; pub struct ModList { /// Ids of the mods the player turned on. pub enabled: BTreeSet, + /// The keys the player chose for mods' keys, mod id → key name → key + /// (`key..:key.keyboard.x` lines). + pub keys: BTreeMap>, /// What the Mods page shows, filled by the game from the mods it found. pub installed: Vec, } @@ -45,20 +52,41 @@ impl ModList { .filter_map(|(k, _)| k.trim().strip_prefix("mod.")) .map(str::to_owned) .collect(); + let mut keys: BTreeMap> = BTreeMap::new(); + for (k, v) in text.lines().filter_map(|l| l.split_once(':')) { + // Mod ids have no dots; a key's name may. + if let Some((id, name)) = k + .trim() + .strip_prefix("key.") + .and_then(|r| r.split_once('.')) + { + let entry = keys.entry(id.to_owned()).or_default(); + entry.insert(name.to_owned(), v.trim().to_owned()); + } + } Self { enabled, + keys, installed: Vec::new(), } } pub fn serialize(&self) -> String { - self.enabled - .iter() - .map(|id| format!("mod.{id}:true\n")) - .collect() + let on = self.enabled.iter().map(|id| format!("mod.{id}:true\n")); + let keys = self.keys.iter().flat_map(|(id, keys)| { + keys.iter() + .map(move |(name, key)| format!("key.{id}.{name}:{key}\n")) + }); + on.chain(keys).collect() + } + /// Mod `id`'s chosen keys, for `Mods::set_bindings`. + pub fn keys_of(&self, id: &str) -> Vec<(&str, &str)> { + self.keys.get(id).map_or_else(Vec::new, |k| { + k.iter().map(|(n, k)| (n.as_str(), k.as_str())).collect() + }) } pub fn save(&self, dir: &Path) { let path = dir.join(FILE); - if self.enabled.is_empty() && !path.exists() { + if self.enabled.is_empty() && self.keys.is_empty() && !path.exists() { return; } let _ = fs::write(path, self.serialize()); @@ -91,6 +119,18 @@ mod tests { assert_eq!(ModList::default().serialize(), ""); } + #[test] + fn keeps_the_keys_players_chose() { + let text = "mod.zoom:true\nkey.zoom.zoom.in:key.mouse.4\nkey.zoom.out:key.keyboard.x\n"; + let list = ModList::parse(text); + assert_eq!( + list.keys_of("zoom"), + [("out", "key.keyboard.x"), ("zoom.in", "key.mouse.4")] + ); + assert!(list.keys_of("other").is_empty()); + assert_eq!(ModList::parse(&list.serialize()), list); + } + #[test] fn toggles_installed_mods_but_not_dev_ones() { let installed = |id: &str, dev| Installed { diff --git a/crates/horizon-mod-sdk/wit/horizon.wit b/crates/horizon-mod-sdk/wit/horizon.wit index 820a82483..f9c147cfa 100644 --- a/crates/horizon-mod-sdk/wit/horizon.wit +++ b/crates/horizon-mod-sdk/wit/horizon.wit @@ -137,6 +137,16 @@ interface ui { /// Up to 256 characters. label: string, } + /// One of the mod's keys (`input.register-key`) as Controls shows a + /// binding, "Zoom: Z". A click waits for the next key or mouse button, + /// which becomes the key (Esc: none). The game keeps the player's + /// choice across starts; the mod's `key-down` and `key-up` follow it. + record key-widget { + /// The mod's own name for the widget. + id: u32, + /// The name the mod registered the key under. + key: string, + } variant widget { /// Text the player reads. text(text-widget), @@ -146,6 +156,8 @@ interface ui { item(item-widget), /// Rows the player picks from. %list(list-widget), + /// A key the player binds. + key(key-widget), } /// A screen the mod owns: a title over widgets stacked down the middle, /// the Done button the game adds, and Esc to leave it. @@ -220,7 +232,8 @@ interface input { claim-items: func(ids: list); /// A key of the mod's: `name` comes back in `key-down` and `key-up`, /// `label` says what it does, `key` is the key as options.txt writes - /// it (`key.keyboard.r`). At most 16; an unknown key is an error. + /// it (`key.keyboard.r`), the default: a key the player chose for it + /// (`ui`'s key widget) wins. At most 16; an unknown key is an error. register-key: func(name: string, label: string, key: string) -> result<_, string>; /// The main hand's stack, as of this tick; none when it is empty. held: func() -> option; diff --git a/crates/horizon-mods/src/runtime.rs b/crates/horizon-mods/src/runtime.rs index 0e7c079d2..f596db81c 100644 --- a/crates/horizon-mods/src/runtime.rs +++ b/crates/horizon-mods/src/runtime.rs @@ -37,7 +37,8 @@ use bindings::{ /// The widget model a `ui` screen or settings page shows (WIT `interface /// ui`), for the game to draw with its menus' own widgets. pub use ui_bindings::horizon::mods::ui::{ - ButtonWidget, ItemWidget, ListWidget, Screen as UiScreen, TextWidget, Widget as UiWidget, + ButtonWidget, ItemWidget, KeyWidget, ListWidget, Screen as UiScreen, TextWidget, + Widget as UiWidget, }; use ui_bindings::{UiHost, horizon::mods::ui}; /// Capability `render`'s imports (docs/mods.md): the game draws the shapes. @@ -564,6 +565,48 @@ impl Mods { d.input.keys.iter().map(move |k| (d.name.as_str(), k)) }) } + /// Mod `i`'s keys, as it registered them (with the player's choices). + pub fn keys_of(&self, i: usize) -> &[ModKey] { + self.entries + .get(i) + .and_then(|e| e.run.as_ref()) + .map_or(&[], |r| r.store.data().input.keys.as_slice()) + } + /// The keys the player chose (`ui`'s key widget), by mod id: each + /// running mod's `name → key`. They replace the defaults registered + /// so far and later ones. Keys that aren't keys are skipped. + pub fn set_bindings<'a>(&mut self, chosen: impl Fn(&str) -> Vec<(&'a str, &'a str)>) { + for e in &mut self.entries { + let (Some(m), Some(run)) = (&e.manifest, e.run.as_mut()) else { + continue; + }; + let input = &mut run.store.data_mut().input; + input.bindings = chosen(&m.id) + .into_iter() + .filter(|(_, key)| key_ok(key)) + .map(|(n, k)| (n.to_owned(), k.to_owned())) + .collect(); + for (name, key) in input.bindings.clone() { + input.rebind(&name, key); + } + } + } + /// The player binds mod `i`'s key `name` to `key` (`key.keyboard.x`, + /// `key.mouse.4`, `key.keyboard.unknown` for none). False when the mod + /// has no such key or `key` isn't one. + pub fn rebind(&mut self, i: usize, name: &str, key: &str) -> bool { + let Some(run) = self.entries.get_mut(i).and_then(|e| e.run.as_mut()) else { + return false; + }; + let input = &mut run.store.data_mut().input; + if !key_ok(key) || !input.keys.iter().any(|k| k.name == name) { + return false; + } + input.bindings.retain(|(n, _)| n != name); + input.bindings.push((name.to_owned(), key.to_owned())); + input.rebind(name, key.to_owned()); + true + } /// The use button went down with `item`: `use-start` to each mod that /// claimed it. pub fn use_start(&mut self, item: &HeldItem) { @@ -813,6 +856,10 @@ fn clip_widgets(widgets: Vec) -> Vec { l.selected = l.selected.filter(|&s| (s as usize) < l.rows.len()); UiWidget::List(l) } + UiWidget::Key(mut k) => { + k.key = clip(k.key); + UiWidget::Key(k) + } }) .collect() } @@ -873,6 +920,9 @@ struct InputState { down: Vec, /// It holds the mouse wheel. wheel: bool, + /// The keys the player chose, `name → key`: they win over + /// `register-key`'s. + bindings: Vec<(String, String)>, } /// Events kept for `poll`, items claimed and keys, per mod. const INPUT_EVENTS: usize = 256; @@ -893,6 +943,17 @@ fn key_ok(key: &str) -> bool { } impl InputState { + /// Key `name` is `key` now; held, it lets go first. + fn rebind(&mut self, name: &str, key: String) { + let Some(k) = self.keys.iter_mut().find(|k| k.name == name) else { + return; + }; + k.key = key; + if let Some(i) = self.down.iter().position(|n| n == name) { + let name = self.down.swap_remove(i); + self.push(InputEvent::KeyUp(name)); + } + } fn push(&mut self, event: InputEvent) { if self.events.len() == INPUT_EVENTS { self.events.pop_front(); @@ -912,6 +973,10 @@ impl input::Host for State { )); } let (name, label) = (clip(name), clip(label)); + let key = match self.input.bindings.iter().find(|(n, _)| *n == name) { + Some((_, chosen)) => chosen.clone(), + None => key, + }; if let Some(k) = self.input.keys.iter_mut().find(|k| k.name == name) { (k.label, k.key) = (label, key); return Ok(()); diff --git a/crates/horizon-mods/tests/sandbox.rs b/crates/horizon-mods/tests/sandbox.rs index 29577a9c6..793d03db6 100644 --- a/crates/horizon-mods/tests/sandbox.rs +++ b/crates/horizon-mods/tests/sandbox.rs @@ -449,7 +449,7 @@ fn ui_screens_settings_and_events() { assert_eq!(mods.settings_widgets(0).len(), 3); assert!(mods.entries()[0].has_ui_settings()); mods.ui_settings_opened(0); - assert_eq!(mods.settings_widgets(0).len(), 2); + assert_eq!(mods.settings_widgets(0).len(), 3); // `/ui_close` closes the mod's own screen. assert!(mods.command("ui_close")); assert!(mods.drain().contains(&Output::CloseScreen { owner: 0 })); @@ -553,6 +553,45 @@ fn input_claims_keys_and_events() { assert_eq!(chat(&mut mods), ["key-down wheel", "key-up wheel"]); } +#[test] +fn the_players_key_choice_wins() { + let chat = |mods: &mut Mods| -> Vec { + mods.drain() + .into_iter() + .filter_map(|o| match o { + Output::Chat { text, .. } => Some(text), + _ => None, + }) + .collect() + }; + let mut mods = load("horizon_mods_test_ui_guest.hmod"); + assert!(mods.command("input_claim")); + assert!(matches!( + &mods.settings_widgets(0)[0], + horizon_mods::UiWidget::Key(k) if k.id == 9 && k.key == "wheel" + )); + // Rebinding while held lets the old key go. + mods.key("key.keyboard.r", true); + assert!(mods.rebind(0, "wheel", "key.mouse.4")); + assert_eq!(mods.keys_of(0)[0].key, "key.mouse.4"); + mods.key("key.keyboard.r", false); + mods.key("key.mouse.4", true); + assert!(!mods.rebind(0, "wheel", "R") && !mods.rebind(0, "nope", "key.keyboard.g")); + chat(&mut mods); + assert!(mods.command("input_poll")); + assert_eq!( + chat(&mut mods), + ["key-down wheel", "key-up wheel", "key-down wheel"] + ); + // A choice saved earlier replaces the default, registered before or after. + let mut mods = load("horizon_mods_test_ui_guest.hmod"); + mods.set_bindings(|_| vec![("wheel", "key.keyboard.g"), ("bad", "G")]); + assert!(mods.command("input_claim")); + assert_eq!(mods.keys_of(0)[0].key, "key.keyboard.g"); + mods.set_bindings(|_| vec![("wheel", "key.keyboard.h")]); + assert_eq!(mods.keys_of(0)[0].key, "key.keyboard.h"); +} + #[test] fn input_wheel_is_the_mods_while_claimed() { let mut mods = load("horizon_mods_test_ui_guest.hmod"); diff --git a/crates/horizon-mods/tests/ui_guest/src/lib.rs b/crates/horizon-mods/tests/ui_guest/src/lib.rs index 9bede11f9..f2765e8da 100644 --- a/crates/horizon-mods/tests/ui_guest/src/lib.rs +++ b/crates/horizon-mods/tests/ui_guest/src/lib.rs @@ -9,7 +9,7 @@ use horizon_mod_sdk::{ Mod, Registration, chat, input::{self, Event}, render::{self, FillShape, ItemShape, Point, QuadShape, Rect, Shape, SpriteShape, TextShape}, - ui::{self, ItemWidget, ListWidget, Screen, TextWidget, Widget}, + ui::{self, ItemWidget, KeyWidget, ListWidget, Screen, TextWidget, Widget}, }; /// One of each shape, the text too long by 44 characters. @@ -162,6 +162,10 @@ impl Mod for Guest { // Not a key: refused. let bad = input::register_key("bad", "Bad", "R").unwrap_err(); chat::show(&format!("claimed; {bad}")); + ui::set_settings(&[Widget::Key(KeyWidget { + id: 9, + key: "wheel".into(), + })]); } "input_poll" => { for event in input::poll() { @@ -225,6 +229,11 @@ impl Mod for Guest { id: 3, label: "Corner: Top Right".into(), }), + // `/input_claim`'s key; drawn as its name until registered. + Widget::Key(KeyWidget { + id: 9, + key: "wheel".into(), + }), ]); chat::show("settings opened"); } diff --git a/docs/api/mods-reference.md b/docs/api/mods-reference.md index 0deeb0ddd..f525ace89 100644 --- a/docs/api/mods-reference.md +++ b/docs/api/mods-reference.md @@ -197,6 +197,15 @@ A thing on a mod's screen or settings page. A button as the options pages have t | `id` | `u32` | The mod's own name for the widget. | | `label` | `string` | Up to 256 characters. | +### record `key-widget` + +One of the mod's keys (`input.register-key`) as Controls shows a binding, "Zoom: Z". A click waits for the next key or mouse button, which becomes the key (Esc: none). The game keeps the player's choice across starts; the mod's `key-down` and `key-up` follow it. + +| Field | Type | | +| --- | --- | --- | +| `id` | `u32` | The mod's own name for the widget. | +| `key` | `string` | The name the mod registered the key under. | + ### variant `widget` | Case | Payload | | @@ -205,6 +214,7 @@ A thing on a mod's screen or settings page. A button as the options pages have t | `button` | `button-widget` | A button. | | `item` | `item-widget` | An item's icon and name. | | `list` | `list-widget` | Rows the player picks from. | +| `key` | `key-widget` | A key the player binds. | ### record `screen` @@ -278,7 +288,7 @@ A mapping as the player has bound it. | Function | Signature | What it does | | --- | --- | --- | | `claim-items` | `func(ids: list)` | The items whose use the mod handles instead of the game, by id (at most 64). Replaces the previous list. | -| `register-key` | `func(name: string, label: string, key: string) -> result<_, string>` | A key of the mod's: `name` comes back in `key-down` and `key-up`, `label` says what it does, `key` is the key as options.txt writes it (`key.keyboard.r`). At most 16; an unknown key is an error. | +| `register-key` | `func(name: string, label: string, key: string) -> result<_, string>` | A key of the mod's: `name` comes back in `key-down` and `key-up`, `label` says what it does, `key` is the key as options.txt writes it (`key.keyboard.r`), the default: a key the player chose for it (`ui`'s key widget) wins. At most 16; an unknown key is an error. | | `held` | `func() -> option` | The main hand's stack, as of this tick; none when it is empty. | | `poll` | `func() -> list` | The events since the last call, oldest first. | | `claim-wheel` | `func(on: bool)` | Takes the mouse wheel while playing (true) or gives it back: while taken, its steps are the mod's `wheel` events and the hotbar doesn't move. The game gives it back when the mod stops. | diff --git a/docs/mods.md b/docs/mods.md index 235574d6c..108e854be 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -87,7 +87,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | `hud` | `hud` | `set-panel(corner, lines)`, `clear()`: up to 16 lines of 256 characters | | `storage` | `storage` | `get(key)`, `set(key, value)`, `delete(key)`: a per-mod folder of up to 64 files, 4 MiB total, at most 1 MiB written per call | | `command` | `command` | `run(line)`: a vanilla command run as the player, with the player's permissions, after the hook returns (8 per call); mods' own commands are not run | -| `ui` | `ui` | `open-screen`, `close-screen`, `set-settings`: a screen the mod owns and a page in Options > Mods, of text, button, item and list widgets the game lays out and draws with its own menus' code. **Experimental:** the manifest needs `experimental = ["ui"]` | +| `ui` | `ui` | `open-screen`, `close-screen`, `set-settings`: a screen the mod owns and a page in Options > Mods, of text, button, item, list and key widgets the game lays out and draws with its own menus' code. A key widget binds one of the mod's keys (`input.register-key`) as Key Binds does: click, then press the key or mouse button (Esc: none); the game keeps the choice in `horizon-mods.txt` (`key..:key.keyboard.z`) and the mod's `key-down` follows it. **Experimental:** the manifest needs `experimental = ["ui"]` | | `render` | `render` | `set-hud(shapes)`: up to 1024 fills, four-cornered shapes, GUI sprites, items and text, kept on the HUD (over the hotbar, under chat and screens, hidden with F1) until replaced; `set-corner(corner, w, h, shapes)`: a box the game places in a corner, stacked with its overlays and other panels, as `hud`'s panels are; `text-width(text)`; `frame-stats()`: the game's FPS, min and 1% low; `gui-size()`; `shake(strength, seconds)`: the view turns, the player doesn't, scaled by Distortion Effects; `set-zoom(zoom)`: the field of view divided by up to 10, eased or at once, with slower turning, while playing and not through a spyglass (the strongest of several mods' zooms counts). The game's own HUD renderer draws them: a mod never supplies shader code. **Experimental:** `experimental = ["render"]` | | `input` | `input` | `claim-items(ids)`: the use button on those items is the mod's (`use-start`, `use-stop` with the ticks held; the game doesn't place, eat or swing); `register-key(name, label, key)`: up to 16 keys (`key.keyboard.r`), heard while playing as `key-down` / `key-up` (the game's own mapping on that key still runs; a screen opening lets held keys up), mouse buttons as `key.mouse.4`; `claim-wheel(on)`: the wheel's steps become `wheel` events and the hotbar doesn't move (the one input a mod takes from the game; a mod's keys never do); `held()`; `mapping(name)`: a movement or click mapping's key label and whether it is held; `clicks-per-second()`; `poll()` returns the events, at most 256 kept. **Experimental:** `experimental = ["input"]` | From 5335354658cbbb1970c9bba6d9605f88e1419eda Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 05:10:19 +0100 Subject: [PATCH 235/238] CI: runners clone what the shared source gained after their first copy The release runner, now parity-main's, cloned the shared source before the sounds were added to it; vanilla-data.sh only copied on the first run, so ambient_sound, audio_attributes and music failed there with "Sound assets missing". Each run now clones any entry the runner's copy lacks. Co-Authored-By: Claude Opus 5.5 --- tools/ci/vanilla-data.sh | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/tools/ci/vanilla-data.sh b/tools/ci/vanilla-data.sh index e8dfb36cc..ebebf3219 100755 --- a/tools/ci/vanilla-data.sh +++ b/tools/ci/vanilla-data.sh @@ -13,17 +13,15 @@ source="${HORIZON_SHARED_SOURCE:-$HOME/code/horizon-parity/shared-local}" version=$(sed -n 's/.*"version": *"\([^"]*\)".*/\1/p' research-lock.json | head -1) if [ ! -f "$shared/minecraft/$version/prepared.json" ]; then [ -f "$source/minecraft/$version/prepared.json" ] || { echo "::error::no imported Minecraft $version in $source"; exit 1; } - mkdir -p "$shared" - if [ "$(uname)" = Darwin ]; then - for entry in "$source"/*; do - [ "$(basename "$entry")" = horizon-client ] || cp -cR "$entry" "$shared/" - done - else - for entry in "$source"/*; do - [ "$(basename "$entry")" = horizon-client ] || cp -R --reflink=auto "$entry" "$shared/" - done - fi fi +# Whatever the source holds and this runner's copy doesn't yet: all of it the first time, and later additions (the +# sounds came after the release runner's first clone, and its audio harnesses failed for want of them). +mkdir -p "$shared" +for entry in "$source"/*; do + name=$(basename "$entry") + [ "$name" = horizon-client ] || [ -e "$shared/$name" ] && continue + if [ "$(uname)" = Darwin ]; then cp -cR "$entry" "$shared/"; else cp -R --reflink=auto "$entry" "$shared/"; fi +done mkdir -p .local "$shared/prepared" for entry in "$shared"/*; do name=$(basename "$entry") From 3e3c0cebb51fa5deb0022ff1c0ca8fa4ba9fc42d Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 05:34:49 +0100 Subject: [PATCH 236/238] Mod keys: rows the game adds to the settings page, not a WIT widget A new case in ui's widget variant stops every ui mod built before it from loading: the runtime links a component only when each imported function's types match exactly ("expected variant of 5 cases, found 4"), so the published FPS Counter and Keystrokes would break. Instead, the game lists a mod's keys first on its page in Options > Mods, as Key Binds rows, and a mod with keys gets a page even without ui. No WIT type changes. docs/api/stability.md said a new case the mod sends isn't breaking; it is, and now says so. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/mods.rs | 113 ++++++++++-------- crates/horizon-mod-sdk/wit/horizon.wit | 17 +-- crates/horizon-mods/src/runtime.rs | 17 ++- crates/horizon-mods/tests/sandbox.rs | 8 +- crates/horizon-mods/tests/ui_guest/src/lib.rs | 11 +- docs/api/mods-reference.md | 12 +- docs/api/stability.md | 9 +- docs/mods.md | 4 +- 8 files changed, 86 insertions(+), 105 deletions(-) diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs index 66d00b233..e55a87d46 100644 --- a/crates/horizon-client/src/game/mods.rs +++ b/crates/horizon-client/src/game/mods.rs @@ -27,7 +27,7 @@ use { text::{self, Text}, ui, }, - horizon_mods::{Clock, Mods, Output, Player, Snapshot, Status, UiScreen, UiWidget}, + horizon_mods::{Clock, ModKey, Mods, Output, Player, Snapshot, Status, UiScreen, UiWidget}, std::time::Instant, winit::{event::MouseButton, keyboard::KeyCode}, }; @@ -102,7 +102,7 @@ struct ModScreen { under: Screen, /// One scroll per `list` widget, in widget order. scrolls: Vec, - /// A key widget waiting for the key to bind: the mod key's name. + /// A key row waiting for the key to bind: the mod key's name. binding: Option, } impl ModsState { @@ -575,7 +575,7 @@ impl Game<'_> { fn mod_ui_view<'a>(mods: &'a Mods, ms: &ModScreen) -> Option<(&'a str, &'a [UiWidget])> { if ms.settings { let widgets = mods.settings_widgets(ms.owner); - if widgets.is_empty() { + if widgets.is_empty() && mods.keys_of(ms.owner).is_empty() { return None; } let name = mods @@ -589,24 +589,25 @@ impl Game<'_> { Some((s.title.as_str(), s.widgets.as_slice())) } } - /// A key widget's label as Key Binds shows one, "Zoom: Z", marked + /// The keys a settings page lists first: the mod's own (`input`). + fn mod_page_keys<'a>(mods: &'a Mods, ms: &ModScreen) -> &'a [ModKey] { + if ms.settings { + mods.keys_of(ms.owner) + } else { + &[] + } + } + /// A key row's label as Key Binds shows one, "Zoom: Z", marked /// "> Zoom: Z <" in yellow while it waits for the new key. - fn mod_key_label(mods: &Mods, owner: usize, name: &str, waiting: bool) -> Text { - let (label, key) = mods - .keys_of(owner) - .iter() - .find(|k| k.name == name) - .map_or((name, "key.keyboard.unknown"), |k| { - (k.label.as_str(), k.key.as_str()) - }); - let value = format!("{label}: {}", keybinds::display_name(key).plain()); + fn mod_key_label(k: &ModKey, waiting: bool) -> Text { + let value = format!("{}: {}", k.label, keybinds::display_name(&k.key).plain()); if waiting { Text::literal(format!("> {value} <")).with_color(text::YELLOW) } else { Text::literal(value) } } - /// The player bound the key widget that waited to `key`: the mod's key + /// The player bound the key row that waited to `key`: the mod's key /// follows it now and at the next start (`horizon-mods.txt`). fn bind_mod_key(&mut self, key: String) { let Some(ms) = self.mods.screen.as_mut() else { @@ -637,7 +638,7 @@ impl Game<'_> { .insert(name, key); self.settings.save(&Self::options_path()); } - /// A key while a mod screen is up: true when a key widget waited for + /// A key while a mod screen is up: true when a key row waited for /// it (Key Binds: Escape binds none). pub(super) fn mod_screen_key(&mut self, key: KeyCode) -> bool { if self @@ -657,13 +658,13 @@ impl Game<'_> { } /// The layout the mod screen's widgets get at `h`: (widget, top, /// height), the column being `COLUMN` wide down the middle. - fn mod_ui_layout(h: f32, widgets: &[UiWidget]) -> Vec<(f32, f32)> { + fn mod_ui_layout(h: f32, top: f32, widgets: &[UiWidget]) -> Vec<(f32, f32)> { let mut out = Vec::with_capacity(widgets.len()); - let mut y = MOD_UI_TOP; + let mut y = top; for w in widgets { let wh = match w { UiWidget::Text(_) => 12.0, - UiWidget::Item(_) | UiWidget::Button(_) | UiWidget::Key(_) => 20.0, + UiWidget::Item(_) | UiWidget::Button(_) => 20.0, UiWidget::List(l) => (l.rows.len().min(6) as f32 * 12.0) + 8.0, }; // Nothing may cover Done; a crowded mod loses what won't fit. @@ -683,7 +684,8 @@ impl Game<'_> { } if settings { self.mods.mods.ui_settings_opened(owner); - if self.mods.mods.settings_widgets(owner).is_empty() { + let keys = self.mods.mods.keys_of(owner); + if self.mods.mods.settings_widgets(owner).is_empty() && keys.is_empty() { self.show_mod_output(); return; } @@ -755,7 +757,27 @@ impl Game<'_> { ); let cx = (batch.width / 2.0).floor(); let x = cx - MOD_UI_COLUMN / 2.0; - let layout = Self::mod_ui_layout(batch.height, widgets); + // A settings page starts with the mod's keys, as Key Binds rows. + let keys = Self::mod_page_keys(&self.mods.mods, ms); + for (i, k) in keys.iter().enumerate() { + let wy = MOD_UI_TOP + i as f32 * MOD_UI_KEY_ROW; + let hover = ui::hovered(self.cursor, x, wy, MOD_UI_COLUMN, 20.0); + let waiting = ms.binding.as_deref() == Some(k.name.as_str()); + let label = Self::mod_key_label(k, waiting); + ui::button( + batch, + atlas, + x, + wy, + MOD_UI_COLUMN, + 20.0, + &label, + true, + hover, + ); + } + let top = MOD_UI_TOP + keys.len() as f32 * MOD_UI_KEY_ROW; + let layout = Self::mod_ui_layout(batch.height, top, widgets); let mut list_i = 0usize; for (w, &(wy, wh)) in widgets.iter().zip(&layout) { match w { @@ -774,22 +796,6 @@ impl Game<'_> { hover, ); } - UiWidget::Key(k) => { - let hover = ui::hovered(self.cursor, x, wy, MOD_UI_COLUMN, 20.0); - let waiting = ms.binding.as_deref() == Some(k.key.as_str()); - let label = Self::mod_key_label(&self.mods.mods, ms.owner, &k.key, waiting); - ui::button( - batch, - atlas, - x, - wy, - MOD_UI_COLUMN, - 20.0, - &label, - true, - hover, - ); - } UiWidget::Text(t) => { let color = if t.color == 0 { u32::MAX @@ -920,7 +926,22 @@ impl Game<'_> { }; let owner = ms.owner; let x = (w / 2.0).floor() - MOD_UI_COLUMN / 2.0; - let layout = Self::mod_ui_layout(h, widgets); + // A key row waits for the key to bind. + let keys = Self::mod_page_keys(&self.mods.mods, ms); + let picked = (0..keys.len()).find(|&i| { + let wy = MOD_UI_TOP + i as f32 * MOD_UI_KEY_ROW; + ui::hovered(cursor, x, wy, MOD_UI_COLUMN, 20.0) + }); + if let Some(i) = picked { + let name = keys[i].name.clone(); + self.audio.play_button_click(); + if let Some(ms) = self.mods.screen.as_mut() { + ms.binding = Some(name); + } + return; + } + let top = MOD_UI_TOP + keys.len() as f32 * MOD_UI_KEY_ROW; + let layout = Self::mod_ui_layout(h, top, widgets); let mut list_i = 0usize; // The `ui_select` call needs `&mut`: find the pick first. let mut hit = None; @@ -935,17 +956,6 @@ impl Game<'_> { self.show_mod_output(); return; } - // A key widget waits for the key to bind. - if let UiWidget::Key(k) = w - && ui::hovered(cursor, x, wy, MOD_UI_COLUMN, 20.0) - { - self.audio.play_button_click(); - let name = k.key.clone(); - if let Some(ms) = self.mods.screen.as_mut() { - ms.binding = Some(name); - } - return; - } let UiWidget::List(l) = w else { continue }; let scroll = ms.scrolls.get(list_i).copied().unwrap_or_default(); list_i += 1; @@ -975,7 +985,9 @@ impl Game<'_> { return; }; let x = (w / 2.0).floor() - MOD_UI_COLUMN / 2.0; - let layout = Self::mod_ui_layout(h, widgets); + let top = + MOD_UI_TOP + Self::mod_page_keys(&self.mods.mods, ms).len() as f32 * MOD_UI_KEY_ROW; + let layout = Self::mod_ui_layout(h, top, widgets); let mut list_i = 0usize; for (w, &(wy, wh)) in widgets.iter().zip(&layout) { let UiWidget::List(l) = w else { continue }; @@ -995,3 +1007,6 @@ impl Game<'_> { const MOD_UI_TOP: f32 = 40.0; #[cfg(feature = "mods")] const MOD_UI_COLUMN: f32 = 200.0; +/// A key row's height with its gap, as the widgets below it space out. +#[cfg(feature = "mods")] +const MOD_UI_KEY_ROW: f32 = 26.0; diff --git a/crates/horizon-mod-sdk/wit/horizon.wit b/crates/horizon-mod-sdk/wit/horizon.wit index f9c147cfa..9088c0c39 100644 --- a/crates/horizon-mod-sdk/wit/horizon.wit +++ b/crates/horizon-mod-sdk/wit/horizon.wit @@ -137,16 +137,6 @@ interface ui { /// Up to 256 characters. label: string, } - /// One of the mod's keys (`input.register-key`) as Controls shows a - /// binding, "Zoom: Z". A click waits for the next key or mouse button, - /// which becomes the key (Esc: none). The game keeps the player's - /// choice across starts; the mod's `key-down` and `key-up` follow it. - record key-widget { - /// The mod's own name for the widget. - id: u32, - /// The name the mod registered the key under. - key: string, - } variant widget { /// Text the player reads. text(text-widget), @@ -156,8 +146,6 @@ interface ui { item(item-widget), /// Rows the player picks from. %list(list-widget), - /// A key the player binds. - key(key-widget), } /// A screen the mod owns: a title over widgets stacked down the middle, /// the Done button the game adds, and Esc to leave it. @@ -232,8 +220,9 @@ interface input { claim-items: func(ids: list); /// A key of the mod's: `name` comes back in `key-down` and `key-up`, /// `label` says what it does, `key` is the key as options.txt writes - /// it (`key.keyboard.r`), the default: a key the player chose for it - /// (`ui`'s key widget) wins. At most 16; an unknown key is an error. + /// it (`key.keyboard.r`), the default: the player can bind another + /// one on the mod's page in Options > Mods, and that one wins. At most + /// 16; an unknown key is an error. register-key: func(name: string, label: string, key: string) -> result<_, string>; /// The main hand's stack, as of this tick; none when it is empty. held: func() -> option; diff --git a/crates/horizon-mods/src/runtime.rs b/crates/horizon-mods/src/runtime.rs index f596db81c..0011fa99d 100644 --- a/crates/horizon-mods/src/runtime.rs +++ b/crates/horizon-mods/src/runtime.rs @@ -37,8 +37,7 @@ use bindings::{ /// The widget model a `ui` screen or settings page shows (WIT `interface /// ui`), for the game to draw with its menus' own widgets. pub use ui_bindings::horizon::mods::ui::{ - ButtonWidget, ItemWidget, KeyWidget, ListWidget, Screen as UiScreen, TextWidget, - Widget as UiWidget, + ButtonWidget, ItemWidget, ListWidget, Screen as UiScreen, TextWidget, Widget as UiWidget, }; use ui_bindings::{UiHost, horizon::mods::ui}; /// Capability `render`'s imports (docs/mods.md): the game draws the shapes. @@ -153,11 +152,13 @@ pub struct Entry { } impl Entry { /// The running mod has a settings page (capability `ui` set widgets - /// with `ui.set-settings`): its row in Options > Mods gains a button. + /// with `ui.set-settings`, or `input` registered keys the player may + /// bind there): its row in Options > Mods gains a button. pub fn has_ui_settings(&self) -> bool { - self.run - .as_ref() - .is_some_and(|r| !r.store.data().ui.settings.is_empty()) + self.run.as_ref().is_some_and(|r| { + let d = r.store.data(); + !d.ui.settings.is_empty() || !d.input.keys.is_empty() + }) } } @@ -856,10 +857,6 @@ fn clip_widgets(widgets: Vec) -> Vec { l.selected = l.selected.filter(|&s| (s as usize) < l.rows.len()); UiWidget::List(l) } - UiWidget::Key(mut k) => { - k.key = clip(k.key); - UiWidget::Key(k) - } }) .collect() } diff --git a/crates/horizon-mods/tests/sandbox.rs b/crates/horizon-mods/tests/sandbox.rs index 793d03db6..3b33ab4b2 100644 --- a/crates/horizon-mods/tests/sandbox.rs +++ b/crates/horizon-mods/tests/sandbox.rs @@ -449,7 +449,7 @@ fn ui_screens_settings_and_events() { assert_eq!(mods.settings_widgets(0).len(), 3); assert!(mods.entries()[0].has_ui_settings()); mods.ui_settings_opened(0); - assert_eq!(mods.settings_widgets(0).len(), 3); + assert_eq!(mods.settings_widgets(0).len(), 2); // `/ui_close` closes the mod's own screen. assert!(mods.command("ui_close")); assert!(mods.drain().contains(&Output::CloseScreen { owner: 0 })); @@ -566,10 +566,8 @@ fn the_players_key_choice_wins() { }; let mut mods = load("horizon_mods_test_ui_guest.hmod"); assert!(mods.command("input_claim")); - assert!(matches!( - &mods.settings_widgets(0)[0], - horizon_mods::UiWidget::Key(k) if k.id == 9 && k.key == "wheel" - )); + // Its keys alone give it a page in Options > Mods, where the game lists them. + assert!(mods.settings_widgets(0).is_empty() && mods.entries()[0].has_ui_settings()); // Rebinding while held lets the old key go. mods.key("key.keyboard.r", true); assert!(mods.rebind(0, "wheel", "key.mouse.4")); diff --git a/crates/horizon-mods/tests/ui_guest/src/lib.rs b/crates/horizon-mods/tests/ui_guest/src/lib.rs index f2765e8da..9bede11f9 100644 --- a/crates/horizon-mods/tests/ui_guest/src/lib.rs +++ b/crates/horizon-mods/tests/ui_guest/src/lib.rs @@ -9,7 +9,7 @@ use horizon_mod_sdk::{ Mod, Registration, chat, input::{self, Event}, render::{self, FillShape, ItemShape, Point, QuadShape, Rect, Shape, SpriteShape, TextShape}, - ui::{self, ItemWidget, KeyWidget, ListWidget, Screen, TextWidget, Widget}, + ui::{self, ItemWidget, ListWidget, Screen, TextWidget, Widget}, }; /// One of each shape, the text too long by 44 characters. @@ -162,10 +162,6 @@ impl Mod for Guest { // Not a key: refused. let bad = input::register_key("bad", "Bad", "R").unwrap_err(); chat::show(&format!("claimed; {bad}")); - ui::set_settings(&[Widget::Key(KeyWidget { - id: 9, - key: "wheel".into(), - })]); } "input_poll" => { for event in input::poll() { @@ -229,11 +225,6 @@ impl Mod for Guest { id: 3, label: "Corner: Top Right".into(), }), - // `/input_claim`'s key; drawn as its name until registered. - Widget::Key(KeyWidget { - id: 9, - key: "wheel".into(), - }), ]); chat::show("settings opened"); } diff --git a/docs/api/mods-reference.md b/docs/api/mods-reference.md index f525ace89..a51fb904b 100644 --- a/docs/api/mods-reference.md +++ b/docs/api/mods-reference.md @@ -197,15 +197,6 @@ A thing on a mod's screen or settings page. A button as the options pages have t | `id` | `u32` | The mod's own name for the widget. | | `label` | `string` | Up to 256 characters. | -### record `key-widget` - -One of the mod's keys (`input.register-key`) as Controls shows a binding, "Zoom: Z". A click waits for the next key or mouse button, which becomes the key (Esc: none). The game keeps the player's choice across starts; the mod's `key-down` and `key-up` follow it. - -| Field | Type | | -| --- | --- | --- | -| `id` | `u32` | The mod's own name for the widget. | -| `key` | `string` | The name the mod registered the key under. | - ### variant `widget` | Case | Payload | | @@ -214,7 +205,6 @@ One of the mod's keys (`input.register-key`) as Controls shows a binding, "Zoom: | `button` | `button-widget` | A button. | | `item` | `item-widget` | An item's icon and name. | | `list` | `list-widget` | Rows the player picks from. | -| `key` | `key-widget` | A key the player binds. | ### record `screen` @@ -288,7 +278,7 @@ A mapping as the player has bound it. | Function | Signature | What it does | | --- | --- | --- | | `claim-items` | `func(ids: list)` | The items whose use the mod handles instead of the game, by id (at most 64). Replaces the previous list. | -| `register-key` | `func(name: string, label: string, key: string) -> result<_, string>` | A key of the mod's: `name` comes back in `key-down` and `key-up`, `label` says what it does, `key` is the key as options.txt writes it (`key.keyboard.r`), the default: a key the player chose for it (`ui`'s key widget) wins. At most 16; an unknown key is an error. | +| `register-key` | `func(name: string, label: string, key: string) -> result<_, string>` | A key of the mod's: `name` comes back in `key-down` and `key-up`, `label` says what it does, `key` is the key as options.txt writes it (`key.keyboard.r`), the default: the player can bind another one on the mod's page in Options > Mods, and that one wins. At most 16; an unknown key is an error. | | `held` | `func() -> option` | The main hand's stack, as of this tick; none when it is empty. | | `poll` | `func() -> list` | The events since the last call, oldest first. | | `claim-wheel` | `func(on: bool)` | Takes the mouse wheel while playing (true) or gives it back: while taken, its steps are the mod's `wheel` events and the hotbar doesn't move. The game gives it back when the mod stops. | diff --git a/docs/api/stability.md b/docs/api/stability.md index 132c971f9..d5bd0e545 100644 --- a/docs/api/stability.md +++ b/docs/api/stability.md @@ -26,12 +26,13 @@ promise, how something is deprecated and removed, and the tests that keep old mo ## What counts as a breaking change Breaking: removing or renaming anything; changing a type, a default, a unit or a meaning; making something optional -required; refusing input that was accepted; a new case in a WIT `enum` or `variant` the game sends to mods (an older -component cannot decode it); a JSON field that changes type. +required; refusing input that was accepted; any change to a WIT type a function uses, a new `enum` or `variant` case +included, in either direction (the runtime links a component only when each imported function's types match exactly: +a mod built before the change no longer loads); a JSON field that changes type. Not breaking: a new function, interface, capability, command, endpoint, tool or `server.toml` key; a new optional -argument or JSON field (readers must ignore fields they don't know); a new WIT case the mod *sends* (the game decodes -every case it ever served); faster, or fixing behaviour to what the reference already says. +argument or JSON field (readers must ignore fields they don't know); a new WIT function or type (mods built earlier +don't import it); faster, or fixing behaviour to what the reference already says. ## Stability levels diff --git a/docs/mods.md b/docs/mods.md index 108e854be..a8d8da851 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -87,9 +87,9 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | `hud` | `hud` | `set-panel(corner, lines)`, `clear()`: up to 16 lines of 256 characters | | `storage` | `storage` | `get(key)`, `set(key, value)`, `delete(key)`: a per-mod folder of up to 64 files, 4 MiB total, at most 1 MiB written per call | | `command` | `command` | `run(line)`: a vanilla command run as the player, with the player's permissions, after the hook returns (8 per call); mods' own commands are not run | -| `ui` | `ui` | `open-screen`, `close-screen`, `set-settings`: a screen the mod owns and a page in Options > Mods, of text, button, item, list and key widgets the game lays out and draws with its own menus' code. A key widget binds one of the mod's keys (`input.register-key`) as Key Binds does: click, then press the key or mouse button (Esc: none); the game keeps the choice in `horizon-mods.txt` (`key..:key.keyboard.z`) and the mod's `key-down` follows it. **Experimental:** the manifest needs `experimental = ["ui"]` | +| `ui` | `ui` | `open-screen`, `close-screen`, `set-settings`: a screen the mod owns and a page in Options > Mods, of text, button, item and list widgets the game lays out and draws with its own menus' code. **Experimental:** the manifest needs `experimental = ["ui"]` | | `render` | `render` | `set-hud(shapes)`: up to 1024 fills, four-cornered shapes, GUI sprites, items and text, kept on the HUD (over the hotbar, under chat and screens, hidden with F1) until replaced; `set-corner(corner, w, h, shapes)`: a box the game places in a corner, stacked with its overlays and other panels, as `hud`'s panels are; `text-width(text)`; `frame-stats()`: the game's FPS, min and 1% low; `gui-size()`; `shake(strength, seconds)`: the view turns, the player doesn't, scaled by Distortion Effects; `set-zoom(zoom)`: the field of view divided by up to 10, eased or at once, with slower turning, while playing and not through a spyglass (the strongest of several mods' zooms counts). The game's own HUD renderer draws them: a mod never supplies shader code. **Experimental:** `experimental = ["render"]` | -| `input` | `input` | `claim-items(ids)`: the use button on those items is the mod's (`use-start`, `use-stop` with the ticks held; the game doesn't place, eat or swing); `register-key(name, label, key)`: up to 16 keys (`key.keyboard.r`), heard while playing as `key-down` / `key-up` (the game's own mapping on that key still runs; a screen opening lets held keys up), mouse buttons as `key.mouse.4`; `claim-wheel(on)`: the wheel's steps become `wheel` events and the hotbar doesn't move (the one input a mod takes from the game; a mod's keys never do); `held()`; `mapping(name)`: a movement or click mapping's key label and whether it is held; `clicks-per-second()`; `poll()` returns the events, at most 256 kept. **Experimental:** `experimental = ["input"]` | +| `input` | `input` | `claim-items(ids)`: the use button on those items is the mod's (`use-start`, `use-stop` with the ticks held; the game doesn't place, eat or swing); `register-key(name, label, key)`: up to 16 keys (`key.keyboard.r`), heard while playing as `key-down` / `key-up` (the game's own mapping on that key still runs; a screen opening lets held keys up), mouse buttons as `key.mouse.4`. The player rebinds them on the mod's page in Options > Mods, where the game lists them first as Key Binds rows (click, then press a key or mouse button; Esc binds none), even for a mod without `ui`; the choice is kept in `horizon-mods.txt` (`key..:key.keyboard.z`) and wins over `register-key`'s default; `claim-wheel(on)`: the wheel's steps become `wheel` events and the hotbar doesn't move (the one input a mod takes from the game; a mod's keys never do); `held()`; `mapping(name)`: a movement or click mapping's key label and whether it is held; `clicks-per-second()`; `poll()` returns the events, at most 256 kept. **Experimental:** `experimental = ["input"]` | | Hook (export) | When | |---|---| From 1aa6f21620833327a6c9aeaa7ab95764682c1041 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 05:27:14 +0100 Subject: [PATCH 237/238] Horizon shader packs: the post stage runs in the game, with hot reload A native pack named by HORIZON_SHADER_PACK (a folder with horizon-shaders.toml or a .hshader in shaderpacks/) is checked, then its post passes run over the world's own target, the one a shaders/ pack reads: no second target and no extra pass, and without a pack the frame only tests one Option. The pack is looked at twice a second; a saved file is checked and rebuilt in place, and a version that fails is refused in the chat while the last good one keeps drawing. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + crates/horizon-client/Cargo.toml | 2 + crates/horizon-client/src/game.rs | 4 + .../horizon-client/src/game/native_shaders.rs | 110 ++++ .../horizon-client/src/game/shader_packs.rs | 16 +- crates/horizon-client/src/gfx.rs | 59 +- crates/horizon-client/src/gfx/native_post.rs | 509 ++++++++++++++++++ .../packs/golden_hour/post/glow.wgsl | 15 +- crates/horizon-shader-api/src/inputs.rs | 2 +- crates/horizon-shader-api/src/package.rs | 7 + docs/api/shaders-reference.md | 2 +- docs/api/shaders.md | 1 + docs/shaders.md | 16 +- 13 files changed, 723 insertions(+), 21 deletions(-) create mode 100644 crates/horizon-client/src/game/native_shaders.rs create mode 100644 crates/horizon-client/src/gfx/native_post.rs diff --git a/Cargo.lock b/Cargo.lock index 25c1ae15d..201f7c83c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1364,6 +1364,7 @@ dependencies = [ "horizon-mods", "horizon-protocol", "horizon-server-files", + "horizon-shader-api", "horizon-shaders", "horizon-telemetry", "horizon-theme", diff --git a/crates/horizon-client/Cargo.toml b/crates/horizon-client/Cargo.toml index 4877c18f2..a585ce19e 100644 --- a/crates/horizon-client/Cargo.toml +++ b/crates/horizon-client/Cargo.toml @@ -18,6 +18,8 @@ horizon-protocol = { path = "../horizon-protocol" } horizon-server-files = { path = "../horizon-server-files" } # Shader packs: reading them and turning their GLSL into naga modules. horizon-shaders = { path = "../horizon-shaders" } +# Horizon's own shader packs: the format, the stages and the check (docs/shaders.md). +horizon-shader-api = { path = "../horizon-shader-api" } horizon-friends = { path = "../horizon-friends" } # Horizon mods: sandboxed WebAssembly components, off unless installed and turned on (docs/mods.md). # Their runtime is the `mods` feature. diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 9a3503666..4638a4667 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -106,6 +106,7 @@ mod mounts; mod moving_pistons; mod music; mod names; +mod native_shaders; mod natural_spawning; mod nautili; mod net_harness; @@ -1507,6 +1508,8 @@ struct Game<'a> { extras: extras::ExtrasState, /// Horizon mods (`game/mods.rs`), none running unless turned on. mods: mods::ModsState, + /// Horizon's own shader pack, while one is picked (`game/native_shaders.rs`). + native_shaders: native_shaders::NativeShaders, /// Meshed sections waiting for an upload budget (Horizon settings). pending_uploads: Vec<((i32, i32, i32), horizon_client::mesher::SectionMesh)>, /// This frame's view frustum, for culling entities and block entities. @@ -12309,6 +12312,7 @@ impl<'a> Game<'a> { frame_graph: Default::default(), extras: Default::default(), mods: Default::default(), + native_shaders: Default::default(), sky_darken_memo: Default::default(), persist: Default::default(), remote: None, diff --git a/crates/horizon-client/src/game/native_shaders.rs b/crates/horizon-client/src/game/native_shaders.rs new file mode 100644 index 000000000..62aaafc42 --- /dev/null +++ b/crates/horizon-client/src/game/native_shaders.rs @@ -0,0 +1,110 @@ +//! Horizon's own shader packs in the game (docs/shaders.md), for development until the Shader +//! Packs page exists: `HORIZON_SHADER_PACK=` naming one in `shaderpacks/` (a folder with +//! `horizon-shaders.toml`, or a `.hshader`) runs it once it passes `horizon_shader_api::check`. +//! The pack is looked at twice a second: a saved file is checked and the pack rebuilt, and a +//! pack that fails is refused with its problems in the chat while the last good one keeps +//! drawing. + +use std::{ + hash::{Hash, Hasher}, + path::Path, + time::{Duration, Instant}, +}; + +use horizon_client::text::Text; +use horizon_shader_api::{Pack, check}; + +use super::Game; + +const LOOK_EVERY: Duration = Duration::from_millis(500); +const RED: u32 = 0xFF5555; + +#[derive(Default)] +pub(super) struct NativeShaders { + /// The pack's files when it was last read (`fingerprint`). + stamp: Option, + looked: Option, +} + +impl Game<'_> { + /// Builds the pack at `path` when its files changed since it was last read. + pub(super) fn tick_native_shader_pack(&mut self, path: &Path) { + let now = Instant::now(); + let state = &mut self.native_shaders; + if state.looked.is_some_and(|t| now - t < LOOK_EVERY) { + return; + } + state.looked = Some(now); + let stamp = fingerprint(path); + if stamp == state.stamp { + return; + } + let reload = state.stamp.is_some(); + state.stamp = stamp; + let Some(renderer) = &mut self.renderer else { + return; + }; + let result = Pack::read(path).map_err(|e| vec![e]).and_then(|pack| { + let report = check(&pack) + .map_err(|p| p.iter().map(ToString::to_string).collect::>())?; + let constants = pack + .manifest + .options + .iter() + .map(|o| (o.id.clone(), o.kind.default_constant())) + .collect(); + let labels = renderer + .set_native_shader_pack(Some((report, constants))) + .map_err(|e| vec![e])?; + Ok((pack.manifest, labels)) + }); + let name = path.file_name().unwrap_or_default().to_string_lossy(); + match result { + Ok((m, labels)) => { + log::info!( + "shader pack {} {}: {} in {} ms", + m.id, + m.version, + labels.join(", "), + now.elapsed().as_millis() + ); + if reload { + self.system_message(Text::literal(format!("Shader pack {} reloaded", m.name))); + } + } + Err(problems) => { + log::warn!("shader pack {name} refused: {}", problems.join("; ")); + self.system_message(Text::colored(format!("Shader pack {name} refused:"), RED)); + for p in problems { + self.system_message(Text::colored(p, RED)); + } + } + } + } +} + +/// A hash of the pack's file names, sizes and modification times (`None` when unreadable). +fn fingerprint(path: &Path) -> Option { + fn walk(path: &Path, hasher: &mut std::hash::DefaultHasher) -> Option<()> { + let meta = std::fs::metadata(path).ok()?; + path.hash(hasher); + if meta.is_dir() { + let mut entries: Vec<_> = std::fs::read_dir(path) + .ok()? + .flatten() + .map(|e| e.path()) + .collect(); + entries.sort(); + for entry in entries { + walk(&entry, hasher)?; + } + } else { + meta.len().hash(hasher); + meta.modified().ok()?.hash(hasher); + } + Some(()) + } + let mut hasher = std::hash::DefaultHasher::new(); + walk(path, &mut hasher)?; + Some(hasher.finish()) +} diff --git a/crates/horizon-client/src/game/shader_packs.rs b/crates/horizon-client/src/game/shader_packs.rs index f534e8391..ed8ad61b7 100644 --- a/crates/horizon-client/src/game/shader_packs.rs +++ b/crates/horizon-client/src/game/shader_packs.rs @@ -1,5 +1,6 @@ //! Shader packs, for development until the Shader Packs page exists: `HORIZON_SHADER_PACK=` -//! runs `shaderpacks/` (a folder or a zip beside `options.txt`) over the world. The +//! runs `shaderpacks/` (a folder or a zip beside `options.txt`) over the world; Horizon's +//! own packs go to `game/native_shaders.rs`. The //! programs of the current dimension's folder are loaded when the dimension changes (spec 1.2). use horizon_client::camera::CameraFluid; @@ -41,15 +42,18 @@ impl Game<'_> { Dim::Nether => -1, Dim::End => 1, }; - if let Some(renderer) = &mut self.renderer + let path = std::env::current_dir() + .unwrap_or_default() + .join("shaderpacks") + .join(&name); + if horizon_shader_api::package::is_pack(&path) { + // One of Horizon's own packs (game/native_shaders.rs). + self.tick_native_shader_pack(&path); + } else if let Some(renderer) = &mut self.renderer && renderer.shader_pack_dimension() != Some(dimension) { let name = name.to_string_lossy(); let started = std::time::Instant::now(); - let path = std::env::current_dir() - .unwrap_or_default() - .join("shaderpacks") - .join(&*name); match Pack::open(&path) { Ok(pack) => { let macros = Macros::standard(); diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 419f83c4a..5db82b19a 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -67,6 +67,7 @@ mod end_sky; mod flat; pub mod lightning; mod lod; +mod native_post; mod occlusion; mod pack_atlas; pub mod pack_draw; @@ -499,6 +500,8 @@ pub struct Renderer { world_post: world_post::Post, /// The shader pack's post-processing, and the dimension (legacy id) it was loaded for. pack_post: Option<(i32, Option)>, + /// The post passes of one of Horizon's own shader packs; never beside `pack_post`'s. + native_post: Option, /// The entity vertices Horizon draws itself while a shader pack draws the others. pack_entity_rest: Vec, /// Set 1 of the cloud pipeline, for a shader pack's `gbuffers_clouds`. @@ -2409,6 +2412,7 @@ impl Renderer { occlusion: occlusion::Occlusion::new(crate::chunk::MIN_Y, crate::chunk::HEIGHT / 16), world_post, pack_post: None, + native_post: None, pack_entity_rest: Vec::new(), cloud_bindings, pack_weather: None, @@ -2916,8 +2920,38 @@ impl Renderer { (programs, p.notes.clone()) }); self.pack_post = Some((dimension, post)); + if self.pack_post.as_ref().is_some_and(|p| p.1.is_some()) { + self.native_post = None; + } report } + /// Runs the post passes of one of Horizon's own shader packs over the world from now on + /// (`None`: no pack), with its options' values; replaces a `shaders/` pack. Returns the + /// passes' labels; on an error the passes that ran keep running. + pub fn set_native_shader_pack( + &mut self, + pack: Option<(horizon_shader_api::Report, Vec<(String, f64)>)>, + ) -> std::result::Result, String> { + let stages = pack.map(|(report, constants)| { + let post: Vec<_> = report + .stages + .into_iter() + .filter(|s| s.kind == horizon_shader_api::StageKind::Post) + .collect(); + (post, constants) + }); + let Some((post, constants)) = stages.filter(|(post, _)| !post.is_empty()) else { + self.native_post = None; + return Ok(Vec::new()); + }; + let format = self.config.format.remove_srgb_suffix(); + let post = native_post::NativePost::new(&self.device, post, &constants, format)?; + let labels = post.labels().into_iter().map(str::to_owned).collect(); + self.pack_post = None; + self.pack_atlas = None; + self.native_post = Some(post); + Ok(labels) + } /// What a shader pack's gbuffers programs read from Horizon's geometry: the sky's, and /// terrain's in the pack's block atlas (made here when missing; none when it does not fit). pub fn pack_inputs(&mut self) -> horizon_shaders::GameInputs { @@ -3980,6 +4014,9 @@ impl Renderer { } // The world's targets: the screen, or the scaled target of `post`. let pack_post = !frame.title && self.pack_post.as_ref().is_some_and(|p| p.1.is_some()); + // One of Horizon's own packs reads the same world target. + let native_post = !frame.title && self.native_post.is_some(); + let pack_reads_world = pack_post || native_post; // The pack's program reads the vertices this frame's draws bind. let pack_draws_terrain = pack_terrain && flat_frame == self.pack_terrain_flat; // The shadow camera's lists, drawn by the pack's shadow programs. @@ -4023,7 +4060,7 @@ impl Renderer { let scaled = self.world_post.prepare( &self.device, [self.config.width, self.config.height], - pack_post, + pack_reads_world, ); let (world_view, world_depth) = match &scaled { Some((color, depth)) => (color, depth), @@ -4062,7 +4099,7 @@ impl Renderer { depth_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Clear(0.0), // Phase 2 of occlusion culling draws over it; a shader pack reads it. - store: if cull.is_some() || pack_post { + store: if cull.is_some() || pack_reads_world { wgpu::StoreOp::Store } else { wgpu::StoreOp::Discard @@ -4128,7 +4165,7 @@ impl Renderer { &mut encoder, world_view, world_depth, - pack_post, + pack_reads_world, ); pass.set_bind_group(0, &self.group, &[]); pass.set_pipeline(&flat.solid); @@ -4157,7 +4194,7 @@ impl Renderer { &mut encoder, world_view, world_depth, - pack_post, + pack_reads_world, ); pass.set_bind_group(0, &self.group, &[]); pass.set_pipeline(&self.solid); @@ -4427,6 +4464,20 @@ impl Renderer { &mut terrain, ); self.pack_post = Some((dimension, Some(post))); + } else if let Some((color, depth)) = &scaled + && native_post + && let Some(post) = &mut self.native_post + { + let size = self.world_post.size().unwrap_or([1, 1]); + post.run( + &self.device, + &self.queue, + &mut encoder, + (color, depth), + &view_target, + frame, + size, + ); } else if scaled.is_some() { self.world_post.finish(&mut encoder, &view_target); } diff --git a/crates/horizon-client/src/gfx/native_post.rs b/crates/horizon-client/src/gfx/native_post.rs new file mode 100644 index 000000000..c4af862d7 --- /dev/null +++ b/crates/horizon-client/src/gfx/native_post.rs @@ -0,0 +1,509 @@ +//! The post stage of Horizon's own shader packs (docs/shaders.md): each `[[post]]` pass is a +//! full-screen pipeline built from its checked naga module (`horizon_shader_api::check`), with +//! the pack's options as pipeline constants. A pass reads bind group 3: the frame, the world's +//! colour and depth, the previous pass's output and two samplers. Passes before the last write +//! half-float targets of the world's size, so values over 1 carry to the next; the last draws +//! onto the screen. + +use glam::{Mat4, Vec3}; +use horizon_shader_api::{Frame as FrameInputs, check::CheckedStage, stages::GROUP}; + +use super::Frame; + +const INTERMEDIATE: wgpu::TextureFormat = wgpu::TextureFormat::Rgba16Float; + +/// The world's colour and depth views a set of bind groups was made for. +type WorldViews = (wgpu::TextureView, wgpu::TextureView); + +pub struct NativePost { + passes: Vec<(String, wgpu::RenderPipeline)>, + layout: wgpu::BindGroupLayout, + frame: wgpu::Buffer, + linear: wgpu::Sampler, + nearest: wgpu::Sampler, + /// Two targets of the world's size, written in turn (none with a single pass). + targets: Option<([u32; 2], [wgpu::TextureView; 2])>, + /// One bind group per pass, for these world views. + groups: Option<(WorldViews, Vec)>, + start: std::time::Instant, + frames: u32, +} + +fn texture_entry(binding: u32, sample_type: wgpu::TextureSampleType) -> wgpu::BindGroupLayoutEntry { + wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + } +} + +fn sampler_entry(binding: u32) -> wgpu::BindGroupLayoutEntry { + wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), + count: None, + } +} + +impl NativePost { + /// Pipelines for `stages` (the pack's post passes, in order) with the option values + /// `constants`, drawing onto a screen of `format`. Errors name the pass wgpu refused. + pub fn new( + device: &wgpu::Device, + stages: Vec, + constants: &[(String, f64)], + format: wgpu::TextureFormat, + ) -> Result { + let float = wgpu::TextureSampleType::Float { filterable: true }; + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Native shader pack post"), + entries: &[ + wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: wgpu::BufferSize::new(size_of::() as u64), + }, + count: None, + }, + texture_entry(1, float), + texture_entry(2, wgpu::TextureSampleType::Depth), + texture_entry(3, float), + sampler_entry(4), + sampler_entry(5), + ], + }); + let mut groups: [Option<&wgpu::BindGroupLayout>; GROUP as usize + 1] = Default::default(); + groups[GROUP as usize] = Some(&layout); + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("Native shader pack post"), + bind_group_layouts: &groups, + ..Default::default() + }); + let count = stages.len(); + let mut passes = Vec::with_capacity(count); + for (i, stage) in stages.into_iter().enumerate() { + let present: Vec<(&str, f64)> = constants + .iter() + .filter(|(id, _)| { + stage + .module + .overrides + .iter() + .any(|(_, o)| o.name.as_ref() == Some(id)) + }) + .map(|(id, v)| (id.as_str(), *v)) + .collect(); + let scope = device.push_error_scope(wgpu::ErrorFilter::Validation); + let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some(&stage.label), + source: wgpu::ShaderSource::Naga(std::borrow::Cow::Owned(stage.module)), + }); + let options = wgpu::PipelineCompilationOptions { + constants: &present, + ..Default::default() + }; + let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some(&stage.label), + layout: Some(&pipeline_layout), + vertex: wgpu::VertexState { + module: &module, + entry_point: Some("horizon_vs_post"), + compilation_options: options.clone(), + buffers: &[], + }, + primitive: Default::default(), + depth_stencil: None, + multisample: Default::default(), + fragment: Some(wgpu::FragmentState { + module: &module, + entry_point: Some("horizon_fs_post"), + compilation_options: options, + targets: &[Some(wgpu::ColorTargetState { + format: if i + 1 == count { format } else { INTERMEDIATE }, + blend: None, + write_mask: wgpu::ColorWrites::ALL, + })], + }), + multiview_mask: None, + cache: None, + }); + if let Some(e) = pollster::block_on(scope.pop()) { + return Err(format!("{}: {e}", stage.file)); + } + passes.push((stage.label, pipeline)); + } + let frame = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Native shader pack frame"), + size: size_of::() as u64, + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + let sampler = |label, filter| { + device.create_sampler(&wgpu::SamplerDescriptor { + label: Some(label), + mag_filter: filter, + min_filter: filter, + ..Default::default() + }) + }; + Ok(Self { + passes, + layout, + frame, + linear: sampler("Native shader pack linear", wgpu::FilterMode::Linear), + nearest: sampler("Native shader pack nearest", wgpu::FilterMode::Nearest), + targets: None, + groups: None, + start: std::time::Instant::now(), + frames: 0, + }) + } + + /// The passes' labels, in order. + pub fn labels(&self) -> Vec<&str> { + self.passes.iter().map(|(l, _)| l.as_str()).collect() + } + + fn prepare( + &mut self, + device: &wgpu::Device, + world: (&wgpu::TextureView, &wgpu::TextureView), + size: [u32; 2], + ) { + if self.passes.len() > 1 && self.targets.as_ref().is_none_or(|t| t.0 != size) { + let target = |label| { + device + .create_texture(&wgpu::TextureDescriptor { + label: Some(label), + size: wgpu::Extent3d { + width: size[0], + height: size[1], + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: INTERMEDIATE, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }) + .create_view(&Default::default()) + }; + self.targets = Some(( + size, + [ + target("Native shader pack A"), + target("Native shader pack B"), + ], + )); + self.groups = None; + } + if self + .groups + .as_ref() + .is_some_and(|((color, depth), _)| color == world.0 && depth == world.1) + { + return; + } + let groups = (0..self.passes.len()) + .map(|i| { + // Pass i reads what pass i - 1 wrote: target (i - 1) % 2, the world for the first. + let previous = match (&self.targets, i) { + (Some((_, t)), 1..) => &t[(i - 1) % 2], + _ => world.0, + }; + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Native shader pack post"), + layout: &self.layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: self.frame.as_entire_binding(), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::TextureView(world.0), + }, + wgpu::BindGroupEntry { + binding: 2, + resource: wgpu::BindingResource::TextureView(world.1), + }, + wgpu::BindGroupEntry { + binding: 3, + resource: wgpu::BindingResource::TextureView(previous), + }, + wgpu::BindGroupEntry { + binding: 4, + resource: wgpu::BindingResource::Sampler(&self.linear), + }, + wgpu::BindGroupEntry { + binding: 5, + resource: wgpu::BindingResource::Sampler(&self.nearest), + }, + ], + }) + }) + .collect(); + self.groups = Some(((world.0.clone(), world.1.clone()), groups)); + } + + /// Runs the passes over the world target `world` (colour, depth) of `size`, the last onto + /// `screen`. + pub fn run( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + world: (&wgpu::TextureView, &wgpu::TextureView), + screen: &wgpu::TextureView, + frame: &Frame, + size: [u32; 2], + ) { + let seconds = self.start.elapsed().as_secs_f32() % 3600.0; + let inputs = frame_inputs(frame, size, seconds, self.frames); + self.frames = self.frames.wrapping_add(1); + self.draw(device, queue, encoder, world, screen, &inputs, size); + } + + fn draw( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + world: (&wgpu::TextureView, &wgpu::TextureView), + screen: &wgpu::TextureView, + inputs: &FrameInputs, + size: [u32; 2], + ) { + self.prepare(device, world, size); + queue.write_buffer(&self.frame, 0, bytemuck::bytes_of(inputs)); + let Some((_, groups)) = &self.groups else { + return; + }; + for (i, ((label, pipeline), group)) in self.passes.iter().zip(groups).enumerate() { + let view = match &self.targets { + Some((_, t)) if i + 1 < self.passes.len() => &t[i % 2], + _ => screen, + }; + let attachments = [Some(wgpu::RenderPassColorAttachment { + view, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::BLACK), + store: wgpu::StoreOp::Store, + }, + })]; + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some(label), + color_attachments: &attachments, + ..Default::default() + }); + pass.set_pipeline(pipeline); + pass.set_bind_group(GROUP, group, &[]); + pass.draw(0..3, 0..1); + } + } +} + +/// The frame as packs read it (docs/api/shaders-reference.md), from the renderer's `Frame`. +pub fn frame_inputs(frame: &Frame, size: [u32; 2], seconds: f32, index: u32) -> FrameInputs { + let aspect = size[0] as f32 / size[1].max(1) as f32; + let view = crate::camera::view_matrix(frame.yaw, frame.pitch); + let projection = + super::world_projection(frame.fov_degrees, aspect, frame.depth_far) * frame.bob; + // A celestial body's direction at `angle` degrees (SkyRenderer: Y -90°, then X by the + // angle, of straight up): overhead at 0, rising in the east (+x). + let body = |angle: f32| { + let a = angle.to_radians(); + Vec3::new(-a.sin(), a.cos(), 0.0) + }; + let (fog_color, fog_start, fog_end) = match &frame.fog { + Some(f) => ( + [f.color[0], f.color[1], f.color[2]], + f.environmental_start.min(f.render_distance_start), + f.environmental_end.min(f.render_distance_end), + ), + None => (frame.sky.fog_color, frame.distance * 0.9, frame.distance), + }; + let pack = &frame.pack; + let level = |l: i32| l as f32 / 15.0; + let m = |m: Mat4| m.to_cols_array_2d(); + FrameInputs { + view: m(view), + projection: m(projection), + view_inverse: m(view.inverse()), + projection_inverse: m(projection.inverse()), + camera_position: frame.camera.as_vec3().to_array(), + time: seconds, + sun_direction: body(frame.sky.sun_angle).to_array(), + day_time: pack.day_time.rem_euclid(24000) as f32 / 24000.0, + moon_direction: body(frame.sky.moon_angle).to_array(), + game_time: frame.shader_game_time, + sky_color: frame.sky.sky_color, + rain: pack.rain, + fog_color, + thunder: pack.thunder, + resolution: [size[0] as f32, size[1] as f32], + eye_light: [level(pack.eye_light[0]), level(pack.eye_light[1])], + fog_start, + fog_end, + render_distance: frame.distance, + far: frame.depth_far, + night_vision: frame.night_vision, + blindness: pack.blindness, + darkness: frame.darkness.1.clamp(0.0, 1.0), + brightness: pack.brightness, + eye_in: pack.eye_in as u32, + dimension: if frame.end_sky { + 1 + } else if frame.nether_lighting { + -1 + } else { + 0 + }, + frame_index: index, + hide_gui: u32::from(pack.hide_gui), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use horizon_shader_api::{MANIFEST, Pack, check}; + + fn device() -> Option<(wgpu::Device, wgpu::Queue)> { + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle()); + let adapter = pollster::block_on(instance.request_adapter(&Default::default())).ok()?; + pollster::block_on(adapter.request_device(&Default::default())).ok() + } + + fn texture( + device: &wgpu::Device, + format: wgpu::TextureFormat, + usage: wgpu::TextureUsages, + ) -> wgpu::Texture { + device.create_texture(&wgpu::TextureDescriptor { + label: None, + size: wgpu::Extent3d { + width: 4, + height: 4, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage, + view_formats: &[], + }) + } + + /// Two passes: the first doubles the world's colour (over 1, so the half-float target + /// must keep it) times an option, the second halves it and writes the depth as alpha. + #[test] + fn passes_chain_through_half_float_targets_with_options_and_depth() { + let Some((device, queue)) = device() else { + return; + }; + let manifest = "schema = 1\nid = \"t\"\nname = \"T\"\nversion = \"1\"\napi = \"0.1\"\n\ + [[post]]\nfile = \"a.wgsl\"\n[[post]]\nfile = \"b.wgsl\"\n\ + [[options]]\nid = \"gain\"\nname = \"Gain\"\ntype = \"float\"\ndefault = 0.0\nmin = 0.0\nmax = 2.0\n"; + let a = "override gain: f32;\nfn post(input: PostInput) -> vec4 { return vec4(input.color.rgb * 2.0 * gain, 1.0); }\n"; + let b = "fn post(input: PostInput) -> vec4 { return vec4(input.color.rgb * 0.5, input.depth); }\n"; + let files = [(MANIFEST, manifest), ("a.wgsl", a), ("b.wgsl", b)] + .map(|(n, t)| (n.to_owned(), t.as_bytes().to_vec())) + .into(); + let report = check(&Pack::from_files(files).unwrap()).unwrap(); + let format = wgpu::TextureFormat::Rgba8Unorm; + let mut post = + NativePost::new(&device, report.stages, &[("gain".into(), 1.0)], format).unwrap(); + assert_eq!(post.labels(), ["a.wgsl", "b.wgsl"]); + + let usage = wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING; + let color = texture(&device, format, usage).create_view(&Default::default()); + let depth = texture(&device, wgpu::TextureFormat::Depth32Float, usage) + .create_view(&Default::default()); + let screen = texture( + &device, + format, + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC, + ); + let mut encoder = device.create_command_encoder(&Default::default()); + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: None, + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: &color, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color { + r: 0.8, + g: 0.4, + b: 0.2, + a: 1.0, + }), + store: wgpu::StoreOp::Store, + }, + })], + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: &depth, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(0.5), + store: wgpu::StoreOp::Store, + }), + stencil_ops: None, + }), + ..Default::default() + }); + let view = screen.create_view(&Default::default()); + post.draw( + &device, + &queue, + &mut encoder, + (&color, &depth), + &view, + &FrameInputs::default(), + [4, 4], + ); + let buffer = device.create_buffer(&wgpu::BufferDescriptor { + label: None, + size: u64::from(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT * 4), + usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + encoder.copy_texture_to_buffer( + screen.as_image_copy(), + wgpu::TexelCopyBufferInfo { + buffer: &buffer, + layout: wgpu::TexelCopyBufferLayout { + offset: 0, + bytes_per_row: Some(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT), + rows_per_image: Some(4), + }, + }, + screen.size(), + ); + queue.submit([encoder.finish()]); + buffer + .slice(..) + .map_async(wgpu::MapMode::Read, |r| r.unwrap()); + device.poll(wgpu::PollType::wait_indefinitely()).unwrap(); + let pixel = buffer.slice(..).get_mapped_range().unwrap()[..4].to_vec(); + let near = |got: u8, want: u8| got.abs_diff(want) <= 1; + assert!( + near(pixel[0], 204) && near(pixel[1], 102) && near(pixel[2], 51) && near(pixel[3], 128), + "{pixel:?}" + ); + } +} diff --git a/crates/horizon-shader-api/packs/golden_hour/post/glow.wgsl b/crates/horizon-shader-api/packs/golden_hour/post/glow.wgsl index 4d523c9ef..e27b98244 100644 --- a/crates/horizon-shader-api/packs/golden_hour/post/glow.wgsl +++ b/crates/horizon-shader-api/packs/golden_hour/post/glow.wgsl @@ -1,9 +1,12 @@ -// A soft halo around bright pixels: twelve taps on two rings, scaled with -// the picture's height so it looks the same at any resolution. +// A soft halo around the brightest pixels (the sun, lava, torches at night): twelve taps on two +// rings, scaled with the picture's height so it looks the same at any resolution. The daytime +// sky is bright too, so it glows only a quarter as much. override glow: f32 = 0.35; -fn bright(c: vec3) -> vec3 { - return c * smoothstep(0.7, 1.0, luminance(c)); +fn bright(uv: vec2) -> vec3 { + let c = textureSampleLevel(previous, linear_sampler, uv, 0.0).rgb; + let sky = select(1.0, 0.25, depth_at(uv) == 0.0); + return c * smoothstep(0.8, 1.0, luminance(c)) * sky; } fn post(input: PostInput) -> vec4 { @@ -16,7 +19,7 @@ fn post(input: PostInput) -> vec4 { let angle = f32(i) * 0.5235988; let radius = select(3.0, 7.0, (i & 1) == 1); let offset = vec2(cos(angle), sin(angle)) * radius * step; - sum += bright(textureSampleLevel(previous, linear_sampler, input.uv + offset, 0.0).rgb); + sum += bright(input.uv + offset); } - return vec4(input.color.rgb + sum * (glow / 6.0), input.color.a); + return vec4(input.color.rgb + sum * (glow / 8.0), input.color.a); } diff --git a/crates/horizon-shader-api/src/inputs.rs b/crates/horizon-shader-api/src/inputs.rs index 5bc82fd82..f25fe2847 100644 --- a/crates/horizon-shader-api/src/inputs.rs +++ b/crates/horizon-shader-api/src/inputs.rs @@ -158,7 +158,7 @@ pub const POST_INPUT: Struct = Struct { doc: "One pixel of the picture, for `fn post(input: PostInput) -> vec4`.", fields: fields! { uv: "vec2" = "Where the pixel is, 0..1 from the top left.", - pixel: "vec2" = "The pixel's centre, in pixels from the top left.", + pixel: "vec2" = "The pixel's centre, in pixels from the top left of what the pass draws (the world's size; the window's for the last pass).", color: "vec4" = "The previous pass's colour here (the world's picture for the first pass).", depth: "f32" = "The world's depth here, reversed: 0 is the sky.", }, diff --git a/crates/horizon-shader-api/src/package.rs b/crates/horizon-shader-api/src/package.rs index e2e644a04..b148d95c5 100644 --- a/crates/horizon-shader-api/src/package.rs +++ b/crates/horizon-shader-api/src/package.rs @@ -46,6 +46,13 @@ pub fn plain_path(path: &str) -> bool { }) } +/// Whether `path` is a pack of this format: a folder holding +/// `horizon-shaders.toml`, or a `.hshader` file. +pub fn is_pack(path: &Path) -> bool { + path.join(MANIFEST).is_file() + || (path.is_file() && path.extension().is_some_and(|e| e == EXTENSION)) +} + impl Pack { /// Reads `path`: a `.hshader` file, or a folder (its hidden files and /// folders, such as `.git`, are skipped). diff --git a/docs/api/shaders-reference.md b/docs/api/shaders-reference.md index f9682a1a4..400d5029b 100644 --- a/docs/api/shaders-reference.md +++ b/docs/api/shaders-reference.md @@ -77,7 +77,7 @@ One pixel of the picture, for `fn post(input: PostInput) -> vec4`. | Field | Type | Meaning | | --- | --- | --- | | `uv` | `vec2` | Where the pixel is, 0..1 from the top left. | -| `pixel` | `vec2` | The pixel's centre, in pixels from the top left. | +| `pixel` | `vec2` | The pixel's centre, in pixels from the top left of what the pass draws (the world's size; the window's for the last pass). | | `color` | `vec4` | The previous pass's colour here (the world's picture for the first pass). | | `depth` | `f32` | The world's depth here, reversed: 0 is the sky. | diff --git a/docs/api/shaders.md b/docs/api/shaders.md index 8e9e6919c..675c5e3fc 100644 --- a/docs/api/shaders.md +++ b/docs/api/shaders.md @@ -96,6 +96,7 @@ stages with options and hot reload, then the overlay, lighting, sky and shadow s ## Changelog +- 2026-10-26: the post stage in the game (`HORIZON_SHADER_PACK`, hot reload with the last good pack kept). - 2026-10-25: `horizon-shader` (`new`, `check`, `pack`), three templates, the example pack and the guide. - 2026-10-25: the check (naga validation, signatures, options, budgets, SPIR-V/Metal/HLSL translation). - 2026-10-25: the stages of API 0.1 (sky, material, post), their inputs and the generated reference. diff --git a/docs/shaders.md b/docs/shaders.md index 99a08fa9a..ef8521cfe 100644 --- a/docs/shaders.md +++ b/docs/shaders.md @@ -13,7 +13,8 @@ Horizon's own format. | Authoring tool | `horizon-shader` (same crate) | `new`, `check`, `pack` | | Templates | `crates/horizon-shader-api/templates/` | post, sky, material | | Example pack | `crates/horizon-shader-api/packs/golden_hour` | a warm sky and two post passes | -| In the game, the launcher, the registry | | not yet: the renderer hooks come next | +| In the game: the post stage | `HORIZON_SHADER_PACK=` (for development, until the Shader Packs page) | post passes, hot reload | +| In the game: sky and material; the launcher and the registry | | not yet | ## Quick start @@ -29,9 +30,9 @@ horizon-shader pack my_pack # check, then write my_pack- ``` golden_hour 1.0.0 (shader API 0.1): ok sky Warm horizon 99 expressions, 0 texture reads - post Glow 185 expressions, 3 texture reads + post Glow 192 expressions, 3 texture reads post Colour grade 201 expressions, 2 texture reads - sha256 fd92a294... + sha256 b5e0d98c... ``` or every problem, at its file, line and column: @@ -43,6 +44,15 @@ my_pack 0.1.0 (shader API 0.1): FAILED `--json` prints the same for tools (`ok`, `id`, `version`, `api`, `sha256`, `stages`, `problems`). +## Trying a pack in the game + +Until the Shader Packs page lists them, put the pack folder (or its `.hshader`) in `shaderpacks/` beside `options.txt` +and start the game with `HORIZON_SHADER_PACK=`. The game checks the pack first, and refuses +one that fails, with its problems in the chat. While it runs, the game looks at the pack twice a second: save a file +and the pack is checked and rebuilt in place ("Shader pack ... reloaded"); if the new version fails, the chat says +where and the last good version keeps drawing. The post passes run today; the sky and material stages are checked +but not drawn yet. + ## A pack A folder (or the same files zipped as a `.hshader`) with `horizon-shaders.toml` and WGSL files: From 2c5dac354eb0f5b7c3812803e5d4798f54973c34 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 06:11:01 +0100 Subject: [PATCH 238/238] parity-main: name a merge only when HEAD fails again and the base passes The bisect ended on the newest merge when the failed harnesses pass everywhere now (a flake, or a runner that was missing the sounds), and on the oldest when they fail everywhere (the runner), and commented on that pull request either way. It now reruns them at HEAD and at the base first and names nobody unless HEAD fails and the base passes. Co-Authored-By: Claude Opus 5.5 --- tests/test_parity_main.py | 29 +++++++++++++++++++++++++++++ tools/parity_main.py | 17 ++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/tests/test_parity_main.py b/tests/test_parity_main.py index 7a72ecae5..aa58375a6 100644 --- a/tests/test_parity_main.py +++ b/tests/test_parity_main.py @@ -33,6 +33,35 @@ class ParityMainTests(unittest.TestCase): self.assertEqual(parity_main.base(runs=[]), parent, "no green run yet: the last merge") self.assertEqual(parity_main.base(runs=[head]), head) + def test_bisect_names_a_merge_only_when_head_fails_and_the_base_passes(self): + with tempfile.TemporaryDirectory() as tmp, mock.patch.object(parity_main, "ROOT", Path(tmp)): + git = lambda *arguments: parity_main.git(*arguments).stdout.strip() + git("init", "-q") + for n in range(4): + git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", + "-m", f"Merge pull request #{n} from x/y") + commits = git("rev-list", "--reverse", "HEAD").split() + last_run = Path(tmp) / ".local/parity-runner/last-run.json" + last_run.parent.mkdir(parents=True) + last_run.write_text('{"failed": ["music"]}') + real_run = parity_main.subprocess.run + for broken_from, blamed in ((None, None), (0, None), (2, "2")): + comments = [] + # git runs for real in the scratch repo; a `gh pr comment` is only recorded, never sent. + fake_run = lambda command, **kwargs: (comments.append(command) if command[0] == "gh" + else real_run(command, **kwargs)) + with mock.patch.object(parity_main, "LAST_RUN", last_run), \ + mock.patch.object(parity_main, "run_harnesses", lambda names, timeout: broken_from is not None + and commits.index(git("rev-parse", "HEAD")) >= broken_from), \ + mock.patch.object(parity_main.subprocess, "run", fake_run), \ + mock.patch("builtins.print") as printed: + parity_main.bisect(commits[0], 60, comment=True) + if blamed: + self.assertEqual([command[3] for command in comments], [blamed]) + else: + self.assertEqual(comments, [], printed.call_args_list[-1]) + self.assertEqual(git("rev-parse", "HEAD"), commits[-1], "HEAD checked out again") + def test_a_harness_a_commit_does_not_have_cannot_fail_there(self): with mock.patch.object(parity_main.subprocess, "run") as run: self.assertFalse(parity_main.run_harnesses(["not_a_harness_yet"], 60)) diff --git a/tools/parity_main.py b/tools/parity_main.py index ff319aed0..76fa49070 100644 --- a/tools/parity_main.py +++ b/tools/parity_main.py @@ -8,7 +8,7 @@ so a run tests every merge since the last green one (`tools/parity.py --changed `), however many landed while the previous run went on. `bisect` reads the harnesses that failed (.local/parity-runner/last-run.json), checks out the first-parent merges between BASE and HEAD by halves and runs only those harnesses, then names the first merge they -fail on (written to $GITHUB_STEP_SUMMARY and, with --comment, on its pull request). HEAD is checked out again at the end. +fail on, once they fail again at HEAD and pass at BASE (written to $GITHUB_STEP_SUMMARY and, with --comment, on its pull request). HEAD is checked out again at the end. """ import argparse @@ -90,7 +90,22 @@ def bisect(since, timeout, comment): git("checkout", "-q", "--detach", commit) return run_harnesses(failed, timeout) + def blame_nobody(why): + line = f"parity-main: {', '.join(failed)} {why}: no merge is named." + print(line) + if os.environ.get("GITHUB_STEP_SUMMARY"): + with open(os.environ["GITHUB_STEP_SUMMARY"], "a", encoding="utf-8") as summary: + summary.write(f"### {line}\n") + return 0 + + # A merge is named only when the harnesses fail again at HEAD and pass at the base: otherwise the search would + # end on the newest merge (a flake, or a runner missing files, passes everywhere now) or the oldest one (the + # runner fails at every commit) and blame it for nothing. try: + if not fails(head): + return blame_nobody(f"pass when run again at {head[:9]}: a flake or the runner") + if fails(since): + return blame_nobody(f"fail at the base {since[:9]} too: the runner or a flake") culprit = first_bad(commits, fails) if len(commits) > 1 else commits[0] finally: git("checkout", "-q", "--detach", head)