Merge remote-tracking branch 'origin/main' into fix/blocked-hit-animation

# Conflicts:
#	crates/horizon-client/src/mobs/anim.rs
This commit is contained in:
Cosmic
2026-10-17 13:09:02 -04:00
137 changed files with 10146 additions and 860 deletions
+9 -4
View File
@@ -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: |
+1
View File
@@ -28,5 +28,6 @@
/horizon-player.txt
/servers.dat
/screenshots/
/.horizon-cache/
/crash-reports/
/debug/
Generated
+4
View File
@@ -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",
]
@@ -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 <this machine's address> --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
@@ -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
}
]
}
@@ -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
}
]
}
@@ -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"
}
]
}
@@ -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
}
]
}
@@ -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"
}
]
}
+120 -12
View File
@@ -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<String>,
}
@@ -87,6 +95,7 @@ fn options() -> Result<Options, String> {
seconds: 60,
view_distance: 10,
seed: 12345,
explore_speed: 22.0,
name: "Bot".into(),
report: None,
};
@@ -110,6 +119,9 @@ fn options() -> Result<Options, String> {
"--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<Options, String> {
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<HashSet<(i32, i32)>>,
/// Where the server last put the bot (a teleport), to start from.
teleport: Mutex<Option<[f64; 3]>>,
error: Mutex<Option<String>>,
@@ -268,6 +291,36 @@ fn join(o: &Options, name: &str) -> Result<Connection, String> {
}
}
/// 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<u8>| 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::<u64>(),
"resent": bots.iter().map(|b| b.resent.load(Ordering::Relaxed)).sum::<u64>(),
"pulled_back": bots.iter().map(|b| b.pulled_back.load(Ordering::Relaxed)).sum::<u64>(),
"waited_ticks": bots.iter().map(|b| b.waited.load(Ordering::Relaxed)).sum::<u64>(),
"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::<Vec<_>>(),
});
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}");
}
+3 -7
View File
@@ -22,12 +22,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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<String> = sync
.dynamic
@@ -121,6 +116,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
eprintln!("[lan] {packet:?}");
}
}
HostEvent::Chat(line) => println!("{line}"),
HostEvent::Chat(line) | HostEvent::Warn(line) => println!("{line}"),
HostEvent::Left(_) => {}
}
}
@@ -232,7 +232,7 @@ fn incremental(
c.lit = true;
world.insert((x, z), Arc::new(c));
}
let palette: Vec<u16> = [
let palette: Vec<u32> = [
"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();
@@ -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;
@@ -56,16 +56,16 @@ fn main() -> horizon_client::Result<()> {
"resource_load_ms={:.3}",
start.elapsed().as_secs_f64() * 1000.0
);
let inactive: Vec<u16> = facts
let inactive: Vec<u32> = 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();
@@ -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<Arc<Chunk>>, 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 {
@@ -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)
}
@@ -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])?);
+3
View File
@@ -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,
+38 -24
View File
@@ -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<u32> = 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<i32> {
pub fn highest(&self, skip: u32, mut matches: impl FnMut(i32) -> bool) -> Option<i32> {
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));
+1 -1
View File
@@ -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<u16> {
fn state(&self, x: i32, y: i32, z: i32) -> Option<u32> {
self.columns.state(x, y, z)
}
fn light(&self, x: i32, y: i32, z: i32) -> Option<u8> {
+9 -1
View File
@@ -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);
+2 -2
View File
@@ -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<u16>,
layers: Vec<u32>,
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));
}
}
+106 -22
View File
@@ -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<u32>)>,
/// 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() {
+21 -1
View File
@@ -88,6 +88,9 @@ pub(super) struct AdvState {
pub(super) changed: BTreeSet<usize>,
pub(super) unlocked: Vec<String>,
pub(super) announced: Vec<Text>,
/// 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<std::thread::JoinHandle<Tree>>,
}
/// 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<Tree> {
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;
+4 -7
View File
@@ -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<u16, Option<&'static str>> =
let mut cache: std::collections::HashMap<u32, Option<&'static str>> =
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()
};
+1 -1
View File
@@ -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);
+65 -27
View File
@@ -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<Mutex<Option<Setup>>> = OnceLock::new();
/// usercache.json (Services.nameToIdCache): used by the server thread only.
static USER_CACHE: Mutex<Option<UserCache>> = 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<NameAndId> {
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<NameAndId> {
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) {
+7 -1
View File
@@ -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.
+1 -1
View File
@@ -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).
+20 -2
View File
@@ -57,6 +57,10 @@ pub(super) struct LanHost {
pub mob_players: HashMap<u32, u32>,
/// An integrated server's owner (game/integrated.rs).
pub owner: Option<Owner>,
/// 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<u128>,
}
/// 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));
}
}
@@ -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);
}
@@ -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<i32>)> {
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);
}
}
}
}
+6 -2
View File
@@ -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<Pos> = 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();
@@ -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)
})
})
@@ -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<BlockPos> {
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<BlockPos> {
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) {
+12 -1
View File
@@ -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.
+28 -4
View File
@@ -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),
+12 -7
View File
@@ -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 {
@@ -243,6 +243,7 @@ impl Game<'_> {
},
tick_rate: self.clock.rate(),
tick_frozen: self.clock.frozen(),
collision: Some(self.collision),
}
}
}
+59 -8
View File
@@ -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::<TerrainVertex>() 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<bool> {
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");
+277 -42
View File
@@ -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<Page>,
pages: Vec<Option<Page>>,
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<wgpu::Buffer> {
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::<u64>() * self.stride
self.pages.iter().flatten().map(|p| p.capacity).sum::<u64>() * 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<wgpu::Buffer> {
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)));
}
}
@@ -144,6 +144,13 @@ impl ServerAdvancements {
) -> Option<Vec<u8>> {
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]));
+428 -92
View File
@@ -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<i32>,
/// The save's tables, for the heightmaps.
pub tables: Option<Arc<crate::save::SaveTables>>,
/// Sections already looked at, by address (see `Encoded`).
cache: std::sync::Mutex<FxHashMap<usize, Encoded>>,
/// Each column's heightmaps, with the sections they were made from.
heights: std::sync::Mutex<HashMap<(i32, i32), Heights>>,
/// 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<usize>,
/// 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<std::sync::Weak<Section>>,
maps: Vec<(i32, Vec<i64>)>,
}
/// 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<Section>,
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<Arc<Layers>>,
}
/// 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<u8>,
block: Vec<u8>,
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<i64>)> {
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<T>(&self, section: &Arc<Section>, 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<usize, Encoded>,
section: &Arc<Section>,
) -> &'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<Section>) -> (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<i32> {
pub fn entity_type(&self, state: u32) -> Option<i32> {
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<i32> = (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(&sections),
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<Tag> {
/// 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<Section>], 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<Section>],
around: u64,
section: impl Fn(&Arc<Section>) -> Arc<Layers>,
) -> LightData {
let n = sections.len();
let mut data = LightData {
sky_mask: Vec::new(),
@@ -425,52 +645,25 @@ fn light_data(sections: &[Arc<Section>], 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,
_ => &sections[b - 1].light,
0 => Arc::new(Layers::of(&Light::Uniform(0))),
_ if b == n + 1 => Arc::new(Layers::of(&Light::Uniform(0xF0))),
_ => section(&sections[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::<str>::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::<Vec<_>>()
};
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::<str>::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<u8> {
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);
+2 -2
View File
@@ -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])];
+6 -6
View File
@@ -58,7 +58,7 @@ struct Sent {
#[derive(Default)]
pub struct Tracker {
sent: std::collections::HashMap<i32, Sent>,
sent: crate::fast_hash::FxHashMap<i32, Sent>,
}
/// 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 {
+27 -5
View File
@@ -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],
+277 -47
View File
@@ -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<S>),
/// 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<Guest>,
pub(super) sync: Arc<SyncData>,
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<i32, usize> =
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<i32> = 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<HostEvent>) {
// 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<HostEvent>,
) {
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<HostEvent>,
) {
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<u8>) -> C {
C::Unknown(id, payload)
}
+1
View File
@@ -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;
+643
View File
@@ -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<i32> {
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<i32>,
}
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<u32>,
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<i32>)>,
/// 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<i32>)>,
}
/// ServerWaypointManager for one level.
#[derive(Default)]
pub struct Waypoints {
/// The transmitters, in the order they were tracked.
waypoints: Vec<u128>,
/// The receivers, in the order they joined.
players: Vec<u128>,
/// Connections by receiver, then source.
connections: HashMap<u128, Vec<(u128, Connection)>>,
memo: HashMap<u128, Memo>,
/// The `locator_bar` game rule as last seen.
enabled: Option<bool>,
}
/// One tick's players, and the packets for their connections.
struct Tick<'a> {
players: HashMap<u128, &'a Player>,
view: HashMap<u128, ([i32; 2], i32)>,
/// 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<Connection> {
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::<i32>() > 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<Connection> {
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<u128> = 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);
}
}
+12 -12
View File
@@ -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<Light>) -> Vec<(i32, i32,
/// Resident chunks the incremental relighter reads and writes.
pub trait LightWorld {
fn state(&self, x: i32, y: i32, z: i32) -> Option<u16>;
fn state(&self, x: i32, y: i32, z: i32) -> Option<u32>;
/// Packed light (sky << 4 | block), None outside loaded chunks.
fn light(&self, x: i32, y: i32, z: i32) -> Option<u8>;
fn set_light(&mut self, x: i32, y: i32, z: i32, packed: u8);
}
impl<S: std::hash::BuildHasher> LightWorld for HashMap<(i32, i32), Arc<Chunk>, S> {
fn state(&self, x: i32, y: i32, z: i32) -> Option<u16> {
fn state(&self, x: i32, y: i32, z: i32) -> Option<u32> {
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);
+8 -13
View File
@@ -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<u16>,
blocks: Vec<u32>,
light: Vec<u8>,
}
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,
)
+6 -6
View File
@@ -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<FluidState, Infallible> {
Ok(self.info[self.id(p) as usize].fluid)
}
fn block(&mut self, p: BlockPos) -> Result<u16, Infallible> {
fn block(&mut self, p: BlockPos) -> Result<u32, Infallible> {
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<bool, Infallible> {
@@ -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);
@@ -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);
}
}
}
+7 -7
View File
@@ -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)]);
+36
View File
@@ -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<bool>,
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::*;
+18 -4
View File
@@ -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<MobEvent>) {
@@ -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<MobEvent>) {
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<MobEvent>) {
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) = (
+197 -9
View File
@@ -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<bool>,
}
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());
}
}
+20 -6
View File
@@ -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
+4
View File
@@ -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()
+110 -5
View File
@@ -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<MobEvent>) {
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<MobEvent>) {
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<MobEvent>) {
@@ -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<MobEvent>,
}
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<Option<()>, Self::Error> {
Ok(None)
}
fn velocity(&mut self, _: ()) -> Result<Vec3, Self::Error> {
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<f32, Self::Error> {
Ok(self.mob.random.next_float())
}
fn random_double(&mut self) -> Result<f64, Self::Error> {
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(())
}
}
+22 -8
View File
@@ -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<Node>,
index: HashMap<(i32, i32, i32), u32>,
index: FxHashMap<(i32, i32, i32), u32>,
heap: Vec<u32>,
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<FxHashMap<(i32, i32, i32), PathType>>,
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<u32>| 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<u32>> = HashMap::new();
let mut by_dir: FxHashMap<(i32, i32, i32), Option<u32>> = FxHashMap::default();
for (dx, dy, dz) in [
(0, -1, 0),
(0, 1, 0),
+25 -11
View File
@@ -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));
+10 -1
View File
@@ -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
+10 -1
View File
@@ -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)
+6 -4
View File
@@ -323,7 +323,7 @@ impl<'a> Plan<'a> {
}
let list: Vec<String> = 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.
+24 -4
View File
@@ -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");
+42 -8
View File
@@ -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<String>) -> Result<ModelLibrary> {
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<String>,
) -> Result<ModelLibrary> {
self.block_models(root, pending, true)
}
fn block_models(
&self,
root: &Path,
mut pending: Vec<String>,
or_missing: bool,
) -> Result<ModelLibrary> {
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<CuboidModel> {
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")?;
+30 -8
View File
@@ -136,11 +136,29 @@ impl StateModels {
})
.collect::<Result<_>>()?;
let possible: Vec<Properties> = 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<String> = 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)?;
+1 -1
View File
@@ -159,7 +159,7 @@ static TABLE: LazyLock<Vec<Def>> = 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())),
+21 -2
View File
@@ -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()
}
+2 -2
View File
@@ -54,12 +54,12 @@ impl<T: Copy> PoiSections<T> {
return cached.pois.clone();
}
let pois: SectionPois<T> = 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([])
@@ -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<BlockStateId> {
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)
};
+11 -11
View File
@@ -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<bool>,
block_entity: Vec<bool>,
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<String, u16> = HashMap::new();
let air = registry.default_state("minecraft:air")?.0;
let mut cache: HashMap<String, u32> = 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<u16> = palette
let palette: Vec<u32> = 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<u32> = 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<G: ChunkGenerator> ChunkGenerator for Persistent<G> {
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
+65 -18
View File
@@ -36,6 +36,9 @@ pub trait ChunkGenerator: Send + Sync + 'static {
fn load(&self, _x: i32, _z: i32) -> Option<Chunk> {
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() {
+9 -1
View File
@@ -31,6 +31,10 @@ struct Globals {
@group(0) @binding(5) var sprites_1: texture_2d_array<f32>;
@group(0) @binding(6) var sprites_2: texture_2d_array<f32>;
@group(0) @binding(7) var sprites_3: texture_2d_array<f32>;
@group(0) @binding(8) var sprites_4: texture_2d_array<f32>;
@group(0) @binding(9) var sprites_5: texture_2d_array<f32>;
@group(0) @binding(10) var sprites_6: texture_2d_array<f32>;
@group(0) @binding(11) var sprites_7: texture_2d_array<f32>;
// 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<f32>, layer: u32) -> vec4<f32> {
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); }
}
}
+12 -12
View File
@@ -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<Self> {
let id = |name: &str| -> crate::Result<u16> {
let id = |name: &str| -> crate::Result<u32> {
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"),
})
+15 -2
View File
@@ -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))),
+8 -9
View File
@@ -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<horizon_world::nbt::Tag>)>,
/// Chunks changed since the last save.
@@ -36,7 +36,7 @@ pub struct World {
/// blocks, which need a block entity).
pub watched: Vec<bool>,
/// 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 {
+6 -5
View File
@@ -38,7 +38,7 @@ fn resources() -> Option<std::path::PathBuf> {
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)
@@ -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();
}
}
}
@@ -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]);
}
+2 -3
View File
@@ -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
+67 -2
View File
@@ -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<String>,
/// 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<String, String>,
pub recipes_upgraded: usize,
pub loot_tables_upgraded: usize,
/// Worldgen and structure files (`data/<id>/worldgen/`, `structure/`).
@@ -291,6 +295,32 @@ fn place(name: &str, id: &str) -> Option<bool> {
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<u8>) -> Option<String> {
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");
+389
View File
@@ -0,0 +1,389 @@
//! Step 1b, the jars: for the mods the player asked to port, their own
//! download of each jar into `<work>/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. `<work>/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<Outcome>,
}
impl Fetched {
pub fn count(&self, how: How) -> usize {
self.files.iter().filter(|o| o.how == how).count()
}
/// `<work>/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<Fetched, String> {
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<String> {
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<String, PathBuf>,
by_name: HashMap<String, PathBuf>,
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::<Value>(&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<PathBuf> {
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<Record> = 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<How> = 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();
}
}
+57 -2
View File
@@ -22,8 +22,20 @@ pub struct Pack {
pub files: Vec<PackFile>,
/// Override files by top folder (`config`, `kubejs`, ...).
pub overrides: BTreeMap<String, usize>,
/// 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<Pack, String> {
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<fs::File>, name: &str) -> Result<Option<V
entry
.read_to_string(&mut text)
.map_err(|e| format!("{name}: {e}"))?;
serde_json::from_str(&text)
serde_json::from_str(text.trim_start_matches('\u{feff}'))
.map(Some)
.map_err(|e| format!("{name}: {e}"))
}
@@ -161,6 +182,7 @@ pub fn mrpack(index: &Value, names: &[String]) -> 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();
}
}
+17
View File
@@ -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;
+71 -188
View File
@@ -6,57 +6,77 @@
//! `<work>/modrinth.json`), the jars in the instance or `<work>/jars/`,
//! classes, tiers and dependency layers in `<work>/inventory.json` and
//! `inventory.csv`.
//! - `fetch <work> [--from <folder>] [<mod id>...]`: the jars of the
//! portable mods (content, data tier; or the ones named) into
//! `<work>/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 `<work>/fetch.json`.
//! - `carry <work> [--resources <prepared>] [<mod id>...]`: the content
//! mods' data and assets into `<work>/mods/<id>/carry/`, a folder mod for
//! local use. With the NeoForge oracle's `<work>/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 <work>`: the coverage report, per mod ported, partial, not yet
//! or not needed with the reason, in `<work>/coverage.json` and
//! `coverage.md`.
//! - `port <pack> <work> [--from <folder>] [--resources <prepared>]`: 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 <pack> <work> | inventory <work> [--offline] | carry <work> [--resources <prepared>] [<mod id>...] | report <work>";
const USAGE: &str = "usage: horizon-port-pack import <pack> <work> | inventory <work> [--offline] | fetch <work> [--from <folder>] [<mod id>...] | carry <work> [--resources <prepared>] [<mod id>...] | report <work> | port <pack> <work> [--from <folder>] [--resources <prepared>]";
fn main() -> ExitCode {
let args: Vec<String> = 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<String> = 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<Record> = 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<T: serde::de::DeserializeOwned>(path: &Path) -> Result<T, String> {
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<inventory::Record> = 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<String> = 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<usize> = (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<std::collections::BTreeSet<String>, 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<inventory::Record> = 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 <value>` from `args`.
fn option(args: &mut Vec<String>, name: &str) -> Result<Option<PathBuf>, 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<String>, name: &str) -> bool {
let before = args.len();
args.retain(|a| a != name);
args.len() != before
}
+3 -3
View File
@@ -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 `<work>/modrinth.json`, so a
//! SHA-1, and its project (categories, licence, sides). Only metadata (the
//! jars are `fetch`'s). Answers are cached in `<work>/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 {
+246
View File
@@ -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<T: serde::de::DeserializeOwned>(path: &Path) -> Result<T, String> {
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 `<work>/pack.json`.
pub fn import(pack: &Path, work: &Path, log: Log) -> Result<import::Pack, 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 = 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: `<work>/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<Vec<inventory::Record>, 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<String> = 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<usize> = (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<BTreeSet<String>, 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 `<work>/mods/<id>/carry/`. Returns how many were.
pub fn carry(
work: &Path,
resources: Option<&Path>,
only: &[String],
log: Log,
) -> Result<usize, String> {
let records: Vec<inventory::Record> = 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: `<work>/coverage.json` and `coverage.md`.
pub fn report(work: &Path, log: Log) -> Result<Vec<report::Row>, String> {
let pack: import::Pack = read_json(&work.join("pack.json"))?;
let records: Vec<inventory::Record> = 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<report::Row>), String> {
let records = match read_json::<Vec<inventory::Record>>(&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<String> = 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)?))
}
+1 -1
View File
@@ -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"
}
+84
View File
@@ -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<Vec<u32>> = 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<i32> = 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<i32> = 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<i32> = (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<i32> = 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.
+150
View File
@@ -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<UUID, String>`.
#[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<i32>,
pub target: WaypointTarget,
}
impl Codec<TrackedWaypoint> for TrackedWaypoint {
fn read(r: &mut Reader) -> Result<TrackedWaypoint> {
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![
+3 -1
View File
@@ -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
+37
View File
@@ -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 {
+6 -3
View File
@@ -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;
+100
View File
@@ -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<NameAndId> {
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<NameAndId> {
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<String> {
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<NameAndId> {
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);
}
}
@@ -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<Info>,
by_name: HashMap<String, usize>,
by_uuid: HashMap<u128, usize>,
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<PathBuf>) -> (Self, Vec<String>) {
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<String>) -> Vec<Info> {
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<NameAndId>,
) -> Option<NameAndId> {
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<NameAndId>,
) -> Option<NameAndId> {
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<NameAndId> {
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<String>) {
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<NameAndId> { 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"}"#));
}
}
@@ -39,7 +39,7 @@ impl NameAndId {
name: name.to_owned(),
}
}
fn from_json(o: &Json) -> Option<Self> {
pub(crate) fn from_json(o: &Json) -> Option<Self> {
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()));
}
+1 -1
View File
@@ -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

Some files were not shown because too many files have changed in this diff Show More