Merge remote-tracking branch 'origin/main' into perf/behaviour-cache-hash-once

This commit is contained in:
Ludovic Marie
2026-10-26 03:12:43 +01:00
133 changed files with 8871 additions and 790 deletions
+32 -2
View File
@@ -3,7 +3,8 @@ name: checks
# check main requires, so it never has a condition: a skipped job counts as passed, and every run of this workflow
# must report a real verify (a pull request that changes no Rust finishes it in seconds instead).
# parity: the vanilla harnesses a pull request's changes can affect, on the Mac mini's runner, which holds the
# maintainer's own Minecraft files (Horizon never downloads the game); .github/workflows/parity.yml. A fork's pull
# maintainer's own Minecraft files (Horizon never downloads the game); .github/workflows/parity.yml. Only for a pull
# request that changes a harness, oracle, probe or the runner: the rest are checked on main by parity-main. A fork's pull
# request runs it only when a maintainer adds the run-parity label (.github/workflows/run-parity.yml), so labels
# never start this workflow. See docs/release-flow.md.
on:
@@ -72,8 +73,37 @@ jobs:
run: |
cargo +stable test --workspace --lib --bins --tests --features horizon-client/mods,horizon-mods/pack
cargo +stable test --workspace --doc --features horizon-client/mods,horizon-mods/pack
parity:
# Most pull requests leave the harnesses to parity-main, which runs them on main a batch of merges at a time
# (.github/workflows/parity-main.yml; releases wait for it). A pull request still runs them itself when it changes
# a harness, an oracle, a probe or the runner, or carries the run-parity label (run-parity.yml, when it is added).
parity-scope:
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
outputs:
run: ${{ steps.scope.outputs.run }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
persist-credentials: false
- id: scope
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
LABELS: ${{ join(github.event.pull_request.labels.*.name, ' ') }}
run: |
changed=$(git diff --name-only "$(git merge-base "$BASE_SHA" HEAD)" HEAD)
harness=$(grep -E '^(tools/test_[a-z0-9_]+_parity\.py|tools/(parity[a-z_]*|lab)\.py|tools/parity_hosts\.json|mods/horizon/src/test/|crates/[^/]+/examples/|crates/horizon-client/src/game/[a-z_]*(fixture|harness)\.rs|\.github/workflows/parity[a-z-]*\.yml)' <<< "$changed" || true)
if [ -n "$harness" ] || [[ " $LABELS " == *" run-parity "* ]]; then
echo "run=true" >> "$GITHUB_OUTPUT"
echo "The pull request runs its harnesses: ${harness:-label run-parity}"
else
echo "run=false" >> "$GITHUB_OUTPUT"
echo "No harness, oracle, probe or runner change: parity-main checks this on main"
fi
parity:
needs: parity-scope
if: needs.parity-scope.outputs.run == 'true'
uses: ./.github/workflows/parity.yml
with:
base: ${{ github.event.pull_request.base.sha }}
+2 -1
View File
@@ -29,7 +29,8 @@ env:
jobs:
audit:
runs-on: [self-hosted, horizon-game]
# The Mac mini's runners only: the audit compares with the previous one, so it stays on one platform.
runs-on: [self-hosted, horizon-game, macOS]
timeout-minutes: 150
steps:
- name: 03:00 in Paris
+72
View File
@@ -0,0 +1,72 @@
name: parity-main
# The vanilla harnesses on main, a batch of merges at a time (docs/release-flow.md#parity-on-main), on the Mac mini's
# runner. One run at a time: merges that land while a run goes on wait in one pending run (GitHub keeps only the
# newest), which checks every merge since main's last green parity-main (tools/parity_main.py base). On a failure it
# bisects the failing harnesses over those merges and comments on the pull request that broke them. Releases ship
# only a commit whose parity-main passed (tools/release_train.py), so nothing a harness catches reaches players.
on:
push:
branches: [main]
workflow_dispatch:
permissions:
contents: read
# The comment on the merge that broke a harness.
pull-requests: write
# tools/parity_main.py base reads earlier runs.
actions: read
concurrency:
group: parity-main
cancel-in-progress: false
env:
# Test runs never reach the telemetry server: no live-count ping, no uploads (docs/telemetry.md).
HORIZON_TELEMETRY_URL: "off"
jobs:
parity-main:
# The Mac mini's runners only: a desktop job slowed down because the maintainer came back could time out, and a
# failure here bisects and comments on merges.
runs-on: [self-hosted, horizon-game, macOS]
timeout-minutes: 240 # a core-crate batch reruns ~300 harnesses (~60-90 min), a bisect a few rounds of the failures
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.sha }}
fetch-depth: 0
persist-credentials: false
# Keeps .local/ (parity state, probe builds, oracle results) and target/ between runs.
clean: false
- uses: dtolnay/rust-toolchain@stable
- name: Tools from the runner
run: bash tools/ci/runner-env.sh
- name: Vanilla data, and resources prepared by this preparer
run: bash tools/ci/vanilla-data.sh
- name: Main's last green parity-main
id: base
env:
GH_TOKEN: ${{ github.token }}
run: echo "sha=$(python3 tools/parity_main.py base)" >> "$GITHUB_OUTPUT"
- name: The harnesses the merges since then can affect
env:
PARITY_HOSTS: ''
BASE_SHA: ${{ steps.base.outputs.sha }}
run: |
echo "Merges since ${BASE_SHA}:"
git log --first-parent --format=' %h %s' "$BASE_SHA..HEAD"
python3 tools/parity.py --changed "$BASE_SHA" --timeout 2400
- name: The merge that broke them
if: failure()
env:
GH_TOKEN: ${{ github.token }}
PARITY_HOSTS: ''
BASE_SHA: ${{ steps.base.outputs.sha }}
run: |
rm -rf .local/parity-main-logs && cp -R .local/parity-runner/logs .local/parity-main-logs 2>/dev/null || true
python3 tools/parity_main.py bisect "$BASE_SHA" --comment
- name: Logs of the failed harnesses
if: failure()
uses: actions/upload-artifact@v4
with:
name: parity-main-logs
path: .local/parity-main-logs/
if-no-files-found: ignore
retention-days: 7
+44 -1
View File
@@ -37,13 +37,56 @@ jobs:
run: bash tools/ci/runner-env.sh
- name: Vanilla data, and resources prepared by this preparer
run: bash tools/ci/vanilla-data.sh
- name: The prepared output is the one the preparer's OUTPUT_VERSION says
env:
BASE_SHA: ${{ inputs.base }}
run: python3 tools/prepare_output.py check --cache --base "$BASE_SHA"
- name: The version constants are the pinned jar's
run: python3 tools/gen_version_consts.py --check
- name: The harnesses this change can affect
env:
PARITY_HOSTS: ''
BASE_SHA: ${{ inputs.base }}
run: python3 tools/parity.py --changed "$BASE_SHA" --timeout 2400 # world_spawn takes ~1480 s on a busy runner
run: |
# main's selection (tools/parity.py --changed --list with main's parity.py and parity_reach.py), so a
# branch made before a better selection landed gets it too; the head's own runner then runs those
# harnesses. A branch that changes the selection itself selects with its own code.
set -euo pipefail
own() { python3 tools/parity.py --changed "$BASE_SHA" --timeout 2400; } # world_spawn takes ~1480 s on a busy runner
fork_point=$(git merge-base "$BASE_SHA" HEAD)
if ! git rev-parse --verify -q origin/main >/dev/null || ! git diff --quiet "$fork_point" HEAD -- tools/parity.py tools/parity_reach.py; then
own
exit
fi
had_reach=$(git ls-files tools/parity_reach.py)
git show origin/main:tools/parity.py > tools/parity.py
git show origin/main:tools/parity_reach.py > tools/parity_reach.py
status=0
python3 tools/parity.py --changed "$BASE_SHA" --list > .local/parity-selection.txt || status=$?
git checkout -- tools/parity.py
if [ -n "$had_reach" ]; then git checkout -- tools/parity_reach.py; else rm -f tools/parity_reach.py; fi
cat .local/parity-selection.txt
if [ "$status" != 0 ]; then
echo "main's selection failed: the head's own selection runs"
own
exit
fi
names=$(sed '$d' .local/parity-selection.txt | grep -E '^[A-Za-z0-9_#]+$' || true)
# The summary line says how many would run: a count that doesn't match the names read (a format change)
# must not let the job pass untested.
expected=$(tail -1 .local/parity-selection.txt | sed -nE 's/^([0-9]+) of [0-9]+ harnesses would run$/\1/p')
count=$(printf '%s' "$names" | grep -c . || true)
if [ -z "$expected" ] || [ "$expected" != "$count" ]; then
echo "main's selection listed $count names, its summary says '${expected:-?}': the head's own selection runs"
own
exit
fi
if [ "$count" = 0 ]; then
echo "Nothing to run."
exit 0
fi
# shellcheck disable=SC2086
python3 tools/parity.py $names --timeout 2400
- name: Logs of the failed harnesses
if: failure()
uses: actions/upload-artifact@v4
+6 -4
View File
@@ -1,6 +1,7 @@
name: release
# Automatic game releases (docs/release-flow.md). After every green `checks` run on main, every 10 minutes (so a
# release waiting for merges to settle goes out without another merge) and by hand, tools/release_train.py decides:
# release waiting for merges to settle goes out without another merge) and by hand, tools/release_train.py decides,
# for a commit whose checks and parity-main (the vanilla harnesses on main) both passed:
# a release when 5 pull requests merged since the last one changed what players run and the newest merged 5 minutes
# ago, or when the oldest has waited 6 hours, or at once for a pull request labelled release-now.
# RELEASES_PAUSED=1 (repository variable) stops them; so does an open issue labelled release-blocker, forced runs too.
@@ -18,7 +19,8 @@ name: release
# gh workflow run release -f dry_run=false -f force=true # on main: release now, whatever the rules say
on:
workflow_run:
workflows: [checks]
# A game release ships a commit both passed on (parity-main runs on batches: the newest commit it passed on).
workflows: [checks, parity-main]
types: [completed]
branches: [main]
schedule:
@@ -64,8 +66,8 @@ jobs:
env:
GH_TOKEN: ${{ github.token }}
RELEASES_PAUSED: ${{ vars.RELEASES_PAUSED }}
# The commit whose checks just passed; a dry run builds the branch it was started on; otherwise
# main's newest commit with green checks.
# The commit whose checks or parity-main just passed; a dry run builds the branch it was started on;
# otherwise main's newest commit with green checks and parity-main.
HEAD_SHA: ${{ github.event.workflow_run.head_sha || (steps.mode.outputs.dry_run == 'true' && github.sha) || '' }}
FORCE: ${{ inputs.force && 'yes' || '' }}
run: |
+1 -1
View File
@@ -82,7 +82,7 @@ python3 tools/agent_control_e2e.py # horizon-ctl / MCP end to end
~25, a file of the client lib ~103, of `horizon-world` ~103–109, of `horizon-core` ~119. Plan for it.
- Rust integration tests: `crates/horizon-client/tests/` (`lan_bot.rs`, `generation_saves.rs`, `streaming_meshes.rs`).
- **macOS:** `cargo build/test --workspace` may fail to link the two wasm guest cdylibs (undefined
`horizon:mods/events` symbols) until #1446 lands: add `--exclude coords-hud --exclude horizon-mods-test-guest --exclude horizon-mods-test-ui-guest`.
`horizon:mods/events` symbols) until #1446 lands: add `--exclude coords-hud --exclude predator-missile --exclude horizon-mods-test-guest --exclude horizon-mods-test-ui-guest`.
Their real check is `cargo test -p horizon-mods` (builds them for `wasm32-unknown-unknown`).
- Linux needs `libasound2-dev libudev-dev`; `--no-default-features` drops audio and controllers (the server build).
- Run and drive the game (starts the `horizon-client` beside `horizon-ctl`; other arguments go to the game):
Generated
+9
View File
@@ -1408,6 +1408,8 @@ dependencies = [
name = "horizon-prepare"
version = "0.1.0"
dependencies = [
"crc32fast",
"libc",
"regex",
"sha1",
"sha2",
@@ -2733,6 +2735,13 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "predator-missile"
version = "0.1.0"
dependencies = [
"horizon-mod-sdk",
]
[[package]]
name = "presser"
version = "0.3.1"
+1 -1
View File
@@ -1,6 +1,6 @@
[workspace]
exclude = ["launcher", "crates/horizon-telemetry-server", "crates/horizon-mod-registry", "third_party/wgpu-hal"]
members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/horizon-mods/tests/ui_guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-shaders", "crates/horizon-shader-api", "crates/horizon-release-sign", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"]
members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/horizon-mods/tests/ui_guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-shaders", "crates/horizon-shader-api", "crates/horizon-release-sign", "crates/horizon-mod-sdk/examples/coords-hud", "crates/horizon-mod-sdk/examples/predator-missile", "xtask"]
resolver = "2"
[workspace.package]
+10
View File
@@ -208,6 +208,8 @@ fn play(path: &Path, report: &mut Report) -> Result<(), String> {
game_time: t,
day_time: 1000 + t,
}),
// The GUI of a 1280 x 720 window at scale 3.
gui_size: (427.0, 240.0),
});
mods.tick(t);
for _ in 0..FRAMES_PER_TICK {
@@ -224,11 +226,19 @@ fn play(path: &Path, report: &mut Report) -> Result<(), String> {
Output::Feedback { text, error } => {
format!("{}: {text}", if error { "error" } else { "feedback" })
}
Output::Command { line } => format!("command: /{line}"),
Output::Stopped { reason, .. } => return Err(format!("it stopped: {reason}")),
Output::OpenScreen { .. } => "opened a screen".to_string(),
Output::CloseScreen { .. } => "closed its screen".to_string(),
Output::Shake { strength, seconds } => {
format!("shook the camera: {strength:.2} for {seconds:.1} s")
}
});
}
let shapes = mods.hud_shapes().count();
if shapes > 0 {
report.output.push(format!("drew {shapes} HUD shape(s)"));
}
match failed(&mods) {
Some(reason) => Err(format!("it stopped: {reason}")),
None => Ok(()),
@@ -598,6 +598,7 @@ fn print(out: &mut String, t: i32, i: usize, m: &Mob, player_down: bool, now: i6
let _ = writeln!(out, " removed {reason}");
return;
}
let _ = write!(out, "\tleashed {}", u8::from(m.extra.leash.is_some()));
let p = m.position;
let v = m.velocity;
let _ = write!(
@@ -977,6 +978,7 @@ pub fn main() -> horizon_client::Result<()> {
}
// Set once the brain exists (below).
x if x.starts_with("cooldown=") => {}
"dead" => m.health = 0.0,
"leashed" => m.extra.leash = Some(horizon_client::mobs::leash::Holder::Player),
x if x.starts_with("leashto=") => {
let n: u32 = x[8..].parse().unwrap();
@@ -1097,6 +1099,7 @@ pub fn main() -> horizon_client::Result<()> {
}
};
}
let mut dropped_leads = 0;
for t in 1..=s.ticks {
world.game_time += 1;
if s.daycycle {
@@ -1208,6 +1211,16 @@ pub fn main() -> horizon_client::Result<()> {
if !alive {
m.removed = true;
}
// Apply the same detach/drop event as Game::break_leash.
for event in &events[from..] {
if let MobEvent::LeashBreak { mob, drop, .. } = event
&& let Some(m) = mobs.iter_mut().find(|m| m.id == *mob)
&& horizon_client::mobs::leash::remove(m).is_some()
&& *drop
{
dropped_leads += 1;
}
}
horizon_client::mobs::apply_brain_events(&mut mobs, &mut events, from);
villager_events(
&mut mobs,
@@ -1427,6 +1440,9 @@ pub fn main() -> horizon_client::Result<()> {
world.game_time,
);
}
if s.name.starts_with("leash_death_") {
let _ = writeln!(out, "{t} leads {dropped_leads}");
}
for e in &events {
if let MobEvent::Sound(name, _, volume, pitch) = e {
let _ = writeln!(
@@ -592,3 +592,21 @@ fn mod_commands_join_the_tree_for_every_player() {
// The game's own tree is left as it was.
assert!(vanilla::game_dispatcher().find("where").is_none());
}
#[test]
fn horizon_commands_are_off_unless_the_server_turns_them_on() {
// By default the game runs vanilla's tree: /help lists no /pregen.
assert!(vanilla::game_dispatcher().find("pregen").is_none());
for dev in [false, true] {
let vanilla_tree = vanilla::game_tree(dev, false);
assert!(vanilla_tree.find("pregen").is_none());
let d = vanilla::game_tree(dev, true);
let parse = d.parse("pregen start 100 circle", &reg());
assert_eq!(parse.executable().unwrap().1, "pregen.start");
// Vanilla's commands are the same with or without it.
assert_eq!(
d.nodes[0].children.len(),
vanilla_tree.nodes[0].children.len() + 1
);
}
}
+62 -5
View File
@@ -538,12 +538,69 @@ fn apply_vanilla_order(d: &mut Dispatcher) {
}
/// The tree the game runs: a release integrated server's, or with
/// `HORIZON_DEV_COMMANDS` set the development one
/// (SharedConstants.DEBUG_DEV_COMMANDS).
/// (SharedConstants.DEBUG_DEV_COMMANDS), plus Horizon's own commands when
/// the server turned them on (`set_horizon_commands`).
pub fn game_dispatcher() -> &'static Dispatcher {
if std::env::var_os("HORIZON_DEV_COMMANDS").is_some() {
dispatcher()
} else {
release_dispatcher()
game_tree(
std::env::var_os("HORIZON_DEV_COMMANDS").is_some(),
HORIZON_COMMANDS.load(std::sync::atomic::Ordering::Relaxed),
)
}
/// Whether this process's server has Horizon's own commands (`/pregen`). Off
/// by default, so `/help` and the command tree are vanilla's; a dedicated
/// server turns them on with `horizon-pregen=true` in server.properties, the
/// game with `--pregen` (docs/pregeneration.md).
static HORIZON_COMMANDS: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
/// Turns Horizon's own commands on or off for the trees `game_dispatcher`
/// returns from now on.
pub fn set_horizon_commands(on: bool) {
HORIZON_COMMANDS.store(on, std::sync::atomic::Ordering::Relaxed);
}
/// The server.properties key that turns `/pregen` on (Horizon's own; vanilla
/// ignores keys it doesn't know).
pub const PREGEN_PROPERTY: &str = "horizon-pregen";
pub(crate) fn game_tree(dev: bool, horizon: bool) -> &'static Dispatcher {
static HORIZON: [std::sync::OnceLock<Dispatcher>; 2] =
[const { std::sync::OnceLock::new() }; 2];
match (dev, horizon) {
(true, false) => dispatcher(),
(false, false) => release_dispatcher(),
(dev, true) => HORIZON[usize::from(dev)].get_or_init(|| {
let mut d = build_with(dev);
horizon_commands(&mut d, 0);
d
}),
}
}
/// Commands vanilla doesn't have, after its own: `/pregen`
/// (docs/pregeneration.md).
fn horizon_commands(d: &mut Dispatcher, root: usize) {
let pregen = d.literal(root, "pregen");
d.requires(pregen, GAMEMASTERS);
let start = d.literal(pregen, "start");
let radius = d.argument(
start,
"radius",
ArgType::Integer {
min: 0,
max: 30_000_000,
},
);
d.executes(radius, "pregen.start");
for shape in ["square", "circle"] {
let shape = d.literal(radius, shape);
d.executes(shape, "pregen.start");
let center = d.argument(shape, "center", ArgType::ColumnPos);
d.executes(center, "pregen.start");
}
for (word, id) in [
("pause", "pregen.pause"),
("continue", "pregen.continue"),
("cancel", "pregen.cancel"),
("status", "pregen.status"),
] {
let w = d.literal(pregen, word);
d.executes(w, id);
}
}
pub fn dispatcher() -> &'static Dispatcher {
+50 -5
View File
@@ -33,7 +33,11 @@ impl Cost {
#[derive(Clone, Debug)]
pub struct Enchantment {
pub id: Arc<str>,
/// The name the pinned English map gave at load: the fallback for a
/// language that has no entry for the key below.
pub description: String,
/// `description`'s translation key (`enchantment.minecraft.<name>`).
pub description_key: String,
pub max_level: u32,
pub weight: u32,
pub anvil_cost: u32,
@@ -224,6 +228,7 @@ impl Enchantments {
.get(key)
.cloned()
.unwrap_or_else(|| id.trim_start_matches("minecraft:").replace('_', " ")),
description_key: key.to_owned(),
max_level: v["max_level"].as_u64().unwrap_or(1) as u32,
weight: v["weight"].as_u64().unwrap_or(1) as u32,
anvil_cost: v["anvil_cost"].as_u64().unwrap_or(0) as u32,
@@ -329,16 +334,26 @@ impl Enchantments {
}
/// `Enchantment.getFullname` as plain text and whether it is a curse (red).
pub fn full_name(&self, id: &str, level: u32) -> (String, bool) {
let language = crate::lang::current();
self.full_name_in(language.as_deref(), id, level)
}
/// `full_name` against `language`; `None` (no language loaded: a
/// dedicated server, the probes) keeps the English names loaded above.
pub fn full_name_in(
&self,
language: Option<&crate::lang::Language>,
id: &str,
level: u32,
) -> (String, bool) {
let tr = |key: &str| language.and_then(|l| l.get(key).map(str::to_owned));
let Some(e) = self.get(id) else {
return (format!("{id} {}", level_name(level)), false);
};
let mut name = e.description.clone();
let mut name = tr(&e.description_key).unwrap_or_else(|| e.description.clone());
if level != 1 || e.max_level != 1 {
name.push(' ');
name.push_str(
self.lang
.get(&format!("enchantment.level.{level}"))
.map_or(&level_name(level), |s| s),
&tr(&format!("enchantment.level.{level}")).unwrap_or_else(|| level_name(level)),
);
}
(name, self.is_curse(id))
@@ -771,7 +786,7 @@ pub(crate) mod tests {
let defs = [
(
"minecraft:sharpness",
json!({"anvil_cost":1,"exclusive_set":"#minecraft:exclusive_set/damage","max_cost":{"base":21,"per_level_above_first":11},"max_level":5,"min_cost":{"base":1,"per_level_above_first":11},"primary_items":"#minecraft:enchantable/melee_weapon","supported_items":"#minecraft:enchantable/sharp_weapon","weight":10,"slots":["mainhand"],"effects":{"minecraft:damage":[{"effect":{"type":"minecraft:add","value":{"type":"minecraft:linear","base":1.0,"per_level_above_first":0.5}}}]}}),
json!({"anvil_cost":1,"description":{"translate":"enchantment.minecraft.sharpness"},"exclusive_set":"#minecraft:exclusive_set/damage","max_cost":{"base":21,"per_level_above_first":11},"max_level":5,"min_cost":{"base":1,"per_level_above_first":11},"primary_items":"#minecraft:enchantable/melee_weapon","supported_items":"#minecraft:enchantable/sharp_weapon","weight":10,"slots":["mainhand"],"effects":{"minecraft:damage":[{"effect":{"type":"minecraft:add","value":{"type":"minecraft:linear","base":1.0,"per_level_above_first":0.5}}}]}}),
),
(
"minecraft:smite",
@@ -795,6 +810,7 @@ pub(crate) mod tests {
.map(|(id, v)| Enchantment {
id: Arc::from(*id),
description: id.trim_start_matches("minecraft:").into(),
description_key: v["description"]["translate"].as_str().unwrap_or("").into(),
max_level: v["max_level"].as_u64().unwrap() as u32,
weight: v["weight"].as_u64().unwrap() as u32,
anvil_cost: v["anvil_cost"].as_u64().unwrap() as u32,
@@ -1088,4 +1104,33 @@ pub(crate) mod tests {
assert!(!e.compatible("minecraft:sharpness", "minecraft:smite"));
assert!(e.compatible("minecraft:sharpness", "minecraft:unbreaking"));
}
/// The enchantment's name and its level come from the selected language,
/// with the English name loaded above as the fallback.
#[test]
fn names_follow_the_language() {
struct Mem(HashMap<String, Vec<u8>>);
impl crate::lang::ResourceSource for Mem {
fn read(&self, path: &str) -> Option<Vec<u8>> {
self.0.get(path).cloned()
}
}
let ru = Mem(HashMap::from([(
"assets/minecraft/lang/ru_ru.json".to_owned(),
r#"{"enchantment.minecraft.sharpness":"Острота","enchantment.level.5":"V"}"#
.as_bytes()
.to_vec(),
)]));
let ru = crate::lang::Language::load(&[&ru], "ru_ru");
let e = sample();
assert_eq!(
e.full_name_in(Some(&ru), "minecraft:sharpness", 5).0,
"Острота V"
);
// mending carries no key in the fixture: the English name stays.
assert_eq!(
e.full_name_in(Some(&ru), "minecraft:mending", 1).0,
"mending"
);
}
}
+69 -15
View File
@@ -97,6 +97,7 @@ mod maps;
mod minecarts;
mod mob_blocks;
mod mob_gear;
mod mod_render;
mod mods;
mod mount_menu;
mod mounts;
@@ -361,7 +362,7 @@ impl Options {
match arg.as_str() {
"--help" | "-h" => {
println!(
"Horizon — Minecraft Java Edition {} in Rust\n\n --resources PATH Prepared resources (horizon-prepare)\n --render-distance N Chunks, 2..64 (default 12)\n --world NAME World folder under saves/ (default \"New World\")\n --seed N World seed for new worlds (default 0)\n --width N, --height N Window size (default 1280 x 720)\n --creative Creative mode (default survival)\n --test-terrain Simple test terrain instead of vanilla generation\n --frames N Exit after N frames (testing)\n --world-frames N Exit after N frames drawn in the world (testing)\n --smoke-report PATH Write a JSON report on exit\n --benchmark SCENARIO Run a benchmark (standard, village, forest, ocean, mountain), write its report to benchmarks/ and quit\n --control Accept horizon-ctl commands on a local socket\n --control-socket PATH Control socket path (default $XDG_RUNTIME_DIR/horizon-control.sock)\n --server HOST[:PORT] Join a multiplayer server (offline mode) at start\n --name NAME Player name for multiplayer (default: generated once, kept in horizon-player.txt)\n --session-file PATH Signed-in account from the launcher (read once, then deleted)\n --dev-mod PATH A Horizon mod (.hmod or folder) to develop: always on, reloaded when rebuilt\n --in-frame Run single player's world in the frame, not on its server thread\n\nControls: mouse look, WASD move, Space jump, Left Shift sneak, Left Ctrl sprint,\nleft click break, right click place/use, 1-9/scroll hotbar, E inventory,\nQ drop, F3 debug, Esc pause.",
"Horizon — Minecraft Java Edition {} in Rust\n\n --resources PATH Prepared resources (horizon-prepare)\n --render-distance N Chunks, 2..64 (default 12)\n --world NAME World folder under saves/ (default \"New World\")\n --seed N World seed for new worlds (default 0)\n --width N, --height N Window size (default 1280 x 720)\n --creative Creative mode (default survival)\n --test-terrain Simple test terrain instead of vanilla generation\n --frames N Exit after N frames (testing)\n --world-frames N Exit after N frames drawn in the world (testing)\n --smoke-report PATH Write a JSON report on exit\n --benchmark SCENARIO Run a benchmark (standard, village, forest, ocean, mountain), write its report to benchmarks/ and quit\n --control Accept horizon-ctl commands on a local socket\n --control-socket PATH Control socket path (default $XDG_RUNTIME_DIR/horizon-control.sock)\n --server HOST[:PORT] Join a multiplayer server (offline mode) at start\n --name NAME Player name for multiplayer (default: generated once, kept in horizon-player.txt)\n --session-file PATH Signed-in account from the launcher (read once, then deleted)\n --dev-mod PATH A Horizon mod (.hmod or folder) to develop: always on, reloaded when rebuilt\n --in-frame Run single player's world in the frame, not on its server thread\n --pregen Add Horizon's /pregen command (docs/pregeneration.md)\n\nControls: mouse look, WASD move, Space jump, Left Shift sneak, Left Ctrl sprint,\nleft click break, right click place/use, 1-9/scroll hotbar, E inventory,\nQ drop, F3 debug, Esc pause.",
horizon_core::VANILLA_VERSION
);
return Ok(None);
@@ -378,6 +379,8 @@ impl Options {
"--control" => o.control = Some(horizon_client::control::default_path()),
"--server-thread" => o.server_thread = true,
"--in-frame" | "--no-server-thread" => o.server_thread = false,
// Horizon's own commands (/pregen), off by default as on vanilla.
"--pregen" => horizon_client::command::vanilla::set_horizon_commands(true),
_ => {
let value = args
.next()
@@ -1240,6 +1243,11 @@ struct Game<'a> {
luminance: Option<horizon_client::dynamic_light::Luminance>,
/// Name tags drawn over everything (`RenderPipelines.TEXT_SEE_THROUGH`).
see_through_vertices: Vec<Vertex>,
/// Runs of `entity_vertices` by entity, for a shader pack's per-entity draws; recorded only
/// while `pack_ids` is set (game/shader_packs.rs).
entity_spans: std::cell::RefCell<Vec<horizon_client::gfx::pack_draw::EntitySpan>>,
/// The shader pack's entity and block entity ids, while one draws entities.
pack_ids: Option<shader_packs::PackIds>,
started: Instant,
previous: Instant,
/// FramerateLimitTracker.latestInputTime.
@@ -1552,6 +1560,8 @@ struct Game<'a> {
/// position) for the guests.
lan_block_events: Vec<([i32; 3], i32, i32, u32)>,
lan_entity_events: Vec<(i32, i8, [f64; 3])>,
/// Global level events for every guest (`ServerLevel.globalLevelEvent`).
lan_global_events: Vec<(i32, [i32; 3], i32)>,
/// `ContainerOpenersCounter`: how many players (the host and the guests)
/// have each chest open.
lid_openers: HashMap<(i32, i32, i32), i32>,
@@ -2765,7 +2775,13 @@ impl Game<'_> {
}
_ => 1.0,
};
self.audio.play(name, c, volume, pitch);
if matches!(id, 1029 | 1030) {
// Clients play the anvil sound from its level event;
// capturing it also sends a duplicate sound packet.
self.audio.play_unshared(name, c, volume, pitch);
} else {
self.audio.play(name, c, volume, pitch);
}
}
}
LevelEvent::Sound {
@@ -4927,6 +4943,7 @@ impl Game<'_> {
// Hud: the action bar and titles draw over the hotbar (nextStratum).
self.draw_boss_bars(&mut batch);
self.draw_extras(&mut batch);
self.draw_mod_shapes(&mut batch);
}
if show_hud {
// Hud.nextContextualInfoState: the jump bar, the experience bar or
@@ -6748,8 +6765,10 @@ impl Game<'_> {
alpha: f64,
mut out: Vec<Vertex>,
) -> Vec<Vertex> {
self.entity_spans.borrow_mut().clear();
self.prepare_entity_culling(camera);
self.prepare_trims();
self.mark_entity(out.len(), "minecraft:entity_flame");
let fire_yaw = if self.camera_type == perspective::CameraType::ThirdPersonFront {
self.yaw + 180.0
} else {
@@ -6841,21 +6860,28 @@ impl Game<'_> {
}
}
// `ItemEntityRenderer`.
self.mark_entity(out.len(), "minecraft:item");
self.dropped_item_geometry(camera, alpha, &mut out);
// `VaultRenderer`.
self.mark_entity(out.len(), "");
self.vault_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:ominous_item_spawner");
self.ominous_item_spawner_geometry(camera, alpha, &mut out);
// Mobs and their projectiles.
// Mobs and their projectiles (a span each).
self.mob_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:player");
self.player_model_geometry(camera, self.clock.camera_fraction(), &mut out);
self.guest_model_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:evoker_fangs");
self.fangs_geometry(camera, alpha, &mut out);
let mut see_through = std::mem::take(&mut self.see_through_vertices);
see_through.clear();
self.mark_entity(out.len(), "minecraft:name_tag");
self.name_tag_geometry(camera, alpha, &mut out, &mut see_through);
self.text_display_geometry(camera, alpha, &mut out, &mut see_through);
self.see_through_vertices = see_through;
// Entity shadows (EntityRenderer.extractShadow).
self.mark_entity(out.len(), "minecraft:entity_shadow");
if let Some(sprite) = self.gui.atlas.sprites.get("misc/shadow").copied() {
let ambient = if self.dim == Dim::Nether { 0.1 } else { 0.0 };
let darken = self.sky_darken();
@@ -6949,13 +6975,14 @@ impl Game<'_> {
v.intersects(o - glam::Vec3::ONE, o + glam::Vec3::splat(2.0))
})
});
if self.entity_culler.is_some() {
if self.entity_culler.is_some() || self.pack_ids.is_some() {
for block in visible {
let start = out.len();
self.mark_block_entity(start, block.1);
self.res
.block_entity_render()
.render(&scene, std::iter::once(block), &mut out);
if !off_screen(block.1) {
if !off_screen(block.1) && self.entity_culler.is_some() {
self.cull_entity_vertices(&mut out, start);
}
}
@@ -6964,6 +6991,7 @@ impl Game<'_> {
.block_entity_render()
.render(&scene, visible, &mut out);
}
self.mark_stage(out.len(), "DESTROY");
for (pos, stage) in self.cracks(camera) {
let state = self.world.block(pos[0], pos[1], pos[2]);
self.res
@@ -6972,6 +7000,7 @@ impl Game<'_> {
}
}
// Experience orbs (ExperienceOrbRenderer): pulsing billboards from a 4x4 icon sheet.
self.mark_entity(out.len(), "minecraft:experience_orb");
if let Some(r) = self
.gui
.atlas
@@ -7035,6 +7064,7 @@ impl Game<'_> {
}
}
// Falling blocks use their baked model, including non-cubic shapes.
self.mark_entity(out.len(), "minecraft:falling_block");
for f in &self.falling {
let Some((_, tint)) = self.block_face_sprite(f.state) else {
continue;
@@ -7065,20 +7095,34 @@ impl Game<'_> {
vertex.color = horizon_client::hud_layout::multiply(vertex.color, tint);
}
}
self.mark_entity(out.len(), "");
self.moving_piston_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:tnt");
self.tnt_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:painting");
self.painting_geometry(camera, &mut out);
self.mark_entity(out.len(), "minecraft:item_frame");
self.frame_geometry(camera, &mut out);
self.mark_entity(out.len(), "minecraft:armor_stand");
self.armor_stand_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:oak_boat");
self.boat_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:minecart");
self.cart_geometry(camera, alpha, &mut out);
self.mark_stage(out.len(), "BLOCK_ENTITIES");
self.beacon_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:fishing_bobber");
self.fishing_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:leash_knot");
self.leash_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:trident");
self.trident_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:firework_rocket");
self.rocket_geometry(camera, alpha, &mut out);
self.mark_entity(out.len(), "minecraft:splash_potion");
self.thrown_potion_geometry(camera, alpha, &mut out);
// Particles (ParticleEngine.extract): camera-facing quads.
self.mark_stage(out.len(), "PARTICLES");
let world = &self.world;
self.particles.render(
camera,
@@ -7089,6 +7133,7 @@ impl Game<'_> {
&mut out,
);
// Breaking progress overlays (destroy_stage_0..9): ours and the other players'.
self.mark_stage(out.len(), "DESTROY");
for (pos, stage) in self.cracks(camera) {
let o = (glam::DVec3::new(f64::from(pos[0]), f64::from(pos[1]), f64::from(pos[2]))
- camera)
@@ -8401,6 +8446,8 @@ impl Game<'_> {
1.0,
1.0,
);
// ItemStack.useOn: a use that acted counts as one of the item.
self.stats.add("used", &stack.item, 1);
self.damage_held(damage);
self.post_block_game_event("minecraft:block_change", pos, self.sculk_actor(), Some(new));
Ok(true)
@@ -8605,6 +8652,14 @@ impl Game<'_> {
1.0,
1.0,
);
// HoneycombItem.useOn's game event, and ItemStack.useOn's used statistic.
self.post_block_game_event(
"minecraft:block_change",
hit.pos,
self.sculk_actor(),
Some(state),
);
self.stats.add("used", &stack.item, 1);
if !self.creative {
self.consume_held();
}
@@ -9196,10 +9251,6 @@ impl Game<'_> {
)
}
fn apply_settings(&mut self) {
if let Some(r) = &self.renderer {
self.settings
.clamp_gui_scale([r.config.width as i32, r.config.height as i32]);
}
self.streaming.radius = self.settings.render_distance;
// Options.biomeBlendRadius, ambientOcclusion and cutoutLeaves:
// LevelExtractor.allChanged when they change.
@@ -9962,6 +10013,7 @@ impl Game<'_> {
self.pitch,
),
};
let (view_yaw, view_pitch) = self.shaken_view(view_yaw, view_pitch);
self.audio
.set_listener([camera.x, camera.y, camera.z], view_yaw, view_pitch);
let block = [
@@ -10193,6 +10245,7 @@ impl Game<'_> {
} else {
self.shader_pack_inputs(camera, alpha as f32)
};
let entity_spans = std::mem::take(self.entity_spans.get_mut());
let frame = Frame {
weather: &weather,
weather_time: horizon_client::weather::Time::new(self.world.game_time, alpha as f32),
@@ -10257,6 +10310,7 @@ impl Game<'_> {
color: sky.cloud_color,
}),
entities: &entities,
entity_spans: &entity_spans,
dynamic_lights: &self.dynamic_lights,
lightning: horizon_client::gfx::lightning::Input {
bolts: &bolts,
@@ -10308,6 +10362,7 @@ impl Game<'_> {
};
// Restore storage even when rendering fails (for example, a lost surface).
self.entity_vertices = entities;
*self.entity_spans.get_mut() = entity_spans;
self.see_through_vertices = see_through;
result
}
@@ -10692,6 +10747,7 @@ impl Game<'_> {
// The loading screen's chunk map fills ring by ring, as vanilla's.
self.streaming.loading =
self.options.execution_role.is_client() && self.loading_reason().is_some();
self.tick_pregen();
let (uploads, unloaded) =
horizon_client::spans::time("streaming", || self.streaming.update(&mut self.world));
if let Some(lod) = &mut self.lod {
@@ -11902,6 +11958,8 @@ impl<'a> Game<'a> {
dynamic_lights: Vec::new(),
luminance: None,
see_through_vertices: Vec::new(),
entity_spans: Default::default(),
pack_ids: None,
health_hud: hud_layout::HealthHud::default(),
started: now,
previous: now,
@@ -12104,6 +12162,7 @@ impl<'a> Game<'a> {
lan_events: Vec::new(),
lan_block_events: Vec::new(),
lan_entity_events: Vec::new(),
lan_global_events: Vec::new(),
lid_openers: HashMap::new(),
lan_acting: None,
guest_packets: Vec::new(),
@@ -12836,6 +12895,7 @@ fn loading_frame(hud: &HudBatch) -> Frame<'_> {
hud,
nether_lighting: false,
entities: &[],
entity_spans: &[],
dynamic_lights: &[],
clouds: None,
lightning: Default::default(),
@@ -13096,10 +13156,6 @@ impl Loading {
return;
}
}
if let Some(r) = &self.renderer {
self.settings
.clamp_gui_scale([r.config.width as i32, r.config.height as i32]);
}
horizon_client::hud::GUI_SCALE.store(self.settings.gui_scale, Ordering::Relaxed);
self.window = Some(window);
}
@@ -13138,8 +13194,6 @@ impl Loading {
self.settings.horizon.api,
) {
Ok(r) => {
self.settings
.clamp_gui_scale([r.config.width as i32, r.config.height as i32]);
horizon_client::hud::GUI_SCALE.store(self.settings.gui_scale, Ordering::Relaxed);
self.renderer = Some(r);
}
+8 -3
View File
@@ -10,6 +10,7 @@ mod items;
mod misc;
mod place;
mod players;
pub(super) mod pregen;
mod raid;
mod terrain_cmds;
mod text_commands;
@@ -758,6 +759,7 @@ impl Game<'_> {
"ride.mount" | "ride.dismount" => self.cmd_ride(id, ctx),
"spreadplayers" => self.cmd_spreadplayers(ctx),
id if id.starts_with("forceload.") => self.cmd_forceload(id, ctx),
id if id.starts_with("pregen.") => self.cmd_pregen(id, ctx),
"fillbiome" => self.cmd_fillbiome(ctx),
id if id.starts_with("worldborder.") => self.cmd_worldborder(id, ctx),
id if id.starts_with("loot.") => self.cmd_loot(id, ctx),
@@ -2499,12 +2501,14 @@ impl Game<'_> {
};
let max = Self::int(ctx, "maxCount").unwrap_or(-1);
let counting = max == 0;
// Inventory.clearOrCountMatchingItems: main, armor, crafting grid, carried.
// Inventory slots 0..40: main, armor feet to head, off hand;
// then clearOrCountMatchingItems visits the crafting grid and carried stack.
let mut removed = 0i32;
let mut budget = if max < 0 { i32::MAX } else { max };
let all = |g: &Self| -> Vec<Option<ItemStack>> {
let mut v: Vec<Option<ItemStack>> = g.inventory.clone();
v.extend(g.armor.iter().cloned());
v.extend(g.armor.iter().rev().cloned());
v.push(g.offhand.clone());
v.extend(g.craft_grid.iter().cloned());
v.push(g.carried.clone());
v
@@ -2518,7 +2522,8 @@ impl Game<'_> {
.collect();
let mut slots: Vec<&mut Option<ItemStack>> = Vec::new();
slots.extend(self.inventory.iter_mut());
slots.extend(self.armor.iter_mut());
slots.extend(self.armor.iter_mut().rev());
slots.push(&mut self.offhand);
slots.extend(self.craft_grid.iter_mut());
slots.push(&mut self.carried);
for (slot, matches) in slots.into_iter().zip(matching) {
@@ -57,6 +57,8 @@ pub(in crate::game) struct CommandExtras {
/// The `CommandResultCallback` results of the outermost source, when a
/// harness records them.
pub results: Option<Vec<(bool, i32)>>,
/// The running `/pregen` task.
pub pregen: Option<super::pregen::PregenTask>,
}
use horizon_core::version::{BUILD_TIME, DATA_VERSION, STABLE, VANILLA_VERSION, VERSION_ID};
@@ -0,0 +1,276 @@
//! `/pregen`, Horizon's own command: generate, light and save every chunk of a
//! square or a circle ahead of the players (docs/pregeneration.md). The task
//! lives on the server and hands its band to the level's streaming, whose
//! workers do the work; chunks behind the band unload and are saved as any
//! chunk the players leave.
use super::{CommandError, Context, Dim, Game, R};
use horizon_client::{
lan::data::component_nbt,
pregen::{Pregen, Shape},
text::{self, Text},
};
use horizon_protocol::packets::play::{self, Clientbound as C};
use std::time::{Duration, Instant};
/// How often the progress is reported.
const REPORT_EVERY: Duration = Duration::from_secs(5);
/// How long the band may stay put before the task looks for what holds it.
const STALL: Duration = Duration::from_secs(30);
pub(in crate::game) struct PregenTask {
pregen: Pregen,
dim: Dim,
paused: bool,
/// Time spent running, up to the last pause, and when it last resumed.
ran: Duration,
resumed: Instant,
reported: Instant,
/// When the band last moved, to notice a task that stopped advancing.
moved: Instant,
/// The player who started it, told the progress too (a guest's connection).
owner: Option<u32>,
}
impl PregenTask {
fn running_for(&self) -> Duration {
if self.paused {
self.ran
} else {
self.ran + self.resumed.elapsed()
}
}
fn progress(&self) -> Text {
let p = &self.pregen;
let seconds = self.running_for().as_secs_f64();
let rate = p.done as f64 / seconds.max(1e-3);
let left = p.total - p.done;
let eta = if rate > 0.0 {
duration(left as f64 / rate)
} else {
"unknown".into()
};
Text::literal(format!(
"Pregenerating {}: {} of {} chunks ({:.1}%), {rate:.1} chunks/s, {} left",
self.dim.id(),
p.done,
p.total,
100.0 * p.done as f64 / p.total.max(1) as f64,
if p.done == p.total {
"nothing".into()
} else {
eta
},
))
}
}
fn failure(message: &str) -> CommandError {
CommandError::new(Text::literal(message))
}
/// `1h 02m 03s`.
fn duration(seconds: f64) -> String {
let s = seconds.round() as u64;
match (s / 3600, s / 60 % 60, s % 60) {
(0, 0, s) => format!("{s}s"),
(0, m, s) => format!("{m}m {s:02}s"),
(h, m, s) => format!("{h}h {m:02}m {s:02}s"),
}
}
impl Game<'_> {
pub(super) fn cmd_pregen(&mut self, id: &str, ctx: &Context) -> R<i32> {
match id {
"pregen.start" => {
if self.cmds.pregen.is_some() {
return Err(failure(
"Pregeneration is already running: /pregen cancel stops it",
));
}
let radius = Self::int(ctx, "radius").unwrap_or(0);
let shape = if ctx.literals.iter().any(|l| l == "circle") {
Shape::Circle
} else {
Shape::Square
};
let [x, z] = if ctx.get("center").is_some() {
self.column_arg(ctx, "center")?
} else {
let p = self.source_position();
[p[0].floor() as i32, p[2].floor() as i32]
};
let pregen = Pregen::new((x >> 4, z >> 4), radius >> 4, shape);
let now = Instant::now();
let total = pregen.total;
self.feedback(Text::literal(format!(
"Pregenerating {total} chunks of {}: a {} of radius {radius} around {x} {z}",
self.dim.id(),
match shape {
Shape::Square => "square",
Shape::Circle => "circle",
},
)));
self.cmds.pregen = Some(PregenTask {
pregen,
dim: self.dim,
paused: false,
ran: Duration::ZERO,
resumed: now,
reported: now,
moved: now,
owner: self.lan_acting,
});
Ok(total as i32)
}
"pregen.pause" | "pregen.continue" => {
let pause = id == "pregen.pause";
let task = self
.cmds
.pregen
.as_mut()
.ok_or_else(|| failure("No pregeneration is running"))?;
if task.paused != pause {
task.paused = pause;
if pause {
task.ran += task.resumed.elapsed();
} else {
task.resumed = Instant::now();
}
}
let (progress, dim) = (task.progress(), task.dim);
if pause && self.dim == dim {
self.streaming.set_band(None);
}
self.feedback(
Text::literal(if pause { "Paused. " } else { "Continuing. " }).append(progress),
);
Ok(1)
}
"pregen.cancel" => {
let task = self
.cmds
.pregen
.take()
.ok_or_else(|| failure("No pregeneration is running"))?;
if self.dim == task.dim {
self.streaming.set_band(None);
}
self.feedback(Text::literal("Cancelled. ").append(task.progress()));
Ok(task.pregen.done as i32)
}
_ => {
let task = self
.cmds
.pregen
.as_ref()
.ok_or_else(|| failure("No pregeneration is running"))?;
let progress = task.progress();
let done = task.pregen.done as i32;
self.feedback(progress);
Ok(done)
}
}
}
/// Move the task's band past the rows that are lit and hand it to the
/// level's streaming, and report the progress now and then. Runs before
/// each streaming update of the level in place.
pub(in crate::game) fn tick_pregen(&mut self) {
let Some(task) = self.cmds.pregen.as_mut() else {
return;
};
if task.dim != self.dim || task.paused {
return;
}
// A row is done once its chunks are lit and post-processed
// (`postprocess_chunks`, on the tick), as vanilla's are by the time
// they are FULL: then they may unload and be saved.
let (streaming, chunks) = (&mut self.streaming, &self.world.chunks);
if task.pregen.advance(|x, z| {
streaming.is_lit(x, z)
&& chunks
.get(&(x, z))
.is_some_and(|c| c.postprocess.is_empty())
}) {
task.moved = Instant::now();
}
streaming.set_band(task.pregen.band());
if task.moved.elapsed() >= STALL {
// Nothing advanced for a while: say what the first row waits
// for, and queue light again for chunks that lost their job.
task.moved = Instant::now();
let row = task.pregen.first_row();
let waiting = row
.iter()
.filter(|k| chunks.get(k).is_some_and(|c| !c.postprocess.is_empty()))
.count();
let [missing, lighting, unlit] = streaming.unstick_light(&mut self.world, &row);
log::warn!(
"Horizon: pregeneration waits on row {:?}: {missing} not loaded, {lighting} being lit, \
{unlit} unlit with no light job (queued again), {waiting} not post-processed; {:?}",
row.first(),
streaming.stages()
);
}
let finished = task.pregen.finished();
if finished || task.reported.elapsed() >= REPORT_EVERY {
task.reported = Instant::now();
let (owner, mut message) = (task.owner, task.progress());
if finished {
message = Text::literal("Done. ")
.append(message)
.with_color(text::GREEN);
self.cmds.pregen = None;
}
self.pregen_report(owner, message);
}
}
/// Whether the level in place has a running task, which the server pumps
/// between its ticks (`pump_pregen`).
pub(in crate::game) fn pregen_runs_here(&self) -> bool {
self.cmds
.pregen
.as_ref()
.is_some_and(|t| t.dim == self.dim && !t.paused)
}
/// The level a running task needs ticked, whoever is in it.
pub(in crate::game) fn pregen_level(&self) -> Option<Dim> {
self.cmds.pregen.as_ref().map(|t| t.dim)
}
/// Keep the workers fed until `until`: collect what they finished and
/// hand them the next jobs as soon as each one is done, rather than once
/// a tick.
pub(in crate::game) fn pump_pregen(&mut self, until: Instant) {
while self.pregen_runs_here() {
let now = Instant::now();
if now >= until {
break;
}
self.streaming
.wait((until - now).min(Duration::from_millis(5)));
self.update_world();
}
}
/// The console and the player who started the task get the progress.
fn pregen_report(&mut self, owner: Option<u32>, message: Text) {
if self.options.dedicated.is_some() {
super::super::dedicated::log("INFO", &message.plain());
} else {
log::info!("{}", message.plain());
}
if let Some(host) = &self.lan {
if let Some(g) = owner.and_then(|c| host.hub.guest(c)) {
g.send(&C::SystemChat(play::SystemChat {
content: component_nbt(&message.component()),
overlay: false,
}));
}
} else if self.options.dedicated.is_none() {
self.system_message(message);
}
}
}
@@ -30,7 +30,7 @@ fn ticks_to_seconds(ticks: i64) -> String {
impl Game<'_> {
// ---- /forceload ----------------------------------------------------------
fn column_arg(&self, ctx: &Context, name: &str) -> R<[i32; 2]> {
pub(super) fn column_arg(&self, ctx: &Context, name: &str) -> R<[i32; 2]> {
let c = self
.coords(ctx, name)
.ok_or_else(|| err("argument.pos2d.incomplete", &[]))?;
@@ -3031,6 +3031,7 @@ impl Game<'_> {
continue;
}
}
self.mark_entity(entity_start, &mob.kind.id);
let light = self.world.light(
mob.position.x.floor() as i32,
(mob.position.y + mob.eye_height()).floor() as i32,
+11 -1
View File
@@ -155,6 +155,14 @@ pub(super) fn preflight(options: &mut Options) -> Result<bool> {
for w in &props.warnings {
log("WARN", w);
}
// Horizon's own commands are the owner's choice, off as on vanilla.
if props
.raw
.get(horizon_client::command::vanilla::PREGEN_PROPERTY)
.is_some_and(|v| v.trim().eq_ignore_ascii_case("true"))
{
horizon_client::command::vanilla::set_horizon_commands(true);
}
props.store(&settings)?;
let eula_file = PathBuf::from("eula.txt");
let agreed = match eula::agreed(&eula_file) {
@@ -667,7 +675,9 @@ impl Game<'_> {
self.poll_server_control(&mut state);
let players = self.lan.as_ref().map_or(0, |h| h.hub.guests.len());
if pause_after > 0 {
if players == 0 {
// A `/pregen` task keeps the server busy as a player would:
// paused, nothing would post-process its chunks.
if players == 0 && self.pregen_level().is_none() {
state.empty_ticks += 1;
} else {
state.empty_ticks = 0;
+4 -3
View File
@@ -1971,9 +1971,10 @@ impl Game<'_> {
self.spawn_orbs([p.x, p.y, p.z], (xp as f32 * 0.08) as u32);
}
if t == 1 {
// Level event 1028: the death roar, heard everywhere.
self.audio
.play("minecraft:entity.ender_dragon.death", None, 5.0, 1.0);
// EnderDragon.tickDeath: global level event 1028, the death
// roar, heard everywhere.
let at = [p.x, p.y, p.z].map(|v| v.floor() as i32);
self.global_level_event(1028, at, 0);
}
if let Some(d) = self.dragon.as_mut() {
d.position.y += 0.1;
+4 -3
View File
@@ -316,9 +316,10 @@ impl Game<'_> {
)?;
}
}
// Global LevelEvent 1038: END_PORTAL_SPAWN.
self.audio
.play("minecraft:block.end_portal.spawn", None, 1.0, 1.0);
// EnderEyeItem.useOn: global level event 1038 (END_PORTAL_SPAWN),
// heard by every player (vanilla places it at the frame's corner
// + (1, 0, 1); only its direction matters).
self.global_level_event(1038, c, 0);
}
Ok(true)
}
+6 -3
View File
@@ -1136,9 +1136,9 @@ impl Game<'_> {
for e in events {
if let horizon_world::level::LevelEvent::LevelEvent { id, pos, data } = e {
// The acting guest does not predict the egg's teleport
// particles, nor bone meal's growth (BoneMealItem.useOn's
// levelEvent names no player).
let except = if matches!(*id, 2015 | 1505) {
// particles, bone meal's growth, or anvil result sounds
// (these levelEvent calls name no excluded player).
let except = if matches!(*id, 2015 | 1505 | 1029 | 1030) {
None
} else {
self.lan_acting
@@ -1177,6 +1177,9 @@ impl Game<'_> {
for (id, event, at) in std::mem::take(&mut self.lan_entity_events) {
host.hub.entity_event(id, event, at);
}
for (kind, pos, data) in std::mem::take(&mut self.lan_global_events) {
host.hub.global_level_event(kind, pos, data);
}
self.lan_broadcast_effects(host);
if host.hub.guests.is_empty() {
host.mob_state.clear();
@@ -1808,14 +1808,13 @@ impl Game<'_> {
g.use_delay -= 1;
}
});
// ServerPlayerGameMode.handleBlockBreakAction: a creative
// player's START breaks the block once (destroyAndAck) and
// leaves nothing being destroyed; its client sends no STOP.
// Kept, the dig broke whatever was put there next, every
// destroy delay, for as long as the player broke nothing else.
// ServerPlayerGameMode.handleBlockBreakAction: creative and
// instant survival STARTs finish without a STOP from the client.
// tick_mining clears mining on completion; forget the START too,
// so a flower or crop planted here later is not broken again.
if digging.is_some()
&& host.hub.guests[gi].creative()
&& let Some(play) = host.play.get_mut(&conn)
&& (host.hub.guests[gi].creative() || play.mining.is_none())
{
play.digging = None;
}
@@ -161,12 +161,12 @@ impl Game<'_> {
+ (self.particles.random().next_float() - self.particles.random().next_float())
* 0.2)
* 0.7;
self.audio.play(
"minecraft:entity.generic.explode",
Some(p.center),
4.0,
pitch,
);
// The packet's explosionSound: a wind charge's burst, a bed's
// or anchor's blast, a modded sound.
let sound = self
.remote_sound_name(&p.sound)
.unwrap_or_else(|| "minecraft:entity.generic.explode".to_owned());
self.audio.play(&sound, Some(p.center), 4.0, pitch);
}
if let Some(options) = self.received_particle(&p.particle, p.center) {
self.particles.add(&options, p.center, [1.0, 0.0, 0.0]);
+2
View File
@@ -79,6 +79,8 @@ impl Game<'_> {
if horizon_client::mobs::leash::remove(m).is_none() {
return;
}
// Leashable.tickLeash gates a dying entity's lead drop on entity_drops.
let drop = drop && (m.alive() || self.ui.rules.bool("entity_drops"));
let at = [m.position.x, m.position.y, m.position.z];
if sound {
self.lead_sound("break", at);
@@ -101,6 +101,39 @@ impl Game<'_> {
}
}
}
/// `ServerLevel.globalLevelEvent`: heard by every player on the server
/// (an end portal opening, the dragon's death roar): here, and sent to
/// every guest whatever their dimension.
pub(super) fn global_level_event(&mut self, id: i32, pos: [i32; 3], data: i32) {
self.global_level_event_sound(id, pos);
if self.audio.capture.is_some() {
self.lan_global_events.push((id, pos, data));
}
}
/// `LevelEventHandler.globalLevelEvent`: the sound two blocks from the
/// camera towards the event, so it is heard from anywhere.
pub(super) fn global_level_event_sound(&mut self, id: i32, pos: [i32; 3]) {
let (sound, volume) = match id {
1023 => ("minecraft:entity.wither.spawn", 1.0),
1028 => ("minecraft:entity.ender_dragon.death", 5.0),
1038 => ("minecraft:block.end_portal.spawn", 1.0),
_ => return,
};
let at = self.eye_position().map(|eye| {
let d = [
f64::from(pos[0]) + 0.5 - eye[0],
f64::from(pos[1]) + 0.5 - eye[1],
f64::from(pos[2]) + 0.5 - eye[2],
];
let length = (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt();
if length < 1.0e-4 {
return eye;
}
[0, 1, 2].map(|k| eye[k] + d[k] / length * 2.0)
});
// Each guest plays it from the global packet.
self.audio.play_unshared(sound, at, volume, 1.0);
}
/// Queue and play level event `id` at `pos` (`Level.levelEvent`).
pub(super) fn level_event_at(&mut self, id: i32, pos: [i32; 3], data: i32) {
self.level_events.push(LevelEvent::LevelEvent {
+10 -3
View File
@@ -365,7 +365,12 @@ impl Game<'_> {
}
*next += self.clock.tick_duration();
if *next > now {
std::thread::sleep(*next - now);
// A `/pregen` task keeps the workers fed until the next tick.
self.pump_pregen(*next);
let now = Instant::now();
if *next > now {
std::thread::sleep(*next - now);
}
} else if now - *next > Duration::from_secs(2) {
*next = now;
}
@@ -373,7 +378,7 @@ impl Game<'_> {
/// The levels this server tick runs, in `MinecraftServer.levels` order
/// (the Overworld, the Nether, the End): every level with a player in
/// it, or the Overworld while nobody is online.
/// it or a `/pregen` task, or the Overworld while nobody is online.
pub(super) fn levels_to_tick(&self) -> Vec<Dim> {
let guests = self
.lan
@@ -381,7 +386,9 @@ impl Game<'_> {
.map_or(&[][..], |h| h.hub.guests.as_slice());
let mut dims: Vec<Dim> = [Dim::Overworld, Dim::Nether, Dim::End]
.into_iter()
.filter(|d| guests.iter().any(|g| g.dimension == d.id()))
.filter(|d| {
guests.iter().any(|g| g.dimension == d.id()) || self.pregen_level() == Some(*d)
})
.collect();
if dims.is_empty() {
dims.push(Dim::Overworld);
@@ -0,0 +1,176 @@
//! Capability `render` (docs/mods.md): the shapes mods keep on the HUD,
//! drawn by the game's own HUD renderer, and their camera shakes. Mods
//! never supply shader code. With no mod drawing, each hook here returns
//! after one check.
use super::Game;
use horizon_client::hud::HudBatch;
#[cfg(feature = "mods")]
use {
horizon_client::{hud::rgba, items::ItemStack, text::Text},
horizon_mods::Shape,
std::time::Instant,
};
/// The camera's jolts from `render.shake`, while any is fading.
#[cfg(feature = "mods")]
#[derive(Default)]
pub(super) struct Shake {
jolts: Vec<Jolt>,
}
#[cfg(feature = "mods")]
struct Jolt {
start: Instant,
strength: f32,
seconds: f32,
}
/// The HUD batch's colour (ABGR) from a mod's 0xAARRGGBB.
#[cfg(feature = "mods")]
fn argb(color: u32) -> u32 {
let [a, r, g, b] = color.to_be_bytes();
rgba(r, g, b, a)
}
/// The view's turn at full strength, in degrees: yaw, then pitch.
#[cfg(feature = "mods")]
const SHAKE_DEGREES: (f32, f32) = (3.0, 2.0);
#[cfg(feature = "mods")]
impl Shake {
pub(super) fn add(&mut self, strength: f32, seconds: f32) {
self.jolts.push(Jolt {
start: Instant::now(),
strength,
seconds,
});
}
/// How far the view turns now (yaw, pitch degrees), `scale` being the
/// player's Distortion Effects; fades jolts out and forgets ended ones.
fn offset(&mut self, now: Instant, scale: f32) -> (f32, f32) {
self.jolts
.retain(|j| now.duration_since(j.start).as_secs_f32() < j.seconds);
let mut strength = 0.0;
for j in &self.jolts {
let left = 1.0 - now.duration_since(j.start).as_secs_f32() / j.seconds;
strength += j.strength * left * left;
}
let strength = strength.min(1.0) * scale.clamp(0.0, 1.0);
if strength <= 0.0 {
return (0.0, 0.0);
}
// Smooth noise: sines at unrelated rates, so it never repeats visibly.
let t = self
.jolts
.first()
.map_or(0.0, |j| now.duration_since(j.start).as_secs_f32());
let wave = |a: f32, b: f32, c: f32| {
((t * a).sin() * 0.5 + (t * b).sin() * 0.3 + (t * c).sin() * 0.2).clamp(-1.0, 1.0)
};
(
strength * SHAKE_DEGREES.0 * wave(23.0, 37.3, 51.7),
strength * SHAKE_DEGREES.1 * wave(29.1, 43.9, 61.3),
)
}
}
#[cfg(not(feature = "mods"))]
impl Game<'_> {
pub(super) fn draw_mod_shapes(&self, _batch: &mut HudBatch) {}
pub(super) fn shaken_view(&mut self, yaw: f32, pitch: f32) -> (f32, f32) {
(yaw, pitch)
}
}
#[cfg(feature = "mods")]
impl Game<'_> {
/// The running mods' HUD shapes, in order, over the HUD.
pub(super) fn draw_mod_shapes(&self, batch: &mut HudBatch) {
let atlas = &self.gui.atlas;
for shape in self.mods.mods.hud_shapes() {
match shape {
Shape::Fill(f) => {
let r = f.rect;
batch.fill(atlas, r.x, r.y, r.w, r.h, argb(f.color));
}
Shape::Quad(q) => {
let (a, b, c, d) = q.corners;
let corners = [[a.x, a.y], [b.x, b.y], [c.x, c.y], [d.x, d.y]];
batch.fill_quad(atlas, corners, argb(q.color));
}
Shape::Sprite(s) => {
let r = s.rect;
let name = s.sprite.strip_prefix("minecraft:").unwrap_or(&s.sprite);
let name = format!("gui/sprites/{name}");
batch.sprite_scaled(atlas, &name, r.x, r.y, r.w, r.h, argb(s.color));
}
Shape::Item(i) => {
if self.res.items().get(&i.item).is_none() {
continue;
}
let start = batch.vertices.len();
let stack = ItemStack::new(i.item.as_str(), 1);
self.draw_item_model(batch, &stack, i.at.x, i.at.y);
batch.scale_since(start, i.at.x, i.at.y, i.scale, i.scale);
}
Shape::Text(t) => {
let text = Text::literal(t.text.clone());
let x = if t.centered {
t.at.x - (HudBatch::text_width(atlas, &t.text) / 2.0).floor()
} else {
t.at.x
};
batch.text_styled(
atlas,
&text,
x,
t.at.y,
argb(t.color | 0xFF00_0000),
t.shadow,
);
}
}
}
}
/// The view's yaw and pitch after the mods' camera shakes; unchanged
/// while none is fading.
pub(super) fn shaken_view(&mut self, yaw: f32, pitch: f32) -> (f32, f32) {
if self.mods.shake.jolts.is_empty() {
return (yaw, pitch);
}
let scale = self.settings.file.double("screenEffectScale") as f32;
let (dy, dp) = self.mods.shake.offset(Instant::now(), scale);
(yaw + dy, (pitch + dp).clamp(-90.0, 90.0))
}
}
#[cfg(all(test, feature = "mods"))]
mod tests {
use super::*;
use std::time::Duration;
#[test]
fn a_jolt_fades_out_and_distortion_scales_it() {
let mut shake = Shake::default();
shake.add(1.0, 2.0);
let start = shake.jolts[0].start;
let at = |s: f32| start + Duration::from_secs_f32(s);
let size = |(y, p): (f32, f32)| y.abs() + p.abs();
// Off with Distortion Effects at 0, inside the limits otherwise.
assert_eq!(shake.offset(at(0.3), 0.0), (0.0, 0.0));
let (y, p) = shake.offset(at(0.3), 1.0);
assert!(y.abs() <= SHAKE_DEGREES.0 && p.abs() <= SHAKE_DEGREES.1);
// Fading: the largest turn over a window shrinks.
let mut peak = |from: f32| {
(0..50)
.map(|i| size(shake.offset(at(from + i as f32 * 0.002), 1.0)))
.fold(0.0, f32::max)
};
let early = peak(0.1);
let late = peak(1.6);
assert!(late < early, "{late} !< {early}");
// Gone once it ends.
assert_eq!(shake.offset(at(2.1), 1.0), (0.0, 0.0));
assert!(shake.jolts.is_empty());
}
}
+80 -45
View File
@@ -33,7 +33,10 @@ use {
#[derive(Default)]
pub(super) struct ModsState {
#[cfg(feature = "mods")]
mods: Mods,
pub(super) mods: Mods,
/// Capability `render`'s camera shakes.
#[cfg(feature = "mods")]
pub(super) shake: super::mod_render::Shake,
/// `--dev-mod PATH`: always on, reloaded when rebuilt.
pub(super) dev: Vec<PathBuf>,
/// When dev mods were last checked for a rebuild.
@@ -42,6 +45,11 @@ pub(super) struct ModsState {
/// The chat's command tree while a running mod owns commands.
#[cfg(feature = "mods")]
tree: Option<Dispatcher>,
/// `command.run` lines for this window's own world, run on the next tick:
/// run at once, their chat lines would reach the mods while their output
/// is still being shown, and a mod answering its own lines would recurse.
#[cfg(feature = "mods")]
commands: Vec<String>,
/// The server's command tree with the running mods' commands added.
#[cfg(feature = "mods")]
remote_tree: Option<Dispatcher>,
@@ -333,10 +341,19 @@ impl Game<'_> {
game_time: self.world.game_time.max(0) as u64,
day_time: self.world.day_time.max(0) as u64,
});
Snapshot { player, clock }
Snapshot {
player,
clock,
gui_size: self.gui_size(),
}
}
/// A game tick.
pub(super) fn tick_mods(&mut self) {
// Here, at the player's level: shown inside a console call (a control
// socket command set the hook off), they would run as the console.
for line in std::mem::take(&mut self.mods.commands) {
self.run_command(&line);
}
if !self.mods.mods.is_running() {
return;
}
@@ -396,6 +413,19 @@ impl Game<'_> {
}
Output::Feedback { text, error: false } => Text::literal(text),
Output::Feedback { text, error: true } => Text::literal(text).with_color(text::RED),
// Vanilla's commands only: a mod running a mod command could loop.
Output::Command { line } => {
let name = line.split(' ').next().unwrap_or_default();
if !vanilla_command(name) {
// Dropped.
} else if self.remote.is_some() {
// The server checks the player's permissions.
self.send_chat(&format!("/{line}"));
} else {
self.mods.commands.push(line);
}
continue;
}
Output::Stopped { name, reason } => {
// Its screen goes with it: the entry's run is gone, so
// `mod_ui_view` finds no widgets and the screen closes.
@@ -407,6 +437,10 @@ impl Game<'_> {
self.open_mod_screen(owner, false);
continue;
}
Output::Shake { strength, seconds } => {
self.mods.shake.add(strength, seconds);
continue;
}
Output::CloseScreen { owner } => {
if self.mods.screen.as_ref().is_some_and(|s| s.owner == owner) {
self.close_mod_screen(false);
@@ -450,30 +484,28 @@ impl Game<'_> {
}
/// A `ui` screen's title and widgets as the mod last pushed them; None
/// when the surface is gone (the mod stopped or closed it).
fn mod_ui_view(&self) -> Option<(String, Vec<UiWidget>)> {
let ms = self.mods.screen.as_ref()?;
/// when the surface is gone (the mod stopped or closed it). Borrows
/// `mods` only, so `self.mods.screen` may stay borrowed for the scrolls.
fn mod_ui_view<'a>(mods: &'a Mods, ms: &ModScreen) -> Option<(&'a str, &'a [UiWidget])> {
if ms.settings {
let widgets = self.mods.mods.settings_widgets(ms.owner);
let widgets = mods.settings_widgets(ms.owner);
if widgets.is_empty() {
return None;
}
let name = self
.mods
.mods
let name = mods
.entries()
.get(ms.owner)
.and_then(|e| e.manifest.as_ref())
.map_or("Mod", |m| m.name.as_str());
Some((name.to_string(), widgets.to_vec()))
Some((name, widgets))
} else {
let s: &UiScreen = self.mods.mods.screen(ms.owner)?;
Some((s.title.clone(), s.widgets.clone()))
let s: &UiScreen = mods.screen(ms.owner)?;
Some((s.title.as_str(), s.widgets.as_slice()))
}
}
/// The layout the mod screen's widgets get at `h`: (widget, top,
/// height), the column being `COLUMN` wide down the middle.
fn mod_ui_layout(&self, h: f32, widgets: &[UiWidget]) -> Vec<(f32, f32)> {
fn mod_ui_layout(h: f32, widgets: &[UiWidget]) -> Vec<(f32, f32)> {
let mut out = Vec::with_capacity(widgets.len());
let mut y = MOD_UI_TOP;
for w in widgets {
@@ -538,13 +570,19 @@ impl Game<'_> {
/// The mod's screen or settings page: its title over widgets stacked
/// down the middle, a Done button, Esc to leave (vanilla's menus).
pub(super) fn draw_mod_screen(&mut self, batch: &mut HudBatch) {
if self.mods.screen.is_none() {
let Some(ms) = self.mods.screen.as_mut() else {
// The screen was dropped while it was up: back to the world,
// mouse held again like any return to Playing.
self.screen = Screen::Playing;
if self.player.is_some() {
self.capture(true);
}
return;
}
let Some((title, widgets)) = self.mod_ui_view() else {
self.mods.screen = None;
self.screen = Screen::Playing;
};
let Some((title, widgets)) = Self::mod_ui_view(&self.mods.mods, ms) else {
// The mod's surface is gone (it stopped or cleared it): leave
// the screen like Done did, without `closed` — the mod ended it.
self.close_mod_screen(false);
return;
};
// Item icons draw in a second pass: `draw_item` needs `&mut self`,
@@ -564,7 +602,7 @@ impl Game<'_> {
);
let cx = (batch.width / 2.0).floor();
let x = cx - MOD_UI_COLUMN / 2.0;
let layout = self.mod_ui_layout(batch.height, &widgets);
let layout = Self::mod_ui_layout(batch.height, widgets);
let mut list_i = 0usize;
for (w, &(wy, wh)) in widgets.iter().zip(&layout) {
match w {
@@ -605,11 +643,8 @@ impl Game<'_> {
);
}
UiWidget::List(l) => {
let scroll = {
let ms = self.mods.screen.as_mut().expect("checked");
ms.scrolls.resize_with(list_i + 1, ui::Scroll::default);
ms.scrolls[list_i]
};
ms.scrolls.resize_with(list_i + 1, ui::Scroll::default);
let scroll = ms.scrolls[list_i];
list_i += 1;
let rows_top = wy + 4.0;
let rows_bottom = wy + wh - 4.0;
@@ -680,53 +715,53 @@ impl Game<'_> {
self.close_mod_screen(true);
return;
}
let Some((_, widgets)) = self.mod_ui_view() else {
let Some(ms) = self.mods.screen.as_ref() else {
return;
};
let Some((_, widgets)) = Self::mod_ui_view(&self.mods.mods, ms) else {
return;
};
let owner = ms.owner;
let x = (w / 2.0).floor() - MOD_UI_COLUMN / 2.0;
let layout = self.mod_ui_layout(h, &widgets);
let layout = Self::mod_ui_layout(h, widgets);
let mut list_i = 0usize;
// The `ui_select` call needs `&mut`: find the pick first.
let mut hit = None;
for (w, &(wy, wh)) in widgets.iter().zip(&layout) {
let UiWidget::List(l) = w else { continue };
let scroll = self
.mods
.screen
.as_ref()
.and_then(|ms| ms.scrolls.get(list_i).copied())
.unwrap_or_default();
let scroll = ms.scrolls.get(list_i).copied().unwrap_or_default();
list_i += 1;
let row = ((cursor.1 - (wy + 4.0) + scroll.amount) / 12.0) as i64;
if ui::hovered(cursor, x + 1.0, wy + 1.0, MOD_UI_COLUMN - 7.0, wh - 2.0)
&& row >= 0
&& (row as usize) < l.rows.len()
{
self.audio.play_button_click();
self.mods.mods.ui_select(
self.mods.screen.as_ref().map_or(0, |ms| ms.owner),
l.id,
row as u32,
);
self.show_mod_output();
return;
hit = Some((l.id, row as u32));
break;
}
}
if let Some((id, row)) = hit {
self.audio.play_button_click();
self.mods.mods.ui_select(owner, id, row);
self.show_mod_output();
}
}
/// The wheel over a `list` scrolls it.
pub(super) fn mod_screen_scroll(&mut self, delta: f32) {
let (w, h) = self.gui_size();
let cursor = self.cursor;
let Some((_, widgets)) = self.mod_ui_view() else {
let Some(ms) = self.mods.screen.as_mut() else {
return;
};
let Some((_, widgets)) = Self::mod_ui_view(&self.mods.mods, ms) else {
return;
};
let x = (w / 2.0).floor() - MOD_UI_COLUMN / 2.0;
let layout = self.mod_ui_layout(h, &widgets);
let layout = Self::mod_ui_layout(h, widgets);
let mut list_i = 0usize;
for (w, &(wy, wh)) in widgets.iter().zip(&layout) {
let UiWidget::List(l) = w else { continue };
let over = ui::hovered(cursor, x, wy, MOD_UI_COLUMN, wh);
let Some(ms) = self.mods.screen.as_mut() else {
return;
};
ms.scrolls.resize_with(list_i + 1, ui::Scroll::default);
if over {
let content = l.rows.len() as f32 * 12.0;
+36
View File
@@ -240,6 +240,42 @@ impl Game<'_> {
discrete: false,
});
}
// ItemFrameRenderer.shouldShowName/getNameTag: only the targeted
// frame's custom-named item, with the HUD visible (also glow frames).
if !self.ui.hide_gui
&& let Some((i, distance)) = self.frame_hit()
&& self.before_block(distance)
&& self.mob_hit().is_none_or(|(_, d)| d > distance)
&& let Some(name) = self.item_frames[i]
.item
.as_ref()
.and_then(|item| item.custom_name())
{
let frame = &self.item_frames[i];
let (lo, hi) = frame.bounds();
let center = glam::DVec3::from_array(std::array::from_fn(|a| (lo[a] + hi[a]) / 2.0));
if center.distance_squared(camera) < 64.0 * 64.0 {
// NAME_TAG is at the entity height (0.5), then the name
// submitter lifts it 0.5. ItemFrameRenderer offsets rendering
// 0.3 toward its facing and -0.25 vertically.
let step = frame.step();
tags.push(NameTag {
at: center
+ glam::DVec3::new(
f64::from(step[0] as f32 * 0.3),
0.75,
f64::from(step[2] as f32 * 0.3),
),
lines: vec![(Text::literal(name).resolve_legacy(), 0.0)],
light: self.world.light(
center.x.floor() as i32,
center.y.floor() as i32,
center.z.floor() as i32,
),
discrete: false,
});
}
}
tags
}
/// `SubmitNodeCollection.submitNameTag`: a discrete name is drawn once,
+6
View File
@@ -142,6 +142,8 @@ impl Game<'_> {
}
let server = &self.server_packs.active;
let root = startup_root(&vanilla, &self.settings.resource_packs, server)?;
let root =
super::mods::content_root(&root, &self.settings.mods.enabled, &self.options.dev_mods);
self.reload_resources(&root)
}
fn reload_resources(&mut self, root: &Path) -> horizon_client::Result<()> {
@@ -192,6 +194,10 @@ impl Game<'_> {
let context = pack.context.clone();
self.pack = pack;
self.gui = gui;
// The atlas's sprites moved: the particle engine re-reads every rect
// and the packs' sprite sets, or every particle draws a stale region.
self.particles
.reload(root, &self.gui.atlas.sprites, &self.gui.atlas.translucent);
horizon_client::ui_theme::reset_atlas();
// Villager hats come from the pack's texture metadata (shared data, swapped in place).
*self
+20 -10
View File
@@ -234,6 +234,22 @@ fn number_format(f: &play::NumberFormat) -> horizon_client::scoreboard::NumberFo
}
impl Game<'_> {
/// A `Holder<SoundEvent>` off the wire: an inline event's location, or
/// the name of a registered one.
pub(super) fn remote_sound_name(
&self,
sound: &(i32, Option<(String, Option<f32>)>),
) -> Option<String> {
match &sound.1 {
Some((location, _)) => Some(location.clone()),
None => self.remote.as_ref().and_then(|r| {
r.builtin
.get("minecraft:sound_event")?
.name(sound.0)
.map(str::to_owned)
}),
}
}
/// Leave the title for a server (ConnectScreen): an empty world fed by the connection.
pub(super) fn connect_server(&mut self, address: &str, name: &str) -> Result<()> {
self.connect_server_with_cookies(address, name, None)
@@ -1710,20 +1726,14 @@ impl Game<'_> {
// ClientLevel.levelEvent: LevelEventHandler, as this client's own
// level plays them (particles and sounds: a spawner's burst, bone
// meal, waxing, a block broken by someone else).
// ClientPacketListener.handleLevelEvent: a global one is heard
// from anywhere (ClientLevel.globalLevelEvent).
C::LevelEvent(p) if p.global => self.global_level_event_sound(p.kind, p.pos),
C::LevelEvent(p) => self.level_event_at(p.kind, p.pos, p.data),
C::LevelParticles(p) => self.remote_level_particles(&p),
C::Explode(p) => self.remote_explode(&p),
C::Sound(p) => {
let name = match &p.sound.1 {
Some((location, _)) => Some(location.clone()),
None => self.remote.as_ref().and_then(|r| {
r.builtin
.get("minecraft:sound_event")?
.name(p.sound.0)
.map(str::to_owned)
}),
};
if let Some(name) = name {
if let Some(name) = self.remote_sound_name(&p.sound) {
let at = [
f64::from(p.x) / 8.0,
f64::from(p.y) / 8.0,
+196 -4
View File
@@ -4,9 +4,24 @@
use horizon_client::camera::CameraFluid;
use horizon_client::gfx::pack_uniforms::PackInputs;
use horizon_shaders::{Macros, Pack, Properties, dimension_folder, load_post};
use horizon_shaders::{BlockIds, Macros, Pack, Properties, dimension_folder, load_post};
use super::{Dim, Game};
use horizon_client::gfx::pack_draw::EntitySpan;
use std::cell::RefCell;
use std::collections::HashMap;
/// The ids a pack gives entities and block entities (4.1, 5.3), for the spans of the entity
/// geometry.
pub(super) struct PackIds {
entities: HashMap<String, i32>,
blocks: BlockIds,
/// `blockEntityId` by block state, filled as states are drawn.
block_states: RefCell<HashMap<u32, i32>>,
/// The id of a name the file lacks: 65535, or 0 without the file.
missing_entity: i32,
missing_block: i32,
}
impl Game<'_> {
/// Loads the pack for the current dimension when it changed, and returns this frame's
@@ -42,8 +57,38 @@ impl Game<'_> {
);
}
let folder = dimension_folder(&pack, dimension);
let post = load_post(&pack, &folder, &properties, &macros, None);
if let Some((programs, notes)) = renderer.set_shader_pack(dimension, Some(post))
let inputs = renderer.pack_inputs();
let mut post = load_post(&pack, &folder, &properties, &macros, &inputs);
let biomes = self.res.biomes();
let names: Vec<&str> = biomes.list.iter().map(|b| b.name.as_str()).collect();
post.custom.set_biomes(&names);
let text = pack.read_text("block.properties").unwrap_or_default();
let ids = BlockIds::parse(&Properties::parse(&text, &macros));
for note in &ids.notes {
log::warn!("shader pack {name}: block.properties: {note}");
}
let text = pack.read_text("entity.properties");
let entities = BlockIds::parse_kind(
&Properties::parse(text.as_deref().unwrap_or_default(), &macros),
"entity",
);
self.pack_ids = Some(PackIds {
entities: entities.plain(),
missing_entity: if text.is_some() { 65535 } else { 0 },
missing_block: if ids.is_empty() { 0 } else { 65535 },
blocks: ids.clone(),
block_states: RefCell::default(),
});
let started_ids = std::time::Instant::now();
let table = block_id_table(&self.pack, &ids);
log::info!(
"shader pack {name}: mc_Entity for {} of {} block sprites in {} ms",
table.chunks(2).filter(|p| p[0] != -1).count(),
table.len() / 2,
started_ids.elapsed().as_millis()
);
if let Some((programs, notes)) =
renderer.set_shader_pack(dimension, Some(post), &table)
{
for note in notes {
log::warn!("shader pack {name}: {note}");
@@ -57,14 +102,21 @@ impl Game<'_> {
}
}
Err(e) => {
self.pack_ids = None;
log::warn!("shader pack {name}: {e}");
renderer.set_shader_pack(dimension, None);
renderer.set_shader_pack(dimension, None, &[]);
}
}
}
let lerp = |(old, new): (f32, f32)| old + (new - old) * partial;
let at = camera.floor().as_ivec3();
let light = self.world.light(at.x, at.y, at.z);
let biome = self
.world
.chunks
.get(&(at.x >> 4, at.z >> 4))
.map(|c| c.biome(at.x, at.y, at.z));
let info = biome.map(|b| self.res.biomes().get(b));
PackInputs {
day_time: self.world.day_time,
rain: lerp(self.world.weather.rain_level),
@@ -79,6 +131,146 @@ impl Game<'_> {
eye_light: [i32::from(light & 15), i32::from(light >> 4)],
hide_gui: self.ui.hide_gui,
brightness: self.settings.gamma,
biome: biome.map_or(-1, i32::from),
biome_category: info.map_or(0, |b| horizon_shaders::biome_category(&b.name)),
biome_precipitation: info.map_or(0, |b| {
horizon_shaders::biome_precipitation(b.precipitation, b.temperature)
}),
temperature: info.map_or(0.0, |b| b.temperature),
rainfall: info.map_or(0.0, |b| b.downfall),
}
}
}
impl Game<'_> {
/// Starts a span of the entity geometry at vertex `start` for entity type `name`
/// (`minecraft:zombie`; "" for one the pack cannot name). Nothing without a pack.
pub(super) fn mark_entity(&self, start: usize, name: &str) {
if let Some(ids) = &self.pack_ids {
let id = ids.entities.get(name).copied();
self.push_span(start, "ENTITIES", id.unwrap_or(ids.missing_entity));
}
}
/// Starts a span for the block entity of block state `state`.
pub(super) fn mark_block_entity(&self, start: usize, state: u32) {
let Some(ids) = &self.pack_ids else { return };
let id = *ids
.block_states
.borrow_mut()
.entry(state)
.or_insert_with(|| {
let registry = self.pack.assets.registry;
registry
.state(horizon_world::registry::BlockStateId(state))
.and_then(|s| {
ids.blocks
.id(&s.name, |k| s.properties.get(k).map(String::as_str))
})
.unwrap_or(ids.missing_block)
});
self.push_span(start, "BLOCK_ENTITIES", id);
}
/// Starts a span of geometry that is not an entity (`PARTICLES`, `DESTROY`...).
pub(super) fn mark_stage(&self, start: usize, stage: &str) {
if self.pack_ids.is_some() {
self.push_span(start, stage, 0);
}
}
fn push_span(&self, start: usize, stage: &str, id: i32) {
let mut spans = self.entity_spans.borrow_mut();
let span = EntitySpan {
start: start as u32,
stage: horizon_shaders::render_stage(stage),
id,
};
// A span that drew nothing gives way to the next.
match spans.last_mut() {
Some(last) if last.start == span.start => *last = span,
_ => spans.push(span),
}
}
}
/// `mc_Entity` (x, y) of each block sprite layer, as (id, fluid) pairs: terrain vertices carry
/// their sprite layer, not their block. A layer takes the id of the block named like its sprite
/// (`block/oak_leaves` for `oak_leaves`), else the id most blocks drawing it share (ties: the
/// lowest); -1 when no block drawing it has one. y is 1 for the fluid sprites, -1 otherwise.
fn block_id_table(pack: &super::PackResources, ids: &BlockIds) -> Vec<i32> {
use std::collections::{BTreeMap, BTreeSet};
let layers = pack
.render
.sprite_layers
.values()
.max()
.map_or(0, |l| l + 1) as usize;
let mut table = vec![-1; layers * 2];
if ids.is_empty() {
return table;
}
let registry = pack.assets.registry;
let catalog = &pack.render.model_catalog;
let sprite = |name: &str| pack.render.sprite_layers.get(name).copied();
// (layer, id) -> (a block named like the sprite, the blocks).
let mut votes: BTreeMap<(u32, i32), (bool, BTreeSet<&str>)> = BTreeMap::new();
for i in 0..registry.len() {
let state_id = horizon_world::registry::BlockStateId(i as u32);
let Some(state) = registry.state(state_id) else {
continue;
};
let Some(id) = ids.id(&state.name, |k| state.properties.get(k).map(String::as_str)) else {
continue;
};
let block = state.name.strip_prefix("minecraft:").unwrap_or(&state.name);
let mut vote = |layer: u32| {
let named = pack
.layer_names
.get(&layer)
.and_then(|n| n.strip_prefix("block/"))
== Some(block);
let v = votes.entry((layer, id)).or_default();
v.0 |= named;
v.1.insert(&state.name);
};
// Fluids draw from their own sprites.
let fluid = match block {
"water" => ["water_still", "water_flow"],
"lava" => ["lava_still", "lava_flow"],
_ => ["", ""],
};
for f in fluid.iter().filter(|f| !f.is_empty()) {
if let Some(l) = sprite(&format!("minecraft:block/{f}")) {
vote(l);
}
}
let origin = horizon_core::position::BlockPos::new(0, 0, 0);
for (_, model) in catalog.collect(state_id, origin).unwrap_or_default() {
for face in &model.faces {
vote(face.layer);
}
}
}
let mut best: BTreeMap<u32, (bool, usize, i32)> = BTreeMap::new();
for ((layer, id), (named, blocks)) in votes {
let candidate = (named, blocks.len(), -id);
let b = best.entry(layer).or_insert(candidate);
if candidate > *b {
*b = candidate;
}
}
for (layer, (_, _, id)) in best {
if let Some(slot) = table.get_mut(layer as usize * 2) {
*slot = -id;
}
}
for f in ["water_still", "water_flow", "lava_still", "lava_flow"] {
if let Some(l) = sprite(&format!("minecraft:block/{f}"))
&& let Some(slot) = table.get_mut(l as usize * 2 + 1)
{
*slot = 1;
}
}
table
}
@@ -272,8 +272,6 @@ impl Loading {
}
pub(super) fn apply_menu_settings(&mut self) {
if let Some(renderer) = &mut self.renderer {
self.settings
.clamp_gui_scale([renderer.config.width as i32, renderer.config.height as i32]);
renderer.gamma = self.settings.gamma;
renderer.set_present_mode(self.settings.vsync, self.settings.horizon.present_mode);
}
+186 -26
View File
@@ -69,7 +69,10 @@ pub mod lightning;
mod lod;
mod occlusion;
mod pack_atlas;
pub mod pack_draw;
mod pack_post;
mod pack_sky;
mod pack_timing;
pub mod pack_uniforms;
mod page_runs;
pub mod panorama;
@@ -86,6 +89,19 @@ const COMPLETE_COLUMN: u32 = (1 << (crate::chunk::HEIGHT / 16)) - 1;
/// its fixed-point UVs (`terrain.wgsl`'s `terrain_uv`).
/// The world pass again after occlusion culling's test: it loads both
/// targets (phase 2 draws over phase 1, reading the test's results).
/// LevelRenderer: no sky inside lava or powder snow (sky fog ends within 2 blocks).
fn sky_open(frame: &Frame) -> bool {
match frame.fog {
Some(f) => f.sky_end > 2.0,
None => !frame.underwater,
}
}
/// The Overworld-style sky (disc, sunset, stars, void) draws this frame.
fn sky_visible(frame: &Frame) -> bool {
!frame.title && frame.skybox && sky_open(frame)
}
fn world_pass_after_culling<'e>(
encoder: &'e mut wgpu::CommandEncoder,
view: &wgpu::TextureView,
@@ -232,6 +248,8 @@ pub struct Frame<'a> {
pub nether_lighting: bool,
/// Camera-relative entity geometry textured from the shared atlas.
pub entities: &'a [Vertex],
/// Runs of `entities` by entity, while a shader pack draws them (`pack_draw::EntitySpan`).
pub entity_spans: &'a [pack_draw::EntitySpan],
pub weather: &'a [crate::weather::Vertex],
pub weather_time: crate::weather::Time,
/// Horizon Extras' dynamic lights, nearest first (`dynamic_light::select`); empty when off.
@@ -475,6 +493,8 @@ pub struct Renderer {
pack_post: Option<(i32, Option<pack_post::PackPost>)>,
/// The block sprites as one atlas, while a shader pack is loaded.
pack_atlas: Option<pack_atlas::PackAtlas>,
/// The pack's terrain program reads flat draws' vertices (`pack_terrain_inputs`).
pack_terrain_flat: bool,
pub hud: HudRenderer,
/// Vanilla's menu background blur, made on first use.
menu_blur: Option<crate::menu_blur::MenuBlur>,
@@ -842,12 +862,17 @@ async fn open_device(
continue;
}
// Mappable primary buffers: section pages that no longer fit in
// video memory go to system memory.
// video memory go to system memory. Shader packs' 16-bit normalized buffers (render
// targets with the adapter's own format features) and R11F_G11F_B10F: a float
// stand-in neither clamps nor drops NaN.
let features = timing::GpuTimer::features(&adapter)
| cull::Cull::features(&adapter)
| (adapter.features()
& (wgpu::Features::INDIRECT_FIRST_INSTANCE
| wgpu::Features::MAPPABLE_PRIMARY_BUFFERS));
| wgpu::Features::MAPPABLE_PRIMARY_BUFFERS
| wgpu::Features::TEXTURE_FORMAT_16BIT_NORM
| wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES
| wgpu::Features::RG11B10UFLOAT_RENDERABLE));
let driver = format!("{} {}", info.driver, info.driver_info);
startup_step(format_args!(
"requesting the device (driver {}) with features [{features}] and the adapter's \
@@ -2149,13 +2174,16 @@ impl Renderer {
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let flat_wanted = flat::wanted(&adapter, &device);
// Readable as storage where flat draws run: a shader pack's terrain program reads them.
let arena = arena::Arena::new(
&device,
std::mem::size_of::<TerrainVertex>() as u64,
1 << 20,
flat_wanted,
);
// Sections as one index list per frame where multi-draw is emulated.
let flat = flat::wanted(&adapter, &device).then(|| {
let flat = flat_wanted.then(|| {
let buffers = [Some(wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<TerrainVertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
@@ -2363,6 +2391,7 @@ impl Renderer {
world_post,
pack_post: None,
pack_atlas: None,
pack_terrain_flat: false,
hud,
menu_blur: None,
info,
@@ -2793,24 +2822,77 @@ impl Renderer {
self.world_post.scale = scale.clamp(0.25, 2.0);
self.world_post.antialias = antialias;
}
/// Runs `post` over the world from now on (`None`: no pack), for the dimension `dimension`.
/// Runs `post` over the world from now on (`None`: no pack), for the dimension `dimension`;
/// `block_ids` gives each block sprite layer's `mc_Entity` (id, fluid) pair.
/// Returns the programs that run and the notes of loading them.
pub fn set_shader_pack(
&mut self,
dimension: i32,
pack: Option<horizon_shaders::Post>,
block_ids: &[i32],
) -> Option<(Vec<String>, Vec<String>)> {
if pack.is_none() {
self.pack_atlas = None;
}
let instance = wgpu::vertex_attr_array![5=>Float32x4];
let vertex = wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<TerrainVertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &TERRAIN_ATTRIBUTES,
};
let instances = wgpu::VertexBufferLayout {
array_stride: 16,
step_mode: wgpu::VertexStepMode::Instance,
attributes: &instance,
};
let buffers = [Some(vertex), Some(instances)];
let flat = self.pack_terrain_flat;
let post = pack.map(|post| {
let world = pack_post::World {
atlas: self.pack_atlas.as_ref().map(|a| (a.view.clone(), a.size())),
lightmap: self.lightmap_view.clone(),
terrain: self.pack_atlas.as_ref().map(|_| pack_post::Geometry {
buffers: if flat { &buffers[..1] } else { &buffers },
table: self.flat.as_ref().filter(|_| flat).map(|f| &f.draw_layout),
depth_write: true,
atlas: None,
ids: None,
cull: true,
}),
block_ids,
celestial: self
.hud
.atlas_texture
.as_ref()
.map(|t| t.create_view(&Default::default())),
};
pack_post::PackPost::new(
&self.device,
&self.queue,
post,
self.config.format.remove_srgb_suffix(),
world,
)
});
if post.is_none() {
self.pack_atlas = None;
} else if self.pack_atlas.is_none() {
let report = post.as_ref().map(|p| {
let programs = p.programs().into_iter().map(str::to_owned).collect();
(programs, p.notes.clone())
});
self.pack_post = Some((dimension, post));
report
}
/// What a shader pack's gbuffers programs read from Horizon's geometry: the sky's, and
/// terrain's in the pack's block atlas (made here when missing; none when it does not fit).
pub fn pack_inputs(&mut self) -> horizon_shaders::GameInputs {
horizon_shaders::GameInputs {
terrain: self.pack_terrain_inputs(),
sky: Some(pack_sky::inputs()),
celestial: Some(pack_sky::celestial_inputs()),
}
}
fn pack_terrain_inputs(&mut self) -> Option<horizon_shaders::GbuffersInputs> {
if self.pack_atlas.is_none() {
let mut encoder = self.device.create_command_encoder(&Default::default());
let layers = self
.sprite_textures
@@ -2827,12 +2909,13 @@ impl Renderer {
);
self.queue.submit([encoder.finish()]);
}
let report = post.as_ref().map(|p| {
let programs = p.programs().into_iter().map(str::to_owned).collect();
(programs, p.notes.clone())
});
self.pack_post = Some((dimension, post));
report
// Flat draws whenever the device has them (`render` keeps them on with a pack).
self.pack_terrain_flat = self.flat.is_some() && self.flat_on;
Some(
self.pack_atlas
.as_ref()?
.terrain_inputs(self.pack_terrain_flat),
)
}
/// The dimension the shader pack was last set for.
pub fn shader_pack_dimension(&self) -> Option<i32> {
@@ -3243,11 +3326,23 @@ impl Renderer {
// Occlusion culling leaves the sections its last result did not see
// to phase 2, after the others, with instance counts of 0. Whether
// the frame has draws enough goes by the last one's.
// A shader pack drawing terrain draws all of it: occlusion culling reads the depth of
// Horizon's own terrain draw.
let pack_terrain = !frame.title
&& self
.pack_post
.as_ref()
.and_then(|p| p.1.as_ref())
.is_some_and(|p| p.has_terrain());
let mut cull = self
.cull
.as_mut()
.filter(|c| {
self.cull_on && !frame.title && self.multi_draw && visible.len() >= c.min_sections
self.cull_on
&& !frame.title
&& !pack_terrain
&& self.multi_draw
&& visible.len() >= c.min_sections
})
.and_then(|cull| cull.begin(&self.device).then_some(cull));
self.phase2_draws.clear();
@@ -3653,7 +3748,7 @@ impl Renderer {
Some(flat)
if self.flat_on
&& !self.draws.is_empty()
&& self.drawn_sections >= flat.min_sections =>
&& (self.drawn_sections >= flat.min_sections || pack_terrain) =>
{
let sections = &self.sections;
let lods = &self.lods;
@@ -3713,6 +3808,26 @@ impl Renderer {
}
// The world's targets: the screen, or the scaled target of `post`.
let pack_post = !frame.title && self.pack_post.as_ref().is_some_and(|p| p.1.is_some());
// The pack's program reads the vertices this frame's draws bind.
let pack_draws_terrain = pack_terrain && flat_frame == self.pack_terrain_flat;
let pack_draws_sky = pack_post
&& self
.pack_post
.as_ref()
.and_then(|p| p.1.as_ref())
.is_some_and(|p| p.has_sky());
let pack_draws_celestial = pack_post
&& self
.pack_post
.as_ref()
.and_then(|p| p.1.as_ref())
.is_some_and(|p| p.has_celestial());
let pack_draws_water = pack_draws_terrain
&& self
.pack_post
.as_ref()
.and_then(|p| p.1.as_ref())
.is_some_and(|p| p.has_water());
let scaled = self.world_post.prepare(
&self.device,
[self.config.width, self.config.height],
@@ -3766,13 +3881,9 @@ impl Renderer {
..Default::default()
});
pass.set_bind_group(0, &self.group, &[]);
// LevelRenderer: no sky inside lava or powder snow (sky fog ends within 2 blocks).
let sky_visible = match frame.fog {
Some(f) => f.sky_end > 2.0,
None => !frame.underwater,
};
let sky_visible = sky_open(frame);
if frame.title || (sky_visible && (frame.skybox || frame.end_sky)) {
if !frame.title && frame.skybox {
if !frame.title && frame.skybox && !pack_draws_sky {
pass.set_pipeline(&self.sky);
pass.draw(0..3, 0..1);
}
@@ -3786,22 +3897,29 @@ impl Renderer {
if frame.end_sky {
self.end_sky.draw(&mut pass);
}
if celestial_vertices > 0 {
if celestial_vertices > 0 && !pack_draws_celestial {
pass.set_vertex_buffer(0, self.celestial_buffer.slice(..));
pass.draw_indexed(0..(celestial_vertices as u32 / 4) * 6, 0, 0..1);
}
pass.set_bind_group(0, &self.group, &[]);
}
if !frame.title && frame.stars.is_some() {
if !frame.title && frame.stars.is_some() && !pack_draws_sky {
self.stars.draw(&mut pass);
pass.set_bind_group(0, &self.group, &[]);
}
if !frame.title && frame.dark_disc {
if !frame.title && frame.dark_disc && !pack_draws_sky {
pass.set_pipeline(&self.dark_disc);
pass.draw(0..3, 0..1);
}
}
match self.flat.as_ref().filter(|_| flat_frame) {
match self
.flat
.as_ref()
.filter(|_| flat_frame && !pack_draws_terrain)
{
_ if pack_draws_terrain => {
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
}
Some(flat) => {
pass.set_pipeline(&flat.solid);
let vertices = |p| self.arena.buffer(p);
@@ -3937,6 +4055,8 @@ impl Renderer {
pass.draw(0..4, debug_depth..debug_depth + debug_top);
}
match self.flat.as_ref().filter(|_| flat_frame) {
// The pack draws translucent terrain after its deferred programs.
_ if pack_draws_water => {}
Some(flat) => {
pass.set_pipeline(&flat.translucent);
flat.draw(&mut pass, &flat.calls[flat.solid_calls..], |p| {
@@ -4025,9 +4145,46 @@ impl Renderer {
}
if let Some((color, depth)) = &scaled
&& pack_post
&& let Some((_, Some(post))) = &mut self.pack_post
&& let Some((dimension, Some(mut post))) = self.pack_post.take()
{
let size = self.world_post.size().unwrap_or([1, 1]);
let flat = self.flat.as_ref().filter(|_| flat_frame);
let pages: Vec<(u32, wgpu::Buffer)> = match flat {
Some(_) if pack_draws_terrain => self
.arena
.page_buffers()
.map(|(p, b)| (p, b.clone()))
.collect(),
_ => Vec::new(),
};
let mut terrain =
|rp: &mut wgpu::RenderPass<'_>,
translucent: bool,
page: &dyn Fn(&mut wgpu::RenderPass<'_>, u32)| {
if !pack_draws_terrain {
return;
}
match flat {
Some(flat) => {
let calls = if translucent {
&flat.calls[flat.solid_calls..]
} else {
&flat.calls[..flat.solid_calls]
};
flat.draw_with(rp, calls, |p| self.arena.buffer(p), page);
}
None => {
rp.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
rp.set_vertex_buffer(1, self.instances.slice(..));
let (first, count) = if translucent {
(solid_draws, translucent_draws)
} else {
(0, solid_draws)
};
self.draw_sections(rp, &self.indirect, 0, first, count);
}
}
};
post.run(
&self.device,
&self.queue,
@@ -4039,7 +4196,10 @@ impl Renderer {
&frame.pack,
size,
[self.config.width, self.config.height],
&pages,
&mut terrain,
);
self.pack_post = Some((dimension, Some(post)));
} else if scaled.is_some() {
self.world_post.finish(&mut encoder, &view_target);
}
+16 -4
View File
@@ -65,8 +65,15 @@ pub struct Arena {
}
impl Arena {
pub fn new(device: &wgpu::Device, stride: u64, capacity: u64) -> Self {
Self::with_usage(device, stride, capacity, wgpu::BufferUsages::VERTEX)
/// Vertices; `storage` also lets shaders read them as a storage buffer (a shader pack's
/// terrain program reads the other corners of a vertex's quad for its normal).
pub fn new(device: &wgpu::Device, stride: u64, capacity: u64, storage: bool) -> Self {
let usage = if storage {
wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::STORAGE
} else {
wgpu::BufferUsages::VERTEX
};
Self::with_usage(device, stride, capacity, usage)
}
/// Sorted translucent indices; `storage` when compute passes read them.
pub fn indices(device: &wgpu::Device, capacity: u64, storage: bool) -> Self {
@@ -148,6 +155,11 @@ impl Arena {
pub fn has_page(&self, page: u32) -> bool {
self.pages.get(page as usize).is_some_and(Option::is_some)
}
/// Each live page's buffer, by page.
pub fn page_buffers(&self) -> impl Iterator<Item = (u32, &wgpu::Buffer)> {
let pages = self.pages.iter().enumerate();
pages.filter_map(|(i, p)| Some((i as u32, &p.as_ref()?.buffer)))
}
pub fn buffer(&self, page: u32) -> &wgpu::Buffer {
&self.pages[page as usize]
.as_ref()
@@ -352,7 +364,7 @@ mod tests {
eprintln!("no GPU adapter: skipped");
return;
};
let mut arena = Arena::new(&device, 4, GRAIN);
let mut arena = Arena::new(&device, 4, GRAIN, false);
arena.page_limit = 2 * GRAIN;
assert_eq!(arena.bytes(), 0);
let (first, a) = alloc(&mut arena, &device, &queue, GRAIN).unwrap();
@@ -395,7 +407,7 @@ mod tests {
return;
};
for mut arena in [
Arena::new(&device, 28, GRAIN),
Arena::new(&device, 28, GRAIN, false),
Arena::indices(&device, GRAIN, false),
] {
alloc(&mut arena, &device, &queue, GRAIN).unwrap();
+14 -1
View File
@@ -73,7 +73,8 @@ pub struct Flat {
phase2_call_data: Vec<[u32; 5]>,
/// The copy of phase 2's draws, and its ranges.
phase2_group: Option<(wgpu::BindGroup, u32)>,
draw_layout: wgpu::BindGroupLayout,
/// The layout of a page's bind group 1 (shader packs' terrain pipelines share it).
pub(super) draw_layout: wgpu::BindGroupLayout,
blocks_per_page: u64,
pages: Vec<Option<PageTable>>,
free_ids: Vec<u32>,
@@ -650,10 +651,22 @@ impl Flat {
pass: &mut wgpu::RenderPass<'_>,
calls: &[Call],
vertices: impl Fn(u32) -> &'a wgpu::Buffer,
) {
self.draw_with(pass, calls, vertices, |_, _| {});
}
/// `draw`, calling `page` before each run of calls from one arena page (a shader pack binds
/// that page's vertices for reading).
pub fn draw_with<'a>(
&self,
pass: &mut wgpu::RenderPass<'_>,
calls: &[Call],
vertices: impl Fn(u32) -> &'a wgpu::Buffer,
page: impl Fn(&mut wgpu::RenderPass<'_>, u32),
) {
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
for call in calls {
self.bind_page(pass, call.page, &vertices);
page(pass, call.page);
pass.draw_indexed(call.first..call.first + call.count, 0, 0..1);
}
}
+105 -5
View File
@@ -3,7 +3,6 @@
//! layer is copied into a grid, row by row from the top left, and an animated layer again
//! whenever its frame changes.
#[expect(dead_code, reason = "the gbuffers pass binds it (next slice)")]
pub(super) struct PackAtlas {
texture: wgpu::Texture,
pub view: wgpu::TextureView,
@@ -64,10 +63,6 @@ impl PackAtlas {
}
/// The atlas's size in pixels (`atlasSize`).
#[cfg_attr(
not(test),
expect(dead_code, reason = "the gbuffers pass reads it (next slice)")
)]
pub fn size(&self) -> [u32; 2] {
[self.texture.width(), self.texture.height()]
}
@@ -118,6 +113,111 @@ impl PackAtlas {
}
}
/// GLSL: the face normal and tangent of a flat-drawn vertex, from its quad's first three corners
/// (quads are four consecutive vertices of the page; position words 0-2, UV word 3).
const FACE: &str = " uint hz_q = (uint(gl_VertexIndex) & ~3u) * 6u;\n \
vec3 hz_p0 = uintBitsToFloat(uvec3(hz_vertices[hz_q], hz_vertices[hz_q + 1u], hz_vertices[hz_q + 2u]));\n \
vec3 hz_p1 = uintBitsToFloat(uvec3(hz_vertices[hz_q + 6u], hz_vertices[hz_q + 7u], hz_vertices[hz_q + 8u]));\n \
vec3 hz_p2 = uintBitsToFloat(uvec3(hz_vertices[hz_q + 12u], hz_vertices[hz_q + 13u], hz_vertices[hz_q + 14u]));\n \
uvec3 hz_w = uvec3(hz_vertices[hz_q + 3u], hz_vertices[hz_q + 9u], hz_vertices[hz_q + 15u]);\n \
vec2 hz_t0 = vec2(hz_w.x & 65535u, hz_w.x >> 16u);\n \
vec2 hz_d1 = vec2(hz_w.y & 65535u, hz_w.y >> 16u) - hz_t0;\n \
vec2 hz_d2 = vec2(hz_w.z & 65535u, hz_w.z >> 16u) - hz_t0;\n \
vec3 hz_e1 = hz_p1 - hz_p0;\n \
vec3 hz_e2 = hz_p2 - hz_p0;\n \
vec3 hz_n = cross(hz_e1, hz_e2);\n \
if (dot(hz_n, hz_n) > 1e-12) {\n \
hz_Normal = normalize(hz_n);\n \
float hz_r = hz_d1.x * hz_d2.y - hz_d2.x * hz_d1.y;\n \
vec3 hz_t = (hz_e1 * hz_d2.y - hz_e2 * hz_d1.y) * sign(hz_r);\n \
vec3 hz_b = (hz_e2 * hz_d1.x - hz_e1 * hz_d2.x) * sign(hz_r);\n \
if (dot(hz_t, hz_t) > 1e-12) {\n \
hz_t = normalize(hz_t - hz_Normal * dot(hz_Normal, hz_t));\n \
hz_tangent = vec4(hz_t, dot(cross(hz_t, hz_Normal), hz_b) < 0.0 ? -1.0 : 1.0);\n \
}\n \
}\n";
impl PackAtlas {
/// The vertex inputs of a gbuffers program drawing terrain (`TerrainVertex`, terrain.wgsl's
/// `vs_terrain` or, for `flat` draws, `vs_terrain_flat`), in atlas coordinates.
pub fn terrain_inputs(&self, flat: bool) -> horizon_shaders::GbuffersInputs {
let mut declarations = String::from(
"layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in uvec2 hz_v_uv;\n\
layout(location = 2) in uint hz_v_layer;\nlayout(location = 3) in vec4 hz_v_color;\n\
layout(location = 4) in uvec2 hz_v_light;\n",
);
let origin = if flat {
declarations.push_str(
"layout(set = 1, binding = 0) readonly buffer HzBlockSlots { uint hz_block_slots[]; };\n\
layout(set = 1, binding = 1) readonly buffer HzSlotOrigins { vec4 hz_slot_origins[]; };\n",
);
// `mc_Entity` of each sprite layer: id, then 1 for fluids or -1 (the game's table),
// and the arena page's vertices (`TerrainVertex`, six words each).
declarations.push_str(
"layout(set = 3, binding = 0) readonly buffer HzBlockIds { int hz_block_ids[]; };\n\
layout(set = 3, binding = 1) readonly buffer HzVertices { uint hz_vertices[]; };\n",
);
"hz_slot_origins[hz_block_slots[uint(gl_VertexIndex) >> 8u]]"
} else {
declarations.push_str("layout(location = 5) in vec4 hz_v_origin;\n");
"hz_v_origin"
};
let [width, height] = self.size();
let scale = [
self.sprite as f32 / width as f32,
self.sprite as f32 / height as f32,
];
let columns = self.columns;
// UVs past a sprite (models up to 2, distant tiles) cannot repeat in an atlas: clamped.
let body = format!(
" vec4 hz_origin = {origin};\n \
uint hz_layer = hz_v_layer & 32767u;\n \
hz_cell = vec2(float(hz_layer % {columns}u), float(hz_layer / {columns}u));\n \
hz_scale = vec2({:?}, {:?});\n \
hz_Vertex = vec4(hz_v_pos + hz_origin.xyz, 1.0);\n \
hz_MultiTexCoord0 = vec4((hz_cell + clamp(vec2(hz_v_uv) * hz_origin.w, 0.0, 1.0)) * hz_scale, 0.0, 1.0);\n \
hz_MultiTexCoord1 = vec4(float(hz_v_light.y), float(hz_v_light.x), 0.0, 1.0);\n \
hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \
hz_Color = hz_v_color;\n \
hz_Normal = vec3(0.0, 1.0, 0.0);\n \
hz_tangent = vec4(1.0, 0.0, 0.0, 1.0);\n",
scale[0], scale[1],
);
let mut body = body;
if flat {
body.push_str(FACE);
}
declarations.push_str("vec2 hz_cell; vec2 hz_scale; vec4 hz_tangent;\n");
let entity = if flat {
"vec4(float(hz_block_ids[2u * hz_layer]), float(hz_block_ids[2u * hz_layer + 1u]), 0.0, 0.0)"
} else {
"vec4(-1.0, -1.0, 0.0, 0.0)"
};
let attributes = [
("mc_Entity", entity),
(
"mc_midTexCoord",
"vec4((hz_cell + 0.5) * hz_scale, 0.0, 1.0)",
),
("at_tangent", "hz_tangent"),
("vaPosition", "hz_Vertex.xyz"),
("vaUV0", "hz_MultiTexCoord0.xy"),
("vaUV2", "hz_MultiTexCoord1.xy"),
("vaColor", "hz_Color"),
("vaNormal", "hz_Normal"),
];
horizon_shaders::GbuffersInputs {
declarations,
body,
attributes: attributes
.iter()
.map(|&(n, v)| (n.to_owned(), v.to_owned()))
.collect(),
after: String::new(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -0,0 +1,81 @@
//! The uniforms a shader pack's gbuffers programs read per draw (`HzDraw` at set 2, the
//! translator's `DRAW_UNIFORMS`): one slot per render stage of a uniform buffer, picked with a
//! dynamic offset at each draw.
use horizon_shaders::{RENDER_STAGES, render_stage};
/// Where a run of `Frame::entities` begins, and what it is (slice 7): the run lasts until the
/// next span. Recorded only while a shader pack draws entities, so Horizon's own frames carry
/// none; Horizon itself still draws the whole list at once.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct EntitySpan {
/// The first vertex.
pub start: u32,
/// The render stage (5.4: `ENTITIES`, `BLOCK_ENTITIES`, `PARTICLES`, `DESTROY`), which picks
/// the program.
pub stage: u32,
/// `entityId` for entities, `blockEntityId` for block entities (5.3), else 0.
pub id: i32,
}
/// `HzDraw`'s size: `renderStage`, `entityId`, `blockEntityId`, a pad, `entityColor`.
const SIZE: u32 = 32;
pub(super) struct DrawUniforms {
pub layout: wgpu::BindGroupLayout,
group: wgpu::BindGroup,
stride: u32,
}
impl DrawUniforms {
pub fn new(device: &wgpu::Device) -> Self {
let stride = SIZE.max(device.limits().min_uniform_buffer_offset_alignment);
let mut contents = vec![0u8; stride as usize * RENDER_STAGES.len()];
for (i, slot) in contents.chunks_mut(stride as usize).enumerate() {
slot[..4].copy_from_slice(&(i as i32).to_le_bytes());
}
let buffer = wgpu::util::DeviceExt::create_buffer_init(
device,
&wgpu::util::BufferInitDescriptor {
label: Some("Shader pack draws"),
contents: &contents,
usage: wgpu::BufferUsages::UNIFORM,
},
);
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Shader pack draws"),
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: true,
min_binding_size: wgpu::BufferSize::new(u64::from(SIZE)),
},
count: None,
}],
});
let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Shader pack draws"),
layout: &layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
buffer: &buffer,
offset: 0,
size: wgpu::BufferSize::new(u64::from(SIZE)),
}),
}],
});
Self {
layout,
group,
stride,
}
}
/// Draws that follow read `renderStage` = `MC_RENDER_STAGE_<stage>`, other values zero.
pub fn bind(&self, pass: &mut wgpu::RenderPass<'_>, stage: &str) {
pass.set_bind_group(2, &self.group, &[self.stride * render_stage(stage)]);
}
}
File diff suppressed because it is too large Load Diff
+12 -3
View File
@@ -1,5 +1,6 @@
// Helpers of a shader pack's post-processing (pack_post.rs): the world into colortex0 and
// depthtex0, and colortex0 onto the screen for packs without `final`.
// depthtex0 (a depth attachment, for terrain's depth test), and colortex0 onto the screen for
// packs without `final`.
@vertex
fn vs_full(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> {
@@ -12,7 +13,7 @@ fn vs_full(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> {
struct World {
@location(0) color: vec4<f32>,
@location(1) depth: vec4<f32>,
@builtin(frag_depth) depth: f32,
}
// Pack buffers keep GL's rows (row 0 at the bottom), and GL's window depth: Horizon's
@@ -23,7 +24,7 @@ fn fs_blit(@builtin(position) p: vec4<f32>) -> World {
let at = vec2<i32>(i32(p.x), size.y - 1 - i32(p.y));
var out: World;
out.color = textureLoad(source, at, 0);
out.depth = vec4<f32>(1.0 - textureLoad(world_depth, at, 0), 0.0, 0.0, 1.0);
out.depth = 1.0 - textureLoad(world_depth, at, 0);
return out;
}
@@ -52,3 +53,11 @@ fn vs_uv(@builtin(vertex_index) i: u32) -> Full {
fn fs_copy(v: Full) -> @location(0) vec4<f32> {
return textureSample(source, linear, v.uv);
}
// One level of a buffer's mip chain from the level above (6.1.7): the average of the 2×2
// texels under each destination texel, by one bilinear sample at their shared corner.
@fragment
fn fs_down(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
let size = max(textureDimensions(source) / 2u, vec2<u32>(1u));
return textureSampleLevel(source, linear, p.xy / vec2<f32>(size), 0.0);
}
+319
View File
@@ -0,0 +1,319 @@
//! The vanilla sky's geometry for a shader pack's `gbuffers_skybasic` (spec 2.3): the sky disc,
//! the sunset fan, the stars and the void disc, as Horizon's own sky draws them (terrain.wgsl,
//! stars.rs), built once into a vertex buffer. The frame's colours and rotations sit in a small
//! uniform (set 1) that the vertex inputs read. `gbuffers_skytextured` draws the frame's sun and
//! moon quads, textured from the shared atlas.
use std::ops::Range;
use wgpu::util::DeviceExt;
/// A sky vertex: a position and what it belongs to (`SKY`...).
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct Vertex {
pos: [f32; 3],
kind: u32,
}
const SKY: u32 = 0;
const SUNSET_APEX: u32 = 1;
/// Rim of the sunset fan: `pos.xy` is (sin, cos) of its angle.
const SUNSET_RIM: u32 = 2;
const STAR: u32 = 3;
const VOID: u32 = 4;
/// The frame's sky (std140).
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct Uniform {
/// Camera-relative world from star space.
stars: [[f32; 4]; 4],
/// Sky colour, alpha 1.
color: [f32; 4],
/// Sunset colour and alpha.
sunrise: [f32; 4],
/// cos and sin of the sunset fan's turn, star brightness.
sun: [f32; 4],
}
/// A sun or moon vertex: position, atlas UV, RGBA8 colour.
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct Celestial {
pos: [f32; 3],
uv: [f32; 2],
color: u32,
}
/// Quads of the sun and moon (and room to spare).
const CELESTIAL_QUADS: usize = 4;
const CELESTIAL_ATTRIBUTES: [wgpu::VertexAttribute; 3] =
wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x2, 2 => Unorm8x4];
pub(super) const ATTRIBUTES: [wgpu::VertexAttribute; 2] =
wgpu::vertex_attr_array![0 => Float32x3, 1 => Uint32];
/// What `gbuffers_skybasic` reads from these vertices.
pub fn inputs() -> horizon_shaders::GbuffersInputs {
let declarations =
"layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in uint hz_v_kind;\n\
layout(set = 1, binding = 0) uniform HzSky { mat4 hz_sky_stars; vec4 hz_sky_color; \
vec4 hz_sky_sunrise; vec4 hz_sky_sun; };\n"
.to_owned();
let body = format!(
" vec3 hz_p = hz_v_pos;\n \
hz_Color = hz_sky_color;\n \
if (hz_v_kind == {SUNSET_APEX}u || hz_v_kind == {SUNSET_RIM}u) {{\n \
vec3 hz_l = vec3(0.0, 100.0, 0.0);\n \
hz_Color = hz_sky_sunrise;\n \
if (hz_v_kind == {SUNSET_RIM}u) {{\n \
hz_l = vec3(hz_v_pos.xy * 120.0, -hz_v_pos.y * 40.0 * hz_sky_sunrise.a);\n \
hz_Color.a = 0.0;\n \
}}\n \
vec3 hz_r = vec3(hz_l.x * hz_sky_sun.x - hz_l.y * hz_sky_sun.y, hz_l.x * hz_sky_sun.y + hz_l.y * hz_sky_sun.x, hz_l.z);\n \
hz_p = vec3(hz_r.x, -hz_r.z, hz_r.y);\n \
}} else if (hz_v_kind == {STAR}u) {{\n \
hz_p = (hz_sky_stars * vec4(hz_v_pos, 1.0)).xyz;\n \
hz_Color = vec4(hz_sky_sun.z);\n \
}} else if (hz_v_kind == {VOID}u) {{\n \
hz_Color = vec4(0.0, 0.0, 0.0, 1.0);\n \
}}\n \
hz_Vertex = vec4(hz_p, 1.0);\n \
hz_MultiTexCoord0 = vec4(0.0, 0.0, 0.0, 1.0);\n \
hz_MultiTexCoord1 = vec4(0.0, 240.0, 0.0, 1.0);\n \
hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \
hz_Normal = vec3(0.0, 1.0, 0.0);\n"
);
horizon_shaders::GbuffersInputs {
declarations,
body,
attributes: vec![
("vaPosition".into(), "hz_Vertex.xyz".into()),
("vaColor".into(), "hz_Color".into()),
("vaNormal".into(), "hz_Normal".into()),
],
// At the far plane: the sky shows only where nothing else drew, and it writes no depth.
after: " gl_Position.z = gl_Position.w;\n".into(),
}
}
/// What `gbuffers_skytextured` reads from the sun and moon's vertices.
pub fn celestial_inputs() -> horizon_shaders::GbuffersInputs {
horizon_shaders::GbuffersInputs {
declarations:
"layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in vec2 hz_v_uv;\n\
layout(location = 2) in vec4 hz_v_color;\n"
.into(),
body: " hz_Vertex = vec4(hz_v_pos, 1.0);\n \
hz_MultiTexCoord0 = vec4(hz_v_uv, 0.0, 1.0);\n \
hz_MultiTexCoord1 = vec4(0.0, 240.0, 0.0, 1.0);\n \
hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \
hz_Color = hz_v_color;\n \
hz_Normal = vec3(0.0, 0.0, 1.0);\n"
.into(),
attributes: vec![
("vaPosition".into(), "hz_Vertex.xyz".into()),
("vaUV0".into(), "hz_v_uv".into()),
("vaColor".into(), "hz_Color".into()),
("vaNormal".into(), "hz_Normal".into()),
],
after: " gl_Position.z = gl_Position.w;\n".into(),
}
}
/// One draw of the sky: its render stage, whether `gbuffers_skytextured` draws it, its vertices.
pub(super) struct Part {
pub stage: &'static str,
pub textured: bool,
pub range: Range<u32>,
}
/// A disc fan at height `y`, radius 512, as triangles (SkyRenderer's sky and void discs).
fn disc(y: f32, kind: u32, out: &mut Vec<Vertex>) {
let sign = y.signum();
let rim = |i: i32| {
let a = (i as f32).to_radians();
Vertex {
pos: [sign * 512.0 * a.cos(), y, 512.0 * a.sin()],
kind,
}
};
let centre = Vertex {
pos: [0.0, y, 0.0],
kind,
};
for i in (-180..180).step_by(45) {
out.extend([centre, rim(i), rim(i + 45)]);
}
}
pub(super) struct PackSky {
vertices: wgpu::Buffer,
celestial: wgpu::Buffer,
uniform: wgpu::Buffer,
pub layout: wgpu::BindGroupLayout,
group: wgpu::BindGroup,
/// Vertex ranges of the sky disc, the sunset fan, the stars and the void.
ranges: [Range<u32>; 4],
}
impl PackSky {
pub fn new(device: &wgpu::Device) -> Self {
let mut v = Vec::new();
disc(16.0, SKY, &mut v);
let sky = 0..v.len() as u32;
let apex = Vertex {
pos: [0.0; 3],
kind: SUNSET_APEX,
};
let rim = |i: u32| {
let a = i as f32 * std::f32::consts::TAU / 16.0;
Vertex {
pos: [a.sin(), a.cos(), 0.0],
kind: SUNSET_RIM,
}
};
for i in 0..16 {
v.extend([apex, rim(i), rim(i + 1)]);
}
let sunset = sky.end..v.len() as u32;
v.extend(
super::stars::star_triangles()
.into_iter()
.map(|pos| Vertex { pos, kind: STAR }),
);
let stars = sunset.end..v.len() as u32;
disc(-4.0, VOID, &mut v);
let void = stars.end..v.len() as u32;
let vertices = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Shader pack sky"),
contents: bytemuck::cast_slice(&v),
usage: wgpu::BufferUsages::VERTEX,
});
let celestial = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Shader pack sun and moon"),
size: (std::mem::size_of::<Celestial>() * 6 * CELESTIAL_QUADS) as u64,
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let uniform = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Shader pack sky"),
size: std::mem::size_of::<Uniform>() as u64,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Shader pack sky"),
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}],
});
let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Shader pack sky"),
layout: &layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniform.as_entire_binding(),
}],
});
Self {
vertices,
celestial,
uniform,
layout,
group,
ranges: [sky, sunset, stars, void],
}
}
pub fn buffers() -> [Option<wgpu::VertexBufferLayout<'static>>; 1] {
[Some(wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &ATTRIBUTES,
})]
}
pub fn celestial_buffers() -> [Option<wgpu::VertexBufferLayout<'static>>; 1] {
[Some(wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Celestial>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &CELESTIAL_ATTRIBUTES,
})]
}
/// Writes the frame's sky and returns its draws in vanilla's order: the disc, the sunset
/// fan while the sun is near the horizon, the sun and the moon, the stars and, with the
/// camera low, the void.
pub fn prepare(&self, queue: &wgpu::Queue, frame: &super::Frame) -> Vec<Part> {
let sun_angle = frame.sky.sun_angle.to_radians();
// The fan turns to the other horizon in the afternoon (terrain.wgsl's sunrise_weight).
let flip = if sun_angle.sin() < 0.0 { 180f32 } else { 0.0 };
let theta = (flip + 90.0).to_radians();
let (rotation, brightness) = frame.stars.unwrap_or_default();
let [r, g, b] = frame.sky.sky_color;
let uniform = Uniform {
stars: rotation.to_cols_array_2d(),
color: [r, g, b, 1.0],
sunrise: frame.sky.sunrise_color,
sun: [theta.cos(), theta.sin(), brightness.clamp(0.0, 1.0), 0.0],
};
queue.write_buffer(&self.uniform, 0, bytemuck::bytes_of(&uniform));
// The sun's quad, then the moon's (Game::celestial_geometry), as triangles.
let quads = frame
.celestial
.as_chunks::<4>()
.0
.iter()
.take(CELESTIAL_QUADS);
let triangles: Vec<Celestial> = quads
.clone()
.flat_map(|q| [0, 1, 2, 0, 2, 3].map(|i| q[i]))
.map(|v| Celestial {
pos: v.pos,
uv: v.uv,
color: v.color,
})
.collect();
queue.write_buffer(&self.celestial, 0, bytemuck::cast_slice(&triangles));
let [sky, sunset, stars, void] = self.ranges.clone();
let part = |stage, textured, range| Part {
stage,
textured,
range,
};
let mut parts = vec![part("SKY", false, sky)];
if frame.sky.sunrise_color[3] > 0.001 {
parts.push(part("SUNSET", false, sunset));
}
for (i, stage) in ["SUN", "MOON"].into_iter().enumerate().take(quads.len()) {
parts.push(part(stage, true, i as u32 * 6..i as u32 * 6 + 6));
}
if frame.stars.is_some() {
parts.push(part("STARS", false, stars));
}
if frame.dark_disc {
parts.push(part("VOID", false, void));
}
parts
}
/// Binds the vertices of the `textured` (sun and moon) or other parts.
pub fn bind(&self, pass: &mut wgpu::RenderPass<'_>, textured: bool) {
if textured {
pass.set_vertex_buffer(0, self.celestial.slice(..));
} else {
pass.set_bind_group(1, &self.group, &[]);
pass.set_vertex_buffer(0, self.vertices.slice(..));
}
}
}
@@ -0,0 +1,147 @@
//! Developers' aid: `HORIZON_SHADER_PACK_TIMING=1` times every pass of a shader pack on the
//! GPU, one frame in `EVERY`, and logs each pass's median every `REPORT` timed frames. The
//! frame after a timed one waits for the GPU before reading the timestamps (Metal resolves
//! them without waiting for the passes that write them), so timing slows the game.
const EVERY: u32 = 30;
const REPORT: usize = 30;
/// Passes timed per frame at most (two timestamps each).
const CAPACITY: u32 = 64;
pub(super) struct PassTiming {
queries: wgpu::QuerySet,
resolve: wgpu::Buffer,
read: wgpu::Buffer,
period_ns: f32,
frame: u32,
/// The passes timed in the frame being recorded or waiting to be read, in order.
names: Vec<String>,
timed: bool,
samples: Vec<(String, Vec<f32>)>,
reports: usize,
}
impl PassTiming {
pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Option<Self> {
if std::env::var_os("HORIZON_SHADER_PACK_TIMING").is_none()
|| !device.features().contains(wgpu::Features::TIMESTAMP_QUERY)
{
return None;
}
let size = u64::from(CAPACITY) * 2 * 8;
let buffer = |label, usage| {
device.create_buffer(&wgpu::BufferDescriptor {
label: Some(label),
size,
usage,
mapped_at_creation: false,
})
};
Some(Self {
queries: device.create_query_set(&wgpu::QuerySetDescriptor {
label: Some("Shader pack pass timing"),
ty: wgpu::QueryType::Timestamp,
count: CAPACITY * 2,
}),
resolve: buffer(
"Shader pack timing resolve",
wgpu::BufferUsages::QUERY_RESOLVE | wgpu::BufferUsages::COPY_SRC,
),
read: buffer(
"Shader pack timing read",
wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
),
period_ns: queue.get_timestamp_period(),
frame: 0,
names: Vec::new(),
timed: false,
samples: Vec::new(),
reports: 0,
})
}
/// At the start of a frame: reads the last timed frame's passes, and decides whether this
/// one is timed.
pub fn begin(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) {
if self.timed && !self.names.is_empty() {
self.collect(device, queue);
}
self.names.clear();
self.frame += 1;
self.timed = self.frame.is_multiple_of(EVERY);
}
/// Timestamp writes for the pass `name`, in a timed frame.
pub fn writes(&mut self, name: &str) -> Option<wgpu::RenderPassTimestampWrites<'_>> {
let i = self.names.len() as u32;
if !self.timed || i >= CAPACITY {
return None;
}
self.names.push(name.to_owned());
Some(wgpu::RenderPassTimestampWrites {
query_set: &self.queries,
beginning_of_pass_write_index: Some(2 * i),
end_of_pass_write_index: Some(2 * i + 1),
})
}
fn collect(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) {
// The timed frame first, then the resolve, so every timestamp is written.
_ = device.poll(wgpu::PollType::wait_indefinitely());
let count = self.names.len() as u32 * 2;
let bytes = u64::from(count) * 8;
let mut encoder = device.create_command_encoder(&Default::default());
encoder.resolve_query_set(&self.queries, 0..count, &self.resolve, 0);
encoder.copy_buffer_to_buffer(&self.resolve, 0, &self.read, 0, bytes);
queue.submit([encoder.finish()]);
let slice = self.read.slice(..bytes);
slice.map_async(wgpu::MapMode::Read, |_| {});
_ = device.poll(wgpu::PollType::wait_indefinitely());
let Ok(data) = slice.get_mapped_range() else {
return;
};
let stamps: Vec<u64> = data
.as_chunks::<8>()
.0
.iter()
.map(|b| u64::from_le_bytes(*b))
.collect();
drop(data);
self.read.unmap();
// Passes overlap on tiled GPUs (a pass begins before the one before it ends): a pass
// costs the time from the later of its beginning and the previous pass's end.
let mut previous_end = 0;
for (name, pair) in self.names.iter().zip(stamps.as_chunks::<2>().0) {
if pair[0] == 0 || pair[1] < pair[0] {
continue;
}
let from = pair[0].max(previous_end).min(pair[1]);
previous_end = pair[1];
let ms = (pair[1] - from) as f32 * self.period_ns / 1e6;
match self.samples.iter_mut().find(|s| s.0 == *name) {
Some(s) => s.1.push(ms),
None => self.samples.push((name.clone(), vec![ms])),
}
}
self.reports += 1;
if self.reports.is_multiple_of(REPORT) {
self.report();
}
}
fn report(&mut self) {
let mut total = 0.0;
let mut parts = Vec::new();
for (name, v) in &mut self.samples {
v.sort_by(f32::total_cmp);
let median = v[v.len() / 2];
total += median;
parts.push(format!("{name} {median:.3}"));
v.clear();
}
log::info!(
"shader pack GPU time per pass (median ms of {REPORT} frames): {} = {total:.3}",
parts.join(", ")
);
}
}
+89 -5
View File
@@ -23,6 +23,12 @@ pub struct PackInputs {
pub hide_gui: bool,
/// The brightness option, 0..1.
pub brightness: f32,
/// The biome at the camera (3.4): registry id, `CAT_*`, `PPT_*`, temperature, downfall.
pub biome: i32,
pub biome_category: i32,
pub biome_precipitation: i32,
pub temperature: f32,
pub rainfall: f32,
}
/// One uniform's value: up to 16 floats or 3 integers.
@@ -134,17 +140,29 @@ pub const NAMES: &[&str] = &[
"heldBlockLightValue",
"heldBlockLightValue2",
"renderStage",
"biome",
"biome_category",
"biome_precipitation",
"temperature",
"rainfall",
"gbufferModelView",
"gbufferModelViewInverse",
"gbufferProjection",
"gbufferProjectionInverse",
"gbufferPreviousModelView",
"gbufferPreviousProjection",
"atlasSize",
"shadowModelView",
"shadowModelViewInverse",
"shadowProjection",
"shadowProjectionInverse",
];
/// Values carried from frame to frame.
pub struct FrameUniforms {
pub values: Vec<Value>,
/// The block atlas's size (`atlasSize`), 0 without one.
pub atlas: [u32; 2],
start: std::time::Instant,
last: Option<std::time::Instant>,
frame: u32,
@@ -157,6 +175,7 @@ impl Default for FrameUniforms {
fn default() -> Self {
Self {
values: Vec::new(),
atlas: [0; 2],
start: std::time::Instant::now(),
last: None,
frame: 0,
@@ -214,14 +233,28 @@ impl FrameUniforms {
for (smoothed, target) in self.eye_smooth.iter_mut().zip(eye) {
*smoothed = smooth(*smoothed, target, 10.0 / 20.0, dt);
}
// Vanilla fades by the stronger of two linear fogs; a pack gets one: the pair (start
// and end together) of the one that is complete nearer (OQ-36). Mixing the nearer start
// of one with the nearer end of the other made fog begin at the camera on clear days.
let (fog_color, fog_start, fog_end) = match &frame.fog {
Some(f) => (
[f.color[0], f.color[1], f.color[2]],
f.environmental_start.min(f.render_distance_start),
f.environmental_end.min(f.render_distance_end),
),
Some(f) => {
let (start, end) = if f.environmental_end < f.render_distance_end {
(f.environmental_start, f.environmental_end)
} else {
(f.render_distance_start, f.render_distance_end)
};
([f.color[0], f.color[1], f.color[2]], start, end)
}
None => (frame.sky.fog_color, frame.distance * 0.9, frame.distance),
};
// The shadow camera (OQ-6, impl-plan.md): orthographic, `shadowDistance` (160) blocks
// each side, 100 blocks from the player toward the shadow light, world +z up.
let light = view
.inverse()
.transform_vector3(if day { sun } else { moon })
.normalize();
let shadow_view = Mat4::look_at_rh(light * 100.0, Vec3::ZERO, Vec3::Z);
let shadow_projection = Mat4::orthographic_rh_gl(-160.0, 160.0, -160.0, 160.0, 0.05, 256.0);
let day_time = inputs.day_time.rem_euclid(24000) as i32;
let world_day = inputs.day_time.div_euclid(24000);
let seconds = (now - self.start).as_secs_f32() % 3600.0;
@@ -243,6 +276,11 @@ impl FrameUniforms {
"worldDay" => Value::i(&[world_day as i32]),
"moonPhase" => Value::i(&[world_day.rem_euclid(8) as i32]),
"frameCounter" => Value::i(&[self.frame as i32]),
"shadowModelView" => m4(shadow_view),
"shadowModelViewInverse" => m4(shadow_view.inverse()),
"shadowProjection" => m4(shadow_projection),
"shadowProjectionInverse" => m4(shadow_projection.inverse()),
"atlasSize" => Value::i(&[self.atlas[0] as i32, self.atlas[1] as i32]),
"frameTime" => Value::f(&[dt]),
"frameTimeCounter" => Value::f(&[seconds]),
"viewWidth" => Value::f(&[size[0] as f32]),
@@ -269,6 +307,11 @@ impl FrameUniforms {
"screenBrightness" => Value::f(&[inputs.brightness]),
"hideGUI" => Value::i(&[inputs.hide_gui as i32]),
"eyeBrightness" => Value::i(&eye.map(|v| v as i32)),
"biome" => Value::i(&[inputs.biome]),
"biome_category" => Value::i(&[inputs.biome_category]),
"biome_precipitation" => Value::i(&[inputs.biome_precipitation]),
"temperature" => Value::f(&[inputs.temperature]),
"rainfall" => Value::f(&[inputs.rainfall]),
"eyeBrightnessSmooth" => Value::i(&self.eye_smooth.map(|v| v.round() as i32)),
"gbufferModelView" => m4(view),
"gbufferModelViewInverse" => m4(view.inverse()),
@@ -299,6 +342,47 @@ impl FrameUniforms {
}
}
impl FrameUniforms {
/// Evaluates the pack's custom uniforms with this frame's values and puts them after
/// `NAMES`' values, in `custom.uniforms()` order.
pub fn eval_custom(&mut self, custom: &mut horizon_shaders::Custom) {
self.values.truncate(NAMES.len());
let values = &self.values;
let builtin = |name: &str| {
let v = values.get(index(name)?)?;
let n = components(name);
Some(match *v {
Value::F(f) => horizon_shaders::Val::vector(&f.map(f64::from)[..n]),
Value::I(i) => horizon_shaders::Val::vector(&i.map(f64::from)[..n.min(3)]),
})
};
let dt = match index("frameTime").and_then(|i| values.get(i)) {
Some(Value::F(f)) => f64::from(f[0]),
_ => 0.0,
};
let out = custom.eval(dt, &builtin);
self.values
.extend(out.iter().map(|v| Value::f(&v.v.map(|x| x as f32)[..v.n])));
}
}
/// How many components the uniform `name` has (16 for a matrix).
fn components(name: &str) -> usize {
match name {
"cameraPosition"
| "previousCameraPosition"
| "upPosition"
| "sunPosition"
| "moonPosition"
| "shadowLightPosition"
| "fogColor"
| "skyColor" => 3,
"eyeBrightness" | "eyeBrightnessSmooth" | "atlasSize" => 2,
n if n.starts_with("gbuffer") || n.starts_with("shadow") && n != "shadowAngle" => 16,
_ => 1,
}
}
/// The position of `name` in [`NAMES`].
pub fn index(name: &str) -> Option<usize> {
NAMES.iter().position(|n| *n == name)
+8 -2
View File
@@ -261,11 +261,17 @@ impl PlayerHealth {
self.hurt_animation_time = 10;
}
/// `LocalPlayer.hurtTo` with `flashOnSetHealth`: a drop below the current
/// health starts the hurt animation.
/// health starts the hurt animation; `damageCooldownTime` (20 on a drop,
/// 10 on a gain) is what lets `Hud.extractPlayerHealth` blink the hearts.
pub fn flash_health_change(&mut self, health: f32) {
let damage = self.health() - health;
if damage > 0.0 {
if damage <= 0.0 {
if damage < 0.0 {
self.hurt.cooldown = 10;
}
} else {
self.hurt.last_hurt = damage;
self.hurt.cooldown = 20;
self.animate_hurt();
}
}
+6
View File
@@ -851,6 +851,12 @@ impl HudBatch {
r.v1 = cv;
self.rect_uv(x, y, w, h, r, color);
}
/// Any four-cornered shape in one colour, corners in order round it.
pub fn fill_quad(&mut self, atlas: &HudAtlas, corners: [[f32; 2]; 4], color: u32) {
let w = atlas.white;
let uv = [(w.u0 + w.u1) / 2.0, (w.v0 + w.v1) / 2.0];
self.quad(corners, [uv; 4], color);
}
/// GuiGraphics.fillGradient: `top` to `bottom`, down the rectangle.
pub fn fill_gradient(
&mut self,
@@ -78,6 +78,17 @@ impl Hub {
);
}
/// `ServerLevel.globalLevelEvent`: the packet with its global flag, for
/// every player on the server (`PlayerList.broadcastAll`).
pub fn global_level_event(&self, kind: i32, pos: [i32; 3], data: i32) {
self.broadcast(&C::LevelEvent(play::LevelEvent {
kind,
pos,
data,
global: true,
}));
}
/// ClientboundBlockEventPacket (`PlayerList.broadcast` within 64 blocks).
pub fn block_event(&self, pos: [i32; 3], b0: i32, b1: i32, state: u32) {
let block = self
+1
View File
@@ -146,6 +146,7 @@ pub mod player_sync;
pub mod poi_sections;
pub mod portal_sections;
pub mod pose;
pub mod pregen;
pub mod projectiles;
pub mod raid;
pub mod random_sequences;
+9
View File
@@ -198,6 +198,15 @@ pub fn tick(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
m.extra.leash_spin = 0.0;
return;
};
// Leashable.tickLeash: a dying entity cannot interact with the level.
if !m.alive() {
events.push(MobEvent::LeashBreak {
mob: m.id,
drop: true,
sound: false,
});
return;
}
let Some(state) = holder_state(w, holder) else {
// The holder is gone: the lead drops.
if !matches!(holder, Holder::Player) {
+5
View File
@@ -3076,8 +3076,13 @@ mod tests {
if client_side {
w.player.health.flash_health_change(20.0);
assert_eq!(w.player.health.hurt_animation_time, 0);
assert_eq!(w.player.health.hurt.cooldown, 0);
w.player.health.flash_health_change(12.0);
assert_eq!(w.player.health.hurt_animation_time, 10);
// LocalPlayer.hurtTo's damageCooldownTime: the HUD's hearts blink.
assert_eq!(w.player.health.hurt.cooldown, 20);
w.player.health.flash_health_change(25.0);
assert_eq!(w.player.health.hurt.cooldown, 10);
}
}
}
-3
View File
@@ -3403,9 +3403,6 @@ mod tests {
assert_eq!(s.gui_scale, expected);
}
s.gui_scale = u32::MAX;
s.clamp_gui_scale(framebuffer);
assert_eq!(s.gui_scale, max);
s.gui_scale = u32::MAX;
Opt::GuiScale.cycle(&mut s, false, framebuffer);
assert_eq!(s.gui_scale, 0);
s.gui_scale = u32::MAX;
+73 -2
View File
@@ -8,7 +8,8 @@ use std::io::Read;
use std::{collections::BTreeSet, fs, io, path::Path, path::PathBuf};
// Bump when overlay/merge semantics change. Markers are disposable cache data.
const VERSION: u32 = 3;
// Version 3 could omit untouched vanilla directories on Windows (#1649, #1688).
const VERSION: u32 = 4;
#[derive(Default, Serialize, Deserialize)]
pub(super) struct Dependencies {
@@ -421,6 +422,73 @@ mod tests {
out
}
#[test]
#[cfg(not(unix))]
fn texture_only_packs_rebuild_incomplete_windows_v3_overlays() {
let f = Fixture::new();
let blockstate = "assets/minecraft/blockstates/oak_stairs.json";
let model = "assets/minecraft/models/block/oak_stairs.json";
let texture = "assets/minecraft/textures/block/stone.png";
let font = "assets/minecraft/textures/font/ascii.png";
f.write(&format!("vanilla/{blockstate}"), br#"{"variants":{}}"#);
f.write(&format!("vanilla/{model}"), br#"{"elements":[]}"#);
f.write(&format!("vanilla/{texture}"), b"vanilla texture");
f.write(&format!("vanilla/{font}"), b"vanilla font");
let folder = f.dir_pack("textures");
f.write(&format!("textures/{texture}"), b"pack texture");
f.write(
"textures.zip",
&archive(
&[
(
"pack.mcmeta",
br#"{"pack":{"description":"test","pack_format":97}}"#.to_vec(),
),
(texture, b"pack texture".to_vec()),
],
true,
),
);
let zip = packs::pack(
"zip".into(),
Text::empty(),
Source::Zip(f.0.join("textures.zip")),
)
.unwrap();
let vanilla = f.0.join("vanilla");
for pack in [folder, zip] {
let out = f.0.join(format!("cached-{}", pack.id));
let selected = std::slice::from_ref(&pack);
packs::build_overlay(&vanilla, selected, &out).unwrap();
let check = || {
for name in [blockstate, model, font] {
assert_eq!(
fs::read(out.join(name)).unwrap(),
fs::read(vanilla.join(name)).unwrap()
);
}
assert_eq!(fs::read(out.join(texture)).unwrap(), b"pack texture");
};
check();
// Before #1688, Windows silently omitted untouched directories.
// A v3 marker still validates because it checks only source files.
let marker_path = marker_path(&out);
let mut marker: Marker =
serde_json::from_slice(&fs::read(&marker_path).unwrap()).unwrap();
marker.version = 3;
for dir in ["blockstates", "models", "textures/font"] {
fs::remove_dir_all(out.join("assets/minecraft").join(dir)).unwrap();
}
fs::write(&marker_path, serde_json::to_vec(&marker).unwrap()).unwrap();
assert!(marker.dependencies.valid());
packs::build_overlay(&vanilla, selected, &out).unwrap();
check();
// The repaired overlay also remains complete on a cache hit.
packs::build_overlay(&vanilla, selected, &out).unwrap();
check();
}
}
#[test]
fn directory_inputs_config_and_owned_output_recovery() {
let f = Fixture::new();
@@ -456,7 +524,10 @@ mod tests {
let path = f.0.join(format!("high/{resource}"));
let original_time = fs::metadata(&path).unwrap().modified().unwrap();
fs::write(&path, br#"{"p":"c"}"#).unwrap();
fs::File::open(path)
// Windows requires a writable handle to restore the timestamp.
fs::OpenOptions::new()
.write(true)
.open(path)
.unwrap()
.set_times(fs::FileTimes::new().set_modified(original_time))
.unwrap();
+36 -10
View File
@@ -1212,10 +1212,7 @@ impl ParticleEngine {
requested: None,
names: Vec::new(),
rects: Vec::new(),
atlas: atlas
.iter()
.map(|(name, &rect)| (name.clone(), (rect, translucent.contains(name))))
.collect(),
atlas: HashMap::new(),
sets: HashMap::new(),
set_sprites: Vec::new(),
emitters: Vec::new(),
@@ -1225,6 +1222,34 @@ impl ParticleEngine {
made: Default::default(),
item_sprites: HashMap::new(),
};
engine.reload(root, atlas, translucent);
engine
}
/// `onResourceManagerReload`: sprite rects and sets come from the
/// reloaded atlas and pack; a live particle keeps drawing its sprite by
/// name or index.
pub fn reload(
&mut self,
root: &Path,
atlas: &HashMap<String, Rect>,
translucent: &HashSet<String>,
) {
self.atlas = atlas
.iter()
.map(|(name, &rect)| (name.clone(), (rect, translucent.contains(name))))
.collect();
self.rects = self
.names
.iter()
.map(|name| self.atlas.get(name).map(|&(rect, _)| rect))
.collect();
self.sets.clear();
self.set_sprites.clear();
self.item_sprites.clear();
self.load_sprites(root);
}
/// The sprite sets of every `particles/*.json` under `root`.
fn load_sprites(&mut self, root: &Path) {
let dir = root.join("assets/minecraft/particles");
for &ty in ParticleType::ALL {
let Ok(bytes) = std::fs::read(dir.join(format!("{}.json", ty.name()))) else {
@@ -1239,18 +1264,16 @@ impl ParticleEngine {
a.iter()
.filter_map(|t| t.as_str())
.map(|t| {
engine
.intern(&format!("particle/{}", t.trim_start_matches("minecraft:")))
self.intern(&format!("particle/{}", t.trim_start_matches("minecraft:")))
})
.collect()
})
.unwrap_or_default();
if !sprites.is_empty() {
engine.sets.insert(ty, engine.set_sprites.len() as u32);
engine.set_sprites.push(sprites);
self.sets.insert(ty, self.set_sprites.len() as u32);
self.set_sprites.push(sprites);
}
}
engine
}
fn intern(&mut self, name: &str) -> u32 {
if let Some(i) = self.names.iter().position(|n| n == name) {
@@ -1601,7 +1624,10 @@ impl ParticleEngine {
return;
}
if let Some(set) = p.set {
let sprites = &self.set_sprites[set as usize];
// A sprite set's index can outlive the reload that rebuilt them.
let Some(sprites) = self.set_sprites.get(set as usize) else {
return;
};
let lifetime = p.lifetime.max(1);
let index = (p.age.max(0) as i64 * (sprites.len() as i64 - 1) / i64::from(lifetime))
.clamp(0, sprites.len() as i64 - 1);
+191
View File
@@ -0,0 +1,191 @@
//! Pregeneration (`/pregen`): every chunk of a square or a circle generated,
//! lit and saved ahead of the players. The area is cut into strips of columns
//! and each strip is swept north to south by a [`Band`] a few rows deep:
//! streaming generates the band's chunks and keeps them until they are lit,
//! then the band moves on and the rows behind it unload and are saved, so
//! memory stays bounded whatever the radius.
use crate::streaming::Band;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Shape {
Square,
Circle,
}
/// The widest strip, in chunks. Strips are swept one after another; the few
/// columns along the edge between two are generated with both.
const STRIP_WIDTH: i32 = 256;
/// How many chunks a band holds at least: enough to keep every worker busy
/// while the rows before it finish.
const BAND_CHUNKS: i32 = 1024;
pub struct Pregen {
/// The centre chunk and the radius in chunks.
pub center: (i32, i32),
pub radius: i32,
pub shape: Shape,
/// The strips' columns, west to east.
strips: Vec<(i32, i32)>,
strip: usize,
/// The band's first row: every row of the strip before it is done.
row: i32,
/// Chunks of the area lit so far, and in all.
pub done: u64,
pub total: u64,
}
impl Pregen {
pub fn new(center: (i32, i32), radius: i32, shape: Shape) -> Self {
let radius = radius.max(0);
let (x0, width) = (center.0 - radius, 2 * radius + 1);
let count = (width + STRIP_WIDTH - 1) / STRIP_WIDTH;
// The columns `i * width / count` on, in i64: the product overflows
// for a radius of a few hundred thousand chunks.
let edge = |i: i32| x0 + (i64::from(width) * i64::from(i) / i64::from(count)) as i32;
let strips = (0..count).map(|i| (edge(i), edge(i + 1) - 1)).collect();
let mut pregen = Self {
center,
radius,
shape,
strips,
strip: 0,
row: 0,
done: 0,
total: 0,
};
pregen.total = (center.1 - radius..=center.1 + radius)
.map(|z| pregen.row_chunks(x0, x0 + width - 1, z))
.sum();
pregen.row = pregen.rows().map_or(0, |r| r.0);
pregen
}
/// How far from the centre column the area reaches at `d` chunks from
/// the centre row (or, the same, from the centre column), if it does.
fn half_width(&self, d: i32) -> Option<i32> {
let (r, d) = (i64::from(self.radius), i64::from(d));
match self.shape {
Shape::Square => (d.abs() <= r).then_some(self.radius),
Shape::Circle => (d * d <= r * r).then(|| (r * r - d * d).isqrt() as i32),
}
}
fn contains(&self, x: i32, z: i32) -> bool {
self.half_width(z - self.center.1)
.is_some_and(|h| (x - self.center.0).abs() <= h)
}
/// The area's chunks in columns `x0..=x1` of row `z`.
fn row_chunks(&self, x0: i32, x1: i32, z: i32) -> u64 {
self.half_width(z - self.center.1).map_or(0, |h| {
let (lo, hi) = (x0.max(self.center.0 - h), x1.min(self.center.0 + h));
(i64::from(hi) - i64::from(lo) + 1).max(0) as u64
})
}
/// The first and last rows of the current strip that hold chunks of the
/// area.
fn rows(&self) -> Option<(i32, i32)> {
let &(x0, x1) = self.strips.get(self.strip)?;
let nearest = (x0 - self.center.0).max(self.center.0 - x1).max(0);
let h = self.half_width(nearest)?;
Some((self.center.1 - h, self.center.1 + h))
}
/// The band streaming should hold now; none once every strip is done.
pub fn band(&self) -> Option<Band> {
let &(x0, x1) = self.strips.get(self.strip)?;
let (_, last) = self.rows()?;
let rows = (BAND_CHUNKS + (x1 - x0)) / (x1 - x0 + 1);
Some(Band {
x0,
x1,
z0: self.row,
z1: (self.row + rows - 1).min(last),
circle: (self.shape == Shape::Circle).then_some((self.center, self.radius)),
})
}
/// Move past the band's first rows once all their chunks are `lit`.
/// Returns whether the band moved.
pub fn advance(&mut self, lit: impl Fn(i32, i32) -> bool) -> bool {
let mut moved = false;
while let (Some(&(x0, x1)), Some((_, last))) = (self.strips.get(self.strip), self.rows()) {
let z = self.row;
if !(x0..=x1).all(|x| !self.contains(x, z) || lit(x, z)) {
break;
}
self.done += self.row_chunks(x0, x1, z);
self.row += 1;
if self.row > last {
self.strip += 1;
self.row = self.rows().map_or(self.row, |r| r.0);
}
moved = true;
}
moved
}
/// The chunks of the band's first row, which it waits for.
pub fn first_row(&self) -> Vec<(i32, i32)> {
let Some(&(x0, x1)) = self.strips.get(self.strip) else {
return Vec::new();
};
(x0..=x1)
.filter(|&x| self.contains(x, self.row))
.map(|x| (x, self.row))
.collect()
}
pub fn finished(&self) -> bool {
self.strip >= self.strips.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sweeps_every_chunk_of_the_area_once() {
for shape in [Shape::Square, Shape::Circle] {
for (center, radius) in [((0, 0), 0), ((3, -7), 5), ((-40, 12), 130), ((0, 0), 300)] {
let mut p = Pregen::new(center, radius, shape);
let r = i64::from(radius);
let area = (-radius..=radius)
.flat_map(|dz| (-radius..=radius).map(move |dx| (dx, dz)))
.filter(|&(dx, dz)| {
shape == Shape::Square
|| i64::from(dx).pow(2) + i64::from(dz).pow(2) <= r * r
})
.count() as u64;
assert_eq!(p.total, area, "{shape:?} {radius}");
let mut seen = std::collections::HashSet::new();
while !p.finished() {
let band = p.band().unwrap();
assert!(band.z0 <= band.z1);
let row = p.first_row();
for &(x, z) in &row {
assert!(seen.insert((x, z)), "{x} {z} twice");
}
assert!(
p.advance(|x, z| z == band.z0 && row.contains(&(x, z))) || row.is_empty()
);
}
assert_eq!(
(p.done, seen.len() as u64),
(area, area),
"{shape:?} {radius}"
);
}
}
}
#[test]
fn a_huge_area_is_planned_at_once() {
let started = std::time::Instant::now();
let p = Pregen::new((0, 0), 30_000_000 >> 4, Shape::Circle);
assert!(p.total > 11_000_000_000_000);
assert!(started.elapsed() < std::time::Duration::from_secs(5));
}
}
+12 -11
View File
@@ -572,6 +572,17 @@ fn merge_block_entity(original: &Tag, loaded: &Tag, current: &Tag) -> Tag {
}
merged
}
/// `BannerBlockEntity.loadAdditional` (and the `components` a block entity keeps).
pub fn decode_banner(tag: &Tag) -> crate::containers::Banner {
crate::containers::Banner {
layers: crate::banner::layers_from_nbt(tag.get("patterns")),
name: text(tag.get("CustomName")),
components: match tag.get("components") {
Some(Tag::Compound(c)) => c.clone(),
_ => Compound::new(),
},
}
}
pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::containers::BlockEntity)> {
use crate::containers::BlockEntity;
let int = |k: &str| number(tag.get(k)).map(|v| v as i32);
@@ -605,17 +616,7 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain
return Some((pos, BlockEntity::Lectern(lectern)));
}
if id == "minecraft:banner" {
return Some((
pos,
BlockEntity::Banner(crate::containers::Banner {
layers: crate::banner::layers_from_nbt(tag.get("patterns")),
name: text(tag.get("CustomName")),
components: match tag.get("components") {
Some(Tag::Compound(c)) => c.clone(),
_ => Compound::new(),
},
}),
));
return Some((pos, BlockEntity::Banner(decode_banner(tag))));
}
if id == "minecraft:beacon" {
let beacon = crate::containers::Beacon {
+26 -7
View File
@@ -104,7 +104,7 @@ pub struct Settings {
pub master_volume: f32,
/// Volumes of every other sound source, indexed like SOUND_SOURCES.
pub volumes: [f32; 11],
/// 0 = auto.
/// Requested scale (0 = auto); rendering caps it to the current framebuffer.
pub gui_scale: u32,
pub view_bobbing: bool,
/// Brightness 0..1 (gamma).
@@ -284,12 +284,6 @@ fn put_s(f: &mut OptionsFile, key: &str, v: &str) {
}
}
impl Settings {
/// `ClampingLazyMaxIntRange.validateValue`: clamp once the framebuffer is known.
pub fn clamp_gui_scale(&mut self, framebuffer: [i32; 2]) {
let max = crate::window::calculate_scale(framebuffer, 0, self.force_unicode) as u32;
self.gui_scale = self.gui_scale.min(max);
}
pub fn load(path: &Path) -> Self {
let horizon = path
.parent()
@@ -597,6 +591,31 @@ impl Settings {
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn gui_scale_survives_temporary_small_framebuffer() {
for requested in [0, 1, 2, 6] {
let settings = Settings::parse(&format!("guiScale:{requested}\n"));
// Hyprland's initial surface can be smaller than the fullscreen one.
for (framebuffer, maximum) in [([715, 860], 2), ([3400, 1913], 7)] {
let effective = crate::window::calculate_scale(
framebuffer,
settings.gui_scale as i32,
settings.force_unicode,
);
assert_eq!(
effective,
if requested == 0 {
maximum
} else {
(requested as i32).min(maximum)
}
);
let saved = Settings::parse(&settings.serialize());
assert_eq!(saved.gui_scale, requested);
}
}
}
#[test]
fn round_trips_vanilla_options_txt() {
let text = "version:4000\nfov:0.5\nrenderDistance:16\nmaxFps:260\nrenderClouds:\"fast\"\nkey_key.jump:key.keyboard.j\nsoundCategory_music:0.25\nresourcePacks:[\"vanilla\",\"file/x.zip\"]\nlang:fr_fr\nsomeFutureOption:42\n";
+156 -4
View File
@@ -278,6 +278,11 @@ pub struct Streaming {
cover: Option<ExtraCover>,
/// Mesh the sections for drawing (false on a server thread).
pub mesh: bool,
/// The pregeneration band (`set_band`).
band: Option<Band>,
/// The columns and the next row the band's decoration ranks were given
/// for (`set_band`).
band_ranked: Option<(i32, i32, i32)>,
}
/// Snapshot only after a scan found no missing candidates. Public range fields
/// and `World::chunks` can change without going through Streaming, so compare
@@ -289,6 +294,7 @@ struct GenerationScan {
radius: i32,
extra: Vec<((i32, i32), i32)>,
forced: HashSet<(i32, i32)>,
band: Option<Band>,
chunk_revision: u64,
}
impl GenerationScan {
@@ -298,6 +304,7 @@ impl GenerationScan {
&& self.radius == ranges.radius
&& self.extra == ranges.extra
&& self.forced == *ranges.forced
&& self.band.as_ref() == ranges.band
&& self.chunk_revision == world.chunks.membership_revision()
}
fn remember(&mut self, ranges: Ranges<'_>, world: &World) {
@@ -307,6 +314,7 @@ impl GenerationScan {
self.extra.clear();
self.extra.extend_from_slice(ranges.extra);
self.forced.clone_from(ranges.forced);
self.band = ranges.band.copied();
self.chunk_revision = world.chunks.membership_revision();
}
}
@@ -348,6 +356,10 @@ impl GenerationScan {
let r = radius + margin.max(reach + 1);
runs.extend((center.1 - r..=center.1 + r).map(|z| (z, center.0 - r, center.0 + r)));
}
if let Some(band) = input.ranges.band {
let r = margin.max(reach + 1);
runs.extend((band.z0 - r..=band.z1 + r).map(|z| (z, band.x0 - r, band.x1 + r)));
}
runs.sort_unstable();
let mut runs = runs.into_iter().peekable();
while let Some((z, x0, mut x1)) = runs.next() {
@@ -392,6 +404,7 @@ impl GenerationScan {
extra: input.ranges.extra,
forced: input.ranges.forced,
cover: input.ranges.cover,
band: input.ranges.band,
};
self.remember(ranges, input.world);
} else {
@@ -564,6 +577,8 @@ impl Streaming {
extra: Vec::new(),
cover: None,
mesh: true,
band: None,
band_ranked: None,
}
}
/// Shares generation resources, but gives distant work its own worker state.
@@ -916,6 +931,7 @@ impl Streaming {
p.is_some()
&& i64::from(x - center.0).pow(2) + i64::from(z - center.1).pow(2) > r4 * r4
&& !ranges.in_extra(x, z, 4)
&& !ranges.in_band(x, z, 4)
&& !ranges
.forced
.iter()
@@ -985,6 +1001,7 @@ impl Streaming {
extra: &self.extra,
forced: &self.forced,
cover: ExtraCover::matching(&self.cover, &self.extra),
band: self.band.as_ref(),
};
let (protos, generating) = (&self.protos, &self.generating);
self.decoration_order.retain(|x, z| {
@@ -1113,6 +1130,7 @@ impl Streaming {
extra: &self.extra,
forced: &self.forced,
cover: ExtraCover::matching(&self.cover, &self.extra),
band: self.band.as_ref(),
};
self.waiting.retain(|&(x, sy, z)| {
let back = ranges.in_view(x, z);
@@ -1189,6 +1207,7 @@ impl Streaming {
extra: &self.extra,
forced: &self.forced,
cover: ExtraCover::matching(&self.cover, &self.extra),
band: self.band.as_ref(),
},
world,
generating: &self.generating,
@@ -1285,6 +1304,7 @@ impl Streaming {
extra: &self.extra,
forced: &self.forced,
cover: ExtraCover::matching(&self.cover, &self.extra),
band: self.band.as_ref(),
}
}
fn near_finished(&self, x: i32, z: i32, reach: i32) -> bool {
@@ -1354,6 +1374,66 @@ impl Streaming {
fn in_range(&self, x: i32, z: i32, margin: i32) -> bool {
self.ranges().in_range(x, z, margin)
}
pub fn band(&self) -> Option<Band> {
self.band
}
/// Generate, light and keep the chunks of `band` (none: stop), and let
/// go of what earlier bands held as it falls out of range. The chunks a
/// band reaches are ranked for decoration row by row, as they first come
/// within reach, so that a task decorates in the same order however its
/// work is timed and shared among the workers.
pub fn set_band(&mut self, band: Option<Band>) {
if self.band == band {
return;
}
self.band = band;
self.generation_scan.exhausted = false;
let Some(band) = band else {
return;
};
// Decoration reaches two chunks past the band and generation three.
const REACH: i32 = 4;
let (x0, x1) = (band.x0 - REACH, band.x1 + REACH);
let first = match self.band_ranked {
Some((rx0, rx1, next)) if (rx0, rx1) == (x0, x1) => next,
_ => band.z0 - REACH,
};
let end = band.z1 + REACH;
for z in first..=end {
for x in x0..=x1 {
self.decoration_order.rank_or_append(x, z);
}
}
self.band_ranked = Some((x0, x1, end.max(first - 1) + 1));
}
/// What holds up the columns `keys`, for a task that stopped advancing:
/// how many are not loaded, loaded but unlit with a light job in flight,
/// and unlit with none (those whose neighbours are all loaded are queued
/// for light again).
pub fn unstick_light(&mut self, world: &mut World, keys: &[(i32, i32)]) -> [usize; 3] {
let mut counts = [0; 3];
for &key in keys {
if !world.chunks.contains_key(&key) {
counts[0] += 1;
} else if self.lit.contains(&key) {
} else if self.lighting.contains_key(&key) {
counts[1] += 1;
} else {
counts[2] += 1;
if Self::region(world, key.0, key.1).is_some() {
world.dirty_light.insert(key);
}
}
}
counts
}
/// Wait up to `timeout` for a worker to finish a job; the next `update`
/// takes it. A server pumps generation with this between its ticks.
pub fn wait(&mut self, timeout: std::time::Duration) {
if let Ok(done) = self.results.recv_timeout(timeout) {
self.deferred.push(done);
}
}
}
/// `ChunkTrackingView.isInViewDistance`: whether the column (x, z) is drawn
/// at this render distance around `center`, the camera's column
@@ -1373,6 +1453,53 @@ struct Ranges<'a> {
/// `extra` as a grid, when one was made for it: `in_extra` without a
/// scan of every area.
cover: Option<&'a ExtraCover>,
band: Option<&'a Band>,
}
/// The chunks a pregeneration task (`/pregen`) wants lit now: rows `z0..=z1`
/// of the columns `x0..=x1`, those within `circle` (a centre and a radius in
/// chunks) when there is one. Like an extra area, it finishes its chunks and
/// the ring around them, so that its own chunks can be lit, and generation
/// reaches past that. A task moves it down a strip of columns a row at a
/// time, so each chunk is generated once and only the rows in flight stay
/// loaded.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Band {
pub x0: i32,
pub x1: i32,
pub z0: i32,
pub z1: i32,
pub circle: Option<((i32, i32), i32)>,
}
impl Band {
/// Whether (x, z) lies within `margin` (Chebyshev) of one of the band's
/// chunks: the square of side `2 * margin + 1` around it meets the
/// rectangle at a point inside the circle.
pub fn within(&self, x: i32, z: i32, margin: i32) -> bool {
let (x0, x1) = (
x.saturating_sub(margin).max(self.x0),
x.saturating_add(margin).min(self.x1),
);
let (z0, z1) = (
z.saturating_sub(margin).max(self.z0),
z.saturating_add(margin).min(self.z1),
);
if x0 > x1 || z0 > z1 {
return false;
}
self.circle.is_none_or(|((cx, cz), r)| {
let dx = i64::from(cx.clamp(x0, x1)) - i64::from(cx);
let dz = i64::from(cz.clamp(z0, z1)) - i64::from(cz);
dx * dx + dz * dz <= i64::from(r) * i64::from(r)
})
}
/// The band's chunks go after the players' (lower runs first), row by
/// row, as the task wants them lit.
fn priority(&self, x: i32, z: i32) -> i64 {
const AFTER_PLAYERS: i64 = 1 << 40;
AFTER_PLAYERS
+ (i64::from(z - self.z0 + 8).max(0) << 20)
+ i64::from(x - self.x0 + 8).max(0)
}
}
/// For every column near the extra areas, how far it lies outside the
/// nearest one's square (Chebyshev distance less its radius; 0 inside). A server has an area per player, and streaming asks of every
@@ -1468,10 +1595,14 @@ impl ExtraCover {
impl Ranges<'_> {
/// `distance`, or `rings` while the level loading screen is up.
fn priority(&self, x: i32, z: i32, loading: bool) -> i64 {
if loading {
let own = if loading {
self.rings(x, z)
} else {
self.distance(x, z)
};
match self.band {
Some(band) if band.within(x, z, 4) => own.min(band.priority(x, z)),
_ => own,
}
}
/// Ring by ring: the Chebyshev distance to the nearest centre first, then
@@ -1532,10 +1663,18 @@ impl Ranges<'_> {
let (dx, dz) = (gap(x - self.center.0), gap(z - self.center.1));
let r = i64::from(self.radius + 1);
// The extra areas are squares: their finished square grown by `reach`.
dx * dx + dz * dz <= r * r || self.in_extra(x, z, 1 + reach)
dx * dx + dz * dz <= r * r
|| self.in_extra(x, z, 1 + reach)
|| self.in_band(x, z, 1 + reach)
}
fn in_range(&self, x: i32, z: i32, margin: i32) -> bool {
self.in_main_range(x, z, margin) || self.in_extra(x, z, margin)
self.in_main_range(x, z, margin)
|| self.in_extra(x, z, margin)
|| self.in_band(x, z, margin)
}
/// Within `margin` of the pregeneration band.
fn in_band(&self, x: i32, z: i32, margin: i32) -> bool {
self.band.is_some_and(|b| b.within(x, z, margin))
}
/// Within `margin` of an extra area's square (Chebyshev distance, as
/// vanilla's ticket levels spread).
@@ -1659,6 +1798,7 @@ mod tests {
extra: &self.extra,
forced: &self.forced,
cover: None,
band: None,
},
world: &self.world,
generating: &self.generating,
@@ -1972,6 +2112,7 @@ mod tests {
extra: &[],
forced: &forced,
cover: None,
band: None,
};
let square = |x: i32, z: i32, n: i32| {
(-n..=n).flat_map(move |dz| (-n..=n).map(move |dx| (x + dx, z + dz)))
@@ -2027,6 +2168,7 @@ mod tests {
extra: &[],
forced: &forced,
cover: None,
band: None,
};
let at = |dx: i32, dz: i32| (2 + dx, -1 + dz);
let key = |(x, z): (i32, i32), loading| r.priority(x, z, loading);
@@ -2053,6 +2195,7 @@ mod tests {
extra: &[((radius * 2 + 9, 3), 4)],
forced: &forced,
cover: None,
band: None,
};
let span = radius * 3 + 20;
for x in -span..=span {
@@ -2093,6 +2236,7 @@ mod tests {
extra: &extra,
forced: &forced,
cover: None,
band: None,
};
let level = |x: i32, z: i32| 31 + ((x - 40).abs().max((z + 9).abs()) - view).max(0);
for x in 40 - view - 8..=40 + view + 8 {
@@ -2148,8 +2292,13 @@ mod cover_tests {
}
/// Every question streaming asks gets the same answer with the grid.
///
/// A debug build checks every fourth cell of each row, shifted by row and case so the cases still cover every
/// cell's position relative to the areas: the full check took 20 of the ~30 s of the crate's tests.
/// `cargo test --release -p horizon-client --lib the_grid_answers_as_the_scan` checks every cell.
#[test]
fn the_grid_answers_as_the_scan() {
let stride = if cfg!(debug_assertions) { 4 } else { 1 };
let mut seed = 0x9E37_79B9_7F4A_7C15u64;
let forced: HashSet<(i32, i32)> = [(40, -40)].into();
for case in 0..60 {
@@ -2161,11 +2310,13 @@ mod cover_tests {
extra: &extra,
forced: &forced,
cover,
band: None,
};
let (scan, grid) = (ranges(None), ranges(Some(&cover)));
let span = [4, 30, 120][case % 3] + 40;
for z in -span..=span {
for x in -span..=span {
let first = -span + (z + case as i32).rem_euclid(stride);
for x in (first..=span).step_by(stride as usize) {
for m in 0..=ExtraCover::REACH + 2 {
assert_eq!(
scan.in_extra(x, z, m),
@@ -2218,6 +2369,7 @@ mod cover_tests {
extra: &extra,
forced: &forced,
cover,
band: None,
};
// The columns streaming asks about: everything kept, and the
// generation scan's ring past it.
+105 -20
View File
@@ -128,6 +128,27 @@ fn text(nbt: &Compound, key: &str) -> Option<String> {
}
}
/// The client block entity for one of a chunk's generated block entities,
/// before a container's loot is unpacked: `StructureTemplate.placeInWorld`
/// hands the template's block entity NBT to the level's block entity.
fn generated_block_entity(block: &str, nbt: &Compound) -> Option<BlockEntity> {
let mut entity = BlockEntity::for_block(block)?;
match &mut entity {
// MonsterRoomFeature and the structures' setEntityId.
BlockEntity::Spawner(s) => {
**s = crate::mobs::spawner::Spawner::load(&Tag::Compound(nbt.clone()));
}
BlockEntity::TrialSpawner(s) => {
**s = crate::mobs::trial_spawner::TrialSpawner::load(&Tag::Compound(nbt.clone()));
}
BlockEntity::Vault(v) => **v = crate::vault::Vault::load(&Tag::Compound(nbt.clone())),
// BannerBlockEntity.loadAdditional: the End cities' black triangles.
BlockEntity::Banner(b) => *b = crate::save::decode_banner(&Tag::Compound(nbt.clone())),
_ => {}
}
Some(entity)
}
/// Client block entities for the generated block entities of a chunk.
pub fn block_entities(
chunk: &ProtoChunk,
@@ -137,19 +158,9 @@ pub fn block_entities(
for (pos, nbt) in &chunk.block_entities {
let [x, y, z] = *pos;
let block = chunk.block_name(x, y, z);
let Some(mut entity) = BlockEntity::for_block(block) else {
let Some(mut entity) = generated_block_entity(block, nbt) else {
continue;
};
// MonsterRoomFeature and the structures' setEntityId.
if let BlockEntity::Spawner(s) = &mut entity {
**s = crate::mobs::spawner::Spawner::load(&Tag::Compound(nbt.clone()));
}
if let BlockEntity::TrialSpawner(s) = &mut entity {
**s = crate::mobs::trial_spawner::TrialSpawner::load(&Tag::Compound(nbt.clone()));
}
if let BlockEntity::Vault(v) = &mut entity {
**v = crate::vault::Vault::load(&Tag::Compound(nbt.clone()));
}
if let (BlockEntity::Chest(slots), Some(table), Some(loot)) =
(&mut entity, text(nbt, "LootTable"), loot)
{
@@ -165,14 +176,10 @@ pub fn block_entities(
.iter_mut()
.zip(loot.fill(&table, seed, 27, Some(biome)))
{
*slot = stack.map(|s| {
let mut item = loot_item(&s);
// An ominous bottle's level (the default, 0, is no component).
if let Some(level) = s.ominous_amplifier.filter(|l| *l > 0) {
item.data_mut().ominous_amplifier = Some(level.clamp(0, 4) as u8);
}
item
});
// With what the rolls put on them: enchantments
// (enchant_with_levels in End city chests), potions and an
// ominous bottle's level.
*slot = stack.map(|s| loot_item_with_components(&s));
}
}
out.push(((x, y, z), entity));
@@ -211,7 +218,7 @@ pub fn unpack_entity_loot(
let slots: Vec<Option<ItemStack>> = loot
.fill(&table, seed, 27, Some(biome))
.into_iter()
.map(|s| s.map(|s| loot_item(&s)))
.map(|s| s.map(|s| loot_item_with_components(&s)))
.collect();
nbt.remove(&JavaString::from("LootTable"));
nbt.remove(&JavaString::from("LootTableSeed"));
@@ -224,6 +231,45 @@ mod tests {
use super::*;
use horizon_world::loot::{ExplorationMap, LootStack};
#[test]
fn placed_chest_loot_keeps_its_enchantments_and_potions() {
let stack =
|item: &str, enchantments: Vec<(String, i32)>, potion: Option<&str>| LootStack {
item: item.into(),
count: 1,
damage: 0,
enchantments,
potion: potion.map(str::to_owned),
map: false,
exploration: None,
ominous_amplifier: None,
};
// An End city chest's enchant_with_levels roll.
let boots = loot_item_with_components(&stack(
"minecraft:diamond_boots",
vec![
("minecraft:protection".into(), 3),
("minecraft:unbreaking".into(), 2),
],
None,
));
assert_eq!(boots.enchantments().len(), 2);
// A book's go into stored_enchantments.
let book = loot_item_with_components(&stack(
"minecraft:enchanted_book",
vec![("minecraft:mending".into(), 1)],
None,
));
assert!(book.enchantments().is_empty());
assert_eq!(book.stored_enchantments().len(), 1);
let arrow = loot_item_with_components(&stack(
"minecraft:tipped_arrow",
Vec::new(),
Some("minecraft:slowness"),
));
assert_eq!(arrow.potion(), Some("minecraft:slowness"));
}
#[test]
fn exploration_maps_wait_in_custom_data_until_searched() {
let mut stack = LootStack {
@@ -278,4 +324,43 @@ mod tests {
stack.exploration = None;
assert!(!has_pending_map(&loot_item(&stack)));
}
/// The End cities' tower tops save their banners' patterns in the
/// template; the placed banner must keep them (`EndCityPieces`).
#[test]
fn a_generated_banner_keeps_its_template_patterns() {
let patterns = Tag::List(vec![
Tag::compound([
("color", Tag::String("black".into())),
("pattern", Tag::String("minecraft:triangle_top".into())),
]),
Tag::compound([
("color", Tag::String("black".into())),
("pattern", Tag::String("minecraft:triangle_bottom".into())),
]),
]);
let nbt = Tag::compound([
("id", Tag::String("minecraft:banner".into())),
("patterns", patterns),
]);
let Tag::Compound(nbt) = nbt else {
unreachable!()
};
let Some(BlockEntity::Banner(banner)) =
generated_block_entity("minecraft:magenta_banner", &nbt)
else {
panic!("a banner block makes a banner block entity");
};
assert_eq!(banner.layers.len(), 2);
assert_eq!(&*banner.layers[0].pattern, "minecraft:triangle_top");
assert_eq!(&*banner.layers[1].pattern, "minecraft:triangle_bottom");
assert_eq!(&*banner.layers[0].color, "black");
// A banner the template saved without patterns stays bare.
let Some(BlockEntity::Banner(bare)) =
generated_block_entity("minecraft:white_banner", &Compound::new())
else {
panic!("a banner block makes a banner block entity");
};
assert!(bare.layers.is_empty());
}
}
@@ -0,0 +1,16 @@
[package]
name = "predator-missile"
version = "0.1.0"
edition.workspace = true
license.workspace = true
publish = false
description = "Example Horizon mod: a missile you launch with /missile and steer with the mouse"
[lib]
crate-type = ["cdylib"]
[dependencies]
horizon-mod-sdk = { path = "../.." }
[lints]
workspace = true
@@ -0,0 +1,8 @@
id = "predator_missile"
name = "Predator Missile"
version = "0.1.0"
description = "/missile launches a guided missile 60 blocks above you. Steer it with the mouse; it explodes on whatever it hits. Needs cheats (it runs vanilla commands as you)."
authors = ["Horizon"]
api = "0.1"
side = "client"
capabilities = ["game-read", "hud", "chat", "command"]
@@ -0,0 +1,161 @@
//! Example Horizon mod (`docs/mods.md`): `/missile` turns you into a guided
//! missile 60 blocks up. You fly along your own look, so the mouse steers;
//! on a block or a mob a stack of primed TNT goes off, and you are put back
//! where you launched. Built from vanilla commands (capability `command`),
//! so it needs cheats.
use horizon_mod_sdk::{
Mod, Registration,
command::run,
game::{self, Player},
hud::{self, Corner, Line},
};
/// Blocks per tick (a fired arrow does about 3).
const SPEED: f64 = 1.5;
const ALTITUDE: i32 = 60;
/// Ticks of flight before the missile detonates on its own.
const FUEL: u32 = 30 * 20;
/// Ticks to watch the blast before going back.
const AFTERMATH: u32 = 40;
/// Primed TNT per blast: the stack's explosions add up.
const WARHEAD: usize = 4;
/// What the impact checks print; the mod hears it through the chat hook.
const IMPACT: &str = "Predator: impact";
enum State {
Idle,
Flying { from: Player, ticks: u32 },
Aftermath { from: Player, ticks: u32 },
}
struct Predator {
state: State,
}
fn launch() {
run("gamerule send_command_feedback false");
run("execute if entity @s[gamemode=creative] run tag @s add pm_creative");
run("gamemode spectator");
run(&format!("tp @s ~ ~{ALTITUDE} ~ ~ 60"));
run("effect give @s night_vision 60 0 true");
// The thermal camera: every mob outlined.
run("effect give @e[type=!player,distance=..256] glowing 60 0 true");
}
/// One tick of flight: stop at the surface (the next step would enter a
/// block or a mob), else move one step along the look.
fn fly() {
run(&format!(
"execute at @s unless block ^ ^ ^{SPEED} #minecraft:air run tellraw @s \"{IMPACT}\""
));
run(&format!(
"execute at @s positioned ^ ^ ^{SPEED} if entity @e[type=!player,distance=..2] run tellraw @s \"{IMPACT}\""
));
run(&format!(
"execute at @s if block ^ ^ ^{SPEED} #minecraft:air run tp @s ^ ^ ^{SPEED}"
));
}
fn detonate() {
for _ in 0..WARHEAD {
run("execute at @s run summon tnt ^ ^ ^1 {fuse:0s}");
}
}
fn land(from: &Player) {
run(&format!(
"tp @s {} {} {} {} {}",
from.x, from.y, from.z, from.yaw, from.pitch
));
run("execute if entity @s[tag=pm_creative] run gamemode creative");
run("execute unless entity @s[tag=pm_creative] run gamemode survival");
run("execute if entity @s[tag=pm_creative] run tag @s remove pm_creative");
run("effect clear @s night_vision");
// shortcut: assumes feedback was on before launch, as in a new world.
run("gamerule send_command_feedback true");
}
fn panel(p: &Player, ticks: u32) -> Vec<Line> {
let green = 0x7CFF7C;
let left = (FUEL - ticks.min(FUEL)) / 20;
[
"PREDATOR".to_string(),
format!("ALT {:.0}", p.y),
format!("FUEL {left}s"),
"Mouse to steer, /missile to detonate".to_string(),
]
.into_iter()
.map(|text| Line { text, color: green })
.collect()
}
impl Predator {
fn impact(&mut self) {
if let State::Flying { from, .. } = &self.state {
detonate();
self.state = State::Aftermath {
from: from.clone(),
ticks: 0,
};
}
}
}
impl Mod for Predator {
fn init() -> Result<(Self, Registration), String> {
let hooks = Registration::new().command("missile").on_tick().on_chat();
Ok((Predator { state: State::Idle }, hooks))
}
fn tick(&mut self, _tick: u64) {
match &mut self.state {
State::Idle => {}
State::Flying { ticks, .. } => {
*ticks += 1;
let ticks = *ticks;
if ticks >= FUEL {
return self.impact();
}
// The first tick waits for the launch teleport.
if ticks > 1 {
fly();
}
if let Some(p) = game::get_player() {
hud::set_panel(Corner::TopLeft, &panel(&p, ticks));
}
}
State::Aftermath { from, ticks } => {
*ticks += 1;
if *ticks >= AFTERMATH {
land(from);
hud::clear();
self.state = State::Idle;
}
}
}
}
fn chat(&mut self, text: &str) {
if text.contains(IMPACT) {
self.impact();
}
}
fn command(&mut self, _name: &str, _args: &str) -> Result<Option<String>, String> {
match self.state {
State::Idle => {
let from = game::get_player().ok_or("Not in a world")?;
launch();
self.state = State::Flying { from, ticks: 0 };
Ok(Some("Missile away".into()))
}
State::Flying { .. } => {
self.impact();
Ok(None)
}
State::Aftermath { .. } => Err("Wait for the blast to clear".into()),
}
}
}
horizon_mod_sdk::export_mod!(Predator);
+2 -2
View File
@@ -21,7 +21,7 @@
//! ```
//!
//! Every interface but `log` is a capability: list the ones the mod uses in
//! `horizon-mod.toml` (`game-read`, `chat`, `hud`, `storage`, `ui`).
//! `horizon-mod.toml` (`game-read`, `chat`, `hud`, `storage`, `command`, `ui`).
//!
//! Widgets (capability `ui`): a mod shows a screen it owns with
//! [`ui::open_screen`] and a page in Options > Mods with
@@ -38,7 +38,7 @@ wit_bindgen::generate!({
pub use exports::horizon::mods::events::{Guest as __Guest, Registration};
pub use exports::horizon::mods::ui_events::Guest as __UiEventsGuest;
pub use horizon::mods::{chat, game, hud, log, storage, ui};
pub use horizon::mods::{chat, command, game, hud, log, render, storage, ui};
/// The game's calls into a mod. Only the hooks [`Registration`] asks for
/// are called; the others may stay as their defaults.
+91
View File
@@ -86,6 +86,14 @@ interface storage {
delete: func(key: string);
}
/// Capability `command`: vanilla commands run as the player, with the
/// player's permissions, after the hook call returns (8 per call). A mod's
/// own commands are not run this way.
interface command {
/// `line` without its slash; its feedback goes to the chat as if typed.
run: func(line: string);
}
/// Capability `ui`: widgets the game draws with its own menus' widgets, for
/// a screen the mod owns and a settings page in Options > Mods. Layout and
/// drawing are the game's: a mod says what its widgets hold, never where
@@ -163,6 +171,82 @@ interface ui-events {
settings-opened: func();
}
/// Capability `render` (experimental: the manifest needs
/// `experimental = ["render"]`): shapes the game draws for the mod with its
/// own renderer, kept until the mod replaces them (no call per frame). A mod
/// never supplies shader code.
interface render {
/// A rectangle in GUI pixels: the top left corner and the size.
record rect {
x: f32,
y: f32,
w: f32,
h: f32,
}
/// A point in GUI pixels from the screen's top left.
record point {
x: f32,
y: f32,
}
record fill-shape {
rect: rect,
/// 0xAARRGGBB.
color: u32,
}
/// Any four-cornered shape (a wheel's segment, an arrow): the corners
/// in order round it.
record quad-shape {
corners: tuple<point, point, point, point>,
/// 0xAARRGGBB.
color: u32,
}
record sprite-shape {
/// One of the game's GUI sprites, as `gui/sprites/` names them
/// (`hud/heart/full`, `widget/button`), the resource packs' versions
/// included. An unknown one draws nothing.
sprite: string,
rect: rect,
/// A tint, 0xAARRGGBB: 0xFFFFFFFF draws it as it is.
color: u32,
}
record item-shape {
/// `minecraft:diamond`, `<mod>:<path>`; an unknown one draws nothing.
item: string,
/// The icon's top left.
at: point,
/// 1.0 is the hotbar's 16 × 16.
scale: f32,
}
record text-shape {
/// Up to 256 characters, one line.
text: string,
at: point,
/// 0xRRGGBB, as `hud`'s lines.
color: u32,
/// Centred on `at.x` instead of starting there.
centered: bool,
shadow: bool,
}
variant shape {
fill(fill-shape),
quad(quad-shape),
sprite(sprite-shape),
item(item-shape),
text(text-shape),
}
/// Replaces the mod's HUD shapes: up to 1024, drawn every frame in
/// order over the HUD (after the hotbar, under chat and screens), hidden
/// with the HUD (F1). An empty list removes them.
set-hud: func(shapes: list<shape>);
/// The screen's size in GUI pixels (width, height), as of this tick.
gui-size: func() -> tuple<f32, f32>;
/// Shakes the camera: a jolt of `strength` (0 to 1, 1 the hardest) that
/// fades out over `seconds` (at most 10). Jolts add up to 1. The view
/// turns, the player doesn't, and the player's Distortion Effects option
/// scales it (0 turns it off).
shake: func(strength: f32, seconds: f32);
}
/// What the game calls. Only the hooks `init` asks for are ever called.
interface events {
record registration {
@@ -194,6 +278,7 @@ world horizon-mod {
import chat;
import hud;
import storage;
import command;
export events;
}
@@ -203,6 +288,7 @@ world horizon-mod {
world horizon-ui-mod {
include horizon-mod;
import ui;
import render;
export ui-events;
}
@@ -211,3 +297,8 @@ world ui-host {
import ui;
export ui-events;
}
/// The game's side of capability `render` (imports only).
world render-host {
import render;
}
+19 -2
View File
@@ -78,21 +78,28 @@ pub enum Capability {
Chat,
Hud,
Storage,
/// Runs vanilla commands as the player, with the player's permissions.
Command,
/// New blocks and items from the mod's `data/` (API 0.2): no WIT
/// interface, the game reads them before the world loads.
Registries,
/// A screen the mod owns and a settings page in Options > Mods, drawn
/// with the menus' own widgets (interface `ui`, calls `ui-events`).
Ui,
/// Shapes on the HUD and the camera's shake, drawn by the game's own
/// renderer (interface `render`).
Render,
}
impl Capability {
pub const ALL: [Capability; 6] = [
pub const ALL: [Capability; 8] = [
Capability::GameRead,
Capability::Chat,
Capability::Hud,
Capability::Storage,
Capability::Command,
Capability::Registries,
Capability::Ui,
Capability::Render,
];
/// The WIT interface it grants, in `horizon:mods`.
pub fn interface(self) -> &'static str {
@@ -101,14 +108,16 @@ impl Capability {
Capability::Chat => "chat",
Capability::Hud => "hud",
Capability::Storage => "storage",
Capability::Command => "command",
Capability::Registries => "registries",
Capability::Ui => "ui",
Capability::Render => "render",
}
}
/// May change in any release: a mod uses it only with
/// `experimental = ["<name>"]` in its manifest.
pub fn is_experimental(self) -> bool {
matches!(self, Capability::Ui)
matches!(self, Capability::Ui | Capability::Render)
}
/// What the player reads before turning the mod on: "May <this>".
pub fn describe(self) -> &'static str {
@@ -117,8 +126,10 @@ impl Capability {
Capability::Chat => "show you chat messages",
Capability::Hud => "draw text on your screen",
Capability::Storage => "save its own settings to disk",
Capability::Command => "run commands as you, with your permissions",
Capability::Registries => "add blocks and items to your worlds",
Capability::Ui => "show its own screens and a settings page",
Capability::Render => "draw on your screen and shake the camera",
}
}
/// The capability a component import needs, `Ok(None)` for one every
@@ -148,8 +159,10 @@ impl fmt::Display for Capability {
Capability::Chat => "chat",
Capability::Hud => "hud",
Capability::Storage => "storage",
Capability::Command => "command",
Capability::Registries => "registries",
Capability::Ui => "ui",
Capability::Render => "render",
};
f.write_str(name)
}
@@ -397,6 +410,10 @@ capabilities = ["game-read", "hud"]
storage,
Err("uses capability storage, missing from horizon-mod.toml".into())
);
assert_eq!(
m.check_imports(["horizon:mods/command@0.1.0"]),
Err("uses capability command, missing from horizon-mod.toml".into())
);
let with_storage = EXAMPLE.replace("\"hud\"", "\"hud\", \"storage\"");
let m = Manifest::parse(&with_storage).unwrap();
assert_eq!(m.check_imports(["horizon:mods/storage@0.1.0"]), Ok(()));
+97 -1
View File
@@ -32,7 +32,7 @@ pub use bindings::horizon::mods::{
use bindings::{
HorizonMod,
exports::horizon::mods::events::Registration,
horizon::mods::{chat, game, hud, log as mod_log, storage},
horizon::mods::{chat, command, game, hud, log as mod_log, storage},
};
/// The widget model a `ui` screen or settings page shows (WIT `interface
/// ui`), for the game to draw with its menus' own widgets.
@@ -40,6 +40,19 @@ pub use ui_bindings::horizon::mods::ui::{
ItemWidget, ListWidget, Screen as UiScreen, TextWidget, Widget as UiWidget,
};
use ui_bindings::{UiHost, horizon::mods::ui};
/// Capability `render`'s imports (docs/mods.md): the game draws the shapes.
mod render_bindings {
wasmtime::component::bindgen!({
path: "../horizon-mod-sdk/wit",
world: "render-host",
});
}
/// What capability `render` asks the game to draw (WIT `interface render`).
pub use render_bindings::horizon::mods::render::{
FillShape, ItemShape, Point as RenderPoint, QuadShape, Rect as RenderRect, Shape, SpriteShape,
TextShape,
};
use render_bindings::{RenderHost, horizon::mods::render};
/// A mod's linear memory cap.
const MEMORY: usize = 64 << 20;
@@ -70,6 +83,8 @@ const STORAGE_WRITE_BUDGET: u64 = 1 << 20;
pub struct Snapshot {
pub player: Option<Player>,
pub clock: Option<Clock>,
/// The screen in GUI pixels (capability `render`'s `gui-size`).
pub gui_size: (f32, f32),
}
/// What mods asked the game to show since the last [`Mods::drain`].
@@ -79,6 +94,9 @@ pub enum Output {
Chat { name: String, text: String },
/// A mod command's feedback, red when `error`.
Feedback { text: String, error: bool },
/// `command.run`: a vanilla command line, without its slash, to run as
/// the player.
Command { line: String },
/// The mod was turned off: it trapped, overran or failed to start.
Stopped { name: String, reason: String },
/// `ui.open-screen`: the mod's entry in `entries()`; what it shows is
@@ -86,6 +104,8 @@ pub enum Output {
OpenScreen { owner: usize },
/// `ui.close-screen`: the mod closed its own screen (no `closed` back).
CloseScreen { owner: usize },
/// `render.shake`: strength 0 to 1, fading out over `seconds` (≤ 10).
Shake { strength: f32, seconds: f32 },
}
/// A mod's text panel, drawn every frame until it changes.
@@ -153,6 +173,8 @@ struct State {
out: Vec<Output>,
panel: Option<Panel>,
ui: UiState,
/// Capability `render`'s HUD shapes, drawn until replaced.
shapes: Vec<Shape>,
/// Capability `storage`'s folder; made on first `set`.
data_dir: PathBuf,
/// Bytes `storage::set` may still write before this hook call ends
@@ -231,6 +253,8 @@ impl Mods {
.map_err(|e| e.to_string())?;
UiHost::add_to_linker::<State, HasSelf<State>>(&mut linker, |s| s)
.map_err(|e| e.to_string())?;
RenderHost::add_to_linker::<State, HasSelf<State>>(&mut linker, |s| s)
.map_err(|e| e.to_string())?;
self.engine = Some((engine, linker));
}
Ok(self.engine.as_ref().expect("made above"))
@@ -290,6 +314,7 @@ impl Mods {
out: Vec::new(),
panel: None,
ui: UiState::default(),
shapes: Vec::new(),
data_dir,
storage_budget: STORAGE_WRITE_BUDGET,
limits: StoreLimitsBuilder::new()
@@ -481,6 +506,14 @@ impl Mods {
.iter()
.filter_map(|e| e.run.as_ref()?.store.data().panel.as_ref())
}
/// The running mods' HUD shapes (capability `render`), each mod's in
/// its own order, mods in load order.
pub fn hud_shapes(&self) -> impl Iterator<Item = &Shape> {
self.entries
.iter()
.filter_map(|e| e.run.as_ref())
.flat_map(|r| &r.store.data().shapes)
}
/// The screen mod `i` last pushed with `ui.open-screen`, while it is up.
pub fn screen(&self, i: usize) -> Option<&UiScreen> {
self.entries
@@ -668,6 +701,69 @@ impl chat::Host for State {
}
}
}
/// HUD shapes one mod may keep (capability `render`).
const SHAPES: usize = 1024;
/// The longest jolt `render.shake` makes, in seconds.
const SHAKE_SECONDS: f32 = 10.0;
impl render::Host for State {
fn set_hud(&mut self, shapes: Vec<Shape>) {
self.shapes = shapes
.into_iter()
.take(SHAPES)
.map(|shape| match shape {
Shape::Sprite(mut s) => {
s.sprite = clip(s.sprite);
Shape::Sprite(s)
}
Shape::Item(mut i) => {
i.item = clip(i.item);
i.scale = if i.scale.is_finite() {
i.scale.clamp(0.0, 16.0)
} else {
1.0
};
Shape::Item(i)
}
Shape::Text(mut t) => {
t.text = clip(t.text);
t.color &= 0xFF_FFFF;
Shape::Text(t)
}
other => other,
})
.collect();
}
fn gui_size(&mut self) -> (f32, f32) {
self.snapshot.gui_size
}
fn shake(&mut self, strength: f32, seconds: f32) {
let ok = |v: f32, max: f32| {
if v.is_finite() {
v.clamp(0.0, max)
} else {
0.0
}
};
let (strength, seconds) = (ok(strength, 1.0), ok(seconds, SHAKE_SECONDS));
if strength > 0.0 && seconds > 0.0 {
self.out.push(Output::Shake { strength, seconds });
}
}
}
impl command::Host for State {
fn run(&mut self, line: String) {
let queued = self
.out
.iter()
.filter(|o| matches!(o, Output::Command { .. }))
.count();
if queued < CHAT_PER_CALL {
self.out.push(Output::Command { line: clip(line) });
}
}
}
impl hud::Host for State {
fn set_panel(&mut self, corner: Corner, lines: Vec<Line>) {
let lines = lines
+76 -7
View File
@@ -1,7 +1,8 @@
//! Loads real mods built with the SDK: the coords-hud example and a guest
//! Loads real mods built with the SDK: the coords-hud and predator-missile
//! examples and a guest
//! that misbehaves on command. Needs `rustup target add wasm32-unknown-unknown`.
use horizon_mods::{
Clock, Corner, Mods, Output, Package, Player, Snapshot, Status, manifest::componentize,
Clock, Corner, Mods, Output, Package, Player, Shape, Snapshot, Status, manifest::componentize,
};
use std::{
path::{Path, PathBuf},
@@ -22,11 +23,11 @@ id = "test_ui_guest"
name = "Test UI Guest"
version = "0.1.0"
api = "0.1"
capabilities = ["chat", "ui"]
experimental = ["ui"]
capabilities = ["chat", "ui", "render"]
experimental = ["ui", "render"]
"#;
/// Builds both mods once and writes their `.hmod` files; returns the folder.
/// Builds the mods once and writes their `.hmod` files; returns the folder.
fn mods_dir() -> &'static Path {
static DIR: OnceLock<PathBuf> = OnceLock::new();
DIR.get_or_init(|| {
@@ -39,6 +40,8 @@ fn mods_dir() -> &'static Path {
"-p",
"coords-hud",
"-p",
"predator-missile",
"-p",
"horizon-mods-test-guest",
"-p",
"horizon-mods-test-ui-guest",
@@ -54,9 +57,13 @@ fn mods_dir() -> &'static Path {
let built = target.join("wasm32-unknown-unknown/release");
let dir = target.join("mods");
std::fs::create_dir_all(&dir).unwrap();
let example = root.join("crates/horizon-mod-sdk/examples/coords-hud/horizon-mod.toml");
let example = |name: &str| {
let path = format!("crates/horizon-mod-sdk/examples/{name}/horizon-mod.toml");
std::fs::read_to_string(root.join(path)).unwrap()
};
let mods = [
("coords_hud", std::fs::read_to_string(example).unwrap()),
("coords_hud", example("coords-hud")),
("predator_missile", example("predator-missile")),
("horizon_mods_test_guest", GUEST_MANIFEST.to_string()),
("horizon_mods_test_ui_guest", UI_MANIFEST.to_string()),
];
@@ -89,9 +96,40 @@ fn player() -> Snapshot {
game_time: 100,
day_time: 24000 + 6000,
}),
gui_size: (427.0, 240.0),
}
}
/// Capability `command`: `/missile` queues vanilla commands, and the chat
/// line its impact check prints sets off the warhead.
#[test]
fn predator_missile_runs_commands() {
let mut mods = load("predator_missile.hmod");
assert_eq!(mods.entries()[0].status, Status::Running);
mods.set_snapshot(player());
let commands = |mods: &mut Mods| -> Vec<String> {
mods.drain()
.into_iter()
.filter_map(|o| match o {
Output::Command { line } => Some(line),
_ => None,
})
.collect()
};
assert!(mods.command("missile"));
let launch = commands(&mut mods);
assert_eq!(launch.len(), 6);
assert!(launch.contains(&"gamemode spectator".to_string()));
mods.tick(1);
mods.tick(2);
let flight = commands(&mut mods);
assert!(flight.iter().any(|l| l.ends_with("run tp @s ^ ^ ^1.5")));
mods.chat("Predator: impact");
let blast = commands(&mut mods);
assert_eq!(blast.len(), 4);
assert!(blast.iter().all(|l| l.contains("summon tnt")));
}
#[test]
fn coords_hud_draws_and_answers() {
let mut mods = load("coords_hud.hmod");
@@ -416,6 +454,37 @@ fn ui_screens_settings_and_events() {
assert!(mods.screen(0).is_none());
}
#[test]
fn render_shapes_and_shakes() {
let mut mods = load("horizon_mods_test_ui_guest.hmod");
mods.set_snapshot(Snapshot {
gui_size: (427.0, 240.0),
..Snapshot::default()
});
assert_eq!(mods.hud_shapes().count(), 0);
assert!(mods.command("render_hud"));
let shapes: Vec<_> = mods.hud_shapes().cloned().collect();
assert_eq!(shapes.len(), 5);
// The guest read `gui-size`; text is clipped to 256 characters.
let Shape::Text(t) = &shapes[4] else {
panic!("{:?}", shapes[4])
};
assert!(t.text.starts_with("gui 427x240 "), "{}", t.text);
assert_eq!(t.text.chars().count(), 256);
assert!(mods.command("render_clear"));
assert_eq!(mods.hud_shapes().count(), 0);
// One jolt, clamped; zero and NaN shake nothing.
mods.drain();
assert!(mods.command("render_shake"));
assert_eq!(
mods.drain(),
[Output::Shake {
strength: 1.0,
seconds: 10.0
}]
);
}
#[test]
fn content_only_mods_load_without_an_engine() {
let manifest = r#"
+68 -3
View File
@@ -1,11 +1,61 @@
//! A mod for the runtime's `ui` tests: `/ui_screen` opens a screen with one
//! of each widget, `/ui_settings` sets a settings page, `/ui_close` closes
//! the screen; the `ui-events` calls answer in chat so the host sees them.
//! `/render_hud`, `/render_clear` and `/render_shake` test `render`.
use horizon_mod_sdk::{
Mod, Registration, chat,
render::{self, FillShape, ItemShape, Point, QuadShape, Rect, Shape, SpriteShape, TextShape},
ui::{self, ItemWidget, ListWidget, Screen, TextWidget, Widget},
};
/// One of each shape, the text too long by 44 characters.
fn shapes() -> Vec<Shape> {
let (w, h) = render::gui_size();
let p = |x, y| Point { x, y };
vec![
Shape::Fill(FillShape {
rect: Rect {
x: 4.0,
y: 4.0,
w: 60.0,
h: 20.0,
},
color: 0x80FF0000,
}),
Shape::Quad(QuadShape {
corners: (
p(w / 2.0, 10.0),
p(w / 2.0 + 20.0, 30.0),
p(w / 2.0, 50.0),
p(w / 2.0 - 20.0, 30.0),
),
color: 0xC000FF00,
}),
Shape::Sprite(SpriteShape {
sprite: "hud/heart/full".into(),
rect: Rect {
x: 70.0,
y: 4.0,
w: 18.0,
h: 18.0,
},
color: 0xFFFFFFFF,
}),
Shape::Item(ItemShape {
item: "minecraft:diamond".into(),
at: p(92.0, 4.0),
scale: 2.0,
}),
Shape::Text(TextShape {
text: format!("gui {w}x{h} {}", "x".repeat(300)),
at: p(w / 2.0, h / 2.0 - 30.0),
color: 0xFFFF55,
centered: true,
shadow: true,
}),
]
}
struct Guest;
fn widgets() -> Vec<Widget> {
@@ -30,9 +80,16 @@ fn widgets() -> Vec<Widget> {
impl Mod for Guest {
fn init() -> Result<(Self, Registration), String> {
let hooks = ["ui_screen", "ui_settings", "ui_close"]
.into_iter()
.fold(Registration::new(), Registration::command);
let hooks = [
"ui_screen",
"ui_settings",
"ui_close",
"render_hud",
"render_clear",
"render_shake",
]
.into_iter()
.fold(Registration::new(), Registration::command);
Ok((Guest, hooks))
}
fn command(&mut self, name: &str, _args: &str) -> Result<Option<String>, String> {
@@ -43,6 +100,14 @@ impl Mod for Guest {
}),
"ui_settings" => ui::set_settings(&widgets()),
"ui_close" => ui::close_screen(),
"render_hud" => render::set_hud(&shapes()),
"render_clear" => render::set_hud(&[]),
// Clamped to (1, 10); the two after it shake nothing.
"render_shake" => {
render::shake(2.0, 99.0);
render::shake(0.0, 1.0);
render::shake(f32::NAN, 1.0);
}
_ => {}
}
Ok(None)
+4
View File
@@ -6,11 +6,15 @@ license = "MIT"
description = "Prepares Horizon's resources from the player's own Minecraft: imports and checks the pinned jars, runs the vanilla exporters, writes the resource folder"
[dependencies]
crc32fast = "1"
regex = { version = "1", default-features = false, features = ["std", "unicode-perl"] }
sha1 = "0.10"
sha2 = "0.10"
zip = { version = "2", default-features = false, features = ["deflate"] }
[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies]
libc = "0.2"
[build-dependencies]
sha2 = "0.10"
@@ -0,0 +1,3 @@
# OUTPUT_VERSION (src/lib.rs) and the digest of what it prepares (python3 tools/prepare_output.py digest),
# one line per version, never edited: a change to the prepared files appends the next version.
1 0a608bf4115a8ed2e2c4f5f2b2609dc10c54e63479af8e56fabaa10af1060389
+35 -43
View File
@@ -143,31 +143,17 @@ pub(crate) fn import_copy(
algorithm: &str,
hint: &str,
) -> Result<PathBuf, String> {
fs::create_dir_all(destination.parent().unwrap()).map_err(|e| e.to_string())?;
if destination.exists() && digest(destination, algorithm)? == checksum {
return Ok(destination.to_path_buf());
}
if !source.is_file() {
return Err(format!("Missing {}. {hint}", source.display())
.trim()
.to_string());
}
let actual = digest(source, algorithm)?;
if actual != checksum {
return Err(format!(
"{} is not the pinned file: expected {algorithm} {checksum}, got {actual}",
source.display()
));
}
let temporary = destination.with_file_name(format!(
".{}.partial-{}",
destination.file_name().unwrap().to_string_lossy(),
std::process::id()
));
let copied = fs::copy(source, &temporary).and_then(|_| fs::rename(&temporary, destination));
let _ = fs::remove_file(&temporary);
copied.map_err(|e| format!("{}: {e}", destination.display()))?;
Ok(destination.to_path_buf())
crate::files::Import::stage(source, destination, checksum, algorithm, false)
.map_err(|error| {
if !source.is_file() {
format!("Missing {}. {hint}", source.display())
.trim()
.to_string()
} else {
error
}
})?
.commit()
}
/// The pinned version JSON from the launcher's versions/ directory. The launcher's copy wins
@@ -653,9 +639,17 @@ pub struct Artifacts {
/// Reuses checked imports from this root or another installed version beside it.
pub fn reuse_imported(root: &Path, log: Log) -> Result<bool, String> {
Ok(reuse_verified(root, log)?.is_some())
}
/// Keep this preparation's verified artifacts and client libraries for the exporters.
pub(crate) fn reuse_verified(
root: &Path,
log: Log,
) -> Result<Option<(Artifacts, Vec<PathBuf>)>, String> {
let root = fs::canonicalize(root).map_err(|e| format!("{}: {e}", root.display()))?;
let lock = Lock::read(&root)?;
let checked = |directory: PathBuf| -> Result<Vec<PathBuf>, String> {
let checked = |directory: PathBuf| -> Result<(Artifacts, Vec<PathBuf>), String> {
let candidate = Lock {
json: lock.json.clone(),
version: lock.version.clone(),
@@ -664,17 +658,9 @@ pub fn reuse_imported(root: &Path, log: Log) -> Result<bool, String> {
};
let found = verify(&candidate)?;
let client = client_libraries(&candidate, &found.manifest)?;
let mut files = vec![
candidate.directory.join("manifest.json"),
candidate.directory.join("jars/client.jar"),
candidate.directory.join("jars/server.jar"),
found.jars["server"].clone(),
];
files.extend(found.libraries);
files.extend(client);
Ok(files)
Ok((found, client))
};
if checked(lock.directory.clone()).is_ok() {
if let Ok((found, client_libraries)) = checked(lock.directory.clone()) {
if [
"prepared.json",
"reports/client-inventory.json",
@@ -683,8 +669,6 @@ pub fn reuse_imported(root: &Path, log: Log) -> Result<bool, String> {
.iter()
.any(|path| !lock.directory.join(path).is_file())
{
let found = verify(&lock)?;
let client_libraries = client_libraries(&lock, &found.manifest)?;
write_import_metadata(
&root,
&lock,
@@ -696,10 +680,10 @@ pub fn reuse_imported(root: &Path, log: Log) -> Result<bool, String> {
)?;
}
log(&format!("Using imported Minecraft {}", lock.version));
return Ok(true);
return Ok(Some((found, client_libraries)));
}
let Some(versions) = root.parent() else {
return Ok(false);
return Ok(None);
};
// Sibling discovery is intended for the launcher's versions/ folder.
let mut siblings: Vec<_> = fs::read_dir(versions)
@@ -711,9 +695,17 @@ pub fn reuse_imported(root: &Path, log: Log) -> Result<bool, String> {
siblings.sort();
for sibling in siblings {
let source = sibling.join(".local/minecraft").join(&lock.version);
let Ok(files) = checked(source.clone()) else {
let Ok((found, client)) = checked(source.clone()) else {
continue;
};
let mut files = vec![
source.join("manifest.json"),
source.join("jars/client.jar"),
source.join("jars/server.jar"),
found.jars["server"].clone(),
];
files.extend(found.libraries);
files.extend(client);
for file in files {
let relative = file.strip_prefix(&source).map_err(|e| e.to_string())?;
import_copy(
@@ -740,9 +732,9 @@ pub fn reuse_imported(root: &Path, log: Log) -> Result<bool, String> {
lock.version,
sibling.display()
));
return Ok(true);
return Ok(Some((found, client_libraries)));
}
Ok(false)
Ok(None)
}
pub fn verify(lock: &Lock) -> Result<Artifacts, String> {
+180
View File
@@ -0,0 +1,180 @@
//! Independent copies, using filesystem cloning when available. Never hard links: exporters
//! rewrite prepared resources in place. Checked imports publish only after verifying the copy.
use crate::{digest, hex};
use sha2::Digest;
use std::fs;
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
fn clone_file(from: &Path, to: &Path) -> io::Result<()> {
#[cfg(target_os = "linux")]
{
use std::os::fd::AsRawFd;
let source = fs::File::open(from)?;
let target = fs::File::create(to)?;
// SAFETY: FICLONE takes a source fd, both descriptors stay open for the call.
if unsafe { libc::ioctl(target.as_raw_fd(), libc::FICLONE, source.as_raw_fd()) } == 0 {
return Ok(());
}
Err(io::Error::last_os_error())
}
#[cfg(target_os = "macos")]
{
use std::ffi::CString;
use std::os::unix::ffi::OsStrExt;
let path = |p: &Path| {
CString::new(p.as_os_str().as_bytes())
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))
};
let (source, target) = (path(from)?, path(to)?);
// SAFETY: both paths are valid NUL-terminated strings for the duration of the call.
if unsafe { libc::clonefile(source.as_ptr(), target.as_ptr(), 0) } == 0 {
return Ok(());
}
Err(io::Error::last_os_error())
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
let _ = (from, to);
Err(io::ErrorKind::Unsupported.into())
}
}
/// Copies an independent file, attempting a reflink first. The destination may be replaced.
pub fn clone_or_copy(from: &Path, to: &Path) -> io::Result<()> {
clone_file(from, to).or_else(|_| fs::copy(from, to).map(|_| ()))
}
fn copy_hash<D: Digest>(from: &Path, to: &Path) -> io::Result<String> {
let mut source = fs::File::open(from)?;
let mut target = fs::File::create(to)?;
let mut hash = D::new();
let mut buffer = vec![0; 64 * 1024];
loop {
let n = source.read(&mut buffer)?;
if n == 0 {
return Ok(hex(&hash.finalize()));
}
target.write_all(&buffer[..n])?;
hash.update(&buffer[..n]);
}
}
/// A verified replacement staged beside its destination. Dropping an unfinished set cleans
/// every temporary file, so a corrupt late panorama face cannot replace earlier placeholders.
pub(crate) struct Import {
temporary: Option<PathBuf>,
destination: PathBuf,
}
impl Import {
pub(crate) fn stage(
source: &Path,
destination: &Path,
checksum: &str,
algorithm: &str,
verified: bool,
) -> Result<Self, String> {
let mut import = Self {
temporary: None,
destination: destination.into(),
};
if verified || (destination.is_file() && digest(destination, algorithm)? == checksum) {
return Ok(import);
}
fs::create_dir_all(destination.parent().unwrap()).map_err(|e| e.to_string())?;
static COPIES: AtomicUsize = AtomicUsize::new(0);
let temporary = destination.with_file_name(format!(
".{}.partial-{}-{}",
destination.file_name().unwrap().to_string_lossy(),
std::process::id(),
COPIES.fetch_add(1, Ordering::Relaxed)
));
import.temporary = Some(temporary.clone());
let actual = if clone_file(source, &temporary).is_ok() {
digest(&temporary, algorithm)?
} else {
match algorithm {
"sha1" => copy_hash::<sha1::Sha1>(source, &temporary),
_ => copy_hash::<sha2::Sha256>(source, &temporary),
}
.map_err(|e| format!("{}: {e}", source.display()))?
};
if actual != checksum {
return Err(format!(
"{} is not the pinned file: expected {algorithm} {checksum}, got {actual}",
source.display()
));
}
Ok(import)
}
pub(crate) fn commit(mut self) -> Result<PathBuf, String> {
if let Some(temporary) = &self.temporary {
fs::rename(temporary, &self.destination)
.map_err(|e| format!("{}: {e}", self.destination.display()))?;
}
self.temporary = None;
Ok(self.destination.clone())
}
}
impl Drop for Import {
fn drop(&mut self) {
if let Some(temporary) = &self.temporary {
let _ = fs::remove_file(temporary);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn copies_are_independent_and_checked_before_publication() {
let dir = std::env::temp_dir().join(format!("horizon-file-copy-{}", std::process::id()));
fs::create_dir_all(&dir).unwrap();
let source = dir.join("source");
let target = dir.join("target");
let data = vec![42; 200_000]; // More than one streaming buffer.
fs::write(&source, &data).unwrap();
for algorithm in ["sha1", "sha256"] {
let checksum = digest(&source, algorithm).unwrap();
Import::stage(&source, &target, &checksum, algorithm, false)
.unwrap()
.commit()
.unwrap();
assert_eq!(digest(&target, algorithm).unwrap(), checksum);
fs::write(&target, b"old destination").unwrap();
assert_eq!(fs::read(&source).unwrap(), data);
assert!(Import::stage(&source, &target, "bad hash", algorithm, false).is_err());
assert_eq!(fs::read(&target).unwrap(), b"old destination");
// Explicitly exercise ordinary streaming copies even on a reflink filesystem.
let streamed = if algorithm == "sha1" {
copy_hash::<sha1::Sha1>(&source, &target).unwrap()
} else {
copy_hash::<sha2::Sha256>(&source, &target).unwrap()
};
assert_eq!(streamed, checksum);
assert_eq!(fs::read(&target).unwrap(), data);
}
clone_or_copy(&source, &target).unwrap();
fs::write(&target, b"rewrite").unwrap();
assert_eq!(fs::read(&source).unwrap(), data);
let staged = Import::stage(
&source,
&target,
&digest(&source, "sha1").unwrap(),
"sha1",
false,
)
.unwrap();
assert_eq!(fs::read_dir(&dir).unwrap().count(), 3);
drop(staged);
assert_eq!(fs::read_dir(&dir).unwrap().count(), 2);
fs::remove_dir_all(dir).unwrap();
}
}
+107 -30
View File
@@ -11,12 +11,14 @@
//! run, keyed by the sha256 of their inputs and outputs recorded in the manifest, as before.
pub mod artifacts;
mod files;
mod inventory;
pub mod java;
pub mod pyjson;
mod sounds;
mod survival;
pub use files::clone_or_copy;
pub use sounds::{fonts, languages, packs, panorama, sounds, BUILT_IN_PACKS};
use pyjson::Value;
@@ -31,6 +33,15 @@ pub type Log<'a> = &'a dyn Fn(&str);
/// The manifest's `schema`: the prepared files' layout, which the launcher compares.
pub const SCHEMA: i64 = 7;
/// What this preparer makes. The launcher copies a prepared version's resources to a newly installed
/// version whose preparer has the same number (and the same pinned jars), so a release that changes
/// this crate without changing its output asks nobody to prepare again. Bump it, and append the new
/// digest to output-versions.txt, when the prepared files change: CI prepares with every change to
/// the preparer and fails when the output is not the one output-versions.txt gives this number
/// (`python3 tools/prepare_output.py check`). The launcher reads this line from the version
/// folder's sources: keep it on one line, as it is.
pub const OUTPUT_VERSION: u32 = 1;
/// Digest of this crate's sources (build.rs): the launcher prepares a version in-process when
/// that version ships these same sources, and runs the version's own binary otherwise.
pub const SOURCES_SHA256: &str = env!("HORIZON_PREPARE_SOURCES_SHA256");
@@ -410,16 +421,28 @@ pub fn prepare(options: &Options, log: Log) -> Result<PathBuf, String> {
&artifacts::minecraft_dir(options.minecraft_dir.as_deref()).join("runtime"),
));
let jdk = java::find(lock.java_major, &homes)?;
if options.import && !artifacts::reuse_imported(root, log)? {
artifacts::import(
root,
&lock,
options.minecraft_dir.as_deref(),
options.server_jar.as_deref(),
log,
)?;
}
let found = artifacts::verify(&lock)?;
let reused = if options.import {
artifacts::reuse_verified(root, log)?
} else {
None
};
let (found, client_libraries) = match reused {
Some(checked) => checked,
None => {
if options.import {
artifacts::import(
root,
&lock,
options.minecraft_dir.as_deref(),
options.server_jar.as_deref(),
log,
)?;
}
let found = artifacts::verify(&lock)?;
let libraries = artifacts::client_libraries(&lock, &found.manifest)?;
(found, libraries)
}
};
let local = root.join(".local");
let resources = local.join("horizon-client").join(&lock.version);
mkdir(&resources)?;
@@ -445,7 +468,6 @@ pub fn prepare(options: &Options, log: Log) -> Result<PathBuf, String> {
} else {
Value::Object(Vec::new())
};
let client_libraries = artifacts::client_libraries(&lock, &found.manifest)?;
let run = Run {
root: root.clone(),
local,
@@ -679,25 +701,9 @@ fn steps(
// and every data-pack resource (worldgen, recipes, loot tables, tags), and
// the vanilla pack's icon (`pack.png`) for the pack screen.
(run.log)("Copying the client's assets and data");
let mut count = 0usize;
let mut client = open_zip(&run.client)?;
let version = pyjson::loads(&zip_text(&mut client, "version.json")?)?;
for name in zip_names(&client) {
let wanted = name.starts_with("assets/") || name.starts_with("data/") || name == "pack.png";
if !wanted || name.ends_with('/') {
continue;
}
let relative = member_path(&name, "Invalid resource path")?;
let data = zip_bytes(&mut client, &name)?;
write_if_changed(&root.join(relative), &data)?;
count += 1;
}
// The JAR holds only gray 1x1 panorama placeholders and an empty Unifont
// include, and none of the built-in packs. Import the indexed ones afterwards, including on reruns that just
// extracted the JAR again. They are optional when preparing from
// already-imported JARs.
sounds::optional_assets(repo, minecraft, run.log);
let mut indexed = sounds::Assets::load(repo, minecraft, run.log);
let (version, count) = extract_client_resources(&run.client, root, indexed.as_mut().ok())?;
sounds::optional_imports(indexed.as_mut().map_err(|e| e.as_str()), run.log);
// One file each, exported again when the exporter or the jar it reads changed.
let single = |what: &str,
@@ -931,6 +937,54 @@ fn steps(
)
}
/// Check existing JAR assets by their ZIP size/CRC without inflating every member again.
/// Indexed panorama/font/language replacements have stronger SHA-1 checks and take precedence.
fn extract_client_resources(
jar: &Path,
root: &Path,
mut indexed: Option<&mut sounds::Assets>,
) -> Result<(Value, usize), String> {
let mut client = open_zip(jar)?;
let version = pyjson::loads(&zip_text(&mut client, "version.json")?)?;
let mut count = 0;
for name in zip_names(&client) {
if !(name.starts_with("assets/") || name.starts_with("data/") || name == "pack.png")
|| name.ends_with('/')
{
continue;
}
let relative = member_path(&name, "Invalid resource path")?;
count += 1;
if let Some(assets) = &mut indexed {
if assets.imported(&name)? {
continue;
}
}
let target = root.join(relative);
let member = client.by_name(&name).map_err(|e| e.to_string())?;
let (size, crc) = (member.size(), member.crc32());
drop(member);
if target.is_file() && fs::metadata(&target).map_err(|e| e.to_string())?.len() == size {
let mut file = fs::File::open(&target).map_err(|e| e.to_string())?;
let mut hash = crc32fast::Hasher::new();
let mut buffer = [0; 16 * 1024];
loop {
let n = file.read(&mut buffer).map_err(|e| e.to_string())?;
if n == 0 {
break;
}
hash.update(&buffer[..n]);
}
if hash.finalize() == crc {
continue;
}
}
mkdir(target.parent().unwrap())?;
write(&target, &zip_bytes(&mut client, &name)?)?;
}
Ok((version, count))
}
fn datagen_reports_missing(root: &Path) -> bool {
["items.json", "registries.json", "commands.json"]
.iter()
@@ -1209,6 +1263,29 @@ mod tests {
fs::remove_dir_all(dir).unwrap();
}
#[test]
fn output_versions_end_at_the_output_version() {
let text = include_str!("../output-versions.txt");
let versions: Vec<(u32, &str)> = text
.lines()
.filter(|l| !l.trim().is_empty() && !l.starts_with('#'))
.map(|l| {
let (n, digest) = l.split_once(' ').unwrap();
(n.parse().unwrap(), digest.trim())
})
.collect();
let numbers: Vec<u32> = versions.iter().map(|v| v.0).collect();
assert_eq!(numbers, (1..=OUTPUT_VERSION).collect::<Vec<_>>());
for (i, (_, digest)) in versions.iter().enumerate() {
assert_eq!(digest.len(), 64);
assert!(!versions[..i].iter().any(|(_, d)| d == digest));
}
// The launcher finds the number with this line in a version folder's sources.
let lib = include_str!("lib.rs");
let line = format!("pub const OUTPUT_VERSION: u32 = {OUTPUT_VERSION};");
assert!(lib.lines().any(|l| l == line));
}
#[test]
fn the_source_digest_is_the_one_build_rs_computed() {
assert_eq!(
+294 -68
View File
@@ -5,7 +5,8 @@
use crate::artifacts::{self, Lock};
use crate::pyjson::Value;
use crate::{digest, read_json, Log};
use crate::{digest, Log};
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};
@@ -18,8 +19,9 @@ fn launcher_assets(
lock: &Lock,
manifest: &Value,
minecraft: &Path,
cached_index: &Path,
log: Log,
) -> Result<(PathBuf, PathBuf), String> {
) -> Result<(PathBuf, PathBuf, Vec<u8>), String> {
let assets = minecraft.join("assets");
let info = manifest
.get("assetIndex")
@@ -33,7 +35,10 @@ fn launcher_assets(
if artifacts::safe_relative(id).is_none_or(|p| p.components().count() != 1) {
return Err(format!("Unsafe asset index id {id:?}"));
}
let index = assets.join("indexes").join(format!("{id}.json"));
let mut index = assets.join("indexes").join(format!("{id}.json"));
if !index.is_file() && cached_index.is_file() {
index = cached_index.to_path_buf();
}
if !index.is_file() {
let version = manifest.get("id").and_then(Value::as_str).unwrap_or("");
return Err(format!(
@@ -42,7 +47,8 @@ fn launcher_assets(
artifacts::launcher_hint(version)
));
}
let checksum = digest(&index, "sha1")?;
let bytes = fs::read(&index).map_err(|e| format!("{}: {e}", index.display()))?;
let checksum = crate::hex(&<sha1::Sha1 as sha1::Digest>::digest(&bytes));
if checksum != sha1 && !lock.asset_index_accepted(&checksum) {
log(&format!(
"{} is an asset index Horizon does not know yet (sha1 {checksum}); Mojang may have \
@@ -50,7 +56,7 @@ fn launcher_assets(
index.display()
));
}
Ok((assets, index))
Ok((assets, index, bytes))
}
/// Where the launcher's asset store keeps the object with this SHA-1.
@@ -61,24 +67,6 @@ fn object_path(assets: &Path, checksum: &str) -> Result<PathBuf, String> {
Ok(assets.join("objects").join(&checksum[..2]).join(checksum))
}
/// One verified object from the launcher's asset store (tools/lab.py `launcher_object`).
fn launcher_object(assets: &Path, checksum: &str) -> Result<PathBuf, String> {
let path = object_path(assets, checksum)?;
if !path.is_file() {
return Err(format!(
"Missing asset object {}. Play the pinned version once in your launcher.",
path.display()
));
}
if digest(&path, "sha1")? != checksum {
return Err(format!(
"Corrupt asset object {}; let the launcher repair it",
path.display()
));
}
Ok(path)
}
/// Whether the game plays this asset: sounds.json and everything under sounds/.
fn is_sound(name: &str) -> bool {
name == "minecraft/sounds.json" || name.starts_with("minecraft/sounds/")
@@ -127,26 +115,56 @@ fn is_language(name: &str) -> bool {
}
/// Full preparation can run from cached JARs without an official launcher install.
pub(crate) fn optional_assets(root: &Path, minecraft: Option<&Path>, log: Log) {
if let Err(error) = panorama(root, minecraft, log) {
log(&format!(
"Title panorama not imported: {error}; run horizon-prepare panorama later"
));
}
if let Err(error) = fonts(root, minecraft, log) {
log(&format!(
"Unifont glyphs not imported: {error}; run horizon-prepare fonts later"
));
}
if let Err(error) = packs(root, minecraft, log) {
log(&format!(
"Programmer Art and High Contrast not imported: {error}; run horizon-prepare packs later"
));
}
if let Err(error) = languages(root, minecraft, log) {
log(&format!(
"Languages not imported: {error}; run horizon-prepare languages later"
));
#[cfg(test)]
fn optional_assets(root: &Path, minecraft: Option<&Path>, log: Log) {
optional_imports(
Assets::load(root, minecraft, log)
.as_mut()
.map_err(|e| e.as_str()),
log,
);
}
pub(crate) fn optional_imports(mut assets: Result<&mut Assets, &str>, log: Log) {
for (wanted, label, required, failure, command) in [
(
is_panorama as fn(&str) -> bool,
"Panorama",
PANORAMA.as_slice(),
"Title panorama",
"panorama",
),
(
is_font,
"Font",
UNIFONT.as_slice(),
"Unifont glyphs",
"fonts",
),
(
is_built_in_pack,
"Built-in pack",
BUILT_IN_PACKS.as_slice(),
"Programmer Art and High Contrast",
"packs",
),
(
is_language,
"Language",
LANGUAGES.as_slice(),
"Languages",
"languages",
),
] {
let result = match &mut assets {
Ok(assets) => assets.copy(wanted, label, required, log).map(|_| ()),
Err(error) => Err(error.to_string()),
};
if let Err(error) = result {
log(&format!(
"{failure} not imported: {error}; run horizon-prepare {command} later"
));
}
}
}
@@ -207,29 +225,98 @@ fn copy_assets(
label: &str,
required: &[&str],
) -> Result<PathBuf, String> {
let lock = Lock::read(root)?;
let minecraft = artifacts::minecraft_dir(minecraft);
let manifest = artifacts::import_manifest(&lock, &minecraft)?;
let (store, index) = launcher_assets(&lock, &manifest, &minecraft, log)?;
let id = manifest
.get("id")
.and_then(Value::as_str)
.ok_or("The version JSON has no id")?;
let target = root.join(".local/horizon-client").join(id);
fs::create_dir_all(&target).map_err(|e| format!("{}: {e}", target.display()))?;
fs::copy(&index, target.join("asset-index.json"))
.map_err(|e| format!("{}: {e}", target.join("asset-index.json").display()))?;
let objects = read_json(&index)?;
let objects = objects
.get("objects")
.and_then(Value::as_object)
.ok_or_else(|| format!("{} has no objects", index.display()))?;
Assets::load(root, minecraft, log)?.copy(wanted_asset, label, required, log)
}
/// A preparation's index and verified destinations. Never persisted: a later invocation
/// checks the bytes again, including when repairing a corrupt prepared file.
pub(crate) struct Assets {
store: PathBuf,
target: PathBuf,
objects: Vec<(String, Value)>,
verified: HashSet<PathBuf>,
}
impl Assets {
pub(crate) fn load(root: &Path, minecraft: Option<&Path>, log: Log) -> Result<Self, String> {
let lock = Lock::read(root)?;
let minecraft = artifacts::minecraft_dir(minecraft);
let manifest = artifacts::import_manifest(&lock, &minecraft)?;
let target = root.join(".local/horizon-client").join(&lock.version);
let (store, index, bytes) = launcher_assets(
&lock,
&manifest,
&minecraft,
&target.join("asset-index.json"),
log,
)?;
let text = std::str::from_utf8(&bytes).map_err(|e| e.to_string())?;
let objects = crate::pyjson::loads(text)?;
let objects = objects
.get("objects")
.and_then(Value::as_object)
.ok_or_else(|| format!("{} has no objects", index.display()))?
.to_vec();
fs::create_dir_all(&target).map_err(|e| e.to_string())?;
crate::write_if_changed(&target.join("asset-index.json"), &bytes)?;
Ok(Self {
store,
target,
objects,
verified: HashSet::new(),
})
}
/// Preserve indexed replacements instead of overwriting them with JAR placeholders.
pub(crate) fn imported(&mut self, name: &str) -> Result<bool, String> {
let Some(name) = name.strip_prefix("assets/") else {
return Ok(false);
};
if !(is_panorama(name) || is_font(name) || is_language(name)) {
return Ok(false);
}
let Some((_, info)) = self.objects.iter().rev().find(|(n, _)| n == name) else {
return Ok(false);
};
let Some(hash) = info.get("hash").and_then(Value::as_str) else {
return Ok(false);
};
let destination = self.target.join("assets").join(name);
if destination.is_file() && digest(&destination, "sha1")? == hash {
self.verified.insert(destination);
return Ok(true);
}
Ok(false)
}
fn copy(
&mut self,
wanted_asset: fn(&str) -> bool,
label: &str,
required: &[&str],
log: Log,
) -> Result<PathBuf, String> {
copy_indexed(self, wanted_asset, label, required, log)
}
}
fn copy_indexed(
assets: &mut Assets,
wanted_asset: fn(&str) -> bool,
label: &str,
required: &[&str],
log: Log,
) -> Result<PathBuf, String> {
let objects = &assets.objects;
// A repeated name keeps its first place and its last value, as a Python dict does.
let mut wanted: Vec<(&str, &Value)> = Vec::new();
let mut positions = HashMap::new();
for (name, info) in objects.iter().filter(|(name, _)| wanted_asset(name)) {
match wanted.iter_mut().find(|(n, _)| n == name) {
Some(entry) => entry.1 = info,
None => wanted.push((name, info)),
if let Some(&position) = positions.get(name.as_str()) {
wanted[position] = (name.as_str(), info);
} else {
positions.insert(name.as_str(), wanted.len());
wanted.push((name, info));
}
}
for name in required {
@@ -251,21 +338,45 @@ fn copy_assets(
.ok_or_else(|| format!("{name} has no size in the asset index"))?;
let relative = artifacts::safe_relative(name)
.ok_or_else(|| format!("Unsafe asset name {name:?} in the asset index"))?;
let source = launcher_object(&store, hash)?;
copies.push((source, target.join("assets").join(relative), hash));
let source = object_path(&assets.store, hash)?;
let destination = assets.target.join("assets").join(relative);
let import = crate::files::Import::stage(
&source,
&destination,
hash,
"sha1",
assets.verified.contains(&destination),
)
.map_err(|error| {
if !source.is_file() {
format!(
"Missing asset object {}. Play the pinned version once in your launcher.",
source.display()
)
} else if error.contains(" is not the pinned file:") {
format!(
"Corrupt asset object {}; let the launcher repair it: {error}",
source.display()
)
} else {
error
}
})?;
copies.push((import, destination));
total += size;
}
// Check the entire set before replacing any placeholders: a missing late face
// must not leave a mixture of real textures and gray faces in either entry point.
for (source, destination, hash) in copies {
artifacts::import_copy(&source, &destination, hash, "sha1", "")?;
for (import, destination) in copies {
import.commit()?;
assets.verified.insert(destination);
}
log(&format!(
"{label} assets ready: {} files, {:.1} MB",
wanted.len(),
total as f64 / 1e6
));
Ok(target)
Ok(assets.target.clone())
}
#[cfg(test)]
@@ -680,6 +791,7 @@ mod tests {
"minecraft/sounds.json is missing from the asset index"
);
fs::remove_file(&index).unwrap();
fs::remove_file(f.root.join(".local/horizon-client/9.9/asset-index.json")).unwrap();
let error = f.run().unwrap_err();
assert!(
error.starts_with("Missing ") && error.contains("play 9.9 once"),
@@ -687,6 +799,120 @@ mod tests {
);
}
#[test]
fn late_corruption_discards_all_staged_replacements() {
let f = Fixture::new("late-corruption");
let target = f.root.join(".local/horizon-client/9.9/assets");
for name in PANORAMA {
let path = target.join(name);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, b"placeholder").unwrap();
}
fs::write(f.object(FIRST_FACE + 6), b"bad overlay").unwrap();
assert!(panorama(&f.root, Some(&f.minecraft), &|_| {}).is_err());
for name in PANORAMA {
assert_eq!(fs::read(target.join(name)).unwrap(), b"placeholder");
}
assert_eq!(
fs::read_dir(target.join("minecraft/textures/gui/title/background"))
.unwrap()
.count(),
7
);
}
#[test]
fn one_index_snapshot_serves_every_import_and_reruns_repair_corruption() {
let f = Fixture::with_extras("snapshot", &PANORAMA, &[("pack.mcmeta", b"{}")]);
let mut assets = Assets::load(&f.root, Some(&f.minecraft), &|_| {}).unwrap();
fs::remove_file(f.minecraft.join("assets/indexes/7.json")).unwrap();
let lines = std::cell::RefCell::new(Vec::new());
optional_imports(Ok(&mut assets), &|line| {
lines.borrow_mut().push(line.to_string())
});
assert_eq!(lines.borrow().len(), 4);
assert!(lines
.borrow()
.iter()
.all(|line| line.contains("assets ready")));
assets
.copy(is_sound, "Sound", &["minecraft/sounds.json"], &|_| {})
.unwrap();
let sound = assets
.target
.join("assets/minecraft/sounds/ambient/cave1.ogg");
let time = fs::metadata(&sound).unwrap().modified().unwrap();
fs::remove_file(f.object(1)).unwrap();
f.run().unwrap(); // A verified destination works even without the source object/index.
assert_eq!(fs::metadata(&sound).unwrap().modified().unwrap(), time);
fs::write(&sound, b"corrupt").unwrap();
assert!(f.run().is_err()); // Verification never carries over to another invocation.
fs::write(f.object(1), &f.objects[1].1).unwrap();
f.run().unwrap();
assert_eq!(fs::read(sound).unwrap(), f.objects[1].1);
}
#[test]
fn jar_reruns_preserve_indexed_assets_and_repair_missing_or_corrupt_members() {
use std::io::Write;
let f = Fixture::with_extras(
"extract",
&PANORAMA,
&[
("pack.mcmeta", b"{}"),
("minecraft/lang/en_us.json", b"{\"indexed\":true}"),
],
);
optional_assets(&f.root, Some(&f.minecraft), &|_| {});
let target = f.root.join(".local/horizon-client/9.9");
let jar = f.root.join("client.jar");
let mut zip = zip::ZipWriter::new(fs::File::create(&jar).unwrap());
for (name, data) in [
("version.json", "{\"world_version\": 1}"),
(
"assets/minecraft/textures/gui/title/background/panorama_0.png",
"gray",
),
("assets/minecraft/font/include/unifont.json", "{}"),
("assets/minecraft/lang/en_us.json", "{}"),
("data/minecraft/test.json", "data"),
("pack.png", "icon"),
] {
zip.start_file(name, zip::write::SimpleFileOptions::default())
.unwrap();
zip.write_all(data.as_bytes()).unwrap();
}
zip.finish().unwrap();
let face = target.join("assets").join(PANORAMA[0]);
let time = fs::metadata(&face).unwrap().modified().unwrap();
// Preparing from cached JARs must also preserve full imports without the launcher.
fs::remove_dir_all(f.minecraft.join("assets")).unwrap();
for _ in 0..2 {
let mut assets = Assets::load(&f.root, Some(&f.minecraft), &|_| {}).unwrap();
let (_, count) =
crate::extract_client_resources(&jar, &target, Some(&mut assets)).unwrap();
assert_eq!(count, 5);
optional_imports(Ok(&mut assets), &|_| {});
assert_eq!(fs::read(&face).unwrap(), PANORAMA[0].as_bytes());
assert_eq!(fs::metadata(&face).unwrap().modified().unwrap(), time);
assert_eq!(
fs::read(target.join("assets").join(UNIFONT[0])).unwrap(),
f.objects[4].1
);
assert_eq!(
fs::read(target.join("assets/minecraft/lang/en_us.json")).unwrap(),
b"{\"indexed\":true}"
);
assert_eq!(
fs::read(target.join("data/minecraft/test.json")).unwrap(),
b"data"
);
assert_eq!(fs::read(target.join("pack.png")).unwrap(), b"icon");
fs::write(target.join("data/minecraft/test.json"), b"oops").unwrap();
fs::remove_file(target.join("pack.png")).unwrap();
}
}
#[test]
fn object_paths_come_from_real_hashes() {
let store = Path::new("assets");
+159
View File
@@ -0,0 +1,159 @@
//! The biome parameters and constants of custom uniform expressions (spec 3.4): `BIOME_<NAME>`,
//! `CAT_<NAME>` and `PPT_<NAME>`.
//!
//! Decisions (impl-plan.md, OQ-11):
//! - `biome` is the biome's index in the game's biome registry, which lists vanilla's biomes
//! sorted by identifier; `BIOME_<NAME>` is the index of `minecraft:<name>`. Names from before
//! the 1.18 renames map to the biome that replaced them. A `BIOME_` name that matches no biome
//! reads -1, so it never equals the player's biome.
//! - `biome_category` comes from the biome's name (categories left the game in 1.19).
use std::collections::HashMap;
/// `CAT_*`, in the order of their values.
pub const CATEGORIES: [&str; 17] = [
"NONE",
"TAIGA",
"EXTREME_HILLS",
"JUNGLE",
"MESA",
"PLAINS",
"SAVANNA",
"ICY",
"THE_END",
"BEACH",
"FOREST",
"OCEAN",
"DESERT",
"RIVER",
"SWAMP",
"MUSHROOM",
"NETHER",
];
/// Old biome names and the biomes that replaced them.
const LEGACY: &[(&str, &str)] = &[
("EXTREME_HILLS", "windswept_hills"),
("MOUNTAINS", "windswept_hills"),
("GRAVELLY_MOUNTAINS", "windswept_gravelly_hills"),
("WOODED_MOUNTAINS", "windswept_forest"),
("SHATTERED_SAVANNA", "windswept_savanna"),
("GIANT_TREE_TAIGA", "old_growth_pine_taiga"),
("GIANT_SPRUCE_TAIGA", "old_growth_spruce_taiga"),
("TALL_BIRCH_FOREST", "old_growth_birch_forest"),
("SNOWY_TUNDRA", "snowy_plains"),
("JUNGLE_EDGE", "sparse_jungle"),
("WOODED_BADLANDS_PLATEAU", "wooded_badlands"),
("STONE_SHORE", "stony_shore"),
("NETHER", "nether_wastes"),
];
/// The constants every pack sees: `CAT_*` and `PPT_*`, plus `BIOME_*` for `biomes` (the
/// registry's identifiers, in id order).
pub fn constants(biomes: &[&str]) -> HashMap<String, f64> {
let mut out: HashMap<String, f64> = CATEGORIES
.iter()
.enumerate()
.map(|(i, c)| (format!("CAT_{c}"), i as f64))
.collect();
for (i, p) in ["NONE", "RAIN", "SNOW"].iter().enumerate() {
out.insert(format!("PPT_{p}"), i as f64);
}
let index = |name: &str| {
biomes
.iter()
.position(|b| b.strip_prefix("minecraft:") == Some(name))
};
for (i, b) in biomes.iter().enumerate() {
if let Some(name) = b.strip_prefix("minecraft:") {
out.insert(format!("BIOME_{}", name.to_ascii_uppercase()), i as f64);
}
}
for (old, new) in LEGACY {
if let Some(i) = index(new) {
out.entry(format!("BIOME_{old}")).or_insert(i as f64);
}
}
out
}
/// `biome_category` of the biome `minecraft:<name>`.
pub fn category(name: &str) -> i32 {
let name = name.strip_prefix("minecraft:").unwrap_or(name);
let has = |words: &[&str]| words.iter().any(|w| name.contains(w));
let cat = if name == "the_void" {
"NONE"
} else if name == "the_end" || name.starts_with("end_") || name == "small_end_islands" {
"THE_END"
} else if has(&["nether", "crimson", "warped", "basalt", "soul_sand"]) {
"NETHER"
} else if has(&["ocean"]) {
"OCEAN"
} else if has(&["river"]) {
"RIVER"
} else if has(&["beach", "shore"]) {
"BEACH"
} else if has(&["swamp"]) {
"SWAMP"
} else if has(&["mushroom"]) {
"MUSHROOM"
} else if has(&["desert"]) {
"DESERT"
} else if has(&["badlands"]) {
"MESA"
} else if has(&["savanna"]) {
"SAVANNA"
} else if has(&["jungle"]) {
"JUNGLE"
} else if has(&["taiga"]) {
"TAIGA"
} else if has(&["windswept", "peaks", "slopes", "meadow", "grove"]) && name != "cherry_grove" {
"EXTREME_HILLS"
} else if has(&["frozen", "snowy", "ice"]) {
"ICY"
} else if has(&["forest", "garden", "cherry"]) {
"FOREST"
} else if has(&["plains"]) {
"PLAINS"
} else {
"NONE"
};
CATEGORIES.iter().position(|c| *c == cat).unwrap_or(0) as i32
}
/// `biome_precipitation`: none where the biome has none, else rain from temperature 0.15 up,
/// snow below it (spec 3.4).
pub fn precipitation(has_precipitation: bool, temperature: f32) -> i32 {
match (has_precipitation, temperature >= 0.15) {
(false, _) => 0,
(true, true) => 1,
(true, false) => 2,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn constants_categories_and_precipitation() {
let c = constants(&[
"minecraft:badlands",
"minecraft:pale_garden",
"minecraft:windswept_hills",
]);
assert_eq!(c["BIOME_PALE_GARDEN"], 1.0);
assert_eq!(c["BIOME_EXTREME_HILLS"], 2.0);
assert_eq!(c["CAT_NETHER"], 16.0);
assert_eq!(c["PPT_SNOW"], 2.0);
assert!(!c.contains_key("BIOME_SULFUR_CAVES"));
assert_eq!(category("minecraft:pale_garden"), 10);
assert_eq!(category("minecraft:deep_frozen_ocean"), 11);
assert_eq!(category("minecraft:snowy_taiga"), 1);
assert_eq!(category("minecraft:crimson_forest"), 16);
assert_eq!(category("minecraft:cherry_grove"), 10);
assert_eq!(precipitation(true, 0.0), 2);
assert_eq!(precipitation(true, 0.8), 1);
assert_eq!(precipitation(false, 2.0), 0);
}
}
+151
View File
@@ -0,0 +1,151 @@
//! `block.properties` (spec 4.1): `block.<id>=<block> <block> ...`, each block
//! `[namespace:]name` with optional state filters `:<property>=<v1>,<v2>`. Values in one filter
//! are alternatives; separate filters must all match. Names the game does not know are simply
//! never asked for. Block tags (`%tag`, tier E) are not read yet.
use crate::Properties;
#[derive(Clone, Debug, PartialEq)]
struct Rule {
id: i32,
/// `minecraft:stone`.
name: String,
filters: Vec<(String, Vec<String>)>,
}
/// The ids `block.properties` gives block states.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct BlockIds {
rules: Vec<Rule>,
/// Lines skipped and why.
pub notes: Vec<String>,
}
impl BlockIds {
pub fn parse(properties: &Properties) -> Self {
Self::parse_kind(properties, "block")
}
/// The same for `<kind>.<id>=` lines: `entity` in `entity.properties`, `item` in
/// `item.properties` (4.1); names there carry no state filters.
pub fn parse_kind(properties: &Properties, kind: &str) -> Self {
let mut out = Self::default();
let prefix = format!("{kind}.");
for e in &properties.entries {
let Some(id) = e.key.strip_prefix(prefix.as_str()) else {
continue;
};
let Ok(id) = id.parse::<i32>() else {
// `layer.*` (4.2) and other keys are not ids.
continue;
};
for word in e.value.split_whitespace() {
if word.starts_with('%') {
out.notes.push(format!(
"line {}: block tags are not read yet: {word}",
e.line
));
continue;
}
let (names, filters): (Vec<&str>, Vec<&str>) =
word.split(':').partition(|p| !p.contains('='));
let name = match names[..] {
[name] => format!("minecraft:{name}"),
[namespace, name] => format!("{namespace}:{name}"),
_ => {
out.notes
.push(format!("line {}: not a block name: {word}", e.line));
continue;
}
};
let filters = filters
.iter()
.filter_map(|f| f.split_once('='))
.map(|(k, v)| (k.to_owned(), v.split(',').map(str::to_owned).collect()))
.collect();
out.rules.push(Rule { id, name, filters });
}
}
out
}
/// The id of block `name` (`minecraft:stone`) in the state with `properties`. The rule with
/// the most filters that all match wins, then the first in the file (one name belongs to
/// one id; a filtered entry is the more specific one).
pub fn id<'a>(&self, name: &str, properties: impl Fn(&str) -> Option<&'a str>) -> Option<i32> {
self.rules
.iter()
.filter(|r| r.name == name)
.filter(|r| {
r.filters
.iter()
.all(|(k, vs)| properties(k).is_some_and(|v| vs.iter().any(|x| x == v)))
})
.min_by_key(|r| std::cmp::Reverse(r.filters.len()))
.map(|r| r.id)
}
/// Every name without state filters and its id (the first line naming it wins): what
/// `entity.properties` and `item.properties` hold.
pub fn plain(&self) -> std::collections::HashMap<String, i32> {
let mut out = std::collections::HashMap::new();
for r in self.rules.iter().filter(|r| r.filters.is_empty()) {
out.entry(r.name.clone()).or_insert(r.id);
}
out
}
pub fn is_empty(&self) -> bool {
self.rules.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Macros;
#[test]
fn names_states_and_precedence() {
let text = "block.10=stone minecraft:dirt mod:thing \\\n grass_block:snowy=false\n\
block.11=grass_block\nblock.12=oak_leaves:persistent=true:distance=1,2 %leaves\n\
layer.translucent=glass\n";
let ids = BlockIds::parse(&Properties::parse(text, &Macros::standard()));
let none = |_: &str| None;
assert_eq!(ids.id("minecraft:stone", none), Some(10));
assert_eq!(ids.id("mod:thing", none), Some(10));
assert_eq!(ids.id("minecraft:glass", none), None);
let snowy = |s: &'static str| move |k: &str| (k == "snowy").then_some(s);
// The filtered entry is the more specific one.
assert_eq!(ids.id("minecraft:grass_block", snowy("false")), Some(10));
assert_eq!(ids.id("minecraft:grass_block", snowy("true")), Some(11));
let leaves = |p: &'static str, d: &'static str| {
move |k: &str| match k {
"persistent" => Some(p),
"distance" => Some(d),
_ => None,
}
};
assert_eq!(
ids.id("minecraft:oak_leaves", leaves("true", "2")),
Some(12)
);
assert_eq!(ids.id("minecraft:oak_leaves", leaves("true", "3")), None);
assert_eq!(ids.id("minecraft:oak_leaves", leaves("false", "1")), None);
assert_eq!(ids.notes.len(), 1, "{:?}", ids.notes);
}
#[test]
fn entity_lines_by_name() {
let text = "entity.10=zombie minecraft:husk\nentity.11=zombie mod:thing\nblock.5=stone\n";
let ids = BlockIds::parse_kind(&Properties::parse(text, &Macros::standard()), "entity");
let plain = ids.plain();
assert_eq!(
plain["minecraft:zombie"], 10,
"the first line naming it wins"
);
assert_eq!(plain["minecraft:husk"], 10);
assert_eq!(plain["mod:thing"], 11);
assert!(!plain.contains_key("minecraft:stone"));
}
}
+611
View File
@@ -0,0 +1,611 @@
//! Custom uniforms and variables of `shaders.properties` (spec 3.4): `uniform.<type>.<name>` and
//! `variable.<type>.<name>` expressions, parsed once and evaluated on the CPU every frame in
//! file order. Uniforms reach every program; variables only later expressions.
//!
//! Decisions (impl-plan.md):
//! - OQ-25: numbers are doubles; booleans are 1 and 0; `%` is C's remainder (`fmod` floors);
//! comparisons and logic read the first component; a value takes its declared type at the
//! end (`int` truncates toward zero, `bool` is "not zero"). Unknown names read 0 and are
//! reported once.
//! - OQ-26: `smooth` runs per frame with the frame time; its fade times are seconds (1 by
//! default) to cover half of the remaining distance, `fadeIn` while rising, `fadeOut` while
//! falling. Each call keeps its own state, so the optional id is not needed.
use std::collections::{HashMap, HashSet};
use crate::Properties;
/// A value: up to 16 components (matrices only as names, for `m.row.column`).
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Val {
pub v: [f64; 16],
/// Components: 1 for a scalar.
pub n: usize,
}
impl Val {
pub fn scalar(x: f64) -> Self {
let mut v = [0.0; 16];
v[0] = x;
Self { v, n: 1 }
}
pub fn vector(c: &[f64]) -> Self {
let mut v = [0.0; 16];
v[..c.len()].copy_from_slice(c);
Self { v, n: c.len() }
}
fn x(&self) -> f64 {
self.v[0]
}
fn truth(&self) -> bool {
self.v[0] != 0.0
}
/// `f` per component; a scalar operand stretches to the other's size.
fn zip(self, o: Val, f: impl Fn(f64, f64) -> f64) -> Val {
let n = self.n.max(o.n).min(4);
let at = |a: &Val, i: usize| if a.n == 1 { a.v[0] } else { a.v[i] };
let mut out = Val::scalar(0.0);
out.n = n;
for i in 0..n {
out.v[i] = f(at(&self, i), at(&o, i));
}
out
}
fn map(self, f: impl Fn(f64) -> f64) -> Val {
let mut out = self;
for i in 0..self.n.min(4) {
out.v[i] = f(self.v[i]);
}
out
}
}
#[derive(Clone, Debug)]
enum Expr {
Num(f64),
/// A name and its component (`x`, `2`, `2.1` as row 2, column 1).
Name(String, Option<(usize, Option<usize>)>),
Call(String, Vec<Expr>, usize),
Neg(Box<Expr>),
Not(Box<Expr>),
Bin(&'static str, Box<Expr>, Box<Expr>),
}
#[derive(Debug)]
struct Item {
name: String,
ty: String,
uniform: bool,
expr: Expr,
}
/// The pack's custom uniforms and variables, with the state `smooth` and `random` keep.
#[derive(Debug, Default)]
pub struct Custom {
items: Vec<Item>,
/// `smooth` state per call site.
smooth: HashMap<usize, f64>,
seed: u64,
unknown: HashSet<String>,
/// `BIOME_*`, `CAT_*` and `PPT_*` (crate::biomes).
constants: HashMap<String, f64>,
/// Expressions that did not parse, and unknown names, as `key: message`.
pub notes: Vec<String>,
calls: usize,
}
impl Custom {
/// Reads every `uniform.*` and `variable.*` key of `properties`, in file order.
pub fn parse(properties: &Properties) -> Self {
let mut out = Custom {
seed: 0x9e37_79b9_7f4a_7c15,
constants: crate::biomes::constants(&[]),
..Default::default()
};
for e in &properties.entries {
let (uniform, rest) = match e.key.split_once('.') {
Some(("uniform", r)) => (true, r),
Some(("variable", r)) => (false, r),
_ => continue,
};
let Some((ty, name)) = rest.split_once('.') else {
continue;
};
if !matches!(ty, "float" | "int" | "bool" | "vec2" | "vec3" | "vec4") {
out.notes
.push(format!("{}: type `{ty}` is not supported", e.key));
continue;
}
match Parser::new(&e.value, &mut out.calls).expr_all() {
Ok(expr) => out.items.push(Item {
name: name.to_owned(),
ty: ty.to_owned(),
uniform,
expr,
}),
Err(m) => out.notes.push(format!("{}: {m}", e.key)),
}
}
out
}
/// Names the game's biomes (registry identifiers, in id order) for `BIOME_*`.
pub fn set_biomes(&mut self, biomes: &[&str]) {
self.constants = crate::biomes::constants(biomes);
}
/// The uniforms' names and declared types, in file order.
pub fn uniforms(&self) -> impl Iterator<Item = (&str, &str)> {
self.items
.iter()
.filter(|i| i.uniform)
.map(|i| (i.name.as_str(), i.ty.as_str()))
}
/// Evaluates everything for a frame `dt` seconds after the last; `builtin` gives the
/// values of the game's uniforms and constants (`BIOME_*`...). Returns the uniforms'
/// values, in the order of `uniforms`.
pub fn eval(&mut self, dt: f64, builtin: &dyn Fn(&str) -> Option<Val>) -> Vec<Val> {
let mut vars: HashMap<String, Val> = HashMap::new();
let mut out = Vec::new();
let items = std::mem::take(&mut self.items);
for item in &items {
let v = self.value(&item.expr, dt, &vars, builtin);
let v = match item.ty.as_str() {
"int" => Val::scalar(v.x().trunc()),
"bool" => Val::scalar(f64::from(u8::from(v.truth()))),
"float" => Val::scalar(v.x()),
t => {
let n = (t.as_bytes()[3] - b'0') as usize;
let mut v = v;
if v.n == 1 {
let x = v.v[0];
v.v[1..n].fill(x);
}
v.n = n;
v
}
};
vars.insert(item.name.clone(), v);
if item.uniform {
out.push(v);
}
}
self.items = items;
out
}
fn random(&mut self) -> f64 {
self.seed ^= self.seed << 13;
self.seed ^= self.seed >> 7;
self.seed ^= self.seed << 17;
(self.seed >> 11) as f64 / (1u64 << 53) as f64
}
fn value(
&mut self,
e: &Expr,
dt: f64,
vars: &HashMap<String, Val>,
builtin: &dyn Fn(&str) -> Option<Val>,
) -> Val {
let ev = |e: &Expr, s: &mut Self| s.value(e, dt, vars, builtin);
match e {
Expr::Num(x) => Val::scalar(*x),
Expr::Name(name, component) => {
let v = match name.as_str() {
"pi" => Some(Val::scalar(std::f64::consts::PI)),
"true" => Some(Val::scalar(1.0)),
"false" => Some(Val::scalar(0.0)),
n => vars
.get(n)
.copied()
.or_else(|| builtin(n))
.or_else(|| self.constants.get(n).map(|&x| Val::scalar(x))),
};
if v.is_none() && name.starts_with("BIOME_") {
if self.unknown.insert(name.clone()) {
self.notes
.push(format!("`{name}` names no biome; it reads -1"));
}
return Val::scalar(-1.0);
}
let Some(v) = v else {
if self.unknown.insert(name.clone()) {
self.notes.push(format!("`{name}` is unknown; it reads 0"));
}
return Val::scalar(0.0);
};
match component {
None => v,
// GLSL's m[column][row] is stored column after column.
Some((row, Some(column))) => Val::scalar(v.v[column * 4 + row]),
Some((i, None)) => Val::scalar(v.v[*i]),
}
}
Expr::Neg(a) => ev(a, self).map(|x| -x),
Expr::Not(a) => Val::scalar(f64::from(u8::from(!ev(a, self).truth()))),
Expr::Bin(op, a, b) => {
let (a, b) = (ev(a, self), ev(b, self));
let bool_ = |c: bool| Val::scalar(f64::from(u8::from(c)));
match *op {
"+" => a.zip(b, |x, y| x + y),
"-" => a.zip(b, |x, y| x - y),
"*" => a.zip(b, |x, y| x * y),
"/" => a.zip(b, |x, y| x / y),
"%" => a.zip(b, |x, y| x % y),
"<" => bool_(a.x() < b.x()),
"<=" => bool_(a.x() <= b.x()),
">" => bool_(a.x() > b.x()),
">=" => bool_(a.x() >= b.x()),
"==" => bool_(a.x() == b.x()),
"!=" => bool_(a.x() != b.x()),
"&&" => bool_(a.truth() && b.truth()),
_ => bool_(a.truth() || b.truth()),
}
}
Expr::Call(f, args, site) => {
let a: Vec<Val> = args.iter().map(|e| ev(e, self)).collect();
let arg = |i: usize| a.get(i).copied().unwrap_or(Val::scalar(0.0));
let x = arg(0).x();
let one = |g: fn(f64) -> f64| arg(0).map(g);
match f.as_str() {
"sin" => one(f64::sin),
"cos" => one(f64::cos),
"tan" => one(f64::tan),
"asin" => one(f64::asin),
"acos" => one(f64::acos),
"atan" if a.len() == 2 => Val::scalar(x.atan2(arg(1).x())),
"atan" => one(f64::atan),
"atan2" => Val::scalar(x.atan2(arg(1).x())),
"torad" | "radians" => one(f64::to_radians),
"todeg" | "degrees" => one(f64::to_degrees),
"min" => a
.iter()
.copied()
.reduce(|p, q| p.zip(q, f64::min))
.unwrap_or(arg(0)),
"max" => a
.iter()
.copied()
.reduce(|p, q| p.zip(q, f64::max))
.unwrap_or(arg(0)),
"clamp" => arg(0).zip(arg(1), f64::max).zip(arg(2), f64::min),
"abs" => one(f64::abs),
"floor" => one(f64::floor),
"ceil" => one(f64::ceil),
"exp" => one(f64::exp),
"exp2" => one(f64::exp2),
"exp10" => one(|v| 10f64.powf(v)),
"frac" => one(|v| v - v.floor()),
"log" if a.len() == 2 => Val::scalar(arg(1).x().log(x)),
"log" => one(f64::ln),
"log2" => one(f64::log2),
"log10" => one(f64::log10),
"sqrt" => one(f64::sqrt),
"round" => one(f64::round),
"signum" | "sign" => one(|v| if v == 0.0 { 0.0 } else { v.signum() }),
"pow" => arg(0).zip(arg(1), f64::powf),
"fmod" => arg(0).zip(arg(1), |p, q| p - q * (p / q).floor()),
"lerp" => arg(1).zip(arg(2), |p, q| p + (q - p) * x),
"mix" => arg(0).zip(arg(1), |p, q| p + (q - p) * arg(2).x()),
"edge" => Val::scalar(if arg(1).x() < x { 0.0 } else { 1.0 }),
"random" if a.len() == 2 => Val::scalar(x + (arg(1).x() - x) * self.random()),
"random" => Val::scalar(self.random()),
"randomInt" if a.len() == 2 => {
Val::scalar((x + (arg(1).x() - x) * self.random()).floor())
}
"randomInt" => Val::scalar((self.random() * f64::from(i32::MAX)).floor()),
"if" | "ifb" => {
let mut i = 0;
while i + 1 < a.len() {
if a[i].truth() {
return a[i + 1];
}
i += 2;
}
a.last().copied().unwrap_or(Val::scalar(0.0))
}
"smooth" => {
let (val, fade_in, fade_out) = match a.len() {
4 => (arg(1).x(), arg(2).x(), arg(3).x()),
3 => (x, arg(1).x(), arg(2).x()),
2 => (x, arg(1).x(), arg(1).x()),
_ => (x, 1.0, 1.0),
};
let state = self.smooth.entry(*site).or_insert(val);
let fade = if val > *state { fade_in } else { fade_out };
if fade > 0.0 {
*state = val + (*state - val) * 0.5f64.powf(dt / fade);
} else {
*state = val;
}
Val::scalar(*state)
}
"print" => arg(2),
"between" => {
Val::scalar(f64::from(u8::from(x >= arg(1).x() && x <= arg(2).x())))
}
"equals" => {
Val::scalar(f64::from(u8::from((x - arg(1).x()).abs() <= arg(2).x())))
}
"in" => Val::scalar(f64::from(u8::from(a[1..].iter().any(|v| v.x() == x)))),
"vec2" | "vec3" | "vec4" => {
let n = (f.as_bytes()[3] - b'0') as usize;
let mut c: Vec<f64> = a.iter().flat_map(|v| v.v[..v.n].to_vec()).collect();
if c.len() == 1 {
c = vec![c[0]; n];
}
c.resize(n, 0.0);
Val::vector(&c)
}
_ => {
if self.unknown.insert(f.clone()) {
self.notes.push(format!("`{f}()` is unknown; it gives 0"));
}
Val::scalar(0.0)
}
}
}
}
}
}
struct Parser<'a> {
tokens: Vec<String>,
at: usize,
calls: &'a mut usize,
}
const OPERATORS: [&str; 18] = [
"||", "&&", "==", "!=", "<=", ">=", "<", ">", "+", "-", "*", "/", "%", "!", "(", ")", ",", ".",
];
impl<'a> Parser<'a> {
fn new(text: &str, calls: &'a mut usize) -> Self {
let mut tokens = Vec::new();
let b = text.as_bytes();
let mut i = 0;
while i < b.len() {
let c = b[i] as char;
if c.is_whitespace() {
i += 1;
} else if c.is_ascii_digit()
|| (c == '.'
&& b.get(i + 1).is_some_and(u8::is_ascii_digit)
&& !tokens
.last()
.is_some_and(|t: &String| t == "." || is_name(t)))
{
let start = i;
while i < b.len()
&& (b[i].is_ascii_alphanumeric()
|| b[i] == b'.'
|| ((b[i] == b'-' || b[i] == b'+') && matches!(b[i - 1], b'e' | b'E')))
{
i += 1;
}
tokens.push(text[start..i].to_owned());
} else if c.is_ascii_alphabetic() || c == '_' {
let start = i;
while i < b.len() && (b[i].is_ascii_alphanumeric() || b[i] == b'_') {
i += 1;
}
tokens.push(text[start..i].to_owned());
} else {
let op = OPERATORS
.iter()
.find(|o| text[i..].starts_with(**o))
.map_or(&text[i..i + c.len_utf8()], |o| o);
i += op.len();
tokens.push(op.to_owned());
}
}
Parser {
tokens,
at: 0,
calls,
}
}
fn peek(&self) -> Option<&str> {
self.tokens.get(self.at).map(String::as_str)
}
fn eat(&mut self, t: &str) -> bool {
let hit = self.peek() == Some(t);
self.at += usize::from(hit);
hit
}
fn expr_all(&mut self) -> Result<Expr, String> {
let e = self.binary(0)?;
match self.peek() {
None => Ok(e),
Some(t) => Err(format!("unexpected `{t}`")),
}
}
/// Precedence levels from the loosest: `||`, `&&`, equality, relations, sums, products.
fn binary(&mut self, level: usize) -> Result<Expr, String> {
const LEVELS: [&[&str]; 6] = [
&["||"],
&["&&"],
&["==", "!="],
&["<", "<=", ">", ">="],
&["+", "-"],
&["*", "/", "%"],
];
if level == LEVELS.len() {
return self.unary();
}
let mut left = self.binary(level + 1)?;
while let Some(op) = self
.peek()
.and_then(|t| LEVELS[level].iter().find(|o| **o == t))
{
self.at += 1;
let right = self.binary(level + 1)?;
left = Expr::Bin(op, Box::new(left), Box::new(right));
}
Ok(left)
}
fn unary(&mut self) -> Result<Expr, String> {
if self.eat("-") {
return Ok(Expr::Neg(Box::new(self.unary()?)));
}
if self.eat("!") {
return Ok(Expr::Not(Box::new(self.unary()?)));
}
self.eat("+");
self.primary()
}
fn primary(&mut self) -> Result<Expr, String> {
let Some(t) = self.peek().map(str::to_owned) else {
return Err("the expression ends early".into());
};
self.at += 1;
if t == "(" {
let e = self.binary(0)?;
return if self.eat(")") {
Ok(e)
} else {
Err("missing `)`".into())
};
}
if t.starts_with(|c: char| c.is_ascii_digit() || c == '.') {
return t
.trim_end_matches(['f', 'F'])
.parse()
.map(Expr::Num)
.map_err(|_| format!("bad number `{t}`"));
}
if !is_name(&t) {
return Err(format!("unexpected `{t}`"));
}
if self.eat("(") {
let mut args = Vec::new();
if !self.eat(")") {
loop {
args.push(self.binary(0)?);
if self.eat(")") {
break;
}
if !self.eat(",") {
return Err(format!("missing `)` after the arguments of `{t}`"));
}
}
}
*self.calls += 1;
return Ok(Expr::Call(t, args, *self.calls));
}
let component = if self.eat(".") {
let c = self.peek().map(str::to_owned).ok_or("nothing after `.`")?;
self.at += 1;
let index = |s: &str| match s {
"x" | "r" | "s" | "0" => Some(0),
"y" | "g" | "t" | "1" => Some(1),
"z" | "b" | "p" | "2" => Some(2),
"w" | "a" | "q" | "3" => Some(3),
_ => None,
};
Some(match c.split_once('.') {
Some((r, col)) => (
index(r).ok_or(format!("bad component `{c}`"))?,
Some(index(col).ok_or(format!("bad component `{c}`"))?),
),
None => (index(&c).ok_or(format!("bad component `{c}`"))?, None),
})
} else {
None
};
Ok(Expr::Name(t, component))
}
}
fn is_name(t: &str) -> bool {
t.starts_with(|c: char| c.is_ascii_alphabetic() || c == '_')
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Macros;
fn custom(text: &str) -> Custom {
Custom::parse(&Properties::parse(text, &Macros::standard()))
}
#[test]
fn expressions_follow_c_precedence_and_file_order() {
let mut c = custom(
"variable.float.a = 1 + 2 * 3 - 4 % 3\n\
uniform.float.b = a / 2\n\
uniform.bool.c = a > 5 && !(b < 1) || false\n\
uniform.int.d = -7 / 2\n\
uniform.vec3.e = vec3(a, cameraPosition.y, 2) * 2\n\
uniform.float.f = if(a < 0, 1, a < 7, 2, 3) + clamp(fmod(-1, 4), 0, 2.5) + min(4, 2, 3)\n\
uniform.float.g = gbufferModelView.1.0 + frac(sunAngle - 0.25) + pi * 0\n\
uniform.float.h = unknownThing + in(biome, 3, BIOME_DESERT)\n\
uniform.int.i = biome == BIOME_PALE_GARDEN || biome == BIOME_OLD\n\
uniform.int.j = CAT_OCEAN + PPT_RAIN\n",
);
c.set_biomes(&["minecraft:beach", "minecraft:desert"]);
assert!(c.notes.is_empty(), "{:?}", c.notes);
let names: Vec<_> = c.uniforms().map(|u| u.0).collect();
assert_eq!(names, ["b", "c", "d", "e", "f", "g", "h", "i", "j"]);
let builtin = |n: &str| match n {
"cameraPosition" => Some(Val::vector(&[1.0, 64.0, 2.0])),
"sunAngle" => Some(Val::scalar(0.125)),
"biome" => Some(Val::scalar(1.0)),
"gbufferModelView" => {
let mut m = Val::scalar(0.0);
m.v[1] = 9.0; // column 0, row 1
m.n = 16;
Some(m)
}
_ => None,
};
let v = c.eval(0.016, &builtin);
let x: Vec<f64> = v.iter().map(|v| v.v[0]).collect();
assert_eq!(
x,
[
3.0,
1.0,
-3.0,
12.0,
2.0 + 2.5 + 2.0,
9.0 + 0.875,
1.0,
0.0,
12.0
]
);
assert_eq!(&v[3].v[..3], &[12.0, 128.0, 4.0]);
assert_eq!(
c.notes,
[
"`unknownThing` is unknown; it reads 0",
"`BIOME_PALE_GARDEN` names no biome; it reads -1",
"`BIOME_OLD` names no biome; it reads -1"
]
);
}
#[test]
fn smooth_halves_the_distance_per_fade_time() {
let mut c = custom("uniform.float.s = smooth(1, target, 2, 0.5)\n");
let mut target = 0.0;
let mut run = |t: f64, dt: f64| {
target = t;
c.eval(dt, &|n: &str| (n == "target").then(|| Val::scalar(target)))[0].v[0]
};
assert_eq!(run(0.0, 0.0), 0.0);
assert!((run(1.0, 2.0) - 0.5).abs() < 1e-9, "rising: fadeIn");
assert!((run(0.0, 0.5) - 0.25).abs() < 1e-9, "falling: fadeOut");
}
#[test]
fn bad_expressions_are_reported() {
let c = custom("uniform.float.a = (1 + \nuniform.mat4.m = 1\nuniform.float.ok = 2\n");
assert_eq!(c.uniforms().count(), 1);
assert_eq!(c.notes.len(), 2, "{:?}", c.notes);
}
}
+15 -4
View File
@@ -12,27 +12,38 @@
//! - [`load_post`]: a dimension's `prepare`, `deferred`, `composite` and `final` programs, in order (2.1).
//! - [`Macros`] and [`eval_condition`]: the macros packs test, and the `#if` arithmetic (1.5).
mod biomes;
mod block_ids;
mod condition;
mod config;
mod custom;
mod discover;
mod lang;
mod macros;
mod options;
mod pack;
mod post;
mod preprocess;
mod properties;
mod translate;
pub use biomes::{category as biome_category, precipitation as biome_precipitation};
pub use block_ids::BlockIds;
pub use condition::eval_condition;
pub use config::{BUFFERS, Buffer, Config, Format, Kind, buffer_index, draw_buffers};
pub use custom::{Custom, Val};
pub use discover::{Found, discover};
pub use lang::Lang;
pub use macros::{MC_VERSION, Macros, RENDER_STAGES};
pub use macros::{MC_VERSION, Macros, RENDER_STAGES, render_stage};
pub use options::switches_on;
pub use pack::{Pack, PackError};
pub use post::{Group, Post, PostProgram, dimension_folder, load_post};
pub use post::{
Blend, CELESTIAL_BLEND, Factor, GameInputs, Group, Post, PostProgram, TRANSLUCENT_BLEND,
dimension_folder, load_post,
};
pub use preprocess::{Diagnostic, Origin, Preprocessed, preprocess};
pub use properties::{Entry, Note, Properties};
pub use translate::{
Error, GbuffersInputs, Interface, Program, Sampler, Target, Uniform, translate_composite,
translate_gbuffers,
DRAW_UNIFORMS, Error, GbuffersInputs, Interface, Program, Sampler, Target, Uniform,
translate_composite, translate_gbuffers,
};
+8
View File
@@ -60,6 +60,14 @@ pub const RENDER_STAGES: [&str; 24] = [
"HAND_TRANSLUCENT",
];
/// The value of `MC_RENDER_STAGE_<name>` (OQ-7: its index in `RENDER_STAGES`).
pub fn render_stage(name: &str) -> u32 {
RENDER_STAGES
.iter()
.position(|s| *s == name)
.unwrap_or_else(|| panic!("no render stage {name}")) as u32
}
impl Macros {
/// The standard macros that need no renderer (spec 1.5.1): `MC_VERSION`, this system's
/// `MC_OS_*`, the GL and GLSL versions Horizon's translation accepts, the render stages and
+103
View File
@@ -0,0 +1,103 @@
//! Switch options (spec 8.1), as far as program-enable expressions (3.3) need them: which are on
//! by default. A switch is `#define NAME` (on) or `//#define NAME` (off) with nothing after the
//! name but a comment, in any stage or included file of the pack. Packs keep their options in
//! included `.glsl` files (or files of any name a source includes) and test them elsewhere
//! (OQ-13), so the test the spec asks for (`#ifdef NAME` / `#ifndef NAME`) may be in any file
//! of the pack.
use std::collections::BTreeSet;
use crate::Pack;
const SOURCES: [&str; 8] = ["vsh", "fsh", "gsh", "csh", "tcs", "tes", "glsl", "inc"];
/// The switch options that are on by default.
pub fn switches_on(pack: &Pack) -> BTreeSet<String> {
let (mut on, mut off, mut tested) = (BTreeSet::new(), BTreeSet::new(), BTreeSet::new());
let files = pack.all_files();
let source = |path: &str| SOURCES.contains(&path.rsplit_once('.').map_or("", |(_, e)| e));
let texts: Vec<(&String, String)> = files
.iter()
.filter(|p| source(p))
.filter_map(|p| Some((p, pack.read_text(p)?)))
.collect();
// Files of other names count when a source includes them (`#include "shaders.settings"`).
let included: BTreeSet<&str> = texts
.iter()
.flat_map(|(_, t)| t.lines())
.filter_map(|l| l.trim().strip_prefix("#include"))
.map(|r| r.trim().trim_matches(|c| c == '"' || c == '<' || c == '>'))
.map(|r| r.rsplit('/').next().unwrap_or(r))
.collect();
let extra: Vec<(&String, String)> = files
.iter()
.filter(|p| !source(p) && included.contains(p.rsplit('/').next().unwrap_or(p)))
.filter_map(|p| Some((p, pack.read_text(p)?)))
.collect();
for (_, text) in texts.iter().chain(&extra) {
for line in text.lines() {
let line = line.trim();
let (commented, rest) = match line.strip_prefix("//") {
Some(rest) => (true, rest.trim_start()),
None => (false, line),
};
let Some((directive, rest)) = rest
.strip_prefix('#')
.map(str::trim_start)
.and_then(|r| r.split_once(|c: char| c.is_whitespace()))
else {
continue;
};
let mut words = rest.split_whitespace();
let Some(name) = words.next() else { continue };
match directive {
"ifdef" | "ifndef" if !commented => _ = tested.insert(name.to_owned()),
"define" if words.next().is_none_or(|w| w.starts_with("//")) => {
let set = if commented { &mut off } else { &mut on };
set.insert(name.to_owned());
}
_ => {}
}
}
}
// Defined both ways is ambiguous (8.1): it keeps its default only where it is on everywhere.
on.retain(|name| tested.contains(name) && !off.contains(name));
on
}
#[cfg(test)]
mod tests {
use super::*;
use crate::pack::tests::zip;
#[test]
fn switches_need_a_test_and_no_value() {
let pack = Pack::from_zip_bytes(
"P.zip",
zip(&[
(
"shaders/lib/settings.glsl",
" #define AO // ambient occlusion\n//#define BLOOM\n#define LEVEL 2 //[1 2]\n\
#define SPLIT\n#define UNTESTED\n",
),
(
"shaders/world0/deferred.fsh",
"#ifdef AO\n#endif\n#ifndef BLOOM\n#endif\n#ifdef SPLIT\n#endif\n#ifdef LEVEL\n#endif\n",
),
("shaders/world-1/deferred.fsh", "//#define SPLIT\n"),
(
"shaders/final.fsh",
"#include \"shaders.settings\"\n#ifdef GODRAYS\n#endif\n",
),
("shaders/shaders.settings", " #define GODRAYS //Toggle\n"),
("shaders/notes.settings", "#define NOT_INCLUDED\n#ifdef NOT_INCLUDED\n"),
("shaders/shaders.properties", "#define NOT_SOURCE\n#ifdef NOT_SOURCE\n"),
]),
)
.unwrap();
assert_eq!(
switches_on(&pack),
BTreeSet::from(["AO".to_owned(), "GODRAYS".to_owned()])
);
}
}
+33
View File
@@ -167,6 +167,39 @@ impl Pack {
names.sort();
names
}
/// Every file of the pack, by its path inside `shaders`, sorted.
pub fn all_files(&self) -> Vec<String> {
let mut names = match &self.files {
Files::Folder(root) => {
let mut names = Vec::new();
let mut dirs = vec![String::new()];
while let Some(dir) = dirs.pop() {
for e in fs::read_dir(root.join(&dir))
.into_iter()
.flatten()
.flatten()
{
let name = e.file_name().to_string_lossy().into_owned();
let path = if dir.is_empty() {
name
} else {
format!("{dir}/{name}")
};
match e.file_type() {
Ok(t) if t.is_dir() => dirs.push(path),
Ok(t) if t.is_file() => names.push(path),
_ => {}
}
}
}
names
}
Files::Zip { entries, .. } => entries.keys().cloned().collect(),
};
names.sort();
names
}
}
/// `a/./b/../c` → `a/c`; `None` for a path that climbs out of `shaders`.
+257 -15
View File
@@ -10,7 +10,10 @@
//! - A program that fails to translate is reported and treated as missing (2.5).
//! - Terrain draws with the first program of its fallback chain that loads (2.3).
use std::collections::BTreeSet;
use crate::config::{self, Config};
use crate::custom::Custom;
use crate::{GbuffersInputs, Macros, Pack, Preprocessed, Program, Properties, Target};
use crate::{eval_condition, preprocess, translate_composite, translate_gbuffers};
@@ -21,10 +24,67 @@ pub enum Group {
Deferred,
Composite,
Final,
/// A gbuffers program: the game draws its geometry with it.
/// A gbuffers program: the game draws its geometry with it, before deferred.
Gbuffers,
/// A gbuffers program drawing translucent geometry, after deferred (2.1).
Translucent,
}
/// A blend factor of `blend.<program>` (3.2), as GL names it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Factor {
Zero,
One,
SrcColor,
OneMinusSrcColor,
DstColor,
OneMinusDstColor,
SrcAlpha,
OneMinusSrcAlpha,
DstAlpha,
OneMinusDstAlpha,
SrcAlphaSaturate,
}
impl Factor {
fn parse(word: &str) -> Option<Self> {
Some(match word {
"ZERO" => Self::Zero,
"ONE" => Self::One,
"SRC_COLOR" => Self::SrcColor,
"ONE_MINUS_SRC_COLOR" => Self::OneMinusSrcColor,
"DST_COLOR" => Self::DstColor,
"ONE_MINUS_DST_COLOR" => Self::OneMinusDstColor,
"SRC_ALPHA" => Self::SrcAlpha,
"ONE_MINUS_SRC_ALPHA" => Self::OneMinusSrcAlpha,
"DST_ALPHA" => Self::DstAlpha,
"ONE_MINUS_DST_ALPHA" => Self::OneMinusDstAlpha,
"SRC_ALPHA_SATURATE" => Self::SrcAlphaSaturate,
_ => return None,
})
}
}
/// Source and destination factors for colour, then for alpha.
pub type Blend = [Factor; 4];
/// The default blend of translucent geometry (3.2).
pub const TRANSLUCENT_BLEND: Blend = [
Factor::SrcAlpha,
Factor::OneMinusSrcAlpha,
Factor::One,
Factor::OneMinusSrcAlpha,
];
/// `gbuffers_water` and its fallbacks (2.3).
const WATER: [&str; 5] = [
"gbuffers_water",
"gbuffers_terrain",
"gbuffers_textured_lit",
"gbuffers_textured",
"gbuffers_basic",
];
/// `gbuffers_terrain` and its fallbacks (2.3).
const TERRAIN: [&str; 4] = [
"gbuffers_terrain",
@@ -33,6 +93,24 @@ const TERRAIN: [&str; 4] = [
"gbuffers_basic",
];
/// `gbuffers_skybasic` and its fallback (2.3).
const SKY: [&str; 2] = ["gbuffers_skybasic", "gbuffers_basic"];
/// `gbuffers_skytextured` and its fallbacks (2.3).
const SKY_TEXTURED: [&str; 3] = [
"gbuffers_skytextured",
"gbuffers_textured",
"gbuffers_basic",
];
/// The sun and moon's default blend: added to the sky, as vanilla draws them.
pub const CELESTIAL_BLEND: Blend = [
Factor::SrcAlpha,
Factor::One,
Factor::One,
Factor::OneMinusSrcAlpha,
];
const GROUPS: [(Group, &str); 4] = [
(Group::Prepare, "prepare"),
(Group::Deferred, "deferred"),
@@ -49,6 +127,11 @@ pub struct PostProgram {
pub buffers: Vec<u32>,
/// `flip.<name>.<buffer>`: buffers forced to flip (true) or kept (false) after this program.
pub flips: Vec<(u32, bool)>,
/// How a gbuffers program's output blends into its buffers; `None` writes them.
pub blend: Option<Blend>,
/// Bit n: `colortex<n>MipmapEnabled` in its fragment stage, so buffer n's mip chain is
/// regenerated right before it (6.1.7; not for gbuffers programs).
pub mipmaps: u16,
}
pub struct Post {
@@ -60,6 +143,14 @@ pub struct Post {
pub composite_pre: Vec<(u32, bool)>,
/// What draws solid and cutout terrain, when the game asked for it and one loads.
pub terrain: Option<PostProgram>,
/// What draws translucent terrain (water, stained glass) after deferred.
pub water: Option<PostProgram>,
/// What draws the sky (disc, sunset, stars, void) before terrain.
pub sky: Option<PostProgram>,
/// What draws the sun and moon, between the sunset and the stars.
pub sky_textured: Option<PostProgram>,
/// The custom uniforms of `shaders.properties` (3.4).
pub custom: Custom,
pub config: Config,
/// Programs skipped and why, as `program: message` or `file:line: message`.
pub notes: Vec<String>,
@@ -76,14 +167,23 @@ pub fn dimension_folder(pack: &Pack, id: i32) -> String {
}
}
/// Loads the post-processing programs of `folder` and, given the game's terrain vertices, the
/// program terrain draws with.
/// The game's geometry a pack may draw with its gbuffers programs: each kind's vertex inputs,
/// when the game can draw it with a pack.
#[derive(Clone, Debug, Default)]
pub struct GameInputs {
pub terrain: Option<GbuffersInputs>,
pub sky: Option<GbuffersInputs>,
pub celestial: Option<GbuffersInputs>,
}
/// Loads the post-processing programs of `folder` and, for the geometry `inputs` describes, the
/// gbuffers programs it draws with.
pub fn load_post(
pack: &Pack,
folder: &str,
properties: &Properties,
macros: &Macros,
terrain: Option<&GbuffersInputs>,
inputs: &GameInputs,
) -> Post {
let mut post = Post {
folder: folder.to_owned(),
@@ -91,6 +191,10 @@ pub fn load_post(
deferred_pre: flips(properties, "deferred_pre"),
composite_pre: flips(properties, "composite_pre"),
terrain: None,
water: None,
sky: None,
sky_textured: None,
custom: Custom::parse(properties),
config: Config::default(),
notes: Vec::new(),
};
@@ -99,6 +203,7 @@ pub fn load_post(
folder,
properties,
macros,
switches: crate::switches_on(pack),
post: &mut post,
};
for (group, base) in GROUPS {
@@ -119,13 +224,43 @@ pub fn load_post(
}
}
}
if let Some(inputs) = terrain {
loader.post.terrain = TERRAIN.iter().find_map(|name| {
loader.load(name.to_string(), Group::Gbuffers, |v, f| {
let mut gbuffers = |names: &[&str], group, inputs: &GbuffersInputs| {
names.iter().find_map(|name| {
loader.load(name.to_string(), group, |v, f| {
translate_gbuffers(v, f, Target::Buffers, inputs)
})
})
};
// The sky blends as vanilla draws its sunset fan and stars unless `blend.*` says otherwise.
let sky = inputs
.sky
.as_ref()
.and_then(|i| gbuffers(&SKY, Group::Gbuffers, i));
let sky = sky.map(|mut p| {
p.blend = blend(properties, &p.name).unwrap_or(Some(TRANSLUCENT_BLEND));
p
});
let celestial = inputs.celestial.as_ref();
let sky_textured = celestial.and_then(|i| gbuffers(&SKY_TEXTURED, Group::Gbuffers, i));
let sky_textured = sky_textured.map(|mut p| {
p.blend = blend(properties, &p.name).unwrap_or(Some(CELESTIAL_BLEND));
p
});
if let Some(inputs) = &inputs.terrain {
let terrain = gbuffers(&TERRAIN, Group::Gbuffers, inputs);
// gbuffers_water falls back to gbuffers_terrain's chain (2.3).
let water = gbuffers(&WATER, Group::Translucent, inputs);
loader.post.terrain = terrain;
loader.post.water = water.map(|mut p| {
p.blend = blend(properties, &p.name).unwrap_or(Some(TRANSLUCENT_BLEND));
p
});
if let Some(p) = &mut loader.post.terrain {
p.blend = blend(properties, &p.name).flatten();
}
}
loader.post.sky = sky;
loader.post.sky_textured = sky_textured;
post.config = std::mem::take(&mut post.config).finish();
post
}
@@ -135,6 +270,7 @@ struct Loader<'a> {
folder: &'a str,
properties: &'a Properties,
macros: &'a Macros,
switches: BTreeSet<String>,
post: &'a mut Post,
}
@@ -156,7 +292,13 @@ impl Loader<'_> {
if !self.pack.exists(&fsh) || !self.pack.exists(&vsh) {
return None;
}
if !enabled(self.properties, self.folder, &name, self.macros) {
if !enabled(
self.properties,
self.folder,
&name,
self.macros,
&self.switches,
) {
return None;
}
if self.pack.exists(&path("gsh")) {
@@ -191,12 +333,24 @@ impl Loader<'_> {
self.post.config.read(&vertex);
self.post.config.read(&fragment);
let buffers = config::draw_buffers(&fragment).unwrap_or_else(|| (0..8).collect());
let mipmaps = match group {
Group::Gbuffers | Group::Translucent => 0,
_ => {
let mut own = Config::default();
own.read(&fragment);
(own.buffers.iter().enumerate())
.filter(|b| b.1.mipmap)
.fold(0, |m, (i, _)| m | 1 << i)
}
};
Some(PostProgram {
flips: flips(self.properties, &name),
name,
group,
program,
buffers,
blend: None,
mipmaps,
})
}
}
@@ -215,8 +369,14 @@ fn diagnostics(pp: &Preprocessed) -> Vec<String> {
}
/// `program.<name>.enabled` (or `program.<folder>/<name>.enabled`): a boolean expression over
/// switch options (3.3). An option is on when its macro is defined.
fn enabled(properties: &Properties, folder: &str, name: &str, macros: &Macros) -> bool {
/// switch options (3.3): those on by default (8.1) and the defined macros.
fn enabled(
properties: &Properties,
folder: &str,
name: &str,
macros: &Macros,
switches: &BTreeSet<String>,
) -> bool {
let keys = [
format!("program.{folder}/{name}.enabled"),
format!("program.{name}.enabled"),
@@ -230,6 +390,7 @@ fn enabled(properties: &Properties, folder: &str, name: &str, macros: &Macros) -
match token {
"true" => condition.push('1'),
"false" => condition.push('0'),
t if switches.contains(t) => condition.push('1'),
t if t.starts_with(|c: char| c.is_alphabetic() || c == '_') => {
condition.push_str(&format!("defined({t})"));
}
@@ -239,6 +400,20 @@ fn enabled(properties: &Properties, folder: &str, name: &str, macros: &Macros) -
eval_condition(&condition, macros).ok() != Some(0)
}
/// `blend.<program>`: `Some(None)` for `off`, `Some(factors)`, or `None` when absent or not
/// four known factors.
fn blend(properties: &Properties, program: &str) -> Option<Option<Blend>> {
let value = properties.get(&format!("blend.{program}"))?.trim();
if value == "off" {
return Some(None);
}
let factors: Vec<Factor> = value
.split_whitespace()
.map(Factor::parse)
.collect::<Option<_>>()?;
Some(Some(factors.try_into().ok()?))
}
/// `flip.<program>.<buffer>=<bool>` keys of one program.
fn flips(properties: &Properties, program: &str) -> Vec<(u32, bool)> {
let prefix = format!("flip.{program}.");
@@ -280,7 +455,8 @@ mod tests {
("shaders/deferred.vsh", VSH),
(
"shaders/deferred.fsh",
"#version 120\nvoid main() { gl_FragColor = vec4(1.0); }\n",
"#version 120\nconst bool colortex5MipmapEnabled = true;\n\
void main() { gl_FragColor = vec4(1.0); }\n",
),
("shaders/composite1.fsh", FSH),
("shaders/composite3.vsh", VSH),
@@ -307,11 +483,18 @@ mod tests {
);
assert_eq!(dimension_folder(&pack, 0), "");
assert_eq!(dimension_folder(&pack, -1), "world-1");
let post = load_post(&pack, "", &properties, &Macros::standard(), None);
let post = load_post(
&pack,
"",
&properties,
&Macros::standard(),
&GameInputs::default(),
);
let names: Vec<&str> = post.programs.iter().map(|p| p.name.as_str()).collect();
assert_eq!(names, ["deferred", "composite", "composite2", "final"]);
let p = &post.programs;
assert_eq!(p[0].buffers, (0..8).collect::<Vec<_>>());
assert_eq!((p[0].mipmaps, p[1].mipmaps), (1 << 5, 0));
assert_eq!(p[1].buffers, [3]);
assert_eq!(p[1].flips, [(3, false)]);
assert_eq!(p[2].flips, [(4, true)]);
@@ -327,13 +510,72 @@ mod tests {
let mut macros = Macros::standard();
macros.define("HQ", "");
let post = load_post(&pack, "world-1", &properties, &macros, None);
let post = load_post(
&pack,
"world-1",
&properties,
&macros,
&GameInputs::default(),
);
let names: Vec<&str> = post.programs.iter().map(|p| p.name.as_str()).collect();
assert_eq!(names, ["final"]);
let post = load_post(&pack, "", &properties, &macros, None);
let post = load_post(&pack, "", &properties, &macros, &GameInputs::default());
assert!(post.programs.iter().any(|p| p.name == "composite3"));
}
/// Water falls back to the terrain chain and blends by default, as the sky does; `blend.*`
/// overrides.
#[test]
fn water_falls_back_to_terrain_and_blends() {
let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/packs/terrain-test");
let pack = Pack::open(&dir).unwrap();
let macros = Macros::standard();
let inputs = crate::GbuffersInputs {
declarations: "layout(location = 0) in vec3 hz_v_pos;".into(),
body: " hz_Vertex = vec4(hz_v_pos, 1.0);\n hz_Color = vec4(1.0);\n".into(),
..Default::default()
};
let inputs = GameInputs {
terrain: Some(inputs.clone()),
sky: Some(inputs.clone()),
celestial: Some(inputs),
};
let post = load_post(&pack, "", &Properties::default(), &macros, &inputs);
let (terrain, water) = (post.terrain.unwrap(), post.water.unwrap());
assert_eq!((terrain.group, terrain.blend), (Group::Gbuffers, None));
assert_eq!(water.name, "gbuffers_terrain");
let sky = post.sky.unwrap();
assert_eq!(
(sky.name.as_str(), sky.group, sky.blend),
(
"gbuffers_skybasic",
Group::Gbuffers,
Some(TRANSLUCENT_BLEND)
)
);
// gbuffers_skytextured falls back to gbuffers_textured, then gbuffers_basic.
let sun = post.sky_textured.unwrap();
assert_eq!(
(sun.name.as_str(), sun.blend),
("gbuffers_textured", Some(CELESTIAL_BLEND))
);
assert_eq!(
(water.group, water.blend),
(Group::Translucent, Some(TRANSLUCENT_BLEND))
);
let properties = Properties::parse(
"blend.gbuffers_terrain = ONE ONE ZERO ONE\nblend.gbuffers_water = off\n",
&macros,
);
let terrain_blend = blend(&properties, "gbuffers_terrain");
assert_eq!(
terrain_blend,
Some(Some([Factor::One, Factor::One, Factor::Zero, Factor::One]))
);
assert_eq!(blend(&properties, "gbuffers_water"), Some(None));
assert_eq!(blend(&properties, "gbuffers_basic"), None);
}
#[test]
fn horizons_test_pack_loads() {
let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/packs/post-test");
@@ -345,7 +587,7 @@ mod tests {
&dimension_folder(&pack, 0),
&properties,
&macros,
None,
&GameInputs::default(),
);
assert!(post.notes.is_empty(), "{:?}", post.notes);
let names: Vec<&str> = post.programs.iter().map(|p| p.name.as_str()).collect();
+173 -9
View File
@@ -45,8 +45,19 @@ pub struct Interface {
pub samplers: Vec<Sampler>,
/// The fragment stage's output locations.
pub outputs: Vec<u32>,
/// A gbuffers program reads `DRAW_UNIFORMS`: the block at set 2, binding 0, set per draw.
pub draw: bool,
}
/// The uniforms a gbuffers program reads per draw (5.3's "D"), in a block of their own
/// (`HzDraw`, std140: three ints, a pad, a vec4; 32 bytes) the game sets for each draw.
pub const DRAW_UNIFORMS: [(&str, &str); 4] = [
("renderStage", "int"),
("entityId", "int"),
("blockEntityId", "int"),
("entityColor", "vec4"),
];
#[derive(Clone, Debug, PartialEq)]
pub struct Uniform {
pub name: String,
@@ -98,6 +109,8 @@ pub struct GbuffersInputs {
/// The value of each attribute a pack may declare (`mc_Entity`, `vaPosition`...), as a GLSL
/// expression of the declared type's size; an attribute not listed reads zero.
pub attributes: Vec<(String, String)>,
/// GLSL statements run last in `main`, with `gl_Position` in wgpu's clip space.
pub after: String,
}
/// Translates a composite-style program (`composite`, `deferred`, `final`...).
@@ -170,6 +183,17 @@ fn translate(
samplers.insert(name.clone(), ty.clone());
}
}
// Per-draw values move to their own block; another type than the documented one keeps
// the frame's block (and reads zero).
let mut draw = false;
if gbuffers.is_some() {
for (name, ty) in DRAW_UNIFORMS {
if uniforms.get(name).is_some_and(|t| t == ty) {
uniforms.remove(name);
draw = true;
}
}
}
let samplers: Vec<Sampler> = samplers
.into_iter()
.enumerate()
@@ -180,13 +204,30 @@ fn translate(
})
.collect();
// Varyings by name: the vertex outputs, then fragment inputs the vertex stage lacks.
// Varyings by name: the vertex outputs, then fragment inputs the vertex stage lacks. A
// matrix or array travels as one varying per column or element (`varying_parts`).
let mut locations: BTreeMap<String, (u32, String)> = BTreeMap::new();
let mut split: BTreeMap<String, (String, Vec<(String, String)>)> = BTreeMap::new();
for var in v.outs.iter().chain(&f.ins) {
let next = locations.len() as u32;
locations
.entry(var.name.clone())
.or_insert((next, var.decl.clone()));
if locations.contains_key(&var.name) || split.contains_key(&var.name) {
continue;
}
match varying_parts(&var.decl) {
Some((global, parts)) => {
let mut copies = Vec::new();
for (k, (decl, access)) in parts.into_iter().enumerate() {
let part = format!("hz_v_{}_{k}", var.name);
let next = locations.len() as u32;
locations.insert(part.clone(), (next, decl));
copies.push((part, access));
}
split.insert(var.name.clone(), (global, copies));
}
None => {
let next = locations.len() as u32;
locations.insert(var.name.clone(), (next, var.decl.clone()));
}
}
}
let header = |stage: &Stage| {
@@ -203,6 +244,12 @@ fn translate(
.unwrap();
}
h.push_str(" };\n");
if draw {
h.push_str(
"layout(set = 2, binding = 0) uniform HzDraw { int renderStage; int entityId; \
int blockEntityId; int hz_draw_pad; vec4 entityColor; };\n",
);
}
for s in &samplers {
let (texture, sampler) = split_sampler(&s.ty);
writeln!(
@@ -225,6 +272,9 @@ fn translate(
)
.unwrap();
}
for (name, (global, _)) in &split {
writeln!(h, "{}", global.replace("@", name)).unwrap();
}
if let (Some(inputs), ShaderStage::Vertex) = (gbuffers, stage.kind) {
h.push_str(&inputs.declarations);
h.push('\n');
@@ -262,9 +312,16 @@ fn translate(
for name in &v.inits {
writeln!(vertex_main, " {name} = hz_init_{name}();").unwrap();
}
vertex_main.push_str(" hz_main();\n");
for (name, (_, copies)) in &split {
for (part, access) in copies {
writeln!(vertex_main, " {part} = {name}{access};").unwrap();
}
}
write!(
vertex_main,
" hz_main();\n gl_Position = vec4(gl_Position.x, {flip}gl_Position.y, (gl_Position.z + gl_Position.w) * 0.5, gl_Position.w);\n}}\n"
" gl_Position = vec4(gl_Position.x, {flip}gl_Position.y, (gl_Position.z + gl_Position.w) * 0.5, gl_Position.w);\n{}}}\n",
gbuffers.map_or("", |g| &g.after)
)
.unwrap();
let mut fragment_main = String::from("\nvoid main() {\n");
@@ -278,6 +335,11 @@ fn translate(
Target::Buffers => " hz_FragCoord = gl_FragCoord;\n",
});
}
for (name, (_, copies)) in &split {
for (part, access) in copies {
writeln!(fragment_main, " {name}{access} = {part};").unwrap();
}
}
for name in &f.inits {
writeln!(fragment_main, " {name} = hz_init_{name}();").unwrap();
}
@@ -327,10 +389,57 @@ fn translate(
uniforms,
samplers,
outputs,
draw,
},
})
}
/// A matrix or array varying (`flat mat3 @[2];`): wgpu's stage interfaces take only scalars
/// and vectors (WGSL), so it becomes a private global (`mat3 @[2];`) and one varying per
/// column or element, each with the qualifiers and how to reach it (`[1][2]`). `None` for
/// scalars and vectors, and for sizes that are not plain numbers.
fn varying_parts(decl: &str) -> Option<(String, Vec<(String, String)>)> {
let (before, after) = decl.split_once('@')?;
let mut words: Vec<&str> = before.split_whitespace().collect();
let ty = words.pop()?;
let qualifiers: String = words.iter().map(|w| format!("{w} ")).collect();
let array = after.trim().trim_end_matches(';').trim();
let mut sizes = Vec::new();
let mut rest = array;
while let Some(r) = rest.strip_prefix('[') {
let (n, r) = r.split_once(']')?;
sizes.push(n.trim().parse::<usize>().ok()?);
rest = r.trim_start();
}
if !rest.is_empty() {
return None;
}
// matC or matCxR: C columns of R rows.
let column = ty.strip_prefix("mat").and_then(|d| {
let (c, r) = d.split_once('x').unwrap_or((d, d));
Some((c.parse::<usize>().ok()?, r.parse::<usize>().ok()?))
});
if sizes.is_empty() && column.is_none() {
return None;
}
let part_ty = column.map_or(ty.to_owned(), |(_, r)| format!("vec{r}"));
if let Some((c, _)) = column {
sizes.push(c);
}
let count: usize = sizes.iter().product();
let parts = (0..count)
.map(|mut k| {
let mut access = String::new();
for &size in sizes.iter().rev() {
access.insert_str(0, &format!("[{}]", k % size));
k /= size;
}
(format!("{qualifiers}{part_ty} @;"), access)
})
.collect();
Some((format!("{ty} @{array};"), parts))
}
/// `sampler2DShadow` → (`texture2D`, `samplerShadow`); `isampler3D` → (`itexture3D`, `sampler`).
pub(super) fn split_sampler(ty: &str) -> (String, &'static str) {
let shadow = ty.ends_with("Shadow");
@@ -394,7 +503,11 @@ fn compile(stage: &Stage, text: &str, header_lines: usize) -> Result<naga::Modul
/// The uniform block's size and member offsets, from naga's layout.
fn block_layout(module: &naga::Module) -> (u32, Vec<(String, u32)>) {
for (_, var) in module.global_variables.iter() {
if var.space != naga::AddressSpace::Uniform {
let frame = var
.binding
.as_ref()
.is_some_and(|b| b.group == 0 && b.binding == 0);
if var.space != naga::AddressSpace::Uniform || !frame {
continue;
}
if let naga::TypeInner::Struct { members, span } = &module.types[var.ty].inner {
@@ -472,6 +585,21 @@ mod tests {
assert_eq!(i.outputs, [0, 1]);
}
/// naga drops non-constant global initializers: they run in our `main`, one per declarator,
/// converting as GLSL's initialization does.
#[test]
fn global_initializers_read_uniforms() {
let fsh = "#version 120\nuniform float rainStrength;\nuniform ivec2 atlasSize;\n\
vec2 size = atlasSize;\nvec3 a = vec3(rainStrength), b = a * 2.0, c;\n\
const float k = rainStrength > 0.5 ? 1.0 : 0.0;\nfloat w[] = float[](rainStrength, 1.0);\n\
void main() { c = b; gl_FragColor = vec4(a + c, size.x * k + w[0]); }\n";
let p = program(
&[("composite.vsh", VSH_120), ("composite.fsh", fsh)],
Target::Window,
);
assert!(p.is_ok(), "{:?}", p.err());
}
#[test]
fn core_programs_translate() {
let vsh = "#version 330 core\nin vec3 vaPosition;\nin vec2 vaUV0;\nuniform mat4 modelViewMatrix, projectionMatrix;\n\
@@ -531,9 +659,9 @@ mod tests {
light = (gl_TextureMatrix[1] * gl_MultiTexCoord1).xy;\n color = gl_Color;\n \
normal = normalize(gl_NormalMatrix * gl_Normal);\n id = mc_Entity.x + mc_midTexCoord.x;\n}\n";
let fsh = "#version 120\nuniform sampler2D texture;\nuniform sampler2D lightmap;\nuniform mat4 gbufferModelView;\n\
varying vec2 uv;\nvarying vec2 light;\nvarying vec4 color;\nvarying vec3 normal;\nvarying float id;\n\
uniform int renderStage;\nvarying vec2 uv;\nvarying vec2 light;\nvarying vec4 color;\nvarying vec3 normal;\nvarying float id;\n\
void main() {\n vec4 c = texture2D(texture, uv) * texture2D(lightmap, light) * color;\n \
/* DRAWBUFFERS:02 */\n gl_FragData[0] = c * gl_FragCoord.z;\n \
/* DRAWBUFFERS:02 */\n gl_FragData[0] = c * gl_FragCoord.z * float(renderStage);\n \
gl_FragData[1] = vec4(normal * 0.5 + 0.5, id);\n}\n";
let files: Vec<(String, &str)> =
[("gbuffers_terrain.vsh", vsh), ("gbuffers_terrain.fsh", fsh)]
@@ -549,6 +677,7 @@ mod tests {
hz_MultiTexCoord1 = vec4(240.0, 240.0, 0.0, 1.0);\n hz_Color = vec4(1.0);\n hz_Normal = vec3(0.0, 1.0, 0.0);\n"
.into(),
attributes: vec![("mc_midTexCoord".into(), "vec2(game_uv) / 32768.0".into())],
..Default::default()
};
let p = translate_gbuffers(
&preprocess(&pack, "gbuffers_terrain.vsh", &m),
@@ -571,6 +700,14 @@ mod tests {
assert!(names.contains(&name), "{name} missing from {names:?}");
}
assert_eq!(p.interface.outputs, vec![0, 1]);
// renderStage is set per draw, in its own block.
assert!(p.interface.draw && !names.contains(&"renderStage"));
let per_draw = p.fragment.global_variables.iter().any(|(_, v)| {
v.binding
.as_ref()
.is_some_and(|b| b.group == 2 && b.binding == 0)
});
assert!(per_draw);
let inputs: Vec<&str> = p.vertex.entry_points[0]
.function
.arguments
@@ -583,6 +720,33 @@ mod tests {
);
}
#[test]
fn matrix_and_array_varyings_travel_in_parts() {
let vsh = "#version 120\nvarying mat3 tbn;\nflat varying ivec2 ids[2];\nvarying mat2x3 m[2];\n\
void main() {\n gl_Position = ftransform();\n tbn = mat3(2.0);\n \
ids[1] = ivec2(3);\n m[1][0] = vec3(4.0);\n}\n";
let fsh = "#version 120\nvarying mat3 tbn;\nflat varying ivec2 ids[2];\nvarying mat2x3 m[2];\n\
void main() { gl_FragColor = vec4(tbn[2] + m[1][0], float(ids[1].x)); }\n";
let p = program(
&[("composite.vsh", vsh), ("composite.fsh", fsh)],
Target::Buffers,
)
.unwrap_or_else(|e| panic!("{e:?}"));
let inputs = p.fragment.entry_points[0].function.arguments.len();
// 3 columns + 2 elements + 2 x 2 columns, and the fragment position.
assert!(inputs >= 9, "{inputs} inputs");
assert_eq!(
varying_parts("flat ivec2 @[2];").unwrap().1,
[
("flat ivec2 @;".to_owned(), "[0]".to_owned()),
("flat ivec2 @;".to_owned(), "[1]".to_owned())
]
);
assert_eq!(varying_parts("mat2x3 @[2];").unwrap().1[3].1, "[1][1]");
assert!(varying_parts("vec4 @;").is_none());
assert!(varying_parts("vec4 @[N];").is_none());
}
#[test]
fn locals_may_hide_a_sampler() {
let fsh = "#version 120\nuniform sampler2D specular;\nstruct M { float specular; };\n\
+95 -21
View File
@@ -338,41 +338,115 @@ impl<'p> Stage<'p> {
}
}
/// A global `T name = a ? b : c;`: naga can't evaluate a conditional at global scope, so
/// the initializer becomes a function of ours on the same line (`T name; T hz_init_name() {
/// return a ? b : c; }`), called by our `main` before the pack's.
/// A global `T name = a ? b : c;` or, without `const`, any initialized global: naga only
/// evaluates constant initializers at global scope (a conditional is not one; GL drivers
/// also take uniforms and other globals there), so each initializer becomes a function of
/// ours on the same line (`T a; T hz_init_a() { return T(...); } T b; ...`), called by our
/// `main` before the pack's, in file order. The return converts as GLSL's initialization
/// does (`vec2 v = ivec2(...)`).
pub(super) fn initializer(&mut self, words: &[usize], to: usize) {
let text = |i: usize| self.tokens[i];
let Some(eq) = words.iter().position(|&i| text(i) == "=") else {
return;
};
if !words[eq..].iter().any(|&i| text(i) == "?")
|| words[..eq].iter().any(|&i| text(i) == ",")
{
// Declarators, split at top-level commas: (first word, `=` if any, end).
let mut declarators = Vec::new();
let (mut begin, mut nesting, mut assign) = (0, 0i32, None);
for (n, &i) in words.iter().enumerate() {
match text(i) {
"(" | "[" | "{" => nesting += 1,
")" | "]" | "}" => nesting -= 1,
"=" if nesting == 0 && assign.is_none() => assign = Some(n),
"," if nesting == 0 => {
declarators.push((begin, assign.take(), n));
begin = n + 1;
}
_ => {}
}
}
declarators.push((begin, assign, words.len()));
let constant = words[..eq].iter().any(|&i| text(i) == "const");
let conditional =
|d: &(usize, Option<usize>, usize)| words[d.0..d.2].iter().any(|&i| text(i) == "?");
if constant && !declarators.iter().any(conditional) {
return;
}
let Some(name_at) = words[..eq].iter().rposition(|&i| is_ident(text(i))) else {
// The first declarator's name ends the type.
let first_end = declarators[0].1.unwrap_or(declarators[0].2);
let Some(name_at) = words[..first_end].iter().rposition(|&i| is_ident(text(i))) else {
return;
};
let name = text(words[name_at]).to_owned();
let array: String = words[name_at + 1..eq].iter().map(|&i| text(i)).collect();
let ty: Vec<&str> = words[..name_at]
.iter()
.map(|&i| text(i))
.filter(|&t| t != "const")
.collect();
let Some(ty) = ty.last() else { return };
// The type, with a `vec3[]`'s count written in.
let mut ty = String::new();
for &i in &words[..name_at] {
match text(i) {
"const" => {}
"]" if ty.ends_with('[') => match self.array_length(i) {
Some(n) => write!(ty, "{n}]").unwrap(),
None => return,
},
t if ty.is_empty() || t == "[" => ty.push_str(t),
// Qualifiers before the type (`highp`...): the type is the last word.
t => ty = t.to_owned(),
}
}
if ty.is_empty() {
return;
}
let mut replaced = Vec::new();
let mut names = Vec::new();
for (n, &(begin, assign, end)) in declarators.iter().enumerate() {
let name_at = if n == 0 { name_at } else { begin };
let name = text(words[name_at]).to_owned();
if !is_ident(&name) {
return;
}
let stop = assign.unwrap_or(end);
let mut array: String = words[name_at + 1..stop].iter().map(|&i| text(i)).collect();
if array == "[]" {
// `T x[] = T[](a, b)`: the count is written in, as the rewrite does for the name.
match assign.and_then(|a| self.array_length(words[a - 1])) {
Some(n) => array = format!("[{n}]"),
None => return,
}
}
let next = if n + 1 < declarators.len() {
format!(" {ty}")
} else {
String::new()
};
let end_token = words.get(end).copied().unwrap_or(to);
match assign {
Some(a) => {
// Scalars, vectors and matrices convert; arrays and structs must match.
let scalar = array.is_empty()
&& !ty.contains('[')
&& (matches!(ty.as_str(), "float" | "int" | "uint" | "bool")
|| ty.contains("vec")
|| ty.starts_with("mat"));
let (open, close) = if scalar {
(format!("{ty}("), ")")
} else {
(String::new(), "")
};
replaced.push((
words[a],
format!("; {ty}{array} hz_init_{name}() {{ return {open}"),
));
replaced.push((end_token, format!("{close}; }}{next}")));
names.push(name);
}
None if n + 1 < declarators.len() => replaced.push((end_token, format!(";{next}"))),
None => {}
}
}
for &i in &words[..name_at] {
if text(i) == "const" {
self.removed.push(i..i + 1);
}
}
self.replaced.insert(
words[eq],
format!("; {ty}{array} hz_init_{name}() {{ return"),
);
self.replaced.insert(to, "; }".into());
self.inits.push(name);
self.replaced.extend(replaced);
self.inits.extend(names);
}
/// A function definition or prototype: remember which parameters are samplers.
@@ -0,0 +1,11 @@
# terrain-test
Horizon's own shader pack for testing gbuffers terrain (written for Horizon, no other pack's code).
`gbuffers_terrain` draws blocks from the atlas with their vertex colour and lightmap, tinted by
the lightmap coordinate's sky light (cooler where the sky is hidden); `mc_midTexCoord` marks each
face's centre with a dot; leaves (id 1 in `block.properties`) are yellow, and any stage but solid terrain (translucent terrain falls back to it)
is red. `gbuffers_skybasic` draws the sky in its vertex colour, a little greener, with magenta
stars and a dark red void; `gbuffers_textured` (the fallback of `gbuffers_skytextured`) the sun,
and the moon tinted green. `composite` puts the depth buffer's view-space distance into the blue
channel of the far half. Copy the folder into `shaderpacks/` and run the game with
`HORIZON_SHADER_PACK=terrain-test`.
@@ -0,0 +1,3 @@
# Leaves draw yellow in gbuffers_terrain.
block.1=oak_leaves spruce_leaves birch_leaves jungle_leaves acacia_leaves dark_oak_leaves \
mangrove_leaves cherry_leaves azalea_leaves flowering_azalea_leaves pale_oak_leaves
@@ -0,0 +1,19 @@
#version 120
uniform sampler2D colortex0;
uniform sampler2D depthtex0;
uniform mat4 gbufferProjectionInverse;
uniform float far;
varying vec2 texcoord;
void main() {
vec3 color = texture2D(colortex0, texcoord).rgb;
if (texcoord.x > 0.5) {
float depth = texture2D(depthtex0, texcoord).r;
vec4 view = gbufferProjectionInverse * vec4(vec3(texcoord, depth) * 2.0 - 1.0, 1.0);
color.b = clamp(length(view.xyz / view.w) / far, 0.0, 1.0);
}
/* DRAWBUFFERS:0 */
gl_FragData[0] = vec4(color, 1.0);
}
@@ -0,0 +1,8 @@
#version 120
varying vec2 texcoord;
void main() {
gl_Position = ftransform();
texcoord = gl_MultiTexCoord0.xy;
}
@@ -0,0 +1,15 @@
#version 120
uniform int renderStage;
varying vec4 glcolor;
void main() {
vec4 color = glcolor;
// Stars magenta, the void dark red, the sky disc a little greener: each stage is told apart.
if (renderStage == MC_RENDER_STAGE_STARS) color.rgb = vec3(1.0, 0.0, 1.0);
if (renderStage == MC_RENDER_STAGE_VOID) color.rgb = vec3(0.3, 0.0, 0.0);
if (renderStage == MC_RENDER_STAGE_SKY) color.g = min(color.g + 0.2, 1.0);
/* DRAWBUFFERS:0 */
gl_FragData[0] = color;
}
@@ -0,0 +1,8 @@
#version 120
varying vec4 glcolor;
void main() {
gl_Position = ftransform();
glcolor = gl_Color;
}
@@ -0,0 +1,28 @@
#version 120
uniform sampler2D texture;
uniform sampler2D lightmap;
uniform ivec2 atlasSize;
uniform int renderStage;
varying vec2 texcoord;
varying vec2 lmcoord;
varying vec2 mid;
varying vec4 glcolor;
varying float blockId;
void main() {
vec4 color = texture2D(texture, texcoord) * glcolor;
if (color.a < 0.1) discard;
color.rgb *= texture2D(lightmap, lmcoord).rgb;
// Less sky light: bluer.
color.rgb = mix(color.rgb * vec3(0.6, 0.7, 1.2), color.rgb, lmcoord.y);
// Leaves (block.properties' id 1) yellow.
if (abs(blockId - 1.0) < 0.5) color.rgb = vec3(color.g, color.g, 0.0);
// A dot at the face's centre, one texel wide.
if (length((texcoord - mid) * vec2(atlasSize)) < 1.0) color.rgb = vec3(1.0, 0.0, 1.0);
// Any other stage than solid terrain (translucent terrain falls back here): red.
if (renderStage != MC_RENDER_STAGE_TERRAIN_SOLID) color.rgb = vec3(1.0, 0.0, 0.0);
/* DRAWBUFFERS:0 */
gl_FragData[0] = color;
}
@@ -0,0 +1,19 @@
#version 120
attribute vec4 mc_midTexCoord;
attribute vec4 mc_Entity;
varying vec2 texcoord;
varying vec2 lmcoord;
varying vec2 mid;
varying vec4 glcolor;
varying float blockId;
void main() {
gl_Position = ftransform();
texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;
lmcoord = (gl_TextureMatrix[1] * gl_MultiTexCoord1).xy;
mid = mc_midTexCoord.xy;
glcolor = gl_Color;
blockId = mc_Entity.x;
}
@@ -0,0 +1,15 @@
#version 120
uniform sampler2D texture;
uniform int renderStage;
varying vec2 texcoord;
varying vec4 glcolor;
void main() {
vec4 color = texture2D(texture, texcoord) * glcolor;
// The moon tinted green: the sun and the moon are told apart.
if (renderStage == MC_RENDER_STAGE_MOON) color.rb *= 0.3;
/* DRAWBUFFERS:0 */
gl_FragData[0] = color;
}
@@ -0,0 +1,10 @@
#version 120
varying vec2 texcoord;
varying vec4 glcolor;
void main() {
gl_Position = ftransform();
texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;
glcolor = gl_Color;
}
+18 -8
View File
@@ -167,21 +167,31 @@ fn real_packs_read() {
declarations: "layout(location = 0) in vec3 hz_v_pos;".into(),
body: " hz_Vertex = vec4(hz_v_pos, 1.0);\n hz_Color = vec4(1.0);\n".into(),
attributes: vec![("mc_Entity".into(), "vec4(-1.0)".into())],
..Default::default()
};
let post = load_post(
&pack,
&folder,
&shaders,
&Macros::standard(),
Some(&terrain),
);
let inputs = horizon_shaders::GameInputs {
terrain: Some(terrain.clone()),
sky: Some(terrain.clone()),
celestial: Some(terrain),
};
let post = load_post(&pack, &folder, &shaders, &Macros::standard(), &inputs);
println!(
" dimension {id} (folder `{folder}`): {} post programs, terrain {:?}, {} notes, {:.0} ms",
" dimension {id} (folder `{folder}`): {} post programs, terrain {:?}, water {:?}, sky {:?} {:?}, {} notes, {:.0} ms",
post.programs.len(),
post.terrain.as_ref().map(|t| &t.name),
post.water.as_ref().map(|t| &t.name),
post.sky.as_ref().map(|t| &t.name),
post.sky_textured.as_ref().map(|t| &t.name),
post.notes.len() + post.config.notes.len(),
start.elapsed().as_secs_f64() * 1000.0
);
let mut custom = post.custom;
custom.eval(0.016, &|_| None);
println!(
" {} custom uniforms; notes: {:?}",
custom.uniforms().count(),
custom.notes.iter().take(4).collect::<Vec<_>>()
);
for n in post.notes.iter().chain(&post.config.notes).take(12) {
println!(" {n}");
}
+1 -1
View File
@@ -6,7 +6,7 @@ license.workspace = true
rust-version = "1.85"
description = "Scheduling hints for the game's threads: the OS calls the crates that forbid unsafe code can't make"
[target.'cfg(any(target_vendor = "apple", target_os = "linux"))'.dependencies]
[target.'cfg(any(target_vendor = "apple", target_os = "linux", target_os = "android"))'.dependencies]
libc = "0.2"
[target.'cfg(windows)'.dependencies]
+9 -9
View File
@@ -45,8 +45,8 @@ fn favour() -> bool {
/// behind the server thread and the render thread (`behind_server`).
static HELPERS_BEHIND: AtomicBool = AtomicBool::new(false);
/// How many nice levels a helper thread goes below the process on Linux.
#[cfg(target_os = "linux")]
/// How many nice levels a helper thread goes below the process on Linux and Android.
#[cfg(any(target_os = "linux", target_os = "android"))]
const BEHIND: i32 = 10;
/// From now on, the helper threads that call `behind_server` as they start
@@ -58,7 +58,7 @@ pub fn put_helpers_behind() {
/// Called by a helper thread as it starts (world generation, lighting and
/// meshing, a connection's packet writer): work no tick or frame waits for.
/// Once `put_helpers_behind` was called, on Linux, where the server and
/// Once `put_helpers_behind` was called, on Linux and Android, where the server and
/// render threads can't be raised (`favour_current`), the helper lowers
/// itself instead, which an unprivileged thread may: 10 nice levels below
/// the process, or, where nice runs out (a game started under nice 10 or
@@ -69,10 +69,10 @@ pub fn behind_server() -> bool {
HELPERS_BEHIND.load(Ordering::Relaxed) && behind()
}
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "android"))]
fn behind() -> bool {
// SAFETY: these read and set the calling thread's own nice value or
// policy (who 0, pid 0 is the calling thread on Linux); `param` lives
// policy (who 0, pid 0 is the calling thread on Linux and Android); `param` lives
// across the call.
unsafe {
let nice = libc::getpriority(libc::PRIO_PROCESS as _, 0);
@@ -85,7 +85,7 @@ fn behind() -> bool {
}
}
#[cfg(not(target_os = "linux"))]
#[cfg(not(any(target_os = "linux", target_os = "android")))]
fn behind() -> bool {
false
}
@@ -117,12 +117,12 @@ mod tests {
}
/// A helper goes behind only once a dedicated server asks for it, and
/// then, on Linux, 10 nice levels below the process or in the idle class.
/// then, on Linux and Android, 10 nice levels below the process or in the idle class.
#[test]
fn helpers_go_behind_only_when_asked() {
assert!(!std::thread::spawn(super::behind_server).join().unwrap());
super::put_helpers_behind();
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "android"))]
std::thread::spawn(|| {
// SAFETY: reads the calling thread's own nice value.
let before = unsafe { libc::getpriority(libc::PRIO_PROCESS as _, 0) };
@@ -142,7 +142,7 @@ mod tests {
})
.join()
.unwrap();
#[cfg(not(target_os = "linux"))]
#[cfg(not(any(target_os = "linux", target_os = "android")))]
assert!(!std::thread::spawn(super::behind_server).join().unwrap());
}
}
@@ -592,6 +592,37 @@ impl Behaviours {
{
return InteractionResult::Pass;
}
if self.data(state).chain.contains(&Class::CopperGolemStatue) {
return self.turn_statue(level, ticks, pos, state, item);
}
self.use_without_item(level, ticks, pos, face, hit, player)
}
/// `CopperGolemStatueBlock.useItemOn` (and its weathering subclass's): anything but an axe
/// (`#minecraft:axes`, every one named `*_axe`), even an empty hand, turns the statue to its
/// next pose; honeycomb waxes a statue that still weathers instead.
fn turn_statue<L: LevelAccess + ?Sized>(
&self,
level: &mut L,
ticks: &mut Ticks,
pos: BlockPos,
state: BlockStateId,
item: Option<&str>,
) -> InteractionResult {
let item = item.unwrap_or("minecraft:air");
let waxed = self.table.name(state).starts_with("minecraft:waxed_");
if item.ends_with("_axe") || (item == "minecraft:honeycomb" && !waxed) {
return InteractionResult::Pass;
}
let mut sim = self.sim(level, ticks);
sim.sound(
pos,
&"minecraft:entity.copper_golem_become_statue".into(),
1.0,
1.0,
);
sim.set_block_and_update(pos, self.table.cycle(state, self.props.copper_golem_pose));
sim.game_event(pos, "minecraft:block_change");
InteractionResult::Success
}
}
+1
View File
@@ -35,6 +35,7 @@ To play, use [the launcher](../launcher/README.md); the project site is [playhor
## Architecture: world generation
- [World generation](world-generation.md): the pipeline from noise to features.
- [Pregeneration](pregeneration.md): `/pregen`, generating, lighting and saving an area ahead of the players.
- [Noise generation](noise-generation.md), [compiled density functions](density-functions.md), [structure density](structure-density.md), [terrain filling](terrain-filling.md).
- Biomes: [lookup](biome-lookup.md), [temperatures](biome-temperatures.md).
- Surfaces: [material surfaces](material-surfaces.md), [material conditions](material-conditions.md), [rule libraries](material-rule-libraries.md), [vertical anchors](vertical-anchors.md), [value providers](value-providers.md).
+105 -2
View File
@@ -11,19 +11,21 @@ The contract between Horizon and its mods: the WIT package `horizon:mods@0.1.0`,
| `chat` | [`chat`](#chat) | show you chat messages |
| `hud` | [`hud`](#hud) | draw text on your screen |
| `storage` | [`storage`](#storage) | save its own settings to disk |
| `command` | [`command`](#command) | run commands as you, with your permissions |
| `registries` | data files (`assets/`, `data/`), no interface | add blocks and items to your worlds |
| `ui` | [`ui`](#ui) | show its own screens and a settings page |
| `render` | [`render`](#render) | draw on your screen and shake the camera |
## World `horizon-mod`
- Imports (what the game offers): `log`, `game`, `chat`, `hud`, `storage`
- Imports (what the game offers): `log`, `game`, `chat`, `hud`, `storage`, `command`
- Exports (what the mod provides): `events`
## World `horizon-ui-mod`
A mod with widgets (capability `ui`): the SDK builds every mod against this world. Mods built for `horizon-mod` alone (no `ui-events` export) still load and simply hear nothing of their widgets.
- Imports (what the game offers): `ui`, `log`, `game`, `chat`, `hud`, `storage`
- Imports (what the game offers): `ui`, `render`, `log`, `game`, `chat`, `hud`, `storage`, `command`
- Exports (what the mod provides): `ui-events`, `events`
## World `ui-host`
@@ -33,6 +35,13 @@ The game's side of `horizon-ui-mod` beyond `horizon-mod`.
- Imports (what the game offers): `ui`
- Exports (what the mod provides): `ui-events`
## World `render-host`
The game's side of capability `render` (imports only).
- Imports (what the game offers): `render`
- Exports (what the mod provides):
## `log`
Access: always granted.
@@ -132,6 +141,16 @@ Capability `storage`: a per-mod folder of small key/value files, quota'd (docs/m
| `set` | `func(key: string, value: list<u8>) -> result<_, string>` | Errs on a malformed key or over quota (bytes for the whole mod). |
| `delete` | `func(key: string)` | No error if `key` was already absent. |
## `command`
Access: capability `command`.
Capability `command`: vanilla commands run as the player, with the player's permissions, after the hook call returns (8 per call). A mod's own commands are not run this way.
| Function | Signature | What it does |
| --- | --- | --- |
| `run` | `func(line: string)` | `line` without its slash; its feedback goes to the chat as if typed. |
## `ui`
Access: capability `ui`.
@@ -205,6 +224,90 @@ What the game calls on a mod that shows widgets (capability `ui`): what the play
| `closed` | `func()` | The player left the mod's screen (Esc or Done); not for a screen the mod closed itself or one that went away with the mod. |
| `settings-opened` | `func()` | The player opened the mod's settings page in Options > Mods: call `ui::set-settings` now to fill it with fresh widgets. |
## `render`
Access: capability `render`.
Capability `render` (experimental: the manifest needs `experimental = ["render"]`): shapes the game draws for the mod with its own renderer, kept until the mod replaces them (no call per frame). A mod never supplies shader code.
### record `rect`
A rectangle in GUI pixels: the top left corner and the size.
| Field | Type | |
| --- | --- | --- |
| `x` | `f32` | |
| `y` | `f32` | |
| `w` | `f32` | |
| `h` | `f32` | |
### record `point`
A point in GUI pixels from the screen's top left.
| Field | Type | |
| --- | --- | --- |
| `x` | `f32` | |
| `y` | `f32` | |
### record `fill-shape`
| Field | Type | |
| --- | --- | --- |
| `rect` | `rect` | |
| `color` | `u32` | 0xAARRGGBB. |
### record `quad-shape`
Any four-cornered shape (a wheel's segment, an arrow): the corners in order round it.
| Field | Type | |
| --- | --- | --- |
| `corners` | `tuple<point, point, point, point>` | |
| `color` | `u32` | 0xAARRGGBB. |
### record `sprite-shape`
| Field | Type | |
| --- | --- | --- |
| `sprite` | `string` | One of the game's GUI sprites, as `gui/sprites/` names them (`hud/heart/full`, `widget/button`), the resource packs' versions included. An unknown one draws nothing. |
| `rect` | `rect` | |
| `color` | `u32` | A tint, 0xAARRGGBB: 0xFFFFFFFF draws it as it is. |
### record `item-shape`
| Field | Type | |
| --- | --- | --- |
| `item` | `string` | `minecraft:diamond`, `<mod>:<path>`; an unknown one draws nothing. |
| `at` | `point` | The icon's top left. |
| `scale` | `f32` | 1.0 is the hotbar's 16 × 16. |
### record `text-shape`
| Field | Type | |
| --- | --- | --- |
| `text` | `string` | Up to 256 characters, one line. |
| `at` | `point` | |
| `color` | `u32` | 0xRRGGBB, as `hud`'s lines. |
| `centered` | `bool` | Centred on `at.x` instead of starting there. |
| `shadow` | `bool` | |
### variant `shape`
| Case | Payload | |
| --- | --- | --- |
| `fill` | `fill-shape` | |
| `quad` | `quad-shape` | |
| `sprite` | `sprite-shape` | |
| `item` | `item-shape` | |
| `text` | `text-shape` | |
| Function | Signature | What it does |
| --- | --- | --- |
| `set-hud` | `func(shapes: list<shape>)` | Replaces the mod's HUD shapes: up to 1024, drawn every frame in order over the HUD (after the hotbar, under chat and screens), hidden with the HUD (F1). An empty list removes them. |
| `gui-size` | `func() -> tuple<f32, f32>` | The screen's size in GUI pixels (width, height), as of this tick. |
| `shake` | `func(strength: f32, seconds: f32)` | Shakes the camera: a jolt of `strength` (0 to 1, 1 the hardest) that fades out over `seconds` (at most 10). Jolts add up to 1. The view turns, the player doesn't, and the player's Distortion Effects option scales it (0 turns it off). |
## `events`
Access: exported by the mod: the game calls it.

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