Merge main into feat/moved-hud-extras

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-26 06:02:48 +01:00
191 changed files with 16182 additions and 3858 deletions
+5 -3
View File
@@ -23,9 +23,11 @@ env:
jobs:
parity-main:
# The Mac mini's runners only: a desktop job slowed down because the maintainer came back could time out, and a
# failure here bisects and comments on merges.
runs-on: [self-hosted, horizon-game, macOS]
# The Mac mini's release runner (label added 2026-10-26), not the two `horizon-game` runners: behind PR parity jobs
# this run waited 2 h while every merge replaced the pending one, and releases wait for it. Releases need it green
# first, so the two never compete. Not the desktop: a job slowed down because the maintainer came back could time
# out, and a failure here bisects and comments on merges.
runs-on: [self-hosted, macOS, horizon-parity-main]
timeout-minutes: 240 # a core-crate batch reruns ~300 harnesses (~60-90 min), a bisect a few rounds of the failures
steps:
- uses: actions/checkout@v4
+13 -1
View File
@@ -36,7 +36,19 @@ jobs:
- name: Tools from the runner
run: bash tools/ci/runner-env.sh
- name: Vanilla data, and resources prepared by this preparer
run: bash tools/ci/vanilla-data.sh
env:
BASE_SHA: ${{ inputs.base }}
run: |
# main's script: one written before a fix to it left a real .local/horizon-client without sounds.json on
# the runner, and every later job there failed `subtitle`. A branch that changes the script runs its own.
set -euo pipefail
if git rev-parse --verify -q origin/main >/dev/null \
&& git diff --quiet "$(git merge-base "$BASE_SHA" HEAD)" HEAD -- tools/ci/vanilla-data.sh; then
git show origin/main:tools/ci/vanilla-data.sh > "$RUNNER_TEMP/vanilla-data.sh"
bash "$RUNNER_TEMP/vanilla-data.sh"
else
bash tools/ci/vanilla-data.sh
fi
- name: The prepared output is the one the preparer's OUTPUT_VERSION says
env:
BASE_SHA: ${{ inputs.base }}
+46 -1
View File
@@ -79,9 +79,50 @@ jobs:
# A dry run builds whatever the decision, to test the builds.
if [[ "${{ steps.mode.outputs.dry_run }}" == true ]]; then echo "release=true" >> "$GITHUB_OUTPUT"; fi
build:
exporters:
needs: decide
if: needs.decide.outputs.release == 'true'
# This runner holds the maintainer's own pinned Minecraft jars. Only our classes leave it.
runs-on: ${{ vars.MAC_RUNNER || 'ludovics-mac-mini' }}
# Three full preparations (two isolated, one batched) besides the compile.
timeout-minutes: 60
steps:
- uses: actions/checkout@v4
with:
ref: ${{ needs.decide.outputs.head }}
persist-credentials: false
clean: true
- uses: dtolnay/rust-toolchain@stable
- run: bash tools/ci/runner-env.sh
- run: bash tools/ci/vanilla-data.sh
- name: Compile and validate original exporters against the pinned jars
shell: bash
run: |
set -euo pipefail
# target/ is gone after the clean checkout, and vanilla-data.sh builds the preparer only when it
# prepares afresh.
cargo build --release --locked -p horizon-prepare
# Compiled here, never taken from the runner's shared preparations: pull requests' parity jobs
# write those (vanilla-data.sh links .local/exporters to them).
rm -rf .local/exporters
bundle=$(target/release/horizon-prepare compile-exporters --root "$PWD" | tail -1)
mkdir -p exporters
cp -R "$bundle/server" "$bundle/client" "$bundle/manifest.json" exporters/
# Only our classes and their manifest leave this runner.
stray=$(find exporters -type f ! -path exporters/manifest.json \
! \( -name '*.class' \( -path 'exporters/server/dev/horizonlab/parity/*' -o -path 'exporters/client/dev/horizonlab/parity/*' \) \))
[ -z "$stray" ] || { echo "::error::not one of our classes: $stray"; exit 1; }
python3 tools/check_prepare_exporters.py --binary target/release/horizon-prepare
- uses: actions/upload-artifact@v4
with:
name: original-exporters
path: exporters/
if-no-files-found: error
retention-days: 3
build:
needs: [decide, exporters]
if: needs.decide.outputs.release == 'true'
name: build ${{ matrix.name }}
strategy:
fail-fast: false
@@ -220,6 +261,10 @@ jobs:
path: ${{ runner.temp }}/smoke-test.json
if-no-files-found: error
retention-days: 3
- uses: actions/download-artifact@v4
with:
name: original-exporters
path: exporters
- name: Package
run: |
set -euo pipefail
+3
View File
@@ -31,3 +31,6 @@
/.horizon-cache/
/crash-reports/
/debug/
# Release-only compiled original exporter bundle.
/exporters/
Generated
+231 -310
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -31,7 +31,7 @@ Horizon is Minecraft: Java Edition 26.3 rewritten in Rust: a native game that re
Horizon is installed through **the Horizon launcher**. Get it from [playhorizon.dev](https://playhorizon.dev) (the Download button picks your system) or from [the launcher releases](https://github.com/ludovic111/horizon/releases?q=launcher-v&expanded=true), for Linux x86-64, macOS on Apple Silicon or Windows x64. The launcher installs and updates the game, prepares its files from your own copy of Minecraft, and updates itself.
**You need** Minecraft: Java Edition 26.3, played once (the official launcher, Prism, MultiMC and other launchers all work); a Java 25 JDK, for the one-time preparation; and a GPU with Vulkan on Linux and Windows, or any Apple Silicon Mac. Step by step, with macOS's first-open prompt and the first launch: [Install and first launch](https://playhorizon.dev/docs/install).
**You need** Minecraft: Java Edition 26.3, played once (the official launcher, Prism, MultiMC and other launchers all work); a Java 25 runtime, for the one-time preparation; and a GPU with Vulkan on Linux and Windows, or any Apple Silicon Mac. Step by step, with macOS's first-open prompt and the first launch: [Install and first launch](https://playhorizon.dev/docs/install).
<table>
<tr>
@@ -367,6 +367,7 @@ pub fn main() -> Result<()> {
lid: &|_| open,
sign: &|_| sign.as_ref(),
banner: &|_| layers.clone(),
skull: &|_| None,
glyph: &|c| atlas.glyph_cell(c),
};
let mut verts = Vec::new();
@@ -7,7 +7,7 @@
//! usage: chat_screen_fixture RESOURCES REGISTRIES CASES OUTPUT
use crate::pack_screen_fixture;
use horizon_client::{
chat::{ChatAction, ChatScreen, Commands},
chat::{ChatAbilities, ChatAction, ChatScreen, ChatVisibility, Commands},
command::vanilla,
hud::HudAtlas,
lang,
@@ -53,6 +53,7 @@ pub fn main() -> horizon_client::Result<()> {
history.len(),
w,
h,
ChatAbilities::new(ChatVisibility::Full),
);
screen.auto_suggestions = case["autoSuggest"] == true;
let mut closed = false;
@@ -319,6 +319,7 @@ fn chat(c: &Value, h: i32, atlas: &HudAtlas) -> Vec<GuiDraw> {
atlas,
comp(&m["text"]),
indicator,
horizon_client::chat::ChatSource::Client,
m["at"].as_u64().unwrap_or(0),
false,
);
@@ -730,6 +730,10 @@ pub struct Scene<'a> {
pub sign: &'a dyn Fn([i32; 3]) -> Option<&'a crate::signs::SignData>,
/// Banner layers at a position: pattern texture and RGB tint, bottom first.
pub banner: &'a dyn Fn([i32; 3]) -> Vec<(String, u32)>,
/// The skin sprite a `player_head` shows at a position (`SkullBlockRenderer`
/// profile → skin): a downloaded `skins/<sha1>` slot or the profile's
/// `DefaultPlayerSkin` fallback; None keeps `SKIN_BY_TYPE` (`DefaultPlayerSkin.getDefaultTexture`, slim steve).
pub skull: &'a dyn Fn([i32; 3]) -> Option<&'a str>,
/// Font glyph cell (none for blank glyphs such as the space) and advance.
pub glyph: &'a dyn Fn(char) -> (Option<Rect>, f32),
}
@@ -1279,6 +1283,13 @@ impl BlockEntityRenderer {
SkullType::Piglin => ("piglin_head", "entity/piglin/piglin"),
SkullType::Player => ("player_head", "entity/player/slim/steve"),
};
// SkullBlockRenderer.getRenderType: the profile's skin when it
// resolves, `SKIN_BY_TYPE` (steve) otherwise.
let texture = if *kind == SkullType::Player {
(scene.skull)(pos).unwrap_or(texture)
} else {
texture
};
let (Some(model), Some(paint)) =
(self.models.get(model), paint(texture, 0xFFFFFF, packed))
else {
@@ -1886,6 +1897,7 @@ mod tests {
lid: &|_| 0.0,
sign: &|_| None,
banner: &|_| Vec::new(),
skull: &|_| None,
glyph: &|_| (None, 0.0),
};
// Compare the cached world path with the original model renderer,
+34
View File
@@ -87,6 +87,19 @@ fn filled(player: &mut dyn Player, new: ItemStack, limit_creative: bool) {
}
}
/// `LayeredCauldronBlock.handleEntityOnFireInside`: a burning entity in a
/// water cauldron lowers it a level; a powder snow cauldron becomes a water
/// cauldron of its level first, then lowers.
pub fn extinguished(cauldron: &BlockState) -> BlockState {
if cauldron.name == "minecraft:powder_snow_cauldron" {
let mut water = cauldron.clone();
water.name = "minecraft:water_cauldron".into();
lowered(&water)
} else {
lowered(cauldron)
}
}
/// `LayeredCauldronBlock.lowerFillLevel`.
fn lowered(cauldron: &BlockState) -> BlockState {
let level: u8 = cauldron
@@ -296,3 +309,24 @@ pub fn interact(
_ => pass(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_burning_entity_lowers_water_and_melts_powder_snow() {
let water = |l| state("minecraft:water_cauldron", Some(l));
assert_eq!(extinguished(&water(3)), water(2));
assert_eq!(extinguished(&water(1)), state("minecraft:cauldron", None));
// Powder snow turns to water of its level, then lowers.
assert_eq!(
extinguished(&state("minecraft:powder_snow_cauldron", Some(3))),
water(2)
);
assert_eq!(
extinguished(&state("minecraft:powder_snow_cauldron", Some(1))),
state("minecraft:cauldron", None)
);
}
}
+167 -10
View File
@@ -19,6 +19,92 @@ mod tests;
/// GuiMessageTag.systemSinglePlayer's indicator colour.
pub const SYSTEM_INDICATOR: u32 = 0xD0D0D0;
/// `ChatVisiblity`: the Chat option's three values.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ChatVisibility {
Full,
System,
Hidden,
}
impl ChatVisibility {
/// Options.chatVisibility's saved value (0 full, 1 system, 2 hidden).
pub fn from_option(value: u8) -> Self {
match value {
0 => Self::Full,
1 => Self::System,
_ => Self::Hidden,
}
}
}
/// `ChatAbilities`: what the Chat option allows, as `Minecraft.computeChatAbilities`
/// derives it from the visibility alone (`ChatRestriction`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ChatAbilities {
pub send_messages: bool,
pub send_commands: bool,
pub receive_player_messages: bool,
pub receive_system_messages: bool,
}
impl ChatAbilities {
/// `ChatRestriction.CHAT_AND_COMMANDS_DISABLED_BY_OPTIONS` (Hidden) takes
/// chat, commands, player messages and system messages;
/// `CHAT_DISABLED_BY_OPTIONS` (System) takes the chat and player messages.
pub fn new(visibility: ChatVisibility) -> Self {
match visibility {
ChatVisibility::Full => Self {
send_messages: true,
send_commands: true,
receive_player_messages: true,
receive_system_messages: true,
},
ChatVisibility::System => Self {
send_messages: false,
send_commands: true,
receive_player_messages: false,
receive_system_messages: true,
},
ChatVisibility::Hidden => Self {
send_messages: false,
send_commands: false,
receive_player_messages: false,
receive_system_messages: false,
},
}
}
/// `visibleMessagesFilter`: the client's own lines always show.
pub fn shows(&self, source: ChatSource) -> bool {
match source {
ChatSource::Player => self.receive_player_messages,
ChatSource::Server => self.receive_system_messages,
ChatSource::Client => true,
}
}
/// Whether the chat screen may send `input` (`hasAllowedInput`).
pub fn allows(&self, input: &str) -> bool {
if input.starts_with('/') {
self.send_commands
} else {
self.send_messages
}
}
}
impl Default for ChatAbilities {
fn default() -> Self {
Self::new(ChatVisibility::Full)
}
}
/// Where a chat line came from (`GuiMessageSource`): the visibility filters
/// player messages and the server's, while the client's own always show.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ChatSource {
Player,
Server,
Client,
}
/// The chat options (doubles, as vanilla keeps them).
#[derive(Clone, Copy, Debug)]
pub struct ChatOptions {
@@ -71,6 +157,7 @@ struct Message {
added: u64,
content: Text,
indicator: Option<u32>,
source: ChatSource,
}
/// A wrapped line (`GuiMessage.Line`).
#[derive(Clone, Debug)]
@@ -105,6 +192,8 @@ pub struct Chat {
pub log: bool,
/// Messages added since the start, for the control socket's events.
pub received: u64,
/// The Chat option's `visibleMessageFilter` (`set_abilities`).
abilities: ChatAbilities,
}
impl Chat {
pub fn lines_per_page(&self, focused: bool) -> usize {
@@ -114,16 +203,32 @@ impl Chat {
fn max_width(&self) -> i32 {
(f64::from(self.options.width_px()) / self.options.scale).floor() as i32
}
/// ChatComponent.addMessage: wrap, prepend and trim to 100 lines.
/// ChatComponent.addMessage: wrap, prepend and trim to 100 lines. A
/// message the Chat option hides is dropped, unlogged.
pub fn add(
&mut self,
atlas: &HudAtlas,
content: Text,
indicator: Option<u32>,
source: ChatSource,
ticks: u64,
focused: bool,
) {
// ChatComponent.logChatMessage: every message is in the log.
self.add_message(&atlas.font, content, indicator, source, ticks, focused);
}
fn add_message(
&mut self,
font: &crate::font::Font,
content: Text,
indicator: Option<u32>,
source: ChatSource,
ticks: u64,
focused: bool,
) {
if !self.abilities.shows(source) {
return;
}
// ChatComponent.logChatMessage: every message shown is in the log.
crate::logging::write_only(log::Level::Info, &format!("[CHAT] {}", content.plain()));
if self.log {
println!("[CHAT] {}", content.plain());
@@ -132,16 +237,17 @@ impl Chat {
added: ticks,
content,
indicator,
source,
};
self.add_lines(atlas, &message, focused);
self.add_lines(font, &message, focused);
self.messages.insert(0, message);
self.messages.truncate(100);
self.received += 1;
}
fn add_lines(&mut self, atlas: &HudAtlas, message: &Message, focused: bool) {
fn add_lines(&mut self, font: &crate::font::Font, message: &Message, focused: bool) {
let width = self.max_width();
let content = self.displayed(&message.content);
for line in atlas.font.wrap_components(&content, width) {
for line in font.wrap_components(&content, width) {
if focused && self.scroll > 0 {
self.new_since_scroll = true;
self.scroll_by(1, focused);
@@ -167,16 +273,38 @@ impl Chat {
content.strip_legacy()
}
}
/// Re-wrap after a width or scale change (ChatComponent.rescaleChat).
/// Re-wrap after a width or scale change (ChatComponent.rescaleChat),
/// keeping only the messages the Chat option shows.
pub fn rescale(&mut self, atlas: &HudAtlas) {
self.rewrap(&atlas.font);
}
fn rewrap(&mut self, font: &crate::font::Font) {
self.scroll = 0;
self.new_since_scroll = false;
self.lines.clear();
let messages: Vec<Message> = self.messages.iter().rev().cloned().collect();
let messages: Vec<Message> = self
.messages
.iter()
.rev()
.filter(|m| self.abilities.shows(m.source))
.cloned()
.collect();
for m in &messages {
self.add_lines(atlas, m, false);
self.add_lines(font, m, false);
}
}
/// `ChatComponent.setVisibleMessageFilter`: the Chat option changed;
/// the messages it now hides leave the chat, those it shows again return.
pub fn set_abilities(&mut self, font: &crate::font::Font, abilities: ChatAbilities) {
if self.abilities != abilities {
self.abilities = abilities;
self.rewrap(font);
}
}
/// Whether a message from `source` would show (`visibleMessagesFilter`).
pub fn shows(&self, source: ChatSource) -> bool {
self.abilities.shows(source)
}
pub fn clear(&mut self) {
self.messages.clear();
self.lines.clear();
@@ -437,6 +565,8 @@ pub struct ChatScreen {
allow_suggestions: bool,
keep_suggestions: bool,
pub auto_suggestions: bool,
/// `CommandSuggestions.setRestrictions`: what the Chat option allows.
pub abilities: ChatAbilities,
}
/// Local completions waiting for the server's reply to request `id`.
struct Awaiting {
@@ -482,6 +612,7 @@ impl ChatScreen {
history_len: usize,
width: i32,
height: i32,
abilities: ChatAbilities,
) -> Self {
let mut input = TextField::new(4, height - 12, width - 4, 12);
input.max_length = 256;
@@ -505,6 +636,7 @@ impl ChatScreen {
allow_suggestions: false,
keep_suggestions: false,
auto_suggestions: true,
abilities,
};
s.update_command_info(m, c);
s
@@ -522,7 +654,8 @@ impl ChatScreen {
if (width, height) != (self.width, self.height) {
let value = self.input.value.clone();
let auto = self.auto_suggestions;
*self = Self::new(m, c, &value, self.history_pos, width, height);
let abilities = self.abilities;
*self = Self::new(m, c, &value, self.history_pos, width, height, abilities);
self.auto_suggestions = auto;
}
}
@@ -604,6 +737,14 @@ impl ChatScreen {
self.pending = Some((range.0, list));
self.usage_width = 0;
self.usage_position = 0;
// CommandSuggestions.updateCommandInfo: the Chat option may take
// the messages.
if !self.abilities.send_messages {
let line =
Text::translate("chat_screen.messages_not_allowed", &[]).with_color(text::RED);
self.usage_width = m.width(&line);
self.usage.push(line);
}
} else {
self.awaiting = None;
self.pending = None;
@@ -708,6 +849,15 @@ impl ChatScreen {
}
self.usage.extend(entries);
}
// CommandSuggestions.updateUsageInfo: the Chat option may take the
// commands, under the usage. (Vanilla also counts a command with a
// message argument, `/say hi`, as a message; Horizon's parse keeps no
// argument kinds, so only a bare message does.)
if !self.abilities.send_commands {
self.usage.push(
Text::translate("chat_screen.commands_not_allowed", &[]).with_color(text::RED),
);
}
self.usage_width = self.usage.iter().map(|u| m.width(u)).max().unwrap_or(0);
self.usage_position = if self.usage.is_empty() {
0
@@ -842,7 +992,14 @@ impl ChatScreen {
}
match key {
K::Escape => return ChatAction::Close,
K::Enter | K::NumpadEnter => return ChatAction::Submit(self.normalized()),
K::Enter | K::NumpadEnter => {
// ChatScreen.keyPressed: without `hasAllowedInput` the input
// stays, unsent, and the screen stays open.
if !self.abilities.allows(&self.input.value) {
return ChatAction::None;
}
return ChatAction::Submit(self.normalized());
}
K::ArrowUp => self.move_in_history(m, c, history, -1),
K::ArrowDown => self.move_in_history(m, c, history, 1),
_ => {
+114 -5
View File
@@ -2,6 +2,11 @@ use super::*;
use crate::{font::Font, hud::Rect};
use std::{collections::HashMap, path::Path};
/// The Chat option's default: no restriction.
fn full() -> ChatAbilities {
ChatAbilities::new(ChatVisibility::Full)
}
fn font() -> Font {
// The missing-glyph metrics let these tests run without game assets.
Font::load(
@@ -139,7 +144,7 @@ fn locate_coordinates_fill_chat_with_teleport_without_submitting() {
assert_eq!(link.component()["click_event"]["command"], expected);
let mut chat = Chat::default();
add(&mut chat, &font, Text::literal("Found at ").append(link));
let mut screen = ChatScreen::new(&m, &commands, "draft", 0, 640, 360);
let mut screen = ChatScreen::new(&m, &commands, "draft", 0, 640, 360, full());
let x = 4.0 + f64::from(font.width("Found at "));
assert!(!screen.click(&m, &commands, x, 315.0));
let Some(ClickEvent::SuggestCommand(command)) = chat.click_at(&font, 360, x, 315.0) else {
@@ -176,7 +181,15 @@ fn history_arrows_recall_messages_and_restore_the_draft() {
registries: &registries,
};
let history = vec!["first message".to_owned(), "latest message".to_owned()];
let mut screen = ChatScreen::new(&m, &commands, "unfinished draft", history.len(), 640, 360);
let mut screen = ChatScreen::new(
&m,
&commands,
"unfinished draft",
history.len(),
640,
360,
full(),
);
for (key, expected) in [
(ArrowUp, "latest message"),
(ArrowUp, "first message"),
@@ -213,7 +226,7 @@ fn mod_commands_are_coloured_and_complete_like_vanilla_ones() {
dispatcher,
registries: &registries,
};
let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360);
let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360, full());
screen.type_text(&m, &c, line);
screen
};
@@ -259,7 +272,7 @@ fn ask_server_nodes_wait_for_the_servers_completions() {
let tab = |screen: &mut ChatScreen| {
screen.key(&m, &c, &[], winit::keyboard::KeyCode::Tab, false, false);
};
let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360);
let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360, full());
screen.type_text(&m, &c, "/bossbar get pt");
let (request, sent) = screen.take_request().expect("the server is asked");
assert_eq!(sent, "/bossbar get pt");
@@ -281,7 +294,7 @@ fn ask_server_nodes_wait_for_the_servers_completions() {
dispatcher: local,
registries: &registries,
};
let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360);
let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360, full());
screen.type_text(&m, &c, "/bossbar get pt");
assert_eq!(screen.take_request(), None);
}
@@ -327,3 +340,99 @@ fn a_server_component_keeps_its_click_and_hover_events() {
let past = 4.0 + f64::from(font.width("CLICK")) + 20.0;
assert!(chat.hover_at(&font, 360, past, 315.0).is_none());
}
/// `ChatVisiblity`: Commands Only keeps the chat out, Hidden takes the
/// commands too (`ChatAbilities`), and Enter sends nothing the option hides.
#[test]
fn the_chat_option_decides_what_may_be_sent() {
let font = font();
let m = Measure(&font);
let registries = Registries::default();
let c = Commands {
dispatcher: crate::command::vanilla::game_dispatcher(),
registries: &registries,
};
let enter = |screen: &mut ChatScreen| {
screen.key(&m, &c, &[], winit::keyboard::KeyCode::Enter, false, false)
};
for (visibility, chat, command) in [
(ChatVisibility::Full, true, true),
(ChatVisibility::System, false, true),
(ChatVisibility::Hidden, false, false),
] {
let abilities = ChatAbilities::new(visibility);
assert_eq!(abilities.allows("hi"), chat, "{visibility:?}");
assert_eq!(abilities.allows("/time set day"), command, "{visibility:?}");
for (line, allowed) in [("hi", chat), ("/time set day", command)] {
let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360, abilities);
screen.type_text(&m, &c, line);
match enter(&mut screen) {
ChatAction::Submit(sent) => {
assert!(allowed, "{visibility:?} sent {sent}");
assert_eq!(sent, line);
}
ChatAction::None => assert!(!allowed, "{visibility:?} kept {line}"),
ChatAction::Close => panic!("Enter does not close the chat"),
}
}
}
}
/// The restriction the option sets is a usage line: red, under the input, for
/// a command the Chat option takes and for a message it takes.
#[test]
fn restricted_input_says_why_it_will_not_send() {
let font = font();
let m = Measure(&font);
let registries = Registries::default();
let c = Commands {
dispatcher: crate::command::vanilla::game_dispatcher(),
registries: &registries,
};
let usage = |visibility, line: &str| {
let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360, ChatAbilities::new(visibility));
screen.type_text(&m, &c, line);
screen.usage.iter().map(|t| t.plain()).collect::<Vec<_>>()
};
assert_eq!(usage(ChatVisibility::Full, "hi"), Vec::<String>::new());
// The font has no language files here, so the line is its key.
assert_eq!(
usage(ChatVisibility::System, "hi"),
["chat_screen.messages_not_allowed"]
);
// A command the option allows says nothing about the restriction.
assert!(
!usage(ChatVisibility::System, "/time set day")
.iter()
.any(|line| line == "chat_screen.commands_not_allowed")
);
assert!(
usage(ChatVisibility::Hidden, "/time set day")
.iter()
.any(|line| line == "chat_screen.commands_not_allowed")
);
}
/// `ChatComponent.setVisibleMessageFilter`: a message the Chat option hides
/// on arrival is dropped; one already in the chat leaves it while the option
/// hides its kind and comes back when it shows it again.
#[test]
fn the_chat_option_filters_the_lines_both_ways() {
let font = font();
let mut chat = Chat::default();
let says = |chat: &mut Chat, text: &str, source| {
chat.add_message(&font, Text::literal(text), None, source, 0, false);
};
let shown =
|chat: &Chat| -> Vec<String> { chat.lines.iter().map(|l| l.content.plain()).collect() };
says(&mut chat, "player", ChatSource::Player);
says(&mut chat, "server", ChatSource::Server);
chat.set_abilities(&font, ChatAbilities::new(ChatVisibility::System));
assert_eq!(shown(&chat), ["server"]);
says(&mut chat, "dropped", ChatSource::Player);
chat.set_abilities(&font, ChatAbilities::new(ChatVisibility::Hidden));
says(&mut chat, "client", ChatSource::Client);
assert_eq!(shown(&chat), ["client"]);
chat.set_abilities(&font, ChatAbilities::new(ChatVisibility::Full));
assert_eq!(shown(&chat), ["client", "server", "player"]);
}
+18
View File
@@ -92,6 +92,13 @@ impl<W: BlockCollisionWorld> BlockCollisionWorld for EntityCollisions<'_, W> {
) -> std::result::Result<bool, Self::Error> {
self.blocks.is_suffocating(chunk, block, p)
}
fn may_reach_out(
&mut self,
chunk: Self::Chunk,
section_y: i32,
) -> std::result::Result<bool, Self::Error> {
self.blocks.may_reach_out(chunk, section_y)
}
fn context_shape(
&mut self,
block: CollisionBlock,
@@ -262,6 +269,17 @@ impl<'w> BlockCollisionWorld for SceneCollisions<'w, '_> {
fn collision_block(&mut self, chunk: Self::Chunk, p: BlockPos) -> io::Result<CollisionBlock> {
self.binding.collision_block(chunk.block(p.x, p.y, p.z))
}
/// From the section's palette, without reading its blocks; a palette
/// of the whole registry reads every cell.
fn may_reach_out(&mut self, chunk: Self::Chunk, section_y: i32) -> io::Result<bool> {
let blocks = usize::try_from(section_y - (crate::chunk::MIN_Y >> 4))
.ok()
.and_then(|s| chunk.sections.get(s))
.map(|section| &section.blocks);
Ok(blocks.is_none_or(|blocks| {
blocks.is_global() || blocks.maybe_has(|v| self.binding.reaches_out(BlockStateId(v)))
}))
}
fn is_suffocating(
&mut self,
_: Self::Chunk,
+64 -3
View File
@@ -83,6 +83,60 @@ pub enum BlockEntity {
Beehive(Box<crate::beehive::Beehive>),
/// `CommandBlockEntity` of the three command blocks (`crate::command_block`).
CommandBlock(Box<crate::command_block::CommandBlock>),
/// `SkullBlockEntity` of every skull and head block.
Skull(Skull),
}
/// `SkullBlockEntity`: the player profile the head shows (kept as loaded:
/// a compound, or a string when it names a player only), the note block
/// sound and `custom_name`.
#[derive(Clone, Debug, Default)]
pub struct Skull {
pub profile: Option<horizon_world::nbt::Tag>,
pub note_block_sound: Option<String>,
pub custom_name: Option<horizon_world::nbt::Tag>,
/// What `profile` draws, read from it once rather than every frame.
pub skin: Option<SkullSkin>,
}
impl Skull {
pub fn new(
profile: Option<horizon_world::nbt::Tag>,
note_block_sound: Option<String>,
custom_name: Option<horizon_world::nbt::Tag>,
) -> Self {
let skin = profile.as_ref().map(SkullSkin::of);
Self {
profile,
note_block_sound,
custom_name,
skin,
}
}
}
/// `SkullBlockRenderer.getPlayerSkin` of a head's profile: its skin's URL
/// with the `skins/<sha1>` sprite it downloads to, and the
/// `DefaultPlayerSkin` drawn until that sprite is there (steve for a
/// profile with no id).
#[derive(Clone, Debug)]
pub struct SkullSkin {
pub download: Option<(String, String)>,
pub fallback: &'static str,
}
impl SkullSkin {
pub fn of(profile: &horizon_world::nbt::Tag) -> Self {
let download = crate::skins::profile_skin_url_nbt(profile).and_then(|url| {
let location = crate::skins::skin_location(&crate::skins::texture_hash(&url)?);
Some((url, location))
});
// DefaultPlayerSkin.get(id), or getDefaultSkin (slim steve) without an id.
let fallback = crate::skins::profile_uuid_nbt(profile)
.map_or("entity/player/slim/steve", crate::tab_list::default_skin);
Self { download, fallback }
}
}
/// The skull and head blocks (`SkullBlock`, `WallSkullBlock`): every
/// `*_skull` and `*_head` but the piston's head.
fn is_skull(name: &str) -> bool {
name.ends_with("_skull") || (name.ends_with("_head") && name != "minecraft:piston_head")
}
/// `CrafterBlockEntity`: nine slots that can be disabled, the powered flag
/// and the ticks left in the crafting animation.
@@ -238,6 +292,9 @@ impl BlockEntity {
if name.ends_with("_banner") {
return Some(Self::Banner(Banner::default()));
}
if is_skull(name) {
return Some(Self::Skull(Skull::default()));
}
if name == "minecraft:beacon" {
return Some(Self::Beacon(Box::new(Beacon {
last_check_y: i32::MIN / 2,
@@ -283,6 +340,7 @@ impl BlockEntity {
Self::Vault(_) => "minecraft:vault",
Self::Beehive(_) => "minecraft:beehive",
Self::CommandBlock(_) => "minecraft:command_block",
Self::Skull(_) => "minecraft:skull",
}
}
pub fn contents(&self) -> Vec<ItemStack> {
@@ -298,7 +356,8 @@ impl BlockEntity {
| Self::TrialSpawner(_)
| Self::Vault(_)
| Self::Beehive(_)
| Self::CommandBlock(_) => Vec::new(),
| Self::CommandBlock(_)
| Self::Skull(_) => Vec::new(),
Self::Lectern(l) => l.book.iter().cloned().collect(),
Self::Sculk(_) => Vec::new(),
Self::Banner(_) => Vec::new(),
@@ -315,7 +374,8 @@ impl BlockEntity {
| Self::Beacon(_)
| Self::Lectern(_)
| Self::Sculk(_)
| Self::Banner(_) => &[],
| Self::Banner(_)
| Self::Skull(_) => &[],
Self::Crafter(c) => &c.items,
Self::CreakingHeart(_)
| Self::Spawner(_)
@@ -335,7 +395,8 @@ impl BlockEntity {
| Self::Beacon(_)
| Self::Lectern(_)
| Self::Sculk(_)
| Self::Banner(_) => &mut [],
| Self::Banner(_)
| Self::Skull(_) => &mut [],
Self::Crafter(c) => &mut c.items,
Self::CreakingHeart(_)
| Self::Spawner(_)
+174
View File
@@ -0,0 +1,174 @@
//! `Player.getCooldowns()` (`ItemCooldowns`) as the game keeps it: the
//! `use_cooldown` component's entries by cooldown group, counted down once a
//! tick, and the percent `GuiGraphicsExtractor.itemCooldown` shades a slot
//! by. The world crate's `item_cooldowns` models vanilla's hash map and its
//! observers for the parity harness; this is the same data with the lookup a
//! slot needs, and no observer (vanilla's own player uses the plain class).
use crate::items::ItemStack;
use horizon_world::nbt::Tag;
use std::collections::HashMap;
use std::sync::Arc;
/// A stack's `minecraft:use_cooldown` component.
#[derive(Clone, Debug, PartialEq)]
pub struct UseCooldown {
pub seconds: f32,
/// `cooldown_group`: the group the entry is filed under, else the item.
pub group: Option<Arc<str>>,
}
impl UseCooldown {
/// `UseCooldown.ticks`: the seconds in ticks, truncated.
pub fn ticks(&self) -> i32 {
(self.seconds * 20.0) as i32
}
/// The component as `items.json` holds it.
pub fn from_json(v: &serde_json::Value) -> Option<Self> {
let seconds = v.get("seconds")?.as_f64()? as f32;
Some(Self {
seconds,
group: v
.get("cooldown_group")
.and_then(|g| g.as_str())
.map(Arc::from),
})
}
/// The component as a stack's saved `components` holds it.
pub fn from_tag(t: &Tag) -> Option<Self> {
let Tag::Compound(c) = t else {
return None;
};
let text = |k: &str| match c.get(&horizon_world::nbt::JavaString::from(k)) {
Some(Tag::String(s)) => Some(s.to_string_lossy()),
_ => None,
};
let seconds = match c.get(&horizon_world::nbt::JavaString::from("seconds")) {
Some(Tag::Float(bits)) => f32::from_bits(*bits),
Some(Tag::Double(bits)) => f64::from_bits(*bits) as f32,
_ => return None,
};
Some(Self {
seconds,
group: text("cooldown_group").map(Arc::from),
})
}
}
/// The component a stack carries: its own patch first (`ItemStack.get`), then
/// the item's default from `items.json` (`ItemInfo::use_cooldown`).
pub fn component(stack: &ItemStack, default: Option<&UseCooldown>) -> Option<UseCooldown> {
stack
.data()
.and_then(|d| d.other("minecraft:use_cooldown"))
.and_then(UseCooldown::from_tag)
.or_else(|| default.cloned())
}
/// `ItemCooldowns.getCooldownGroup`: the component's group, else the item id.
pub fn group(stack: &ItemStack, default: Option<&UseCooldown>) -> Arc<str> {
component(stack, default)
.and_then(|c| c.group)
.unwrap_or_else(|| Arc::clone(&stack.item))
}
/// `ItemCooldowns`: start and end ticks by group, and the tick counter.
#[derive(Clone, Debug, Default)]
pub struct Cooldowns {
tick_count: i32,
entries: HashMap<Arc<str>, (i32, i32)>,
}
impl Cooldowns {
/// `getCooldownPercent`: what is left of the entry, from 1 to 0.
pub fn percent(&self, group: &str, partial_tick: f32) -> f32 {
let Some((start, end)) = self.entries.get(group) else {
return 0.0;
};
let duration = end.wrapping_sub(*start) as f32;
let remaining = *end as f32 - (self.tick_count as f32 + partial_tick);
(remaining / duration).clamp(0.0, 1.0)
}
/// `isOnCooldown`: whether the group still has time to run.
pub fn is_on_cooldown(&self, group: &str) -> bool {
self.percent(group, 0.0) > 0.0
}
/// `addCooldown`.
pub fn add(&mut self, group: Arc<str>, ticks: i32) {
self.entries.insert(
group,
(self.tick_count, self.tick_count.wrapping_add(ticks)),
);
}
/// `ItemCooldowns.tick`: the counter, then every entry that has run out,
/// which it hands back (`onCooldownEnded`).
pub fn tick(&mut self) -> Vec<Arc<str>> {
self.tick_count = self.tick_count.wrapping_add(1);
let done: Vec<Arc<str>> = self
.entries
.iter()
.filter(|(_, (_, end))| *end <= self.tick_count)
.map(|(group, _)| Arc::clone(group))
.collect();
self.entries.retain(|_, (_, end)| *end > self.tick_count);
done
}
/// `removeCooldown`.
pub fn remove(&mut self, group: &str) {
self.entries.remove(group);
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn stack(item: &str) -> ItemStack {
ItemStack::new(item, 1)
}
#[test]
fn the_percent_falls_from_one_to_zero_over_the_entry() {
let mut c = Cooldowns::default();
c.add(Arc::from("minecraft:ender_pearl"), 20);
assert!(c.is_on_cooldown("minecraft:ender_pearl"));
assert_eq!(c.percent("minecraft:ender_pearl", 0.0), 1.0);
for _ in 0..10 {
assert!(c.tick().is_empty());
}
assert_eq!(c.percent("minecraft:ender_pearl", 0.0), 0.5);
// The partial tick moves it within the tick.
assert_eq!(c.percent("minecraft:ender_pearl", 0.5), 0.475);
for _ in 0..9 {
c.tick();
}
assert!(c.is_on_cooldown("minecraft:ender_pearl"));
// ItemCooldowns.tick drops the entry at its end tick, and says so.
assert_eq!(c.tick(), [Arc::<str>::from("minecraft:ender_pearl")]);
assert!(!c.is_on_cooldown("minecraft:ender_pearl"));
assert!(c.is_empty());
assert_eq!(c.percent("minecraft:ender_pearl", 0.0), 0.0);
}
#[test]
fn the_group_comes_from_the_stack_then_the_item() {
let pearl = stack("minecraft:ender_pearl");
let default = UseCooldown::from_json(&json!({"seconds": 1.0})).expect("the component");
assert_eq!(&*group(&pearl, Some(&default)), "minecraft:ender_pearl");
// A stack's own component overrides the item's, group included.
let mut grouped = stack("minecraft:ender_pearl");
grouped.data_mut().set_other(
"minecraft:use_cooldown",
Some(Tag::compound([
("seconds", Tag::Float(2.0f32.to_bits())),
("cooldown_group", Tag::String("minecraft:pearls".into())),
])),
);
let c = component(&grouped, Some(&default)).expect("the stack's component");
assert_eq!(c.ticks(), 40);
assert_eq!(&*group(&grouped, Some(&default)), "minecraft:pearls");
// An item without the component has no cooldown.
assert!(component(&stack("minecraft:dirt"), None).is_none());
}
}
+794
View File
@@ -0,0 +1,794 @@
//! Server dialogs (`net.minecraft.server.dialog`): what `Dialog.DIRECT_CODEC`
//! decodes from a `show_dialog` packet, the values its inputs hold, and the
//! click event an action makes of them (`Action.createAction`).
use crate::text::Text;
use horizon_world::nbt::Tag;
use serde_json::{Value, json};
/// `CommonDialogData` and the type's own fields.
#[derive(Clone, Debug)]
pub struct Dialog {
pub kind: Kind,
pub title: Text,
pub external_title: Option<Text>,
pub can_close_with_escape: bool,
pub pause: bool,
pub after_action: AfterAction,
pub body: Vec<Body>,
pub inputs: Vec<Input>,
}
/// The dialog types (`DialogTypes`).
#[derive(Clone, Debug)]
pub enum Kind {
/// `NoticeDialog`: one button, "OK" when the server names none.
Notice(Button),
Confirmation {
yes: Button,
no: Button,
},
MultiAction {
actions: Vec<Button>,
exit: Option<Button>,
columns: i32,
},
ServerLinks {
exit: Option<Button>,
columns: i32,
button_width: i32,
},
/// `DialogListDialog`: its dialogs as sent (ids, a tag or inline dialogs).
DialogList {
dialogs: Tag,
exit: Option<Button>,
columns: i32,
button_width: i32,
},
}
/// `DialogAction`: the screen after an action runs.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AfterAction {
Close,
None,
WaitForResponse,
}
/// `ActionButton`: `CommonButtonData` and what it does.
#[derive(Clone, Debug)]
pub struct Button {
pub label: Text,
pub tooltip: Option<Text>,
pub width: i32,
pub action: Option<Action>,
}
/// `Action`: a fixed click event, or one made from the inputs' values.
#[derive(Clone, Debug, PartialEq)]
pub enum Action {
/// `StaticAction`: a click event as it is.
Static(Click),
/// `CommandTemplate`: a command with `$(key)` replaced by input values.
RunCommand(String),
/// `CustomAll`: a custom click event whose payload is `additions` plus
/// every input's value under its key.
Custom { id: String, additions: Option<Tag> },
}
/// `ClickEvent`, the kinds a server may send (`isAllowedFromServer`).
#[derive(Clone, Debug, PartialEq)]
pub enum Click {
OpenUrl(String),
RunCommand(String),
SuggestCommand(String),
/// The dialog: an id of the `minecraft:dialog` registry or the dialog itself.
ShowDialog(Tag),
ChangePage(i32),
CopyToClipboard(String),
Custom {
id: String,
payload: Option<Tag>,
},
}
/// `DialogBody`.
#[derive(Clone, Debug)]
pub enum Body {
/// `PlainMessage`: centred text wrapped to `width`.
Message { contents: Text, width: i32 },
/// `ItemBody`: the item as `ItemStackTemplate` sent it.
Item {
item: Tag,
description: Option<(Text, i32)>,
show_decorations: bool,
show_tooltip: bool,
width: i32,
height: i32,
},
}
/// `Input`: the key its value goes by, and its control.
#[derive(Clone, Debug)]
pub struct Input {
pub key: String,
pub control: Control,
}
/// `InputControl`.
#[derive(Clone, Debug)]
pub enum Control {
Text {
width: i32,
label: Text,
label_visible: bool,
initial: String,
max_length: i32,
/// `MultilineOptions`: the most lines and the height, when set.
multiline: Option<(Option<i32>, Option<i32>)>,
},
Boolean {
label: Text,
initial: bool,
on_true: String,
on_false: String,
},
SingleOption {
width: i32,
/// Each option's id, its label and whether it is the initial one.
options: Vec<(String, Option<Text>, bool)>,
label: Text,
label_visible: bool,
},
NumberRange {
width: i32,
label: Text,
label_format: String,
start: f32,
end: f32,
initial: Option<f32>,
step: Option<f32>,
},
}
/// What an input holds while the dialog is open.
#[derive(Clone, Debug, PartialEq)]
pub enum InputValue {
Text(String),
Bool(bool),
/// The chosen option's index.
Option(usize),
/// The slider's position, 0 to 1.
Slider(f32),
}
fn text(t: &Tag) -> Text {
crate::scoreboard::comp_text(&crate::scoreboard::tag_to_json(t))
}
fn string(t: Option<&Tag>) -> Option<String> {
match t? {
Tag::String(s) => Some(s.to_string_lossy()),
_ => None,
}
}
fn number(t: Option<&Tag>) -> Option<f64> {
Some(match t? {
Tag::Byte(v) => f64::from(*v),
Tag::Short(v) => f64::from(*v),
Tag::Int(v) => f64::from(*v),
Tag::Long(v) => *v as f64,
Tag::Float(v) => f64::from(f32::from_bits(*v)),
Tag::Double(v) => f64::from_bits(*v),
_ => return None,
})
}
fn int(t: &Tag, key: &str, default: i32) -> i32 {
number(t.get(key)).map_or(default, |v| v as i32)
}
fn boolean(t: &Tag, key: &str, default: bool) -> bool {
number(t.get(key)).map_or(default, |v| v != 0.0)
}
fn list(t: Option<&Tag>) -> &[Tag] {
match t {
Some(Tag::List(items)) => items,
_ => &[],
}
}
/// A registry name without its default namespace.
fn path(id: &str) -> &str {
id.strip_prefix("minecraft:").unwrap_or(id)
}
impl Button {
/// `ActionButton.CODEC`.
fn parse(t: &Tag) -> Option<Self> {
Some(Self {
label: text(t.get("label")?),
tooltip: t.get("tooltip").map(text),
width: int(t, "width", 150),
action: t.get("action").and_then(Action::parse),
})
}
/// `NoticeDialog.DEFAULT_ACTION`.
fn ok() -> Self {
Self {
label: Text::translate("gui.ok", &[]),
tooltip: None,
width: 150,
action: None,
}
}
}
impl Click {
/// `ClickEvent.CODEC`, from its `action` (a click event in a component)
/// or `type` (an action) field.
pub fn parse(t: &Tag, kind: &str) -> Option<Self> {
let s = |key| string(t.get(key));
Some(match path(kind) {
"open_url" => Self::OpenUrl(s("url")?),
"run_command" => Self::RunCommand(s("command")?),
"suggest_command" => Self::SuggestCommand(s("command")?),
"show_dialog" => Self::ShowDialog(t.get("dialog")?.clone()),
"change_page" => Self::ChangePage(int(t, "page", 1)),
"copy_to_clipboard" => Self::CopyToClipboard(s("value")?),
"custom" => Self::Custom {
id: s("id")?,
payload: t.get("payload").cloned(),
},
_ => return None,
})
}
}
impl Action {
/// `Action.CODEC`, dispatched on `type`.
fn parse(t: &Tag) -> Option<Self> {
let kind = string(t.get("type"))?;
Some(match path(&kind) {
"dynamic/run_command" => Self::RunCommand(string(t.get("template"))?),
"dynamic/custom" => Self::Custom {
id: string(t.get("id"))?,
additions: t.get("additions").cloned(),
},
other => Self::Static(Click::parse(t, other)?),
})
}
/// `createAction`: the click event, given each input's key, control and value.
pub fn click(&self, inputs: &[Input], values: &[InputValue]) -> Option<Click> {
let values = || inputs.iter().zip(values);
Some(match self {
Self::Static(click) => click.clone(),
Self::RunCommand(template) => Click::RunCommand(substitute(template, &|key| {
values()
.find(|(i, _)| i.key == key)
.map(|(i, v)| i.control.substitution(v))
})),
Self::Custom { id, additions } => {
let mut tag = match additions {
Some(t @ Tag::Compound(_)) => t.clone(),
_ => Tag::Compound(Default::default()),
};
for (input, value) in values() {
if let Tag::Compound(map) = &mut tag {
map.insert(input.key.as_str().into(), input.control.tag(value));
}
}
Click::Custom {
id: id.clone(),
payload: Some(tag),
}
}
})
}
}
/// `ParsedTemplate.instantiate`: each `$(key)` replaced by its value, or by
/// nothing for a key no input has.
fn substitute(template: &str, value: &dyn Fn(&str) -> Option<String>) -> String {
let mut out = String::new();
let mut rest = template;
while let Some(at) = rest.find("$(") {
let Some(end) = rest[at..].find(')') else {
break;
};
out.push_str(&rest[..at]);
out.push_str(&value(&rest[at + 2..at + end]).unwrap_or_default());
rest = &rest[at + end + 1..];
}
out.push_str(rest);
out
}
/// `StringTag.escapeWithoutQuotes`.
fn escape(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for c in s.chars() {
match c {
'"' | '\'' | '\\' => {
out.push('\\');
out.push(c);
}
'\u{8}' => out.push_str("\\b"),
'\t' => out.push_str("\\t"),
'\n' => out.push_str("\\n"),
'\u{c}' => out.push_str("\\f"),
'\r' => out.push_str("\\r"),
c if c < ' ' => out.push_str(&format!("\\x{:02x}", c as u32)),
c => out.push(c),
}
}
out
}
impl Control {
/// `InputControl.MAP_CODEC`, dispatched on `type`.
fn parse(t: &Tag) -> Option<Self> {
let label = || t.get("label").map(text);
Some(match path(&string(t.get("type"))?) {
"text" => Self::Text {
width: int(t, "width", 200),
label: label()?,
label_visible: boolean(t, "label_visible", true),
initial: string(t.get("initial")).unwrap_or_default(),
max_length: int(t, "max_length", 32),
multiline: t.get("multiline").map(|m| {
let get = |key| number(m.get(key)).map(|v| v as i32);
(get("max_lines"), get("height"))
}),
},
"boolean" => Self::Boolean {
label: label()?,
initial: boolean(t, "initial", false),
on_true: string(t.get("on_true")).unwrap_or_else(|| "true".into()),
on_false: string(t.get("on_false")).unwrap_or_else(|| "false".into()),
},
"single_option" => Self::SingleOption {
width: int(t, "width", 200),
// SingleOptionInput.Entry.CODEC: a full entry or just its id.
options: list(t.get("options"))
.iter()
.filter_map(|o| match o {
Tag::String(id) => Some((id.to_string_lossy(), None, false)),
o => Some((
string(o.get("id"))?,
o.get("display").map(text),
boolean(o, "initial", false),
)),
})
.collect(),
label: label()?,
label_visible: boolean(t, "label_visible", true),
},
"number_range" => {
let float = |key| number(t.get(key)).map(|v| v as f32);
Self::NumberRange {
width: int(t, "width", 200),
label: label()?,
label_format: string(t.get("label_format"))
.unwrap_or_else(|| "options.generic_value".into()),
start: float("start")?,
end: float("end")?,
initial: float("initial"),
step: float("step"),
}
}
_ => return None,
})
}
/// The value it starts with.
pub fn initial(&self) -> InputValue {
match self {
Self::Text { initial, .. } => InputValue::Text(initial.clone()),
Self::Boolean { initial, .. } => InputValue::Bool(*initial),
Self::SingleOption { options, .. } => {
InputValue::Option(options.iter().position(|o| o.2).unwrap_or(0))
}
Self::NumberRange {
start,
end,
initial,
..
} => InputValue::Slider(to_slider(
initial.unwrap_or((start + end) / 2.0),
*start,
*end,
)),
}
}
/// `ValueGetter.asTemplateSubstitution`.
pub fn substitution(&self, value: &InputValue) -> String {
match (self, value) {
(Self::Text { .. }, InputValue::Text(s)) => escape(s),
(
Self::Boolean {
on_true, on_false, ..
},
InputValue::Bool(b),
) => if *b { on_true } else { on_false }.clone(),
(Self::SingleOption { options, .. }, InputValue::Option(i)) => {
options.get(*i).map(|o| o.0.clone()).unwrap_or_default()
}
(Self::NumberRange { .. }, InputValue::Slider(_)) => {
number_string(self.scaled(value).unwrap_or(0.0))
}
_ => String::new(),
}
}
/// `ValueGetter.asTag`.
pub fn tag(&self, value: &InputValue) -> Tag {
match (self, value) {
(Self::Boolean { .. }, InputValue::Bool(b)) => Tag::Byte(i8::from(*b)),
(Self::NumberRange { .. }, _) => {
Tag::Float(self.scaled(value).unwrap_or(0.0).to_bits())
}
(Self::Text { .. }, InputValue::Text(s)) => Tag::String(s.as_str().into()),
_ => Tag::String(self.substitution(value).as_str().into()),
}
}
/// `RangeInfo.computeScaledValue`: a number range's value at its slider position.
pub fn scaled(&self, value: &InputValue) -> Option<f32> {
let (
Self::NumberRange {
start,
end,
initial,
step,
..
},
InputValue::Slider(slider),
) = (self, value)
else {
return None;
};
let (start, end) = (*start, *end);
let in_range = start + slider * (end - start);
let Some(step) = step else {
return Some(in_range);
};
let first = initial.unwrap_or((start + end) / 2.0);
// Math.round: half up.
let steps = ((in_range - first) / step + 0.5).floor() as i32;
let result = first + steps as f32 * step;
let position = to_slider(result, start, end);
if (0.0..=1.0).contains(&position) {
return Some(result);
}
Some(first + (steps - steps.signum()) as f32 * step)
}
}
/// `RangeInfo.scaledValueToSlider`.
fn to_slider(value: f32, start: f32, end: f32) -> f32 {
if start == end {
0.5
} else {
(value - start) / (end - start)
}
}
/// `SliderImpl.valueToString`: whole numbers without a fraction, the rest as
/// Java's `Float.toString`.
pub fn number_string(v: f32) -> String {
if v == (v as i32) as f32 {
(v as i32).to_string()
} else {
crate::snbt::java_float(v)
}
}
impl Body {
/// `DialogBody.DIALOG_BODY_CODEC`.
fn parse(t: &Tag) -> Option<Self> {
let message = |t: &Tag| -> Option<(Text, i32)> {
// PlainMessage.CODEC: the full form or just its contents.
match t {
Tag::Compound(_) if t.get("contents").is_some() => {
Some((text(t.get("contents")?), int(t, "width", 200)))
}
t => Some((text(t), 200)),
}
};
Some(match path(&string(t.get("type"))?) {
"plain_message" => {
let (contents, width) = message(t)?;
Self::Message { contents, width }
}
"item" => Self::Item {
item: t.get("item")?.clone(),
description: t.get("description").and_then(message),
show_decorations: boolean(t, "show_decorations", true),
show_tooltip: boolean(t, "show_tooltip", true),
width: int(t, "width", 16),
height: int(t, "height", 16),
},
_ => return None,
})
}
}
impl Dialog {
/// `Dialog.DIRECT_CODEC`; `None` for a type or field it can't read
/// (vanilla fails the whole packet then).
pub fn parse(t: &Tag) -> Option<Self> {
let button = |key: &str| t.get(key).and_then(Button::parse);
let kind = match path(&string(t.get("type"))?) {
"notice" => Kind::Notice(match t.get("action") {
Some(b) => Button::parse(b)?,
None => Button::ok(),
}),
"confirmation" => Kind::Confirmation {
yes: button("yes")?,
no: button("no")?,
},
"multi_action" => Kind::MultiAction {
actions: list(t.get("actions"))
.iter()
.map(Button::parse)
.collect::<Option<Vec<_>>>()
.filter(|a| !a.is_empty())?,
exit: button("exit_action"),
columns: int(t, "columns", 2),
},
"server_links" => Kind::ServerLinks {
exit: button("exit_action"),
columns: int(t, "columns", 2),
button_width: int(t, "button_width", 150),
},
"dialog_list" => Kind::DialogList {
dialogs: t.get("dialogs")?.clone(),
exit: button("exit_action"),
columns: int(t, "columns", 2),
button_width: int(t, "button_width", 150),
},
_ => return None,
};
// DialogBody.COMPACT_LIST_CODEC: one body, or a list of them.
let body = match t.get("body") {
Some(Tag::List(items)) => items.iter().filter_map(Body::parse).collect(),
Some(one) => Body::parse(one).into_iter().collect(),
None => Vec::new(),
};
let inputs = list(t.get("inputs"))
.iter()
.filter_map(|i| {
Some(Input {
key: string(i.get("key"))?,
control: Control::parse(i)?,
})
})
.collect();
Some(Self {
kind,
title: text(t.get("title")?),
external_title: t.get("external_title").map(text),
can_close_with_escape: boolean(t, "can_close_with_escape", true),
pause: boolean(t, "pause", true),
after_action: match string(t.get("after_action")).as_deref() {
Some("none") => AfterAction::None,
Some("wait_for_response") => AfterAction::WaitForResponse,
_ => AfterAction::Close,
},
body,
inputs,
})
}
/// `onCancel`: what Escape (or closing it) does.
pub fn on_cancel(&self) -> Option<&Action> {
match &self.kind {
Kind::Notice(b) => b.action.as_ref(),
Kind::Confirmation { no, .. } => no.action.as_ref(),
Kind::MultiAction { exit, .. }
| Kind::ServerLinks { exit, .. }
| Kind::DialogList { exit, .. } => exit.as_ref().and_then(|b| b.action.as_ref()),
}
}
/// The buttons it draws, in order: the list, then the footer's.
pub fn buttons(&self) -> Vec<&Button> {
match &self.kind {
Kind::Notice(b) => vec![b],
Kind::Confirmation { yes, no } => vec![yes, no],
Kind::MultiAction { actions, exit, .. } => actions.iter().chain(exit).collect(),
Kind::ServerLinks { exit, .. } | Kind::DialogList { exit, .. } => exit.iter().collect(),
}
}
/// What `status` reports of it.
pub fn json(&self, values: &[InputValue]) -> Value {
let kind = match &self.kind {
Kind::Notice(_) => "notice",
Kind::Confirmation { .. } => "confirmation",
Kind::MultiAction { .. } => "multi_action",
Kind::ServerLinks { .. } => "server_links",
Kind::DialogList { .. } => "dialog_list",
};
let inputs: Vec<Value> = self
.inputs
.iter()
.zip(values)
.map(|(i, v)| {
let kind = match i.control {
Control::Text { .. } => "text",
Control::Boolean { .. } => "boolean",
Control::SingleOption { .. } => "single_option",
Control::NumberRange { .. } => "number_range",
};
json!({"key": i.key, "type": kind, "value": i.control.substitution(v)})
})
.collect();
json!({
"type": kind,
"title": self.title.plain(),
"body": self.body.iter().map(|b| match b {
Body::Message { contents, .. } => contents.plain(),
Body::Item { item, .. } => string(item.get("id")).unwrap_or_default(),
}).collect::<Vec<_>>(),
"inputs": inputs,
"buttons": self.buttons().iter().map(|b| b.label.plain()).collect::<Vec<_>>(),
"can_close_with_escape": self.can_close_with_escape,
"after_action": match self.after_action {
AfterAction::Close => "close",
AfterAction::None => "none",
AfterAction::WaitForResponse => "wait_for_response",
},
})
}
}
#[cfg(test)]
mod tests {
use super::*;
fn s(v: &str) -> Tag {
Tag::String(v.into())
}
/// The register dialog AuthMe 6 sends in configuration (the shape its
/// `show_dialog` packet has on the test bed).
fn register() -> Tag {
Tag::compound([
("type", s("minecraft:multi_action")),
("title", s("Register")),
("can_close_with_escape", Tag::Byte(0)),
("after_action", s("wait_for_response")),
(
"body",
Tag::compound([
("type", s("minecraft:plain_message")),
("contents", s("Choose a password")),
]),
),
(
"inputs",
Tag::List(vec![Tag::compound([
("type", s("minecraft:text")),
("key", s("password")),
("label", s("Password")),
])]),
),
(
"actions",
Tag::List(vec![
Tag::compound([
("label", s("Register")),
(
"action",
Tag::compound([
("type", s("minecraft:dynamic/custom")),
("id", s("authme:prejoin-register/submit")),
]),
),
]),
Tag::compound([
("label", s("Cancel")),
(
"action",
Tag::compound([
("type", s("minecraft:dynamic/run_command")),
("template", s("say $(password) $(missing)!")),
]),
),
]),
]),
),
])
}
#[test]
fn decodes_a_multi_action_dialog_with_defaults() {
let d = Dialog::parse(&register()).unwrap();
assert_eq!(d.title.plain(), "Register");
assert!(!d.can_close_with_escape);
assert!(d.pause);
assert_eq!(d.after_action, AfterAction::WaitForResponse);
assert!(matches!(&d.body[..], [Body::Message { width: 200, .. }]));
let Kind::MultiAction {
actions,
exit,
columns,
} = &d.kind
else {
panic!("{:?}", d.kind)
};
assert_eq!((actions.len(), exit.is_none(), *columns), (2, true, 2));
assert_eq!(actions[0].width, 150);
assert!(d.on_cancel().is_none());
let [
Input {
key,
control:
Control::Text {
max_length: 32,
width: 200,
..
},
},
] = &d.inputs[..]
else {
panic!("{:?}", d.inputs)
};
assert_eq!(key, "password");
}
#[test]
fn actions_carry_the_input_values() {
let d = Dialog::parse(&register()).unwrap();
let values = [InputValue::Text("p\"w".into())];
let Kind::MultiAction { actions, .. } = &d.kind else {
unreachable!()
};
let custom = actions[0]
.action
.as_ref()
.unwrap()
.click(&d.inputs, &values);
assert_eq!(
custom,
Some(Click::Custom {
id: "authme:prejoin-register/submit".into(),
payload: Some(Tag::compound([("password", s("p\"w"))])),
})
);
// The template escapes the text; a key no input has becomes nothing.
let command = actions[1]
.action
.as_ref()
.unwrap()
.click(&d.inputs, &values);
assert_eq!(command, Some(Click::RunCommand("say p\\\"w !".into())));
}
#[test]
fn a_notice_without_an_action_has_ok() {
let d = Dialog::parse(&Tag::compound([("type", s("notice")), ("title", s("Hi"))])).unwrap();
let Kind::Notice(b) = &d.kind else {
unreachable!()
};
assert_eq!(b.label, Text::translate("gui.ok", &[]));
// Without a title, or with an unknown type, there is no dialog.
assert!(Dialog::parse(&Tag::compound([("type", s("notice"))])).is_none());
assert!(Dialog::parse(&Tag::compound([("type", s("x")), ("title", s("Hi"))])).is_none());
}
#[test]
fn number_ranges_snap_to_their_step() {
let mut range = Control::NumberRange {
width: 200,
label: Text::literal("n"),
label_format: "options.generic_value".into(),
start: 0.0,
end: 10.0,
initial: Some(4.0),
step: Some(3.0),
};
assert_eq!(range.initial(), InputValue::Slider(0.4));
assert_eq!(range.scaled(&InputValue::Slider(0.62)), Some(7.0));
assert_eq!(range.scaled(&InputValue::Slider(1.0)), Some(10.0));
let Control::NumberRange { step, .. } = &mut range else {
unreachable!()
};
*step = Some(4.0);
// 12 is out of range: one step less.
assert_eq!(range.scaled(&InputValue::Slider(1.0)), Some(8.0));
assert_eq!(range.substitution(&InputValue::Slider(0.0)), "0");
assert_eq!(number_string(2.5), "2.5");
}
}
+382
View File
@@ -0,0 +1,382 @@
//! `DialogScreen` and its kinds (`SimpleDialogScreen`, `MultiButtonDialogScreen`):
//! where a server dialog's title, body, inputs and buttons go in a
//! `HeaderAndFooterLayout` (a 33 high header, the body in a `ScrollableLayout`,
//! the footer), and the `WaitingForResponseScreen` an answer can leave open.
use crate::{
dialog::{Body, Control, Dialog, Kind},
font::Font,
text::Text,
};
/// `HeaderAndFooterLayout.DEFAULT_HEADER_AND_FOOTER_HEIGHT`.
const BAR: f32 = 33.0;
/// `Checkbox.getBoxSize`.
pub const BOX: f32 = 17.0;
/// `DialogScreen.WARNING_BUTTON_SPRITES`: normal, disabled, highlighted.
pub const WARNING_SPRITES: [&str; 3] = [
"gui/sprites/dialog/warning_button",
"gui/sprites/dialog/warning_button_disabled",
"gui/sprites/dialog/warning_button_highlighted",
];
/// One drawn piece; `y` is from the top of the body (before it scrolls) for
/// the body's parts, from the top of the screen for the footer's.
#[derive(Clone, Debug, PartialEq)]
pub enum Part {
/// A line of text and where it starts.
Line(Text, f32, f32),
/// A text input's box.
Field { input: usize, rect: [f32; 4] },
/// A boolean input: the box, its label right of it.
Check { input: usize, rect: [f32; 4] },
/// A button of `Dialog::buttons`.
Button { index: usize, rect: [f32; 4] },
}
#[derive(Clone, Debug)]
pub struct Layout {
/// The title and where it is drawn.
pub title: (f32, f32),
pub warning: [f32; 4],
/// The body's visible area (`ScrollableLayout`'s container): x, y, w, h.
pub area: [f32; 4],
/// The body's full height: more than the area's scrolls.
pub content: f32,
pub body: Vec<Part>,
pub footer: Vec<Part>,
}
/// A laid-out element of a `LinearLayout` before it is placed.
struct Element {
w: f32,
h: f32,
parts: Vec<Part>,
}
impl Element {
/// Its parts moved to `(x, y)`.
fn at(self, x: f32, y: f32) -> impl Iterator<Item = Part> {
self.parts.into_iter().map(move |p| match p {
Part::Line(t, px, py) => Part::Line(t, px + x, py + y),
Part::Field { input, rect } => Part::Field {
input,
rect: shift(rect, x, y),
},
Part::Check { input, rect } => Part::Check {
input,
rect: shift(rect, x, y),
},
Part::Button { index, rect } => Part::Button {
index,
rect: shift(rect, x, y),
},
})
}
}
fn shift([x, y, w, h]: [f32; 4], dx: f32, dy: f32) -> [f32; 4] {
[x + dx, y + dy, w, h]
}
/// `FrameLayout.alignInDimension` at 0.5: the offset that centres `inner`.
fn centre(outer: f32, inner: f32) -> f32 {
((outer - inner) * 0.5).trunc()
}
/// `FocusableTextWidget` with the body's settings: `width` wide, the text
/// wrapped to `width - 8` and centred, 4 of padding all round.
fn message(font: &Font, contents: &Text, width: f32) -> Element {
let lines = font.split_lines(contents, width - 8.0);
let mid = (width / 2.0).trunc();
let parts = lines
.into_iter()
.enumerate()
.map(|(i, (line, _))| {
let x = mid - (font.width_text(&line) / 2.0).trunc();
Part::Line(line, x, 4.0 + 9.0 * i as f32)
})
.collect::<Vec<_>>();
Element {
w: width,
h: 9.0 * parts.len() as f32 + 8.0,
parts,
}
}
/// `InputControlHandlers`: the control for input `i`, if Horizon has it.
fn input(font: &Font, i: usize, control: &Control) -> Option<Element> {
Some(match control {
Control::Text {
width,
label,
label_visible,
..
} => {
let w = *width as f32;
if !label_visible {
return Some(Element {
w,
h: 20.0,
parts: vec![Part::Field {
input: i,
rect: [0.0, 0.0, w, 20.0],
}],
});
}
// CommonLayouts.labeledElement: the label, 4 below it the box,
// both on the left.
Element {
w: w.max(font.width_text(label)),
h: 9.0 + 4.0 + 20.0,
parts: vec![
Part::Line(label.clone(), 0.0, 0.0),
Part::Field {
input: i,
rect: [0.0, 13.0, w, 20.0],
},
],
}
}
Control::Boolean { label, .. } => {
let w = BOX + 4.0 + font.width_text(label);
Element {
w,
h: BOX,
parts: vec![Part::Check {
input: i,
rect: [0.0, 0.0, w, BOX],
}],
}
}
Control::SingleOption { .. } | Control::NumberRange { .. } => return None,
})
}
/// `DialogScreen.packControlsIntoColumns`: full rows in a grid 2 apart, what
/// is left in a centred row under them.
fn columns(widths: &[f32], columns: usize, first: usize) -> Element {
let columns = columns.max(1);
let full = widths.len() / columns * columns;
let row_width = |ws: &[f32]| ws.iter().sum::<f32>() + 2.0 * (ws.len().max(1) - 1) as f32;
let mut col = vec![0.0f32; columns];
for (i, w) in widths[..full].iter().enumerate() {
col[i % columns] = col[i % columns].max(*w);
}
let last = &widths[full..];
if !last.is_empty() {
// GridLayout spreads a cell over every column it spans (Divisor).
let spread = row_width(last) - 2.0 * (columns - 1) as f32;
let (each, extra) = ((spread / columns as f32).floor(), spread % columns as f32);
for (c, w) in col.iter_mut().enumerate() {
*w = w.max(each + f32::from(u8::from((c as f32) < extra)));
}
}
let x_of = |c: usize| col[..c].iter().sum::<f32>() + 2.0 * c as f32;
let mut parts = Vec::new();
for (i, w) in widths[..full].iter().enumerate() {
let (r, c) = (i / columns, i % columns);
parts.push(Part::Button {
index: first + i,
rect: [x_of(c) + centre(col[c], *w), 22.0 * r as f32, *w, 20.0],
});
}
let width = x_of(columns - 1) + col[columns - 1];
if !last.is_empty() {
let y = 22.0 * (full / columns) as f32;
let mut x = centre(width, row_width(last));
for (i, w) in last.iter().enumerate() {
parts.push(Part::Button {
index: first + full + i,
rect: [x, y, *w, 20.0],
});
x += w + 2.0;
}
}
let rows = full / columns + usize::from(!last.is_empty());
Element {
w: width,
h: (22.0 * rows as f32 - 2.0).max(0.0),
parts,
}
}
impl Layout {
/// `DialogScreen.init` and `repositionElements` on a `w` by `h` screen.
pub fn new(dialog: &Dialog, font: &Font, (w, h): (f32, f32)) -> Self {
// The header: the title, 10 right of it the warning button, both
// centred together.
let tw = font.width_text(&dialog.title);
let (hx, hy) = (centre(w, tw + 30.0), centre(BAR, 20.0));
let title = (hx, hy + centre(20.0, 9.0));
let warning = [hx + tw + 10.0, hy, 20.0, 20.0];
let mut elements: Vec<Element> = dialog
.body
.iter()
.filter_map(|b| match b {
Body::Message { contents, width } => Some(message(font, contents, *width as f32)),
Body::Item { .. } => None,
})
.collect();
elements.extend(
dialog
.inputs
.iter()
.enumerate()
.filter_map(|(i, input_)| input(font, i, &input_.control)),
);
let buttons = dialog.buttons();
let (listed, footer_buttons, footer_h) = match &dialog.kind {
Kind::MultiAction {
actions,
exit,
columns: n,
} => {
let widths: Vec<f32> = actions.iter().map(|b| b.width as f32).collect();
elements.push(columns(&widths, *n as usize, 0));
// ButtonListDialogScreen: the exit button in the footer, else a 5 high one.
let footer = exit.as_ref().map(|_| actions.len()).into_iter().collect();
(
actions.len(),
footer,
if exit.is_some() { BAR } else { 5.0 },
)
}
Kind::ServerLinks { exit, .. } | Kind::DialogList { exit, .. } => {
let footer = exit.as_ref().map(|_| 0).into_iter().collect();
(0, footer, if exit.is_some() { BAR } else { 5.0 })
}
// SimpleDialogScreen: the buttons side by side, 8 apart.
Kind::Notice(_) | Kind::Confirmation { .. } => {
(0, (0..buttons.len()).collect::<Vec<_>>(), BAR)
}
};
debug_assert!(listed + footer_buttons.len() <= buttons.len());
// The body: a column 10 apart, each element centred in it.
let body_w = elements.iter().map(|e| e.w).fold(0.0, f32::max);
let content = elements.iter().map(|e| e.h).sum::<f32>()
+ 10.0 * elements.len().saturating_sub(1) as f32;
// ScrollableLayout: 10 each side for the scrollbar (ReserveStrategy.BOTH),
// no higher than what the header and footer leave.
let area_w = body_w + 20.0;
let area_h = content.min(h - BAR - footer_h).max(0.0);
let area_y = (BAR + 30.0).min(h - footer_h - area_h);
let area_x = centre(w, area_w);
let mut body = Vec::new();
let mut y = 0.0;
for e in elements {
let (x, eh) = (area_x + 10.0 + centre(body_w, e.w), e.h);
body.extend(e.at(x, y));
y += eh + 10.0;
}
let widths: Vec<f32> = footer_buttons
.iter()
.map(|&i| buttons[i].width as f32)
.collect();
let row = widths.iter().sum::<f32>() + 8.0 * widths.len().saturating_sub(1) as f32;
let (mut x, fy) = (centre(w, row), h - footer_h + centre(footer_h, 20.0));
let mut footer = Vec::new();
for (&i, bw) in footer_buttons.iter().zip(&widths) {
footer.push(Part::Button {
index: i,
rect: [x, fy, *bw, 20.0],
});
x += bw + 8.0;
}
Self {
title,
warning: Self::in_bounds(warning, w, h),
area: [area_x, area_y, area_w, area_h],
content,
body,
footer,
}
}
/// `makeSureWarningButtonIsInBounds`.
fn in_bounds(r: [f32; 4], w: f32, h: f32) -> [f32; 4] {
if r[0] < 0.0 || r[1] < 0.0 || r[0] > w - 20.0 || r[1] > h - 20.0 {
[(w - 40.0).max(0.0), 5.0f32.min(h), 20.0, 20.0]
} else {
r
}
}
/// Every part where it is drawn with the body scrolled by `scroll`, and
/// whether it is in the body (clipped to its area).
pub fn placed(&self, scroll: f32) -> impl Iterator<Item = (Part, bool)> + '_ {
let dy = self.area[1] - scroll;
let body = self.body.iter().map(move |p| {
let p = match p.clone() {
Part::Line(t, x, y) => Part::Line(t, x, y + dy),
Part::Field { input, rect } => Part::Field {
input,
rect: shift(rect, 0.0, dy),
},
Part::Check { input, rect } => Part::Check {
input,
rect: shift(rect, 0.0, dy),
},
Part::Button { index, rect } => Part::Button {
index,
rect: shift(rect, 0.0, dy),
},
};
(p, true)
});
body.chain(self.footer.iter().map(|p| (p.clone(), false)))
}
}
/// `WaitingForResponseScreen`: its Back button's label after `ticks`, and
/// whether it shows and works (it shows after a second, works after five).
pub fn waiting_button(ticks: u32) -> (Text, bool, bool) {
let seconds = (ticks / 20).min(5);
let label = match seconds {
0 => Text::literal(""),
5 => Text::translate("gui.back", &[]),
s => Text::translate(
"gui.waitingForResponse.button.inactive",
&[Text::literal((5 - s).to_string())],
),
};
(label, seconds >= 1, seconds == 5)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn grids_pack_full_rows_and_centre_the_rest() {
// AuthMe's two buttons: one row, 2 apart.
let two = columns(&[150.0, 150.0], 2, 0);
assert_eq!((two.w, two.h), (302.0, 20.0));
// Three in two columns: the third alone, centred under them.
let three = columns(&[150.0, 150.0, 150.0], 2, 0);
assert_eq!((three.w, three.h), (302.0, 42.0));
assert_eq!(
three.parts[2],
Part::Button {
index: 2,
rect: [76.0, 22.0, 150.0, 20.0]
}
);
// One in two columns spreads over both.
let one = columns(&[100.0], 2, 0);
assert_eq!(one.w, 100.0);
}
#[test]
fn the_waiting_screens_button_counts_down() {
assert!(!waiting_button(0).1);
assert_eq!(
waiting_button(20).0,
Text::translate(
"gui.waitingForResponse.button.inactive",
&[Text::literal("4")]
)
);
assert_eq!(
waiting_button(100),
(Text::translate("gui.back", &[]), true, true)
);
}
}
+15 -5
View File
@@ -25,6 +25,20 @@ impl Culler {
vertices: &mut Vec<crate::mesher::Vertex>,
start: usize,
opaque: impl FnMut([i32; 3]) -> bool,
) -> bool {
let hidden = self.hidden(vertices, start, opaque);
if hidden {
vertices.truncate(start);
}
hidden
}
/// Whether this entity's completed camera-relative mesh (`vertices[start..]`) is hidden.
pub fn hidden(
&self,
vertices: &[crate::mesher::Vertex],
start: usize,
opaque: impl FnMut([i32; 3]) -> bool,
) -> bool {
if start >= vertices.len() || self.remaining.get() == 0 {
return false;
@@ -42,11 +56,7 @@ impl Culler {
}
}
// Cover floating-point and rasterization edges as well as the vertices.
if self.occluded(min.map(|v| v - 0.05), max.map(|v| v + 0.05), opaque) {
vertices.truncate(start);
return true;
}
false
self.occluded(min.map(|v| v - 0.05), max.map(|v| v + 0.05), opaque)
}
/// Returns true only when a complete opaque block plane covers the bounds.
+233 -27
View File
@@ -37,6 +37,7 @@ mod debug_keys;
mod decorated_pot;
mod dedicated;
mod deflect;
mod dialogs;
mod discord;
mod dispense_fixture;
mod dispenser;
@@ -352,10 +353,7 @@ impl Options {
dedicated: None,
benchmark: None,
dev_mods: Vec::new(),
// The command harness (HORIZON_COMMAND_SCRIPT) drives the world
// itself, without the event loop: it stays in the frame.
server_thread: std::env::var("HORIZON_SERVER_THREAD").map_or(true, |v| v != "0")
&& std::env::var_os("HORIZON_COMMAND_SCRIPT").is_none(),
server_thread: std::env::var("HORIZON_SERVER_THREAD").map_or(true, |v| v != "0"),
execution_role: execution_role::ExecutionRole::Client,
};
let mut session_file = std::env::var("HORIZON_SESSION_FILE").ok();
@@ -1146,6 +1144,7 @@ struct Game<'a> {
pending_pack_reload: bool,
/// A server's resource packs (game/server_packs.rs).
server_packs: server_packs::ServerPackState,
dialogs: dialogs::DialogState,
world_renderer_ready: bool,
collision: &'a BlockCollisionRegistry<'a>,
motion: &'a MotionResources,
@@ -1214,6 +1213,8 @@ struct Game<'a> {
left: bool,
right: bool,
cursor: (f32, f32),
/// The cursor icon the window shows (`widgets::take_cursor` each frame).
cursor_icon: horizon_client::widgets::Cursor,
captured: bool,
focused: bool,
/// The keyboard's modifier state (`KeyEvent.modifiers`).
@@ -1245,6 +1246,11 @@ struct Game<'a> {
luminance: Option<horizon_client::dynamic_light::Luminance>,
/// Name tags drawn over everything (`RenderPipelines.TEXT_SEE_THROUGH`).
see_through_vertices: Vec<Vertex>,
/// Runs of `entity_vertices` by entity, for a shader pack's per-entity draws; recorded only
/// while `pack_ids` is set (game/shader_packs.rs).
entity_spans: std::cell::RefCell<Vec<horizon_client::gfx::pack_draw::EntitySpan>>,
/// The shader pack's entity and block entity ids, while one draws entities.
pack_ids: Option<shader_packs::PackIds>,
started: Instant,
previous: Instant,
/// FramerateLimitTracker.latestInputTime.
@@ -1331,6 +1337,10 @@ struct Game<'a> {
hand: Hand,
/// The shield's item cooldown after an axe disabled it (ticks).
shield_cooldown: u32,
/// `Player.getCooldowns()`: the `use_cooldown` component's entries, what
/// `itemDecorations` shades a slot by. A server's players have their own
/// (`GuestPlay::cooldowns`), swapped in for their turn.
cooldowns: horizon_client::cooldowns::Cooldowns,
/// The source of a command run through /execute (the player otherwise).
command_source: Option<commands::execute::Source>,
titles: titles::Titles,
@@ -1503,6 +1513,9 @@ struct Game<'a> {
/// This frame's view frustum, for culling entities and block entities.
view_frustum: Option<horizon_client::gfx::Frustum>,
entity_culler: Option<horizon_client::entity_culling::Culler>,
/// `Renderer::pack_shadow_casters` for this frame's entities: (keep what the culler hides,
/// build the player's model in first person); (false, false) without a shader pack.
pack_shadow_casters: (bool, bool),
/// A scripted flight from the control socket (`fly`).
flight: Option<control::Flight>,
/// The running agent action from the control socket (game/agent.rs).
@@ -1571,6 +1584,9 @@ struct Game<'a> {
chat_capture: Option<Vec<horizon_client::text::Text>>,
/// Packets for the guest acting now, sent once its action returns.
guest_packets: Vec<horizon_protocol::packets::play::Clientbound>,
/// The acting guest's cooldowns, as (item, ticks): their packet needs the
/// hub's item registry, which the guest's turn does not hold.
guest_cooldowns: Vec<(Arc<str>, i32)>,
/// `broadcastToAdmins` lines a guest's command queued, sent once it
/// returns: the line, whether operators get it and whether the console
/// logs it.
@@ -2591,10 +2607,41 @@ impl Game<'_> {
}
}
}
// BigDripleafBlock.entityInside also asks whether the player stands on
// the ground and how high its feet are: the leaves its box's feet
// layer is inside (the host and the LAN guests).
let mut standing = Vec::new();
let feet = self
.player
.iter()
.filter(|_| !self.spectator())
.map(|p| {
(
[p.position.x, p.position.y, p.position.z],
p.ground.on_ground,
f64::from(p.dimensions.width) / 2.0,
)
})
.chain(
self.lan_guest_feet()
.into_iter()
.map(|(at, on_ground)| (at, on_ground, 0.3)),
);
for (at, on_ground, w) in feet {
let y = (at[1] + 1e-7).floor() as i32;
for x in (at[0] - w + 1e-7).floor() as i32..=(at[0] + w - 1e-7).floor() as i32 {
for z in (at[2] - w + 1e-7).floor() as i32..=(at[2] + w - 1e-7).floor() as i32 {
standing.push((BlockPos::new(x, y, z), on_ground, at[1]));
}
}
}
self.with_level(|b, level, ticks| {
for pos in inside {
b.entity_inside(level, ticks, pos);
}
for (pos, on_ground, feet_y) in standing {
b.entity_on_big_dripleaf(level, ticks, pos, on_ground, feet_y);
}
});
self.process_level_events()?;
self.tick_scheduled()?;
@@ -3416,6 +3463,25 @@ impl Game<'_> {
|| self.interact_mob()?
|| self.interact_knot())
}
/// `ItemStack.applyAfterUseComponentSideEffects`: a used stack with a
/// `use_cooldown` component starts its entry (`UseCooldown.apply`). A
/// server's guest also gets `ClientboundCooldownPacket`
/// (`ServerItemCooldowns.onCooldownStarted`), queued for their turn's end.
pub(super) fn apply_use_cooldown(&mut self, stack: &ItemStack) {
let default = self
.res
.items()
.get(&stack.item)
.and_then(|i| i.use_cooldown.clone());
let Some(component) = horizon_client::cooldowns::component(stack, default.as_ref()) else {
return;
};
let group = horizon_client::cooldowns::group(stack, default.as_ref());
self.cooldowns.add(Arc::clone(&group), component.ticks());
if self.lan_acting.is_some() {
self.guest_cooldowns.push((group, component.ticks()));
}
}
/// One hand's turn in `startUseItem`: `useItemOn` the block aimed at,
/// then `useItem`. Whether the click ends here (it acted or failed).
fn use_hand(&mut self, block_hit: Option<&Hit>) -> Result<bool> {
@@ -3500,6 +3566,17 @@ impl Game<'_> {
let Some(info) = self.res.items().get(&stack.item).cloned() else {
return Ok(false);
};
// ServerPlayerGameMode.useItem and useItemOn: an item on cooldown is
// not used (PASS, so the other hand may still act).
if self
.cooldowns
.is_on_cooldown(&horizon_client::cooldowns::group(
&stack,
info.use_cooldown.as_ref(),
))
{
return Ok(false);
}
if block_hit.is_some() && (self.use_bone_meal(&stack)? || self.ignite_tnt(&stack)?) {
return Ok(true);
}
@@ -3826,6 +3903,8 @@ impl Game<'_> {
}
if ticks >= duration {
self.using = None;
// ItemStack.finishUsingItem's applyAfterUseComponentSideEffects.
self.apply_use_cooldown(&stack);
self.finish_consuming(&info, consumable, &stack);
} else {
self.using = Some((slot, ticks));
@@ -4323,6 +4402,7 @@ impl Game<'_> {
MotionEvent::Snowflake { position, velocity } => self.snowflake(position, velocity),
MotionEvent::EnterBlock { state } => self.trigger_enter_block(state),
MotionEvent::InsidePortal { pos } => self.player_inside_nether_portal(pos),
MotionEvent::CauldronExtinguish { pos } => self.cauldron_extinguish(pos),
MotionEvent::InsideWitherRose => self.wither_rose_contact(),
MotionEvent::SmashLanding {
state,
@@ -4428,6 +4508,7 @@ impl Game<'_> {
// Player.tick's ItemCooldowns.tick for the window's player (a
// server's players count theirs down in `tick_guests`).
self.shield_cooldown = self.shield_cooldown.saturating_sub(1);
self.cooldowns.tick();
horizon_client::spans::time("tick_using", || self.tick_using());
horizon_client::spans::time("tick_hud", || self.tick_hud());
// The player's digging comes in with its packets, before the level
@@ -4705,6 +4786,24 @@ impl Game<'_> {
}
window.set_cursor_visible(!self.captured);
}
/// `GuiGraphicsExtractor.applyCursor` -> `Window.selectCursor` at the end
/// of a frame: the cursor the frame's widgets asked for, set on the window
/// only when it changes.
fn apply_cursor_icon(&mut self) {
use horizon_client::widgets::Cursor;
let wanted = horizon_client::widgets::take_cursor();
if wanted == self.cursor_icon {
return;
}
if let Some(window) = &self.window {
window.set_cursor(match wanted {
Cursor::Arrow => winit::window::CursorIcon::Default,
Cursor::PointingHand => winit::window::CursorIcon::Pointer,
Cursor::NotAllowed => winit::window::CursorIcon::NotAllowed,
});
}
self.cursor_icon = wanted;
}
/// LevelLoadingScreen.onClose -> Gui.setScreen(null) -> grabMouse.
fn capture_loaded_world(&mut self) {
// Loading may finish after focus was lost or another screen opened.
@@ -4810,17 +4909,17 @@ impl Game<'_> {
count_text: Option<String>,
) {
// GuiGraphicsExtractor.itemDecorations, laid out at the origin and
// moved to (x, y). Horizon tracks no item cooldowns to shade yet.
let max_damage = self
.res
.items()
.get(&stack.item)
.map_or(0, |info| info.max_damage as i32);
// moved to (x, y): the damage bar, the cooldown shade and the count.
let info = self.res.items().get(&stack.item);
let max_damage = info.map_or(0, |info| info.max_damage as i32);
let cooldown =
horizon_client::cooldowns::group(stack, info.and_then(|i| i.use_cooldown.as_ref()));
let partial = self.clock.fraction() as f32;
let decorations = hud_overlays::ItemDecorations {
count: stack.count,
count_text,
bar: hud_overlays::damage_bar(stack.damage as i32, max_damage),
cooldown: 0.0,
cooldown: self.cooldowns.percent(&cooldown, partial),
};
let mut draws = vec![hud_layout::GuiDraw::Push(vec![
hud_layout::PoseOp::Translate(x, y),
@@ -4837,11 +4936,14 @@ impl Game<'_> {
fn hud(&mut self, size: [u32; 2]) -> HudBatch {
// The pack prompt replaces the connecting screen (setScreen), but
// in game it opens over the world.
let mut batch = if self.server_packs.prompt.is_some() && self.player.is_none() {
let mut batch = if (self.server_packs.prompt.is_some() || self.dialog_open())
&& self.player.is_none()
{
HudBatch::new(size)
} else {
self.hud_layers(size)
};
self.draw_dialog(&mut batch);
self.draw_pack_prompt(&mut batch);
// Gui.extractRenderState: the debug overlay comes after the screen
// and the toasts, so it is over everything, menus included.
@@ -5155,7 +5257,8 @@ impl Game<'_> {
| BlockEntity::TrialSpawner(_)
| BlockEntity::Vault(_)
| BlockEntity::Beehive(_)
| BlockEntity::CommandBlock(_),
| BlockEntity::CommandBlock(_)
| BlockEntity::Skull(_),
) => {}
None => {
v.extend((0..self.craft_size * self.craft_size).map(|i| 100 + i));
@@ -5424,7 +5527,8 @@ impl Game<'_> {
| BlockEntity::TrialSpawner(_)
| BlockEntity::Vault(_)
| BlockEntity::Beehive(_)
| BlockEntity::CommandBlock(_),
| BlockEntity::CommandBlock(_)
| BlockEntity::Skull(_),
) => {}
None if self.mount_active() => self.draw_mount_background(batch, ox, oy),
None => {
@@ -6762,8 +6866,10 @@ impl Game<'_> {
alpha: f64,
mut out: Vec<Vertex>,
) -> Vec<Vertex> {
self.entity_spans.borrow_mut().clear();
self.prepare_entity_culling(camera);
self.prepare_trims();
self.mark_entity(out.len(), "minecraft:entity_flame");
let fire_yaw = if self.camera_type == perspective::CameraType::ThirdPersonFront {
self.yaw + 180.0
} else {
@@ -6855,14 +6961,21 @@ impl Game<'_> {
}
}
// `ItemEntityRenderer`.
self.mark_entity(out.len(), "minecraft:item");
self.dropped_item_geometry(camera, alpha, &mut out);
// `VaultRenderer`.
self.mark_entity(out.len(), "");
self.vault_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:ominous_item_spawner");
self.ominous_item_spawner_geometry(camera, alpha, &mut out);
// Mobs and their projectiles.
// Mobs and their projectiles (a span each).
self.mob_geometry(camera, alpha, &mut out);
// In first person only a shader pack's shadow pass draws the player.
self.mark_player(out.len(), self.camera_type.first_person());
self.player_model_geometry(camera, self.clock.camera_fraction(), &mut out);
self.mark_entity(out.len(), "minecraft:player");
self.guest_model_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:evoker_fangs");
self.fangs_geometry(camera, alpha, &mut out);
let mut see_through = std::mem::take(&mut self.see_through_vertices);
see_through.clear();
@@ -6870,6 +6983,7 @@ impl Game<'_> {
self.text_display_geometry(camera, alpha, &mut out, &mut see_through);
self.see_through_vertices = see_through;
// Entity shadows (EntityRenderer.extractShadow).
self.mark_translucent_entity(out.len(), "minecraft:entity_shadow");
if let Some(sprite) = self.gui.atlas.sprites.get("misc/shadow").copied() {
let ambient = if self.dim == Dim::Nether { 0.1 } else { 0.0 };
let darken = self.sky_darken();
@@ -6933,6 +7047,7 @@ impl Game<'_> {
_ => None,
},
banner: &|p| self.banner_layers_at(p),
skull: &|p| self.skull_skin_at(p),
glyph: &|c| self.gui.atlas.glyph_cell(c),
};
// Block entities outside the view frustum are skipped (with a
@@ -6963,13 +7078,14 @@ impl Game<'_> {
v.intersects(o - glam::Vec3::ONE, o + glam::Vec3::splat(2.0))
})
});
if self.entity_culler.is_some() {
if self.entity_culler.is_some() || self.pack_ids.is_some() {
for block in visible {
let start = out.len();
self.mark_block_entity(start, block.1);
self.res
.block_entity_render()
.render(&scene, std::iter::once(block), &mut out);
if !off_screen(block.1) {
if !off_screen(block.1) && self.entity_culler.is_some() {
self.cull_entity_vertices(&mut out, start);
}
}
@@ -6978,6 +7094,7 @@ impl Game<'_> {
.block_entity_render()
.render(&scene, visible, &mut out);
}
self.mark_stage(out.len(), "DESTROY");
for (pos, stage) in self.cracks(camera) {
let state = self.world.block(pos[0], pos[1], pos[2]);
self.res
@@ -6986,6 +7103,7 @@ impl Game<'_> {
}
}
// Experience orbs (ExperienceOrbRenderer): pulsing billboards from a 4x4 icon sheet.
self.mark_entity(out.len(), "minecraft:experience_orb");
if let Some(r) = self
.gui
.atlas
@@ -7049,6 +7167,7 @@ impl Game<'_> {
}
}
// Falling blocks use their baked model, including non-cubic shapes.
self.mark_entity(out.len(), "minecraft:falling_block");
for f in &self.falling {
let Some((_, tint)) = self.block_face_sprite(f.state) else {
continue;
@@ -7079,20 +7198,35 @@ impl Game<'_> {
vertex.color = horizon_client::hud_layout::multiply(vertex.color, tint);
}
}
self.mark_entity(out.len(), "");
self.moving_piston_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:tnt");
self.tnt_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:painting");
self.painting_geometry(camera, &mut out);
self.mark_entity(out.len(), "minecraft:item_frame");
self.frame_geometry(camera, &mut out);
self.mark_entity(out.len(), "minecraft:armor_stand");
self.armor_stand_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:oak_boat");
self.boat_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:minecart");
self.cart_geometry(camera, alpha, &mut out);
// Beams: `gbuffers_beaconbeam`, not a block entity's program (5.4 has no stage for them).
self.mark_stage(out.len(), "NONE");
self.beacon_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:fishing_bobber");
self.fishing_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:leash_knot");
self.leash_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:trident");
self.trident_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:firework_rocket");
self.rocket_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:splash_potion");
self.thrown_potion_geometry(camera, alpha, &mut out);
// Particles (ParticleEngine.extract): camera-facing quads.
self.mark_stage(out.len(), "PARTICLES");
let world = &self.world;
self.particles.render(
camera,
@@ -7103,6 +7237,7 @@ impl Game<'_> {
&mut out,
);
// 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) {
let o = (glam::DVec3::new(f64::from(pos[0]), f64::from(pos[1]), f64::from(pos[2]))
- camera)
@@ -8439,6 +8574,35 @@ impl Game<'_> {
self.level_set(pos, id)?;
Ok(true)
}
/// `LayeredCauldronBlock.handleEntityOnFireInside` for a burning player
/// in the cauldron at `pos` (`mayInteract`: not a spectator): the fill
/// level drops (`lowerFillLevel`, a `block_change` with no source). A
/// server's cauldron is the server's.
pub(super) fn cauldron_extinguish(&mut self, pos: BlockPos) {
if self.remote.is_some() {
return;
}
let at = [pos.x, pos.y, pos.z];
let registry = self.pack.assets.registry;
let Some(old) = registry
.state(self.world.block(at[0], at[1], at[2]))
.filter(|s| {
matches!(
s.name.as_str(),
"minecraft:water_cauldron" | "minecraft:powder_snow_cauldron"
)
})
.cloned()
else {
return;
};
let Ok(new) = registry.resolve(&horizon_client::cauldron::extinguished(&old)) else {
return;
};
if self.level_set(at, new).is_ok() {
self.post_block_game_event("minecraft:block_change", at, None, Some(new));
}
}
fn damage_held(&mut self, amount: u32) {
if self.creative {
return;
@@ -9220,6 +9384,9 @@ impl Game<'_> {
)
}
fn apply_settings(&mut self) {
// LocalPlayer.refreshChatAbilities: the chat shows what the Chat option allows.
let abilities = self.chat_abilities();
self.ui.chat.set_abilities(&self.gui.atlas.font, abilities);
self.streaming.radius = self.settings.render_distance;
// Options.biomeBlendRadius, ambientOcclusion and cutoutLeaves:
// LevelExtractor.allChanged when they change.
@@ -10214,6 +10381,7 @@ impl Game<'_> {
} else {
self.shader_pack_inputs(camera, alpha as f32)
};
let entity_spans = std::mem::take(self.entity_spans.get_mut());
let frame = Frame {
weather: &weather,
weather_time: horizon_client::weather::Time::new(self.world.game_time, alpha as f32),
@@ -10278,6 +10446,7 @@ impl Game<'_> {
color: sky.cloud_color,
}),
entities: &entities,
entity_spans: &entity_spans,
dynamic_lights: &self.dynamic_lights,
lightning: horizon_client::gfx::lightning::Input {
bolts: &bolts,
@@ -10329,6 +10498,7 @@ impl Game<'_> {
};
// Restore storage even when rendering fails (for example, a lost surface).
self.entity_vertices = entities;
*self.entity_spans.get_mut() = entity_spans;
self.see_through_vertices = see_through;
result
}
@@ -10400,6 +10570,7 @@ impl Game<'_> {
|| (self.title.is_none()
&& self.player.is_some()
&& (self.screen.blurs_background(self.pause_menu)
|| self.dialog_open()
|| (self.screen == Screen::Win && self.win_screen_pauses())));
let radius = self.settings.menu_blur as f32;
(blurred && radius >= 1.0).then_some(radius)
@@ -11060,6 +11231,12 @@ impl ApplicationHandler for Game<'_> {
if self.screen == Screen::Advancements {
self.advancements_drag();
}
if self.screen == Screen::Stats {
self.stats_drag();
}
if self.screen == Screen::WorldOptions {
self.world_options_drag();
}
if self.screen == Screen::Creative {
let (_, oy) = Self::creative_origin(&b);
self.creative_screen.drag_scrollbar(self.cursor.1 - oy);
@@ -11096,7 +11273,9 @@ impl ApplicationHandler for Game<'_> {
}) * wheel_sensitivity
};
let d = scaled(raw_y) as f32;
if let Some(title) = &mut self.title
if self.dialog_open() {
self.dialog_scroll(d);
} else if let Some(title) = &mut self.title
&& self.screen != Screen::Win
{
let size = self
@@ -11110,6 +11289,7 @@ impl ApplicationHandler for Game<'_> {
if self.screen == Screen::Playing
&& self.sleeping.is_none()
&& self.player.is_some()
&& !self.mod_wheel(d)
&& !self.extras_scroll(d)
&& let Some((wheel, (_, vertical_steps))) =
self.mouse
@@ -11166,6 +11346,11 @@ impl ApplicationHandler for Game<'_> {
self.pack_prompt_click()?;
}
}
WindowEvent::MouseInput { state, button, .. } if self.dialog_open() => {
if state == ElementState::Pressed && button == MouseButton::Left {
self.dialog_click()?;
}
}
WindowEvent::MouseInput { .. } if self.screen == Screen::Win => {}
WindowEvent::MouseInput { state, button, .. } if self.screen == Screen::Options => {
if self.options_waiting_for_key() && state == ElementState::Pressed {
@@ -11280,6 +11465,7 @@ impl ApplicationHandler for Game<'_> {
return Ok(());
}
self.extras_mouse(button, pressed);
self.mod_mouse_key(button, pressed);
match self.settings.keys.translate_mouse(button) {
Some(Err(b)) => self.play_mouse(b, pressed)?,
Some(Ok(k)) if pressed => {
@@ -11339,6 +11525,9 @@ impl ApplicationHandler for Game<'_> {
Screen::WorldOptions if pressed && button == MouseButton::Left => {
self.world_options_click()
}
Screen::WorldOptions if button == MouseButton::Left => {
self.world_options_release()
}
Screen::Friends if pressed && button == MouseButton::Left => {
self.friends_screen_click()
}
@@ -11362,6 +11551,7 @@ impl ApplicationHandler for Game<'_> {
.map_or([1, 1], |r| [r.config.width, r.config.height]);
self.stats_click(size);
}
Screen::Stats if button == MouseButton::Left => self.stats_release(),
Screen::Mod if pressed && button == MouseButton::Left => {
self.mod_screen_click()
}
@@ -11376,6 +11566,14 @@ impl ApplicationHandler for Game<'_> {
self.answer_pack_prompt(false)?;
}
}
WindowEvent::KeyboardInput { event, .. } if self.dialog_open() => {
let shortcut = self.edit_shortcut(&event);
if event.state == ElementState::Pressed
&& let PhysicalKey::Code(key) = event.physical_key
{
self.dialog_key(key, shortcut, event.text.as_deref())?
}
}
WindowEvent::KeyboardInput { event, .. }
if self.screen == Screen::Chat && self.title.is_none() =>
{
@@ -11605,6 +11803,7 @@ impl ApplicationHandler for Game<'_> {
self.advance_flight();
let ticked = Instant::now();
let rendered = self.render()?;
self.apply_cursor_icon();
if let Some(threshold) = self.options.spikes
&& let Some(r) = self.renderer.as_ref()
{
@@ -11858,6 +12057,7 @@ impl<'a> Game<'a> {
world_loader: None,
pending_pack_reload: false,
server_packs: Default::default(),
dialogs: Default::default(),
world_renderer_ready: false,
collision: res.collision(),
motion: res.motion(),
@@ -11912,6 +12112,7 @@ impl<'a> Game<'a> {
left: false,
right: false,
cursor: (0.0, 0.0),
cursor_icon: horizon_client::widgets::Cursor::Arrow,
captured: false,
focused: true,
modifiers: winit::keyboard::ModifiersState::empty(),
@@ -11930,6 +12131,8 @@ impl<'a> Game<'a> {
dynamic_lights: Vec::new(),
luminance: None,
see_through_vertices: Vec::new(),
entity_spans: Default::default(),
pack_ids: None,
health_hud: hud_layout::HealthHud::default(),
started: now,
previous: now,
@@ -11987,6 +12190,7 @@ impl<'a> Game<'a> {
offhand: None,
hand: Hand::Main,
shield_cooldown: 0,
cooldowns: Default::default(),
command_source: None,
titles: Default::default(),
tool_highlight: Default::default(),
@@ -12102,6 +12306,7 @@ impl<'a> Game<'a> {
agent: agent::Agent::default(),
view_frustum: None,
entity_culler: None,
pack_shadow_casters: (false, false),
pending_uploads: Vec::new(),
frame_graph: Default::default(),
extras: Default::default(),
@@ -12136,6 +12341,7 @@ impl<'a> Game<'a> {
lid_openers: HashMap::new(),
lan_acting: None,
guest_packets: Vec::new(),
guest_cooldowns: Vec::new(),
lan_acting_name: None,
lan_acting_uuid: None,
chat_capture: None,
@@ -12865,6 +13071,7 @@ fn loading_frame(hud: &HudBatch) -> Frame<'_> {
hud,
nether_lighting: false,
entities: &[],
entity_spans: &[],
dynamic_lights: &[],
clouds: None,
lightning: Default::default(),
@@ -12967,11 +13174,11 @@ fn start_game<'a>(
);
}
if let Ok(spec) = std::env::var("HORIZON_DISPENSE_FIXTURE") {
game.dispense_fixture(&spec)?;
game.run_component_harness(integrated::ComponentHarness::Dispenser(spec))?;
return Ok(None);
}
if let Ok(spec) = std::env::var("HORIZON_SCULK_FIXTURE") {
game.sculk_fixture(&spec)?;
game.run_component_harness(integrated::ComponentHarness::Sculk(spec))?;
return Ok(None);
}
if game.options.execution_role == execution_role::ExecutionRole::DedicatedServer {
@@ -12993,8 +13200,8 @@ fn start_game<'a>(
Some(folder) => {
let (seed, creative) = (game.options.seed, game.options.creative);
if std::env::var_os("HORIZON_COMMAND_SCRIPT").is_some() {
// The command harness runs before entering the window loop.
game.open_world(&folder, None, Some(seed), creative, None)?;
// Headless, but with the same server and memory connection as play.
game.open_integrated(&folder, None, Some(seed), creative, None)?;
} else {
// Start after the window is handed over, so --world uses the
// same responsive initialization path as the title screen.
@@ -13021,11 +13228,10 @@ fn start_game<'a>(
game.title = Some(title);
}
}
if let (Some(script), Some(output)) = (
std::env::var_os("HORIZON_COMMAND_SCRIPT"),
std::env::var_os("HORIZON_COMMAND_OUTPUT"),
) {
game.run_command_harness(script.as_ref(), output.as_ref())?;
if std::env::var_os("HORIZON_COMMAND_SCRIPT").is_some()
&& std::env::var_os("HORIZON_COMMAND_OUTPUT").is_some()
{
game.wait_command_harness()?;
return Ok(None);
}
Ok(Some(game))
+16
View File
@@ -390,6 +390,9 @@ impl Game<'_> {
Ok(())
}
pub(super) fn screen_label(&self) -> &'static str {
if self.dialog_open() {
return "dialog";
}
if self.title.is_some() {
return "title";
}
@@ -485,6 +488,7 @@ impl Game<'_> {
})
});
json!({
"dialog": self.dialog_status(),
"screen": self.screen_label(),
"menu": self.title.as_ref().map(|_| self.title_value()),
"world": (self.title.is_none()).then(|| match &self.integrated {
@@ -682,6 +686,18 @@ impl Game<'_> {
v["craft_size"] = json!(self.craft_size);
v["station"] = json!(self.station.as_ref().map(|s| format!("{:?}", s.kind)));
v["container"] = json!(self.container);
// The title a server gave the menu, span by span as it is drawn.
if let Some(title) = self.remote_menu_title() {
let spans = title.spans.iter().map(|s| {
json!({
"text": s.text,
"color": s.style.color.map(|c| format!("#{c:06X}")),
"bold": s.style.is_bold(),
"italic": s.style.is_italic(),
})
});
v["title"] = json!(spans.collect::<Vec<_>>());
}
// The recipe book's buttons (GUI pixels): the book button, then
// the recipe buttons of the page, page arrows, filter and tabs.
let book = self.recipe_book_targets();
@@ -8,8 +8,10 @@ use horizon_client::{
block_entity_anim::{BellEntity, EndGateway, Shulker, ShulkerStatus, Ticker},
block_entity_render::BlockAnim,
collisions::{SceneCollisions, no_collision_empty},
containers::BlockEntity,
particles::{ParticleOptions, ParticleType},
save::number,
skins,
};
use horizon_core::geometry::{Aabb, Vec3};
use horizon_world::nbt::Tag;
@@ -392,4 +394,25 @@ impl Game<'_> {
pub(super) fn chest_lid(&self, pos: [i32; 3], partial: f32) -> f32 {
self.be_anims.lid(pos, partial)
}
/// `SkullBlockRenderer.getPlayerSkin`: the skin sprite of the profile of
/// the player head at `pos`. The skin download is started here; while it
/// is in flight (or when it cannot be got) the profile's default skin
/// stands in. A head with no profile answers None: `SKIN_BY_TYPE`.
pub(super) fn skull_skin_at(&self, pos: [i32; 3]) -> Option<&str> {
let Some(BlockEntity::Skull(skull)) =
self.world.block_entities.get(&(pos[0], pos[1], pos[2]))
else {
return None;
};
let skin = skull.skin.as_ref()?;
if let Some((url, location)) = &skin.download {
self.skin_downloads
.borrow_mut()
.state(skins::Kind::Skin, url);
if self.gui.atlas.sprites.contains_key(location) {
return Some(location);
}
}
Some(skin.fallback)
}
}
+36 -8
View File
@@ -2,7 +2,10 @@
//! (game rules, the world clock, difficulty), bound to the game.
use super::{Game, Screen};
use horizon_client::{
chat::{Chat, ChatAction, ChatScreen, Commands, SYSTEM_INDICATOR},
chat::{
Chat, ChatAbilities, ChatAction, ChatScreen, ChatSource, ChatVisibility, Commands,
SYSTEM_INDICATOR,
},
command::Registries,
gamerules::GameRules,
hud::HudBatch,
@@ -425,6 +428,7 @@ impl Game<'_> {
self.ui.chat.recent.len(),
b.width as i32,
b.height as i32,
self.chat_abilities(),
);
screen.auto_suggestions = self.settings.auto_suggestions;
self.ui.chat_screen = Some(screen);
@@ -443,6 +447,10 @@ impl Game<'_> {
self.capture(true);
}
}
/// `Minecraft.computeChatAbilities` from the Chat option.
pub(super) fn chat_abilities(&self) -> ChatAbilities {
ChatAbilities::new(ChatVisibility::from_option(self.settings.chat_visibility))
}
/// A chat message from the game (ChatComponent.addServerSystemMessage).
pub(super) fn system_message(&mut self, text: Text) {
if let Some(capture) = self.chat_capture.as_mut() {
@@ -452,7 +460,11 @@ impl Game<'_> {
// Connected to a server (single player's own included), the chat on
// screen is the connection's: the game's own messages join it.
if self.remote.is_some() {
self.chat_line(text);
self.chat_line(text, ChatSource::Server);
return;
}
// The Chat option's Hidden level takes the server's messages too.
if !self.ui.chat.shows(ChatSource::Server) {
return;
}
let focused = self.screen == Screen::Chat;
@@ -461,6 +473,7 @@ impl Game<'_> {
&self.gui.atlas,
text,
Some(SYSTEM_INDICATOR),
ChatSource::Server,
self.ticks,
focused,
);
@@ -570,7 +583,12 @@ impl Game<'_> {
}
}
/// ChatScreen.handleChatInput: history, then a command or a chat line.
/// The Chat option's restrictions stop the message here too, so a
/// control-socket `chat` cannot send what the player's setting hides.
pub(super) fn handle_chat_input(&mut self, message: &str) {
if !self.chat_abilities().allows(message) {
return;
}
self.ui.chat.add_recent(message);
// On a server (single player's own included) the server runs it,
// unless a client mod owns the command.
@@ -592,9 +610,14 @@ impl Game<'_> {
);
let focused = self.screen == Screen::Chat;
let heard = self.mods.running().then(|| line.plain());
self.ui
.chat
.add(&self.gui.atlas, line, None, self.ticks, focused);
self.ui.chat.add(
&self.gui.atlas,
line,
None,
ChatSource::Player,
self.ticks,
focused,
);
if let Some(line) = heard {
self.mods_chat(&line);
}
@@ -898,9 +921,14 @@ impl Game<'_> {
Err(e) => Text::translate("screenshot.failure", &[Text::literal(e.clone())]),
};
let focused = self.screen == Screen::Chat;
self.ui
.chat
.add(&self.gui.atlas, message, None, self.ticks, focused);
self.ui.chat.add(
&self.gui.atlas,
message,
None,
ChatSource::Client,
self.ticks,
focused,
);
}
}
/// `yyyy-MM-dd_HH.mm.ss[_n].png` (Util.getFilenameFormattedDateTime, local time
+114 -27
View File
@@ -1,6 +1,7 @@
//! Headless command runner for `tools/test_command_exec_parity.py`: with
//! `HORIZON_COMMAND_SCRIPT` and `HORIZON_COMMAND_OUTPUT` set, the opened world
//! runs without a window. Each script line is a JSON operation: `cmd` runs a
//! runs on its server thread with a connected, headless client. Each script
//! line is a JSON operation: `cmd` runs a
//! command as the player (Commands.performPrefixedCommand) and records every
//! system message it produced, as styled spans, with the results reported to
//! the player's `CommandResultCallback`; `tick` advances the game; `settle`
@@ -41,30 +42,100 @@ pub(super) fn spans(text: &Text) -> Value {
}
impl Game<'_> {
/// Pump the real client's connection, without predicting movement: the
/// oracle's player stays where commands put it. Errors cross the server
/// boundary through Integrated::stop, including script and save failures.
pub(super) fn wait_command_harness(&mut self) -> Result<()> {
let server = self.integrated.take().ok_or("command harness: no server")?;
while !server.stopped() {
self.poll_stationary_client()?;
std::thread::sleep(Duration::from_millis(2));
}
server.stop()
}
/// The owner's authoritative body and level, using the same swaps as a
/// server command. Always put them back, also when a script operation fails.
fn harness_owner<T>(&mut self, f: impl FnOnce(&mut Self) -> Result<T>) -> Result<T> {
let owner = self
.serving
.as_ref()
.ok_or("command harness: not a server")?
.owner;
let mut host = self.lan.take().ok_or("command harness: no hub")?;
let guest = host.hub.guests.iter().find(|g| g.uuid == owner && g.placed);
let Some(guest) = guest else {
self.lan = Some(host);
return Err("command harness: owner is not connected".into());
};
let (conn, dim, back) = (guest.conn, super::Dim::from_id(&guest.dimension), self.dim);
if let Err(error) = self.enter_level(Some(&mut host), dim) {
self.lan = Some(host);
return Err(error);
}
let swap = self
.enter_guest(&mut host, conn)
.expect("the owner is not already acting");
self.lan = Some(host);
let result = f(self);
let mut host = self.lan.take().expect("the operation puts the hub back");
self.leave_guest(&mut host, swap);
host.hub.sync_all_effects();
self.run_level_teleports(&mut host);
let restored = self.enter_level(Some(&mut host), back);
self.lan = Some(host);
restored?;
result
}
/// The player's portal process finished in the nether portal it stands
/// in (the oracle's player is not ticked, so neither is the wait).
fn harness_portal(&mut self) -> Result<()> {
let Some(p) = &self.player else {
return Err("command harness: portal without a player".into());
};
let entry = horizon_core::position::BlockPos::containing(p.position);
let block = self.world.block(entry.x, entry.y, entry.z);
if &*self.res.facts().get(block).name != "minecraft:nether_portal" {
return Err("command harness: the player is not in a nether portal".into());
}
self.player_through_nether_portal(entry)
let (conn, plan) = self.harness_owner(|g| {
let p = g
.player
.as_ref()
.ok_or("command harness: portal without a player")?;
let entry = horizon_core::position::BlockPos::containing(p.position);
let block = g.world.block(entry.x, entry.y, entry.z);
if &*g.res.facts().get(block).name != "minecraft:nether_portal" {
return Err("command harness: the player is not in a nether portal".into());
}
let plan = g.portal_plan(
entry,
p.position,
p.dimensions.width,
p.dimensions.height,
true,
);
Ok((
g.lan_acting.ok_or("command harness: no acting owner")?,
plan,
))
})?;
// Start travel after restoring the owner's body: change_level updates
// its hub record, which leave_guest would otherwise overwrite.
self.in_owner_level(|g| {
let mut host = g.lan.take().ok_or("command harness: no hub")?;
g.start_server_trip(&mut host, conn, plan.dim, Some(plan));
g.lan = Some(host);
Ok(())
})
}
/// Load terrain until the player stands in lit chunks.
fn harness_settle(&mut self, deadline: Instant) -> Result<()> {
loop {
self.update_world();
self.spawn_player()?;
if let Some(p) = &self.player {
let (cx, cz) = (
(p.position.x.floor() as i32) >> 4,
(p.position.z.floor() as i32) >> 4,
);
if (-1..=1).all(|dz| (-1..=1).all(|dx| self.streaming.is_lit(cx + dx, cz + dz))) {
// Finish requested terrain before inspecting it, so worker
// timing cannot change which command positions are loaded.
if !self.streaming.busy()
&& (-1..=1).all(|dz| (-1..=1).all(|dx| self.streaming.is_lit(cx + dx, cz + dz)))
{
// The oracle's settle also waits for the entities saved
// in those chunks (areEntitiesActuallyLoadedAndTicking):
// after a dimension change they come back from the save.
@@ -95,7 +166,19 @@ impl Game<'_> {
.and_then(|v| v.parse().ok())
.unwrap_or(600),
);
self.harness_settle(deadline)?;
// Join/configuration uses the normal hub, but never advances the world.
while !self
.lan
.as_ref()
.is_some_and(|h| h.hub.guests.iter().any(|g| g.placed && g.loaded))
{
self.tick_server(true)?;
if Instant::now() > deadline {
return Err("command harness: owner did not connect".into());
}
std::thread::sleep(Duration::from_millis(2));
}
self.harness_owner(|g| g.harness_settle(deadline))?;
let mut out = String::new();
for line in std::fs::read_to_string(script)?.lines() {
if line.trim().is_empty() {
@@ -107,9 +190,12 @@ impl Game<'_> {
let command = op["cmd"].as_str().unwrap_or("");
self.chat_capture = Some(Vec::new());
self.cmds.results = Some(Vec::new());
self.as_console(|g| {
g.run_command(command.strip_prefix('/').unwrap_or(command))
});
self.harness_owner(|g| {
g.as_console(|g| {
g.run_command(command.strip_prefix('/').unwrap_or(command))
});
Ok(())
})?;
let messages = self.chat_capture.take().unwrap_or_default();
let results = self.cmds.results.take().unwrap_or_default();
if op["record"].as_bool() != Some(false) {
@@ -124,17 +210,8 @@ impl Game<'_> {
}
"tick" => {
self.chat_capture = Some(Vec::new());
// The oracle's player has no client moving it: hold the
// player where commands put it while the game ticks.
let held = self.player.as_ref().map(|p| p.position);
for _ in 0..op["n"].as_u64().unwrap_or(1) {
self.update_world();
self.tick()?;
if let (Some(p), Some(at)) = (self.player.as_mut(), held) {
p.position = at;
p.velocity = horizon_core::geometry::Vec3::new(0.0, 0.0, 0.0);
p.fall_distance = 0.0;
}
self.tick_server(false)?;
}
let messages = self.chat_capture.take().unwrap_or_default();
if op["record"].as_bool() == Some(true) {
@@ -147,7 +224,17 @@ impl Game<'_> {
out.push('\n');
}
}
"settle" => self.harness_settle(deadline)?,
"settle" => {
// Finish portal arrivals using connection-only passes too.
while !self.server_trips.is_empty() {
self.tick_server(true)?;
if Instant::now() > deadline {
return Err("command harness: portal did not arrive".into());
}
std::thread::sleep(Duration::from_millis(2));
}
self.harness_owner(|g| g.harness_settle(deadline))?;
}
"portal" => self.harness_portal()?,
other => return Err(format!("command harness: unknown op {other}").into()),
}
+10 -4
View File
@@ -752,7 +752,8 @@ impl Game<'_> {
&& !id.ends_with("get")
&& let Ok(t) = self.entities(ctx, "target", false)
{
self.cmds.waypoint_dirty.push(t[0]);
let target = self.waypoint_target(t[0]);
self.cmds.waypoint_dirty.push(target);
}
result
}
@@ -2841,9 +2842,14 @@ impl Game<'_> {
capture.push(line.clone());
}
let focused = self.screen == Screen::Chat;
self.ui
.chat
.add(&self.gui.atlas, line, None, self.ticks, focused);
self.ui.chat.add(
&self.gui.atlas,
line,
None,
horizon_client::chat::ChatSource::Player,
self.ticks,
focused,
);
Ok(1)
}
/// MessageArgument: `@p`-style selectors in a message become names.
@@ -214,8 +214,22 @@ impl Game<'_> {
// ---- /waypoint ------------------------------------------------------
/// A guest's body is only `Player` during its turn; retained command state
/// must use its connection so the next server tick still finds that body.
pub(super) fn waypoint_target(&self, t: Target) -> Target {
match t {
Target::Player => self.lan_acting.map_or(t, Target::Guest),
_ => t,
}
}
/// A living entity's `waypoint_transmit_range` (`isTransmittingWaypoint`).
fn transmit_range(&mut self, t: Target) -> f64 {
if matches!(t, Target::Guest(_)) {
return self
.for_player(t, |g| g.transmit_range(Target::Player))
.unwrap_or(0.0);
}
if t == Target::Player {
return self
.player
@@ -244,7 +258,7 @@ impl Game<'_> {
fn waypoint_transmitters(&mut self) -> Vec<Target> {
if self.cmds.waypoints.is_none() {
let tracked = if self.transmit_range(Target::Player) > 0.0 {
vec![Target::Player]
vec![self.waypoint_target(Target::Player)]
} else {
Vec::new()
};
@@ -257,6 +271,12 @@ impl Game<'_> {
.collect();
self.cmds.waypoints = Some(alive.clone());
alive
.into_iter()
.map(|t| match t {
Target::Guest(conn) if self.lan_acting == Some(conn) => Target::Player,
_ => t,
})
.collect()
}
/// The waypoint part of a living entity's tick: dirty transmit ranges
/// track or untrack it.
@@ -279,7 +299,7 @@ impl Game<'_> {
fn waypoint_icon(&self, t: Target) -> WaypointIcon {
self.cmds
.waypoint_icons
.get(&t)
.get(&self.waypoint_target(t))
.cloned()
.unwrap_or_default()
}
@@ -389,6 +409,7 @@ impl Game<'_> {
&[Text::colored(name, rgb)],
)
};
let t = self.waypoint_target(t);
self.cmds.waypoint_icons.insert(t, icon);
// WaypointCommand.mutateIcon: untrack, change, track again.
self.waypoint_transmitters();
@@ -608,6 +629,7 @@ impl Game<'_> {
.hub
.guests
.iter()
.filter(|g| lan.hub.singleplayer_owner != Some(g.uuid))
.filter(|g| match &s.player_name {
Some(name) => g.name.eq_ignore_ascii_case(name),
None => s.kind == 'a' || s.kind == 'e',
@@ -619,7 +641,7 @@ impl Game<'_> {
if host.is_empty() && guests.is_empty() {
return Err(err("argument.entity.notfound.player", &[]));
}
if self.lan.is_none() {
if self.lan.is_none() || (self.serving() && self.published_port().is_none()) {
return Err(err("commands.kick.singleplayer.failed", &[]));
}
let reason_raw = match ctx.get("reason") {
+12 -1
View File
@@ -241,6 +241,13 @@ impl Game<'_> {
"pad" => Ok(Some(self.pad_command(&words)?)),
"key" => {
let key = key_code(arg(1)?).ok_or_else(|| format!("unknown key {}", words[1]))?;
// A server's dialog takes the keys, in configuration too.
if self.dialog_open() {
if arg(2)? != "up" {
self.dialog_key(key, None, None)?;
}
return ok();
}
self.require_world()?;
// The sign editor takes raw presses (Enter, arrows, Backspace, Escape).
if self.sign_edit.is_some() {
@@ -538,13 +545,17 @@ impl Game<'_> {
))
}
"type" => {
self.require_world()?;
let text = line
.trim_start()
.strip_prefix("type")
.unwrap_or("")
.strip_prefix(' ')
.unwrap_or("");
// A server's dialog, in configuration too.
if self.dialog_type(text) {
return ok();
}
self.require_world()?;
if self.book.is_some() {
self.book_type(text);
return ok();
+10 -18
View File
@@ -43,10 +43,6 @@ fn arrow_damage(arrow: &mut Projectile, fallback: &mut impl RandomSource) -> f32
#[derive(Default)]
pub(super) struct Creatures {
pub projectiles: Vec<Projectile>,
/// Player status effects applied by mobs: (effect, ticks left, amplifier).
pub pearl_cooldown: u32,
/// The wind charge's use cooldown (ticks).
pub wind_cooldown: u32,
/// Wind bursts from a guest's turn, waiting for it to end (game/wind.rs).
pub wind_bursts: Vec<super::wind::Burst>,
/// Ticks left of the elder guardian's apparition (MobAppearanceParticle).
@@ -642,7 +638,6 @@ impl Game<'_> {
self.tick_allays();
self.tick_breeze_effects();
self.tick_sniffer_effects();
self.tick_player_effects();
self.piglin_pickups();
self.sulfur_cube_pickups();
// Hurt and death sounds of damage dealt outside the mob ticks.
@@ -1572,13 +1567,6 @@ impl Game<'_> {
}
self.give_player_effect("minecraft:wither", 40, 0);
}
/// Burning applied to the player by mobs.
fn tick_player_effects(&mut self) {
self.creatures.wind_cooldown = self.creatures.wind_cooldown.saturating_sub(1);
if self.creatures.pearl_cooldown > 0 {
self.creatures.pearl_cooldown -= 1;
}
}
pub(super) fn hurt_player_by(&mut self, damage_type: &str, amount: f32) {
if self.creative {
return;
@@ -2879,14 +2867,14 @@ impl Game<'_> {
let kind = match item {
"minecraft:snowball" => ProjectileKind::Snowball,
"minecraft:egg" | "minecraft:blue_egg" | "minecraft:brown_egg" => ProjectileKind::Egg,
"minecraft:ender_pearl" => {
if self.creatures.pearl_cooldown > 0 {
return Ok(true);
"minecraft:ender_pearl" => ProjectileKind::EnderPearl,
"minecraft:wind_charge" => {
let thrown = self.throw_wind_charge()?;
if thrown {
self.apply_use_cooldown(stack);
}
self.creatures.pearl_cooldown = 20;
ProjectileKind::EnderPearl
return Ok(thrown);
}
"minecraft:wind_charge" => return self.throw_wind_charge(),
_ if item.ends_with("_spawn_egg") => return self.use_spawn_egg(stack),
_ => return Ok(false),
};
@@ -2911,6 +2899,9 @@ impl Game<'_> {
);
self.creatures.projectiles.push(proj);
self.player_projectile_shot();
// ItemStack.use's applyAfterUseComponentSideEffects: the pearl's
// second is the `use_cooldown` component, as for any thrown item.
self.apply_use_cooldown(stack);
let sound = match kind {
ProjectileKind::Snowball => "minecraft:entity.snowball.throw",
ProjectileKind::Egg => "minecraft:entity.egg.throw",
@@ -3031,6 +3022,7 @@ impl Game<'_> {
continue;
}
}
self.mark_entity(entity_start, &mob.kind.id);
let light = self.world.light(
mob.position.x.floor() as i32,
(mob.position.y + mob.eye_height()).floor() as i32,
+611
View File
@@ -0,0 +1,611 @@
//! A server's dialogs bound to the game (`ClientCommonPacketListenerImpl.
//! showDialog` and `clearDialog`, `DialogScreen`, its `WarningScreen` and the
//! `WaitingForResponseScreen`): the screen over whatever is open (the
//! connecting screen in configuration), its inputs, and the actions its
//! buttons send back.
use super::Game;
use horizon_client::{
Result,
clipboard::EditShortcut,
confirm_screen::Confirm,
dialog::{AfterAction, Click, Control, Dialog, InputValue},
dialog_screen::{BOX, Layout, Part, WARNING_SPRITES, waiting_button},
hud::{HudBatch, rgba},
text::Text,
ui,
};
use horizon_world::nbt::Tag;
use std::time::Instant;
use winit::keyboard::KeyCode;
/// An open `DialogScreen`.
struct Open {
dialog: Dialog,
values: Vec<InputValue>,
/// The box of each text input.
fields: Vec<Option<ui::EditBox>>,
focus: Option<usize>,
scroll: ui::Scroll,
}
/// The dialog screens a server can open.
#[derive(Default)]
pub(super) struct DialogState {
open: Option<Open>,
/// `WaitingForResponseScreen`, since when.
waiting: Option<Instant>,
/// `DialogScreen.WarningScreen`, over the dialog.
warning: bool,
}
fn warning_texts() -> (Text, Text, [Text; 2]) {
let t = |key: &str| Text::translate(key, &[]);
(
t("menu.custom_screen_info.title"),
t("menu.custom_screen_info.contents"),
[t("menu.disconnect"), t("gui.back")],
)
}
impl Game<'_> {
/// Whether a dialog screen takes the input.
pub(super) fn dialog_open(&self) -> bool {
let d = &self.dialogs;
d.open.is_some() || d.waiting.is_some() || d.warning
}
/// `showDialog`: the dialog replaces an open one (and a waiting screen);
/// under the warning screen, it is what Back returns to.
pub(super) fn show_dialog(&mut self, tag: &Tag) {
let Some(dialog) = Dialog::parse(tag) else {
log::warn!("Horizon: failed to show dialog for data {tag:?}");
return;
};
let values = dialog.inputs.iter().map(|i| i.control.initial()).collect();
let fields = dialog
.inputs
.iter()
.map(|i| match &i.control {
Control::Text {
width,
initial,
max_length,
..
} => {
let mut edit = ui::EditBox::new(0.0, 0.0, *width as f32, 20.0);
edit.max_length = (*max_length).max(1) as usize;
edit.set_value(initial);
edit.focused = false;
Some(edit)
}
_ => None,
})
.collect();
self.dialogs.open = Some(Open {
dialog,
values,
fields,
focus: None,
scroll: ui::Scroll::default(),
});
self.dialogs.waiting = None;
self.capture(false);
}
/// `clearDialog`: the dialog closes (under the warning screen, Back then
/// returns to what was open before it).
pub(super) fn clear_dialog(&mut self) {
if self.dialogs.open.take().is_some() && !self.dialog_open() {
self.dialog_closed();
}
}
/// Every dialog screen goes (a disconnect, or the level loading screen).
pub(super) fn close_dialogs(&mut self) {
self.dialogs = DialogState::default();
}
fn dialog_closed(&mut self) {
if self.player.is_some() && self.screen == super::Screen::Playing && self.title.is_none() {
self.capture(true);
}
}
/// The input values as the controls hold them now.
fn dialog_values(open: &mut Open) {
for (value, field) in open.values.iter_mut().zip(&open.fields) {
if let Some(f) = field {
*value = InputValue::Text(f.value.clone());
}
}
}
/// `DialogScreen.runAction`: the click event, then the screen the dialog's
/// `after_action` leaves.
fn run_dialog_action(&mut self, click: Option<Click>, after: AfterAction) -> Result<()> {
match after {
AfterAction::None => {}
AfterAction::Close => self.dialogs.open = None,
AfterAction::WaitForResponse => {
self.dialogs.open = None;
self.dialogs.waiting = Some(Instant::now());
}
}
match click {
Some(Click::Custom { id, payload }) => {
if let Some(r) = &self.remote {
r.session.send_custom_click(&id, payload);
}
}
Some(Click::RunCommand(command)) => {
// In configuration there is nothing to run it (vanilla warns).
if self.player.is_some() {
let command = command.strip_prefix('/').unwrap_or(&command);
self.send_chat(&format!("/{command}"));
} else {
log::warn!(
"Horizon: commands are not supported in configuration, trying to run '{command}'"
);
}
}
Some(Click::ShowDialog(dialog)) => self.show_dialog_ref(&dialog),
Some(Click::CopyToClipboard(value)) => {
use horizon_client::clipboard::Clipboard;
self.clipboard.set(&value);
}
Some(other) => log::info!("Horizon: dialog action {other:?} is not supported yet"),
None => {}
}
if !self.dialog_open() {
self.dialog_closed();
}
Ok(())
}
/// A `show_dialog` click event's dialog: a registry id or the dialog itself.
fn show_dialog_ref(&mut self, dialog: &Tag) {
match dialog {
Tag::String(id) => {
let id = id.to_string_lossy();
let name = if id.contains(':') {
id.clone()
} else {
format!("minecraft:{id}")
};
let found = self.remote.as_ref().and_then(|r| {
let entries = r.registries.registries.get("minecraft:dialog")?;
entries.iter().find(|(n, _)| *n == name)?.1.clone()
});
match found {
Some(tag) => self.show_dialog(&tag),
None => log::warn!("Horizon: unknown dialog {id}"),
}
}
tag => self.show_dialog(tag),
}
}
/// `ClientboundShowDialogPacket` in play: a registry entry or the dialog.
pub(super) fn show_play_dialog(
&mut self,
dialog: &horizon_protocol::packets::play::DialogHolder,
) {
use horizon_protocol::packets::play::DialogHolder;
match dialog {
DialogHolder::Direct(tag) => self.show_dialog(tag),
DialogHolder::Registry(id) => {
let tag = self
.remote
.as_ref()
.and_then(|r| r.registries.data("minecraft:dialog", *id).cloned());
match tag {
Some(tag) => self.show_dialog(&tag),
None => log::warn!("Horizon: unknown dialog {id}"),
}
}
}
}
fn dialog_layout(&self) -> Option<Layout> {
let open = self.dialogs.open.as_ref()?;
Some(Layout::new(
&open.dialog,
&self.gui.atlas.font,
self.gui_size(),
))
}
pub(super) fn draw_dialog(&mut self, batch: &mut HudBatch) {
if !self.dialog_open() {
return;
}
let (w, h) = (batch.width, batch.height);
let cursor = self.cursor;
let atlas = &self.gui.atlas;
if self.player.is_none() {
// In place of the connecting screen, on its background.
batch.fill(atlas, 0.0, 0.0, w, h, rgba(20, 20, 20, 255));
}
ui::menu_background(batch, atlas, 0.0, 0.0, w, h, self.player.is_some());
if self.dialogs.warning {
let (title, message, [disconnect, back]) = warning_texts();
Confirm::new(&atlas.font, (w, h), &title, &message).draw(
batch,
atlas,
cursor,
[&disconnect, &back],
);
return;
}
if let Some(since) = self.dialogs.waiting {
// HeaderAndFooterLayout(33, 0) centred: the title, the button 33 below.
let ticks = (since.elapsed().as_millis() / 50) as u32;
let (label, visible, active) = waiting_button(ticks);
let title = Text::translate("gui.waitingForResponse.title", &[]);
let top = ((h - 53.0) / 2.0).trunc();
let tw = atlas.font.width_text(&title);
batch.text_styled(
atlas,
&title,
((w - tw) / 2.0).trunc(),
top + 12.0,
u32::MAX,
true,
);
if visible {
let (bx, by) = (((w - 200.0) / 2.0).trunc(), top + 33.0);
let hover = ui::hovered(cursor, bx, by, 200.0, 20.0);
ui::button(batch, atlas, bx, by, 200.0, 20.0, &label, active, hover);
}
return;
}
let Some(layout) = self.dialog_layout() else {
return;
};
let Some(open) = self.dialogs.open.as_mut() else {
return;
};
let atlas = &self.gui.atlas;
let [ax, ay, aw, ah] = layout.area;
open.scroll.clamp(ay, ay + ah, layout.content);
batch.text_styled(
atlas,
&open.dialog.title,
layout.title.0,
layout.title.1,
u32::MAX,
true,
);
let [wx, wy, ww, wh] = layout.warning;
let warn_hover = ui::hovered(cursor, wx, wy, ww, wh);
horizon_client::widgets::record("warning", layout.warning, true);
batch.sprite_scaled(
atlas,
WARNING_SPRITES[if warn_hover { 2 } else { 0 }],
wx,
wy,
ww,
wh,
u32::MAX,
);
let buttons = open.dialog.buttons();
let in_area = |y: f32| y >= ay && y < ay + ah;
let mut tooltip = None;
let mut start = None;
for (part, clipped) in layout.placed(open.scroll.amount) {
if clipped && start.is_none() {
start = Some(batch.vertices.len());
} else if !clipped && let Some(s) = start.take() {
batch.clip_since(s, ax, ay, ax + aw, ay + ah);
}
match part {
Part::Line(text, x, y) => {
batch.text_styled(atlas, &text, x, y, u32::MAX, true);
}
Part::Field { input, rect } => {
if let Some(Some(edit)) = open.fields.get_mut(input) {
[edit.x, edit.y, edit.width, edit.height] = rect;
edit.focused = open.focus == Some(input);
edit.draw(batch, atlas, None);
}
}
Part::Check { input, rect } => {
let [x, y, cw, ch] = rect;
let hover = in_area(cursor.1) && ui::hovered(cursor, x, y, cw, ch);
let on = open.values.get(input) == Some(&InputValue::Bool(true));
let sprite = match (on, hover) {
(true, true) => "gui/sprites/widget/checkbox_selected_highlighted",
(true, false) => "gui/sprites/widget/checkbox_selected",
(false, true) => "gui/sprites/widget/checkbox_highlighted",
(false, false) => "gui/sprites/widget/checkbox",
};
let Control::Boolean { label, .. } = &open.dialog.inputs[input].control else {
continue;
};
horizon_client::widgets::record(&label.plain(), rect, true);
batch.sprite_scaled(atlas, sprite, x, y, BOX, BOX, u32::MAX);
batch.text_styled(
atlas,
label,
x + BOX + 4.0,
y + 4.0,
rgba(224, 224, 224, 255),
true,
);
}
Part::Button { index, rect } => {
let [x, y, bw, bh] = rect;
let hover =
(!clipped || in_area(cursor.1)) && ui::hovered(cursor, x, y, bw, bh);
let b = buttons[index];
ui::button(batch, atlas, x, y, bw, bh, &b.label, true, hover);
if hover && let Some(t) = &b.tooltip {
tooltip = Some(t.clone());
}
}
}
}
if let Some(s) = start {
batch.clip_since(s, ax, ay, ax + aw, ay + ah);
}
if layout.content > ah {
ui::scrollbar(
batch,
atlas,
ax + aw - 6.0,
ay,
ay + ah,
layout.content,
open.scroll.amount,
);
}
if warn_hover {
tooltip = Some(Text::translate("menu.custom_screen_info.tooltip", &[]));
}
if let Some(t) = tooltip {
let lines: Vec<Text> = atlas
.font
.split_lines(&t, 170.0)
.into_iter()
.map(|l| l.0)
.collect();
ui::tooltip(batch, atlas, &lines, cursor);
}
}
/// A left click on a dialog screen.
pub(super) fn dialog_click(&mut self) -> Result<()> {
let cursor = self.cursor;
if self.dialogs.warning {
let (title, message, _) = warning_texts();
let answer = Confirm::new(&self.gui.atlas.font, self.gui_size(), &title, &message)
.answer_at(cursor);
match answer {
Some(true) => {
self.audio.play_button_click();
let reason = Text::translate("menu.custom_screen_info.disconnect", &[]);
return self.leave_server(Some(reason.plain()));
}
Some(false) => {
self.audio.play_button_click();
self.dialogs.warning = false;
if !self.dialog_open() {
self.dialog_closed();
}
}
None => {}
}
return Ok(());
}
if let Some(since) = self.dialogs.waiting {
let (w, h) = self.gui_size();
let active = waiting_button((since.elapsed().as_millis() / 50) as u32).2;
let (bx, by) = (
((w - 200.0) / 2.0).trunc(),
((h - 53.0) / 2.0).trunc() + 33.0,
);
if active && ui::hovered(cursor, bx, by, 200.0, 20.0) {
self.audio.play_button_click();
self.dialogs.waiting = None;
self.dialog_closed();
}
return Ok(());
}
let Some(layout) = self.dialog_layout() else {
return Ok(());
};
let [wx, wy, ww, wh] = layout.warning;
if ui::hovered(cursor, wx, wy, ww, wh) {
self.audio.play_button_click();
self.dialogs.warning = true;
return Ok(());
}
let Some(open) = self.dialogs.open.as_mut() else {
return Ok(());
};
let [_, ay, _, ah] = layout.area;
let mut action = None;
open.focus = None;
for (part, clipped) in layout.placed(open.scroll.amount) {
if clipped && !(cursor.1 >= ay && cursor.1 < ay + ah) {
continue;
}
match part {
Part::Field {
input,
rect: [x, y, w, h],
} if ui::hovered(cursor, x, y, w, h) => {
open.focus = Some(input);
if let Some(Some(edit)) = open.fields.get_mut(input) {
edit.focused = true;
edit.click(&self.gui.atlas, cursor.0, false, false);
}
}
Part::Check {
input,
rect: [x, y, w, h],
} if ui::hovered(cursor, x, y, w, h) => {
if let Some(InputValue::Bool(b)) = open.values.get_mut(input) {
*b = !*b;
}
action = Some(None);
}
Part::Button {
index,
rect: [x, y, w, h],
} if ui::hovered(cursor, x, y, w, h) => {
action = Some(Some(index));
}
_ => {}
}
}
let Some(action) = action else {
return Ok(());
};
self.audio.play_button_click();
let Some(index) = action else {
return Ok(());
};
let Some(open) = self.dialogs.open.as_mut() else {
return Ok(());
};
Self::dialog_values(open);
let d = &open.dialog;
let click = d.buttons()[index]
.action
.as_ref()
.and_then(|a| a.click(&d.inputs, &open.values));
let after = d.after_action;
self.run_dialog_action(click, after)
}
/// The mouse wheel over a dialog: its body scrolls 10 a step.
pub(super) fn dialog_scroll(&mut self, delta: f32) {
let Some(layout) = self.dialog_layout() else {
return;
};
if let Some(open) = self.dialogs.open.as_mut() {
let [_, ay, _, ah] = layout.area;
open.scroll.wheel(delta, 10.0, ay, ay + ah, layout.content);
}
}
/// A key pressed on a dialog screen: Escape closes what allows it, Tab
/// moves between the text inputs, the rest goes to the focused one
/// (`shortcut`: the press as an edit shortcut; `text`: what it types).
pub(super) fn dialog_key(
&mut self,
key: KeyCode,
shortcut: Option<EditShortcut>,
text: Option<&str>,
) -> Result<()> {
let escape = key == KeyCode::Escape;
if self.dialogs.warning {
if escape {
self.dialogs.warning = false;
if !self.dialog_open() {
self.dialog_closed();
}
}
return Ok(());
}
if let Some(since) = self.dialogs.waiting {
// shouldCloseOnEsc: once Back works.
if escape && waiting_button((since.elapsed().as_millis() / 50) as u32).2 {
self.dialogs.waiting = None;
self.dialog_closed();
}
return Ok(());
}
let ctrl = self.modifiers.control_key() || self.modifiers.super_key();
let shift = self.modifiers.shift_key();
let Some(open) = self.dialogs.open.as_mut() else {
return Ok(());
};
if escape {
if !open.dialog.can_close_with_escape {
return Ok(());
}
// onClose: the cancel action, and the screen closes.
Self::dialog_values(open);
let d = &open.dialog;
let click = d.on_cancel().and_then(|a| a.click(&d.inputs, &open.values));
return self.run_dialog_action(click, AfterAction::Close);
}
if key == KeyCode::Tab {
let texts: Vec<usize> = (0..open.fields.len())
.filter(|&i| open.fields[i].is_some())
.collect();
if !texts.is_empty() {
let at = open.focus.and_then(|f| texts.iter().position(|&i| i == f));
let next = match at {
None if shift => texts.len() - 1,
None => 0,
Some(p) if shift => (p + texts.len() - 1) % texts.len(),
Some(p) => (p + 1) % texts.len(),
};
open.focus = Some(texts[next]);
}
return Ok(());
}
let Some(Some(edit)) = open.focus.and_then(|f| open.fields.get_mut(f)) else {
return Ok(());
};
if let Some(shortcut) = shortcut {
edit.shortcut(shortcut, &mut self.clipboard);
return Ok(());
}
if edit.key(key, ctrl, shift) {
return Ok(());
}
let typed: String = text
.unwrap_or_default()
.chars()
.filter(|c| !c.is_control())
.collect();
if !typed.is_empty() {
edit.insert_text(&typed);
}
Ok(())
}
/// `type TEXT` with a dialog open: into the focused text input, or the
/// first one.
pub(super) fn dialog_type(&mut self, text: &str) -> bool {
let Some(open) = self.dialogs.open.as_mut() else {
return false;
};
let focus = open
.focus
.or_else(|| open.fields.iter().position(Option::is_some));
let Some(Some(edit)) = focus.and_then(|f| open.fields.get_mut(f)) else {
return false;
};
open.focus = focus;
edit.insert_text(text);
true
}
/// What `state` reports: the open dialog and its values, or the waiting
/// and warning screens.
pub(super) fn dialog_status(&self) -> serde_json::Value {
let d = &self.dialogs;
let screen = if d.warning {
Some("warning")
} else if d.waiting.is_some() {
Some("waiting_for_response")
} else {
None
};
let open = d.open.as_ref().map(|open| {
let mut values = open.values.clone();
for (value, field) in values.iter_mut().zip(&open.fields) {
if let Some(f) = field {
*value = InputValue::Text(f.value.clone());
}
}
open.dialog.json(&values)
});
serde_json::json!({ "open": open, "screen": screen })
}
}
@@ -90,6 +90,10 @@ enum Target {
impl Game<'_> {
/// Run every case of `spec` ("CASES,OUTPUT") and write the results.
pub(super) fn dispense_fixture(&mut self, spec: &str) -> Result<()> {
assert!(
!self.options.execution_role.is_client(),
"fixture requires a server Game"
);
let (input, output) = spec
.split_once(',')
.ok_or("HORIZON_DISPENSE_FIXTURE=CASES,OUTPUT")?;
@@ -15,6 +15,11 @@ fn opaque(facts: &StateFacts) -> bool {
impl Game<'_> {
pub(super) fn prepare_entity_culling(&mut self, camera: glam::DVec3) {
// Spans tell the pack's passes which entities are hidden; without them, nothing changes.
self.pack_shadow_casters = match (&self.renderer, &self.pack_ids) {
(Some(r), Some(_)) => r.pack_shadow_casters(),
_ => (false, false),
};
let at = camera.to_array().map(|v| v.floor() as i32);
self.entity_culler = (self.settings.horizon.entity_culling
// Packs can replace an opaque block's model or texture.
@@ -28,24 +33,29 @@ impl Game<'_> {
/// Use complete generated geometry so equipment, animation and custom
/// models cannot stick out of an assumed collision-sized culling box.
pub(super) fn cull_entity_vertices(&self, vertices: &mut Vec<Vertex>, start: usize) {
if let Some(culler) = &self.entity_culler {
culler.cull_vertices(vertices, start, |pos| {
let state = self.world.block(pos[0], pos[1], pos[2]);
if !opaque(self.res.facts().get(state)) {
return false;
}
let Some(y) = pos[1].checked_sub(MIN_Y) else {
return false;
};
let section = (pos[0] >> 4, y.div_euclid(16), pos[2] >> 4);
// Newly edited or not-yet-uploaded terrain is not an occluder.
self.renderer
.as_ref()
.is_some_and(|r| r.has_section(section))
&& !self.world.dirty_sections.contains(&section)
&& !self.streaming.is_meshing(section)
&& !self.pending_uploads.iter().any(|(key, _)| *key == section)
});
let Some(culler) = &self.entity_culler else {
return;
};
let hidden = culler.hidden(vertices, start, |pos| {
let state = self.world.block(pos[0], pos[1], pos[2]);
if !opaque(self.res.facts().get(state)) {
return false;
}
let Some(y) = pos[1].checked_sub(MIN_Y) else {
return false;
};
let section = (pos[0] >> 4, y.div_euclid(16), pos[2] >> 4);
// Newly edited or not-yet-uploaded terrain is not an occluder.
self.renderer
.as_ref()
.is_some_and(|r| r.has_section(section))
&& !self.world.dirty_sections.contains(&section)
&& !self.streaming.is_meshing(section)
&& !self.pending_uploads.iter().any(|(key, _)| *key == section)
});
// A shader pack's shadow pass still draws what the camera cannot see.
if hidden && !(self.pack_shadow_casters.0 && self.hide_spans_from(start, vertices.len())) {
vertices.truncate(start);
}
}
}
+22 -9
View File
@@ -229,25 +229,38 @@ impl Game<'_> {
fn zoom_factor(&self) -> f32 {
self.extras.zoom.factor(self.scoping())
}
/// The zoom in effect, the extra's or a mod's (`render.set-zoom`),
/// whichever is stronger: (factor, smooth, turning scale).
fn zoom_now(&self) -> (f32, bool, f64) {
let factor = self.zoom_factor();
let e = &self.settings.extras;
let own = (
factor,
e.zoom_smooth,
turn_scale(factor, e.zoom_sensitivity),
);
match self.mod_zoom() {
Some(z) if z.factor > factor => (z.factor, z.smooth, f64::from(z.turn)),
_ => own,
}
}
/// The field-of-view modifier target, divided by a smooth zoom.
pub(super) fn zoomed_fov_target(&self, vanilla: f32) -> f32 {
if self.settings.extras.zoom_smooth {
fov_target(vanilla, self.zoom_factor())
} else {
vanilla
match self.zoom_now() {
(factor, true, _) => fov_target(vanilla, factor),
_ => vanilla,
}
}
/// The field of view in degrees, divided by an instant zoom.
pub(super) fn zoomed_fov_degrees(&self, fov: f32) -> f32 {
if self.settings.extras.zoom_smooth {
fov
} else {
fov / self.zoom_factor()
match self.zoom_now() {
(factor, false, _) => fov / factor,
_ => fov,
}
}
/// Mouse turning while zoomed.
pub(super) fn zoom_turn_scale(&self) -> f64 {
turn_scale(self.zoom_factor(), self.settings.extras.zoom_sensitivity)
self.zoom_now().2
}
/// The minimap and mods' corner panels, in the HUD layer (hidden with F1
/// by the caller). The top left corner gives way to the debug screen.
@@ -45,7 +45,8 @@ pub(super) struct RulesScreen {
initial: GameRules,
/// IntegerRuleEntry's boxes, by rule.
numbers: Vec<(&'static str, EditBox)>,
scroll: f32,
/// RuleList's scrolling: the wheel and the held scrollbar.
scroll: ui::Scroll,
/// ConfirmScreen "Game Rule Changes" in its place.
confirm: bool,
}
@@ -120,7 +121,7 @@ impl RulesScreen {
values: rules.clone(),
initial: rules.clone(),
numbers,
scroll: 0.0,
scroll: ui::Scroll::default(),
confirm: false,
}
}
@@ -156,12 +157,17 @@ impl RulesScreen {
})
.collect()
}
fn max_scroll(rows: usize, h: f32) -> f32 {
(ROW * rows as f32 + 4.0 - (h - HEADER - FOOTER)).max(0.0)
}
/// Entry `i`'s top: 2 under the list's top, less the scroll.
fn row_top(&self, i: usize) -> f32 {
HEADER + 2.0 - self.scroll.trunc() + ROW * i as f32
HEADER + 2.0 - self.scroll.amount.trunc() + ROW * i as f32
}
/// The list's scrollbar column (AbstractSelectionList: 6 pixels right of
/// the entries) and the pixels it scrolls.
fn bar(w: f32) -> f32 {
row_left(w) + ROW_WIDTH + 6.0 + 2.0
}
fn content(&self) -> f32 {
ROW * self.rows().len() as f32 + 4.0
}
/// getEntryAtPosition: the row under the pointer, inside the list.
fn hovered(&self, rows: usize, (x, y): (f32, f32), (w, h): (f32, f32)) -> Option<usize> {
@@ -212,10 +218,28 @@ impl RulesScreen {
/// The mouse wheel (AbstractSelectionList: half an entry a step).
pub(super) fn wheel(&mut self, delta: f32, h: f32) {
if !self.confirm {
let max = Self::max_scroll(self.rows().len(), h);
self.scroll = (self.scroll - delta * ROW / 2.0).clamp(0.0, max);
let content = self.content();
self.scroll
.wheel(delta, ROW / 2.0, HEADER, h - FOOTER, content);
}
}
/// A press on the scrollbar holds it (AbstractSelectionList.updateScrolling).
pub(super) fn press_scrollbar(&mut self, cursor: (f32, f32), (w, h): (f32, f32)) -> bool {
let content = self.content();
self.scroll
.press(cursor, Self::bar(w), HEADER, h - FOOTER, content)
}
/// The pointer moved while the scrollbar is held.
pub(super) fn drag(&mut self, y: f32, h: f32) {
if self.scroll.held() {
let content = self.content();
self.scroll.drag(y, HEADER, h - FOOTER, content);
}
}
/// The button came up: the scrollbar lets go.
pub(super) fn release(&mut self) {
self.scroll.release();
}
fn focused(&mut self) -> Option<(Option<&'static str>, &mut EditBox)> {
if self.search.focused {
return Some((None, &mut self.search));
@@ -229,7 +253,7 @@ impl RulesScreen {
/// number sets its rule when valid (IntegerRuleEntry's responder).
fn edited(&mut self, rule: Option<&'static str>) {
let Some(name) = rule else {
self.scroll = 0.0;
self.scroll.amount = 0.0;
return;
};
let Some(r) = gamerules::rule(name) else {
@@ -286,6 +310,11 @@ impl RulesScreen {
if on(cx, cy, 150.0, 20.0) {
return self.close();
}
// A press on the scrollbar holds it and clicks nothing else.
if self.press_scrollbar(cursor, size) {
self.unfocus();
return None;
}
// A click moves the focus to the widget under it.
let (sx, sy) = search_at(w);
if on(sx, sy, SEARCH_WIDTH, 15.0) {
@@ -360,7 +389,8 @@ impl RulesScreen {
return;
}
let rows = self.rows();
self.scroll = self.scroll.clamp(0.0, Self::max_scroll(rows.len(), h));
let content = ROW * rows.len() as f32 + 4.0;
self.scroll.clamp(HEADER, h - FOOTER, content);
// RuleList: the in-world list background, its entries clipped to it,
// then the separators and scrollbar.
ui::list_background(batch, atlas, HEADER, h - FOOTER, true);
@@ -421,16 +451,14 @@ impl RulesScreen {
}
batch.clip_since(first, 0.0, HEADER, w, h - FOOTER);
ui::separators(batch, atlas, HEADER, h - FOOTER, true);
let content = ROW * rows.len() as f32 + 4.0;
let bar_x = left + ROW_WIDTH + 6.0 + 2.0;
ui::scrollbar(
batch,
atlas,
bar_x,
Self::bar(w),
HEADER,
h - FOOTER,
content,
self.scroll.trunc(),
self.scroll.amount.trunc(),
);
// The header: the title centred over the search box.
let title = Text::translate("editGamerule.title", &[]);
@@ -536,6 +564,29 @@ mod tests {
);
}
/// AbstractScrollArea: a press on the bar holds it, a drag moves the
/// list (to the ends past them), a release lets go.
#[test]
fn the_scroll_bar_can_be_held_dragged_and_let_go() {
let mut s = RulesScreen::new(&GameRules::default());
let (w, h) = (427.0, 240.0);
let (top, bottom, content) = (HEADER, h - FOOTER, s.content());
assert!(
content > bottom - top,
"the rules overflow a 240-high screen"
);
let bar = RulesScreen::bar(w);
assert!(!s.press_scrollbar((bar - 4.0, top + 5.0), (w, h)));
assert!(s.press_scrollbar((bar + 3.0, top + 5.0), (w, h)));
s.drag(top + 40.0, h);
assert!(s.scroll.amount > 0.0);
s.drag(bottom + 10.0, h);
assert_eq!(s.scroll.amount, content - (bottom - top));
s.release();
s.drag(top, h);
assert_eq!(s.scroll.amount, content - (bottom - top));
}
#[test]
fn edits_change_rules_and_bad_numbers_hold_done() {
let mut s = RulesScreen::new(&GameRules::default());
+54 -3
View File
@@ -143,7 +143,7 @@ pub(super) struct Serving {
flags: Arc<Flags>,
tasks: Receiver<Task>,
/// The window's profile.
owner: u128,
pub(super) owner: u128,
/// Open to LAN or closed from inside a player's command, while the hub
/// is out of its place: applied after the tick.
pending: Option<Publish>,
@@ -357,6 +357,16 @@ impl Game<'_> {
});
let _ = ready.send(Ok((pipe, self.level.name.clone())));
self.settle_spawn()?;
if let (Some(script), Some(output)) = (
std::env::var_os("HORIZON_COMMAND_SCRIPT"),
std::env::var_os("HORIZON_COMMAND_OUTPUT"),
) {
// The script owns the clock: connection setup and terrain loading
// cannot add wall-clock-dependent world ticks to oracle output.
let result = self.run_command_harness(script.as_ref(), output.as_ref());
let stopped = self.stop_integrated();
return result.and(stopped);
}
let mut next = Instant::now();
while !flags.stop.load(Ordering::Relaxed) {
self.run_tasks();
@@ -692,6 +702,48 @@ fn serve(
tasks: Receiver<Task>,
ready: &Sender<Ready>,
) -> Result<()> {
let mut server = server_game(res, pack, options, settings)?;
server.run_integrated(launch, flags, tasks, ready)
}
/// Component oracles supply their own synthetic levels and entity phases.
/// They still execute on a server Game, never the client's world.
pub(super) enum ComponentHarness {
Dispenser(String),
Sculk(String),
}
impl Game<'_> {
pub(super) fn run_component_harness(&self, fixture: ComponentHarness) -> Result<()> {
let (res, pack, settings) = (self.res.clone(), self.pack.clone(), self.settings.clone());
let mut options = self.options.clone();
options.execution_role = ExecutionRole::IntegratedServer;
options.dedicated = Some(Vec::new());
options.server_thread = false;
let result = std::thread::Builder::new()
.name("Server thread".into())
.spawn(move || {
let run = || -> Result<()> {
let mut server = server_game(&res, pack, options, settings)?;
match fixture {
ComponentHarness::Dispenser(spec) => server.dispense_fixture(&spec),
ComponentHarness::Sculk(spec) => server.sculk_fixture(&spec),
}
};
run().map_err(|e| e.to_string())
})?
.join()
.map_err(|_| "the component harness server panicked")?;
result.map_err(Into::into)
}
}
fn server_game(
res: &Arc<Resources>,
pack: PackResources,
options: Options,
settings: Settings,
) -> Result<Game<'_>> {
// It draws nothing: only the font, which books and chat measure with.
let atlas = HudAtlas::fonts(&options.resources)?;
let gui = UiResources {
@@ -702,8 +754,7 @@ fn serve(
}),
};
let audio = Audio::silent(&options.resources);
let mut server = Game::new(res, pack, gui, options, settings, audio, None)?;
server.run_integrated(launch, flags, tasks, ready)
Game::new(res, pack, gui, options, settings, audio, None)
}
#[cfg(test)]
+41 -1
View File
@@ -38,6 +38,8 @@ pub(super) struct GuestPlay {
use_delay: u32,
/// The shield's ItemCooldowns entry: an axe disabled it for this many ticks.
pub(super) shield_cooldown: u32,
/// The guest's `Player.getCooldowns()` (`Game::cooldowns` for its turn).
pub(super) cooldowns: horizon_client::cooldowns::Cooldowns,
attack_ticks: u32,
/// The main hand's item at the guest's last tick (`Game::attack_item`).
attack_item: Option<std::sync::Arc<str>>,
@@ -84,6 +86,7 @@ pub(super) struct GuestPlay {
equipped: Option<Vec<Option<ItemStack>>>,
/// The guest's cast fishing hook (`Player.fishing`).
pub(super) hook: Option<super::fishing::Hook>,
sculk: super::sculk::PlayerEvents,
}
/// A guest's command waiting for the hub (`LanHost::commands`).
@@ -125,6 +128,8 @@ pub(super) struct GuestSwap {
using: Option<(usize, u32)>,
use_delay: u32,
pub(super) shield_cooldown: u32,
/// The guest's item cooldowns (`Game::cooldowns` for its turn).
cooldowns: horizon_client::cooldowns::Cooldowns,
attack_ticks: u32,
attack_item: Option<std::sync::Arc<str>>,
swing: horizon_client::first_person::SwingState,
@@ -252,6 +257,7 @@ impl Game<'_> {
if fresh {
// A new body (joined, respawned, changed level): nothing on it yet.
play.equipped = None;
play.sculk = Default::default();
}
let g = &mut host.hub.guests[i];
// The guest's body.
@@ -259,6 +265,7 @@ impl Game<'_> {
host.play.insert(conn, play);
return None;
};
self.sculk.swap_player(&mut play.sculk);
// The guest's own CombatTracker: damage to them is not the host's.
// A new body starts with no record of the last one.
let guest_combat = if fresh {
@@ -328,6 +335,7 @@ impl Game<'_> {
let using = std::mem::replace(&mut self.using, play.using.take());
let use_delay = std::mem::replace(&mut self.use_delay, play.use_delay);
let shield_cooldown = std::mem::replace(&mut self.shield_cooldown, play.shield_cooldown);
let cooldowns = std::mem::replace(&mut self.cooldowns, std::mem::take(&mut play.cooldowns));
let attack_ticks = std::mem::replace(&mut self.attack_ticks, play.attack_ticks);
let attack_item = std::mem::replace(&mut self.attack_item, play.attack_item.take());
let swing = std::mem::replace(&mut self.swing, play.swing);
@@ -412,6 +420,7 @@ impl Game<'_> {
using,
use_delay,
shield_cooldown,
cooldowns,
attack_ticks,
attack_item,
swing,
@@ -469,6 +478,7 @@ impl Game<'_> {
using,
use_delay,
shield_cooldown,
cooldowns,
attack_ticks,
attack_item,
swing,
@@ -492,6 +502,7 @@ impl Game<'_> {
projectiles,
modifiers,
} = swap;
self.sculk.swap_player(&mut play.sculk);
// The guest's damage record back into their slot, the host's in place.
let guest_combat = std::mem::replace(&mut self.combat, combat);
self.guest_combat.insert(conn, guest_combat);
@@ -508,6 +519,16 @@ impl Game<'_> {
for packet in std::mem::replace(&mut self.guest_packets, packets) {
host.hub.send_to(conn, &packet);
}
// ServerItemCooldowns.onCooldownStarted: the guest's own cooldown.
for (group, duration) in std::mem::take(&mut self.guest_cooldowns) {
host.hub.send_to(
conn,
&C::Cooldown(play::Cooldown {
group: group.to_string(),
duration,
}),
);
}
(self.lan_acting, self.lan_acting_name, self.lan_acting_uuid) = acting;
play.death_cause = std::mem::replace(&mut self.ui.death_cause, death_cause);
// An ender chest the guest opened: the menu shows their inventory
@@ -537,6 +558,7 @@ impl Game<'_> {
play.using = std::mem::replace(&mut self.using, using);
play.use_delay = std::mem::replace(&mut self.use_delay, use_delay);
play.shield_cooldown = std::mem::replace(&mut self.shield_cooldown, shield_cooldown);
play.cooldowns = std::mem::replace(&mut self.cooldowns, cooldowns);
play.attack_ticks = std::mem::replace(&mut self.attack_ticks, attack_ticks);
play.attack_item = std::mem::replace(&mut self.attack_item, attack_item);
play.swing = std::mem::replace(&mut self.swing, swing);
@@ -1781,8 +1803,19 @@ impl Game<'_> {
}
let play = host.play.entry(conn).or_default();
// Player.tick's ItemCooldowns.tick, once a tick for each player
// (the shield an axe disabled).
// (the shield an axe disabled, a thrown pearl).
play.shield_cooldown = play.shield_cooldown.saturating_sub(1);
// ServerItemCooldowns.onCooldownEnded: a finished entry is told to
// the guest (a duration of 0 removes it).
for group in play.cooldowns.tick() {
host.hub.send_to(
conn,
&C::Cooldown(play::Cooldown {
group: group.to_string(),
duration: 0,
}),
);
}
let digging = play.digging;
let using = play.using.is_some();
let delayed = play.destroy_delay > 0 || play.use_delay > 0 || play.attack_ticks < 100;
@@ -2111,6 +2144,12 @@ impl Game<'_> {
let _ = w.apply_fire_contact();
let _ = w.server_hazards_tick(in_wall);
let mut events = std::mem::take(&mut w.player.events);
// LayeredCauldronBlock.handleEntityOnFireInside along those moves.
for event in &events {
if let MotionEvent::CauldronExtinguish { pos } = event {
game.cauldron_extinguish(*pos);
}
}
// WitherRoseBlock.entityInside along those moves.
for event in &events {
if matches!(event, MotionEvent::InsideWitherRose) {
@@ -2152,6 +2191,7 @@ impl Game<'_> {
let Some((events, marked)) = events else {
return;
};
self.as_guest(host, conn, |game| game.tick_player_sculk_events());
for event in events {
match event {
MotionEvent::HurtSound(sound) => self.player_hurt_sound(sound, at),
@@ -171,6 +171,19 @@ impl Game<'_> {
})
.collect()
}
/// The same guests' feet and whether they stand on the ground, for the
/// blocks that also ask that (`BigDripleafBlock.canEntityTilt`).
pub(super) fn lan_guest_feet(&self) -> Vec<([f64; 3], bool)> {
let Some(host) = self.lan.as_ref() else {
return Vec::new();
};
host.hub
.guests
.iter()
.filter(|g| host.hub.here(g) && g.loaded && !g.dead && !g.spectator())
.map(|g| (g.position, g.on_ground))
.collect()
}
/// The guests a projectile can hit, under their entity ids.
pub(super) fn lan_projectile_targets(&self) -> Vec<(Who, Aabb)> {
+25 -1
View File
@@ -388,7 +388,10 @@ impl Restrictions {
1 => Some("chat_restriction.chat_disabled_by_options"),
_ => None,
};
let (chat, commands) = (chat_visibility == 0, chat_visibility < 2);
let abilities = horizon_client::chat::ChatAbilities::new(
horizon_client::chat::ChatVisibility::from_option(chat_visibility),
);
let (chat, commands) = (abilities.send_messages, abilities.send_commands);
// The body's widgets: text blocks (lines, top) and the action's top.
let mut blocks = Vec::new();
let mut action = None;
@@ -802,6 +805,27 @@ impl Game<'_> {
}
}
/// The pointer moved while a list's scrollbar is held: the game rules'
/// list follows it (AbstractSelectionList.mouseDragged).
pub(super) fn world_options_drag(&mut self) {
let h = self.gui_size().1;
if let Some(o) = self.world_options.as_mut()
&& o.view == View::GameRules
&& let Some(rules) = o.rules.as_mut()
{
rules.drag(self.cursor.1, h);
}
}
/// The button came up: a held scrollbar lets go (onRelease).
pub(super) fn world_options_release(&mut self) {
if let Some(o) = self.world_options.as_mut()
&& let Some(rules) = o.rules.as_mut()
{
rules.release();
}
}
pub(super) fn world_options_click(&mut self) {
let Some(mut o) = self.world_options.take() else {
return;
+15 -3
View File
@@ -21,7 +21,7 @@ use super::{
use horizon_client::{
Result, block_entity_anim::BlockEntityAnims, random_tick_sections::RandomTickSections,
save::SaveDir, streaming::Streaming, tick_chunk_membership::TickChunkMembership,
ticket_levels::ChunkTickets, world::World,
ticket_levels::ChunkTickets, vanillagen::VanillaTerrain, world::World,
};
use horizon_world::level::{LevelEvent, Ticks};
use std::{
@@ -39,6 +39,7 @@ const IDLE_TICKS: u32 = 300;
pub(super) struct Parked {
world: World,
streaming: Streaming,
locator: Option<Arc<VanillaTerrain>>,
save: Arc<SaveDir>,
block_ticks: Ticks,
tick_chunks: TickChunkMembership,
@@ -109,13 +110,21 @@ pub(super) struct Levels {
impl Game<'_> {
/// A new level for `dim`, opened from the save (`MinecraftServer.createLevels`).
fn open_level(&self, dim: Dim) -> Result<Parked> {
let (streaming, save) =
self.make_streaming(&self.world_root.clone(), dim, self.level.seed)?;
let (streaming, save, locator) = super::world_loading::make_streaming(
&self.res,
&self.pack,
&self.options,
&self.settings,
&self.world_root,
dim,
self.level.seed,
)?;
let mut world = World::new(self.res.facts().air, self.level.seed);
world.fast_lava = dim == Dim::Nether;
Ok(Parked {
world,
streaming,
locator,
save,
block_ticks: Ticks::new(self.level.seed as i32),
tick_chunks: Default::default(),
@@ -163,6 +172,9 @@ impl Game<'_> {
use std::mem::swap;
swap(&mut self.world, &mut p.world);
swap(&mut self.streaming, &mut p.streaming);
let mut locator = self.end.locator();
swap(&mut locator, &mut p.locator);
self.end.set_locator(locator);
swap(&mut self.save, &mut p.save);
swap(&mut self.block_ticks, &mut p.block_ticks);
swap(&mut self.tick_chunks, &mut p.tick_chunks);
+6 -5
View File
@@ -923,15 +923,16 @@ impl Game<'_> {
batch.rect_uv(ox, oy, 176.0, height, r, u32::MAX);
}
let grey = horizon_client::hud::rgba(64, 64, 64, 255);
let title = self
.remote_menu_title()
.unwrap_or_else(|| self.container_title(self.translate(key, fallback)));
let title = &self.remote_menu_title().unwrap_or_else(|| {
horizon_client::text::Text::literal(self.container_title(self.translate(key, fallback)))
.resolve_legacy()
});
let title_x = if screen == Screen::Hopper {
8.0
} else {
(176.0 - horizon_client::hud::HudBatch::text_width(atlas, &title)) / 2.0
(176.0 - atlas.font.width_text(title)) / 2.0
};
batch.text(atlas, &title, ox + title_x, oy + 6.0, grey, false);
batch.text_styled(atlas, title, ox + title_x, oy + 6.0, grey, false);
let inventory = self.translate("container.inventory", "Inventory");
batch.text(atlas, &inventory, ox + 8.0, oy + height - 94.0, grey, false);
true
@@ -1463,6 +1463,7 @@ impl Game<'_> {
lid: &lid,
sign: &|_| None,
banner: &|_| Vec::new(),
skull: &|_| None,
glyph: &|c| self.gui.atlas.glyph_cell(c),
};
self.res
+60 -2
View File
@@ -1,6 +1,7 @@
//! Capability `input` (docs/mods.md): the use button on items a mod claims,
//! and the mod's own keys. With no mod claiming items or keys, each hook
//! here returns after one check.
//! the mod's own keys and the mouse wheel; and `render`'s zoom. With no mod
//! claiming items, keys or the wheel, each hook here returns after one
//! check.
use super::Game;
use winit::{event::MouseButton, keyboard::KeyCode};
#[cfg(feature = "mods")]
@@ -20,6 +21,21 @@ impl Game<'_> {
false
}
pub(super) fn mod_key(&mut self, _key: KeyCode, _pressed: bool, _repeat: bool) {}
pub(super) fn mod_mouse_key(&mut self, _button: MouseButton, _pressed: bool) {}
pub(super) fn mod_wheel(&mut self, _steps: f32) -> bool {
false
}
pub(super) fn mod_zoom(&self) -> Option<ModZoom> {
None
}
}
/// A mod's zoom (`render.set-zoom`), clamped by the runtime.
#[derive(Clone, Copy)]
pub(super) struct ModZoom {
pub(super) factor: f32,
pub(super) smooth: bool,
pub(super) turn: f32,
}
#[cfg(feature = "mods")]
@@ -89,4 +105,46 @@ impl Game<'_> {
self.mods.mods.key(name, pressed);
}
}
/// A mouse button while playing: a mod's key bound to it
/// (`key.mouse.4`...), as `mod_key`.
pub(super) fn mod_mouse_key(&mut self, button: MouseButton, pressed: bool) {
if self.mods.mods.has_keys() {
let name = horizon_client::keybinds::mouse_name(button);
self.mods.mods.key(&name, pressed);
}
}
/// Wheel steps while playing: true when a mod holds the wheel, so the
/// hotbar doesn't move.
pub(super) fn mod_wheel(&mut self, steps: f32) -> bool {
self.mods.mods.wheel(steps)
}
/// Playing, with nothing over the world: mods' keys and zoom apply.
fn mod_playing(&self) -> bool {
self.screen == Screen::Playing
&& self.title.is_none()
&& self.sign_edit.is_none()
&& self.book.is_none()
}
/// Once a frame, before the mods run: a screen that opened lets their
/// held keys up.
pub(super) fn mod_input_frame(&mut self) {
let playing = self.mod_playing();
if self.mods.playing && !playing {
self.mods.mods.release_keys();
}
self.mods.playing = playing;
}
/// The strongest zoom a mod asks for, while playing; the spyglass's
/// own view wins.
pub(super) fn mod_zoom(&self) -> Option<ModZoom> {
if !self.mod_playing() || self.scoping() {
return None;
}
let z = self.mods.mods.zoom()?;
Some(ModZoom {
factor: z.factor,
smooth: z.smooth,
turn: z.turn,
})
}
}
+13 -3
View File
@@ -66,6 +66,9 @@ pub(super) struct ModsState {
/// Extras that are official mods now, being installed.
#[cfg(feature = "mods")]
pub(super) moved: super::moved_extras::MovedState,
/// Playing at the last frame, for `mod_input_frame`.
#[cfg(feature = "mods")]
pub(super) playing: bool,
}
/// The mappings `input.mapping` answers for, with the default key their
@@ -437,6 +440,7 @@ impl Game<'_> {
if !self.mods.mods.is_running() {
return;
}
self.mod_input_frame();
if self.mods.mods.wants_frames() {
let snapshot = self.mod_snapshot();
self.mods.mods.set_snapshot(snapshot);
@@ -510,10 +514,16 @@ impl Game<'_> {
}
};
// Not through system_message: a mod must not hear its own lines.
// Written by this client, so the Chat option doesn't hide it.
let focused = self.screen == Screen::Chat;
self.ui
.chat
.add(&self.gui.atlas, line, None, self.ticks, focused);
self.ui.chat.add(
&self.gui.atlas,
line,
None,
horizon_client::chat::ChatSource::Client,
self.ticks,
focused,
);
}
// A mod may set or clear its settings page in any call, not only
// while loading: its row's Settings button follows.
+40
View File
@@ -240,6 +240,42 @@ impl Game<'_> {
discrete: false,
});
}
// ItemFrameRenderer.shouldShowName/getNameTag: only the targeted
// frame's custom-named item, with the HUD visible (also glow frames).
if !self.ui.hide_gui
&& let Some((i, distance)) = self.frame_hit()
&& self.before_block(distance)
&& self.mob_hit().is_none_or(|(_, d)| d > distance)
&& let Some(name) = self.item_frames[i]
.item
.as_ref()
.and_then(|item| item.custom_name())
{
let frame = &self.item_frames[i];
let (lo, hi) = frame.bounds();
let center = glam::DVec3::from_array(std::array::from_fn(|a| (lo[a] + hi[a]) / 2.0));
if center.distance_squared(camera) < 64.0 * 64.0 {
// NAME_TAG is at the entity height (0.5), then the name
// submitter lifts it 0.5. ItemFrameRenderer offsets rendering
// 0.3 toward its facing and -0.25 vertically.
let step = frame.step();
tags.push(NameTag {
at: center
+ glam::DVec3::new(
f64::from(step[0] as f32 * 0.3),
0.75,
f64::from(step[2] as f32 * 0.3),
),
lines: vec![(Text::literal(name).resolve_legacy(), 0.0)],
light: self.world.light(
center.x.floor() as i32,
center.y.floor() as i32,
center.z.floor() as i32,
),
discrete: false,
});
}
}
tags
}
/// `SubmitNodeCollection.submitNameTag`: a discrete name is drawn once,
@@ -257,12 +293,16 @@ impl Game<'_> {
const FADED: u32 = 0x7F;
for tag in self.name_tags(camera, alpha) {
let o = tag.at - camera;
// `gbuffers_entities` draws name tag text (2.3); a sneaking player's faded tag, on its
// background, blends.
if tag.discrete {
self.mark_translucent_entity(out.len(), "minecraft:name_tag");
self.name_tag_quads(o, &tag.lines, FADED, true, 0, vertex_light(tag.light), out);
continue;
}
// LightCoordsUtil.lightCoordsWithEmission(lightCoords, 2).
let lit = (tag.light >> 4).max(2) << 4 | (tag.light & 15).max(2);
self.mark_entity(out.len(), "minecraft:name_tag");
self.name_tag_quads(o, &tag.lines, 0xFF, false, 0, vertex_light(lit), out);
// RenderPipelines.TEXT_SEE_THROUGH: no depth test, no lightmap.
self.name_tag_quads(o, &tag.lines, FADED, true, 2, 0, see_through);
@@ -412,7 +412,8 @@ impl Game<'_> {
}
/// Whether the player's own model shows in the world view.
fn player_model_shown(&self) -> bool {
!self.camera_type.first_person()
// A shader pack's shadow pass draws the player in first person too (shadowPlayer).
(!self.camera_type.first_person() || self.pack_shadow_casters.1)
&& self.sleeping.is_none()
&& !self.spectator()
&& self.potions.effects.get("minecraft:invisibility").is_none()
+201 -56
View File
@@ -58,6 +58,12 @@ fn remote_text(tag: &Tag) -> horizon_client::text::Text {
comp_text(&tag_to_json(tag))
}
/// `ClientboundOpenScreenPacket.title` as a container screen draws it: the
/// component's styles, and the `§` codes a literal holds (`StringDecomposer`).
fn menu_title(tag: &Tag) -> horizon_client::text::Text {
remote_text(tag).resolve_legacy()
}
/// A section's sky and block light layers from the wire.
type LightLayers = (Option<Vec<u8>>, Option<Vec<u8>>);
@@ -123,6 +129,9 @@ pub(super) struct Remote {
loaded_sent: bool,
/// The view distance last sent in ClientInformation.
sent_view: i8,
/// The Chat option's visibility last sent in ClientInformation
/// (`Options.broadcastOptions` sends it again when it changes).
sent_chat_visibility: i32,
/// BlockStatePredictionHandler: blocks changed ahead of the server (a predicted break),
/// with the sequence of the last action that changed them and the state the server
/// last said they have. Until the server acknowledges that sequence its block updates
@@ -130,8 +139,8 @@ pub(super) struct Remote {
predictions: HashMap<[i32; 3], (i32, i32)>,
/// Open container: (window id, menu name).
pub(super) menu: Option<(i32, String)>,
/// The open menu's title as the server named it.
pub(super) menu_title: Option<String>,
/// The open menu's title as the server named it, with its styles.
pub(super) menu_title: Option<horizon_client::text::Text>,
state_id: i32,
use_target: Option<[i32; 3]>,
entities: HashMap<i32, Tracked>,
@@ -422,6 +431,7 @@ impl Game<'_> {
abilities: None,
loaded_sent: false,
sent_view: view,
sent_chat_visibility: self.settings.chat_visibility as i32,
predictions: HashMap::new(),
menu: None,
menu_title: None,
@@ -489,6 +499,7 @@ impl Game<'_> {
r.session.disconnect();
}
self.server_packs_disconnect();
self.close_dialogs();
// Single player's server thread saves the world before the title shows.
let single_player = match self.integrated.take() {
Some(server) => {
@@ -630,14 +641,23 @@ impl Game<'_> {
)
}
/// A line in the connection's chat, with its styles, click and hover
/// events (ChatComponent.addMessage).
pub(super) fn chat_line(&mut self, content: horizon_client::text::Text) {
/// events (ChatComponent.addMessage). The lines the Chat option hides
/// are dropped there (its `visibleMessageFilter`).
pub(super) fn chat_line(
&mut self,
content: horizon_client::text::Text,
source: horizon_client::chat::ChatSource,
) {
if !self.ui.chat.shows(source) {
return;
}
let text = content.plain();
log::info!("[chat] {text}");
self.ui.chat.add(
&self.gui.atlas,
content,
None,
source,
self.ticks,
self.screen == Screen::Chat,
);
@@ -700,6 +720,23 @@ impl Game<'_> {
pub(super) fn remote_backlog(&self) -> usize {
self.remote.as_ref().map_or(0, |r| r.backlog.len())
}
/// A headless command oracle has a real connection but no movement tick.
/// Acknowledge loaded terrain without predicting or sending movement.
pub(super) fn poll_stationary_client(&mut self) -> Result<()> {
self.poll_remote()?;
let loaded = self.player.as_ref().is_some_and(|p| {
self.streaming.is_lit(
(p.position.x.floor() as i32) >> 4,
(p.position.z.floor() as i32) >> 4,
)
});
if loaded && self.remote.as_ref().is_some_and(|r| !r.loaded_sent) {
self.send(S::PlayerLoaded(Empty));
self.remote.as_mut().unwrap().loaded_sent = true;
}
Ok(())
}
/// Process what the network thread received (once per frame, like the
/// client's packet processor), in order. Chunk packets get
/// `CHUNK_PACKETS_PER_FRAME`: once a frame has spent it, the events from
@@ -736,6 +773,8 @@ impl Game<'_> {
} => {
self.set_registries(registries);
self.extras_joined();
// handleLogin: the level loading screen replaces any dialog.
self.close_dialogs();
if let Some(r) = self.remote.as_mut() {
r.joined = true;
r.profile = uuid;
@@ -764,6 +803,8 @@ impl Game<'_> {
}
Event::PackPush(p) => self.handle_pack_push(p),
Event::PackPop(id) => self.handle_pack_pop(id),
Event::ShowDialog(dialog) => self.show_dialog(&dialog),
Event::ClearDialog => self.clear_dialog(),
Event::DecodeError(e) => {
if let Some(r) = self.remote.as_mut() {
r.decode_errors += 1;
@@ -1402,13 +1443,29 @@ impl Game<'_> {
m.on_ground = on_ground;
return;
}
if let Some(p) = self.creatures.projectiles.iter_mut().find(|p| {
p.id == id as u32
&& matches!(
p.kind,
horizon_client::projectiles::ProjectileKind::LargeFireball { .. }
)
}) {
if path.is_some() {
p.position = Vec3::new(position[0], position[1], position[2]);
}
if let Some((yaw, pitch)) = rotation {
p.yaw = yaw;
p.pitch = pitch;
}
return;
}
if let Some(p) = r.pending_items.get_mut(&id) {
p.0 = position;
}
// ItemEntity and FallingBlockEntity keep Entity's NO_OP
// InterpolationHandler: moveOrInterpolateTo puts them there at
// once, and their own tick moves them on between the server's
// updates (`tick_remote_entities`).
// ItemEntity, FallingBlockEntity and PrimedTnt keep Entity's
// NO_OP InterpolationHandler: moveOrInterpolateTo puts them there
// at once, and their own tick moves them on between the server's
// updates (`tick_remote_entities`, `tick_remote_projectiles`).
let at = Vec3::new(position[0], position[1], position[2]);
if let Some(d) = self.dropped.iter_mut().find(|d| d.id == id as u32) {
d.position = at;
@@ -1419,6 +1476,11 @@ impl Game<'_> {
f.position = at;
return;
}
if let Some(t) = self.tnts.iter_mut().find(|t| t.id == id as u32) {
t.position = at;
t.on_ground = on_ground;
return;
}
// AbstractInterpolationHandler.interpolateTo: without a rotation,
// the one it was heading for.
let rotation = rotation.or_else(|| r.lerps.get(&id).and_then(|l| l.rotation));
@@ -1612,9 +1674,10 @@ impl Game<'_> {
if let Some(r) = self.remote.as_mut() {
r.sent_view = view;
}
self.send(S::ClientInformation(
horizon_protocol::packets::configuration::ClientInformation::new(view),
));
let mut info =
horizon_protocol::packets::configuration::ClientInformation::new(view);
info.chat_visibility = self.settings.chat_visibility as i32;
self.send(S::ClientInformation(info));
}
C::Respawn(p) => {
// A new player, without effects: the server sends them again.
@@ -1916,10 +1979,14 @@ impl Game<'_> {
}
// LivingEntity.DATA_LIVING_ENTITY_FLAGS: for another player
// too, remote_mob_data's apply reads index 8 above.
let other = self
.remote
.as_ref()
.is_some_and(|r| r.level_players.contains_key(&p.id) && r.entity_id != p.id);
let mannequin = self
.mobs
.iter()
.any(|m| m.id == p.id as u32 && &*m.kind.id == "minecraft:mannequin");
let other = mannequin
|| self.remote.as_ref().is_some_and(|r| {
r.level_players.contains_key(&p.id) && r.entity_id != p.id
});
// LivingEntity.onSyncedDataUpdated(SLEEPING_POS_ID): another
// player lies down in their bed (drawn lying in it), or gets up.
if other {
@@ -1970,11 +2037,13 @@ impl Game<'_> {
}
}
// Entity.DATA_SHARED_FLAGS_ID's crouching bit: another
// player's crouch (their model and Entity.isDiscrete's name tag).
if self
.remote
.as_ref()
.is_some_and(|r| r.level_players.contains_key(&p.id))
// player's or a mannequin's crouch (their model and
// Entity.isDiscrete's name tag).
if (mannequin
|| self
.remote
.as_ref()
.is_some_and(|r| r.level_players.contains_key(&p.id)))
&& let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32)
{
for (index, _, value) in &p.values {
@@ -2377,6 +2446,16 @@ impl Game<'_> {
}
self.merge_remote_tree();
}
C::Cooldown(p) => {
// ClientPacketListener.handleItemCooldown: the server starts
// (or confirms) a group's cooldown; 0 ends it.
let group: Arc<str> = Arc::from(p.group.as_str());
if p.duration == 0 {
self.cooldowns.remove(&group);
} else {
self.cooldowns.add(group, p.duration);
}
}
C::SystemChat(p) => {
// The component as sent, translated in the client's own
// language. ChatListener.handleOverlay for the action bar.
@@ -2384,7 +2463,7 @@ impl Game<'_> {
if p.overlay {
self.titles.set_overlay(text, false);
} else {
self.chat_line(text);
self.chat_line(text, horizon_client::chat::ChatSource::Server);
}
}
C::PlayerChat(p) => {
@@ -2394,12 +2473,12 @@ impl Game<'_> {
.as_ref()
.map_or_else(|| serde_json::Value::from(p.content.as_str()), tag_to_json);
let line = self.format_chat(&p.chat_type, content);
self.chat_line(line);
self.chat_line(line, horizon_client::chat::ChatSource::Player);
}
C::DisguisedChat(p) => {
let content = horizon_client::scoreboard::tag_to_json(&p.message);
let line = self.format_chat(&p.chat_type, content);
self.chat_line(line);
self.chat_line(line, horizon_client::chat::ChatSource::Player);
}
C::PlayerInfoUpdate(p) => {
if let Some(r) = self.remote.as_mut() {
@@ -2614,6 +2693,9 @@ impl Game<'_> {
C::SetTitleText(p) => self.titles.set_title(remote_text(&p.text)),
C::SetSubtitleText(p) => self.titles.set_subtitle(remote_text(&p.text)),
C::SetTitlesAnimation(p) => self.titles.set_times(p.fade_in, p.stay, p.fade_out),
// ClientCommonPacketListenerImpl.handleShowDialog / handleClearDialog.
C::ShowDialog(p) => self.show_play_dialog(&p.dialog),
C::ClearDialog(_) => self.clear_dialog(),
C::ClearTitles(p) => {
self.titles.clear();
if p.value {
@@ -2791,6 +2873,53 @@ impl Game<'_> {
{
mob.skin = Some(skin);
}
// ClientMannequin.updateSkin (PlayerSkinRenderCache.lookup): a
// mannequin's DATA_PROFILE `textures` property resolves like a
// player's, never required to be signed.
let mannequins: Vec<(u32, horizon_client::skins::Textures)> = self
.mobs
.iter()
.filter(|m| &*m.kind.id == "minecraft:mannequin" && m.profile.is_some())
.map(|m| {
let t = m
.profile
.as_ref()
.and_then(|p| p.properties.iter().find(|p| p.name == "textures"));
let textures = t.map_or_else(Default::default, |t| {
let signature = if t.signature.is_some() {
horizon_client::skins::Signature::Signed
} else {
horizon_client::skins::Signature::Unsigned
};
horizon_client::skins::unpack(
&t.value,
signature,
&horizon_client::skins::TEXTURE_URIS,
)
});
(m.id, textures)
})
.collect();
let mut resolved: Vec<(u32, Option<String>)> = Vec::new();
for (id, textures) in mannequins {
let skin = {
let mut downloads = self.skin_downloads.borrow_mut();
horizon_client::skins::lookup(&textures, false, |kind, url| {
downloads.state(kind, url)
})
};
resolved.push((id, skin));
}
for (id, skin) in resolved {
let skin = skin.filter(|s| !s.starts_with("skins/") || self.show_skin(s));
if let Some(mob) = self
.mobs
.iter_mut()
.find(|m| m.id == id && &*m.kind.id == "minecraft:mannequin")
{
mob.skin = skin;
}
}
}
/// Puts a downloaded skin in an atlas skin slot: a free one, else the
/// least recently shown.
@@ -2912,14 +3041,23 @@ impl Game<'_> {
});
}
/// `Entity.lerpMotion` (and `recreateFromPacket`'s motion) for the
/// entities whose own tick moves them on a client: dropped items and
/// falling blocks.
/// entities whose own tick moves them on a client: dropped items, falling
/// blocks and primed TNT.
fn remote_motion(&mut self, id: i32, velocity: [f64; 3]) {
let v = Vec3::new(velocity[0], velocity[1], velocity[2]);
if let Some(d) = self.dropped.iter_mut().find(|d| d.id == id as u32) {
d.velocity = v;
} else if let Some(f) = self.falling.iter_mut().find(|f| f.id == id as u32) {
f.velocity = v;
} else if let Some(t) = self.tnts.iter_mut().find(|t| t.id == id as u32) {
t.velocity = v;
} else if let Some(p) = self
.creatures
.projectiles
.iter_mut()
.find(|p| p.id == id as u32)
{
p.velocity = v;
} else if let Some(p) = self
.remote
.as_mut()
@@ -3032,16 +3170,19 @@ impl Game<'_> {
fn tick_remote_level(&mut self) -> Result<()> {
self.ticks += 1;
self.track_experience_display();
// Options.broadcastOptions: a changed render distance is sent again.
// Options.broadcastOptions: a changed render distance or Chat option
// is sent again.
let view = client_view_distance(self.settings.render_distance);
let visibility = self.settings.chat_visibility as i32;
if let Some(r) = self.remote.as_mut()
&& r.joined
&& r.sent_view != view
&& (r.sent_view != view || r.sent_chat_visibility != visibility)
{
r.sent_view = view;
self.send(S::ClientInformation(
horizon_protocol::packets::configuration::ClientInformation::new(view),
));
r.sent_chat_visibility = visibility;
let mut info = horizon_protocol::packets::configuration::ClientInformation::new(view);
info.chat_visibility = visibility;
self.send(S::ClientInformation(info));
}
// Minecraft.tick: TickRateManager.tick; ClientLevel.tickTime advances
// the time only while the level runs normally.
@@ -3404,6 +3545,10 @@ impl Game<'_> {
m.head_yaw += wrap_degrees(*yaw - m.head_yaw) / *steps as f32;
*steps -= 1;
}
// Chicken.aiStep: the wings follow the server's on-ground flag.
if m.species() == horizon_client::mobs::Species::Chicken {
horizon_client::mobs::farm::chicken_flap(m);
}
// LivingEntity.updatingUsingItem: the ticks the item has been
// used before this one. Another player's use particles; a mob's
// drawn bow only needs the count (its pull follows it).
@@ -4123,8 +4268,12 @@ impl Game<'_> {
let Some(info) = self.res.items().get(&held.item).cloned() else {
return false;
};
// MultiPlayerGameMode.useItem runs the use here too: food starts
// being eaten (Consumable.startConsuming), a book and quill opens.
// MultiPlayerGameMode.useItem runs the use here too (inside
// startPrediction): the `use_cooldown` component starts its entry, and
// the server's ClientboundCooldownPacket confirms it.
self.apply_use_cooldown(&held);
// Food starts being eaten (Consumable.startConsuming), a book and
// quill opens.
self.with_hand(hand, |g| g.start_eating(&info));
if self.with_hand(hand, |g| g.remote_use_book(&held)) {
return true;
@@ -4463,10 +4612,12 @@ impl Game<'_> {
_ => Ok(false),
}
}
/// ClientPacketListener.sendChat / sendCommand (unsigned).
/// ClientPacketListener.sendChat / sendCommand (unsigned). The Chat
/// option's Hidden and System levels keep chat out and Hidden keeps the
/// commands out (ChatAbilities).
pub(super) fn send_chat(&mut self, message: &str) {
let message = message.trim();
if message.is_empty() {
if message.is_empty() || !self.chat_abilities().allows(message) {
return;
}
if let Some(command) = message.strip_prefix('/') {
@@ -4781,14 +4932,7 @@ impl Game<'_> {
return;
}
if let Some(kind) = Self::remote_station_kind(&menu) {
let title = plain(
&p.title,
&self
.remote
.as_ref()
.map(|r| r.lang.clone())
.unwrap_or_default(),
);
let title = menu_title(&p.title);
let pos = target.map_or([0, 0, 0], |t| [t.0, t.1, t.2]);
self.remote_open_station(p.container, menu, kind, title, pos);
return;
@@ -4847,20 +4991,16 @@ impl Game<'_> {
self.send(S::ContainerClose(play::ContainerClose {
container: p.container,
}));
self.chat_line(horizon_client::text::Text::literal(format!(
"(Horizon cannot show {menu} screens yet)"
)));
self.chat_line(
horizon_client::text::Text::literal(format!(
"(Horizon cannot show {menu} screens yet)"
)),
horizon_client::chat::ChatSource::Client,
);
return;
}
};
let title = plain(
&p.title,
&self
.remote
.as_ref()
.map(|r| r.lang.clone())
.unwrap_or_default(),
);
let title = menu_title(&p.title);
if let Some(r) = self.remote.as_mut() {
r.menu = Some((p.container, menu.clone()));
r.menu_title = Some(title);
@@ -4904,7 +5044,7 @@ impl Game<'_> {
Some(self.remote.as_ref()?.menu.as_ref()?.1.as_str())
}
/// The title the server gave the menu it opened.
pub(super) fn remote_menu_title(&self) -> Option<String> {
pub(super) fn remote_menu_title(&self) -> Option<horizon_client::text::Text> {
self.remote.as_ref()?.menu_title.clone()
}
/// A language change reaches the server-sent components too: their titles
@@ -4917,7 +5057,7 @@ impl Game<'_> {
}
}
/// A server's chest-like menu (`MenuType.GENERIC_9xN`): its rows and title.
pub(super) fn remote_chest_menu(&self) -> Option<(usize, String)> {
pub(super) fn remote_chest_menu(&self) -> Option<(usize, horizon_client::text::Text)> {
let r = self.remote.as_ref()?;
let rows = r
.menu
@@ -4926,7 +5066,12 @@ impl Game<'_> {
.strip_prefix("minecraft:generic_9x")?
.parse()
.ok()?;
Some((rows, r.menu_title.clone().unwrap_or_default()))
Some((
rows,
r.menu_title
.clone()
.unwrap_or_else(horizon_client::text::Text::empty),
))
}
/// ServerboundPlayerCommandPacket STOP_SLEEPING.
pub(super) fn remote_stop_sleeping(&mut self) {
@@ -75,6 +75,11 @@ fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin) {
// Zombie's three): what VillagerProfessionLayer draws.
(Species::Villager, 19, Value::Ids(data))
| (Species::ZombieVillager, 20, Value::Ids(data)) => villager_data(m, data, builtin),
// Mannequin.DATA_PROFILE (after Avatar's two): whose skin it
// wears; update_remote_skins resolves it like a tab player's.
(Species::Mannequin, 17, Value::Profile(profile)) => {
m.profile = Some(profile.clone());
}
_ => {}
}
}
@@ -295,4 +300,22 @@ mod tests {
apply(&mut e, &[(17, s::BYTE, Value::Byte(2))]);
assert_eq!(e.extra.variant2, 2);
}
#[test]
fn a_mannequin_exists_and_takes_its_profile() {
let Some(mut m) = mob("minecraft:mannequin") else {
return;
};
assert_eq!(&*m.kind.id, "minecraft:mannequin");
let profile = horizon_protocol::entity_data::Profile {
name: Some("Gommunity".into()),
uuid: Some(1),
..Default::default()
};
apply(
&mut m,
&[(17, s::RESOLVABLE_PROFILE, Value::Profile(profile.clone()))],
);
assert_eq!(m.profile, Some(profile));
}
}
@@ -220,10 +220,21 @@ impl Game<'_> {
/// One tick of interpolation, as `tick_remote_objects`.
pub(super) fn tick_remote_projectiles(&mut self, mut step: impl FnMut(u32, Vec3) -> Vec3) {
for t in &mut self.tnts {
t.previous = t.position;
t.position = step(t.id, t.position);
}
// PrimedTnt.tick runs on a client too: it falls, bounces and burns its
// fuse down (the swell and flash follow it) between the server's
// updates (every 10 ticks), and a client discards it when the fuse
// runs out; only the server explodes it.
let (world, collision, assets, facts) = (
&self.world,
self.collision,
&self.pack.assets,
self.res.facts(),
);
self.tnts.retain_mut(|t| {
// A fluid lookup that fails leaves it where its move put it.
let _ = t.tick(world, collision, assets, facts);
t.fuse > 0
});
// FallingBlockEntity.tick runs on a client too: the block falls by
// itself between the server's updates (every 20 ticks), then waits
// where it lands for the server to place it and remove it.
@@ -238,7 +249,25 @@ impl Game<'_> {
for p in &mut self.creatures.projectiles {
p.previous = p.position;
p.age += 1;
p.position = step(p.id, p.position);
if matches!(p.kind, ProjectileKind::LargeFireball { .. }) {
// AbstractHurtingProjectile.tick advances on the client between
// the tracker updates (ten ticks apart for a ghast's fireball).
let at = p.position;
let water = self
.res
.facts()
.get(self.world.block(
at.x.floor() as i32,
at.y.floor() as i32,
at.z.floor() as i32,
))
.fluid
== horizon_client::blocks::Fluid::Water;
p.apply_hurting_inertia(water);
p.position = p.position.add_vector(p.velocity);
} else {
p.position = step(p.id, p.position);
}
}
for t in &mut self.tridents {
let id = t.id;
@@ -18,7 +18,7 @@ impl Game<'_> {
container: i32,
menu: String,
kind: Kind,
title: String,
title: horizon_client::text::Text,
pos: [i32; 3],
) {
if let Some(r) = self.remote.as_mut() {
+36 -6
View File
@@ -63,6 +63,12 @@ struct Steps {
on_ground: bool,
in_water: bool,
}
#[derive(Default)]
pub(super) struct PlayerEvents {
steps: Option<Steps>,
last_health: Option<f32>,
}
#[derive(Default)]
pub(super) struct SculkState {
/// The player's warning level toward a warden.
@@ -86,6 +92,15 @@ pub(super) struct SculkState {
}
impl SculkState {
pub(super) fn swap_player(&mut self, other: &mut PlayerEvents) {
let steps = self.steps.remove(&Who::Player);
if let Some(guest) = other.steps.take() {
self.steps.insert(Who::Player, guest);
}
other.steps = steps;
std::mem::swap(&mut self.last_health, &mut other.last_health);
}
/// Exchange the level-held parts (its sculk block entities and the
/// chunks scanned for them) with another level's (game/levels.rs).
pub(super) fn swap_level(&mut self, other: &mut Self) {
@@ -718,17 +733,24 @@ impl Game<'_> {
})
};
// The ticking ones in the index's order, with whether the chunks
// around them tick too.
// around them tick too. Most sensors and catalysts wait for a
// vibration or a death: their tick would change nothing, so they
// are passed over here (a vibration scheduled during these ticks is
// chosen on the next tick at the earliest, as in vanilla).
let entities = &self.world.block_entities;
let due: Vec<((i32, i32, i32), bool)> = self
.sculk
.index
.iter()
.filter_map(|&key| {
let (cx, cz) = (key.0 >> 4, key.2 >> 4);
ticks(cx, cz).then(|| {
let adjacent = (-1..=1).all(|dx| (-1..=1).all(|dz| ticks(cx + dx, cz + dz)));
(key, adjacent)
})
if !ticks(cx, cz)
|| matches!(entities.get(&key), Some(BlockEntity::Sculk(e)) if e.idle())
{
return None;
}
let adjacent = (-1..=1).all(|dx| (-1..=1).all(|dz| ticks(cx + dx, cz + dz)));
Some((key, adjacent))
})
.collect();
for (key, adjacent) in due {
@@ -1491,6 +1513,12 @@ impl Game<'_> {
/// Events from entities moving, getting hurt and dying, derived from what
/// changed this tick.
fn tick_entity_events(&mut self) {
self.tick_player_sculk_events();
self.tick_mob_sculk_events();
}
/// Entity.move emits on the authoritative server too, for each ServerPlayer.
pub(super) fn tick_player_sculk_events(&mut self) {
// The player.
let player = self.player.as_ref().map(|p| {
(
@@ -1518,7 +1546,9 @@ impl Game<'_> {
}
self.sculk.last_health = Some(health);
}
// Mobs.
}
fn tick_mob_sculk_events(&mut self) {
let snapshot: Vec<(u32, [f64; 3], bool, bool, Species, Option<i64>)> = self
.mobs
.iter()
@@ -156,6 +156,10 @@ const CLIENT_SOUNDS: &[&str] = &[
impl Game<'_> {
/// Run every scenario of `spec` ("SCENARIOS,OUTPUT") and write the results.
pub(super) fn sculk_fixture(&mut self, spec: &str) -> Result<()> {
assert!(
!self.options.execution_role.is_client(),
"fixture requires a server Game"
);
let (input, output) = spec
.split_once(',')
.ok_or("HORIZON_SCULK_FIXTURE=SCENARIOS,OUTPUT")?;
@@ -170,7 +170,7 @@ impl Game<'_> {
/// The player moves to `target`'s level (opened if nobody was there)
/// and is told it is there, at the approximate exit, while it loads. The
/// level whose tick is running is put back in place afterwards.
fn start_server_trip(
pub(super) fn start_server_trip(
&mut self,
host: &mut LanHost,
conn: u32,
+285 -4
View File
@@ -4,9 +4,30 @@
use horizon_client::camera::CameraFluid;
use horizon_client::gfx::pack_uniforms::PackInputs;
use horizon_shaders::{Macros, Pack, Properties, dimension_folder, load_post};
use horizon_shaders::{BlockIds, Macros, Pack, Properties, dimension_folder, load_post};
use super::{Dim, Game};
use horizon_client::gfx::pack_draw::EntitySpan;
use std::cell::RefCell;
use std::collections::HashMap;
/// The ids a pack gives entities and block entities (4.1, 5.3), for the spans of the entity
/// geometry.
pub(super) struct PackIds {
entities: HashMap<String, i32>,
blocks: BlockIds,
/// `blockEntityId` by block state, filled as states are drawn.
block_states: RefCell<HashMap<u32, i32>>,
/// The id of a name the file lacks: 65535, or 0 without the file.
missing_entity: i32,
missing_block: i32,
/// `item.properties` (4.1) and the id of a held item the file lacks, or of an empty hand:
/// −1, or 0 without the file (5.3).
items: HashMap<String, i32>,
missing_item: i32,
/// `oldHandLight` (3.2): the first held item and light are those of the brighter hand.
old_hand_light: bool,
}
impl Game<'_> {
/// Loads the pack for the current dimension when it changed, and returns this frame's
@@ -42,8 +63,49 @@ impl Game<'_> {
);
}
let folder = dimension_folder(&pack, dimension);
let post = load_post(&pack, &folder, &properties, &macros, None);
if let Some((programs, notes)) = renderer.set_shader_pack(dimension, Some(post))
let inputs = renderer.pack_inputs();
let mut post = load_post(&pack, &folder, &properties, &macros, &inputs);
let biomes = self.res.biomes();
let names: Vec<&str> = biomes.list.iter().map(|b| b.name.as_str()).collect();
post.custom.set_biomes(&names);
let text = pack.read_text("block.properties").unwrap_or_default();
let ids = BlockIds::parse(&Properties::parse(&text, &macros));
for note in &ids.notes {
log::warn!("shader pack {name}: block.properties: {note}");
}
let text = pack.read_text("entity.properties");
let entities = BlockIds::parse_kind(
&Properties::parse(text.as_deref().unwrap_or_default(), &macros),
"entity",
);
let text = pack.read_text("item.properties");
let items = BlockIds::parse_kind(
&Properties::parse(text.as_deref().unwrap_or_default(), &macros),
"item",
);
self.pack_ids = Some(PackIds {
items: items.plain(),
missing_item: if text.is_some() { -1 } else { 0 },
// OQ-21: on unless the pack turns it off.
old_hand_light: properties
.get("oldHandLight")
.is_none_or(|v| v.trim() != "false"),
entities: entities.plain(),
missing_entity: if text.is_some() { 65535 } else { 0 },
missing_block: if ids.is_empty() { 0 } else { 65535 },
blocks: ids.clone(),
block_states: RefCell::default(),
});
let started_ids = std::time::Instant::now();
let table = block_id_table(&self.pack, &ids);
log::info!(
"shader pack {name}: mc_Entity for {} of {} block sprites in {} ms",
table.chunks(2).filter(|p| p[0] != -1).count(),
table.len() / 2,
started_ids.elapsed().as_millis()
);
if let Some((programs, notes)) =
renderer.set_shader_pack(dimension, Some(post), &table)
{
for note in notes {
log::warn!("shader pack {name}: {note}");
@@ -57,14 +119,22 @@ impl Game<'_> {
}
}
Err(e) => {
self.pack_ids = None;
log::warn!("shader pack {name}: {e}");
renderer.set_shader_pack(dimension, None);
renderer.set_shader_pack(dimension, None, &[]);
}
}
}
let lerp = |(old, new): (f32, f32)| old + (new - old) * partial;
let at = camera.floor().as_ivec3();
let light = self.world.light(at.x, at.y, at.z);
let biome = self
.world
.chunks
.get(&(at.x >> 4, at.z >> 4))
.map(|c| c.biome(at.x, at.y, at.z));
let (held_items, held_light) = self.held_for_pack();
let info = biome.map(|b| self.res.biomes().get(b));
PackInputs {
day_time: self.world.day_time,
rain: lerp(self.world.weather.rain_level),
@@ -79,6 +149,217 @@ impl Game<'_> {
eye_light: [i32::from(light & 15), i32::from(light >> 4)],
hide_gui: self.ui.hide_gui,
brightness: self.settings.gamma,
biome: biome.map_or(-1, i32::from),
biome_category: info.map_or(0, |b| horizon_shaders::biome_category(&b.name)),
biome_precipitation: info.map_or(0, |b| {
horizon_shaders::biome_precipitation(b.precipitation, b.temperature)
}),
temperature: info.map_or(0.0, |b| b.temperature),
rainfall: info.map_or(0.0, |b| b.downfall),
held_items,
held_light,
}
}
/// `heldItemId(2)` and `heldBlockLightValue(2)`: main hand, then off hand (5.2, 5.3).
fn held_for_pack(&mut self) -> ([i32; 2], [i32; 2]) {
if self.pack_ids.is_none() {
return ([0; 2], [0; 2]);
}
if self.luminance.is_none() {
self.luminance = Some(horizon_client::dynamic_light::Luminance::new(
self.res.facts(),
self.pack.assets.registry,
));
}
let (Some(ids), Some(lum)) = (&self.pack_ids, &self.luminance) else {
return ([0; 2], [0; 2]);
};
let hands = [self.selected, super::shield::OFFHAND_SLOT].map(|slot| {
let item = self.stack_in(slot).map(|s| &*s.item);
let id = item.and_then(|i| ids.items.get(i).copied());
(
id.unwrap_or(ids.missing_item),
item.map_or(0, |i| i32::from(lum.of(i))),
)
});
let first = if ids.old_hand_light && hands[1].1 > hands[0].1 {
hands[1]
} else {
hands[0]
};
([first.0, hands[1].0], [first.1, hands[1].1])
}
}
impl Game<'_> {
/// Starts a span of the entity geometry at vertex `start` for entity type `name`
/// (`minecraft:zombie`; "" for one the pack cannot name). Nothing without a pack.
pub(super) fn mark_entity(&self, start: usize, name: &str) {
self.mark_entity_part(start, name, false);
}
/// Starts a span for entity geometry that blends (name tags, shadows).
pub(super) fn mark_translucent_entity(&self, start: usize, name: &str) {
self.mark_entity_part(start, name, true);
}
fn mark_entity_part(&self, start: usize, name: &str, translucent: bool) {
if let Some(ids) = &self.pack_ids {
let id = ids.entities.get(name).copied();
self.push_span(start, "ENTITIES", id.unwrap_or(ids.missing_entity));
if let Some(last) = self.entity_spans.borrow_mut().last_mut() {
last.translucent = translucent;
}
}
}
/// Starts a span for the block entity of block state `state`.
pub(super) fn mark_block_entity(&self, start: usize, state: u32) {
let Some(ids) = &self.pack_ids else { return };
let id = *ids
.block_states
.borrow_mut()
.entry(state)
.or_insert_with(|| {
let registry = self.pack.assets.registry;
registry
.state(horizon_world::registry::BlockStateId(state))
.and_then(|s| {
ids.blocks
.id(&s.name, |k| s.properties.get(k).map(String::as_str))
})
.unwrap_or(ids.missing_block)
});
self.push_span(start, "BLOCK_ENTITIES", id);
let emission = self
.res
.facts()
.get(horizon_world::registry::BlockStateId(state))
.emission;
if let Some(last) = self.entity_spans.borrow_mut().last_mut() {
last.emissive = emission > 0;
}
}
/// Starts the span of the local player's model; `hidden` in first person, where only a
/// pack's shadow pass draws it.
pub(super) fn mark_player(&self, start: usize, hidden: bool) {
self.mark_entity(start, "minecraft:player");
if self.pack_ids.is_some()
&& let Some(last) = self.entity_spans.borrow_mut().last_mut()
{
last.player = true;
last.hidden = hidden;
}
}
/// Marks vertices `start..end` (one entity) as hidden from the camera, for an entity
/// Horizon's culling would drop but a pack's shadow pass still draws (`EntitySpan::hide`).
pub(super) fn hide_spans_from(&self, start: usize, end: usize) -> bool {
EntitySpan::hide(&mut self.entity_spans.borrow_mut(), start, end)
}
/// Starts a span of geometry that is not an entity (`PARTICLES`, `DESTROY`...).
pub(super) fn mark_stage(&self, start: usize, stage: &str) {
if self.pack_ids.is_some() {
self.push_span(start, stage, 0);
}
}
fn push_span(&self, start: usize, stage: &str, id: i32) {
let mut spans = self.entity_spans.borrow_mut();
let span = EntitySpan {
start: start as u32,
stage: horizon_shaders::render_stage(stage),
id,
..Default::default()
};
// A span that drew nothing gives way to the next.
match spans.last_mut() {
Some(last) if last.start == span.start => *last = span,
_ => spans.push(span),
}
}
}
/// `mc_Entity` (x, y) of each block sprite layer, as (id, fluid) pairs: terrain vertices carry
/// their sprite layer, not their block. A layer takes the id of the block named like its sprite
/// (`block/oak_leaves` for `oak_leaves`), else the id most blocks drawing it share (ties: the
/// lowest); -1 when no block drawing it has one. y is 1 for the fluid sprites, -1 otherwise.
fn block_id_table(pack: &super::PackResources, ids: &BlockIds) -> Vec<i32> {
use std::collections::{BTreeMap, BTreeSet};
let layers = pack
.render
.sprite_layers
.values()
.max()
.map_or(0, |l| l + 1) as usize;
let mut table = vec![-1; layers * 2];
if ids.is_empty() {
return table;
}
let registry = pack.assets.registry;
let catalog = &pack.render.model_catalog;
let sprite = |name: &str| pack.render.sprite_layers.get(name).copied();
// (layer, id) -> (a block named like the sprite, the blocks).
let mut votes: BTreeMap<(u32, i32), (bool, BTreeSet<&str>)> = BTreeMap::new();
for i in 0..registry.len() {
let state_id = horizon_world::registry::BlockStateId(i as u32);
let Some(state) = registry.state(state_id) else {
continue;
};
let Some(id) = ids.id(&state.name, |k| state.properties.get(k).map(String::as_str)) else {
continue;
};
let block = state.name.strip_prefix("minecraft:").unwrap_or(&state.name);
let mut vote = |layer: u32| {
let named = pack
.layer_names
.get(&layer)
.and_then(|n| n.strip_prefix("block/"))
== Some(block);
let v = votes.entry((layer, id)).or_default();
v.0 |= named;
v.1.insert(&state.name);
};
// Fluids draw from their own sprites.
let fluid = match block {
"water" => ["water_still", "water_flow"],
"lava" => ["lava_still", "lava_flow"],
_ => ["", ""],
};
for f in fluid.iter().filter(|f| !f.is_empty()) {
if let Some(l) = sprite(&format!("minecraft:block/{f}")) {
vote(l);
}
}
let origin = horizon_core::position::BlockPos::new(0, 0, 0);
for (_, model) in catalog.collect(state_id, origin).unwrap_or_default() {
for face in &model.faces {
vote(face.layer);
}
}
}
let mut best: BTreeMap<u32, (bool, usize, i32)> = BTreeMap::new();
for ((layer, id), (named, blocks)) in votes {
let candidate = (named, blocks.len(), -id);
let b = best.entry(layer).or_insert(candidate);
if candidate > *b {
*b = candidate;
}
}
for (layer, (_, _, id)) in best {
if let Some(slot) = table.get_mut(layer as usize * 2) {
*slot = -id;
}
}
for f in ["water_still", "water_flow", "lava_still", "lava_flow"] {
if let Some(l) = sprite(&format!("minecraft:block/{f}"))
&& let Some(slot) = table.get_mut(l as usize * 2 + 1)
{
*slot = 1;
}
}
table
}
+15 -11
View File
@@ -11,6 +11,7 @@ use horizon_client::{
hud::{HudBatch, rgba},
items::ItemStack,
stations::{self, AnvilResult, EnchantOffers},
text::Text,
};
use horizon_core::random::{LegacyRandom, RandomSource};
@@ -1160,15 +1161,15 @@ impl Game<'_> {
}
}
let default = self.translate("container.brewing", "Brewing Stand");
let title = self
.station
.as_ref()
.and_then(|s| self.world.block_names.get(&(s.pos[0], s.pos[1], s.pos[2])))
.cloned()
.or_else(|| self.remote_menu_title())
.unwrap_or(default);
let tw = HudBatch::text_width(atlas, &title);
batch.text(
let title = self.remote_menu_title().unwrap_or_else(|| {
let named = self
.station
.as_ref()
.and_then(|s| self.world.block_names.get(&(s.pos[0], s.pos[1], s.pos[2])));
Text::literal(named.cloned().unwrap_or(default)).resolve_legacy()
});
let tw = atlas.font.width_text(&title);
batch.text_styled(
atlas,
&title,
ox + (176.0 - tw) / 2.0,
@@ -1195,7 +1196,10 @@ impl Game<'_> {
}
}
} else if let Some(kind) = self.station.as_ref().map(|s| s.kind) {
let title = self.translate(kind.title_key(), "");
// A server's menu has the title it was given.
let title = self
.remote_menu_title()
.unwrap_or_else(|| Text::literal(self.translate(kind.title_key(), "")));
{
let atlas = &self.gui.atlas;
Self::part(
@@ -1207,7 +1211,7 @@ impl Game<'_> {
(ox, oy),
);
let (tx, ty) = kind.title_pos();
batch.text(atlas, &title, ox + tx, oy + ty, GREY, false);
batch.text_styled(atlas, &title, ox + tx, oy + ty, GREY, false);
batch.text(atlas, &inventory_label, ox + 8.0, oy + 72.0, GREY, false);
}
if kind == Kind::Cartography {
+51 -9
View File
@@ -158,7 +158,8 @@ const COLUMNS: [(&str, &str); 6] = [
#[derive(Default)]
pub(super) struct StatsUi {
tab: usize,
scroll: f32,
/// The list's scrolling: the wheel and the held scrollbar.
scroll: horizon_client::ui::Scroll,
sort: Option<(usize, i32)>,
}
/// `MenuTabBar` geometry: three tabs over at most 400 pixels.
@@ -182,6 +183,15 @@ impl Game<'_> {
self.stats_ui = StatsUi::default();
self.screen = Screen::Stats;
}
/// The list's scrollbar column (right of the 280 pixel list) and the
/// pixels it scrolls.
fn stats_bar(w: f32) -> f32 {
((w - 280.0) / 2.0).floor() + 280.0 + 6.0
}
fn stats_content(&self) -> f32 {
let (row_h, rows) = self.stats_rows();
row_h * rows as f32 + 4.0
}
fn stats_rows(&self) -> (f32, usize) {
match self.stats_ui.tab {
0 => (14.0, CUSTOM.len()),
@@ -260,10 +270,9 @@ impl Game<'_> {
// The list.
let (row_h, rows) = self.stats_rows();
let content = row_h * rows as f32 + 4.0;
let max_scroll = (content - (bottom - top)).max(0.0);
self.stats_ui.scroll = self.stats_ui.scroll.clamp(0.0, max_scroll);
self.stats_ui.scroll.clamp(top, bottom, content);
let left = ((w - 280.0) / 2.0).floor();
let scroll = self.stats_ui.scroll;
let scroll = self.stats_ui.scroll.amount;
let row_y = |i: usize| top + 4.0 + i as f32 * row_h - scroll;
let visible = |y: f32| y >= top && y + row_h <= bottom;
match self.stats_ui.tab {
@@ -408,11 +417,11 @@ impl Game<'_> {
ui::scrollbar(
batch,
atlas,
left + 280.0 + 6.0,
Self::stats_bar(w),
top,
bottom,
content,
self.stats_ui.scroll,
self.stats_ui.scroll.amount,
);
self.draw_stats_footer(batch, w, h);
}
@@ -433,6 +442,10 @@ impl Game<'_> {
);
}
pub(super) fn stats_click(&mut self, size: [u32; 2]) {
// A press on the scrollbar holds it and clicks nothing else.
if self.stats_press_scrollbar(size) {
return;
}
let b = HudBatch::new(size);
let (w, h) = (b.width, b.height);
let (x0, tw) = tabs(w);
@@ -441,7 +454,7 @@ impl Game<'_> {
&& self.stats_ui.tab != i
{
self.stats_ui.tab = i;
self.stats_ui.scroll = 0.0;
self.stats_ui.scroll.amount = 0.0;
self.audio
.play("minecraft:ui.button.click", None, 0.25, 1.0);
return;
@@ -461,7 +474,7 @@ impl Game<'_> {
}
if self.stats_ui.tab == 1 {
let left = ((w - 280.0) / 2.0).floor();
let y = TAB_H + 4.0 - self.stats_ui.scroll;
let y = TAB_H + 4.0 - self.stats_ui.scroll.amount;
for c in 0..COLUMNS.len() {
let x = left + 75.0 + 40.0 * c as f32 - 18.0;
if ui::hovered(self.cursor, x, y + 1.0, 18.0, 18.0) {
@@ -479,6 +492,35 @@ impl Game<'_> {
}
pub(super) fn stats_scroll(&mut self, amount: f32) {
let (row_h, _) = self.stats_rows();
self.stats_ui.scroll -= amount * row_h / 2.0;
let h = self.gui_size().1;
let content = self.stats_content();
self.stats_ui
.scroll
.wheel(amount, row_h / 2.0, TAB_H, h - FOOTER, content);
}
/// A press on the statistics list's scrollbar holds it
/// (`AbstractSelectionList.updateScrolling`).
pub(super) fn stats_press_scrollbar(&mut self, size: [u32; 2]) -> bool {
let b = HudBatch::new(size);
let (w, h) = (b.width, b.height);
let content = self.stats_content();
self.stats_ui
.scroll
.press(self.cursor, Self::stats_bar(w), TAB_H, h - FOOTER, content)
}
/// The pointer moved while the scrollbar is held.
pub(super) fn stats_drag(&mut self) {
if !self.stats_ui.scroll.held() {
return;
}
let h = self.gui_size().1;
let content = self.stats_content();
self.stats_ui
.scroll
.drag(self.cursor.1, TAB_H, h - FOOTER, content);
}
/// The button came up: the scrollbar lets go.
pub(super) fn stats_release(&mut self) {
self.stats_ui.scroll.release();
}
}
+4 -4
View File
@@ -2,7 +2,7 @@
//! kept in the dropped item's `container` component) and ender chests (the
//! player's `PlayerEnderChestContainer`, saved as `EnderItems`).
use super::Game;
use horizon_client::{containers::BlockEntity, items::ItemStack};
use horizon_client::{containers::BlockEntity, items::ItemStack, text::Text};
pub(super) fn is_shulker_box(name: &str) -> bool {
name.ends_with("shulker_box")
@@ -233,7 +233,7 @@ impl Game<'_> {
generic.then(|| self.container_size() / 9)
}
/// The title of the open chest-like menu.
fn chest_title(&self, rows: usize) -> String {
fn chest_title(&self, rows: usize) -> Text {
if let Some((_, title)) = self.remote_chest_menu() {
return title;
}
@@ -246,7 +246,7 @@ impl Game<'_> {
_ if rows > 3 => ("container.chestDouble", "Large Chest"),
_ => ("container.chest", "Chest"),
};
self.container_title(self.translate(key, fallback))
Text::literal(self.container_title(self.translate(key, fallback))).resolve_legacy()
}
/// `ContainerScreen` / `ShulkerBoxScreen`: the background with its
/// container rows, the title and the inventory label.
@@ -294,7 +294,7 @@ impl Game<'_> {
r.u1 = r.u0 + 176.0 * du;
batch.rect_uv(ox, oy + top, 176.0, 96.0, r, u32::MAX);
}
batch.text(
batch.text_styled(
atlas,
&self.chest_title(rows),
ox + 8.0,
+1 -1
View File
@@ -33,7 +33,7 @@ impl PrimedTnt {
),
)
}
fn tick(
pub(super) fn tick(
&mut self,
world: &horizon_client::world::World,
collision: &horizon_world::block_collision::BlockCollisionRegistry<'_>,
-4
View File
@@ -52,10 +52,6 @@ fn triggers(name: &str) -> bool {
impl Game<'_> {
/// `WindChargeItem.use`.
pub(super) fn throw_wind_charge(&mut self) -> Result<bool> {
if self.creatures.wind_cooldown > 0 {
return Ok(true);
}
self.creatures.wind_cooldown = 10;
let (Some(eye), Some(p)) = (self.eye_position(), self.player.as_ref()) else {
return Ok(false);
};
@@ -246,6 +246,9 @@ pub(super) fn make_streaming(
let inner = Arc::new(VanillaTerrain::for_world(&options.resources.join("worldgen"), pack.assets.registry, res.biomes(), seed, dim.id(), structures).map_err(|e| {
format!("{e}\nPrepare the worldgen bundle with the launcher, or: horizon-prepare --prepare")
})?);
if dim == Dim::Overworld && structures {
inner.search_rings_behind();
}
locator = Some(inner.clone());
Streaming::new(
Arc::new(Persistent {
+430 -42
View File
@@ -69,8 +69,15 @@ pub mod lightning;
mod lod;
mod occlusion;
mod pack_atlas;
pub mod pack_draw;
mod pack_entities;
mod pack_hand;
mod pack_post;
mod pack_shadow;
mod pack_sky;
mod pack_timing;
pub mod pack_uniforms;
mod pack_weather;
mod page_runs;
pub mod panorama;
mod resident;
@@ -86,6 +93,19 @@ const COMPLETE_COLUMN: u32 = (1 << (crate::chunk::HEIGHT / 16)) - 1;
/// its fixed-point UVs (`terrain.wgsl`'s `terrain_uv`).
/// The world pass again after occlusion culling's test: it loads both
/// targets (phase 2 draws over phase 1, reading the test's results).
/// LevelRenderer: no sky inside lava or powder snow (sky fog ends within 2 blocks).
fn sky_open(frame: &Frame) -> bool {
match frame.fog {
Some(f) => f.sky_end > 2.0,
None => !frame.underwater,
}
}
/// The Overworld-style sky (disc, sunset, stars, void) draws this frame.
fn sky_visible(frame: &Frame) -> bool {
!frame.title && frame.skybox && sky_open(frame)
}
fn world_pass_after_culling<'e>(
encoder: &'e mut wgpu::CommandEncoder,
view: &wgpu::TextureView,
@@ -232,6 +252,8 @@ pub struct Frame<'a> {
pub nether_lighting: bool,
/// Camera-relative entity geometry textured from the shared atlas.
pub entities: &'a [Vertex],
/// Runs of `entities` by entity, while a shader pack draws them (`pack_draw::EntitySpan`).
pub entity_spans: &'a [pack_draw::EntitySpan],
pub weather: &'a [crate::weather::Vertex],
pub weather_time: crate::weather::Time,
/// Horizon Extras' dynamic lights, nearest first (`dynamic_light::select`); empty when off.
@@ -449,6 +471,8 @@ pub struct Renderer {
/// Per-frame scratch, kept to avoid allocating every frame.
visible: Vec<resident::Visible>,
visible_lods: Vec<lod::Visible>,
/// The sections a shader pack's shadow camera sees (`pack_post::ShadowView`).
shadow_visible: Vec<resident::Visible>,
translucent_draws: Vec<(u64, usize, [u32; 5], (u32, Option<u32>))>,
origins: Vec<[f32; 4]>,
/// `DrawIndexedIndirectArgs` words: index count, instance count, first
@@ -463,6 +487,8 @@ pub struct Renderer {
/// Each section's solid draws, to group them by arena page.
solid_runs: page_runs::PageRuns,
phase2_runs: page_runs::PageRuns,
/// The shadow camera's draws by page, in bands of distance from the light.
shadow_runs: page_runs::PageRuns,
/// Block-entity blocks per meshed section (world position, state id).
block_entities: HashMap<SectionKey, Vec<([i32; 3], u32)>>,
/// Changes whenever `block_entities` does (unique across renderers).
@@ -473,8 +499,16 @@ pub struct Renderer {
world_post: world_post::Post,
/// The shader pack's post-processing, and the dimension (legacy id) it was loaded for.
pack_post: Option<(i32, Option<pack_post::PackPost>)>,
/// The entity vertices Horizon draws itself while a shader pack draws the others.
pack_entity_rest: Vec<Vertex>,
/// Set 1 of the cloud pipeline, for a shader pack's `gbuffers_clouds`.
cloud_bindings: wgpu::BindGroupLayout,
/// Rain and snow handed to a shader pack's `gbuffers_weather`, once a pack draws them.
pack_weather: Option<pack_weather::PackWeather>,
/// The block sprites as one atlas, while a shader pack is loaded.
pack_atlas: Option<pack_atlas::PackAtlas>,
/// The pack's terrain program reads flat draws' vertices (`pack_terrain_inputs`).
pack_terrain_flat: bool,
pub hud: HudRenderer,
/// Vanilla's menu background blur, made on first use.
menu_blur: Option<crate::menu_blur::MenuBlur>,
@@ -842,12 +876,17 @@ async fn open_device(
continue;
}
// Mappable primary buffers: section pages that no longer fit in
// video memory go to system memory.
// video memory go to system memory. Shader packs' 16-bit normalized buffers (render
// targets with the adapter's own format features) and R11F_G11F_B10F: a float
// stand-in neither clamps nor drops NaN.
let features = timing::GpuTimer::features(&adapter)
| cull::Cull::features(&adapter)
| (adapter.features()
& (wgpu::Features::INDIRECT_FIRST_INSTANCE
| wgpu::Features::MAPPABLE_PRIMARY_BUFFERS));
| wgpu::Features::MAPPABLE_PRIMARY_BUFFERS
| wgpu::Features::TEXTURE_FORMAT_16BIT_NORM
| wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES
| wgpu::Features::RG11B10UFLOAT_RENDERABLE));
let driver = format!("{} {}", info.driver, info.driver_info);
startup_step(format_args!(
"requesting the device (driver {}) with features [{features}] and the adapter's \
@@ -1484,7 +1523,10 @@ impl Renderer {
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8Unorm,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
// COPY_SRC: a shader pack's weather copies the rain and snow sprites out.
usage: wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::COPY_DST
| wgpu::TextureUsages::COPY_SRC,
view_formats: &[],
},
wgpu::util::TextureDataOrder::LayerMajor,
@@ -2149,13 +2191,16 @@ impl Renderer {
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let flat_wanted = flat::wanted(&adapter, &device);
// Readable as storage where flat draws run: a shader pack's terrain program reads them.
let arena = arena::Arena::new(
&device,
std::mem::size_of::<TerrainVertex>() as u64,
1 << 20,
flat_wanted,
);
// Sections as one index list per frame where multi-draw is emulated.
let flat = flat::wanted(&adapter, &device).then(|| {
let flat = flat_wanted.then(|| {
let buffers = [Some(wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<TerrainVertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
@@ -2349,6 +2394,7 @@ impl Renderer {
multi_draw,
visible: Vec::new(),
visible_lods: Vec::new(),
shadow_visible: Vec::new(),
translucent_draws: Vec::new(),
origins: Vec::new(),
draws: Vec::new(),
@@ -2357,12 +2403,17 @@ impl Renderer {
phase2_pages: Vec::new(),
solid_runs: page_runs::PageRuns::default(),
phase2_runs: page_runs::PageRuns::default(),
shadow_runs: page_runs::PageRuns::default(),
block_entities: HashMap::new(),
block_entity_revision: next_block_entity_revision(),
occlusion: occlusion::Occlusion::new(crate::chunk::MIN_Y, crate::chunk::HEIGHT / 16),
world_post,
pack_post: None,
pack_entity_rest: Vec::new(),
cloud_bindings,
pack_weather: None,
pack_atlas: None,
pack_terrain_flat: false,
hud,
menu_blur: None,
info,
@@ -2548,6 +2599,18 @@ impl Renderer {
pub fn section_count(&self) -> usize {
self.sections.len()
}
/// What a shader pack's shadow pass draws of the entities this frame: (entities or block
/// entities other than the player, so those Horizon's culling hides from the camera are
/// kept for it; the local player, so its model is built in first person too). (false,
/// false) without a pack or its shadow pass.
pub fn pack_shadow_casters(&self) -> (bool, bool) {
let Some((_, Some(post))) = &self.pack_post else {
return (false, false);
};
let c = post.shadow_casters();
((c.entities && !c.only_player) || c.blocks, c.entities)
}
pub fn has_section(&self, key: SectionKey) -> bool {
self.sections.contains(key)
}
@@ -2793,24 +2856,84 @@ impl Renderer {
self.world_post.scale = scale.clamp(0.25, 2.0);
self.world_post.antialias = antialias;
}
/// Runs `post` over the world from now on (`None`: no pack), for the dimension `dimension`.
/// Runs `post` over the world from now on (`None`: no pack), for the dimension `dimension`;
/// `block_ids` gives each block sprite layer's `mc_Entity` (id, fluid) pair.
/// Returns the programs that run and the notes of loading them.
pub fn set_shader_pack(
&mut self,
dimension: i32,
pack: Option<horizon_shaders::Post>,
block_ids: &[i32],
) -> Option<(Vec<String>, Vec<String>)> {
if pack.is_none() {
self.pack_atlas = None;
}
let instance = wgpu::vertex_attr_array![5=>Float32x4];
let vertex = wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<TerrainVertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &TERRAIN_ATTRIBUTES,
};
let instances = wgpu::VertexBufferLayout {
array_stride: 16,
step_mode: wgpu::VertexStepMode::Instance,
attributes: &instance,
};
let buffers = [Some(vertex), Some(instances)];
let flat = self.pack_terrain_flat;
let post = pack.map(|post| {
let world = pack_post::World {
atlas: self.pack_atlas.as_ref().map(|a| (a.view.clone(), a.size())),
lightmap: self.lightmap_view.clone(),
terrain: self.pack_atlas.as_ref().map(|_| pack_post::Geometry {
buffers: if flat { &buffers[..1] } else { &buffers },
table: self.flat.as_ref().filter(|_| flat).map(|f| &f.draw_layout),
depth_write: true,
atlas: None,
ids: None,
cull: true,
}),
block_ids,
celestial: self
.hud
.atlas_texture
.as_ref()
.map(|t| t.create_view(&Default::default())),
clouds: Some(&self.cloud_bindings),
weather: true,
hand: true,
};
pack_post::PackPost::new(
&self.device,
&self.queue,
post,
self.config.format.remove_srgb_suffix(),
world,
)
});
if post.is_none() {
self.pack_atlas = None;
} else if self.pack_atlas.is_none() {
let report = post.as_ref().map(|p| {
let programs = p.programs().into_iter().map(str::to_owned).collect();
(programs, p.notes.clone())
});
self.pack_post = Some((dimension, post));
report
}
/// What a shader pack's gbuffers programs read from Horizon's geometry: the sky's, and
/// terrain's in the pack's block atlas (made here when missing; none when it does not fit).
pub fn pack_inputs(&mut self) -> horizon_shaders::GameInputs {
horizon_shaders::GameInputs {
terrain: self.pack_terrain_inputs(),
sky: Some(pack_sky::inputs()),
celestial: Some(pack_sky::celestial_inputs()),
entities: Some(pack_entities::inputs()),
clouds: Some(pack_entities::cloud_inputs()),
weather: Some(pack_weather::inputs()),
hand: Some(pack_hand::inputs()),
}
}
fn pack_terrain_inputs(&mut self) -> Option<horizon_shaders::GbuffersInputs> {
if self.pack_atlas.is_none() {
let mut encoder = self.device.create_command_encoder(&Default::default());
let layers = self
.sprite_textures
@@ -2827,12 +2950,13 @@ impl Renderer {
);
self.queue.submit([encoder.finish()]);
}
let report = post.as_ref().map(|p| {
let programs = p.programs().into_iter().map(str::to_owned).collect();
(programs, p.notes.clone())
});
self.pack_post = Some((dimension, post));
report
// Flat draws whenever the device has them (`render` keeps them on with a pack).
self.pack_terrain_flat = self.flat.is_some() && self.flat_on;
Some(
self.pack_atlas
.as_ref()?
.terrain_inputs(self.pack_terrain_flat),
)
}
/// The dimension the shader pack was last set for.
pub fn shader_pack_dimension(&self) -> Option<i32> {
@@ -3208,8 +3332,43 @@ impl Renderer {
);
visible_lods.sort_unstable_by_key(|v| (v.distance, v.key));
}
// A shader pack's shadow pass draws the sections in its camera's box, all of them (the
// occlusion graph follows the eye, not the light), within shadowDistanceRenderMul.
let shadow_view = self
.pack_post
.as_ref()
.and_then(|p| p.1.as_ref())
.filter(|_| !frame.title)
.and_then(|p| p.shadow_view(frame));
let mut shadow_visible = std::mem::take(&mut self.shadow_visible);
shadow_visible.clear();
if let Some(v) = &shadow_view {
let radius = (frame.detail_distance / 16.0) as i32;
let radius = v
.distance
.map_or(radius, |d| radius.min((d / 16.0).ceil() as i32));
let light = Frustum {
planes: frustum(v.cull),
};
crate::spans::time("shadow visibility", || {
self.sections.visible(
camera,
radius,
&light,
|_| true,
|_, _| false,
&mut shadow_visible,
)
});
// Nearest the light first, so the depth test rejects what lies under it.
let light = v.light;
shadow_visible.sort_unstable_by(|a, b| {
let height = |s: &resident::Visible| glam::Vec3::from(s.origin).dot(light);
height(b).total_cmp(&height(a))
});
}
self.drawn_sections = visible.len() + visible_lods.len();
let instances = self.drawn_sections;
let instances = self.drawn_sections + shadow_visible.len();
if instances as u64 > self.instance_capacity {
self.instance_capacity = (instances as u64).next_power_of_two();
self.instances = self.device.create_buffer(&wgpu::BufferDescriptor {
@@ -3226,7 +3385,8 @@ impl Renderer {
visible
.iter()
.map(|v| section_instance(v.origin))
.chain(visible_lods.iter().map(|v| lod::instance(v.origin))),
.chain(visible_lods.iter().map(|v| lod::instance(v.origin)))
.chain(shadow_visible.iter().map(|v| section_instance(v.origin))),
);
if !self.origins.is_empty() {
self.uploads.buffer(
@@ -3243,11 +3403,23 @@ impl Renderer {
// Occlusion culling leaves the sections its last result did not see
// to phase 2, after the others, with instance counts of 0. Whether
// the frame has draws enough goes by the last one's.
// A shader pack drawing terrain draws all of it: occlusion culling reads the depth of
// Horizon's own terrain draw.
let pack_terrain = !frame.title
&& self
.pack_post
.as_ref()
.and_then(|p| p.1.as_ref())
.is_some_and(|p| p.has_terrain());
let mut cull = self
.cull
.as_mut()
.filter(|c| {
self.cull_on && !frame.title && self.multi_draw && visible.len() >= c.min_sections
self.cull_on
&& !frame.title
&& !pack_terrain
&& self.multi_draw
&& visible.len() >= c.min_sections
})
.and_then(|cull| cull.begin(&self.device).then_some(cull));
self.phase2_draws.clear();
@@ -3418,9 +3590,87 @@ impl Renderer {
self.translucency_cursor += budget;
let translucent_draws = self.draws.len() as u32 - solid_draws;
self.section_draws = self.draws.len();
// The shadow camera's sections after the camera's: quads that can face the light, then
// translucent ones (in their last sorted order; depth decides there).
let shadow_first = self.draws.len() as u32;
let mut shadow_translucent = Vec::new();
self.shadow_runs.clear();
if let Some(view) = &shadow_view {
let base = self.drawn_sections;
let toward = view.light * 1.0e5;
// How far below the nearest section to the light (squared, as bits, as `PageRuns`
// takes distances): each band's draws go by page, a call per page and band.
let height = |v: &resident::Visible| glam::Vec3::from(v.origin).dot(view.light);
let top = shadow_visible.first().map_or(0.0, height);
for (k, v) in shadow_visible.iter().enumerate() {
let s = self.sections.get_mut(v.slot);
let instance = (base + k) as u32;
let depth = f64::from(top - height(v)).max(0.0);
let distance = (depth * depth).to_bits();
// A section the camera never saw has no sorted indices yet: sorted once now.
if let Some((index_page, index_start)) = s.sorted_indices
&& !s.sort.is_sorted()
{
let origin = [
v.key.0 * 16,
crate::chunk::MIN_Y + v.key.1 * 16,
v.key.2 * 16,
];
if let Some(indices) =
s.sort
.update(camera.to_array(), origin, &mut self.translucent_sorter)
{
self.uploads.buffer(
&mut encoder,
self.sorted_indices.buffer(index_page),
index_start * 4,
bytemuck::cast_slice(indices),
);
}
}
let before = self.draws.len();
facing_runs(&s.sides, Some(toward), v.origin, |first, quads| {
let start = s.start as u32 + first * 4;
self.draws.push([quads * 6, 1, 0, start, instance]);
self.draw_pages.push((s.page, None));
});
let len = self.draws.len() - before;
self.shadow_runs.push(distance, s.page, before, len, [0; 3]);
if let Some((index_page, index_start)) = s.sorted_indices {
let start = s.start + u64::from(s.solid) * 4;
shadow_translucent.push((
distance,
[
s.translucent * 6,
1,
index_start as u32,
start as u32,
instance,
],
(s.page, Some(index_page)),
));
}
}
}
let shadow_solid = self.draws.len() as u32 - shadow_first;
self.shadow_runs
.regroup(&mut self.draws, &mut self.draw_pages, shadow_first as usize);
self.shadow_runs.clear();
for (distance, draw, pages) in shadow_translucent {
self.shadow_runs
.push(distance, pages.0, self.draws.len(), 1, [0; 3]);
self.draws.push(draw);
self.draw_pages.push(pages);
}
self.shadow_runs.regroup(
&mut self.draws,
&mut self.draw_pages,
(shadow_first + shadow_solid) as usize,
);
let shadow_translucent = self.draws.len() as u32 - shadow_first - shadow_solid;
// Metal replays a multi-draw as one draw call per entry.
self.section_draw_calls = if self.multi_draw && self.info.backend != wgpu::Backend::Metal {
(0..self.draws.len())
(0..self.section_draws)
.filter(|&i| {
i == 0
|| i == phase1_draws as usize
@@ -3429,7 +3679,7 @@ impl Renderer {
})
.count()
} else {
self.draws.len()
self.section_draws
};
let debug_depth = frame.debug_lines.len() as u32;
let debug_top = frame.debug_lines_on_top.len() as u32;
@@ -3472,6 +3722,22 @@ impl Renderer {
self.uploads
.buffer(&mut encoder, &self.outline, 0, bytemuck::cast_slice(&lines));
}
// While a shader pack draws entities, the runs of `entity_spans` its programs draw are
// left out of Horizon's own entity draw.
let mut entities = frame.entities;
if !frame.title
&& let Some((_, Some(post))) = &mut self.pack_post
&& post.has_entities()
{
post.prepare_entities(
&self.device,
&self.queue,
frame.entities,
frame.entity_spans,
&mut self.pack_entity_rest,
);
entities = &self.pack_entity_rest;
}
let [
entity_quads,
crumbling_quads,
@@ -3479,12 +3745,9 @@ impl Renderer {
water_mask_quads,
shadow_quads,
translucent_particle_quads,
] = self.entity_batch.update(
frame.entities,
frame.additive,
frame.hand,
frame.see_through,
);
] = self
.entity_batch
.update(entities, frame.additive, frame.hand, frame.see_through);
let weather_quads = frame.weather.len() as u32 / 4;
let world_quads = entity_quads
+ crumbling_quads
@@ -3653,7 +3916,9 @@ impl Renderer {
Some(flat)
if self.flat_on
&& !self.draws.is_empty()
&& self.drawn_sections >= flat.min_sections =>
&& (self.drawn_sections >= flat.min_sections
|| pack_terrain
|| shadow_view.is_some()) =>
{
let sections = &self.sections;
let lods = &self.lods;
@@ -3668,9 +3933,11 @@ impl Renderer {
&self.draw_pages,
phase1_draws as usize,
solid_draws as usize,
[shadow_first, shadow_first + shadow_solid].map(|d| d as usize),
phase2,
visible
.iter()
.chain(&shadow_visible)
.filter_map(|v| {
let id = sections.get(v.slot).flat_id?;
Some((id, section_instance(v.origin)))
@@ -3686,7 +3953,7 @@ impl Renderer {
_ => false,
};
if flat_frame {
self.section_draw_calls = self.flat.as_ref().map_or(0, |f| f.calls.len());
self.section_draw_calls = self.flat.as_ref().map_or(0, |f| f.shadow_calls[0]);
} else if self.multi_draw && !self.draws.is_empty() {
if self.draws.len() as u64 > self.indirect_capacity {
self.indirect_capacity = (self.draws.len() as u64).next_power_of_two();
@@ -3713,6 +3980,46 @@ impl Renderer {
}
// The world's targets: the screen, or the scaled target of `post`.
let pack_post = !frame.title && self.pack_post.as_ref().is_some_and(|p| p.1.is_some());
// The pack's program reads the vertices this frame's draws bind.
let pack_draws_terrain = pack_terrain && flat_frame == self.pack_terrain_flat;
// The shadow camera's lists, drawn by the pack's shadow programs.
let pack_draws_shadow = shadow_view.is_some() && flat_frame == self.pack_terrain_flat;
let pack_draws_sky = pack_post
&& self
.pack_post
.as_ref()
.and_then(|p| p.1.as_ref())
.is_some_and(|p| p.has_sky());
let pack_draws_celestial = pack_post
&& self
.pack_post
.as_ref()
.and_then(|p| p.1.as_ref())
.is_some_and(|p| p.has_celestial());
let pack_draws_water = pack_draws_terrain
&& self
.pack_post
.as_ref()
.and_then(|p| p.1.as_ref())
.is_some_and(|p| p.has_water());
let pack_draws_clouds = pack_post
&& self
.pack_post
.as_ref()
.and_then(|p| p.1.as_ref())
.is_some_and(|p| p.has_clouds());
let pack_draws_hand = pack_post
&& self
.pack_post
.as_ref()
.and_then(|p| p.1.as_ref())
.is_some_and(|p| p.has_hand());
let pack_draws_weather = pack_post
&& self
.pack_post
.as_ref()
.and_then(|p| p.1.as_ref())
.is_some_and(|p| p.has_weather());
let scaled = self.world_post.prepare(
&self.device,
[self.config.width, self.config.height],
@@ -3766,13 +4073,9 @@ impl Renderer {
..Default::default()
});
pass.set_bind_group(0, &self.group, &[]);
// LevelRenderer: no sky inside lava or powder snow (sky fog ends within 2 blocks).
let sky_visible = match frame.fog {
Some(f) => f.sky_end > 2.0,
None => !frame.underwater,
};
let sky_visible = sky_open(frame);
if frame.title || (sky_visible && (frame.skybox || frame.end_sky)) {
if !frame.title && frame.skybox {
if !frame.title && frame.skybox && !pack_draws_sky {
pass.set_pipeline(&self.sky);
pass.draw(0..3, 0..1);
}
@@ -3786,22 +4089,29 @@ impl Renderer {
if frame.end_sky {
self.end_sky.draw(&mut pass);
}
if celestial_vertices > 0 {
if celestial_vertices > 0 && !pack_draws_celestial {
pass.set_vertex_buffer(0, self.celestial_buffer.slice(..));
pass.draw_indexed(0..(celestial_vertices as u32 / 4) * 6, 0, 0..1);
}
pass.set_bind_group(0, &self.group, &[]);
}
if !frame.title && frame.stars.is_some() {
if !frame.title && frame.stars.is_some() && !pack_draws_sky {
self.stars.draw(&mut pass);
pass.set_bind_group(0, &self.group, &[]);
}
if !frame.title && frame.dark_disc {
if !frame.title && frame.dark_disc && !pack_draws_sky {
pass.set_pipeline(&self.dark_disc);
pass.draw(0..3, 0..1);
}
}
match self.flat.as_ref().filter(|_| flat_frame) {
match self
.flat
.as_ref()
.filter(|_| flat_frame && !pack_draws_terrain)
{
_ if pack_draws_terrain => {
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
}
Some(flat) => {
pass.set_pipeline(&flat.solid);
let vertices = |p| self.arena.buffer(p);
@@ -3875,7 +4185,9 @@ impl Renderer {
pass.draw_indexed(0..entity_quads * 6, 0, 0..1);
pass.set_bind_group(0, &self.group, &[]);
}
self.weather.draw(&mut pass, &self.entity_group);
if !pack_draws_weather {
self.weather.draw(&mut pass, &self.entity_group);
}
pass.set_bind_group(0, &self.group, &[]);
if crumbling_quads > 0 {
pass.set_pipeline(&self.crumbling_pipeline);
@@ -3937,9 +4249,11 @@ impl Renderer {
pass.draw(0..4, debug_depth..debug_depth + debug_top);
}
match self.flat.as_ref().filter(|_| flat_frame) {
// The pack draws translucent terrain after its deferred programs.
_ if pack_draws_water => {}
Some(flat) => {
pass.set_pipeline(&flat.translucent);
flat.draw(&mut pass, &flat.calls[flat.solid_calls..], |p| {
flat.draw(&mut pass, flat.translucent_calls(), |p| {
self.arena.buffer(p)
});
pass.set_bind_group(0, &self.group, &[]);
@@ -4004,7 +4318,7 @@ impl Renderer {
0..1,
);
}
if cloud_quads > 0 {
if cloud_quads > 0 && !pack_draws_clouds {
pass.set_pipeline(&self.cloud_pipeline);
pass.set_bind_group(0, &self.entity_group, &[]);
pass.set_bind_group(1, &self.cloud_group, &[]);
@@ -4025,9 +4339,79 @@ impl Renderer {
}
if let Some((color, depth)) = &scaled
&& pack_post
&& let Some((_, Some(post))) = &mut self.pack_post
&& let Some((dimension, Some(mut post))) = self.pack_post.take()
{
let size = self.world_post.size().unwrap_or([1, 1]);
let weather = match &self.hud.atlas_texture {
Some(atlas) if pack_draws_weather => self
.pack_weather
.get_or_insert_with(|| pack_weather::PackWeather::new(&self.device))
.prepare(
&self.device,
&mut encoder,
&mut self.uploads,
atlas,
frame.weather,
frame.weather_time,
&self.indices,
),
_ => None,
};
post.set_weather(weather);
let flat = self.flat.as_ref().filter(|_| flat_frame);
let pages: Vec<(u32, wgpu::Buffer)> = match flat {
Some(_) if pack_draws_terrain || pack_draws_shadow => self
.arena
.page_buffers()
.map(|(p, b)| (p, b.clone()))
.collect(),
_ => Vec::new(),
};
let mut terrain =
|rp: &mut wgpu::RenderPass<'_>,
list: pack_post::TerrainList,
page: &dyn Fn(&mut wgpu::RenderPass<'_>, u32)| {
use pack_post::TerrainList as L;
let shadow = matches!(list, L::ShadowSolid | L::ShadowTranslucent);
if !(if shadow {
pack_draws_shadow
} else {
pack_draws_terrain
}) {
return;
}
match flat {
Some(flat) => {
let [shadow, shadow_translucent] = flat.shadow_calls;
let calls = match list {
L::Solid => &flat.calls[..flat.solid_calls],
L::Translucent => flat.translucent_calls(),
L::ShadowSolid => &flat.calls[shadow..shadow_translucent],
L::ShadowTranslucent => &flat.calls[shadow_translucent..],
};
flat.draw_with(rp, calls, |p| self.arena.buffer(p), page);
}
None => {
rp.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
rp.set_vertex_buffer(1, self.instances.slice(..));
let (first, count) = match list {
L::Solid => (0, solid_draws),
L::Translucent => (solid_draws, translucent_draws),
L::ShadowSolid => (shadow_first, shadow_solid),
L::ShadowTranslucent => {
(shadow_first + shadow_solid, shadow_translucent)
}
};
self.draw_sections(rp, &self.indirect, 0, first, count);
}
}
};
post.set_clouds(pack_draws_clouds.then(|| pack_post::Clouds {
vertices: self.cloud_buffer.clone(),
group: self.cloud_group.clone(),
indices: self.indices.clone(),
quads: cloud_quads,
}));
post.run(
&self.device,
&self.queue,
@@ -4039,11 +4423,14 @@ impl Renderer {
&frame.pack,
size,
[self.config.width, self.config.height],
&pages,
&mut terrain,
);
self.pack_post = Some((dimension, Some(post)));
} else if scaled.is_some() {
self.world_post.finish(&mut encoder, &view_target);
}
if hand_quads > 0 {
if hand_quads > 0 && !pack_draws_hand {
let attachments = [Some(wgpu::RenderPassColorAttachment {
view: &view_target,
resolve_target: None,
@@ -4248,6 +4635,7 @@ impl Renderer {
let _ = self.started;
self.visible = visible;
self.visible_lods = visible_lods;
self.shadow_visible = shadow_visible;
Ok(true)
}
}
+16 -4
View File
@@ -65,8 +65,15 @@ pub struct Arena {
}
impl Arena {
pub fn new(device: &wgpu::Device, stride: u64, capacity: u64) -> Self {
Self::with_usage(device, stride, capacity, wgpu::BufferUsages::VERTEX)
/// Vertices; `storage` also lets shaders read them as a storage buffer (a shader pack's
/// terrain program reads the other corners of a vertex's quad for its normal).
pub fn new(device: &wgpu::Device, stride: u64, capacity: u64, storage: bool) -> Self {
let usage = if storage {
wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::STORAGE
} else {
wgpu::BufferUsages::VERTEX
};
Self::with_usage(device, stride, capacity, usage)
}
/// Sorted translucent indices; `storage` when compute passes read them.
pub fn indices(device: &wgpu::Device, capacity: u64, storage: bool) -> Self {
@@ -148,6 +155,11 @@ impl Arena {
pub fn has_page(&self, page: u32) -> bool {
self.pages.get(page as usize).is_some_and(Option::is_some)
}
/// Each live page's buffer, by page.
pub fn page_buffers(&self) -> impl Iterator<Item = (u32, &wgpu::Buffer)> {
let pages = self.pages.iter().enumerate();
pages.filter_map(|(i, p)| Some((i as u32, &p.as_ref()?.buffer)))
}
pub fn buffer(&self, page: u32) -> &wgpu::Buffer {
&self.pages[page as usize]
.as_ref()
@@ -352,7 +364,7 @@ mod tests {
eprintln!("no GPU adapter: skipped");
return;
};
let mut arena = Arena::new(&device, 4, GRAIN);
let mut arena = Arena::new(&device, 4, GRAIN, false);
arena.page_limit = 2 * GRAIN;
assert_eq!(arena.bytes(), 0);
let (first, a) = alloc(&mut arena, &device, &queue, GRAIN).unwrap();
@@ -395,7 +407,7 @@ mod tests {
return;
};
for mut arena in [
Arena::new(&device, 28, GRAIN),
Arena::new(&device, 28, GRAIN, false),
Arena::indices(&device, GRAIN, false),
] {
alloc(&mut arena, &device, &queue, GRAIN).unwrap();
+80 -5
View File
@@ -40,6 +40,8 @@ enum Source {
struct Plan {
phase1_calls: usize,
solid_calls: usize,
/// Where the shadow camera's solid and translucent calls begin.
shadow_calls: [usize; 2],
total: u64,
}
const QUADS: u32 = u32::MAX;
@@ -73,7 +75,8 @@ pub struct Flat {
phase2_call_data: Vec<[u32; 5]>,
/// The copy of phase 2's draws, and its ranges.
phase2_group: Option<(wgpu::BindGroup, u32)>,
draw_layout: wgpu::BindGroupLayout,
/// The layout of a page's bind group 1 (shader packs' terrain pipelines share it).
pub(super) draw_layout: wgpu::BindGroupLayout,
blocks_per_page: u64,
pages: Vec<Option<PageTable>>,
free_ids: Vec<u32>,
@@ -96,6 +99,10 @@ pub struct Flat {
pub calls: Vec<Call>,
pub phase1_calls: usize,
pub solid_calls: usize,
/// Where a shader pack's shadow camera's solid and translucent calls
/// begin (`calls.len()` without them): the camera's translucent calls
/// end at the first.
pub shadow_calls: [usize; 2],
/// Frames with fewer sections in view keep the per-section draws.
pub min_sections: usize,
range_align: u64,
@@ -163,12 +170,16 @@ fn buffer(
/// draw calls, one per run of draws in the same vertex page, phase 1's solid
/// ones, phase 2's (draws `phase1_draws..solid_draws`) and translucent ones
/// apart. A phase 2 range names its draw in phase 2 and its call in phase 2
/// instead of a destination: the call's room holds all of its draws.
/// instead of a destination: the call's room holds all of its draws. Draws
/// from `shadow[0]` (solid) and `shadow[1]` (translucent) on are a shader
/// pack's shadow camera's, with calls of their own.
#[allow(clippy::too_many_arguments)]
fn plan(
draws: &[[u32; 5]],
pages: &[(u32, Option<u32>)],
phase1_draws: usize,
solid_draws: usize,
shadow: [usize; 2],
align: u64,
calls: &mut Vec<Call>,
ranges: &mut Vec<Range>,
@@ -178,6 +189,7 @@ fn plan(
ranges.clear();
segments.clear();
let (mut phase1_calls, mut solid_calls) = (None, None);
let mut shadow_calls = [None; 2];
let mut total = 0u64;
let mut by_source: Vec<(Source, Vec<Range>)> = Vec::new();
for (i, (d, &(page, sorted))) in draws.iter().zip(pages).enumerate() {
@@ -187,10 +199,14 @@ fn plan(
if i == solid_draws {
solid_calls = Some(calls.len());
}
match calls.last_mut() {
Some(last) if last.page == page && i != phase1_draws && i != solid_draws => {
last.count += d[0]
for (k, &from) in shadow.iter().enumerate() {
if i == from {
shadow_calls[k].get_or_insert(calls.len());
}
}
let starts = i == phase1_draws || i == solid_draws || shadow.contains(&i);
match calls.last_mut() {
Some(last) if last.page == page && !starts => last.count += d[0],
_ => calls.push(Call {
page,
first: total as u32,
@@ -226,6 +242,7 @@ fn plan(
Plan {
phase1_calls: phase1_calls.unwrap_or(calls.len()).min(solid_calls),
solid_calls,
shadow_calls: shadow_calls.map(|c| c.unwrap_or(calls.len())),
total,
}
}
@@ -339,6 +356,7 @@ impl Flat {
calls: Vec::new(),
phase1_calls: 0,
solid_calls: 0,
shadow_calls: [0; 2],
min_sections: if std::env::var("HORIZON_FLAT_DRAWS").as_deref() == Ok("1") {
0
} else {
@@ -411,6 +429,7 @@ impl Flat {
///
/// When culling, the solid draws from `phase1_draws` are phase 2's, which
/// `phase2` holds as the test leaves them: `flatten_phase2` copies them.
/// Draws from `shadow` on are a shader pack's shadow camera's (`plan`).
#[allow(clippy::too_many_arguments)]
pub fn prepare(
&mut self,
@@ -421,6 +440,7 @@ impl Flat {
pages: &[(u32, Option<u32>)],
phase1_draws: usize,
solid_draws: usize,
shadow: [usize; 2],
phase2: Option<&wgpu::Buffer>,
origins: impl Iterator<Item = (u32, [f32; 4])>,
sorted_page: impl Fn(u32) -> wgpu::Buffer,
@@ -463,12 +483,14 @@ impl Flat {
let Plan {
phase1_calls,
solid_calls,
shadow_calls,
total,
} = plan(
draws,
pages,
phase1_draws,
solid_draws,
shadow,
self.range_align,
&mut self.calls,
&mut self.range_data,
@@ -476,10 +498,12 @@ impl Flat {
);
self.phase1_calls = phase1_calls;
self.solid_calls = solid_calls;
self.shadow_calls = shadow_calls;
if total == 0 || total * 4 > device.limits().max_storage_buffer_binding_size {
self.calls.clear();
self.phase1_calls = 0;
self.solid_calls = 0;
self.shadow_calls = [0; 2];
return false;
}
if self.range_data.len() as u64 > self.range_capacity {
@@ -643,6 +667,11 @@ impl Flat {
pass.dispatch_workgroups((*count).min(GROUP_LIMIT), count.div_ceil(GROUP_LIMIT), 1);
}
/// The camera's translucent calls.
pub fn translucent_calls(&self) -> &[Call] {
&self.calls[self.solid_calls..self.shadow_calls[0]]
}
/// Draws `calls` with a flat pipeline set and `vertices` giving each
/// page's vertex buffer.
pub fn draw<'a>(
@@ -650,10 +679,22 @@ impl Flat {
pass: &mut wgpu::RenderPass<'_>,
calls: &[Call],
vertices: impl Fn(u32) -> &'a wgpu::Buffer,
) {
self.draw_with(pass, calls, vertices, |_, _| {});
}
/// `draw`, calling `page` before each run of calls from one arena page (a shader pack binds
/// that page's vertices for reading).
pub fn draw_with<'a>(
&self,
pass: &mut wgpu::RenderPass<'_>,
calls: &[Call],
vertices: impl Fn(u32) -> &'a wgpu::Buffer,
page: impl Fn(&mut wgpu::RenderPass<'_>, u32),
) {
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
for call in calls {
self.bind_page(pass, call.page, &vertices);
page(pass, call.page);
pass.draw_indexed(call.first..call.first + call.count, 0, 0..1);
}
}
@@ -722,11 +763,13 @@ mod tests {
phase1_calls,
solid_calls,
total,
..
} = plan(
&draws,
&pages,
solid,
solid,
[draws.len(); 2],
4,
&mut calls,
&mut ranges,
@@ -761,6 +804,35 @@ mod tests {
assert_eq!(ranges.len(), 12);
}
/// A shader pack's shadow draws follow the camera's, in calls of their
/// own even from the same page: solid ones, then translucent ones.
#[test]
fn plan_gives_the_shadow_lists_calls_of_their_own() {
let (mut draws, mut pages, solid) = sample();
let camera = draws.len();
draws.extend([[6, 1, 0, 40, 6], [6 * 2, 1, 0, 64, 7], [6, 1, 6, 80, 7]]);
pages.extend([(0, None), (0, None), (0, Some(0))]);
let (mut calls, mut ranges, mut segments) = (Vec::new(), Vec::new(), Vec::new());
let plan = plan(
&draws,
&pages,
solid,
solid,
[camera, camera + 2],
4,
&mut calls,
&mut ranges,
&mut segments,
);
assert_eq!((plan.solid_calls, plan.shadow_calls), (3, [4, 5]));
let call = |page, first, count| Call { page, first, count };
assert_eq!(
&calls[3..],
[call(0, 60, 18), call(0, 78, 18), call(0, 96, 6)]
);
assert_eq!(plan.total, 6 * 17);
}
/// Phase 2's draws (here the third and fourth) get calls of their own,
/// each with room for all of its draws, and ranges naming their draw
/// and call in phase 2.
@@ -773,6 +845,7 @@ mod tests {
&pages,
2,
solid,
[draws.len(); 2],
4,
&mut calls,
&mut ranges,
@@ -783,6 +856,7 @@ mod tests {
Plan {
phase1_calls: 1,
solid_calls: 3,
shadow_calls: [4, 4],
total: 6 * 13
}
);
@@ -886,6 +960,7 @@ mod tests {
&pages,
phase1,
solid,
[draws.len(); 2],
align,
&mut calls,
&mut ranges,
+106 -5
View File
@@ -3,7 +3,6 @@
//! layer is copied into a grid, row by row from the top left, and an animated layer again
//! whenever its frame changes.
#[expect(dead_code, reason = "the gbuffers pass binds it (next slice)")]
pub(super) struct PackAtlas {
texture: wgpu::Texture,
pub view: wgpu::TextureView,
@@ -64,10 +63,6 @@ impl PackAtlas {
}
/// The atlas's size in pixels (`atlasSize`).
#[cfg_attr(
not(test),
expect(dead_code, reason = "the gbuffers pass reads it (next slice)")
)]
pub fn size(&self) -> [u32; 2] {
[self.texture.width(), self.texture.height()]
}
@@ -118,6 +113,112 @@ impl PackAtlas {
}
}
/// GLSL: the face normal and tangent of a flat-drawn vertex, from its quad's first three corners
/// (quads are four consecutive vertices of the page; position words 0-2, UV word 3).
const FACE: &str = " uint hz_q = (uint(gl_VertexIndex) & ~3u) * 6u;\n \
vec3 hz_p0 = uintBitsToFloat(uvec3(hz_vertices[hz_q], hz_vertices[hz_q + 1u], hz_vertices[hz_q + 2u]));\n \
vec3 hz_p1 = uintBitsToFloat(uvec3(hz_vertices[hz_q + 6u], hz_vertices[hz_q + 7u], hz_vertices[hz_q + 8u]));\n \
vec3 hz_p2 = uintBitsToFloat(uvec3(hz_vertices[hz_q + 12u], hz_vertices[hz_q + 13u], hz_vertices[hz_q + 14u]));\n \
uvec3 hz_w = uvec3(hz_vertices[hz_q + 3u], hz_vertices[hz_q + 9u], hz_vertices[hz_q + 15u]);\n \
vec2 hz_t0 = vec2(hz_w.x & 65535u, hz_w.x >> 16u);\n \
vec2 hz_d1 = vec2(hz_w.y & 65535u, hz_w.y >> 16u) - hz_t0;\n \
vec2 hz_d2 = vec2(hz_w.z & 65535u, hz_w.z >> 16u) - hz_t0;\n \
vec3 hz_e1 = hz_p1 - hz_p0;\n \
vec3 hz_e2 = hz_p2 - hz_p0;\n \
vec3 hz_n = cross(hz_e1, hz_e2);\n \
if (dot(hz_n, hz_n) > 1e-12) {\n \
hz_Normal = normalize(hz_n);\n \
float hz_r = hz_d1.x * hz_d2.y - hz_d2.x * hz_d1.y;\n \
vec3 hz_t = (hz_e1 * hz_d2.y - hz_e2 * hz_d1.y) * sign(hz_r);\n \
vec3 hz_b = (hz_e2 * hz_d1.x - hz_e1 * hz_d2.x) * sign(hz_r);\n \
if (dot(hz_t, hz_t) > 1e-12) {\n \
hz_t = normalize(hz_t - hz_Normal * dot(hz_Normal, hz_t));\n \
hz_tangent = vec4(hz_t, dot(cross(hz_t, hz_Normal), hz_b) < 0.0 ? -1.0 : 1.0);\n \
}\n \
}\n";
impl PackAtlas {
/// The vertex inputs of a gbuffers program drawing terrain (`TerrainVertex`, terrain.wgsl's
/// `vs_terrain` or, for `flat` draws, `vs_terrain_flat`), in atlas coordinates.
pub fn terrain_inputs(&self, flat: bool) -> horizon_shaders::GbuffersInputs {
let mut declarations = String::from(
"layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in uvec2 hz_v_uv;\n\
layout(location = 2) in uint hz_v_layer;\nlayout(location = 3) in vec4 hz_v_color;\n\
layout(location = 4) in uvec2 hz_v_light;\n",
);
let origin = if flat {
declarations.push_str(
"layout(set = 1, binding = 0) readonly buffer HzBlockSlots { uint hz_block_slots[]; };\n\
layout(set = 1, binding = 1) readonly buffer HzSlotOrigins { vec4 hz_slot_origins[]; };\n",
);
// `mc_Entity` of each sprite layer: id, then 1 for fluids or -1 (the game's table),
// and the arena page's vertices (`TerrainVertex`, six words each).
declarations.push_str(
"layout(set = 3, binding = 0) readonly buffer HzBlockIds { int hz_block_ids[]; };\n\
layout(set = 3, binding = 1) readonly buffer HzVertices { uint hz_vertices[]; };\n",
);
"hz_slot_origins[hz_block_slots[uint(gl_VertexIndex) >> 8u]]"
} else {
declarations.push_str("layout(location = 5) in vec4 hz_v_origin;\n");
"hz_v_origin"
};
let [width, height] = self.size();
let scale = [
self.sprite as f32 / width as f32,
self.sprite as f32 / height as f32,
];
let columns = self.columns;
// UVs past a sprite (models up to 2, distant tiles) cannot repeat in an atlas: clamped.
let body = format!(
" vec4 hz_origin = {origin};\n \
uint hz_layer = hz_v_layer & 32767u;\n \
hz_cell = vec2(float(hz_layer % {columns}u), float(hz_layer / {columns}u));\n \
hz_scale = vec2({:?}, {:?});\n \
hz_Vertex = vec4(hz_v_pos + hz_origin.xyz, 1.0);\n \
hz_MultiTexCoord0 = vec4((hz_cell + clamp(vec2(hz_v_uv) * hz_origin.w, 0.0, 1.0)) * hz_scale, 0.0, 1.0);\n \
hz_MultiTexCoord1 = vec4(float(hz_v_light.y), float(hz_v_light.x), 0.0, 1.0);\n \
hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \
hz_Color = hz_v_color;\n \
hz_Normal = vec3(0.0, 1.0, 0.0);\n \
hz_tangent = vec4(1.0, 0.0, 0.0, 1.0);\n",
scale[0], scale[1],
);
let mut body = body;
if flat {
body.push_str(FACE);
}
declarations.push_str("vec2 hz_cell; vec2 hz_scale; vec4 hz_tangent;\n");
let entity = if flat {
"vec4(float(hz_block_ids[2u * hz_layer]), float(hz_block_ids[2u * hz_layer + 1u]), 0.0, 0.0)"
} else {
"vec4(-1.0, -1.0, 0.0, 0.0)"
};
let attributes = [
("mc_Entity", entity),
(
"mc_midTexCoord",
"vec4((hz_cell + 0.5) * hz_scale, 0.0, 1.0)",
),
("at_tangent", "hz_tangent"),
("vaPosition", "hz_Vertex.xyz"),
("vaUV0", "hz_MultiTexCoord0.xy"),
("vaUV2", "hz_MultiTexCoord1.xy"),
("vaColor", "hz_Color"),
("vaNormal", "hz_Normal"),
];
horizon_shaders::GbuffersInputs {
declarations,
body,
attributes: attributes
.iter()
.map(|&(n, v)| (n.to_owned(), v.to_owned()))
.collect(),
after: String::new(),
alpha_test: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
+152
View File
@@ -0,0 +1,152 @@
//! The uniforms a shader pack's gbuffers programs read per draw (`HzDraw` at set 2, the
//! translator's `DRAW_UNIFORMS`): one slot per render stage of a uniform buffer, picked with a
//! dynamic offset at each draw.
use horizon_shaders::{RENDER_STAGES, render_stage};
/// Where a run of `Frame::entities` begins, and what it is (slice 7): the run lasts until the
/// next span. Recorded only while a shader pack draws entities, so Horizon's own frames carry
/// none; Horizon itself still draws the whole list at once.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct EntitySpan {
/// The first vertex.
pub start: u32,
/// The render stage (5.4: `ENTITIES`, `BLOCK_ENTITIES`, `PARTICLES`, `DESTROY`), which picks
/// the program.
pub stage: u32,
/// `entityId` for entities, `blockEntityId` for block entities (5.3), else 0.
pub id: i32,
/// An entity part that blends (a name tag, a shadow): `gbuffers_entities_translucent`.
pub translucent: bool,
/// Hidden from the camera (Horizon's entity culling, the player in first person): only the
/// shadow pass draws it.
pub hidden: bool,
/// The local player (`shadowPlayer`).
pub player: bool,
/// A block entity whose block emits light (`shadowLightBlockEntities`).
pub emissive: bool,
}
impl EntitySpan {
/// Marks vertices `start..end` of `spans`' list (one entity) as hidden: the span covering
/// `start` is split there, the spans inside are hidden, and the geometry after `end` goes
/// on in the last span's kind. False when no span covers `start`.
pub fn hide(spans: &mut Vec<EntitySpan>, start: usize, end: usize) -> bool {
let Some(i) = spans.iter().rposition(|s| s.start as usize <= start) else {
return false;
};
let after = spans[spans.len() - 1];
if spans[i].start as usize != start {
let split = EntitySpan {
start: start as u32,
..spans[i]
};
spans.insert(i + 1, split);
}
for s in spans.iter_mut().skip(i) {
if s.start as usize >= start {
s.hidden = true;
}
}
if end > start {
spans.push(EntitySpan {
start: end as u32,
..after
});
}
true
}
}
/// `HzDraw`'s size: `renderStage`, `entityId`, `blockEntityId`, a pad, `entityColor`.
const SIZE: u32 = 32;
pub(super) struct DrawUniforms {
pub layout: wgpu::BindGroupLayout,
group: wgpu::BindGroup,
stride: u32,
}
impl DrawUniforms {
pub fn new(device: &wgpu::Device) -> Self {
let stride = SIZE.max(device.limits().min_uniform_buffer_offset_alignment);
let mut contents = vec![0u8; stride as usize * RENDER_STAGES.len()];
for (i, slot) in contents.chunks_mut(stride as usize).enumerate() {
slot[..4].copy_from_slice(&(i as i32).to_le_bytes());
}
let buffer = wgpu::util::DeviceExt::create_buffer_init(
device,
&wgpu::util::BufferInitDescriptor {
label: Some("Shader pack draws"),
contents: &contents,
usage: wgpu::BufferUsages::UNIFORM,
},
);
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Shader pack draws"),
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: true,
min_binding_size: wgpu::BufferSize::new(u64::from(SIZE)),
},
count: None,
}],
});
let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Shader pack draws"),
layout: &layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
buffer: &buffer,
offset: 0,
size: wgpu::BufferSize::new(u64::from(SIZE)),
}),
}],
});
Self {
layout,
group,
stride,
}
}
/// Draws that follow read `renderStage` = `MC_RENDER_STAGE_<stage>`, other values zero.
pub fn bind(&self, pass: &mut wgpu::RenderPass<'_>, stage: &str) {
pass.set_bind_group(2, &self.group, &[self.stride * render_stage(stage)]);
}
}
#[cfg(test)]
mod tests {
use super::EntitySpan;
fn span(start: u32, id: i32) -> EntitySpan {
EntitySpan {
start,
id,
..Default::default()
}
}
#[test]
fn hide_splits_the_span_around_one_entity() {
// Two cows in one span: the second (vertices 8..16) is hidden.
let mut spans = vec![span(0, 7)];
assert!(EntitySpan::hide(&mut spans, 8, 16));
let hidden = EntitySpan {
hidden: true,
..span(8, 7)
};
assert_eq!(spans, [span(0, 7), hidden, span(16, 7)]);
// An entity with its own span and a part after it (armour) is hidden whole.
let mut spans = vec![span(0, 1), span(4, 2), span(6, 3)];
assert!(EntitySpan::hide(&mut spans, 4, 10));
assert!(!spans[0].hidden && spans[1].hidden && spans[2].hidden);
assert_eq!(spans[3], span(10, 3));
assert!(!EntitySpan::hide(&mut Vec::new(), 0, 4));
}
}
@@ -0,0 +1,524 @@
//! Entities, block entities, translucent entity parts and particles drawn by a shader pack's
//! `gbuffers_entities`, `gbuffers_block`, `gbuffers_entities_translucent` and
//! `gbuffers_particles` (spec 2.3, 5.3, 5.4): the runs of `Frame::entities` the game marked (`EntitySpan`), each its
//! own draw with its `entityId` or `blockEntityId` and `entityColor` in a per-draw slot of
//! `HzDraw` (set 2). Horizon draws whatever the pack does not.
use std::ops::Range;
use horizon_shaders::render_stage;
use super::Vertex;
use super::pack_draw::EntitySpan;
/// `HzDraw`'s size (pack_draw.rs).
const SLOT: u64 = 32;
const ATTRIBUTES: [wgpu::VertexAttribute; 6] = wgpu::vertex_attr_array![
0 => Float32x3, 1 => Float32x2, 2 => Uint32, 3 => Unorm8x4, 4 => Uint8x4, 5 => Snorm8x4
];
/// The entity vertex: Horizon's own (`Vertex`).
pub fn buffers() -> [Option<wgpu::VertexBufferLayout<'static>>; 1] {
[Some(wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &ATTRIBUTES,
})]
}
/// What `gbuffers_entities` and `gbuffers_block` read from an entity vertex: camera-relative
/// position, the shared atlas's UV, colour, light (block, sky in 16ths) and the world-oriented
/// normal. The quad's corners, read from the same vertices at set 1, give the face normal where
/// the vertex has none (block entities) and `at_tangent` along the texture's u, as for terrain.
pub fn inputs() -> horizon_shaders::GbuffersInputs {
let declarations = "layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in vec2 hz_v_uv;\n\
layout(location = 2) in uint hz_v_overlay;\nlayout(location = 3) in vec4 hz_v_color;\n\
layout(location = 4) in uvec4 hz_v_light;\nlayout(location = 5) in vec4 hz_v_normal;\n\
layout(set = 1, binding = 0) readonly buffer HzEntityVertices { uint hz_entity_vertices[]; };\n\
vec4 hz_tangent; vec2 hz_mid;\n"
.to_owned();
let words = std::mem::size_of::<Vertex>() / 4;
let corner = |k: usize, field: &str| {
let at = |o: usize| format!("hz_entity_vertices[hz_q + {}u]", k * words + o);
match field {
"pos" => format!("uintBitsToFloat(uvec3({}, {}, {}))", at(0), at(1), at(2)),
_ => format!("uintBitsToFloat(uvec2({}, {}))", at(3), at(4)),
}
};
let body = format!(
" uint hz_q = (uint(gl_VertexIndex) & ~3u) * {words}u;\n \
vec3 hz_e1 = {p1} - {p0};\n \
vec3 hz_e2 = {p2} - {p0};\n \
vec2 hz_t0 = {t0};\n \
vec2 hz_d1 = {t1} - hz_t0;\n \
vec2 hz_d2 = {t2} - hz_t0;\n \
hz_mid = (hz_t0 + {t2}) * 0.5;\n \
vec3 hz_n = cross(hz_e1, hz_e2);\n \
hz_Normal = dot(hz_v_normal.xyz, hz_v_normal.xyz) > 0.01 ? normalize(hz_v_normal.xyz)\n \
: dot(hz_n, hz_n) > 1e-12 ? normalize(hz_n) : vec3(0.0, 1.0, 0.0);\n \
hz_tangent = vec4(1.0, 0.0, 0.0, 1.0);\n \
float hz_r = hz_d1.x * hz_d2.y - hz_d2.x * hz_d1.y;\n \
vec3 hz_t = (hz_e1 * hz_d2.y - hz_e2 * hz_d1.y) * sign(hz_r);\n \
vec3 hz_b = (hz_e2 * hz_d1.x - hz_e1 * hz_d2.x) * sign(hz_r);\n \
hz_t -= hz_Normal * dot(hz_Normal, hz_t);\n \
if (dot(hz_t, hz_t) > 1e-12) {{\n \
hz_t = normalize(hz_t);\n \
hz_tangent = vec4(hz_t, dot(cross(hz_t, hz_Normal), hz_b) < 0.0 ? -1.0 : 1.0);\n \
}}\n \
hz_Vertex = vec4(hz_v_pos, 1.0);\n \
hz_MultiTexCoord0 = vec4(hz_v_uv, 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",
p0 = corner(0, "pos"),
p1 = corner(1, "pos"),
p2 = corner(2, "pos"),
t0 = corner(0, "uv"),
t1 = corner(1, "uv"),
t2 = corner(2, "uv"),
);
let attributes = [
("at_tangent", "hz_tangent"),
("mc_midTexCoord", "vec4(hz_mid, 0.0, 1.0)"),
("vaPosition", "hz_Vertex.xyz"),
("vaUV0", "hz_v_uv"),
("vaUV1", "ivec2(0, 10)"),
("vaUV2", "ivec2(hz_MultiTexCoord1.xy)"),
("vaColor", "hz_Color"),
("vaNormal", "hz_Normal"),
];
horizon_shaders::GbuffersInputs {
declarations,
body,
attributes: attributes
.iter()
.map(|&(n, v)| (n.to_owned(), v.to_owned()))
.collect(),
after: String::new(),
// Cutout, as vanilla's entity types mostly are (OQ-23): the loader's test at 0.1.
alpha_test: Some(horizon_shaders::AlphaTest {
op: ">",
reference: 0.1,
}),
}
}
/// What `gbuffers_clouds` reads from the clouds' mesh (entity vertices, `vs_cloud`): the
/// position moved by the clouds' offset, the colour of the face's shade from their palette,
/// full sky light. Set 1 holds the clouds' uniform (`clouds::Uniform`).
pub fn cloud_inputs() -> horizon_shaders::GbuffersInputs {
let declarations = "layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in vec2 hz_v_uv;\n\
layout(location = 2) in uint hz_v_overlay;\nlayout(location = 3) in vec4 hz_v_color;\n\
layout(location = 4) in uvec4 hz_v_light;\nlayout(location = 5) in vec4 hz_v_normal;\n\
layout(set = 1, binding = 0) uniform HzCloud { vec4 hz_cloud_offset; vec4 hz_cloud_colors[4]; };\n"
.to_owned();
let body = " hz_Vertex = vec4(hz_v_pos + hz_cloud_offset.xyz, 1.0);\n \
hz_MultiTexCoord0 = vec4(hz_v_uv, 0.0, 1.0);\n \
hz_MultiTexCoord1 = vec4(0.0, 240.0, 0.0, 1.0);\n \
hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \
hz_Color = hz_cloud_colors[min(hz_v_light.x, 3u)];\n \
hz_Normal = dot(hz_v_normal.xyz, hz_v_normal.xyz) > 0.01 ? normalize(hz_v_normal.xyz) : vec3(0.0, 1.0, 0.0);\n"
.to_owned();
let attributes = [
("vaPosition", "hz_Vertex.xyz"),
("vaUV0", "hz_v_uv"),
("vaUV2", "ivec2(0, 240)"),
("vaColor", "hz_Color"),
("vaNormal", "hz_Normal"),
];
horizon_shaders::GbuffersInputs {
declarations,
body,
attributes: attributes
.iter()
.map(|&(n, v)| (n.to_owned(), v.to_owned()))
.collect(),
after: String::new(),
alpha_test: None,
}
}
/// `entityColor` for a run whose first quad carries overlay code `overlay`: the hurt red or a
/// white flash, its alpha how much of that colour shows over the entity (vanilla mixes the
/// overlay texel's colour in by one minus the texel's alpha).
pub fn entity_color(overlay: u32) -> [f32; 4] {
let [r, g, b, a] = crate::overlay::HURT_TEXEL.map(|v| f32::from(v) / 255.0);
let white = |u: u32| {
let a = f32::from(crate::overlay::white_alpha(u)) / 255.0;
[1.0, 1.0, 1.0, 1.0 - a]
};
match overlay {
1 | 8 => [r, g, b, 1.0 - a],
5 => white(15),
16..32 => white(overlay - crate::overlay::WHITE),
_ => [0.0; 4],
}
}
/// One draw: its quads in `PackEntities`' buffer, its program and its slot of `HzDraw`.
pub(super) struct Draw {
/// The first vertex.
pub base: u32,
pub quads: u32,
/// The kind of program that draws it (`ENTITY`, `BLOCK`, `TRANSLUCENT`, `PARTICLE`).
pub program: usize,
/// The pack's gbuffers program draws it (else Horizon does).
pub pack: bool,
/// The pack's shadow program draws it into the shadow map too.
pub shadow: bool,
pub slot: u32,
}
/// Which entity runs a pack's shadow pass draws (2.4, 3.2).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Casters {
/// `shadow_entities` (or `shadow`) draws entities...
pub entities: bool,
/// ...only the local player (`shadowPlayer` without `shadowEntities`).
pub only_player: bool,
/// `shadow_block` (or `shadow`) draws block entities...
pub blocks: bool,
/// ...only those whose block emits light (`shadowLightBlockEntities`).
pub only_lit: bool,
}
impl Casters {
/// Whether the shadow pass draws `span`'s run, of kind `program`.
pub fn cast(&self, span: &EntitySpan, program: usize) -> bool {
match program {
ENTITY => self.entities && (!self.only_player || span.player),
BLOCK => self.blocks && (!self.only_lit || span.emissive),
_ => false,
}
}
}
/// The vertices and per-draw uniforms of the runs a pack draws, rebuilt each frame.
pub(super) struct PackEntities {
vertices: wgpu::Buffer,
/// Set 1: the vertices again, for the quads' corners.
pub table: wgpu::BindGroupLayout,
pub table_group: wgpu::BindGroup,
slots: wgpu::Buffer,
pub group: wgpu::BindGroup,
stride: u64,
/// Two triangles per quad, for `quads` quads.
pub indices: wgpu::Buffer,
quads: u32,
scratch: Vec<Vertex>,
uniforms: Vec<u8>,
pub draws: Vec<Draw>,
}
impl PackEntities {
pub fn new(device: &wgpu::Device, layout: &wgpu::BindGroupLayout) -> Self {
let stride = SLOT.max(u64::from(
device.limits().min_uniform_buffer_offset_alignment,
));
let table = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Shader pack entity vertices"),
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: true },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}],
});
let (vertices, table_group) = Self::vertex_buffer(device, &table, 4096);
let (slots, group) = Self::slot_buffer(device, layout, 64, stride);
Self {
vertices,
table,
table_group,
slots,
group,
stride,
indices: quad_indices(device, 1024),
quads: 1024,
scratch: Vec::new(),
uniforms: Vec::new(),
draws: Vec::new(),
}
}
fn vertex_buffer(
device: &wgpu::Device,
table: &wgpu::BindGroupLayout,
vertices: u64,
) -> (wgpu::Buffer, wgpu::BindGroup) {
let buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Shader pack entity vertices"),
size: vertices * std::mem::size_of::<Vertex>() as u64,
usage: wgpu::BufferUsages::VERTEX
| wgpu::BufferUsages::STORAGE
| wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Shader pack entity vertices"),
layout: table,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: buffer.as_entire_binding(),
}],
});
(buffer, group)
}
fn slot_buffer(
device: &wgpu::Device,
layout: &wgpu::BindGroupLayout,
slots: u64,
stride: u64,
) -> (wgpu::Buffer, wgpu::BindGroup) {
let buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Shader pack entity draws"),
size: slots * stride,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Shader pack entity draws"),
layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
buffer: &buffer,
offset: 0,
size: wgpu::BufferSize::new(SLOT),
}),
}],
});
(buffer, group)
}
pub fn vertices(&self) -> wgpu::BufferSlice<'_> {
self.vertices.slice(..)
}
/// The dynamic offset of draw slot `slot`.
pub fn offset(&self, slot: u32) -> u32 {
(u64::from(slot) * self.stride) as u32
}
/// Splits `entities` between the pack, which draws the runs of `spans` its programs cover
/// (`drawn`: whether it has `gbuffers_entities`, `gbuffers_block` or their fallbacks), and
/// Horizon, which draws the rest (`rest`). Uploads the pack's part, and the runs its shadow
/// programs draw (`shadow`). Runs hidden from the camera only ever cast shadows.
#[allow(clippy::too_many_arguments)]
pub fn prepare(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
layout: &wgpu::BindGroupLayout,
entities: &[Vertex],
spans: &[EntitySpan],
drawn: [bool; PROGRAMS],
shadow: Casters,
rest: &mut Vec<Vertex>,
) {
self.draws.clear();
self.scratch.clear();
self.uniforms.clear();
rest.clear();
let first = spans.first().map_or(entities.len(), |s| s.start as usize);
rest.extend_from_slice(&entities[..first.min(entities.len())]);
for (span, range) in runs(entities.len(), spans) {
let run = &entities[range];
let program = program_of(&span).filter(|_| plain(run));
let pack = program.is_some_and(|p| drawn[p]) && !span.hidden;
let casts = program.is_some_and(|p| shadow.cast(&span, p));
if !pack && !span.hidden {
rest.extend_from_slice(run);
}
let Some(program) = program.filter(|_| (pack || casts) && !run.is_empty()) else {
continue;
};
let start = self.scratch.len() as u32;
self.scratch.extend_from_slice(run);
let (entity_id, block_id) = match program {
BLOCK => (0, span.id),
_ => (span.id, 0),
};
let mut bytes = [0u8; SLOT as usize];
bytes[0..4].copy_from_slice(&(span.stage as i32).to_le_bytes());
bytes[4..8].copy_from_slice(&entity_id.to_le_bytes());
bytes[8..12].copy_from_slice(&block_id.to_le_bytes());
for (i, c) in entity_color(run[0].layer).iter().enumerate() {
bytes[16 + 4 * i..20 + 4 * i].copy_from_slice(&c.to_le_bytes());
}
self.uniforms.extend_from_slice(&bytes);
self.uniforms
.resize(self.uniforms.len() + (self.stride - SLOT) as usize, 0);
self.draws.push(Draw {
base: start,
quads: run.len() as u32 / 4,
program,
pack,
shadow: casts,
slot: self.draws.len() as u32,
});
}
if self.draws.is_empty() {
return;
}
let vertex_size = std::mem::size_of::<Vertex>() as u64;
let (need_vertices, need_slots) = (self.scratch.len() as u64, self.draws.len() as u64);
let have_vertices = self.vertices.size() / vertex_size;
let have_slots = self.slots.size() / self.stride;
if need_vertices > have_vertices {
(self.vertices, self.table_group) =
Self::vertex_buffer(device, &self.table, need_vertices.next_power_of_two());
}
if need_slots > have_slots {
(self.slots, self.group) =
Self::slot_buffer(device, layout, need_slots.next_power_of_two(), self.stride);
}
let quads = self.draws.iter().map(|d| d.quads).max().unwrap_or(0);
if quads > self.quads {
self.quads = quads.next_power_of_two();
self.indices = quad_indices(device, self.quads);
}
queue.write_buffer(&self.vertices, 0, bytemuck::cast_slice(&self.scratch));
queue.write_buffer(&self.slots, 0, &self.uniforms);
}
}
/// Two triangles for each of `quads` quads, from vertex 0.
pub(super) fn quad_indices(device: &wgpu::Device, quads: u32) -> wgpu::Buffer {
let indices: Vec<u32> = (0..quads)
.flat_map(|q| [0, 1, 2, 0, 2, 3].map(|i| q * 4 + i))
.collect();
wgpu::util::DeviceExt::create_buffer_init(
device,
&wgpu::util::BufferInitDescriptor {
label: Some("Shader pack entity indices"),
contents: bytemuck::cast_slice(&indices),
usage: wgpu::BufferUsages::INDEX,
},
)
}
/// Whether a run holds only geometry a gbuffers program can draw from the shared atlas: no
/// celestial body, cloud, beam, End portal or crack (overlay codes Horizon's entity shader
/// draws in its own way; entity.wgsl).
fn plain(run: &[Vertex]) -> bool {
run.iter()
.step_by(4)
.all(|v| !matches!(v.layer, 2 | 4 | 6 | 9 | 10 | 11 | 12))
}
/// The programs of entity runs, in the order they draw.
pub const ENTITY: usize = 0;
pub const BLOCK: usize = 1;
pub const TRANSLUCENT: usize = 2;
pub const PARTICLE: usize = 3;
pub const PROGRAMS: usize = 4;
/// Which program draws `span`'s run, given which of them (or their fallbacks) the pack has;
/// `None`: Horizon draws it.
#[cfg(test)]
fn drawn_by_pack(span: &EntitySpan, drawn: [bool; PROGRAMS]) -> Option<usize> {
program_of(span).filter(|&p| drawn[p])
}
/// The kind of program that draws `span`'s run, when a pack may.
pub fn program_of(span: &EntitySpan) -> Option<usize> {
Some(if span.stage == render_stage("ENTITIES") {
if span.translucent {
TRANSLUCENT
} else {
ENTITY
}
} else if span.stage == render_stage("BLOCK_ENTITIES") {
BLOCK
} else if span.stage == render_stage("PARTICLES") {
PARTICLE
} else {
return None;
})
}
/// Each span with its vertex range: from its start to the next span's (or the end).
pub fn runs(
len: usize,
spans: &[EntitySpan],
) -> impl Iterator<Item = (EntitySpan, Range<usize>)> + '_ {
spans.iter().enumerate().map(move |(i, s)| {
let end = spans.get(i + 1).map_or(len, |n| n.start as usize);
let start = (s.start as usize).min(len);
(*s, start..end.clamp(start, len))
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn runs_split_the_list_between_pack_and_horizon() {
let v = |layer| Vertex {
layer,
..bytemuck::Zeroable::zeroed()
};
let span = |start, stage: &str, id| EntitySpan {
start,
stage: render_stage(stage),
id,
..Default::default()
};
let spans = [
span(2, "ENTITIES", 7),
span(5, "PARTICLES", 0),
span(6, "BLOCK_ENTITIES", 9),
];
let ranges: Vec<_> = runs(10, &spans).map(|(_, r)| r).collect();
assert_eq!(ranges, [2..5, 5..6, 6..10]);
let all = [true; PROGRAMS];
assert_eq!(drawn_by_pack(&spans[0], all), Some(ENTITY));
assert_eq!(drawn_by_pack(&spans[1], all), Some(PARTICLE));
assert_eq!(drawn_by_pack(&spans[1], [true, true, true, false]), None);
assert_eq!(drawn_by_pack(&spans[2], [true, false, true, true]), None);
assert_eq!(drawn_by_pack(&spans[2], all), Some(BLOCK));
let shadow = EntitySpan {
translucent: true,
..spans[0]
};
assert_eq!(drawn_by_pack(&shadow, all), Some(TRANSLUCENT));
assert_eq!(drawn_by_pack(&span(0, "DESTROY", 0), all), None);
// A beacon beam's quads stay Horizon's.
assert!(plain(&[v(0), v(0), v(0), v(0), v(1)]));
assert!(!plain(&[v(0), v(0), v(0), v(0), v(6)]));
assert_eq!(entity_color(1)[0], 1.0);
assert!((entity_color(1)[3] - (1.0 - 178.0 / 255.0)).abs() < 1e-6);
assert_eq!(entity_color(0), [0.0; 4]);
// The shadow pass: entities, the player alone, block entities, those that emit light.
let all = Casters {
entities: true,
blocks: true,
..Default::default()
};
let player = EntitySpan {
player: true,
..spans[0]
};
let lit = EntitySpan {
emissive: true,
..spans[2]
};
assert!(all.cast(&spans[0], ENTITY) && all.cast(&spans[2], BLOCK));
assert!(!all.cast(&spans[1], PARTICLE));
let some = Casters {
only_player: true,
only_lit: true,
..all
};
assert!(!some.cast(&spans[0], ENTITY) && some.cast(&player, ENTITY));
assert!(!some.cast(&spans[2], BLOCK) && some.cast(&lit, BLOCK));
}
}
+169
View File
@@ -0,0 +1,169 @@
//! The first-person hand and held items drawn by a shader pack's `gbuffers_hand` (spec 2.3):
//! `Frame::hand`'s entity vertices, read as entities are (`pack_entities::inputs`), with the
//! hand's own projection as `gl_ProjectionMatrix` (5.2: `gbufferProjection` stays the world's).
use glam::Mat4;
use super::Vertex;
/// `MC_HAND_DEPTH`'s value (1.5): the hand's depth is scaled by it through its projection, so
/// that it sits in front of the world in the shared depth buffer.
const HAND_DEPTH: f32 = 0.125;
/// `HzHand`: the hand's projection and its inverse.
const UNIFORM: u64 = 2 * 64;
/// What `gbuffers_hand` reads: the entity inputs, and the hand's projection from set 1.
pub fn inputs() -> horizon_shaders::GbuffersInputs {
let mut inputs = super::pack_entities::inputs();
inputs.declarations.push_str(
"layout(set = 1, binding = 1) uniform HzHand { mat4 hz_hand_projection; mat4 hz_hand_projection_inverse; };\n",
);
inputs.body.push_str(
" hz_ProjectionMatrix = hz_hand_projection;\n \
hz_ProjectionMatrixInverse = hz_hand_projection_inverse;\n \
hz_ModelViewProjectionMatrix = hz_hand_projection * hz_ModelViewMatrix;\n",
);
inputs
}
/// The hand's projection in GL's conventions: `hud_fov` degrees, the view bob, depth scaled by
/// `MC_HAND_DEPTH`.
pub fn projection(hud_fov: f32, aspect: f32, far: f32, bob: Mat4) -> Mat4 {
Mat4::from_scale(glam::Vec3::new(1.0, 1.0, HAND_DEPTH))
* Mat4::perspective_rh_gl(hud_fov.to_radians(), aspect, 0.05, far)
* bob
}
/// The hand's vertices (also read at set 1 for normals and tangents), its uniform and quad
/// indices.
pub struct PackHand {
pub layout: wgpu::BindGroupLayout,
vertices: wgpu::Buffer,
uniform: wgpu::Buffer,
pub group: wgpu::BindGroup,
pub indices: wgpu::Buffer,
capacity: u32,
/// Quads uploaded this frame.
pub quads: u32,
}
impl PackHand {
pub fn new(device: &wgpu::Device) -> Self {
let entry = |binding, ty| wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
};
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Shader pack hand"),
entries: &[
entry(0, wgpu::BufferBindingType::Storage { read_only: true }),
entry(1, wgpu::BufferBindingType::Uniform),
],
});
let uniform = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Shader pack hand"),
size: UNIFORM,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let capacity = 256;
let (vertices, group) = Self::vertex_buffer(device, &layout, &uniform, capacity);
Self {
indices: super::pack_entities::quad_indices(device, capacity),
layout,
vertices,
uniform,
group,
capacity,
quads: 0,
}
}
fn vertex_buffer(
device: &wgpu::Device,
layout: &wgpu::BindGroupLayout,
uniform: &wgpu::Buffer,
quads: u32,
) -> (wgpu::Buffer, wgpu::BindGroup) {
let buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Shader pack hand vertices"),
size: u64::from(quads) * 4 * std::mem::size_of::<Vertex>() as u64,
usage: wgpu::BufferUsages::VERTEX
| wgpu::BufferUsages::STORAGE
| wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Shader pack hand"),
layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: uniform.as_entire_binding(),
},
],
});
(buffer, group)
}
/// Uploads this frame's hand and its projection.
pub fn prepare(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
hand: &[Vertex],
projection: Mat4,
) {
self.quads = (hand.len() / 4) as u32;
if self.quads == 0 {
return;
}
if self.quads > self.capacity {
self.capacity = self.quads.next_power_of_two();
(self.vertices, self.group) =
Self::vertex_buffer(device, &self.layout, &self.uniform, self.capacity);
self.indices = super::pack_entities::quad_indices(device, self.capacity);
}
queue.write_buffer(&self.vertices, 0, bytemuck::cast_slice(hand));
let matrices = [projection, projection.inverse()].map(|m| m.to_cols_array());
queue.write_buffer(&self.uniform, 0, bytemuck::cast_slice(&matrices));
}
pub fn vertices(&self) -> wgpu::BufferSlice<'_> {
self.vertices.slice(..)
}
}
#[cfg(test)]
mod tests {
use super::*;
/// The hand's window depth lies within `MC_HAND_DEPTH` of the middle (packs tell the hand
/// apart by it), and x, y match the plain projection.
#[test]
fn hand_depth_is_scaled_around_the_middle() {
let p = projection(70.0, 16.0 / 9.0, 512.0, Mat4::IDENTITY);
let plain = Mat4::perspective_rh_gl(70f32.to_radians(), 16.0 / 9.0, 0.05, 512.0);
for z in [-0.06f32, -0.5, -3.0, -400.0] {
let v = glam::Vec4::new(0.1, -0.2, z, 1.0);
let (a, b) = (p * v, plain * v);
let window = (a.z / a.w) * 0.5 + 0.5;
assert!(
(window - 0.5).abs() <= HAND_DEPTH * 0.5 + 1e-6,
"{z}: {window}"
);
assert!((a.x / a.w - b.x / b.w).abs() < 1e-6);
}
}
}
File diff suppressed because it is too large Load Diff
+12 -3
View File
@@ -1,5 +1,6 @@
// Helpers of a shader pack's post-processing (pack_post.rs): the world into colortex0 and
// depthtex0, and colortex0 onto the screen for packs without `final`.
// depthtex0 (a depth attachment, for terrain's depth test), and colortex0 onto the screen for
// packs without `final`.
@vertex
fn vs_full(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> {
@@ -12,7 +13,7 @@ fn vs_full(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> {
struct World {
@location(0) color: vec4<f32>,
@location(1) depth: vec4<f32>,
@builtin(frag_depth) depth: f32,
}
// Pack buffers keep GL's rows (row 0 at the bottom), and GL's window depth: Horizon's
@@ -23,7 +24,7 @@ fn fs_blit(@builtin(position) p: vec4<f32>) -> World {
let at = vec2<i32>(i32(p.x), size.y - 1 - i32(p.y));
var out: World;
out.color = textureLoad(source, at, 0);
out.depth = vec4<f32>(1.0 - textureLoad(world_depth, at, 0), 0.0, 0.0, 1.0);
out.depth = 1.0 - textureLoad(world_depth, at, 0);
return out;
}
@@ -52,3 +53,11 @@ fn vs_uv(@builtin(vertex_index) i: u32) -> Full {
fn fs_copy(v: Full) -> @location(0) vec4<f32> {
return textureSample(source, linear, v.uv);
}
// One level of a buffer's mip chain from the level above (6.1.7): the average of the 2×2
// texels under each destination texel, by one bilinear sample at their shared corner.
@fragment
fn fs_down(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
let size = max(textureDimensions(source) / 2u, vec2<u32>(1u));
return textureSampleLevel(source, linear, p.xy / vec2<f32>(size), 0.0);
}
@@ -0,0 +1,208 @@
//! A shader pack's shadow buffers (spec 6.3): the shadow map's depth (`shadowtex0`, and
//! `shadowtex1` without translucent geometry), and the colour buffers `shadowcolor0/1`, each
//! with two sides that `shadowcomp` programs flip as composite programs flip colortex (6.1.6).
//! Square, `shadowMapResolution` pixels.
type Ops<T> = wgpu::Operations<T>;
type Color = wgpu::Color;
/// Loaded and stored.
pub(super) fn load<T>() -> Ops<T> {
wgpu::Operations {
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
}
}
fn clear(c: [f32; 4]) -> Ops<Color> {
wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
r: c[0].into(),
g: c[1].into(),
b: c[2].into(),
a: c[3].into(),
}),
store: wgpu::StoreOp::Store,
}
}
/// What the shadow pass writes and the programs read.
pub(super) struct ShadowTargets {
pub size: u32,
/// `shadowtex0`, written by the shadow pass.
pub depth: (wgpu::Texture, wgpu::TextureView),
/// `shadowtex1`: a copy of `shadowtex0` taken before translucent terrain, when read.
pub depth1: Option<(wgpu::Texture, wgpu::TextureView)>,
/// Both sides of each shadowcolor buffer used.
pub color: [Option<[wgpu::TextureView; 2]>; 2],
/// The clear colour of each buffer cleared every frame.
pub clears: [Option<[f32; 4]>; 2],
/// Bit n set: B is shadowcolor n's "main".
pub main: u16,
/// Nothing cleared yet this frame: the first pass into the shadow buffers clears them as it
/// begins (`clear_ops`), which saves a pass that loads and stores them all on tiled GPUs.
fresh: bool,
/// Bit n set: shadowcolor n was cleared that way.
cleared: u8,
/// Depth comparison for `sampler2DShadow` (hardware filtering): linear, as GL filters it.
pub comparison: wgpu::Sampler,
}
fn target(
device: &wgpu::Device,
label: &str,
size: u32,
format: wgpu::TextureFormat,
copy: wgpu::TextureUsages,
) -> (wgpu::Texture, wgpu::TextureView) {
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some(label),
size: wgpu::Extent3d {
width: size,
height: size,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::RENDER_ATTACHMENT | copy,
view_formats: &[],
});
let view = texture.create_view(&Default::default());
(texture, view)
}
impl ShadowTargets {
/// The buffers of `shadow` the pack uses: `colors` (bit n: shadowcolor n), `depth1` when
/// some program reads `shadowtex1`. The resolution is kept within the device's limit.
pub fn new(
device: &wgpu::Device,
shadow: &horizon_shaders::Shadow,
formats: [wgpu::TextureFormat; 2],
colors: u16,
depth1: bool,
) -> Self {
let size = shadow
.resolution
.clamp(1, device.limits().max_texture_dimension_2d);
let copy = wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::COPY_DST;
let depth = wgpu::TextureFormat::Depth32Float;
let color = std::array::from_fn(|n| {
(colors & 1 << n != 0).then(|| {
[0, 1].map(|side| {
let label = format!("Shader pack shadowcolor{n} {}", ["A", "B"][side]);
target(
device,
&label,
size,
formats[n],
wgpu::TextureUsages::empty(),
)
.1
})
})
});
Self {
size,
depth: target(device, "Shader pack shadowtex0", size, depth, copy),
depth1: depth1.then(|| target(device, "Shader pack shadowtex1", size, depth, copy)),
color,
clears: std::array::from_fn(|n| {
let b = &shadow.colors[n];
b.clear.then(|| b.clear_color.unwrap_or([1.0; 4]))
}),
main: 0,
fresh: false,
cleared: 0,
comparison: device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("Shader pack shadow comparison"),
compare: Some(wgpu::CompareFunction::LessEqual),
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
..Default::default()
}),
}
}
/// Side `alt` (of "main" and "alt") of shadowcolor `n`.
pub fn color(&self, n: usize, alt: bool) -> Option<&wgpu::TextureView> {
let side = usize::from(self.main & 1 << n != 0) ^ usize::from(alt);
self.color.get(n)?.as_ref().map(|c| &c[side])
}
/// A new frame: the shadow map and the shadowcolor buffers cleared every frame (6.3) are
/// cleared by the first pass that begins (`clear_ops`), or else by `clear_rest`.
pub fn begin_frame(&mut self) {
self.fresh = true;
self.cleared = 0;
}
/// The load operations of a pass into the shadow map and shadowcolor buffers `writes`:
/// clears in the frame's first one, loads after.
pub fn clear_ops(
&mut self,
writes: impl Iterator<Item = usize>,
) -> (Ops<f32>, [Ops<Color>; 2]) {
if !std::mem::take(&mut self.fresh) {
return (load(), [load(); 2]);
}
let mut colors = [load(); 2];
for n in writes.filter(|&n| n < 2) {
if let Some(c) = self.clears[n] {
colors[n] = clear(c);
self.cleared |= 1 << n;
}
}
let depth = wgpu::Operations {
load: wgpu::LoadOp::Clear(1.0),
store: wgpu::StoreOp::Store,
};
(depth, colors)
}
/// Clears what no pass has cleared this frame: all of it when no shadow program drew, the
/// cleared shadowcolor buffers the first one did not write.
pub fn clear_rest(&mut self, encoder: &mut wgpu::CommandEncoder) {
let depth = std::mem::take(&mut self.fresh);
let cleared = std::mem::replace(&mut self.cleared, 0b11);
let attachments: Vec<_> = (0..2)
.filter(|n| cleared & 1 << n == 0)
.filter_map(|n| {
Some(Some(wgpu::RenderPassColorAttachment {
view: self.color(n, false)?,
depth_slice: None,
resolve_target: None,
ops: clear(self.clears[n]?),
}))
})
.collect();
if !depth && attachments.is_empty() {
return;
}
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Shader pack shadow clear"),
color_attachments: &attachments,
depth_stencil_attachment: depth.then_some(wgpu::RenderPassDepthStencilAttachment {
view: &self.depth.1,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(1.0),
store: wgpu::StoreOp::Store,
}),
stencil_ops: None,
}),
..Default::default()
});
}
/// `shadowtex1`: the shadow map as it is now (before translucent geometry).
pub fn copy_depth1(&self, encoder: &mut wgpu::CommandEncoder) {
if let Some((copy, _)) = &self.depth1 {
encoder.copy_texture_to_texture(
self.depth.0.as_image_copy(),
copy.as_image_copy(),
self.depth.0.size(),
);
}
}
}
+323
View File
@@ -0,0 +1,323 @@
//! The vanilla sky's geometry for a shader pack's `gbuffers_skybasic` (spec 2.3): the sky disc,
//! the sunset fan, the stars and the void disc, as Horizon's own sky draws them (terrain.wgsl,
//! stars.rs), built once into a vertex buffer. The frame's colours and rotations sit in a small
//! uniform (set 1) that the vertex inputs read. `gbuffers_skytextured` draws the frame's sun and
//! moon quads, textured from the shared atlas.
use std::ops::Range;
use wgpu::util::DeviceExt;
/// A sky vertex: a position and what it belongs to (`SKY`...).
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct Vertex {
pos: [f32; 3],
kind: u32,
}
const SKY: u32 = 0;
const SUNSET_APEX: u32 = 1;
/// Rim of the sunset fan: `pos.xy` is (sin, cos) of its angle.
const SUNSET_RIM: u32 = 2;
const STAR: u32 = 3;
const VOID: u32 = 4;
/// The frame's sky (std140).
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct Uniform {
/// Camera-relative world from star space.
stars: [[f32; 4]; 4],
/// Sky colour, alpha 1.
color: [f32; 4],
/// Sunset colour and alpha.
sunrise: [f32; 4],
/// cos and sin of the sunset fan's turn, star brightness.
sun: [f32; 4],
}
/// A sun or moon vertex: position, atlas UV, RGBA8 colour.
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct Celestial {
pos: [f32; 3],
uv: [f32; 2],
color: u32,
}
/// Quads of the sun and moon (and room to spare).
const CELESTIAL_QUADS: usize = 4;
const CELESTIAL_ATTRIBUTES: [wgpu::VertexAttribute; 3] =
wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x2, 2 => Unorm8x4];
pub(super) const ATTRIBUTES: [wgpu::VertexAttribute; 2] =
wgpu::vertex_attr_array![0 => Float32x3, 1 => Uint32];
/// What `gbuffers_skybasic` reads from these vertices.
pub fn inputs() -> horizon_shaders::GbuffersInputs {
let declarations =
"layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in uint hz_v_kind;\n\
layout(set = 1, binding = 0) uniform HzSky { mat4 hz_sky_stars; vec4 hz_sky_color; \
vec4 hz_sky_sunrise; vec4 hz_sky_sun; };\n"
.to_owned();
let body = format!(
" vec3 hz_p = hz_v_pos;\n \
hz_Color = hz_sky_color;\n \
if (hz_v_kind == {SUNSET_APEX}u || hz_v_kind == {SUNSET_RIM}u) {{\n \
vec3 hz_l = vec3(0.0, 100.0, 0.0);\n \
hz_Color = hz_sky_sunrise;\n \
if (hz_v_kind == {SUNSET_RIM}u) {{\n \
hz_l = vec3(hz_v_pos.xy * 120.0, -hz_v_pos.y * 40.0 * hz_sky_sunrise.a);\n \
hz_Color.a = 0.0;\n \
}}\n \
vec3 hz_r = vec3(hz_l.x * hz_sky_sun.x - hz_l.y * hz_sky_sun.y, hz_l.x * hz_sky_sun.y + hz_l.y * hz_sky_sun.x, hz_l.z);\n \
hz_p = vec3(hz_r.x, -hz_r.z, hz_r.y);\n \
}} else if (hz_v_kind == {STAR}u) {{\n \
hz_p = (hz_sky_stars * vec4(hz_v_pos, 1.0)).xyz;\n \
hz_Color = vec4(hz_sky_sun.z);\n \
}} else if (hz_v_kind == {VOID}u) {{\n \
hz_Color = vec4(0.0, 0.0, 0.0, 1.0);\n \
}}\n \
hz_Vertex = vec4(hz_p, 1.0);\n \
hz_MultiTexCoord0 = vec4(0.0, 0.0, 0.0, 1.0);\n \
hz_MultiTexCoord1 = vec4(0.0, 240.0, 0.0, 1.0);\n \
hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \
hz_Normal = vec3(0.0, 1.0, 0.0);\n"
);
horizon_shaders::GbuffersInputs {
declarations,
body,
attributes: vec![
("vaPosition".into(), "hz_Vertex.xyz".into()),
("vaColor".into(), "hz_Color".into()),
("vaNormal".into(), "hz_Normal".into()),
],
// At the far plane: the sky shows only where nothing else drew, and it writes no depth.
after: " gl_Position.z = gl_Position.w;\n".into(),
alpha_test: None,
}
}
/// What `gbuffers_skytextured` reads from the sun and moon's vertices.
pub fn celestial_inputs() -> horizon_shaders::GbuffersInputs {
horizon_shaders::GbuffersInputs {
declarations:
"layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in vec2 hz_v_uv;\n\
layout(location = 2) in vec4 hz_v_color;\n"
.into(),
body: " hz_Vertex = vec4(hz_v_pos, 1.0);\n \
hz_MultiTexCoord0 = vec4(hz_v_uv, 0.0, 1.0);\n \
hz_MultiTexCoord1 = vec4(0.0, 240.0, 0.0, 1.0);\n \
hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \
hz_Color = hz_v_color;\n \
hz_Normal = vec3(0.0, 0.0, 1.0);\n"
.into(),
attributes: vec![
("vaPosition".into(), "hz_Vertex.xyz".into()),
("vaUV0".into(), "hz_v_uv".into()),
("vaColor".into(), "hz_Color".into()),
("vaNormal".into(), "hz_Normal".into()),
],
after: " gl_Position.z = gl_Position.w;\n".into(),
alpha_test: None,
}
}
/// One draw of the sky: its render stage, whether `gbuffers_skytextured` draws it, its vertices.
pub(super) struct Part {
pub stage: &'static str,
pub textured: bool,
pub range: Range<u32>,
}
/// A disc fan at height `y`, radius 512, as triangles (SkyRenderer's sky and void discs).
fn disc(y: f32, kind: u32, out: &mut Vec<Vertex>) {
let sign = y.signum();
let rim = |i: i32| {
let a = (i as f32).to_radians();
Vertex {
pos: [sign * 512.0 * a.cos(), y, 512.0 * a.sin()],
kind,
}
};
let centre = Vertex {
pos: [0.0, y, 0.0],
kind,
};
for i in (-180..180).step_by(45) {
out.extend([centre, rim(i), rim(i + 45)]);
}
}
pub(super) struct PackSky {
vertices: wgpu::Buffer,
celestial: wgpu::Buffer,
uniform: wgpu::Buffer,
pub layout: wgpu::BindGroupLayout,
group: wgpu::BindGroup,
/// Vertex ranges of the sky disc, the sunset fan, the stars and the void.
ranges: [Range<u32>; 4],
}
impl PackSky {
pub fn new(device: &wgpu::Device) -> Self {
let mut v = Vec::new();
disc(16.0, SKY, &mut v);
let sky = 0..v.len() as u32;
let apex = Vertex {
pos: [0.0; 3],
kind: SUNSET_APEX,
};
let rim = |i: u32| {
let a = i as f32 * std::f32::consts::TAU / 16.0;
Vertex {
pos: [a.sin(), a.cos(), 0.0],
kind: SUNSET_RIM,
}
};
for i in 0..16 {
v.extend([apex, rim(i), rim(i + 1)]);
}
let sunset = sky.end..v.len() as u32;
v.extend(
super::stars::star_triangles()
.into_iter()
.map(|pos| Vertex { pos, kind: STAR }),
);
let stars = sunset.end..v.len() as u32;
disc(-4.0, VOID, &mut v);
let void = stars.end..v.len() as u32;
let vertices = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Shader pack sky"),
contents: bytemuck::cast_slice(&v),
usage: wgpu::BufferUsages::VERTEX,
});
let celestial = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Shader pack sun and moon"),
size: (std::mem::size_of::<Celestial>() * 6 * CELESTIAL_QUADS) as u64,
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let uniform = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Shader pack sky"),
size: std::mem::size_of::<Uniform>() as u64,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Shader pack sky"),
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}],
});
let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Shader pack sky"),
layout: &layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniform.as_entire_binding(),
}],
});
Self {
vertices,
celestial,
uniform,
layout,
group,
ranges: [sky, sunset, stars, void],
}
}
pub fn buffers() -> [Option<wgpu::VertexBufferLayout<'static>>; 1] {
[Some(wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &ATTRIBUTES,
})]
}
pub fn celestial_buffers() -> [Option<wgpu::VertexBufferLayout<'static>>; 1] {
[Some(wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Celestial>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &CELESTIAL_ATTRIBUTES,
})]
}
/// Writes the frame's sky and returns its draws in vanilla's order: the disc, the sunset
/// fan while the sun is near the horizon, the sun and the moon, the stars and, with the
/// camera low, the void. `tilt` is the pack's `sunPathRotation` (degrees): the sun, moon
/// and stars turn with it (7.1).
pub fn prepare(&self, queue: &wgpu::Queue, frame: &super::Frame, tilt: f32) -> Vec<Part> {
let turn = super::pack_uniforms::tilt_matrix(tilt);
let sun_angle = frame.sky.sun_angle.to_radians();
// The fan turns to the other horizon in the afternoon (terrain.wgsl's sunrise_weight).
let flip = if sun_angle.sin() < 0.0 { 180f32 } else { 0.0 };
let theta = (flip + 90.0).to_radians();
let (rotation, brightness) = frame.stars.unwrap_or_default();
let [r, g, b] = frame.sky.sky_color;
let uniform = Uniform {
stars: (turn * rotation).to_cols_array_2d(),
color: [r, g, b, 1.0],
sunrise: frame.sky.sunrise_color,
sun: [theta.cos(), theta.sin(), brightness.clamp(0.0, 1.0), 0.0],
};
queue.write_buffer(&self.uniform, 0, bytemuck::bytes_of(&uniform));
// The sun's quad, then the moon's (Game::celestial_geometry), as triangles.
let quads = frame
.celestial
.as_chunks::<4>()
.0
.iter()
.take(CELESTIAL_QUADS);
let triangles: Vec<Celestial> = quads
.clone()
.flat_map(|q| [0, 1, 2, 0, 2, 3].map(|i| q[i]))
.map(|v| Celestial {
pos: turn.transform_vector3(v.pos.into()).into(),
uv: v.uv,
color: v.color,
})
.collect();
queue.write_buffer(&self.celestial, 0, bytemuck::cast_slice(&triangles));
let [sky, sunset, stars, void] = self.ranges.clone();
let part = |stage, textured, range| Part {
stage,
textured,
range,
};
let mut parts = vec![part("SKY", false, sky)];
if frame.sky.sunrise_color[3] > 0.001 {
parts.push(part("SUNSET", false, sunset));
}
for (i, stage) in ["SUN", "MOON"].into_iter().enumerate().take(quads.len()) {
parts.push(part(stage, true, i as u32 * 6..i as u32 * 6 + 6));
}
if frame.stars.is_some() {
parts.push(part("STARS", false, stars));
}
if frame.dark_disc {
parts.push(part("VOID", false, void));
}
parts
}
/// Binds the vertices of the `textured` (sun and moon) or other parts.
pub fn bind(&self, pass: &mut wgpu::RenderPass<'_>, textured: bool) {
if textured {
pass.set_vertex_buffer(0, self.celestial.slice(..));
} else {
pass.set_bind_group(1, &self.group, &[]);
pass.set_vertex_buffer(0, self.vertices.slice(..));
}
}
}
@@ -0,0 +1,148 @@
//! Developers' aid: `HORIZON_SHADER_PACK_TIMING=1` times every pass of a shader pack on the
//! GPU, one frame in `EVERY`, and logs each pass's median every `REPORT` timed frames. The
//! frame after a timed one waits for the GPU before reading the timestamps (Metal resolves
//! them without waiting for the passes that write them), so timing slows the game.
const EVERY: u32 = 30;
const REPORT: usize = 30;
/// Passes timed per frame at most (two timestamps each).
const CAPACITY: u32 = 64;
pub(super) struct PassTiming {
queries: wgpu::QuerySet,
resolve: wgpu::Buffer,
read: wgpu::Buffer,
period_ns: f32,
frame: u32,
/// The passes timed in the frame being recorded or waiting to be read, in order.
names: Vec<String>,
timed: bool,
samples: Vec<(String, Vec<f32>)>,
reports: usize,
}
impl PassTiming {
pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Option<Self> {
if std::env::var_os("HORIZON_SHADER_PACK_TIMING").is_none()
|| !device.features().contains(wgpu::Features::TIMESTAMP_QUERY)
{
return None;
}
let size = u64::from(CAPACITY) * 2 * 8;
let buffer = |label, usage| {
device.create_buffer(&wgpu::BufferDescriptor {
label: Some(label),
size,
usage,
mapped_at_creation: false,
})
};
Some(Self {
queries: device.create_query_set(&wgpu::QuerySetDescriptor {
label: Some("Shader pack pass timing"),
ty: wgpu::QueryType::Timestamp,
count: CAPACITY * 2,
}),
resolve: buffer(
"Shader pack timing resolve",
wgpu::BufferUsages::QUERY_RESOLVE | wgpu::BufferUsages::COPY_SRC,
),
read: buffer(
"Shader pack timing read",
wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
),
period_ns: queue.get_timestamp_period(),
frame: 0,
names: Vec::new(),
timed: false,
samples: Vec::new(),
reports: 0,
})
}
/// At the start of a frame: reads the last timed frame's passes, and decides whether this
/// one is timed.
pub fn begin(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) {
if self.timed && !self.names.is_empty() {
self.collect(device, queue);
}
self.names.clear();
self.frame += 1;
self.timed = self.frame.is_multiple_of(EVERY);
}
/// Timestamp writes for the pass `name`, in a timed frame.
pub fn writes(&mut self, name: &str) -> Option<wgpu::RenderPassTimestampWrites<'_>> {
let i = self.names.len() as u32;
if !self.timed || i >= CAPACITY {
return None;
}
self.names.push(name.to_owned());
Some(wgpu::RenderPassTimestampWrites {
query_set: &self.queries,
beginning_of_pass_write_index: Some(2 * i),
end_of_pass_write_index: Some(2 * i + 1),
})
}
fn collect(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) {
// The timed frame first, then the resolve, so every timestamp is written.
_ = device.poll(wgpu::PollType::wait_indefinitely());
let count = self.names.len() as u32 * 2;
let bytes = u64::from(count) * 8;
let mut encoder = device.create_command_encoder(&Default::default());
encoder.resolve_query_set(&self.queries, 0..count, &self.resolve, 0);
encoder.copy_buffer_to_buffer(&self.resolve, 0, &self.read, 0, bytes);
queue.submit([encoder.finish()]);
let slice = self.read.slice(..bytes);
slice.map_async(wgpu::MapMode::Read, |_| {});
_ = device.poll(wgpu::PollType::wait_indefinitely());
let Ok(data) = slice.get_mapped_range() else {
return;
};
let stamps: Vec<u64> = data
.as_chunks::<8>()
.0
.iter()
.map(|b| u64::from_le_bytes(*b))
.collect();
drop(data);
self.read.unmap();
// Passes overlap on tiled GPUs (a pass begins before the one before it ends): a pass
// costs the time from the later of its beginning and the previous pass's end.
let mut previous_end = 0;
for (name, pair) in self.names.iter().zip(stamps.as_chunks::<2>().0) {
if pair[0] == 0 || pair[1] < pair[0] {
continue;
}
let from = pair[0].max(previous_end).min(pair[1]);
previous_end = pair[1];
let ms = (pair[1] - from) as f32 * self.period_ns / 1e6;
match self.samples.iter_mut().find(|s| s.0 == *name) {
Some(s) => s.1.push(ms),
None => self.samples.push((name.clone(), vec![ms])),
}
}
self.reports += 1;
if self.reports.is_multiple_of(REPORT) {
self.report();
}
}
fn report(&mut self) {
let mut total = 0.0;
let mut parts = Vec::new();
// A pass that did not run in this window (no entities, the sky out of sight) is left out.
for (name, v) in self.samples.iter_mut().filter(|s| !s.1.is_empty()) {
v.sort_by(f32::total_cmp);
let median = v[v.len() / 2];
total += median;
parts.push(format!("{name} {median:.3}"));
v.clear();
}
log::info!(
"shader pack GPU time per pass (median ms of {REPORT} frames): {} = {total:.3}",
parts.join(", ")
);
}
}
+206 -16
View File
@@ -3,7 +3,8 @@
//! Matrices follow GL's conventions (5.5): `gbufferProjection` maps view depth to clip z in
//! [-w, w], and the depth buffers packs read hold GL window depth.
use glam::{Mat4, Vec3, Vec4};
use glam::{DMat4, DVec3, Mat4, Vec3, Vec4};
use horizon_shaders::Shadow;
use super::Frame;
@@ -23,6 +24,15 @@ pub struct PackInputs {
pub hide_gui: bool,
/// The brightness option, 0..1.
pub brightness: f32,
/// The biome at the camera (3.4): registry id, `CAT_*`, `PPT_*`, temperature, downfall.
pub biome: i32,
pub biome_category: i32,
pub biome_precipitation: i32,
pub temperature: f32,
pub rainfall: f32,
/// `heldItemId`, `heldItemId2` (item.properties, 5.3) and `heldBlockLightValue(2)`.
pub held_items: [i32; 2],
pub held_light: [i32; 2],
}
/// One uniform's value: up to 16 floats or 3 integers.
@@ -134,43 +144,129 @@ pub const NAMES: &[&str] = &[
"heldBlockLightValue",
"heldBlockLightValue2",
"renderStage",
"biome",
"biome_category",
"biome_precipitation",
"temperature",
"rainfall",
"gbufferModelView",
"gbufferModelViewInverse",
"gbufferProjection",
"gbufferProjectionInverse",
"gbufferPreviousModelView",
"gbufferPreviousProjection",
"atlasSize",
"shadowModelView",
"shadowModelViewInverse",
"shadowProjection",
"shadowProjectionInverse",
];
/// Values carried from frame to frame.
pub struct FrameUniforms {
pub values: Vec<Value>,
/// The block atlas's size (`atlasSize`), 0 without one.
pub atlas: [u32; 2],
start: std::time::Instant,
last: Option<std::time::Instant>,
frame: u32,
previous: Option<(Vec3, Mat4, Mat4)>,
wetness: f32,
eye_smooth: [f32; 2],
/// The pack's shadow settings (7.1).
pub shadow: Shadow,
}
impl Default for FrameUniforms {
fn default() -> Self {
Self {
values: Vec::new(),
atlas: [0; 2],
start: std::time::Instant::now(),
last: None,
frame: 0,
previous: None,
wetness: 0.0,
eye_smooth: [0.0; 2],
shadow: Shadow::default(),
}
}
}
/// `(0, 100, 0)` on the frame the sky draws a body on: Y -90°, then X by its angle in degrees.
fn body(angle: f32) -> Vec3 {
(Mat4::from_rotation_y((-90.0f32).to_radians()) * Mat4::from_rotation_x(angle.to_radians()))
.transform_vector3(Vec3::new(0.0, 100.0, 0.0))
/// `(0, 100, 0)` on the frame the sky draws a body on: Y -90°, then X by its angle in degrees;
/// `sunPathRotation` (`tilt`, degrees) then turns the path about the world's X axis (7.1, OQ-6).
fn body(angle: f32, tilt: f32) -> Vec3 {
(tilt_matrix(tilt)
* Mat4::from_rotation_y((-90.0f32).to_radians())
* Mat4::from_rotation_x(angle.to_radians()))
.transform_vector3(Vec3::new(0.0, 100.0, 0.0))
}
/// `sunPathRotation`'s turn of the sky's bodies: `tilt` degrees about the world's X axis, so a
/// positive angle moves the noon sun toward +Z (south).
pub fn tilt_matrix(tilt: f32) -> Mat4 {
Mat4::from_rotation_x(tilt.to_radians())
}
/// The world direction of the shadow light (7.1): the sun by day, the moon by night.
pub fn shadow_light(frame: &Frame, shadow: &Shadow) -> Vec3 {
let sun_angle = (frame.sky.sun_angle / 360.0 + 0.25).rem_euclid(1.0);
let angle = if sun_angle < 0.5 {
frame.sky.sun_angle
} else {
frame.sky.moon_angle
};
body(angle, shadow.sun_path_rotation).normalize()
}
/// `shadow_camera`'s view and projection in wgpu's clip space (depth 0..1), for culling.
pub fn shadow_culling(frame: &Frame, shadow: &Shadow) -> Mat4 {
let (view, gl) = shadow_camera(shadow_light(frame, shadow), frame.camera, shadow);
// GL's depth -w..w to wgpu's 0..w.
let depth = Mat4::from_cols(
Vec4::X,
Vec4::Y,
Vec4::new(0.0, 0.0, 0.5, 0.0),
Vec4::new(0.0, 0.0, 0.5, 1.0),
);
depth * gl * view
}
/// Distance from the snapped point to the shadow camera, toward the light: the middle of the
/// default near and far planes (OQ-6).
const SHADOW_EYE: f64 = 128.0;
/// The shadow camera (7.1; OQ-6, impl-plan.md): its view (player space to shadow view space)
/// and GL projection. The camera looks from the light, world +Z up (+Y when the light is near
/// Z); the point it centres moves in steps of `shadowIntervalSize` along the shadow view's
/// own axes, and it sits `SHADOW_EYE` blocks toward the light from it. Orthographic over
/// `shadowDistance` each side, or perspective with `shadowMapFov`.
pub fn shadow_camera(light: Vec3, camera: DVec3, shadow: &Shadow) -> (Mat4, Mat4) {
let light = light.as_dvec3();
let up = if light.z.abs() > 0.99 {
DVec3::Y
} else {
DVec3::Z
};
let rotation = DMat4::look_to_rh(DVec3::ZERO, -light, up);
let at = rotation.transform_vector3(camera);
let step = f64::from(shadow.interval);
let snapped = if step > 0.0 {
(at / step).floor() * step
} else {
at
};
let offset = at - snapped - DVec3::new(0.0, 0.0, SHADOW_EYE);
let view = (DMat4::from_translation(offset) * rotation).as_mat4();
let (near, far) = (shadow.near, shadow.far);
let projection = match shadow.fov {
Some(fov) => Mat4::perspective_rh_gl(fov.to_radians(), 1.0, near, far),
None => {
let d = shadow.distance;
Mat4::orthographic_rh_gl(-d, d, -d, d, near, far)
}
};
(view, projection)
}
/// `value` moved toward `target` with a half-life of `half_life` seconds over `dt` (5.6).
@@ -201,8 +297,9 @@ impl FrameUniforms {
let to_view = |v: Vec3| view.transform_vector3(v);
let sun_angle = (frame.sky.sun_angle / 360.0 + 0.25).rem_euclid(1.0);
let day = sun_angle < 0.5;
let sun = to_view(body(frame.sky.sun_angle));
let moon = to_view(body(frame.sky.moon_angle));
let tilt = self.shadow.sun_path_rotation;
let sun = to_view(body(frame.sky.sun_angle, tilt));
let moon = to_view(body(frame.sky.moon_angle, tilt));
// Half-lives are given in ticks (7.2); smoothing runs per frame in seconds (OQ-26).
let half_life = if inputs.rain > self.wetness {
600.0
@@ -214,14 +311,22 @@ impl FrameUniforms {
for (smoothed, target) in self.eye_smooth.iter_mut().zip(eye) {
*smoothed = smooth(*smoothed, target, 10.0 / 20.0, dt);
}
// Vanilla fades by the stronger of two linear fogs; a pack gets one: the pair (start
// and end together) of the one that is complete nearer (OQ-36). Mixing the nearer start
// of one with the nearer end of the other made fog begin at the camera on clear days.
let (fog_color, fog_start, fog_end) = match &frame.fog {
Some(f) => (
[f.color[0], f.color[1], f.color[2]],
f.environmental_start.min(f.render_distance_start),
f.environmental_end.min(f.render_distance_end),
),
Some(f) => {
let (start, end) = if f.environmental_end < f.render_distance_end {
(f.environmental_start, f.environmental_end)
} else {
(f.render_distance_start, f.render_distance_end)
};
([f.color[0], f.color[1], f.color[2]], start, end)
}
None => (frame.sky.fog_color, frame.distance * 0.9, frame.distance),
};
let light = shadow_light(frame, &self.shadow);
let (shadow_view, shadow_projection) = shadow_camera(light, frame.camera, &self.shadow);
let day_time = inputs.day_time.rem_euclid(24000) as i32;
let world_day = inputs.day_time.div_euclid(24000);
let seconds = (now - self.start).as_secs_f32() % 3600.0;
@@ -243,6 +348,11 @@ impl FrameUniforms {
"worldDay" => Value::i(&[world_day as i32]),
"moonPhase" => Value::i(&[world_day.rem_euclid(8) as i32]),
"frameCounter" => Value::i(&[self.frame as i32]),
"shadowModelView" => m4(shadow_view),
"shadowModelViewInverse" => m4(shadow_view.inverse()),
"shadowProjection" => m4(shadow_projection),
"shadowProjectionInverse" => m4(shadow_projection.inverse()),
"atlasSize" => Value::i(&[self.atlas[0] as i32, self.atlas[1] as i32]),
"frameTime" => Value::f(&[dt]),
"frameTimeCounter" => Value::f(&[seconds]),
"viewWidth" => Value::f(&[size[0] as f32]),
@@ -269,6 +379,11 @@ impl FrameUniforms {
"screenBrightness" => Value::f(&[inputs.brightness]),
"hideGUI" => Value::i(&[inputs.hide_gui as i32]),
"eyeBrightness" => Value::i(&eye.map(|v| v as i32)),
"biome" => Value::i(&[inputs.biome]),
"biome_category" => Value::i(&[inputs.biome_category]),
"biome_precipitation" => Value::i(&[inputs.biome_precipitation]),
"temperature" => Value::f(&[inputs.temperature]),
"rainfall" => Value::f(&[inputs.rainfall]),
"eyeBrightnessSmooth" => Value::i(&self.eye_smooth.map(|v| v.round() as i32)),
"gbufferModelView" => m4(view),
"gbufferModelViewInverse" => m4(view.inverse()),
@@ -276,8 +391,11 @@ impl FrameUniforms {
"gbufferProjectionInverse" => m4(projection.inverse()),
"gbufferPreviousModelView" => m4(previous_view),
"gbufferPreviousProjection" => m4(previous_projection),
// Held items wait for `item.properties` (5.3: 0 without the file); renderStage
// is NONE outside gbuffers draws.
"heldItemId" => Value::i(&[inputs.held_items[0]]),
"heldItemId2" => Value::i(&[inputs.held_items[1]]),
"heldBlockLightValue" => Value::i(&[inputs.held_light[0]]),
"heldBlockLightValue2" => Value::i(&[inputs.held_light[1]]),
// renderStage is NONE outside gbuffers draws.
_ => Value::i(&[0]),
}
};
@@ -299,6 +417,47 @@ impl FrameUniforms {
}
}
impl FrameUniforms {
/// Evaluates the pack's custom uniforms with this frame's values and puts them after
/// `NAMES`' values, in `custom.uniforms()` order.
pub fn eval_custom(&mut self, custom: &mut horizon_shaders::Custom) {
self.values.truncate(NAMES.len());
let values = &self.values;
let builtin = |name: &str| {
let v = values.get(index(name)?)?;
let n = components(name);
Some(match *v {
Value::F(f) => horizon_shaders::Val::vector(&f.map(f64::from)[..n]),
Value::I(i) => horizon_shaders::Val::vector(&i.map(f64::from)[..n.min(3)]),
})
};
let dt = match index("frameTime").and_then(|i| values.get(i)) {
Some(Value::F(f)) => f64::from(f[0]),
_ => 0.0,
};
let out = custom.eval(dt, &builtin);
self.values
.extend(out.iter().map(|v| Value::f(&v.v.map(|x| x as f32)[..v.n])));
}
}
/// How many components the uniform `name` has (16 for a matrix).
fn components(name: &str) -> usize {
match name {
"cameraPosition"
| "previousCameraPosition"
| "upPosition"
| "sunPosition"
| "moonPosition"
| "shadowLightPosition"
| "fogColor"
| "skyColor" => 3,
"eyeBrightness" | "eyeBrightnessSmooth" | "atlasSize" => 2,
n if n.starts_with("gbuffer") || n.starts_with("shadow") && n != "shadowAngle" => 16,
_ => 1,
}
}
/// The position of `name` in [`NAMES`].
pub fn index(name: &str) -> Option<usize> {
NAMES.iter().position(|n| *n == name)
@@ -323,7 +482,38 @@ mod tests {
#[test]
fn noon_is_straight_up_and_the_sun_sets_in_the_west() {
assert!(body(0.0).abs_diff_eq(Vec3::new(0.0, 100.0, 0.0), 1e-3));
assert!(body(90.0).abs_diff_eq(Vec3::new(-100.0, 0.0, 0.0), 1e-3));
assert!(body(0.0, 0.0).abs_diff_eq(Vec3::new(0.0, 100.0, 0.0), 1e-3));
assert!(body(90.0, 0.0).abs_diff_eq(Vec3::new(-100.0, 0.0, 0.0), 1e-3));
// sunPathRotation tilts noon toward +Z and leaves sunset on the horizon.
assert!(body(0.0, 30.0).abs_diff_eq(Vec3::new(0.0, 86.60254, 50.0), 1e-3));
assert!(body(90.0, 30.0).abs_diff_eq(Vec3::new(-100.0, 0.0, 0.0), 1e-3));
}
#[test]
fn the_shadow_camera_looks_down_from_the_light_in_steps() {
let shadow = Shadow::default();
let mut camera = DVec3::new(1000.3, 70.0, -20.7);
// Noon: the light is straight up.
let (view, projection) = shadow_camera(Vec3::Y, camera, &shadow);
let at = |p: Vec3| view.transform_point3(p);
// Straight down: a block below the player is farther from the light.
assert!(at(Vec3::new(0.0, -1.0, 0.0)).z < at(Vec3::ZERO).z);
// The player is less than a step from the centre, 128 blocks from the light.
let centre = at(Vec3::ZERO);
assert!(centre.x.abs() <= 2.0 && centre.y.abs() <= 2.0, "{centre}");
assert!((centre.z + 128.0).abs() < 1e-3, "{centre}");
// Moving within a step keeps the world where it was in the map.
camera.x = 1000.6;
let (moved, _) = shadow_camera(Vec3::Y, camera, &shadow);
let world = Vec3::new(5.0, -3.0, 9.0);
let shift = Vec3::new(0.3, 0.0, 0.0);
assert!(
moved
.transform_point3(world - shift)
.abs_diff_eq(at(world), 1e-3)
);
// shadowDistance blocks reach the map's edge.
let edge = projection.project_point3(Vec3::new(160.0, 0.0, -128.0));
assert!((edge.x.abs() - 1.0).abs() < 1e-4, "{edge}");
}
}
@@ -0,0 +1,264 @@
//! Rain and snow drawn by a shader pack's `gbuffers_weather` (spec 2.3): the game's weather
//! quads as entity vertices, their UV animation applied here rather than in Horizon's weather
//! shader, each kind textured from its own copy of its sprite so that the pack's `texture`
//! repeats it across the column as a standalone rain or snow texture would.
use crate::weather::{Time, Vertex as WeatherVertex};
use super::Vertex;
use super::pack_post::Weather;
/// What `gbuffers_weather` reads from a weather vertex: camera-relative position, the UV in
/// repeats of the sprite, colour (with vanilla's fade) and light. Weather has no normal; the
/// pack sees straight up.
pub fn inputs() -> horizon_shaders::GbuffersInputs {
let declarations =
"layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in vec2 hz_v_uv;\n\
layout(location = 2) in uint hz_v_overlay;\nlayout(location = 3) in vec4 hz_v_color;\n\
layout(location = 4) in uvec4 hz_v_light;\nlayout(location = 5) in vec4 hz_v_normal;\n"
.to_owned();
let body = " hz_Vertex = vec4(hz_v_pos, 1.0);\n \
hz_MultiTexCoord0 = vec4(hz_v_uv, 0.0, 1.0);\n \
hz_MultiTexCoord1 = vec4(float(hz_v_light.y), float(hz_v_light.x), 0.0, 1.0);\n \
hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \
hz_Color = hz_v_color;\n \
hz_Normal = vec3(0.0, 1.0, 0.0);\n"
.to_owned();
let attributes = [
("vaPosition", "hz_Vertex.xyz"),
("vaUV0", "hz_v_uv"),
("vaUV2", "ivec2(hz_MultiTexCoord1.xy)"),
("vaColor", "hz_Color"),
("vaNormal", "hz_Normal"),
];
horizon_shaders::GbuffersInputs {
declarations,
body,
attributes: attributes
.iter()
.map(|&(n, v)| (n.to_owned(), v.to_owned()))
.collect(),
after: String::new(),
// Vanilla's weather drops texels below 0.1, as Horizon's own weather shader does.
alpha_test: Some(horizon_shaders::AlphaTest {
op: ">",
reference: 0.1,
}),
}
}
/// The UV phase of `v` at `time`: `weather_phase` of entity.wgsl, on the CPU.
pub fn phase(v: &WeatherVertex, time: Time) -> [f32; 2] {
let [ticks, fall_ticks, partial, _] = time.values;
let a = v.animation;
if v.snow == 0 {
let t = ticks + a[1] + partial;
return [0.0, (-t / 32.0 * a[0]) % 32.0];
}
let t = ticks + partial;
let du = t * 0.01 * a[2];
let dv = t * a[3] * 0.001;
let base = |lo: u32, hi: u32| f64::from_bits(u64::from(lo) | u64::from(hi) << 32);
let u = (base(v.bases[0], v.bases[1]) + f64::from(du)) as f32;
let v_base = (base(v.bases[2], v.bases[3]) + f64::from(dv)) as f32;
[u, -(fall_ticks + partial) / 512.0 + v_base]
}
/// One kind's sprite copied out of the shared atlas: the sprite's rectangle (bits) and its view.
type Sprite = Option<([u32; 4], wgpu::TextureView)>;
/// The pack's weather vertices and the rain and snow sprites, kept across frames.
pub struct PackWeather {
vertices: wgpu::Buffer,
capacity: usize,
scratch: Vec<Vertex>,
sprites: [Sprite; 2],
/// Bumped when a sprite is copied again.
generation: u64,
}
impl PackWeather {
pub fn new(device: &wgpu::Device) -> Self {
Self {
vertices: Self::buffer(device, 64),
capacity: 64,
scratch: Vec::new(),
sprites: [None, None],
generation: 0,
}
}
fn buffer(device: &wgpu::Device, capacity: usize) -> wgpu::Buffer {
device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Shader pack weather"),
size: (capacity * std::mem::size_of::<Vertex>()) as u64,
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
})
}
/// This frame's rain then snow (the game emits them in that order) as entity vertices, and
/// the sprites they read. `None` when there is none.
pub fn prepare(
&mut self,
device: &wgpu::Device,
encoder: &mut wgpu::CommandEncoder,
uploads: &mut super::upload::Uploads,
atlas: &wgpu::Texture,
weather: &[WeatherVertex],
time: Time,
indices: &wgpu::Buffer,
) -> Option<Weather> {
if weather.is_empty() {
return None;
}
let rain = weather.iter().take_while(|v| v.snow == 0).count() as u32 / 4;
let snow = weather.len() as u32 / 4 - rain;
for (kind, first) in [(0, weather.first()), (1, weather.get(rain as usize * 4))] {
let Some(first) = first.filter(|v| v.snow as usize == kind) else {
continue;
};
let key = first.rect.map(f32::to_bits);
if self.sprites[kind].as_ref().is_none_or(|s| s.0 != key) {
let view = copy_sprite(device, encoder, atlas, first.rect);
self.sprites[kind] = Some((key, view));
self.generation += 1;
}
}
self.scratch.clear();
self.scratch.extend(weather.iter().map(|v| {
let [du, dv] = phase(v, time);
Vertex {
pos: v.pos,
uv: [v.uv[0] + du, v.uv[1] + dv],
layer: 0,
color: v.color,
light: v.light,
normal: [0; 4],
}
}));
if self.scratch.len() > self.capacity {
self.capacity = self.scratch.len().next_power_of_two();
self.vertices = Self::buffer(device, self.capacity);
}
uploads.buffer(
encoder,
&self.vertices,
0,
bytemuck::cast_slice(&self.scratch),
);
let view = |k: usize| self.sprites[k].as_ref().map(|s| s.1.clone());
Some(Weather {
vertices: self.vertices.clone(),
indices: indices.clone(),
quads: [0..rain, rain..rain + snow],
sprites: [view(0), view(1)],
generation: self.generation,
})
}
}
/// A texture holding the atlas's texels under `rect` (normalised x, y, width, height), as
/// Horizon's weather shader reads them (`weather_texel`).
fn copy_sprite(
device: &wgpu::Device,
encoder: &mut wgpu::CommandEncoder,
atlas: &wgpu::Texture,
rect: [f32; 4],
) -> wgpu::TextureView {
let size = [atlas.width() as f32, atlas.height() as f32];
let origin = [0, 1].map(|i| (rect[i] * size[i]).round() as u32);
let extent = [0, 1].map(|i| ((rect[i + 2] * size[i]).round() as u32).max(1));
let extent = [0, 1].map(|i| {
extent[i].min(
[atlas.width(), atlas.height()][i]
.saturating_sub(origin[i])
.max(1),
)
});
let size = wgpu::Extent3d {
width: extent[0],
height: extent[1],
depth_or_array_layers: 1,
};
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("Shader pack weather sprite"),
size,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: atlas.format(),
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
encoder.copy_texture_to_texture(
wgpu::TexelCopyTextureInfo {
texture: atlas,
mip_level: 0,
origin: wgpu::Origin3d {
x: origin[0],
y: origin[1],
z: 0,
},
aspect: wgpu::TextureAspect::All,
},
wgpu::TexelCopyTextureInfo {
texture: &texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
size,
);
texture.create_view(&Default::default())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::weather::Parameters;
/// The CPU phase matches the game's own weather columns (`Parameters::column`), which
/// Horizon's weather shader is tested against.
#[test]
fn phase_matches_the_weather_columns() {
for i in 0..512i32 {
let x = i.wrapping_mul(45_238_971);
let z = i.wrapping_mul(-418_711).wrapping_add(i32::MIN);
let p = Parameters::new(x, z);
let bases = p.snow_base.map(f64::to_bits);
for ticks in [0, 511, 512, 131_071, 131_072, 1_000_003] {
let partial = [0.0, 0.5, 0.999][i as usize % 3];
for snow in [false, true] {
let column = p.column(snow, ticks, x, -64, 320, z, 0, partial);
let v = WeatherVertex {
pos: [0.0; 3],
uv: [0.0; 2],
rect: [0.0; 4],
animation: [
p.rain_speed,
p.rain_tick_offset,
p.snow_drift[0],
p.snow_drift[1],
],
bases: [
bases[0] as u32,
(bases[0] >> 32) as u32,
bases[1] as u32,
(bases[1] >> 32) as u32,
],
color: 0,
light: 0,
snow: u32::from(snow),
};
let [u, v] = phase(&v, Time::new(ticks, partial));
assert_eq!(
[u.to_bits(), v.to_bits()],
[column.u_offset.to_bits(), column.v_offset.to_bits()],
"x {x} z {z} ticks {ticks} snow {snow}"
);
}
}
}
}
}
+5
View File
@@ -77,6 +77,8 @@ pub struct ItemInfo {
pub consumable: Option<horizon_world::consumable::Consumable<String, ConsumeEffect>>,
/// `use_remainder`: what a finished use leaves (`UseRemainder`).
pub use_remainder: Option<String>,
/// `use_cooldown`: the cooldown a use starts (`UseCooldown`).
pub use_cooldown: Option<crate::cooldowns::UseCooldown>,
pub model: String,
/// Main-hand `attack_damage` / `attack_speed` modifier amounts.
pub attack_damage: f32,
@@ -568,6 +570,9 @@ impl Items {
use_remainder: c["minecraft:use_remainder"]["id"]
.as_str()
.map(str::to_owned),
use_cooldown: c
.get("minecraft:use_cooldown")
.and_then(crate::cooldowns::UseCooldown::from_json),
model: c["minecraft:item_model"]
.as_str()
.unwrap_or(name)
+14
View File
@@ -937,6 +937,20 @@ pub fn entity_tag(entity: &BlockEntity) -> Option<Tag> {
match entity {
BlockEntity::Sign(sign) => Some(Tag::compound(sign.encode())),
BlockEntity::Spawner(s) => Some(s.update_tag()),
// SkullBlockEntity: the profile is the whole reason a head differs.
BlockEntity::Skull(s) => {
let mut entries = Vec::new();
if let Some(profile) = &s.profile {
entries.push(("profile", profile.clone()));
}
if let Some(sound) = &s.note_block_sound {
entries.push(("note_block_sound", Tag::String(sound.as_str().into())));
}
if let Some(name) = &s.custom_name {
entries.push(("custom_name", name.clone()));
}
Some(Tag::compound(entries))
}
BlockEntity::Jukebox(_) | BlockEntity::Lectern(_) => None,
_ => None,
}
+3
View File
@@ -46,6 +46,7 @@ pub mod confirm_screen;
pub mod container_screen;
pub mod containers;
pub mod control;
pub mod cooldowns;
pub mod crafting;
pub mod crash;
pub mod creative;
@@ -54,6 +55,8 @@ pub mod datapack;
pub mod debug_chart;
pub mod debug_keys;
pub mod debug_overlay;
pub mod dialog;
pub mod dialog_screen;
pub mod discord_presence;
pub mod dynamic_light;
pub mod effects;
+4
View File
@@ -861,6 +861,9 @@ pub struct Mob {
/// The skin texture of a player mob (`PlayerInfo.getSkin`), which
/// replaces the type's default when its download is ready.
pub skin: Option<String>,
/// A mannequin's `DATA_PROFILE` (ResolvableProfile): the profile's
/// `textures` property resolves to `skin` like a player's.
pub profile: Option<horizon_protocol::entity_data::Profile>,
}
/// Events a mob tick asks the game to act on.
#[derive(Clone, Debug)]
@@ -1586,6 +1589,7 @@ impl Mob {
rider: None,
outbox: Vec::new(),
skin: None,
profile: None,
attrs: Attrs {
speed: kind.speed,
attack_damage: kind.attack_damage,
+8 -2
View File
@@ -664,8 +664,10 @@ fn ai_step(m: &mut Mob, w: &MobWorld, level_random: &mut LegacyRandom, events: &
m.ai.xxa *= 0.98;
m.ai.zza *= 0.98;
// LivingEntity.isImmobile: no goals or input (a biting, stunned or
// roaring ravager).
let immobile = m.species() == Species::Ravager && super::ravager::immobile(m);
// roaring ravager). A mannequin is an Avatar, not a Mob: LivingEntity's
// serverAiStep has no goals, so it only falls and is pushed.
let immobile = (m.species() == Species::Ravager && super::ravager::immobile(m))
|| m.species() == Species::Mannequin;
if m.alive() && !immobile {
server_ai_step(m, w, level_random, events);
} else {
@@ -1018,6 +1020,10 @@ pub fn despawn_check(m: &mut Mob, w: &MobWorld) -> bool {
/// Mob.checkDespawn, before the tick (players are the only despawn
/// reference). Returns false when the mob is discarded.
fn check_despawn(m: &mut Mob, w: &MobWorld) -> bool {
// A mannequin is a LivingEntity, which never despawns.
if m.species() == Species::Mannequin {
return true;
}
let Some(p) = w.nearest_player else {
return true;
};
+52 -9
View File
@@ -69,18 +69,10 @@ pub fn register(m: &mut Mob) {
}
/// Chicken.aiStep: wing flapping, slowed falling and laying eggs.
pub fn chicken_ai_step(m: &mut Mob, _w: &MobWorld, events: &mut Vec<MobEvent>) {
let a = &mut m.anim;
a.flap_o = a.flap;
a.flap_speed_o = a.flap_speed;
a.flap_speed = (a.flap_speed + if m.on_ground { -1.0 } else { 4.0 } * 0.3).clamp(0.0, 1.0);
if !m.on_ground && a.flapping < 1.0 {
a.flapping = 1.0;
}
a.flapping *= 0.9;
chicken_flap(m);
if !m.on_ground && m.velocity.y < 0.0 {
m.velocity.y *= 0.6;
}
a.flap += a.flapping * 2.0;
if m.alive() && !m.is_baby() && !m.extra.jockey {
m.extra.egg_time -= 1;
if m.extra.egg_time <= 0 {
@@ -95,6 +87,21 @@ pub fn chicken_ai_step(m: &mut Mob, _w: &MobWorld, events: &mut Vec<MobEvent>) {
}
}
}
/// Chicken.aiStep's wings: `flapSpeed` rises in the air and falls on the
/// ground, `flapping` restarts at 1 in the air and decays, and `flap` turns
/// by it. It runs on a client too, for the chickens it only displays (the
/// falling slowdown and the eggs are the server's).
pub fn chicken_flap(m: &mut Mob) {
let a = &mut m.anim;
a.flap_o = a.flap;
a.flap_speed_o = a.flap_speed;
a.flap_speed = (a.flap_speed + if m.on_ground { -1.0 } else { 4.0 } * 0.3).clamp(0.0, 1.0);
if !m.on_ground && a.flapping < 1.0 {
a.flapping = 1.0;
}
a.flapping *= 0.9;
a.flap += a.flapping * 2.0;
}
/// Farm animal climate from the biome tags (`spawns_warm/cold_variant_farm_animals`).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum Climate {
@@ -125,3 +132,39 @@ pub fn sheep_color(climate: Climate, random: &mut impl RandomSource) -> u8 {
6
}
}
#[cfg(test)]
mod tests {
use super::*;
use horizon_core::geometry::Vec3;
/// `chicken_flap` reads only the ground flag: any mob type will do.
fn chicken() -> Mob {
Mob::new(1, crate::mobs::sensor_scan_tests::kind(), Vec3::ZERO, 0.0)
}
#[test]
fn a_falling_chicken_flaps_and_a_landed_one_folds_its_wings() {
let mut m = chicken();
m.on_ground = true;
chicken_flap(&mut m);
assert_eq!(m.anim.flap_speed, 0.0);
assert_eq!(m.anim.flap, 0.0);
// In the air flapSpeed rises by 1.2 (clamped to 1), flapping restarts
// at 1 and decays by 0.9, and flap turns by twice flapping.
m.on_ground = false;
chicken_flap(&mut m);
assert_eq!(m.anim.flap_speed, 1.0);
assert_eq!(m.anim.flapping, 0.9);
assert_eq!(m.anim.flap, 1.8);
chicken_flap(&mut m);
assert_eq!(m.anim.flap_o, 1.8);
assert_eq!(m.anim.flap_speed_o, 1.0);
assert_eq!(m.anim.flap, 3.6);
// Landed, flapSpeed falls by 0.3 a tick and flapping decays.
m.on_ground = true;
chicken_flap(&mut m);
assert!((m.anim.flap_speed - 0.7).abs() < 1.0e-6);
assert!((m.anim.flapping - 0.81).abs() < 1.0e-6);
}
}
+9 -4
View File
@@ -92,6 +92,11 @@ fn climate(m: &Mob) -> &'static str {
_ => "temperate",
}
}
/// `AvatarRenderer` entities: a mannequin draws exactly like a player —
/// the player model, its resolved skin and the same scale.
fn avatar(m: &Mob) -> bool {
matches!(&*m.kind.id, "minecraft:player" | "minecraft:mannequin")
}
/// The model layer key and texture for the mob's body.
pub fn body(m: &Mob) -> (String, String) {
use Species as S;
@@ -103,7 +108,7 @@ pub fn body(m: &Mob) -> (String, String) {
s.to_owned()
}
};
if &*m.kind.id == "minecraft:player" {
if avatar(m) {
return (
"main".into(),
m.skin.clone().unwrap_or_else(|| m.kind.texture.clone()),
@@ -334,7 +339,7 @@ pub fn body_color(m: &Mob, alpha: f32) -> u32 {
pub fn layers(m: &Mob) -> Vec<Layer> {
use Species as S;
let mut v = Vec::new();
if &*m.kind.id == "minecraft:player" {
if avatar(m) {
return v;
}
let white = 0xFFFF_FFFF;
@@ -754,7 +759,7 @@ pub fn base_transform_at(m: &Mob, alpha: f32, origin: glam::Mat4) -> glam::Mat4
body += (f64::from(ticks * 3.25).cos() * std::f64::consts::PI * 0.4) as f32;
}
// A sleeping villager, or another player lying in their bed.
let sleep = (m.species() == Species::Villager || m.kind.id == "minecraft:player")
let sleep = (m.species() == Species::Villager || avatar(m))
.then(|| super::villager::sleep_pose(m))
.flatten();
let rotation = match sleep {
@@ -873,7 +878,7 @@ fn red_overlay(m: &Mob) -> bool {
m.hurt_time > 0 || (m.death_time > 0 && !super::creaking::tearing_down(m))
}
fn species_scale(m: &Mob, alpha: f32) -> glam::Mat4 {
if m.kind.id == "minecraft:player" {
if avatar(m) {
// AvatarRenderer.scale, shared by the inventory and world renderer.
return glam::Mat4::from_scale(glam::Vec3::splat(0.9375));
}
+14 -1
View File
@@ -94,6 +94,7 @@ pub enum Species {
Nautilus,
ZombieNautilus,
SulfurCube,
Mannequin,
}
/// Baby bounding boxes (each class's BABY_DIMENSIONS).
#[derive(Clone, Copy, Debug)]
@@ -1307,6 +1308,18 @@ pub const ALL: &[Def] = &[
None,
Baby::None
),
// Mannequin: the player model; the DATA_PROFILE ResolvableProfile picks
// its skin, this texture only while that has none.
def!(
Mannequin,
"mannequin",
"player#main",
"entity/player/wide/steve",
A::Humanoid { zombie_arms: false },
["", "entity.generic.hurt", "entity.generic.death"],
None,
Baby::None
),
];
pub fn def(s: Species) -> &'static Def {
ALL.iter().find(|d| d.species == s).unwrap_or(&ALL[0])
@@ -1488,7 +1501,7 @@ impl Def {
// Nautilus.handleAirSupply: it dries out on land.
needs_water: aquatic || s == S::Nautilus,
water_sensitive: matches!(s, S::Enderman | S::Blaze | S::SnowGolem),
animal: !matches!(s, S::Bat | S::SulfurCube)
animal: !matches!(s, S::Bat | S::SulfurCube | S::Mannequin)
&& !self.hostile_class()
&& !aquatic
&& !matches!(
+104 -17
View File
@@ -167,6 +167,11 @@ pub enum MotionEvent {
InsidePortal {
pos: BlockPos,
},
/// `LayeredCauldronBlock.handleEntityOnFireInside`: a burning player in
/// a water or powder snow cauldron; the server lowers its fill level.
CauldronExtinguish {
pos: BlockPos,
},
/// `WitherRoseBlock.entityInside`: the player's box swept through a
/// wither rose; the server adds its Wither unless peaceful.
InsideWitherRose,
@@ -1277,22 +1282,31 @@ impl MotionWorld<'_, '_, '_> {
continue;
}
let powder_snow = name == "minecraft:powder_snow";
let lava_cauldron = name == "minecraft:lava_cauldron";
// A filled cauldron's content top in blocks: the lava's 15/16
// (LavaCauldronBlock.FILLED_SHAPE), or a layered cauldron's
// getPixelContentHeight, 6 + 3 per level (FILLED_SHAPES).
let content_top = match name {
"minecraft:lava_cauldron" => Some(15.0 / 16.0),
"minecraft:water_cauldron" | "minecraft:powder_snow_cauldron" => {
let level = self
.assets
.registry
.state(state)
.and_then(|s| s.properties.get("level"))
.and_then(|l| l.parse::<u8>().ok())
.unwrap_or(1);
Some((6.0 + 3.0 * f64::from(level)) / 16.0)
}
_ => None,
};
// getEntityInsideCollisionShape is the whole block but for powder
// snow (its collision shape, the whole block when it is empty),
// filled cauldrons, end portals and tripwires; Horizon doesn't
// follow the player into the water and powder snow cauldrons, end
// portals or tripwires yet.
if matches!(
name,
"minecraft:water_cauldron"
| "minecraft:powder_snow_cauldron"
| "minecraft:end_portal"
| "minecraft:tripwire"
) {
// follow the player into those last two.
if matches!(name, "minecraft:end_portal" | "minecraft:tripwire") {
continue;
}
let inside = !(powder_snow || lava_cauldron) || {
let inside = !(powder_snow || content_top.is_some()) || {
let context = self.context();
let binding = self.collision_binding;
let shape = binding.collision_shape_with_context(
@@ -1305,12 +1319,12 @@ impl MotionWorld<'_, '_, '_> {
|| {
let offset = Vec3::new(f64::from(p.x), f64::from(p.y), f64::from(p.z));
let mut boxes: Vec<_> = shape.moved(offset).boxes().to_vec();
if lava_cauldron {
// LavaCauldronBlock.FILLED_SHAPE: the cauldron's
// shape or its lava, Block.column(12, 4, 15).
if let Some(top) = content_top {
// The cauldron's shape or its content,
// Block.column(12, 4, top).
boxes.push(Aabb::new(
Vec3::new(2.0 / 16.0, 4.0 / 16.0, 2.0 / 16.0).add_vector(offset),
Vec3::new(14.0 / 16.0, 15.0 / 16.0, 14.0 / 16.0).add_vector(offset),
Vec3::new(14.0 / 16.0, top, 14.0 / 16.0).add_vector(offset),
));
}
self.player
@@ -1320,11 +1334,15 @@ impl MotionWorld<'_, '_, '_> {
}
};
if inside && visited.insert(p.pack()) {
// CactusBlock, CampfireBlock and LavaCauldronBlock.entityInside
// (`entity.hurt`), and WitherRoseBlock's server-side effect.
// CactusBlock, CampfireBlock and the cauldrons' entityInside
// (`entity.hurt`, the fire and the fill level), and
// WitherRoseBlock's server-side effect.
match name {
"minecraft:cactus" => self.environment_hurt("minecraft:cactus", 1.0)?,
"minecraft:lava_cauldron" => self.lava_cauldron_inside()?,
"minecraft:water_cauldron" | "minecraft:powder_snow_cauldron" => {
self.layered_cauldron_inside(p);
}
"minecraft:wither_rose" => {
self.player.events.push(MotionEvent::InsideWitherRose);
}
@@ -1395,6 +1413,18 @@ impl MotionWorld<'_, '_, '_> {
self.player.ignite_for_ticks(300)?;
self.environment_hurt("minecraft:lava", 4.0)
}
/// `LayeredCauldronBlock.entityInside`: on a server, a burning player
/// asks for the cauldron's `handleEntityOnFireInside` (runBefore
/// EXTINGUISH, so while still burning), then EXTINGUISH
/// (`Entity.clearFire`) puts the fire out.
fn layered_cauldron_inside(&mut self, pos: BlockPos) {
if self.player.fire_ticks > 0 && !self.player.spectator {
self.player
.events
.push(MotionEvent::CauldronExtinguish { pos });
}
self.player.fire_ticks = self.player.fire_ticks.min(0);
}
/// `WebBlock.entityInside`: stuck at a quarter of the speed, half with
/// the Weaving effect.
fn web_inside(&mut self) {
@@ -2457,6 +2487,63 @@ mod tests {
assert!(away.fire_ticks <= 0);
}
#[test]
fn a_water_cauldron_puts_out_a_burning_player() {
let root = std::env::var_os("HORIZON_TEST_RESOURCES")
.map(std::path::PathBuf::from)
.unwrap_or_else(|| {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../.local/horizon-client")
.join(horizon_core::VANILLA_VERSION)
});
if !root.join("movement-properties.nbt").exists() {
return;
}
let assets = Assets::load(&root).unwrap();
let render = assets.reload(&root).unwrap();
let binding = crate::collisions::load(&root, assets.registry).unwrap();
let resources = MotionResources::load(&root, &assets).unwrap();
let mut scene = World::test_scene(&assets, &render, 1).unwrap();
let water = assets
.registry
.resolve(&horizon_world::chunk::BlockState {
name: "minecraft:water_cauldron".into(),
properties: [("level".to_owned(), "3".to_owned())].into(),
})
.unwrap();
scene.set(20, 4, 20, water).unwrap();
let mut tick = |player: &mut PlayerMotion| {
MotionWorld {
entities: &[],
player,
scene: &mut scene,
assets: &assets,
collision_binding: &binding,
resources: &resources,
}
.controlled_simulation_tick(Default::default())
.unwrap();
};
let asks = |player: &PlayerMotion| {
player
.events
.iter()
.filter(|e| matches!(e, MotionEvent::CauldronExtinguish { .. }))
.count()
};
// Burning, on the cauldron's floor in its water: put out, and the
// server is asked to lower the water.
let mut burning = PlayerMotion::new(&resources, Vec3::new(20.5, 4.25, 20.5)).unwrap();
burning.fire_ticks = 100;
tick(&mut burning);
assert!(burning.fire_ticks <= 0, "the water must put the fire out");
assert_eq!(asks(&burning), 1);
// Not burning: nothing to lower.
let mut dry = PlayerMotion::new(&resources, Vec3::new(20.5, 4.25, 20.5)).unwrap();
tick(&mut dry);
assert_eq!(asks(&dry), 0);
}
#[test]
fn standing_in_a_wither_rose_asks_for_its_effect() {
let root = std::env::var_os("HORIZON_TEST_RESOURCES")
+30
View File
@@ -55,6 +55,10 @@ pub enum Event {
PackPush(cfg::ResourcePackPush),
/// ClientboundResourcePackPopPacket: one pack, or every pack.
PackPop(Option<u128>),
/// `show_dialog` in configuration: the dialog itself.
ShowDialog(horizon_world::nbt::Tag),
/// `clear_dialog` in configuration.
ClearDialog,
/// `ClientboundTransferPacket`: the server sent us to another address.
/// The connection ends right after; the game reconnects there with the
/// stored cookies.
@@ -234,6 +238,25 @@ impl Session {
w.shutdown();
}
}
/// ServerboundCustomClickActionPacket (a dialog's answer), in whichever
/// phase the connection is.
pub fn send_custom_click(&self, id: &str, payload: Option<horizon_world::nbt::Tag>) {
let click = cfg::CustomClickAction {
id: id.to_owned(),
payload,
};
let mut writer = self.writer.lock().unwrap();
let body = if self.configuring.load(Ordering::Relaxed) {
cfg::Serverbound::CustomClickAction(click).encode()
} else {
play::Serverbound::CustomClickAction(click).encode()
};
if let Some(w) = writer.as_mut()
&& w.send(&body).is_err()
{
w.shutdown();
}
}
pub fn disconnect(&mut self) {
if let Some(w) = self.writer.lock().unwrap().take() {
w.shutdown();
@@ -602,6 +625,13 @@ fn configure(
cfg::Clientbound::ResourcePackPop(p) => {
let _ = send.send(Event::PackPop(p.id));
}
// The game shows it; the answer goes back from there.
cfg::Clientbound::ShowDialog(d) => {
let _ = send.send(Event::ShowDialog(d.dialog));
}
cfg::Clientbound::ClearDialog(_) => {
let _ = send.send(Event::ClearDialog);
}
cfg::Clientbound::CookieRequest(q) => {
let payload = cookies.lock().unwrap().get(&q.key).cloned();
reply(cfg::Serverbound::CookieResponse(cfg::CookieResponse {
+3
View File
@@ -831,6 +831,7 @@ impl PackScreen {
d::label(&mut out, &lines, mx, my, self.width - 50, true);
for ((bx, by), key) in buttons.into_iter().zip(["gui.yes", "gui.no"]) {
let hover = d::inside(mouse, bx, by, 150, 20);
crate::widgets::widget_cursor(hover, true);
d::button(&mut out, m, &tr(key), bx, by, 150, 20, true, hover);
}
return out;
@@ -856,6 +857,8 @@ impl PackScreen {
} else {
"gui.done"
});
// The cursor follows the mouse; the highlight also keyboard focus.
crate::widgets::widget_cursor(hover, true);
d::button(
&mut out,
m,
+73 -2
View File
@@ -8,7 +8,8 @@ use std::io::Read;
use std::{collections::BTreeSet, fs, io, path::Path, path::PathBuf};
// Bump when overlay/merge semantics change. Markers are disposable cache data.
const VERSION: u32 = 3;
// Version 3 could omit untouched vanilla directories on Windows (#1649, #1688).
const VERSION: u32 = 4;
#[derive(Default, Serialize, Deserialize)]
pub(super) struct Dependencies {
@@ -421,6 +422,73 @@ mod tests {
out
}
#[test]
#[cfg(not(unix))]
fn texture_only_packs_rebuild_incomplete_windows_v3_overlays() {
let f = Fixture::new();
let blockstate = "assets/minecraft/blockstates/oak_stairs.json";
let model = "assets/minecraft/models/block/oak_stairs.json";
let texture = "assets/minecraft/textures/block/stone.png";
let font = "assets/minecraft/textures/font/ascii.png";
f.write(&format!("vanilla/{blockstate}"), br#"{"variants":{}}"#);
f.write(&format!("vanilla/{model}"), br#"{"elements":[]}"#);
f.write(&format!("vanilla/{texture}"), b"vanilla texture");
f.write(&format!("vanilla/{font}"), b"vanilla font");
let folder = f.dir_pack("textures");
f.write(&format!("textures/{texture}"), b"pack texture");
f.write(
"textures.zip",
&archive(
&[
(
"pack.mcmeta",
br#"{"pack":{"description":"test","pack_format":97}}"#.to_vec(),
),
(texture, b"pack texture".to_vec()),
],
true,
),
);
let zip = packs::pack(
"zip".into(),
Text::empty(),
Source::Zip(f.0.join("textures.zip")),
)
.unwrap();
let vanilla = f.0.join("vanilla");
for pack in [folder, zip] {
let out = f.0.join(format!("cached-{}", pack.id));
let selected = std::slice::from_ref(&pack);
packs::build_overlay(&vanilla, selected, &out).unwrap();
let check = || {
for name in [blockstate, model, font] {
assert_eq!(
fs::read(out.join(name)).unwrap(),
fs::read(vanilla.join(name)).unwrap()
);
}
assert_eq!(fs::read(out.join(texture)).unwrap(), b"pack texture");
};
check();
// Before #1688, Windows silently omitted untouched directories.
// A v3 marker still validates because it checks only source files.
let marker_path = marker_path(&out);
let mut marker: Marker =
serde_json::from_slice(&fs::read(&marker_path).unwrap()).unwrap();
marker.version = 3;
for dir in ["blockstates", "models", "textures/font"] {
fs::remove_dir_all(out.join("assets/minecraft").join(dir)).unwrap();
}
fs::write(&marker_path, serde_json::to_vec(&marker).unwrap()).unwrap();
assert!(marker.dependencies.valid());
packs::build_overlay(&vanilla, selected, &out).unwrap();
check();
// The repaired overlay also remains complete on a cache hit.
packs::build_overlay(&vanilla, selected, &out).unwrap();
check();
}
}
#[test]
fn directory_inputs_config_and_owned_output_recovery() {
let f = Fixture::new();
@@ -456,7 +524,10 @@ mod tests {
let path = f.0.join(format!("high/{resource}"));
let original_time = fs::metadata(&path).unwrap().modified().unwrap();
fs::write(&path, br#"{"p":"c"}"#).unwrap();
fs::File::open(path)
// Windows requires a writable handle to restore the timestamp.
fs::OpenOptions::new()
.write(true)
.open(path)
.unwrap()
.set_times(fs::FileTimes::new().set_modified(original_time))
.unwrap();
+33 -13
View File
@@ -572,6 +572,23 @@ impl Projectile {
}
(self.position.y > -128.0, Hit::None)
}
/// AbstractHurtingProjectile.applyInertia, on both server and client.
pub fn apply_hurting_inertia(&mut self, water: bool) {
// WitherSkull.getInertia: 0.73 for dangerous skulls.
let inertia = match self.kind {
ProjectileKind::WitherSkull { dangerous: true } => 0.73,
_ if water => 0.8,
_ => 0.95,
};
let v = self.velocity;
let len = (v.x * v.x + v.y * v.y + v.z * v.z).sqrt().max(1.0e-9);
self.velocity = Vec3::new(
(v.x + v.x / len * self.acceleration) * inertia,
(v.y + v.y / len * self.acceleration) * inertia,
(v.z + v.z / len * self.acceleration) * inertia,
);
}
/// One tick: returns whether to keep the projectile and what it hit.
pub fn tick(&mut self, w: &impl ProjectileWorld) -> (bool, Hit) {
self.previous = self.position;
@@ -584,19 +601,7 @@ impl Projectile {
ProjectileKind::SmallFireball
| ProjectileKind::LargeFireball { .. }
| ProjectileKind::WitherSkull { .. } => {
// WitherSkull.getInertia: 0.73 for dangerous skulls.
let inertia = match self.kind {
ProjectileKind::WitherSkull { dangerous: true } => 0.73,
_ if water => 0.8,
_ => 0.95,
};
let v = self.velocity;
let len = (v.x * v.x + v.y * v.y + v.z * v.z).sqrt().max(1.0e-9);
self.velocity = Vec3::new(
(v.x + v.x / len * self.acceleration) * inertia,
(v.y + v.y / len * self.acceleration) * inertia,
(v.z + v.z / len * self.acceleration) * inertia,
);
self.apply_hurting_inertia(water);
}
// Arrows tick with `arrows::tick`.
ProjectileKind::Arrow { .. } => {}
@@ -819,6 +824,21 @@ mod tests {
// (0.1 + 0.1) * 0.95
assert!((p.velocity.x - 0.19).abs() < 1e-9);
}
#[test]
fn fireball_inertia_preserves_custom_acceleration() {
let mut p = Projectile::fireball(
ProjectileKind::SmallFireball,
Vec3::new(0.0, 100.0, 0.0),
None,
[1.0, 0.0, 0.0],
);
// A redirected projectile can carry a different acceleration power.
p.acceleration = 0.2;
p.tick(&Empty(vec![]));
assert!((p.velocity.x - 0.285).abs() < 1e-9);
assert!((p.position.x - 0.285).abs() < 1e-9);
}
}
#[cfg(test)]
+85 -12
View File
@@ -553,6 +553,17 @@ pub fn encode_block_entity(
entries.push(("creaking", uuid_tag(uuid)));
}
}
BlockEntity::Skull(s) => {
if let Some(profile) = &s.profile {
entries.push(("profile", profile.clone()));
}
if let Some(sound) = &s.note_block_sound {
entries.push(("note_block_sound", string(sound)));
}
if let Some(name) = &s.custom_name {
entries.push(("custom_name", name.clone()));
}
}
BlockEntity::Spawner(s) => entries.extend(s.save()),
BlockEntity::TrialSpawner(s) => entries.extend(s.save()),
BlockEntity::Vault(v) => entries.extend(v.save()),
@@ -572,6 +583,17 @@ fn merge_block_entity(original: &Tag, loaded: &Tag, current: &Tag) -> Tag {
}
merged
}
/// `BannerBlockEntity.loadAdditional` (and the `components` a block entity keeps).
pub fn decode_banner(tag: &Tag) -> crate::containers::Banner {
crate::containers::Banner {
layers: crate::banner::layers_from_nbt(tag.get("patterns")),
name: text(tag.get("CustomName")),
components: match tag.get("components") {
Some(Tag::Compound(c)) => c.clone(),
_ => Compound::new(),
},
}
}
pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::containers::BlockEntity)> {
use crate::containers::BlockEntity;
let int = |k: &str| number(tag.get(k)).map(|v| v as i32);
@@ -605,17 +627,7 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain
return Some((pos, BlockEntity::Lectern(lectern)));
}
if id == "minecraft:banner" {
return Some((
pos,
BlockEntity::Banner(crate::containers::Banner {
layers: crate::banner::layers_from_nbt(tag.get("patterns")),
name: text(tag.get("CustomName")),
components: match tag.get("components") {
Some(Tag::Compound(c)) => c.clone(),
_ => Compound::new(),
},
}),
));
return Some((pos, BlockEntity::Banner(decode_banner(tag))));
}
if id == "minecraft:beacon" {
let beacon = crate::containers::Beacon {
@@ -627,6 +639,18 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain
};
return Some((pos, BlockEntity::Beacon(Box::new(beacon))));
}
// A skull's block entity id is not its block's name (every head block).
if id == "minecraft:skull" {
return Some((
pos,
BlockEntity::Skull(crate::containers::Skull::new(
tag.get("profile").cloned(),
text(tag.get("note_block_sound")),
tag.get("custom_name")
.and_then(crate::block_entity_nbt::component),
)),
));
}
// A spawner's block entity id is not its block's name.
let block = match id.as_str() {
"minecraft:mob_spawner" => "minecraft:spawner",
@@ -699,7 +723,8 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain
| BlockEntity::Beacon(_)
| BlockEntity::Lectern(_)
| BlockEntity::Banner(_)
| BlockEntity::CreakingHeart(_) => {}
| BlockEntity::CreakingHeart(_)
| BlockEntity::Skull(_) => {}
BlockEntity::Spawner(s) => **s = crate::mobs::spawner::Spawner::load(tag),
BlockEntity::TrialSpawner(s) => **s = crate::mobs::trial_spawner::TrialSpawner::load(tag),
BlockEntity::Vault(v) => **v = crate::vault::Vault::load(tag),
@@ -3892,6 +3917,54 @@ mod tests {
}
use super::*;
#[test]
fn skull_block_entity_keeps_its_profile() {
// A placed `player_head` as vanilla saves it: the owning player's
// `profile` survives load, encode and `canonical`.
let saved = horizon_world::nbt::parse_snbt(
r#"{id:"minecraft:skull",x:10,y:64,z:20,profile:{name:"Steve",id:[I;1,2,3,4],properties:[{name:"textures",value:"abc"}]},note_block_sound:"minecraft:entity.player.hurt",custom_name:"Trophy"}"#,
)
.unwrap()
.0;
let (pos, back) = decode_block_entity(&saved).expect("skull");
assert_eq!(pos, (10, 64, 20));
let crate::containers::BlockEntity::Skull(skull) = &back else {
panic!("not a skull");
};
assert_eq!(skull.profile, saved.get("profile").cloned());
assert_eq!(
skull.note_block_sound.as_deref(),
Some("minecraft:entity.player.hurt")
);
assert_eq!(back.id(), "minecraft:skull");
let tag = encode_block_entity(pos, &back, "minecraft:player_head");
assert_eq!(
crate::block_entity_nbt::id(&tag).as_deref(),
Some("minecraft:skull")
);
assert_eq!(tag.get("profile"), saved.get("profile"));
let (_, again) = decode_block_entity(&tag).expect("skull again");
let crate::containers::BlockEntity::Skull(again) = again else {
panic!("not a skull");
};
assert_eq!(again.profile, skull.profile);
// A profile-less head (skeleton, creeper) round-trips too.
let plain = encode_block_entity(
pos,
&crate::containers::BlockEntity::Skull(crate::containers::Skull::default()),
"minecraft:creeper_head",
);
let (_, decoded) = decode_block_entity(&plain).expect("plain skull");
assert!(matches!(decoded, crate::containers::BlockEntity::Skull(_)));
// The heads and skulls have one; the piston's head is no skull.
use crate::containers::BlockEntity;
assert!(matches!(
BlockEntity::for_block("minecraft:player_wall_head"),
Some(BlockEntity::Skull(_))
));
assert!(BlockEntity::for_block("minecraft:piston_head").is_none());
}
#[test]
fn uuid_text_round_trips() {
let u = [-158327916, 1572358581, -2125825222, -487948715];
+7
View File
@@ -413,6 +413,13 @@ impl SculkEntity {
spreader: SculkSpreader::default(),
}
}
/// Whether its block entity tick does nothing: no vibration chosen
/// or on its way, none waiting to be chosen, and no charge to spread.
pub fn idle(&self) -> bool {
self.vibration.current.is_none()
&& self.vibration.selector.current().is_none()
&& self.spreader.cursors.is_empty()
}
pub fn for_block(name: &str) -> Option<Self> {
Some(Self::new(match name {
"minecraft:sculk_sensor" => SculkKind::Sensor,
+77
View File
@@ -130,6 +130,47 @@ pub fn profile_skin_url(profile: &serde_json::Value) -> Option<String> {
unpack(value, Signature::Unsigned, &TEXTURE_URIS).skin
}
/// `profile_skin_url` on a block entity's `profile` NBT (`SkullBlockEntity`):
/// the same `properties` list, held as a compound. A name-only profile (a
/// string tag) has no properties and no skin URL.
pub fn profile_skin_url_nbt(profile: &horizon_world::nbt::Tag) -> Option<String> {
let properties = match profile.get("properties")? {
horizon_world::nbt::Tag::List(list) => list,
_ => return None,
};
let value = properties.iter().find_map(|p| {
let name = nbt_text(p.get("name"))?;
(name == "textures")
.then(|| nbt_text(p.get("value")))
.flatten()
})?;
unpack(&value, Signature::Unsigned, &TEXTURE_URIS).skin
}
/// The profile's uuid as u128: `id` as an int array (block entity NBT) or a
/// uuid string (older and component forms). Used for `DefaultPlayerSkin.get`
/// when a head's skin is not downloaded yet.
pub fn profile_uuid_nbt(profile: &horizon_world::nbt::Tag) -> Option<u128> {
match profile.get("id")? {
horizon_world::nbt::Tag::IntArray(v) if v.len() == 4 => Some(
v.iter()
.fold(0u128, |a, &w| (a << 32) | u128::from(w as u32)),
),
horizon_world::nbt::Tag::String(s) => {
let digits: String = s.to_string_lossy().chars().filter(|c| *c != '-').collect();
u128::from_str_radix(&digits, 16).ok()
}
_ => None,
}
}
fn nbt_text(tag: Option<&horizon_world::nbt::Tag>) -> Option<String> {
match tag {
Some(horizon_world::nbt::Tag::String(s)) => Some(s.to_string_lossy()),
_ => None,
}
}
/// `MinecraftProfileTexture.getHash`: the base name of the URL's path.
pub fn texture_hash(url: &str) -> Option<String> {
let rest = url.split_once("://")?.1;
@@ -457,6 +498,42 @@ mod tests {
use super::*;
#[test]
fn skull_block_entity_profiles_supply_head_skins() {
let value = "eyJ0ZXh0dXJlcyI6IHsiU0tJTiI6IHsidXJsIjogImh0dHBzOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlL2FiYzEyMyJ9fX0=";
let profile = horizon_world::nbt::parse_snbt(&format!(
r#"{{name:"T",id:[I;-158327916,1572358581,-2125825222,-487948715],properties:[{{name:"textures",value:"{value}"}}]}}"#
))
.unwrap()
.0;
assert_eq!(
super::profile_skin_url_nbt(&profile).as_deref(),
Some("https://textures.minecraft.net/texture/abc123")
);
assert_eq!(
super::profile_uuid_nbt(&profile),
Some(
[-158327916, 1572358581, -2125825222, -487948715]
.iter()
.fold(0u128, |a, &w: &i32| (a << 32) | u128::from(w as u32))
)
);
// A name-only profile (a string tag) has no properties: no url, no id.
let named = horizon_world::nbt::Tag::String("MHF_Blaze".into());
assert_eq!(super::profile_skin_url_nbt(&named), None);
assert_eq!(super::profile_uuid_nbt(&named), None);
// A uuid in string form decodes too.
let text_id = horizon_world::nbt::parse_snbt(
r#"{name:"T",id:"e843a1f4-0000-0001-0000-000200000003"}"#,
)
.unwrap()
.0;
assert_eq!(
super::profile_uuid_nbt(&text_id),
Some(u128::from_str_radix("e843a1f4000000010000000200000003", 16).unwrap())
);
}
#[test]
fn sha1_and_base64() {
let hex: String = sha1(b"abc").iter().map(|b| format!("{b:02x}")).collect();
+105 -20
View File
@@ -128,6 +128,27 @@ fn text(nbt: &Compound, key: &str) -> Option<String> {
}
}
/// The client block entity for one of a chunk's generated block entities,
/// before a container's loot is unpacked: `StructureTemplate.placeInWorld`
/// hands the template's block entity NBT to the level's block entity.
fn generated_block_entity(block: &str, nbt: &Compound) -> Option<BlockEntity> {
let mut entity = BlockEntity::for_block(block)?;
match &mut entity {
// MonsterRoomFeature and the structures' setEntityId.
BlockEntity::Spawner(s) => {
**s = crate::mobs::spawner::Spawner::load(&Tag::Compound(nbt.clone()));
}
BlockEntity::TrialSpawner(s) => {
**s = crate::mobs::trial_spawner::TrialSpawner::load(&Tag::Compound(nbt.clone()));
}
BlockEntity::Vault(v) => **v = crate::vault::Vault::load(&Tag::Compound(nbt.clone())),
// BannerBlockEntity.loadAdditional: the End cities' black triangles.
BlockEntity::Banner(b) => *b = crate::save::decode_banner(&Tag::Compound(nbt.clone())),
_ => {}
}
Some(entity)
}
/// Client block entities for the generated block entities of a chunk.
pub fn block_entities(
chunk: &ProtoChunk,
@@ -137,19 +158,9 @@ pub fn block_entities(
for (pos, nbt) in &chunk.block_entities {
let [x, y, z] = *pos;
let block = chunk.block_name(x, y, z);
let Some(mut entity) = BlockEntity::for_block(block) else {
let Some(mut entity) = generated_block_entity(block, nbt) else {
continue;
};
// MonsterRoomFeature and the structures' setEntityId.
if let BlockEntity::Spawner(s) = &mut entity {
**s = crate::mobs::spawner::Spawner::load(&Tag::Compound(nbt.clone()));
}
if let BlockEntity::TrialSpawner(s) = &mut entity {
**s = crate::mobs::trial_spawner::TrialSpawner::load(&Tag::Compound(nbt.clone()));
}
if let BlockEntity::Vault(v) = &mut entity {
**v = crate::vault::Vault::load(&Tag::Compound(nbt.clone()));
}
if let (BlockEntity::Chest(slots), Some(table), Some(loot)) =
(&mut entity, text(nbt, "LootTable"), loot)
{
@@ -165,14 +176,10 @@ pub fn block_entities(
.iter_mut()
.zip(loot.fill(&table, seed, 27, Some(biome)))
{
*slot = stack.map(|s| {
let mut item = loot_item(&s);
// An ominous bottle's level (the default, 0, is no component).
if let Some(level) = s.ominous_amplifier.filter(|l| *l > 0) {
item.data_mut().ominous_amplifier = Some(level.clamp(0, 4) as u8);
}
item
});
// With what the rolls put on them: enchantments
// (enchant_with_levels in End city chests), potions and an
// ominous bottle's level.
*slot = stack.map(|s| loot_item_with_components(&s));
}
}
out.push(((x, y, z), entity));
@@ -211,7 +218,7 @@ pub fn unpack_entity_loot(
let slots: Vec<Option<ItemStack>> = loot
.fill(&table, seed, 27, Some(biome))
.into_iter()
.map(|s| s.map(|s| loot_item(&s)))
.map(|s| s.map(|s| loot_item_with_components(&s)))
.collect();
nbt.remove(&JavaString::from("LootTable"));
nbt.remove(&JavaString::from("LootTableSeed"));
@@ -224,6 +231,45 @@ mod tests {
use super::*;
use horizon_world::loot::{ExplorationMap, LootStack};
#[test]
fn placed_chest_loot_keeps_its_enchantments_and_potions() {
let stack =
|item: &str, enchantments: Vec<(String, i32)>, potion: Option<&str>| LootStack {
item: item.into(),
count: 1,
damage: 0,
enchantments,
potion: potion.map(str::to_owned),
map: false,
exploration: None,
ominous_amplifier: None,
};
// An End city chest's enchant_with_levels roll.
let boots = loot_item_with_components(&stack(
"minecraft:diamond_boots",
vec![
("minecraft:protection".into(), 3),
("minecraft:unbreaking".into(), 2),
],
None,
));
assert_eq!(boots.enchantments().len(), 2);
// A book's go into stored_enchantments.
let book = loot_item_with_components(&stack(
"minecraft:enchanted_book",
vec![("minecraft:mending".into(), 1)],
None,
));
assert!(book.enchantments().is_empty());
assert_eq!(book.stored_enchantments().len(), 1);
let arrow = loot_item_with_components(&stack(
"minecraft:tipped_arrow",
Vec::new(),
Some("minecraft:slowness"),
));
assert_eq!(arrow.potion(), Some("minecraft:slowness"));
}
#[test]
fn exploration_maps_wait_in_custom_data_until_searched() {
let mut stack = LootStack {
@@ -278,4 +324,43 @@ mod tests {
stack.exploration = None;
assert!(!has_pending_map(&loot_item(&stack)));
}
/// The End cities' tower tops save their banners' patterns in the
/// template; the placed banner must keep them (`EndCityPieces`).
#[test]
fn a_generated_banner_keeps_its_template_patterns() {
let patterns = Tag::List(vec![
Tag::compound([
("color", Tag::String("black".into())),
("pattern", Tag::String("minecraft:triangle_top".into())),
]),
Tag::compound([
("color", Tag::String("black".into())),
("pattern", Tag::String("minecraft:triangle_bottom".into())),
]),
]);
let nbt = Tag::compound([
("id", Tag::String("minecraft:banner".into())),
("patterns", patterns),
]);
let Tag::Compound(nbt) = nbt else {
unreachable!()
};
let Some(BlockEntity::Banner(banner)) =
generated_block_entity("minecraft:magenta_banner", &nbt)
else {
panic!("a banner block makes a banner block entity");
};
assert_eq!(banner.layers.len(), 2);
assert_eq!(&*banner.layers[0].pattern, "minecraft:triangle_top");
assert_eq!(&*banner.layers[1].pattern, "minecraft:triangle_bottom");
assert_eq!(&*banner.layers[0].color, "black");
// A banner the template saved without patterns stays bare.
let Some(BlockEntity::Banner(bare)) =
generated_block_entity("minecraft:white_banner", &Compound::new())
else {
panic!("a banner block makes a banner block entity");
};
assert!(bare.layers.is_empty());
}
}
+1
View File
@@ -317,6 +317,7 @@ pub fn button(
hover: bool,
) {
crate::widgets::record(&label.plain(), [x, y, w, h], active);
crate::widgets::widget_cursor(hover, active);
let sprite = if !active {
"gui/sprites/widget/button_disabled"
} else if hover {
+56 -2
View File
@@ -41,6 +41,10 @@ pub struct VanillaTerrain {
tick_states: Vec<u32>,
seed: i64,
dimension: String,
/// When the last proto chunk was generated, in milliseconds since
/// `created`: the ring search waits for a pause in generation.
last_proto: std::sync::atomic::AtomicU64,
created: std::time::Instant,
}
/// A proto chunk, or the position of one whose generation failed.
enum VanillaProto {
@@ -139,6 +143,8 @@ impl VanillaTerrain {
tick_states,
seed,
dimension: dimension.to_owned(),
last_proto: std::sync::atomic::AtomicU64::new(0),
created: std::time::Instant::now(),
})
}
/// A proto chunk's blocks, biomes, scheduled ticks and post-processing
@@ -272,9 +278,53 @@ impl VanillaTerrain {
pub fn origin(&self) -> crate::Result<ChunkPos> {
Ok(self.generator.origin()?)
}
/// Concentric ring positions of a structure set (strongholds), once known.
/// Concentric ring positions of a structure set (strongholds), searched
/// for now if no chunk needed them yet.
pub fn ring_positions(&self, set: &str) -> Option<Vec<ChunkPos>> {
self.generator.ring_positions(set)
let mut worker = self.query.lock().unwrap_or_else(|e| e.into_inner());
let worker = worker.get_or_insert_with(|| self.generator.worker());
worker.ring_positions(set)
}
/// Searches the concentric rings on a thread of their own, as vanilla's
/// background executor does, once generation pauses for two seconds
/// (at the latest a minute after the world opens) and in the idle
/// class: on a laptop's few cores, sharing them with the first chunks,
/// which don't need the rings, made those 3 s slower. An eye of ender
/// thrown later finds the rings ready, and a chunk or a query that
/// needs them first shares the remaining searches with this thread.
pub fn search_rings_behind(self: &Arc<Self>) {
const QUIET: u64 = 2_000;
const LATEST: u64 = 60_000;
let terrain = Arc::downgrade(self);
let spawned = std::thread::Builder::new()
.name("Ring search".into())
.spawn(move || {
horizon_threads::idle_current();
loop {
std::thread::sleep(std::time::Duration::from_millis(500));
// The world closed: nothing to search for.
let Some(terrain) = terrain.upgrade() else {
return;
};
let now = terrain.elapsed_ms();
let last = terrain
.last_proto
.load(std::sync::atomic::Ordering::Relaxed);
if now >= LATEST || (last > 0 && now - last >= QUIET) {
let _ = terrain
.generator
.worker()
.ring_positions("minecraft:strongholds");
return;
}
}
});
if let Err(e) = spawned {
log::warn!("ring search thread: {e}");
}
}
fn elapsed_ms(&self) -> u64 {
u64::try_from(self.created.elapsed().as_millis()).unwrap_or(u64::MAX)
}
}
/// The worldgen bundle at `dir`, loaded once and shared by every generator
@@ -323,6 +373,10 @@ impl ChunkGenerator for VanillaTerrain {
}
}
fn proto(&self, worker: &mut GeneratorWorker, x: i32, z: i32) -> Proto {
self.last_proto.store(
self.elapsed_ms().max(1),
std::sync::atomic::Ordering::Relaxed,
);
match worker.generate_proto(ChunkPos::new(x, z)) {
Ok(chunk) => Box::new(VanillaProto::Ready(Box::new(chunk))),
Err(e) => {
+64 -2
View File
@@ -1,8 +1,47 @@
//! The labelled widgets and focus targets drawn in the last frame, for the
//! control socket: a script clicks a button by its label (`horizon-ctl
//! click-button Respawn`) or any widget by its id in [`tree`] (`horizon-ctl
//! click done`), whatever screen drew it.
use std::cell::RefCell;
//! click done`), whatever screen drew it. Also the mouse cursor the widget
//! under the mouse asks for.
use std::cell::{Cell, RefCell};
/// The mouse cursor a frame's widgets asked for (`CursorTypes`; the others
/// aren't asked for yet).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Cursor {
#[default]
Arrow,
PointingHand,
NotAllowed,
}
thread_local! {
/// `GuiGraphicsExtractor.requestCursor`: the last request since [`take_cursor`].
static CURSOR: Cell<Cursor> = const { Cell::new(Cursor::Arrow) };
}
/// `GuiGraphicsExtractor.requestCursor`: the last request of a frame wins.
pub fn request_cursor(cursor: Cursor) {
CURSOR.set(cursor);
}
/// `AbstractWidget.handleCursor`: a hovered widget asks for the pointing
/// hand, or for not-allowed when it is inactive.
pub fn widget_cursor(hovered: bool, active: bool) {
if hovered {
request_cursor(if active {
Cursor::PointingHand
} else {
Cursor::NotAllowed
});
}
}
/// The cursor the frame asked for, and back to the arrow for the next one
/// (the window shows the arrow unless a widget asks).
pub fn take_cursor() -> Cursor {
CURSOR.replace(Cursor::Arrow)
}
/// A labelled widget in GUI pixels (the coordinates screens take input in).
#[derive(Clone, Debug)]
@@ -133,6 +172,29 @@ pub fn record_as(label: &str, [x, y, w, h]: [f32; 4], enabled: bool, kind: Kind)
});
}
#[cfg(test)]
mod cursor_tests {
use super::*;
#[test]
fn hovered_widgets_ask_for_the_hand_or_not_allowed() {
let _ = take_cursor();
widget_cursor(false, true);
assert_eq!(take_cursor(), Cursor::Arrow);
widget_cursor(true, true);
assert_eq!(take_cursor(), Cursor::PointingHand);
// Taken: the next frame starts from the arrow.
assert_eq!(take_cursor(), Cursor::Arrow);
widget_cursor(true, false);
assert_eq!(take_cursor(), Cursor::NotAllowed);
// The last request of a frame wins.
widget_cursor(true, false);
widget_cursor(false, true);
widget_cursor(true, true);
assert_eq!(take_cursor(), Cursor::PointingHand);
}
}
/// Once per event loop turn with the count of rendered frames: when a new
/// frame was drawn since the last call, its buttons become the last frame's.
pub fn end_frame(frames: u64) {
+1 -1
View File
@@ -7,7 +7,7 @@ rust-version = "1.85"
description = "Write Horizon mods in Rust: WebAssembly components for the horizon:mod world (docs/mods.md)"
[dependencies]
wit-bindgen = { version = "0.57.1", default-features = false, features = ["macros", "realloc"] }
wit-bindgen = { version = "0.60.0", default-features = false, features = ["macros", "realloc"] }
[lints]
workspace = true
+24 -1
View File
@@ -207,9 +207,13 @@ interface input {
/// It came up again, or the item left the hand, or a screen opened.
use-stop(item-use),
/// A key `register-key` named went down or up while playing (no
/// screen open). The game's own mapping on that key still runs.
/// screen open). The game's own mapping on that key still runs. A
/// screen opening lets every held key up.
key-down(string),
key-up(string),
/// Mouse wheel steps while the mod holds the wheel (`claim-wheel`):
/// positive away from the player, as the hotbar reads them.
wheel(f32),
}
/// The items whose use the mod handles instead of the game, by id (at
/// most 64). Replaces the previous list.
@@ -222,6 +226,10 @@ interface input {
held: func() -> option<held-item>;
/// The events since the last call, oldest first.
poll: func() -> list<event>;
/// Takes the mouse wheel while playing (true) or gives it back: while
/// taken, its steps are the mod's `wheel` events and the hotbar doesn't
/// move. The game gives it back when the mod stops.
claim-wheel: func(on: bool);
/// A mapping as the player has bound it.
record mapping-state {
/// The bound key's name as Controls shows it: `W`, `Space`, `LMB`.
@@ -339,6 +347,21 @@ interface render {
}
/// The game's frame counter, recomputed twice a second.
frame-stats: func() -> frame-rate;
record zoom {
/// The field of view is divided by it: 1 is none, at most 10.
factor: f32,
/// Ease in and out through the game's field-of-view easing, as the
/// spyglass does, instead of at once.
smooth: bool,
/// Mouse turning is multiplied by it while zoomed: 1 keeps the
/// player's sensitivity, at least 0.05.
turn: f32,
}
/// Zooms the camera until the next call (`factor` 1 ends it), only
/// while playing: a screen opening or the spyglass's own view (which
/// wins) leave the camera vanilla's. With several mods, the strongest
/// zoom counts.
set-zoom: func(zoom: zoom);
}
/// What the game calls. Only the hooks `init` asks for are ever called.
+5 -5
View File
@@ -3,7 +3,7 @@ name = "horizon-mods"
version = "0.1.0"
edition.workspace = true
license.workspace = true
rust-version = "1.86"
rust-version = "1.95"
description = "Horizon's mod runtime: sandboxed WebAssembly components (docs/mods.md)"
[dependencies]
@@ -12,11 +12,11 @@ serde_json = "1"
toml = "1.1.6"
zip = { version = "2", default-features = false, features = ["deflate"] }
log = "0.4"
# The 36 LTS line: security fixes until August 2027, and it builds on the client's Rust.
wasmtime = { version = "36.0.18", default-features = false, features = ["runtime", "cranelift", "component-model", "std"], optional = true }
# The 48 LTS line: security fixes until August 2028, on the same wasm-tools (0.254) as wit-component and the SDK's wit-bindgen.
wasmtime = { version = "48.0.7", default-features = false, features = ["runtime", "cranelift", "component-model", "std"], optional = true }
# Turns a mod's core module into a component (cargo horizon-mod build, the tests).
wit-component = { version = "0.239", optional = true }
wit-parser = { version = "0.239", optional = true }
wit-component = { version = "0.254", optional = true }
wit-parser = { version = "0.254", optional = true }
# The registry index's checks (feature `index`): SHA-256, and minisign signatures (no dependencies of its own).
sha2 = { version = "0.10", optional = true }
minisign-verify = { version = "0.3", optional = true }
+2 -2
View File
@@ -137,9 +137,9 @@ impl Capability {
Capability::Command => "run commands as you, with your permissions",
Capability::Registries => "add blocks and items to your worlds",
Capability::Ui => "show its own screens and a settings page",
Capability::Render => "draw on your screen and shake the camera",
Capability::Render => "draw on your screen, shake and zoom the camera",
Capability::Input => {
"see the keys you hold and your clicks, take over the use button on its own items, and add keys"
"see the keys you hold and your clicks, take over the use button on its own items and the mouse wheel, and add keys"
}
}
}
+67 -1
View File
@@ -53,7 +53,7 @@ pub use render_bindings::horizon::mods::input::{Event as InputEvent, HeldItem, I
/// What capability `render` asks the game to draw (WIT `interface render`).
pub use render_bindings::horizon::mods::render::{
Corner as RenderCorner, FillShape, FrameRate, ItemShape, Point as RenderPoint, QuadShape,
Rect as RenderRect, Shape, SpriteShape, TextShape,
Rect as RenderRect, Shape, SpriteShape, TextShape, Zoom as ModZoom,
};
use render_bindings::{RenderHost, horizon::mods::render};
@@ -192,6 +192,8 @@ struct State {
shapes: Vec<Shape>,
/// Capability `render`'s corner panel.
corner: Option<CornerPanel>,
/// Capability `render`'s zoom, when the mod zooms.
zoom: Option<ModZoom>,
input: InputState,
/// Capability `storage`'s folder; made on first `set`.
data_dir: PathBuf,
@@ -334,6 +336,7 @@ impl Mods {
ui: UiState::default(),
shapes: Vec::new(),
corner: None,
zoom: None,
input: InputState::default(),
data_dir,
storage_budget: STORAGE_WRITE_BUDGET,
@@ -590,6 +593,16 @@ impl Mods {
.map(|k| k.name.clone())
.collect();
for name in names {
let held = input.down.iter().position(|n| *n == name);
match (down, held) {
(true, None) => input.down.push(name.clone()),
(false, Some(i)) => {
input.down.swap_remove(i);
}
// Already let go when a screen opened.
(false, None) => continue,
(true, Some(_)) => {}
}
input.push(if down {
InputEvent::KeyDown(name)
} else {
@@ -598,6 +611,35 @@ impl Mods {
}
}
}
/// A screen opened, or the game stopped playing: every mod key held
/// lets go (`key-up`), so a key needs a new press afterwards.
pub fn release_keys(&mut self) {
for run in self.entries.iter_mut().filter_map(|e| e.run.as_mut()) {
let input = &mut run.store.data_mut().input;
for name in std::mem::take(&mut input.down) {
input.push(InputEvent::KeyUp(name));
}
}
}
/// Some running mod holds the mouse wheel.
pub fn wheel_claimed(&self) -> bool {
self.inputs().any(|i| i.wheel)
}
/// Wheel steps while playing: `wheel` to the mods holding it; false
/// when none does (the hotbar takes them).
pub fn wheel(&mut self, steps: f32) -> bool {
let event = InputEvent::Wheel(steps);
let claimed = self.wheel_claimed();
self.push_input(|i| i.wheel, &event);
claimed
}
/// The strongest zoom a running mod asks for (capability `render`).
pub fn zoom(&self) -> Option<ModZoom> {
self.entries
.iter()
.filter_map(|e| e.run.as_ref()?.store.data().zoom)
.max_by(|a, b| a.factor.total_cmp(&b.factor))
}
fn inputs(&self) -> impl Iterator<Item = &InputState> {
self.entries
.iter()
@@ -827,6 +869,10 @@ struct InputState {
claimed: Vec<String>,
keys: Vec<ModKey>,
events: std::collections::VecDeque<InputEvent>,
/// The mod's keys held now, by name.
down: Vec<String>,
/// It holds the mouse wheel.
wheel: bool,
}
/// Events kept for `poll`, items claimed and keys, per mod.
const INPUT_EVENTS: usize = 256;
@@ -892,12 +938,19 @@ impl input::Host for State {
fn clicks_per_second(&mut self) -> (u32, u32) {
self.snapshot.cps
}
fn claim_wheel(&mut self, on: bool) {
self.input.wheel = on;
}
}
/// HUD shapes one mod may keep (capability `render`).
const SHAPES: usize = 1024;
/// The longest jolt `render.shake` makes, in seconds.
const SHAKE_SECONDS: f32 = 10.0;
/// `render.set-zoom`'s limits: `Camera.tickFov` keeps the modifier at 0.1
/// or more, and turning never stops.
const ZOOM_MAX: f32 = 10.0;
const TURN_MIN: f32 = 0.05;
/// A mod's corner panel: shapes in a `width` × `height` box the game
/// places in `corner`, stacked with the other overlays.
@@ -966,6 +1019,19 @@ impl render::Host for State {
.sum::<f32>()
.ceil()
}
fn set_zoom(&mut self, zoom: ModZoom) {
let finite = |v: f32, lo: f32, hi: f32| if v.is_finite() { v.clamp(lo, hi) } else { lo };
let factor = finite(zoom.factor, 1.0, ZOOM_MAX);
self.zoom = (factor > 1.0).then(|| ModZoom {
factor,
smooth: zoom.smooth,
turn: if zoom.turn.is_finite() {
zoom.turn.clamp(TURN_MIN, 1.0)
} else {
1.0
},
});
}
fn frame_stats(&mut self) -> FrameRate {
let (fps, min, low) = self.snapshot.frame;
FrameRate { fps, min, low }
+2 -2
View File
@@ -52,7 +52,7 @@ fn definition(resolve: &Resolve, kind: &TypeDefKind) -> String {
match kind {
TypeDefKind::Option(t) => format!("option<{}>", ty(resolve, t)),
TypeDefKind::List(t) => format!("list<{}>", ty(resolve, t)),
TypeDefKind::FixedSizeList(t, n) => format!("list<{}, {n}>", ty(resolve, t)),
TypeDefKind::FixedLengthList(t, n) => format!("list<{}, {n}>", ty(resolve, t)),
TypeDefKind::Result(r) => match (&r.ok, &r.err) {
(None, None) => "result".into(),
(ok, None) => format!("result<{}>", of(ok)),
@@ -82,7 +82,7 @@ fn signature(resolve: &Resolve, f: &Function) -> String {
let params: Vec<_> = f
.params
.iter()
.map(|(name, t)| format!("{name}: {}", ty(resolve, t)))
.map(|p| format!("{}: {}", p.name, ty(resolve, &p.ty)))
.collect();
let result = f
.result
+50
View File
@@ -543,6 +543,56 @@ fn input_claims_keys_and_events() {
});
assert!(mods.command("input_held"));
assert_eq!(chat(&mut mods), ["holding minecraft:stick x3 in 2"]);
// A screen opening lets held keys up, once; the release that follows
// is not sent again.
mods.key("key.keyboard.r", true);
mods.release_keys();
mods.release_keys();
mods.key("key.keyboard.r", false);
assert!(mods.command("input_poll"));
assert_eq!(chat(&mut mods), ["key-down wheel", "key-up wheel"]);
}
#[test]
fn input_wheel_is_the_mods_while_claimed() {
let mut mods = load("horizon_mods_test_ui_guest.hmod");
let chat = |mods: &mut Mods| -> Vec<String> {
mods.drain()
.into_iter()
.filter_map(|o| match o {
Output::Chat { text, .. } => Some(text),
_ => None,
})
.collect()
};
// Unclaimed, the hotbar keeps the wheel and the mod hears nothing.
assert!(!mods.wheel_claimed() && !mods.wheel(1.0));
assert!(mods.command("input_wheel"));
assert!(mods.wheel_claimed());
assert!(mods.wheel(1.0) && mods.wheel(-2.0));
assert!(mods.command("input_poll"));
assert_eq!(chat(&mut mods), ["wheel 1", "wheel -2"]);
assert!(mods.command("input_wheel off"));
assert!(!mods.wheel(1.0));
}
#[test]
fn render_zoom_is_clamped_and_the_strongest_counts() {
let mut mods = load("horizon_mods_test_ui_guest.hmod");
let zoom = |mods: &mut Mods, args: &str| {
assert!(mods.command(&format!("zoom {args}")));
mods.zoom().map(|z| (z.factor, z.smooth, z.turn))
};
assert!(mods.zoom().is_none());
assert_eq!(zoom(&mut mods, "4 1 0.25"), Some((4.0, true, 0.25)));
// Over 10 and a turn under 0.05 are clamped.
assert_eq!(zoom(&mut mods, "50 0 0"), Some((10.0, false, 0.05)));
// A NaN turn keeps the player's sensitivity.
assert_eq!(zoom(&mut mods, "2 0 x"), Some((2.0, false, 1.0)));
// 1, under 1 and NaN end the zoom.
assert_eq!(zoom(&mut mods, "1 0 1"), None);
assert_eq!(zoom(&mut mods, "0.5 0 1"), None);
assert_eq!(zoom(&mut mods, "x 0 1"), None);
}
#[test]
+24 -2
View File
@@ -2,7 +2,8 @@
//! of each widget, `/ui_settings` sets a settings page, `/ui_close` closes
//! the screen; the `ui-events` calls answer in chat so the host sees them.
//! `/render_hud`, `/render_clear` and `/render_shake` test `render`;
//! `/input_claim`, `/input_poll` and `/input_held` test `input`;
//! `/input_claim`, `/input_poll`, `/input_held` and `/input_wheel` test
//! `input`; `/zoom FACTOR SMOOTH TURN` tests `render.set-zoom`;
//! `/corner` and `/readings` the calls the official extras mods use.
use horizon_mod_sdk::{
Mod, Registration, chat,
@@ -93,6 +94,8 @@ impl Mod for Guest {
"input_claim",
"input_poll",
"input_held",
"input_wheel",
"zoom",
"corner",
"readings",
]
@@ -100,7 +103,7 @@ impl Mod for Guest {
.fold(Registration::new(), Registration::command);
Ok((Guest, hooks))
}
fn command(&mut self, name: &str, _args: &str) -> Result<Option<String>, String> {
fn command(&mut self, name: &str, args: &str) -> Result<Option<String>, String> {
match name {
"ui_screen" => ui::open_screen(&Screen {
title: "Guest's screen".into(),
@@ -167,6 +170,7 @@ impl Mod for Guest {
Event::UseStop(u) => format!("use-stop {} {}", u.item.id, u.ticks),
Event::KeyDown(k) => format!("key-down {k}"),
Event::KeyUp(k) => format!("key-up {k}"),
Event::Wheel(steps) => format!("wheel {steps}"),
});
}
}
@@ -176,6 +180,24 @@ impl Mod for Guest {
});
chat::show(&format!("holding {held}"));
}
"input_wheel" => input::claim_wheel(args != "off"),
"zoom" => {
let mut words = args.split(' ');
let mut number = || {
words
.next()
.and_then(|w| w.parse().ok())
.unwrap_or(f32::NAN)
};
let factor = number();
let smooth = number() == 1.0;
let turn = number();
render::set_zoom(render::Zoom {
factor,
smooth,
turn,
});
}
// Clamped to (1, 10); the two after it shake nothing.
"render_shake" => {
render::shake(2.0, 99.0);

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