mirror of
https://github.com/horizon-rust/horizon.git
synced 2026-11-09 10:39:46 -10:00
Compare commits
174
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
40f82d0e99 | ||
|
|
bc08885276 | ||
|
|
bc7b8889de | ||
|
|
7180a52bf2 | ||
|
|
8e37c4a4b7 | ||
|
|
94ba80f90d | ||
|
|
238fc99add | ||
|
|
ac96d3fa35 | ||
|
|
ceff25fca1 | ||
|
|
36d0695742 | ||
|
|
94484675a8 | ||
|
|
9097d2973d | ||
|
|
00e573cf87 | ||
|
|
a3bf6531e0 | ||
|
|
0ddd7ec41d | ||
|
|
cea1a54e31 | ||
|
|
23dd21e928 | ||
|
|
6cf95bb4d7 | ||
|
|
cf09a6610a | ||
|
|
094bb83dff | ||
|
|
7433b49bef | ||
|
|
dfc91826bd | ||
|
|
2782c5e810 | ||
|
|
86f1749664 | ||
|
|
d149cc1dca | ||
|
|
ae963b0a98 | ||
|
|
c30911cddf | ||
|
|
38eac394be | ||
|
|
5546650bd7 | ||
|
|
c277636360 | ||
|
|
68a01ee7ca | ||
|
|
15e83578ee | ||
|
|
66e8c0d04d | ||
|
|
6bab737cae | ||
|
|
7093c79eeb | ||
|
|
9f5ed29593 | ||
|
|
a4da356144 | ||
|
|
6c2c706959 | ||
|
|
6769422c88 | ||
|
|
ebe7b4a4fd | ||
|
|
1cce1cbf31 | ||
|
|
ed5da43953 | ||
|
|
3295c54097 | ||
|
|
d0da7ec157 | ||
|
|
042d2ce46a | ||
|
|
8f756e8487 | ||
|
|
b7bf458117 | ||
|
|
3dcfd7276b | ||
|
|
bfbf06a439 | ||
|
|
b7b137a829 | ||
|
|
725208d8de | ||
|
|
3516aa9641 | ||
|
|
971f341977 | ||
|
|
cd362fb354 | ||
|
|
216eca2f51 | ||
|
|
7dccd1fa39 | ||
|
|
e84e5b2dd3 | ||
|
|
7b9f3f8e83 | ||
|
|
c9d73d5bea | ||
|
|
5062adfb0c | ||
|
|
56b703899c | ||
|
|
c51a5eec3f | ||
|
|
21aabee4c0 | ||
|
|
7be13f1e9a | ||
|
|
ff83eccf6a | ||
|
|
f2718e086d | ||
|
|
dc382b898a | ||
|
|
b5f396461e | ||
|
|
e963d24058 | ||
|
|
3eec96a8b1 | ||
|
|
249e8db13c | ||
|
|
1d2dd56a06 | ||
|
|
22f3da10c0 | ||
|
|
c992f24496 | ||
|
|
005249b984 | ||
|
|
63dae1216c | ||
|
|
eb3157d251 | ||
|
|
d577d49a78 | ||
|
|
8a9bd15a3d | ||
|
|
a40f3d82c1 | ||
|
|
db8048a1b6 | ||
|
|
7383ff1908 | ||
|
|
4f4c87df64 | ||
|
|
6676c3a9a9 | ||
|
|
af3c22b9f1 | ||
|
|
182eec2cb7 | ||
|
|
37bdbf9def | ||
|
|
53d1afc5b1 | ||
|
|
a94b5a1b2e | ||
|
|
b4c7f409d2 | ||
|
|
3365fd6bc4 | ||
|
|
c0da1ac2e1 | ||
|
|
45cefe3278 | ||
|
|
032ae31f34 | ||
|
|
47d55b5690 | ||
|
|
db4a145e6b | ||
|
|
59d958c09e | ||
|
|
1a2015e305 | ||
|
|
197172ef3c | ||
|
|
c3c0e2d868 | ||
|
|
528d98bf36 | ||
|
|
9d6d292348 | ||
|
|
7973f9935b | ||
|
|
d119d6da2d | ||
|
|
4bc93d9a5f | ||
|
|
671af567f4 | ||
|
|
1f19e94d74 | ||
|
|
fcbeaefd37 | ||
|
|
8bf0614d9d | ||
|
|
83b3c9306f | ||
|
|
e6184afbe2 | ||
|
|
96c5616505 | ||
|
|
55b78015fe | ||
|
|
cb1198a38d | ||
|
|
17b50d7959 | ||
|
|
7766619ecb | ||
|
|
6e21ce9476 | ||
|
|
365a44ff91 | ||
|
|
358456bb02 | ||
|
|
07068153c6 | ||
|
|
f5c6b7dfd1 | ||
|
|
8d5dd89421 | ||
|
|
6280d2630c | ||
|
|
0568b2beff | ||
|
|
aa741b3570 | ||
|
|
21860eefba | ||
|
|
0ccd2a4397 | ||
|
|
dd8214d3a3 | ||
|
|
34b5df1397 | ||
|
|
74eaac0c21 | ||
|
|
b9efd702dc | ||
|
|
79cd63b8bc | ||
|
|
a2fb9bc76e | ||
|
|
72c6784172 | ||
|
|
678d6da047 | ||
|
|
24c9ad202b | ||
|
|
6020c75004 | ||
|
|
1215645c4e | ||
|
|
c587403015 | ||
|
|
3ebcf19eed | ||
|
|
f2f02e57bd | ||
|
|
c10aea9eb8 | ||
|
|
bf22beb825 | ||
|
|
10fb981317 | ||
|
|
443a42f97a | ||
|
|
7f67dc8333 | ||
|
|
230b167744 | ||
|
|
3c710450fb | ||
|
|
6a6aa75cb9 | ||
|
|
9b72080c32 | ||
|
|
c93db2be5f | ||
|
|
df6c3384fb | ||
|
|
c71978372d | ||
|
|
987dc976d3 | ||
|
|
6187796e59 | ||
|
|
70db97e662 | ||
|
|
cf28dc7745 | ||
|
|
e8196d58c4 | ||
|
|
0ef952921d | ||
|
|
9698eed74f | ||
|
|
f9dfc692e5 | ||
|
|
233e09221d | ||
|
|
af9edec8b9 | ||
|
|
2a44a86383 | ||
|
|
de9ab4646d | ||
|
|
2d2a2621f2 | ||
|
|
067ca7dfb3 | ||
|
|
b3dd537570 | ||
|
|
d2dbe9227c | ||
|
|
e0b91d71bf | ||
|
|
5ed7535acd | ||
|
|
1bb2149e88 | ||
|
|
d59333ab3a | ||
|
|
4b502a3c48 |
No files matched your search
@@ -13,6 +13,14 @@ else (videos, Discord and other community material) lives elsewhere.
|
||||
suggest something. A pull request for a roadmap item carries its `roadmap:<id>` label; ideas go to the suggestion
|
||||
queue (the hub, or `/suggest` on Discord) rather than to issues, which are for bugs.
|
||||
|
||||
**Mod-related pull requests go to [arismoko/horizon](https://github.com/arismoko/horizon)**, the fork that carries the
|
||||
mod API and reviews and merges that work. That covers the mod API, native hooks and events for mods, native mods,
|
||||
content mods (`.hmod`), mixins, wasm mods, `cargo horizon-mod`, `horizon-mods`, mod builds and the registry, and
|
||||
their docs ([mods](docs/mods.md), [native mods](docs/native-mods.md)). Open the pull request there from your usual
|
||||
branch. Mod-related pull requests opened here are closed with a pointer to it. Engine extension points that Horizon's
|
||||
own crates register (the [layer split](docs/layers.md)), resource and shader pack formats, and gameplay fixes stay
|
||||
here.
|
||||
|
||||
## Build it
|
||||
|
||||
- **Prerequisites:** Rust (stable), Python 3.10+, JDK 25, and on Linux the ALSA and udev headers
|
||||
|
||||
Generated
+3
-7
@@ -1418,6 +1418,7 @@ dependencies = [
|
||||
name = "horizon-chat-filter"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"horizon-server-files",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"toml",
|
||||
@@ -1528,7 +1529,6 @@ dependencies = [
|
||||
"horizon-client-render",
|
||||
"horizon-command",
|
||||
"horizon-core",
|
||||
"horizon-friends",
|
||||
"horizon-hdr",
|
||||
"horizon-hooks",
|
||||
"horizon-items",
|
||||
@@ -1592,7 +1592,6 @@ dependencies = [
|
||||
"horizon-optifine",
|
||||
"horizon-polytone",
|
||||
"horizon-protocol",
|
||||
"horizon-render-pipeline",
|
||||
"horizon-shader-api",
|
||||
"horizon-shaders",
|
||||
"horizon-system",
|
||||
@@ -1668,6 +1667,7 @@ dependencies = [
|
||||
"getrandom 0.3.4",
|
||||
"hkdf",
|
||||
"horizon-addresses",
|
||||
"horizon-platform",
|
||||
"if-addrs",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -1793,6 +1793,7 @@ dependencies = [
|
||||
name = "horizon-official"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"horizon-friends",
|
||||
"horizon-game",
|
||||
"horizon-heartbeat",
|
||||
"horizon-platform",
|
||||
@@ -1891,10 +1892,6 @@ dependencies = [
|
||||
"ring",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "horizon-render-pipeline"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "horizon-server"
|
||||
version = "0.1.0"
|
||||
@@ -1957,7 +1954,6 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"half",
|
||||
"horizon-core",
|
||||
"horizon-render-pipeline",
|
||||
"naga",
|
||||
"png",
|
||||
"zip",
|
||||
|
||||
@@ -43,7 +43,6 @@ members = [
|
||||
"crates/cargo-horizon-mod",
|
||||
"crates/horizon-port-pack",
|
||||
"crates/horizon-shaders",
|
||||
"crates/horizon-render-pipeline",
|
||||
"crates/horizon-optifine",
|
||||
"crates/horizon-polytone",
|
||||
"crates/horizon-shader-api",
|
||||
|
||||
@@ -7,6 +7,8 @@ rust-version.workspace = true
|
||||
description = "The dedicated server's chat filter: its [chat_filter] settings, the slur list's matcher and the mutes, with no game state (docs/chat-filter.md)"
|
||||
|
||||
[dependencies]
|
||||
# The moderation durations, shared with `/tempban`.
|
||||
horizon-server-files = { path = "../horizon-server-files" }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
toml = "1.1"
|
||||
|
||||
@@ -10,6 +10,9 @@ use std::path::{Path, PathBuf};
|
||||
|
||||
mod normalise;
|
||||
|
||||
pub use horizon_server_files::duration::{
|
||||
DurationUnit, duration_parts, duration_text, now_millis, parse_duration,
|
||||
};
|
||||
pub use normalise::{Run, Text};
|
||||
|
||||
/// The list built into the game, used when `words_file` is empty.
|
||||
@@ -474,61 +477,6 @@ pub fn append_log(path: &Path, line: &str) -> std::io::Result<()> {
|
||||
writeln!(f, "{line}")
|
||||
}
|
||||
|
||||
/// `/tempmute`'s duration in milliseconds: a positive whole number and a unit, `s`, `m`, `h`, `d` or
|
||||
/// `w` (`10m`, `2h`, `1d`).
|
||||
pub fn parse_duration(text: &str) -> Option<i64> {
|
||||
let unit = match text.chars().last()? {
|
||||
's' => 1_000,
|
||||
'm' => MINUTE,
|
||||
'h' => 60 * MINUTE,
|
||||
'd' => 24 * 60 * MINUTE,
|
||||
'w' => 7 * 24 * 60 * MINUTE,
|
||||
_ => return None,
|
||||
};
|
||||
let count: i64 = text[..text.len() - 1].parse().ok()?;
|
||||
(count > 0).then(|| count.checked_mul(unit)).flatten()
|
||||
}
|
||||
|
||||
/// The unit used when presenting a moderation duration to a player.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum DurationUnit {
|
||||
Minute,
|
||||
Hour,
|
||||
Day,
|
||||
}
|
||||
|
||||
/// A duration's rounded count and unit, shared by logs and translated replies.
|
||||
pub fn duration_parts(millis: i64) -> (i64, DurationUnit) {
|
||||
let (unit, kind) = if millis <= 60 * MINUTE {
|
||||
(MINUTE, DurationUnit::Minute)
|
||||
} else if millis <= 48 * 60 * MINUTE {
|
||||
(60 * MINUTE, DurationUnit::Hour)
|
||||
} else {
|
||||
(24 * 60 * MINUTE, DurationUnit::Day)
|
||||
};
|
||||
(
|
||||
millis.div_euclid(unit) + i64::from(millis.rem_euclid(unit) > 0),
|
||||
kind,
|
||||
)
|
||||
}
|
||||
|
||||
/// `millis` for logs, rounded up: minutes up to an hour, hours up to two days, then days.
|
||||
pub fn duration_text(millis: i64) -> String {
|
||||
let (count, unit) = duration_parts(millis);
|
||||
let unit = match unit {
|
||||
DurationUnit::Minute => "minute",
|
||||
DurationUnit::Hour => "hour",
|
||||
DurationUnit::Day => "day",
|
||||
};
|
||||
format!("{count} {unit}{}", if count == 1 { "" } else { "s" })
|
||||
}
|
||||
|
||||
pub fn now_millis() -> i64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_or(0, |d| d.as_millis() as i64)
|
||||
}
|
||||
|
||||
/// `YYYY-MM-DDTHH:MM:SSZ` for epoch milliseconds.
|
||||
pub fn utc(millis: i64) -> String {
|
||||
let secs = millis.div_euclid(1000);
|
||||
|
||||
@@ -670,6 +670,39 @@ pub fn with_horizon_icon(
|
||||
attributes
|
||||
}
|
||||
|
||||
/// The screen a window is on: its size in physical pixels, its refresh rate where the OS gives
|
||||
/// it, and its scale. For `horizon-ctl status` and the performance statistics' `system.display`.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Display {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub refresh_hz: Option<u32>,
|
||||
pub scale: f64,
|
||||
}
|
||||
|
||||
/// The screen `window` is on, else the primary one, else the only one: right after the window
|
||||
/// is made, Wayland doesn't say yet which screen it is on.
|
||||
pub fn monitor(window: &winit::window::Window) -> Option<winit::monitor::MonitorHandle> {
|
||||
let only = || {
|
||||
let mut all = window.available_monitors();
|
||||
all.next().filter(|_| all.next().is_none())
|
||||
};
|
||||
window
|
||||
.current_monitor()
|
||||
.or_else(|| window.primary_monitor())
|
||||
.or_else(only)
|
||||
}
|
||||
|
||||
/// [`monitor`]'s size, refresh rate and scale.
|
||||
pub fn display(window: &winit::window::Window) -> Option<Display> {
|
||||
monitor(window).map(|m| Display {
|
||||
width: m.size().width,
|
||||
height: m.size().height,
|
||||
refresh_hz: m.refresh_rate_millihertz().map(|r| r.div_ceil(1000)),
|
||||
scale: m.scale_factor(),
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -42,8 +42,6 @@ horizon-chat-filter = { path = "../horizon-chat-filter" }
|
||||
horizon-shaders = { path = "../horizon-shaders" }
|
||||
# Horizon's own shader packs: the format, the stages and the check (docs/shaders.md).
|
||||
horizon-shader-api = { path = "../horizon-shader-api" }
|
||||
# Play with Friends: invite codes and their tunnel; the `friends` feature.
|
||||
horizon-friends = { path = "../horizon-friends", optional = true }
|
||||
# The narrator (GameNarrator): text-to-speech of chat, system messages and screens.
|
||||
horizon-narrator = { path = "../horizon-narrator" }
|
||||
# Horizon mods: sandboxed WebAssembly components, off unless installed and turned on (docs/mods.md).
|
||||
@@ -111,16 +109,17 @@ objc2-foundation = { version = "0.3", default-features = false, features = ["std
|
||||
wgpu = { version = "30.0.1", default-features = false, features = ["gles"] }
|
||||
|
||||
[features]
|
||||
default = ["audio", "controllers", "discord", "friends"]
|
||||
default = ["audio", "controllers", "horizon-extras"]
|
||||
# Sound output. Without it (and `controllers`) the program links neither libasound nor libudev on
|
||||
# Linux: the server build (`--no-default-features`, released as horizon-engine; docs/server.md).
|
||||
audio = ["horizon-client-render/audio"]
|
||||
# Controllers: gilrs, and the DualSense's own output. Without it only `horizon-ctl pad`'s virtual pad.
|
||||
controllers = ["dep:gilrs", "horizon-client-input/controllers"]
|
||||
# Horizon's own parts (docs/forking.md, docs/layers.md): the features below, and the official
|
||||
# server in the list (`official_servers`).
|
||||
horizon-extras = ["discord"]
|
||||
# "Playing Horizon" on the player's Discord profile (`discord_presence`).
|
||||
discord = []
|
||||
# Play with Friends, joining side (`net.rs`): without it no invite code joins.
|
||||
friends = ["dep:horizon-friends"]
|
||||
# Records the call sites of mob random draws (`mob_ai_probe` traces).
|
||||
rng-trace = ["horizon-core/rng-trace"]
|
||||
# Compatibility with earlier experiment commands: main already uses mimalloc.
|
||||
|
||||
@@ -26,7 +26,6 @@ horizon-narrator = { path = "../horizon-narrator" }
|
||||
horizon-optifine = { path = "../horizon-optifine" }
|
||||
horizon-polytone = { path = "../horizon-polytone" }
|
||||
horizon-protocol = { path = "../horizon-protocol" }
|
||||
horizon-render-pipeline = { path = "../horizon-render-pipeline" }
|
||||
horizon-shader-api = { path = "../horizon-shader-api" }
|
||||
horizon-shaders = { path = "../horizon-shaders" }
|
||||
horizon-system = { path = "../horizon-system" }
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! The Horizon Extras page, on the settings shell (`settings_shell`): which rows it has and what
|
||||
//! pressing them does. Each one's value and what it does stay in `extras::Item`.
|
||||
//! pressing them does. Each one's value and what it does stay in `extras::Item`; the sections crates
|
||||
//! add (`horizon_hooks::pages::Place::Extras`) keep theirs.
|
||||
use crate::{
|
||||
extras::{self, Item},
|
||||
lang,
|
||||
@@ -7,6 +8,7 @@ use crate::{
|
||||
settings::Settings,
|
||||
settings_shell::{self as shell, Binding, Group, Output, Row, Screen, Sheet, Value},
|
||||
};
|
||||
use horizon_hooks::pages::{self as added, Act, Label};
|
||||
|
||||
const TITLE: &str = "horizon.pages.extras";
|
||||
const ABOUT: &str = "horizon.pages.extras.tooltip";
|
||||
@@ -31,6 +33,121 @@ pub(crate) enum ExtraRow {
|
||||
/// It isn't installed: where to get it. A note, not a button: Options > Mods already has the
|
||||
/// one way into the Mods page (`every_setting_and_page_is_reached_from_one_place`).
|
||||
ZoomInstall,
|
||||
/// Row `.1` of the section registered page `.0` adds (`Place::Extras`).
|
||||
Added(usize, usize),
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// A test's own pages, instead of the game's registered ones: each test runs on its thread.
|
||||
static TEST_PAGES: std::cell::Cell<Option<&'static [&'static dyn added::Page]>> =
|
||||
const { std::cell::Cell::new(None) };
|
||||
}
|
||||
|
||||
/// The pages crates registered, all places.
|
||||
fn pages() -> &'static [&'static dyn added::Page] {
|
||||
#[cfg(test)]
|
||||
if let Some(pages) = TEST_PAGES.get() {
|
||||
return pages;
|
||||
}
|
||||
added::engine::all()
|
||||
}
|
||||
|
||||
/// The sections of this page, by their index among the registered pages.
|
||||
fn sections() -> impl Iterator<Item = (usize, &'static dyn added::Page)> {
|
||||
let pages = pages().iter().copied().enumerate();
|
||||
pages.filter(|(_, page)| page.place() == added::Place::Extras)
|
||||
}
|
||||
|
||||
fn label_text(label: Label) -> String {
|
||||
match label {
|
||||
Label::Key(key) => lang::get(key),
|
||||
Label::Value(text) => text,
|
||||
}
|
||||
}
|
||||
|
||||
/// The page's group named `name`, made at the end if it has none.
|
||||
fn group_named<'a>(
|
||||
groups: &'a mut Vec<Group<ExtraRow>>,
|
||||
name: &'static str,
|
||||
) -> &'a mut Group<ExtraRow> {
|
||||
let at = groups.iter().position(|group| group.name == name);
|
||||
let at = at.unwrap_or_else(|| {
|
||||
groups.push(Group {
|
||||
name,
|
||||
rows: Vec::new(),
|
||||
});
|
||||
groups.len() - 1
|
||||
});
|
||||
&mut groups[at]
|
||||
}
|
||||
|
||||
/// The sections' rows, in the groups their titles and headings name.
|
||||
fn add_sections(groups: &mut Vec<Group<ExtraRow>>) {
|
||||
for (page, section) in sections() {
|
||||
let mut rows = Vec::new();
|
||||
section.rows(&mut rows);
|
||||
let mut name = section.title();
|
||||
for (r, row) in rows.into_iter().enumerate() {
|
||||
let id = ExtraRow::Added(page, r);
|
||||
let row = match row {
|
||||
added::Row::Heading(key) => {
|
||||
name = key;
|
||||
continue;
|
||||
}
|
||||
added::Row::Note(text) => Row::new(id, text, Value::Note).live(false),
|
||||
added::Row::Toggle { label, on } => {
|
||||
Row::new(id, lang::get(label), Value::Switch(on))
|
||||
}
|
||||
added::Row::Cycle { label, value } => {
|
||||
Row::new(id, lang::get(label), Value::Choice(label_text(value)))
|
||||
}
|
||||
added::Row::Slider { label, value, text } => {
|
||||
let position = value.clamp(0.0, 1.0);
|
||||
let text = label_text(text);
|
||||
let value = Value::Slider {
|
||||
position,
|
||||
steps: None,
|
||||
text,
|
||||
};
|
||||
Row::new(id, lang::get(label), value)
|
||||
}
|
||||
added::Row::Button { label } => Row::new(id, lang::get(label), Value::Press),
|
||||
added::Row::Key { label, key } => {
|
||||
let key = crate::keybinds::display_name(&key).plain();
|
||||
Row::new(id, lang::get(label), Value::Read(key))
|
||||
}
|
||||
// A row kind newer than this engine.
|
||||
_ => continue,
|
||||
};
|
||||
let row = match section.tooltip(r) {
|
||||
Some(about) => row.about(label_text(about)),
|
||||
None => row,
|
||||
};
|
||||
group_named(groups, name).rows.push(row);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Row `r` of registered page `page` was acted on: the options screen saves when it closes.
|
||||
fn act(page: usize, r: usize, act: Act) -> Vec<Output> {
|
||||
let Some(section) = pages().get(page) else {
|
||||
return Vec::new();
|
||||
};
|
||||
section.act(r, act);
|
||||
vec![Output::Event(Event::Changed)]
|
||||
}
|
||||
|
||||
/// Whether row `r` of registered page `page` steps (a switch or a cycle).
|
||||
fn steps(page: usize, r: usize) -> bool {
|
||||
let mut rows = Vec::new();
|
||||
if let Some(section) = pages().get(page) {
|
||||
section.rows(&mut rows);
|
||||
}
|
||||
matches!(
|
||||
rows.get(r),
|
||||
Some(added::Row::Toggle { .. } | added::Row::Cycle { .. })
|
||||
)
|
||||
}
|
||||
|
||||
fn item_row(item: Item, s: &Settings) -> Row<ExtraRow> {
|
||||
@@ -85,9 +202,14 @@ impl Screen for ExtrasScreen {
|
||||
fn note_lines(&self) -> usize {
|
||||
NOTE_LINES
|
||||
}
|
||||
/// The rows read the extras, the mods and whether cosmetics are offered.
|
||||
/// The rows read the extras, the mods, whether cosmetics are offered and the sections' rows.
|
||||
fn stamp(&self, s: &Settings) -> u64 {
|
||||
shell::stamp_of(&(&s.extras, &s.mods, horizon_cosmetics::enabled()))
|
||||
let mut added = Vec::new();
|
||||
for (_, section) in sections() {
|
||||
section.rows(&mut added);
|
||||
}
|
||||
let cosmetics = horizon_cosmetics::enabled();
|
||||
shell::stamp_of(&(&s.extras, &s.mods, cosmetics, added))
|
||||
}
|
||||
fn sheets(&self, s: &Settings) -> Vec<Sheet<(), ExtraRow>> {
|
||||
let mut groups = vec![Group {
|
||||
@@ -107,6 +229,7 @@ impl Screen for ExtrasScreen {
|
||||
}
|
||||
}
|
||||
}
|
||||
add_sections(&mut groups);
|
||||
vec![Sheet::new((), lang::get(TITLE), lang::get(ABOUT), groups)]
|
||||
}
|
||||
fn press(&mut self, s: &mut Settings, row: ExtraRow, back: bool) -> Vec<Output> {
|
||||
@@ -125,16 +248,29 @@ impl Screen for ExtrasScreen {
|
||||
Output::Event(Event::NativeSettings(i)),
|
||||
],
|
||||
ExtraRow::ZoomInstall => Vec::new(),
|
||||
ExtraRow::Added(page, r) => {
|
||||
act(page, r, if back { Act::PressBack } else { Act::Press })
|
||||
}
|
||||
}
|
||||
}
|
||||
fn slide(&mut self, _s: &mut Settings, row: ExtraRow, position: f32) {
|
||||
if let ExtraRow::Added(page, r) = row {
|
||||
act(page, r, Act::Slide(position));
|
||||
}
|
||||
}
|
||||
/// With the wheel extra on, the wheel over an item changes it, without a click; the mod's rows
|
||||
/// let the list scroll.
|
||||
fn wheel_step(&mut self, s: &mut Settings, row: ExtraRow, back: bool) -> Option<Vec<Output>> {
|
||||
let ExtraRow::Item(item) = row else {
|
||||
return None;
|
||||
};
|
||||
item.cycle(&mut s.extras, back);
|
||||
Some(vec![Output::Event(Event::Changed)])
|
||||
match row {
|
||||
ExtraRow::Item(item) => {
|
||||
item.cycle(&mut s.extras, back);
|
||||
Some(vec![Output::Event(Event::Changed)])
|
||||
}
|
||||
ExtraRow::Added(page, r) if steps(page, r) => {
|
||||
Some(act(page, r, if back { Act::PressBack } else { Act::Press }))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -292,4 +428,148 @@ mod tests {
|
||||
shell.key(&mut s, K::ArrowDown, None, false, size);
|
||||
assert_eq!(shell.focused_row(), Some(first));
|
||||
}
|
||||
|
||||
/// A section with a switch in the page's Multiplayer group, then a heading of its own with a
|
||||
/// number that steps either way, a note and a volume.
|
||||
struct Section {
|
||||
on: std::sync::Mutex<bool>,
|
||||
step: std::sync::Mutex<i32>,
|
||||
volume: std::sync::Mutex<f32>,
|
||||
}
|
||||
impl added::Page for Section {
|
||||
fn id(&self) -> &'static str {
|
||||
"test.section"
|
||||
}
|
||||
fn title(&self) -> &'static str {
|
||||
"horizon.extras.header.multiplayer"
|
||||
}
|
||||
fn place(&self) -> added::Place {
|
||||
added::Place::Extras
|
||||
}
|
||||
fn rows(&self, rows: &mut Vec<added::Row>) {
|
||||
rows.push(added::Row::Toggle {
|
||||
label: "test.section.switch",
|
||||
on: *self.on.lock().unwrap(),
|
||||
});
|
||||
rows.push(added::Row::Heading("test.section.heading"));
|
||||
rows.push(added::Row::Cycle {
|
||||
label: "test.section.step",
|
||||
value: Label::Value(self.step.lock().unwrap().to_string()),
|
||||
});
|
||||
rows.push(added::Row::Note("a note".into()));
|
||||
rows.push(added::Row::Slider {
|
||||
label: "test.section.volume",
|
||||
value: *self.volume.lock().unwrap(),
|
||||
text: Label::Value(String::new()),
|
||||
});
|
||||
}
|
||||
fn act(&self, row: usize, act: Act) {
|
||||
match (row, act) {
|
||||
(0, Act::Press | Act::PressBack) => {
|
||||
let mut on = self.on.lock().unwrap();
|
||||
*on = !*on;
|
||||
}
|
||||
(2, Act::Press) => *self.step.lock().unwrap() += 1,
|
||||
(2, Act::PressBack) => *self.step.lock().unwrap() -= 1,
|
||||
(4, Act::Slide(v)) => *self.volume.lock().unwrap() = v,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A page for Horizon Settings: never on Extras.
|
||||
struct Elsewhere;
|
||||
impl added::Page for Elsewhere {
|
||||
fn id(&self) -> &'static str {
|
||||
"test.elsewhere"
|
||||
}
|
||||
fn title(&self) -> &'static str {
|
||||
"test.elsewhere"
|
||||
}
|
||||
fn place(&self) -> added::Place {
|
||||
added::Place::HorizonSettings
|
||||
}
|
||||
fn rows(&self, rows: &mut Vec<added::Row>) {
|
||||
rows.push(added::Row::Button {
|
||||
label: "test.elsewhere.button",
|
||||
});
|
||||
}
|
||||
fn act(&self, _: usize, _: Act) {}
|
||||
}
|
||||
|
||||
fn with_section() -> &'static Section {
|
||||
let section: &'static Section = Box::leak(Box::new(Section {
|
||||
on: false.into(),
|
||||
step: 0.into(),
|
||||
volume: 0.5.into(),
|
||||
}));
|
||||
let pages: &'static [&'static dyn added::Page] = Box::leak(Box::new([
|
||||
&Elsewhere as &dyn added::Page,
|
||||
section as &dyn added::Page,
|
||||
]));
|
||||
TEST_PAGES.set(Some(pages));
|
||||
section
|
||||
}
|
||||
|
||||
/// The section's first row joins the page's own Multiplayer group, after its rows; its heading
|
||||
/// starts a group after the page's; a page for elsewhere adds nothing.
|
||||
#[test]
|
||||
fn a_section_joins_the_group_its_title_names_and_heads_its_own() {
|
||||
with_section();
|
||||
let s = Settings::default();
|
||||
let sheets = ExtrasScreen.sheets(&s);
|
||||
let groups = &sheets[0].groups;
|
||||
let ids = |name: &str| -> Vec<ExtraRow> {
|
||||
let group = groups.iter().find(|g| g.name == name).unwrap();
|
||||
group.rows.iter().map(|r| r.id).collect()
|
||||
};
|
||||
let multiplayer = ids("horizon.extras.header.multiplayer");
|
||||
assert_eq!(multiplayer.last(), Some(&ExtraRow::Added(1, 0)));
|
||||
assert_eq!(groups.last().unwrap().name, "test.section.heading");
|
||||
let own = [
|
||||
ExtraRow::Added(1, 2),
|
||||
ExtraRow::Added(1, 3),
|
||||
ExtraRow::Added(1, 4),
|
||||
];
|
||||
assert_eq!(ids("test.section.heading"), own);
|
||||
let all = rows_of(&s);
|
||||
assert!(!all.iter().any(|r| matches!(r.id, ExtraRow::Added(0, _))));
|
||||
}
|
||||
|
||||
/// A click flips the switch, a Shift click steps the number back, the wheel steps it, and the
|
||||
/// note takes no click; the rows show what the section holds after each.
|
||||
#[test]
|
||||
fn a_sections_rows_act_on_it() {
|
||||
let section = with_section();
|
||||
let mut s = Settings::default();
|
||||
let mut shell = Shell::<ExtrasScreen>::default();
|
||||
let switch = ExtraRow::Added(1, 0);
|
||||
let at = shell.point_at(&s, SIZE, switch);
|
||||
assert!(
|
||||
!shell
|
||||
.click(&mut s, at, SIZE, false, false, false)
|
||||
.is_empty()
|
||||
);
|
||||
assert!(*section.on.lock().unwrap());
|
||||
let value = |s: &Settings, id| rows_of(s).into_iter().find(|r| r.id == id).unwrap().value;
|
||||
assert_eq!(value(&s, switch), Value::Switch(true));
|
||||
let step = ExtraRow::Added(1, 2);
|
||||
let mut screen = ExtrasScreen;
|
||||
screen.press(&mut s, step, true);
|
||||
assert_eq!(value(&s, step), Value::Choice("-1".into()));
|
||||
assert!(screen.wheel_step(&mut s, step, false).is_some());
|
||||
assert_eq!(*section.step.lock().unwrap(), 0);
|
||||
assert_eq!(
|
||||
screen.wheel_step(&mut s, ExtraRow::Added(1, 3), false),
|
||||
None
|
||||
);
|
||||
let note = shell.point_at(&s, SIZE, ExtraRow::Added(1, 3));
|
||||
assert!(
|
||||
shell
|
||||
.click(&mut s, note, SIZE, false, false, false)
|
||||
.is_empty()
|
||||
);
|
||||
screen.slide(&mut s, ExtraRow::Added(1, 4), 0.25);
|
||||
assert_eq!(*section.volume.lock().unwrap(), 0.25);
|
||||
}
|
||||
}
|
||||
@@ -231,8 +231,8 @@ pub struct FogInputs<'a> {
|
||||
/// `GameRenderer.nightVisionScale` while Night Vision is on without
|
||||
/// Darkness, else 0 (`FogRenderer.computeFogColor`'s brightenFactor).
|
||||
pub night_vision: f32,
|
||||
/// The packs' Polytone `environment` fog settings.
|
||||
pub environment: horizon_polytone::colors::Environment,
|
||||
/// The packs' `environment` fog settings (Polytone's).
|
||||
pub environment: crate::pack_palette::Environment,
|
||||
}
|
||||
|
||||
/// `FogRenderer.setupFog` + `computeFogColor`.
|
||||
@@ -581,7 +581,7 @@ mod tests {
|
||||
let tables = FogTables {
|
||||
water_fog_end: vec![1.0],
|
||||
};
|
||||
let environment = horizon_polytone::colors::Environment {
|
||||
let environment = crate::pack_palette::Environment {
|
||||
powder_snow_fog: Some(0xFF0000),
|
||||
blindness_fog_distance: 40.0,
|
||||
water_fog_brightening: 0.0,
|
||||
@@ -613,7 +613,7 @@ mod tests {
|
||||
}
|
||||
fn inputs_with<'a>(
|
||||
input: &FogInputs<'a>,
|
||||
environment: horizon_polytone::colors::Environment,
|
||||
environment: crate::pack_palette::Environment,
|
||||
) -> FogInputs<'a> {
|
||||
FogInputs {
|
||||
environment,
|
||||
|
||||
@@ -239,6 +239,7 @@ mod reuse;
|
||||
mod stars;
|
||||
mod surface_setup;
|
||||
use surface_setup::{AdapterChoice, AdapterFailure, open_adapter, pick_adapter, set_up_surface};
|
||||
mod off_page;
|
||||
pub mod submit;
|
||||
mod sunrise_fan;
|
||||
mod timing;
|
||||
@@ -250,6 +251,7 @@ mod wgsl_prefix;
|
||||
mod world_post;
|
||||
pub mod world_primitives;
|
||||
mod world_warp;
|
||||
use off_page::move_off_page;
|
||||
|
||||
/// A section's instance: its camera-relative origin and, in `w`, the step of
|
||||
/// its fixed-point UVs (`terrain.wgsl`'s `terrain_uv`).
|
||||
@@ -726,62 +728,6 @@ impl Part {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `Renderer::compact_arena`'s and `shrink_translucent`'s moves: what the meshes
|
||||
/// hold on `page` of `arena` (`part`) goes to its other pages in video memory,
|
||||
/// until the next range would pass `budget` bytes (it keeps what is left; the
|
||||
/// first range moves whatever its size, so none keeps the page closed), the
|
||||
/// copies into `encoder`, the ranges' flat draw ids (`flat`) and, in the
|
||||
/// section arena, GPU culling's records (`records`) along. False when the other
|
||||
/// pages have no room left.
|
||||
fn move_off_page<'a>(
|
||||
arena: &mut arena::Arena,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
mut flat: Option<(&mut flat::Flat, &wgpu::Device, &mut upload::Uploads)>,
|
||||
mut records: Option<&mut gpu_culling::Records>,
|
||||
meshes: impl Iterator<Item = &'a mut SectionGpu>,
|
||||
page: u32,
|
||||
part: Part,
|
||||
budget: &mut u64,
|
||||
) -> bool {
|
||||
debug_assert!(
|
||||
records.is_none() || matches!(part, Part::Section { .. }),
|
||||
"records follow the section arena's ranges"
|
||||
);
|
||||
let mut moved = false;
|
||||
for s in meshes {
|
||||
// Nothing of a mesh here: split, one without solid quads (its (0, 0) is no
|
||||
// range of page 0); in the other arenas, one without translucent quads.
|
||||
let Some((_, start, vertices)) = part.of(s).filter(|r| r.0 == page) else {
|
||||
continue;
|
||||
};
|
||||
let bytes = vertices * arena.stride();
|
||||
if moved && bytes > *budget {
|
||||
break;
|
||||
}
|
||||
let Some(to) = arena.alloc_moved(vertices) else {
|
||||
return false;
|
||||
};
|
||||
arena.copy(encoder, (page, start), to, vertices);
|
||||
arena.release(page, start, vertices);
|
||||
if let (Some((flat, device, uploads)), Some(id)) = (&mut flat, s.flat_id) {
|
||||
let key = match part {
|
||||
Part::Translucent => to.0 + flat::TRANSLUCENT_KEYS,
|
||||
_ => to.0,
|
||||
};
|
||||
flat.place(device, encoder, uploads, id, key, to.1, vertices);
|
||||
}
|
||||
part.set(s, to);
|
||||
// Its solid quads start the range in either layout.
|
||||
if let (Part::Section { .. }, Some(records), Some(id)) =
|
||||
(part, records.as_deref_mut(), s.gpu_id)
|
||||
{
|
||||
records.relocate(id, to.0, u64::from(s.solid_base()));
|
||||
}
|
||||
*budget = budget.saturating_sub(bytes);
|
||||
moved = true;
|
||||
}
|
||||
true
|
||||
}
|
||||
/// Starts moving a page of a large arena far larger than its ranges in use
|
||||
/// (`Arena::oversized`) into a new page of the size they get (`Arena::open_page`),
|
||||
/// for `Renderer::shrink_translucent`: the page closes and `move_off_page` empties
|
||||
@@ -10116,7 +10062,11 @@ impl Renderer {
|
||||
let pack_reads_world = pack_post || native_post;
|
||||
laps.lap("r_uploads");
|
||||
// Phase 2 of occlusion culling draws over the depth, and a shader pack or TAA reads it.
|
||||
// Post effects (a resource pack's chains, `use_depth_buffer`) read the world's depth
|
||||
// after the world pass, as `GameRenderer.render` hands them the main target's.
|
||||
let post_reads_depth = !frame.title && !frame.post_effects.effects.is_empty();
|
||||
let keep_depth = pack_reads_world
|
||||
|| post_reads_depth
|
||||
|| (taa && self.world_post.taa)
|
||||
|| oit_on
|
||||
|| native_render::render_needs().world_depth
|
||||
@@ -13714,6 +13664,7 @@ mod tests {
|
||||
Some(Reported {
|
||||
paper_white: 240,
|
||||
peak: 998,
|
||||
peak_guessed: true,
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
@@ -13721,6 +13672,7 @@ mod tests {
|
||||
Some(Reported {
|
||||
paper_white: 480,
|
||||
peak: 480,
|
||||
peak_guessed: true,
|
||||
})
|
||||
);
|
||||
// White above the calibrated peak (a Samsung QD-OLED's 380, the slider at 456).
|
||||
@@ -13729,6 +13681,7 @@ mod tests {
|
||||
Some(Reported {
|
||||
paper_white: 456,
|
||||
peak: 456,
|
||||
peak_guessed: true,
|
||||
})
|
||||
);
|
||||
assert_eq!(os_reported(&info(Some(1000.0), None)), None);
|
||||
@@ -15419,11 +15372,9 @@ fn log_os_reported(reported: Option<crate::graphics::Reported>) {
|
||||
None => "Horizon renderer: the system doesn't report the display's luminance".into(),
|
||||
});
|
||||
}
|
||||
/// What the OS reports in absolute nits (Windows: DXGI's `MaxLuminance` and the SDR
|
||||
/// white level the brightness slider sets), which is HDR output's scale there: SDR
|
||||
/// white is Paper White, the display's peak is Peak Brightness. Both are kept within
|
||||
/// the settings' ranges. The OS's figures often come from the display's EDID and may
|
||||
/// run optimistic; the player's own value always wins.
|
||||
/// What the OS reports in absolute nits, HDR output's scale (Windows: DXGI's SDR white level,
|
||||
/// the brightness slider's, as Paper White, and its `MaxLuminance`, mostly the display's EDID
|
||||
/// guess, as Peak Brightness), kept within the settings' ranges. The player's values win.
|
||||
fn os_reported(info: &wgpu::DisplayHdrInfo) -> Option<crate::graphics::Reported> {
|
||||
let luminance = info.luminance?;
|
||||
let usable = |n: f32| n.is_finite() && n > 0.0;
|
||||
@@ -15435,6 +15386,7 @@ fn os_reported(info: &wgpu::DisplayHdrInfo) -> Option<crate::graphics::Reported>
|
||||
// Windows' SDR brightness slider can set white above the reported peak (480 against
|
||||
// a calibrated 380): the highlights then stay at white instead of dropping below it.
|
||||
peak: (peak.round() as u32).clamp(200, 10_000).max(paper_white),
|
||||
peak_guessed: true,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -15470,6 +15422,7 @@ fn reported(l: horizon_hdr::wayland::Luminances) -> crate::graphics::Reported {
|
||||
crate::graphics::Reported {
|
||||
paper_white: 203,
|
||||
peak: peak.clamp(200, 10_000),
|
||||
peak_guessed: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15718,11 +15671,11 @@ mod shadow_caster_tests {
|
||||
#[test]
|
||||
fn the_shadow_pass_draws_terrain_outside_the_pack_s_planes() {
|
||||
let kept = |near: Option<f32>, far: Option<f32>, y: f64, min: [f32; 3], depth: bool| {
|
||||
let shadow = horizon_render_pipeline::Shadow {
|
||||
let shadow = horizon_shaders::Shadow {
|
||||
distance: 128.0,
|
||||
near,
|
||||
far,
|
||||
..horizon_render_pipeline::Shadow::default()
|
||||
..horizon_shaders::Shadow::default()
|
||||
};
|
||||
let camera = DVec3::new(13.5, y + 1.62, -13.5);
|
||||
let view = pack_post::ShadowView {
|
||||
|
||||
@@ -338,7 +338,7 @@ pub struct NativeFrame {
|
||||
reflection_sampler: wgpu::Sampler,
|
||||
/// The shadow map the stages read, and the camera that draws it when the pack asks for one.
|
||||
pub shadow_map: super::native_shadow::ShadowMap,
|
||||
shadow_camera: Option<horizon_render_pipeline::Shadow>,
|
||||
shadow_camera: Option<horizon_shaders::Shadow>,
|
||||
/// Whether the pack's material reads `reflection` (`NativePack::new`): else no mirror
|
||||
/// is drawn and `has_reflection` stays 0.
|
||||
pub reflects: bool,
|
||||
@@ -1497,7 +1497,7 @@ pub fn frame_inputs(
|
||||
size: [u32; 2],
|
||||
seconds: f32,
|
||||
index: u32,
|
||||
shadow: Option<&horizon_render_pipeline::Shadow>,
|
||||
shadow: Option<&horizon_shaders::Shadow>,
|
||||
) -> FrameInputs {
|
||||
let aspect = size[0] as f32 / size[1].max(1) as f32;
|
||||
let view = crate::camera::view_matrix(frame.yaw, frame.pitch);
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
//! draw lists (`ShadowView`) serve both; the map's texels are distorted toward the camera as the
|
||||
//! prelude's `shadow_position` undoes.
|
||||
|
||||
use horizon_render_pipeline::Shadow;
|
||||
use horizon_shader_api::{Api, Frame as FrameInputs, manifest::Shadow as Settings, stages::GROUP};
|
||||
use horizon_shaders::Shadow;
|
||||
|
||||
use super::native_material::TerrainShape;
|
||||
use super::wgsl_prefix::{TERRAIN, prefixed, section};
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
//! `move_off_page`, moved out of `gfx.rs` unchanged.
|
||||
use super::*;
|
||||
|
||||
/// `Renderer::compact_arena`'s and `shrink_translucent`'s moves: what the meshes
|
||||
/// hold on `page` of `arena` (`part`) goes to its other pages in video memory,
|
||||
/// until the next range would pass `budget` bytes (it keeps what is left; the
|
||||
/// first range moves whatever its size, so none keeps the page closed), the
|
||||
/// copies into `encoder`, the ranges' flat draw ids (`flat`) and, in the
|
||||
/// section arena, GPU culling's records (`records`) along. False when the other
|
||||
/// pages have no room left.
|
||||
pub(super) fn move_off_page<'a>(
|
||||
arena: &mut arena::Arena,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
mut flat: Option<(&mut flat::Flat, &wgpu::Device, &mut upload::Uploads)>,
|
||||
mut records: Option<&mut gpu_culling::Records>,
|
||||
meshes: impl Iterator<Item = &'a mut SectionGpu>,
|
||||
page: u32,
|
||||
part: Part,
|
||||
budget: &mut u64,
|
||||
) -> bool {
|
||||
debug_assert!(
|
||||
records.is_none() || matches!(part, Part::Section { .. }),
|
||||
"records follow the section arena's ranges"
|
||||
);
|
||||
let mut moved = false;
|
||||
for s in meshes {
|
||||
// Nothing of a mesh here: split, one without solid quads (its (0, 0) is no
|
||||
// range of page 0); in the other arenas, one without translucent quads.
|
||||
let Some((_, start, vertices)) = part.of(s).filter(|r| r.0 == page) else {
|
||||
continue;
|
||||
};
|
||||
let bytes = vertices * arena.stride();
|
||||
if moved && bytes > *budget {
|
||||
break;
|
||||
}
|
||||
let Some(to) = arena.alloc_moved(vertices) else {
|
||||
return false;
|
||||
};
|
||||
arena.copy(encoder, (page, start), to, vertices);
|
||||
arena.release(page, start, vertices);
|
||||
if let (Some((flat, device, uploads)), Some(id)) = (&mut flat, s.flat_id) {
|
||||
let key = match part {
|
||||
Part::Translucent => to.0 + flat::TRANSLUCENT_KEYS,
|
||||
_ => to.0,
|
||||
};
|
||||
flat.place(device, encoder, uploads, id, key, to.1, vertices);
|
||||
}
|
||||
part.set(s, to);
|
||||
// Its solid quads start the range in either layout.
|
||||
if let (Part::Section { .. }, Some(records), Some(id)) =
|
||||
(part, records.as_deref_mut(), s.gpu_id)
|
||||
{
|
||||
records.relocate(id, to.0, u64::from(s.solid_base()));
|
||||
}
|
||||
*budget = budget.saturating_sub(bytes);
|
||||
moved = true;
|
||||
}
|
||||
true
|
||||
}
|
||||
@@ -300,6 +300,7 @@ pub(crate) mod tests {
|
||||
luminance: Some(Reported {
|
||||
paper_white: 400,
|
||||
peak: 1000,
|
||||
peak_guessed: false,
|
||||
}),
|
||||
hdr: system,
|
||||
},
|
||||
@@ -650,6 +651,7 @@ pub(crate) mod tests {
|
||||
window_reading.luminance = Some(Reported {
|
||||
paper_white: 300,
|
||||
peak: 800,
|
||||
peak_guessed: false,
|
||||
});
|
||||
assert_eq!(window_output(window_reading).nits[..2], [300.0, 800.0]);
|
||||
assert_eq!(
|
||||
|
||||
@@ -549,6 +549,7 @@ mod tests {
|
||||
reading.luminance = Some(Reported {
|
||||
paper_white: 250,
|
||||
peak: 600,
|
||||
peak_guessed: false,
|
||||
});
|
||||
// 150 nits under the reported paper white of 250: 0.6 of it.
|
||||
settings.hdr_ui_nits = 150;
|
||||
@@ -584,6 +585,7 @@ mod tests {
|
||||
reading.luminance = Some(Reported {
|
||||
paper_white,
|
||||
peak: 1000,
|
||||
peak_guessed: false,
|
||||
});
|
||||
let next = candidate(&settings, caps, reading, &published);
|
||||
let again = commit(&mut swapchain, &published, next, caps, &mut rejected);
|
||||
@@ -729,6 +731,7 @@ mod tests {
|
||||
luminance: (hdr == Some(true)).then_some(Reported {
|
||||
paper_white: 300,
|
||||
peak: 800,
|
||||
peak_guessed: false,
|
||||
}),
|
||||
hdr,
|
||||
};
|
||||
|
||||
@@ -4,8 +4,7 @@
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use horizon_render_pipeline::{Format, Kind};
|
||||
use horizon_shaders::{BufferObject, CustomImage};
|
||||
use horizon_shaders::{BufferObject, CustomImage, Format, Kind};
|
||||
|
||||
pub(super) struct PackImage {
|
||||
pub(super) def: CustomImage,
|
||||
|
||||
@@ -18,8 +18,7 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use glam::{DMat4, DVec3};
|
||||
use horizon_render_pipeline::{BUFFERS, Format, Kind, SHADOW_COLORS};
|
||||
use horizon_shaders::{Group, Post};
|
||||
use horizon_shaders::{BUFFERS, Format, Group, Kind, Post, SHADOW_COLORS};
|
||||
use smallvec::SmallVec;
|
||||
|
||||
use super::pack_draw::DrawUniforms;
|
||||
@@ -893,7 +892,7 @@ fn depth_linear(interface: &horizon_shaders::Interface, linear: u8) -> [f32; 4]
|
||||
}
|
||||
|
||||
fn source(name: &str, shadow_space: bool, shadow_pass: bool, watershadow: bool) -> Source {
|
||||
let shadow_color = horizon_render_pipeline::shadow_color(name);
|
||||
let shadow_color = horizon_shaders::shadow_color(name);
|
||||
let shadow_depth = match name {
|
||||
"shadowtex0" | "watershadow" | "shadowtex0HW" => Some(0),
|
||||
"shadowtex1" | "shadowtex1HW" => Some(1),
|
||||
@@ -912,7 +911,7 @@ fn source(name: &str, shadow_space: bool, shadow_pass: bool, watershadow: bool)
|
||||
};
|
||||
}
|
||||
}
|
||||
if shadow_space && horizon_render_pipeline::buffer_index(name).is_some() {
|
||||
if shadow_space && horizon_shaders::buffer_index(name).is_some() {
|
||||
// colortex buffers are not this pass's.
|
||||
return Source::Dummy;
|
||||
}
|
||||
@@ -926,7 +925,7 @@ fn source(name: &str, shadow_space: bool, shadow_pass: bool, watershadow: bool)
|
||||
"normals" => Source::Normals,
|
||||
"specular" => Source::Specular,
|
||||
horizon_shaders::WATER_MASK => Source::WaterMask,
|
||||
n => horizon_render_pipeline::buffer_index(n).map_or(Source::Dummy, Source::Color),
|
||||
n => horizon_shaders::buffer_index(n).map_or(Source::Dummy, Source::Color),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1583,7 +1582,7 @@ fn bindings(
|
||||
!unbound
|
||||
&& (t.stage != pack_textures::stage(group)
|
||||
|| released.0 != t.stage
|
||||
|| horizon_render_pipeline::buffer_index(&t.name)
|
||||
|| horizon_shaders::buffer_index(&t.name)
|
||||
.is_none_or(|b| released.1 & 1 << b == 0))
|
||||
});
|
||||
let image = pack
|
||||
@@ -2433,7 +2432,7 @@ impl PackPost {
|
||||
let shadow_config = post.config.shadow.clone();
|
||||
let noise_size = post.config.noise_resolution.unwrap_or(256);
|
||||
let half_lives = post.config.half_lives;
|
||||
for (name, ticks) in horizon_render_pipeline::HALF_LIVES.iter().zip(half_lives) {
|
||||
for (name, ticks) in horizon_shaders::HALF_LIVES.iter().zip(half_lives) {
|
||||
if let Some(t) = ticks {
|
||||
notes.push(format!("`{name}` is {t} ticks"));
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
//! a fit to one driver's values, not a rule Iris states. `RGBA4` and `RGBA2` read back the `RGBA8`
|
||||
//! bytes in the same probe, as Horizon's 8-bit buffers already do.
|
||||
|
||||
use horizon_render_pipeline::{Format, Kind};
|
||||
use horizon_shaders::{Format, Kind};
|
||||
use naga::{BinaryOperator, Expression, Literal, MathFunction, Statement, VectorSize};
|
||||
|
||||
/// The bits each channel keeps in a buffer of format `f`, for the formats that keep fewer than
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
//! with two sides that `shadowcomp` programs flip as composite programs flip colortex (6.1.6).
|
||||
//! Square, `shadowMapResolution` pixels.
|
||||
|
||||
use horizon_render_pipeline::SHADOW_COLORS;
|
||||
use horizon_shaders::SHADOW_COLORS;
|
||||
|
||||
type Ops<T> = wgpu::Operations<T>;
|
||||
type Color = wgpu::Color;
|
||||
@@ -97,11 +97,7 @@ pub(super) fn stand_in(device: &wgpu::Device) -> wgpu::TextureView {
|
||||
}
|
||||
|
||||
impl ShadowTargets {
|
||||
pub fn reset_history(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
shadow: &horizon_render_pipeline::Shadow,
|
||||
) {
|
||||
pub fn reset_history(&mut self, device: &wgpu::Device, shadow: &horizon_shaders::Shadow) {
|
||||
let mut colors = 0;
|
||||
let mut doubled = 0;
|
||||
let formats = std::array::from_fn(|n| match &self.color[n] {
|
||||
@@ -129,7 +125,7 @@ impl ShadowTargets {
|
||||
/// resolution is kept within the device's limit.
|
||||
pub fn new(
|
||||
device: &wgpu::Device,
|
||||
shadow: &horizon_render_pipeline::Shadow,
|
||||
shadow: &horizon_shaders::Shadow,
|
||||
formats: [wgpu::TextureFormat; SHADOW_COLORS],
|
||||
colors: u16,
|
||||
doubled: u16,
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
//! A shader pack's custom textures (`texture.<stage>.<name>`, `texture.noise`) on the GPU, and
|
||||
//! which of them a program's sampler reads.
|
||||
|
||||
use horizon_render_pipeline::Kind;
|
||||
use horizon_shaders::{CustomTexture, Group, Texels, Texture, TextureDim, TextureSource};
|
||||
use horizon_shaders::{CustomTexture, Group, Kind, Texels, Texture, TextureDim, TextureSource};
|
||||
|
||||
/// What a custom texture is once uploaded: an image of ours, or one the game already has.
|
||||
pub(super) enum Image {
|
||||
@@ -119,12 +118,12 @@ pub(super) fn find(textures: &[PackTexture], group: Group, ty: &str, name: &str)
|
||||
} else {
|
||||
wgpu::TextureViewDimension::D2
|
||||
};
|
||||
let buffer = horizon_render_pipeline::buffer_index(name);
|
||||
let buffer = horizon_shaders::buffer_index(name);
|
||||
let staged = textures.iter().position(|t| {
|
||||
t.stage == stage(group)
|
||||
&& t.dimension == dimension
|
||||
&& (t.name == name
|
||||
|| buffer.is_some() && horizon_render_pipeline::buffer_index(&t.name) == buffer)
|
||||
|| buffer.is_some() && horizon_shaders::buffer_index(&t.name) == buffer)
|
||||
});
|
||||
staged.or_else(|| {
|
||||
textures.iter().position(|t| {
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
//! [-w, w], and the depth buffers packs read hold GL window depth.
|
||||
|
||||
use glam::{DMat4, DVec3, Mat4, Vec3, Vec4};
|
||||
use horizon_render_pipeline::Shadow;
|
||||
use horizon_shaders::Shadow;
|
||||
|
||||
use super::Frame;
|
||||
|
||||
@@ -440,7 +440,7 @@ pub struct FrameUniforms {
|
||||
center_depth: Option<f32>,
|
||||
/// The pack's shadow settings (7.1).
|
||||
pub shadow: Shadow,
|
||||
/// The pack's smoothing half-lives in ticks (`horizon_render_pipeline::HALF_LIVES`).
|
||||
/// The pack's smoothing half-lives in ticks (`horizon_shaders::HALF_LIVES`).
|
||||
pub half_lives: [Option<f32>; 4],
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,22 @@ fn marker_driver(text: &str) -> &str {
|
||||
pub struct Reported {
|
||||
pub paper_white: u32,
|
||||
pub peak: u32,
|
||||
/// The peak is the display's own claim (DXGI's `MaxLuminance`, mostly its EDID figure),
|
||||
/// which often runs high: Auto still uses it, but the HDR page asks to calibrate.
|
||||
pub peak_guessed: bool,
|
||||
}
|
||||
|
||||
/// Whether HDR is set up for the display, and whether the player kept the system's setup.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum HdrSetup {
|
||||
/// The system says nothing: the player calibrates.
|
||||
Unknown,
|
||||
/// The system says, but only guesses the peak: the player calibrates.
|
||||
Guessed,
|
||||
/// Peak Brightness and Paper White follow the system.
|
||||
Automatic,
|
||||
/// The system said, and the player changed them anyway.
|
||||
Changed,
|
||||
}
|
||||
|
||||
/// The top of Peak Brightness's scale: `peak_nits_at(PEAK_STEPS)` is PQ's 10 000 nits.
|
||||
@@ -1207,6 +1223,15 @@ impl Graphics {
|
||||
n => n,
|
||||
}
|
||||
}
|
||||
/// How HDR is set up for the display the system `reported`.
|
||||
pub fn hdr_setup(&self, reported: Option<Reported>) -> HdrSetup {
|
||||
match reported {
|
||||
None => HdrSetup::Unknown,
|
||||
Some(_) if self.hdr_peak != 0 || self.hdr_paper_white != 0 => HdrSetup::Changed,
|
||||
Some(r) if r.peak_guessed => HdrSetup::Guessed,
|
||||
Some(_) => HdrSetup::Automatic,
|
||||
}
|
||||
}
|
||||
/// The black level, in nits. Not the system's: files from before saved 0 for that
|
||||
/// (Auto), and a reported black would lift every display's shadows by default.
|
||||
pub fn black_level_nits(&self) -> f32 {
|
||||
@@ -1320,6 +1345,7 @@ mod tests {
|
||||
let reported = Some(Reported {
|
||||
paper_white: 300,
|
||||
peak: 1000,
|
||||
peak_guessed: false,
|
||||
});
|
||||
assert_eq!(g.ui_nits(reported), 300);
|
||||
g.hdr_paper_white = 120;
|
||||
@@ -1328,6 +1354,33 @@ mod tests {
|
||||
assert_eq!((g.ui_nits(None), g.ui_nits(reported)), (400, 400));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hdr_setup_asks_to_calibrate_where_the_peak_is_guessed() {
|
||||
let wayland = Reported {
|
||||
paper_white: 203,
|
||||
peak: 1015,
|
||||
peak_guessed: false,
|
||||
};
|
||||
let windows = Reported {
|
||||
paper_white: 400,
|
||||
peak: 1000,
|
||||
peak_guessed: true,
|
||||
};
|
||||
let mut g = Graphics::default();
|
||||
assert_eq!(g.hdr_setup(None), HdrSetup::Unknown);
|
||||
assert_eq!(g.hdr_setup(Some(wayland)), HdrSetup::Automatic);
|
||||
assert_eq!(g.hdr_setup(Some(windows)), HdrSetup::Guessed);
|
||||
// Auto still takes the guess, over the 1000-nit default.
|
||||
assert_eq!(g.peak_nits(Some(windows)), 1000);
|
||||
assert_eq!(g.paper_white_nits(Some(windows)), 400);
|
||||
for (peak, paper_white) in [(800, 0), (0, 240)] {
|
||||
(g.hdr_peak, g.hdr_paper_white) = (peak, paper_white);
|
||||
assert_eq!(g.hdr_setup(Some(windows)), HdrSetup::Changed);
|
||||
assert_eq!(g.hdr_setup(Some(wayland)), HdrSetup::Changed);
|
||||
assert_eq!(g.hdr_setup(None), HdrSetup::Unknown);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hdr_modes_keep_boolean_files_and_round_trip_auto() {
|
||||
assert_eq!(Graphics::parse("").hdr, HdrMode::Off);
|
||||
|
||||
@@ -8,7 +8,8 @@ use crate::{
|
||||
gfx::EffectiveOutput,
|
||||
graphics::{
|
||||
EntityCullingMode, FarDetail, FrameGeneration, GpuCullingMode, GraphicsApi, HdrMode,
|
||||
LeafCullingMode, Overlay, Pacing, ParticleCullingMode, PresentMode, Preset, Upscaling,
|
||||
HdrSetup as Setup, LeafCullingMode, Overlay, Pacing, ParticleCullingMode, PresentMode,
|
||||
Preset, Upscaling,
|
||||
},
|
||||
graphics_style::Cost,
|
||||
lang,
|
||||
@@ -675,28 +676,9 @@ const LOD_STEPS: [u32; 24] = [
|
||||
crate::lod::MAX_CHUNKS,
|
||||
];
|
||||
|
||||
/// Whether the system set HDR up for the display, and whether the player kept it.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum Setup {
|
||||
/// The system says nothing: the player calibrates.
|
||||
Unknown,
|
||||
/// Peak Brightness and Paper White follow the system.
|
||||
Automatic,
|
||||
/// The system said, and the player changed them anyway.
|
||||
Changed,
|
||||
}
|
||||
/// `out`: the output of the renderer the settings are shown for, with what its display reports.
|
||||
fn setup(s: &Settings, out: &EffectiveOutput) -> Setup {
|
||||
setup_of(&s.horizon, out.reported.is_some())
|
||||
}
|
||||
fn setup_of(g: &crate::graphics::Graphics, reported: bool) -> Setup {
|
||||
if !reported {
|
||||
Setup::Unknown
|
||||
} else if g.hdr_peak == 0 && g.hdr_paper_white == 0 {
|
||||
Setup::Automatic
|
||||
} else {
|
||||
Setup::Changed
|
||||
}
|
||||
s.horizon.hdr_setup(out.reported)
|
||||
}
|
||||
/// What the HDR page says while HDR output is held back on this window's graphics driver
|
||||
/// (`EffectiveOutput::held`): picking a mode tries again (`H::cycle`).
|
||||
@@ -714,13 +696,19 @@ fn description(
|
||||
(Item::Horizon(H::HdrCalibrate), Setup::Unknown) => {
|
||||
"Your display didn't say how bright it gets: calibrate to set it up."
|
||||
}
|
||||
(Item::Horizon(H::HdrCalibrate), Setup::Guessed) => {
|
||||
"Your display's reported peak is often too high: calibrate to set it up."
|
||||
}
|
||||
(Item::Horizon(H::HdrCalibrate), Setup::Automatic) => {
|
||||
"Already set up for your display. Only calibrate if HDR looks wrong."
|
||||
}
|
||||
(Item::Horizon(H::HdrCalibrate), Setup::Changed) => {
|
||||
"Puts Peak Brightness and Paper White back to your display's setup."
|
||||
}
|
||||
(Item::Horizon(H::HdrPeak | H::HdrPaperWhite), Setup::Automatic | Setup::Changed) => {
|
||||
// A guessed peak's Auto is what calibration replaced; paper white is the player's own.
|
||||
(Item::Horizon(h @ (H::HdrPeak | H::HdrPaperWhite)), Setup::Automatic | Setup::Changed)
|
||||
if h == H::HdrPaperWhite || out.reported.is_some_and(|r| !r.peak_guessed) =>
|
||||
{
|
||||
"Set up for your display. Leave on Auto unless it looks wrong."
|
||||
}
|
||||
(Item::Horizon(H::GpuCulling), _) if s.horizon.gpu_culling == GpuCullingMode::Auto => {
|
||||
@@ -944,7 +932,7 @@ impl H {
|
||||
H::FrameGeneration => labelled("Frame Generation", g.frame_generation.label()),
|
||||
H::Hdr => labelled("HDR", g.hdr.label()),
|
||||
H::HdrCalibrate => Text::literal(lang::get(match setup(s, out) {
|
||||
Setup::Unknown => "Calibrate...",
|
||||
Setup::Unknown | Setup::Guessed => "Calibrate...",
|
||||
Setup::Automatic => "Calibrate Anyway...",
|
||||
Setup::Changed => "Use Automatic Setup",
|
||||
})),
|
||||
|
||||
@@ -120,14 +120,86 @@ fn auto_levels_and_setup_read_their_own_renderers_report() {
|
||||
assert_eq!(label(H::HdrCalibrate, &silent), "Calibrate...");
|
||||
}
|
||||
|
||||
/// The setup rows' label and description for each kind of report: none, a compositor's
|
||||
/// (Wayland), and Windows', whose peak is the display's own guess.
|
||||
#[test]
|
||||
fn hdr_setup_follows_what_the_system_reported() {
|
||||
let mut g = crate::graphics::Graphics::default();
|
||||
assert_eq!(setup_of(&g, false), Setup::Unknown);
|
||||
assert_eq!(setup_of(&g, true), Setup::Automatic);
|
||||
g.hdr_paper_white = 80;
|
||||
assert_eq!(setup_of(&g, true), Setup::Changed);
|
||||
assert_eq!(setup_of(&g, false), Setup::Unknown);
|
||||
fn the_setup_rows_ask_to_calibrate_unless_the_system_set_the_display_up() {
|
||||
let trusted = offering(true, Some(true));
|
||||
let mut guessed = trusted;
|
||||
guessed.reported = guessed.reported.map(|r| crate::graphics::Reported {
|
||||
peak_guessed: true,
|
||||
..r
|
||||
});
|
||||
let rows = |s: &Settings, out: &EffectiveOutput| {
|
||||
[H::HdrCalibrate, H::HdrPeak, H::HdrPaperWhite].map(|h| {
|
||||
let item = Item::Horizon(h);
|
||||
(item.label(s, out).plain(), description(item, s, out, "-"))
|
||||
})
|
||||
};
|
||||
let calibrate = "Calibrate...";
|
||||
let set_up = "Set up for your display. Leave on Auto unless it looks wrong.";
|
||||
let mut s = Settings::default();
|
||||
s.horizon.hdr = HdrMode::On;
|
||||
let none = (
|
||||
calibrate.into(),
|
||||
"Your display didn't say how bright it gets: calibrate to set it up.",
|
||||
);
|
||||
assert_eq!(
|
||||
rows(&s, &NONE),
|
||||
[
|
||||
none.clone(),
|
||||
("Peak Brightness: Auto (1000)".into(), "-"),
|
||||
("Paper White: Auto (203)".into(), "-"),
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
rows(&s, &guessed),
|
||||
[
|
||||
(
|
||||
calibrate.into(),
|
||||
"Your display's reported peak is often too high: calibrate to set it up."
|
||||
),
|
||||
("Peak Brightness: Auto (1000)".into(), "-"),
|
||||
("Paper White: Auto (400)".into(), "-"),
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
rows(&s, &trusted),
|
||||
[
|
||||
(
|
||||
"Calibrate Anyway...".into(),
|
||||
"Already set up for your display. Only calibrate if HDR looks wrong."
|
||||
),
|
||||
("Peak Brightness: Auto (1000)".into(), set_up),
|
||||
("Paper White: Auto (400)".into(), set_up),
|
||||
]
|
||||
);
|
||||
// Calibrated or set by hand, either report offers its automatic setup back.
|
||||
s.horizon.hdr_peak = 800;
|
||||
// Peak's Auto stays a guess on Windows: only Paper White is said to be set up there.
|
||||
let changed = |peak| {
|
||||
[
|
||||
(
|
||||
"Use Automatic Setup".into(),
|
||||
"Puts Peak Brightness and Paper White back to your display's setup.",
|
||||
),
|
||||
("Peak Brightness: 800".into(), peak),
|
||||
("Paper White: Auto (400)".into(), set_up),
|
||||
]
|
||||
};
|
||||
assert_eq!(rows(&s, &guessed), changed("-"));
|
||||
assert_eq!(rows(&s, &trusted), changed(set_up));
|
||||
assert_eq!(rows(&s, &NONE)[0], none);
|
||||
let mut ui = ui();
|
||||
ui.screen.output = guessed;
|
||||
let at = |[x, y, ..]: [f32; 4]| (x + 8.0, y + 4.0);
|
||||
let calibrate = row(&mut ui, &s, Item::Horizon(H::HdrCalibrate));
|
||||
assert_eq!(press(&mut ui, &mut s, at(calibrate)), [CLICK, CHANGED]);
|
||||
assert_eq!((s.horizon.hdr_peak, s.horizon.hdr_paper_white), (0, 0));
|
||||
assert_eq!(
|
||||
press(&mut ui, &mut s, at(calibrate)),
|
||||
[CLICK, Output::Push(OptionsPage::HdrCalibration)]
|
||||
);
|
||||
}
|
||||
|
||||
/// UI Brightness reads as Paper White does, in nits apart from it: Auto shows the paper white
|
||||
|
||||
@@ -67,7 +67,6 @@ mod jei_sprites;
|
||||
pub mod jigsaw_block;
|
||||
pub mod keypad;
|
||||
pub mod terrain_model;
|
||||
pub use horizon_polytone::colors as polytone_colors;
|
||||
pub use horizon_polytone::creative_tabs as polytone_creative_tabs;
|
||||
pub use horizon_world::json_folder;
|
||||
pub mod level_loading;
|
||||
|
||||
@@ -2593,8 +2593,7 @@ impl Lod {
|
||||
self.returned_at = std::time::Instant::now();
|
||||
let nodes = &self.plan.nodes;
|
||||
self.mesh_samples.retain(|key| nodes.contains(key));
|
||||
// Queued tiles the view left are not sampled (a running one is
|
||||
// dropped when it finishes, `finish_sample`).
|
||||
// Queued tiles the view left are not sampled; a running one is dropped (`finish_sample`).
|
||||
drop_queued_samples(&self.shared, &mut self.pending, |key| {
|
||||
self.plan.nodes.contains(key)
|
||||
});
|
||||
@@ -2680,6 +2679,7 @@ impl Lod {
|
||||
}
|
||||
if let Some((columns, mask)) = self.kept.take(&key) {
|
||||
self.ready.insert(key, columns);
|
||||
self.sync_sample(key);
|
||||
if let Some(mask) = mask {
|
||||
self.detail_masks.insert(key, mask);
|
||||
}
|
||||
|
||||
@@ -339,6 +339,7 @@ impl Opt {
|
||||
Opt::SharePresence => "options.sharePresence",
|
||||
Opt::DirectionalAudio => "options.directionalAudio",
|
||||
Opt::MusicToast => "options.musicToast",
|
||||
Opt::Page(page, row) => pages::slider_caption(page, row),
|
||||
}
|
||||
}
|
||||
pub(crate) fn kind(self, s: &Settings) -> Kind {
|
||||
@@ -402,6 +403,7 @@ impl Opt {
|
||||
Opt::PadLookDeadzone => unit(s.controller.look_deadzone, 0.0, 0.5),
|
||||
Opt::PadTriggerThreshold => unit(s.controller.trigger_threshold, 0.1, 0.9),
|
||||
Opt::SprintWindow => unit(s.sprint_window as f32, 0.0, 10.0),
|
||||
Opt::Page(page, row) => pages::slider_kind(page, row),
|
||||
_ => Kind::Cycle,
|
||||
}
|
||||
}
|
||||
@@ -878,6 +880,7 @@ impl Opt {
|
||||
gamepad::TextKeyboard::Horizon => "horizon.options.keyboard.horizon",
|
||||
gamepad::TextKeyboard::Steam => "horizon.options.keyboard.steam",
|
||||
}),
|
||||
Opt::Page(page, row) => pages::slider_text(page, row),
|
||||
_ => Text::empty(),
|
||||
}
|
||||
}
|
||||
@@ -952,6 +955,7 @@ impl Opt {
|
||||
Opt::PadLookDeadzone => s.controller.look_deadzone = step(0.0, 50.0) / 100.0,
|
||||
Opt::PadTriggerThreshold => s.controller.trigger_threshold = step(10.0, 90.0) / 100.0,
|
||||
Opt::SprintWindow => s.sprint_window = step(0.0, 10.0) as i32,
|
||||
Opt::Page(page, row) => pages::slide(page, row, v),
|
||||
_ => {}
|
||||
}
|
||||
if self.affects_preset() {
|
||||
@@ -1331,6 +1335,8 @@ pub enum Page {
|
||||
ModProject,
|
||||
/// A native mod's settings page: its index in `mods.installed`.
|
||||
NativeMod(usize),
|
||||
/// A page a crate registered (`horizon_hooks::pages`): its index in `OptionsUi::pages`.
|
||||
Added(usize),
|
||||
/// Horizon's shader packs, either kind (`shader_pack_list`).
|
||||
ShaderPacks,
|
||||
/// The picked pack's options: the shader screen (`shader_ui`) at the pack's main screen.
|
||||
@@ -1390,6 +1396,8 @@ enum Act {
|
||||
/// A native mod's key (mod, key) waits for a key; a button of its page.
|
||||
NativeKey(usize, usize),
|
||||
NativeButton(usize, usize),
|
||||
/// A registered page's row: (page, row).
|
||||
PageRow(usize, usize),
|
||||
OpenModFolder,
|
||||
/// Browse mods: the page of listing `.0` of `mod_browser`.
|
||||
ModProject(usize),
|
||||
@@ -1617,7 +1625,8 @@ pub struct OptionsUi {
|
||||
/// Key Binds: the mapping waiting for a key ([`NATIVE_KEY`]: a native
|
||||
/// mod's, `selected_native_key`).
|
||||
pub selected_key: Option<&'static str>,
|
||||
/// The native mod's key waiting: (mod, key) in `mods.installed`.
|
||||
/// The native mod's key waiting: (mod, key) in `mods.installed`, or a registered page's
|
||||
/// (page, row) ([`pages::PAGE_KEY`]).
|
||||
selected_native_key: Option<(usize, usize)>,
|
||||
/// Bindings: the action waiting for a pad button.
|
||||
pub selected_pad_action: Option<&'static str>,
|
||||
@@ -1861,25 +1870,6 @@ fn wide_extras(s: &Settings, width: impl Fn(&str) -> f32) -> Vec<crate::extras::
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
/// What a button opening one of Horizon's pages says on hover.
|
||||
fn horizon_page_tooltip(page: Page) -> Option<&'static str> {
|
||||
Some(match page {
|
||||
Page::Horizon => "horizon.pages.settings.tooltip",
|
||||
Page::Studio => "horizon.pages.studio.tooltip",
|
||||
Page::Graphics => "horizon.pages.graphics.tooltip",
|
||||
Page::GraphicsAdvanced => "horizon.pages.graphics_advanced.tooltip",
|
||||
Page::Controller => "horizon.pages.controller.tooltip",
|
||||
Page::Extras => "horizon.pages.extras.tooltip",
|
||||
Page::Pvp => "horizon.pages.pvp.tooltip",
|
||||
Page::Agent => "horizon.pages.agent.tooltip",
|
||||
Page::SplitProfiles => "horizon.pages.split_profiles.tooltip",
|
||||
Page::ShaderPacks => "horizon.pages.shader_packs.tooltip",
|
||||
Page::Mods => "horizon.pages.mods.tooltip",
|
||||
Page::SplitSetup => "horizon.pages.split_setup.tooltip",
|
||||
Page::Music => "horizon.pages.music.tooltip",
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
/// Rows of an OptionsList: (big?, options) or a header.
|
||||
enum Row {
|
||||
Big(Opt),
|
||||
@@ -1899,14 +1889,17 @@ fn tooltip_text(s: &Settings, wd: &Widget) -> Option<Text> {
|
||||
return None;
|
||||
};
|
||||
let key = opt.tooltip_key(s);
|
||||
return lang::has(&key).then(|| tr(&key));
|
||||
let Opt::Page(page, row) = opt else {
|
||||
return lang::has(&key).then(|| tr(&key));
|
||||
};
|
||||
return pages::tooltip(*page, *row);
|
||||
};
|
||||
let mut tip = None;
|
||||
if let Act::Extra(item) = act {
|
||||
tip = Some(Text::literal(item.description_text()));
|
||||
}
|
||||
if let Act::Push(page) = act
|
||||
&& let Some(text) = horizon_page_tooltip(*page)
|
||||
&& let Some(text) = pages::horizon_page_tooltip(*page)
|
||||
{
|
||||
tip = Some(tr(text));
|
||||
}
|
||||
@@ -1926,6 +1919,7 @@ fn tooltip_text(s: &Settings, wd: &Widget) -> Option<Text> {
|
||||
}
|
||||
match act {
|
||||
Act::Cycle(opt) if !opt.available() => Some(tr("horizon.options.unavailable")),
|
||||
Act::PageRow(page, row) => pages::tooltip(*page, *row),
|
||||
Act::ToggleMod(i) => match s.mods.installed.get(*i) {
|
||||
Some(installed) => {
|
||||
let description = installed
|
||||
@@ -2367,6 +2361,7 @@ impl OptionsUi {
|
||||
Page::ModBrowser => "horizon.mods.browse.title",
|
||||
Page::ModProject => "horizon.mods.browse.project",
|
||||
Page::NativeMod(_) => "Mod Settings",
|
||||
Page::Added(i) => pages::title(i),
|
||||
Page::ShaderPacks => "horizon.pages.shader_packs",
|
||||
Page::ShaderOptions => "horizon.pages.shader_options",
|
||||
Page::PackOptions => "horizon.pack.options.title",
|
||||
@@ -2647,47 +2642,7 @@ impl OptionsUi {
|
||||
flush(&mut rows, &mut section);
|
||||
rows
|
||||
}
|
||||
Page::Horizon => {
|
||||
// Two to a row, as vanilla's OptionsList.addSmall lays out
|
||||
// buttons; each opens one of Horizon's settings pages. Each page has
|
||||
// one way in: graphics and shader packs are Video Settings' (the graphics
|
||||
// screen), the controller is Controls', mods are the Options screen's.
|
||||
let open =
|
||||
|key: &str, page: Page| button(0.0, 0.0, 150.0, tr(key), Act::Push(page));
|
||||
vec![
|
||||
Row::Buttons(
|
||||
open("horizon.pages.open.extras", Page::Extras),
|
||||
Some(open("horizon.pages.open.pvp", Page::Pvp)),
|
||||
),
|
||||
Row::Buttons(
|
||||
open(
|
||||
"horizon.pages.open.split_setup",
|
||||
if self.split_guest {
|
||||
Page::SplitProfiles
|
||||
} else {
|
||||
Page::SplitSetup
|
||||
},
|
||||
),
|
||||
Some(open("horizon.pages.open.studio", Page::Studio)),
|
||||
),
|
||||
// No AI hides the AI Agent page and everything AI.
|
||||
Row::Buttons(
|
||||
self.extra_button(s, crate::extras::Item::DiscordPresence),
|
||||
Some(if s.extras.no_ai {
|
||||
self.extra_button(s, crate::extras::Item::NoAi)
|
||||
} else {
|
||||
open("horizon.pages.open.agent", Page::Agent)
|
||||
}),
|
||||
),
|
||||
]
|
||||
.into_iter()
|
||||
.chain(
|
||||
(!s.extras.no_ai).then(|| {
|
||||
Row::Buttons(self.extra_button(s, crate::extras::Item::NoAi), None)
|
||||
}),
|
||||
)
|
||||
.collect()
|
||||
}
|
||||
Page::Horizon => self.horizon_rows(s),
|
||||
Page::Agent => {
|
||||
let mut rows = vec![Row::Buttons(
|
||||
self.extra_button(s, crate::extras::Item::AgentControl),
|
||||
@@ -2970,6 +2925,7 @@ impl OptionsUi {
|
||||
Page::ModBrowser => self.mod_browser_rows(),
|
||||
Page::ModProject => self.mod_project_rows(s),
|
||||
Page::NativeMod(i) => self.native_rows(s, i),
|
||||
Page::Added(i) => self.added_rows(i),
|
||||
_ => {
|
||||
let _ = s;
|
||||
Vec::new()
|
||||
@@ -4238,6 +4194,7 @@ impl OptionsUi {
|
||||
}
|
||||
Act::NativeKey(i, k) => self.wait_native_key(i, k),
|
||||
Act::NativeButton(i, b) => events.push(Event::NativeButton(i, b)),
|
||||
Act::PageRow(p, r) => events.extend(self.page_act(p, r, self.shift)),
|
||||
Act::OpenModFolder => events.push(Event::OpenModFolder),
|
||||
Act::ModProject(i) => {
|
||||
if let Some(listing) = self.mod_browser.listings.get(i) {
|
||||
@@ -4741,8 +4698,8 @@ impl OptionsUi {
|
||||
let Some(name) = self.selected_key.take() else {
|
||||
return Vec::new();
|
||||
};
|
||||
if name == NATIVE_KEY {
|
||||
return self.bind_native_key(s, value);
|
||||
if name == NATIVE_KEY || name == pages::PAGE_KEY {
|
||||
return self.bind_native_key(s, name, value);
|
||||
}
|
||||
if !s.extras.set_key(name, value.clone()) {
|
||||
s.keys.set(name, &value);
|
||||
@@ -5126,6 +5083,7 @@ impl OptionsUi {
|
||||
}
|
||||
Act::NativeKey(i, k) => self.wait_native_key(i, k),
|
||||
Act::NativeButton(i, b) => events.push(Event::NativeButton(i, b)),
|
||||
Act::PageRow(p, r) => events.extend(self.page_act(p, r, shift)),
|
||||
Act::OpenModFolder => events.push(Event::OpenModFolder),
|
||||
Act::ModProject(i) => {
|
||||
if let Some(listing) = self.mod_browser.listings.get(i) {
|
||||
|
||||
@@ -1,8 +1,230 @@
|
||||
//! The pages crates add to the options screens: a native mod's settings page (its keys and
|
||||
//! buttons). Kept out of options_ui.rs, which may not grow (tools/check_file_size.py).
|
||||
//! buttons), and the pages crates above the engine register (`horizon_hooks::pages`, docs/layers.md
|
||||
//! "Platform registries") with their entries on Horizon Settings. Kept out of options_ui.rs, which
|
||||
//! may not grow (tools/check_file_size.py).
|
||||
use super::*;
|
||||
use horizon_hooks::pages as added;
|
||||
|
||||
/// `selected_key` while a registered page's key waits (`selected_native_key`: page, row).
|
||||
pub(super) const PAGE_KEY: &str = "horizon.pageKey";
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// A test's own pages, instead of the game's registered ones: each test runs on its thread.
|
||||
static TEST_PAGES: std::cell::Cell<Option<&'static [&'static dyn added::Page]>> =
|
||||
const { std::cell::Cell::new(None) };
|
||||
}
|
||||
|
||||
/// The pages crates registered.
|
||||
fn registered() -> &'static [&'static dyn added::Page] {
|
||||
#[cfg(test)]
|
||||
if let Some(pages) = TEST_PAGES.get() {
|
||||
return pages;
|
||||
}
|
||||
added::engine::all()
|
||||
}
|
||||
|
||||
/// A registered page's title (a translation key).
|
||||
pub(super) fn title(page: usize) -> &'static str {
|
||||
registered().get(page).map_or("", |p| p.title())
|
||||
}
|
||||
|
||||
/// Row `r` of a registered page, as it is now.
|
||||
fn row(page: usize, r: usize) -> Option<added::Row> {
|
||||
let mut rows = Vec::new();
|
||||
registered().get(page)?.rows(&mut rows);
|
||||
rows.into_iter().nth(r)
|
||||
}
|
||||
|
||||
/// A registered page's slider ([`Opt::Page`]): its caption (a translation key).
|
||||
pub(super) fn slider_caption(page: usize, r: usize) -> &'static str {
|
||||
match row(page, r) {
|
||||
Some(added::Row::Slider { label, .. }) => label,
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
/// A registered page's slider: its position.
|
||||
pub(super) fn slider_kind(page: usize, r: usize) -> Kind {
|
||||
match row(page, r) {
|
||||
Some(added::Row::Slider { value, .. }) => Kind::Slider(value.clamp(0.0, 1.0)),
|
||||
_ => Kind::Cycle,
|
||||
}
|
||||
}
|
||||
|
||||
/// A registered page's slider: the value its message shows after the caption.
|
||||
pub(super) fn slider_text(page: usize, r: usize) -> Text {
|
||||
match row(page, r) {
|
||||
Some(added::Row::Slider { text, .. }) => label_text(text),
|
||||
_ => Text::empty(),
|
||||
}
|
||||
}
|
||||
|
||||
/// A registered page's slider moved.
|
||||
pub(super) fn slide(page: usize, r: usize, value: f32) {
|
||||
if let Some(registered) = registered().get(page) {
|
||||
registered.act(r, added::Act::Slide(value));
|
||||
}
|
||||
}
|
||||
|
||||
/// What a registered page's row says on hover.
|
||||
pub(super) fn tooltip(page: usize, r: usize) -> Option<Text> {
|
||||
registered().get(page)?.tooltip(r).map(label_text)
|
||||
}
|
||||
|
||||
/// What a button opening one of Horizon's pages says on hover.
|
||||
pub(super) fn horizon_page_tooltip(page: Page) -> Option<&'static str> {
|
||||
Some(match page {
|
||||
Page::Horizon => "horizon.pages.settings.tooltip",
|
||||
Page::Studio => "horizon.pages.studio.tooltip",
|
||||
Page::Graphics => "horizon.pages.graphics.tooltip",
|
||||
Page::GraphicsAdvanced => "horizon.pages.graphics_advanced.tooltip",
|
||||
Page::Controller => "horizon.pages.controller.tooltip",
|
||||
Page::Extras => "horizon.pages.extras.tooltip",
|
||||
Page::Pvp => "horizon.pages.pvp.tooltip",
|
||||
Page::Agent => "horizon.pages.agent.tooltip",
|
||||
Page::SplitProfiles => "horizon.pages.split_profiles.tooltip",
|
||||
Page::ShaderPacks => "horizon.pages.shader_packs.tooltip",
|
||||
Page::Mods => "horizon.pages.mods.tooltip",
|
||||
Page::SplitSetup => "horizon.pages.split_setup.tooltip",
|
||||
Page::Music => "horizon.pages.music.tooltip",
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
fn label_text(label: added::Label) -> Text {
|
||||
match label {
|
||||
added::Label::Key(key) => tr(key),
|
||||
added::Label::Value(value) => Text::literal(value),
|
||||
}
|
||||
}
|
||||
|
||||
impl OptionsUi {
|
||||
/// Horizon Settings: its pages, then the pages crates registered for it.
|
||||
pub(super) fn horizon_rows(&self, s: &Settings) -> Vec<Row> {
|
||||
// Two to a row, as vanilla's OptionsList.addSmall lays out
|
||||
// buttons; each opens one of Horizon's settings pages. Each page has
|
||||
// one way in: graphics and shader packs are Video Settings' (the graphics
|
||||
// screen), the controller is Controls', mods are the Options screen's.
|
||||
let open = |key: &str, page: Page| button(0.0, 0.0, 150.0, tr(key), Act::Push(page));
|
||||
let mut rows: Vec<Row> = vec![
|
||||
Row::Buttons(
|
||||
open("horizon.pages.open.extras", Page::Extras),
|
||||
Some(open("horizon.pages.open.pvp", Page::Pvp)),
|
||||
),
|
||||
Row::Buttons(
|
||||
open(
|
||||
"horizon.pages.open.split_setup",
|
||||
if self.split_guest {
|
||||
Page::SplitProfiles
|
||||
} else {
|
||||
Page::SplitSetup
|
||||
},
|
||||
),
|
||||
Some(open("horizon.pages.open.studio", Page::Studio)),
|
||||
),
|
||||
// No AI hides the AI Agent page and everything AI.
|
||||
Row::Buttons(
|
||||
self.extra_button(s, crate::extras::Item::DiscordPresence),
|
||||
Some(if s.extras.no_ai {
|
||||
self.extra_button(s, crate::extras::Item::NoAi)
|
||||
} else {
|
||||
open("horizon.pages.open.agent", Page::Agent)
|
||||
}),
|
||||
),
|
||||
]
|
||||
.into_iter()
|
||||
.chain(
|
||||
(!s.extras.no_ai)
|
||||
.then(|| Row::Buttons(self.extra_button(s, crate::extras::Item::NoAi), None)),
|
||||
)
|
||||
.collect();
|
||||
self.added_entries(&mut rows);
|
||||
rows
|
||||
}
|
||||
|
||||
/// The entries of the pages registered for Horizon Settings, two to a row like the engine's.
|
||||
fn added_entries(&self, rows: &mut Vec<Row>) {
|
||||
let mut entries = registered()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, page)| page.place() == added::Place::HorizonSettings)
|
||||
.map(|(i, page)| button(0.0, 0.0, 150.0, tr(page.title()), Act::Push(Page::Added(i))));
|
||||
while let Some(first) = entries.next() {
|
||||
rows.push(Row::Buttons(first, entries.next()));
|
||||
}
|
||||
}
|
||||
|
||||
/// A registered page's rows, asked of it each time the page is drawn.
|
||||
pub(super) fn added_rows(&self, page: usize) -> Vec<Row> {
|
||||
let Some(registered) = registered().get(page) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let mut rows = Vec::new();
|
||||
registered.rows(&mut rows);
|
||||
rows.into_iter()
|
||||
.enumerate()
|
||||
.map(|(r, row)| self.added_row(page, r, row))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn added_row(&self, page: usize, r: usize, row: added::Row) -> Row {
|
||||
let act = Act::PageRow(page, r);
|
||||
let wide = |label: Text| Row::Buttons(button(0.0, 0.0, 310.0, label, act.clone()), None);
|
||||
match row {
|
||||
added::Row::Heading(key) => Row::Heading(key.to_owned()),
|
||||
added::Row::Note(text) => Row::Note(text),
|
||||
added::Row::Toggle { label, on } => wide(generic(label, on_off(on))),
|
||||
added::Row::Cycle { label, value } => wide(generic(label, label_text(value))),
|
||||
added::Row::Slider { .. } => Row::Big(Opt::Page(page, r)),
|
||||
added::Row::Button { label } => wide(tr(label)),
|
||||
added::Row::Key { label, key } => {
|
||||
let mut message = keybinds::display_name(&key);
|
||||
if self.selected_key == Some(PAGE_KEY)
|
||||
&& self.selected_native_key == Some((page, r))
|
||||
{
|
||||
message = Text::literal("> ")
|
||||
.append(message.with_color(text::WHITE))
|
||||
.append(" <")
|
||||
.with_color(text::YELLOW);
|
||||
}
|
||||
let caption = button(0.0, 0.0, 150.0, tr(label), Act::None);
|
||||
let change = button(0.0, 0.0, 150.0, message, act);
|
||||
Row::Buttons(inactive(caption), Some(change))
|
||||
}
|
||||
// A row kind newer than this engine: shown empty.
|
||||
_ => Row::Note(String::new()),
|
||||
}
|
||||
}
|
||||
|
||||
/// A registered page's row was pressed: a key row waits for a key, any other row hears it.
|
||||
pub(super) fn page_act(&mut self, page: usize, r: usize, shift: bool) -> Vec<Event> {
|
||||
let Some(registered) = registered().get(page) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let mut rows = Vec::new();
|
||||
registered.rows(&mut rows);
|
||||
match rows.get(r) {
|
||||
Some(added::Row::Key { .. }) => {
|
||||
self.selected_key = Some(PAGE_KEY);
|
||||
self.selected_native_key = Some((page, r));
|
||||
Vec::new()
|
||||
}
|
||||
Some(_) => {
|
||||
registered.act(
|
||||
r,
|
||||
if shift {
|
||||
added::Act::PressBack
|
||||
} else {
|
||||
added::Act::Press
|
||||
},
|
||||
);
|
||||
vec![Event::Changed]
|
||||
}
|
||||
None => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// A native mod's page: its name, its keys as Key Binds rows (the
|
||||
/// player's choice, else the default), then its buttons.
|
||||
pub(super) fn native_rows(&self, s: &Settings, i: usize) -> Vec<Row> {
|
||||
@@ -46,9 +268,21 @@ impl OptionsUi {
|
||||
self.selected_native_key = Some((i, k));
|
||||
}
|
||||
|
||||
/// The key pressed for the native mod's key that waited for one.
|
||||
pub(super) fn bind_native_key(&mut self, s: &mut Settings, value: String) -> Vec<Event> {
|
||||
/// The key pressed for the native mod's key, or the registered page's ([`PAGE_KEY`]), that
|
||||
/// waited for one.
|
||||
pub(super) fn bind_native_key(
|
||||
&mut self,
|
||||
s: &mut Settings,
|
||||
name: &str,
|
||||
value: String,
|
||||
) -> Vec<Event> {
|
||||
let (i, k) = self.selected_native_key.take().unwrap_or_default();
|
||||
if name == PAGE_KEY {
|
||||
if let Some(page) = registered().get(i) {
|
||||
page.act(k, added::Act::Bind(value));
|
||||
}
|
||||
return vec![Event::Changed];
|
||||
}
|
||||
let Some(m) = s.mods.installed.get(i) else {
|
||||
return Vec::new();
|
||||
};
|
||||
@@ -60,3 +294,171 @@ impl OptionsUi {
|
||||
vec![Event::Changed]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::Mutex;
|
||||
|
||||
/// A page with a heading, a switch, a number that steps either way, a key and a volume.
|
||||
struct Sample {
|
||||
on: Mutex<bool>,
|
||||
step: Mutex<i32>,
|
||||
key: Mutex<String>,
|
||||
volume: Mutex<f32>,
|
||||
}
|
||||
|
||||
impl added::Page for Sample {
|
||||
fn id(&self) -> &'static str {
|
||||
"test.sample"
|
||||
}
|
||||
fn title(&self) -> &'static str {
|
||||
"test.sample.title"
|
||||
}
|
||||
fn place(&self) -> added::Place {
|
||||
added::Place::HorizonSettings
|
||||
}
|
||||
fn rows(&self, rows: &mut Vec<added::Row>) {
|
||||
rows.push(added::Row::Heading("test.sample.heading"));
|
||||
rows.push(added::Row::Toggle {
|
||||
label: "test.sample.switch",
|
||||
on: *self.on.lock().unwrap(),
|
||||
});
|
||||
rows.push(added::Row::Cycle {
|
||||
label: "test.sample.step",
|
||||
value: added::Label::Value(self.step.lock().unwrap().to_string()),
|
||||
});
|
||||
rows.push(added::Row::Key {
|
||||
label: "test.sample.key",
|
||||
key: self.key.lock().unwrap().clone(),
|
||||
});
|
||||
let volume = *self.volume.lock().unwrap();
|
||||
rows.push(added::Row::Slider {
|
||||
label: "test.sample.volume",
|
||||
value: volume,
|
||||
text: added::Label::Value(format!("{}%", (volume * 100.0).round())),
|
||||
});
|
||||
}
|
||||
fn act(&self, row: usize, act: added::Act) {
|
||||
match (row, act) {
|
||||
(1, added::Act::Press) => {
|
||||
let mut on = self.on.lock().unwrap();
|
||||
*on = !*on;
|
||||
}
|
||||
(2, added::Act::Press) => *self.step.lock().unwrap() += 1,
|
||||
(2, added::Act::PressBack) => *self.step.lock().unwrap() -= 1,
|
||||
(3, added::Act::Bind(key)) => *self.key.lock().unwrap() = key,
|
||||
(4, added::Act::Slide(v)) => *self.volume.lock().unwrap() = v,
|
||||
other => panic!("row and act the page doesn't expect: {other:?}"),
|
||||
}
|
||||
}
|
||||
fn tooltip(&self, row: usize) -> Option<added::Label> {
|
||||
(row == 1).then_some(added::Label::Key("test.sample.switch.tooltip"))
|
||||
}
|
||||
}
|
||||
|
||||
static SAMPLE: Sample = Sample {
|
||||
on: Mutex::new(false),
|
||||
step: Mutex::new(0),
|
||||
key: Mutex::new(String::new()),
|
||||
volume: Mutex::new(0.0),
|
||||
};
|
||||
static PAGES: [&dyn added::Page; 1] = [&SAMPLE];
|
||||
|
||||
/// Clicks the active button that does `act`, with Shift held or not.
|
||||
fn press(ui: &mut OptionsUi, s: &mut Settings, act: Act, shift: bool) -> Vec<Event> {
|
||||
let size = (480.0, 240.0);
|
||||
let (widgets, ..) = ui.layout(s, size.0, size.1);
|
||||
let (x, y) = widgets
|
||||
.iter()
|
||||
.find_map(|w| match &w.kind {
|
||||
WKind::Button {
|
||||
act: a,
|
||||
active: true,
|
||||
..
|
||||
} if *a == act => Some((w.x + 1.0, w.y + 1.0)),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap_or_else(|| panic!("no button does {act:?}"));
|
||||
ui.shift = shift;
|
||||
let atlas = super::super::tests::atlas();
|
||||
ui.click(s, &atlas, x, y, size, [480, 240], false, false)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_registered_page_opens_from_horizon_settings_and_hears_its_rows() {
|
||||
let mut s = Settings::default();
|
||||
TEST_PAGES.set(Some(&PAGES));
|
||||
let mut ui = OptionsUi::default();
|
||||
ui.show(Page::Horizon);
|
||||
press(&mut ui, &mut s, Act::Push(Page::Added(0)), false);
|
||||
assert_eq!(ui.page(), Page::Added(0));
|
||||
// Rows: the heading (0), the switch (1), the step (2), the key (3).
|
||||
let events = press(&mut ui, &mut s, Act::PageRow(0, 1), false);
|
||||
assert_eq!(
|
||||
events.last(),
|
||||
Some(&Event::Changed),
|
||||
"the settings get saved"
|
||||
);
|
||||
assert!(*SAMPLE.on.lock().unwrap());
|
||||
press(&mut ui, &mut s, Act::PageRow(0, 2), false);
|
||||
press(&mut ui, &mut s, Act::PageRow(0, 2), true);
|
||||
press(&mut ui, &mut s, Act::PageRow(0, 2), true);
|
||||
assert_eq!(*SAMPLE.step.lock().unwrap(), -1, "Shift steps back");
|
||||
// A key row waits for the next key, as on Key Binds, and the page gets its name.
|
||||
press(&mut ui, &mut s, Act::PageRow(0, 3), false);
|
||||
let atlas = super::super::tests::atlas();
|
||||
let events = ui.key(&mut s, &atlas, winit::keyboard::KeyCode::KeyZ, None);
|
||||
assert_eq!(events, [Event::Changed]);
|
||||
assert_eq!(*SAMPLE.key.lock().unwrap(), "key.keyboard.z");
|
||||
assert_eq!(ui.selected_key, None);
|
||||
// The volume is one of the screen's own sliders: dragged to its middle, then let go.
|
||||
let size = (480.0, 240.0);
|
||||
let (widgets, ..) = ui.layout(&s, size.0, size.1);
|
||||
let slider = widgets
|
||||
.iter()
|
||||
.find(|w| {
|
||||
matches!(
|
||||
w.kind,
|
||||
WKind::Slider {
|
||||
opt: Opt::Page(0, 4),
|
||||
..
|
||||
}
|
||||
)
|
||||
})
|
||||
.expect("the volume's slider");
|
||||
let (x, y) = (slider.x + slider.w / 2.0, slider.y + 1.0);
|
||||
ui.click(&mut s, &atlas, x, y, size, [480, 240], false, false);
|
||||
let volume = *SAMPLE.volume.lock().unwrap();
|
||||
assert!((0.45..0.55).contains(&volume), "{volume}");
|
||||
assert!(ui.release().contains(&Event::Changed));
|
||||
// The slider shows the page's value once let go.
|
||||
let (widgets, ..) = ui.layout(&s, size.0, size.1);
|
||||
let shown = widgets.iter().find_map(|w| match w.kind {
|
||||
WKind::Slider {
|
||||
opt: Opt::Page(0, 4),
|
||||
value,
|
||||
..
|
||||
} => Some(value),
|
||||
_ => None,
|
||||
});
|
||||
assert_eq!(shown, Some(volume));
|
||||
// The page's tooltip for its switch.
|
||||
let switch = widgets
|
||||
.iter()
|
||||
.find(|w| {
|
||||
matches!(
|
||||
&w.kind,
|
||||
WKind::Button {
|
||||
act: Act::PageRow(0, 1),
|
||||
..
|
||||
}
|
||||
)
|
||||
})
|
||||
.expect("the switch");
|
||||
assert_eq!(
|
||||
tooltip_text(&s, switch).map(|t| t.plain()),
|
||||
Some(tr("test.sample.switch.tooltip").plain())
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
//! Polytone's custom colours (`horizon_polytone::colors`, `colors.json` or `color.properties`)
|
||||
//! as the engine's pack colours ([`crate::pack_palette::PackColors`]).
|
||||
use std::path::Path;
|
||||
|
||||
use horizon_polytone::colors::PackColors;
|
||||
|
||||
use crate::block_entity_render::DYES;
|
||||
use crate::pack_palette::{self, Environment};
|
||||
|
||||
/// The colours of every namespace's file under the pack overlay at `root`.
|
||||
pub fn read(root: &Path) -> pack_palette::PackColors {
|
||||
let colors = PackColors::read(root);
|
||||
let by_dye = |f: &dyn Fn(&str) -> Option<u32>| std::array::from_fn(|i| f(DYES[i].0));
|
||||
let env = colors.env;
|
||||
pack_palette::PackColors {
|
||||
dye_diffuse: by_dye(&|name| colors.dye(name, "diffuse")),
|
||||
dye_text: by_dye(&|name| colors.dye(name, "text")),
|
||||
sheep: by_dye(&|name| colors.sheep(name)),
|
||||
redstone: std::array::from_fn(|i| colors.redstone(i as u8)),
|
||||
effects: colors.effects(),
|
||||
entity_shadows: colors.entity_shadows(),
|
||||
xp_bar: colors.xp_bar(),
|
||||
env: Environment {
|
||||
powder_snow_fog: env.powder_snow_fog,
|
||||
blindness_fog_distance: env.blindness_fog_distance,
|
||||
darkness_fog_distance: env.darkness_fog_distance,
|
||||
boss_fog_distance: env.boss_fog_distance,
|
||||
water_fog_brightening: env.water_fog_brightening,
|
||||
water_fog_brightening_time: env.water_fog_brightening_time,
|
||||
horizon_height: env.horizon_height,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::pack_palette::{diffuse, redstone, sheep_wool, text};
|
||||
|
||||
#[test]
|
||||
fn a_pack_recolours_only_what_it_names() {
|
||||
// Dyes no other test draws: the table is process-wide.
|
||||
let dir = std::env::temp_dir().join(format!("pack-dyes-{}", std::process::id()));
|
||||
let polytone = dir.join("assets/obs/polytone");
|
||||
std::fs::create_dir_all(&polytone).unwrap();
|
||||
std::fs::write(
|
||||
polytone.join("colors.json"),
|
||||
r#"{"dye": {"cyan": {"diffuse": "00ff00", "text": "0000ff"}}, "sheep": {"pink": "2040ff"}, "redstone": {"7": "0x123456"}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
crate::pack_palette::set(&read(&dir));
|
||||
let (cyan, pink, purple) = (9, 6, 10);
|
||||
assert_eq!(diffuse(cyan), 0x00ff00);
|
||||
assert_eq!(text(cyan), 0x0000ff);
|
||||
assert_eq!(sheep_wool(pink as u8), 0x2040ff);
|
||||
// Wool reads vanilla's dye, not the pack's diffuse: 75% of 0x169C9C.
|
||||
assert_eq!(sheep_wool(cyan as u8), 0x107575);
|
||||
assert_eq!(diffuse(purple), 0x8932B8);
|
||||
assert_eq!(text(purple), 0xA020F0);
|
||||
assert_eq!((redstone(7), redstone(15)), (Some(0x123456), None));
|
||||
crate::pack_palette::set(&Default::default());
|
||||
assert_eq!(redstone(7), None);
|
||||
assert_eq!(diffuse(cyan), 0x169C9C);
|
||||
assert_eq!(sheep_wool(pink as u8), 0xB6687F);
|
||||
std::fs::remove_dir_all(&dir).ok();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
//! Polytone's item modifiers' durability bar colours, on the engine's
|
||||
//! [`crate::pack_palette::ItemBars`].
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// An item modifier's `bar_color` of the `damage` type: a row of colours picked by the stack's
|
||||
/// damage, the first pixel undamaged, the last at full damage (measured on the real mod).
|
||||
struct DamageBar {
|
||||
targets: horizon_polytone::Targets,
|
||||
row: Vec<u32>,
|
||||
}
|
||||
|
||||
/// The packs' damage bars, the last read first.
|
||||
struct Bars(Vec<DamageBar>);
|
||||
|
||||
impl crate::pack_palette::ItemBars for Bars {
|
||||
fn color(&self, item: &str, damage: f32) -> Option<u32> {
|
||||
let bar = self
|
||||
.0
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|b| b.targets.matches(item, |_| false))?;
|
||||
let last = (bar.row.len() - 1) as f32;
|
||||
Some(bar.row[(damage.clamp(0.0, 1.0) * last).round() as usize])
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads the packs' item modifiers' durability bar colours under a pack overlay root: an
|
||||
/// `<item>_bar.png` alone, or a json whose `bar_color` is `damage` with its `<name>_bar.png` (or
|
||||
/// `<name>.png`) beside it. Colormap ids and inline colormaps aren't read. `None` without any.
|
||||
pub fn read(root: &Path) -> Option<Arc<dyn crate::pack_palette::ItemBars>> {
|
||||
let mut bars = Vec::new();
|
||||
for namespace in std::fs::read_dir(root.join("assets"))
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.flatten()
|
||||
{
|
||||
let base = namespace.path().join("polytone/item_modifiers");
|
||||
let mut stack = vec![base.clone()];
|
||||
while let Some(dir) = stack.pop() {
|
||||
for entry in std::fs::read_dir(&dir).into_iter().flatten().flatten() {
|
||||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
stack.push(path);
|
||||
} else if let Some(bar) = item_bar(root, &base, &path) {
|
||||
bars.push(bar);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
(!bars.is_empty()).then(|| Arc::new(Bars(bars)) as _)
|
||||
}
|
||||
|
||||
fn item_bar(root: &Path, base: &Path, path: &Path) -> Option<DamageBar> {
|
||||
let name = path.file_name()?.to_str()?;
|
||||
let row = |png: &Path| {
|
||||
let (width, _, rgba) = crate::sprites::read_rgba(png).ok()?;
|
||||
let row: Vec<u32> = rgba[..width as usize * 4]
|
||||
.as_chunks::<4>()
|
||||
.0
|
||||
.iter()
|
||||
.map(|p| u32::from(p[0]) << 16 | u32::from(p[1]) << 8 | u32::from(p[2]))
|
||||
.collect();
|
||||
(!row.is_empty()).then_some(row)
|
||||
};
|
||||
if let Some(stem) = name.strip_suffix("_bar.png") {
|
||||
// A json of the same name owns its png.
|
||||
if path.with_file_name(format!("{stem}.json")).exists() {
|
||||
return None;
|
||||
}
|
||||
let relative = path.parent()?.strip_prefix(base).ok()?.join(stem);
|
||||
let namespace = base.parent()?.parent()?.file_name()?.to_str()?;
|
||||
let id = format!("{namespace}:{}", relative.to_str()?.replace('\\', "/"));
|
||||
let targets = horizon_polytone::Targets::parse(None, Some(&id)).ok()?;
|
||||
return Some(DamageBar {
|
||||
targets,
|
||||
row: row(path)?,
|
||||
});
|
||||
}
|
||||
let stem = name.strip_suffix(".json")?;
|
||||
let json: serde_json::Value = horizon_world::json_file::read(path).ok()?;
|
||||
if json.get("bar_color")?.as_str()? != "damage" {
|
||||
return None;
|
||||
}
|
||||
let png = [format!("{stem}_bar.png"), format!("{stem}.png")]
|
||||
.into_iter()
|
||||
.map(|png| path.with_file_name(png))
|
||||
.find(|png| png.exists())?;
|
||||
let relative = path.strip_prefix(root).unwrap_or(path).to_string_lossy();
|
||||
let implicit = horizon_polytone::implicit_id(&relative, "item_modifiers");
|
||||
let targets =
|
||||
horizon_polytone::Targets::parse(json.get("targets"), implicit.as_deref()).ok()?;
|
||||
Some(DamageBar {
|
||||
targets,
|
||||
row: row(&png)?,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn png_row(path: &Path, pixels: &[[u8; 3]]) {
|
||||
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
let file = std::fs::File::create(path).unwrap();
|
||||
let mut encoder = png::Encoder::new(file, pixels.len() as u32, 1);
|
||||
encoder.set_color(png::ColorType::Rgba);
|
||||
let data: Vec<u8> = pixels
|
||||
.iter()
|
||||
.flat_map(|p| [p[0], p[1], p[2], 255])
|
||||
.collect();
|
||||
encoder
|
||||
.write_header()
|
||||
.unwrap()
|
||||
.write_image_data(&data)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_damage_bar_picks_its_pixel_by_the_stacks_damage() {
|
||||
let dir = std::env::temp_dir().join(format!("pack-bars-{}", std::process::id()));
|
||||
let modifiers = dir.join("assets/minecraft/polytone/item_modifiers");
|
||||
let rgby = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0]];
|
||||
png_row(&modifiers.join("diamond_pickaxe_bar.png"), &rgby);
|
||||
std::fs::write(
|
||||
modifiers.join("tools.json"),
|
||||
r#"{"targets": ["minecraft:iron_axe"], "bar_color": "damage"}"#,
|
||||
)
|
||||
.unwrap();
|
||||
png_row(&modifiers.join("tools_bar.png"), &[[1, 2, 3], [4, 5, 6]]);
|
||||
let bars = read(&dir).unwrap();
|
||||
// The real mod: damage 200 of 1561 shows the first pixel, 1400 the last.
|
||||
assert_eq!(
|
||||
bars.color("minecraft:diamond_pickaxe", 200.0 / 1561.0),
|
||||
Some(0xFF0000)
|
||||
);
|
||||
assert_eq!(
|
||||
bars.color("minecraft:diamond_pickaxe", 1400.0 / 1561.0),
|
||||
Some(0xFFFF00)
|
||||
);
|
||||
assert_eq!(bars.color("minecraft:iron_axe", 0.9), Some(0x040506));
|
||||
assert_eq!(bars.color("minecraft:diamond_shovel", 0.5), None);
|
||||
assert!(read(&dir.join("none")).is_none());
|
||||
std::fs::remove_dir_all(&dir).ok();
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,9 @@
|
||||
//! crate until the platform's executable installs it: it uses only the crate's public API, so it
|
||||
//! moves to a platform crate unchanged, and nothing else in the crate names it.
|
||||
|
||||
mod colors;
|
||||
mod custom_sky;
|
||||
mod item_bars;
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
@@ -14,10 +16,18 @@ use crate::pack_hooks::PackFormats;
|
||||
pub struct Formats;
|
||||
|
||||
impl PackFormats for Formats {
|
||||
fn colors(&self, root: &Path) -> Option<crate::pack_palette::PackColors> {
|
||||
Some(colors::read(root))
|
||||
}
|
||||
|
||||
fn custom_sky(&self, root: &Path) -> Option<Arc<dyn crate::custom_sky::SkyLayers>> {
|
||||
let sky = custom_sky::CustomSky::load(root);
|
||||
(!sky.is_empty()).then(|| Arc::new(sky) as _)
|
||||
}
|
||||
|
||||
fn item_bars(&self, root: &Path) -> Option<Arc<dyn crate::pack_palette::ItemBars>> {
|
||||
item_bars::read(root)
|
||||
}
|
||||
}
|
||||
|
||||
/// Installs these formats ([`crate::pack_hooks::install`]); the game does at startup.
|
||||
|
||||
@@ -10,10 +10,20 @@ use std::sync::{Arc, OnceLock};
|
||||
/// What the pack formats beyond vanilla's add to a pack. Each method reads one overlay of a pack
|
||||
/// (its folder at `root`); the default is "nothing in this format".
|
||||
pub trait PackFormats: Send + Sync {
|
||||
/// The colours the overlay replaces where vanilla hardcodes them.
|
||||
fn colors(&self, _root: &Path) -> Option<crate::pack_palette::PackColors> {
|
||||
None
|
||||
}
|
||||
|
||||
/// The sky layers the overlay draws over the vanilla sky, if it has any.
|
||||
fn custom_sky(&self, _root: &Path) -> Option<Arc<dyn crate::custom_sky::SkyLayers>> {
|
||||
None
|
||||
}
|
||||
|
||||
/// The durability bar colours the overlay sets by item, if any.
|
||||
fn item_bars(&self, _root: &Path) -> Option<Arc<dyn crate::pack_palette::ItemBars>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// No format beyond vanilla's.
|
||||
|
||||
@@ -1,14 +1,87 @@
|
||||
//! Polytone's dye, sheep and redstone colours (`dye.<name>.diffuse|text`, `sheep.<name>`,
|
||||
//! `redstone.<power>`), for drawing only: the pack recolours collars, tropical fish, banners, wool,
|
||||
//! sign text and redstone wire and dust, while gameplay (crafting, item data, the beacon) keeps
|
||||
//! vanilla's `DyeColor`. Lock-free: the draw paths and the mesher's workers read it per face.
|
||||
//! The colours a pack replaces where vanilla hardcodes them (Polytone's `colors.json`, read by the
|
||||
//! platform's pack formats, [`crate::pack_hooks`]): dye, sheep and redstone colours, for drawing
|
||||
//! only (the pack recolours collars, tropical fish, banners, wool, sign text and redstone wire and
|
||||
//! dust, while gameplay keeps vanilla's `DyeColor`), mob effect colours, the experience number's,
|
||||
//! entity shadows, the effect fogs' settings and durability bar colours. Lock-free: the draw paths
|
||||
//! and the mesher's workers read the dye, sheep and redstone tables per face.
|
||||
use std::path::Path;
|
||||
use std::sync::RwLock;
|
||||
use std::sync::atomic::{AtomicU64, Ordering::Relaxed};
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use crate::block_entity_render::DYES;
|
||||
use crate::signs::DYE_TEXT;
|
||||
use horizon_polytone::colors::PackColors;
|
||||
|
||||
/// The fog settings vanilla hardcodes, as the packs set them (vanilla's by default).
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Environment {
|
||||
/// The fog colour inside powder snow (RGB).
|
||||
pub powder_snow_fog: Option<u32>,
|
||||
/// The fog ends of Blindness (5), Darkness (15) and bosses that add world fog (96).
|
||||
pub blindness_fog_distance: f32,
|
||||
pub darkness_fog_distance: f32,
|
||||
pub boss_fog_distance: f32,
|
||||
/// How much the underwater fog colour washes out (1 vanilla, 0 none).
|
||||
pub water_fog_brightening: f32,
|
||||
/// The ticks under water it takes (vanilla's 600).
|
||||
pub water_fog_brightening_time: f32,
|
||||
/// Below it the sky draws its dark disc (vanilla's 63; flat worlds keep their floor).
|
||||
pub horizon_height: f32,
|
||||
}
|
||||
|
||||
impl Default for Environment {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
powder_snow_fog: None,
|
||||
blindness_fog_distance: 5.0,
|
||||
darkness_fog_distance: 15.0,
|
||||
boss_fog_distance: 96.0,
|
||||
water_fog_brightening: 1.0,
|
||||
water_fog_brightening_time: 600.0,
|
||||
horizon_height: 63.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The packs' colours; `None` (and the defaults) where they keep vanilla's.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct PackColors {
|
||||
/// By dye id (`DyeColor` order): `getTextureDiffuseColor`, `getTextColor`, the sheep's wool.
|
||||
pub dye_diffuse: [Option<u32>; 16],
|
||||
pub dye_text: [Option<u32>; 16],
|
||||
pub sheep: [Option<u32>; 16],
|
||||
/// `RedStoneWireBlock.COLORS` by power.
|
||||
pub redstone: [Option<u32>; 16],
|
||||
/// Mob effects' colours, by effect id without vanilla's namespace.
|
||||
pub effects: Vec<(String, u32)>,
|
||||
/// False hides every entity's round shadow, whatever the video option says.
|
||||
pub entity_shadows: bool,
|
||||
/// The experience level number's colour and outline.
|
||||
pub xp_bar: [Option<u32>; 2],
|
||||
/// The effect fogs' settings: the fog reads them every frame.
|
||||
pub env: Environment,
|
||||
}
|
||||
|
||||
impl Default for PackColors {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
dye_diffuse: [None; 16],
|
||||
dye_text: [None; 16],
|
||||
sheep: [None; 16],
|
||||
redstone: [None; 16],
|
||||
effects: Vec::new(),
|
||||
entity_shadows: true,
|
||||
xp_bar: [None; 2],
|
||||
env: Environment::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PackColors {
|
||||
/// Whether entities draw their round shadow, as far as the packs say.
|
||||
pub fn entity_shadows(&self) -> bool {
|
||||
self.entity_shadows
|
||||
}
|
||||
}
|
||||
|
||||
/// 0 is unset, else `1 << 32 | rgb`.
|
||||
static DIFFUSE: [AtomicU64; 16] = [const { AtomicU64::new(0) }; 16];
|
||||
@@ -16,6 +89,30 @@ static TEXT: [AtomicU64; 16] = [const { AtomicU64::new(0) }; 16];
|
||||
static SHEEP: [AtomicU64; 16] = [const { AtomicU64::new(0) }; 16];
|
||||
static REDSTONE: [AtomicU64; 16] = [const { AtomicU64::new(0) }; 16];
|
||||
|
||||
/// The colours of the pack overlay at `root`, from the installed pack formats; their dye, sheep
|
||||
/// and redstone colours go to the draw paths' tables ([`set`]).
|
||||
pub fn read(root: &Path) -> PackColors {
|
||||
let formats = crate::pack_hooks::formats();
|
||||
let colors = formats.colors(root).unwrap_or_default();
|
||||
set(&colors);
|
||||
*ITEM_BARS.write().unwrap_or_else(|e| e.into_inner()) = formats.item_bars(root);
|
||||
colors
|
||||
}
|
||||
|
||||
/// The packs' durability bar colours by item (Polytone's item modifiers' `bar_color`).
|
||||
pub trait ItemBars: Send + Sync {
|
||||
/// The bar colour (RGB) of `item` at this damage (0 to 1), when a pack sets one.
|
||||
fn color(&self, item: &str, damage: f32) -> Option<u32>;
|
||||
}
|
||||
|
||||
static ITEM_BARS: RwLock<Option<Arc<dyn ItemBars>>> = RwLock::new(None);
|
||||
|
||||
/// A pack's durability bar colour (RGB) for an item at this damage (0 to 1).
|
||||
pub fn item_bar_color(item: &str, damage: f32) -> Option<u32> {
|
||||
let bars = ITEM_BARS.read().unwrap_or_else(|e| e.into_inner());
|
||||
bars.as_ref()?.color(item, damage)
|
||||
}
|
||||
|
||||
/// Takes the colours of the packs just loaded (vanilla's where they set none).
|
||||
pub fn set(colors: &PackColors) {
|
||||
let store = |table: &[AtomicU64; 16], i: usize, color: Option<u32>| {
|
||||
@@ -24,129 +121,39 @@ pub fn set(colors: &PackColors) {
|
||||
Relaxed,
|
||||
);
|
||||
};
|
||||
for (i, (name, _)) in DYES.iter().enumerate() {
|
||||
store(&DIFFUSE, i, colors.dye(name, "diffuse"));
|
||||
store(&TEXT, i, colors.dye(name, "text"));
|
||||
store(&SHEEP, i, colors.sheep(name));
|
||||
store(&REDSTONE, i, colors.redstone(i as u8));
|
||||
for i in 0..16 {
|
||||
store(&DIFFUSE, i, colors.dye_diffuse[i]);
|
||||
store(&TEXT, i, colors.dye_text[i]);
|
||||
store(&SHEEP, i, colors.sheep[i]);
|
||||
store(&REDSTONE, i, colors.redstone[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/// An item modifier's `bar_color` of the `damage` type: a row of colours picked by the stack's
|
||||
/// damage, the first pixel undamaged, the last at full damage (measured on the real mod).
|
||||
struct DamageBar {
|
||||
targets: horizon_polytone::Targets,
|
||||
row: Vec<u32>,
|
||||
}
|
||||
static ITEM_BARS: RwLock<Vec<DamageBar>> = RwLock::new(Vec::new());
|
||||
|
||||
/// Reads the packs' item modifiers' durability bar colours under a pack overlay root: an
|
||||
/// `<item>_bar.png` alone, or a json whose `bar_color` is `damage` with its `<name>_bar.png` (or
|
||||
/// `<name>.png`) beside it. Colormap ids and inline colormaps aren't read.
|
||||
pub fn set_item_bars(root: &Path) {
|
||||
let mut bars = Vec::new();
|
||||
for namespace in std::fs::read_dir(root.join("assets"))
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.flatten()
|
||||
{
|
||||
let base = namespace.path().join("polytone/item_modifiers");
|
||||
let mut stack = vec![base.clone()];
|
||||
while let Some(dir) = stack.pop() {
|
||||
for entry in std::fs::read_dir(&dir).into_iter().flatten().flatten() {
|
||||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
stack.push(path);
|
||||
} else if let Some(bar) = item_bar(root, &base, &path) {
|
||||
bars.push(bar);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*ITEM_BARS.write().unwrap_or_else(|e| e.into_inner()) = bars;
|
||||
}
|
||||
|
||||
fn item_bar(root: &Path, base: &Path, path: &Path) -> Option<DamageBar> {
|
||||
let name = path.file_name()?.to_str()?;
|
||||
let row = |png: &Path| {
|
||||
let (width, _, rgba) = crate::sprites::read_rgba(png).ok()?;
|
||||
let row: Vec<u32> = rgba[..width as usize * 4]
|
||||
.as_chunks::<4>()
|
||||
.0
|
||||
.iter()
|
||||
.map(|p| u32::from(p[0]) << 16 | u32::from(p[1]) << 8 | u32::from(p[2]))
|
||||
.collect();
|
||||
(!row.is_empty()).then_some(row)
|
||||
};
|
||||
if let Some(stem) = name.strip_suffix("_bar.png") {
|
||||
// A json of the same name owns its png.
|
||||
if path.with_file_name(format!("{stem}.json")).exists() {
|
||||
return None;
|
||||
}
|
||||
let relative = path.parent()?.strip_prefix(base).ok()?.join(stem);
|
||||
let namespace = base.parent()?.parent()?.file_name()?.to_str()?;
|
||||
let id = format!("{namespace}:{}", relative.to_str()?.replace('\\', "/"));
|
||||
let targets = horizon_polytone::Targets::parse(None, Some(&id)).ok()?;
|
||||
return Some(DamageBar {
|
||||
targets,
|
||||
row: row(path)?,
|
||||
});
|
||||
}
|
||||
let stem = name.strip_suffix(".json")?;
|
||||
let json: serde_json::Value = horizon_world::json_file::read(path).ok()?;
|
||||
if json.get("bar_color")?.as_str()? != "damage" {
|
||||
return None;
|
||||
}
|
||||
let png = [format!("{stem}_bar.png"), format!("{stem}.png")]
|
||||
.into_iter()
|
||||
.map(|png| path.with_file_name(png))
|
||||
.find(|png| png.exists())?;
|
||||
let relative = path.strip_prefix(root).unwrap_or(path).to_string_lossy();
|
||||
let implicit = horizon_polytone::implicit_id(&relative, "item_modifiers");
|
||||
let targets =
|
||||
horizon_polytone::Targets::parse(json.get("targets"), implicit.as_deref()).ok()?;
|
||||
Some(DamageBar {
|
||||
targets,
|
||||
row: row(&png)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// A pack's durability bar colour (RGB) for an item at this damage (0 to 1).
|
||||
pub fn item_bar_color(item: &str, damage: f32) -> Option<u32> {
|
||||
let bars = ITEM_BARS.read().unwrap_or_else(|e| e.into_inner());
|
||||
let bar = bars
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|b| b.targets.matches(item, |_| false))?;
|
||||
let last = (bar.row.len() - 1) as f32;
|
||||
Some(bar.row[(damage.clamp(0.0, 1.0) * last).round() as usize])
|
||||
}
|
||||
|
||||
fn read(table: &[AtomicU64; 16], i: usize) -> Option<u32> {
|
||||
fn get(table: &[AtomicU64; 16], i: usize) -> Option<u32> {
|
||||
let v = table[i & 15].load(Relaxed);
|
||||
(v != 0).then_some(v as u32)
|
||||
}
|
||||
|
||||
/// `DyeColor.getTextureDiffuseColor` (RGB) as the packs draw it.
|
||||
pub fn diffuse(dye: usize) -> u32 {
|
||||
read(&DIFFUSE, dye).unwrap_or(DYES[dye & 15].1)
|
||||
get(&DIFFUSE, dye).unwrap_or(DYES[dye & 15].1)
|
||||
}
|
||||
|
||||
/// `DyeColor.getTextColor` (RGB) as the packs draw it.
|
||||
pub fn text(dye: usize) -> u32 {
|
||||
read(&TEXT, dye).unwrap_or(DYE_TEXT[dye & 15].1)
|
||||
get(&TEXT, dye).unwrap_or(DYE_TEXT[dye & 15].1)
|
||||
}
|
||||
|
||||
/// A pack's `RedStoneWireBlock.COLORS` entry for this power (RGB), when it sets one.
|
||||
pub fn redstone(power: u8) -> Option<u32> {
|
||||
read(&REDSTONE, usize::from(power))
|
||||
get(&REDSTONE, usize::from(power))
|
||||
}
|
||||
|
||||
/// `ColorLerper.Type.SHEEP` wool colour: a pack's `sheep.<name>`, else white 0xE6E6E6 and the
|
||||
/// others 75% of vanilla's dye (a pack's `dye.*.diffuse` doesn't reach wool).
|
||||
pub fn sheep_wool(dye: u8) -> u32 {
|
||||
let i = usize::from(dye & 15);
|
||||
if let Some(color) = read(&SHEEP, i) {
|
||||
if let Some(color) = get(&SHEEP, i) {
|
||||
return color;
|
||||
}
|
||||
if i == 0 {
|
||||
@@ -156,81 +163,3 @@ pub fn sheep_wool(dye: u8) -> u32 {
|
||||
let f = |c: u32| ((c as f32 * 0.75).floor() as u32).min(255);
|
||||
f(src >> 16 & 255) << 16 | f(src >> 8 & 255) << 8 | f(src & 255)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_pack_recolours_only_what_it_names() {
|
||||
// Dyes no other test draws: the table is process-wide.
|
||||
let dir = std::env::temp_dir().join(format!("pack-dyes-{}", std::process::id()));
|
||||
let polytone = dir.join("assets/obs/polytone");
|
||||
std::fs::create_dir_all(&polytone).unwrap();
|
||||
std::fs::write(
|
||||
polytone.join("colors.json"),
|
||||
r#"{"dye": {"cyan": {"diffuse": "00ff00", "text": "0000ff"}}, "sheep": {"pink": "2040ff"}, "redstone": {"7": "0x123456"}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
set(&PackColors::read(&dir));
|
||||
let (cyan, pink, purple) = (9, 6, 10);
|
||||
assert_eq!(diffuse(cyan), 0x00ff00);
|
||||
assert_eq!(text(cyan), 0x0000ff);
|
||||
assert_eq!(sheep_wool(pink as u8), 0x2040ff);
|
||||
// Wool reads vanilla's dye, not the pack's diffuse: 75% of 0x169C9C.
|
||||
assert_eq!(sheep_wool(cyan as u8), 0x107575);
|
||||
assert_eq!(diffuse(purple), 0x8932B8);
|
||||
assert_eq!(text(purple), 0xA020F0);
|
||||
assert_eq!((redstone(7), redstone(15)), (Some(0x123456), None));
|
||||
set(&PackColors::default());
|
||||
assert_eq!(redstone(7), None);
|
||||
assert_eq!(diffuse(cyan), 0x169C9C);
|
||||
assert_eq!(sheep_wool(pink as u8), 0xB6687F);
|
||||
std::fs::remove_dir_all(&dir).ok();
|
||||
}
|
||||
|
||||
fn png_row(path: &Path, pixels: &[[u8; 3]]) {
|
||||
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
let file = std::fs::File::create(path).unwrap();
|
||||
let mut encoder = png::Encoder::new(file, pixels.len() as u32, 1);
|
||||
encoder.set_color(png::ColorType::Rgba);
|
||||
let data: Vec<u8> = pixels
|
||||
.iter()
|
||||
.flat_map(|p| [p[0], p[1], p[2], 255])
|
||||
.collect();
|
||||
encoder
|
||||
.write_header()
|
||||
.unwrap()
|
||||
.write_image_data(&data)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_damage_bar_picks_its_pixel_by_the_stacks_damage() {
|
||||
let dir = std::env::temp_dir().join(format!("pack-bars-{}", std::process::id()));
|
||||
let modifiers = dir.join("assets/minecraft/polytone/item_modifiers");
|
||||
let rgby = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0]];
|
||||
png_row(&modifiers.join("diamond_pickaxe_bar.png"), &rgby);
|
||||
std::fs::write(
|
||||
modifiers.join("tools.json"),
|
||||
r#"{"targets": ["minecraft:iron_axe"], "bar_color": "damage"}"#,
|
||||
)
|
||||
.unwrap();
|
||||
png_row(&modifiers.join("tools_bar.png"), &[[1, 2, 3], [4, 5, 6]]);
|
||||
set_item_bars(&dir);
|
||||
// The real mod: damage 200 of 1561 shows the first pixel, 1400 the last.
|
||||
assert_eq!(
|
||||
item_bar_color("minecraft:diamond_pickaxe", 200.0 / 1561.0),
|
||||
Some(0xFF0000)
|
||||
);
|
||||
assert_eq!(
|
||||
item_bar_color("minecraft:diamond_pickaxe", 1400.0 / 1561.0),
|
||||
Some(0xFFFF00)
|
||||
);
|
||||
assert_eq!(item_bar_color("minecraft:iron_axe", 0.9), Some(0x040506));
|
||||
assert_eq!(item_bar_color("minecraft:diamond_shovel", 0.5), None);
|
||||
set_item_bars(&dir.join("none"));
|
||||
assert_eq!(item_bar_color("minecraft:diamond_pickaxe", 0.5), None);
|
||||
std::fs::remove_dir_all(&dir).ok();
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@ mod native;
|
||||
pub mod polytone;
|
||||
mod polytone_gpu;
|
||||
mod polytone_model;
|
||||
mod spawn;
|
||||
pub use native::{ParticleController, ParticleUpdate};
|
||||
|
||||
/// `ParticleOptions` with the parameters of the option classes used here.
|
||||
@@ -2026,73 +2027,6 @@ impl ParticleEngine {
|
||||
(eye[0] - pos[0]).powi(2) + (eye[1] - pos[1]).powi(2) + (eye[2] - pos[2]).powi(2) <= 9.0
|
||||
})
|
||||
}
|
||||
/// `ClientPacketListener.handleParticleEvent` for `ServerLevel.sendParticles`.
|
||||
pub fn send_particles(
|
||||
&mut self,
|
||||
options: &ParticleOptions,
|
||||
at: [f64; 3],
|
||||
spread: [f64; 3],
|
||||
speed: f64,
|
||||
count: u32,
|
||||
) {
|
||||
if count == 0 {
|
||||
let v = [speed * spread[0], speed * spread[1], speed * spread[2]];
|
||||
self.add(options, at, v);
|
||||
return;
|
||||
}
|
||||
for _ in 0..count {
|
||||
let r = &mut self.random;
|
||||
let p = [
|
||||
at[0] + r.next_gaussian() * spread[0],
|
||||
at[1] + r.next_gaussian() * spread[1],
|
||||
at[2] + r.next_gaussian() * spread[2],
|
||||
];
|
||||
let v = [
|
||||
r.next_gaussian() * speed,
|
||||
r.next_gaussian() * speed,
|
||||
r.next_gaussian() * speed,
|
||||
];
|
||||
self.add(options, p, v);
|
||||
}
|
||||
}
|
||||
/// `ParticleEngine.createParticle`: always spawned, but `ParticleEngine.add`
|
||||
/// drops a spore blossom air particle once `ParticleLimit.SPORE_BLOSSOM` is full.
|
||||
pub fn create(&mut self, options: &ParticleOptions, p: [f64; 3], v: [f64; 3]) {
|
||||
if let Some(requests) = &mut self.created_requests {
|
||||
requests.push((options.clone(), p, v));
|
||||
return;
|
||||
}
|
||||
let seed = self.seeds.next_long();
|
||||
if let Some(mut particle) = self.make(options, p, v, seed) {
|
||||
*self.made.entry(options.ty.name()).or_default() += 1;
|
||||
// Resource packs' Polytone particle modifiers (#11486), once as it spawns.
|
||||
if let Some(registry) = &self.polytone
|
||||
&& registry.modifies_particles()
|
||||
{
|
||||
let id = match &options.mod_particle {
|
||||
Some(name) => name.clone(),
|
||||
None => format!("minecraft:{}", options.ty.name()),
|
||||
};
|
||||
polytone::modify(registry, &id, options.source.as_deref(), &mut particle);
|
||||
}
|
||||
// The provider has drawn, so rejecting here keeps vanilla's RNG order.
|
||||
// shortcut: counts live plus queued particles by scanning instead of
|
||||
// vanilla's counted map; use a counter if spore spawning shows in profiles.
|
||||
if particle.ty == ParticleType::SporeBlossomAir
|
||||
&& self.particles.len() + self.pending.len() >= SPORE_BLOSSOM_LIMIT
|
||||
&& self
|
||||
.particles
|
||||
.iter()
|
||||
.chain(self.pending.iter())
|
||||
.filter(|q| q.ty == ParticleType::SporeBlossomAir)
|
||||
.count()
|
||||
>= SPORE_BLOSSOM_LIMIT
|
||||
{
|
||||
return;
|
||||
}
|
||||
self.pending.push(particle);
|
||||
}
|
||||
}
|
||||
/// How many particles of each type were made so far.
|
||||
pub fn made(&self) -> &std::collections::BTreeMap<&'static str, u64> {
|
||||
&self.made
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
//! Where particles come into the engine: a server's particle packet and a single particle made
|
||||
//! where the game asks for one.
|
||||
use super::*;
|
||||
|
||||
impl ParticleEngine {
|
||||
/// `ClientPacketListener.handleParticleEvent` for `ServerLevel.sendParticles`.
|
||||
pub fn send_particles(
|
||||
&mut self,
|
||||
options: &ParticleOptions,
|
||||
at: [f64; 3],
|
||||
spread: [f64; 3],
|
||||
speed: f64,
|
||||
count: u32,
|
||||
) {
|
||||
if count == 0 {
|
||||
let v = [speed * spread[0], speed * spread[1], speed * spread[2]];
|
||||
self.add(options, at, v);
|
||||
return;
|
||||
}
|
||||
for _ in 0..count {
|
||||
let r = &mut self.random;
|
||||
let p = [
|
||||
at[0] + r.next_gaussian() * spread[0],
|
||||
at[1] + r.next_gaussian() * spread[1],
|
||||
at[2] + r.next_gaussian() * spread[2],
|
||||
];
|
||||
let v = [
|
||||
r.next_gaussian() * speed,
|
||||
r.next_gaussian() * speed,
|
||||
r.next_gaussian() * speed,
|
||||
];
|
||||
self.add(options, p, v);
|
||||
}
|
||||
}
|
||||
/// `ParticleEngine.createParticle`: always spawned, but `ParticleEngine.add`
|
||||
/// drops a spore blossom air particle once `ParticleLimit.SPORE_BLOSSOM` is full.
|
||||
pub fn create(&mut self, options: &ParticleOptions, p: [f64; 3], v: [f64; 3]) {
|
||||
if let Some(requests) = &mut self.created_requests {
|
||||
requests.push((options.clone(), p, v));
|
||||
return;
|
||||
}
|
||||
let seed = self.seeds.next_long();
|
||||
if let Some(mut particle) = self.make(options, p, v, seed) {
|
||||
*self.made.entry(options.ty.name()).or_default() += 1;
|
||||
// Resource packs' Polytone particle modifiers (#11486), once as it spawns.
|
||||
if let Some(registry) = &self.polytone
|
||||
&& registry.modifies_particles()
|
||||
{
|
||||
let id = match &options.mod_particle {
|
||||
Some(name) => name.clone(),
|
||||
None => format!("minecraft:{}", options.ty.name()),
|
||||
};
|
||||
polytone::modify(registry, &id, options.source.as_deref(), &mut particle);
|
||||
}
|
||||
// The provider has drawn, so rejecting here keeps vanilla's RNG order.
|
||||
// shortcut: counts live plus queued particles by scanning instead of
|
||||
// vanilla's counted map; use a counter if spore spawning shows in profiles.
|
||||
if particle.ty == ParticleType::SporeBlossomAir
|
||||
&& self.particles.len() + self.pending.len() >= SPORE_BLOSSOM_LIMIT
|
||||
&& self
|
||||
.particles
|
||||
.iter()
|
||||
.chain(self.pending.iter())
|
||||
.filter(|q| q.ty == ParticleType::SporeBlossomAir)
|
||||
.count()
|
||||
>= SPORE_BLOSSOM_LIMIT
|
||||
{
|
||||
return;
|
||||
}
|
||||
self.pending.push(particle);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -864,6 +864,8 @@ pub enum Opt {
|
||||
PadGameplayGuide,
|
||||
/// `options.modelPart.*`: one skin layer (index into `options_file::MODEL_PARTS`).
|
||||
ModelPart(usize),
|
||||
/// A slider of a page a crate registered (`horizon_hooks::pages`): (page, row).
|
||||
Page(usize, usize),
|
||||
/// 3D skin layers: the player's outer layer as extruded slabs.
|
||||
SkinLayers3d,
|
||||
MainHand,
|
||||
|
||||
@@ -51,7 +51,7 @@ pub fn enabled() -> bool {
|
||||
}
|
||||
/// Record this thread's spans even with spans off, their wall time only (no CPU clock read):
|
||||
/// what the render thread's slow frames spent their time in, for the opt-in performance
|
||||
/// statistics (`telemetry::Session::frame`). The thread takes them every frame ([`take`]).
|
||||
/// statistics (`play_session::Session::frame`). The thread takes them every frame ([`take`]).
|
||||
pub fn record_here(on: bool) {
|
||||
HERE.with(|h| h.set(on));
|
||||
}
|
||||
@@ -466,7 +466,7 @@ pub mod startup {
|
||||
Mutex, OnceLock,
|
||||
atomic::{AtomicBool, AtomicU32, Ordering},
|
||||
},
|
||||
time::Instant,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
static ON: AtomicBool = AtomicBool::new(false);
|
||||
@@ -489,6 +489,11 @@ pub mod startup {
|
||||
ON.store(sink.is_some(), Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// The time since the process started ([`init`]'s origin; zero before it).
|
||||
pub fn since_start() -> Duration {
|
||||
ORIGIN.get().map_or(Duration::ZERO, Instant::elapsed)
|
||||
}
|
||||
|
||||
/// Whether a trace is being written.
|
||||
#[inline]
|
||||
pub fn on() -> bool {
|
||||
|
||||
@@ -122,12 +122,13 @@ controllers = ["horizon-client-lib/controllers", "dep:gilrs", "dep:horizon-duals
|
||||
# the server list, the Horizon Settings and Mods pages, Discord and Play with Friends. Players always get them; there is no switch. `grep -rn horizon-extras crates/` lists
|
||||
# every place it gates. Without it (`--no-default-features --features audio,controllers`) the
|
||||
# client is best effort: CI builds only the default.
|
||||
horizon-extras = ["discord", "friends"]
|
||||
horizon-extras = ["horizon-client-lib/horizon-extras", "discord", "friends"]
|
||||
# Online extras a server or a port can leave out; the server build has none of them. "Playing Horizon"
|
||||
# on Discord (game/discord.rs).
|
||||
discord = ["horizon-client-lib/discord"]
|
||||
# Play with Friends: sharing a world behind an invite code (game/friends.rs) and joining one.
|
||||
friends = ["horizon-client-lib/friends", "dep:horizon-friends"]
|
||||
# Play with Friends: sharing a world behind an invite code (game/friends.rs). Joining one is
|
||||
# horizon-friends' `game` side, which horizon-official registers (`horizon_hooks::tunnel`).
|
||||
friends = ["dep:horizon-friends"]
|
||||
# Records the call sites of mob random draws (`mob_ai_probe` traces).
|
||||
rng-trace = ["horizon-core/rng-trace", "horizon-client-lib/rng-trace"]
|
||||
# Rust's allocations go to the system heap instead of mimalloc, where heap profilers that replace
|
||||
|
||||
@@ -441,6 +441,7 @@
|
||||
"HDR Calibration %s/%s: %s": "HDR Calibration %s/%s: %s",
|
||||
"Sets the three below full screen, one test picture at a time.": "Sets the three below full screen, one test picture at a time.",
|
||||
"Your display didn't say how bright it gets: calibrate to set it up.": "Your display didn't say how bright it gets: calibrate to set it up.",
|
||||
"Your display's reported peak is often too high: calibrate to set it up.": "Your display's reported peak is often too high: calibrate to set it up.",
|
||||
"Already set up for your display. Only calibrate if HDR looks wrong.": "Already set up for your display. Only calibrate if HDR looks wrong.",
|
||||
"Puts Peak Brightness and Paper White back to your display's setup.": "Puts Peak Brightness and Paper White back to your display's setup.",
|
||||
"Set up for your display. Leave on Auto unless it looks wrong.": "Set up for your display. Leave on Auto unless it looks wrong.",
|
||||
|
||||
@@ -21,11 +21,11 @@ pub use horizon_client_render::{
|
||||
mipmap, mod_browser, mod_list, model_library, models, multiline_edit, native_emissive,
|
||||
options_file, options_fixes, options_ui, overlay, pack_browser, pack_formats, pack_hooks,
|
||||
pack_palette, pack_screen, packs, pad_cursor, pad_glyph, par, particles, poi_sections,
|
||||
polytone_colors, polytone_creative_tabs, polytone_post, post_chain, potent_sulfur, preedit,
|
||||
providers, pvp, pvp_hud, random_source, screen_draw, screen_effects, sculk, section_mask,
|
||||
server_terrain, settings, shader_options, shader_pack_list, signs, skins, social, sound_engine,
|
||||
sound_events, sound_preview, spans, split_layout, split_map, split_profiles, sprites,
|
||||
streaming, structure_block, tab_list, terrain_model, test_block, test_instance_block, testgen,
|
||||
polytone_creative_tabs, polytone_post, post_chain, potent_sulfur, preedit, providers, pvp,
|
||||
pvp_hud, random_source, screen_draw, screen_effects, sculk, section_mask, server_terrain,
|
||||
settings, shader_options, shader_pack_list, signs, skins, social, sound_engine, sound_events,
|
||||
sound_preview, spans, split_layout, split_map, split_profiles, sprites, streaming,
|
||||
structure_block, tab_list, terrain_model, test_block, test_instance_block, testgen,
|
||||
title_edition, trims, ui, vault, weather, widgets, world, worldborder, zip,
|
||||
};
|
||||
pub mod attack_strength;
|
||||
@@ -70,7 +70,6 @@ pub mod falling_block;
|
||||
pub mod fire_render;
|
||||
pub mod first_person;
|
||||
pub mod flatgen;
|
||||
pub mod friends_screen;
|
||||
pub mod furnace;
|
||||
pub mod game_mode_switcher;
|
||||
pub mod game_rules_screen;
|
||||
@@ -103,12 +102,15 @@ pub mod native_data;
|
||||
pub mod net;
|
||||
pub mod nether_portal;
|
||||
pub mod no_resources;
|
||||
#[cfg(feature = "horizon-extras")]
|
||||
pub mod official_servers;
|
||||
pub mod offline_name;
|
||||
pub mod old_world;
|
||||
pub mod osk;
|
||||
pub mod patrol;
|
||||
pub mod perf_overlay;
|
||||
pub mod pick_item;
|
||||
pub mod play_session;
|
||||
pub mod player_sync;
|
||||
pub mod portal_sections;
|
||||
pub mod pose;
|
||||
|
||||
@@ -84,7 +84,7 @@ fn slime_at(world: &World, facts: &BlockFacts, position: BlockPos) -> bool {
|
||||
mod fluids;
|
||||
mod item_fall;
|
||||
mod item_no_physics;
|
||||
pub use fluids::{fluid_flow, update_entity_fluids, water_clip};
|
||||
pub use fluids::{fall_reset_hit, fluid_flow, update_entity_fluids, water_clip};
|
||||
|
||||
#[cfg(test)]
|
||||
mod drop_motion_tests {
|
||||
|
||||
@@ -45,6 +45,30 @@ pub fn fluid_flow(world: &World, fluids: &FluidResources, pos: [i32; 3]) -> [f64
|
||||
let flow = flow_at(&mut scene, state, at).unwrap_or(Vec3::ZERO);
|
||||
[flow.x, flow.y, flow.z]
|
||||
}
|
||||
/// `Entity.move`'s fall-reset ray along a move from `from`, up to 8 blocks:
|
||||
/// `ClipContext.Fluid.WATER` (the water's shape) and
|
||||
/// `ClipContext.Block.FALLDAMAGE_RESETTING` (a block `tagged` says is a reset
|
||||
/// block clips as a full block). Both shapes are queried for every visited
|
||||
/// block, the water's first (its first query fixes the shared cached shape);
|
||||
/// either one hitting ends the ray.
|
||||
pub fn fall_reset_hit(
|
||||
world: &World,
|
||||
fluids: &FluidResources,
|
||||
tagged: impl Fn(BlockPos) -> bool,
|
||||
from: Vec3,
|
||||
moved: Vec3,
|
||||
) -> bool {
|
||||
let to = from.add_vector(moved.normalize().scale(moved.length().min(8.0)));
|
||||
let hit = horizon_core::geometry::traverse_blocks(from, to, 1_000_000, |p| {
|
||||
let water = water_clip(world, fluids, from, to, p);
|
||||
let reset = tagged(p)
|
||||
&& horizon_core::geometry::VoxelShape::block()
|
||||
.clip(from, to, p)
|
||||
.is_some();
|
||||
(water || reset).then_some(())
|
||||
});
|
||||
matches!(hit, Ok(Some(())))
|
||||
}
|
||||
/// `ClipContext.Fluid.WATER` in a fall-reset ray through `p`: the water's
|
||||
/// shape from the shared `FlowingFluid.shapes` cache (the first query of a
|
||||
/// fluid state fixes its height), as the player's own ray reads it.
|
||||
|
||||
@@ -233,7 +233,7 @@ fn is_animal(id: &str) -> bool {
|
||||
use crate::mobs::persistent_anger;
|
||||
/// The single player's UUID (`offline_uuid("Player")`): the entity the
|
||||
/// neutral mobs are angry at.
|
||||
fn player_uuid() -> [i32; 4] {
|
||||
pub(crate) fn player_uuid() -> [i32; 4] {
|
||||
let u = horizon_protocol::crypto::offline_uuid("Player");
|
||||
[
|
||||
(u >> 96) as u32 as i32,
|
||||
@@ -1297,7 +1297,7 @@ pub fn mob_to_nbt(
|
||||
crate::mobs::nautilus::to_nbt(m, map);
|
||||
}
|
||||
"minecraft:sulfur_cube" => crate::mobs::sulfur_cube::to_nbt(m, map),
|
||||
"minecraft:bee" => crate::mobs::bee::to_nbt(m, map),
|
||||
"minecraft:bee" => crate::mobs::bee::to_nbt(m, map, &mut uuid_of),
|
||||
"minecraft:sniffer" => crate::mobs::sniffer::to_nbt(m, map),
|
||||
"minecraft:wither" => {
|
||||
map.insert("Invul".into(), Tag::Int(m.extra.counter));
|
||||
@@ -2666,9 +2666,9 @@ pub fn mob_from_nbt_with_random(
|
||||
m.extra.counter = number(tag.get("still_timeout")).unwrap_or(0) as i32;
|
||||
}
|
||||
if m.species() == crate::mobs::Species::Shulker {
|
||||
m.extra.variant3 = number(tag.get("AttachFace")).unwrap_or(0).clamp(0, 5) as u8;
|
||||
m.extra.counter = number(tag.get("Peek")).unwrap_or(0).clamp(0, 100) as i32;
|
||||
m.extra.color = number(tag.get("Color")).map_or(16, |c| c.clamp(0, 16) as u8);
|
||||
m.extra.variant3 = (number(tag.get("AttachFace")).unwrap_or(0) % 6).unsigned_abs() as u8;
|
||||
m.extra.counter = number(tag.get("Peek")).map_or(0, |v| v as i8 as i32);
|
||||
m.extra.color = number(tag.get("Color")).map_or(16, |v| v as i8 as u8);
|
||||
m.anim.v[0] = m.extra.counter as f32 * 0.01;
|
||||
m.anim.vo[0] = m.anim.v[0];
|
||||
// Shulker.setPos: square on its block.
|
||||
|
||||
@@ -20,13 +20,18 @@ pub fn main_with(mods: crate::mod_api::Mods) {
|
||||
horizon_client::options_ui::HORIZON_EXTRAS
|
||||
.store(false, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
horizon_client::telemetry::mark_start();
|
||||
// The process's start, for the startup trace and the start's milestones.
|
||||
horizon_client::spans::startup::init(std::time::Instant::now());
|
||||
// Opt-in telemetry keeps each crash report (docs/telemetry.md), whatever the mode.
|
||||
crash::on_report(horizon_client::telemetry::keep_crash);
|
||||
// Opt-in telemetry's play session plugs in through the hooks until it moves to a Horizon
|
||||
// crate in horizon-official's parts (layer plan M5). horizon-extras: telemetry is Horizon's
|
||||
// (docs/layers.md); without it no session is kept.
|
||||
#[cfg(feature = "horizon-extras")]
|
||||
let _ = horizon_hooks::register(&horizon_client::telemetry::HOOKS);
|
||||
// Crates above the engine registered their hooks before this (horizon-official's `main`);
|
||||
// none registers after this point.
|
||||
horizon_hooks::engine::seal();
|
||||
horizon_client::spans::startup::init(std::time::Instant::now());
|
||||
horizon_client::spans::trace::start_from_env();
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
let game_args = args.iter().take_while(|a| *a != "--dedicated");
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
//! HUD boxes in the corners: the minimap's place and mods' panels, small
|
||||
//! boxes in GUI pixels stacked per corner (`docs/extras.md`).
|
||||
//! boxes in GUI pixels stacked per corner (`docs/extras.md`), and the layers crates above the
|
||||
//! engine register (`horizon_hooks::hud`).
|
||||
use crate::{
|
||||
extras::Corner,
|
||||
hud::{HudAtlas, HudBatch, Rect, rgba},
|
||||
};
|
||||
use horizon_hooks::hud;
|
||||
|
||||
/// The gap between an overlay and the screen edge or the next overlay.
|
||||
const MARGIN: f32 = 2.0;
|
||||
@@ -125,6 +127,68 @@ pub fn draw_lines(
|
||||
}
|
||||
}
|
||||
|
||||
/// Draws the HUD layers crates registered, after the extras' and mods' boxes, which they stack
|
||||
/// with. `shown`: whether each corner of `Corner::ALL` shows (the debug screen covers some).
|
||||
pub fn draw_layers(
|
||||
layers: &[&dyn hud::Layer],
|
||||
batch: &mut HudBatch,
|
||||
atlas: &HudAtlas,
|
||||
corners: &mut Corners,
|
||||
shown: [bool; 4],
|
||||
) {
|
||||
let mut canvas = Canvas {
|
||||
batch,
|
||||
atlas,
|
||||
corners,
|
||||
shown,
|
||||
};
|
||||
for layer in layers {
|
||||
layer.draw(&mut canvas);
|
||||
}
|
||||
}
|
||||
|
||||
struct Canvas<'a> {
|
||||
batch: &'a mut HudBatch,
|
||||
atlas: &'a HudAtlas,
|
||||
corners: &'a mut Corners,
|
||||
shown: [bool; 4],
|
||||
}
|
||||
|
||||
/// `0xAARRGGBB` as the batch's colour.
|
||||
fn argb_color(argb: u32) -> u32 {
|
||||
let [a, r, g, b] = argb.to_be_bytes();
|
||||
rgba(r, g, b, a)
|
||||
}
|
||||
|
||||
impl hud::Canvas for Canvas<'_> {
|
||||
fn size(&self) -> (f32, f32) {
|
||||
(self.batch.width, self.batch.height)
|
||||
}
|
||||
fn tr(&self, key: &str) -> String {
|
||||
crate::lang::get(key)
|
||||
}
|
||||
fn text_width(&self, text: &str) -> f32 {
|
||||
HudBatch::text_width(self.atlas, text)
|
||||
}
|
||||
fn text(&mut self, x: f32, y: f32, text: &str, argb: u32, shadow: bool) {
|
||||
self.batch
|
||||
.text(self.atlas, text, x, y, argb_color(argb), shadow);
|
||||
}
|
||||
fn fill(&mut self, x: f32, y: f32, w: f32, h: f32, argb: u32) {
|
||||
self.batch.fill(self.atlas, x, y, w, h, argb_color(argb));
|
||||
}
|
||||
fn place(&mut self, corner: hud::Corner, w: f32, h: f32) -> Option<(f32, f32)> {
|
||||
let corner = match corner {
|
||||
hud::Corner::TopLeft => Corner::TopLeft,
|
||||
hud::Corner::TopRight => Corner::TopRight,
|
||||
hud::Corner::BottomLeft => Corner::BottomLeft,
|
||||
hud::Corner::BottomRight => Corner::BottomRight,
|
||||
};
|
||||
let i = Corner::ALL.iter().position(|c| *c == corner)?;
|
||||
self.shown[i].then(|| self.corners.place(corner, w, h))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -140,4 +204,58 @@ mod tests {
|
||||
assert_eq!(c.place(Corner::BottomRight, 50.0, 10.0), (348.0, 274.0));
|
||||
assert_eq!(c.place(Corner::BottomLeft, 10.0, 10.0), (2.0, 288.0));
|
||||
}
|
||||
|
||||
/// Draws a box of one line in a corner and keeps where it went.
|
||||
struct Panel {
|
||||
corner: hud::Corner,
|
||||
at: std::sync::Mutex<Vec<Option<(f32, f32)>>>,
|
||||
}
|
||||
|
||||
impl hud::Layer for Panel {
|
||||
fn draw(&self, canvas: &mut dyn hud::Canvas) {
|
||||
let w = canvas.text_width("12 CPS") + 4.0;
|
||||
let at = canvas.place(self.corner, w, 12.0);
|
||||
if let Some((x, y)) = at {
|
||||
canvas.fill(x, y, w, 12.0, 0x7800_0000);
|
||||
canvas.text(x + 2.0, y + 2.0, "12 CPS", 0xFFFF_FFFF, true);
|
||||
}
|
||||
self.at.lock().unwrap().push(at);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layers_stack_with_the_extras_and_skip_hidden_corners() {
|
||||
let atlas =
|
||||
HudAtlas::build(std::path::Path::new("/nonexistent/horizon-test-assets")).unwrap();
|
||||
let mut batch = HudBatch::new([800, 600]);
|
||||
let (width, height) = (batch.width, batch.height);
|
||||
let mut corners = Corners::new(width, height, 0.0);
|
||||
// The minimap already took the top right.
|
||||
corners.place(Corner::TopRight, 64.0, 64.0);
|
||||
let right = Panel {
|
||||
corner: hud::Corner::TopRight,
|
||||
at: Default::default(),
|
||||
};
|
||||
let left = Panel {
|
||||
corner: hud::Corner::TopLeft,
|
||||
at: Default::default(),
|
||||
};
|
||||
let layers: [&dyn hud::Layer; 2] = [&right, &left];
|
||||
let empty = batch.vertices.len();
|
||||
draw_layers(
|
||||
&layers,
|
||||
&mut batch,
|
||||
&atlas,
|
||||
&mut corners,
|
||||
[false, true, true, true],
|
||||
);
|
||||
let (_, y) = right.at.lock().unwrap()[0].expect("the top right shows");
|
||||
assert_eq!(y, 2.0 + 64.0 + 2.0, "under the minimap");
|
||||
assert_eq!(
|
||||
*left.at.lock().unwrap(),
|
||||
[None],
|
||||
"the debug screen covers the top left"
|
||||
);
|
||||
assert!(batch.vertices.len() > empty, "the shown box is drawn");
|
||||
}
|
||||
}
|
||||
@@ -650,7 +650,7 @@ enum Screen {
|
||||
/// WorldOptionsScreen (game/lan_screen.rs).
|
||||
WorldOptions,
|
||||
/// Play with Friends, from the pause menu's Friends button
|
||||
/// (`friends_screen`, game/friends.rs).
|
||||
/// (game/friends.rs, game/friends/screen.rs).
|
||||
Friends,
|
||||
/// `SocialInteractionsScreen` (`social_screen`, game/social.rs).
|
||||
Social,
|
||||
@@ -2028,7 +2028,7 @@ pub struct PackResources {
|
||||
/// The packs' Polytone post chains (#11486), when a pack has some.
|
||||
polytone_post: Option<Arc<horizon_client::polytone_post::PolytonePost>>,
|
||||
/// The packs' Polytone custom colours (`polytone/colors.json`).
|
||||
polytone_colors: Arc<horizon_client::polytone_colors::PackColors>,
|
||||
polytone_colors: Arc<horizon_client::pack_palette::PackColors>,
|
||||
}
|
||||
/// A quick craft drag as the screen draws it: each dragged slot's count and
|
||||
/// whether the slot caps it (None: hidden), and what stays in hand.
|
||||
@@ -2801,7 +2801,7 @@ struct Game<'a> {
|
||||
agent_last: Option<Instant>,
|
||||
frame_stats: horizon_client::control::FrameStats,
|
||||
/// This play session's frame and tick times, for opt-in performance statistics.
|
||||
perf: horizon_client::telemetry::Session,
|
||||
perf: horizon_client::play_session::Session,
|
||||
/// Control replies waiting for a rendered frame or a benchmark deadline.
|
||||
pending_screenshot: Option<std::sync::mpsc::Sender<String>>,
|
||||
/// `screenshot-sequence` while it runs.
|
||||
@@ -5490,34 +5490,6 @@ impl Game<'_> {
|
||||
);
|
||||
cancel
|
||||
}
|
||||
/// The ticks the frame shows (animated textures, the HUD, a shader pack's clock): the
|
||||
/// client's, or under `HORIZON_STILL` (`FixedStep::still`) the world's game time, which
|
||||
/// moves only while the world runs.
|
||||
pub(super) fn visual_ticks(&self) -> u64 {
|
||||
if self.clock.still() {
|
||||
self.world.game_time.max(0) as u64
|
||||
} else {
|
||||
self.ticks
|
||||
}
|
||||
}
|
||||
/// The clock of the animated textures: `Minecraft.runTick` ticks the `TextureManager`
|
||||
/// only while the level runs normally. Under `HORIZON_STILL` it is the world's ticks.
|
||||
pub(super) fn texture_clock(&self) -> u64 {
|
||||
if self.clock.still() {
|
||||
self.visual_ticks()
|
||||
} else {
|
||||
self.texture_ticks
|
||||
}
|
||||
}
|
||||
/// `Util.getMillis` for what the frame shows (the world border's scroll, the hearts' blink,
|
||||
/// the glint's under the switch): under `HORIZON_STILL`, the world's ticks in milliseconds.
|
||||
pub(super) fn visual_millis(&self) -> i64 {
|
||||
if self.clock.still() {
|
||||
self.visual_ticks() as i64 * 50
|
||||
} else {
|
||||
self.started.elapsed().as_millis() as i64
|
||||
}
|
||||
}
|
||||
/// `PresenceHandler.getPresenceStatus`: what an all-sharing player is doing, for
|
||||
/// the friends service's `updatePresence`.
|
||||
fn presence_status(&self) -> horizon_client::social::PresenceStatus {
|
||||
@@ -6170,7 +6142,7 @@ impl Game<'_> {
|
||||
let atlas = &g.gui.atlas;
|
||||
let width = |s: &str| HudBatch::text_width(atlas, s) as i32;
|
||||
let draws = hud_layout::experience_colored(
|
||||
g.pack.polytone_colors.xp_bar(),
|
||||
g.pack.polytone_colors.xp_bar,
|
||||
wi,
|
||||
hi,
|
||||
g.experience.progress,
|
||||
@@ -6503,18 +6475,6 @@ impl Game<'_> {
|
||||
self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1);
|
||||
}
|
||||
}
|
||||
/// `AbstractContainerScreen` keeps `isQuickCrafting` and the drag it
|
||||
/// belongs to on the screen. Horizon keeps both on `Game` (`inv_clicks`,
|
||||
/// `inv_drag`, `last_inv_click`), so they are dropped when the pointer
|
||||
/// changes hands: a screen that opens or closes starts with its own
|
||||
/// state, and a press whose release never reached the screen -- the
|
||||
/// pointer left the window, or the screen closed first -- would
|
||||
/// otherwise leave every later press on every later screen swallowed.
|
||||
pub(super) fn reset_inventory_clicks(&mut self) {
|
||||
self.inv_clicks.cancel_drag();
|
||||
self.inv_drag = None;
|
||||
self.last_inv_click = None;
|
||||
}
|
||||
pub(super) fn close_container(&mut self) {
|
||||
// `Gui.setScreen` clears the old screen's focus before it goes: the
|
||||
// key that closed the container (Tab bound to the inventory) has
|
||||
@@ -6558,20 +6518,6 @@ impl Game<'_> {
|
||||
}
|
||||
self.close_ender_chest();
|
||||
}
|
||||
/// A sound a menu click made the player play (a bundle's insert or
|
||||
/// remove): `Entity.playSound` at the player.
|
||||
pub(super) fn player_menu_sound(&mut self, sound: &str) {
|
||||
let Some(p) = self.player.as_ref() else {
|
||||
return;
|
||||
};
|
||||
let at = [p.position.x, p.position.y, p.position.z];
|
||||
let (volume, pitch) = if sound == horizon_client::bundle::INSERT_FAIL {
|
||||
(1.0, 1.0)
|
||||
} else {
|
||||
(0.8, 0.8 + self.world.random.next_float() * 0.4)
|
||||
};
|
||||
self.audio.play(sound, Some(at), volume, pitch);
|
||||
}
|
||||
/// `Camera.setRotation`: the camera's orientation, turned round in
|
||||
/// third-person front view.
|
||||
pub(super) fn camera_orientation(&self) -> glam::Quat {
|
||||
@@ -12689,11 +12635,11 @@ pub fn run_with() -> Result<()> {
|
||||
options.spikes = Some(dedicated::SLOW_TICK_MS);
|
||||
}
|
||||
tick_profile::want_spans(tick_profile::SPANS_SPIKES, options.spikes.is_some());
|
||||
// A player who allows performance statistics also says what their slow frames spent their
|
||||
// time in: this thread, which draws the frames, times its steps (wall time only).
|
||||
// A hook that wants to know what slow frames spent their time in (a player who allows
|
||||
// performance statistics): this thread, which draws the frames, times its steps (wall time
|
||||
// only).
|
||||
if options.execution_role.is_client()
|
||||
&& horizon_telemetry::Consent::load(&horizon_client::logging::game_home())
|
||||
.allows(horizon_telemetry::Kind::Perf)
|
||||
&& horizon_hooks::engine::wants_frame_steps(&horizon_client::logging::game_home())
|
||||
{
|
||||
horizon_client::spans::record_here(true);
|
||||
}
|
||||
@@ -12720,16 +12666,9 @@ pub fn run_with() -> Result<()> {
|
||||
if !options.offscreen && !synchronous_startup(&options) {
|
||||
let version = horizon_client::telemetry::version();
|
||||
horizon_hooks::engine::started(&horizon_hooks::Started::new(&version));
|
||||
let player_dir = horizon_client::logging::player_dir();
|
||||
// horizon-extras: the official server is Horizon's (docs/forking.md).
|
||||
if cfg!(feature = "horizon-extras")
|
||||
&& let Err(e) = horizon_client::multiplayer::add_official_server(&player_dir)
|
||||
{
|
||||
log::warn!("could not add the official server to servers.dat: {e}");
|
||||
}
|
||||
if let Err(e) = horizon_client::multiplayer::withdraw_partner_servers(&player_dir) {
|
||||
log::warn!("could not take the withdrawn partner server out of servers.dat: {e}");
|
||||
}
|
||||
#[cfg(feature = "horizon-extras")]
|
||||
horizon_client::official_servers::update(&horizon_client::logging::player_dir());
|
||||
}
|
||||
mods::register_components(&vanilla_root)?;
|
||||
mods::register_standalone_models();
|
||||
|
||||
@@ -686,23 +686,15 @@ impl Game<'_> {
|
||||
if fall == 0.0 || length < 1.0 {
|
||||
return fall;
|
||||
}
|
||||
let to = from.add_vector(moved.normalize().scale(moved.length().min(8.0)));
|
||||
let reset = &self.res.motion().fall_reset;
|
||||
let fluids = &self.res.motion().fluids;
|
||||
let hit = horizon_core::geometry::traverse_blocks(from, to, 1_000_000, |p| {
|
||||
// A tagged block clips as a full block, not its collider.
|
||||
let name = &self.res.facts().get(self.world.block(p.x, p.y, p.z)).name;
|
||||
if reset.contains(&**name) && VoxelShape::block().clip(from, to, p).is_some() {
|
||||
return Some(());
|
||||
}
|
||||
// The water's shape, through the shared fluid shape cache.
|
||||
horizon_client::entities::water_clip(&self.world, fluids, from, to, p).then_some(())
|
||||
});
|
||||
if matches!(hit, Ok(Some(()))) {
|
||||
0.0
|
||||
} else {
|
||||
fall
|
||||
}
|
||||
let hit = horizon_client::entities::fall_reset_hit(
|
||||
&self.world,
|
||||
&self.res.motion().fluids,
|
||||
|p| reset.contains(&*self.res.facts().get(self.world.block(p.x, p.y, p.z)).name),
|
||||
from,
|
||||
moved,
|
||||
);
|
||||
if hit { 0.0 } else { fall }
|
||||
}
|
||||
/// `AbstractBoat.checkFallDamage` after a move that went `moved_y` (with
|
||||
/// `b` already where it landed): on the ground the fall resets; a move
|
||||
|
||||
@@ -70,6 +70,23 @@ pub(super) enum BookScreen {
|
||||
lectern: Option<(i32, i32, i32)>,
|
||||
},
|
||||
}
|
||||
impl BookScreen {
|
||||
/// `BookSignScreen`'s constructor: its title box opens with the title
|
||||
/// kept from the last time, which Cancel stored (`titleValue`).
|
||||
fn begin_signing(&mut self) {
|
||||
if let Self::Edit {
|
||||
signing,
|
||||
title,
|
||||
title_edit,
|
||||
..
|
||||
} = self
|
||||
{
|
||||
*signing = true;
|
||||
title_edit.set_value(title.as_str());
|
||||
title_edit.reset_blink();
|
||||
}
|
||||
}
|
||||
}
|
||||
/// The pages a book screen reads: a written book's components (so their
|
||||
/// click events stay), else a writable book's text.
|
||||
fn view_pages(data: Option<&StackData>) -> Vec<Text> {
|
||||
@@ -988,7 +1005,7 @@ impl Game<'_> {
|
||||
title,
|
||||
..
|
||||
}) if !title.trim().is_empty() => self.save_book(true),
|
||||
Some(BookScreen::Edit { signing, .. }) => *signing = true,
|
||||
Some(book @ BookScreen::Edit { .. }) => book.begin_signing(),
|
||||
_ => return Err("not a book and quill".into()),
|
||||
},
|
||||
Some("done") => match self.book.as_mut() {
|
||||
@@ -1190,8 +1207,8 @@ impl Game<'_> {
|
||||
self.turn_page(false);
|
||||
} else if inside(w / 2.0 - 100.0, 196.0, 98.0, 20.0) {
|
||||
click(self);
|
||||
if let Some(BookScreen::Edit { signing, .. }) = self.book.as_mut() {
|
||||
*signing = true;
|
||||
if let Some(book) = self.book.as_mut() {
|
||||
book.begin_signing();
|
||||
}
|
||||
} else if inside(w / 2.0 + 2.0, 196.0, 98.0, 20.0) {
|
||||
click(self);
|
||||
@@ -1809,3 +1826,46 @@ mod lectern_reach_tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod sign_tests {
|
||||
use super::{BookScreen, book_page_box, book_title_box};
|
||||
|
||||
/// `BookSignScreen`'s constructor sets its title box to `titleValue`, what
|
||||
/// Cancel kept; reopening the sign screen shows the title typed before,
|
||||
/// not an empty box beside a stale `title`.
|
||||
#[test]
|
||||
fn the_sign_screen_opens_with_the_title_it_kept() {
|
||||
let mut book = BookScreen::Edit {
|
||||
slot: 0,
|
||||
pages: vec!["page".into()],
|
||||
page: 0,
|
||||
signing: false,
|
||||
title: "My Book".into(),
|
||||
title_edit: book_title_box(),
|
||||
page_edit: book_page_box(),
|
||||
};
|
||||
assert_eq!(book_title_value(&book), "");
|
||||
book.begin_signing();
|
||||
let BookScreen::Edit {
|
||||
signing,
|
||||
title,
|
||||
title_edit,
|
||||
..
|
||||
} = &book
|
||||
else {
|
||||
panic!("the book stays open");
|
||||
};
|
||||
assert!(signing);
|
||||
assert_eq!(title.as_str(), "My Book");
|
||||
assert_eq!(title_edit.value, "My Book");
|
||||
assert_eq!(title_edit.cursor, 7);
|
||||
}
|
||||
|
||||
fn book_title_value(book: &BookScreen) -> String {
|
||||
match book {
|
||||
BookScreen::Edit { title_edit, .. } => title_edit.value.clone(),
|
||||
BookScreen::View { .. } => String::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,8 +42,8 @@ impl Reply {
|
||||
}
|
||||
|
||||
pub(super) fn duration(millis: i64) -> Text {
|
||||
use horizon_chat_filter::DurationUnit;
|
||||
let (count, unit) = horizon_chat_filter::duration_parts(millis);
|
||||
use horizon_server_files::duration::{DurationUnit, duration_parts};
|
||||
let (count, unit) = duration_parts(millis);
|
||||
let key = match (unit, count == 1) {
|
||||
(DurationUnit::Minute, true) => "horizon.moderation.duration.minute",
|
||||
(DurationUnit::Minute, false) => "horizon.moderation.duration.minutes",
|
||||
@@ -93,7 +93,7 @@ pub(super) const MUTE_USAGES: [&str; 4] = [
|
||||
fn mute_time(words: &[&str]) -> (Option<i64>, usize) {
|
||||
match words
|
||||
.get(2)
|
||||
.and_then(|w| horizon_chat_filter::parse_duration(w))
|
||||
.and_then(|w| horizon_server_files::duration::parse_duration(w))
|
||||
{
|
||||
Some(ms) => (Some(ms), 3),
|
||||
None => (None, 2),
|
||||
@@ -133,7 +133,7 @@ impl Game<'_> {
|
||||
count: u32,
|
||||
action: Action,
|
||||
) {
|
||||
let now = horizon_chat_filter::now_millis();
|
||||
let now = horizon_server_files::duration::now_millis();
|
||||
let summary = format!(
|
||||
"[chat-filter] Refused {name}'s {} (hit {count}: {})",
|
||||
place.name(),
|
||||
@@ -281,7 +281,7 @@ impl Game<'_> {
|
||||
let reason = |from: usize| (words.len() > from).then(|| words[from..].join(" "));
|
||||
match words {
|
||||
["mutelist"] => {
|
||||
let now = horizon_chat_filter::now_millis();
|
||||
let now = horizon_server_files::duration::now_millis();
|
||||
let mutes = host.hub.mutes();
|
||||
if mutes.is_empty() {
|
||||
return vec![Reply::translated("horizon.moderation.mutelist.empty", &[])];
|
||||
@@ -319,13 +319,15 @@ impl Game<'_> {
|
||||
let (time, from) = mute_time(words);
|
||||
vec![self.mute(host, name, time, reason(from), by)]
|
||||
}
|
||||
["tempmute", name, time, ..] => match horizon_chat_filter::parse_duration(time) {
|
||||
Some(ms) => vec![self.mute(host, name, Some(ms), reason(3), by)],
|
||||
None => vec![Reply::translated(
|
||||
"horizon.moderation.invalid_duration",
|
||||
&[Text::literal(*time)],
|
||||
)],
|
||||
},
|
||||
["tempmute", name, time, ..] => {
|
||||
match horizon_server_files::duration::parse_duration(time) {
|
||||
Some(ms) => vec![self.mute(host, name, Some(ms), reason(3), by)],
|
||||
None => vec![Reply::translated(
|
||||
"horizon.moderation.invalid_duration",
|
||||
&[Text::literal(*time)],
|
||||
)],
|
||||
}
|
||||
}
|
||||
["mute", ..] => vec![Reply::translated("horizon.moderation.usage.mute", &[])],
|
||||
["tempmute", ..] => vec![Reply::translated("horizon.moderation.usage.tempmute", &[])],
|
||||
["unmute", ..] => vec![Reply::translated("horizon.moderation.usage.unmute", &[])],
|
||||
@@ -353,10 +355,10 @@ impl Game<'_> {
|
||||
let Some((uuid, name)) = user else {
|
||||
return Reply::no_player();
|
||||
};
|
||||
let until = time.map(|ms| horizon_chat_filter::now_millis() + ms);
|
||||
let until = time.map(|ms| horizon_server_files::duration::now_millis() + ms);
|
||||
let what = time.map_or_else(
|
||||
|| "until unmuted".into(),
|
||||
|ms| format!("for {}", horizon_chat_filter::duration_text(ms)),
|
||||
|ms| format!("for {}", horizon_server_files::duration::duration_text(ms)),
|
||||
);
|
||||
let why = reason
|
||||
.as_deref()
|
||||
|
||||
@@ -1567,11 +1567,11 @@ impl Game<'_> {
|
||||
"streaming": self.streaming.stages().into_iter().map(|(k, v)| (k.to_string(), v.into())).collect::<serde_json::Map<_, _>>(),
|
||||
"dirty_light": self.world.dirty_light.len(),
|
||||
"renderer": renderer,
|
||||
"display": self.window.as_deref().and_then(horizon_client::telemetry::monitor).map(|m| json!({
|
||||
"width": m.size().width,
|
||||
"height": m.size().height,
|
||||
"scale": m.scale_factor(),
|
||||
"refresh_hz": m.refresh_rate_millihertz().map(|r| r.div_ceil(1000)),
|
||||
"display": self.window.as_deref().and_then(horizon_client::window::display).map(|d| json!({
|
||||
"width": d.width,
|
||||
"height": d.height,
|
||||
"scale": d.scale,
|
||||
"refresh_hz": d.refresh_hz,
|
||||
})),
|
||||
"uptime_seconds": self.started.elapsed().as_secs_f64(),
|
||||
"server": self.remote_status(),
|
||||
|
||||
@@ -623,3 +623,6 @@ fn stack_drop_requires_ctrl_even_with_sprint_latched() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[path = "play_ping_tests.rs"]
|
||||
mod play_ping_tests;
|
||||
@@ -2,8 +2,10 @@
|
||||
//! chunk-generation spawning (`NaturalSpawner`), mob events (attacks,
|
||||
//! breeding, conversions, drops), projectiles, player interactions
|
||||
//! (feeding, shearing, milking, spawn eggs, throwing) and rendering.
|
||||
mod mob_geometry;
|
||||
mod projectile_world;
|
||||
mod pufferfish_player;
|
||||
mod spawn_eggs;
|
||||
|
||||
use super::{
|
||||
Dim, Game, Hand, InteractTarget, entity_culling::EntityCullDecision,
|
||||
@@ -7450,171 +7452,6 @@ impl Game<'_> {
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
/// `Mob.checkAndHandleImportantInteractions` with a spawn egg of mob
|
||||
/// `index`'s own kind (`SpawnEggItem.spawnOffspringFromSpawnEgg`): a baby
|
||||
/// at the mob's feet, taking the parent's variant as `getBreedOffspring`
|
||||
/// does, and one egg used. False (PASS) when the mob has no baby form.
|
||||
fn spawn_egg_offspring(&mut self, index: usize) -> bool {
|
||||
let Some(stack) = self.held().cloned() else {
|
||||
return false;
|
||||
};
|
||||
let parent = &self.mobs[index];
|
||||
if !parent.alive()
|
||||
|| !horizon_client::mobs::finalize::spawn_egg_is_for(&stack.item, &parent.kind.id)
|
||||
|| !horizon_client::mobs::species::can_be_a_baby(parent.species())
|
||||
// WanderingTrader.getBreedOffspring returns null: no offspring, the
|
||||
// click carries on to trading.
|
||||
|| parent.species() == Species::WanderingTrader
|
||||
{
|
||||
return false;
|
||||
}
|
||||
let (kind, at) = (
|
||||
parent.kind.id.clone(),
|
||||
[parent.position.x, parent.position.y, parent.position.z],
|
||||
);
|
||||
let (species, variant, color) =
|
||||
(parent.species(), parent.extra.variant, parent.extra.color);
|
||||
let target = self.mob_entity(&self.mobs[index]);
|
||||
// SpawnEggItem.spawnOffspringFromSpawnEgg: `type.create(level, SPAWN_ITEM_USE)`
|
||||
// creates the baby without finalizeSpawn, and `snapTo(pos, 0, 0)` faces
|
||||
// it south without a level-random draw.
|
||||
let Some(id) =
|
||||
self.spawn_mob_internal(&kind, at, false, false, false, false, SpawnYaw::Fixed(0.0))
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
if let Some(child) = self.mobs.iter_mut().find(|m| m.id == id) {
|
||||
horizon_client::mobs::finalize::spawn_egg_offspring(child, species, variant, color);
|
||||
}
|
||||
// `Mob.checkAndHandleImportantInteractions` then calls
|
||||
// `onOffspringSpawnedFromEgg`: `Fox`'s makes the baby trust the player
|
||||
// who used the egg (a fox egg used on a block trusts nobody).
|
||||
if species == Species::Fox {
|
||||
let spawner = self.acting_player_uuid();
|
||||
if let Some(child) = self.mobs.iter_mut().find(|m| m.id == id) {
|
||||
horizon_client::mobs::fox::add_trusted_entity(child, spawner);
|
||||
}
|
||||
}
|
||||
self.trigger_item_entity("minecraft:player_interacted_with_entity", &stack, &target);
|
||||
self.consume_held();
|
||||
true
|
||||
}
|
||||
/// `SpawnEggItem.use`: the SOURCE_ONLY ray must hit a liquid block.
|
||||
pub(super) fn spawn_egg_fluid_hit(&self) -> Option<super::Hit> {
|
||||
let hit = self.bucket_hit()?;
|
||||
matches!(
|
||||
&*self.block_name(hit.pos),
|
||||
"minecraft:water" | "minecraft:lava"
|
||||
)
|
||||
.then_some(hit)
|
||||
}
|
||||
/// Shared peaceful-mode eligibility for spawn eggs.
|
||||
pub(super) fn spawn_egg_allowed(&self, item: &str) -> bool {
|
||||
let kind = item.trim_end_matches("_spawn_egg");
|
||||
self.ui.difficulty != 0
|
||||
|| self
|
||||
.res
|
||||
.mob_types()
|
||||
.get(kind)
|
||||
.is_none_or(|t| horizon_client::mobs::spawn_rules::allowed_in_peaceful(t.species))
|
||||
}
|
||||
/// `SpawnEggItem.useOn` for a block, or `use` for a fluid source.
|
||||
fn use_spawn_egg(&mut self, stack: &ItemStack) -> Result<bool> {
|
||||
let fluid = matches!(self.guest_use, Some(super::GuestUse::Item(_)));
|
||||
let Some(hit) = (if fluid {
|
||||
self.spawn_egg_fluid_hit()
|
||||
} else {
|
||||
self.pick()
|
||||
}) else {
|
||||
return Ok(false);
|
||||
};
|
||||
let kind = stack.item.trim_end_matches("_spawn_egg").to_owned();
|
||||
if !self.spawn_egg_allowed(&stack.item) {
|
||||
self.use_failed = true;
|
||||
return Ok(true);
|
||||
}
|
||||
if fluid
|
||||
&& !self.may_build()
|
||||
&& !self.can_place_on_in_adventure([
|
||||
hit.pos[0] - hit.face[0],
|
||||
hit.pos[1] - hit.face[1],
|
||||
hit.pos[2] - hit.face[2],
|
||||
])
|
||||
{
|
||||
self.use_failed = true;
|
||||
return Ok(true);
|
||||
}
|
||||
// SpawnEggItem.useOn on a spawner sets its mob (and an op's egg data).
|
||||
let (spawner_id, data) = horizon_client::block_entity_item::spawn_egg_on_spawner(
|
||||
stack,
|
||||
self.acting_player_is_op(),
|
||||
);
|
||||
if let Some(done) = self.use_spawn_egg_on_spawner(hit.pos, &spawner_id, data.as_ref()) {
|
||||
if done {
|
||||
self.swing_arm();
|
||||
self.award_stat("used", &stack.item, 1);
|
||||
self.consume_held();
|
||||
}
|
||||
return Ok(true);
|
||||
}
|
||||
// useOn: a clicked block without a collision shape (vines, grass)
|
||||
// takes the mob itself, any other the one on the clicked face.
|
||||
let p = if fluid || self.collision_empty(hit.pos) {
|
||||
hit.pos
|
||||
} else {
|
||||
[
|
||||
hit.pos[0] + hit.face[0],
|
||||
hit.pos[1] + hit.face[1],
|
||||
hit.pos[2] + hit.face[2],
|
||||
]
|
||||
};
|
||||
let at = [
|
||||
f64::from(p[0]) + 0.5,
|
||||
f64::from(p[1]),
|
||||
f64::from(p[2]) + 0.5,
|
||||
];
|
||||
// SpawnEggItem.spawnMob: `EntityType.spawn`.
|
||||
let Some(id) = self.spawn_mob_spawned(&kind, at, SpawnYaw::Created) else {
|
||||
return Ok(false);
|
||||
};
|
||||
// SpawnEggItem.spawnMob: `EntityType.create` finalizes the mob, then
|
||||
// `PostSpawnProcessor` puts the egg's components on it.
|
||||
if let Some(m) = self.mobs.iter_mut().find(|m| m.id == id) {
|
||||
horizon_client::entity_nbt::apply_item_components(m, stack);
|
||||
}
|
||||
self.apply_spawn_egg_entity_data(id, stack);
|
||||
self.post_block_game_event("minecraft:entity_place", p, Some(Who::Player), None);
|
||||
self.award_stat("used", &stack.item, 1);
|
||||
self.swing_arm();
|
||||
if !self.creative
|
||||
&& let Some(s) = self.held_mut().as_mut()
|
||||
{
|
||||
s.count -= 1;
|
||||
if s.count == 0 {
|
||||
*self.held_mut() = None;
|
||||
}
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
/// `EntityType.updateCustomEntityTag`: the egg's `minecraft:entity_data` on
|
||||
/// the mob it spawned, once the guards let it through, as
|
||||
/// `TypedEntityData.loadInto`: the mob's own compound, the data merged over
|
||||
/// it, loaded back into the same mob (`/data merge entity`'s loader).
|
||||
pub(super) fn apply_spawn_egg_entity_data(&mut self, id: u32, stack: &ItemStack) {
|
||||
let Some(i) = self.mobs.iter().position(|m| m.id == id) else {
|
||||
return;
|
||||
};
|
||||
let kind = self.mobs[i].kind.id.clone();
|
||||
let op = self.acting_player_is_op();
|
||||
let Some(data) = horizon_client::block_entity_item::spawn_egg_entity_data(stack, &kind, op)
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let own = self.mob_tree_nbt(i);
|
||||
let tag = horizon_client::data_cmd::merged(&own, data);
|
||||
// Nothing to report: the mob's own compound always loads back.
|
||||
let _ = self.apply_entity_nbt(super::commands::Target::Mob(id), tag);
|
||||
}
|
||||
/// A beam target's centre (half its height), interpolated.
|
||||
fn beam_target(&self, who: Option<Who>, alpha: f64) -> Option<glam::DVec3> {
|
||||
match who? {
|
||||
@@ -7785,624 +7622,6 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn mob_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec<Vertex>) {
|
||||
// The coarse mob bounds pay off only in dense async scenes; otherwise,
|
||||
// keep the existing exact mesh-culling path.
|
||||
if self.settings.horizon.entity_culling == EntityCullingMode::Asynchronous
|
||||
&& self
|
||||
.async_occlusion_culler
|
||||
.as_ref()
|
||||
.is_some_and(|culler| culler.camera_is_stationary())
|
||||
&& self.settings.horizon.entity_culling_precheck != 0
|
||||
&& self.mobs.len() >= self.settings.horizon.entity_culling_precheck as usize
|
||||
&& !self.pack_shadow_casters.0
|
||||
&& horizon_client::mod_api::mods().is_empty()
|
||||
{
|
||||
self.mob_geometry_inner::<true>(camera, alpha, out);
|
||||
} else {
|
||||
self.mob_geometry_inner::<false>(camera, alpha, out);
|
||||
}
|
||||
}
|
||||
|
||||
fn mob_geometry_inner<const PRECHECK: bool>(
|
||||
&self,
|
||||
camera: glam::DVec3,
|
||||
alpha: f64,
|
||||
out: &mut Vec<Vertex>,
|
||||
) {
|
||||
let atlas = &self.gui.atlas;
|
||||
let lookup = |name: &str| atlas.sprites.get(name).map(|r| [r.u0, r.v0, r.u1, r.v1]);
|
||||
let scale = self.view_scale();
|
||||
self.rendered_mobs.set(0);
|
||||
let skinning = self.skinning();
|
||||
// What custom entity model animations read of the frame.
|
||||
horizon_client::mobs::cem_anim::set_frame(horizon_client::mobs::cem_anim::Frame {
|
||||
time: self.world.game_time as f64 + alpha,
|
||||
day_time: self.world.day_time as f64 + alpha,
|
||||
dimension: match self.dim {
|
||||
Dim::Overworld => 0.0,
|
||||
Dim::Nether => -1.0,
|
||||
Dim::End => 1.0,
|
||||
},
|
||||
player: self.player.as_ref().map_or(camera.to_array(), |p| {
|
||||
[p.position.x, p.position.y, p.position.z]
|
||||
}),
|
||||
player_rot: [self.pitch, self.yaw],
|
||||
paused: self.pause,
|
||||
first_person: self.camera_type.first_person(),
|
||||
third_person_reversed: self.camera_type
|
||||
== super::perspective::CameraType::ThirdPersonFront,
|
||||
..Default::default()
|
||||
});
|
||||
for mob in &self.mobs {
|
||||
// LevelRenderer: the entity the view looks out of is not drawn
|
||||
// in first person.
|
||||
if self.hides_as_camera(mob.id) {
|
||||
continue;
|
||||
}
|
||||
let is_player = &*mob.kind.id == "minecraft:player";
|
||||
let d = glam::DVec3::new(mob.position.x, mob.position.y, mob.position.z) - camera;
|
||||
// Entity.shouldRenderAtSqrDistance: 64 times the bounding box's
|
||||
// mean edge, scaled by the view scale. RemotePlayer's box counts
|
||||
// ten times.
|
||||
let (w, h, _) = mob.dims();
|
||||
let edge = (w + h + w) / 3.0 * if is_player { 10.0 } else { 1.0 };
|
||||
let range = edge * 64.0 * scale;
|
||||
if d.length_squared() >= range * range {
|
||||
continue;
|
||||
}
|
||||
let beam = horizon_client::mobs::guardian::beam(mob, alpha as f32);
|
||||
// EntityRenderer.shouldRender: the renderer's interpolated culling box,
|
||||
// grown by 0.5 (GuardianRenderer also draws while its beam is out).
|
||||
if let Some(view) = &self.view_frustum
|
||||
&& beam.is_none()
|
||||
{
|
||||
let (lo, hi) = super::entity_culling::cull_box(mob, alpha);
|
||||
let (lo, hi) = (
|
||||
(lo - camera - glam::DVec3::splat(0.5)).as_vec3(),
|
||||
(hi - camera + glam::DVec3::splat(0.5)).as_vec3(),
|
||||
);
|
||||
if !view.intersects(lo, hi) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
let outline = self.mob_outline(mob);
|
||||
// Entity.DATA_SHARED_FLAGS_ID invisible: only the name tag draws (and
|
||||
// the outline of a glowing mob, LivingEntityRenderer.getRenderType).
|
||||
// A player keeps its armour, elytra and held items
|
||||
// (`LivingEntityRenderer.submit` skips only the body).
|
||||
let layers_only = mob.invisible && outline.is_none() && is_player;
|
||||
if mob.invisible && outline.is_none() && !layers_only {
|
||||
continue;
|
||||
}
|
||||
let culling = if PRECHECK
|
||||
&& outline.is_none()
|
||||
&& !is_player
|
||||
&& mob.kind.id.starts_with("minecraft:")
|
||||
&& [
|
||||
&mob.extra.head,
|
||||
&mob.extra.chest,
|
||||
&mob.extra.legs,
|
||||
&mob.extra.feet,
|
||||
&mob.extra.main_hand,
|
||||
&mob.extra.off_hand,
|
||||
]
|
||||
.iter()
|
||||
.all(|item| item.is_none())
|
||||
&& beam.is_none()
|
||||
{
|
||||
self.entity_cull_decision(mob, alpha)
|
||||
} else {
|
||||
EntityCullDecision::CheckVertices
|
||||
};
|
||||
if culling.hides_entity() {
|
||||
continue;
|
||||
}
|
||||
let entity_start = out.len();
|
||||
self.mark_motion(entity_start, (1 << 32) | u64::from(mob.id));
|
||||
self.mark_entity(entity_start, &mob.kind.id);
|
||||
self.rendered_mobs.set(self.rendered_mobs.get() + 1);
|
||||
// `EntityRenderer.getPackedLightCoords`: the block the mob's light
|
||||
// probe (its eye) is in, at the position this frame draws rather
|
||||
// than this tick's.
|
||||
let probe = mob.light_probe(alpha);
|
||||
let light = self.world.light(probe[0], probe[1], probe[2]);
|
||||
let mut packed = (u32::from(light >> 4) * 16) | ((u32::from(light & 15) * 16) << 8);
|
||||
// EntityRenderer.getBlockLightLevel: a burning mob has 15 block
|
||||
// light, and so do the Allay, Blaze, Magma Cube, Vex and Wither
|
||||
// renderers' overrides; the sky light stays the level's.
|
||||
if mob.fire_ticks > 0
|
||||
|| matches!(
|
||||
mob.species(),
|
||||
Species::Allay
|
||||
| Species::Blaze
|
||||
| Species::MagmaCube
|
||||
| Species::Vex
|
||||
| Species::Wither
|
||||
)
|
||||
{
|
||||
packed = (packed & 0xFF) | ((15 * 16) << 8);
|
||||
}
|
||||
// GlowSquidRenderer.getBlockLightLevel: the body brightens as the
|
||||
// dark ticks run out (the block light is the second byte).
|
||||
if mob.species() == Species::GlowSquid {
|
||||
let lit = (packed >> 12) & 15;
|
||||
let block = horizon_client::mobs::aquatic::glow_squid_block_light(mob, lit);
|
||||
packed = (packed & 0xFF) | ((block * 16) << 8);
|
||||
}
|
||||
// A native mod's own model in place of the vanilla one
|
||||
// (`native_mob_submit`), before any of the vanilla model's parts
|
||||
// (a sulfur cube's swallowed block) are drawn.
|
||||
// A player's own hook (`native_player_submit`) is asked for the
|
||||
// other players in view.
|
||||
let who = is_player
|
||||
.then(|| self.remote_player_name(mob.id))
|
||||
.flatten()
|
||||
.map(|name| NativePlayerRender { name, local: false });
|
||||
let modded = if layers_only {
|
||||
false
|
||||
} else if let Some(who) = who {
|
||||
self.mod_player_geometry(mob, who, alpha as f32, camera, packed, out)
|
||||
} else {
|
||||
!is_player && self.mod_mob_geometry(mob, alpha as f32, camera, packed, out)
|
||||
};
|
||||
if modded {
|
||||
if let Some(rgb) = outline {
|
||||
self.mark_outline(&out[entity_start..], rgb);
|
||||
if mob.invisible {
|
||||
out.truncate(entity_start);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if outline.is_none() {
|
||||
self.cull_entity_vertices(out, entity_start);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if mob.species() == Species::SulfurCube
|
||||
&& !horizon_client::mobs::trader::is_invisible(mob)
|
||||
{
|
||||
self.sulfur_cube_body_geometry(
|
||||
mob,
|
||||
alpha as f32,
|
||||
horizon_client::mobs::render::base_transform(mob, alpha as f32, camera),
|
||||
packed,
|
||||
out,
|
||||
);
|
||||
}
|
||||
// Other players on a server: PlayerRenderer's HumanoidArmorLayer.
|
||||
let armor = if is_player {
|
||||
self.worn_armor_layers(mob)
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let body = out.len();
|
||||
// The GPU path: an ordinary mob, no layers the CPU path changes after drawing them
|
||||
// (a player's, the HDR tiers below, the sulfur cube's shell). A glowing mob's
|
||||
// silhouette is posed too (`SkinFrame::outline_since`), unless it is invisible:
|
||||
// then only the outline draws.
|
||||
let silhouette_on_gpu = outline.is_none() || !mob.invisible;
|
||||
let posable = skinning
|
||||
&& !is_player
|
||||
&& !layers_only
|
||||
&& silhouette_on_gpu
|
||||
&& armor.is_empty()
|
||||
&& !matches!(
|
||||
mob.species(),
|
||||
Species::Blaze | Species::MagmaCube | Species::GlowSquid | Species::SulfurCube
|
||||
);
|
||||
let mut skinned = None;
|
||||
if posable {
|
||||
let mut models = self.skin_models.borrow_mut();
|
||||
let mut frame = self.skin_frame.borrow_mut();
|
||||
let mark = frame.mark();
|
||||
let mut skin = horizon_client::mobs::skin::Skin::new(&mut models, &mut frame);
|
||||
horizon_client::mobs::render::render_skinned(
|
||||
mob,
|
||||
alpha as f32,
|
||||
camera,
|
||||
&lookup,
|
||||
packed,
|
||||
self.res.mob_data(),
|
||||
out,
|
||||
&mut skin,
|
||||
);
|
||||
skinned = Some((mark, skin.bounds));
|
||||
} else {
|
||||
let render = if layers_only {
|
||||
horizon_client::mobs::render::render_layers
|
||||
} else {
|
||||
horizon_client::mobs::render::render_with
|
||||
};
|
||||
render(
|
||||
mob,
|
||||
alpha as f32,
|
||||
camera,
|
||||
&lookup,
|
||||
packed,
|
||||
self.res.mob_data(),
|
||||
&armor,
|
||||
out,
|
||||
);
|
||||
}
|
||||
// HDR output: mobs that are light shine by their kind, as blocks do (entity.wgsl
|
||||
// fs_hdr_mark); a tier's window lifts only their bright texels.
|
||||
if let Some(tier) = match mob.species() {
|
||||
Species::Blaze => Some(horizon_client::mesher::BRIGHT),
|
||||
Species::MagmaCube | Species::GlowSquid => Some(horizon_client::mesher::LOW),
|
||||
_ => None,
|
||||
} {
|
||||
for v in &mut out[body..] {
|
||||
v.layer |= u32::from(tier) << 16;
|
||||
}
|
||||
}
|
||||
// AvatarRenderer.shouldRenderLayers: a spectator draws no layers.
|
||||
let layers = horizon_client::mobs::render::layers_shown(mob);
|
||||
if is_player && layers {
|
||||
// PlayerRenderer's CapeLayer, WingsLayer, ParrotOnShoulderLayer
|
||||
// and SpinAttackEffectLayer. Only the cape hides with the body;
|
||||
// the others have no `isInvisible` check.
|
||||
let chest = mob.extra.chest.as_deref();
|
||||
if !layers_only {
|
||||
self.player_cape_geometry(mob, chest, camera, alpha as f32, packed, out);
|
||||
}
|
||||
if let Some((_, name)) = self
|
||||
.remote_player_names()
|
||||
.into_iter()
|
||||
.find(|(id, _)| *id == mob.id)
|
||||
{
|
||||
self.player_ears_geometry(mob, &name, camera, alpha as f32, packed, out);
|
||||
}
|
||||
self.player_elytra_geometry(mob, chest, camera, alpha as f32, packed, out);
|
||||
self.shoulder_parrots_geometry(mob, camera, alpha as f32, packed, out);
|
||||
self.spin_attack_geometry(mob, camera, alpha as f32, packed, out);
|
||||
self.stuck_in_body_geometry(mob, mob.id as i32, camera, alpha as f32, packed, out);
|
||||
}
|
||||
// Cosmetics (game/cosmetics.rs), posed on the GPU with the mob's own jobs: culled
|
||||
// with it.
|
||||
if !layers_only
|
||||
&& super::cosmetics::enabled()
|
||||
&& (is_player || &*mob.kind.id == "minecraft:mannequin")
|
||||
{
|
||||
let mark = self.skin_frame.borrow().mark();
|
||||
let base = horizon_client::mobs::render::base_transform(mob, alpha as f32, camera);
|
||||
let gpu = skinning;
|
||||
if let Some((lo, hi)) = self.cosmetics_geometry(
|
||||
mob,
|
||||
false,
|
||||
base,
|
||||
Some(camera),
|
||||
alpha as f32,
|
||||
packed,
|
||||
gpu,
|
||||
out,
|
||||
) {
|
||||
skinned = Some(match skinned {
|
||||
Some((first, Some((l, h)))) => (first, Some((l.min(lo), h.max(hi)))),
|
||||
Some((first, None)) => (first, Some((lo, hi))),
|
||||
None => (mark, Some((lo, hi))),
|
||||
});
|
||||
}
|
||||
}
|
||||
// CustomHeadLayer (a pumpkin, skull or head item) included.
|
||||
if layers {
|
||||
// Held items join the GPU path's draws, their silhouette too.
|
||||
let skin = (skinning && silhouette_on_gpu).then_some(&mut skinned);
|
||||
self.mob_block_geometry(mob, camera, alpha, packed, skin, out);
|
||||
// `native_mob_layer`: a native mod's layer, over the vanilla ones.
|
||||
self.mod_mob_layer_geometry(mob, who, alpha as f32, camera, packed, out);
|
||||
}
|
||||
if let Some(rgb) = outline {
|
||||
self.mark_outline(&out[entity_start..], rgb);
|
||||
if let Some(((first, _, _), _)) = skinned {
|
||||
let color = horizon_client::mobs::render::abgr(rgb);
|
||||
self.skin_frame.borrow_mut().outline_since(first, color);
|
||||
}
|
||||
if mob.invisible {
|
||||
out.truncate(entity_start);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// Glowing outlines and a guardian's beam remain visible independently
|
||||
// of whether the body is hidden behind terrain.
|
||||
if outline.is_none() && culling.checks_vertices() && beam.is_none() {
|
||||
// What the GPU draws of the mob counts by its bounds' corners.
|
||||
self.cull_skinned(out, entity_start, skinned);
|
||||
}
|
||||
// GuardianRenderer: the laser to the locked target.
|
||||
if let Some((time, scale)) = beam
|
||||
&& let Some(target) = self.beam_target(mob.ai.target, alpha)
|
||||
&& let Some(uv) = lookup("entity/guardian/guardian_beam")
|
||||
{
|
||||
let a = alpha;
|
||||
let eye = glam::DVec3::new(
|
||||
mob.previous.x + (mob.position.x - mob.previous.x) * a,
|
||||
mob.previous.y + (mob.position.y - mob.previous.y) * a + mob.eye_height(),
|
||||
mob.previous.z + (mob.position.z - mob.previous.z) * a,
|
||||
) - camera;
|
||||
horizon_client::mobs::guardian::beam_geometry(
|
||||
eye.as_vec3(),
|
||||
(target - camera).as_vec3(),
|
||||
time,
|
||||
scale,
|
||||
uv,
|
||||
out,
|
||||
);
|
||||
}
|
||||
}
|
||||
// What follows isn't a tracked mob: close the last one's span.
|
||||
self.mark_motion(out.len(), 0);
|
||||
self.elder_apparition_geometry(alpha, out);
|
||||
self.spawner_geometry(camera, alpha, out);
|
||||
// Projectiles: the baked vanilla arrow model, the rest as item billboards.
|
||||
let (right, up) = thrown_item_axes(self.camera_type, self.yaw, self.pitch);
|
||||
let live = self.creatures.projectiles.iter().map(|a| {
|
||||
let at = [
|
||||
a.previous.x + (a.position.x - a.previous.x) * alpha,
|
||||
a.previous.y + (a.position.y - a.previous.y) * alpha,
|
||||
a.previous.z + (a.position.z - a.previous.z) * alpha,
|
||||
];
|
||||
(a, at)
|
||||
});
|
||||
// ItemPickupParticle: a picked-up arrow flying to its collector.
|
||||
let taken = self.pickups.live.iter().filter_map(|p| match &p.taken {
|
||||
super::pickups::Taken::Arrow(a) => Some((&**a, super::pickups::position(p, alpha))),
|
||||
_ => None,
|
||||
});
|
||||
// A glowing projectile's outline: what it drew up to the next one.
|
||||
let mut glowing = None;
|
||||
for (a, at) in live.chain(taken) {
|
||||
if let Some((start, rgb)) = glowing.take() {
|
||||
self.mark_outline(&out[start..], rgb);
|
||||
}
|
||||
if a.id != 0
|
||||
&& let Some(rgb) = self.thing_outline(a.id, a.kind.id())
|
||||
{
|
||||
glowing = Some((out.len(), rgb));
|
||||
}
|
||||
let p = glam::DVec3::from_array(at) - camera;
|
||||
let c = p.as_vec3();
|
||||
// EntityRenderer.getPackedLightCoords: the block its light probe
|
||||
// (the eye) is in, at the position this frame draws rather than
|
||||
// this tick's, and not the one under its feet.
|
||||
let light = self.world.light(
|
||||
at[0].floor() as i32,
|
||||
a.light_probe_y(alpha).floor() as i32,
|
||||
at[2].floor() as i32,
|
||||
);
|
||||
let mut packed = (u32::from(light >> 4) * 16) | ((u32::from(light & 15) * 16) << 8);
|
||||
// EntityRenderer.getBlockLightLevel: a shulker bullet and anything
|
||||
// on fire are drawn at 15 block light (the fireball renderers'
|
||||
// own override); the sky light stays the level's.
|
||||
if let Some(block) = a.block_light_override() {
|
||||
packed = (packed & 0xFF) | (u32::from(block) * 16) << 8;
|
||||
}
|
||||
if a.is_arrow() {
|
||||
let Some(model) = self.arrow_model.as_ref() else {
|
||||
continue;
|
||||
};
|
||||
// SpectralArrowRenderer, and TippableArrowRenderer's `getColor() > 0`.
|
||||
let texture = if a.is_spectral_arrow() {
|
||||
"entity/projectiles/arrow_spectral"
|
||||
} else if a.arrow.as_ref().is_some_and(|s| s.effect_color > 0) {
|
||||
"entity/projectiles/arrow_tipped"
|
||||
} else {
|
||||
"entity/projectiles/arrow"
|
||||
};
|
||||
let Some(r) = atlas.sprites.get(texture).copied() else {
|
||||
continue;
|
||||
};
|
||||
// ArrowRenderer: `Mth.lerp(partialTick, state.yRotO, state.yRot)`
|
||||
// and the same for the pitch, so a turning arrow is drawn
|
||||
// between the two ticks it turned over.
|
||||
let alpha32 = alpha as f32;
|
||||
let yaw = a.previous_yaw + alpha32 * (a.yaw - a.previous_yaw);
|
||||
let pitch = a.previous_pitch + alpha32 * (a.pitch - a.previous_pitch);
|
||||
let m = glam::Mat4::from_translation(c)
|
||||
* glam::Mat4::from_rotation_y((yaw - 90.0).to_radians())
|
||||
* glam::Mat4::from_rotation_z(pitch.to_radians());
|
||||
// ArrowRenderer: `state.shake = shakeTime - partialTicks`; ArrowModel.setupAnim
|
||||
// rolls the root while the shake timer runs.
|
||||
let shake = a
|
||||
.arrow
|
||||
.as_ref()
|
||||
.map_or(0.0, |s| s.shake as f32 - alpha as f32);
|
||||
let roll = if shake > 0.0 {
|
||||
-(shake * 3.0).msin() * shake * (std::f32::consts::PI / 180.0)
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
let pose_for = |part: &horizon_client::mobs::Part| {
|
||||
let mut pose = part.pose;
|
||||
if std::ptr::eq(part, model) {
|
||||
pose[5] += roll;
|
||||
}
|
||||
pose
|
||||
};
|
||||
horizon_client::mobs::render_model(
|
||||
model,
|
||||
m,
|
||||
&pose_for,
|
||||
[r.u0, r.v0, r.u1, r.v1],
|
||||
packed,
|
||||
0,
|
||||
out,
|
||||
);
|
||||
if a.arrow.as_ref().is_some_and(|s| s.on_fire()) {
|
||||
flame_geometry(atlas, c, 0.5, 0.5, self.yaw, light, out);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// ThrownTridentRenderer.
|
||||
if matches!(a.kind, ProjectileKind::Trident) {
|
||||
let Some(model) = self.trident_model.as_ref() else {
|
||||
continue;
|
||||
};
|
||||
let Some(r) = atlas.sprites.get("entity/trident/trident").copied() else {
|
||||
continue;
|
||||
};
|
||||
let m = glam::Mat4::from_translation(c)
|
||||
* glam::Mat4::from_rotation_y((a.yaw - 90.0).to_radians())
|
||||
* glam::Mat4::from_rotation_z((a.pitch + 90.0).to_radians());
|
||||
let poses = horizon_client::mobs::anim::Poses::new(model);
|
||||
horizon_client::mobs::render::draw(
|
||||
model,
|
||||
m,
|
||||
&poses,
|
||||
[r.u0, r.v0, r.u1, r.v1],
|
||||
0xFFFF_FFFF,
|
||||
packed,
|
||||
0,
|
||||
out,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
// WindChargeRenderer.
|
||||
if matches!(
|
||||
a.kind,
|
||||
ProjectileKind::WindCharge | ProjectileKind::BreezeWindCharge
|
||||
) {
|
||||
self.wind_charge_geometry(a, c, packed, alpha as f32, out);
|
||||
continue;
|
||||
}
|
||||
// ShulkerBulletRenderer: the tumbling bullet model, then a larger
|
||||
// translucent copy of it (colour 0x26FFFFFF); always 15 block light.
|
||||
if matches!(a.kind, ProjectileKind::ShulkerBullet { .. }) {
|
||||
let model = &self.entity_models.shulker_bullet;
|
||||
if let (Some(model), Some(r)) =
|
||||
(model, atlas.sprites.get("entity/shulker/spark").copied())
|
||||
{
|
||||
use horizon_core::geometry::trig;
|
||||
let tc = a.age as f32 + alpha as f32;
|
||||
let mut poses = horizon_client::mobs::anim::Poses::new(model);
|
||||
poses.yrot("main", a.yaw.to_radians());
|
||||
poses.xrot("main", a.pitch.to_radians());
|
||||
let light = (packed & 0xFF) | ((15 * 16) << 8);
|
||||
let base = glam::Mat4::from_translation(c)
|
||||
* glam::Mat4::from_translation(glam::Vec3::new(0.0, 0.15, 0.0))
|
||||
* glam::Mat4::from_rotation_y(
|
||||
(trig::sin(f64::from(tc * 0.1)) * 180.0).to_radians(),
|
||||
)
|
||||
* glam::Mat4::from_rotation_x(
|
||||
(trig::cos(f64::from(tc * 0.1)) * 180.0).to_radians(),
|
||||
)
|
||||
* glam::Mat4::from_rotation_z(
|
||||
(trig::sin(f64::from(tc * 0.15)) * 360.0).to_radians(),
|
||||
)
|
||||
* glam::Mat4::from_scale(glam::Vec3::new(-0.5, -0.5, 0.5));
|
||||
let uv = [r.u0, r.v0, r.u1, r.v1];
|
||||
horizon_client::mobs::render::draw(
|
||||
model,
|
||||
base,
|
||||
&poses,
|
||||
uv,
|
||||
0xFFFF_FFFF,
|
||||
light,
|
||||
0,
|
||||
out,
|
||||
);
|
||||
// The translucent shell blends over the opaque core
|
||||
// (`RenderTypes.entityTranslucent`).
|
||||
let shell = out.len();
|
||||
horizon_client::mobs::render::draw(
|
||||
model,
|
||||
base * glam::Mat4::from_scale(glam::Vec3::splat(1.5)),
|
||||
&poses,
|
||||
uv,
|
||||
0x26FF_FFFF,
|
||||
light,
|
||||
0,
|
||||
out,
|
||||
);
|
||||
horizon_client::gfx::mark_oit(&mut out[shell..]);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// WitherSkullRenderer: the skull model facing its flight.
|
||||
if let Some(dangerous) = super::wither::skull_kind(a) {
|
||||
let model = &self.entity_models.wither_skull;
|
||||
let tex = if dangerous {
|
||||
"entity/wither/wither_invulnerable"
|
||||
} else {
|
||||
"entity/wither/wither"
|
||||
};
|
||||
if let (Some(model), Some(r)) = (model, atlas.sprites.get(tex).copied()) {
|
||||
let mut poses = horizon_client::mobs::anim::Poses::new(model);
|
||||
poses.yrot("head", a.yaw.to_radians());
|
||||
poses.xrot("head", a.pitch.to_radians());
|
||||
let base = glam::Mat4::from_translation(c)
|
||||
* glam::Mat4::from_scale(glam::Vec3::new(-1.0, -1.0, 1.0));
|
||||
horizon_client::mobs::render::draw(
|
||||
model,
|
||||
base,
|
||||
&poses,
|
||||
[r.u0, r.v0, r.u1, r.v1],
|
||||
0xFFFF_FFFF,
|
||||
packed,
|
||||
0,
|
||||
out,
|
||||
);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(stack) = a.drawn_item() {
|
||||
let owner = super::item_draw::owner_at(at, 0.0, a.kind.id());
|
||||
let state = self.resolve_item(&stack, DisplayContext::Ground, Some(&owner), 0);
|
||||
let pose = glam::Mat4::from_translation(c)
|
||||
* glam::Mat4::from_quat(self.camera_orientation());
|
||||
self.submit_item_world(&state, pose, packed, out);
|
||||
continue;
|
||||
}
|
||||
// `Entity.displayFireAnimation`: a thrown item that flew through a
|
||||
// fire block burns (`ThrowableProjectile.tick`'s
|
||||
// `applyEffectsFromBlocks`), and ThrownItemRenderer draws the flame
|
||||
// over its box as any other entity renderer does.
|
||||
if a.on_fire() {
|
||||
let size = a.kind.size() as f32;
|
||||
flame_geometry(atlas, c, size, size, self.yaw, light, out);
|
||||
}
|
||||
// `ThrownItemRenderer` for a fireball: the item `Fireball.getItem`
|
||||
// returns (a fire charge until `setItem` gave it another), its model
|
||||
// at the renderer's scale for the kind. `packed` is already 15, the
|
||||
// renderer's full bright.
|
||||
if let Some(stack) = a.item.as_ref() {
|
||||
let owner = super::item_draw::owner_at(at, 0.0, a.kind.id());
|
||||
let state = self.resolve_item(stack, DisplayContext::Ground, Some(&owner), 0);
|
||||
let pose = glam::Mat4::from_translation(c)
|
||||
* glam::Mat4::from_quat(self.camera_orientation())
|
||||
* glam::Mat4::from_scale(glam::Vec3::splat(a.sprite_scale()));
|
||||
self.submit_item_world(&state, pose, packed, out);
|
||||
continue;
|
||||
}
|
||||
let Some(name) = a.item_sprite() else {
|
||||
continue;
|
||||
};
|
||||
let Some(r) = atlas.sprites.get(name).copied() else {
|
||||
continue;
|
||||
};
|
||||
let lift = is_item_billboard(&a.kind);
|
||||
let quad = thrown_item_quad(c, right, up, a.sprite_scale(), lift);
|
||||
for (v, uv) in quad
|
||||
.iter()
|
||||
.zip([[r.u0, r.v1], [r.u1, r.v1], [r.u1, r.v0], [r.u0, r.v0]])
|
||||
{
|
||||
out.push(Vertex {
|
||||
normal: [0; 4],
|
||||
pos: v.to_array(),
|
||||
uv,
|
||||
layer: 0,
|
||||
color: 0xFFFF_FFFF,
|
||||
light: packed,
|
||||
|
||||
glint_uv: [0.0, 0.0],
|
||||
});
|
||||
}
|
||||
}
|
||||
if let Some((start, rgb)) = glowing {
|
||||
self.mark_outline(&out[start..], rgb);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `ThrownItemRenderer.submit` turns the item by `CameraRenderState.orientation`
|
||||
/// (`Camera.setRotation`): the billboard's right and up axes are the camera's.
|
||||
|
||||
@@ -0,0 +1,629 @@
|
||||
//! Drawing the mobs, players and projectiles the client sees, each frame (`mob_geometry`).
|
||||
use super::*;
|
||||
|
||||
impl Game<'_> {
|
||||
pub(in crate::game) fn mob_geometry(
|
||||
&self,
|
||||
camera: glam::DVec3,
|
||||
alpha: f64,
|
||||
out: &mut Vec<Vertex>,
|
||||
) {
|
||||
// The coarse mob bounds pay off only in dense async scenes; otherwise,
|
||||
// keep the existing exact mesh-culling path.
|
||||
if self.settings.horizon.entity_culling == EntityCullingMode::Asynchronous
|
||||
&& self
|
||||
.async_occlusion_culler
|
||||
.as_ref()
|
||||
.is_some_and(|culler| culler.camera_is_stationary())
|
||||
&& self.settings.horizon.entity_culling_precheck != 0
|
||||
&& self.mobs.len() >= self.settings.horizon.entity_culling_precheck as usize
|
||||
&& !self.pack_shadow_casters.0
|
||||
&& horizon_client::mod_api::mods().is_empty()
|
||||
{
|
||||
self.mob_geometry_inner::<true>(camera, alpha, out);
|
||||
} else {
|
||||
self.mob_geometry_inner::<false>(camera, alpha, out);
|
||||
}
|
||||
}
|
||||
|
||||
fn mob_geometry_inner<const PRECHECK: bool>(
|
||||
&self,
|
||||
camera: glam::DVec3,
|
||||
alpha: f64,
|
||||
out: &mut Vec<Vertex>,
|
||||
) {
|
||||
let atlas = &self.gui.atlas;
|
||||
let lookup = |name: &str| atlas.sprites.get(name).map(|r| [r.u0, r.v0, r.u1, r.v1]);
|
||||
let scale = self.view_scale();
|
||||
self.rendered_mobs.set(0);
|
||||
let skinning = self.skinning();
|
||||
// What custom entity model animations read of the frame.
|
||||
horizon_client::mobs::cem_anim::set_frame(horizon_client::mobs::cem_anim::Frame {
|
||||
time: self.world.game_time as f64 + alpha,
|
||||
day_time: self.world.day_time as f64 + alpha,
|
||||
dimension: match self.dim {
|
||||
Dim::Overworld => 0.0,
|
||||
Dim::Nether => -1.0,
|
||||
Dim::End => 1.0,
|
||||
},
|
||||
player: self.player.as_ref().map_or(camera.to_array(), |p| {
|
||||
[p.position.x, p.position.y, p.position.z]
|
||||
}),
|
||||
player_rot: [self.pitch, self.yaw],
|
||||
paused: self.pause,
|
||||
first_person: self.camera_type.first_person(),
|
||||
third_person_reversed: self.camera_type
|
||||
== super::super::perspective::CameraType::ThirdPersonFront,
|
||||
..Default::default()
|
||||
});
|
||||
for mob in &self.mobs {
|
||||
// LevelRenderer: the entity the view looks out of is not drawn
|
||||
// in first person.
|
||||
if self.hides_as_camera(mob.id) {
|
||||
continue;
|
||||
}
|
||||
let is_player = &*mob.kind.id == "minecraft:player";
|
||||
let d = glam::DVec3::new(mob.position.x, mob.position.y, mob.position.z) - camera;
|
||||
// Entity.shouldRenderAtSqrDistance: 64 times the bounding box's
|
||||
// mean edge, scaled by the view scale. RemotePlayer's box counts
|
||||
// ten times.
|
||||
let (w, h, _) = mob.dims();
|
||||
let edge = (w + h + w) / 3.0 * if is_player { 10.0 } else { 1.0 };
|
||||
let range = edge * 64.0 * scale;
|
||||
if d.length_squared() >= range * range {
|
||||
continue;
|
||||
}
|
||||
let beam = horizon_client::mobs::guardian::beam(mob, alpha as f32);
|
||||
// EntityRenderer.shouldRender: the renderer's interpolated culling box,
|
||||
// grown by 0.5 (GuardianRenderer also draws while its beam is out).
|
||||
if let Some(view) = &self.view_frustum
|
||||
&& beam.is_none()
|
||||
{
|
||||
let (lo, hi) = super::super::entity_culling::cull_box(mob, alpha);
|
||||
let (lo, hi) = (
|
||||
(lo - camera - glam::DVec3::splat(0.5)).as_vec3(),
|
||||
(hi - camera + glam::DVec3::splat(0.5)).as_vec3(),
|
||||
);
|
||||
if !view.intersects(lo, hi) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
let outline = self.mob_outline(mob);
|
||||
// Entity.DATA_SHARED_FLAGS_ID invisible: only the name tag draws (and
|
||||
// the outline of a glowing mob, LivingEntityRenderer.getRenderType).
|
||||
// A player keeps its armour, elytra and held items
|
||||
// (`LivingEntityRenderer.submit` skips only the body).
|
||||
let layers_only = mob.invisible && outline.is_none() && is_player;
|
||||
if mob.invisible && outline.is_none() && !layers_only {
|
||||
continue;
|
||||
}
|
||||
let culling = if PRECHECK
|
||||
&& outline.is_none()
|
||||
&& !is_player
|
||||
&& mob.kind.id.starts_with("minecraft:")
|
||||
&& [
|
||||
&mob.extra.head,
|
||||
&mob.extra.chest,
|
||||
&mob.extra.legs,
|
||||
&mob.extra.feet,
|
||||
&mob.extra.main_hand,
|
||||
&mob.extra.off_hand,
|
||||
]
|
||||
.iter()
|
||||
.all(|item| item.is_none())
|
||||
&& beam.is_none()
|
||||
{
|
||||
self.entity_cull_decision(mob, alpha)
|
||||
} else {
|
||||
EntityCullDecision::CheckVertices
|
||||
};
|
||||
if culling.hides_entity() {
|
||||
continue;
|
||||
}
|
||||
let entity_start = out.len();
|
||||
self.mark_motion(entity_start, (1 << 32) | u64::from(mob.id));
|
||||
self.mark_entity(entity_start, &mob.kind.id);
|
||||
self.rendered_mobs.set(self.rendered_mobs.get() + 1);
|
||||
// `EntityRenderer.getPackedLightCoords`: the block the mob's light
|
||||
// probe (its eye) is in, at the position this frame draws rather
|
||||
// than this tick's.
|
||||
let probe = mob.light_probe(alpha);
|
||||
let light = self.world.light(probe[0], probe[1], probe[2]);
|
||||
let mut packed = (u32::from(light >> 4) * 16) | ((u32::from(light & 15) * 16) << 8);
|
||||
// EntityRenderer.getBlockLightLevel: a burning mob has 15 block
|
||||
// light, and so do the Allay, Blaze, Magma Cube, Vex and Wither
|
||||
// renderers' overrides; the sky light stays the level's.
|
||||
if mob.fire_ticks > 0
|
||||
|| matches!(
|
||||
mob.species(),
|
||||
Species::Allay
|
||||
| Species::Blaze
|
||||
| Species::MagmaCube
|
||||
| Species::Vex
|
||||
| Species::Wither
|
||||
)
|
||||
{
|
||||
packed = (packed & 0xFF) | ((15 * 16) << 8);
|
||||
}
|
||||
// GlowSquidRenderer.getBlockLightLevel: the body brightens as the
|
||||
// dark ticks run out (the block light is the second byte).
|
||||
if mob.species() == Species::GlowSquid {
|
||||
let lit = (packed >> 12) & 15;
|
||||
let block = horizon_client::mobs::aquatic::glow_squid_block_light(mob, lit);
|
||||
packed = (packed & 0xFF) | ((block * 16) << 8);
|
||||
}
|
||||
// A native mod's own model in place of the vanilla one
|
||||
// (`native_mob_submit`), before any of the vanilla model's parts
|
||||
// (a sulfur cube's swallowed block) are drawn.
|
||||
// A player's own hook (`native_player_submit`) is asked for the
|
||||
// other players in view.
|
||||
let who = is_player
|
||||
.then(|| self.remote_player_name(mob.id))
|
||||
.flatten()
|
||||
.map(|name| NativePlayerRender { name, local: false });
|
||||
let modded = if layers_only {
|
||||
false
|
||||
} else if let Some(who) = who {
|
||||
self.mod_player_geometry(mob, who, alpha as f32, camera, packed, out)
|
||||
} else {
|
||||
!is_player && self.mod_mob_geometry(mob, alpha as f32, camera, packed, out)
|
||||
};
|
||||
if modded {
|
||||
if let Some(rgb) = outline {
|
||||
self.mark_outline(&out[entity_start..], rgb);
|
||||
if mob.invisible {
|
||||
out.truncate(entity_start);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if outline.is_none() {
|
||||
self.cull_entity_vertices(out, entity_start);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if mob.species() == Species::SulfurCube
|
||||
&& !horizon_client::mobs::trader::is_invisible(mob)
|
||||
{
|
||||
self.sulfur_cube_body_geometry(
|
||||
mob,
|
||||
alpha as f32,
|
||||
horizon_client::mobs::render::base_transform(mob, alpha as f32, camera),
|
||||
packed,
|
||||
out,
|
||||
);
|
||||
}
|
||||
// Other players on a server: PlayerRenderer's HumanoidArmorLayer.
|
||||
let armor = if is_player {
|
||||
self.worn_armor_layers(mob)
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let body = out.len();
|
||||
// The GPU path: an ordinary mob, no layers the CPU path changes after drawing them
|
||||
// (a player's, the HDR tiers below, the sulfur cube's shell). A glowing mob's
|
||||
// silhouette is posed too (`SkinFrame::outline_since`), unless it is invisible:
|
||||
// then only the outline draws.
|
||||
let silhouette_on_gpu = outline.is_none() || !mob.invisible;
|
||||
let posable = skinning
|
||||
&& !is_player
|
||||
&& !layers_only
|
||||
&& silhouette_on_gpu
|
||||
&& armor.is_empty()
|
||||
&& !matches!(
|
||||
mob.species(),
|
||||
Species::Blaze | Species::MagmaCube | Species::GlowSquid | Species::SulfurCube
|
||||
);
|
||||
let mut skinned = None;
|
||||
if posable {
|
||||
let mut models = self.skin_models.borrow_mut();
|
||||
let mut frame = self.skin_frame.borrow_mut();
|
||||
let mark = frame.mark();
|
||||
let mut skin = horizon_client::mobs::skin::Skin::new(&mut models, &mut frame);
|
||||
horizon_client::mobs::render::render_skinned(
|
||||
mob,
|
||||
alpha as f32,
|
||||
camera,
|
||||
&lookup,
|
||||
packed,
|
||||
self.res.mob_data(),
|
||||
out,
|
||||
&mut skin,
|
||||
);
|
||||
skinned = Some((mark, skin.bounds));
|
||||
} else {
|
||||
let render = if layers_only {
|
||||
horizon_client::mobs::render::render_layers
|
||||
} else {
|
||||
horizon_client::mobs::render::render_with
|
||||
};
|
||||
render(
|
||||
mob,
|
||||
alpha as f32,
|
||||
camera,
|
||||
&lookup,
|
||||
packed,
|
||||
self.res.mob_data(),
|
||||
&armor,
|
||||
out,
|
||||
);
|
||||
}
|
||||
// HDR output: mobs that are light shine by their kind, as blocks do (entity.wgsl
|
||||
// fs_hdr_mark); a tier's window lifts only their bright texels.
|
||||
if let Some(tier) = match mob.species() {
|
||||
Species::Blaze => Some(horizon_client::mesher::BRIGHT),
|
||||
Species::MagmaCube | Species::GlowSquid => Some(horizon_client::mesher::LOW),
|
||||
_ => None,
|
||||
} {
|
||||
for v in &mut out[body..] {
|
||||
v.layer |= u32::from(tier) << 16;
|
||||
}
|
||||
}
|
||||
// AvatarRenderer.shouldRenderLayers: a spectator draws no layers.
|
||||
let layers = horizon_client::mobs::render::layers_shown(mob);
|
||||
if is_player && layers {
|
||||
// PlayerRenderer's CapeLayer, WingsLayer, ParrotOnShoulderLayer
|
||||
// and SpinAttackEffectLayer. Only the cape hides with the body;
|
||||
// the others have no `isInvisible` check.
|
||||
let chest = mob.extra.chest.as_deref();
|
||||
if !layers_only {
|
||||
self.player_cape_geometry(mob, chest, camera, alpha as f32, packed, out);
|
||||
}
|
||||
if let Some((_, name)) = self
|
||||
.remote_player_names()
|
||||
.into_iter()
|
||||
.find(|(id, _)| *id == mob.id)
|
||||
{
|
||||
self.player_ears_geometry(mob, &name, camera, alpha as f32, packed, out);
|
||||
}
|
||||
self.player_elytra_geometry(mob, chest, camera, alpha as f32, packed, out);
|
||||
self.shoulder_parrots_geometry(mob, camera, alpha as f32, packed, out);
|
||||
self.spin_attack_geometry(mob, camera, alpha as f32, packed, out);
|
||||
self.stuck_in_body_geometry(mob, mob.id as i32, camera, alpha as f32, packed, out);
|
||||
}
|
||||
// Cosmetics (game/cosmetics.rs), posed on the GPU with the mob's own jobs: culled
|
||||
// with it.
|
||||
if !layers_only
|
||||
&& super::super::cosmetics::enabled()
|
||||
&& (is_player || &*mob.kind.id == "minecraft:mannequin")
|
||||
{
|
||||
let mark = self.skin_frame.borrow().mark();
|
||||
let base = horizon_client::mobs::render::base_transform(mob, alpha as f32, camera);
|
||||
let gpu = skinning;
|
||||
if let Some((lo, hi)) = self.cosmetics_geometry(
|
||||
mob,
|
||||
false,
|
||||
base,
|
||||
Some(camera),
|
||||
alpha as f32,
|
||||
packed,
|
||||
gpu,
|
||||
out,
|
||||
) {
|
||||
skinned = Some(match skinned {
|
||||
Some((first, Some((l, h)))) => (first, Some((l.min(lo), h.max(hi)))),
|
||||
Some((first, None)) => (first, Some((lo, hi))),
|
||||
None => (mark, Some((lo, hi))),
|
||||
});
|
||||
}
|
||||
}
|
||||
// CustomHeadLayer (a pumpkin, skull or head item) included.
|
||||
if layers {
|
||||
// Held items join the GPU path's draws, their silhouette too.
|
||||
let skin = (skinning && silhouette_on_gpu).then_some(&mut skinned);
|
||||
self.mob_block_geometry(mob, camera, alpha, packed, skin, out);
|
||||
// `native_mob_layer`: a native mod's layer, over the vanilla ones.
|
||||
self.mod_mob_layer_geometry(mob, who, alpha as f32, camera, packed, out);
|
||||
}
|
||||
if let Some(rgb) = outline {
|
||||
self.mark_outline(&out[entity_start..], rgb);
|
||||
if let Some(((first, _, _), _)) = skinned {
|
||||
let color = horizon_client::mobs::render::abgr(rgb);
|
||||
self.skin_frame.borrow_mut().outline_since(first, color);
|
||||
}
|
||||
if mob.invisible {
|
||||
out.truncate(entity_start);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// Glowing outlines and a guardian's beam remain visible independently
|
||||
// of whether the body is hidden behind terrain.
|
||||
if outline.is_none() && culling.checks_vertices() && beam.is_none() {
|
||||
// What the GPU draws of the mob counts by its bounds' corners.
|
||||
self.cull_skinned(out, entity_start, skinned);
|
||||
}
|
||||
// GuardianRenderer: the laser to the locked target.
|
||||
if let Some((time, scale)) = beam
|
||||
&& let Some(target) = self.beam_target(mob.ai.target, alpha)
|
||||
&& let Some(uv) = lookup("entity/guardian/guardian_beam")
|
||||
{
|
||||
let a = alpha;
|
||||
let eye = glam::DVec3::new(
|
||||
mob.previous.x + (mob.position.x - mob.previous.x) * a,
|
||||
mob.previous.y + (mob.position.y - mob.previous.y) * a + mob.eye_height(),
|
||||
mob.previous.z + (mob.position.z - mob.previous.z) * a,
|
||||
) - camera;
|
||||
horizon_client::mobs::guardian::beam_geometry(
|
||||
eye.as_vec3(),
|
||||
(target - camera).as_vec3(),
|
||||
time,
|
||||
scale,
|
||||
uv,
|
||||
out,
|
||||
);
|
||||
}
|
||||
}
|
||||
// What follows isn't a tracked mob: close the last one's span.
|
||||
self.mark_motion(out.len(), 0);
|
||||
self.elder_apparition_geometry(alpha, out);
|
||||
self.spawner_geometry(camera, alpha, out);
|
||||
// Projectiles: the baked vanilla arrow model, the rest as item billboards.
|
||||
let (right, up) = thrown_item_axes(self.camera_type, self.yaw, self.pitch);
|
||||
let live = self.creatures.projectiles.iter().map(|a| {
|
||||
let at = [
|
||||
a.previous.x + (a.position.x - a.previous.x) * alpha,
|
||||
a.previous.y + (a.position.y - a.previous.y) * alpha,
|
||||
a.previous.z + (a.position.z - a.previous.z) * alpha,
|
||||
];
|
||||
(a, at)
|
||||
});
|
||||
// ItemPickupParticle: a picked-up arrow flying to its collector.
|
||||
let taken = self.pickups.live.iter().filter_map(|p| match &p.taken {
|
||||
super::super::pickups::Taken::Arrow(a) => {
|
||||
Some((&**a, super::super::pickups::position(p, alpha)))
|
||||
}
|
||||
_ => None,
|
||||
});
|
||||
// A glowing projectile's outline: what it drew up to the next one.
|
||||
let mut glowing = None;
|
||||
for (a, at) in live.chain(taken) {
|
||||
if let Some((start, rgb)) = glowing.take() {
|
||||
self.mark_outline(&out[start..], rgb);
|
||||
}
|
||||
if a.id != 0
|
||||
&& let Some(rgb) = self.thing_outline(a.id, a.kind.id())
|
||||
{
|
||||
glowing = Some((out.len(), rgb));
|
||||
}
|
||||
let p = glam::DVec3::from_array(at) - camera;
|
||||
let c = p.as_vec3();
|
||||
// EntityRenderer.getPackedLightCoords: the block its light probe
|
||||
// (the eye) is in, at the position this frame draws rather than
|
||||
// this tick's, and not the one under its feet.
|
||||
let light = self.world.light(
|
||||
at[0].floor() as i32,
|
||||
a.light_probe_y(alpha).floor() as i32,
|
||||
at[2].floor() as i32,
|
||||
);
|
||||
let mut packed = (u32::from(light >> 4) * 16) | ((u32::from(light & 15) * 16) << 8);
|
||||
// EntityRenderer.getBlockLightLevel: a shulker bullet and anything
|
||||
// on fire are drawn at 15 block light (the fireball renderers'
|
||||
// own override); the sky light stays the level's.
|
||||
if let Some(block) = a.block_light_override() {
|
||||
packed = (packed & 0xFF) | (u32::from(block) * 16) << 8;
|
||||
}
|
||||
if a.is_arrow() {
|
||||
let Some(model) = self.arrow_model.as_ref() else {
|
||||
continue;
|
||||
};
|
||||
// SpectralArrowRenderer, and TippableArrowRenderer's `getColor() > 0`.
|
||||
let texture = if a.is_spectral_arrow() {
|
||||
"entity/projectiles/arrow_spectral"
|
||||
} else if a.arrow.as_ref().is_some_and(|s| s.effect_color > 0) {
|
||||
"entity/projectiles/arrow_tipped"
|
||||
} else {
|
||||
"entity/projectiles/arrow"
|
||||
};
|
||||
let Some(r) = atlas.sprites.get(texture).copied() else {
|
||||
continue;
|
||||
};
|
||||
// ArrowRenderer: `Mth.lerp(partialTick, state.yRotO, state.yRot)`
|
||||
// and the same for the pitch, so a turning arrow is drawn
|
||||
// between the two ticks it turned over.
|
||||
let alpha32 = alpha as f32;
|
||||
let yaw = a.previous_yaw + alpha32 * (a.yaw - a.previous_yaw);
|
||||
let pitch = a.previous_pitch + alpha32 * (a.pitch - a.previous_pitch);
|
||||
let m = glam::Mat4::from_translation(c)
|
||||
* glam::Mat4::from_rotation_y((yaw - 90.0).to_radians())
|
||||
* glam::Mat4::from_rotation_z(pitch.to_radians());
|
||||
// ArrowRenderer: `state.shake = shakeTime - partialTicks`; ArrowModel.setupAnim
|
||||
// rolls the root while the shake timer runs.
|
||||
let shake = a
|
||||
.arrow
|
||||
.as_ref()
|
||||
.map_or(0.0, |s| s.shake as f32 - alpha as f32);
|
||||
let roll = if shake > 0.0 {
|
||||
-(shake * 3.0).msin() * shake * (std::f32::consts::PI / 180.0)
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
let pose_for = |part: &horizon_client::mobs::Part| {
|
||||
let mut pose = part.pose;
|
||||
if std::ptr::eq(part, model) {
|
||||
pose[5] += roll;
|
||||
}
|
||||
pose
|
||||
};
|
||||
horizon_client::mobs::render_model(
|
||||
model,
|
||||
m,
|
||||
&pose_for,
|
||||
[r.u0, r.v0, r.u1, r.v1],
|
||||
packed,
|
||||
0,
|
||||
out,
|
||||
);
|
||||
if a.arrow.as_ref().is_some_and(|s| s.on_fire()) {
|
||||
flame_geometry(atlas, c, 0.5, 0.5, self.yaw, light, out);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// ThrownTridentRenderer.
|
||||
if matches!(a.kind, ProjectileKind::Trident) {
|
||||
let Some(model) = self.trident_model.as_ref() else {
|
||||
continue;
|
||||
};
|
||||
let Some(r) = atlas.sprites.get("entity/trident/trident").copied() else {
|
||||
continue;
|
||||
};
|
||||
let m = glam::Mat4::from_translation(c)
|
||||
* glam::Mat4::from_rotation_y((a.yaw - 90.0).to_radians())
|
||||
* glam::Mat4::from_rotation_z((a.pitch + 90.0).to_radians());
|
||||
let poses = horizon_client::mobs::anim::Poses::new(model);
|
||||
horizon_client::mobs::render::draw(
|
||||
model,
|
||||
m,
|
||||
&poses,
|
||||
[r.u0, r.v0, r.u1, r.v1],
|
||||
0xFFFF_FFFF,
|
||||
packed,
|
||||
0,
|
||||
out,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
// WindChargeRenderer.
|
||||
if matches!(
|
||||
a.kind,
|
||||
ProjectileKind::WindCharge | ProjectileKind::BreezeWindCharge
|
||||
) {
|
||||
self.wind_charge_geometry(a, c, packed, alpha as f32, out);
|
||||
continue;
|
||||
}
|
||||
// ShulkerBulletRenderer: the tumbling bullet model, then a larger
|
||||
// translucent copy of it (colour 0x26FFFFFF); always 15 block light.
|
||||
if matches!(a.kind, ProjectileKind::ShulkerBullet { .. }) {
|
||||
let model = &self.entity_models.shulker_bullet;
|
||||
if let (Some(model), Some(r)) =
|
||||
(model, atlas.sprites.get("entity/shulker/spark").copied())
|
||||
{
|
||||
use horizon_core::geometry::trig;
|
||||
let tc = a.age as f32 + alpha as f32;
|
||||
let mut poses = horizon_client::mobs::anim::Poses::new(model);
|
||||
poses.yrot("main", a.yaw.to_radians());
|
||||
poses.xrot("main", a.pitch.to_radians());
|
||||
let light = (packed & 0xFF) | ((15 * 16) << 8);
|
||||
let base = glam::Mat4::from_translation(c)
|
||||
* glam::Mat4::from_translation(glam::Vec3::new(0.0, 0.15, 0.0))
|
||||
* glam::Mat4::from_rotation_y(
|
||||
(trig::sin(f64::from(tc * 0.1)) * 180.0).to_radians(),
|
||||
)
|
||||
* glam::Mat4::from_rotation_x(
|
||||
(trig::cos(f64::from(tc * 0.1)) * 180.0).to_radians(),
|
||||
)
|
||||
* glam::Mat4::from_rotation_z(
|
||||
(trig::sin(f64::from(tc * 0.15)) * 360.0).to_radians(),
|
||||
)
|
||||
* glam::Mat4::from_scale(glam::Vec3::new(-0.5, -0.5, 0.5));
|
||||
let uv = [r.u0, r.v0, r.u1, r.v1];
|
||||
horizon_client::mobs::render::draw(
|
||||
model,
|
||||
base,
|
||||
&poses,
|
||||
uv,
|
||||
0xFFFF_FFFF,
|
||||
light,
|
||||
0,
|
||||
out,
|
||||
);
|
||||
// The translucent shell blends over the opaque core
|
||||
// (`RenderTypes.entityTranslucent`).
|
||||
let shell = out.len();
|
||||
horizon_client::mobs::render::draw(
|
||||
model,
|
||||
base * glam::Mat4::from_scale(glam::Vec3::splat(1.5)),
|
||||
&poses,
|
||||
uv,
|
||||
0x26FF_FFFF,
|
||||
light,
|
||||
0,
|
||||
out,
|
||||
);
|
||||
horizon_client::gfx::mark_oit(&mut out[shell..]);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// WitherSkullRenderer: the skull model facing its flight.
|
||||
if let Some(dangerous) = super::super::wither::skull_kind(a) {
|
||||
let model = &self.entity_models.wither_skull;
|
||||
let tex = if dangerous {
|
||||
"entity/wither/wither_invulnerable"
|
||||
} else {
|
||||
"entity/wither/wither"
|
||||
};
|
||||
if let (Some(model), Some(r)) = (model, atlas.sprites.get(tex).copied()) {
|
||||
let mut poses = horizon_client::mobs::anim::Poses::new(model);
|
||||
poses.yrot("head", a.yaw.to_radians());
|
||||
poses.xrot("head", a.pitch.to_radians());
|
||||
let base = glam::Mat4::from_translation(c)
|
||||
* glam::Mat4::from_scale(glam::Vec3::new(-1.0, -1.0, 1.0));
|
||||
horizon_client::mobs::render::draw(
|
||||
model,
|
||||
base,
|
||||
&poses,
|
||||
[r.u0, r.v0, r.u1, r.v1],
|
||||
0xFFFF_FFFF,
|
||||
packed,
|
||||
0,
|
||||
out,
|
||||
);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(stack) = a.drawn_item() {
|
||||
let owner = super::super::item_draw::owner_at(at, 0.0, a.kind.id());
|
||||
let state = self.resolve_item(&stack, DisplayContext::Ground, Some(&owner), 0);
|
||||
let pose = glam::Mat4::from_translation(c)
|
||||
* glam::Mat4::from_quat(self.camera_orientation());
|
||||
self.submit_item_world(&state, pose, packed, out);
|
||||
continue;
|
||||
}
|
||||
// `Entity.displayFireAnimation`: a thrown item that flew through a
|
||||
// fire block burns (`ThrowableProjectile.tick`'s
|
||||
// `applyEffectsFromBlocks`), and ThrownItemRenderer draws the flame
|
||||
// over its box as any other entity renderer does.
|
||||
if a.on_fire() {
|
||||
let size = a.kind.size() as f32;
|
||||
flame_geometry(atlas, c, size, size, self.yaw, light, out);
|
||||
}
|
||||
// `ThrownItemRenderer` for a fireball: the item `Fireball.getItem`
|
||||
// returns (a fire charge until `setItem` gave it another), its model
|
||||
// at the renderer's scale for the kind. `packed` is already 15, the
|
||||
// renderer's full bright.
|
||||
if let Some(stack) = a.item.as_ref() {
|
||||
let owner = super::super::item_draw::owner_at(at, 0.0, a.kind.id());
|
||||
let state = self.resolve_item(stack, DisplayContext::Ground, Some(&owner), 0);
|
||||
let pose = glam::Mat4::from_translation(c)
|
||||
* glam::Mat4::from_quat(self.camera_orientation())
|
||||
* glam::Mat4::from_scale(glam::Vec3::splat(a.sprite_scale()));
|
||||
self.submit_item_world(&state, pose, packed, out);
|
||||
continue;
|
||||
}
|
||||
let Some(name) = a.item_sprite() else {
|
||||
continue;
|
||||
};
|
||||
let Some(r) = atlas.sprites.get(name).copied() else {
|
||||
continue;
|
||||
};
|
||||
let lift = is_item_billboard(&a.kind);
|
||||
let quad = thrown_item_quad(c, right, up, a.sprite_scale(), lift);
|
||||
for (v, uv) in quad
|
||||
.iter()
|
||||
.zip([[r.u0, r.v1], [r.u1, r.v1], [r.u1, r.v0], [r.u0, r.v0]])
|
||||
{
|
||||
out.push(Vertex {
|
||||
normal: [0; 4],
|
||||
pos: v.to_array(),
|
||||
uv,
|
||||
layer: 0,
|
||||
color: 0xFFFF_FFFF,
|
||||
light: packed,
|
||||
|
||||
glint_uv: [0.0, 0.0],
|
||||
});
|
||||
}
|
||||
}
|
||||
if let Some((start, rgb)) = glowing {
|
||||
self.mark_outline(&out[start..], rgb);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
//! Spawn eggs: a baby from an egg used on its own kind, an egg used on a block or a fluid, and the
|
||||
//! egg's entity data on the mob it spawned.
|
||||
use super::*;
|
||||
|
||||
impl Game<'_> {
|
||||
/// `Mob.checkAndHandleImportantInteractions` with a spawn egg of mob
|
||||
/// `index`'s own kind (`SpawnEggItem.spawnOffspringFromSpawnEgg`): a baby
|
||||
/// at the mob's feet, taking the parent's variant as `getBreedOffspring`
|
||||
/// does, and one egg used. False (PASS) when the mob has no baby form.
|
||||
pub(super) fn spawn_egg_offspring(&mut self, index: usize) -> bool {
|
||||
let Some(stack) = self.held().cloned() else {
|
||||
return false;
|
||||
};
|
||||
let parent = &self.mobs[index];
|
||||
if !parent.alive()
|
||||
|| !horizon_client::mobs::finalize::spawn_egg_is_for(&stack.item, &parent.kind.id)
|
||||
|| !horizon_client::mobs::species::can_be_a_baby(parent.species())
|
||||
// WanderingTrader.getBreedOffspring returns null: no offspring, the
|
||||
// click carries on to trading.
|
||||
|| parent.species() == Species::WanderingTrader
|
||||
{
|
||||
return false;
|
||||
}
|
||||
let (kind, at) = (
|
||||
parent.kind.id.clone(),
|
||||
[parent.position.x, parent.position.y, parent.position.z],
|
||||
);
|
||||
let (species, variant, color) =
|
||||
(parent.species(), parent.extra.variant, parent.extra.color);
|
||||
let target = self.mob_entity(&self.mobs[index]);
|
||||
// SpawnEggItem.spawnOffspringFromSpawnEgg: `type.create(level, SPAWN_ITEM_USE)`
|
||||
// creates the baby without finalizeSpawn, and `snapTo(pos, 0, 0)` faces
|
||||
// it south without a level-random draw.
|
||||
let Some(id) =
|
||||
self.spawn_mob_internal(&kind, at, false, false, false, false, SpawnYaw::Fixed(0.0))
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
if let Some(child) = self.mobs.iter_mut().find(|m| m.id == id) {
|
||||
horizon_client::mobs::finalize::spawn_egg_offspring(child, species, variant, color);
|
||||
}
|
||||
// `Mob.checkAndHandleImportantInteractions` then calls
|
||||
// `onOffspringSpawnedFromEgg`: `Fox`'s makes the baby trust the player
|
||||
// who used the egg (a fox egg used on a block trusts nobody).
|
||||
if species == Species::Fox {
|
||||
let spawner = self.acting_player_uuid();
|
||||
if let Some(child) = self.mobs.iter_mut().find(|m| m.id == id) {
|
||||
horizon_client::mobs::fox::add_trusted_entity(child, spawner);
|
||||
}
|
||||
}
|
||||
self.trigger_item_entity("minecraft:player_interacted_with_entity", &stack, &target);
|
||||
self.consume_held();
|
||||
true
|
||||
}
|
||||
/// `SpawnEggItem.use`: the SOURCE_ONLY ray must hit a liquid block.
|
||||
pub(in crate::game) fn spawn_egg_fluid_hit(&self) -> Option<super::super::Hit> {
|
||||
let hit = self.bucket_hit()?;
|
||||
matches!(
|
||||
&*self.block_name(hit.pos),
|
||||
"minecraft:water" | "minecraft:lava"
|
||||
)
|
||||
.then_some(hit)
|
||||
}
|
||||
/// Shared peaceful-mode eligibility for spawn eggs.
|
||||
pub(in crate::game) fn spawn_egg_allowed(&self, item: &str) -> bool {
|
||||
let kind = item.trim_end_matches("_spawn_egg");
|
||||
self.ui.difficulty != 0
|
||||
|| self
|
||||
.res
|
||||
.mob_types()
|
||||
.get(kind)
|
||||
.is_none_or(|t| horizon_client::mobs::spawn_rules::allowed_in_peaceful(t.species))
|
||||
}
|
||||
/// `SpawnEggItem.useOn` for a block, or `use` for a fluid source.
|
||||
pub(super) fn use_spawn_egg(&mut self, stack: &ItemStack) -> Result<bool> {
|
||||
let fluid = matches!(self.guest_use, Some(super::super::GuestUse::Item(_)));
|
||||
let Some(hit) = (if fluid {
|
||||
self.spawn_egg_fluid_hit()
|
||||
} else {
|
||||
self.pick()
|
||||
}) else {
|
||||
return Ok(false);
|
||||
};
|
||||
let kind = stack.item.trim_end_matches("_spawn_egg").to_owned();
|
||||
if !self.spawn_egg_allowed(&stack.item) {
|
||||
self.use_failed = true;
|
||||
return Ok(true);
|
||||
}
|
||||
if fluid
|
||||
&& !self.may_build()
|
||||
&& !self.can_place_on_in_adventure([
|
||||
hit.pos[0] - hit.face[0],
|
||||
hit.pos[1] - hit.face[1],
|
||||
hit.pos[2] - hit.face[2],
|
||||
])
|
||||
{
|
||||
self.use_failed = true;
|
||||
return Ok(true);
|
||||
}
|
||||
// SpawnEggItem.useOn on a spawner sets its mob (and an op's egg data).
|
||||
let (spawner_id, data) = horizon_client::block_entity_item::spawn_egg_on_spawner(
|
||||
stack,
|
||||
self.acting_player_is_op(),
|
||||
);
|
||||
if let Some(done) = self.use_spawn_egg_on_spawner(hit.pos, &spawner_id, data.as_ref()) {
|
||||
if done {
|
||||
self.swing_arm();
|
||||
self.award_stat("used", &stack.item, 1);
|
||||
self.consume_held();
|
||||
}
|
||||
return Ok(true);
|
||||
}
|
||||
// useOn: a clicked block without a collision shape (vines, grass)
|
||||
// takes the mob itself, any other the one on the clicked face.
|
||||
let p = if fluid || self.collision_empty(hit.pos) {
|
||||
hit.pos
|
||||
} else {
|
||||
[
|
||||
hit.pos[0] + hit.face[0],
|
||||
hit.pos[1] + hit.face[1],
|
||||
hit.pos[2] + hit.face[2],
|
||||
]
|
||||
};
|
||||
let at = [
|
||||
f64::from(p[0]) + 0.5,
|
||||
f64::from(p[1]),
|
||||
f64::from(p[2]) + 0.5,
|
||||
];
|
||||
// SpawnEggItem.spawnMob: `EntityType.spawn`.
|
||||
let Some(id) = self.spawn_mob_spawned(&kind, at, SpawnYaw::Created) else {
|
||||
return Ok(false);
|
||||
};
|
||||
// SpawnEggItem.spawnMob: `EntityType.create` finalizes the mob, then
|
||||
// `PostSpawnProcessor` puts the egg's components on it.
|
||||
if let Some(m) = self.mobs.iter_mut().find(|m| m.id == id) {
|
||||
horizon_client::entity_nbt::apply_item_components(m, stack);
|
||||
}
|
||||
self.apply_spawn_egg_entity_data(id, stack);
|
||||
self.post_block_game_event("minecraft:entity_place", p, Some(Who::Player), None);
|
||||
self.award_stat("used", &stack.item, 1);
|
||||
self.swing_arm();
|
||||
if !self.creative
|
||||
&& let Some(s) = self.held_mut().as_mut()
|
||||
{
|
||||
s.count -= 1;
|
||||
if s.count == 0 {
|
||||
*self.held_mut() = None;
|
||||
}
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
/// `EntityType.updateCustomEntityTag`: the egg's `minecraft:entity_data` on
|
||||
/// the mob it spawned, once the guards let it through, as
|
||||
/// `TypedEntityData.loadInto`: the mob's own compound, the data merged over
|
||||
/// it, loaded back into the same mob (`/data merge entity`'s loader).
|
||||
pub(in crate::game) fn apply_spawn_egg_entity_data(&mut self, id: u32, stack: &ItemStack) {
|
||||
let Some(i) = self.mobs.iter().position(|m| m.id == id) else {
|
||||
return;
|
||||
};
|
||||
let kind = self.mobs[i].kind.id.clone();
|
||||
let op = self.acting_player_is_op();
|
||||
let Some(data) = horizon_client::block_entity_item::spawn_egg_entity_data(stack, &kind, op)
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let own = self.mob_tree_nbt(i);
|
||||
let tag = horizon_client::data_cmd::merged(&own, data);
|
||||
// Nothing to report: the mob's own compound always loads back.
|
||||
let _ = self.apply_entity_nbt(super::super::commands::Target::Mob(id), tag);
|
||||
}
|
||||
}
|
||||
@@ -2671,7 +2671,7 @@ mod tests {
|
||||
#[test]
|
||||
fn a_temporarily_banned_player_is_refused_with_the_end_until_it_has_passed() {
|
||||
use super::{BanEntry, BanList, Lists, NameAndId};
|
||||
use horizon_chat_filter::now_millis;
|
||||
use horizon_server_files::duration::now_millis;
|
||||
let dir = std::env::temp_dir().join(format!("horizon-can-login-{}", std::process::id()));
|
||||
fs::create_dir_all(&dir).unwrap();
|
||||
let mut lists = Lists {
|
||||
|
||||
@@ -273,6 +273,12 @@ impl Game<'_> {
|
||||
}
|
||||
self.draw_pvp_hud(batch, &mut corners);
|
||||
self.native_hud(batch, &mut corners);
|
||||
// The layers crates registered (`horizon_hooks::hud`): none, no canvas.
|
||||
let layers = horizon_hooks::hud::engine::all();
|
||||
if !layers.is_empty() {
|
||||
let shown = Corner::ALL.map(|corner| self.native_corner_shown(corner));
|
||||
extras_hud::draw_layers(layers, batch, &self.gui.atlas, &mut corners, shown);
|
||||
}
|
||||
}
|
||||
/// The height the status effect icons take at the top right
|
||||
/// (`Hud.extractEffects`: beneficial at y 1, harmful at y 26).
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
//! Play with friends, host side: "Start Sharing" on the Play with Friends
|
||||
//! screen (`friends_screen`, the pause menu's Friends button) opens the world to LAN if
|
||||
//! screen (`screen`, the pause menu's Friends button) opens the world to LAN if
|
||||
//! it isn't (with Open to LAN's defaults) and shares that port behind an
|
||||
//! invite code (horizon-friends). The code holds this computer's addresses
|
||||
//! and the key, so friends join with no server of ours; a relay the player
|
||||
//! configured adds a short code and a fallback. The integrated server is the
|
||||
//! LAN one, unchanged: tunnelled guests are LAN guests that connect from the
|
||||
//! loopback address.
|
||||
//! loopback address. The joining side is horizon-friends' (`game`), registered by
|
||||
//! horizon-official.
|
||||
use super::{Game, lan::LanSettings};
|
||||
use horizon_client::{
|
||||
Result,
|
||||
friends_screen::{self, Action, FriendsView},
|
||||
hud::HudBatch,
|
||||
lang,
|
||||
text::Text,
|
||||
};
|
||||
use horizon_client::{Result, hud::HudBatch, lang, text::Text};
|
||||
use horizon_friends::{Host, HostConfig, HostEvent, PathMode, host::DEFAULT_PORT, reach, stun};
|
||||
use horizon_protocol::{GAME_VERSION, PROTOCOL_VERSION};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
mod screen;
|
||||
|
||||
use screen::{self as friends_screen, Action, FriendsView};
|
||||
pub use screen::{TITLE, title};
|
||||
|
||||
pub(super) struct Sharing {
|
||||
host: Host,
|
||||
/// Sharing opened the world to LAN, so stopping closes it again.
|
||||
@@ -240,6 +240,11 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the pause menu offers Play with Friends (a singleplayer world).
|
||||
pub(super) fn friends_shown(&self) -> bool {
|
||||
self.friends_view().shown
|
||||
}
|
||||
|
||||
/// What Play with Friends shows.
|
||||
pub(super) fn friends_view(&self) -> FriendsView {
|
||||
let guests = match &self.lan {
|
||||
|
||||
+5
-5
@@ -1,9 +1,9 @@
|
||||
//! Play with Friends: Horizon's own sharing of the open world behind an
|
||||
//! invite code (`game/friends.rs`). The pause menu's Friends button, the icon
|
||||
//! invite code (`game/friends.rs`), with the rest of Play with Friends' game glue. The pause menu's Friends button, the icon
|
||||
//! vanilla's PauseScreen already has, opens it. It is drawn the way a mod adds
|
||||
//! a screen to vanilla: the menu background, a centred title and text,
|
||||
//! vanilla's buttons and Done in the footer.
|
||||
use crate::{
|
||||
use horizon_client::{
|
||||
hud::{HudAtlas, HudBatch, rgba},
|
||||
multiplayer::wrap,
|
||||
text::Text,
|
||||
@@ -14,7 +14,7 @@ use crate::{
|
||||
pub const TITLE: &str = "Play with Friends";
|
||||
/// A string of this screen: the language table's, or the English text.
|
||||
fn tr(key: &str, english: &str) -> String {
|
||||
crate::lang::get_or(key, english)
|
||||
horizon_client::lang::get_or(key, english)
|
||||
}
|
||||
/// The title in the current language.
|
||||
pub fn title() -> String {
|
||||
@@ -245,8 +245,8 @@ pub fn draw(
|
||||
for b in buttons {
|
||||
let hover = b.active && ui::hovered(cursor, b.x, b.y, b.w, 20.0);
|
||||
if matches!(b.action, Action::Done) {
|
||||
crate::widgets::declare_action(
|
||||
crate::widgets::Action::Done,
|
||||
horizon_client::widgets::declare_action(
|
||||
horizon_client::widgets::Action::Done,
|
||||
[b.x, b.y, b.w, 20.0],
|
||||
b.active,
|
||||
);
|
||||
@@ -1,11 +1,17 @@
|
||||
//! Play with Friends left out of the build (the `friends` feature off, as in the server build):
|
||||
//! no world is shared, the pause menu has no Friends button (`FriendsView::shown` is false, as on
|
||||
//! no world is shared, the pause menu has no Friends button (`friends_shown` is false, as on
|
||||
//! another server) and `horizon-ctl friends` says it isn't there. game/friends.rs is the real one.
|
||||
use super::{Game, Screen};
|
||||
use horizon_client::{Result, friends_screen::FriendsView, hud::HudBatch};
|
||||
use horizon_client::{Result, hud::HudBatch};
|
||||
|
||||
pub const HELP: &[&str] = &[];
|
||||
|
||||
/// The pause menu's Friends icon is vanilla's, drawn inactive: its name for the widget tree.
|
||||
pub const TITLE: &str = "Play with Friends";
|
||||
pub fn title() -> String {
|
||||
horizon_client::lang::get_or("horizon.friends.title", TITLE)
|
||||
}
|
||||
|
||||
impl Game<'_> {
|
||||
pub(super) fn friends_lan_closed(&mut self) {}
|
||||
|
||||
@@ -18,8 +24,8 @@ impl Game<'_> {
|
||||
|
||||
pub(super) fn friends_screen_click(&mut self) {}
|
||||
|
||||
pub(super) fn friends_view(&self) -> FriendsView {
|
||||
FriendsView::default()
|
||||
pub(super) fn friends_shown(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// `horizon-ctl friends ...`.
|
||||
|
||||
@@ -1820,7 +1820,7 @@ impl Game<'_> {
|
||||
// from carries those, as `save_guest` gives them on leaving. The map
|
||||
// stays their one owner.
|
||||
let live = self.guest_attachments.get(&uuid).cloned();
|
||||
let g = &mut host.hub.guests[i];
|
||||
let g = playerdata::with_warden(&mut host.hub.guests[i], self.warden_tracker_tag());
|
||||
let held = live.map(|a| std::mem::replace(&mut g.attachments, a));
|
||||
playerdata::store(g, &mut self.level, vehicle);
|
||||
if let Some(held) = held {
|
||||
|
||||
@@ -899,4 +899,32 @@ impl Game<'_> {
|
||||
f.input_clicked(world_recipes!(self), before);
|
||||
}
|
||||
}
|
||||
|
||||
/// `AbstractContainerScreen` keeps `isQuickCrafting` and the drag it
|
||||
/// belongs to on the screen. Horizon keeps both on `Game` (`inv_clicks`,
|
||||
/// `inv_drag`, `last_inv_click`), so they are dropped when the pointer
|
||||
/// changes hands: a screen that opens or closes starts with its own
|
||||
/// state, and a press whose release never reached the screen -- the
|
||||
/// pointer left the window, or the screen closed first -- would
|
||||
/// otherwise leave every later press on every later screen swallowed.
|
||||
pub(super) fn reset_inventory_clicks(&mut self) {
|
||||
self.inv_clicks.cancel_drag();
|
||||
self.inv_drag = None;
|
||||
self.last_inv_click = None;
|
||||
}
|
||||
|
||||
/// A sound a menu click made the player play (a bundle's insert or
|
||||
/// remove): `Entity.playSound` at the player.
|
||||
pub(super) fn player_menu_sound(&mut self, sound: &str) {
|
||||
let Some(p) = self.player.as_ref() else {
|
||||
return;
|
||||
};
|
||||
let at = [p.position.x, p.position.y, p.position.z];
|
||||
let (volume, pitch) = if sound == horizon_client::bundle::INSERT_FAIL {
|
||||
(1.0, 1.0)
|
||||
} else {
|
||||
(0.8, 0.8 + self.world.random.next_float() * 0.4)
|
||||
};
|
||||
self.audio.play(sound, Some(at), volume, pitch);
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,10 @@ use horizon_core::{
|
||||
};
|
||||
use horizon_world::registry::BlockStateId;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
mod index;
|
||||
mod nbt;
|
||||
pub(super) use index::CartIndex;
|
||||
use index::cart_at;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub(super) enum CartKind {
|
||||
@@ -187,102 +191,6 @@ pub(super) const TNT_PRIME_EVENT: i8 = 70;
|
||||
mod display;
|
||||
use display::{cart_render_bounds, display_offset, shown_unless_air};
|
||||
pub(super) use display::{default_display_offset, forget_unreadable_display};
|
||||
/// The carts' and mobs' boxes for one tick's cart pushes (`push_from_cart`,
|
||||
/// `pickup_from_cart`), so a cart reads its neighbours instead of every cart and
|
||||
/// every mob of the level. Both answer in slot order, as the scans did.
|
||||
///
|
||||
/// The cart grid follows each ticked cart ([`CartIndex::moved`]); it is
|
||||
/// rebuilt once the list's length changed (a cart gone or added), and dropped
|
||||
/// after a command, which can move or replace any cart
|
||||
/// ([`CartIndex::forget_carts`]). The mob grid holds until the mob
|
||||
/// list's next write ([`ListStamp`]). A native mod's hook can move either kind
|
||||
/// unseen, so with mods loaded both scans stay.
|
||||
#[derive(Default)]
|
||||
pub(super) struct CartIndex {
|
||||
carts: Option<(SeenGrid, Vec<u32>)>,
|
||||
/// The grid, and the carts a mob rides.
|
||||
mobs: Option<(ListStamp, SeenGrid, FxHashSet<u32>)>,
|
||||
near: Vec<u32>,
|
||||
}
|
||||
impl CartIndex {
|
||||
fn usable() -> bool {
|
||||
horizon_client::mod_api::mods().is_empty()
|
||||
}
|
||||
/// The cart in slot `i` has these bounds now.
|
||||
fn moved(&mut self, i: usize, bounds: Aabb) {
|
||||
if let Some((grid, ids)) = &mut self.carts
|
||||
&& i < ids.len()
|
||||
{
|
||||
grid.update(i, bounds);
|
||||
}
|
||||
}
|
||||
fn forget_carts(&mut self) {
|
||||
self.carts = None;
|
||||
}
|
||||
/// The slots of the carts whose box may meet `area`, for the cart in slot
|
||||
/// `i` (just moved); false: scan them all.
|
||||
fn carts_near(&mut self, carts: &[Minecart], i: usize, area: Aabb, out: &mut Vec<u32>) -> bool {
|
||||
if !Self::usable() {
|
||||
return false;
|
||||
}
|
||||
let fresh = |ids: &[u32]| ids.len() == carts.len() && ids[i] == carts[i].id;
|
||||
if !self.carts.as_ref().is_some_and(|(_, ids)| fresh(ids)) {
|
||||
self.carts = Some((
|
||||
SeenGrid::new(carts.iter().map(Minecart::bounds)),
|
||||
carts.iter().map(|c| c.id).collect(),
|
||||
));
|
||||
}
|
||||
let Some((grid, ids)) = &mut self.carts else {
|
||||
return false;
|
||||
};
|
||||
grid.update(i, carts[i].bounds());
|
||||
if !grid.candidates(area, out) {
|
||||
return false;
|
||||
}
|
||||
if out.iter().any(|&j| ids[j as usize] != carts[j as usize].id) {
|
||||
// A cart was replaced in place: start over next time.
|
||||
self.carts = None;
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
/// The slots of the mobs whose box may meet `area` into `out`, and
|
||||
/// whether a mob rides cart `cart`; None: scan them all.
|
||||
fn mobs_near(
|
||||
&mut self,
|
||||
mobs: &JoinList<Mob>,
|
||||
cart: u32,
|
||||
area: Aabb,
|
||||
out: &mut Vec<u32>,
|
||||
) -> Option<bool> {
|
||||
if !Self::usable() {
|
||||
return None;
|
||||
}
|
||||
if !self
|
||||
.mobs
|
||||
.as_ref()
|
||||
.is_some_and(|(stamp, ..)| *stamp == mobs.stamp())
|
||||
{
|
||||
self.mobs = Some((
|
||||
mobs.stamp(),
|
||||
SeenGrid::new(mobs.iter().map(Mob::bounds)),
|
||||
mobs.iter().filter_map(|m| m.extra.mount).collect(),
|
||||
));
|
||||
}
|
||||
let (_, grid, mounted) = self.mobs.as_ref()?;
|
||||
grid.candidates(area, out).then(|| mounted.contains(&cart))
|
||||
}
|
||||
}
|
||||
/// Where the cart `id` is in `carts`: at `hint` (where it was a moment ago,
|
||||
/// the usual case while carts tick in order) or found by a scan. A world
|
||||
/// with hundreds of mineshaft chest carts asked this of every cart several
|
||||
/// times a tick.
|
||||
fn cart_at(carts: &[Minecart], id: u32, hint: usize) -> Option<usize> {
|
||||
if carts.get(hint).is_some_and(|c| c.id == id) {
|
||||
return Some(hint);
|
||||
}
|
||||
carts.iter().position(|c| c.id == id)
|
||||
}
|
||||
/// `AbstractMinecart.push(entity)` for a non-cart `entity` at `feet`:
|
||||
/// the push the cart loses (the entity gets a quarter of it back).
|
||||
pub(super) fn cart_push_from(feet: Vec3, cart: Vec3) -> Option<(f64, f64)> {
|
||||
@@ -2984,177 +2892,6 @@ impl Game<'_> {
|
||||
.render_state(&scene, state.0, m, light, out);
|
||||
}
|
||||
}
|
||||
/// Saving and loading.
|
||||
impl Minecart {
|
||||
pub fn to_nbt(
|
||||
&self,
|
||||
uuid: [i32; 4],
|
||||
raw: Option<&horizon_world::nbt::Tag>,
|
||||
) -> horizon_world::nbt::Tag {
|
||||
use horizon_world::nbt::{JavaString, Tag};
|
||||
let mut tag = horizon_client::entity_nbt::vehicle_to_nbt(
|
||||
self.kind.id(),
|
||||
self.position,
|
||||
self.velocity,
|
||||
[self.yaw, self.pitch],
|
||||
(self.on_ground, self.fall_distance),
|
||||
uuid,
|
||||
raw,
|
||||
);
|
||||
horizon_client::entity_nbt::write_saved_flags(&mut tag, self.on_ground, self.no_gravity);
|
||||
if let Tag::Compound(map) = &mut tag {
|
||||
// ContainerEntity.addChestVehicleSaveData belongs only to the
|
||||
// container subclasses, never to a plain or specialized cart.
|
||||
if self.kind.slots() == 0 {
|
||||
map.remove(&JavaString::from("Items"));
|
||||
}
|
||||
if self.kind == CartKind::CommandBlock {
|
||||
for key in ["LastOutput", "LastExecution"] {
|
||||
map.remove(&JavaString::from(key));
|
||||
}
|
||||
}
|
||||
map.remove(&JavaString::from("DisplayOffset"));
|
||||
map.remove(&JavaString::from("DisplayState"));
|
||||
let mut put = |k: &str, v: Tag| {
|
||||
map.insert(JavaString::from(k), v);
|
||||
};
|
||||
put("FlippedRotation", Tag::Byte(i8::from(self.flipped)));
|
||||
// AbstractMinecart saves firstTick as HasTicked. A cart never burns:
|
||||
// Entity.move sets -getFireImmuneTicks(), 0 for all but players.
|
||||
put("HasTicked", Tag::Byte(i8::from(self.first_tick)));
|
||||
put("Fire", Tag::Short(0));
|
||||
// addAdditionalSaveData: the offset only off its default, the state only when set.
|
||||
if let Some(offset) = self.custom_offset
|
||||
&& f64::from(offset) != default_display_offset(self.kind)
|
||||
{
|
||||
put("DisplayOffset", Tag::Int(offset));
|
||||
}
|
||||
if let Some(state) = &self.custom_display {
|
||||
put("DisplayState", state.clone());
|
||||
}
|
||||
match self.kind {
|
||||
CartKind::Chest | CartKind::Hopper => {
|
||||
put("Items", horizon_client::save::encode_items(&self.items));
|
||||
if self.kind == CartKind::Hopper {
|
||||
put("Enabled", Tag::Byte(i8::from(self.enabled)));
|
||||
}
|
||||
}
|
||||
CartKind::Furnace => {
|
||||
put("PushX", Tag::Double(self.push.x.to_bits()));
|
||||
put("PushZ", Tag::Double(self.push.z.to_bits()));
|
||||
put("Fuel", Tag::Short(self.fuel as i16));
|
||||
}
|
||||
CartKind::Tnt => put("fuse", Tag::Int(self.fuse)),
|
||||
// BaseCommandBlock.save (its CustomName shares the entity's key).
|
||||
CartKind::CommandBlock => {
|
||||
for (key, value) in self.command.save() {
|
||||
if !matches!(key, "CustomName" | "powered" | "conditionMet" | "auto") {
|
||||
put(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
// BaseSpawner.save: the delays, ranges, SpawnData and SpawnPotentials.
|
||||
CartKind::Spawner => {
|
||||
if let Some(spawner) = &self.spawner {
|
||||
for (key, value) in
|
||||
horizon_client::mobs::spawner::SpawnerExt::save(&**spawner)
|
||||
{
|
||||
put(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
CartKind::Rideable => {}
|
||||
}
|
||||
// A loaded loot table goes back while the items are still its roll;
|
||||
// otherwise the items replace it, as vanilla's unpackLootTable does.
|
||||
if self.loot.as_ref().is_some_and(|l| l.restore(map)) {
|
||||
if let Some(table) = horizon_client::save::text(map.get(&"LootTable".into()))
|
||||
.and_then(|id| horizon_client_core::identifier::normalize_id(&id))
|
||||
{
|
||||
map.insert("LootTable".into(), Tag::String(table.as_str().into()));
|
||||
}
|
||||
} else {
|
||||
map.remove(&JavaString::from("LootTable"));
|
||||
map.remove(&JavaString::from("LootTableSeed"));
|
||||
}
|
||||
}
|
||||
tag
|
||||
}
|
||||
pub fn from_nbt(tag: &horizon_world::nbt::Tag, id: u32, kind: CartKind) -> Option<Self> {
|
||||
let (position, _, _) = horizon_client::entity_nbt::boat_from_nbt(tag)?;
|
||||
let mut c = Self::new(id, kind, position);
|
||||
c.load(tag)?;
|
||||
Some(c)
|
||||
}
|
||||
/// `Entity.load` on this cart: the saved fields are read over the live
|
||||
/// ones, the rest (riders, lerp, hurt state) stays; its kind never changes.
|
||||
/// `None`, with nothing changed, when the compound has no position.
|
||||
pub fn load(&mut self, tag: &horizon_world::nbt::Tag) -> Option<()> {
|
||||
use horizon_world::nbt::Tag;
|
||||
let (position, velocity, yaw) = horizon_client::entity_nbt::boat_from_nbt(tag)?;
|
||||
let kind = self.kind;
|
||||
let c = self;
|
||||
c.position = position;
|
||||
c.previous = position;
|
||||
c.velocity = velocity;
|
||||
c.yaw = yaw;
|
||||
c.previous_yaw = yaw;
|
||||
c.no_gravity = horizon_client::entity_nbt::flag(tag, "NoGravity");
|
||||
c.on_ground = horizon_client::entity_nbt::flag(tag, "OnGround");
|
||||
c.pitch = horizon_client::entity_nbt::rotation(tag)[1];
|
||||
c.previous_pitch = c.pitch;
|
||||
// ValueInput's numeric getters take any NumericTag: getIntOr is its
|
||||
// intValue, getShortOr the low 16 bits of it, getBooleanOr
|
||||
// `byteValue() != 0` (the low 8), getDoubleOr its doubleValue.
|
||||
let int = |k: &str| {
|
||||
tag.get(k)
|
||||
.and_then(horizon_client_core::nbt_path::int_value)
|
||||
};
|
||||
let flag = |k: &str, default: bool| int(k).map_or(default, |v| v as i8 != 0);
|
||||
let double = |k: &str| match tag.get(k) {
|
||||
Some(Tag::Byte(v)) => f64::from(*v),
|
||||
Some(Tag::Short(v)) => f64::from(*v),
|
||||
Some(Tag::Int(v)) => f64::from(*v),
|
||||
Some(Tag::Long(v)) => *v as f64,
|
||||
Some(Tag::Float(v)) => f64::from(f32::from_bits(*v)),
|
||||
Some(Tag::Double(v)) => f64::from_bits(*v),
|
||||
_ => 0.0,
|
||||
};
|
||||
// Entity.load: OnGround and fall_distance, as saved.
|
||||
(c.on_ground, c.fall_distance) = horizon_client::entity_nbt::ground_from_nbt(tag);
|
||||
c.flipped = flag("FlippedRotation", false);
|
||||
c.first_tick = flag("HasTicked", false);
|
||||
if kind.slots() > 0 {
|
||||
c.items = horizon_client::save::decode_items(tag.get("Items"), kind.slots());
|
||||
}
|
||||
c.enabled = flag("Enabled", true);
|
||||
c.fuel = int("Fuel").map_or(0, |v| i32::from(v as i16));
|
||||
// readAdditionalSaveData: the saved push has no y.
|
||||
c.push = Vec3::new(double("PushX"), 0.0, double("PushZ"));
|
||||
c.fuse = int("fuse").unwrap_or(-1);
|
||||
// readAdditionalSaveData: DisplayState, DisplayOffset (default offset when absent).
|
||||
c.custom_display = tag.get("DisplayState").cloned();
|
||||
c.custom_offset = int("DisplayOffset");
|
||||
if kind == CartKind::CommandBlock {
|
||||
c.command = horizon_client::command_block::CommandBlock::load(tag);
|
||||
// MinecartCommandBlock.readAdditionalSaveData sets the synched command.
|
||||
// A changed value invokes onSyncedDataUpdated -> setCommand, resetting successes.
|
||||
if c.synced_command != c.command.command {
|
||||
c.synced_command.clone_from(&c.command.command);
|
||||
c.command.set_command(&c.synced_command);
|
||||
}
|
||||
c.synced_output = c.command.last_output_string();
|
||||
}
|
||||
// MinecartSpawner.readAdditionalSaveData: BaseSpawner.load.
|
||||
if kind == CartKind::Spawner {
|
||||
c.spawner = Some(Box::new(horizon_client::mobs::spawner::SpawnerExt::load(
|
||||
tag,
|
||||
)));
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
}
|
||||
|
||||
/// `VehicleEntity.hurtServer`'s destroy test: a creative player, damage past
|
||||
/// 40, or (`MinecartTNT.shouldSourceDestroy`) fire or an explosion hitting a
|
||||
/// TNT cart.
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
//! The carts' and mobs' boxes for a tick's cart pushes ([`CartIndex`]).
|
||||
use super::*;
|
||||
|
||||
/// The carts' and mobs' boxes for one tick's cart pushes (`push_from_cart`,
|
||||
/// `pickup_from_cart`), so a cart reads its neighbours instead of every cart and
|
||||
/// every mob of the level. Both answer in slot order, as the scans did.
|
||||
///
|
||||
/// The cart grid follows each ticked cart ([`CartIndex::moved`]); it is
|
||||
/// rebuilt once the list's length changed (a cart gone or added), and dropped
|
||||
/// after a command, which can move or replace any cart
|
||||
/// ([`CartIndex::forget_carts`]). The mob grid holds until the mob
|
||||
/// list's next write ([`ListStamp`]). A native mod's hook can move either kind
|
||||
/// unseen, so with mods loaded both scans stay.
|
||||
#[derive(Default)]
|
||||
pub(in crate::game) struct CartIndex {
|
||||
pub(super) carts: Option<(SeenGrid, Vec<u32>)>,
|
||||
/// The grid, and the carts a mob rides.
|
||||
pub(super) mobs: Option<(ListStamp, SeenGrid, FxHashSet<u32>)>,
|
||||
pub(super) near: Vec<u32>,
|
||||
}
|
||||
impl CartIndex {
|
||||
pub(super) fn usable() -> bool {
|
||||
horizon_client::mod_api::mods().is_empty()
|
||||
}
|
||||
/// The cart in slot `i` has these bounds now.
|
||||
pub(super) fn moved(&mut self, i: usize, bounds: Aabb) {
|
||||
if let Some((grid, ids)) = &mut self.carts
|
||||
&& i < ids.len()
|
||||
{
|
||||
grid.update(i, bounds);
|
||||
}
|
||||
}
|
||||
pub(super) fn forget_carts(&mut self) {
|
||||
self.carts = None;
|
||||
}
|
||||
/// The slots of the carts whose box may meet `area`, for the cart in slot
|
||||
/// `i` (just moved); false: scan them all.
|
||||
pub(super) fn carts_near(
|
||||
&mut self,
|
||||
carts: &[Minecart],
|
||||
i: usize,
|
||||
area: Aabb,
|
||||
out: &mut Vec<u32>,
|
||||
) -> bool {
|
||||
if !Self::usable() {
|
||||
return false;
|
||||
}
|
||||
let fresh = |ids: &[u32]| ids.len() == carts.len() && ids[i] == carts[i].id;
|
||||
if !self.carts.as_ref().is_some_and(|(_, ids)| fresh(ids)) {
|
||||
self.carts = Some((
|
||||
SeenGrid::new(carts.iter().map(Minecart::bounds)),
|
||||
carts.iter().map(|c| c.id).collect(),
|
||||
));
|
||||
}
|
||||
let Some((grid, ids)) = &mut self.carts else {
|
||||
return false;
|
||||
};
|
||||
grid.update(i, carts[i].bounds());
|
||||
if !grid.candidates(area, out) {
|
||||
return false;
|
||||
}
|
||||
if out.iter().any(|&j| ids[j as usize] != carts[j as usize].id) {
|
||||
// A cart was replaced in place: start over next time.
|
||||
self.carts = None;
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
/// The slots of the mobs whose box may meet `area` into `out`, and
|
||||
/// whether a mob rides cart `cart`; None: scan them all.
|
||||
pub(super) fn mobs_near(
|
||||
&mut self,
|
||||
mobs: &JoinList<Mob>,
|
||||
cart: u32,
|
||||
area: Aabb,
|
||||
out: &mut Vec<u32>,
|
||||
) -> Option<bool> {
|
||||
if !Self::usable() {
|
||||
return None;
|
||||
}
|
||||
if !self
|
||||
.mobs
|
||||
.as_ref()
|
||||
.is_some_and(|(stamp, ..)| *stamp == mobs.stamp())
|
||||
{
|
||||
self.mobs = Some((
|
||||
mobs.stamp(),
|
||||
SeenGrid::new(mobs.iter().map(Mob::bounds)),
|
||||
mobs.iter().filter_map(|m| m.extra.mount).collect(),
|
||||
));
|
||||
}
|
||||
let (_, grid, mounted) = self.mobs.as_ref()?;
|
||||
grid.candidates(area, out).then(|| mounted.contains(&cart))
|
||||
}
|
||||
}
|
||||
/// Where the cart `id` is in `carts`: at `hint` (where it was a moment ago,
|
||||
/// the usual case while carts tick in order) or found by a scan. A world
|
||||
/// with hundreds of mineshaft chest carts asked this of every cart several
|
||||
/// times a tick.
|
||||
pub(super) fn cart_at(carts: &[Minecart], id: u32, hint: usize) -> Option<usize> {
|
||||
if carts.get(hint).is_some_and(|c| c.id == id) {
|
||||
return Some(hint);
|
||||
}
|
||||
carts.iter().position(|c| c.id == id)
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
//! Saving and loading a minecart.
|
||||
use super::*;
|
||||
|
||||
/// Saving and loading.
|
||||
impl Minecart {
|
||||
pub fn to_nbt(
|
||||
&self,
|
||||
uuid: [i32; 4],
|
||||
raw: Option<&horizon_world::nbt::Tag>,
|
||||
) -> horizon_world::nbt::Tag {
|
||||
use horizon_world::nbt::{JavaString, Tag};
|
||||
let mut tag = horizon_client::entity_nbt::vehicle_to_nbt(
|
||||
self.kind.id(),
|
||||
self.position,
|
||||
self.velocity,
|
||||
[self.yaw, self.pitch],
|
||||
(self.on_ground, self.fall_distance),
|
||||
uuid,
|
||||
raw,
|
||||
);
|
||||
horizon_client::entity_nbt::write_saved_flags(&mut tag, self.on_ground, self.no_gravity);
|
||||
if let Tag::Compound(map) = &mut tag {
|
||||
// ContainerEntity.addChestVehicleSaveData belongs only to the
|
||||
// container subclasses, never to a plain or specialized cart.
|
||||
if self.kind.slots() == 0 {
|
||||
map.remove(&JavaString::from("Items"));
|
||||
}
|
||||
if self.kind == CartKind::CommandBlock {
|
||||
for key in ["LastOutput", "LastExecution"] {
|
||||
map.remove(&JavaString::from(key));
|
||||
}
|
||||
}
|
||||
map.remove(&JavaString::from("DisplayOffset"));
|
||||
map.remove(&JavaString::from("DisplayState"));
|
||||
let mut put = |k: &str, v: Tag| {
|
||||
map.insert(JavaString::from(k), v);
|
||||
};
|
||||
put("FlippedRotation", Tag::Byte(i8::from(self.flipped)));
|
||||
// AbstractMinecart saves firstTick as HasTicked. A cart never burns:
|
||||
// Entity.move sets -getFireImmuneTicks(), 0 for all but players.
|
||||
put("HasTicked", Tag::Byte(i8::from(self.first_tick)));
|
||||
put("Fire", Tag::Short(0));
|
||||
// addAdditionalSaveData: the offset only off its default, the state only when set.
|
||||
if let Some(offset) = self.custom_offset
|
||||
&& f64::from(offset) != default_display_offset(self.kind)
|
||||
{
|
||||
put("DisplayOffset", Tag::Int(offset));
|
||||
}
|
||||
if let Some(state) = &self.custom_display {
|
||||
put("DisplayState", state.clone());
|
||||
}
|
||||
match self.kind {
|
||||
CartKind::Chest | CartKind::Hopper => {
|
||||
put("Items", horizon_client::save::encode_items(&self.items));
|
||||
if self.kind == CartKind::Hopper {
|
||||
put("Enabled", Tag::Byte(i8::from(self.enabled)));
|
||||
}
|
||||
}
|
||||
CartKind::Furnace => {
|
||||
put("PushX", Tag::Double(self.push.x.to_bits()));
|
||||
put("PushZ", Tag::Double(self.push.z.to_bits()));
|
||||
put("Fuel", Tag::Short(self.fuel as i16));
|
||||
}
|
||||
CartKind::Tnt => put("fuse", Tag::Int(self.fuse)),
|
||||
// BaseCommandBlock.save (its CustomName shares the entity's key).
|
||||
CartKind::CommandBlock => {
|
||||
for (key, value) in self.command.save() {
|
||||
if !matches!(key, "CustomName" | "powered" | "conditionMet" | "auto") {
|
||||
put(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
// BaseSpawner.save: the delays, ranges, SpawnData and SpawnPotentials.
|
||||
CartKind::Spawner => {
|
||||
if let Some(spawner) = &self.spawner {
|
||||
for (key, value) in
|
||||
horizon_client::mobs::spawner::SpawnerExt::save(&**spawner)
|
||||
{
|
||||
put(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
CartKind::Rideable => {}
|
||||
}
|
||||
// A loaded loot table goes back while the items are still its roll;
|
||||
// otherwise the items replace it, as vanilla's unpackLootTable does.
|
||||
if self.loot.as_ref().is_some_and(|l| l.restore(map)) {
|
||||
if let Some(table) = horizon_client::save::text(map.get(&"LootTable".into()))
|
||||
.and_then(|id| horizon_client_core::identifier::normalize_id(&id))
|
||||
{
|
||||
map.insert("LootTable".into(), Tag::String(table.as_str().into()));
|
||||
}
|
||||
} else {
|
||||
map.remove(&JavaString::from("LootTable"));
|
||||
map.remove(&JavaString::from("LootTableSeed"));
|
||||
}
|
||||
}
|
||||
tag
|
||||
}
|
||||
pub fn from_nbt(tag: &horizon_world::nbt::Tag, id: u32, kind: CartKind) -> Option<Self> {
|
||||
let (position, _, _) = horizon_client::entity_nbt::boat_from_nbt(tag)?;
|
||||
let mut c = Self::new(id, kind, position);
|
||||
c.load(tag)?;
|
||||
Some(c)
|
||||
}
|
||||
/// `Entity.load` on this cart: the saved fields are read over the live
|
||||
/// ones, the rest (riders, lerp, hurt state) stays; its kind never changes.
|
||||
/// `None`, with nothing changed, when the compound has no position.
|
||||
pub fn load(&mut self, tag: &horizon_world::nbt::Tag) -> Option<()> {
|
||||
use horizon_world::nbt::Tag;
|
||||
let (position, velocity, yaw) = horizon_client::entity_nbt::boat_from_nbt(tag)?;
|
||||
let kind = self.kind;
|
||||
let c = self;
|
||||
c.position = position;
|
||||
c.previous = position;
|
||||
c.velocity = velocity;
|
||||
c.yaw = yaw;
|
||||
c.previous_yaw = yaw;
|
||||
c.no_gravity = horizon_client::entity_nbt::flag(tag, "NoGravity");
|
||||
c.on_ground = horizon_client::entity_nbt::flag(tag, "OnGround");
|
||||
c.pitch = horizon_client::entity_nbt::rotation(tag)[1];
|
||||
c.previous_pitch = c.pitch;
|
||||
// ValueInput's numeric getters take any NumericTag: getIntOr is its
|
||||
// intValue, getShortOr the low 16 bits of it, getBooleanOr
|
||||
// `byteValue() != 0` (the low 8), getDoubleOr its doubleValue.
|
||||
let int = |k: &str| {
|
||||
tag.get(k)
|
||||
.and_then(horizon_client_core::nbt_path::int_value)
|
||||
};
|
||||
let flag = |k: &str, default: bool| int(k).map_or(default, |v| v as i8 != 0);
|
||||
let double = |k: &str| match tag.get(k) {
|
||||
Some(Tag::Byte(v)) => f64::from(*v),
|
||||
Some(Tag::Short(v)) => f64::from(*v),
|
||||
Some(Tag::Int(v)) => f64::from(*v),
|
||||
Some(Tag::Long(v)) => *v as f64,
|
||||
Some(Tag::Float(v)) => f64::from(f32::from_bits(*v)),
|
||||
Some(Tag::Double(v)) => f64::from_bits(*v),
|
||||
_ => 0.0,
|
||||
};
|
||||
// Entity.load: OnGround and fall_distance, as saved.
|
||||
(c.on_ground, c.fall_distance) = horizon_client::entity_nbt::ground_from_nbt(tag);
|
||||
c.flipped = flag("FlippedRotation", false);
|
||||
c.first_tick = flag("HasTicked", false);
|
||||
if kind.slots() > 0 {
|
||||
c.items = horizon_client::save::decode_items(tag.get("Items"), kind.slots());
|
||||
}
|
||||
c.enabled = flag("Enabled", true);
|
||||
c.fuel = int("Fuel").map_or(0, |v| i32::from(v as i16));
|
||||
// readAdditionalSaveData: the saved push has no y.
|
||||
c.push = Vec3::new(double("PushX"), 0.0, double("PushZ"));
|
||||
c.fuse = int("fuse").unwrap_or(-1);
|
||||
// readAdditionalSaveData: DisplayState, DisplayOffset (default offset when absent).
|
||||
c.custom_display = tag.get("DisplayState").cloned();
|
||||
c.custom_offset = int("DisplayOffset");
|
||||
if kind == CartKind::CommandBlock {
|
||||
c.command = horizon_client::command_block::CommandBlock::load(tag);
|
||||
// MinecartCommandBlock.readAdditionalSaveData sets the synched command.
|
||||
// A changed value invokes onSyncedDataUpdated -> setCommand, resetting successes.
|
||||
if c.synced_command != c.command.command {
|
||||
c.synced_command.clone_from(&c.command.command);
|
||||
c.command.set_command(&c.synced_command);
|
||||
}
|
||||
c.synced_output = c.command.last_output_string();
|
||||
}
|
||||
// MinecartSpawner.readAdditionalSaveData: BaseSpawner.load.
|
||||
if kind == CartKind::Spawner {
|
||||
c.spawner = Some(Box::new(horizon_client::mobs::spawner::SpawnerExt::load(
|
||||
tag,
|
||||
)));
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
}
|
||||
@@ -157,7 +157,7 @@ impl Game<'_> {
|
||||
/// The numbers `Mob.compareArmor` and `compareWeapons` read off `stack`
|
||||
/// in `slot` for the mob `m`.
|
||||
pub(super) fn stack_facts(&self, m: &Mob, stack: &ItemStack, slot: Slot) -> Facts {
|
||||
let (armor, toughness, attack) = self.attributes(m, &stack.item, slot);
|
||||
let (armor, toughness, attack) = self.attributes(m, stack, slot);
|
||||
Facts {
|
||||
armor,
|
||||
armor_toughness: toughness,
|
||||
@@ -173,22 +173,18 @@ impl Game<'_> {
|
||||
|
||||
/// `Mob.getApproximateAttributeWith` for the item `id` in `slot`: each
|
||||
/// attribute starts from the mob's own value. (armour, toughness, damage)
|
||||
fn attributes(&self, m: &Mob, id: &str, slot: Slot) -> (f64, f64, f64) {
|
||||
let empty: &[(String, String, f64, String, String)] = &[];
|
||||
let modifiers = self
|
||||
.items()
|
||||
.get(id)
|
||||
.map_or(empty, |i| i.modifiers.as_slice());
|
||||
fn attributes(&self, m: &Mob, stack: &ItemStack, slot: Slot) -> (f64, f64, f64) {
|
||||
let modifiers = self.items().attribute_modifiers(stack);
|
||||
(
|
||||
equip::attribute_with(modifiers, "minecraft:armor", f64::from(m.kind.armor), slot),
|
||||
equip::attribute_with(&modifiers, "minecraft:armor", f64::from(m.kind.armor), slot),
|
||||
equip::attribute_with(
|
||||
modifiers,
|
||||
&modifiers,
|
||||
"minecraft:armor_toughness",
|
||||
f64::from(m.kind.armor_toughness),
|
||||
slot,
|
||||
),
|
||||
equip::attribute_with(
|
||||
modifiers,
|
||||
&modifiers,
|
||||
"minecraft:attack_damage",
|
||||
f64::from(m.attrs.attack_damage),
|
||||
slot,
|
||||
|
||||
@@ -10,11 +10,7 @@ use horizon_client::{
|
||||
net::FRIEND,
|
||||
};
|
||||
use horizon_core::geometry::Vec3;
|
||||
#[cfg(feature = "friends")]
|
||||
use horizon_friends::invite::{self, Parsed};
|
||||
use std::borrow::Cow;
|
||||
#[cfg(feature = "friends")]
|
||||
use std::net::SocketAddr;
|
||||
use winit::keyboard::KeyCode;
|
||||
|
||||
/// HUD and first-person hand visibility. Other hand-render restrictions still apply.
|
||||
@@ -109,28 +105,11 @@ impl NativeWorldId {
|
||||
}
|
||||
}
|
||||
|
||||
/// The part of a friend's invite anyone may see. A host's code is fresh each
|
||||
/// time it shares, so a friend's world is known for that sharing only. The
|
||||
/// relay is kept beside the addresses: two hosts on other networks can share
|
||||
/// a private address and the fixed direct port.
|
||||
#[cfg(feature = "friends")]
|
||||
fn invite_public_part(code: &str) -> String {
|
||||
match invite::parse(code) {
|
||||
Some(Parsed::Short(code)) => code.nameplate,
|
||||
Some(Parsed::Long(invite)) => {
|
||||
let relay = invite
|
||||
.relay
|
||||
.map(|r| format!("{}/{}", r.address, r.code.nameplate));
|
||||
let endpoints = invite.endpoints.iter().map(SocketAddr::to_string);
|
||||
endpoints.chain(relay).collect::<Vec<_>>().join(",")
|
||||
}
|
||||
None => String::new(),
|
||||
}
|
||||
}
|
||||
/// The part of a friend's invite anyone may see (`horizon_hooks::tunnel::Resolver::public_part`).
|
||||
/// A host's code is fresh each time it shares, so a friend's world is known for that sharing only.
|
||||
/// Without Play with Friends no invite is read: a `friend:` world shows no part of it.
|
||||
#[cfg(not(feature = "friends"))]
|
||||
fn invite_public_part(_code: &str) -> String {
|
||||
String::new()
|
||||
fn invite_public_part(code: &str) -> String {
|
||||
horizon_hooks::engine::tunnel().map_or_else(String::new, |r| r.public_part(code))
|
||||
}
|
||||
|
||||
/// A living entity known to the client, before interpolation. No authoritative writes.
|
||||
@@ -558,8 +537,6 @@ impl Game<'_> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[cfg(feature = "friends")]
|
||||
use horizon_friends::code::Code;
|
||||
|
||||
#[test]
|
||||
fn a_world_is_its_save_in_single_player_and_its_address_on_a_server() {
|
||||
@@ -573,52 +550,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "friends")]
|
||||
fn friends_worlds_are_told_apart_by_their_hosts_never_by_their_secrets() {
|
||||
const PORT: u16 = 50000;
|
||||
const RELAY: &str = "relay.example.com:443";
|
||||
// A short code: its nameplate, then its secret.
|
||||
let short = |nameplate: &str, secret: &str| Code {
|
||||
nameplate: nameplate.into(),
|
||||
secret: secret.into(),
|
||||
};
|
||||
let invite = |secret: u8, host: &str, relay: Option<Code>| {
|
||||
let code = invite::Invite {
|
||||
secret: [secret; invite::SECRET],
|
||||
port: PORT,
|
||||
endpoints: vec![format!("{host}:{PORT}").parse().unwrap()],
|
||||
relay: relay.map(|code| invite::RelayRef {
|
||||
address: RELAY.into(),
|
||||
code,
|
||||
}),
|
||||
}
|
||||
.encode();
|
||||
NativeWorldId::of(None, &format!("{FRIEND}{code}"))
|
||||
};
|
||||
assert_ne!(
|
||||
invite(1, "192.168.1.5", None),
|
||||
invite(1, "192.168.1.6", None),
|
||||
"two friends"
|
||||
);
|
||||
assert_ne!(
|
||||
invite(1, "192.168.1.5", Some(short("ABCD", "WXYZ"))),
|
||||
invite(1, "192.168.1.5", Some(short("EFGH", "WXYZ"))),
|
||||
"two friends on other networks behind the same private address"
|
||||
);
|
||||
assert_eq!(
|
||||
invite(1, "192.168.1.5", Some(short("ABCD", "WXYZ"))),
|
||||
invite(2, "192.168.1.5", Some(short("ABCD", "QRST"))),
|
||||
"the invite's and the relay code's secrets take no part"
|
||||
);
|
||||
let typed = |code: Code| NativeWorldId::of(None, &format!("{FRIEND}{code}"));
|
||||
assert_eq!(typed(short("ABCD", "WXYZ")), typed(short("ABCD", "QRST")));
|
||||
assert_eq!(
|
||||
typed(short("ABCD", "WXYZ")),
|
||||
NativeWorldId::Server(format!("{FRIEND}ABCD"))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_hud_tells_chat_and_death_from_other_screens() {
|
||||
assert_eq!(
|
||||
|
||||
@@ -102,8 +102,16 @@ impl Game<'_> {
|
||||
if food && m.health < m.attrs.max_health {
|
||||
// feed(player, hand, stack, 2, 1).
|
||||
let sound = nautilus::eat_sound(m);
|
||||
// TamableAnimal.feed: heals 2 x the food's nutrition, 1 for an
|
||||
// item with no food component.
|
||||
let nutrition = self
|
||||
.res
|
||||
.items()
|
||||
.get(item)
|
||||
.and_then(|i| i.food.as_ref())
|
||||
.map(|f| f.0 as i32);
|
||||
let m = &mut self.mobs[index];
|
||||
m.heal(2.0);
|
||||
m.heal(nautilus::feed_heal(nutrition));
|
||||
self.use_nautilus_food(item);
|
||||
self.nautilus_sound(index, sound);
|
||||
return true;
|
||||
|
||||
@@ -226,6 +226,25 @@ impl Game<'_> {
|
||||
});
|
||||
}
|
||||
|
||||
/// `Entity.collide`'s `getEntityCollisions` for a body a piston or a
|
||||
/// shulker lid pushes: the entities it can collide with
|
||||
/// (`canCollideWith`, which for every mover but a vehicle is
|
||||
/// `canBeCollidedWith`): a boat, a living shulker, a happy ghast held
|
||||
/// still. A pushed boat or minecart collides by `canVehicleCollide`
|
||||
/// instead, which this doesn't cover, and the player moves itself.
|
||||
fn pushed_entity_colliders(&self, target: Target) -> Vec<Aabb> {
|
||||
match target {
|
||||
Target::Entity(EntityHandle::Boat(_) | EntityHandle::Cart(_)) => Vec::new(),
|
||||
Target::Entity(handle) => {
|
||||
let own = with_entity!(self, handle, entity => Entity::bounds(entity));
|
||||
let mut boxes = self.item_entity_collisions();
|
||||
boxes.retain(|b| *b != own);
|
||||
boxes
|
||||
}
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The collision view of the moving piston at `pos` takes the progress
|
||||
/// its tick gave it. One no longer moving keeps its entry until
|
||||
/// `sync_moving_pistons`.
|
||||
@@ -284,6 +303,7 @@ impl Game<'_> {
|
||||
self.shulker_push_player(movement);
|
||||
return;
|
||||
}
|
||||
let entities = self.pushed_entity_colliders(target);
|
||||
let Ok(moved) = pushed_movement(
|
||||
SceneCollisions {
|
||||
world: &self.world,
|
||||
@@ -298,6 +318,7 @@ impl Game<'_> {
|
||||
body.step_height,
|
||||
body.on_ground,
|
||||
None,
|
||||
&entities,
|
||||
) else {
|
||||
return;
|
||||
};
|
||||
@@ -665,6 +686,7 @@ impl Game<'_> {
|
||||
self.projectile_piston_block_effects(i, at);
|
||||
}
|
||||
self.piston_block_triggers(before, before);
|
||||
self.release_after_block_move(target);
|
||||
return;
|
||||
}
|
||||
// `Entity.move`: a projectile a cobweb or powder snow holds stops dead
|
||||
@@ -695,6 +717,7 @@ impl Game<'_> {
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
let entities = self.pushed_entity_colliders(target);
|
||||
let resolved = with_piston_noclip(piston_direction, || {
|
||||
pushed_movement(
|
||||
SceneCollisions {
|
||||
@@ -709,6 +732,7 @@ impl Game<'_> {
|
||||
step_height,
|
||||
on_ground,
|
||||
tnt.as_ref().map(|t| t.3),
|
||||
&entities,
|
||||
)
|
||||
.unwrap_or(Vec3::ZERO)
|
||||
});
|
||||
@@ -747,6 +771,9 @@ impl Game<'_> {
|
||||
|| limited.length_squared() - resolved.length_squared() < 1.0e-7;
|
||||
if moved {
|
||||
self.apply_push(target, resolved);
|
||||
} else {
|
||||
// A move the collisions cut to nothing still ends in the check.
|
||||
self.release_after_block_move(target);
|
||||
}
|
||||
if let Some((i, from, ..)) = tnt {
|
||||
self.finish_tnt_piston_move(i, from, resolved);
|
||||
@@ -864,26 +891,37 @@ impl Game<'_> {
|
||||
}
|
||||
Target::Entity(handle) => {
|
||||
with_entity_mut!(self, handle, entity => entity.move_by_piston(movement));
|
||||
let Some(bounds) =
|
||||
with_entity!(self, handle, entity => entity.piston_release_bounds())
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let no_block_collision = {
|
||||
let mut scene = SceneCollisions {
|
||||
world: &self.world,
|
||||
binding: self.collision,
|
||||
subject: None,
|
||||
};
|
||||
no_collision_empty(&mut scene, bounds)
|
||||
};
|
||||
with_entity_mut!(self, handle, entity => {
|
||||
entity.release_after_piston(no_block_collision)
|
||||
});
|
||||
self.release_after_block_move(target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `AbstractArrow.move` with a mover other than the arrow itself (a
|
||||
/// piston, a shulker lid): after `Entity.move`, whether or not it went
|
||||
/// anywhere, a stuck arrow or trident with nothing to collide with within
|
||||
/// 0.06 starts falling (`shouldFall`, `startFalling`). Its own tick only
|
||||
/// checks once the block at its position changes, a tick later.
|
||||
fn release_after_block_move(&mut self, target: Target) {
|
||||
let Target::Entity(handle) = target else {
|
||||
return;
|
||||
};
|
||||
let Some(bounds) = with_entity!(self, handle, entity => entity.piston_release_bounds())
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let no_block_collision = {
|
||||
let mut scene = SceneCollisions {
|
||||
world: &self.world,
|
||||
binding: self.collision,
|
||||
subject: None,
|
||||
};
|
||||
no_collision_empty(&mut scene, bounds)
|
||||
};
|
||||
with_entity_mut!(self, handle, entity => {
|
||||
entity.release_after_piston(no_block_collision)
|
||||
});
|
||||
}
|
||||
|
||||
/// `causeBounce`: a slime block sets the velocity along the movement axis.
|
||||
fn bounce(&mut self, target: Target, movement: Direction) {
|
||||
match target {
|
||||
@@ -939,19 +977,16 @@ fn pushed_movement<W>(
|
||||
step_height: f32,
|
||||
on_ground: bool,
|
||||
context: Option<CollisionContext<'_>>,
|
||||
entities: &[Aabb],
|
||||
) -> horizon_client::Result<Vec3>
|
||||
where
|
||||
W: BlockCollisionWorld<Error = std::io::Error> + EntityMovementWorld<Error = std::io::Error>,
|
||||
{
|
||||
// Without the entity's own context, the one `resolve` reads at its feet.
|
||||
let context = context.unwrap_or_else(|| CollisionContext::position(before.min_position().y));
|
||||
EntityCollisions::with_border(blocks, border, at).resolve_with_context(
|
||||
before,
|
||||
movement,
|
||||
step_height,
|
||||
on_ground,
|
||||
context,
|
||||
)
|
||||
let mut collisions = EntityCollisions::with_border(blocks, border, at);
|
||||
collisions.entities = entities;
|
||||
collisions.resolve_with_context(before, movement, step_height, on_ground, context)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -980,6 +1015,7 @@ mod tests {
|
||||
0.0,
|
||||
false,
|
||||
None,
|
||||
&[],
|
||||
)
|
||||
.expect("the push resolves");
|
||||
// The wall stops the body 0.4 blocks short of the whole push.
|
||||
@@ -1003,6 +1039,7 @@ mod tests {
|
||||
0.0,
|
||||
false,
|
||||
None,
|
||||
&[],
|
||||
)
|
||||
.expect("the push resolves");
|
||||
assert!(
|
||||
@@ -1521,6 +1558,48 @@ mod h255_mob_piston_ground_tests {
|
||||
&mut std::collections::HashMap::new(),
|
||||
);
|
||||
}
|
||||
/// `AbstractArrow.move` after a push the collisions cut to nothing: a stuck
|
||||
/// arrow with nothing within 0.06 of it starts falling all the same.
|
||||
#[test]
|
||||
#[ignore = "needs prepared resources in HORIZON_TEST_RESOURCES"]
|
||||
fn a_stuck_arrow_whose_push_a_wall_stops_falls_when_nothing_holds_it() {
|
||||
with_game(|game| {
|
||||
assert!(game.run_command("setblock 6 202 4 minecraft:stone"));
|
||||
game.creatures.projectiles.clear();
|
||||
assert!(game.run_command("summon minecraft:arrow 5.75 202.5 4.5 {NoGravity:0b}"));
|
||||
let i = game
|
||||
.creatures
|
||||
.projectiles
|
||||
.iter()
|
||||
.position(|p| p.arrow.is_some())
|
||||
.expect("the arrow");
|
||||
{
|
||||
let p = &mut game.creatures.projectiles[i];
|
||||
p.velocity = Vec3::ZERO;
|
||||
p.stuck = true;
|
||||
p.arrow.as_mut().unwrap().in_ground = true;
|
||||
}
|
||||
let p = &game.creatures.projectiles[i];
|
||||
let (bounds, at) = (p.bounds(), [p.position.x, p.position.z]);
|
||||
let before = p.position;
|
||||
game.move_pushed(
|
||||
Target::Entity(EntityHandle::Projectile(i)),
|
||||
bounds,
|
||||
at,
|
||||
0.0,
|
||||
false,
|
||||
Vec3::new(0.3, 0.0, 0.0),
|
||||
Direction::East,
|
||||
&mut std::collections::HashMap::new(),
|
||||
);
|
||||
let p = &game.creatures.projectiles[i];
|
||||
assert_eq!(p.position, before, "the stone stops the push");
|
||||
assert!(
|
||||
!p.arrow.as_ref().unwrap().in_ground,
|
||||
"nothing holds it, so it falls"
|
||||
);
|
||||
});
|
||||
}
|
||||
#[test]
|
||||
#[ignore = "needs prepared resources in HORIZON_TEST_RESOURCES"]
|
||||
fn h255_added_no_request_and_exhausted_budget_keep_state() {
|
||||
|
||||
@@ -0,0 +1,334 @@
|
||||
//! Registered play-Ping regression with the production sender-flush completion boundary.
|
||||
use crate::game::{Game, lan::LanSettings};
|
||||
use horizon_client::lan::{Joined, NetConfig, Out, PlayerSender, guest::Guest, hub::HubConfig};
|
||||
use horizon_protocol::{
|
||||
buf::Reader,
|
||||
packets::{
|
||||
configuration::ClientInformation,
|
||||
play::{Clientbound as C, Serverbound as S},
|
||||
status,
|
||||
},
|
||||
};
|
||||
use std::sync::{Arc, atomic::AtomicI32, mpsc};
|
||||
|
||||
fn decode(packet: S) -> (S, usize) {
|
||||
let bytes = packet.encode();
|
||||
let mut reader = Reader::new(&bytes);
|
||||
let id = reader.varint().unwrap();
|
||||
(S::decode(id, reader.rest()).unwrap(), bytes.len())
|
||||
}
|
||||
|
||||
fn output(rx: &mpsc::Receiver<Out>) -> Vec<C> {
|
||||
rx.try_iter()
|
||||
.flat_map(|out| match out {
|
||||
Out::Packet(body) => vec![body],
|
||||
Out::Packets(bodies) => bodies,
|
||||
Out::Disconnect(reason) => panic!("unexpected disconnect: {reason:?}"),
|
||||
})
|
||||
.map(|body| {
|
||||
println!("actual outbound play body={body:02x?}");
|
||||
let mut reader = Reader::new(&body);
|
||||
let id = reader.varint().unwrap();
|
||||
C::decode(id, reader.rest()).unwrap()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn pass(game: &mut Game<'_>, conn: u32, packet: S) {
|
||||
let (packet, bytes) = decode(packet);
|
||||
if let S::PingRequest(p) = &packet {
|
||||
println!(
|
||||
"actual decoded play PingRequest conn={conn} value={} bits={:016x}",
|
||||
p.time, p.time as u64
|
||||
);
|
||||
}
|
||||
let hub = &game.lan.as_ref().unwrap().hub;
|
||||
assert_eq!(hub.guests.len(), 2);
|
||||
assert_ne!(hub.guests[0].uuid, hub.guests[1].uuid);
|
||||
assert_ne!(hub.guests[0].conn, hub.guests[1].conn);
|
||||
let registered = hub.guest(conn).expect("origin connection registered");
|
||||
assert_eq!(registered.uuid, u128::from(conn));
|
||||
assert!(!registered.fake && !registered.sender.is_fake());
|
||||
assert!(registered.left_reason.is_empty());
|
||||
assert_eq!(registered.dimension, "minecraft:overworld");
|
||||
let guest = game.lan.as_mut().unwrap().hub.guest_mut(conn).unwrap();
|
||||
guest.inbox.push_back((packet, bytes));
|
||||
guest.inbox_bytes += bytes;
|
||||
// Already registered connections: this is a second level pass, without polling sockets.
|
||||
game.level_pass.first = false;
|
||||
game.level_pass.packets_drained = false;
|
||||
let clock_before = game.world.game_time;
|
||||
game.tick_lan_packets();
|
||||
// tick_lan_packets has ended its Holding scope; production flushes these senders later.
|
||||
horizon_client::lan::flush_all(
|
||||
game.lan
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.hub
|
||||
.guests
|
||||
.iter()
|
||||
.map(|g| &g.sender),
|
||||
);
|
||||
assert_eq!(
|
||||
game.world.game_time, clock_before,
|
||||
"sender completion adds no world tick"
|
||||
);
|
||||
println!("actual sender completion conn={conn} world_clock={clock_before}");
|
||||
assert!(
|
||||
game.level_pass.packets_drained,
|
||||
"real Game packet phase completed"
|
||||
);
|
||||
assert!(
|
||||
game.lan
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.hub
|
||||
.guest(conn)
|
||||
.unwrap()
|
||||
.inbox
|
||||
.is_empty(),
|
||||
"decoded request was drained by the actual Hub"
|
||||
);
|
||||
}
|
||||
|
||||
fn fixture() -> (Game<'static>, [mpsc::Receiver<Out>; 2]) {
|
||||
let mut game = crate::game::native_machines::tests::server_game();
|
||||
game.build_registries();
|
||||
game.lan_host(
|
||||
LanSettings {
|
||||
port: 0,
|
||||
guest_commands: false,
|
||||
force_game_mode: false,
|
||||
},
|
||||
NetConfig {
|
||||
announce: false,
|
||||
..NetConfig::loopback(0, "play-ping-test".into(), 2)
|
||||
},
|
||||
HubConfig {
|
||||
default_game_mode: 0,
|
||||
guest_commands: false,
|
||||
force_game_mode: false,
|
||||
hardcore: false,
|
||||
view_distance: 2,
|
||||
simulation_distance: 2,
|
||||
dimension: "minecraft:overworld".into(),
|
||||
allow_flight: false,
|
||||
seed: 0,
|
||||
max_players: 2,
|
||||
integrated: false,
|
||||
levels: Default::default(),
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let receivers = [1, 2].map(|conn| {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let mut guest = Guest::new(
|
||||
Joined {
|
||||
conn,
|
||||
uuid: u128::from(conn),
|
||||
name: format!("PingGuest{conn}"),
|
||||
properties: Vec::new(),
|
||||
info: ClientInformation::new(2),
|
||||
brand: "test".into(),
|
||||
sender: PlayerSender::new(tx, Arc::default()),
|
||||
latency: Arc::new(AtomicI32::new(0)),
|
||||
address: "127.0.0.1:25565".parse().unwrap(),
|
||||
expected: false,
|
||||
configured: Default::default(),
|
||||
},
|
||||
conn as i32,
|
||||
[0.5, 80.0, 0.5],
|
||||
2,
|
||||
);
|
||||
guest.placed = true;
|
||||
guest.loaded = true;
|
||||
guest.loading = false;
|
||||
guest.load_timeout = 0;
|
||||
guest.last_action_tick = 7;
|
||||
game.lan.as_mut().unwrap().hub.guests.push(guest);
|
||||
rx
|
||||
});
|
||||
game.lan.as_mut().unwrap().hub.ticks = 123;
|
||||
(game, receivers)
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "needs prepared resources and isolated scratch homes/cwd"]
|
||||
fn registered_play_ping_echoes_exact_long_to_only_its_connection() {
|
||||
let (mut game, receivers) = fixture();
|
||||
for (turn, time) in [
|
||||
1,
|
||||
0,
|
||||
-1,
|
||||
i64::MIN,
|
||||
i64::MAX,
|
||||
0x0123_4567_89ab_cdef,
|
||||
0xfedc_ba98_7654_3210_u64 as i64,
|
||||
]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
let index = turn % 2;
|
||||
let conn = index as u32 + 1;
|
||||
pass(&mut game, conn, S::PingRequest(status::Ping { time }));
|
||||
let packets = output(&receivers[index]);
|
||||
let echoed: Vec<_> = packets
|
||||
.iter()
|
||||
.filter_map(|p| match p {
|
||||
C::PongResponse(p) => Some(p.time),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(
|
||||
echoed,
|
||||
vec![time],
|
||||
"missing or incorrect registered Game play-Ping reply"
|
||||
);
|
||||
assert_eq!(packets.len(), 1, "one request produces exactly one reply");
|
||||
assert_eq!(
|
||||
game.lan
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.hub
|
||||
.guest(conn)
|
||||
.unwrap()
|
||||
.last_action_tick,
|
||||
7,
|
||||
"transport ping does not count as a player action"
|
||||
);
|
||||
assert!(
|
||||
output(&receivers[1 - index]).is_empty(),
|
||||
"no reply to the other connection"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "needs prepared resources and isolated scratch homes/cwd"]
|
||||
fn dead_guest_play_ping_is_transport_not_an_action() {
|
||||
let (mut game, receivers) = fixture();
|
||||
game.lan.as_mut().unwrap().hub.guest_mut(1).unwrap().dead = true;
|
||||
pass(
|
||||
&mut game,
|
||||
1,
|
||||
S::PingRequest(status::Ping { time: i64::MIN }),
|
||||
);
|
||||
let packets = output(&receivers[0]);
|
||||
assert!(matches!(packets.as_slice(), [C::PongResponse(p)] if p.time == i64::MIN));
|
||||
assert!(output(&receivers[1]).is_empty());
|
||||
assert_eq!(
|
||||
game.lan
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.hub
|
||||
.guest(1)
|
||||
.unwrap()
|
||||
.last_action_tick,
|
||||
7
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "needs prepared resources and isolated scratch homes/cwd"]
|
||||
fn server_ping_client_pong_keeps_the_existing_prediction_route() {
|
||||
let (mut game, receivers) = fixture();
|
||||
let hub = &mut game.lan.as_mut().unwrap().hub;
|
||||
let mut config = horizon_anticheat::Config::default();
|
||||
config.log.clear();
|
||||
config.strikes.clear();
|
||||
config.prediction.enabled = true;
|
||||
hub.set_anticheat(config);
|
||||
hub.ticks = 123;
|
||||
let holding = horizon_client::lan::Holding::start();
|
||||
hub.anticheat_ping();
|
||||
drop(holding);
|
||||
// Ending Holding is not delivery: independently admit the existing flush boundary first.
|
||||
for receiver in &receivers {
|
||||
assert!(
|
||||
output(receiver).is_empty(),
|
||||
"actual server Ping is unavailable before flush"
|
||||
);
|
||||
}
|
||||
println!("actual reverse Ping unavailable after Holding drop, before flush");
|
||||
horizon_client::lan::flush_all(hub.guests.iter().map(|g| &g.sender));
|
||||
for receiver in &receivers {
|
||||
assert!(matches!(output(receiver).as_slice(), [C::Ping(p)] if p.id == 123));
|
||||
}
|
||||
pass(
|
||||
&mut game,
|
||||
1,
|
||||
S::Pong(horizon_protocol::packets::configuration::Ping { id: 123 }),
|
||||
);
|
||||
let record = game
|
||||
.lan
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.hub
|
||||
.guest_mut(1)
|
||||
.unwrap()
|
||||
.anticheat
|
||||
.record
|
||||
.as_deref_mut()
|
||||
.expect("existing client Pong updates prediction");
|
||||
assert_eq!(record.tick_end(0, false).seen, Some(123));
|
||||
for receiver in &receivers {
|
||||
assert!(output(receiver).is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "needs prepared resources and isolated scratch homes/cwd"]
|
||||
fn held_guest_play_ping_survives_the_acknowledgement_filter() {
|
||||
let (mut game, receivers) = fixture();
|
||||
let guest = game.lan.as_mut().unwrap().hub.guest_mut(1).unwrap();
|
||||
guest.placed = false;
|
||||
guest.held = true;
|
||||
pass(&mut game, 1, S::PingRequest(status::Ping { time: -1 }));
|
||||
assert!(matches!(output(&receivers[0]).as_slice(), [C::PongResponse(p)] if p.time == -1));
|
||||
assert!(output(&receivers[1]).is_empty());
|
||||
assert_eq!(
|
||||
game.lan
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.hub
|
||||
.guest(1)
|
||||
.unwrap()
|
||||
.last_action_tick,
|
||||
7
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "needs prepared resources and isolated scratch homes/cwd"]
|
||||
fn handover_guest_play_ping_survives_the_acknowledgement_filter() {
|
||||
let (mut game, receivers) = fixture();
|
||||
let handover = horizon_client::lan::arrivals::Handover {
|
||||
id: 0x1234,
|
||||
since: 123,
|
||||
record: serde_json::json!({"fixture": "handover-state-sentinel"}),
|
||||
};
|
||||
game.lan
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.hub
|
||||
.guest_mut(1)
|
||||
.unwrap()
|
||||
.handover = Some(handover.clone());
|
||||
pass(
|
||||
&mut game,
|
||||
1,
|
||||
S::PingRequest(status::Ping {
|
||||
time: 0xfedc_ba98_7654_3210_u64 as i64,
|
||||
}),
|
||||
);
|
||||
let packets = output(&receivers[0]);
|
||||
assert!(
|
||||
matches!(packets.as_slice(), [C::PongResponse(p)]
|
||||
if p.time == 0xfedc_ba98_7654_3210_u64 as i64),
|
||||
"missing matching handover play-Ping reply"
|
||||
);
|
||||
assert!(output(&receivers[1]).is_empty());
|
||||
let guest = game.lan.as_ref().unwrap().hub.guest(1).unwrap();
|
||||
assert_eq!(guest.last_action_tick, 7);
|
||||
assert_eq!(guest.handover.as_ref(), Some(&handover));
|
||||
}
|
||||
@@ -726,6 +726,37 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The ticks the frame shows (animated textures, the HUD, a shader pack's clock): the
|
||||
/// client's, or under `HORIZON_STILL` (`FixedStep::still`) the world's game time, which
|
||||
/// moves only while the world runs.
|
||||
pub(super) fn visual_ticks(&self) -> u64 {
|
||||
if self.clock.still() {
|
||||
self.world.game_time.max(0) as u64
|
||||
} else {
|
||||
self.ticks
|
||||
}
|
||||
}
|
||||
|
||||
/// The clock of the animated textures: `Minecraft.runTick` ticks the `TextureManager`
|
||||
/// only while the level runs normally. Under `HORIZON_STILL` it is the world's ticks.
|
||||
pub(super) fn texture_clock(&self) -> u64 {
|
||||
if self.clock.still() {
|
||||
self.visual_ticks()
|
||||
} else {
|
||||
self.texture_ticks
|
||||
}
|
||||
}
|
||||
|
||||
/// `Util.getMillis` for what the frame shows (the world border's scroll, the hearts' blink,
|
||||
/// the glint's under the switch): under `HORIZON_STILL`, the world's ticks in milliseconds.
|
||||
pub(super) fn visual_millis(&self) -> i64 {
|
||||
if self.clock.still() {
|
||||
self.visual_ticks() as i64 * 50
|
||||
} else {
|
||||
self.started.elapsed().as_millis() as i64
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Game<'_> {
|
||||
/// What the packs' Polytone animation modes read this tick (wiki "Variant Textures"): the
|
||||
|
||||
@@ -487,7 +487,7 @@ pub(super) fn load_resources(
|
||||
};
|
||||
let resources = Arc::new(resources);
|
||||
if menu {
|
||||
horizon_client::telemetry::mark_core_ready();
|
||||
horizon_client::play_session::core_ready();
|
||||
log::info!(
|
||||
"Horizon: core-ready after {} ms",
|
||||
started.elapsed().as_millis()
|
||||
@@ -526,7 +526,7 @@ pub(super) fn load_resources(
|
||||
.set(rest)
|
||||
.map_err(|_| "world resources already loaded".to_owned())?;
|
||||
progress.set(9);
|
||||
horizon_client::telemetry::mark_rest_ready();
|
||||
horizon_client::play_session::rest_ready();
|
||||
log::info!(
|
||||
"Horizon: rest-ready after {} ms",
|
||||
started.elapsed().as_millis()
|
||||
|
||||
@@ -292,10 +292,8 @@ fn pack_particles(
|
||||
}
|
||||
|
||||
/// The packs' custom colours; their dye and sheep colours go to the draw paths' table.
|
||||
fn read_colors(root: &std::path::Path) -> horizon_client::polytone_colors::PackColors {
|
||||
let colors = horizon_client::polytone_colors::PackColors::read(root);
|
||||
horizon_client::pack_palette::set(&colors);
|
||||
horizon_client::pack_palette::set_item_bars(root);
|
||||
horizon_client::brewing::set_pack_colors(colors.effects());
|
||||
fn read_colors(root: &std::path::Path) -> horizon_client::pack_palette::PackColors {
|
||||
let colors = horizon_client::pack_palette::read(root);
|
||||
horizon_client::brewing::set_pack_colors(colors.effects.clone());
|
||||
colors
|
||||
}
|
||||
@@ -141,11 +141,12 @@ fn pause_buttons(
|
||||
remote: bool,
|
||||
links: bool,
|
||||
additions: bool,
|
||||
split: bool,
|
||||
rows: &[(&'static str, u8)],
|
||||
) -> Vec<(f32, f32, f32, &'static str, u8, bool)> {
|
||||
let row3 = links || additions;
|
||||
let (x0, y0) = pause_grid(w, h, u8::from(row3) + u8::from(split));
|
||||
let down = 24.0 * f32::from(u8::from(row3) + u8::from(split));
|
||||
let extra = u8::from(row3) + rows.len() as u8;
|
||||
let (x0, y0) = pause_grid(w, h, extra);
|
||||
let down = 24.0 * f32::from(extra);
|
||||
let mut v = vec![
|
||||
(x0 + 4.0, y0 + 50.0, 204.0, "menu.returnToGame", 0, true),
|
||||
(x0 + 4.0, y0 + 74.0, 98.0, "gui.advancements", 4, true),
|
||||
@@ -159,9 +160,9 @@ fn pause_buttons(
|
||||
// CommonDialogData.computeExternalTitle of Dialogs.SERVER_LINKS.
|
||||
v.push((x0 + 4.0, y0 + 122.0, 204.0, "menu.server_links", 11, true));
|
||||
}
|
||||
if split {
|
||||
let y = y0 + 122.0 + if row3 { 24.0 } else { 0.0 };
|
||||
v.push((x0 + 4.0, y, 204.0, SPLIT_SETTINGS, 30, true));
|
||||
for (k, &(key, id)) in rows.iter().enumerate() {
|
||||
let y = y0 + 122.0 + 24.0 * (f32::from(u8::from(row3)) + k as f32);
|
||||
v.push((x0 + 4.0, y, 204.0, key, id, true));
|
||||
}
|
||||
v.extend([
|
||||
(x0 + 4.0, y0 + 122.0 + down, 98.0, "menu.options", 1, true),
|
||||
@@ -228,14 +229,17 @@ fn pause_icon_focus_ids(friends: bool) -> Vec<u8> {
|
||||
.collect()
|
||||
}
|
||||
const SPLIT_SETTINGS: &str = "horizon.pages.split_settings";
|
||||
/// The pause menu's first id for the pages crates registered there: id `ADDED_PAGES + i` opens
|
||||
/// registered page `i`.
|
||||
const ADDED_PAGES: u8 = 40;
|
||||
fn pause_icons(
|
||||
w: f32,
|
||||
h: f32,
|
||||
links: bool,
|
||||
additions: bool,
|
||||
split: bool,
|
||||
rows: &[(&'static str, u8)],
|
||||
) -> [(f32, f32, &'static str); 4] {
|
||||
let (x0, y0) = pause_grid(w, h, u8::from(links || additions) + u8::from(split));
|
||||
let (x0, y0) = pause_grid(w, h, u8::from(links || additions) + rows.len() as u8);
|
||||
let (x0, y) = (x0 + 60.0, y0 + 98.0);
|
||||
[
|
||||
(x0, y, "gui/sprites/pause_menu/bug"),
|
||||
@@ -307,7 +311,7 @@ impl Game<'_> {
|
||||
self.remote.is_some() && self.integrated.is_none()
|
||||
}
|
||||
pub(super) fn draw_pause_menu(&mut self, batch: &mut HudBatch) {
|
||||
let friends = self.friends_view().shown;
|
||||
let friends = self.friends_shown();
|
||||
let atlas = &self.gui.atlas;
|
||||
let (w, h) = (batch.width, batch.height);
|
||||
ui::menu_background(batch, atlas, 0.0, 0.0, w, h, true);
|
||||
@@ -331,7 +335,7 @@ impl Game<'_> {
|
||||
self.external_server(),
|
||||
links,
|
||||
additions.is_some(),
|
||||
self.options.split_guest || self.split_hud.menu,
|
||||
&self.pause_rows(),
|
||||
) {
|
||||
let is_focused = self.pause_focus == Some(id);
|
||||
let hover = active && (ui::hovered(self.cursor, x, y, bw, 20.0) || is_focused);
|
||||
@@ -358,15 +362,9 @@ impl Game<'_> {
|
||||
tooltip = Some(Text::translate("menu.custom_options.tooltip", &[]));
|
||||
}
|
||||
}
|
||||
for (i, (x, y, sprite)) in pause_icons(
|
||||
w,
|
||||
h,
|
||||
links,
|
||||
additions.is_some(),
|
||||
self.options.split_guest || self.split_hud.menu,
|
||||
)
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
for (i, (x, y, sprite)) in pause_icons(w, h, links, additions.is_some(), &self.pause_rows())
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
let active = icon_active(i, friends);
|
||||
let is_focused = (self.pause_focus == Some(10) && i == FRIENDS_ICON)
|
||||
@@ -375,11 +373,7 @@ impl Game<'_> {
|
||||
// The icon has no label: name it for controller focus and the
|
||||
// control socket's widget tree.
|
||||
if i == FRIENDS_ICON {
|
||||
horizon_client::widgets::record(
|
||||
horizon_client::friends_screen::TITLE,
|
||||
[x, y, 20.0, 20.0],
|
||||
active,
|
||||
);
|
||||
horizon_client::widgets::record(super::friends::TITLE, [x, y, 20.0, 20.0], active);
|
||||
}
|
||||
ui::button(
|
||||
batch,
|
||||
@@ -416,7 +410,7 @@ impl Game<'_> {
|
||||
batch.sprite_scaled(atlas, sprite, x + 3.0, y + 3.0, 15.0, 15.0, u32::MAX);
|
||||
if hover {
|
||||
tooltip = Some(if i == FRIENDS_ICON {
|
||||
Text::literal(horizon_client::friends_screen::title())
|
||||
Text::literal(super::friends::title())
|
||||
} else {
|
||||
Text::translate(ICON_LABELS[i], &[])
|
||||
});
|
||||
@@ -456,6 +450,10 @@ impl Game<'_> {
|
||||
// PauseScreen: `new SocialInteractionsScreen(this)`.
|
||||
15 => self.open_social(true),
|
||||
30 => self.open_split_settings(),
|
||||
id @ ADDED_PAGES.. => {
|
||||
let page = usize::from(id - ADDED_PAGES);
|
||||
self.open_options_at(false, horizon_client::options_ui::Page::Added(page));
|
||||
}
|
||||
// addCustomDialogButtons: DialogConnectionAccess.openDialog of the
|
||||
// additions dialog, from the pause menu.
|
||||
14 => {
|
||||
@@ -471,6 +469,23 @@ impl Game<'_> {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// The pause menu's full-width rows above Options: Splitscreen's settings, then the pages
|
||||
/// crates registered for the pause menu (`horizon_hooks::pages`), by their title.
|
||||
fn pause_rows(&self) -> Vec<(&'static str, u8)> {
|
||||
let split = self.options.split_guest || self.split_hud.menu;
|
||||
let pages = horizon_hooks::pages::engine::all().iter().enumerate();
|
||||
let added = pages
|
||||
.filter(|(i, page)| {
|
||||
page.place() == horizon_hooks::pages::Place::Pause
|
||||
&& *i < usize::from(u8::MAX - ADDED_PAGES)
|
||||
})
|
||||
.map(|(i, page)| (page.title(), ADDED_PAGES + i as u8));
|
||||
split
|
||||
.then_some((SPLIT_SETTINGS, 30))
|
||||
.into_iter()
|
||||
.chain(added)
|
||||
.collect()
|
||||
}
|
||||
pub(super) fn pause_click(&mut self) -> Result<()> {
|
||||
let (w, h) = {
|
||||
let size = self
|
||||
@@ -482,13 +497,7 @@ impl Game<'_> {
|
||||
};
|
||||
let links = !self.server_links().is_empty();
|
||||
let additions = self.pause_screen_additions();
|
||||
let icons = pause_icons(
|
||||
w,
|
||||
h,
|
||||
links,
|
||||
additions.is_some(),
|
||||
self.options.split_guest || self.split_hud.menu,
|
||||
);
|
||||
let icons = pause_icons(w, h, links, additions.is_some(), &self.pause_rows());
|
||||
let friends = icons[FRIENDS_ICON];
|
||||
let hit = pause_buttons(
|
||||
w,
|
||||
@@ -496,7 +505,7 @@ impl Game<'_> {
|
||||
self.external_server(),
|
||||
links,
|
||||
additions.is_some(),
|
||||
self.options.split_guest || self.split_hud.menu,
|
||||
&self.pause_rows(),
|
||||
)
|
||||
.into_iter()
|
||||
.find(|b| b.5 && ui::hovered(self.cursor, b.0, b.1, b.2, 20.0))
|
||||
@@ -509,9 +518,8 @@ impl Game<'_> {
|
||||
.map(|i| 12 + i as u8)
|
||||
})
|
||||
.or_else(|| {
|
||||
(self.friends_view().shown
|
||||
&& ui::hovered(self.cursor, friends.0, friends.1, 20.0, 20.0))
|
||||
.then_some(10)
|
||||
(self.friends_shown() && ui::hovered(self.cursor, friends.0, friends.1, 20.0, 20.0))
|
||||
.then_some(10)
|
||||
});
|
||||
if let Some(id) = hit {
|
||||
self.pause_focus = Some(id);
|
||||
@@ -536,10 +544,10 @@ impl Game<'_> {
|
||||
self.external_server(),
|
||||
links,
|
||||
self.pause_screen_additions().is_some(),
|
||||
self.options.split_guest || self.split_hud.menu,
|
||||
&self.pause_rows(),
|
||||
);
|
||||
let mut active_ids: Vec<u8> = buttons.into_iter().filter(|b| b.5).map(|b| b.4).collect();
|
||||
let friends = self.friends_view().shown;
|
||||
let friends = self.friends_shown();
|
||||
active_ids.extend(pause_icon_focus_ids(friends));
|
||||
if active_ids.is_empty() {
|
||||
return Ok(());
|
||||
@@ -584,20 +592,20 @@ impl Game<'_> {
|
||||
self.external_server(),
|
||||
links,
|
||||
self.pause_screen_additions().is_some(),
|
||||
self.options.split_guest || self.split_hud.menu,
|
||||
&self.pause_rows(),
|
||||
);
|
||||
let mut rects: Vec<(u8, f32, f32, f32)> = buttons
|
||||
.into_iter()
|
||||
.filter(|b| b.5)
|
||||
.map(|b| (b.4, b.0, b.1, b.2))
|
||||
.collect();
|
||||
let friends = self.friends_view().shown;
|
||||
let friends = self.friends_shown();
|
||||
for (i, (x, y, _)) in pause_icons(
|
||||
w,
|
||||
h,
|
||||
links,
|
||||
self.pause_screen_additions().is_some(),
|
||||
self.options.split_guest || self.split_hud.menu,
|
||||
&self.pause_rows(),
|
||||
)
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
@@ -915,7 +923,10 @@ impl Game<'_> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Screen, death_buttons, pause_buttons, pause_icon_focus_ids, pause_icons};
|
||||
use super::{
|
||||
ADDED_PAGES, SPLIT_SETTINGS, Screen, death_buttons, pause_buttons, pause_icon_focus_ids,
|
||||
pause_icons,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn death_buttons_sit_on_whole_gui_pixels() {
|
||||
@@ -998,7 +1009,7 @@ mod tests {
|
||||
#[test]
|
||||
fn pause_menu_uses_disconnect_for_servers_and_keeps_world_options() {
|
||||
for (remote, label) in [(false, "menu.returnToMenu"), (true, "menu.disconnect")] {
|
||||
let buttons = pause_buttons(427.0, 240.0, remote, false, false, false);
|
||||
let buttons = pause_buttons(427.0, 240.0, remote, false, false, &[]);
|
||||
let quit = buttons.iter().find(|b| b.4 == 2).unwrap();
|
||||
assert_eq!(quit.3, label);
|
||||
assert!(quit.5);
|
||||
@@ -1023,14 +1034,14 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn server_links_adds_a_full_width_row_above_options() {
|
||||
let with = pause_buttons(427.0, 240.0, true, true, false, false);
|
||||
let without = pause_buttons(427.0, 240.0, true, false, false, false);
|
||||
let with = pause_buttons(427.0, 240.0, true, true, false, &[]);
|
||||
let without = pause_buttons(427.0, 240.0, true, false, false, &[]);
|
||||
let links = with.iter().find(|b| b.4 == 11).unwrap();
|
||||
assert_eq!(links.2, 204.0);
|
||||
assert_eq!(links.3, "menu.server_links");
|
||||
assert!(links.5);
|
||||
// The icon row sits above it; Options and Disconnect move down 24.
|
||||
let icons = pause_icons(427.0, 240.0, true, false, false);
|
||||
let icons = pause_icons(427.0, 240.0, true, false, &[]);
|
||||
let y0_icons = ((240.0f32 - 190.0) * 0.25).floor() + 98.0;
|
||||
assert_eq!(icons[0].1, y0_icons);
|
||||
assert!(links.1 > icons[0].1);
|
||||
@@ -1046,8 +1057,8 @@ mod tests {
|
||||
fn pause_screen_additions_takes_the_server_links_row() {
|
||||
// getCustomAdditions: one additions button in row 3, never next to the
|
||||
// links button, so the grid keeps one extra row either way.
|
||||
let with = pause_buttons(427.0, 240.0, true, true, true, false);
|
||||
let links = pause_buttons(427.0, 240.0, true, true, false, false);
|
||||
let with = pause_buttons(427.0, 240.0, true, true, true, &[]);
|
||||
let links = pause_buttons(427.0, 240.0, true, true, false, &[]);
|
||||
assert!(with.iter().all(|b| b.4 != 11));
|
||||
let button = with.iter().find(|b| b.4 == 14).unwrap();
|
||||
assert_eq!(
|
||||
@@ -1060,14 +1071,42 @@ mod tests {
|
||||
assert_eq!(add.1, link.1);
|
||||
}
|
||||
assert_eq!(
|
||||
pause_icons(427.0, 240.0, true, true, false)[0].1,
|
||||
pause_icons(427.0, 240.0, true, false, false)[0].1
|
||||
pause_icons(427.0, 240.0, true, true, &[])[0].1,
|
||||
pause_icons(427.0, 240.0, true, false, &[])[0].1
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn registered_pages_stack_above_options_in_the_pause_menu() {
|
||||
let rows = [
|
||||
(SPLIT_SETTINGS, 30),
|
||||
("test.page.a", ADDED_PAGES),
|
||||
("test.page.b", ADDED_PAGES + 2),
|
||||
];
|
||||
let buttons = pause_buttons(427.0, 300.0, true, false, false, &rows);
|
||||
let y = |id| buttons.iter().find(|b| b.4 == id).unwrap().1;
|
||||
assert_eq!(y(ADDED_PAGES), y(30) + 24.0);
|
||||
assert_eq!(y(ADDED_PAGES + 2), y(ADDED_PAGES) + 24.0);
|
||||
assert_eq!(
|
||||
y(1),
|
||||
y(ADDED_PAGES + 2) + 24.0,
|
||||
"Options under the last one"
|
||||
);
|
||||
// The grid stays centred: three more rows start it higher.
|
||||
let without = pause_buttons(427.0, 300.0, true, false, false, &[]);
|
||||
let back = |b: &[(f32, f32, f32, &str, u8, bool)]| b.iter().find(|b| b.4 == 0).unwrap().1;
|
||||
assert!(back(&buttons) < back(&without));
|
||||
}
|
||||
#[test]
|
||||
fn split_settings_preserves_dialog_rows_and_icon_actions() {
|
||||
for (links, additions) in [(false, false), (true, false), (false, true), (true, true)] {
|
||||
let buttons = pause_buttons(427.0, 300.0, true, links, additions, true);
|
||||
let buttons = pause_buttons(
|
||||
427.0,
|
||||
300.0,
|
||||
true,
|
||||
links,
|
||||
additions,
|
||||
&[(SPLIT_SETTINGS, 30)],
|
||||
);
|
||||
let split = buttons.iter().find(|b| b.4 == 30).unwrap();
|
||||
let options = buttons.iter().find(|b| b.4 == 1).unwrap();
|
||||
assert_eq!(options.1, split.1 + 24.0);
|
||||
@@ -1080,7 +1119,7 @@ mod tests {
|
||||
}
|
||||
assert!(buttons.iter().all(|b| b.4 != 12 && b.4 != 13));
|
||||
assert!(
|
||||
!pause_buttons(427.0, 300.0, true, links, additions, false)
|
||||
!pause_buttons(427.0, 300.0, true, links, additions, &[])
|
||||
.iter()
|
||||
.any(|b| b.4 == 30)
|
||||
);
|
||||
|
||||
@@ -359,12 +359,19 @@ impl Game<'_> {
|
||||
self.sculk.tracker = WardenSpawnTracker::from_nbt(tag);
|
||||
}
|
||||
}
|
||||
/// Write the tracker into the player data being saved.
|
||||
/// The tracker as saved, once it has been loaded.
|
||||
pub(super) fn warden_tracker_tag(&self) -> Option<horizon_world::nbt::Tag> {
|
||||
self.sculk
|
||||
.tracker_loaded
|
||||
.then(|| self.sculk.tracker.to_nbt())
|
||||
}
|
||||
/// Write the tracker into the player data being saved. With a LAN owner,
|
||||
/// `store_owner` rewrites that from the owner's guest, so it puts the
|
||||
/// tracker there as well (`playerdata::with_warden`).
|
||||
pub(super) fn save_warden_tracker(&mut self) {
|
||||
if !self.sculk.tracker_loaded {
|
||||
let Some(tag) = self.warden_tracker_tag() else {
|
||||
return;
|
||||
}
|
||||
let tag = self.sculk.tracker.to_nbt();
|
||||
};
|
||||
let extra = self
|
||||
.level
|
||||
.player_extra
|
||||
|
||||
@@ -53,7 +53,7 @@ pub(super) struct StartupMenu {
|
||||
pub reload_packs: bool,
|
||||
/// Browse mods' worker (`mod_browser`).
|
||||
mod_jobs: super::mod_browser::Jobs,
|
||||
pub perf: horizon_client::telemetry::Session,
|
||||
pub perf: horizon_client::play_session::Session,
|
||||
}
|
||||
|
||||
impl StartupMenu {
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
//! the time left. Vanilla has no such commands: they reach operators' command trees (completions included)
|
||||
//! as a native mod's commands do (`moderation_commands`), and the game runs them itself.
|
||||
use super::{Game, chat_filter::Reply, dedicated, lan::LanHost};
|
||||
use horizon_chat_filter::{duration_text, now_millis, parse_duration};
|
||||
use horizon_client::{mod_api::NativeCommand, text::Text};
|
||||
use horizon_protocol::packets::play::{self, Clientbound as C};
|
||||
use horizon_protocol::text::translatable;
|
||||
use horizon_server_files::duration::{duration_text, now_millis, parse_duration};
|
||||
use horizon_server_files::user_lists::{BanEntry, BanList, NameAndId, format_date};
|
||||
|
||||
/// The operator level `/tempban` needs, as `/ban`'s.
|
||||
|
||||
@@ -169,33 +169,29 @@ impl Game<'_> {
|
||||
/// Leaving the game with a world open (window closed, Quit, `quit`):
|
||||
/// menu items go back (Player.closeContainer), then the world is saved,
|
||||
/// once.
|
||||
/// Keeps this play session's performance summary for the launcher to send, if the player
|
||||
/// allows performance statistics (`horizon_client::telemetry`), and starts the next one.
|
||||
/// Ends this play session for the hooks (`horizon_client::play_session`): opt-in telemetry
|
||||
/// keeps its performance summary for the launcher to send, if the player allows it.
|
||||
pub(super) fn finish_perf(&mut self) {
|
||||
// One session per computer: P1's (a splitscreen player's would count it again).
|
||||
if self.options.split_guest {
|
||||
return;
|
||||
}
|
||||
let s = &self.settings;
|
||||
let settings = horizon_telemetry::payload::Settings {
|
||||
render_distance: s.render_distance.clamp(0, 255) as u8,
|
||||
simulation_distance: s.simulation_distance.clamp(0, 255) as u8,
|
||||
// 260 is the slider's "unlimited".
|
||||
max_fps: if s.max_fps >= 260 {
|
||||
0
|
||||
} else {
|
||||
s.max_fps.min(u32::from(u16::MAX)) as u16
|
||||
},
|
||||
vsync: s.vsync,
|
||||
fullscreen: s.fullscreen,
|
||||
graphics: s.graphics_preset.clone(),
|
||||
};
|
||||
// Again now: on Wayland the window is on a screen only once it has been shown.
|
||||
if let Some(window) = &self.window {
|
||||
horizon_client::telemetry::note_display(window);
|
||||
}
|
||||
self.perf
|
||||
.finish(&horizon_client::logging::game_home(), settings);
|
||||
let s = &self.settings;
|
||||
let home = horizon_client::logging::game_home();
|
||||
self.perf.finish(&horizon_hooks::WorldLeft {
|
||||
home: &home,
|
||||
render_distance: s.render_distance,
|
||||
simulation_distance: s.simulation_distance,
|
||||
// 260 is the slider's "unlimited".
|
||||
max_fps: (s.max_fps < 260).then_some(s.max_fps),
|
||||
vsync: s.vsync,
|
||||
fullscreen: s.fullscreen,
|
||||
graphics: &s.graphics_preset,
|
||||
});
|
||||
}
|
||||
|
||||
pub(super) fn save_for_exit(&mut self) -> Result<()> {
|
||||
|
||||
@@ -504,6 +504,69 @@ impl ServerAdvancements {
|
||||
w.f32(r["experience"].as_f64().unwrap_or(0.0) as f32);
|
||||
(cooking_category(kind, r), vec![&r["ingredient"]])
|
||||
}
|
||||
// DyeRecipe.display: the target, then the dyes kept to those with
|
||||
// the DYE component, shown as the demo of dyeing that target.
|
||||
// Its placement is the target and the dye.
|
||||
"minecraft:crafting_dye" => {
|
||||
let Some(dye_component) = sync.id("minecraft:data_component_type", "minecraft:dye")
|
||||
else {
|
||||
return Vec::new();
|
||||
};
|
||||
let (target, dye) = (&r["target"], &r["dye"]);
|
||||
w.varint(
|
||||
sync.id("minecraft:recipe_display", "minecraft:crafting_shapeless")
|
||||
.unwrap_or(0),
|
||||
);
|
||||
w.varint(2);
|
||||
w.varint(slot("minecraft:tag"));
|
||||
holder_set(&mut w, target, &item);
|
||||
w.varint(slot("minecraft:only_with_component"));
|
||||
w.varint(slot("minecraft:tag"));
|
||||
holder_set(&mut w, dye, &item);
|
||||
w.varint(dye_component);
|
||||
w.varint(slot("minecraft:dyed"));
|
||||
w.varint(slot("minecraft:only_with_component"));
|
||||
w.varint(slot("minecraft:tag"));
|
||||
holder_set(&mut w, dye, &item);
|
||||
w.varint(dye_component);
|
||||
w.varint(slot("minecraft:tag"));
|
||||
holder_set(&mut w, target, &item);
|
||||
station(&mut w, "minecraft:crafting_table");
|
||||
(
|
||||
crafting_category(r["category"].as_str().unwrap_or("")).name(),
|
||||
vec![target, dye],
|
||||
)
|
||||
}
|
||||
// ImbueRecipe.display: four materials, the source kept to any
|
||||
// potion, four materials again, and the potion-wrapped result.
|
||||
// Its placement names the source in the middle cell.
|
||||
"minecraft:crafting_imbue" => {
|
||||
let (source, material) = (&r["source"], &r["material"]);
|
||||
w.varint(
|
||||
sync.id("minecraft:recipe_display", "minecraft:crafting_shaped")
|
||||
.unwrap_or(0),
|
||||
);
|
||||
w.varint(3);
|
||||
w.varint(3);
|
||||
w.varint(9);
|
||||
for cell in 0..9 {
|
||||
if cell == 4 {
|
||||
w.varint(slot("minecraft:with_any_potion"));
|
||||
}
|
||||
w.varint(slot("minecraft:tag"));
|
||||
holder_set(&mut w, if cell == 4 { source } else { material }, &item);
|
||||
}
|
||||
w.varint(slot("minecraft:with_any_potion"));
|
||||
result(&mut w);
|
||||
station(&mut w, "minecraft:crafting_table");
|
||||
(
|
||||
crafting_category(r["category"].as_str().unwrap_or("")).name(),
|
||||
vec![
|
||||
material, material, material, material, source, material, material,
|
||||
material, material,
|
||||
],
|
||||
)
|
||||
}
|
||||
_ => return Vec::new(),
|
||||
};
|
||||
self.entry_tail(&mut w, r, sync, category, &placed, flags);
|
||||
@@ -756,7 +819,15 @@ mod tests {
|
||||
"minecraft:redstone_ore": {"protocol_id": 2},
|
||||
"minecraft:redstone": {"protocol_id": 3},
|
||||
"minecraft:furnace": {"protocol_id": 4},
|
||||
"minecraft:blast_furnace": {"protocol_id": 5}
|
||||
"minecraft:blast_furnace": {"protocol_id": 5},
|
||||
"minecraft:leather_helmet": {"protocol_id": 6},
|
||||
"minecraft:crafting_table": {"protocol_id": 7},
|
||||
"minecraft:arrow": {"protocol_id": 8},
|
||||
"minecraft:lingering_potion": {"protocol_id": 9},
|
||||
"minecraft:tipped_arrow": {"protocol_id": 10}
|
||||
}},
|
||||
"minecraft:data_component_type": {"entries": {
|
||||
"minecraft:dye": {"protocol_id": 46}
|
||||
}},
|
||||
"minecraft:slot_display": {"entries": {
|
||||
"minecraft:empty": {"protocol_id": 0},
|
||||
@@ -962,6 +1033,105 @@ mod tests {
|
||||
std::fs::remove_dir_all(&root).unwrap();
|
||||
}
|
||||
|
||||
/// DyeRecipe.display: the target, then the dyes kept to what carries the
|
||||
/// DYE component, the demo of dyeing the target with one, a crafting
|
||||
/// table; its placement is the target and the dye.
|
||||
#[test]
|
||||
fn a_dye_recipe_reaches_the_book_as_its_dyed_demo() {
|
||||
let root = resources(
|
||||
"dye_recipe",
|
||||
&[(
|
||||
"leather_helmet_dyed",
|
||||
r##"{"type":"minecraft:crafting_dye","dye":"#minecraft:dyes",
|
||||
"group":"dyed_armor","result":{"id":"minecraft:leather_helmet"},
|
||||
"target":"minecraft:leather_helmet"}"##,
|
||||
)],
|
||||
);
|
||||
let (sync, e) = entry(&root, "minecraft:leather_helmet_dyed");
|
||||
let item = |n: &str| sync.id("minecraft:item", n).unwrap();
|
||||
let leather = item("minecraft:leather_helmet");
|
||||
let dye = SlotDisplay::OnlyWithComponent(
|
||||
Box::new(SlotDisplay::Tag(HolderSet::Tag("minecraft:dyes".into()))),
|
||||
sync.id("minecraft:data_component_type", "minecraft:dye")
|
||||
.unwrap(),
|
||||
);
|
||||
assert_eq!(
|
||||
e.display,
|
||||
RecipeDisplay::Shapeless {
|
||||
ingredients: vec![
|
||||
SlotDisplay::Tag(HolderSet::Items(vec![leather])),
|
||||
dye.clone(),
|
||||
],
|
||||
result: SlotDisplay::Dyed {
|
||||
dye: Box::new(dye),
|
||||
target: Box::new(SlotDisplay::Tag(HolderSet::Items(vec![leather]))),
|
||||
},
|
||||
station: SlotDisplay::Item(item("minecraft:crafting_table")),
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
e.requirements,
|
||||
Some(vec![
|
||||
HolderSet::Items(vec![leather]),
|
||||
HolderSet::Tag("minecraft:dyes".into()),
|
||||
])
|
||||
);
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
}
|
||||
|
||||
/// ImbueRecipe.display: a 3×3 of four materials, the source kept to any
|
||||
/// potion, four materials again; the result is potion-wrapped, and the
|
||||
/// source sits in the middle of the placement.
|
||||
#[test]
|
||||
fn an_imbue_recipe_reaches_the_book_with_any_potion() {
|
||||
let root = resources(
|
||||
"imbue_recipe",
|
||||
&[(
|
||||
"tipped_arrow",
|
||||
r#"{"type":"minecraft:crafting_imbue","material":"minecraft:arrow",
|
||||
"result":{"count":8,"id":"minecraft:tipped_arrow"},
|
||||
"source":"minecraft:lingering_potion"}"#,
|
||||
)],
|
||||
);
|
||||
let (sync, e) = entry(&root, "minecraft:tipped_arrow");
|
||||
let item = |n: &str| sync.id("minecraft:item", n).unwrap();
|
||||
let material = SlotDisplay::Tag(HolderSet::Items(vec![item("minecraft:arrow")]));
|
||||
let source = SlotDisplay::Tag(HolderSet::Items(vec![item("minecraft:lingering_potion")]));
|
||||
let mut cells = vec![material.clone(); 4];
|
||||
cells.push(SlotDisplay::WithAnyPotion(Box::new(source.clone())));
|
||||
cells.extend(std::iter::repeat_n(material, 4));
|
||||
assert_eq!(
|
||||
e.display,
|
||||
RecipeDisplay::Shaped {
|
||||
width: 3,
|
||||
height: 3,
|
||||
ingredients: cells,
|
||||
result: SlotDisplay::WithAnyPotion(Box::new(SlotDisplay::ItemStack(Stack::new(
|
||||
item("minecraft:tipped_arrow"),
|
||||
8,
|
||||
)))),
|
||||
station: SlotDisplay::Item(item("minecraft:crafting_table")),
|
||||
}
|
||||
);
|
||||
let arrow = HolderSet::Items(vec![item("minecraft:arrow")]);
|
||||
let potion = HolderSet::Items(vec![item("minecraft:lingering_potion")]);
|
||||
assert_eq!(
|
||||
e.requirements,
|
||||
Some(vec![
|
||||
arrow.clone(),
|
||||
arrow.clone(),
|
||||
arrow.clone(),
|
||||
arrow.clone(),
|
||||
potion,
|
||||
arrow.clone(),
|
||||
arrow.clone(),
|
||||
arrow.clone(),
|
||||
arrow,
|
||||
])
|
||||
);
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_recipe_with_an_unknown_ingredient_kind_stays_out_of_the_book() {
|
||||
let root = resources(
|
||||
|
||||
@@ -38,6 +38,7 @@ use horizon_world::{
|
||||
};
|
||||
use std::sync::{Arc, Mutex, atomic::Ordering};
|
||||
|
||||
mod acknowledgements;
|
||||
/// GameEvent types (ClientboundGameEventPacket).
|
||||
pub mod chat;
|
||||
pub mod chat_filter;
|
||||
@@ -2416,41 +2417,6 @@ impl Hub {
|
||||
events
|
||||
}
|
||||
|
||||
/// Keeps, in a frozen player's inbox, the packets that answer the server
|
||||
/// (teleport, chunk batch, loaded, keep-alive, ping, tick end, client
|
||||
/// information) and drops the rest. A chat message or signed command is
|
||||
/// dropped too, but the messages it says the client has seen are taken
|
||||
/// first (`unpackAndApplyLastSeen`), and a chat acknowledgement is handled
|
||||
/// here, in the order they came: the client counts them as acknowledged,
|
||||
/// and its next message after the freeze would not validate otherwise.
|
||||
pub(crate) fn keep_acknowledgements(&mut self, i: usize, events: &mut Vec<HostEvent>) {
|
||||
let inbox = std::mem::take(&mut self.guests[i].inbox);
|
||||
let mut bytes = 0;
|
||||
for (packet, size) in inbox {
|
||||
match &packet {
|
||||
S::Chat(c) => {
|
||||
self.apply_last_seen(i, &c.last_seen, events);
|
||||
}
|
||||
S::ChatCommandSigned(c) => {
|
||||
self.apply_last_seen(i, &c.last_seen, events);
|
||||
}
|
||||
S::ChatAck(c) => self.chat_ack(i, c.offset, events),
|
||||
S::AcceptTeleportation(_)
|
||||
| S::PlayerLoaded(_)
|
||||
| S::ChunkBatchReceived(_)
|
||||
| S::ClientInformation(_)
|
||||
| S::ClientTickEnd(_)
|
||||
| S::KeepAlive(_)
|
||||
| S::Pong(_) => {
|
||||
bytes += size;
|
||||
self.guests[i].inbox.push_back((packet, size));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
self.guests[i].inbox_bytes = bytes;
|
||||
}
|
||||
|
||||
/// Handle the players in this level's packets (`ServerGamePacketListenerImpl`
|
||||
/// for each), before the level's scheduled block ticks so a place landed in
|
||||
/// the same tick as a falling block's schedule still sees the support
|
||||
@@ -4069,7 +4035,8 @@ impl Hub {
|
||||
| S::RecipeBookSeenRecipe(_)
|
||||
| S::ClientTickEnd(_)
|
||||
| S::KeepAlive(_)
|
||||
| S::Pong(_) => false,
|
||||
| S::Pong(_)
|
||||
| S::PingRequest(_) => false,
|
||||
S::MovePlayerPos(p) => self.moved_far(i, [p.x, p.y, p.z]),
|
||||
S::MovePlayerPosRot(p) => self.moved_far(i, [p.x, p.y, p.z]),
|
||||
S::PlayerInput(_) => self.guests[i].client_loaded(),
|
||||
@@ -4167,6 +4134,8 @@ impl Hub {
|
||||
g.max_unacknowledged = 10;
|
||||
}
|
||||
S::Pong(p) => self.anticheat_pong(i, p.id),
|
||||
// ServerGamePacketListenerImpl.handlePingRequest: echo on this connection.
|
||||
S::PingRequest(p) => self.send_to(conn, &C::PongResponse(p)),
|
||||
// handleAttack is the game's too: a move queued behind an attack would move
|
||||
// the player (or land them on the ground, losing a falling attack's critical
|
||||
// hit) before the game checks their reach and state.
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
//! Transport acknowledgements retained while a guest is held or handed over.
|
||||
use super::*;
|
||||
|
||||
impl Hub {
|
||||
/// Keeps, in a frozen player's inbox, the packets that answer the server
|
||||
/// (teleport, chunk batch, loaded, keep-alive, ping, tick end, client
|
||||
/// information) and drops the rest. A chat message or signed command is
|
||||
/// dropped too, but the messages it says the client has seen are taken
|
||||
/// first (`unpackAndApplyLastSeen`), and a chat acknowledgement is handled
|
||||
/// here, in the order they came: the client counts them as acknowledged,
|
||||
/// and its next message after the freeze would not validate otherwise.
|
||||
pub(crate) fn keep_acknowledgements(&mut self, i: usize, events: &mut Vec<HostEvent>) {
|
||||
let inbox = std::mem::take(&mut self.guests[i].inbox);
|
||||
let mut bytes = 0;
|
||||
for (packet, size) in inbox {
|
||||
match &packet {
|
||||
S::Chat(c) => {
|
||||
self.apply_last_seen(i, &c.last_seen, events);
|
||||
}
|
||||
S::ChatCommandSigned(c) => {
|
||||
self.apply_last_seen(i, &c.last_seen, events);
|
||||
}
|
||||
S::ChatAck(c) => self.chat_ack(i, c.offset, events),
|
||||
S::AcceptTeleportation(_)
|
||||
| S::PlayerLoaded(_)
|
||||
| S::ChunkBatchReceived(_)
|
||||
| S::ClientInformation(_)
|
||||
| S::ClientTickEnd(_)
|
||||
| S::KeepAlive(_)
|
||||
| S::Pong(_)
|
||||
| S::PingRequest(_) => {
|
||||
bytes += size;
|
||||
self.guests[i].inbox.push_back((packet, size));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
self.guests[i].inbox_bytes = bytes;
|
||||
}
|
||||
}
|
||||
@@ -53,6 +53,27 @@ pub fn exists(world: &Path, uuid: u128) -> bool {
|
||||
path(world, uuid).is_file()
|
||||
}
|
||||
|
||||
/// The key `ServerPlayer.addAdditionalSaveData` stores the `WardenSpawnTracker` under.
|
||||
pub const WARDEN_TRACKER_KEY: &str = "warden_spawn_tracker";
|
||||
|
||||
/// Put a player's `warden_spawn_tracker` into the compound `store` writes from
|
||||
/// (`guest.saved`), so a save keeps it rather than the older one loaded with
|
||||
/// the file (the LAN owner's tracker lives in `Game`, `save_warden_tracker`).
|
||||
/// Other saved keys are left as they are; no tracker (not loaded yet) changes
|
||||
/// nothing.
|
||||
pub fn with_warden(guest: &mut Guest, tracker: Option<Tag>) -> &mut Guest {
|
||||
let Some(tracker) = tracker else {
|
||||
return guest;
|
||||
};
|
||||
let saved = guest
|
||||
.saved
|
||||
.get_or_insert_with(|| Tag::Compound(Default::default()));
|
||||
if let Tag::Compound(map) = saved {
|
||||
map.insert(WARDEN_TRACKER_KEY.into(), tracker);
|
||||
}
|
||||
guest
|
||||
}
|
||||
|
||||
/// `PlayerDataStorage.load`: a guest's saved state, if they have been here
|
||||
/// before. The player's file, else its `.dat_old` (what a save cut off
|
||||
/// between its two renames leaves, `write_dat`).
|
||||
@@ -651,6 +672,42 @@ mod tests {
|
||||
)
|
||||
}
|
||||
|
||||
/// #9965: the owner's warden warning level and cooldown reach the file on
|
||||
/// a save, instead of the ones the file was loaded with.
|
||||
#[test]
|
||||
fn a_stored_warden_tracker_replaces_the_loaded_one() {
|
||||
let mut g = guest();
|
||||
g.saved = Some(Tag::compound(vec![
|
||||
(
|
||||
"warden_spawn_tracker",
|
||||
Tag::compound(vec![
|
||||
("warning_level", Tag::Int(2)),
|
||||
("cooldown_ticks", Tag::Int(77)),
|
||||
("ticks_since_last_warning", Tag::Int(5)),
|
||||
]),
|
||||
),
|
||||
("unrelated_key", Tag::Int(9)),
|
||||
]));
|
||||
let tracker = crate::sculk::WardenSpawnTracker {
|
||||
ticks_since_last_warning: 0,
|
||||
warning_level: 3,
|
||||
cooldown_ticks: 200,
|
||||
};
|
||||
with_warden(&mut g, Some(tracker.to_nbt()));
|
||||
let (tag, _) = round_trip(&g);
|
||||
let mut back = guest();
|
||||
let mut level = Level::default();
|
||||
level.read_player(&tag);
|
||||
level.player_extra = Some(tag);
|
||||
apply(&mut back, &level);
|
||||
let saved = back.saved.as_ref().unwrap();
|
||||
let read =
|
||||
crate::sculk::WardenSpawnTracker::from_nbt(saved.get("warden_spawn_tracker").unwrap());
|
||||
assert_eq!((read.warning_level, read.cooldown_ticks), (3, 200));
|
||||
assert_eq!(read.ticks_since_last_warning, 0);
|
||||
assert!(matches!(saved.get("unrelated_key"), Some(Tag::Int(9))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_new_player_faces_the_world_spawn_angle() {
|
||||
let mut g = guest();
|
||||
|
||||
@@ -2362,19 +2362,13 @@ fn never_removed_when_far(s: Species) -> bool {
|
||||
| Species::Wither
|
||||
)
|
||||
}
|
||||
/// Movement speed bonus multiplier (baby zombies are 50% faster, etc.).
|
||||
/// The movement speed multiplier of the mob's transient `minecraft:baby`
|
||||
/// modifier (`Zombie.SPEED_MODIFIER_BABY` +0.5, `Piglin.SPEED_MODIFIER_BABY`
|
||||
/// +0.2, both `ADD_MULTIPLIED_BASE`): the same modifier `/attribute` reads, so
|
||||
/// one taken away with `modifier remove` no longer speeds the baby up.
|
||||
pub fn speed_bonus(m: &Mob) -> f64 {
|
||||
match m.species() {
|
||||
Species::Zombie
|
||||
| Species::Husk
|
||||
| Species::Drowned
|
||||
| Species::ZombieVillager
|
||||
| Species::ZombifiedPiglin
|
||||
| Species::Piglin
|
||||
if m.is_baby() =>
|
||||
{
|
||||
1.5
|
||||
}
|
||||
match super::finalize::transient(m, super::finalize::Attr::MovementSpeed) {
|
||||
Some((amount, super::finalize::Op::AddMultipliedBase)) => 1.0 + amount,
|
||||
_ => 1.0,
|
||||
}
|
||||
}
|
||||
@@ -6290,3 +6284,45 @@ mod hurt_teleport_contact_tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod baby_speed_tests {
|
||||
use super::*;
|
||||
use crate::mobs::{finalize, sensor_scan_tests::kind};
|
||||
use horizon_core::geometry::Vec3;
|
||||
use std::sync::Arc;
|
||||
|
||||
fn baby(species: Species, id: &str) -> Mob {
|
||||
let mut k = kind();
|
||||
let km = Arc::make_mut(&mut k);
|
||||
km.id = id.into();
|
||||
km.species = species;
|
||||
let mut m = Mob::new(1, k, Vec3::ZERO, 0.0);
|
||||
finalize::set_baby(&mut m, true);
|
||||
m
|
||||
}
|
||||
|
||||
/// #11994: movement reads the transient `minecraft:baby` modifier itself:
|
||||
/// `Zombie.SPEED_MODIFIER_BABY` +0.5 and `Piglin.SPEED_MODIFIER_BABY` +0.2
|
||||
/// (`ADD_MULTIPLIED_BASE`), and none once `/attribute ... modifier remove`
|
||||
/// took it away, until the next `setBaby`.
|
||||
#[test]
|
||||
fn a_babys_speed_follows_its_baby_modifier() {
|
||||
let mut zombie = baby(Species::Zombie, "minecraft:zombie");
|
||||
assert_eq!(speed_bonus(&zombie), 1.5);
|
||||
zombie
|
||||
.extra
|
||||
.attributes
|
||||
.get_or_insert_default()
|
||||
.baby_speed_removed = true;
|
||||
assert_eq!(speed_bonus(&zombie), 1.0, "removed with /attribute");
|
||||
finalize::set_baby(&mut zombie, true);
|
||||
assert_eq!(speed_bonus(&zombie), 1.5, "setBaby adds it back");
|
||||
// The piglin's is 0.2F widened to a double, not the zombie's 0.5.
|
||||
let piglin = baby(Species::Piglin, "minecraft:piglin");
|
||||
assert_eq!(speed_bonus(&piglin), 1.0 + f64::from(0.2f32));
|
||||
let mut adult = baby(Species::Zombie, "minecraft:zombie");
|
||||
finalize::set_baby(&mut adult, false);
|
||||
assert_eq!(speed_bonus(&adult), 1.0);
|
||||
}
|
||||
}
|
||||
@@ -1218,8 +1218,13 @@ impl Goal for Wander {
|
||||
pub fn stable_destination(w: &MobWorld, p: [i32; 3]) -> bool {
|
||||
!w.facts.is_air(w.world.block(p[0], p[1] - 1, p[2]))
|
||||
}
|
||||
/// `Bee.addAdditionalSaveData` (and NeutralMob's `anger_end_time`).
|
||||
pub fn to_nbt(m: &Mob, map: &mut horizon_world::nbt::Compound) {
|
||||
/// `Bee.addAdditionalSaveData` (and NeutralMob's `anger_end_time` and
|
||||
/// `angry_at`).
|
||||
pub fn to_nbt(
|
||||
m: &Mob,
|
||||
map: &mut horizon_world::nbt::Compound,
|
||||
uuid_of: &mut impl FnMut(u32) -> Option<[i32; 4]>,
|
||||
) {
|
||||
use horizon_world::nbt::Tag;
|
||||
let b = get(m).cloned().unwrap_or_default();
|
||||
for (key, pos) in [("hive_pos", b.hive), ("flower_pos", b.flower)] {
|
||||
@@ -1241,6 +1246,20 @@ pub fn to_nbt(m: &Mob, map: &mut horizon_world::nbt::Compound) {
|
||||
map.insert("CannotEnterHiveTicks".into(), Tag::Int(b.stay_out_of_hive));
|
||||
map.insert("CropsGrownSincePollination".into(), Tag::Int(b.crops_grown));
|
||||
map.insert("anger_end_time".into(), Tag::Long(b.anger_end));
|
||||
// `NeutralMob.addPersistentAngerSaveData`: `storeNullable`, nothing when
|
||||
// the bee has no target. A mob target read from the save but not in the
|
||||
// level (`Who::Mob(0)`) keeps the loaded UUID.
|
||||
match b.angry_at {
|
||||
Some(Who::Mob(0)) => {}
|
||||
target => match target.and_then(|who| crate::entity_nbt::who_uuid(who, uuid_of)) {
|
||||
Some(uuid) => {
|
||||
map.insert("angry_at".into(), Tag::IntArray(uuid.to_vec()));
|
||||
}
|
||||
None => {
|
||||
map.remove(&"angry_at".into());
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
/// `Bee.readAdditionalSaveData`.
|
||||
pub fn from_nbt(m: &mut Mob, tag: &horizon_world::nbt::Tag) {
|
||||
@@ -1260,6 +1279,13 @@ pub fn from_nbt(m: &mut Mob, tag: &horizon_world::nbt::Tag) {
|
||||
Some(Tag::Long(v)) => *v,
|
||||
_ => -1,
|
||||
};
|
||||
// `EntityReference.read`: the player's UUID is the player, any other one a
|
||||
// mob the level may not have loaded (`Who::Mob(0)`).
|
||||
let angry_at = match crate::save::uuid_from_tag(tag.get("angry_at")) {
|
||||
Some(uuid) if uuid == crate::entity_nbt::player_uuid() => Some(Who::Player),
|
||||
Some(_) => Some(Who::Mob(0)),
|
||||
None => None,
|
||||
};
|
||||
let b = st(m);
|
||||
b.ticks_without_nectar = int("TicksSincePollination").unwrap_or(0);
|
||||
b.stay_out_of_hive = int("CannotEnterHiveTicks").unwrap_or(0);
|
||||
@@ -1267,6 +1293,7 @@ pub fn from_nbt(m: &mut Mob, tag: &horizon_world::nbt::Tag) {
|
||||
b.hive = pos("hive_pos");
|
||||
b.flower = pos("flower_pos");
|
||||
b.anger_end = anger_end;
|
||||
b.angry_at = angry_at;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -1419,4 +1446,39 @@ mod tests {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// `NeutralMob.addPersistentAngerSaveData` and
|
||||
/// `readPersistentAngerSaveData`: `anger_end_time` and `angry_at`, the
|
||||
/// reference to the player the bee is angry at (`Bee` keeps the anger
|
||||
/// itself). `mob_to_nbt`/`mob_from_nbt` are the pair `Game::mob_tree_nbt`
|
||||
/// and `Game::load_mob` go through. Horizon modelled the target in
|
||||
/// `Bee.angry_at` and wrote only the timer, so a bee that had stung a
|
||||
/// player came back with its anger still counting but no one to attack.
|
||||
#[test]
|
||||
fn a_bees_anger_target_survives_a_save() {
|
||||
use crate::entity_nbt::{mob_from_nbt, mob_to_nbt, player_uuid};
|
||||
use horizon_world::nbt::Tag;
|
||||
let mut m = bee();
|
||||
{
|
||||
let b = super::st(&mut m);
|
||||
b.anger_end = 500;
|
||||
b.angry_at = Some(Who::Player);
|
||||
}
|
||||
let saved = mob_to_nbt(&m, [1, 2, 3, 4], None, None, |_| None);
|
||||
assert_eq!(
|
||||
saved.get("angry_at"),
|
||||
Some(&Tag::IntArray(player_uuid().to_vec())),
|
||||
"the save names the player"
|
||||
);
|
||||
let types = std::collections::HashMap::from([(m.kind.id.clone(), m.kind.clone())]);
|
||||
let back = mob_from_nbt(&saved, &types, 2, None).unwrap();
|
||||
let b = super::get(&back).unwrap();
|
||||
assert_eq!(b.angry_at, Some(Who::Player), "the reloaded target");
|
||||
assert_eq!(b.anger_end, 500, "the reloaded timer");
|
||||
// `storeNullable`: a calm bee writes no `angry_at`, even over a
|
||||
// compound that still names one.
|
||||
let calm = bee();
|
||||
let saved = mob_to_nbt(&calm, [1, 2, 3, 4], Some(&saved), None, |_| None);
|
||||
assert_eq!(saved.get("angry_at"), None, "no target, no key");
|
||||
}
|
||||
}
|
||||
@@ -151,20 +151,26 @@ pub fn damage_bonus(levels: &[(Arc<str>, u32)], target: Option<Species>) -> f32
|
||||
/// to the attribute map when the item is equipped.
|
||||
pub fn attack_damage(m: &Mob, items: Option<&crate::items::Items>) -> f32 {
|
||||
let base = f64::from(m.attrs.attack_damage);
|
||||
let modifiers = m
|
||||
.extra
|
||||
.main_hand
|
||||
.as_deref()
|
||||
.and_then(|id| items?.get(id))
|
||||
.map(|i| i.modifiers.as_slice());
|
||||
modifiers.map_or(base, |modifiers| {
|
||||
super::equip::attribute_with(
|
||||
modifiers,
|
||||
"minecraft:attack_damage",
|
||||
base,
|
||||
super::equip::Slot::MainHand,
|
||||
)
|
||||
}) as f32
|
||||
let modifiers = match items {
|
||||
Some(items) => m.extra.main_hand_stack().map_or_else(
|
||||
|| {
|
||||
m.extra
|
||||
.main_hand
|
||||
.as_deref()
|
||||
.and_then(|id| items.get(id))
|
||||
.map(|i| i.modifiers.clone())
|
||||
.unwrap_or_default()
|
||||
},
|
||||
|stack| items.attribute_modifiers(stack),
|
||||
),
|
||||
None => Vec::new(),
|
||||
};
|
||||
super::equip::attribute_with(
|
||||
&modifiers,
|
||||
"minecraft:attack_damage",
|
||||
base,
|
||||
super::equip::Slot::MainHand,
|
||||
) as f32
|
||||
}
|
||||
/// The extra damage of the mob's held weapon against a target species.
|
||||
pub fn melee_bonus(m: &Mob, target: Option<Species>) -> f32 {
|
||||
|
||||
@@ -830,6 +830,12 @@ impl Mob {
|
||||
let length = moved.x * moved.x + moved.y * moved.y + moved.z * moved.z;
|
||||
let wanted_length = wanted.x * wanted.x + wanted.y * wanted.y + wanted.z * wanted.z;
|
||||
if length > 1.0e-7 || wanted_length - length < 1.0e-7 {
|
||||
// A move of a block or more through water or a reset block (a
|
||||
// vine, a ladder) resets the fall before the move's drop counts.
|
||||
if self.fall_distance != 0.0 && length >= 1.0 && fall_reset_hit(w, self.position, moved)
|
||||
{
|
||||
self.fall_distance = 0.0;
|
||||
}
|
||||
self.position = Vec3::new(
|
||||
self.position.x + moved.x,
|
||||
self.position.y + moved.y,
|
||||
@@ -2189,3 +2195,18 @@ impl RestitutionWorld for MobRestitution<'_, '_> {
|
||||
#[cfg(test)]
|
||||
#[path = "living_tests.rs"]
|
||||
mod tests;
|
||||
/// `Entity.move`'s fall-reset ray for a mob, with the world's
|
||||
/// `minecraft:fall_damage_resetting` blocks.
|
||||
fn fall_reset_hit(w: &MobWorld, from: Vec3, moved: Vec3) -> bool {
|
||||
let reset = w.block_tags.get("minecraft:fall_damage_resetting");
|
||||
crate::entities::fall_reset_hit(
|
||||
w.world,
|
||||
w.fluid_tables,
|
||||
|p| {
|
||||
let name = &w.facts.get(w.world.block(p.x, p.y, p.z)).name;
|
||||
reset.is_some_and(|t| t.contains(&**name))
|
||||
},
|
||||
from,
|
||||
moved,
|
||||
)
|
||||
}
|
||||
@@ -90,6 +90,11 @@ pub fn is_food(m: &Mob, item: &str, taming: &[String]) -> bool {
|
||||
m.kind.food.iter().any(|t| t == item)
|
||||
}
|
||||
}
|
||||
/// `TamableAnimal.feed`'s heal: twice the food's nutrition, or 1 for an item
|
||||
/// without a food component (a fish bucket).
|
||||
pub fn feed_heal(nutrition: Option<i32>) -> f32 {
|
||||
nutrition.map_or(1.0, |n| 2.0 * n as f32)
|
||||
}
|
||||
/// The ambient, hurt and death sounds by age and whether it is under water.
|
||||
pub fn sounds(m: &Mob) -> [&'static str; 3] {
|
||||
let under = m.fluids.eye_in_water;
|
||||
@@ -1122,3 +1127,22 @@ mod cooldown_memory_tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod feed_heal_tests {
|
||||
use super::feed_heal;
|
||||
|
||||
#[test]
|
||||
fn heals_twice_the_nutrition_of_vanilla_foods() {
|
||||
// Foods.java: cod 2, pufferfish 1, cooked cod 5, cooked salmon 6.
|
||||
assert_eq!(feed_heal(Some(2)), 4.0);
|
||||
assert_eq!(feed_heal(Some(1)), 2.0);
|
||||
assert_eq!(feed_heal(Some(5)), 10.0);
|
||||
assert_eq!(feed_heal(Some(6)), 12.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn item_without_food_component_heals_one() {
|
||||
assert_eq!(feed_heal(None), 1.0);
|
||||
}
|
||||
}
|
||||
@@ -48,39 +48,144 @@ impl SeenGrid {
|
||||
return;
|
||||
}
|
||||
if let Some(list) = self.cells.get_mut(&old)
|
||||
&& let Some(k) = list.iter().position(|&j| j == i as u32)
|
||||
&& let Ok(k) = list.binary_search(&(i as u32))
|
||||
{
|
||||
list.swap_remove(k);
|
||||
list.remove(k);
|
||||
}
|
||||
self.cells.entry(at).or_default().push(i as u32);
|
||||
let list = self.cells.entry(at).or_default();
|
||||
let idx = match list.binary_search(&(i as u32)) {
|
||||
Ok(k) | Err(k) => k,
|
||||
};
|
||||
list.insert(idx, i as u32);
|
||||
}
|
||||
/// The indices, ascending, of every entry whose box can meet `area`
|
||||
/// (a superset: callers still test it). False when the area spans more
|
||||
/// columns than the grid holds (or is not finite): scan them all.
|
||||
/// columns than the grid has entries (at least 64), or is not finite:
|
||||
/// scan them all.
|
||||
pub fn candidates(&self, area: Aabb, out: &mut Vec<u32>) -> bool {
|
||||
out.clear();
|
||||
// A box meets `area` only if its minimum corner is below area.max
|
||||
// and within `reach` (plus a margin for rounding) of area.min.
|
||||
let margin = self.reach + 1.0;
|
||||
let lo = column(area.min(Axis::X) - margin, area.min(Axis::Z) - margin);
|
||||
let hi = column(area.max(Axis::X), area.max(Axis::Z));
|
||||
let span = |a: i32, b: i32| i64::from(b) - i64::from(a) + 1;
|
||||
let (nx, nz) = (span(lo.0, hi.0), span(lo.1, hi.1));
|
||||
if nx < 1 || nz < 1 || nx.saturating_mul(nz) > self.cells.len() as i64 {
|
||||
let min_x = area.min(Axis::X) - margin;
|
||||
let min_z = area.min(Axis::Z) - margin;
|
||||
let max_x = area.max(Axis::X);
|
||||
let max_z = area.max(Axis::Z);
|
||||
if !min_x.is_finite() || !min_z.is_finite() || !max_x.is_finite() || !max_z.is_finite() {
|
||||
return false;
|
||||
}
|
||||
let lo = column(min_x, min_z);
|
||||
let hi = column(max_x, max_z);
|
||||
let span = |a: i32, b: i32| i64::from(b) - i64::from(a) + 1;
|
||||
let (nx, nz) = (span(lo.0, hi.0), span(lo.1, hi.1));
|
||||
let max_cells = (self.cell_of.len() as i64).max(64);
|
||||
if nx < 1 || nz < 1 || nx.saturating_mul(nz) > max_cells {
|
||||
return false;
|
||||
}
|
||||
let mut slices = [&[][..]; 16];
|
||||
let mut num_slices = 0;
|
||||
let mut overflow = false;
|
||||
for x in lo.0..=hi.0 {
|
||||
for z in lo.1..=hi.1 {
|
||||
if let Some(list) = self.cells.get(&(x, z)) {
|
||||
out.extend_from_slice(list);
|
||||
if let Some(list) = self.cells.get(&(x, z))
|
||||
&& !list.is_empty()
|
||||
{
|
||||
if num_slices < slices.len() {
|
||||
slices[num_slices] = list.as_slice();
|
||||
num_slices += 1;
|
||||
} else {
|
||||
overflow = true;
|
||||
out.extend_from_slice(list);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out.sort_unstable();
|
||||
if overflow {
|
||||
for s in &slices[..num_slices] {
|
||||
out.extend_from_slice(s);
|
||||
}
|
||||
out.sort_unstable();
|
||||
return true;
|
||||
}
|
||||
merge_slices(&slices[..num_slices], out);
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fn merge_2(s0: &[u32], s1: &[u32], out: &mut Vec<u32>) {
|
||||
out.reserve(s0.len() + s1.len());
|
||||
let (mut i, mut j) = (0, 0);
|
||||
while i < s0.len() && j < s1.len() {
|
||||
if s0[i] < s1[j] {
|
||||
out.push(s0[i]);
|
||||
i += 1;
|
||||
} else {
|
||||
out.push(s1[j]);
|
||||
j += 1;
|
||||
}
|
||||
}
|
||||
out.extend_from_slice(&s0[i..]);
|
||||
out.extend_from_slice(&s1[j..]);
|
||||
}
|
||||
|
||||
fn merge_slices(slices: &[&[u32]], out: &mut Vec<u32>) {
|
||||
match slices.len() {
|
||||
0 => {}
|
||||
1 => out.extend_from_slice(slices[0]),
|
||||
2 => merge_2(slices[0], slices[1], out),
|
||||
3 => {
|
||||
let (mut i, mut j, mut k) = (0, 0, 0);
|
||||
let (s0, s1, s2) = (slices[0], slices[1], slices[2]);
|
||||
out.reserve(s0.len() + s1.len() + s2.len());
|
||||
while i < s0.len() || j < s1.len() || k < s2.len() {
|
||||
let v0 = if i < s0.len() { s0[i] } else { u32::MAX };
|
||||
let v1 = if j < s1.len() { s1[j] } else { u32::MAX };
|
||||
let v2 = if k < s2.len() { s2[k] } else { u32::MAX };
|
||||
if v0 <= v1 && v0 <= v2 {
|
||||
out.push(v0);
|
||||
i += 1;
|
||||
} else if v1 <= v2 {
|
||||
out.push(v1);
|
||||
j += 1;
|
||||
} else {
|
||||
out.push(v2);
|
||||
k += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
4 => {
|
||||
let (mut i, mut j, mut k, mut l) = (0, 0, 0, 0);
|
||||
let (s0, s1, s2, s3) = (slices[0], slices[1], slices[2], slices[3]);
|
||||
out.reserve(s0.len() + s1.len() + s2.len() + s3.len());
|
||||
while i < s0.len() || j < s1.len() || k < s2.len() || l < s3.len() {
|
||||
let v0 = if i < s0.len() { s0[i] } else { u32::MAX };
|
||||
let v1 = if j < s1.len() { s1[j] } else { u32::MAX };
|
||||
let v2 = if k < s2.len() { s2[k] } else { u32::MAX };
|
||||
let v3 = if l < s3.len() { s3[l] } else { u32::MAX };
|
||||
if v0 <= v1 && v0 <= v2 && v0 <= v3 {
|
||||
out.push(v0);
|
||||
i += 1;
|
||||
} else if v1 <= v2 && v1 <= v3 {
|
||||
out.push(v1);
|
||||
j += 1;
|
||||
} else if v2 <= v3 {
|
||||
out.push(v2);
|
||||
k += 1;
|
||||
} else {
|
||||
out.push(v3);
|
||||
l += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
for s in slices {
|
||||
out.extend_from_slice(s);
|
||||
}
|
||||
out.sort_unstable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -145,4 +250,91 @@ mod tests {
|
||||
);
|
||||
assert!(!grid.candidates(huge, &mut out));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn candidates_merges_multiple_cells_in_strict_order() {
|
||||
// Place boxes across 4 adjacent grid cells: (0, 0), (4, 0), (0, 4), (4, 4)
|
||||
// With interleaving snapshot indices to test 1, 2, 3, and 4-way merges.
|
||||
let boxes = vec![
|
||||
boxed(0.5, 64.0, 0.5, 0.6), // 0: cell (0, 0)
|
||||
boxed(4.5, 64.0, 0.5, 0.6), // 1: cell (1, 0)
|
||||
boxed(0.5, 64.0, 4.5, 0.6), // 2: cell (0, 1)
|
||||
boxed(4.5, 64.0, 4.5, 0.6), // 3: cell (1, 1)
|
||||
boxed(0.8, 64.0, 0.8, 0.6), // 4: cell (0, 0)
|
||||
boxed(4.8, 64.0, 0.8, 0.6), // 5: cell (1, 0)
|
||||
boxed(0.8, 64.0, 4.8, 0.6), // 6: cell (0, 1)
|
||||
boxed(4.8, 64.0, 4.8, 0.6), // 7: cell (1, 1)
|
||||
];
|
||||
let grid = SeenGrid::new(boxes.into_iter());
|
||||
let mut out = Vec::new();
|
||||
|
||||
// Query covering only cell (0, 0): indices 0, 4
|
||||
let q1 = boxed(0.1, 64.0, 0.1, 1.5);
|
||||
assert!(grid.candidates(q1, &mut out));
|
||||
assert_eq!(out, vec![0, 4]);
|
||||
|
||||
// Query covering cells (0, 0) and (1, 0): indices 0, 1, 4, 5
|
||||
let q2 = Aabb::new(Vec3::new(0.0, 64.0, 0.0), Vec3::new(6.0, 66.0, 1.5));
|
||||
assert!(grid.candidates(q2, &mut out));
|
||||
assert_eq!(out, vec![0, 1, 4, 5]);
|
||||
|
||||
// Query covering all 4 cells: indices 0..8 in order
|
||||
let q4 = Aabb::new(Vec3::new(0.0, 64.0, 0.0), Vec3::new(6.0, 66.0, 6.0));
|
||||
assert!(grid.candidates(q4, &mut out));
|
||||
assert_eq!(out, vec![0, 1, 2, 3, 4, 5, 6, 7]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn bench_dense_farm() {
|
||||
let n = 300;
|
||||
let mut seed = 0x1234_5678_9abc_def0_u64;
|
||||
let mut next = |range: f64| {
|
||||
seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1);
|
||||
(seed >> 11) as f64 / (1u64 << 53) as f64 * range
|
||||
};
|
||||
// 300 mobs in a 1.5 x 1.5 block pen around (3.5, 64.0, 3.5) crossing cell boundary at 4.0
|
||||
let mut boxes: Vec<Aabb> = (0..n)
|
||||
.map(|_| {
|
||||
let x = 3.5 + next(1.5);
|
||||
let z = 3.5 + next(1.5);
|
||||
boxed(x, 64.0, z, 0.6)
|
||||
})
|
||||
.collect();
|
||||
let mut grid = SeenGrid::new(boxes.iter().copied());
|
||||
let mut out = Vec::new();
|
||||
|
||||
let t = std::time::Instant::now();
|
||||
let rounds = 100;
|
||||
let mut fallback_count = 0;
|
||||
let mut query_dur = std::time::Duration::ZERO;
|
||||
let mut update_dur = std::time::Duration::ZERO;
|
||||
for _ in 0..rounds {
|
||||
for i in 0..n {
|
||||
let area = boxes[i];
|
||||
let t0 = std::time::Instant::now();
|
||||
if !grid.candidates(area, &mut out) {
|
||||
fallback_count += 1;
|
||||
out.clear();
|
||||
out.extend(0..n as u32);
|
||||
}
|
||||
query_dur += t0.elapsed();
|
||||
// small jitter
|
||||
let x = 3.5 + next(1.5);
|
||||
let z = 3.5 + next(1.5);
|
||||
boxes[i] = boxed(x, 64.0, z, 0.6);
|
||||
let t1 = std::time::Instant::now();
|
||||
grid.update(i, boxes[i]);
|
||||
update_dur += t1.elapsed();
|
||||
}
|
||||
}
|
||||
let elapsed = t.elapsed();
|
||||
println!(
|
||||
"{rounds} ticks of {n} mobs in 1.5x1.5 pen: {elapsed:?} (query: {:.3} ms, update: {:.3} ms, per tick: {:.3} ms, fallback: {fallback_count}/{})",
|
||||
query_dur.as_secs_f64() * 1e3 / rounds as f64,
|
||||
update_dur.as_secs_f64() * 1e3 / rounds as f64,
|
||||
elapsed.as_secs_f64() * 1e3 / rounds as f64,
|
||||
rounds * n
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -855,9 +855,11 @@ pub fn texture(m: &Mob) -> String {
|
||||
"red",
|
||||
"black",
|
||||
];
|
||||
match COLORS.get(m.extra.color as usize) {
|
||||
Some(c) => format!("entity/shulker/shulker_{c}"),
|
||||
None => "entity/shulker/shulker".into(),
|
||||
// Shulker.getColor: the saved byte is signed; 16 and above are undyed, and
|
||||
// DyeColor.byId sends a negative id to white (OutOfBoundsStrategy.ZERO).
|
||||
match m.extra.color as i8 {
|
||||
16.. => "entity/shulker/shulker".into(),
|
||||
c => format!("entity/shulker/shulker_{}", COLORS[c.max(0) as usize]),
|
||||
}
|
||||
}
|
||||
/// `attachFace.getOpposite().getRotation()` by attach face: the open
|
||||
@@ -1144,3 +1146,46 @@ mod tests {
|
||||
assert!((h - 1.0).abs() < 1e-9, "{h}");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod saved_bytes_tests {
|
||||
use super::*;
|
||||
|
||||
/// `Shulker.readAdditionalSaveData`: `AttachFace` is
|
||||
/// `Direction.LEGACY_ID_CODEC` (`from3DDataValue` wraps the id through
|
||||
/// `abs(id % 6)`), while `Peek` and `Color` are raw `getByteOr` bytes. The
|
||||
/// load clamped all three to their setters' ranges instead.
|
||||
#[test]
|
||||
fn a_shulker_reads_its_face_and_lid_bytes_like_vanilla() {
|
||||
use crate::entity_nbt::mob_from_nbt;
|
||||
use horizon_world::nbt::Tag;
|
||||
let mut kind = crate::mobs::sensor_scan_tests::kind();
|
||||
let k = std::sync::Arc::make_mut(&mut kind);
|
||||
k.id = "minecraft:shulker".into();
|
||||
k.species = Species::Shulker;
|
||||
let types = [("minecraft:shulker".into(), kind)].into();
|
||||
let tag = Tag::compound([
|
||||
("id", Tag::String("minecraft:shulker".into())),
|
||||
(
|
||||
"Pos",
|
||||
Tag::List(vec![
|
||||
Tag::Double(0.0f64.to_bits()),
|
||||
Tag::Double(64.0f64.to_bits()),
|
||||
Tag::Double(0.0f64.to_bits()),
|
||||
]),
|
||||
),
|
||||
("AttachFace", Tag::Byte(7)),
|
||||
("Peek", Tag::Byte(-1)),
|
||||
("Color", Tag::Byte(-1)),
|
||||
]);
|
||||
let m = mob_from_nbt(&tag, &types, 1, None).unwrap();
|
||||
assert_eq!(m.extra.variant3, 1, "abs(7 % 6) is up");
|
||||
assert_eq!(m.extra.counter, -1, "the peek byte is kept");
|
||||
assert_eq!(m.extra.color, 255, "the color byte is kept");
|
||||
assert_eq!(
|
||||
texture(&m),
|
||||
"entity/shulker/shulker_white",
|
||||
"DyeColor.byId(-1)"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,63 @@ pub struct TargetInfo {
|
||||
/// `isPassenger`.
|
||||
pub passenger: bool,
|
||||
}
|
||||
thread_local! {
|
||||
static NEAR_BUF_POOL: std::cell::RefCell<Vec<Vec<u32>>> = const { std::cell::RefCell::new(Vec::new()) };
|
||||
}
|
||||
|
||||
pub struct NearIter<'a> {
|
||||
others: &'a [Seen],
|
||||
indices: Option<Vec<u32>>,
|
||||
pos: usize,
|
||||
fallback: Option<std::slice::Iter<'a, Seen>>,
|
||||
}
|
||||
|
||||
impl<'a> Iterator for NearIter<'a> {
|
||||
type Item = &'a Seen;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if let Some(indices) = &self.indices
|
||||
&& self.pos < indices.len()
|
||||
{
|
||||
let idx = indices[self.pos];
|
||||
self.pos += 1;
|
||||
return Some(&self.others[idx as usize]);
|
||||
}
|
||||
if let Some(all) = &mut self.fallback {
|
||||
return all.next();
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
if let Some(indices) = &self.indices {
|
||||
let rem = indices.len().saturating_sub(self.pos);
|
||||
(rem, Some(rem))
|
||||
} else if let Some(all) = &self.fallback {
|
||||
all.size_hint()
|
||||
} else {
|
||||
(0, Some(0))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for NearIter<'_> {
|
||||
fn drop(&mut self) {
|
||||
if let Some(mut buf) = self.indices.take() {
|
||||
buf.clear();
|
||||
NEAR_BUF_POOL.with(|pool| {
|
||||
if let Ok(mut pool) = pool.try_borrow_mut()
|
||||
&& pool.len() < 8
|
||||
{
|
||||
pool.push(buf);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Chunk and section cache for raycast block traversals.
|
||||
/// Caches the current section to avoid section index checks and hashmap lookups
|
||||
/// for consecutive voxels in the same 16x16x16 section.
|
||||
@@ -512,15 +569,39 @@ impl MobWorld<'_> {
|
||||
}
|
||||
/// The other mobs whose boxes may meet `area` (callers test the boxes),
|
||||
/// in snapshot order: through the grid when there is one, else all.
|
||||
pub fn others_near(&self, area: Aabb) -> impl Iterator<Item = &Seen> {
|
||||
let mut near = Vec::new();
|
||||
pub fn others_near(&self, area: Aabb) -> NearIter<'_> {
|
||||
let mut buf = NEAR_BUF_POOL.with(|pool| {
|
||||
pool.try_borrow_mut()
|
||||
.ok()
|
||||
.and_then(|mut p| p.pop())
|
||||
.unwrap_or_default()
|
||||
});
|
||||
let indexed = self
|
||||
.others_grid
|
||||
.is_some_and(|grid| grid.candidates(area, &mut near));
|
||||
let all = (!indexed).then(|| self.others.iter());
|
||||
near.into_iter()
|
||||
.map(|i| &self.others[i as usize])
|
||||
.chain(all.into_iter().flatten())
|
||||
.is_some_and(|grid| grid.candidates(area, &mut buf));
|
||||
if indexed {
|
||||
NearIter {
|
||||
others: self.others,
|
||||
indices: Some(buf),
|
||||
pos: 0,
|
||||
fallback: None,
|
||||
}
|
||||
} else {
|
||||
buf.clear();
|
||||
NEAR_BUF_POOL.with(|pool| {
|
||||
if let Ok(mut pool) = pool.try_borrow_mut()
|
||||
&& pool.len() < 8
|
||||
{
|
||||
pool.push(buf);
|
||||
}
|
||||
});
|
||||
NearIter {
|
||||
others: self.others,
|
||||
indices: None,
|
||||
pos: 0,
|
||||
fallback: Some(self.others.iter()),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn seen(&self, id: u32) -> Option<&Seen> {
|
||||
match self.others_index {
|
||||
|
||||
@@ -2148,18 +2148,16 @@ impl MotionWorld<'_, '_, '_> {
|
||||
let gravity = living_motion::effective_gravity(self)?;
|
||||
self.player.velocity = fall_flying_movement(v, self.player.pitch, self.player.yaw, gravity);
|
||||
let movement = self.player.velocity;
|
||||
let before = self.player.position;
|
||||
self.move_self(movement)?;
|
||||
// LivingEntity.handleFallFlyingCollisions: the fall-damage sound plays
|
||||
// on both sides (a glider's own travel is client-side, and the sound
|
||||
// must reach its own ears, #8906), only the server hurts, and it
|
||||
// compares the delta before the move with the one after it, which
|
||||
// `Entity.move` leaves as the movement it resolved. `move_entity` does
|
||||
// not write that back, so the distance actually travelled stands in.
|
||||
// compares the horizontal speed before `updateFallFlyingMovement`
|
||||
// with the delta `Entity.move` leaves (the collided axes zeroed).
|
||||
if self.player.collision.horizontal {
|
||||
let travelled = self.player.position.subtract(before);
|
||||
let new_h = (travelled.x * travelled.x + travelled.z * travelled.z).sqrt();
|
||||
let dmg = fly_into_wall_damage(move_h, new_h);
|
||||
let v = self.player.velocity;
|
||||
let after_h = (v.x * v.x + v.z * v.z).sqrt();
|
||||
let dmg = fly_into_wall_damage(move_h, after_h);
|
||||
if dmg > 0.0 {
|
||||
let first = self.player.events.len();
|
||||
// getFallDamageSound((int) dmg)
|
||||
@@ -3148,6 +3146,7 @@ mod tests {
|
||||
assert_eq!(fly_into_wall_damage(0.3, 0.0), 0.0);
|
||||
assert!((fly_into_wall_damage(0.5, 0.1) - 1.0).abs() < 1e-5);
|
||||
assert!((fly_into_wall_damage(1.2, 0.0) - 9.0).abs() < 1e-5);
|
||||
assert!(fly_into_wall_damage(0.01, 0.0) < 0.0);
|
||||
}
|
||||
|
||||
use super::*;
|
||||
@@ -4195,17 +4194,16 @@ mod tests {
|
||||
health: &resources.health,
|
||||
};
|
||||
w.travel_fall_flying().unwrap();
|
||||
assert!(
|
||||
w.player.events.iter().any(|e| matches!(
|
||||
let is_fall = |e: &_| {
|
||||
matches!(
|
||||
e,
|
||||
MotionEvent::Fall(crate::landing::FallEvent::Sound {
|
||||
sound: crate::landing::SceneSound::SmallFall,
|
||||
..
|
||||
})
|
||||
)),
|
||||
"{:?}",
|
||||
w.player.events
|
||||
);
|
||||
MotionEvent::Fall(crate::landing::FallEvent::Sound { .. })
|
||||
)
|
||||
};
|
||||
assert_eq!(w.player.events.iter().filter(|e| is_fall(e)).count(), 1);
|
||||
// The sound must play only once on impact, not repeat on subsequent ticks (#12304):
|
||||
w.travel_fall_flying().unwrap();
|
||||
assert_eq!(w.player.events.iter().filter(|e| is_fall(e)).count(), 1);
|
||||
assert!(
|
||||
!w.player
|
||||
.hurt_log
|
||||
|
||||
@@ -330,7 +330,7 @@ fn ping_all(servers: &[ServerEntry], pings: &Pings, icons: &Icons, file: &Path)
|
||||
/// Save the list after a move or delete, keeping the icons pings stored in
|
||||
/// servers.dat since it was loaded, and the Code of Conduct accepted on a server.
|
||||
fn save_servers(file: &Path, servers: &mut [ServerEntry]) {
|
||||
let _guard = SAVING.lock();
|
||||
let _guard = servers::lock();
|
||||
let saved = servers::load(file);
|
||||
for entry in servers.iter_mut() {
|
||||
if let Some(s) = saved.iter().find(|s| s.ip == entry.ip) {
|
||||
@@ -340,11 +340,9 @@ fn save_servers(file: &Path, servers: &mut [ServerEntry]) {
|
||||
}
|
||||
let _ = servers::save(file, servers);
|
||||
}
|
||||
/// Held while servers.dat is read and written back.
|
||||
static SAVING: Mutex<()> = Mutex::new(());
|
||||
/// Store `icon` on the entries for `address` (OnlineServerEntry.updateServerList).
|
||||
fn save_icon(file: &Path, address: &str, icon: Option<String>) {
|
||||
let _guard = SAVING.lock();
|
||||
let _guard = servers::lock();
|
||||
let mut list = servers::load(file);
|
||||
let mut changed = false;
|
||||
for entry in list.iter_mut().filter(|e| e.ip == address) {
|
||||
@@ -364,113 +362,6 @@ pub fn save_server_icon(saves: &Path, address: &str, png: &[u8]) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Horizon's official survival server: a name and an address like any entry. The address is
|
||||
/// `horizon_telemetry::server_status::OFFICIAL_ADDRESS`, the one place it is kept.
|
||||
pub const OFFICIAL_SERVER: (&str, &str) = (
|
||||
"Horizon Official",
|
||||
horizon_telemetry::server_status::OFFICIAL_ADDRESS,
|
||||
);
|
||||
/// Beside servers.dat once the official server was offered, so a player who
|
||||
/// deletes it never gets it back: the address offered, so a new one moves the
|
||||
/// list once. The launcher writes the same file.
|
||||
const OFFICIAL_MARKER: &str = "horizon_official_server.txt";
|
||||
|
||||
/// An address as the game connects to it: any case, 25565 when no port is given.
|
||||
fn address_key(ip: &str) -> Option<(String, u16)> {
|
||||
servers::parse_address(ip).map(|(h, p)| (h.to_ascii_lowercase(), p))
|
||||
}
|
||||
|
||||
/// Puts the official server at the top of the list in `game_dir`, once: not
|
||||
/// again after the player deletes it, and not when the list has its address.
|
||||
/// When it moved (the marker names another address), the entries for its
|
||||
/// earlier addresses move to the new one, once (`move_official`).
|
||||
pub fn add_official_server(game_dir: &Path) -> std::io::Result<()> {
|
||||
let (name, address) = OFFICIAL_SERVER;
|
||||
let marker = game_dir.join(OFFICIAL_MARKER);
|
||||
// A marker that is there but can't be read: offered already, at an address not known.
|
||||
let offered = match std::fs::read_to_string(&marker) {
|
||||
Ok(text) => Some(text),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
|
||||
Err(_) => Some(String::new()),
|
||||
};
|
||||
if offered
|
||||
.as_deref()
|
||||
.is_some_and(|a| address_key(a) == address_key(address))
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
let file = game_dir.join("servers.dat");
|
||||
let _guard = SAVING.lock();
|
||||
let mut list = servers::load(&file);
|
||||
let previous = horizon_telemetry::server_status::OFFICIAL_PREVIOUS;
|
||||
if move_official(&mut list, address, previous) {
|
||||
servers::save(&file, &list)?;
|
||||
}
|
||||
if offered.is_none()
|
||||
&& !list
|
||||
.iter()
|
||||
.any(|s| address_key(&s.ip) == address_key(address))
|
||||
{
|
||||
servers::prepend(&file, &ServerEntry::new(name, address))?;
|
||||
}
|
||||
std::fs::write(marker, format!("{address}\n"))
|
||||
}
|
||||
|
||||
/// Afri-Network, which the game added once below the official server (#9299) until the
|
||||
/// partnership ended: the name and address the game gave its entry.
|
||||
const WITHDRAWN_PARTNER: (&str, &str) = ("Afri-Network (South Africa)", "afri-network.co.za");
|
||||
/// Beside servers.dat: the partner addresses the game offered, one per line (#9299).
|
||||
const PARTNER_MARKER: &str = "horizon_partner_servers.txt";
|
||||
|
||||
/// Takes the withdrawn partner server back out of the list in `game_dir`, once, where the game
|
||||
/// added it: the marker says it was offered and the entry still has the name the game gave it
|
||||
/// (an entry the player made or renamed keeps its place). Then the marker goes.
|
||||
pub fn withdraw_partner_servers(game_dir: &Path) -> std::io::Result<()> {
|
||||
let marker = game_dir.join(PARTNER_MARKER);
|
||||
let offered = match std::fs::read_to_string(&marker) {
|
||||
Ok(text) => text,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let (name, address) = WITHDRAWN_PARTNER;
|
||||
if offered
|
||||
.lines()
|
||||
.any(|l| address_key(l) == address_key(address))
|
||||
{
|
||||
let _guard = SAVING.lock();
|
||||
servers::remove_where(&game_dir.join("servers.dat"), |n, ip| {
|
||||
n == name && address_key(ip) == address_key(address)
|
||||
})?;
|
||||
}
|
||||
std::fs::remove_file(marker)
|
||||
}
|
||||
|
||||
/// Points the entries for an earlier official address at `address`, each
|
||||
/// keeping the player's name and fields. One entry for the server at most: an
|
||||
/// old one goes when the list already has `address`. Whether anything changed.
|
||||
fn move_official(list: &mut Vec<ServerEntry>, address: &str, previous: &[&str]) -> bool {
|
||||
let new = address_key(address);
|
||||
let old = |ip: &str| {
|
||||
let ip = address_key(ip);
|
||||
ip.is_some() && ip != new && previous.iter().any(|p| address_key(p) == ip)
|
||||
};
|
||||
let mut present = list.iter().any(|s| address_key(&s.ip) == new);
|
||||
let mut changed = false;
|
||||
list.retain_mut(|s| {
|
||||
if !old(&s.ip) {
|
||||
return true;
|
||||
}
|
||||
changed = true;
|
||||
if present {
|
||||
return false;
|
||||
}
|
||||
present = true;
|
||||
s.ip = address.to_owned();
|
||||
true
|
||||
});
|
||||
changed
|
||||
}
|
||||
|
||||
/// The address and kind of list row `index`: saved servers, then discovered LAN games.
|
||||
fn entry_address(servers: &[ServerEntry], lan: &Scanner, index: usize) -> Option<(String, bool)> {
|
||||
match servers.get(index) {
|
||||
@@ -2100,7 +1991,7 @@ pub fn remember_direct_join(saves: &Path, address: &str) {
|
||||
return;
|
||||
}
|
||||
let file = servers_file(saves);
|
||||
let _guard = SAVING.lock();
|
||||
let _guard = servers::lock();
|
||||
let (shown, hidden) = servers::load_all(&file);
|
||||
if shown.iter().chain(&hidden).any(|s| s.ip == address) {
|
||||
return;
|
||||
@@ -2297,22 +2188,10 @@ mod tests {
|
||||
));
|
||||
}
|
||||
|
||||
#[cfg(feature = "friends")]
|
||||
#[test]
|
||||
fn direct_connection_joins_a_pasted_invite_and_keeps_host_names() {
|
||||
let invite = horizon_friends::invite::Invite {
|
||||
secret: *b"0123456789",
|
||||
port: 25580,
|
||||
endpoints: vec!["192.168.1.20:25580".parse().unwrap()],
|
||||
relay: None,
|
||||
}
|
||||
.encode();
|
||||
assert_eq!(
|
||||
direct_address(&format!(" {invite} ")),
|
||||
format!("{}{invite}", crate::net::FRIEND)
|
||||
);
|
||||
fn direct_connection_keeps_host_names() {
|
||||
for host in ["localhost", "example.org:25566", "abcdefgh", "[::1]:25568"] {
|
||||
assert_eq!(direct_address(host), host);
|
||||
assert_eq!(direct_address(&format!(" {host} ")), host);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2546,138 +2425,6 @@ mod tests {
|
||||
assert!(favicon_bytes(&serde_json::json!({ "favicon": encoded })).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_official_server_is_added_once_at_the_top() {
|
||||
let dir = std::env::temp_dir().join(format!("horizon-mp-official-{}", std::process::id()));
|
||||
let (name, address) = OFFICIAL_SERVER;
|
||||
let file = dir.join("servers.dat");
|
||||
let ips = || -> Vec<String> { servers::load(&file).into_iter().map(|s| s.ip).collect() };
|
||||
let fresh = |list: &[ServerEntry]| {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
if !list.is_empty() {
|
||||
servers::save(&file, list).unwrap();
|
||||
}
|
||||
};
|
||||
// A fresh list: just the official server, named like it.
|
||||
fresh(&[]);
|
||||
add_official_server(&dir).unwrap();
|
||||
assert_eq!(servers::load(&file), [ServerEntry::new(name, address)]);
|
||||
// An existing list: on top, the player's servers kept in their order.
|
||||
let mine = [
|
||||
ServerEntry::new("Mine", "127.0.0.1:2"),
|
||||
ServerEntry::new("Other", "example.org"),
|
||||
];
|
||||
fresh(&mine);
|
||||
add_official_server(&dir).unwrap();
|
||||
assert_eq!(ips(), [address, "127.0.0.1:2", "example.org"]);
|
||||
// Once only: a second start adds nothing.
|
||||
add_official_server(&dir).unwrap();
|
||||
assert_eq!(ips(), [address, "127.0.0.1:2", "example.org"]);
|
||||
// Already there under the player's own name and spelling: not duplicated.
|
||||
fresh(&[
|
||||
ServerEntry::new("SMP", "Play.PlayHorizon.dev:25565"),
|
||||
mine[0].clone(),
|
||||
]);
|
||||
add_official_server(&dir).unwrap();
|
||||
assert_eq!(ips(), ["Play.PlayHorizon.dev:25565", "127.0.0.1:2"]);
|
||||
// Deleted by the player: never back.
|
||||
fresh(&mine);
|
||||
add_official_server(&dir).unwrap();
|
||||
servers::save(&file, &mine).unwrap();
|
||||
add_official_server(&dir).unwrap();
|
||||
assert_eq!(ips(), ["127.0.0.1:2", "example.org"]);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_withdrawn_partner_server_leaves_lists_the_game_put_it_in_once() {
|
||||
let dir = std::env::temp_dir().join(format!("horizon-mp-partner-{}", std::process::id()));
|
||||
let official = OFFICIAL_SERVER.1;
|
||||
let file = dir.join("servers.dat");
|
||||
let marker = dir.join(PARTNER_MARKER);
|
||||
let ips = || -> Vec<String> { servers::load(&file).into_iter().map(|s| s.ip).collect() };
|
||||
let fresh = |list: &[ServerEntry], offered: bool| {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
servers::save(&file, list).unwrap();
|
||||
if offered {
|
||||
std::fs::write(&marker, "afri-network.co.za\n").unwrap();
|
||||
}
|
||||
};
|
||||
let added = ServerEntry::new("Afri-Network (South Africa)", "afri-network.co.za");
|
||||
// The game added it: it goes, the rest stays in order, and only once.
|
||||
fresh(
|
||||
&[
|
||||
ServerEntry::new("Horizon Official", official),
|
||||
added.clone(),
|
||||
ServerEntry::new("Mine", "127.0.0.1:2"),
|
||||
],
|
||||
true,
|
||||
);
|
||||
withdraw_partner_servers(&dir).unwrap();
|
||||
assert_eq!(ips(), [official, "127.0.0.1:2"]);
|
||||
assert!(!marker.exists());
|
||||
servers::save(&file, std::slice::from_ref(&added)).unwrap();
|
||||
withdraw_partner_servers(&dir).unwrap();
|
||||
assert_eq!(ips(), ["afri-network.co.za"]);
|
||||
// The player's own entry for that address (their name) stays.
|
||||
fresh(&[ServerEntry::new("ZA", "Afri-Network.co.za:25565")], true);
|
||||
withdraw_partner_servers(&dir).unwrap();
|
||||
assert_eq!(ips(), ["Afri-Network.co.za:25565"]);
|
||||
// Never offered (no marker): nothing changes.
|
||||
fresh(&[added], false);
|
||||
withdraw_partner_servers(&dir).unwrap();
|
||||
assert_eq!(ips(), ["afri-network.co.za"]);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_moved_official_server_moves_the_old_entry_once() {
|
||||
let dir = std::env::temp_dir().join(format!("horizon-mp-moved-{}", std::process::id()));
|
||||
let address = OFFICIAL_SERVER.1;
|
||||
let file = dir.join("servers.dat");
|
||||
let marker = dir.join(OFFICIAL_MARKER);
|
||||
let ips = || -> Vec<String> { servers::load(&file).into_iter().map(|s| s.ip).collect() };
|
||||
let offered_before = |list: &[ServerEntry]| {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
servers::save(&file, list).unwrap();
|
||||
std::fs::write(&marker, "horizon.bunchy7s.dev\n").unwrap();
|
||||
};
|
||||
// The old address under the player's own name and spelling: moved, name and place kept.
|
||||
let mut old = ServerEntry::new("Our SMP", "Horizon.Bunchy7s.dev:25565");
|
||||
old.icon = Some("AAAA".into());
|
||||
offered_before(&[ServerEntry::new("Mine", "127.0.0.1:2"), old]);
|
||||
add_official_server(&dir).unwrap();
|
||||
let list = servers::load(&file);
|
||||
assert_eq!(ips(), ["127.0.0.1:2", address]);
|
||||
assert_eq!(
|
||||
(list[1].name.as_str(), list[1].icon.as_deref()),
|
||||
("Our SMP", Some("AAAA"))
|
||||
);
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(&marker).unwrap(),
|
||||
format!("{address}\n")
|
||||
);
|
||||
// Once: the old address added back afterwards is the player's.
|
||||
servers::prepend(&file, &ServerEntry::new("Old", "horizon.bunchy7s.dev")).unwrap();
|
||||
add_official_server(&dir).unwrap();
|
||||
assert_eq!(ips(), ["horizon.bunchy7s.dev", "127.0.0.1:2", address]);
|
||||
// The new address already listed: no second entry.
|
||||
offered_before(&[
|
||||
ServerEntry::new("Old", "horizon.bunchy7s.dev"),
|
||||
ServerEntry::new("New", address),
|
||||
]);
|
||||
add_official_server(&dir).unwrap();
|
||||
assert_eq!(ips(), [address]);
|
||||
// Deleted by the player before the move: not offered again; another port is left alone.
|
||||
offered_before(&[ServerEntry::new("Test", "horizon.bunchy7s.dev:25570")]);
|
||||
add_official_server(&dir).unwrap();
|
||||
assert_eq!(ips(), ["horizon.bunchy7s.dev:25570"]);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arrow_keys_select_servers_and_shift_moves_them() {
|
||||
let dir = std::env::temp_dir().join(format!("horizon-mp-keys-{}", std::process::id()));
|
||||
|
||||
@@ -30,6 +30,8 @@ use std::{
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use horizon_hooks::tunnel;
|
||||
|
||||
/// `CommonListenerCookie.serverCookies`: cookies the server asked the client
|
||||
/// to store (`ClientboundStoreCookiePacket`), answered to
|
||||
/// `ClientboundCookieRequestPacket`. A `transfer` carries them to the next
|
||||
@@ -496,62 +498,36 @@ impl Drop for Session {
|
||||
}
|
||||
|
||||
/// An address of the form `friend:K7QM-3XPD` joins a friend's world through
|
||||
/// its invite code (horizon-friends): the session goes through a tunnel the
|
||||
/// client reaches on a loopback port, so everything after is a plain join.
|
||||
#[cfg(feature = "friends")]
|
||||
pub const FRIEND: &str = horizon_friends::ADDRESS_PREFIX;
|
||||
/// Without the `friends` feature such an address is refused (`join_friend`).
|
||||
#[cfg(not(feature = "friends"))]
|
||||
pub const FRIEND: &str = "friend:";
|
||||
/// its invite code (Play with Friends, through a hook's [`tunnel::Resolver`]):
|
||||
/// the session goes through a tunnel the client reaches on a loopback port, so
|
||||
/// everything after is a plain join.
|
||||
pub const FRIEND: &str = tunnel::PREFIX;
|
||||
|
||||
/// Whether `code` is an invite code; with `full`, only a full one (a short
|
||||
/// relay code could also be a host name).
|
||||
#[cfg(feature = "friends")]
|
||||
/// relay code could also be a host name). Without a resolver nothing is one.
|
||||
pub fn is_invite(code: &str, full: bool) -> bool {
|
||||
use horizon_friends::invite::{Parsed, parse};
|
||||
match parse(code) {
|
||||
Some(Parsed::Long(_)) => true,
|
||||
Some(Parsed::Short(_)) => !full,
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
/// Without the `friends` feature nothing is one.
|
||||
#[cfg(not(feature = "friends"))]
|
||||
pub fn is_invite(_code: &str, _full: bool) -> bool {
|
||||
false
|
||||
horizon_hooks::engine::tunnel().is_some_and(|r| r.is_code(code, full))
|
||||
}
|
||||
|
||||
/// An address as players and logs see it: a friend's invite code stays
|
||||
/// out of it (its secret is in there).
|
||||
pub fn shown_address(address: &str) -> String {
|
||||
match address.strip_prefix(FRIEND) {
|
||||
#[cfg(feature = "friends")]
|
||||
Some(code) => match horizon_friends::invite::parse(code) {
|
||||
Some(horizon_friends::invite::Parsed::Short(c)) => {
|
||||
format!("Friend's world ({}-****)", c.nameplate)
|
||||
}
|
||||
_ => "Friend's world".to_owned(),
|
||||
Some(code) => match horizon_hooks::engine::tunnel().and_then(|r| r.nameplate(code)) {
|
||||
Some(nameplate) => format!("Friend's world ({nameplate}-****)"),
|
||||
None => "Friend's world".to_owned(),
|
||||
},
|
||||
#[cfg(not(feature = "friends"))]
|
||||
Some(_) => "Friend's world".to_owned(),
|
||||
None => address.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The relay the player set (`friendsRelay` in horizon.txt, beside
|
||||
/// options.txt), else `HORIZON_RELAY_URL`. None by default.
|
||||
/// options.txt), else the resolver's (`HORIZON_RELAY_URL`). None by default.
|
||||
pub fn friends_relay_setting() -> Option<String> {
|
||||
let dir = std::env::current_dir().ok()?;
|
||||
crate::ui_theme::preference(&dir, "friendsRelay")
|
||||
.filter(|r| !r.is_empty())
|
||||
.or_else(relay_from_env)
|
||||
}
|
||||
#[cfg(feature = "friends")]
|
||||
use horizon_friends::relay_url as relay_from_env;
|
||||
/// `HORIZON_RELAY_URL` is the friends feature's: none without it.
|
||||
#[cfg(not(feature = "friends"))]
|
||||
fn relay_from_env() -> Option<String> {
|
||||
None
|
||||
.or_else(|| horizon_hooks::engine::tunnel().and_then(|r| r.relay_from_env()))
|
||||
}
|
||||
/// Remember the relay typed on Join a friend (empty forgets it).
|
||||
pub fn save_friends_relay(relay: &str) {
|
||||
@@ -562,55 +538,35 @@ pub fn save_friends_relay(relay: &str) {
|
||||
|
||||
/// Reach the friend's world from the code (directly, or through a relay)
|
||||
/// and open the tunnel: the loopback address to connect to, and the token to send on it first.
|
||||
#[cfg(feature = "friends")]
|
||||
/// Without a resolver (a build without Play with Friends) the address is refused, never retried.
|
||||
fn join_friend(
|
||||
code: &str,
|
||||
lang: &Language,
|
||||
send: &Sender<Event>,
|
||||
) -> Result<(String, u16, Vec<u8>), Reason> {
|
||||
use horizon_friends::{
|
||||
PathMode,
|
||||
guest::{self, GuestConfig},
|
||||
let Some(resolver) = horizon_hooks::engine::tunnel() else {
|
||||
return Err(Reason::keyed_arg(
|
||||
"horizon.multiplayer.invalid_address",
|
||||
lang,
|
||||
&shown_address(&format!("{FRIEND}{code}")),
|
||||
));
|
||||
};
|
||||
status(send, &tr(lang, "horizon.multiplayer.finding_friend"));
|
||||
let tunnel = guest::join(&GuestConfig {
|
||||
code: code.to_owned(),
|
||||
relay: friends_relay_setting(),
|
||||
protocol: PROTOCOL_VERSION,
|
||||
game: horizon_protocol::GAME_VERSION.to_owned(),
|
||||
path: PathMode::from_env(),
|
||||
})
|
||||
.map_err(|e| Reason::plain(e.to_string()))?;
|
||||
status(
|
||||
send,
|
||||
&if tunnel.direct {
|
||||
tr(lang, "horizon.multiplayer.connected_friend_direct")
|
||||
} else {
|
||||
tr(lang, "horizon.multiplayer.connected_friend_relay")
|
||||
},
|
||||
);
|
||||
let loopback = tunnel
|
||||
.serve_loopback()
|
||||
.map_err(|e| Reason::plain(e.to_string()))?;
|
||||
Ok((
|
||||
"127.0.0.1".to_owned(),
|
||||
loopback.port,
|
||||
loopback.token.to_vec(),
|
||||
))
|
||||
}
|
||||
|
||||
/// A build without Play with Friends: the address is refused, never retried.
|
||||
#[cfg(not(feature = "friends"))]
|
||||
fn join_friend(
|
||||
code: &str,
|
||||
lang: &Language,
|
||||
_: &Sender<Event>,
|
||||
) -> Result<(String, u16, Vec<u8>), Reason> {
|
||||
Err(Reason::keyed_arg(
|
||||
"horizon.multiplayer.invalid_address",
|
||||
lang,
|
||||
&shown_address(&format!("{FRIEND}{code}")),
|
||||
))
|
||||
let mut progress = |p: tunnel::Progress| {
|
||||
let key = match p {
|
||||
tunnel::Progress::Finding => "horizon.multiplayer.finding_friend",
|
||||
tunnel::Progress::Connected { direct: true } => {
|
||||
"horizon.multiplayer.connected_friend_direct"
|
||||
}
|
||||
tunnel::Progress::Connected { direct: false } => {
|
||||
"horizon.multiplayer.connected_friend_relay"
|
||||
}
|
||||
};
|
||||
status(send, &tr(lang, key));
|
||||
};
|
||||
let opened = resolver
|
||||
.open(code, friends_relay_setting(), &mut progress)
|
||||
.map_err(Reason::plain)?;
|
||||
Ok(("127.0.0.1".to_owned(), opened.port, opened.token))
|
||||
}
|
||||
|
||||
fn status(send: &Sender<Event>, text: &str) {
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
//! Horizon's own entries in the multiplayer list (docs/layers.md: Horizon layer): the official
|
||||
//! server, added once at the top, and the withdrawn partner server, taken back out once. Behind
|
||||
//! `horizon-extras` (docs/forking.md), so a plain build adds no server and holds no Horizon
|
||||
//! address. It reads and writes servers.dat only through `horizon_protocol::servers`, ready to
|
||||
//! leave the engine crates for a Horizon crate.
|
||||
use horizon_protocol::servers::{self, ServerEntry};
|
||||
use std::path::Path;
|
||||
|
||||
/// At startup of a player's game: the official server into the list in `game_dir` and the
|
||||
/// withdrawn partner server out of it, a failure only logged.
|
||||
pub fn update(game_dir: &Path) {
|
||||
if let Err(e) = add_official_server(game_dir) {
|
||||
log::warn!("could not add the official server to servers.dat: {e}");
|
||||
}
|
||||
if let Err(e) = withdraw_partner_servers(game_dir) {
|
||||
log::warn!("could not take the withdrawn partner server out of servers.dat: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Horizon's official survival server: a name and an address like any entry. The address is
|
||||
/// `horizon_telemetry::server_status::OFFICIAL_ADDRESS`, the one place it is kept.
|
||||
pub const OFFICIAL_SERVER: (&str, &str) = (
|
||||
"Horizon Official",
|
||||
horizon_telemetry::server_status::OFFICIAL_ADDRESS,
|
||||
);
|
||||
/// Beside servers.dat once the official server was offered, so a player who
|
||||
/// deletes it never gets it back: the address offered, so a new one moves the
|
||||
/// list once. The launcher writes the same file.
|
||||
const OFFICIAL_MARKER: &str = "horizon_official_server.txt";
|
||||
|
||||
/// An address as the game connects to it: any case, 25565 when no port is given.
|
||||
fn address_key(ip: &str) -> Option<(String, u16)> {
|
||||
servers::parse_address(ip).map(|(h, p)| (h.to_ascii_lowercase(), p))
|
||||
}
|
||||
|
||||
/// Puts the official server at the top of the list in `game_dir`, once: not
|
||||
/// again after the player deletes it, and not when the list has its address.
|
||||
/// When it moved (the marker names another address), the entries for its
|
||||
/// earlier addresses move to the new one, once (`move_official`).
|
||||
pub fn add_official_server(game_dir: &Path) -> std::io::Result<()> {
|
||||
let (name, address) = OFFICIAL_SERVER;
|
||||
let marker = game_dir.join(OFFICIAL_MARKER);
|
||||
// A marker that is there but can't be read: offered already, at an address not known.
|
||||
let offered = match std::fs::read_to_string(&marker) {
|
||||
Ok(text) => Some(text),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
|
||||
Err(_) => Some(String::new()),
|
||||
};
|
||||
if offered
|
||||
.as_deref()
|
||||
.is_some_and(|a| address_key(a) == address_key(address))
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
let file = game_dir.join("servers.dat");
|
||||
let _guard = servers::lock();
|
||||
let mut list = servers::load(&file);
|
||||
let previous = horizon_telemetry::server_status::OFFICIAL_PREVIOUS;
|
||||
if move_official(&mut list, address, previous) {
|
||||
servers::save(&file, &list)?;
|
||||
}
|
||||
if offered.is_none()
|
||||
&& !list
|
||||
.iter()
|
||||
.any(|s| address_key(&s.ip) == address_key(address))
|
||||
{
|
||||
servers::prepend(&file, &ServerEntry::new(name, address))?;
|
||||
}
|
||||
std::fs::write(marker, format!("{address}\n"))
|
||||
}
|
||||
|
||||
/// Afri-Network, which the game added once below the official server (#9299) until the
|
||||
/// partnership ended: the name and address the game gave its entry.
|
||||
const WITHDRAWN_PARTNER: (&str, &str) = ("Afri-Network (South Africa)", "afri-network.co.za");
|
||||
/// Beside servers.dat: the partner addresses the game offered, one per line (#9299).
|
||||
const PARTNER_MARKER: &str = "horizon_partner_servers.txt";
|
||||
|
||||
/// Takes the withdrawn partner server back out of the list in `game_dir`, once, where the game
|
||||
/// added it: the marker says it was offered and the entry still has the name the game gave it
|
||||
/// (an entry the player made or renamed keeps its place). Then the marker goes.
|
||||
pub fn withdraw_partner_servers(game_dir: &Path) -> std::io::Result<()> {
|
||||
let marker = game_dir.join(PARTNER_MARKER);
|
||||
let offered = match std::fs::read_to_string(&marker) {
|
||||
Ok(text) => text,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let (name, address) = WITHDRAWN_PARTNER;
|
||||
if offered
|
||||
.lines()
|
||||
.any(|l| address_key(l) == address_key(address))
|
||||
{
|
||||
let _guard = servers::lock();
|
||||
servers::remove_where(&game_dir.join("servers.dat"), |n, ip| {
|
||||
n == name && address_key(ip) == address_key(address)
|
||||
})?;
|
||||
}
|
||||
std::fs::remove_file(marker)
|
||||
}
|
||||
|
||||
/// Points the entries for an earlier official address at `address`, each
|
||||
/// keeping the player's name and fields. One entry for the server at most: an
|
||||
/// old one goes when the list already has `address`. Whether anything changed.
|
||||
fn move_official(list: &mut Vec<ServerEntry>, address: &str, previous: &[&str]) -> bool {
|
||||
let new = address_key(address);
|
||||
let old = |ip: &str| {
|
||||
let ip = address_key(ip);
|
||||
ip.is_some() && ip != new && previous.iter().any(|p| address_key(p) == ip)
|
||||
};
|
||||
let mut present = list.iter().any(|s| address_key(&s.ip) == new);
|
||||
let mut changed = false;
|
||||
list.retain_mut(|s| {
|
||||
if !old(&s.ip) {
|
||||
return true;
|
||||
}
|
||||
changed = true;
|
||||
if present {
|
||||
return false;
|
||||
}
|
||||
present = true;
|
||||
s.ip = address.to_owned();
|
||||
true
|
||||
});
|
||||
changed
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_official_server_is_added_once_at_the_top() {
|
||||
let dir = std::env::temp_dir().join(format!("horizon-mp-official-{}", std::process::id()));
|
||||
let (name, address) = OFFICIAL_SERVER;
|
||||
let file = dir.join("servers.dat");
|
||||
let ips = || -> Vec<String> { servers::load(&file).into_iter().map(|s| s.ip).collect() };
|
||||
let fresh = |list: &[ServerEntry]| {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
if !list.is_empty() {
|
||||
servers::save(&file, list).unwrap();
|
||||
}
|
||||
};
|
||||
// A fresh list: just the official server, named like it.
|
||||
fresh(&[]);
|
||||
add_official_server(&dir).unwrap();
|
||||
assert_eq!(servers::load(&file), [ServerEntry::new(name, address)]);
|
||||
// An existing list: on top, the player's servers kept in their order.
|
||||
let mine = [
|
||||
ServerEntry::new("Mine", "127.0.0.1:2"),
|
||||
ServerEntry::new("Other", "example.org"),
|
||||
];
|
||||
fresh(&mine);
|
||||
add_official_server(&dir).unwrap();
|
||||
assert_eq!(ips(), [address, "127.0.0.1:2", "example.org"]);
|
||||
// Once only: a second start adds nothing.
|
||||
add_official_server(&dir).unwrap();
|
||||
assert_eq!(ips(), [address, "127.0.0.1:2", "example.org"]);
|
||||
// Already there under the player's own name and spelling: not duplicated.
|
||||
fresh(&[
|
||||
ServerEntry::new("SMP", "Play.PlayHorizon.dev:25565"),
|
||||
mine[0].clone(),
|
||||
]);
|
||||
add_official_server(&dir).unwrap();
|
||||
assert_eq!(ips(), ["Play.PlayHorizon.dev:25565", "127.0.0.1:2"]);
|
||||
// Deleted by the player: never back.
|
||||
fresh(&mine);
|
||||
add_official_server(&dir).unwrap();
|
||||
servers::save(&file, &mine).unwrap();
|
||||
add_official_server(&dir).unwrap();
|
||||
assert_eq!(ips(), ["127.0.0.1:2", "example.org"]);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_withdrawn_partner_server_leaves_lists_the_game_put_it_in_once() {
|
||||
let dir = std::env::temp_dir().join(format!("horizon-mp-partner-{}", std::process::id()));
|
||||
let official = OFFICIAL_SERVER.1;
|
||||
let file = dir.join("servers.dat");
|
||||
let marker = dir.join(PARTNER_MARKER);
|
||||
let ips = || -> Vec<String> { servers::load(&file).into_iter().map(|s| s.ip).collect() };
|
||||
let fresh = |list: &[ServerEntry], offered: bool| {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
servers::save(&file, list).unwrap();
|
||||
if offered {
|
||||
std::fs::write(&marker, "afri-network.co.za\n").unwrap();
|
||||
}
|
||||
};
|
||||
let added = ServerEntry::new("Afri-Network (South Africa)", "afri-network.co.za");
|
||||
// The game added it: it goes, the rest stays in order, and only once.
|
||||
fresh(
|
||||
&[
|
||||
ServerEntry::new("Horizon Official", official),
|
||||
added.clone(),
|
||||
ServerEntry::new("Mine", "127.0.0.1:2"),
|
||||
],
|
||||
true,
|
||||
);
|
||||
withdraw_partner_servers(&dir).unwrap();
|
||||
assert_eq!(ips(), [official, "127.0.0.1:2"]);
|
||||
assert!(!marker.exists());
|
||||
servers::save(&file, std::slice::from_ref(&added)).unwrap();
|
||||
withdraw_partner_servers(&dir).unwrap();
|
||||
assert_eq!(ips(), ["afri-network.co.za"]);
|
||||
// The player's own entry for that address (their name) stays.
|
||||
fresh(&[ServerEntry::new("ZA", "Afri-Network.co.za:25565")], true);
|
||||
withdraw_partner_servers(&dir).unwrap();
|
||||
assert_eq!(ips(), ["Afri-Network.co.za:25565"]);
|
||||
// Never offered (no marker): nothing changes.
|
||||
fresh(&[added], false);
|
||||
withdraw_partner_servers(&dir).unwrap();
|
||||
assert_eq!(ips(), ["afri-network.co.za"]);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_moved_official_server_moves_the_old_entry_once() {
|
||||
let dir = std::env::temp_dir().join(format!("horizon-mp-moved-{}", std::process::id()));
|
||||
let address = OFFICIAL_SERVER.1;
|
||||
let file = dir.join("servers.dat");
|
||||
let marker = dir.join(OFFICIAL_MARKER);
|
||||
let ips = || -> Vec<String> { servers::load(&file).into_iter().map(|s| s.ip).collect() };
|
||||
let offered_before = |list: &[ServerEntry]| {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
servers::save(&file, list).unwrap();
|
||||
std::fs::write(&marker, "horizon.bunchy7s.dev\n").unwrap();
|
||||
};
|
||||
// The old address under the player's own name and spelling: moved, name and place kept.
|
||||
let mut old = ServerEntry::new("Our SMP", "Horizon.Bunchy7s.dev:25565");
|
||||
old.icon = Some("AAAA".into());
|
||||
offered_before(&[ServerEntry::new("Mine", "127.0.0.1:2"), old]);
|
||||
add_official_server(&dir).unwrap();
|
||||
let list = servers::load(&file);
|
||||
assert_eq!(ips(), ["127.0.0.1:2", address]);
|
||||
assert_eq!(
|
||||
(list[1].name.as_str(), list[1].icon.as_deref()),
|
||||
("Our SMP", Some("AAAA"))
|
||||
);
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(&marker).unwrap(),
|
||||
format!("{address}\n")
|
||||
);
|
||||
// Once: the old address added back afterwards is the player's.
|
||||
servers::prepend(&file, &ServerEntry::new("Old", "horizon.bunchy7s.dev")).unwrap();
|
||||
add_official_server(&dir).unwrap();
|
||||
assert_eq!(ips(), ["horizon.bunchy7s.dev", "127.0.0.1:2", address]);
|
||||
// The new address already listed: no second entry.
|
||||
offered_before(&[
|
||||
ServerEntry::new("Old", "horizon.bunchy7s.dev"),
|
||||
ServerEntry::new("New", address),
|
||||
]);
|
||||
add_official_server(&dir).unwrap();
|
||||
assert_eq!(ips(), [address]);
|
||||
// Deleted by the player before the move: not offered again; another port is left alone.
|
||||
offered_before(&[ServerEntry::new("Test", "horizon.bunchy7s.dev:25570")]);
|
||||
add_official_server(&dir).unwrap();
|
||||
assert_eq!(ips(), ["horizon.bunchy7s.dev:25570"]);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
//! A play session as the hooks above the engine hear it (`horizon_hooks`): the title screen's
|
||||
//! first frame, a world opening and its first frame, the frames played, and leaving the world.
|
||||
//! It also marks those moments in the startup trace (`spans::startup`). Opt-in telemetry's
|
||||
//! performance summary is built from these hooks (docs/telemetry.md).
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::time::Instant;
|
||||
|
||||
use horizon_hooks::{Frame, Milestone, WorldLeft, engine};
|
||||
|
||||
use crate::spans::{self, startup};
|
||||
|
||||
/// One window's session: moved from the startup menu to the game with the window.
|
||||
#[derive(Default)]
|
||||
pub struct Session {
|
||||
/// When the world being opened started to, until its first frame.
|
||||
opening: Option<Instant>,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
/// The title screen drew a frame: the first time, how long the start took.
|
||||
pub fn title_shown(&mut self) {
|
||||
static SHOWN: AtomicBool = AtomicBool::new(false);
|
||||
if !SHOWN.swap(true, Ordering::Relaxed) {
|
||||
startup::mark("title.shown");
|
||||
// The frame itself: the menu bundle can be ready seconds before the renderer that
|
||||
// draws it (pipelines compiling on a slow driver).
|
||||
engine::startup(Milestone::TitleShown, startup::since_start());
|
||||
}
|
||||
}
|
||||
|
||||
/// A world starts loading.
|
||||
pub fn world_opening(&mut self) {
|
||||
if self.opening.is_none() {
|
||||
startup::arm("world.opening");
|
||||
}
|
||||
self.opening.get_or_insert_with(Instant::now);
|
||||
}
|
||||
|
||||
/// An entry queued during startup was cancelled before opening a world.
|
||||
pub fn cancel_world_opening(&mut self) {
|
||||
self.opening = None;
|
||||
}
|
||||
|
||||
/// A frame drawn while playing (not in menus or loading), with the spans the render thread
|
||||
/// recorded in it ([`spans::take_raw`]): the step it spent most of its time in is
|
||||
/// [`spans::culprit`].
|
||||
pub fn frame(&mut self, ms: f32, frame_spans: &[spans::Span]) {
|
||||
if let Some(opening) = self.opening.take() {
|
||||
startup::mark("world.first_frame");
|
||||
engine::world_shown(opening.elapsed());
|
||||
}
|
||||
engine::frame(&Frame::new(ms, &|| spans::culprit(frame_spans)));
|
||||
}
|
||||
|
||||
/// The player left the world: the session ends.
|
||||
pub fn finish(&mut self, left: &WorldLeft<'_>) {
|
||||
engine::world_left(left);
|
||||
}
|
||||
}
|
||||
|
||||
/// The resources the menus need are loaded.
|
||||
pub fn core_ready() {
|
||||
engine::startup(Milestone::CoreReady, startup::since_start());
|
||||
}
|
||||
|
||||
/// What a world needs is loaded too.
|
||||
pub fn rest_ready() {
|
||||
engine::startup(Milestone::RestReady, startup::since_start());
|
||||
}
|
||||
@@ -232,10 +232,7 @@ pub fn roll_container(
|
||||
|
||||
/// A saved `LootTableSeed`, if any.
|
||||
pub fn loot_seed(nbt: &Tag) -> Option<i64> {
|
||||
match nbt.get("LootTableSeed") {
|
||||
Some(Tag::Long(seed)) => Some(*seed),
|
||||
_ => None,
|
||||
}
|
||||
horizon_client_core::nbt_value::number(nbt.get("LootTableSeed"))
|
||||
}
|
||||
|
||||
fn text(nbt: &Compound, key: &str) -> Option<String> {
|
||||
@@ -318,6 +315,18 @@ mod tests {
|
||||
use super::*;
|
||||
use horizon_world::loot::{ExplorationMap, LootStack};
|
||||
|
||||
#[test]
|
||||
fn a_loot_seed_is_any_number() {
|
||||
assert_eq!(
|
||||
loot_seed(&Tag::compound([("LootTableSeed", Tag::Int(42))])),
|
||||
Some(42)
|
||||
);
|
||||
assert_eq!(
|
||||
loot_seed(&Tag::compound([("LootTableSeed", Tag::Long(7))])),
|
||||
Some(7)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_saved_mod_sign_uses_its_vanilla_entity_type() {
|
||||
for hanging in [false, true] {
|
||||
|
||||
@@ -3,10 +3,16 @@
|
||||
//! summary, in the outbox. It never sends those itself: the launcher does, at its next start (the
|
||||
//! game's only sending is the anonymous "I'm playing" ping, `heartbeat.rs`).
|
||||
//! Without the player's yes for that kind nothing is kept, and crash-reports/ stays local.
|
||||
//!
|
||||
//! The play session's summary is built from the game's hooks ([`HOOKS`], `horizon_hooks`), as a
|
||||
//! Horizon crate does: it leaves the engine for the Horizon layer when the official executable
|
||||
//! ships (layer plan M5, docs/layers.md).
|
||||
use std::path::Path;
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
use std::time::Instant;
|
||||
use std::sync::{LazyLock, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
use horizon_hooks::{Frame, Hooks, Milestone, WorldLeft};
|
||||
|
||||
use horizon_telemetry::payload::{Crash, Display, Perf, Settings, System, Timings};
|
||||
use horizon_telemetry::scrub::Scrubber;
|
||||
@@ -19,32 +25,70 @@ const MIN_SECONDS: f32 = 60.0;
|
||||
/// world, the server or the resource packs.
|
||||
const CRASH_DETAILS: [&str; 3] = ["Horizon Version", "Graphics Adapter", "Render Settings"];
|
||||
|
||||
static STARTED: OnceLock<Instant> = OnceLock::new();
|
||||
static CORE_READY_MS: AtomicU32 = AtomicU32::new(u32::MAX);
|
||||
static REST_READY_MS: AtomicU32 = AtomicU32::new(u32::MAX);
|
||||
/// The screen the window opened on.
|
||||
static DISPLAY: Mutex<Option<Display>> = Mutex::new(None);
|
||||
|
||||
/// When the game started, for the time to the title screen. Call first thing in `main`.
|
||||
pub fn mark_start() {
|
||||
STARTED.get_or_init(Instant::now);
|
||||
/// The game's hooks, for `horizon_hooks::register`.
|
||||
pub static HOOKS: Telemetry = Telemetry {
|
||||
session: LazyLock::new(|| Mutex::new(Session::default())),
|
||||
};
|
||||
|
||||
/// The play session's performance, as the game's hooks tell it.
|
||||
pub struct Telemetry {
|
||||
session: LazyLock<Mutex<Session>>,
|
||||
}
|
||||
|
||||
pub fn mark_core_ready() {
|
||||
mark_ready(&CORE_READY_MS);
|
||||
}
|
||||
|
||||
pub fn mark_rest_ready() {
|
||||
mark_ready(&REST_READY_MS);
|
||||
}
|
||||
|
||||
fn mark_ready(mark: &AtomicU32) {
|
||||
if let Some(started) = STARTED.get() {
|
||||
let ms = started.elapsed().as_millis().min(u128::from(u32::MAX - 1)) as u32;
|
||||
let _ = mark.compare_exchange(u32::MAX, ms, Ordering::Relaxed, Ordering::Relaxed);
|
||||
impl Telemetry {
|
||||
fn session(&self) -> std::sync::MutexGuard<'_, Session> {
|
||||
self.session.lock().unwrap_or_else(|e| e.into_inner())
|
||||
}
|
||||
}
|
||||
|
||||
impl Hooks for Telemetry {
|
||||
fn startup(&self, milestone: Milestone, since_start: Duration) {
|
||||
match milestone {
|
||||
Milestone::CoreReady => mark_ready(&CORE_READY_MS, since_start),
|
||||
Milestone::RestReady => mark_ready(&REST_READY_MS, since_start),
|
||||
Milestone::TitleShown => self.session().title_shown(since_start),
|
||||
}
|
||||
}
|
||||
|
||||
/// A player who allows performance statistics also says what their slow frames spent their
|
||||
/// time in.
|
||||
fn wants_frame_steps(&self, home: &Path) -> bool {
|
||||
horizon_telemetry::Consent::load(home).allows(horizon_telemetry::Kind::Perf)
|
||||
}
|
||||
|
||||
fn world_shown(&self, since_opening: Duration) {
|
||||
self.session().world_shown(since_opening);
|
||||
}
|
||||
|
||||
fn frame(&self, frame: &Frame<'_>) {
|
||||
self.session().frame(frame.ms, || frame.slowest_step());
|
||||
}
|
||||
|
||||
fn world_left(&self, left: &WorldLeft<'_>) {
|
||||
let settings = Settings {
|
||||
render_distance: left.render_distance.clamp(0, 255) as u8,
|
||||
simulation_distance: left.simulation_distance.clamp(0, 255) as u8,
|
||||
max_fps: left
|
||||
.max_fps
|
||||
.map_or(0, |f| f.min(u32::from(u16::MAX)) as u16),
|
||||
vsync: left.vsync,
|
||||
fullscreen: left.fullscreen,
|
||||
graphics: left.graphics.to_owned(),
|
||||
};
|
||||
self.session().finish(left.home, settings);
|
||||
}
|
||||
}
|
||||
|
||||
fn mark_ready(mark: &AtomicU32, since_start: Duration) {
|
||||
let ms = since_start.as_millis().min(u128::from(u32::MAX - 1)) as u32;
|
||||
let _ = mark.compare_exchange(u32::MAX, ms, Ordering::Relaxed, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
fn readiness_ms(mark: &AtomicU32) -> Option<u32> {
|
||||
let ms = mark.load(Ordering::Relaxed);
|
||||
(ms != u32::MAX).then_some(ms)
|
||||
@@ -60,28 +104,10 @@ pub fn set_display(width: u32, height: u32, refresh_hz: u32, scale: f64) {
|
||||
});
|
||||
}
|
||||
|
||||
/// The screen `window` is on, else the primary one, else the only one: right after the window
|
||||
/// is made, Wayland doesn't say yet which screen it is on.
|
||||
pub fn monitor(window: &winit::window::Window) -> Option<winit::monitor::MonitorHandle> {
|
||||
let only = || {
|
||||
let mut all = window.available_monitors();
|
||||
all.next().filter(|_| all.next().is_none())
|
||||
};
|
||||
window
|
||||
.current_monitor()
|
||||
.or_else(|| window.primary_monitor())
|
||||
.or_else(only)
|
||||
}
|
||||
|
||||
/// Keeps [`monitor`]'s size, refresh rate and scale for the items' `system.display`.
|
||||
/// Keeps the screen `window` is on ([`crate::window::display`]) for the items' `system.display`.
|
||||
pub fn note_display(window: &winit::window::Window) {
|
||||
if let Some(m) = monitor(window) {
|
||||
set_display(
|
||||
m.size().width,
|
||||
m.size().height,
|
||||
m.refresh_rate_millihertz().map_or(0, |r| r.div_ceil(1000)),
|
||||
m.scale_factor(),
|
||||
);
|
||||
if let Some(d) = crate::window::display(window) {
|
||||
set_display(d.width, d.height, d.refresh_hz.unwrap_or(0), d.scale);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,42 +220,33 @@ pub struct Session {
|
||||
/// The start was measured: only the first session carries it.
|
||||
title_measured: bool,
|
||||
to_world_ms: Option<u32>,
|
||||
opening: Option<Instant>,
|
||||
slow_frames: Vec<horizon_telemetry::payload::SlowFrames>,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
/// The title screen drew a frame: the first time, how long the start took.
|
||||
pub fn title_shown(&mut self) {
|
||||
/// The title screen drew its first frame, `since_start` after the game started.
|
||||
pub fn title_shown(&mut self, since_start: Duration) {
|
||||
if !self.title_measured {
|
||||
self.title_measured = true;
|
||||
crate::spans::startup::mark("title.shown");
|
||||
// The frame itself: the menu bundle can be ready seconds before
|
||||
// the renderer that draws it (pipelines compiling on a slow driver).
|
||||
self.to_title_ms = STARTED.get().map(|s| s.elapsed().as_millis() as u32);
|
||||
self.to_title_ms = Some(since_start.as_millis().min(u128::from(u32::MAX)) as u32);
|
||||
}
|
||||
}
|
||||
|
||||
/// A world starts loading.
|
||||
pub fn world_opening(&mut self) {
|
||||
if self.opening.is_none() {
|
||||
crate::spans::startup::arm("world.opening");
|
||||
/// A world's first frame, `since_opening` after it started opening: the session's first
|
||||
/// world counts.
|
||||
pub fn world_shown(&mut self, since_opening: Duration) {
|
||||
if self.to_world_ms.is_none() {
|
||||
self.to_world_ms = Some(since_opening.as_millis().min(u128::from(u32::MAX)) as u32);
|
||||
}
|
||||
self.opening.get_or_insert_with(Instant::now);
|
||||
}
|
||||
|
||||
/// An entry queued during startup was cancelled before opening a world.
|
||||
pub fn cancel_world_opening(&mut self) {
|
||||
self.opening = None;
|
||||
}
|
||||
|
||||
/// A frame drawn while playing (not in menus or loading), with the spans the render thread
|
||||
/// recorded in it ([`crate::spans::take_raw`]): a frame of `SLOW_FRAME_MS` or more counts
|
||||
/// under the step it spent most of its time in ([`crate::spans::culprit`]).
|
||||
pub fn frame(&mut self, ms: f32, spans: &[crate::spans::Span]) {
|
||||
/// A frame drawn while playing (not in menus or loading): a frame of `SLOW_FRAME_MS` or
|
||||
/// more counts under the step it spent most of its time in (`slowest_step`, the game's
|
||||
/// `spans::culprit`).
|
||||
pub fn frame(&mut self, ms: f32, slowest_step: impl FnOnce() -> Option<(String, f32)>) {
|
||||
use horizon_telemetry::payload::{SLOW_FRAME_MS, SlowFrames};
|
||||
if ms >= SLOW_FRAME_MS {
|
||||
let (mut path, span_ms) = crate::spans::culprit(spans).unwrap_or_default();
|
||||
let (mut path, span_ms) = slowest_step().unwrap_or_default();
|
||||
// A path too long for the item keeps its last steps.
|
||||
while path.len() > SlowFrames::MAX_NAME
|
||||
&& let Some((_, rest)) = path.split_once('/')
|
||||
@@ -238,12 +255,6 @@ impl Session {
|
||||
}
|
||||
SlowFrames::record(&mut self.slow_frames, ms, &path, span_ms);
|
||||
}
|
||||
if let Some(opening) = self.opening.take() {
|
||||
crate::spans::startup::mark("world.first_frame");
|
||||
if self.to_world_ms.is_none() {
|
||||
self.to_world_ms = Some(opening.elapsed().as_millis() as u32);
|
||||
}
|
||||
}
|
||||
self.frames.record(ms);
|
||||
self.played_ms += ms;
|
||||
}
|
||||
@@ -471,25 +482,30 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancelled_startup_entry_does_not_time_menu_browsing() {
|
||||
fn the_time_to_a_world_is_the_sessions_first() {
|
||||
let mut session = Session::default();
|
||||
session.world_opening();
|
||||
assert!(session.opening.is_some());
|
||||
session.cancel_world_opening();
|
||||
assert!(session.opening.is_none());
|
||||
session.frame(16.0, &[]);
|
||||
assert!(session.to_world_ms.is_none());
|
||||
session.world_opening();
|
||||
session.frame(16.0, &[]);
|
||||
assert!(session.to_world_ms.is_some());
|
||||
session.world_shown(Duration::from_millis(4200));
|
||||
session.world_shown(Duration::from_millis(900));
|
||||
assert_eq!(session.to_world_ms, Some(4200));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_time_to_the_title_goes_with_the_first_session_only() {
|
||||
let home = home("title");
|
||||
let mut session = Session::default();
|
||||
session.title_shown(Duration::from_millis(3100));
|
||||
assert_eq!(session.to_title_ms, Some(3100));
|
||||
session.finish(&home, Settings::default());
|
||||
session.title_shown(Duration::from_millis(9000));
|
||||
assert_eq!(session.to_title_ms, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_session_is_kept_only_with_consent_and_once_long_enough() {
|
||||
let home = home("session");
|
||||
let mut session = Session::default();
|
||||
session.world_opening();
|
||||
(0..2000).for_each(|_| session.frame(16.6, &[]));
|
||||
session.world_shown(Duration::from_millis(1500));
|
||||
(0..2000).for_each(|_| session.frame(16.6, || None));
|
||||
session.finish(&home, Settings::default());
|
||||
assert!(outbox::pending(&home).is_empty(), "33 s, and no consent");
|
||||
allow(
|
||||
@@ -499,11 +515,11 @@ mod tests {
|
||||
..Choices::NONE
|
||||
},
|
||||
);
|
||||
(0..2000).for_each(|_| session.frame(16.6, &[]));
|
||||
(0..2000).for_each(|_| session.frame(16.6, || None));
|
||||
session.finish(&home, Settings::default());
|
||||
assert!(outbox::pending(&home).is_empty(), "33 s is too short");
|
||||
session.world_opening();
|
||||
(0..4000).for_each(|_| session.frame(16.6, &[]));
|
||||
session.world_shown(Duration::from_millis(1500));
|
||||
(0..4000).for_each(|_| session.frame(16.6, || None));
|
||||
(0..1200).for_each(|_| session.tick(4.0));
|
||||
session.finish(
|
||||
&home,
|
||||
@@ -536,13 +552,15 @@ mod tests {
|
||||
cpu_ms: ms,
|
||||
depth,
|
||||
};
|
||||
let culprit = |spans: &[Span]| crate::spans::culprit(spans);
|
||||
let mut session = Session::default();
|
||||
(0..4000).for_each(|_| session.frame(16.6, &[span("tick", 10.0, 0)]));
|
||||
let ticking = [span("tick", 10.0, 0)];
|
||||
(0..4000).for_each(|_| session.frame(16.6, || culprit(&ticking)));
|
||||
let frozen = [span("r_uploads", 280.0, 1), span("renderer", 300.0, 0)];
|
||||
session.frame(320.0, &frozen);
|
||||
session.frame(900.0, &frozen);
|
||||
session.frame(260.0, &[]);
|
||||
session.frame(249.0, &frozen);
|
||||
session.frame(320.0, || culprit(&frozen));
|
||||
session.frame(900.0, || culprit(&frozen));
|
||||
session.frame(260.0, || culprit(&[]));
|
||||
session.frame(249.0, || culprit(&frozen));
|
||||
let perf = session.summary(Settings::default()).unwrap();
|
||||
let seen: Vec<_> = perf
|
||||
.slow_frames
|
||||
|
||||
@@ -8,6 +8,8 @@ description = "Play with friends: invite codes, the relay protocol, the end-to-e
|
||||
|
||||
[dependencies]
|
||||
horizon-addresses = { path = "../horizon-addresses" }
|
||||
# The game's side (`game`): the platform's hooks, for horizon-official.
|
||||
horizon-platform = { path = "../horizon-platform", optional = true }
|
||||
# The key exchange keyed by the code's secret, then the frames.
|
||||
spake2 = { version = "0.4", features = ["getrandom"] }
|
||||
chacha20poly1305 = "0.10"
|
||||
@@ -21,5 +23,10 @@ if-addrs = "0.15"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1.0.151"
|
||||
|
||||
[features]
|
||||
# Play with Friends in the game: the resolver of `friend:` addresses on the platform's hooks
|
||||
# (`game.rs`), which horizon-official registers. The launcher and the relay don't need it.
|
||||
game = ["dep:horizon-platform"]
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -0,0 +1,174 @@
|
||||
//! Play with Friends in the game, joining side (the `game` feature): the engine's `friend:`
|
||||
//! addresses (`horizon_platform::hooks::tunnel`) read as invite codes, and joined through this
|
||||
//! crate's tunnel. `horizon-official` registers [`HOOKS`]; the game's own executable has none, so
|
||||
//! there no invite code joins.
|
||||
use crate::{
|
||||
PathMode,
|
||||
guest::{self, GuestConfig},
|
||||
invite::{Parsed, parse},
|
||||
};
|
||||
use horizon_platform::{
|
||||
hooks::{
|
||||
Hooks,
|
||||
tunnel::{Opened, Progress, Resolver},
|
||||
},
|
||||
version::{PROTOCOL_VERSION, VANILLA_VERSION},
|
||||
};
|
||||
use std::net::SocketAddr;
|
||||
|
||||
/// What `horizon-official` registers: Play with Friends' hooks.
|
||||
pub static HOOKS: Friends = Friends;
|
||||
|
||||
pub struct Friends;
|
||||
|
||||
impl Hooks for Friends {
|
||||
fn tunnel(&self) -> Option<&dyn Resolver> {
|
||||
Some(&Invites)
|
||||
}
|
||||
}
|
||||
|
||||
struct Invites;
|
||||
|
||||
impl Resolver for Invites {
|
||||
fn is_code(&self, code: &str, full: bool) -> bool {
|
||||
match parse(code) {
|
||||
Some(Parsed::Long(_)) => true,
|
||||
Some(Parsed::Short(_)) => !full,
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn nameplate(&self, code: &str) -> Option<String> {
|
||||
match parse(code) {
|
||||
Some(Parsed::Short(c)) => Some(c.nameplate),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The relay is kept beside the addresses: two hosts on other networks can
|
||||
/// share a private address and the fixed direct port.
|
||||
fn public_part(&self, code: &str) -> String {
|
||||
match parse(code) {
|
||||
Some(Parsed::Short(code)) => code.nameplate,
|
||||
Some(Parsed::Long(invite)) => {
|
||||
let relay = invite
|
||||
.relay
|
||||
.map(|r| format!("{}/{}", r.address, r.code.nameplate));
|
||||
let endpoints = invite.endpoints.iter().map(SocketAddr::to_string);
|
||||
endpoints.chain(relay).collect::<Vec<_>>().join(",")
|
||||
}
|
||||
None => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn relay_from_env(&self) -> Option<String> {
|
||||
crate::relay_url()
|
||||
}
|
||||
|
||||
fn open(
|
||||
&self,
|
||||
code: &str,
|
||||
relay: Option<String>,
|
||||
progress: &mut dyn FnMut(Progress),
|
||||
) -> Result<Opened, String> {
|
||||
progress(Progress::Finding);
|
||||
let tunnel = guest::join(&GuestConfig {
|
||||
code: code.to_owned(),
|
||||
relay,
|
||||
protocol: PROTOCOL_VERSION,
|
||||
game: VANILLA_VERSION.to_owned(),
|
||||
path: PathMode::from_env(),
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
progress(Progress::Connected {
|
||||
direct: tunnel.direct,
|
||||
});
|
||||
let loopback = tunnel.serve_loopback().map_err(|e| e.to_string())?;
|
||||
Ok(Opened {
|
||||
port: loopback.port,
|
||||
token: loopback.token.to_vec(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Invites, Resolver};
|
||||
use crate::{
|
||||
code::Code,
|
||||
invite::{self, Invite},
|
||||
};
|
||||
use horizon_platform::hooks::tunnel;
|
||||
|
||||
fn short(nameplate: &str, secret: &str) -> Code {
|
||||
Code {
|
||||
nameplate: nameplate.into(),
|
||||
secret: secret.into(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_engine_and_the_codes_name_the_same_addresses() {
|
||||
assert_eq!(tunnel::PREFIX, crate::ADDRESS_PREFIX);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invites_are_read_and_their_secret_never_shown() {
|
||||
let invite = Invite {
|
||||
secret: *b"0123456789",
|
||||
port: 25580,
|
||||
endpoints: vec!["192.168.1.20:25580".parse().unwrap()],
|
||||
relay: None,
|
||||
}
|
||||
.encode();
|
||||
// Direct Connection takes a full invite; a short code needs Join a Friend.
|
||||
assert!(Invites.is_code(&invite, true));
|
||||
let short = short("K7QM", "3XPD").to_string();
|
||||
assert!(Invites.is_code(&short, false));
|
||||
assert!(!Invites.is_code(&short, true));
|
||||
// A short code could be a host name, so Direct Connection keeps host names.
|
||||
for host in ["localhost", "example.org:25566", "abcdefgh", "[::1]:25568"] {
|
||||
assert!(!Invites.is_code(host, true), "{host}");
|
||||
}
|
||||
// Only a short code's name may be shown; a full invite is all secret.
|
||||
assert_eq!(Invites.nameplate(&invite), None);
|
||||
assert_eq!(Invites.nameplate(&short).as_deref(), Some("K7QM"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn friends_worlds_are_told_apart_by_their_hosts_never_by_their_secrets() {
|
||||
const PORT: u16 = 50000;
|
||||
const RELAY: &str = "relay.example.com:443";
|
||||
let invite = |secret: u8, host: &str, relay: Option<Code>| {
|
||||
let code = Invite {
|
||||
secret: [secret; invite::SECRET],
|
||||
port: PORT,
|
||||
endpoints: vec![format!("{host}:{PORT}").parse().unwrap()],
|
||||
relay: relay.map(|code| invite::RelayRef {
|
||||
address: RELAY.into(),
|
||||
code,
|
||||
}),
|
||||
}
|
||||
.encode();
|
||||
Invites.public_part(&code)
|
||||
};
|
||||
assert_ne!(
|
||||
invite(1, "192.168.1.5", None),
|
||||
invite(1, "192.168.1.6", None),
|
||||
"two friends"
|
||||
);
|
||||
assert_ne!(
|
||||
invite(1, "192.168.1.5", Some(short("ABCD", "WXYZ"))),
|
||||
invite(1, "192.168.1.5", Some(short("EFGH", "WXYZ"))),
|
||||
"two friends on other networks behind the same private address"
|
||||
);
|
||||
assert_eq!(
|
||||
invite(1, "192.168.1.5", Some(short("ABCD", "WXYZ"))),
|
||||
invite(2, "192.168.1.5", Some(short("ABCD", "QRST"))),
|
||||
"the invite's and the relay code's secrets take no part"
|
||||
);
|
||||
let typed = |code: Code| Invites.public_part(&code.to_string());
|
||||
assert_eq!(typed(short("ABCD", "WXYZ")), typed(short("ABCD", "QRST")));
|
||||
assert_eq!(typed(short("ABCD", "WXYZ")), "ABCD");
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@
|
||||
pub mod code;
|
||||
pub mod crypto;
|
||||
pub mod direct;
|
||||
#[cfg(feature = "game")]
|
||||
pub mod game;
|
||||
pub mod guest;
|
||||
pub mod host;
|
||||
pub mod invite;
|
||||
|
||||
Loaded 100 of 148 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user