Merge main into #1264: keep the --snapshot usage line

main's warmed-timing change and this branch both edited the usage comment
at the top of examples/chunkgen_bench.rs. Kept this branch's line, which
adds [--snapshot PATH]; no code change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-22 20:29:09 +02:00
71 changed files with 3847 additions and 387 deletions
@@ -0,0 +1,153 @@
//! Read-only saved chunk benchmark. All timings exclude resource/corpus setup.
//!
//! load_bench RESOURCES DIMENSION_DIR [--rounds N] [--limit N] [--checksum]
//! Add --profile-worker when a sampling collector cannot sample the main thread.
//! Reports warm filesystem SaveDir::load (read, inflate, NBT, decode and light),
//! plus isolated inflate/NBT and native decode. No files in the save are changed.
use horizon_client::{
assets::Assets,
biome::Biomes,
blocks::BlockFacts,
save::{SaveDir, SaveTables, decode_with},
};
use horizon_world::{nbt::Limits, region::RegionFile};
use sha2::{Digest, Sha256};
use std::{hint::black_box, path::PathBuf, sync::Arc, time::Instant};
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
fn main() -> Result<()> {
if std::env::args().any(|a| a == "--profile-worker") {
std::thread::spawn(|| run().map_err(|e| e.to_string()))
.join()
.expect("benchmark worker")
.map_err(Into::into)
} else {
run()
}
}
fn run() -> Result<()> {
let args: Vec<String> = std::env::args().skip(1).collect();
let usage = "load_bench RESOURCES DIMENSION_DIR [--rounds N] [--limit N] [--checksum]";
let root = PathBuf::from(args.first().ok_or(usage)?);
let dimension = PathBuf::from(args.get(1).ok_or(usage)?);
let flag = |name: &str, default: usize| -> Result<usize> {
Ok(match args.iter().position(|a| a == name) {
Some(i) => args.get(i + 1).ok_or(usage)?.parse()?,
None => default,
})
};
let rounds = flag("--rounds", 5)?;
let limit = flag("--limit", 256)?;
if rounds == 0 || limit == 0 {
return Err("rounds and limit must be positive".into());
}
let assets = Assets::load(&root)?;
let facts = BlockFacts::load(&root, assets.registry)?;
let biomes = Biomes::load(&root)?;
let tables = Arc::new(SaveTables::new(&assets).with_block_entities(&facts));
let source = SaveDir::new(dimension.clone());
let _ = source.tables.set(tables.clone());
let mut paths: Vec<_> = std::fs::read_dir(dimension.join("region"))?
.map(|e| e.map(|e| e.path()))
.collect::<std::io::Result<_>>()?;
paths.retain(|p| p.extension().is_some_and(|e| e == "mca"));
paths.sort();
let mut corpus = Vec::new();
'regions: for path in paths {
let mut region = RegionFile::open(path)?;
let mut positions: Vec<_> = region.chunks().map(|(pos, _)| pos).collect();
positions.sort_by_key(|p| (p.x, p.z));
for pos in positions {
let record = region.read(pos)?.ok_or("missing record")?;
let tag = record.nbt(Limits::default())?;
if decode_with(&tag, assets.registry, &biomes, Some(&tables)).is_none() {
continue;
}
corpus.push((pos, record, tag));
if corpus.len() == limit {
break 'regions;
}
}
}
if corpus.is_empty() {
return Err("no full chunks in corpus".into());
}
let mut records_hash = Sha256::new();
for (pos, record, _) in &corpus {
records_hash.update(pos.x.to_le_bytes());
records_hash.update(pos.z.to_le_bytes());
records_hash.update((record.compressed.len() as u64).to_le_bytes());
records_hash.update(&record.compressed);
}
println!(
"{}",
serde_json::json!({"corpus": dimension, "chunks": corpus.len(), "rounds": rounds,
"compressed_records_sha256": format!("{:x}", records_hash.finalize()),
"compressed_bytes": corpus.iter().map(|(_, r, _)| r.compressed.len()).sum::<usize>()})
);
// Prime SaveDir's region handles and the filesystem cache before measurement.
for (pos, _, _) in &corpus {
black_box(
source
.load(pos.x, pos.z, assets.registry, &biomes)
.ok_or("load failed")?,
);
}
for round in 0..rounds {
let started = Instant::now();
for (pos, _, _) in &corpus {
black_box(
source
.load(pos.x, pos.z, assets.registry, &biomes)
.ok_or("load failed")?,
);
}
let load_s = started.elapsed().as_secs_f64();
let started = Instant::now();
for (_, record, _) in &corpus {
black_box(record.nbt(Limits::default())?);
}
let nbt_s = started.elapsed().as_secs_f64();
let started = Instant::now();
for (_, _, tag) in &corpus {
black_box(
decode_with(tag, assets.registry, &biomes, Some(&tables)).ok_or("decode failed")?,
);
}
let decode_s = started.elapsed().as_secs_f64();
println!(
"{}",
serde_json::json!({"round": round, "load_s": load_s,
"inflate_nbt_s": nbt_s, "decode_s": decode_s})
);
}
if args.iter().any(|a| a == "--checksum") {
let mut hash = Sha256::new();
let mut light_loaded = 0;
for (pos, _, _) in &corpus {
let chunk = source
.load(pos.x, pos.z, assets.registry, &biomes)
.ok_or("load failed")?;
hash.update(pos.x.to_le_bytes());
hash.update(pos.z.to_le_bytes());
light_loaded += usize::from(chunk.light_loaded);
for section in &chunk.sections {
for i in 0..4096 {
hash.update(section.get(i).to_le_bytes());
hash.update([section.light(i)]);
}
for i in 0..64 {
hash.update(section.biomes.get(i).to_le_bytes());
}
}
}
println!(
"{}",
serde_json::json!({"blocks_biomes_light_sha256": format!("{:x}", hash.finalize()),
"light_loaded": light_loaded})
);
}
Ok(())
}
@@ -1027,6 +1027,7 @@ fn main() -> horizon_client::Result<()> {
nearest_player: player.as_ref().filter(|p| !p.spectator).map(|p| p.position),
others: $snapshot,
others_grid: $grid,
others_index: None,
daylight: (world.day_time.rem_euclid(24000)) < 12000,
game_time: world.game_time,
day_time: world.day_time,
@@ -1132,7 +1133,7 @@ fn main() -> horizon_client::Result<()> {
}
#[allow(unused_mut)] // filled only with the rng-trace feature
let mut traces: Vec<String> = Vec::new();
let mut pushes = Vec::new();
let mut pushes = horizon_client::mobs::PendingPushes::default();
let mut born = Vec::new();
for i in 0..mobs.len() {
if mobs[i].removed {
+1 -1
View File
@@ -528,7 +528,7 @@ impl Furnace {
let has_fuel = self.items[1].is_some();
if lit || has_fuel && has_ingredient {
if let Some(ingredient) = self.items[0].clone() {
if let Some(recipe) = recipes.cook(self.kind.recipe_type(), &ingredient).cloned() {
if let Some(recipe) = recipes.cook(self.kind.recipe_type(), &ingredient) {
if self.cooking_timer == 0 {
self.cooking_total = total_time(recipe.time, self.speed);
}
+96
View File
@@ -1102,7 +1102,11 @@ pub struct Recipes {
facts: HashMap<Arc<str>, Facts>,
/// The `#minecraft:curse` enchantments.
curses: HashSet<String>,
/// The first cooking recipe of each kind taking each item, built at the
/// first lookup (furnaces look theirs up every tick).
cook_index: std::sync::OnceLock<CookIndex>,
}
type CookIndex = HashMap<String, crate::fast_hash::FxHashMap<String, usize>>;
/// `MinMaxBounds.Ints` as (min, max) with the given defaults.
fn bounds(v: Option<&Value>, default: (i64, i64), open: (i64, i64)) -> (i64, i64) {
match v {
@@ -1421,6 +1425,7 @@ impl Recipes {
meta,
facts,
curses,
cook_index: Default::default(),
})
}
/// Match a `size`x`size` grid (row-major) against the crafting recipes.
@@ -1606,6 +1611,22 @@ impl Recipes {
out
}
pub fn cook(&self, kind: &str, input: &ItemStack) -> Option<&Cooking> {
let index = self.cook_index.get_or_init(|| {
let mut index = CookIndex::new();
for (i, c) in self.cooking.iter().enumerate() {
let of_kind = index.entry(c.kind.clone()).or_default();
for item in &c.ingredient.0 {
of_kind.entry(item.clone()).or_insert(i);
}
}
index
});
let i = *index.get(kind)?.get(&*input.item)?;
self.cooking.get(i)
}
/// `cook` by scanning every recipe, as it was before the index.
#[cfg(test)]
fn cook_scan(&self, kind: &str, input: &ItemStack) -> Option<&Cooking> {
self.cooking
.iter()
.find(|c| c.kind == kind && c.ingredient.test(input))
@@ -1625,6 +1646,80 @@ mod tests {
s.data_mut().banner_patterns = vec![BannerLayer::new("minecraft:cross", "red")];
s
}
/// About vanilla's count of cooking recipes: four kinds, ingredients of
/// one to twelve items, overlapping (several recipes take some items).
fn cooking_book() -> Recipes {
let kinds = ["smelting", "blasting", "smoking", "campfire_cooking"];
let cooking = (0..320usize)
.map(|i| Cooking {
id: format!("r{i}"),
category: "misc".into(),
group: String::new(),
kind: kinds[i % 4].into(),
ingredient: ingredient(
&(0..1 + i % 12)
.map(|k| format!("minecraft:item_{}", (i * 7 + k * 13) % 400))
.collect::<Vec<_>>()
.iter()
.map(String::as_str)
.collect::<Vec<_>>(),
),
result: ItemStack::new("minecraft:stone", 1),
experience: 0.1,
time: 200,
})
.collect();
Recipes {
crafting: Vec::new(),
cooking,
meta: HashMap::new(),
facts: HashMap::new(),
curses: HashSet::new(),
cook_index: Default::default(),
}
}
#[test]
fn cook_finds_the_first_recipe_the_scan_finds() {
let recipes = cooking_book();
for kind in [
"smelting",
"blasting",
"smoking",
"campfire_cooking",
"other",
] {
for n in 0..420 {
let stack = ItemStack::new(&format!("minecraft:item_{n}"), 1);
let a = recipes.cook(kind, &stack).map(|c| &c.id);
let b = recipes.cook_scan(kind, &stack).map(|c| &c.id);
assert_eq!(a, b, "{kind} item_{n}");
}
}
}
/// A furnace's recipe lookup, once per lit furnace per tick.
#[test]
#[ignore]
fn cook_cost() {
let recipes = cooking_book();
let stacks: Vec<ItemStack> = (0..1000)
.map(|n| ItemStack::new(&format!("minecraft:item_{}", n % 400), 1))
.collect();
let t = std::time::Instant::now();
let a = stacks
.iter()
.filter(|s| recipes.cook_scan("smelting", s).cloned().is_some())
.count();
let before = t.elapsed().as_secs_f64() * 1e3;
recipes.cook("smelting", &stacks[0]);
let t = std::time::Instant::now();
let b = stacks
.iter()
.filter(|s| recipes.cook("smelting", s).is_some())
.count();
let after = t.elapsed().as_secs_f64() * 1e3;
assert_eq!(a, b);
println!("1000 furnace lookups (scan and clone -> index): {before:.3} ms -> {after:.3} ms");
}
fn run(special: &Special, cells: &[ItemStack]) -> Option<ItemStack> {
let recipes = Recipes {
crafting: Vec::new(),
@@ -1632,6 +1727,7 @@ mod tests {
meta: HashMap::new(),
facts: HashMap::new(),
curses: HashSet::new(),
cook_index: Default::default(),
};
let ctx = Context {
recipes: &recipes,
+6
View File
@@ -583,6 +583,10 @@ pub struct ArmorStand {
pub no_base_plate: bool,
pub invisible: bool,
pub marker: bool,
/// `CustomName`: the text of a hologram.
pub custom_name: Option<crate::text::Text>,
/// `CustomNameVisible`.
pub name_visible: bool,
/// Game time of the last hit (the wobble and the second-hit window).
pub last_hit: i64,
}
@@ -603,6 +607,8 @@ impl ArmorStand {
no_base_plate: false,
invisible: false,
marker: false,
custom_name: None,
name_visible: false,
last_hit: i64::MIN / 2,
}
}
+52
View File
@@ -13,6 +13,7 @@ use crate::{
doubles, float, floats, item_from_nbt, item_to_nbt, number, put, read_doubles, text,
uuid_from_tag,
},
text::Text,
};
use horizon_core::geometry::Vec3;
use horizon_world::nbt::{Compound, Tag};
@@ -555,6 +556,14 @@ fn custom_name(tag: &Tag) -> Option<String> {
_ => None,
}
}
/// `CustomName` as text: a string keeps its legacy formatting codes, anything
/// else is a component (`ComponentSerialization`).
pub fn name_text(tag: &Tag) -> Text {
match tag {
Tag::String(s) => Text::legacy(&s.to_string_lossy()),
t => crate::scoreboard::comp_text(&crate::scoreboard::tag_to_json(t)),
}
}
pub fn mob_from_nbt(tag: &Tag, types: &HashMap<String, Arc<MobType>>, id: u32) -> Option<Mob> {
let kind = types.get(&self::kind(tag)?)?.clone();
let position = position(tag)?;
@@ -606,6 +615,7 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap<String, Arc<MobType>>, id: u32) -
m.persistent = flag(tag, "PersistenceRequired");
m.extra.pick_up_loot = flag(tag, "CanPickUpLoot");
m.custom_name = custom_name(tag);
m.custom_name_text = tag.get("CustomName").map(name_text);
if m.kind.category == "creature" {
m.extra.age = number(tag.get("Age")).unwrap_or(0) as i32;
m.extra.forced_age = number(tag.get("ForcedAge")).unwrap_or(0) as i32;
@@ -1266,6 +1276,21 @@ pub fn armor_stand_to_nbt(a: &ArmorStand, uuid: [i32; 4], raw: Option<&Tag>) ->
} else if let Tag::Compound(map) = &mut tag {
map.remove(&"Marker".into());
}
if a.name_visible {
put(&mut tag, "CustomNameVisible", Tag::Byte(1));
} else if let Tag::Compound(map) = &mut tag {
map.remove(&"CustomNameVisible".into());
}
// A name from the save keeps its own shape unless it changed.
if let Some(name) = &a.custom_name
&& !tag.get("CustomName").is_some_and(|t| name_text(t) == *name)
{
put(
&mut tag,
"CustomName",
crate::scoreboard::json_to_tag(&name.component()),
);
}
put(&mut tag, "Health", Tag::Float(a.health.to_bits()));
put(&mut tag, "Fire", Tag::Short(a.fire_ticks as i16));
// LivingEntity.addAdditionalSaveData and ArmorStand's own fields.
@@ -1294,6 +1319,8 @@ pub fn armor_stand_from_nbt(tag: &Tag, id: u32) -> Option<ArmorStand> {
a.velocity = motion(tag);
a.health = float(tag.get("Health")).unwrap_or(20.0) as f32;
a.fire_ticks = number(tag.get("Fire")).unwrap_or(0) as i32;
a.custom_name = tag.get("CustomName").map(name_text);
a.name_visible = flag(tag, "CustomNameVisible");
if let Some(eq) = tag.get("equipment") {
for (i, slot) in ArmorStand::SLOTS.iter().enumerate() {
a.equipment[i] = eq.get(slot).and_then(item_from_nbt);
@@ -1574,6 +1601,31 @@ mod tests {
assert!(ominous_item_spawner_from_nbt(&raw).is_none());
}
#[test]
fn armor_stand_names_keep_their_formatting_across_a_save() {
let tag = Tag::compound([
(
"Pos",
Tag::List(vec![
Tag::Double(0.0f64.to_bits()),
Tag::Double(64.0f64.to_bits()),
Tag::Double(0.0f64.to_bits()),
]),
),
("CustomName", string("§6Hello")),
("CustomNameVisible", Tag::Byte(1)),
]);
let a = armor_stand_from_nbt(&tag, 7).unwrap();
let name = a.custom_name.as_ref().unwrap();
assert_eq!(name.plain(), "Hello");
assert!(name.spans[0].style.color.is_some());
assert!(a.name_visible);
let saved = armor_stand_to_nbt(&a, [0; 4], Some(&tag));
let restored = armor_stand_from_nbt(&saved, 7).unwrap();
assert_eq!(restored.custom_name, a.custom_name);
assert!(restored.name_visible);
}
#[test]
fn falling_block_round_trip_keeps_impact_distance_and_settings() {
let state = Tag::compound([("Name", string("minecraft:damaged_anvil"))]);
+20 -8
View File
@@ -214,6 +214,7 @@ use horizon_world::{
registry::BlockStateId,
};
use std::{
cell::RefCell,
collections::{HashMap, HashSet},
path::PathBuf,
sync::{
@@ -272,9 +273,10 @@ struct Options {
benchmark: Option<String>,
/// `--dev-mod PATH` (repeatable): mods always on and reloaded when rebuilt.
dev_mods: Vec<PathBuf>,
/// Single player runs on an integrated server thread (game/integrated.rs),
/// as vanilla's does: the default. `--no-server-thread` or
/// `HORIZON_SERVER_THREAD=0` runs the world in the frame instead.
/// `--server-thread` or `HORIZON_SERVER_THREAD=1`: single player runs on an
/// integrated server thread (game/integrated.rs), as vanilla's does. Off by
/// default again until a server's players can board, ride and hit
/// vehicles (#1222); `--no-server-thread` keeps it off.
server_thread: bool,
/// Set on the integrated server's own options: it draws nothing, so it skips meshing.
integrated: bool,
@@ -331,7 +333,7 @@ impl Options {
dev_mods: Vec::new(),
// The command harness (HORIZON_COMMAND_SCRIPT) drives the world
// itself, without the event loop: it stays in the frame.
server_thread: std::env::var("HORIZON_SERVER_THREAD").map_or(true, |v| v != "0")
server_thread: std::env::var("HORIZON_SERVER_THREAD").is_ok_and(|v| v == "1")
&& std::env::var_os("HORIZON_COMMAND_SCRIPT").is_none(),
integrated: false,
};
@@ -341,7 +343,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 --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 --no-server-thread 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 --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 --server-thread Run single player's world on its server thread (no vehicles yet)\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);
@@ -1387,7 +1389,7 @@ struct Game<'a> {
map_patches: Vec<horizon_client::font::Patch>,
/// Player skin downloads, and which skin each atlas skin slot holds
/// (least recently shown first).
skin_downloads: horizon_client::skins::Downloader,
skin_downloads: RefCell<horizon_client::skins::Downloader>,
skin_slots: Vec<(String, usize)>,
cart_models: minecarts::CartModels,
/// The boat the player rides.
@@ -1417,6 +1419,8 @@ struct Game<'a> {
arrow_model: Option<horizon_client::mobs::Part>,
tnts: Vec<tnt::PrimedTnt>,
hoppers: HashMap<(i32, i32, i32), machines::HopperState>,
/// Where item entities are, while the block entities tick (hoppers).
item_columns: Option<machines::ItemColumns>,
creative_screen: Creative,
control: Option<horizon_client::control::Server>,
frame_stats: horizon_client::control::FrameStats,
@@ -4485,10 +4489,17 @@ impl Game<'_> {
};
if capture {
self.mouse.set_ignore_first_move();
// macOS warps by re-associating the mouse with the pointer,
// which undoes a Locked grab and leaves the pointer free: warp
// first there. Wayland moves a pointer only while it is locked,
// so everywhere else warp after grabbing.
#[cfg(target_os = "macos")]
warp(window);
self.captured = window
.set_cursor_grab(CursorGrabMode::Locked)
.or_else(|_| window.set_cursor_grab(CursorGrabMode::Confined))
.is_ok();
#[cfg(not(target_os = "macos"))]
if self.captured && self.focused {
warp(window);
}
@@ -11627,11 +11638,11 @@ impl<'a> Game<'a> {
breath_clouds: Vec::new(),
dragon_model: prepared.dragon_model,
map_patches: Vec::new(),
skin_downloads: horizon_client::skins::Downloader::new(
skin_downloads: RefCell::new(horizon_client::skins::Downloader::new(
std::env::current_dir()
.unwrap_or_default()
.join("assets/skins"),
),
)),
skin_slots: Vec::new(),
cart_models: prepared.cart_models,
riding: None,
@@ -11651,6 +11662,7 @@ impl<'a> Game<'a> {
arrow_model: prepared.arrow_model,
tnts: Vec::new(),
hoppers: HashMap::new(),
item_columns: None,
creative_screen: prepared.creative_screen,
control: None,
frame_stats: Default::default(),
+16 -6
View File
@@ -9,18 +9,28 @@ impl Game<'_> {
pub(super) fn tick_allays(&mut self) {
let griefing = self.ui.rules.bool("mob_griefing");
let player = self.player.as_ref().map(|p| p.position);
// The playing jukeboxes, found at the first allay: no allay starts
// or stops one, and the block entities run into the thousands.
let mut jukeboxes: Option<Vec<(i32, i32, i32)>> = None;
for i in 0..self.mobs.len() {
if self.mobs[i].species() != Species::Allay || !self.mobs[i].alive() {
continue;
}
let pos = self.mobs[i].position;
// Dancing: a playing jukebox within 10 blocks.
let dancing = self.world.block_entities.iter().any(|(k, e)| {
matches!(e, BlockEntity::Jukebox(j) if j.playing)
&& (f64::from(k.0) + 0.5 - pos.x).powi(2)
+ (f64::from(k.1) + 0.5 - pos.y).powi(2)
+ (f64::from(k.2) + 0.5 - pos.z).powi(2)
<= 100.0
let jukeboxes = jukeboxes.get_or_insert_with(|| {
self.world
.block_entities
.iter()
.filter(|(_, e)| matches!(e, BlockEntity::Jukebox(j) if j.playing))
.map(|(k, _)| *k)
.collect()
});
let dancing = jukeboxes.iter().any(|k| {
(f64::from(k.0) + 0.5 - pos.x).powi(2)
+ (f64::from(k.1) + 0.5 - pos.y).powi(2)
+ (f64::from(k.2) + 0.5 - pos.z).powi(2)
<= 100.0
});
self.mobs[i].extra.flag =
dancing && !horizon_client::mobs::goals::should_panic(&self.mobs[i]);
+11 -1
View File
@@ -432,6 +432,10 @@ impl Game<'_> {
(false, false) => (0.5, 1.975),
};
let edge = (width + height + width) / 3.0 * 4.0;
// 26.3 compares the size with RenderSystem.DEFAULT_DEPTH_CLEAR_VALUE,
// which is 0.0 (reverse Z; the bytecode is `dconst_0`): an empty
// marker box falls back to 4, so a visible marker stand draws.
// Holograms stay hidden through the invisible flag instead.
let range = if edge == 0.0 { 4.0 } else { edge } * 64.0;
let dragon = a.equipment[5]
.as_ref()
@@ -700,9 +704,15 @@ impl Game<'_> {
self.dim == super::Dim::Nether,
)),
};
let profile = stack
.extra
.as_deref()
.and_then(|d| d.other.iter().find(|(k, _)| k == "minecraft:profile"))
.map(|(_, t)| horizon_client::scoreboard::tag_to_json(t))
.unwrap_or(serde_json::Value::Null);
self.pack.item_renderer.submit_special(
&special,
&serde_json::Value::Null,
&profile,
false,
pose,
&self.gui.atlas.sprites,
@@ -24,6 +24,7 @@ impl Game<'_> {
let ticking = self.block_ticking_area();
let mut spawners = Vec::new();
let mut trial_spawners = Vec::new();
self.item_columns = Some(Default::default());
for slot in self.world.block_entity_tickers() {
let pos = slot.0;
// Ticks before this one can remove it (`TickingBlockEntity.isRemoved`).
@@ -56,6 +57,8 @@ impl Game<'_> {
_ => {}
}
}
// Items move between passes: the columns hold for this one only.
self.item_columns = None;
self.update_song_volumes();
self.scan_for_hearts();
// The client ticks (particles, sounds, the turning mob) are for a
+27 -5
View File
@@ -390,6 +390,7 @@ impl Game<'_> {
player,
others,
others_grid: None,
others_index: None,
daylight: self.dim != Dim::Nether && self.sky_darken() < 4,
game_time: self.world.game_time,
day_time: self.world.day_time,
@@ -402,7 +403,7 @@ impl Game<'_> {
end: self.dim == Dim::End,
world_seed: self.level.seed,
sea_level: if self.dim == Dim::Nether { 32 } else { 63 },
teams: self.mob_teams(),
teams: std::sync::Arc::new(self.mob_teams()),
items: Some(self.res.items()),
difficulty: self.local_difficulty(
self.player
@@ -499,13 +500,26 @@ impl Game<'_> {
// The level random, lent to the mobs (brain behaviours draw from
// it) and taken back after their ticks.
let mut scratch = self.world.random.clone();
let mut pushes = Vec::new();
let mut pushes = horizon_client::mobs::PendingPushes::default();
let mut keep = Vec::with_capacity(mobs.len());
let mut grid =
horizon_client::mobs::seen_grid::SeenGrid::new(snapshot.iter().map(|s| s.bounds));
// All but the snapshot is the same for every mob of the tick, so
// build it once: the player view and the teams scan every UUID.
let base = self.mob_world_base(&[]);
// Each mob keeps its snapshot slot all tick; the first slot of an
// id answers, as a scan would.
let mut index = horizon_client::fast_hash::FxHashMap::<u32, u32>::default();
for (i, s) in snapshot.iter().enumerate() {
index.entry(s.id).or_insert(i as u32);
}
for i in 0..mobs.len() {
let mut w = self.mob_world_base(&snapshot);
w.others_grid = Some(&grid);
let mut w = MobWorld {
others: &snapshot,
others_grid: Some(&grid),
others_index: Some(&index),
..base.clone()
};
let m = &mut mobs[i];
w.nearest_player = super::natural_spawning::nearest_player(
&players,
@@ -589,7 +603,11 @@ impl Game<'_> {
if let Some(host) = self.lan.as_mut()
&& !guests.is_empty()
{
bindings.retain(|id, _| mobs.iter().any(|m| m.id == *id));
// A set: a scan of every mob for each binding is quadratic, and a
// server's loaded mobs run into the thousands.
let live: horizon_client::fast_hash::FxHashSet<u32> =
mobs.iter().map(|m| m.id).collect();
bindings.retain(|id, _| live.contains(id));
host.mob_players = bindings;
}
self.mobs.append(&mut mobs);
@@ -2929,6 +2947,10 @@ impl Game<'_> {
let scale = f64::from(self.settings.entity_distance);
for mob in &self.mobs {
let entity_start = out.len();
// Entity.DATA_SHARED_FLAGS_ID invisible: only the name tag draws.
if mob.invisible {
continue;
}
let d = glam::DVec3::new(mob.position.x, mob.position.y, mob.position.z) - camera;
// Entity.shouldRenderAtSqrDistance: 64 times the bounding box's
// mean edge, scaled by the entity distance option.
+1 -1
View File
@@ -753,7 +753,7 @@ impl Game<'_> {
let lerp = |a: f64, b: f64| a + f64::from(alpha) * (b - a);
let lerpf = |a: f32, b: f32| a + alpha * (b - a);
for m in &self.mobs {
if m.removed || m.has_effect("minecraft:invisibility") {
if m.removed || m.invisible || m.has_effect("minecraft:invisibility") {
continue;
}
let pos = [
@@ -1736,7 +1736,9 @@ impl Game<'_> {
mob.head_pitch = old.head_pitch;
mob.persistent = old.persistent;
mob.custom_name = old.custom_name.clone();
mob.custom_name_text = old.custom_name_text.clone();
mob.name_visible = old.name_visible;
mob.invisible = old.invisible;
mob.no_ai = old.no_ai;
mob.silent = old.silent;
mob.no_gravity = old.no_gravity;
+4 -3
View File
@@ -6,9 +6,10 @@
//! the world's options) with [`Integrated::call`], as vanilla's client calls
//! into its `IntegratedServer`.
//!
//! On by default. `--no-server-thread` or `HORIZON_SERVER_THREAD=0` runs
//! the world in the frame instead (the escape hatch while the network
//! player's side catches up with the in-frame one).
//! Opt-in again (`--server-thread` or `HORIZON_SERVER_THREAD=1`): it was the
//! default for one day, but a server's players can't board, ride or hit
//! boats and minecarts yet (#1222), and single player's window is one of
//! them here.
use super::{
Game, Options, PackResources, Resources, UiResources,
lan::{LanSettings, Owner},
+18 -1
View File
@@ -9,12 +9,13 @@ use horizon_assets::item_model::{
};
use horizon_client::{
block_entity_render::{diffuse_lights, vertex_light},
hud::HudBatch,
hud::{HudBatch, Rect},
item_render::{self, ItemRenderState, SpecialHost, WorldSink},
items::ItemStack,
mesher::Vertex,
};
use serde_json::Value;
use std::{cell::RefCell, collections::HashMap};
/// The game's answers to what the resolver cannot read from the stack.
pub(super) struct Env<'a, 'g> {
@@ -126,6 +127,8 @@ pub(super) fn local_time(pattern: &str) -> String {
/// Banner and shield pattern textures from the banner pattern registry.
pub(super) struct Host<'a> {
banners: &'a horizon_client::banner::Banners,
sprites: &'a HashMap<String, Rect>,
skins: &'a RefCell<horizon_client::skins::Downloader>,
}
impl SpecialHost for Host<'_> {
fn banner_texture(&self, pattern: &str) -> Option<String> {
@@ -138,6 +141,18 @@ impl SpecialHost for Host<'_> {
.pattern(pattern)
.map(|_| self.banners.shield_texture(pattern))
}
fn player_skin(&self, profile: &Value) -> Option<String> {
// SkullBlockRenderer.getPlayerSkin: SkinManager.getInsecureSkinInformation
// accepts a texture property without a signature, unlike the tab list.
let url = horizon_client::skins::profile_skin_url(profile)?;
let location =
horizon_client::skins::skin_location(&horizon_client::skins::texture_hash(&url)?);
let _ = self
.skins
.borrow_mut()
.state(horizon_client::skins::Kind::Skin, &url);
self.sprites.contains_key(&location).then_some(location)
}
}
/// `ItemOwner` of an entity holding or showing an item.
@@ -177,6 +192,8 @@ impl Game<'_> {
pub(super) fn item_host(&self) -> Host<'_> {
Host {
banners: &self.res.stations().banners,
sprites: &self.gui.atlas.sprites,
skins: &self.skin_downloads,
}
}
/// The client level as item models read it.
@@ -1730,6 +1730,13 @@ impl Game<'_> {
break;
}
}
// A ServerPlayer travels on the server too
// (MoveSimulationType.AUTHORITATIVE_SIDE_AND_SERVER), so every tick
// on a ladder or vine LivingEntity.handleOnClimbable resets its fall
// distance: climbing down doesn't hurt where it lands (#1255).
if w.climbable().unwrap_or(false) {
w.player.fall_distance = 0.0;
}
let _ = w.tick_environment_damage();
let _ = w.apply_fire_contact();
let _ = w.server_hazards_tick(in_wall);
+164
View File
@@ -327,6 +327,46 @@ impl Containers<'_> {
}
}
/// The block columns any item entity's box reaches, so that a hopper with
/// no item over it skips the scan of every item. Kept for one block-entity
/// pass, in which items do not move: removing one leaves a superset, still
/// right, and adding one (always pushed at the end, under a new id) or
/// removing the last rebuilds the columns.
#[derive(Default)]
pub(super) struct ItemColumns {
/// The item count and last item id when last asked, if built.
seen: Option<(usize, Option<u32>)>,
cells: horizon_client::fast_hash::FxHashSet<(i32, i32)>,
}
impl ItemColumns {
/// Whether an item's box may reach into column (x, z).
fn may_reach(
&mut self,
items: &[horizon_client::entities::ItemEntity],
x: i32,
z: i32,
) -> bool {
let (len, last) = (items.len(), items.last().map(|i| i.id));
let stale = self
.seen
.is_none_or(|(seen_len, seen_last)| len > seen_len || last != seen_last);
self.seen = Some((len, last));
if stale {
self.cells.clear();
for item in items {
let b = item.bounds();
let (lo, hi) = (b.min_position(), b.max_position());
for cx in lo.x.floor() as i32..=hi.x.floor() as i32 {
for cz in lo.z.floor() as i32..=hi.z.floor() as i32 {
self.cells.insert((cx, cz));
}
}
}
}
self.cells.contains(&(x, z))
}
}
fn bone_meal() -> ItemStack {
ItemStack {
item: "minecraft:bone_meal".into(),
@@ -648,6 +688,11 @@ impl Game<'_> {
f64::from(pos.2) + 1.0,
],
);
if let Some(columns) = self.item_columns.as_mut()
&& !columns.may_reach(&self.dropped, pos.0, pos.2)
{
return false;
}
let mut i = 0;
while i < self.dropped.len() {
let b = self.dropped[i].bounds();
@@ -880,3 +925,122 @@ impl Game<'_> {
true
}
}
#[cfg(test)]
mod item_column_tests {
use super::ItemColumns;
use horizon_client::{entities::ItemEntity, items::ItemStack};
use horizon_core::geometry::Vec3;
fn item(id: u32, x: f64, y: f64, z: f64) -> ItemEntity {
let at = Vec3::new(x, y, z);
ItemEntity {
health: 5,
fire_ticks: 0,
id,
stack: ItemStack::new("minecraft:stone", 1),
thrower: None,
position: at,
previous: at,
velocity: Vec3::ZERO,
on_ground: true,
age: 0,
pickup_delay: 0,
bob_offset: 0.0,
ticks: 0,
}
}
/// `hopper_suck`'s test: some item box meets the hopper's SUCK_AABB.
fn scan(items: &[ItemEntity], (x, y, z): (i32, i32, i32)) -> bool {
let (min, max) = (
Vec3::new(f64::from(x), f64::from(y) + 11.0 / 16.0, f64::from(z)),
Vec3::new(f64::from(x) + 1.0, f64::from(y) + 2.0, f64::from(z) + 1.0),
);
items.iter().any(|i| {
let b = i.bounds();
let (lo, hi) = (b.min_position(), b.max_position());
lo.x < max.x
&& hi.x > min.x
&& lo.y < max.y
&& hi.y > min.y
&& lo.z < max.z
&& hi.z > min.z
})
}
/// One tick of empty hoppers looking for items: each scans every item
/// (before) or asks the columns first (after, build included).
#[test]
#[ignore]
fn hopper_suck_cost() {
for (hoppers, count) in [(500i32, 200u32), (2000, 500), (2000, 2000)] {
let mut seed = 99u64;
let mut next = |range: f64| {
seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1);
(seed >> 11) as f64 / (1u64 << 53) as f64 * range
};
let items: Vec<ItemEntity> = (0..count)
.map(|id| item(id, next(160.0), 64.0, next(160.0)))
.collect();
let at: Vec<(i32, i32, i32)> = (0..hoppers)
.map(|_| (next(160.0) as i32, 63, next(160.0) as i32))
.collect();
let t = std::time::Instant::now();
let a: Vec<bool> = at.iter().map(|&h| scan(&items, h)).collect();
let before = t.elapsed().as_secs_f64() * 1e3;
let t = std::time::Instant::now();
let mut columns = ItemColumns::default();
let b: Vec<bool> = at
.iter()
.map(|&h| columns.may_reach(&items, h.0, h.2) && scan(&items, h))
.collect();
let after = t.elapsed().as_secs_f64() * 1e3;
assert_eq!(a, b);
println!("{hoppers} hoppers, {count} items: {before:.3} ms -> {after:.3} ms");
}
}
#[test]
fn no_hopper_with_an_item_over_it_is_skipped() {
let mut seed = 0x1234_5678_9abc_def1u64;
let mut next = |range: f64| {
seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1);
(seed >> 11) as f64 / (1u64 << 53) as f64 * range
};
let mut items: Vec<ItemEntity> = Vec::new();
let mut columns = ItemColumns::default();
let mut next_id = 0;
let mut skipped = 0;
for round in 0..3000 {
// Items come and go as hoppers take them and crafters drop them.
match round % 5 {
0 | 1 => {
next_id += 1;
// On edges too: a quarter of them sit on a block line.
let snap = |v: f64, edge: bool| if edge { v.floor() + 0.125 } else { v };
let edge = round % 4 == 0;
items.push(item(
next_id,
snap(next(24.0) - 12.0, edge),
64.0,
snap(next(24.0) - 12.0, edge),
));
}
2 if !items.is_empty() => {
let i = next(items.len() as f64) as usize;
items.remove(i);
}
_ => {}
}
let hopper = (next(26.0) as i32 - 13, 63, next(26.0) as i32 - 13);
let reach = columns.may_reach(&items, hopper.0, hopper.2);
let found = scan(&items, hopper);
assert!(
reach || !found,
"round {round}: skipped a hopper with an item over it"
);
skipped += usize::from(!reach);
}
assert!(skipped > 1000, "skipped only {skipped}");
}
}
+31
View File
@@ -275,6 +275,37 @@ impl Game<'_> {
}
self.maps.data.get_mut(&id)
}
/// `ClientboundMapItemDataPacket.handle`: the server's colours for a map
/// this client does not own (lobby map art, a framed map on a wall).
pub(super) fn remote_map_item_data(
&mut self,
p: &horizon_protocol::packets::play::MapItemData,
) {
let dimension = self.dimension_id();
let map = self
.maps
.data
.entry(p.map_id)
.or_insert_with(|| MapData::fresh(0.0, 0.0, p.scale.max(0) as u8, dimension));
map.locked = p.locked;
let Some(patch) = &p.patch else {
map.dirty = false;
return;
};
for x in 0..patch.width {
for y in 0..patch.height {
let Some(&color) = patch.colors.get((x + y * patch.width) as usize) else {
continue;
};
let (px, py) = (patch.start_x + x, patch.start_y + y);
if (0..128).contains(&px) && (0..128).contains(&py) {
map.update_color(px, py, color);
}
}
}
// The server owns a remote map: only the texture has to change.
map.dirty = false;
}
fn next_map_id(&mut self) -> i32 {
let last = *self.maps.last_id.get_or_insert_with(|| {
horizon_client::save::read_dat(&self.world_root.join("data/minecraft/maps/last_id.dat"))
+68 -3
View File
@@ -124,7 +124,7 @@ impl Game<'_> {
team,
viewer,
self.spectator(),
m.has_effect("minecraft:invisibility"),
m.invisible || m.has_effect("minecraft:invisibility"),
);
// Entity.isDiscrete: a crouching player.
let discrete = player && m.extra.crouching;
@@ -144,9 +144,16 @@ impl Game<'_> {
continue;
}
// getDisplayName: the name in the team's prefix, suffix and colour.
let member = if player {
Text::literal(&name)
} else {
m.custom_name_text
.clone()
.unwrap_or_else(|| Text::literal(&name))
};
let shown = match team {
Some(t) => t.formatted_name(Text::literal(&name)),
None => Text::literal(&name),
Some(t) => t.formatted_name(member),
None => member,
};
let mut lines = Vec::new();
// EntityRenderer.extractNameTags: the score shows within the
@@ -171,6 +178,64 @@ impl Game<'_> {
discrete,
});
}
// Armor stands name themselves: an invisible one with a visible name
// is a hologram (its model is skipped, its name tag is not).
for a in &self.armor_stands {
let Some(name) = &a.custom_name else {
continue;
};
if !a.name_visible {
continue;
}
let height = match (a.marker, a.small) {
(true, _) => 0.0,
(false, true) => 0.9875,
(false, false) => 1.975,
};
let distance_sqr =
glam::DVec3::new(a.position.x, a.position.y, a.position.z).distance_squared(camera);
if distance_sqr >= 64.0 * 64.0 {
continue;
}
let team = self.mob_team(a.id);
let invisible = sb::invisible_to(team, viewer, self.spectator(), false);
let view = sb::NameView {
discrete: false,
distance_sqr,
invisible_to_viewer: invisible,
hud_hidden: self.ui.hide_gui,
is_camera: false,
is_vehicle: false,
};
if !sb::name_shown(team, viewer, view) {
continue;
}
let shown = match team {
Some(t) => t.formatted_name(name.clone()),
None => name.clone(),
};
let mut lines = Vec::new();
let mut lift = 0.0;
let holder = self.persist.uuids.get(&a.id).map(|u| uuid_text(*u));
if distance_sqr < 10.0 * 10.0
&& let Some(score) = holder.and_then(|h| self.below_name_text(&h))
{
lines.push((score, 0.0));
lift = f64::from(9.0f32 * 1.15 * 0.025);
}
lines.push((shown, lift));
let l = self.world.light(
a.position.x.floor() as i32,
(a.position.y + height).floor() as i32,
a.position.z.floor() as i32,
);
tags.push(NameTag {
at: glam::DVec3::new(a.position.x, a.position.y + height + 0.5, a.position.z),
lines,
light: l,
discrete: false,
});
}
tags
}
/// `SubmitNodeCollection.submitNameTag`: a discrete name is drawn once,
@@ -9,6 +9,7 @@ use horizon_client::{
mobs::{
Mob, MobWorld, Species,
data::{self, MobData},
seen_grid::SeenGrid,
spawn_rules::{Dimension, SpawnLevel},
spawn_selection::{
self, Category, Counted, Heightmap, NaturalLevel, SpawnCost, SpawnPlayer, SpawnRequest,
@@ -75,8 +76,72 @@ pub(super) struct GameLevel<'g, 'a> {
pub(super) pending: std::cell::RefCell<Vec<Aabb>>,
/// The types and boxes of what a spawner created this tick.
pub(super) pending_ids: std::cell::RefCell<Vec<(String, Aabb)>>,
/// The game's mobs by box, built once a pass has asked more than
/// [`SCANNED_QUERIES`] box queries (the mobs do not change while it
/// holds the game); the first ones scan, as the build costs a few scans.
pub(super) boxes: std::cell::OnceCell<MobBoxes>,
pub(super) box_queries: std::cell::Cell<u32>,
}
const SCANNED_QUERIES: u32 = 8;
/// A mob not removed: its index in `Game::mobs`, box, alive, and whether
/// it is a living shulker.
type MobBox = (usize, Aabb, bool, bool);
fn mob_box(i: usize, m: &Mob) -> MobBox {
let alive = m.alive();
(
i,
m.bounds(),
alive,
alive && m.species() == Species::Shulker,
)
}
/// The boxes of the game's mobs not removed, and a grid over them.
pub(super) struct MobBoxes {
entries: Vec<MobBox>,
grid: SeenGrid,
}
impl MobBoxes {
fn new(mobs: &[Mob]) -> Self {
let entries: Vec<_> = mobs
.iter()
.enumerate()
.filter(|(_, m)| !m.removed)
.map(|(i, m)| mob_box(i, m))
.collect();
let grid = SeenGrid::new(entries.iter().map(|e| e.1));
Self { entries, grid }
}
/// The entries whose boxes meet `area` (and possibly a few more).
fn near(&self, area: Aabb) -> impl Iterator<Item = MobBox> + '_ {
let mut near = Vec::new();
let indexed = self.grid.candidates(area, &mut near);
let all = (!indexed).then(|| self.entries.iter());
near.into_iter()
.map(|i| self.entries[i as usize])
.chain(all.into_iter().flatten().copied())
}
}
impl GameLevel<'_, '_> {
/// The mobs not removed whose boxes may meet `area` (callers test them).
fn mobs_near(&self, area: Aabb) -> Box<dyn Iterator<Item = MobBox> + '_> {
let asked = self.box_queries.get() + 1;
self.box_queries.set(asked);
if asked <= SCANNED_QUERIES && self.boxes.get().is_none() {
return Box::new(
self.game
.mobs
.iter()
.enumerate()
.filter(|(_, m)| !m.removed)
.map(|(i, m)| mob_box(i, m)),
);
}
Box::new(
self.boxes
.get_or_init(|| MobBoxes::new(&self.game.mobs))
.near(area),
)
}
fn biome(&self, x: i32, y: i32, z: i32) -> Option<&BiomeInfo> {
let chunk = self.game.world.chunks.get(&(x >> 4, z >> 4))?;
Some(self.game.res.biomes().get(chunk.biome(x, y, z)))
@@ -224,12 +289,9 @@ impl NaturalLevel for GameLevel<'_, '_> {
// getEntityCollisions(null, box): living shulkers are solid.
let grown = bounds.inflate(Vec3::new(1e-7, 1e-7, 1e-7));
no_collision_empty(&mut scene, bounds)
&& !self.game.mobs.iter().any(|m| {
m.species() == Species::Shulker
&& m.alive()
&& !m.removed
&& m.bounds().intersects(grown)
})
&& !self
.mobs_near(grown)
.any(|(_, b, _, shulker)| shulker && b.intersects(grown))
&& !self
.pending_ids
.borrow()
@@ -244,10 +306,8 @@ impl NaturalLevel for GameLevel<'_, '_> {
!player
&& !self.pending.borrow().iter().any(|b| b.intersects(bounds))
&& !self
.game
.mobs
.iter()
.any(|m| m.alive() && !m.removed && m.bounds().intersects(bounds))
.mobs_near(bounds)
.any(|(_, b, alive, _)| alive && b.intersects(bounds))
}
fn spawn_mobs(&self) -> bool {
self.game.ui.rules.bool("spawn_mobs")
@@ -289,10 +349,8 @@ impl horizon_client::mobs::spawner::SpawnerLevel for GameLevel<'_, '_> {
}
fn count_entities(&self, id: &str, bounds: Aabb) -> usize {
let mobs = self
.game
.mobs
.iter()
.filter(|m| !m.removed && m.kind.id == id && m.bounds().intersects(bounds))
.mobs_near(bounds)
.filter(|&(i, b, ..)| self.game.mobs[i].kind.id == id && b.intersects(bounds))
.count();
let pending = self
.pending_ids
@@ -311,6 +369,8 @@ impl<'a> Game<'a> {
players: self.level_players(),
pending: Default::default(),
pending_ids: Default::default(),
boxes: Default::default(),
box_queries: Default::default(),
}
}
/// Creates a mob the spawner accepted, finalized the game's way; the
+13 -1
View File
@@ -34,6 +34,9 @@ impl Game<'_> {
if !self.ui.rules.bool("mob_griefing") {
return;
}
// Whether each item is piglin-loved, kept until an item is added or
// removed (added ones are pushed at the end under a new id).
let mut loved: (Option<(usize, Option<u32>)>, Vec<bool>) = (None, Vec::new());
for i in 0..self.mobs.len() {
let m = &self.mobs[i];
if m.species() != Species::Piglin
@@ -46,11 +49,20 @@ impl Game<'_> {
continue;
}
let pos = m.position;
let key = (self.dropped.len(), self.dropped.last().map(|e| e.id));
if loved.0 != Some(key) {
loved.0 = Some(key);
loved.1 = self
.dropped
.iter()
.map(|e| self.piglin_loved(&e.stack.item))
.collect();
}
let best = self
.dropped
.iter()
.enumerate()
.filter(|(_, e)| self.piglin_loved(&e.stack.item))
.filter(|&(j, _)| loved.1[j])
.map(|(j, e)| (j, e.position.distance_squared(pos)))
.filter(|(_, d)| *d <= 81.0)
.min_by(|a, b| a.1.total_cmp(&b.1));
+230 -81
View File
@@ -140,6 +140,8 @@ pub(super) struct Remote {
tab_players: HashMap<u128, horizon_client::tab_list::TabPlayer>,
/// Each listed profile's unpacked `textures` property.
skin_textures: HashMap<u128, horizon_client::skins::Textures>,
/// The skin texture each profile's players show, once its download is ready.
skins: HashMap<u128, String>,
listed: std::collections::HashSet<u128>,
/// `ClientboundLoginPacket.onlineMode`: the player list shows heads.
online_mode: bool,
@@ -194,6 +196,30 @@ fn face_direction(face: [i32; 3]) -> u8 {
}
}
/// A `minecraft:profile` component as the item data JSON the model reads.
fn profile_tag(p: &horizon_protocol::item::Profile) -> Tag {
let properties = p
.properties
.iter()
.map(|(name, value, signature)| {
let mut entries: Vec<(&'static str, Tag)> = vec![
("name", Tag::String(name.as_str().into())),
("value", Tag::String(value.as_str().into())),
];
if let Some(signature) = signature {
entries.push(("signature", Tag::String(signature.as_str().into())));
}
Tag::compound(entries)
})
.collect::<Vec<_>>();
let mut entries: Vec<(&'static str, Tag)> = Vec::new();
if let Some(name) = &p.name {
entries.push(("name", Tag::String(name.as_str().into())));
}
entries.push(("properties", Tag::List(properties)));
Tag::compound(entries)
}
/// A protocol number format as the scoreboard's.
fn number_format(f: &play::NumberFormat) -> horizon_client::scoreboard::NumberFormat {
use horizon_client::scoreboard::{NumberFormat, tag_to_json};
@@ -355,6 +381,7 @@ impl Game<'_> {
players: HashMap::new(),
tab_players: HashMap::new(),
skin_textures: HashMap::new(),
skins: HashMap::new(),
listed: Default::default(),
online_mode: false,
level_players: HashMap::new(),
@@ -515,66 +542,118 @@ impl Game<'_> {
let mut out = ItemStack {
item: self.item_name(s.item),
count: s.count.max(0) as u32,
damage: s.damage().max(0) as u32,
damage: 0,
extra: None,
};
use horizon_protocol::item::{Component, component};
if let Some(Component::Int(rgb)) = s.component(component::DYED_COLOR) {
out.data_mut().dyed_color = Some(*rgb);
}
if let Some(Component::Int(id)) = s.component(component::MAP_ID) {
out.data_mut().map_id = Some(*id);
}
if let Some(Component::Int(cost)) = s.component(component::REPAIR_COST) {
out.data_mut().repair_cost = (*cost).max(0) as u32;
}
if let Some(Component::Int(mode)) = s.component(component::MAP_POST_PROCESSING) {
out.data_mut().map_post_processing = Some((*mode).clamp(0, 1) as u8);
}
if let Some(Component::WritableBook(pages)) = s.component(component::WRITABLE_BOOK_CONTENT)
{
out.data_mut().writable_pages = pages.clone();
}
if let Some(Component::WrittenBook(b)) = s.component(component::WRITTEN_BOOK_CONTENT) {
let lang = self
.remote
.as_ref()
.map(|r| r.lang.clone())
.unwrap_or_default();
out.data_mut().written = Some(horizon_client::item_data::WrittenBook {
title: b.title.clone(),
author: b.author.clone(),
generation: b.generation.clamp(0, 3) as u8,
pages: b.pages.iter().map(|p| plain(p, &lang)).collect(),
});
}
if let Some(Component::Text(name)) = s.component(component::CUSTOM_NAME) {
let lang = self
.remote
.as_ref()
.map(|r| r.lang.clone())
.unwrap_or_default();
out.data_mut().custom_name = Some(plain(name, &lang));
}
// The enchantments by the server's registry ids.
let levels = |list: &[(i32, i32)]| -> Vec<(Arc<str>, u32)> {
let names = self.remote.as_ref().map(|r| &r.registries);
list.iter()
.filter_map(|(id, level)| {
let name = names?.name("minecraft:enchantment", *id)?;
Some((Arc::from(name), (*level).max(0) as u32))
})
.collect()
};
if let Some(Component::Enchantments(list)) = s.component(component::ENCHANTMENTS) {
out.data_mut().enchantments = levels(list);
}
if let Some(Component::Enchantments(list)) = s.component(component::STORED_ENCHANTMENTS) {
out.data_mut().stored_enchantments = levels(list);
// A vanilla `components` compound, then the same decoder the saved
// and command item NBT use, so tooltips and models see every field.
let components = self.protocol_components(s);
let (damage, data) = horizon_client::item_data::decode_components(components.as_ref());
out.damage = damage;
if let Some(data) = data {
*out.data_mut() = *data;
}
out.tidy();
Some(out)
}
/// The item's patched components as vanilla's `components` compound.
fn protocol_components(&self, s: &Stack) -> Option<Tag> {
use horizon_protocol::item::{Component, component};
let r = self.remote.as_ref()?;
let name = |id: i32| r.builtin.get("minecraft:data_component_type")?.name(id);
let mut entries: Vec<(&str, Tag)> = Vec::new();
for (kind, c) in &s.added {
let Some(key) = name(*kind) else {
continue;
};
let value = match c {
Component::Int(v) if *kind == component::RARITY => {
let rarity = ["common", "uncommon", "rare", "epic"];
Tag::String(rarity.get(*v as usize).copied().unwrap_or("common").into())
}
Component::Int(v) => Tag::Int(*v),
Component::Unit => Tag::Compound(Default::default()),
Component::Bool(v) => Tag::Byte(i8::from(*v)),
Component::Text(t) | Component::Nbt(t) => t.clone(),
Component::Ident(v) => Tag::String(v.as_str().into()),
Component::Lore(lines) => Tag::List(lines.clone()),
Component::WritableBook(pages) => Tag::compound([(
"pages",
Tag::List(
pages
.iter()
.map(|p| Tag::String(p.as_str().into()))
.collect(),
),
)]),
Component::WrittenBook(b) => Tag::compound([
("title", Tag::String(b.title.as_str().into())),
("author", Tag::String(b.author.as_str().into())),
("generation", Tag::Int(b.generation)),
("pages", Tag::List(b.pages.clone())),
("resolved", Tag::Byte(i8::from(b.resolved))),
]),
Component::Enchantments(list) => {
// Enchantments are a synced (data-driven) registry: the
// ids index the server's RegistryData, not the builtin
// report, so a datapack enchantment names correctly.
let levels = list.iter().filter_map(|(id, level)| {
let name = r.registries.name("minecraft:enchantment", *id)?;
Some((name, Tag::Int(*level)))
});
Tag::compound(levels)
}
Component::AttackRange(v) => Tag::compound([
("min_reach", Tag::Float(v[0].to_bits())),
("max_reach", Tag::Float(v[1].to_bits())),
("min_attack_momentum", Tag::Float(v[2].to_bits())),
("max_attack_momentum", Tag::Float(v[3].to_bits())),
("hitbox_margin", Tag::Float(v[4].to_bits())),
("mob_factor", Tag::Float(v[5].to_bits())),
]),
Component::Profile(p) => profile_tag(p),
Component::CustomModelData {
floats,
flags,
strings,
colors,
} => Tag::compound([
(
"floats",
Tag::List(floats.iter().map(|f| Tag::Float(f.to_bits())).collect()),
),
(
"flags",
Tag::List(flags.iter().map(|b| Tag::Byte(i8::from(*b))).collect()),
),
(
"strings",
Tag::List(
strings
.iter()
.map(|s| Tag::String(s.as_str().into()))
.collect(),
),
),
(
"colors",
Tag::List(colors.iter().map(|c| Tag::Int(*c)).collect()),
),
]),
Component::TooltipDisplay { hide, hidden } => {
let hidden = hidden
.iter()
.filter_map(|id| Some(Tag::String(name(*id)?.into())));
Tag::compound([
("hide_tooltip", Tag::Byte(i8::from(*hide))),
("hidden_components", Tag::List(hidden.collect())),
])
}
};
entries.push((key, value));
}
Some(Tag::compound(entries))
}
fn to_network(&self, s: &Option<ItemStack>) -> Option<Stack> {
let s = s.as_ref()?;
let mut stack = Stack::new(self.item_id(&s.item), s.count as i32);
@@ -1677,6 +1756,29 @@ impl Game<'_> {
}
}
C::SetEntityData(p) => {
// Entity.DATA_SHARED_FLAGS_ID, DATA_CUSTOM_NAME and
// DATA_CUSTOM_NAME_VISIBLE: server NPCs and holograms rely on
// these instead of saved NBT.
if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) {
for (index, _, value) in &p.values {
match (index, value) {
(0, Value::Byte(flags)) => m.invisible = flags & 0x20 != 0,
(2, Value::OptText(name)) => {
let text = name.as_ref().map(horizon_client::entity_nbt::name_text);
m.custom_name =
text.as_ref().map(horizon_client::text::Text::plain);
m.custom_name_text = text;
}
(2, Value::Text(name)) => {
let text = horizon_client::entity_nbt::name_text(name);
m.custom_name = Some(text.plain());
m.custom_name_text = Some(text);
}
(3, Value::Bool(visible)) => m.name_visible = *visible,
_ => {}
}
}
}
if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32)
&& m.species() == horizon_client::mobs::Species::Rabbit
{
@@ -2314,6 +2416,7 @@ impl Game<'_> {
.then(|| Vec3::new(f64::from(x) + 0.5, f64::from(y), f64::from(z) + 0.5));
}
C::ServerData(p) => self.remote_server_data(p.icon.as_deref()),
C::MapItemData(p) => self.remote_map_item_data(&p),
// handleTickingState / handleTickingStep: the level's TickRateManager.
C::TickingState(p) => {
self.clock.set_rate(p.rate);
@@ -2459,33 +2562,20 @@ impl Game<'_> {
pub(super) fn remote_tab_players(
&mut self,
) -> Option<(Vec<horizon_client::tab_list::TabPlayer>, bool, Vec<u128>)> {
let r = self.remote.as_ref()?;
let mut players: Vec<_> = r
.listed
.iter()
.filter_map(|u| r.tab_players.get(u).cloned())
.collect();
self.update_remote_skins();
let mut players: Vec<_> = {
let r = self.remote.as_ref()?;
r.listed
.iter()
.filter_map(|u| r.tab_players.get(u).cloned())
.collect()
};
for p in &mut players {
let Some(textures) = r.skin_textures.get(&p.uuid) else {
continue;
};
// PlayerInfo.createSkinLookup: only the local player's own skin
// may be unsigned.
let downloads = &mut self.skin_downloads;
let skin = horizon_client::skins::lookup(textures, p.uuid != r.profile, |kind, url| {
downloads.state(kind, url)
});
if let Some(skin) = skin {
p.skin = skin;
}
}
for (location, rgba) in self.skin_downloads.take_ready() {
self.store_skin(location, rgba);
}
for p in &mut players {
if p.skin.starts_with("skins/") && !self.show_skin(&p.skin) {
p.skin = horizon_client::tab_list::default_skin(p.uuid).to_owned();
}
p.skin = self
.remote
.as_ref()
.and_then(|r| r.skins.get(&p.uuid).cloned())
.unwrap_or_else(|| horizon_client::tab_list::default_skin(p.uuid).to_owned());
}
let r = self.remote.as_ref()?;
let upside_down = players
@@ -2498,9 +2588,68 @@ impl Game<'_> {
.collect();
Some((players, r.online_mode, upside_down))
}
/// `PlayerInfo.getSkin` for every player entity, including NPCs that are
/// never in the tab list: resolve each profile's textures and give the
/// level's player models their skin.
fn update_remote_skins(&mut self) {
let Some(r) = self.remote.as_ref() else {
return;
};
let local = r.profile;
let entries: Vec<(u128, horizon_client::skins::Textures)> = r
.skin_textures
.iter()
.map(|(uuid, textures)| (*uuid, textures.clone()))
.collect();
let mut resolved: Vec<(u128, String)> = Vec::new();
for (uuid, textures) in entries {
// PlayerInfo.createSkinLookup: only the local player's own skin
// may be unsigned.
let skin = {
let mut downloads = self.skin_downloads.borrow_mut();
horizon_client::skins::lookup(&textures, uuid != local, |kind, url| {
downloads.state(kind, url)
})
};
if let Some(skin) = skin {
resolved.push((uuid, skin));
}
}
resolved.retain(|(_, skin)| !skin.starts_with("skins/") || self.show_skin(skin));
let Some((level, skins, own)) = self.remote.as_mut().map(|r| {
r.skins = resolved.into_iter().collect();
(
r.level_players
.iter()
.map(|(id, uuid)| (*id as u32, *uuid))
.collect::<Vec<_>>(),
r.skins.clone(),
r.skins.get(&r.profile).cloned(),
)
}) else {
return;
};
for (id, uuid) in level {
let Some(skin) = skins.get(&uuid) else {
continue;
};
if let Some(mob) = self
.mobs
.iter_mut()
.find(|m| m.id == id && &*m.kind.id == "minecraft:player")
{
mob.skin = Some(skin.clone());
}
}
if let Some(skin) = own
&& let Some(mob) = self.player_model.as_mut()
{
mob.skin = Some(skin);
}
}
/// Puts a downloaded skin in an atlas skin slot: a free one, else the
/// least recently shown.
fn store_skin(&mut self, location: String, rgba: Vec<u8>) {
pub(super) fn store_skin(&mut self, location: String, rgba: Vec<u8>) {
use horizon_client::hud::SKIN_SLOTS;
let slot = if self.skin_slots.len() < SKIN_SLOTS {
self.skin_slots.len()
@@ -123,6 +123,14 @@ impl Game<'_> {
} else if let Some(a) = self.armor_stands.iter_mut().find(|a| a.id == id) {
match (index, value) {
(0, Value::Byte(flags)) => a.invisible = flags & 0x20 != 0,
// Entity.DATA_CUSTOM_NAME and DATA_CUSTOM_NAME_VISIBLE.
(2, Value::OptText(name)) => {
a.custom_name = name.as_ref().map(horizon_client::entity_nbt::name_text);
}
(2, Value::Text(name)) => {
a.custom_name = Some(horizon_client::entity_nbt::name_text(name));
}
(3, Value::Bool(visible)) => a.name_visible = *visible,
(9, Value::Float(health)) => a.health = *health,
// ArmorStand.DATA_CLIENT_FLAGS.
(15, Value::Byte(flags)) => {
+8 -5
View File
@@ -431,20 +431,18 @@ impl Game<'_> {
self.score.board.players_team(self.acting_player_name())
}
/// The teams of the player and of the mobs on one (empty without teams).
pub(super) fn mob_teams(&self) -> horizon_client::mobs::Teams {
pub(super) fn mob_teams(&self) -> horizon_client::mobs::Teams<'_> {
let board = &self.score.board;
if board.teams.is_empty() {
return Default::default();
}
horizon_client::mobs::Teams {
player: self.player_team().cloned(),
player: self.player_team(),
mobs: self
.persist
.uuids
.iter()
.filter_map(|(id, uuid)| {
Some((*id, board.players_team(&uuid_text(*uuid))?.clone()))
})
.filter_map(|(id, uuid)| Some((*id, board.players_team(&uuid_text(*uuid))?)))
.collect(),
}
}
@@ -599,6 +597,11 @@ impl Game<'_> {
/// objective.
pub(super) fn draw_tab_list(&mut self, batch: &mut HudBatch) {
use horizon_client::tab_list::{self, TabInputs};
// Player heads and the player list both draw from the same skin slots.
let ready: Vec<_> = self.skin_downloads.borrow_mut().take_ready();
for (location, rgba) in ready {
self.store_skin(location, rgba);
}
let key = self.screen == super::Screen::Playing
&& self.keys.contains(&winit::keyboard::KeyCode::Tab);
let (players, online_mode, upside_down) = match self.remote_tab_players() {
+6 -1
View File
@@ -71,6 +71,8 @@ pub(super) struct SculkState {
/// The sculk block entities in the order they were added.
index: Index,
pub known: HashSet<(i32, i32)>,
/// The chunk map's membership revision `known` was taken at.
pub known_revision: Option<u64>,
// Keyed by mob ids the server gives out: the fast hasher is safe.
steps: FxHashMap<Who, Steps>,
damaged: FxHashMap<u32, i64>,
@@ -336,7 +338,10 @@ impl Game<'_> {
/// the block writes recorded since the last tick.
pub(super) fn sculk_sync_index(&mut self) {
// Chunks that arrived or left (every half second is plenty).
if self.sculk.ticks.is_multiple_of(10) {
// Nothing to do while no chunk came or went.
let revision = self.world.chunks.membership_revision();
if self.sculk.ticks.is_multiple_of(10) && self.sculk.known_revision != Some(revision) {
self.sculk.known_revision = Some(revision);
let loaded: HashSet<(i32, i32)> = self.world.chunks.keys().copied().collect();
let arrived: Vec<(i32, i32)> = loaded.difference(&self.sculk.known).copied().collect();
for key in arrived {
@@ -433,6 +433,7 @@ impl Game<'_> {
});
self.level_events.clear();
self.sculk.known = self.world.chunks.keys().copied().collect();
self.sculk.known_revision = Some(self.world.chunks.membership_revision());
self.sculk_sync_index();
let mut names: HashMap<u32, String> = HashMap::new();
let mut sources: Vec<u32> = Vec::new();
@@ -194,7 +194,9 @@ fn carry_over(from: &Mob, to: &mut Mob) {
to.fire_ticks = from.fire_ticks;
to.persistent = from.persistent;
to.custom_name = from.custom_name.clone();
to.custom_name_text = from.custom_name_text.clone();
to.name_visible = from.name_visible;
to.invisible = from.invisible;
to.extra.pick_up_loot = from.extra.pick_up_loot;
to.extra.main_hand = from.extra.main_hand.clone();
to.extra.off_hand = from.extra.off_hand.clone();
+4 -2
View File
@@ -30,8 +30,10 @@ pub struct HudAtlas {
}
/// Atlas rows reserved for glyphs rasterized at run time.
pub const GLYPH_ROWS: u32 = 512;
/// 128x128 atlas slots for map textures.
pub const MAP_SLOTS: usize = 16;
/// 128x128 atlas slots for map textures. A lobby paints its signs as walls of
/// framed maps, so the pool holds a wall; too few slots re-upload every map
/// every frame. 26.3's `MapTextureManager` keeps a texture per map instead.
pub const MAP_SLOTS: usize = 256;
/// The atlas slot the minimap extra draws into.
pub const MINIMAP_SPRITE: &str = "horizon/minimap";
/// Atlas slots for downloaded player skins (`skins/<sha1>`), 64x64 each.
+24 -1
View File
@@ -302,7 +302,10 @@ fn filterable_text(t: &Tag) -> Option<String> {
fn read_text(t: &Tag) -> Option<String> {
match t {
Tag::String(s) => Some(s.to_string_lossy()),
Tag::Compound(_) => t.get("text").and_then(string),
// A component's root `text` may be empty with the visible words in
// `extra` (every legacy display name ViaVersion produces looks like
// this), so flatten the whole component instead of reading one key.
Tag::Compound(_) => Some(crate::entity_nbt::name_text(t).plain()),
_ => None,
}
}
@@ -860,6 +863,26 @@ mod tests {
assert_eq!(back.custom_name(), Some("Blade"));
}
#[test]
fn custom_name_reads_the_extra_spans() {
let name = Tag::compound([
(
"extra",
Tag::List(vec![
Tag::compound([("text", Tag::String("Games ".into()))]),
Tag::compound([("text", Tag::String("Right click".into()))]),
]),
),
("text", Tag::String("".into())),
]);
let components = Tag::compound([("minecraft:custom_name", name)]);
let (_, extra) = decode_components(Some(&components));
assert_eq!(
extra.and_then(|d| d.custom_name.clone()).as_deref(),
Some("Games Right click")
);
}
#[test]
fn banner_layers_and_shield_base_round_trip() {
let mut stack = ItemStack::new("minecraft:shield", 1);
+18
View File
@@ -486,10 +486,28 @@ pub fn lines(
translate: &dyn Fn(&str) -> Option<String>,
) -> Vec<String> {
let mut lines = vec![name_line(stack, translate)];
if TooltipDisplay::for_stack(stack).shows("minecraft:lore") {
lines.extend(lore_lines(stack));
}
lines.extend(detail_lines(stack, info, translate));
lines
}
/// `LoreComponent.addToTooltip`: the saved component lines, as plain text.
fn lore_lines(stack: &ItemStack) -> Vec<String> {
let Some(Tag::List(entries)) = stack.data().and_then(|d| d.other("minecraft:lore")) else {
return Vec::new();
};
entries
.iter()
.map(|entry| {
let json = crate::scoreboard::tag_to_json(entry);
let text = crate::component::to_text(&json, &mut |s| crate::text::Text::literal(s));
text.plain()
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
+98 -14
View File
@@ -242,6 +242,30 @@ fn tracked(cx: i32, cz: i32, r: i32, x: i32, z: i32) -> bool {
dx * dx + dz * dz < i64::from(r) * i64::from(r)
}
/// The view's queue around a new centre: the queued chunks still tracked, then
/// the tracked ones neither sent nor queued, closest first (a stable sort, so
/// equally close chunks keep their order).
fn requeue(
queue: &mut Vec<(i32, i32)>,
sent: &crate::fast_hash::FxHashSet<(i32, i32)>,
(cx, cz): (i32, i32),
r: i32,
) {
queue.retain(|&(x, z)| tracked(cx, cz, r, x, z));
// A walking player changes chunk every few ticks with hundreds of chunks
// queued: a set, not a scan of the queue for each of the view's chunks.
let mut queued: crate::fast_hash::FxHashSet<(i32, i32)> = queue.iter().copied().collect();
for dz in -r - 2..=r + 2 {
for dx in -r - 2..=r + 2 {
let key = (cx + dx, cz + dz);
if tracked(cx, cz, r, key.0, key.1) && !sent.contains(&key) && queued.insert(key) {
queue.push(key);
}
}
}
queue.sort_by_key(|&(x, z)| i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2));
}
/// What a level's players were last told of its clock and weather.
#[derive(Clone, Copy)]
struct LevelSync {
@@ -1295,20 +1319,7 @@ impl Hub {
g.sent_chunks.remove(&(x, z));
g.send(&C::ForgetLevelChunk(play::ForgetLevelChunk { pos: [x, z] }));
}
g.chunk_queue.retain(|&(x, z)| tracked(cx, cz, r, x, z));
for dz in -r - 2..=r + 2 {
for dx in -r - 2..=r + 2 {
let key = (cx + dx, cz + dz);
if tracked(cx, cz, r, key.0, key.1)
&& !g.sent_chunks.contains(&key)
&& !g.chunk_queue.contains(&key)
{
g.chunk_queue.push(key);
}
}
}
g.chunk_queue
.sort_by_key(|&(x, z)| i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2));
requeue(&mut g.chunk_queue, &g.sent_chunks, (cx, cz), r);
}
/// PlayerChunkSender.sendNextChunks' choice: ready chunks, closest first,
@@ -2485,3 +2496,76 @@ fn ticking_state(rate: f32, frozen: bool) -> C {
w.bool(frozen);
raw(ids::TICKING_STATE, w.data)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::fast_hash::FxHashSet;
/// `requeue` as it was, scanning the queue for each chunk of the view.
fn requeue_scan(
queue: &mut Vec<(i32, i32)>,
sent: &FxHashSet<(i32, i32)>,
(cx, cz): (i32, i32),
r: i32,
) {
queue.retain(|&(x, z)| tracked(cx, cz, r, x, z));
for dz in -r - 2..=r + 2 {
for dx in -r - 2..=r + 2 {
let key = (cx + dx, cz + dz);
if tracked(cx, cz, r, key.0, key.1) && !sent.contains(&key) && !queue.contains(&key)
{
queue.push(key);
}
}
}
queue.sort_by_key(|&(x, z)| i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2));
}
/// A player walking a loop, sending a few chunks a tick: the queue
/// after every move is the one the scan built.
#[test]
fn requeue_matches_the_scan() {
for r in [2, 5, 10, 12, 32] {
let (mut a, mut b) = (Vec::new(), Vec::new());
let mut sent = FxHashSet::default();
for step in 0..60i32 {
let center = ((step % 17) - 8, (step * 3 % 11) - 5);
requeue(&mut a, &sent, center, r);
requeue_scan(&mut b, &sent, center, r);
assert_eq!(a, b, "radius {r}, step {step}");
for key in a.drain(..a.len().min(7)) {
sent.insert(key);
}
b.drain(..b.len().min(7));
sent.retain(|&(x, z)| tracked(center.0, center.1, r, x, z));
}
}
}
/// cargo test --release -p horizon-client --lib lan::hub::tests::requeue_cost -- --ignored --nocapture
#[test]
#[ignore]
fn requeue_cost() {
for r in [10, 16, 32] {
for (name, f) in [
("scan", requeue_scan as fn(&mut _, &_, _, _)),
("set", requeue as fn(&mut _, &_, _, _)),
] {
let sent = FxHashSet::default();
let mut queue = Vec::new();
let start = std::time::Instant::now();
let moves = 200;
for step in 0..moves {
// Nothing sent yet (an explorer waiting on generation).
f(&mut queue, &sent, (step, 0), r);
}
let per = start.elapsed().as_secs_f64() * 1e6 / f64::from(moves);
println!(
"view {r}: {name} {per:.1} us per chunk crossed, {} queued",
queue.len()
);
}
}
}
}
+574 -23
View File
@@ -807,7 +807,11 @@ pub struct Mob {
pub persistent: bool,
/// `CustomName` (plain text) and `CustomNameVisible`.
pub custom_name: Option<String>,
/// The styled `CustomName`, when it came from a component.
pub custom_name_text: Option<crate::text::Text>,
pub name_visible: bool,
/// `Entity.DATA_SHARED_FLAGS_ID` bit 0x20: the model is hidden.
pub invisible: bool,
pub random: LegacyRandom,
pub ai: Box<ai::Ai>,
pub extra: Extra,
@@ -846,6 +850,9 @@ pub struct Mob {
/// from the game), handed over with the next tick's events or drained
/// by the game.
pub outbox: Vec<MobEvent>,
/// The skin texture of a player mob (`PlayerInfo.getSkin`), which
/// replaces the type's default when its download is ready.
pub skin: Option<String>,
}
/// Events a mob tick asks the game to act on.
#[derive(Clone, Debug)]
@@ -1310,31 +1317,67 @@ impl Mob {
/// `Entity.push` on other mobs acts at once in vanilla: a mob ticking later
/// moves with it this tick. Takes the `Push` events aimed at mobs out of
/// `events[from..]` into `pending`, which the caller adds to each mob's
/// velocity before its tick (and to every mob once all have ticked).
pub fn take_mob_pushes(events: &mut Vec<MobEvent>, from: usize, pending: &mut Vec<(u32, Vec3)>) {
let mut i = from;
while i < events.len() {
/// velocity before its tick (and to every mob once all have ticked). The
/// other events keep their order.
pub fn take_mob_pushes(events: &mut Vec<MobEvent>, from: usize, pending: &mut PendingPushes) {
// One pass: a crowd's tick holds a push per neighbour, and removing each
// in place would shift the rest every time.
let mut kept = from;
for i in from..events.len() {
if let MobEvent::Push {
target: Who::Mob(id),
velocity,
} = events[i]
{
pending.push((id, Vec3::new(velocity[0], velocity[1], velocity[2])));
events.remove(i);
pending.push(id, Vec3::new(velocity[0], velocity[1], velocity[2]));
} else {
i += 1;
events.swap(kept, i);
kept += 1;
}
}
events.truncate(kept);
}
/// Pushes waiting for their mob's tick, in push order. A list while short;
/// past [`PendingPushes::LIST_MAX`] (a crowd pushing each other) one list
/// per mob, as scanning the whole list for each mob grows with its square.
#[derive(Default)]
pub struct PendingPushes {
list: Vec<(u32, Vec3)>,
by_mob: crate::fast_hash::FxHashMap<u32, Vec<Vec3>>,
}
impl PendingPushes {
const LIST_MAX: usize = 64;
pub fn push(&mut self, id: u32, v: Vec3) {
if self.by_mob.is_empty() && self.list.len() < Self::LIST_MAX {
self.list.push((id, v));
return;
}
// Older pushes first, so each mob's stay in order.
for (id, v) in self.list.drain(..) {
self.by_mob.entry(id).or_default().push(v);
}
self.by_mob.entry(id).or_default().push(v);
}
}
/// Adds the pending pushes aimed at `m` to its velocity, in push order.
pub fn apply_mob_pushes(m: &mut Mob, pending: &mut Vec<(u32, Vec3)>) {
pending.retain(|(id, v)| {
if *id != m.id {
return true;
}
pub fn apply_mob_pushes(m: &mut Mob, pending: &mut PendingPushes) {
let mut add = |v: Vec3| {
m.velocity = Vec3::new(m.velocity.x + v.x, m.velocity.y + v.y, m.velocity.z + v.z);
false
});
};
if !pending.list.is_empty() {
pending.list.retain(|(id, v)| {
if *id != m.id {
return true;
}
add(*v);
false
});
}
if !pending.by_mob.is_empty()
&& let Some(pushes) = pending.by_mob.remove(&m.id)
{
pushes.into_iter().for_each(add);
}
}
/// Applies the brain memories a mob's tick set on other mobs (from event
/// index `from` on), at once: vanilla sets them on the other entity during
@@ -1406,6 +1449,7 @@ pub struct PlayerView {
/// (`EntitySelector.pushableBy` on the client).
pub remote: bool,
}
#[derive(Clone)]
pub struct MobWorld<'a> {
pub world: &'a World,
pub registry: &'a horizon_world::registry::BlockRegistry,
@@ -1420,6 +1464,8 @@ pub struct MobWorld<'a> {
pub others: &'a [Seen],
/// `others` by position, when the caller keeps one (the mob ticks).
pub others_grid: Option<&'a seen_grid::SeenGrid>,
/// Where each id stands in `others`, when the caller keeps one.
pub others_index: Option<&'a crate::fast_hash::FxHashMap<u32, u32>>,
pub daylight: bool,
pub game_time: i64,
pub day_time: i64,
@@ -1438,22 +1484,23 @@ pub struct MobWorld<'a> {
pub world_seed: i64,
/// `Level.getSeaLevel` (63 in the overworld, -63 in a flat world).
pub sea_level: i32,
/// The scoreboard teams of the player and the mobs (collision rules).
pub teams: Teams,
/// The scoreboard teams of the player and the mobs (collision rules),
/// shared: the mob ticks build them once a tick, not once a mob.
pub teams: std::sync::Arc<Teams<'a>>,
/// Item data (max stack sizes), when the host has it.
pub items: Option<&'a crate::items::Items>,
}
/// `Entity.getTeam` of the player and of the mobs on a team.
#[derive(Clone, Debug, Default)]
pub struct Teams {
pub player: Option<crate::scoreboard::Team>,
pub mobs: HashMap<u32, crate::scoreboard::Team>,
pub struct Teams<'a> {
pub player: Option<&'a crate::scoreboard::Team>,
pub mobs: HashMap<u32, &'a crate::scoreboard::Team>,
}
impl Teams {
pub fn of(&self, who: Who) -> Option<&crate::scoreboard::Team> {
impl<'a> Teams<'a> {
pub fn of(&self, who: Who) -> Option<&'a crate::scoreboard::Team> {
match who {
Who::Player => self.player.as_ref(),
Who::Mob(id) => self.mobs.get(&id),
Who::Player => self.player,
Who::Mob(id) => self.mobs.get(&id).copied(),
}
}
}
@@ -1515,10 +1562,13 @@ impl Mob {
last_damage: None,
removed: false,
custom_name: None,
custom_name_text: None,
name_visible: false,
invisible: false,
anim: anim::AnimState::default(),
rider: None,
outbox: Vec::new(),
skin: None,
attrs: Attrs {
speed: kind.speed,
attack_damage: kind.attack_damage,
@@ -2139,3 +2189,504 @@ mod elytra_tests {
assert!((wings.angles[1] - 0.08726646).abs() < 0.00001);
}
}
#[cfg(test)]
mod teams_tests {
use super::*;
use crate::save::uuid_text;
use crate::scoreboard::{Scoreboard, Team};
/// A board where every other mob is on the player's team, as with a
/// datapack that colours its mobs' names.
fn board(mobs: u32) -> (Scoreboard, HashMap<u32, [i32; 4]>) {
let mut board = Scoreboard::default();
board.add_team("red");
board.add_to_team("Steve", "red");
let uuids: HashMap<u32, [i32; 4]> = (0..mobs)
.map(|id| (id, [id as i32, 7, -(id as i32), 11]))
.collect();
for (id, uuid) in &uuids {
if id % 2 == 0 {
board.add_to_team(&uuid_text(*uuid), "red");
}
}
(board, uuids)
}
/// `Game::mob_teams` as built for each mob before: owned clones of the teams.
fn owned(board: &Scoreboard, uuids: &HashMap<u32, [i32; 4]>) -> HashMap<u32, Team> {
uuids
.iter()
.filter_map(|(id, u)| Some((*id, board.players_team(&uuid_text(*u))?.clone())))
.collect()
}
fn borrowed<'a>(board: &'a Scoreboard, uuids: &HashMap<u32, [i32; 4]>) -> Teams<'a> {
Teams {
player: board.players_team("Steve"),
mobs: uuids
.iter()
.filter_map(|(id, u)| Some((*id, board.players_team(&uuid_text(*u))?)))
.collect(),
}
}
#[test]
fn borrowed_teams_answer_as_the_clones() {
let (board, uuids) = board(40);
let teams = borrowed(&board, &uuids);
let old = owned(&board, &uuids);
for id in 0..41 {
assert_eq!(teams.of(Who::Mob(id)), old.get(&id), "mob {id}");
}
assert_eq!(teams.of(Who::Player).map(|t| t.name.as_str()), Some("red"));
}
/// One tick's worth of team building: once per mob (before) against once.
#[test]
#[ignore]
fn teams_cost() {
for mobs in [100u32, 250, 500] {
let (board, uuids) = board(mobs);
let t = std::time::Instant::now();
for _ in 0..mobs {
std::hint::black_box(owned(&board, &uuids));
}
let before = t.elapsed();
let t = std::time::Instant::now();
let reps = 20;
for _ in 0..reps {
std::hint::black_box(borrowed(&board, &uuids));
}
let after = t.elapsed() / reps;
println!(
"{mobs} mobs, half on a team: per tick {:.3} ms -> {:.3} ms",
before.as_secs_f64() * 1e3,
after.as_secs_f64() * 1e3
);
}
}
}
#[cfg(test)]
mod sensor_scan_tests {
use super::*;
use horizon_core::geometry::Vec3;
use serde_json::json;
fn kind() -> Arc<MobType> {
Arc::new(MobType {
id: "minecraft:cow".into(),
species: Species::Cow,
width: 0.9,
height: 1.4,
eye_height: 1.3,
category: "creature".into(),
max_health: 10.0,
speed: 0.2,
flying_speed: 0.4,
attack_damage: 0.0,
attack_knockback: 0.0,
follow_range: 16.0,
step_height: 0.6,
armor: 0.0,
armor_toughness: 0.0,
knockback_resistance: 0.0,
jump_strength: 0.42,
attributes: Arc::default(),
gravity: 0.08,
safe_fall_distance: 3.0,
fall_damage_multiplier: 1.0,
water_efficiency: 0.0,
water_slowdown: 0.8,
air_speed: 0.02,
baby_scale: 0.5,
head_rot_speed: 10.0,
max_head_x_rot: 40.0,
max_head_y_rot: 75.0,
ambient_interval: 120,
flags: Flags {
animal: true,
..Default::default()
},
model: Arc::new(Part::parse("root", &json!({}))),
layers: HashMap::new(),
texture: String::new(),
brain: Brain::Passive,
anim: Anim::Rabbit,
sounds: [""; 3],
step_sound: None,
food: Arc::default(),
tempt: Arc::default(),
})
}
/// `n` mobs in pens of 20 spread over a square of `side` blocks, as on a
/// server's farms; their snapshot and its grid.
fn herd(n: u32, side: f64) -> (Vec<Seen>, seen_grid::SeenGrid) {
let kind = kind();
let mut seed = 0x9e37_79b9_7f4a_7c15u64;
let mut next = |range: f64| {
seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1);
(seed >> 11) as f64 / (1u64 << 53) as f64 * range
};
let mut pen = Vec3::ZERO;
let seen: Vec<Seen> = (0..n)
.map(|id| {
if id % 20 == 0 {
pen = Vec3::new(next(side), 64.0, next(side));
}
let at = Vec3::new(pen.x + next(8.0), 64.0, pen.z + next(8.0));
Mob::new(id, kind.clone(), at, 0.0).seen(None)
})
.collect();
let grid = seen_grid::SeenGrid::new(seen.iter().map(|s| s.bounds));
(seen, grid)
}
/// `NearestLivingEntitySensor` over every mob, as the brains ran it.
fn scan(seen: &[Seen], me: &Seen, area: Aabb) -> Vec<u32> {
seen.iter()
.filter(|o| o.id != me.id && o.alive && o.bounds.intersects(area))
.map(|o| o.id)
.collect()
}
fn indexed(seen: &[Seen], grid: &seen_grid::SeenGrid, me: &Seen, area: Aabb) -> Vec<u32> {
let mut near = Vec::new();
assert!(grid.candidates(area, &mut near));
near.iter()
.map(|&i| &seen[i as usize])
.filter(|o| o.id != me.id && o.alive && o.bounds.intersects(area))
.map(|o| o.id)
.collect()
}
#[test]
fn the_grid_finds_what_the_scan_finds() {
let (seen, grid) = herd(600, 160.0);
for me in &seen {
let area = me.bounds.inflate(Vec3::new(16.0, 16.0, 16.0));
assert_eq!(indexed(&seen, &grid, me, area), scan(&seen, me, area));
}
}
/// `MobWorld::seen` once per mob (a target, a breeding partner...), by
/// scan as before and by the tick's id index.
#[test]
#[ignore]
fn lookup_cost() {
for n in [500u32, 1000, 2000] {
let (seen, _) = herd(n, 256.0);
let index: crate::fast_hash::FxHashMap<u32, u32> = seen
.iter()
.enumerate()
.map(|(i, s)| (s.id, i as u32))
.collect();
let wanted: Vec<u32> = (0..n).map(|i| (i * 7919) % n).collect();
let t = std::time::Instant::now();
let a: usize = wanted
.iter()
.filter_map(|&id| seen.iter().find(|s| s.id == id))
.map(|s| s.id as usize)
.sum();
let before = t.elapsed().as_secs_f64() * 1e3;
let t = std::time::Instant::now();
let b: usize = wanted
.iter()
.filter_map(|&id| index.get(&id).map(|&i| &seen[i as usize]))
.map(|s| s.id as usize)
.sum();
let after = t.elapsed().as_secs_f64() * 1e3;
assert_eq!(a, b);
println!(
"{n} lookups over {n} mobs ({} B each): {before:.3} ms -> {after:.3} ms",
std::mem::size_of::<Seen>()
);
}
}
/// Natural spawning's `isUnobstructed` box test against every mob (real
/// `Mob`s), by scan and by a grid built once for the pass.
#[test]
#[ignore]
fn spawn_box_cost() {
let kind = kind();
for n in [500u32, 1000, 2000] {
let (seen, _) = herd(n, 384.0);
let mobs: Vec<Mob> = seen
.iter()
.map(|s| Mob::new(s.id, kind.clone(), s.position, 0.0))
.collect();
let mut seed = 7u64;
let mut next = |range: f64| {
seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1);
(seed >> 11) as f64 / (1u64 << 53) as f64 * range
};
let queries: Vec<Aabb> = (0..100)
.map(|_| {
let (x, z) = (next(384.0), next(384.0));
Aabb::new(Vec3::new(x, 64.0, z), Vec3::new(x + 0.9, 65.4, z + 0.9))
})
.collect();
let t = std::time::Instant::now();
let a: Vec<bool> = queries
.iter()
.map(|&q| {
mobs.iter()
.any(|m| m.alive() && !m.removed && m.bounds().intersects(q))
})
.collect();
let before = t.elapsed().as_secs_f64() * 1e3;
let t = std::time::Instant::now();
let boxes: Vec<Aabb> = mobs.iter().map(|m| m.bounds()).collect();
let grid = seen_grid::SeenGrid::new(boxes.iter().copied());
let build = t.elapsed().as_secs_f64() * 1e3;
let mut near = Vec::new();
let b: Vec<bool> = queries
.iter()
.map(|&q| {
assert!(grid.candidates(q, &mut near));
near.iter().any(|&i| boxes[i as usize].intersects(q))
})
.collect();
let after = t.elapsed().as_secs_f64() * 1e3;
assert_eq!(a, b);
println!(
"{n} mobs, 100 spawn box tests: {before:.3} ms -> {after:.3} ms (grid build {build:.3} ms included)"
);
}
}
/// One tick of sensing: a twentieth of the mobs fire their sensor.
#[test]
#[ignore]
fn sensor_cost() {
for (n, side) in [(500u32, 256.0), (1000, 384.0), (2000, 512.0)] {
let (seen, grid) = herd(n, side);
let time = |f: &dyn Fn(&Seen, Aabb) -> usize| {
let t = std::time::Instant::now();
let mut found = 0;
for me in &seen {
found += f(me, me.bounds.inflate(Vec3::new(16.0, 16.0, 16.0)));
}
(t.elapsed().as_secs_f64() * 1e3 / 20.0, found)
};
let (before, a) = time(&|me, area| scan(&seen, me, area).len());
let (after, b) = time(&|me, area| indexed(&seen, &grid, me, area).len());
assert_eq!(a, b);
println!("{n} mobs: per tick {before:.3} ms -> {after:.3} ms");
}
}
}
#[cfg(test)]
mod push_tests {
use super::*;
use horizon_core::geometry::Vec3;
use serde_json::json;
fn kind() -> Arc<MobType> {
Arc::new(MobType {
id: "minecraft:cow".into(),
species: Species::Cow,
width: 0.9,
height: 1.4,
eye_height: 1.3,
category: "creature".into(),
max_health: 10.0,
speed: 0.2,
flying_speed: 0.4,
attack_damage: 0.0,
attack_knockback: 0.0,
follow_range: 16.0,
step_height: 0.6,
armor: 0.0,
armor_toughness: 0.0,
knockback_resistance: 0.0,
jump_strength: 0.42,
attributes: Arc::default(),
gravity: 0.08,
safe_fall_distance: 3.0,
fall_damage_multiplier: 1.0,
water_efficiency: 0.0,
water_slowdown: 0.8,
air_speed: 0.02,
baby_scale: 0.5,
head_rot_speed: 10.0,
max_head_x_rot: 40.0,
max_head_y_rot: 75.0,
ambient_interval: 120,
flags: Flags {
animal: true,
..Default::default()
},
model: Arc::new(Part::parse("root", &json!({}))),
layers: HashMap::new(),
texture: String::new(),
brain: Brain::Passive,
anim: Anim::Rabbit,
sounds: [""; 3],
step_sound: None,
food: Arc::default(),
tempt: Arc::default(),
})
}
/// The pending pushes as one list, scanned for each mob, as before.
fn take_old(events: &mut Vec<MobEvent>, from: usize, pending: &mut Vec<(u32, Vec3)>) {
let mut i = from;
while i < events.len() {
if let MobEvent::Push {
target: Who::Mob(id),
velocity,
} = events[i]
{
pending.push((id, Vec3::new(velocity[0], velocity[1], velocity[2])));
events.remove(i);
} else {
i += 1;
}
}
}
fn apply_old(velocity: &mut Vec3, id: u32, pending: &mut Vec<(u32, Vec3)>) {
pending.retain(|(to, v)| {
if *to != id {
return true;
}
*velocity = Vec3::new(velocity.x + v.x, velocity.y + v.y, velocity.z + v.z);
false
});
}
fn mobs(n: u32) -> Vec<Mob> {
let kind = kind();
(0..n)
.map(|id| Mob::new(id, kind.clone(), Vec3::ZERO, 0.0))
.collect()
}
/// A crowd tick: each mob takes its pushes, then pushes every other
/// mob (and the player, and hisses now and then); then all take theirs.
fn crowd_tick(
n: u32,
tick: u32,
mut on_push: impl FnMut(&mut Vec<MobEvent>, usize),
mut on_apply: impl FnMut(u32),
) -> Vec<MobEvent> {
let mut events = Vec::new();
for i in 0..n {
on_apply(i);
let from = events.len();
for j in 0..n {
if j != i && !(i * 31 + j * 17 + tick).is_multiple_of(5) {
let f = f64::from(i * 7 + j + tick) * 0.001;
events.push(MobEvent::Push {
target: Who::Mob(j),
velocity: [f, -f * 0.5, f * 0.25],
});
}
if j.is_multiple_of(9) {
events.push(MobEvent::Hiss { mob: j });
}
}
events.push(MobEvent::Push {
target: Who::Player,
velocity: [0.1, 0.0, 0.1],
});
on_push(&mut events, from);
}
for i in 0..n {
on_apply(i);
}
events
}
#[test]
fn pushes_land_as_with_the_list() {
for n in [1u32, 3, 8, 20, 60] {
let mut new_mobs = mobs(n);
let mut old_v = vec![Vec3::ZERO; n as usize];
let mut pending = PendingPushes::default();
let mut old_pending = Vec::new();
for tick in 0..4 {
let new_events = {
let pending = std::cell::RefCell::new(&mut pending);
let mobs = std::cell::RefCell::new(&mut new_mobs);
crowd_tick(
n,
tick,
|e, from| take_mob_pushes(e, from, &mut pending.borrow_mut()),
|i| {
apply_mob_pushes(
&mut mobs.borrow_mut()[i as usize],
&mut pending.borrow_mut(),
)
},
)
};
let old_events = {
let pending = std::cell::RefCell::new(&mut old_pending);
let v = std::cell::RefCell::new(&mut old_v);
crowd_tick(
n,
tick,
|e, from| take_old(e, from, &mut pending.borrow_mut()),
|i| {
apply_old(
&mut v.borrow_mut()[i as usize],
i,
&mut pending.borrow_mut(),
)
},
)
};
assert_eq!(format!("{new_events:?}"), format!("{old_events:?}"));
for i in 0..n as usize {
let (a, b) = (new_mobs[i].velocity, old_v[i]);
assert_eq!(
(a.x.to_bits(), a.y.to_bits(), a.z.to_bits()),
(b.x.to_bits(), b.y.to_bits(), b.z.to_bits()),
"{n} mobs, tick {tick}, mob {i}"
);
}
}
}
}
/// One tick of a pen of `n` mobs pushing each other.
#[test]
#[ignore]
fn push_cost() {
for n in [10u32, 30, 100, 200] {
let mut ms = mobs(n);
let mut v = vec![Vec3::ZERO; n as usize];
let t = std::time::Instant::now();
{
let pending = std::cell::RefCell::new(Vec::new());
let v = std::cell::RefCell::new(&mut v);
crowd_tick(
n,
0,
|e, from| take_old(e, from, &mut pending.borrow_mut()),
|i| {
apply_old(
&mut v.borrow_mut()[i as usize],
i,
&mut pending.borrow_mut(),
)
},
);
}
let before = t.elapsed().as_secs_f64() * 1e3;
let t = std::time::Instant::now();
{
let pending = std::cell::RefCell::new(PendingPushes::default());
let ms = std::cell::RefCell::new(&mut ms);
crowd_tick(
n,
0,
|e, from| take_mob_pushes(e, from, &mut pending.borrow_mut()),
|i| {
apply_mob_pushes(
&mut ms.borrow_mut()[i as usize],
&mut pending.borrow_mut(),
)
},
);
}
let after = t.elapsed().as_secs_f64() * 1e3;
println!("{n} mobs pushing each other: {before:.3} ms -> {after:.3} ms");
}
}
}
@@ -606,7 +606,7 @@ fn sense_living(m: &mut Mob, w: &MobWorld) {
let r = m.attrs.follow_range;
let area = m.bounds().inflate(Vec3::new(r, r, r));
let mut found: Vec<(Who, f64)> = Vec::new();
for o in w.others {
for o in w.others_near(area) {
if o.id != m.id && o.alive && o.bounds.intersects(area) {
found.push((Who::Mob(o.id), m.distance_sqr(o.position)));
}
+1 -1
View File
@@ -441,7 +441,7 @@ impl Goal for PufferfishPuff {
.as_ref()
.is_some_and(|p| p.alive && !p.creative && area.intersects(p.bounds));
// TARGETING_CONDITIONS: not other fish (not_scary_for_pufferfish) and alive.
let mob = cx.w.others.iter().any(|s| {
let mob = cx.w.others_near(area).any(|s| {
s.id != m.id
&& s.alive
&& !matches!(
+5 -6
View File
@@ -391,12 +391,11 @@ impl Goal for LeaveBlock {
let target = cx.w.world.block(x, y, z);
let below = cx.w.world.block(x, y - 1, z);
let bf = cx.w.facts.get(below);
let free = !cx.w.others.iter().any(|s| {
s.bounds.intersects(Aabb::new(
Vec3::new(f64::from(x), f64::from(y), f64::from(z)),
Vec3::new(f64::from(x + 1), f64::from(y + 1), f64::from(z + 1)),
))
});
let block = Aabb::new(
Vec3::new(f64::from(x), f64::from(y), f64::from(z)),
Vec3::new(f64::from(x + 1), f64::from(y + 1), f64::from(z + 1)),
);
let free = !cx.w.others_near(block).any(|s| s.bounds.intersects(block));
if cx.w.facts.is_air(target)
&& !cx.w.facts.is_air(below)
&& &*bf.name != "minecraft:bedrock"
+12 -4
View File
@@ -1,7 +1,11 @@
//! Vanilla's `GoalSelector` (priorities, MOVE/LOOK/JUMP/TARGET control flags,
//! interruption) and the shared goals of `net.minecraft.world.entity.ai.goal`.
use super::{Mob, MobEvent, MobWorld, Seen, Species, Who, world::TargetInfo};
use horizon_core::{geometry::Vec3, jmath, random::RandomSource};
use horizon_core::{
geometry::{Aabb, Vec3},
jmath,
random::RandomSource,
};
use std::sync::Arc;
pub const MOVE: u8 = 1;
@@ -970,8 +974,7 @@ impl Goal for LookAtPlayer {
let (id, only) = (m.id, self.only);
let at = Vec3::new(m.position.x, m.eye_y(), m.position.z);
let candidates: Vec<(u32, Vec3)> =
cx.w.others
.iter()
cx.w.others_near(area)
.filter(|s| s.id != id && s.alive && s.bounds.intersects(area))
.filter(|s| only.is_none_or(|o| s.species == o))
.map(|s| (s.id, s.position))
@@ -1902,7 +1905,12 @@ impl Goal for FollowParent {
let species = m.species();
let pos = m.position;
let mut best: Option<(u32, f64)> = None;
for s in cx.w.others.iter().filter(|s| {
// Every position in range puts the box's bottom face in this area.
let area = Aabb::new(
Vec3::new(pos.x - 9.0, pos.y - 5.0, pos.z - 9.0),
Vec3::new(pos.x + 9.0, pos.y + 5.0, pos.z + 9.0),
);
for s in cx.w.others_near(area).filter(|s| {
s.alive
&& s.species == species
&& !s.baby
+4 -1
View File
@@ -68,7 +68,10 @@ impl Goal for DefendVillage {
if !b.intersects(p.bounds) || m.position.distance_squared(p.position) > 64.0 * 64.0 {
return false;
}
let bad = cx.w.others.iter().any(|s| {
// A position inside `b` puts the box's bottom face there (one block
// wider, as the box test is strict).
let near = b.inflate(Vec3::new(1.0, 1.0, 1.0));
let bad = cx.w.others_near(near).any(|s| {
s.species == Species::Villager
&& s.alive
&& s.player_rep <= -100
@@ -299,7 +299,7 @@ fn sense_living(m: &mut Mob, w: &MobWorld) {
let r = m.attrs.follow_range;
let area = m.bounds().inflate(Vec3::new(r, r, r));
let mut found: Vec<(Who, f64)> = Vec::new();
for o in w.others {
for o in w.others_near(area) {
if o.id != m.id && o.alive && o.bounds.intersects(area) {
found.push((Who::Mob(o.id), m.distance_sqr(o.position)));
}
+1 -1
View File
@@ -177,7 +177,7 @@ fn roar(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
velocity: fling(p.position),
});
}
for s in w.others.iter() {
for s in w.others_near(area) {
if s.id == m.id || !s.alive || s.species == Species::Ravager || !s.bounds.intersects(area) {
continue;
}
+4 -1
View File
@@ -104,7 +104,10 @@ pub fn body(m: &Mob) -> (String, String) {
}
};
if &*m.kind.id == "minecraft:player" {
return ("main".into(), m.kind.texture.to_string());
return (
"main".into(),
m.skin.clone().unwrap_or_else(|| m.kind.texture.clone()),
);
}
match m.species() {
S::Llama | S::TraderLlama => {
@@ -568,7 +568,7 @@ fn sense_living(m: &mut Mob, w: &MobWorld) {
let r = m.attrs.follow_range;
let area = m.bounds().inflate(Vec3::new(r, r, r));
let mut found: Vec<(Who, f64)> = Vec::new();
for o in w.others {
for o in w.others_near(area) {
if o.id != m.id && o.alive && o.bounds.intersects(area) {
found.push((Who::Mob(o.id), m.distance_sqr(o.position)));
}
@@ -898,8 +898,9 @@ fn poi_matches(m: &Mob, f: PoiFilter, p: Poi) -> bool {
_ => false,
}
}
/// Tickets taken on a POI: by this villager's memories and by others'.
fn tickets(m: &Mob, w: &MobWorld, p: [i32; 3]) -> usize {
/// Tickets taken on a POI: by this villager's memories and by others'
/// (counted once per search by [`others_tickets`]).
fn tickets(m: &Mob, others: &crate::fast_hash::FxHashMap<[i32; 3], usize>, p: [i32; 3]) -> usize {
let mut n = 0;
if let Some(mm) = mem_ref(m) {
for s in [
@@ -913,16 +914,22 @@ fn tickets(m: &Mob, w: &MobWorld, p: [i32; 3]) -> usize {
}
}
}
n + others.get(&p).copied().unwrap_or(0)
}
/// The tickets the other living villagers' POIs take, by POI.
fn others_tickets(m: &Mob, w: &MobWorld) -> crate::fast_hash::FxHashMap<[i32; 3], usize> {
let mut taken = crate::fast_hash::FxHashMap::default();
for o in w.others {
if o.id != m.id && o.alive && o.species == Species::Villager {
for q in [o.job, o.bed, o.potential_job, o.meeting] {
if q == Some(p) {
n += 1;
}
for q in [o.job, o.bed, o.potential_job, o.meeting]
.into_iter()
.flatten()
{
*taken.entry(q).or_insert(0) += 1;
}
}
}
n
taken
}
/// The POIs of chunk `(qx, qz)` from `y0` to `y1` (within the world), in
/// scan order: y, then z, then x. Only the indexed POI blocks of the sections
@@ -962,6 +969,8 @@ fn pois_in_range(
) -> Vec<(Poi, [i32; 3])> {
let c = block_of(m.position);
let mut found = Vec::new();
// Every candidate asks how taken it is: count the others' once.
let mut taken = None;
let r2 = i64::from(radius) * i64::from(radius);
let (cx, cz) = (c[0] >> 4, c[2] >> 4);
let chunks = radius.div_euclid(16) + 1;
@@ -974,7 +983,8 @@ fn pois_in_range(
|| (p[2] - c[2]).abs() > radius
|| dist_sqr(p, c) > r2
|| !poi_matches(m, f, poi)
|| tickets(m, w, p) >= poi.max_tickets()
|| tickets(m, taken.get_or_insert_with(|| others_tickets(m, w)), p)
>= poi.max_tickets()
{
return;
}
@@ -492,7 +492,7 @@ fn sense_entities(m: &mut Mob, w: &MobWorld) {
let r = m.attrs.follow_range;
let area = m.bounds().inflate(Vec3::new(r, r, r));
let mut found: Vec<(Who, f64)> = Vec::new();
for o in w.others {
for o in w.others_near(area) {
if o.id != m.id && o.alive && o.bounds.intersects(area) {
found.push((Who::Mob(o.id), m.distance_sqr(o.position)));
}
+15 -3
View File
@@ -226,7 +226,13 @@ impl MobWorld<'_> {
.chain(all.into_iter().flatten())
}
pub fn seen(&self, id: u32) -> Option<&Seen> {
self.others.iter().find(|s| s.id == id)
match self.others_index {
Some(index) => index
.get(&id)
.and_then(|&i| self.others.get(i as usize))
.filter(|s| s.id == id),
None => self.others.iter().find(|s| s.id == id),
}
}
/// Living mobs of the given species within `range` of `from`, nearest first.
pub fn nearby<'s>(
@@ -235,9 +241,15 @@ impl MobWorld<'_> {
range: f64,
pred: impl Fn(&Seen) -> bool + 's,
) -> impl Iterator<Item = &'s Seen> + 's {
// A position within `range` puts the box's bottom face in this area
// (one block wider, as the box test is strict).
let r = range + 1.0;
let area = Aabb::new(
Vec3::new(from.x - r, from.y - r, from.z - r),
Vec3::new(from.x + r, from.y + r, from.z + r),
);
let mut v: Vec<&Seen> = self
.others
.iter()
.others_near(area)
.filter(|s| {
s.alive
&& (s.position.x - from.x).abs() <= range
+2 -1
View File
@@ -716,7 +716,8 @@ impl MotionWorld<'_, '_, '_> {
supporting_block: self.player.ground.supporting_block,
}
}
fn climbable(&mut self) -> Result<bool> {
/// `LivingEntity.onClimbable` at the player's feet (it remembers the block).
pub fn climbable(&mut self) -> Result<bool> {
if self.player.spectator {
return Ok(false);
}
+33
View File
@@ -112,6 +112,24 @@ pub fn unpack(value: &str, signature: Signature, valid_uris: &[&str]) -> Texture
out
}
/// The skin URL of a `minecraft:profile` component's property list. Player
/// heads and mannequins read their profile this way, and `SkinManager` lets
/// those textures be unsigned (`getInsecureSkinInformation`).
pub fn profile_skin_url(profile: &serde_json::Value) -> Option<String> {
let value = profile
.get("properties")?
.as_array()?
.iter()
.find_map(|p| {
if p.get("name")?.as_str()? == "textures" {
p.get("value")?.as_str()
} else {
None
}
})?;
unpack(value, Signature::Unsigned, &TEXTURE_URIS).skin
}
/// `MinecraftProfileTexture.getHash`: the base name of the URL's path.
pub fn texture_hash(url: &str) -> Option<String> {
let rest = url.split_once("://")?.1;
@@ -393,6 +411,21 @@ pub fn sha1(data: &[u8]) -> [u8; 20] {
#[cfg(test)]
mod tests {
#[test]
fn profile_components_supply_unsigned_head_skins() {
let value = "eyJ0ZXh0dXJlcyI6IHsiU0tJTiI6IHsidXJsIjogImh0dHBzOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlL2FiYzEyMyJ9fX0=";
let profile = serde_json::json!({"properties": [{"name": "textures", "value": value}]});
assert_eq!(
super::profile_skin_url(&profile).as_deref(),
Some("https://textures.minecraft.net/texture/abc123")
);
let other = serde_json::json!({"properties": [{"name": "other", "value": value}]});
assert_eq!(super::profile_skin_url(&other), None);
let bad = serde_json::json!({"properties": [{"name": "textures", "value": "not base64"}]});
assert_eq!(super::profile_skin_url(&bad), None);
assert_eq!(super::profile_skin_url(&serde_json::json!({})), None);
}
/// Textures come over plain HTTP too (a local server here; https is the
/// same request through rustls), and only 2xx answers count.
#[test]
+238
View File
@@ -276,6 +276,8 @@ pub struct Streaming {
/// chunks finish out to `radius + 1` (Chebyshev) and generation reaches
/// past that as around the player. They are generated and kept but not meshed.
pub extra: Vec<((i32, i32), i32)>,
/// `extra`'s squares as a grid, made again when `extra` changes.
cover: Option<ExtraCover>,
/// Mesh the sections for drawing (false on a server thread).
pub mesh: bool,
}
@@ -391,6 +393,7 @@ impl GenerationScan {
radius: input.ranges.radius,
extra: input.ranges.extra,
forced: input.ranges.forced,
cover: input.ranges.cover,
};
self.remember(ranges, input.world);
} else {
@@ -562,6 +565,7 @@ impl Streaming {
meshed_total: 0,
remote: false,
extra: Vec::new(),
cover: None,
mesh: true,
}
}
@@ -808,6 +812,7 @@ impl Streaming {
if self.paused {
return (uploads, Vec::new());
}
ExtraCover::refresh(&mut self.cover, &self.extra);
self.relight_edits(world);
let deferred = std::mem::take(&mut self.deferred);
let received = std::iter::from_fn(|| self.results.try_recv().ok());
@@ -982,6 +987,7 @@ impl Streaming {
radius: self.radius,
extra: &self.extra,
forced: &self.forced,
cover: ExtraCover::matching(&self.cover, &self.extra),
};
let (protos, generating) = (&self.protos, &self.generating);
self.decoration_order.retain(|x, z| {
@@ -1109,6 +1115,7 @@ impl Streaming {
radius: self.radius,
extra: &self.extra,
forced: &self.forced,
cover: ExtraCover::matching(&self.cover, &self.extra),
};
self.waiting.retain(|&(x, sy, z)| {
let back = ranges.in_view(x, z);
@@ -1185,6 +1192,7 @@ impl Streaming {
radius: self.radius,
extra: &self.extra,
forced: &self.forced,
cover: ExtraCover::matching(&self.cover, &self.extra),
},
world,
generating: &self.generating,
@@ -1280,6 +1288,7 @@ impl Streaming {
radius: self.radius,
extra: &self.extra,
forced: &self.forced,
cover: ExtraCover::matching(&self.cover, &self.extra),
}
}
fn near_finished(&self, x: i32, z: i32, reach: i32) -> bool {
@@ -1365,6 +1374,100 @@ struct Ranges<'a> {
radius: i32,
extra: &'a [((i32, i32), i32)],
forced: &'a HashSet<(i32, i32)>,
/// `extra` as a grid, when one was made for it: `in_extra` without a
/// scan of every area.
cover: Option<&'a ExtraCover>,
}
/// 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
/// loaded and wanted column whether one reaches it, several times a tick:
/// with fifty players, a scan of fifty squares per question.
struct ExtraCover {
extra: Vec<((i32, i32), i32)>,
x0: i32,
z0: i32,
width: u32,
depth: u32,
cells: Box<[i8]>,
}
impl ExtraCover {
/// The largest margin the grid answers for; beyond it (and outside the
/// grid) a column is further than that from every square.
const REACH: i32 = 4;
/// The largest grid, in columns (a mebibyte).
const CELLS: u64 = 1 << 20;
fn matching<'a>(
cover: &'a Option<ExtraCover>,
extra: &[((i32, i32), i32)],
) -> Option<&'a ExtraCover> {
cover.as_ref().filter(|c| c.extra == extra)
}
/// Make the grid again if `extra` changed; none for one area or none
/// (a scan is as quick), or areas too far apart.
fn refresh(cover: &mut Option<ExtraCover>, extra: &[((i32, i32), i32)]) {
if cover.as_ref().is_some_and(|c| c.extra == extra) {
return;
}
*cover = Self::new(extra);
}
fn new(extra: &[((i32, i32), i32)]) -> Option<Self> {
if extra.len() < 2 || extra.iter().any(|&(_, r)| !(0..=64).contains(&r)) {
return None;
}
let k = i64::from(Self::REACH);
let (mut x0, mut z0, mut x1, mut z1) = (i64::MAX, i64::MAX, i64::MIN, i64::MIN);
for &((x, z), r) in extra {
let (x, z, r) = (i64::from(x), i64::from(z), i64::from(r) + k);
(x0, z0, x1, z1) = (x0.min(x - r), z0.min(z - r), x1.max(x + r), z1.max(z + r));
}
let (width, depth) = ((x1 - x0 + 1) as u64, (z1 - z0 + 1) as u64);
let fits = |lo: i64, hi: i64| lo >= i64::from(i32::MIN) && hi <= i64::from(i32::MAX);
if width * depth > Self::CELLS || !fits(x0, x1) || !fits(z0, z1) {
return None;
}
let (x0, z0, width, depth) = (x0 as i32, z0 as i32, width as u32, depth as u32);
let mut cells = vec![(Self::REACH + 1) as i8; (width * depth) as usize].into_boxed_slice();
// Inside a square counts as 0: no margin asked for is below it. Each
// row of a square is a run of one value with a slope at either end.
let lower = |run: &mut [i8], v: i8| run.iter_mut().for_each(|c| *c = (*c).min(v));
for &((cx, cz), r) in extra {
let reach = r + Self::REACH;
for dz in -reach..=reach {
let start = (cz + dz - z0) as usize * width as usize + (cx - x0) as usize;
let row = &mut cells[start - reach as usize..=start + reach as usize];
let flat = dz.abs().max(r);
let centre = reach as usize;
lower(
&mut row[centre - flat as usize..=centre + flat as usize],
(flat - r) as i8,
);
for d in flat + 1..=reach {
let v = (d - r) as i8;
lower(&mut row[centre - d as usize..=centre - d as usize], v);
lower(&mut row[centre + d as usize..=centre + d as usize], v);
}
}
}
Some(Self {
extra: extra.to_vec(),
x0,
z0,
width,
depth,
cells,
})
}
/// Whether (x, z) is within `margin` of an area's square, for a
/// `margin` up to `REACH`.
fn within(&self, x: i32, z: i32, margin: i32) -> bool {
let (dx, dz) = (
x.wrapping_sub(self.x0) as u32,
z.wrapping_sub(self.z0) as u32,
);
dx < self.width
&& dz < self.depth
&& i32::from(self.cells[(dz * self.width + dx) as usize]) <= margin
}
}
impl Ranges<'_> {
/// `distance`, or `rings` while the level loading screen is up.
@@ -1441,6 +1544,11 @@ impl Ranges<'_> {
/// Within `margin` of an extra area's square (Chebyshev distance, as
/// vanilla's ticket levels spread).
fn in_extra(&self, x: i32, z: i32, margin: i32) -> bool {
if let Some(cover) = self.cover
&& (0..=ExtraCover::REACH).contains(&margin)
{
return cover.within(x, z, margin);
}
self.extra
.iter()
.any(|&(c, radius)| (x - c.0).abs().max((z - c.1).abs()) <= radius + margin)
@@ -1554,6 +1662,7 @@ mod tests {
radius: self.radius,
extra: &self.extra,
forced: &self.forced,
cover: None,
},
world: &self.world,
generating: &self.generating,
@@ -1866,6 +1975,7 @@ mod tests {
radius,
extra: &[],
forced: &forced,
cover: None,
};
let square = |x: i32, z: i32, n: i32| {
(-n..=n).flat_map(move |dz| (-n..=n).map(move |dx| (x + dx, z + dz)))
@@ -1920,6 +2030,7 @@ mod tests {
radius: 12,
extra: &[],
forced: &forced,
cover: None,
};
let at = |dx: i32, dz: i32| (2 + dx, -1 + dz);
let key = |(x, z): (i32, i32), loading| r.priority(x, z, loading);
@@ -1945,6 +2056,7 @@ mod tests {
radius,
extra: &[((radius * 2 + 9, 3), 4)],
forced: &forced,
cover: None,
};
let span = radius * 3 + 20;
for x in -span..=span {
@@ -1984,6 +2096,7 @@ mod tests {
radius: 2,
extra: &extra,
forced: &forced,
cover: 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 {
@@ -2021,3 +2134,128 @@ mod tests {
}
}
}
#[cfg(test)]
mod cover_tests {
use super::*;
fn areas(seed: &mut u64, n: usize, spread: i32) -> Vec<((i32, i32), i32)> {
let mut next = |m: i32| {
*seed ^= *seed << 13;
*seed ^= *seed >> 7;
*seed ^= *seed << 17;
(*seed % (2 * m as u64 + 1)) as i32 - m
};
(0..n)
.map(|_| ((next(spread), next(spread)), 2 + next(14).abs()))
.collect()
}
/// Every question streaming asks gets the same answer with the grid.
#[test]
fn the_grid_answers_as_the_scan() {
let mut seed = 0x9E37_79B9_7F4A_7C15u64;
let forced: HashSet<(i32, i32)> = [(40, -40)].into();
for case in 0..60 {
let extra = areas(&mut seed, 2 + case % 20, [4, 30, 120][case % 3]);
let cover = ExtraCover::new(&extra).expect("a grid");
let ranges = |cover| Ranges {
center: (3, -2),
radius: 10,
extra: &extra,
forced: &forced,
cover,
};
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 {
for m in 0..=ExtraCover::REACH + 2 {
assert_eq!(
scan.in_extra(x, z, m),
grid.in_extra(x, z, m),
"case {case} ({x}, {z}) {m}"
);
}
for d in [false, true] {
assert_eq!(scan.unloaded(x, z, d), grid.unloaded(x, z, d));
assert_eq!(scan.requested(x, z, d), grid.requested(x, z, d));
assert_eq!(scan.chunk_kept(x, z, d), grid.chunk_kept(x, z, d));
}
}
}
}
}
#[test]
fn a_stale_or_impossible_grid_is_not_used() {
let mut cover = None;
ExtraCover::refresh(&mut cover, &[((0, 0), 11), ((5, 5), 11)]);
assert!(ExtraCover::matching(&cover, &[((0, 0), 11), ((5, 5), 11)]).is_some());
assert!(ExtraCover::matching(&cover, &[((0, 0), 11), ((5, 6), 11)]).is_none());
// Too far apart for a mebibyte of grid, and one area alone: scans.
assert!(ExtraCover::new(&[((0, 0), 11), ((100_000, 0), 11)]).is_none());
assert!(ExtraCover::new(&[((0, 0), 11)]).is_none());
assert!(ExtraCover::new(&[((i32::MAX - 1, 0), 11), ((i32::MAX - 3, 0), 11)]).is_none());
assert!(ExtraCover::new(&[((0, i32::MAX - 1), 11), ((0, i32::MAX - 3), 11)]).is_none());
assert!(ExtraCover::new(&[((0, i32::MIN + 1), 11), ((0, i32::MIN + 3), 11)]).is_none());
}
/// cargo test --release -p horizon-client --lib cover_tests::cover_cost -- --ignored --nocapture
#[test]
#[ignore]
fn cover_cost() {
let forced = HashSet::new();
for players in [10usize, 25, 50] {
// The benchmark's bots: most near spawn, a fifth flying outwards.
let extra: Vec<_> = (0..players)
.map(|i| {
let a = i as f64 * 2.399;
let d = if i % 5 == 0 { 30.0 } else { 3.0 };
(((a.cos() * d) as i32, (a.sin() * d) as i32), 11)
})
.collect();
let cover = ExtraCover::new(&extra).unwrap();
let ranges = |cover| Ranges {
center: (0, 0),
radius: 10,
extra: &extra,
forced: &forced,
cover,
};
// The columns streaming asks about: everything kept, and the
// generation scan's ring past it.
let columns: Vec<(i32, i32)> = (-60..=60)
.flat_map(|z| (-60..=60).map(move |x| (x, z)))
.filter(|&(x, z)| {
ranges(None).requested(x, z, true) || !ranges(None).unloaded(x, z, true)
})
.collect();
let rounds = 100;
let time = |r: &Ranges| {
let t = std::time::Instant::now();
let mut n = 0usize;
for _ in 0..rounds {
for &(x, z) in &columns {
n += usize::from(r.unloaded(x, z, true));
n += usize::from(r.requested(x, z, true));
n += usize::from(r.in_range(x, z, 1));
}
}
std::hint::black_box(n);
t.elapsed().as_secs_f64() * 1e3 / f64::from(rounds)
};
let t = std::time::Instant::now();
for _ in 0..rounds {
std::hint::black_box(ExtraCover::new(std::hint::black_box(&extra)));
}
let build = t.elapsed().as_secs_f64() * 1e3 / f64::from(rounds);
println!(
"{players} players, {} columns, 3 questions each: scan {:.3} ms, grid {:.3} ms (+ {build:.3} ms to build)",
columns.len(),
time(&ranges(None)),
time(&ranges(Some(&cover))),
);
}
}
}
+224 -8
View File
@@ -45,13 +45,52 @@ pub struct Sources {
/// block ticking or lower; `None` ticks everywhere (a game with no server and
/// no player: the headless fixtures).
#[derive(Clone, Default)]
pub struct Levels(Option<Arc<FxHashMap<(i32, i32), (u8, u8)>>>);
pub struct Levels(Option<Arc<Map>>);
/// The levels by chunk: a grid over the tickets' bounding box while it is
/// small (players near each other, the usual case), a map otherwise. Every
/// block entity, mob and ticking chunk asks each tick, so an index beats a
/// hash; and a player crossing into another chunk rebuilds it all.
enum Map {
Grid {
x0: i32,
z0: i32,
width: u32,
depth: u32,
cells: Box<[(u8, u8)]>,
},
Sparse(FxHashMap<(i32, i32), (u8, u8)>),
}
/// The largest grid, in chunks: 1 MiB of levels, a box 724 chunks wide.
const GRID_CELLS: u64 = 1 << 19;
impl Map {
fn get(&self, x: i32, z: i32) -> (u8, u8) {
match self {
Self::Grid {
x0,
z0,
width,
depth,
cells,
} => {
let (dx, dz) = (x.wrapping_sub(*x0) as u32, z.wrapping_sub(*z0) as u32);
if dx < *width && dz < *depth {
cells[(dz * width + dx) as usize]
} else {
NONE
}
}
Self::Sparse(map) => map.get(&(x, z)).copied().unwrap_or(NONE),
}
}
}
impl Levels {
/// Chunk coordinates.
fn get(&self, x: i32, z: i32) -> Option<(u8, u8)> {
let map = self.0.as_ref()?;
Some(map.get(&(x, z)).copied().unwrap_or(NONE))
Some(self.0.as_ref()?.get(x, z))
}
/// `DistanceManager.inEntityTickingRange`, in an entity-ticking chunk:
/// where entities tick.
@@ -112,19 +151,89 @@ impl ChunkTickets {
}
}
fn compute(s: &Sources) -> FxHashMap<(i32, i32), (u8, u8)> {
/// How far a player's tickets reach, and the simulation ticket's level.
fn reach(s: &Sources) -> (i32, i32, i32) {
let view = s.view_distance.clamp(0, 32);
let simulation = s.simulation_distance.clamp(0, 32);
// PLAYER_LOADING within the view distance, PLAYER_SIMULATION at the
// player's chunk (`getPlayerTicketLevel`: never below 0).
let player_sim = (i32::from(ENTITY_TICKING) - simulation).max(0);
let reach = (view + 1).max(i32::from(BLOCK_TICKING) - player_sim);
(view, player_sim, reach)
}
fn compute(s: &Sources) -> Map {
let (view, player_sim, reach) = reach(s);
let boxes = s
.players
.iter()
.map(|&p| (p, reach))
.chain(s.forced.iter().map(|&f| (f, 1)));
let Some((x0, z0, x1, z1)) = boxes.fold(None, |b, ((x, z), r)| {
let (lo_x, lo_z, hi_x, hi_z) = (
i64::from(x) - i64::from(r),
i64::from(z) - i64::from(r),
i64::from(x) + i64::from(r),
i64::from(z) + i64::from(r),
);
Some(match b {
None => (lo_x, lo_z, hi_x, hi_z),
Some((a, b, c, d)) => (lo_x.min(a), lo_z.min(b), hi_x.max(c), hi_z.max(d)),
})
}) else {
return Map::Sparse(FxHashMap::default());
};
let (width, depth) = ((x1 - x0 + 1) as u64, (z1 - z0 + 1) as u64);
if width * depth > GRID_CELLS {
return Map::Sparse(compute_sparse(s));
}
let (x0, z0, width, depth) = (x0 as i32, z0 as i32, width as u32, depth as u32);
let mut cells = vec![NONE; (width * depth) as usize].into_boxed_slice();
let mut lower = |x: i32, z: i32, loading: i32, sim: i32| {
let i = ((z - z0) as u32 * width + (x - x0) as u32) as usize;
let e = &mut cells[i];
e.0 = e.0.min(loading.min(i32::from(NONE.0)) as u8);
e.1 = e.1.min(sim.min(i32::from(NONE.1)) as u8);
};
for &(px, pz) in &s.players {
for dz in -reach..=reach {
for dx in -reach..=reach {
let d = dx.abs().max(dz.abs());
lower(
px + dx,
pz + dz,
i32::from(ENTITY_TICKING) + (d - view).max(0),
player_sim + d,
);
}
}
}
for &(fx, fz) in &s.forced {
for dz in -1i32..=1 {
for dx in -1i32..=1 {
let level = i32::from(ENTITY_TICKING) + dx.abs().max(dz.abs());
lower(fx + dx, fz + dz, level, level);
}
}
}
Map::Grid {
x0,
z0,
width,
depth,
cells,
}
}
/// The levels as a map, for tickets spread too far apart for a grid.
fn compute_sparse(s: &Sources) -> FxHashMap<(i32, i32), (u8, u8)> {
let (view, player_sim, reach) = reach(s);
let mut levels: FxHashMap<(i32, i32), (u8, u8)> = FxHashMap::default();
let mut lower = |key, loading: i32, sim: i32| {
let e = levels.entry(key).or_insert(NONE);
e.0 = e.0.min(loading.min(i32::from(NONE.0)) as u8);
e.1 = e.1.min(sim.min(i32::from(NONE.1)) as u8);
};
// PLAYER_LOADING within the view distance, PLAYER_SIMULATION at the
// player's chunk (`getPlayerTicketLevel`: never below 0).
let player_sim = (i32::from(ENTITY_TICKING) - simulation).max(0);
let reach = (view + 1).max(i32::from(BLOCK_TICKING) - player_sim);
for &(px, pz) in &s.players {
for dz in -reach..=reach {
for dx in -reach..=reach {
@@ -229,4 +338,111 @@ mod tests {
t.update(None);
assert!(t.levels().0.is_none());
}
/// The grid answers as the map does, everywhere around the tickets.
#[test]
fn grid_matches_the_map() {
let mut seed = 0x2545_f491_4f6c_dd1du64;
let mut next = |n: i32| {
seed ^= seed << 13;
seed ^= seed >> 7;
seed ^= seed << 17;
(seed % (2 * n as u64 + 1)) as i32 - n
};
for case in 0..200 {
let spread = [3, 20, 80][case % 3];
let players: Vec<_> = (0..1 + case % 12)
.map(|_| (next(spread), next(spread)))
.collect();
let forced: Vec<_> = (0..case % 4)
.map(|_| (next(spread), next(spread)))
.collect();
let s = Sources {
players,
forced,
view_distance: 2 + next(12).abs(),
simulation_distance: 2 + next(12).abs(),
};
let Map::Grid { .. } = compute(&s) else {
panic!("case {case} fell back to the map");
};
let (grid, map) = (compute(&s), Map::Sparse(compute_sparse(&s)));
let r = spread + 50;
for z in -r..=r {
for x in -r..=r {
assert_eq!(grid.get(x, z), map.get(x, z), "case {case} at ({x}, {z})");
}
}
}
}
#[test]
fn far_apart_tickets_use_the_map() {
let l = levels(
&[(0, 0), (100_000, -100_000)],
&[(i32::MAX - 2, i32::MIN + 2)],
10,
10,
);
assert!(matches!(**l.0.as_ref().unwrap(), Map::Sparse(_)));
assert!(l.entity_ticking(100_010, -100_010) && !l.block_ticking(50_000, 0));
assert!(l.entity_ticking(i32::MAX - 2, i32::MIN + 2));
}
/// cargo test --release -p horizon-client --lib ticket_levels::tests::levels_cost -- --ignored --nocapture
#[test]
#[ignore]
fn levels_cost() {
for players in [1usize, 10, 25, 50] {
// The benchmark's bots: most near spawn, a fifth flying outwards.
let at: Vec<_> = (0..players)
.map(|i| {
let a = i as f64 * 2.399;
let d = if i % 5 == 0 { 30.0 } else { 3.0 };
((a.cos() * d) as i32, (a.sin() * d) as i32)
})
.collect();
let s = Sources {
players: at.clone(),
forced: Vec::new(),
view_distance: 10,
simulation_distance: 10,
};
let rounds = 200;
let t = std::time::Instant::now();
for _ in 0..rounds {
std::hint::black_box(compute_sparse(std::hint::black_box(&s)));
}
let map_build = t.elapsed().as_secs_f64() * 1e3 / rounds as f64;
let t = std::time::Instant::now();
for _ in 0..rounds {
std::hint::black_box(compute(std::hint::black_box(&s)));
}
let grid_build = t.elapsed().as_secs_f64() * 1e3 / rounds as f64;
// A tick's questions: every chunk within 12 of every player.
let (map, grid) = (Map::Sparse(compute_sparse(&s)), compute(&s));
let ask = |m: &Map| {
let t = std::time::Instant::now();
let mut n = 0u32;
for _ in 0..rounds {
for &(px, pz) in &at {
for z in pz - 12..=pz + 12 {
for x in px - 12..=px + 12 {
n +=
u32::from(std::hint::black_box(m.get(x, z)).0 <= BLOCK_TICKING);
}
}
}
}
std::hint::black_box(n);
t.elapsed().as_secs_f64() * 1e3 / rounds as f64
};
println!(
"{players} players: build {map_build:.3} -> {grid_build:.3} ms, {} lookups {:.3} -> {:.3} ms",
players * 625,
ask(&map),
ask(&grid)
);
}
}
}
+47 -1
View File
@@ -602,8 +602,18 @@ pub(crate) fn clamp(value: f64, min: f64, max: f64) -> f64 {
java_min(value, max)
}
}
/// `Mth.floor`, which is `(int) Math.floor(v)`: truncate, then step down for
/// negative fractions. A truncating conversion and a compare, where
/// `f64::floor` is a libm call on baseline x86-64. Both casts saturate (NaN is
/// 0), so the step down saturates too: below `i32::MIN` stays `i32::MIN`.
#[inline]
pub(crate) fn floor_i32(value: f64) -> i32 {
value.floor() as i32
let truncated = value as i32;
if value < f64::from(truncated) {
truncated.saturating_sub(1)
} else {
truncated
}
}
fn lerp(amount: f64, start: f64, end: f64) -> f64 {
start + amount * (end - start)
@@ -635,6 +645,42 @@ fn next_down(value: f64) -> f64 {
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn floor_i32_is_mths_floor() {
for v in [
0.0,
-0.0,
0.5,
-0.5,
1.0,
-1.0,
2.999,
-2.999,
1e-300,
-1e-300,
16_777_215.5,
-16_777_215.5,
// Past the i32 range `(int) Math.floor` saturates (vanilla 26.3's
// BlockCollisions and BlockPos.containing rely on it).
2_147_483_647.5,
2_147_483_648.0,
-2_147_483_648.0,
-2_147_483_648.5,
-2_147_483_649.0,
3e9,
-3e9,
f64::MAX,
f64::MIN,
f64::INFINITY,
f64::NEG_INFINITY,
] {
assert_eq!(floor_i32(v), v.floor() as i32, "{v}");
}
assert_eq!(floor_i32(f64::NAN), 0);
assert_eq!(floor_i32(-3e9), i32::MIN);
assert_eq!(floor_i32(-2_147_483_648.5), i32::MIN);
}
#[test]
fn ray_faces_and_half_open_bounds() {
let b = Aabb::block(BlockPos::new(0, 0, 0));
+20 -4
View File
@@ -61,8 +61,8 @@ impl GradientTable {
i32::from(self.permutations[(index & 255) as usize])
}
fn gradient(&self, index: i32) -> [i32; 3] {
GRADIENTS[(self.permute(index) & 15) as usize]
fn gradient(&self, index: i32) -> [f32; 3] {
PERLIN_GRADIENTS[(self.permute(index) & 15) as usize]
}
}
@@ -87,8 +87,24 @@ const GRADIENTS: [[i32; 3]; 16] = [
[0, -1, -1],
];
fn dot(gradient: [i32; 3], point: [f32; 3]) -> f32 {
gradient[0] as f32 * point[0] + gradient[1] as f32 * point[1] + gradient[2] as f32 * point[2]
/// [`GRADIENTS`] as floats, so a dot product needs no conversions: every
/// component is -1, 0 or 1, which a float holds exactly.
const PERLIN_GRADIENTS: [[f32; 3]; 16] = {
let mut out = [[0.0; 3]; 16];
let mut i = 0;
while i < 16 {
out[i] = [
GRADIENTS[i][0] as f32,
GRADIENTS[i][1] as f32,
GRADIENTS[i][2] as f32,
];
i += 1;
}
out
};
fn dot(gradient: [f32; 3], point: [f32; 3]) -> f32 {
gradient[0] * point[0] + gradient[1] * point[1] + gradient[2] * point[2]
}
/// Vanilla's periodic coordinate reduction with the normal Far Lands flag off.
+50 -22
View File
@@ -39,7 +39,7 @@ impl PerlinNoise {
)
}
fn gradients(&self, cell: [i32; 3]) -> [[i32; 3]; 8] {
fn gradients(&self, cell: [i32; 3]) -> [[f32; 3]; 8] {
self.gradients_at(
self.table.permute(cell[0]),
self.table.permute(cell[0].wrapping_add(1)),
@@ -52,7 +52,7 @@ impl PerlinNoise {
/// resolved. `add_volume` reuses one X column across every Y, so hoisting
/// the two X permutes out of that loop avoids repeating them per Y.
#[inline]
fn gradients_at(&self, p: i32, q: i32, cell_y: i32, cell_z: i32) -> [[i32; 3]; 8] {
fn gradients_at(&self, p: i32, q: i32, cell_y: i32, cell_z: i32) -> [[f32; 3]; 8] {
let lower = self.table.permute(p.wrapping_add(cell_y));
let upper = self.table.permute(p.wrapping_add(cell_y).wrapping_add(1));
let lower_x = self.table.permute(q.wrapping_add(cell_y));
@@ -66,7 +66,7 @@ impl PerlinNoise {
})
}
fn corner_values(gradients: [[i32; 3]; 8], point: [f32; 3]) -> [f32; 8] {
fn corner_values(gradients: [[f32; 3]; 8], point: [f32; 3]) -> [f32; 8] {
std::array::from_fn(|i| {
dot(
gradients[i],
@@ -99,7 +99,7 @@ impl PerlinNoise {
let values = Self::corner_values(gradients, point);
let fade = point.map(smoothstep);
let gradient =
std::array::from_fn::<_, 3, _>(|axis| lerp3(fade, gradients.map(|g| g[axis] as f32)));
std::array::from_fn::<_, 3, _>(|axis| lerp3(fade, gradients.map(|g| g[axis])));
let changes = [
lerp2(
fade[1],
@@ -164,6 +164,31 @@ impl PerlinNoise {
return Err(DensityError::BufferTooShort);
}
let [size_x, size_y, size_z] = volume.sizes();
// Every X/Z column walks the same Y coordinates, so their cells, offsets
// and fades are computed once per volume instead of once per column.
let mut stack = [YSample::default(); STACK_Y];
let mut heap = Vec::new();
let ys = if size_y <= STACK_Y {
&mut stack[..size_y]
} else {
heap.resize(size_y, YSample::default());
&mut heap[..]
};
for (y, sample) in ys.iter_mut().enumerate() {
let input_y = f64::from(volume.coordinate(1, y)) * y_scale;
let shifted_y = wrap(input_y) + self.table.offsets[1];
let cell = floor_i32(shifted_y);
let dy = shifted_y - f64::from(cell);
*sample = YSample {
cell,
fade: smoothstep(dy as f32),
offset: match fudge_y_scale {
Some(scale) => (dy - compute_fudge_y(input_y, dy, scale)) as f32,
None => dy as f32,
},
};
}
let ys = &*ys;
let mut index = 0;
for z in 0..size_z {
let shifted_z =
@@ -182,37 +207,40 @@ impl PerlinNoise {
let mut previous_y = i32::MIN;
let mut xz_dots = [0.0_f32; 8];
let mut y_gradients = [0.0_f32; 8];
for y in 0..size_y {
let input_y = f64::from(volume.coordinate(1, y)) * y_scale;
let shifted_y = wrap(input_y) + self.table.offsets[1];
let cell_y = floor_i32(shifted_y);
let dy = shifted_y - f64::from(cell_y);
let fade_y = smoothstep(dy as f32);
if previous_y != cell_y {
let gradients = self.gradients_at(p, q, cell_y, cell_z);
for (sample, out) in ys.iter().zip(&mut buffer[index..index + size_y]) {
if previous_y != sample.cell {
let gradients = self.gradients_at(p, q, sample.cell, cell_z);
for i in 0..8 {
xz_dots[i] = gradients[i][0] as f32 * (dx - (i & 1) as f32)
+ gradients[i][2] as f32 * (dz - ((i >> 2) & 1) as f32);
y_gradients[i] = gradients[i][1] as f32;
xz_dots[i] = gradients[i][0] * (dx - (i & 1) as f32)
+ gradients[i][2] * (dz - ((i >> 2) & 1) as f32);
y_gradients[i] = gradients[i][1];
}
previous_y = cell_y;
previous_y = sample.cell;
}
let dy = match fudge_y_scale {
Some(scale) => (dy - compute_fudge_y(input_y, dy, scale)) as f32,
None => dy as f32,
};
let dy = sample.offset;
let values = std::array::from_fn(|i| {
xz_dots[i] + y_gradients[i] * (dy - ((i >> 1) & 1) as f32)
});
buffer[index] += amplitude * lerp3([fade_x, fade_y, fade_z], values);
index += 1;
*out += amplitude * lerp3([fade_x, sample.fade, fade_z], values);
}
index += size_y;
}
}
Ok(())
}
}
/// Y columns up to this height keep their per-Y samples on the stack.
const STACK_Y: usize = 64;
#[derive(Clone, Copy, Default)]
struct YSample {
cell: i32,
fade: f32,
/// The Y offset inside the cell used for the corner values, after any fudge.
offset: f32,
}
#[derive(Clone, Debug)]
pub struct SmearedPerlinNoise {
base: PerlinNoise,
+134 -16
View File
@@ -577,14 +577,21 @@ impl PalettedContainer {
if list.len() == 1 << self.bits {
return Ok(());
}
for i in 0..self.strategy.entries() {
let index = read(&self.words, self.bits, i);
if index as usize >= list.len() {
return Err(PaletteError::BadIndex {
index,
palette: list.len(),
});
let per = 64 / usize::from(self.bits);
let mut remaining = self.strategy.entries();
let mask = mask(self.bits);
for &word in &self.words {
let count = remaining.min(per);
for slot in 0..count {
let index = ((word >> (slot * usize::from(self.bits))) & mask) as u32;
if index as usize >= list.len() {
return Err(PaletteError::BadIndex {
index,
palette: list.len(),
});
}
}
remaining -= count;
}
Ok(())
}
@@ -644,6 +651,18 @@ impl PalettedContainer {
actual: data.len(),
});
}
if let Config::List(bits) = config {
// List palettes already use the saved indices as resident storage.
// Validate them without allocating an unpacked copy that is unused.
let container = Self {
strategy,
bits,
words: data.to_vec(),
palette: Palette::List(palette),
};
container.check_indices()?;
return Ok(container);
}
let mut ids = Vec::with_capacity(entries);
for i in 0..entries {
let index = read(data, disc_bits, i);
@@ -655,15 +674,7 @@ impl PalettedContainer {
}
ids.push(index);
}
Ok(match config {
Config::List(bits) => Self {
strategy,
bits,
words: data.to_vec(),
palette: Palette::List(palette),
},
_ => Self::build(strategy, palette, &ids),
})
Ok(Self::build(strategy, palette, &ids))
}
/// Whether any palette entry satisfies `predicate` (`maybeHas`): entries
/// no longer used by any position still count.
@@ -796,6 +807,113 @@ mod tests {
assert!(PalettedContainer::from_wire(BLOCKS, 0, vec![9], vec![]).is_ok());
}
#[test]
fn saved_validation_matches_entrywise_reference() {
// Keep the previous loader as an independent entry-at-a-time reference:
// errors, resident palette order and unused storage bits must all agree.
fn reference(
strategy: Strategy,
palette: Vec<u32>,
data: Option<&[u64]>,
) -> Result<PalettedContainer, PaletteError> {
if palette.is_empty() {
return Err(PaletteError::BadPalette { len: 0 });
}
let config = strategy.config(ceil_log2(palette.len()));
let entries = strategy.entries();
let bits = match config {
Config::Single => return Ok(PalettedContainer::new(strategy, palette[0])),
Config::List(bits) => bits,
Config::Global => ceil_log2(palette.len()),
};
let expected = storage_len(bits, entries);
let data = data.ok_or(PaletteError::BadStorage {
expected,
actual: 0,
})?;
if data.len() != expected {
return Err(PaletteError::BadStorage {
expected,
actual: data.len(),
});
}
let mut ids = Vec::with_capacity(entries);
for i in 0..entries {
let index = read(data, bits, i);
if index as usize >= palette.len() {
return Err(PaletteError::BadIndex {
index,
palette: palette.len(),
});
}
ids.push(index);
}
Ok(match config {
Config::List(bits) => PalettedContainer {
strategy,
bits,
palette: Palette::List(palette),
words: data.to_vec(),
},
_ => PalettedContainer::build(strategy, palette, &ids),
})
}
fn compare(strategy: Strategy, palette: &[u32], data: Option<&[u64]>) {
let expected = reference(strategy, palette.to_vec(), data);
let actual = PalettedContainer::from_packed(strategy, palette.to_vec(), data);
match (actual, expected) {
(Ok(a), Ok(b)) => {
assert_eq!(a.strategy, b.strategy);
assert_eq!(a.bits, b.bits);
assert_eq!(a.palette, b.palette);
assert_eq!(a.words, b.words);
}
(Err(a), Err(b)) => assert_eq!(a, b),
(a, b) => panic!("loader disagreement: {a:?} vs {b:?}"),
}
}
for strategy in [BLOCKS, BIOMES] {
for len in [
0, 1, 2, 3, 4, 5, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, 129, 255,
256, 257, 511, 512, 513, 1023, 1024, 1025, 2047, 2048, 2049, 4096, 4097,
] {
let palette: Vec<u32> = (0..len).map(|i| (i as u32 * 7) % 127).collect();
compare(strategy, &palette, None);
compare(strategy, &palette, Some(&[]));
if len <= 1 {
compare(strategy, &palette, Some(&[u64::MAX]));
continue;
}
let bits = match strategy.config(ceil_log2(len)) {
Config::List(bits) => bits,
_ => ceil_log2(len),
};
let entries = strategy.entries();
let mut words = pack_words(bits, entries, (0..entries).map(|i| (i % len) as u32));
compare(strategy, &palette, Some(&words));
compare(strategy, &palette, Some(&words[..words.len() - 1]));
// Nonzero padding in every word and in the final partial word
// is legal and must be preserved by resident list palettes.
let per = 64 / usize::from(bits);
for (word, positions) in words.iter_mut().zip((0..entries).step_by(per)) {
let used = (entries - positions).min(per) * usize::from(bits);
if used < 64 {
*word |= u64::MAX << used;
}
}
compare(strategy, &palette, Some(&words));
if len < 1usize << bits {
for i in [0, per - 1, per, entries / 2, entries - 1] {
let mut invalid = words.clone();
write(&mut invalid, bits, i, len as u32);
// A second invalid entry verifies first-error ordering.
write(&mut invalid, bits, entries - 1, mask(bits) as u32);
compare(strategy, &palette, Some(&invalid));
}
}
}
}
}
#[test]
fn division_magic_is_exact() {
for bits in 1..=32u8 {
let per = 64 / usize::from(bits);
+88 -8
View File
@@ -41,11 +41,19 @@ fn javac_major(javac: &Path) -> Result<u32, String> {
.skip_while(|w| *w != "javac")
.nth(1)
.ok_or_else(|| {
format!(
"{} -version printed no version: {}",
javac.display(),
text.trim()
)
// macOS ships /usr/bin/javac as a placeholder that only says no Java is installed.
if text.contains("Unable to locate a Java Runtime") {
format!(
"{} is macOS's placeholder (no Java installed)",
javac.display()
)
} else {
format!(
"{} -version printed no version: {}",
javac.display(),
text.trim()
)
}
})?;
let mut parts = version.split(['.', '-', '+', '_']);
let first: u32 = parts.next().and_then(|p| p.parse().ok()).unwrap_or(0);
@@ -126,11 +134,38 @@ fn installed_jdks(parents: &[PathBuf], major: i64) -> Vec<PathBuf> {
homes
}
/// The Java runtimes the official launcher downloaded into `runtime` (a game directory's
/// `runtime` folder: `<component>/<platform>/<component>/`, a macOS one in `jre.bundle`). Mojang's
/// runtimes are whole JDKs, javac included, so a player who has run Minecraft has one already.
pub fn minecraft_runtimes(runtime: &Path) -> Vec<PathBuf> {
let children = |dir: &Path| -> Vec<PathBuf> {
let mut paths: Vec<PathBuf> = fs::read_dir(dir)
.map(|entries| entries.flatten().map(|e| e.path()).collect())
.unwrap_or_default();
paths.sort();
paths
};
let mut homes = Vec::new();
for component in children(runtime) {
for platform in children(&component) {
let home = platform.join(component.file_name().unwrap_or_default());
for home in [home.join("jre.bundle/Contents/Home"), home] {
if executable(&home.join("bin"), "javac").is_file() {
homes.push(home);
break;
}
}
}
}
homes
}
/// Finds a JDK with `javac` of at least `major`, the version research-lock.json pins:
/// `JAVA_HOME` when it holds one, else `javac` and `java` on the PATH (as the Python preparer
/// ran them), else, on macOS, the JDK `/usr/libexec/java_home -v <major>` reports, else a JDK
/// in the folders installers put them in (`jdk_parents`).
pub fn find(major: i64) -> Result<Jdk, String> {
/// ran them), else, on macOS, the JDK `/usr/libexec/java_home -v <major>` reports, else the JDK
/// homes in `more` (the launcher's pick, Minecraft's own runtimes), else a JDK in the folders
/// installers put them in (`jdk_parents`).
pub fn find(major: i64, more: &[PathBuf]) -> Result<Jdk, String> {
let need = format!(
"Java {major} is needed (a JDK with javac, e.g. Eclipse Temurin {major}). \
Install it, or set JAVA_HOME to it, and try again."
@@ -164,6 +199,14 @@ pub fn find(major: i64) -> Result<Jdk, String> {
}
}
}
for home in more {
let bin = home.join("bin");
candidates.push((
executable(&bin, "javac"),
executable(&bin, "java"),
"Minecraft's Java",
));
}
for home in installed_jdks(&jdk_parents(), major) {
let bin = home.join("bin");
candidates.push((
@@ -205,6 +248,15 @@ pub fn classpath<'a>(paths: impl IntoIterator<Item = &'a Path>) -> String {
}
impl Jdk {
/// The JDK's home, the folder above `bin` (what `JAVA_HOME` names).
pub fn home(&self) -> PathBuf {
self.javac
.parent()
.and_then(Path::parent)
.map(Path::to_path_buf)
.unwrap_or_default()
}
/// `javac --release <major> -proc:none` with Python's `subprocess.run(..., check=True,
/// stdout=log, stderr=STDOUT, timeout=3600)`.
pub fn javac(&self, major: i64, args: &[String], log: &fs::File) -> Result<(), String> {
@@ -301,4 +353,32 @@ mod tests {
);
fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn minecraft_runtimes_are_found_in_the_launchers_layout() {
let dir =
std::env::temp_dir().join(format!("horizon-prepare-runtimes-{}", std::process::id()));
let _ = fs::remove_dir_all(&dir);
let make = |home: PathBuf, javac: bool| {
fs::create_dir_all(home.join("bin")).unwrap();
if javac {
fs::write(executable(&home.join("bin"), "javac"), b"").unwrap();
}
home
};
let gamma = make(
dir.join("java-runtime-gamma/linux/java-runtime-gamma"),
true,
);
let epsilon = make(
dir.join(
"java-runtime-epsilon/mac-os-arm64/java-runtime-epsilon/jre.bundle/Contents/Home",
),
true,
);
make(dir.join("jre-legacy/linux/jre-legacy"), false);
assert_eq!(minecraft_runtimes(&dir), [epsilon, gamma]);
assert!(minecraft_runtimes(&dir.join("missing")).is_empty());
fs::remove_dir_all(&dir).unwrap();
}
}
+8 -1
View File
@@ -74,6 +74,9 @@ pub struct Options {
pub minecraft_dir: Option<PathBuf>,
/// The pinned server jar the player downloaded (`--server-jar`, else HORIZON_SERVER_JAR).
pub server_jar: Option<PathBuf>,
/// The JDK to try after JAVA_HOME and the PATH (the launcher's pick, e.g. Minecraft's
/// own Java it downloaded from Mojang).
pub java_home: Option<PathBuf>,
}
pub(crate) fn hex(bytes: &[u8]) -> String {
@@ -402,7 +405,11 @@ pub fn prepare(options: &Options, log: Log) -> Result<PathBuf, String> {
.map_err(|e| format!("{}: {e}", options.root.display()))?;
let lock = artifacts::Lock::read(root)?;
// Java first: nothing below can finish without it, and the player should hear it at once.
let jdk = java::find(lock.java_major)?;
let mut homes: Vec<PathBuf> = options.java_home.iter().cloned().collect();
homes.extend(java::minecraft_runtimes(
&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,
+1
View File
@@ -62,6 +62,7 @@ fn main() -> ExitCode {
import: false,
minecraft_dir: None,
server_jar: None,
java_home: None,
};
let mut root = None;
let mut args = std::env::args_os().skip(1).peekable();
+244 -3
View File
@@ -5,7 +5,7 @@
use crate::{
Result,
buf::{Codec, Reader, Writer},
item::{Slot, Stack},
item::{Slot, Stack, Template},
};
use horizon_world::nbt::Tag;
@@ -28,6 +28,150 @@ pub enum Value {
Id(i32),
/// Several VarInts (VillagerData).
Ids(Vec<i32>),
/// A particle choice: the `minecraft:particle_type` id plus its data.
/// Rendered nowhere (the client spawns no world particles yet); decoded
/// so the rest of an entry list survives a particle slot.
Particle(Particle),
/// A particle list (PARTICLES: a VarInt count, then each particle).
Particles(Vec<Particle>),
}
/// One `ParticleTypes.STREAM_CODEC` value: the `minecraft:particle_type` id,
/// then the data its type carries (26.3 registry order).
#[derive(Clone, Debug, PartialEq)]
pub struct Particle {
pub id: i32,
pub data: Option<ParticleData>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum ParticleData {
/// Block state id (block, block_marker, falling_dust, dust_pillar,
/// block_crumble).
State(i32),
/// ARGB color (entity_effect, tinted_leaves, flash).
Color(i32),
/// Dust: color then scale.
ColorScale(i32, f32),
/// Dust color transition: from color, to color, scale.
ColorScale2(i32, i32, f32),
/// Spell (effect, instant_effect): color then power.
ColorPower(i32, f32),
/// A bare float (dragon_breath power, sculk_charge roll).
Scalar(f32),
/// Delay in ticks (shriek).
Delay(i32),
/// Trail: target (3 doubles), ARGB color, duration.
Trail([f64; 3], i32, i32),
/// An item (ItemStackTemplate: item, count, patch).
Item(Stack),
/// Geyser and geyser_plume: water blocks.
Geyser(i32),
/// Geyser_base and geyser_poof: water blocks, then the burst impulse.
GeyserBase(i32, f32),
/// Vibration: where it travels to, then its arrival time in ticks.
Vibration { source: PositionSource, ticks: i32 },
}
/// `PositionSource.STREAM_CODEC`: a `minecraft:position_source_type` id
/// (block 0, entity 1), then that type's data.
#[derive(Clone, Debug, PartialEq)]
pub enum PositionSource {
Block([i32; 3]),
Entity { id: i32, y_offset: f32 },
}
impl Particle {
pub fn read(r: &mut Reader) -> Result<Particle> {
let id = r.varint()?;
// `minecraft:particle_type` registry order as of 26.3.
let data = match id {
1 | 2 | 36 | 121 | 125 => Some(ParticleData::State(r.varint()?)),
28 | 46 | 52 => Some(ParticleData::Color(r.i32()?)),
21 => {
let color = r.i32()?;
Some(ParticleData::ColorScale(color, r.f32()?))
}
22 => {
let from = r.i32()?;
let to = r.i32()?;
Some(ParticleData::ColorScale2(from, to, r.f32()?))
}
23 | 56 => {
let color = r.i32()?;
Some(ParticleData::ColorPower(color, r.f32()?))
}
15 | 48 => Some(ParticleData::Scalar(r.f32()?)),
115 => Some(ParticleData::Delay(r.varint()?)),
59 => {
let at = [r.f64()?, r.f64()?, r.f64()?];
let color = r.i32()?;
Some(ParticleData::Trail(at, color, r.varint()?))
}
57 => Some(ParticleData::Item(Template::read(r)?)),
58 => {
let source = match r.varint()? {
0 => PositionSource::Block(crate::buf::Pos::read(r)?),
1 => {
let id = r.varint()?;
PositionSource::Entity {
id,
y_offset: r.f32()?,
}
}
kind => return Err(crate::Error::new(format!("position source type {kind}"))),
};
Some(ParticleData::Vibration {
source,
ticks: r.varint()?,
})
}
7 | 10 => Some(ParticleData::Geyser(r.i32()?)),
8 | 9 => {
let water = r.i32()?;
Some(ParticleData::GeyserBase(water, r.f32()?))
}
// Every other registered particle carries no data.
_ => None,
};
Ok(Particle { id, data })
}
pub fn write(&self, w: &mut Writer) {
w.varint(self.id);
let Some(data) = &self.data else { return };
match data {
ParticleData::State(s) | ParticleData::Delay(s) => w.varint(*s),
ParticleData::Color(c) | ParticleData::Geyser(c) => w.i32(*c),
ParticleData::ColorScale(c, f)
| ParticleData::ColorPower(c, f)
| ParticleData::GeyserBase(c, f) => {
w.i32(*c);
w.f32(*f);
}
ParticleData::ColorScale2(a, b, f) => {
w.i32(*a);
w.i32(*b);
w.f32(*f);
}
ParticleData::Scalar(f) => w.f32(*f),
ParticleData::Trail(at, c, d) => {
at.iter().for_each(|v| w.f64(*v));
w.i32(*c);
w.varint(*d);
}
ParticleData::Item(s) => Template::write(s, w),
ParticleData::Vibration { source, ticks } => {
match source {
PositionSource::Block(pos) => {
w.varint(0);
crate::buf::Pos::write(pos, w);
}
PositionSource::Entity { id, y_offset } => {
w.varint(1);
w.varint(*id);
w.f32(*y_offset);
}
}
w.varint(*ticks);
}
}
}
}
/// Serializer ids in EntityDataSerializers registration order.
pub mod serializer {
@@ -113,6 +257,15 @@ fn read_value(kind: i32, r: &mut Reader) -> Result<Option<Value>> {
| PAINTING_VARIANT..=WEATHERING_COPPER_STATE
| HUMANOID_ARM
| DYE_COLOR => Value::Id(r.varint()?),
PARTICLE => Value::Particle(Particle::read(r)?),
PARTICLES => {
let n = r.count(65536)?;
let mut list = Vec::with_capacity(n.min(64));
for _ in 0..n {
list.push(Particle::read(r)?);
}
Value::Particles(list)
}
_ => return Ok(None),
}))
}
@@ -147,6 +300,11 @@ fn write_value(kind: i32, v: &Value, w: &mut Writer) {
w.uuid(*u);
}
}
Value::Particle(p) => p.write(w),
Value::Particles(list) => {
w.varint(list.len() as i32);
list.iter().for_each(|p| p.write(w));
}
}
let _ = kind;
}
@@ -208,10 +366,93 @@ mod tests {
EntityData::read(&mut Reader::new(&w.data)).unwrap(),
entries
);
// A particle (16) cannot be skipped: keep what came before.
let mut bytes = vec![0, 0, 7, 9, 16, 1, 2, 3];
// Serializer 200 is unknown: the list stops there, earlier entries kept.
let mut bytes = vec![0, 0, 7, 9, 200, 1, 2, 3];
bytes.push(0xFF);
let read = EntityData::read(&mut Reader::new(&bytes)).unwrap();
assert_eq!(read, vec![(0, 0, Value::Byte(7))]);
// A block particle decodes fully and round-trips.
let entries = vec![(
10u8,
serializer::PARTICLE,
Value::Particle(Particle {
id: 1,
data: Some(ParticleData::State(2)),
}),
)];
let mut w = Writer::new();
EntityData::write(&entries, &mut w);
assert_eq!(
EntityData::read(&mut Reader::new(&w.data)).unwrap(),
entries
);
}
/// Every particle data shape (26.3 codecs) round-trips, and a PARTICLES
/// list is a VarInt count then each particle, so the entry after it
/// still decodes.
#[test]
fn particles_round_trip() {
let all = [
(1, Some(ParticleData::State(2))),
(7, Some(ParticleData::Geyser(5))),
(8, Some(ParticleData::GeyserBase(3, 0.5))),
(15, Some(ParticleData::Scalar(1.0))),
(21, Some(ParticleData::ColorScale(0xff0000, 1.5))),
(22, Some(ParticleData::ColorScale2(1, 2, 0.75))),
(23, Some(ParticleData::ColorPower(-1, 2.0))),
(28, Some(ParticleData::Color(0x7f00ff00))),
(
57,
Some(ParticleData::Item(Stack {
item: 42,
count: 3,
added: Vec::new(),
removed: vec![7],
})),
),
(
58,
Some(ParticleData::Vibration {
source: PositionSource::Block([-12, 64, 300]),
ticks: 20,
}),
),
(
58,
Some(ParticleData::Vibration {
source: PositionSource::Entity {
id: 99,
y_offset: 1.25,
},
ticks: 8,
}),
),
(59, Some(ParticleData::Trail([1.5, -2.0, 3.25], 9, 40))),
(115, Some(ParticleData::Delay(6))),
(0, None),
]
.map(|(id, data)| Particle { id, data });
let mut entries: Vec<_> = all
.iter()
.map(|p| (10u8, serializer::PARTICLE, Value::Particle(p.clone())))
.collect();
entries.push((11, serializer::PARTICLES, Value::Particles(all.to_vec())));
entries.push((12, serializer::BYTE, Value::Byte(4)));
let mut w = Writer::new();
EntityData::write(&entries, &mut w);
assert_eq!(
EntityData::read(&mut Reader::new(&w.data)).unwrap(),
entries
);
// Wire shape: an empty list is one 0 byte, then the next entry.
let bytes = [11, serializer::PARTICLES as u8, 0, 12, 0, 4, 0xFF];
assert_eq!(
EntityData::read(&mut Reader::new(&bytes)).unwrap(),
vec![
(11, serializer::PARTICLES, Value::Particles(Vec::new())),
(12, serializer::BYTE, Value::Byte(4)),
]
);
}
}
+134
View File
@@ -25,7 +25,10 @@ pub mod component {
pub const REPAIR_COST: i32 = 19;
pub const CREATIVE_SLOT_LOCK: i32 = 20;
pub const ENCHANTMENT_GLINT_OVERRIDE: i32 = 21;
/// `AttackRange`: six floats, in the record's field order.
pub const ATTACK_RANGE: i32 = 30;
pub const STORED_ENCHANTMENTS: i32 = 45;
pub const PROFILE: i32 = 72;
pub const DYED_COLOR: i32 = 47;
pub const MAP_ID: i32 = 48;
pub const MAP_POST_PROCESSING: i32 = 50;
@@ -57,6 +60,8 @@ pub enum Component {
Ident(String),
Lore(Vec<Tag>),
Enchantments(Vec<(i32, i32)>),
AttackRange([f32; 6]),
Profile(Profile),
CustomModelData {
floats: Vec<f32>,
flags: Vec<bool>,
@@ -81,6 +86,15 @@ fn filterable<T>(r: &mut Reader, value: impl Fn(&mut Reader) -> Result<T>) -> Re
}
Ok(raw)
}
/// `ResolvableProfile`: an optional name and UUID and the profile's property
/// list, followed by `PlayerSkin.Patch`'s four optional textures.
#[derive(Clone, Debug, PartialEq)]
pub struct Profile {
pub name: Option<String>,
pub id: Option<u128>,
pub properties: Vec<(String, String, Option<String>)>,
}
impl Component {
fn read(kind: i32, r: &mut Reader) -> Result<Self> {
use component::*;
@@ -129,6 +143,56 @@ impl Component {
.collect::<Result<_>>()?,
)
}
ATTACK_RANGE => {
let mut values = [0.0f32; 6];
for v in &mut values {
*v = r.f32()?;
}
Self::AttackRange(values)
}
PROFILE => {
fn properties(r: &mut Reader) -> Result<Vec<(String, String, Option<String>)>> {
let n = r.count(1024)?;
let mut out = Vec::with_capacity(n);
for _ in 0..n {
let name = r.string(32767)?;
let value = r.string(32767)?;
let signature = if r.bool()? {
Some(r.string(32767)?)
} else {
None
};
out.push((name, value, signature));
}
Ok(out)
}
// Either<GameProfile, Partial>: the partial form carries
// optional fields, the resolved one a UUID, a name and the
// properties.
let (name, id, properties) = if r.u8()? == 0 {
let name = if r.bool()? { Some(r.string(64)?) } else { None };
let id = if r.bool()? { Some(r.uuid()?) } else { None };
(name, id, properties(r)?)
} else {
let id = Some(r.uuid()?);
let name = Some(r.string(64)?);
(name, id, properties(r)?)
};
// PlayerSkin.Patch: body, cape, elytra and model, each absent
// in the profiles servers send for heads.
for _ in 0..4 {
if r.bool()? {
// ClientAsset.ResourceTexture: its id and its texture path.
r.string(32767)?;
r.string(32767)?;
}
}
Self::Profile(Profile {
name,
id,
properties,
})
}
CUSTOM_MODEL_DATA => {
let n = r.count(r.remaining())?;
let floats = (0..n).map(|_| r.f32()).collect::<Result<_>>()?;
@@ -175,6 +239,30 @@ impl Component {
w.varint(*level);
}
}
Self::AttackRange(values) => values.iter().for_each(|v| w.f32(*v)),
Self::Profile(p) => {
w.u8(0);
w.bool(p.name.is_some());
if let Some(name) = &p.name {
w.string(name);
}
w.bool(p.id.is_some());
if let Some(id) = p.id {
w.uuid(id);
}
w.varint(p.properties.len() as i32);
for (name, value, signature) in &p.properties {
w.string(name);
w.string(value);
w.bool(signature.is_some());
if let Some(signature) = signature {
w.string(signature);
}
}
for _ in 0..4 {
w.bool(false);
}
}
Self::CustomModelData {
floats,
flags,
@@ -471,4 +559,50 @@ mod tests {
let bytes = [1, 5, 1, 0, 99];
assert!(Slot::read(&mut Reader::new(&bytes)).is_err());
}
#[test]
fn attack_range_and_profiles_round_trip() {
let mut sword = Stack::new(900, 1);
sword.added.push((
component::ATTACK_RANGE,
Component::AttackRange([0.0, 3.0, 0.0, 4.0, 0.1, 1.0]),
));
sword.added.push((
component::PROFILE,
Component::Profile(Profile {
name: Some("Player53470".into()),
id: Some(0x7a5d_6a1a_30d7_4187_88c5_efe9_1201_8922),
properties: vec![("textures".into(), "dGV4dHVyZXM=".into(), None)],
}),
));
round::<Option<Stack>, Slot>(Some(sword));
}
#[test]
fn a_partial_profile_decodes_from_the_wire() {
// count 1, item 1, one added component of kind 72 (profile): a partial
// profile with an empty name, a UUID, one unsigned texture property
// and an empty skin patch.
let uuid = [
0x7a, 0x5d, 0x6a, 0x1a, 0x30, 0xd7, 0x41, 0x87, 0x88, 0xc5, 0xef, 0xe9, 0x12, 0x01,
0x89, 0x22,
];
let mut bytes = vec![1, 1, 1, 0, 72, 0, 1, 0, 1];
bytes.extend_from_slice(&uuid);
bytes.extend_from_slice(&[1, 8]);
bytes.extend_from_slice(b"textures");
bytes.extend_from_slice(&[1, b'x', 0, 0, 0, 0, 0]);
let mut r = Reader::new(&bytes);
let stack = Slot::read(&mut r).unwrap().unwrap();
assert_eq!(r.remaining(), 0);
let Some(Component::Profile(profile)) = stack.component(component::PROFILE) else {
panic!("the profile component did not decode");
};
assert_eq!(profile.name.as_deref(), Some(""));
assert_eq!(profile.id, Some(0x7a5d_6a1a_30d7_4187_88c5_efe9_1201_8922));
assert_eq!(
profile.properties,
[("textures".to_owned(), "x".to_owned(), None)]
);
}
}
@@ -273,6 +273,68 @@ packet!(Login {
online_mode: bool => Bool,
enforces_secure_chat: bool => Bool,
});
packet!(
/// MapDecoration: a registry holder, its map position and rotation, and an
/// optional name.
MapDecoration {
kind: i32 => VarInt,
x: i8 => Byte,
y: i8 => Byte,
rot: i8 => Byte,
name: Option<Tag> => Opt<Nbt>,
}
);
/// MapItemSavedData.MapPatch: an empty patch is a zero width.
#[derive(Clone, Debug, PartialEq)]
pub struct MapPatch {
pub start_x: i32,
pub start_y: i32,
pub width: i32,
pub height: i32,
pub colors: Vec<u8>,
}
pub struct MapPatchOpt;
impl Codec<Option<MapPatch>> for MapPatchOpt {
fn read(r: &mut Reader) -> Result<Option<MapPatch>> {
let width = i32::from(r.u8()?);
if width == 0 {
return Ok(None);
}
let height = i32::from(r.u8()?);
let start_x = i32::from(r.u8()?);
let start_y = i32::from(r.u8()?);
let n = r.count(r.remaining())?;
let colors = r.bytes(n)?.to_vec();
Ok(Some(MapPatch {
start_x,
start_y,
width,
height,
colors,
}))
}
fn write(patch: &Option<MapPatch>, w: &mut Writer) {
let Some(patch) = patch else {
w.u8(0);
return;
};
w.u8(patch.width as u8);
w.u8(patch.height as u8);
w.u8(patch.start_x as u8);
w.u8(patch.start_y as u8);
w.varint(patch.colors.len() as i32);
w.bytes(&patch.colors);
}
}
packet!(MapItemData {
map_id: i32 => VarInt,
scale: i8 => Byte,
locked: bool => Bool,
decorations: Option<Vec<MapDecoration>> => Opt<List<MapDecoration>>,
patch: Option<MapPatch> => MapPatchOpt,
});
/// VecDelta: per-axis deltas in 1/4096 blocks, linear or in timed steps.
#[derive(Clone, Debug, PartialEq)]
pub enum VecDelta {
@@ -1425,6 +1487,7 @@ packet_enum!(Clientbound, crate::ids::play::clientbound {
LevelEvent(LevelEvent) = LEVEL_EVENT,
LightUpdate(LightUpdate) = LIGHT_UPDATE,
Login(Login) = LOGIN,
MapItemData(MapItemData) = MAP_ITEM_DATA,
MoveEntityPos(MoveEntityPos) = MOVE_ENTITY_POS,
MoveEntityPosRot(MoveEntityPosRot) = MOVE_ENTITY_POS_ROT,
MoveEntityRot(MoveEntityRot) = MOVE_ENTITY_ROT,
@@ -2490,4 +2553,26 @@ mod tests {
assert_eq!(crate::ids::play::clientbound::UPDATE_ATTRIBUTES, 134);
assert_eq!(Clientbound::decode(134, &body[2..]).unwrap(), p);
}
#[test]
fn map_item_data_carries_a_server_patch() {
let patch = MapPatch {
start_x: 3,
start_y: 4,
width: 2,
height: 1,
colors: vec![1, 2],
};
let mut w = Writer::new();
MapPatchOpt::write(&Some(patch.clone()), &mut w);
assert_eq!(w.data, [2, 1, 3, 4, 2, 1, 2]);
let p = Clientbound::MapItemData(MapItemData {
map_id: 2146483647,
scale: 1,
locked: true,
decorations: None,
patch: Some(patch),
});
round_trip(p, Clientbound::encode, Clientbound::decode);
}
}
@@ -120,6 +120,7 @@ pub struct NoiseBasedAquifer<P, F, B> {
statuses: Vec<Option<FluidStatus>>,
locations: Vec<i64>,
surfaces: HashMap<i64, i32>,
neighborhood: Option<([i32; 3], [(usize, BlockPos); 12])>,
skip_above_y: i32,
schedule: bool,
}
@@ -180,6 +181,7 @@ impl<P: FluidPicker, F: PositionalRandom, B: AquiferBlockStates> NoiseBasedAquif
statuses,
locations,
surfaces: HashMap::new(),
neighborhood: None,
skip_above_y: 0,
schedule: false,
};
@@ -479,48 +481,76 @@ impl<P: FluidPicker, F: PositionalRandom, B: AquiferBlockStates> NoiseBasedAquif
y.wrapping_add(1).div_euclid(12),
z.wrapping_sub(5) >> 4,
];
let mut distances = [i32::MAX; 4];
let mut indices = [0usize; 4];
for dx in 0..=1i32 {
for dy in -1..=1i32 {
for dz in 0..=1i32 {
let cell = [
grid[0].wrapping_add(dx),
grid[1].wrapping_add(dy),
grid[2].wrapping_add(dz),
];
let index = self.grid_index(cell)?;
if self.locations[index] == i64::MAX {
let mut random = self.random.at(BlockPos::new(cell[0], cell[1], cell[2]));
let px = cell[0]
.wrapping_shl(4)
.wrapping_add(random.next_int_bound(10).expect("positive bound"));
let py = cell[1]
.wrapping_mul(12)
.wrapping_add(random.next_int_bound(9).expect("positive bound"));
let pz = cell[2]
.wrapping_shl(4)
.wrapping_add(random.next_int_bound(10).expect("positive bound"));
self.locations[index] = BlockPos::new(px, py, pz).pack();
}
let point = BlockPos::unpack(self.locations[index]);
let ax = point.x.wrapping_sub(x);
let ay = point.y.wrapping_sub(y);
let az = point.z.wrapping_sub(z);
let distance = ax
.wrapping_mul(ax)
.wrapping_add(ay.wrapping_mul(ay))
.wrapping_add(az.wrapping_mul(az));
if let Some(slot) = distances.iter().position(|old| distance <= *old) {
for i in (slot + 1..4).rev() {
distances[i] = distances[i - 1];
indices[i] = indices[i - 1];
// Fill in the original cell order, publishing only after checked traversal
// succeeds. Failed traversal keeps the last complete neighborhood.
if self.neighborhood.as_ref().map(|(key, _)| *key) != Some(grid) {
let mut entries = [(0, BlockPos::default()); 12];
let mut next = 0;
for dx in 0..=1i32 {
for dy in -1..=1i32 {
for dz in 0..=1i32 {
let cell = [
grid[0].wrapping_add(dx),
grid[1].wrapping_add(dy),
grid[2].wrapping_add(dz),
];
let index = self.grid_index(cell)?;
if self.locations[index] == i64::MAX {
let mut random =
self.random.at(BlockPos::new(cell[0], cell[1], cell[2]));
let px = cell[0]
.wrapping_shl(4)
.wrapping_add(random.next_int_bound(10).expect("positive bound"));
let py = cell[1]
.wrapping_mul(12)
.wrapping_add(random.next_int_bound(9).expect("positive bound"));
let pz = cell[2]
.wrapping_shl(4)
.wrapping_add(random.next_int_bound(10).expect("positive bound"));
self.locations[index] = BlockPos::new(px, py, pz).pack();
}
distances[slot] = distance;
indices[slot] = index;
entries[next] = (index, BlockPos::unpack(self.locations[index]));
next += 1;
}
}
}
self.neighborhood = Some((grid, entries));
}
let mut distances = [i32::MAX; 4];
let mut indices = [0usize; 4];
for &(index, point) in &self
.neighborhood
.as_ref()
.expect("populated neighborhood")
.1
{
let ax = point.x.wrapping_sub(x);
let ay = point.y.wrapping_sub(y);
let az = point.z.wrapping_sub(z);
let distance = ax
.wrapping_mul(ax)
.wrapping_add(ay.wrapping_mul(ay))
.wrapping_add(az.wrapping_mul(az));
// Equal distances insert before existing entries, as in vanilla.
if distance <= distances[0] {
distances = [distance, distances[0], distances[1], distances[2]];
indices = [index, indices[0], indices[1], indices[2]];
} else if distance <= distances[1] {
distances[3] = distances[2];
distances[2] = distances[1];
distances[1] = distance;
indices[3] = indices[2];
indices[2] = indices[1];
indices[1] = index;
} else if distance <= distances[2] {
distances[3] = distances[2];
distances[2] = distance;
indices[3] = indices[2];
indices[2] = index;
} else if distance <= distances[3] {
distances[3] = distance;
indices[3] = index;
}
}
let a = self.status(context, indices[0])?;
let s01 = similarity(distances[0], distances[1]);
@@ -123,10 +123,12 @@ pub struct BiomeTemperature {
}
impl BiomeTemperature {
pub fn new(settings: BiomeTemperatureSettings) -> Self {
// Empty until used: a decoration region makes one per biome, and
// most biomes never ask for a temperature.
Self {
settings,
values: HashMap::with_capacity(1024),
order: VecDeque::with_capacity(1024),
values: HashMap::new(),
order: VecDeque::new(),
}
}
pub fn settings(&self) -> BiomeTemperatureSettings {
@@ -202,8 +202,11 @@ fn build(
#[derive(Debug)]
pub struct ClimateIndex<T> {
entries: Vec<(ParameterPoint, T)>,
nodes: Vec<Node>,
root: usize,
/// The built tree renumbered breadth first, root at 0, so each branch's
/// children are one contiguous run and a search walks memory in order.
spaces: Vec<[Parameter; 7]>,
/// Per node: its first child and child count, or its entry and 0 for a leaf.
links: Vec<(u32, u32)>,
}
impl<T> ClimateIndex<T> {
pub fn new(entries: Vec<(ParameterPoint, T)>) -> Result<Self, IndexError> {
@@ -232,10 +235,25 @@ impl<T> ClimateIndex<T> {
.collect();
let children = (0..nodes.len()).collect();
let root = build(&mut nodes, children, branching)?;
let mut order = vec![root];
let mut links = Vec::with_capacity(nodes.len());
let mut next = 0;
while next < order.len() {
let node = &nodes[order[next]];
next += 1;
links.push(match &node.children {
Children::Leaf(entry) => (*entry as u32, 0),
Children::Branch(children) => {
let first = order.len() as u32;
order.extend_from_slice(children);
(first, children.len() as u32)
}
});
}
Ok(Self {
entries,
nodes,
root,
spaces: order.iter().map(|id| nodes[*id].space).collect(),
links,
})
}
pub fn entries(&self) -> &[(ParameterPoint, T)] {
@@ -263,18 +281,18 @@ impl<T> ClimateIndex<T> {
}
/// Preorder leaf entry indices expose stable tree layout for comparison.
pub fn leaf_order(&self) -> Vec<usize> {
fn walk(nodes: &[Node], id: usize, out: &mut Vec<usize>) {
match &nodes[id].children {
Children::Leaf(entry) => out.push(*entry),
Children::Branch(children) => {
for child in children {
walk(nodes, *child, out);
fn walk(links: &[(u32, u32)], id: usize, out: &mut Vec<usize>) {
match links[id] {
(entry, 0) => out.push(entry as usize),
(first, count) => {
for child in first..first + count {
walk(links, child as usize, out);
}
}
}
}
let mut result = Vec::with_capacity(self.entries.len());
walk(&self.nodes, self.root, &mut result);
walk(&self.links, 0, &mut result);
result
}
fn search(
@@ -283,19 +301,20 @@ impl<T> ClimateIndex<T> {
values: &[i64; 7],
previous: Option<usize>,
) -> Result<usize, IndexError> {
let Children::Branch(children) = &self.nodes[id].children else {
let (first, count) = self.links[id];
if count == 0 {
return Ok(id);
};
}
let mut best = previous;
let mut fitness = previous.map_or(i64::MAX, |id| distance(&self.nodes[id].space, values));
for child in children {
let lower = distance(&self.nodes[*child].space, values);
let mut fitness = previous.map_or(i64::MAX, |id| distance(&self.spaces[id], values));
for child in first as usize..(first + count) as usize {
let lower = distance(&self.spaces[child], values);
if fitness > lower {
let result = self.search(*child, values, best)?;
let next = if result == *child {
let result = self.search(child, values, best)?;
let next = if result == child {
lower
} else {
distance(&self.nodes[result].space, values)
distance(&self.spaces[result], values)
};
if fitness > next {
fitness = next;
@@ -313,13 +332,11 @@ pub struct ClimateSearch<'a, T> {
}
impl<'a, T> ClimateSearch<'a, T> {
pub fn find(&mut self, target: TargetPoint) -> Result<&'a T, IndexError> {
let result = self
.index
.search(self.index.root, &target.parameters(), self.last)?;
let result = self.index.search(0, &target.parameters(), self.last)?;
self.last = Some(result);
let Children::Leaf(entry) = self.index.nodes[result].children else {
let (entry, 0) = self.index.links[result] else {
unreachable!("search returns a leaf");
};
Ok(&self.index.entries[entry].1)
Ok(&self.index.entries[entry as usize].1)
}
}
@@ -22,7 +22,7 @@ use horizon_core::{
},
};
use std::{
collections::{BTreeMap, BTreeSet},
collections::{BTreeMap, BTreeSet, HashMap},
sync::Arc,
};
@@ -58,7 +58,10 @@ impl From<RegionError> for PlacementQueryError {
}
}
pub struct TerrainRegion<'r, B: TerrainBlockStates> {
chunks: BTreeMap<(i32, i32), TerrainChunk<'r, B>>,
/// Ordered by X then Z.
chunks: Vec<TerrainChunk<'r, B>>,
/// Each chunk's index in `chunks`: every block access looks one up.
index: ChunkIndex,
min_y: i32,
height: i32,
generation: WorldGenerationContext,
@@ -68,6 +71,77 @@ pub struct TerrainRegion<'r, B: TerrainBlockStates> {
block_queries: Option<RegionBlockQueries>,
world_seed: Option<i64>,
}
/// Where each chunk of a region sits in its list: a grid over the chunks'
/// bounding box while it is small (the usual square of neighbours), else a
/// map (chunks spread far apart).
enum ChunkIndex {
Grid {
origin: (i32, i32),
columns: i64,
rows: i64,
/// Row by row from `origin`; `u32::MAX` where no chunk is.
cells: Vec<u32>,
},
Sparse(HashMap<(i32, i32), u32>),
}
impl ChunkIndex {
/// The largest grid, in chunk columns.
const GRID_CELLS: i64 = 1 << 20;
/// `None` when two chunks share a position.
fn new(positions: &[(i32, i32)]) -> Option<Self> {
let x0 = positions.iter().map(|p| p.0).min()?;
let z0 = positions.iter().map(|p| p.1).min()?;
let columns = i64::from(positions.iter().map(|p| p.0).max()?) - i64::from(x0) + 1;
let rows = i64::from(positions.iter().map(|p| p.1).max()?) - i64::from(z0) + 1;
if columns * rows > Self::GRID_CELLS {
let mut map = HashMap::with_capacity(positions.len());
for (i, &p) in positions.iter().enumerate() {
if map.insert(p, i as u32).is_some() {
return None;
}
}
return Some(Self::Sparse(map));
}
let mut cells = vec![u32::MAX; (columns * rows) as usize];
for (i, &(x, z)) in positions.iter().enumerate() {
let cell = (i64::from(z) - i64::from(z0)) * columns + (i64::from(x) - i64::from(x0));
let cell = &mut cells[cell as usize];
if *cell != u32::MAX {
return None;
}
*cell = i as u32;
}
Some(Self::Grid {
origin: (x0, z0),
columns,
rows,
cells,
})
}
fn get(&self, x: i32, z: i32) -> Option<usize> {
match self {
Self::Grid {
origin,
columns,
rows,
cells,
} => {
let (dx, dz) = (
i64::from(x) - i64::from(origin.0),
i64::from(z) - i64::from(origin.1),
);
if !(0..*columns).contains(&dx) || !(0..*rows).contains(&dz) {
return None;
}
match cells[(dz * columns + dx) as usize] {
u32::MAX => None,
i => Some(i as usize),
}
}
Self::Sparse(map) => map.get(&(x, z)).map(|&i| i as usize),
}
}
}
impl<'r, B: TerrainBlockStates> TerrainRegion<'r, B> {
pub fn new(
chunks: Vec<TerrainChunk<'r, B>>,
@@ -83,15 +157,16 @@ impl<'r, B: TerrainBlockStates> TerrainRegion<'r, B> {
}) {
return Err(RegionError::InvalidChunks);
}
let mut stored = BTreeMap::new();
for chunk in chunks {
let p = chunk.position();
if stored.insert((p.x, p.z), chunk).is_some() {
return Err(RegionError::InvalidChunks);
}
}
let mut chunks = chunks;
chunks.sort_by_key(|c| (c.position().x, c.position().z));
let positions: Vec<(i32, i32)> = chunks
.iter()
.map(|c| (c.position().x, c.position().z))
.collect();
let index = ChunkIndex::new(&positions).ok_or(RegionError::InvalidChunks)?;
Ok(Self {
chunks: stored,
chunks,
index,
sea_level: generator.sea_level,
game_time: 0,
next_tick_order: 0,
@@ -114,10 +189,7 @@ impl<'r, B: TerrainBlockStates> TerrainRegion<'r, B> {
mut self,
bindings: &super::block_feature::FeatureBlockBindings<'r>,
) -> Result<Self, RegionError> {
if !std::ptr::eq(
self.chunks.first_key_value().unwrap().1.registry(),
bindings.registry(),
) {
if !std::ptr::eq(self.chunks[0].registry(), bindings.registry()) {
return Err(RegionError::InvalidBindings);
}
self.block_queries = Some(RegionBlockQueries {
@@ -186,20 +258,25 @@ impl<'r, B: TerrainBlockStates> TerrainRegion<'r, B> {
}
/// Release the resident chunks, ordered by X then Z; match them by position.
pub fn into_chunks(self) -> Vec<TerrainChunk<'r, B>> {
self.chunks.into_values().collect()
self.chunks
}
pub fn chunks(&self) -> impl Iterator<Item = &TerrainChunk<'r, B>> {
self.chunks.values()
self.chunks.iter()
}
fn index(&self, x: i32, z: i32) -> Option<usize> {
self.index.get(x, z)
}
pub fn chunk(&self, x: i32, z: i32) -> Result<&TerrainChunk<'r, B>, RegionError> {
self.chunks
.get(&(x, z))
.ok_or(RegionError::MissingChunk([x, z]))
match self.index(x, z) {
Some(i) => Ok(&self.chunks[i]),
None => Err(RegionError::MissingChunk([x, z])),
}
}
pub fn chunk_mut(&mut self, x: i32, z: i32) -> Result<&mut TerrainChunk<'r, B>, RegionError> {
self.chunks
.get_mut(&(x, z))
.ok_or(RegionError::MissingChunk([x, z]))
match self.index(x, z) {
Some(i) => Ok(&mut self.chunks[i]),
None => Err(RegionError::MissingChunk([x, z])),
}
}
pub fn block(&self, [x, y, z]: [i32; 3]) -> Result<BlockStateId, RegionError> {
Ok(self.chunk(x >> 4, z >> 4)?.block(x, y, z))
@@ -302,12 +379,7 @@ impl<B: TerrainBlockStates> PlacementContext for TerrainRegion<'_, B> {
block: BlockStateId,
delay: i32,
) -> Result<(), PlacementQueryError> {
let registry = self
.chunks
.first_key_value()
.expect("nonempty region")
.1
.registry();
let registry = self.chunks[0].registry();
let state = registry
.state(block)
.ok_or(RegionError::InvalidTickState(block))?;
@@ -1129,4 +1129,41 @@ impl PlacementContext for World<'_> {
fn height(&mut self, kind: HeightmapType, x: i32, z: i32) -> Result<i32, PlacementQueryError> {
self.region.height(kind, x, z)
}
/// OreFeature's search for a column under its blob, without a trait call
/// per column. Defers to `height` when a heightmap still has to be primed.
fn any_height_at_least(
&mut self,
kind: HeightmapType,
[x0, z0]: [i32; 2],
size: i32,
y: i32,
) -> Result<Option<bool>, PlacementQueryError> {
let (x1, z1) = (x0.wrapping_add(size), z0.wrapping_add(size));
if size < 0 || x1 < x0 || z1 < z0 {
return Ok(None);
}
for cx in x0 >> 4..=x1 >> 4 {
for cz in z0 >> 4..=z1 >> 4 {
if !self
.region
.chunk(cx, cz)
.is_ok_and(|c| c.heightmap(kind).is_some())
{
return Ok(None);
}
}
}
for x in x0..=x1 {
for z in z0..=z1 {
let map = self.region.chunk(x >> 4, z >> 4)?.heightmap(kind).unwrap();
let floor = map
.first_available(x & 15, z & 15)
.map_err(|e| RegionError::from(super::terrain::TerrainError::from(e)))?;
if y <= floor {
return Ok(Some(true));
}
}
}
Ok(Some(false))
}
}
@@ -180,6 +180,27 @@ impl OreFeature {
let z_start = origin[2].wrapping_sub(spread_ceil).wrapping_sub(max_radius);
let size_xz = 2 * (spread_ceil + max_radius);
let size_y = 2 * (2 + max_radius);
if let Some(found) = world
.any_height_at_least(
HeightmapType::OceanFloorWg,
[x_start, z_start],
size_xz,
y_start,
)
.map_err(FeatureExecutionError::Query)?
{
return match found {
true => self.do_place(
world,
random,
[x0, x1, z0, z1, y0, y1],
[x_start, y_start, z_start],
size_xz,
size_y,
),
false => Ok(false),
};
}
for x in 0..=size_xz {
for z in 0..=size_xz {
let (px, pz) = (x_start.wrapping_add(x), z_start.wrapping_add(z));
@@ -243,7 +264,8 @@ impl OreFeature {
}
}
}
let mut tested = vec![false; (size_xz as usize) * (size_y as usize) * (size_xz as usize)];
let tested_len = (size_xz as usize) * (size_y as usize) * (size_xz as usize);
let mut tested = vec![0u64; tested_len.div_ceil(64)];
let mut placed = 0;
for [xx, yy, zz, r] in data {
if r < 0.0 {
@@ -280,16 +302,15 @@ impl OreFeature {
* i64::from(size_xz)
* i64::from(size_y);
// The sphere bounds keep indices inside the native BitSet.
let Some(bit) = usize::try_from(index)
.ok()
.and_then(|index| tested.get_mut(index))
let Some(index) = usize::try_from(index).ok().filter(|&i| i < tested_len)
else {
return Err(FeatureExecutionError::WorkLimit);
};
if *bit {
let (word, bit) = (&mut tested[index / 64], 1u64 << (index % 64));
if *word & bit != 0 {
continue;
}
*bit = true;
*word |= bit;
let p = [x, y, z];
if !world
.ensure_can_write(p)
@@ -351,6 +351,18 @@ pub trait PlacementContext {
/// World minimum, which can differ from the generator's minimum.
fn min_y(&mut self) -> Result<i32, PlacementQueryError>;
fn height(&mut self, kind: HeightmapType, x: i32, z: i32) -> Result<i32, PlacementQueryError>;
/// Whether any column of the square `x..=x + size`, `z..=z + size` has
/// `height(kind)` at least `y`, read faster than column by column; `None`
/// when the world can't (callers then ask `height` for each column).
fn any_height_at_least(
&mut self,
_kind: HeightmapType,
_corner: [i32; 2],
_size: i32,
_y: i32,
) -> Result<Option<bool>, PlacementQueryError> {
Ok(None)
}
}
impl PlacementContext for WorldGenerationContext {
fn max_y(&mut self) -> Result<i32, PlacementQueryError> {
@@ -385,3 +385,46 @@ fn retained_upgrade_status_allows_old_biome_reads() {
}
}
}
#[test]
fn spread_out_regions_route_like_dense_ones() {
// Chunks too far apart for a grid over their bounding box (fixtures put
// one at the far edge of the world beside the origin's neighbours).
let registry = registry();
let context = WorldGenerationContext::new(0, 16, 8);
let chunk =
|x, z| TerrainChunk::new(ChunkPos::new(x, z), 0, 16, &registry, Properties).unwrap();
for far in [4_194_304, -4_194_304] {
let mut region = TerrainRegion::new(
vec![chunk(0, 0), chunk(far, 0), chunk(1, -1), chunk(-1, 1)],
context,
)
.unwrap();
let x = far * 16 + 3;
region
.set_block_worldgen([x, 7, 5], BlockStateId(2))
.unwrap();
assert_eq!(region.block([x, 7, 5]).unwrap(), BlockStateId(2));
assert_eq!(region.block([3, 7, 5]).unwrap(), BlockStateId(0));
assert_eq!(region.block([16, 7, -1]).unwrap(), BlockStateId(0));
assert!(matches!(
region.block([x + 16, 7, 5]),
Err(RegionError::MissingChunk([_, 0]))
));
assert!(matches!(
region.block([0, 7, 16]),
Err(RegionError::MissingChunk([0, 1]))
));
let order: Vec<_> = region
.into_chunks()
.iter()
.map(|c| (c.position().x, c.position().z))
.collect();
let mut sorted = order.clone();
sorted.sort();
assert_eq!(order, sorted, "into_chunks is ordered by X then Z");
// A position twice is refused either way.
assert!(
TerrainRegion::new(vec![chunk(0, 0), chunk(far, 0), chunk(far, 0)], context).is_err()
);
}
}
+100 -18
View File
@@ -995,6 +995,78 @@ fn download_for_owner(
}
}
/// A JDK of at least `major` for the preparation. `None` when horizon-prepare finds one by
/// itself (JAVA_HOME, the PATH, an installed JDK): the preparation then runs as before, in an
/// unchanged environment (a JDK found on the PATH can be a stub such as macOS's
/// /usr/bin/javac, whose folder is no Java home: `JAVA_HOME=/usr` makes it call itself
/// forever). Else the home of the Java the official launcher downloaded into one of the
/// `games` folders, else of Minecraft's own Java downloaded from Mojang into Horizon's game
/// folder, the runtime the version JSON at `manifest` names (Mojang's runtimes are whole JDKs).
fn java(
major: i64,
games: &[PathBuf],
manifest: &Path,
log: &dyn Fn(String),
) -> Result<Option<PathBuf>, String> {
if horizon_prepare::java::find(major, &[]).is_ok() {
return Ok(None);
}
let mut runtimes: Vec<PathBuf> = Vec::new();
for home in games
.iter()
.flat_map(|game| horizon_prepare::java::minecraft_runtimes(&game.join("runtime")))
{
if !runtimes.contains(&home) {
runtimes.push(home);
}
}
let missing = match horizon_prepare::java::find(major, &runtimes) {
Ok(jdk) => return Ok(Some(jdk.home())),
Err(e) => e,
};
let component = read_json(manifest)
.and_then(|json| json["javaVersion"]["component"].as_str().map(String::from))
.ok_or_else(|| missing.clone())?;
// Mojang builds its Linux Java for glibc: without glibc's loader (NixOS without nix-ld,
// musl distributions) it cannot start, so don't download it.
if cfg!(all(target_os = "linux", target_arch = "x86_64")) && !Path::new(GLIBC_LOADER).exists() {
return Err(format!(
"{missing}\n • Minecraft's own Java from Mojang needs glibc ({GLIBC_LOADER} is missing; on NixOS, enable programs.nix-ld), so install a JDK instead."
));
}
let fetch = |url: &str| -> Result<Vec<u8>, String> {
let mut body = Vec::new();
get(url)?
.into_reader()
.read_to_end(&mut body)
.map_err(|e| e.to_string())?;
Ok(body)
};
let runtime = data_dir().join("minecraft").join("runtime");
let home = mojang::java_runtime(&component, &runtime, 8, &fetch, log).map_err(|e| {
format!("{missing}\n • Horizon also tried to download Minecraft's own Java from Mojang, but: {e}")
})?;
horizon_prepare::java::find(major, &[home]).map(|jdk| Some(jdk.home()))
}
/// The dynamic loader glibc programs on x86-64 Linux start through.
const GLIBC_LOADER: &str = "/lib64/ld-linux-x86-64.so.2";
/// Runs `cmd` with the JDK at `home`: JAVA_HOME names it and its `bin` leads the PATH, as the
/// versions' own preparers look for Java.
fn with_java(cmd: &mut Command, home: &Path) {
let mut path = vec![home.join("bin")];
path.extend(
std::env::var_os("PATH")
.iter()
.flat_map(std::env::split_paths),
);
cmd.env("JAVA_HOME", home);
if let Ok(path) = std::env::join_paths(path) {
cmd.env("PATH", path);
}
}
/// Checks what the preparation needs and picks how to run it. PLAY.txt describes the same
/// steps: the player's own launcher install, the server jar from that version's own json, and
/// Java (and Python, for versions before horizon-prepare). Also returns the Minecraft folder
@@ -1008,6 +1080,7 @@ pub fn preparer(
let version = game_version(tag);
let dir = version_dir(tag);
let reused = horizon_prepare::artifacts::reuse_imported(&dir, &|line| log(line.to_string()))?;
let chosen = minecraft.to_path_buf();
let minecraft = if reused {
minecraft.to_path_buf()
} else {
@@ -1044,25 +1117,27 @@ pub fn preparer(
.join(&version)
.join(format!("{version}.json"));
let binary = preparer_binary(&dir);
let python = if binary.is_some() {
// The JDK the version pins, found as horizon-prepare finds it, so a missing one is
// reported before the server jar is downloaded.
let major = read_json(&dir.join("research-lock.json"))
.and_then(|lock| lock["minecraft"]["java_major"].as_i64())
.ok_or("The version's research-lock.json has no java_major")?;
horizon_prepare::java::find(major)?;
None
} else {
let python = python()
.ok_or("This version prepares with Python: Python 3.10 or newer is needed. Install it, or pick a newer version.")?;
if !found("java", "-version") {
return Err(
"Java 25 is needed (a JDK, e.g. Eclipse Temurin 25). Install it and try again."
.into(),
);
}
Some(python)
let python = match binary {
Some(_) => None,
None => Some(python().ok_or(
"This version prepares with Python: Python 3.10 or newer is needed. Install it, or pick a newer version.",
)?),
};
// The JDK the version pins, found as horizon-prepare finds it, so a missing one is
// reported (or downloaded) before the server jar is downloaded. Versions from before
// java_major ran Java 25.
let major = read_json(&dir.join("research-lock.json"))
.and_then(|lock| lock["minecraft"]["java_major"].as_i64())
.unwrap_or(25);
let mut games = vec![chosen, minecraft.clone(), data_dir().join("minecraft")];
games.extend(
sources::Folders::current()
.map(|f| sources::known_folders(sources::Os::current(), &f))
.unwrap_or_default()
.into_iter()
.map(|(_, dir)| dir),
);
let java_home = java(major, &games, &manifest, log)?;
let jar = data_dir()
.join("server-jars")
.join(format!("{version}.jar"));
@@ -1094,10 +1169,14 @@ pub fn preparer(
import: !reused,
minecraft_dir: Some(minecraft.clone()),
server_jar: (!reused).then_some(jar),
java_home: java_home.clone(),
};
return Ok((Preparer::InProcess(options), minecraft));
}
let mut cmd = Command::new(binary);
if let Some(home) = &java_home {
with_java(&mut cmd, home);
}
cmd.current_dir(&dir).arg("--root").arg(&dir);
if !reused {
cmd.args(["--prepare", "--server-jar"]).arg(&jar);
@@ -1106,6 +1185,9 @@ pub fn preparer(
return Ok((Preparer::Process(cmd), minecraft));
}
let mut cmd = Command::new(python.unwrap());
if let Some(home) = &java_home {
with_java(&mut cmd, home);
}
cmd.current_dir(&dir)
.args(["-u", "tools/prepare_native_client.py"]);
if !reused {
+116
View File
@@ -13,6 +13,8 @@ use std::sync::Mutex;
use std::time::Duration;
const OBJECTS: &str = "https://resources.download.minecraft.net";
/// The index of Mojang's Java runtimes, the one the official launcher reads.
const JAVA_RUNTIMES: &str = "https://launchermeta.mojang.com/v1/products/java-runtime/2ec0cc96c44e5a76b9c8b7c39df7210883d12871/all.json";
/// One file to download.
struct File {
@@ -84,6 +86,120 @@ pub fn download(
Ok(())
}
/// Mojang's name for this system in its Java runtime index.
fn java_platform() -> Option<&'static str> {
Some(match (std::env::consts::OS, std::env::consts::ARCH) {
("macos", "aarch64") => "mac-os-arm64",
("macos", "x86_64") => "mac-os",
("linux", "x86_64") => "linux",
("linux", "x86") => "linux-i386",
("windows", "x86_64") => "windows-x64",
("windows", "aarch64") => "windows-arm64",
("windows", "x86") => "windows-x86",
_ => return None,
})
}
/// Downloads Mojang's Java runtime `component` (the version JSON's `javaVersion.component`, e.g.
/// `java-runtime-epsilon`) into the game directory's `runtime` folder, as the official launcher
/// lays it out: `<component>/<platform>/<component>/`. Each file is checked against its sha1;
/// files already there and whole are kept. Mojang's runtimes are whole JDKs, javac included,
/// which is what the exporters need.
pub fn java_runtime(
component: &str,
runtime: &Path,
jobs: usize,
fetch: Fetch,
log: &dyn Fn(String),
) -> Result<PathBuf, String> {
let platform = java_platform().ok_or("Mojang has no Java for this system")?;
let index: Value = serde_json::from_slice(&fetch(JAVA_RUNTIMES)?)
.map_err(|e| format!("Mojang's Java index: {e}"))?;
let entry = &index[platform][component][0];
if entry.is_null() {
return Err(format!("Mojang has no {component} for {platform}"));
}
let manifest = &entry["manifest"];
let manifest: Value = serde_json::from_slice(&fetch_checked(
fetch,
&text(manifest, "url")?,
&text(manifest, "sha1")?,
)?)
.map_err(|e| format!("Mojang's {component} manifest: {e}"))?;
let files = manifest["files"]
.as_object()
.ok_or_else(|| format!("Mojang's {component} manifest lists no files"))?;
let home = runtime.join(component).join(platform).join(component);
let mut downloads = Vec::new();
let mut executables = Vec::new();
let mut links = Vec::new();
for (name, file) in files {
let path = home.join(relative(name)?);
match file["type"].as_str() {
Some("file") => {
let raw = &file["downloads"]["raw"];
if file["executable"] == true {
executables.push(path.clone());
}
downloads.push(File {
url: text(raw, "url")?,
sha1: text(raw, "sha1")?,
path,
});
}
Some("link") => links.push((path, text(file, "target")?)),
_ => {}
}
}
let name = entry["version"]["name"].as_str().unwrap_or(component);
log(format!(
"Downloading Minecraft's Java {name} from Mojang ({} files)",
downloads.len()
));
fetch_all(&downloads, jobs, fetch, log)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
for path in &executables {
fs::set_permissions(path, fs::Permissions::from_mode(0o755))
.map_err(|e| format!("{}: {e}", path.display()))?;
}
for (path, target) in &links {
if !link_stays_inside(name_in(&home, path), target) {
return Err(format!(
"Unsafe link {target:?} in Mojang's {component} manifest"
));
}
let _ = fs::remove_file(path);
fs::create_dir_all(path.parent().unwrap())
.and_then(|_| std::os::unix::fs::symlink(target, path))
.map_err(|e| format!("{}: {e}", path.display()))?;
}
}
#[cfg(not(unix))]
let _ = (executables, links);
Ok(home)
}
fn name_in<'a>(home: &Path, path: &'a Path) -> &'a Path {
path.strip_prefix(home).unwrap_or(path)
}
/// Whether a link at `link` (relative to the runtime's home) pointing at `target` stays in the
/// home.
fn link_stays_inside(link: &Path, target: &str) -> bool {
let mut depth = link.components().count().saturating_sub(1);
for c in Path::new(target).components() {
match c {
Component::Normal(_) => depth += 1,
Component::CurDir => {}
Component::ParentDir if depth > 0 => depth -= 1,
_ => return false,
}
}
true
}
fn read_json(path: &Path) -> Result<Value, String> {
let text = fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?;
serde_json::from_str(&text).map_err(|e| format!("{}: {e}", path.display()))
+96
View File
@@ -230,3 +230,99 @@ fn version_file_paths_stay_in_the_folder() {
assert!(relative(bad).is_err(), "{bad}");
}
}
#[test]
fn links_stay_inside_the_runtime() {
let at = |p: &str| PathBuf::from(p);
assert!(link_stays_inside(
&at("legal/java.compiler/LICENSE"),
"../java.base/LICENSE"
));
assert!(link_stays_inside(&at("lib/a"), "b"));
assert!(!link_stays_inside(&at("lib/a"), "../../b"));
assert!(!link_stays_inside(&at("a"), "/etc/passwd"));
}
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
#[test]
fn java_runtime_lays_out_mojangs_runtime_with_its_executables_and_links() {
const JAVAC: &[u8] = b"#!/bin/sh\necho javac 25.0.1\n";
const LICENSE: &[u8] = b"a fake license";
let manifest = serde_json::json!({"files": {
"bin": {"type": "directory"},
"bin/javac": {"type": "file", "executable": true, "downloads": {"raw":
{"sha1": sha1_hex(JAVAC), "size": JAVAC.len(), "url": "https://piston-data.mojang.com/javac"}}},
"legal/java.base/LICENSE": {"type": "file", "executable": false, "downloads": {"raw":
{"sha1": sha1_hex(LICENSE), "size": LICENSE.len(), "url": "https://piston-data.mojang.com/license"}}},
"legal/java.compiler/LICENSE": {"type": "link", "target": "../java.base/LICENSE"},
}})
.to_string();
let index = serde_json::json!({"linux": {"java-runtime-epsilon": [{
"manifest": {"sha1": sha1_hex(manifest.as_bytes()), "url": "https://piston-meta.mojang.com/m.json"},
"version": {"name": "25.0.1"},
}]}})
.to_string();
let server = Server {
files: HashMap::from([
(JAVA_RUNTIMES.to_string(), index.into_bytes()),
(
"https://piston-meta.mojang.com/m.json".into(),
manifest.into_bytes(),
),
(
"https://piston-data.mojang.com/javac".into(),
JAVAC.to_vec(),
),
(
"https://piston-data.mojang.com/license".into(),
LICENSE.to_vec(),
),
]),
asked: Mutex::new(Vec::new()),
};
let dir = scratch("java");
let home = java_runtime(
"java-runtime-epsilon",
&dir,
2,
&|url| server.fetch(url),
&|_| {},
)
.unwrap();
assert_eq!(
home,
dir.join("java-runtime-epsilon/linux/java-runtime-epsilon")
);
use std::os::unix::fs::PermissionsExt;
let mode = fs::metadata(home.join("bin/javac"))
.unwrap()
.permissions()
.mode();
assert_eq!(mode & 0o111, 0o111);
assert_eq!(
fs::read(home.join("legal/java.compiler/LICENSE")).unwrap(),
LICENSE
);
// horizon-prepare finds it where the official launcher would have put it.
assert_eq!(horizon_prepare::java::minecraft_runtimes(&dir), [home]);
// A second call keeps the files: only the index and manifest are asked again.
server.asked();
java_runtime(
"java-runtime-epsilon",
&dir,
2,
&|url| server.fetch(url),
&|_| {},
)
.unwrap();
assert_eq!(server.asked().len(), 2);
assert!(java_runtime(
"java-runtime-zeta",
&dir,
2,
&|url| server.fetch(url),
&|_| {}
)
.is_err());
fs::remove_dir_all(&dir).unwrap();
}
+87 -4
View File
@@ -114,6 +114,10 @@ address. The script then checks, from both sides, what a guest's game used to ge
server's game time after the guest's game stalls for a few seconds (#848).
- growth: far from the host, the chunks around the guest tick (#890): water placed there flows
and wheat on farmland grows (random ticks), however the host runs its world.
- ladder: the guest climbs down ten blocks of ladder and lands unhurt: the server resets its fall
distance on a ladder every tick, as LivingEntity.handleOnClimbable does (#1255).
- boats: the host (single player, on its server thread with --server-thread) right-clicks a
boat on water and rides it, sneaks out, and breaks it with its fists (#1222).
- flight: a player in creative double-taps jump and flies, stays up, and stops flying in survival:
the guest, and on the server thread the host's window too, take what may fly from the
server's abilities (ClientPacketListener.handlePlayerAbilities).
@@ -155,7 +159,7 @@ ROOT = Path(__file__).resolve().parents[1]
GUEST_NAME = 'Guest'
# The host plays single player on its server thread (--server-thread).
SERVER_THREAD = False
CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight']
CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats']
class Game:
@@ -2279,6 +2283,80 @@ def check_flight(host, guest, out):
return seen
def check_ladder(host, guest, out):
"""#1255: climbing down a ladder doesn't hurt where the guest lands."""
x, y, z = (int(v // 1) for v in pos_of(guest))
lx, lz = x + 6, z
guest('command', command='gamemode survival')
guest('command', command=f'fill {lx - 2} {y} {lz - 2} {lx + 2} {y + 12} {lz + 2} minecraft:air')
guest('command', command=f'fill {lx - 2} {y - 1} {lz - 2} {lx + 2} {y - 1} {lz + 2} minecraft:stone')
guest('command', command=f'fill {lx} {y} {lz + 1} {lx} {y + 10} {lz + 1} minecraft:stone')
guest('command', command=f'fill {lx} {y} {lz} {lx} {y + 10} {lz} minecraft:ladder[facing=north]')
guest.wait(10)
try:
before = remote(guest)['health']
# No input on a ladder: the guest slides down at 0.15 a tick (LivingEntity.handleOnClimbable).
guest('command', command=f'tp @s {lx + 0.5} {y + 10} {lz + 0.4} 0 0')
for _ in range(40):
guest.wait(5)
if pos_of(guest)[1] < y + 0.01:
break
guest.wait(20)
landed, after = pos_of(guest), remote(guest)['health']
assert landed[1] < y + 0.01, f'the guest stayed on the ladder at {landed}'
assert after >= before, f'climbing down ten blocks of ladder took the guest from {before} to {after} health'
return {'landed': landed, 'health': after}
finally:
guest('command', command=f'fill {lx} {y} {lz} {lx} {y + 10} {lz + 1} minecraft:air')
guest('command', command=f'tp @s {x + 0.5} {y} {z + 0.5}')
def check_boats(host, guest, out):
"""#1222: the single player boards a boat on water with a right click, sneaks out, and breaks it."""
x, y, z = (int(v // 1) for v in pos_of(host))
bx = x + 3
host('command', command='gamemode survival')
host('command', command=f'fill {bx - 1} {y} {z - 1} {bx + 1} {y + 3} {z + 1} minecraft:air')
host('command', command=f'fill {bx - 1} {y - 1} {z - 1} {bx + 1} {y - 1} {z + 1} minecraft:water')
host('command', command=f'summon minecraft:oak_boat {bx + 0.5} {y - 0.1} {z + 0.5}')
host.wait(30)
boats = [o for o in host('status')['objects'] if o['kind'].endswith('oak_boat')
and abs(o['position'][0] - (bx + 0.5)) < 2 and abs(o['position'][2] - (z + 0.5)) < 2]
try:
assert boats, 'the host sees no boat on the water'
boat = boats[0]
bp = boat['position']
host.raw('look-at', f'{bp[0]:.2f}', f'{bp[1] + 0.3:.2f}', f'{bp[2]:.2f}')
host.raw('mouse', 'right', 'click')
host.wait(10)
riding = host('status').get('riding')
assert riding == boat['id'], f'right-clicking the boat {boat["id"]}: riding {riding}'
host.raw('key', 'shift', 'down')
host.wait(10)
host.raw('key', 'shift', 'up')
host.wait(10)
riding = host('status').get('riding')
assert riding is None, f'sneaking out of the boat: riding {riding}'
host('command', command=f'tp @s {x + 0.5} {y} {z + 0.5}')
host.wait(5)
gone = False
for _ in range(10):
here = [o for o in host('status')['objects'] if o['id'] == boat['id']]
if not here:
gone = True
break
bp = here[0]['position']
host.raw('look-at', f'{bp[0]:.2f}', f'{bp[1] + 0.3:.2f}', f'{bp[2]:.2f}')
host.raw('mouse', 'left', 'click')
# VehicleEntity.tick takes 1 off the damage every tick: hit fast.
host.wait(2)
assert gone, 'ten hits left the boat in place'
return {'boat': boat['id'], 'rode': True, 'broken': True}
finally:
host('command', command=f'kill @e[type=minecraft:oak_boat,x={bx},y={y},z={z},distance=..6]')
host('command', command=f'fill {bx - 1} {y - 1} {z - 1} {bx + 1} {y - 1} {z + 1} minecraft:stone')
def check_growth(host, guest, out):
"""Block ticks near a guest 1000 blocks from the host: only the guest's tickets tick them."""
x0, y0, z0 = pos_of(guest)
@@ -2591,13 +2669,18 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'name
'xp': check_xp, 'objects': check_objects, 'projectiles': check_projectiles, 'blast': check_blast,
'light': check_light, 'plates': check_plates,
'swings': check_swings, 'cracks': check_cracks,
'time': check_time, 'growth': check_growth, 'flight': check_flight, 'fireball': check_fireball, 'deflect': check_deflect, 'barter': check_barter, 'worn': check_worn, 'kills': check_kills, 'arrows': check_arrows, 'predict': check_predict}
'time': check_time, 'growth': check_growth, 'flight': check_flight, 'fireball': check_fireball, 'deflect': check_deflect, 'barter': check_barter, 'worn': check_worn, 'kills': check_kills, 'arrows': check_arrows, 'predict': check_predict, 'ladder': check_ladder, 'boats': check_boats}
def run(ctl, out, resources, checks):
host, guest = Game('host', ctl, resources), Game('guest', ctl, resources)
# Either way explicitly: the game's default is the server thread.
host.d.env['HORIZON_SERVER_THREAD'] = '1' if SERVER_THREAD else '0'
# Without --server-thread the host runs the game's default, the world in the frame, as players
# get it (`boats` fails there if the default goes back to the server thread before it has
# vehicles, #1222).
if SERVER_THREAD:
host.d.env['HORIZON_SERVER_THREAD'] = '1'
else:
host.d.env.pop('HORIZON_SERVER_THREAD', None)
results = {}
try:
host.pid = host('launch', wait=False, offscreen=sys.platform == 'darwin')['pid']
+1 -1
View File
@@ -5,7 +5,7 @@ Download the launcher, let it install Horizon, then prepare the game from your o
## What you need
- **Minecraft: Java Edition 26.3, played once.** Install it with the official Minecraft launcher (or Prism Launcher, MultiMC, the Modrinth App, ATLauncher or GDLauncher) and start 26.3 once, so it downloads its files. Horizon reads them from there and never copies them anywhere else.
- **Java 25, a JDK.** For example [Eclipse Temurin 25](https://adoptium.net/temurin/releases/?version=25). Only the one-time preparation uses it. The game itself needs no Java.
- **Java 25, a JDK**, for the one-time preparation only (the game itself needs no Java). Nothing to install: the launcher uses the Java the Minecraft launcher downloaded for 26.3, or downloads that same Java from Mojang. A JDK you installed yourself (for example [Eclipse Temurin 25](https://adoptium.net/temurin/releases/?version=25)) works too.
- **A computer on the [supported list](/docs/#supported-systems).** On Linux and Windows that means a graphics card with Vulkan (or DirectX 12 on Windows).
## Download the launcher
+1 -1
View File
@@ -30,7 +30,7 @@ The launcher's **Settings** page has a **Logs** row with an **Open** button. On
- **The game does not start, or crashes straight away.** Update the launcher and Horizon, and your graphics drivers. On Windows, try **Graphics API: DirectX 12** (see [Settings](/docs/settings#graphics-api-on-windows)).
- **"Not prepared" or "Minecraft 26.3 was not found".** Play 26.3 once in the official Minecraft launcher, or point the launcher at your Minecraft folder (**Choose…** next to Minecraft folder on the Play page), then choose **Prepare** again.
- **Preparation says Java 25 is needed.** Install a Java 25 JDK, for example Eclipse Temurin 25.
- **Preparation says Java 25 is needed.** Update the launcher: it then uses the Java the Minecraft launcher downloaded for 26.3, or downloads it from Mojang. If it still can't (no internet, say), install a Java 25 JDK, for example Eclipse Temurin 25.
- **Slow or stuttering.** Lower the render distance, turn LOD off, or pick the **Performance** preset. The performance overlay (Video, Advanced, Overlay) shows what is slow.
- **A resource or shader pack broke something.** Switch it off, or take it out of its folder.