diff --git a/.dockerignore b/.dockerignore
index 417a1867c..458330c93 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -2,7 +2,6 @@
target/
.local/
**/node_modules/
-website/
benchmarks/
packs/
mods/
diff --git a/.gitattributes b/.gitattributes
index 358aeae7a..f9e1922d7 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -2,5 +2,5 @@
# where Git for Windows' core.autocrlf would check them out with CRLF.
docs/launcher-commands.md text eol=lf
docs/horizon-ctl-commands.md text eol=lf
-website/src/tokens.css text eol=lf
+crates/horizon-theme/tokens.css text eol=lf
docs/api/*-reference.md text eol=lf
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index eff82fa99..39ea920da 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -73,6 +73,42 @@ jobs:
run: |
cargo +stable test --workspace --lib --bins --tests --features horizon-client/mods,horizon-mods/pack
cargo +stable test --workspace --doc --features horizon-client/mods,horizon-mods/pack
+ # The launcher is its own workspace (launcher/Cargo.toml), so verify's cargo runs never build it. A pull request that
+ # changes it or a crate it builds is checked here, and so is every push to main: two pull requests can each pass
+ # alone and break it together (#2059 and the registry ratings did, 2026-10-26).
+ launcher:
+ runs-on: ubuntu-latest
+ timeout-minutes: 30
+ steps:
+ - uses: actions/checkout@v4
+ with:
+ fetch-depth: 0
+ - name: Does this pull request change the launcher or a crate it builds?
+ if: github.event_name == 'pull_request'
+ id: launcher
+ env:
+ BASE_SHA: ${{ github.event.pull_request.base.sha }}
+ HEAD_SHA: ${{ github.event.pull_request.head.sha }}
+ run: python3 tools/ci/rust_changes.py --launcher "$BASE_SHA" "$HEAD_SHA" >> "$GITHUB_OUTPUT"
+ - name: Install Rust and the headers GPUI links against
+ if: steps.launcher.outputs.required != 'false'
+ run: |
+ sudo apt-get update && sudo apt-get install -y --no-install-recommends libxkbcommon-x11-dev libwayland-dev \
+ libx11-xcb-dev libfontconfig-dev libvulkan-dev libasound2-dev libudev-dev
+ rustup toolchain install stable --profile minimal --component rustfmt --component clippy
+ - uses: Swatinem/rust-cache@v2
+ if: steps.launcher.outputs.required != 'false'
+ with:
+ workspaces: launcher
+ shared-key: launcher
+ save-if: ${{ github.ref == 'refs/heads/main' }}
+ - name: Check the launcher's formatting, lint and tests
+ if: steps.launcher.outputs.required != 'false'
+ working-directory: launcher
+ run: |
+ cargo +stable fmt --check
+ cargo +stable clippy --all-targets --locked -- -D warnings
+ cargo +stable test --locked --lib
# Most pull requests leave the harnesses to parity-main, which runs them on main a batch of merges at a time
# (.github/workflows/parity-main.yml; releases wait for it). A pull request still runs them itself when it changes
# a harness, an oracle, a probe or the runner, or carries the run-parity label (run-parity.yml, when it is added).
diff --git a/.github/workflows/parity-main.yml b/.github/workflows/parity-main.yml
index c47005ab0..70fb33796 100644
--- a/.github/workflows/parity-main.yml
+++ b/.github/workflows/parity-main.yml
@@ -48,6 +48,9 @@ jobs:
GH_TOKEN: ${{ github.token }}
run: echo "sha=$(python3 tools/parity_main.py base)" >> "$GITHUB_OUTPUT"
- name: The harnesses the merges since then can affect
+ id: harnesses
+ # A failure here is judged by the next step: the run passes when every failed harness passes again at HEAD.
+ continue-on-error: true
env:
PARITY_HOSTS: ''
BASE_SHA: ${{ steps.base.outputs.sha }}
@@ -55,8 +58,8 @@ jobs:
echo "Merges since ${BASE_SHA}:"
git log --first-parent --format=' %h %s' "$BASE_SHA..HEAD"
python3 tools/parity.py --changed "$BASE_SHA" --timeout 2400
- - name: The merge that broke them
- if: failure()
+ - name: A flake, or the merge that broke them
+ if: steps.harnesses.outcome == 'failure'
env:
GH_TOKEN: ${{ github.token }}
PARITY_HOSTS: ''
@@ -65,7 +68,7 @@ jobs:
rm -rf .local/parity-main-logs && cp -R .local/parity-runner/logs .local/parity-main-logs 2>/dev/null || true
python3 tools/parity_main.py bisect "$BASE_SHA" --comment
- name: Logs of the failed harnesses
- if: failure()
+ if: steps.harnesses.outcome == 'failure'
uses: actions/upload-artifact@v4
with:
name: parity-main-logs
diff --git a/AGENTS.md b/AGENTS.md
index 24dc38b8e..4d2a258ec 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -56,7 +56,8 @@ Game workspace (root `Cargo.toml`); `launcher/` and `crates/horizon-telemetry-se
Other top-level folders: `tools/` (harnesses and dev scripts), `tests/` (Python unit tests: `python3 -m
unittest discover -s tests`), `mods/horizon/src/test/java/dev/horizonlab/parity/` (Java oracles),
-`website/` (playhorizon.dev; its paths are relative to `website/`), `docs/`, `.github/workflows/`.
+`docs/`, `.github/workflows/`. The website (playhorizon.dev) is the private ludovic111/horizon-site: its build
+reads `README.md`, `launcher/links.json`, `docs/api/`, `roadmap/roadmap.json` and `crates/horizon-theme/tokens.css` from main.
## Build and test fast
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 21951c083..03f3bd97b 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -4,8 +4,8 @@ Thanks for helping. Horizon is Minecraft: Java Edition 26.3 rewritten in Rust, a
everything is that **it plays exactly like vanilla**. This page is the short version of how we work; questions are
welcome on [Discord](https://discord.gg/HcTeMZxe5E).
-This repository holds the game, the launcher and the website, plus the tools, tests and docs that build, check and
-release them. Anything else (videos, Discord and other community material) lives elsewhere.
+This repository holds the game and the launcher, plus the tools, tests and docs that build, check and release them.
+The website (playhorizon.dev) is built in a separate repository from this one's README, `docs/api/` and roadmap. Anything else (videos, Discord and other community material) lives elsewhere.
**Where to start: [playhorizon.dev/hub](https://playhorizon.dev/hub).** It shows what is being done now and next
([ROADMAP.md](ROADMAP.md) in the repository), the work board, good first tasks, and the forms to report a bug or
diff --git a/Cargo.lock b/Cargo.lock
index 1d3b52a3b..c5568074f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1370,6 +1370,7 @@ dependencies = [
"horizon-core",
"horizon-dualsense",
"horizon-friends",
+ "horizon-hdr",
"horizon-mods",
"horizon-protocol",
"horizon-server-files",
@@ -1445,6 +1446,14 @@ dependencies = [
"spake2",
]
+[[package]]
+name = "horizon-hdr"
+version = "0.1.0"
+dependencies = [
+ "ash",
+ "wgpu",
+]
+
[[package]]
name = "horizon-mod-sdk"
version = "0.1.0"
diff --git a/Cargo.toml b/Cargo.toml
index 181ab1832..b35917847 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[workspace]
exclude = ["launcher", "crates/horizon-telemetry-server", "crates/horizon-mod-registry", "third_party/wgpu-hal"]
-members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/horizon-client", "crates/horizon-client-core", "crates/horizon-command", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/horizon-mods/tests/ui_guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-shaders", "crates/horizon-shader-api", "crates/horizon-release-sign", "crates/horizon-mod-sdk/examples/coords-hud", "crates/horizon-mod-sdk/examples/predator-missile", "crates/horizon-mod-sdk/examples/welcome", "mods/official/fps-overlay", "mods/official/keystrokes", "mods/official/minimap", "mods/official/zoom", "xtask"]
+members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-hdr", "crates/horizon-world", "crates/horizon-client", "crates/horizon-client-core", "crates/horizon-command", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/horizon-mods/tests/ui_guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-shaders", "crates/horizon-shader-api", "crates/horizon-release-sign", "crates/horizon-mod-sdk/examples/coords-hud", "crates/horizon-mod-sdk/examples/predator-missile", "crates/horizon-mod-sdk/examples/welcome", "mods/official/fps-overlay", "mods/official/keystrokes", "mods/official/minimap", "mods/official/zoom", "xtask"]
resolver = "2"
[workspace.package]
diff --git a/ROADMAP.md b/ROADMAP.md
index a04eca2ce..9b82f4fac 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -134,7 +134,7 @@ Better tools for mod makers: a test runner that needs no game window, templates
`cargo horizon-mod test` runs a mod's test scripts against a fake game snapshot ([#1816](https://github.com/ludovic111/horizon/issues/1816)) and, for server mods, scripted server events ([#1837](https://github.com/ludovic111/horizon/issues/1837)); the content template and the guides are merged ([#1859](https://github.com/ludovic111/horizon/issues/1859), [#1810](https://github.com/ludovic111/horizon/issues/1810), [docs/api/stability.md](docs/api/stability.md)). Left: templates for hud, server and machine mods, typed wrappers in the SDK, a frozen 0.1 compatibility mod that every runtime must load, a CI check that fails when a stable reference entry is removed without a new major version, and the SDK on crates.io once the API is stable.
-More: [website/docs/test-a-mod.md](website/docs/test-a-mod.md)
+More: [https://playhorizon.dev/docs/test-a-mod](https://playhorizon.dev/docs/test-a-mod)
@@ -160,6 +160,66 @@ Translatable components resolved through the selected language for items, contai
+#### Cooldowns drawn on items
+
+After you throw an ender pearl, its slot should show the cooldown sweeping away, as in vanilla.
+
+Technical
+
+Vanilla draws `ItemCooldowns`' percent as a white overlay over the slot (`GuiGraphicsExtractor.itemDecorations` and `itemCooldown`) in the hotbar and in inventories. Issue [#1763](https://github.com/ludovic111/horizon/issues/1763).
+
+More: [#1933](https://github.com/ludovic111/horizon/issues/1933)
+
+
+
+#### Chat settings and team prefixes
+
+The Hidden and Commands Only chat settings should hide chat, and team prefixes should appear before names in chat.
+
+Technical
+
+Vanilla checks `ChatVisiblity` before it delivers chat and system messages ([#1376](https://github.com/ludovic111/horizon/issues/1376)), and chat uses the team-formatted display name ([#1378](https://github.com/ludovic111/horizon/issues/1378)). Both issues have exact steps to reproduce.
+
+More: [#1932](https://github.com/ludovic111/horizon/issues/1932) · [#1841](https://github.com/ludovic111/horizon/issues/1841)
+
+
+
+#### A pointing hand over buttons
+
+The mouse pointer should turn into a pointing hand over menu buttons, as in vanilla 26.3.
+
+Technical
+
+26.3's `AbstractWidget` asks for a cursor from `CursorTypes` when it is hovered; winit's `CursorIcon::Pointer` gives the same shape. Issue [#1347](https://github.com/ludovic111/horizon/issues/1347).
+
+More: [#1897](https://github.com/ludovic111/horizon/issues/1897)
+
+
+
+#### Ctrl+Backspace and scroll bars in menus
+
+Ctrl+Backspace should delete a whole word in the creative search, and every list's scroll bar should be draggable.
+
+Technical
+
+`EditBox` deletes by word with Ctrl ([#1764](https://github.com/ludovic111/horizon/issues/1764)); the Game Rules and Statistics lists' scroll bars can't be dragged ([#1782](https://github.com/ludovic111/horizon/issues/1782), after [#978](https://github.com/ludovic111/horizon/issues/978)).
+
+More: [#1884](https://github.com/ludovic111/horizon/issues/1884) · [#1931](https://github.com/ludovic111/horizon/issues/1931)
+
+
+
+#### Chickens flap while they fall
+
+Chickens should flap their wings while they fall, not stand still in the air.
+
+Technical
+
+Vanilla's `Chicken.aiStep` updates `flap` and `flapSpeed` when the chicken isn't on the ground, and the model's wings follow them. Issue [#1706](https://github.com/ludovic111/horizon/issues/1706).
+
+More: [#1892](https://github.com/ludovic111/horizon/issues/1892)
+
+
+
### Rendering
#### Shader packs, written again from scratch
@@ -250,7 +310,7 @@ Horizon should run well on small machines: integrated graphics, the Steam Deck,
Partly done: an opt-in 60 FPS cap with fewer workers on battery, Far Detail Reduced for integrated GPUs and animated textures on the GPU (both shipped). Left: measured targets and fixes per class of machine (memory use, worker counts, startup) on a Steam Deck, old Intel and Apple Silicon Macs and 8 GB computers, reported on the Benchmarks page.
-More: [docs/benchmark.md](docs/benchmark.md) · [website/docs/performance.md](website/docs/performance.md)
+More: [docs/benchmark.md](docs/benchmark.md) · [https://playhorizon.dev/docs/performance](https://playhorizon.dev/docs/performance)
@@ -540,7 +600,9 @@ You can port a mod to Horizon with your own AI agent on your own computer. Horiz
Technical
-A player runs their own agent locally with `horizon-ctl` and the MCP servers ([docs/agent-control.md](docs/agent-control.md)), `cargo horizon-mod`, `horizon-port-pack` and the parity harnesses as the check. Horizon writes the guide ([website/docs/agents.md](website/docs/agents.md), [docs/modpacks.md](docs/modpacks.md)) and runs no agents, accounts or keys for players. The No AI switch turns all of it off.
+A player runs their own agent locally with `horizon-ctl` and the MCP servers ([docs/agent-control.md](docs/agent-control.md)), `cargo horizon-mod`, `horizon-port-pack` and the parity harnesses as the check. Horizon writes the guide (playhorizon.dev/docs/agents, [docs/modpacks.md](docs/modpacks.md)) and runs no agents, accounts or keys for players. The No AI switch turns all of it off.
+
+More: [https://playhorizon.dev/docs/agents](https://playhorizon.dev/docs/agents)
@@ -876,7 +938,9 @@ Run Horizon's built-in benchmark and share the report: every new computer makes
Technical
-`horizon-launcher play --arg=--benchmark --arg=standard` writes a JSON report; `horizon-launcher data benchmark ` shows what would be sent and uploads it ([website/docs/performance.md](website/docs/performance.md)). Results from three installs with the same hardware appear on the Benchmarks page. Example: [#1711](https://github.com/ludovic111/horizon/issues/1711).
+`horizon-launcher play --arg=--benchmark --arg=standard` writes a JSON report; `horizon-launcher data benchmark ` shows what would be sent and uploads it (playhorizon.dev/docs/performance). Results from three installs with the same hardware appear on the Benchmarks page. Example: [#1711](https://github.com/ludovic111/horizon/issues/1711).
+
+More: [https://playhorizon.dev/docs/performance](https://playhorizon.dev/docs/performance)
@@ -902,56 +966,6 @@ Armour and tools in End city chests should come enchanted, as in vanilla.
-#### Cooldowns drawn on items
-
-After you throw an ender pearl, its slot should show the cooldown sweeping away, as in vanilla.
-
-Technical
-
-Vanilla draws `ItemCooldowns`' percent as a white overlay over the slot (`GuiGraphicsExtractor.itemDecorations` and `itemCooldown`) in the hotbar and in inventories. Issue [#1763](https://github.com/ludovic111/horizon/issues/1763).
-
-
-
-#### Chat settings and team prefixes
-
-The Hidden and Commands Only chat settings should hide chat, and team prefixes should appear before names in chat.
-
-Technical
-
-Vanilla checks `ChatVisiblity` before it delivers chat and system messages ([#1376](https://github.com/ludovic111/horizon/issues/1376)), and chat uses the team-formatted display name ([#1378](https://github.com/ludovic111/horizon/issues/1378)). Both issues have exact steps to reproduce.
-
-
-
-#### A pointing hand over buttons
-
-The mouse pointer should turn into a pointing hand over menu buttons, as in vanilla 26.3.
-
-Technical
-
-26.3's `AbstractWidget` asks for a cursor from `CursorTypes` when it is hovered; winit's `CursorIcon::Pointer` gives the same shape. Issue [#1347](https://github.com/ludovic111/horizon/issues/1347).
-
-
-
-#### Ctrl+Backspace and scroll bars in menus
-
-Ctrl+Backspace should delete a whole word in the creative search, and every list's scroll bar should be draggable.
-
-Technical
-
-`EditBox` deletes by word with Ctrl ([#1764](https://github.com/ludovic111/horizon/issues/1764)); the Game Rules and Statistics lists' scroll bars can't be dragged ([#1782](https://github.com/ludovic111/horizon/issues/1782), after [#978](https://github.com/ludovic111/horizon/issues/978)).
-
-
-
-#### Chickens flap while they fall
-
-Chickens should flap their wings while they fall, not stand still in the air.
-
-Technical
-
-Vanilla's `Chicken.aiStep` updates `flap` and `flapSpeed` when the chicken isn't on the ground, and the model's wings follow them. Issue [#1706](https://github.com/ludovic111/horizon/issues/1706).
-
-
-
## Recently shipped
- **OpenGL for old computers** (v0.2.63): Horizon now starts on old PCs whose graphics card has no Vulkan or DirectX 12 driver: it switches to OpenGL by itself, and Options > Graphics can pick OpenGL by hand. [#1917](https://github.com/ludovic111/horizon/issues/1917) · [#1920](https://github.com/ludovic111/horizon/issues/1920) · [#1936](https://github.com/ludovic111/horizon/issues/1936)
diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md
index 63a1f7da3..e83ccd840 100644
--- a/THIRD_PARTY_NOTICES.md
+++ b/THIRD_PARTY_NOTICES.md
@@ -17,4 +17,5 @@ by The Pixelify Sans Project Authors, licensed under the SIL Open Font License 1
`crates/horizon-theme/icons/pixel/*.svg` are icons from
[Pixelarticons](https://github.com/halfmage/pixelarticons) by Gerrit Halfmann, under the MIT
-license (`crates/horizon-theme/icons/pixel/LICENSE-pixelarticons.txt`).
+license (`crates/horizon-theme/icons/pixel/LICENSE-pixelarticons.txt`). `star.svg` is Horizon's own, drawn on
+the same grid.
diff --git a/crates/cargo-horizon-mod/src/check.rs b/crates/cargo-horizon-mod/src/check.rs
index f42674da8..33095a115 100644
--- a/crates/cargo-horizon-mod/src/check.rs
+++ b/crates/cargo-horizon-mod/src/check.rs
@@ -262,6 +262,9 @@ fn play(path: &Path, report: &mut Report) -> Result<(), String> {
format!("{}: {text}", if error { "error" } else { "feedback" })
}
Output::Command { line } => format!("command: /{line}"),
+ Output::QuietCommands { lines, .. } => {
+ format!("{} quiet command(s)", lines.len())
+ }
Output::Stopped { reason, .. } => return Err(format!("it stopped: {reason}")),
Output::OpenScreen { .. } => "opened a screen".to_string(),
Output::CloseScreen { .. } => "closed its screen".to_string(),
diff --git a/crates/horizon-assets/src/item_model.rs b/crates/horizon-assets/src/item_model.rs
index 5999805eb..d567bc6ac 100644
--- a/crates/horizon-assets/src/item_model.rs
+++ b/crates/horizon-assets/src/item_model.rs
@@ -1031,19 +1031,22 @@ pub struct ItemModels {
}
impl ItemModels {
- pub fn components(&self, stack: &StackView) -> Components {
- let mut out = self
- .prototypes
- .get(&stack.item)
- .cloned()
- .unwrap_or_default();
+ /// The stack's components: its item's defaults with the stack's patch on top. A stack
+ /// with no patch (most stacks a screen draws) borrows the defaults instead of copying them.
+ pub fn components(&self, stack: &StackView) -> std::borrow::Cow<'_, Components> {
+ static NONE: Components = BTreeMap::new();
+ let prototype = self.prototypes.get(&stack.item).unwrap_or(&NONE);
+ if stack.patch.is_empty() {
+ return std::borrow::Cow::Borrowed(prototype);
+ }
+ let mut out = prototype.clone();
for (key, value) in &stack.patch {
match value {
Some(v) => out.insert(key.clone(), v.clone()),
None => out.remove(key),
};
}
- out
+ std::borrow::Cow::Owned(out)
}
/// `ItemModelResolver.updateForTopItem`.
pub fn resolve(&self, stack: &StackView, ctx: &Context, env: &dyn ItemEnv) -> ResolvedItem {
diff --git a/crates/horizon-client-core/src/block_entity_nbt.rs b/crates/horizon-client-core/src/block_entity_nbt.rs
index 93f290515..6e214fd61 100644
--- a/crates/horizon-client-core/src/block_entity_nbt.rs
+++ b/crates/horizon-client-core/src/block_entity_nbt.rs
@@ -664,7 +664,9 @@ pub fn canonical(tag: &Tag) -> Option {
}
"minecraft:trial_spawner" => {
// `TrialSpawnerStateData.Packed` and `FullConfig`: every key is optional
- // and defaulted by vanilla; they are kept as loaded.
+ // and defaulted by vanilla; they are kept as loaded. No `spawn_data` is
+ // added: an absent one loads as Optional.empty and is not stored, and
+ // an empty entity would keep the spawner from ever picking a mob.
if let Tag::Compound(c) = tag {
for (k, v) in c {
let key = k.to_string_lossy();
@@ -676,12 +678,6 @@ pub fn canonical(tag: &Tag) -> Option {
}
}
}
- if !f.out.contains_key(&"spawn_data".into()) {
- f.put(
- "spawn_data",
- Tag::compound([("entity", Tag::Compound(Compound::new()))]),
- );
- }
}
"minecraft:vault" => {
// `VaultConfig.DEFAULT` (when the config is absent): the trial key and the
@@ -827,6 +823,21 @@ mod tests {
canonical(&snbt(s)).expect("known type")
}
+ #[test]
+ fn a_trial_spawner_without_spawn_data_saves_none() {
+ let fresh = canon(
+ r#"{id:"minecraft:trial_spawner",x:1,y:2,z:3,normal_config:"minecraft:trial_chamber/ranged/skeleton/normal"}"#,
+ );
+ assert_eq!(fresh.get("spawn_data"), None);
+ let picked = canon(
+ r#"{id:"minecraft:trial_spawner",x:1,y:2,z:3,spawn_data:{entity:{id:"minecraft:skeleton"}}}"#,
+ );
+ assert_eq!(
+ picked.get("spawn_data"),
+ Some(&snbt(r#"{entity:{id:"minecraft:skeleton"}}"#))
+ );
+ }
+
#[test]
fn every_type_has_defaults() {
for id in TYPES.iter().chain(NEWEST.iter()) {
diff --git a/crates/horizon-client/Cargo.toml b/crates/horizon-client/Cargo.toml
index 923a48a34..5611edbc9 100644
--- a/crates/horizon-client/Cargo.toml
+++ b/crates/horizon-client/Cargo.toml
@@ -14,6 +14,7 @@ horizon-command = { path = "../horizon-command" }
horizon-core = { path = "../horizon-core" }
# The server thread ahead of the workers (MinecraftServer.spin's Thread.setPriority).
horizon-threads = { path = "../horizon-threads" }
+horizon-hdr = { path = "../horizon-hdr" }
horizon-world = { path = "../horizon-world" }
horizon-protocol = { path = "../horizon-protocol" }
# server.properties, eula.txt and the user lists, shared with horizon-server.
diff --git a/crates/horizon-client/examples/probe/creative_mining_probe.rs b/crates/horizon-client/examples/probe/creative_mining_probe.rs
new file mode 100644
index 000000000..897e4bf81
--- /dev/null
+++ b/crates/horizon-client/examples/probe/creative_mining_probe.rs
@@ -0,0 +1,48 @@
+use horizon_client::{
+ Result,
+ item_data::StackData,
+ items::{ItemStack, Items},
+};
+use horizon_world::nbt::Tag;
+use serde_json::Value;
+use std::{
+ fs::File,
+ io::{BufRead, BufReader, BufWriter, Write},
+ path::Path,
+};
+
+pub fn main() -> Result<()> {
+ let args: Vec<_> = crate::args().collect();
+ let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3");
+ let items = Items::catalog(&root)?;
+ let mut out = BufWriter::new(File::create(&args[2])?);
+ for line in BufReader::new(File::open(&args[1])?).lines() {
+ let row: Value = serde_json::from_str(&line?)?;
+ let mut stack = ItemStack::new(
+ row["item"].as_str().unwrap(),
+ row["count"].as_u64().unwrap() as u32,
+ );
+ match row["tool"].as_str().unwrap() {
+ "default" => {}
+ mode => {
+ let mut data = StackData::default();
+ let tool = if mode == "implicit" {
+ Tag::compound([] as [(&str, Tag); 0])
+ } else {
+ Tag::compound([(
+ "can_destroy_blocks_in_creative",
+ Tag::Byte(i8::from(mode == "allow")),
+ )])
+ };
+ data.set_other("minecraft:tool", Some(tool));
+ stack.extra = Some(Box::new(data));
+ }
+ }
+ writeln!(
+ out,
+ "{}",
+ items.can_destroy_block(Some(&stack), row["creative"].as_bool().unwrap())
+ )?;
+ }
+ Ok(())
+}
diff --git a/crates/horizon-client/examples/probe/cushion_placement_probe.rs b/crates/horizon-client/examples/probe/cushion_placement_probe.rs
new file mode 100644
index 000000000..45fbdbd72
--- /dev/null
+++ b/crates/horizon-client/examples/probe/cushion_placement_probe.rs
@@ -0,0 +1,89 @@
+use horizon_client::{
+ Result,
+ cushion::{self, World},
+};
+use horizon_core::geometry::{Aabb, ShapeOp, Vec3, VoxelShape};
+use serde_json::Value;
+use std::{
+ collections::HashMap,
+ fs::File,
+ io::{BufRead, BufReader, BufWriter, Write},
+};
+
+struct Cell {
+ outline: VoxelShape,
+ collision: VoxelShape,
+ suffocating: bool,
+}
+struct Fixture(HashMap<[i32; 3], Cell>);
+impl World for Fixture {
+ fn outline_bounds(&self, p: [i32; 3]) -> Option {
+ self.0
+ .get(&p)?
+ .outline
+ .bounds()
+ .map(|b| b.move_by(Vec3::new(p[0] as f64, p[1] as f64, p[2] as f64)))
+ }
+ fn collision_shapes(&self, _: Aabb) -> Vec {
+ self.0
+ .iter()
+ .map(|(p, c)| {
+ c.collision
+ .moved(Vec3::new(p[0] as f64, p[1] as f64, p[2] as f64))
+ })
+ .collect()
+ }
+ fn suffocating(&self, p: [i32; 3]) -> bool {
+ self.0.get(&p).is_some_and(|c| c.suffocating)
+ }
+}
+fn shape(value: &Value) -> Result {
+ let mut shape = VoxelShape::empty();
+ for b in value.as_array().unwrap() {
+ let f = |i: usize| b[i].as_f64().unwrap();
+ shape = shape.join(
+ &VoxelShape::from_box(Aabb::new(
+ Vec3::new(f(0), f(1), f(2)),
+ Vec3::new(f(3), f(4), f(5)),
+ ))?,
+ ShapeOp::OR,
+ )?;
+ }
+ Ok(shape)
+}
+pub fn main() -> Result<()> {
+ let args: Vec<_> = crate::args().collect();
+ let mut out = BufWriter::new(File::create(&args[2])?);
+ for line in BufReader::new(File::open(&args[1])?).lines() {
+ let row: Value = serde_json::from_str(&line?)?;
+ let mut world = Fixture(HashMap::new());
+ for c in row["cells"].as_array().unwrap() {
+ let p = std::array::from_fn(|i| c[i].as_i64().unwrap() as i32);
+ world.0.insert(
+ p,
+ Cell {
+ outline: shape(&c[3])?,
+ collision: shape(&c[4])?,
+ suffocating: c[5].as_bool().unwrap(),
+ },
+ );
+ }
+ let p = &row["pos"];
+ let (x, y, z) = (
+ p[0].as_f64().unwrap(),
+ p[1].as_f64().unwrap(),
+ p[2].as_f64().unwrap(),
+ );
+ let bounds = Aabb::new(
+ Vec3::new(x - 0.5, y, z - 0.5),
+ Vec3::new(x + 0.5, y + 0.25, z + 0.5),
+ );
+ writeln!(
+ out,
+ "{} {}",
+ cushion::can_place(&world, bounds),
+ cushion::survives(&world, bounds)
+ )?;
+ }
+ Ok(())
+}
diff --git a/crates/horizon-client/examples/probe/entity_anim_fixture.rs b/crates/horizon-client/examples/probe/entity_anim_fixture.rs
index 0a2e2951f..4aceb9f6f 100644
--- a/crates/horizon-client/examples/probe/entity_anim_fixture.rs
+++ b/crates/horizon-client/examples/probe/entity_anim_fixture.rs
@@ -9,7 +9,7 @@ use horizon_client::mesher::Vertex;
use horizon_client::mobs::{
Mob, MobData, MobTypes, Part, Species, allay,
anim::{self, Poses, State},
- aquatic, body_anim as b_anim, equine, guardian,
+ aquatic, bat, body_anim as b_anim, equine, guardian,
keyframes::AnimationState,
render, shulker, sniffer, species, tame,
};
@@ -108,6 +108,22 @@ fn entity_state(mob: &mut Mob, species: Species, case: &Value) {
let flag = |k: &str| case[k].as_bool().unwrap_or(false);
let num = |k: &str| case[k].as_f64().unwrap_or(0.0) as f32;
match species {
+ Species::Bat => {
+ mob.extra.flag = flag("isResting");
+ mob.anim.states[0].started = case["flyAnimationState"].as_f64().map(|v| v as f32);
+ mob.anim.states[1].started = case["restAnimationState"].as_f64().map(|v| v as f32);
+ if let Some(ticks) = case["@batTicks"].as_array() {
+ for (i, flags) in ticks.iter().enumerate() {
+ mob.age = i as u32 + 1;
+ bat::set_resting(mob, flags.as_i64().unwrap_or(0) & 1 != 0);
+ if flag("@batInFrame") {
+ bat::tick(mob);
+ } else {
+ bat::setup_animation_states(mob);
+ }
+ }
+ }
+ }
Species::Sheep => mob.extra.anim_ticks = num("eatTick") as i32,
Species::Guardian | Species::ElderGuardian => {
mob.anim.v[guardian::TAIL] = num("tailAnimation");
@@ -377,7 +393,7 @@ pub fn main() -> horizon_client::Result<()> {
mob.extra.age = -24000;
}
mob.age = f("ageInTicks") as u32;
- let state = State {
+ let mut state = State {
pos: f("walkAnimationPos"),
speed: f("walkAnimationSpeed"),
age: f("ageInTicks"),
@@ -389,6 +405,20 @@ pub fn main() -> horizon_client::Result<()> {
};
let mut poses = Poses::new(&model);
entity_state(&mut mob, def.species, case);
+ if let Some(ticks) = case["@walkTicks"].as_array() {
+ for speed in ticks {
+ mob.previous_limb_speed = mob.limb_speed;
+ mob.limb_speed += (speed.as_f64().unwrap() as f32 - mob.limb_speed) * 0.4;
+ mob.limb_position += mob.limb_speed;
+ }
+ if !case["@alive"].as_bool().unwrap() {
+ mob.health = 0.0;
+ }
+ let extracted = anim::state(&mob, f("@partial"));
+ state.pos = extracted.pos;
+ state.speed = extracted.speed;
+ }
+
let flag = |k: &str| case[k].as_bool().unwrap_or(false);
let num = |k: &str| case[k].as_f64().unwrap_or(0.0) as f32;
let started = |k: &str| AnimationState {
@@ -456,14 +486,6 @@ pub fn main() -> horizon_client::Result<()> {
roll_amount: num("rollAmount"),
},
),
- Species::Bat => b_anim::bat(
- p,
- s,
- flag("isResting"),
- &started("flyAnimationState"),
- &started("restAnimationState"),
- &animations,
- ),
Species::Armadillo => b_anim::armadillo(
p,
s,
@@ -590,7 +612,11 @@ pub fn main() -> horizon_client::Result<()> {
}
_ => anim::setup(&mob, s, p, &animations),
}
- out.push_str(&format!("{}\n", emit(&model, &mut poses, case, layer)));
+ let mut result = emit(&model, &mut poses, case, layer);
+ if case["@walkTicks"].is_array() {
+ result["walk"] = json!([state.pos, state.speed]);
+ }
+ out.push_str(&format!("{result}\n"));
}
fs::write(&args[2], out)?;
Ok(())
diff --git a/crates/horizon-client/examples/probe/main.rs b/crates/horizon-client/examples/probe/main.rs
index e80088fd0..6b4c3d7dd 100644
--- a/crates/horizon-client/examples/probe/main.rs
+++ b/crates/horizon-client/examples/probe/main.rs
@@ -27,7 +27,9 @@ mod chat_screen_fixture;
mod cloud_fixture;
mod command_parse_fixture;
mod container_screen_probe;
+mod creative_mining_probe;
mod creative_search_fixture;
+mod cushion_placement_probe;
mod emitter_particles_probe;
mod enchanting_fixture;
mod entity_anim_fixture;
@@ -83,6 +85,7 @@ mod sound_engine_fixture;
mod sound_event_fixture;
mod spawn_rules_fixture;
mod sprint_probe;
+mod squid_client_probe;
mod subtitle_fixture;
mod tab_list_fixture;
mod team_rules_fixture;
@@ -107,6 +110,9 @@ probes! {
block_transform_fixture,
breaking_fixture,
camera_probe,
+ squid_client_probe,
+ creative_mining_probe,
+ cushion_placement_probe,
cauldron_fixture,
chat_screen_fixture,
cloud_fixture,
diff --git a/crates/horizon-client/examples/probe/mob_ai_probe.rs b/crates/horizon-client/examples/probe/mob_ai_probe.rs
index 49f56c250..ecdf02901 100644
--- a/crates/horizon-client/examples/probe/mob_ai_probe.rs
+++ b/crates/horizon-client/examples/probe/mob_ai_probe.rs
@@ -49,6 +49,7 @@ struct Scenario {
name: String,
ticks: i32,
time: i64,
+ difficulty: u8,
rain: bool,
thunder: bool,
origin: [i32; 2],
@@ -85,11 +86,13 @@ fn parse(text: &str) -> Vec {
"scenario" => {
s.name = f[1].to_owned();
s.time = 6000;
+ s.difficulty = 2;
for kv in &f[2..] {
let (k, v) = kv.split_once('=').unwrap_or((kv, ""));
match k {
"ticks" => s.ticks = v.parse().unwrap(),
"time" => s.time = v.parse().unwrap(),
+ "difficulty" => s.difficulty = v.parse().unwrap(),
"rain" => s.rain = true,
"thunder" => s.thunder = true,
"levelseed" => {}
@@ -954,6 +957,10 @@ pub fn main() -> horizon_client::Result<()> {
for flag in &spec.flags {
match flag.as_str() {
"persistent" => m.persistent = true,
+ "named" => {
+ m.custom_name = Some("Peaceful regression".into());
+ m.persistent = true;
+ }
"ridden" => {
m.extra.head = Some("minecraft:red_harness".into());
m.rider = Some(horizon_client::mobs::equine::Rider::default());
@@ -1090,7 +1097,7 @@ pub fn main() -> horizon_client::Result<()> {
block_tags: &items.block_tags,
biomes: &biomes,
mob_griefing: true,
- difficulty: LocalDifficulty::new(2, world.day_time, 0, 0.0),
+ difficulty: LocalDifficulty::new(s.difficulty, world.day_time, 0, 0.0),
end: false,
world_seed: 0,
// FlatLevelSource.getSeaLevel.
diff --git a/crates/horizon-client/examples/probe/section_mesh_fixture.rs b/crates/horizon-client/examples/probe/section_mesh_fixture.rs
index e212a5268..89624b78c 100644
--- a/crates/horizon-client/examples/probe/section_mesh_fixture.rs
+++ b/crates/horizon-client/examples/probe/section_mesh_fixture.rs
@@ -7,7 +7,7 @@ use horizon_client::{
biome::{BiomeInfo, Biomes, GrassModifier},
blocks::BlockFacts,
chunk::{Chunk, Light, MIN_Y, index},
- mesher::{MeshTables, Mesher, OPAQUE_LAYER, TerrainVertex},
+ mesher::{EMISSION_BITS, MeshTables, Mesher, OPAQUE_LAYER, TerrainVertex},
models,
};
use horizon_core::biome::BiomeZoom;
@@ -161,7 +161,15 @@ fn scene(
.iter()
.map(|v| {
let ([x, y, z], [u, w]) = (v.pos, v.uv());
- json!([x, y, z, u, w, argb(v.color), light_coords(v.light)])
+ json!([
+ x,
+ y,
+ z,
+ u,
+ w,
+ argb(v.color | EMISSION_BITS),
+ light_coords(v.light)
+ ])
})
.collect();
quads.push(json!({"layer": layer, "sprite": sprite, "vertices": vertices}));
diff --git a/crates/horizon-client/examples/probe/squid_client_probe.rs b/crates/horizon-client/examples/probe/squid_client_probe.rs
new file mode 100644
index 000000000..f9f9c9b2c
--- /dev/null
+++ b/crates/horizon-client/examples/probe/squid_client_probe.rs
@@ -0,0 +1,51 @@
+use horizon_client::{
+ Result,
+ mobs::{Mob, MobTypes, aquatic},
+};
+use horizon_core::geometry::Vec3;
+use serde_json::Value;
+use std::{
+ fs::File,
+ io::{BufRead, BufReader, BufWriter, Write},
+ path::Path,
+ sync::Arc,
+};
+
+pub fn main() -> Result<()> {
+ let args: Vec<_> = crate::args().collect();
+ let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3");
+ let types = MobTypes::load(&root)?;
+ let mut out = BufWriter::new(File::create(&args[2])?);
+ for line in BufReader::new(File::open(&args[1])?).lines() {
+ let row: Value = serde_json::from_str(&line?)?;
+ let kind = Arc::new(
+ types.types[&format!("minecraft:{}", row["species"].as_str().unwrap())].clone(),
+ );
+ let mut mob = Mob::new(1, kind, Vec3::ZERO, 0.0);
+ mob.fluids.in_water = row["water"].as_bool().unwrap();
+ mob.velocity = Vec3::new(
+ row["x"].as_f64().unwrap(),
+ row["y"].as_f64().unwrap(),
+ row["z"].as_f64().unwrap(),
+ );
+ mob.body_yaw = row["body"].as_f64().unwrap() as f32;
+ for (i, value) in row["v"].as_array().unwrap().iter().enumerate() {
+ mob.anim.v[i] = value.as_f64().unwrap() as f32;
+ }
+ if row["reset"].as_bool().unwrap() {
+ aquatic::squid_cycle_event(&mut mob);
+ }
+ let velocity = mob.velocity;
+ aquatic::squid_client_step(&mut mob);
+ assert_eq!(
+ velocity, mob.velocity,
+ "a remote animation must preserve packet velocity"
+ );
+ write!(out, "{:08x}", mob.body_yaw.to_bits())?;
+ for value in &mob.anim.v[..6] {
+ write!(out, " {:08x}", value.to_bits())?;
+ }
+ writeln!(out)?;
+ }
+ Ok(())
+}
diff --git a/crates/horizon-client/src/animate_tick.rs b/crates/horizon-client/src/animate_tick.rs
index 48e8f16f4..b3a587114 100644
--- a/crates/horizon-client/src/animate_tick.rs
+++ b/crates/horizon-client/src/animate_tick.rs
@@ -58,7 +58,10 @@ enum Animate {
BubbleColumn {
drag: bool,
},
- Fire,
+ /// `BaseFireBlock` (`SoulFireBlock.canBurn` is always true).
+ Fire {
+ soul: bool,
+ },
/// An active sculk sensor.
SculkSensor,
/// Potent sulfur that is not dry.
@@ -140,7 +143,8 @@ fn classify(name: &str, prop: &dyn Fn(&str) -> Option) -> Animate {
"bubble_column" => Animate::BubbleColumn {
drag: prop("drag").as_deref() == Some("true"),
},
- "fire" | "soul_fire" => Animate::Fire,
+ "fire" => Animate::Fire { soul: false },
+ "soul_fire" => Animate::Fire { soul: true },
"dried_ghast" => Animate::DriedGhast {
waterlogged: prop("waterlogged").as_deref() == Some("true"),
},
@@ -190,6 +194,8 @@ pub struct AnimateTables {
power_transmitter: Vec,
/// `#triggers_ambient_dried_ghast_block_sounds` (soul sand, soul soil).
dried_ghast_sounds: Vec,
+ /// `FireBlock.canBurn`: states with ignite odds above zero (none waterlogged).
+ burnable: Vec,
/// Biome id -> ambient particles and probabilities (`minecraft:visual/ambient_particles`).
ambient: Vec>,
}
@@ -220,6 +226,21 @@ impl AnimateTables {
})
.collect()
};
+ let behaviour: serde_json::Value =
+ serde_json::from_slice(&std::fs::read(root.join("block-behaviours.json"))?)?;
+ let ignite_odds = behaviour["fire"]["igniteOdds"].as_object();
+ let burnable = (0..registry.len() as u32)
+ .map(|id| {
+ // FireBlock.getIgniteOdds: a waterlogged state never burns.
+ registry.state(BlockStateId(id)).is_some_and(|s| {
+ s.properties.get("waterlogged").map(String::as_str) != Some("true")
+ && ignite_odds
+ .and_then(|m| m.get(s.name.as_str()))
+ .and_then(serde_json::Value::as_i64)
+ .is_some_and(|odds| odds > 0)
+ })
+ })
+ .collect();
let mut ambient = vec![Vec::new(); biomes.list.len()];
for (name, &id) in &biomes.index {
let path = root.join(format!(
@@ -248,6 +269,7 @@ impl AnimateTables {
power_provider: by_state("minecraft:enchantment_power_provider"),
power_transmitter: by_state("minecraft:enchantment_power_transmitter"),
dried_ghast_sounds: by_state("minecraft:triggers_ambient_dried_ghast_block_sounds"),
+ burnable,
ambient,
})
}
@@ -1103,7 +1125,7 @@ fn block_animate_tick(
}
}
}
- Animate::Fire => {
+ Animate::Fire { soul } => {
let r = engine.random();
if next_int(r, 24) == 0 {
let (volume, pitch) = (1.0 + r.next_float(), r.next_float() * 0.7 + 0.3);
@@ -1115,6 +1137,55 @@ fn block_animate_tick(
pitch,
);
}
+ // BaseFireBlock.animateTick: three large smoke above the fire, or
+ // two at each burnable neighbour's face when nothing burnable or
+ // sturdy supports it (SoulFireBlock.canBurn is always true).
+ let can_burn = |p: [i32; 3]| {
+ soul || tables
+ .burnable
+ .get(ctx.state(p).0 as usize)
+ .copied()
+ .unwrap_or(false)
+ };
+ let below = [pos[0], pos[1] - 1, pos[2]];
+ let sturdy = ctx.facts.get(ctx.state(below)).sturdy_full & (1 << 1) != 0;
+ if !can_burn(below) && !sturdy {
+ // (neighbour, coordinate axis, face at the axis' high end): the
+ // west, east, north, south and above faces, in vanilla order.
+ for (at, axis, high) in [
+ ([pos[0] - 1, pos[1], pos[2]], 0usize, false),
+ ([pos[0] + 1, pos[1], pos[2]], 0, true),
+ ([pos[0], pos[1], pos[2] - 1], 2, false),
+ ([pos[0], pos[1], pos[2] + 1], 2, true),
+ ([pos[0], pos[1] + 1, pos[2]], 1, true),
+ ] {
+ if !can_burn(at) {
+ continue;
+ }
+ for _ in 0..2 {
+ let r = engine.random();
+ let d = [r.next_double(), r.next_double(), r.next_double()];
+ let mut p = [fx + d[0], fy + d[1], fz + d[2]];
+ // The face coordinate hugs the edge: `* 0.1F` (float).
+ p[axis] = if high {
+ f64::from(pos[axis]) + 1.0 - d[axis] * f64::from(0.1f32)
+ } else {
+ f64::from(pos[axis]) + d[axis] * f64::from(0.1f32)
+ };
+ add(engine, T::LargeSmoke, p, [0.0; 3]);
+ }
+ }
+ } else {
+ for _ in 0..3 {
+ let r = engine.random();
+ let p = [
+ fx + r.next_double(),
+ fy + r.next_double() * 0.5 + 0.5,
+ fz + r.next_double(),
+ ];
+ add(engine, T::LargeSmoke, p, [0.0; 3]);
+ }
+ }
}
}
}
diff --git a/crates/horizon-client/src/benchmark.rs b/crates/horizon-client/src/benchmark.rs
index bcb94589c..12e38e78a 100644
--- a/crates/horizon-client/src/benchmark.rs
+++ b/crates/horizon-client/src/benchmark.rs
@@ -323,10 +323,15 @@ const QUIET_MESHES_PER_S: f64 = 100.0;
/// happened (`tools/perf_bench.py`'s `wait_loaded`). With `status.streaming`, the sections are
/// drawn once no stage has held work for half a second: blocks changing after the load (fluids
/// settling, random ticks) only remesh in bursts. Without it, meshing has to go quiet.
+///
+/// Right after a teleport (and at spawn) the game reports nothing queued and steady counts for over
+/// a second before it starts loading: the counts must change once, or stay full for 5 s, and the
+/// resident chunks reach [`view_chunks`], before "steady" means loaded.
fn wait_loaded(control: &Control, near: impl Fn(&Value) -> bool) -> Result {
let deadline = Instant::now() + LOAD_LIMIT;
let (mut last, mut changed, mut meshing) = (None, Instant::now(), Instant::now());
let mut previous: Option<(Instant, f64)> = None;
+ let (mut first, mut moved, began) = (None, false, Instant::now());
while Instant::now() < deadline {
let s = control.status();
let now = Instant::now();
@@ -339,6 +344,19 @@ fn wait_loaded(control: &Control, near: impl Fn(&Value) -> bool) -> Result 0
+ && resident >= min_chunks
+ && now - began > Duration::from_secs(5));
+ if min_chunks > 0 && (!moved || resident < min_chunks) {
+ (last, changed, previous, meshing) = (None, now, None, now);
+ thread::sleep(Duration::from_millis(50));
+ continue;
+ }
if last != Some(counts) {
(last, changed) = (Some(counts), now);
}
@@ -370,6 +388,12 @@ fn wait_loaded(control: &Control, near: impl Fn(&Value) -> bool) -> Result u64 {
+ (0.8 * std::f64::consts::PI * (distance * distance) as f64) as u64
+}
+
/// Vanilla's yaw facing from `start` to `end`.
fn yaw(start: [f64; 3], end: [f64; 3]) -> f64 {
(-(end[0] - start[0])).atan2(end[2] - start[2]).to_degrees()
@@ -420,6 +444,13 @@ mod tests {
assert!(scenes("nether").is_none());
}
+ #[test]
+ fn view_chunks_matches_perf_bench() {
+ assert_eq!(view_chunks(12), 361);
+ assert_eq!(view_chunks(32), 2573);
+ assert_eq!(view_chunks(0), 0);
+ }
+
#[test]
fn yaw_matches_vanilla() {
// South (+Z) is 0, west (-X) 90, east (+X) -90.
diff --git a/crates/horizon-client/src/block_entity_render.rs b/crates/horizon-client/src/block_entity_render.rs
index 10c5e2509..28a876d80 100644
--- a/crates/horizon-client/src/block_entity_render.rs
+++ b/crates/horizon-client/src/block_entity_render.rs
@@ -558,12 +558,11 @@ fn frac(x: f32) -> f32 {
/// `StringSplitter.splitLines(...).get(0)` for plain text: the first line,
/// wrapped at the last space that fits or mid-word when none does.
fn first_line(line: &str, max_width: f32, advance: &dyn Fn(char) -> f32) -> String {
- let chars: Vec = line.chars().collect();
let mut width = 0.0;
let mut last_space = None;
- for (i, &c) in chars.iter().enumerate() {
+ for (i, c) in line.char_indices() {
if c == '\n' {
- return chars[..i].iter().collect();
+ return line[..i].to_owned();
}
if c == ' ' {
last_space = Some(i);
@@ -572,9 +571,11 @@ fn first_line(line: &str, max_width: f32, advance: &dyn Fn(char) -> f32) -> Stri
if width > max_width {
let end = match last_space {
Some(s) if s > 0 => s,
- _ => i.max(1),
+ _ if i > 0 => i,
+ // The first char alone is too wide: take that one char.
+ _ => c.len_utf8(),
};
- return chars[..end].iter().collect();
+ return line[..end].to_owned();
}
}
line.to_owned()
@@ -2098,4 +2099,13 @@ mod tests {
assert_eq!(out[1].pos, [2.0, 0.0, 0.0]);
assert_eq!(out[2].uv, [0.5, 0.5]);
}
+
+ #[test]
+ fn first_line_wraps_on_bytes_without_splitting_a_char() {
+ let advance = |c: char| if c == ' ' { 4.0 } else { 6.0 };
+ assert_eq!(first_line("ab cd ef", 13.0, &advance), "ab");
+ assert_eq!(first_line("abcdef", 13.0, &advance), "ab");
+ assert_eq!(first_line("\u{6771}", 3.0, &advance), "\u{6771}");
+ assert_eq!(first_line("ab\ncd", 100.0, &advance), "ab");
+ }
}
diff --git a/crates/horizon-client/src/bundle.rs b/crates/horizon-client/src/bundle.rs
index 8658b5815..42473729f 100644
--- a/crates/horizon-client/src/bundle.rs
+++ b/crates/horizon-client/src/bundle.rs
@@ -166,6 +166,38 @@ impl Contents {
Some(stack)
}
+ /// The stacks inside, in display order (the first is the next out).
+ pub fn items(&self) -> &[ItemStack] {
+ &self.items
+ }
+
+ /// `getNumberOfItemsToShow`: a full grid shows 11 and a `+N`, so the last
+ /// row keeps its fourth cell for the count.
+ pub fn shown_count(&self) -> usize {
+ let count = self.items.len();
+ let available: usize = if count > 12 { 11 } else { 12 };
+ let on_last_row = count % 4;
+ let empty = if on_last_row == 0 { 0 } else { 4 - on_last_row };
+ count.min(available.saturating_sub(empty))
+ }
+
+ /// The items the tooltip shows, and how many sit beyond them.
+ pub fn shown(&self) -> (&[ItemStack], u32) {
+ let n = self.shown_count();
+ let hidden = self.items[n..].iter().map(|s| s.count).sum();
+ (&self.items[..n], hidden)
+ }
+
+ /// The `Fraction` weight as the float the progress bar reads.
+ pub fn weight_fraction(&self) -> f32 {
+ self.weight.n as f32 / self.weight.d as f32
+ }
+
+ /// `Fraction.ONE`: a full bundle draws the full bar and says so.
+ pub fn is_full(&self) -> bool {
+ self.weight.n >= self.weight.d
+ }
+
/// `toImmutable`, written back into the bundle's component.
pub fn store(self, bundle: &mut ItemStack) {
bundle.data_mut().bundle_contents = self.items;
@@ -190,3 +222,174 @@ mod tests {
assert_eq!(over.how_many(Weight::new(1, 64)), 0);
}
}
+
+// ------------------------------------------------------------- the tooltip
+
+/// `ClientBundleTooltip`'s geometry, relative to the tooltip box's top-left:
+/// the 96 pixel column of a 4 wide slot grid and the progress bar under it.
+#[derive(Clone, Debug, PartialEq)]
+pub struct TooltipLayout {
+ /// The component's own width (96) and height, the text's excluded.
+ pub width: f32,
+ pub height: f32,
+ /// Each slot's top-left and the stack in it, in reading order.
+ pub slots: Vec<([f32; 2], ItemStack)>,
+ /// The `+N` of the items past the grid: its top-left and the count.
+ pub overflow: Option<([f32; 2], u32)>,
+ /// The progress bar's top-left, its fill's width out of 94, and whether
+ /// the weight fills it (the full sprite).
+ pub bar: [f32; 2],
+ pub fill: f32,
+ pub full: bool,
+ /// `item.minecraft.bundle.empty` / `.full`, centred over the bar.
+ pub label: Option<&'static str>,
+ /// An empty bundle's description, wrapped at `width` from here.
+ pub empty_description: Option<[f32; 2]>,
+}
+
+/// The tooltip of a bundle's contents (`ClientBundleTooltip`), or None when
+/// the stack is not a bundle. `x`/`y` are the box's top-left, `box_width` the
+/// tooltip's width, which the component is centred in.
+pub fn tooltip_layout(
+ items: &Items,
+ stack: &ItemStack,
+ x: f32,
+ y: f32,
+ box_width: f32,
+) -> Option {
+ let contents = Contents::of(items, stack)?;
+ let offset = ((box_width - 96.0) / 2.0).floor();
+ if contents.items().is_empty() {
+ // The description takes the top; the bar follows it.
+ let description_height = 9.0 * 2.0;
+ return Some(TooltipLayout {
+ width: 96.0,
+ height: description_height + 13.0 + 8.0,
+ slots: Vec::new(),
+ overflow: None,
+ bar: [x + offset, y + description_height + 4.0],
+ fill: 0.0,
+ full: false,
+ label: Some("item.minecraft.bundle.empty"),
+ empty_description: Some([x + offset, y]),
+ });
+ }
+ let (shown, hidden) = contents.shown();
+ // `gridSizeY` counts `slotCount()` (at most twelve), not the slots the
+ // overflow count leaves room for.
+ let rows = contents.items().len().min(12).div_ceil(4).max(1);
+ let grid_height = rows as f32 * 24.0;
+ let x_start = x + offset + 96.0;
+ let y_start = y + grid_height;
+ let mut slots = Vec::with_capacity(shown.len());
+ let mut overflow = None;
+ // Rows fill bottom-up and columns right-to-left, as vanilla draws them.
+ let mut slot = 0;
+ for row in 1..=rows {
+ for column in 1..=4 {
+ let at = [x_start - column as f32 * 24.0, y_start - row as f32 * 24.0];
+ if hidden > 0 && row == 1 && column == 1 {
+ overflow = Some((at, hidden));
+ } else if let Some(stack) = shown.get(slot) {
+ slots.push((at, stack.clone()));
+ slot += 1;
+ }
+ }
+ }
+ let weight = contents.weight_fraction();
+ Some(TooltipLayout {
+ width: 96.0,
+ height: grid_height + 13.0 + 8.0,
+ slots,
+ overflow,
+ bar: [x + offset, y + grid_height + 4.0],
+ // `Mth.mulAndTruncate(weight, 94)`, kept within the bar.
+ fill: (weight * 94.0).trunc().clamp(0.0, 94.0),
+ full: contents.is_full(),
+ label: if contents.is_full() {
+ Some("item.minecraft.bundle.full")
+ } else {
+ None
+ },
+ empty_description: None,
+ })
+}
+
+#[cfg(test)]
+mod tooltip_tests {
+ use super::*;
+
+ /// The prepared resources, when they are on this machine: the tooltip
+ /// asks the item registry whether a stack is a bundle.
+ fn items() -> Option {
+ let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
+ .join("../../.local/horizon-client/26.3");
+ if !root.join("items.json").exists() {
+ return None;
+ }
+ let assets = crate::assets::Assets::load(&root).ok()?;
+ let facts = crate::blocks::BlockFacts::load(&root, assets.registry).ok()?;
+ Items::load(&root, assets.registry, &facts).ok()
+ }
+
+ fn bundle_with(counts: &[(&str, u32)]) -> ItemStack {
+ let mut bundle = ItemStack::new("minecraft:bundle", 1);
+ bundle.data_mut().bundle_contents = counts
+ .iter()
+ .map(|(item, n)| ItemStack::new(item, *n))
+ .collect();
+ bundle
+ }
+
+ #[test]
+ fn a_grid_shows_twelve_stacks_and_counts_the_rest() {
+ let Some(items) = items() else { return };
+ let twelve: Vec<(&str, u32)> = (0..12).map(|_| ("minecraft:stone", 1)).collect();
+ let layout = tooltip_layout(&items, &bundle_with(&twelve), 0.0, 0.0, 96.0).unwrap();
+ assert_eq!(layout.slots.len(), 12);
+ assert_eq!(layout.overflow, None);
+ assert_eq!(layout.height, 3.0 * 24.0 + 13.0 + 8.0);
+ // Rows fill bottom-up and columns right-to-left: the first item is in
+ // the bottom-right cell, the twelfth in the top-left.
+ assert_eq!(layout.slots[0].0, [96.0 - 24.0, 3.0 * 24.0 - 24.0]);
+ assert_eq!(&*layout.slots[0].1.item, "minecraft:stone");
+ assert_eq!(layout.slots[11].0, [0.0, 0.0]);
+ // A wider tooltip centres the 96 pixel column in it.
+ let centred = tooltip_layout(&items, &bundle_with(&twelve), 0.0, 0.0, 120.0).unwrap();
+ assert_eq!(centred.slots[0].0[0], 12.0 + 96.0 - 24.0);
+ // Thirteen stacks: `getNumberOfItemsToShow` leaves the last row room
+ // for the count, so 8 slots show and `+6` (four stones and two dirt)
+ // takes the first cell.
+ let mut counts = twelve;
+ counts.push(("minecraft:dirt", 2));
+ let layout = tooltip_layout(&items, &bundle_with(&counts), 0.0, 0.0, 96.0).unwrap();
+ assert_eq!(layout.slots.len(), 8);
+ assert_eq!(layout.overflow, Some(([96.0 - 24.0, 48.0], 6)));
+ }
+
+ #[test]
+ fn the_bar_fills_with_the_weight_and_an_empty_bundle_explains_itself() {
+ let Some(items) = items() else { return };
+ let layout = tooltip_layout(&items, &bundle_with(&[]), 4.0, 8.0, 96.0).unwrap();
+ assert!(layout.slots.is_empty());
+ assert_eq!(layout.fill, 0.0);
+ assert_eq!(layout.label, Some("item.minecraft.bundle.empty"));
+ assert_eq!(layout.empty_description, Some([4.0, 8.0]));
+ assert_eq!(layout.bar, [4.0, 8.0 + 18.0 + 4.0]);
+ // A bundle holding one 64 stack is full: the whole bar, the full label.
+ let layout = tooltip_layout(
+ &items,
+ &bundle_with(&[("minecraft:stone", 64)]),
+ 0.0,
+ 0.0,
+ 96.0,
+ )
+ .unwrap();
+ assert!(layout.full);
+ assert_eq!(layout.fill, 94.0);
+ assert_eq!(layout.label, Some("item.minecraft.bundle.full"));
+ // Something that is not a bundle has no component.
+ let dirt = ItemStack::new("minecraft:dirt", 1);
+ assert!(tooltip_layout(&items, &dirt, 0.0, 0.0, 96.0).is_none());
+ }
+}
diff --git a/crates/horizon-client/src/chat_tests.rs b/crates/horizon-client/src/chat_tests.rs
index 6b058c0fb..beec6cd81 100644
--- a/crates/horizon-client/src/chat_tests.rs
+++ b/crates/horizon-client/src/chat_tests.rs
@@ -436,3 +436,34 @@ fn the_chat_option_filters_the_lines_both_ways() {
chat.set_abilities(&font, ChatAbilities::new(ChatVisibility::Full));
assert_eq!(shown(&chat), ["client", "server", "player"]);
}
+
+/// The in-frame LAN host's chat: a guest's line is a player message, so the
+/// Chat option's player-message permission hides it at Commands Only and
+/// Hidden; command feedback (`/list`, `/tp`) is a system message and stays
+/// at Commands Only.
+#[test]
+fn a_guests_line_follows_the_player_message_permission() {
+ let font = font();
+ let shown = |visibility, source| {
+ let mut chat = Chat::default();
+ chat.set_abilities(&font, ChatAbilities::new(visibility));
+ chat.add_message(&font, Text::literal("line"), None, source, 0, false);
+ chat.messages.len()
+ };
+ for (visibility, player, system) in [
+ (ChatVisibility::Full, 1, 1),
+ (ChatVisibility::System, 0, 1),
+ (ChatVisibility::Hidden, 0, 0),
+ ] {
+ assert_eq!(
+ shown(visibility, ChatSource::Player),
+ player,
+ "{visibility:?}"
+ );
+ assert_eq!(
+ shown(visibility, ChatSource::Server),
+ system,
+ "{visibility:?}"
+ );
+ }
+}
diff --git a/crates/horizon-client/src/chunk.rs b/crates/horizon-client/src/chunk.rs
index 41330df5f..7cd375343 100644
--- a/crates/horizon-client/src/chunk.rs
+++ b/crates/horizon-client/src/chunk.rs
@@ -24,6 +24,20 @@ pub fn block_strategy() -> Strategy {
Strategy::blocks(BLOCK_BITS.load(std::sync::atomic::Ordering::Relaxed))
}
+/// Frees `columns` (`count` of them) on a short-lived thread of its own
+/// when there are many: a teleport or a dimension change drops a few
+/// thousand columns at once, and freeing their sections took the frame
+/// 15-20 ms. A few are freed here.
+pub fn free_columns(columns: T, count: usize) {
+ if count < 64 {
+ return;
+ }
+ // Should the thread not start, the closure drops `columns` here.
+ let _ = std::thread::Builder::new()
+ .name("Chunk freeing".into())
+ .spawn(move || drop(columns));
+}
+
/// 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);
@@ -147,6 +161,9 @@ pub struct Chunk {
/// Structure starts reaching into this chunk (`structures.References`):
/// structure id and the start's chunk.
pub structure_refs: Vec<(Arc, (i32, i32))>,
+ /// The starts generation made in this chunk (`structures.starts`), saved
+ /// with the state their placement has reached by then.
+ pub structure_starts: Vec>,
}
impl Chunk {
pub fn empty(x: i32, z: i32) -> Self {
@@ -164,6 +181,7 @@ impl Chunk {
block_entities: Vec::new(),
block_names: Vec::new(),
structure_refs: Vec::new(),
+ structure_starts: Vec::new(),
postprocess: Vec::new(),
saved: None,
entities: Vec::new(),
diff --git a/crates/horizon-client/src/clipboard.rs b/crates/horizon-client/src/clipboard.rs
index c21460cdb..76872091c 100644
--- a/crates/horizon-client/src/clipboard.rs
+++ b/crates/horizon-client/src/clipboard.rs
@@ -346,6 +346,11 @@ mod tests {
assert_eq!(filter_text(s, true), "abc\nde\u{80}");
assert_eq!(strip_formatting("§aRed§r §zx§"), "Red §zx§");
assert_eq!(helper_clipboard_contents("§lone\r\ntwo"), "one\ntwo");
+ // TextFieldHelper.getClipboardContents runs before insertText's filter.
+ assert_eq!(
+ filter_text(&helper_clipboard_contents("§aHello"), false),
+ "Hello"
+ );
}
struct Field {
diff --git a/crates/horizon-client/src/container_screen.rs b/crates/horizon-client/src/container_screen.rs
index 2c490861a..77c7ad8aa 100644
--- a/crates/horizon-client/src/container_screen.rs
+++ b/crates/horizon-client/src/container_screen.rs
@@ -275,4 +275,30 @@ mod tests {
Release::Click(Input::QuickMove)
);
}
+
+ /// A press whose release never arrives (the pointer left the window, or
+ /// the screen closed first) leaves `isQuickCrafting` set; the next screen
+ /// starts with its own state, as `AbstractContainerScreen` does, so the
+ /// reset the game runs on open/close makes clicks work again.
+ #[test]
+ fn a_lost_release_does_not_wedge_the_next_screen() {
+ let mut clicks = Clicks::default();
+ let slot = Target::::Slot(3);
+ // Pressed with a stack carried: a quick craft drag starts, and its
+ // release is lost.
+ assert_eq!(clicks.press(&slot, Button::Left, false, false, false), None);
+ assert!(clicks.quick_crafting().is_some());
+ assert_eq!(
+ clicks.press(&slot, Button::Left, true, false, false),
+ None,
+ "the stuck drag swallows the next press"
+ );
+ // A new screen: fresh state, so the press is a normal click again.
+ clicks = Clicks::default();
+ assert!(clicks.quick_crafting().is_none());
+ assert_eq!(
+ clicks.press(&slot, Button::Left, true, false, false),
+ Some(Input::Pickup)
+ );
+ }
}
diff --git a/crates/horizon-client/src/containers.rs b/crates/horizon-client/src/containers.rs
index d13c5e45b..6aba0970e 100644
--- a/crates/horizon-client/src/containers.rs
+++ b/crates/horizon-client/src/containers.rs
@@ -49,6 +49,17 @@ pub struct Furnace {
/// rather than per item, and a save keeps it as the `RecipesUsed` compound.
pub recipes_used: HashMap,
}
+/// The key `recipes_used` keeps a recipe under: its full id, as vanilla's `ResourceKey`
+/// writes it in `RecipesUsed`. Horizon's own recipe ids are file names without the namespace, so
+/// keeping both forms would write a key twice, and vanilla refuses such a furnace
+/// ("Duplicate entry for key").
+pub fn recipe_used_key(id: &str) -> String {
+ if id.contains(':') {
+ id.to_owned()
+ } else {
+ format!("minecraft:{id}")
+ }
+}
/// A hopper's slots: the only plain container with five.
pub const HOPPER_SLOTS: usize = 5;
@@ -83,9 +94,66 @@ pub enum BlockEntity {
Beehive(Box),
/// `CommandBlockEntity` of the three command blocks (`crate::command_block`).
CommandBlock(Box),
+ /// `CampfireBlockEntity` of campfires and soul campfires.
+ Campfire(Box),
/// `SkullBlockEntity` of every skull and head block.
Skull(Skull),
}
+/// `CampfireBlockEntity`: four cooking slots, each with its
+/// `cookingProgress` and `cookingTime` (the recipe's).
+#[derive(Clone, Debug, Default, PartialEq)]
+pub struct Campfire {
+ pub items: [Option; 4],
+ pub progress: [i32; 4],
+ pub total: [i32; 4],
+}
+impl Campfire {
+ /// `placeFood`'s slot: the first empty one, given the item's
+ /// `campfire_cooking` time; it takes one item of `held`.
+ pub fn place_food(&mut self, held: &mut ItemStack, cooking_time: i32) -> bool {
+ let Some(slot) = self.items.iter().position(Option::is_none) else {
+ return false;
+ };
+ self.total[slot] = cooking_time;
+ self.progress[slot] = 0;
+ self.items[slot] = Some(ItemStack {
+ count: 1,
+ ..held.clone()
+ });
+ held.count = held.count.saturating_sub(1);
+ true
+ }
+ /// `cookTick` (lit): each item's progress rises by one; the slots whose
+ /// progress reached their cooking time give up their item (the caller
+ /// drops its result).
+ pub fn cook_tick(&mut self) -> Vec<(usize, ItemStack)> {
+ let mut done = Vec::new();
+ for i in 0..4 {
+ if self.items[i].is_none() {
+ continue;
+ }
+ self.progress[i] += 1;
+ if self.progress[i] >= self.total[i]
+ && let Some(item) = self.items[i].take()
+ {
+ done.push((i, item));
+ }
+ }
+ done
+ }
+ /// `cooldownTick` (unlit): the progress falls by two, no lower than 0.
+ /// Whether anything changed (`setChanged`).
+ pub fn cooldown_tick(&mut self) -> bool {
+ let mut changed = false;
+ for i in 0..4 {
+ if self.progress[i] > 0 {
+ changed = true;
+ self.progress[i] = (self.progress[i] - 2).clamp(0, self.total[i]);
+ }
+ }
+ changed
+ }
+}
/// `SkullBlockEntity`: the player profile the head shows (kept as loaded:
/// a compound, or a string when it names a player only), the note block
/// sound and `custom_name`.
@@ -304,6 +372,9 @@ impl BlockEntity {
if name == "minecraft:brewing_stand" {
return Some(Self::Brewing(Box::new(crate::brewing::BrewingStand::new())));
}
+ if name == "minecraft:campfire" || name == "minecraft:soul_campfire" {
+ return Some(Self::Campfire(Box::default()));
+ }
let size = match name {
"minecraft:chest" | "minecraft:trapped_chest" | "minecraft:barrel" => 27,
n if n.ends_with("copper_chest") => 27,
@@ -341,6 +412,7 @@ impl BlockEntity {
Self::Beehive(_) => "minecraft:beehive",
Self::CommandBlock(_) => "minecraft:command_block",
Self::Skull(_) => "minecraft:skull",
+ Self::Campfire(_) => "minecraft:campfire",
}
}
pub fn contents(&self) -> Vec {
@@ -362,6 +434,7 @@ impl BlockEntity {
Self::Sculk(_) => Vec::new(),
Self::Banner(_) => Vec::new(),
Self::Crafter(c) => c.items.iter().flatten().cloned().collect(),
+ Self::Campfire(c) => c.items.iter().flatten().cloned().collect(),
}
}
pub fn slots(&self) -> &[Option] {
@@ -382,7 +455,9 @@ impl BlockEntity {
| Self::TrialSpawner(_)
| Self::Vault(_)
| Self::Beehive(_)
- | Self::CommandBlock(_) => &[],
+ | Self::CommandBlock(_)
+ // Not a Container (only Clearable): hoppers don't reach in.
+ | Self::Campfire(_) => &[],
}
}
pub fn slots_mut(&mut self) -> &mut [Option] {
@@ -403,7 +478,8 @@ impl BlockEntity {
| Self::TrialSpawner(_)
| Self::Vault(_)
| Self::Beehive(_)
- | Self::CommandBlock(_) => &mut [],
+ | Self::CommandBlock(_)
+ | Self::Campfire(_) => &mut [],
}
}
}
@@ -638,7 +714,10 @@ impl Furnace {
Some(out) => out.count += recipe.result.count,
None => self.items[2] = Some(recipe.result.clone()),
}
- *self.recipes_used.entry(recipe.id.clone()).or_insert(0) += 1;
+ *self
+ .recipes_used
+ .entry(recipe_used_key(&recipe.id))
+ .or_insert(0) += 1;
if let Some(i) = self.items[0].as_mut() {
i.count -= 1;
if i.count == 0 {
@@ -692,6 +771,41 @@ mod tests {
use super::*;
use horizon_core::random::{LegacyRandom, RandomSource};
+ #[test]
+ fn campfire_slots_take_food_cook_it_and_cool_down() {
+ let mut c = Campfire::default();
+ let mut beef = ItemStack::new("minecraft:beef", 2);
+ // placeFood: the first free slot, one item, its recipe's time.
+ assert!(c.place_food(&mut beef, 600));
+ assert_eq!(beef.count, 1);
+ assert_eq!(c.items[0].as_ref().map(|s| s.count), Some(1));
+ assert_eq!((c.progress[0], c.total[0]), (0, 600));
+ let mut fish = ItemStack::new("minecraft:cod", 1);
+ assert!(c.place_food(&mut fish, 3));
+ assert_eq!(c.items[1].as_ref().map(|s| &*s.item), Some("minecraft:cod"));
+ // cookTick: the cod is done on its third tick, the beef keeps cooking.
+ assert!(c.cook_tick().is_empty());
+ assert!(c.cook_tick().is_empty());
+ let done = c.cook_tick();
+ assert_eq!(done.len(), 1);
+ assert_eq!((done[0].0, &*done[0].1.item), (1, "minecraft:cod"));
+ assert!(c.items[1].is_none());
+ assert_eq!(c.progress[0], 3);
+ // cooldownTick: two a tick, never below zero.
+ assert!(c.cooldown_tick());
+ assert_eq!(c.progress[0], 1);
+ assert!(c.cooldown_tick());
+ assert_eq!(c.progress[0], 0);
+ // Full: four slots, no fifth.
+ for _ in 0..3 {
+ let mut more = ItemStack::new("minecraft:potato", 1);
+ assert!(c.place_food(&mut more, 600));
+ }
+ let mut fifth = ItemStack::new("minecraft:potato", 1);
+ assert!(!c.place_food(&mut fifth, 600));
+ assert_eq!(fifth.count, 1);
+ }
+
/// A whole amount pays itself without a draw; a fraction is a chance of one more.
#[test]
fn create_experience_rounds_the_fraction_by_chance() {
diff --git a/crates/horizon-client/src/crafting.rs b/crates/horizon-client/src/crafting.rs
index 690383963..899ce90d9 100644
--- a/crates/horizon-client/src/crafting.rs
+++ b/crates/horizon-client/src/crafting.rs
@@ -1612,7 +1612,9 @@ impl Recipes {
}
/// The `experience` of the cooking recipe with this file name, and 0 for one this data
/// pack does not have (a save from another pack can name it).
+ /// `id` with or without the `minecraft:` namespace (a furnace keeps the full id).
pub fn cooking_experience(&self, id: &str) -> f32 {
+ let id = id.strip_prefix("minecraft:").unwrap_or(id);
self.cooking
.iter()
.find(|c| c.id == id)
diff --git a/crates/horizon-client/src/creative.rs b/crates/horizon-client/src/creative.rs
index 249e40b11..1a227db65 100644
--- a/crates/horizon-client/src/creative.rs
+++ b/crates/horizon-client/src/creative.rs
@@ -1,9 +1,14 @@
//! Creative inventory (CreativeModeInventoryScreen): vanilla's tabs and their
//! contents as built by the game's own display generators
//! (`creative-inventory.json`), a 9x5 scrolling grid and the search tab.
-use crate::{Result, items::ItemStack};
+use crate::{Result, items::ItemStack, ui::EditBox};
use serde_json::Value;
-use std::{collections::HashMap, fs, path::Path};
+use std::{
+ collections::HashMap,
+ fs,
+ path::Path,
+ sync::{Arc, Mutex},
+};
#[derive(Clone, Debug)]
pub struct Tab {
@@ -35,13 +40,22 @@ pub struct Creative {
pub scroll: f32,
/// The scrollbar is held (scrolling): dragging moves it.
pub scrolling: bool,
- pub search: String,
- /// When the search box took the focus (`EditBox.setFocused`): its
- /// cursor blinks from then.
- pub search_focused: std::time::Instant,
+ /// CreativeModeInventoryScreen's search box: an unbordered `EditBox` of 50.
+ pub search: EditBox,
/// The search tab's stacks, in its display order.
search_items: Vec,
search_index: Vec,
+ /// ItemPickerMenu.items: what the grid shows, filled when the tab or the
+ /// search changes (selectTab, refreshSearchResults), not on every frame.
+ shown: Mutex>,
+}
+/// The grid's stacks for one tab, search text and language.
+struct Shown {
+ tab: usize,
+ search: String,
+ /// A display name, so a language change refills the search results.
+ language: String,
+ items: Arc<[ItemStack]>,
}
/// Display names from the pinned en_us language file.
#[derive(Default)]
@@ -284,10 +298,16 @@ impl Creative {
selected: 0,
scroll: 0.0,
scrolling: false,
- search: String::new(),
- search_focused: std::time::Instant::now(),
+ search: {
+ let mut search = EditBox::new(0.0, 0.0, 80.0, 9.0);
+ search.bordered = false;
+ search.max_length = 50;
+ search.text_color = u32::MAX;
+ search
+ },
search_items,
search_index,
+ shown: Mutex::new(None),
}
}
/// Where a stack comes in the creative tabs (the search tab lists them
@@ -300,18 +320,35 @@ impl Creative {
.position(|s| s.same_item_same_components(stack))
.or_else(|| items.iter().position(|s| s.item == stack.item))
}
+ /// The tabs without their stacks: what the screen draws of them each frame.
+ pub fn tab_headers(&self) -> Vec {
+ self.tabs
+ .iter()
+ .map(|t| Tab {
+ id: t.id.clone(),
+ title: t.title.clone(),
+ icon: t.icon.clone(),
+ items: Vec::new(),
+ top: t.top,
+ column: t.column,
+ search: t.search,
+ inventory: t.inventory,
+ })
+ .collect()
+ }
pub fn tab(&self) -> &Tab {
&self.tabs[self.selected.min(self.tabs.len() - 1)]
}
/// selectTab: a different tab empties the search box; the scrollbar goes to the top.
pub fn select(&mut self, tab: usize) {
if tab != self.selected {
- self.search.clear();
+ self.search.set_value("");
}
self.selected = tab;
self.scroll = 0.0;
if self.tab().search {
- self.search_focused = std::time::Instant::now();
+ self.search.focused = true;
+ self.search.reset_blink();
}
}
/// The search tab's stacks.
@@ -380,17 +417,44 @@ impl Creative {
})
.collect()
}
- /// Items shown by the selected tab (the search tab filters by `search`).
- pub fn items(&self, names: &Names) -> Vec {
- let tab = self.tab();
- if tab.search {
- return self
- .search_results(&self.search, names)
+ /// Items shown by the selected tab (the search tab filters by `search`),
+ /// worked out again only when the tab, the search or the language changed.
+ pub fn items(&self, names: &Names) -> Arc<[ItemStack]> {
+ let tab = self.selected.min(self.tabs.len() - 1);
+ let search = if self.tab().search {
+ self.search.value.as_str()
+ } else {
+ ""
+ };
+ let language = names.display_get("itemGroup.search").unwrap_or_default();
+ let mut shown = self.shown.lock().unwrap_or_else(|e| e.into_inner());
+ if let Some(s) = shown.as_ref()
+ && s.tab == tab
+ && s.search == search
+ && s.language == language
+ {
+ return s.items.clone();
+ }
+ let items: Arc<[ItemStack]> = if self.tab().search {
+ self.search_results(search, names)
.into_iter()
.map(|i| self.search_items[i].clone())
- .collect();
- }
- tab.items.clone()
+ .collect()
+ } else {
+ self.tab().items.as_slice().into()
+ };
+ *shown = Some(Shown {
+ tab,
+ search: search.to_owned(),
+ language,
+ items: items.clone(),
+ });
+ items
+ }
+ /// Drop the shown items, so the next `items` works them out again: a
+ /// reload can rename items without changing the Search label.
+ pub fn forget_items(&self) {
+ *self.shown.lock().unwrap_or_else(|e| e.into_inner()) = None;
}
pub fn rows(&self, names: &Names) -> usize {
self.items(names).len().div_ceil(9)
@@ -754,13 +818,13 @@ mod tests {
(1, false, true),
(2, true, false),
] {
- c.search = "anvil".into();
+ c.search.set_value("anvil");
c.scroll = 0.5;
c.select(selected);
assert_eq!(c.selected, selected);
assert_eq!(c.tab().inventory, inventory);
assert_eq!(c.tab().search, search);
- assert!(c.search.is_empty());
+ assert!(c.search.value.is_empty());
assert_eq!(c.scroll, 0.0);
}
}
@@ -780,12 +844,12 @@ mod tests {
assert_eq!(c.tabs[0].items[0].item.as_ref(), "minecraft:anvil");
let mut c = c;
c.select(1);
- c.search = "anvil".into();
+ c.search.set_value("anvil");
c.scroll = 0.5;
c.select(1);
- assert_eq!((c.search.as_str(), c.scroll), ("anvil", 0.0));
+ assert_eq!((c.search.value.as_str(), c.scroll), ("anvil", 0.0));
c.select(0);
- assert_eq!(c.search, "");
+ assert_eq!(c.search.value, "");
}
#[test]
@@ -899,4 +963,25 @@ mod tests {
c.scroll = 1.0;
assert_eq!(c.scroller_offset(), 95.0);
}
+
+ #[test]
+ fn search_edits_at_its_cursor() {
+ use winit::keyboard::KeyCode;
+ let mut c = creative(0);
+ c.select(1);
+ c.search.insert_text("hello world");
+ for _ in 0..5 {
+ assert!(c.search.key(KeyCode::ArrowLeft, false, false));
+ }
+ c.search.insert_text("X");
+ assert_eq!(c.search.value, "hello Xworld");
+ c.search.key(KeyCode::Home, false, true);
+ c.search.insert_text("Y");
+ assert_eq!(c.search.value, "Yworld");
+ // Ctrl+Left jumps a word; one step right and Backspace deletes before the cursor.
+ c.search.key(KeyCode::ArrowLeft, true, false);
+ c.search.key(KeyCode::ArrowRight, false, false);
+ c.search.key(KeyCode::Backspace, false, false);
+ assert_eq!(c.search.value, "world");
+ }
}
diff --git a/crates/horizon-client/src/cushion.rs b/crates/horizon-client/src/cushion.rs
new file mode 100644
index 000000000..7610a4aab
--- /dev/null
+++ b/crates/horizon-client/src/cushion.rs
@@ -0,0 +1,59 @@
+//! Cushion placement and support checks (`Cushion.canBePlacedAt`/`wouldSurviveAt`).
+use horizon_core::geometry::{Aabb, ShapeOp, Vec3, VoxelShape};
+
+pub trait World {
+ fn outline_bounds(&self, pos: [i32; 3]) -> Option;
+ fn collision_shapes(&self, bounds: Aabb) -> Vec;
+ fn suffocating(&self, pos: [i32; 3]) -> bool;
+}
+
+pub fn blocks_in(a: Aabb) -> impl Iterator- {
+ let (lo, hi) = (a.min_position(), a.max_position());
+ let (x0, y0, z0) = (
+ lo.x.floor() as i32,
+ lo.y.floor() as i32,
+ lo.z.floor() as i32,
+ );
+ let (x1, y1, z1) = (
+ hi.x.floor() as i32,
+ hi.y.floor() as i32,
+ hi.z.floor() as i32,
+ );
+ (y0..=y1).flat_map(move |y| (z0..=z1).flat_map(move |z| (x0..=x1).map(move |x| [x, y, z])))
+}
+
+pub fn survives(world: &impl World, bounds: Aabb) -> bool {
+ let (lo, hi) = (bounds.min_position(), bounds.max_position());
+ let anchor = Aabb::new(
+ Vec3::new(lo.x, lo.y - 0.015625, lo.z),
+ Vec3::new(hi.x.next_down(), lo.y, hi.z.next_down()),
+ );
+ // The extra eighth block expands the scan, not the intersection test.
+ let supported = blocks_in(anchor.expand_towards(Vec3::new(0.0, -0.125, 0.0))).any(|p| {
+ world
+ .outline_bounds(p)
+ .is_some_and(|shape| shape.intersects(anchor))
+ });
+ supported && !blocks_in(bounds.next_deflated()).all(|p| world.suffocating(p))
+}
+
+pub fn can_place(world: &impl World, bounds: Aabb) -> bool {
+ if !survives(world, bounds) {
+ return false;
+ }
+ let (lo, hi) = (bounds.min_position(), bounds.max_position());
+ let slab = Aabb::new(lo, Vec3::new(hi.x, lo.y + 0.015625, hi.z)).next_deflated();
+ let Ok(mut remaining) = VoxelShape::from_box(slab) else {
+ return false;
+ };
+ for shape in world.collision_shapes(slab) {
+ let Ok(next) = remaining.join(&shape, ShapeOp::ONLY_FIRST) else {
+ return false;
+ };
+ remaining = next;
+ if remaining.is_empty() {
+ return false;
+ }
+ }
+ true
+}
diff --git a/crates/horizon-client/src/debug_overlay.rs b/crates/horizon-client/src/debug_overlay.rs
index 7140905a8..f55d4ee6e 100644
--- a/crates/horizon-client/src/debug_overlay.rs
+++ b/crates/horizon-client/src/debug_overlay.rs
@@ -221,18 +221,17 @@ pub fn scale(
pub fn draw(font: &Font, columns: [&[String]; 2], scale: f32, width: i32) -> Vec
{
let mut out = vec![GuiDraw::Push(vec![PoseOp::Scale(scale, scale)])];
for (lines, align_left) in [(columns[0], true), (columns[1], false)] {
- let placed: Vec<(&String, i32, i32)> = lines
+ let placed: Vec<(&String, i32, i32, i32)> = lines
.iter()
.enumerate()
.filter(|(_, l)| !l.is_empty())
.map(|(i, l)| {
let w = font.width(l) as i32;
let left = if align_left { 2 } else { width - 2 - w };
- (l, left, 2 + LINE_HEIGHT * i as i32)
+ (l, left, 2 + LINE_HEIGHT * i as i32, w)
})
.collect();
- for (line, left, top) in &placed {
- let w = font.width(line) as i32;
+ for (_, left, top, w) in &placed {
out.push(fill(
left - 1,
top - 1,
@@ -241,7 +240,7 @@ pub fn draw(font: &Font, columns: [&[String]; 2], scale: f32, width: i32) -> Vec
BACKGROUND,
));
}
- for (line, left, top) in placed {
+ for (line, left, top, _) in placed {
out.push(GuiDraw::Text {
text: Text::literal(line.clone()),
x: left,
diff --git a/crates/horizon-client/src/entities.rs b/crates/horizon-client/src/entities.rs
index 6e8089e9d..69e053411 100644
--- a/crates/horizon-client/src/entities.rs
+++ b/crates/horizon-client/src/entities.rs
@@ -591,6 +591,57 @@ impl Painting {
)
}
}
+/// The `DyeColor` ordinal of a colour name (`DyeColor.byName`), white when
+/// the name is unknown.
+pub fn dye_index(name: &str) -> u8 {
+ crate::mobs::render::DYE_NAMES
+ .iter()
+ .position(|n| *n == name)
+ .unwrap_or(0) as u8
+}
+/// A cushion (`Cushion`, a `BlockAttachedEntity`): a 1 x 0.25 block pillow
+/// that seats a player. It sits on the top face of a block, at the clicked
+/// height, and is coloured by its `DyeColor`.
+#[derive(Clone, Debug)]
+pub struct Cushion {
+ pub id: u32,
+ /// `Entity.position`: the bottom centre of the cushion.
+ pub position: Vec3,
+ /// `BlockAttachedEntity.pos` (the saved `block_pos`).
+ pub pos: [i32; 3],
+ pub yaw: f32,
+ /// `DyeColor` ordinal: white 0, orange 1, ..., black 15.
+ pub color: u8,
+ /// BlockAttachedEntity.ticksSinceLastCheck.
+ pub check_interval: u32,
+}
+impl Cushion {
+ /// `Entity.makeBoundingBox`: x and z half a block out, y to y + 0.25.
+ pub fn bounds(&self) -> ([f64; 3], [f64; 3]) {
+ let p = self.position;
+ (
+ [p.x - 0.5, p.y, p.z - 0.5],
+ [p.x + 0.5, p.y + 0.25, p.z + 0.5],
+ )
+ }
+ /// The `DyeColor` name of the cushion.
+ pub fn color_name(&self) -> &'static str {
+ crate::mobs::render::DYE_NAMES[usize::from(self.color.min(15))]
+ }
+ /// `Cushion.getCushionItemStackWithData`: the matching coloured item.
+ pub fn item(&self) -> String {
+ format!("minecraft:{}_cushion", self.color_name())
+ }
+ /// `Cushion.interact`'s seat: the vehicle's PASSENGER attachment
+ /// `(0, 0.25, 0)` less the player's `(0, 0.6, 0)`.
+ pub fn seat(&self) -> Vec3 {
+ Vec3::new(self.position.x, self.position.y - 0.35, self.position.z)
+ }
+ /// `Entity.getDismountLocationForPassenger`: on top of the cushion.
+ pub fn dismount(&self) -> Vec3 {
+ Vec3::new(self.position.x, self.position.y + 0.25, self.position.z)
+ }
+}
/// An item frame or glow item frame (`ItemFrame`).
#[derive(Clone, Debug)]
pub struct ItemFrame {
diff --git a/crates/horizon-client/src/entity.wgsl b/crates/horizon-client/src/entity.wgsl
index 2e865dfb1..79e11719f 100644
--- a/crates/horizon-client/src/entity.wgsl
+++ b/crates/horizon-client/src/entity.wgsl
@@ -166,12 +166,23 @@ fn linear_fog(d: f32, start: f32, end: f32) -> f32 {
if d >= end { return 1.0; }
return (d - start) / (end - start);
}
+// CRUMBLING_UV_BEGIN
+fn crumbling_uv(uv: vec2, packed_origin: vec2, sprite_size: vec2, dimensions: vec2) -> vec2 {
+ let size = vec2(dimensions);
+ // Undo stage_light's 16-bit quantization before adding local texels. Otherwise
+ // different stages can fall on opposite sides of a nearest-sampling boundary.
+ let origin = round(packed_origin * size);
+ let extent = max(round(sprite_size * size), vec2(1.0));
+ let local = min(floor(fract(uv) * extent), extent - vec2(1.0));
+ return (origin + local + vec2(0.5)) / size;
+}
+// CRUMBLING_UV_END
@fragment fn fs_entity(in: Out, @builtin(front_facing) front: bool) -> @location(0) vec4 {
var uv = in.uv;
if in.overlay == 6u { uv.y = in.wrap.x + fract(in.uv.y) * in.wrap.y; }
if in.overlay == 11u || in.overlay == 12u {
// Each crack's stage sprite origin comes in its light bytes (breaking::stage_light).
- uv = in.wrap + fract(in.uv) * globals.crumbling_sprite.zw;
+ uv = crumbling_uv(in.uv, in.wrap, globals.crumbling_sprite.zw, textureDimensions(atlas));
}
var color = textureSample(atlas, atlas_sampler, uv) * select(in.back_color, in.color, front);
if in.overlay == 9u || in.overlay == 10u {
diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs
index 00576ea02..7f3565067 100644
--- a/crates/horizon-client/src/entity_nbt.rs
+++ b/crates/horizon-client/src/entity_nbt.rs
@@ -6,7 +6,7 @@
//! survive a Horizon save.
use crate::{
arrows::Pickup,
- entities::{ArmorStand, ExperienceOrb, ItemEntity, ItemFrame, Painting},
+ entities::{ArmorStand, Cushion, ExperienceOrb, ItemEntity, ItemFrame, Painting},
mobs::{Mob, MobType, Who},
projectiles::{Projectile, ProjectileKind},
save::{
@@ -1313,6 +1313,79 @@ pub fn painting_from_nbt(tag: &Tag) -> Option<([i32; 3], u8, String)> {
let facing = (number(tag.get("facing")).unwrap_or(0) & 3) as u8;
Some((pos, facing, text(tag.get("variant"))?))
}
+/// Cushion.addAdditionalSaveData: the block it sits on and its colour.
+pub fn cushion_to_nbt(c: &Cushion, uuid: [i32; 4], raw: Option<&Tag>) -> Tag {
+ let base = Base {
+ id: "minecraft:cushion",
+ uuid,
+ position: c.position,
+ motion: Vec3::ZERO,
+ rotation: [c.yaw, 0.0],
+ on_ground: false,
+ fall_distance: 0.0,
+ };
+ let mut tag = base.tag(raw);
+ put(&mut tag, "block_pos", Tag::IntArray(c.pos.to_vec()));
+ put(&mut tag, "color", string(c.color_name()));
+ tag
+}
+/// The position, block position, yaw and colour of a saved cushion.
+pub fn cushion_from_nbt(tag: &Tag, id: u32) -> Option {
+ let position = position(tag)?;
+ let pos = match tag.get("block_pos") {
+ Some(Tag::IntArray(v)) if v.len() == 3 => [v[0], v[1], v[2]],
+ _ => [
+ position.x.floor() as i32,
+ position.y.floor() as i32,
+ position.z.floor() as i32,
+ ],
+ };
+ let yaw = match tag.get("Rotation") {
+ Some(Tag::List(l)) => l
+ .first()
+ .and_then(|v| crate::save::float(Some(v)))
+ .unwrap_or(0.0) as f32,
+ _ => 0.0,
+ };
+ let color =
+ crate::entities::dye_index(&text(tag.get("color")).unwrap_or_else(|| "white".into()));
+ Some(Cushion {
+ id,
+ position,
+ pos,
+ yaw,
+ color,
+ check_interval: 0,
+ })
+}
+
+#[cfg(test)]
+mod cushion_tests {
+ use super::*;
+
+ #[test]
+ fn cushion_save_preserves_color_anchor_rotation_and_unknown_data() {
+ let cushion = Cushion {
+ id: 7,
+ position: Vec3::new(-1.5, 64.5, 3.5),
+ pos: [-2, 64, 3],
+ yaw: 270.0,
+ color: 14,
+ check_interval: 55,
+ };
+ let raw = Tag::compound([("CustomName", string("Seat"))]);
+ let tag = cushion_to_nbt(&cushion, [1, 2, 3, 4], Some(&raw));
+ assert_eq!(text(tag.get("CustomName")).as_deref(), Some("Seat"));
+ let loaded = cushion_from_nbt(&tag, 8).unwrap();
+ assert_eq!(loaded.id, 8);
+ assert_eq!(loaded.position, cushion.position);
+ assert_eq!(loaded.pos, cushion.pos);
+ assert_eq!(loaded.yaw, cushion.yaw);
+ assert_eq!(loaded.color_name(), "red");
+ assert_eq!(loaded.check_interval, 0);
+ assert_eq!(loaded.bounds(), cushion.bounds());
+ }
+}
/// ItemFrame.addAdditionalSaveData.
pub fn item_frame_to_nbt(f: &ItemFrame, uuid: [i32; 4], raw: Option<&Tag>) -> Tag {
let (lo, hi) = f.bounds();
diff --git a/crates/horizon-client/src/font.rs b/crates/horizon-client/src/font.rs
index 1903cab7e..d0e8c8499 100644
--- a/crates/horizon-client/src/font.rs
+++ b/crates/horizon-client/src/font.rs
@@ -1201,6 +1201,31 @@ mod tests {
assert!(!font.take_patches().is_empty());
}
+ /// A renderer rebuilt over the same font (the loading screen's handoff)
+ /// makes its atlas texture from the textures alone, so the glyphs it had
+ /// rasterized into the previous texture must be placed into the new one.
+ #[test]
+ fn a_rebuilt_atlas_gets_its_glyphs_again() {
+ let sheet = png(16, 8, |x, _| x < 2 || (8..14).contains(&x));
+ let default = br#"{"providers":[{"type":"bitmap","file":"minecraft:font/a.png","ascent":7,"chars":["ab"]}]}"#;
+ let root = root(
+ "rebuild",
+ &[
+ ("assets/minecraft/font/default.json", default),
+ ("assets/minecraft/textures/font/a.png", &sheet),
+ ],
+ );
+ let font = load(&root);
+ let _ = std::fs::remove_dir_all(&root);
+ let first = font.glyph('a');
+ assert_eq!(font.take_patches().len(), 1);
+ font.reset_cache();
+ // Nothing is left over: drawing the character again places its pixels.
+ assert!(font.take_patches().is_empty());
+ assert_eq!(font.glyph('a').advance, first.advance);
+ assert_eq!(font.take_patches().len(), 1);
+ }
+
#[test]
fn provider_filters_follow_the_font_options() {
let sheet = png(8, 8, |x, _| x < 4);
diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs
index cdbd72d7b..a28cf102a 100644
--- a/crates/horizon-client/src/game.rs
+++ b/crates/horizon-client/src/game.rs
@@ -18,6 +18,7 @@ mod boats;
mod books;
mod bossbars;
mod breezes;
+mod campfire;
mod cartography;
mod chat;
mod client_ambient;
@@ -33,6 +34,7 @@ mod copper_golems;
mod creaking;
mod creatures;
mod crossbow;
+pub(crate) mod cushions;
mod debug_keys;
mod decorated_pot;
mod dedicated;
@@ -80,6 +82,7 @@ mod lan_mobs;
mod lan_mount_menu;
mod lan_particles;
mod lan_pvp;
+mod lan_quiet;
mod lan_screen;
mod lan_stations;
mod lan_trading;
@@ -147,6 +150,7 @@ mod scoreboard;
mod screens;
mod sculk;
mod sculk_fixture;
+mod server_mods;
mod server_packs;
mod server_travel;
mod shader_packs;
@@ -1266,6 +1270,13 @@ struct Game<'a> {
/// Controllers (Horizon's own).
pad: pad::Pads,
frames: u64,
+ /// `item_level` for one frame, game time, day time and dimension.
+ item_level_cache: std::cell::RefCell<
+ Option<(
+ (u64, i64, i64, &'static str),
+ horizon_assets::item_model::LevelState,
+ )>,
+ >,
/// Frames drawn with the player in the world, for `--world-frames`.
world_frames: u64,
fps: f32,
@@ -1429,6 +1440,8 @@ struct Game<'a> {
map_frame_model: frames::FrameModel,
glow_map_frame_model: frames::FrameModel,
armor_stands: Vec,
+ /// `Cushion` decoration entities (game/cushions.rs).
+ cushions: Vec,
/// Entities of types Horizon does not simulate.
others: Vec,
armor_stand_assets: armor_stands::ArmorStandAssets,
@@ -1444,6 +1457,8 @@ struct Game<'a> {
boat_models: boats::BoatModels,
/// EndCrystalModel's baked layer.
crystal_model: Option,
+ /// CushionModel's baked layer.
+ cushion_model: Option,
carts: Vec,
/// Songs playing from jukeboxes (tracked sound handles).
jukebox_sounds: HashMap<(i32, i32, i32), u64>,
@@ -1580,6 +1595,8 @@ struct Game<'a> {
/// position) for the guests.
lan_block_events: Vec<([i32; 3], i32, i32, u32)>,
lan_entity_events: Vec<(i32, i8, [f64; 3])>,
+ /// Successful crits (attacking guest, hit entity), queued while the hub is taken.
+ lan_critical_hits: Vec<(Option, i32)>,
/// Global level events for every guest (`ServerLevel.globalLevelEvent`).
lan_global_events: Vec<(i32, [i32; 3], i32)>,
/// `ContainerOpenersCounter`: how many players (the host and the guests)
@@ -2785,6 +2802,9 @@ impl Game<'_> {
pos,
data,
} => self.consume_teleport_particles([pos.x, pos.y, pos.z], data),
+ LevelEvent::LevelEvent { id: 2003, pos, .. } => {
+ self.eye_break_particles([pos.x, pos.y, pos.z]);
+ }
LevelEvent::LevelEvent {
id: 2013,
pos,
@@ -3333,9 +3353,17 @@ impl Game<'_> {
self.with_player_actor(|game| game.break_block_as_player(pos))
}
fn break_block_as_player(&mut self, pos: [i32; 3]) -> Result<()> {
+ if !self
+ .res
+ .items()
+ .can_destroy_block(self.held(), self.creative)
+ {
+ return Ok(());
+ }
self.pad_rumble(horizon_client::gamepad::Rumble::BlockBroken);
// ServerPlayerGameMode.destroyBlock.
let state = self.world.block(pos[0], pos[1], pos[2]);
+ let broken = state;
let facts = self.res.facts().get(state).clone();
let state = self.crack_mined_pot(pos, state);
// Block.playerWillDestroy: mining their gold angers all nearby piglins.
@@ -3367,6 +3395,9 @@ impl Game<'_> {
creative,
)
});
+ if removed {
+ self.mod_block_event(pos, broken, false);
+ }
if removed && !creative {
let held = self.held().cloned();
self.mine_block_with_held(&facts.name, facts.hardness);
@@ -3475,6 +3506,7 @@ impl Game<'_> {
fn interact_entity(&mut self) -> Result {
Ok(self.interact_frame()
|| self.interact_armor_stand()
+ || self.interact_cushion()
|| self.interact_boat()
|| self.interact_cart()
|| self.interact_mob()?
@@ -3661,6 +3693,7 @@ impl Game<'_> {
|| self.place_painting(hit, &stack)
|| self.place_item_frame(hit, &stack)
|| self.place_armor_stand(hit, &stack)
+ || self.place_cushion(hit, &stack)
|| self.place_end_crystal(hit, &stack))
{
return Ok(true);
@@ -3709,6 +3742,7 @@ impl Game<'_> {
return Ok(true);
};
let target = [pos.x, pos.y, pos.z];
+ self.mod_block_event(target, state, true);
if let Some(entity) = BlockEntity::for_block(&self.res.facts().get(state).name) {
let key = (target[0], target[1], target[2]);
self.world.block_entities.insert(key, entity);
@@ -3951,7 +3985,12 @@ impl Game<'_> {
return Ok(());
};
// GameType.isBlockPlacingRestricted: adventure and spectator.
- if !self.may_build() {
+ if !self.may_build()
+ || !self
+ .res
+ .items()
+ .can_destroy_block(self.held(), self.creative)
+ {
self.mining = None;
return Ok(());
}
@@ -4242,6 +4281,7 @@ impl Game<'_> {
self.update_totem_ready();
self.tick_command_extras();
self.tick_mods();
+ self.tick_server_mods();
let camera_eye = self.camera_eye_height();
let stick = self.pad_move();
if self.riding.is_some() && self.player.is_some() {
@@ -4544,6 +4584,7 @@ impl Game<'_> {
horizon_client::spans::time("tick_paintings", || self.tick_paintings());
horizon_client::spans::time("tick_item_frames", || self.tick_item_frames());
horizon_client::spans::time("tick_armor_stands", || self.tick_armor_stands());
+ horizon_client::spans::time("tick_cushions", || self.tick_cushions());
horizon_client::spans::time("tick_boats", || self.tick_boats());
horizon_client::spans::time("tick_carts", || self.tick_carts());
horizon_client::spans::time("tick_thunder", || self.tick_thunder());
@@ -4752,7 +4793,10 @@ impl Game<'_> {
if capture && !self.focused {
return;
}
- if !capture {
+ if capture {
+ // The inventory's screens are behind us: no drag survives them.
+ self.reset_inventory_clicks();
+ } else {
self.stop_mining();
}
// The real pointer takes over from an agent's.
@@ -5278,7 +5322,8 @@ impl Game<'_> {
| BlockEntity::Vault(_)
| BlockEntity::Beehive(_)
| BlockEntity::CommandBlock(_)
- | BlockEntity::Skull(_),
+ | BlockEntity::Skull(_)
+ | BlockEntity::Campfire(_),
) => {}
None => {
v.extend((0..self.craft_size * self.craft_size).map(|i| 100 + i));
@@ -5548,7 +5593,8 @@ impl Game<'_> {
| BlockEntity::Vault(_)
| BlockEntity::Beehive(_)
| BlockEntity::CommandBlock(_)
- | BlockEntity::Skull(_),
+ | BlockEntity::Skull(_)
+ | BlockEntity::Campfire(_),
) => {}
None if self.mount_active() => self.draw_mount_background(batch, ox, oy),
None => {
@@ -5591,7 +5637,18 @@ impl Game<'_> {
self.draw_inventory_effects(batch, (ox, oy), 176);
}
}
+ /// `AbstractContainerScreen` keeps `isQuickCrafting` and the drag it
+ /// belongs to on the screen; Horizon keeps them on the game, so they are
+ /// dropped when a screen closes. A press whose release never reaches the
+ /// screen (the pointer left the window, or the screen closed first) would
+ /// otherwise leave every later press on every later screen swallowed.
+ pub(super) fn reset_inventory_clicks(&mut self) {
+ self.inv_clicks = Default::default();
+ self.inv_drag = None;
+ self.last_inv_click = None;
+ }
pub(super) fn close_container(&mut self) {
+ self.reset_inventory_clicks();
self.close_station();
self.mount_closed();
self.recipe_ui.reset_screen();
@@ -5687,8 +5744,9 @@ impl Game<'_> {
if self.creative_screen.tab().search {
// CreativeModeInventoryScreen.slotClicked: moveCursorToEnd, then
// setHighlightPos(0), so typing replaces the whole query.
- let search = &self.creative_screen.search;
- self.text_selection.select(("creative.search", 0), search);
+ let search = &mut self.creative_screen.search;
+ search.move_cursor_to_end(false);
+ search.highlight = 0;
}
let items = self.creative_screen.items(&self.pack.names);
let first = self.creative_screen.first_row(&self.pack.names);
@@ -6330,6 +6388,13 @@ impl Game<'_> {
false,
);
if outside {
+ // AbstractRecipeBookScreen.mouseClicked: the book takes the click
+ // before the outside test, and its tabs sit left of the strip
+ // hasClickedOutside counts as the book.
+ if self.recipe_book_click(right) {
+ self.inv_clicks.skip_release();
+ return;
+ }
// slotId -999: an empty hand throws nothing; a carried stack
// starts a drag whose release drops it.
match input {
@@ -6895,6 +6960,9 @@ impl Game<'_> {
self.prepare_entity_culling(camera);
self.prepare_trims();
self.mark_entity(out.len(), "minecraft:entity_flame");
+ // One scan for both passes that draw them (block entities below and the
+ // overlay near the end of this function).
+ let cracks = self.cracks(camera);
let fire_yaw = if self.camera_type == perspective::CameraType::ThirdPersonFront {
self.yaw + 180.0
} else {
@@ -6991,6 +7059,9 @@ impl Game<'_> {
// `VaultRenderer`.
self.mark_entity(out.len(), "");
self.vault_geometry(camera, alpha, &mut out);
+ // `CampfireRenderer`.
+ self.mark_entity(out.len(), "");
+ self.campfire_geometry(camera, &mut out);
self.mark_entity(out.len(), "minecraft:ominous_item_spawner");
self.ominous_item_spawner_geometry(camera, alpha, &mut out);
// Mobs and their projectiles (a span each).
@@ -7120,7 +7191,7 @@ impl Game<'_> {
.render(&scene, visible, &mut out);
}
self.mark_stage(out.len(), "DESTROY");
- for (pos, stage) in self.cracks(camera) {
+ for &(pos, stage) in &cracks {
let state = self.world.block(pos[0], pos[1], pos[2]);
self.res
.block_entity_render()
@@ -7233,6 +7304,7 @@ impl Game<'_> {
self.frame_geometry(camera, &mut out);
self.mark_entity(out.len(), "minecraft:armor_stand");
self.armor_stand_geometry(camera, alpha, &mut out);
+ self.cushion_geometry(camera, &mut out);
self.mark_entity(out.len(), "minecraft:oak_boat");
self.boat_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:minecart");
@@ -7263,7 +7335,7 @@ impl Game<'_> {
);
// Breaking progress overlays (destroy_stage_0..9): ours and the other players'.
self.mark_stage(out.len(), "DESTROY");
- for (pos, stage) in self.cracks(camera) {
+ for &(pos, stage) in &cracks {
let o = (glam::DVec3::new(f64::from(pos[0]), f64::from(pos[1]), f64::from(pos[2]))
- camera)
.as_vec3();
@@ -7311,6 +7383,20 @@ impl Game<'_> {
/// other players' (`renderBlockDestroyAnimation`: within 32 blocks of the camera, the
/// furthest progress on each block).
fn cracks(&self, camera: glam::DVec3) -> Vec<([i32; 3], [f32; 2])> {
+ // `destroy_stage_0` .. `destroy_stage_9`: the sprite name by stage, so the
+ // lookup does not format a string per crack.
+ const DESTROY_STAGES: [&str; 10] = [
+ "block/destroy_stage_0",
+ "block/destroy_stage_1",
+ "block/destroy_stage_2",
+ "block/destroy_stage_3",
+ "block/destroy_stage_4",
+ "block/destroy_stage_5",
+ "block/destroy_stage_6",
+ "block/destroy_stage_7",
+ "block/destroy_stage_8",
+ "block/destroy_stage_9",
+ ];
let mut stages: Vec<([i32; 3], u32)> = Vec::new();
let mut add = |pos: [i32; 3], stage: u32| match stages.iter_mut().find(|c| c.0 == pos) {
Some(c) => c.1 = c.1.max(stage),
@@ -7336,11 +7422,8 @@ impl Game<'_> {
d.length_squared() <= 1024.0
})
.filter_map(|(pos, stage)| {
- let r = self
- .gui
- .atlas
- .sprites
- .get(&format!("block/destroy_stage_{stage}"))?;
+ let name = DESTROY_STAGES.get(stage as usize)?;
+ let r = self.gui.atlas.sprites.get(*name)?;
Some((pos, [r.u0, r.v0]))
})
.collect()
@@ -7518,6 +7601,7 @@ impl Game<'_> {
.map_or(0.0, |i| i.attack_damage);
if self.attack_frame()
|| self.attack_armor_stand()
+ || self.attack_cushion()
|| self.attack_boat(hand_damage)
|| self.attack_cart(hand_damage)
|| self.attack_dragon()
@@ -7605,10 +7689,9 @@ impl Game<'_> {
self.stats
.custom("damage_dealt", (actual * 10.0).round() as i32);
if crit {
- self.particles.tracking_emitter(
- horizon_client::particles::ParticleOptions::simple(
- horizon_client::particles::ParticleType::Crit,
- ),
+ self.critical_hit(
+ self.lan_acting,
+ self.mobs[index].id as i32,
feet,
width,
height,
@@ -7990,6 +8073,7 @@ impl Game<'_> {
/// (scaled coordinates, or the End portal's fixed destination).
pub(super) fn switch_level(&mut self, target: Dim, from: Vec3) -> Result {
// Save the departing dimension's changed chunks.
+ self.save.save_forced_chunks(&self.streaming.forced)?;
self.streaming.save(&mut self.world, true);
let keys: Vec<_> = self.world.modified.drain().collect();
let chunks = self.with_entities(keys.iter().filter_map(|k| self.world.chunks.get(k)));
@@ -8263,6 +8347,7 @@ impl Game<'_> {
self.boats.clear();
self.carts.clear();
self.armor_stands.clear();
+ self.cushions.clear();
self.item_frames.clear();
self.paintings.clear();
self.others.clear();
@@ -8681,6 +8766,13 @@ impl Game<'_> {
{
return Ok(UseResult::Success { swing: true });
}
+ if allow_block_actions
+ && let Some(hit) = self.pick()
+ && let Some(placed) = self.use_on_campfire(hit.pos)?
+ {
+ // SUCCESS_SERVER swings; a full campfire's CONSUME does not.
+ return Ok(UseResult::Success { swing: placed });
+ }
// BucketItem has no useOn: a guest's UseItemOn leaves it to UseItem.
let bucket = horizon_client::items::bucket_contents(&stack.item);
if matches!(self.guest_use, Some(GuestUse::OnBlock(_)))
@@ -9731,8 +9823,9 @@ impl Game<'_> {
fn draw_creative(&mut self, batch: &mut HudBatch) {
let (ox, oy) = Self::creative_origin(batch);
let selected = self.creative_screen.selected;
- let tabs = self.creative_screen.tabs.clone();
- let tab = self.creative_screen.tab().clone();
+ // Only what is drawn of the tabs: their stacks stay where they are.
+ let tabs = self.creative_screen.tab_headers();
+ let tab = tabs[selected.min(tabs.len() - 1)].clone();
let tab_sprite = |t: &horizon_client::creative::Tab, on: bool| {
format!(
"gui/sprites/container/creative_inventory/tab_{}_{}_{}",
@@ -9798,20 +9891,16 @@ impl Game<'_> {
);
}
if tab.search {
- // The search box: an unbordered EditBox of 80 at (82, 6), white.
- let search = &self.creative_screen.search;
- horizon_client::ui::append_field(
- batch,
- atlas,
- search,
- (ox + 82.0, oy + 6.0, 80.0),
- (u32::MAX, true),
- clipboard::CREATIVE_SEARCH_CHARS,
- self.text_selection
- .covers(clipboard::CREATIVE_SEARCH, search),
- true,
- self.creative_screen.search_focused.elapsed().as_millis(),
- );
+ // CreativeModeInventoryScreen: an unbordered EditBox of 50 at (82, 6).
+ let search = &mut self.creative_screen.search;
+ search.x = ox + 82.0;
+ search.y = oy + 6.0;
+ search.width = 80.0;
+ search.bordered = false;
+ search.focused = true;
+ search.text_color = u32::MAX;
+ search.max_length = clipboard::CREATIVE_SEARCH_CHARS;
+ search.draw(batch, atlas, None);
}
let mut hovered: Option = None;
if tab.inventory {
@@ -9919,8 +10008,7 @@ impl Game<'_> {
} else if self.carried.is_none()
&& let Some(item) = hovered
{
- let lines = self.stack_tooltip(&item);
- self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1);
+ self.draw_stack_tooltip(batch, &item, self.cursor.0, self.cursor.1);
}
self.draw_inventory_effects(batch, (ox, oy), Creative::WIDTH as i32);
}
@@ -9973,6 +10061,9 @@ impl Game<'_> {
}
return;
}
+ if !right && self.creative_search_click(ox, oy) {
+ return;
+ }
// Holding the scrollbar scrolls as the mouse moves (mouseDragged).
if !right
&& self
@@ -9985,6 +10076,24 @@ impl Game<'_> {
self.inventory_press(right);
}
}
+ /// A click on the search tab's box (`EditBox.onClick`).
+ fn creative_search_click(&mut self, ox: f32, oy: f32) -> bool {
+ if !self.creative_screen.tab().search {
+ return false;
+ }
+ let (x, y, w, h) = (ox + 82.0, oy + 6.0, 80.0, 9.0);
+ let (mx, my) = self.cursor;
+ if mx < x || my < y || mx >= x + w || my >= y + h {
+ return false;
+ }
+ let shift = self.has_shift_down();
+ let search = &mut self.creative_screen.search;
+ search.x = x;
+ search.y = y;
+ search.width = w;
+ search.click(&self.gui.atlas, mx, shift, false);
+ true
+ }
/// Copy the live player and world state into the `Level` that gets saved.
pub(super) fn sync_level(&mut self) {
// Level.write saves the game rules (game_rules.dat) with the rest.
@@ -10104,6 +10213,7 @@ impl Game<'_> {
}
/// Save the level in place: its changed chunks, its entities and its raids.
fn save_level(&mut self, wait: bool) -> Result<()> {
+ self.save.save_forced_chunks(&self.streaming.forced)?;
self.streaming.save(&mut self.world, wait);
let keys: Vec<_> = self.world.modified.drain().collect();
let chunks = self.with_entities(keys.iter().filter_map(|k| self.world.chunks.get(k)));
@@ -10303,8 +10413,6 @@ impl Game<'_> {
Vec::new()
};
let mut hud = horizon_client::spans::time("hud", || self.hud(size));
- // Map textures changed since the last drawn frame (game/maps.rs).
- hud.patches.append(&mut self.map_patches);
// LevelLoadingScreen (Reason.OTHER) draws the title's panorama.
let title = self.title.is_some()
|| self.loading_reason() == Some(horizon_client::level_loading::Reason::Other);
@@ -10406,6 +10514,10 @@ impl Game<'_> {
} else {
self.shader_pack_inputs(camera, alpha as f32)
};
+ // Map textures and font glyphs changed while building this frame's
+ // geometry (game/maps.rs, world text) upload with this batch; draining
+ // map_patches any earlier leaves them a frame behind.
+ hud.patches.append(&mut self.map_patches);
let entity_spans = std::mem::take(self.entity_spans.get_mut());
let frame = Frame {
weather: &weather,
@@ -11021,11 +11133,7 @@ impl Game<'_> {
bytes += size;
}
}
- for c in unloaded {
- for sy in 0..(HEIGHT / 16) {
- renderer.remove((c.x, sy, c.z));
- }
- }
+ renderer.remove_columns(unloaded.iter().map(|c| (c.x, c.z)));
if let (Some(lod), Some(player)) = (&mut self.lod, &self.player) {
let lod_distance = self.settings.horizon.lod_distance_on(&renderer.info);
lod.set_near_loading(self.streaming.busy());
@@ -11632,8 +11740,17 @@ impl ApplicationHandler for Game<'_> {
// The hovered slot took the key (`ignoreTextInput`).
} else if let Some(shortcut) = self.edit_shortcut(&event) {
self.creative_search_shortcut(shortcut);
- } else if event.physical_key == PhysicalKey::Code(KeyCode::Backspace) {
- self.creative_search_backspace(self.has_control_down());
+ } else if let PhysicalKey::Code(key) = event.physical_key {
+ // EditBox.keyPressed: arrows, Home, End, Delete and Backspace.
+ let before = self.creative_screen.search.value.clone();
+ let (ctrl, shift) = (self.has_control_down(), self.has_shift_down());
+ if self.creative_screen.search.key(key, ctrl, shift) {
+ if self.creative_screen.search.value != before {
+ self.creative_screen.scroll = 0.0;
+ }
+ } else {
+ self.creative_search_edit(event.text.as_deref());
+ }
} else {
self.creative_search_edit(event.text.as_deref());
}
@@ -11654,6 +11771,9 @@ impl ApplicationHandler for Game<'_> {
// The anvil rename box takes every key but Escape (AnvilScreen.keyPressed).
if let Some(shortcut) = self.edit_shortcut(&event) {
self.station_shortcut(shortcut);
+ } else if let PhysicalKey::Code(key) = event.physical_key
+ && self.station_key(key)
+ {
} else {
let backspace = event.physical_key == PhysicalKey::Code(KeyCode::Backspace);
self.station_text(event.text.as_deref(), backspace);
@@ -12166,6 +12286,7 @@ impl<'a> Game<'a> {
last_input: now,
pad: Default::default(),
frames: 0,
+ item_level_cache: Default::default(),
world_frames: 0,
fps: 0.0,
fps_frames: 0,
@@ -12270,6 +12391,7 @@ impl<'a> Game<'a> {
map_frame_model: prepared.map_frame_model,
glow_map_frame_model: prepared.glow_map_frame_model,
armor_stands: Vec::new(),
+ cushions: Vec::new(),
others: Vec::new(),
armor_stand_assets: prepared.armor_stand_assets,
waypoint_styles: prepared.waypoint_styles,
@@ -12279,6 +12401,7 @@ impl<'a> Game<'a> {
boats: Vec::new(),
boat_models: prepared.boat_models,
crystal_model: prepared.crystal_model,
+ cushion_model: prepared.cushion_model,
carts: Vec::new(),
jukebox_sounds: HashMap::new(),
bolts: Vec::new(),
@@ -12365,6 +12488,7 @@ impl<'a> Game<'a> {
lan_events: Vec::new(),
lan_block_events: Vec::new(),
lan_entity_events: Vec::new(),
+ lan_critical_hits: Vec::new(),
lan_global_events: Vec::new(),
lid_openers: HashMap::new(),
lan_acting: None,
@@ -12416,6 +12540,7 @@ struct GameCpuResources {
boat_models: boats::BoatModels,
cart_models: minecarts::CartModels,
crystal_model: Option,
+ cushion_model: Option,
dragon_model: Option,
arrow_model: Option,
trident_model: Option,
@@ -12436,6 +12561,7 @@ impl GameCpuResources {
boat_models: boats::BoatModels::load(document.as_ref()),
cart_models: minecarts::CartModels::load(document.as_ref()),
crystal_model: model("minecraft:end_crystal#main"),
+ cushion_model: model("minecraft:cushion#main"),
dragon_model: model("minecraft:ender_dragon#main"),
arrow_model: model("minecraft:arrow#main"),
trident_model: model("minecraft:trident#main"),
@@ -12995,6 +13121,14 @@ fn build_renderer(
) -> Result {
let assets = &pack.render;
let atlas = &gui.atlas;
+ // The new renderer's atlas texture starts from `HudAtlas.pixels`, which
+ // holds the textures but never a glyph rasterized at run time. The font's
+ // cache still answers with the rects it drew into the previous renderer's
+ // texture, so forget them: the glyphs that are drawn again are placed into
+ // this texture (as `ui_theme` does when its atlas changes). Without this,
+ // Chinese and Japanese menu labels go blank when the loading screen hands
+ // over to the game.
+ atlas.font.reset_cache();
let mut renderer = pollster::block_on(Renderer::new(
window,
offscreen,
@@ -13041,6 +13175,12 @@ fn loading_renderer(
Some(atlas) => (atlas.pixels.as_slice(), atlas.width, atlas.height),
None => (EMPTY_HUD.as_slice(), 1, 1),
};
+ // A glyph rasterized into an earlier texture is not in these pixels; the
+ // new texture gets it again when the character is drawn (see
+ // `build_renderer`).
+ if let Some(atlas) = atlas {
+ atlas.font.reset_cache();
+ }
let mut renderer = pollster::block_on(Renderer::new(
window,
offscreen,
@@ -13777,7 +13917,7 @@ mod startup_cpu_tests {
r#"{
"minecraft:end_crystal#main": {}, "minecraft:ender_dragon#main": {},
"minecraft:arrow#main": {}, "minecraft:trident#main": {},
- "minecraft:armor_stand#main": {}
+ "minecraft:armor_stand#main": {}, "minecraft:cushion#main": {}
}"#,
)
.unwrap();
@@ -13797,6 +13937,7 @@ mod startup_cpu_tests {
.unwrap();
std::fs::remove_dir_all(&root).unwrap();
assert!(prepared.crystal_model.is_some());
+ assert!(prepared.cushion_model.is_some());
assert!(prepared.dragon_model.is_some());
assert!(prepared.arrow_model.is_some());
assert!(prepared.trident_model.is_some());
diff --git a/crates/horizon-client/src/game/beacon.rs b/crates/horizon-client/src/game/beacon.rs
index 24ead3a3d..97177f2b9 100644
--- a/crates/horizon-client/src/game/beacon.rs
+++ b/crates/horizon-client/src/game/beacon.rs
@@ -515,8 +515,7 @@ impl Game<'_> {
if self.carried.is_some() {
self.draw_carried(batch);
} else if let Some(stack) = stack_tip {
- let lines = self.stack_tooltip(&stack);
- self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1);
+ self.draw_stack_tooltip(batch, &stack, self.cursor.0, self.cursor.1);
} else if let Some(key) = tooltip {
let text = self.translate(&key, &key);
self.draw_tooltip_lines(
diff --git a/crates/horizon-client/src/game/block_entity_ticks.rs b/crates/horizon-client/src/game/block_entity_ticks.rs
index e48c8f398..e0798b4cb 100644
--- a/crates/horizon-client/src/game/block_entity_ticks.rs
+++ b/crates/horizon-client/src/game/block_entity_ticks.rs
@@ -56,6 +56,7 @@ impl Game<'_> {
Some(BlockEntity::Beehive(_)) => self.tick_beehive(pos),
Some(BlockEntity::Furnace(_)) => self.tick_furnace(pos)?,
Some(BlockEntity::Brewing(_)) => self.tick_brewing_stand(pos)?,
+ Some(BlockEntity::Campfire(_)) => self.tick_campfire(pos)?,
_ => {}
}
}
@@ -92,7 +93,10 @@ impl Game<'_> {
| "minecraft:vault"
| "minecraft:beehive"
| "minecraft:bee_nest"
- | "minecraft:brewing_stand" => Ticking::Yes,
+ | "minecraft:brewing_stand"
+ // Lit, cookTick; unlit, cooldownTick.
+ | "minecraft:campfire"
+ | "minecraft:soul_campfire" => Ticking::Yes,
"minecraft:jukebox" => {
let record = registry
.state(state)
diff --git a/crates/horizon-client/src/game/boats.rs b/crates/horizon-client/src/game/boats.rs
index df7158d9a..06dfab22d 100644
--- a/crates/horizon-client/src/game/boats.rs
+++ b/crates/horizon-client/src/game/boats.rs
@@ -55,7 +55,7 @@ pub(super) struct Boat {
/// `ChestBoat.CONTAINER_SIZE`.
pub(super) const CHEST_SLOTS: usize = 27;
pub(super) const WIDTH: f64 = 1.375;
-const HEIGHT: f64 = 0.5625;
+pub(super) const HEIGHT: f64 = 0.5625;
impl Boat {
pub fn new(id: u32, wood: Arc, position: Vec3, yaw: f32) -> Self {
Self {
@@ -587,6 +587,9 @@ impl Game<'_> {
}
/// The player's tick while riding: steer, tick the boat, sit in it.
pub(super) fn tick_riding(&mut self, keys: Keys) -> horizon_client::Result<()> {
+ if self.tick_cushion_riding(keys) {
+ return Ok(());
+ }
let Some(i) = self.boats.iter().position(|b| Some(b.id) == self.riding) else {
if self.tick_cart_riding(keys) || self.tick_mount_riding(keys) {
return Ok(());
diff --git a/crates/horizon-client/src/game/books.rs b/crates/horizon-client/src/game/books.rs
index 9af1da70c..2f38dffd9 100644
--- a/crates/horizon-client/src/game/books.rs
+++ b/crates/horizon-client/src/game/books.rs
@@ -17,7 +17,6 @@ use horizon_client::{
use horizon_core::position::BlockPos;
use horizon_protocol::packets::play::{self, Clientbound as C, Serverbound as S};
use horizon_world::level::flags as level_flags;
-use std::time::Instant;
use winit::{
event::{ElementState, KeyEvent},
keyboard::{KeyCode, PhysicalKey},
@@ -27,8 +26,24 @@ const BLACK: u32 = 0xFF00_0000;
const PAGE_CHARS: usize = 1024;
const MAX_PAGES: usize = 100;
const LINES: usize = 14;
-/// `BookSignScreen`'s title box.
-const BOOK_TITLE: horizon_client::clipboard::FieldId = ("book.title", 0);
+
+fn book_title_box() -> ui::EditBox {
+ let mut edit = ui::EditBox::new(0.0, 0.0, 114.0, 9.0);
+ edit.bordered = false;
+ edit.max_length = 15;
+ edit.text_color = BLACK;
+ edit.focused = true;
+ edit
+}
+fn book_page_box() -> ui::EditBox {
+ let mut edit = ui::EditBox::new(0.0, 0.0, 114.0, 9.0);
+ edit.bordered = false;
+ edit.multiline = true;
+ edit.max_length = PAGE_CHARS;
+ edit.text_color = BLACK;
+ edit.focused = true;
+ edit
+}
pub(super) enum BookScreen {
Edit {
@@ -38,7 +53,10 @@ pub(super) enum BookScreen {
page: usize,
signing: bool,
title: String,
- opened: Instant,
+ /// BookSignScreen's title box (`EditBox` of 15).
+ title_edit: ui::EditBox,
+ /// BookEditScreen's page (`MultiLineEditBox`).
+ page_edit: ui::EditBox,
},
View {
pages: Vec,
@@ -84,13 +102,16 @@ impl Game<'_> {
if pages.is_empty() {
pages.push(String::new());
}
+ let mut page_edit = book_page_box();
+ page_edit.set_value(&pages[0]);
BookScreen::Edit {
slot: self.held_slot(),
pages,
page: 0,
signing: false,
title: String::new(),
- opened: Instant::now(),
+ title_edit: book_title_box(),
+ page_edit,
}
}
"minecraft:written_book" => BookScreen::View {
@@ -239,34 +260,24 @@ impl Game<'_> {
Some(KeyCode::PageDown) => self.turn_page(true),
_ => {}
},
- Some(BookScreen::Edit {
- signing: true,
- title,
- ..
- }) => match key {
+ Some(BookScreen::Edit { signing: true, .. }) => match key {
Some(KeyCode::Escape) => {
if let Some(BookScreen::Edit { signing, .. }) = self.book.as_mut() {
*signing = false;
}
}
- Some(KeyCode::Backspace) => {
- if !self.text_selection.take(BOOK_TITLE, title) {
- title.pop();
- }
- }
Some(KeyCode::Enter | KeyCode::NumpadEnter) => {
- if !title.trim().is_empty() {
+ let blank = self.book.as_ref().is_some_and(
+ |b| matches!(b, BookScreen::Edit { title, .. } if title.trim().is_empty()),
+ );
+ if !blank {
self.save_book(true);
}
}
+ Some(key) if self.book_title_key(key) => {}
_ => {
if let Some(typed) = text {
- self.text_selection.take(BOOK_TITLE, title);
- for c in typed.chars().filter(|c| !c.is_control()) {
- if title.chars().count() < 15 {
- title.push(c);
- }
- }
+ self.book_title_insert(typed);
}
}
},
@@ -274,24 +285,13 @@ impl Game<'_> {
Some(KeyCode::Escape) => self.save_book(false),
Some(KeyCode::PageUp) => self.turn_page(false),
Some(KeyCode::PageDown) => self.turn_page(true),
- Some(KeyCode::Backspace) => {
- if !self.take_page_selection() {
- self.edit_page(|p| {
- p.pop();
- })
- }
- }
- Some(KeyCode::Enter | KeyCode::NumpadEnter) => {
- self.take_page_selection();
- self.type_into_page("\n")
- }
+ Some(KeyCode::ArrowUp) => self.book_move_line(-1),
+ Some(KeyCode::ArrowDown) => self.book_move_line(1),
+ Some(KeyCode::Enter | KeyCode::NumpadEnter) => self.page_insert("\n"),
+ Some(key) if self.book_page_key(key) => {}
_ => {
if let Some(typed) = text {
- let typed: String = typed.chars().filter(|c| !c.is_control()).collect();
- if !typed.is_empty() {
- self.take_page_selection();
- }
- self.type_into_page(&typed);
+ self.page_insert(typed);
}
}
},
@@ -302,99 +302,258 @@ impl Game<'_> {
pub(super) fn book_type(&mut self, text: &str) {
let text = text.replace("\\n", "\n");
match self.book.as_mut() {
- Some(BookScreen::Edit {
- signing: true,
- title,
- ..
- }) => {
- for c in text.chars().filter(|c| !c.is_control()) {
- if title.chars().count() < 15 {
- title.push(c);
- }
- }
- }
- Some(BookScreen::Edit { .. }) => self.type_into_page(&text),
+ Some(BookScreen::Edit { signing: true, .. }) => self.book_title_insert(&text),
+ Some(BookScreen::Edit { .. }) => self.page_insert(&text),
_ => {}
}
}
- /// Before typing on a page: Select All's selection is cleared (true).
- fn take_page_selection(&mut self) -> bool {
- match self.book.as_mut() {
- Some(BookScreen::Edit { pages, page, .. }) => self
- .text_selection
- .take(("book.page", *page), &mut pages[*page]),
- _ => false,
+ /// `BookSignScreen.keyPressed` on the title box.
+ fn book_title_key(&mut self, key: KeyCode) -> bool {
+ let (ctrl, shift) = (self.has_control_down(), self.has_shift_down());
+ let Some(BookScreen::Edit {
+ title, title_edit, ..
+ }) = self.book.as_mut()
+ else {
+ return false;
+ };
+ if !title_edit.key(key, ctrl, shift) {
+ return false;
}
+ title.clone_from(&title_edit.value);
+ true
}
- /// Ctrl+A/C/V/X: the title box (an `EditBox` of 15) while signing, else
- /// the page (`MultilineTextField.insertText`: line breaks kept, cut to
- /// the 1024 characters left, rejected when it overflows the 14 lines).
- pub(super) fn book_shortcut(&mut self, shortcut: horizon_client::clipboard::EditShortcut) {
- let font = &self.gui.atlas.font;
- match self.book.as_mut() {
- Some(BookScreen::Edit {
- signing: true,
- title,
- ..
- }) => {
- Self::append_shortcut(
- &mut self.text_selection,
- &mut self.clipboard,
- BOOK_TITLE,
- title,
- shortcut,
- 15,
- );
- }
- Some(BookScreen::Edit { pages, page, .. }) => {
- // truncateInsertionText counts the whole value, selection included.
- let room = PAGE_CHARS.saturating_sub(pages[*page].chars().count());
- self.text_selection.apply(
- ("book.page", *page),
- &mut pages[*page],
- shortcut,
- &mut self.clipboard,
- |value, pasted| {
- let text: String = horizon_client::clipboard::filter_text(pasted, true)
- .chars()
- .take(room)
- .collect();
- let next = format!("{value}{text}");
- font.split(&Text::literal(&next), 114.0).len() <= LINES && {
- *value = next;
- true
- }
- },
- );
- }
- _ => {}
- }
- }
- fn edit_page(&mut self, f: impl FnOnce(&mut String)) {
- if let Some(BookScreen::Edit { pages, page, .. }) = self.book.as_mut() {
- f(&mut pages[*page]);
- }
- }
- /// MultiLineEditBox: characters that keep the page within 14 lines and 1024 characters.
- fn type_into_page(&mut self, typed: &str) {
- let Some(BookScreen::Edit { pages, page, .. }) = self.book.as_ref() else {
+ fn book_title_insert(&mut self, text: &str) {
+ let Some(BookScreen::Edit {
+ title, title_edit, ..
+ }) = self.book.as_mut()
+ else {
return;
};
- let mut text = pages[*page].clone();
- for c in typed.chars() {
- let mut next = text.clone();
- next.push(c);
- if next.chars().count() <= PAGE_CHARS && self.book_lines(&next).len() <= LINES {
- text = next;
+ title_edit.insert_text(text);
+ title.clone_from(&title_edit.value);
+ }
+ /// `BookEditScreen.keyPressed` on the page, apart from the line moves.
+ fn book_page_key(&mut self, key: KeyCode) -> bool {
+ let (ctrl, shift) = (self.has_control_down(), self.has_shift_down());
+ let before = match self.book.as_ref() {
+ Some(BookScreen::Edit { page_edit, .. }) => Some((
+ page_edit.value.clone(),
+ page_edit.cursor,
+ page_edit.highlight,
+ )),
+ _ => return false,
+ };
+ let handled = if let Some(BookScreen::Edit { page_edit, .. }) = self.book.as_mut() {
+ page_edit.key(key, ctrl, shift)
+ } else {
+ false
+ };
+ if handled {
+ self.page_keep(before);
+ }
+ handled
+ }
+ /// One character at a time, so a page stops at the last character that fits.
+ fn page_insert(&mut self, text: &str) {
+ for c in text.chars() {
+ self.page_change(|edit| edit.insert_text(&c.to_string()));
+ }
+ }
+ /// Ctrl+A/C/V/X: the title box while signing, else the page.
+ pub(super) fn book_shortcut(&mut self, shortcut: horizon_client::clipboard::EditShortcut) {
+ let signing = matches!(self.book, Some(BookScreen::Edit { signing: true, .. }));
+ if signing {
+ if let Some(BookScreen::Edit {
+ title, title_edit, ..
+ }) = self.book.as_mut()
+ {
+ title_edit.shortcut(shortcut, &mut self.clipboard);
+ title.clone_from(&title_edit.value);
+ }
+ return;
+ }
+ let before = match self.book.as_ref() {
+ Some(BookScreen::Edit { page_edit, .. }) => Some((
+ page_edit.value.clone(),
+ page_edit.cursor,
+ page_edit.highlight,
+ )),
+ _ => None,
+ };
+ if let Some(BookScreen::Edit { page_edit, .. }) = self.book.as_mut() {
+ page_edit.shortcut(shortcut, &mut self.clipboard);
+ }
+ self.page_keep(before);
+ }
+ /// Edit the page box, then keep it only within 14 lines and 1024 characters.
+ fn page_change(&mut self, f: impl FnOnce(&mut ui::EditBox)) -> bool {
+ let before = match self.book.as_ref() {
+ Some(BookScreen::Edit { page_edit, .. }) => Some((
+ page_edit.value.clone(),
+ page_edit.cursor,
+ page_edit.highlight,
+ )),
+ _ => return false,
+ };
+ let ran = if let Some(BookScreen::Edit { page_edit, .. }) = self.book.as_mut() {
+ f(page_edit);
+ true
+ } else {
+ false
+ };
+ ran && self.page_keep(before)
+ }
+ /// Write the page, or put the previous text back when it no longer fits.
+ fn page_keep(&mut self, before: Option<(String, usize, usize)>) -> bool {
+ let next = match self.book.as_ref() {
+ Some(BookScreen::Edit { page_edit, .. }) => page_edit.value.clone(),
+ _ => return false,
+ };
+ let ok = next.chars().count() <= PAGE_CHARS && self.book_lines(&next).len() <= LINES;
+ let Some(BookScreen::Edit {
+ pages,
+ page,
+ page_edit,
+ ..
+ }) = self.book.as_mut()
+ else {
+ return false;
+ };
+ if ok {
+ pages[*page].clone_from(&page_edit.value);
+ } else if let Some((value, cursor, highlight)) = before {
+ page_edit.value = value;
+ page_edit.cursor = cursor;
+ page_edit.highlight = highlight;
+ }
+ ok
+ }
+ /// ArrowUp and ArrowDown move a visual line, keeping the column.
+ fn book_move_line(&mut self, dir: isize) {
+ let shift = self.has_shift_down();
+ let (text, cursor) = match self.book.as_ref() {
+ Some(BookScreen::Edit {
+ pages,
+ page,
+ page_edit,
+ ..
+ }) => (pages[*page].clone(), page_edit.cursor),
+ _ => return,
+ };
+ let rows = self.page_rows(&text);
+ let (x, line) = self.caret_on(&text, &rows, cursor);
+ let next = (line as isize + dir).clamp(0, rows.len() as isize - 1) as usize;
+ let (start, end) = rows[next];
+ let chars: Vec = text.chars().collect();
+ let slice: String = chars[start..end].iter().collect();
+ let pos = start + self.gui.atlas.font.fit(&slice, x);
+ if let Some(BookScreen::Edit { page_edit, .. }) = self.book.as_mut() {
+ page_edit.set_cursor(pos, shift);
+ }
+ }
+ /// Wrapped rows of a page as character ranges, the same breaks as `font.split`.
+ fn page_rows(&self, page: &str) -> Vec<(usize, usize)> {
+ let font = &self.gui.atlas.font;
+ let chars: Vec = page.chars().collect();
+ let mut rows = Vec::new();
+ let mut start = 0;
+ while start < chars.len() {
+ let mut end = chars.len();
+ let mut next = chars.len();
+ let mut last_space = None;
+ for i in start..chars.len() {
+ if chars[i] == '\n' {
+ end = i;
+ next = i + 1;
+ break;
+ }
+ if chars[i] == ' ' {
+ last_space = Some(i);
+ }
+ let slice: String = chars[start..=i].iter().collect();
+ if font.width(&slice) > 114.0 {
+ match last_space {
+ Some(s) if s > start => {
+ end = s;
+ next = s + 1;
+ }
+ _ => {
+ end = i.max(start + 1);
+ next = end;
+ }
+ }
+ break;
+ }
+ }
+ rows.push((start, end));
+ start = next;
+ if start == chars.len() && chars.last() == Some(&'\n') {
+ rows.push((chars.len(), chars.len()));
}
}
- self.edit_page(|p| *p = text);
+ if rows.is_empty() {
+ rows.push((0, 0));
+ }
+ rows
+ }
+ /// GUI x of the caret and the row it sits on.
+ fn caret_on(&self, page: &str, rows: &[(usize, usize)], cursor: usize) -> (f32, usize) {
+ let chars: Vec = page.chars().collect();
+ for (i, &(start, end)) in rows.iter().enumerate() {
+ if cursor <= end || i + 1 == rows.len() {
+ let take = cursor.saturating_sub(start).min(end.saturating_sub(start));
+ let prefix: String = chars.iter().skip(start).take(take).collect();
+ return (self.gui.atlas.font.width(&prefix), i);
+ }
+ }
+ (0.0, 0)
+ }
+ fn page_index_at(&self, page: &str, local_x: f32, local_y: f32) -> usize {
+ let rows = self.page_rows(page);
+ let line = (local_y / 9.0).floor().max(0.0) as usize;
+ let line = line.min(rows.len().saturating_sub(1));
+ let (start, end) = rows[line];
+ let chars: Vec = page.chars().collect();
+ let slice: String = chars[start..end].iter().collect();
+ start + self.gui.atlas.font.fit(&slice, local_x.max(0.0))
+ }
+ /// A click on the centred title (`EditBox.onClick`).
+ fn book_title_click(&mut self, field_x: f32, mx: f32) {
+ let shift = self.has_shift_down();
+ let Some(BookScreen::Edit {
+ title, title_edit, ..
+ }) = self.book.as_mut()
+ else {
+ return;
+ };
+ let tx = field_x + ((114.0 - self.gui.atlas.font.width(title)) / 2.0).floor();
+ let rel = (mx - tx).max(0.0);
+ let pos = self.gui.atlas.font.fit(title, rel);
+ title_edit.set_cursor(pos, shift);
+ }
+ /// A click on the open page.
+ fn book_page_click(&mut self, origin_x: f32, origin_y: f32, mx: f32, my: f32) {
+ let shift = self.has_shift_down();
+ let text = match self.book.as_ref() {
+ Some(BookScreen::Edit { pages, page, .. }) => pages[*page].clone(),
+ _ => return,
+ };
+ let pos = self.page_index_at(&text, mx - origin_x, my - origin_y);
+ if let Some(BookScreen::Edit { page_edit, .. }) = self.book.as_mut() {
+ page_edit.set_cursor(pos, shift);
+ }
}
/// The page buttons (`pageForward` adds a page while editing).
fn turn_page(&mut self, forward: bool) {
let mut lectern_page = None;
match self.book.as_mut() {
- Some(BookScreen::Edit { pages, page, .. }) => {
+ Some(BookScreen::Edit {
+ pages,
+ page,
+ page_edit,
+ ..
+ }) => {
+ let before = *page;
if forward {
if *page + 1 >= pages.len() && pages.len() < MAX_PAGES {
pages.push(String::new());
@@ -405,6 +564,10 @@ impl Game<'_> {
} else if *page > 0 {
*page -= 1;
}
+ if *page != before {
+ let text = pages[*page].clone();
+ page_edit.set_value(&text);
+ }
}
Some(BookScreen::View {
lectern: Some(key), ..
@@ -764,6 +927,8 @@ impl Game<'_> {
if let Some(BookScreen::Edit { signing, .. }) = self.book.as_mut() {
*signing = false;
}
+ } else if inside(left + 36.0, 50.0, 114.0, 9.0) {
+ self.book_title_click(left + 36.0, mx);
}
}
Some(BookScreen::Edit { page, .. }) => {
@@ -780,6 +945,8 @@ impl Game<'_> {
} else if inside(w / 2.0 + 2.0, 196.0, 98.0, 20.0) {
click(self);
self.save_book(false);
+ } else if inside(left + 36.0, 32.0, 114.0, 14.0 * 9.0) {
+ self.book_page_click(left + 36.0, 32.0, mx, my);
}
}
Some(BookScreen::View {
@@ -865,7 +1032,7 @@ impl Game<'_> {
BookScreen::Edit {
signing: true,
title,
- opened,
+ title_edit,
..
} => {
let edit = tr("book.editTitle", "Enter Book Title:");
@@ -881,19 +1048,35 @@ impl Game<'_> {
// The title box: an unbordered, centred EditBox of 114 with
// black text and no shadow; the value alone is centred, the
// cursor follows it.
+ let value = &title_edit.value;
let x = left + 36.0;
- let tx = x + ((114.0 - HudBatch::text_width(atlas, title)) / 2.0).floor();
- horizon_client::ui::append_field(
- batch,
- atlas,
- title,
- (tx, 50.0, x + 114.0 - tx),
- (BLACK, false),
- 15,
- self.text_selection.covers(BOOK_TITLE, title),
- true,
- opened.elapsed().as_millis(),
- );
+ let tx = x + ((114.0 - HudBatch::text_width(atlas, value)) / 2.0).floor();
+ batch.text(atlas, value, tx, 50.0, BLACK, false);
+ if title_edit.cursor != title_edit.highlight {
+ let (a, b) = (
+ title_edit.cursor.min(title_edit.highlight),
+ title_edit.cursor.max(title_edit.highlight),
+ );
+ let pre: String = value.chars().take(a).collect();
+ let sel: String = value.chars().skip(a).take(b - a).collect();
+ let sx = tx + HudBatch::text_width(atlas, &pre);
+ batch.text_highlight(
+ atlas,
+ (sx, 49.0, HudBatch::text_width(atlas, &sel), 11.0),
+ true,
+ );
+ }
+ if title_edit.cursor_visible() {
+ let pre: String = value.chars().take(title_edit.cursor).collect();
+ batch.fill(
+ atlas,
+ tx + HudBatch::text_width(atlas, &pre),
+ 49.0,
+ 1.0,
+ 9.0,
+ BLACK,
+ );
+ }
let by = tr("book.byAuthor", "by %1$s").replace("%1$s", super::chat::PLAYER_NAME);
let tw = HudBatch::text_width(atlas, &by);
batch.text(
@@ -941,12 +1124,12 @@ impl Game<'_> {
BookScreen::Edit {
pages,
page,
- opened,
+ page_edit,
..
} => {
indicator(batch, *page, pages.len());
- let blink = (opened.elapsed().as_millis() / 300) % 2 == 0;
- let lines = self.book_lines(&pages[*page]);
+ let text = &pages[*page];
+ let lines = self.book_lines(text);
for (i, line) in lines.iter().take(LINES).enumerate() {
batch.text_styled(
atlas,
@@ -957,27 +1140,39 @@ impl Game<'_> {
false,
);
}
- if blink {
- // The cursor after the last character.
- let last = lines
- .last()
- .map(|l| atlas.font.width_text(l))
- .unwrap_or(0.0);
- let row = lines.len().saturating_sub(1) as f32
- + if pages[*page].ends_with('\n') {
- 1.0
- } else {
- 0.0
- };
- let x = if pages[*page].ends_with('\n') {
- 0.0
- } else {
- last
- };
+ let rows = self.page_rows(text);
+ let (sel_a, sel_b) = (
+ page_edit.cursor.min(page_edit.highlight),
+ page_edit.cursor.max(page_edit.highlight),
+ );
+ if sel_a != sel_b {
+ let chars: Vec = text.chars().collect();
+ for (i, &(start, end)) in rows.iter().enumerate().take(LINES) {
+ let from = sel_a.max(start);
+ let to = sel_b.min(end);
+ if from >= to {
+ continue;
+ }
+ let pre: String = chars[start..from].iter().collect();
+ let sel: String = chars[from..to].iter().collect();
+ batch.text_highlight(
+ atlas,
+ (
+ left + 36.0 + atlas.font.width(&pre),
+ top + 29.0 + i as f32 * 9.0,
+ atlas.font.width(&sel),
+ 9.0,
+ ),
+ true,
+ );
+ }
+ }
+ if page_edit.cursor_visible() {
+ let (x, row) = self.caret_on(text, &rows, page_edit.cursor);
batch.fill(
atlas,
left + 36.0 + x,
- top + 29.0 + row * 9.0,
+ top + 29.0 + row as f32 * 9.0,
1.0,
9.0,
BLACK,
diff --git a/crates/horizon-client/src/game/campfire.rs b/crates/horizon-client/src/game/campfire.rs
new file mode 100644
index 000000000..c388b7561
--- /dev/null
+++ b/crates/horizon-client/src/game/campfire.rs
@@ -0,0 +1,190 @@
+//! Campfires' cooking: `CampfireBlock.useItemOn` (`CampfireBlockEntity.placeFood`)
+//! and the block entity's `cookTick` (lit) and `cooldownTick` (unlit). The
+//! items drawn on the fire are `campfire_geometry`'s; a server's clients get
+//! them through the block entity's update tag (`lan::chunks::entity_tag`).
+use super::Game;
+use horizon_assets::item_model::DisplayContext;
+use horizon_client::{
+ Result, block_entities::Pos, block_entity_render::vertex_light, containers::BlockEntity,
+ items::ItemStack, mesher::Vertex, mobs::Who,
+};
+use horizon_core::position::BlockPos;
+
+/// The `campfire_cooking` recipe kind (`RecipeType.CAMPFIRE_COOKING`).
+const KIND: &str = "minecraft:campfire_cooking";
+
+fn is_campfire(name: &str) -> bool {
+ name == "minecraft:campfire" || name == "minecraft:soul_campfire"
+}
+
+impl Game<'_> {
+ /// `CampfireBlock.useItemOn` with a held item that is a campfire recipe's
+ /// input (`RecipePropertySet.CAMPFIRE_INPUT`): on a server level the food
+ /// goes into the first free slot (`placeFood`), and the use is consumed
+ /// whether or not there was room. A client of a server leaves the placing
+ /// to the server. Whether the food went in; None when this isn't a
+ /// campfire use, so the item's own use runs.
+ pub(super) fn use_on_campfire(&mut self, pos: [i32; 3]) -> Result> {
+ let state = self.world.block(pos[0], pos[1], pos[2]);
+ if !is_campfire(&self.res.facts().get(state).name) {
+ return Ok(None);
+ }
+ let Some(held) = self.held().cloned() else {
+ return Ok(None);
+ };
+ let Some(time) = self.res.recipes().cook(KIND, &held).map(|r| r.time as i32) else {
+ return Ok(None);
+ };
+ // A client of a server predicts the placing; the server decides.
+ if self.remote.is_some() {
+ return Ok(Some(true));
+ }
+ let key = (pos[0], pos[1], pos[2]);
+ let placed = match self.world.block_entities.get_mut(&key) {
+ Some(BlockEntity::Campfire(c)) => {
+ let mut one = held.clone();
+ c.place_food(&mut one, time)
+ }
+ _ => false,
+ };
+ if placed {
+ // ItemStack.consumeAndReturn: creative keeps the item.
+ self.consume_held();
+ self.stats.custom("interact_with_campfire", 1);
+ self.post_block_game_event(
+ "minecraft:block_change",
+ pos,
+ Some(Who::Player),
+ Some(state),
+ );
+ self.mark_block_entity_updated(key);
+ }
+ Ok(Some(placed))
+ }
+
+ /// `CampfireBlockEntity.cookTick` of a lit campfire, `cooldownTick` of an
+ /// unlit one. A finished item's result (the recipe's, or the item itself
+ /// when no recipe takes it any more) drops at the block's corner
+ /// (`Containers.dropItemStack` at its integer position), then the clients
+ /// see the slot empty and a `block_change` goes out.
+ pub(super) fn tick_campfire(&mut self, pos: Pos) -> Result<()> {
+ let state = self.world.block(pos.0, pos.1, pos.2);
+ let lit = self
+ .pack
+ .assets
+ .registry
+ .state(state)
+ .and_then(|s| s.properties.get("lit"))
+ .is_some_and(|v| v == "true");
+ let Some(BlockEntity::Campfire(c)) = self.world.block_entities.get_mut(&pos) else {
+ return Ok(());
+ };
+ // BlockEntity.setChanged: a timer that moved is saved with the
+ // chunk (no update to the clients, who don't see the timers).
+ let changed = if lit {
+ c.items.iter().any(Option::is_some)
+ } else {
+ c.cooldown_tick()
+ };
+ if changed {
+ self.world.modified.insert((pos.0 >> 4, pos.2 >> 4));
+ }
+ if !lit {
+ return Ok(());
+ }
+ let Some(BlockEntity::Campfire(c)) = self.world.block_entities.get_mut(&pos) else {
+ return Ok(());
+ };
+ let done = c.cook_tick();
+ for (_, item) in done {
+ let result = self
+ .res
+ .recipes()
+ .cook(KIND, &item)
+ .map_or_else(|| item.clone(), |r| r.result.clone());
+ self.campfire_drop(pos, result);
+ self.mark_block_entity_updated(pos);
+ self.post_block_game_event(
+ "minecraft:block_change",
+ [pos.0, pos.1, pos.2],
+ None,
+ Some(state),
+ );
+ }
+ Ok(())
+ }
+
+ /// `CampfireRenderer`: each cooking item lies on the fire, slot `i`
+ /// turned to `Direction.from2DDataValue((i + facing) % 4)`, laid flat
+ /// (90° about X), at a corner and three eighths of its size, drawn as a
+ /// FIXED item seeded by the block's position plus the slot.
+ pub(super) fn campfire_geometry(&self, camera: glam::DVec3, out: &mut Vec) {
+ for (key, entity) in &self.world.block_entities {
+ let BlockEntity::Campfire(c) = entity else {
+ continue;
+ };
+ if c.items.iter().all(Option::is_none) {
+ continue;
+ }
+ let at = glam::DVec3::new(f64::from(key.0), f64::from(key.1), f64::from(key.2));
+ if at.distance_squared(camera) >= 64.0 * 64.0 {
+ continue;
+ }
+ // Direction.get2DDataValue: south 0, west 1, north 2, east 3.
+ let state = self.world.block(key.0, key.1, key.2);
+ let facing = match self
+ .pack
+ .assets
+ .registry
+ .state(state)
+ .and_then(|s| s.properties.get("facing"))
+ .map(String::as_str)
+ {
+ Some("west") => 1,
+ Some("north") => 2,
+ Some("east") => 3,
+ _ => 0,
+ };
+ let light = vertex_light(self.world.light(key.0, key.1, key.2));
+ let packed = BlockPos::new(key.0, key.1, key.2).pack();
+ for (i, stack) in c.items.iter().enumerate() {
+ let Some(stack) = stack else {
+ continue;
+ };
+ let seed = (packed as i32).wrapping_add(i as i32);
+ let item = self.resolve_item(stack, DisplayContext::Fixed, None, seed);
+ if item.is_empty() {
+ continue;
+ }
+ // Direction.toYRot: 90° per 2D step from south.
+ let yaw = -(((i + facing) % 4) as f32 * 90.0);
+ let pose = glam::Mat4::from_translation(
+ (at - camera).as_vec3() + glam::Vec3::new(0.5, 0.449_218_75, 0.5),
+ ) * glam::Mat4::from_rotation_y(yaw.to_radians())
+ * glam::Mat4::from_rotation_x(90f32.to_radians())
+ * glam::Mat4::from_translation(glam::Vec3::new(-0.3125, -0.3125, 0.0))
+ * glam::Mat4::from_scale(glam::Vec3::splat(0.375));
+ self.submit_item_world(&item, pose, light, out);
+ }
+ }
+ }
+
+ /// `Containers.dropItemStack(level, x, y, z, stack)` at the block's
+ /// integer position.
+ fn campfire_drop(&mut self, pos: Pos, stack: ItemStack) {
+ let at = BlockPos::new(pos.0, pos.1, pos.2);
+ for piece in
+ horizon_world::level::removal::drop_item_stack(&mut self.world.random, at, stack.count)
+ {
+ self.spawn_item(
+ ItemStack {
+ count: piece.count,
+ ..stack.clone()
+ },
+ piece.pos,
+ piece.motion,
+ 0,
+ );
+ }
+ }
+}
diff --git a/crates/horizon-client/src/game/chat.rs b/crates/horizon-client/src/game/chat.rs
index 5d3919c8b..5b40adf9e 100644
--- a/crates/horizon-client/src/game/chat.rs
+++ b/crates/horizon-client/src/game/chat.rs
@@ -464,7 +464,28 @@ impl Game<'_> {
return;
}
// The Chat option's Hidden level takes the server's messages too.
- if !self.ui.chat.shows(ChatSource::Server) {
+ self.local_chat_line(text, Some(SYSTEM_INDICATOR), ChatSource::Server);
+ }
+ /// A player's chat line on the window's own chat: a LAN guest's message
+ /// while the host runs in-frame (`HostEvent::Chat`). `ChatListener`
+ /// checks `canReceivePlayerMessages` for these, so Commands Only and
+ /// Hidden drop them; command feedback (`/list`, `/tp`) is a system
+ /// message and takes the system-message permission instead.
+ pub(super) fn player_message(&mut self, text: Text) {
+ if let Some(capture) = self.chat_capture.as_mut() {
+ capture.push(text);
+ return;
+ }
+ // A server's client receives the line as a packet (PlayerChat or
+ // DisguisedChat) and filters it there.
+ if self.remote.is_none() {
+ self.local_chat_line(text, None, ChatSource::Player);
+ }
+ }
+ /// A line on the window's own chat when the Chat option lets `source`
+ /// through; running mods hear it.
+ fn local_chat_line(&mut self, text: Text, indicator: Option, source: ChatSource) {
+ if !self.ui.chat.shows(source) {
return;
}
let focused = self.screen == Screen::Chat;
@@ -472,8 +493,8 @@ impl Game<'_> {
self.ui.chat.add(
&self.gui.atlas,
text,
- Some(SYSTEM_INDICATOR),
- ChatSource::Server,
+ indicator,
+ source,
self.ticks,
focused,
);
@@ -602,7 +623,10 @@ impl Game<'_> {
return;
}
if let Some(command) = message.strip_prefix('/') {
- self.run_command(command);
+ // A client mod's command first, then a server mod's, as on a server.
+ if !self.mod_command(command) && !self.local_server_mod_command(command) {
+ self.run_command(command);
+ }
} else {
let line = Text::translate(
"chat.type.text",
@@ -622,6 +646,7 @@ impl Game<'_> {
self.mods_chat(&line);
}
self.lan_host_chat(message);
+ self.mod_player_chat(super::server_mods::Who::Local, message);
}
}
pub(super) fn chat_scroll(&mut self, amount: f32) {
diff --git a/crates/horizon-client/src/game/clipboard.rs b/crates/horizon-client/src/game/clipboard.rs
index c87aa3681..6e81bf3ee 100644
--- a/crates/horizon-client/src/game/clipboard.rs
+++ b/crates/horizon-client/src/game/clipboard.rs
@@ -63,8 +63,8 @@ impl Game<'_> {
}
}
}
- /// Typing or a backspace in a field of the title screens: in the server
- /// screens' append-only fields Select All's selection is replaced first.
+ /// Typing or a backspace in a title-screen field. A field that still only
+ /// appends replaces a Select All selection before the edit.
pub(super) fn title_edit(&mut self, typed: Option<&str>, backspace: bool) {
let Some(title) = self.title.as_mut() else {
return;
@@ -86,42 +86,31 @@ impl Game<'_> {
}
/// Shortcuts in the creative inventory's search box (an `EditBox` of 50).
pub(super) fn creative_search_shortcut(&mut self, shortcut: EditShortcut) {
- if Self::append_shortcut(
- &mut self.text_selection,
- &mut self.clipboard,
- CREATIVE_SEARCH,
- &mut self.creative_screen.search,
- shortcut,
- CREATIVE_SEARCH_CHARS,
- ) {
+ if self
+ .creative_screen
+ .search
+ .shortcut(shortcut, &mut self.clipboard)
+ {
self.creative_screen.scroll = 0.0;
}
}
/// A backspace in the creative search box: with Ctrl, the whole word
/// before the caret (`EditBox.deleteWords(-1)`), otherwise one character.
- /// A Select All selection is what it deletes, as in `EditBox.keyPressed`.
+ /// A selection is what it deletes, as in `EditBox.keyPressed`.
pub(super) fn creative_search_backspace(&mut self, ctrl: bool) {
- let search = &mut self.creative_screen.search;
- if !self.text_selection.take(CREATIVE_SEARCH, search) {
- if ctrl {
- horizon_client::ui::delete_words(search, -1);
- } else {
- search.pop();
- }
- }
+ self.creative_screen
+ .search
+ .key(KeyCode::Backspace, ctrl, false);
self.creative_screen.scroll = 0.0;
}
/// Typing in the creative search box.
pub(super) fn creative_search_edit(&mut self, typed: Option<&str>) {
- let search = &mut self.creative_screen.search;
- self.text_selection.take(CREATIVE_SEARCH, search);
if let Some(text) = typed {
- clipboard::append_limited(search, text, CREATIVE_SEARCH_CHARS);
+ self.creative_screen.search.insert_text(text);
+ self.creative_screen.scroll = 0.0;
}
- self.creative_screen.scroll = 0.0;
}
}
/// `CreativeModeInventoryScreen`'s search box: `setMaxLength(50)`.
pub(super) const CREATIVE_SEARCH_CHARS: usize = 50;
-pub(super) const CREATIVE_SEARCH: horizon_client::clipboard::FieldId = ("creative.search", 0);
diff --git a/crates/horizon-client/src/game/combat.rs b/crates/horizon-client/src/game/combat.rs
index 227aa0332..106e0e1a1 100644
--- a/crates/horizon-client/src/game/combat.rs
+++ b/crates/horizon-client/src/game/combat.rs
@@ -52,11 +52,16 @@ impl Game<'_> {
/// Remember who deals the next `damage_type` damage to the player.
pub(super) fn note_attacker(&mut self, damage_type: &str, mob: u32) {
if let Some(name) = self.mob_display_name(mob) {
- self.combat
- .pending
- .push((damage_type.to_owned(), (mob, name)));
+ self.note_named_attacker(damage_type, mob, name);
}
}
+ /// `note_attacker` for an entity that is not one of `self.mobs` (the
+ /// ender dragon).
+ pub(super) fn note_named_attacker(&mut self, damage_type: &str, id: u32, name: Text) {
+ self.combat
+ .pending
+ .push((damage_type.to_owned(), (id, name)));
+ }
/// `LivingEntity.handleDamageEvent` for the player: the hurt sound of the
/// source's `DamageEffects` (`Player.getHurtSound`).
pub(super) fn player_hurt_sound(&mut self, sound: &str, at: [f64; 3]) {
diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs
index f3afdcab6..8f934ad8a 100644
--- a/crates/horizon-client/src/game/commands.rs
+++ b/crates/horizon-client/src/game/commands.rs
@@ -85,16 +85,16 @@ fn format_double(v: f64) -> String {
impl Game<'_> {
/// Commands.performCommand for a line without its slash.
- pub(super) fn run_command(&mut self, command: &str) {
+ pub(super) fn run_command(&mut self, command: &str) -> bool {
if self.mod_command(command) {
- return;
+ return true;
}
let d = vanilla::game_dispatcher();
let parse = d.parse(command, &self.ui.registries);
let result = match parse.executable() {
Err(e) => {
self.syntax_failure(&e, command);
- return;
+ return false;
}
Ok((ctx, _)) => {
let ctx = ctx.clone();
@@ -103,6 +103,7 @@ impl Game<'_> {
self.run_chain(&root, &ctx)
}
};
+ let ok = result.is_ok();
if let Err(e) = result {
if e.input.is_some() {
self.syntax_failure(&e, command);
@@ -114,6 +115,20 @@ impl Game<'_> {
if self.player.is_some() {
self.check_inventory_trigger();
}
+ ok
+ }
+ /// `run_command` with its output suppressed
+ /// (`CommandSourceStack.withSuppressedOutput`): the player's own source,
+ /// rights and checks, no feedback and no failure in the chat.
+ pub(super) fn run_command_quietly(&mut self, command: &str) -> bool {
+ let quiet = execute::Source {
+ silent: true,
+ ..self.player_source()
+ };
+ let outer = self.command_source.replace(quiet);
+ let ok = self.run_command(command);
+ self.command_source = outer;
+ ok
}
fn syntax_failure(&mut self, e: &CommandError, command: &str) {
self.command_failure(e.message.clone());
@@ -1870,11 +1885,12 @@ impl Game<'_> {
}
}
}
- let Some((_, new)) = horizon_client::save::decode_block_entity(&tag) else {
+ let Some((_, mut new)) = horizon_client::save::decode_block_entity(&tag) else {
return false;
};
+ let rolled = self.take_loot_table(key, &tag, &mut new);
let after = horizon_client::save::encode_block_entity(key, &new, &name);
- if after == before {
+ if after == before && !rolled {
return false;
}
self.world.block_entities.insert(key, new);
@@ -1882,6 +1898,79 @@ impl Game<'_> {
let _ = self.world.set(pos[0], pos[1], pos[2], state);
true
}
+ /// RandomizableContainer.tryLoadLootTable for a tag a command gives a
+ /// container (`setblock`, `data`): an empty container with a `LootTable`
+ /// is rolled now, as a loaded chunk's are (`save::roll_loaded_loot`), and
+ /// the roll is kept with its chunk, so that the container saves, and
+ /// `data get` shows, its table and seed until its items change. A tag
+ /// without one drops what the container had. True when it had a table.
+ pub(super) fn take_loot_table(
+ &mut self,
+ key: (i32, i32, i32),
+ tag: &horizon_world::nbt::Tag,
+ entity: &mut horizon_client::containers::BlockEntity,
+ ) -> bool {
+ use horizon_client::{containers::BlockEntity, save, structure_blocks};
+ let table = save::text(tag.get("LootTable"));
+ let rolled = match (table, entity) {
+ (Some(table), BlockEntity::Chest(slots)) if slots.iter().all(Option::is_none) => {
+ let loot = structure_blocks::loot_data(&self.options.resources);
+ let at = Vec3::new(f64::from(key.0), f64::from(key.1), f64::from(key.2));
+ let biome = self.biome_name_at(at).unwrap_or_default();
+ Some(structure_blocks::roll_container(
+ slots,
+ loot.as_deref(),
+ table,
+ structure_blocks::loot_seed(tag),
+ key,
+ &biome,
+ ))
+ }
+ _ => None,
+ };
+ let Some(chunk) = self.world.chunks.get_mut(&(key.0 >> 4, key.2 >> 4)) else {
+ return false;
+ };
+ let held = |c: &horizon_client::chunk::Chunk| {
+ c.saved
+ .as_ref()
+ .is_some_and(|s| s.rolled_loot.iter().any(|r| r.pos == key))
+ };
+ if rolled.is_none() && !held(chunk) {
+ return false;
+ }
+ let chunk = std::sync::Arc::make_mut(chunk);
+ let mut extras = chunk
+ .saved
+ .take()
+ .map(std::sync::Arc::unwrap_or_clone)
+ .unwrap_or_default();
+ extras.rolled_loot.retain(|r| r.pos != key);
+ let taken = rolled.is_some();
+ extras.rolled_loot.extend(rolled);
+ chunk.saved = Some(std::sync::Arc::new(extras));
+ taken
+ }
+ /// The loot table and seed a container still holding its roll saves
+ /// (`RolledLoot::restore`), in place of its items.
+ pub(super) fn restore_loot_table(
+ &self,
+ key: (i32, i32, i32),
+ tag: &mut horizon_world::nbt::Tag,
+ ) {
+ let horizon_world::nbt::Tag::Compound(map) = tag else {
+ return;
+ };
+ let rolled = self
+ .world
+ .chunks
+ .get(&(key.0 >> 4, key.2 >> 4))
+ .and_then(|c| c.saved.as_ref())
+ .and_then(|s| s.rolled_loot.iter().find(|r| r.pos == key));
+ if let Some(r) = rolled {
+ r.restore(map);
+ }
+ }
fn cmd_fill(&mut self, ctx: &Context) -> R {
let a = self.block_pos_arg(ctx, "from")?;
let b = self.block_pos_arg(ctx, "to")?;
@@ -2578,38 +2667,7 @@ impl Game<'_> {
/// `PoiTypes`: the point of interest type of a block state.
fn poi_type(&self, state: horizon_world::registry::BlockStateId) -> Option<&'static str> {
let s = self.pack.assets.registry.state(state)?;
- let name = s.name.strip_prefix("minecraft:")?;
- Some(match name {
- "blast_furnace" => "minecraft:armorer",
- "smoker" => "minecraft:butcher",
- "cartography_table" => "minecraft:cartographer",
- "brewing_stand" => "minecraft:cleric",
- "composter" => "minecraft:farmer",
- "barrel" => "minecraft:fisherman",
- "fletching_table" => "minecraft:fletcher",
- "cauldron" | "lava_cauldron" | "water_cauldron" | "powder_snow_cauldron" => {
- "minecraft:leatherworker"
- }
- "lectern" => "minecraft:librarian",
- "stonecutter" => "minecraft:mason",
- "loom" => "minecraft:shepherd",
- "smithing_table" => "minecraft:toolsmith",
- "grindstone" => "minecraft:weaponsmith",
- "bell" => "minecraft:meeting",
- "beehive" => "minecraft:beehive",
- "bee_nest" => "minecraft:bee_nest",
- "nether_portal" => "minecraft:nether_portal",
- "lodestone" => "minecraft:lodestone",
- "test_instance_block" => "minecraft:test_instance",
- n if n.ends_with("_bed") => {
- if s.properties.get("part").map(String::as_str) != Some("head") {
- return None;
- }
- "minecraft:home"
- }
- n if n.ends_with("lightning_rod") => "minecraft:lightning_rod",
- _ => return None,
- })
+ horizon_client::save::poi_type(s).map(|(kind, _)| kind)
}
/// `LocateCommand.locatePoi`: `PoiManager.findClosestWithType` within
/// 256 blocks of the source, over the loaded chunks.
diff --git a/crates/horizon-client/src/game/commands/data.rs b/crates/horizon-client/src/game/commands/data.rs
index 18035ddd7..ac16c5e7c 100644
--- a/crates/horizon-client/src/game/commands/data.rs
+++ b/crates/horizon-client/src/game/commands/data.rs
@@ -286,6 +286,7 @@ impl Game<'_> {
if let Tag::Compound(c) = &mut tag {
c.remove(&"keepPacked".into());
}
+ self.restore_loot_table(key, &mut tag);
Ok(tag)
}
DataRef::Storage(id) => Ok(self.score.storage.get(id)),
@@ -296,7 +297,8 @@ impl Game<'_> {
DataRef::Entity(t) => self.apply_entity_nbt(*t, tag),
DataRef::Block(p) => {
let key = (p[0], p[1], p[2]);
- if let Some((_, new)) = save::decode_block_entity(&tag) {
+ if let Some((_, mut new)) = save::decode_block_entity(&tag) {
+ self.take_loot_table(key, &tag, &mut new);
self.world.block_entities.insert(key, new);
let state = self.world.block(p[0], p[1], p[2]);
// Redraw what shows the block entity (signs, banners).
diff --git a/crates/horizon-client/src/game/commands/world_cmds.rs b/crates/horizon-client/src/game/commands/world_cmds.rs
index 30f019997..687eb3f88 100644
--- a/crates/horizon-client/src/game/commands/world_cmds.rs
+++ b/crates/horizon-client/src/game/commands/world_cmds.rs
@@ -49,26 +49,12 @@ impl Game<'_> {
let mut tracker = Tracker::default();
for &t in &targets {
let h = holder(self.target_name(t));
- // ServerRecipeBook.addRecipes/removeRecipes count recipe book
- // displays (a transmute recipe has one per material count).
- let mut n = 0;
- // ServerPlayer.awardRecipes / resetRecipes: the displays changed.
- let index = &self.res.stations().recipe_index;
- n += if give {
- let (count, unlocked) = self.level.recipe_book.add_recipes(index, &recipes);
- self.recipe_unlocked(&unlocked);
- count as i32
- } else {
- let removed = self.level.recipe_book.remove_recipes(index, &recipes);
- let count = removed.len() as i32;
- // A network player's client drops them from its book.
- if self.lan_acting.is_some() && !removed.is_empty() {
- self.guest_packets.push(C::RecipeBookRemove(
- horizon_protocol::packets::recipes::RecipeBookRemove { ids: removed },
- ));
- }
- count
- };
+ // ServerPlayer.awardRecipes / resetRecipes on the target's own
+ // ServerRecipeBook: a guest's is swapped in for the call, and the
+ // packets go to its client.
+ let n = self
+ .for_player(t, |g| g.change_recipes(give, &recipes))
+ .unwrap_or(0);
tracker.track(&h, n);
}
let verb = if give { "give" } else { "take" };
@@ -92,6 +78,27 @@ impl Game<'_> {
Ok(total)
}
+ /// ServerRecipeBook.addRecipes/removeRecipes for the player in place: how
+ /// many recipe book displays changed (a transmute recipe has one per
+ /// material count).
+ fn change_recipes(&mut self, give: bool, recipes: &[String]) -> i32 {
+ let index = &self.res.stations().recipe_index;
+ if give {
+ let (count, unlocked) = self.level.recipe_book.add_recipes(index, recipes);
+ self.recipe_unlocked(&unlocked);
+ return count as i32;
+ }
+ let removed = self.level.recipe_book.remove_recipes(index, recipes);
+ let count = removed.len() as i32;
+ // A network player's client drops them from its book.
+ if self.lan_acting.is_some() && !removed.is_empty() {
+ self.guest_packets.push(C::RecipeBookRemove(
+ horizon_protocol::packets::recipes::RecipeBookRemove { ids: removed },
+ ));
+ }
+ count
+ }
+
// ---- /damage ----------------------------------------------------------------
fn damage_kind(ty: &str) -> DamageKind {
diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs
index 3a8562ee1..a2ed7fd66 100644
--- a/crates/horizon-client/src/game/creatures.rs
+++ b/crates/horizon-client/src/game/creatures.rs
@@ -50,9 +50,9 @@ pub(super) struct Creatures {
/// The wandering trader spawner's clock.
pub trader: super::wandering_trader::Spawner,
}
-struct ProjWorld<'a, 'w> {
- w: &'a MobWorld<'w>,
- targets: Vec<(Who, Aabb)>,
+pub(super) struct ProjWorld<'a, 'w> {
+ pub w: &'a MobWorld<'w>,
+ pub targets: Vec<(Who, Aabb)>,
}
impl ProjectileWorld for ProjWorld<'_, '_> {
fn clip(&self, from: Vec3, to: Vec3) -> Option<(Vec3, [i32; 3], [i32; 3])> {
@@ -373,6 +373,7 @@ impl Game<'_> {
// Drawn seated (Entity.isPassenger); set apart because the
// json! above is at the macro recursion limit.
state["passenger"] = m.extra.passenger.into();
+ state["guardian_beam"] = serde_json::json!(horizon_client::mobs::guardian::beam(m, 0.0));
state
})
.collect();
@@ -391,6 +392,9 @@ impl Game<'_> {
}
pub(super) fn mob_world_base<'s>(&'s self, others: &'s [Seen]) -> MobWorld<'s> {
let (rain, thunder) = self.world.weather.levels(1.0);
+ // Level.canHaveWeather: only the overworld's rain and thunder levels
+ // ever rise, so nothing rains or thunders in the Nether or the End.
+ let weather = self.dim == Dim::Overworld;
let player = self.player_view();
MobWorld {
world: &self.world,
@@ -407,8 +411,8 @@ impl Game<'_> {
daylight: self.dim != Dim::Nether && self.sky_darken() < 4,
game_time: self.world.game_time,
day_time: self.world.day_time,
- raining: rain > 0.2,
- thundering: thunder > 0.9,
+ raining: weather && rain > 0.2,
+ thundering: weather && thunder > 0.9,
nether: self.dim == Dim::Nether,
block_tags: &self.res.items().block_tags,
biomes: self.res.biomes(),
@@ -837,6 +841,7 @@ impl Game<'_> {
pub(super) fn handle_mob_events(&mut self, events: Vec) -> Result<()> {
for e in events {
match e {
+ MobEvent::SquidCycle { mob, at } => self.lan_entity_event(mob as i32, 19, at),
MobEvent::RabbitHop { mob, at } => self.lan_entity_event(mob as i32, 1, at),
MobEvent::Sound(name, at, volume, pitch)
| MobEvent::LocalSound(name, at, volume, pitch) => {
@@ -1062,7 +1067,10 @@ impl Game<'_> {
}
MobEvent::BreakBlock { pos, drop } => {
let old = self.world.block(pos[0], pos[1], pos[2]);
- self.destroy_particles(pos, old);
+ // Block.spawnDestroyParticles: fire breaks leave nothing.
+ if !self.res.facts().get(old).has_class("BaseFireBlock") {
+ self.destroy_particles(pos, old);
+ }
if drop {
let drops = self.res.items().block_drops_in(
self.pack.assets.registry,
@@ -1084,6 +1092,9 @@ impl Game<'_> {
self.ensure_item_particle_sprites();
}
let options = horizon_client::particles::ParticleOptions::simple(ty);
+ if ty == horizon_client::particles::ParticleType::SonicBoom {
+ self.lan_sonic_boom(&options, at);
+ }
// Count 0: one particle exactly there, at rest.
if count == 0 {
self.particles.add(&options, at, [0.0; 3]);
diff --git a/crates/horizon-client/src/game/cushions.rs b/crates/horizon-client/src/game/cushions.rs
new file mode 100644
index 000000000..0847321d6
--- /dev/null
+++ b/crates/horizon-client/src/game/cushions.rs
@@ -0,0 +1,494 @@
+//! Cushions: `CushionItem.useOn` placement on the top face of a block,
+//! `Cushion.canBePlacedAt`/`survives`, `Cushion.interact` (sit),
+//! `BlockAttachedEntity.hurtServer` (break, dropping the matching colour),
+//! `BlockAttachedEntity.tick` and `CushionRenderer`.
+//!
+//! A cushion is a `BlockAttachedEntity`, not a block: it has no block state,
+//! only an entity whose `Pos` is the click point's height. Placement uses
+//! outline bounds, collision shapes and suffocation predicates.
+use super::{Game, Hit, view_vector};
+use horizon_client::entities::{Cushion, dye_index};
+use horizon_client::items::ItemStack;
+use horizon_client::mobs::anim::Poses;
+use horizon_core::geometry::{Aabb, Vec3};
+use winit::keyboard::KeyCode;
+
+/// `Direction.fromYRot(yaw).toYRot()`: the nearest 90 degree step.
+pub(crate) fn snap_yaw(yaw: f32) -> f32 {
+ (((f64::from(yaw) / 90.0 + 0.5).floor() as i64) & 3) as f32 * 90.0
+}
+
+fn aabb(bounds: ([f64; 3], [f64; 3])) -> Aabb {
+ Aabb::new(
+ Vec3::new(bounds.0[0], bounds.0[1], bounds.0[2]),
+ Vec3::new(bounds.1[0], bounds.1[1], bounds.1[2]),
+ )
+}
+
+fn overlaps(a: Aabb, b: Aabb) -> bool {
+ a.intersects(b)
+}
+use horizon_client::cushion::blocks_in;
+
+struct PlacementWorld<'a, 'r>(&'a Game<'r>);
+impl horizon_client::cushion::World for PlacementWorld<'_, '_> {
+ fn outline_bounds(&self, p: [i32; 3]) -> Option {
+ let boxes = self.0.outline_boxes(p);
+ let mut shape = *boxes.first()?;
+ for b in &boxes[1..] {
+ for k in 0..3 {
+ shape[k] = shape[k].min(b[k]);
+ shape[k + 3] = shape[k + 3].max(b[k + 3]);
+ }
+ }
+ Some(Aabb::new(
+ Vec3::new(shape[0], shape[1], shape[2]),
+ Vec3::new(shape[3], shape[4], shape[5]),
+ ))
+ }
+
+ fn collision_shapes(&self, bounds: Aabb) -> Vec {
+ let binding = self.0.res.collision();
+ let mut world = horizon_client::collisions::SceneCollisions {
+ world: &self.0.world,
+ binding,
+ subject: None,
+ };
+ // Include neighbour blocks whose collision shape can reach outside their cell.
+ blocks_in(bounds.inflate(Vec3::new(1.0, 1.0, 1.0)))
+ .filter_map(|p| {
+ let state = self.0.world.block(p[0], p[1], p[2]);
+ binding
+ .collision_shape(&mut world, state, p)
+ .ok()
+ .map(|s| s.moved(Vec3::new(f64::from(p[0]), f64::from(p[1]), f64::from(p[2]))))
+ })
+ .collect()
+ }
+ fn suffocating(&self, p: [i32; 3]) -> bool {
+ let state = self.0.world.block(p[0], p[1], p[2]);
+ let facts = self.0.res.facts().get(state);
+ let name = facts.name.as_ref();
+ // Blocks' explicit predicates, then the default causes_suffocation/full-shape rule.
+ match name {
+ "minecraft:dirt_path"
+ | "minecraft:farmland"
+ | "minecraft:mud"
+ | "minecraft:soul_sand" => true,
+ "minecraft:piston" | "minecraft:sticky_piston" => self
+ .0
+ .pack
+ .assets
+ .registry
+ .state(state)
+ .is_some_and(|s| s.properties.get("extended").is_some_and(|p| p == "false")),
+ "minecraft:mangrove_roots" | "minecraft:moving_piston" => false,
+ _ if name.ends_with("_leaves")
+ || name.ends_with("glass")
+ || name.ends_with("copper_grate") =>
+ {
+ false
+ }
+ // ShulkerBoxBlock: suffocating while closed; a box seen open
+ // here is rare enough to count as closed.
+ _ if name.ends_with("shulker_box") => true,
+ _ => {
+ facts.full_collision
+ && self
+ .0
+ .res
+ .items()
+ .block_in_tag("minecraft:causes_suffocation", name)
+ }
+ }
+ }
+}
+
+impl Game<'_> {
+ /// The cushion under the crosshair, its distance and the hit point.
+ pub(super) fn cushion_hit(&self) -> Option<(usize, f64, [f64; 3])> {
+ let eye = self.eye_position()?;
+ let dir = view_vector(self.yaw, self.pitch);
+ let reach: f64 = if self.creative { 5.0 } else { 3.0 };
+ let mut best: Option<(usize, f64, [f64; 3])> = None;
+ for (i, c) in self.cushions.iter().enumerate() {
+ let Some(d) = super::ray_box(eye, dir, reach, aabb(c.bounds())) else {
+ continue;
+ };
+ if best.is_none_or(|b| d < b.1) {
+ best = Some((i, d, std::array::from_fn(|k| eye[k] + dir[k] * d)));
+ }
+ }
+ best
+ }
+
+ pub(super) fn cushion_survives(&self, c: &Cushion) -> bool {
+ horizon_client::cushion::survives(&PlacementWorld(self), aabb(c.bounds()))
+ }
+
+ /// `Cushion.destroyIfInFire`: a fire-tagged block inside the deflated box.
+ fn cushion_in_fire(&self, c: &Cushion) -> bool {
+ let bounds = aabb(c.bounds()).next_deflated();
+ blocks_in(bounds).any(|p| {
+ let facts = self.res.facts().get(self.world.block(p[0], p[1], p[2]));
+ self.res.items().block_in_tag("minecraft:fire", &facts.name)
+ })
+ }
+
+ /// `CushionItem.useOn`: on the top face only, spawning at the click's
+ /// height above the clicked block (or the replaceable block itself).
+ pub(super) fn place_cushion(&mut self, hit: &Hit, stack: &ItemStack) -> bool {
+ let Some(info) = self.res.items().get(&stack.item).cloned() else {
+ return false;
+ };
+ let Some(default_color) = info.cushion_color.as_deref() else {
+ return false;
+ };
+ let color_override = stack
+ .data()
+ .and_then(|d| d.other("minecraft:cushion/color"))
+ .and_then(|tag| match tag {
+ horizon_world::nbt::Tag::String(s) => Some(s.to_string_lossy()),
+ _ => None,
+ });
+ let color = dye_index(color_override.as_deref().unwrap_or(default_color));
+ // CushionItem.recalculateContextForSpecialCollisionShapes: open containers
+ // (cauldrons, hoppers and composters) use the collision ray's actual height.
+ let mut adjusted = Hit {
+ pos: hit.pos,
+ face: hit.face,
+ point: hit.point,
+ };
+ let state = self.world.block(hit.pos[0], hit.pos[1], hit.pos[2]);
+ if self.res.items().block_in_tag(
+ "minecraft:cushion_uses_collision_shape",
+ &self.res.facts().get(state).name,
+ ) && let Some(eye) = self.eye_position()
+ {
+ let start = Vec3::new(eye[0], eye[1], eye[2]);
+ let point = Vec3::new(hit.point[0], hit.point[1], hit.point[2]);
+ let end = point.add_vector(point.subtract(start).normalize().scale(0.001));
+ let binding = self.res.collision();
+ let mut world = horizon_client::collisions::SceneCollisions {
+ world: &self.world,
+ binding,
+ subject: None,
+ };
+ if let Ok(shape) = binding.collision_shape(&mut world, state, hit.pos)
+ && let Some(result) = shape.clip(
+ start,
+ end,
+ horizon_core::position::BlockPos::new(hit.pos[0], hit.pos[1], hit.pos[2]),
+ )
+ {
+ adjusted.face = result.direction.normal();
+ adjusted.point = [result.location.x, result.location.y, result.location.z];
+ }
+ }
+ let hit = &adjusted;
+ if hit.face != [0, 1, 0] {
+ return false;
+ }
+ // BlockPlaceContext: a replaceable clicked block is replaced.
+ let clicked = self
+ .res
+ .facts()
+ .get(self.world.block(hit.pos[0], hit.pos[1], hit.pos[2]));
+ let pos = if clicked.replaceable {
+ hit.pos
+ } else {
+ std::array::from_fn(|i| hit.pos[i] + hit.face[i])
+ };
+ // `Vec3.atCenterOfWithY(clickedPos, clickLocation.y)`.
+ let y = hit.point[1];
+ let position = Vec3::new(f64::from(pos[0]) + 0.5, y, f64::from(pos[2]) + 0.5);
+ let bounds = aabb((
+ [position.x - 0.5, position.y, position.z - 0.5],
+ [position.x + 0.5, position.y + 0.25, position.z + 0.5],
+ ));
+ if !horizon_client::cushion::can_place(&PlacementWorld(self), bounds) {
+ return false;
+ }
+ // `getEntitiesOfClass(Cushion.class, spawnAABB).isEmpty()`.
+ if self
+ .cushions
+ .iter()
+ .any(|c| overlaps(aabb(c.bounds()), bounds))
+ {
+ return false;
+ }
+ self.next_entity += 1;
+ let id = self.next_entity;
+ // `BlockAttachedEntity.setPos`: `BlockPos.containing(position)`.
+ let attached = [
+ position.x.floor() as i32,
+ position.y.floor() as i32,
+ position.z.floor() as i32,
+ ];
+ self.cushions.push(Cushion {
+ id,
+ position,
+ pos: attached,
+ yaw: snap_yaw(self.yaw),
+ color,
+ check_interval: 0,
+ });
+ self.world
+ .modified
+ .insert((attached[0] >> 4, attached[2] >> 4));
+ // `destroyIfInFire`: placed into fire, it breaks at once.
+ let placed = self.cushions.len() - 1;
+ if self.cushion_in_fire(&self.cushions[placed]) {
+ self.break_cushion(placed, false);
+ }
+ self.audio.play(
+ "minecraft:entity.cushion.place",
+ Some([position.x, position.y, position.z]),
+ 0.75,
+ 0.8,
+ );
+ let ctx = self.who_context(Some(horizon_client::mobs::Who::Player), None);
+ self.post_game_event(
+ "minecraft:entity_place",
+ [position.x, position.y, position.z],
+ ctx,
+ );
+ if !self.creative {
+ self.consume_held();
+ }
+ true
+ }
+
+ /// `Cushion.interact`: sit down unless sneaking or already occupied.
+ pub(super) fn interact_cushion(&mut self) -> bool {
+ let Some((i, d, _)) = self.cushion_hit() else {
+ return false;
+ };
+ if !self.before_block(d) {
+ return false;
+ }
+ // isSecondaryUseActive.
+ if self.keys.contains(&KeyCode::ShiftLeft) {
+ return false;
+ }
+ let id = self.cushions[i].id;
+ let occupied = self.riding == Some(id)
+ || self
+ .lan
+ .as_ref()
+ .is_some_and(|h| h.hub.guests.iter().any(|g| g.vehicle == Some(id)));
+ // `Cushion.interact`: an occupied cushion (`isVehicle`) passes.
+ if occupied {
+ return false;
+ }
+ // `Player.startRiding` gives up the vehicle it already has; the
+ // client never sends that click, so pass instead.
+ if self.riding.is_some() {
+ return false;
+ }
+ self.riding = Some(id);
+ let p = self.cushions[i].position;
+ self.audio.play(
+ "minecraft:entity.cushion.sit",
+ Some([p.x, p.y, p.z]),
+ 1.0,
+ 1.0,
+ );
+ self.swing_arm();
+ true
+ }
+
+ /// `Cushion.hurtServer` from a player: killed, then the item dropped.
+ pub(super) fn attack_cushion(&mut self) -> bool {
+ let Some((i, d, _)) = self.cushion_hit() else {
+ return false;
+ };
+ if !self.before_block(d) || self.mob_hit().is_some_and(|m| m.1 < d) {
+ return false;
+ }
+ // `skipAttackInteraction` when `mayInteract` is false.
+ if !self.may_build() {
+ return true;
+ }
+ self.break_cushion(i, true);
+ true
+ }
+
+ /// `Cushion.dropItem`: the break sound and particles, then the coloured
+ /// item unless drops are off or the breaker has infinite materials.
+ pub(super) fn break_cushion(&mut self, index: usize, by_player: bool) {
+ let c = self.cushions.remove(index);
+ let p = c.position;
+ self.world.modified.insert((c.pos[0] >> 4, c.pos[2] >> 4));
+ if self.riding == Some(c.id) {
+ self.riding = None;
+ }
+ self.audio.play(
+ "minecraft:entity.cushion.break",
+ Some([p.x, p.y, p.z]),
+ 1.0,
+ 1.0,
+ );
+ self.cushion_break_particles(&c);
+ if !self.ui.rules.bool("entity_drops") || (by_player && self.creative) {
+ return;
+ }
+ let stack = ItemStack {
+ item: std::sync::Arc::from(c.item()),
+ count: 1,
+ damage: 0,
+ extra: None,
+ };
+ self.spawn_item(stack, [p.x, p.y, p.z], [0.0; 3], 10);
+ }
+
+ /// `Cushion.showBreakingParticles`: ten of the matching wool block.
+ fn cushion_break_particles(&mut self, c: &Cushion) {
+ let name = format!("minecraft:{}_wool", c.color_name());
+ let Some(state) = self.pack.assets.registry.default_state(&name) else {
+ return;
+ };
+ let at = [
+ c.position.x.floor() as i32,
+ c.position.y.floor() as i32,
+ c.position.z.floor() as i32,
+ ];
+ let Some((sprite, tint)) = self.particle_sprite(state, at) else {
+ return;
+ };
+ let options = horizon_client::particles::ParticleOptions::block(
+ horizon_client::particles::ParticleType::Block,
+ sprite,
+ tint,
+ );
+ let p = c.position;
+ self.particles.send_particles(
+ &options,
+ [p.x, p.y + 0.666_666_7, p.z],
+ [0.25, 0.0625, 0.25],
+ 0.05,
+ 10,
+ );
+ }
+
+ /// `BlockAttachedEntity.tick`: survive and burn checks after the post-increment reaches 100.
+ pub(super) fn tick_cushions(&mut self) {
+ let ticking = self.entity_ticking_area();
+ let mut i = 0;
+ while i < self.cushions.len() {
+ let p = self.cushions[i].position;
+ if !ticking(p.x.floor() as i32, p.z.floor() as i32) {
+ i += 1;
+ continue;
+ }
+ let previous = self.cushions[i].check_interval;
+ self.cushions[i].check_interval += 1;
+ if previous < 100 {
+ i += 1;
+ continue;
+ }
+ self.cushions[i].check_interval = 0;
+ let c = self.cushions[i].clone();
+ if self.cushion_in_fire(&c) || !self.cushion_survives(&c) {
+ self.break_cushion(i, false);
+ continue;
+ }
+ i += 1;
+ }
+ }
+
+ /// `Player.rideTick` on a cushion: hold the seat, and get up on sneak.
+ pub(super) fn tick_cushion_riding(&mut self, keys: super::Keys) -> bool {
+ let Some(c) = self
+ .cushions
+ .iter()
+ .find(|c| Some(c.id) == self.riding)
+ .cloned()
+ else {
+ return false;
+ };
+ if keys.shift {
+ let at = c.dismount();
+ self.riding = None;
+ if let Some(p) = self.player.as_mut() {
+ self.previous_position = p.position;
+ p.position = at;
+ p.velocity = Vec3::ZERO;
+ p.fall_distance = 0.0;
+ }
+ // `Cushion.removePassenger`: the get-up sound for a rider that
+ // left rather than one whose cushion was killed.
+ self.audio.play(
+ "minecraft:entity.cushion.get_up",
+ Some([c.position.x, c.position.y, c.position.z]),
+ 1.0,
+ 1.0,
+ );
+ return true;
+ }
+ let seat = c.seat();
+ if let Some(p) = self.player.as_mut() {
+ self.previous_position = p.position;
+ p.position = seat;
+ p.velocity = Vec3::ZERO;
+ p.fall_distance = 0.0;
+ }
+ true
+ }
+
+ /// `CushionRenderer`: the coloured model, rotated to the nearest
+ /// direction, with its bottom on the cushion's position.
+ pub(super) fn cushion_geometry(
+ &self,
+ camera: glam::DVec3,
+ out: &mut Vec,
+ ) {
+ let Some(model) = self.cushion_model.as_ref() else {
+ return;
+ };
+ let sprites = &self.gui.atlas.sprites;
+ for c in &self.cushions {
+ let entity_start = out.len();
+ let a = aabb(c.bounds());
+ let (lo, hi) = (a.min_position(), a.max_position());
+ let (lo, hi) = (
+ glam::DVec3::new(lo.x, lo.y, lo.z),
+ glam::DVec3::new(hi.x, hi.y, hi.z),
+ );
+ let range =
+ super::entity_culling::default_range(hi - lo, self.settings.entity_distance);
+ if !self.entity_in_view(camera, (lo + hi) / 2.0, range, lo, hi) {
+ continue;
+ }
+ let Some(uv) = sprites
+ .get(&format!("entity/cushion/{}_cushion", c.color_name()))
+ .map(|r| [r.u0, r.v0, r.u1, r.v1])
+ else {
+ continue;
+ };
+ let p = glam::DVec3::new(c.position.x, c.position.y, c.position.z) - camera;
+ let base = glam::Mat4::from_translation(p.as_vec3())
+ * glam::Mat4::from_rotation_y((180.0 - snap_yaw(c.yaw)).to_radians())
+ * glam::Mat4::from_rotation_x(std::f32::consts::PI)
+ * glam::Mat4::from_translation(glam::Vec3::new(0.0, -0.25, 0.0));
+ // `EntityRenderer.getPackedLightCoords` probes at the eye height.
+ let l = self.world.light(
+ c.position.x.floor() as i32,
+ (c.position.y + 0.2125).floor() as i32,
+ c.position.z.floor() as i32,
+ );
+ let packed = (u32::from(l >> 4) * 16) | ((u32::from(l & 15) * 16) << 8);
+ horizon_client::mobs::render::draw(
+ model,
+ base,
+ &Poses::new(model),
+ uv,
+ 0xFFFF_FFFF,
+ packed,
+ 0,
+ out,
+ );
+ self.cull_entity_vertices(out, entity_start);
+ }
+ }
+}
diff --git a/crates/horizon-client/src/game/dragon.rs b/crates/horizon-client/src/game/dragon.rs
index 8c316deeb..c75c2c762 100644
--- a/crates/horizon-client/src/game/dragon.rs
+++ b/crates/horizon-client/src/game/dragon.rs
@@ -12,11 +12,13 @@ use horizon_client::{
hud_layout, hud_overlays,
mesher::Vertex,
particles::{ParticleOptions, ParticleType},
+ text::Text,
};
use horizon_core::{
geometry::{Aabb, Vec3},
random::{LegacyRandom, RandomSource},
};
+use horizon_protocol::entity_data::Value;
use horizon_world::nbt::Tag;
/// Mob ids from here on are dragon parts (`SPECIAL + part`) and crystals
@@ -1558,6 +1560,7 @@ impl Game<'_> {
y -= 1.0;
}
self.breath_clouds.push(Breath {
+ id: 0,
at: [x, y.floor() + 1.0, z],
radius: 5.0,
age: 0,
@@ -1691,25 +1694,8 @@ impl Game<'_> {
}
if d.random.next_int_bound(10).unwrap_or(1) == 0 {
let p = d.position;
- d.nearest_crystal = self
- .crystals
- .iter()
- .enumerate()
- .filter(|(_, c)| {
- (c.position.x - p.x).abs() <= 32.0
- && (c.position.y - p.y).abs() <= 32.0
- && (c.position.z - p.z).abs() <= 32.0
- })
- .min_by(|a, b| {
- let da = (a.1.position.x - p.x).powi(2)
- + (a.1.position.y - p.y).powi(2)
- + (a.1.position.z - p.z).powi(2);
- let db = (b.1.position.x - p.x).powi(2)
- + (b.1.position.y - p.y).powi(2)
- + (b.1.position.z - p.z).powi(2);
- da.total_cmp(&db)
- })
- .map(|(i, _)| i);
+ let nearest = self.nearest_crystal_to(p);
+ self.dragon.as_mut().expect("dragon").nearest_crystal = nearest;
}
let d = self.dragon.as_mut().expect("dragon");
d.yaw = wrap(d.yaw);
@@ -1870,7 +1856,7 @@ impl Game<'_> {
pl.velocity.z += zd / dd * 4.0;
}
if !sitting {
- g.hurt_player_by("minecraft:mob_attack", 5.0);
+ g.dragon_hurts_player("minecraft:mob_attack", 5.0);
}
});
}
@@ -1879,7 +1865,7 @@ impl Game<'_> {
for h in heads {
if h.inflate(Vec3::new(1.0, 1.0, 1.0)).intersects(p.bounds) {
self.as_fight_player(p.who, |g| {
- g.hurt_player_by("minecraft:mob_attack", 10.0);
+ g.dragon_hurts_player("minecraft:mob_attack", 10.0);
});
}
}
@@ -2037,6 +2023,7 @@ impl Game<'_> {
// assignDirectionalMovement: the direction times the acceleration power 0.1.
let velocity = Vec3::new(v.x * 0.1, v.y * 0.1, v.z * 0.1);
self.dragon_fireballs.push(DragonFireball {
+ id: 0,
position: at,
previous: at,
velocity,
@@ -2083,6 +2070,7 @@ impl Game<'_> {
});
if hit_block || hit_player || b.age > 600 {
self.breath_clouds.push(Breath {
+ id: 0,
at: [next.x, next.y, next.z],
radius: 3.0,
age: 0,
@@ -2161,10 +2149,20 @@ impl Game<'_> {
self.breath_clouds.append(&mut clouds);
for (who, amount) in harmed {
self.as_fight_player(who, |g| {
- g.hurt_player_by("minecraft:indirect_magic", amount);
+ g.dragon_hurts_player("minecraft:indirect_magic", amount);
});
}
}
+ /// Damage from the dragon, which is its causing entity
+ /// (`DamageSources.mobAttack(dragon)`, `indirectMagic(cloud, dragon)`): the
+ /// death message names it.
+ fn dragon_hurts_player(&mut self, damage_type: &str, amount: f32) {
+ if let Some(id) = self.dragon.as_ref().map(|d| d.id) {
+ let name = Text::translate("entity.minecraft.ender_dragon", &[]);
+ self.note_named_attacker(damage_type, id, name);
+ }
+ self.hurt_player_by(damage_type, amount);
+ }
/// `BossHealthOverlay.extractRenderState`: whatever `publish_boss_bars`
/// (or a server's packets) put on this client's overlay.
pub(super) fn draw_boss_bars(&self, batch: &mut HudBatch) {
@@ -2202,15 +2200,58 @@ impl Game<'_> {
}
}
pub(super) struct DragonFireball {
- position: Vec3,
- previous: Vec3,
- velocity: Vec3,
+ /// Its entity id once a server's players track it (0 until then).
+ pub(super) id: u32,
+ pub(super) position: Vec3,
+ pub(super) previous: Vec3,
+ pub(super) velocity: Vec3,
age: i32,
}
+impl DragonFireball {
+ /// One a server sent (`handleAddEntity`).
+ pub(super) fn remote(id: u32, at: Vec3, velocity: Vec3) -> Self {
+ Self {
+ id,
+ position: at,
+ previous: at,
+ velocity,
+ age: 0,
+ }
+ }
+ /// `AbstractHurtingProjectile.tick` on a server's client: `applyInertia`
+ /// first (power 0.1, inertia 0.95 or 0.8 in water), then the move; only
+ /// the server decides what it hits.
+ pub(super) fn remote_step(&mut self, water: bool) {
+ self.previous = self.position;
+ self.age += 1;
+ let v = self.velocity;
+ // Vec3.normalize: a vector shorter than 1e-5 stays zero.
+ let l = (v.x * v.x + v.y * v.y + v.z * v.z).sqrt();
+ let n = if l < 1.0e-5 {
+ Vec3::new(0.0, 0.0, 0.0)
+ } else {
+ Vec3::new(v.x / l, v.y / l, v.z / l)
+ };
+ let inertia = f64::from(if water { 0.8f32 } else { 0.95f32 });
+ let v = Vec3::new(
+ (v.x + n.x * 0.1) * inertia,
+ (v.y + n.y * 0.1) * inertia,
+ (v.z + n.z * 0.1) * inertia,
+ );
+ self.velocity = v;
+ self.position = Vec3::new(
+ self.position.x + v.x,
+ self.position.y + v.y,
+ self.position.z + v.z,
+ );
+ }
+}
pub(super) struct Breath {
- at: [f64; 3],
- radius: f32,
- age: i32,
+ /// Its entity id once a server's players track it (0 until then).
+ pub(super) id: u32,
+ pub(super) at: [f64; 3],
+ pub(super) radius: f32,
+ pub(super) age: i32,
duration: i32,
from_dragon: bool,
}
@@ -3132,6 +3173,204 @@ impl Game<'_> {
let _ = self.activate_exit_portal();
self.fight.dirty = true;
}
+ /// `EnderDragon.checkCrystals`' search: the nearest crystal within 32
+ /// blocks on each axis.
+ fn nearest_crystal_to(&self, p: Vec3) -> Option {
+ self.crystals
+ .iter()
+ .enumerate()
+ .filter(|(_, c)| {
+ (c.position.x - p.x).abs() <= 32.0
+ && (c.position.y - p.y).abs() <= 32.0
+ && (c.position.z - p.z).abs() <= 32.0
+ })
+ .min_by(|a, b| {
+ let da = (a.1.position.x - p.x).powi(2)
+ + (a.1.position.y - p.y).powi(2)
+ + (a.1.position.z - p.z).powi(2);
+ let db = (b.1.position.x - p.x).powi(2)
+ + (b.1.position.y - p.y).powi(2)
+ + (b.1.position.z - p.z).powi(2);
+ da.total_cmp(&db)
+ })
+ .map(|(i, _)| i)
+ }
+}
+
+/// The dragon on a server's client (`ClientPacketListener.handleAddEntity`
+/// and the client side of `EnderDragon.aiStep`): the server flies it and
+/// sends its moves, health and phase; the client flaps its wings, keeps
+/// its flight history and places its parts, so it can be drawn and hit.
+impl Game<'_> {
+ /// `handleAddEntity` for `minecraft:ender_dragon`. Its constructor
+ /// sets the full health and the hovering phase until the server's
+ /// synched data says otherwise.
+ pub(super) fn spawn_remote_dragon(&mut self, id: i32, position: [f64; 3], yaw: f32) {
+ let at = Vec3::new(position[0], position[1], position[2]);
+ let mut d = Dragon::new(at, yaw, i64::from(id));
+ d.id = id as u32;
+ d.phase = Phase::from_id(10);
+ self.dragon = Some(d);
+ self.place_dragon_parts();
+ if let Some(d) = self.dragon.as_mut() {
+ for part in &mut d.parts {
+ part.1 = part.0;
+ }
+ }
+ }
+ /// The remote dragon, if `id` is it.
+ fn remote_dragon_mut(&mut self, id: i32) -> Option<&mut Dragon> {
+ self.dragon.as_mut().filter(|d| d.id as i32 == id)
+ }
+ /// `handleSetEntityData`: LivingEntity.DATA_HEALTH_ID and
+ /// EnderDragon.DATA_PHASE (`onSyncedDataUpdated` sets the phase).
+ pub(super) fn remote_dragon_data(&mut self, id: i32, values: &[(u8, i32, Value)]) {
+ let Some(d) = self.remote_dragon_mut(id) else {
+ return;
+ };
+ for (index, _, value) in values {
+ match (index, value) {
+ (9, Value::Float(health)) => d.health = *health,
+ (16, Value::Int(phase)) => d.set_phase(Phase::from_id(*phase)),
+ _ => {}
+ }
+ }
+ }
+ /// `handleSetEntityMotion`: the velocity its wings flap by.
+ pub(super) fn remote_dragon_motion(&mut self, id: i32, velocity: [f64; 3]) {
+ if let Some(d) = self.remote_dragon_mut(id) {
+ d.velocity = Vec3::new(velocity[0], velocity[1], velocity[2]);
+ }
+ }
+ /// `handleHurtAnimation` and `handleDamageEvent`: the red flash.
+ pub(super) fn remote_dragon_hurt(&mut self, id: i32) {
+ if let Some(d) = self.remote_dragon_mut(id) {
+ d.hurt_time = 10;
+ }
+ }
+ /// `handleRemoveEntities`, or a crystal removed: the healing beam
+ /// keeps pointing at the same crystal.
+ pub(super) fn remove_remote_dragon(&mut self, id: i32) {
+ if self.remote_dragon_mut(id).is_some() {
+ self.dragon = None;
+ return;
+ }
+ let Some(index) = self.crystals.iter().position(|c| c.id as i32 == id) else {
+ return;
+ };
+ if let Some(d) = self.dragon.as_mut() {
+ match d.nearest_crystal {
+ Some(n) if n == index => d.nearest_crystal = None,
+ Some(n) if n > index => d.nearest_crystal = Some(n - 1),
+ _ => {}
+ }
+ }
+ }
+ /// One client tick of the dragon. `lerp`: the step toward its last
+ /// sent position and yaw (LivingEntity's interpolation), and the
+ /// steps left including this one.
+ pub(super) fn tick_remote_dragon(&mut self, lerp: Option<([f64; 3], Option, u32)>) {
+ let Some(d) = self.dragon.as_mut() else {
+ return;
+ };
+ d.previous = d.position;
+ d.previous_yaw = d.yaw;
+ d.old_flap = d.flap;
+ d.ticks += 1;
+ d.hurt_time = (d.hurt_time - 1).max(0);
+ for part in &mut d.parts {
+ part.1 = part.0;
+ }
+ if let Some((target, yaw, steps)) = lerp {
+ let n = f64::from(steps.max(1));
+ d.position = Vec3::new(
+ d.position.x + (target[0] - d.position.x) / n,
+ d.position.y + (target[1] - d.position.y) / n,
+ d.position.z + (target[2] - d.position.z) / n,
+ );
+ if let Some(yaw) = yaw {
+ d.yaw += wrap(yaw - d.yaw) / steps.max(1) as f32;
+ }
+ }
+ if !d.alive() {
+ // EnderDragon.tickDeath on a client: it rises, and bursts
+ // from tick 180 to 200; the server removes it.
+ d.death_time += 1;
+ let (t, p) = (d.death_time, d.position);
+ if (180..=200).contains(&t) {
+ let r = &mut d.random;
+ let o = [
+ f64::from((r.next_float() - 0.5) * 8.0),
+ f64::from((r.next_float() - 0.5) * 4.0),
+ f64::from((r.next_float() - 0.5) * 8.0),
+ ];
+ self.particles.add(
+ &ParticleOptions::simple(ParticleType::ExplosionEmitter),
+ [p.x + o[0], p.y + 2.0 + o[1], p.z + o[2]],
+ [0.0; 3],
+ );
+ }
+ if let Some(d) = self.dragon.as_mut() {
+ d.position.y += 0.1;
+ for part in &mut d.parts {
+ part.0.y += 0.1;
+ }
+ }
+ return;
+ }
+ // processFlappingMovement and onFlap: its wing beats, and their sound.
+ let flap_was = (d.old_flap * std::f32::consts::TAU).cos();
+ let v = d.velocity;
+ let mut flap_speed = 0.2 / ((v.x.hypot(v.z) as f32) * 10.0 + 1.0);
+ flap_speed *= 2f32.powf(v.y as f32);
+ d.flap += if d.phase.sitting() { 0.1 } else { flap_speed };
+ let flap_now = (d.flap * std::f32::consts::TAU).cos();
+ let at = [d.position.x, d.position.y, d.position.z];
+ if flap_was <= -0.3 && flap_now >= -0.3 {
+ let pitch = 0.8 + self.world.random.next_float() * 0.3;
+ self.audio
+ .play("minecraft:entity.ender_dragon.flap", Some(at), 5.0, pitch);
+ }
+ let d = self.dragon.as_mut().expect("dragon");
+ if !d.phase.sitting() {
+ d.growl -= 1;
+ if d.growl < 0 {
+ d.growl = 200 + d.random.next_int_bound(200).unwrap_or(0);
+ let pitch = 0.8 + self.world.random.next_float() * 0.3;
+ self.audio
+ .play("minecraft:entity.ender_dragon.growl", Some(at), 2.5, pitch);
+ }
+ }
+ // checkCrystals' search runs on a client too (the healing beam).
+ let d = self.dragon.as_mut().expect("dragon");
+ if d.random.next_int_bound(10).unwrap_or(1) == 0 {
+ let p = d.position;
+ let nearest = self.nearest_crystal_to(p);
+ self.dragon.as_mut().expect("dragon").nearest_crystal = nearest;
+ }
+ let d = self.dragon.as_mut().expect("dragon");
+ d.yaw = wrap(d.yaw);
+ d.record();
+ self.place_dragon_parts();
+ }
+ /// The dragon's parts as a server's client attacks them: the server's
+ /// ids for `EnderDragonPart`s follow the dragon's.
+ pub(super) fn remote_dragon_part_hit(
+ &self,
+ eye: [f64; 3],
+ dir: [f64; 3],
+ reach: f64,
+ ) -> Option<(i32, f64, [f64; 3])> {
+ let d = self.dragon.as_ref().filter(|d| d.alive() && d.id != 0)?;
+ (0..8)
+ .filter_map(|i| {
+ let b = d.part_box(i);
+ let p = d.parts[i].0;
+ super::ray_box(eye, dir, reach, b)
+ .map(|t| ((d.id + 1 + i as u32) as i32, t, [p.x, p.y, p.z]))
+ })
+ .min_by(|a, b| a.1.total_cmp(&b.1))
+ }
}
#[cfg(test)]
diff --git a/crates/horizon-client/src/game/end.rs b/crates/horizon-client/src/game/end.rs
index 904c035c5..32d5113b0 100644
--- a/crates/horizon-client/src/game/end.rs
+++ b/crates/horizon-client/src/game/end.rs
@@ -129,15 +129,32 @@ impl EndState {
survive: false,
});
}
- /// One tick of a server's client: `step` moves each toward where the
- /// server put it.
- pub(super) fn step_remote_eyes(&mut self, mut step: impl FnMut(u32, [f64; 3]) -> [f64; 3]) {
+ /// `EyeOfEnder.tick` on a server's client: it moves by its last known
+ /// motion; only the server steers it.
+ pub(super) fn step_remote_eyes(&mut self) {
for e in &mut self.eyes {
e.previous = e.position;
e.life += 1;
- e.position = glam::DVec3::from_array(step(e.id, e.position.to_array()));
+ e.position += e.velocity;
}
}
+ /// A server's move: an eye has no interpolation handler, so
+ /// `moveOrInterpolateTo` puts it there at once. False when `id` isn't one.
+ pub(super) fn move_remote_eye(&mut self, id: u32, at: [f64; 3]) -> bool {
+ let Some(e) = self.eyes.iter_mut().find(|e| e.id == id) else {
+ return false;
+ };
+ e.position = glam::DVec3::from_array(at);
+ true
+ }
+ /// `handleSetEntityMotion` for an eye. False when `id` isn't one.
+ pub(super) fn remote_eye_motion(&mut self, id: u32, velocity: [f64; 3]) -> bool {
+ let Some(e) = self.eyes.iter_mut().find(|e| e.id == id) else {
+ return false;
+ };
+ e.velocity = glam::DVec3::from_array(velocity);
+ true
+ }
pub(super) fn remove_remote_eye(&mut self, id: u32) {
self.eyes.retain(|e| e.id != id);
}
@@ -1099,6 +1116,7 @@ impl Game<'_> {
/// EyeOfEnder.tick: fly, then drop (4 in 5) or shatter after 80 ticks.
fn tick_eyes(&mut self) {
let mut eyes = std::mem::take(&mut self.end.eyes);
+ let mut shattered = Vec::new();
eyes.retain_mut(|eye| {
eye.previous = eye.position;
let next = eye.position + eye.velocity;
@@ -1122,10 +1140,44 @@ impl Game<'_> {
extra: None,
};
self.spawn_item(stack, at, [vx, 0.2, vz], 0);
+ } else {
+ shattered.push(at.map(|v| v.floor() as i32));
}
false
});
self.end.eyes = eyes;
+ for pos in shattered {
+ self.level_event_at(2003, pos, 0);
+ }
+ }
+
+ /// `LevelEventHandler` 2003: a shattered eye's item crumbs and two rings
+ /// of portal particles drawn in towards it.
+ pub(super) fn eye_break_particles(&mut self, pos: [i32; 3]) {
+ let (x, y, z) = (
+ f64::from(pos[0]) + 0.5,
+ f64::from(pos[1]),
+ f64::from(pos[2]) + 0.5,
+ );
+ let crumbs = self.item_particle(&ItemStack::new("minecraft:ender_eye", 1));
+ for _ in 0..8 {
+ let r = self.particles.random();
+ let v = [
+ r.next_gaussian() * 0.15,
+ r.next_double() * 0.2,
+ r.next_gaussian() * 0.15,
+ ];
+ self.particles.add(&crumbs, [x, y, z], v);
+ }
+ let portal = ParticleOptions::simple(ParticleType::Portal);
+ let mut angle = 0.0f64;
+ while angle < std::f64::consts::PI * 2.0 {
+ let (c, s) = (angle.cos(), angle.sin());
+ let at = [x + c * 5.0, y - 0.4, z + s * 5.0];
+ self.particles.add(&portal, at, [c * -5.0, 0.0, s * -5.0]);
+ self.particles.add(&portal, at, [c * -7.0, 0.0, s * -7.0]);
+ angle += std::f64::consts::PI / 20.0;
+ }
}
/// `EyeOfEnder.tick` on a client: behind each eye (a quarter of its last
diff --git a/crates/horizon-client/src/game/fireworks.rs b/crates/horizon-client/src/game/fireworks.rs
index e6b4eb136..fd160a920 100644
--- a/crates/horizon-client/src/game/fireworks.rs
+++ b/crates/horizon-client/src/game/fireworks.rs
@@ -17,6 +17,8 @@ use horizon_core::{
random::RandomSource,
};
+pub(super) const FIREWORK_EXPLODE_EVENT: i8 = 17;
+
pub(super) struct Rocket {
/// The entity id a server tracks it by (0 until it is tracked).
pub id: u32,
@@ -261,6 +263,14 @@ impl Game<'_> {
/// `FireworkRocketEntity.explode` and `dealExplosionDamage`.
fn explode_rocket(&mut self, r: &Rocket) {
let at = [r.position.x, r.position.y, r.position.z];
+ // FireworkRocketEntity.explode broadcasts before discard. Send now:
+ // tick_lan removes missing tracked entities before flushing effects.
+ if r.id != 0
+ && let Some(host) = &self.lan
+ {
+ host.hub
+ .entity_event(r.id as i32, FIREWORK_EXPLODE_EVENT, at);
+ }
if !r.explosions.is_empty() {
let damage = 5.0 + r.explosions.len() as f32 * 2.0;
let from = r.position;
@@ -322,6 +332,16 @@ impl Game<'_> {
self.particles
.fireworks(at, v, r.explosions.clone(), camera);
}
+ /// FireworkRocketEntity.handleEntityEvent: use the synchronized item data.
+ pub(super) fn remote_firework_explosion(&mut self, id: i32) {
+ let camera = self.eye_position();
+ if let Some(r) = self.rockets.iter().find(|r| r.id == id as u32) {
+ let at = [r.position.x, r.position.y, r.position.z];
+ let v = [r.velocity.x, r.velocity.y, r.velocity.z];
+ self.particles
+ .fireworks(at, v, r.explosions.clone(), camera.unwrap_or(at));
+ }
+ }
/// `dealExplosionDamage` on a player that is not the rocket's
/// `attachedToEntity`: within 5 blocks, when the rocket sees its feet or
/// its middle (`ClipContext.Block.COLLIDER`, no fluids).
diff --git a/crates/horizon-client/src/game/fishing.rs b/crates/horizon-client/src/game/fishing.rs
index d454df406..22cb3774b 100644
--- a/crates/horizon-client/src/game/fishing.rs
+++ b/crates/horizon-client/src/game/fishing.rs
@@ -231,7 +231,7 @@ impl Game<'_> {
}
damage
}
- fn biome_name_at(&self, p: Vec3) -> Option {
+ pub(super) fn biome_name_at(&self, p: Vec3) -> Option {
let (x, y, z) = (p.x.floor() as i32, p.y.floor() as i32, p.z.floor() as i32);
let c = self.world.chunks.get(&(x >> 4, z >> 4))?;
Some(
diff --git a/crates/horizon-client/src/game/frames.rs b/crates/horizon-client/src/game/frames.rs
index 7f5e6c26a..d08c5f283 100644
--- a/crates/horizon-client/src/game/frames.rs
+++ b/crates/horizon-client/src/game/frames.rs
@@ -355,12 +355,23 @@ impl Game<'_> {
* glam::DMat4::from_rotation_y(y_rot.to_radians());
let light = self.world.light(f.pos[0], f.pos[1], f.pos[2]);
let packed = (u32::from(light >> 4) * 16) | ((u32::from(light & 15) * 16) << 8);
+ // ItemFrameRenderer.getBlockLightLevel: a glow frame's model is at least 5 bright.
+ let frame_light = if f.glow {
+ (light & 0xF0) | (light & 15).max(5)
+ } else {
+ light
+ };
+ let frame_packed =
+ (u32::from(frame_light >> 4) * 16) | ((u32::from(frame_light & 15) * 16) << 8);
let mut quad =
|m: &glam::DMat4, corners: [[f64; 3]; 4], uv: [[f32; 2]; 4], light: u32| {
- for (c, uv) in corners.iter().zip(uv) {
- let p = m.transform_point3(glam::DVec3::from_array(*c));
+ // The frame model is drawn with the entity shader, lit by its face normal.
+ let at = corners.map(|c| m.transform_point3(glam::DVec3::from_array(c)));
+ let n = (at[1] - at[0]).cross(at[2] - at[0]).normalize_or_zero();
+ let normal = [0, 1, 2].map(|k| (n[k].clamp(-1.0, 1.0) * 127.0) as i8);
+ for (p, uv) in at.into_iter().zip(uv) {
out.push(Vertex {
- normal: [0; 4],
+ normal: [normal[0], normal[1], normal[2], -127],
pos: p.as_vec3().to_array(),
uv,
layer: 0,
@@ -393,7 +404,7 @@ impl Game<'_> {
]
}),
map_uv(r, *uv),
- packed,
+ frame_packed,
);
}
}
diff --git a/crates/horizon-client/src/game/hand.rs b/crates/horizon-client/src/game/hand.rs
index a7218ff3c..d91d3b979 100644
--- a/crates/horizon-client/src/game/hand.rs
+++ b/crates/horizon-client/src/game/hand.rs
@@ -151,6 +151,7 @@ impl Game<'_> {
before(self.painting_hit().map(|h| h.1))
|| before(self.frame_hit().map(|h| h.1))
|| before(self.armor_stand_hit().map(|h| h.1))
+ || before(self.cushion_hit().map(|h| h.1))
|| before(self.boat_hit().map(|h| h.1))
|| before(self.cart_hit().map(|h| h.1))
|| before(self.dragon_hit().map(|h| h.1))
diff --git a/crates/horizon-client/src/game/integrated.rs b/crates/horizon-client/src/game/integrated.rs
index 14a18caa6..dd1c04e23 100644
--- a/crates/horizon-client/src/game/integrated.rs
+++ b/crates/horizon-client/src/game/integrated.rs
@@ -222,7 +222,6 @@ impl Game<'_> {
options.report = None;
options.server = None;
options.world = None;
- options.dev_mods.clear();
self.join_world_rest()?;
let (res, pack, settings) = (self.res.clone(), self.pack.clone(), self.settings.clone());
let flags = Arc::new(Flags::default());
@@ -292,6 +291,9 @@ impl Game<'_> {
launch.create.as_ref(),
)?)?;
let saved = Box::new(self.level.clone());
+ // IntegratedServer runs the server mods; the window the client mods.
+ self.mods.dev = self.options.dev_mods.clone();
+ self.start_mods();
// Math.max(2, options.renderDistance): past vanilla's 32 when the
// window's render distance is (Horizon's options go to 64).
let view_distance = self.settings.render_distance.max(2);
diff --git a/crates/horizon-client/src/game/item_draw.rs b/crates/horizon-client/src/game/item_draw.rs
index 1d79f077d..b6b7e2fb7 100644
--- a/crates/horizon-client/src/game/item_draw.rs
+++ b/crates/horizon-client/src/game/item_draw.rs
@@ -196,13 +196,31 @@ impl Game<'_> {
skins: &self.skin_downloads,
}
}
- /// The client level as item models read it.
+ /// The client level as item models read it: worked out once a frame (a
+ /// screen of items read the same level, its sky sampled once).
pub(super) fn item_level(&self) -> LevelState {
let dimension = match self.dim {
Dim::Overworld => "minecraft:overworld",
Dim::Nether => "minecraft:the_nether",
Dim::End => "minecraft:the_end",
};
+ let key = (
+ self.frames,
+ self.world.game_time,
+ self.world.day_time,
+ dimension,
+ );
+ let mut cached = self.item_level_cache.borrow_mut();
+ if let Some((at, level)) = cached.as_ref()
+ && *at == key
+ {
+ return level.clone();
+ }
+ let level = self.level_state_now(dimension);
+ *cached = Some((key, level.clone()));
+ level
+ }
+ fn level_state_now(&self, dimension: &'static str) -> LevelState {
let sky = self
.dim_environment()
.sample(self.world.day_time, None, None);
diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs
index 50fa222e6..63aea7e9f 100644
--- a/crates/horizon-client/src/game/lan.rs
+++ b/crates/horizon-client/src/game/lan.rs
@@ -14,7 +14,7 @@ use horizon_client::{
hub::{HostEvent, HostState, Hub, HubConfig, TickCtx},
playerdata,
},
- mobs::{Mob, leash::Holder},
+ mobs::{Mob, Who, leash::Holder},
text::Text,
};
use horizon_core::geometry::Vec3;
@@ -71,6 +71,8 @@ pub(super) struct LanHost {
/// Guests' commands waiting for the hub to be back in `self.lan`, where
/// a command can reach every player (game/lan_actions.rs).
pub commands: Vec,
+ /// Mods' quiet commands waiting to run a few a tick (game/lan_quiet.rs).
+ pub quiet: std::collections::VecDeque,
/// The entities the guests track, described again every tick
/// (`lan_entities`) over last tick's states.
states: lan::entity::States,
@@ -145,6 +147,7 @@ pub(super) fn baby_index(kind: &str) -> Option {
fn mob_meta(
m: &Mob,
hub: &Hub,
+ player_entity: Option,
meta: &mut Vec<(u8, i32, Value)>,
defaults: &mut Vec<(u8, i32, Value)>,
) {
@@ -194,6 +197,32 @@ fn mob_meta(
let e = &m.extra;
let id = |registry: &str, name: &str| hub.registry_id(registry, &format!("minecraft:{name}"));
match kind {
+ "minecraft:guardian" | "minecraft:elder_guardian" => {
+ // Guardian.DATA_ID_MOVING and DATA_ID_ATTACK_TARGET.
+ if e.flag2 {
+ meta.push((16, serializer::BOOLEAN, Value::Bool(true)));
+ }
+ let target = if e.flag {
+ m.ai.target.and_then(|who| match who {
+ Who::Player => player_entity,
+ Who::Mob(id) => Some(id as i32),
+ })
+ } else {
+ None
+ };
+ if let Some(target) = target {
+ meta.push((17, serializer::INT, Value::Int(target)));
+ }
+ defaults.push((16, serializer::BOOLEAN, Value::Bool(false)));
+ defaults.push((17, serializer::INT, Value::Int(0)));
+ }
+ // Bat.DATA_ID_FLAGS: resting in bit 0, flying by default.
+ "minecraft:bat" => {
+ if e.flag {
+ meta.push((16, serializer::BYTE, Value::Byte(1)));
+ }
+ defaults.push((16, serializer::BYTE, Value::Byte(0)));
+ }
// Sheep.DATA_WOOL_ID: the colour in the low nibble, 16 when sheared.
"minecraft:sheep" => {
let value = (e.color & 15) | if e.sheared { 16 } else { 0 };
@@ -596,6 +625,7 @@ impl Game<'_> {
waypoint_dead: Default::default(),
border_sent: None,
commands: Vec::new(),
+ quiet: std::collections::VecDeque::new(),
states: Default::default(),
});
self.audio.capture = Some(Vec::new());
@@ -634,6 +664,7 @@ impl Game<'_> {
self.world.light_log = None;
self.audio.capture = None;
self.lan_events.clear();
+ self.lan_critical_hits.clear();
self.streaming.extra.clear();
if announce {
self.system_message(Text::translate(
@@ -820,12 +851,13 @@ impl Game<'_> {
for m in self.mobs.iter().filter(|m| !m.removed) {
// The lead's holder: the player the mob sees (a guest, else the
// host) or another mob. A fence knot isn't an entity here.
+ let player_entity = mob_players
+ .get(&m.id)
+ .and_then(|conn| hub.guest(*conn))
+ .map(|g| g.entity_id)
+ .or(host_entity);
let leash = m.extra.leash.and_then(|h| match h {
- Holder::Player => mob_players
- .get(&m.id)
- .and_then(|conn| hub.guest(*conn))
- .map(|g| g.entity_id)
- .or(host_entity),
+ Holder::Player => player_entity,
Holder::Mob(id) => Some(id as i32),
Holder::Knot(_) => None,
});
@@ -835,7 +867,7 @@ impl Game<'_> {
e.on_ground = m.on_ground;
e.velocity = [m.velocity.x, m.velocity.y, m.velocity.z];
e.needs_sync = m.needs_sync;
- mob_meta(m, hub, &mut e.meta, &mut e.defaults);
+ mob_meta(m, hub, player_entity, &mut e.meta, &mut e.defaults);
mob_equipment(m, hub, &mut e.equipment);
self.mob_attributes(m, &mut e.attributes);
e.leash = leash;
@@ -960,6 +992,9 @@ impl Game<'_> {
event @ (HostEvent::Chat { .. } | HostEvent::Warn(_) | HostEvent::Error(_)) => {
self.lan_log_event(event);
}
+ HostEvent::PlayerChat { sender, text } => {
+ self.mod_player_chat(super::server_mods::Who::Uuid(sender), &text);
+ }
HostEvent::Joined(conn) => {
self.lan_guest_joined(&mut host, conn, state.as_ref(), entities, spawn);
if self.options.dedicated.is_some()
@@ -1029,6 +1064,14 @@ impl Game<'_> {
}
host.states = states;
}
+ if !host.quiet.is_empty() {
+ self.lan = Some(host);
+ self.run_quiet_commands();
+ host = self
+ .lan
+ .take()
+ .expect("the hub is back after the quiet commands");
+ }
self.lan_flush_effects(&mut host);
if !paused {
self.tick_guests(&mut host);
@@ -1162,8 +1205,30 @@ impl Game<'_> {
self.lan_entity_events.push((id, event, at));
}
}
+ /// `ServerPlayer.crit`: this window's emitter, and an animation for the
+ /// attacker's client and observers once the hub is back in place.
+ pub(super) fn critical_hit(
+ &mut self,
+ attacker: Option,
+ id: i32,
+ feet: [f64; 3],
+ width: f64,
+ height: f64,
+ ) {
+ use horizon_client::particles::{ParticleOptions, ParticleType};
+ self.particles.tracking_emitter(
+ ParticleOptions::simple(ParticleType::Crit),
+ feet,
+ width,
+ height,
+ );
+ if self.audio.capture.is_some() {
+ self.lan_critical_hits.push((attacker, id));
+ }
+ }
/// Send the tick's level events, sounds and mob animations.
fn lan_flush_effects(&mut self, host: &mut LanHost) {
+ let critical_hits = std::mem::take(&mut self.lan_critical_hits);
for (kind, pos, data, acting) in std::mem::take(&mut self.lan_events) {
if kind == 2001 {
host.broken
@@ -1236,6 +1301,14 @@ impl Game<'_> {
seen.insert(m.id, (m.hurt_time, dead, swing));
}
host.mob_state.insert(self.dim, seen);
+ for (attacker, target) in critical_hits {
+ let Some(attacker) = attacker.map_or(Some(host.host_entity), |conn| {
+ host.hub.guest(conn).map(|g| g.entity_id)
+ }) else {
+ continue;
+ };
+ host.hub.critical_hit(attacker, target);
+ }
}
/// Keep the chunks around every guest loaded.
@@ -1403,7 +1476,11 @@ impl Game<'_> {
let tag = if secure { "" } else { "[Not Secure] " };
super::dedicated::log("INFO", &format!("{tag}{line}"));
}
- HostEvent::Chat { line, .. } => self.system_message(Text::literal(line)),
+ // Player chat (and `/say`'s or `/me`'s, which are chat messages
+ // too): the host's Chat option decides, as `ChatListener` does
+ // for the guests' packets. Command feedback is a system message
+ // and takes the system-message permission instead.
+ HostEvent::Chat { line, .. } => self.player_message(Text::literal(line)),
HostEvent::Warn(line) if dedicated => super::dedicated::log("WARN", &line),
HostEvent::Warn(line) => log::warn!("{line}"),
HostEvent::Error(line) if dedicated => super::dedicated::log("ERROR", &line),
diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs
index bbb0c37c0..43bb573dc 100644
--- a/crates/horizon-client/src/game/lan_actions.rs
+++ b/crates/horizon-client/src/game/lan_actions.rs
@@ -77,6 +77,10 @@ pub(super) struct GuestPlay {
last_inventory: u64,
/// `Entity.portalProcess` and `portalCooldown` (game/server_travel.rs).
pub(super) portal: horizon_client::nether_portal::PortalState,
+ /// `ServerPlayer.wonGame`: the End poem is showing, the player waits for
+ /// its client's PERFORM_RESPAWN (`respawn_requested`).
+ pub(super) won_game: bool,
+ pub(super) respawn_requested: bool,
/// `Player.takeXpDelay`: ticks until the next orb can be taken.
xp_delay: u32,
/// What last killed the guest outright (`/kill`): the death message's key.
@@ -94,11 +98,14 @@ pub(super) struct GuestPlay {
/// A guest's command waiting for the hub (`LanHost::commands`).
pub(super) struct GuestCommand {
- conn: u32,
- command: String,
- permission: u8,
+ pub(super) conn: u32,
+ pub(super) command: String,
+ pub(super) permission: u8,
/// An integrated server's: no level teleports (yet).
- integrated: bool,
+ pub(super) integrated: bool,
+ /// A mod's quiet command (game/lan_quiet.rs): its output suppressed, as
+ /// `CommandSourceStack.withSuppressedOutput` does.
+ pub(super) quiet: bool,
}
/// The game's own state while a guest's is swapped in (`enter_guest`), and
@@ -872,7 +879,11 @@ impl Game<'_> {
.filter(|g| host.hub.here(g))
.filter(|g| {
g.menu.as_ref().is_some_and(|m| {
- m.slots.valid(&self.world) && !m.slots.still_valid(&self.world, g, m.pos)
+ (m.slots.valid(&self.world) && !m.slots.still_valid(&self.world, g, m.pos))
+ || m.pos.is_some_and(|k| {
+ k.1 == super::minecarts::CART_KEY_Y
+ && !self.chest_vehicle_still_valid(k.2, g)
+ })
})
})
.map(|g| g.conn)
@@ -886,6 +897,35 @@ impl Game<'_> {
}
}
+ /// `ContainerEntity.isChestVehicleStillValid`: the chest minecart or chest
+ /// boat is still there and within the guest's entity interaction range
+ /// plus 4 of its box.
+ fn chest_vehicle_still_valid(&self, id: i32, g: &horizon_client::lan::guest::Guest) -> bool {
+ let bounds = self
+ .carts
+ .iter()
+ .find(|c| c.id as i32 == id)
+ .map(|c| c.bounds())
+ .or_else(|| {
+ self.boats
+ .iter()
+ .find(|b| b.id as i32 == id)
+ .map(|b| b.bounds())
+ });
+ let Some(b) = bounds else {
+ return false;
+ };
+ let eye = [g.position[0], g.position[1] + g.eye_height(), g.position[2]];
+ let (lo, hi) = (b.min_position(), b.max_position());
+ let d = [
+ eye[0] - eye[0].clamp(lo.x, hi.x),
+ eye[1] - eye[1].clamp(lo.y, hi.y),
+ eye[2] - eye[2].clamp(lo.z, hi.z),
+ ];
+ let range = if g.creative() { 5.0 } else { 3.0 } + 4.0;
+ d[0] * d[0] + d[1] * d[1] + d[2] * d[2] < range * range
+ }
+
/// A packet from a guest that the hub does not handle itself.
pub(super) fn lan_guest_packet(&mut self, host: &mut LanHost, conn: u32, packet: S) {
let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) else {
@@ -1210,6 +1250,9 @@ impl Game<'_> {
self.guest_carried_back(host, gi);
}
S::ChatCommand(c) => self.guest_chat_command(host, conn, &c.command),
+ S::CustomPayload(p) if p.channel == horizon_protocol::quiet::CHANNEL => {
+ self.guest_quiet_commands(host, conn, &p.data)
+ }
S::ChatCommandSigned(p) => self.guest_signed_command(host, conn, p),
S::ChangeDifficulty(p) if host.hub.guests[gi].permission >= 2 => {
if !self.level.difficulty_locked && (0..=3).contains(&p.difficulty) {
@@ -1308,6 +1351,13 @@ impl Game<'_> {
self.guest_suggestions(host, conn, &payload);
}
S::PlaceRecipe(p) => self.lan_place_recipe(host, conn, &p),
+ // handleClientCommand: PERFORM_RESPAWN after the poem leaves the End
+ // (the next server tick sends the player on).
+ S::ClientCommand(c)
+ if c.action == 0 && host.play.get(&conn).is_some_and(|p| p.won_game) =>
+ {
+ host.play.entry(conn).or_default().respawn_requested = true;
+ }
S::ClientCommand(c) if c.action == 0 => self.guest_respawn(host, conn),
// ServerStatsCounter.sendStats.
S::ClientCommand(c) if c.action == 1 => {
@@ -1533,6 +1583,10 @@ impl Game<'_> {
);
return;
}
+ // A server mod's command, for every player whatever their level.
+ if self.guest_server_mod_command(host, conn, command.trim_start_matches('/')) {
+ return;
+ }
// An integrated server runs every player's commands at their own
// permission level (`CommandSourceStack.permissions`); a LAN world
// follows its Allow Commands, and a dedicated server leaves it to
@@ -1559,6 +1613,7 @@ impl Game<'_> {
command: command.trim_start_matches('/').to_owned(),
permission,
integrated,
+ quiet: false,
});
}
@@ -1573,41 +1628,54 @@ impl Game<'_> {
return;
}
let c = host.commands.remove(0);
- let mut host = self.lan.take().expect("checked above");
- let Some(swap) = self.enter_guest(&mut host, c.conn) else {
- self.lan = Some(host);
- continue;
- };
- self.lan = Some(host);
- self.chat_capture = Some(Vec::new());
- let level = std::mem::replace(&mut self.ui.registries.permission, c.permission);
- self.run_command(&c.command);
- self.ui.registries.permission = level;
- let lines = self.chat_capture.take().unwrap_or_default();
- let mut host = self.lan.take().expect("a command puts the hub back");
- self.leave_guest(&mut host, swap);
- host.hub.sync_all_effects();
- if !c.integrated {
- // A teleport into another level happens before its feedback is sent.
- self.run_level_teleports(&mut host);
- }
- // What the command earned is announced before its feedback.
- self.announce_guest_advancements(&mut host);
- for line in lines {
- host.hub.send_to(
- c.conn,
- &C::SystemChat(play::SystemChat {
- content: horizon_client::lan::data::component_nbt(&line.component()),
- overlay: false,
- }),
- );
- }
- // The other operators hear of it; never the player whose command it was.
- self.send_admin_feedback(&mut host, Some(c.conn));
- self.lan = Some(host);
+ self.run_guest_command(c);
}
}
+ /// One guest's command as that guest, its feedback sent to them; false
+ /// when it failed (or the guest is gone).
+ pub(super) fn run_guest_command(&mut self, c: GuestCommand) -> bool {
+ let Some(mut host) = self.lan.take() else {
+ return false;
+ };
+ let Some(swap) = self.enter_guest(&mut host, c.conn) else {
+ self.lan = Some(host);
+ return false;
+ };
+ self.lan = Some(host);
+ self.chat_capture = Some(Vec::new());
+ let level = std::mem::replace(&mut self.ui.registries.permission, c.permission);
+ let ok = if c.quiet {
+ self.run_command_quietly(&c.command)
+ } else {
+ self.run_command(&c.command)
+ };
+ self.ui.registries.permission = level;
+ let lines = self.chat_capture.take().unwrap_or_default();
+ let mut host = self.lan.take().expect("a command puts the hub back");
+ self.leave_guest(&mut host, swap);
+ host.hub.sync_all_effects();
+ if !c.integrated {
+ // A teleport into another level happens before its feedback is sent.
+ self.run_level_teleports(&mut host);
+ }
+ // What the command earned is announced before its feedback.
+ self.announce_guest_advancements(&mut host);
+ for line in lines {
+ host.hub.send_to(
+ c.conn,
+ &C::SystemChat(play::SystemChat {
+ content: horizon_client::lan::data::component_nbt(&line.component()),
+ overlay: false,
+ }),
+ );
+ }
+ // The other operators hear of it; never the player whose command it was.
+ self.send_admin_feedback(&mut host, Some(c.conn));
+ self.lan = Some(host);
+ ok
+ }
+
/// The `broadcastToAdmins` lines a command queued (`source`: the guest
/// whose it was; none for the host's or the console's): every other
/// operator gets them, and the console logs them as the server's own
diff --git a/crates/horizon-client/src/game/lan_chat.rs b/crates/horizon-client/src/game/lan_chat.rs
index f678480ab..621955967 100644
--- a/crates/horizon-client/src/game/lan_chat.rs
+++ b/crates/horizon-client/src/game/lan_chat.rs
@@ -28,7 +28,11 @@ impl Game<'_> {
/// SignableCommand.of: the command's message arguments, by name, with
/// their text, not past a redirect to the root (`execute ... run`).
- fn signable_arguments(&mut self, permission: u8, command: &str) -> Vec<(String, String)> {
+ pub(super) fn signable_arguments(
+ &mut self,
+ permission: u8,
+ command: &str,
+ ) -> Vec<(String, String)> {
let parse = self.parse_as(permission, command);
let nodes = &vanilla::game_dispatcher().nodes;
let root = parse.context.root;
diff --git a/crates/horizon-client/src/game/lan_entities.rs b/crates/horizon-client/src/game/lan_entities.rs
index cd4ba2fb8..529666590 100644
--- a/crates/horizon-client/src/game/lan_entities.rs
+++ b/crates/horizon-client/src/game/lan_entities.rs
@@ -245,6 +245,16 @@ impl Game<'_> {
}
out.push(e);
}
+ for c in &self.cushions {
+ let p = c.position;
+ let mut e = state(c.id, "minecraft:cushion", [p.x, p.y, p.z], c.yaw, false);
+ // Cushion.DATA_COLOR, set unless it is the default white.
+ if c.color != 0 {
+ e.meta
+ .push((8, serializer::DYE_COLOR, Value::Id(i32::from(c.color))));
+ }
+ out.push(e);
+ }
self.lan_projectile_states(host_entity, hub, out);
for o in &self.others {
let p = o.position;
@@ -279,6 +289,10 @@ impl Game<'_> {
self.bolts.iter_mut().for_each(|b| give(&mut b.id));
self.potions.thrown_ids().for_each(&mut give);
self.end.eye_ids().for_each(&mut give);
+ self.breath_clouds.iter_mut().for_each(|b| give(&mut b.id));
+ self.dragon_fireballs
+ .iter_mut()
+ .for_each(|b| give(&mut b.id));
}
/// Primed TNT, falling blocks, projectiles, tridents, firework rockets and
@@ -435,6 +449,35 @@ impl Game<'_> {
}
out.push(e);
}
+ // The dragon's breath clouds (AreaEffectCloud's DATA_RADIUS,
+ // DATA_WAITING for its 20-tick wait, and DATA_PARTICLE): the
+ // client draws them from these (`tick_remote_clouds`).
+ let breath = self.network_particle(
+ &horizon_client::particles::ParticleOptions::simple(
+ horizon_client::particles::ParticleType::DragonBreath,
+ ),
+ None,
+ );
+ for b in self.dragon_fireballs.iter().filter(|b| b.id != 0) {
+ let p = b.position;
+ let mut e = state(b.id, "minecraft:dragon_fireball", at(p), 0.0, false);
+ e.velocity = [b.velocity.x, b.velocity.y, b.velocity.z];
+ out.push(e);
+ }
+ for b in self.breath_clouds.iter().filter(|b| b.id != 0) {
+ let mut e = state(b.id, "minecraft:area_effect_cloud", b.at, 0.0, false);
+ e.meta.push((8, serializer::FLOAT, Value::Float(b.radius)));
+ if b.age < 20 {
+ e.meta.push((9, serializer::BOOLEAN, Value::Bool(true)));
+ }
+ if let Some(p) = &breath {
+ e.meta
+ .push((10, serializer::PARTICLE, Value::Particle(p.clone())));
+ }
+ e.defaults
+ .push((9, serializer::BOOLEAN, Value::Bool(false)));
+ out.push(e);
+ }
out.extend(self.lan_dragon_state());
}
}
diff --git a/crates/horizon-client/src/game/lan_particles.rs b/crates/horizon-client/src/game/lan_particles.rs
index 0e35392bf..c41fdf1ad 100644
--- a/crates/horizon-client/src/game/lan_particles.rs
+++ b/crates/horizon-client/src/game/lan_particles.rs
@@ -25,6 +25,33 @@ fn rgb(c: [f32; 4]) -> [f32; 3] {
}
impl Game<'_> {
+ /// `SonicBoom.tick` uses `ServerLevel.sendParticles` for each ray point.
+ /// Keep the local event's rendering; connected players receive one at rest.
+ pub(super) fn lan_sonic_boom(&self, options: &ParticleOptions, at: [f64; 3]) {
+ let (Some(host), Some(particle)) = (&self.lan, self.network_particle(options, None)) else {
+ return;
+ };
+ let packet = C::LevelParticles(play::LevelParticles {
+ particle,
+ override_limiter: false,
+ always_show: false,
+ pos: at,
+ dist: [0.0; 3],
+ max_speed: [0.0; 3],
+ count: 0,
+ randomization: 0,
+ });
+ for g in host.hub.guests.iter().filter(|g| host.hub.here(g)) {
+ // ServerLevel compares the player's block centre, strictly within 32.
+ let distance: f64 = (0..3)
+ .map(|k| (g.position[k].floor() + 0.5 - at[k]).powi(2))
+ .sum();
+ if distance < 32.0 * 32.0 {
+ g.send(&packet);
+ }
+ }
+ }
+
/// `ParticleTypes.STREAM_CODEC`'s value for `options`: the type's
/// network id and the data it carries (`state` for the block particles).
/// None while not serving, or for a particle whose data isn't sent from
@@ -68,7 +95,11 @@ impl Game<'_> {
/// The options a server's particle stands for (None for a type or data
/// this client doesn't draw).
- fn received_particle(&mut self, p: &Particle, at: [f64; 3]) -> Option {
+ pub(super) fn received_particle(
+ &mut self,
+ p: &Particle,
+ at: [f64; 3],
+ ) -> Option {
let name = self
.remote
.as_ref()?
diff --git a/crates/horizon-client/src/game/lan_pvp.rs b/crates/horizon-client/src/game/lan_pvp.rs
index 467c4537c..c7c6b6525 100644
--- a/crates/horizon-client/src/game/lan_pvp.rs
+++ b/crates/horizon-client/src/game/lan_pvp.rs
@@ -32,6 +32,8 @@ pub(super) enum Victim {
/// The attacker's side of the hit.
pub(super) struct Strike {
+ /// The attacking guest, kept after their turn through `as_guest` ends.
+ attacker: Option,
amount: f32,
scale: f32,
crit: bool,
@@ -119,6 +121,7 @@ impl Game<'_> {
}
self.swing_arm();
Some(Strike {
+ attacker: self.lan_acting,
amount,
scale,
crit,
@@ -250,6 +253,25 @@ impl Game<'_> {
Victim::Host => land(self),
Victim::Guest(conn) => self.guest_struck_by(host, conn, land).unwrap_or(false),
};
+ if hurt && s.crit {
+ let target = match victim {
+ Victim::Host => self.player.as_ref().map(|p| (host.host_entity, p)),
+ Victim::Guest(conn) => host
+ .hub
+ .guest(conn)
+ .and_then(|g| g.motion.as_deref().map(|p| (g.entity_id, p))),
+ };
+ if let Some((id, p)) = target {
+ let d = p.dimensions;
+ self.critical_hit(
+ s.attacker,
+ id,
+ [p.position.x, p.position.y, p.position.z],
+ f64::from(d.width),
+ f64::from(d.height),
+ );
+ }
+ }
let sound = match (hurt, s.crit, s.scale > 0.9) {
(false, ..) => "minecraft:entity.player.attack.nodamage",
(true, true, _) => "minecraft:entity.player.attack.crit",
diff --git a/crates/horizon-client/src/game/lan_quiet.rs b/crates/horizon-client/src/game/lan_quiet.rs
new file mode 100644
index 000000000..63d382c02
--- /dev/null
+++ b/crates/horizon-client/src/game/lan_quiet.rs
@@ -0,0 +1,165 @@
+//! Quiet commands (`horizon_protocol::quiet`): a Horizon client's mod sends a
+//! batch of command lines in one payload, and the server runs each exactly as
+//! that player typing it would (`guest_command`'s checks, the player's own
+//! permission level, at the time it runs) with its output suppressed, as
+//! vanilla's functions run (`CommandSourceStack.withSuppressedOutput`): no
+//! feedback and no failure in anyone's chat. A batch waits in a queue and runs
+//! [`PER_TICK`] lines a player a tick, so a mod can't flood a server; each
+//! finished batch is one line in the server's log.
+use super::{Game, lan::LanHost, lan_actions::GuestCommand};
+use horizon_protocol::quiet;
+use std::collections::{HashMap, VecDeque};
+
+/// Lines a player may have waiting at most; a batch past it loses the rest.
+pub(super) const PENDING: usize = 4096;
+/// Lines run a player a tick at most.
+pub(super) const PER_TICK: usize = 64;
+
+/// A batch waiting to run.
+pub(super) struct QuietBatch {
+ conn: u32,
+ mod_id: String,
+ lines: VecDeque,
+ ran: usize,
+ failed: usize,
+ /// Lines past [`PENDING`], never run.
+ dropped: usize,
+}
+
+impl Game<'_> {
+ /// A `horizon:quiet_commands` payload from `conn`: queued (or logged as
+ /// refused when it breaks the payload's caps).
+ pub(super) fn guest_quiet_commands(&mut self, host: &mut LanHost, conn: u32, data: &[u8]) {
+ let Some(name) = host.hub.guest(conn).map(|g| g.name.clone()) else {
+ return;
+ };
+ let batch = match quiet::decode(data) {
+ Ok(b) => b,
+ Err(e) => {
+ super::dedicated::log(
+ "WARN",
+ &format!("{name} sent quiet commands that were refused: {e}"),
+ );
+ return;
+ }
+ };
+ let waiting: usize = host
+ .quiet
+ .iter()
+ .filter(|b| b.conn == conn)
+ .map(|b| b.lines.len())
+ .sum();
+ let room = PENDING.saturating_sub(waiting);
+ let dropped = batch.lines.len().saturating_sub(room);
+ host.quiet.push_back(QuietBatch {
+ conn,
+ mod_id: batch.mod_id,
+ lines: batch.lines.into_iter().take(room).collect(),
+ ran: 0,
+ failed: 0,
+ dropped,
+ });
+ }
+
+ /// The waiting batches, [`PER_TICK`] lines a player this tick, in order.
+ pub(super) fn run_quiet_commands(&mut self) {
+ let mut left: HashMap = HashMap::new();
+ let mut i = 0;
+ loop {
+ let Some(host) = self.lan.as_mut() else {
+ return;
+ };
+ let Some(batch) = host.quiet.get_mut(i) else {
+ return;
+ };
+ let conn = batch.conn;
+ let budget = left.entry(conn).or_insert(PER_TICK);
+ let gone = host.hub.guest(conn).is_none();
+ if gone || batch.lines.is_empty() {
+ let batch = host.quiet.remove(i).expect("got it above");
+ if !gone {
+ let name = host
+ .hub
+ .guest(conn)
+ .map_or(String::new(), |g| g.name.clone());
+ super::dedicated::log("INFO", &summary(&name, &batch));
+ }
+ continue;
+ }
+ if *budget == 0 {
+ i += 1;
+ continue;
+ }
+ *budget -= 1;
+ let line = batch.lines.pop_front().expect("not empty");
+ let ok = self.run_quiet_line(conn, &line);
+ if let Some(batch) = self.lan.as_mut().and_then(|h| h.quiet.get_mut(i)) {
+ batch.ran += 1;
+ batch.failed += usize::from(!ok);
+ }
+ }
+ }
+
+ /// One line as `guest_command` would run it for `conn`, quietly. False
+ /// when it is refused or fails. A command that carries a chat message
+ /// (`say`, `msg`...) is refused: it would need the player's signature.
+ fn run_quiet_line(&mut self, conn: u32, line: &str) -> bool {
+ let allowed = self.options.dedicated.is_some() || self.allow_commands();
+ let Some(host) = self.lan.as_ref() else {
+ return false;
+ };
+ let integrated = host.hub.config.integrated;
+ if !integrated && (!host.settings.guest_commands || !allowed) {
+ return false;
+ }
+ let Some(permission) = host.hub.guest(conn).map(|g| g.permission) else {
+ return false;
+ };
+ let command = line.trim_start_matches('/');
+ if !self.signable_arguments(permission, command).is_empty() {
+ return false;
+ }
+ self.run_guest_command(GuestCommand {
+ conn,
+ command: command.to_owned(),
+ permission,
+ integrated,
+ quiet: true,
+ })
+ }
+}
+
+/// The batch's line in the server's log.
+fn summary(name: &str, b: &QuietBatch) -> String {
+ let mut line = format!(
+ "{name} ran {} quiet command(s) from mod {}: {} failed",
+ b.ran, b.mod_id, b.failed
+ );
+ if b.dropped > 0 {
+ line += &format!(", {} dropped (more than {PENDING} waiting)", b.dropped);
+ }
+ line
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn the_log_line_counts_the_batch() {
+ let b = QuietBatch {
+ conn: 1,
+ mod_id: "orbital".into(),
+ lines: VecDeque::new(),
+ ran: 12,
+ failed: 3,
+ dropped: 0,
+ };
+ assert_eq!(
+ summary("Alex", &b),
+ "Alex ran 12 quiet command(s) from mod orbital: 3 failed"
+ );
+ let b = QuietBatch { dropped: 5, ..b };
+ assert!(summary("Alex", &b).ends_with(", 5 dropped (more than 4096 waiting)"));
+ }
+}
diff --git a/crates/horizon-client/src/game/lan_trading.rs b/crates/horizon-client/src/game/lan_trading.rs
index 1bec9809d..7beeeba2f 100644
--- a/crates/horizon-client/src/game/lan_trading.rs
+++ b/crates/horizon-client/src/game/lan_trading.rs
@@ -144,15 +144,16 @@ impl Game<'_> {
.offers
.iter()
.filter_map(|o| {
- let cost =
- |st: &ItemStack, hub: &mut horizon_client::lan::hub::Hub| play::NetCost {
- item: hub.item_id(&st.item),
- count: st.count as i32,
- };
+ let cost = |st: &ItemStack, hub: &mut horizon_client::lan::hub::Hub| {
+ hub.stack(&Some(st.clone())).map(play::NetCost::from)
+ };
Some(play::NetOffer {
- cost_a: cost(&o.base_cost_a.stack, &mut host.hub),
+ cost_a: cost(&o.base_cost_a.stack, &mut host.hub)?,
result: host.hub.stack(&Some(o.result.clone()))?,
- cost_b: o.cost_b.as_ref().map(|b| cost(&b.stack, &mut host.hub)),
+ cost_b: match &o.cost_b {
+ Some(b) => Some(cost(&b.stack, &mut host.hub)?),
+ None => None,
+ },
out_of_stock: o.out_of_stock(),
uses: o.uses,
max_uses: o.max_uses,
diff --git a/crates/horizon-client/src/game/lan_vehicles.rs b/crates/horizon-client/src/game/lan_vehicles.rs
index f16a1e37d..00795aa62 100644
--- a/crates/horizon-client/src/game/lan_vehicles.rs
+++ b/crates/horizon-client/src/game/lan_vehicles.rs
@@ -56,6 +56,13 @@ impl Game<'_> {
let (lo, hi) = corners(a.bounds());
return Some((lo, hi, at(a.position)));
}
+ if let Some(c) = self.cushions.iter().find(|c| c.id == id) {
+ let (lo, hi) = corners(Aabb::new(
+ Vec3::new(c.position.x - 0.5, c.position.y, c.position.z - 0.5),
+ Vec3::new(c.position.x + 0.5, c.position.y + 0.25, c.position.z + 0.5),
+ ));
+ return Some((lo, hi, at(c.position)));
+ }
if let Some(f) = self.item_frames.iter().find(|f| f.id == id) {
let (lo, hi) = f.bounds();
return Some((lo, hi, centre(f.pos)));
@@ -98,9 +105,10 @@ impl Game<'_> {
let boat = self.boats.iter().position(|b| b.id == vehicle);
let cart = self.carts.iter().any(|c| c.id == vehicle);
let mount = self.mobs.iter().any(|m| m.id == vehicle);
+ let cushion = self.cushions.iter().any(|c| c.id == vehicle);
// The vehicle is gone (broken, in another level) or the rider died:
// its client drops it with the entity or the death screen.
- if dead || (boat.is_none() && !cart && !mount) {
+ if dead || (boat.is_none() && !cart && !mount && !cushion) {
let g = &mut host.hub.guests[gi];
(g.vehicle, g.vehicle_good) = (None, None);
return;
@@ -115,6 +123,17 @@ impl Game<'_> {
// Getting out goes through leave_guest, which tells the client and
// teleports it (ServerPlayer.dismountTo).
let seat = self.as_guest(host, conn, |game| {
+ // A cushion does not move: the rider sits on it until it sneaks
+ // (`Cushion.removePassenger` plays the get-up sound).
+ if game.cushions.iter().any(|c| Some(c.id) == game.riding) {
+ if keys.shift {
+ game.tick_cushion_riding(keys);
+ return None;
+ }
+ let c = game.cushions.iter().find(|c| Some(c.id) == game.riding)?;
+ let p = c.seat();
+ return Some([p.x, p.y, p.z]);
+ }
if let Some(i) = game.boats.iter().position(|b| Some(b.id) == game.riding) {
if keys.shift {
game.dismount(i);
@@ -168,6 +187,12 @@ impl Game<'_> {
.iter()
.find(|c| c.id == vehicle)
.map(|c| (c.position, c.position.add_vector(Vec3::new(0.0, SEAT, 0.0))))
+ })
+ .or_else(|| {
+ self.cushions
+ .iter()
+ .find(|c| c.id == vehicle)
+ .map(|c| (c.position, c.seat()))
});
if let Some((p, seat)) = at {
let g = &mut host.hub.guests[gi];
diff --git a/crates/horizon-client/src/game/levels.rs b/crates/horizon-client/src/game/levels.rs
index e8a8cfb59..9137a26d1 100644
--- a/crates/horizon-client/src/game/levels.rs
+++ b/crates/horizon-client/src/game/levels.rs
@@ -50,6 +50,7 @@ pub(super) struct Parked {
paintings: Vec,
item_frames: Vec,
armor_stands: Vec,
+ cushions: Vec,
others: Vec,
boats: Vec,
carts: Vec,
@@ -74,6 +75,7 @@ pub(super) struct Parked {
lan_events: Vec<(i32, [i32; 3], i32, Option)>,
lan_block_events: Vec<([i32; 3], i32, i32, u32)>,
lan_entity_events: Vec<(i32, i8, [f64; 3])>,
+ lan_critical_hits: Vec<(Option, i32)>,
/// Only the per-level parts of these are used (`swap_level`).
anims: BlockEntityAnims,
raiding: Raiding,
@@ -135,6 +137,7 @@ impl Game<'_> {
paintings: Vec::new(),
item_frames: Vec::new(),
armor_stands: Vec::new(),
+ cushions: Vec::new(),
others: Vec::new(),
boats: Vec::new(),
carts: Vec::new(),
@@ -159,6 +162,7 @@ impl Game<'_> {
lan_events: Vec::new(),
lan_block_events: Vec::new(),
lan_entity_events: Vec::new(),
+ lan_critical_hits: Vec::new(),
anims: Default::default(),
raiding: Default::default(),
sculk: Default::default(),
@@ -185,6 +189,7 @@ impl Game<'_> {
swap(&mut self.paintings, &mut p.paintings);
swap(&mut self.item_frames, &mut p.item_frames);
swap(&mut self.armor_stands, &mut p.armor_stands);
+ swap(&mut self.cushions, &mut p.cushions);
swap(&mut self.others, &mut p.others);
swap(&mut self.boats, &mut p.boats);
swap(&mut self.carts, &mut p.carts);
@@ -209,6 +214,7 @@ impl Game<'_> {
swap(&mut self.lan_events, &mut p.lan_events);
swap(&mut self.lan_block_events, &mut p.lan_block_events);
swap(&mut self.lan_entity_events, &mut p.lan_entity_events);
+ swap(&mut self.lan_critical_hits, &mut p.lan_critical_hits);
let (a, b) = (&mut self.be_anims, &mut p.anims);
swap(&mut a.chests, &mut b.chests);
swap(&mut a.books, &mut b.books);
diff --git a/crates/horizon-client/src/game/minecarts.rs b/crates/horizon-client/src/game/minecarts.rs
index a19b6df73..072059e49 100644
--- a/crates/horizon-client/src/game/minecarts.rs
+++ b/crates/horizon-client/src/game/minecarts.rs
@@ -87,9 +87,12 @@ pub(super) struct Minecart {
pub fuse: i32,
/// Hopper minecarts: an unpowered activator rail enables them.
pub enabled: bool,
+ /// Chest minecarts: the loot table rolled into `items` on load, saved
+ /// again while the items are still that roll.
+ pub loot: Option,
}
-const WIDTH: f64 = 0.98;
-const HEIGHT: f64 = 0.7;
+pub(super) const WIDTH: f64 = 0.98;
+pub(super) const HEIGHT: f64 = 0.7;
/// AbstractMinecart.getDefaultDisplayOffset, in sixteenths of a block.
fn display_offset(kind: CartKind) -> f64 {
if kind == CartKind::Hopper { 1.0 } else { 6.0 }
@@ -97,7 +100,7 @@ fn display_offset(kind: CartKind) -> f64 {
/// The minecart's passenger attachment minus Player.DEFAULT_VEHICLE_ATTACHMENT.
pub(super) const SEAT: f64 = 0.1875 - 0.6;
/// The placeholder container key under which an open cart's items live.
-pub(super) const CART_KEY_Y: i32 = -100_000;
+pub(super) const CART_KEY_Y: i32 = horizon_client::lan::menu::VEHICLE_KEY_Y;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(super) enum Shape {
@@ -182,6 +185,7 @@ impl Minecart {
push: [0.0; 2],
fuse: -1,
enabled: true,
+ loot: None,
}
}
pub fn bounds(&self) -> Aabb {
@@ -1527,6 +1531,12 @@ impl Minecart {
}
CartKind::Rideable => {}
}
+ // A loaded loot table goes back while the items are still its roll;
+ // otherwise the items replace it, as vanilla's unpackLootTable does.
+ if !self.loot.as_ref().is_some_and(|l| l.restore(map)) {
+ map.remove(&JavaString::from("LootTable"));
+ map.remove(&JavaString::from("LootTableSeed"));
+ }
}
tag
}
diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs
index a83277376..d6e38dbbf 100644
--- a/crates/horizon-client/src/game/mods.rs
+++ b/crates/horizon-client/src/game/mods.rs
@@ -17,6 +17,7 @@ use std::{path::PathBuf, time::Duration};
#[cfg(feature = "mods")]
use {
super::Screen,
+ super::{execution_role::ExecutionRole, lan::LanHost, server_mods::ModEvent},
horizon_client::{
extras::Corner,
extras_hud,
@@ -28,7 +29,8 @@ use {
ui,
},
horizon_mods::{
- Clock, ModKey, Mods, Output, Player, Runs, Snapshot, Status, UiScreen, UiWidget,
+ Clock, ModKey, Mods, Output, Player, Runs, ServerPlayer, Snapshot, Status, UiScreen,
+ UiWidget,
},
std::time::Instant,
winit::{event::MouseButton, keyboard::KeyCode},
@@ -52,11 +54,24 @@ pub(super) struct ModsState {
/// The chat's command tree while a running mod owns commands.
#[cfg(feature = "mods")]
tree: Option,
+ /// A server mod runs here: what players do is queued for it.
+ #[cfg(feature = "mods")]
+ pub(super) server: bool,
+ /// The players server mods saw last tick, for joins and leaves.
+ #[cfg(feature = "mods")]
+ pub(super) players: Vec,
+ /// What players did this tick, for server mods at its end.
+ #[cfg(feature = "mods")]
+ pub(super) events: Vec,
/// `command.run` lines for this window's own world, run on the next tick:
/// run at once, their chat lines would reach the mods while their output
/// is still being shown, and a mod answering its own lines would recurse.
#[cfg(feature = "mods")]
commands: Vec,
+ /// `command.run-quiet` batches for this window's own world, by mod id,
+ /// run on the next tick as `commands` are.
+ #[cfg(feature = "mods")]
+ quiet: Vec<(String, Vec)>,
/// The server's command tree with the running mods' commands added.
#[cfg(feature = "mods")]
remote_tree: Option,
@@ -272,7 +287,7 @@ impl Game<'_> {
.collect();
let enabled = &self.settings.mods.enabled;
let on = |id: &str| enabled.contains(id) || dev_ids.iter().any(|d| d == id);
- let mods = Mods::load_for(Runs::Client, &paths, on, vanilla_command);
+ let mods = Mods::load_for(self.mod_runs(), &paths, on, vanilla_command);
self.mods.mods = mods;
let chosen = &self.settings.mods;
self.mods.mods.set_bindings(|id| chosen.keys_of(id));
@@ -282,8 +297,18 @@ impl Game<'_> {
self.sync_mod_commands();
self.show_mod_output();
}
+ /// Which mods this game runs: a server the server mods, the window
+ /// client mods, and both when it simulates the world itself (in-frame).
+ fn mod_runs(&self) -> Runs {
+ match self.options.execution_role {
+ ExecutionRole::IntegratedServer | ExecutionRole::DedicatedServer => Runs::Server,
+ ExecutionRole::Client if self.options.server_thread => Runs::Client,
+ ExecutionRole::Client => Runs::Both,
+ }
+ }
/// The Mods page's rows, from what loaded.
fn list_mods(&mut self) {
+ self.mods.server = self.mods.mods.runs_server();
let dev = &self.mods.dev;
self.settings.mods.installed = self
.mods
@@ -369,8 +394,18 @@ impl Game<'_> {
self.start_mods();
}
}
- /// What mods read this tick (or frame, for mods with a `frame` hook).
+ /// What mods read this tick (or frame, for mods with a `frame` hook): the
+ /// server's players as they were at the last tick's end.
fn mod_snapshot(&mut self) -> Snapshot {
+ let players = if self.mods.server {
+ self.mods.players.clone()
+ } else {
+ Vec::new()
+ };
+ self.mod_snapshot_with(players)
+ }
+ /// What mods read, with these players on the server.
+ pub(super) fn mod_snapshot_with(&mut self, players: Vec) -> Snapshot {
let player = self.player.as_ref().map(|p| Player {
x: p.position.x,
y: p.position.y,
@@ -414,9 +449,111 @@ impl Game<'_> {
mappings,
cps,
font_widths: self.mods.font_widths.clone(),
- players: Vec::new(),
+ players,
+ view_hit: self.mod_view_hit(),
}
}
+ /// The first block along the player's view within
+ /// [`horizon_mods::RAYCAST_MAX`] and its distance from the eye
+ /// (`game.raycast`): through the loaded chunks as the crosshair's ray
+ /// goes (fluids pass), then across the distant terrain's ground heights,
+ /// a block at a time.
+ fn mod_view_hit(&self) -> Option<(f32, horizon_mods::Hit)> {
+ if !self.mods.mods.wants_raycast() {
+ return None;
+ }
+ let eye = self.eye_position()?;
+ let dir = super::view_vector(self.yaw, self.pitch);
+ let reach = f64::from(horizon_mods::RAYCAST_MAX);
+ let (pos, point, state) = match self.clip_ray(eye, dir, reach) {
+ Some(hit) => (
+ hit.pos,
+ hit.point,
+ self.world.block(hit.pos[0], hit.pos[1], hit.pos[2]),
+ ),
+ None => self.lod_ray(eye, dir, reach)?,
+ };
+ let distance = (0..3).map(|i| (point[i] - eye[i]).powi(2)).sum::();
+ let hit = horizon_mods::Hit {
+ block_x: pos[0],
+ block_y: pos[1],
+ block_z: pos[2],
+ x: point[0],
+ y: point[1],
+ z: point[2],
+ block: self.res.facts().get(state).name.to_string(),
+ };
+ Some((distance.sqrt() as f32, hit))
+ }
+ /// Past the resident chunks: the view walked across the distant
+ /// terrain's ground heights (`Lod::ground`), a block at a time. Columns in
+ /// loaded chunks are left to `clip_ray`, which has already missed them.
+ fn lod_ray(
+ &self,
+ eye: [f64; 3],
+ dir: [f64; 3],
+ reach: f64,
+ ) -> Option<([i32; 3], [f64; 3], horizon_world::registry::BlockStateId)> {
+ let lod = self.lod.as_ref()?;
+ let mut t = 0.0;
+ while t <= reach {
+ let p = [0, 1, 2].map(|i| eye[i] + dir[i] * t);
+ let (x, z) = (p[0].floor() as i32, p[2].floor() as i32);
+ if !self.world.loaded(x, z)
+ && let Some((height, state)) = lod.ground(x, z)
+ && p[1] <= f64::from(height)
+ {
+ return Some(([x, height - 1, z], [p[0], f64::from(height), p[2]], state));
+ }
+ t += 1.0;
+ }
+ None
+ }
+ /// A mod's `command.run-quiet` lines (vanilla's commands only, as
+ /// `command.run`): to a server that runs them quietly as one payload
+ /// (`horizon_protocol::quiet`), to any other as chat commands, or for this
+ /// window's own world next tick.
+ fn mod_quiet_commands(&mut self, mod_id: String, lines: Vec) {
+ let lines: Vec = lines
+ .into_iter()
+ .filter(|l| vanilla_command(l.split(' ').next().unwrap_or_default()))
+ .collect();
+ match self.remote.as_ref().map(|r| r.quiet_commands) {
+ Some(true) => {
+ for data in horizon_protocol::quiet::encode(&mod_id, &lines) {
+ self.send(horizon_protocol::packets::play::Serverbound::CustomPayload(
+ horizon_protocol::packets::configuration::CustomPayload {
+ channel: horizon_protocol::quiet::CHANNEL.into(),
+ data,
+ },
+ ));
+ }
+ }
+ // The server checks the player's permissions; its feedback shows.
+ Some(false) => {
+ for line in lines {
+ self.send_chat(&format!("/{line}"));
+ }
+ }
+ None => self.mods.quiet.push((mod_id, lines)),
+ }
+ }
+ /// A quiet batch in this window's own world, as a server runs one
+ /// (game/lan_quiet.rs): each line as the player typing it, no chat, a
+ /// chat message refused; one line in the log.
+ fn run_quiet_batch(&mut self, mod_id: &str, lines: &[String]) {
+ let permission = self.ui.registries.permission;
+ let mut failed = 0;
+ for line in lines {
+ let ok = self.signable_arguments(permission, line).is_empty()
+ && self.run_command_quietly(line);
+ failed += usize::from(!ok);
+ }
+ log::info!(
+ "[mod {mod_id}] ran {} quiet command(s): {failed} failed",
+ lines.len()
+ );
+ }
/// A game tick.
pub(super) fn tick_mods(&mut self) {
// Here, at the player's level: shown inside a console call (a control
@@ -424,6 +561,9 @@ impl Game<'_> {
for line in std::mem::take(&mut self.mods.commands) {
self.run_command(&line);
}
+ for (mod_id, lines) in std::mem::take(&mut self.mods.quiet) {
+ self.run_quiet_batch(&mod_id, &lines);
+ }
if !self.mods.mods.is_running() {
return;
}
@@ -433,23 +573,32 @@ impl Game<'_> {
self.mods.mods.tick(self.ticks);
self.show_mod_output();
}
- /// Once per frame: the frame hook, and dev mods' rebuilds.
- pub(super) fn mods_frame(&mut self, elapsed: Duration) {
- if !self.mods.dev.is_empty()
- && self
+ /// Restarts the dev mods rebuilt since the last check, once a second.
+ pub(super) fn reload_dev_mods(&mut self) {
+ if self.mods.dev.is_empty()
+ || self
.mods
.checked
- .is_none_or(|t| t.elapsed() >= RELOAD_CHECK)
+ .is_some_and(|t| t.elapsed() < RELOAD_CHECK)
{
- self.mods.checked = Some(Instant::now());
- if self.mods.mods.reload_changed(vanilla_command) {
- let chosen = &self.settings.mods;
- self.mods.mods.set_bindings(|id| chosen.keys_of(id));
- self.list_mods();
- self.sync_mod_commands();
+ return;
+ }
+ self.mods.checked = Some(Instant::now());
+ if self.mods.mods.reload_changed(vanilla_command) {
+ let chosen = &self.settings.mods;
+ self.mods.mods.set_bindings(|id| chosen.keys_of(id));
+ self.list_mods();
+ self.sync_mod_commands();
+ if self.options.execution_role.is_client() {
self.system_message(Text::literal("Mods reloaded").with_color(text::GRAY));
+ } else {
+ log::info!("Horizon: mods reloaded");
}
}
+ }
+ /// Once per frame: the frame hook, and dev mods' rebuilds.
+ pub(super) fn mods_frame(&mut self, elapsed: Duration) {
+ self.reload_dev_mods();
if !self.mods.mods.is_running() {
return;
}
@@ -480,8 +629,13 @@ impl Game<'_> {
self.mods.mods.chat(line);
self.show_mod_output();
}
- /// Chat lines, command feedback and stopped mods, into the chat.
- fn show_mod_output(&mut self) {
+ /// What mods showed: client mods' lines into the chat, server mods'
+ /// to their players.
+ pub(super) fn show_mod_output(&mut self) {
+ self.route_mod_output(None);
+ }
+ /// [`Game::show_mod_output`], with the hub when it is out of `self.lan`.
+ pub(super) fn route_mod_output(&mut self, mut host: Option<&mut LanHost>) {
for output in self.mods.mods.drain() {
let line = match output {
Output::Chat { name, text } => {
@@ -504,12 +658,25 @@ impl Game<'_> {
}
continue;
}
+ Output::QuietCommands { mod_id, lines } => {
+ self.mod_quiet_commands(mod_id, lines);
+ continue;
+ }
Output::Stopped { name, reason } => {
// Its screen goes with it: the entry's run is gone, so
// `mod_ui_view` finds no widgets and the screen closes.
self.list_mods();
self.sync_mod_commands();
- Text::literal(format!("Mod {name} stopped: {reason}")).with_color(text::RED)
+ let line = format!("Mod {name} stopped: {reason}");
+ if self.options.dedicated.is_some() {
+ super::dedicated::log("WARN", &line);
+ }
+ Text::literal(line).with_color(text::RED)
+ }
+ server
+ @ (Output::Tell { .. } | Output::Broadcast { .. } | Output::Reply { .. }) => {
+ self.route_server_output(host.as_deref_mut(), server);
+ continue;
}
Output::OpenScreen { owner } => {
self.open_mod_screen(owner, false);
@@ -525,20 +692,8 @@ impl Game<'_> {
}
continue;
}
- // Server mods' lines: this game runs no server mod yet.
- Output::Tell { .. } | Output::Broadcast { .. } | Output::Reply { .. } => continue,
};
- // Not through system_message: a mod must not hear its own lines.
- // Written by this client, so the Chat option doesn't hide it.
- let focused = self.screen == Screen::Chat;
- self.ui.chat.add(
- &self.gui.atlas,
- line,
- None,
- horizon_client::chat::ChatSource::Client,
- self.ticks,
- focused,
- );
+ self.add_mod_chat_line(line);
}
// A mod may set or clear its settings page in any call, not only
// while loading: its row's Settings button follows.
@@ -547,6 +702,20 @@ impl Game<'_> {
row.has_settings = e.has_ui_settings();
}
}
+ /// A mod's line in the window's chat; not through system_message: a mod
+ /// must not hear its own lines.
+ /// Written by this client, so the Chat option doesn't hide it.
+ pub(super) fn add_mod_chat_line(&mut self, line: Text) {
+ let focused = self.screen == Screen::Chat;
+ self.ui.chat.add(
+ &self.gui.atlas,
+ line,
+ None,
+ horizon_client::chat::ChatSource::Client,
+ self.ticks,
+ focused,
+ );
+ }
/// The mods' HUD panels, stacked with the extras in their corners.
pub(super) fn draw_mod_panels(&self, batch: &mut HudBatch, corners: &mut Corners) {
for panel in self.mods.mods.panels() {
diff --git a/crates/horizon-client/src/game/names.rs b/crates/horizon-client/src/game/names.rs
index f249bd01f..9f7561e53 100644
--- a/crates/horizon-client/src/game/names.rs
+++ b/crates/horizon-client/src/game/names.rs
@@ -86,6 +86,10 @@ impl Game<'_> {
.iter()
.filter_map(|m| Some((m, m.custom_name.clone()?, false)));
let players = players.into_iter().map(|(m, name)| (m, name, true));
+ // Who is riding whom: asked once per tag below, and rescanning every mob
+ // for each tag made this quadratic in the number of loaded mobs.
+ let ridden: std::collections::HashSet =
+ self.mobs.iter().filter_map(|o| o.extra.mount).collect();
for (m, name, player) in named.chain(players) {
if !m.alive() {
continue;
@@ -134,7 +138,7 @@ impl Game<'_> {
invisible_to_viewer: invisible,
hud_hidden: self.ui.hide_gui,
is_camera: false,
- is_vehicle: self.mobs.iter().any(|o| o.extra.mount == Some(m.id)),
+ is_vehicle: ridden.contains(&m.id),
};
// MobRenderer.shouldShowName: a visible name, or the mob under the
// crosshair; players (LivingEntityRenderer) need neither.
@@ -310,7 +314,7 @@ impl Game<'_> {
}
/// The lines of one name tag, `o` from the camera: the text at
/// `text_alpha` in its layer, on the translucent black background
- /// (unlit, a hair behind) when `background`.
+ /// (in `text_layer` like the glyphs, a hair behind) when `background`.
#[allow(clippy::too_many_arguments)]
fn name_tag_quads(
&self,
@@ -382,7 +386,7 @@ impl Game<'_> {
[white.u0, white.v0, white.u1, white.v1],
BACKGROUND,
true,
- 2,
+ text_layer,
);
}
let mut x = x0;
diff --git a/crates/horizon-client/src/game/net_harness.rs b/crates/horizon-client/src/game/net_harness.rs
index 1ec573e7c..468f1db71 100644
--- a/crates/horizon-client/src/game/net_harness.rs
+++ b/crates/horizon-client/src/game/net_harness.rs
@@ -3,7 +3,10 @@
//! window, waits until it stands in the world with the chunks around it,
//! sends a chat message and `/list`, collects the chat it is shown, leaves
//! and writes a JSON report (position, chunks, chat lines, whether the
-//! server kept it connected throughout).
+//! server kept it connected throughout). With `HORIZON_NET_QUIET` (lines
+//! split by `;`) it then sends them as one batch of a mod's quiet commands
+//! (`horizon_protocol::quiet`, `tools/quiet_commands_e2e.py`) and waits two
+//! seconds for any chat they bring.
use super::Game;
use horizon_client::Result;
use serde_json::json;
@@ -23,6 +26,9 @@ impl Game<'_> {
let mut step_at = Instant::now();
let mut chat_at = 0;
let mut position = None;
+ let quiet: Vec = std::env::var("HORIZON_NET_QUIET")
+ .map(|v| v.split(';').map(str::to_owned).collect())
+ .unwrap_or_default();
let mut chunks = 0;
let mut error = None;
while Instant::now() < deadline {
@@ -71,6 +77,22 @@ impl Game<'_> {
step_at = now;
chat_at = shown;
}
+ 2 if answered && !quiet.is_empty() => {
+ for data in horizon_protocol::quiet::encode("net_harness", &quiet) {
+ self.send(horizon_protocol::packets::play::Serverbound::CustomPayload(
+ horizon_protocol::packets::configuration::CustomPayload {
+ channel: horizon_protocol::quiet::CHANNEL.into(),
+ data,
+ },
+ ));
+ }
+ step = 4;
+ step_at = now;
+ }
+ 4 if now - step_at > Duration::from_secs(2) => {
+ step = 3;
+ break;
+ }
2 if answered => {
step = 3;
break;
diff --git a/crates/horizon-client/src/game/nether_portal.rs b/crates/horizon-client/src/game/nether_portal.rs
index 353ad216c..87ba81c93 100644
--- a/crates/horizon-client/src/game/nether_portal.rs
+++ b/crates/horizon-client/src/game/nether_portal.rs
@@ -454,11 +454,24 @@ impl Game<'_> {
for m in &self.mobs {
self.portals.before.insert(m.id, m.position);
}
+ for b in &self.boats {
+ self.portals.before.insert(b.id, b.position);
+ }
+ for c in &self.carts {
+ self.portals.before.insert(c.id, c.position);
+ }
let ids: Vec<(u32, bool)> = self
.dropped
.iter()
.map(|e| (e.id, true))
.chain(self.mobs.iter().map(|m| (m.id, self.mob_can_use_portal(m))))
+ .chain(
+ self.boats
+ .iter()
+ .map(|b| b.id)
+ .chain(self.carts.iter().map(|c| c.id))
+ .map(|id| (id, !self.vehicle_occupied(id))),
+ )
.collect();
let present: std::collections::HashSet = ids.iter().map(|(id, _)| *id).collect();
self.portals.entities.retain(|id, _| present.contains(id));
@@ -501,8 +514,15 @@ impl Game<'_> {
&& self.riding != Some(m.id)
&& m.species() != Species::Wither
}
- /// `NetherPortalBlock.entityInside` for the items and mobs that moved this
- /// tick: the portal blocks their box swept through.
+ /// Whether the player, a server player or a mob rides vehicle `id`.
+ fn vehicle_occupied(&self, id: u32) -> bool {
+ self.riding == Some(id)
+ || self.ridden_by_guest(id)
+ || self.mobs.iter().any(|m| m.extra.mount == Some(id))
+ }
+ /// `NetherPortalBlock.entityInside` for the items, mobs and empty boats
+ /// and minecarts that moved this tick: the portal blocks their box swept
+ /// through.
pub(super) fn find_entity_portals(&mut self) {
if self.remote.is_some() {
return;
@@ -519,8 +539,23 @@ impl Game<'_> {
.filter(|m| self.mob_can_use_portal(m))
.map(|m| (m.id, m.position, m.width(), m.height()))
.collect();
+ let vehicles: Vec<(u32, Vec3, f32, f32)> = self
+ .boats
+ .iter()
+ .map(|b| (b.id, b.position, super::boats::WIDTH, super::boats::HEIGHT))
+ .chain(self.carts.iter().map(|c| {
+ (
+ c.id,
+ c.position,
+ super::minecarts::WIDTH,
+ super::minecarts::HEIGHT,
+ )
+ }))
+ .filter(|(id, ..)| !self.vehicle_occupied(*id))
+ .map(|(id, at, w, h)| (id, at, w as f32, h as f32))
+ .collect();
let mut portal_sections = std::mem::take(&mut self.portals.sections);
- for (id, to, width, height) in items.into_iter().chain(mobs) {
+ for (id, to, width, height) in items.into_iter().chain(mobs).chain(vehicles) {
let from = self.portals.before.get(&id).copied().unwrap_or(to);
if let Some(pos) = self.swept_portal(from, to, width, height, &mut portal_sections) {
inside.push((id, pos));
@@ -678,6 +713,44 @@ impl Game<'_> {
}
self.portals.arrivals.push(Arrival { plan, tag });
self.portals.dirty = true;
+ } else if let Some(i) = self.boats.iter().position(|b| b.id == id) {
+ let b = &self.boats[i];
+ let (width, height) = (super::boats::WIDTH as f32, super::boats::HEIGHT as f32);
+ let plan = self.portal_plan(entry, b.position, width, height, false);
+ if !self.may_enter(plan.dim) {
+ return;
+ }
+ let uuid = self.uuid_for(id);
+ let b = &self.boats[i];
+ let tag = horizon_client::entity_nbt::boat_to_nbt(
+ &b.entity_type(),
+ b.position,
+ b.velocity,
+ b.yaw,
+ uuid,
+ self.persist_raw(id),
+ b.chest.then_some(&b.items[..]),
+ );
+ self.boats.remove(i);
+ self.forget_entity(id);
+ self.portals.arrivals.push(Arrival { plan, tag });
+ self.portals.dirty = true;
+ } else if let Some(i) = self.carts.iter().position(|c| c.id == id) {
+ let c = &self.carts[i];
+ let (width, height) = (
+ super::minecarts::WIDTH as f32,
+ super::minecarts::HEIGHT as f32,
+ );
+ let plan = self.portal_plan(entry, c.position, width, height, false);
+ if !self.may_enter(plan.dim) {
+ return;
+ }
+ let uuid = self.uuid_for(id);
+ let tag = self.carts[i].to_nbt(uuid, self.persist_raw(id));
+ self.carts.remove(i);
+ self.forget_entity(id);
+ self.portals.arrivals.push(Arrival { plan, tag });
+ self.portals.dirty = true;
}
}
/// Entities waiting for the loaded dimension whose exit area is loaded:
diff --git a/crates/horizon-client/src/game/options.rs b/crates/horizon-client/src/game/options.rs
index 243eced5c..7ac4ae98b 100644
--- a/crates/horizon-client/src/game/options.rs
+++ b/crates/horizon-client/src/game/options.rs
@@ -257,6 +257,7 @@ impl Game<'_> {
lang::set(lang::Language::load(&[&root], code));
self.refresh_remote_language();
self.ui.chat.rescale(&self.gui.atlas);
+ self.creative_screen.forget_items();
}
/// Settings that Horizon applies beyond render distance, volume and GUI scale.
pub(super) fn apply_ui_settings(&mut self) {
@@ -268,6 +269,11 @@ impl Game<'_> {
r.animated_textures = s.horizon.animated_textures;
r.set_mipmap_levels(s.mipmap_levels.max(0) as u32);
r.set_world_post(s.horizon.resolution_scale as f32 / 100.0, s.horizon.fxaa);
+ r.set_hdr(
+ s.horizon.hdr,
+ s.horizon.hdr_paper_white as f32,
+ s.horizon.hdr_peak as f32,
+ );
}
// Window.updateFullscreenIfChanged: only a changed option asks the
// window to change, so applying settings never undoes a fullscreen
diff --git a/crates/horizon-client/src/game/packs.rs b/crates/horizon-client/src/game/packs.rs
index ee57e9f2b..569c63083 100644
--- a/crates/horizon-client/src/game/packs.rs
+++ b/crates/horizon-client/src/game/packs.rs
@@ -163,7 +163,7 @@ impl Game<'_> {
self.pack.context.biomes.clone(),
self.pack.context.light.clone(),
)?;
- // Keep the working GPU resources, but release its exclusive window surface.
+ // Keep the working GPU resources, and hand its window surface over.
if let Some(renderer) = &mut self.renderer {
renderer.release_surface();
}
@@ -221,6 +221,8 @@ impl Game<'_> {
self.apply_ui_settings();
}
self.ui.chat.rescale(&self.gui.atlas);
+ // The creative screen survives the reload; item names may not.
+ self.creative_screen.forget_items();
Ok(())
}
}
diff --git a/crates/horizon-client/src/game/persist.rs b/crates/horizon-client/src/game/persist.rs
index 45ef8801f..b5e650e70 100644
--- a/crates/horizon-client/src/game/persist.rs
+++ b/crates/horizon-client/src/game/persist.rs
@@ -279,7 +279,8 @@ impl Game<'_> {
k if super::minecarts::CartKind::from_id(k).is_some() => {
let kind = super::minecarts::CartKind::from_id(k).unwrap();
let id = self.next_entity + 1;
- if let Some(c) = super::minecarts::Minecart::from_nbt(&tag, id, kind) {
+ if let Some(mut c) = super::minecarts::Minecart::from_nbt(&tag, id, kind) {
+ self.roll_cart_loot(&mut c, &tag);
self.next_entity = id;
self.remember(id, uuid, tag);
self.carts.push(c);
@@ -314,6 +315,13 @@ impl Game<'_> {
self.item_frames.push(f);
}
}
+ "minecraft:cushion" => {
+ self.next_entity += 1;
+ if let Some(c) = entity_nbt::cushion_from_nbt(&tag, self.next_entity) {
+ self.remember(c.id, uuid, tag);
+ self.cushions.push(c);
+ }
+ }
"minecraft:painting" => {
if let Some((pos, facing, variant)) = entity_nbt::painting_from_nbt(&tag)
&& let Some(v) = self.painting_variants.get(&variant).cloned()
@@ -527,6 +535,30 @@ impl Game<'_> {
pub(super) fn persist_raw(&self, id: u32) -> Option<&Tag> {
self.persist.raw.get(&id)
}
+ /// A loaded chest minecart with a loot table and no items: the table
+ /// rolled now, as generated containers are (Horizon does not unpack on
+ /// first open), and kept to save it again while unchanged.
+ fn roll_cart_loot(&self, cart: &mut super::minecarts::Minecart, tag: &Tag) {
+ let Some(table) = horizon_client::save::text(tag.get("LootTable")) else {
+ return;
+ };
+ if cart.items.is_empty() || cart.items.iter().any(Option::is_some) {
+ return;
+ }
+ let loot = horizon_client::structure_blocks::loot_data(&self.options.resources);
+ let p = cart.position;
+ let pos = (p.x.floor() as i32, p.y.floor() as i32, p.z.floor() as i32);
+ let biome = self.biome_name_at(p).unwrap_or_default();
+ let seed = horizon_client::structure_blocks::loot_seed(tag);
+ cart.loot = Some(horizon_client::structure_blocks::roll_container(
+ &mut cart.items,
+ loot.as_deref(),
+ table,
+ seed,
+ pos,
+ &biome,
+ ));
+ }
fn remember(&mut self, id: u32, uuid: Option<[i32; 4]>, tag: Tag) {
self.restore_portal_cooldown(id, &tag);
if let Some(u) = uuid {
@@ -776,6 +808,18 @@ impl Game<'_> {
out.entry(chunk_of(at)).or_default().push(tag);
}
}
+ for c in &self.cushions {
+ if keys.contains(&chunk_of(c.position)) {
+ let uuid = self
+ .persist
+ .uuids
+ .get(&c.id)
+ .copied()
+ .unwrap_or_else(random_uuid);
+ let tag = entity_nbt::cushion_to_nbt(c, uuid, self.persist.raw.get(&c.id));
+ out.entry(chunk_of(c.position)).or_default().push(tag);
+ }
+ }
for p in &self.paintings {
let at = painting_center(p);
if keys.contains(&chunk_of(at)) {
@@ -868,6 +912,13 @@ impl Game<'_> {
}
keep
});
+ self.cushions.retain(|c| {
+ let keep = !gone(c.position);
+ if !keep {
+ removed.push(c.id);
+ }
+ keep
+ });
// Riders leave with their root vehicle, a boat's with the boat.
let leaving: HashSet = (0..self.mobs.len())
.filter(|&i| {
@@ -934,6 +985,7 @@ impl Game<'_> {
.chain(self.boats.iter().map(|e| e.id))
.chain(self.carts.iter().map(|e| e.id))
.chain(self.armor_stands.iter().map(|e| e.id))
+ .chain(self.cushions.iter().map(|e| e.id))
.chain(self.item_frames.iter().map(|e| e.id))
.chain(self.paintings.iter().map(|e| e.id))
.chain(self.others.iter().map(|e| e.id))
diff --git a/crates/horizon-client/src/game/potions.rs b/crates/horizon-client/src/game/potions.rs
index b28c2fb4d..13eae7d3e 100644
--- a/crates/horizon-client/src/game/potions.rs
+++ b/crates/horizon-client/src/game/potions.rs
@@ -71,6 +71,8 @@ pub(super) struct BodyPotions {
pub(super) struct PotionState {
pub(super) effects: PlayerEffects,
thrown: Vec,
+ /// The area effect clouds a server tracks (game/remote_projectiles.rs).
+ pub(super) clouds: Vec,
applied: Vec<(&'static str, &'static str, f64, &'static str)>,
/// Armor the protection table was computed for.
protection_for: Option>>,
@@ -141,14 +143,22 @@ impl PotionState {
t.stack = stack;
}
}
- /// One tick of a server's client: `step` moves each toward where the
- /// server put it.
- pub(super) fn step_remote_thrown(&mut self, mut step: impl FnMut(u32, Vec3) -> Vec3) {
- for t in &mut self.thrown {
- t.previous = t.position;
- t.age += 1;
- t.position = step(t.id, t.position);
- }
+ /// A server's move: thrown potions have no interpolation handler, so
+ /// `moveOrInterpolateTo` puts them there at once. False when `id` isn't one.
+ pub(super) fn move_remote_thrown(&mut self, id: u32, at: Vec3) -> bool {
+ let Some(t) = self.thrown.iter_mut().find(|t| t.id == id) else {
+ return false;
+ };
+ t.position = at;
+ true
+ }
+ /// `handleSetEntityMotion` for a thrown potion. False when `id` isn't one.
+ pub(super) fn remote_thrown_motion(&mut self, id: u32, velocity: Vec3) -> bool {
+ let Some(t) = self.thrown.iter_mut().find(|t| t.id == id) else {
+ return false;
+ };
+ t.velocity = velocity;
+ true
}
pub(super) fn remove_remote_thrown(&mut self, id: u32) {
self.thrown.retain(|t| t.id != id);
@@ -484,6 +494,7 @@ impl Game<'_> {
thrown.retain_mut(|t| {
t.age += 1;
t.previous = t.position;
+ self.thrown_potion_motion(t);
let next = Vec3::new(
t.position.x + t.velocity.x,
t.position.y + t.velocity.y,
@@ -520,18 +531,6 @@ impl Game<'_> {
return false;
}
t.position = next;
- // ThrowableItemProjectile: drag 0.99 then gravity 0.05.
- // ThrownExperienceBottle.getDefaultGravity is 0.07.
- let gravity = if &*t.stack.item == "minecraft:experience_bottle" {
- 0.07
- } else {
- 0.05
- };
- t.velocity = Vec3::new(
- t.velocity.x * 0.99,
- t.velocity.y * 0.99 - gravity,
- t.velocity.z * 0.99,
- );
t.age < 1200 && t.position.y > -128.0
});
self.potions.thrown = thrown;
@@ -539,6 +538,60 @@ impl Game<'_> {
self.splash(&stack, at);
}
}
+ /// `ThrowableProjectile.tick`'s `applyGravity` and `applyInertia`, which
+ /// come before the move: gravity 0.05 (0.07 for an experience bottle),
+ /// then drag 0.99 (0.8 in water).
+ fn thrown_potion_motion(&self, t: &mut ThrownPotion) {
+ let gravity = if &*t.stack.item == "minecraft:experience_bottle" {
+ 0.07
+ } else {
+ 0.05
+ };
+ let at = t.position;
+ let water = self
+ .res
+ .facts()
+ .get(self.world.block(
+ at.x.floor() as i32,
+ at.y.floor() as i32,
+ at.z.floor() as i32,
+ ))
+ .fluid
+ == horizon_client::blocks::Fluid::Water;
+ let inertia = if water { 0.8 } else { 0.99 };
+ t.velocity = Vec3::new(
+ t.velocity.x * inertia,
+ (t.velocity.y - gravity) * inertia,
+ t.velocity.z * inertia,
+ );
+ }
+ /// `ThrowableProjectile.tick` on a server's client: it falls and moves
+ /// by itself between the server's updates and stops at a block; only
+ /// the server breaks it.
+ pub(super) fn step_remote_thrown(&mut self) {
+ let mut thrown = std::mem::take(&mut self.potions.thrown);
+ for t in &mut thrown {
+ t.previous = t.position;
+ t.age += 1;
+ self.thrown_potion_motion(t);
+ let next = Vec3::new(
+ t.position.x + t.velocity.x,
+ t.position.y + t.velocity.y,
+ t.position.z + t.velocity.z,
+ );
+ let state = self.world.block(
+ next.x.floor() as i32,
+ next.y.floor() as i32,
+ next.z.floor() as i32,
+ );
+ if self.res.facts().get(state).full_collision {
+ t.velocity = Vec3::ZERO;
+ } else {
+ t.position = next;
+ }
+ }
+ self.potions.thrown = thrown;
+ }
/// `ThrownItemRenderer` for thrown splash and lingering potions and
/// experience bottles: the item (GROUND) facing the camera at scale 1,
/// lit at the light probe (the eye, 0.85 of the 0.25 height).
diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs
index 5651647b7..e9f30e84b 100644
--- a/crates/horizon-client/src/game/remote.rs
+++ b/crates/horizon-client/src/game/remote.rs
@@ -11,7 +11,7 @@ use super::{Dim, Game, Mining, Screen};
use horizon_client::first_person::{Hand, SwingAnimation};
use horizon_client::{
Result,
- chunk::{Chunk, SECTIONS},
+ chunk::Chunk,
containers::{BlockEntity, Furnace, FurnaceKind},
entities::{Experience, ExperienceOrb, ItemEntity},
hud::{HudBatch, rgba},
@@ -109,6 +109,8 @@ enum Received {
pub(super) struct Remote {
pub session: Session,
pub brand: String,
+ /// The server announced it runs mods' quiet commands.
+ pub quiet_commands: bool,
pub(super) builtin: Arc,
lang: Arc,
pub(super) registries: Arc,
@@ -419,6 +421,7 @@ impl Game<'_> {
self.remote = Some(Remote {
session,
brand: "unknown".into(),
+ quiet_commands: false,
builtin: Arc::new(builtin),
lang,
registries: Arc::new(Dynamic::default()),
@@ -839,7 +842,15 @@ impl Game<'_> {
remote.brand = brand;
}
}
- Event::Reconfigured(registries) => self.set_registries(registries),
+ Event::QuietCommands => {
+ if let Some(remote) = &mut self.remote {
+ remote.quiet_commands = true;
+ }
+ }
+ Event::Reconfigured(registries) => {
+ self.boss_overlay.reset();
+ self.set_registries(registries);
+ }
// handleTransfer: "Cannot transfer to server from
// singleplayer" (no ServerData), so the world just closes.
Event::Transfer { host, port } => {
@@ -998,6 +1009,9 @@ impl Game<'_> {
self.world.day_time,
self.world.weather,
);
+ let old = std::mem::take(&mut self.world.chunks);
+ let count = old.len();
+ horizon_client::chunk::free_columns(old, count);
self.world = World::new(self.res.facts().air, spawn.seed);
self.world.game_time = time;
self.world.day_time = day;
@@ -1059,14 +1073,14 @@ impl Game<'_> {
if gone.is_empty() {
return;
}
+ let mut freed = Vec::with_capacity(gone.len());
for &(x, z) in &gone {
- self.streaming.forget(&mut self.world, x, z);
- if let Some(renderer) = self.renderer.as_mut() {
- for sy in 0..SECTIONS as i32 {
- renderer.remove((x, sy, z));
- }
- }
+ freed.extend(self.streaming.forget(&mut self.world, x, z));
}
+ if let Some(renderer) = self.renderer.as_mut() {
+ renderer.remove_columns(gone.iter().copied());
+ }
+ horizon_client::chunk::free_columns(freed, gone.len());
// One pass for all of them.
self.world
.block_entities
@@ -1255,6 +1269,9 @@ impl Game<'_> {
} else {
self.res.mob_types().get(kind).cloned()
};
+ if kind == "minecraft:ender_dragon" {
+ self.spawn_remote_dragon(id, position, yaw);
+ }
if kind == "minecraft:experience_orb" {
// Its value (ExperienceOrb.DATA_VALUE) follows in SetEntityData.
self.orbs.retain(|o| o.id != id as u32);
@@ -1361,13 +1378,17 @@ impl Game<'_> {
m.on_ground = on_ground;
return;
}
- if let Some(p) = self.creatures.projectiles.iter_mut().find(|p| {
- p.id == id as u32
- && matches!(
- p.kind,
- horizon_client::projectiles::ProjectileKind::LargeFireball { .. }
- )
- }) {
+ // Projectiles keep Entity's NO_OP InterpolationHandler too: the
+ // server's position replaces theirs at once and their own tick
+ // flies them on (`tick_remote_projectiles`). Eased towards it
+ // instead, they only moved when an update came, every 10 to 20
+ // ticks.
+ if let Some(p) = self
+ .creatures
+ .projectiles
+ .iter_mut()
+ .find(|p| p.id == id as u32)
+ {
if path.is_some() {
p.position = Vec3::new(position[0], position[1], position[2]);
}
@@ -1377,6 +1398,22 @@ impl Game<'_> {
}
return;
}
+ if path.is_some()
+ && (self.potions.move_remote_thrown(
+ id as u32,
+ Vec3::new(position[0], position[1], position[2]),
+ ) || self.end.move_remote_eye(id as u32, position))
+ {
+ return;
+ }
+ // A dragon fireball keeps Entity's NO_OP InterpolationHandler too:
+ // the server's position replaces its own at once.
+ if let Some(b) = self.dragon_fireballs.iter_mut().find(|b| b.id == id as u32) {
+ if path.is_some() {
+ b.position = Vec3::new(position[0], position[1], position[2]);
+ }
+ return;
+ }
if let Some(p) = r.pending_items.get_mut(&id) {
p.0 = position;
}
@@ -1423,6 +1460,8 @@ impl Game<'_> {
self.mobs.retain(|m| m.id != id as u32);
self.orbs.retain(|o| o.id != id as u32);
self.dropped.retain(|d| d.id != id as u32);
+ // Before the crystals go: the dragon's beam index follows them.
+ self.remove_remote_dragon(id);
self.remove_remote_object(id as u32);
self.remove_remote_projectile(id as u32);
if let Some(r) = self.remote.as_mut() {
@@ -1631,9 +1670,7 @@ impl Game<'_> {
.block_entities
.retain(|k, _| (k.0 >> 4, k.2 >> 4) != (x, z));
if let Some(renderer) = self.renderer.as_mut() {
- for sy in 0..SECTIONS as i32 {
- renderer.remove((x, sy, z));
- }
+ renderer.remove_columns([(x, z)]);
}
}
C::ChunkBatchFinished(_) => {
@@ -1957,6 +1994,7 @@ impl Game<'_> {
}
self.remote_object_data(p.id, &p.values);
self.remote_projectile_data(p.id, &p.values);
+ self.remote_dragon_data(p.id, &p.values);
}
// ClientPacketListener.handleSetEquipment: what a mob or another
// player holds and wears (EquipmentSlot ordinals).
@@ -2038,6 +2076,8 @@ impl Game<'_> {
} else if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) {
m.limb_speed = 1.5;
m.hurt_time = 10;
+ } else {
+ self.remote_dragon_hurt(p.id);
}
}
C::HurtAnimation(p) => {
@@ -2047,9 +2087,14 @@ impl Game<'_> {
}
} else if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) {
m.hurt_time = 10;
+ } else {
+ self.remote_dragon_hurt(p.id);
}
}
C::EntityEvent(p) => {
+ if p.event == super::fireworks::FIREWORK_EXPLODE_EVENT {
+ self.remote_firework_explosion(p.id);
+ }
// LocalPlayer.setPermissions also refreshes an open World Options screen.
if (24..=28).contains(&p.event)
&& self.remote.as_ref().is_some_and(|r| r.entity_id == p.id)
@@ -2063,6 +2108,11 @@ impl Game<'_> {
{
horizon_client::mobs::rabbit::hop_event(m);
}
+ if p.event == 19
+ && let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32)
+ {
+ horizon_client::mobs::aquatic::squid_cycle_event(m);
+ }
// EntityEvent.DEATH: start the death animation.
if p.event == 3
&& let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32)
@@ -2935,8 +2985,12 @@ impl Game<'_> {
/// entities whose own tick moves them on a client: dropped items, falling
/// blocks and primed TNT.
fn remote_motion(&mut self, id: i32, velocity: [f64; 3]) {
+ self.remote_dragon_motion(id, velocity);
let v = Vec3::new(velocity[0], velocity[1], velocity[2]);
- if let Some(d) = self.dropped.iter_mut().find(|d| d.id == id as u32) {
+ if let Some(m) = self.mobs.iter_mut().find(|m| m.id == id as u32) {
+ // Squid.aiStep derives its body pitch and roll from network motion.
+ m.velocity = v;
+ } else if let Some(d) = self.dropped.iter_mut().find(|d| d.id == id as u32) {
d.velocity = v;
} else if let Some(f) = self.falling.iter_mut().find(|f| f.id == id as u32) {
f.velocity = v;
@@ -2949,12 +3003,16 @@ impl Game<'_> {
.find(|p| p.id == id as u32)
{
p.velocity = v;
+ } else if let Some(b) = self.dragon_fireballs.iter_mut().find(|b| b.id == id as u32) {
+ b.velocity = v;
} else if let Some(p) = self
.remote
.as_mut()
.and_then(|r| r.pending_items.get_mut(&id))
{
p.1 = v;
+ } else if !self.potions.remote_thrown_motion(id as u32, v) {
+ self.end.remote_eye_motion(id as u32, velocity);
}
}
fn apply_remote_health(&mut self) {
@@ -3061,6 +3119,12 @@ impl Game<'_> {
fn tick_remote_level(&mut self) -> Result<()> {
self.ticks += 1;
self.track_experience_display();
+ // Player.tick's ItemCooldowns.tick: this path replaces `tick_world`'s
+ // for a server's client (multiplayer and the default single player),
+ // so the entries advance here or the slot stays shaded until the
+ // server says the entry ended. `onCooldownEnded` is the plain
+ // ItemCooldowns' no-op on a client.
+ self.cooldowns.tick();
// Options.broadcastOptions: a changed render distance or Chat option
// is sent again.
let view = client_view_distance(self.settings.render_distance);
@@ -3424,6 +3488,13 @@ impl Game<'_> {
if m.species() == horizon_client::mobs::Species::Creeper {
horizon_client::mobs::hostile::creeper_client_tick(m);
}
+ if matches!(
+ m.species(),
+ horizon_client::mobs::Species::Guardian
+ | horizon_client::mobs::Species::ElderGuardian
+ ) {
+ horizon_client::mobs::guardian::client_tick(m);
+ }
if m.hurt_time > 0 {
m.hurt_time -= 1;
}
@@ -3483,6 +3554,8 @@ impl Game<'_> {
} else if m.alive() {
m.body_rotation_tick();
}
+ // Squid.aiStep follows LivingEntity's interpolation and head turn.
+ horizon_client::mobs::aquatic::squid_client_step(m);
// WalkAnimationState.update from the horizontal distance moved.
let (dx, dz) = (m.position.x - m.previous.x, m.position.z - m.previous.z);
let target = (((dx * dx + dz * dz).sqrt() * 4.0) as f32).min(1.0);
@@ -3491,6 +3564,9 @@ impl Game<'_> {
if m.species() == horizon_client::mobs::Species::Rabbit {
horizon_client::mobs::rabbit::post_tick(m);
}
+ if m.species() == horizon_client::mobs::Species::Bat {
+ horizon_client::mobs::bat::setup_animation_states(m);
+ }
}
for o in self.orbs.iter_mut().filter(|_| running) {
o.previous = o.position;
@@ -3519,6 +3595,18 @@ impl Game<'_> {
self.remote_player_eating(&item, ticks, at, eye, pitch, yaw);
}
if running {
+ // The dragon: LivingEntity's interpolation, inside its aiStep.
+ let dragon_lerp = self
+ .dragon
+ .as_ref()
+ .and_then(|d| lerps.get_mut(&(d.id as i32)))
+ .filter(|l| l.steps > 0)
+ .map(|l| {
+ let steps = l.steps;
+ l.steps -= 1;
+ (l.target, l.rotation.map(|r| r.0), steps)
+ });
+ self.tick_remote_dragon(dragon_lerp);
// Boats, minecarts, armor stands, projectiles and the rest
// interpolate like mobs.
let mut step = |id: u32, at: Vec3| match lerps.get_mut(&(id as i32)) {
@@ -3681,6 +3769,15 @@ impl Game<'_> {
/// MultiPlayerGameMode.startDestroyBlock / continueDestroyBlock.
fn remote_mining(&mut self, start: bool) {
+ if !self
+ .res
+ .items()
+ .can_destroy_block(self.held(), self.creative)
+ {
+ self.stop_remote_mining();
+ self.mining = None;
+ return;
+ }
let Some(r) = self.remote.as_mut() else {
return;
};
@@ -3820,19 +3917,25 @@ impl Game<'_> {
.and_modify(|p| p.0 = sequence)
.or_insert((sequence, state.0 as i32));
}
- let s = self.res.facts().get(state).sounds.clone();
- let at = [
- f64::from(pos[0]) + 0.5,
- f64::from(pos[1]) + 0.5,
- f64::from(pos[2]) + 0.5,
- ];
- self.audio.play(
- &s.break_sound,
- Some(at),
- (s.volume + 1.0) / 2.0,
- s.pitch * 0.8,
- );
- self.destroy_particles(pos, state);
+ // Block.playerWillDestroy's destroy event (particles and the break
+ // sound), which BaseFireBlock skips: the server's extinguish sound
+ // is all a player hears.
+ let facts = self.res.facts().get(state);
+ if !facts.has_class("BaseFireBlock") {
+ let s = facts.sounds.clone();
+ let at = [
+ f64::from(pos[0]) + 0.5,
+ f64::from(pos[1]) + 0.5,
+ f64::from(pos[2]) + 0.5,
+ ];
+ self.audio.play(
+ &s.break_sound,
+ Some(at),
+ (s.volume + 1.0) / 2.0,
+ s.pitch * 0.8,
+ );
+ self.destroy_particles(pos, state);
+ }
// The server's block update follows (waterlogged blocks leave water).
let air = self.world.air;
if self.world.set(pos[0], pos[1], pos[2], air).is_ok() {
@@ -4506,6 +4609,7 @@ impl Game<'_> {
}
KeyCode::KeyE => {
// The inventory opens without telling the server.
+ self.recipe_ui.reset_screen();
self.screen = if self.creative {
Screen::Creative
} else {
@@ -4772,6 +4876,7 @@ impl Game<'_> {
self.remote_close_local();
}
fn remote_close_local(&mut self) {
+ self.recipe_ui.reset_screen();
if let Some(r) = self.remote.as_mut() {
r.menu = None;
r.menu_title = None;
@@ -4807,16 +4912,20 @@ impl Game<'_> {
if self.open_menu_name(p.container) != "minecraft:merchant" {
return;
}
- let cost = |g: &Self, c: &play::NetCost| horizon_client::merchant::ItemCost {
- stack: ItemStack::new(&g.item_name(c.item), c.count.max(0) as u32),
+ let cost = |g: &Self, c: &play::NetCost| {
+ g.to_stack(&Some(c.clone().into()))
+ .map(|stack| horizon_client::merchant::ItemCost { stack })
};
let offers: Vec<_> = p
.offers
.iter()
.filter_map(|o| {
let mut offer = horizon_client::merchant::MerchantOffer {
- base_cost_a: cost(self, &o.cost_a),
- cost_b: o.cost_b.as_ref().map(|b| cost(self, b)),
+ base_cost_a: cost(self, &o.cost_a)?,
+ cost_b: match &o.cost_b {
+ Some(b) => Some(cost(self, b)?),
+ None => None,
+ },
result: self.to_stack(&Some(o.result.clone()))?,
uses: o.uses,
max_uses: o.max_uses,
@@ -4842,6 +4951,9 @@ impl Game<'_> {
}
/// ClientboundOpenScreenPacket for the menus Horizon draws.
fn remote_open_screen(&mut self, p: play::OpenScreen) {
+ // A new RecipeBookComponent owns fresh ghost slots and an overlay,
+ // even when the server replaces a menu without a close packet.
+ self.recipe_ui.reset_screen();
let menu = self
.remote
.as_ref()
diff --git a/crates/horizon-client/src/game/remote_mob_data.rs b/crates/horizon-client/src/game/remote_mob_data.rs
index fbfc5a4fb..c6702dfb6 100644
--- a/crates/horizon-client/src/game/remote_mob_data.rs
+++ b/crates/horizon-client/src/game/remote_mob_data.rs
@@ -4,14 +4,14 @@
//! own simulation, which a client of a server doesn't run.
use super::Game;
use horizon_client::mobs::render::{PROFESSIONS, VILLAGER_TYPES};
-use horizon_client::mobs::{Mob, Species, tame};
+use horizon_client::mobs::{Mob, Species, Who, bat, tame};
use horizon_protocol::entity_data::Value;
use horizon_protocol::registry::Builtin;
/// The values `apply` takes from one SetEntityData packet, by vanilla's entry
/// indices (Entity 0..=7, LivingEntity 8..=14, Mob 15, then the subclasses).
/// `builtin` names the ids of built-in registries.
-fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin) {
+fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin, local_entity: i32) {
let species = m.species();
let baby = super::lan::baby_index(&m.kind.id);
for (index, _, value) in values {
@@ -46,6 +46,21 @@ fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin) {
_ => {}
}
match (species, *index, value) {
+ (Species::Guardian | Species::ElderGuardian, 16, Value::Bool(moving)) => {
+ m.extra.flag2 = *moving;
+ }
+ (Species::Guardian | Species::ElderGuardian, 17, Value::Int(target)) => {
+ // Guardian.onSyncedDataUpdated resets the visual charge clock.
+ m.extra.counter2 = 0;
+ m.extra.flag = *target != 0;
+ m.ai.target = (*target != 0).then_some(if *target == local_entity {
+ Who::Player
+ } else {
+ Who::Mob(*target as u32)
+ });
+ }
+ // Bat.DATA_ID_FLAGS: resting in bit 0.
+ (Species::Bat, 16, Value::Byte(flags)) => bat::set_resting(m, flags & 1 != 0),
// Sheep.DATA_WOOL_ID: the colour in the low nibble, 16 sheared.
(Species::Sheep, 18, Value::Byte(wool)) => {
m.extra.color = (*wool & 15) as u8;
@@ -114,7 +129,7 @@ impl Game<'_> {
return;
};
if let Some(m) = self.mobs.iter_mut().find(|m| m.id == id as u32) {
- apply(m, values, &r.builtin);
+ apply(m, values, &r.builtin, r.entity_id);
}
}
}
@@ -126,6 +141,10 @@ pub(super) fn client_defaults(m: &mut Mob) {
if matches!(m.species(), Species::Wolf | Species::Cat) {
m.extra.color = tame::RED;
}
+ if m.species() == Species::Bat {
+ // Bat's constructor sets resting only on the server; the data default is 0.
+ bat::set_resting(m, false);
+ }
}
#[cfg(test)]
@@ -147,7 +166,7 @@ mod tests {
}
fn apply(m: &mut Mob, values: &[(u8, i32, Value)]) {
- super::apply(m, values, &builtin());
+ super::apply(m, values, &builtin(), 99);
}
fn mob(kind: &str) -> Option {
@@ -158,6 +177,49 @@ mod tests {
Some(m)
}
+ #[test]
+ fn guardian_charge_follows_target_updates_and_caps_at_its_duration() {
+ use horizon_client::mobs::guardian::{beam, client_tick};
+ for (kind, duration) in [("guardian", 80), ("elder_guardian", 60)] {
+ let Some(mut m) = mob(&format!("minecraft:{kind}")) else {
+ return;
+ };
+ assert!(beam(&m, 0.0).is_none());
+ apply(
+ &mut m,
+ &[
+ (16, s::BOOLEAN, Value::Bool(true)),
+ (17, s::INT, Value::Int(99)),
+ ],
+ );
+ assert!(m.extra.flag2);
+ assert_eq!(m.ai.target, Some(Who::Player));
+ assert_eq!(beam(&m, 0.0), Some((0.0, 0.0)));
+ for _ in 0..duration + 10 {
+ client_tick(&mut m);
+ }
+ assert_eq!(beam(&m, 0.0), Some((duration as f32, 1.0)));
+ // Another player or a mob retains its network entity id.
+ apply(&mut m, &[(17, s::INT, Value::Int(42))]);
+ assert_eq!(m.ai.target, Some(Who::Mob(42)));
+ assert_eq!(m.extra.counter2, 0);
+ client_tick(&mut m);
+ assert_eq!(m.extra.counter2, 1);
+ apply(
+ &mut m,
+ &[
+ (16, s::BOOLEAN, Value::Bool(false)),
+ (17, s::INT, Value::Int(0)),
+ ],
+ );
+ client_tick(&mut m);
+ assert!(!m.extra.flag2);
+ assert_eq!(m.ai.target, None);
+ assert_eq!(m.extra.counter2, 0);
+ assert!(beam(&m, 0.0).is_none());
+ }
+ }
+
#[test]
fn a_sheep_takes_its_wool_and_a_calf_its_size() {
let Some(mut m) = mob("minecraft:sheep") else {
@@ -174,6 +236,69 @@ mod tests {
assert!(!calf.is_baby());
}
+ #[test]
+ fn bat_flags_select_continuous_client_clocks_without_changing_motion() {
+ use horizon_client::mobs::{MobTypes, anim, keyframes::Animation};
+ use horizon_core::geometry::Vec3;
+ use serde_json::json;
+ use std::{collections::HashMap, fs, sync::Arc};
+
+ // Original minimal resources keep this regression runnable without vanilla data.
+ let root =
+ std::env::temp_dir().join(format!("horizon-bat-animation-{}", std::process::id()));
+ fs::create_dir(&root).unwrap();
+ fs::write(root.join("entity-types.json"), r#"{"minecraft:bat": {}}"#).unwrap();
+ fs::write(
+ root.join("entity-models.json"),
+ r#"{"minecraft:bat#main": {"children": {"right_wing": {}, "head": {}}}}"#,
+ )
+ .unwrap();
+ let mut types = MobTypes::load(&root).unwrap();
+ fs::remove_dir_all(root).unwrap();
+ let position = Vec3::new(1.0, 2.25, 3.0);
+ let velocity = Vec3::new(0.2, 0.3, 0.4);
+ let mut m = Mob::new(
+ 1,
+ Arc::new(types.types.remove("minecraft:bat").unwrap()),
+ position,
+ 0.0,
+ );
+ assert!(bat::resting(&m));
+ client_defaults(&mut m);
+ assert!(!bat::resting(&m));
+ m.velocity = velocity;
+ m.age = 1;
+ bat::setup_animation_states(&mut m);
+ m.age = 4;
+ apply(&mut m, &[(16, s::BYTE, Value::Byte(2))]);
+ bat::setup_animation_states(&mut m);
+ assert_eq!(m.anim.states[0].started, Some(1.0));
+
+ let curve = Animation::parse(&json!({"length": 1, "looping": true,
+ "bones": {"right_wing": [{"target": "rotation", "keyframes":
+ [[0,[0,0,0],[0,0,0],"linear"],[1,[0,1,0],[0,1,0],"linear"]]}]}}));
+ let animations = HashMap::from([("BatAnimation.BAT_FLYING".into(), curve)]);
+ let wing = |partial| {
+ let mut poses = anim::Poses::new(&m.kind.model);
+ anim::setup(&m, &anim::state(&m, partial), &mut poses, &animations);
+ poses.get("right_wing")[4]
+ };
+ assert!(wing(0.5) > wing(0.0));
+
+ m.age = 5;
+ apply(&mut m, &[(16, s::BYTE, Value::Byte(3))]);
+ bat::setup_animation_states(&mut m);
+ assert!(bat::resting(&m));
+ assert_eq!(m.anim.states[0].started, None);
+ assert_eq!(m.anim.states[1].started, Some(5.0));
+ m.age = 7;
+ apply(&mut m, &[(16, s::BYTE, Value::Byte(0))]);
+ bat::setup_animation_states(&mut m);
+ assert_eq!(m.anim.states[0].started, Some(7.0));
+ assert_eq!(m.anim.states[1].started, None);
+ assert_eq!((m.position, m.velocity), (position, velocity));
+ }
+
#[test]
fn a_wolf_takes_its_owner_flags_and_collar() {
let Some(mut m) = mob("minecraft:wolf") else {
diff --git a/crates/horizon-client/src/game/remote_objects.rs b/crates/horizon-client/src/game/remote_objects.rs
index 251d277f3..05b1a2b9e 100644
--- a/crates/horizon-client/src/game/remote_objects.rs
+++ b/crates/horizon-client/src/game/remote_objects.rs
@@ -3,7 +3,7 @@
//! `handleSetEquipment`): the server simulates them, the client places them
//! where it says and interpolates their moves like a mob's.
use super::Game;
-use horizon_client::entities::{ArmorStand, ItemFrame, Painting};
+use horizon_client::entities::{ArmorStand, Cushion, ItemFrame, Painting};
use horizon_core::geometry::Vec3;
use horizon_protocol::entity_data::Value;
use std::sync::Arc;
@@ -57,6 +57,16 @@ impl Game<'_> {
invisible: false,
check_interval: 0,
});
+ } else if kind == "minecraft:cushion" {
+ // BlockAttachedEntity.pos: `BlockPos.containing` of the spawn.
+ self.cushions.push(Cushion {
+ id,
+ position: at,
+ pos: block,
+ yaw: super::cushions::snap_yaw(yaw),
+ color: 0,
+ check_interval: 0,
+ });
} else if kind == "minecraft:lightning_bolt" {
self.spawn_remote_bolt(id, position);
} else if kind == "minecraft:painting" {
@@ -152,6 +162,11 @@ impl Game<'_> {
}
_ => {}
}
+ } else if let Some(c) = self.cushions.iter_mut().find(|c| c.id == id) {
+ // Cushion.DATA_COLOR (DyeColor, a byte-backed id).
+ if let (8, Value::Id(color)) = (index, value) {
+ c.color = (*color).clamp(0, 15) as u8;
+ }
} else if let Some(b) = self.boats.iter_mut().find(|b| b.id == id) {
// VehicleEntity's hurt, direction and damage; the paddles.
match (index, value) {
@@ -267,6 +282,12 @@ impl Game<'_> {
out.push(json!({"id": a.id, "kind": "minecraft:armor_stand", "position": at(a.position), "yaw": a.yaw,
"small": a.small, "arms": a.show_arms, "no_base_plate": a.no_base_plate, "equipment": items}));
}
+ for c in &self.cushions {
+ out.push(
+ json!({"id": c.id, "kind": "minecraft:cushion", "position": at(c.position),
+ "yaw": c.yaw, "color": c.color_name()}),
+ );
+ }
for f in &self.item_frames {
out.push(
json!({"id": f.id, "kind": f.kind(), "position": f.pos, "facing": f.facing,
@@ -286,6 +307,7 @@ impl Game<'_> {
self.carts.clear();
self.boats.clear();
self.armor_stands.clear();
+ self.cushions.clear();
self.item_frames.clear();
self.paintings.clear();
}
@@ -295,6 +317,7 @@ impl Game<'_> {
self.carts.retain(|c| c.id != id);
self.boats.retain(|b| b.id != id);
self.armor_stands.retain(|a| a.id != id);
+ self.cushions.retain(|c| c.id != id);
self.item_frames.retain(|f| f.id != id);
self.paintings.retain(|p| p.id != id);
self.bolts.retain(|b| b.id != id);
diff --git a/crates/horizon-client/src/game/remote_projectiles.rs b/crates/horizon-client/src/game/remote_projectiles.rs
index a117b1b7c..897999c6b 100644
--- a/crates/horizon-client/src/game/remote_projectiles.rs
+++ b/crates/horizon-client/src/game/remote_projectiles.rs
@@ -3,11 +3,26 @@
//! and `handleSetEntityData`): the server simulates them, the client draws
//! them where it says and interpolates their moves.
use super::{FallStep, FallingBlock, Game, fireworks::fireworks_of, view_vector};
-use horizon_client::projectiles::{Projectile, ProjectileKind};
-use horizon_core::{geometry::Vec3, position::BlockPos};
+use horizon_client::particles::{ParticleOptions, ParticleType};
+use horizon_client::projectiles::{Hit, Projectile, ProjectileKind, ProjectileWorld};
+use horizon_core::{geometry::Vec3, position::BlockPos, random::RandomSource};
use horizon_protocol::entity_data::Value;
use horizon_world::registry::BlockStateId;
+/// An `AreaEffectCloud` a server tracks: what its client tick reads.
+pub(super) struct RemoteCloud {
+ id: u32,
+ at: [f64; 3],
+ radius: f32,
+ waiting: bool,
+ particle: ParticleOptions,
+}
+
+/// `AreaEffectCloud.DEFAULT_PARTICLE`: a white entity effect.
+fn default_cloud_particle() -> ParticleOptions {
+ ParticleOptions::colored(ParticleType::EntityEffect, [1.0; 4])
+}
+
/// The projectile a type id names, as Horizon models it.
fn projectile_kind(kind: &str) -> Option {
Some(match kind {
@@ -37,6 +52,32 @@ fn projectile_kind(kind: &str) -> Option {
})
}
+/// `AbstractArrow.tick` on a client: water drag before the move, air drag
+/// after it, then gravity; a block it reaches holds it (`inGround`).
+fn remote_arrow_tick(p: &mut Projectile, w: &impl ProjectileWorld) {
+ p.previous = p.position;
+ p.age += 1;
+ if p.stuck {
+ return;
+ }
+ let water = w.in_water(p.position);
+ if water {
+ p.velocity = p.velocity.scale(0.6);
+ }
+ let to = p.position.add_vector(p.velocity);
+ if let Some((at, _, _)) = w.clip(p.position, to) {
+ p.position = at;
+ p.velocity = Vec3::ZERO;
+ p.stuck = true;
+ return;
+ }
+ p.position = to;
+ if !water {
+ p.velocity = p.velocity.scale(0.99);
+ }
+ p.velocity.y -= 0.05;
+}
+
impl Game<'_> {
/// `handleAddEntity` for these types; false when `kind` isn't one.
pub(super) fn spawn_remote_projectile(
@@ -92,6 +133,16 @@ impl Game<'_> {
| "minecraft:experience_bottle" => {
self.potions.add_remote_thrown(id, kind, at, velocity);
}
+ "minecraft:dragon_fireball" => self
+ .dragon_fireballs
+ .push(super::dragon::DragonFireball::remote(id, at, velocity)),
+ "minecraft:area_effect_cloud" => self.potions.clouds.push(RemoteCloud {
+ id,
+ at: position,
+ radius: 3.0,
+ waiting: false,
+ particle: default_cloud_particle(),
+ }),
"minecraft:eye_of_ender" => {
self.end
.add_remote_eye(id, position, [velocity.x, velocity.y, velocity.z])
@@ -125,6 +176,31 @@ impl Game<'_> {
pub(super) fn remote_projectile_data(&mut self, id: i32, values: &[(u8, i32, Value)]) {
let id = id as u32;
self.remote_hook_data(id, values);
+ if let Some(at) = self
+ .potions
+ .clouds
+ .iter()
+ .find(|c| c.id == id)
+ .map(|c| c.at)
+ {
+ // AreaEffectCloud.DATA_RADIUS, DATA_WAITING and DATA_PARTICLE.
+ for (index, _, value) in values {
+ let particle = match (index, value) {
+ (10, Value::Particle(p)) => self.received_particle(p, at),
+ _ => None,
+ };
+ let Some(c) = self.potions.clouds.iter_mut().find(|c| c.id == id) else {
+ return;
+ };
+ match (index, value) {
+ (8, Value::Float(r)) => c.radius = *r,
+ (9, Value::Bool(w)) => c.waiting = *w,
+ (10, _) => c.particle = particle.unwrap_or_else(default_cloud_particle),
+ _ => {}
+ }
+ }
+ return;
+ }
if self.rockets.iter().any(|r| r.id == id) {
for (index, _, value) in values {
match (index, value) {
@@ -246,29 +322,34 @@ impl Game<'_> {
}
}
self.falling = falling;
- for p in &mut self.creatures.projectiles {
- p.previous = p.position;
- p.age += 1;
- if matches!(p.kind, ProjectileKind::LargeFireball { .. }) {
- // AbstractHurtingProjectile.tick advances on the client between
- // the tracker updates (ten ticks apart for a ghast's fireball).
- let at = p.position;
- let water = self
- .res
- .facts()
- .get(self.world.block(
- at.x.floor() as i32,
- at.y.floor() as i32,
- at.z.floor() as i32,
- ))
- .fluid
- == horizon_client::blocks::Fluid::Water;
- p.apply_hurting_inertia(water);
- p.position = p.position.add_vector(p.velocity);
- } else {
- p.position = step(p.id, p.position);
+ // Projectiles tick on the client too and fly on between the tracker's
+ // updates (10 to 20 ticks apart); a block stops them there, and only
+ // the server decides what they hit.
+ let mut projectiles = std::mem::take(&mut self.creatures.projectiles);
+ {
+ let w = self.mob_world_base(&[]);
+ let world = super::creatures::ProjWorld {
+ w: &w,
+ targets: Vec::new(),
+ };
+ for p in &mut projectiles {
+ match p.kind {
+ ProjectileKind::Arrow { .. } => remote_arrow_tick(p, &world),
+ // ShulkerBullet.tick: only the server steers it.
+ ProjectileKind::ShulkerBullet { .. } => {
+ p.previous = p.position;
+ p.age += 1;
+ p.position = p.position.add_vector(p.velocity);
+ }
+ _ => {
+ if !matches!(p.tick(&world).1, Hit::None) {
+ p.velocity = Vec3::ZERO;
+ }
+ }
+ }
}
}
+ self.creatures.projectiles = projectiles;
for t in &mut self.tridents {
let id = t.id;
t.remote_step(|at| step(id, at));
@@ -302,12 +383,78 @@ impl Game<'_> {
c.time += 1;
c.position = step(c.id, c.position);
}
- self.potions.step_remote_thrown(&mut step);
+ self.step_remote_thrown();
self.step_remote_hooks(&mut step);
- self.end.step_remote_eyes(|id, at| {
- let at = step(id, Vec3::new(at[0], at[1], at[2]));
- [at.x, at.y, at.z]
- });
+ self.end.step_remote_eyes();
+ self.tick_remote_clouds();
+ // The dragon's fireballs fly on between the tracker's updates, with
+ // their dragon_breath trail (`AbstractHurtingProjectile.getTrailParticle`).
+ let trail = ParticleOptions::simple(ParticleType::DragonBreath);
+ for b in &mut self.dragon_fireballs {
+ let at = b.position;
+ let water = self
+ .res
+ .facts()
+ .get(self.world.block(
+ at.x.floor() as i32,
+ at.y.floor() as i32,
+ at.z.floor() as i32,
+ ))
+ .fluid
+ == horizon_client::blocks::Fluid::Water;
+ b.remote_step(water);
+ let p = b.position;
+ self.particles.add(&trail, [p.x, p.y + 0.5, p.z], [0.0; 3]);
+ }
+ }
+
+ /// `AreaEffectCloud.clientTick`: while waiting, now and then two
+ /// particles near the middle; then about one per square block of the
+ /// disc, drifting (an entity effect stays still).
+ fn tick_remote_clouds(&mut self) {
+ for c in &self.potions.clouds {
+ let r = self.particles.random();
+ if c.waiting && r.next_boolean() {
+ continue;
+ }
+ let (count, spread) = if c.waiting {
+ (2, 0.2)
+ } else {
+ (
+ (std::f32::consts::PI * c.radius * c.radius).ceil() as i32,
+ c.radius,
+ )
+ };
+ for _ in 0..count {
+ let r = self.particles.random();
+ let angle = r.next_float() * std::f32::consts::TAU;
+ let distance = r.next_float().sqrt() * spread;
+ let at = [
+ c.at[0] + f64::from(angle.cos() * distance),
+ c.at[1],
+ c.at[2] + f64::from(angle.sin() * distance),
+ ];
+ let (options, velocity) = if c.particle.ty == ParticleType::EntityEffect {
+ let default = c.waiting && r.next_boolean();
+ let options = if default {
+ default_cloud_particle()
+ } else {
+ c.particle.clone()
+ };
+ (options, [0.0; 3])
+ } else if c.waiting {
+ (c.particle.clone(), [0.0; 3])
+ } else {
+ let v = [
+ (0.5 - r.next_double()) * 0.15,
+ f64::from(0.01f32),
+ (0.5 - r.next_double()) * 0.15,
+ ];
+ (c.particle.clone(), v)
+ };
+ self.particles.add(&options, at, velocity);
+ }
+ }
}
pub(super) fn remove_remote_projectile(&mut self, id: u32) {
@@ -318,6 +465,8 @@ impl Game<'_> {
self.rockets.retain(|r| r.id != id);
self.crystals.retain(|c| c.id != id);
self.potions.remove_remote_thrown(id);
+ self.potions.clouds.retain(|c| c.id != id);
+ self.dragon_fireballs.retain(|b| b.id != id);
self.end.remove_remote_eye(id);
self.remove_remote_hook(id);
}
@@ -330,7 +479,11 @@ impl Game<'_> {
self.tridents.clear();
self.rockets.clear();
self.crystals.clear();
+ // The dragon a server sent goes with its crystals.
+ self.dragon = None;
self.potions.clear_thrown();
+ self.potions.clouds.clear();
+ self.dragon_fireballs.clear();
self.end.clear_remote_eyes();
self.clear_remote_hooks();
}
diff --git a/crates/horizon-client/src/game/remote_vehicles.rs b/crates/horizon-client/src/game/remote_vehicles.rs
index 620aff97c..877ed66e6 100644
--- a/crates/horizon-client/src/game/remote_vehicles.rs
+++ b/crates/horizon-client/src/game/remote_vehicles.rs
@@ -56,6 +56,11 @@ impl Game<'_> {
let p = a.position;
hits.push((a.id as i32, d, [p.x, p.y, p.z]));
}
+ if let Some((i, d, _)) = self.cushion_hit() {
+ let c = &self.cushions[i];
+ let p = c.position;
+ hits.push((c.id as i32, d, [p.x, p.y, p.z]));
+ }
if let Some((i, d)) = self.boat_hit() {
let p = self.boats[i].position;
hits.push((self.boats[i].id as i32, d, [p.x, p.y, p.z]));
@@ -79,6 +84,8 @@ impl Game<'_> {
hits.push((c.id as i32, d, [p.x, p.y, p.z]));
}
}
+ // EnderDragonPart.isPickable: the server's dragon hurts by part.
+ hits.extend(self.remote_dragon_part_hit(eye, dir, reach));
}
let (id, distance, origin) = hits.into_iter().min_by(|a, b| a.1.total_cmp(&b.1))?;
let point = at(distance);
@@ -138,7 +145,10 @@ impl Game<'_> {
self.position_rider();
return true;
}
- let seat = if let Some(i) = self.boats.iter().position(|b| b.id == id) {
+ let seat = if let Some(c) = self.cushions.iter().find(|c| c.id == id) {
+ // The seat does not move; the rider sits on the cushion.
+ Some(c.seat())
+ } else if let Some(i) = self.boats.iter().position(|b| b.id == id) {
// AbstractBoat.tick on the controlling client: steer and move.
let mut b = self.boats[i].clone();
b.input = [keys.left, keys.right, keys.forward, keys.backward];
diff --git a/crates/horizon-client/src/game/saving.rs b/crates/horizon-client/src/game/saving.rs
index 378ffe9ce..1c6632562 100644
--- a/crates/horizon-client/src/game/saving.rs
+++ b/crates/horizon-client/src/game/saving.rs
@@ -179,6 +179,7 @@ impl Game<'_> {
if keys.is_empty() {
self.prune_entity_memory();
self.save_raids();
+ self.save.save_forced_chunks(&self.streaming.forced)?;
self.save_game_metadata()?;
save.stage = Stage::Writes;
}
diff --git a/crates/horizon-client/src/game/server_mods.rs b/crates/horizon-client/src/game/server_mods.rs
new file mode 100644
index 000000000..db88639eb
--- /dev/null
+++ b/crates/horizon-client/src/game/server_mods.rs
@@ -0,0 +1,356 @@
+//! Server mods in the game (`docs/mods.md`, "Server mods"): the game that
+//! simulates the world (in-frame single player, the server thread, a LAN
+//! host, the dedicated server) runs them. At the end of each tick it tells
+//! them who joined and left, then what players said, broke and placed since
+//! the last one, then ticks them; a player's command reaches them at once.
+//! Their lines go to the players they name. With no server mod on, each hook
+//! here is one check.
+use super::{Game, lan::LanHost};
+#[cfg(not(feature = "mods"))]
+use horizon_world::registry::BlockStateId;
+
+/// Who did something a server mod hears about, resolved at the tick's end.
+#[cfg_attr(not(feature = "mods"), allow(dead_code))]
+pub(super) enum Who {
+ /// The window's own player, in-frame.
+ Local,
+ /// A connection's player (the server thread's owner is one).
+ Conn(u32),
+ Uuid(u128),
+}
+
+/// What players did during a tick, for the server mods at its end.
+#[cfg_attr(not(feature = "mods"), allow(dead_code))]
+pub(super) enum ModEvent {
+ Chat(Who, String),
+ Block {
+ who: Who,
+ pos: [i32; 3],
+ state: String,
+ placed: bool,
+ },
+}
+
+#[cfg(not(feature = "mods"))]
+impl Game<'_> {
+ pub(super) fn tick_server_mods(&mut self) {}
+ pub(super) fn mod_block_event(&mut self, _pos: [i32; 3], _state: BlockStateId, _placed: bool) {}
+ pub(super) fn mod_player_chat(&mut self, _who: Who, _text: &str) {}
+ pub(super) fn local_server_mod_command(&mut self, _line: &str) -> bool {
+ false
+ }
+ pub(super) fn guest_server_mod_command(
+ &mut self,
+ _host: &mut LanHost,
+ _conn: u32,
+ _line: &str,
+ ) -> bool {
+ false
+ }
+}
+
+#[cfg(feature = "mods")]
+mod run {
+ use super::{Game, LanHost, ModEvent, Who};
+ use horizon_client::{
+ command::args::GameMode as Mode,
+ lan::data::component_nbt,
+ text::{self, Text},
+ };
+ use horizon_mods::{BlockPos, GameMode, Output, ServerPlayer};
+ use horizon_protocol::packets::play::{self, Clientbound as C};
+ use horizon_world::registry::BlockStateId;
+
+ /// `UUID.toString()`.
+ fn uuid_string(u: u128) -> String {
+ let h = format!("{u:032x}");
+ format!(
+ "{}-{}-{}-{}-{}",
+ &h[..8],
+ &h[8..12],
+ &h[12..16],
+ &h[16..20],
+ &h[20..]
+ )
+ }
+
+ fn mode(id: i32) -> GameMode {
+ match id {
+ 1 => GameMode::Creative,
+ 2 => GameMode::Adventure,
+ 3 => GameMode::Spectator,
+ _ => GameMode::Survival,
+ }
+ }
+
+ impl Game<'_> {
+ /// The players on this game's server: the window's own player
+ /// in-frame, then every connection's. `host` is the hub when it is
+ /// out of `self.lan`.
+ pub(in crate::game) fn server_players(&self, host: Option<&LanHost>) -> Vec {
+ let mut out = Vec::new();
+ if self.options.execution_role.is_client()
+ && self.remote.is_none()
+ && let Some(p) = &self.player
+ {
+ out.push(ServerPlayer {
+ id: self.player_uuid_string(),
+ name: crate::game::chat::PLAYER_NAME.to_owned(),
+ x: p.position.x,
+ y: p.position.y,
+ z: p.position.z,
+ dimension: self.dim.id().to_owned(),
+ game_mode: match self.mode {
+ Mode::Survival => GameMode::Survival,
+ Mode::Creative => GameMode::Creative,
+ Mode::Adventure => GameMode::Adventure,
+ Mode::Spectator => GameMode::Spectator,
+ },
+ health: p.health.health(),
+ });
+ }
+ let guests = host
+ .or(self.lan.as_ref())
+ .into_iter()
+ .flat_map(|h| &h.hub.guests);
+ out.extend(guests.filter(|g| g.placed).map(|g| ServerPlayer {
+ id: uuid_string(g.uuid),
+ name: g.name.clone(),
+ x: g.position[0],
+ y: g.position[1],
+ z: g.position[2],
+ dimension: g.dimension.clone(),
+ game_mode: mode(g.game_mode),
+ health: g.health,
+ }));
+ out
+ }
+
+ /// Whether what players do now reaches server mods: one is running
+ /// and this game simulates the world.
+ fn hears_players(&self) -> bool {
+ self.mods.server && self.remote.is_none()
+ }
+
+ /// The tick's end: joins and leaves, the tick's events, the tick hook.
+ pub(in crate::game) fn tick_server_mods(&mut self) {
+ if !self.options.execution_role.is_client() {
+ // A server has no frames to check its dev mods in.
+ self.reload_dev_mods();
+ }
+ if !self.mods.server {
+ return;
+ }
+ let players = self.server_players(None);
+ let snapshot = self.mod_snapshot_with(players.clone());
+ self.mods.mods.set_snapshot(snapshot);
+ let before = std::mem::take(&mut self.mods.players);
+ for p in before
+ .iter()
+ .filter(|p| !players.iter().any(|q| q.id == p.id))
+ {
+ self.mods.mods.player_left(p);
+ }
+ for p in players
+ .iter()
+ .filter(|p| !before.iter().any(|q| q.id == p.id))
+ {
+ self.mods.mods.player_joined(p);
+ }
+ for event in std::mem::take(&mut self.mods.events) {
+ let who = |w: &Who| {
+ let id = match w {
+ Who::Local => self.player_uuid_string(),
+ Who::Uuid(u) => uuid_string(*u),
+ Who::Conn(c) => {
+ let guests = self.lan.iter().flat_map(|h| &h.hub.guests);
+ let g = guests.into_iter().find(|g| g.conn == *c);
+ g.map_or_else(String::new, |g| uuid_string(g.uuid))
+ }
+ };
+ players.iter().chain(&before).find(|p| p.id == id).cloned()
+ };
+ match event {
+ ModEvent::Chat(w, text) => {
+ if let Some(p) = who(&w) {
+ self.mods.mods.player_chat(&p, &text);
+ }
+ }
+ ModEvent::Block {
+ who: w,
+ pos,
+ state,
+ placed,
+ } => {
+ let Some(p) = who(&w) else { continue };
+ let pos = BlockPos {
+ x: pos[0],
+ y: pos[1],
+ z: pos[2],
+ };
+ if placed {
+ self.mods.mods.block_placed(&p, pos, &state);
+ } else {
+ self.mods.mods.block_broken(&p, pos, &state);
+ }
+ }
+ }
+ }
+ self.mods.mods.server_tick(self.ticks);
+ self.mods.players = players;
+ self.show_mod_output();
+ }
+
+ /// A player broke (or placed) `state` at `pos`: the player acting now.
+ pub(in crate::game) fn mod_block_event(
+ &mut self,
+ pos: [i32; 3],
+ state: BlockStateId,
+ placed: bool,
+ ) {
+ if !self.hears_players() {
+ return;
+ }
+ let who = self.lan_acting.map_or(Who::Local, Who::Conn);
+ let state = self.state_string(state);
+ self.mods.events.push(ModEvent::Block {
+ who,
+ pos,
+ state,
+ placed,
+ });
+ }
+
+ /// A chat message a player sent.
+ pub(in crate::game) fn mod_player_chat(&mut self, who: Who, text: &str) {
+ if self.hears_players() {
+ self.mods.events.push(ModEvent::Chat(who, text.to_owned()));
+ }
+ }
+
+ /// `/line` typed by the window's own player in-frame: true when a
+ /// server mod owns it.
+ pub(in crate::game) fn local_server_mod_command(&mut self, line: &str) -> bool {
+ if !self.hears_players() {
+ return false;
+ }
+ let players = self.server_players(None);
+ let Some(me) = players.first().cloned() else {
+ return false;
+ };
+ let snapshot = self.mod_snapshot_with(players);
+ self.mods.mods.set_snapshot(snapshot);
+ let owned = self.mods.mods.server_command(&me, line);
+ self.show_mod_output();
+ owned
+ }
+
+ /// `/line` typed by a connection's player: true when a server mod
+ /// owns it, whatever the player's permission level.
+ pub(in crate::game) fn guest_server_mod_command(
+ &mut self,
+ host: &mut LanHost,
+ conn: u32,
+ line: &str,
+ ) -> bool {
+ if !self.hears_players() {
+ return false;
+ }
+ let Some(g) = host.hub.guest(conn) else {
+ return false;
+ };
+ let id = uuid_string(g.uuid);
+ let players = self.server_players(Some(host));
+ let Some(me) = players.iter().find(|p| p.id == id).cloned() else {
+ return false;
+ };
+ let snapshot = self.mod_snapshot_with(players);
+ self.mods.mods.set_snapshot(snapshot);
+ if !self.mods.mods.server_command(&me, line) {
+ return false;
+ }
+ self.route_mod_output(Some(host));
+ true
+ }
+
+ /// Sends a server mod's line to the player with this id: the
+ /// window's own chat for the local player, a system message for a
+ /// connection's. `host` is the hub when it is out of `self.lan`.
+ pub(in crate::game) fn tell_server_player(
+ &mut self,
+ host: Option<&mut LanHost>,
+ id: &str,
+ line: Text,
+ ) {
+ let local = self.options.execution_role.is_client()
+ && self.remote.is_none()
+ && self.player.is_some();
+ if local && self.player_uuid_string() == id {
+ self.add_mod_chat_line(line);
+ return;
+ }
+ let Some(host) = host.or(self.lan.as_mut()) else {
+ return;
+ };
+ let to = host
+ .hub
+ .guests
+ .iter()
+ .find(|g| g.placed && uuid_string(g.uuid) == id);
+ if let Some(conn) = to.map(|g| g.conn) {
+ host.hub.send_to(
+ conn,
+ &C::SystemChat(play::SystemChat {
+ content: component_nbt(&line.component()),
+ overlay: false,
+ }),
+ );
+ }
+ }
+
+ /// Routes a server mod's output: `Tell` and `Reply` to their player,
+ /// `Broadcast` to everyone and the log.
+ pub(in crate::game) fn route_server_output(
+ &mut self,
+ mut host: Option<&mut LanHost>,
+ output: Output,
+ ) {
+ let named = |name: &str, text: String| {
+ let mut line = Text::literal(format!("[{name}] ")).with_color(text::GRAY);
+ line.push(Text::literal(text));
+ line
+ };
+ match output {
+ Output::Tell { name, player, text } => {
+ self.tell_server_player(host, &player, named(&name, text));
+ }
+ Output::Reply {
+ player,
+ text,
+ error,
+ } => {
+ let line = Text::literal(text);
+ let line = if error {
+ line.with_color(text::RED)
+ } else {
+ line
+ };
+ self.tell_server_player(host, &player, line);
+ }
+ Output::Broadcast { name, text } => {
+ let plain = format!("[{name}] {text}");
+ if self.options.dedicated.is_some() {
+ super::super::dedicated::log("INFO", &plain);
+ } else {
+ log::info!("{plain}");
+ }
+ for p in self.server_players(host.as_deref()) {
+ let line = named(&name, text.clone());
+ self.tell_server_player(host.as_deref_mut(), &p.id, line);
+ }
+ }
+ _ => {}
+ }
+ }
+ }
+}
diff --git a/crates/horizon-client/src/game/server_travel.rs b/crates/horizon-client/src/game/server_travel.rs
index 158b2301e..bcaf37a76 100644
--- a/crates/horizon-client/src/game/server_travel.rs
+++ b/crates/horizon-client/src/game/server_travel.rs
@@ -13,6 +13,7 @@ use super::respawn_anchor::RespawnConfig;
use super::{Dim, Game, lan::LanHost};
use horizon_client::{lan::hub::TickCtx, text::Text};
use horizon_core::{geometry::Vec3, position::BlockPos};
+use horizon_protocol::packets::play::{self, Clientbound as C};
/// A trip under way: the player is in the destination's level, whose
/// chunks are loading around `target`.
@@ -103,8 +104,35 @@ impl Game<'_> {
if let Some(pos) = self.nether_portal_inside(at) {
self.portals.player.set_inside(pos, PLAYER_DELAY);
}
+ // `ServerPlayer.showEndCredits`: the player stays out of the world while the
+ // poem rolls, then PERFORM_RESPAWN takes it to its respawn point.
+ let play = host.play.entry(conn).or_default();
+ if play.won_game {
+ if std::mem::take(&mut play.respawn_requested) {
+ play.won_game = false;
+ self.start_server_trip(host, conn, Dim::Overworld, None);
+ self.portals.player.cooldown = 10;
+ }
+ return;
+ }
let (end_portal, gateway) = self.inside_end_portals(at, WIDTH, HEIGHT);
if end_portal {
+ // EndPortalBlock.entityInside: the first exit from the End shows the credits.
+ if self.dim == Dim::End && !host.hub.guest(conn).is_some_and(|g| g.seen_credits) {
+ if let Some(g) = host.hub.guests.iter_mut().find(|g| g.conn == conn) {
+ g.seen_credits = true;
+ }
+ host.play.entry(conn).or_default().won_game = true;
+ // ClientboundGameEventPacket.WIN_GAME.
+ host.hub.send_to(
+ conn,
+ &C::GameEvent(play::GameEvent {
+ event: 4,
+ param: 0.0,
+ }),
+ );
+ return;
+ }
if self.portals.player.cooldown == 0 {
let target = if self.dim == Dim::End {
Dim::Overworld
diff --git a/crates/horizon-client/src/game/sign_edit.rs b/crates/horizon-client/src/game/sign_edit.rs
index 2f50ad9ab..2d010f1f5 100644
--- a/crates/horizon-client/src/game/sign_edit.rs
+++ b/crates/horizon-client/src/game/sign_edit.rs
@@ -6,9 +6,9 @@ use horizon_client::{
containers::BlockEntity,
hud::{HudBatch, rgba},
signs::wood_type,
+ ui::EditBox,
};
use horizon_protocol::packets::play::{self, Clientbound as C, Serverbound as S};
-use std::time::Instant;
use winit::{
event::{ElementState, KeyEvent},
keyboard::{KeyCode, PhysicalKey},
@@ -18,7 +18,16 @@ pub(super) struct SignEditor {
pub pos: (i32, i32, i32),
pub front: bool,
pub line: usize,
- pub opened: Instant,
+ /// The line being edited (`AbstractSignEditScreen`'s text field).
+ edit: EditBox,
+}
+
+fn sign_box() -> EditBox {
+ let mut edit = EditBox::new(0.0, 0.0, 90.0, 9.0);
+ edit.bordered = false;
+ edit.max_length = 384;
+ edit.focused = true;
+ edit
}
impl Game<'_> {
@@ -28,8 +37,9 @@ impl Game<'_> {
pos,
front,
line: 0,
- opened: Instant::now(),
+ edit: sign_box(),
});
+ self.sign_load();
self.keys.clear();
self.right = false;
self.capture(false);
@@ -140,97 +150,154 @@ impl Game<'_> {
/// A key and the text it typed on the sign being edited (the keyboard,
/// or a controller's on-screen keyboard).
pub(super) fn sign_input(&mut self, key: Option, typed: Option<&str>) {
- let Some(edit) = self.sign_edit.as_mut() else {
+ if self.sign_edit.is_none() {
return;
- };
- let front = edit.front;
- let id = sign_line_id(front, edit.line);
+ }
match key {
Some(KeyCode::Escape) => self.close_sign_editor(),
- Some(KeyCode::ArrowUp) => edit.line = (edit.line + 3) & 3,
+ Some(KeyCode::ArrowUp) => self.sign_move_line(3),
Some(KeyCode::ArrowDown | KeyCode::Enter | KeyCode::NumpadEnter) => {
- edit.line = (edit.line + 1) & 3
+ self.sign_move_line(1)
}
- Some(KeyCode::Backspace) => {
- let (line, key) = (edit.line, edit.pos);
- if let Some(BlockEntity::Sign(sign)) = self.world.block_entities.get_mut(&key) {
- let text = if front {
- &mut sign.front
- } else {
- &mut sign.back
- };
- let value = &mut text.lines[line];
- if !self.text_selection.take(id, value) {
- value.pop();
- }
- }
+ Some(
+ key @ (KeyCode::ArrowLeft
+ | KeyCode::ArrowRight
+ | KeyCode::Home
+ | KeyCode::End
+ | KeyCode::Backspace
+ | KeyCode::Delete),
+ ) => {
+ let (ctrl, shift) = (self.has_control_down(), self.has_shift_down());
+ self.sign_change(|edit| {
+ edit.key(key, ctrl, shift);
+ });
}
_ => {
let Some(typed) = typed else {
return;
};
- let line = edit.line;
- let atlas = &self.gui.atlas;
- let width = |s: &str| HudBatch::text_width(atlas, s);
- let Some(key) = self.sign_edit.as_ref().map(|e| e.pos) else {
- return;
- };
- if let Some(BlockEntity::Sign(sign)) = self.world.block_entities.get_mut(&key) {
- let max = sign.max_line_width();
- let text = if front {
- &mut sign.front
- } else {
- &mut sign.back
- };
- self.text_selection.take(id, &mut text.lines[line]);
- for ch in typed.chars().filter(|c| !c.is_control()) {
- let mut next = text.lines[line].clone();
- next.push(ch);
- // TextFieldHelper filter: font.width(s) <= maxTextLineWidth.
- if width(&next) <= max {
- text.lines[line] = next;
- }
- }
- }
+ self.sign_change(|edit| edit.insert_text(typed));
}
}
}
- /// `TextFieldHelper.keyPressed`'s shortcuts on the edited line: a paste
- /// (formatting and carriage returns removed) is taken whole or not at
- /// all, as the line must stay within the sign's width.
- fn sign_shortcut(&mut self, shortcut: horizon_client::clipboard::EditShortcut) {
+ /// Up or down a line: the box writes the line it leaves and loads the next.
+ fn sign_move_line(&mut self, by: usize) {
+ self.sign_write();
+ if let Some(edit) = self.sign_edit.as_mut() {
+ edit.line = (edit.line + by) & 3;
+ }
+ self.sign_load();
+ }
+ /// The edited line's text.
+ fn sign_current(&self) -> Option {
+ let edit = self.sign_edit.as_ref()?;
+ let BlockEntity::Sign(sign) = self.world.block_entities.get(&edit.pos)? else {
+ return None;
+ };
+ let side = if edit.front { &sign.front } else { &sign.back };
+ Some(side.lines[edit.line].clone())
+ }
+ /// `EditBox.setValue` from the line now being edited.
+ fn sign_load(&mut self) {
+ let Some(text) = self.sign_current() else {
+ return;
+ };
+ if let Some(edit) = self.sign_edit.as_mut() {
+ edit.edit.set_value(&text);
+ }
+ }
+ /// The box's text onto the sign, when it still fits the line.
+ fn sign_write(&mut self) {
let Some(edit) = self.sign_edit.as_ref() else {
return;
};
- let (key, front, line) = (edit.pos, edit.front, edit.line);
- let Some(BlockEntity::Sign(sign)) = self.world.block_entities.get_mut(&key) else {
+ let (pos, front, line, value) = (edit.pos, edit.front, edit.line, edit.edit.value.clone());
+ if !self.sign_fits(&value) {
+ return;
+ }
+ let Some(BlockEntity::Sign(sign)) = self.world.block_entities.get_mut(&pos) else {
return;
};
- let max = sign.max_line_width();
- let text = if front {
+ let side = if front {
&mut sign.front
} else {
&mut sign.back
};
- let atlas = &self.gui.atlas;
- self.text_selection.apply(
- sign_line_id(front, line),
- &mut text.lines[line],
- shortcut,
- &mut self.clipboard,
- |value, pasted| {
- let next = format!(
- "{value}{}",
- horizon_client::clipboard::helper_clipboard_contents(pasted)
- );
- HudBatch::text_width(atlas, &next) <= max && {
- *value = next;
- true
- }
- },
- );
+ side.lines[line] = value;
}
- /// Done button (`this.width / 2 - 100, this.height / 4 + 144`, 200x20).
+ /// TextFieldHelper: `font.width(s) <= maxTextLineWidth`.
+ fn sign_fits(&self, value: &str) -> bool {
+ let Some(edit) = self.sign_edit.as_ref() else {
+ return false;
+ };
+ let Some(BlockEntity::Sign(sign)) = self.world.block_entities.get(&edit.pos) else {
+ return false;
+ };
+ HudBatch::text_width(&self.gui.atlas, value) <= sign.max_line_width()
+ }
+ /// Edit the line, then keep the result only when it still fits.
+ fn sign_change(&mut self, f: impl FnOnce(&mut EditBox)) {
+ let before = self
+ .sign_edit
+ .as_ref()
+ .map(|e| (e.edit.value.clone(), e.edit.cursor, e.edit.highlight));
+ if let Some(edit) = self.sign_edit.as_mut() {
+ f(&mut edit.edit);
+ let clean = horizon_client::clipboard::strip_formatting(&edit.edit.value);
+ if clean != edit.edit.value {
+ let n = horizon_client::ui::char_len(&clean);
+ edit.edit.value = clean;
+ edit.edit.cursor = edit.edit.cursor.min(n);
+ edit.edit.highlight = edit.edit.highlight.min(n);
+ }
+ }
+ self.sign_keep(before);
+ }
+ /// Write the box when the line still fits; otherwise put the old text back.
+ fn sign_keep(&mut self, before: Option<(String, usize, usize)>) {
+ let value = self
+ .sign_edit
+ .as_ref()
+ .map(|e| e.edit.value.clone())
+ .unwrap_or_default();
+ if self.sign_fits(&value) {
+ self.sign_write();
+ } else if let (Some(edit), Some((value, cursor, highlight))) =
+ (self.sign_edit.as_mut(), before)
+ {
+ edit.edit.value = value;
+ edit.edit.cursor = cursor;
+ edit.edit.highlight = highlight;
+ }
+ }
+ /// `TextFieldHelper.keyPressed`'s shortcuts on the edited line. Paste
+ /// reads `getClipboardContents` before `insertText`. A paste that would
+ /// pass the sign's width is refused whole.
+ fn sign_shortcut(&mut self, shortcut: horizon_client::clipboard::EditShortcut) {
+ if shortcut == horizon_client::clipboard::EditShortcut::Paste {
+ let raw = horizon_client::clipboard::Clipboard::get(&mut self.clipboard);
+ let text = horizon_client::clipboard::helper_clipboard_contents(&raw);
+ self.sign_change(|edit| edit.insert_text(&text));
+ return;
+ }
+ let before = self
+ .sign_edit
+ .as_ref()
+ .map(|e| (e.edit.value.clone(), e.edit.cursor, e.edit.highlight));
+ if let Some(edit) = self.sign_edit.as_mut() {
+ edit.edit.shortcut(shortcut, &mut self.clipboard);
+ let clean = horizon_client::clipboard::strip_formatting(&edit.edit.value);
+ if clean != edit.edit.value {
+ let n = horizon_client::ui::char_len(&clean);
+ edit.edit.value = clean;
+ edit.edit.cursor = edit.edit.cursor.min(n);
+ edit.edit.highlight = edit.edit.highlight.min(n);
+ }
+ }
+ self.sign_keep(before);
+ }
+ /// Done button (`this.width / 2 - 100, this.height / 4 + 144`, 200x20),
+ /// or a click on a line (`EditBox.onClick`).
pub(super) fn sign_edit_click(&mut self, size: [u32; 2]) {
let b = HudBatch::new(size);
let (x, y) = (b.width / 2.0 - 100.0, b.height / 4.0 + 144.0);
@@ -238,7 +305,56 @@ impl Game<'_> {
if mx >= x && mx < x + 200.0 && my >= y && my < y + 20.0 {
self.audio.play_button_click();
self.close_sign_editor();
+ return;
}
+ let Some((cx, top, line_height, max_width, lines)) = self.sign_lines(b.width) else {
+ return;
+ };
+ let shift = self.has_shift_down();
+ for i in 0..lines.len() {
+ let ly = top + i as f32 * line_height;
+ let left = cx - max_width / 2.0;
+ if mx < left || mx >= left + max_width || my < ly || my >= ly + line_height {
+ continue;
+ }
+ let current = self.sign_edit.as_ref().map(|e| e.line);
+ if current != Some(i) {
+ self.sign_move_line((i + 4 - current.unwrap_or(0)) & 3);
+ }
+ let value = self
+ .sign_edit
+ .as_ref()
+ .map(|e| e.edit.value.clone())
+ .unwrap_or_default();
+ let lw = HudBatch::text_width(&self.gui.atlas, &value);
+ let text_x = cx - (lw as i32 / 2) as f32;
+ if let Some(edit) = self.sign_edit.as_mut() {
+ edit.edit.x = text_x;
+ edit.edit.y = ly;
+ edit.edit.width = max_width;
+ edit.edit.click(&self.gui.atlas, mx, shift, false);
+ }
+ self.sign_write();
+ return;
+ }
+ }
+ /// Centre x, the first line's y, the line height, the width limit and the four lines.
+ fn sign_lines(&self, width: f32) -> Option<(f32, f32, f32, f32, Vec)> {
+ let edit = self.sign_edit.as_ref()?;
+ let BlockEntity::Sign(sign) = self.world.block_entities.get(&edit.pos)? else {
+ return None;
+ };
+ let text = if edit.front { &sign.front } else { &sign.back };
+ let line_height = sign.line_height();
+ let y = if sign.hanging { 125.0 } else { 90.0 };
+ let top = y - (4.0 * line_height / 2.0).floor();
+ Some((
+ width / 2.0,
+ top,
+ line_height,
+ sign.max_line_width(),
+ text.lines.to_vec(),
+ ))
}
pub(super) fn draw_sign_editor(&self, batch: &mut HudBatch) {
let Some(edit) = &self.sign_edit else { return };
@@ -304,23 +420,48 @@ impl Game<'_> {
let color = rgba((color >> 16) as u8, (color >> 8) as u8, color as u8, 255);
let line_height = sign.line_height();
let midpoint = (4.0 * line_height / 2.0).floor();
- let blink = (edit.opened.elapsed().as_millis() / 300) % 2 == 0;
for (i, line) in text.lines.iter().enumerate() {
- let lw = HudBatch::text_width(atlas, line);
+ let shown = if i == edit.line {
+ edit.edit.value.as_str()
+ } else {
+ line.as_str()
+ };
+ let lw = HudBatch::text_width(atlas, shown);
let x = cx - (lw as i32 / 2) as f32;
let ly = y + i as f32 * line_height - midpoint;
- batch.text(atlas, line, x, ly, color, false);
- if i == edit.line
- && self
- .text_selection
- .covers(sign_line_id(edit.front, i), line)
- {
- // AbstractSignEditScreen: the selection from its start to its end.
- batch.text_highlight(atlas, (x, ly, lw, line_height), true);
+ batch.text(atlas, shown, x, ly, color, false);
+ if i != edit.line {
+ continue;
}
- if i == edit.line && blink {
- // TextCursorUtils.extractAppendCursor: an underscore after the text.
- batch.text(atlas, "_", x + lw, ly, color, false);
+ let value = &edit.edit.value;
+ if edit.edit.cursor != edit.edit.highlight {
+ let (a, b) = (
+ edit.edit.cursor.min(edit.edit.highlight),
+ edit.edit.cursor.max(edit.edit.highlight),
+ );
+ let pre: String = value.chars().take(a).collect();
+ let sel: String = value.chars().skip(a).take(b - a).collect();
+ batch.text_highlight(
+ atlas,
+ (
+ x + HudBatch::text_width(atlas, &pre),
+ ly,
+ HudBatch::text_width(atlas, &sel),
+ line_height,
+ ),
+ true,
+ );
+ }
+ if edit.edit.cursor_visible() {
+ let pre: String = value.chars().take(edit.edit.cursor).collect();
+ batch.fill(
+ atlas,
+ x + HudBatch::text_width(atlas, &pre),
+ ly,
+ 1.0,
+ line_height,
+ color,
+ );
}
}
let (bx, by) = (w / 2.0 - 100.0, h / 4.0 + 144.0);
@@ -344,8 +485,3 @@ impl Game<'_> {
);
}
}
-
-/// The sign line being edited, for Select All.
-fn sign_line_id(front: bool, line: usize) -> horizon_client::clipboard::FieldId {
- (if front { "sign.front" } else { "sign.back" }, line)
-}
diff --git a/crates/horizon-client/src/game/spill.rs b/crates/horizon-client/src/game/spill.rs
index b7283c57b..7f370507f 100644
--- a/crates/horizon-client/src/game/spill.rs
+++ b/crates/horizon-client/src/game/spill.rs
@@ -398,8 +398,12 @@ impl GameLevel<'_> {
}
}
PreRemove::Campfire => {
- for _ in 0..4 {
- self.drop_item_stack(pos, None);
+ let items = match &entity {
+ Some(BlockEntity::Campfire(c)) => c.items.clone(),
+ _ => Default::default(),
+ };
+ for stack in items {
+ self.drop_item_stack(pos, stack);
}
}
PreRemove::Jukebox => {
diff --git a/crates/horizon-client/src/game/stations.rs b/crates/horizon-client/src/game/stations.rs
index f7f05e1c4..56b19e393 100644
--- a/crates/horizon-client/src/game/stations.rs
+++ b/crates/horizon-client/src/game/stations.rs
@@ -135,8 +135,10 @@ pub(super) struct Station {
pub(super) offers: EnchantOffers,
bookcases: i32,
pub(super) anvil: AnvilResult,
- /// Anvil rename box text.
+ /// Anvil rename box text (`AnvilMenu.itemName`).
pub(super) name: String,
+ /// The box itself (`AnvilScreen`'s `EditBox` of 50, unbordered).
+ pub(super) rename: horizon_client::ui::EditBox,
/// Stonecutter: the menu's recipe list (horizon_client::menu), its
/// `selectedRecipeIndex` and the screen's first shown index.
pub(super) stone: horizon_client::menu::Stonecutter,
@@ -146,8 +148,6 @@ pub(super) struct Station {
anvil_last: Option,
/// Anvil on a server: the name last sent (the client menu's `itemName`).
pub(super) anvil_sent: Option,
- /// When the screen opened: the rename box has the focus from then.
- opened: std::time::Instant,
pub(super) last_sound: u64,
/// Villager trading state.
pub(super) merchant: Option>,
@@ -169,12 +169,12 @@ impl Station {
bookcases: 0,
anvil: AnvilResult::default(),
name: String::new(),
+ rename: anvil_box(),
stone: Default::default(),
selected: None,
start: 0,
anvil_last: None,
anvil_sent: None,
- opened: std::time::Instant::now(),
last_sound: 0,
merchant: None,
beacon: [None; 2],
@@ -868,6 +868,9 @@ impl Game<'_> {
if kind == Kind::Stonecutter && self.stonecutter_click(local) {
return true;
}
+ if kind == Kind::Anvil && !right && self.anvil_name_click(ox, oy) {
+ return true;
+ }
}
let Some(slot) = self.slot_at(&batch, self.cursor.0, self.cursor.1) else {
return true;
@@ -894,6 +897,8 @@ impl Game<'_> {
.as_ref()
.map(|i| stations::hover_name(&|id| self.pack.names.display_item(id), i))
.unwrap_or_default();
+ let name = s.name.clone();
+ s.rename.set_value(&name);
}
self.station_changed();
}
@@ -960,21 +965,29 @@ impl Game<'_> {
let Some(s) = self.station.as_mut() else {
return;
};
- let cleared = self.text_selection.take(ANVIL_NAME, &mut s.name);
if backspace {
- if !cleared {
- s.name.pop();
- }
+ s.rename.delete_chars(-1);
} else if let Some(t) = text {
- for c in t.chars().filter(|c| !c.is_control()) {
- if s.name.chars().count() < ANVIL_NAME_CHARS {
- s.name.push(c);
- }
- }
+ s.rename.insert_text(t);
}
+ s.name.clone_from(&s.rename.value);
self.station_changed();
self.remote_anvil_rename();
}
+ /// `AnvilScreen.keyPressed`: the rename box handles the key.
+ pub(super) fn station_key(&mut self, key: winit::keyboard::KeyCode) -> bool {
+ let (ctrl, shift) = (self.has_control_down(), self.has_shift_down());
+ let Some(s) = self.station.as_mut() else {
+ return false;
+ };
+ if !s.rename.key(key, ctrl, shift) {
+ return false;
+ }
+ s.name.clone_from(&s.rename.value);
+ self.station_changed();
+ self.remote_anvil_rename();
+ true
+ }
/// Ctrl+A/C/V/X in the anvil's rename box (an `EditBox` of 50) or the
/// recipe book's search box.
pub(super) fn station_shortcut(&mut self, shortcut: horizon_client::clipboard::EditShortcut) {
@@ -985,18 +998,52 @@ impl Game<'_> {
let Some(s) = self.station.as_mut() else {
return;
};
- if Self::append_shortcut(
- &mut self.text_selection,
- &mut self.clipboard,
- ANVIL_NAME,
- &mut s.name,
- shortcut,
- ANVIL_NAME_CHARS,
- ) {
+ if s.rename.shortcut(shortcut, &mut self.clipboard) {
+ s.name.clone_from(&s.rename.value);
self.station_changed();
self.remote_anvil_rename();
}
}
+ /// A click in the anvil's name box (`EditBox.onClick`).
+ fn anvil_name_click(&mut self, ox: f32, oy: f32) -> bool {
+ let open = self
+ .station
+ .as_ref()
+ .is_some_and(|s| s.kind == Kind::Anvil && s.inputs[0].is_some());
+ if !open {
+ return false;
+ }
+ let (x, y, w, h) = (ox + 62.0, oy + 24.0, 103.0, 12.0);
+ let (mx, my) = self.cursor;
+ if mx < x || my < y || mx >= x + w || my >= y + h {
+ return false;
+ }
+ let shift = self.has_shift_down();
+ let Some(s) = self.station.as_mut() else {
+ return false;
+ };
+ s.rename.x = x;
+ s.rename.y = y;
+ s.rename.width = w;
+ s.rename.click(&self.gui.atlas, mx, shift, false);
+ true
+ }
+ /// AnvilScreen's name box, drawn after the station picture.
+ fn draw_anvil_name(&mut self, batch: &mut HudBatch, ox: f32, oy: f32) {
+ let Some(s) = self.station.as_mut() else {
+ return;
+ };
+ let edit = &mut s.rename;
+ edit.x = ox + 62.0;
+ edit.y = oy + 24.0;
+ edit.width = 103.0;
+ edit.height = 12.0;
+ edit.bordered = false;
+ edit.focused = true;
+ edit.text_color = u32::MAX;
+ edit.max_length = ANVIL_NAME_CHARS;
+ edit.draw(batch, &self.gui.atlas, None);
+ }
// ---------------------------------------------------------------- drawing
@@ -1094,6 +1141,10 @@ impl Game<'_> {
let (ox, oy) = self.inventory_origin(batch);
let local = (self.cursor.0 - ox, self.cursor.1 - oy);
let mut tooltip: Option> = None;
+ let mut anvil_name = false;
+ // Keep item tooltips as stacks until drawing: text alone loses the
+ // bundle contents component (including in the player's inventory).
+ let mut tooltip_stack: Option = None;
{
let atlas = &self.gui.atlas;
batch.fill(
@@ -1428,18 +1479,8 @@ impl Game<'_> {
);
}
if s.inputs[0].is_some() {
- // AnvilScreen's name box: white, 103 wide, no inverted highlight.
- horizon_client::ui::append_field(
- batch,
- atlas,
- &s.name,
- (ox + 62.0, oy + 24.0, 103.0),
- (u32::MAX, true),
- ANVIL_NAME_CHARS,
- self.text_selection.covers(ANVIL_NAME, &s.name),
- false,
- s.opened.elapsed().as_millis(),
- );
+ // Drawn once this borrow of the station ends.
+ anvil_name = true;
}
let cost = s.anvil.cost;
if cost > 0 {
@@ -1579,13 +1620,16 @@ impl Game<'_> {
);
self.draw_item(batch, &result, ox + x, oy + y);
if hover {
- tooltip = Some(self.stack_tooltip(&result));
+ tooltip_stack = Some(result);
}
}
}
}
}
}
+ if anvil_name {
+ self.draw_anvil_name(batch, ox, oy);
+ }
let drag = self.drag_view();
for i in self.station_slots().unwrap_or_default() {
let Some((sx, sy)) = self.slot_position_any(i) else {
@@ -1606,13 +1650,16 @@ impl Game<'_> {
);
if self.carried.is_none()
&& tooltip.is_none()
+ && tooltip_stack.is_none()
&& let Some(stack) = self.slot_mut(slot).clone()
{
- tooltip = Some(self.stack_tooltip(&stack));
+ tooltip_stack = Some(stack);
}
}
if self.carried.is_some() {
self.draw_carried(batch);
+ } else if let Some(stack) = tooltip_stack {
+ self.draw_stack_tooltip(batch, &stack, self.cursor.0, self.cursor.1);
} else if let Some(lines) = tooltip {
self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1);
}
@@ -1656,6 +1703,8 @@ impl Game<'_> {
};
let name = if let Some(n) = stack.custom_name() {
n.to_owned()
+ } else if let Some(n) = stack.item_name() {
+ n
} else if stack.data().is_some_and(|d| d.ominous_banner) {
self.translate("block.minecraft.ominous_banner", "Ominous Banner")
} else if stack.data().and_then(|d| d.potion.as_ref()).is_some()
@@ -1682,7 +1731,9 @@ impl Game<'_> {
// ItemContainerContents.addToTooltip.
let hover_name = |s: &ItemStack| {
s.custom_name()
- .map_or_else(|| self.pack.names.display_item(&s.item), str::to_owned)
+ .map(str::to_owned)
+ .or_else(|| s.item_name())
+ .unwrap_or_else(|| self.pack.names.display_item(&s.item))
};
if display.shows("minecraft:container") {
for text in
@@ -1907,32 +1958,65 @@ impl Game<'_> {
if lines.is_empty() {
return;
}
- let lines: Vec<_> = lines
+ let lines = Self::resolved(lines);
+ let atlas = &self.gui.atlas;
+ let (x, y, w, h) =
+ Self::tooltip_geometry(atlas, &lines, (x, y), (batch.width, batch.height), 0.0);
+ Self::tooltip_box(batch, atlas, x, y, w, h);
+ Self::tooltip_text(batch, atlas, &lines, x, y);
+ }
+ /// A tooltip's lines with their legacy codes resolved and their colours.
+ fn resolved>(
+ lines: &[(T, u32)],
+ ) -> Vec<(horizon_client::text::Text, u32)> {
+ lines
.iter()
.map(|(text, color)| {
let text: horizon_client::text::Text = text.clone().into();
(text.resolve_legacy(), *color)
})
- .collect();
- let atlas = &self.gui.atlas;
+ .collect()
+ }
+ /// The tooltip's text's top-left corner and the box's size: beside the
+ /// cursor, kept on screen, `extra` pixels taller than the text.
+ fn tooltip_geometry(
+ atlas: &horizon_client::hud::HudAtlas,
+ lines: &[(horizon_client::text::Text, u32)],
+ (x, y): (f32, f32),
+ (width, height): (f32, f32),
+ extra: f32,
+ ) -> (f32, f32, f32, f32) {
let w = lines
.iter()
.map(|(t, _)| atlas.font.width_text(t))
- .fold(0.0, f32::max);
- let h = if lines.len() == 1 {
+ .fold(0.0, f32::max)
+ .max(if extra > 0.0 { 96.0 } else { 0.0 });
+ let text_h = if lines.len() == 1 {
8.0
} else {
8.0 + 2.0 + (lines.len() - 1) as f32 * 10.0
};
+ let h = text_h + extra;
let mut x = x + 12.0;
let mut y = y - 12.0;
- if x + w > batch.width - 4.0 {
+ if x + w > width - 4.0 {
x = (x - 28.0 - w).max(4.0);
}
- if y + h + 3.0 > batch.height {
- y = batch.height - h - 3.0;
+ if y + h + 3.0 > height {
+ y = height - h - 3.0;
}
y = y.max(4.0);
+ (x, y, w, h)
+ }
+ /// The box and its frame sprites.
+ fn tooltip_box(
+ batch: &mut HudBatch,
+ atlas: &horizon_client::hud::HudAtlas,
+ x: f32,
+ y: f32,
+ w: f32,
+ h: f32,
+ ) {
batch.fill(atlas, x - 3.0, y - 4.0, w + 6.0, 1.0, rgba(16, 0, 16, 240));
batch.fill(
atlas,
@@ -1968,12 +2052,137 @@ impl Game<'_> {
h + 4.0,
rgba(60, 0, 190, 80),
);
+ }
+ fn tooltip_text(
+ batch: &mut HudBatch,
+ atlas: &horizon_client::hud::HudAtlas,
+ lines: &[(horizon_client::text::Text, u32)],
+ x: f32,
+ y: f32,
+ ) {
let mut ty = y;
for (i, (text, color)) in lines.iter().enumerate() {
batch.text_styled(atlas, text, x, ty, *color, true);
ty += if i == 0 { 12.0 } else { 10.0 };
}
}
+ /// A stack's tooltip: its lines, and under them a bundle's contents
+ /// (`ClientBundleTooltip`: the 4 wide grid, the `+N` of what it leaves
+ /// out, and the weight bar).
+ pub(super) fn draw_stack_tooltip(
+ &mut self,
+ batch: &mut HudBatch,
+ stack: &ItemStack,
+ x: f32,
+ y: f32,
+ ) {
+ let lines = Self::resolved(&self.stack_tooltip(stack));
+ if lines.is_empty() {
+ return;
+ }
+ // The component's height does not depend on where it is drawn, so a
+ // probe at the origin gives the box its extra height.
+ let probe = horizon_client::bundle::tooltip_layout(self.res.items(), stack, 0.0, 0.0, 96.0);
+ let extra = probe.as_ref().map_or(0.0, |l| l.height + 2.0);
+ let atlas = &self.gui.atlas;
+ let (x, y, w, h) =
+ Self::tooltip_geometry(atlas, &lines, (x, y), (batch.width, batch.height), extra);
+ Self::tooltip_box(batch, atlas, x, y, w, h);
+ let text_h = h - extra;
+ Self::tooltip_text(batch, atlas, &lines, x, y);
+ let Some(layout) =
+ horizon_client::bundle::tooltip_layout(self.res.items(), stack, x, y + text_h + 2.0, w)
+ else {
+ return;
+ };
+ self.draw_bundle_tooltip(batch, &layout);
+ }
+ /// `ClientBundleTooltip.extractImage`: the slots, the count past them and
+ /// the progress bar.
+ fn draw_bundle_tooltip(
+ &mut self,
+ batch: &mut HudBatch,
+ layout: &horizon_client::bundle::TooltipLayout,
+ ) {
+ for (at, stack) in &layout.slots {
+ // Each draw takes the atlas on its own: the item's needs `&mut self`.
+ horizon_client::ui::nine_slice(
+ batch,
+ &self.gui.atlas,
+ "gui/sprites/container/bundle/slot_background",
+ at[0],
+ at[1],
+ 24.0,
+ 24.0,
+ u32::MAX,
+ );
+ let stack = stack.clone();
+ let [x, y] = *at;
+ self.draw_item(batch, &stack, x + 4.0, y + 4.0);
+ }
+ let atlas = &self.gui.atlas;
+ if let Some((at, hidden)) = layout.overflow {
+ let label = horizon_client::text::Text::literal(format!("+{hidden}"));
+ let w = atlas.font.width_text(&label);
+ batch.text_styled(
+ atlas,
+ &label,
+ at[0] + 12.0 - (w / 2.0).floor(),
+ at[1] + 10.0,
+ u32::MAX,
+ true,
+ );
+ }
+ if let Some((at, description)) = layout
+ .empty_description
+ .zip(Some("item.minecraft.bundle.empty.description"))
+ {
+ let text = horizon_client::text::Text::translate(description, &[]);
+ let mut ty = at[1];
+ for line in atlas.font.split_lines(&text, layout.width) {
+ batch.text_styled(atlas, &line.0, at[0], ty, 0xFFAA_AAAA, false);
+ ty += 9.0;
+ }
+ }
+ let [bx, by] = layout.bar;
+ let fill_sprite = if layout.full {
+ "gui/sprites/container/bundle/bundle_progressbar_full"
+ } else {
+ "gui/sprites/container/bundle/bundle_progressbar_fill"
+ };
+ horizon_client::ui::nine_slice(
+ batch,
+ atlas,
+ fill_sprite,
+ bx + 1.0,
+ by,
+ layout.fill,
+ 13.0,
+ u32::MAX,
+ );
+ horizon_client::ui::nine_slice(
+ batch,
+ atlas,
+ "gui/sprites/container/bundle/bundle_progressbar_border",
+ bx,
+ by,
+ 96.0,
+ 13.0,
+ u32::MAX,
+ );
+ if let Some(key) = layout.label {
+ let label = horizon_client::text::Text::translate(key, &[]);
+ let w = atlas.font.width_text(&label);
+ batch.text_styled(
+ atlas,
+ &label,
+ bx + 48.0 - (w / 2.0).floor(),
+ by + 3.0,
+ u32::MAX,
+ true,
+ );
+ }
+ }
/// Hovered-slot tooltip on the plain inventory, crafting and container screens.
pub(super) fn draw_slot_tooltip(&mut self, batch: &mut HudBatch) {
if self.carried.is_some() {
@@ -1984,8 +2193,7 @@ impl Game<'_> {
};
let stack = self.slot_view(slot);
if let Some(stack) = stack {
- let lines = self.stack_tooltip(&stack);
- self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1);
+ self.draw_stack_tooltip(batch, &stack, self.cursor.0, self.cursor.1);
} else if let Some(text) = self.crafter_tooltip(slot) {
self.draw_tooltip_lines(
batch,
@@ -2041,5 +2249,13 @@ impl Game<'_> {
}
/// The anvil's rename box (`AnvilScreen`: `setMaxLength(50)`).
-const ANVIL_NAME: horizon_client::clipboard::FieldId = ("anvil.name", 0);
const ANVIL_NAME_CHARS: usize = 50;
+
+fn anvil_box() -> horizon_client::ui::EditBox {
+ let mut edit = horizon_client::ui::EditBox::new(0.0, 0.0, 103.0, 12.0);
+ edit.bordered = false;
+ edit.max_length = ANVIL_NAME_CHARS;
+ edit.text_color = u32::MAX;
+ edit.focused = true;
+ edit
+}
diff --git a/crates/horizon-client/src/game/text_displays.rs b/crates/horizon-client/src/game/text_displays.rs
index f6c946bcc..45bd64fa7 100644
--- a/crates/horizon-client/src/game/text_displays.rs
+++ b/crates/horizon-client/src/game/text_displays.rs
@@ -210,6 +210,7 @@ impl Game<'_> {
if bg != 0 {
batch.fill(
atlas,
+ // submitTextBackground(-1, -1, width, height): corners, not a size.
-1.0,
-1.0,
width + 1.0,
diff --git a/crates/horizon-client/src/game/trading.rs b/crates/horizon-client/src/game/trading.rs
index 87e0c18c6..6986d8baa 100644
--- a/crates/horizon-client/src/game/trading.rs
+++ b/crates/horizon-client/src/game/trading.rs
@@ -803,8 +803,7 @@ impl Game<'_> {
text.split('\n').map(|l| (l.to_owned(), u32::MAX)).collect();
self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1);
} else if let Some(stack) = tooltip {
- let lines = self.stack_tooltip(&stack);
- self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1);
+ self.draw_stack_tooltip(batch, &stack, self.cursor.0, self.cursor.1);
}
true
}
diff --git a/crates/horizon-client/src/game/world_loading.rs b/crates/horizon-client/src/game/world_loading.rs
index 0cea31fd0..ef47cc9c0 100644
--- a/crates/horizon-client/src/game/world_loading.rs
+++ b/crates/horizon-client/src/game/world_loading.rs
@@ -261,6 +261,7 @@ pub(super) fn make_streaming(
res.threads(),
)
};
+ streaming.forced = save.load_forced_chunks();
streaming.radius = settings.render_distance;
streaming.biome_blend = settings.biome_blend;
streaming.smooth_lighting = settings.smooth_lighting;
diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs
index 5ff14003c..70d882a98 100644
--- a/crates/horizon-client/src/gfx.rs
+++ b/crates/horizon-client/src/gfx.rs
@@ -32,6 +32,8 @@ fn entity_shader() -> String {
crate::overlay::wgsl_constants() + include_str!("entity.wgsl")
}
+#[cfg(test)]
+mod crumbling_tests;
mod entity_batch;
mod weather;
use entity_batch::EntityBatch;
@@ -65,8 +67,10 @@ mod arena;
mod cull;
mod end_sky;
mod flat;
+mod hdr;
pub mod lightning;
mod lod;
+mod native_material;
mod native_pack;
mod native_post;
mod native_sky;
@@ -332,6 +336,10 @@ pub struct Renderer {
adapter: wgpu::Adapter,
/// The window's swapchain; `None` when rendering offscreen (`--offscreen`).
surface: Option>,
+ /// The surface `release_surface` set aside for the renderer that replaces
+ /// this one on the same device: reused, not created again, as a new
+ /// surface for the window can come back lost (Wayland on Mesa).
+ spare_surface: std::sync::Mutex>>,
/// The offscreen colour target, sized like `config`.
offscreen: Option,
/// Offscreen submissions still on the GPU: at most two frames run ahead.
@@ -429,6 +437,8 @@ pub struct Renderer {
/// The mipmap levels sampled (the Mipmap Levels option), at most `mip_levels - 1`.
mipmaps: u32,
terrain_layout: wgpu::BindGroupLayout,
+ /// terrain.wgsl's `SPRITE_ARRAYS` and `SPRITE_SPLIT`, for a shader pack's terrain.
+ terrain_sprites: [f64; 2],
sprite_sampler: wgpu::Sampler,
lightmap_view: wgpu::TextureView,
lightmap_sampler: wgpu::Sampler,
@@ -460,7 +470,7 @@ pub struct Renderer {
lod_retiring: crate::fast_hash::FxHashSet,
/// Finished tile meshes `upload_lods` has yet to upload.
lod_pending: Option,
- detail_columns: HashMap<(i32, i32), u32>,
+ detail_columns: crate::fast_hash::FxHashMap<(i32, i32), u32>,
/// Bumped whenever a column becomes or stops being complete.
detail_revision: u64,
arena: arena::Arena,
@@ -494,13 +504,17 @@ pub struct Renderer {
/// The shadow camera's draws by page, in bands of distance from the light.
shadow_runs: page_runs::PageRuns,
/// Block-entity blocks per meshed section (world position, state id).
- block_entities: HashMap>,
+ block_entities: crate::fast_hash::FxHashMap>,
/// Changes whenever `block_entities` does (unique across renderers).
block_entity_revision: u64,
/// VisGraph face connectivity per meshed section (cave culling).
occlusion: occlusion::Occlusion,
/// Resolution scale and FXAA (Horizon settings).
world_post: world_post::Post,
+ /// HDR output (Horizon setting): `Some` while the swapchain is scRGB.
+ hdr: Option,
+ /// Whether the window's surface can present scRGB.
+ hdr_capable: bool,
/// The shader pack's post-processing, and the dimension (legacy id) it was loaded for.
pack_post: Option<(i32, Option)>,
/// The post passes of one of Horizon's own shader packs; never beside `pack_post`'s.
@@ -907,15 +921,24 @@ async fn open_device(
limits.max_storage_buffer_binding_size,
limits.max_texture_array_layers,
));
- match adapter
- .request_device(&wgpu::DeviceDescriptor {
- label: Some("Horizon device"),
- required_features: features,
- required_limits: limits,
- ..Default::default()
- })
- .await
- {
+ let desc = wgpu::DeviceDescriptor {
+ label: Some("Horizon device"),
+ required_features: features,
+ required_limits: limits,
+ ..Default::default()
+ };
+ #[cfg(not(target_os = "macos"))]
+ let opened = match info.backend {
+ wgpu::Backend::Vulkan => horizon_hdr::open_vulkan_device(&adapter, &desc),
+ _ => None,
+ };
+ #[cfg(target_os = "macos")]
+ let opened = None;
+ let opened = match opened {
+ Some(opened) => opened,
+ None => adapter.request_device(&desc).await,
+ };
+ match opened {
Ok((device, queue)) => {
return Ok(Opened {
instance,
@@ -1027,7 +1050,15 @@ impl Renderer {
surface: if offscreen {
None
} else {
- Some(renderer.instance.create_surface(window.clone())?)
+ let spare = renderer
+ .spare_surface
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .take();
+ Some(match spare {
+ Some(surface) => surface,
+ None => renderer.instance.create_surface(window.clone())?,
+ })
},
instance: renderer.instance.clone(),
adapter: renderer.adapter.clone(),
@@ -1040,7 +1071,7 @@ impl Renderer {
let info = adapter.get_info();
crate::graphics::suggest_reduced_far_detail(crate::graphics::lod_capped(&info));
let size = window.inner_size();
- let (config, present_modes) = if let Some(surface) = &surface {
+ let (config, present_modes, hdr_capable) = if let Some(surface) = &surface {
let mut config = surface
.get_default_config(&adapter, size.width.max(1), size.height.max(1))
.ok_or("surface has no supported configuration")?;
@@ -1068,7 +1099,7 @@ impl Renderer {
));
}
surface.configure(&device, &config);
- (config, caps.present_modes.clone())
+ (config, caps.present_modes.clone(), hdr::supported(&caps))
} else {
let format = wgpu::TextureFormat::Bgra8Unorm;
let config = wgpu::SurfaceConfiguration {
@@ -1082,7 +1113,7 @@ impl Renderer {
view_formats: vec![format],
color_space: wgpu::SurfaceColorSpace::default(),
};
- (config, Vec::new())
+ (config, Vec::new(), false)
};
let offscreen = surface
.is_none()
@@ -1926,14 +1957,20 @@ impl Renderer {
compilation_options: Default::default(),
targets: &[Some(wgpu::ColorTargetState {
format: config.format.remove_srgb_suffix(),
- // RenderPipelines.CELESTIAL: additive (src alpha, one).
+ // RenderPipelines.CELESTIAL: additive (src alpha, one). The
+ // alpha goes to 0: HDR output (hdr.wgsl) lets the sun and
+ // moon shine; nothing else reads the frame's alpha.
blend: Some(wgpu::BlendState {
color: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::One,
operation: wgpu::BlendOperation::Add,
},
- alpha: wgpu::BlendComponent::OVER,
+ alpha: wgpu::BlendComponent {
+ src_factor: wgpu::BlendFactor::Zero,
+ dst_factor: wgpu::BlendFactor::Zero,
+ operation: wgpu::BlendOperation::Add,
+ },
}),
write_mask: wgpu::ColorWrites::ALL,
})],
@@ -2297,6 +2334,7 @@ impl Renderer {
instance,
adapter,
surface,
+ spare_surface: std::sync::Mutex::new(None),
offscreen,
in_flight: std::collections::VecDeque::new(),
first_frame: if previous.is_none() {
@@ -2370,6 +2408,7 @@ impl Renderer {
mip_levels,
mipmaps: mip_levels - 1,
terrain_layout: bindings,
+ terrain_sprites: sprite_constants.map(|(_, v)| v),
sprite_sampler,
lightmap_view,
lightmap_sampler,
@@ -2391,7 +2430,7 @@ impl Renderer {
lod_cutout: Default::default(),
lod_retiring: Default::default(),
lod_pending: None,
- detail_columns: HashMap::new(),
+ detail_columns: Default::default(),
detail_revision: 0,
arena,
sorted_indices,
@@ -2412,10 +2451,12 @@ impl Renderer {
solid_runs: page_runs::PageRuns::default(),
phase2_runs: page_runs::PageRuns::default(),
shadow_runs: page_runs::PageRuns::default(),
- block_entities: HashMap::new(),
+ block_entities: Default::default(),
block_entity_revision: next_block_entity_revision(),
occlusion: occlusion::Occlusion::new(crate::chunk::MIN_Y, crate::chunk::HEIGHT / 16),
world_post,
+ hdr: None,
+ hdr_capable,
pack_post: None,
native: None,
pass_timing: pack_timing::PassTiming::logging(),
@@ -2473,19 +2514,38 @@ impl Renderer {
view_formats: &config.view_formats,
})
}
- /// Release the window surface before another renderer uses the same window.
+ /// Hand the window surface over to the renderer that replaces this one on
+ /// the same device (it takes `spare_surface`).
pub fn release_surface(&mut self) {
- self.surface = None;
+ *self
+ .spare_surface
+ .get_mut()
+ .unwrap_or_else(std::sync::PoisonError::into_inner) = self.surface.take();
}
/// Reattach a window after surface loss or a failed renderer replacement.
pub fn recreate_surface(&mut self) -> Result<()> {
if self.offscreen.is_some() {
return Ok(());
}
- // Wayland allows only one tearing-control object per window surface.
- self.release_surface();
- let surface = self.instance.create_surface(self.window.clone())?;
- surface.configure(&self.device, &self.config);
+ // The surface set aside for a replacement that failed, else a new one
+ // (Wayland allows only one tearing-control object per window surface,
+ // so the old one goes first).
+ let spare = self
+ .spare_surface
+ .get_mut()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .take();
+ self.surface = None;
+ let surface = match spare {
+ Some(surface) => surface,
+ None => self.instance.create_surface(self.window.clone())?,
+ };
+ Self::configure(
+ &self.device,
+ &surface,
+ &self.surface_config(),
+ self.hdr.as_ref(),
+ );
self.surface = Some(surface);
Ok(())
}
@@ -2548,7 +2608,12 @@ impl Renderer {
&& let Some(surface) = &self.surface
{
self.config.present_mode = mode;
- surface.configure(&self.device, &self.config);
+ Self::configure(
+ &self.device,
+ surface,
+ &self.surface_config(),
+ self.hdr.as_ref(),
+ );
}
}
pub fn resize(&mut self, width: u32, height: u32) {
@@ -2559,7 +2624,12 @@ impl Renderer {
self.config.width = width;
self.config.height = height;
if let Some(surface) = &self.surface {
- surface.configure(&self.device, &self.config);
+ Self::configure(
+ &self.device,
+ surface,
+ &self.surface_config(),
+ self.hdr.as_ref(),
+ );
} else {
self.offscreen = Some(Self::offscreen_target(&self.device, &self.config));
}
@@ -2848,20 +2918,106 @@ impl Renderer {
self.detail_columns.insert(column, next);
}
}
- pub fn remove(&mut self, key: SectionKey) {
- self.set_detail(key, false);
- if self.block_entities.remove(&key).is_some() {
+ /// Every section of these columns goes: one lookup per column where the
+ /// tables are by column, and the meshes' ranges freed in one batch. A
+ /// teleport removes a few thousand columns in one frame.
+ pub fn remove_columns(&mut self, columns: impl IntoIterator- ) {
+ let mut released = Vec::new();
+ let mut block_entities = false;
+ for (x, z) in columns {
+ if self.detail_columns.remove(&(x, z)) == Some(COMPLETE_COLUMN) {
+ self.detail_revision += 1;
+ }
+ self.occlusion.remove_column(x, z);
+ let retiring = !self.lod_cutout.is_empty() && self.lod_cutout.contains(&(x, z));
+ for sy in 0..crate::chunk::SECTIONS as i32 {
+ let key = (x, sy, z);
+ block_entities |= self.block_entities.remove(&key).is_some();
+ if retiring {
+ // Distant tiles still leave its column out: drawn until they don't.
+ self.lod_retiring.insert(key);
+ } else if let Some(section) = self.sections.remove(key) {
+ released.push(section);
+ }
+ }
+ }
+ if block_entities {
self.block_entity_revision = next_block_entity_revision();
}
- self.occlusion.remove(key);
- if !self.lod_cutout.is_empty() && self.lod_cutout.contains(&(key.0, key.2)) {
- // Distant tiles still leave its column out: drawn until they don't.
- self.lod_retiring.insert(key);
- } else {
- self.release_section(key);
+ if let Some(flat) = &mut self.flat {
+ for id in released.iter().filter_map(|s| s.flat_id) {
+ flat.remove(id);
+ }
}
+ self.sorted_indices
+ .release_all(released.iter().filter_map(|s| {
+ let (page, start) = s.sorted_indices?;
+ Some((page, start, u64::from(s.translucent) * 6))
+ }));
+ self.arena.release_all(
+ released
+ .iter()
+ .map(|s| (s.page, s.start, u64::from(s.solid + s.translucent) * 4)),
+ );
}
/// Horizon's resolution scale (world pixels per window pixel) and FXAA.
+ /// Configures `surface` and, while HDR output is on, sends its HDR metadata
+ /// again: a new swapchain starts without it.
+ fn configure(
+ device: &wgpu::Device,
+ surface: &wgpu::Surface<'_>,
+ config: &wgpu::SurfaceConfiguration,
+ hdr: Option<&hdr::Hdr>,
+ ) {
+ surface.configure(device, config);
+ if let Some(hdr) = hdr {
+ horizon_hdr::set_metadata(device, surface, hdr.paper_white, hdr.peak);
+ }
+ }
+ fn configure_surface(&self) {
+ if let Some(surface) = &self.surface {
+ Self::configure(
+ &self.device,
+ surface,
+ &self.surface_config(),
+ self.hdr.as_ref(),
+ );
+ }
+ }
+ fn send_hdr_metadata(&self) {
+ if let (Some(surface), Some(hdr)) = (&self.surface, &self.hdr) {
+ horizon_hdr::set_metadata(&self.device, surface, hdr.paper_white, hdr.peak);
+ }
+ }
+ /// The swapchain's configuration: `config` (what every pipeline draws in), or
+ /// scRGB while HDR output is on.
+ fn surface_config(&self) -> wgpu::SurfaceConfiguration {
+ let mut config = self.config.clone();
+ if self.hdr.is_some() {
+ config.format = hdr::FORMAT;
+ config.view_formats = vec![hdr::FORMAT];
+ config.color_space = hdr::COLOR_SPACE;
+ config.usage = wgpu::TextureUsages::RENDER_ATTACHMENT;
+ }
+ config
+ }
+ /// HDR output (Horizon setting): scRGB at `paper_white` nits, the world's
+ /// highlights expanded up to `peak` nits. Off where the surface can't present it.
+ pub fn set_hdr(&mut self, on: bool, paper_white: f32, peak: f32) {
+ let on = on && self.hdr_capable && self.surface.is_some();
+ if on != self.hdr.is_some() {
+ self.hdr =
+ on.then(|| hdr::Hdr::new(&self.device, self.config.format.remove_srgb_suffix()));
+ self.configure_surface();
+ }
+ if let Some(hdr) = &mut self.hdr
+ && (hdr.paper_white, hdr.peak) != (paper_white, peak)
+ {
+ hdr.paper_white = paper_white;
+ hdr.peak = peak;
+ self.send_hdr_metadata();
+ }
+ }
pub fn set_world_post(&mut self, scale: f32, antialias: bool) {
self.world_post.scale = scale.clamp(0.25, 2.0);
self.world_post.antialias = antialias;
@@ -2937,9 +3093,9 @@ impl Renderer {
self.native = None;
self.pack_atlas = None;
}
- /// Runs the sky and post stages of one of Horizon's own shader packs from now on (`None`:
- /// no pack), with its options' values; replaces a `shaders/` pack. Returns the stages'
- /// labels; on an error the pack that ran keeps running.
+ /// Runs the sky, material and post stages of one of Horizon's own shader packs from now
+ /// on (`None`: no pack), with its options' values; replaces a `shaders/` pack. Returns the
+ /// stages' labels; on an error the pack that ran keeps running.
pub fn set_native_shader_pack(
&mut self,
pack: Option<(horizon_shader_api::Report, Vec<(String, f64)>)>,
@@ -2948,14 +3104,14 @@ impl Renderer {
self.native = None;
return Ok(Vec::new());
};
- let format = self.config.format.remove_srgb_suffix();
- let (pack, labels) = native_pack::NativePack::new(
- &self.device,
- &self.terrain_layout,
- report,
- &constants,
- format,
- )?;
+ let terrain = native_material::TerrainShape {
+ globals: &self.terrain_layout,
+ flat: self.flat.as_ref().map(|f| &f.draw_layout),
+ sprites: self.terrain_sprites,
+ format: self.config.format.remove_srgb_suffix(),
+ };
+ let (pack, labels) =
+ native_pack::NativePack::new(&self.device, &terrain, report, &constants)?;
self.native = pack.draws().then_some(pack);
if self.native.is_some() {
self.pack_post = None;
@@ -3197,7 +3353,12 @@ impl Renderer {
}
Some(wgpu::CurrentSurfaceTexture::Outdated) => {
if let Some(surface) = &self.surface {
- surface.configure(&self.device, &self.config);
+ Self::configure(
+ &self.device,
+ surface,
+ &self.surface_config(),
+ self.hdr.as_ref(),
+ );
}
return Ok(false);
}
@@ -3209,9 +3370,11 @@ impl Renderer {
return Err("surface acquisition failed validation".into());
}
};
- let target_texture = match &surface_frame {
- Some(f) => f.texture.clone(),
- None => self.offscreen.clone().ok_or("no offscreen target")?,
+ let size = [self.config.width, self.config.height];
+ let target_texture = match (&surface_frame, &mut self.hdr) {
+ (Some(_), Some(hdr)) => hdr.frame(&self.device, size),
+ (Some(f), None) => f.texture.clone(),
+ (None, _) => self.offscreen.clone().ok_or("no offscreen target")?,
};
// One command buffer for the frame, after the section copies staged
// since the last one: Metal commits every command buffer wgpu-core
@@ -4179,6 +4342,21 @@ impl Renderer {
pass.set_bind_group(0, &self.group, &[]);
sky.draw(&mut pass, &native.frame);
}
+ // One of Horizon's own packs with a material stage: terrain draws with its pipelines,
+ // which read the pack's frame at group 3.
+ let material = self
+ .native
+ .as_ref()
+ .filter(|_| !frame.title)
+ .and_then(|n| Some((n.material.as_ref()?, &n.frame.group)));
+ let solid_pipeline = material.map_or(&self.solid, |(m, _)| &m.solid);
+ let translucent_pipeline = material.map_or(&self.translucent, |(m, _)| &m.translucent);
+ let pack_flat = material.and_then(|(m, _)| m.flat.as_ref());
+ let pack_frame = |pass: &mut wgpu::RenderPass<'_>| {
+ if let Some((_, group)) = material {
+ pass.set_bind_group(horizon_shader_api::stages::GROUP, group, &[]);
+ }
+ };
{
let attachments = [Some(wgpu::RenderPassColorAttachment {
view: world_view,
@@ -4212,6 +4390,7 @@ impl Renderer {
..Default::default()
});
pass.set_bind_group(0, &self.group, &[]);
+ pack_frame(&mut pass);
if frame.title || (sky_visible && (frame.skybox || frame.end_sky)) {
if !frame.title && frame.skybox && !pack_draws_sky && !native_sky_pass {
match self
@@ -4260,7 +4439,7 @@ impl Renderer {
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
}
Some(flat) => {
- pass.set_pipeline(&flat.solid);
+ pass.set_pipeline(pack_flat.map_or(&flat.solid, |p| &p.0));
let vertices = |p| self.arena.buffer(p);
match cull {
// As below: phase 1, the test, then phase 2's draws
@@ -4278,7 +4457,8 @@ impl Renderer {
pack_reads_world,
);
pass.set_bind_group(0, &self.group, &[]);
- pass.set_pipeline(&flat.solid);
+ pack_frame(&mut pass);
+ pass.set_pipeline(pack_flat.map_or(&flat.solid, |p| &p.0));
flat.draw_phase2(&mut pass, vertices);
}
None => flat.draw(&mut pass, &flat.calls[..flat.solid_calls], vertices),
@@ -4287,7 +4467,7 @@ impl Renderer {
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
}
None => {
- pass.set_pipeline(&self.solid);
+ pass.set_pipeline(solid_pipeline);
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
pass.set_vertex_buffer(1, self.instances.slice(..));
match cull {
@@ -4307,7 +4487,8 @@ impl Renderer {
pack_reads_world,
);
pass.set_bind_group(0, &self.group, &[]);
- pass.set_pipeline(&self.solid);
+ pack_frame(&mut pass);
+ pass.set_pipeline(solid_pipeline);
pass.set_index_buffer(
self.indices.slice(..),
wgpu::IndexFormat::Uint32,
@@ -4399,7 +4580,7 @@ impl Renderer {
// The pack draws translucent terrain after its deferred programs.
_ if pack_draws_water => {}
Some(flat) => {
- pass.set_pipeline(&flat.translucent);
+ pass.set_pipeline(pack_flat.map_or(&flat.translucent, |p| &p.1));
flat.draw(&mut pass, flat.translucent_calls(), |p| {
self.arena.buffer(p)
});
@@ -4407,7 +4588,7 @@ impl Renderer {
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
}
None => {
- pass.set_pipeline(&self.translucent);
+ pass.set_pipeline(translucent_pipeline);
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
pass.set_vertex_buffer(1, self.instances.slice(..));
self.draw_sections(
@@ -4591,6 +4772,9 @@ impl Renderer {
} else if scaled.is_some() {
self.world_post.finish(&mut encoder, &view_target);
}
+ if let Some(hdr) = &self.hdr {
+ hdr.keep_world(&mut encoder);
+ }
if hand_quads > 0 && !pack_draws_hand {
let attachments = [Some(wgpu::RenderPassColorAttachment {
view: &view_target,
@@ -4745,6 +4929,9 @@ impl Renderer {
self.section_copy_bytes = 0;
self.section_uploads.finish(&encoder);
}
+ if let (Some(hdr), Some(f)) = (&self.hdr, &surface_frame) {
+ hdr.present(&self.queue, &mut encoder, &f.texture, !frame.title);
+ }
let commands = crate::spans::time("encode", || encoder.finish());
let first_frame = self.first_frame == FirstFrame::Submit;
if first_frame {
@@ -4781,7 +4968,12 @@ impl Renderer {
}
self.queue.present(surface_frame);
if reconfigure && let Some(surface) = &self.surface {
- surface.configure(&self.device, &self.config);
+ Self::configure(
+ &self.device,
+ surface,
+ &self.surface_config(),
+ self.hdr.as_ref(),
+ );
}
} else {
// No swapchain paces offscreen frames: keep two on the GPU at most.
diff --git a/crates/horizon-client/src/gfx/arena.rs b/crates/horizon-client/src/gfx/arena.rs
index 34b4d332d..8f320a408 100644
--- a/crates/horizon-client/src/gfx/arena.rs
+++ b/crates/horizon-client/src/gfx/arena.rs
@@ -39,6 +39,35 @@ impl FreeList {
}
self.0.insert(start, len);
}
+ /// `give` for many ranges: one merge of the sorted ranges into the list,
+ /// where giving them one by one costs a few tree lookups each.
+ fn give_all(&mut self, mut ranges: Vec<(u64, u64)>) {
+ if ranges.len() < 64 {
+ for (start, len) in ranges {
+ self.give(start, len);
+ }
+ return;
+ }
+ ranges.sort_unstable();
+ let mut free = std::mem::take(&mut self.0).into_iter().peekable();
+ let mut given = ranges.into_iter().peekable();
+ let mut merged: Vec<(u64, u64)> = Vec::new();
+ loop {
+ let next = match (free.peek(), given.peek()) {
+ (Some(a), Some(b)) if a.0 <= b.0 => free.next(),
+ (_, Some(_)) => given.next(),
+ (Some(_), None) => free.next(),
+ (None, None) => break,
+ };
+ let (start, len) = next.expect("peeked");
+ match merged.last_mut() {
+ Some(last) if last.0 + last.1 == start => last.1 += len,
+ _ => merged.push((start, len)),
+ }
+ }
+ // Sorted: the tree is built in one pass.
+ self.0 = merged.into_iter().collect();
+ }
}
/// One vertex buffer of the arena; only the newest page grows.
@@ -237,6 +266,26 @@ impl Arena {
.free
.give(start, len);
}
+ /// `release` for many ranges (page, start, vertices) at once.
+ pub fn release_all(&mut self, ranges: impl IntoIterator
- ) {
+ let mut by_page: Vec
> = Vec::new();
+ for (page, start, vertices) in ranges {
+ let page = page as usize;
+ if by_page.len() <= page {
+ by_page.resize_with(page + 1, Vec::new);
+ }
+ by_page[page].push((start, Self::span(vertices).min(self.page_limit)));
+ }
+ for (page, ranges) in by_page.into_iter().enumerate() {
+ if !ranges.is_empty() {
+ self.pages[page]
+ .as_mut()
+ .expect("live arena page")
+ .free
+ .give_all(ranges);
+ }
+ }
+ }
/// Moves a page into a larger buffer; false (the page unchanged) when
/// there is no memory for it.
fn grow(
@@ -435,6 +484,44 @@ mod tests {
assert_eq!(free, FreeList::new(4 * GRAIN));
}
+ #[test]
+ fn many_ranges_given_at_once_coalesce_as_one_by_one() {
+ let fill = || {
+ let mut free = FreeList::new(1 << 30);
+ let taken: Vec<(u64, u64)> = (0..500u64)
+ .map(|i| {
+ let len = GRAIN * (1 + i * 7 % 5);
+ (free.take(len).unwrap(), len)
+ })
+ .collect();
+ for &(start, len) in taken.iter().step_by(3) {
+ free.give(start, len);
+ }
+ (free, taken)
+ };
+ // Every other range of those still taken, in a scattered order.
+ let (mut one_by_one, taken) = fill();
+ let (mut at_once, _) = fill();
+ let mut given: Vec<(u64, u64)> = (0..taken.len())
+ .filter(|i| i % 3 != 0 && i % 2 == 0)
+ .map(|i| taken[i])
+ .collect();
+ given.sort_by_key(|&(start, _)| start.wrapping_mul(0x9E37_79B9_7F4A_7C15));
+ assert!(given.len() >= 64);
+ for &(start, len) in &given {
+ one_by_one.give(start, len);
+ }
+ at_once.give_all(given);
+ assert_eq!(at_once, one_by_one);
+ // The rest too: everything is free again, in one range.
+ let rest: Vec<(u64, u64)> = (0..taken.len())
+ .filter(|i| i % 3 != 0 && i % 2 != 0)
+ .map(|i| taken[i])
+ .collect();
+ at_once.give_all(rest);
+ assert_eq!(at_once, FreeList::new(1 << 30));
+ }
+
#[test]
fn first_fit_reuses_the_lowest_hole() {
let mut free = FreeList::new(8 * GRAIN);
diff --git a/crates/horizon-client/src/gfx/crumbling_tests.rs b/crates/horizon-client/src/gfx/crumbling_tests.rs
new file mode 100644
index 000000000..620a57cf9
--- /dev/null
+++ b/crates/horizon-client/src/gfx/crumbling_tests.rs
@@ -0,0 +1,191 @@
+use wgpu::util::DeviceExt;
+
+/// Sample the production WGSL with the production nearest/no-mipmap atlas sampler.
+#[test]
+#[ignore = "requires a Vulkan/Metal/DX12 adapter"]
+fn crack_samples_stay_aligned_between_stages() {
+ let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle());
+ let adapter =
+ pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default()))
+ .unwrap();
+ eprintln!("crack sampling adapter: {:?}", adapter.get_info());
+ let (device, queue) =
+ pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())).unwrap();
+ #[repr(C)]
+ #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
+ struct Case {
+ uv: [f32; 2],
+ stage: u32,
+ extent: u32,
+ }
+ let mut cases = Vec::new();
+ let mut expected = Vec::<[u32; 2]>::new();
+ // Include exact texel boundaries, their neighbours, negative projected UVs,
+ // wrapping and the far sprite edge. Origins straddle the packing error's sign change.
+ for extent in [16, 32] {
+ for x in [32, 2080, 4064] {
+ for y in [0, 32] {
+ for i in -32..=32 {
+ for delta in [-0.0001, 0.0, 0.0001] {
+ let uv = [i as f32 / 16.0 + delta, 0.5 - delta];
+ cases.push(Case {
+ uv,
+ stage: crate::breaking::stage_light([
+ x as f32 / 4096.0,
+ y as f32 / 64.0,
+ ]),
+ extent,
+ });
+ expected.push(uv.map(|v| {
+ (((v - v.floor()) * extent as f32).floor() as u32).min(extent - 1)
+ }));
+ }
+ }
+ }
+ }
+ }
+ let math = include_str!("../entity.wgsl")
+ .split("// CRUMBLING_UV_BEGIN")
+ .nth(1)
+ .unwrap()
+ .split("// CRUMBLING_UV_END")
+ .next()
+ .unwrap();
+ let source = format!(
+ "{math}\n{}",
+ r#"
+struct Case { uv: vec2, stage: u32, extent: u32 }
+@group(0) @binding(0) var cases: array;
+@group(0) @binding(1) var results: array>;
+@group(0) @binding(2) var atlas: texture_2d;
+@group(0) @binding(3) var atlas_sampler: sampler;
+@compute @workgroup_size(64) fn main(@builtin(global_invocation_id) id: vec3) {
+ if id.x >= arrayLength(&cases) { return; }
+ let c = cases[id.x];
+ let origin = vec2(f32(c.stage & 65535u), f32(c.stage >> 16u)) / 65535.0;
+ let dimensions = textureDimensions(atlas);
+ let uv = crumbling_uv(c.uv, origin, vec2(f32(c.extent)) / vec2(dimensions), dimensions);
+ // Keep the old addressing as a control: the fixture must reproduce its stage jitter.
+ let old_uv = origin + fract(c.uv) * vec2(f32(c.extent)) / vec2(dimensions);
+ results[id.x] = vec4(
+ vec2(round(textureSampleLevel(atlas, atlas_sampler, uv, 0.0).rg * 255.0)),
+ vec2(round(textureSampleLevel(atlas, atlas_sampler, old_uv, 0.0).rg * 255.0)));
+}"#
+ );
+ let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
+ label: Some("Crack sampling regression"),
+ source: wgpu::ShaderSource::Wgsl(source.into()),
+ });
+ let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
+ label: None,
+ layout: None,
+ module: &shader,
+ entry_point: Some("main"),
+ compilation_options: Default::default(),
+ cache: None,
+ });
+ // Every sprite repeats the same coordinate pattern, so moving a stage's
+ // atlas origin must leave every sampled local texel unchanged.
+ let pixels: Vec = (0..64)
+ .flat_map(|y| (0..4096).flat_map(move |x| [(x % 32) as u8, (y % 32) as u8, 0, 255]))
+ .collect();
+ let atlas = device.create_texture_with_data(
+ &queue,
+ &wgpu::TextureDescriptor {
+ label: None,
+ size: wgpu::Extent3d {
+ width: 4096,
+ height: 64,
+ depth_or_array_layers: 1,
+ },
+ mip_level_count: 1,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format: wgpu::TextureFormat::Rgba8Unorm,
+ usage: wgpu::TextureUsages::TEXTURE_BINDING,
+ view_formats: &[],
+ },
+ wgpu::util::TextureDataOrder::LayerMajor,
+ &pixels,
+ );
+ let view = atlas.create_view(&Default::default());
+ let sampler = device.create_sampler(&wgpu::SamplerDescriptor::default());
+ let input = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
+ label: None,
+ contents: bytemuck::cast_slice(&cases),
+ usage: wgpu::BufferUsages::STORAGE,
+ });
+ let size = (expected.len() * 16) as u64;
+ let output = device.create_buffer(&wgpu::BufferDescriptor {
+ label: None,
+ size,
+ usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
+ mapped_at_creation: false,
+ });
+ let readback = device.create_buffer(&wgpu::BufferDescriptor {
+ label: None,
+ size,
+ usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ });
+ let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: None,
+ layout: &pipeline.get_bind_group_layout(0),
+ entries: &[
+ wgpu::BindGroupEntry {
+ binding: 0,
+ resource: input.as_entire_binding(),
+ },
+ wgpu::BindGroupEntry {
+ binding: 1,
+ resource: output.as_entire_binding(),
+ },
+ wgpu::BindGroupEntry {
+ binding: 2,
+ resource: wgpu::BindingResource::TextureView(&view),
+ },
+ wgpu::BindGroupEntry {
+ binding: 3,
+ resource: wgpu::BindingResource::Sampler(&sampler),
+ },
+ ],
+ });
+ let mut encoder = device.create_command_encoder(&Default::default());
+ {
+ let mut pass = encoder.begin_compute_pass(&Default::default());
+ pass.set_pipeline(&pipeline);
+ pass.set_bind_group(0, &group, &[]);
+ pass.dispatch_workgroups((cases.len() as u32).div_ceil(64), 1, 1);
+ }
+ encoder.copy_buffer_to_buffer(&output, 0, &readback, 0, size);
+ queue.submit([encoder.finish()]);
+ let (tx, rx) = std::sync::mpsc::channel();
+ readback.map_async(wgpu::MapMode::Read, .., move |r| tx.send(r).unwrap());
+ device.poll(wgpu::PollType::wait_indefinitely()).unwrap();
+ rx.recv().unwrap().unwrap();
+ let mapped = readback.get_mapped_range(..).unwrap();
+ let actual: &[[u32; 4]] = bytemuck::cast_slice(&mapped);
+ for (i, (actual, expected)) in actual.iter().zip(&expected).enumerate() {
+ assert_eq!(
+ &actual[..2],
+ expected,
+ "case {i}, uv={:?}, stage={}, extent={}",
+ cases[i].uv,
+ cases[i].stage,
+ cases[i].extent
+ );
+ }
+ let old_mismatches = actual
+ .iter()
+ .zip(&expected)
+ .filter(|(a, e)| &a[2..] != e.as_slice())
+ .count();
+ assert!(
+ old_mismatches > 0,
+ "fixture must detect the original sampling bug"
+ );
+ eprintln!(
+ "{} crack texture samples matched; old addressing mismatched {old_mismatches}",
+ cases.len()
+ );
+}
diff --git a/crates/horizon-client/src/gfx/hdr.rs b/crates/horizon-client/src/gfx/hdr.rs
new file mode 100644
index 000000000..11deed546
--- /dev/null
+++ b/crates/horizon-client/src/gfx/hdr.rs
@@ -0,0 +1,222 @@
+//! HDR output (Horizon setting, off by default): the frame renders exactly as
+//! without it, into an 8-bit target in vanilla's gamma space, and one pass
+//! (`hdr.wgsl`) writes it to an scRGB swapchain at the player's paper white,
+//! expanding the world's highlights towards their peak brightness. The world
+//! is copied aside before the hand and HUD so only it is expanded.
+
+/// The swapchain format and colour space HDR output presents in.
+pub const FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba16Float;
+pub const COLOR_SPACE: wgpu::SurfaceColorSpace = wgpu::SurfaceColorSpace::ExtendedSrgbLinear;
+
+/// Whether `caps` can present scRGB.
+pub fn supported(caps: &wgpu::SurfaceCapabilities) -> bool {
+ caps.color_spaces(FORMAT)
+ .contains(wgpu::SurfaceColorSpaces::EXTENDED_SRGB_LINEAR)
+}
+
+struct Targets {
+ size: [u32; 2],
+ frame: wgpu::Texture,
+ world: wgpu::Texture,
+ group: wgpu::BindGroup,
+}
+
+pub struct Hdr {
+ layout: wgpu::BindGroupLayout,
+ pipeline: wgpu::RenderPipeline,
+ params: wgpu::Buffer,
+ /// The frame's format: what every other pipeline draws in.
+ format: wgpu::TextureFormat,
+ targets: Option,
+ /// `HORIZON_HDR_DEBUG=1`: paint expanded pixels magenta.
+ debug: bool,
+ /// Paper white and peak brightness, in nits.
+ pub paper_white: f32,
+ pub peak: f32,
+}
+
+impl Hdr {
+ pub fn new(device: &wgpu::Device, format: wgpu::TextureFormat) -> Self {
+ let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
+ label: Some("Horizon HDR"),
+ source: wgpu::ShaderSource::Wgsl(include_str!("../hdr.wgsl").into()),
+ });
+ let texture = |binding| wgpu::BindGroupLayoutEntry {
+ binding,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ ty: wgpu::BindingType::Texture {
+ sample_type: wgpu::TextureSampleType::Float { filterable: false },
+ view_dimension: wgpu::TextureViewDimension::D2,
+ multisampled: false,
+ },
+ count: None,
+ };
+ let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("Horizon HDR bindings"),
+ entries: &[
+ texture(0),
+ texture(1),
+ wgpu::BindGroupLayoutEntry {
+ binding: 2,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ ty: wgpu::BindingType::Buffer {
+ ty: wgpu::BufferBindingType::Uniform,
+ has_dynamic_offset: false,
+ min_binding_size: None,
+ },
+ count: None,
+ },
+ ],
+ });
+ let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
+ label: Some("Horizon HDR"),
+ bind_group_layouts: &[Some(&layout)],
+ ..Default::default()
+ });
+ let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
+ label: Some("Horizon HDR"),
+ layout: Some(&pipeline_layout),
+ vertex: wgpu::VertexState {
+ module: &shader,
+ entry_point: Some("vs_hdr"),
+ compilation_options: Default::default(),
+ buffers: &[],
+ },
+ primitive: Default::default(),
+ depth_stencil: None,
+ multisample: Default::default(),
+ fragment: Some(wgpu::FragmentState {
+ module: &shader,
+ entry_point: Some("fs_hdr"),
+ compilation_options: Default::default(),
+ targets: &[Some(wgpu::ColorTargetState {
+ format: FORMAT,
+ blend: None,
+ write_mask: wgpu::ColorWrites::ALL,
+ })],
+ }),
+ multiview_mask: None,
+ cache: None,
+ });
+ let params = device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("Horizon HDR parameters"),
+ size: 16,
+ usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ });
+ Self {
+ layout,
+ pipeline,
+ params,
+ format,
+ targets: None,
+ debug: std::env::var_os("HORIZON_HDR_DEBUG").is_some_and(|v| v == "1"),
+ paper_white: 200.0,
+ peak: 1000.0,
+ }
+ }
+
+ /// The 8-bit frame every pass draws into this frame, sized like the window.
+ pub fn frame(&mut self, device: &wgpu::Device, size: [u32; 2]) -> wgpu::Texture {
+ if self.targets.as_ref().is_none_or(|t| t.size != size) {
+ let texture = |label, usage| {
+ device.create_texture(&wgpu::TextureDescriptor {
+ label: Some(label),
+ size: wgpu::Extent3d {
+ width: size[0].max(1),
+ height: size[1].max(1),
+ depth_or_array_layers: 1,
+ },
+ mip_level_count: 1,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format: self.format,
+ usage: usage | wgpu::TextureUsages::TEXTURE_BINDING,
+ view_formats: &[],
+ })
+ };
+ let frame = texture(
+ "Horizon HDR frame",
+ wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
+ );
+ let world = texture("Horizon HDR world", wgpu::TextureUsages::COPY_DST);
+ let view = |t: &wgpu::Texture| t.create_view(&Default::default());
+ let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("Horizon HDR bindings"),
+ layout: &self.layout,
+ entries: &[
+ wgpu::BindGroupEntry {
+ binding: 0,
+ resource: wgpu::BindingResource::TextureView(&view(&frame)),
+ },
+ wgpu::BindGroupEntry {
+ binding: 1,
+ resource: wgpu::BindingResource::TextureView(&view(&world)),
+ },
+ wgpu::BindGroupEntry {
+ binding: 2,
+ resource: self.params.as_entire_binding(),
+ },
+ ],
+ });
+ self.targets = Some(Targets {
+ size,
+ frame,
+ world,
+ group,
+ });
+ }
+ self.targets.as_ref().unwrap().frame.clone()
+ }
+
+ /// Keeps the world as drawn so far, before the hand and HUD cover it.
+ pub fn keep_world(&self, encoder: &mut wgpu::CommandEncoder) {
+ let Some(t) = &self.targets else {
+ return;
+ };
+ encoder.copy_texture_to_texture(
+ t.frame.as_image_copy(),
+ t.world.as_image_copy(),
+ t.frame.size(),
+ );
+ }
+
+ /// Writes the finished frame to the swapchain image `target`.
+ pub fn present(
+ &self,
+ queue: &wgpu::Queue,
+ encoder: &mut wgpu::CommandEncoder,
+ target: &wgpu::Texture,
+ world: bool,
+ ) {
+ let Some(t) = &self.targets else {
+ return;
+ };
+ let paper = self.paper_white.max(1.0);
+ let params = [
+ paper / 80.0,
+ (self.peak / paper).max(1.0),
+ f32::from(u8::from(world)),
+ f32::from(u8::from(self.debug)),
+ ];
+ queue.write_buffer(&self.params, 0, bytemuck::cast_slice(¶ms));
+ let view = target.create_view(&Default::default());
+ let attachments = [Some(wgpu::RenderPassColorAttachment {
+ view: &view,
+ resolve_target: None,
+ depth_slice: None,
+ ops: wgpu::Operations {
+ load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
+ store: wgpu::StoreOp::Store,
+ },
+ })];
+ let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
+ label: Some("Horizon HDR"),
+ color_attachments: &attachments,
+ ..Default::default()
+ });
+ pass.set_pipeline(&self.pipeline);
+ pass.set_bind_group(0, &t.group, &[]);
+ pass.draw(0..3, 0..1);
+ }
+}
diff --git a/crates/horizon-client/src/gfx/native_material.rs b/crates/horizon-client/src/gfx/native_material.rs
new file mode 100644
index 000000000..e7f8f7bf2
--- /dev/null
+++ b/crates/horizon-client/src/gfx/native_material.rs
@@ -0,0 +1,328 @@
+//! The material stage of Horizon's own shader packs (docs/shaders.md): the pack's `material`
+//! function gives the lit colour of each point of a block's surface, before the alpha test and
+//! fog. Terrain still draws with Horizon's meshes, culling and draw lists; only its pipelines
+//! change. Their shader is the pack's stage (`CheckedStage::source`), then a copy of
+//! terrain.wgsl's terrain section whose names all take a `horizon_terrain_` prefix
+//! (`wgsl_prefix.rs`), then entries that hand the pack what vanilla's `fs_solid` and
+//! `fs_translucent` combine: the texture, the vertex tint, the light levels and the lightmap's
+//! colour. They read the terrain's group 0, the flat draws' group 1 where those run, and the
+//! pack's frame at group 3.
+
+use horizon_shader_api::{check::CheckedStage, stages::GROUP};
+
+use super::native_post::{NativeFrame, present_constants};
+use super::wgsl_prefix::{TERRAIN, prefixed, section};
+
+const PREFIX: &str = "horizon_terrain_";
+
+/// The entries around the pack's `material`: vanilla's vertex work without the lightmap
+/// product, then the material, the alpha test and fog as `fs_solid` and `fs_translucent` do.
+const ENTRIES: &str = "
+struct HorizonMaterialOut {
+ @builtin(position) clip: vec4,
+ @location(0) uv: vec2,
+ @location(1) @interpolate(flat) layer: u32,
+ @location(2) tint: vec4,
+ @location(3) light: vec2,
+ @location(4) lightmap: vec3,
+ @location(5) position: vec3,
+ @location(6) spherical: f32,
+ @location(7) cylindrical: f32,
+}
+fn horizon_material_vertex(position: vec3, uv: vec2, layer: u32, color: vec4,
+ light: vec2, origin: vec3) -> HorizonMaterialOut {
+ let pos = position + origin;
+ var out: HorizonMaterialOut;
+ out.clip = horizon_terrain_globals.view_proj * vec4(pos, 1.0);
+ out.uv = uv;
+ out.layer = layer;
+ out.tint = vec4(color.rgb, 1.0);
+ let block = max(f32(light.y), horizon_terrain_dynamic_block_light(pos));
+ let sky = f32(light.x);
+ // 16 per level, 15 levels.
+ out.light = min(vec2(block, sky) / 240.0, vec2(1.0));
+ out.lightmap = horizon_terrain_sample_lightmap(sky / 16.0, block / 16.0).rgb;
+ out.position = pos;
+ out.spherical = length(pos);
+ out.cylindrical = max(length(pos.xz), abs(pos.y));
+ return out;
+}
+@vertex fn horizon_material_vs(
+ @location(0) position: vec3, @location(1) uv: vec2,
+ @location(2) layer: u32, @location(3) color: vec4, @location(4) light: vec2,
+ @location(5) origin: vec4) -> HorizonMaterialOut {
+ return horizon_material_vertex(position, horizon_terrain_terrain_uv(uv, origin.w), layer,
+ color, light, origin.xyz);
+}
+@vertex fn horizon_material_vs_flat(
+ @builtin(vertex_index) vertex: u32,
+ @location(0) position: vec3, @location(1) uv: vec2,
+ @location(2) layer: u32, @location(3) color: vec4, @location(4) light: vec2
+) -> HorizonMaterialOut {
+ let origin = horizon_terrain_slot_origins[horizon_terrain_block_slots[vertex >> 8u]];
+ return horizon_material_vertex(position, horizon_terrain_terrain_uv(uv, origin.w), layer,
+ color, light, origin.xyz);
+}
+fn horizon_material_color(in: HorizonMaterialOut, translucent: bool) -> vec4 {
+ let albedo = horizon_terrain_sprite(in.uv, in.layer & 0x7fffu);
+ return material(MaterialInput(albedo, in.tint, in.light, in.lightmap, in.position, in.uv,
+ translucent)).color;
+}
+@fragment fn horizon_material_fs_solid(in: HorizonMaterialOut) -> @location(0) vec4 {
+ let color = horizon_material_color(in, false);
+ // Bit 15 of the layer marks opaque leaves (forceOpaque): no alpha cutout.
+ if color.a < 0.5 && (in.layer & 0x8000u) == 0u { discard; }
+ return horizon_terrain_apply_fog(vec4(color.rgb, 1.0), in.spherical, in.cylindrical);
+}
+@fragment fn horizon_material_fs_translucent(in: HorizonMaterialOut) -> @location(0) vec4 {
+ let color = horizon_material_color(in, true);
+ if color.a < 0.01 { discard; }
+ return horizon_terrain_apply_fog(color, in.spherical, in.cylindrical);
+}
+";
+
+/// What terrain pipelines are built against: the renderer's layouts and constants.
+pub struct TerrainShape<'a> {
+ /// The terrain's group 0.
+ pub globals: &'a wgpu::BindGroupLayout,
+ /// The flat draws' group 1, where they run (`flat.rs`).
+ pub flat: Option<&'a wgpu::BindGroupLayout>,
+ /// terrain.wgsl's `SPRITE_ARRAYS` and `SPRITE_SPLIT`.
+ pub sprites: [f64; 2],
+ /// The world target's colour format.
+ pub format: wgpu::TextureFormat,
+}
+
+/// The pack's terrain pipelines: solid (with the cutout test) and translucent, for the
+/// renderer's instanced draws and, where they run, its flat ones.
+pub struct NativeMaterial {
+ pub solid: wgpu::RenderPipeline,
+ pub translucent: wgpu::RenderPipeline,
+ pub flat: Option<(wgpu::RenderPipeline, wgpu::RenderPipeline)>,
+}
+
+/// terrain.wgsl's terrain section (its globals, bindings, sampling, lighting and fog), with
+/// every name it declares prefixed.
+fn vanilla_terrain() -> String {
+ prefixed(
+ section(TERRAIN, "struct Globals {", "// Sky (SkyRenderer)"),
+ PREFIX,
+ )
+}
+
+/// The terrain shader for the pack's material stage `source`.
+fn shader_source(source: &str) -> String {
+ format!(
+ "{source}\n// Horizon's terrain (terrain.wgsl)\n{}{ENTRIES}",
+ vanilla_terrain()
+ )
+}
+
+/// The material's naga module, its errors in the stage's file.
+fn module(stage: &CheckedStage) -> Result {
+ let text = shader_source(&stage.source);
+ naga::front::wgsl::parse_str(&text)
+ .map_err(|e| format!("{}: {}", stage.file, e.emit_to_string(&text)))
+}
+
+impl NativeMaterial {
+ /// The pipelines of the pack's material `stage` with the option values `constants`.
+ pub fn new(
+ device: &wgpu::Device,
+ shape: &TerrainShape<'_>,
+ frame: &NativeFrame,
+ stage: &CheckedStage,
+ constants: &[(String, f64)],
+ ) -> Result {
+ let module = module(stage)?;
+ let mut constants = present_constants(&module, constants);
+ let sprite_names = [
+ "horizon_terrain_SPRITE_ARRAYS",
+ "horizon_terrain_SPRITE_SPLIT",
+ ];
+ constants.extend(sprite_names.into_iter().zip(shape.sprites));
+ let scope = device.push_error_scope(wgpu::ErrorFilter::Validation);
+ let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
+ label: Some(&stage.label),
+ source: wgpu::ShaderSource::Naga(std::borrow::Cow::Owned(module)),
+ });
+ let layout = |flat: Option<&wgpu::BindGroupLayout>| {
+ let mut groups: [Option<&wgpu::BindGroupLayout>; GROUP as usize + 1] =
+ Default::default();
+ groups[0] = Some(shape.globals);
+ groups[1] = flat;
+ groups[GROUP as usize] = Some(&frame.layout);
+ device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
+ label: Some("Native shader pack terrain"),
+ bind_group_layouts: &groups,
+ ..Default::default()
+ })
+ };
+ let instance_attributes = wgpu::vertex_attr_array![5=>Float32x4];
+ let vertices = wgpu::VertexBufferLayout {
+ array_stride: std::mem::size_of::() as u64,
+ step_mode: wgpu::VertexStepMode::Vertex,
+ attributes: &super::TERRAIN_ATTRIBUTES,
+ };
+ let instances = wgpu::VertexBufferLayout {
+ array_stride: 16,
+ step_mode: wgpu::VertexStepMode::Instance,
+ attributes: &instance_attributes,
+ };
+ // As the renderer's terrain pipelines (gfx.rs): back faces culled, reversed depth.
+ let pipeline = |layout: &wgpu::PipelineLayout,
+ vertex: &str,
+ buffers: &[Option>],
+ fragment: &str,
+ blend: Option| {
+ let options = wgpu::PipelineCompilationOptions {
+ constants: &constants,
+ ..Default::default()
+ };
+ device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
+ label: Some(&stage.label),
+ layout: Some(layout),
+ vertex: wgpu::VertexState {
+ module: &module,
+ entry_point: Some(vertex),
+ compilation_options: options.clone(),
+ buffers,
+ },
+ primitive: wgpu::PrimitiveState {
+ cull_mode: Some(wgpu::Face::Back),
+ ..Default::default()
+ },
+ depth_stencil: Some(wgpu::DepthStencilState {
+ format: wgpu::TextureFormat::Depth32Float,
+ depth_write_enabled: Some(true),
+ depth_compare: Some(wgpu::CompareFunction::GreaterEqual),
+ stencil: Default::default(),
+ bias: Default::default(),
+ }),
+ multisample: Default::default(),
+ fragment: Some(wgpu::FragmentState {
+ module: &module,
+ entry_point: Some(fragment),
+ compilation_options: options,
+ targets: &[Some(wgpu::ColorTargetState {
+ format: shape.format,
+ blend,
+ write_mask: wgpu::ColorWrites::ALL,
+ })],
+ }),
+ multiview_mask: None,
+ cache: None,
+ })
+ };
+ let blend = Some(wgpu::BlendState::ALPHA_BLENDING);
+ let instanced = layout(None);
+ let buffers = [Some(vertices.clone()), Some(instances)];
+ let solid = pipeline(
+ &instanced,
+ "horizon_material_vs",
+ &buffers,
+ "horizon_material_fs_solid",
+ None,
+ );
+ let translucent = pipeline(
+ &instanced,
+ "horizon_material_vs",
+ &buffers,
+ "horizon_material_fs_translucent",
+ blend,
+ );
+ let flat = shape.flat.map(|group| {
+ let layout = layout(Some(group));
+ let buffers = [Some(vertices.clone())];
+ let vs = "horizon_material_vs_flat";
+ (
+ pipeline(&layout, vs, &buffers, "horizon_material_fs_solid", None),
+ pipeline(
+ &layout,
+ vs,
+ &buffers,
+ "horizon_material_fs_translucent",
+ blend,
+ ),
+ )
+ });
+ if let Some(e) = pollster::block_on(scope.pop()) {
+ return Err(format!("{}: {e}", stage.file));
+ }
+ Ok(Self {
+ solid,
+ translucent,
+ flat,
+ })
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use horizon_shader_api::{MANIFEST, Pack, StageKind, check};
+
+ fn stage(material: &str) -> CheckedStage {
+ let manifest = "schema = 1\nid = \"t\"\nname = \"T\"\nversion = \"1\"\napi = \"0.1\"\n\
+ [material]\nfile = \"material.wgsl\"\n\
+ [[options]]\nid = \"gain\"\nname = \"Gain\"\ntype = \"float\"\n\
+ default = 1.0\nmin = 0.0\nmax = 2.0\n";
+ let files = [(MANIFEST, manifest), ("material.wgsl", material)]
+ .map(|(n, t)| (n.to_owned(), t.as_bytes().to_vec()))
+ .into();
+ let mut report = check(&Pack::from_files(files).unwrap()).unwrap();
+ assert_eq!(report.stages[0].kind, StageKind::Material);
+ report.stages.remove(0)
+ }
+
+ #[test]
+ fn the_terrain_takes_a_prefix_and_validates_with_a_pack() {
+ let terrain = vanilla_terrain();
+ for name in [
+ "Globals",
+ "globals",
+ "sprites_7",
+ "sprite",
+ "SPRITE_SPLIT",
+ "block_slots",
+ "sample_lightmap",
+ "apply_fog",
+ ] {
+ assert!(terrain.contains(&format!("{PREFIX}{name}")), "{name}");
+ }
+ assert!(!terrain.contains("fs_sky"), "the sky stays out");
+ // A pack may use any name Horizon's terrain uses unprefixed.
+ let own = "override gain: f32;\nconst PI: f32 = 3.0;\n\
+ fn sprite(c: vec4) -> vec4 { return c * PI; }\n\
+ fn apply_fog(c: vec4) -> vec4 { return c * gain; }\n\
+ fn material(input: MaterialInput) -> Surface {\n \
+ let lit = input.albedo * input.tint * vec4(input.lightmap, 1.0);\n \
+ return Surface(apply_fog(sprite(lit)));\n}\n";
+ let stage = stage(own);
+ let module = module(&stage).unwrap();
+ let info = naga::valid::Validator::new(
+ naga::valid::ValidationFlags::all(),
+ naga::valid::Capabilities::all(),
+ )
+ .validate(&module)
+ .unwrap_or_else(|e| panic!("{}", e.emit_to_string(&shader_source(&stage.source))));
+ let entries: Vec<&str> = module
+ .entry_points
+ .iter()
+ .map(|e| e.name.as_str())
+ .collect();
+ for entry in [
+ "horizon_material_vs",
+ "horizon_material_vs_flat",
+ "horizon_material_fs_solid",
+ "horizon_material_fs_translucent",
+ ] {
+ assert!(entries.contains(&entry), "{entry}");
+ }
+ drop(info);
+ assert_eq!(
+ present_constants(&module, &[("gain".into(), 1.5), ("other".into(), 1.0)]),
+ [("gain", 1.5)]
+ );
+ }
+}
diff --git a/crates/horizon-client/src/gfx/native_pack.rs b/crates/horizon-client/src/gfx/native_pack.rs
index 3f9206e11..4fb5cdc59 100644
--- a/crates/horizon-client/src/gfx/native_pack.rs
+++ b/crates/horizon-client/src/gfx/native_pack.rs
@@ -1,8 +1,9 @@
//! One of Horizon's own shader packs while it runs (docs/shaders.md): the frame its stages
-//! read, its sky and post stages, and the timer of their passes.
+//! read, its sky, material and post stages, and the timer of their passes.
use horizon_shader_api::{Report, StageKind};
+use super::native_material::{NativeMaterial, TerrainShape};
use super::native_post::{NativeFrame, NativePost};
use super::native_sky::NativeSky;
use super::pack_timing::PassTiming;
@@ -10,6 +11,7 @@ use super::pack_timing::PassTiming;
pub struct NativePack {
pub frame: NativeFrame,
pub sky: Option,
+ pub material: Option,
pub post: Option,
/// GPU time of each pass, while the renderer asks for it (`pack_timing.rs`).
pub timing: Option,
@@ -17,16 +19,16 @@ pub struct NativePack {
impl NativePack {
/// The pack of the checked stages `report` with the option values `constants`, drawing
- /// onto targets of `format`; `globals` is the terrain's group 0, which the sky reads.
+ /// onto the world's targets as `terrain` describes; the sky reads the terrain's group 0.
/// Also returns its stages' labels, in the order they draw. Errors name the stage wgpu
/// refused.
pub fn new(
device: &wgpu::Device,
- globals: &wgpu::BindGroupLayout,
+ terrain: &TerrainShape<'_>,
report: Report,
constants: &[(String, f64)],
- format: wgpu::TextureFormat,
) -> Result<(Self, Vec), String> {
+ let (globals, format) = (terrain.globals, terrain.format);
let frame = NativeFrame::new(device);
let (post, others): (Vec<_>, Vec<_>) = report
.stages
@@ -37,16 +39,28 @@ impl NativePack {
.find(|s| s.kind == StageKind::Sky)
.map(|stage| NativeSky::new(device, globals, &frame, stage, constants, format))
.transpose()?;
+ let material = others
+ .iter()
+ .find(|s| s.kind == StageKind::Material)
+ .map(|stage| NativeMaterial::new(device, terrain, &frame, stage, constants))
+ .transpose()?;
let post = (!post.is_empty())
.then(|| NativePost::new(device, post, constants, format, &frame))
.transpose()?;
let mut labels: Vec = sky.iter().map(|s| s.label.clone()).collect();
+ labels.extend(
+ others
+ .iter()
+ .filter(|s| material.is_some() && s.kind == StageKind::Material)
+ .map(|s| s.label.clone()),
+ );
if let Some(post) = &post {
labels.extend(post.labels().into_iter().map(str::to_owned));
}
let pack = Self {
frame,
sky,
+ material,
post,
timing: None,
};
@@ -55,6 +69,6 @@ impl NativePack {
/// Whether the pack draws anything the game runs today.
pub fn draws(&self) -> bool {
- self.sky.is_some() || self.post.is_some()
+ self.sky.is_some() || self.material.is_some() || self.post.is_some()
}
}
diff --git a/crates/horizon-client/src/gfx/occlusion.rs b/crates/horizon-client/src/gfx/occlusion.rs
index e17ba87d1..22e2f3c81 100644
--- a/crates/horizon-client/src/gfx/occlusion.rs
+++ b/crates/horizon-client/src/gfx/occlusion.rs
@@ -522,16 +522,27 @@ impl Occlusion {
}
pub fn set(&mut self, key: SectionKey, vis: u64) {
if self.columns.set(key, vis) {
- self.edits.push(Edit::Set(key, vis));
- self.dirty = true;
- self.changed.push(key);
- if self.running.is_some() {
- self.changed_late.push(key);
- }
+ self.changed(key, vis);
}
}
- pub fn remove(&mut self, key: SectionKey) {
- self.set(key, ALL_VISIBLE);
+ fn changed(&mut self, key: SectionKey, vis: u64) {
+ self.edits.push(Edit::Set(key, vis));
+ self.dirty = true;
+ self.changed.push(key);
+ if self.running.is_some() {
+ self.changed_late.push(key);
+ }
+ }
+ /// Every section of the column `x`, `z` goes (sees through every face).
+ pub fn remove_column(&mut self, x: i32, z: i32) {
+ let Some(column) = self.columns.map.remove(&(x, z)) else {
+ return;
+ };
+ for (y, &vis) in column.iter().enumerate() {
+ if vis != ALL_VISIBLE {
+ self.changed((x, y as i32, z), ALL_VISIBLE);
+ }
+ }
}
pub fn clear(&mut self) {
self.columns.map.clear();
diff --git a/crates/horizon-client/src/gfx/pack_atlas.rs b/crates/horizon-client/src/gfx/pack_atlas.rs
index 42a2abee1..c7a67a50b 100644
--- a/crates/horizon-client/src/gfx/pack_atlas.rs
+++ b/crates/horizon-client/src/gfx/pack_atlas.rs
@@ -169,6 +169,7 @@ impl PackAtlas {
];
let columns = self.columns;
// UVs past a sprite (models up to 2, distant tiles) cannot repeat in an atlas: clamped.
+ // The colour's alpha holds HDR's emission level (`mesher::EMISSION_BITS`): packs get 1.
let body = format!(
" vec4 hz_origin = {origin};\n \
uint hz_layer = hz_v_layer & 32767u;\n \
@@ -178,7 +179,7 @@ impl PackAtlas {
hz_MultiTexCoord0 = vec4((hz_cell + clamp(vec2(hz_v_uv) * hz_origin.w, 0.0, 1.0)) * hz_scale, 0.0, 1.0);\n \
hz_MultiTexCoord1 = vec4(float(hz_v_light.y), float(hz_v_light.x), 0.0, 1.0);\n \
hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \
- hz_Color = hz_v_color;\n \
+ hz_Color = vec4(hz_v_color.rgb, 1.0);\n \
hz_Normal = vec3(0.0, 1.0, 0.0);\n \
hz_tangent = vec4(1.0, 0.0, 0.0, 1.0);\n",
scale[0], scale[1],
diff --git a/crates/horizon-client/src/gfx/pack_uniforms.rs b/crates/horizon-client/src/gfx/pack_uniforms.rs
index c4df4377b..8f5f1a2de 100644
--- a/crates/horizon-client/src/gfx/pack_uniforms.rs
+++ b/crates/horizon-client/src/gfx/pack_uniforms.rs
@@ -399,7 +399,8 @@ impl FrameUniforms {
_ => Value::i(&[0]),
}
};
- self.values = NAMES.iter().map(|n| value(n)).collect();
+ self.values.clear();
+ self.values.extend(NAMES.iter().map(|n| value(n)));
self.frame = (self.frame + 1) % 720_720;
}
diff --git a/crates/horizon-client/src/graphics.rs b/crates/horizon-client/src/graphics.rs
index b51a586ab..4f6f04bc0 100644
--- a/crates/horizon-client/src/graphics.rs
+++ b/crates/horizon-client/src/graphics.rs
@@ -255,6 +255,12 @@ pub struct Graphics {
pub resolution_scale: u32,
/// FXAA over the world.
pub fxaa: bool,
+ /// HDR output (scRGB), where the display path offers it.
+ pub hdr: bool,
+ /// HDR paper white (where SDR white sits), in nits.
+ pub hdr_paper_white: u32,
+ /// HDR peak brightness the world's highlights reach, in nits.
+ pub hdr_peak: u32,
/// Battery mode: at most 60 frames per second in a world and 30 in menus
/// or behind other windows, and half the worker threads. Off by default;
/// it changes when frames are drawn, never what they show or the ticks.
@@ -286,6 +292,9 @@ impl Default for Graphics {
overlay: Overlay::Off,
resolution_scale: 100,
fxaa: false,
+ hdr: false,
+ hdr_paper_white: 200,
+ hdr_peak: 1000,
battery: false,
api: GraphicsApi::Auto,
shader_pack: String::new(),
@@ -337,6 +346,9 @@ impl Graphics {
"workers" => g.workers = number(g.workers).min(64),
"resolutionScale" => g.resolution_scale = number(g.resolution_scale).clamp(50, 200),
"fxaa" => g.fxaa = flag(g.fxaa),
+ "hdr" => g.hdr = flag(g.hdr),
+ "hdrPaperWhite" => g.hdr_paper_white = number(g.hdr_paper_white).clamp(80, 480),
+ "hdrPeak" => g.hdr_peak = number(g.hdr_peak).clamp(200, 2000),
"batteryMode" => g.battery = flag(g.battery),
"graphicsApi" => g.api = GraphicsApi::from_name(value).unwrap_or_default(),
"pickedShaderPack" => g.shader_pack = value.to_owned(),
@@ -354,7 +366,7 @@ impl Graphics {
}
pub fn serialize(&self) -> String {
format!(
- "preset:{}\nlodDistance:{}\nfarDetail:{}\ncaveCulling:{}\nentityCulling:{}\nchunkUpdates:{}\nuploadBudgetKiB:{}\nsky:{}\nfog:{}\nweather:{}\nanimatedTextures:{}\npresentMode:{}\nframePacing:{}\nworkers:{}\noverlay:{}\nresolutionScale:{}\nfxaa:{}\nbatteryMode:{}\ngraphicsApi:{}\npickedShaderPack:{}\n",
+ "preset:{}\nlodDistance:{}\nfarDetail:{}\ncaveCulling:{}\nentityCulling:{}\nchunkUpdates:{}\nuploadBudgetKiB:{}\nsky:{}\nfog:{}\nweather:{}\nanimatedTextures:{}\npresentMode:{}\nframePacing:{}\nworkers:{}\noverlay:{}\nresolutionScale:{}\nfxaa:{}\nhdr:{}\nhdrPaperWhite:{}\nhdrPeak:{}\nbatteryMode:{}\ngraphicsApi:{}\npickedShaderPack:{}\n",
self.preset.name(),
self.lod_distance,
self.far_detail.name(),
@@ -379,6 +391,9 @@ impl Graphics {
},
self.resolution_scale,
bool_text(self.fxaa),
+ bool_text(self.hdr),
+ self.hdr_paper_white,
+ self.hdr_peak,
bool_text(self.battery),
self.api.name(),
self.shader_pack,
@@ -444,7 +459,7 @@ pub fn apply_preset(preset: Preset, s: &mut crate::settings::Settings) {
let g = &mut s.horizon;
g.preset = preset;
g.cave_culling = true;
- g.animated_textures = preset != Preset::Performance;
+ g.animated_textures = true;
g.chunk_updates = 0;
g.upload_budget_kib = 0;
g.sky = true;
@@ -476,6 +491,9 @@ mod tests {
overlay: Overlay::Graph,
resolution_scale: 75,
fxaa: true,
+ hdr: true,
+ hdr_paper_white: 240,
+ hdr_peak: 1400,
battery: true,
api: GraphicsApi::Dx12,
shader_pack: "Golden Hour".into(),
diff --git a/crates/horizon-client/src/graphics_ui.rs b/crates/horizon-client/src/graphics_ui.rs
index ba99f89a8..3c7a40e02 100644
--- a/crates/horizon-client/src/graphics_ui.rs
+++ b/crates/horizon-client/src/graphics_ui.rs
@@ -37,6 +37,9 @@ pub enum H {
Overlay,
ResolutionScale,
Antialiasing,
+ Hdr,
+ HdrPaperWhite,
+ HdrPeak,
BatteryMode,
GraphicsApi,
}
@@ -221,6 +224,21 @@ pub fn rows(level: Level) -> Vec {
Medium,
"FXAA smooths jagged edges of the world; slightly softer image.",
),
+ h(
+ H::Hdr,
+ None,
+ "HDR output on an HDR display: lava, fire and sunlight shine brighter.",
+ ),
+ h(
+ H::HdrPaperWhite,
+ None,
+ "HDR: how bright white paper and menus look. Match your desktop.",
+ ),
+ h(
+ H::HdrPeak,
+ None,
+ "HDR: how bright the brightest highlights get. Your display's peak.",
+ ),
h(
H::PresentMode,
None,
@@ -324,6 +342,8 @@ impl H {
}
H::Workers => Some(g.workers as f32 / max_workers() as f32),
H::ResolutionScale => Some((g.resolution_scale as f32 - 50.0) / 150.0),
+ H::HdrPaperWhite => Some((g.hdr_paper_white as f32 - 80.0) / 400.0),
+ H::HdrPeak => Some((g.hdr_peak as f32 - 200.0) / 1800.0),
_ => None,
}
}
@@ -365,6 +385,9 @@ impl H {
},
H::ResolutionScale => labelled("Resolution Scale", format!("{}%", g.resolution_scale)),
H::Antialiasing => labelled("Anti-aliasing", if g.fxaa { "FXAA" } else { "Off" }),
+ H::Hdr => labelled("HDR", on_off(g.hdr)),
+ H::HdrPaperWhite => labelled("Paper White", format!("{} nits", g.hdr_paper_white)),
+ H::HdrPeak => labelled("Peak Brightness", format!("{} nits", g.hdr_peak)),
H::BatteryMode => labelled("Battery Mode", on_off(g.battery)),
H::GraphicsApi => labelled("Graphics API", g.api.label()),
H::Overlay => labelled(
@@ -387,6 +410,8 @@ impl H {
}
H::Workers => g.workers = (v * max_workers() as f32).round() as u32,
H::ResolutionScale => g.resolution_scale = (50.0 + (v * 30.0).round() * 5.0) as u32,
+ H::HdrPaperWhite => g.hdr_paper_white = (80.0 + (v * 40.0).round() * 10.0) as u32,
+ H::HdrPeak => g.hdr_peak = (200.0 + (v * 36.0).round() * 50.0) as u32,
_ => {}
}
}
@@ -418,6 +443,7 @@ impl H {
H::AnimatedTextures => g.animated_textures = !g.animated_textures,
H::PresentMode => g.present_mode = step(&PresentMode::ALL, g.present_mode, back),
H::Antialiasing => g.fxaa = !g.fxaa,
+ H::Hdr => g.hdr = !g.hdr,
H::BatteryMode => g.battery = !g.battery,
H::GraphicsApi => g.api = step(&GraphicsApi::ALL, g.api, back),
H::Pacing => {
@@ -453,6 +479,7 @@ impl Item {
Item::Horizon(H::Weather) => g.weather,
Item::Horizon(H::AnimatedTextures) => g.animated_textures,
Item::Horizon(H::Antialiasing) => g.fxaa,
+ Item::Horizon(H::Hdr) => g.hdr,
Item::Horizon(H::BatteryMode) => g.battery,
Item::Vanilla(Opt::ViewBobbing) => s.view_bobbing,
Item::Vanilla(Opt::Vsync) => s.vsync,
diff --git a/crates/horizon-client/src/hdr.wgsl b/crates/horizon-client/src/hdr.wgsl
new file mode 100644
index 000000000..bf6c27cca
--- /dev/null
+++ b/crates/horizon-client/src/hdr.wgsl
@@ -0,0 +1,69 @@
+// Horizon's HDR output pass: the frame, drawn as without HDR in vanilla's gamma
+// space, decoded to linear light at the paper white and written to the scRGB
+// swapchain (1.0 = 80 nits). Bright pixels of light sources that still show the
+// world as it was before the hand and HUD are expanded towards the peak.
+@group(0) @binding(0) var frame: texture_2d;
+@group(0) @binding(1) var world: texture_2d;
+
+struct Params {
+ // Paper white in scRGB units (nits / 80).
+ paper: f32,
+ // Peak brightness relative to paper white; 1 expands nothing.
+ peak: f32,
+ // 1 when this frame drew a world.
+ world: f32,
+ // 1 paints expanded pixels magenta (HORIZON_HDR_DEBUG=1).
+ debug: f32,
+};
+@group(0) @binding(2) var params: Params;
+
+// Light sources are lifted evenly rather than by a highlight curve: vanilla's
+// textures were drawn for SDR, so a curve on their brightest pixels blows out
+// near-white spots (glowstone) and leaves the rest (lava, a jack o'lantern's
+// face) nearly as it was. Pixels brighter than LIFT_FULL get STRENGTH of the
+// headroom, dimmer ones less down to none at LIFT_START (lava's crust, a
+// lantern's metal), and no channel goes past the peak. Brightness is the mean
+// of luminance and the brightest channel, so saturated light counts as bright.
+const LIFT_START: f32 = 0.18;
+const LIFT_FULL: f32 = 0.42;
+const STRENGTH: f32 = 0.7;
+
+// Whether `c` still shows the world pixel `w`: the same, or darkened evenly
+// across its channels, as the vignette and other screen-wide tints darken it.
+// The hand, HUD and text replace pixels instead, so they never pass.
+fn shows_world(c: vec3, w: vec3) -> bool {
+ if all(c == w) {
+ return true;
+ }
+ let k = c / max(w, vec3(1.0 / 255.0));
+ let mean = (k.r + k.g + k.b) / 3.0;
+ return mean <= 1.01 && mean > 0.5 && all(abs(k - vec3(mean)) < vec3(0.03));
+}
+
+@vertex fn vs_hdr(@builtin(vertex_index) i: u32) -> @builtin(position) vec4 {
+ let p = vec2(f32((i << 1u) & 2u), f32(i & 2u));
+ return vec4(p * 2.0 - 1.0, 0.0, 1.0);
+}
+
+@fragment fn fs_hdr(@builtin(position) pos: vec4) -> @location(0) vec4 {
+ let at = vec2(pos.xy);
+ let c = textureLoad(frame, at, 0).rgb;
+ // Gamma 2.2, as SDR displays and the compositors' SDR paths decode it.
+ var light = pow(max(c, vec3(0.0)), vec3(2.2));
+ let w = textureLoad(world, at, 0);
+ // The world's alpha is 0 on light-emitting blocks, the sun and the moon
+ // (terrain.wgsl, the celestial pipeline): only those shine.
+ if params.world > 0.5 && params.peak > 1.0 && w.a < 0.75 && shows_world(c, w.rgb) {
+ let y = 0.5 * (dot(light, vec3(0.2126, 0.7152, 0.0722)) + max(light.r, max(light.g, light.b)));
+ let lift = smoothstep(LIFT_START, LIFT_FULL, y);
+ if lift > 0.0 {
+ let top = max(light.r, max(light.g, light.b));
+ let gain = min(1.0 + (params.peak - 1.0) * STRENGTH * lift, params.peak / max(top, 1e-4));
+ light *= max(gain, 1.0);
+ if params.debug > 0.5 {
+ light = mix(light, vec3(gain, 0.0, gain), 0.6);
+ }
+ }
+ }
+ return vec4(light * params.paper, 1.0);
+}
diff --git a/crates/horizon-client/src/hud_overlays.rs b/crates/horizon-client/src/hud_overlays.rs
index ecc23a5ee..ebd860e08 100644
--- a/crates/horizon-client/src/hud_overlays.rs
+++ b/crates/horizon-client/src/hud_overlays.rs
@@ -1111,8 +1111,9 @@ impl SubtitleOverlay {
s.purge_old_instances(3000.0 * frame.multiplier, now);
}
if s.audible_from(position) && !s.played.is_empty() {
- width = width.max(width_of(&Text::translate(&s.key, &[])));
- audible.push(i);
+ let text_width = width_of(&Text::translate(&s.key, &[]));
+ width = width.max(text_width);
+ audible.push((i, text_width));
}
}
if audible.is_empty() {
@@ -1121,7 +1122,7 @@ impl SubtitleOverlay {
let w = |s: &str| font.width(s) as i32;
width += w("<") + w(" ") + w(">") + w(" ");
let mut row = 0;
- for i in audible {
+ for (i, text_width) in audible {
let s = &self.subtitles[i];
let text_value = Text::translate(&s.key, &[]);
let Some(closest) = s.best_candidate(position) else {
@@ -1145,7 +1146,6 @@ impl SubtitleOverlay {
let half_width = width / 2;
let height = 9;
let half_height = height / 2;
- let text_width = width_of(&text_value);
let brightness = s.brightness(closest, frame.multiplier, now);
out.push(GuiDraw::Push(vec![
PoseOp::Translate(
diff --git a/crates/horizon-client/src/item_data.rs b/crates/horizon-client/src/item_data.rs
index c07e2f352..ac624e85b 100644
--- a/crates/horizon-client/src/item_data.rs
+++ b/crates/horizon-client/src/item_data.rs
@@ -204,6 +204,14 @@ impl ItemStack {
pub fn custom_name(&self) -> Option<&str> {
self.data().and_then(|d| d.custom_name.as_deref())
}
+ /// `minecraft:item_name` in the current language (`getItemName`'s
+ /// component). It stays in the patch as loaded, so a translatable name
+ /// saves and reaches clients unchanged; the ominous banner's is modelled
+ /// by `ominous_banner` instead.
+ pub fn item_name(&self) -> Option {
+ let name = self.data()?.other("minecraft:item_name")?;
+ Some(crate::entity_nbt::name_text(name).plain())
+ }
pub fn potion(&self) -> Option<&str> {
self.data().and_then(|d| d.potion.as_deref())
}
diff --git a/crates/horizon-client/src/item_render.rs b/crates/horizon-client/src/item_render.rs
index c2f5c8400..4f981b874 100644
--- a/crates/horizon-client/src/item_render.rs
+++ b/crates/horizon-client/src/item_render.rs
@@ -363,6 +363,9 @@ pub struct ItemRenderer {
sprites: FileSprites,
baked: Mutex>>,
extents: Mutex>>>,
+ /// GUI items already drawn: the HUD vertices of a static item at a GUI position
+ /// (`gui::draw`), which are the same every frame it stays there.
+ pub(crate) gui_drawn: Mutex>>,
/// Entity model layers of the special renderers.
parts: HashMap,
/// `colormap/grass.png` as ARGB.
@@ -452,6 +455,7 @@ impl ItemRenderer {
},
baked: Mutex::default(),
extents: Mutex::default(),
+ gui_drawn: Mutex::default(),
parts,
grass,
})
diff --git a/crates/horizon-client/src/item_render/gui.rs b/crates/horizon-client/src/item_render/gui.rs
index e3d7cc876..ff25fff1d 100644
--- a/crates/horizon-client/src/item_render/gui.rs
+++ b/crates/horizon-client/src/item_render/gui.rs
@@ -8,7 +8,7 @@
use super::{Foil, ItemQuad, ItemRenderState, ItemRenderer, SpecialHost};
use crate::hud::{HudBatch, Rect};
use glam::{EulerRot, Mat4, Vec2, Vec3};
-use std::{collections::HashMap, f32::consts::PI};
+use std::{collections::HashMap, f32::consts::PI, sync::Arc};
mod depth;
@@ -148,6 +148,95 @@ pub fn draw(
if state.is_empty() {
return;
}
+ // A static item drawn where it was drawn before gives the same vertices:
+ // take them instead of submitting, sorting and clipping its quads again.
+ let key = DrawnKey::of(state, renderer, sprites, x, y);
+ if let Some(key) = &key
+ && let Some(drawn) = renderer
+ .gui_drawn
+ .lock()
+ .ok()
+ .and_then(|d| d.get(key).cloned())
+ {
+ batch.vertices.extend_from_slice(&drawn);
+ return;
+ }
+ let start = batch.vertices.len();
+ draw_now(state, renderer, sprites, host, batch, x, y, time);
+ if let Some(key) = key
+ && let Ok(mut drawn) = renderer.gui_drawn.lock()
+ {
+ // GUI positions and items are few; a long scroll through every tab stays bounded.
+ if drawn.len() >= DRAWN_LIMIT {
+ drawn.clear();
+ }
+ drawn.insert(key, batch.vertices[start..].into());
+ }
+}
+
+/// Entries of `ItemRenderer::gui_drawn` before it starts over.
+const DRAWN_LIMIT: usize = 8192;
+
+/// What a GUI item's vertices depend on: its layers' baked models (kept for the
+/// renderer's life, so their addresses name them), tints and transforms, the
+/// atlas, and the exact position. None for items whose drawing changes with time
+/// or the host: a glint, or a special model renderer.
+#[derive(Clone, PartialEq, Eq, Hash)]
+pub(crate) struct DrawnKey {
+ renderer: usize,
+ sprites: (usize, usize),
+ at: [u32; 2],
+ oversized: bool,
+ layers: Vec<(usize, Vec, [u32; 16], bool)>,
+}
+impl DrawnKey {
+ fn of(
+ state: &ItemRenderState,
+ renderer: &ItemRenderer,
+ sprites: &HashMap,
+ x: f32,
+ y: f32,
+ ) -> Option {
+ if state.animated {
+ return None;
+ }
+ let layers = state
+ .layers
+ .iter()
+ .map(|layer| match &layer.geometry {
+ super::LayerGeometry::Quads(baked) if layer.foil == super::FoilType::None => {
+ Some((
+ Arc::as_ptr(baked) as usize,
+ layer.tints.clone(),
+ layer.matrix.to_cols_array().map(f32::to_bits),
+ layer.uses_block_light,
+ ))
+ }
+ _ => None,
+ })
+ .collect::>>()?;
+ Some(Self {
+ renderer: std::ptr::from_ref(renderer) as usize,
+ sprites: (std::ptr::from_ref(sprites) as usize, sprites.len()),
+ at: [x.to_bits(), y.to_bits()],
+ oversized: state.oversized_in_gui,
+ layers,
+ })
+ }
+}
+
+/// [`draw`] without the cache.
+#[allow(clippy::too_many_arguments)]
+fn draw_now(
+ state: &ItemRenderState,
+ renderer: &ItemRenderer,
+ sprites: &HashMap,
+ host: &dyn SpecialHost,
+ batch: &mut HudBatch,
+ x: f32,
+ y: f32,
+ time: f32,
+) {
let pose = Mat4::from_translation(Vec3::new(x + 8.0, y + 8.0, 0.0))
* Mat4::from_scale(Vec3::new(16.0, -16.0, 16.0));
let mut quads: Vec = Vec::new();
diff --git a/crates/horizon-client/src/item_sync.rs b/crates/horizon-client/src/item_sync.rs
index 930304ec4..fc35e15e7 100644
--- a/crates/horizon-client/src/item_sync.rs
+++ b/crates/horizon-client/src/item_sync.rs
@@ -238,6 +238,125 @@ mod tests {
use super::*;
use horizon_protocol::registry::Ids;
+ #[test]
+ fn merchant_water_cost_survives_the_offer_packet() {
+ use horizon_protocol::{
+ buf::{Codec, Reader, Writer},
+ packets::play::{NetOffer, Offers},
+ };
+ let mut components = vec![String::new(); component::POTION_CONTENTS as usize + 1];
+ components[component::POTION_CONTENTS as usize] = "minecraft:potion_contents".into();
+ let builtin = Builtin {
+ registries: [
+ (
+ "minecraft:item".into(),
+ Ids::from_names(vec!["minecraft:potion".into(), "minecraft:emerald".into()]),
+ ),
+ (
+ "minecraft:potion".into(),
+ Ids::from_names(vec!["minecraft:water".into()]),
+ ),
+ (
+ "minecraft:data_component_type".into(),
+ Ids::from_names(components),
+ ),
+ ]
+ .into(),
+ };
+ let dynamic = Dynamic::default();
+ let water = ItemStack::new("minecraft:potion", 1).with_potion("minecraft:water");
+ let wire = crate::lan::guest::stack_to_network(
+ |name| builtin.get("minecraft:item").unwrap().id(name).unwrap(),
+ |registry, name| builtin.get(registry)?.id(name),
+ &Some(water.clone()),
+ )
+ .unwrap();
+ let offers = vec![NetOffer {
+ cost_a: wire.into(),
+ result: Stack::new(1, 1),
+ cost_b: Some(Stack::new(1, 2).into()),
+ out_of_stock: false,
+ uses: 0,
+ max_uses: 2,
+ xp: 0,
+ special_price: 0,
+ price_multiplier: 0.05,
+ demand: 0,
+ }];
+ let mut w = Writer::new();
+ Offers::write(&offers, &mut w);
+ let mut r = Reader::new(&w.data);
+ let decoded = Offers::read(&mut r).unwrap();
+ assert_eq!(r.remaining(), 0);
+ let cost = crate::merchant::ItemCost {
+ stack: from_network(&decoded[0].cost_a.clone().into(), &builtin, &dynamic),
+ };
+ assert_eq!(cost.stack, water);
+ assert_eq!(cost.stack.potion(), Some("minecraft:water"));
+ let authoritative = crate::merchant::ItemCost {
+ stack: water.clone(),
+ };
+ for payment in [
+ water,
+ ItemStack::new("minecraft:potion", 1).with_potion("minecraft:swiftness"),
+ ItemStack::new("minecraft:potion", 1),
+ ] {
+ assert_eq!(cost.test(&payment), authoritative.test(&payment));
+ }
+ assert!(cost.test(&ItemStack::new("minecraft:potion", 1).with_potion("minecraft:water")));
+ assert!(
+ !cost.test(&ItemStack::new("minecraft:potion", 1).with_potion("minecraft:swiftness"))
+ );
+ let emerald = crate::merchant::ItemCost {
+ stack: from_network(
+ &decoded[0].cost_b.clone().unwrap().into(),
+ &builtin,
+ &dynamic,
+ ),
+ };
+ assert!(emerald.test(&ItemStack::new("minecraft:emerald", 2)));
+ }
+
+ /// A bundle's contents cross the wire, so a server's client can draw
+ /// its tooltip (BundleContents' stream codec: a list of templates).
+ #[test]
+ fn a_bundle_keeps_its_contents_across_the_wire() {
+ let mut components = vec![String::new(); 60];
+ components[component::BUNDLE_CONTENTS as usize] = "minecraft:bundle_contents".into();
+ let items = ["minecraft:air", "minecraft:bundle", "minecraft:stone"];
+ let builtin = Builtin {
+ registries: [
+ (
+ "minecraft:item".to_owned(),
+ Ids::from_names(items.iter().map(|s| s.to_string()).collect()),
+ ),
+ (
+ "minecraft:data_component_type".to_owned(),
+ Ids::from_names(components),
+ ),
+ ]
+ .into(),
+ };
+ let dynamic = Dynamic {
+ registries: Default::default(),
+ };
+ let mut bundle = ItemStack::new("minecraft:bundle", 1);
+ bundle.data_mut().bundle_contents = vec![ItemStack::new("minecraft:stone", 32)];
+ let wire = crate::lan::guest::stack_to_network(
+ |name| items.iter().position(|i| *i == name).unwrap_or(0) as i32,
+ |_, _| None,
+ &Some(bundle),
+ )
+ .unwrap();
+ let back = from_network(&wire, &builtin, &dynamic);
+ let contents = &back.data().expect("components").bundle_contents;
+ assert_eq!(contents.len(), 1);
+ assert_eq!(
+ (&*contents[0].item, contents[0].count),
+ ("minecraft:stone", 32)
+ );
+ }
+
/// What a creative client sends (`stack_to_network`) comes back whole on
/// the server: damage, both enchantment lists, repair cost, name, trim.
#[test]
@@ -246,6 +365,7 @@ mod tests {
for (id, name) in [
(component::DAMAGE, "minecraft:damage"),
(component::CUSTOM_NAME, "minecraft:custom_name"),
+ (component::ITEM_NAME, "minecraft:item_name"),
(component::ENCHANTMENTS, "minecraft:enchantments"),
(component::POTION_CONTENTS, "minecraft:potion_contents"),
(component::REPAIR_COST, "minecraft:repair_cost"),
@@ -305,6 +425,10 @@ mod tests {
.push((Arc::from("minecraft:sharpness"), 2));
d.repair_cost = 9;
d.custom_name = Some("Keeper".to_owned());
+ // A translatable item name reaches the client as the same component.
+ let item_name = Tag::compound([("translate", Tag::String("filled_map.monument".into()))]);
+ d.other
+ .push(("minecraft:item_name".into(), item_name.clone()));
d.trim = Some((Arc::from("minecraft:gold"), Arc::from("minecraft:sentry")));
let id = |registry: &str, name: &str| {
builtin
@@ -334,6 +458,7 @@ mod tests {
);
assert_eq!(d.repair_cost, 9);
assert_eq!(d.custom_name.as_deref(), Some("Keeper"));
+ assert_eq!(d.other("minecraft:item_name"), Some(&item_name));
assert_eq!(
d.trim,
Some((Arc::from("minecraft:gold"), Arc::from("minecraft:sentry")))
diff --git a/crates/horizon-client/src/item_tooltip.rs b/crates/horizon-client/src/item_tooltip.rs
index 6e51de391..88f01439e 100644
--- a/crates/horizon-client/src/item_tooltip.rs
+++ b/crates/horizon-client/src/item_tooltip.rs
@@ -56,10 +56,9 @@ impl<'a> TooltipDisplay<'a> {
else {
return true;
};
- let component = horizon_world::nbt::JavaString::from(component);
- !hidden
- .iter()
- .any(|tag| matches!(tag, Tag::String(id) if id == &component))
+ !hidden.iter().any(|tag| {
+ matches!(tag, Tag::String(id) if id.0.iter().copied().eq(component.encode_utf16()))
+ })
}
}
@@ -189,6 +188,9 @@ pub fn name_line(stack: &ItemStack, translate: &dyn Fn(&str) -> Option)
if let Some(name) = stack.custom_name() {
return name.to_owned();
}
+ if let Some(name) = stack.item_name() {
+ return name;
+ }
if stack.data().is_some_and(|d| d.ominous_banner) {
return translate("block.minecraft.ominous_banner")
.unwrap_or_else(|| "Ominous Banner".to_owned());
@@ -612,6 +614,14 @@ mod tests {
assert_eq!(rarity(&banner, "common"), "uncommon");
banner.data_mut().custom_name = Some("Captain's Standard".into());
assert_eq!(name_line(&banner, &translate), "Captain's Standard");
+ let mut custom_item = ItemStack::new("minecraft:diamond_sword", 1);
+ custom_item.data_mut().other.push((
+ "minecraft:item_name".into(),
+ Tag::String("Excalibur".into()),
+ ));
+ assert_eq!(name_line(&custom_item, &translate), "Excalibur");
+ custom_item.data_mut().custom_name = Some("Custom Blade".into());
+ assert_eq!(name_line(&custom_item, &translate), "Custom Blade");
banner.enchant("minecraft:unbreaking", 1);
assert_eq!(rarity(&banner, "common"), "rare");
let plain = ItemStack::new("minecraft:white_banner", 1);
diff --git a/crates/horizon-client/src/items.rs b/crates/horizon-client/src/items.rs
index 8ac450bcb..b4092a201 100644
--- a/crates/horizon-client/src/items.rs
+++ b/crates/horizon-client/src/items.rs
@@ -71,6 +71,8 @@ pub struct ItemInfo {
/// The `tool` component's `damage_per_block`, when the item has the
/// component (a mace or trident has one without rules).
pub tool_damage_per_block: Option,
+ /// `tool.can_destroy_blocks_in_creative` (defaults to true).
+ pub can_destroy_blocks_in_creative: bool,
pub food: Option<(u32, f32)>,
pub can_always_eat: bool,
/// `consumable`: its timing, sound, particles and `on_consume_effects`.
@@ -120,6 +122,8 @@ pub struct ItemInfo {
pub crafting_remainder: Option,
/// `dye`: the dye colour name (dyes, and anything else with the component).
pub dye: Option,
+ /// `cushion/color`: the DyeColor name of a cushion item.
+ pub cushion_color: Option,
/// `block_transformer`: the registry entry an axe, hoe or shovel uses on blocks.
pub block_transformer: Option,
/// Has a default `bundle_contents` (the bundles, `BundleItem`).
@@ -251,6 +255,10 @@ pub struct Items {
loot: HashMap,
/// The `jukebox_song` registry.
pub songs: HashMap,
+ /// The enchantment registry: loot functions enchant the items they roll
+ /// (`EnchantWithLevelsFunction`, `EnchantRandomlyFunction`), and the
+ /// station menus share this copy.
+ pub enchantments: Arc,
}
/// A `JukeboxSong`.
#[derive(Clone, Debug)]
@@ -552,6 +560,10 @@ impl Items {
tool_damage_per_block: c
.get("minecraft:tool")
.map(|t| t["damage_per_block"].as_u64().unwrap_or(1) as u32),
+ can_destroy_blocks_in_creative:
+ c["minecraft:tool"]["can_destroy_blocks_in_creative"]
+ .as_bool()
+ .unwrap_or(true),
food,
can_always_eat,
// Consumable.CODEC's defaults.
@@ -599,6 +611,7 @@ impl Items {
.map(str::to_owned),
jukebox_song: c["minecraft:jukebox_playable"].as_str().map(str::to_owned),
dye: c["minecraft:dye"].as_str().map(str::to_owned),
+ cushion_color: c["minecraft:cushion/color"].as_str().map(str::to_owned),
block_transformer: c["minecraft:block_transformer"].as_str().map(str::to_owned),
bundle: c.get("minecraft:bundle_contents").is_some(),
crafting_remainder: CRAFT_REMAINDERS
@@ -701,12 +714,15 @@ impl Items {
item_tag_order: load_tag_lists(root, "item")?,
loot,
songs: load_songs(root),
+ enchantments: Arc::new(crate::enchant::Enchantments::load(root)),
})
}
pub fn get(&self, id: &str) -> Option<&ItemInfo> {
self.items.get(id)
}
- /// Only the block and item tags, for tools that need tag membership.
+ /// Only the block and item tags (and the enchantment registry, which the
+ /// station rules and loot functions are built from), for tools that need
+ /// tag membership.
pub fn tags_only(root: &Path) -> Result {
Ok(Self {
items: HashMap::new(),
@@ -717,6 +733,7 @@ impl Items {
item_tag_order: load_tag_lists(root, "item")?,
loot: HashMap::new(),
songs: HashMap::new(),
+ enchantments: Arc::new(crate::enchant::Enchantments::load(root)),
})
}
/// An item table with only tags, for unit tests of item rules.
@@ -734,6 +751,7 @@ impl Items {
item_tags,
loot: HashMap::new(),
songs: HashMap::new(),
+ enchantments: Arc::new(crate::enchant::Enchantments::default()),
}
}
/// Whether `block` is in the block tag `tag` (`minecraft:fire`, no `#`).
@@ -746,6 +764,26 @@ impl Items {
None => spec == block,
}
}
+ /// `Item.canDestroyBlock`: a tool may forbid block destruction with instabuild.
+ pub fn can_destroy_block(&self, held: Option<&ItemStack>, creative: bool) -> bool {
+ if !creative {
+ return true;
+ }
+ let Some(held) = held.filter(|s| s.count > 0 && &*s.item != "minecraft:air") else {
+ return true;
+ };
+ if let Some(tool) = held.data().and_then(|d| d.other("minecraft:tool")) {
+ return tool
+ .get("can_destroy_blocks_in_creative")
+ .and_then(|v| match v {
+ horizon_world::nbt::Tag::Byte(v) => Some(*v),
+ _ => None,
+ })
+ .is_none_or(|v| v != 0);
+ }
+ self.get(&held.item)
+ .is_none_or(|info| info.can_destroy_blocks_in_creative)
+ }
/// `Tool.getMiningSpeed` for the held item (1.0 without a tool component).
pub fn mining_speed(&self, held: Option<&ItemStack>, block: &str) -> f32 {
let Some(info) = held.and_then(|s| self.get(&s.item)) else {
@@ -1371,6 +1409,16 @@ impl LootContext<'_> {
self.modifiers(&modifiers, &mut stack, random);
produced.push(stack);
}
+ /// An enchantment function's `options` (`HolderSet`): a tag, a list or
+ /// one id; every enchantment when absent (`registryAccess().listElements`).
+ fn enchant_pool(e: &crate::enchant::Enchantments, options: &Value) -> Vec> {
+ match options {
+ Value::String(s) if s.starts_with('#') => e.tag(s).to_vec(),
+ Value::String(s) => vec![Arc::from(s.as_str())],
+ Value::Array(a) => a.iter().filter_map(|x| x.as_str().map(Arc::from)).collect(),
+ _ => e.list.iter().map(|x| Arc::clone(&x.id)).collect(),
+ }
+ }
fn modifiers(&self, m: &Value, stack: &mut ItemStack, random: &mut R) {
let list: Vec<&Value> = match m {
Value::Array(a) => a.iter().collect(),
@@ -1449,6 +1497,54 @@ impl LootContext<'_> {
.with_potion(id);
}
}
+ // EnchantWithLevelsFunction: EnchantmentHelper.enchantItem on
+ // the context's random (the End cities' gear).
+ "minecraft:enchant_with_levels" => {
+ let cost = Self::int_number(&f["levels"], random);
+ let e = &self.items.enchantments;
+ let pool = Self::enchant_pool(e, &f["options"]);
+ let chosen = e.select(self.items, random, &stack.item, cost, &pool);
+ // EnchantmentHelper.enchantItem: a book becomes one
+ // enchanted book (the fresh stack's count).
+ if &*stack.item == "minecraft:book" {
+ *stack = ItemStack::new("minecraft:enchanted_book", 1);
+ }
+ for (id, level) in chosen {
+ stack.enchant(&id, level);
+ }
+ }
+ // EnchantRandomlyFunction: one enchantment the item accepts,
+ // at a level between its min and max.
+ "minecraft:enchant_randomly" => {
+ let e = &self.items.enchantments;
+ let book = &*stack.item == "minecraft:book";
+ let only_compatible = f["only_compatible"].as_bool().unwrap_or(true);
+ let options: Vec> = Self::enchant_pool(e, &f["options"])
+ .into_iter()
+ .filter(|id| {
+ book || !only_compatible || e.can_enchant(self.items, id, &stack.item)
+ })
+ .collect();
+ if options.is_empty() {
+ continue;
+ }
+ let id = options
+ [random.next_int_bound(options.len() as i32).unwrap_or(0) as usize]
+ .clone();
+ let max = e.get(&id).map_or(1, |d| d.max_level).max(1);
+ // `Mth.nextInt(random, 1, max)`: a single-level
+ // enchantment (Mending) returns 1 without drawing, so the
+ // rolls after this one keep vanilla's sequence.
+ let level = if max <= 1 {
+ 1
+ } else {
+ 1 + random.next_int_bound(max as i32).unwrap_or(0) as u32
+ };
+ if book {
+ *stack = ItemStack::new("minecraft:enchanted_book", 1);
+ }
+ stack.enchant(&id, level);
+ }
"minecraft:limit_count" => {
let limit = &f["limit"];
if let Some(min) = limit["min"].as_i64() {
@@ -1658,6 +1754,102 @@ mod tests {
assert_eq!(drops[0].potion(), Some("minecraft:fire_resistance"));
}
+ /// `/loot give` and every other table this dispatcher rolls: the two
+ /// enchantment functions (`EnchantWithLevelsFunction`,
+ /// `EnchantRandomlyFunction`) put their enchantments on the stack.
+ #[test]
+ fn loot_enchant_functions_enchant_the_rolled_item() {
+ let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
+ .join("../../.local/horizon-client/26.3");
+ if !root.join("items.json").exists() {
+ return;
+ }
+ let assets = crate::assets::Assets::load(&root).unwrap();
+ let facts = crate::blocks::BlockFacts::load(&root, assets.registry).unwrap();
+ let items = Items::load(&root, assets.registry, &facts).unwrap();
+ let props = BTreeMap::new();
+ let mut random = LegacyRandom::new(11);
+ // A sword at levels 20..39 comes out enchanted, as in an End city.
+ let levels = json!({"pools":[{"rolls":1,"entries":[{"type":"minecraft:item",
+ "name":"minecraft:diamond_sword","modifier":[
+ {"type":"minecraft:enchant_with_levels","levels":{"type":"minecraft:uniform","min":20.0,"max":39.0}}]}]}]});
+ let mut enchanted = 0;
+ for _ in 0..40 {
+ let drops = ctx(&items, None, &props, "").evaluate(&levels, &mut random);
+ if drops[0].enchantments().is_empty() {
+ continue;
+ }
+ enchanted += 1;
+ assert_eq!(&*drops[0].item, "minecraft:diamond_sword");
+ // Levels stay within the enchantment's range.
+ for (id, level) in drops[0].enchantments() {
+ let max = items
+ .enchantments
+ .get(id)
+ .expect("a known enchantment")
+ .max_level;
+ assert!((1..=max).contains(level), "{id} at {level}");
+ }
+ }
+ assert!(
+ enchanted > 30,
+ "only {enchanted} of 40 rolls were enchanted"
+ );
+ // `enchant_randomly` on a book makes one enchanted book, its levels
+ // stored; the pool is the enchantment tag it names.
+ let book = json!({"pools":[{"rolls":1,"entries":[{"type":"minecraft:item",
+ "name":"minecraft:book","modifier":[
+ {"type":"minecraft:enchant_randomly","options":"#minecraft:on_random_loot"}]}]}]});
+ let drops = ctx(&items, None, &props, "").evaluate(&book, &mut random);
+ assert_eq!(&*drops[0].item, "minecraft:enchanted_book");
+ assert_eq!(drops[0].count, 1);
+ assert!(drops[0].enchantments().is_empty());
+ let stored = drops[0].stored_enchantments();
+ assert_eq!(stored.len(), 1, "{stored:?}");
+ assert!(
+ items
+ .enchantments
+ .in_tag("minecraft:on_random_loot", &stored[0].0)
+ );
+ // An item no enchantment accepts keeps none of them.
+ let dirt = json!({"pools":[{"rolls":1,"entries":[{"type":"minecraft:item",
+ "name":"minecraft:dirt","modifier":[
+ {"type":"minecraft:enchant_with_levels","levels":{"type":"minecraft:constant","value":30.0}}]}]}]});
+ let drops = ctx(&items, None, &props, "").evaluate(&dirt, &mut random);
+ assert!(drops[0].enchantments().is_empty());
+ }
+
+ /// `Mth.nextInt(random, 1, max)` returns `1` without drawing when the
+ /// enchantment has one level (`EnchantRandomlyFunction.enchantItem`), so
+ /// the rolls after it must not shift. Vanilla with `LegacyRandom(11)`: a
+ /// Mending book, then `set_count` uniform 1..64, is 46.
+ #[test]
+ fn a_single_level_enchantment_draws_no_level() {
+ let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
+ .join("../../.local/horizon-client/26.3");
+ if !root.join("items.json").exists() {
+ return;
+ }
+ let assets = crate::assets::Assets::load(&root).unwrap();
+ let facts = crate::blocks::BlockFacts::load(&root, assets.registry).unwrap();
+ let items = Items::load(&root, assets.registry, &facts).unwrap();
+ let props = BTreeMap::new();
+ let table = json!({"pools":[{"rolls":1,"entries":[{"type":"minecraft:item",
+ "name":"minecraft:book","modifier":[
+ {"type":"minecraft:enchant_randomly","options":"minecraft:mending"},
+ {"type":"minecraft:set_count","count":{"type":"minecraft:uniform","min":1.0,"max":64.0}}]}]}]});
+ let drops = ctx(&items, None, &props, "").evaluate(&table, &mut LegacyRandom::new(11));
+ assert_eq!(drops.len(), 1);
+ assert_eq!(&*drops[0].item, "minecraft:enchanted_book");
+ assert_eq!(
+ drops[0].stored_enchantments(),
+ [("minecraft:mending".to_owned().into(), 1)],
+ "the book keeps its single-level enchantment"
+ );
+ // 46 is vanilla's count for this seed; an extra level draw gives 28.
+ assert_eq!(drops[0].count, 46);
+ }
+
#[test]
fn always_edible_and_consume_effects_loaded() {
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
diff --git a/crates/horizon-client/src/lan/chunks.rs b/crates/horizon-client/src/lan/chunks.rs
index d7dcc6cd8..122d37bcd 100644
--- a/crates/horizon-client/src/lan/chunks.rs
+++ b/crates/horizon-client/src/lan/chunks.rs
@@ -952,6 +952,11 @@ pub fn entity_tag(entity: &BlockEntity) -> Option {
Some(Tag::compound(entries))
}
BlockEntity::Jukebox(_) | BlockEntity::Lectern(_) => None,
+ // CampfireBlockEntity.getUpdateTag: the items alone.
+ BlockEntity::Campfire(c) => Some(Tag::compound([(
+ "Items",
+ crate::save::encode_items(&c.items),
+ )])),
_ => None,
}
}
diff --git a/crates/horizon-client/src/lan/conn.rs b/crates/horizon-client/src/lan/conn.rs
index fb91f3006..f2dcf701d 100644
--- a/crates/horizon-client/src/lan/conn.rs
+++ b/crates/horizon-client/src/lan/conn.rs
@@ -823,6 +823,17 @@ fn login_and_configure(
_ => {}
}
};
+ // A Horizon client hears that this server runs its mods' quiet commands
+ // (horizon_protocol::quiet); a vanilla one is told nothing.
+ if brand == "horizon" {
+ send(
+ c,
+ cfg::Clientbound::CustomPayload(cfg::CustomPayload {
+ channel: horizon_protocol::quiet::CHANNEL.into(),
+ data: Vec::new(),
+ }),
+ )?;
+ }
for packet in sync.registry_packets(known_pack) {
send(c, cfg::Clientbound::RegistryData(packet))?;
}
diff --git a/crates/horizon-client/src/lan/containers.rs b/crates/horizon-client/src/lan/containers.rs
index 7e150e7bd..002f95cbb 100644
--- a/crates/horizon-client/src/lan/containers.rs
+++ b/crates/horizon-client/src/lan/containers.rs
@@ -120,6 +120,21 @@ impl Hub {
);
}
+ /// `ServerPlayer.crit`: send the victim's animation to the players tracking
+ /// the attacker and the attacker themselves (`sendToTrackingPlayersAndSelf`).
+ pub fn critical_hit(&self, attacker: i32, target: i32) {
+ let body = C::Animate(play::Animate {
+ id: target,
+ action: 1,
+ })
+ .encode();
+ for g in self.guests.iter().filter(|g| {
+ self.here(g) && (g.entity_id == attacker || g.tracker.is_tracking(attacker))
+ }) {
+ g.sender.send(body.clone());
+ }
+ }
+
/// `ServerLevel.broadcastDamageEvent`: the clients around show the hurt
/// entity's flash and hear its hurt sound.
pub fn damage_event(
diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs
index aa1994257..7e43cf04e 100644
--- a/crates/horizon-client/src/lan/guest.rs
+++ b/crates/horizon-client/src/lan/guest.rs
@@ -237,6 +237,8 @@ pub struct Guest {
pub drag: Option<(u8, Vec)>,
/// The compound this player was loaded from, for fields Horizon does not model.
pub saved: Option,
+ /// `ServerPlayer.seenCredits`: the end poem has been shown.
+ pub seen_credits: bool,
/// `ServerPlayer.recipeBook`.
pub recipe_book: crate::recipe_book::Settings,
/// `PlayerAdvancements`: the criteria obtained, whether they came from
@@ -358,6 +360,7 @@ impl Guest {
next_window: 0,
drag: None,
saved: None,
+ seen_credits: false,
recipe_book: Default::default(),
advancements: Default::default(),
advancements_loaded: false,
@@ -601,6 +604,18 @@ fn network_stack(
horizon_protocol::item::Component::Stacks(charged),
));
}
+ // What a bundle holds, for its tooltip (ClientBundleTooltip).
+ if !d.bundle_contents.is_empty() {
+ let contents = d
+ .bundle_contents
+ .iter()
+ .filter_map(|c| network_stack(item_id, synced_id, &Some(c.clone())))
+ .collect();
+ stack.added.push((
+ horizon_protocol::item::component::BUNDLE_CONTENTS,
+ horizon_protocol::item::Component::Stacks(contents),
+ ));
+ }
// The books a lectern shows and a guest reads.
if !d.writable_pages.is_empty() {
stack.added.push((
@@ -633,6 +648,13 @@ fn network_stack(
horizon_protocol::item::Component::Text(horizon_protocol::text::literal(name)),
));
}
+ // The component as loaded, so a translatable name stays one.
+ if let Some(name) = s.data().and_then(|d| d.other("minecraft:item_name")) {
+ stack.added.push((
+ horizon_protocol::item::component::ITEM_NAME,
+ horizon_protocol::item::Component::Text(name.clone()),
+ ));
+ }
Some(stack)
}
diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs
index 910911d80..b1a8cbc62 100644
--- a/crates/horizon-client/src/lan/hub.rs
+++ b/crates/horizon-client/src/lan/hub.rs
@@ -128,6 +128,11 @@ pub enum HostEvent {
line: String,
secure: bool,
},
+ /// A chat message a player sent (after it was broadcast), for server mods.
+ PlayerChat {
+ sender: u128,
+ text: String,
+ },
/// A server log warning (ServerGamePacketListenerImpl's LOGGER.warn).
Warn(String),
/// A server log error (LOGGER.error).
@@ -371,9 +376,21 @@ impl Hub {
id
}
pub fn stack(&mut self, s: &Option) -> Option {
- let id = s.as_ref().map(|s| self.item_id(&s.item))?;
+ let item = s.as_ref()?.item.clone();
+ let id = self.item_id(&item);
let sync = self.sync.clone();
- stack_to_network(|_| id, |registry, name| sync.id(registry, name), s)
+ // The stacks inside it (a bundle's contents, a crossbow's projectiles)
+ // have ids of their own.
+ let item_id = |name: &str| {
+ if name == &*item {
+ return id;
+ }
+ sync.builtin
+ .get("minecraft:item")
+ .and_then(|i| i.id(name))
+ .unwrap_or(0)
+ };
+ stack_to_network(item_id, |registry, name| sync.id(registry, name), s)
}
pub fn broadcast(&self, packet: &C) {
diff --git a/crates/horizon-client/src/lan/hub/chat.rs b/crates/horizon-client/src/lan/hub/chat.rs
index bdc7c3fb2..591c0dfc5 100644
--- a/crates/horizon-client/src/lan/hub/chat.rs
+++ b/crates/horizon-client/src/lan/hub/chat.rs
@@ -219,6 +219,10 @@ impl Hub {
};
let line = format!("<{name}> {}", message.content);
self.broadcast_chat(&message, &bound, line, events);
+ events.push(HostEvent::PlayerChat {
+ sender: uuid,
+ text: message.content,
+ });
}
/// handleChatSessionUpdate: a new profile key, validated against the
diff --git a/crates/horizon-client/src/lan/menu.rs b/crates/horizon-client/src/lan/menu.rs
index 4185dadc4..859bafa23 100644
--- a/crates/horizon-client/src/lan/menu.rs
+++ b/crates/horizon-client/src/lan/menu.rs
@@ -37,6 +37,10 @@ pub struct Menu {
pub sent_data: Vec,
}
+/// The y of the key a chest minecart's or chest boat's items are kept under
+/// while a menu shows them.
+pub const VEHICLE_KEY_Y: i32 = -100_000;
+
/// Where the slots of a container menu live.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Slots {
@@ -72,10 +76,12 @@ impl Slots {
Slots::Ender => at.into_iter().collect(),
_ => self.halves(),
};
+ // A vehicle's container is kept under a key, not a block: the game
+ // checks its reach to the vehicle (`isChestVehicleStillValid`).
self.valid(world)
&& blocks
.iter()
- .all(|k| guest.within_block_range([k.0, k.1, k.2], 4.0))
+ .all(|k| k.1 == VEHICLE_KEY_Y || guest.within_block_range([k.0, k.1, k.2], 4.0))
}
/// Container slot `n`.
pub fn get(self, world: &World, guest: &Guest, mut n: usize) -> Option {
diff --git a/crates/horizon-client/src/lan/playerdata.rs b/crates/horizon-client/src/lan/playerdata.rs
index 90fe0bcfe..f5eb935c9 100644
--- a/crates/horizon-client/src/lan/playerdata.rs
+++ b/crates/horizon-client/src/lan/playerdata.rs
@@ -85,6 +85,7 @@ pub fn apply(guest: &mut Guest, level: &Level) {
.map(|s| [s[0] + 0.5, s[1], s[2] + 0.5]);
guest.respawn_elsewhere = level.respawn_elsewhere.clone();
guest.saved = level.player_extra.clone();
+ guest.seen_credits = level.seen_credits;
let extra = level.player_extra.as_ref();
let short = |key: &str, default: i64| {
extra
@@ -166,6 +167,7 @@ pub fn store(guest: &Guest, level: &mut Level) {
level.spawn = guest.spawn.map(|s| [s[0] - 0.5, s[1], s[2] - 0.5]);
level.respawn_elsewhere = guest.respawn_elsewhere.clone();
level.on_ground = guest.on_ground;
+ level.seen_credits = guest.seen_credits;
level.player_extra = guest.saved.clone();
// The live body's fire, air and fall distance (Entity.saveWithoutId),
// or what the file had while the player has no body yet.
diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs
index 232904b7a..fa5ef1c4d 100644
--- a/crates/horizon-client/src/lib.rs
+++ b/crates/horizon-client/src/lib.rs
@@ -46,6 +46,7 @@ pub mod cooldowns;
pub mod crafting;
pub mod crash;
pub mod creative;
+pub mod cushion;
pub mod data_cmd;
pub mod datapack;
pub mod debug_chart;
diff --git a/crates/horizon-client/src/lod.rs b/crates/horizon-client/src/lod.rs
index a429863a7..1ee61cdf4 100644
--- a/crates/horizon-client/src/lod.rs
+++ b/crates/horizon-client/src/lod.rs
@@ -752,6 +752,25 @@ impl Lod {
saved_order: VecDeque::new(),
}
}
+ /// The solid ground at block column (x, z): its top face's height and
+ /// its block, from the finest tile sampled there. For aiming past the
+ /// resident chunks (`game.raycast`); water is passed, as the view's ray
+ /// passes it.
+ pub fn ground(&self, x: i32, z: i32) -> Option<(i32, BlockStateId)> {
+ (0..=MAX_LEVEL).find_map(|level| {
+ let size = 16 << level;
+ let key = TileKey {
+ x: x.div_euclid(size),
+ z: z.div_euclid(size),
+ level,
+ };
+ let columns = self.ready.get(&key).filter(|c| c.len() == CELLS * CELLS)?;
+ let cell = size / CELLS as i32;
+ let (cx, cz) = ((x - key.x * size) / cell, (z - key.z * size) / cell);
+ let surface = columns[cz as usize * CELLS + cx as usize].surface?;
+ Some((surface.height, surface.state))
+ })
+ }
pub fn stats(&self) -> (usize, usize) {
(self.active.len(), self.ready.len())
}
diff --git a/crates/horizon-client/src/mesher.rs b/crates/horizon-client/src/mesher.rs
index fff3c6aa0..48d2f08bd 100644
--- a/crates/horizon-client/src/mesher.rs
+++ b/crates/horizon-client/src/mesher.rs
@@ -113,6 +113,14 @@ mod vertex_tests {
assert!((a - b).abs() <= 0.5 / 32768.0, "{a} {b}");
}
}
+ #[test]
+ fn emission_keeps_the_colour_and_the_free_bits() {
+ let rgb = super::pack_rgb([0x12, 0x34, 0x56]);
+ assert_eq!(super::emitting(rgb, 0), rgb);
+ assert_eq!(super::emitting(rgb, 9), 0xF656_3412);
+ assert_eq!(super::emitting(rgb, 15), 0xF056_3412);
+ assert_eq!(super::emitting(rgb, 15) | super::EMISSION_BITS, rgb);
+ }
}
pub struct SectionMesh {
pub solid: Vec,
@@ -566,6 +574,14 @@ impl Padded {
}
/// Set on a terrain vertex's sprite layer when its texture alpha must be ignored.
pub const OPAQUE_LAYER: u16 = 1 << 15;
+/// The bits of `TerrainVertex::color`'s alpha byte that hold the light its face emits, for
+/// HDR output's highlights (`gfx/hdr.rs`): 15 minus the level, so the usual 0xFF means none.
+/// The byte's other four bits stay set.
+pub const EMISSION_BITS: u32 = 0x0F00_0000;
+/// `color` marked as emitting light `level` (0 to 15).
+pub fn emitting(color: u32, level: u8) -> u32 {
+ color & !EMISSION_BITS | u32::from(15 - level.min(15)) << 24
+}
const UNSET: u32 = u32::MAX;
const NO_BIOME: u16 = u16::MAX;
/// The `ColorResolver`s `ClientLevel` caches block tints for.
@@ -880,7 +896,7 @@ impl Mesher<'_> {
} else {
face.layer as u16
},
- pack_rgb(rgb),
+ emitting(pack_rgb(rgb), state.emission.max(q.light_emission)),
pack_coords(lighting::with_emission(coords[i], q.light_emission)),
));
}
diff --git a/crates/horizon-client/src/mesher/fluid.rs b/crates/horizon-client/src/mesher/fluid.rs
index 6e42685ba..d30b914a4 100644
--- a/crates/horizon-client/src/mesher/fluid.rs
+++ b/crates/horizon-client/src/mesher/fluid.rs
@@ -227,8 +227,11 @@ pub(super) fn tesselate(
} else {
0xFF_FFFF
};
+ // Lava emits light level 15.
+ let emission = if water { 0 } else { 15 };
let color = |by: f32| {
- pack_rgb([16, 8, 0].map(|shift| lighting::scale((tint >> shift & 255) as i32, by) as u32))
+ let rgb = [16, 8, 0].map(|shift| lighting::scale((tint >> shift & 255) as i32, by) as u32);
+ crate::mesher::emitting(pack_rgb(rgb), emission)
};
let height = |p: [i32; 3]| -> f32 {
let s = at(p);
diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs
index 1b5dddbe7..00c21633e 100644
--- a/crates/horizon-client/src/mobs.rs
+++ b/crates/horizon-client/src/mobs.rs
@@ -709,6 +709,9 @@ pub struct Extra {
/// A copper golem's `openedChestPos`.
pub opened_chest: Option<[i32; 3]>,
pub point: Option<[f64; 3]>,
+ /// `NEAREST_REPELLENT`: the nearest repelling block a hoglin or piglin
+ /// sensed, looked up every 20 ticks.
+ pub repellent: Option<[i32; 3]>,
pub vector: [f64; 3],
/// Active status effects: (effect id, ticks left, amplifier).
pub effects: Vec<(&'static str, i32, i32)>,
@@ -868,6 +871,11 @@ pub struct Mob {
/// Events a mob tick asks the game to act on.
#[derive(Clone, Debug)]
pub enum MobEvent {
+ /// Squid.handleEntityEvent(19), resetting the remote tentacle cycle.
+ SquidCycle {
+ mob: u32,
+ at: [f64; 3],
+ },
/// Rabbit entity event 1, sent only when a ground jump actually happens.
RabbitHop {
mob: u32,
diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs
index c9bc15fda..2676c3a37 100644
--- a/crates/horizon-client/src/mobs/ai.rs
+++ b/crates/horizon-client/src/mobs/ai.rs
@@ -1024,6 +1024,10 @@ fn check_despawn(m: &mut Mob, w: &MobWorld) -> bool {
if m.species() == Species::Mannequin {
return true;
}
+ // Mob.checkDespawn discards disallowed types even when persistent or NoAI.
+ if w.difficulty.effective == 0.0 && !super::spawn_rules::allowed_in_peaceful(m.species()) {
+ return false;
+ }
let Some(p) = w.nearest_player else {
return true;
};
@@ -1238,6 +1242,20 @@ pub fn walk_target_value(m: &Mob, w: &MobWorld, p: [i32; 3]) -> f32 {
if m.species() == Species::Strider {
return super::strider::walk_target_value(m, w, p);
}
+ // Hoglin.getWalkTargetValue: nowhere near a repellent, crimson nylium best.
+ if m.species() == Species::Hoglin {
+ let near = m.extra.repellent.is_some_and(|r| {
+ let d = |i: usize| i64::from(r[i] - p[i]);
+ d(0) * d(0) + d(1) * d(1) + d(2) * d(2) < 64
+ });
+ return if near {
+ -1.0
+ } else if w.block_name(p[0], p[1] - 1, p[2]) == "minecraft:crimson_nylium" {
+ 10.0
+ } else {
+ 0.0
+ };
+ }
// Bee.getWalkTargetValue: any air block.
if m.species() == Species::Bee {
return if w.facts.is_air(w.world.block(p[0], p[1], p[2])) {
diff --git a/crates/horizon-client/src/mobs/anim.rs b/crates/horizon-client/src/mobs/anim.rs
index b2158132b..b81f224ae 100644
--- a/crates/horizon-client/src/mobs/anim.rs
+++ b/crates/horizon-client/src/mobs/anim.rs
@@ -102,7 +102,7 @@ impl AnimState {
pub fn end_tick(&mut self, _swinging: bool) {}
}
/// Part poses for one draw, keyed by part name.
-#[derive(Clone)]
+#[derive(Clone, Default)]
pub struct Poses {
map: HashMap,
dummy: [f32; 9],
@@ -113,20 +113,24 @@ pub struct Poses {
}
impl Poses {
pub fn new(model: &Part) -> Self {
- let mut map = HashMap::new();
+ let mut poses = Self::default();
+ poses.reset(model);
+ poses
+ }
+ /// Refills this for another model, keeping the map and the sets' capacity:
+ /// a mob draws its body and every equipment layer each frame.
+ pub fn reset(&mut self, model: &Part) {
fn walk(p: &Part, map: &mut HashMap) {
map.insert(p.name.clone(), p.pose);
for c in &p.children {
walk(c, map);
}
}
- walk(model, &mut map);
- Self {
- map,
- dummy: [0.0; 9],
- skipped: Default::default(),
- hidden: Default::default(),
- }
+ self.map.clear();
+ self.dummy = [0.0; 9];
+ self.skipped.clear();
+ self.hidden.clear();
+ walk(model, &mut self.map);
}
pub fn get(&mut self, name: &str) -> &mut [f32; 9] {
match self.map.get_mut(name) {
@@ -189,14 +193,15 @@ pub fn state(m: &Mob, alpha: f32) -> State {
swing += 1.0;
}
let swing = m.anim.attack_anim_o + swing * alpha;
- let alive = m.alive();
+ // LivingEntityRenderer.extractRenderState: riding motion is not walking.
+ let walking = m.alive() && !m.extra.passenger;
State {
- pos: if alive {
+ pos: if walking {
m.limb_position - m.limb_speed * (1.0 - alpha)
} else {
0.0
},
- speed: if alive {
+ speed: if walking {
(m.previous_limb_speed + (m.limb_speed - m.previous_limb_speed) * alpha).min(1.0)
} else {
0.0
diff --git a/crates/horizon-client/src/mobs/aquatic.rs b/crates/horizon-client/src/mobs/aquatic.rs
index dc80c6ea4..3f86d8edc 100644
--- a/crates/horizon-client/src/mobs/aquatic.rs
+++ b/crates/horizon-client/src/mobs/aquatic.rs
@@ -234,12 +234,40 @@ pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) {
}
/// Squid.aiStep: tentacle cycle, propulsion and body rotation.
fn squid_ai_step(m: &mut Mob) {
+ squid_step(m, false);
+}
+
+/// Squid.aiStep also runs on remote entities: animate from packet velocity,
+/// without running goals or replacing that velocity with the server's movement vector.
+pub fn squid_client_step(m: &mut Mob) {
+ if matches!(m.species(), Species::Squid | Species::GlowSquid) {
+ m.anim.vo = m.anim.v;
+ squid_step(m, true);
+ }
+}
+
+/// Squid.handleEntityEvent(19): begin the next server-synchronised tentacle cycle.
+pub fn squid_cycle_event(m: &mut Mob) {
+ if matches!(m.species(), Species::Squid | Species::GlowSquid) {
+ m.anim.v[TENTACLE_MOVEMENT] = 0.0;
+ }
+}
+
+fn squid_step(m: &mut Mob, client: bool) {
let v = &mut m.anim.v;
v[TENTACLE_MOVEMENT] += v[TENTACLE_SPEED];
- if v[TENTACLE_MOVEMENT] > PI * 2.0 {
- v[TENTACLE_MOVEMENT] -= PI * 2.0;
- if m.random.next_int_bound(10).unwrap_or(1) == 0 {
- m.anim.v[TENTACLE_SPEED] = 1.0 / (m.random.next_float() + 1.0) * 0.2;
+ if f64::from(v[TENTACLE_MOVEMENT]) > std::f64::consts::TAU {
+ if client {
+ v[TENTACLE_MOVEMENT] = PI * 2.0;
+ } else {
+ v[TENTACLE_MOVEMENT] -= PI * 2.0;
+ if m.random.next_int_bound(10).unwrap_or(1) == 0 {
+ m.anim.v[TENTACLE_SPEED] = 1.0 / (m.random.next_float() + 1.0) * 0.2;
+ }
+ m.outbox.push(MobEvent::SquidCycle {
+ mob: m.id,
+ at: [m.position.x, m.position.y, m.position.z],
+ });
}
}
let v = &mut m.anim.v;
@@ -248,28 +276,39 @@ fn squid_ai_step(m: &mut Mob) {
let scale = v[TENTACLE_MOVEMENT] / PI;
v[TENTACLE_ANGLE] = (scale * scale * PI).msin() * PI * 0.25;
if scale > 0.75 {
- let mv = m.extra.vector;
- m.velocity = Vec3::new(mv[0], mv[1], mv[2]);
+ if !client {
+ let mv = m.extra.vector;
+ m.velocity = Vec3::new(mv[0], mv[1], mv[2]);
+ }
m.anim.v[ROTATE_SPEED] = 1.0;
} else {
m.anim.v[ROTATE_SPEED] *= 0.8;
}
} else {
v[TENTACLE_ANGLE] = 0.0;
- m.velocity = Vec3::new(m.velocity.x * 0.9, m.velocity.y * 0.9, m.velocity.z * 0.9);
+ if !client {
+ m.velocity = Vec3::new(m.velocity.x * 0.9, m.velocity.y * 0.9, m.velocity.z * 0.9);
+ }
m.anim.v[ROTATE_SPEED] *= 0.99;
}
let mv = m.velocity;
let horizontal = (mv.x * mv.x + mv.z * mv.z).sqrt();
- m.body_yaw += (-(mv.x.atan2(mv.z) as f32).to_degrees() - m.body_yaw) * 0.1;
+ m.body_yaw += (-(horizon_core::geometry::trig::atan2(mv.x, mv.z) as f32) * 57.295776
+ - m.body_yaw)
+ * 0.1;
m.yaw = m.body_yaw;
let v = &mut m.anim.v;
v[Z_BODY_ROT] += PI * v[ROTATE_SPEED] * 1.5;
- v[X_BODY_ROT] += (-(horizontal.atan2(mv.y) as f32).to_degrees() - v[X_BODY_ROT]) * 0.1;
+ v[X_BODY_ROT] += (-(horizon_core::geometry::trig::atan2(horizontal, mv.y) as f32)
+ * 57.295776
+ - v[X_BODY_ROT])
+ * 0.1;
} else {
v[TENTACLE_ANGLE] = v[TENTACLE_MOVEMENT].msin().abs() * PI * 0.25;
- let yd = m.velocity.y - m.kind.gravity;
- m.velocity = Vec3::new(0.0, yd * 0.98, 0.0);
+ if !client {
+ let yd = m.velocity.y - m.kind.gravity;
+ m.velocity = Vec3::new(0.0, yd * 0.98, 0.0);
+ }
let v = &mut m.anim.v;
v[X_BODY_ROT] += (-90.0 - v[X_BODY_ROT]) * 0.02;
}
diff --git a/crates/horizon-client/src/mobs/bat.rs b/crates/horizon-client/src/mobs/bat.rs
index 9f38e9e60..ca5efa4a2 100644
--- a/crates/horizon-client/src/mobs/bat.rs
+++ b/crates/horizon-client/src/mobs/bat.rs
@@ -132,6 +132,10 @@ pub fn tick(m: &mut Mob) {
} else {
m.velocity.y *= 0.6;
}
+ setup_animation_states(m);
+}
+/// `Bat.setupAnimationStates`: client visuals, without changing server motion.
+pub fn setup_animation_states(m: &mut Mob) {
let age = m.age as f32;
if resting(m) {
m.anim.states[0].stop();
diff --git a/crates/horizon-client/src/mobs/enderman.rs b/crates/horizon-client/src/mobs/enderman.rs
index fcfba832e..c5ed01680 100644
--- a/crates/horizon-client/src/mobs/enderman.rs
+++ b/crates/horizon-client/src/mobs/enderman.rs
@@ -138,9 +138,10 @@ fn teleport_towards(
let z = m.position.z + (m.random.next_double() - 0.5) * 8.0 - d[2] * 16.0;
teleport_to(m, w, x, y, z, events)
}
-/// Enderman.customServerAiStep: in daylight, flee by teleporting.
+/// Enderman.customServerAiStep: in daylight, flee by teleporting. The End has a
+/// fixed time, so `Level.isBrightOutside` is never true there.
pub fn custom_server_ai_step(m: &mut Mob, cx: &mut Cx) {
- if cx.w.daylight && m.age as i32 >= m.extra.counter + 600 {
+ if cx.w.daylight && !cx.w.end && m.age as i32 >= m.extra.counter + 600 {
let br = super::hostile::mob_brightness(m, cx.w);
let (x, y, z) = (
m.position.x.floor() as i32,
diff --git a/crates/horizon-client/src/mobs/guardian.rs b/crates/horizon-client/src/mobs/guardian.rs
index d6219794d..9704de044 100644
--- a/crates/horizon-client/src/mobs/guardian.rs
+++ b/crates/horizon-client/src/mobs/guardian.rs
@@ -241,9 +241,7 @@ pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) {
} else {
a.v[SPIKES] += (1.0 - a.v[SPIKES]) * 0.06;
}
- if m.extra.flag && m.extra.counter2 < attack_duration(m) {
- m.extra.counter2 += 1;
- }
+ client_tick(m);
if in_water {
m.air = 300;
} else if m.on_ground {
@@ -332,6 +330,13 @@ pub fn elder_server_ai_step(m: &mut Mob, cx: &mut Cx) {
]);
}
}
+/// Guardian.aiStep's visual attack clock, also used by server clients.
+/// Does not run attack goals, movement, damage or random draws.
+pub fn client_tick(m: &mut Mob) {
+ if m.extra.flag && m.extra.counter2 < attack_duration(m) {
+ m.extra.counter2 += 1;
+ }
+}
/// The beam's clock for rendering: (attack time with the partial tick,
/// attack scale), while a target is locked.
pub fn beam(m: &Mob, partial: f32) -> Option<(f32, f32)> {
diff --git a/crates/horizon-client/src/mobs/piglin.rs b/crates/horizon-client/src/mobs/piglin.rs
index 410364097..fff536932 100644
--- a/crates/horizon-client/src/mobs/piglin.rs
+++ b/crates/horizon-client/src/mobs/piglin.rs
@@ -9,7 +9,7 @@ use super::{
NearestTarget, Prey, RandomLookAround, Stroll, goal_clone,
},
};
-use horizon_core::geometry::trig::Mth;
+use horizon_core::geometry::{Vec3, trig::Mth};
use horizon_core::random::RandomSource;
use std::f32::consts::PI;
@@ -83,12 +83,16 @@ pub fn register_hoglin(m: &mut Mob) {
g.add(1, BabyPanic(super::goals::Panic::new(1.25)));
g.add(2, MeleeAttack::new(1.0, true));
g.add(3, super::goals::Breed::new(1.0));
+ g.add(3, HoglinAvoidRepellent);
g.add(4, super::goals::FollowParent::new(1.1));
g.add(5, Stroll::avoiding_water(0.4));
g.add(6, LookAtPlayer::new(8.0));
m.ai.targets.add(1, HurtByTarget::new().alert());
- m.ai.targets
- .add(2, NearestTarget::player(true).when(|m, _| !m.is_baby()));
+ // `HoglinAi.findNearestValidAttackTarget`: nothing while pacified.
+ m.ai.targets.add(
+ 2,
+ NearestTarget::player(true).when(|m, _| !m.is_baby() && m.extra.ints[0] <= 0),
+ );
}
pub fn register_zoglin(m: &mut Mob) {
let g = &mut m.ai.goals;
@@ -108,6 +112,49 @@ pub fn register_zoglin(m: &mut Mob) {
.interval(10),
);
}
+/// `SetWalkTargetAwayFrom.pos(NEAREST_REPELLENT, 1.0, 8, true)` of the hoglin's
+/// idle activity: closer than 8 blocks to a repellent, walk away from it.
+#[derive(Clone, Debug)]
+struct HoglinAvoidRepellent;
+impl Goal for HoglinAvoidRepellent {
+ goal_clone!();
+ fn flags(&self) -> u8 {
+ MOVE
+ }
+ fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
+ let Some(r) = m.extra.repellent else {
+ return false;
+ };
+ // Vec3.atBottomCenterOf.
+ let avoid = [
+ f64::from(r[0]) + 0.5,
+ f64::from(r[1]),
+ f64::from(r[2]) + 0.5,
+ ];
+ if m.distance_sqr(Vec3::new(avoid[0], avoid[1], avoid[2])) >= 64.0 {
+ return false;
+ }
+ for _ in 0..10 {
+ if let Some(p) = m.land_random_pos_away(cx.w, 16, 7, avoid) {
+ let at = [
+ p[0].floor() as i32,
+ p[1].floor() as i32,
+ p[2].floor() as i32,
+ ];
+ m.ai.pending_path = m.create_path_to(cx.w, at, 0);
+ return m.ai.pending_path.is_some();
+ }
+ }
+ false
+ }
+ fn can_continue(&mut self, m: &mut Mob, _: &mut Cx) -> bool {
+ !m.ai.nav.is_done()
+ }
+ fn start(&mut self, m: &mut Mob, _: &mut Cx) {
+ let path = m.ai.pending_path.take();
+ m.nav_move_to_path(path, 1.0);
+ }
+}
/// Baby hoglins flee what hurts them; adults stand their ground.
#[derive(Clone, Debug)]
struct BabyPanic(super::goals::Panic);
@@ -165,12 +212,45 @@ impl Goal for GoToWantedItem {
m.extra.point = None;
}
}
+/// `HoglinSpecificSensor.findNearestRepellent`: the nearest `#hoglin_repellents`
+/// block within 8 blocks sideways and 4 up or down.
+fn find_hoglin_repellent(m: &Mob, w: &MobWorld) -> Option<[i32; 3]> {
+ let tag = w.block_tags.get("minecraft:hoglin_repellents")?;
+ let at = [
+ m.position.x.floor() as i32,
+ m.position.y.floor() as i32,
+ m.position.z.floor() as i32,
+ ];
+ w.nearest_block(at, 8, 4, |_, name| tag.contains(name))
+}
+/// The hoglin's repellent memories: the sensor (every 20 ticks), the end of
+/// `PACIFIED` (`ints[0]`), a hit ending it (`HoglinAi.wasHurtBy`, `ints[1]`
+/// remembers the hit seen) and `BecomePassiveIfMemoryPresent(NEAREST_REPELLENT, 200)`.
+fn hoglin_repellent(m: &mut Mob, w: &MobWorld) {
+ if m.extra.ints[0] > 0 {
+ m.extra.ints[0] -= 1;
+ }
+ if m.ai.last_hurt_by.is_some() && m.ai.last_hurt_by_time != m.extra.ints[1] {
+ m.extra.ints[1] = m.ai.last_hurt_by_time;
+ m.extra.ints[0] = 0;
+ }
+ if m.age.wrapping_add(m.id).is_multiple_of(20) {
+ m.extra.repellent = find_hoglin_repellent(m, w);
+ }
+ if m.extra.ints[0] <= 0 && m.extra.repellent.is_some() {
+ m.extra.ints[0] = 200;
+ m.ai.target = None;
+ }
+}
/// Per-tick piglin and hoglin state: admiring, anger, zombification and the
/// hoglin attack animation. Returns a conversion when it happens.
pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) {
if m.extra.anim_ticks > 0 {
m.extra.anim_ticks -= 1;
}
+ if m.species() == Species::Hoglin && m.alive() {
+ hoglin_repellent(m, w);
+ }
if m.extra.anger > 0 {
m.extra.anger -= 1;
if m.extra.anger == 0 && m.ai.target == Some(Who::Player) && !hostile_to_player(m, w) {
diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs
index bbbf26738..077fd38a6 100644
--- a/crates/horizon-client/src/mobs/render.rs
+++ b/crates/horizon-client/src/mobs/render.rs
@@ -951,21 +951,25 @@ fn draw_normals(
return;
}
let normals = normals.part(p);
- for poly in part.polygons.iter().filter(|_| !poses.skips(part)) {
- let normal = poly.vertex_normal(normals);
- for v in poly.vertices {
- let pos = m.transform_point3(glam::Vec3::new(v[0], v[1], v[2]) / 16.0);
- out.push(Vertex {
- normal,
- pos: pos.to_array(),
- uv: [
- uv[0] + (uv[2] - uv[0]) * v[3],
- uv[1] + (uv[3] - uv[1]) * v[4],
- ],
- layer: overlay,
- color,
- light,
- });
+ // `skips` is a name lookup: ask once per part, not once per polygon. A part
+ // hidden this way still draws its children, so only the polygons are gated.
+ if !poses.skips(part) {
+ for poly in &part.polygons {
+ let normal = poly.vertex_normal(normals);
+ for v in poly.vertices {
+ let pos = m.transform_point3(glam::Vec3::new(v[0], v[1], v[2]) / 16.0);
+ out.push(Vertex {
+ normal,
+ pos: pos.to_array(),
+ uv: [
+ uv[0] + (uv[2] - uv[0]) * v[3],
+ uv[1] + (uv[3] - uv[1]) * v[4],
+ ],
+ layer: overlay,
+ color,
+ light,
+ });
+ }
}
}
for c in &part.children {
@@ -1094,6 +1098,9 @@ fn render_visible(
}
}
}
+ // One Poses refilled for every layer (armour draws up to seven): it keeps
+ // its maps' capacity.
+ let mut layer_poses = Poses::default();
for layer in all_layers {
let Some(part) = m.kind.layer(&layer.model).cloned() else {
continue;
@@ -1101,8 +1108,8 @@ fn render_visible(
let Some(uv) = atlas(&layer.texture) else {
continue;
};
- let mut poses = Poses::new(&part);
- anim::setup(m, &s, &mut poses, &data.animations);
+ layer_poses.reset(&part);
+ anim::setup(m, &s, &mut layer_poses, &data.animations);
if matches!(m.species(), Species::Villager | Species::ZombieVillager)
&& let Some((folder, variant)) = layer
.texture
@@ -1113,7 +1120,7 @@ fn render_visible(
data.villager_hats
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
- .apply(folder, variant, profession, &mut poses);
+ .apply(folder, variant, profession, &mut layer_poses);
}
let light = if layer.full_bright {
240 | 240 << 8
@@ -1128,7 +1135,16 @@ fn render_visible(
None => overlay,
};
let start = out.len();
- draw(&part, base, &poses, uv, layer.color, light, overlay, out);
+ draw(
+ &part,
+ base,
+ &layer_poses,
+ uv,
+ layer.color,
+ light,
+ overlay,
+ out,
+ );
if layer.full_bright && m.species() != Species::Warden {
for vertex in &mut out[start..] {
vertex.normal = [0; 4];
diff --git a/crates/horizon-client/src/mobs/villager.rs b/crates/horizon-client/src/mobs/villager.rs
index 575fafaed..148034323 100644
--- a/crates/horizon-client/src/mobs/villager.rs
+++ b/crates/horizon-client/src/mobs/villager.rs
@@ -284,12 +284,14 @@ pub fn xp(m: &Mob) -> i32 {
m.extra.trade.as_ref().map_or(0, |t| t.xp)
}
/// `Villager.setVillagerData(withProfession)`: a new profession forgets the
-/// offers.
+/// offers (`offers = null`), so the next `getOffers` rolls the new
+/// profession's trades.
pub fn set_profession(m: &mut Mob, profession: u8) {
if m.extra.variant2 != profession {
m.extra.variant2 = profession;
if let Some(t) = m.extra.trade.as_mut() {
t.offers.clear();
+ t.unset = true;
}
}
}
@@ -846,6 +848,70 @@ pub fn read_nbt(m: &mut Mob, tag: &horizon_world::nbt::Tag) {
mod tests {
use super::*;
+ fn villager() -> Mob {
+ use crate::mobs::{Anim, Brain, Flags, MobType, Part, Species};
+ use std::{collections::HashMap, sync::Arc};
+ let kind = Arc::new(MobType {
+ id: "minecraft:villager".into(),
+ species: Species::Villager,
+ width: 0.6,
+ height: 1.95,
+ eye_height: 1.62,
+ category: "misc".into(),
+ max_health: 20.0,
+ speed: 0.5,
+ flying_speed: 0.02,
+ attack_damage: 0.0,
+ attack_knockback: 0.0,
+ follow_range: 48.0,
+ step_height: 0.6,
+ armor: 0.0,
+ armor_toughness: 0.0,
+ knockback_resistance: 0.0,
+ jump_strength: 0.42,
+ attributes: Arc::default(),
+ gravity: 0.08,
+ safe_fall_distance: 3.0,
+ fall_damage_multiplier: 1.0,
+ water_efficiency: 0.0,
+ water_slowdown: 0.8,
+ air_speed: 0.02,
+ baby_scale: 0.5,
+ head_rot_speed: 10.0,
+ max_head_x_rot: 40.0,
+ max_head_y_rot: 75.0,
+ ambient_interval: 80,
+ flags: Flags::default(),
+ model: Arc::new(Part::parse("root", &serde_json::json!({}))),
+ layers: HashMap::new(),
+ texture: String::new(),
+ brain: Brain::Passive,
+ anim: Anim::Villager,
+ sounds: [""; 3],
+ step_sound: None,
+ food: Arc::default(),
+ tempt: Arc::default(),
+ });
+ Mob::new(1, kind, horizon_core::geometry::Vec3::ZERO, 0.0)
+ }
+ #[test]
+ fn a_new_profession_rolls_new_offers() {
+ let mut m = villager();
+ set_profession(&mut m, 1);
+ // Offers made for the first profession.
+ m.extra.trade.get_or_insert_default().unset = false;
+ // The same profession keeps them.
+ set_profession(&mut m, 1);
+ assert!(!m.extra.trade.as_ref().unwrap().unset);
+ // A new one forgets them (offers = null), so the next getOffers
+ // (ensure_offers) rolls the new profession's trades.
+ set_profession(&mut m, 2);
+ let t = m.extra.trade.as_ref().unwrap();
+ assert!(t.offers.is_empty());
+ assert!(t.unset);
+ assert_eq!(m.extra.variant2, 2);
+ }
+
fn stack(item: &str, count: u32) -> ItemStack {
ItemStack::new(item, count)
}
diff --git a/crates/horizon-client/src/mobs/world.rs b/crates/horizon-client/src/mobs/world.rs
index 7836a49ef..aa665decc 100644
--- a/crates/horizon-client/src/mobs/world.rs
+++ b/crates/horizon-client/src/mobs/world.rs
@@ -65,6 +65,35 @@ impl MobWorld<'_> {
pub fn has_tag(&self, tag: &str, block: &str) -> bool {
self.block_tags.get(tag).is_some_and(|s| s.contains(block))
}
+ /// `findBlocksInBoxByManhattanDistance(at, range_xz, range_y).filterState(..).findFirst()`:
+ /// the first block `matches` accepts (given its position and name), going
+ /// out from `at` by Manhattan distance as `BlockPos.withinBoxByManhattanDistance` does.
+ pub fn nearest_block(
+ &self,
+ at: [i32; 3],
+ range_xz: i32,
+ range_y: i32,
+ matches: impl Fn([i32; 3], &str) -> bool,
+ ) -> Option<[i32; 3]> {
+ // Visiting order: distance, then x, then y, then +z before -z.
+ let mut best: Option<((i32, i32, i32, bool), [i32; 3])> = None;
+ for dx in -range_xz..=range_xz {
+ for dy in -range_y..=range_y {
+ for dz in -range_xz..=range_xz {
+ let key = (dx.abs() + dy.abs() + dz.abs(), dx, dy, dz < 0);
+ if best.is_some_and(|(k, _)| k <= key) {
+ continue;
+ }
+ let p = [at[0] + dx, at[1] + dy, at[2] + dz];
+ let state = self.world.block(p[0], p[1], p[2]);
+ if !self.facts.is_air(state) && matches(p, &self.facts.get(state).name) {
+ best = Some((key, p));
+ }
+ }
+ }
+ }
+ best.map(|(_, p)| p)
+ }
/// Level.clip with COLLIDER shapes (optionally stopping at any fluid): true on a miss.
pub fn clear_between(&self, from: Vec3, to: Vec3, fluids_block: bool) -> bool {
let mut scene = SceneCollisions {
diff --git a/crates/horizon-client/src/multiplayer.rs b/crates/horizon-client/src/multiplayer.rs
index 07d8552ff..39ea26502 100644
--- a/crates/horizon-client/src/multiplayer.rs
+++ b/crates/horizon-client/src/multiplayer.rs
@@ -11,9 +11,15 @@ use std::{
collections::HashMap,
path::{Path, PathBuf},
sync::{Arc, Mutex},
- time::Duration,
+ time::{Duration, Instant},
};
+const SERVER_NAME: crate::clipboard::FieldId = ("server.name", 0);
+const SERVER_ADDRESS: crate::clipboard::FieldId = ("server.address", 0);
+const SERVER_DIRECT: crate::clipboard::FieldId = ("server.direct", 0);
+const FRIEND_CODE: crate::clipboard::FieldId = ("friend.code", 0);
+const FRIEND_RELAY: crate::clipboard::FieldId = ("friend.relay", 0);
+
/// The result of pinging one address.
#[derive(Clone, Debug)]
pub enum Ping {
@@ -491,7 +497,7 @@ impl Screen {
w / 2.0 - 100.0,
h / 4.0 + 96.0 + 18.0,
200.0,
- "Done",
+ &crate::lang::get_or("gui.done", "Done"),
servers::parse_address(address).is_some(),
50,
),
@@ -499,7 +505,7 @@ impl Screen {
w / 2.0 - 100.0,
h / 4.0 + 120.0 + 18.0,
200.0,
- "Cancel",
+ &crate::lang::get_or("gui.cancel", "Cancel"),
true,
51,
),
@@ -532,7 +538,7 @@ impl Screen {
w / 2.0 - 100.0,
h / 4.0 + 96.0 + 12.0,
200.0,
- "Join Server",
+ &crate::lang::get_or("selectServer.select", "Join Server"),
servers::parse_address(address).is_some(),
52,
),
@@ -540,7 +546,7 @@ impl Screen {
w / 2.0 - 100.0,
h / 4.0 + 120.0 + 12.0,
200.0,
- "Cancel",
+ &crate::lang::get_or("gui.cancel", "Cancel"),
true,
53,
),
@@ -558,7 +564,7 @@ impl Screen {
w / 2.0 - 100.0,
h / 4.0 + 120.0 + 18.0,
200.0,
- "Cancel",
+ &crate::lang::get_or("gui.cancel", "Cancel"),
true,
57,
),
@@ -568,7 +574,7 @@ impl Screen {
w / 2.0 - 100.0,
h / 2.0 + 30.0,
200.0,
- "Back to Server List",
+ &crate::lang::get_or("gui.toMenu", "Back to Server List"),
true,
54,
)];
@@ -587,18 +593,21 @@ impl Screen {
}
}
/// An EditBox and its label: the label at `label_y` (one pixel in, in
- /// 0xA0A0A0), the 200x20 box at `box_y`.
+ /// 0xA0A0A0), the 200x20 box at `box_y`. The box is the same `EditBox`
+ /// the world screens edit (`Caret`): cursor, selection and scrolling.
fn field(
atlas: &HudAtlas,
batch: &mut HudBatch,
+ caret: &mut crate::ui::Caret,
+ focused_at: Instant,
label: &str,
value: &str,
+ id: crate::clipboard::FieldId,
+ max: usize,
(label_y, box_y): (f32, f32),
focused: bool,
- blink: bool,
) {
let x = (batch.width.ceil() / 2.0).floor() - 100.0;
- crate::widgets::target_as([x, box_y, 200.0, 20.0], crate::widgets::Kind::Field);
batch.text(
atlas,
label,
@@ -607,35 +616,25 @@ impl Screen {
rgba(160, 160, 160, 255),
true,
);
- batch.fill(
+ caret.draw(
+ batch,
atlas,
- x,
- box_y,
- 200.0,
- 20.0,
- if focused {
- u32::MAX
- } else {
- rgba(160, 160, 160, 255)
- },
- );
- batch.fill(atlas, x + 1.0, box_y + 1.0, 198.0, 18.0, rgba(0, 0, 0, 255));
- let shown = if focused && blink {
- format!("{value}_")
- } else {
- value.to_owned()
- };
- batch.text(
- atlas,
- &shown,
- x + 4.0,
- box_y + 6.0,
- rgba(224, 224, 224, 255),
- true,
+ (id, value, max),
+ [x, box_y, 200.0, 20.0],
+ focused,
+ focused_at,
);
}
- pub fn draw(&self, atlas: &HudAtlas, batch: &mut HudBatch, blink: bool, cursor: (f32, f32)) {
+ pub fn draw(
+ &self,
+ atlas: &HudAtlas,
+ batch: &mut HudBatch,
+ caret: &mut crate::ui::Caret,
+ focused_at: Instant,
+ cursor: (f32, f32),
+ ) {
let (w, h) = (batch.width, batch.height);
+ let max = self.max_length();
crate::ui::menu_background(batch, atlas, 0.0, 0.0, w, h, false);
let centered = |batch: &mut HudBatch, text: &str, y: f32, color: u32| {
let tw = HudBatch::text_width(atlas, text);
@@ -876,10 +875,10 @@ impl Screen {
} => {
centered(
batch,
- if index.is_some() {
- "Edit Server Info"
+ &if index.is_some() {
+ crate::lang::get_or("manageServer.edit.title", "Edit Server Info")
} else {
- "Add Server"
+ crate::lang::get_or("manageServer.add.title", "Add Server")
},
17.0,
u32::MAX,
@@ -888,20 +887,26 @@ impl Screen {
Self::field(
atlas,
batch,
- "Server Name",
+ caret,
+ focused_at,
+ &crate::lang::get_or("manageServer.enterName", "Server Name"),
name,
+ SERVER_NAME,
+ max,
(53.0, 66.0),
*focus == 0,
- blink,
);
Self::field(
atlas,
batch,
- "Server Address",
+ caret,
+ focused_at,
+ &crate::lang::get_or("manageServer.enterIp", "Server Address"),
address,
+ SERVER_ADDRESS,
+ max,
(94.0, 106.0),
*focus == 1,
- blink,
);
}
Self::Direct { address } => {
@@ -916,11 +921,14 @@ impl Screen {
Self::field(
atlas,
batch,
- &crate::lang::get_or("addServer.enterIp", "Server Address"),
+ caret,
+ focused_at,
+ &crate::lang::get_or("manageServer.enterIp", "Server Address"),
address,
+ SERVER_DIRECT,
+ max,
(100.0, 116.0),
true,
- blink,
);
}
Self::ConfirmDelete { name, lines, .. } => {
@@ -954,26 +962,32 @@ impl Screen {
Self::field(
atlas,
batch,
+ caret,
+ focused_at,
"Invite Code",
code,
+ FRIEND_CODE,
+ max,
(53.0, 64.0),
*focus == 0,
- blink,
);
Self::field(
atlas,
batch,
+ caret,
+ focused_at,
"Relay (optional)",
relay,
+ FRIEND_RELAY,
+ max,
(94.0, 105.0),
*focus == 1,
- blink,
);
}
Self::Disconnected { reason, retry } => {
centered(
batch,
- "Connection Lost",
+ &crate::lang::get_or("disconnect.lost", "Connection Lost"),
h / 2.0 - 40.0,
rgba(170, 170, 170, 255),
);
@@ -1019,7 +1033,7 @@ impl Screen {
(Self::List { .. }, Some(42)) => {
*self = Self::Edit {
index: None,
- name: "Minecraft Server".into(),
+ name: crate::lang::get_or("selectServer.defaultName", "Minecraft Server"),
address: String::new(),
focus: 1,
packs: None,
@@ -1261,6 +1275,19 @@ impl Screen {
scroll.release();
}
}
+ /// The focused box, in GUI pixels: name at 66, address at 106
+ /// (`ManageServerScreen`), direct address at 116, friend fields at 64 and 105.
+ pub fn focused_box(&self, (w, _h): (f32, f32)) -> Option<[f32; 4]> {
+ let y = match self {
+ Self::Edit { focus: 0, .. } => 66.0,
+ Self::Edit { .. } => 106.0,
+ Self::Direct { .. } => 116.0,
+ Self::Friend { focus: 0, .. } => 64.0,
+ Self::Friend { .. } => 105.0,
+ _ => return None,
+ };
+ Some([(w / 2.0).floor() - 100.0, y, 200.0, 20.0])
+ }
fn focused_field(&mut self) -> Option<&mut String> {
match self {
Self::Edit {
@@ -1297,11 +1324,11 @@ impl Screen {
/// The focused text field, its id and maximum length (copy and paste).
pub fn focused_text(&mut self) -> Option<(crate::clipboard::FieldId, &mut String, usize)> {
let id = match self {
- Self::Edit { focus: 0, .. } => ("server.name", 0),
- Self::Edit { .. } => ("server.address", 0),
- Self::Direct { .. } => ("server.direct", 0),
- Self::Friend { focus: 0, .. } => ("friend.code", 0),
- Self::Friend { .. } => ("friend.relay", 0),
+ Self::Edit { focus: 0, .. } => SERVER_NAME,
+ Self::Edit { .. } => SERVER_ADDRESS,
+ Self::Direct { .. } => SERVER_DIRECT,
+ Self::Friend { focus: 0, .. } => FRIEND_CODE,
+ Self::Friend { .. } => FRIEND_RELAY,
_ => return None,
};
let max = self.max_length();
@@ -1315,21 +1342,6 @@ impl Screen {
128
}
}
- pub fn type_text(&mut self, text: &str) {
- let max = self.max_length();
- if let Some(field) = self.focused_field() {
- for c in text.chars().filter(|c| !c.is_control()) {
- if field.chars().count() < max {
- field.push(c);
- }
- }
- }
- }
- pub fn backspace(&mut self) {
- if let Some(field) = self.focused_field() {
- field.pop();
- }
- }
pub fn tab(&mut self) {
if let Self::Edit { focus, .. } | Self::Friend { focus, .. } = self {
*focus = (*focus + 1) % 2;
diff --git a/crates/horizon-client/src/net.rs b/crates/horizon-client/src/net.rs
index 0eb260477..34daaf85c 100644
--- a/crates/horizon-client/src/net.rs
+++ b/crates/horizon-client/src/net.rs
@@ -40,6 +40,8 @@ pub enum Event {
Status(String),
/// The server's `minecraft:brand` custom payload.
Brand(String),
+ /// The server runs mods' quiet commands (`horizon_protocol::quiet`).
+ QuietCommands,
/// Configuration finished: the play state begins with these registries.
Joined {
registries: Arc,
@@ -668,6 +670,8 @@ fn receive_brand(payload: &cfg::CustomPayload, send: &Sender) {
&& let Ok(brand) = horizon_protocol::buf::Reader::new(&payload.data).string(32767)
{
let _ = send.send(Event::Brand(brand));
+ } else if payload.channel == horizon_protocol::quiet::CHANNEL {
+ let _ = send.send(Event::QuietCommands);
}
}
diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs
index 6f4082ec3..995fcb1f6 100644
--- a/crates/horizon-client/src/save.rs
+++ b/crates/horizon-client/src/save.rs
@@ -36,7 +36,7 @@ use horizon_world::{
worldgen::terrain::TerrainBlockStates,
};
use std::{
- collections::{BTreeMap, HashMap},
+ collections::{BTreeMap, HashMap, HashSet},
fs,
path::{Path, PathBuf},
sync::{
@@ -247,6 +247,44 @@ pub struct SaveTables {
block_entity: Vec,
heightmaps: crate::assets::Properties,
air: u32,
+ /// The POI type and its max tickets of each state (`PoiTypes`).
+ poi: Vec>,
+}
+/// `PoiTypes.register`: the POI type of a block state and its max tickets.
+pub fn poi_type(state: &horizon_world::chunk::BlockState) -> Option<(&'static str, i32)> {
+ let name = state.name.strip_prefix("minecraft:")?;
+ Some(match name {
+ "blast_furnace" => ("minecraft:armorer", 1),
+ "smoker" => ("minecraft:butcher", 1),
+ "cartography_table" => ("minecraft:cartographer", 1),
+ "brewing_stand" => ("minecraft:cleric", 1),
+ "composter" => ("minecraft:farmer", 1),
+ "barrel" => ("minecraft:fisherman", 1),
+ "fletching_table" => ("minecraft:fletcher", 1),
+ "cauldron" | "lava_cauldron" | "water_cauldron" | "powder_snow_cauldron" => {
+ ("minecraft:leatherworker", 1)
+ }
+ "lectern" => ("minecraft:librarian", 1),
+ "stonecutter" => ("minecraft:mason", 1),
+ "loom" => ("minecraft:shepherd", 1),
+ "smithing_table" => ("minecraft:toolsmith", 1),
+ "grindstone" => ("minecraft:weaponsmith", 1),
+ "bell" => ("minecraft:meeting", 32),
+ "beehive" => ("minecraft:beehive", 0),
+ "bee_nest" => ("minecraft:bee_nest", 0),
+ "nether_portal" => ("minecraft:nether_portal", 0),
+ "lodestone" => ("minecraft:lodestone", 0),
+ "test_instance_block" => ("minecraft:test_instance", 0),
+ // PoiTypes.BEDS is the sixteen colours: no straw bed.
+ bed if bed.ends_with("_bed") && bed != "straw_bed" => {
+ if state.properties.get("part").map(String::as_str) != Some("head") {
+ return None;
+ }
+ ("minecraft:home", 1)
+ }
+ rod if rod.ends_with("lightning_rod") => ("minecraft:lightning_rod", 0),
+ _ => return None,
+ })
}
impl SaveTables {
pub fn new(assets: &crate::assets::Assets) -> Self {
@@ -275,6 +313,9 @@ impl SaveTables {
block_entity: Vec::new(),
heightmaps: assets.properties.clone(),
air: registry.default_state("minecraft:air").map_or(0, |s| s.0),
+ poi: (0..registry.len())
+ .map(|id| registry.state(BlockStateId(id as u32)).and_then(poi_type))
+ .collect(),
}
}
/// Tables with only the heightmaps' state facts, for tests.
@@ -287,6 +328,7 @@ impl SaveTables {
block_entity: Vec::new(),
heightmaps,
air: 0,
+ poi: Vec::new(),
}
}
/// Which states carry a block entity (`BlockFacts`), to drop entries for
@@ -297,6 +339,35 @@ impl SaveTables {
}
}
+/// A container's loot table and what Horizon rolled from it. Vanilla keeps
+/// `LootTable` and `LootTableSeed` until the container is first opened;
+/// Horizon rolls when it generates or loads the chunk, and saves the table
+/// again while the container still holds exactly the rolled items.
+#[derive(Clone, Debug, PartialEq)]
+pub struct RolledLoot {
+ pub pos: (i32, i32, i32),
+ pub table: String,
+ /// The saved `LootTableSeed`, written back unless 0 (trySaveLootTable).
+ pub seed: Option,
+ /// The rolled `Items`.
+ pub items: Tag,
+}
+impl RolledLoot {
+ /// Puts the loot table back in place of `Items` while they are still the
+ /// roll; false once the container holds anything else.
+ pub fn restore(&self, map: &mut Compound) -> bool {
+ if map.get(&"Items".into()) != Some(&self.items) {
+ return false;
+ }
+ map.remove(&"Items".into());
+ map.insert("LootTable".into(), string(&self.table));
+ if let Some(seed) = self.seed.filter(|s| *s != 0) {
+ map.insert("LootTableSeed".into(), Tag::Long(seed));
+ }
+ true
+ }
+}
+
/// Save data loaded with a chunk that Horizon keeps but does not simulate.
#[derive(Clone, Debug, Default)]
pub struct ChunkExtras {
@@ -318,6 +389,8 @@ pub struct ChunkExtras {
pub fluid_ticks: Vec,
/// Game time written as `LastUpdate`.
pub last_update: i64,
+ /// Containers still holding their loot table's roll.
+ pub rolled_loot: Vec,
}
fn tick_list(ticks: &[SavedTick], name: impl Fn(u32) -> Option