diff --git a/.github/workflows/launcher.yml b/.github/workflows/launcher.yml index 2ab491207..c902acf32 100644 --- a/.github/workflows/launcher.yml +++ b/.github/workflows/launcher.yml @@ -1,7 +1,6 @@ name: launcher # Builds the launcher on Linux, macOS and Windows, and releases it by itself (docs/release-flow.md): after a -# push to main that changes the launcher or what it builds in (crates/horizon-prepare, crates/horizon-theme, -# crates/horizon-telemetry), +# push to main that changes the launcher or a crate it builds in (the paths below), # every 10 minutes (a release waiting for merges to settle goes out) and by hand, tools/release_train.py decides as # for the game. # A release is launcher-vX.Y.Z: the patch after the last launcher tag, or the next minor when a merged pull @@ -14,10 +13,15 @@ on: push: branches: [main, launcher, 'launcher-*'] paths: + # Every crate the launcher builds in, as tools/release_train.py's LAUNCHER_PATHS. - 'launcher/**' + - 'crates/horizon-bench/**' + - 'crates/horizon-friends/**' + - 'crates/horizon-mods/**' + - 'crates/horizon-port-pack/**' - 'crates/horizon-prepare/**' - - 'crates/horizon-theme/**' - 'crates/horizon-telemetry/**' + - 'crates/horizon-theme/**' - '.github/workflows/launcher.yml' schedule: - cron: '7-59/10 * * * *' @@ -138,7 +142,8 @@ jobs: echo "::notice::No Metal toolchain on this runner: GPUI compiles its shaders at run time" echo "LAUNCHER_FEATURES=gpui/runtime_shaders" >> "$GITHUB_ENV" fi - - run: cargo test --release --locked --lib ${LAUNCHER_FEATURES:+--features "$LAUNCHER_FEATURES"} + # Every test target (the library, the binary, tests/), on each system the launcher ships for. + - run: cargo test --release --locked ${LAUNCHER_FEATURES:+--features "$LAUNCHER_FEATURES"} - run: cargo build --release --locked ${LAUNCHER_FEATURES:+--features "$LAUNCHER_FEATURES"} - name: The version is the release's run: | diff --git a/.gitignore b/.gitignore index eb29ad10d..7fffd6a99 100644 --- a/.gitignore +++ b/.gitignore @@ -28,5 +28,6 @@ /horizon-player.txt /servers.dat /screenshots/ +/.horizon-cache/ /crash-reports/ /debug/ diff --git a/Cargo.lock b/Cargo.lock index 1d5897d11..2f389dfef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1573,6 +1573,7 @@ dependencies = [ "horizon-protocol", "horizon-world", "log", + "ureq", ] [[package]] @@ -1608,7 +1609,10 @@ dependencies = [ "horizon-core", "libm", "lz4_flex", + "postcard", + "serde", "serde_json", + "sha2", "xxhash-rust", ] diff --git a/benchmarks/server-equal-work-2026-10-17/README.md b/benchmarks/server-equal-work-2026-10-17/README.md new file mode 100644 index 000000000..737b05858 --- /dev/null +++ b/benchmarks/server-equal-work-2026-10-17/README.md @@ -0,0 +1,168 @@ +# Horizon's server against vanilla 26.3, equal work (2026-10-17) + +The same bots on Horizon's dedicated server and on vanilla 26.3's, one server at a time, on the same +machine, with the work each server does made the same before the numbers are compared. This replaces the +first run in docs/server.md section 9, where the work wasn't equal. + +## What was made equal + +The first run (PR #556) had three differences that made the comparison unfair. Each is fixed in the code +that ran here: + +- **Refused moves.** Vanilla pulls a player back when a move is too fast ("moved too quickly") or ends + inside a block ("moved wrongly"). Horizon now runs both checks the way vanilla does (PR #586), so a bot + that flies too fast is pulled back on both servers. +- **Chunk sending.** Vanilla paces chunks by the client's acknowledgements (PlayerChunkSender: one + unacknowledged batch at first, then ten, each at most the client's desired chunks per tick). Horizon used + to send twice as many per batch; it now paces them the same way (PR #586), and the bots acknowledge + batches and ask for a rate the way vanilla's client does, so both servers send at the pace the client + asks for. Chunks sent again while the bot still held + them (`resent`) are counted: 0 on both servers in every run. +- **Movement gated on chunks.** A bot doesn't move while the chunk it stands in hasn't arrived (vanilla's + client waits the same way), so a slower server isn't asked to teleport bots into terrain it hasn't sent. + +One difference isn't in Horizon yet: vanilla 26.3's locator bar (waypoints), which costs vanilla O(players²) +work per move. Every vanilla run here sets `gamerule locator_bar false`, so neither server does it. + +## Method + +`tools/server_bench.py` runs each configuration on a fresh server: + +- the same `server.properties` on both (seed 12345, view and simulation distance 10, offline, creative, + difficulty easy, `sync-chunk-writes=true`), and `gamerule respawn_radius 0`; +- the server waits for `Done`, the bots join one a second, a 60-second warm-up, then 300 seconds of + measurement; +- MSPT (average, P50, P95, P99) comes from each server's own `/tick query`, asked every 5 seconds on the + console, so both measure themselves the same way (vanilla's ServerTickRateManager numbers, which Horizon + reproduces); TPS is 20 while the average fits in 50 ms, else 1000 / MSPT; +- RSS and CPU are the server's whole process tree, once a second (100 % is one core); +- chunk counts come from the bots: chunks received per second, chunks no bot had before (new terrain), and + pull-backs (position packets that move a bot back). + +The bots are `horizon-bots` (creative, flying 24 blocks above spawn, 20 moves a second): + +- `walk`: flies to random points within 48 blocks of spawn at sprint speed (5.6 blocks a second); +- `explore`: flies straight outwards, by default at about 22 blocks a second, as fast as the server sends + chunks. + +Machine: the ThinkPad (Intel Core i7-7500U, 2 cores / 4 threads, 32 GB, NixOS), servers under `nice -n 19` +because it also runs other services; vanilla on OpenJDK 25.0.4.1 with a 4 GB heap (`-Xms4g -Xmx4g`). The +bots ran on the Mac mini and reached the ThinkPad over the tailnet, so they took none of the server's CPU. + +What ran: Horizon from `origin/main` at bf851bfe with #586 (move checks and chunk pacing), the bots and +`server_bench.py` from this PR, #623 (sections encoded once, its first commit) and #649 (a tick's packets +handed to each connection together). The ThinkPad copy wasn't a git checkout, so the JSON reports have an +empty `commit`. Vanilla: the pinned 26.3 server jar. + +## Results + +MSPT is the average milliseconds per tick (50 is the limit for 20 TPS); P95 is the 95th percentile. CPU is +the whole process tree. "New chunks/s" are chunks no bot had before. + +### Walk (the same work on both servers) + +| Bots | Server | MSPT | P95 | TPS | RSS | CPU | Chunks/s | New chunks/s | +|---:|---|---:|---:|---:|---:|---:|---:|---:| +| 1 | Horizon | 5.5 | 12.1 | 20.0 | 1.37 GB | 57 % | 9 | 0.3 | +| 1 | Vanilla | 18.3 | 27.3 | 20.0 | 3.35 GB | 66 % | 10 | 0.3 | +| 10 | Horizon | 11.5 | 22.1 | 20.0 | 1.41 GB | 124 % | 94 | 2.4 | +| 10 | Vanilla | 16.8 | 30.5 | 20.0 | 3.41 GB | 84 % | 100 | 2.8 | +| 25 | Horizon | 20.7 | 40.4 | 20.0 | 1.45 GB | 170 % | 236 | 5.7 | +| 25 | Vanilla | 16.0 | 26.4 | 20.0 | 3.47 GB | 115 % | 252 | 6.7 | +| 50 | Horizon | 31.4 | 79.0 | 20.0 | 1.52 GB | 173 % | 451 | 10.3 | +| 50 | Vanilla | 20.5 | 29.7 | 20.0 | 3.55 GB | 194 % | 502 | 12.2 | +| 100 | Horizon | 114.5 | 256.2 | 8.7 | 1.33 GB | 181 % | 484 | 30.8 | +| 100 | Vanilla | stopped by its watchdog | | | | | | | + +Both servers sent the same chunks (about 10 a second per bot, the churn of flying around) and sent none +twice. Vanilla pulled back a few bots for moving too quickly when a tick ran long (5 to 33 per run); +Horizon none. At 100 bots vanilla's watchdog stopped the server (a tick ran over 60 s, in +`ChunkMap$TrackedEntity.updatePlayer`); Horizon kept running at 8.7 TPS. + +### Explore at a rate both servers keep up with (equal work) + +The bots fly outwards at 0.8 blocks a second (`--explore-speed 0.8`), slow enough that neither server makes +them wait for terrain, so both generate and send the same new chunks. + +| Bots | Server | MSPT | P95 | TPS | RSS | CPU | Chunks/s | New chunks/s | +|---:|---|---:|---:|---:|---:|---:|---:|---:| +| 1 | Horizon | 4.0 | 6.6 | 20.0 | 1.36 GB | 25 % | 2 | 1.3 | +| 1 | Vanilla | 18.1 | 27.1 | 20.0 | 3.32 GB | 73 % | 2 | 1.4 | +| 10 | Horizon | 13.5 | 24.8 | 20.0 | 1.64 GB | 89 % | 13 | 11.8 | +| 10 | Vanilla | 29.2 | 43.0 | 20.0 | 3.52 GB | 143 % | 13 | 11.9 | +| 25 | Horizon | 20.2 | 36.8 | 20.0 | 1.74 GB | 121 % | 32 | 28.9 | +| 25 | Vanilla | 37.0 | 49.4 | 20.0 | 3.73 GB | 169 % | 32 | 28.9 | + +### Explore as fast as each server sends chunks (not equal work) + +The default 22 blocks a second is faster than either server can generate, so each server sets the pace and +a faster server is given more work: from 10 bots Horizon generated 1.5 to 2 times vanilla's new chunks a +second. Read it as throughput, not as a like-for-like tick time. + +| Bots | Server | MSPT | P95 | TPS | RSS | CPU | Chunks/s | New chunks/s | +|---:|---|---:|---:|---:|---:|---:|---:|---:| +| 1 | Horizon | 10.6 | 24.6 | 20.0 | 1.34 GB | 322 % | 39 | 34.6 | +| 1 | Vanilla | 9.9 | 21.3 | 20.0 | 3.48 GB | 245 % | 36 | 33.4 | +| 10 | Horizon | 30.4 | 52.3 | 20.0 | 2.18 GB | 345 % | 47 | 46.7 | +| 10 | Vanilla | 17.0 | 29.6 | 20.0 | 4.04 GB | 242 % | 22 | 22.4 | +| 25 | Horizon | 50.7 | 89.3 | 19.7 | 2.30 GB | 286 % | 50 | 49.7 | +| 25 | Vanilla | 24.2 | 36.5 | 20.0 | 4.40 GB | 320 % | 34 | 33.7 | +| 50 | Horizon | 50.7 | 102.7 | 19.7 | 1.72 GB | 213 % | 97 | 96.5 | +| 50 | Vanilla | stopped by its watchdog | | | | | | | +| 100 | Horizon | 92.9 | 139.6 | 10.8 | 1.70 GB | 322 % | 114 | 113.6 | +| 100 | Vanilla | stopped by its watchdog | | | | | | | + +Vanilla's watchdog stopped it at 50 bots (in `ClientboundLightUpdatePacketData`, copying light for a chunk +packet) and at 100 (in `Entity.move` from a move packet). + +## Findings + +- Horizon's RSS is 1.9 to 2.6 times smaller in every run, and it starts in 11 s against vanilla's 15 s. + Vanilla's heap is fixed at 4 GB (`-Xms4g`), so part of its RSS is that setting rather than need. +- With 1 bot Horizon's tick is 3.3 times shorter walking and 4.5 times shorter exploring at equal work; + at 10 bots it is still shorter in both. +- At equal exploring work Horizon's tick is about half of vanilla's at 10 and 25 bots. +- When the bots only fly around spawn (`walk`), vanilla's tick hardly grows with players (16 to 20 ms from + 1 to 50 bots) while Horizon's grows by about 0.5 ms per bot, so vanilla is ahead from about 20 bots: 16.0 + against 20.7 ms at 25 and 20.5 against 31.4 ms at 50. Both keep 20 TPS up to 50. +- With 100 bots vanilla's watchdog stops it in every pass, and Horizon keeps running at 9 to 11 TPS. + +### Where Horizon's walk tick goes at 25 bots + +Profiled on the ThinkPad with `perf` (the server thread, 20 s, self time): + +| Share | What | Whose | +|---:|---|---| +| 18 % | `Streaming::update`: every tick it walks all loaded columns and proto chunks and tests each against every player's area (unloading, dropping protos, finishing and decoration candidates), so the cost grows with players times chunks | streaming / engine tick | +| 20 % | `PalettedContainer::get` and `World::block`, mostly from block entity ticks: on the Mac profile `tick_hoppers` looks up the block under every block entity in the loaded world each tick and compares names as strings, and spawners rebuild their display id | engine tick | +| 8 % | `light_data` when a chunk is sent: the light layers were built twice per chunk; a follow-up commit on #623 builds them once and keeps each section's packed layers | server (this work) | +| 7 % | encoding chunk sections and heightmaps for the chunks each bot newly sees | server | +| 4 % | `Hub::tick` (moves, tracking) | server | +| 2 % | the moved-wrongly collision check | server | + +The first two grow with the number of players but aren't specific to the server: they are the +engine's tick and streaming, reported to perf-server-2. Before #623 and #649, encoding every chunk section +once per player and waking each connection's writer thread once per packet cost more than everything above +(at 50 walking bots the tick was 281 ms; see the earlier runs). + +## Earlier runs (same machine and method) + +- `run-1-before-chunk-cache.json`: before #623, with vanilla's locator bar on. Horizon's walk tick was + 27.5, 40.7, 281 and 467 ms at 10, 25, 50 and 100 bots, and vanilla's 33.2 ms at 50 (the locator bar). +- `run-2-chunk-cache.json`: Horizon with #623 only: 13.2, 23.5, 31.4 and 84.4 ms walking at 10 to 100 + bots. + +## Files + +- `horizon.json`, `vanilla.json`: the walk and explore passes (vanilla with `locator_bar=false`). +- `explore-equal-rate.json`: the explore pass at 0.8 blocks a second, both servers. +- `run-1-before-chunk-cache.json`, `run-2-chunk-cache.json`: the earlier runs. + +To run it again (bots on another machine reached at `--connect`): + + cargo build --release -p horizon-client --bin horizon-client -p horizon-server -p horizon-bots + python3 tools/server_bench.py --servers horizon,vanilla --bots 1,10,25,50,100 --behaviour walk,explore \ + --gamerule locator_bar=false --nice 19 --bots-via 'ssh macmini' --bots-bin /path/to/horizon-bots \ + --connect --out results.json + python3 tools/server_bench.py --servers horizon,vanilla --bots 1,10,25 --behaviour explore \ + --explore-speed 0.8 --gamerule locator_bar=false --nice 19 ... --out explore-equal-rate.json diff --git a/benchmarks/server-equal-work-2026-10-17/explore-equal-rate.json b/benchmarks/server-equal-work-2026-10-17/explore-equal-rate.json new file mode 100644 index 000000000..bd097e43a --- /dev/null +++ b/benchmarks/server-equal-work-2026-10-17/explore-equal-rate.json @@ -0,0 +1,267 @@ +{ + "kind": "horizon-server-benchmark", + "date": "2026-10-17T05:47:12+00:00", + "machine": { + "system": "Linux", + "machine": "x86_64", + "cpus": 4 + }, + "commit": "", + "java": [ + "openjdk 25.0.4.1 2026-08-18" + ], + "settings": { + "servers": "horizon,vanilla", + "bots": "1,10,25", + "behaviour": "explore", + "join_every": 1.0, + "warmup": 60, + "seconds": 300, + "seed": 12345, + "explore_speed": 0.8, + "gamerule": "locator_bar=false", + "heap": "4g", + "out": "/home/ludovic/scratch/sh2-bench5.json", + "nice": 19, + "bots_via": "ssh -o BatchMode=yes macmini", + "bots_bin": "/tmp/sh2-horizon-bots", + "connect": "100.72.51.8", + "properties": { + "level-seed": "12345", + "view-distance": "10", + "simulation-distance": "10", + "online-mode": "false", + "white-list": "false", + "gamemode": "creative", + "force-gamemode": "true", + "spawn-protection": "0", + "max-players": "200", + "difficulty": "easy", + "sync-chunk-writes": "true" + } + }, + "runs": [ + { + "server": "horizon", + "bots": 1, + "behaviour": "explore", + "start_seconds": 10.91, + "mspt": { + "average": 4.02, + "p50": 3.75, + "p95": 6.63, + "p99": 14.55 + }, + "mspt_worst_p99": 63.3, + "tps": 20.0, + "rss_mb": { + "mean": 1392.42, + "max": 1746.1 + }, + "cpu_percent": 24.52, + "chunks_per_second": 1.69, + "unique_per_second": 1.34, + "forgotten_per_second": 1.62, + "waited_ticks_per_second": 0.0, + "chunks_total": 1048, + "chunks_per_bot": { + "explore": 1048.0 + }, + "unique_chunks_per_bot": { + "explore": 952.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 1, + "behaviour": "explore", + "start_seconds": 15.13, + "mspt": { + "average": 18.12, + "p50": 18.78, + "p95": 27.07, + "p99": 60.81 + }, + "mspt_worst_p99": 1397.6, + "tps": 20.0, + "rss_mb": { + "mean": 3397.31, + "max": 3430.2 + }, + "cpu_percent": 72.99, + "chunks_per_second": 1.69, + "unique_per_second": 1.38, + "forgotten_per_second": 1.62, + "waited_ticks_per_second": 0.0, + "chunks_total": 1048, + "chunks_per_bot": { + "explore": 1048.0 + }, + "unique_chunks_per_bot": { + "explore": 952.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 10, + "behaviour": "explore", + "start_seconds": 11.12, + "mspt": { + "average": 13.49, + "p50": 12.09, + "p95": 24.81, + "p99": 40.9 + }, + "mspt_worst_p99": 101.4, + "tps": 20.0, + "rss_mb": { + "mean": 1684.04, + "max": 2660.6 + }, + "cpu_percent": 89.04, + "chunks_per_second": 13.08, + "unique_per_second": 11.84, + "forgotten_per_second": 13.02, + "waited_ticks_per_second": 0.0, + "chunks_total": 9277, + "chunks_per_bot": { + "explore": 927.7 + }, + "unique_chunks_per_bot": { + "explore": 889.9 + }, + "resent": 0, + "pulled_back": 3381, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "explore", + "start_seconds": 14.84, + "mspt": { + "average": 29.18, + "p50": 26.76, + "p95": 43.02, + "p99": 80.16 + }, + "mspt_worst_p99": 425.5, + "tps": 20.0, + "rss_mb": { + "mean": 3609.51, + "max": 3791.6 + }, + "cpu_percent": 142.79, + "chunks_per_second": 13.1, + "unique_per_second": 11.88, + "forgotten_per_second": 13.12, + "waited_ticks_per_second": 0.0, + "chunks_total": 9245, + "chunks_per_bot": { + "explore": 924.5 + }, + "unique_chunks_per_bot": { + "explore": 885.3 + }, + "resent": 0, + "pulled_back": 3388, + "moved_too_quickly": 6, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "explore", + "start_seconds": 11.02, + "mspt": { + "average": 20.25, + "p50": 18.73, + "p95": 36.78, + "p99": 55.76 + }, + "mspt_worst_p99": 92.3, + "tps": 20.0, + "rss_mb": { + "mean": 1782.26, + "max": 2741.9 + }, + "cpu_percent": 120.8, + "chunks_per_second": 32.22, + "unique_per_second": 28.89, + "forgotten_per_second": 32.19, + "waited_ticks_per_second": 0.0, + "chunks_total": 23357, + "chunks_per_bot": { + "explore": 934.28 + }, + "unique_chunks_per_bot": { + "explore": 890.32 + }, + "resent": 0, + "pulled_back": 11580, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "explore", + "start_seconds": 14.54, + "mspt": { + "average": 37.04, + "p50": 34.77, + "p95": 49.42, + "p99": 101.67 + }, + "mspt_worst_p99": 735.2, + "tps": 20.0, + "rss_mb": { + "mean": 3815.46, + "max": 4202.0 + }, + "cpu_percent": 169.15, + "chunks_per_second": 32.34, + "unique_per_second": 28.93, + "forgotten_per_second": 32.31, + "waited_ticks_per_second": 0.0, + "chunks_total": 23306, + "chunks_per_bot": { + "explore": 932.24 + }, + "unique_chunks_per_bot": { + "explore": 886.96 + }, + "resent": 0, + "pulled_back": 11539, + "moved_too_quickly": 6, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 59 + } + ] +} diff --git a/benchmarks/server-equal-work-2026-10-17/horizon.json b/benchmarks/server-equal-work-2026-10-17/horizon.json new file mode 100644 index 000000000..d0b162c44 --- /dev/null +++ b/benchmarks/server-equal-work-2026-10-17/horizon.json @@ -0,0 +1,415 @@ +{ + "kind": "horizon-server-benchmark", + "date": "2026-10-17T05:04:57+00:00", + "machine": { + "system": "Linux", + "machine": "x86_64", + "cpus": 4 + }, + "commit": "", + "java": [ + "openjdk 25.0.4.1 2026-08-18" + ], + "settings": { + "servers": "horizon", + "bots": "1,10,25,50,100", + "behaviour": "walk,explore", + "join_every": 1.0, + "warmup": 60, + "seconds": 300, + "seed": 12345, + "explore_speed": 0, + "gamerule": "locator_bar=false", + "heap": "4g", + "out": "/home/ludovic/scratch/sh2-bench4.json", + "nice": 19, + "bots_via": "ssh -o BatchMode=yes macmini", + "bots_bin": "/tmp/sh2-horizon-bots", + "connect": "100.72.51.8", + "properties": { + "level-seed": "12345", + "view-distance": "10", + "simulation-distance": "10", + "online-mode": "false", + "white-list": "false", + "gamemode": "creative", + "force-gamemode": "true", + "spawn-protection": "0", + "max-players": "200", + "difficulty": "easy", + "sync-chunk-writes": "true" + } + }, + "runs": [ + { + "server": "horizon", + "bots": 1, + "behaviour": "walk", + "start_seconds": 11.02, + "mspt": { + "average": 5.45, + "p50": 4.63, + "p95": 12.14, + "p99": 30.63 + }, + "mspt_worst_p99": 91.5, + "tps": 20.0, + "rss_mb": { + "mean": 1398.5, + "max": 1708.4 + }, + "cpu_percent": 57.03, + "chunks_per_second": 9.17, + "unique_per_second": 0.26, + "forgotten_per_second": 9.2, + "waited_ticks_per_second": 0.0, + "chunks_total": 3708, + "chunks_per_bot": { + "walk": 3708.0 + }, + "unique_chunks_per_bot": { + "walk": 694.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 10, + "behaviour": "walk", + "start_seconds": 11.02, + "mspt": { + "average": 11.51, + "p50": 10.6, + "p95": 22.13, + "p99": 36.92 + }, + "mspt_worst_p99": 71.9, + "tps": 20.0, + "rss_mb": { + "mean": 1440.2, + "max": 1832.4 + }, + "cpu_percent": 124.19, + "chunks_per_second": 94.04, + "unique_per_second": 2.38, + "forgotten_per_second": 93.95, + "waited_ticks_per_second": 0.0, + "chunks_total": 37269, + "chunks_per_bot": { + "walk": 3726.9 + }, + "unique_chunks_per_bot": { + "walk": 688.9 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "walk", + "start_seconds": 10.91, + "mspt": { + "average": 20.74, + "p50": 19.27, + "p95": 40.42, + "p99": 57.12 + }, + "mspt_worst_p99": 102.0, + "tps": 20.0, + "rss_mb": { + "mean": 1485.4, + "max": 1871.9 + }, + "cpu_percent": 169.52, + "chunks_per_second": 235.81, + "unique_per_second": 5.68, + "forgotten_per_second": 235.83, + "waited_ticks_per_second": 0.0, + "chunks_total": 94456, + "chunks_per_bot": { + "walk": 3778.24 + }, + "unique_chunks_per_bot": { + "walk": 692.96 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "walk", + "start_seconds": 11.12, + "mspt": { + "average": 31.41, + "p50": 25.39, + "p95": 78.97, + "p99": 110.73 + }, + "mspt_worst_p99": 192.9, + "tps": 20.0, + "rss_mb": { + "mean": 1558.34, + "max": 1915.3 + }, + "cpu_percent": 173.27, + "chunks_per_second": 450.85, + "unique_per_second": 10.3, + "forgotten_per_second": 451.86, + "waited_ticks_per_second": 0.0, + "chunks_total": 186885, + "chunks_per_bot": { + "walk": 3737.7 + }, + "unique_chunks_per_bot": { + "walk": 688.9 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 100, + "behaviour": "walk", + "start_seconds": 18.16, + "mspt": { + "average": 114.55, + "p50": 101.37, + "p95": 256.19, + "p99": 338.64 + }, + "mspt_worst_p99": 442.6, + "tps": 8.73, + "rss_mb": { + "mean": 1357.65, + "max": 1358.4 + }, + "cpu_percent": 181.21, + "chunks_per_second": 484.16, + "unique_per_second": 30.76, + "forgotten_per_second": 478.77, + "waited_ticks_per_second": 0.0, + "chunks_total": 212947, + "chunks_per_bot": { + "walk": 2129.47 + }, + "unique_chunks_per_bot": { + "walk": 583.02 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 100, + "bots_failed": [], + "tick_queries": 57 + }, + { + "server": "horizon", + "bots": 1, + "behaviour": "explore", + "start_seconds": 17.45, + "mspt": { + "average": 10.62, + "p50": 9.2, + "p95": 24.61, + "p99": 38.18 + }, + "mspt_worst_p99": 81.1, + "tps": 20.0, + "rss_mb": { + "mean": 1372.73, + "max": 1568.7 + }, + "cpu_percent": 322.38, + "chunks_per_second": 39.21, + "unique_per_second": 34.6, + "forgotten_per_second": 38.55, + "waited_ticks_per_second": 0.51, + "chunks_total": 12549, + "chunks_per_bot": { + "explore": 12549.0 + }, + "unique_chunks_per_bot": { + "explore": 11168.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 10, + "behaviour": "explore", + "start_seconds": 11.32, + "mspt": { + "average": 30.38, + "p50": 27.84, + "p95": 52.31, + "p99": 77.89 + }, + "mspt_worst_p99": 158.3, + "tps": 20.0, + "rss_mb": { + "mean": 2231.56, + "max": 4022.6 + }, + "cpu_percent": 344.99, + "chunks_per_second": 47.34, + "unique_per_second": 46.73, + "forgotten_per_second": 44.57, + "waited_ticks_per_second": 112.27, + "chunks_total": 19870, + "chunks_per_bot": { + "explore": 1987.0 + }, + "unique_chunks_per_bot": { + "explore": 1966.5 + }, + "resent": 0, + "pulled_back": 16130, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "explore", + "start_seconds": 11.42, + "mspt": { + "average": 50.67, + "p50": 47.61, + "p95": 89.3, + "p99": 119.15 + }, + "mspt_worst_p99": 222.3, + "tps": 19.73, + "rss_mb": { + "mean": 2358.4, + "max": 4273.7 + }, + "cpu_percent": 286.32, + "chunks_per_second": 50.39, + "unique_per_second": 49.67, + "forgotten_per_second": 47.45, + "waited_ticks_per_second": 311.85, + "chunks_total": 28954, + "chunks_per_bot": { + "explore": 1158.16 + }, + "unique_chunks_per_bot": { + "explore": 1147.56 + }, + "resent": 0, + "pulled_back": 36319, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "explore", + "start_seconds": 20.06, + "mspt": { + "average": 50.69, + "p50": 47.48, + "p95": 102.67, + "p99": 133.27 + }, + "mspt_worst_p99": 274.4, + "tps": 19.73, + "rss_mb": { + "mean": 1764.01, + "max": 2542.0 + }, + "cpu_percent": 212.83, + "chunks_per_second": 97.33, + "unique_per_second": 96.47, + "forgotten_per_second": 76.74, + "waited_ticks_per_second": 750.49, + "chunks_total": 38538, + "chunks_per_bot": { + "explore": 770.76 + }, + "unique_chunks_per_bot": { + "explore": 763.84 + }, + "resent": 0, + "pulled_back": 39880, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 100, + "behaviour": "explore", + "start_seconds": 11.82, + "mspt": { + "average": 92.93, + "p50": 87.84, + "p95": 139.6, + "p99": 178.38 + }, + "mspt_worst_p99": 272.9, + "tps": 10.76, + "rss_mb": { + "mean": 1736.38, + "max": 1855.7 + }, + "cpu_percent": 321.53, + "chunks_per_second": 113.89, + "unique_per_second": 113.56, + "forgotten_per_second": 101.68, + "waited_ticks_per_second": 1179.4, + "chunks_total": 84617, + "chunks_per_bot": { + "explore": 846.17 + }, + "unique_chunks_per_bot": { + "explore": 845.01 + }, + "resent": 0, + "pulled_back": 128104, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 100, + "bots_failed": [], + "tick_queries": 58 + } + ] +} diff --git a/benchmarks/server-equal-work-2026-10-17/run-1-before-chunk-cache.json b/benchmarks/server-equal-work-2026-10-17/run-1-before-chunk-cache.json new file mode 100644 index 000000000..d79b3f179 --- /dev/null +++ b/benchmarks/server-equal-work-2026-10-17/run-1-before-chunk-cache.json @@ -0,0 +1,693 @@ +{ + "kind": "horizon-server-benchmark", + "date": "2026-10-17T00:37:35+00:00", + "machine": { + "system": "Linux", + "machine": "x86_64", + "cpus": 4 + }, + "commit": "", + "java": [ + "openjdk 25.0.4.1 2026-08-18" + ], + "settings": { + "servers": "horizon,vanilla", + "bots": "1,10,25,50,100", + "behaviour": "walk,explore", + "join_every": 1.0, + "warmup": 60, + "seconds": 300, + "seed": 12345, + "heap": "4g", + "out": "/home/ludovic/scratch/sh2-bench.json", + "nice": 19, + "bots_via": "ssh -o BatchMode=yes macmini", + "bots_bin": "/tmp/sh2-horizon-bots", + "connect": "100.72.51.8", + "properties": { + "level-seed": "12345", + "view-distance": "10", + "simulation-distance": "10", + "online-mode": "false", + "white-list": "false", + "gamemode": "creative", + "force-gamemode": "true", + "spawn-protection": "0", + "max-players": "200", + "difficulty": "easy", + "sync-chunk-writes": "true" + } + }, + "runs": [ + { + "server": "horizon", + "bots": 1, + "behaviour": "walk", + "start_seconds": 11.12, + "mspt": { + "average": 7.63, + "p50": 5.51, + "p95": 17.46, + "p99": 36.79 + }, + "mspt_worst_p99": 79.6, + "tps": 20.0, + "rss_mb": { + "mean": 1391.94, + "max": 1700.7 + }, + "cpu_percent": 60.18, + "chunks_per_second": 9.15, + "unique_per_second": 0.29, + "forgotten_per_second": 9.18, + "waited_ticks_per_second": 0.0, + "chunks_total": 3682, + "chunks_per_bot": { + "walk": 3682.0 + }, + "unique_chunks_per_bot": { + "walk": 692.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 1, + "behaviour": "walk", + "start_seconds": 15.35, + "mspt": { + "average": 18.08, + "p50": 18.66, + "p95": 28.05, + "p99": 46.94 + }, + "mspt_worst_p99": 456.4, + "tps": 20.0, + "rss_mb": { + "mean": 3455.72, + "max": 3471.7 + }, + "cpu_percent": 65.67, + "chunks_per_second": 9.89, + "unique_per_second": 0.29, + "forgotten_per_second": 9.92, + "waited_ticks_per_second": 0.0, + "chunks_total": 3827, + "chunks_per_bot": { + "walk": 3827.0 + }, + "unique_chunks_per_bot": { + "walk": 699.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 10, + "behaviour": "walk", + "start_seconds": 11.22, + "mspt": { + "average": 27.53, + "p50": 24.93, + "p95": 53.93, + "p99": 77.03 + }, + "mspt_worst_p99": 111.4, + "tps": 20.0, + "rss_mb": { + "mean": 1433.85, + "max": 1831.9 + }, + "cpu_percent": 138.65, + "chunks_per_second": 95.98, + "unique_per_second": 2.38, + "forgotten_per_second": 95.78, + "waited_ticks_per_second": 0.0, + "chunks_total": 38041, + "chunks_per_bot": { + "walk": 3804.1 + }, + "unique_chunks_per_bot": { + "walk": 690.8 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "walk", + "start_seconds": 14.83, + "mspt": { + "average": 18.92, + "p50": 16.02, + "p95": 39.0, + "p99": 56.13 + }, + "mspt_worst_p99": 180.3, + "tps": 20.0, + "rss_mb": { + "mean": 3479.45, + "max": 3535.7 + }, + "cpu_percent": 99.39, + "chunks_per_second": 99.93, + "unique_per_second": 2.94, + "forgotten_per_second": 100.04, + "waited_ticks_per_second": 0.0, + "chunks_total": 38411, + "chunks_per_bot": { + "walk": 3841.1 + }, + "unique_chunks_per_bot": { + "walk": 697.1 + }, + "resent": 0, + "pulled_back": 7, + "moved_too_quickly": 7, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "walk", + "start_seconds": 11.52, + "mspt": { + "average": 40.69, + "p50": 37.28, + "p95": 81.16, + "p99": 108.42 + }, + "mspt_worst_p99": 149.9, + "tps": 20.0, + "rss_mb": { + "mean": 1444.12, + "max": 1882.6 + }, + "cpu_percent": 176.94, + "chunks_per_second": 244.11, + "unique_per_second": 6.51, + "forgotten_per_second": 243.96, + "waited_ticks_per_second": 0.0, + "chunks_total": 95219, + "chunks_per_bot": { + "walk": 3808.76 + }, + "unique_chunks_per_bot": { + "walk": 695.64 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "walk", + "start_seconds": 15.23, + "mspt": { + "average": 18.81, + "p50": 17.26, + "p95": 30.16, + "p99": 53.9 + }, + "mspt_worst_p99": 390.0, + "tps": 20.0, + "rss_mb": { + "mean": 3517.36, + "max": 3555.7 + }, + "cpu_percent": 148.52, + "chunks_per_second": 252.34, + "unique_per_second": 7.75, + "forgotten_per_second": 251.95, + "waited_ticks_per_second": 0.0, + "chunks_total": 96645, + "chunks_per_bot": { + "walk": 3865.8 + }, + "unique_chunks_per_bot": { + "walk": 698.4 + }, + "resent": 0, + "pulled_back": 11, + "moved_too_quickly": 11, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "walk", + "start_seconds": 12.72, + "mspt": { + "average": 281.42, + "p50": 278.28, + "p95": 343.64, + "p99": 384.23 + }, + "mspt_worst_p99": 475.1, + "tps": 3.55, + "rss_mb": { + "mean": 1334.32, + "max": 1335.4 + }, + "cpu_percent": 197.11, + "chunks_per_second": 391.5, + "unique_per_second": 21.24, + "forgotten_per_second": 388.33, + "waited_ticks_per_second": 0.0, + "chunks_total": 153136, + "chunks_per_bot": { + "walk": 3062.72 + }, + "unique_chunks_per_bot": { + "walk": 661.82 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 57 + }, + { + "server": "vanilla", + "bots": 50, + "behaviour": "walk", + "start_seconds": 15.74, + "mspt": { + "average": 33.21, + "p50": 29.9, + "p95": 53.37, + "p99": 109.14 + }, + "mspt_worst_p99": 833.3, + "tps": 20.0, + "rss_mb": { + "mean": 3568.07, + "max": 3594.9 + }, + "cpu_percent": 266.59, + "chunks_per_second": 496.3, + "unique_per_second": 18.64, + "forgotten_per_second": 495.77, + "waited_ticks_per_second": 0.0, + "chunks_total": 191965, + "chunks_per_bot": { + "walk": 3839.3 + }, + "unique_chunks_per_bot": { + "walk": 696.22 + }, + "resent": 0, + "pulled_back": 184, + "moved_too_quickly": 183, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 100, + "behaviour": "walk", + "start_seconds": 11.22, + "mspt": { + "average": 467.37, + "p50": 464.62, + "p95": 524.07, + "p99": 548.77 + }, + "mspt_worst_p99": 581.1, + "tps": 2.14, + "rss_mb": { + "mean": 1353.68, + "max": 1354.9 + }, + "cpu_percent": 191.96, + "chunks_per_second": 377.62, + "unique_per_second": 55.8, + "forgotten_per_second": 348.88, + "waited_ticks_per_second": 0.0, + "chunks_total": 165719, + "chunks_per_bot": { + "walk": 1657.19 + }, + "unique_chunks_per_bot": { + "walk": 549.8 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 100, + "bots_failed": [], + "tick_queries": 56 + }, + { + "server": "vanilla", + "bots": 100, + "behaviour": "walk", + "start_seconds": 14.84, + "error": "[Errno 32] Broken pipe" + }, + { + "server": "horizon", + "bots": 1, + "behaviour": "explore", + "start_seconds": 11.12, + "mspt": { + "average": 17.07, + "p50": 16.6, + "p95": 32.82, + "p99": 50.3 + }, + "mspt_worst_p99": 113.9, + "tps": 20.0, + "rss_mb": { + "mean": 1391.72, + "max": 1566.6 + }, + "cpu_percent": 333.8, + "chunks_per_second": 39.42, + "unique_per_second": 34.77, + "forgotten_per_second": 39.34, + "waited_ticks_per_second": 0.0, + "chunks_total": 13860, + "chunks_per_bot": { + "explore": 13860.0 + }, + "unique_chunks_per_bot": { + "explore": 12379.0 + }, + "resent": 0, + "pulled_back": 192, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 1, + "behaviour": "explore", + "start_seconds": 16.03, + "mspt": { + "average": 9.9, + "p50": 8.04, + "p95": 20.41, + "p99": 63.62 + }, + "mspt_worst_p99": 351.1, + "tps": 20.0, + "rss_mb": { + "mean": 3709.92, + "max": 3776.2 + }, + "cpu_percent": 246.64, + "chunks_per_second": 36.06, + "unique_per_second": 33.33, + "forgotten_per_second": 35.73, + "waited_ticks_per_second": 0.0, + "chunks_total": 12369, + "chunks_per_bot": { + "explore": 12369.0 + }, + "unique_chunks_per_bot": { + "explore": 11559.0 + }, + "resent": 0, + "pulled_back": 222, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "horizon", + "bots": 10, + "behaviour": "explore", + "start_seconds": 11.42, + "mspt": { + "average": 33.72, + "p50": 31.08, + "p95": 61.07, + "p99": 89.09 + }, + "mspt_worst_p99": 142.6, + "tps": 20.0, + "rss_mb": { + "mean": 2239.97, + "max": 4108.8 + }, + "cpu_percent": 350.83, + "chunks_per_second": 46.49, + "unique_per_second": 46.08, + "forgotten_per_second": 44.33, + "waited_ticks_per_second": 111.28, + "chunks_total": 19915, + "chunks_per_bot": { + "explore": 1991.5 + }, + "unique_chunks_per_bot": { + "explore": 1974.4 + }, + "resent": 0, + "pulled_back": 17516, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "explore", + "start_seconds": 15.15, + "mspt": { + "average": 16.25, + "p50": 13.67, + "p95": 28.92, + "p99": 115.9 + }, + "mspt_worst_p99": 1202.1, + "tps": 20.0, + "rss_mb": { + "mean": 4202.72, + "max": 4502.7 + }, + "cpu_percent": 247.47, + "chunks_per_second": 22.3, + "unique_per_second": 22.25, + "forgotten_per_second": 22.64, + "waited_ticks_per_second": 78.89, + "chunks_total": 10092, + "chunks_per_bot": { + "explore": 1009.2 + }, + "unique_chunks_per_bot": { + "explore": 1007.4 + }, + "resent": 0, + "pulled_back": 16286, + "moved_too_quickly": 12, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "explore", + "start_seconds": 11.82, + "mspt": { + "average": 48.03, + "p50": 44.99, + "p95": 83.06, + "p99": 109.63 + }, + "mspt_worst_p99": 191.8, + "tps": 20.0, + "rss_mb": { + "mean": 2214.16, + "max": 4598.6 + }, + "cpu_percent": 356.03, + "chunks_per_second": 64.2, + "unique_per_second": 63.5, + "forgotten_per_second": 60.9, + "waited_ticks_per_second": 301.65, + "chunks_total": 32247, + "chunks_per_bot": { + "explore": 1289.88 + }, + "unique_chunks_per_bot": { + "explore": 1279.4 + }, + "resent": 0, + "pulled_back": 42159, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "explore", + "start_seconds": 14.73, + "mspt": { + "average": 25.73, + "p50": 22.75, + "p95": 37.72, + "p99": 201.98 + }, + "mspt_worst_p99": 4051.5, + "tps": 20.0, + "rss_mb": { + "mean": 4355.9, + "max": 4983.2 + }, + "cpu_percent": 309.3, + "chunks_per_second": 33.33, + "unique_per_second": 33.21, + "forgotten_per_second": 34.22, + "waited_ticks_per_second": 258.68, + "chunks_total": 19426, + "chunks_per_bot": { + "explore": 777.04 + }, + "unique_chunks_per_bot": { + "explore": 772.92 + }, + "resent": 0, + "pulled_back": 53918, + "moved_too_quickly": 93, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "explore", + "start_seconds": 11.01, + "mspt": { + "average": 108.61, + "p50": 107.37, + "p95": 151.96, + "p99": 177.41 + }, + "mspt_worst_p99": 271.6, + "tps": 9.21, + "rss_mb": { + "mean": 1775.98, + "max": 1956.9 + }, + "cpu_percent": 359.7, + "chunks_per_second": 100.32, + "unique_per_second": 98.91, + "forgotten_per_second": 95.42, + "waited_ticks_per_second": 518.15, + "chunks_total": 55956, + "chunks_per_bot": { + "explore": 1119.12 + }, + "unique_chunks_per_bot": { + "explore": 1111.42 + }, + "resent": 0, + "pulled_back": 69967, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 50, + "behaviour": "explore", + "start_seconds": 14.74, + "error": "[Errno 32] Broken pipe" + }, + { + "server": "horizon", + "bots": 100, + "behaviour": "explore", + "start_seconds": 11.12, + "mspt": { + "average": 244.43, + "p50": 243.58, + "p95": 308.39, + "p99": 360.12 + }, + "mspt_worst_p99": 470.2, + "tps": 4.09, + "rss_mb": { + "mean": 1806.85, + "max": 2084.5 + }, + "cpu_percent": 310.46, + "chunks_per_second": 186.87, + "unique_per_second": 182.99, + "forgotten_per_second": 161.75, + "waited_ticks_per_second": 1131.65, + "chunks_total": 100389, + "chunks_per_bot": { + "explore": 1003.89 + }, + "unique_chunks_per_bot": { + "explore": 991.9 + }, + "resent": 0, + "pulled_back": 56791, + "moved_too_quickly": 344, + "moved_wrongly": 0, + "bots_joined": 100, + "bots_failed": [], + "tick_queries": 57 + }, + { + "server": "vanilla", + "bots": 100, + "behaviour": "explore", + "start_seconds": 14.83, + "error": "[Errno 32] Broken pipe" + } + ] +} diff --git a/benchmarks/server-equal-work-2026-10-17/run-2-chunk-cache.json b/benchmarks/server-equal-work-2026-10-17/run-2-chunk-cache.json new file mode 100644 index 000000000..4c182395c --- /dev/null +++ b/benchmarks/server-equal-work-2026-10-17/run-2-chunk-cache.json @@ -0,0 +1,413 @@ +{ + "kind": "horizon-server-benchmark", + "date": "2026-10-17T02:22:59+00:00", + "machine": { + "system": "Linux", + "machine": "x86_64", + "cpus": 4 + }, + "commit": "", + "java": [ + "openjdk 25.0.4.1 2026-08-18" + ], + "settings": { + "servers": "horizon", + "bots": "1,10,25,50,100", + "behaviour": "walk,explore", + "join_every": 1.0, + "warmup": 60, + "seconds": 300, + "seed": 12345, + "heap": "4g", + "out": "/home/ludovic/scratch/sh2-bench2.json", + "nice": 19, + "bots_via": "ssh -o BatchMode=yes macmini", + "bots_bin": "/tmp/sh2-horizon-bots", + "connect": "100.72.51.8", + "properties": { + "level-seed": "12345", + "view-distance": "10", + "simulation-distance": "10", + "online-mode": "false", + "white-list": "false", + "gamemode": "creative", + "force-gamemode": "true", + "spawn-protection": "0", + "max-players": "200", + "difficulty": "easy", + "sync-chunk-writes": "true" + } + }, + "runs": [ + { + "server": "horizon", + "bots": 1, + "behaviour": "walk", + "start_seconds": 11.12, + "mspt": { + "average": 5.41, + "p50": 4.63, + "p95": 11.61, + "p99": 30.5 + }, + "mspt_worst_p99": 64.6, + "tps": 20.0, + "rss_mb": { + "mean": 1398.84, + "max": 1708.5 + }, + "cpu_percent": 56.84, + "chunks_per_second": 9.19, + "unique_per_second": 0.27, + "forgotten_per_second": 9.22, + "waited_ticks_per_second": 0.0, + "chunks_total": 3713, + "chunks_per_bot": { + "walk": 3713.0 + }, + "unique_chunks_per_bot": { + "walk": 694.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 10, + "behaviour": "walk", + "start_seconds": 10.92, + "mspt": { + "average": 13.18, + "p50": 11.74, + "p95": 26.5, + "p99": 44.82 + }, + "mspt_worst_p99": 87.6, + "tps": 20.0, + "rss_mb": { + "mean": 1439.83, + "max": 1831.6 + }, + "cpu_percent": 132.94, + "chunks_per_second": 93.87, + "unique_per_second": 2.38, + "forgotten_per_second": 93.77, + "waited_ticks_per_second": 0.0, + "chunks_total": 37240, + "chunks_per_bot": { + "walk": 3724.0 + }, + "unique_chunks_per_bot": { + "walk": 688.7 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "walk", + "start_seconds": 10.92, + "mspt": { + "average": 23.52, + "p50": 21.92, + "p95": 44.7, + "p99": 63.12 + }, + "mspt_worst_p99": 119.1, + "tps": 20.0, + "rss_mb": { + "mean": 1485.82, + "max": 1872.0 + }, + "cpu_percent": 173.82, + "chunks_per_second": 235.63, + "unique_per_second": 5.64, + "forgotten_per_second": 235.72, + "waited_ticks_per_second": 0.0, + "chunks_total": 94549, + "chunks_per_bot": { + "walk": 3781.96 + }, + "unique_chunks_per_bot": { + "walk": 692.8 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "walk", + "start_seconds": 10.91, + "mspt": { + "average": 31.36, + "p50": 26.08, + "p95": 70.6, + "p99": 92.83 + }, + "mspt_worst_p99": 125.5, + "tps": 20.0, + "rss_mb": { + "mean": 1556.81, + "max": 1913.0 + }, + "cpu_percent": 192.16, + "chunks_per_second": 468.69, + "unique_per_second": 10.43, + "forgotten_per_second": 468.78, + "waited_ticks_per_second": 0.0, + "chunks_total": 193547, + "chunks_per_bot": { + "walk": 3870.94 + }, + "unique_chunks_per_bot": { + "walk": 692.84 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 100, + "behaviour": "walk", + "start_seconds": 11.02, + "mspt": { + "average": 84.38, + "p50": 81.5, + "p95": 132.92, + "p99": 168.38 + }, + "mspt_worst_p99": 245.5, + "tps": 11.85, + "rss_mb": { + "mean": 1395.64, + "max": 1941.3 + }, + "cpu_percent": 271.28, + "chunks_per_second": 850.05, + "unique_per_second": 21.79, + "forgotten_per_second": 851.1, + "waited_ticks_per_second": 0.0, + "chunks_total": 368464, + "chunks_per_bot": { + "walk": 3684.64 + }, + "unique_chunks_per_bot": { + "walk": 680.58 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 100, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 1, + "behaviour": "explore", + "start_seconds": 11.02, + "mspt": { + "average": 11.63, + "p50": 10.27, + "p95": 24.52, + "p99": 40.53 + }, + "mspt_worst_p99": 99.9, + "tps": 20.0, + "rss_mb": { + "mean": 1413.81, + "max": 1596.1 + }, + "cpu_percent": 329.84, + "chunks_per_second": 39.47, + "unique_per_second": 34.81, + "forgotten_per_second": 39.42, + "waited_ticks_per_second": 0.0, + "chunks_total": 13798, + "chunks_per_bot": { + "explore": 13798.0 + }, + "unique_chunks_per_bot": { + "explore": 12330.0 + }, + "resent": 0, + "pulled_back": 185, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 10, + "behaviour": "explore", + "start_seconds": 11.02, + "mspt": { + "average": 28.49, + "p50": 26.2, + "p95": 48.58, + "p99": 72.04 + }, + "mspt_worst_p99": 125.7, + "tps": 20.0, + "rss_mb": { + "mean": 2231.06, + "max": 4077.8 + }, + "cpu_percent": 344.95, + "chunks_per_second": 46.31, + "unique_per_second": 45.88, + "forgotten_per_second": 44.14, + "waited_ticks_per_second": 111.95, + "chunks_total": 19722, + "chunks_per_bot": { + "explore": 1972.2 + }, + "unique_chunks_per_bot": { + "explore": 1957.0 + }, + "resent": 0, + "pulled_back": 18533, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "explore", + "start_seconds": 11.12, + "mspt": { + "average": 40.04, + "p50": 37.61, + "p95": 62.39, + "p99": 83.64 + }, + "mspt_worst_p99": 147.0, + "tps": 20.0, + "rss_mb": { + "mean": 2466.85, + "max": 4504.5 + }, + "cpu_percent": 340.13, + "chunks_per_second": 60.61, + "unique_per_second": 59.62, + "forgotten_per_second": 56.28, + "waited_ticks_per_second": 299.57, + "chunks_total": 32877, + "chunks_per_bot": { + "explore": 1315.08 + }, + "unique_chunks_per_bot": { + "explore": 1300.72 + }, + "resent": 0, + "pulled_back": 46272, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "explore", + "start_seconds": 11.32, + "mspt": { + "average": 61.82, + "p50": 59.21, + "p95": 90.46, + "p99": 115.58 + }, + "mspt_worst_p99": 320.9, + "tps": 16.18, + "rss_mb": { + "mean": 2634.54, + "max": 4883.1 + }, + "cpu_percent": 356.96, + "chunks_per_second": 95.26, + "unique_per_second": 94.31, + "forgotten_per_second": 85.86, + "waited_ticks_per_second": 528.08, + "chunks_total": 59251, + "chunks_per_bot": { + "explore": 1185.02 + }, + "unique_chunks_per_bot": { + "explore": 1173.96 + }, + "resent": 0, + "pulled_back": 99493, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 100, + "behaviour": "explore", + "start_seconds": 10.81, + "mspt": { + "average": 103.24, + "p50": 100.03, + "p95": 144.77, + "p99": 178.8 + }, + "mspt_worst_p99": 258.6, + "tps": 9.69, + "rss_mb": { + "mean": 1784.48, + "max": 1922.0 + }, + "cpu_percent": 336.94, + "chunks_per_second": 115.06, + "unique_per_second": 114.86, + "forgotten_per_second": 109.43, + "waited_ticks_per_second": 1127.57, + "chunks_total": 93839, + "chunks_per_bot": { + "explore": 938.39 + }, + "unique_chunks_per_bot": { + "explore": 935.28 + }, + "resent": 0, + "pulled_back": 133404, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 100, + "bots_failed": [], + "tick_queries": 58 + } + ] +} diff --git a/benchmarks/server-equal-work-2026-10-17/vanilla.json b/benchmarks/server-equal-work-2026-10-17/vanilla.json new file mode 100644 index 000000000..014554cee --- /dev/null +++ b/benchmarks/server-equal-work-2026-10-17/vanilla.json @@ -0,0 +1,324 @@ +{ + "kind": "horizon-server-benchmark", + "date": "2026-10-17T03:29:30+00:00", + "machine": { + "system": "Linux", + "machine": "x86_64", + "cpus": 4 + }, + "commit": "", + "java": [ + "openjdk 25.0.4.1 2026-08-18" + ], + "settings": { + "servers": "vanilla", + "bots": "1,10,25,50,100", + "behaviour": "walk,explore", + "join_every": 1.0, + "warmup": 60, + "seconds": 300, + "seed": 12345, + "gamerule": "locator_bar=false", + "heap": "4g", + "out": "/home/ludovic/scratch/sh2-bench3.json", + "nice": 19, + "bots_via": "ssh -o BatchMode=yes macmini", + "bots_bin": "/tmp/sh2-horizon-bots", + "connect": "100.72.51.8", + "properties": { + "level-seed": "12345", + "view-distance": "10", + "simulation-distance": "10", + "online-mode": "false", + "white-list": "false", + "gamemode": "creative", + "force-gamemode": "true", + "spawn-protection": "0", + "max-players": "200", + "difficulty": "easy", + "sync-chunk-writes": "true" + } + }, + "runs": [ + { + "server": "vanilla", + "bots": 1, + "behaviour": "walk", + "start_seconds": 15.43, + "mspt": { + "average": 18.32, + "p50": 19.26, + "p95": 27.34, + "p99": 45.48 + }, + "mspt_worst_p99": 393.3, + "tps": 20.0, + "rss_mb": { + "mean": 3428.43, + "max": 3437.7 + }, + "cpu_percent": 66.47, + "chunks_per_second": 9.92, + "unique_per_second": 0.29, + "forgotten_per_second": 9.95, + "waited_ticks_per_second": 0.0, + "chunks_total": 3850, + "chunks_per_bot": { + "walk": 3850.0 + }, + "unique_chunks_per_bot": { + "walk": 699.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "walk", + "start_seconds": 14.76, + "mspt": { + "average": 16.8, + "p50": 14.55, + "p95": 30.52, + "p99": 50.01 + }, + "mspt_worst_p99": 205.2, + "tps": 20.0, + "rss_mb": { + "mean": 3490.15, + "max": 3580.7 + }, + "cpu_percent": 83.7, + "chunks_per_second": 100.01, + "unique_per_second": 2.83, + "forgotten_per_second": 100.04, + "waited_ticks_per_second": 0.0, + "chunks_total": 38623, + "chunks_per_bot": { + "walk": 3862.3 + }, + "unique_chunks_per_bot": { + "walk": 697.2 + }, + "resent": 0, + "pulled_back": 5, + "moved_too_quickly": 5, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "walk", + "start_seconds": 14.84, + "mspt": { + "average": 16.01, + "p50": 14.55, + "p95": 26.39, + "p99": 51.26 + }, + "mspt_worst_p99": 397.8, + "tps": 20.0, + "rss_mb": { + "mean": 3550.02, + "max": 3593.5 + }, + "cpu_percent": 114.96, + "chunks_per_second": 251.62, + "unique_per_second": 6.72, + "forgotten_per_second": 251.16, + "waited_ticks_per_second": 0.0, + "chunks_total": 98701, + "chunks_per_bot": { + "walk": 3948.04 + }, + "unique_chunks_per_bot": { + "walk": 698.84 + }, + "resent": 0, + "pulled_back": 10, + "moved_too_quickly": 10, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "vanilla", + "bots": 50, + "behaviour": "walk", + "start_seconds": 14.43, + "mspt": { + "average": 20.52, + "p50": 19.55, + "p95": 29.72, + "p99": 49.04 + }, + "mspt_worst_p99": 600.4, + "tps": 20.0, + "rss_mb": { + "mean": 3633.12, + "max": 3658.7 + }, + "cpu_percent": 193.84, + "chunks_per_second": 501.86, + "unique_per_second": 12.15, + "forgotten_per_second": 501.8, + "waited_ticks_per_second": 0.0, + "chunks_total": 203429, + "chunks_per_bot": { + "walk": 4068.58 + }, + "unique_chunks_per_bot": { + "walk": 698.66 + }, + "resent": 0, + "pulled_back": 33, + "moved_too_quickly": 32, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "vanilla", + "bots": 100, + "behaviour": "walk", + "start_seconds": 15.34, + "error": "[Errno 32] Broken pipe" + }, + { + "server": "vanilla", + "bots": 1, + "behaviour": "explore", + "start_seconds": 15.55, + "mspt": { + "average": 9.93, + "p50": 8.12, + "p95": 21.33, + "p99": 60.19 + }, + "mspt_worst_p99": 373.0, + "tps": 20.0, + "rss_mb": { + "mean": 3564.11, + "max": 3655.0 + }, + "cpu_percent": 245.18, + "chunks_per_second": 35.93, + "unique_per_second": 33.35, + "forgotten_per_second": 35.64, + "waited_ticks_per_second": 0.0, + "chunks_total": 12340, + "chunks_per_bot": { + "explore": 12340.0 + }, + "unique_chunks_per_bot": { + "explore": 11569.0 + }, + "resent": 0, + "pulled_back": 261, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "explore", + "start_seconds": 15.03, + "mspt": { + "average": 16.99, + "p50": 14.46, + "p95": 29.61, + "p99": 132.18 + }, + "mspt_worst_p99": 1244.0, + "tps": 20.0, + "rss_mb": { + "mean": 4135.2, + "max": 4453.1 + }, + "cpu_percent": 242.32, + "chunks_per_second": 22.39, + "unique_per_second": 22.41, + "forgotten_per_second": 22.78, + "waited_ticks_per_second": 78.78, + "chunks_total": 10177, + "chunks_per_bot": { + "explore": 1017.7 + }, + "unique_chunks_per_bot": { + "explore": 1016.2 + }, + "resent": 0, + "pulled_back": 17314, + "moved_too_quickly": 19, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "explore", + "start_seconds": 14.84, + "mspt": { + "average": 24.19, + "p50": 21.22, + "p95": 36.51, + "p99": 174.82 + }, + "mspt_worst_p99": 1532.8, + "tps": 20.0, + "rss_mb": { + "mean": 4504.55, + "max": 4988.9 + }, + "cpu_percent": 319.55, + "chunks_per_second": 33.78, + "unique_per_second": 33.66, + "forgotten_per_second": 34.64, + "waited_ticks_per_second": 269.22, + "chunks_total": 20522, + "chunks_per_bot": { + "explore": 820.88 + }, + "unique_chunks_per_bot": { + "explore": 815.76 + }, + "resent": 0, + "pulled_back": 46246, + "moved_too_quickly": 28, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 50, + "behaviour": "explore", + "start_seconds": 14.73, + "error": "[Errno 32] Broken pipe" + }, + { + "server": "vanilla", + "bots": 100, + "behaviour": "explore", + "start_seconds": 14.54, + "error": "[Errno 32] Broken pipe" + } + ] +} diff --git a/crates/horizon-bots/src/main.rs b/crates/horizon-bots/src/main.rs index d76da5e84..090daeb4e 100644 --- a/crates/horizon-bots/src/main.rs +++ b/crates/horizon-bots/src/main.rs @@ -9,6 +9,11 @@ //! horizon-bots --server 127.0.0.1:25565 --bots 25 --behaviour mixed --seconds 360 --report bots.json //! //! It prints one JSON line a second (bots online, chunks received) and writes a summary with `--report`. +//! A bot moves only while it holds the chunk it stands in: vanilla's client stops ticking its player in a chunk +//! it hasn't received, so a player can't outrun the server's chunk sending, on any server. +//! Each bot also keeps the chunks it holds, as a client's chunk cache does, so the report tells chunks that +//! entered the view apart from chunks sent again while the bot still held them (`resent`), and counts the +//! times the server pulled a bot back (`pulled_back`: a teleport after the first, as for "moved too quickly"). use horizon_protocol::{ PROTOCOL_VERSION, @@ -25,6 +30,7 @@ use horizon_protocol::{ }; use serde_json::json; use std::{ + collections::HashSet, sync::{ Arc, Mutex, atomic::{AtomicBool, AtomicU64, Ordering}, @@ -43,8 +49,9 @@ horizon-bots: load-test bots for a Minecraft Java Edition 26.3 server (offline m --seconds N how long to run after the first bot joins; default 60 --view-distance N what the bots ask for; default 10 --seed N default 12345 + --explore-speed B blocks a second an explore bot flies; default 22 (as fast as chunks come) --name PREFIX bot names: PREFIX0, PREFIX1...; default Bot - --report FILE a JSON summary at the end + --report FILE a JSON summary at the end (- prints it after the per-second lines) "; #[derive(Clone, Copy, Debug, PartialEq)] @@ -73,6 +80,7 @@ struct Options { seconds: u64, view_distance: i8, seed: u64, + explore_speed: f64, name: String, report: Option, } @@ -87,6 +95,7 @@ fn options() -> Result { seconds: 60, view_distance: 10, seed: 12345, + explore_speed: 22.0, name: "Bot".into(), report: None, }; @@ -110,6 +119,9 @@ fn options() -> Result { "--seconds" => o.seconds = value.parse().map_err(|_| number("seconds"))?, "--view-distance" => o.view_distance = value.parse().map_err(|_| number("2 to 32"))?, "--seed" => o.seed = value.parse().map_err(|_| number("a number"))?, + "--explore-speed" => { + o.explore_speed = value.parse().map_err(|_| number("blocks a second"))?; + } "--name" => o.name = value, "--report" => o.report = Some(value), _ => return Err(format!("unknown option {arg}; --help lists them")), @@ -129,6 +141,17 @@ fn options() -> Result { struct Bot { online: AtomicBool, chunks: AtomicU64, + /// Chunks received while the bot already held them (no ForgetLevelChunk in between). + resent: AtomicU64, + /// Different chunks received over the run. + unique: AtomicU64, + forgotten: AtomicU64, + /// Teleports after the spawn one: the server refused a move. + pulled_back: AtomicU64, + /// Ticks the bot couldn't move because it didn't hold its chunk yet. + waited: AtomicU64, + /// The chunks the bot holds (ClientChunkCache). + held: Mutex>, /// Where the server last put the bot (a teleport), to start from. teleport: Mutex>, error: Mutex>, @@ -268,6 +291,36 @@ fn join(o: &Options, name: &str) -> Result { } } +/// ChunkBatchSizeCalculator: the chunks per tick a vanilla client asks for, from how long its batches take +/// to arrive. +struct BatchSize { + nanos_per_chunk: f64, + old_samples_weight: u32, + started: Instant, +} + +impl BatchSize { + fn new() -> Self { + Self { + nanos_per_chunk: 2_000_000.0, + old_samples_weight: 1, + started: Instant::now(), + } + } + fn finished(&mut self, size: i32) { + if size > 0 { + let per_chunk = self.started.elapsed().as_nanos() as f64 / f64::from(size); + let clamped = per_chunk.clamp(self.nanos_per_chunk / 3.0, self.nanos_per_chunk * 3.0); + let weight = f64::from(self.old_samples_weight); + self.nanos_per_chunk = (self.nanos_per_chunk * weight + clamped) / (weight + 1.0); + self.old_samples_weight = (self.old_samples_weight + 1).min(49); + } + } + fn desired_chunks_per_tick(&self) -> f32 { + (7_000_000.0 / self.nanos_per_chunk) as f32 + } +} + /// Reads the play state: answers what a client must answer, counts chunks, records teleports. fn read( mut reader: horizon_protocol::connection::ConnReader, @@ -277,6 +330,9 @@ fn read( use ids::play::clientbound as cb; use ids::play::serverbound as sb; let send = |body: Vec| writer.lock().unwrap().send(&body); + let mut seen = HashSet::new(); + let mut batch = BatchSize::new(); + let mut teleports = 0u64; loop { let (id, body) = match reader.recv() { Ok(packet) => packet, @@ -299,14 +355,38 @@ fn read( }, cb::LEVEL_CHUNK_WITH_LIGHT => { bot.chunks.fetch_add(1, Ordering::Relaxed); + if let (Ok(x), Ok(z)) = (r.i32(), r.i32()) { + if !bot.held.lock().unwrap().insert((x, z)) { + bot.resent.fetch_add(1, Ordering::Relaxed); + } + if seen.insert((x, z)) { + bot.unique.fetch_add(1, Ordering::Relaxed); + } + } Ok(()) } - cb::CHUNK_BATCH_FINISHED => send( - play::Serverbound::ChunkBatchReceived(play::ChunkBatchReceived { - chunks_per_tick: 25.0, - }) - .encode(), - ), + cb::FORGET_LEVEL_CHUNK => { + if let Ok(play::Clientbound::ForgetLevelChunk(p)) = + play::Clientbound::decode(id, &body) + && bot.held.lock().unwrap().remove(&(p.pos[0], p.pos[1])) + { + bot.forgotten.fetch_add(1, Ordering::Relaxed); + } + Ok(()) + } + cb::CHUNK_BATCH_START => { + batch.started = Instant::now(); + Ok(()) + } + cb::CHUNK_BATCH_FINISHED => { + batch.finished(r.varint().unwrap_or(0)); + send( + play::Serverbound::ChunkBatchReceived(play::ChunkBatchReceived { + chunks_per_tick: batch.desired_chunks_per_tick(), + }) + .encode(), + ) + } cb::PLAYER_POSITION => match play::Clientbound::decode(id, &body) { Ok(play::Clientbound::PlayerPosition(p)) => { let mut at = bot.teleport.lock().unwrap(); @@ -320,6 +400,10 @@ fn read( } }); *at = Some(pos); + teleports += 1; + if teleports > 1 { + bot.pulled_back.fetch_add(1, Ordering::Relaxed); + } send( play::Serverbound::AcceptTeleportation(play::AcceptTeleportation { id: p.teleport, @@ -416,7 +500,10 @@ fn run_bot( if let Some(at) = bot.teleport.lock().unwrap().take() { pos = at; } - if pos[1] < cruise { + let here = ((pos[0].floor() as i32) >> 4, (pos[2].floor() as i32) >> 4); + if !bot.held.lock().unwrap().contains(&here) { + bot.waited.fetch_add(1, Ordering::Relaxed); + } else if pos[1] < cruise { pos[1] = (pos[1] + 0.5).min(cruise); } else { match behaviour { @@ -440,9 +527,11 @@ fn run_bot( } } Behaviour::Explore => { - // Flying fast in creative: about 22 blocks a second, which keeps new chunks coming. - pos[0] += heading.cos() * 1.1; - pos[2] += heading.sin() * 1.1; + // Flying in creative, by default about 22 blocks a second, which keeps new + // chunks coming as fast as the server sends them. + let step = o.explore_speed / 20.0; + pos[0] += heading.cos() * step; + pos[2] += heading.sin() * step; } } } @@ -520,6 +609,9 @@ fn main() { .filter(|b| b.online.load(Ordering::SeqCst)) .count(); let chunks: u64 = bots.iter().map(|b| b.chunks.load(Ordering::Relaxed)).sum(); + let total = |f: fn(&Bot) -> &AtomicU64| -> u64 { + bots.iter().map(|b| f(b).load(Ordering::Relaxed)).sum() + }; let failed = bots .iter() .filter(|b| b.error.lock().unwrap().is_some()) @@ -536,6 +628,11 @@ fn main() { "failed": failed, "chunks": chunks, "chunks_per_second": chunks - previous_chunks, + "unique": total(|b| &b.unique), + "forgotten": total(|b| &b.forgotten), + "resent": total(|b| &b.resent), + "pulled_back": total(|b| &b.pulled_back), + "waited_ticks": total(|b| &b.waited), }) ); previous_chunks = chunks; @@ -554,19 +651,30 @@ fn main() { "bots": o.bots, "behaviour": o.behaviour, "seed": o.seed, + "explore_speed": o.explore_speed, "view_distance": o.view_distance, "seconds": o.seconds, "joined": bots.iter().filter(|b| b.joined_after_ms.load(Ordering::SeqCst) > 0).count(), "chunks": bots.iter().map(|b| b.chunks.load(Ordering::Relaxed)).sum::(), + "resent": bots.iter().map(|b| b.resent.load(Ordering::Relaxed)).sum::(), + "pulled_back": bots.iter().map(|b| b.pulled_back.load(Ordering::Relaxed)).sum::(), + "waited_ticks": bots.iter().map(|b| b.waited.load(Ordering::Relaxed)).sum::(), "per_bot": bots.iter().enumerate().map(|(i, b)| json!({ "name": format!("{}{i}", o.name), "behaviour": behaviour_of(&o.behaviour, i).name(), "joined_after_ms": b.joined_after_ms.load(Ordering::SeqCst), "chunks": b.chunks.load(Ordering::Relaxed), + "unique_chunks": b.unique.load(Ordering::Relaxed), + "resent": b.resent.load(Ordering::Relaxed), + "forgotten": b.forgotten.load(Ordering::Relaxed), + "pulled_back": b.pulled_back.load(Ordering::Relaxed), + "waited_ticks": b.waited.load(Ordering::Relaxed), "error": *b.error.lock().unwrap(), })).collect::>(), }); - if let Some(path) = &o.report { + if o.report.as_deref() == Some("-") { + println!("{summary:#}"); + } else if let Some(path) = &o.report { if let Err(e) = std::fs::write(path, format!("{summary:#}\n")) { eprintln!("horizon-bots: {path}: {e}"); } diff --git a/crates/horizon-client/examples/lan_flat.rs b/crates/horizon-client/examples/lan_flat.rs index 16fc36b95..4800f05fa 100644 --- a/crates/horizon-client/examples/lan_flat.rs +++ b/crates/horizon-client/examples/lan_flat.rs @@ -22,12 +22,7 @@ fn main() -> Result<(), Box> { let port: u16 = args.next().ok_or("port")?.parse()?; let assets = Assets::load(&root)?; let facts = BlockFacts::load(&root, assets.registry)?; - let state = |name: &str| { - assets - .registry - .default_state(name) - .map_or(0, |s| s.0 as u16) - }; + let state = |name: &str| assets.registry.default_state(name).map_or(0, |s| s.0); let sync = Arc::new(SyncData::load(&root)?); let biomes: Vec = sync .dynamic @@ -121,6 +116,7 @@ fn main() -> Result<(), Box> { clock_rate: 1.0, tick_rate: 20.0, tick_frozen: false, + collision: None, }; for event in hub.tick(&mut ctx) { match event { @@ -132,7 +128,7 @@ fn main() -> Result<(), Box> { eprintln!("[lan] {packet:?}"); } } - HostEvent::Chat(line) => println!("{line}"), + HostEvent::Chat(line) | HostEvent::Warn(line) => println!("{line}"), HostEvent::Left(_) => {} } } diff --git a/crates/horizon-client/examples/light_parity.rs b/crates/horizon-client/examples/light_parity.rs index afc3e3156..29488130e 100644 --- a/crates/horizon-client/examples/light_parity.rs +++ b/crates/horizon-client/examples/light_parity.rs @@ -232,7 +232,7 @@ fn incremental( c.lit = true; world.insert((x, z), Arc::new(c)); } - let palette: Vec = [ + let palette: Vec = [ "minecraft:air", "minecraft:stone", "minecraft:torch", @@ -242,7 +242,7 @@ fn incremental( "minecraft:glass", ] .iter() - .filter_map(|n| registry.default_state(n).map(|s| s.0 as u16)) + .filter_map(|n| registry.default_state(n).map(|s| s.0)) .chain( facts .states @@ -257,7 +257,7 @@ fn incremental( ] .contains(&&*f.name) }) - .map(|(i, _)| i as u16) + .map(|(i, _)| i as u32) .step_by(3), ) .collect(); diff --git a/crates/horizon-client/examples/options_probe.rs b/crates/horizon-client/examples/options_probe.rs index 1cdf8c8fb..f186af994 100644 --- a/crates/horizon-client/examples/options_probe.rs +++ b/crates/horizon-client/examples/options_probe.rs @@ -1,7 +1,10 @@ #![forbid(unsafe_code)] //! Loads and saves option files like `Options.load`/`save`, for the //! differential harness (tools/test_options_parity.py). -use horizon_client::{Result, keybinds, options_file::OptionsFile}; +use horizon_client::{ + Result, keybinds, + options_file::{OptionsFile, V}, +}; use std::{fs, io::Write}; fn main() -> Result<()> { @@ -14,11 +17,16 @@ fn main() -> Result<()> { let mut first = true; for path in names { let name = path.file_name().unwrap().to_string_lossy().into_owned(); - let file = if name == "absent.txt" { + let mut file = if name == "absent.txt" { OptionsFile::default() } else { OptionsFile::parse(&String::from_utf8_lossy(&fs::read(&path)?)) }; + // Horizon extends the slider to 64; vanilla rejects 33..64 to its default. + // Compare that deliberate extension as vanilla would serialize it. + if file.int("renderDistance") > 32 { + file.set("renderDistance", V::Int(12)); + } write!(out, "== {name}\n{}", file.serialize())?; if first { first = false; diff --git a/crates/horizon-client/examples/random_tick_eligibility_bench.rs b/crates/horizon-client/examples/random_tick_eligibility_bench.rs index 856d7d520..49c32a8f7 100644 --- a/crates/horizon-client/examples/random_tick_eligibility_bench.rs +++ b/crates/horizon-client/examples/random_tick_eligibility_bench.rs @@ -56,16 +56,16 @@ fn main() -> horizon_client::Result<()> { "resource_load_ms={:.3}", start.elapsed().as_secs_f64() * 1000.0 ); - let inactive: Vec = facts + let inactive: Vec = facts .states .iter() .enumerate() .filter(|(_, f)| !f.random_ticks) .take(128) - .map(|(i, _)| i as u16) + .map(|(i, _)| i as u32) .collect(); - let active = registry.default_state("minecraft:grass_block").unwrap().0 as u16; - assert!(facts.get(BlockStateId(u32::from(active))).random_ticks); + let active = registry.default_state("minecraft:grass_block").unwrap().0; + assert!(facts.get(BlockStateId(active)).random_ticks); for (name, palette) in [("sparse", 1), ("dense", 128)] { let start = Instant::now(); let mut chunks = FxHashMap::default(); diff --git a/crates/horizon-client/examples/save_batch_bench.rs b/crates/horizon-client/examples/save_batch_bench.rs index 11c4f64ed..4ee5103de 100644 --- a/crates/horizon-client/examples/save_batch_bench.rs +++ b/crates/horizon-client/examples/save_batch_bench.rs @@ -4,7 +4,7 @@ //! Exact NBT equality is always validated. No GPU, compression or disk I/O timed. use horizon_client::{ biome::Biomes, - chunk::{BLOCK_STRATEGY, Chunk}, + chunk::{Chunk, block_strategy}, containers::BlockEntity, items::ItemStack, save::{self, ChunkExtras}, @@ -64,7 +64,7 @@ fn fixture(per_chunk: usize) -> (Vec>, Entities, Names) { }; let mut template = Chunk::empty(0, 0); for s in &mut template.sections[..8] { - Arc::make_mut(s).blocks = PalettedContainer::new(BLOCK_STRATEGY, 1); + Arc::make_mut(s).blocks = PalettedContainer::new(block_strategy(), 1); } template.heights = Arc::new([64; 256]); for i in 0..256 { diff --git a/crates/horizon-client/examples/save_parity.rs b/crates/horizon-client/examples/save_parity.rs index ba2c4b0fa..3954e3bb3 100644 --- a/crates/horizon-client/examples/save_parity.rs +++ b/crates/horizon-client/examples/save_parity.rs @@ -257,9 +257,9 @@ fn main() -> horizon_client::Result<()> { ); Ok(()) } -fn tables_air(assets: &Assets) -> u16 { +fn tables_air(assets: &Assets) -> u32 { assets .registry .default_state("minecraft:air") - .map_or(0, |s| s.0 as u16) + .map_or(0, |s| s.0) } diff --git a/crates/horizon-client/examples/section_mesh_fixture.rs b/crates/horizon-client/examples/section_mesh_fixture.rs index a42448794..6359f32d0 100644 --- a/crates/horizon-client/examples/section_mesh_fixture.rs +++ b/crates/horizon-client/examples/section_mesh_fixture.rs @@ -83,7 +83,7 @@ fn scene( for block in row["blocks"].as_array().ok_or("missing blocks")? { let (x, y, z) = (int(&block[0])?, int(&block[1])?, int(&block[2])?); let i = chunk_index(x, z)?; - chunks[i].set(x & 15, y, z & 15, u16::try_from(int(&block[3])?)?); + chunks[i].set(x & 15, y, z & 15, u32::try_from(int(&block[3])?)?); } for cell in row["lights"].as_array().ok_or("missing lights")? { let (x, y, z) = (int(&cell[0])?, int(&cell[1])?, int(&cell[2])?); diff --git a/crates/horizon-client/src/assets.rs b/crates/horizon-client/src/assets.rs index b8551a059..f278740f5 100644 --- a/crates/horizon-client/src/assets.rs +++ b/crates/horizon-client/src/assets.rs @@ -189,6 +189,9 @@ impl Assets { .default_state(&format!("minecraft:{name}")) .ok_or_else(|| invalid("missing test-scene material"))?; } + // Chunks hold every state of this registry: content mods can take it + // past vanilla's 16-bit global palette. + crate::chunk::set_block_states(registry.len()); let registry: &'static BlockRegistry = Box::leak(Box::new(registry)); Self::with_models( root, diff --git a/crates/horizon-client/src/chunk.rs b/crates/horizon-client/src/chunk.rs index b0b2ef262..ce2c4ea71 100644 --- a/crates/horizon-client/src/chunk.rs +++ b/crates/horizon-client/src/chunk.rs @@ -9,11 +9,26 @@ pub const MIN_Y: i32 = -64; pub const HEIGHT: i32 = 384; pub const SECTIONS: usize = (HEIGHT / 16) as usize; -/// Block states are `PalettedContainer`s over the pinned registry (35,723 -/// states: a 16-bit global palette); biomes are 4x4x4 containers over the -/// biome table (67 entries: a 7-bit global palette). -pub const BLOCK_STRATEGY: Strategy = Strategy::blocks(16); +/// Biomes are 4x4x4 containers over the biome table (67 entries: a 7-bit +/// global palette). pub const BIOME_STRATEGY: Strategy = Strategy::biomes(7); + +/// Bits of the block states' global palette: 16 for vanilla's 35,723 states +/// (what vanilla servers and clients send), more when content mods add states +/// past 65,536 (docs/modpacks.md). Set once, before the first chunk. +static BLOCK_BITS: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(16); + +/// Block states are `PalettedContainer`s over the registry: vanilla's 16-bit +/// global palette, wider with enough content mods. +pub fn block_strategy() -> Strategy { + Strategy::blocks(BLOCK_BITS.load(std::sync::atomic::Ordering::Relaxed)) +} + +/// Sizes the block states' global palette for a registry of `states`. +pub fn set_block_states(states: usize) { + let bits = horizon_core::paletted::ceil_log2(states).max(16); + BLOCK_BITS.store(bits, std::sync::atomic::Ordering::Relaxed); +} /// Packed light per block: sky light in the high nibble, block light in the low one. #[derive(Clone)] pub enum Light { @@ -43,7 +58,7 @@ pub struct Section { impl Default for Section { fn default() -> Self { Self { - blocks: PalettedContainer::new(BLOCK_STRATEGY, 0), + blocks: PalettedContainer::new(block_strategy(), 0), biomes: PalettedContainer::new(BIOME_STRATEGY, 0), light: Light::Uniform(0xF0), } @@ -51,27 +66,26 @@ impl Default for Section { } impl Section { #[inline] - pub fn get(&self, i: usize) -> u16 { - self.blocks.get(i) as u16 + pub fn get(&self, i: usize) -> u32 { + self.blocks.get(i) } - pub fn set(&mut self, i: usize, v: u16) -> u16 { - self.blocks.set(i, u32::from(v)) as u16 + pub fn set(&mut self, i: usize, v: u32) -> u32 { + self.blocks.set(i, v) } /// All 4,096 block states in index order. - pub fn unpack(&self, out: &mut [u16; 4096]) { - self.blocks.unpack_u16(out); + pub fn unpack(&self, out: &mut [u32; 4096]) { + self.blocks.unpack(out); } /// The block states of a whole section at once. - pub fn from_blocks(values: &[u16; 4096]) -> PalettedContainer { - let wide: Vec = values.iter().map(|&v| u32::from(v)).collect(); - PalettedContainer::from_values(BLOCK_STRATEGY, &wide) + pub fn from_blocks(values: &[u32; 4096]) -> PalettedContainer { + PalettedContainer::from_values(block_strategy(), values) } #[inline] pub fn light(&self, i: usize) -> u8 { self.light.get(i) } - pub fn is_uniform(&self, v: u16) -> bool { - self.blocks.is_uniform(u32::from(v)) + pub fn is_uniform(&self, v: u32) -> bool { + self.blocks.is_uniform(v) } /// Heap bytes held by the block, biome and light storage (benchmark accounting). pub fn heap_bytes(&self) -> usize { @@ -147,7 +161,7 @@ impl Chunk { } } #[inline] - pub fn get(&self, x: i32, y: i32, z: i32) -> u16 { + pub fn get(&self, x: i32, y: i32, z: i32) -> u32 { let s = (y - MIN_Y) >> 4; if !(0..SECTIONS as i32).contains(&s) { return 0; @@ -156,11 +170,11 @@ impl Chunk { } #[inline] pub fn block(&self, x: i32, y: i32, z: i32) -> BlockStateId { - BlockStateId(u32::from(self.get(x, y, z))) + BlockStateId(self.get(x, y, z)) } /// The highest y where `matches`, scanning down. Sections holding only /// `skip` are passed over whole: `matches` must be false for it. - pub fn highest(&self, skip: u16, mut matches: impl FnMut(i32) -> bool) -> Option { + pub fn highest(&self, skip: u32, mut matches: impl FnMut(i32) -> bool) -> Option { self.sections .iter() .enumerate() @@ -171,7 +185,7 @@ impl Chunk { (base..base + 16).rev().find(|&y| matches(y)) }) } - pub fn set(&mut self, x: i32, y: i32, z: i32, v: u16) -> u16 { + pub fn set(&mut self, x: i32, y: i32, z: i32, v: u32) -> u32 { let s = (y - MIN_Y) >> 4; if !(0..SECTIONS as i32).contains(&s) { return v; @@ -228,15 +242,15 @@ mod tests { Arc::make_mut(&mut chunk.sections[5]).set(i, cave_air); } chunk.set(3, 150, 4, stone); - let full = |c: &Chunk, matches: &dyn Fn(u16) -> bool| { + let full = |c: &Chunk, matches: &dyn Fn(u32) -> bool| { (MIN_Y..MIN_Y + HEIGHT) .rev() .find(|&y| matches(c.get(3, y, 4))) }; for matches in [ - &(|v: u16| v != air) as &dyn Fn(u16) -> bool, - &|v: u16| v == stone, - &|v: u16| v == cave_air, + &(|v: u32| v != air) as &dyn Fn(u32) -> bool, + &|v: u32| v == stone, + &|v: u32| v == cave_air, ] { let fast = chunk.highest(air, |y| matches(chunk.get(3, y, 4))); assert_eq!(fast, full(&chunk, matches)); diff --git a/crates/horizon-client/src/chunk_map.rs b/crates/horizon-client/src/chunk_map.rs index 81e06f020..ed25491c5 100644 --- a/crates/horizon-client/src/chunk_map.rs +++ b/crates/horizon-client/src/chunk_map.rs @@ -76,7 +76,7 @@ impl<'a> IntoIterator for &'a ChunkMap { } } impl crate::light::LightWorld for ChunkMap { - fn state(&self, x: i32, y: i32, z: i32) -> Option { + fn state(&self, x: i32, y: i32, z: i32) -> Option { self.columns.state(x, y, z) } fn light(&self, x: i32, y: i32, z: i32) -> Option { diff --git a/crates/horizon-client/src/crafting.rs b/crates/horizon-client/src/crafting.rs index 457ddf497..0b8f2856b 100644 --- a/crates/horizon-client/src/crafting.rs +++ b/crates/horizon-client/src/crafting.rs @@ -1209,7 +1209,15 @@ impl Recipes { for (prefix, path) in files { let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or(""); let name = format!("{prefix}{stem}"); - let v: Value = serde_json::from_slice(&fs::read(&path)?)?; + // Vanilla's RecipeManager logs a file that does not parse and + // goes on ("Couldn't parse data file"). + let v: Value = match serde_json::from_slice(&fs::read(&path)?) { + Ok(v) => v, + Err(e) => { + log::warn!("Horizon: recipe {name} not loaded: {e}"); + continue; + } + }; let text = |k: &str| v[k].as_str().unwrap_or("").to_owned(); meta.insert(name.clone(), (text("category"), text("group"))); let ing = |k: &str| Ingredient::parse(&v[k], items); diff --git a/crates/horizon-client/src/flatgen.rs b/crates/horizon-client/src/flatgen.rs index d7764c30e..edf601768 100644 --- a/crates/horizon-client/src/flatgen.rs +++ b/crates/horizon-client/src/flatgen.rs @@ -4,7 +4,7 @@ use horizon_world::{nbt::Tag, registry::BlockRegistry}; /// The layers of a flat generator, bottom up from the world's minimum Y. pub struct FlatTerrain { - layers: Vec, + layers: Vec, biome: u16, } impl FlatTerrain { @@ -30,7 +30,7 @@ impl FlatTerrain { for layer in list { let block = crate::save::text(layer.get("block"))?; let height = crate::save::number(layer.get("height")).unwrap_or(0).max(0); - let state = registry.default_state(&block)?.0 as u16; + let state = registry.default_state(&block)?.0; layers.extend(std::iter::repeat_n(state, height as usize)); } } diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 2a7d05ae4..82bff9ca3 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -68,6 +68,7 @@ mod lan_mobs; mod lan_pvp; mod lan_screen; mod lan_trading; +mod lan_waypoints; mod leads; mod level_particles; mod lightning; @@ -967,8 +968,9 @@ struct Game<'a> { /// `ClientAvatarState`: walked distance and view bob. avatar: horizon_client::camera::AvatarBob, flash: u32, - /// Sleeping in a bed: (bed head position, ticks asleep). - sleeping: Option<([i32; 3], u32)>, + /// The head of the bed while sleeping; the visual timer also survives waking. + sleeping: Option<[i32; 3]>, + sleep_timer: u32, dim: Dim, world_root: PathBuf, /// A player carried through a portal, re-placed once the destination loads. @@ -1125,7 +1127,7 @@ struct Game<'a> { lan_events: Vec<(i32, [i32; 3], i32, Option)>, /// Block events (position, b0, b1, state) and entity events (id, event, /// position) for the guests. - lan_block_events: Vec<([i32; 3], i32, i32, u16)>, + lan_block_events: Vec<([i32; 3], i32, i32, u32)>, lan_entity_events: Vec<(i32, i8, [f64; 3])>, /// `ContainerOpenersCounter`: how many players (the host and the guests) /// have each chest open. @@ -3182,7 +3184,7 @@ impl Game<'_> { if !self.streaming.is_lit(cx, cz) { return Ok(()); } - let playing = self.screen == Screen::Playing; + let playing = self.screen == Screen::Playing && self.sleeping.is_none(); let held = |k| playing && self.keys.contains(&k); let keys = Keys { forward: held(KeyCode::KeyW), @@ -3276,7 +3278,12 @@ impl Game<'_> { let feet = [player.position.x, player.position.y, player.position.z]; let player_velocity = [player.velocity.x, player.velocity.y, player.velocity.z]; self.previous_eye = self.eye; - self.eye += (camera_eye.unwrap_or(player.dimensions.eye_height) - self.eye) * 0.5; + let eye_height = if self.sleeping.is_some() { + 0.2 + } else { + player.dimensions.eye_height + }; + self.eye += (camera_eye.unwrap_or(eye_height) - self.eye) * 0.5; let crouching = player.pose == horizon_client::pose::Pose::Crouching; self.tick_avatar(before, riding); self.record_combat(&events); @@ -3539,7 +3546,11 @@ impl Game<'_> { }); horizon_client::spans::time("tick_particles", || self.tick_particles()); } - if self.right && self.use_delay == 0 && self.screen == Screen::Playing { + if self.right + && self.use_delay == 0 + && self.screen == Screen::Playing + && self.sleeping.is_none() + { self.use_with_hand()?; self.use_delay = 4; } @@ -3922,15 +3933,7 @@ impl Game<'_> { self.draw_boss_bars(&mut batch); self.draw_extras(&mut batch); } - // Hud.extractSleepOverlay, under the sidebar, titles and chat. - if let Some((_, ticks)) = self.sleeping { - let draws = hud_overlays::sleep_overlay(wi, hi, ticks as i32); - hud_layout::paint(&mut batch, &self.atlas, &draws); - } if show_hud { - self.draw_titles(&mut batch); - self.draw_scoreboard(&mut batch); - self.draw_subtitles(&mut batch); // The mount's hearts (and the jump bar that replaces the experience bar). let (vehicle_hearts, jump_bar) = if spectator { (0, false) @@ -4002,6 +4005,14 @@ impl Game<'_> { self.draw_selected_name(&mut batch, !self.creative); } } + // Hud.extractSleepOverlay: over all health/food bars, under titles and chat. + let draws = hud_overlays::sleep_overlay(wi, hi, self.sleep_timer as i32); + hud_layout::paint(&mut batch, &self.atlas, &draws); + if show_hud { + self.draw_titles(&mut batch); + self.draw_scoreboard(&mut batch); + self.draw_subtitles(&mut batch); + } let atlas = &self.atlas; if show_hud && let Some((text, at)) = &self.message @@ -6231,6 +6242,7 @@ impl Game<'_> { self.fns = Default::default(); self.bosses = Default::default(); reset_world_progress(&mut self.adv, &mut self.stats); + self.prefetch_advancement_tree(); self.restore_player_environment = true; // RandomSequences: the world's data/minecraft/random_sequences.dat. self.rand_sequences = @@ -6368,6 +6380,8 @@ impl Game<'_> { self.clear_entities(); self.travelling = None; self.player = None; + self.sleeping = None; + self.sleep_timer = 0; self.spawn = None; self.respawn_travel = None; self.respawn_config = @@ -6725,6 +6739,8 @@ impl Game<'_> { self.streaming.paused = true; self.world = World::new(self.res.facts.air, 0); self.player = None; + self.sleeping = None; + self.sleep_timer = 0; self.clear_entities(); self.screen = Screen::Playing; if let Some(r) = self.renderer.as_mut() { @@ -7554,6 +7570,7 @@ impl Game<'_> { // GameRenderer.renderItemInHand: F1 hides the hand with the HUD. if self.spectator() || self.ui.hide_gui + || self.sleeping.is_some() || !self.camera_type.first_person() || self .player @@ -8029,13 +8046,23 @@ impl Game<'_> { return; } self.message = Some(("Respawn point set".into(), Instant::now())); - self.sleeping = Some((head, 0)); + self.stop_mining(); + self.left = false; + self.right = false; + self.using = None; + self.sleeping = Some(head); + self.sleep_timer = 0; self.stats.custom("sleep_in_bed", 1); self.trigger_player("minecraft:slept_in_bed"); self.stats.reset("custom", "minecraft:time_since_rest"); } + fn wake_up(&mut self) { + self.sleeping = None; + self.sleep_timer = 100; + } fn tick_sleep(&mut self) { - let Some((head, ticks)) = self.sleeping else { + self.sleep_timer = tick_sleep_timer(self.sleep_timer, self.sleeping.is_some()); + let Some(head) = self.sleeping else { return; }; if self.keys.contains(&KeyCode::ShiftLeft) @@ -8044,7 +8071,7 @@ impl Game<'_> { .as_ref() .is_none_or(|p| p.health.dead_or_dying()) { - self.sleeping = None; + self.wake_up(); return; } if let Some(p) = self.player.as_mut() { @@ -8055,18 +8082,15 @@ impl Game<'_> { ); p.velocity = Vec3::ZERO; } - let ticks = ticks + 1; // ServerLevel: once everyone has slept 100 ticks, skip to morning and clear weather. - if ticks >= 100 { + if self.sleep_timer >= 100 { let t = self.world.day_time; self.world.day_time = t + 24000 - t.rem_euclid(24000); self.world.weather.raining = false; self.world.weather.thundering = false; self.world.weather.rain_time = 0; self.world.weather.thunder_time = 0; - self.sleeping = None; - } else { - self.sleeping = Some((head, ticks)); + self.wake_up(); } } fn creative_origin(batch: &HudBatch) -> (f32, f32) { @@ -8874,6 +8898,18 @@ impl Game<'_> { } /// A key press while a world is open (keyboard or control socket). fn key_pressed(&mut self, key: KeyCode) -> Result<()> { + if self.sleeping.is_some() + && self.screen == Screen::Playing + && !matches!( + key, + KeyCode::F1 | KeyCode::F2 | KeyCode::F11 | KeyCode::KeyT | KeyCode::Slash + ) + { + if matches!(key, KeyCode::Escape | KeyCode::ShiftLeft) { + self.wake_up(); + } + return Ok(()); + } if self.sort_key_press(key) { return Ok(()); } @@ -9004,6 +9040,9 @@ impl Game<'_> { } /// A mouse button while playing (window or control socket). fn play_mouse(&mut self, button: MouseButton, pressed: bool) -> Result<()> { + if self.sleeping.is_some() { + return Ok(()); + } if self.remote.is_some() { return self.remote_mouse(button, pressed); } @@ -9333,6 +9372,7 @@ impl ApplicationHandler for Game<'_> { title.scroll_worlds(d, self.cursor, (batch.width, batch.height)); } if self.screen == Screen::Playing + && self.sleeping.is_none() && self.player.is_some() && !self.extras_scroll(d) && let Some((wheel, _)) = @@ -10124,6 +10164,7 @@ impl<'a> Game<'a> { avatar: Default::default(), flash: 0, sleeping: None, + sleep_timer: 0, dim: Dim::Overworld, world_root: PathBuf::new(), travelling: None, @@ -10270,6 +10311,9 @@ pub fn run() -> Result<()> { horizon_client::spans::enable(options.spikes.is_some()); let vanilla_root = options.resources.clone(); let early_settings = Settings::load(&std::env::current_dir()?.join("options.txt")); + // Snapshots of derived tables live in the game folder, never beside the + // shared resources. + Behaviours::set_cache_dir(std::env::current_dir()?.join(".horizon-cache")); let mut pack_error = None; options.resources = packs::startup_root(&vanilla_root, &early_settings.resource_packs) .unwrap_or_else(|e| { @@ -10519,6 +10563,17 @@ pub fn run() -> Result<()> { Ok(()) } +// Player.tick: cap the sleeping fade at 100, then fade out over 10 waking ticks. +fn tick_sleep_timer(timer: u32, sleeping: bool) -> u32 { + if sleeping { + (timer + 1).min(100) + } else if (1..109).contains(&timer) { + timer + 1 + } else { + 0 + } +} + fn reset_world_progress(adv: &mut advancements::AdvState, stats: &mut stats::Stats) { *adv = Default::default(); *stats = Default::default(); @@ -10553,6 +10608,35 @@ mod tests { use horizon_client::container_screen::{Button, Clicks, Release, Target}; use horizon_client::keymapping::{KeyAction, SPRINT, ToggleKeys}; + #[test] + fn sleep_fade_lasts_ten_ticks_after_waking() { + let alpha = |timer| match hud_overlays::sleep_overlay(320, 240, timer).first() { + Some(hud_layout::GuiDraw::Fill { color, .. }) => color >> 24, + None => 0, + _ => panic!("sleep overlay must be a full-screen fill"), + }; + assert_eq!(tick_sleep_timer(0, false), 0); + let mut timer = 0; + for _ in 0..50 { + timer = tick_sleep_timer(timer, true); + } + assert_eq!(alpha(timer as i32), 110); + for _ in 0..100 { + timer = tick_sleep_timer(timer, true); + } + assert_eq!(timer, 100); + assert_eq!(alpha(timer as i32), 220); + for _ in 0..5 { + timer = tick_sleep_timer(timer, false); + } + assert_eq!(alpha(timer as i32), 110); + for _ in 0..5 { + timer = tick_sleep_timer(timer, false); + } + assert_eq!(timer, 0); + assert!(hud_overlays::sleep_overlay(320, 240, timer as i32).is_empty()); + } + /// An integrated server thread shares the loaded resources with the client. #[test] fn shared_resources_can_cross_threads() { diff --git a/crates/horizon-client/src/game/advancements.rs b/crates/horizon-client/src/game/advancements.rs index 22d9a4da0..5cf09bc44 100644 --- a/crates/horizon-client/src/game/advancements.rs +++ b/crates/horizon-client/src/game/advancements.rs @@ -88,6 +88,9 @@ pub(super) struct AdvState { pub(super) changed: BTreeSet, pub(super) unlocked: Vec, pub(super) announced: Vec, + /// The tree being read on another thread since the world opened + /// (`prefetch_advancement_tree`), taken by the first `advancement_tree`. + pub(super) pending_tree: Option>, } /// The game's world for predicates. struct View<'a, 'g>(&'a Game<'g>); @@ -221,10 +224,27 @@ impl Game<'_> { /// The advancement tree, loaded once. pub(super) fn advancement_tree(&mut self) -> Arc { if self.adv.tree.list.is_empty() { - self.adv.tree = Arc::new(Tree::load(&self.options.resources.join("data"))); + let tree = match self.adv.pending_tree.take().map(|h| h.join()) { + Some(Ok(tree)) => tree, + _ => Tree::load(&self.options.resources.join("data")), + }; + self.adv.tree = Arc::new(tree); } self.adv.tree.clone() } + /// Start reading the advancement files on another thread while the world + /// loads. The first tick's first criterion trigger used to read and parse + /// all of them on the game thread (about 60 ms). + pub(super) fn prefetch_advancement_tree(&mut self) { + if !self.adv.tree.list.is_empty() || self.adv.pending_tree.is_some() { + return; + } + let data = self.options.resources.join("data"); + self.adv.pending_tree = std::thread::Builder::new() + .name("horizon-advancements".into()) + .spawn(move || Tree::load(&data)) + .ok(); + } pub(super) fn load_advancements(&mut self) { if self.adv.loaded || self.remote.is_some() || self.no_player_here() { return; diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index 6c4817f63..962075ee7 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -2312,7 +2312,7 @@ impl Game<'_> { }; let from = self.source_position().map(|v| v.floor() as i32); const RANGE: i32 = 256; - let mut cache: std::collections::HashMap> = + let mut cache: std::collections::HashMap> = std::collections::HashMap::new(); let mut best: Option<(i64, [i32; 3], &'static str)> = None; let (cx0, cz0) = ((from[0] - RANGE) >> 4, (from[2] - RANGE) >> 4); @@ -2323,15 +2323,12 @@ impl Game<'_> { continue; }; for (i, section) in chunk.sections.iter().enumerate() { - let mut wanted_state = |v: u32| { - let state = v as u16; + let mut wanted_state = |state: u32| { cache .entry(state) .or_insert_with(|| { - self.poi_type(horizon_world::registry::BlockStateId(u32::from( - state, - ))) - .filter(|t| wanted.iter().any(|w| w == t)) + self.poi_type(horizon_world::registry::BlockStateId(state)) + .filter(|t| wanted.iter().any(|w| w == t)) }) .is_some() }; diff --git a/crates/horizon-client/src/game/creaking.rs b/crates/horizon-client/src/game/creaking.rs index 1caa419ff..b079545e6 100644 --- a/crates/horizon-client/src/game/creaking.rs +++ b/crates/horizon-client/src/game/creaking.rs @@ -333,7 +333,7 @@ impl Game<'_> { } for i in 0..4096 { let id = section.get(i); - if !hearts.get(usize::from(id)).copied().unwrap_or(false) { + if !hearts.get(id as usize).copied().unwrap_or(false) { continue; } let (lx, lz, ly) = ((i & 15) as i32, ((i >> 4) & 15) as i32, (i >> 8) as i32); diff --git a/crates/horizon-client/src/game/dedicated.rs b/crates/horizon-client/src/game/dedicated.rs index 9c4b41db0..33bedf696 100644 --- a/crates/horizon-client/src/game/dedicated.rs +++ b/crates/horizon-client/src/game/dedicated.rs @@ -13,7 +13,9 @@ use horizon_client::{ server::{ eula, java_properties::now_millis, + profiles, properties::{GAME_MODES, ServerProperties, SystemEntropy}, + user_cache::UserCache, user_lists::{ BanEntry, BanList, IpBanList, NameAndId, OpEntry, OpList, WhiteEntry, WhiteList, format_date, @@ -57,6 +59,18 @@ struct Setup { started: Instant, } static SETUP: OnceLock>> = OnceLock::new(); +/// usercache.json (Services.nameToIdCache): used by the server thread only. +static USER_CACHE: Mutex> = Mutex::new(None); + +/// PlayerList.placeNewPlayer: a player who joined is cached (and the file saved). +pub(super) fn player_joined(id: u128, name: &str) { + if let Some(cache) = USER_CACHE.lock().unwrap().as_mut() { + cache.add(NameAndId { + id, + name: name.to_owned(), + }); + } +} /// Log4j's console pattern: `[HH:mm:ss] [thread/LEVEL]: message`. pub(super) fn log(level: &str, message: &str) { @@ -348,6 +362,14 @@ impl Game<'_> { line.clear(); } })?; + // Main: Services.create loads the universe's usercache.json. + let (mut cache, warnings) = UserCache::load(universe.join("usercache.json")); + for warning in warnings { + log_from("main", "WARN", &warning); + } + // DedicatedServer.initServer: names nobody has resolve offline only without authentication. + cache.resolve_offline_users = !props.online_mode; + *USER_CACHE.lock().unwrap() = Some(cache); log("INFO", "Starting minecraft server version 26.3"); log("INFO", "Loading properties"); let game_mode = props.game_mode; @@ -641,23 +663,36 @@ impl Game<'_> { host.hub.resend_commands(); } - /// A known player by name: online first, else the offline profile. - fn resolve(&self, name: &str) -> NameAndId { - self.lan - .as_ref() - .and_then(|h| { - h.hub - .guests - .iter() - .find(|g| g.name.eq_ignore_ascii_case(name)) + /// A player on the server, by name. + fn online_player(&self, name: &str) -> Option { + let hub = &self.lan.as_ref()?.hub; + let g = hub + .guests + .iter() + .find(|g| g.name.eq_ignore_ascii_case(name))?; + Some(NameAndId { + id: g.uuid, + name: g.name.clone(), + }) + } + + /// GameProfileArgument for a name: the user cache, else the profile + /// service, else (offline) the offline player. None, told to the console: + /// no such player. + fn resolve(&self, name: &str) -> Option { + let found = USER_CACHE.lock().unwrap().as_mut().and_then(|cache| { + cache.get_by_name(name, |lower| { + let found = profiles::find_by_name(&profiles::discovery_url(), lower); + if found.is_none() { + log("WARN", &format!("Couldn't find profile with name: {lower}")); + } + found }) - .map_or_else( - || NameAndId::offline(name), - |g| NameAndId { - id: g.uuid, - name: g.name.clone(), - }, - ) + }); + if found.is_none() { + self.console_reply(Text::translate("argument.player.unknown", &[])); + } + found } /// MinecraftServer.sendSystemMessage: what the console is told. @@ -740,7 +775,9 @@ impl Game<'_> { )); } "op" | "deop" if !rest.is_empty() => { - let user = self.resolve(rest[0]); + let Some(user) = self.resolve(rest[0]) else { + return; + }; let level = 4; let changed = { let mut l = state.lists.lock().unwrap(); @@ -770,7 +807,9 @@ impl Game<'_> { } "whitelist" => self.console_whitelist(state, &rest), "ban" if !rest.is_empty() => { - let user = self.resolve(rest[0]); + let Some(user) = self.resolve(rest[0]) else { + return; + }; let reason = (rest.len() > 1).then(|| rest[1..].join(" ")); let added = state.lists.lock().unwrap().bans.add(BanEntry::new( user.clone(), @@ -860,7 +899,9 @@ impl Game<'_> { } } "pardon" if !rest.is_empty() => { - let user = self.resolve(rest[0]); + let Some(user) = self.resolve(rest[0]) else { + return; + }; let removed = state.lists.lock().unwrap().bans.remove(&user); let key = if removed { "commands.pardon.success" @@ -926,15 +967,10 @@ impl Game<'_> { } } "kick" if !rest.is_empty() => { - let user = self.resolve(rest[0]); - let online = self - .lan - .as_ref() - .is_some_and(|h| h.hub.guests.iter().any(|g| g.uuid == user.id)); - if !online { + let Some(user) = self.online_player(rest[0]) else { self.console_reply(Text::translate("argument.entity.notfound.player", &[])); return; - } + }; let reason = if rest.len() > 1 { Tag::String(JavaString::from(rest[1..].join(" ").as_str())) } else { @@ -1014,7 +1050,9 @@ impl Game<'_> { self.console_reply(Text::translate("commands.whitelist.reloaded", &[])); } ["add", name] | ["remove", name] => { - let user = self.resolve(name); + let Some(user) = self.resolve(name) else { + return; + }; let mut l = state.lists.lock().unwrap(); let (ok, key) = if rest[0] == "add" { if l.white.contains(&user) { diff --git a/crates/horizon-client/src/game/fov.rs b/crates/horizon-client/src/game/fov.rs index 952a266d8..64047b4f7 100644 --- a/crates/horizon-client/src/game/fov.rs +++ b/crates/horizon-client/src/game/fov.rs @@ -55,7 +55,13 @@ impl Game<'_> { .fov_player() .map_or(1.0, |p| field_of_view_modifier(&p, first_person, effect)); let target = self.zoomed_fov_target(target); - let eye = self.player.as_ref().map(|p| p.dimensions.eye_height); + let eye = self.player.as_ref().map(|p| { + if self.sleeping.is_some() { + 0.2 + } else { + p.dimensions.eye_height + } + }); self.view.tick(eye, target); } /// The player's `deathTime` while dead or dying. diff --git a/crates/horizon-client/src/game/hand.rs b/crates/horizon-client/src/game/hand.rs index a932913af..19e66174b 100644 --- a/crates/horizon-client/src/game/hand.rs +++ b/crates/horizon-client/src/game/hand.rs @@ -67,7 +67,7 @@ impl Game<'_> { /// attack clicks since the last tick and the held attack button. pub(super) fn tick_local_hand(&mut self) -> Result<()> { let attack_clicks = std::mem::take(&mut self.attack_clicks); - if self.screen == Screen::Playing && self.title.is_none() { + if self.screen == Screen::Playing && self.title.is_none() && self.sleeping.is_none() { let mut clicks = std::mem::take(&mut self.clicks); // keyAttack.isDown(); playing implies the grabbed mouse here // (scripts under --control hold buttons without grabbing it). diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 078083006..1113c31f5 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -57,6 +57,10 @@ pub(super) struct LanHost { pub mob_players: HashMap, /// An integrated server's owner (game/integrated.rs). pub owner: Option, + /// The players' locator bars (game/lan_waypoints.rs), and the guests who + /// are dead, whose respawn replaces their waypoint. + pub waypoints: lan::waypoints::Waypoints, + pub waypoint_dead: std::collections::HashSet, } /// The player who owns an integrated server's world: a guest whose state is @@ -258,7 +262,7 @@ fn mob_meta(m: &Mob, hub: &Hub) -> Vec<(u8, i32, Value)> { meta.push(( 16, serializer::OPTIONAL_BLOCK_STATE, - Value::Id(i32::from(state.0 as u16)), + Value::Id(state.0 as i32), )); } } @@ -502,6 +506,8 @@ impl Game<'_> { mob_state: HashMap::new(), mob_players: HashMap::new(), owner: None, + waypoints: Default::default(), + waypoint_dead: Default::default(), }); self.audio.capture = Some(Vec::new()); Ok(()) @@ -782,7 +788,12 @@ impl Game<'_> { }, tick_rate: self.clock.rate(), tick_frozen: self.clock.frozen(), + collision: Some(self.collision), }; + host.hub.movement_checks = ( + self.ui.rules.bool("player_movement_check"), + self.ui.rules.bool("elytra_movement_check"), + ); host.hub.tick(&mut ctx) }; if let Some(state) = &state @@ -799,6 +810,7 @@ impl Game<'_> { && let Some(g) = host.hub.guest(conn) { // PlayerList.placeNewPlayer, then the broadcast join message. + super::dedicated::player_joined(g.uuid, &g.name); let [x, y, z] = g.position; super::dedicated::log( "INFO", @@ -845,6 +857,10 @@ impl Game<'_> { super::dedicated::log("INFO", &format!("[Not Secure] {line}")); } HostEvent::Chat(line) => self.system_message(Text::literal(line)), + HostEvent::Warn(line) if self.options.dedicated.is_some() => { + super::dedicated::log("WARN", &line); + } + HostEvent::Warn(line) => log::warn!("{line}"), HostEvent::Packet(conn, packet) => { self.lan_guest_packet(&mut host, conn, *packet); } @@ -857,6 +873,7 @@ impl Game<'_> { self.lan_swings(&mut host); self.lan_update_streaming(&host); self.lan_update_models(&mut host); + self.lan_update_waypoints(&mut host, state.as_ref()); drop(holding); for g in &host.hub.guests { g.sender.flush(); @@ -910,6 +927,7 @@ impl Game<'_> { clock_rate: self.ui.clock.rate, tick_rate: self.clock.rate(), tick_frozen: self.clock.frozen(), + collision: Some(self.collision), }; host.hub.place_new_player(conn, &mut ctx); } @@ -932,7 +950,7 @@ impl Game<'_> { /// `Level.blockEvent` for the guests (`ClientboundBlockEventPacket`). pub(super) fn lan_block_event(&mut self, pos: [i32; 3], b0: i32, b1: i32) { if self.audio.capture.is_some() { - let state = self.world.block(pos[0], pos[1], pos[2]).0 as u16; + let state = self.world.block(pos[0], pos[1], pos[2]).0; self.lan_block_events.push((pos, b0, b1, state)); } } diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 1d3433cf0..7cd4b0474 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -944,6 +944,7 @@ impl Game<'_> { }, tick_rate: self.clock.rate(), tick_frozen: self.clock.frozen(), + collision: Some(self.collision), }; host.hub.respawn(i, spawn, &ctx); } diff --git a/crates/horizon-client/src/game/lan_waypoints.rs b/crates/horizon-client/src/game/lan_waypoints.rs new file mode 100644 index 000000000..0d0ab69e4 --- /dev/null +++ b/crates/horizon-client/src/game/lan_waypoints.rs @@ -0,0 +1,95 @@ +//! The locator bar of the players on a served world (lan/waypoints.rs): what +//! each player looks like to the waypoint manager this tick. +use super::{Game, lan::LanHost}; +use horizon_client::{ + items::ItemStack, + lan::{ + hub::HostState, + waypoints::{self, HIDING_HEADS, Icon, PLAYER_RANGE}, + }, +}; + +/// `waypoint_transmit_range`: hidden by crouching, invisibility or a head worn. +fn transmit_range(crouching: bool, invisible: bool, head: Option<&ItemStack>) -> f64 { + let hiding_head = head.is_some_and(|s| HIDING_HEADS.contains(&&*s.item)); + if crouching || invisible || hiding_head { + 0.0 + } else { + PLAYER_RANGE + } +} + +impl Game<'_> { + /// The player's team and its colour, as the locator bar shows it. + fn waypoint_team(&self, name: &str) -> Option<(String, Option)> { + let team = self.score.board.players_team(name)?; + let color = team.color.as_deref().and_then(waypoints::team_rgb); + Some((team.name.clone(), color)) + } + + pub(super) fn lan_update_waypoints(&mut self, host: &mut LanHost, state: Option<&HostState>) { + let mut players = Vec::new(); + if let Some(h) = state { + let icon = self + .cmds + .waypoint_icons + .get(&super::commands::Target::Player) + .map(|i| Icon { + style: i.style.clone(), + color: i.color, + }) + .unwrap_or_default(); + players.push(waypoints::Player { + uuid: h.uuid, + conn: None, + position: h.position, + spectator: self.spectator(), + transmit_range: transmit_range( + h.sneaking, + self.potions.effects.get("invisibility").is_some(), + h.equipment[5].as_ref(), + ), + receive_range: PLAYER_RANGE, + first_tick: false, + view_distance: host.hub.config.view_distance, + team: self.waypoint_team(&h.name), + icon, + }); + } + let mut respawned = Vec::new(); + for g in host.hub.guests.iter().filter(|g| g.placed) { + // PlayerList.respawn makes a new ServerPlayer. + if g.dead { + host.waypoint_dead.insert(g.uuid); + } else if host.waypoint_dead.remove(&g.uuid) { + respawned.push(g.uuid); + } + // Player.updatePlayerPose: the shift key crouches unless flying. + let crouching = g.sneaking && !g.flying && !g.fall_flying && !g.sleeping; + players.push(waypoints::Player { + uuid: g.uuid, + conn: Some(g.conn), + position: g.position, + spectator: g.spectator(), + transmit_range: transmit_range( + crouching, + g.effects.get("invisibility").is_some(), + g.armor[0].as_ref(), + ), + receive_range: PLAYER_RANGE, + first_tick: g.ticks == 0, + view_distance: g.view_distance, + team: self.waypoint_team(&g.name), + icon: Icon::default(), + }); + } + host.waypoint_dead + .retain(|uuid| players.iter().any(|p| p.uuid == *uuid)); + let locator_bar = self.ui.rules.bool("locator_bar"); + for (conn, packet) in host.waypoints.tick(&players, &respawned, locator_bar) { + if let Some(g) = host.hub.guest(conn) { + g.send(&packet); + } + } + } +} diff --git a/crates/horizon-client/src/game/machines.rs b/crates/horizon-client/src/game/machines.rs index 0d334d248..6947041f2 100644 --- a/crates/horizon-client/src/game/machines.rs +++ b/crates/horizon-client/src/game/machines.rs @@ -506,11 +506,15 @@ impl Game<'_> { .default_state("minecraft:hopper") .map(|s| self.res.facts.get(s).name.clone()); let Some(hopper) = hopper else { return Ok(()) }; + // A hopper's block entity is a plain container (`Chest`): only those + // need their block looked up (the ticker runs for a HopperBlockEntity + // whose block is still a hopper, `BlockEntityType.isValid`). let mut positions: Vec = self .world .block_entities - .keys() - .copied() + .iter() + .filter(|(_, e)| matches!(e, BlockEntity::Chest(_))) + .map(|(p, _)| *p) .filter(|p| self.res.facts.get(self.world.block(p.0, p.1, p.2)).name == hopper) .collect(); positions.sort_unstable(); diff --git a/crates/horizon-client/src/game/natural_spawning.rs b/crates/horizon-client/src/game/natural_spawning.rs index 571bbf5e0..80d26a823 100644 --- a/crates/horizon-client/src/game/natural_spawning.rs +++ b/crates/horizon-client/src/game/natural_spawning.rs @@ -206,7 +206,7 @@ impl NaturalLevel for GameLevel<'_, '_> { .chunks .get(&(x >> 4, z >> 4)) .and_then(|chunk| { - chunk.highest(world.air.0 as u16, |y| { + chunk.highest(world.air.0, |y| { spawn_selection::heightmap_blocks(self, kind, x, y, z) }) }) diff --git a/crates/horizon-client/src/game/nether_portal.rs b/crates/horizon-client/src/game/nether_portal.rs index df684e713..9ff9e8421 100644 --- a/crates/horizon-client/src/game/nether_portal.rs +++ b/crates/horizon-client/src/game/nether_portal.rs @@ -486,9 +486,10 @@ impl Game<'_> { .filter(|m| self.mob_can_use_portal(m)) .map(|m| (m.id, m.position, m.width(), m.height())) .collect(); + let mut portal_sections = horizon_client::fast_hash::FxHashMap::default(); for (id, to, width, height) in items.into_iter().chain(mobs) { let from = self.portals.before.get(&id).copied().unwrap_or(to); - if let Some(pos) = self.swept_portal(from, to, width, height) { + if let Some(pos) = self.swept_portal(from, to, width, height, &mut portal_sections) { inside.push((id, pos)); } } @@ -502,7 +503,14 @@ impl Game<'_> { } /// The first nether portal block an entity box swept through /// (`Entity.checkInsideBlocks`, whose first portal sets the entry). - fn swept_portal(&self, from: Vec3, to: Vec3, width: f32, height: f32) -> Option { + fn swept_portal( + &self, + from: Vec3, + to: Vec3, + width: f32, + height: f32, + portal_sections: &mut horizon_client::fast_hash::FxHashMap<(i32, i32, i32), bool>, + ) -> Option { let half = f64::from(width) / 2.0; let bounds = horizon_core::geometry::Aabb::new( Vec3::new(to.x - half, to.y, to.z - half), @@ -513,6 +521,13 @@ impl Game<'_> { f64::from(1.0e-5_f32), f64::from(1.0e-5_f32), )); + // Every block the sweep visits lies in the box covering both ends. + let reach = bounds + .union_bounds(bounds.move_by(from.subtract(to))) + .inflate(Vec3::new(1.0, 1.0, 1.0)); + if !self.may_hold_portal(reach, portal_sections) { + return None; + } let mut found = None; let _ = horizon_core::geometry::sweep_intersected_blocks( from, @@ -534,6 +549,54 @@ impl Game<'_> { ); found } + /// Whether a section `reach` touches has a nether portal state in its + /// palette: without one, no block there is a portal. Answers are kept + /// per section for the tick; a box over many sections is not checked. + fn may_hold_portal( + &self, + reach: horizon_core::geometry::Aabb, + seen: &mut horizon_client::fast_hash::FxHashMap<(i32, i32, i32), bool>, + ) -> bool { + use horizon_client::chunk::{MIN_Y, SECTIONS}; + let (min, max) = (reach.min_position(), reach.max_position()); + if !min.is_finite() || !max.is_finite() { + return true; + } + let (min, max) = (BlockPos::containing(min), BlockPos::containing(max)); + let top = SECTIONS as i32 - 1; + let (x0, x1) = (min.x >> 4, max.x >> 4); + let (z0, z1) = (min.z >> 4, max.z >> 4); + let (y0, y1) = ( + ((min.y - MIN_Y) >> 4).clamp(0, top), + ((max.y - MIN_Y) >> 4).clamp(0, top), + ); + let count = (i64::from(x1) - i64::from(x0) + 1) + * (i64::from(y1) - i64::from(y0) + 1) + * (i64::from(z1) - i64::from(z0) + 1); + if count > 64 { + return true; + } + let behaviours = &self.res.behaviours; + let chunks = &self.world.chunks; + (x0..=x1).any(|cx| { + (z0..=z1).any(|cz| { + (y0..=y1).any(|sy| { + *seen.entry((cx, sy, cz)).or_insert_with(|| { + chunks + .get(&(cx, cz)) + .and_then(|c| c.sections.get(sy as usize)) + .is_some_and(|s| { + s.blocks.maybe_has(|v| { + behaviours + .portal_axis(horizon_world::registry::BlockStateId(v)) + .is_some() + }) + }) + }) + }) + }) + }) + } /// `Entity.teleportCrossDimension` away from the loaded dimension: the /// entity is saved and removed, and waits for its dimension to load. fn send_through_portal(&mut self, id: u32, entry: BlockPos) { diff --git a/crates/horizon-client/src/game/perspective.rs b/crates/horizon-client/src/game/perspective.rs index 50abc8e56..4214bdd70 100644 --- a/crates/horizon-client/src/game/perspective.rs +++ b/crates/horizon-client/src/game/perspective.rs @@ -201,7 +201,7 @@ impl Game<'_> { ) }; let p = self.player.as_ref()?; - let sleeping = self.sleeping.map(|(head, _)| { + let sleeping = self.sleeping.map(|head| { let state = self.world.block(head[0], head[1], head[2]); let props = self .pack @@ -320,6 +320,15 @@ impl Game<'_> { .map_or(25, |s| Self::charge_ticks(s).max(1)) as i32; let attack = (self.swing.old_animation, self.swing.animation); let swinging = self.swing.swinging(); + // currentSwing.hand().asArm(mainArm). + let swing_right_arm = swinging.then(|| { + (self.swing.attack_hand() == horizon_client::first_person::Hand::Main) + != self.settings.main_hand_left + }); + // LivingEntity.isVisuallySwimming and Entity.isInWater. + let visually_swimming = + p.pose == Pose::Swimming || (!p.fall_flying && p.pose == Pose::FallFlying); + let in_water = p.fluids.was_touching_water; let (yaw, pitch) = (self.yaw, self.pitch); let main = self.inventory[self.selected] .as_ref() @@ -387,6 +396,8 @@ impl Game<'_> { m.extra.charged = charged; m.extra.ints[5] = charge_duration; m.extra.elytra.tick(fall_flying, crouching, velocity); + m.extra.swim.tick(visually_swimming, in_water); + m.extra.swing_right_arm = swing_right_arm; m.hurt_time = hurt; } /// Whether the player's own model shows in the world view. diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 05e1f81c3..64ed8c3e6 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -9,7 +9,7 @@ use super::{Dim, Game, Mining, Screen}; use horizon_client::{ Result, - chunk::{BIOME_STRATEGY, BLOCK_STRATEGY, Chunk, Light, MIN_Y, SECTIONS, Section}, + chunk::{BIOME_STRATEGY, Chunk, Light, MIN_Y, SECTIONS, Section, block_strategy}, containers::{BlockEntity, Furnace, FurnaceKind}, entities::{Experience, ItemEntity}, hud::{HudBatch, rgba}, @@ -22,7 +22,11 @@ use horizon_client::{ streaming::{ChunkGenerator, Proto, Streaming}, world::World, }; -use horizon_core::{geometry::Vec3, paletted::PalettedContainer, random::RandomSource}; +use horizon_core::{ + geometry::Vec3, + paletted::{PalettedContainer, Strategy}, + random::RandomSource, +}; use horizon_protocol::{ entity_data::{ITEM_ENTITY_ITEM, Value}, item::{Hashed, Stack}, @@ -633,11 +637,31 @@ impl Game<'_> { if !(0..SECTIONS as i32).contains(&target) { continue; } - let strategy = BLOCK_STRATEGY; + // A global section is as wide as the server's registry: 16 bits + // from a vanilla server, wider from a Horizon host with the same + // content mods. Read it at that width, then widen it to ours. + let ours = block_strategy(); + let strategy = if ours.wire_has_palette(s.states.bits) { + ours + } else { + Strategy::blocks(s.states.bits) + }; let palette = s.states.palette.iter().map(|&p| p as u32).collect(); let words = s.states.data.iter().map(|&w| w as u64).collect(); - let blocks = PalettedContainer::from_wire(strategy, s.states.bits, palette, words) + let mut blocks = PalettedContainer::from_wire(strategy, s.states.bits, palette, words) .map_err(|e| e.to_string())?; + if strategy.global_bits != ours.global_bits { + let mut values = [0u32; 4096]; + blocks.unpack(&mut values); + // Ids past our registry (a host with mods we lack) read as air. + let limit = 1u64 << ours.global_bits; + for v in &mut values { + if u64::from(*v) >= limit { + *v = 0; + } + } + blocks = PalettedContainer::from_values(ours, &values); + } let mut section = Section { blocks, biomes: PalettedContainer::from_values(BIOME_STRATEGY, &cells), diff --git a/crates/horizon-client/src/game/sculk.rs b/crates/horizon-client/src/game/sculk.rs index 6701181e7..567ac29a1 100644 --- a/crates/horizon-client/src/game/sculk.rs +++ b/crates/horizon-client/src/game/sculk.rs @@ -301,11 +301,11 @@ impl Game<'_> { self.world.watched = t.kinds.iter().map(|k| *k != 0).collect(); self.sculk.tables = t; } - fn kind_of_state(&self, state: u16) -> u8 { + fn kind_of_state(&self, state: u32) -> u8 { self.sculk .tables .kinds - .get(usize::from(state)) + .get(state as usize) .copied() .unwrap_or(0) } @@ -345,10 +345,10 @@ impl Game<'_> { // SculkShriekerBlockEntity.preRemoveSideEffects. if old_kind == KIND_SHRIEKER && let Some(BlockEntity::Sculk(e)) = self.world.block_entities.get(&key) - && self.shrieker_shrieking(BlockStateId(u32::from(old))) + && self.shrieker_shrieking(BlockStateId(old)) { let warning = e.warning_level; - self.shrieker_respond(key, BlockStateId(u32::from(old)), warning); + self.shrieker_respond(key, BlockStateId(old), warning); } if matches!( self.world.block_entities.get(&key), @@ -373,11 +373,16 @@ impl Game<'_> { let mut found = Vec::new(); for (s, section) in chunk.sections.iter().enumerate() { let y0 = MIN_Y + s as i32 * 16; + // Without a sculk state in its palette, no block of the section + // is one: most sections are passed over without reading a block. + if !section.blocks.maybe_has(|v| self.kind_of_state(v) != 0) { + continue; + } if section.blocks.uniform().is_some() { // A uniform section of sculk blocks does not exist. continue; } - let mut a = [0u16; 4096]; + let mut a = [0u32; 4096]; section.unpack(&mut a); for (i, v) in a.iter().enumerate() { if self.kind_of_state(*v) != 0 { @@ -847,12 +852,12 @@ impl Game<'_> { let state = self.world.block(key.0, key.1, key.2); let is_warden = matches!(who, Who::Mob(id) if self.mobs.iter().any(|m| m.id == id && m.species() == Species::Warden)); - match self.kind_of_state(state.0 as u16) { + match self.kind_of_state(state.0) { KIND_SENSOR | KIND_CALIBRATED => { if is_warden || !self.res.behaviours.sculk_sensor_can_activate(state) { continue; } - let calibrated = self.kind_of_state(state.0 as u16) == KIND_CALIBRATED; + let calibrated = self.kind_of_state(state.0) == KIND_CALIBRATED; if calibrated { let back = self.calibrated_signal(key); if back != 0 && back != 1 { diff --git a/crates/horizon-client/src/game/server_travel.rs b/crates/horizon-client/src/game/server_travel.rs index d5b1e4d95..e82291978 100644 --- a/crates/horizon-client/src/game/server_travel.rs +++ b/crates/horizon-client/src/game/server_travel.rs @@ -243,6 +243,7 @@ impl Game<'_> { }, tick_rate: self.clock.rate(), tick_frozen: self.clock.frozen(), + collision: Some(self.collision), } } } diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 7391ad8b9..4280efd4a 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -385,7 +385,7 @@ fn sprite_split() -> u32 { .unwrap_or(SPRITE_SPLIT) } /// Sprite arrays terrain.wgsl binds (`sprites`, `sprites_1`...). -const SPRITE_ARRAYS: u32 = 4; +const SPRITE_ARRAYS: u32 = 8; /// The views of terrain.wgsl's sprite arrays, the unused ones a 1-layer /// stand-in. fn sprite_views( @@ -441,6 +441,10 @@ fn terrain_group( view(5, &sprites[1]), view(6, &sprites[2]), view(7, &sprites[3]), + view(8, &sprites[4]), + view(9, &sprites[5]), + view(10, &sprites[6]), + view(11, &sprites[7]), ], }) } @@ -615,8 +619,12 @@ impl Renderer { adapter .request_device(&wgpu::DeviceDescriptor { label: Some("Horizon device"), + // Mappable primary buffers: section pages that no + // longer fit in video memory go to system memory. required_features: timing::GpuTimer::features(&adapter) - | (adapter.features() & wgpu::Features::INDIRECT_FIRST_INSTANCE), + | (adapter.features() + & (wgpu::Features::INDIRECT_FIRST_INSTANCE + | wgpu::Features::MAPPABLE_PRIMARY_BUFFERS)), required_limits: limits.clone(), ..Default::default() }) @@ -739,6 +747,10 @@ impl Renderer { entry(5, vf, sprite_array), entry(6, vf, sprite_array), entry(7, vf, sprite_array), + entry(8, vf, sprite_array), + entry(9, vf, sprite_array), + entry(10, vf, sprite_array), + entry(11, vf, sprite_array), ], }); let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { @@ -1921,11 +1933,22 @@ impl Renderer { self.indices = Self::quad_indices(&self.device, self.index_quads); } let vertices = (mesh.solid.len() + mesh.translucent.len()) as u64; - let (page, start) = self.arena.alloc(&self.device, &self.queue, vertices); - let sorted_indices = (translucent > 0).then(|| { - self.sorted_indices - .alloc(&self.device, &self.queue, u64::from(translucent) * 6) - }); + // With video and system memory both full, the section is not drawn. + let Some((page, start)) = self.arena.alloc(&self.device, &self.queue, vertices) else { + return; + }; + let sorted_indices = if translucent > 0 { + let indices = + self.sorted_indices + .alloc(&self.device, &self.queue, u64::from(translucent) * 6); + if indices.is_none() { + self.arena.release(page, start, vertices); + return; + } + indices + } else { + None + }; let stride = std::mem::size_of::() as u64; for (offset, part) in [(0, &mesh.solid), (mesh.solid.len(), &mesh.translucent)] { if !part.is_empty() { @@ -2181,6 +2204,8 @@ impl Renderer { ); } pub fn render(&mut self, frame: &Frame) -> Result { + self.arena.trim(); + self.sorted_indices.trim(); if self.suspended { return Ok(false); } @@ -3490,13 +3515,39 @@ fn mix_frames(current: &mut [u8], next: &[u8], millis: u32) { let t = millis as f32 / 1000.0; for (c, n) in current.iter_mut().zip(next) { let (a, b) = (f32::from(*c) / 255.0, f32::from(*n) / 255.0); - *c = ((a * (1.0 - t) + b * t) * 255.0).round() as u8; + *c = round_unorm8((a * (1.0 - t) + b * t) * 255.0); } } +/// `x.round() as u8` for `x >= 0`, without the `roundf` library call that +/// x86-64 makes for every `f32::round` (it has no rounding instruction before +/// SSE4.1): the fraction `x - trunc(x)` is exact below 2^23, so rounding half +/// away from zero is adding one when it is at least a half. +#[inline] +fn round_unorm8(x: f32) -> u8 { + let whole = x as u8; + whole.saturating_add(u8::from(x - f32::from(whole) >= 0.5)) +} #[cfg(test)] mod sprite_upload_tests { use super::*; #[test] + fn blended_frames_round_like_f32_round() { + for millis in (0..1000).step_by(7).chain([999]) { + let t = millis as f32 / 1000.0; + for a in 0..=255u8 { + for b in 0..=255u8 { + let x = (f32::from(a) / 255.0 * (1.0 - t) + f32::from(b) / 255.0 * t) * 255.0; + assert_eq!(round_unorm8(x), x.round() as u8, "{a} {b} {millis}"); + } + } + } + for x in [ + 0.0, 0.49999997, 0.5, 1.5, 2.5, 254.49998, 254.5, 255.0, 255.00002, 300.0, + ] { + assert_eq!(round_unorm8(x), x.round() as u8, "{x}"); + } + } + #[test] fn real_rail_mips_do_not_change_with_larger_array_layers() { let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) .join("../../.local/horizon-client/26.3"); diff --git a/crates/horizon-client/src/gfx/arena.rs b/crates/horizon-client/src/gfx/arena.rs index 233333e04..8becb6499 100644 --- a/crates/horizon-client/src/gfx/arena.rs +++ b/crates/horizon-client/src/gfx/arena.rs @@ -49,10 +49,16 @@ struct Page { /// Terrain vertices in pages of at most `page_limit` vertices (devices cap /// buffer sizes): a section's vertices sit in one page. pub struct Arena { - pages: Vec, + pages: Vec>, stride: u64, page_limit: u64, + initial_capacity: u64, usage: wgpu::BufferUsages, + /// A page had to go to system memory (logged once). + spilled: bool, + /// Tests: GPU-local pages fail as if video memory were full. + #[cfg(test)] + video_memory_full: bool, } impl Arena { @@ -73,27 +79,49 @@ impl Arena { let page_limit = (bytes / stride) / GRAIN * GRAIN; let capacity = capacity.next_multiple_of(GRAIN).min(page_limit); Self { - pages: vec![Page { - buffer: Self::create(device, stride, capacity, usage), - capacity, - free: FreeList::new(capacity), - }], + pages: Vec::new(), stride, page_limit, + initial_capacity: capacity, usage, + spilled: false, + #[cfg(test)] + video_memory_full: false, } } - fn create( - device: &wgpu::Device, - stride: u64, - capacity: u64, - usage: wgpu::BufferUsages, - ) -> wgpu::Buffer { - device.create_buffer(&wgpu::BufferDescriptor { - label: Some("Section buffer arena"), - size: capacity * stride, - usage: usage | wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC, - mapped_at_creation: false, + /// A buffer of `capacity` vertices in video memory or, when that is full + /// and the device allows it, in system memory: Vulkan drivers fail an + /// allocation rather than page like OpenGL ones, and wgpu's default error + /// handler panics. `None` when neither has room. + fn create(&mut self, device: &wgpu::Device, capacity: u64) -> Option { + let usage = self.usage | wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC; + #[cfg(test)] + let local = (!self.video_memory_full) + .then(|| try_create(device, capacity * self.stride, usage)) + .flatten(); + #[cfg(not(test))] + let local = try_create(device, capacity * self.stride, usage); + local.or_else(|| { + // Mappable buffers are placed in host-visible memory, which the + // allocator takes from system memory once video memory is full. + if !device + .features() + .contains(wgpu::Features::MAPPABLE_PRIMARY_BUFFERS) + { + return None; + } + let buffer = try_create( + device, + capacity * self.stride, + usage | wgpu::BufferUsages::MAP_WRITE, + )?; + if !std::mem::replace(&mut self.spilled, true) { + log::warn!( + "Horizon: video memory is full; more terrain is kept in system memory \ + (lower the render distance if frames slow down)" + ); + } + Some(buffer) }) } /// The rounded length a request of `vertices` occupies. @@ -102,55 +130,97 @@ impl Arena { } /// Bytes of every page's buffer. pub fn bytes(&self) -> u64 { - self.pages.iter().map(|p| p.capacity).sum::() * self.stride + self.pages.iter().flatten().map(|p| p.capacity).sum::() * self.stride } pub fn buffer(&self, page: u32) -> &wgpu::Buffer { - &self.pages[page as usize].buffer + &self.pages[page as usize] + .as_ref() + .expect("live arena page") + .buffer } /// A range of `vertices`: (page, first vertex). Grows the newest page /// (copying its contents on the GPU) or starts a new one when nothing - /// free is large enough. + /// free is large enough; `None` when no memory is left for it. pub fn alloc( &mut self, device: &wgpu::Device, queue: &wgpu::Queue, vertices: u64, - ) -> (u32, u64) { + ) -> Option<(u32, u64)> { let len = Self::span(vertices).min(self.page_limit); for (i, page) in self.pages.iter_mut().enumerate() { - if let Some(start) = page.free.take(len) { - return (i as u32, start); + if let Some(page) = page + && let Some(start) = page.free.take(len) + { + return Some((i as u32, start)); } } - let last = self.pages.len() - 1; - let current = self.pages[last].capacity; - if current < self.page_limit { + let last = self.pages.len().saturating_sub(1); + if let Some(page) = self.pages.last().and_then(Option::as_ref) + && page.capacity < self.page_limit + { + let current = page.capacity; let grown = (current * 2).max(current + len).min(self.page_limit); - if grown - current >= len || grown == self.page_limit { - self.grow(device, queue, last, grown); - if let Some(start) = self.pages[last].free.take(len) { - return (last as u32, start); - } + if (grown - current >= len || grown == self.page_limit) + && self.grow(device, queue, last, grown) + && let Some(start) = self.pages[last].as_mut().unwrap().free.take(len) + { + return Some((last as u32, start)); } } - let capacity = (1 << 20u64).max(len).min(self.page_limit); + let capacity = if self.pages.is_empty() { + self.initial_capacity + } else { + 1 << 20 + } + .max(len) + .min(self.page_limit); + // A page just large enough when memory is short. + let (buffer, capacity) = match self.create(device, capacity) { + Some(buffer) => (buffer, capacity), + None if capacity > len => (self.create(device, len)?, len), + None => return None, + }; let mut free = FreeList::new(capacity); let start = free.take(len).expect("a new page fits one section"); - self.pages.push(Page { - buffer: Self::create(device, self.stride, capacity, self.usage), + let page = Page { + buffer, capacity, free, - }); - ((self.pages.len() - 1) as u32, start) + }; + let index = self + .pages + .iter() + .position(Option::is_none) + .unwrap_or_else(|| { + self.pages.push(None); + self.pages.len() - 1 + }); + self.pages[index] = Some(page); + Some((index as u32, start)) } pub fn release(&mut self, page: u32, start: u64, vertices: u64) { let len = Self::span(vertices).min(self.page_limit); - self.pages[page as usize].free.give(start, len); + self.pages[page as usize] + .as_mut() + .expect("live arena page") + .free + .give(start, len); } - fn grow(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, page: usize, capacity: u64) { + /// Moves a page into a larger buffer; false (the page unchanged) when + /// there is no memory for it. + fn grow( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + page: usize, + capacity: u64, + ) -> bool { let capacity = capacity.next_multiple_of(GRAIN).min(self.page_limit); - let buffer = Self::create(device, self.stride, capacity, self.usage); - let old = &mut self.pages[page]; + let Some(buffer) = self.create(device, capacity) else { + return false; + }; + let old = self.pages[page].as_mut().expect("live arena page"); let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("Grow section arena"), }); @@ -161,18 +231,124 @@ impl Arena { old.buffer = buffer; old.capacity = capacity; old.free.give(previous, capacity - previous); + true } - pub fn clear(&mut self) { + /// Reclaim after an upload batch, so replacing a section can reuse its range. + pub fn trim(&mut self) { for page in &mut self.pages { - page.free = FreeList::new(page.capacity); + if let Some(p) = page + && p.free.0.len() == 1 + && p.free.0.get(&0) == Some(&p.capacity) + { + *page = None; + } + } + // Keep live page indices stable; later allocations reuse interior holes. + while self.pages.last().is_some_and(Option::is_none) { + self.pages.pop(); } } + pub fn clear(&mut self) { + self.pages.clear(); + } +} + +/// `create_buffer` with an out-of-memory failure returned instead of raised +/// (raised errors are fatal). +fn try_create(device: &wgpu::Device, size: u64, usage: wgpu::BufferUsages) -> Option { + let scope = device.push_error_scope(wgpu::ErrorFilter::OutOfMemory); + let buffer = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Section buffer arena"), + size, + usage, + mapped_at_creation: false, + }); + // Native error scopes resolve at once. + pollster::block_on(scope.pop()).is_none().then_some(buffer) } #[cfg(test)] mod tests { use super::*; + fn device() -> Option<(wgpu::Device, wgpu::Queue)> { + device_with(wgpu::Features::empty()) + } + + /// A device with those of `features` the adapter has. + fn device_with(features: wgpu::Features) -> 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()?; + eprintln!("arena GPU regression: {}", adapter.get_info().name); + let required_features = adapter.features() & features; + pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor { + required_features, + ..Default::default() + })) + .ok() + } + + #[test] + fn empty_pages_are_reclaimed_without_moving_live_ranges() { + let Some((device, queue)) = device() else { + eprintln!("no GPU adapter: skipped"); + return; + }; + let mut arena = Arena::new(&device, 4, GRAIN); + arena.page_limit = 2 * GRAIN; + assert_eq!(arena.bytes(), 0); + let (first, a) = arena.alloc(&device, &queue, GRAIN).unwrap(); + let (_, b) = arena.alloc(&device, &queue, GRAIN).unwrap(); + let (live, c) = arena.alloc(&device, &queue, 2 * GRAIN).unwrap(); + let buffer = arena.buffer(live).clone(); + let data = [1u8, 2, 3, 4]; + queue.write_buffer(&buffer, c * 4, &data); + assert_eq!(arena.bytes(), 4 * GRAIN * 4); + arena.release(first, a, GRAIN); + arena.trim(); + assert_eq!(arena.bytes(), 4 * GRAIN * 4); + arena.release(first, b, GRAIN); + arena.trim(); + assert_eq!(arena.bytes(), 2 * GRAIN * 4); + assert_eq!(arena.buffer(live), &buffer); + let (reused, d) = arena.alloc(&device, &queue, GRAIN).unwrap(); + assert_eq!(reused, first); + + let (tx, rx) = std::sync::mpsc::channel(); + wgpu::util::DownloadBuffer::read_buffer( + &device, + &queue, + &arena.buffer(live).slice(c * 4..c * 4 + 4), + move |result| tx.send(result.unwrap().to_vec()).unwrap(), + ); + device.poll(wgpu::PollType::wait_indefinitely()).unwrap(); + assert_eq!(rx.recv().unwrap(), data); + arena.release(reused, d, GRAIN); + arena.release(live, c, 2 * GRAIN); + arena.trim(); + assert_eq!(arena.bytes(), 0); + assert!(arena.pages.is_empty()); + } + + #[test] + fn clearing_an_arena_releases_buffers_and_can_allocate_again() { + let Some((device, queue)) = device() else { + eprintln!("no GPU adapter: skipped"); + return; + }; + for mut arena in [ + Arena::new(&device, 28, GRAIN), + Arena::indices(&device, GRAIN), + ] { + arena.alloc(&device, &queue, GRAIN).unwrap(); + assert_eq!(arena.bytes(), arena.stride * GRAIN); + arena.clear(); + assert_eq!(arena.bytes(), 0); + assert_eq!(arena.alloc(&device, &queue, GRAIN), Some((0, 0))); + assert_eq!(arena.bytes(), arena.stride * GRAIN); + } + } + #[test] fn free_ranges_coalesce() { let mut free = FreeList::new(4 * GRAIN); @@ -197,4 +373,63 @@ mod tests { free.give(a, GRAIN); assert_eq!(free.take(GRAIN), Some(a)); } + + fn read(device: &wgpu::Device, queue: &wgpu::Queue, buffer: &wgpu::Buffer, offset: u64) -> u32 { + let (tx, rx) = std::sync::mpsc::channel(); + wgpu::util::DownloadBuffer::read_buffer( + device, + queue, + &buffer.slice(offset..offset + 4), + move |result| tx.send(result.unwrap().to_vec()).unwrap(), + ); + device.poll(wgpu::PollType::wait_indefinitely()).unwrap(); + u32::from_le_bytes(rx.recv().unwrap()[..4].try_into().unwrap()) + } + + #[test] + fn full_video_memory_spills_to_system_memory() { + let Some((device, queue)) = device_with(wgpu::Features::MAPPABLE_PRIMARY_BUFFERS) else { + return; + }; + if !device + .features() + .contains(wgpu::Features::MAPPABLE_PRIMARY_BUFFERS) + { + return; + } + let mut arena = Arena::indices(&device, GRAIN); + let (page, start) = arena.alloc(&device, &queue, GRAIN).unwrap(); + queue.write_buffer(arena.buffer(page), start * 4, &7u32.to_le_bytes()); + arena.video_memory_full = true; + // The full page can't grow in video memory: it moves to system + // memory with its contents, and new ranges come from there. + let (grown, next) = arena.alloc(&device, &queue, GRAIN).unwrap(); + assert!(arena.spilled); + assert_eq!(grown, page); + assert!( + arena + .buffer(page) + .usage() + .contains(wgpu::BufferUsages::MAP_WRITE) + ); + queue.write_buffer(arena.buffer(grown), next * 4, &9u32.to_le_bytes()); + assert_eq!(read(&device, &queue, arena.buffer(page), start * 4), 7); + assert_eq!(read(&device, &queue, arena.buffer(grown), next * 4), 9); + } + + #[test] + fn no_memory_left_is_not_an_error() { + let Some((device, queue)) = device() else { + return; + }; + let mut arena = Arena::indices(&device, GRAIN); + arena.alloc(&device, &queue, GRAIN).unwrap(); + arena.video_memory_full = true; + // Without mappable primary buffers nothing can spill: the request + // fails, and the arena keeps what it has. + assert_eq!(arena.alloc(&device, &queue, GRAIN), None); + assert_eq!(arena.bytes(), GRAIN * 4); + arena.release(0, 0, GRAIN); + assert_eq!(arena.alloc(&device, &queue, GRAIN), Some((0, 0))); + } } diff --git a/crates/horizon-client/src/lan/advancements.rs b/crates/horizon-client/src/lan/advancements.rs index 0e0caf7ff..e0b8ad450 100644 --- a/crates/horizon-client/src/lan/advancements.rs +++ b/crates/horizon-client/src/lan/advancements.rs @@ -144,6 +144,13 @@ impl ServerAdvancements { ) -> Option> { let n = self.tree.list.len(); let first = visible.len() != n; + // flushDirty does nothing unless it is the first packet or progress + // changed: visibility follows progress alone, and every grant and + // revoke is in `changed`. This skips walking the whole tree for + // every player every tick. + if !first && changed.is_empty() { + return None; + } let now = self .tree .visible(&self.kids, |i| progress.done(&self.tree.list[i])); diff --git a/crates/horizon-client/src/lan/chunks.rs b/crates/horizon-client/src/lan/chunks.rs index 99b87a20e..04eb9f224 100644 --- a/crates/horizon-client/src/lan/chunks.rs +++ b/crates/horizon-client/src/lan/chunks.rs @@ -4,6 +4,7 @@ use crate::{ chunk::{Chunk, Light, MIN_Y, SECTIONS, Section}, containers::BlockEntity, + fast_hash::FxHashMap, }; use horizon_protocol::{ buf::set_bit, @@ -90,12 +91,82 @@ pub struct ChunkEncoder { pub block_ids: Vec, /// The save's tables, for the heightmaps. pub tables: Option>, + /// Sections already looked at, by address (see `Encoded`). + cache: std::sync::Mutex>, + /// Each column's heightmaps, with the sections they were made from. + heights: std::sync::Mutex>, /// The level's sections among a column's: all of them in the Overworld, /// y 0 to 255 in the Nether and the End (`DimensionType` min_y and height). sections: std::ops::Range, /// Whether the level has sky light (`DimensionType.hasSkyLight`). sky: bool, } + +/// A column's client heightmaps, valid while it holds the same sections. +struct Heights { + sections: Vec>, + maps: Vec<(i32, Vec)>, +} + +/// What a resident section gives the wire, kept while the section is +/// unchanged. Resident sections are immutable behind `Arc` (edits go through +/// `Arc::make_mut`, which moves an edited section to a new address while a +/// `Weak` points at the old one), and the `Weak` keeps the old address from +/// being reused: the same address is the same, unchanged section. +struct Encoded { + section: std::sync::Weak
, + non_air: bool, + /// LevelChunkSection.write, and its block entity blocks. + wire: Option<(Arc<[u8]>, Entities)>, + /// The section's light as the wire's two DataLayers, when not uniform. + light: Option>, +} + +/// A section's blocks that have a block entity type, as (index, state) in +/// index order (y, then z, then x). +type Entities = Arc<[(u16, u32)]>; + +/// One section's light as DataLayers (two blocks per byte, the even one in +/// the low nibble), and whether each holds any light. +struct Layers { + sky: Vec, + block: Vec, + sky_any: bool, + block_any: bool, +} +impl Layers { + fn of(light: &Light) -> Self { + let (mut sky, mut block) = (vec![0u8; 2048], vec![0u8; 2048]); + let (sky_any, block_any) = match light { + Light::Uniform(v) => { + let (sk, bl) = (v >> 4, v & 15); + sky.fill(sk | sk << 4); + block.fill(bl | bl << 4); + (sk != 0, bl != 0) + } + Light::Full(a) => { + for ((pair, sky), block) in + a.as_chunks::<2>().0.iter().zip(&mut sky).zip(&mut block) + { + let [even, odd] = *pair; + *sky = even >> 4 | (odd & 0xF0); + *block = (even & 15) | odd << 4; + } + let all = a.iter().fold(0u8, |acc, &v| acc | v); + (all & 0xF0 != 0, all & 15 != 0) + } + }; + Self { + sky, + block, + sky_any, + block_any, + } + } +} + +/// Sections kept before the ones no longer resident are dropped. +const CACHE_SWEEP: usize = 1 << 15; impl ChunkEncoder { /// `biome_names`: the internal biome list in order; `biome_registry`: /// the synchronized biome entries in network order. @@ -148,10 +219,101 @@ impl ChunkEncoder { biome_bits: ceil_log2(biome_registry.len()), block_ids, tables: None, + cache: Default::default(), + heights: Default::default(), sections: 0..SECTIONS, sky: true, } } + + /// The client heightmaps (Heightmap.prime over the column), from the + /// cache while the column's sections are unchanged. + fn heightmaps(&self, chunk: &Chunk, tables: &crate::save::SaveTables) -> Vec<(i32, Vec)> { + let same = |h: &Heights| { + h.sections.len() == chunk.sections.len() + && h.sections + .iter() + .zip(&chunk.sections) + .all(|(w, s)| std::ptr::eq(w.as_ptr(), Arc::as_ptr(s))) + }; + let mut heights = self.heights.lock().unwrap(); + if let Some(h) = heights.get(&(chunk.x, chunk.z)) + && same(h) + { + return h.maps.clone(); + } + if heights.len() >= CACHE_SWEEP / 4 { + heights.retain(|_, h| h.sections.iter().all(|w| w.strong_count() > 0)); + if heights.len() >= CACHE_SWEEP / 8 { + heights.clear(); + } + } + let min_y = MIN_Y + 16 * self.sections.start as i32; + let height = 16 * self.sections.len() as i32; + let maps = crate::save::client_heightmaps_in(chunk, tables, min_y, height); + heights.insert( + (chunk.x, chunk.z), + Heights { + sections: chunk.sections.iter().map(Arc::downgrade).collect(), + maps: maps.clone(), + }, + ); + maps + } + + /// The cached entry for a section, made with `non_air` when missing. + fn cached(&self, section: &Arc
, read: impl FnOnce(&mut Encoded) -> T) -> T { + read(self.entry(&mut self.cache.lock().unwrap(), section)) + } + + /// A section's entry in the locked cache, made with `non_air` when missing. + fn entry<'a>( + &self, + cache: &'a mut FxHashMap, + section: &Arc
, + ) -> &'a mut Encoded { + if cache.len() >= CACHE_SWEEP { + cache.retain(|_, e| e.section.strong_count() > 0); + if cache.len() >= CACHE_SWEEP / 2 { + cache.clear(); + } + } + cache + .entry(Arc::as_ptr(section) as usize) + .or_insert_with(|| Encoded { + section: Arc::downgrade(section), + non_air: self.non_air(section), + wire: None, + light: None, + }) + } + + /// LevelChunkSection.hasOnlyAir, negated. + fn non_air(&self, section: &Section) -> bool { + let non_air = |v: u32| self.flags.get(v as usize).copied().unwrap_or(0) & NON_AIR != 0; + // The palette first: most sections are all air, or one block. + if !section.blocks.maybe_has(non_air) { + false + } else if let Some(v) = section.blocks.uniform() { + non_air(v) + } else { + // Stops at the first block, usually near the bottom. + (0..BLOCKS).any(|i| non_air(section.blocks.get(i))) + } + } + + /// A section on the wire, from the cache or encoded now. + fn section_wire(&self, section: &Arc
) -> (Arc<[u8]>, Entities) { + if let Some(wire) = self.cached(section, |e| e.wire.clone()) { + return wire; + } + let (encoded, entities) = self.section(section); + let mut w = horizon_protocol::buf::Writer::new(); + encoded.write(&mut w); + let wire: (Arc<[u8]>, Entities) = (w.data.into(), entities.into()); + self.cached(section, |e| e.wire = Some(wire.clone())); + wire + } /// Encode for a level of `min_y` and `height` (multiples of 16 within /// the column), with or without sky light. pub fn set_dimension(&mut self, min_y: i32, height: i32, sky: bool) { @@ -161,24 +323,26 @@ impl ChunkEncoder { let n = usize::try_from(height >> 4).unwrap_or(SECTIONS); self.sections = lo..(lo + n).min(SECTIONS); self.sky = sky; + // The heightmaps depend on the level's height. + self.heights.get_mut().unwrap().clear(); } - pub fn entity_type(&self, state: u16) -> Option { + pub fn entity_type(&self, state: u32) -> Option { self.entity_types - .get(usize::from(state)) + .get(state as usize) .filter(|t| **t >= 0) .map(|t| i32::from(*t)) } - pub fn is_air(&self, state: u16) -> bool { + pub fn is_air(&self, state: u32) -> bool { self.flags - .get(usize::from(state)) + .get(state as usize) .is_none_or(|f| f & NON_AIR == 0) } - fn section(&self, section: &Section) -> (horizon_protocol::chunk::Section, bool) { + fn section(&self, section: &Section) -> (horizon_protocol::chunk::Section, Vec<(u16, u32)>) { let mut values = Vec::with_capacity(BLOCKS); - let (mut non_empty, mut fluids, mut entities) = (0i16, 0i16, false); - let mut count = |state: u16, n: i16| { - let f = self.flags.get(usize::from(state)).copied().unwrap_or(0); + let (mut non_empty, mut fluids, mut entities) = (0i16, 0i16, Vec::new()); + let mut count = |state: u32, n: i16| { + let f = self.flags.get(state as usize).copied().unwrap_or(0); if f & NON_AIR != 0 { non_empty += n; if f & FLUID != 0 { @@ -187,11 +351,21 @@ impl ChunkEncoder { } f & ENTITY != 0 }; - let mut unpacked = [0u16; BLOCKS]; - section.unpack(&mut unpacked); - for &v in &unpacked { - entities |= count(v, 1); - values.push(i32::from(v)); + // One block everywhere (all air, mostly): what from_values would give. + let uniform = section.blocks.uniform(); + if let Some(v) = uniform { + if count(v, BLOCKS as i16) { + entities = (0..BLOCKS as u16).map(|i| (i, v)).collect(); + } + } else { + let mut unpacked = [0u32; BLOCKS]; + section.unpack(&mut unpacked); + for (i, &v) in unpacked.iter().enumerate() { + if count(v, 1) { + entities.push((i as u16, v)); + } + values.push(v as i32); + } } let biome_values: Vec = (0..BIOMES) .map(|j| { @@ -206,7 +380,18 @@ impl ChunkEncoder { horizon_protocol::chunk::Section { non_empty, fluids, - states: Paletted::from_values(&values, Strategy::Blocks, 16), + states: match uniform { + Some(v) => Paletted { + bits: 0, + palette: vec![v as i32], + data: Vec::new(), + }, + None => Paletted::from_values( + &values, + Strategy::Blocks, + crate::chunk::block_strategy().global_bits, + ), + }, biomes: Paletted::from_values(&biome_values, Strategy::Biomes, self.biome_bits), }, entities, @@ -217,13 +402,10 @@ impl ChunkEncoder { /// The non-air sections of a column as bits (LevelChunkSection.hasOnlyAir). fn occupied(&self, chunk: &Chunk) -> u64 { let mut bits = 0u64; + // One lock for the column: encode_in asks for nine columns' sections. + let mut cache = self.cache.lock().unwrap(); for (i, s) in chunk.sections[self.sections.clone()].iter().enumerate() { - let mut unpacked = [0u16; BLOCKS]; - s.unpack(&mut unpacked); - let any = unpacked - .iter() - .any(|&v| self.flags.get(usize::from(v)).copied().unwrap_or(0) & NON_AIR != 0); - if any { + if self.entry(&mut cache, s).non_air { bits |= 1 << i; } } @@ -241,8 +423,8 @@ impl ChunkEncoder { } } } - let mut packet = self.encode(chunk, &world.block_entities); - packet.light = self.light(chunk, around); + let mut packet = self.encode_blocks(chunk, &world.block_entities); + packet.light = self.light_data(chunk, around); packet } @@ -251,7 +433,18 @@ impl ChunkEncoder { chunk: &Chunk, block_entities: &HashMap<(i32, i32, i32), BlockEntity>, ) -> LevelChunkWithLight { - let mut sections = Vec::with_capacity(self.sections.len()); + let mut packet = self.encode_blocks(chunk, block_entities); + packet.light = self.light_data(chunk, u64::MAX); + packet + } + + /// The packet without its light. + fn encode_blocks( + &self, + chunk: &Chunk, + block_entities: &HashMap<(i32, i32, i32), BlockEntity>, + ) -> LevelChunkWithLight { + let mut buffer = Vec::new(); let mut entity_infos = Vec::new(); for (i, s) in chunk .sections @@ -260,31 +453,32 @@ impl ChunkEncoder { .skip(self.sections.start) .take(self.sections.len()) { - let (encoded, has_entities) = self.section(s); - if has_entities { - for y in 0..16 { - for z in 0..16 { - for x in 0..16 { - let state = s.get(crate::chunk::index(x, y, z)); - if let Some(kind) = self.entity_type(state) { - let wy = MIN_Y + (i as i32) * 16 + y; - let pos = (chunk.x * 16 + x, wy, chunk.z * 16 + z); - entity_infos.push(BlockEntityInfo { - packed_xz: ((x << 4) | z) as i8, - y: wy as i16, - kind, - // BlockEntityInfo.create: an empty update tag is not sent. - tag: block_entities - .get(&pos) - .and_then(entity_tag) - .unwrap_or(Tag::End), - }); - } - } - } - } + let (encoded, entities) = self.section_wire(s); + // The section's block entity blocks, kept with its bytes, in the + // order a scan of every block would find them. + for &(index, state) in entities.iter() { + let Some(kind) = self.entity_type(state) else { + continue; + }; + let (x, y, z) = ( + i32::from(index & 15), + i32::from(index >> 8), + i32::from(index >> 4 & 15), + ); + let wy = MIN_Y + (i as i32) * 16 + y; + let pos = (chunk.x * 16 + x, wy, chunk.z * 16 + z); + entity_infos.push(BlockEntityInfo { + packed_xz: ((x << 4) | z) as i8, + y: wy as i16, + kind, + // BlockEntityInfo.create: an empty update tag is not sent. + tag: block_entities + .get(&pos) + .and_then(entity_tag) + .unwrap_or(Tag::End), + }); } - sections.push(encoded); + buffer.extend_from_slice(&encoded); } // A loaded chunk's block entities enter its maps in the save's order // (ChunkAccess.pendingBlockEntities, then LevelChunk.blockEntities): @@ -316,22 +510,38 @@ impl ChunkEncoder { heightmaps: self .tables .as_ref() - .map(|t| { - let min_y = MIN_Y + 16 * self.sections.start as i32; - let height = 16 * self.sections.len() as i32; - crate::save::client_heightmaps_in(chunk, t, min_y, height) - }) + .map(|t| self.heightmaps(chunk, t)) .unwrap_or_default(), - buffer: ChunkData::from_sections(§ions), + buffer, block_entities: entity_infos, }, - light: self.light(chunk, u64::MAX), + light: LightData { + sky_mask: Vec::new(), + block_mask: Vec::new(), + empty_sky_mask: Vec::new(), + empty_block_mask: Vec::new(), + sky: Vec::new(), + block: Vec::new(), + }, } } - /// The light layers of the level's sections. - fn light(&self, chunk: &Chunk, around: u64) -> LightData { - let mut data = light_data(&chunk.sections[self.sections.clone()], around); + /// ClientboundLightUpdatePacketData: the light layers of the sections + /// that exist around the column (`around`: the non-air sections of it + /// and its neighbours, as bits). A resident section's layers come from + /// the cache while it is unchanged (uniform light is quicker to fill than + /// to keep). + fn light_data(&self, chunk: &Chunk, around: u64) -> LightData { + let mut data = light_data_with(&chunk.sections[self.sections.clone()], around, |s| { + if matches!(s.light, Light::Uniform(_)) { + return Arc::new(Layers::of(&s.light)); + } + self.cached(s, |e| e.light.clone()).unwrap_or_else(|| { + let layers = Arc::new(Layers::of(&s.light)); + self.cached(s, |e| e.light = Some(layers.clone())); + layers + }) + }); if !self.sky { // No sky light storage at all. data.sky_mask.clear(); @@ -412,7 +622,17 @@ pub fn entity_tag(entity: &BlockEntity) -> Option { /// non-air section of this column or a neighbouring one (`around`, bit i for /// section i; LayerLightSectionStorage.updateSectionStatus). An existing /// layer is sent, or marked empty when all dark; the others are left out. +#[cfg(test)] fn light_data(sections: &[Arc
], around: u64) -> LightData { + light_data_with(sections, around, |s| Arc::new(Layers::of(&s.light))) +} + +/// The light layers, `section` giving a resident section's. +fn light_data_with( + sections: &[Arc
], + around: u64, + section: impl Fn(&Arc
) -> Arc, +) -> LightData { let n = sections.len(); let mut data = LightData { sky_mask: Vec::new(), @@ -425,52 +645,25 @@ fn light_data(sections: &[Arc
], around: u64) -> LightData { // Light section b holds chunk section b - 1. let exists = |b: usize| (b.saturating_sub(2)..=b.min(n - 1)).any(|s| s + 2 >= b && around >> s & 1 != 0); - let full_sky = Light::Uniform(0xF0); for b in 0..n + 2 { if !exists(b) { continue; } - let light = match b { + let layers = match b { // Below the world nothing is lit; above it the sky is full. - 0 => &Light::Uniform(0), - _ if b == n + 1 => &full_sky, - _ => §ions[b - 1].light, + 0 => Arc::new(Layers::of(&Light::Uniform(0))), + _ if b == n + 1 => Arc::new(Layers::of(&Light::Uniform(0xF0))), + _ => section(§ions[b - 1]), }; - let (mut sky, mut block) = (vec![0u8; 2048], vec![0u8; 2048]); - let (mut sky_any, mut block_any) = (false, false); - match light { - Light::Uniform(v) => { - let (sk, bl) = (v >> 4, v & 15); - sky_any = sk != 0; - block_any = bl != 0; - let (sb, bb) = (sk | sk << 4, bl | bl << 4); - sky.fill(sb); - block.fill(bb); - } - Light::Full(a) => { - for (j, &v) in a.iter().enumerate() { - let (sk, bl) = (v >> 4, v & 15); - sky_any |= sk != 0; - block_any |= bl != 0; - if j & 1 == 0 { - sky[j >> 1] |= sk; - block[j >> 1] |= bl; - } else { - sky[j >> 1] |= sk << 4; - block[j >> 1] |= bl << 4; - } - } - } - } - if sky_any { + if layers.sky_any { set_bit(&mut data.sky_mask, b); - data.sky.push(sky); + data.sky.push(layers.sky.clone()); } else { set_bit(&mut data.empty_sky_mask, b); } - if block_any { + if layers.block_any { set_bit(&mut data.block_mask, b); - data.block.push(block); + data.block.push(layers.block.clone()); } else { set_bit(&mut data.empty_block_mask, b); } @@ -499,6 +692,149 @@ mod tests { assert_eq!(block_entity_type("minecraft:stone"), None); } + #[test] + fn block_entities_come_from_the_cached_section_in_scan_order() { + let builtin = Builtin::parse( + r#"{"minecraft:block_entity_type": {"entries": { + "minecraft:chest": {"protocol_id": 1}, + "minecraft:sign": {"protocol_id": 7}}}}"#, + ) + .unwrap(); + let states = [ + "minecraft:air", + "minecraft:stone", + "minecraft:chest", + "minecraft:oak_sign", + ] + .map(|n| (Arc::::from(n), false)); + let encoder = ChunkEncoder::new(states, &builtin, &[], &[]); + let mut chunk = Chunk::empty(2, -3); + for (x, y, z, state) in [(15, 3, 0, 2), (0, 3, 15, 3), (4, 3, 4, 1), (7, 40, 2, 2)] { + chunk.set(x, MIN_Y + y, z, state); + } + // Every block of a section looked at, as the scan did before the cache. + let scan = |chunk: &Chunk| { + let mut found = Vec::new(); + for (i, s) in chunk.sections.iter().enumerate() { + for y in 0..16 { + for z in 0..16 { + for x in 0..16 { + let state = s.get(crate::chunk::index(x, y, z)); + if let Some(kind) = encoder.entity_type(state) { + found.push((x, MIN_Y + i as i32 * 16 + y, z, kind)); + } + } + } + } + } + // Then the order the packet gives them, as encode_blocks does. + java_hash_order(&mut found, |e| { + (chunk.x * 16 + e.0, e.1, chunk.z * 16 + e.2) + }); + found + }; + let sent = |chunk: &Chunk| { + let packet = encoder.encode(chunk, &HashMap::new()); + let infos = packet.chunk.block_entities; + infos + .iter() + .map(|e| { + let xz = e.packed_xz as u8; + ( + i32::from(xz >> 4), + i32::from(e.y), + i32::from(xz & 15), + e.kind, + ) + }) + .collect::>() + }; + let expected = scan(&chunk); + assert_eq!(expected.len(), 3); + assert_eq!(sent(&chunk), expected); + // From the cache the second time, and after an edit. + assert_eq!(sent(&chunk), expected); + chunk.set(15, MIN_Y + 3, 0, 0); + chunk.set(9, MIN_Y + 3, 9, 3); + assert_eq!(sent(&chunk), scan(&chunk)); + } + + fn encoder() -> ChunkEncoder { + let states = ["minecraft:air", "minecraft:stone", "minecraft:water"] + .map(|n| (Arc::::from(n), n.ends_with("water"))); + ChunkEncoder::new(states, &Builtin::default(), &[], &[]) + } + + /// The bytes on the wire for a section, encoded afresh. + fn fresh(encoder: &ChunkEncoder, section: &Section) -> Vec { + let mut w = horizon_protocol::buf::Writer::new(); + encoder.section(section).0.write(&mut w); + w.data + } + + #[test] + fn cached_sections_follow_edits() { + let encoder = encoder(); + let mut chunk = Chunk::empty(0, 0); + chunk.set(1, MIN_Y + 2, 3, 1); + let before = encoder.encode(&chunk, &HashMap::new()); + assert_eq!(encoder.occupied(&chunk), 1); + assert_eq!( + &*encoder.section_wire(&chunk.sections[0]).0, + fresh(&encoder, &chunk.sections[0]) + ); + // An edit moves the section: the cache sees a new one. + chunk.set(4, MIN_Y + 20, 4, 2); + chunk.set(1, MIN_Y + 2, 3, 0); + assert_eq!(encoder.occupied(&chunk), 2); + let after = encoder.encode(&chunk, &HashMap::new()); + assert_ne!(before.chunk.buffer, after.chunk.buffer); + let mut expected = Vec::new(); + for s in &chunk.sections { + expected.extend(fresh(&encoder, s)); + } + assert_eq!(after.chunk.buffer, expected); + } + + #[test] + fn light_layers_pack_two_blocks_per_byte() { + let mut chunk = Chunk::empty(0, 0); + let mut light = Box::new([0u8; 4096]); + for (i, v) in light.iter_mut().enumerate() { + *v = (i * 37 % 251) as u8; + } + Arc::make_mut(&mut chunk.sections[3]).light = Light::Full(light.clone()); + let data = light_data(&chunk.sections, u64::MAX); + // Layer 4 holds section 3. Below the world (layer 0) there is no sky + // light, so the sky layers listed start at layer 1; section 3 has the + // only block light. + let (sky, block) = (&data.sky[3], &data.block[0]); + for j in 0..4096 { + let (s, b) = if j & 1 == 0 { + (sky[j >> 1] & 15, block[j >> 1] & 15) + } else { + (sky[j >> 1] >> 4, block[j >> 1] >> 4) + }; + assert_eq!((s, b), (light[j] >> 4, light[j] & 15), "block {j}"); + } + } + + #[test] + fn cached_light_follows_edits() { + let encoder = encoder(); + let mut chunk = Chunk::empty(0, 0); + let mut light = Box::new([0x5Au8; 4096]); + Arc::make_mut(&mut chunk.sections[2]).light = Light::Full(light.clone()); + let first = encoder.encode(&chunk, &HashMap::new()).light; + assert_eq!(first, light_data(&chunk.sections, u64::MAX)); + // Relighting moves the section, so the cached layers aren't reused. + light[7] = 0xF3; + Arc::make_mut(&mut chunk.sections[2]).light = Light::Full(light); + let second = encoder.encode(&chunk, &HashMap::new()).light; + assert_ne!(first, second); + assert_eq!(second, light_data(&chunk.sections, u64::MAX)); + } + #[test] fn light_masks_cover_the_world_plus_borders() { let chunk = Chunk::empty(0, 0); diff --git a/crates/horizon-client/src/lan/containers.rs b/crates/horizon-client/src/lan/containers.rs index 0fa47510a..04c57afd2 100644 --- a/crates/horizon-client/src/lan/containers.rs +++ b/crates/horizon-client/src/lan/containers.rs @@ -79,11 +79,11 @@ impl Hub { } /// ClientboundBlockEventPacket (`PlayerList.broadcast` within 64 blocks). - pub fn block_event(&self, pos: [i32; 3], b0: i32, b1: i32, state: u16) { + pub fn block_event(&self, pos: [i32; 3], b0: i32, b1: i32, state: u32) { let block = self .encoder .block_ids - .get(usize::from(state)) + .get(state as usize) .copied() .unwrap_or(0); let at = [f64::from(pos[0]), f64::from(pos[1]), f64::from(pos[2])]; diff --git a/crates/horizon-client/src/lan/entity.rs b/crates/horizon-client/src/lan/entity.rs index 3b926b372..a37010d79 100644 --- a/crates/horizon-client/src/lan/entity.rs +++ b/crates/horizon-client/src/lan/entity.rs @@ -58,7 +58,7 @@ struct Sent { #[derive(Default)] pub struct Tracker { - sent: std::collections::HashMap, + sent: crate::fast_hash::FxHashMap, } /// VecDeltaCodec.encode. @@ -66,11 +66,11 @@ fn encode(v: f64) -> i64 { (v * 4096.0).round() as i64 } -/// The horizontal range within which a client sees an entity of `kind` -/// (TrackedEntity.updatePlayer: the type's range, capped by the view distance). -pub fn visible_range(kind: &str, view_distance: i32) -> f64 { - let chunks = i32::from(tracking::settings(kind).0); - f64::from(chunks.min(view_distance) * 16) +/// The type's tracking range in chunks: a client sees an entity of `kind` +/// within it, capped by the view distance, horizontally +/// (TrackedEntity.updatePlayer). +pub fn tracking_chunks(kind: &str) -> i32 { + i32::from(tracking::settings(kind).0) } impl Tracker { diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index db426ca21..b0ecf772f 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -8,7 +8,7 @@ use horizon_protocol::{ packets::{configuration::ClientInformation, play}, }; use std::{ - collections::{HashSet, VecDeque}, + collections::VecDeque, sync::{Arc, atomic::AtomicI32}, }; @@ -73,15 +73,31 @@ pub struct Guest { /// the client accepts it (ServerGamePacketListenerImpl.awaitingPositionFromClient). pub awaiting_teleport: Option<(i32, [f64; 3])>, pub teleport_counter: i32, + /// The hub tick the pending teleport was (re)sent on (awaitingTeleportTime). + pub teleport_tick: u64, + /// Where the player was when this tick's packets began (firstGoodX/Y/Z), + /// for the moved-too-quickly check. + pub first_good: [f64; 3], + /// Position moves received in all and by the start of this tick + /// (receivedMovePacketCount, knownMovePacketCount). + pub received_moves: i32, + pub known_moves: i32, + /// A position move arrived since the client's last ClientTickEnd + /// (receivedPositionThisTick): a second one is a protocol error. + pub position_this_tick: bool, /// The client finished loading terrain (ServerboundPlayerLoadedPacket). pub loaded: bool, // Chunks. pub center: Option<(i32, i32)>, - pub sent_chunks: HashSet<(i32, i32)>, + pub sent_chunks: crate::fast_hash::FxHashSet<(i32, i32)>, pub chunk_queue: Vec<(i32, i32)>, + /// PlayerChunkSender: batches sent but not yet acknowledged, how many + /// may be (1 until the client's first acknowledgement, then 10), the + /// rate the client asks for and the chunks the next batch may hold. pub unacknowledged: u32, + pub max_unacknowledged: u32, pub chunks_per_tick: f32, - pub chunk_credit: f32, + pub batch_quota: f32, // Entities. pub tracker: Tracker, // Player state the gameplay code reads and writes. @@ -187,13 +203,19 @@ impl Guest { sleeping: false, awaiting_teleport: None, teleport_counter: 0, + teleport_tick: 0, + first_good: position, + received_moves: 0, + known_moves: 0, + position_this_tick: false, loaded: false, center: None, - sent_chunks: HashSet::new(), + sent_chunks: Default::default(), chunk_queue: Vec::new(), unacknowledged: 0, + max_unacknowledged: 1, chunks_per_tick: 9.0, - chunk_credit: 0.0, + batch_quota: 0.0, tracker: Tracker::default(), motion: None, inventory: vec![None; 36], diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index 83f745dd4..4eb9d5d5a 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -12,17 +12,24 @@ use super::{ guest::{Guest, SentEffect, stack_to_network}, }; use crate::{ + collisions::SceneCollisions, effects::{EffectInstance, PlayerEffects}, items::ItemStack, world::World, }; +use horizon_core::geometry::{Aabb, ShapeOp, Vec3, VoxelShape}; use horizon_protocol::ids::play::clientbound as ids; use horizon_protocol::{ entity_data::{Value, serializer}, packets::play::{self, Clientbound as C, Serverbound as S}, text::{literal, translatable}, }; -use horizon_world::nbt::{JavaString, Tag}; +use horizon_world::{ + block_collision::BlockCollisionRegistry, + block_collision_scan::BlockCollisionScan, + collision_context::CollisionContext, + nbt::{JavaString, Tag}, +}; use std::sync::{Arc, atomic::Ordering}; /// GameEvent types (ClientboundGameEventPacket). @@ -88,6 +95,9 @@ pub struct TickCtx<'a> { /// TickRateManager: the tick rate and whether the game is frozen. pub tick_rate: f32, pub tick_frozen: bool, + /// Block collisions, for ServerGamePacketListenerImpl's moved-wrongly + /// check (none: moves are not checked against blocks). + pub collision: Option<&'a BlockCollisionRegistry<'a>>, } /// What the gameplay code has to do for a guest. @@ -99,6 +109,8 @@ pub enum HostEvent { Packet(u32, Box), /// A chat line to show on the host (already broadcast to the guests). Chat(String), + /// A server log warning (ServerGamePacketListenerImpl's LOGGER.warn). + Warn(String), } pub struct Hub { @@ -108,6 +120,9 @@ pub struct Hub { pub guests: Vec, pub(super) sync: Arc, pub ticks: u64, + /// The `player_movement_check` and `elytra_movement_check` game rules, + /// set by the game before each tick. + pub movement_checks: (bool, bool), last_clock: i64, last_rate: f32, ticking: (f32, bool), @@ -199,6 +214,7 @@ impl Hub { guests: Vec::new(), sync, ticks: 0, + movement_checks: (true, true), last_clock: i64::MIN, last_rate: 0.0, ticking: (20.0, false), @@ -727,7 +743,9 @@ impl Hub { /// ServerGamePacketListenerImpl.teleport: move the client and wait for its confirmation. pub fn teleport(&mut self, i: usize, position: Option<[f64; 3]>) { + let ticks = self.ticks; let g = &mut self.guests[i]; + g.teleport_tick = ticks; if let Some(p) = position { g.position = p; g.previous_position = p; @@ -936,7 +954,9 @@ impl Hub { .iter() .map(|g| (g.uuid, g.latency.load(Ordering::Relaxed))) .collect(); - if self.ticks.is_multiple_of(100) && !latencies.is_empty() { + // PlayerList.tick: UPDATE_LATENCY once `++sendAllPlayerInfoIn > 600`, + // so on the 601st tick, then every 601. + if self.ticks.is_multiple_of(601) && !latencies.is_empty() { let entries = latencies .iter() .map(|(uuid, latency)| play::PlayerInfo { @@ -1080,15 +1100,16 @@ impl Hub { g.chunk_queue .sort_by_key(|&(x, z)| i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2)); } - // Send ready chunks, closest first, in batches. - if g.chunk_queue.is_empty() || g.unacknowledged >= 10 || self.ticks <= g.placed_at + 1 { + // PlayerChunkSender.sendNextChunks: ready chunks, closest first, in + // batches the client's acknowledgements pace. + if self.ticks <= g.placed_at + 1 || g.unacknowledged >= g.max_unacknowledged { return; } - g.chunk_credit = (g.chunk_credit + g.chunks_per_tick).min(g.chunks_per_tick.max(1.0) * 2.0); - let budget = g.chunk_credit.floor() as usize; - if budget == 0 { + g.batch_quota = (g.batch_quota + g.chunks_per_tick).min(g.chunks_per_tick.max(1.0)); + if g.batch_quota < 1.0 || g.chunk_queue.is_empty() { return; } + let budget = g.batch_quota.floor() as usize; let mut packets = Vec::new(); let mut taken = Vec::new(); for (idx, key) in g.chunk_queue.iter().enumerate() { @@ -1109,7 +1130,7 @@ impl Hub { if packets.is_empty() { return; } - g.chunk_credit -= packets.len() as f32; + g.batch_quota -= packets.len() as f32; g.send(&C::ChunkBatchStart(horizon_protocol::packets::Empty)); for p in &packets { g.send(p); @@ -1172,7 +1193,7 @@ impl Hub { let (x, y, z) = positions[0]; C::BlockUpdate(play::BlockUpdate { pos: [x, y, z], - state: i32::from(world.block(x, y, z).0 as u16), + state: world.block(x, y, z).0 as i32, }) } else { C::SectionBlocksUpdate(play::SectionBlocksUpdate { @@ -1201,7 +1222,7 @@ impl Hub { let Some(tag) = super::chunks::entity_tag(entity) else { continue; }; - let state = world.block(x, y, z).0 as u16; + let state = world.block(x, y, z).0; let Some(kind) = self.encoder.entity_type(state) else { continue; }; @@ -1360,6 +1381,12 @@ impl Hub { all.extend(ctx.entities.iter().cloned()); let by_id: std::collections::HashMap = all.iter().enumerate().map(|(i, e)| (e.id, i)).collect(); + // Each entity type's tracking range, looked up once rather than for + // every player. + let ranges: Vec = all + .iter() + .map(|e| super::entity::tracking_chunks(&e.kind)) + .collect(); for gi in 0..self.guests.len() { if !self.guests[gi].placed { continue; @@ -1369,8 +1396,9 @@ impl Hub { let origin = self.guests[gi].position; // TrackedEntity.updatePlayer: within the type's range (capped by the // view distance) horizontally, in a chunk the client has. - let visible = |g: &Guest, e: &EntityState| { - let range = super::entity::visible_range(&e.kind, view); + let visible = |g: &Guest, ei: usize| { + let e = &all[ei]; + let range = f64::from(ranges[ei].min(view) * 16); let (dx, dz) = (origin[0] - e.position[0], origin[2] - e.position[2]); let chunk = ( (e.position[0].floor() as i32) >> 4, @@ -1382,7 +1410,7 @@ impl Hub { let mut removed = Vec::new(); for id in self.guests[gi].tracker.ids() { match by_id.get(&id) { - Some(&ei) if visible(&self.guests[gi], &all[ei]) => {} + Some(&ei) if visible(&self.guests[gi], ei) => {} _ => removed.push(id), } } @@ -1392,14 +1420,14 @@ impl Hub { } self.guests[gi].send(&C::RemoveEntities(play::RemoveEntities { ids: removed })); } - for e in &all { + for (ei, e) in all.iter().enumerate() { if e.id == me { continue; } if self.guests[gi].tracker.is_tracking(e.id) { let packets = self.guests[gi].tracker.update(e); self.guests[gi].send_all(&packets); - } else if visible(&self.guests[gi], e) { + } else if visible(&self.guests[gi], ei) { let Some(kind) = self.entity_type(&e.kind) else { continue; }; @@ -1565,6 +1593,11 @@ impl Hub { } fn process_inbox(&mut self, i: usize, ctx: &mut TickCtx, events: &mut Vec) { + // ServerGamePacketListenerImpl.tickPlayer ran between the last + // tick's packets and these: resetPosition, and the moves it counted. + let g = &mut self.guests[i]; + g.first_good = g.position; + g.known_moves = g.received_moves; let mut budget = 400; while budget > 0 { let Some(packet) = self.guests[i].inbox.pop_front() else { @@ -1593,23 +1626,42 @@ impl Hub { self.guests[i].last_action_tick = self.ticks; } match packet { + // handleAcceptTeleportPacket. S::AcceptTeleportation(p) => { let g = &mut self.guests[i]; - if g.awaiting_teleport.is_some_and(|(id, _)| id == p.id) { + if let Some((id, at)) = g.awaiting_teleport + && id == p.id + { + if p.x.is_nan() + || p.y.is_nan() + || p.z.is_nan() + || !p.yaw.is_finite() + || !p.pitch.is_finite() + { + self.kick(conn, invalid_movement()); + continue; + } + g.position = at; g.awaiting_teleport = None; + let rot = (p.yaw, p.pitch); + self.position_change(i, [p.x, p.y, p.z], rot, 0, ctx, events); } } S::MovePlayerPos(p) => { - self.move_player(i, Some([p.x, p.y, p.z]), None, p.flags); + self.move_player(i, Some([p.x, p.y, p.z]), None, p.flags, ctx, events); } S::MovePlayerPosRot(p) => { - self.move_player(i, Some([p.x, p.y, p.z]), Some((p.yaw, p.pitch)), p.flags); + let (pos, rot) = ([p.x, p.y, p.z], (p.yaw, p.pitch)); + self.move_player(i, Some(pos), Some(rot), p.flags, ctx, events); } S::MovePlayerRot(p) => { - self.move_player(i, None, Some((p.yaw, p.pitch)), p.flags); + self.move_player(i, None, Some((p.yaw, p.pitch)), p.flags, ctx, events); + } + S::MovePlayerStatus(p) => { + self.move_player(i, None, None, p.flags, ctx, events); } - S::MovePlayerStatus(p) => self.move_player(i, None, None, p.flags), S::PlayerLoaded(_) => self.guests[i].loaded = true, + // PlayerChunkSender.onChunkBatchReceivedByClient. S::ChunkBatchReceived(p) => { let g = &mut self.guests[i]; g.unacknowledged = g.unacknowledged.saturating_sub(1); @@ -1618,7 +1670,12 @@ impl Hub { } else { p.chunks_per_tick.clamp(0.01, 64.0) }; + if g.unacknowledged == 0 { + g.batch_quota = 1.0; + } + g.max_unacknowledged = 10; } + S::ClientTickEnd(_) => self.guests[i].position_this_tick = false, S::ClientInformation(info) => { let g = &mut self.guests[i]; g.view_distance = @@ -1637,7 +1694,6 @@ impl Hub { 0 => self.guests[i].sleeping = false, _ => {} }, - S::ClientTickEnd(_) => {} S::Chat(c) => { let text: String = c.message.chars().filter(|c| !c.is_control()).collect(); if !text.is_empty() { @@ -1649,48 +1705,205 @@ impl Hub { other => events.push(HostEvent::Packet(conn, Box::new(other))), } } - let _ = ctx; } /// ServerGamePacketListenerImpl.handleMovePlayer. - fn move_player(&mut self, i: usize, pos: Option<[f64; 3]>, rot: Option<(f32, f32)>, flags: u8) { - let g = &mut self.guests[i]; - if g.awaiting_teleport.is_some() || !g.loaded && g.ticks == 0 && pos.is_none() { + fn move_player( + &mut self, + i: usize, + pos: Option<[f64; 3]>, + rot: Option<(f32, f32)>, + flags: u8, + ctx: &TickCtx, + events: &mut Vec, + ) { + let conn = self.guests[i].conn; + // containsInvalidValues, on the packet's values (0 where it has none). + let (yaw, pitch) = rot.unwrap_or_default(); + if pos.is_some_and(|p| p.iter().any(|v| v.is_nan())) + || !yaw.is_finite() + || !pitch.is_finite() + { + self.kick(conn, invalid_movement()); return; } - if let Some(p) = pos { - if p.iter().any(|v| !v.is_finite() || v.abs() > 3.0e7) { + let g = &mut self.guests[i]; + if pos.is_some() { + if g.position_this_tick { + self.kick(conn, invalid_movement()); return; } - // A jump of more than a hundred blocks is a rejected move. - let d2: f64 = (0..3).map(|k| (p[k] - g.position[k]).powi(2)).sum(); - if d2 > 100.0 * 100.0 { - let at = g.position; - self.teleport(i, Some(at)); - return; - } - g.previous_position = g.position; - g.position = p; + g.position_this_tick = true; } - let moved = pos.map_or([0.0; 3], |_| { - [0, 1, 2].map(|k| g.position[k] - g.previous_position[k]) - }); + if !g.loaded && g.ticks == 0 && pos.is_none() { + return; + } + // updateAwaitingTeleport: moves wait for the client to accept the + // teleport, which is sent again after 20 ticks without an answer. + if let Some((_, at)) = g.awaiting_teleport { + if self.ticks - g.teleport_tick > 20 { + self.teleport(i, Some(at)); + } + return; + } + let to = pos.unwrap_or(g.position); + let rot = rot.unwrap_or((g.yaw, g.pitch)); + self.position_change(i, to, rot, flags, ctx, events); + } + + /// ServerGamePacketListenerImpl.handlePlayerPositionChange. + fn position_change( + &mut self, + i: usize, + to: [f64; 3], + (yaw, pitch): (f32, f32), + flags: u8, + ctx: &TickCtx, + events: &mut Vec, + ) { + let (frozen, (check, elytra_check)) = (self.ticking.1, self.movement_checks); + let g = &mut self.guests[i]; + let to = [ + to[0].clamp(-3.0E7, 3.0E7), + to[1].clamp(-2.0E7, 2.0E7), + to[2].clamp(-3.0E7, 3.0E7), + ]; + let (yaw, pitch) = (wrap_degrees(yaw), wrap_degrees(pitch)); + let d = [0, 1, 2].map(|k| to[k] - g.first_good[k]); + let moved = d[0] * d[0] + d[1] * d[1] + d[2] * d[2]; + if g.sleeping { + if moved > 1.0 { + (g.yaw, g.pitch) = (yaw, pitch); + self.teleport(i, None); + } + return; + } + // TickRateManager.runsNormally: not frozen (a frozen game's steps + // are not counted either; Horizon's hub does not see them). + if !frozen { + g.received_moves += 1; + let mut packets = g.received_moves - g.known_moves; + if packets > 5 { + packets = 1; + } + // shouldCheckPlayerMovement. The server holds no velocity for a + // guest (getDeltaMovement), so nothing is expected. + if check && (!g.fall_flying || elytra_check) { + let limit = if g.fall_flying { 300.0 } else { 100.0 }; + if moved > limit * f64::from(packets) { + events.push(HostEvent::Warn(format!( + "{} moved too quickly! {},{},{}", + g.name, + crate::java_text::double_to_string(d[0]), + crate::java_text::double_to_string(d[1]), + crate::java_text::double_to_string(d[2]) + ))); + self.teleport(i, None); + return; + } + } + } + let start = g.position; + let (wrongly, refused) = self.collide(i, to, ctx); + if wrongly { + let line = format!("{} moved wrongly!", self.guests[i].name); + events.push(HostEvent::Warn(line)); + } + if refused { + let g = &mut self.guests[i]; + (g.yaw, g.pitch) = (yaw, pitch); + self.teleport(i, Some(start)); + return; + } + let g = &mut self.guests[i]; + g.previous_position = g.position; + g.position = to; + let moved = [0, 1, 2].map(|k| g.position[k] - g.previous_position[k]); if g.moves.is_empty() { g.moves_from_ground = g.on_ground; } g.moves.push((moved, flags & 1 != 0)); - if let Some((yaw, pitch)) = rot - && yaw.is_finite() - && pitch.is_finite() - { - g.yaw = yaw % 360.0; - g.pitch = pitch.clamp(-90.0, 90.0); - g.head_yaw = g.yaw; - } + g.yaw = yaw; + g.pitch = pitch.clamp(-90.0, 90.0); + g.head_yaw = g.yaw; g.on_ground = flags & 1 != 0; g.ticks = g.ticks.max(1); } + /// The collision part of handlePlayerPositionChange: whether the move + /// counts as moved wrongly (logged), and whether it is refused (the + /// player is sent back where they were). + fn collide(&self, i: usize, to: [f64; 3], ctx: &TickCtx) -> (bool, bool) { + let g = &self.guests[i]; + // noPhysics (spectators) and sleepers move freely. + let Some(binding) = ctx.collision else { + return (false, false); + }; + if g.spectator() || g.sleeping { + return (false, false); + } + let height = if g.fall_flying { + 0.6 + } else if g.sneaking { + 1.5 + } else { + 1.8 + }; + let at = |p: [f64; 3]| { + Aabb::new( + Vec3::new(p[0] - 0.3, p[1], p[2] - 0.3), + Vec3::new(p[0] + 0.3, p[1] + height, p[2] + 0.3), + ) + }; + let (old, new) = (at(g.position), at(to)); + let mut scene = SceneCollisions { + world: ctx.world, + binding, + subject: None, + }; + // player.move, then how far from the target it ended (vanilla drops + // the vertical part: `yDist > -0.5 || yDist < 0.5` always holds). + let movement = Vec3::new( + to[0] - g.position[0], + to[1] - g.position[1], + to[2] - g.position[2], + ); + let fail = !g.creative() + && scene + .resolve(old, movement, 0.6, g.on_ground) + .is_ok_and(|done| { + let (dx, dz) = (movement.x - done.x, movement.z - done.z); + dx * dx + dz * dz > 0.0625 + }); + if fail && scene.unobstructed(old).unwrap_or(true) { + return (true, true); + } + // isEntityCollidingWithAnythingNew: a block the old box did not + // already touch. + let shrink = f64::from(1.0E-5_f32); + let shrink = Vec3::new(shrink, shrink, shrink); + let Ok(old_shape) = VoxelShape::from_box(old.deflate(shrink)) else { + return (fail, false); + }; + let context = CollisionContext::position(g.position[1]); + let Ok(scan) = + BlockCollisionScan::new(&mut scene, context, new.deflate(shrink), false, 1 << 20) + else { + return (fail, false); + }; + for hit in scan { + let Ok(hit) = hit else { break }; + if !hit + .shape + .join_is_not_empty(&old_shape, ShapeOp::AND) + .unwrap_or(true) + { + return (fail, true); + } + } + (fail, false) + } + /// Acknowledge a block interaction (ClientboundBlockChangedAckPacket). /// ServerGamePacketListenerImpl.ackBlockChangesUpTo: sent with the next tick's block /// changes (`tick`). @@ -1918,6 +2131,23 @@ impl Hub { } /// A packet Horizon sends as raw bytes (no fields worth a type). +/// multiplayer.disconnect.invalid_player_movement. +/// Mth.wrapDegrees. +fn wrap_degrees(degrees: f32) -> f32 { + let mut wrapped = degrees % 360.0; + if wrapped >= 180.0 { + wrapped -= 360.0; + } + if wrapped < -180.0 { + wrapped += 360.0; + } + wrapped +} + +fn invalid_movement() -> Tag { + translatable("multiplayer.disconnect.invalid_player_movement", &[]) +} + fn raw(id: i32, payload: Vec) -> C { C::Unknown(id, payload) } diff --git a/crates/horizon-client/src/lan/mod.rs b/crates/horizon-client/src/lan/mod.rs index 58badc010..f074506b5 100644 --- a/crates/horizon-client/src/lan/mod.rs +++ b/crates/horizon-client/src/lan/mod.rs @@ -20,6 +20,7 @@ pub mod menu; pub mod playerdata; mod recipes; pub mod tracking; +pub mod waypoints; pub use conn::{Holding, PlayerSender}; use horizon_protocol::packets::play; diff --git a/crates/horizon-client/src/lan/waypoints.rs b/crates/horizon-client/src/lan/waypoints.rs new file mode 100644 index 000000000..a8797dbaf --- /dev/null +++ b/crates/horizon-client/src/lan/waypoints.rs @@ -0,0 +1,643 @@ +//! The locator bar for remote players: ServerWaypointManager and +//! WaypointTransmitter's connections, for players (the transmitters vanilla has +//! by default: `waypoint_transmit_range` is 0 for every other entity). +//! +//! Vanilla updates a player's connections each time the player's position +//! changes (`Entity.setPosRaw`, then `ServerChunkCache.move`). The hub sees the +//! position once a tick, after the moves it handled, and updates from there: +//! the same packets, since a client sends one move a tick. Hiding (crouching, +//! invisibility, a carved pumpkin or a mob head worn) changes +//! `waypoint_transmit_range` and tracks or untracks the waypoint, as +//! `LivingEntity.onAttributeUpdated` does; joining or leaving a team remakes the +//! connections with the team's colour (`ServerScoreboard.updatePlayerWaypoint`). +use horizon_protocol::packets::play::{ + Clientbound as C, TrackedWaypoint, WaypointId, WaypointTarget, waypoint_operation, +}; +use std::collections::HashMap; + +/// `Player.createAttributes`: both waypoint ranges. +pub const PLAYER_RANGE: f64 = 6.0e7; +/// `WaypointTransmitter.REALLY_FAR_DISTANCE`. +const REALLY_FAR: f32 = 332.0; +/// `EntityAzimuthConnection.update`: half a degree. +const AZIMUTH_STEP: f32 = 0.008_726_646; + +/// Items that hide their wearer (`Waypoint.addHideAttribute` on the head slot). +pub const HIDING_HEADS: [&str; 8] = [ + "minecraft:carved_pumpkin", + "minecraft:skeleton_skull", + "minecraft:wither_skeleton_skull", + "minecraft:player_head", + "minecraft:zombie_head", + "minecraft:creeper_head", + "minecraft:dragon_head", + "minecraft:piglin_head", +]; + +/// A team colour as the locator bar draws it (`Waypoint.Icon.cloneAndAssignStyle`: +/// `TeamColor.rgb()`, black lightened so it shows). +pub fn team_rgb(color: &str) -> Option { + let rgb = match color { + "black" => return Some(-13_619_152), + "dark_blue" => 0x0000AA, + "dark_green" => 0x00AA00, + "dark_aqua" => 0x00AAAA, + "dark_red" => 0xAA0000, + "dark_purple" => 0xAA00AA, + "gold" => 0xFFAA00, + "gray" => 0xAAAAAA, + "dark_gray" => 0x555555, + "blue" => 0x5555FF, + "green" => 0x55FF55, + "aqua" => 0x55FFFF, + "red" => 0xFF5555, + "light_purple" => 0xFF55FF, + "yellow" => 0xFFFF55, + "white" => 0xFFFFFF, + _ => return None, + }; + Some(rgb) +} + +/// Waypoint.Icon. +#[derive(Clone, Debug, PartialEq)] +pub struct Icon { + pub style: String, + pub color: Option, +} + +impl Default for Icon { + fn default() -> Self { + Self { + style: "minecraft:default".into(), + color: None, + } + } +} + +/// A player as the waypoint manager sees them this tick. +#[derive(Clone, Debug)] +pub struct Player { + pub uuid: u128, + /// The connection that receives the player's locator bar (None: the host, + /// whose own game draws it). + pub conn: Option, + pub position: [f64; 3], + pub spectator: bool, + /// `waypoint_transmit_range`: 0 while hidden. + pub transmit_range: f64, + pub receive_range: f64, + /// `Entity.firstTick`: the player has not ticked since joining. + pub first_tick: bool, + /// The chunk view radius (`ChunkMap.getPlayerViewDistance`). + pub view_distance: i32, + /// The player's team and its colour (`TeamColor.rgb`). + pub team: Option<(String, Option)>, + /// The icon `/waypoint modify` set. + pub icon: Icon, +} + +impl Player { + fn block(&self) -> [i32; 3] { + self.position.map(|v| v.floor() as i32) + } + fn chunk(&self) -> [i32; 2] { + let [x, _, z] = self.block(); + [x >> 4, z >> 4] + } + /// `Entity.distanceTo`. + fn distance_to(&self, other: &Player) -> f32 { + let d2: f64 = (0..3) + .map(|k| (self.position[k] - other.position[k]).powi(2)) + .sum(); + d2.sqrt() as f32 + } +} + +#[derive(Clone, Copy, Debug, PartialEq)] +enum Kind { + Block([i32; 3]), + Chunk([i32; 2]), + Azimuth(f32), +} + +#[derive(Clone, Debug)] +struct Connection { + kind: Kind, + icon: Icon, +} + +/// What the manager remembers of a player between ticks. +struct Memo { + position: [f64; 3], + /// The centre of the player's ChunkTrackingView (`ChunkMap.move`). + view_center: [i32; 2], + transmitting: bool, + team: Option<(String, Option)>, +} + +/// ServerWaypointManager for one level. +#[derive(Default)] +pub struct Waypoints { + /// The transmitters, in the order they were tracked. + waypoints: Vec, + /// The receivers, in the order they joined. + players: Vec, + /// Connections by receiver, then source. + connections: HashMap>, + memo: HashMap, + /// The `locator_bar` game rule as last seen. + enabled: Option, +} + +/// One tick's players, and the packets for their connections. +struct Tick<'a> { + players: HashMap, + view: HashMap, + /// Players a respawn replaced this tick: a new ServerPlayer, on its first tick. + fresh: &'a [u128], + enabled: bool, + out: Vec<(u32, C)>, +} + +impl Tick<'_> { + fn send( + &mut self, + receiver: u128, + source: u128, + operation: i32, + icon: &Icon, + target: WaypointTarget, + ) { + let Some(conn) = self.players.get(&receiver).and_then(|p| p.conn) else { + return; + }; + self.out.push(( + conn, + C::TrackedWaypoint(TrackedWaypoint { + operation, + id: WaypointId::Uuid(source), + style: icon.style.clone(), + color: icon.color, + target, + }), + )); + } + fn connect(&mut self, receiver: u128, source: u128, c: &Connection) { + let target = match c.kind { + Kind::Block(p) => WaypointTarget::Block(p), + Kind::Chunk(p) => WaypointTarget::Chunk(p), + Kind::Azimuth(a) => WaypointTarget::Azimuth(a), + }; + self.send(receiver, source, waypoint_operation::TRACK, &c.icon, target); + } + /// `ClientboundTrackedWaypointPacket.removeWaypoint`: an empty waypoint with the null icon. + fn disconnect(&mut self, receiver: u128, source: u128) { + self.send( + receiver, + source, + waypoint_operation::UNTRACK, + &Icon::default(), + WaypointTarget::Empty, + ); + } + /// `ChunkTrackingView.isInViewDistance` of the receiver's view. + fn chunk_visible(&self, [x, z]: [i32; 2], receiver: u128) -> bool { + let Some(&([cx, cz], r)) = self.view.get(&receiver) else { + return false; + }; + let dx = i64::from(((x - cx).abs() - 1).max(0)); + let dz = i64::from(((z - cz).abs() - 1).max(0)); + dx * dx + dz * dz < i64::from(r) * i64::from(r) + } + /// `WaypointTransmitter.doesSourceIgnoreReceiver`. + fn ignores(&self, source: &Player, receiver: &Player) -> bool { + if receiver.spectator { + return false; + } + if source.spectator { + return true; + } + let range = source.transmit_range.min(receiver.receive_range); + f64::from(source.distance_to(receiver)) >= range + } + fn really_far(source: &Player, receiver: &Player) -> bool { + source.distance_to(receiver) > REALLY_FAR + } + /// `EntityAzimuthConnection`: the angle of the receiver seen from the source, + /// `Mth.atan2` of the offset turned clockwise. + fn azimuth(source: &Player, receiver: &Player) -> f32 { + let d = [0, 1, 2].map(|k| receiver.position[k] - source.position[k]); + horizon_core::geometry::trig::atan2(d[0], -d[2]) as f32 + } + /// `LivingEntity.makeWaypointConnectionWith`. + fn make(&self, source: u128, receiver: u128) -> Option { + let (s, r) = (self.players.get(&source)?, self.players.get(&receiver)?); + if s.first_tick || self.fresh.contains(&source) || source == receiver || self.ignores(s, r) + { + return None; + } + // Waypoint.Icon.cloneAndAssignStyle: the icon's colour, else the team's. + let mut icon = s.icon.clone(); + if icon.color.is_none() { + icon.color = s.team.as_ref().and_then(|t| t.1); + } + let kind = if Self::really_far(s, r) { + Kind::Azimuth(Self::azimuth(s, r)) + } else if !self.chunk_visible(s.chunk(), receiver) { + Kind::Chunk(s.chunk()) + } else { + Kind::Block(s.block()) + }; + Some(Connection { kind, icon }) + } + /// `Connection.isBroken` for each kind. + fn broken(&self, c: &Connection, source: u128, receiver: u128) -> bool { + let (Some(s), Some(r)) = (self.players.get(&source), self.players.get(&receiver)) else { + return true; + }; + match c.kind { + Kind::Block(last) => { + let now = s.block(); + (0..3).map(|k| (now[k] - last[k]).abs()).sum::() > 1 || self.ignores(s, r) + } + Kind::Chunk(last) => { + let now = s.chunk(); + let moved = (now[0] - last[0]).abs().max((now[1] - last[1]).abs()); + moved > 1 || self.ignores(s, r) || self.chunk_visible(last, receiver) + } + Kind::Azimuth(_) => { + self.ignores(s, r) + || self.chunk_visible(s.chunk(), receiver) + || !Self::really_far(s, r) + } + } + } + /// `Connection.update`: the packet when the waypoint moved. + fn update(&mut self, c: &mut Connection, source: u128, receiver: u128) { + let (Some(s), Some(r)) = (self.players.get(&source), self.players.get(&receiver)) else { + return; + }; + let target = match &mut c.kind { + Kind::Block(last) => { + let now = s.block(); + if now == *last { + return; + } + *last = now; + WaypointTarget::Block(now) + } + Kind::Chunk(last) => { + let now = s.chunk(); + if now == *last { + return; + } + *last = now; + WaypointTarget::Chunk(now) + } + Kind::Azimuth(last) => { + let now = Self::azimuth(s, r); + if (now - *last).abs() <= AZIMUTH_STEP { + return; + } + *last = now; + WaypointTarget::Azimuth(now) + } + }; + let icon = c.icon.clone(); + self.send(receiver, source, waypoint_operation::UPDATE, &icon, target); + } +} + +impl Waypoints { + fn connection(&mut self, receiver: u128, source: u128) -> Option<&mut Connection> { + self.connections + .get_mut(&receiver)? + .iter_mut() + .find(|(s, _)| *s == source) + .map(|(_, c)| c) + } + fn put(&mut self, receiver: u128, source: u128, c: Connection) { + let row = self.connections.entry(receiver).or_default(); + match row.iter_mut().find(|(s, _)| *s == source) { + Some(slot) => slot.1 = c, + None => row.push((source, c)), + } + } + fn take(&mut self, receiver: u128, source: u128) -> Option { + let row = self.connections.get_mut(&receiver)?; + let i = row.iter().position(|(s, _)| *s == source)?; + Some(row.remove(i).1) + } + + /// `createConnection`. + fn create_connection(&mut self, t: &mut Tick, receiver: u128, source: u128) { + if receiver == source || !t.enabled { + return; + } + match t.make(source, receiver) { + Some(c) => { + t.connect(receiver, source, &c); + self.put(receiver, source, c); + } + None => { + if self.take(receiver, source).is_some() { + t.disconnect(receiver, source); + } + } + } + } + /// `updateConnection`. + fn update_connection(&mut self, t: &mut Tick, receiver: u128, source: u128) { + if receiver == source || !t.enabled { + return; + } + let Some(mut c) = self.connection(receiver, source).cloned() else { + return; + }; + if !t.broken(&c, source, receiver) { + t.update(&mut c, source, receiver); + self.put(receiver, source, c); + return; + } + match t.make(source, receiver) { + Some(new) => { + t.connect(receiver, source, &new); + self.put(receiver, source, new); + } + None => { + t.disconnect(receiver, source); + self.take(receiver, source); + } + } + } + /// `trackWaypoint`. + fn track(&mut self, t: &mut Tick, source: u128) { + if !self.waypoints.contains(&source) { + self.waypoints.push(source); + } + for receiver in self.players.clone() { + self.create_connection(t, receiver, source); + } + } + /// `updateWaypoint`. + fn update_waypoint(&mut self, t: &mut Tick, source: u128) { + if !self.waypoints.contains(&source) { + return; + } + for receiver in self.players.clone() { + if self.connection(receiver, source).is_some() { + self.update_connection(t, receiver, source); + } else { + self.create_connection(t, receiver, source); + } + } + } + /// `untrackWaypoint`. + fn untrack(&mut self, t: &mut Tick, source: u128) { + for receiver in self.players.clone() { + if self.take(receiver, source).is_some() { + t.disconnect(receiver, source); + } + } + self.waypoints.retain(|w| *w != source); + } + /// `addPlayer`. + fn add_player(&mut self, t: &mut Tick, player: u128, transmitting: bool) { + if !self.players.contains(&player) { + self.players.push(player); + } + for source in self.waypoints.clone() { + self.create_connection(t, player, source); + } + if transmitting { + self.track(t, player); + } + } + /// `updatePlayer`. + fn update_player(&mut self, t: &mut Tick, player: u128) { + for source in self.waypoints.clone() { + if self.connection(player, source).is_some() { + self.update_connection(t, player, source); + } else { + self.create_connection(t, player, source); + } + } + } + /// `removePlayer`. + fn remove_player(&mut self, t: &mut Tick, player: u128) { + for (source, _) in self.connections.remove(&player).unwrap_or_default() { + t.disconnect(player, source); + } + self.untrack(t, player); + self.players.retain(|p| *p != player); + } + /// `breakAllConnections`. + fn break_all(&mut self, t: &mut Tick) { + for receiver in self.players.clone() { + for (source, _) in self.connections.remove(&receiver).unwrap_or_default() { + t.disconnect(receiver, source); + } + } + self.connections.clear(); + } + + /// One tick: players who left, joined, moved, hid or changed team, and the + /// `locator_bar` game rule. `respawned` players are removed and added again, + /// as `PlayerList.respawn` replaces the ServerPlayer. Returns the packets for + /// each connection. + pub fn tick( + &mut self, + players: &[Player], + respawned: &[u128], + locator_bar: bool, + ) -> Vec<(u32, C)> { + let mut t = Tick { + players: players.iter().map(|p| (p.uuid, p)).collect(), + view: HashMap::new(), + fresh: respawned, + enabled: locator_bar, + out: Vec::new(), + }; + // Vanilla reads the rule when it connects; changing it breaks or remakes + // every connection (MinecraftServer.onGameRuleChanged). + let was = self.enabled.replace(locator_bar); + for (uuid, memo) in &self.memo { + if let Some(p) = t.players.get(uuid) { + t.view.insert(*uuid, (memo.view_center, p.view_distance)); + } + } + // Players who left, and the ServerPlayer a respawn replaced. + let gone: Vec = self + .players + .iter() + .copied() + .filter(|uuid| !t.players.contains_key(uuid) || respawned.contains(uuid)) + .collect(); + for uuid in gone { + self.remove_player(&mut t, uuid); + self.memo.remove(&uuid); + } + if was == Some(true) && !locator_bar { + self.break_all(&mut t); + } + // Players who joined (PlayerList.placeNewPlayer: addPlayer). + for p in players { + if self.memo.contains_key(&p.uuid) { + continue; + } + let center = p.chunk(); + t.view.insert(p.uuid, (center, p.view_distance)); + let transmitting = p.transmit_range > 0.0; + self.memo.insert( + p.uuid, + Memo { + position: p.position, + view_center: center, + transmitting, + team: p.team.clone(), + }, + ); + if p.receive_range > 0.0 { + self.add_player(&mut t, p.uuid, transmitting); + } else if transmitting { + self.track(&mut t, p.uuid); + } + } + if was == Some(false) && locator_bar { + for player in self.players.clone() { + self.update_player(&mut t, player); + } + } + for p in players { + let memo = &self.memo[&p.uuid]; + // Entity.setPosRaw, then ServerChunkCache.move. + if memo.position != p.position { + let moved_view = memo.view_center != p.chunk(); + let transmitting = memo.transmitting; + self.memo.get_mut(&p.uuid).unwrap().position = p.position; + if !p.first_tick { + if transmitting { + self.update_waypoint(&mut t, p.uuid); + } + if p.receive_range > 0.0 { + self.update_player(&mut t, p.uuid); + } + } + if moved_view { + self.memo.get_mut(&p.uuid).unwrap().view_center = p.chunk(); + t.view.insert(p.uuid, (p.chunk(), p.view_distance)); + } + if p.receive_range > 0.0 { + self.update_player(&mut t, p.uuid); + } + } + // LivingEntity.onAttributeUpdated(WAYPOINT_TRANSMIT_RANGE). + let transmitting = p.transmit_range > 0.0; + if transmitting != self.memo[&p.uuid].transmitting { + self.memo.get_mut(&p.uuid).unwrap().transmitting = transmitting; + if transmitting { + self.track(&mut t, p.uuid); + } else { + self.untrack(&mut t, p.uuid); + } + } + // ServerScoreboard: a team joined, left or recoloured remakes the connections. + if p.team != self.memo[&p.uuid].team { + self.memo.get_mut(&p.uuid).unwrap().team = p.team.clone(); + for receiver in self.players.clone() { + self.create_connection(&mut t, receiver, p.uuid); + } + } + } + t.out + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn player(uuid: u128, conn: u32, position: [f64; 3]) -> Player { + Player { + uuid, + conn: Some(conn), + position, + spectator: false, + transmit_range: PLAYER_RANGE, + receive_range: PLAYER_RANGE, + first_tick: false, + view_distance: 4, + team: None, + icon: Icon::default(), + } + } + + /// The packets to `conn`, as (operation, target). + fn to(out: &[(u32, C)], conn: u32) -> Vec<(i32, WaypointTarget)> { + out.iter() + .filter(|(c, _)| *c == conn) + .map(|(_, p)| match p { + C::TrackedWaypoint(w) => (w.operation, w.target), + other => panic!("{other:?}"), + }) + .collect() + } + + #[test] + fn a_new_player_shows_once_it_moves() { + let mut w = Waypoints::default(); + let a = player(1, 10, [0.5, 100.0, 0.5]); + let mut b = player(2, 20, [5.5, 100.0, 0.5]); + b.first_tick = true; + w.tick(std::slice::from_ref(&a), &[], true); + let out = w.tick(&[a.clone(), b.clone()], &[], true); + // B gets A at once; A sees nothing of B while B hasn't ticked. + assert_eq!(to(&out, 20), [(0, WaypointTarget::Block([0, 100, 0]))]); + assert!(to(&out, 10).is_empty()); + b.first_tick = false; + assert!(w.tick(&[a.clone(), b.clone()], &[], true).is_empty()); + b.position[0] += 1.0; + let out = w.tick(&[a.clone(), b.clone()], &[], true); + assert_eq!(to(&out, 10), [(0, WaypointTarget::Block([6, 100, 0]))]); + // One block: an update. Three: a new connection. Out of view: a chunk. + b.position[0] += 1.0; + let out = w.tick(&[a.clone(), b.clone()], &[], true); + assert_eq!(to(&out, 10), [(2, WaypointTarget::Block([7, 100, 0]))]); + b.position[0] += 3.0; + let out = w.tick(&[a.clone(), b.clone()], &[], true); + assert_eq!(to(&out, 10), [(0, WaypointTarget::Block([10, 100, 0]))]); + b.position[0] = 100.5; + let out = w.tick(&[a.clone(), b.clone()], &[], true); + assert_eq!(to(&out, 10), [(0, WaypointTarget::Chunk([6, 0]))]); + // Really far: an angle. + b.position[0] = 1000.5; + let out = w.tick(&[a.clone(), b.clone()], &[], true); + assert!(matches!( + to(&out, 10)[..], + [(0, WaypointTarget::Azimuth(_))] + )); + // Hidden: untracked; the rule off: every connection broken. + b.transmit_range = 0.0; + let out = w.tick(&[a.clone(), b.clone()], &[], true); + assert_eq!(to(&out, 10), [(1, WaypointTarget::Empty)]); + let out = w.tick(&[a.clone(), b.clone()], &[], false); + assert_eq!(to(&out, 20), [(1, WaypointTarget::Empty)]); + assert!(to(&out, 10).is_empty()); + // Leaving untracks what was left. + let out = w.tick(&[a.clone(), b.clone()], &[], true); + assert!(matches!( + to(&out, 20)[..], + [(0, WaypointTarget::Azimuth(_))] + )); + let out = w.tick(&[b.clone()], &[], true); + assert_eq!(to(&out, 20), [(1, WaypointTarget::Empty)]); + } + + #[test] + fn team_colours() { + assert_eq!(team_rgb("red"), Some(0xFF5555)); + assert_eq!(team_rgb("black"), Some(0xFF30_3030_u32 as i32)); + assert_eq!(team_rgb("reset"), None); + } +} diff --git a/crates/horizon-client/src/light.rs b/crates/horizon-client/src/light.rs index fd271f40f..595efd9b2 100644 --- a/crates/horizon-client/src/light.rs +++ b/crates/horizon-client/src/light.rs @@ -165,14 +165,14 @@ impl LightTables { fn edge_occluded(&self, top: usize, bottom: usize) -> bool { self.cells[bottom].dampening != 0 || self.blocked(top, bottom, Direction::Down as usize) } - pub fn emission(&self, state: u16) -> u8 { + pub fn emission(&self, state: u32) -> u8 { self.cells[state as usize].emission } - pub fn dampening(&self, state: u16) -> u8 { + pub fn dampening(&self, state: u32) -> u8 { self.cells[state as usize].dampening } /// Whether a state change can change light (`hasDifferentLightProperties`). - pub fn differs(&self, old: u16, new: u16) -> bool { + pub fn differs(&self, old: u32, new: u32) -> bool { let (a, b) = (&self.cells[old as usize], &self.cells[new as usize]); old != new && (a.dampening != b.dampening || a.emission != b.emission || a.shaped || b.shaped) @@ -211,12 +211,12 @@ pub fn light_chunk( // up to 14 blocks into it (sky light there is always 15). let h = ((top_section + 1) * 16).min(HEIGHT as usize); let at = |x: usize, y: usize, z: usize| (y * W + z) * W + x; - let mut states = vec![0u16; W * W * h]; + let mut states = vec![0u32; W * W * h]; let mut sky = vec![0u8; W * W * h]; let mut block = vec![0u8; W * W * h]; let mut queue = VecDeque::new(); let mut sky_queue = VecDeque::new(); - let mut ids = Box::new([0u16; 4096]); + let mut ids = Box::new([0u32; 4096]); for (dz, row) in region.iter().enumerate() { for (dx, chunk) in row.iter().enumerate() { let Some(chunk) = chunk else { continue }; @@ -391,13 +391,13 @@ pub fn apply_relighting(chunk: &mut Chunk, lights: Vec) -> Vec<(i32, i32, /// Resident chunks the incremental relighter reads and writes. pub trait LightWorld { - fn state(&self, x: i32, y: i32, z: i32) -> Option; + fn state(&self, x: i32, y: i32, z: i32) -> Option; /// Packed light (sky << 4 | block), None outside loaded chunks. fn light(&self, x: i32, y: i32, z: i32) -> Option; fn set_light(&mut self, x: i32, y: i32, z: i32, packed: u8); } impl LightWorld for HashMap<(i32, i32), Arc, S> { - fn state(&self, x: i32, y: i32, z: i32) -> Option { + fn state(&self, x: i32, y: i32, z: i32) -> Option { if !(MIN_Y..MIN_Y + HEIGHT).contains(&y) { return None; } @@ -611,10 +611,10 @@ fn relight_layer( mod tests { use super::*; - const AIR: u16 = 0; - const STONE: u16 = 1; - const TORCH: u16 = 2; - const SLAB: u16 = 3; + const AIR: u32 = 0; + const STONE: u32 = 1; + const TORCH: u32 = 2; + const SLAB: u32 = 3; /// Air, stone, torch and a bottom slab (full bottom face, half sides, /// empty top), with a third face shape for the half sides. fn tables() -> LightTables { @@ -889,7 +889,7 @@ mod tests { for (si, s) in fixture.sections.iter_mut().enumerate() { let section = Arc::make_mut(s); for i in 0..4096 { - section.set(i, ((i + si) % 16) as u16); + section.set(i, ((i + si) % 16) as u32); } for i in 0..64 { section.biomes.set(i, (i % 4) as u32); diff --git a/crates/horizon-client/src/mesher.rs b/crates/horizon-client/src/mesher.rs index 7f1191126..c3990f598 100644 --- a/crates/horizon-client/src/mesher.rs +++ b/crates/horizon-client/src/mesher.rs @@ -434,17 +434,17 @@ fn pi(x: i32, y: i32, z: i32) -> usize { } /// Blocks and light of one section plus a `B`-block border. struct Padded { - blocks: Vec, + blocks: Vec, light: Vec, } impl Padded { fn new(region: &[[Option<&Chunk>; 3]; 3], sy: i32) -> Self { - let mut blocks = vec![0u16; P * P * P]; + let mut blocks = vec![0u32; P * P * P]; let mut light = vec![0u8; P * P * P]; let base_y = MIN_Y + sy * 16; // The section itself is unpacked in one pass; the border cells of the // neighbouring sections are read one by one. - let mut own = Box::new([0u16; 4096]); + let mut own = Box::new([0u32; 4096]); let center = region[1][1].map(|c| &c.sections[sy as usize]); if let Some(section) = center { section.unpack(&mut own); @@ -643,10 +643,8 @@ impl Mesher<'_> { } let state = &info[id as usize]; if state.block_entity { - out.block_entities.push(( - [center.x * 16 + x, base_y + y, center.z * 16 + z], - u32::from(id), - )); + out.block_entities + .push(([center.x * 16 + x, base_y + y, center.z * 16 + z], id)); } if state.solid && Direction::ALL.iter().all(|d| { @@ -665,10 +663,7 @@ impl Mesher<'_> { continue; } let world = BlockPos::new(center.x * 16 + x, base_y + y, center.z * 16 + z); - let Ok(models) = self - .assets - .model_catalog - .collect(BlockStateId(id.into()), world) + let Ok(models) = self.assets.model_catalog.collect(BlockStateId(id), world) else { continue; }; @@ -700,8 +695,8 @@ impl Mesher<'_> { && self .visibility .should_render_face( - BlockStateId(id.into()), - BlockStateId(neighbor.into()), + BlockStateId(id), + BlockStateId(neighbor), d, true, ) diff --git a/crates/horizon-client/src/mesher/fluid.rs b/crates/horizon-client/src/mesher/fluid.rs index 41b1e200e..c29f2b92c 100644 --- a/crates/horizon-client/src/mesher/fluid.rs +++ b/crates/horizon-client/src/mesher/fluid.rs @@ -159,28 +159,28 @@ struct FlowView<'a> { info: &'a [Info], } impl FlowView<'_> { - fn id(&self, p: BlockPos) -> u16 { + fn id(&self, p: BlockPos) -> u32 { self.pad.blocks[pi(p.x, p.y, p.z)] } } impl FluidFlowWorld for FlowView<'_> { type Error = Infallible; - type Block = u16; + type Block = u32; fn fluid(&mut self, p: BlockPos) -> Result { Ok(self.info[self.id(p) as usize].fluid) } - fn block(&mut self, p: BlockPos) -> Result { + fn block(&mut self, p: BlockPos) -> Result { Ok(self.id(p)) } - fn blocks_fluid_flow(&self, state: u16) -> bool { + fn blocks_fluid_flow(&self, state: u32) -> bool { self.info[state as usize].blocks_flow } - fn is_ice_class(&self, state: u16) -> bool { + fn is_ice_class(&self, state: u32) -> bool { self.info[state as usize].ice } fn is_face_sturdy( &mut self, - state: u16, + state: u32, _: BlockPos, d: Direction, ) -> Result { diff --git a/crates/horizon-client/src/mesher/leaf_culling_tests.rs b/crates/horizon-client/src/mesher/leaf_culling_tests.rs index 16ea98cef..9369a1ba2 100644 --- a/crates/horizon-client/src/mesher/leaf_culling_tests.rs +++ b/crates/horizon-client/src/mesher/leaf_culling_tests.rs @@ -13,12 +13,12 @@ fn leaf_culling_removes_only_internal_leaf_and_root_faces() { let visibility = crate::models::load_visibility(&root, assets.registry).unwrap(); let tables = MeshTables::new(&assets, &facts, visibility.clone()).unwrap(); let biomes = Biomes::load(&root).unwrap(); - let state = |name| assets.registry.default_state(name).unwrap().0 as u16; + let state = |name| assets.registry.default_state(name).unwrap().0; let oak = state("minecraft:oak_leaves"); let birch = state("minecraft:birch_leaves"); let roots = state("minecraft:mangrove_roots"); let stone = state("minecraft:stone"); - let mesh = |blocks: &[([i32; 3], u16)], cull_leaves, cutout_leaves| { + let mesh = |blocks: &[([i32; 3], u32)], cull_leaves, cutout_leaves| { let mut center = Chunk::empty(0, 0); let mut east = Chunk::empty(1, 0); for &([x, y, z], state) in blocks { @@ -51,7 +51,7 @@ fn leaf_culling_removes_only_internal_leaf_and_root_faces() { let vertices = mesh(&[(at, id)], false, true); let models = assets .model_catalog - .collect(BlockStateId(id.into()), BlockPos::new(at[0], at[1], at[2])) + .collect(BlockStateId(id), BlockPos::new(at[0], at[1], at[2])) .unwrap(); let faces: Vec<_> = models.iter().flat_map(|(_, model)| &model.faces).collect(); assert_eq!(vertices.len(), faces.len() * 4); diff --git a/crates/horizon-client/src/mesher/visibility_tests.rs b/crates/horizon-client/src/mesher/visibility_tests.rs index 2f95c45cf..32eb4800c 100644 --- a/crates/horizon-client/src/mesher/visibility_tests.rs +++ b/crates/horizon-client/src/mesher/visibility_tests.rs @@ -105,7 +105,7 @@ fn fixture(mut opaque: impl FnMut(i32, i32, i32) -> bool) -> Padded { for y in 0..16 { for z in 0..16 { for x in 0..16 { - pad.blocks[pi(x, y, z)] = u16::from(opaque(x, y, z)) + 2 * ((x + y + z) as u16 & 1); + pad.blocks[pi(x, y, z)] = u32::from(opaque(x, y, z)) + 2 * ((x + y + z) as u32 & 1); } } } diff --git a/crates/horizon-client/src/minimap.rs b/crates/horizon-client/src/minimap.rs index 8015e2ab2..8b250e1af 100644 --- a/crates/horizon-client/src/minimap.rs +++ b/crates/horizon-client/src/minimap.rs @@ -75,8 +75,8 @@ impl Palette { } } #[inline] - fn get(&self, id: u16) -> (u8, u8) { - let i = usize::from(id); + fn get(&self, id: u32) -> (u8, u8) { + let i = id as usize; ( self.color.get(i).copied().unwrap_or(0), self.flags.get(i).copied().unwrap_or(AIR), @@ -137,7 +137,7 @@ fn scan_column( (0, MIN_Y as i16, 0) } /// The highest block of a chunk worth scanning from. -fn top_of(chunk: &Chunk, air: u16, layer: Layer) -> i32 { +fn top_of(chunk: &Chunk, air: u32, layer: Layer) -> i32 { let surface = (0..SECTIONS) .rev() .find(|&s| { @@ -152,7 +152,7 @@ fn top_of(chunk: &Chunk, air: u16, layer: Layer) -> i32 { Layer::Below(head) => surface.min(head), } } -fn scan_tile(chunk: &Chunk, palette: &Palette, air: u16, layer: Layer, revision: u64) -> Tile { +fn scan_tile(chunk: &Chunk, palette: &Palette, air: u32, layer: Layer, revision: u64) -> Tile { let top = top_of(chunk, air, layer); let mut tile = Tile { color: [0; 256], @@ -287,7 +287,7 @@ impl Minimap { ) -> usize { let palette = self.palette.get_or_insert_with(|| Palette::new(facts)); let started = Instant::now(); - let air = world.air.0 as u16; + let air = world.air.0; // Tiles of unloaded or distant chunks go. self.tiles.retain(|k, _| { (k.0 - center.0).abs() <= radius + 2 @@ -417,7 +417,7 @@ mod tests { (LAVA, 2), ]) } - fn chunk(columns: &[(i32, i32, &[(i32, u16)])]) -> Chunk { + fn chunk(columns: &[(i32, i32, &[(i32, u32)])]) -> Chunk { let mut c = Chunk::empty(0, 0); for &(x, z, blocks) in columns { for &(y, id) in blocks { @@ -467,7 +467,7 @@ mod tests { fn below_a_height_shows_the_floor_and_cuts_walls() { let p = palette(); // A cave: stone everywhere from 0 to 20, air at 10 and 11 in column 0. - let mut blocks: Vec<(i32, u16)> = (0..=20).map(|y| (y, 2)).collect(); + let mut blocks: Vec<(i32, u32)> = (0..=20).map(|y| (y, 2)).collect(); let solid = blocks.clone(); blocks.retain(|&(y, _)| y != 10 && y != 11); let c = chunk(&[(0, 0, &blocks), (1, 0, &solid)]); diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 0a58c376c..0611f0c85 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -692,6 +692,10 @@ pub struct Extra { pub fall_fly_ticks: i32, pub main_hand_left: bool, pub elytra: ElytraAnimation, + pub swim: SwimAnimation, + /// The arm the current swing moves (`currentSwing.hand().asArm`): `Some(true)` + /// for the right arm. + pub swing_right_arm: Option, pub flag: bool, pub flag2: bool, /// Species scratch values (the wither's head targets and clocks). @@ -809,6 +813,8 @@ pub struct Mob { /// Entity.wasEyeInWater: the eyes were in water before this tick's fluid /// update (`isUnderWater` reads it). pub was_eye_in_water: bool, + /// `Entity.firstTick`: no splash for a mob that appears in water. + pub first_tick: bool, pub no_gravity: bool, /// `Entity.mainSupportingBlockPos`: the block it stood on after its last /// move. @@ -1486,6 +1492,7 @@ impl Mob { extra: Extra::default(), fluids: living::Fluids::default(), was_eye_in_water: false, + first_tick: true, no_gravity: false, supporting: None, no_ai: false, @@ -2070,6 +2077,35 @@ impl ElytraAnimation { } } +/// `LivingEntity.swimAmount` and the render state the swimming pose reads +/// (`isVisuallySwimming`, `isInWater`). +#[derive(Clone, Copy, Debug, Default)] +pub struct SwimAnimation { + pub old: f32, + pub amount: f32, + /// `isVisuallySwimming`: in the swimming pose (or still in the gliding + /// pose after a glide ended). + pub visually: bool, + pub in_water: bool, +} +impl SwimAnimation { + /// `LivingEntity.updateSwimAmount`. + pub fn tick(&mut self, visually: bool, in_water: bool) { + self.old = self.amount; + self.amount = if visually { + (self.amount + 0.09).min(1.0) + } else { + (self.amount - 0.09).max(0.0) + }; + self.visually = visually; + self.in_water = in_water; + } + /// `LivingEntity.getSwimAmount`. + pub fn amount(&self, partial: f32) -> f32 { + self.old + partial * (self.amount - self.old) + } +} + #[cfg(test)] mod elytra_tests { use super::*; diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 318b37d5c..9ab29d0a5 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -225,6 +225,22 @@ pub fn register(m: &mut Mob) { Family::Bat => super::bat::register(m), Family::Bee => super::bee::register(m), } + // FloatGoal's constructor lets the navigation float, as do the + // constructors of these brain-driven and flying mobs. A floater rises + // 0.04 a tick in Mob.jumpInLiquid, the others 0.3 (the bounce of #542). + if m.ai.goals.has("Float") + || matches!( + m.species(), + Species::Allay + | Species::Armadillo + | Species::Camel + | Species::Goat + | Species::Parrot + | Species::Wither + ) + { + m.ai.nav.can_float = true; + } } /// Mob.tick additions that run before the entity tick (creeper fuse, puffing). fn pre_tick(m: &mut Mob, events: &mut Vec) { @@ -428,10 +444,7 @@ fn tick_swing(m: &mut Mob) { /// Entity.baseTick + LivingEntity.baseTick + Mob.baseTick. fn base_tick(m: &mut Mob, w: &MobWorld, events: &mut Vec) { m.was_eye_in_water = m.fluids.eye_in_water; - m.fluids = w.fluids(m.bounds(), m.eye_y()); - if m.fluids.in_water { - m.fall_distance = 0.0; - } + m.update_fluids(w, events); let fire = crate::motion::environment::fire_contact(w.world, w.registry, m.bounds()); if fire > 0.0 { m.ticks_frozen = 0; @@ -474,6 +487,7 @@ fn base_tick(m: &mut Mob, w: &MobWorld, events: &mut Vec) { if m.position.y < f64::from(crate::chunk::MIN_Y - 64) { m.damage(4.0, Damage::of(DamageKind::OutOfWorld)); } + m.first_tick = false; if m.alive() { // LivingEntity.baseTick: suffocation inside full blocks. let (ex, ey, ez) = ( diff --git a/crates/horizon-client/src/mobs/anim.rs b/crates/horizon-client/src/mobs/anim.rs index 36e6d78de..ecba0adcc 100644 --- a/crates/horizon-client/src/mobs/anim.rs +++ b/crates/horizon-client/src/mobs/anim.rs @@ -236,6 +236,8 @@ pub enum ArmPose { Bow, CrossbowHold, CrossbowCharge, + /// Raising a trident to throw it (or to launch with Riptide). + ThrowTrident, } /// HumanoidModel.setupAnim for piglins: `PiglinModel` poses the arms itself. pub fn humanoid_item(p: &mut Poses, s: &State, _holding: bool) { @@ -277,6 +279,12 @@ pub struct Hum { pub charge: (f32, f32), /// `PiglinModel.setupAttackAnimation`: swing a held weapon down. pub weapon_swing: bool, + /// `swimAmount` at this frame. + pub swim: f32, + /// `isUsingItem`: the swim stroke leaves the arms alone. + pub using_item: bool, + /// The arm the current swing moves (`Some(true)` for the right one). + pub swing_right_arm: Option, } impl Hum { pub fn new(right: ArmPose, left: ArmPose) -> Self { @@ -291,6 +299,9 @@ impl Hum { holding: false, charge: (0.0, 0.0), weapon_swing: false, + swim: 0.0, + using_item: false, + swing_right_arm: None, } } } @@ -299,7 +310,7 @@ impl ArmPose { matches!(self, Self::Bow | Self::CrossbowHold | Self::CrossbowCharge) } fn affects_offhand(self) -> bool { - self.two_handed() + self.two_handed() || self == Self::ThrowTrident } } /// `AnimationUtils.animateCrossbowHold`. @@ -360,6 +371,11 @@ fn pose_arm(p: &mut Poses, pose: ArmPose, right: bool, h: &Hum) { p.xrot("right_arm", -PI / 2.0 + hx); p.xrot("left_arm", -PI / 2.0 + hx); } + ArmPose::ThrowTrident => { + let x = p.get(arm)[3]; + p.xrot(arm, x * 0.5 - PI); + p.yrot(arm, 0.0); + } ArmPose::CrossbowHold => crossbow_hold(p, hx, hy, right), ArmPose::CrossbowCharge => crossbow_charge(p, h.charge.0, h.charge.1, right), } @@ -445,12 +461,17 @@ pub(super) fn humanoid(p: &mut Poses, s: &State, h: &Hum) { let xm = ((1.0 - (1.0 - swing) * (1.0 - swing)) * PI).msin(); let x = drop + ym * 1.2 - xm * 0.4; let y = 0.1 - ym * 0.6; - p.xrot("right_arm", x); - p.yrot("right_arm", if raise { -y } else { y }); - p.zrot("right_arm", 0.0); - p.xrot("left_arm", x); - p.yrot("left_arm", if raise { y } else { -y }); - p.zrot("left_arm", 0.0); + // An arm raising a trident keeps its pose. + if h.right != ArmPose::ThrowTrident { + p.xrot("right_arm", x); + p.yrot("right_arm", if raise { -y } else { y }); + p.zrot("right_arm", 0.0); + } + if h.left != ArmPose::ThrowTrident { + p.xrot("left_arm", x); + p.yrot("left_arm", if raise { y } else { -y }); + p.zrot("left_arm", 0.0); + } } if h.crouching { p.xrot("body", 0.5); @@ -465,6 +486,86 @@ pub(super) fn humanoid(p: &mut Poses, s: &State, h: &Hum) { } bob(p, "right_arm", s.age, 1.0); bob(p, "left_arm", s.age, -1.0); + if h.swim > 0.0 { + swim(p, s.pos, h); + } +} +/// `Mth.rotLerpRad`. +fn rot_lerp_rad(a: f32, from: f32, to: f32) -> f32 { + let mut diff = to - from; + while diff < -PI { + diff += PI * 2.0; + } + while diff >= PI { + diff -= PI * 2.0; + } + from + a * diff +} +/// `HumanoidModel.setupSwimAnimation`: the stroke of both arms over 26 +/// walk-animation units, the head down and a slow flutter kick. The left arm +/// eases the short way round, the right one linearly, as in vanilla. +fn swim(p: &mut Poses, pos: f32, h: &Hum) { + let amount = h.swim; + if !h.fall_flying { + let x = p.get("head")[3]; + p.xrot( + "head", + rot_lerp_rad(amount, x, -std::f32::consts::FRAC_PI_4), + ); + } + let swim_pos = pos % 26.0; + let right_amount = if h.swing_right_arm == Some(true) { + 0.0 + } else { + amount + }; + let left_amount = if h.swing_right_arm == Some(false) { + 0.0 + } else { + amount + }; + let lerp = |a: f32, from: f32, to: f32| from + a * (to - from); + let quadratic = |x: f32| -65.0 * x + x * x; + if !h.using_item { + // Targets (x, y, z) for the left and right arm. + let targets = if swim_pos < 14.0 { + let stroke = 1.870_796_4 * quadratic(swim_pos) / quadratic(14.0); + Some(([0.0, PI, PI + stroke], [0.0, PI, PI - stroke])) + } else if swim_pos < 22.0 { + let t = (swim_pos - 14.0) / 8.0; + let x = PI / 2.0 * t; + Some(( + [x, PI, 5.012_389 - 1.870_796_4 * t], + [x, PI, 1.270_796_3 + 1.870_796_4 * t], + )) + } else if swim_pos < 26.0 { + let t = (swim_pos - 22.0) / 4.0; + let x = PI / 2.0 - PI / 2.0 * t; + Some(([x, PI, PI], [x, PI, PI])) + } else { + None + }; + if let Some((left, right)) = targets { + for (i, target) in left.into_iter().enumerate() { + let angle = &mut p.get("left_arm")[3 + i]; + *angle = rot_lerp_rad(left_amount, *angle, target); + } + for (i, target) in right.into_iter().enumerate() { + let angle = &mut p.get("right_arm")[3 + i]; + *angle = lerp(right_amount, *angle, target); + } + } + } + let x = p.get("left_leg")[3]; + p.xrot( + "left_leg", + lerp(amount, x, 0.3 * (pos * 0.333_333_34 + PI).mcos()), + ); + let x = p.get("right_leg")[3]; + p.xrot( + "right_leg", + lerp(amount, x, 0.3 * (pos * 0.333_333_34).mcos()), + ); } /// Apply the mob's model animation to `p`. pub fn setup( @@ -536,7 +637,21 @@ pub fn setup( | Anim::ZombieVillager | Anim::ZombifiedPiglin | Anim::Humanoid { zombie_arms: true } => { - let mut h = Hum::new(ArmPose::Empty, ArmPose::Empty); + // DrownedRenderer.getArmPose: an aggressive drowned raises the + // trident in its main arm. + let throwing = m.kind.anim == Anim::Drowned + && m.ai.aggressive + && m.extra.main_hand.as_deref() == Some("minecraft:trident"); + let main = if throwing { + ArmPose::ThrowTrident + } else { + ArmPose::Empty + }; + let mut h = if m.extra.main_hand_left { + Hum::new(ArmPose::Empty, main) + } else { + Hum::new(main, ArmPose::Empty) + }; h.zombie = Some(m.ai.aggressive); h.holding = m.extra.main_hand.is_some(); h.crouching = m.extra.crouching; @@ -561,6 +676,7 @@ pub fn setup( match m.extra.main_hand.as_deref() { Some(_) if using_main && m.extra.blocking => ArmPose::Block, Some("minecraft:bow") if using_main => ArmPose::Bow, + Some("minecraft:trident") if using_main => ArmPose::ThrowTrident, Some("minecraft:crossbow") if using_main && !m.extra.charged => { ArmPose::CrossbowCharge } @@ -573,7 +689,7 @@ pub fn setup( } else { right }; - let off = if player && m.extra.off_hand.is_some() && !pose.two_handed() { + let off = if player && m.extra.off_hand.is_some() && !pose.affects_offhand() { if m.extra.using_item > 0 && m.extra.using_off_hand && m.extra.blocking { ArmPose::Block } else { @@ -596,6 +712,9 @@ pub fn setup( } h.crouching = m.extra.crouching; h.passenger = m.extra.passenger; + h.swim = m.extra.swim.amount(s.partial); + h.using_item = m.extra.using_item > 0; + h.swing_right_arm = m.extra.swing_right_arm; humanoid(p, s, &h); if m.ai.aggressive && !bow { // SkeletonModel melee pose. @@ -761,5 +880,74 @@ mod flight_pose_tests { humanoid(&mut flight, &state, &h); assert_eq!(flight.get("head")[3], -std::f32::consts::FRAC_PI_4); assert_eq!(flight.get("right_leg")[3], ground.get("right_leg")[3] / 8.0); + // THROW_TRIDENT: the arm goes up over the head, and zombie arms leave it there. + let mut throw = make(); + let mut h = Hum::new(ArmPose::ThrowTrident, ArmPose::Item); + h.zombie = Some(true); + humanoid(&mut throw, &state, &h); + let still = (state.pos * 0.6662 + PI).mcos() * 2.0 * 0.5; + assert_eq!(throw.get("right_arm")[3], still * 0.5 - PI); + assert_eq!(throw.get("right_arm")[4], 0.0); + assert_eq!(throw.get("left_arm")[3], -PI / 1.5); + } + + #[test] + fn swimming_strokes_the_arms_and_kicks() { + let state = State { + pos: 7.0, + speed: 1.0, + age: 0.0, + y_rot: 0.0, + x_rot: 30.0, + swing: 0.0, + baby: false, + partial: 0.0, + }; + let make = || Poses { + map: ["head", "right_arm", "left_arm", "right_leg", "left_leg"] + .into_iter() + .map(|n| (n.to_string(), [0.0; 9])) + .collect(), + dummy: [0.0; 9], + skipped: Default::default(), + hidden: Default::default(), + }; + let same_angle = |a: f32, b: f32| ((a - b + PI).rem_euclid(PI * 2.0) - PI).abs() < 1e-5; + let mut h = Hum::new(ArmPose::Empty, ArmPose::Empty); + h.swim = 1.0; + let mut p = make(); + humanoid(&mut p, &state, &h); + // Mid-stroke: both arms sweep out from overhead by the quadratic. + let stroke = 1.870_796_4 * (-65.0 * 7.0 + 49.0) / (-65.0 * 14.0 + 196.0); + assert_eq!(p.get("right_arm")[5], PI - stroke); + assert!(same_angle(p.get("left_arm")[5], PI + stroke)); + assert_eq!(p.get("right_arm")[4], PI); + assert!(same_angle(p.get("head")[3], -std::f32::consts::FRAC_PI_4)); + assert!(same_angle( + p.get("right_leg")[3], + 0.3 * (7.0f32 * 0.333_333_34).mcos() + )); + // Using an item keeps the arms; a swing frees its own arm only. + let mut using = make(); + h.using_item = true; + humanoid(&mut using, &state, &h); + assert_eq!(using.get("right_arm")[4], 0.0); + h.using_item = false; + h.swing_right_arm = Some(true); + let mut swinging = make(); + humanoid( + &mut swinging, + &State { + swing: 0.5, + ..state + }, + &h, + ); + assert!(swinging.get("right_arm")[4] < 1.0); + assert!(same_angle(swinging.get("left_arm")[4], PI)); + // Out of the pose nothing changes. + let mut dry = make(); + humanoid(&mut dry, &state, &Hum::new(ArmPose::Empty, ArmPose::Empty)); + assert_eq!(dry.get("head")[3], 30.0f32.to_radians()); } } diff --git a/crates/horizon-client/src/mobs/equine.rs b/crates/horizon-client/src/mobs/equine.rs index 46d33b576..e7d73f0fa 100644 --- a/crates/horizon-client/src/mobs/equine.rs +++ b/crates/horizon-client/src/mobs/equine.rs @@ -83,19 +83,33 @@ pub fn register(m: &mut Mob) { return super::llama::register(m); } let tempt = m.kind.tempt.clone(); - let rears = can_rear(m.species()); + let species = m.species(); + let rears = can_rear(species); + let stand_interval = m.kind.ambient_interval; let g = &mut m.ai.goals; - g.add(0, Float); - g.add(1, MountPanic(Panic::new(1.2))); g.add(1, RunAroundLikeCrazy); g.add(2, Breed::new(1.0)); - g.add(3, Tempt::new(1.25, tempt, false)); g.add(4, FollowParent::new(1.0)); g.add(6, Stroll::avoiding_water(0.7)); g.add(7, LookAtPlayer::new(6.0)); g.add(8, RandomLookAround::default()); if rears { - g.add(9, RandomStand { next: -80 }); + // resetStandInterval: -getAmbientStandInterval (400 for every horse). + g.add( + 9, + RandomStand { + next: -stand_interval, + }, + ); + } + // addBehaviourGoals, after the rest: none for the skeleton horse (it + // sinks), no mount panic for the zombie horse. + if species != Species::SkeletonHorse { + g.add(0, Float); + if species != Species::ZombieHorse { + g.add(1, MountPanic(Panic::new(1.2))); + } + g.add(3, Tempt::new(1.25, tempt, false)); } } /// `generateMaxHealth`, `generateSpeed`, `generateJumpStrength` and the coat. @@ -285,7 +299,7 @@ impl Goal for RandomStand { fn can_use(&mut self, m: &mut Mob, _: &mut Cx) -> bool { self.next += 1; if self.next > 0 && m.random.next_int_bound(1000).unwrap_or(1000) < self.next { - self.next = -80; + self.next = -m.kind.ambient_interval; return m.random.next_int_bound(10).unwrap_or(1) == 0; } false diff --git a/crates/horizon-client/src/mobs/goals.rs b/crates/horizon-client/src/mobs/goals.rs index 0d13b0e81..9419a23ba 100644 --- a/crates/horizon-client/src/mobs/goals.rs +++ b/crates/horizon-client/src/mobs/goals.rs @@ -94,6 +94,10 @@ impl GoalSelector { .map(|g| g.goal.name()) .collect() } + /// Whether a goal of this name is registered, running or not. + pub fn has(&self, name: &str) -> bool { + self.goals.iter().any(|g| g.goal.name() == name) + } pub fn is_running(&self, name: &str) -> bool { self.goals .iter() diff --git a/crates/horizon-client/src/mobs/living.rs b/crates/horizon-client/src/mobs/living.rs index 6bf246541..ebbfabf6b 100644 --- a/crates/horizon-client/src/mobs/living.rs +++ b/crates/horizon-client/src/mobs/living.rs @@ -398,10 +398,7 @@ impl Mob { /// (touching water resets the fall), then Entity.checkFallDamage. fn check_fall_damage(&mut self, w: &MobWorld, moved: Vec3, events: &mut Vec) { if !self.fluids.in_water { - self.fluids = w.fluids(self.bounds(), self.eye_y()); - if self.fluids.in_water { - self.fall_distance = 0.0; - } + self.update_fluids(w, events); } if !self.fluids.in_water && moved.y < 0.0 { self.fall_distance -= f64::from(moved.y as f32); @@ -413,6 +410,23 @@ impl Mob { self.fall_distance = 0.0; } } + /// Entity.updateFluidInteraction: the fluid state, then touching water + /// resets the fall and entering it (after the first tick) splashes. + pub(super) fn update_fluids(&mut self, w: &MobWorld, events: &mut Vec) { + let was_touching_water = self.fluids.in_water; + self.fluids = w.fluids(self.bounds(), self.eye_y()); + if self.fluids.in_water { + self.fall_distance = 0.0; + if !was_touching_water && !self.first_tick { + // The splash is the mob's own: Horizon's mobs carry no + // controlling passenger here. + let _ = horizon_world::water_splash::water_splash( + &mut Splash { mob: self, events }, + 1_000_000, + ); + } + } + } /// Entity.applyMovementEmissionAndPlaySound: walked distance, footsteps /// and swimming sounds. fn movement_emission(&mut self, w: &MobWorld, moved: Vec3, events: &mut Vec) { @@ -811,7 +825,8 @@ impl Mob { self.velocity.y += if self.species() == super::Species::Bee { 0.01 } else if self.ai.nav.can_float { - 0.04 + // LivingEntity.jumpInLiquid adds 0.04F. + f64::from(0.04_f32) } else { 0.3 }; @@ -915,3 +930,93 @@ fn free_position( let free = allowed_shape.join(&collisions, ShapeOp::ONLY_FIRST).ok()?; free.closest_point(preferred) } + +/// `Entity.doWaterSplashEffect` for a mob: its own random, its splash sound +/// (`getSwimSplashSound`), particles for the client and the SPLASH game event. +struct Splash<'a> { + mob: &'a mut Mob, + events: &'a mut Vec, +} +impl horizon_world::water_splash::WaterSplashWorld for Splash<'_> { + type Error = std::convert::Infallible; + type Entity = (); + type Sound = &'static str; + fn this_entity(&self) {} + fn controlling_passenger(&mut self) -> Result, Self::Error> { + Ok(None) + } + fn velocity(&mut self, _: ()) -> Result { + Ok(self.mob.velocity) + } + fn splash_sound(&mut self, _high_speed: bool) -> Result<&'static str, Self::Error> { + use super::Species; + // Entity.getSwimSplashSound and getSwimHighSpeedSplashSound: the + // overrides are Monster's, the hoglin's, the dolphin's and the axolotl's. + let species = self.mob.species(); + Ok(match species { + Species::Dolphin => "entity.dolphin.splash", + Species::Axolotl => "entity.axolotl.splash", + _ if species == Species::Hoglin || super::ai::is_monster(species) => { + "entity.hostile.splash" + } + _ => "entity.generic.splash", + }) + } + fn random_float(&mut self) -> Result { + Ok(self.mob.random.next_float()) + } + fn random_double(&mut self) -> Result { + Ok(self.mob.random.next_double()) + } + fn play_sound( + &mut self, + sound: &'static str, + volume: f32, + pitch: f32, + ) -> Result<(), Self::Error> { + if !self.mob.silent { + let at = [ + self.mob.position.x, + self.mob.position.y, + self.mob.position.z, + ]; + self.events.push(MobEvent::Sound(sound, at, volume, pitch)); + } + Ok(()) + } + fn position(&self) -> Vec3 { + self.mob.position + } + fn width(&self) -> f32 { + self.mob.width() + } + fn particle( + &mut self, + kind: horizon_world::water_splash::SplashParticle, + position: Vec3, + velocity: Vec3, + ) -> Result<(), Self::Error> { + self.events.push(MobEvent::ParticleAt { + kind: match kind { + horizon_world::water_splash::SplashParticle::Bubble => "bubble", + horizon_world::water_splash::SplashParticle::Splash => "splash", + }, + at: [position.x, position.y, position.z], + velocity: [velocity.x, velocity.y, velocity.z], + }); + Ok(()) + } + fn splash_event(&mut self) -> Result<(), Self::Error> { + self.events.push(MobEvent::GameEvent { + event: "minecraft:splash", + at: [ + self.mob.position.x, + self.mob.position.y, + self.mob.position.z, + ], + source: self.mob.id, + state: horizon_world::registry::BlockStateId(0), + }); + Ok(()) + } +} diff --git a/crates/horizon-client/src/mobs/path.rs b/crates/horizon-client/src/mobs/path.rs index 368dead97..abadc9565 100644 --- a/crates/horizon-client/src/mobs/path.rs +++ b/crates/horizon-client/src/mobs/path.rs @@ -4,8 +4,8 @@ //! `AmphibiousNodeEvaluator`), ported rule for rule. use super::{MobWorld, data}; use crate::blocks::Fluid; +use crate::fast_hash::FxHashMap; use horizon_core::geometry::{Aabb, Vec3}; -use std::collections::HashMap; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum PathType { @@ -301,10 +301,13 @@ pub struct Finder<'a, 'w> { mob: &'a PathMob, eval: Evaluator, nodes: Vec, - index: HashMap<(i32, i32, i32), u32>, + index: FxHashMap<(i32, i32, i32), u32>, heap: Vec, - types: HashMap<(i32, i32, i32), PathType>, - collisions: HashMap<[u64; 6], bool>, + types: FxHashMap<(i32, i32, i32), PathType>, + /// `path_type` of each position asked during this query: it reads up to + /// 26 blocks, and overlapping mob boxes ask for the same positions. + statics: std::cell::RefCell>, + collisions: FxHashMap<[u64; 6], bool>, entity_width: i32, entity_height: i32, entity_depth: i32, @@ -319,10 +322,11 @@ impl<'a, 'w> Finder<'a, 'w> { mob, eval, nodes: Vec::new(), - index: HashMap::new(), + index: FxHashMap::default(), heap: Vec::new(), - types: HashMap::new(), - collisions: HashMap::new(), + types: FxHashMap::default(), + statics: Default::default(), + collisions: FxHashMap::default(), entity_width: (mob.width + 1.0).floor() as i32, entity_height: (mob.height + 1.0).floor() as i32, entity_depth: (mob.width + 1.0).floor() as i32, @@ -385,7 +389,17 @@ impl<'a, 'w> Finder<'a, 'w> { } // --- path types ----------------------------------------------------- /// WalkNodeEvaluator.getPathTypeStatic (+ the fly/amphibious overrides). + /// The world doesn't change during a query, so each position's type is + /// worked out once. fn path_type(&self, x: i32, y: i32, z: i32) -> PathType { + if let Some(t) = self.statics.borrow().get(&(x, y, z)) { + return *t; + } + let t = self.compute_path_type(x, y, z); + self.statics.borrow_mut().insert((x, y, z), t); + t + } + fn compute_path_type(&self, x: i32, y: i32, z: i32) -> PathType { match self.eval { Evaluator::Fly => self.fly_path_type(x, y, z), Evaluator::Amphibious { .. } => { @@ -991,7 +1005,7 @@ impl<'a, 'w> Finder<'a, 'w> { let malus = |s: &Self, n: Option| n.is_some_and(|n| s.nodes[n as usize].cost_malus >= 0.0); // Direction.values(): down, up, north, south, west, east. - let mut by_dir: HashMap<(i32, i32, i32), Option> = HashMap::new(); + let mut by_dir: FxHashMap<(i32, i32, i32), Option> = FxHashMap::default(); for (dx, dy, dz) in [ (0, -1, 0), (0, 1, 0), diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs index e8120e92e..21ab88aa3 100644 --- a/crates/horizon-client/src/mobs/render.rs +++ b/crates/horizon-client/src/mobs/render.rs @@ -765,6 +765,17 @@ pub fn base_transform_at(m: &Mob, alpha: f32, origin: glam::Mat4) -> glam::Mat4 .unwrap_or_else(|| glam::Mat4::from_rotation_y((180.0 - body).to_radians())), }; let mut base = origin * rotation; + if m.death_time > 0 && !super::creaking::tearing_down(m) { + let f = ((m.death_time as f32 + alpha - 1.0) / 20.0 * 1.6) + .sqrt() + .min(1.0); + base *= glam::Mat4::from_rotation_z(f * flip_degrees(m).to_radians()); + } else if matches!(m.custom_name.as_deref(), Some("Dinnerbone" | "Grumm")) { + // isEntityUpsideDown + base *= glam::Mat4::from_translation(glam::Vec3::new(0.0, m.height() + 0.1, 0.0)) + * glam::Mat4::from_rotation_z(PI); + } + // AvatarRenderer.setupRotations, after LivingEntityRenderer's. if m.extra.fall_flying { let ticks = m.extra.fall_fly_ticks as f32 + alpha; let scale = (ticks * ticks / 100.0).min(1.0); @@ -785,17 +796,20 @@ pub fn base_transform_at(m: &Mob, alpha: f32, origin: glam::Mat4) -> glam::Mat4 (sign * horizon_core::jmath::acos(dot.abs().min(1.0))) as f32, ); } - } - - if m.death_time > 0 && !super::creaking::tearing_down(m) { - let f = ((m.death_time as f32 + alpha - 1.0) / 20.0 * 1.6) - .sqrt() - .min(1.0); - base *= glam::Mat4::from_rotation_z(f * flip_degrees(m).to_radians()); - } else if matches!(m.custom_name.as_deref(), Some("Dinnerbone" | "Grumm")) { - // isEntityUpsideDown - base *= glam::Mat4::from_translation(glam::Vec3::new(0.0, m.height() + 0.1, 0.0)) - * glam::Mat4::from_rotation_z(PI); + } else if m.extra.swim.amount(alpha) > 0.0 { + // AvatarRenderer.setupRotations: tip forward with the swim amount, + // following the pitch in water, then lie along the swimming box. + let amount = m.extra.swim.amount(alpha); + let pitch = m.previous_head_pitch + (m.head_pitch - m.previous_head_pitch) * alpha; + let target = if m.extra.swim.in_water { + -90.0 - pitch + } else { + -90.0 + }; + base *= glam::Mat4::from_rotation_x((amount * target).to_radians()); + if m.extra.swim.visually { + base *= glam::Mat4::from_translation(glam::Vec3::new(0.0, -1.0, 0.3)); + } } base *= species_rotations(m, alpha); base *= glam::Mat4::from_scale(glam::Vec3::new(-1.0, -1.0, 1.0)); diff --git a/crates/horizon-client/src/mobs/species.rs b/crates/horizon-client/src/mobs/species.rs index fcfd61992..28ea729b3 100644 --- a/crates/horizon-client/src/mobs/species.rs +++ b/crates/horizon-client/src/mobs/species.rs @@ -1634,7 +1634,16 @@ impl Def { use Species as S; match self.species { S::Turtle => 200, - S::Horse | S::Donkey | S::Mule | S::Llama | S::TraderLlama => 400, + // AbstractHorse.getAmbientSoundInterval, for every subclass. + S::Horse + | S::Donkey + | S::Mule + | S::ZombieHorse + | S::SkeletonHorse + | S::Llama + | S::TraderLlama + | S::Camel + | S::CamelHusk => 400, S::Ocelot => 900, S::Squid | S::GlowSquid diff --git a/crates/horizon-client/src/mobs/tame.rs b/crates/horizon-client/src/mobs/tame.rs index 4e5e3d290..1e725df80 100644 --- a/crates/horizon-client/src/mobs/tame.rs +++ b/crates/horizon-client/src/mobs/tame.rs @@ -855,8 +855,17 @@ impl Goal for CatSitOnBlock { JUMP | MOVE } fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + if !m.extra.tame || m.extra.sitting { + return false; + } + // MoveToBlockGoal.canUse draws the next delay only once the last one + // has run out. + if self.0.next_start > 0 { + self.0.next_start -= 1; + return false; + } let next = reduced(200 + m.random.next_int_bound(200).unwrap_or(0)); - m.extra.tame && !m.extra.sitting && self.0.can_use(m, cx.w, next, can_sit_on) + self.0.can_use(m, cx.w, next, can_sit_on) } fn can_continue(&mut self, _: &mut Mob, cx: &mut Cx) -> bool { self.0.can_continue(cx.w, can_sit_on) diff --git a/crates/horizon-client/src/mod_content.rs b/crates/horizon-client/src/mod_content.rs index f78808da7..25df87352 100644 --- a/crates/horizon-client/src/mod_content.rs +++ b/crates/horizon-client/src/mod_content.rs @@ -323,7 +323,7 @@ impl<'a> Plan<'a> { } let list: Vec = by_mod.iter().map(|(m, n)| format!("{m} {n}")).collect(); return Err(format!( - "{next} block states, more than the {MAX_STATES} chunks can hold ({} in the game; \ + "{next} block states, more than the {MAX_STATES} a world can hold ({} in the game; \ the mods: {})", names.len(), list.join(", ") @@ -644,9 +644,11 @@ const NBT_TABLES: [&str; 6] = [ /// In a root built here: the content mods' ids, the namespaces loaders /// read besides `minecraft`. const MARKER: &str = "horizon-mod-content.json"; -/// Block states a world can hold: chunk sections store a state in 16 bits -/// (crate::chunk). Mods past it are refused, not wrapped onto other blocks. -pub const MAX_STATES: u32 = 1 << 16; +/// Block states a world can hold. Chunk sections widen their global palette +/// past vanilla's 16 bits to fit the registry (crate::chunk::set_block_states); +/// this caps it at 20 bits, about 29 times vanilla's 35,723 states. Mods past +/// it are refused, not wrapped onto other blocks. +pub const MAX_STATES: u32 = 1 << 20; /// The content mods' namespaces in a root built by [`build`]; none in any /// other root, so vanilla's loaders read exactly what they did. diff --git a/crates/horizon-client/src/mod_content_tests.rs b/crates/horizon-client/src/mod_content_tests.rs index 7067eef40..2a040b0fc 100644 --- a/crates/horizon-client/src/mod_content_tests.rs +++ b/crates/horizon-client/src/mod_content_tests.rs @@ -266,17 +266,17 @@ fn refuses_unknown_templates_and_vanilla_ids() { } #[test] -fn refuses_more_states_than_chunks_hold() { +fn refuses_more_states_than_a_world_holds() { let Some(base) = prepared() else { return; }; let scratch = std::env::temp_dir().join(format!("horizon-mod-states-{}", std::process::id())); let _ = fs::remove_dir_all(&scratch); let mut m = ruby_mod(&scratch.join("ruby")); - // Redstone wire has 1296 states: 40 copies pass any game's 65536. + // Redstone wire has 1296 states: 810 copies pass 2^20 with any game. let mut wire = m.content.blocks[0].clone(); wire.template = "minecraft:redstone_wire".into(); - for i in 0..40 { + for i in 0..810 { let mut b = wire.clone(); b.id = format!("ruby:wire_{i}"); m.content.blocks.push(b); @@ -284,13 +284,33 @@ fn refuses_more_states_than_chunks_hold() { let e = build(&base, std::slice::from_ref(&m), &scratch.join("out")).unwrap_err(); assert!( e.to_string() - .contains("more than the 65536 chunks can hold"), + .contains("more than the 1048576 a world can hold"), "{e}" ); assert!(e.to_string().contains("the mods: ruby "), "{e}"); fs::remove_dir_all(&scratch).unwrap(); } +#[test] +fn builds_past_sixteen_bit_states() { + let Some(base) = prepared() else { + return; + }; + let scratch = std::env::temp_dir().join(format!("horizon-mod-wide-{}", std::process::id())); + let _ = fs::remove_dir_all(&scratch); + let mut m = ruby_mod(&scratch.join("ruby")); + // 40 redstone wires: 51,840 states, past 65,536 with vanilla's 35,723. + let mut wire = m.content.blocks[0].clone(); + wire.template = "minecraft:redstone_wire".into(); + for i in 0..40 { + let mut b = wire.clone(); + b.id = format!("ruby:wire_{i}"); + m.content.blocks.push(b); + } + build(&base, std::slice::from_ref(&m), &scratch.join("out")).unwrap(); + fs::remove_dir_all(&scratch).unwrap(); +} + #[test] fn no_content_keeps_the_base_root() { let base = Path::new("/nonexistent/base"); diff --git a/crates/horizon-client/src/model_library.rs b/crates/horizon-client/src/model_library.rs index 7f7852ad0..c6a1a9d87 100644 --- a/crates/horizon-client/src/model_library.rs +++ b/crates/horizon-client/src/model_library.rs @@ -42,25 +42,52 @@ impl ParsedModels { /// Blocks require every referenced resource, including parents. Items instead /// skip broken resources and use the resolver's missing-model fallback. - pub(crate) fn block_library( + pub(crate) fn block_library(&self, root: &Path, pending: Vec) -> Result { + self.block_models(root, pending, false) + } + /// [`Self::block_library`], but a model that does not read takes the + /// built-in missing model, logged, as vanilla's model discovery does + /// ("Missing block model"). Mods' content gets this; vanilla's resources + /// always read. + pub(crate) fn block_library_or_missing( + &self, + root: &Path, + pending: Vec, + ) -> Result { + self.block_models(root, pending, true) + } + fn block_models( &self, root: &Path, mut pending: Vec, + or_missing: bool, ) -> Result { let mut models = BTreeMap::new(); + let mut skipped = std::collections::BTreeSet::new(); while let Some(name) = pending.pop() { - if models.contains_key(&name) { + if models.contains_key(&name) || skipped.contains(&name) { continue; } let model = match self.models.get(&name) { Some(model) => Arc::clone(model), None => { - let (namespace, path) = - name.split_once(':').ok_or("invalid model identifier")?; - let text = fs::read_to_string( - root.join(format!("assets/{namespace}/models/{path}.json")), - )?; - Arc::new(CuboidModel::from_json(&text)?) + let read = || -> Result { + let (namespace, path) = + name.split_once(':').ok_or("invalid model identifier")?; + let text = fs::read_to_string( + root.join(format!("assets/{namespace}/models/{path}.json")), + )?; + Ok(CuboidModel::from_json(&text)?) + }; + match read() { + Ok(model) => Arc::new(model), + Err(e) if or_missing => { + log::warn!("Horizon: missing block model {name}: {e}"); + skipped.insert(name); + continue; + } + Err(e) => return Err(e), + } } }; if let Some(parent) = &model.parent { @@ -141,6 +168,13 @@ mod tests { .to_string() }; assert_eq!(load(&direct), load(&parsed)); + // Mods' content: vanilla's missing model instead of an error. + let lenient = parsed.block_library_or_missing(&fixture.0, vec![name.into()])?; + assert_eq!( + format!("{:?}", lenient.resolve(name)?.elements()), + format!("{:?}", lenient.resolve(MISSING_MODEL)?.elements()), + "{name}" + ); } let items = parsed.into_item_library()?; let block = shared.resolve("horizon:nested/child")?; diff --git a/crates/horizon-client/src/models/catalog.rs b/crates/horizon-client/src/models/catalog.rs index ecedd0f95..a78ddc951 100644 --- a/crates/horizon-client/src/models/catalog.rs +++ b/crates/horizon-client/src/models/catalog.rs @@ -136,11 +136,29 @@ impl StateModels { }) .collect::>()?; let possible: Vec = serde_json::from_value(facts["states"].clone())?; - let resource = fs::read_to_string( + // Vanilla's BlockStateModelLoader: a definition that does not + // read or bind is logged and its block gets no model; the other + // blocks still load. + let loaded = fs::read_to_string( root.join(format!("assets/{namespace}/blockstates/{path}.json")), - )?; - let definition = BlockStateDefinition::from_json(&resource)?; - let bound = definition.bind(&schema, &possible)?; + ) + .map_err(|e| e.to_string()) + .and_then(|text| BlockStateDefinition::from_json(&text).map_err(|e| e.to_string())) + .and_then(|definition| { + let bound = definition + .bind(&schema, &possible) + .map_err(|e| e.to_string())?; + Ok((definition, bound)) + }); + let (definition, bound) = match loaded { + Ok(loaded) => loaded, + Err(e) => { + log::warn!( + "Horizon: blockstate definition of {namespace}:{path} not loaded: {e}" + ); + continue; + } + }; let native_order: Vec<_> = by_name .get(name.as_str()) .map(Vec::as_slice) @@ -185,10 +203,14 @@ impl StateModels { } choices.push(CompiledChoices::new(&definition)?); } - let library = models.block_library( - root, - variants.iter().map(|(name, _, _)| name.clone()).collect(), - )?; + let names: Vec = variants.iter().map(|(name, _, _)| name.clone()).collect(); + let library = match models.block_library(root, names.clone()) { + Ok(library) => library, + Err(e) => { + log::warn!("Horizon: block models incomplete ({e}); using the missing model"); + models.block_library_or_missing(root, names)? + } + }; let mut baked = BTreeMap::new(); for (name, turns, lock) in variants { let model = library.resolve(&name)?; diff --git a/crates/horizon-client/src/options_file.rs b/crates/horizon-client/src/options_file.rs index 339bb0fdf..49c94613f 100644 --- a/crates/horizon-client/src/options_file.rs +++ b/crates/horizon-client/src/options_file.rs @@ -159,7 +159,7 @@ static TABLE: LazyLock> = LazyLock::new(|| { b("reducedDebugInfo", false), def("renderClouds", O(Clouds), V::Int(2)), i("cloudRange", 2, 128, 128), - i("renderDistance", 2, 32, 12), + i("renderDistance", 2, 64, 12), i("simulationDistance", 5, 32, 12), u("screenEffectScale", 1.0), def("soundDevice", O(AnyStr), V::Str(String::new())), diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index fab36defa..dca180e15 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -190,7 +190,7 @@ impl Opt { let unit = |v: f32, lo: f32, hi: f32| Kind::Slider(((v - lo) / (hi - lo)).clamp(0.0, 1.0)); match self { Opt::Fov => unit(s.fov, 30.0, 110.0), - Opt::RenderDistance => unit(s.render_distance as f32, 2.0, 32.0), + Opt::RenderDistance => unit(s.render_distance as f32, 2.0, 64.0), Opt::SimulationDistance => unit(s.simulation_distance as f32, 5.0, 32.0), Opt::Sensitivity => Kind::Slider(s.sensitivity), Opt::WheelSensitivity => unit(s.mouse_wheel_sensitivity, 0.01, 10.0), @@ -484,7 +484,7 @@ impl Opt { let step = |lo: f32, hi: f32| (lo + v * (hi - lo)).round(); match self { Opt::Fov => s.fov = step(30.0, 110.0), - Opt::RenderDistance => s.render_distance = step(2.0, 32.0) as i32, + Opt::RenderDistance => s.render_distance = step(2.0, 64.0) as i32, Opt::SimulationDistance => s.simulation_distance = step(5.0, 32.0) as i32, Opt::Sensitivity => s.sensitivity = v, Opt::WheelSensitivity => s.mouse_wheel_sensitivity = 0.01 + v * 9.99, @@ -2356,6 +2356,25 @@ mod tests { use super::*; use crate::ui_theme::{MenuStyle, set_style, style}; + #[test] + fn render_distance_slider_round_trips_through_extended_options_range() { + let mut s = Settings::default(); + for (position, distance) in [(0.0, 2), (0.5, 33), (1.0, 64)] { + set_slider(Opt::RenderDistance, &mut s, position); + assert_eq!(s.render_distance, distance); + let reloaded = Settings::parse(&s.serialize()); + assert_eq!(reloaded.render_distance, distance); + assert_eq!( + Opt::RenderDistance.slider_position(&reloaded), + Some(position) + ); + } + let mut file = crate::options_file::OptionsFile::default(); + file.set_text("renderDistance", "64").unwrap(); + assert!(file.set_text("renderDistance", "65").is_err()); + assert!(file.set_text("renderDistance", "1").is_err()); + assert_eq!(file.int("renderDistance"), 64); + } fn atlas() -> HudAtlas { HudAtlas::build(std::path::Path::new("/nonexistent/horizon-test-assets")).unwrap() } diff --git a/crates/horizon-client/src/poi_sections.rs b/crates/horizon-client/src/poi_sections.rs index 2116362e0..79ff0441d 100644 --- a/crates/horizon-client/src/poi_sections.rs +++ b/crates/horizon-client/src/poi_sections.rs @@ -54,12 +54,12 @@ impl PoiSections { return cached.pois.clone(); } let pois: SectionPois = if section.blocks.maybe_has(|v| kind(v).is_some()) { - let mut states = [0u16; 4096]; + let mut states = [0u32; 4096]; section.unpack(&mut states); states .iter() .enumerate() - .filter_map(|(i, &state)| kind(u32::from(state)).map(|poi| (i as u16, poi))) + .filter_map(|(i, &state)| kind(state).map(|poi| (i as u16, poi))) .collect() } else { Arc::from([]) diff --git a/crates/horizon-client/src/random_tick_sections.rs b/crates/horizon-client/src/random_tick_sections.rs index 97a1f02fa..cd06f2ac9 100644 --- a/crates/horizon-client/src/random_tick_sections.rs +++ b/crates/horizon-client/src/random_tick_sections.rs @@ -134,7 +134,7 @@ mod tests { let mut chunk = Chunk::empty(0, 0); for i in [0, 9, SECTIONS - 1] { Arc::make_mut(&mut chunk.sections[i]).blocks = - Section::from_blocks(&std::array::from_fn(|j| (j % 300) as u16)); + Section::from_blocks(&std::array::from_fn(|j| (j % 300) as u32)); } let chunk = Arc::new(chunk); let mut cache = RandomTickSections::default(); @@ -176,12 +176,9 @@ mod tick_parity { } fn set_raw(&mut self, pos: BlockPos, state: BlockStateId) -> Option { let chunk = self.chunks.get_mut(&(pos.x >> 4, pos.z >> 4))?; - Some(BlockStateId(u32::from(Arc::make_mut(chunk).set( - pos.x, - pos.y, - pos.z, - state.0 as u16, - )))) + Some(BlockStateId( + Arc::make_mut(chunk).set(pos.x, pos.y, pos.z, state.0), + )) } fn is_loaded(&self, pos: BlockPos) -> bool { self.chunks.contains_key(&(pos.x >> 4, pos.z >> 4)) @@ -233,7 +230,7 @@ mod tick_parity { // Every sampled block can run real behaviour and change to dirt. for i in [4, 7] { Arc::make_mut(&mut c.sections[i]).blocks = - crate::chunk::Section::from_blocks(&[grass.0 as u16; 4096]); + crate::chunk::Section::from_blocks(&[grass.0; 4096]); } Arc::new(c) }; diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index 398a340a2..6c1564c5a 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -16,7 +16,7 @@ use crate::{ Result, biome::Biomes, - chunk::{BIOME_STRATEGY, BLOCK_STRATEGY, Chunk, Light, MIN_Y, SECTIONS}, + chunk::{BIOME_STRATEGY, Chunk, Light, MIN_Y, SECTIONS, block_strategy}, streaming::ChunkGenerator, }; use horizon_core::{ @@ -238,7 +238,7 @@ pub struct SaveTables { default_state: Vec, block_entity: Vec, heightmaps: crate::assets::Properties, - air: u16, + air: u32, } impl SaveTables { pub fn new(assets: &crate::assets::Assets) -> Self { @@ -266,9 +266,7 @@ impl SaveTables { default_state, block_entity: Vec::new(), heightmaps: assets.properties.clone(), - air: registry - .default_state("minecraft:air") - .map_or(0, |s| s.0 as u16), + air: registry.default_state("minecraft:air").map_or(0, |s| s.0), } } /// Which states carry a block entity (`BlockFacts`), to drop entries for @@ -1035,8 +1033,8 @@ fn decode_column( } } } - let air = registry.default_state("minecraft:air")?.0 as u16; - let mut cache: HashMap = HashMap::new(); + let air = registry.default_state("minecraft:air")?.0; + let mut cache: HashMap = HashMap::new(); let Some(Tag::List(sections)) = tag.get("sections") else { return Some(chunk); }; @@ -1051,7 +1049,7 @@ fn decode_column( if let Some(states) = section.get("block_states") && let Some(Tag::List(palette)) = states.get("palette") { - let ids: Vec = palette + let palette: Vec = palette .iter() .map(|p| { let state = BlockState::from_nbt(p).ok(); @@ -1074,7 +1072,7 @@ fn decode_column( registry.id(&full).or(Some(default)) }) }) - .map_or(air, |id| id.0 as u16) + .map_or(air, |id| id.0) }) }) .collect(); @@ -1085,9 +1083,8 @@ fn decode_column( } _ => None, }; - let palette: Vec = ids.iter().map(|&id| u32::from(id)).collect(); if let Ok(blocks) = - PalettedContainer::from_packed(BLOCK_STRATEGY, palette, data.as_deref()) + PalettedContainer::from_packed(block_strategy(), palette, data.as_deref()) { target.blocks = blocks; } @@ -2789,6 +2786,9 @@ impl ChunkGenerator for Persistent { chunk.entities = self.save.load_entities(x, z); Some(chunk) } + fn prepare_loaded(&self, worker: &mut G::Worker, chunk: &Chunk) { + self.inner.prepare_loaded(worker, chunk); + } fn proto(&self, worker: &mut G::Worker, x: i32, z: i32) -> crate::streaming::Proto { let mut proto = self.inner.proto(worker, x, z); if let Some(saved) = self diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 994316324..38fa33c7d 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -36,6 +36,9 @@ pub trait ChunkGenerator: Send + Sync + 'static { fn load(&self, _x: i32, _z: i32) -> Option { None } + /// What a loaded chunk needs from the generator before the game uses it, + /// on the worker that loaded it. + fn prepare_loaded(&self, _worker: &mut Self::Worker, _chunk: &Chunk) {} fn proto(&self, worker: &mut Self::Worker, x: i32, z: i32) -> Proto; fn decorates(&self) -> bool { false @@ -261,25 +264,34 @@ impl GenerationScan { } { let mut wanted = Vec::new(); - let areas: Vec<((i32, i32), i32)> = - std::iter::once((input.ranges.center, input.ranges.radius)) - .chain(input.ranges.extra.iter().copied()) - .collect(); - for (center, radius) in areas { + // Each area's square of columns, as one run per row. Players' + // areas overlap (a server's players often stand together), so + // the runs of a row are merged and each column is looked at once. + let mut runs: Vec<(i32, i32, i32)> = Vec::new(); + for (center, radius) in std::iter::once((input.ranges.center, input.ranges.radius)) + .chain(input.ranges.extra.iter().copied()) + { let r = radius + margin.max(reach + 1); - for dz in -r..=r { - for dx in -r..=r { - let (x, z) = (center.0 + dx, center.1 + dz); - if !input.ranges.requested(x, z, input.decorates) - || input.world.chunks.contains_key(&(x, z)) - || input.generating.contains(&(x, z)) - || input.protos.contains_key(&(x, z)) - || input.finishing.contains(&(x, z)) - { - continue; - } - wanted.push((input.ranges.distance(x, z), x, z)); + runs.extend((center.1 - r..=center.1 + r).map(|z| (z, center.0 - r, center.0 + r))); + } + runs.sort_unstable(); + let mut runs = runs.into_iter().peekable(); + while let Some((z, x0, mut x1)) = runs.next() { + while let Some((_, _, next_x1)) = + runs.next_if(|&(nz, nx0, _)| nz == z && nx0 <= x1.saturating_add(1)) + { + x1 = x1.max(next_x1); + } + for x in x0..=x1 { + if !input.ranges.requested(x, z, input.decorates) + || input.world.chunks.contains_key(&(x, z)) + || input.generating.contains(&(x, z)) + || input.protos.contains_key(&(x, z)) + || input.finishing.contains(&(x, z)) + { + continue; } + wanted.push((input.ranges.distance(x, z), x, z)); } } // Forced chunks and their generation neighbourhood. @@ -369,7 +381,10 @@ impl Streaming { }; let done = match job { Job::Generate(x, z) => match generator.load(x, z) { - Some(chunk) => Done::Generated(chunk), + Some(chunk) => { + generator.prepare_loaded(&mut worker, &chunk); + Done::Generated(chunk) + } None => { let proto = generator.proto(&mut worker, x, z); let decorated = generator.is_decorated(&proto); @@ -1563,6 +1578,38 @@ mod tests { } } + #[test] + fn generation_scan_looks_at_overlapping_areas_once() { + // A server's players: many areas around one spot, some apart, one + // touching another only at a corner, and part of the land present. + for decorates in [false, true] { + let mut f = GenerationFixture::new(10, decorates); + for i in 0..40 { + f.extra.push(((i % 7 - 3, i / 7 - 3), 19)); + } + f.extra.push(((200, -150), 19)); + f.extra.push(((243, -107), 19)); + f.extra.push(((-60, 5), 4)); + f.forced.insert((90, 90)); + let chunk = Arc::new(Chunk::empty(0, 0)); + for x in -40i32..40 { + for z in -40i32..40 { + match (x * 7 + z * 13).rem_euclid(5) { + 0 => drop(f.world.chunks.insert((x, z), chunk.clone())), + 1 => drop(f.protos.insert((x, z), None)), + 2 if x % 3 == 0 => drop(f.generating.insert((x, z))), + _ => {} + } + } + } + assert!(baseline_candidates(f.input(), usize::MAX).len() > 1000); + for capacity in [usize::MAX, f.generating.len() + 7] { + let mut gate = GenerationScan::default(); + assert!(f.assert_same(&mut gate, capacity)); + } + } + } + #[test] #[ignore = "reproducible actual generation scheduler benchmark; run with --ignored --nocapture"] fn generation_scan_benchmark() { diff --git a/crates/horizon-client/src/terrain.wgsl b/crates/horizon-client/src/terrain.wgsl index 4689e7d2b..dad173efd 100644 --- a/crates/horizon-client/src/terrain.wgsl +++ b/crates/horizon-client/src/terrain.wgsl @@ -31,6 +31,10 @@ struct Globals { @group(0) @binding(5) var sprites_1: texture_2d_array; @group(0) @binding(6) var sprites_2: texture_2d_array; @group(0) @binding(7) var sprites_3: texture_2d_array; +@group(0) @binding(8) var sprites_4: texture_2d_array; +@group(0) @binding(9) var sprites_5: texture_2d_array; +@group(0) @binding(10) var sprites_6: texture_2d_array; +@group(0) @binding(11) var sprites_7: texture_2d_array; // How many sprite arrays hold layers (a pipeline constant): 1 for vanilla, // which then samples exactly as before. override SPRITE_ARRAYS: u32 = 1u; @@ -47,7 +51,11 @@ fn sprite(uv: vec2, layer: u32) -> vec4 { case 0u: { return textureSampleGrad(sprites, sprite_sampler, uv, l, dx, dy); } case 1u: { return textureSampleGrad(sprites_1, sprite_sampler, uv, l, dx, dy); } case 2u: { return textureSampleGrad(sprites_2, sprite_sampler, uv, l, dx, dy); } - default: { return textureSampleGrad(sprites_3, sprite_sampler, uv, l, dx, dy); } + case 3u: { return textureSampleGrad(sprites_3, sprite_sampler, uv, l, dx, dy); } + case 4u: { return textureSampleGrad(sprites_4, sprite_sampler, uv, l, dx, dy); } + case 5u: { return textureSampleGrad(sprites_5, sprite_sampler, uv, l, dx, dy); } + case 6u: { return textureSampleGrad(sprites_6, sprite_sampler, uv, l, dx, dy); } + default: { return textureSampleGrad(sprites_7, sprite_sampler, uv, l, dx, dy); } } } diff --git a/crates/horizon-client/src/testgen.rs b/crates/horizon-client/src/testgen.rs index e0a5d49b3..6268a8b6c 100644 --- a/crates/horizon-client/src/testgen.rs +++ b/crates/horizon-client/src/testgen.rs @@ -4,24 +4,24 @@ use crate::{chunk::Chunk, streaming::ChunkGenerator}; use horizon_world::registry::BlockRegistry; pub struct TestTerrain { - stone: u16, - dirt: u16, - grass: u16, - water: u16, - sand: u16, - bedrock: u16, - log: u16, - leaves: u16, - short_grass: u16, + stone: u32, + dirt: u32, + grass: u32, + water: u32, + sand: u32, + bedrock: u32, + log: u32, + leaves: u32, + short_grass: u32, plains: u16, } impl TestTerrain { pub fn new(registry: &BlockRegistry, biomes: &crate::biome::Biomes) -> crate::Result { - let id = |name: &str| -> crate::Result { + let id = |name: &str| -> crate::Result { Ok(registry .default_state(name) .ok_or_else(|| format!("missing {name}"))? - .0 as u16) + .0) }; Ok(Self { stone: id("minecraft:stone")?, @@ -41,7 +41,7 @@ impl TestTerrain { ] .into(), })? - .0 as u16, + .0, short_grass: id("minecraft:short_grass")?, plains: biomes.id("minecraft:plains"), }) diff --git a/crates/horizon-client/src/vanillagen.rs b/crates/horizon-client/src/vanillagen.rs index fc18e8404..657817bb9 100644 --- a/crates/horizon-client/src/vanillagen.rs +++ b/crates/horizon-client/src/vanillagen.rs @@ -119,7 +119,7 @@ impl VanillaTerrain { chunk.terrain.min_y().max(MIN_Y), (chunk.terrain.min_y() + chunk.terrain.height()).min(MIN_Y + HEIGHT), ); - let mut values = Box::new([0u16; 4096]); + let mut values = Box::new([0u32; 4096]); for si in 0..SECTIONS as i32 { let base = MIN_Y + si * 16; if base + 16 <= lo || base >= hi { @@ -130,7 +130,7 @@ impl VanillaTerrain { for y in base.max(lo)..(base + 16).min(hi) { for z in 0..16 { for x in 0..16 { - let id = chunk.block(bx + x, y, bz + z).0 as u16; + let id = chunk.block(bx + x, y, bz + z).0; values[((y & 15) << 8 | z << 4 | x) as usize] = id; any |= id != 0; } @@ -265,6 +265,19 @@ impl ChunkGenerator for VanillaTerrain { fn worker(&self) -> GeneratorWorker { self.generator.worker() } + /// The structure starts a saved chunk references, as vanilla reads them + /// with the chunk: built here, on the loading worker, into the + /// generator's cache, rather than on the game thread the first time + /// spawning or a mob asks which structures hold a position (a village + /// start took 70 to 110 ms there, one tick each, after a world loaded). + fn prepare_loaded(&self, worker: &mut GeneratorWorker, chunk: &Chunk) { + let mut starts: Vec<(i32, i32)> = chunk.structure_refs.iter().map(|(_, at)| *at).collect(); + starts.sort_unstable(); + starts.dedup(); + for (x, z) in starts { + worker.structure_starts(ChunkPos::new(x, z)); + } + } fn proto(&self, worker: &mut GeneratorWorker, x: i32, z: i32) -> Proto { match worker.generate_proto(ChunkPos::new(x, z)) { Ok(chunk) => Box::new(VanillaProto::Ready(Box::new(chunk))), diff --git a/crates/horizon-client/src/world.rs b/crates/horizon-client/src/world.rs index 44e442ec1..05b89234c 100644 --- a/crates/horizon-client/src/world.rs +++ b/crates/horizon-client/src/world.rs @@ -21,7 +21,7 @@ pub struct World { pub dirty_light: crate::fast_hash::FxHashSet<(i32, i32)>, /// Edits on lit chunks (position, old state, new state), relit /// incrementally by streaming. - pub light_edits: Vec<((i32, i32, i32), u16, u16)>, + pub light_edits: Vec<((i32, i32, i32), u32, u32)>, /// Saved entities of chunks that just loaded, spawned by the game. pub loaded_entities: Vec<((i32, i32), Vec)>, /// Chunks changed since the last save. @@ -36,7 +36,7 @@ pub struct World { /// blocks, which need a block entity). pub watched: Vec, /// Writes of watched states: position, previous and new state. - pub watched_writes: Vec<((i32, i32, i32), u16, u16)>, + pub watched_writes: Vec<((i32, i32, i32), u32, u32)>, /// `ChunkAccess.inhabitedTime` of resident chunks: ticks a player spent /// within spawning range (local difficulty). pub inhabited: HashMap<(i32, i32), i64>, @@ -198,12 +198,11 @@ impl World { if y < MIN_Y || y >= MIN_Y + (SECTIONS as i32) * 16 { return Err("write outside world height".into()); } - let previous = Arc::make_mut(chunk).set(x & 15, y, z & 15, state.0 as u16); - if u32::from(previous) != state.0 { - let watches = |id: u16| self.watched.get(usize::from(id)).copied().unwrap_or(false); - if watches(previous) || watches(state.0 as u16) { - self.watched_writes - .push(((x, y, z), previous, state.0 as u16)); + let previous = Arc::make_mut(chunk).set(x & 15, y, z & 15, state.0); + if previous != state.0 { + let watches = |id: u32| self.watched.get(id as usize).copied().unwrap_or(false); + if watches(previous) || watches(state.0) { + self.watched_writes.push(((x, y, z), previous, state.0)); } self.revision = self.revision.wrapping_add(1); if let Some(log) = &mut self.block_log { @@ -225,7 +224,7 @@ impl World { } let _ = sy; if self.chunks.get(&(x >> 4, z >> 4)).is_some_and(|c| c.lit) { - self.light_edits.push(((x, y, z), previous, state.0 as u16)); + self.light_edits.push(((x, y, z), previous, state.0)); } else { for dz in -1..=1 { for dx in -1..=1 { diff --git a/crates/horizon-client/tests/lan_bot.rs b/crates/horizon-client/tests/lan_bot.rs index 36f53444c..432ea044f 100644 --- a/crates/horizon-client/tests/lan_bot.rs +++ b/crates/horizon-client/tests/lan_bot.rs @@ -38,7 +38,7 @@ fn resources() -> Option { root.join("items.json").exists().then_some(root) } -const STONE: u16 = 1; +const STONE: u32 = 1; fn flat_world() -> World { let mut world = World::new(BlockStateId(0), 0); @@ -163,7 +163,7 @@ struct Server { hub: Hub, world: World, next_entity: u32, - placed: Vec<(u32, [i32; 3], u16, i32)>, + placed: Vec<(u32, [i32; 3], u32, i32)>, } impl Server { fn tick(&mut self, entities: &[EntityState]) { @@ -193,6 +193,7 @@ impl Server { clock_rate: 1.0, tick_rate: 20.0, tick_frozen: false, + collision: None, }; let events = self.hub.tick(&mut ctx); for event in events { @@ -202,7 +203,7 @@ impl Server { if let S::UseItemOn(p) = *packet { let at = [p.pos[0], p.pos[1] + 1, p.pos[2]]; ctx.world - .set(at[0], at[1], at[2], BlockStateId(u32::from(STONE))) + .set(at[0], at[1], at[2], BlockStateId(STONE)) .unwrap(); self.placed.push((conn, at, STONE, p.sequence)); self.hub.ack(conn, p.sequence); @@ -348,7 +349,7 @@ fn status_and_join() { chunks += 1; let sections = l.chunk.sections(24, 16, 7).map_err(|e| e.to_string())?; // The floor is in the lowest section. - assert_eq!(sections[0].states.get(4 * 256), i32::from(STONE)); + assert_eq!(sections[0].states.get(4 * 256), STONE as i32); assert_eq!(sections[0].non_empty, 256); assert_eq!(sections[1].non_empty, 0); assert!(!l.light.sky.is_empty()); @@ -394,7 +395,7 @@ fn status_and_join() { assert!(chunks >= 9, "only {chunks} chunks"); assert_eq!( block_update, - Some(([9, MIN_Y + 5, 9], i32::from(STONE))), + Some(([9, MIN_Y + 5, 9], STONE as i32)), "placed block was not broadcast" ); Ok(bot.name) diff --git a/crates/horizon-client/tests/streaming_meshes.rs b/crates/horizon-client/tests/streaming_meshes.rs index 74edd5049..6804d197e 100644 --- a/crates/horizon-client/tests/streaming_meshes.rs +++ b/crates/horizon-client/tests/streaming_meshes.rs @@ -107,7 +107,7 @@ fn columns_loaded_past_the_render_distance_mesh_when_they_come_in_range() { /// A one-block cave under the sky: a stone floor at y 62 with a glowstone, /// and a roof at y 64 with holes, so that every light change in the cave is /// sampled by a face. -fn roofed(x: i32, z: i32, stone: u16, glowstone: u16) -> Chunk { +fn roofed(x: i32, z: i32, stone: u32, glowstone: u32) -> Chunk { let mut chunk = Chunk::empty(x, z); for lz in 0..16 { for lx in 0..16 { @@ -143,7 +143,7 @@ fn relit_columns_keep_every_mesh_current() { biomes: Arc::new(Biomes::load(&root).unwrap()), assets: assets.clone(), }); - let state = |name: &str| assets.registry.default_state(name).unwrap().0 as u16; + let state = |name: &str| assets.registry.default_state(name).unwrap().0; let (stone, glowstone, torch) = ( state("minecraft:stone"), state("minecraft:glowstone"), @@ -185,9 +185,7 @@ fn relit_columns_keep_every_mesh_current() { let (bx, bz) = (16 + lx, lz); world.set(bx, 64, bz, BlockStateId(0)).unwrap(); if i % 3 == 0 { - world - .set(bx, 63, bz, BlockStateId(u32::from(torch))) - .unwrap(); + world.set(bx, 63, bz, BlockStateId(torch)).unwrap(); } } } diff --git a/crates/horizon-client/tests/wide_states.rs b/crates/horizon-client/tests/wide_states.rs new file mode 100644 index 000000000..690732ceb --- /dev/null +++ b/crates/horizon-client/tests/wide_states.rs @@ -0,0 +1,40 @@ +//! Block states past vanilla's 16-bit global palette (docs/modpacks.md): +//! content mods can take the registry past 65,536 states. Its own test binary, +//! since the palette width is set once for the whole process. + +use horizon_client::chunk::{Chunk, Section, block_strategy, set_block_states}; + +#[test] +fn sections_hold_states_past_sixteen_bits() { + // Vanilla's width until the registry needs more (one test: the width is + // global to the process). + assert_eq!(block_strategy().global_bits, 16); + set_block_states(35_723); + assert_eq!(block_strategy().global_bits, 16); + set_block_states(100_000); + assert_eq!(block_strategy().global_bits, 17); + + // A few states past 65,536 stay in the section's own palette. + let mut chunk = Chunk::empty(0, 0); + chunk.set(1, 70, 2, 70_000); + chunk.set(3, 70, 4, 99_999); + assert_eq!(chunk.get(1, 70, 2), 70_000); + assert_eq!(chunk.get(3, 70, 4), 99_999); + assert_eq!(chunk.get(0, 70, 0), 0); + + // More than 256 distinct states take the global palette at 17 bits. + let mut values = [0u32; 4096]; + for (i, v) in values.iter_mut().enumerate() { + *v = 65_000 + (i as u32 % 1000) * 30; + } + let mut section = Section { + blocks: Section::from_blocks(&values), + ..Section::default() + }; + let mut out = [0u32; 4096]; + section.unpack(&mut out); + assert_eq!(out, values); + section.set(17, 99_998); + assert_eq!(section.get(17), 99_998); + assert_eq!(section.get(18), values[18]); +} diff --git a/crates/horizon-port-pack/Cargo.toml b/crates/horizon-port-pack/Cargo.toml index 3b903ad03..775a853a8 100644 --- a/crates/horizon-port-pack/Cargo.toml +++ b/crates/horizon-port-pack/Cargo.toml @@ -7,6 +7,8 @@ rust-version = "1.86" description = "Port a Java modpack to Horizon: import, inventory, classification and data carry-over (docs/modpacks.md)" [dependencies] +# Content path rules only (no runtime): what the game's content loader accepts. +horizon-mods = { path = "../horizon-mods", default-features = false } serde = { version = "1", features = ["derive"] } serde_json = "1" sha1 = "0.10" @@ -14,8 +16,5 @@ toml = "1.1.6" ureq = { version = "2", default-features = false, features = ["tls"] } zip = { version = "2", default-features = false, features = ["deflate"] } -[dev-dependencies] -horizon-mods = { path = "../horizon-mods", default-features = false } - [lints] workspace = true diff --git a/crates/horizon-port-pack/src/carry.rs b/crates/horizon-port-pack/src/carry.rs index a637d6246..dfe4d3dbc 100644 --- a/crates/horizon-port-pack/src/carry.rs +++ b/crates/horizon-port-pack/src/carry.rs @@ -19,6 +19,10 @@ pub struct Report { /// Files outside what a content mod may hold (other namespaces, vanilla /// data other than tags), kept in `outside/` for review. pub outside: Vec, + /// Files of the mod's own content the game would refuse, kept in + /// `outside/` too, with why: a path vanilla does not read (upper case), + /// JSON that does not parse, a vanilla tag the mod replaces. + pub unreadable: BTreeMap, pub recipes_upgraded: usize, pub loot_tables_upgraded: usize, /// Worldgen and structure files (`data//worldgen/`, `structure/`). @@ -291,6 +295,32 @@ fn place(name: &str, id: &str) -> Option { Some(ns == id || shared_tags) } +/// Why the game would refuse a file of the mod's own content, if it would. +/// Vanilla skips such files one by one (an invalid resource path, a data +/// file that does not parse); Horizon's content loader refuses the whole +/// set, so they stay out of the carry. +fn unreadable(name: &str, bytes: &mut Vec) -> Option { + if let Err(e) = horizon_mods::content::check_path(name) { + return Some(e); + } + if !(name.ends_with(".json") || name.ends_with(".mcmeta")) { + return None; + } + // A byte-order mark: Horizon's JSON readers take the file without it. + if bytes.starts_with(b"\xEF\xBB\xBF") { + bytes.drain(..3); + } + let value: Value = match serde_json::from_slice(bytes) { + Ok(v) => v, + Err(e) => return Some(format!("does not parse as JSON: {e}")), + }; + let shared_tag = name.starts_with("data/minecraft/tags/") || name.starts_with("data/c/tags/"); + if shared_tag && value.get("replace").and_then(Value::as_bool) == Some(true) { + return Some("replaces a shared tag; a content mod may only add to it".into()); + } + None +} + /// Carries `jar` over into `out/carry/`. With the oracle's `facts`, blocks /// and items are the ones the mod registers, with the templates and numbers /// the facts give; `allowed` says which vanilla blocks the game has. @@ -334,6 +364,15 @@ pub fn carry( entry .read_to_end(&mut bytes) .map_err(|e| format!("{name}: {e}"))?; + let unreadable = if keep { + unreadable(&name, &mut bytes) + } else { + None + }; + let keep = keep && unreadable.is_none(); + if let Some(why) = unreadable { + report.unreadable.insert(name.clone(), why); + } let dest = if keep { report.copied += 1; inside.join(&name) @@ -716,6 +755,17 @@ mod tests { r#"{"values": ["gem:gem_block"]}"#, ), ("data/c/tags/item/gems.json", r#"{"values": ["gem:gem"]}"#), + // What the game would refuse: vanilla skips these files alone. + ("assets/gem/lang/ry_UA.json", r#"{"item.gem.gem": "Gem"}"#), + ("data/gem/tags/item/empty.json", ""), + ( + "data/minecraft/tags/worldgen/biome/has_structure/village_plains.json", + r#"{"replace": true, "values": []}"#, + ), + ( + "assets/gem/lang/de_de.json", + "\u{feff}{\"item.gem.gem\": \"Juwel\"}", + ), ]; for (name, text) in files { zip.start_file(*name, zip::write::SimpleFileOptions::default()) @@ -732,8 +782,23 @@ mod tests { assert_eq!(info.census.classes, 1); let out = dir.join("work/mods/gem"); let report = carry(&jar, &info, &out, None, &|_| true).unwrap(); - assert_eq!(report.copied, 12); - assert!(report.outside.is_empty()); + assert_eq!(report.copied, 13); + let unreadable: Vec<&str> = report.unreadable.keys().map(String::as_str).collect(); + assert_eq!( + unreadable, + [ + "assets/gem/lang/ry_UA.json", + "data/gem/tags/item/empty.json", + "data/minecraft/tags/worldgen/biome/has_structure/village_plains.json", + ] + ); + assert_eq!(report.outside.len(), 3); + assert!(!out.join("carry/data/gem/tags/item/empty.json").exists()); + // A byte-order mark is dropped, the file kept. + assert_eq!( + fs::read(out.join("carry/assets/gem/lang/de_de.json")).unwrap()[0], + b'{' + ); assert_eq!((report.recipes_upgraded, report.recipes_kept.len()), (1, 1)); assert_eq!(report.item_definitions_added, 2); assert_eq!(report.blocks["gem_door"], "minecraft:oak_door"); diff --git a/crates/horizon-port-pack/src/fetch.rs b/crates/horizon-port-pack/src/fetch.rs new file mode 100644 index 000000000..011e99e1f --- /dev/null +++ b/crates/horizon-port-pack/src/fetch.rs @@ -0,0 +1,389 @@ +//! Step 1b, the jars: for the mods the player asked to port, their own +//! download of each jar into `/jars/`. Only from where the pack itself +//! points (a Modrinth pack's download URLs) or the same file on Modrinth's +//! public CDN (found by its SHA-1), and only kept when the hash matches; or +//! copied from a folder the player already has (an installed instance, a +//! downloads folder). CurseForge's own downloads need an API key, so a file +//! only CurseForge has is listed as needing one. `/fetch.json` says +//! what happened to each file. +use crate::{ + import::{Pack, PackFile, sha1_hex}, + inventory::Record, + modrinth::{self, Cache}, + pipeline::{Log, read_json, write_json}, +}; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use std::{ + collections::HashMap, + fs, + io::Read, + path::{Path, PathBuf}, +}; + +/// Hosts a Modrinth pack may download from (the `.mrpack` format's list). +const HOSTS: [&str; 4] = [ + "https://cdn.modrinth.com/", + "https://github.com/", + "https://raw.githubusercontent.com/", + "https://gitlab.com/", +]; +/// No mod jar is this large; a bigger answer is not one. +const MAX_JAR: u64 = 512 << 20; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum How { + /// Already in `jars/` (or the imported instance). + Present, + /// Downloaded from the pack's URL or Modrinth's CDN, hash checked. + Downloaded, + /// Copied from the player's folder. + Local, + /// Only on CurseForge, whose downloads need an API key. + NeedsKey, + NotFound, + Failed, +} + +impl How { + pub fn label(self) -> &'static str { + match self { + How::Present => "already here", + How::Downloaded => "downloaded", + How::Local => "copied from your folder", + How::NeedsKey => "needs a CurseForge key", + How::NotFound => "not found", + How::Failed => "failed", + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Outcome { + pub key: String, + pub file: String, + pub how: How, + #[serde(default, skip_serializing_if = "String::is_empty")] + pub detail: String, +} + +#[derive(Debug, Default, Serialize, Deserialize)] +pub struct Fetched { + pub files: Vec, +} + +impl Fetched { + pub fn count(&self, how: How) -> usize { + self.files.iter().filter(|o| o.how == how).count() + } + /// `/fetch.json`, empty when the step has not run. + pub fn load(work: &Path) -> Self { + read_json(&work.join("fetch.json")).unwrap_or_default() + } +} + +/// The jars of the records `want` picks that the work folder lacks. +pub fn fetch( + work: &Path, + records: &[Record], + from: Option<&Path>, + want: &dyn Fn(&Record) -> bool, + log: Log, +) -> Result { + let pack: Pack = read_json(&work.join("pack.json"))?; + let cache = Cache::load(&work.join("modrinth.json")); + let files: HashMap<&str, &PackFile> = pack.files.iter().map(|f| (f.path.as_str(), f)).collect(); + let jars = work.join("jars"); + fs::create_dir_all(&jars).map_err(|e| format!("{}: {e}", jars.display()))?; + let local = from.map(Local::index); + let wanted: Vec<&Record> = records.iter().filter(|r| want(r)).collect(); + let mut fetched = Fetched::default(); + for (i, r) in wanted.iter().enumerate() { + let Some(file) = files.get(r.path.as_str()) else { + continue; + }; + let name = file.file_name(); + let outcome = |how, detail: String| Outcome { + key: r.key.clone(), + file: name.to_owned(), + how, + detail, + }; + let dest = jars.join(name); + let sha1 = file.sha1.as_deref(); + let o = if r.jar.is_some() || matches(&dest, sha1) { + outcome(How::Present, String::new()) + } else if let Some(path) = local.as_ref().and_then(|l| l.find(file)) { + match fs::copy(&path, &dest) { + Ok(_) => outcome(How::Local, path.display().to_string()), + Err(e) => outcome(How::Failed, format!("{}: {e}", path.display())), + } + } else if let Some(url) = url_of(file, &cache) { + log(format!("{}/{} {name}", i + 1, wanted.len())); + match download(&url, sha1, &dest) { + Ok(()) => outcome(How::Downloaded, url), + Err(e) => outcome(How::Failed, e), + } + } else if pack.format == "curseforge" || file.curseforge.is_some() { + outcome( + How::NeedsKey, + "on CurseForge only: choose your installed instance, or put the jar in jars/" + .into(), + ) + } else { + outcome(How::NotFound, "no download link in the pack".into()) + }; + if matches!(o.how, How::Failed | How::NeedsKey | How::NotFound) { + log(format!("{}: {} ({})", o.key, o.how.label(), o.detail)); + } + fetched.files.push(o); + } + write_json(&work.join("fetch.json"), &fetched)?; + log(format!( + "jars: {} downloaded, {} copied, {} already here, {} need a CurseForge key, {} not found, {} failed", + fetched.count(How::Downloaded), + fetched.count(How::Local), + fetched.count(How::Present), + fetched.count(How::NeedsKey), + fetched.count(How::NotFound), + fetched.count(How::Failed) + )); + Ok(fetched) +} + +/// Whether `path` holds the file with this SHA-1 (any file when none is known). +fn matches(path: &Path, sha1: Option<&str>) -> bool { + match (fs::read(path), sha1) { + (Ok(bytes), Some(h)) => sha1_hex(&bytes).eq_ignore_ascii_case(h), + (Ok(_), None) => true, + (Err(_), _) => false, + } +} + +/// The pack's own download link, else the same file on Modrinth. +pub fn url_of(file: &PackFile, cache: &Cache) -> Option { + let allowed = |u: &&str| HOSTS.iter().any(|h| u.starts_with(h)); + if let Some(u) = file.downloads.iter().map(String::as_str).find(allowed) { + return Some(u.to_owned()); + } + let sha1 = file.sha1.as_deref()?; + cache.versions.get(sha1)?["files"] + .as_array()? + .iter() + .find(|f| f["hashes"]["sha1"].as_str() == Some(sha1))?["url"] + .as_str() + .filter(allowed) + .map(str::to_owned) +} + +/// Downloads `url` to `dest`, kept only if its SHA-1 is `sha1`. +fn download(url: &str, sha1: Option<&str>, dest: &Path) -> Result<(), String> { + let response = ureq::get(url) + .set("User-Agent", modrinth::AGENT) + .call() + .map_err(|e| format!("{url}: {e}"))?; + let mut bytes = Vec::new(); + response + .into_reader() + .take(MAX_JAR) + .read_to_end(&mut bytes) + .map_err(|e| format!("{url}: {e}"))?; + let got = sha1_hex(&bytes); + if let Some(want) = sha1.filter(|w| !w.eq_ignore_ascii_case(&got)) { + return Err(format!("{url}: SHA-1 {got}, the pack says {want}")); + } + let part = dest.with_extension("part"); + fs::write(&part, &bytes).map_err(|e| format!("{}: {e}", part.display()))?; + fs::rename(&part, dest).map_err(|e| format!("{}: {e}", dest.display())) +} + +/// The jars in a folder of the player's, by SHA-1 and by name, and what a +/// CurseForge app instance (`minecraftinstance.json`) says each +/// project's file is called. +#[derive(Default)] +struct Local { + by_sha1: HashMap, + by_name: HashMap, + curseforge: HashMap<(u64, u64), String>, +} + +impl Local { + fn index(dir: &Path) -> Local { + let mut local = Local::default(); + local.walk(dir, 3); + if let Ok(text) = fs::read_to_string(dir.join("minecraftinstance.json")) { + if let Ok(v) = serde_json::from_str::(&text) { + local.curseforge = curseforge_names(&v); + } + } + local + } + + fn walk(&mut self, dir: &Path, depth: usize) { + let Ok(entries) = fs::read_dir(dir) else { + return; + }; + for path in entries.flatten().map(|e| e.path()) { + if path.is_dir() { + if depth > 0 { + self.walk(&path, depth - 1); + } + } else if path.extension().is_some_and(|x| x == "jar") { + if let Ok(bytes) = fs::read(&path) { + self.by_sha1.insert(sha1_hex(&bytes), path.clone()); + } + if let Some(n) = path.file_name() { + self.by_name + .insert(n.to_string_lossy().into_owned(), path.clone()); + } + } + } + } + + fn find(&self, file: &PackFile) -> Option { + if let Some(h) = &file.sha1 { + return self.by_sha1.get(&h.to_ascii_lowercase()).cloned(); + } + let name = file + .curseforge + .and_then(|ids| self.curseforge.get(&ids)) + .map_or(file.file_name(), String::as_str); + self.by_name.get(name).cloned() + } +} + +/// `installedAddons[]`: (project, file) → the jar's file name. +pub fn curseforge_names(instance: &Value) -> HashMap<(u64, u64), String> { + instance["installedAddons"] + .as_array() + .into_iter() + .flatten() + .filter_map(|a| { + let file = &a["installedFile"]; + Some(( + (a["addonID"].as_u64()?, file["id"].as_u64()?), + file["fileName"].as_str()?.to_owned(), + )) + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + fn file(sha1: Option<&str>, downloads: &[&str]) -> PackFile { + PackFile { + path: "mods/a.jar".into(), + sha1: sha1.map(str::to_owned), + downloads: downloads.iter().map(|d| (*d).to_owned()).collect(), + ..PackFile::default() + } + } + + #[test] + fn links_come_from_the_pack_or_modrinth_and_only_allowed_hosts() { + let mut cache = Cache::default(); + cache.versions.insert( + "ab".into(), + json!({"files": [ + {"hashes": {"sha1": "cd"}, "url": "https://cdn.modrinth.com/other.jar"}, + {"hashes": {"sha1": "ab"}, "url": "https://cdn.modrinth.com/a.jar"}]}), + ); + let pack_link = file( + Some("ab"), + &["https://evil.example/a.jar", "https://github.com/a.jar"], + ); + assert_eq!( + url_of(&pack_link, &cache).as_deref(), + Some("https://github.com/a.jar") + ); + let modrinth = file(Some("ab"), &["https://evil.example/a.jar"]); + assert_eq!( + url_of(&modrinth, &cache).as_deref(), + Some("https://cdn.modrinth.com/a.jar") + ); + assert_eq!(url_of(&file(Some("ef"), &[]), &cache), None); + assert_eq!(url_of(&file(None, &[]), &cache), None); + } + + #[test] + fn reads_a_curseforge_instance() { + let instance = json!({"installedAddons": [ + {"addonID": 238222, "installedFile": {"id": 5101366, "fileName": "jei-1.21.1.jar"}}, + {"addonID": 1, "installedFile": {"fileName": "no-id.jar"}}]}); + let names = curseforge_names(&instance); + assert_eq!(names.len(), 1); + assert_eq!(names[&(238222, 5101366)], "jei-1.21.1.jar"); + } + + #[test] + fn copies_the_players_jars_and_lists_what_needs_a_key() { + let root = std::env::temp_dir().join(format!("horizon-fetch-{}", std::process::id())); + let _ = fs::remove_dir_all(&root); + let (work, mine) = (root.join("work"), root.join("instance/mods")); + fs::create_dir_all(&mine).unwrap(); + fs::create_dir_all(&work).unwrap(); + fs::write(mine.join("renamed.jar"), b"jar a").unwrap(); + fs::write(mine.join("cf.jar"), b"jar b").unwrap(); + fs::write( + root.join("instance/minecraftinstance.json"), + json!({"installedAddons": [{"addonID": 7, "installedFile": {"id": 8, "fileName": "cf.jar"}}]}) + .to_string(), + ) + .unwrap(); + let pack = Pack { + format: "curseforge".into(), + files: vec![ + PackFile { + path: "mods/a.jar".into(), + sha1: Some(sha1_hex(b"jar a")), + ..PackFile::default() + }, + PackFile { + path: "mods/curseforge-7-8".into(), + curseforge: Some((7, 8)), + ..PackFile::default() + }, + PackFile { + path: "mods/curseforge-9-9".into(), + curseforge: Some((9, 9)), + ..PackFile::default() + }, + ], + ..Pack::default() + }; + write_json(&work.join("pack.json"), &pack).unwrap(); + let records: Vec = pack + .files + .iter() + .map(|f| Record { + key: f.file_name().into(), + path: f.path.clone(), + ..Record::default() + }) + .collect(); + let fetched = fetch( + &work, + &records, + Some(&root.join("instance")), + &|_| true, + &|_| {}, + ) + .unwrap(); + let hows: Vec = fetched.files.iter().map(|o| o.how).collect(); + assert_eq!(hows, [How::Local, How::Local, How::NeedsKey]); + assert_eq!(fs::read(work.join("jars/a.jar")).unwrap(), b"jar a"); + assert_eq!( + fs::read(work.join("jars/curseforge-7-8")).unwrap(), + b"jar b" + ); + // A second run finds them in place. + let again = fetch(&work, &records, None, &|_| true, &|_| {}).unwrap(); + assert_eq!(again.count(How::Present), 2); + assert_eq!(Fetched::load(&work).count(How::Present), 2); + fs::remove_dir_all(&root).unwrap(); + } +} diff --git a/crates/horizon-port-pack/src/import.rs b/crates/horizon-port-pack/src/import.rs index dd712acf1..0a3aa8d06 100644 --- a/crates/horizon-port-pack/src/import.rs +++ b/crates/horizon-port-pack/src/import.rs @@ -22,8 +22,20 @@ pub struct Pack { pub files: Vec, /// Override files by top folder (`config`, `kubejs`, ...). pub overrides: BTreeMap, + /// A CurseForge zip's files by project and file id only: how many its + /// manifest lists, when the files came from the pack's own mod list. + #[serde(default, skip_serializing_if = "is_zero")] + pub curseforge_files: usize, } +fn is_zero(n: &usize) -> bool { + *n == 0 +} + +/// Where Crash Assistant keeps its list in an instance (and in the +/// overrides of packs that ship it, such as All the Mods 10). +pub const MODLIST: &str = "config/crash_assistant/modlist.json"; + #[derive(Debug, Default, Serialize, Deserialize)] pub struct PackFile { /// Path in the instance (`mods/create-1.21.1-6.0.4.jar`). @@ -83,7 +95,16 @@ pub fn import(path: &Path) -> Result { return Ok(mrpack(&index, &names(&zip))); } if let Some(manifest) = read_json(&mut zip, "manifest.json")? { - return Ok(curseforge(&manifest, &names(&zip))); + let mut pack = curseforge(&manifest, &names(&zip)); + // The manifest names files only by CurseForge ids, which need a key + // to resolve; the pack's own mod list names them and gives their + // SHA-1s, so Modrinth can answer for most. + let root = manifest["overrides"].as_str().unwrap_or("overrides"); + if let Some(list) = read_json(&mut zip, &format!("{root}/{MODLIST}"))? { + pack.curseforge_files = pack.files.len(); + pack.files = modlist(&list)?.files; + } + return Ok(pack); } Err(format!( "{}: neither modrinth.index.json nor manifest.json", @@ -103,7 +124,7 @@ fn read_json(zip: &mut zip::ZipArchive, name: &str) -> Result Pack { names, &["overrides", "client-overrides", "server-overrides"], ), + ..Pack::default() } } @@ -197,6 +219,7 @@ pub fn curseforge(manifest: &Value, names: &[String]) -> Pack { loader_version: loader_version.to_owned(), files, overrides: overrides(names, &[root]), + ..Pack::default() } } @@ -418,4 +441,36 @@ mod tests { assert_eq!(p.files[0].curseforge, Some((238222, 5101366))); assert_eq!(p.overrides["kubejs"], 1); } + + #[test] + fn a_curseforge_zip_takes_names_from_its_mod_list() { + use std::io::Write; + let dir = std::env::temp_dir().join(format!("horizon-cf-zip-{}", std::process::id())); + fs::create_dir_all(&dir).unwrap(); + let path = dir.join("pack.zip"); + let mut zip = zip::ZipWriter::new(fs::File::create(&path).unwrap()); + let opts = zip::write::SimpleFileOptions::default(); + let manifest = json!({"name": "Pack", "overrides": "overrides", + "minecraft": {"version": "1.21.1", "modLoaders": [{"id": "neoforge-21.1.259"}]}, + "files": [{"projectID": 1, "fileID": 2}, {"projectID": 3, "fileID": 4}]}); + zip.start_file("manifest.json", opts).unwrap(); + zip.write_all(manifest.to_string().as_bytes()).unwrap(); + zip.start_file(format!("overrides/{MODLIST}"), opts) + .unwrap(); + let list = json!({"mcw-doors-1.1.2.jar": {"jarName": "mcw-doors-1.1.2.jar", + "modId": "mcwdoors", "modrinthHash": "0123456789abcdef0123456789abcdef01234567"}}); + // Crash Assistant writes a byte order mark. + zip.write_all(format!("\u{feff}{list}").as_bytes()).unwrap(); + zip.finish().unwrap(); + let pack = import(&path).unwrap(); + assert_eq!( + (pack.format.as_str(), pack.name.as_str()), + ("curseforge", "Pack") + ); + assert_eq!(pack.curseforge_files, 2); + assert_eq!(pack.files.len(), 1); + assert_eq!(pack.files[0].path, "mods/mcw-doors-1.1.2.jar"); + assert_eq!(pack.overrides["config"], 1); + fs::remove_dir_all(&dir).unwrap(); + } } diff --git a/crates/horizon-port-pack/src/lib.rs b/crates/horizon-port-pack/src/lib.rs new file mode 100644 index 000000000..ecd61956c --- /dev/null +++ b/crates/horizon-port-pack/src/lib.rs @@ -0,0 +1,17 @@ +//! Port a Java modpack to Horizon (`docs/modpacks.md`): the steps behind the +//! `horizon-port-pack` command, and the launcher's "Import modpack". +//! +//! Every step reads and writes files in a work folder (`pipeline`), so it can +//! be rerun, resumed and inspected. The work folder belongs to the user: the +//! jars they downloaded and what is carried over from them stay there and +//! are never uploaded or committed. +pub mod carry; +pub mod classify; +pub mod fetch; +pub mod import; +pub mod inventory; +pub mod jar; +pub mod modrinth; +pub mod oracle; +pub mod pipeline; +pub mod report; diff --git a/crates/horizon-port-pack/src/main.rs b/crates/horizon-port-pack/src/main.rs index 3e6cdf4d7..e766a442d 100644 --- a/crates/horizon-port-pack/src/main.rs +++ b/crates/horizon-port-pack/src/main.rs @@ -6,57 +6,77 @@ //! `/modrinth.json`), the jars in the instance or `/jars/`, //! classes, tiers and dependency layers in `/inventory.json` and //! `inventory.csv`. +//! - `fetch [--from ] [...]`: the jars of the +//! portable mods (content, data tier; or the ones named) into +//! `/jars/`: copied from the player's folder, or downloaded from the +//! pack's own links or Modrinth's CDN, hash checked. What only CurseForge +//! has needs a key and is listed in `/fetch.json`. //! - `carry [--resources ] [...]`: the content //! mods' data and assets into `/mods//carry/`, a folder mod for //! local use. With the NeoForge oracle's `/oracle/facts.json` //! (`tools/neoforge_oracle.py`), blocks and items come from its facts; //! `--resources` limits templates to the blocks Horizon's tables have. -//! //! - `report `: the coverage report, per mod ported, partial, not yet //! or not needed with the reason, in `/coverage.json` and //! `coverage.md`. +//! - `port [--from ] [--resources ]`: every +//! step for the portable mods, as the launcher's "Import modpack" runs it. //! //! The work folder belongs to the user: jars and carried-over files stay //! there and are never uploaded or committed. -mod carry; -mod classify; -mod import; -mod inventory; -mod jar; -mod modrinth; -mod oracle; -mod report; +use horizon_port_pack::{fetch, inventory::Record, pipeline}; +use std::{ + path::{Path, PathBuf}, + process::ExitCode, +}; -use std::{fs, path::Path, process::ExitCode}; - -const USAGE: &str = "usage: horizon-port-pack import | inventory [--offline] | carry [--resources ] [...] | report "; +const USAGE: &str = "usage: horizon-port-pack import | inventory [--offline] | fetch [--from ] [...] | carry [--resources ] [...] | report | port [--from ] [--resources ]"; fn main() -> ExitCode { - let args: Vec = std::env::args().skip(1).collect(); - let path = |i: usize| args.get(i).map(Path::new).ok_or(USAGE.to_owned()); - let result = match args.first().map(String::as_str) { - Some("import") => path(1) - .and_then(|p| Ok((p, path(2)?))) - .and_then(|(p, w)| import(p, w)), - Some("inventory") => { - path(1).and_then(|w| inventory(w, args.iter().any(|a| a == "--offline"))) + let mut args: Vec = std::env::args().skip(1).collect(); + let result = (|| { + let from = option(&mut args, "--from")?; + let resources = option(&mut args, "--resources")?; + let offline = take_flag(&mut args, "--offline"); + let path = |i: usize| args.get(i).map(Path::new).ok_or(USAGE.to_owned()); + let rest = || args.get(2..).unwrap_or_default().to_vec(); + let log = &|line: String| println!("{line}"); + match args.first().map(String::as_str) { + Some("import") => pipeline::import(path(1)?, path(2)?, log).map(drop), + Some("inventory") => pipeline::inventory(path(1)?, offline, log).map(drop), + Some("fetch") => { + let work = path(1)?; + let records: Vec = pipeline::read_json(&work.join("inventory.json"))?; + let only = rest(); + let want = |r: &Record| { + if only.is_empty() { + pipeline::portable(r) + } else { + only.contains(&r.key) + } + }; + fetch::fetch(work, &records, from.as_deref(), &want, log).map(drop) + } + Some("carry") => { + pipeline::carry(path(1)?, resources.as_deref(), &rest(), log).map(drop) + } + Some("report") => { + let work = path(1)?; + pipeline::report(work, log)?; + println!("{}", work.join("coverage.md").display()); + Ok(()) + } + Some("port") => { + let work = path(2)?; + pipeline::import(path(1)?, work, log)?; + pipeline::inventory(work, offline, log)?; + pipeline::port(work, resources.as_deref(), from.as_deref(), log)?; + println!("{}", work.join("coverage.md").display()); + Ok(()) + } + _ => Err(USAGE.to_owned()), } - Some("carry") => path(1).and_then(|w| { - let mut rest = args[2..].to_vec(); - let resources = match rest.iter().position(|a| a == "--resources") { - Some(i) if i + 1 < rest.len() => { - let r = rest.remove(i + 1); - rest.remove(i); - Some(std::path::PathBuf::from(r)) - } - Some(_) => return Err(USAGE.to_owned()), - None => None, - }; - carry(w, resources.as_deref(), &rest) - }), - Some("report") => path(1).and_then(report), - _ => Err(USAGE.to_owned()), - }; + })(); match result { Ok(()) => ExitCode::SUCCESS, Err(e) => { @@ -66,158 +86,21 @@ fn main() -> ExitCode { } } -fn write_json(path: &Path, value: &impl serde::Serialize) -> Result<(), String> { - let text = serde_json::to_string_pretty(value).map_err(|e| e.to_string())?; - fs::write(path, text).map_err(|e| format!("{}: {e}", path.display())) -} - -fn read_json(path: &Path) -> Result { - let text = fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?; - serde_json::from_str(&text).map_err(|e| format!("{}: {e}", path.display())) -} - -fn report(work: &Path) -> Result<(), String> { - let pack: import::Pack = read_json(&work.join("pack.json"))?; - let records: Vec = read_json(&work.join("inventory.json"))?; - let rows = report::build(&records, work); - write_json(&work.join("coverage.json"), &rows)?; - let title = format!( - "{} ({} {}, Minecraft {})", - pack.name, pack.loader, pack.loader_version, pack.minecraft - ); - fs::write(work.join("coverage.md"), report::markdown(&title, &rows)) - .map_err(|e| format!("coverage.md: {e}"))?; - println!("{title}: {} files: {:?}", rows.len(), report::counts(&rows)); - println!("{}", work.join("coverage.md").display()); - Ok(()) -} - -fn import(pack: &Path, work: &Path) -> Result<(), String> { - let pack = import::import(pack)?; - fs::create_dir_all(work).map_err(|e| format!("{}: {e}", work.display()))?; - write_json(&work.join("pack.json"), &pack)?; - let mut kinds = std::collections::BTreeMap::<&str, usize>::new(); - for f in &pack.files { - *kinds.entry(f.kind()).or_default() += 1; - } - println!( - "{} ({}): Minecraft {}, {} {}; files {kinds:?}; overrides {:?}", - pack.name, pack.format, pack.minecraft, pack.loader, pack.loader_version, pack.overrides - ); - Ok(()) -} - -fn inventory(work: &Path, offline: bool) -> Result<(), String> { - let pack: import::Pack = read_json(&work.join("pack.json"))?; - let cache_path = work.join("modrinth.json"); - let mut cache = modrinth::Cache::load(&cache_path); - if !offline { - let hashes: Vec = pack.files.iter().filter_map(|f| f.sha1.clone()).collect(); - cache.fetch(&hashes)?; - write_json(&cache_path, &cache)?; - } - let records = inventory::build(&pack, &cache, work); - write_json(&work.join("inventory.json"), &records)?; - fs::write(work.join("inventory.csv"), inventory::csv(&records)) - .map_err(|e| format!("inventory.csv: {e}"))?; - let jars = records.iter().filter(|r| r.jar.is_some()).count(); - println!("{} files, {jars} jars read", records.len()); - for (title, field) in [ - ( - "class", - (|r: &inventory::Record| r.class.as_str()) as fn(&inventory::Record) -> &str, - ), - ("tier", |r| r.tier.as_str()), - ("confidence", |r| r.confidence.as_str()), - ("source", |r| r.source.as_str()), - ] { - println!("{title}: {:?}", inventory::counts(&records, field)); - } - let layers = records.iter().map(|r| r.layer).max().map_or(0, |m| m + 1); - let per_layer: Vec = (0..layers) - .map(|l| records.iter().filter(|r| r.layer == l).count()) - .collect(); - println!("layers: {per_layer:?}"); - let missing: usize = records.iter().map(|r| r.missing.len()).sum(); - if missing > 0 { - println!("{missing} required dependencies are not in the pack (see inventory.json)"); - } - Ok(()) -} - -/// The vanilla blocks a prepared resource root has (`facts.json`'s defaults). -fn vanilla_blocks(resources: &Path) -> Result, String> { - let facts: serde_json::Value = read_json(&resources.join("facts.json"))?; - let defaults = facts["defaults"] - .as_object() - .ok_or("facts.json: no defaults")?; - Ok(defaults.keys().cloned().collect()) -} - -fn carry(work: &Path, resources: Option<&Path>, only: &[String]) -> Result<(), String> { - let records: Vec = read_json(&work.join("inventory.json"))?; - let facts_path = work.join("oracle/facts.json"); - let facts = if facts_path.exists() { - let f = oracle::Facts::read(&facts_path)?; - println!( - "oracle facts: {} blocks, {} items", - f.blocks.len(), - f.items.len() - ); - Some(f) - } else { - None - }; - let vanilla = resources.map(vanilla_blocks).transpose()?; - let allowed = |b: &str| vanilla.as_ref().is_none_or(|v| v.contains(b)); - let mut done = 0; - for r in &records { - let chosen = if only.is_empty() { - r.class == "content" - } else { - only.contains(&r.key) - }; - let (Some(jar), Some(info)) = (&r.jar, &r.info) else { - if chosen && !only.is_empty() { - eprintln!("{}: no jar (put it in {}/jars/)", r.key, work.display()); - } - continue; - }; - if !chosen { - continue; - } - match carry::carry( - jar, - info, - &work.join("mods").join(&r.key), - facts.as_ref(), - &allowed, - ) { - Ok(report) => { - done += 1; - println!( - "{}: {} files, {} recipes upgraded ({} kept), {} loot tables upgraded, {} item definitions added, {} blocks ({} left out, {} with differences), {} items ({} left out), {} convention tags, {} outside", - r.key, - report.copied, - report.recipes_upgraded, - report.recipes_kept.len(), - report.loot_tables_upgraded, - report.item_definitions_added, - report.blocks.len(), - report.blocks_unknown.len(), - report.differences.len(), - report.items, - report.items_unknown.len(), - report.convention_tags, - report.outside.len() - ); - } - Err(e) => eprintln!("{}: {e}", r.key), +/// Removes `--name ` from `args`. +fn option(args: &mut Vec, name: &str) -> Result, String> { + match args.iter().position(|a| a == name) { + Some(i) if i + 1 < args.len() => { + let value = args.remove(i + 1); + args.remove(i); + Ok(Some(PathBuf::from(value))) } + Some(_) => Err(USAGE.to_owned()), + None => Ok(None), } - println!( - "{done} mods carried over into {}", - work.join("mods").display() - ); - Ok(()) +} + +fn take_flag(args: &mut Vec, name: &str) -> bool { + let before = args.len(); + args.retain(|a| a != name); + args.len() != before } diff --git a/crates/horizon-port-pack/src/modrinth.rs b/crates/horizon-port-pack/src/modrinth.rs index 8c34a2004..4e90c871a 100644 --- a/crates/horizon-port-pack/src/modrinth.rs +++ b/crates/horizon-port-pack/src/modrinth.rs @@ -1,13 +1,13 @@ //! Modrinth metadata for the pack's files: the version of each file by -//! SHA-1, and its project (categories, licence, sides). Only metadata: no -//! jar is downloaded. Answers are cached in `/modrinth.json`, so a +//! SHA-1, and its project (categories, licence, sides). Only metadata (the +//! jars are `fetch`'s). Answers are cached in `/modrinth.json`, so a //! rerun works offline. use serde::{Deserialize, Serialize}; use serde_json::Value; use std::{collections::BTreeMap, path::Path}; const API: &str = "https://api.modrinth.com/v2"; -const AGENT: &str = "ludovic111/horizon horizon-port-pack/0.1 (docs/modpacks.md)"; +pub(crate) const AGENT: &str = "ludovic111/horizon horizon-port-pack/0.1 (docs/modpacks.md)"; #[derive(Debug, Default, Serialize, Deserialize)] pub struct Cache { diff --git a/crates/horizon-port-pack/src/pipeline.rs b/crates/horizon-port-pack/src/pipeline.rs new file mode 100644 index 000000000..80a88ee18 --- /dev/null +++ b/crates/horizon-port-pack/src/pipeline.rs @@ -0,0 +1,246 @@ +//! The steps on a work folder, as the command line and the launcher run +//! them. Each one reads what the earlier steps wrote and says what it did +//! through `log`, one line at a time. +use crate::{carry, fetch, import, inventory, modrinth, oracle, report}; +use std::{ + collections::{BTreeMap, BTreeSet}, + fs, + path::Path, +}; + +/// Where a step's lines go: the terminal, or the launcher's log. +pub type Log<'a> = &'a dyn Fn(String); + +pub fn write_json(path: &Path, value: &impl serde::Serialize) -> Result<(), String> { + let text = serde_json::to_string_pretty(value).map_err(|e| e.to_string())?; + fs::write(path, text).map_err(|e| format!("{}: {e}", path.display())) +} + +pub fn read_json(path: &Path) -> Result { + let text = fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?; + serde_json::from_str(&text).map_err(|e| format!("{}: {e}", path.display())) +} + +/// Step 1: the pack at `pack` to `/pack.json`. +pub fn import(pack: &Path, work: &Path, log: Log) -> Result { + let pack = import::import(pack)?; + fs::create_dir_all(work).map_err(|e| format!("{}: {e}", work.display()))?; + write_json(&work.join("pack.json"), &pack)?; + let mut kinds = BTreeMap::<&str, usize>::new(); + for f in &pack.files { + *kinds.entry(f.kind()).or_default() += 1; + } + log(format!( + "{} ({}): Minecraft {}, {} {}; files {kinds:?}; overrides {:?}", + pack.name, pack.format, pack.minecraft, pack.loader, pack.loader_version, pack.overrides + )); + Ok(pack) +} + +/// Step 2: `/inventory.json` and `inventory.csv`. Online, Modrinth is +/// asked about the files it has not answered for yet; when it cannot be +/// reached, the cached answers and the reviewed table classify alone. +pub fn inventory(work: &Path, offline: bool, log: Log) -> Result, String> { + let pack: import::Pack = read_json(&work.join("pack.json"))?; + let cache_path = work.join("modrinth.json"); + let mut cache = modrinth::Cache::load(&cache_path); + if !offline { + let hashes: Vec = pack.files.iter().filter_map(|f| f.sha1.clone()).collect(); + match cache.fetch(&hashes) { + Ok(()) => write_json(&cache_path, &cache)?, + Err(e) => log(format!("Modrinth not reached, classifying offline: {e}")), + } + } + let records = inventory::build(&pack, &cache, work); + write_json(&work.join("inventory.json"), &records)?; + fs::write(work.join("inventory.csv"), inventory::csv(&records)) + .map_err(|e| format!("inventory.csv: {e}"))?; + let jars = records.iter().filter(|r| r.jar.is_some()).count(); + log(format!("{} files, {jars} jars read", records.len())); + for (title, field) in [ + ( + "class", + (|r: &inventory::Record| r.class.as_str()) as fn(&inventory::Record) -> &str, + ), + ("tier", |r| r.tier.as_str()), + ("confidence", |r| r.confidence.as_str()), + ("source", |r| r.source.as_str()), + ] { + log(format!("{title}: {:?}", inventory::counts(&records, field))); + } + let layers = records.iter().map(|r| r.layer).max().map_or(0, |m| m + 1); + let per_layer: Vec = (0..layers) + .map(|l| records.iter().filter(|r| r.layer == l).count()) + .collect(); + log(format!("layers: {per_layer:?}")); + let missing: usize = records.iter().map(|r| r.missing.len()).sum(); + if missing > 0 { + log(format!( + "{missing} required dependencies are not in the pack (see inventory.json)" + )); + } + Ok(records) +} + +/// The vanilla blocks a prepared resource root has (`facts.json`'s defaults). +fn vanilla_blocks(resources: &Path) -> Result, String> { + let facts: serde_json::Value = read_json(&resources.join("facts.json"))?; + let defaults = facts["defaults"] + .as_object() + .ok_or("facts.json: no defaults")?; + Ok(defaults.keys().cloned().collect()) +} + +/// Step 4: the content mods with a jar (or the ones in `only`) carried over +/// into `/mods//carry/`. Returns how many were. +pub fn carry( + work: &Path, + resources: Option<&Path>, + only: &[String], + log: Log, +) -> Result { + let records: Vec = read_json(&work.join("inventory.json"))?; + let facts_path = work.join("oracle/facts.json"); + let facts = if facts_path.exists() { + let f = oracle::Facts::read(&facts_path)?; + log(format!( + "oracle facts: {} blocks, {} items", + f.blocks.len(), + f.items.len() + )); + Some(f) + } else { + None + }; + // Without the prepared tables, templates are not limited to what the game has. + let vanilla = resources.and_then(|r| { + vanilla_blocks(r) + .inspect_err(|e| log(format!("templates not limited to the game's blocks: {e}"))) + .ok() + }); + let allowed = |b: &str| vanilla.as_ref().is_none_or(|v| v.contains(b)); + let mut done = 0; + for r in &records { + let chosen = if only.is_empty() { + r.class == "content" + } else { + only.contains(&r.key) + }; + let (Some(jar), Some(info)) = (&r.jar, &r.info) else { + if chosen && !only.is_empty() { + log(format!( + "{}: no jar (put it in {}/jars/)", + r.key, + work.display() + )); + } + continue; + }; + if !chosen { + continue; + } + // The oracle's facts for the mods it ran; a mod it did not load (a + // library missing) keeps the guesses from its blockstates. + let prefix = format!("{}:", info.mod_id); + let seen = |f: &&oracle::Facts| { + f.blocks + .keys() + .chain(f.items.keys()) + .any(|k| k.starts_with(&prefix)) + }; + match carry::carry( + jar, + info, + &work.join("mods").join(&r.key), + facts.as_ref().filter(seen), + &allowed, + ) { + Ok(report) => { + done += 1; + log(format!( + "{}: {} files, {} recipes upgraded ({} kept), {} loot tables upgraded, {} item definitions added, {} blocks ({} left out, {} with differences), {} items ({} left out), {} convention tags, {} outside", + r.key, + report.copied, + report.recipes_upgraded, + report.recipes_kept.len(), + report.loot_tables_upgraded, + report.item_definitions_added, + report.blocks.len(), + report.blocks_unknown.len(), + report.differences.len(), + report.items, + report.items_unknown.len(), + report.convention_tags, + report.outside.len() + )); + } + Err(e) => log(format!("{}: {e}", r.key)), + } + } + log(format!( + "{done} mods carried over into {}", + work.join("mods").display() + )); + Ok(done) +} + +/// The pack's title as the report heads it. +pub fn title(pack: &import::Pack) -> String { + format!( + "{} ({} {}, Minecraft {})", + pack.name, pack.loader, pack.loader_version, pack.minecraft + ) +} + +/// Step 7: `/coverage.json` and `coverage.md`. +pub fn report(work: &Path, log: Log) -> Result, String> { + let pack: import::Pack = read_json(&work.join("pack.json"))?; + let records: Vec = read_json(&work.join("inventory.json"))?; + let rows = report::build(&records, work); + write_json(&work.join("coverage.json"), &rows)?; + let title = title(&pack); + fs::write(work.join("coverage.md"), report::markdown(&title, &rows)) + .map_err(|e| format!("coverage.md: {e}"))?; + log(format!( + "{title}: {} files: {:?}", + rows.len(), + report::counts(&rows) + )); + Ok(rows) +} + +/// What a player's machine can port today without an agent: content mods +/// of the data tier (carry-over only). +pub fn portable(r: &inventory::Record) -> bool { + r.class == "content" && r.tier == "data" +} + +/// Everything after the import, for the portable mods: their jars (`fetch`, +/// from `from` when the player has them), the inventory again with the jars +/// read, the carry-over and the report. `resources` is the prepared +/// version the ports will play on. +pub fn port( + work: &Path, + resources: Option<&Path>, + from: Option<&Path>, + log: Log, +) -> Result<(fetch::Fetched, Vec), String> { + let records = match read_json::>(&work.join("inventory.json")) { + Ok(r) => r, + Err(_) => inventory(work, false, log)?, + }; + let fetched = fetch::fetch(work, &records, from, &portable, log)?; + // The jars tell classes and tiers better than metadata alone. + let records = inventory(work, true, log)?; + let only: Vec = records + .iter() + .filter(|r| portable(r) && r.jar.is_some()) + .map(|r| r.key.clone()) + .collect(); + if only.is_empty() { + log("no portable mod has its jar: nothing to carry over".into()); + } else { + carry(work, resources, &only, log)?; + } + Ok((fetched, report(work, log)?)) +} diff --git a/crates/horizon-port-pack/src/report.rs b/crates/horizon-port-pack/src/report.rs index ffa025c79..a98dd0432 100644 --- a/crates/horizon-port-pack/src/report.rs +++ b/crates/horizon-port-pack/src/report.rs @@ -83,7 +83,7 @@ pub fn license_kind(license: &str) -> &'static str { /// What still blocks a mod of this tier that has not been carried over. fn not_yet(tier: &str) -> &'static str { match tier { - "data" => "data tier: carry-over only, no agent; its jar is not in the work folder", + "data" => "data tier: carry-over only, no agent; its jar is not here yet (fetch gets it)", "logic" => { "needs mod API 0.3: behaviour callbacks (use, place, break, ticks) and server events" } diff --git a/crates/horizon-protocol/src/chunk.rs b/crates/horizon-protocol/src/chunk.rs index 3343eacf5..32d7f98b6 100644 --- a/crates/horizon-protocol/src/chunk.rs +++ b/crates/horizon-protocol/src/chunk.rs @@ -114,6 +114,67 @@ impl Paletted { /// Pack values the way the server's container would have grown: /// the smallest configuration for the number of distinct values. pub fn from_values(values: &[i32], strategy: Strategy, global_bits: u8) -> Self { + // Palette indices by value, in first-seen order: a table over the + // value range (registry ids are small), kept per thread and cleared + // after use, with the previous value remembered, since neighbouring + // blocks repeat. + thread_local! { + static INDEX: std::cell::RefCell> = const { std::cell::RefCell::new(Vec::new()) }; + } + let max = values.iter().copied().max().unwrap_or(0); + if values.iter().any(|&v| v < 0) || max >= 1 << 20 { + return Self::from_values_slow(values, strategy, global_bits); + } + let mut palette: Vec = Vec::new(); + let mut ids = Vec::with_capacity(values.len()); + INDEX.with_borrow_mut(|index| { + if index.len() <= max as usize { + index.resize(max as usize + 1, u32::MAX); + } + let (mut last, mut last_id) = (-1i32, 0u32); + for &v in values { + if v != last { + let slot = &mut index[v as usize]; + if *slot == u32::MAX { + *slot = palette.len() as u32; + palette.push(v); + } + (last, last_id) = (v, *slot); + } + ids.push(last_id); + } + for &v in &palette { + index[v as usize] = u32::MAX; + } + }); + let needed = if palette.len() <= 1 { + 0 + } else { + (usize::BITS - (palette.len() - 1).leading_zeros()) as u8 + }; + let (has_palette, bits) = strategy.configuration(needed, global_bits); + if bits == 0 { + return Self { + bits: 0, + palette: vec![palette.first().copied().unwrap_or(0)], + data: Vec::new(), + }; + } + let mut data = vec![0i64; storage_len(bits, values.len())]; + let per = 64 / bits as usize; + for (i, (&v, &id)) in values.iter().zip(&ids).enumerate() { + let id = if has_palette { u64::from(id) } else { v as u64 }; + data[i / per] |= (id << ((i % per) * bits as usize)) as i64; + } + Self { + bits, + palette: if has_palette { palette } else { Vec::new() }, + data, + } + } + + /// `from_values` for any values: linear palette searches. + fn from_values_slow(values: &[i32], strategy: Strategy, global_bits: u8) -> Self { let mut palette: Vec = Vec::new(); for &v in values { if !palette.contains(&v) { @@ -290,6 +351,29 @@ pub fn nibble(layer: &[u8], i: usize) -> u8 { mod tests { use super::*; + #[test] + fn from_values_matches_the_linear_search() { + let mut seed = 12345u64; + let mut next = |n: u64| { + seed = seed + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407); + ((seed >> 33) % n) as i32 + }; + for distinct in [1u64, 2, 16, 17, 200, 300, 5000] { + let values: Vec = (0..BLOCKS).map(|_| next(distinct) * 7).collect(); + assert_eq!( + Paletted::from_values(&values, Strategy::Blocks, 16), + Paletted::from_values_slow(&values, Strategy::Blocks, 16) + ); + let biomes: Vec = values[..BIOMES].to_vec(); + assert_eq!( + Paletted::from_values(&biomes, Strategy::Biomes, 7), + Paletted::from_values_slow(&biomes, Strategy::Biomes, 7) + ); + } + } + #[test] fn bit_storage_matches_simple_bit_storage() { // 5 bits: 12 values per long, the top 4 bits of each long unused. diff --git a/crates/horizon-protocol/src/packets/play.rs b/crates/horizon-protocol/src/packets/play.rs index 2d9f7e629..f14f8f817 100644 --- a/crates/horizon-protocol/src/packets/play.rs +++ b/crates/horizon-protocol/src/packets/play.rs @@ -1269,12 +1269,126 @@ packet_enum!(Clientbound, crate::ids::play::clientbound { UpdateAdvancements(UpdateAdvancements) = UPDATE_ADVANCEMENTS, UpdateMobEffect(UpdateMobEffect) = UPDATE_MOB_EFFECT, UpdateTags(UpdateTags) = UPDATE_TAGS, + TrackedWaypoint(TrackedWaypoint) = WAYPOINT, }); packet!( /// ClientboundClearTitlesPacket: whether to also reset the timings. Bool1 { value: bool => Bool } ); +/// ClientboundTrackedWaypointPacket.Operation, by ordinal. +pub mod waypoint_operation { + pub const TRACK: i32 = 0; + pub const UNTRACK: i32 = 1; + pub const UPDATE: i32 = 2; +} + +/// TrackedWaypoint's identifier: `Either`. +#[derive(Clone, Debug, PartialEq)] +pub enum WaypointId { + Uuid(u128), + Name(String), +} + +/// What a TrackedWaypoint points at (its `Type` and contents). +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum WaypointTarget { + /// `TrackedWaypoint.empty`: an untracked waypoint. + Empty, + /// A block position (`Vec3iWaypoint`). + Block([i32; 3]), + /// A chunk (`ChunkWaypoint`). + Chunk([i32; 2]), + /// An angle in radians (`AzimuthWaypoint`). + Azimuth(f32), +} + +/// ClientboundTrackedWaypointPacket: the locator bar's waypoints. +#[derive(Clone, Debug, PartialEq)] +pub struct TrackedWaypoint { + /// `waypoint_operation`. + pub operation: i32, + pub id: WaypointId, + /// Waypoint.Icon: the style (a `waypoint_style_asset` key) and an RGB colour. + pub style: String, + pub color: Option, + pub target: WaypointTarget, +} + +impl Codec for TrackedWaypoint { + fn read(r: &mut Reader) -> Result { + let operation = r.varint()?; + if !(0..=2).contains(&operation) { + return Err(Error::new(format!("waypoint operation {operation}"))); + } + let id = if r.bool()? { + WaypointId::Uuid(r.uuid()?) + } else { + WaypointId::Name(r.string(32767)?) + }; + let style = Ident::read(r)?; + // ByteBufCodecs.RGB_COLOR: three bytes, read back as an opaque ARGB colour. + let color = if r.bool()? { + let [red, green, blue] = [r.u8()?, r.u8()?, r.u8()?].map(i32::from); + Some(-16_777_216 | red << 16 | green << 8 | blue) + } else { + None + }; + let target = match r.varint()? { + 0 => WaypointTarget::Empty, + 1 => WaypointTarget::Block([r.varint()?, r.varint()?, r.varint()?]), + 2 => WaypointTarget::Chunk([r.varint()?, r.varint()?]), + 3 => WaypointTarget::Azimuth(r.f32()?), + other => return Err(Error::new(format!("waypoint type {other}"))), + }; + Ok(TrackedWaypoint { + operation, + id, + style, + color, + target, + }) + } + fn write(v: &TrackedWaypoint, w: &mut Writer) { + w.varint(v.operation); + match &v.id { + WaypointId::Uuid(uuid) => { + w.bool(true); + w.uuid(*uuid); + } + WaypointId::Name(name) => { + w.bool(false); + w.string(name); + } + } + w.string(&v.style); + w.bool(v.color.is_some()); + if let Some(color) = v.color { + for shift in [16, 8, 0] { + w.u8((color >> shift) as u8); + } + } + match v.target { + WaypointTarget::Empty => w.varint(0), + WaypointTarget::Block([x, y, z]) => { + w.varint(1); + w.varint(x); + w.varint(y); + w.varint(z); + } + WaypointTarget::Chunk([x, z]) => { + w.varint(2); + w.varint(x); + w.varint(z); + } + WaypointTarget::Azimuth(angle) => { + w.varint(3); + w.f32(angle); + } + } + } +} + // ---- serverbound ------------------------------------------------------- packet!(AcceptTeleportation { id: i32 => VarInt, x: f64 => Double, y: f64 => Double, z: f64 => Double, yaw: f32 => Float, pitch: f32 => Float }); @@ -1489,6 +1603,42 @@ mod tests { } } + #[test] + fn tracked_waypoint_bytes() { + // TRACK, a UUID, the default style, red (#FF5555), a block position. + let track = Clientbound::TrackedWaypoint(TrackedWaypoint { + operation: waypoint_operation::TRACK, + id: WaypointId::Uuid(0x0102_0304_0506_0708_090a_0b0c_0d0e_0f10), + style: "minecraft:default".into(), + color: Some(0xFFFF_5555_u32 as i32), + target: WaypointTarget::Block([1, -2, 300]), + }); + let mut expected = vec![141, 1, 0, 1]; + expected.extend(1..=16u8); + expected.push(17); + expected.extend(b"minecraft:default"); + expected.extend([ + 1, 0xFF, 0x55, 0x55, 1, 1, 0xFE, 0xFF, 0xFF, 0xFF, 0x0F, 0xAC, 0x02, + ]); + assert_eq!(track.encode(), expected); + assert_eq!(Clientbound::decode(141, &expected[2..]).unwrap(), track); + for target in [ + WaypointTarget::Empty, + WaypointTarget::Chunk([-3, 4]), + WaypointTarget::Azimuth(-1.25), + ] { + let packet = Clientbound::TrackedWaypoint(TrackedWaypoint { + operation: waypoint_operation::UPDATE, + id: WaypointId::Name("spawn".into()), + style: "minecraft:bowtie".into(), + color: None, + target, + }); + let bytes = packet.encode(); + assert_eq!(Clientbound::decode(141, &bytes[2..]).unwrap(), packet); + } + } + #[test] fn clientbound_round_trips() { let packets = vec![ diff --git a/crates/horizon-server-files/Cargo.toml b/crates/horizon-server-files/Cargo.toml index 52114718d..184dadc90 100644 --- a/crates/horizon-server-files/Cargo.toml +++ b/crates/horizon-server-files/Cargo.toml @@ -4,13 +4,15 @@ version = "0.1.0" edition.workspace = true license.workspace = true rust-version = "1.90" -description = "The dedicated server's vanilla files: server.properties, eula.txt, ops, whitelist and bans" +description = "The dedicated server's vanilla files: server.properties, eula.txt, ops, whitelist and bans, and account lookups" [dependencies] horizon-core = { path = "../horizon-core" } horizon-protocol = { path = "../horizon-protocol" } horizon-world = { path = "../horizon-world" } log = "0.4" +# Online mode: finding accounts by name (the services' profiles endpoint). +ureq = { version = "2", default-features = false, features = ["tls"] } [lints] workspace = true diff --git a/crates/horizon-server-files/src/json.rs b/crates/horizon-server-files/src/json.rs index 8e1d865b2..47fe6d5d3 100644 --- a/crates/horizon-server-files/src/json.rs +++ b/crates/horizon-server-files/src/json.rs @@ -78,6 +78,43 @@ impl Json { } } + /// `new GsonBuilder().create().toJson(element)`: compact, HTML-safe + /// strings, null members skipped. + pub fn gson(&self) -> String { + let mut out = String::new(); + self.write_gson(&mut out); + out + } + fn write_gson(&self, out: &mut String) { + match self { + Json::String(s) => quote(out, s, true), + Json::Array(items) => { + out.push('['); + for (i, v) in items.iter().enumerate() { + if i > 0 { + out.push(','); + } + v.write_gson(out); + } + out.push(']'); + } + Json::Object(m) => { + out.push('{'); + let members = m.iter().filter(|(_, v)| *v != Json::Null); + for (i, (k, v)) in members.enumerate() { + if i > 0 { + out.push(','); + } + quote(out, k, true); + out.push(':'); + v.write_gson(out); + } + out.push('}'); + } + other => other.write_compact(out), + } + } + /// `new GsonBuilder().setPrettyPrinting().create().toJson(element, writer)`: /// two-space indents, HTML-safe strings, null members skipped. pub fn pretty(&self) -> String { diff --git a/crates/horizon-server-files/src/lib.rs b/crates/horizon-server-files/src/lib.rs index 30c75c869..08c059905 100644 --- a/crates/horizon-server-files/src/lib.rs +++ b/crates/horizon-server-files/src/lib.rs @@ -1,10 +1,13 @@ //! The dedicated server's files and settings, ported from //! net.minecraft.server.dedicated and net.minecraft.server.players: -//! server.properties, eula.txt and the four user lists. The game's dedicated -//! server (`horizon-client --dedicated`) reads them, and `horizon-server` -//! manages them; this crate needs neither a window nor the game. +//! server.properties, eula.txt, the four user lists, usercache.json and +//! looking players up by name. The game's dedicated server +//! (`horizon-client --dedicated`) reads them, and `horizon-server` manages +//! them; this crate needs neither a window nor the game. pub mod eula; pub mod java_properties; pub mod json; +pub mod profiles; pub mod properties; +pub mod user_cache; pub mod user_lists; diff --git a/crates/horizon-server-files/src/profiles.rs b/crates/horizon-server-files/src/profiles.rs new file mode 100644 index 000000000..a093fbfb1 --- /dev/null +++ b/crates/horizon-server-files/src/profiles.rs @@ -0,0 +1,100 @@ +//! Finding a player's account by name, as an online-mode server does for the +//! names given to `op`, `whitelist` and `ban` (CachedUserNameToIdResolver, +//! MinecraftServicesProfileRepository.findProfileByName): the services' +//! `profiles` / `getByName` endpoint, found through the discovery document. +use crate::{ + json::{self, Json}, + user_lists::NameAndId, +}; +use std::time::Duration; + +/// Mojang's services discovery document. +pub const DEFAULT_DISCOVERY: &str = "https://discovery.minecraftservices.com/minecraft/client"; + +/// The discovery document's URL: `HORIZON_API_DISCOVERY` overrides it like +/// authlib's `-Dminecraft.api.discovery.host`. +pub fn discovery_url() -> String { + std::env::var("HORIZON_API_DISCOVERY") + .ok() + .filter(|u| !u.is_empty()) + .unwrap_or_else(|| DEFAULT_DISCOVERY.to_owned()) +} + +/// StringUtil.isValidPlayerName: at most 16 UTF-16 units, none a control +/// character, a space or above ASCII. +pub fn valid_player_name(name: &str) -> bool { + name.encode_utf16().count() <= 16 && name.encode_utf16().all(|c| c > 32 && c < 127) +} + +/// How a server resolves a name it was given (CachedUserNameToIdResolver.get, +/// without the usercache.json cache): an offline server makes the offline +/// profile; an online one asks the services (for the name in lower case) and +/// knows nobody they don't. +pub fn resolve(name: &str, online: bool) -> Option { + if online && valid_player_name(name) { + find_by_name(&discovery_url(), &name.to_ascii_lowercase()) + } else { + (!online).then(|| NameAndId::offline(name)) + } +} + +/// MinecraftServicesProfileRepository.findProfileByName: the account named +/// `name` (any case), or None when there is none or the services don't +/// answer (vanilla logs both as "Couldn't find profile with name"). +pub fn find_by_name(discovery: &str, name: &str) -> Option { + let agent = ureq::AgentBuilder::new() + .timeout(Duration::from_secs(15)) + .user_agent(concat!("Horizon/", env!("CARGO_PKG_VERSION"))) + .build(); + let doc = json::parse(&agent.get(discovery).call().ok()?.into_string().ok()?, true).ok()?; + let url = endpoint(&doc, "profiles", "getByName")?.replace("{name}", &name.to_lowercase()); + found(&agent.get(&url).call().ok()?.into_string().ok()?, name) +} + +/// MinecraftServicesDiscoveryService.getUrl(service, endpoint). +fn endpoint(doc: &Json, service: &str, name: &str) -> Option { + doc.get("discovery")? + .get(service)? + .get("endpoints")? + .get(name)? + .get("uri")? + .as_string() +} + +/// The answer, authlib's NameAndId: `{"id", "name"}`, the id undashed. +pub fn found(answer: &str, name: &str) -> Option { + let profile = json::parse(answer, true).ok()?; + let found = profile.get("name")?.as_string()?; + let id = profile.get("id")?.as_string()?.replace('-', ""); + (found.eq_ignore_ascii_case(name) && id.len() == 32).then_some(())?; + Some(NameAndId { + id: u128::from_str_radix(&id, 16).ok()?, + name: found, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn names_and_answers() { + assert!(valid_player_name("Alex_123")); + assert!(!valid_player_name("two words")); + assert!(!valid_player_name("ThisNameIsTooLong")); + assert!(!valid_player_name("caf\u{e9}")); + assert_eq!(resolve("Alex", false), Some(NameAndId::offline("Alex"))); + assert_eq!(resolve("not valid", true), None); + let answer = r#"{"id":"069a79f444e94726a5befca90e38aaf5","name":"Notch"}"#; + assert_eq!( + found(answer, "notch"), + Some(NameAndId { + id: 0x069a79f444e94726a5befca90e38aaf5, + name: "Notch".into() + }) + ); + assert_eq!(found(answer, "Steve"), None); + assert_eq!(found("{}", "Notch"), None); + assert_eq!(found("", "Notch"), None); + } +} diff --git a/crates/horizon-server-files/src/user_cache.rs b/crates/horizon-server-files/src/user_cache.rs new file mode 100644 index 000000000..c8ccd6cf0 --- /dev/null +++ b/crates/horizon-server-files/src/user_cache.rs @@ -0,0 +1,318 @@ +//! usercache.json (CachedUserNameToIdResolver): the players the server has +//! seen join or has looked up by name, so commands find a player by name +//! without asking the profile service again. Each entry expires a month after +//! it was added; the file keeps the 1000 most recently used. +use crate::{ + java_properties::{civil_from_days, days_from_civil, now_millis}, + json::{self, Json}, + profiles::valid_player_name, + user_lists::{NameAndId, format_date, parse_date}, +}; +use std::{collections::HashMap, path::PathBuf}; + +const MRU_LIMIT: usize = 1000; + +#[derive(Clone, Debug)] +struct Info { + user: NameAndId, + expires: i64, + last_access: u64, +} + +/// `Calendar.add(MONTH, 1)` on a UTC time: the same day and time a month +/// later, the day clamped to the end of a shorter month. +pub fn one_month_later(millis: i64) -> i64 { + let days = millis.div_euclid(86_400_000); + let time = millis.rem_euclid(86_400_000); + let (y, m, d) = civil_from_days(days); + let (y, m) = if m == 12 { (y + 1, 1) } else { (y, m + 1) }; + let last = days_from_civil( + if m == 12 { y + 1 } else { y }, + if m == 12 { 1 } else { m + 1 }, + 1, + ) - days_from_civil(y, m, 1); + let d = d.min(last as u32); + days_from_civil(y, m, d) * 86_400_000 + time +} + +pub struct UserCache { + file: PathBuf, + /// Every entry ever added; the maps point into it, as vanilla's two maps + /// share their GameProfileInfo objects. + infos: Vec, + by_name: HashMap, + by_uuid: HashMap, + operations: u64, + /// Off on an online-mode server: a name the profile service doesn't know + /// is then no player at all, rather than an offline one. + pub resolve_offline_users: bool, +} + +impl UserCache { + /// The cache in `file`, loaded; also returns the warnings vanilla logs. + pub fn load(file: impl Into) -> (Self, Vec) { + let mut cache = Self { + file: file.into(), + infos: Vec::new(), + by_name: HashMap::new(), + by_uuid: HashMap::new(), + operations: 0, + resolve_offline_users: true, + }; + let mut warnings = Vec::new(); + let entries = match std::fs::read_to_string(&cache.file) { + Ok(text) => cache.parse(&text, &mut warnings), + Err(_) => Vec::new(), + }; + for info in entries.into_iter().rev() { + cache.safe_add(info); + } + (cache, warnings) + } + + fn parse(&self, text: &str, warnings: &mut Vec) -> Vec { + let items = match json::parse(text, false) { + Ok(Json::Array(items)) => items, + // An empty file is Gson's null. + Ok(Json::Null) => return Vec::new(), + _ if text.trim().is_empty() => return Vec::new(), + _ => { + warnings.push(format!( + "Failed to load profile cache {}", + self.file.display() + )); + return Vec::new(); + } + }; + let mut out = Vec::new(); + for item in &items { + if !matches!(item, Json::Object(_)) { + continue; + } + let Some(user) = NameAndId::from_json(item) else { + continue; + }; + let Some(date) = item.get("expiresOn").and_then(Json::as_string) else { + continue; + }; + match parse_date(&date) { + Some(expires) => out.push(Info { + user, + expires, + last_access: 0, + }), + None => warnings.push(format!("Failed to parse date {date}")), + } + } + out + } + + fn safe_add(&mut self, mut info: Info) -> usize { + self.operations += 1; + info.last_access = self.operations; + let index = self.infos.len(); + self.by_name.insert(info.user.name.to_lowercase(), index); + self.by_uuid.insert(info.user.id, index); + self.infos.push(info); + index + } + + /// `add`: a player who joined, remembered for a month; saves the file. + pub fn add(&mut self, user: NameAndId) { + self.add_internal(user, now_millis()); + } + + fn add_internal(&mut self, user: NameAndId, now: i64) -> NameAndId { + let index = self.safe_add(Info { + user, + expires: one_month_later(now), + last_access: 0, + }); + self.save(); + self.infos[index].user.clone() + } + + /// `get(String)`: the player with this name (any case). An expired entry is + /// dropped; a name not cached is asked of `lookup` (the profile service, + /// given the lower-case name) and, failing that, made an offline player + /// when offline users resolve. What is found is cached. + pub fn get_by_name( + &mut self, + name: &str, + lookup: impl FnOnce(&str) -> Option, + ) -> Option { + self.get_by_name_at(name, now_millis(), lookup) + } + + fn get_by_name_at( + &mut self, + name: &str, + now: i64, + lookup: impl FnOnce(&str) -> Option, + ) -> Option { + let name = name.to_lowercase(); + let mut found = self.by_name.get(&name).copied(); + let mut needs_save = false; + if let Some(index) = found + && now >= self.infos[index].expires + { + let user = self.infos[index].user.clone(); + self.by_uuid.remove(&user.id); + self.by_name.remove(&user.name.to_lowercase()); + needs_save = true; + found = None; + } + let result = if let Some(index) = found { + self.operations += 1; + self.infos[index].last_access = self.operations; + Some(self.infos[index].user.clone()) + } else { + let profile = if valid_player_name(&name) { + lookup(&name) + } else { + None + }; + let profile = profile.or_else(|| { + self.resolve_offline_users + .then(|| NameAndId::offline(&name)) + }); + match profile { + Some(profile) => { + needs_save = false; + Some(self.add_internal(profile, now)) + } + None => None, + } + }; + if needs_save { + self.save(); + } + result + } + + /// `get(UUID)`: a cached player by id (expired or not). + pub fn get_by_id(&mut self, id: u128) -> Option { + let index = *self.by_uuid.get(&id)?; + self.operations += 1; + self.infos[index].last_access = self.operations; + Some(self.infos[index].user.clone()) + } + + /// The file's text: the 1000 most recently used entries, most recent + /// first, as Gson writes them (compact, HTML-safe). + pub fn to_json(&self) -> String { + let mut entries: Vec<&Info> = self.by_uuid.values().map(|&i| &self.infos[i]).collect(); + entries.sort_by_key(|info| std::cmp::Reverse(info.last_access)); + let items = entries + .into_iter() + .take(MRU_LIMIT) + .map(|info| { + let mut o = Json::Object(Vec::new()); + info.user.append_to(&mut o); + o.add("expiresOn", Json::String(format_date(info.expires))); + o + }) + .collect(); + Json::Array(items).gson() + } + + /// `save`: errors are ignored, as vanilla does. + pub fn save(&self) { + let _ = std::fs::write(&self.file, self.to_json()); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const DAY: i64 = 86_400_000; + + fn cache(text: &str) -> (UserCache, Vec) { + let dir = std::env::temp_dir().join(format!( + "horizon-usercache-{}-{}", + std::process::id(), + text.len() + )); + std::fs::create_dir_all(&dir).unwrap(); + let file = dir.join("usercache.json"); + std::fs::write(&file, text).unwrap(); + UserCache::load(file) + } + + #[test] + fn months() { + let jan31 = days_from_civil(2026, 1, 31) * DAY + 3_723_000; + assert_eq!( + format_date(one_month_later(jan31)), + "2026-02-28 01:02:03 +0000" + ); + let dec15 = days_from_civil(2026, 12, 15) * DAY; + assert_eq!( + format_date(one_month_later(dec15)), + "2027-01-15 00:00:00 +0000" + ); + let jan31_leap = days_from_civil(2028, 1, 31) * DAY; + assert_eq!( + format_date(one_month_later(jan31_leap)), + "2028-02-29 00:00:00 +0000" + ); + } + + #[test] + fn keeps_order_and_skips_bad_entries() { + let (c, warnings) = cache( + r#"[{"uuid":"069a79f4-44e9-4726-a5be-fca90e38aaf5","name":"Notch","expiresOn":"2099-01-01 00:00:00 +0000"}, + 1, {"name":"NoId"}, {"uuid":"853c80ef-3c37-49fd-aa49-938b674adae6","name":"NoDate"}, + {"uuid":"853c80ef-3c37-49fd-aa49-938b674adae6","name":"BadDate","expiresOn":"soon"}, + {"uuid":"61699b2e-d327-4a01-9f1e-0ea8c3f06bc6","name":"Alex","expiresOn":"2099-01-01 00:00:00 +0000"}]"#, + ); + assert_eq!(warnings, vec!["Failed to parse date soon".to_owned()]); + assert_eq!( + c.to_json(), + r#"[{"uuid":"069a79f4-44e9-4726-a5be-fca90e38aaf5","name":"Notch","expiresOn":"2099-01-01 00:00:00 +0000"},{"uuid":"61699b2e-d327-4a01-9f1e-0ea8c3f06bc6","name":"Alex","expiresOn":"2099-01-01 00:00:00 +0000"}]"# + ); + } + + #[test] + fn names_resolve_through_the_cache() { + let (mut c, _) = cache( + r#"[{"uuid":"069a79f4-44e9-4726-a5be-fca90e38aaf5","name":"Notch","expiresOn":"2099-01-01 00:00:00 +0000"}, + {"uuid":"61699b2e-d327-4a01-9f1e-0ea8c3f06bc6","name":"Old","expiresOn":"2000-01-01 00:00:00 +0000"}]"#, + ); + let now = days_from_civil(2026, 10, 17) * DAY; + let never = |_: &str| -> Option { panic!("looked up") }; + // A hit in any case, and it becomes the most recent. + let notch = c.get_by_name_at("NOTCH", now, never).unwrap(); + assert_eq!(notch.name, "Notch"); + // Expired: dropped and looked up again, by its lower-case name. + let mut asked = Vec::new(); + let old = c + .get_by_name_at("Old", now, |n| { + asked.push(n.to_owned()); + None + }) + .unwrap(); + assert_eq!(asked, ["old"]); + assert_eq!(old, NameAndId::offline("old")); + // Not a valid name: never looked up. + let odd = c.get_by_name_at("a b", now, never).unwrap(); + assert_eq!(odd, NameAndId::offline("a b")); + c.resolve_offline_users = false; + assert_eq!(c.get_by_name_at("Nobody", now, |_| None), None); + let found = NameAndId { + id: 7, + name: "Steve".into(), + }; + let steve = c.get_by_name_at("steve", now, |_| Some(found.clone())); + assert_eq!(steve, Some(found)); + let json = c.to_json(); + let names: Vec<&str> = json + .split("\"name\":\"") + .skip(1) + .map(|s| s.split('"').next().unwrap()) + .collect(); + assert_eq!(names, ["Steve", "a b", "old", "Notch"]); + assert!(json.contains(r#""name":"Steve","expiresOn":"2026-11-17 00:00:00 +0000"}"#)); + } +} diff --git a/crates/horizon-server-files/src/user_lists.rs b/crates/horizon-server-files/src/user_lists.rs index 3d35aa62f..77de64e31 100644 --- a/crates/horizon-server-files/src/user_lists.rs +++ b/crates/horizon-server-files/src/user_lists.rs @@ -39,7 +39,7 @@ impl NameAndId { name: name.to_owned(), } } - fn from_json(o: &Json) -> Option { + pub(crate) fn from_json(o: &Json) -> Option { let uuid = o.get("uuid")?.as_string()?; let name = o.get("name")?.as_string()?; Some(Self { @@ -47,7 +47,7 @@ impl NameAndId { name, }) } - fn append_to(&self, o: &mut Json) { + pub(crate) fn append_to(&self, o: &mut Json) { o.add("uuid", Json::String(uuid_string(self.id))); o.add("name", Json::String(self.name.clone())); } diff --git a/crates/horizon-server/src/config.rs b/crates/horizon-server/src/config.rs index c62869e27..3c389f858 100644 --- a/crates/horizon-server/src/config.rs +++ b/crates/horizon-server/src/config.rs @@ -23,7 +23,7 @@ motd = "A Minecraft Server" port = 25565 bind = "" # listen on every address, or only on this one (server-ip) max_players = 20 -online_mode = true # not supported by Horizon yet: the server runs offline and says so +online_mode = true # check players' Minecraft accounts (false: anyone joins under any name) whitelist = true # only whitelisted players and operators can join enforce_whitelist = false # kick online players taken off the whitelist view_distance = 10 diff --git a/crates/horizon-server/src/init.rs b/crates/horizon-server/src/init.rs index 37e43df80..757b6def8 100644 --- a/crates/horizon-server/src/init.rs +++ b/crates/horizon-server/src/init.rs @@ -6,7 +6,7 @@ use crate::{config, config::Config, engine}; use horizon_server_files::{ eula, java_properties::now_millis, - user_lists::{NameAndId, OpEntry, OpList, WhiteEntry, WhiteList}, + user_lists::{OpEntry, OpList, WhiteEntry, WhiteList}, }; use std::{ io::{BufRead, IsTerminal, Write}, @@ -23,6 +23,8 @@ pub struct Answers { pub whitelist: Option, pub players: Vec, pub op: Option, + /// online-mode (true unless set): players' Minecraft accounts are checked. + pub online: Option, /// Take the default for every question not answered by a flag. pub yes: bool, } @@ -30,7 +32,7 @@ pub struct Answers { impl Answers { /// The answers in the environment, for containers: `HORIZON_EULA=true`, `HORIZON_WORLD`, /// `HORIZON_SEED`, `HORIZON_PORT`, `HORIZON_WHITELIST=true|false`, `HORIZON_PLAYERS` (names separated by - /// commas) and `HORIZON_OP`. Flags given on the command line win. + /// commas), `HORIZON_OP` and `HORIZON_ONLINE=true|false`. Flags given on the command line win. pub fn from_env() -> Result { let var = |name: &str| std::env::var(name).ok().filter(|v| !v.is_empty()); let flag = |name: &str| var(name).map(|v| v.eq_ignore_ascii_case("true")); @@ -54,6 +56,7 @@ impl Answers { }) .unwrap_or_default(), op: var("HORIZON_OP"), + online: flag("HORIZON_ONLINE"), yes: false, }) } @@ -172,6 +175,9 @@ pub fn run(dir: &Path, answers: Answers) -> Result<(), String> { } // 4. Who may join. + if let Some(online) = answers.online { + cfg.server.online_mode = online; + } cfg.server.whitelist = answers.whitelist.unwrap_or_else(|| { prompt.yes( "Only let in players you list (whitelist)?", @@ -193,7 +199,7 @@ pub fn run(dir: &Path, answers: Answers) -> Result<(), String> { .yes(&format!("Make {first} an operator?"), true) .then(|| first.clone()) }); - add_players(dir, &players, op.as_deref())?; + add_players(dir, &players, op.as_deref(), cfg.server.online_mode)?; // 5. The game and its files. match engine::program(&cfg, dir) { @@ -213,24 +219,29 @@ pub fn run(dir: &Path, answers: Answers) -> Result<(), String> { toml_path.display(), dir.display() ); - if cfg.server.online_mode { + if !cfg.server.online_mode { println!( - "Note: Horizon's server doesn't check Mojang accounts yet (online mode), so it runs offline: \ - keep the whitelist on, or play on a LAN or a tailnet." + "Note: online mode is off, so anyone can join under any name: keep the whitelist on, or play on a \ + LAN or a tailnet." ); } Ok(()) } -/// Adds players to the whitelist (and one as an operator) in vanilla's files, with offline UUIDs as the -/// server runs offline. -fn add_players(dir: &Path, players: &[String], op: Option<&str>) -> Result<(), String> { +/// Adds players to the whitelist (and one as an operator) in vanilla's files, with the profiles the server +/// will see: offline ones, or in online mode their accounts. +fn add_players( + dir: &Path, + players: &[String], + op: Option<&str>, + online: bool, +) -> Result<(), String> { if !players.is_empty() { let mut white = WhiteList::new(dir.join("whitelist.json")); let _ = white.load(); for name in players { white.add(WhiteEntry { - user: NameAndId::offline(name), + user: crate::lists::account(name, online)?, }); } println!("Whitelisted: {}", players.join(", ")); @@ -239,7 +250,7 @@ fn add_players(dir: &Path, players: &[String], op: Option<&str>) -> Result<(), S let mut ops = OpList::new(dir.join("ops.json")); let _ = ops.load(); ops.add(OpEntry { - user: NameAndId::offline(name), + user: crate::lists::account(name, online)?, level: 4, bypasses_player_limit: false, }); @@ -264,6 +275,8 @@ mod tests { whitelist: Some(true), players: vec!["Alex".into(), "Steve".into()], op: Some("Alex".into()), + // Offline: no account lookups in a test. + online: Some(false), yes: true, }; run(&dir, answers).unwrap(); @@ -277,6 +290,7 @@ mod tests { ("survival", "12345", 25599) ); assert!(eula::agreed(&dir.join("eula.txt")).unwrap()); + assert!(!cfg.server.online_mode); let white = std::fs::read_to_string(dir.join("whitelist.json")).unwrap(); assert!( white.contains("\"Alex\"") && white.contains("\"Steve\""), diff --git a/crates/horizon-server/src/lists.rs b/crates/horizon-server/src/lists.rs index 398ccfe10..a6c466666 100644 --- a/crates/horizon-server/src/lists.rs +++ b/crates/horizon-server/src/lists.rs @@ -20,7 +20,7 @@ pub fn run(dir: &Path, command: &str, args: &[String]) -> Result<(), String> { let message = match (command, args.as_slice()) { ("op", [name]) => { let mut ops = load(OpList::new(ops_path))?; - let user = NameAndId::offline(name); + let user = account(name, config.server.online_mode)?; if ops.contains(&user) { "Nothing changed. The player already is an operator".to_owned() } else { @@ -34,7 +34,8 @@ pub fn run(dir: &Path, command: &str, args: &[String]) -> Result<(), String> { } ("deop", [name]) => { let mut ops = load(OpList::new(ops_path))?; - if ops.remove(&NameAndId::offline(name)) { + let user = listed(ops.entries().iter().map(|e| &e.user), name); + if user.is_some_and(|user| ops.remove(&user)) { format!("Made {name} no longer a server operator") } else { "Nothing changed. The player is not an operator".to_owned() @@ -42,7 +43,7 @@ pub fn run(dir: &Path, command: &str, args: &[String]) -> Result<(), String> { } ("whitelist", ["add", name]) => { let mut white = load(WhiteList::new(white_path))?; - let user = NameAndId::offline(name); + let user = account(name, config.server.online_mode)?; if white.contains(&user) { "Player is already whitelisted".to_owned() } else { @@ -52,7 +53,8 @@ pub fn run(dir: &Path, command: &str, args: &[String]) -> Result<(), String> { } ("whitelist", ["remove", name]) => { let mut white = load(WhiteList::new(white_path))?; - if white.remove(&NameAndId::offline(name)) { + let user = listed(white.entries().iter().map(|e| &e.user), name); + if user.is_some_and(|user| white.remove(&user)) { format!("Removed {name} from the whitelist") } else { "Player is not whitelisted".to_owned() @@ -97,6 +99,23 @@ pub fn run(dir: &Path, command: &str, args: &[String]) -> Result<(), String> { Ok(()) } +/// The profile a running server would store for `name`: the offline one, or in online mode the account the +/// services know by that name. +pub fn account(name: &str, online: bool) -> Result { + horizon_server_files::profiles::resolve(name, online).ok_or_else(|| { + if online { + format!("That player does not exist (no Minecraft account is named {name}, or the services didn't answer)") + } else { + "That player does not exist".to_owned() + } + }) +} + +/// The entry already on a list under `name` (any case), so removing needs no lookup. +fn listed<'a>(mut users: impl Iterator, name: &str) -> Option { + users.find(|u| u.name.eq_ignore_ascii_case(name)).cloned() +} + /// A list from its file; a missing file is an empty list, a broken one an error (vanilla would replace it). fn load( mut list: horizon_server_files::user_lists::UserList, diff --git a/crates/horizon-server/src/main.rs b/crates/horizon-server/src/main.rs index af45015de..c37da27a5 100644 --- a/crates/horizon-server/src/main.rs +++ b/crates/horizon-server/src/main.rs @@ -23,7 +23,7 @@ Usage: horizon-server [--dir DIR] init Set up a server here: the EULA, the world, the port, who may join (asks, or takes flags: --eula --world NAME --seed SEED --port N --whitelist/--no-whitelist --player NAME... --op NAME - --yes) + --offline --yes) run Run the server in the foreground; type console commands, Ctrl-C stops it cleanly. It restarts the game if it crashes. start Run the server in the background (its output goes to logs/supervisor.log) @@ -150,6 +150,7 @@ fn init_answers(args: &[String]) -> Result { "--no-whitelist" => answers.whitelist = Some(false), "--player" => answers.players.push(value(arg)?), "--op" => answers.op = Some(value(arg)?), + "--offline" => answers.online = Some(false), other => return Err(format!("init: unknown option {other}")), } } diff --git a/crates/horizon-world/Cargo.toml b/crates/horizon-world/Cargo.toml index 078692bff..737edd289 100644 --- a/crates/horizon-world/Cargo.toml +++ b/crates/horizon-world/Cargo.toml @@ -9,6 +9,9 @@ description = "Horizon vanilla world data, NBT, regions and chunk storage" [dependencies] libm = "=0.2.16" serde_json = { version = "1.0.151", features = ["raw_value"] } +serde = { version = "1", features = ["derive"] } +sha2 = "0.10" +postcard = { version = "=1.1.3", default-features = false, features = ["alloc"] } horizon-core = { path = "../horizon-core" } flate2 = { version = "1.1.10", default-features = false, features = ["rust_backend"] } lz4_flex = { version = "0.14", default-features = false, features = ["std", "safe-encode", "safe-decode", "checked-decode"] } diff --git a/crates/horizon-world/examples/behaviour_load_profile.rs b/crates/horizon-world/examples/behaviour_load_profile.rs new file mode 100644 index 000000000..d9d7a1147 --- /dev/null +++ b/crates/horizon-world/examples/behaviour_load_profile.rs @@ -0,0 +1,32 @@ +//! Measure registry and behaviour load times using prepared client resources. +//! `behaviour_load_profile RESOURCES [CACHE_DIR]`: with CACHE_DIR, loads use and +//! refresh the snapshot there, as the client does with its `.horizon-cache`. +use horizon_world::level::{Behaviours, data::load_registry}; +use std::{path::Path, time::Instant}; + +fn main() -> Result<(), Box> { + let root = std::env::args() + .nth(1) + .expect("resource directory argument"); + let root = Path::new(&root); + if let Some(dir) = std::env::args().nth(2) { + Behaviours::set_cache_dir(dir.into()); + } + let started = Instant::now(); + let registry = load_registry(root)?; + eprintln!( + "registry_ms={:.3}", + started.elapsed().as_secs_f64() * 1000.0 + ); + for iteration in 0..3 { + let started = Instant::now(); + let behaviours = Behaviours::load(®istry, root)?; + eprintln!( + "behaviours_{}_ms={:.3}", + iteration, + started.elapsed().as_secs_f64() * 1000.0 + ); + std::hint::black_box(behaviours); + } + Ok(()) +} diff --git a/crates/horizon-world/src/level/data.rs b/crates/horizon-world/src/level/data.rs index 42f8f75de..280f21582 100644 --- a/crates/horizon-world/src/level/data.rs +++ b/crates/horizon-world/src/level/data.rs @@ -21,6 +21,9 @@ use std::{ path::Path, sync::Arc, }; +#[path = "data/cache.rs"] +mod cache; +pub(super) use cache::set_dir as set_cache_dir; /// Per-state flags read from vanilla accessors over an empty level. pub mod state_flags { @@ -251,7 +254,7 @@ macro_rules! named_blocks { #[derive(Clone, Copy, Debug)] pub struct Named { $(pub $field: BlockId,)* } impl Named { - fn resolve(table: &StateTable) -> io::Result { + pub(super) fn resolve(table: &StateTable) -> io::Result { Ok(Self { $($field: table.block_by_name(concat!("minecraft:", $name)).ok_or_else(|| invalid(concat!("missing block ", $name)))?,)* }) } } @@ -393,7 +396,7 @@ macro_rules! tags { #[derive(Clone, Debug, Default)] pub struct Tags { $(pub $field: Arc,)* } impl Tags { - fn resolve(all: &HashMap>) -> Self { + pub(super) fn resolve(all: &HashMap>) -> Self { Self { $($field: all.get(concat!("minecraft:", $name)).cloned().unwrap_or_default(),)* } } } @@ -487,7 +490,7 @@ macro_rules! props { #[derive(Clone, Copy, Debug)] pub struct Props { $(pub $field: PropKey,)* } impl Props { - fn resolve(table: &mut StateTable) -> Self { + pub(super) fn resolve(table: &mut StateTable) -> Self { Self { $($field: table.key($name),)* } } } @@ -653,23 +656,65 @@ pub fn load_registry(root: &Path) -> io::Result { } pub(super) fn load(registry: &BlockRegistry, root: &Path) -> io::Result { - let behaviour = read_json(&root.join("block-behaviours.json"))?; - load_with_behaviour_json(registry, root, &behaviour) + cached(registry, root, || load_uncached(registry, root)) } +/// [`load`] with `block-behaviours.json` already parsed by the caller. pub(super) fn load_with_behaviour_json( registry: &BlockRegistry, root: &Path, behaviour: &Value, +) -> io::Result { + cached(registry, root, || { + build_with_behaviour_json(registry, root, behaviour) + }) +} + +/// [`load`] with the client's typed gameplay projection. The snapshot is +/// still tried first: on a hit the projection is not needed. +pub(super) fn load_with_gameplay( + registry: &BlockRegistry, + root: &Path, + behaviour: &Value, + gameplay_states: &[G], +) -> io::Result { + cached(registry, root, || { + build_with_gameplay(registry, root, behaviour, gameplay_states) + }) +} + +/// The snapshot when it matches, else `build` and refresh the snapshot. +fn cached( + registry: &BlockRegistry, + root: &Path, + build: impl FnOnce() -> io::Result, +) -> io::Result { + if let Some(behaviours) = cache::load(registry, root) { + return Ok(behaviours); + } + let behaviours = build()?; + cache::store(registry, root, &behaviours); + Ok(behaviours) +} + +fn load_uncached(registry: &BlockRegistry, root: &Path) -> io::Result { + let behaviour = read_json(&root.join("block-behaviours.json"))?; + build_with_behaviour_json(registry, root, &behaviour) +} + +fn build_with_behaviour_json( + registry: &BlockRegistry, + root: &Path, + behaviour: &Value, ) -> io::Result { let gameplay = read_json(&root.join("gameplay-facts.json"))?; let gameplay_states = gameplay["states"] .as_array() .ok_or_else(|| fail("missing gameplay states"))?; - load_with_gameplay(registry, root, behaviour, gameplay_states) + build_with_gameplay(registry, root, behaviour, gameplay_states) } -pub(super) fn load_with_gameplay( +fn build_with_gameplay( registry: &BlockRegistry, root: &Path, behaviour: &Value, @@ -744,7 +789,7 @@ pub(super) fn load_with_gameplay( // Collision shapes. let collision_tag = nbt::read_named( &fs::read(root.join("cached-collision.nbt"))?, - Limits::default(), + Limits::tables(), ) .map_err(|e| invalid(e.to_string()))? .0 diff --git a/crates/horizon-world/src/level/data/cache.rs b/crates/horizon-world/src/level/data/cache.rs new file mode 100644 index 000000000..59bc1f8d8 --- /dev/null +++ b/crates/horizon-world/src/level/data/cache.rs @@ -0,0 +1,867 @@ +//! A disposable, versioned snapshot of resolved behaviour tables. +//! +//! The source resources remain authoritative. Failed reads, unsupported +//! versions, corrupt payloads and write failures always use the normal loader. +//! The snapshot lives in the game's own cache folder ([`set_dir`]), never in the +//! resource folder: that one is shared by worktrees and copied between installed +//! versions by the launcher. Its key includes the running executable, so a build +//! that resolves the tables differently never reads another build's snapshot. +use super::*; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::{ + io::Write, + path::PathBuf, + sync::{ + OnceLock, + atomic::{AtomicU64, Ordering}, + }, + time::UNIX_EPOCH, +}; + +const MAGIC: &[u8; 8] = b"HZBEHAV\0"; +const VERSION: u32 = 2; +const FILE_NAME: &str = "behaviours-v2.bin"; +const MAX_FILE_BYTES: usize = 128 * 1024 * 1024; +const MAX_DECODED_BYTES: usize = 256 * 1024 * 1024; +static NEXT_TEMP: AtomicU64 = AtomicU64::new(0); +static DIR: OnceLock = OnceLock::new(); + +/// Keep snapshots in `dir` (the client passes `/.horizon-cache`). +/// Without it (tests, tools, servers), `Behaviours::load` always uses the source loader. +pub(in crate::level) fn set_dir(dir: PathBuf) { + let _ = DIR.set(dir); +} + +#[derive(Serialize, Deserialize)] +struct Snapshot { + tags: HashMap>, + facts: Vec, + shapes: Vec, + shape_info: Vec<[u8; 3]>, + fluid_tags: [u8; 6], + blocks: Vec, + items: HashMap, +} + +#[derive(Serialize, Deserialize)] +struct Fact { + flags: u32, + sturdy: u32, + weak: u32, + strong: u32, + fluid_kind: u8, + fluid_amount: u8, + fluid_falling: bool, + emission: u8, + dampening: u8, + hardness: f32, + push: u8, + instrument: u8, + shape: i32, +} + +#[derive(Serialize, Deserialize)] +struct Shape { + coords: [Vec; 3], + occupied: Vec, + cube: bool, + coordinate_cubes: [bool; 3], + min: [i32; 3], + max: [i32; 3], + /// Preserve the shared singleton identities used by vanilla shape shortcuts. + singleton: u8, +} + +#[derive(Serialize, Deserialize)] +struct SetTypeSnapshot { + name: String, + can_open_by_hand: bool, + can_open_by_wind_charge: bool, + arrows: bool, + mobs_only: bool, + door_open: String, + door_close: String, + trapdoor_open: String, + trapdoor_close: String, + plate_on: String, + plate_off: String, + button_on: String, + button_off: String, +} + +#[derive(Serialize, Deserialize)] +struct SoundsSnapshot { + volume: f32, + pitch: f32, + place: String, + break_: String, + fall: String, +} + +#[derive(Serialize, Deserialize)] +struct Block { + chain: Vec, + interfaces: u16, + set_type: Option, + gate_open: Option, + gate_close: Option, + open_sound: Option, + close_sound: Option, + ticks_to_stay_pressed: i32, + max_weight: i32, + link: Option, + link2: Option, + support_tag: Option>, + support_tag2: Option>, + feature: Option, + grow_probability: f64, + growth_direction: u8, + schedule_fluid_ticks: bool, + weather: u8, + ignite_odds: i32, + burn_odds: i32, + sounds: SoundsSnapshot, + item: String, + dynamic_shape: bool, +} + +#[derive(Serialize, Deserialize)] +struct Item { + block: u16, + wall_block: Option, + attachment: Option, + kind: u8, + game_master: bool, +} + +fn fingerprint(registry: &BlockRegistry, root: &Path) -> io::Result<[u8; 32]> { + let mut hasher = Sha256::new(); + hasher.update(b"horizon resolved behaviours 2"); + // The build that resolved the tables: the version constant alone misses + // loader changes that keep the snapshot layout. + let exe = std::env::current_exe()?; + let built = fs::metadata(&exe)?; + hash_str(&mut hasher, &exe.to_string_lossy()); + hasher.update(built.len().to_le_bytes()); + let modified = built + .modified()? + .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)?; + } + // 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(); + for index in 0..registry.len() { + let state = registry + .state(BlockStateId(index as u32)) + .ok_or_else(|| invalid("missing registry state"))?; + hash_str(&mut hasher, &state.name); + let mut props: Vec<_> = state.properties.iter().collect(); + props.sort_by_key(|(key, _)| *key); + hasher.update((props.len() as u32).to_le_bytes()); + for (key, value) in props { + hash_str(&mut hasher, key); + hash_str(&mut hasher, value); + } + if seen.insert(state.name.as_str()) { + let default = registry + .default_state(&state.name) + .ok_or_else(|| invalid("missing default state"))?; + hasher.update(default.0.to_le_bytes()); + } + } + Ok(hasher.finalize().into()) +} + +fn hash_str(hasher: &mut Sha256, text: &str) { + hasher.update((text.len() as u64).to_le_bytes()); + hasher.update(text.as_bytes()); +} + +fn hash_file(hasher: &mut Sha256, root: &Path, path: &Path) -> io::Result<()> { + let relative = path + .strip_prefix(root) + .map_err(|e| invalid(e.to_string()))?; + hash_str(hasher, &relative.to_string_lossy()); + let bytes = fs::read(path)?; + hasher.update((bytes.len() as u64).to_le_bytes()); + hasher.update(bytes); + Ok(()) +} + +fn direction(index: u8) -> io::Result { + Direction::ALL + .get(index as usize) + .copied() + .ok_or_else(|| invalid("cached direction out of range")) +} +fn direction_index(direction: Direction) -> u8 { + Direction::ALL.iter().position(|d| *d == direction).unwrap() as u8 +} +fn class(index: u16) -> io::Result { + Class::ALL + .get(index as usize) + .copied() + .ok_or_else(|| invalid("cached block class out of range")) +} +fn kind(index: u8) -> io::Result { + Ok(match index { + 0 => ItemKind::Block, + 1 => ItemKind::DoubleHigh, + 2 => ItemKind::StandingAndWall, + 3 => ItemKind::Scaffolding, + 4 => ItemKind::PlaceOnWater, + 5 => ItemKind::HangingSign, + 6 => ItemKind::SolidBucket, + 7 => ItemKind::Other, + _ => return Err(invalid("cached item kind out of range")), + }) +} +fn kind_index(kind: ItemKind) -> u8 { + match kind { + ItemKind::Block => 0, + ItemKind::DoubleHigh => 1, + ItemKind::StandingAndWall => 2, + ItemKind::Scaffolding => 3, + ItemKind::PlaceOnWater => 4, + ItemKind::HangingSign => 5, + ItemKind::SolidBucket => 6, + ItemKind::Other => 7, + } +} +fn push(index: u8) -> io::Result { + Ok(match index { + 0 => PushReaction::PushPull, + 1 => PushReaction::Push, + 2 => PushReaction::Popped, + 3 => PushReaction::Immoveable, + 4 => PushReaction::IgnoreEntity, + _ => return Err(invalid("cached push reaction out of range")), + }) +} +fn push_index(push: PushReaction) -> u8 { + match push { + PushReaction::PushPull => 0, + PushReaction::Push => 1, + PushReaction::Popped => 2, + PushReaction::Immoveable => 3, + PushReaction::IgnoreEntity => 4, + } +} + +impl From<&SetType> for SetTypeSnapshot { + fn from(value: &SetType) -> Self { + Self { + name: value.name.to_string(), + can_open_by_hand: value.can_open_by_hand, + can_open_by_wind_charge: value.can_open_by_wind_charge, + arrows: value.arrows, + mobs_only: value.mobs_only, + door_open: value.door_open.to_string(), + door_close: value.door_close.to_string(), + trapdoor_open: value.trapdoor_open.to_string(), + trapdoor_close: value.trapdoor_close.to_string(), + plate_on: value.plate_on.to_string(), + plate_off: value.plate_off.to_string(), + button_on: value.button_on.to_string(), + button_off: value.button_off.to_string(), + } + } +} +impl From for SetType { + fn from(value: SetTypeSnapshot) -> Self { + Self { + name: value.name.into(), + can_open_by_hand: value.can_open_by_hand, + can_open_by_wind_charge: value.can_open_by_wind_charge, + arrows: value.arrows, + mobs_only: value.mobs_only, + door_open: Arc::from(value.door_open), + door_close: Arc::from(value.door_close), + trapdoor_open: Arc::from(value.trapdoor_open), + trapdoor_close: Arc::from(value.trapdoor_close), + plate_on: Arc::from(value.plate_on), + plate_off: Arc::from(value.plate_off), + button_on: Arc::from(value.button_on), + button_off: Arc::from(value.button_off), + } + } +} +impl From<&Sounds> for SoundsSnapshot { + fn from(value: &Sounds) -> Self { + Self { + volume: value.volume, + pitch: value.pitch, + place: value.place.to_string(), + break_: value.break_.to_string(), + fall: value.fall.to_string(), + } + } +} +impl From for Sounds { + fn from(value: SoundsSnapshot) -> Self { + Self { + volume: value.volume, + pitch: value.pitch, + place: Arc::from(value.place), + break_: Arc::from(value.break_), + fall: Arc::from(value.fall), + } + } +} + +impl Snapshot { + fn capture(behaviours: &Behaviours) -> Self { + let tags = behaviours + .all_tags + .iter() + .map(|(key, value)| (key.clone(), value.0.clone())) + .collect(); + let facts = behaviours + .facts + .iter() + .map(|fact| Fact { + flags: fact.flags, + sturdy: fact.sturdy, + weak: fact.weak, + strong: fact.strong, + fluid_kind: fact.fluid.kind() as u8, + fluid_amount: fact.fluid.amount(), + fluid_falling: fact.fluid.falling(), + emission: fact.emission, + dampening: fact.dampening, + hardness: fact.hardness, + push: push_index(fact.push), + instrument: fact.instrument, + shape: fact.shape, + }) + .collect(); + let shapes = behaviours + .shapes + .iter() + .map(|shape| { + let sizes = shape.sizes(); + let mut occupied = Vec::with_capacity(sizes.iter().product()); + for x in 0..sizes[0] { + for y in 0..sizes[1] { + for z in 0..sizes[2] { + occupied.push(shape.is_full(x as i32, y as i32, z as i32)); + } + } + } + let (min, max) = shape.discrete_bounds(); + Shape { + coords: std::array::from_fn(|axis| { + shape + .coordinates( + [ + horizon_core::geometry::Axis::X, + horizon_core::geometry::Axis::Y, + horizon_core::geometry::Axis::Z, + ][axis], + ) + .to_vec() + }), + occupied, + cube: shape.is_cube(), + coordinate_cubes: shape.cube_coordinate_axes(), + min, + max, + singleton: if shape.same_identity(&VoxelShape::block()) { + 1 + } else if shape.same_identity(&VoxelShape::empty()) { + 2 + } else { + 0 + }, + } + }) + .collect(); + let shape_info = behaviours + .shape_info + .iter() + .map(|info| [info.full_faces, info.nonempty_faces, info.wall_cover]) + .collect(); + let f = behaviours.fluid_tags; + let fluid_tags = [ + f.water, + f.lava, + f.supports_frogspawn, + f.supports_lily_pad, + f.supports_sugar_cane_adjacently, + f.bubble_column_can_occupy, + ]; + let blocks = behaviours + .blocks + .iter() + .map(|block| Block { + chain: block.chain.iter().map(|class| *class as u16).collect(), + interfaces: block.interfaces, + set_type: block + .set_type + .as_ref() + .map(|value| SetTypeSnapshot::from(value.as_ref())), + gate_open: block.gate_open.as_ref().map(|v| v.to_string()), + gate_close: block.gate_close.as_ref().map(|v| v.to_string()), + open_sound: block.open_sound.as_ref().map(|v| v.to_string()), + close_sound: block.close_sound.as_ref().map(|v| v.to_string()), + ticks_to_stay_pressed: block.ticks_to_stay_pressed, + max_weight: block.max_weight, + link: block.link.map(|v| v.0), + link2: block.link2.map(|v| v.0), + support_tag: block.support_tag.as_ref().map(|v| v.0.clone()), + support_tag2: block.support_tag2.as_ref().map(|v| v.0.clone()), + feature: block.feature.as_ref().map(|v| v.to_string()), + grow_probability: block.grow_probability, + growth_direction: direction_index(block.growth_direction), + schedule_fluid_ticks: block.schedule_fluid_ticks, + weather: block.weather, + ignite_odds: block.ignite_odds, + burn_odds: block.burn_odds, + sounds: SoundsSnapshot::from(block.sounds.as_ref()), + item: block.item.to_string(), + dynamic_shape: block.dynamic_shape, + }) + .collect(); + let items = behaviours + .items + .iter() + .map(|(name, item)| { + ( + name.clone(), + Item { + block: item.block.0, + wall_block: item.wall_block.map(|v| v.0), + attachment: item.attachment.map(direction_index), + kind: kind_index(item.kind), + game_master: item.game_master, + }, + ) + }) + .collect(); + Self { + tags, + facts, + shapes, + shape_info, + fluid_tags, + blocks, + items, + } + } + + fn restore(self, registry: &BlockRegistry) -> io::Result { + let mut table = StateTable::new(registry)?; + let props = Props::resolve(&mut table); + let named = Named::resolve(&table)?; + if self.facts.len() != table.state_count() + || self.blocks.len() != table.block_count() + || self.shape_info.len() != self.shapes.len() + { + return Err(invalid("cached behaviour table size mismatch")); + } + let all_tags: HashMap> = self + .tags + .into_iter() + .map(|(key, value)| (key, Arc::new(BlockSet(value)))) + .collect(); + let tags = Tags::resolve(&all_tags); + let shapes = self + .shapes + .into_iter() + .map(|wire| { + let shape = match wire.singleton { + 1 => VoxelShape::block(), + 2 => VoxelShape::empty(), + 0 => { + if wire.cube { + let sizes = std::array::from_fn(|axis| { + wire.coords[axis].len().saturating_sub(1) + }); + VoxelShape::from_cube(sizes, &wire.occupied) + .map_err(|e| invalid(e.to_string()))? + } else { + VoxelShape::from_mixed_grid( + wire.coords, + &wire.occupied, + wire.coordinate_cubes, + ) + .map_err(|e| invalid(e.to_string()))? + } + } + _ => return Err(invalid("invalid cached shape identity")), + }; + shape + .with_discrete_bounds(wire.min, wire.max) + .map_err(|e| invalid(e.to_string())) + }) + .collect::>>()?; + let shape_info = self + .shape_info + .into_iter() + .map(|row| ShapeInfo { + full_faces: row[0], + nonempty_faces: row[1], + wall_cover: row[2], + }) + .collect(); + let facts = self + .facts + .into_iter() + .map(|wire| { + if wire.shape < -1 || wire.shape >= shapes.len() as i32 { + return Err(invalid("cached shape index out of range")); + } + let fluid_kind = FluidKind::ALL + .get(wire.fluid_kind as usize) + .copied() + .ok_or_else(|| invalid("cached fluid kind out of range"))?; + Ok(StateFacts { + flags: wire.flags, + sturdy: wire.sturdy, + weak: wire.weak, + strong: wire.strong, + fluid: FluidState::new(fluid_kind, wire.fluid_amount, wire.fluid_falling), + emission: wire.emission, + dampening: wire.dampening, + hardness: wire.hardness, + push: push(wire.push)?, + instrument: wire.instrument, + shape: wire.shape, + }) + }) + .collect::>>()?; + let f = self.fluid_tags; + let fluid_tags = FluidTags { + water: f[0], + lava: f[1], + supports_frogspawn: f[2], + supports_lily_pad: f[3], + supports_sugar_cane_adjacently: f[4], + bubble_column_can_occupy: f[5], + }; + let blocks = self + .blocks + .into_iter() + .map(|wire| { + let chain = wire + .chain + .into_iter() + .map(class) + .collect::>>()?; + let mut classes = ClassSet::default(); + for class in &chain { + classes.insert(*class); + } + let class = chain.first().copied().unwrap_or(Class::Block); + for link in [wire.link, wire.link2].into_iter().flatten() { + if link as usize >= table.block_count() { + return Err(invalid("cached block link out of range")); + } + } + Ok(BlockData { + class, + chain, + classes, + interfaces: wire.interfaces, + set_type: wire.set_type.map(|v| Arc::new(SetType::from(v))), + gate_open: wire.gate_open.map(Arc::from), + gate_close: wire.gate_close.map(Arc::from), + open_sound: wire.open_sound.map(Arc::from), + close_sound: wire.close_sound.map(Arc::from), + ticks_to_stay_pressed: wire.ticks_to_stay_pressed, + max_weight: wire.max_weight, + link: wire.link.map(BlockId), + link2: wire.link2.map(BlockId), + support_tag: wire.support_tag.map(|v| Arc::new(BlockSet(v))), + support_tag2: wire.support_tag2.map(|v| Arc::new(BlockSet(v))), + feature: wire.feature.map(Arc::from), + grow_probability: wire.grow_probability, + growth_direction: direction(wire.growth_direction)?, + schedule_fluid_ticks: wire.schedule_fluid_ticks, + weather: wire.weather, + ignite_odds: wire.ignite_odds, + burn_odds: wire.burn_odds, + sounds: Arc::new(Sounds::from(wire.sounds)), + item: Arc::from(wire.item), + dynamic_shape: wire.dynamic_shape, + }) + }) + .collect::>>()?; + let items = self + .items + .into_iter() + .map(|(name, wire)| { + if wire.block as usize >= table.block_count() + || wire + .wall_block + .is_some_and(|id| id as usize >= table.block_count()) + { + return Err(invalid("cached item block out of range")); + } + Ok(( + name, + ItemBinding { + block: BlockId(wire.block), + wall_block: wire.wall_block.map(BlockId), + attachment: wire.attachment.map(direction).transpose()?, + kind: kind(wire.kind)?, + game_master: wire.game_master, + }, + )) + }) + .collect::>>()?; + let liquid_blocks = FluidBlockBindings::new(registry)?; + let mut dynamic = HashMap::new(); + for (id, fact) in facts.iter().enumerate() { + if fact.shape < 0 { + let state = BlockStateId(id as u32); + if let Some(support) = crate::dynamic_support::DynamicSupport::from_state( + registry.state(state).unwrap(), + )? { + dynamic.insert(state, support); + } + } + } + Ok(Behaviours { + table, + props, + named, + tags, + all_tags, + facts, + shapes, + shape_info, + fluid_tags, + blocks, + items, + liquid_blocks, + dynamic, + occlusion_cache: Default::default(), + growth: Default::default(), + }) + } +} + +pub(super) fn load(registry: &BlockRegistry, root: &Path) -> Option { + load_from(DIR.get()?, registry, root) +} + +fn load_from(dir: &Path, registry: &BlockRegistry, root: &Path) -> Option { + let bytes = fs::read(dir.join(FILE_NAME)).ok()?; + let fingerprint = fingerprint(registry, root).ok()?; + // Validate bounds before decoding untrusted cache contents. + if bytes.len() < 8 + 4 + 32 + 32 || bytes.len() > MAX_FILE_BYTES { + return None; + } + if &bytes[..8] != MAGIC + || u32::from_le_bytes(bytes[8..12].try_into().ok()?) != VERSION + || bytes[12..44] != fingerprint + { + return None; + } + let body = &bytes[76..]; + if Sha256::digest(body).as_slice() != &bytes[44..76] { + return None; + } + let expanded_len = u32::from_le_bytes(body.get(..4)?.try_into().ok()?) as usize; + if expanded_len > MAX_DECODED_BYTES { + return None; + } + let expanded = lz4_flex::decompress_size_prepended(body).ok()?; + let snapshot: Snapshot = postcard::from_bytes(&expanded).ok()?; + snapshot.restore(registry).ok() +} + +pub(super) fn store(registry: &BlockRegistry, root: &Path, behaviours: &Behaviours) { + if let Some(dir) = DIR.get() { + let _ = write(dir, registry, root, behaviours); + } +} + +fn write( + dir: &Path, + registry: &BlockRegistry, + root: &Path, + behaviours: &Behaviours, +) -> io::Result<()> { + let fingerprint = fingerprint(registry, root)?; + let snapshot = Snapshot::capture(behaviours); + let serialized = postcard::to_allocvec(&snapshot).map_err(|e| invalid(e.to_string()))?; + let body = lz4_flex::compress_prepend_size(&serialized); + if body.len() + 76 > MAX_FILE_BYTES { + return Err(invalid("behaviour cache too large")); + } + let mut bytes = Vec::with_capacity(76 + body.len()); + bytes.extend_from_slice(MAGIC); + bytes.extend_from_slice(&VERSION.to_le_bytes()); + bytes.extend_from_slice(&fingerprint); + bytes.extend_from_slice(&Sha256::digest(&body)); + bytes.extend_from_slice(&body); + fs::create_dir_all(dir)?; + let path = dir.join(FILE_NAME); + let tmp = dir.join(format!( + "{FILE_NAME}.tmp-{}-{}", + std::process::id(), + NEXT_TEMP.fetch_add(1, Ordering::Relaxed) + )); + let result = (|| { + let mut file = fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&tmp)?; + file.write_all(&bytes)?; + file.sync_all()?; + fs::rename(&tmp, &path) + })(); + if result.is_err() { + let _ = fs::remove_file(&tmp); + } + result +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::atomic::AtomicU64; + + static NEXT_FIXTURE: AtomicU64 = AtomicU64::new(0); + + struct FixtureDir(PathBuf); + impl Drop for FixtureDir { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } + } + + fn fixture() -> Option { + let source = std::env::var_os("HORIZON_RESOURCES") + .map(PathBuf::from) + .unwrap_or_else(|| { + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3") + }); + if !source.join("block-behaviours.json").exists() { + eprintln!("skipping behaviour cache tests: no client resources"); + return None; + } + let dir = std::env::temp_dir().join(format!( + "horizon-behaviour-cache-test-{}-{}", + std::process::id(), + NEXT_FIXTURE.fetch_add(1, Ordering::Relaxed), + )); + fs::create_dir_all(dir.join("data/minecraft/tags/fluid")).unwrap(); + for file in [ + "facts.json", + "block-behaviours.json", + "gameplay-facts.json", + "block-tags.json", + "cached-collision.nbt", + ] { + fs::copy(source.join(file), dir.join(file)).unwrap(); + } + for entry in fs::read_dir(source.join("data/minecraft/tags/fluid")).unwrap() { + let entry = entry.unwrap(); + if entry.file_type().unwrap().is_file() { + fs::copy( + entry.path(), + dir.join("data/minecraft/tags/fluid") + .join(entry.file_name()), + ) + .unwrap(); + } + } + Some(FixtureDir(dir)) + } + + #[test] + fn resolved_snapshot_roundtrips_all_fields() { + let Some(root) = fixture() else { + return; + }; + let registry = super::super::load_registry(&root.0).unwrap(); + let original = super::super::load_uncached(®istry, &root.0).unwrap(); + let captured = Snapshot::capture(&original); + let restored = captured.restore(®istry).unwrap(); + assert_eq!( + serde_json::to_value(Snapshot::capture(&original)).unwrap(), + serde_json::to_value(Snapshot::capture(&restored)).unwrap(), + "all resolved fields, shape grids and identities should roundtrip", + ); + } + + #[test] + fn cache_invalidates_sources_and_recovers_from_corruption() { + let Some(root) = fixture() else { + return; + }; + // The game's cache folder, apart from the resources. + let dir = root.0.join("game/.horizon-cache"); + let registry = super::super::load_registry(&root.0).unwrap(); + // What `data::load` does once `set_dir` gave it a folder. + let load_or_build = || { + load_from(&dir, ®istry, &root.0).unwrap_or_else(|| { + let built = super::super::load_uncached(®istry, &root.0).unwrap(); + write(&dir, ®istry, &root.0, &built).unwrap(); + built + }) + }; + assert!(load_from(&dir, ®istry, &root.0).is_none()); + let initial = load_or_build(); + assert!(dir.join(FILE_NAME).is_file()); + assert!( + !root.0.join(FILE_NAME).exists(), + "the resource folder stays untouched" + ); + let hit = load_from(&dir, ®istry, &root.0).expect("cache hit"); + assert_eq!(initial.table.state_count(), hit.table.state_count()); + let tags = root.0.join("block-tags.json"); + fs::OpenOptions::new() + .append(true) + .open(&tags) + .unwrap() + .write_all(b"\n") + .unwrap(); + assert!( + load_from(&dir, ®istry, &root.0).is_none(), + "changed resource must invalidate" + ); + load_or_build(); + assert!( + load_from(&dir, ®istry, &root.0).is_some(), + "fallback should replace cache" + ); + let cache_file = dir.join(FILE_NAME); + let mut bytes = fs::read(&cache_file).unwrap(); + bytes[80] ^= 0x80; + fs::write(&cache_file, bytes).unwrap(); + assert!( + load_from(&dir, ®istry, &root.0).is_none(), + "checksum failure must invalidate" + ); + load_or_build(); + assert!(load_from(&dir, ®istry, &root.0).is_some()); + // A snapshot written under another key (another build of the game, + // other resources) is never trusted. + let mut other = fs::read(&cache_file).unwrap(); + other[12] ^= 1; + fs::write(&cache_file, other).unwrap(); + assert!(load_from(&dir, ®istry, &root.0).is_none()); + } +} diff --git a/crates/horizon-world/src/level/mod.rs b/crates/horizon-world/src/level/mod.rs index dbd877fc5..f1f0b21ac 100644 --- a/crates/horizon-world/src/level/mod.rs +++ b/crates/horizon-world/src/level/mod.rs @@ -484,6 +484,13 @@ impl Behaviours { ) -> io::Result { data::load_with_behaviour_json(registry, resources_dir, behaviour) } + + /// Keep a snapshot of the resolved tables in `dir` (a game folder's + /// `.horizon-cache`), so later launches of the same build restore it instead + /// of resolving the resources again. Without it, [`Self::load`] never caches. + pub fn set_cache_dir(dir: std::path::PathBuf) { + data::set_cache_dir(dir); + } pub fn facts(&self, state: BlockStateId) -> &StateFacts { &self.facts[state.0 as usize] } diff --git a/crates/horizon-world/src/nbt.rs b/crates/horizon-world/src/nbt.rs index 7e4d76a41..414767795 100644 --- a/crates/horizon-world/src/nbt.rs +++ b/crates/horizon-world/src/nbt.rs @@ -100,11 +100,13 @@ impl Default for Limits { impl Limits { /// The prepared per-state block tables (`cached-collision.nbt`, /// `block-properties.nbt`, ...): trusted files with a row per block - /// state, which content mods' states grow past the default caps. + /// state, which content mods' states grow past the default caps + /// (vanilla's 35,723 states fill about a tenth of these; ATM10's ported + /// mods, 193,762 states, about half). pub fn tables() -> Self { Self { - max_bytes: 1 << 28, - max_nodes: 16_000_000, + max_bytes: 1 << 30, + max_nodes: 128_000_000, max_depth: 512, } } diff --git a/crates/horizon-world/src/worldgen/feature_catalog.rs b/crates/horizon-world/src/worldgen/feature_catalog.rs index 0b8d56cc4..4db251114 100644 --- a/crates/horizon-world/src/worldgen/feature_catalog.rs +++ b/crates/horizon-world/src/worldgen/feature_catalog.rs @@ -192,7 +192,7 @@ impl FeatureCatalog { if names.is_empty() { return Ok(serde_json::to_string(&format!("#{tag}"))?); } - if names.len() > 8192 { + if names.len() > super::state_provider::MAX_TAG_BLOCKS { return Err(DecodeError::Field("placed-feature tag size")); } names diff --git a/crates/horizon-world/src/worldgen/generator_structures.rs b/crates/horizon-world/src/worldgen/generator_structures.rs index f5487dc33..e78ce0772 100644 --- a/crates/horizon-world/src/worldgen/generator_structures.rs +++ b/crates/horizon-world/src/worldgen/generator_structures.rs @@ -229,8 +229,9 @@ impl StructureState { if cache.len() >= START_CACHE_LIMIT { cache.clear(); } - cache.insert((chunk.x, chunk.z), starts.clone()); - starts + // Another thread may have built the same starts meanwhile: everyone + // keeps the first, so placement state on its pieces is shared. + cache.entry((chunk.x, chunk.z)).or_insert(starts).clone() } /// ChunkGenerator.findNearestMapStructure: the nearest start of one of /// `names` from block `from` (rings: every ring chunk; random spreads: diff --git a/crates/horizon-world/src/worldgen/state_provider.rs b/crates/horizon-world/src/worldgen/state_provider.rs index 37e9c866b..098decdf1 100644 --- a/crates/horizon-world/src/worldgen/state_provider.rs +++ b/crates/horizon-world/src/worldgen/state_provider.rs @@ -93,7 +93,7 @@ impl<'r> StateProviderBindings<'r> { } let mut bound_tags = BTreeMap::new(); for (name, entries) in tags { - if entries.len() > 8192 { + if entries.len() > MAX_TAG_BLOCKS { return Err(DecodeError::Field("provider tag size")); } let values = entries @@ -558,6 +558,11 @@ impl BlockStateProvider { } } +/// Blocks a bound block tag may list (8,192 before content mods, vanilla's +/// tags fit well within it): content mods' blocks join the shared tags +/// (`mineable/pickaxe` takes every stone block of a pack). +pub(crate) const MAX_TAG_BLOCKS: usize = 1 << 20; + /// Bind the pinned registry's full-axis/full-facing/horizontal-facing property /// families. A five-way facing or two-axis property is a different property and /// is intentionally left unchanged. Unrelated properties retain their values. diff --git a/crates/horizon-world/tests/state_provider_bounds.rs b/crates/horizon-world/tests/state_provider_bounds.rs index 033d4675c..23553e6b7 100644 --- a/crates/horizon-world/tests/state_provider_bounds.rs +++ b/crates/horizon-world/tests/state_provider_bounds.rs @@ -41,7 +41,13 @@ fn bound_recursive_providers_and_registry_lists_before_sampling() { let branching = serde_json::json!({"if_true":{"type":"all_of","predicates":vec![serde_json::json!({"type":"true"});7]},"then":{"id":"stone"}}); let oversized = serde_json::json!({"type":"rule_based","rules":vec![branching;8192]}); assert!(BlockStateProvider::from_json(&oversized.to_string(), &bindings).is_err()); - let tags = BTreeMap::from([("horizon:oversized".into(), vec!["stone".into(); 8193])]); + // Content mods' blocks join shared tags: the cap is 2^20 blocks. + let tags = BTreeMap::from([("horizon:large".into(), vec!["stone".into(); 8193])]); + assert!(StateProviderBindings::new(®istry, BTreeSet::new(), &tags).is_ok()); + let tags = BTreeMap::from([( + "horizon:oversized".into(), + vec!["stone".into(); (1 << 20) + 1], + )]); assert!(StateProviderBindings::new(®istry, BTreeSet::new(), &tags).is_err()); let tags = BTreeMap::from([("tag".into(), vec![]), ("minecraft:tag".into(), vec![])]); assert!(StateProviderBindings::new(®istry, BTreeSet::new(), &tags).is_err()); diff --git a/docs/modpacks.md b/docs/modpacks.md index 1d8609cef..2d7663dde 100644 --- a/docs/modpacks.md +++ b/docs/modpacks.md @@ -237,11 +237,19 @@ How the game loads them, without touching vanilla's path: - Loaders that read only `data/minecraft/` (recipes, loot, tags) read every namespace, as vanilla does. With no mods there is only `minecraft`, so nothing changes. - The overlay is cached like the resource-pack overlay and rebuilt when a mod file changes. -- **Limits.** Chunk sections store a block state in 16 bits: vanilla 26.3 has 35,723 states, - so mods may add 29,813. Past that the content root is refused with the states per mod - (Macaw's Doors, Fences, Paths and Trapdoors alone add 36,154: not all four fit). Block - sprites fill texture arrays of 2048 layers, four at most (8,192; vanilla takes 1,250, - identical textures share a layer). Wider state ids are the next step for big packs. +- **Limits.** Chunk sections size their global palette to the registry: 16 bits for vanilla's + 35,723 states (the same chunks, saves and packets as before), one more bit each time content + mods double it, up to 2^20 states (1,048,576). Past that the content root is refused with the + states per mod. ATM10's 42 carried mods take the registry to 193,762 states (18 bits). On the + network the width is the registry's: unchanged for vanilla clients as long as no content mod + is on, wider only between Horizons that share the same mods; a Horizon with content mods still + reads a vanilla server's 16-bit chunks. Block sprites fill texture arrays of 2048 layers, eight + at most (16,384; vanilla takes 1,250, identical textures share a layer; ATM10's 42 take 9,790). +- **Broken mod files load as vanilla loads them.** A blockstate definition that does not bind + (a multipart value the block does not have), a block model that is missing or does not parse, + and a recipe that does not parse are logged and skipped: the block gets no model or the + missing model, the recipe is absent, everything else loads. Vanilla's own files never hit + these paths. - Not in 0.2: multiplayer registry sync (single player and the integrated server first), worlds opened without a mod they used (vanilla turns unknown blocks into air; we will ask first), vanilla ids redefined by a mod (refused). @@ -267,16 +275,32 @@ inspected: └── coverage.json, coverage.md ``` -**Status.** `import`, `inventory`, `carry` and `report` run today: +**Status.** `import`, `inventory`, `fetch`, `carry` and `report` run today, and `port` runs +them all for what a player's machine can port (content mods of the data tier), as the +launcher's "Import modpack" does: ``` cargo run -p horizon-port-pack -- import ~/Downloads/pack.mrpack ~/.horizon/ports/pack cargo run -p horizon-port-pack -- inventory ~/.horizon/ports/pack # --offline reuses modrinth.json +cargo run -p horizon-port-pack -- fetch ~/.horizon/ports/pack # the portable mods' jars, or name them +# --from : copy the player's own jars (an installed instance, a downloads folder) cargo run -p horizon-port-pack -- carry ~/.horizon/ports/pack # content mods with a jar, or name them # --resources : only templates Horizon's tables have cargo run -p horizon-port-pack -- report ~/.horizon/ports/pack # coverage.json and coverage.md +cargo run -p horizon-port-pack -- port ~/Downloads/pack.zip ~/.horizon/ports/pack [--from ] [--resources ] ``` +`fetch` gets each jar the player's way, never from us: copied from a folder they have (matched +by SHA-1, or through a CurseForge app instance's `minecraftinstance.json`), else downloaded from +the link the pack itself gives (a `.mrpack`'s, on the format's hosts only) or, for a CurseForge +pack, the same file on Modrinth's public CDN, found by its SHA-1. A jar is kept only when its +hash matches. A file only CurseForge has is listed as needing a CurseForge API key (Ludovic's +decision; open question below), with the player's installed instance as the way round it. +`/fetch.json` says what happened to each file. On All the Mods 10 8.3's CurseForge zip +(2026-10-17): of the 54 data-tier content mods, 42 come from Modrinth's CDN (84 MB) and 12 need +the key or the instance; `port` then carries the 42 over (0 ported, 42 partial: the harness is a +dev step). + `inventory` reads metadata only: Modrinth answers by file hash, and the jars only if the user's instance has them (an imported instance folder, or jars put in `/jars/`). Without jars, classes come from the reviewed table, Modrinth categories and sides; with jars, from their @@ -286,8 +310,12 @@ only): content 54, library 33, client-ui 26, performance 25, other 14, unknown 2 `carry` copies a jar's `assets//`, `data//`, `data/minecraft/tags/` and `data/c/tags/` into `mods//carry/`, a folder content mod (API 0.2) the game loads with `--dev-mod`, and -keeps what a content mod may not hold (other namespaces, vanilla data) in `outside/`. No class -file is copied. It upgrades from 1.21.1 to 26.3: vanilla-type recipes (ingredients), loot tables +keeps what a content mod may not hold (other namespaces, vanilla data) in `outside/`. Files of +the mod's own content that the game's content loader would refuse whole also go to `outside/`, +listed with the reason in the report's `unreadable`: paths vanilla does not read (upper case), +JSON that does not parse, and shared tags with `"replace": true`. Vanilla skips them one by one. +A byte-order mark is dropped. For ATM10's 42 mods that is 19 files in 7 mods, and the 42 load as +carried. No class file is copied. It upgrades from 1.21.1 to 26.3: vanilla-type recipes (ingredients), loot tables (`conditions` lists become one `condition`, `functions` become `modifier`, kinds move to `type`, tool matches on silk touch or shears become `minecraft:tool/can_silk_touch` and `minecraft:tool/can_shear`), and the `items/` definitions 1.21.4 introduced. @@ -310,7 +338,7 @@ block's name breaking ties (`_pane`, `_gate`, `_trapdoor`). | Format | Read | Jars come from | |---|---|---| | Modrinth `.mrpack` | `modrinth.index.json`: `dependencies` (minecraft, neoforge / fabric-loader / forge / quilt-loader), `files[]` with path, sha1/sha512, `env` (client/server), download URLs; `overrides/`, `client-overrides/`, `server-overrides/` | downloaded from the listed Modrinth URLs, hash-checked | -| CurseForge zip | `manifest.json`: `minecraft.version`, `modLoaders[]`, `files[]` (`projectID`, `fileID`, `required`), `overrides` | the user's installed instance (CurseForge app `minecraftinstance.json` + `mods/`). CurseForge's API needs a key, so we take what the user already downloaded | +| CurseForge zip | `manifest.json`: `minecraft.version`, `modLoaders[]`, `files[]` (`projectID`, `fileID`, `required`), `overrides`. When the overrides hold Crash Assistant's `config/crash_assistant/modlist.json` (All the Mods 10 does), its jar names, mod ids and SHA-1s replace the bare ids | the user's installed instance (CurseForge app `minecraftinstance.json` + `mods/`), or the same file on Modrinth by SHA-1. CurseForge's API needs a key, so we take what the user already downloaded | | Prism / MultiMC instance | `mmc-pack.json` components (`net.minecraft`, `net.neoforged`, `net.fabricmc.fabric-loader`), `instance.cfg`, `.minecraft/mods/*.jar`, packwiz `.pw.toml` files if present | the instance folder | | packwiz | `pack.toml`, `index.toml`, `mods/*.pw.toml` | as listed | | Crash Assistant `modlist.json` | jar name → mod id, name, version, Modrinth SHA-1; the loader entry (NeoForge `21.1.x` is Minecraft 1.21.1). Packs such as ATM10 publish it in their repository | metadata only; jars put in `/jars/` | diff --git a/docs/server.md b/docs/server.md index 0eb0aaf35..30b81f64a 100644 --- a/docs/server.md +++ b/docs/server.md @@ -33,6 +33,12 @@ The dedicated server is the game itself, run headless: `whitelist on|off|list|reload|add|remove`, `ban`, `ban-ip`, `pardon`, `pardon-ip`, `banlist`, `kick` and `say` behave as in `DedicatedServer`. Every other command goes to the game's own dispatcher as the server (permission level 4). The log follows vanilla's Log4j pattern, in `logs/latest.log` and on stdout. +- **Player names.** `usercache.json` in the universe folder works as vanilla's `CachedUserNameToIdResolver`. + Players are added when they join and kept for a month, and the file keeps the 1000 most recently used, + newest first. The console finds a player by name in it, in any case. A name it doesn't hold is asked of the + profile service (`getByName`). If the service doesn't know the name, an offline server uses the offline + player of the lower-case name; an online server answers "That player does not exist". + `tools/test_user_cache_parity.py` checks this against vanilla, with a mock profile service. ### Measured (2026-10-16, Mac mini M4 Pro, main at bd8dff20, release build) @@ -84,7 +90,7 @@ not tick cost, so it is in scope here (see section 9). | Management | Only stdin. There is no service, no backups, no status beyond `list`, no restart after a crash, and a server can't be managed while it runs in the background. | | Packaging | The server is the full game client. Resources come from `horizon-prepare`, which needs a JDK and the official launcher's client install, both awkward on a headless VPS. | | Tick times | The dedicated loop never recorded its tick times in the game clock (only the windowed loop did), so `/tick query` on a dedicated server reported 0 ms. Fixed in PR 2b, which the benchmark needs (section 9). | -| Scale | Not measured with more than one player. The tick is single-threaded, and each remote player costs the hub one tracking and chunk-sending pass. | +| Scale | Measured against vanilla on equal work (section 9, `benchmarks/server-equal-work-2026-10-17/`): 20 TPS up to 50 bots, 9 to 11 TPS at 100, where vanilla's watchdog stops it. Horizon is ahead with few players and when exploring; with 25 to 50 players near spawn vanilla's tick is shorter. | ### Harnesses on main @@ -457,8 +463,22 @@ reproducible by anyone, and show the same gameplay on both sides. - Vanilla's chunk sending yields under load. - Horizon sent each nearby `walk` bot 1.5–4 times as many chunks. - Horizon sent 2–4 times the chunks per second in every multi-bot run. The next run equalises the work - (slower `explore`, a `walk`-only pass) on a quiet machine before anything is published. + Horizon sent 2–4 times the chunks per second in every multi-bot run. +- **Equal-work run (2026-10-17,** `benchmarks/server-equal-work-2026-10-17/`, on the ThinkPad with the bots + on the Mac mini). Horizon now checks moves and paces chunks as vanilla does (#586), the bots wait for the + chunk they stand in, an `--explore-speed` pass keeps both servers at the same rate, and vanilla runs with + `gamerule locator_bar false` (Horizon has no locator bar yet). Results: + - Memory: Horizon's RSS is 1.9 to 2.6 times smaller in every run (vanilla's heap is fixed at 4 GB). + - Walking near spawn: Horizon's tick is shorter with 1 and 10 bots (5.5 against 18.3 ms, 11.5 against + 16.8), and longer with 25 and 50 (20.7 against 16.0, 31.4 against 20.5). Both keep 20 TPS up to 50. + - Exploring at the same rate: Horizon's tick is about half of vanilla's (13.5 against 29.2 ms at 10 bots, + 20.2 against 37.0 at 25). + - At 100 bots: vanilla's watchdog stops it in every pass; Horizon runs at 9 to 11 TPS. + + Two server costs were found and fixed on the way: every chunk section was encoded once per player (#623), + and every packet woke its connection's writer thread (#649). Together they took the 50-bot walking tick + from 281 ms to 31 ms. At 25 walking bots most of what remains is the engine's streaming and block entity + ticks, both of which grow with players (the README has the profile). - **Ownership.** perf-server-1 owns making the tick faster. This work only measures it, files what it finds, and fixes startup, RAM and packaging. diff --git a/launcher/Cargo.lock b/launcher/Cargo.lock index 4a281729d..bde1037b3 100644 --- a/launcher/Cargo.lock +++ b/launcher/Cargo.lock @@ -2664,6 +2664,7 @@ dependencies = [ "gpui", "horizon-bench", "horizon-friends", + "horizon-port-pack", "horizon-prepare", "horizon-telemetry", "horizon-theme", @@ -2680,6 +2681,30 @@ dependencies = [ "zip", ] +[[package]] +name = "horizon-mods" +version = "0.1.0" +dependencies = [ + "log", + "serde", + "serde_json", + "toml 1.1.8+spec-1.1.0", + "zip", +] + +[[package]] +name = "horizon-port-pack" +version = "0.1.0" +dependencies = [ + "horizon-mods", + "serde", + "serde_json", + "sha1", + "toml 1.1.8+spec-1.1.0", + "ureq", + "zip", +] + [[package]] name = "horizon-prepare" version = "0.1.0" diff --git a/launcher/Cargo.toml b/launcher/Cargo.toml index 8f5873f87..34af6e2a1 100644 --- a/launcher/Cargo.toml +++ b/launcher/Cargo.toml @@ -26,6 +26,8 @@ horizon-prepare = { path = "../crates/horizon-prepare" } # Join a friend: invite codes (the game does the joining). horizon-friends = { path = "../crates/horizon-friends" } horizon-bench = { path = "../crates/horizon-bench" } +# Import modpack: the porting pipeline, run on the player's own files (docs/modpacks.md). +horizon-port-pack = { path = "../crates/horizon-port-pack" } # Opt-in data collection: the consent, the outbox and its upload (docs/telemetry.md). horizon-telemetry = { path = "../crates/horizon-telemetry" } # The session file's random name. diff --git a/launcher/README.md b/launcher/README.md index e5edba182..b3dff7fde 100644 --- a/launcher/README.md +++ b/launcher/README.md @@ -25,6 +25,16 @@ The opening, then Play ([clip](docs/opening.webm)): preparation reuses checksum-verified game jars and libraries from the current or another installed Horizon version, so the original Minecraft install is only required when no valid imported copy exists. +- **Modpacks**: import a Java modpack (a Modrinth `.mrpack`, a CurseForge zip, or a Prism, + MultiMC or CurseForge instance folder) and see its coverage report: per mod, ported, partial, + not yet or not needed, with the reason ([docs/modpacks.md](../docs/modpacks.md)). **Port** + gets the jars of what this computer can port today (content mods with data and assets only) + the player's own way: copied from a folder they have, or downloaded from the pack's own links + or the same file on Modrinth's CDN, hash checked; files only CurseForge has need its API key, so + they come from your installed instance. It carries them over for the selected version, and + **Play** starts the game as the pack's own profile (`ports//home/` in the game's home: + its own saves and options) with those mods. Nothing is uploaded or shared: mod jars and ports + stay on this computer. - **Patch notes**: each release's notes, newest first, with a link to GitHub Sponsors. Quiet "Discord" and "Website" links sit in the footer. Data lives in the platform's data directory (`~/.local/share/horizon-launcher` on Linux, @@ -83,6 +93,11 @@ The same binary is a command-line tool; without a subcommand it opens the window horizon-launcher account sign-out horizon-launcher account select play as this account horizon-launcher account offline play without an account + horizon-launcher modpack [list] the imported packs and their coverage + horizon-launcher modpack import a .mrpack, CurseForge zip or instance folder + horizon-launcher modpack port [--from ] [--version ] get the jars and carry them over + horizon-launcher modpack play [--version ] play it as its own profile + horizon-launcher modpack remove horizon-launcher benchmark [--repeats N] measure the launcher (see Performance log) horizon-launcher data [status] what data collection is allowed, the install ID, what waits horizon-launcher data allow|deny performance, crashes, bugs, benchmarks @@ -92,7 +107,7 @@ The same binary is a command-line tool; without a subcommand it opens the window horizon-launcher data benchmark upload a benchmark report, printed first `--json` prints JSON where there is something to report (`versions`, `install`, `notes`, -`next`, `links`, `menu-style`, `update --check`, `logs`, `account`, `benchmark`, `data`). Errors go to stderr with exit code 1. `server` runs in the terminal, +`next`, `links`, `menu-style`, `update --check`, `logs`, `account`, `benchmark`, `data`, `modpack`). Errors go to stderr with exit code 1. `server` runs in the terminal, so lines you type reach its console. `HORIZON_LAUNCHER_HOME` replaces the data folder (for scripts and tests). @@ -192,6 +207,7 @@ bundled so the type is the same on every platform. ## Screenshots ![Server](docs/server.png) +![Modpacks](docs/modpacks.png) ![Versions](docs/versions.png) ![Patch notes](docs/patch-notes.png) ![Settings](docs/settings.png) diff --git a/launcher/docs/modpacks.png b/launcher/docs/modpacks.png new file mode 100644 index 000000000..9abe41c64 Binary files /dev/null and b/launcher/docs/modpacks.png differ diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index 4f5d00ca0..6082d1ee9 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -1709,6 +1709,10 @@ pub enum Msg { /// The selected account's session file for the game about to start (none = offline), or /// why it could not be renewed. Session(Result, String>), + /// A modpack step's line (`modpacks`). + PackLine(String), + /// A modpack step ended: the pack as it now is. + PackDone(Result, String>), } /// A running process; its output arrives as `Msg::Line`, its end as `Msg::Exit`. diff --git a/launcher/src/cli.rs b/launcher/src/cli.rs index df9cfc6b1..b140979b3 100644 --- a/launcher/src/cli.rs +++ b/launcher/src/cli.rs @@ -76,6 +76,33 @@ enum DataCmd { }, } +#[derive(Subcommand)] +enum ModpackCmd { + /// The imported packs and their coverage (the default) + List, + /// Import a .mrpack, a CurseForge zip or an instance folder, and write its coverage report + Import { path: PathBuf }, + /// Get the jars of what this computer can port and carry them over + Port { + /// The pack's folder name, as list shows it + pack: String, + /// A folder with your own jars (an installed instance, a downloads folder) + #[arg(long)] + from: Option, + /// The installed version the ports are for (default: the selected one) + #[arg(long)] + version: Option, + }, + /// Play a ported pack as its own profile + Play { + pack: String, + #[arg(long)] + version: Option, + }, + /// Remove an imported pack: its jars, ports and the profile's saves + Remove { pack: String }, +} + #[derive(Subcommand)] enum Cmd { /// List Horizon releases, or the installed versions @@ -161,6 +188,12 @@ enum Cmd { #[command(subcommand)] action: Option, }, + /// Modpacks: import a Java pack, see its coverage report, port what this computer can and + /// play it as its own profile (docs/modpacks.md) + Modpack { + #[command(subcommand)] + action: Option, + }, /// Serve the Model Context Protocol on stdin and stdout, for AI agents Mcp, /// Measure the launcher: starts (the first is the cold one), page switching, idle CPU and @@ -400,6 +433,7 @@ fn execute(command: Cmd, as_json: bool) -> Result<(), String> { println!("Horizon is in the applications menu."); } } + Cmd::Modpack { action } => modpack(action.unwrap_or(ModpackCmd::List), as_json)?, Cmd::Mcp => unreachable!(), } Ok(()) @@ -591,3 +625,73 @@ fn data(action: DataCmd, as_json: bool) -> Result<(), String> { } Ok(()) } + +fn modpack(action: ModpackCmd, as_json: bool) -> Result<(), String> { + use horizon_launcher::modpacks::{self, Imported}; + use horizon_port_pack::report::Status; + let log = &|line: String| eprintln!("{line}"); + let work = |pack: &str| { + let work = modpacks::ports_dir().join(pack); + modpacks::load(&work).map(|_| work) + }; + let show = |p: &Imported| { + let counts: Vec = Status::ALL + .iter() + .map(|s| format!("{} {}", p.count(*s), s.label())) + .collect(); + if as_json { + json(serde_json::json!({ + "pack": p.slug, "name": p.title(), "summary": p.summary(), + "counts": Status::ALL.iter().map(|s| (s.label(), p.count(*s))).collect::>(), + "portable": p.portable, "ported_mods": p.mods.len(), "jars": p.jars(), + "report": p.work.join("coverage.md"), + })); + } else { + println!( + "{} ({})\n {}\n {}", + p.title(), + p.slug, + p.summary(), + counts.join(", ") + ); + println!(" {} portable, {} carried over", p.portable, p.mods.len()); + if let Some(jars) = p.jars() { + println!(" {jars}"); + } + println!(" {}", p.work.join("coverage.md").display()); + } + }; + match action { + ModpackCmd::List => { + let packs = modpacks::list(); + if packs.is_empty() && !as_json { + println!("No modpack imported yet: horizon-launcher modpack import "); + } + packs.iter().for_each(show); + } + ModpackCmd::Import { path } => show(&modpacks::import(&path, log)?), + ModpackCmd::Port { + pack, + from, + version, + } => { + let tag = backend::resolve_version(version.as_deref()).ok(); + let resources = tag + .filter(|t| backend::is_prepared(t)) + .map(|t| backend::resources(&t)); + show(&modpacks::port( + &work(&pack)?, + resources.as_deref(), + from.as_deref(), + log, + )?) + } + ModpackCmd::Play { pack, version } => { + let mut cmd = backend::client(&backend::resolve_version(version.as_deref())?)?; + modpacks::apply_profile(&mut cmd, &work(&pack)?)?; + attached(cmd)? + } + ModpackCmd::Remove { pack } => modpacks::remove(&work(&pack)?)?, + } + Ok(()) +} diff --git a/launcher/src/lib.rs b/launcher/src/lib.rs index 5df6a7cbb..11023a46d 100644 --- a/launcher/src/lib.rs +++ b/launcher/src/lib.rs @@ -2,6 +2,7 @@ pub mod account; pub mod backend; pub mod desktop; +pub mod modpacks; pub mod mojang; pub mod sources; pub mod telemetry; diff --git a/launcher/src/main.rs b/launcher/src/main.rs index cc66aa08e..ec1dacf78 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -6,6 +6,7 @@ mod collection; mod game_tools; mod markdown; mod mcp; +mod modpacks_page; mod perf; mod theme; @@ -29,14 +30,16 @@ use theme::Theme; enum Page { Play, Versions, + Modpacks, Notes, Settings, } impl Page { - const ALL: [(Page, &'static str); 4] = [ + const ALL: [(Page, &'static str); 5] = [ (Page::Play, "Play"), (Page::Versions, "Versions"), + (Page::Modpacks, "Modpacks"), (Page::Notes, "Patch notes"), (Page::Settings, "Settings"), ]; @@ -161,6 +164,17 @@ struct Launcher { play_pressed: Option, /// `horizon-launcher benchmark` started this window to measure it. bench: Option, + /// The Modpacks page: the imported packs (read when the page first opens), the one shown, + /// the report's filter, and the pipeline step running now with its log. + packs: Vec, + packs_loaded: Option, + pack_open: Option, + pack_filter: Option, + pack_busy: bool, + pack_log: VecDeque, + pack_error: Option, + /// The game starting now plays this pack's profile (its work folder). + starting_pack: Option, } impl Launcher { @@ -266,6 +280,14 @@ impl Launcher { prepare_started: None, play_pressed: None, bench: perf::Bench::from_env(), + packs: Vec::new(), + packs_loaded: None, + pack_open: None, + pack_filter: None, + pack_busy: false, + pack_log: VecDeque::new(), + pack_error: None, + starting_pack: None, }; if this.bench.is_some() { this.perf.keep_local_log(false); @@ -406,6 +428,14 @@ impl Launcher { Err(e) => self.client_error = Some(format!("Playing offline: {e}")), } self.accounts = Accounts::load(); + if let Some(work) = self.starting_pack.take() { + if let Err(e) = + horizon_launcher::modpacks::apply_profile(&mut cmd, &work) + { + self.client_error = Some(e); + continue; + } + } if let Some(code) = self.joining.take() { if let Err(e) = backend::join_friend(&mut cmd, &code) { self.client_error = Some(e); @@ -473,6 +503,13 @@ impl Launcher { Err(e) => SelfUpdate::Failed(e), }; } + Msg::PackLine(line) => { + self.pack_log.push_back(line); + if self.pack_log.len() > LOG_LINES { + self.pack_log.pop_front(); + } + } + Msg::PackDone(result) => self.pack_done(result), Msg::Line(job, line) => { if job == Job::Client && line.starts_with(perf::GAME_FIRST_FRAME) { if let Some(pressed) = self.play_pressed.take() { @@ -671,37 +708,8 @@ impl Launcher { if self.client.is_some() || self.starting_client.is_some() { return; } - self.client_error = None; - self.crash = None; - self.prepare_log.clear(); - // 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 session (a network round trip when the token needs renewing) comes first; - // `drain` starts the game when it arrives. - self.starting_client = Some(tag); - self.play_pressed = Some(Instant::now()); - let tx = self.tx.clone(); - std::thread::spawn(move || { - let session = (|| { - let mut accounts = Accounts::load(); - let Some((a, secrets)) = account::session_for_launch( - &account::Endpoints::live(), - account::store(), - &mut accounts, - )? - else { - return Ok(None); - }; - account::write_session_file( - &backend::game_home().join("sessions"), - &a, - &secrets, - ) - .map(Some) - })(); - let _ = tx.send(Msg::Session(session)); - }); + self.starting_pack = None; + self.start_client(tag); } Mode::Server => { if let Some(server) = &mut self.server { @@ -729,6 +737,38 @@ impl Launcher { } } + /// Starts the game: the session first (a network round trip when the token needs + /// renewing), then `drain` starts the process when it arrives. + fn start_client(&mut self, tag: String) { + self.client_error = None; + self.crash = None; + self.prepare_log.clear(); + // 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 session (a network round trip when the token needs renewing) comes first; + // `drain` starts the game when it arrives. + self.starting_client = Some(tag); + self.play_pressed = Some(Instant::now()); + let tx = self.tx.clone(); + std::thread::spawn(move || { + let session = (|| { + let mut accounts = Accounts::load(); + let Some((a, secrets)) = account::session_for_launch( + &account::Endpoints::live(), + account::store(), + &mut accounts, + )? + else { + return Ok(None); + }; + account::write_session_file(&backend::game_home().join("sessions"), &a, &secrets) + .map(Some) + })(); + let _ = tx.send(Msg::Session(session)); + }); + } + fn choose_folder(&mut self, server: bool, cx: &mut Context) { let paths = cx.prompt_for_paths(PathPromptOptions { files: false, @@ -2774,6 +2814,7 @@ impl Render for Launcher { let content = match self.page { Page::Play => self.play_page(cx), Page::Versions => self.versions_page(cx), + Page::Modpacks => self.modpacks_page(cx), Page::Notes => self.notes_page(cx), Page::Settings => self.settings_page(cx), }; diff --git a/launcher/src/modpacks.rs b/launcher/src/modpacks.rs new file mode 100644 index 000000000..6ff53e4be --- /dev/null +++ b/launcher/src/modpacks.rs @@ -0,0 +1,250 @@ +//! Modpacks: import a Java modpack (a Modrinth `.mrpack`, a CurseForge zip, an instance folder), +//! show what Horizon runs of it, port that on this computer from the player's own downloads, and +//! play it as a profile of its own (docs/modpacks.md). +//! +//! Each pack lives in `ports//` in the game's home: the work folder `horizon-port-pack` +//! writes (pack.json, inventory, the jars, the carried-over mods, the coverage report) and +//! `home/`, the profile's own saves and options. Nothing is uploaded or shared: mod jars are +//! often All Rights Reserved, so the ports stay on the player's machine. +use crate::backend; +use horizon_port_pack::{ + fetch::{Fetched, How}, + import::Pack, + pipeline::{self, Log}, + report::{Row, Status}, +}; +use std::fs; +use std::path::{Path, PathBuf}; +use std::process::Command; + +/// Where imported packs live. +pub fn ports_dir() -> PathBuf { + backend::game_home().join("ports") +} + +/// A pack's folder name: its name in lowercase words joined by dashes. +pub fn slug(name: &str) -> String { + let words: Vec = name + .split(|c: char| !c.is_ascii_alphanumeric()) + .filter(|w| !w.is_empty()) + .map(str::to_ascii_lowercase) + .collect(); + if words.is_empty() { + "pack".into() + } else { + words.join("-") + } +} + +/// An imported pack, as its work folder has it. +pub struct Imported { + pub slug: String, + pub work: PathBuf, + pub pack: Pack, + pub rows: Vec, + pub fetched: Fetched, + /// The carried-over mods the profile loads. + pub mods: Vec, + /// The mods the port step gets and carries over (content, data tier). + pub portable: usize, +} + +impl Imported { + pub fn title(&self) -> &str { + if self.pack.name.is_empty() { + &self.slug + } else { + &self.pack.name + } + } + pub fn count(&self, status: Status) -> usize { + self.rows.iter().filter(|r| r.status == status).count() + } + /// "Minecraft 1.21.1 · neoforge 21.1.259 · 496 mods". + pub fn summary(&self) -> String { + let mut parts = Vec::new(); + if !self.pack.minecraft.is_empty() { + parts.push(format!("Minecraft {}", self.pack.minecraft)); + } + if !self.pack.loader.is_empty() { + parts.push(format!("{} {}", self.pack.loader, self.pack.loader_version)); + } + parts.push(format!("{} files", self.pack.files.len())); + parts.join(" · ") + } + /// What getting the jars did, in one line, or None before it ran. + pub fn jars(&self) -> Option { + if self.fetched.files.is_empty() { + return None; + } + let mut parts = Vec::new(); + for (how, what) in [ + (How::Downloaded, "downloaded"), + (How::Local, "copied from your folder"), + (How::Present, "already here"), + ( + How::NeedsKey, + "only on CurseForge (needs its API key: choose your installed instance instead)", + ), + (How::NotFound, "not found"), + (How::Failed, "failed"), + ] { + let n = self.fetched.count(how); + if n > 0 { + parts.push(format!("{n} {what}")); + } + } + Some(format!("Jars: {}.", parts.join(", "))) + } +} + +/// Reads the pack in `work`. +pub fn load(work: &Path) -> Result { + let pack: Pack = pipeline::read_json(&work.join("pack.json"))?; + let rows: Vec = pipeline::read_json(&work.join("coverage.json")).unwrap_or_default(); + let records: Vec = + pipeline::read_json(&work.join("inventory.json")).unwrap_or_default(); + Ok(Imported { + slug: work + .file_name() + .map(|n| n.to_string_lossy().into_owned()) + .unwrap_or_default(), + work: work.to_path_buf(), + pack, + rows, + fetched: Fetched::load(work), + mods: carried(work), + portable: records.iter().filter(|r| pipeline::portable(r)).count(), + }) +} + +/// Every imported pack, by name. +pub fn list() -> Vec { + let mut packs: Vec = fs::read_dir(ports_dir()) + .into_iter() + .flatten() + .flatten() + .filter_map(|e| load(&e.path()).ok()) + .collect(); + packs.sort_by_key(|p| p.title().to_ascii_lowercase()); + packs +} + +/// The carried-over mods in a work folder (`mods//carry/` with its manifest). +pub fn carried(work: &Path) -> Vec { + let mut mods: Vec = fs::read_dir(work.join("mods")) + .into_iter() + .flatten() + .flatten() + .map(|e| e.path().join("carry")) + .filter(|c| c.join("horizon-mod.toml").is_file()) + .collect(); + mods.sort(); + mods +} + +/// Imports the pack at `path` into `ports//` and writes its coverage report from +/// metadata (Modrinth answers by file hash; nothing is downloaded yet). Importing the same pack +/// again keeps the jars and ports it already has. +pub fn import(path: &Path, log: Log) -> Result { + let pack = horizon_port_pack::import::import(path)?; + let name = if pack.name.is_empty() || pack.name == "modlist" { + path.file_stem() + .map(|s| s.to_string_lossy().into_owned()) + .unwrap_or_default() + } else { + pack.name.clone() + }; + let work = ports_dir().join(slug(&name)); + log(format!("Importing {name} into {}", work.display())); + pipeline::import(path, &work, log)?; + pipeline::inventory(&work, false, log)?; + pipeline::report(&work, log)?; + load(&work) +} + +/// Ports what this computer can: the portable mods' jars (from `from` when the player has them, +/// else the pack's links or Modrinth), carried over for `resources`, then the report again. +pub fn port( + work: &Path, + resources: Option<&Path>, + from: Option<&Path>, + log: Log, +) -> Result { + pipeline::port(work, resources, from, log)?; + load(work) +} + +/// Removes an imported pack: its jars, ports and the profile's saves. +pub fn remove(work: &Path) -> Result<(), String> { + if work.parent() != Some(ports_dir().as_path()) { + return Err(format!("{} is not an imported pack", work.display())); + } + fs::remove_dir_all(work).map_err(|e| format!("{}: {e}", work.display())) +} + +/// The profile's own game home: saves, options.txt, logs. +pub fn profile_home(work: &Path) -> PathBuf { + work.join("home") +} + +/// Turns the game's command into this pack's profile: its own home (the player's options.txt +/// copied in the first time, so keys and video settings carry over) and its carried-over mods. +pub fn apply_profile(cmd: &mut Command, work: &Path) -> Result<(), String> { + let mods = carried(work); + if mods.is_empty() { + return Err("Nothing is ported yet: port the pack first.".into()); + } + let home = profile_home(work); + fs::create_dir_all(&home).map_err(|e| format!("{}: {e}", home.display()))?; + let options = home.join("options.txt"); + let own = backend::game_home().join("options.txt"); + if !options.exists() && own.is_file() { + let _ = fs::copy(own, &options); + } + cmd.current_dir(&home).env("HORIZON_HOME", &home); + for m in mods { + cmd.arg("--dev-mod").arg(m); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn slugs() { + assert_eq!(slug("All the Mods 10"), "all-the-mods-10"); + assert_eq!(slug("Create: Above & Beyond!"), "create-above-beyond"); + assert_eq!(slug(" "), "pack"); + } + + #[test] + fn a_profile_has_its_own_home_and_mods() { + let root = + std::env::temp_dir().join(format!("horizon-launcher-pack-{}", std::process::id())); + let _ = fs::remove_dir_all(&root); + let work = root.join("ports/small"); + let mut cmd = Command::new("true"); + assert!(apply_profile(&mut cmd, &work).is_err()); + for id in ["a", "b"] { + let carry = work.join("mods").join(id).join("carry"); + fs::create_dir_all(&carry).unwrap(); + fs::write(carry.join("horizon-mod.toml"), "").unwrap(); + } + // Left out: no manifest. + fs::create_dir_all(work.join("mods/c/carry")).unwrap(); + apply_profile(&mut cmd, &work).unwrap(); + let args: Vec = cmd + .get_args() + .map(|a| a.to_string_lossy().into_owned()) + .collect(); + assert_eq!(args.len(), 4); + assert_eq!(Path::new(&args[1]), work.join("mods").join("a").join("carry")); + assert_eq!(Path::new(&args[3]), work.join("mods").join("b").join("carry")); + assert_eq!(cmd.get_current_dir(), Some(profile_home(&work).as_path())); + assert!(profile_home(&work).is_dir()); + fs::remove_dir_all(&root).unwrap(); + } +} diff --git a/launcher/src/modpacks_page.rs b/launcher/src/modpacks_page.rs new file mode 100644 index 000000000..2398421c6 --- /dev/null +++ b/launcher/src/modpacks_page.rs @@ -0,0 +1,412 @@ +//! The Modpacks page: import a pack, read its coverage report, port what this computer can and +//! play it as its own profile (`horizon_launcher::modpacks`). +use super::*; +use horizon_launcher::modpacks::{self, Imported}; +use horizon_port_pack::report::Status; +use std::path::PathBuf; + +/// The report's filter: every row, or one status. +pub(super) const FILTERS: [(Option, &str); 5] = [ + (None, "All"), + (Some(Status::Ported), "Ported"), + (Some(Status::Partial), "Partial"), + (Some(Status::NotYet), "Not yet"), + (Some(Status::NotNeeded), "Not needed"), +]; + +/// Lines of the pipeline's log the page shows while it works. +const LOG_SHOWN: usize = 6; + +impl Launcher { + fn open_pack(&self) -> Option<&Imported> { + let slug = self.pack_open.as_ref()?; + self.packs.iter().find(|p| &p.slug == slug) + } + + /// Runs a pipeline step on a thread; its lines and its end arrive as messages. + fn pack_job( + &mut self, + job: impl FnOnce(&dyn Fn(String)) -> Result + Send + 'static, + ) { + if self.pack_busy { + return; + } + self.pack_busy = true; + self.pack_log.clear(); + self.pack_error = None; + let tx = self.tx.clone(); + std::thread::spawn(move || { + let lines = tx.clone(); + let log = move |line: String| { + let _ = lines.send(Msg::PackLine(line)); + }; + let result = job(&log); + let _ = tx.send(Msg::PackDone(result.map(Box::new))); + }); + } + + /// A finished step: the pack's new state, opened. + pub(super) fn pack_done(&mut self, result: Result, String>) { + self.pack_busy = false; + match result { + Ok(pack) => { + self.pack_open = Some(pack.slug.clone()); + self.packs.retain(|p| p.slug != pack.slug); + self.packs.push(*pack); + self.packs.sort_by_key(|p| p.title().to_ascii_lowercase()); + } + Err(e) => self.pack_error = Some(e), + } + } + + fn choose_pack(&mut self, folder: bool, cx: &mut Context) { + let paths = cx.prompt_for_paths(PathPromptOptions { + files: !folder, + directories: folder, + multiple: false, + prompt: Some("Import".into()), + }); + cx.spawn(async move |this, cx| { + if let Ok(Ok(Some(mut paths))) = paths.await { + if let Some(path) = paths.pop() { + let _ = this.update(cx, |this, cx| { + this.pack_job(move |log| modpacks::import(&path, log)); + cx.notify(); + }); + } + } + }) + .detach(); + } + + /// Ports the open pack; `from` is a folder of the player's jars, chosen when `ask`. + fn port_pack(&mut self, ask: bool, cx: &mut Context) { + let Some(work) = self.open_pack().map(|p| p.work.clone()) else { + return; + }; + let resources = self + .selected() + .filter(|tag| backend::is_prepared(tag)) + .map(|tag| backend::resources(tag)); + if !ask { + self.pack_job(move |log| modpacks::port(&work, resources.as_deref(), None, log)); + return; + } + let paths = cx.prompt_for_paths(PathPromptOptions { + files: false, + directories: true, + multiple: false, + prompt: Some("Use these jars".into()), + }); + cx.spawn(async move |this, cx| { + if let Ok(Ok(Some(mut paths))) = paths.await { + if let Some(from) = paths.pop() { + let _ = this.update(cx, |this, cx| { + this.pack_job(move |log| { + modpacks::port(&work, resources.as_deref(), Some(&from), log) + }); + cx.notify(); + }); + } + } + }) + .detach(); + } + + fn play_pack(&mut self, work: PathBuf) { + let Some(tag) = self.selected().cloned() else { + return; + }; + if self.client.is_some() || self.starting_client.is_some() { + return; + } + self.starting_pack = Some(work); + self.start_client(tag); + } + + pub(super) fn modpacks_page(&mut self, cx: &mut Context) -> Div { + let t = self.theme; + if self.packs_loaded.is_none() { + self.packs = modpacks::list(); + if self.pack_open.is_none() { + self.pack_open = self.packs.first().map(|p| p.slug.clone()); + } + self.packs_loaded = Some(Instant::now()); + } + let header = page_header( + "Modpacks", + "Bring a Java modpack: see what Horizon runs of it, port that on this computer from \ + your own downloads, and play it as its own profile. Nothing is uploaded or shared.", + &t, + ); + let busy = self.pack_busy; + let pick = |id: &'static str, label: &'static str, folder: bool| { + let b = button(id, label, Kind::Secondary, &t); + if busy { + disabled(b) + } else { + b.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + this.choose_pack(folder, cx); + cx.notify(); + })) + } + }; + let actions = div() + .flex() + .flex_wrap() + .items_center() + .gap(px(8.)) + .child(pick("import-file", "Import a pack file…", false)) + .child(pick("import-folder", "Import an instance folder…", true)) + .child(text(theme::CAPTION).text_color(t.muted).child( + ".mrpack (Modrinth), .zip (CurseForge), or a Prism, MultiMC or CurseForge instance", + )); + let mut page = div() + .flex() + .flex_col() + .gap(px(32.)) + .child(header) + .child(actions); + if busy || !self.pack_log.is_empty() { + page = page.child(self.pack_log_plate(&t)); + } + if let Some(e) = &self.pack_error { + page = page.child(notice(e.clone(), &t)); + } + if self.packs.is_empty() { + return page.child(centered( + "No modpack yet. Import one to see what Horizon can run of it.", + &t, + )); + } + if self.packs.len() > 1 { + let mut list = div().flex().flex_col().border_t_1().border_color(t.rule); + for (i, p) in self.packs.iter().enumerate() { + let open = self.pack_open.as_ref() == Some(&p.slug); + let slug = p.slug.clone(); + let right = if open { + caps("Open").text_color(t.accent).into_any_element() + } else { + button(format!("open-{slug}"), "Open", Kind::Ghost, &t) + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + this.pack_open = Some(slug.clone()); + cx.notify(); + })) + .into_any_element() + }; + list = list.child(index_row( + i + 1, + p.title().to_string(), + Some(p.summary().into_any_element()), + Some(right), + &t, + )); + } + page = page.child(list); + } + match self.pack_detail(cx) { + Some(detail) => page.child(detail), + None => page, + } + } + + fn pack_log_plate(&self, t: &Theme) -> Div { + let start = self.pack_log.len().saturating_sub(LOG_SHOWN); + plate(t) + .flex() + .flex_col() + .gap(px(4.)) + .p(px(16.)) + .child( + caps(if self.pack_busy { "Working" } else { "Done" }) + .text_color(if self.pack_busy { t.accent } else { t.muted }), + ) + .children(self.pack_log.iter().skip(start).map(|line| { + text(theme::CAPTION) + .font_family(theme::MONO) + .text_color(t.ink_soft) + .truncate() + .child(line.clone()) + })) + } + + fn pack_detail(&self, cx: &mut Context) -> Option
{ + let t = self.theme; + let p = self.open_pack()?; + let mut counts = div().flex().gap(px(12.)); + for (status, label) in FILTERS.iter().skip(1) { + let status = status.expect("the first filter is All"); + let color = match status { + Status::Ported => t.success, + Status::Partial => t.accent, + _ => t.ink, + }; + counts = counts.child( + plate(&t) + .flex() + .flex_col() + .flex_1() + .gap(px(4.)) + .p(px(16.)) + .child( + text(theme::DISPLAY) + .text_color(color) + .child(p.count(status).to_string()), + ) + .child(caps(*label).text_color(t.muted)), + ); + } + let can_play = self.selected().is_some_and(|tag| backend::is_prepared(tag)); + let running = self.client.is_some() || self.starting_client.is_some(); + let mut buttons = div().flex().flex_wrap().items_center().gap(px(8.)); + let play_label = if p.mods.is_empty() { + "Play".to_string() + } else { + format!("Play with {} mods", p.mods.len()) + }; + let mut play = button("pack-play", play_label, Kind::Primary, &t); + if p.mods.is_empty() || !can_play || running || self.pack_busy { + play = disabled(play); + } else { + let work = p.work.clone(); + play = play.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + this.play_pack(work.clone()); + cx.notify(); + })); + } + let port_label = format!("Port {} mods", p.portable); + let mut port = button("pack-port", port_label, Kind::Secondary, &t); + let mut from = button("pack-from", "Use jars from my instance…", Kind::Ghost, &t); + if self.pack_busy || p.portable == 0 { + port = disabled(port); + from = disabled(from); + } else { + port = port.on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.port_pack(false, cx); + cx.notify(); + })); + from = from.on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.port_pack(true, cx); + cx.notify(); + })); + } + let confirming = self.confirm_remove.as_deref() == Some(p.slug.as_str()); + let slug = p.slug.clone(); + let work = p.work.clone(); + let remove = button( + "pack-remove", + if confirming { + "Remove for good" + } else { + "Remove" + }, + if confirming { + Kind::Secondary + } else { + Kind::Ghost + }, + &t, + ) + .when(confirming, |b| b.text_color(t.danger)) + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + if this.confirm_remove.as_ref() == Some(&slug) { + match modpacks::remove(&work) { + Ok(()) => { + this.packs.retain(|p| p.slug != slug); + this.pack_open = this.packs.first().map(|p| p.slug.clone()); + } + Err(e) => this.pack_error = Some(e), + } + this.confirm_remove = None; + } else { + this.confirm_remove = Some(slug.clone()); + } + cx.notify(); + })); + buttons = buttons.child(play).child(port).child(from).child(remove); + let mut notes = vec![format!( + "Port gets the jars of the {} mods this computer can port today (content with data \ + and assets only) from the pack's own links, Modrinth, or your folder, and carries \ + them over. The ports stay here, in {}.", + p.portable, + short(&p.work) + )]; + if let Some(jars) = p.jars() { + notes.push(jars); + } + if !can_play { + notes.push("Playing needs a prepared Horizon version (Play page).".into()); + } + let mut rows: Vec<&horizon_port_pack::report::Row> = p + .rows + .iter() + .filter(|r| self.pack_filter.is_none_or(|s| r.status == s)) + .collect(); + rows.sort_by(|a, b| (a.status, &a.key).cmp(&(b.status, &b.key))); + let filter = segments("pack-filter", &FILTERS, self.pack_filter, &t, { + let this = cx.entity().downgrade(); + move |value, _, cx| { + let _ = this.update(cx, |this, cx| { + this.pack_filter = value; + cx.notify(); + }); + } + }); + let mut list = div().flex().flex_col().border_t_1().border_color(t.rule); + for (i, r) in rows.iter().enumerate() { + let color = match r.status { + Status::Ported => t.success, + Status::Partial => t.accent, + Status::NotYet => t.ink_soft, + Status::NotNeeded => t.muted, + }; + let name = if r.name.is_empty() || r.name == r.key { + r.key.clone() + } else { + format!("{} ({})", r.name, r.key) + }; + let detail = format!("{} · {} · {}: {}", r.class, r.tier, r.license, r.reason); + list = list.child(index_row( + i + 1, + name, + Some(detail.into_any_element()), + Some( + caps(r.status.label()) + .flex_none() + .text_color(color) + .into_any_element(), + ), + &t, + )); + } + Some( + div() + .flex() + .flex_col() + .gap(px(24.)) + .child( + div() + .flex() + .flex_col() + .gap(px(4.)) + .child( + text(theme::TITLE) + .text_color(t.ink) + .child(p.title().to_string()), + ) + .child(text(theme::UI).text_color(t.muted).child(p.summary())), + ) + .child(counts) + .child(buttons) + .children(self.client_error.clone().map(|e| notice(e, &t))) + .children(notes.into_iter().map(|n| { + text(theme::CAPTION) + .max_w(px(720.)) + .text_color(t.muted) + .child(n) + })) + .child(div().flex().child(filter)) + .child(list), + ) + } +} diff --git a/launcher/src/sources.rs b/launcher/src/sources.rs index aafdb51e4..f6a9753ea 100644 --- a/launcher/src/sources.rs +++ b/launcher/src/sources.rs @@ -426,16 +426,26 @@ fn make_link(target: &Path, link: &Path) -> std::io::Result<()> { std::os::unix::fs::symlink(target, link) } -/// Symlinks need developer mode on Windows: a junction for a folder, a hard link for a file. +/// Symlinks need developer mode on Windows: a junction for a folder, a hard link for a file (a +/// copy on another volume). #[cfg(windows)] fn make_link(target: &Path, link: &Path) -> std::io::Result<()> { + use std::os::windows::process::CommandExt; if !target.is_dir() { return fs::hard_link(target, link).or_else(|_| fs::copy(target, link).map(|_| ())); } + // Always quoted: cmd reads `&`, `^` or `(` in an unquoted path (a user folder may have + // them) as its own syntax. A Windows path never holds a quote. + let quoted = |path: &Path| { + let mut arg = std::ffi::OsString::from("\""); + arg.push(path); + arg.push("\""); + arg + }; let status = std::process::Command::new("cmd") .args(["/C", "mklink", "/J"]) - .arg(link) - .arg(target) + .raw_arg(quoted(link)) + .raw_arg(quoted(target)) .stdout(std::process::Stdio::null()) .status()?; match status.success() { diff --git a/launcher/src/sources_tests.rs b/launcher/src/sources_tests.rs index f603aaf0a..381d19f27 100644 --- a/launcher/src/sources_tests.rs +++ b/launcher/src/sources_tests.rs @@ -94,15 +94,25 @@ fn resolve_with( } } +/// Whether `a` and `b` are one file, as a symlink (Unix) or a hard link (Windows) makes them: +/// a byte written through `a` shows in `b`. Both are left as they were. +fn same_file(a: &Path, b: &Path) -> bool { + use std::io::Write; + let (before_a, before_b) = (fs::read(a).unwrap(), fs::read(b).unwrap()); + let mut file = fs::OpenOptions::new().write(true).open(a).unwrap(); + file.write_all(&[!before_a[0]]).unwrap(); + drop(file); + let same = fs::read(b).unwrap() != before_b; + fs::write(a, &before_a).unwrap(); + same +} + /// The linked folder is the official layout, pointing at the launcher's files. fn assert_linked(linked: &Path, base: &Path, jar: &str) { let v = linked.join("versions/26.3"); assert_eq!(fs::read(v.join("26.3.jar")).unwrap(), CLIENT); assert_eq!(fs::read(v.join("26.3.json")).unwrap(), MANIFEST); - assert_eq!( - fs::canonicalize(v.join("26.3.jar")).unwrap(), - fs::canonicalize(base.join(jar)).unwrap() - ); + assert!(same_file(&v.join("26.3.jar"), &base.join(jar))); assert_eq!( fs::read(linked.join("libraries/org/lib/1/lib-1.jar")).unwrap(), b"lib" @@ -110,6 +120,19 @@ fn assert_linked(linked: &Path, base: &Path, jar: &str) { assert!(linked.join("assets/indexes/30.json").is_file()); } +/// Folders whose names cmd would read as its own syntax unquoted are still linked (Windows makes +/// a junction through `mklink /J`; a name without spaces isn't quoted by itself). +#[test] +fn folders_with_ampersands_are_linked() { + let s = Scratch::new("a&b(c)"); + let jar = "libraries/com/mojang/minecraft/26.3/minecraft-26.3-client.jar"; + let dir = s.launcher("Prism&Co", "", jar); + let linked = s.0.join("linked&here"); + let got = resolve_with(&dir, &[], &linked, &Mojang::new(MANIFEST)).unwrap(); + assert_eq!(got, linked); + assert_linked(&linked, &dir, jar); +} + #[test] fn maven_names_become_repository_paths() { assert_eq!( @@ -295,10 +318,9 @@ fn relinking_follows_the_launcher_the_files_now_come_from() { let modrinth = s.0.join("ModrinthApp"); s.write("ModrinthApp/meta/versions/26.3/26.3.jar", CLIENT); resolve_with(&modrinth, &[], &linked, &mojang).unwrap(); - assert_eq!( - fs::canonicalize(linked.join("versions/26.3/26.3.jar")).unwrap(), - fs::canonicalize(modrinth.join("meta/versions/26.3/26.3.jar")).unwrap() - ); + let jar = linked.join("versions/26.3/26.3.jar"); + assert!(same_file(&jar, &modrinth.join("meta/versions/26.3/26.3.jar"))); + assert!(!same_file(&jar, &prism.join(prism_jar))); // The Modrinth App folder has no libraries or assets: the old links are gone, and the // folders they pointed at are untouched. assert!(fs::symlink_metadata(linked.join("libraries")).is_err()); diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java b/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java new file mode 100644 index 000000000..e41c693ee --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java @@ -0,0 +1,382 @@ +package dev.horizonlab.parity; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.zip.Deflater; +import java.util.zip.Inflater; +import net.minecraft.SharedConstants; +import net.minecraft.core.UUIDUtil; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.ProtocolInfo; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.protocol.Packet; +import net.minecraft.network.protocol.common.ClientboundDisconnectPacket; +import net.minecraft.network.protocol.common.ClientboundKeepAlivePacket; +import net.minecraft.network.protocol.common.ClientboundPingPacket; +import net.minecraft.network.protocol.common.ServerboundClientInformationPacket; +import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; +import net.minecraft.network.protocol.common.ServerboundKeepAlivePacket; +import net.minecraft.network.protocol.common.ServerboundPongPacket; +import net.minecraft.network.protocol.common.custom.BrandPayload; +import net.minecraft.network.protocol.configuration.ClientboundFinishConfigurationPacket; +import net.minecraft.network.protocol.configuration.ClientboundSelectKnownPacks; +import net.minecraft.network.protocol.configuration.ConfigurationProtocols; +import net.minecraft.network.protocol.configuration.ServerboundFinishConfigurationPacket; +import net.minecraft.network.protocol.configuration.ServerboundSelectKnownPacks; +import net.minecraft.network.protocol.game.ClientboundChunkBatchFinishedPacket; +import net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket; +import net.minecraft.network.protocol.game.ClientboundSystemChatPacket; +import net.minecraft.network.protocol.game.ClientboundTrackedWaypointPacket; +import net.minecraft.network.protocol.game.GameProtocols; +import net.minecraft.network.protocol.game.ServerboundAcceptTeleportationPacket; +import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; +import net.minecraft.network.protocol.game.ServerboundChunkBatchReceivedPacket; +import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket; +import net.minecraft.network.protocol.game.ServerboundPlayerInputPacket; +import net.minecraft.network.protocol.game.ServerboundPlayerLoadedPacket; +import net.minecraft.network.protocol.handshake.ClientIntent; +import net.minecraft.network.protocol.handshake.ClientIntentionPacket; +import net.minecraft.network.protocol.handshake.HandshakeProtocols; +import net.minecraft.network.protocol.login.ClientboundLoginCompressionPacket; +import net.minecraft.network.protocol.login.ClientboundLoginDisconnectPacket; +import net.minecraft.network.protocol.login.ClientboundLoginFinishedPacket; +import net.minecraft.network.protocol.login.LoginProtocols; +import net.minecraft.network.protocol.login.ServerboundHelloPacket; +import net.minecraft.network.protocol.login.ServerboundLoginAcknowledgedPacket; +import net.minecraft.server.level.ClientInformation; +import net.minecraft.world.entity.PositionMoveRotation; +import net.minecraft.world.entity.player.Input; +import net.minecraft.world.phys.Vec3; + +/** + * Players made of vanilla's own protocol code, for the locator bar + * (tools/test_locator_bar_parity.py). Each joins in offline mode, answers + * keep-alives, pings, teleports and chunk batches, and records every + * ClientboundTrackedWaypointPacket it receives, decoded by vanilla's codec + * (a byte vanilla would not read is an error) and summarised from its bytes. + * + *

{@code LocatorBarClient HOST PORT OUT} takes its script on stdin, one + * line at a time: {@code step LABEL} (the label the next packets are recorded + * under), {@code join NAME}, {@code cmd NAME COMMAND...}, {@code shift NAME + * on|off} (the shift key, as ServerboundPlayerInputPacket), {@code fly NAME + * on|off} (ServerboundPlayerAbilitiesPacket), {@code quit NAME} and {@code + * end}, which writes each player's lines to OUT. + */ +public final class LocatorBarClient { + private static volatile String step = "start"; + private static final Map NAMES = new LinkedHashMap<>(); + + public static void main(String[] args) throws Exception { + ParityData.init(); + String host = args[0]; + int port = Integer.parseInt(args[1]); + Path output = Path.of(args[2]); + Map players = new LinkedHashMap<>(); + BufferedReader script = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)); + String line; + while ((line = script.readLine()) != null) { + String[] w = line.trim().split(" ", 3); + try { + switch (w[0]) { + case "step" -> step = w[1]; + case "join" -> { + Player p = new Player(w[1]); + players.put(w[1], p); + NAMES.put(UUIDUtil.createOfflinePlayerUUID(w[1]), w[1]); + p.join(host, port); + } + case "cmd" -> players.get(w[1]).command(w[2]); + case "shift" -> players.get(w[1]).send(new ServerboundPlayerInputPacket( + new Input(false, false, false, false, false, w[2].equals("on"), false))); + case "fly" -> { + net.minecraft.world.entity.player.Abilities abilities = new net.minecraft.world.entity.player.Abilities(); + abilities.flying = w[2].equals("on"); + players.get(w[1]).send(new net.minecraft.network.protocol.game.ServerboundPlayerAbilitiesPacket(abilities)); + } + case "quit" -> players.get(w[1]).close(); + case "end" -> { + try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(output, StandardCharsets.UTF_8))) { + for (Player p : players.values()) { + synchronized (p.lines) { + for (String l : p.lines) out.println(p.name + " " + l); + } + } + } + for (Player p : players.values()) p.close(); + System.exit(0); + } + default -> throw new IllegalArgumentException("unknown script line " + line); + } + } catch (Exception e) { + Player p = w.length > 1 ? players.get(w[1]) : null; + if (p != null) p.record("error " + w[0] + " " + e); + else System.err.println("error " + line + ": " + e); + } + } + System.exit(1); + } + + /** The waypoint packet's fields, read back from its bytes (the operation and type are private enums). */ + static String waypoint(byte[] payload) { + FriendlyByteBuf b = new FriendlyByteBuf(Unpooled.wrappedBuffer(payload)); + b.readVarInt(); // the packet id + String operation = switch (b.readVarInt()) { + case 0 -> "track"; + case 1 -> "untrack"; + case 2 -> "update"; + default -> "operation?"; + }; + String id = b.readBoolean() ? NAMES.getOrDefault(b.readUUID(), "uuid?") : "name:" + b.readUtf(); + String style = b.readUtf(); + String color = b.readBoolean() ? String.format("#%02x%02x%02x", b.readUnsignedByte(), b.readUnsignedByte(), b.readUnsignedByte()) : "-"; + String target = switch (b.readVarInt()) { + case 0 -> "empty"; + case 1 -> "block " + b.readVarInt() + " " + b.readVarInt() + " " + b.readVarInt(); + case 2 -> "chunk " + b.readVarInt() + " " + b.readVarInt(); + case 3 -> "azimuth " + b.readFloat(); + default -> "type?"; + }; + return operation + " " + id + " " + style + " " + color + " " + target; + } + + /** One connection: login and configuration in the caller's thread, then play on its own. */ + private static final class Player { + final String name; + final List lines = new ArrayList<>(); + private Socket socket; + private DataInputStream in; + private OutputStream out; + private int threshold = -1; + private ProtocolInfo inbound; + private ProtocolInfo outbound; + private PositionMoveRotation position = new PositionMoveRotation(Vec3.ZERO, Vec3.ZERO, 0, 0); + private boolean loaded; + private final Object replyLock = new Object(); + private int replies; + + Player(String name) { + this.name = name; + } + + /** Runs a command and waits for its feedback, so a slow server still takes each step in its step. */ + void command(String command) throws Exception { + synchronized (replyLock) { + int before = replies; + send(new ServerboundChatCommandPacket(command)); + long until = System.nanoTime() + 30_000_000_000L; + while (replies == before) { + long left = (until - System.nanoTime()) / 1_000_000L; + if (left <= 0) { + record("error no reply to /" + command); + return; + } + replyLock.wait(left); + } + } + } + + void record(String line) { + synchronized (lines) { + lines.add("step=" + step + " " + line); + } + } + + void close() throws Exception { + if (socket != null) socket.close(); + } + + void join(String host, int port) throws Exception { + socket = new Socket(); + socket.connect(new InetSocketAddress(host, port), 10_000); + socket.setTcpNoDelay(true); + in = new DataInputStream(socket.getInputStream()); + out = socket.getOutputStream(); + outbound = HandshakeProtocols.SERVERBOUND; + send(new ClientIntentionPacket(SharedConstants.getProtocolVersion(), host, port, ClientIntent.LOGIN)); + outbound = LoginProtocols.SERVERBOUND; + inbound = LoginProtocols.CLIENTBOUND; + send(new ServerboundHelloPacket(name, UUIDUtil.createOfflinePlayerUUID(name))); + while (true) { + Packet p = read().packet; + if (p instanceof ClientboundLoginCompressionPacket c) { + threshold = c.getCompressionThreshold(); + } else if (p instanceof ClientboundLoginFinishedPacket) { + send(ServerboundLoginAcknowledgedPacket.INSTANCE); + break; + } else if (p instanceof ClientboundLoginDisconnectPacket d) { + throw new IllegalStateException("login refused: " + d.reason().getString()); + } + } + outbound = ConfigurationProtocols.SERVERBOUND; + inbound = ConfigurationProtocols.CLIENTBOUND; + send(new ServerboundCustomPayloadPacket(new BrandPayload("vanilla"))); + send(new ServerboundClientInformationPacket(ClientInformation.createDefault())); + while (true) { + Packet p = read().packet; + if (p instanceof ClientboundSelectKnownPacks k) { + send(new ServerboundSelectKnownPacks(k.knownPacks())); + } else if (p instanceof ClientboundKeepAlivePacket k) { + send(new ServerboundKeepAlivePacket(k.getId())); + } else if (p instanceof ClientboundPingPacket k) { + send(new ServerboundPongPacket(k.getId())); + } else if (p instanceof ClientboundFinishConfigurationPacket) { + send(ServerboundFinishConfigurationPacket.INSTANCE); + break; + } else if (p instanceof ClientboundDisconnectPacket d) { + throw new IllegalStateException("disconnected: " + d.reason().getString()); + } + } + GameProtocols.Context context = new GameProtocols.Context() { + @Override + public boolean hasInfiniteMaterials() { + return true; + } + + @Override + public boolean canUseCommandBlocks() { + return true; + } + }; + outbound = GameProtocols.SERVERBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(ParityData.access), context); + inbound = GameProtocols.CLIENTBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(ParityData.access)); + Thread reader = new Thread(this::play, name); + reader.setDaemon(true); + reader.start(); + } + + private void play() { + try { + while (true) { + Received r = read(); + Packet p = r.packet; + if (p instanceof ClientboundTrackedWaypointPacket) { + record(waypoint(r.payload)); + } else if (p instanceof ClientboundSystemChatPacket) { + synchronized (replyLock) { + replies++; + replyLock.notifyAll(); + } + } else if (p instanceof ClientboundKeepAlivePacket k) { + send(new ServerboundKeepAlivePacket(k.getId())); + } else if (p instanceof ClientboundPingPacket k) { + send(new ServerboundPongPacket(k.getId())); + } else if (p instanceof ClientboundPlayerPositionPacket t) { + position = PositionMoveRotation.calculateAbsolute(position, t.change(), t.relatives()); + Vec3 at = position.position(); + send(new ServerboundAcceptTeleportationPacket(t.id(), at.x, at.y, at.z, position.yRot(), position.xRot())); + // Only the spawn teleport is answered with a position too (VanillaProtocolClient). + if (!loaded) { + send(new ServerboundMovePlayerPacket.PosRot(at, position.yRot(), position.xRot(), false, false)); + send(new ServerboundPlayerLoadedPacket()); + loaded = true; + } + } else if (p instanceof ClientboundChunkBatchFinishedPacket) { + send(new ServerboundChunkBatchReceivedPacket(64.0F)); + } else if (p instanceof ClientboundDisconnectPacket d) { + record("disconnected " + d.reason().getString()); + return; + } + } + } catch (Exception e) { + if (!socket.isClosed()) record("error play " + e); + } + } + + private record Received(Packet packet, byte[] payload) {} + + private Received read() throws Exception { + byte[] payload = readFrame(); + ByteBuf buf = Unpooled.wrappedBuffer(payload); + Packet packet = inbound.codec().decode(buf); + if (buf.readableBytes() > 0) { + throw new IllegalStateException(packet.type().id() + " was larger than expected: " + buf.readableBytes() + " bytes left"); + } + return new Received(packet, payload); + } + + synchronized void send(Packet packet) throws Exception { + ByteBuf body = Unpooled.buffer(); + ((ProtocolInfo) outbound).codec().encode(body, packet); + byte[] raw = new byte[body.readableBytes()]; + body.readBytes(raw); + ByteBuf frame = Unpooled.buffer(); + if (threshold >= 0) { + ByteBuf inner = Unpooled.buffer(); + if (raw.length >= threshold) { + writeVarInt(inner, raw.length); + Deflater d = new Deflater(); + d.setInput(raw); + d.finish(); + byte[] tmp = new byte[raw.length + 64]; + while (!d.finished()) inner.writeBytes(tmp, 0, d.deflate(tmp)); + d.end(); + } else { + writeVarInt(inner, 0); + inner.writeBytes(raw); + } + writeVarInt(frame, inner.readableBytes()); + frame.writeBytes(inner); + } else { + writeVarInt(frame, raw.length); + frame.writeBytes(raw); + } + byte[] bytes = new byte[frame.readableBytes()]; + frame.readBytes(bytes); + out.write(bytes); + out.flush(); + } + + private byte[] readFrame() throws Exception { + byte[] frame = new byte[readVarInt(in)]; + in.readFully(frame); + if (threshold < 0) return frame; + ByteArrayInputStream header = new ByteArrayInputStream(frame); + int dataLength = readVarInt(header); + int skip = frame.length - header.available(); + if (dataLength == 0) return java.util.Arrays.copyOfRange(frame, skip, frame.length); + Inflater inflater = new Inflater(); + inflater.setInput(frame, skip, frame.length - skip); + byte[] raw = new byte[dataLength]; + int n = inflater.inflate(raw); + inflater.end(); + if (n != dataLength) throw new IllegalStateException("decompressed " + n + " of " + dataLength + " bytes"); + return raw; + } + } + + private static int readVarInt(InputStream in) throws Exception { + int value = 0; + for (int shift = 0; shift < 35; shift += 7) { + int b = in.read(); + if (b < 0) throw new java.io.EOFException(); + value |= (b & 0x7F) << shift; + if ((b & 0x80) == 0) return value; + } + throw new IllegalStateException("VarInt too big"); + } + + private static void writeVarInt(ByteBuf buf, int value) { + while ((value & ~0x7F) != 0) { + buf.writeByte((value & 0x7F) | 0x80); + value >>>= 7; + } + buf.writeByte(value); + } +} diff --git a/tools/profile_behaviour_cache.py b/tools/profile_behaviour_cache.py new file mode 100644 index 000000000..00ba65a6d --- /dev/null +++ b/tools/profile_behaviour_cache.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +"""Measure the resource-dependent Behaviours::load portion of client startup. + +Build the behaviour_load_profile example in the checkout to be measured first: + cargo build --release -p horizon-world --example behaviour_load_profile + +Run the profiler from the repository root with the matching example binary. +This measures behaviour loading, not time to first rendered frame. With +--cache-dir, the example reads and refreshes the resolved snapshot there, as the +client does with its .horizon-cache folder: the first load of a run builds it +when it is missing, the next loads restore it. +""" +import argparse +import json +import re +import statistics +import subprocess +from pathlib import Path + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--binary", type=Path, default=Path("target/release/examples/behaviour_load_profile")) + parser.add_argument("--resources", type=Path, default=Path(".local/horizon-client/26.3")) + parser.add_argument("--cache-dir", type=Path, + help="snapshot folder (default: no snapshot, every load is uncached)") + parser.add_argument("--repeats", type=int, default=5) + parser.add_argument("--output", type=Path) + args = parser.parse_args() + if args.repeats < 1: + parser.error("--repeats must be at least one") + samples = [] + for _ in range(args.repeats): + command = [str(args.binary.resolve()), str(args.resources.resolve())] + if args.cache_dir: + command.append(str(args.cache_dir.resolve())) + run = subprocess.run(command, text=True, capture_output=True, check=True) + values = {key: float(value) for key, value in + re.findall(r"^(registry_ms|behaviours_[0-2]_ms)=([\d.]+)$", run.stderr, re.M)} + if len(values) != 4: + raise RuntimeError(f"incomplete profile output: {run.stderr}") + samples.append(values) + summary = {key: {"median_ms": round(statistics.median(row[key] for row in samples), 3), + "samples_ms": [row[key] for row in samples]} + for key in samples[0]} + result = {"scope": "behaviour loader only; not full client startup", + "binary": str(args.binary), "resources": str(args.resources), "repeats": args.repeats, + "cache_dir": str(args.cache_dir) if args.cache_dir else None, + "metrics": summary} + print(json.dumps(result, indent=2)) + if args.output: + args.output.parent.mkdir(parents=True, exist_ok=True) + args.output.write_text(json.dumps(result, indent=2) + "\n") + + +if __name__ == "__main__": + main() diff --git a/tools/release_train.py b/tools/release_train.py index a1af28449..70e7b0287 100644 --- a/tools/release_train.py +++ b/tools/release_train.py @@ -39,7 +39,17 @@ EXPORTERS = "mods/horizon/src/test/java/dev/horizonlab/parity/" # What players run. Documentation (*.md), tests, benchmarks and harness probes (examples/) never count. GAME_PATHS = ["crates/", "xtask/", "packaging/", "Cargo.toml", "Cargo.lock", "research-lock.json"] -LAUNCHER_PATHS = ["launcher/", "crates/horizon-prepare/", "crates/horizon-theme/", "crates/horizon-telemetry/"] +# Every crate the launcher builds in (cargo metadata --manifest-path launcher/Cargo.toml: the path packages). +LAUNCHER_PATHS = [ + "launcher/", + "crates/horizon-bench/", + "crates/horizon-friends/", + "crates/horizon-mods/", + "crates/horizon-port-pack/", + "crates/horizon-prepare/", + "crates/horizon-telemetry/", + "crates/horizon-theme/", +] LAUNCHER_IGNORED = ["launcher/links.json", "launcher/docs/", "launcher/tests/"] FOOTER = { diff --git a/tools/server_bench.py b/tools/server_bench.py index 5a64c80cf..c79d6aab1 100755 --- a/tools/server_bench.py +++ b/tools/server_bench.py @@ -8,10 +8,18 @@ then measures for a while: - MSPT (average, P50, P95, P99) from each server's own `/tick query`, asked every 5 seconds on the console: vanilla's ServerTickRateManager numbers, which Horizon reproduces, so both measure themselves the same way; - RSS and CPU of the server's whole process tree, once a second; -- chunks the bots receive per second (new terrain while the `explore` bots fly outwards); +- chunks the bots receive per second (new terrain while the `explore` bots fly outwards), chunks sent again + while a bot still held them, and the moves the server refused (pull-backs; "moved too quickly" and + "moved wrongly" lines); - the start time to `Done`, and every bot that failed to join or was dropped. - python3 tools/server_bench.py --servers horizon,vanilla --bots 1,10,25 --seconds 120 + python3 tools/server_bench.py --servers horizon,vanilla --bots 1,10,25 --behaviour walk,explore + +The bots can run on another machine, so they don't take the server's CPU: `--bots-via 'ssh macmini'` with +`--bots-bin` naming horizon-bots there and `--connect` the address it reaches this machine at. `--nice 19` +runs every server at the lowest priority (a shared machine). The explore bots fly as fast as each server +sends them chunks, so a faster server gets more terrain to generate; `--explore-speed` slows them to a rate +both servers keep up with, for equal work. The vanilla side runs the pinned server jar from tools/lab.py prepare (your own copy). It needs Java 25. The Horizon side runs target/release/horizon-server with the engine beside it; build both first: @@ -186,8 +194,16 @@ def server_command(kind, directory, args): return [server, '--dir', directory, 'run'] -def run_once(kind, bots, args, out): - directory = out / f'{kind}-{bots}' +def bots_command(args): + """horizon-bots here, or through `--bots-via` (for example `ssh macmini`) on another machine.""" + if args.bots_via: + return [*args.bots_via.split(), args.bots_bin or 'horizon-bots'] + return [args.bots_bin or BIN / f'horizon-bots{EXE}'] + + +def run_once(kind, bots, behaviour, args, out): + name = f'{kind}-{behaviour}-{bots}' + directory = out / name shutil.rmtree(directory, ignore_errors=True) directory.mkdir(parents=True) port = free_port() @@ -195,19 +211,27 @@ def run_once(kind, bots, args, out): (directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items())) (directory / 'eula.txt').write_text('eula=true\n') command = server_command(kind, directory, args) + if args.nice: + command = ['nice', '-n', str(args.nice), *command] started = time.time() - server = Server(command, directory, out / f'{kind}-{bots}.log') - result = {'server': kind, 'bots': bots} + server = Server(command, directory, out / f'{name}.log') + result = {'server': kind, 'bots': bots, 'behaviour': behaviour} try: server.wait_for(r'/INFO\]: Done \(', 600) result['start_seconds'] = round(time.time() - started, 2) + # Every bot appears on the world spawn itself, not spread around it, so both servers see the same paths. + server.console('gamerule respawn_radius 0') + for rule in filter(None, args.gamerule.split(',')): + server.console('gamerule ' + rule.replace('=', ' ')) time.sleep(5) - report = out / f'{kind}-{bots}-bots.json' + report = out / f'{name}-bots.json' total = bots * args.join_every + args.warmup + args.seconds + # The summary comes back on stdout after the per-second lines, so it works over ssh too. bot_process = subprocess.Popen( - [BIN / f'horizon-bots{EXE}', '--server', f'127.0.0.1:{port}', '--bots', str(bots), - '--behaviour', args.behaviour, '--join-every', str(args.join_every), '--seconds', str(int(total)), - '--seed', str(args.seed), '--report', report], + [*bots_command(args), '--server', f'{args.connect}:{port}', '--bots', str(bots), + '--behaviour', behaviour, '--join-every', str(args.join_every), '--seconds', str(int(total)), + '--seed', str(args.seed), '--report', '-', + *(['--explore-speed', str(args.explore_speed)] if args.explore_speed else [])], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) bot_lines = [] threading.Thread(target=lambda: bot_lines.extend(bot_process.stdout), daemon=True).start() @@ -229,11 +253,27 @@ def run_once(kind, bots, args, out): if (t := tick_query(server)) is not None: ticks.append(t) next_query = now + 5 - measured = [json.loads(l) for l in list(bot_lines) if l.startswith('{')] - window = [l for l in measured if l['t'] >= measured[-1]['t'] - args.seconds] if measured else [] bot_process.wait(timeout=total + 120) - summary = json.loads(report.read_text()) if report.exists() else {} + time.sleep(0.5) + measured, rest = [], [] + for line in list(bot_lines): + try: + sample = json.loads(line) + except ValueError: + sample = None + if isinstance(sample, dict) and 't' in sample: + measured.append(sample) + else: + rest.append(line) + window = [l for l in measured if l['t'] >= measured[-1]['t'] - args.seconds] if measured else [] + summary_text = ''.join(rest) + summary = json.loads(summary_text) if summary_text.strip() else {} + report.write_text(json.dumps(summary, indent=2) + '\n') mean = lambda xs: round(statistics.fmean(xs), 2) if xs else None + per_bot = summary.get('per_bot', []) + by_behaviour = {} + for b in per_bot: + by_behaviour.setdefault(b['behaviour'], []).append(b) result.update({ 'mspt': {k: mean([t[k] for t in ticks]) for k in ('average', 'p50', 'p95', 'p99')}, 'mspt_worst_p99': max((t['p99'] for t in ticks), default=None), @@ -242,6 +282,19 @@ def run_once(kind, bots, args, out): 'rss_mb': {'mean': mean([r / 1e6 for r in rss]), 'max': round(max(rss) / 1e6, 1) if rss else None}, 'cpu_percent': mean(cpu), 'chunks_per_second': mean([l['chunks_per_second'] for l in window]), + # Over the measured window: different chunks reaching the bots, chunks they had dropped and got + # again (walking back), and ticks the bots stood still waiting for their own chunk. + **({k + '_per_second': round((window[-1][k] - window[0][k]) / max(len(window) - 1, 1), 2) + for k in ('unique', 'forgotten', 'waited_ticks')} if len(window) > 1 else {}), + 'chunks_total': summary.get('chunks'), + # Per bot over the whole run: every chunk received, the different ones, and those sent again + # while the bot still held them. + 'chunks_per_bot': {k: mean([b['chunks'] for b in v]) for k, v in by_behaviour.items()}, + 'unique_chunks_per_bot': {k: mean([b['unique_chunks'] for b in v]) for k, v in by_behaviour.items()}, + 'resent': summary.get('resent'), + 'pulled_back': summary.get('pulled_back'), + 'moved_too_quickly': sum('moved too quickly!' in l for l in server.since(0)), + 'moved_wrongly': sum('moved wrongly!' in l for l in server.since(0)), 'bots_joined': summary.get('joined'), 'bots_failed': [b for b in summary.get('per_bot', []) if b.get('error')], 'tick_queries': len(ticks), @@ -257,23 +310,34 @@ def main(): parser = argparse.ArgumentParser(description=__doc__.split('\n\n')[0]) parser.add_argument('--servers', default='horizon,vanilla') parser.add_argument('--bots', default='1,10,25,50') - parser.add_argument('--behaviour', default='mixed') + parser.add_argument('--behaviour', default='walk,explore', help='one pass per behaviour (comma-separated)') parser.add_argument('--join-every', type=float, default=1.0) parser.add_argument('--warmup', type=int, default=60) parser.add_argument('--seconds', type=int, default=300) parser.add_argument('--seed', type=int, default=12345) + parser.add_argument('--explore-speed', type=float, default=0, + help='blocks a second the explore bots fly (default: the bots\' 22, as fast as chunks ' + 'come); a speed both servers keep up with makes the explore pass equal work') + parser.add_argument('--gamerule', default='', + help="more game rules for both servers, 'name=value,...' (locator_bar=false: Horizon has " + "no locator bar yet)") parser.add_argument('--heap', default='4g', help='the Java heap, -Xms and -Xmx (vanilla)') parser.add_argument('--out', type=Path, default=None, help='the report (JSON)') + parser.add_argument('--nice', type=int, default=0, help='run the servers under nice -n N') + parser.add_argument('--bots-via', default='', help="a command prefix to run the bots elsewhere ('ssh host')") + parser.add_argument('--bots-bin', default='', help='horizon-bots (on the --bots-via machine)') + parser.add_argument('--connect', default='127.0.0.1', help='the address the bots reach the server at') args = parser.parse_args() out = lab.LOCAL / 'server-bench' out.mkdir(parents=True, exist_ok=True) runs = [] - for bots in [int(b) for b in args.bots.split(',')]: - for kind in args.servers.split(','): - print(f'{kind}, {bots} bots...', flush=True) - result = run_once(kind, bots, args, out) - runs.append(result) - print(json.dumps(result), flush=True) + for behaviour in args.behaviour.split(','): + for bots in [int(b) for b in args.bots.split(',')]: + for kind in args.servers.split(','): + print(f'{kind}, {bots} {behaviour} bots...', flush=True) + result = run_once(kind, bots, behaviour, args, out) + runs.append(result) + print(json.dumps(result), flush=True) report = { 'kind': 'horizon-server-benchmark', 'date': datetime.datetime.now(datetime.timezone.utc).isoformat(timespec='seconds'), @@ -286,13 +350,16 @@ def main(): path = args.out or out / 'report.json' path.parent.mkdir(parents=True, exist_ok=True) path.write_text(json.dumps(report, indent=2) + '\n') - print(f'\n{"server":8} {"bots":>4} {"start s":>8} {"MSPT":>6} {"P95":>6} {"P99":>6} {"RSS MB":>7} {"CPU %":>6} {"chunks/s":>8} {"joined":>6}') + print(f'\n{"server":8} {"pass":8} {"bots":>4} {"start s":>8} {"MSPT":>6} {"P95":>6} {"P99":>6} {"RSS MB":>7} ' + f'{"CPU %":>6} {"chunks/s":>8} {"/bot":>6} {"resent":>6} {"pulled":>6} {"joined":>6}') for r in runs: if 'error' in r: - print(f'{r["server"]:8} {r["bots"]:>4} failed: {r["error"].splitlines()[0]}') + print(f'{r["server"]:8} {r["behaviour"]:8} {r["bots"]:>4} failed: {r["error"].splitlines()[0]}') continue - print(f'{r["server"]:8} {r["bots"]:>4} {r["start_seconds"]:>8} {r["mspt"]["average"]!s:>6} {r["mspt"]["p95"]!s:>6} ' - f'{r["mspt"]["p99"]!s:>6} {r["rss_mb"]["mean"]!s:>7} {r["cpu_percent"]!s:>6} {r["chunks_per_second"]!s:>8} ' + per_bot = r['chunks_per_bot'].get(r['behaviour']) + print(f'{r["server"]:8} {r["behaviour"]:8} {r["bots"]:>4} {r["start_seconds"]:>8} {r["mspt"]["average"]!s:>6} ' + f'{r["mspt"]["p95"]!s:>6} {r["mspt"]["p99"]!s:>6} {r["rss_mb"]["mean"]!s:>7} {r["cpu_percent"]!s:>6} ' + f'{r["chunks_per_second"]!s:>8} {per_bot!s:>6} {r["resent"]!s:>6} {r["pulled_back"]!s:>6} ' f'{r["bots_joined"]!s:>6}') print(f'\nWrote {path}') diff --git a/tools/test_dedicated_server_parity.py b/tools/test_dedicated_server_parity.py index c30abe4b0..0f40893c8 100644 --- a/tools/test_dedicated_server_parity.py +++ b/tools/test_dedicated_server_parity.py @@ -35,7 +35,8 @@ exactly: shift-click changes and its lit and cooking totals; - the console lines about the player, the chat, `list`, `say` and `stop`; - the files the servers leave: server.properties (but the date line), - eula.txt, ops.json, whitelist.json, banned-players.json, banned-ips.json. + eula.txt, ops.json, whitelist.json, banned-players.json, banned-ips.json, + usercache.json (but the date the player's entry expires). Online mode needs Mojang's session servers and is not tested. """ @@ -238,7 +239,8 @@ def console_lines(lines): def server_files(directory): out = [] - for name in ['server.properties', 'eula.txt', 'ops.json', 'whitelist.json', 'banned-players.json', 'banned-ips.json']: + for name in ['server.properties', 'eula.txt', 'ops.json', 'whitelist.json', 'banned-players.json', 'banned-ips.json', + 'usercache.json']: path = directory / name if not path.exists(): out.append(f'{name}: missing') @@ -249,6 +251,9 @@ def server_files(directory): text = re.sub(r'^(#[^\n]*\n)#[^\n]*\n', r'\1#\n', text) # Random per server, and each side has its own port. text = re.sub(r'\n(management-server-secret|server-port)=[^\n]*', r'\n\1=', text) + if name == 'usercache.json': + # A month from when the player joined. + text = re.sub(r'"expiresOn":"[^"]*"', '"expiresOn":""', text) out.append(f'{name}: {text!r}') return out diff --git a/tools/test_locator_bar_parity.py b/tools/test_locator_bar_parity.py new file mode 100644 index 000000000..bc7d21750 --- /dev/null +++ b/tools/test_locator_bar_parity.py @@ -0,0 +1,259 @@ +#!/usr/bin/env python3 +"""The locator bar on a dedicated server, against vanilla's, seen by vanilla clients. + +A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat +world vanilla generated first (players appear at its spawn), with the same server.properties (offline, +creative, view distance 4) and two operators. LocatorBarClient, two +players made of vanilla's protocol code (any byte vanilla would not read is an error), joins Alice and then +Bob, who fly as creative players do (a player floating without flying falls on the server every tick), and +records every ClientboundTrackedWaypointPacket each of them receives. Bob then: + +- teleports one block, three blocks, out of Alice's view, one chunk on, really far (an azimuth) and turns + around her, and back; +- turns invisible and visible, joins a red team (by the console), has it turned blue, leaves it; +- crouches and stands up, wears a carved pumpkin and takes it off; +- turns spectator and moves, then creative again; + +the console turns the `locator_bar` game rule off and on, and Bob leaves. The waypoint packets each player +received in each step are compared exactly: operation, player, style, colour and the block, chunk or angle. +""" +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 + from .test_dedicated_server_parity import Server, free_port +except ImportError: + import lab + import test_rust_parity as base + from test_dedicated_server_parity import Server, free_port + +NAME = 'locator-bar-parity' +PROPERTIES = { + 'online-mode': 'false', + 'white-list': 'false', + 'level-seed': '12345', + 'level-type': 'minecraft\\:flat', + 'generate-structures': 'false', + 'gamemode': 'creative', + 'force-gamemode': 'true', + 'view-distance': '4', + 'simulation-distance': '4', + 'spawn-protection': '0', + 'max-players': '5', + 'pause-when-empty-seconds': '-1', +} +PLAYERS = ['Alice', 'Bob'] + +# (label, [what the console does], [what the players do]); every step then waits for its packets. +SCRIPT = [ + ('alice-joins', [], ['join Alice', 'fly Alice on', 'cmd Alice tp @s 0.5 100 0.5']), + ('bob-joins', [], ['join Bob', 'fly Bob on', 'cmd Bob tp @s 2.5 100 0.5']), + ('near', [], ['cmd Bob tp @s 5.5 100 0.5']), + ('one-block', [], ['cmd Bob tp @s 6.5 100 0.5']), + ('three-blocks', [], ['cmd Bob tp @s 9.5 100 0.5']), + ('out-of-view', [], ['cmd Bob tp @s 100.5 100 0.5']), + ('next-chunk', [], ['cmd Bob tp @s 116.5 100 0.5']), + ('really-far', [], ['cmd Bob tp @s 1000.5 100 0.5']), + ('turn-around', [], ['cmd Bob tp @s 1000.5 100 300.5']), + ('half-degree', [], ['cmd Bob tp @s 1000.5 100 302.5']), + ('invisible', [], ['cmd Bob effect give @s minecraft:invisibility infinite 0 true']), + ('visible', [], ['cmd Bob effect clear @s']), + ('back-near', [], ['cmd Bob tp @s 3.5 100 0.5']), + ('red-team', ['team add red', 'team modify red color red', 'team join red Bob'], []), + ('blue-team', ['team modify red color blue'], []), + ('no-team', ['team leave Bob'], []), + # Crouching needs the ground: a flying player who holds shift goes down instead. + ('landed', [], ['cmd Bob tp @s 3.5 -60 0.5', 'fly Bob off']), + ('crouch', [], ['shift Bob on']), + ('stand', [], ['shift Bob off']), + ('flying-again', [], ['fly Bob on', 'cmd Bob tp @s 3.5 100 0.5']), + ('pumpkin', [], ['cmd Bob item replace entity @s armor.head with minecraft:carved_pumpkin']), + ('no-pumpkin', [], ['cmd Bob item replace entity @s armor.head with minecraft:air']), + ('rule-off', ['gamerule locator_bar false'], []), + ('moves-without', [], ['cmd Bob tp @s 4.5 100 0.5']), + ('rule-on', ['gamerule locator_bar true'], []), + ('spectator', [], ['cmd Bob gamemode spectator', 'cmd Bob tp @s 5.5 100 0.5']), + ('creative', [], ['cmd Bob gamemode creative', 'cmd Bob tp @s 6.5 100 0.5']), + ('bob-leaves', [], ['quit Bob']), +] +STEP_SECONDS = 2.0 +# Vanilla's server moves a player who just joined on their own first ticks (a tiny step and back), which tracks +# them where they spawned; Horizon does not simulate its guests. These steps compare what the bar shows at +# their end (Bob has moved by then) rather than every packet. +SETTLING_STEPS = {'alice-joins', 'bob-joins'} + + +def shown(lines): + """The transcript with each settling step reduced to what each player's bar shows at its end.""" + out, state = [], {} + + def flush(step): + for (receiver, source), rest in sorted(state.items()): + out.append(f'{receiver} step={step} shows {source} {rest}') + state.clear() + + current = None + for line in lines: + receiver, step, *packet = line.split(' ') + step = step.removeprefix('step=') + if current in SETTLING_STEPS and step != current: + flush(current) + current = step + if step not in SETTLING_STEPS or len(packet) < 3: + out.append(line) + elif packet[0] == 'untrack': + state.pop((receiver, packet[1]), None) + else: + state[(receiver, packet[1])] = ' '.join(packet[2:]) + if current in SETTLING_STEPS: + flush(current) + return out + + +def ops_json(): + """Both players at level 4, by their offline UUIDs.""" + import hashlib + import json + import uuid as uuidlib + entries = [] + for name in PLAYERS: + digest = bytearray(hashlib.md5(f'OfflinePlayer:{name}'.encode()).digest()) + digest[6] = (digest[6] & 0x0F) | 0x30 + digest[8] = (digest[8] & 0x3F) | 0x80 + entries.append({'uuid': str(uuidlib.UUID(bytes=bytes(digest))), 'name': name, 'level': 4, + 'bypassesPlayerLimit': False}) + return json.dumps(entries, indent=2) + + +def server_dir(directory, world=None): + if directory.exists(): + shutil.rmtree(directory) + directory.mkdir(parents=True) + port = free_port() + props = dict(PROPERTIES, **{'server-port': str(port)}) + (directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items()), + encoding='iso-8859-1') + (directory / 'eula.txt').write_text('eula=true\n') + (directory / 'ops.json').write_text(ops_json()) + if world is not None: + shutil.copytree(world, directory / 'world') + (directory / 'world' / 'session.lock').unlink(missing_ok=True) + return port + + +def generate_world(server_command, build): + """The flat world both servers load, made once by vanilla: players appear at its spawn itself + (respawn_radius 0), on the ground at 0 -60 0, and the chunks around it are saved.""" + directory = build / 'generate' + server_dir(directory) + server = Server(server_command, directory, 'generate') + try: + server.wait_for(r'Done \(') + for command, reply in [('gamerule respawn_radius 0', 'respawn_radius'), + ('setworldspawn 0 -60 0', 'Set the world spawn'), + ('forceload add -96 -96 96 96', 'force loaded'), + ('save-all flush', 'Saved the game')]: + server.console(command) + server.wait_for(reply, timeout=120) + server.console('forceload remove all') + finally: + server.stop() + return directory / 'world' + + +def run_side(label, server_command, client_command, build, 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: + 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) + try: + for step, console, actions in SCRIPT: + client.stdin.write(f'step {step}\n') + client.stdin.flush() + for command in console: + server.console(command) + for action in actions: + client.stdin.write(action + '\n') + client.stdin.flush() + if action.startswith('join'): + # Joining takes the world's first chunks: wait for the server to place the player. + server.wait_for(rf'{action.split()[1]} joined the game', timeout=120) + time.sleep(STEP_SECONDS) + client.stdin.write('end\n') + client.stdin.flush() + 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 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) + import json + 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 = generate_world(vanilla_server, build) + vanilla = run_side('vanilla', vanilla_server, client, build, world) + horizon = run_side('horizon', horizon_server, client, build, world) + errors = [l for l in vanilla + horizon if ' error ' in f' {l} ' or 'disconnected' in l] + vanilla, horizon = shown(vanilla), shown(horizon) + # Each player's packets in their order; which player's line comes first in a step doesn't matter. + 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 len(vanilla) < 20: + raise AssertionError(f'only {len(vanilla)} waypoint packets: 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], + packets=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='Locator bar waypoints (ClientboundTrackedWaypointPacket) two vanilla-codec players receive on a ' + 'dedicated server: block, chunk and azimuth connections as one moves, invisibility, team colours, ' + 'crouching, a carved pumpkin, the locator_bar game rule, spectator mode and leaving.')) + print(f'locator bar: {len(vanilla)} waypoint packets identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') diff --git a/tools/test_mob_ai_parity.py b/tools/test_mob_ai_parity.py index a32ec2cf9..9bb244ce9 100644 --- a/tools/test_mob_ai_parity.py +++ b/tools/test_mob_ai_parity.py @@ -262,6 +262,12 @@ def scenarios(): add('bee_hive_rain', 300, [HIVE, 'mob minecraft:bee 0.5 -58 0.5 0 162 persistent'], 'rain') add('bee_hive_morning', 1000, [HIVE, 'mob minecraft:bee 0.5 -58 0.5 0 163 persistent'], 'time=22900 daycycle') add('bee_hive_honey', 2600, [HIVE, 'mob minecraft:bee 0.5 -58 0.5 0 164 persistent nectar']) + # Mobs dropped into deep water (#542): FloatGoal users float at the surface (their navigation + # can float, so Mob.jumpInLiquid adds 0.04 a tick), zombies sink. + for species, seed in [('pig', 171), ('cow', 172), ('wolf', 173), ('cat', 174), ('horse', 175), + ('witch', 176), ('vindicator', 177), ('zombie', 178)]: + add(f'{species}_deep_water', 160, ['fill -4 -64 -4 4 -61 4 minecraft:water', + f'mob minecraft:{species} 0.5 -62 0.5 0 {seed} persistent']) return s diff --git a/tools/test_online_mode_parity.py b/tools/test_online_mode_parity.py index 8e349bf0d..e6679b7b5 100644 --- a/tools/test_online_mode_parity.py +++ b/tools/test_online_mode_parity.py @@ -21,7 +21,10 @@ Also checked, with vanilla's messages: - a token the session service rejects ("Failed to log in: Invalid session"); - a join the session service forgets (the server: "Failed to verify username!"); - no account at all (the client refuses before joining); -- a verified player the whitelist refuses. +- a verified player the whitelist refuses; +- `whitelist add` in online mode: a player who joined before is known by name + and let in; another name is looked up through the services' profile lookup, + and one with no account is refused ("That player does not exist"). Horizon's dedicated server then runs the same steps in online mode (`HORIZON_API_DISCOVERY` points it at the mock too): it must give the client @@ -100,6 +103,14 @@ class Services: query = dict(urllib.parse.parse_qsl(url.query)) if url.path == '/minecraft/client': return self.reply(200, services.discovery()) + if url.path.startswith('/users/profiles/minecraft/'): + # profiles/getByName: the account with that name, ignoring case. + name = urllib.parse.unquote(url.path.rsplit('/', 1)[1]) + with services.lock: + services.log.append(('lookup', name)) + if name.lower() == PLAYER.lower(): + return self.reply(200, {'id': PLAYER_UUID, 'name': PLAYER}) + return self.reply(404, {'error': 'NOT_FOUND', 'errorMessage': f'Couldn\'t find any profile with name {name}'}) if url.path == '/session/minecraft/hasJoined': with services.lock: ok = services.joined.get(query.get('username')) == query.get('serverId') @@ -107,11 +118,15 @@ class Services: if ok: return self.reply(200, {'id': PLAYER_UUID, 'name': query['username'], 'properties': []}) return self.reply(204) + with services.lock: + services.log.append(('unknown', url.path)) self.reply(404, {'error': 'NotFound', 'path': url.path}) def do_POST(self): body = json.loads(self.rfile.read(int(self.headers.get('Content-Length', 0))) or b'{}') if self.path != '/session/minecraft/join': + with services.lock: + services.log.append(('unknown', self.path)) return self.reply(404, {'error': 'NotFound'}) token, profile, server_id = body.get('accessToken'), body.get('selectedProfile'), body.get('serverId') accepted = token in (GOOD, FORGET) and profile == PLAYER_UUID @@ -135,7 +150,8 @@ class Services: 'session': endpoints(join='/session/minecraft/join', verify='/session/minecraft/hasJoined', getProfileById='/session/minecraft/profile/{profileId}'), 'player': {'endpoints': {}}, - 'profiles': {'endpoints': {'getTexture': {'validUris': [self.url + '/texture/{textureId}']}}}, + 'profiles': {'endpoints': {'getTexture': {'validUris': [self.url + '/texture/{textureId}']}, + 'getByName': {'uri': self.url + '/users/profiles/minecraft/{name}'}}}, 'telemetry': {'endpoints': {}}, }} @@ -189,8 +205,19 @@ def player_lines(lines): return out +def wait_after(server, mark, pattern, timeout=30): + """A line matching `pattern` among those the server printed after `mark`.""" + regex = re.compile(pattern) + deadline = time.time() + timeout + while time.time() < deadline: + if any(regex.search(l) for l in server.lines[mark:]): + return + time.sleep(0.2) + raise AssertionError(f'no {pattern!r} after line {mark} of {server.log_path.name}') + + def run_side(label, command, binary, build, services, discovery): - """The five logins against one server; its checks and its log about the player.""" + """The six logins against one server; its checks and its log about the player.""" port = free_port() directory = build / label shutil.rmtree(directory, ignore_errors=True) @@ -261,6 +288,24 @@ def run_side(label, command, binary, build, services, discovery): check('not whitelisted: client message', got['error'], 'disconnected: You are not white-listed on this server!') check('not whitelisted: verified first', [(e[0], e[3]) for e in services.take_log()], [('join', True), ('verify', True)]) + + # 6. Whitelisted by name: the server knows the player from their first join (its name cache), so it + # asks the services nothing, and lets them in. + mark = len(server.lines) + server.console('whitelist add tester') + wait_after(server, mark, r'Added Tester to the whitelist') + check('whitelist add: known player, no lookup', services.take_log(), []) + got = run_client(binary, build, port, f'{label}-whitelisted', GOOD, discovery) + wait_after(server, mark, r'Tester left the game') + check('whitelisted: connected throughout', got['error'], None) + services.take_log() + # A name nobody joined with: the server looks the account up, and there is none. + mark = len(server.lines) + server.console('whitelist add Nobody') + wait_after(server, mark, r'That player does not exist') + check('whitelist add: lookup failure logged', + any("Couldn't find profile with name: nobody" in l for l in server.lines[mark:]), True) + check('whitelist add: no such account', services.take_log(), [('lookup', 'nobody')]) time.sleep(1) finally: server.stop() @@ -310,7 +355,7 @@ def main(): java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], checks=len(checks), scope='Online-mode login against vanilla\'s dedicated server and Horizon\'s, with a mock session service: ' 'server hash, RSA key packet, AES/CFB8, session join and verify, play; rejected token, forgotten ' - 'join, no account, whitelist; the servers\' log lines about the player.')) + 'join, no account, whitelist, whitelist add by account name; the servers\' log lines about the player.')) print(f'online mode: {len(checks)} checks ok') diff --git a/tools/test_options_parity.py b/tools/test_options_parity.py index 95732663d..cf7698e5c 100644 --- a/tools/test_options_parity.py +++ b/tools/test_options_parity.py @@ -1,7 +1,9 @@ #!/usr/bin/env python3 """Compare options.txt loading and saving, and the key mapping table, with the pinned client: every file is loaded through the real Options.load and written -back with Options.save, and the Rust port must produce identical text.""" +back with Options.save. The Rust probe normalizes Horizon's intentional 33..64 +render-distance extension to vanilla's default; a Rust slider/persistence +regression covers the extended range.""" from pathlib import Path import random import shutil @@ -182,7 +184,8 @@ def main(): 'fields, lenient Gson parsing and syntax-error aborts, key mappings (names, scancode-number forms, unbinding), ' 'sound volumes, model parts, line endings, the OPTIONS data fixers across version boundaries, and the ' 'KeyMapping table (order, defaults, categories, toggle mappings). Does not cover option screens or ' - 'runtime side effects of setting an option.') + 'runtime side effects of setting an option. Horizon render distances 33..64 are normalized to vanilla\'s ' + 'default by options_probe; the slider/persistence unit regression checks that intentional extension.') harness.differential( 'rust-options-parity', 'VanillaOptionsOracle', 'options_probe', make_cases, scope, sources=[lab.ROOT / 'crates/horizon-client/src/options_file.rs', lab.ROOT / 'crates/horizon-client/src/options_fixes.rs', diff --git a/tools/test_user_cache_parity.py b/tools/test_user_cache_parity.py new file mode 100644 index 000000000..621514cc8 --- /dev/null +++ b/tools/test_user_cache_parity.py @@ -0,0 +1,246 @@ +#!/usr/bin/env python3 +"""usercache.json and the console's player names, against vanilla's dedicated server. + +The console finds a player by name through the user cache (CachedUserNameToIdResolver): a name it holds +in any case, else the profile service's `getByName` (asked with the lower-case name), else, in offline +mode, the offline player of that lower-case name; what it finds is cached for a month and the file +rewritten, most recently used first. A small mock of the Minecraft services (dev only: the discovery +document and `getByName`, which knows one account) stands in for Mojang's; vanilla finds it through +`-Dminecraft.api.discovery.host`, Horizon through `HORIZON_API_DISCOVERY`. + +Each server starts in offline mode in a folder holding a usercache.json with a live entry, an expired +one, entries without a date, with a bad date, without an id, and not an object. The console then +whitelists, ops, deops and bans players: the cached one in another case, the expired one, the account the +service knows, names it doesn't know, a name too long to ask about. The server restarts in online mode, +where a name nobody knows is no player at all, and stops. Compared exactly: the console's replies, the +names asked of the service in order, and usercache.json, whitelist.json, ops.json and +banned-players.json after each run (the dates the servers wrote are placeholders). + +Players who join are added by the dedicated-server harness, which compares usercache.json too. +""" +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +import json +import os +from pathlib import Path +import platform +import re +import shutil +import subprocess +import sys +import threading +import time +import urllib.parse + +try: + from . import lab, test_rust_parity as base + from .test_dedicated_server_parity import Server, free_port +except ImportError: + import lab + import test_rust_parity as base + from test_dedicated_server_parity import Server, free_port + +NAME = 'user-cache-parity' +PROPERTIES = { + 'online-mode': 'false', + 'level-seed': '12345', + 'generate-structures': 'false', + 'view-distance': '2', + 'simulation-distance': '2', + 'spawn-protection': '0', + 'pause-when-empty-seconds': '-1', +} +NOTCH = '069a79f444e94726a5befca90e38aaf5' +SEEDED = [ + {'uuid': '853c80ef-3c37-49fd-aa49-938b674adae6', 'name': 'Seeded', 'expiresOn': '2099-01-01 00:00:00 +0000'}, + {'uuid': '61699b2e-d327-4a01-9f1e-0ea8c3f06bc6', 'name': 'Expired', 'expiresOn': '2001-02-03 04:05:06 +0000'}, + {'uuid': '00000000-0000-0000-0000-000000000001', 'name': 'NoDate'}, + {'uuid': '00000000-0000-0000-0000-000000000002', 'name': 'BadDate', 'expiresOn': 'tomorrow'}, + {'name': 'NoId', 'expiresOn': '2099-01-01 00:00:00 +0000'}, + 'not an object', + {'uuid': '00000000-0000-0000-0000-000000000003', 'name': 'Later', 'expiresOn': '2098-05-06 07:08:09 +0100'}, +] +# (console command, the reply it waits for) +OFFLINE = [ + ('whitelist add SEEDED', r'System chat: (Added|Player is already)'), + ('whitelist add Notch', r'System chat: (Added|Player is already)'), + ('op expired', r'System chat: (Made|Nothing changed)'), + ('ban Nobody_Here', r'System chat: (Banned|Nothing changed)'), + ('whitelist add NameMuchTooLongForMinecraft', r'System chat: (Added|Player is already)'), + ('deop NOTCH', r'System chat: (Made|Nothing changed)'), + ('whitelist add later', r'System chat: (Added|Player is already)'), + ('whitelist remove notch', r'System chat: (Removed|Player is not)'), +] +ONLINE = [ + ('whitelist add Somebody_Else', r'System chat: (Added|That player does not exist)'), + ('whitelist add nobody_here', r'System chat: (Added|Player is already)'), +] +FILES = ['usercache.json', 'whitelist.json', 'ops.json', 'banned-players.json'] + + +class Services: + """The mock discovery document and profile lookup by name.""" + + def __init__(self): + self.asked = [] + self.lock = threading.Lock() + services = self + + class Handler(BaseHTTPRequestHandler): + def log_message(self, *args): + pass + + def reply(self, status, body): + data = json.dumps(body).encode() + self.send_response(status) + self.send_header('Content-Type', 'application/json') + self.send_header('Content-Length', str(len(data))) + self.end_headers() + self.wfile.write(data) + + def do_GET(self): + url = urllib.parse.urlparse(self.path) + if url.path == '/minecraft/client': + return self.reply(200, services.discovery()) + if url.path.startswith('/profiles/name/'): + name = urllib.parse.unquote(url.path.removeprefix('/profiles/name/')) + with services.lock: + services.asked.append(name) + if name.lower() == 'notch': + return self.reply(200, {'id': NOTCH, 'name': 'Notch'}) + return self.reply(404, {'path': url.path, 'errorMessage': f"Couldn't find any profile with name {name}"}) + self.reply(404, {'error': 'NotFound', 'path': url.path}) + + self.server = ThreadingHTTPServer(('127.0.0.1', 0), Handler) + self.url = f'http://127.0.0.1:{self.server.server_address[1]}' + threading.Thread(target=self.server.serve_forever, daemon=True).start() + + def discovery(self): + def endpoints(**names): + return {'endpoints': {k: {'uri': self.url + v} for k, v in names.items()}} + return {'environment': 'mock', 'product': 'minecraft', 'discovery': { + 'authentication': endpoints(getPublicKeys='/publickeys'), + 'session': endpoints(join='/session/minecraft/join', verify='/session/minecraft/hasJoined', + getProfileById='/session/minecraft/profile/{profileId}'), + 'player': endpoints(), + 'profiles': endpoints(getByName='/profiles/name/{name}'), + 'telemetry': endpoints(), + }} + + def take(self): + with self.lock: + asked, self.asked = self.asked, [] + return asked + + def stop(self): + self.server.shutdown() + + +def write_properties(directory, online): + props = dict(PROPERTIES, **{'server-port': str(free_port()), 'online-mode': str(online).lower()}) + (directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items()), + encoding='iso-8859-1') + + +def files(directory): + out = [] + for name in FILES: + path = directory / name + if not path.exists(): + out.append(f'{name}: missing') + continue + text = path.read_text(encoding='utf-8') + # The dates written now: a month on for the cache, the ban's creation. + text = re.sub(r'"(expiresOn|created)": ?"20[2-3]\d-[^"]*"', r'"\1":""', text) + out.append(f'{name}: {text}') + return out + + +def wait_after(server, seen, pattern, timeout=60): + """Waits for a line matching `pattern` after the first `seen` lines.""" + deadline = time.time() + timeout + regex = re.compile(pattern) + while not any(regex.search(l) for l in server.lines[seen:]): + if time.time() > deadline or server.process.poll() is not None: + raise AssertionError(f'{server.log_path.name}: no reply matching {pattern!r}:\n' + + '\n'.join(server.lines[-20:])) + time.sleep(0.1) + + +def run(label, command, build, services, steps, online, transcript): + directory = build / label + write_properties(directory, online) + server = Server(command, directory, f'{label}-{"online" if online else "offline"}') + try: + server.wait_for(r'Done \(') + services.take() + for line, reply in steps: + seen = len(server.lines) + server.console(line) + wait_after(server, seen, reply) + replies = [l for l in server.lines[seen:] if 'System chat:' in l] + transcript.append(f'> {line}') + transcript += [re.sub(r'^\[[^]]*\] ', '', l) for l in replies] + transcript.append(f'asked: {services.take()}') + finally: + server.stop() + transcript += files(directory) + + +def run_side(label, command, build, services): + directory = build / label + if directory.exists(): + shutil.rmtree(directory) + directory.mkdir(parents=True) + (directory / 'eula.txt').write_text('eula=true\n') + (directory / 'usercache.json').write_text(json.dumps(SEEDED), encoding='utf-8') + transcript = [] + run(label, command, build, services, OFFLINE, False, transcript) + run(label, command, build, services, ONLINE, True, transcript) + return transcript + + +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) + 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') + services = Services() + discovery = services.url + '/minecraft/client' + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', f'-Dminecraft.api.discovery.host={discovery}', + '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + os.environ['HORIZON_API_DISCOVERY'] = discovery + try: + vanilla = run_side('vanilla', vanilla_server, build, services) + horizon = run_side('horizon', horizon_server, build, services) + finally: + services.stop() + (build / 'vanilla.txt').write_text('\n'.join(vanilla) + '\n', encoding='utf-8') + (build / 'horizon.txt').write_text('\n'.join(horizon) + '\n', encoding='utf-8') + if vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=1, lineterm='')) + raise AssertionError(f'transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' + diff[:6000]) + if not any('Notch' in l for l in vanilla if l.startswith('usercache.json')): + raise AssertionError(f'the cache never held the looked-up account (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='usercache.json (load, expiry, most recently used order, Gson output) and the console finding ' + 'players by name through it and the profile service, offline and online.')) + print(f'user cache: {len(vanilla)} lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}')