diff --git a/README.md b/README.md index 32ecb88c4..1f6fde223 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ The client starts on the title screen. Command-line options (`--world`, `--seed` ## Project status -Parity is **not** complete. The table below is the project's own audit of each subsystem. Still missing: the warden's brain and the sculk shrieker summoning it, villager breeding and gossip, spawned mob gear enchantments, the resource pack screen, a dedicated server mode, and worlds older than 1.17. +Parity is **not** complete. The table below is the project's own audit of each subsystem. Still missing: the warden's brain and the sculk shrieker summoning it, villager breeding and gossip, the resource pack screen, a dedicated server mode, and worlds older than 1.17. ### Java parity: 88% @@ -134,7 +134,7 @@ from vanilla in places: `mob_ai` (71 of 72 scenarios; a tempted ghastling) and | Lighting | 1 | None: saved and relit light matches vanilla, slabs and stairs included | | World generation | 1 | None: 2,352 whole chunks over 225 structure sites, in all three dimensions, identical to vanilla | | Region files and saves | 0.75 | Saves round-trip with vanilla, and worlds from 1.17 to 26.2 upgrade as vanilla's DataFixers do; worlds older than 1.17 are not upgraded | -| Entities and physics | 0.75 | Attributes, dimensions, death loot, spawn rules, natural spawning (15,500 cases), raids and patrols (1,900 cases) and mob AI (71 of 72 scenarios, villager brains included) match; the warden's brain, villager breeding and gossip, and spawned mob gear are missing | +| Entities and physics | 0.75 | Attributes, dimensions, death loot, spawn rules, natural spawning (15,500 cases), raids and patrols (1,900 cases), spawned mobs' gear, enchantments and state (`finalizeSpawn`, 24,000 cases) and mob AI (71 of 72 scenarios, villager brains included) match; the warden's brain and villager breeding and gossip are missing | | Interaction and inventory | 1 | None: menu clicks match over 8,000 click sequences, the loom and the crafter included; recipe book placement (6,000 cases), its buttons (1,200), the saved book (1,500) and the loom and crafter screens (1,600) match vanilla | | Menus, HUD and chat | 0.75 | Every command runs as vanilla (758 commands over 39,685 parsed inputs), scoreboards and teams match (81,000 operations), and the player list matches over 844,758 draw calls; the focused chat is not compared and player list heads use default skins | | Audio | 1 | None: the sound engine, sound events, music, situational music, ambience and subtitles match vanilla | @@ -151,17 +151,12 @@ from vanilla in places: `mob_ai` (71 of 72 scenarios; a tempted ghastling) and - **Worlds from 1.17 onwards** open and are upgraded as vanilla does; server.properties, eula.txt and the server user lists are read and written as vanilla does. - **Builds for Linux, macOS (Apple Silicon) and Windows.** -### Known issues - -- **Render distance 2 never finishes loading:** a world opened with the render distance at 2 chunks stays on "Loading terrain…". Use a render distance of 3 or more (in Options > Video, or `--render-distance 12` on the command line). - ### Roadmap -1. Fix loading at render distance 2; finish the warden's brain and the sculk shrieker summoning it, and the tempted ghastling. -2. Villager breeding and gossip, and spawned mob gear enchantments (both started on their branches). -3. A dedicated server mode, and LAN hosting tested with a vanilla client. -4. Save upgrades from before 1.17, and the resource pack screen. -5. Platform-independent float math, so every harness also gives vanilla's results on macOS. +1. Finish the warden's brain and the sculk shrieker summoning it, and the tempted ghastling. +2. A dedicated server mode, and LAN hosting tested with a vanilla client. +3. Save upgrades from before 1.17, and the resource pack screen. +4. Platform-independent float math, so every harness also gives vanilla's results on macOS. The rewrite is complete when it reaches one-to-one parity and packaged native builds pass the launch checks in [Rust rewrite status](docs/rust-parity.md#completion-criteria-and-playable-builds). diff --git a/crates/horizon-client/examples/finalize_spawn_fixture.rs b/crates/horizon-client/examples/finalize_spawn_fixture.rs new file mode 100644 index 000000000..9b1ba4f38 --- /dev/null +++ b/crates/horizon-client/examples/finalize_spawn_fixture.rs @@ -0,0 +1,531 @@ +//! `Mob.finalizeSpawn` for differential tests against vanilla +//! (tools/test_finalize_spawn_parity.py): reads the cases of +//! `VanillaFinalizeSpawnOracle`, runs `mobs::finalize` with the same level +//! random and entity randoms, and prints every entity of the case in the +//! oracle's format. +//! +//! usage: finalize_spawn_fixture RESOURCES INPUT OUTPUT HALLOWEEN +use horizon_client::{ + assets::Assets, + blocks::BlockFacts, + enchant::Enchantments, + items::Items, + mobs::{ + Mob, MobTypes, Species, + data::MobData, + finalize::{self, Attr, Ctx, FinalizeLevel, Reason, SpawnData, SpawnRegistries}, + gear, + render::{DYE_NAMES, PROFESSIONS, VILLAGER_TYPES}, + spawning::LocalDifficulty, + }, +}; +use horizon_core::{ + geometry::{Aabb, Vec3}, + random::{LegacyRandom, RandomSource}, +}; +use std::{collections::HashMap, fmt::Write as _, fs, path::Path, sync::Arc}; + +struct Level<'a> { + types: &'a MobTypes, + data: &'a MobData, + biome: String, + structure: Option<(String, [i32; 6])>, + phase: usize, + monsters: bool, + peaceful: bool, + time: i64, + halloween: bool, + roof: Option, + eseed: i64, + /// Every entity of the case in creation order (case chickens first). + created: usize, + /// The level's entities that can carry a zombie: (id, bounds, ridden). + chickens: Vec<(u32, Aabb, bool)>, + rides: Vec<(u32, u32)>, +} +impl FinalizeLevel for Level<'_> { + fn biome(&self, _pos: [i32; 3]) -> String { + self.biome.clone() + } + fn biome_tag(&self, tag: &str, _pos: [i32; 3]) -> bool { + self.data.biome_has(tag, &self.biome) + } + fn structures_with_piece_at(&self, pos: [i32; 3]) -> Vec { + match &self.structure { + Some((id, b)) + if pos[0] >= b[0] + && pos[0] <= b[3] + && pos[1] >= b[1] + && pos[1] <= b[4] + && pos[2] >= b[2] + && pos[2] <= b[5] => + { + vec![id.clone()] + } + _ => Vec::new(), + } + } + fn moon_phase(&self, _pos: [i32; 3]) -> usize { + self.phase + } + fn spawning_monsters(&self) -> bool { + self.monsters + } + fn no_collision(&self, bounds: Aabb) -> bool { + // A floor under the spawn point and an optional roof layer. + self.roof.is_none_or(|y| { + let (lo, hi) = ( + bounds.min(horizon_core::geometry::Axis::Y), + bounds.max(horizon_core::geometry::Axis::Y), + ); + !(lo < f64::from(y) + 1.0 && hi > f64::from(y)) + }) + } + fn free_chicken(&self, bounds: Aabb) -> Option { + self.chickens + .iter() + .find(|(_, b, ridden)| !ridden && b.intersects(bounds)) + .map(|c| c.0) + } + fn ride_chicken(&mut self, chicken: u32, rider: u32) { + if let Some(c) = self.chickens.iter_mut().find(|c| c.0 == chicken) { + c.2 = true; + } + self.rides.push((chicken, rider)); + } + fn game_time(&self) -> i64 { + self.time + } + fn halloween(&self) -> bool { + self.halloween + } + fn create(&mut self, kind: &str, _reason: Reason) -> Option { + if self.peaceful && !finalize::allowed_in_peaceful(kind) { + return None; + } + let t = Arc::new(self.types.types.get(kind)?.clone()); + let index = self.created; + self.created += 1; + let mut m = Mob::new(index as u32 + 1, t, Vec3::ZERO, 0.0); + m.random = LegacyRandom::new(self.eseed.wrapping_add(index as i64)); + Some(m) + } +} + +fn hex64(v: f64) -> String { + format!("{:x}", v.to_bits()) +} +fn hex32(v: f32) -> String { + format!("{:x}", v.to_bits()) +} +const HORSE: [&str; 7] = [ + "white", + "creamy", + "chestnut", + "brown", + "black", + "gray", + "dark_brown", +]; +const MARKINGS: [&str; 5] = ["none", "white", "white_field", "white_dots", "black_dots"]; +const RABBITS: [&str; 6] = ["brown", "white", "black", "white_splotched", "gold", "salt"]; +const PARROTS: [&str; 5] = ["red_blue", "blue", "green", "yellow_blue", "gray"]; +const AXOLOTLS: [&str; 5] = ["lucy", "wild", "gold", "cyan", "blue"]; +const SALMON: [&str; 3] = ["small", "medium", "large"]; +const GENES: [&str; 7] = [ + "normal", + "lazy", + "worried", + "playful", + "brown", + "weak", + "aggressive", +]; +const MEMORIES: [&str; 7] = [ + "long_jump_cooldown", + "ram_cooldown", + "attack_target_cooldown", + "hunted_recently", + "dig_cooldown", + "is_emerging", + "home", +]; + +fn variant(m: &Mob, regs: &SpawnRegistries, v: &mut Vec) { + use Species as S; + let s = m.species(); + let named = |reg: &finalize::VariantRegistry, i: u8| { + format!( + "minecraft:{}", + reg.names.get(i as usize).map_or("?", |n| n.as_str()) + ) + }; + let var = format!("minecraft:{}", finalize::variant_name(s, m.extra.variant)); + match s { + S::Pig => { + v.push(var); + v.push(named(®s.pig_sound, m.extra.sound_variant)); + } + S::Cow => { + v.push(var); + v.push(named(®s.cow_sound, m.extra.sound_variant)); + } + S::Mooshroom => v.push(if m.extra.variant == 0 { "red" } else { "brown" }.into()), + S::Chicken => { + v.push(var); + v.push(named(®s.chicken_sound, m.extra.sound_variant)); + v.push(format!("jockey={}", m.extra.jockey)); + } + S::Cat => { + v.push(var); + v.push(named(®s.cat_sound, m.extra.sound_variant)); + } + S::Wolf => { + v.push(var); + v.push(named(®s.wolf_sound, m.extra.sound_variant)); + } + S::Frog | S::ZombieNautilus => v.push(var), + S::Sheep => v.push(DYE_NAMES[m.extra.color as usize & 15].into()), + S::Horse => v.push(format!( + "{}/{}", + HORSE[m.extra.variant as usize % 7], + MARKINGS[m.extra.variant2 as usize % 5] + )), + S::Llama | S::TraderLlama => v.push(format!( + "{}/{}", + horizon_client::mobs::llama::VARIANTS[m.extra.variant as usize % 4], + m.extra.variant2 + )), + S::SkeletonHorse => v.push(format!("trap={}", m.extra.trap)), + S::Fox => v.push(if m.extra.variant == 1 { "snow" } else { "red" }.into()), + S::Rabbit => v.push(RABBITS[m.extra.variant as usize % 6].into()), + S::Parrot => v.push(PARROTS[m.extra.variant as usize % 5].into()), + S::Axolotl => v.push(AXOLOTLS[m.extra.variant as usize % 5].into()), + S::TropicalFish => { + let p = u32::from(m.extra.variant); + let pattern = if p < 6 { p << 8 } else { 1 | (p - 6) << 8 }; + let packed = (pattern & 0xFFFF) + | (u32::from(m.extra.variant2) & 0xFF) << 16 + | (u32::from(m.extra.variant3) & 0xFF) << 24; + v.push((packed as i32).to_string()); + } + S::Salmon => v.push(SALMON[m.extra.variant as usize % 3].into()), + S::Panda => v.push(format!( + "{}/{}", + GENES[m.extra.variant as usize % 7], + GENES[m.extra.variant2 as usize % 7] + )), + S::Goat => v.push(format!( + "scream={} horns={},{}", + m.extra.screaming, !m.extra.lost_horns[0], !m.extra.lost_horns[1] + )), + S::Slime | S::MagmaCube | S::SulfurCube => v.push(format!("size={}", m.extra.size.max(1))), + S::Villager => v.push(format!( + "minecraft:{}/minecraft:{}/{}", + VILLAGER_TYPES[m.extra.variant as usize % 7], + PROFESSIONS[m.extra.variant2 as usize % PROFESSIONS.len()], + m.extra.variant3.max(1) + )), + S::ZombieVillager => v.push(format!( + "minecraft:{}/{}", + VILLAGER_TYPES[m.extra.variant as usize % 7], + m.extra.variant3.max(1) + )), + S::Phantom => v.push(format!("size={}", m.extra.size)), + S::Turtle => v.push(format!( + "home={}", + m.extra + .home + .map_or("-".into(), |h| format!("{},{},{}", h[0], h[1], h[2])) + )), + S::Dolphin => v.push(format!("air={}", m.air)), + _ => {} + } + if matches!( + s, + S::Zombie | S::Husk | S::Drowned | S::ZombieVillager | S::ZombifiedPiglin + ) { + v.push(format!("doors={}", m.extra.can_break_doors)); + } + if horizon_client::mobs::raider::is_raider(s) { + let st = horizon_client::mobs::raider::state(m); + v.push(format!( + "leader={} patrolling={}", + st.is_some_and(|s| s.patrol_leader), + st.is_some_and(|s| s.patrolling) + )); + v.push(format!("raid={}", st.is_some_and(|s| s.can_join_raid))); + } + if s == S::Strider { + v.push(format!("saddled={}", m.extra.saddled)); + } +} + +fn describe(m: &Mob, all: &[Mob], regs: &SpawnRegistries) -> String { + let mut t = vec![m.kind.id.clone()]; + let vehicle = m + .extra + .mount + .and_then(|v| all.iter().position(|o| o.id == v)) + .map_or("-".to_owned(), |i| i.to_string()); + t.push(format!("veh={vehicle}")); + if finalize::ageable(m.species()) { + t.push(format!("age={}", m.extra.age)); + } else { + t.push(format!("baby={}", m.is_baby())); + } + t.push(format!("hp={}", hex32(m.health))); + t.push(format!("lh={}", m.extra.left_handed)); + t.push(format!("loot={}", m.can_pick_up_loot())); + t.push(format!("persist={}", m.persistent)); + let mut eq = Vec::new(); + for (slot, name) in finalize::SLOT_NAMES.iter().enumerate() { + let Some(id) = finalize::item(m, slot) else { + continue; + }; + let mut text = id.to_string(); + let g = match slot { + 0 => Some(gear::MAIN), + 1 => Some(gear::OFF), + 2 => Some(gear::FEET), + 3 => Some(gear::LEGS), + 4 => Some(gear::CHEST), + 5 => Some(gear::HEAD), + _ => None, + }; + if let (Some(g), Some(levels)) = (g, gear::gear(m)) { + let mut list: Vec = levels.slots[g] + .iter() + .map(|(e, l)| format!("{e}={l}")) + .collect(); + list.sort(); + if !list.is_empty() { + let _ = write!(text, "[{}]", list.join(",")); + } + } + if slot == 5 && horizon_client::mobs::raider::is_captain(m) { + text.push_str("*4"); + } + eq.push(format!("{name}:{text}")); + } + t.push(format!( + "eq={}", + if eq.is_empty() { + "-".into() + } else { + eq.join(";") + } + )); + let drops: Vec = finalize::SLOT_NAMES + .iter() + .enumerate() + .filter_map(|(i, name)| { + let d = finalize::stored_drop_chance(m, i); + (d != 0.085).then(|| format!("{name}:{}", hex32(d))) + }) + .collect(); + t.push(format!( + "drop={}", + if drops.is_empty() { + "-".into() + } else { + drops.join(";") + } + )); + let attrs: Vec = [ + Attr::MaxHealth, + Attr::MovementSpeed, + Attr::FollowRange, + Attr::AttackDamage, + Attr::KnockbackResistance, + Attr::SpawnReinforcements, + Attr::JumpStrength, + ] + .into_iter() + .filter(|a| finalize::default_value(m, *a).is_some()) + .map(|a| format!("{}:{}", a.name(), hex64(finalize::value(m, a)))) + .collect(); + t.push(format!("a={}", attrs.join(","))); + let mut fx: Vec = m + .extra + .effects + .iter() + .map(|(e, d, a)| format!("{e}:{a}:{d}")) + .collect(); + fx.sort(); + t.push(format!( + "fx={}", + if fx.is_empty() { + "-".into() + } else { + fx.join(",") + } + )); + let mem: Vec = MEMORIES + .iter() + .filter_map(|name| { + let (_, value, ttl) = m.extra.memories.iter().find(|x| x.0 == *name)?; + let text = match *name { + "hunted_recently" => "true".to_owned(), + "dig_cooldown" | "is_emerging" => "INSTANCE".to_owned(), + "home" => m + .extra + .home + .map_or("-".into(), |h| format!("{},{},{}", h[0], h[1], h[2])), + _ => value.to_string(), + }; + Some(format!("{name}:{text}:{ttl}")) + }) + .collect(); + if !mem.is_empty() { + t.push(format!("mem={}", mem.join(","))); + } + let mut v = Vec::new(); + variant(m, regs, &mut v); + if !v.is_empty() { + t.push(format!("v={}", v.join(" ").replace(' ', "_"))); + } + t.join(" ") +} + +fn main() -> horizon_client::Result<()> { + let args: Vec = std::env::args().collect(); + let root = Path::new( + args.get(1) + .ok_or("usage: finalize_spawn_fixture RESOURCES INPUT OUTPUT HALLOWEEN")?, + ); + let input = fs::read_to_string(args.get(2).ok_or("missing INPUT")?)?; + let halloween = args.get(4).is_some_and(|h| h == "true"); + let assets = Assets::load(root)?; + let facts = BlockFacts::load(root, assets.registry)?; + let mob_data = MobData::load(root, &facts); + let items = Items::tags_only(root)?; + let enchantments = Enchantments::load(root); + let types = MobTypes::load(root)?; + let registries = SpawnRegistries::load(root); + let camel = &types.types["minecraft:camel_husk"]; + let data = SpawnData { + registries: ®istries, + enchantments: &enchantments, + items: &items, + camel_husk_size: (camel.width as f32, camel.height as f32), + }; + let mut out = format!("halloween {halloween}\n"); + for line in input.lines().filter(|l| !l.trim().is_empty()) { + let mut c: HashMap<&str, &str> = HashMap::new(); + let mut name = ""; + for token in line.split_whitespace() { + match token.split_once('=') { + Some((k, v)) => { + c.insert(k, v); + } + None => name = token, + } + } + let get = + |k: &str, d: &'static str| -> String { c.get(k).copied().unwrap_or(d).to_owned() }; + let p: Vec = get("pos", "0,0,0") + .split(',') + .map(|x| x.parse().unwrap_or(0.0)) + .collect(); + let pos = Vec3::new(p[0], p[1], p[2]); + let block = [ + p[0].floor() as i32, + p[1].floor() as i32, + p[2].floor() as i32, + ]; + let base = match get("difficulty", "normal").as_str() { + "peaceful" => 0, + "easy" => 1, + "hard" => 3, + _ => 2, + }; + let phase: usize = get("phase", "0").parse().unwrap_or(0); + let time: i64 = get("time", "0").parse().unwrap_or(0); + let inhabited: i64 = get("inhabited", "0").parse().unwrap_or(0); + let difficulty = LocalDifficulty::new( + base, + time, + inhabited, + horizon_client::mobs::spawning::MOON_BRIGHTNESS[phase & 7], + ); + let mut level = Level { + types: &types, + data: &mob_data, + biome: get("biome", "minecraft:plains"), + structure: c.get("structure").map(|s| { + ( + (*s).to_owned(), + [ + block[0] - 8, + block[1] - 8, + block[2] - 8, + block[0] + 8, + block[1] + 8, + block[2] + 8, + ], + ) + }), + phase, + monsters: get("monsters", "1") != "0", + peaceful: base == 0, + time, + halloween, + roof: c + .get("roof") + .and_then(|r| r.parse::().ok()) + .map(|r| block[1] + r), + eseed: get("eseed", "0").parse().unwrap_or(0), + created: 0, + chickens: Vec::new(), + rides: Vec::new(), + }; + let mut all: Vec = Vec::new(); + let chickens: usize = get("chickens", "0").parse().unwrap_or(0); + for i in 0..chickens { + if let Some(mut chicken) = level.create("minecraft:chicken", Reason::Load) { + chicken.position = Vec3::new(pos.x + i as f64, pos.y, pos.z); + level.chickens.push((chicken.id, chicken.bounds(), false)); + all.push(chicken); + } + } + let kind = get("type", "minecraft:pig"); + let reason = Reason::parse(&get("reason", "NATURAL")).unwrap_or(Reason::Natural); + let pack: usize = get("pack", "1").parse().unwrap_or(1); + let mut random = LegacyRandom::new(get("seed", "0").parse().unwrap_or(0)); + let mut group = None; + for _ in 0..pack { + let Some(mut mob) = level.create(&kind, reason) else { + break; + }; + mob.position = pos; + mob.previous = pos; + let mut cx = Ctx { + level: &mut level, + random: &mut random, + difficulty, + data: &data, + created: Vec::new(), + }; + group = finalize::finalize(&mut cx, &mut mob, reason, group); + let created = std::mem::take(&mut cx.created); + all.push(mob); + all.extend(created); + } + for (chicken, rider) in std::mem::take(&mut level.rides) { + if let Some(c) = all.iter_mut().find(|m| m.id == chicken) { + c.extra.jockey = true; + c.extra.rider = Some(rider); + } + } + all.sort_by_key(|m| m.id); + let mut parts: Vec = all.iter().map(|m| describe(m, &all, ®istries)).collect(); + for m in &mut all { + parts.push(format!("rng {}", m.random.next_long())); + } + parts.push(format!("next {}", random.next_long())); + let _ = writeln!(out, "{name} {}", parts.join(" | ")); + } + fs::write(args.get(3).ok_or("missing OUTPUT")?, out)?; + Ok(()) +} diff --git a/crates/horizon-client/examples/lan_probe.rs b/crates/horizon-client/examples/lan_probe.rs new file mode 100644 index 000000000..43ce51f9d --- /dev/null +++ b/crates/horizon-client/examples/lan_probe.rs @@ -0,0 +1,46 @@ +//! Horizon's LAN world discovery for tools/test_lan_parity.py: the cases of +//! VanillaLanOracle (announcements sent, datagrams heard, list resets) run +//! through `lan::discovery`, printing the same lines. +use horizon_client::lan::discovery::{ + LanGame, add_server, parse_address, parse_motd, ping_string_text, +}; + +fn hex(bytes: &[u8]) -> String { + bytes.iter().map(|b| format!("{b:02x}")).collect() +} +fn unhex(s: &str) -> Vec { + (0..s.len() / 2) + .map(|i| u8::from_str_radix(&s[2 * i..2 * i + 2], 16).unwrap()) + .collect() +} + +fn main() { + let args: Vec = std::env::args().collect(); + let cases = std::fs::read_to_string(&args[1]).unwrap(); + let mut out = String::new(); + let mut games: Vec = Vec::new(); + for line in cases.lines() { + let f: Vec<&str> = line.split('\t').collect(); + match f[0] { + "ping" => out += &format!("ping {}\n", hex(ping_string_text(f[1], f[2]).as_bytes())), + "heard" => { + let received = String::from_utf8_lossy(&unhex(f[2])).into_owned(); + out += &format!( + "heard motd={} address={}\n", + parse_motd(&received), + parse_address(&received).unwrap_or_else(|| "null".into()) + ); + if add_server(&mut games, &received, f[1]) { + out += "list"; + for g in &games { + out += &format!(" [{} @ {}]", g.motd, g.address); + } + out += "\n"; + } + } + "reset" => games.clear(), + other => panic!("{other}"), + } + } + std::fs::write(&args[2], out).unwrap(); +} diff --git a/crates/horizon-client/examples/mob_ai_probe.rs b/crates/horizon-client/examples/mob_ai_probe.rs index df0d70b62..729ac88f1 100644 --- a/crates/horizon-client/examples/mob_ai_probe.rs +++ b/crates/horizon-client/examples/mob_ai_probe.rs @@ -223,56 +223,133 @@ fn goals(m: &Mob, names: Vec<&'static str>) -> String { .join("|") } -fn seen(m: &Mob) -> Seen { - Seen { - id: m.id, - species: m.species(), - position: m.position, - bounds: m.bounds(), - eye_y: m.eye_y(), - alive: m.alive(), - baby: m.is_baby(), - in_love: if m.species() == Species::Villager { - horizon_client::mobs::villager::can_breed(m) - } else { - m.extra.in_love > 0 - }, - tame: m.extra.tame, - hostile: m.hostile(), - target: m.ai.target, - health: m.health, - variant: m.extra.variant, - color: m.extra.color, - job: (m.species() == Species::Villager) - .then_some(m.extra.home) - .flatten(), - bed: (m.species() == Species::Villager) - .then_some(m.extra.bed) - .flatten(), - yaw: m.yaw, - velocity: m.velocity, - leash: m.extra.leash, - caravan: horizon_client::mobs::caravan::head_of(m), - delay: m.extra.ints[5], - hurt_by: m.ai.last_hurt_by.map(|w| (w, m.ai.last_hurt_by_time)), - active_raid: horizon_client::mobs::raider::has_active_raid(m), - player_rep: 0, - pushable: m.pushable(), - category: m.category(), - potential_job: horizon_client::mobs::villager_brain::potential_job_site(m), - meeting: (m.species() == Species::Villager) - .then_some(m.extra.meeting) - .flatten(), - profession: m.extra.variant2, - xp: horizon_client::mobs::villager::xp(m), - stats: [ - f64::from(m.attrs.max_health), - m.extra.vector[0], - m.attrs.speed, - ], +/// The oracle's villager flags: profession, level, xp, food, inventory, +/// gossip about the player and the last sleep. +fn villager_flag(m: &mut Mob, flag: &str) { + use horizon_client::mobs::villager; + let (k, v) = flag + .split_once('=') + .unwrap_or_else(|| panic!("unknown mob flag {flag}")); + match k { + "profession" => { + let p = horizon_client::mobs::render::PROFESSIONS + .iter() + .position(|p| *p == v) + .expect("profession"); + villager::set_profession(m, p as u8); + } + "level" => m.extra.variant3 = v.parse().unwrap(), + "xp" => { + m.extra.trade.get_or_insert_with(Default::default).xp = v.parse().unwrap(); + } + "food" => villager::ext_mut(m).food_level = v.parse().unwrap(), + "inv" => { + for item in v.split(',') { + let (name, count) = item.split_once('*').unwrap_or((item, "1")); + villager::ext_mut(m) + .inventory + .push(horizon_client::items::ItemStack::new( + &if name.contains(':') { + name.to_owned() + } else { + format!("minecraft:{name}") + }, + count.parse().unwrap(), + )); + } + } + "gossip" => { + let g: Vec<&str> = v.split(':').collect(); + assert_eq!(g[0], "player", "gossip about the player only"); + let kind = horizon_client::gossip::GossipType::from_id(g[1]).expect("gossip type"); + villager::ext_mut(m) + .gossips + .add(PLAYER_UUID, kind, g[2].parse().unwrap()); + } + "slept" => villager::ext_mut(m).last_slept = Some(v.parse().unwrap()), + _ => panic!("unknown mob flag {flag}"), } } +/// The random seed of the entity a scenario spawns at this index (the +/// oracle's `spawnSeed`). +fn spawn_seed(index: usize) -> i64 { + 1000 + 7919 * index as i64 +} + +/// Villager events that act at once in vanilla (`TradeWithVillager` and +/// `VillagerMakeLove` write the other villager directly). +fn villager_events( + mobs: &mut [Mob], + events: &mut Vec, + from: usize, + w: &MobWorld, + types: &MobTypes, + items: &Items, + born: &mut Vec, + day_time: i64, +) { + use horizon_client::mobs::villager; + let index = |mobs: &[Mob], id: u32| { + mobs.iter() + .position(|m| m.id == id && m.species() == Species::Villager) + }; + let mut i = from; + while i < events.len() { + match events[i] { + MobEvent::VillagerMeet { mob, other } => { + if let (Some(a), Some(b)) = (index(mobs, mob), index(mobs, other)) { + let meeting = villager::meet(mobs, a, b, w.game_time, |item| { + items.get(item).map_or(64, |i| i.max_stack) + }); + assert!( + meeting.thrown.is_empty(), + "villagers toss items: not modelled here" + ); + } + events.remove(i); + } + MobEvent::VillagerBirth { + parent, + partner, + child, + } => { + events.remove(i); + let (Some(a), Some(b)) = (index(mobs, parent), index(mobs, partner)) else { + continue; + }; + villager::ext_mut(&mut mobs[b]).eat_and_digest(); + let Some((bed, variant)) = child else { + continue; + }; + mobs[b].extra.age = 6000; + let index = mobs.len() + born.len(); + let kind = types.types.get("minecraft:villager").expect("villager"); + let p = mobs[a].position; + let mut child = Mob::new(100_000 + index as u32, Arc::new(kind.clone()), p, 0.0); + child.persistent = true; + // The flat world is all plains (type 0). + child.extra.variant = variant.unwrap_or(0); + child.extra.variant2 = villager::NONE; + child.extra.age = -24000; + child.extra.bed = Some(bed); + let seed = spawn_seed(index); + child.random = LegacyRandom::new(seed); + horizon_client::mobs::villager_brain::init(&mut child, w.game_time, day_time, seed); + born.push(child); + } + _ => i += 1, + } + } +} + +/// The player's UUID (the oracle's `PLAYER_UUID`). +const PLAYER_UUID: [i32; 4] = [0, 0x1234, 0, 0x5678]; + +fn seen(m: &Mob) -> Seen { + m.seen(Some(PLAYER_UUID)) +} + /// The probe's mobs as a creaking heart sees them: creakings it creates take /// the n-th seed from 1000, as the oracle's do. struct Host<'a> { @@ -479,9 +556,95 @@ fn print(out: &mut String, t: i32, i: usize, m: &Mob) { if let Some((acts, mem)) = brain { let _ = write!(out, "\tact {acts}\tmem {mem}"); } + if m.species() == Species::Villager { + let _ = write!(out, "\tvx {}", villager(m)); + } out.push('\n'); } +fn stack(s: &horizon_client::items::ItemStack) -> String { + if s.count == 0 { + return "-".into(); + } + format!("{}*{}", s.item.trim_start_matches("minecraft:"), s.count) +} + +/// The oracle's `villager`: data, food, inventory, gossip, sleep and offers. +fn villager(m: &Mob) -> String { + use horizon_client::mobs::villager; + let e = villager::ext(m); + let mut slots: Vec = (0..8) + .map(|k| { + e.and_then(|e| e.inventory.get(k)) + .map_or_else(|| "-".to_owned(), stack) + }) + .collect(); + while slots.last().is_some_and(|s| s == "-") { + slots.pop(); + } + // Villagers gossip about players only. + let label = |t: [i32; 4]| { + if t == PLAYER_UUID { + "player".to_owned() + } else { + format!("{t:?}") + } + }; + let mut gossips: Vec = e + .map(|e| { + e.gossips + .entries() + .map(|(t, k, v)| format!("{}:{}={v}", label(t), k.id())) + .collect() + }) + .unwrap_or_default(); + gossips.sort(); + let join = |v: &[String]| { + if v.is_empty() { + "-".to_owned() + } else { + v.join(",") + } + }; + let mut s = format!( + "prof {} lvl {} xp {} food {} age {} inv {} gossip {} rep {} sleep {}", + horizon_client::mobs::render::PROFESSIONS[m.extra.variant2 as usize % 15], + m.extra.variant3.max(1), + villager::xp(m), + e.map_or(0, |e| e.food_level), + m.extra.age, + join(&slots), + join(&gossips), + e.map_or(0, |e| e.gossips.reputation(PLAYER_UUID)), + match (villager::sleeping(m), m.extra.bed) { + (true, Some(b)) => format!("{},{},{}", b[0], b[1], b[2]), + _ => "-".into(), + } + ); + if let Some(t) = m.extra.trade.as_ref().filter(|t| !t.offers.is_empty()) { + let offers: Vec = t + .offers + .iter() + .map(|o| { + format!( + "{}+{}>{}/{}/{}/{}/{}", + stack(&o.base_cost_a.stack), + o.cost_b + .as_ref() + .map_or_else(|| "-".to_owned(), |c| stack(&c.stack)), + stack(&o.result), + o.uses, + o.max_uses, + o.demand, + o.special_price_diff + ) + }) + .collect(); + s.push_str(&format!(" offers {}", join(&offers))); + } + s +} + fn main() -> horizon_client::Result<()> { let args: Vec = std::env::args().skip(1).collect(); if args.len() != 3 { @@ -608,7 +771,7 @@ fn main() -> horizon_client::Result<()> { let n: u32 = x[8..].parse().unwrap(); m.extra.leash = Some(horizon_client::mobs::leash::Holder::Mob(100_000 + n)); } - other => panic!("unknown mob flag {other}"), + other => villager_flag(&mut m, other), } } m.random = LegacyRandom::new(spec.seed); @@ -657,7 +820,7 @@ fn main() -> horizon_client::Result<()> { velocity: Vec3::ZERO, yaw: 0.0, hero: false, - uuid: [0, 0x1234, 0, 0x5678], + uuid: PLAYER_UUID, allies: Default::default(), } }); @@ -767,6 +930,7 @@ fn main() -> horizon_client::Result<()> { #[allow(unused_mut)] // filled only with the rng-trace feature let mut traces: Vec = Vec::new(); let mut pushes = Vec::new(); + let mut born = Vec::new(); for i in 0..mobs.len() { if mobs[i].removed { continue; @@ -790,6 +954,16 @@ fn main() -> horizon_client::Result<()> { m.removed = true; } horizon_client::mobs::apply_brain_events(&mut mobs, &mut events, from); + villager_events( + &mut mobs, + &mut events, + from, + &w, + &types, + &items, + &mut born, + s.time, + ); // ServerLevel ticks entities one after another: the ones // after this mob see it as it is now. if let Some(e) = snapshot.iter_mut().find(|e| e.id == mobs[i].id) { @@ -822,6 +996,7 @@ fn main() -> horizon_client::Result<()> { added.append(&mut host.added); } drop(w); + mobs.append(&mut born); mobs.append(&mut added); for e in &heart_events { match e { diff --git a/crates/horizon-client/src/audio.rs b/crates/horizon-client/src/audio.rs index 5853a4d7f..51d8343c2 100644 --- a/crates/horizon-client/src/audio.rs +++ b/crates/horizon-client/src/audio.rs @@ -329,12 +329,26 @@ fn mix(mixer: &mut Mixer, data: &mut [f32], channels: usize, rate: u32) { } impl Audio { pub fn new(root: &Path) -> Self { + Self::with_output(root, true) + } + /// No output device: the dedicated server. + pub fn silent(root: &Path) -> Self { + Self::with_output(root, false) + } + fn with_output(root: &Path, output: bool) -> Self { let registry = SoundRegistry::load(root); let mixer = Arc::new(Mutex::new(Mixer::default())); - let (stream, live) = match Self::open(mixer.clone()) { + let opened = if output { + Self::open(mixer.clone()) + } else { + Err("no output on a dedicated server".into()) + }; + let (stream, live) = match opened { Ok(v) => (Some(v), true), Err(e) => { - eprintln!("Horizon: audio unavailable: {e}"); + if output { + eprintln!("Horizon: audio unavailable: {e}"); + } (None, false) } }; diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index b0fa1cc3b..6745c5335 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -140,7 +140,7 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag { for (key, value) in [ ("AbsorptionAmount", Tag::Float(0)), ("FallFlying", Tag::Byte(0)), - ("CanPickUpLoot", Tag::Byte(i8::from(m.extra.pick_up_loot))), + ("CanPickUpLoot", Tag::Byte(i8::from(m.can_pick_up_loot()))), ("LeftHanded", Tag::Byte(0)), ( "Brain", diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index cc07c48f1..f86435c9b 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -22,6 +22,7 @@ mod copper_golems; mod creaking; mod creatures; mod crossbow; +mod dedicated; mod dispense_fixture; mod dispenser; mod dragon; @@ -58,6 +59,7 @@ mod music; mod names; mod natural_spawning; mod nautili; +mod net_harness; mod options; mod packs; mod paintings; @@ -184,6 +186,9 @@ struct Options { /// Multiplayer: `--server HOST[:PORT]` joins at start; `--name` is the offline player name. server: Option, player_name: String, + /// `--dedicated`: run as a dedicated server; vanilla's server arguments + /// (`--nogui`, `--port`, `--world`, `--universe`, `--initSettings`) follow. + dedicated: Option>, } impl Options { fn read() -> Result> { @@ -215,6 +220,7 @@ impl Options { control: None, server: None, player_name: std::env::var("HORIZON_PLAYER_NAME").unwrap_or_else(|_| "Player".into()), + dedicated: None, }; let mut args = std::env::args().skip(1); while let Some(arg) = args.next() { @@ -225,6 +231,10 @@ impl Options { ); return Ok(None); } + "--dedicated" => { + o.dedicated = Some(args.by_ref().collect()); + break; + } "--creative" => o.creative = true, "--test-terrain" => o.test_terrain = true, "--no-pause" => o.no_pause = true, @@ -2573,6 +2583,10 @@ impl Game<'_> { } let riding = self.riding.is_some(); let Some(player) = self.player.as_mut() else { + // A dedicated server has no local player: the world still runs. + if self.options.dedicated.is_some() { + return self.tick_world(running); + } return Ok(()); }; // Hold position until the surrounding terrain exists. @@ -2760,6 +2774,11 @@ impl Game<'_> { self.screen = Screen::Dead; self.capture(false); } + self.tick_world(running) + } + /// What the tick runs after the local player's own update: effects, the + /// world's entities and blocks, and the network hub. + fn tick_world(&mut self, running: bool) -> Result<()> { self.tick_effects(); self.tick_burning(); self.tick_using(); @@ -8053,6 +8072,9 @@ pub fn run() -> Result<()> { let Some(mut options) = Options::read()? else { return Ok(()); }; + if options.dedicated.is_some() && !dedicated::preflight(&mut options)? { + return Ok(()); + } let started = Instant::now(); let vanilla_root = options.resources.clone(); let early_settings = Settings::load(&std::env::current_dir()?.join("options.txt")); @@ -8127,7 +8149,11 @@ pub fn run() -> Result<()> { .join("saves") .join(".horizon-title"), )); - let audio = Audio::new(&root); + let audio = if options.dedicated.is_some() { + Audio::silent(&root) + } else { + Audio::new(&root) + }; let world = World::new(facts.air, 0); let creative = options.creative; let stations = horizon_client::stations::StationData::load(&root, &items); @@ -8417,6 +8443,9 @@ pub fn run() -> Result<()> { if let Ok(spec) = std::env::var("HORIZON_SCULK_FIXTURE") { return game.sculk_fixture(&spec); } + if game.options.dedicated.is_some() { + return game.run_dedicated(); + } match game.options.world.clone() { _ if game.options.server.is_some() => { let (address, name) = ( @@ -8424,6 +8453,9 @@ pub fn run() -> Result<()> { game.options.player_name.clone(), ); game.connect_server(&address, &name)?; + if let Some(report) = std::env::var_os("HORIZON_NET_REPORT") { + return game.run_net_harness(report.as_ref()); + } } Some(folder) => { let (seed, creative) = (game.options.seed, game.options.creative); diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 94231b9be..bd958893f 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -87,60 +87,7 @@ impl ProjectileWorld for ProjWorld<'_, '_> { } } pub(super) fn seen(m: &Mob, player: Option<[i32; 4]>) -> Seen { - let villager = m.species() == Species::Villager; - Seen { - id: m.id, - species: m.species(), - position: m.position, - bounds: m.bounds(), - eye_y: m.eye_y(), - alive: m.alive(), - baby: m.is_baby(), - // A villager is "in love" when it is willing to breed (`canBreed`). - in_love: if villager { - horizon_client::mobs::villager::can_breed(m) - } else { - m.extra.in_love > 0 - }, - tame: m.extra.tame, - hostile: m.hostile(), - target: m.ai.target, - health: m.health, - variant: m.extra.variant, - color: m.extra.color, - job: (m.species() == Species::Villager) - .then_some(m.extra.home) - .flatten(), - bed: (m.species() == Species::Villager) - .then_some(m.extra.bed) - .flatten(), - yaw: m.yaw, - velocity: m.velocity, - leash: m.extra.leash, - caravan: horizon_client::mobs::caravan::head_of(m), - delay: m.extra.ints[5], - hurt_by: m.ai.last_hurt_by.map(|w| (w, m.ai.last_hurt_by_time)), - active_raid: horizon_client::mobs::raider::has_active_raid(m), - player_rep: match (villager, player) { - (true, Some(uuid)) => { - horizon_client::mobs::villager::ext(m).map_or(0, |e| e.gossips.reputation(uuid)) - } - _ => 0, - }, - pushable: m.pushable(), - category: m.category(), - potential_job: horizon_client::mobs::villager_brain::potential_job_site(m), - meeting: (m.species() == Species::Villager) - .then_some(m.extra.meeting) - .flatten(), - profession: m.extra.variant2, - xp: horizon_client::mobs::villager::xp(m), - stats: [ - f64::from(m.attrs.max_health), - m.extra.vector[0], - m.attrs.speed, - ], - } + m.seen(player) } impl Game<'_> { pub(super) fn player_view(&self) -> Option { @@ -967,7 +914,11 @@ impl Game<'_> { } MobEvent::CureZombieVillager { mob } => self.cure_zombie_villager(mob), MobEvent::VillagerMeet { mob, other } => self.villager_meet(mob, other), - MobEvent::VillagerBirth { parent, partner } => self.villager_birth(parent, partner), + MobEvent::VillagerBirth { + parent, + partner, + child, + } => self.villager_birth(parent, partner, child), MobEvent::GolemCheck { mob, needed } => self.villager_golem_check(mob, needed), MobEvent::Firework { at, color, flight } => { self.villager_firework(at, color, flight) diff --git a/crates/horizon-client/src/game/dedicated.rs b/crates/horizon-client/src/game/dedicated.rs new file mode 100644 index 000000000..8ca5f159d --- /dev/null +++ b/crates/horizon-client/src/game/dedicated.rs @@ -0,0 +1,1056 @@ +//! The dedicated server (net.minecraft.server.Main and DedicatedServer): +//! `horizon-client --dedicated [--nogui] [--port N] [--world NAME] +//! [--universe DIR] [--initSettings]`. It reads server.properties and +//! eula.txt from the working directory, opens `/` and +//! runs the same game as single player, without a local player: the world, +//! its simulation and the play-state hub that LAN hosting uses, configured +//! from server.properties, with the operator, whitelist and ban lists and a +//! console on standard input. +use super::{Game, Options, lan::LanSettings}; +use horizon_client::{ + Result, + lan::{LoginGate, NetConfig, hub::HubConfig}, + server::{ + eula, + java_properties::now_millis, + properties::{GAME_MODES, ServerProperties, SystemEntropy}, + user_lists::{ + BanEntry, BanList, IpBanList, NameAndId, OpEntry, OpList, WhiteEntry, WhiteList, + format_date, + }, + }, + text::Text, +}; +use horizon_protocol::text::translatable; +use horizon_world::nbt::{JavaString, Tag}; +use std::{ + path::PathBuf, + sync::{Arc, Mutex, OnceLock, mpsc}, + time::{Duration, Instant}, +}; + +/// Double.toString for the magnitudes positions have (no exponent form +/// between 1e-3 and 1e7). +pub(super) fn java_double(v: f64) -> String { + if v == 0.0 || (1e-3..1e7).contains(&v.abs()) { + format!("{v:?}") + } else { + let s = format!("{v:E}"); + let (m, e) = s.split_once('E').unwrap_or((&s, "0")); + let m = if m.contains('.') { + m.to_owned() + } else { + format!("{m}.0") + }; + format!("{m}E{e}") + } +} + +/// What the startup read before the game's resources were loaded. +struct Setup { + props: ServerProperties, + universe: PathBuf, + level_name: String, + port: Option, + started: Instant, +} +static SETUP: OnceLock>> = OnceLock::new(); + +/// Log4j's console pattern: `[HH:mm:ss] [thread/LEVEL]: message`. +pub(super) fn log(level: &str, message: &str) { + let secs = now_millis().div_euclid(1000).rem_euclid(86_400); + println!( + "[{:02}:{:02}:{:02}] [Server thread/{level}]: {message}", + secs / 3600, + secs / 60 % 60, + secs % 60 + ); +} + +/// Main.main up to the server start: arguments, server.properties (stored +/// back at once), eula.txt. False when the process should stop here. +pub(super) fn preflight(options: &mut Options) -> Result { + let args = options.dedicated.clone().unwrap_or_default(); + let mut universe = PathBuf::from("."); + let mut world = None; + let mut port = None; + let mut init_settings = false; + let mut it = args.iter(); + while let Some(arg) = it.next() { + let name = arg.trim_start_matches('-'); + match name { + "nogui" + | "demo" + | "bonusChest" + | "forceUpgrade" + | "eraseCache" + | "recreateRegionFiles" + | "safeMode" + | "jfrProfile" => {} + "initSettings" => init_settings = true, + "help" => { + eprintln!( + "Option Description\n------ -----------\n--help\n--initSettings Initializes 'server.properties' and 'eula.txt', then quits\n--nogui\n--port (default: -1)\n--universe (default: .)\n--world " + ); + return Ok(false); + } + "universe" => universe = PathBuf::from(it.next().ok_or("--universe needs a value")?), + "world" => world = Some(it.next().ok_or("--world needs a value")?.clone()), + "port" => { + let v: i32 = it.next().ok_or("--port needs a value")?.parse()?; + port = u16::try_from(v).ok(); + } + "serverId" | "pidFile" => { + let value = it.next().ok_or("missing value")?; + if name == "pidFile" { + std::fs::write(value, std::process::id().to_string())?; + } + } + _ => return Err(format!("{arg} is not a recognized option").into()), + } + } + let started = Instant::now(); + let settings = PathBuf::from("server.properties"); + let props = ServerProperties::from_file(&settings, &mut SystemEntropy::default())?; + for w in &props.warnings { + log("WARN", w); + } + props.store(&settings)?; + let eula_file = PathBuf::from("eula.txt"); + let agreed = match eula::agreed(&eula_file) { + Ok(a) => a, + Err(w) => { + log("WARN", &w); + false + } + }; + if init_settings { + let abs = |p: &PathBuf| std::env::current_dir().map_or(p.clone(), |d| d.join(p)); + log( + "INFO", + &format!( + "Initialized '{}' and '{}'", + abs(&settings).display(), + abs(&eula_file).display() + ), + ); + return Ok(false); + } + if !agreed { + log( + "INFO", + "You need to agree to the EULA in order to run the server. Go to eula.txt for more info.", + ); + return Ok(false); + } + let level_name = world.unwrap_or_else(|| props.level_name.clone()); + SETUP + .get_or_init(|| Mutex::new(None)) + .lock() + .unwrap() + .replace(Setup { + props, + universe, + level_name, + port, + started, + }); + Ok(true) +} + +/// The operator, whitelist and ban lists (DedicatedPlayerList), shared with +/// the connection threads that check logins. +pub(super) struct Lists { + pub ops: OpList, + pub white: WhiteList, + pub bans: BanList, + pub ip_bans: IpBanList, + pub use_whitelist: bool, + pub enforce_whitelist: bool, +} +impl Lists { + fn load() -> Self { + let mut l = Self { + ops: OpList::new("ops.json"), + white: WhiteList::new("whitelist.json"), + bans: BanList::new("banned-players.json"), + ip_bans: IpBanList::new("banned-ips.json"), + use_whitelist: false, + enforce_whitelist: false, + }; + let warn = |what: &str, r: std::result::Result<(), String>| { + if let Err(e) = r { + log("WARN", &format!("Failed to load {what}: {e}")); + } + }; + // DedicatedPlayerList's constructor order. + warn("user banlist", l.bans.load()); + let _ = l.bans.save(); + warn("ip banlist", l.ip_bans.load()); + let _ = l.ip_bans.save(); + warn("operators list", l.ops.load()); + warn("white-list", l.white.load()); + let _ = l.ops.save(); + if !l.white.file.exists() { + let _ = l.white.save(); + } + l + } + /// PlayerList.isWhiteListed. + fn whitelisted(&self, user: &NameAndId) -> bool { + !self.use_whitelist || self.ops.contains(user) || self.white.contains(user) + } + /// PlayerList.canPlayerLogin before the player count. + fn can_login(&mut self, address: std::net::SocketAddr, user: &NameAndId) -> Option { + if self.bans.contains(user) + && let Some(ban) = self.bans.get(user).cloned() + { + let mut reason = translatable_tag( + "multiplayer.disconnect.banned.reason", + vec![ban_reason(&ban.reason)], + ); + if let Some(expires) = ban.expires { + append( + &mut reason, + translatable_tag( + "multiplayer.disconnect.banned.expiration", + vec![Tag::String(JavaString::from(ban_date(expires).as_str()))], + ), + ); + } + return Some(reason); + } + if !self.whitelisted(user) { + return Some(translatable("multiplayer.disconnect.not_whitelisted", &[])); + } + let ip = address.ip().to_string(); + if self.ip_bans.contains(&ip) + && let Some(ban) = self.ip_bans.get(&ip).cloned() + { + let mut reason = translatable_tag( + "multiplayer.disconnect.banned_ip.reason", + vec![ban_reason(&ban.reason)], + ); + if let Some(expires) = ban.expires { + append( + &mut reason, + translatable_tag( + "multiplayer.disconnect.banned_ip.expiration", + vec![Tag::String(JavaString::from(ban_date(expires).as_str()))], + ), + ); + } + return Some(reason); + } + None + } +} + +/// PlayerList.BAN_DATE_FORMAT ("yyyy-MM-dd 'at' HH:mm:ss z"), in UTC. +fn ban_date(millis: i64) -> String { + let s = format_date(millis); + format!("{} at {} UTC", &s[..10], &s[11..19]) +} +/// BanListEntry.getReasonMessage. +fn ban_reason(reason: &Option) -> Tag { + match reason { + Some(r) => Tag::String(JavaString::from(r.as_str())), + None => translatable("multiplayer.disconnect.banned.reason.default", &[]), + } +} +fn translatable_tag(key: &str, args: Vec) -> Tag { + Tag::compound([ + ("translate", Tag::String(JavaString::from(key))), + ("with", Tag::List(args)), + ]) +} +/// MutableComponent.append. +fn append(to: &mut Tag, sibling: Tag) { + if let Tag::Compound(map) = to { + let extra = map + .entry(JavaString::from("extra")) + .or_insert_with(|| Tag::List(Vec::new())); + if let Tag::List(items) = extra { + items.push(sibling); + } + } +} + +/// What the server loop keeps between ticks. +struct ServerState { + lists: Arc>, + console: mpsc::Receiver, + stop: bool, + autosave: bool, + empty_ticks: i32, +} + +impl Game<'_> { + /// DedicatedServer.initServer and MinecraftServer.runServer. + pub(super) fn run_dedicated(&mut self) -> Result<()> { + let Setup { + props, + universe, + level_name, + port, + started, + } = SETUP + .get() + .and_then(|m| m.lock().unwrap().take()) + .ok_or("dedicated server not prepared")?; + // The console thread starts first, as in initServer. + let (tx, console) = mpsc::channel(); + std::thread::Builder::new() + .name("Server console handler".into()) + .spawn(move || { + let stdin = std::io::stdin(); + let mut line = String::new(); + while stdin.read_line(&mut line).is_ok_and(|n| n > 0) { + if tx + .send(line.trim_end_matches(['\r', '\n']).to_owned()) + .is_err() + { + break; + } + line.clear(); + } + })?; + log("INFO", "Starting minecraft server version 26.3"); + log("INFO", "Loading properties"); + let game_mode = props.game_mode; + log( + "INFO", + &format!( + "Default game type: {}", + GAME_MODES[game_mode as usize].to_uppercase() + ), + ); + let port = port.unwrap_or(props.server_port as u16); + log( + "INFO", + &format!( + "Starting Minecraft server on {}:{port}", + if props.server_ip.is_empty() { + "*" + } else { + &props.server_ip + } + ), + ); + if props.online_mode { + // Authentication needs Mojang's session servers (an HTTPS client + // and RSA), which Horizon does not have yet: offline mode. + log( + "WARN", + "online-mode=true is not supported by Horizon yet: running in offline mode", + ); + } + log("WARN", "**** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!"); + log( + "WARN", + "The server will make no attempt to authenticate usernames. Beware.", + ); + log( + "WARN", + "While this makes the game possible to play without internet access, it also opens up the ability for hackers to connect with any username they choose.", + ); + log( + "WARN", + "To change this, set \"online-mode\" to \"true\" in the server.properties file.", + ); + let mut lists = Lists::load(); + lists.use_whitelist = props.white_list; + lists.enforce_whitelist = props.enforce_whitelist; + let lists = Arc::new(Mutex::new(lists)); + log("INFO", &format!("Preparing level \"{level_name}\"")); + let world_dir = std::env::current_dir()?.join(&universe).join(&level_name); + let folder = world_dir.to_string_lossy().into_owned(); + let new_world = !world_dir.join("level.dat").exists(); + self.open_world(&folder, Some(&level_name), Some(props.seed), game_mode == 1)?; + if new_world { + self.level.hardcore = props.hardcore; + } + // The single-player host's state is not used: players have their own files. + self.level.position = None; + // DedicatedServer.initServer: worldData.setGameType; forceDifficulty. + self.set_default_game_mode(game_mode); + self.ui.difficulty = props.difficulty as u8; + self.settings.render_distance = props.view_distance.clamp(2, 32); + self.streaming.radius = self.settings.render_distance; + self.settle_spawn()?; + let gate_lists = lists.clone(); + let gate: LoginGate = Arc::new(move |address, uuid, name| { + let user = NameAndId { + id: uuid, + name: name.to_owned(), + }; + gate_lists.lock().unwrap().can_login(address, &user) + }); + let bypass_lists = lists.clone(); + let favicon = std::fs::read("server-icon.png") + .ok() + .filter(|b| png_size(b) == Some((64, 64))); + let net = NetConfig { + bind: (!props.server_ip.is_empty()) + .then(|| props.server_ip.parse().ok()) + .flatten(), + port, + motd: props.motd.clone(), + max_players: props.max_players.max(0) as usize, + compression_threshold: props.network_compression_threshold, + enable_status: props.enable_status, + hide_online_players: props.hide_online_players, + favicon, + // DedicatedServer.enforceSecureProfile needs online mode. + enforces_secure_chat: false, + accepts_transfers: props.accepts_transfers, + announce: false, + bypasses_limit: Some(Arc::new(move |uuid| { + bypass_lists + .lock() + .unwrap() + .ops + .entries() + .iter() + .any(|e| e.user.id == uuid && e.bypasses_player_limit) + })), + gate: Some(gate), + }; + let config = HubConfig { + default_game_mode: game_mode, + guest_commands: true, + force_game_mode: props.force_game_mode, + hardcore: self.level.hardcore, + view_distance: props.view_distance.clamp(2, 32), + simulation_distance: props.simulation_distance.clamp(2, 32), + dimension: self.dim.id().to_owned(), + seed: self.level.seed, + max_players: props.max_players.max(0) as usize, + dedicated: true, + }; + let settings = LanSettings { + port, + guest_commands: true, + force_game_mode: props.force_game_mode, + }; + if let Err(e) = self.lan_host(settings, net, config, None) { + log("WARN", "**** FAILED TO BIND TO PORT!"); + log("WARN", &format!("The exception was: {e}")); + log("WARN", "Perhaps a server is already running on that port?"); + return Ok(()); + } + self.sync_ops(&lists); + log( + "INFO", + &format!( + "Done ({:.3}s)! For help, type \"help\"", + started.elapsed().as_secs_f64() + ), + ); + // DedicatedServer.initServer ends with saveEverything(false, true, true). + self.save_game()?; + self.log_saved_levels(); + let mut state = ServerState { + lists, + console, + stop: false, + autosave: true, + empty_ticks: 0, + }; + let pause_after = props.pause_when_empty_seconds * 20; + let idle_timeout = props.player_idle_timeout; + let mut next = Instant::now(); + while !state.stop { + while let Ok(line) = state.console.try_recv() { + self.console_command(&mut state, &line); + } + let players = self.lan.as_ref().map_or(0, |h| h.hub.guests.len()); + if pause_after > 0 { + if players == 0 { + state.empty_ticks += 1; + } else { + state.empty_ticks = 0; + } + } + let paused = pause_after > 0 && state.empty_ticks >= pause_after; + if paused && state.empty_ticks == pause_after { + log( + "INFO", + &format!( + "Server empty for {} seconds, pausing", + props.pause_when_empty_seconds + ), + ); + self.save_game()?; + } + self.update_dedicated_streaming(); + self.update_world(); + if paused { + // MinecraftServer.tickServer: only the connections run. + self.tick_lan(); + } else { + if !state.autosave { + // `save-off`: the periodic save inside the tick is skipped. + self.ticks = self.ticks.max(1); + } + self.tick()?; + } + if idle_timeout > 0 { + self.kick_idle(idle_timeout); + } + let rate = self.clock.rate().max(1.0); + next += Duration::from_secs_f64(1.0 / f64::from(rate)); + let now = Instant::now(); + if next > now { + std::thread::sleep(next - now); + } else if now - next > Duration::from_secs(2) { + next = now; + } + } + self.stop_dedicated() + } + + /// Load the spawn area and find the world spawn as single player does, + /// without keeping a player there. + fn settle_spawn(&mut self) -> Result<()> { + let deadline = Instant::now() + Duration::from_secs(600); + while self.player.is_none() { + self.update_world(); + self.spawn_player()?; + if Instant::now() > deadline { + return Err("the spawn area did not load".into()); + } + std::thread::sleep(Duration::from_millis(2)); + } + self.player = None; + Ok(()) + } + + /// The chunks to keep loaded: the players' (lan_update_streaming), or + /// the spawn chunk while nobody is online. + fn update_dedicated_streaming(&mut self) { + let center = self + .lan + .as_ref() + .and_then(|h| h.hub.guests.iter().find(|g| g.placed).map(|g| g.chunk())) + .or_else(|| { + self.spawn + .map(|s| ((s.x.floor() as i32) >> 4, (s.z.floor() as i32) >> 4)) + }); + if let Some(c) = center { + self.streaming.center = c; + } + } + + /// ServerGamePacketListenerImpl.tick: players idle longer than the + /// timeout are kicked. + fn kick_idle(&mut self, minutes: i32) { + let Some(host) = self.lan.as_mut() else { + return; + }; + let limit = u64::try_from(minutes).unwrap_or(0) * 1200; + let idle: Vec = host + .hub + .guests + .iter() + .filter(|g| host.hub.ticks.saturating_sub(g.last_action_tick) > limit) + .map(|g| g.conn) + .collect(); + for conn in idle { + host.hub + .kick(conn, translatable("multiplayer.disconnect.idling", &[])); + } + } + + /// Ops' permission levels for the hub (PlayerList.getProfilePermissions). + fn sync_ops(&mut self, lists: &Arc>) { + let Some(host) = self.lan.as_mut() else { + return; + }; + host.hub.ops = lists + .lock() + .unwrap() + .ops + .entries() + .iter() + .map(|e| (e.user.id, e.level as u8)) + .collect(); + host.hub.resend_commands(); + } + + /// A known player by name: online first, else the offline profile. + fn resolve(&self, name: &str) -> NameAndId { + self.lan + .as_ref() + .and_then(|h| { + h.hub + .guests + .iter() + .find(|g| g.name.eq_ignore_ascii_case(name)) + }) + .map_or_else( + || NameAndId::offline(name), + |g| NameAndId { + id: g.uuid, + name: g.name.clone(), + }, + ) + } + + /// MinecraftServer.sendSystemMessage: what the console is told. + fn console_reply(&self, text: Text) { + log("INFO", &format!("System chat: {}", text.plain())); + } + + /// DedicatedServer.handleConsoleInputs: one console line, run as the + /// server (permission level 4). The dedicated-only commands are here; + /// everything else goes to the game's dispatcher. + fn console_command(&mut self, state: &mut ServerState, line: &str) { + let line = line.strip_prefix('/').unwrap_or(line); + let mut words = line.split_whitespace(); + let Some(command) = words.next() else { + return; + }; + let rest: Vec<&str> = words.collect(); + let lit = |s: &str| Text::literal(s.to_owned()); + match command { + "stop" => { + self.console_reply(Text::translate("commands.stop.stopping", &[])); + state.stop = true; + } + "save-all" => { + self.console_reply(Text::translate("commands.save.saving", &[])); + match self.save_game() { + Ok(()) => self.console_reply(Text::translate("commands.save.success", &[])), + Err(e) => log("ERROR", &format!("{e}")), + } + } + "save-off" => { + if state.autosave { + state.autosave = false; + self.console_reply(Text::translate("commands.save.disabled", &[])); + } else { + self.console_reply(Text::translate("commands.save.alreadyOff", &[])); + } + } + "save-on" => { + if state.autosave { + self.console_reply(Text::translate("commands.save.alreadyOn", &[])); + } else { + state.autosave = true; + self.console_reply(Text::translate("commands.save.enabled", &[])); + } + } + "list" => { + let guests: Vec<(String, u128)> = self + .lan + .as_ref() + .map(|h| { + h.hub + .guests + .iter() + .map(|g| (g.name.clone(), g.uuid)) + .collect() + }) + .unwrap_or_default(); + let max = self + .lan + .as_ref() + .map_or(0, |h| h.hub.lan.shared.max_players()); + let names: Vec = if rest.first() == Some(&"uuids") { + guests + .iter() + .map(|(n, u)| { + format!("{n} ({})", horizon_protocol::crypto::uuid_string(*u)) + }) + .collect() + } else { + guests.iter().map(|(n, _)| n.clone()).collect() + }; + self.console_reply(Text::translate( + "commands.list.players", + &[ + lit(&guests.len().to_string()), + lit(&max.to_string()), + lit(&names.join(", ")), + ], + )); + } + "op" | "deop" if !rest.is_empty() => { + let user = self.resolve(rest[0]); + let level = 4; + let changed = { + let mut l = state.lists.lock().unwrap(); + if command == "op" { + if l.ops.contains(&user) { + false + } else { + l.ops.add(OpEntry { + user: user.clone(), + level, + bypasses_player_limit: false, + }) + } + } else { + l.ops.remove(&user) + } + }; + let key = match (command, changed) { + ("op", true) => "commands.op.success", + ("op", false) => "commands.op.failed", + (_, true) => "commands.deop.success", + _ => "commands.deop.failed", + }; + self.console_reply(Text::translate(key, &[lit(&user.name)])); + let lists = state.lists.clone(); + self.sync_ops(&lists); + } + "whitelist" => self.console_whitelist(state, &rest), + "ban" if !rest.is_empty() => { + let user = self.resolve(rest[0]); + let reason = (rest.len() > 1).then(|| rest[1..].join(" ")); + let added = state.lists.lock().unwrap().bans.add(BanEntry::new( + user.clone(), + Some("Server".into()), + None, + reason.clone(), + )); + if added { + self.console_reply(Text::translate( + "commands.ban.success", + &[ + lit(&user.name), + reason.as_deref().map_or_else( + || { + Text::translate( + "multiplayer.disconnect.banned.reason.default", + &[], + ) + }, + lit, + ), + ], + )); + self.kick_uuid(user.id, translatable("multiplayer.disconnect.banned", &[])); + } else { + self.console_reply(Text::translate("commands.ban.failed", &[])); + } + } + "ban-ip" if !rest.is_empty() => { + let target = rest[0]; + let ip = if target.parse::().is_ok() { + Some(target.to_owned()) + } else { + self.lan.as_ref().and_then(|h| { + h.hub + .guests + .iter() + .find(|g| g.name.eq_ignore_ascii_case(target)) + .map(|g| g.address.ip().to_string()) + }) + }; + let Some(ip) = ip else { + self.console_reply(Text::translate("commands.banip.invalid", &[])); + return; + }; + let reason = (rest.len() > 1).then(|| rest[1..].join(" ")); + let added = state.lists.lock().unwrap().ip_bans.add(BanEntry::new( + ip.clone(), + Some("Server".into()), + None, + reason.clone(), + )); + if !added { + self.console_reply(Text::translate("commands.banip.failed", &[])); + return; + } + self.console_reply(Text::translate( + "commands.banip.success", + &[ + lit(&ip), + reason.as_deref().map_or_else( + || Text::translate("multiplayer.disconnect.banned.reason.default", &[]), + lit, + ), + ], + )); + let banned: Vec = self + .lan + .as_ref() + .map(|h| { + h.hub + .guests + .iter() + .filter(|g| g.address.ip().to_string() == ip) + .map(|g| g.uuid) + .collect() + }) + .unwrap_or_default(); + if !banned.is_empty() { + self.console_reply(Text::translate( + "commands.banip.info", + &[lit(&banned.len().to_string()), lit("")], + )); + } + for uuid in banned { + self.kick_uuid(uuid, translatable("multiplayer.disconnect.ip_banned", &[])); + } + } + "pardon" if !rest.is_empty() => { + let user = self.resolve(rest[0]); + let removed = state.lists.lock().unwrap().bans.remove(&user); + let key = if removed { + "commands.pardon.success" + } else { + "commands.pardon.failed" + }; + self.console_reply(Text::translate(key, &[lit(&user.name)])); + } + "pardon-ip" if !rest.is_empty() => { + let ip = rest[0].to_owned(); + if ip.parse::().is_err() { + self.console_reply(Text::translate("commands.pardonip.invalid", &[])); + return; + } + let removed = state.lists.lock().unwrap().ip_bans.remove(&ip); + let key = if removed { + "commands.pardonip.success" + } else { + "commands.pardonip.failed" + }; + self.console_reply(Text::translate(key, &[lit(&ip)])); + } + "banlist" => { + let l = state.lists.lock().unwrap(); + let mut lines = Vec::new(); + let which = rest.first().copied().unwrap_or(""); + if which != "ips" { + for b in l.bans.entries() { + lines.push((b.user.name.clone(), b.source.clone(), b.reason.clone())); + } + } + if which != "players" { + for b in l.ip_bans.entries() { + lines.push((b.user.clone(), b.source.clone(), b.reason.clone())); + } + } + drop(l); + if lines.is_empty() { + self.console_reply(Text::translate("commands.banlist.none", &[])); + } else { + self.console_reply(Text::translate( + "commands.banlist.list", + &[lit(&lines.len().to_string())], + )); + for (name, source, reason) in lines { + self.console_reply(Text::translate( + "commands.banlist.entry", + &[ + lit(&name), + lit(&source), + reason.as_deref().map_or_else( + || { + Text::translate( + "multiplayer.disconnect.banned.reason.default", + &[], + ) + }, + lit, + ), + ], + )); + } + } + } + "kick" if !rest.is_empty() => { + let user = self.resolve(rest[0]); + let online = self + .lan + .as_ref() + .is_some_and(|h| h.hub.guests.iter().any(|g| g.uuid == user.id)); + if !online { + self.console_reply(Text::translate("argument.entity.notfound.player", &[])); + return; + } + let reason = if rest.len() > 1 { + Tag::String(JavaString::from(rest[1..].join(" ").as_str())) + } else { + translatable("multiplayer.disconnect.kicked", &[]) + }; + self.kick_uuid(user.id, reason); + self.console_reply(Text::translate( + "commands.kick.success", + &[ + lit(&user.name), + if rest.len() > 1 { + lit(&rest[1..].join(" ")) + } else { + Text::translate("multiplayer.disconnect.kicked", &[]) + }, + ], + )); + } + "say" if !rest.is_empty() => { + // SayCommand from the console: an unsigned system chat + // message of type say_command, logged as not secure. + let message = rest.join(" "); + log("INFO", &format!("[Not Secure] [Server] {message}")); + if let Some(h) = &self.lan { + h.hub.say("Server", &message); + } + } + _ => { + // The game's dispatcher, captured for the console. + self.chat_capture = Some(Vec::new()); + self.run_command(line); + for text in self.chat_capture.take().unwrap_or_default() { + self.console_reply(text); + } + } + } + } + + fn console_whitelist(&mut self, state: &mut ServerState, rest: &[&str]) { + let lit = |s: &str| Text::literal(s.to_owned()); + match rest { + ["on"] | ["off"] => { + let on = rest[0] == "on"; + let mut l = state.lists.lock().unwrap(); + let key = match (on, l.use_whitelist) { + (true, true) => "commands.whitelist.alreadyOn", + (true, false) => "commands.whitelist.enabled", + (false, false) => "commands.whitelist.alreadyOff", + (false, true) => "commands.whitelist.disabled", + }; + l.use_whitelist = on; + drop(l); + self.store_property("white-list", on.to_string()); + self.console_reply(Text::translate(key, &[])); + } + ["list"] => { + let names: Vec = state + .lists + .lock() + .unwrap() + .white + .entries() + .iter() + .map(|e| e.user.name.clone()) + .collect(); + if names.is_empty() { + self.console_reply(Text::translate("commands.whitelist.none", &[])); + } else { + self.console_reply(Text::translate( + "commands.whitelist.list", + &[lit(&names.len().to_string()), lit(&names.join(", "))], + )); + } + } + ["reload"] => { + let _ = state.lists.lock().unwrap().white.load(); + self.console_reply(Text::translate("commands.whitelist.reloaded", &[])); + } + ["add", name] | ["remove", name] => { + let user = self.resolve(name); + let mut l = state.lists.lock().unwrap(); + let (ok, key) = if rest[0] == "add" { + if l.white.contains(&user) { + (false, "commands.whitelist.add.failed") + } else { + l.white.add(WhiteEntry { user: user.clone() }); + (true, "commands.whitelist.add.success") + } + } else if l.white.remove(&user) { + (true, "commands.whitelist.remove.success") + } else { + (false, "commands.whitelist.remove.failed") + }; + drop(l); + let args = if ok { + vec![lit(&user.name)] + } else { + Vec::new() + }; + self.console_reply(Text::translate(key, &args)); + } + _ => self.console_reply(Text::translate("command.unknown.argument", &[])), + } + } + + /// DedicatedServerSettings.update: change one key and store the file. + fn store_property(&mut self, key: &str, value: String) { + let path = PathBuf::from("server.properties"); + if let Ok(p) = ServerProperties::from_file(&path, &mut SystemEntropy::default()) + && let Ok(p) = p.update(key, value) + { + let _ = p.store(&path); + } + } + + fn kick_uuid(&mut self, uuid: u128, reason: Tag) { + if let Some(h) = self.lan.as_mut() { + let conns: Vec = h + .hub + .guests + .iter() + .filter(|g| g.uuid == uuid) + .map(|g| g.conn) + .collect(); + for conn in conns { + h.hub.kick(conn, reason.clone()); + } + } + } + + /// MinecraftServer.stopServer: players saved and sent away, worlds saved. + /// MinecraftServer.saveAllChunks, not silent: one line per level, in + /// the server's level order. + fn log_saved_levels(&self) { + for dimension in [ + "minecraft:overworld", + "minecraft:the_nether", + "minecraft:the_end", + ] { + log( + "INFO", + &format!( + "Saving chunks for level 'ServerLevel[{}]'/{dimension}", + self.level.name + ), + ); + } + } + + fn stop_dedicated(&mut self) -> Result<()> { + log("INFO", "Stopping server"); + log("INFO", "Saving players"); + if let Some(h) = self.lan.as_mut() { + for g in &h.hub.guests { + horizon_client::lan::playerdata::save(g, &self.world_root); + } + let conns: Vec = h.hub.guests.iter().map(|g| g.conn).collect(); + for conn in conns { + h.hub.kick( + conn, + translatable("multiplayer.disconnect.server_shutdown", &[]), + ); + } + } + std::thread::sleep(Duration::from_millis(100)); + log("INFO", "Saving worlds"); + self.save_game()?; + self.log_saved_levels(); + self.lan_close(false); + Ok(()) + } +} + +/// The width and height of a PNG (its IHDR chunk). +fn png_size(bytes: &[u8]) -> Option<(u32, u32)> { + if bytes.len() < 24 || &bytes[..8] != b"\x89PNG\r\n\x1a\n" || &bytes[12..16] != b"IHDR" { + return None; + } + let w = u32::from_be_bytes(bytes[16..20].try_into().ok()?); + let h = u32::from_be_bytes(bytes[20..24].try_into().ok()?); + Some((w, h)) +} diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 2dffef7a6..a29ca085c 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -7,7 +7,7 @@ use horizon_client::{ Result, items::ItemStack, lan::{ - self, Lan, MAX_PLAYERS, + self, Lan, MAX_PLAYERS, NetConfig, chunks::ChunkEncoder, data::SyncData, entity::EntityState, @@ -345,6 +345,40 @@ impl Game<'_> { if self.lan.is_some() || self.remote.is_some() || self.title.is_some() { return Err("already open".into()); } + let motd = format!("{PLAYER_NAME} - {}", self.level.name); + let host_uuid = self.level.player_uuid.map_or_else( + || offline_uuid(PLAYER_NAME), + |u| { + u128::from(u[0] as u32) << 96 + | u128::from(u[1] as u32) << 64 + | u128::from(u[2] as u32) << 32 + | u128::from(u[3] as u32) + }, + ); + let config = HubConfig { + default_game_mode: self.level.mode(), + guest_commands: settings.guest_commands, + force_game_mode: settings.force_game_mode, + hardcore: self.level.hardcore, + view_distance: self.settings.render_distance.clamp(2, 32), + simulation_distance: self.settings.render_distance.clamp(2, 32), + dimension: self.dim.id().to_owned(), + seed: self.level.seed, + max_players: MAX_PLAYERS, + dedicated: false, + }; + let net = NetConfig::lan(settings.port, motd, MAX_PLAYERS); + self.lan_host(settings, net, config, Some(host_uuid)) + } + /// Start the network side and the play-state hub: for the LAN (with the + /// host's own profile reserved) or for a dedicated server (no host). + pub(super) fn lan_host( + &mut self, + settings: LanSettings, + net: NetConfig, + config: HubConfig, + host: Option, + ) -> Result<()> { let root = self.options.resources.clone(); let sync = SYNC .get_or_init(|| SyncData::load(&root).map(Arc::new)) @@ -362,7 +396,7 @@ impl Game<'_> { .get("minecraft:worldgen/biome") .map(|v| v.iter().map(|(n, _)| n.clone()).collect()) .unwrap_or_default(); - let encoder = ChunkEncoder::new( + let mut encoder = ChunkEncoder::new( self.res .facts .states @@ -372,37 +406,20 @@ impl Game<'_> { &biome_names, ®istry, ); - let motd = format!("{PLAYER_NAME} - {}", self.level.name); - let lan = Lan::start(sync.clone(), settings.port, motd, MAX_PLAYERS)?; - let host_uuid = self.level.player_uuid.map_or_else( - || offline_uuid(PLAYER_NAME), - |u| { - u128::from(u[0] as u32) << 96 - | u128::from(u[1] as u32) << 64 - | u128::from(u[2] as u32) << 32 - | u128::from(u[3] as u32) - }, + encoder.tables = Some( + self.save + .tables + .get_or_init(|| Arc::new(horizon_client::save::SaveTables::new(&self.res.assets))) + .clone(), ); - lan.reserve(host_uuid); + let lan = Lan::start_with(sync.clone(), net)?; + if let Some(uuid) = host { + lan.reserve(uuid); + } + let host_uuid = host.unwrap_or(0); self.next_entity += 1; let host_entity = self.next_entity as i32; - let hub = Hub::new( - lan, - sync, - encoder, - HubConfig { - default_game_mode: self.level.mode(), - guest_commands: settings.guest_commands, - force_game_mode: settings.force_game_mode, - hardcore: self.level.hardcore, - view_distance: self.settings.render_distance.clamp(2, 32), - simulation_distance: self.settings.render_distance.clamp(2, 32), - dimension: self.dim.id().to_owned(), - seed: self.level.seed, - max_players: MAX_PLAYERS, - dedicated: false, - }, - ); + let hub = Hub::new(lan, sync, encoder, config); let port = hub.lan.port; self.world.block_log = Some(Vec::new()); self.lan = Some(LanHost { @@ -543,7 +560,9 @@ impl Game<'_> { let Some(mut host) = self.lan.take() else { return; }; - if let Some(state) = self.lan_host_state(&host) { + let state = self.lan_host_state(&host); + // A dedicated server runs the hub without a host player. + if state.is_some() || host.hub.config.dedicated { let has_guests = !host.hub.guests.is_empty(); let entities = if has_guests { self.lan_entities(&mut host.hub) @@ -554,7 +573,7 @@ impl Game<'_> { let events = { let mut ctx = TickCtx { world: &mut self.world, - host: Some(&state), + host: state.as_ref(), entities: &entities, next_entity: &mut self.next_entity, spawn, @@ -570,18 +589,56 @@ impl Game<'_> { }; host.hub.tick(&mut ctx) }; - if host.host_mode != state.game_mode { + if let Some(state) = &state + && host.host_mode != state.game_mode + { host.host_mode = state.game_mode; host.hub.set_host_game_mode(state.uuid, state.game_mode); } for event in events { match event { HostEvent::Joined(conn) => { - self.lan_guest_joined(&mut host, conn, &state, &entities, spawn) + self.lan_guest_joined(&mut host, conn, state.as_ref(), &entities, spawn); + if self.options.dedicated.is_some() + && let Some(g) = host.hub.guest(conn) + { + // PlayerList.placeNewPlayer, then the broadcast join message. + let [x, y, z] = g.position; + super::dedicated::log( + "INFO", + &format!( + "{}[/{}] logged in with entity id {} at ({}, {}, {})", + g.name, + g.address, + g.entity_id, + super::dedicated::java_double(x), + super::dedicated::java_double(y), + super::dedicated::java_double(z) + ), + ); + super::dedicated::log( + "INFO", + &format!("System chat: {} joined the game", g.name), + ); + } } HostEvent::Left(guest) => { playerdata::save(&guest, &self.world_root); host.models.remove(&guest.conn); + if self.options.dedicated.is_some() && guest.placed { + super::dedicated::log( + "INFO", + &format!("{} lost connection: {}", guest.name, guest.left_reason), + ); + super::dedicated::log( + "INFO", + &format!("System chat: {} left the game", guest.name), + ); + } + } + // MinecraftServer.logChatMessage of an unsigned message. + HostEvent::Chat(line) if self.options.dedicated.is_some() => { + super::dedicated::log("INFO", &format!("[Not Secure] {line}")); } HostEvent::Chat(line) => self.system_message(Text::literal(line)), HostEvent::Packet(conn, packet) => { @@ -602,7 +659,7 @@ impl Game<'_> { &mut self, host: &mut LanHost, conn: u32, - state: &HostState, + state: Option<&HostState>, entities: &[EntityState], spawn: [f64; 3], ) { @@ -624,7 +681,7 @@ impl Game<'_> { } let mut ctx = TickCtx { world: &mut self.world, - host: Some(state), + host: state, entities, next_entity: &mut self.next_entity, spawn, @@ -688,7 +745,9 @@ impl Game<'_> { .guests .iter() .filter(|g| g.placed) - .map(|g| (g.chunk(), g.view_distance + 1)) + // The tracked square's corners and their lit neighbours lie + // within (view distance + 2) * √2 of the player's chunk. + .map(|g| (g.chunk(), (g.view_distance + 2) * 3 / 2 + 1)) .collect(); } diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index c86774d08..6366ca474 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -511,6 +511,21 @@ impl Game<'_> { /// Commands.performCommand for a guest with command access. fn guest_command(&mut self, host: &mut LanHost, conn: u32, command: &str) { + // ListPlayersCommand needs no permission and lists every player. + let words: Vec<&str> = command.trim_start_matches('/').split(' ').collect(); + if words[0] == "list" && (words.len() == 1 || words[1..] == ["uuids"]) { + let me = (self.options.dedicated.is_none() && self.player.is_some()) + .then_some((host.host_uuid, super::chat::PLAYER_NAME)); + let content = host.hub.list_reply(me, words.len() == 2); + host.hub.send_to( + conn, + &C::SystemChat(play::SystemChat { + content, + overlay: false, + }), + ); + return; + } if !host.settings.guest_commands || !self.cheats { host.hub.send_to( conn, diff --git a/crates/horizon-client/src/game/net_harness.rs b/crates/horizon-client/src/game/net_harness.rs new file mode 100644 index 000000000..755a4c562 --- /dev/null +++ b/crates/horizon-client/src/game/net_harness.rs @@ -0,0 +1,93 @@ +//! Headless multiplayer client for `tools/test_dedicated_server_parity.py`: +//! with `--server` and `HORIZON_NET_REPORT` set, the client joins without a +//! window, waits until it stands in the world with the chunks around it, +//! sends a chat message and `/list`, collects the chat it is shown, leaves +//! and writes a JSON report (position, chunks, chat lines, whether the +//! server kept it connected throughout). +use super::Game; +use horizon_client::Result; +use serde_json::json; +use std::{ + path::Path, + time::{Duration, Instant}, +}; + +impl Game<'_> { + pub(super) fn run_net_harness(&mut self, report: &Path) -> Result<()> { + let deadline = Instant::now() + Duration::from_secs(120); + let mut next = Instant::now(); + let mut spawned_at: Option = None; + let mut last_chunks = 0; + let mut quiet_since = Instant::now(); + let mut step = 0; + let mut step_at = Instant::now(); + let mut position = None; + let mut chunks = 0; + let mut error = None; + while Instant::now() < deadline { + self.update_world(); + if self.remote.is_none() { + error = Some("disconnected".to_owned()); + break; + } + if Instant::now() >= next { + next += Duration::from_millis(50); + self.tick()?; + } + let now = Instant::now(); + if let Some(p) = &self.player { + spawned_at.get_or_insert(now); + position.get_or_insert([p.position.x, p.position.y, p.position.z]); + } + let count = self.world.chunks.len(); + if count != last_chunks { + last_chunks = count; + quiet_since = now; + } + match step { + 0 if spawned_at.is_some() + && count > 0 + && now - quiet_since > Duration::from_secs(3) => + { + chunks = count; + self.send_chat("hello from the horizon client"); + step = 1; + step_at = now; + } + 1 if now - step_at > Duration::from_millis(1500) => { + self.send_chat("/list"); + step = 2; + step_at = now; + } + 2 if now - step_at > Duration::from_millis(1500) => { + step = 3; + break; + } + _ => {} + } + std::thread::sleep(Duration::from_millis(5)); + } + if step < 3 && error.is_none() { + error = Some(format!("timed out at step {step}")); + } + let chat: Vec = self + .remote + .as_ref() + .map(|r| r.chat.iter().map(|(t, _)| t.clone()).collect()) + .unwrap_or_default(); + if self.remote.is_some() { + self.leave_server(None)?; + } + std::fs::write( + report, + json!({ + "position": position, + "chunks": chunks, + "chat": chat, + "error": error, + }) + .to_string(), + )?; + Ok(()) + } +} diff --git a/crates/horizon-client/src/game/perspective.rs b/crates/horizon-client/src/game/perspective.rs index cd387342f..33b9905a0 100644 --- a/crates/horizon-client/src/game/perspective.rs +++ b/crates/horizon-client/src/game/perspective.rs @@ -144,6 +144,7 @@ pub(super) fn player_type(root: &Path) -> Option> { armor_toughness: 0.0, knockback_resistance: 0.0, jump_strength: 0.42, + attributes: Default::default(), gravity: 0.08, safe_fall_distance: 3.0, fall_damage_multiplier: 1.0, diff --git a/crates/horizon-client/src/game/villagers.rs b/crates/horizon-client/src/game/villagers.rs index cbf3d015b..f0bdf5009 100644 --- a/crates/horizon-client/src/game/villagers.rs +++ b/crates/horizon-client/src/game/villagers.rs @@ -8,7 +8,6 @@ use horizon_client::{ mobs::{Mob, Species, spawning, villager}, }; use horizon_core::{geometry::Vec3, random::RandomSource}; -use std::collections::HashMap; fn block_of(p: Vec3) -> [i32; 3] { [p.x.floor() as i32, p.y.floor() as i32, p.z.floor() as i32] @@ -59,68 +58,13 @@ impl Game<'_> { }; let now = self.world.game_time; let items = &self.res.items; - let max = |item: &str| items.get(item).map_or(64, |i| i.max_stack); - // Villager.gossip: both must be past their cool-down. - let ready = { - let ea = villager::ext(&self.mobs[ia]); - let eb = villager::ext(&self.mobs[ib]); - ea.is_none_or(|e| e.can_gossip(now)) && eb.is_none_or(|e| e.can_gossip(now)) - }; - let mut golem = false; - if ready { - let source = villager::ext(&self.mobs[ib]).cloned().unwrap_or_default(); - let me = &mut self.mobs[ia]; - let mut random = me.random.clone(); - let e = villager::ext_mut(me); - e.gossips.transfer_from(&source.gossips, &mut random, 10); - e.last_gossip_time = now; - me.random = random; - villager::ext_mut(&mut self.mobs[ib]).last_gossip_time = now; - golem = true; + let meeting = villager::meet(&mut self.mobs, ia, ib, now, |item| { + items.get(item).map_or(64, |i| i.max_stack) + }); + for stack in meeting.thrown { + self.villager_throw(meeting.from, meeting.eye, meeting.to, stack); } - // Surplus food goes to a hungry neighbour (farmers hand it to anyone). - let farmer = villager::is_farmer(&self.mobs[ia]); - let (from, eye, to) = ( - self.mobs[ia].position, - self.mobs[ia].eye_y(), - self.mobs[ib].position, - ); - let wants_more = villager::ext(&self.mobs[ib]).is_none_or(|e| e.wants_more_food()); - let excess = villager::ext(&self.mobs[ia]).is_some_and(|e| e.has_excess_food()); - let mut thrown = Vec::new(); - if excess - && (farmer || wants_more) - && let Some(s) = villager::ext_mut(&mut self.mobs[ia]) - .take_half_stack(|s| villager::food_points(&s.item) > 0, max) - { - thrown.push(s); - } - // A farmer's wheat. - if farmer - && villager::ext(&self.mobs[ia]).is_some_and(|e| e.count_item("minecraft:wheat") > 32) - && let Some(s) = villager::ext_mut(&mut self.mobs[ia]) - .take_half_stack(|s| &*s.item == "minecraft:wheat", max) - { - thrown.push(s); - } - // Wares the neighbour's profession asks for and ours does not. - let theirs = villager::requested_items(&self.mobs[ib]); - let mine = villager::requested_items(&self.mobs[ia]); - let wanted: Vec<&str> = theirs - .iter() - .copied() - .filter(|i| !mine.contains(i)) - .collect(); - if !wanted.is_empty() - && let Some(s) = villager::ext_mut(&mut self.mobs[ia]) - .take_half_stack(|s| wanted.contains(&&*s.item), max) - { - thrown.push(s); - } - for stack in thrown { - self.villager_throw(from, eye, to, stack); - } - if golem { + if meeting.gossiped { self.villager_golem_check(a, 5); } } @@ -219,46 +163,39 @@ impl Game<'_> { /// `VillagerMakeLove.tryToGiveBirth`: both eat, a free bed within 48 /// blocks is taken for the child, who is a baby villager of one of /// three villager types. - pub(super) fn villager_birth(&mut self, parent: u32, partner: u32) { + pub(super) fn villager_birth( + &mut self, + parent: u32, + partner: u32, + child: Option<([i32; 3], Option)>, + ) { let (Some(ia), Some(ib)) = (self.villager_index(parent), self.villager_index(partner)) else { return; }; - villager::ext_mut(&mut self.mobs[ia]).eat_and_digest(); villager::ext_mut(&mut self.mobs[ib]).eat_and_digest(); let from = self.mobs[ia].position; - let angry = |g: &mut Self| { + let Some((bed, variant)) = child else { for i in [ia, ib] { - let p = g.mobs[i].position; - g.particles_at("angry_villager", [p.x, p.y + 1.8, p.z]); + let p = self.mobs[i].position; + self.particles_at("angry_villager", [p.x, p.y + 1.8, p.z]); } - }; - let Some(bed) = self.take_vacant_bed(block_of(from)) else { - angry(self); return; }; - // Villager.getBreedOffspring: half the biome's type, a quarter each parent's. - let roll = self.mobs[ia].random.next_double(); - let kind_of = |m: &Mob| m.extra.variant; - let facts = self.biome_facts( - from.x.floor() as i32, - from.y.floor() as i32, - from.z.floor() as i32, - ); - let variant = if roll < 0.5 { + let variant = variant.unwrap_or_else(|| { + let facts = self.biome_facts( + from.x.floor() as i32, + from.y.floor() as i32, + from.z.floor() as i32, + ); spawning::villager_type(&facts) - } else if roll < 0.75 { - kind_of(&self.mobs[ia]) - } else { - kind_of(&self.mobs[ib]) - }; + }); + self.mobs[ib].extra.age = 6000; let Some(id) = self.spawn_mob_for("minecraft:villager", [from.x, from.y, from.z], true, true) else { return; }; - self.mobs[ia].extra.age = 6000; - self.mobs[ib].extra.age = 6000; if let Some(child) = self.mobs.iter_mut().find(|m| m.id == id) { child.extra.variant = variant; child.extra.variant2 = villager::NONE; @@ -267,44 +204,6 @@ impl Game<'_> { } self.particles_at("heart", [from.x, from.y + 1.8, from.z]); } - /// `PoiManager.take(HOME, ..., 48)`: the nearest unclaimed bed head. - fn take_vacant_bed(&self, from: [i32; 3]) -> Option<[i32; 3]> { - let claimed: Vec<[i32; 3]> = self - .mobs - .iter() - .filter(|m| m.species() == Species::Villager) - .filter_map(|m| m.extra.bed) - .collect(); - let mut known: HashMap = HashMap::new(); - let mut best: Option<(i32, [i32; 3])> = None; - for y in from[1] - 8..=from[1] + 8 { - for z in from[2] - 48..=from[2] + 48 { - for x in from[0] - 48..=from[0] + 48 { - let state = self.world.block(x, y, z); - if self.res.facts.is_air(state) { - continue; - } - let free_head = *known.entry(state.0).or_insert_with(|| { - self.res.facts.get(state).name.ends_with("_bed") - && self.res.assets.registry.state(state).is_some_and(|s| { - s.properties.get("part").map(String::as_str) == Some("head") - && s.properties.get("occupied").map(String::as_str) - != Some("true") - }) - }); - let d = (x - from[0]).pow(2) + (y - from[1]).pow(2) + (z - from[2]).pow(2); - if free_head - && d <= 48 * 48 - && !claimed.contains(&[x, y, z]) - && best.is_none_or(|(b, _)| d < b) - { - best = Some((d, [x, y, z])); - } - } - } - } - best.map(|(_, p)| p) - } fn particles_at(&mut self, kind: &'static str, at: [f64; 3]) { if let Some(ty) = horizon_client::particles::ParticleType::from_name(kind) { let options = horizon_client::particles::ParticleOptions::simple(ty); diff --git a/crates/horizon-client/src/gossip.rs b/crates/horizon-client/src/gossip.rs index 63cc7e265..9a964a3e6 100644 --- a/crates/horizon-client/src/gossip.rs +++ b/crates/horizon-client/src/gossip.rs @@ -110,6 +110,15 @@ impl Gossips { pub fn clear(&mut self) { self.map.clear(); } + /// Every (target, type, value) held. + pub fn entries(&self) -> impl Iterator + '_ { + self.map.iter().flat_map(|(t, e)| { + GossipType::ALL + .into_iter() + .filter(move |k| e[k.index()] != 0) + .map(move |k| (*t, k, e[k.index()])) + }) + } pub fn get(&self, target: Target, kind: GossipType) -> i32 { self.map.get(&target).map_or(0, |e| e[kind.index()]) } diff --git a/crates/horizon-client/src/lan/chunks.rs b/crates/horizon-client/src/lan/chunks.rs index 70538468b..2451d373f 100644 --- a/crates/horizon-client/src/lan/chunks.rs +++ b/crates/horizon-client/src/lan/chunks.rs @@ -88,6 +88,8 @@ pub struct ChunkEncoder { biome_bits: u8, /// The block registry id of each state's block, for BlockEvent. pub block_ids: Vec, + /// The save's tables, for the heightmaps. + pub tables: Option>, } impl ChunkEncoder { /// `biome_names`: the internal biome list in order; `biome_registry`: @@ -140,6 +142,7 @@ impl ChunkEncoder { biome_ids, biome_bits: ceil_log2(biome_registry.len()), block_ids, + tables: None, } } pub fn entity_type(&self, state: u16) -> Option { @@ -194,6 +197,38 @@ impl ChunkEncoder { } /// The chunk column with its light and block entities. + /// The non-air sections of a column as bits (LevelChunkSection.hasOnlyAir). + fn occupied(&self, chunk: &Chunk) -> u64 { + let mut bits = 0u64; + for (i, s) in chunk.sections.iter().enumerate() { + let mut unpacked = [0u16; BLOCKS]; + s.unpack(&mut unpacked); + let any = unpacked + .iter() + .any(|&v| self.flags.get(usize::from(v)).copied().unwrap_or(0) & NON_AIR != 0); + if any { + bits |= 1 << i; + } + } + bits + } + + /// The chunk column with its light and block entities. `world` gives the + /// neighbouring columns, whose sections decide which light layers exist. + pub fn encode_in(&self, chunk: &Chunk, world: &crate::world::World) -> LevelChunkWithLight { + let mut around = 0u64; + for dz in -1..=1 { + for dx in -1..=1 { + if let Some(c) = world.chunks.get(&(chunk.x + dx, chunk.z + dz)) { + around |= self.occupied(c); + } + } + } + let mut packet = self.encode(chunk, &world.block_entities); + packet.light = light_data(chunk, around); + packet + } + pub fn encode( &self, chunk: &Chunk, @@ -215,12 +250,12 @@ impl ChunkEncoder { packed_xz: ((x << 4) | z) as i8, y: wy as i16, kind, + // BlockEntityInfo.create: an empty update tag is not sent. tag: block_entities .get(&pos) .and_then(entity_tag) - .unwrap_or_else( - || Tag::compound(Vec::<(&str, Tag)>::new()), - ), + .or_else(|| saved_update_tag(chunk, pos)) + .unwrap_or(Tag::End), }); } } @@ -229,19 +264,118 @@ impl ChunkEncoder { } sections.push(encoded); } + java_hash_order(&mut entity_infos, |e| { + ( + chunk.x * 16 + i32::from(e.packed_xz as u8 >> 4), + i32::from(e.y), + chunk.z * 16 + i32::from(e.packed_xz as u8 & 15), + ) + }); LevelChunkWithLight { x: chunk.x, z: chunk.z, chunk: ChunkData { - heightmaps: Vec::new(), + heightmaps: self + .tables + .as_ref() + .map(|t| crate::save::client_heightmaps(chunk, t)) + .unwrap_or_default(), buffer: ChunkData::from_sections(§ions), block_entities: entity_infos, }, - light: light_data(chunk), + light: light_data(chunk, u64::MAX), } } } +/// The order LevelChunk.getBlockEntities() iterates in: a fastutil +/// Object2ObjectOpenHashMap keyed by BlockPos (linear probing from +/// HashCommon.mix(hashCode), grown at 3/4 by reinserting from the top of +/// the table, iterated from the top down), filled in the order of the +/// java.util.HashMap of pending block entities that the loaded chunk's list +/// (`items` in section order) went through. +fn java_hash_order(items: &mut Vec, pos: impl Fn(&T) -> (i32, i32, i32)) { + // Vec3i.hashCode. + let hash = |&(x, y, z): &(i32, i32, i32)| { + y.wrapping_add(z.wrapping_mul(31)) + .wrapping_mul(31) + .wrapping_add(x) + }; + let keys: Vec = items.iter().map(|i| hash(&pos(i))).collect(); + // java.util.HashMap: buckets in index order, insertion order within one. + let mut capacity = 16usize; + while keys.len() * 4 > capacity * 3 { + capacity *= 2; + } + let mut order: Vec = (0..keys.len()).collect(); + order.sort_by_key(|&i| { + ((keys[i] ^ ((keys[i] as u32) >> 16) as i32) as u32 as usize) & (capacity - 1) + }); + // Object2ObjectOpenHashMap. + let mix = |h: i32| { + let h = h.wrapping_mul(0x9E37_79B9_u32 as i32) as u32; + (h ^ (h >> 16)) as usize + }; + let put = |table: &mut Vec>, i: usize| { + let mask = table.len() - 1; + let mut at = mix(keys[i]) & mask; + while table[at].is_some() { + at = (at + 1) & mask; + } + table[at] = Some(i); + }; + // Default expected size 16: HashCommon.arraySize(16, 0.75) = 32 slots. + let mut table: Vec> = vec![None; 32]; + for (size, &i) in order.iter().enumerate() { + put(&mut table, i); + // insert: `if (size++ >= maxFill) rehash(arraySize(size + 1, f))`. + let max_fill = (table.len() * 3).div_ceil(4).min(table.len() - 1); + if size >= max_fill { + let grown = ((size + 2) * 4).div_ceil(3).next_power_of_two().max(2); + let old = std::mem::replace(&mut table, vec![None; grown]); + for &j in old.iter().rev().flatten() { + put(&mut table, j); + } + } + } + let iteration: Vec = table.iter().rev().flatten().copied().collect(); + let mut slots: Vec> = items.drain(..).map(Some).collect(); + items.extend(iteration.into_iter().filter_map(|i| slots[i].take())); +} + +/// The update tag of a block entity Horizon keeps only as loaded NBT: +/// SpawnerBlockEntity.getUpdateTag is its saved fields without +/// SpawnPotentials (saveCustomOnly, so no id or position). +fn saved_update_tag(chunk: &Chunk, pos: (i32, i32, i32)) -> Option { + let saved = chunk.saved.as_ref()?; + let tag = saved.block_entities.iter().find(|t| { + let Tag::Compound(c) = t else { return false }; + let at = |k: &str| match c.get(&k.into()) { + Some(Tag::Int(v)) => Some(*v), + _ => None, + }; + (at("x"), at("y"), at("z")) == (Some(pos.0), Some(pos.1), Some(pos.2)) + })?; + if crate::block_entity_nbt::id(tag).as_deref() != Some("minecraft:mob_spawner") { + return None; + } + let Tag::Compound(mut c) = crate::block_entity_nbt::canonical(tag)? else { + return None; + }; + for key in [ + "id", + "x", + "y", + "z", + "keepPacked", + "SpawnPotentials", + "components", + ] { + c.remove(&key.into()); + } + Some(Tag::Compound(c)) +} + /// The update tag of a block entity (getUpdateTag) for what the client shows. pub fn entity_tag(entity: &BlockEntity) -> Option { match entity { @@ -251,8 +385,12 @@ pub fn entity_tag(entity: &BlockEntity) -> Option { } } -/// Light sections run from one below the world to one above it. -fn light_data(chunk: &Chunk) -> LightData { +/// Light sections run from one below the world to one above it. A layer +/// exists where vanilla's light storage keeps one: within one section of a +/// non-air section of this column or a neighbouring one (`around`, bit i for +/// section i; LayerLightSectionStorage.updateSectionStatus). An existing +/// layer is sent, or marked empty when all dark; the others are left out. +fn light_data(chunk: &Chunk, around: u64) -> LightData { let mut data = LightData { sky_mask: Vec::new(), block_mask: Vec::new(), @@ -261,14 +399,24 @@ fn light_data(chunk: &Chunk) -> LightData { sky: Vec::new(), block: Vec::new(), }; - // The section below the world has no sky light; the one above is full. - set_bit(&mut data.empty_sky_mask, 0); - set_bit(&mut data.empty_block_mask, 0); - for (i, s) in chunk.sections.iter().enumerate() { - let bit = i + 1; + // Light section b holds chunk section b - 1. + let exists = |b: usize| { + (b.saturating_sub(2)..=b.min(SECTIONS - 1)).any(|s| s + 2 >= b && around >> s & 1 != 0) + }; + let full_sky = Light::Uniform(0xF0); + for b in 0..SECTIONS + 2 { + if !exists(b) { + continue; + } + let light = match b { + // Below the world nothing is lit; above it the sky is full. + 0 => &Light::Uniform(0), + _ if b == SECTIONS + 1 => &full_sky, + _ => &chunk.sections[b - 1].light, + }; let (mut sky, mut block) = (vec![0u8; 2048], vec![0u8; 2048]); let (mut sky_any, mut block_any) = (false, false); - match &s.light { + match light { Light::Uniform(v) => { let (sk, bl) = (v >> 4, v & 15); sky_any = sk != 0; @@ -293,21 +441,18 @@ fn light_data(chunk: &Chunk) -> LightData { } } if sky_any { - set_bit(&mut data.sky_mask, bit); + set_bit(&mut data.sky_mask, b); data.sky.push(sky); } else { - set_bit(&mut data.empty_sky_mask, bit); + set_bit(&mut data.empty_sky_mask, b); } if block_any { - set_bit(&mut data.block_mask, bit); + set_bit(&mut data.block_mask, b); data.block.push(block); } else { - set_bit(&mut data.empty_block_mask, bit); + set_bit(&mut data.empty_block_mask, b); } } - set_bit(&mut data.sky_mask, SECTIONS + 1); - data.sky.push(vec![0xFF; 2048]); - set_bit(&mut data.empty_block_mask, SECTIONS + 1); data } @@ -335,7 +480,9 @@ mod tests { #[test] fn light_masks_cover_the_world_plus_borders() { let chunk = Chunk::empty(0, 0); - let data = light_data(&chunk); + // A column with nothing around it has no light layers at all. + assert!(light_data(&chunk, 0).sky.is_empty()); + let data = light_data(&chunk, u64::MAX); // Default sections are full sky light and no block light. assert_eq!(data.sky.len(), SECTIONS + 1); assert_eq!(data.block.len(), 0); diff --git a/crates/horizon-client/src/lan/conn.rs b/crates/horizon-client/src/lan/conn.rs index 6cd41d6d6..f67c647fd 100644 --- a/crates/horizon-client/src/lan/conn.rs +++ b/crates/horizon-client/src/lan/conn.rs @@ -35,16 +35,88 @@ const KEEP_ALIVE_EVERY: Duration = Duration::from_secs(15); const KEEP_ALIVE_TIMEOUT: Duration = Duration::from_secs(30); /// Packets queued for one client beyond this many bytes disconnect it. const MAX_PENDING: usize = 96 << 20; -/// Connection.setupCompression threshold of the integrated server. -const COMPRESSION_THRESHOLD: i32 = 256; - /// Vanilla's server-side disconnect during login is a JSON component. -fn login_disconnect(c: &mut Connection, key: &str) { - let json = format!("{{\"translate\":\"{key}\"}}"); +fn login_disconnect(c: &mut Connection, reason: &horizon_world::nbt::Tag) { + let json = component_json(reason); let _ = c.send(&login::Clientbound::Disconnect(login::Disconnect { reason_json: json }).encode()); } +/// A component as ComponentSerialization.CODEC writes it with JsonOps (and +/// Gson without HTML escaping): plain text collapses to a string, the +/// content fields come first, then `extra`, then the style. +pub fn component_json(tag: &horizon_world::nbt::Tag) -> String { + component_value(tag).compact() +} +fn component_value(tag: &horizon_world::nbt::Tag) -> crate::server::json::Json { + use crate::server::json::Json; + use horizon_world::nbt::Tag; + const ORDER: &[&str] = &[ + "type", + "text", + "translate", + "fallback", + "with", + "score", + "selector", + "separator", + "keybind", + "nbt", + "interpret", + "block", + "entity", + "storage", + "source", + "object", + "sprite", + "atlas", + "player", + "hat", + "extra", + "color", + "shadow_color", + "bold", + "italic", + "underlined", + "strikethrough", + "obfuscated", + "click_event", + "hover_event", + "insertion", + "font", + ]; + match tag { + Tag::String(s) => Json::String(s.to_string_lossy()), + Tag::List(items) => Json::Array(items.iter().map(component_value).collect()), + Tag::Compound(map) => { + if map.len() == 1 + && let Some(Tag::String(t)) = map.get(&horizon_world::nbt::JavaString::from("text")) + { + return Json::String(t.to_string_lossy()); + } + let mut keys: Vec = map.keys().map(|k| k.to_string_lossy()).collect(); + keys.sort_by_key(|k| ORDER.iter().position(|o| o == k).unwrap_or(ORDER.len())); + Json::Object( + keys.into_iter() + .map(|k| { + let v = &map[&horizon_world::nbt::JavaString::from(k.as_str())]; + let value = match v { + Tag::Byte(b) => Json::Bool(*b != 0), + Tag::Int(i) => Json::Number(i.to_string()), + other => component_value(other), + }; + (k, value) + }) + .collect(), + ) + } + Tag::Byte(b) => Json::Bool(*b != 0), + Tag::Int(i) => Json::Number(i.to_string()), + Tag::Long(i) => Json::Number(i.to_string()), + _ => Json::Null, + } +} + /// StringUtil.isValidPlayerName. pub fn valid_name(name: &str) -> bool { (1..=16).contains(&name.len()) && name.chars().all(|c| c as u32 > 32 && (c as u32) < 127) @@ -63,8 +135,88 @@ impl PlayerSender { } } -pub fn serve(stream: TcpStream, shared: Arc) -> Result<(), String> { +/// LegacyQueryHandler: the pre-1.7 server list ping. What the first read +/// holds decides the reply (1.3 and older: no more bytes; 1.4 and 1.5: 0x01; +/// 1.6: 0x01 then the MC|PingHost payload); anything else gets no reply. +fn serve_legacy(mut stream: TcpStream, shared: &Shared) -> Result<(), String> { + use std::io::{Read, Write}; + let _ = stream.set_read_timeout(Some(Duration::from_millis(100))); + let mut data = Vec::new(); + let mut chunk = [0u8; 1024]; + // The bytes of the first read: what arrived together. + if let Ok(n) = stream.read(&mut chunk) { + data.extend_from_slice(&chunk[..n]); + } + let in_string = |d: &[u8], at: &mut usize| -> Option { + let len = usize::from(u16::from_be_bytes([*d.get(*at)?, *d.get(*at + 1)?])); + let bytes = d.get(*at + 2..*at + 2 + len * 2)?; + *at += 2 + len * 2; + Some(String::from_utf16_lossy( + &bytes + .chunks(2) + .map(|c| u16::from_be_bytes([c[0], c[1]])) + .collect::>(), + )) + }; + let motd = shared.motd(); + let (online, max) = (shared.online(), shared.max_players()); + let body = if data.len() == 1 { + format!("{motd}\u{a7}{online}\u{a7}{max}") + } else { + if data[1] != 1 { + return Ok(()); + } + if data.len() > 2 { + // readCustomPayloadPacket. + let mut at = 3; + let valid = data[2] == 0xFA + && in_string(&data, &mut at).as_deref() == Some("MC|PingHost") + && data + .get(at..at + 2) + .map(|b| usize::from(u16::from_be_bytes([b[0], b[1]]))) + == Some(data.len().saturating_sub(at + 2)) + && { + at += 2; + data.get(at).is_some_and(|v| *v >= 73) + } + && { + at += 1; + in_string(&data, &mut at).is_some() + } + && data + .get(at..at + 4) + .is_some_and(|b| i32::from_be_bytes([b[0], b[1], b[2], b[3]]) <= 65535); + if !valid { + return Ok(()); + } + } + format!("\u{a7}1\0127\026.3\0{motd}\0{online}\0{max}") + }; + let units: Vec = body.encode_utf16().collect(); + let mut out = vec![0xFF]; + out.extend_from_slice(&(units.len() as u16).to_be_bytes()); + for u in units { + out.extend_from_slice(&u.to_be_bytes()); + } + stream.write_all(&out).map_err(|e| e.to_string())?; + let _ = stream.shutdown(std::net::Shutdown::Both); + Ok(()) +} + +pub fn serve( + stream: TcpStream, + address: std::net::SocketAddr, + shared: Arc, +) -> Result<(), String> { let e = |e: horizon_protocol::Error| e.to_string(); + // LegacyQueryHandler, ahead of the frame decoder while status is on. + let mut first = [0u8; 1]; + if shared.config.enable_status + && stream.peek(&mut first).is_ok_and(|n| n == 1) + && first[0] == 0xFE + { + return serve_legacy(stream, &shared); + } let mut c = Connection::from_stream(stream).map_err(e)?; c.reader .set_read_timeout(Some(Duration::from_secs(30))) @@ -75,10 +227,20 @@ pub fn serve(stream: TcpStream, shared: Arc) -> Result<(), String> { _ => return Err("expected a handshake".into()), }; match intention.intent { + // ServerHandshakePacketListenerImpl: a server that does not reply + // to status requests closes the connection. + handshake::STATUS if !shared.config.enable_status => Ok(()), handshake::STATUS => serve_status(&mut c, &shared), + handshake::TRANSFER if !shared.config.accepts_transfers => { + login_disconnect( + &mut c, + &translatable("multiplayer.disconnect.transfers_disabled", &[]), + ); + Ok(()) + } handshake::LOGIN | handshake::TRANSFER => { if intention.protocol != PROTOCOL_VERSION { - let key = if intention.protocol < PROTOCOL_VERSION { + let key = if intention.protocol < 754 { "multiplayer.disconnect.outdated_client" } else { "multiplayer.disconnect.incompatible" @@ -93,7 +255,7 @@ pub fn serve(stream: TcpStream, shared: Arc) -> Result<(), String> { ); return Ok(()); } - serve_login(c, &shared) + serve_login(c, address, &shared) } other => Err(format!("unknown intent {other}")), } @@ -101,10 +263,14 @@ pub fn serve(stream: TcpStream, shared: Arc) -> Result<(), String> { fn serve_status(c: &mut Connection, shared: &Shared) -> Result<(), String> { let e = |e: horizon_protocol::Error| e.to_string(); + let mut requested = false; loop { let (id, payload) = c.recv().map_err(e)?; match status::Serverbound::decode(id, &payload).map_err(e)? { + // ServerStatusPacketListenerImpl: a second request disconnects. + status::Serverbound::StatusRequest(_) if requested => return Ok(()), status::Serverbound::StatusRequest(_) => { + requested = true; let json = shared.status_json(); c.send( &status::Clientbound::StatusResponse(status::StatusResponse { json }).encode(), @@ -121,7 +287,11 @@ fn serve_status(c: &mut Connection, shared: &Shared) -> Result<(), String> { } } -fn serve_login(mut c: Connection, shared: &Arc) -> Result<(), String> { +fn serve_login( + mut c: Connection, + address: std::net::SocketAddr, + shared: &Arc, +) -> Result<(), String> { let e = |e: horizon_protocol::Error| e.to_string(); let (id, payload) = c.recv().map_err(e)?; let hello = match login::Serverbound::decode(id, &payload).map_err(e)? { @@ -129,20 +299,23 @@ fn serve_login(mut c: Connection, shared: &Arc) -> Result<(), String> { _ => return Err("expected a login hello".into()), }; if !valid_name(&hello.name) { - login_disconnect(&mut c, "multiplayer.disconnect.invalid_name"); + login_disconnect( + &mut c, + &translatable("multiplayer.disconnect.invalid_name", &[]), + ); return Ok(()); } // Offline mode: the profile is derived from the name (PlayerList.canPlayerLogin // then rejects duplicates and a full server). let uuid = offline_uuid(&hello.name); - if let Some(reason) = shared.try_admit(uuid, &hello.name) { - login_disconnect(&mut c, reason); + if let Some(reason) = shared.try_admit(address, uuid, &hello.name) { + login_disconnect(&mut c, &reason); return Ok(()); } let result = login_and_configure(&mut c, shared, uuid, &hello.name); match result { Ok(Some(parts)) => { - play(c, shared, parts); + play(c, shared, parts, address); Ok(()) } Ok(None) => { @@ -170,14 +343,12 @@ fn login_and_configure( name: &str, ) -> Result, String> { let e = |e: horizon_protocol::Error| e.to_string(); - c.send( - &login::Clientbound::Compression(login::Compression { - threshold: COMPRESSION_THRESHOLD, - }) - .encode(), - ) - .map_err(e)?; - c.set_compression(COMPRESSION_THRESHOLD); + let threshold = shared.config.compression_threshold; + if threshold >= 0 { + c.send(&login::Clientbound::Compression(login::Compression { threshold }).encode()) + .map_err(e)?; + c.set_compression(threshold); + } c.send( &login::Clientbound::LoginFinished(login::LoginFinished { profile: login::GameProfile { @@ -208,7 +379,8 @@ fn login_and_configure( channel: "minecraft:brand".into(), data: { let mut w = horizon_protocol::buf::Writer::new(); - w.string("horizon"); + // MinecraftServer.getServerModName. + w.string("vanilla"); w.data }, }), @@ -266,7 +438,7 @@ fn login_and_configure( })) } -fn play(c: Connection, shared: &Arc, parts: Parts) { +fn play(c: Connection, shared: &Arc, parts: Parts, address: std::net::SocketAddr) { let conn_id = shared.next_id(); let events = shared.events.clone(); let _ = c.reader.set_read_timeout(Some(KEEP_ALIVE_TIMEOUT)); @@ -294,6 +466,7 @@ fn play(c: Connection, shared: &Arc, parts: Parts) { brand: parts.brand, sender, latency, + address, }; let reason = if events.send(NetEvent::Joined(Box::new(joined))).is_ok() { read_loop(&mut reader, conn_id, &events, &alive) @@ -317,6 +490,15 @@ fn read_loop( loop { let (id, payload) = match reader.recv() { Ok(p) => p, + // The client closed the connection: Connection.channelInactive + // without details, multiplayer.disconnect.generic. + Err(e) + if e.0 == "Connection closed by the server" + || e.0.contains("reset by peer") + || e.0.contains("aborted") => + { + return "Disconnected".to_owned(); + } Err(e) => return e.to_string(), }; match play::Serverbound::decode(id, &payload) { diff --git a/crates/horizon-client/src/lan/discovery.rs b/crates/horizon-client/src/lan/discovery.rs index 07eca3481..0f695e6b0 100644 --- a/crates/horizon-client/src/lan/discovery.rs +++ b/crates/horizon-client/src/lan/discovery.rs @@ -10,34 +10,61 @@ use std::{ atomic::{AtomicBool, Ordering}, }, thread::JoinHandle, - time::{Duration, Instant}, + time::Duration, }; pub const GROUP: Ipv4Addr = Ipv4Addr::new(224, 0, 2, 60); pub const PORT: u16 = 4445; /// LanServerPinger sleeps 1500 ms between announcements. const INTERVAL: Duration = Duration::from_millis(1500); -/// LanServerDetection.LanServerList drops entries not refreshed for 5 s. -const EXPIRY: Duration = Duration::from_secs(5); /// LanServerPinger.createPingString. pub fn ping_string(motd: &str, port: u16) -> String { - format!("[MOTD]{motd}[/MOTD][AD]{port}[/AD]") + ping_string_text(motd, &port.to_string()) } -fn between<'a>(s: &'a str, open: &str, close: &str) -> Option<&'a str> { - let start = s.find(open)? + open.len(); - let end = s[start..].find(close)? + start; - Some(&s[start..end]) +/// LanServerPinger.createPingString with the address as text. +pub fn ping_string_text(motd: &str, address: &str) -> String { + format!("[MOTD]{motd}[/MOTD][AD]{address}[/AD]") } -/// LanServerPinger.parseMotd; vanilla shows "missing no" when absent. +/// String.indexOf(needle, from) on a Rust string (byte offsets). +fn index_of(s: &str, needle: &str, from: usize) -> Option { + s.get(from..)?.find(needle).map(|i| i + from) +} +/// LanServerPinger.parseMotd; "missing no" when there is none. pub fn parse_motd(s: &str) -> String { - between(s, "[MOTD]", "[/MOTD]") - .unwrap_or("missing no") - .to_owned() + let Some(start) = s.find("[MOTD]") else { + return "missing no".to_owned(); + }; + match index_of(s, "[/MOTD]", start + "[MOTD]".len()) { + Some(end) => s[start + "[MOTD]".len()..end].to_owned(), + None => "missing no".to_owned(), + } } -/// LanServerPinger.parseAddress: the announced port, if present and valid. -pub fn parse_port(s: &str) -> Option { - between(s, "[AD]", "[/AD]")?.parse().ok().filter(|p| *p > 0) +/// LanServerPinger.parseAddress: the text between [AD] and [/AD] after the +/// only [/MOTD], as announced (vanilla does not check it is a port). +pub fn parse_address(s: &str) -> Option { + let end_motd = s.find("[/MOTD]")?; + if index_of(s, "[/MOTD]", end_motd + "[/MOTD]".len()).is_some() { + return None; + } + let start = index_of(s, "[AD]", end_motd + "[/MOTD]".len())?; + let end = index_of(s, "[/AD]", start + "[AD]".len())?; + Some(s[start + "[AD]".len()..end].to_owned()) +} +/// LanServerDetection.LanServerList.addServer: an announcement from `ip`. +/// A new address joins the list; a known one keeps its first MOTD. True +/// when the list changed. +pub fn add_server(games: &mut Vec, ping: &str, ip: &str) -> bool { + let motd = parse_motd(ping); + let Some(address) = parse_address(ping) else { + return false; + }; + let address = format!("{ip}:{address}"); + if games.iter().any(|g| g.address == address) { + return false; + } + games.push(LanGame { motd, address }); + true } /// Announces one hosted world until dropped. @@ -48,7 +75,6 @@ pub struct Pinger { impl Pinger { pub fn start(motd: String, port: u16) -> io::Result { let socket = UdpSocket::bind(("0.0.0.0", 0))?; - let _ = socket.set_multicast_ttl_v4(4); let stop = Arc::new(AtomicBool::new(false)); let flag = stop.clone(); let message = ping_string(&motd, port); @@ -86,7 +112,7 @@ pub struct LanGame { pub motd: String, pub address: String, } -type Games = Arc>>; +type Games = Arc>>; /// Listens for announcements until the last clone is dropped. #[derive(Clone, Debug)] @@ -118,11 +144,10 @@ impl Scanner { inner: Arc::new(ScanInner { games, stop }), } } - /// The live announcements, oldest first. + /// The worlds announced since the scan started, in order (vanilla's + /// list keeps them while the multiplayer screen is open). pub fn games(&self) -> Vec { - let mut games = self.inner.games.lock().unwrap(); - games.retain(|(_, seen)| seen.elapsed() < EXPIRY); - games.iter().map(|(g, _)| g.clone()).collect() + self.inner.games.lock().unwrap().clone() } } fn bind_listener() -> io::Result { @@ -138,18 +163,7 @@ fn listen(socket: UdpSocket, games: Games, stop: Arc) { continue; }; let text = String::from_utf8_lossy(&buf[..n]); - let Some(port) = parse_port(&text) else { - continue; - }; - let game = LanGame { - motd: parse_motd(&text), - address: format!("{}:{port}", from.ip()), - }; - let mut games = games.lock().unwrap(); - match games.iter_mut().find(|(g, _)| g.address == game.address) { - Some(entry) => *entry = (game, Instant::now()), - None => games.push((game, Instant::now())), - } + add_server(&mut games.lock().unwrap(), &text, &from.ip().to_string()); } } @@ -162,8 +176,17 @@ mod tests { let s = ping_string("Steve - World", 25565); assert_eq!(s, "[MOTD]Steve - World[/MOTD][AD]25565[/AD]"); assert_eq!(parse_motd(&s), "Steve - World"); - assert_eq!(parse_port(&s), Some(25565)); - assert_eq!(parse_port("[AD]0[/AD]"), None); + assert_eq!(parse_address(&s).as_deref(), Some("25565")); + assert_eq!(parse_address("[AD]1[/AD]"), None); + assert_eq!(parse_address("[/MOTD][AD]x[/AD][/MOTD]"), None); assert_eq!(parse_motd("junk"), "missing no"); + let mut games = Vec::new(); + assert!(add_server(&mut games, &s, "10.0.0.2")); + assert!(!add_server( + &mut games, + &ping_string("Other", 25565), + "10.0.0.2" + )); + assert_eq!(games[0].motd, "Steve - World"); } } diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index c32e19d07..d4c929c4c 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -20,6 +20,20 @@ pub const SPECTATOR: i32 = 3; pub struct Guest { pub conn: u32, + /// The client's address (IP bans, `list`). + pub address: std::net::SocketAddr, + /// The hub tick of the player's last action (ServerPlayer.lastActionTime), + /// for player-idle-timeout. + pub last_action_tick: u64, + /// The next ClientboundPlayerChatPacket.globalIndex for this connection. + pub chat_index: i32, + /// The hub tick the player was placed on: chunks go out from the tick + /// after next, behind the first doTick's packets, as vanilla's do. + pub placed_at: u64, + /// The chunk cache centre placeNewPlayer already sent. + pub center_sent: Option<(i32, i32)>, + /// Why the connection closed (the console's "lost connection"). + pub left_reason: String, pub uuid: u128, pub name: String, pub entity_id: i32, @@ -97,6 +111,12 @@ impl Guest { pub fn new(j: Joined, entity_id: i32, position: [f64; 3], view_distance: i32) -> Self { Self { conn: j.conn, + address: j.address, + last_action_tick: 0, + chat_index: 0, + placed_at: 0, + center_sent: None, + left_reason: String::new(), uuid: j.uuid, name: j.name, entity_id, diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index ed9771043..8f5d993d7 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -126,6 +126,54 @@ pub fn yellow_translation(key: &str, args: &[&str]) -> Tag { t } +/// Player.getDisplayName for a player in no team: the name, suggesting +/// `/tell NAME `, showing the player on hover and inserting the name. +pub fn display_name(uuid: u128, name: &str) -> Tag { + let words = [ + (uuid >> 96) as i32, + (uuid >> 64) as i32, + (uuid >> 32) as i32, + uuid as i32, + ]; + Tag::compound([ + ("text", string(name)), + ( + "click_event", + Tag::compound([ + ("action", string("suggest_command")), + ("command", string(&format!("/tell {name} "))), + ]), + ), + ( + "hover_event", + Tag::compound([ + ("action", string("show_entity")), + ("id", string("minecraft:player")), + ("uuid", Tag::IntArray(words.to_vec())), + ("name", string(name)), + ]), + ), + ("insertion", string(name)), + ]) +} + +/// A yellow translation of player names (`multiplayer.player.joined`). +fn yellow_names(key: &str, names: Vec) -> Tag { + Tag::compound([ + ("translate", string(key)), + ("with", Tag::List(names)), + ("color", string("yellow")), + ]) +} + +/// ChunkTrackingView.contains: the chunks a player at (cx, cz) with view +/// distance `r` is sent (isWithinDistance with the neighbours' buffer). +fn tracked(cx: i32, cz: i32, r: i32, x: i32, z: i32) -> bool { + let dx = i64::from(((x - cx).abs() - 2).max(0)); + let dz = i64::from(((z - cz).abs() - 2).max(0)); + dx * dx + dz * dz < i64::from(r) * i64::from(r) +} + impl Hub { pub fn new(lan: Lan, sync: Arc, encoder: ChunkEncoder, config: HubConfig) -> Self { let chat_type = sync @@ -214,19 +262,123 @@ impl Hub { overlay: false, })); } - /// A chat message from `sender` (PlayerChatMessage as a disguised chat). + /// A chat message from the host (a disguised chat: the host has no + /// connection to sign with). pub fn chat(&self, sender: &str, text: &str) { - self.broadcast(&self.chat_packet(sender, text)); - } - fn chat_packet(&self, sender: &str, text: &str) -> C { - C::DisguisedChat(play::DisguisedChat { + self.broadcast(&C::DisguisedChat(play::DisguisedChat { message: literal(text), chat_type: play::ChatTypeBound { chat_type: self.chat_type, name: literal(sender), target: None, }, - }) + })); + } + /// ListPlayersCommand's reply: the players in join order (the host of a + /// LAN world first), as display names or names with ids, joined by + /// gray commas (ComponentUtils.formatList). + pub fn list_reply(&self, host: Option<(u128, &str)>, uuids: bool) -> Tag { + let players: Vec<(u128, &str)> = host + .into_iter() + .chain( + self.guests + .iter() + .filter(|g| g.placed) + .map(|g| (g.uuid, g.name.as_str())), + ) + .collect(); + let format = |(uuid, name): (u128, &str)| { + if uuids { + Tag::compound([ + ("translate", string("commands.list.nameAndId")), + ( + "with", + Tag::List(vec![ + string(name), + string(&horizon_protocol::crypto::uuid_string(uuid)), + ]), + ), + ]) + } else { + display_name(uuid, name) + } + }; + let names = match players.len() { + 0 => string(""), + 1 => format(players[0]), + _ => { + let mut extra = Vec::new(); + for (k, p) in players.iter().enumerate() { + if k > 0 { + extra.push(Tag::compound([ + ("text", string(", ")), + ("color", string("gray")), + ])); + } + extra.push(format(*p)); + } + Tag::compound([("text", string("")), ("extra", Tag::List(extra))]) + } + }; + Tag::compound([ + ("translate", string("commands.list.players")), + ( + "with", + Tag::List(vec![ + Tag::Int(players.len() as i32), + Tag::Int(self.lan.shared.max_players() as i32), + names, + ]), + ), + ]) + } + + /// SayCommand from a source without a player: a disguised chat of type + /// say_command named after the source. + pub fn say(&self, source: &str, text: &str) { + let chat_type = self + .sync + .id("minecraft:chat_type", "minecraft:say_command") + .unwrap_or(self.chat_type); + self.broadcast(&C::DisguisedChat(play::DisguisedChat { + message: literal(text), + chat_type: play::ChatTypeBound { + chat_type, + name: literal(source), + target: None, + }, + })); + } + /// PlayerList.broadcastChatMessage of a guest's unsigned message + /// (PlayerChatMessage.unsigned): a ClientboundPlayerChatPacket to every + /// player, each connection numbering its messages. + pub fn player_chat(&mut self, from: usize, text: &str) { + let (uuid, name) = (self.guests[from].uuid, self.guests[from].name.clone()); + let timestamp = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |d| d.as_millis() as i64); + let bound = play::ChatTypeBound { + chat_type: self.chat_type, + name: display_name(uuid, &name), + target: None, + }; + for g in self.guests.iter_mut().filter(|g| g.placed) { + let packet = C::PlayerChat(play::PlayerChat { + global_index: g.chat_index, + sender: uuid, + index: 0, + signature: None, + content: text.to_owned(), + timestamp, + salt: 0, + last_seen: Vec::new(), + unsigned_content: None, + filter: (0, Vec::new()), + chat_type: bound.clone(), + }); + g.chat_index += 1; + g.send(&packet); + } } /// Disconnect a guest with a reason (ServerGamePacketListenerImpl.disconnect). @@ -275,6 +427,7 @@ impl Hub { *ctx.next_entity += 1; let conn = j.conn; let mut guest = Guest::new(j, entity_id, ctx.spawn, self.config.view_distance); + guest.last_action_tick = self.ticks; guest.game_mode = self.config.default_game_mode; guest.permission = self.default_permission(guest.uuid); guest.joined_tick = self.ticks; @@ -356,7 +509,10 @@ impl Hub { self.broadcast_except( conn, &C::SystemChat(play::SystemChat { - content: yellow_translation("multiplayer.player.joined", &[&name]), + content: yellow_names( + "multiplayer.player.joined", + vec![display_name(self.guests[i].uuid, &name)], + ), overlay: false, }), ); @@ -365,7 +521,7 @@ impl Hub { let g = &self.guests[i]; g.send(&C::ServerData(play::ServerData { motd: literal(&self.lan.shared.motd()), - icon: None, + icon: self.lan.shared.config.favicon.clone(), })); // The player list: everyone already here, then the newcomer to all. let mut entries = Vec::new(); @@ -390,6 +546,15 @@ impl Hub { }; self.broadcast(&C::PlayerInfoUpdate(newcomer)); self.send_level_info(i, ctx); + // ServerLevel.addNewPlayer: the chunk tracking starts at the player. + let g = &mut self.guests[i]; + let (cx, cz) = g.chunk(); + g.send(&C::SetChunkCacheCenter(play::SetChunkCacheCenter { + x: cx, + z: cz, + })); + g.center_sent = Some((cx, cz)); + g.placed_at = self.ticks; // ServerPlayer.sendPostEffects and initInventoryMenu. self.guests[i].send(&raw(ids::POST_EFFECTS, vec![0])); self.send_inventory(i); @@ -409,9 +574,7 @@ impl Hub { /// The permissions changed: tell everyone their level and commands again. pub fn resend_commands(&mut self) { for i in 0..self.guests.len() { - if !self.config.dedicated { - self.guests[i].permission = self.default_permission(self.guests[i].uuid); - } + self.guests[i].permission = self.default_permission(self.guests[i].uuid); if self.guests[i].placed { self.send_permission_level(i); } @@ -586,6 +749,20 @@ impl Hub { g.inbox.push_back(*packet); } } + NetEvent::Duplicate(uuid) => { + let conns: Vec = self + .guests + .iter() + .filter(|g| g.uuid == uuid) + .map(|g| g.conn) + .collect(); + for conn in conns { + self.kick( + conn, + translatable("multiplayer.disconnect.duplicate_login", &[]), + ); + } + } } } for i in 0..self.guests.len() { @@ -639,7 +816,8 @@ impl Hub { } fn remove_guest(&mut self, i: usize, reason: &str) -> Guest { - let g = self.guests.remove(i); + let mut g = self.guests.remove(i); + g.left_reason = reason.to_owned(); if std::env::var_os("HORIZON_NET_DEBUG").is_some() { eprintln!("[lan] {} left: {reason}", g.name); } @@ -652,7 +830,10 @@ impl Hub { self.broadcast(&C::RemoveEntities(play::RemoveEntities { ids: vec![g.entity_id], })); - self.system_message(yellow_translation("multiplayer.player.left", &[&g.name])); + self.system_message(yellow_names( + "multiplayer.player.left", + vec![display_name(g.uuid, &g.name)], + )); g } @@ -729,27 +910,31 @@ impl Hub { let g = &mut self.guests[i]; if g.center != Some((cx, cz)) { g.center = Some((cx, cz)); - g.send(&C::SetChunkCacheCenter(play::SetChunkCacheCenter { - x: cx, - z: cz, - })); + if g.center_sent.take() != Some((cx, cz)) { + g.send(&C::SetChunkCacheCenter(play::SetChunkCacheCenter { + x: cx, + z: cz, + })); + } // Chunks that left the view. let forget: Vec<_> = g .sent_chunks .iter() .copied() - .filter(|&(x, z)| (x - cx).abs().max((z - cz).abs()) > r) + .filter(|&(x, z)| !tracked(cx, cz, r, x, z)) .collect(); for (x, z) in forget { g.sent_chunks.remove(&(x, z)); g.send(&C::ForgetLevelChunk(play::ForgetLevelChunk { pos: [x, z] })); } - g.chunk_queue - .retain(|&(x, z)| (x - cx).abs().max((z - cz).abs()) <= r); - for dz in -r..=r { - for dx in -r..=r { + 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 !g.sent_chunks.contains(&key) && !g.chunk_queue.contains(&key) { + if tracked(cx, cz, r, key.0, key.1) + && !g.sent_chunks.contains(&key) + && !g.chunk_queue.contains(&key) + { g.chunk_queue.push(key); } } @@ -758,7 +943,7 @@ impl Hub { .sort_by_key(|&(x, z)| i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2)); } // Send ready chunks, closest first, in batches. - if g.chunk_queue.is_empty() || g.unacknowledged >= 10 { + if g.chunk_queue.is_empty() || g.unacknowledged >= 10 || self.ticks <= g.placed_at + 1 { return; } g.chunk_credit = (g.chunk_credit + g.chunks_per_tick).min(g.chunks_per_tick.max(1.0) * 2.0); @@ -779,7 +964,7 @@ impl Hub { continue; } packets.push(C::LevelChunkWithLight( - self.encoder.encode(chunk, &ctx.world.block_entities), + self.encoder.encode_in(chunk, ctx.world), )); taken.push(idx); } @@ -806,7 +991,7 @@ impl Hub { let Some(chunk) = world.chunks.get(&(x, z)) else { return; }; - let packet = C::LevelChunkWithLight(self.encoder.encode(chunk, &world.block_entities)); + let packet = C::LevelChunkWithLight(self.encoder.encode_in(chunk, world)); let body = packet.encode(); for g in self .guests @@ -1092,6 +1277,15 @@ impl Hub { /// ServerPlayer.doTick: health, food, experience and the inventory menu. fn sync_player_state(&mut self, i: usize) { + // ServerEntity.sendDirtyEntityData on the player's first tick: the + // health its creation set is still dirty, and goes to the player too. + if self.guests[i].sent_health.is_none() { + let g = &self.guests[i]; + g.send(&C::SetEntityData(play::SetEntityData { + id: g.entity_id, + values: vec![(9, serializer::FLOAT, Value::Float(g.health))], + })); + } let health = { let g = &self.guests[i]; (g.health, g.food, g.saturation) @@ -1142,6 +1336,13 @@ impl Hub { // ---- incoming ------------------------------------------------------ + /// The squared movement past 1.0E-5F (handlePlayerKnownMovement). + fn moved_far(&self, i: usize, to: [f64; 3]) -> bool { + let from = self.guests[i].position; + let d: f64 = (0..3).map(|k| (to[k] - from[k]).powi(2)).sum(); + d > f64::from(1.0E-5_f32) + } + fn process_inbox(&mut self, i: usize, ctx: &mut TickCtx, events: &mut Vec) { let mut budget = 400; while budget > 0 { @@ -1150,6 +1351,26 @@ impl Hub { }; budget -= 1; let conn = self.guests[i].conn; + // ServerPlayer.resetLastActionTime: every handler of a player's + // own action resets it, movement only when it moved + // (handlePlayerKnownMovement), PlayerInput once loaded. + let acted = match &packet { + S::AcceptTeleportation(_) + | S::MovePlayerStatus(_) + | S::MovePlayerRot(_) + | S::PlayerLoaded(_) + | S::ChunkBatchReceived(_) + | S::ClientInformation(_) + | S::ClientTickEnd(_) + | S::KeepAlive(_) => false, + S::MovePlayerPos(p) => self.moved_far(i, [p.x, p.y, p.z]), + S::MovePlayerPosRot(p) => self.moved_far(i, [p.x, p.y, p.z]), + S::PlayerInput(_) => self.guests[i].loaded, + _ => true, + }; + if acted { + self.guests[i].last_action_tick = self.ticks; + } match packet { S::AcceptTeleportation(p) => { let g = &mut self.guests[i]; @@ -1202,7 +1423,7 @@ impl Hub { let text: String = c.message.chars().filter(|c| !c.is_control()).collect(); if !text.is_empty() { let name = self.guests[i].name.clone(); - self.chat(&name, &text); + self.player_chat(i, &text); events.push(HostEvent::Chat(format!("<{name}> {text}"))); } } diff --git a/crates/horizon-client/src/lan/mod.rs b/crates/horizon-client/src/lan/mod.rs index cd0214c5e..748e1d26b 100644 --- a/crates/horizon-client/src/lan/mod.rs +++ b/crates/horizon-client/src/lan/mod.rs @@ -20,14 +20,17 @@ pub mod tracking; pub use conn::PlayerSender; use horizon_protocol::packets::play; -use horizon_protocol::{GAME_VERSION, PROTOCOL_VERSION, packets::configuration::ClientInformation}; +use horizon_protocol::{ + GAME_VERSION, PROTOCOL_VERSION, crypto::uuid_string, packets::configuration::ClientInformation, + text::translatable, +}; use std::{ collections::HashSet, io, net::{SocketAddr, TcpListener, TcpStream}, sync::{ Arc, Mutex, - atomic::{AtomicBool, AtomicI32, AtomicU32, Ordering}, + atomic::{AtomicBool, AtomicI32, AtomicU32, AtomicUsize, Ordering}, mpsc::{Receiver, Sender, channel}, }, thread::JoinHandle, @@ -44,6 +47,9 @@ pub enum NetEvent { Packet(u32, Box), /// The connection ended (reason for the log). Left(u32, String), + /// PlayerList.disconnectAllPlayersWithProfile: a new login of this + /// profile replaces the player already in the world. + Duplicate(u128), } pub struct Joined { pub conn: u32, @@ -54,6 +60,7 @@ pub struct Joined { pub sender: PlayerSender, /// Smoothed keep-alive round trip in milliseconds. pub latency: Arc, + pub address: SocketAddr, } /// Queued for a connection's writer thread. pub enum Out { @@ -64,54 +71,232 @@ pub enum Out { Close, } +/// PlayerList.canPlayerLogin for a dedicated server: the disconnect reason +/// (bans, whitelist) for a profile connecting from an address, if refused. +pub type LoginGate = + Arc Option + Send + Sync>; + +/// How the server side of the network behaves: an integrated server's LAN +/// uses the defaults, a dedicated server takes them from server.properties. +#[derive(Clone)] +pub struct NetConfig { + /// server-ip (all interfaces when absent). + pub bind: Option, + /// 0 picks a free port. + pub port: u16, + pub motd: String, + pub max_players: usize, + /// network-compression-threshold; negative disables compression. + pub compression_threshold: i32, + /// enable-status: answer status pings. + pub enable_status: bool, + pub hide_online_players: bool, + /// server-icon.png (64x64 PNG bytes). + pub favicon: Option>, + /// MinecraftServer.enforceSecureProfile. + pub enforces_secure_chat: bool, + pub accepts_transfers: bool, + /// LanServerPinger: announce the world on the local network. + pub announce: bool, + /// Ops bypass a full server (ServerOpList.canBypassPlayerLimit). + pub bypasses_limit: Option bool + Send + Sync>>, + pub gate: Option, +} +impl NetConfig { + /// IntegratedServer.publishServer. + pub fn lan(port: u16, motd: String, max_players: usize) -> Self { + Self { + bind: None, + port, + motd, + max_players, + compression_threshold: 256, + enable_status: true, + hide_online_players: false, + favicon: None, + enforces_secure_chat: false, + accepts_transfers: false, + announce: true, + bypasses_limit: None, + gate: None, + } + } +} + /// State shared with the connection threads. pub struct Shared { pub sync: Arc, pub events: Sender, + pub config: NetConfig, motd: Mutex, - online: Mutex>, - max_players: usize, + max_players: AtomicUsize, + /// PlayerList.players in join order: profile id and name. + online: Mutex>, + /// Profiles held by the host (the LAN host's own name). + reserved: Mutex>, next: AtomicU32, stop: AtomicBool, + random: Mutex, } impl Shared { pub fn next_id(&self) -> u32 { self.next.fetch_add(1, Ordering::Relaxed) } - /// PlayerList.canPlayerLogin: duplicates and a full server are refused. - pub fn try_admit(&self, uuid: u128, _name: &str) -> Option<&'static str> { - let mut online = self.online.lock().unwrap(); - if online.contains(&uuid) { - return Some("multiplayer.disconnect.duplicate_login"); + /// PlayerList.canPlayerLogin then disconnectAllPlayersWithProfile: the + /// refusal reason, or None once the profile holds a place. A profile + /// already playing is kicked and its place awaited. + pub fn try_admit( + &self, + address: SocketAddr, + uuid: u128, + name: &str, + ) -> Option { + if let Some(gate) = &self.config.gate + && let Some(reason) = gate(address, uuid, name) + { + return Some(reason); } - if online.len() >= self.max_players { - return Some("multiplayer.disconnect.server_full"); + if self.reserved.lock().unwrap().contains(&uuid) { + return Some(translatable("multiplayer.disconnect.duplicate_login", &[])); } - online.insert(uuid); + { + let online = self.online.lock().unwrap(); + let bypass = self.config.bypasses_limit.as_ref().is_some_and(|b| b(uuid)); + let taken = online.len() + self.reserved.lock().unwrap().len(); + if taken >= self.max_players() && !bypass { + return Some(translatable("multiplayer.disconnect.server_full", &[])); + } + } + if self.is_online(uuid) { + let _ = self.events.send(NetEvent::Duplicate(uuid)); + let deadline = std::time::Instant::now() + Duration::from_secs(30); + while self.is_online(uuid) { + if std::time::Instant::now() > deadline { + return Some(translatable("multiplayer.disconnect.slow_login", &[])); + } + std::thread::sleep(Duration::from_millis(10)); + } + } + self.online.lock().unwrap().push((uuid, name.to_owned())); None } + fn is_online(&self, uuid: u128) -> bool { + self.online.lock().unwrap().iter().any(|(u, _)| *u == uuid) + } pub fn motd(&self) -> String { self.motd.lock().unwrap().clone() } + pub fn max_players(&self) -> usize { + self.max_players.load(Ordering::Relaxed) + } + pub fn set_max_players(&self, n: usize) { + self.max_players.store(n, Ordering::Relaxed); + } pub fn release(&self, uuid: u128) { - self.online.lock().unwrap().remove(&uuid); + self.online.lock().unwrap().retain(|(u, _)| *u != uuid); } pub fn online(&self) -> usize { - self.online.lock().unwrap().len() + self.online.lock().unwrap().len() + self.reserved.lock().unwrap().len() } - /// The status response (ServerStatus): version, players and the MOTD. + fn next_random(&self, bound: usize) -> usize { + let mut r = self.random.lock().unwrap(); + *r = r + .wrapping_mul(6_364_136_223_846_793_005) + .wrapping_add(1_442_695_040_888_963_407); + ((*r >> 33) as usize) % bound.max(1) + } + /// The status response (MinecraftServer.buildServerStatus encoded with + /// ServerStatus.CODEC): description, players with up to 12 shuffled + /// names, version, favicon and enforcesSecureChat when true. pub fn status_json(&self) -> String { + use crate::server::json::Json; let motd = self.motd.lock().unwrap().clone(); - serde_json::json!({ - "version": { "name": GAME_VERSION, "protocol": PROTOCOL_VERSION }, - "players": { "max": self.max_players, "online": self.online() }, - "description": { "text": motd }, - "enforcesSecureChat": false, - }) - .to_string() + let online: Vec<(u128, String)> = self.online.lock().unwrap().clone(); + let count = self.online(); + let mut players = vec![ + ( + "max".to_owned(), + Json::Number(self.max_players().to_string()), + ), + ("online".to_owned(), Json::Number(count.to_string())), + ]; + if !self.config.hide_online_players && !online.is_empty() { + let size = online.len().min(12); + let offset = self.next_random(online.len() - size + 1); + let mut sample: Vec<(u128, String)> = online[offset..offset + size].to_vec(); + for i in (1..sample.len()).rev() { + let j = self.next_random(i + 1); + sample.swap(i, j); + } + players.push(( + "sample".to_owned(), + Json::Array( + sample + .into_iter() + .map(|(u, n)| { + Json::Object(vec![ + ("id".to_owned(), Json::String(uuid_string(u))), + ("name".to_owned(), Json::String(n)), + ]) + }) + .collect(), + ), + )); + } + let mut status = vec![ + ("description".to_owned(), Json::String(motd)), + ("players".to_owned(), Json::Object(players)), + ( + "version".to_owned(), + Json::Object(vec![ + ("name".to_owned(), Json::String(GAME_VERSION.to_owned())), + ( + "protocol".to_owned(), + Json::Number(PROTOCOL_VERSION.to_string()), + ), + ]), + ), + ]; + if let Some(icon) = &self.config.favicon { + status.push(( + "favicon".to_owned(), + Json::String(format!("data:image/png;base64,{}", base64(icon))), + )); + } + if self.config.enforces_secure_chat { + status.push(("enforcesSecureChat".to_owned(), Json::Bool(true))); + } + Json::Object(status).compact() } } +/// java.util.Base64.getEncoder (padded, no line breaks). +pub fn base64(data: &[u8]) -> String { + const A: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + let mut out = String::with_capacity(data.len().div_ceil(3) * 4); + for chunk in data.chunks(3) { + let b = [ + chunk[0], + *chunk.get(1).unwrap_or(&0), + *chunk.get(2).unwrap_or(&0), + ]; + let n = u32::from(b[0]) << 16 | u32::from(b[1]) << 8 | u32::from(b[2]); + out.push(char::from(A[(n >> 18) as usize & 63])); + out.push(char::from(A[(n >> 12) as usize & 63])); + out.push(if chunk.len() > 1 { + char::from(A[(n >> 6) as usize & 63]) + } else { + '=' + }); + out.push(if chunk.len() > 2 { + char::from(A[n as usize & 63]) + } else { + '=' + }); + } + out +} + /// A running LAN server. pub struct Lan { pub shared: Arc, @@ -129,19 +314,33 @@ impl Lan { motd: String, max_players: usize, ) -> io::Result { - let listener = TcpListener::bind(("0.0.0.0", port))?; + Self::start_with(sync, NetConfig::lan(port, motd, max_players)) + } + /// ServerConnectionListener.startTcpServerListener with a configuration. + pub fn start_with(sync: Arc, config: NetConfig) -> io::Result { + let bind = config + .bind + .unwrap_or(std::net::IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED)); + let listener = TcpListener::bind((bind, config.port))?; listener.set_nonblocking(true)?; let port = listener.local_addr()?.port(); - let _pinger = discovery::Pinger::start(motd.clone(), port).ok(); + let _pinger = if config.announce { + discovery::Pinger::start(config.motd.clone(), port).ok() + } else { + None + }; let (tx, events) = channel(); let shared = Arc::new(Shared { sync, events: tx, - motd: Mutex::new(motd), - online: Mutex::new(HashSet::new()), - max_players, + motd: Mutex::new(config.motd.clone()), + max_players: AtomicUsize::new(config.max_players), + online: Mutex::new(Vec::new()), + reserved: Mutex::new(HashSet::new()), next: AtomicU32::new(1), stop: AtomicBool::new(false), + random: Mutex::new(crate::server::java_properties::now_millis() as u64 | 1), + config, }); let accept_shared = shared.clone(); let listener = std::thread::Builder::new() @@ -161,7 +360,7 @@ impl Lan { } /// Reserve a uuid for the host so a guest cannot take the name. pub fn reserve(&self, uuid: u128) { - self.shared.online.lock().unwrap().insert(uuid); + self.shared.reserved.lock().unwrap().insert(uuid); } pub fn set_motd(&self, motd: &str) { *self.shared.motd.lock().unwrap() = motd.to_owned(); @@ -198,7 +397,7 @@ fn accept_loop(listener: TcpListener, shared: Arc) { } fn handle(stream: TcpStream, addr: SocketAddr, shared: Arc) { let _ = stream.set_nonblocking(false); - if let Err(e) = conn::serve(stream, shared) + if let Err(e) = conn::serve(stream, addr, shared) && std::env::var_os("HORIZON_NET_DEBUG").is_some() { eprintln!("[lan] {addr}: {e}"); diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 1178deb6b..5787ea22a 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -25,6 +25,7 @@ pub mod drops; pub mod enderman; pub mod equine; pub mod farm; +pub mod finalize; pub mod gear; pub mod goals; pub mod golem; @@ -205,6 +206,8 @@ pub struct MobType { pub armor_toughness: f32, pub knockback_resistance: f64, pub jump_strength: f64, + /// Every default attribute value of the type (`entity-types.json`). + pub attributes: Arc>, pub gravity: f64, pub safe_fall_distance: f64, pub fall_damage_multiplier: f64, @@ -238,12 +241,35 @@ impl MobType { self.layers.get(key) } } +/// Each type's default attribute values, parsed exactly (serde_json's +/// default float parsing can miss the nearest double by one ulp). +fn exact_attributes(bytes: &[u8]) -> HashMap> { + use serde_json::value::RawValue; + type Raw = HashMap>; + let Ok(types) = serde_json::from_slice::(bytes) else { + return HashMap::new(); + }; + types + .into_iter() + .filter_map(|(id, t)| { + let t: Raw = serde_json::from_str(t.get()).ok()?; + let attrs: Raw = serde_json::from_str(t.get("attributes")?.get()).ok()?; + let values = attrs + .into_iter() + .filter_map(|(k, v)| v.get().trim().parse::().ok().map(|v| (k, v))) + .collect(); + Some((id, values)) + }) + .collect() +} pub struct MobTypes { pub types: HashMap, } impl MobTypes { pub fn load(root: &Path) -> crate::Result { - let types: Value = serde_json::from_slice(&fs::read(root.join("entity-types.json"))?)?; + let bytes = fs::read(root.join("entity-types.json"))?; + let types: Value = serde_json::from_slice(&bytes)?; + let exact = exact_attributes(&bytes); let models: Value = serde_json::from_slice(&fs::read(root.join("entity-models.json"))?)?; let item_tags = crate::items::load_tags(root, "item").unwrap_or_default(); sulfur_cube::load_archetype_items(&item_tags); @@ -268,7 +294,8 @@ impl MobTypes { let Some(model) = models.get(&model_key) else { continue; }; - let attr = |k: &str| t["attributes"][format!("minecraft:{k}")].as_f64(); + let defaults = exact.get(&id).cloned().unwrap_or_default(); + let attr = |k: &str| defaults.get(&format!("minecraft:{k}")).copied(); let mut layers = HashMap::new(); if let Some(all) = models.as_object() { for prefix in def.layer_prefixes() { @@ -302,6 +329,7 @@ impl MobTypes { armor_toughness: attr("armor_toughness").unwrap_or(0.0) as f32, knockback_resistance: attr("knockback_resistance").unwrap_or(0.0), jump_strength: attr("jump_strength").unwrap_or(0.42), + attributes: Arc::new(defaults.clone()), gravity: attr("gravity").unwrap_or(0.08), safe_fall_distance: attr("safe_fall_distance").unwrap_or(3.0), fall_damage_multiplier: attr("fall_damage_multiplier").unwrap_or(1.0), @@ -461,6 +489,9 @@ pub struct Attrs { pub attack_damage: f32, pub max_health: f32, pub follow_range: f64, + pub knockback_resistance: f64, + pub spawn_reinforcements: f64, + pub jump_strength: f64, } /// Species-specific persistent state (variants, colours, taming, ageing...). #[derive(Clone, Debug, Default)] @@ -561,6 +592,24 @@ pub struct Extra { /// `Mob.dropChances` by `EquipmentSlot` (`drops::SLOTS` order) when set /// explicitly; None uses the defaults of `drops::drop_chance`. pub drop_chances: Option>, + /// `Mob.leftHanded`. + pub left_handed: bool, + /// The sound variant: index in the species' `*_sound_variant` registry. + pub sound_variant: u8, + /// `Zombie.canBreakDoors`. + pub can_break_doors: bool, + /// A goat's `IsScreamingGoat` and the horns it lost (left, right). + pub screaming: bool, + pub lost_horns: [bool; 2], + /// Attribute bases and permanent modifiers set by `finalizeSpawn`. + pub attributes: Option>, + /// Brain memories `finalizeSpawn` sets: (memory, value, ticks to expiry). + pub memories: Vec<(&'static str, i64, i64)>, + /// A camel's `LAST_POSE_CHANGE_TICK`. + pub pose_change_tick: i64, + /// The school leader a fish follows, and a tropical fish that swims alone. + pub leader: Option, + pub lone: bool, /// A breeze's or creaking's brain (`monster_brain`). pub brain: Option>, } @@ -798,10 +847,14 @@ pub enum MobEvent { mob: u32, other: u32, }, - /// Two villagers make a baby. + /// Two villagers make a baby (`VillagerMakeLove.tryToGiveBirth`): the + /// parent has eaten and, with a bed for the child, rests; the partner + /// eats, and rests too if `child` holds the child's bed and type + /// (`None`: the biome's). VillagerBirth { parent: u32, partner: u32, + child: Option<([i32; 3], Option)>, }, /// A villager asks whether enough neighbours want an iron golem. GolemCheck { @@ -936,11 +989,65 @@ pub struct Seen { /// `render::PROFESSIONS`), a llama's strength. And a villager's experience. pub profession: u8, pub xp: i32, + /// What other villagers read of a villager's state and brain. + pub villager: Option>, /// `MAX_HEALTH`, `JUMP_STRENGTH` and `MOVEMENT_SPEED` base values (a /// horse's breeding stock). pub stats: [f64; 3], } impl Mob { + /// What other mobs see of this one this tick (`player`: the player's + /// UUID, which villagers' reputation is keyed by). + pub fn seen(&self, player: Option<[i32; 4]>) -> Seen { + let m = self; + let villager = m.species() == Species::Villager; + Seen { + id: m.id, + species: m.species(), + position: m.position, + bounds: m.bounds(), + eye_y: m.eye_y(), + alive: m.alive(), + baby: m.is_baby(), + // A villager is "in love" when it is willing to breed (`canBreed`). + in_love: if villager { + villager::can_breed(m) + } else { + m.extra.in_love > 0 + }, + tame: m.extra.tame, + hostile: m.hostile(), + target: m.ai.target, + health: m.health, + variant: m.extra.variant, + color: m.extra.color, + job: villager.then_some(m.extra.home).flatten(), + bed: villager.then_some(m.extra.bed).flatten(), + yaw: m.yaw, + velocity: m.velocity, + leash: m.extra.leash, + caravan: caravan::head_of(m), + delay: m.extra.ints[5], + hurt_by: m.ai.last_hurt_by.map(|w| (w, m.ai.last_hurt_by_time)), + active_raid: raider::has_active_raid(m), + player_rep: match (villager, player) { + (true, Some(uuid)) => villager::ext(m).map_or(0, |e| e.gossips.reputation(uuid)), + _ => 0, + }, + pushable: m.pushable(), + category: m.category(), + potential_job: villager_brain::potential_job_site(m), + meeting: villager.then_some(m.extra.meeting).flatten(), + profession: m.extra.variant2, + xp: villager::xp(m), + villager: villager.then(|| Arc::new(villager::seen(m))), + stats: [ + f64::from(m.attrs.max_health), + m.extra.vector[0], + m.attrs.speed, + ], + } + } /// `EntityType.getCategory`'s name. pub fn category(&self) -> &'static str { match self.kind.category.as_str() { @@ -1000,6 +1107,7 @@ pub fn apply_brain_events(mobs: &mut [Mob], events: &mut Vec, from: us MobEvent::LockGaze { mob, partner } => { if let Some(m) = mobs.iter_mut().find(|m| m.id == mob) { animal_brain::lock_gaze(m, partner); + villager_brain::lock_gaze_half(m, Who::Mob(partner)); } events.remove(i); } @@ -1156,17 +1264,24 @@ impl Mob { attack_damage: kind.attack_damage, max_health: kind.max_health, follow_range: kind.follow_range, + knockback_resistance: kind.knockback_resistance, + spawn_reinforcements: kind + .attributes + .get("minecraft:spawn_reinforcements") + .copied() + .unwrap_or(0.0), + jump_strength: kind.jump_strength, }, kind, }; - // setCanPickUpLoot(true) in the Villager, AbstractPiglin, Allay, - // Dolphin, Fox and Panda constructors. + // setCanPickUpLoot(true) in the Villager, AbstractPiglin, Dolphin, + // Fox and Panda constructors (an allay's is false until it holds + // something: it overrides canPickUpLoot). mob.extra.pick_up_loot = matches!( mob.species(), Species::Villager | Species::Piglin | Species::PiglinBrute - | Species::Allay | Species::Dolphin | Species::Fox | Species::Panda @@ -1174,6 +1289,15 @@ impl Mob { ai::register(&mut mob); mob } + /// `Mob.canPickUpLoot`, with the overrides: an allay that holds + /// something off its pickup cooldown, a sulfur cube with no block. + pub fn can_pick_up_loot(&self) -> bool { + match self.species() { + Species::Allay => self.extra.main_hand.is_some() && self.extra.ints[3] <= 0, + Species::SulfurCube => !sulfur_cube::has_body(self), + _ => self.extra.pick_up_loot, + } + } pub fn bounds(&self) -> Aabb { let w = f64::from(self.width()) / 2.0; Aabb::new( diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 9c825d61b..8d6c728fd 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -895,11 +895,14 @@ fn check_despawn(m: &mut Mob, w: &MobWorld) -> bool { || super::nautilus::is_nautilus(m.species()) && m.extra.tame || m.species() == Species::SulfurCube && (super::sulfur_cube::has_body(m) || m.extra.scratch[3] != 0); - // removeWhenFarAway: animals, wardens, patrols and raiders, zombie - // villagers being cured and ridden mobs stay. + // removeWhenFarAway: animals, wardens, allays, patrols and raiders, + // zombie villagers being cured and ridden mobs stay; the wither's + // checkDespawn never removes it for distance. let creature = m.kind.category == "creature"; - let removable = !(m.species() == Species::Warden - || creature && m.kind.flags.animal && !m.extra.jockey + let removable = !(matches!( + m.species(), + Species::Warden | Species::Allay | Species::Wither + ) || creature && m.kind.flags.animal && !m.extra.jockey || super::raider::keeps_alive(m, d) || super::zombie_villager::keeps_alive(m) || super::riders::keeps_alive(m)); diff --git a/crates/horizon-client/src/mobs/allay.rs b/crates/horizon-client/src/mobs/allay.rs index 2b674fb87..3c25053ba 100644 --- a/crates/horizon-client/src/mobs/allay.rs +++ b/crates/horizon-client/src/mobs/allay.rs @@ -27,7 +27,6 @@ pub fn register(m: &mut Mob) { m.ai.nav.kind = NavKind::Flying; // FlyingPathNavigation.setRequiredPathLength(48). m.ai.nav.required_path_length = 48.0; - m.persistent = true; let g = &mut m.ai.goals; g.add(0, Panic::new(2.5)); g.add(1, Fetch); diff --git a/crates/horizon-client/src/mobs/aquatic.rs b/crates/horizon-client/src/mobs/aquatic.rs index 667802b26..8cb1f7873 100644 --- a/crates/horizon-client/src/mobs/aquatic.rs +++ b/crates/horizon-client/src/mobs/aquatic.rs @@ -445,35 +445,38 @@ impl Goal for PufferfishPuff { } } /// finalizeSpawn variants (tropical fish patterns and colours). +/// TropicalFish.COMMON_VARIANTS: (pattern index, base colour, pattern colour). +pub const COMMON_TROPICAL: [(u8, u8, u8); 22] = [ + (7, 1, 7), + (6, 7, 7), + (6, 7, 11), + (11, 0, 7), + (1, 11, 7), + (0, 1, 0), + (5, 6, 3), + (9, 10, 4), + (11, 0, 14), + (5, 0, 4), + (8, 0, 7), + (11, 0, 1), + (3, 9, 6), + (4, 5, 3), + (10, 14, 0), + (2, 7, 14), + (9, 14, 0), + (6, 0, 4), + (0, 14, 0), + (1, 7, 0), + (3, 9, 4), + (6, 4, 4), +]; pub fn finalize(m: &mut Mob) { if m.species() == Species::TropicalFish { - // TropicalFish.COMMON_VARIANTS: (pattern index, base colour, pattern colour). - const COMMON: [(u8, u8, u8); 22] = [ - (7, 1, 7), - (6, 7, 7), - (6, 7, 11), - (11, 0, 7), - (1, 11, 7), - (0, 1, 0), - (5, 6, 3), - (9, 10, 4), - (11, 0, 14), - (5, 0, 4), - (8, 0, 7), - (11, 0, 1), - (3, 9, 6), - (4, 5, 3), - (10, 14, 0), - (2, 7, 14), - (9, 14, 0), - (6, 0, 4), - (0, 14, 0), - (1, 7, 0), - (3, 9, 4), - (6, 4, 4), - ]; let (p, b, c) = if m.random.next_float() < 0.9 { - COMMON[m.random.next_int_bound(COMMON.len() as i32).unwrap_or(0) as usize] + COMMON_TROPICAL[m + .random + .next_int_bound(COMMON_TROPICAL.len() as i32) + .unwrap_or(0) as usize] } else { ( m.random.next_int_bound(12).unwrap_or(0) as u8, diff --git a/crates/horizon-client/src/mobs/finalize.rs b/crates/horizon-client/src/mobs/finalize.rs new file mode 100644 index 000000000..0e5a0a2ec --- /dev/null +++ b/crates/horizon-client/src/mobs/finalize.rs @@ -0,0 +1,2051 @@ +//! `Mob.finalizeSpawn` and its overrides, for every spawn path (natural +//! spawning, chunk generation, spawners, trial spawners, spawn eggs, +//! commands, structures, patrols, raids, jockeys, conversions...). +//! +//! Every draw vanilla takes from `level.getRandom()` comes from the random +//! the caller passes (the level's random for a running level, the region's +//! for chunk generation); the few that come from the entity's own random +//! (`Zombie.handleAttributes`, `Piglin.createSpawnWeapon`, salmon sizes) come +//! from `Mob::random`. The order of the draws follows vanilla's class chain: +//! each species runs its own part before or after its superclass as vanilla +//! does. Jockeys a mob brings (`EntityType.create` inside finalizeSpawn) are +//! made through [`FinalizeLevel::create`], finalized with the same random and +//! returned in [`Ctx::created`]. +//! +//! Variant state kept in [`super::Extra`]: `variant` is the registry or enum +//! index of the species' variant (cat, wolf, frog, farm animals by climate, +//! zombie nautilus, horse, llama, fox, rabbit, parrot, axolotl, salmon, +//! tropical fish pattern, panda main gene, villager type), `variant2` the +//! second part (horse markings, llama strength, panda hidden gene, tropical +//! fish base colour), `variant3` the tropical fish pattern colour, and +//! `sound_variant` the index in the species' sound variant registry. +use super::{Mob, Species, farm::Climate, gear, spawning::LocalDifficulty}; +use crate::{enchant::Enchantments, items::Items}; +use horizon_core::{ + geometry::Aabb, + random::{LegacyRandom, RandomSource}, +}; +use serde_json::Value; +use std::{fs, path::Path, sync::Arc}; + +/// `EntitySpawnReason`. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum Reason { + Natural, + ChunkGeneration, + Spawner, + Structure, + Breeding, + MobSummoned, + Jockey, + Event, + Conversion, + Reinforcement, + Triggered, + Bucket, + SpawnItemUse, + Command, + Dispenser, + Patrol, + TrialSpawner, + Load, + DimensionTravel, +} +impl Reason { + pub const ALL: [Self; 19] = [ + Self::Natural, + Self::ChunkGeneration, + Self::Spawner, + Self::Structure, + Self::Breeding, + Self::MobSummoned, + Self::Jockey, + Self::Event, + Self::Conversion, + Self::Reinforcement, + Self::Triggered, + Self::Bucket, + Self::SpawnItemUse, + Self::Command, + Self::Dispenser, + Self::Patrol, + Self::TrialSpawner, + Self::Load, + Self::DimensionTravel, + ]; + pub fn name(self) -> &'static str { + match self { + Self::Natural => "NATURAL", + Self::ChunkGeneration => "CHUNK_GENERATION", + Self::Spawner => "SPAWNER", + Self::Structure => "STRUCTURE", + Self::Breeding => "BREEDING", + Self::MobSummoned => "MOB_SUMMONED", + Self::Jockey => "JOCKEY", + Self::Event => "EVENT", + Self::Conversion => "CONVERSION", + Self::Reinforcement => "REINFORCEMENT", + Self::Triggered => "TRIGGERED", + Self::Bucket => "BUCKET", + Self::SpawnItemUse => "SPAWN_ITEM_USE", + Self::Command => "COMMAND", + Self::Dispenser => "DISPENSER", + Self::Patrol => "PATROL", + Self::TrialSpawner => "TRIAL_SPAWNER", + Self::Load => "LOAD", + Self::DimensionTravel => "DIMENSION_TRAVEL", + } + } + pub fn parse(name: &str) -> Option { + Self::ALL.into_iter().find(|r| r.name() == name) + } +} + +/// The level as finalizeSpawn reads it. +pub trait FinalizeLevel { + /// The biome id at a block. + fn biome(&self, pos: [i32; 3]) -> String; + /// The biome at a block is in a biome tag (`minecraft:...`). + fn biome_tag(&self, tag: &str, pos: [i32; 3]) -> bool; + /// The structures with a piece at a block. + fn structures_with_piece_at(&self, pos: [i32; 3]) -> Vec; + /// `EnvironmentAttributes.MOON_PHASE` at a block (0 full moon to 7). + fn moon_phase(&self, pos: [i32; 3]) -> usize; + /// `ServerLevel.isSpawningMonsters`. + fn spawning_monsters(&self) -> bool; + /// `Level.noCollision(AABB)`. + fn no_collision(&self, bounds: Aabb) -> bool; + /// The first chicken (in entity order) whose box meets `bounds` and + /// that is not being ridden (`Zombie.finalizeSpawn`'s chicken jockeys). + fn free_chicken(&self, bounds: Aabb) -> Option; + /// A chicken found by `free_chicken` carries the zombie `rider` + /// (`setChickenJockey(true)` and `startRiding`). + fn ride_chicken(&mut self, chicken: u32, rider: u32); + /// `getGameTime`. + fn game_time(&self) -> i64; + /// `SpecialDates.isHalloween`. + fn halloween(&self) -> bool; + /// `EntityType.create(level, reason)`: a new mob of a type (an id and an + /// own random from the caller), or None where it cannot exist (monsters + /// in peaceful). + fn create(&mut self, kind: &str, reason: Reason) -> Option; +} + +/// `AgeableMob.AgeableMobGroupData`. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Ageable { + pub size: i32, + pub should_spawn_baby: bool, + pub baby_chance: f32, +} +impl Ageable { + pub fn new(should_spawn_baby: bool, baby_chance: f32) -> Self { + Self { + size: 0, + should_spawn_baby, + baby_chance, + } + } + /// `AgeableMobGroupData(boolean)`. + pub fn babies(should_spawn_baby: bool) -> Self { + Self::new(should_spawn_baby, 0.05) + } + /// `AgeableMobGroupData(float)`. + pub fn chance(baby_chance: f32) -> Self { + Self::new(true, baby_chance) + } +} +/// `SpawnGroupData`: what the mobs of a pack share. +#[derive(Clone, Debug, PartialEq)] +pub enum Group { + Ageable(Ageable), + /// `Zombie.ZombieGroupData`, and `Husk.HuskGroupData` with `tried_camel`. + Zombie { + baby: bool, + jockey: bool, + husk: Option, + }, + /// `Spider.SpiderEffectsGroupData`. + Spider(Option<&'static str>), + Horse(Ageable, u8), + Llama(Ageable, u8), + Fox(Ageable, u8), + Rabbit(Ageable, u8), + Axolotl(Ageable, [u8; 2]), + Wolf(Ageable, u8), + /// `AbstractSchoolingFish.SchoolSpawnGroupData` (the leader's id), and + /// `TropicalFishGroupData` with its variant. + School(u32, Option<(u8, u8, u8)>), +} +impl Group { + fn ageable(&mut self) -> Option<&mut Ageable> { + match self { + Self::Ageable(a) + | Self::Horse(a, _) + | Self::Llama(a, _) + | Self::Fox(a, _) + | Self::Rabbit(a, _) + | Self::Axolotl(a, _) + | Self::Wolf(a, _) => Some(a), + _ => None, + } + } +} + +/// A variant registry entry's spawn conditions (`SpawnPrioritySelectors`). +#[derive(Clone, Debug)] +enum Condition { + Biome(Vec), + Structure(Vec), + Moon(Option, Option), +} +#[derive(Clone, Debug)] +pub struct VariantRegistry { + /// Entry names in registry order (sorted by id). + pub names: Vec, + selectors: Vec<(usize, Option, i32)>, +} +impl VariantRegistry { + fn load(dir: &Path, structure_tags: &dyn Fn(&str) -> Vec) -> Self { + let mut names: Vec = fs::read_dir(dir) + .into_iter() + .flatten() + .flatten() + .filter_map(|e| { + e.file_name() + .to_str() + .and_then(|n| n.strip_suffix(".json")) + .map(str::to_owned) + }) + .collect(); + names.sort(); + let mut selectors = Vec::new(); + for (i, name) in names.iter().enumerate() { + let Ok(bytes) = fs::read(dir.join(format!("{name}.json"))) else { + continue; + }; + let Ok(v) = serde_json::from_slice::(&bytes) else { + continue; + }; + for s in v["spawn_conditions"].as_array().into_iter().flatten() { + let priority = s["priority"].as_i64().unwrap_or(0) as i32; + let c = &s["condition"]; + let list = |v: &Value| -> Vec { + match v { + Value::String(s) => vec![s.clone()], + Value::Array(a) => a + .iter() + .filter_map(|x| x.as_str().map(str::to_owned)) + .collect(), + _ => Vec::new(), + } + }; + let condition = match c["type"].as_str() { + Some("minecraft:biome") => Some(Condition::Biome(list(&c["biomes"]))), + Some("minecraft:structure") => Some(Condition::Structure( + list(&c["structures"]) + .into_iter() + .flat_map(|s| match s.strip_prefix('#') { + Some(tag) => structure_tags(tag), + None => vec![s], + }) + .collect(), + )), + Some("minecraft:moon_brightness") => { + let r = &c["range"]; + Some(match r.as_f64() { + Some(x) => Condition::Moon(Some(x), Some(x)), + None => Condition::Moon(r["min"].as_f64(), r["max"].as_f64()), + }) + } + _ => None, + }; + selectors.push((i, condition, priority)); + } + } + Self { names, selectors } + } + pub fn index(&self, name: &str) -> Option { + let name = name.strip_prefix("minecraft:").unwrap_or(name); + self.names.iter().position(|n| n == name) + } + /// `VariantUtils.selectVariantToSpawn`: the entries of the highest + /// priority whose condition holds, one of them at random. + pub fn select( + &self, + level: &L, + pos: [i32; 3], + random: &mut impl RandomSource, + ) -> Option { + let mut unpacked: Vec<&(usize, Option, i32)> = self.selectors.iter().collect(); + unpacked.sort_by_key(|s| std::cmp::Reverse(s.2)); + let mut highest = i32::MIN; + let mut selected = Vec::new(); + for (entry, condition, priority) in unpacked { + if *priority < highest { + continue; + } + let ok = match condition { + None => true, + Some(Condition::Biome(list)) => list.iter().any(|b| match b.strip_prefix('#') { + Some(tag) => level.biome_tag(tag, pos), + None => level.biome(pos) == *b, + }), + Some(Condition::Structure(list)) => { + let here = level.structures_with_piece_at(pos); + here.iter().any(|s| list.contains(s)) + } + Some(Condition::Moon(min, max)) => { + let b = f64::from(super::spawning::MOON_BRIGHTNESS[level.moon_phase(pos) & 7]); + min.is_none_or(|m| b >= m) && max.is_none_or(|m| b <= m) + } + }; + if ok { + highest = *priority; + selected.push(*entry); + } + } + if selected.is_empty() { + return None; + } + Some(selected[random.next_int_bound(selected.len() as i32).unwrap_or(0) as usize]) + } + /// `Registry.getRandom`: any entry. + pub fn random(&self, random: &mut impl RandomSource) -> usize { + if self.names.is_empty() { + return 0; + } + random.next_int_bound(self.names.len() as i32).unwrap_or(0) as usize + } +} + +/// The registries finalizeSpawn reads. +pub struct SpawnRegistries { + pub pig: VariantRegistry, + pub cow: VariantRegistry, + pub chicken: VariantRegistry, + pub cat: VariantRegistry, + pub wolf: VariantRegistry, + pub frog: VariantRegistry, + pub zombie_nautilus: VariantRegistry, + pub pig_sound: VariantRegistry, + pub cow_sound: VariantRegistry, + pub chicken_sound: VariantRegistry, + pub cat_sound: VariantRegistry, + pub wolf_sound: VariantRegistry, +} +impl SpawnRegistries { + /// Loads the variant registries from a resources directory + /// (`data/minecraft//`). + pub fn load(root: &Path) -> Self { + let data = root.join("data/minecraft"); + let structure_tag = |tag: &str| -> Vec { + let path = tag.strip_prefix("minecraft:").unwrap_or(tag); + let file = data.join(format!("tags/worldgen/structure/{path}.json")); + let Ok(bytes) = fs::read(file) else { + return Vec::new(); + }; + serde_json::from_slice::(&bytes) + .ok() + .and_then(|v| v["values"].as_array().cloned()) + .into_iter() + .flatten() + .filter_map(|x| x.as_str().map(str::to_owned)) + .collect() + }; + let r = |name: &str| VariantRegistry::load(&data.join(name), &structure_tag); + Self { + pig: r("pig_variant"), + cow: r("cow_variant"), + chicken: r("chicken_variant"), + cat: r("cat_variant"), + wolf: r("wolf_variant"), + frog: r("frog_variant"), + zombie_nautilus: r("zombie_nautilus_variant"), + pig_sound: r("pig_sound_variant"), + cow_sound: r("cow_sound_variant"), + chicken_sound: r("chicken_sound_variant"), + cat_sound: r("cat_sound_variant"), + wolf_sound: r("wolf_sound_variant"), + } + } +} + +/// What finalizeSpawn needs besides the level: registries, enchantments. +pub struct SpawnData<'a> { + pub registries: &'a SpawnRegistries, + pub enchantments: &'a Enchantments, + pub items: &'a Items, + /// `EntityTypes.CAMEL_HUSK` dimensions (width, height). + pub camel_husk_size: (f32, f32), +} + +/// A finalizeSpawn call and the entities it made. +pub struct Ctx<'a, L: FinalizeLevel + ?Sized, R: RandomSource> { + pub level: &'a mut L, + pub random: &'a mut R, + pub difficulty: LocalDifficulty, + pub data: &'a SpawnData<'a>, + /// Jockeys and mounts made along the way, finalized, with their riding + /// links set (`Extra::mount` / `Extra::rider`). + pub created: Vec, +} + +// ---- attributes ----------------------------------------------------------- + +/// The attributes finalizeSpawn changes. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Attr { + MaxHealth, + MovementSpeed, + FollowRange, + AttackDamage, + KnockbackResistance, + SpawnReinforcements, + JumpStrength, + Armor, +} +impl Attr { + pub const ALL: [Self; 8] = [ + Self::MaxHealth, + Self::MovementSpeed, + Self::FollowRange, + Self::AttackDamage, + Self::KnockbackResistance, + Self::SpawnReinforcements, + Self::JumpStrength, + Self::Armor, + ]; + pub fn name(self) -> &'static str { + match self { + Self::MaxHealth => "max_health", + Self::MovementSpeed => "movement_speed", + Self::FollowRange => "follow_range", + Self::AttackDamage => "attack_damage", + Self::KnockbackResistance => "knockback_resistance", + Self::SpawnReinforcements => "spawn_reinforcements", + Self::JumpStrength => "jump_strength", + Self::Armor => "armor", + } + } + /// `RangedAttribute` bounds. + fn range(self) -> (f64, f64) { + match self { + Self::MaxHealth => (1.0, 1024.0), + Self::MovementSpeed => (0.0, 1024.0), + Self::FollowRange => (0.0, 2048.0), + Self::AttackDamage => (0.0, 2048.0), + Self::KnockbackResistance | Self::SpawnReinforcements => (0.0, 1.0), + Self::JumpStrength => (0.0, 32.0), + Self::Armor => (0.0, 30.0), + } + } +} +/// `AttributeModifier.Operation`. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Op { + AddValue, + AddMultipliedBase, + AddMultipliedTotal, +} +/// A permanent attribute modifier (`AttributeModifier`). +#[derive(Clone, Debug, PartialEq)] +pub struct Modifier { + pub attr: Attr, + pub id: &'static str, + pub amount: f64, + pub op: Op, +} +/// Attribute base values that differ from the type's and the permanent +/// modifiers finalizeSpawn added, in the order vanilla adds them. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Attributes { + pub bases: Vec<(Attr, f64)>, + pub modifiers: Vec, +} +/// The type's default value of an attribute, if the mob has it. +pub fn default_value(m: &Mob, a: Attr) -> Option { + m.kind + .attributes + .get(&format!("minecraft:{}", a.name())) + .copied() +} +pub fn base_value(m: &Mob, a: Attr) -> f64 { + m.extra + .attributes + .as_ref() + .and_then(|s| s.bases.iter().find(|(x, _)| *x == a).map(|(_, v)| *v)) + .or_else(|| default_value(m, a)) + .unwrap_or(0.0) +} +/// The transient modifiers of the mob's state (`SPEED_MODIFIER_BABY`). +fn transient(m: &Mob, a: Attr) -> Option<(f64, Op)> { + if a != Attr::MovementSpeed || !m.is_baby() { + return None; + } + match m.species() { + Species::Zombie + | Species::Husk + | Species::Drowned + | Species::ZombieVillager + | Species::ZombifiedPiglin => Some((0.5, Op::AddMultipliedBase)), + Species::Piglin => Some((f64::from(0.2f32), Op::AddMultipliedBase)), + _ => None, + } +} +/// The attribute modifiers of the mob's effects (`MobEffect.addAttributeModifier`, +/// scaled by the amplifier plus one): strength and speed. +fn effect_modifiers(m: &Mob, a: Attr) -> impl Iterator + '_ { + m.extra + .effects + .iter() + .filter_map(move |&(effect, _, amplifier)| { + let level = f64::from(amplifier + 1); + match (effect.trim_start_matches("minecraft:"), a) { + ("strength", Attr::AttackDamage) => Some((3.0 * level, Op::AddValue)), + ("speed", Attr::MovementSpeed) => { + Some((f64::from(0.2f32) * level, Op::AddMultipliedTotal)) + } + _ => None, + } + }) +} +/// `AttributeInstance.getValue`: the base, then the modifiers by operation. +pub fn value(m: &Mob, a: Attr) -> f64 { + compute(m, a, true) +} +fn compute(m: &Mob, a: Attr, with_transient: bool) -> f64 { + let mut base = base_value(m, a); + let empty = Vec::new(); + let mods = m.extra.attributes.as_ref().map_or(&empty, |s| &s.modifiers); + let mut ops: Vec<(f64, Op)> = mods + .iter() + .filter(|x| x.attr == a) + .map(|x| (x.amount, x.op)) + .collect(); + if with_transient { + ops.extend(transient(m, a)); + ops.extend(effect_modifiers(m, a)); + } + for (amount, op) in &ops { + if *op == Op::AddValue { + base += amount; + } + } + let mut result = base; + for (amount, op) in &ops { + if *op == Op::AddMultipliedBase { + result += base * amount; + } + } + for (amount, op) in &ops { + if *op == Op::AddMultipliedTotal { + result *= 1.0 + amount; + } + } + let (lo, hi) = a.range(); + if result.is_nan() { + lo + } else { + result.clamp(lo, hi) + } +} +/// Copies the attribute values into the fields the game reads. +fn refresh(m: &mut Mob, a: Attr) { + match a { + Attr::MaxHealth => m.attrs.max_health = value(m, a) as f32, + Attr::MovementSpeed => m.attrs.speed = base_with_modifiers(m, a), + Attr::FollowRange => m.attrs.follow_range = value(m, a), + Attr::AttackDamage => m.attrs.attack_damage = value(m, a) as f32, + Attr::KnockbackResistance => m.attrs.knockback_resistance = value(m, a), + Attr::SpawnReinforcements => m.attrs.spawn_reinforcements = value(m, a), + Attr::JumpStrength => m.attrs.jump_strength = value(m, a), + Attr::Armor => {} + } +} +/// The value without the transient baby modifier (`speed_bonus` applies +/// that one to movement speed). +fn base_with_modifiers(m: &Mob, a: Attr) -> f64 { + compute(m, a, false) +} +/// `AttributeInstance.setBaseValue`. +pub fn set_base(m: &mut Mob, a: Attr, v: f64) { + let s = m.extra.attributes.get_or_insert_with(Box::default); + match s.bases.iter_mut().find(|(x, _)| *x == a) { + Some(b) => b.1 = v, + None => s.bases.push((a, v)), + } + refresh(m, a); +} +pub fn has_modifier(m: &Mob, a: Attr, id: &str) -> bool { + m.extra + .attributes + .as_ref() + .is_some_and(|s| s.modifiers.iter().any(|x| x.attr == a && x.id == id)) +} +/// `addOrReplacePermanentModifier`. +pub fn add_modifier(m: &mut Mob, a: Attr, id: &'static str, amount: f64, op: Op) { + let s = m.extra.attributes.get_or_insert_with(Box::default); + let new = Modifier { + attr: a, + id, + amount, + op, + }; + match s.modifiers.iter_mut().find(|x| x.attr == a && x.id == id) { + Some(x) => *x = new, + None => s.modifiers.push(new), + } + refresh(m, a); +} +pub fn max_health(m: &Mob) -> f32 { + value(m, Attr::MaxHealth) as f32 +} + +// ---- species facts -------------------------------------------------------- + +/// `AgeableMob` subclasses. +pub fn ageable(s: Species) -> bool { + use Species as S; + matches!( + s, + S::Pig + | S::Cow + | S::Mooshroom + | S::Sheep + | S::Chicken + | S::Rabbit + | S::Horse + | S::Donkey + | S::Mule + | S::Llama + | S::TraderLlama + | S::Goat + | S::Panda + | S::PolarBear + | S::Fox + | S::Wolf + | S::Cat + | S::Ocelot + | S::Parrot + | S::Bee + | S::Turtle + | S::Armadillo + | S::Frog + | S::Axolotl + | S::Squid + | S::GlowSquid + | S::Dolphin + | S::Villager + | S::WanderingTrader + | S::Slime + | S::MagmaCube + | S::SulfurCube + | S::Hoglin + | S::Strider + | S::Camel + | S::CamelHusk + | S::Sniffer + | S::HappyGhast + | S::Nautilus + | S::ZombieNautilus + | S::ZombieHorse + | S::SkeletonHorse + ) +} +fn zombie(s: Species) -> bool { + matches!( + s, + Species::Zombie + | Species::Husk + | Species::Drowned + | Species::ZombieVillager + | Species::ZombifiedPiglin + ) +} +fn skeleton(s: Species) -> bool { + matches!( + s, + Species::Skeleton + | Species::Stray + | Species::Bogged + | Species::Parched + | Species::WitherSkeleton + ) +} +fn raider(s: Species) -> bool { + super::raider::is_raider(s) +} +/// `EntityType.isAllowedInPeaceful`: the types built with `notInPeaceful`. +pub fn allowed_in_peaceful(kind: &str) -> bool { + !matches!( + kind.strip_prefix("minecraft:").unwrap_or(kind), + "blaze" + | "bogged" + | "breeze" + | "cave_spider" + | "creaking" + | "creeper" + | "drowned" + | "elder_guardian" + | "enderman" + | "endermite" + | "evoker" + | "ghast" + | "giant" + | "guardian" + | "hoglin" + | "husk" + | "illusioner" + | "magma_cube" + | "parched" + | "phantom" + | "piglin_brute" + | "pillager" + | "ravager" + | "silverfish" + | "skeleton" + | "slime" + | "spider" + | "stray" + | "vex" + | "vindicator" + | "warden" + | "witch" + | "wither" + | "wither_skeleton" + | "zoglin" + | "zombie" + | "zombie_villager" + | "zombified_piglin" + ) +} +/// `getBabyStartAge`. +fn baby_start_age(s: Species) -> i32 { + if s == Species::Sniffer { + super::sniffer::BABY_START_AGE + } else { + -24000 + } +} +/// The age a non-ageable baby (zombies, piglins, zoglins) keeps. +pub const NON_AGEABLE_BABY: i32 = i32::MIN / 2; + +/// `AgeableMob.setAge`, with each class's `ageBoundaryReached`. +pub fn set_age(m: &mut Mob, age: i32) { + let old = m.extra.age; + m.extra.age = age; + if (old < 0) != (age < 0) { + age_boundary_reached(m); + } +} +fn age_boundary_reached(m: &mut Mob) { + let baby = m.is_baby(); + match m.species() { + Species::Goat => set_base(m, Attr::AttackDamage, if baby { 1.0 } else { 2.0 }), + Species::Hoglin => set_base(m, Attr::AttackDamage, if baby { 0.5 } else { 6.0 }), + Species::SulfurCube if !baby => sulfur_size(m, 2), + _ => {} + } +} +/// `setBaby`: AgeableMob through its age, zombies, piglins and zoglins. +pub fn set_baby(m: &mut Mob, baby: bool) { + let s = m.species(); + if ageable(s) { + set_age(m, if baby { baby_start_age(s) } else { 0 }); + return; + } + match s { + _ if zombie(s) || s == Species::Piglin => { + m.extra.age = if baby { NON_AGEABLE_BABY } else { 0 }; + } + Species::Zoglin => { + m.extra.age = if baby { NON_AGEABLE_BABY } else { 0 }; + if baby { + set_base(m, Attr::AttackDamage, 0.5); + } + } + _ => {} + } +} + +// ---- equipment ------------------------------------------------------------ + +/// `EquipmentSlot` names (the `drops::SLOTS` order). +pub const SLOT_NAMES: [&str; 8] = super::drops::SLOTS; +const MAINHAND: usize = 0; +const OFFHAND: usize = 1; +const FEET: usize = 2; +const LEGS: usize = 3; +const CHEST: usize = 4; +const HEAD: usize = 5; +const SADDLE: usize = 7; +/// The `gear` slot of an `EquipmentSlot`. +fn gear_slot(slot: usize) -> usize { + match slot { + MAINHAND => gear::MAIN, + OFFHAND => gear::OFF, + FEET => gear::FEET, + LEGS => gear::LEGS, + CHEST => gear::CHEST, + _ => gear::HEAD, + } +} +pub fn item(m: &Mob, slot: usize) -> Option> { + match slot { + MAINHAND => m.extra.main_hand.clone(), + OFFHAND => m.extra.off_hand.clone(), + FEET => m.extra.feet.clone(), + LEGS => m.extra.legs.clone(), + CHEST => m.extra.chest.clone(), + HEAD => m.extra.head.clone(), + SADDLE => m.extra.saddled.then(|| Arc::from("minecraft:saddle")), + _ => None, + } +} +/// `setItemSlot` with a fresh stack (enchantments cleared). +pub fn set_item(m: &mut Mob, slot: usize, id: &str) { + let v = Some(Arc::from(id)); + match slot { + MAINHAND => m.extra.main_hand = v, + OFFHAND => m.extra.off_hand = v, + FEET => m.extra.feet = v, + LEGS => m.extra.legs = v, + CHEST => m.extra.chest = v, + HEAD => m.extra.head = v, + SADDLE => { + m.extra.saddled = true; + m.extra.saddle_stack = None; + return; + } + _ => return, + } + gear::set(m, gear_slot(slot), Vec::new()); +} +/// `Mob.setDropChance`. +pub fn set_drop_chance(m: &mut Mob, slot: usize, chance: f32) { + let c = m + .extra + .drop_chances + .get_or_insert_with(|| Box::new([super::drops::DEFAULT_DROP_CHANCE; 8])); + c[slot] = chance; +} +/// The drop chance stored for a slot (`DropChances.byEquipment`). +pub fn stored_drop_chance(m: &Mob, slot: usize) -> f32 { + m.extra + .drop_chances + .as_ref() + .map_or(super::drops::DEFAULT_DROP_CHANCE, |c| c[slot]) +} + +impl Ctx<'_, L, R> { + fn float(&mut self) -> f32 { + self.random.next_float() + } + fn int(&mut self, bound: i32) -> i32 { + self.random.next_int_bound(bound).unwrap_or(0) + } + fn special(&self) -> f32 { + self.difficulty.special() + } + /// `UniformInt.sample`. + fn uniform(&mut self, min: i32, max: i32) -> i32 { + self.random + .next_int_between_inclusive(min, max) + .unwrap_or(min) + } + + // ---- Mob ---------------------------------------------------------- + + /// `Mob.finalizeSpawn`: the follow range spawn bonus and left-handedness. + fn mob(&mut self, m: &mut Mob, g: Option) -> Option { + if !has_modifier(m, Attr::FollowRange, "minecraft:random_spawn_bonus") { + let bonus = self.random.triangle(0.0, 0.114_850_000_000_000_01); + add_modifier( + m, + Attr::FollowRange, + "minecraft:random_spawn_bonus", + bonus, + Op::AddMultipliedBase, + ); + } + m.extra.left_handed = self.float() < 0.05; + g + } + /// `AgeableMob.finalizeSpawn`. + fn ageable(&mut self, m: &mut Mob, g: Option) -> Option { + let mut g = g.unwrap_or(Group::Ageable(Ageable::babies(true))); + if let Some(a) = g.ageable() { + let (should, size, chance) = (a.should_spawn_baby, a.size, a.baby_chance); + if should && size > 0 && self.float() <= chance { + let start = baby_start_age(m.species()); + set_age(m, start); + } + if let Some(a) = g.ageable() { + a.size += 1; + } + } + self.mob(m, Some(g)) + } + /// `Mob.populateDefaultEquipmentSlots`: a set of armour from the head + /// down, of one material. + fn armor(&mut self, m: &mut Mob) { + if self.float() >= 0.15 * self.special() { + return; + } + let mut kind = self.int(3); + for _ in 1..=3 { + if self.float() < 0.1087 { + kind += 1; + } + } + let partial = if self.difficulty.hard { 0.1 } else { 0.25 }; + const MATERIAL: [&str; 6] = [ + "leather", + "copper", + "golden", + "chainmail", + "iron", + "diamond", + ]; + let mut first = true; + // EQUIPMENT_POPULATION_ORDER: head, chest, legs, feet. + for (slot, piece) in [ + (HEAD, "helmet"), + (CHEST, "chestplate"), + (LEGS, "leggings"), + (FEET, "boots"), + ] { + if !first && self.float() < partial { + break; + } + first = false; + if item(m, slot).is_none() + && let Some(material) = MATERIAL.get(kind as usize) + { + set_item(m, slot, &format!("minecraft:{material}_{piece}")); + } + } + } + /// `populateDefaultEquipmentSlots` of each class. + fn populate(&mut self, m: &mut Mob) { + use Species as S; + match m.species() { + S::Zombie | S::Husk | S::ZombieVillager => { + self.armor(m); + let chance = if self.difficulty.hard { 0.05 } else { 0.01 }; + if self.float() < chance { + let weapon = match self.int(6) { + 0 => "minecraft:iron_sword", + 1 => "minecraft:iron_spear", + _ => "minecraft:iron_shovel", + }; + set_item(m, MAINHAND, weapon); + } + } + S::Drowned => { + if f64::from(self.float()) > 0.9 { + let weapon = if self.int(16) < 10 { + "minecraft:trident" + } else { + "minecraft:fishing_rod" + }; + set_item(m, MAINHAND, weapon); + } + } + S::ZombifiedPiglin => { + let weapon = if self.int(20) == 0 { + "minecraft:golden_spear" + } else { + "minecraft:golden_sword" + }; + set_item(m, MAINHAND, weapon); + } + S::WitherSkeleton => set_item(m, MAINHAND, "minecraft:stone_sword"), + s if skeleton(s) => { + self.armor(m); + set_item(m, MAINHAND, "minecraft:bow"); + } + S::Pillager => set_item(m, MAINHAND, "minecraft:crossbow"), + S::Vindicator => { + if super::raider::state(m).is_none_or(|s| s.raid_id.is_none()) { + set_item(m, MAINHAND, "minecraft:iron_axe"); + } + } + S::Piglin => { + if !m.is_baby() { + for (slot, piece) in [ + (HEAD, "helmet"), + (CHEST, "chestplate"), + (LEGS, "leggings"), + (FEET, "boots"), + ] { + if self.float() < 0.1 { + set_item(m, slot, &format!("minecraft:golden_{piece}")); + } + } + } + } + S::PiglinBrute => set_item(m, MAINHAND, "minecraft:golden_axe"), + S::Vex => { + set_item(m, MAINHAND, "minecraft:iron_sword"); + set_drop_chance(m, MAINHAND, 0.0); + } + S::Fox => { + if self.float() < 0.2 { + let odds = self.float(); + let held = if odds < 0.05 { + "minecraft:emerald" + } else if odds < 0.2 { + "minecraft:egg" + } else if odds < 0.4 { + if self.random.next_boolean() { + "minecraft:rabbit_foot" + } else { + "minecraft:rabbit_hide" + } + } else if odds < 0.6 { + "minecraft:wheat" + } else if odds < 0.8 { + "minecraft:leather" + } else { + "minecraft:feather" + }; + set_item(m, MAINHAND, held); + } + } + _ => self.armor(m), + } + } + /// `Mob.populateDefaultEquipmentEnchantments`: the weapon one time in + /// four and each piece of armour one time in two, scaled by the local + /// difficulty, through the `mob_spawn_equipment` provider. + fn enchant(&mut self, m: &mut Mob) { + if m.species() == Species::WitherSkeleton { + return; + } + self.enchant_slot(m, MAINHAND, 0.25); + // Pillager.enchantSpawnedWeapon: one crossbow in 300 pierces + // (`pillager_spawn_crossbow`). + if m.species() == Species::Pillager + && self.int(300) == 0 + && item(m, MAINHAND).as_deref() == Some("minecraft:crossbow") + { + gear::upgrade(m, gear::MAIN, "minecraft:piercing", 1); + } + // EquipmentSlot.VALUES order: feet, legs, chest, head. + for slot in [FEET, LEGS, CHEST, HEAD] { + self.enchant_slot(m, slot, 0.5); + } + } + fn enchant_slot(&mut self, m: &mut Mob, slot: usize, chance: f32) { + let Some(id) = item(m, slot) else { return }; + if self.float() >= chance * self.special() { + return; + } + self.enchant_by_cost(m, slot, &id); + } + /// `EnchantmentsByCostWithDifficulty.enchant` (`mob_spawn_equipment`: + /// cost 5 to 5 + 17 x the special multiplier, `#on_mob_spawn_equipment`). + fn enchant_by_cost(&mut self, m: &mut Mob, slot: usize, id: &str) { + let top = 5 + (self.special() * 17.0) as i32; + let cost = self.random.next_int_between_inclusive(5, top).unwrap_or(5); + let e = self.data.enchantments; + let pool = e.tag("minecraft:on_mob_spawn_equipment"); + let picked = e.select(self.data.items, self.random, id, cost, pool); + for (enchantment, level) in picked { + gear::upgrade(m, gear_slot(slot), &enchantment, level); + } + } + fn halloween(&mut self, m: &mut Mob) { + if m.extra.head.is_none() && self.level.halloween() && self.float() < 0.25 { + let head = if self.float() < 0.1 { + "minecraft:jack_o_lantern" + } else { + "minecraft:carved_pumpkin" + }; + set_item(m, HEAD, head); + set_drop_chance(m, HEAD, 0.0); + } + } + + // ---- jockeys ------------------------------------------------------ + + fn create(&mut self, kind: &str, reason: Reason, at: &Mob) -> Option { + let mut j = self.level.create(kind, reason)?; + // snapTo(x, y, z, yRot, 0). + j.position = at.position; + j.previous = at.position; + j.yaw = at.yaw; + j.previous_yaw = at.yaw; + j.body_yaw = at.yaw; + j.head_yaw = at.yaw; + j.head_pitch = 0.0; + Some(j) + } + /// `startRiding`: the rider leaves the vehicle it was on. + fn ride(&mut self, rider: &mut Mob, vehicle: &mut Mob) { + if let Some(old) = rider.extra.mount + && let Some(v) = self.created.iter_mut().find(|c| c.id == old) + && v.extra.rider == Some(rider.id) + { + v.extra.rider = None; + } + rider.extra.mount = Some(vehicle.id); + rider.extra.passenger = true; + if vehicle.extra.rider.is_none() { + vehicle.extra.rider = Some(rider.id); + } + } + + // ---- the classes -------------------------------------------------- + + fn zombie(&mut self, m: &mut Mob, g: Option, reason: Reason) -> Option { + let mut g = self.mob(m, g); + let special = self.special(); + if reason != Reason::Conversion { + m.extra.pick_up_loot = self.float() < 0.55 * special; + } + if g.is_none() { + g = Some(Group::Zombie { + baby: self.float() < 0.05, + jockey: true, + husk: None, + }); + } + if let Some(Group::Zombie { baby, jockey, .. }) = g { + if baby { + set_baby(m, true); + if jockey { + if f64::from(self.float()) < 0.05 { + let search = m + .bounds() + .inflate(horizon_core::geometry::Vec3::new(5.0, 3.0, 5.0)); + if let Some(chicken) = self.level.free_chicken(search) { + self.level.ride_chicken(chicken, m.id); + m.extra.mount = Some(chicken); + m.extra.passenger = true; + } + } else if f64::from(self.float()) < 0.05 + && let Some(mut chicken) = + self.create("minecraft:chicken", Reason::Jockey, m) + { + self.finalize(&mut chicken, Reason::Jockey, None); + chicken.extra.jockey = true; + self.ride(m, &mut chicken); + self.created.push(chicken); + } + } + } + m.extra.can_break_doors = self.float() < special * 0.1; + if reason != Reason::Conversion { + self.populate(m); + self.enchant(m); + } + } + self.halloween(m); + self.zombie_attributes(m, special, reason); + g + } + /// `Zombie.handleAttributes`, from the zombie's own random. + fn zombie_attributes(&mut self, m: &mut Mob, special: f32, reason: Reason) { + let r = &mut m.random; + let reinforcements = if m.kind.species == Species::ZombifiedPiglin { + 0.0 + } else { + r.next_double() * f64::from(0.1f32) + }; + let knockback = r.next_double() * f64::from(0.05f32); + let follow = r.next_double() * 1.5 * f64::from(special); + let leader = r.next_float() < special * 0.05; + let leader_rolls = + leader.then(|| (r.next_double() * 0.25 + 0.5, r.next_double() * 3.0 + 1.0)); + set_base(m, Attr::SpawnReinforcements, reinforcements); + add_modifier( + m, + Attr::KnockbackResistance, + "minecraft:random_spawn_bonus", + knockback, + Op::AddValue, + ); + if follow > 1.0 { + add_modifier( + m, + Attr::FollowRange, + "minecraft:zombie_random_spawn_bonus", + follow, + Op::AddMultipliedTotal, + ); + } + if let Some((reinforce, health)) = leader_rolls { + add_modifier( + m, + Attr::SpawnReinforcements, + "minecraft:leader_zombie_bonus", + reinforce, + Op::AddValue, + ); + add_modifier( + m, + Attr::MaxHealth, + "minecraft:leader_zombie_bonus", + health, + Op::AddMultipliedTotal, + ); + if !matches!( + reason, + Reason::Conversion | Reason::Load | Reason::DimensionTravel + ) { + m.health = max_health(m); + } + m.extra.can_break_doors = true; + } + } + fn husk(&mut self, m: &mut Mob, g: Option, reason: Reason) -> Option { + let mut g = self.zombie(m, g, reason); + if reason != Reason::Conversion { + m.extra.pick_up_loot = self.float() < 0.55 * self.special(); + } + if let Some(Group::Zombie { baby, jockey, .. }) = g { + g = Some(Group::Zombie { + baby, + jockey, + husk: Some(reason != Reason::Natural), + }); + } + if let Some(Group::Zombie { + husk: Some(tried @ false), + .. + }) = &mut g + { + let (bx, by, bz) = ( + m.position.x.floor(), + m.position.y.floor(), + m.position.z.floor(), + ); + let (w, h) = self.data.camel_husk_size; + let half = w / 2.0; + let bounds = Aabb::new( + horizon_core::geometry::Vec3::new( + bx + 0.5 - f64::from(half), + by, + bz + 0.5 - f64::from(half), + ), + horizon_core::geometry::Vec3::new( + bx + 0.5 + f64::from(half), + by + f64::from(h), + bz + 0.5 + f64::from(half), + ), + ); + if self.level.no_collision(bounds) { + *tried = true; + if self.float() < 0.1 { + set_item(m, MAINHAND, "minecraft:iron_spear"); + if let Some(mut camel) = + self.level.create("minecraft:camel_husk", Reason::Natural) + { + // setPos: the husk's position, the camel's own rotation. + camel.position = m.position; + camel.previous = m.position; + self.finalize(&mut camel, reason, None); + self.ride(m, &mut camel); + if let Some(mut parched) = + self.create("minecraft:parched", Reason::Natural, m) + { + self.finalize(&mut parched, reason, None); + self.ride(&mut parched, &mut camel); + self.created.push(camel); + self.created.push(parched); + } else { + self.created.push(camel); + } + } + } + } + } + g + } + fn drowned(&mut self, m: &mut Mob, g: Option, reason: Reason) -> Option { + let g = self.zombie(m, g, reason); + if m.extra.off_hand.is_none() && self.float() < 0.03 { + set_item(m, OFFHAND, "minecraft:nautilus_shell"); + set_drop_chance(m, OFFHAND, 2.0); + } + let at = [ + m.position.x.floor() as i32, + m.position.y.floor() as i32, + m.position.z.floor() as i32, + ]; + if matches!(reason, Reason::Natural | Reason::Structure) + && m.extra.main_hand.as_deref() == Some("minecraft:trident") + && self.float() < 0.5 + && !m.is_baby() + && !self + .level + .biome_tag("minecraft:more_frequent_drowned_spawns", at) + && let Some(mut nautilus) = self.create("minecraft:zombie_nautilus", Reason::Jockey, m) + { + if reason == Reason::Structure { + nautilus.persistent = true; + } + self.finalize(&mut nautilus, reason, None); + self.ride(m, &mut nautilus); + self.created.push(nautilus); + } + g + } + fn skeleton(&mut self, m: &mut Mob, g: Option) -> Option { + let g = self.mob(m, g); + self.populate(m); + self.enchant(m); + m.extra.pick_up_loot = self.float() < 0.55 * self.special(); + self.halloween(m); + if m.species() == Species::WitherSkeleton { + set_base(m, Attr::AttackDamage, 4.0); + } + g + } + fn spider(&mut self, m: &mut Mob, g: Option, reason: Reason) -> Option { + let mut g = self.mob(m, g); + if self.int(100) == 0 + && let Some(mut skeleton) = self.create("minecraft:skeleton", Reason::Jockey, m) + { + self.finalize(&mut skeleton, reason, None); + self.ride(&mut skeleton, m); + self.created.push(skeleton); + } + if g.is_none() { + let mut effect = None; + if self.difficulty.hard && self.float() < 0.1 * self.special() { + effect = Some(match self.int(5) { + 0 | 1 => "minecraft:speed", + 2 => "minecraft:strength", + 3 => "minecraft:regeneration", + _ => "minecraft:invisibility", + }); + } + g = Some(Group::Spider(effect)); + } + if let Some(Group::Spider(Some(effect))) = g { + m.add_effect(effect, -1, 0); + } + g + } + /// `Raider` then `PatrollingMonster.finalizeSpawn`. + fn raider(&mut self, m: &mut Mob, g: Option, reason: Reason) -> Option { + let witch = m.species() == Species::Witch; + super::raider::state_mut(m).can_join_raid = !witch || reason != Reason::Natural; + if !matches!(reason, Reason::Patrol | Reason::Event | Reason::Structure) + && self.float() < 0.06 + && super::raider::can_be_leader(m.species()) + { + super::raider::state_mut(m).patrol_leader = true; + } + if super::raider::state(m).is_some_and(|s| s.patrol_leader) { + set_item(m, HEAD, super::raider::BANNER); + set_drop_chance(m, HEAD, 2.0); + } + if reason == Reason::Patrol { + super::raider::state_mut(m).patrolling = true; + } + self.mob(m, g) + } + fn piglin(&mut self, m: &mut Mob, g: Option, reason: Reason) -> Option { + if reason != Reason::Structure { + if self.float() < 0.2 { + set_baby(m, true); + } else if !m.is_baby() { + // createSpawnWeapon, from the piglin's own random. + let r = &mut m.random; + let weapon = if f64::from(r.next_float()) < 0.5 { + "minecraft:crossbow" + } else if r.next_int_bound(10).unwrap_or(1) == 0 { + "minecraft:golden_spear" + } else { + "minecraft:golden_sword" + }; + set_item(m, MAINHAND, weapon); + } + } + // PiglinAi.initMemories: HUNTED_RECENTLY for 30 to 120 seconds. + let hunt = self.uniform(600, 2400); + set_memory(m, "hunted_recently", 1, i64::from(hunt)); + self.populate(m); + self.enchant(m); + self.mob(m, g) + } + fn strider(&mut self, m: &mut Mob, g: Option) -> Option { + if m.is_baby() { + return self.ageable(m, g); + } + let mut g = g; + if self.int(30) == 0 && self.level.spawning_monsters() { + if let Some(mut jockey) = self.create("minecraft:zombified_piglin", Reason::Jockey, m) { + let baby = self.float() < 0.05; + g = Some(self.strider_jockey( + m, + &mut jockey, + Some(Group::Zombie { + baby, + jockey: false, + husk: None, + }), + )); + set_item(&mut jockey, MAINHAND, "minecraft:warped_fungus_on_a_stick"); + set_item(m, SADDLE, "minecraft:saddle"); + set_drop_chance(m, SADDLE, 2.0); + self.created.push(jockey); + } + } else if self.int(10) == 0 { + if let Some(mut jockey) = self.create("minecraft:strider", Reason::Jockey, m) { + set_age(&mut jockey, -24000); + g = Some(self.strider_jockey(m, &mut jockey, None)); + self.created.push(jockey); + } + } else { + g = Some(Group::Ageable(Ageable::chance(0.5))); + } + self.ageable(m, g) + } + /// `Strider.spawnJockey`. + fn strider_jockey(&mut self, m: &mut Mob, jockey: &mut Mob, g: Option) -> Group { + jockey.position = m.position; + jockey.yaw = m.yaw; + self.finalize(jockey, Reason::Jockey, g); + self.ride(jockey, m); + Group::Ageable(Ageable::chance(0.0)) + } + /// `AbstractHorse.finalizeSpawn` with each `randomizeAttributes`. + fn horse(&mut self, m: &mut Mob, g: Option) -> Option { + let g = g.or(Some(Group::Ageable(Ageable::chance(0.2)))); + use Species as S; + match m.species() { + S::Horse => { + let health = self.horse_health(); + set_base(m, Attr::MaxHealth, f64::from(health)); + let speed = self.horse_speed(); + set_base(m, Attr::MovementSpeed, speed); + let jump = self.horse_jump(); + set_base(m, Attr::JumpStrength, jump); + } + S::Donkey | S::Mule | S::Llama | S::TraderLlama => { + let health = self.horse_health(); + set_base(m, Attr::MaxHealth, f64::from(health)); + } + S::SkeletonHorse => { + let jump = self.horse_jump(); + set_base(m, Attr::JumpStrength, jump); + } + S::ZombieHorse => { + let d = 0.066_666_666_666_666_67; + let jump = 0.5 + + self.random.next_double() * d + + self.random.next_double() * d + + self.random.next_double() * d; + set_base(m, Attr::JumpStrength, jump); + let speed = (9.0 + + self.random.next_double() + + self.random.next_double() + + self.random.next_double()) + / f64::from(42.16f32); + set_base(m, Attr::MovementSpeed, speed); + } + _ => {} + } + self.ageable(m, g) + } + fn horse_health(&mut self) -> f32 { + 15.0 + self.int(8) as f32 + self.int(9) as f32 + } + fn horse_speed(&mut self) -> f64 { + (f64::from(0.45f32) + + self.random.next_double() * 0.3 + + self.random.next_double() * 0.3 + + self.random.next_double() * 0.3) + * 0.25 + } + fn horse_jump(&mut self) -> f64 { + f64::from(0.4f32) + + self.random.next_double() * 0.2 + + self.random.next_double() * 0.2 + + self.random.next_double() * 0.2 + } + fn villager_type(&mut self, m: &mut Mob) { + let at = block(m); + m.extra.variant = villager_type(&self.level.biome(at)); + } + /// `VariantUtils.selectVariantToSpawn` into `variant`, by registry name. + fn variant(&mut self, m: &mut Mob, registry: fn(&SpawnRegistries) -> &VariantRegistry) { + let r = registry(self.data.registries); + let at = block(m); + if let Some(i) = r.select(&*self.level, at, self.random) { + let name = r.names[i].clone(); + m.extra.variant = variant_index(m.species(), &name); + } + } + fn sound(&mut self, m: &mut Mob, registry: fn(&SpawnRegistries) -> &VariantRegistry) { + let r = registry(self.data.registries); + m.extra.sound_variant = r.random(self.random) as u8; + } + + /// `finalizeSpawn` of the mob's class. + pub fn finalize(&mut self, m: &mut Mob, reason: Reason, g: Option) -> Option { + use Species as S; + let s = m.species(); + match s { + S::Pig | S::Cow | S::Chicken => { + let (v, sound): ( + fn(&SpawnRegistries) -> &VariantRegistry, + fn(&SpawnRegistries) -> &VariantRegistry, + ) = match s { + S::Pig => (|r| &r.pig, |r| &r.pig_sound), + S::Cow => (|r| &r.cow, |r| &r.cow_sound), + _ => (|r| &r.chicken, |r| &r.chicken_sound), + }; + self.variant(m, v); + self.sound(m, sound); + self.ageable(m, g) + } + S::Cat => { + let g = self.ageable(m, g); + self.variant(m, |r| &r.cat); + self.sound(m, |r| &r.cat_sound); + g + } + S::Wolf => { + let g = match g { + Some(Group::Wolf(a, v)) => { + m.extra.variant = v; + Some(Group::Wolf(a, v)) + } + other => { + let r = &self.data.registries.wolf; + let at = block(m); + match r.select(&*self.level, at, self.random) { + Some(i) => { + let v = variant_index(s, &r.names[i]); + m.extra.variant = v; + Some(Group::Wolf(Ageable::babies(false), v)) + } + None => other, + } + } + }; + self.sound(m, |r| &r.wolf_sound); + self.ageable(m, g) + } + S::Frog => { + self.variant(m, |r| &r.frog); + let jump = self.uniform(100, 140); + set_memory(m, "long_jump_cooldown", i64::from(jump), i64::MAX); + self.ageable(m, g) + } + S::ZombieNautilus => { + self.variant(m, |r| &r.zombie_nautilus); + let cooldown = self.uniform(2400, 3600); + set_memory(m, "attack_target_cooldown", i64::from(cooldown), i64::MAX); + m.extra.counter = cooldown; + self.ageable(m, g) + } + S::Nautilus => { + let cooldown = self.uniform(2400, 3600); + set_memory(m, "attack_target_cooldown", i64::from(cooldown), i64::MAX); + m.extra.counter = cooldown; + self.ageable(m, g) + } + S::Sheep => { + let climate = if self + .level + .biome_tag("minecraft:spawns_warm_variant_farm_animals", block(m)) + { + Climate::Warm + } else if self + .level + .biome_tag("minecraft:spawns_cold_variant_farm_animals", block(m)) + { + Climate::Cold + } else { + Climate::Temperate + }; + m.extra.color = super::farm::sheep_color(climate, self.random); + self.ageable(m, g) + } + S::Rabbit => { + let at = block(m); + let roll = self.int(100); + let mut variant = if self.level.biome_tag("minecraft:spawns_white_rabbits", at) { + if roll < 80 { 1 } else { 3 } + } else if self.level.biome_tag("minecraft:spawns_gold_rabbits", at) { + 4 + } else if roll < 50 { + 0 + } else if roll < 90 { + 5 + } else { + 2 + }; + let g = match g { + Some(Group::Rabbit(a, v)) => { + variant = v; + Group::Rabbit(a, v) + } + _ => Group::Rabbit(Ageable::chance(1.0), variant), + }; + m.extra.variant = variant; + self.ageable(m, Some(g)) + } + S::Horse => { + let (g, variant) = match g { + Some(Group::Horse(a, v)) => (Group::Horse(a, v), v), + _ => { + let v = self.int(7) as u8; + (Group::Horse(Ageable::babies(true), v), v) + } + }; + let markings = self.int(5) as u8; + m.extra.variant = variant; + m.extra.variant2 = markings; + self.horse(m, Some(g)) + } + S::Llama | S::TraderLlama => { + let mut g = g; + if s == S::TraderLlama { + if reason == Reason::Event { + set_age(m, 0); + } + if g.is_none() { + g = Some(Group::Ageable(Ageable::babies(false))); + } + } + let max = if self.float() < 0.04 { 5 } else { 3 }; + m.extra.variant2 = (1 + self.int(max)) as u8; + let g = match g { + Some(Group::Llama(a, v)) => { + m.extra.variant = v; + Group::Llama(a, v) + } + _ => { + let v = self.int(4) as u8; + m.extra.variant = v; + Group::Llama(Ageable::babies(true), v) + } + }; + self.horse(m, Some(g)) + } + S::Donkey | S::Mule | S::SkeletonHorse => self.horse(m, g), + S::ZombieHorse => { + if reason == Reason::Natural + && let Some(mut zombie) = self.create("minecraft:zombie", Reason::Jockey, m) + { + self.finalize(&mut zombie, reason, None); + set_item(&mut zombie, MAINHAND, "minecraft:iron_spear"); + self.ride(&mut zombie, m); + self.created.push(zombie); + } + self.horse(m, g) + } + S::Camel | S::CamelHusk => { + let now = self.level.game_time(); + m.extra.pose_change_tick = (now - 52 - 1).max(0); + super::camel::stand_up_instantly(m); + self.horse(m, g) + } + S::Goat => { + let jump = self.uniform(600, 1200); + set_memory(m, "long_jump_cooldown", i64::from(jump), i64::MAX); + let ram = self.uniform(600, 6000); + set_memory(m, "ram_cooldown", i64::from(ram), i64::MAX); + m.extra.screaming = self.random.next_double() < 0.02; + age_boundary_reached(m); + if !m.is_baby() && self.float() < 0.1 { + let left = self.random.next_boolean(); + m.extra.lost_horns[usize::from(!left)] = true; + } + self.ageable(m, g) + } + S::Panda => { + let main = panda_gene(self.int(16)); + let hidden = panda_gene(self.int(16)); + m.extra.variant = main; + m.extra.variant2 = hidden; + let shown = panda_variant(main, hidden); + if shown == 5 { + set_base(m, Attr::MaxHealth, 10.0); + } + if shown == 1 { + set_base(m, Attr::MovementSpeed, f64::from(0.07f32)); + } + self.ageable(m, g.or(Some(Group::Ageable(Ageable::chance(0.2))))) + } + S::PolarBear | S::Ocelot => { + self.ageable(m, g.or(Some(Group::Ageable(Ageable::chance(1.0))))) + } + S::Fox => { + let at = block(m); + let mut variant = u8::from(self.level.biome_tag("minecraft:spawns_snow_foxes", at)); + let mut baby = false; + let g = match g { + Some(Group::Fox(a, v)) => { + variant = v; + baby = a.size >= 2; + Group::Fox(a, v) + } + _ => Group::Fox(Ageable::babies(false), variant), + }; + m.extra.variant = variant; + if baby { + set_age(m, -24000); + } + self.populate(m); + self.ageable(m, Some(g)) + } + S::Parrot => { + m.extra.variant = self.int(5) as u8; + self.ageable(m, g.or(Some(Group::Ageable(Ageable::babies(false))))) + } + S::Axolotl => { + if reason == Reason::Bucket { + return g; + } + let mut baby = false; + let g = match g { + Some(Group::Axolotl(a, types)) => { + baby = a.size >= 2; + Group::Axolotl(a, types) + } + _ => { + let a = self.int(4) as u8; + let b = self.int(4) as u8; + Group::Axolotl(Ageable::babies(false), [a, b]) + } + }; + if let Group::Axolotl(_, types) = g { + m.extra.variant = types[self.int(2) as usize]; + } + if baby { + set_age(m, -24000); + } + self.ageable(m, Some(g)) + } + S::Squid | S::GlowSquid => { + self.ageable(m, g.or(Some(Group::Ageable(Ageable::chance(0.05))))) + } + S::Dolphin => { + m.air = 4800; + m.head_pitch = 0.0; + self.ageable(m, g.or(Some(Group::Ageable(Ageable::chance(0.1))))) + } + S::Turtle => { + m.extra.home = Some(block(m)); + self.ageable(m, g) + } + S::Villager | S::WanderingTrader => { + if s == S::Villager { + if reason == Reason::Breeding { + m.extra.variant2 = 0; + } + self.villager_type(m); + } + self.ageable(m, g.or(Some(Group::Ageable(Ageable::babies(false))))) + } + S::Hoglin => { + if self.float() < 0.2 { + set_baby(m, true); + } + self.ageable(m, g) + } + S::Zoglin => { + if self.float() < 0.2 { + set_baby(m, true); + } + self.mob(m, g) + } + S::Strider => self.strider(m, g), + S::Slime | S::MagmaCube | S::SulfurCube => { + let g = if s == S::SulfurCube { + g + } else { + g.or(Some(Group::Ageable(Ageable::babies(false)))) + }; + let g = self.ageable(m, g); + if s == S::SulfurCube { + let size = if m.is_baby() { 1 } else { 2 }; + sulfur_size(m, size); + } else { + let mut scale = self.int(3); + if scale < 2 && self.float() < 0.5 * self.special() { + scale += 1; + } + cube_size(m, 1 << scale); + } + g + } + S::Cod | S::Salmon | S::TropicalFish => { + if s == S::Salmon { + // A weighted list (small 30, medium 50, large 15) on the + // salmon's own random. + let r = m.random.next_int_bound(95).unwrap_or(0); + m.extra.variant = if r < 30 { + 0 + } else if r < 80 { + 1 + } else { + 2 + }; + } + let given = g; + self.mob(m, None); + let g = match given { + None => Group::School(m.id, None), + Some(Group::School(leader, v)) => { + m.extra.leader = Some(leader); + Group::School(leader, v) + } + Some(other) => other, + }; + if s != S::TropicalFish { + return Some(g); + } + let (g, (p, b, c)) = match g { + Group::School(leader, Some(v)) => (Group::School(leader, Some(v)), v), + other => { + if f64::from(self.float()) < 0.9 { + let v = super::aquatic::COMMON_TROPICAL[self.int(22) as usize]; + (Group::School(m.id, Some(v)), v) + } else { + m.extra.lone = true; + let p = self.int(12) as u8; + let b = self.int(16) as u8; + let c = self.int(16) as u8; + (other, (p, b, c)) + } + } + }; + m.extra.variant = p; + m.extra.variant2 = b; + m.extra.variant3 = c; + Some(g) + } + S::Zombie | S::ZombifiedPiglin => self.zombie(m, g, reason), + S::ZombieVillager => { + self.villager_type(m); + // ZombieVillager's constructor picks the profession from the + // fresh entity's random, before anything seeds it: an + // independent draw that leaves the mob's own random alone. + let mut fresh = LegacyRandom::new( + (m.random.state() as i64).wrapping_mul(0x5851_F42D_4C95_7F2D) ^ i64::from(m.id), + ); + m.extra.variant2 = fresh.next_int_bound(15).unwrap_or(0) as u8; + self.zombie(m, g, reason) + } + S::Husk => self.husk(m, g, reason), + S::Drowned => self.drowned(m, g, reason), + s if skeleton(s) => self.skeleton(m, g), + S::Spider => self.spider(m, g, reason), + // CaveSpider.finalizeSpawn returns at once. + S::CaveSpider => g, + S::Pillager => { + self.populate(m); + self.enchant(m); + self.raider(m, g, reason) + } + S::Vindicator => { + let g = self.raider(m, g, reason); + self.populate(m); + self.enchant(m); + g + } + S::Illusioner => { + set_item(m, MAINHAND, "minecraft:bow"); + self.raider(m, g, reason) + } + s if raider(s) => self.raider(m, g, reason), + S::Piglin => self.piglin(m, g, reason), + S::PiglinBrute => { + let home = block(m); + m.extra.home = Some(home); + set_memory(m, "home", 0, i64::MAX); + self.populate(m); + self.mob(m, g) + } + S::Vex => { + self.populate(m); + self.enchant(m); + self.mob(m, g) + } + S::Warden => { + super::warden::finalize(m, reason == Reason::Triggered, &mut Vec::new()); + set_memory(m, "dig_cooldown", 0, 1200); + if reason == Reason::Triggered { + set_memory(m, "is_emerging", 0, super::warden::EMERGE_DURATION); + } + self.mob(m, g) + } + S::Phantom => { + let at = block(m); + m.extra.home = Some([at[0], at[1] + 5, at[2]]); + m.extra.size = 0; + self.mob(m, g) + } + S::Shulker => { + m.yaw = 0.0; + m.head_yaw = 0.0; + m.previous_yaw = 0.0; + m.previous_head_yaw = 0.0; + self.mob(m, g) + } + _ if ageable(s) => self.ageable(m, g), + _ => self.mob(m, g), + } + } +} + +/// `Mob.finalizeSpawn` of a fresh mob. Returns the group data for the next +/// mob of the pack. +pub fn finalize( + cx: &mut Ctx<'_, L, R>, + m: &mut Mob, + reason: Reason, + g: Option, +) -> Option { + cx.finalize(m, reason, g) +} + +fn block(m: &Mob) -> [i32; 3] { + [ + m.position.x.floor() as i32, + m.position.y.floor() as i32, + m.position.z.floor() as i32, + ] +} +/// A brain memory finalizeSpawn sets: value and ticks until it expires. +fn set_memory(m: &mut Mob, name: &'static str, value: i64, ttl: i64) { + match m.extra.memories.iter_mut().find(|x| x.0 == name) { + Some(x) => *x = (name, value, ttl), + None => m.extra.memories.push((name, value, ttl)), + } +} +/// `Panda.Gene.getRandom` (enum order: normal, lazy, worried, playful, +/// brown, weak, aggressive). +fn panda_gene(roll: i32) -> u8 { + match roll { + 0 => 1, + 1 => 2, + 2 => 3, + 4 => 6, + r if r < 9 => 5, + r if r < 11 => 4, + _ => 0, + } +} +/// `Panda.Gene.getVariantFromGenes`: a recessive gene shows when both match. +pub fn panda_variant(main: u8, hidden: u8) -> u8 { + let recessive = matches!(main, 4 | 5); + if recessive && main != hidden { 0 } else { main } +} +/// `AbstractCubeMob.setSize(size, true)` for slimes and magma cubes. +/// `SulfurCube.setSize(size, true)`, with the attribute bases it sets. +fn sulfur_size(m: &mut Mob, size: i32) { + super::sulfur_cube::set_size(m, size, true); + let size = m.extra.size; + set_base(m, Attr::MaxHealth, f64::from(4 * size)); + set_base( + m, + Attr::MovementSpeed, + f64::from(0.2f32 + 0.1f32 * size as f32), + ); + m.health = max_health(m); +} +fn cube_size(m: &mut Mob, size: i32) { + let size = size.clamp(1, 127); + m.extra.size = size; + set_base(m, Attr::MaxHealth, f64::from(size * size)); + set_base( + m, + Attr::MovementSpeed, + f64::from(0.2f32 + 0.1f32 * size as f32), + ); + set_base(m, Attr::AttackDamage, f64::from(size)); + if m.species() == Species::MagmaCube { + set_base(m, Attr::Armor, f64::from(size * 3)); + } + m.health = max_health(m); +} +/// The index the game keeps for a variant registry entry. +pub fn variant_index(s: Species, name: &str) -> u8 { + let name = name.strip_prefix("minecraft:").unwrap_or(name); + let i = match s { + Species::Cat => super::tame::CAT_VARIANTS.iter().position(|v| *v == name), + Species::Wolf => super::tame::WOLF_VARIANTS.iter().position(|v| v.0 == name), + Species::ZombieNautilus => super::nautilus::VARIANTS.iter().position(|v| *v == name), + _ => ["temperate", "warm", "cold"] + .iter() + .position(|v| *v == name), + }; + i.unwrap_or(0) as u8 +} +/// The registry name of the game's variant index. +pub fn variant_name(s: Species, index: u8) -> &'static str { + let i = index as usize; + match s { + Species::Cat => super::tame::CAT_VARIANTS.get(i).copied().unwrap_or("tabby"), + Species::Wolf => super::tame::WOLF_VARIANTS.get(i).map_or("pale", |v| v.0), + Species::ZombieNautilus => super::nautilus::VARIANTS + .get(i) + .copied() + .unwrap_or("temperate"), + _ => ["temperate", "warm", "cold"] + .get(i) + .copied() + .unwrap_or("temperate"), + } +} +/// `VillagerType.byBiome` (index into `render::VILLAGER_TYPES`: plains, +/// desert, jungle, savanna, snow, swamp, taiga). +pub fn villager_type(biome: &str) -> u8 { + match biome.strip_prefix("minecraft:").unwrap_or(biome) { + "badlands" | "desert" | "eroded_badlands" | "wooded_badlands" => 1, + "bamboo_jungle" | "jungle" | "sparse_jungle" => 2, + "savanna_plateau" | "savanna" | "windswept_savanna" => 3, + "deep_frozen_ocean" | "frozen_ocean" | "frozen_river" | "ice_spikes" | "snowy_beach" + | "snowy_taiga" | "snowy_plains" | "grove" | "snowy_slopes" | "frozen_peaks" + | "jagged_peaks" => 4, + "swamp" | "mangrove_swamp" => 5, + "old_growth_spruce_taiga" + | "old_growth_pine_taiga" + | "windswept_gravelly_hills" + | "windswept_hills" + | "taiga" + | "windswept_forest" => 6, + _ => 0, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn panda_genes_follow_the_vanilla_table() { + assert_eq!(panda_gene(0), 1); + assert_eq!(panda_gene(3), 5); + assert_eq!(panda_gene(4), 6); + assert_eq!(panda_gene(9), 4); + assert_eq!(panda_gene(15), 0); + assert_eq!(panda_variant(4, 0), 0); + assert_eq!(panda_variant(4, 4), 4); + assert_eq!(panda_variant(6, 0), 6); + } + #[test] + fn reasons_round_trip() { + for r in Reason::ALL { + assert_eq!(Reason::parse(r.name()), Some(r)); + } + } + #[test] + fn villager_types_by_biome() { + assert_eq!(villager_type("minecraft:badlands"), 1); + assert_eq!(villager_type("minecraft:grove"), 4); + assert_eq!(villager_type("minecraft:plains"), 0); + } +} diff --git a/crates/horizon-client/src/mobs/living.rs b/crates/horizon-client/src/mobs/living.rs index 03d2f201f..e0a147116 100644 --- a/crates/horizon-client/src/mobs/living.rs +++ b/crates/horizon-client/src/mobs/living.rs @@ -132,6 +132,38 @@ impl MobWorld<'_> { .map(|shape| shape.boxes().iter().map(|b| b.move_by(offset)).collect()) .unwrap_or_default() } + /// `Level.findSupportingBlock`: of the blocks whose collision shape + /// meets `area`, the one whose centre is nearest `from` (ties: the + /// greatest by `Vec3i.compareTo`, y then z then x). + pub fn supporting_block(&self, area: Aabb, from: Vec3) -> Option<[i32; 3]> { + use horizon_core::geometry::Axis; + let range = |axis| { + (area.min(axis) - 1.0e-7).floor() as i32 - 1 + ..=(area.max(axis) + 1.0e-7).floor() as i32 + 1 + }; + let mut best: Option<(f64, [i32; 3])> = None; + for y in range(Axis::Y) { + for z in range(Axis::Z) { + for x in range(Axis::X) { + if !self + .block_collision_boxes(x, y, z) + .iter() + .any(|b| b.intersects(area)) + { + continue; + } + let d = (f64::from(x) + 0.5 - from.x).powi(2) + + (f64::from(y) + 0.5 - from.y).powi(2) + + (f64::from(z) + 0.5 - from.z).powi(2); + let greater = |b: [i32; 3]| (y, z, x) > (b[1], b[2], b[0]); + if best.is_none_or(|(bd, b)| d < bd || d == bd && greater(b)) { + best = Some((d, [x, y, z])); + } + } + } + } + best.map(|(_, p)| p) + } pub fn no_collision(&self, b: Aabb) -> bool { let mut scene = SceneCollisions { world: self.world, @@ -346,7 +378,8 @@ impl Mob { let full = (moved.length() * f64::from(0.6_f32)) as f32; let horizontal = ((moved.x * moved.x + moved.z * moved.z).sqrt() * f64::from(0.6_f32)) as f32; - let on = self.on_pos(1.0e-5); + let supporting = self.supporting_block(w, moved); + let on = self.supported_on_pos(w, supporting, 1.0e-5); let state = w.world.block(on[0], on[1], on[2]); let climbable = w.facts.get(state).name.as_ref() == "minecraft:powder_snow" || w.has_tag("minecraft:climbable", &w.facts.get(state).name); @@ -354,7 +387,7 @@ impl Mob { steps.move_dist += if climbable { full } else { horizontal }; steps.fly_dist += full; if steps.move_dist > steps.next_step && !w.facts.is_air(state) { - let legacy = self.on_pos(0.2); + let legacy = self.supported_on_pos(w, supporting, 0.2); let legacy_state = w.world.block(legacy[0], legacy[1], legacy[2]); let produced = !w.facts.is_air(legacy_state) && (self.on_ground || climbable) @@ -397,6 +430,68 @@ impl Mob { fn fluids_swimming(&self) -> bool { false } + /// `Entity.checkSupportingBlock` after a move: on the ground, the block + /// under the feet (`mainSupportingBlockPos`), looked for again where + /// the mob came from if none is under it now. + fn supporting_block(&self, w: &MobWorld, moved: Vec3) -> Option<[i32; 3]> { + if !self.on_ground { + return None; + } + let b = self.bounds(); + use horizon_core::geometry::Axis; + let area = Aabb::new( + Vec3::new(b.min(Axis::X), b.min(Axis::Y) - 1.0e-6, b.min(Axis::Z)), + Vec3::new(b.max(Axis::X), b.min(Axis::Y), b.max(Axis::Z)), + ); + w.supporting_block(area, self.position).or_else(|| { + let back = Vec3::new(-moved.x, 0.0, -moved.z); + let shifted = Aabb::new( + Vec3::new( + area.min(Axis::X) + back.x, + area.min(Axis::Y), + area.min(Axis::Z) + back.z, + ), + Vec3::new( + area.max(Axis::X) + back.x, + area.max(Axis::Y), + area.max(Axis::Z) + back.z, + ), + ); + w.supporting_block(shifted, self.position) + }) + } + /// `Entity.getOnPos(offset)` with a supporting block: its column (its + /// own position for fences, walls and gates up to half a block down). + fn supported_on_pos( + &self, + w: &MobWorld, + supporting: Option<[i32; 3]>, + offset: f32, + ) -> [i32; 3] { + let Some(p) = supporting else { + return self.on_pos(offset); + }; + if offset <= 1.0e-5 { + return p; + } + let name = &w.facts.get(w.world.block(p[0], p[1], p[2])).name; + let tall = [ + "minecraft:fences", + "minecraft:walls", + "minecraft:fence_gates", + ] + .iter() + .any(|t| w.has_tag(t, name)); + if offset <= 0.5 && tall { + p + } else { + [ + p[0], + (self.position.y - f64::from(offset)).floor() as i32, + p[2], + ] + } + } /// Entity.getOnPos(offset) without a supporting block record. pub fn on_pos(&self, offset: f32) -> [i32; 3] { [ diff --git a/crates/horizon-client/src/mobs/villager.rs b/crates/horizon-client/src/mobs/villager.rs index a19417098..f568cb47b 100644 --- a/crates/horizon-client/src/mobs/villager.rs +++ b/crates/horizon-client/src/mobs/villager.rs @@ -496,6 +496,144 @@ impl Ext { now < self.last_gossip_time || now >= self.last_gossip_time + 1200 } } +/// What a villager ticking later reads of another one this tick (vanilla +/// reads the other villager itself). +#[derive(Clone, Debug, Default)] +pub struct Seen { + pub gossips: crate::gossip::Gossips, + pub last_gossip_time: i64, + /// `countFoodPointsInInventory`. + pub food_points: i32, + /// The `INTERACTION_TARGET` memory (play tag's chases). + pub interaction_target: Option, + /// The `PATH` memory, when not done: its previous and next nodes. + pub path_nodes: Option<(Option<[i32; 3]>, [i32; 3])>, + /// `LAST_SLEPT` and whether `GOLEM_DETECTED_RECENTLY` is remembered. + pub last_slept: Option, + pub golem_detected: bool, +} +impl Seen { + /// `Villager.wantsToSpawnGolem`. + pub fn wants_golem(&self, now: i64) -> bool { + self.last_slept.is_some_and(|t| now - t < 24000) && !self.golem_detected + } + /// `Villager.wantsMoreFood`. + pub fn wants_more_food(&self) -> bool { + self.food_points < 12 + } +} +pub fn seen(m: &Mob) -> Seen { + let e = ext(m); + let (interaction_target, path_nodes, golem_detected, last_slept) = + super::villager_brain::seen_memories(m); + Seen { + gossips: e.map(|e| e.gossips.clone()).unwrap_or_default(), + last_gossip_time: e.map_or(0, |e| e.last_gossip_time), + food_points: e.map_or(0, Ext::food_in_inventory), + interaction_target, + path_nodes, + last_slept: last_slept.or(e.and_then(|e| e.last_slept)), + golem_detected, + } +} +/// What `TradeWithVillager.tick` hands over: stacks tossed from the first +/// villager's hand (`from`, its eye height) toward the second (`to`), and +/// whether the gossip happened (`spawnGolemIfNeeded` follows). +pub struct Meeting { + pub thrown: Vec, + pub from: horizon_core::geometry::Vec3, + pub eye: f64, + pub to: horizon_core::geometry::Vec3, + pub gossiped: bool, +} +/// `TradeWithVillager.tick` for villagers `a` and `b` (indices into `mobs`): +/// `Villager.gossip` (at most once in 1200 ticks for both), then surplus +/// food and wanted wares taken out of `a`'s inventory. +pub fn meet( + mobs: &mut [Mob], + a: usize, + b: usize, + now: i64, + max_stack: impl Fn(&str) -> u32, +) -> Meeting { + let ready = ext(&mobs[a]).is_none_or(|e| e.can_gossip(now)) + && ext(&mobs[b]).is_none_or(|e| e.can_gossip(now)); + if ready { + let source = ext(&mobs[b]).map(|e| e.gossips.clone()).unwrap_or_default(); + let me = &mut mobs[a]; + let mut random = me.random.clone(); + let e = ext_mut(me); + e.gossips.transfer_from(&source, &mut random, 10); + e.last_gossip_time = now; + me.random = random; + ext_mut(&mut mobs[b]).last_gossip_time = now; + } + // Surplus food goes to a hungry neighbour (farmers hand it to anyone). + let farmer = is_farmer(&mobs[a]); + let wants_more = ext(&mobs[b]).is_none_or(Ext::wants_more_food); + let excess = ext(&mobs[a]).is_some_and(Ext::has_excess_food); + let mut thrown = Vec::new(); + if excess + && (farmer || wants_more) + && let Some(s) = + ext_mut(&mut mobs[a]).take_half_stack(|s| food_points(&s.item) > 0, &max_stack) + { + thrown.push(s); + } + // A farmer's wheat. + if farmer + && ext(&mobs[a]).is_some_and(|e| e.count_item("minecraft:wheat") > 32) + && let Some(s) = + ext_mut(&mut mobs[a]).take_half_stack(|s| &*s.item == "minecraft:wheat", &max_stack) + { + thrown.push(s); + } + // Wares the neighbour's profession asks for and ours does not. + let theirs = requested_items(&mobs[b]); + let mine = requested_items(&mobs[a]); + let wanted: Vec<&str> = theirs + .iter() + .copied() + .filter(|i| !mine.contains(i)) + .collect(); + if !wanted.is_empty() + && let Some(s) = + ext_mut(&mut mobs[a]).take_half_stack(|s| wanted.contains(&&*s.item), &max_stack) + { + thrown.push(s); + } + Meeting { + thrown, + from: mobs[a].position, + eye: mobs[a].eye_y(), + to: mobs[b].position, + gossiped: ready, + } +} +/// `PoiManager.take(HOME, ..., 48)`: the nearest bed head not `claimed` +/// (`free_head`: a bed head that is not occupied). +pub fn take_vacant_bed( + claimed: &[[i32; 3]], + from: [i32; 3], + mut free_head: impl FnMut(i32, i32, i32) -> bool, +) -> Option<[i32; 3]> { + let mut best: Option<(i32, [i32; 3])> = None; + for y in from[1] - 8..=from[1] + 8 { + for z in from[2] - 48..=from[2] + 48 { + for x in from[0] - 48..=from[0] + 48 { + let d = (x - from[0]).pow(2) + (y - from[1]).pow(2) + (z - from[2]).pow(2); + if d <= 48 * 48 + && best.is_none_or(|(b, _)| d < b) + && free_head(x, y, z) + && !claimed.contains(&[x, y, z]) + { + best = Some((d, [x, y, z])); + } + } + } + } + best.map(|(_, p)| p) +} /// `Villager.canBreed`: enough food, awake and an adult. pub fn can_breed(m: &Mob) -> bool { m.species() == Species::Villager diff --git a/crates/horizon-client/src/mobs/villager_brain.rs b/crates/horizon-client/src/mobs/villager_brain.rs index 86dff2251..4d94ca2bb 100644 --- a/crates/horizon-client/src/mobs/villager_brain.rs +++ b/crates/horizon-client/src/mobs/villager_brain.rs @@ -663,7 +663,7 @@ fn sense_bed(m: &mut Mob, w: &MobWorld, lr: &mut LegacyRandom) { let mut cache = std::mem::take(&mut brain_mut(m).bed_cache); let mut tried = 0; let mut targets = Vec::new(); - for p in homes { + for &p in &homes { if cache.iter().any(|(q, _)| *q == p) { continue; } @@ -679,8 +679,12 @@ fn sense_bed(m: &mut Mob, w: &MobWorld, lr: &mut LegacyRandom) { } else { m.create_path_to_any(w, &targets, 1) }; + // createPath hands back the current path when it still heads for a + // target position it was asked: only a bed (a POI) counts. if let Some(path) = path.filter(|p| p.reached) { - mem(m).nearest_bed.set(path.target); + if homes.contains(&path.target) { + mem(m).nearest_bed.set(path.target); + } } else if tried < 5 { cache.retain(|(_, until)| *until >= last_update); } @@ -1609,13 +1613,7 @@ impl B { } t.begin(cx); let target = mem(m).interaction_target.value().unwrap(); - let mm = mem(m); - mm.look_target.set(Tracker::Entity(target, true)); - mm.walk_target.set(WalkTarget { - target: Tracker::Entity(target, true), - speed: 0.5, - close: 2, - }); + lock_gaze(m, target, cx); true } B::MakeLove { t, birth } => { @@ -1628,7 +1626,7 @@ impl B { } t.begin(cx); let target = mem(m).breed_target.value().unwrap(); - lock_gaze(m, target); + lock_gaze(m, target, cx); *birth = now + 275 + i64::from(m.random.next_int_bound(50).unwrap_or(0)); true } @@ -2853,6 +2851,7 @@ impl B { && i.position.distance_squared(m.position) <= 5.0 { let Who::Mob(other) = target else { return }; + lock_gaze(m, target, cx); cx.events.push(MobEvent::VillagerMeet { mob: m.id, other }); } } @@ -2870,11 +2869,29 @@ impl B { .target_info(target) .is_some_and(|t| t.position.distance_squared(m.position) <= 5.0) { - lock_gaze(m, target); + lock_gaze(m, target, cx); if now >= *birth { + // The parent's half happens in its own tick: it eats, + // takes a bed for the child, picks its type and rests. + super::villager::ext_mut(m).eat_and_digest(); + let child = take_bed_for_child(m, cx.w).map(|bed| { + // Villager.getBreedOffspring: half the biome's type, + // a quarter each parent's. + let roll = m.random.next_double(); + let variant = if roll < 0.5 { + None + } else if roll < 0.75 { + Some(m.extra.variant) + } else { + Some(cx.w.seen(partner).map_or(m.extra.variant, |s| s.variant)) + }; + m.extra.age = 6000; + (bed, variant) + }); cx.events.push(MobEvent::VillagerBirth { parent: m.id, partner, + child, }); } else if m.random.next_int_bound(35).unwrap_or(1) == 0 { let mut p = vec![m.position]; @@ -3366,8 +3383,38 @@ fn breeding_possible(m: &mut Mob, w: &MobWorld) -> bool { && super::villager::can_breed(m) && w.seen(id).is_some_and(|s| s.in_love) } -/// `BehaviorUtils.lockGazeAndWalkToEachOther`, this villager's half. -fn lock_gaze(m: &mut Mob, target: Who) { +/// `VillagerMakeLove.takeVacantBed`: a bed head no villager has claimed. +fn take_bed_for_child(m: &Mob, w: &MobWorld) -> Option<[i32; 3]> { + let claimed: Vec<[i32; 3]> = w + .others + .iter() + .filter(|s| s.species == Species::Villager && s.id != m.id) + .filter_map(|s| s.bed) + .chain(m.extra.bed) + .collect(); + let p = m.position; + let from = [p.x.floor() as i32, p.y.floor() as i32, p.z.floor() as i32]; + super::villager::take_vacant_bed(&claimed, from, |x, y, z| { + w.prop(x, y, z, "part") == Some("head") && w.prop(x, y, z, "occupied") != Some("true") + }) +} +/// `BehaviorUtils.lockGazeAndWalkToEachOther`: this villager's half, and +/// the other villager's through a `LockGaze` event. +fn lock_gaze(m: &mut Mob, target: Who, cx: &mut Bx) { + lock_gaze_half(m, target); + if let Who::Mob(other) = target { + cx.events.push(MobEvent::LockGaze { + mob: other, + partner: m.id, + }); + } +} +/// One villager's half of `lockGazeAndWalkToEachOther`: look at the other +/// and walk to it. +pub fn lock_gaze_half(m: &mut Mob, target: Who) { + if super::villager::ext(m).is_none_or(|e| e.brain.is_none()) { + return; + } let mm = mem(m); mm.look_target.set(Tracker::Entity(target, true)); mm.walk_target.set(WalkTarget { @@ -4080,3 +4127,34 @@ pub fn potential_job_site(m: &Mob) -> Option<[i32; 3]> { pub fn seen_can_breed(s: &Seen) -> bool { s.species == Species::Villager && s.in_love } +/// The memories other villagers read: the interaction target, the path's +/// nodes (while it is not done), whether a golem was seen lately and the +/// last sleep. +#[allow(clippy::type_complexity)] +pub fn seen_memories( + m: &Mob, +) -> ( + Option, + Option<(Option<[i32; 3]>, [i32; 3])>, + bool, + Option, +) { + let Some(mm) = mem_ref(m) else { + return (None, None, false, None); + }; + let path = mm + .path + .present() + .then_some(m.ai.nav.path.as_ref()) + .flatten(); + let nodes = path.filter(|p| !p.is_done()).map(|p| { + let n = |i: usize| [p.nodes[i].x, p.nodes[i].y, p.nodes[i].z]; + ((p.next > 0).then(|| n(p.next - 1)), n(p.next)) + }); + ( + mm.interaction_target.value(), + nodes, + mm.golem_detected_recently.present(), + mm.last_slept.value(), + ) +} diff --git a/crates/horizon-client/src/mobs/wither.rs b/crates/horizon-client/src/mobs/wither.rs index eda2b76c4..6702686c5 100644 --- a/crates/horizon-client/src/mobs/wither.rs +++ b/crates/horizon-client/src/mobs/wither.rs @@ -28,7 +28,6 @@ pub fn register(m: &mut Mob) { hovers: false, }; m.ai.nav.kind = NavKind::Flying; - m.persistent = true; let g = &mut m.ai.goals; g.add(0, DoNothing); g.add( diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index 1b1a21d18..673752bcb 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -639,6 +639,32 @@ fn heightmaps(chunk: &Chunk, tables: &SaveTables) -> Tag { ) })) } +/// The heightmaps a ClientboundLevelChunkPacketData carries (the types with +/// sendToClient: WORLD_SURFACE, MOTION_BLOCKING, MOTION_BLOCKING_NO_LEAVES), +/// as (network id, packed longs) in Heightmap.Types order. +pub fn client_heightmaps(chunk: &Chunk, tables: &SaveTables) -> Vec<(i32, Vec)> { + let kinds = [ + (1, HeightmapType::WorldSurface), + (4, HeightmapType::MotionBlocking), + (5, HeightmapType::MotionBlockingNoLeaves), + ]; + let mut maps: Vec<(i32, Heightmap)> = kinds + .iter() + .filter_map(|(id, k)| { + Heightmap::new(*k, MIN_Y, SECTIONS as i32 * 16) + .ok() + .map(|m| (*id, m)) + }) + .collect(); + let view = HeightmapView { chunk, tables }; + let mut refs: Vec<&mut Heightmap> = maps.iter_mut().map(|(_, m)| m).collect(); + if Heightmap::prime(&view, &mut refs).is_err() { + return Vec::new(); + } + maps.iter() + .map(|(id, m)| (*id, m.raw_data().iter().map(|&w| w as i64).collect())) + .collect() +} /// Vanilla light layer (nibbles, index y<<8|z<<4|x, low nibble first). fn nibbles(light: &Light, sky: bool) -> Vec { let mut out = vec![0u8; 2048]; diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index bad7b5332..e4a3063b3 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -341,7 +341,7 @@ impl Streaming { match done { Done::Proto(x, z, proto) => { self.generating.remove(&(x, z)); - if self.in_range(x, z, 4) { + if self.in_range(x, z, 4) || self.near_finished(x, z, 2) { self.protos.insert((x, z), Some(proto)); if !self.decorates { self.decorated.insert((x, z)); @@ -371,7 +371,7 @@ impl Streaming { self.generating.remove(&key); self.finishing.remove(&key); self.decorated.insert(key); - if self.in_range(key.0, key.1, 2) { + if self.in_range(key.0, key.1, 2) || self.near_finished(key.0, key.1, 1) { self.generated_total += 1; // A chunk saved with its light needs no light job; its // unlit neighbours still light themselves from it. @@ -435,7 +435,9 @@ impl Streaming { .chunks .keys() .copied() - .filter(|&(x, z)| !self.remote && !self.in_range(x, z, 3)) + .filter(|&(x, z)| { + !self.remote && !self.in_range(x, z, 3) && !self.near_finished(x, z, 1) + }) .collect(); for key in &far { self.decorated.remove(key); @@ -499,7 +501,9 @@ impl Streaming { .protos .iter() .filter(|(k, p)| { - p.is_some() && !self.decorated.contains(k) && self.in_range(k.0, k.1, 2) + p.is_some() + && !self.decorated.contains(k) + && (self.in_range(k.0, k.1, 2) || self.near_finished(k.0, k.1, 1)) }) .map(|(k, _)| (self.distance(k.0, k.1), k.0, k.1)) .collect(); @@ -540,17 +544,19 @@ impl Streaming { } // Generation (load or proto), closest first, bounded in flight. let margin = if self.decorates { 3 } else { 1 }; + // Neighbours a finished chunk needs generated: decorated ones need theirs. + let reach = if self.decorates { 2 } else { 1 }; if !self.remote && self.generating.len() < workers * 3 { let mut wanted = Vec::new(); let areas: Vec<((i32, i32), i32)> = std::iter::once((self.center, self.radius)) .chain(self.extra.iter().copied()) .collect(); for (center, radius) in areas { - let r = radius + margin; + let r = radius + margin.max(reach + 1); for dz in -r..=r { for dx in -r..=r { let (x, z) = (center.0 + dx, center.1 + dz); - if !self.in_range(x, z, margin) + if !(self.in_range(x, z, margin) || self.near_finished(x, z, reach)) || world.chunks.contains_key(&(x, z)) || self.generating.contains(&(x, z)) || self.protos.contains_key(&(x, z)) @@ -728,6 +734,46 @@ impl Streaming { } } } + fn ranges(&self) -> Ranges<'_> { + Ranges { + center: self.center, + radius: self.radius, + extra: &self.extra, + forced: &self.forced, + } + } + fn near_finished(&self, x: i32, z: i32, reach: i32) -> bool { + self.ranges().near_finished(x, z, reach) + } + fn in_range(&self, x: i32, z: i32, margin: i32) -> bool { + self.ranges().in_range(x, z, margin) + } + fn in_main_range(&self, x: i32, z: i32, margin: i32) -> bool { + self.ranges().in_main_range(x, z, margin) + } +} +/// The areas kept loaded: circles around the player and `extra`, and forced chunks. +struct Ranges<'a> { + center: (i32, i32), + radius: i32, + extra: &'a [((i32, i32), i32)], + forced: &'a HashSet<(i32, i32)>, +} +impl Ranges<'_> { + /// Whether a chunk finished here (`in_range(_, _, 1)`) lies within `reach` + /// chunks of (x, z) on both axes: its decoration needs its 3x3 neighbours + /// decorated, which need their own 3x3 neighbours generated. The ranges are + /// circles, so a fixed margin alone misses the corners of those squares + /// (render distance 2 never finished the chunks next to the player). + fn near_finished(&self, x: i32, z: i32, reach: i32) -> bool { + let near = |c: (i32, i32), radius: i32| { + let gap = |d: i32| i64::from((d.abs() - reach).max(0)); + let (dx, dz) = (gap(x - c.0), gap(z - c.1)); + let r = i64::from(radius + 1); + dx * dx + dz * dz <= r * r + }; + near(self.center, self.radius) || self.extra.iter().any(|&(c, r)| near(c, r)) + } fn in_range(&self, x: i32, z: i32, margin: i32) -> bool { self.in_main_range(x, z, margin) || self.extra.iter().any(|&(c, radius)| { @@ -757,3 +803,63 @@ impl Drop for Streaming { } } } +#[cfg(test)] +mod tests { + use super::*; + + /// The ranges `Streaming::update` checks at each step, from generation to + /// light, must let every chunk the player spawns on finish and light: the + /// 3x3 columns around the player need their own 3x3 neighbours finished, + /// each of those its 3x3 neighbours decorated, and each of those its 3x3 + /// neighbours generated and kept. Render distance 2 once stopped short. + #[test] + fn spawn_neighbourhood_completes_at_every_distance() { + let forced = HashSet::new(); + for decorates in [true, false] { + let (margin, reach) = if decorates { (3, 2) } else { (1, 1) }; + for radius in 2..=32 { + let r = Ranges { + center: (5, -7), + radius, + extra: &[], + forced: &forced, + }; + let square = |x: i32, z: i32, n: i32| { + (-n..=n).flat_map(move |dz| (-n..=n).map(move |dx| (x + dx, z + dz))) + }; + let generated = |x, z| { + (r.in_range(x, z, margin) || r.near_finished(x, z, reach)) + && (x - r.center.0).abs() <= radius + margin.max(reach + 1) + && (z - r.center.1).abs() <= radius + margin.max(reach + 1) + }; + let proto_kept = |x, z| r.in_range(x, z, 4) || r.near_finished(x, z, 2); + let decorated = + |x, z| !decorates || r.in_range(x, z, 2) || r.near_finished(x, z, 1); + let chunk_kept = |x, z| r.in_range(x, z, 2) || r.near_finished(x, z, 1); + let unloaded = |x, z| !r.in_range(x, z, 3) && !r.near_finished(x, z, 1); + let finishes = |x: i32, z: i32| { + r.in_range(x, z, 1) + && chunk_kept(x, z) + && !unloaded(x, z) + && square(x, z, 1).all(|(nx, nz)| { + decorated(nx, nz) + && (!decorates + || square(nx, nz, 1) + .all(|(gx, gz)| generated(gx, gz) && proto_kept(gx, gz))) + && generated(nx, nz) + && proto_kept(nx, nz) + }) + }; + let (cx, cz) = r.center; + for (x, z) in square(cx, cz, 1) { + for (nx, nz) in square(x, z, 1) { + assert!( + finishes(nx, nz), + "radius {radius} (decorates: {decorates}): ({nx}, {nz}) never finishes" + ); + } + } + } + } + } +} diff --git a/crates/horizon-client/tests/lan_bot.rs b/crates/horizon-client/tests/lan_bot.rs index 49cec6509..7a4a4a3c4 100644 --- a/crates/horizon-client/tests/lan_bot.rs +++ b/crates/horizon-client/tests/lan_bot.rs @@ -302,7 +302,8 @@ fn status_and_join() { }); let v: serde_json::Value = serde_json::from_str(&json).unwrap(); assert_eq!(v["version"]["protocol"], 777); - assert_eq!(v["description"]["text"], "Test - World"); + // ServerStatus.CODEC writes a plain literal MOTD as a bare string. + assert_eq!(v["description"], "Test - World"); // Join, receive the world, place a block, see another player arrive. let report = run(&mut s, move || { diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaFinalizeSpawnOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaFinalizeSpawnOracle.java new file mode 100644 index 000000000..fa520e409 --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaFinalizeSpawnOracle.java @@ -0,0 +1,527 @@ +package dev.horizonlab.parity; + +import java.io.PrintWriter; +import java.lang.reflect.*; +import java.nio.charset.StandardCharsets; +import java.nio.file.*; +import java.util.*; +import java.util.function.Predicate; +import net.minecraft.SharedConstants; +import net.minecraft.core.*; +import net.minecraft.core.component.DataComponents; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.Identifier; +import net.minecraft.resources.RegistryDataLoader; +import net.minecraft.resources.ResourceKey; +import net.minecraft.server.Bootstrap; +import net.minecraft.server.RegistryLayer; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.packs.PackType; +import net.minecraft.server.packs.repository.ServerPacksSource; +import net.minecraft.server.packs.resources.MultiPackResourceManager; +import net.minecraft.sounds.SoundEvent; +import net.minecraft.sounds.SoundSource; +import net.minecraft.tags.TagLoader; +import net.minecraft.util.RandomSource; +import net.minecraft.world.Difficulty; +import net.minecraft.world.DifficultyInstance; +import net.minecraft.world.effect.MobEffectInstance; +import net.minecraft.world.entity.*; +import net.minecraft.world.entity.ai.Brain; +import net.minecraft.world.entity.ai.attributes.Attribute; +import net.minecraft.world.entity.ai.attributes.AttributeInstance; +import net.minecraft.world.entity.ai.attributes.Attributes; +import net.minecraft.world.entity.ai.memory.MemoryModuleType; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.enchantment.ItemEnchantments; +import net.minecraft.world.level.*; +import net.minecraft.world.level.biome.Biome; +import net.minecraft.world.level.biome.BiomeManager; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.border.WorldBorder; +import net.minecraft.world.level.dimension.DimensionType; +import net.minecraft.world.level.entity.EntityTypeTest; +import net.minecraft.world.level.gamerules.GameRules; +import net.minecraft.world.level.levelgen.structure.BoundingBox; +import net.minecraft.world.level.levelgen.structure.Structure; +import net.minecraft.world.level.levelgen.structure.StructurePiece; +import net.minecraft.world.level.levelgen.structure.StructureStart; +import net.minecraft.world.level.levelgen.structure.pieces.PiecesContainer; +import net.minecraft.world.level.material.FluidState; +import net.minecraft.world.phys.AABB; +import net.minecraft.world.phys.Vec3; + +/** + * Original oracle: Mob.finalizeSpawn as vanilla runs it, for every mob type, + * spawn reason, difficulty and local difficulty, on a stand-in ServerLevel + * (one biome, an optional structure piece, a floor and an optional roof, + * chickens standing by, the moon phase). + * + * Each case seeds the level random (what finalizeSpawn reads through + * level.getRandom()) and the entities' own randoms: every entity created + * in the case, the case's mobs and the jockeys finalizeSpawn makes, gets + * a LegacyRandomSource seeded eseed + its creation index, swapped in + * through each EntityType's factory. A pack of mobs is finalized in turn + * with the group data passed along, as NaturalSpawner does, each added to + * the level with its passengers. + * + * The log lists every entity of the case in creation order (equipment and + * enchantments, drop chances, CanPickUpLoot, LeftHanded, persistence, age, + * health, attribute values, effects, the species' variant state and spawn + * memories, its vehicle) and the next draws of the level random and of each + * entity's random, which pin every draw in between. + */ +public final class VanillaFinalizeSpawnOracle { + static Object unsafe; + static Method allocate, putObject, objectFieldOffset; + + static Object alloc(Class c) throws Exception { return allocate.invoke(unsafe, c); } + + static void put(Object target, Class owner, String field, Object value) throws Exception { + Field f = owner.getDeclaredField(field); + putObject.invoke(unsafe, target, (long) objectFieldOffset.invoke(unsafe, f), value); + } + + static Object get(Object target, Class owner, String field) { + try { + Field f = owner.getDeclaredField(field); + f.setAccessible(true); + return f.get(target); + } catch (Exception e) { throw new RuntimeException(e); } + } + + static final class Lvl extends ServerLevel { + Lvl() { super(null, null, null, null, null, null, false, 0L, List.of(), false); } + RegistryAccess registries; + net.minecraft.server.MinecraftServer fake; + BlockState floor; + int floorY, roofY; + Holder biome; + Difficulty difficulty; + DimensionType dimensionType; + ResourceKey dimensionKey; + List entities = new ArrayList<>(); + net.minecraft.world.attribute.EnvironmentAttributeSystem attributes; + BiomeManager biomeManager; + WorldBorder border = new WorldBorder(); + GameRules rules; + Structures structures; + RandomSource levelRandom; + long time; + int next = 1; + @Override public net.minecraft.server.MinecraftServer getServer() { return fake; } + @Override public RegistryAccess registryAccess() { return registries; } + @Override public Difficulty getDifficulty() { return difficulty; } + @Override public long getGameTime() { return time; } + @Override public int getNextEntityId() { return next++; } + @Override public net.minecraft.world.flag.FeatureFlagSet enabledFeatures() { return net.minecraft.world.flag.FeatureFlags.REGISTRY.allFlags(); } + @Override public BlockState getBlockState(BlockPos pos) { + if (pos.getY() == floorY) return floor; + if (pos.getY() == roofY) return Blocks.STONE.defaultBlockState(); + return Blocks.AIR.defaultBlockState(); + } + @Override public FluidState getFluidState(BlockPos pos) { return getBlockState(pos).getFluidState(); } + @Override public DimensionType dimensionType() { return dimensionType; } + @Override public ResourceKey dimension() { return dimensionKey; } + @Override public Holder getBiome(BlockPos pos) { return biome; } + @Override public BiomeManager getBiomeManager() { return biomeManager; } + @Override public net.minecraft.world.attribute.EnvironmentAttributeSystem environmentAttributes() { return attributes; } + @Override public WorldBorder getWorldBorder() { return border; } + @Override public GameRules getGameRules() { return rules; } + @Override public RandomSource getRandom() { return levelRandom; } + @Override public int getSeaLevel() { return 63; } + net.minecraft.server.ServerScoreboard scoreboard; + @Override public net.minecraft.server.ServerScoreboard getScoreboard() { return scoreboard; } + @Override public net.minecraft.world.level.StructureManager structureManager() { return structures; } + @Override public BlockGetter getChunkForCollisions(int chunkX, int chunkZ) { return this; } + @Override public boolean isSpawningMonsters() { return rules.get(GameRules.SPAWN_MOBS) && rules.get(GameRules.SPAWN_MONSTERS); } + @Override public void playSeededSound(net.minecraft.world.entity.Entity except, double x, double y, double z, Holder sound, SoundSource source, float volume, float pitch, long seed) {} + @Override public void playSeededSound(net.minecraft.world.entity.Entity except, net.minecraft.world.entity.Entity sourceEntity, Holder sound, SoundSource source, float volume, float pitch, long seed) {} + @Override public void gameEvent(Holder gameEvent, Vec3 position, net.minecraft.world.level.gameevent.GameEvent.Context context) {} + @Override public void broadcastEntityEvent(Entity entity, byte event) {} + @SuppressWarnings({"unchecked", "rawtypes"}) + @Override public List players() { return List.of(); } + @Override public Iterable getAllEntities() { return entities; } + @Override public List getEntities(Entity except, AABB bb, Predicate selector) { + List out = new ArrayList<>(); + for (Entity e : entities) if (e != except && e.getBoundingBox().intersects(bb) && selector.test(e)) out.add(e); + return out; + } + @Override public List getEntities(EntityTypeTest type, AABB bb, Predicate selector) { + List out = new ArrayList<>(); + for (Entity e : entities) { + T t = type.tryCast(e); + if (t != null && e.getBoundingBox().intersects(bb) && selector.test(t)) out.add(t); + } + return out; + } + @Override public boolean addFreshEntity(Entity entity) { if (!entities.contains(entity)) entities.add(entity); return true; } + @Override public void addFreshEntityWithPassengers(Entity entity) { entity.getSelfAndPassengers().forEach(this::addFreshEntity); } + @Override public Player getNearestPlayer(double x, double y, double z, double maxDist, boolean filter) { return null; } + } + + /** One structure start whose single piece covers a box around the spawn point. */ + static final class Structures extends net.minecraft.world.level.StructureManager { + Structures() { super(null, null, null); } + Lvl level; + Structure structure; + StructureStart start; + @Override public List startsForStructure(int sectionX, int sectionZ, Predicate matcher) { + return start != null && matcher.test(structure) ? List.of(start) : List.of(); + } + @Override public List startsForStructure(int sectionX, int sectionZ, Structure s) { + return start != null && s == structure ? List.of(start) : List.of(); + } + @Override public RegistryAccess registryAccess() { return level.registries; } + } + + static final class Piece extends StructurePiece { + Piece(BoundingBox box) { super(null, 0, box); } + @Override protected void addAdditionalSaveData(net.minecraft.world.level.levelgen.structure.pieces.StructurePieceSerializationContext context, net.minecraft.nbt.CompoundTag tag) {} + @Override public void postProcess(WorldGenLevel level, net.minecraft.world.level.StructureManager structureManager, net.minecraft.world.level.chunk.ChunkGenerator generator, RandomSource random, + BoundingBox chunkBB, ChunkPos chunkPos, BlockPos referencePos) {} + } + + /** Entities created in the current case, and the seed base of their randoms. */ + static List created = new ArrayList<>(); + static long entitySeed; + static boolean seeding; + static int otherIds; + + @SuppressWarnings({"unchecked", "rawtypes"}) + static void wrapFactories() throws Exception { + Field factory = EntityType.class.getDeclaredField("factory"); + long offset = (long) objectFieldOffset.invoke(unsafe, factory); + factory.setAccessible(true); + for (EntityType type : BuiltInRegistries.ENTITY_TYPE) { + EntityType.EntityFactory original = (EntityType.EntityFactory) factory.get(type); + EntityType.EntityFactory wrapped = (t, level) -> { + Entity e = original.create(t, level); + if (e != null && !seeding) e.setId(1000000 + otherIds++); + if (e != null && seeding) { + try { + put(e, Entity.class, "random", RandomSource.create(entitySeed + created.size())); + } catch (Exception ex) { throw new RuntimeException(ex); } + created.add(e); + e.setId(created.size()); + } + return e; + }; + putObject.invoke(unsafe, type, offset, wrapped); + } + } + + static String hex(double v) { return Long.toHexString(Double.doubleToRawLongBits(v)); } + static String hex(float v) { return Integer.toHexString(Float.floatToRawIntBits(v)); } + static String id(Holder h) { return h.unwrapKey().map(k -> k.identifier().toString()).orElse("?"); } + + static final EquipmentSlot[] SLOTS = {EquipmentSlot.MAINHAND, EquipmentSlot.OFFHAND, EquipmentSlot.FEET, EquipmentSlot.LEGS, + EquipmentSlot.CHEST, EquipmentSlot.HEAD, EquipmentSlot.BODY, EquipmentSlot.SADDLE}; + + static String stack(ItemStack s) { + StringBuilder b = new StringBuilder(BuiltInRegistries.ITEM.getKey(s.getItem()).toString()); + if (s.getCount() != 1) b.append('x').append(s.getCount()); + ItemEnchantments e = s.getOrDefault(DataComponents.ENCHANTMENTS, ItemEnchantments.EMPTY); + if (!e.isEmpty()) { + List list = new ArrayList<>(); + for (var entry : e.entrySet()) list.add(id(entry.getKey()) + "=" + entry.getIntValue()); + list.sort(null); + b.append('[').append(String.join(",", list)).append(']'); + } + // Components besides enchantments (the ominous banner's). + int other = s.getComponentsPatch().forget(t -> t == DataComponents.ENCHANTMENTS).size(); + if (other > 0) b.append('*').append(other); + return b.toString(); + } + + /** Loaded after the bootstrap. */ + static final class Lists { + static final List>> ATTRIBUTES = List.of( + Map.entry("max_health", Attributes.MAX_HEALTH), Map.entry("movement_speed", Attributes.MOVEMENT_SPEED), + Map.entry("follow_range", Attributes.FOLLOW_RANGE), Map.entry("attack_damage", Attributes.ATTACK_DAMAGE), + Map.entry("knockback_resistance", Attributes.KNOCKBACK_RESISTANCE), Map.entry("spawn_reinforcements", Attributes.SPAWN_REINFORCEMENTS_CHANCE), + Map.entry("jump_strength", Attributes.JUMP_STRENGTH)); + + static final List>> MEMORIES = List.of( + Map.entry("long_jump_cooldown", MemoryModuleType.LONG_JUMP_COOLDOWN_TICKS), Map.entry("ram_cooldown", MemoryModuleType.RAM_COOLDOWN_TICKS), + Map.entry("attack_target_cooldown", MemoryModuleType.ATTACK_TARGET_COOLDOWN), Map.entry("hunted_recently", MemoryModuleType.HUNTED_RECENTLY), + Map.entry("dig_cooldown", MemoryModuleType.DIG_COOLDOWN), Map.entry("is_emerging", MemoryModuleType.IS_EMERGING), + Map.entry("home", MemoryModuleType.HOME)); + } + + /** The species state finalizeSpawn sets. */ + static void variant(Entity e, List v) { + if (e instanceof net.minecraft.world.entity.animal.pig.Pig p) { + v.add(id(p.getVariant())); + v.add(id((Holder) data(p, p.getClass(), "DATA_SOUND_VARIANT_ID"))); + } else if (e instanceof net.minecraft.world.entity.animal.cow.Cow c) { + v.add(id(c.getVariant())); + v.add(id((Holder) data(c, net.minecraft.world.entity.animal.cow.Cow.class, "DATA_SOUND_VARIANT_ID"))); + } else if (e instanceof net.minecraft.world.entity.animal.cow.MushroomCow m) { + v.add(m.getVariant().getSerializedName()); + } else if (e instanceof net.minecraft.world.entity.animal.chicken.Chicken c) { + v.add(id(c.getVariant())); + v.add(id((Holder) data(c, c.getClass(), "DATA_SOUND_VARIANT_ID"))); + v.add("jockey=" + c.isChickenJockey()); + } else if (e instanceof net.minecraft.world.entity.animal.feline.Cat c) { + v.add(id(c.getVariant())); + v.add(id((Holder) data(c, c.getClass(), "DATA_SOUND_VARIANT_ID"))); + } else if (e instanceof net.minecraft.world.entity.animal.wolf.Wolf w) { + v.add(id((Holder) call(w, net.minecraft.world.entity.animal.wolf.Wolf.class, "getVariant"))); + v.add(id((Holder) data(w, w.getClass(), "DATA_SOUND_VARIANT_ID"))); + } else if (e instanceof net.minecraft.world.entity.animal.frog.Frog f) { + v.add(id(f.getVariant())); + } else if (e instanceof net.minecraft.world.entity.animal.nautilus.ZombieNautilus n) { + v.add(id(n.getVariant())); + } else if (e instanceof net.minecraft.world.entity.animal.sheep.Sheep s) { + v.add(s.getColor().getSerializedName()); + } else if (e instanceof net.minecraft.world.entity.animal.equine.Horse h) { + v.add(h.getVariant().getSerializedName() + "/" + h.getMarkings().name().toLowerCase(Locale.ROOT)); + } else if (e instanceof net.minecraft.world.entity.animal.equine.Llama l) { + v.add(l.getVariant().getSerializedName() + "/" + l.getStrength()); + } else if (e instanceof net.minecraft.world.entity.animal.equine.SkeletonHorse h) { + v.add("trap=" + h.isTrap()); + } else if (e instanceof net.minecraft.world.entity.animal.fox.Fox f) { + v.add(f.getVariant().getSerializedName()); + } else if (e instanceof net.minecraft.world.entity.animal.rabbit.Rabbit r) { + v.add(r.getVariant().getSerializedName()); + } else if (e instanceof net.minecraft.world.entity.animal.parrot.Parrot p) { + v.add(p.getVariant().getSerializedName()); + } else if (e instanceof net.minecraft.world.entity.animal.axolotl.Axolotl a) { + v.add(a.getVariant().getSerializedName()); + } else if (e instanceof net.minecraft.world.entity.animal.fish.TropicalFish f) { + v.add(String.valueOf(call(f, net.minecraft.world.entity.animal.fish.TropicalFish.class, "getPackedVariant"))); + } else if (e instanceof net.minecraft.world.entity.animal.fish.Salmon s) { + v.add(s.getVariant().getSerializedName()); + } else if (e instanceof net.minecraft.world.entity.animal.panda.Panda p) { + v.add(p.getMainGene().getSerializedName() + "/" + p.getHiddenGene().getSerializedName()); + } else if (e instanceof net.minecraft.world.entity.animal.goat.Goat g) { + v.add("scream=" + g.isScreamingGoat() + " horns=" + g.hasLeftHorn() + "," + g.hasRightHorn()); + } else if (e instanceof net.minecraft.world.entity.monster.cubemob.AbstractCubeMob s) { + v.add("size=" + s.getSize()); + } else if (e instanceof net.minecraft.world.entity.npc.villager.Villager vi) { + v.add(id(vi.getVillagerData().type()) + "/" + id(vi.getVillagerData().profession()) + "/" + vi.getVillagerData().level()); + } else if (e instanceof net.minecraft.world.entity.monster.zombie.ZombieVillager z) { + // The profession comes from the constructor's unseeded random. + v.add(id(z.getVillagerData().type()) + "/" + z.getVillagerData().level()); + } else if (e instanceof net.minecraft.world.entity.monster.Phantom p) { + v.add("size=" + p.getPhantomSize()); + } else if (e instanceof net.minecraft.world.entity.animal.turtle.Turtle t) { + BlockPos h = (BlockPos) get(t, net.minecraft.world.entity.animal.turtle.Turtle.class, "homePos"); + v.add("home=" + (h == null ? "-" : h.getX() + "," + h.getY() + "," + h.getZ())); + } else if (e instanceof net.minecraft.world.entity.animal.dolphin.Dolphin d) { + v.add("air=" + d.getAirSupply()); + } + if (e instanceof net.minecraft.world.entity.monster.zombie.Zombie z) v.add("doors=" + z.canBreakDoors()); + if (e instanceof net.minecraft.world.entity.monster.PatrollingMonster p) v.add("leader=" + p.isPatrolLeader() + " patrolling=" + call(p, net.minecraft.world.entity.monster.PatrollingMonster.class, "isPatrolling")); + if (e instanceof net.minecraft.world.entity.raid.Raider r) v.add("raid=" + r.canJoinRaid()); + if (e instanceof net.minecraft.world.entity.monster.Strider s) v.add("saddled=" + s.isSaddled()); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + static Object data(Entity e, Class owner, String name) { + return e.getEntityData().get((net.minecraft.network.syncher.EntityDataAccessor) fieldValue(owner, name)); + } + + static Object call(Object target, Class owner, String name) { + try { + Method m = owner.getDeclaredMethod(name); + m.setAccessible(true); + return m.invoke(target); + } catch (Exception ex) { throw new RuntimeException(ex); } + } + + static Object fieldValue(Class owner, String name) { + try { + Field f = owner.getDeclaredField(name); + f.setAccessible(true); + return f.get(null); + } catch (Exception ex) { throw new RuntimeException(ex); } + } + + static String describe(Entity e, List all) { + List t = new ArrayList<>(); + t.add(BuiltInRegistries.ENTITY_TYPE.getKey(e.getType()).toString()); + Entity vehicle = e.getVehicle(); + t.add("veh=" + (vehicle == null ? "-" : Integer.toString(all.indexOf(vehicle)))); + if (e instanceof AgeableMob a) t.add("age=" + a.getAge()); + else if (e instanceof LivingEntity l) t.add("baby=" + l.isBaby()); + if (e instanceof LivingEntity l) t.add("hp=" + hex(l.getHealth())); + if (e instanceof Mob m) { + t.add("lh=" + m.isLeftHanded()); + t.add("loot=" + m.canPickUpLoot()); + t.add("persist=" + m.isPersistenceRequired()); + List eq = new ArrayList<>(); + for (EquipmentSlot s : SLOTS) { + ItemStack st = m.getItemBySlot(s); + if (!st.isEmpty()) eq.add(s.getSerializedName() + ":" + stack(st)); + } + t.add("eq=" + (eq.isEmpty() ? "-" : String.join(";", eq))); + List drops = new ArrayList<>(); + for (EquipmentSlot s : SLOTS) { + float d = m.getDropChances().byEquipment(s); + if (d != 0.085F) drops.add(s.getSerializedName() + ":" + hex(d)); + } + t.add("drop=" + (drops.isEmpty() ? "-" : String.join(";", drops))); + } + if (e instanceof LivingEntity l) { + List attrs = new ArrayList<>(); + for (var a : Lists.ATTRIBUTES) { + AttributeInstance inst = l.getAttribute(a.getValue()); + if (inst != null) attrs.add(a.getKey() + ":" + hex(inst.getValue())); + } + t.add("a=" + String.join(",", attrs)); + List fx = new ArrayList<>(); + for (MobEffectInstance i : l.getActiveEffects()) fx.add(id(i.getEffect()) + ":" + i.getAmplifier() + ":" + i.getDuration()); + fx.sort(null); + t.add("fx=" + (fx.isEmpty() ? "-" : String.join(",", fx))); + Brain brain = l.getBrain(); + List mem = new ArrayList<>(); + for (var m : Lists.MEMORIES) { + if (!brain.hasMemoryValue(m.getValue())) continue; + Object value = brain.getMemory(m.getValue()).orElse(null); + String text = value instanceof GlobalPos g ? g.pos().getX() + "," + g.pos().getY() + "," + g.pos().getZ() : String.valueOf(value); + mem.add(m.getKey() + ":" + text + ":" + brain.getTimeUntilExpiry(m.getValue())); + } + if (!mem.isEmpty()) t.add("mem=" + String.join(",", mem)); + } + List v = new ArrayList<>(); + variant(e, v); + if (!v.isEmpty()) t.add("v=" + String.join(" ", v).replace(' ', '_')); + return String.join(" ", t); + } + + public static void main(String[] args) throws Exception { + SharedConstants.tryDetectVersion(); + Bootstrap.bootStrap(); + var unsafeField = Class.forName("sun.misc.Unsafe").getDeclaredField("theUnsafe"); + unsafeField.setAccessible(true); + unsafe = unsafeField.get(null); + allocate = unsafe.getClass().getMethod("allocateInstance", Class.class); + putObject = unsafe.getClass().getMethod("putObject", Object.class, long.class, Object.class); + objectFieldOffset = unsafe.getClass().getMethod("objectFieldOffset", Field.class); + var repo = ServerPacksSource.createVanillaTrustedRepository(); + repo.reload(); + repo.setSelected(List.of("vanilla")); + var resources = new MultiPackResourceManager(PackType.SERVER_DATA, repo.openAllSelected()); + var layers = RegistryLayer.createRegistryAccess(); + var staticTags = TagLoader.loadTagsForExistingRegistries(resources, layers.getLayer(RegistryLayer.STATIC)); + var context = TagLoader.buildUpdatedLookups(layers.getAccessForLoading(RegistryLayer.WORLD), staticTags); + RegistryAccess.Frozen world = RegistryDataLoader.load(resources, context, RegistryDataLoader.WORLD_REGISTRIES, Runnable::run).join(); + var worldLayers = layers.replaceFrom(RegistryLayer.WORLD, world); + var loaded = net.minecraft.server.ReloadableServerRegistries.reload(worldLayers, staticTags, resources, Runnable::run).join(); + HolderLookup.Provider lookup = loaded.lookupWithUpdatedTags(); + var components = BuiltInRegistries.DATA_COMPONENT_INITIALIZERS.build(lookup); + staticTags.forEach(Registry.PendingTags::apply); + components.forEach(net.minecraft.core.component.DataComponentInitializers.PendingComponents::apply); + RegistryAccess access = loaded.layers().compositeAccess(); + var biomes = access.lookupOrThrow(Registries.BIOME); + var dimensions = access.lookupOrThrow(Registries.DIMENSION_TYPE); + var blockLookup = access.lookupOrThrow(Registries.BLOCK); + var structureLookup = access.lookupOrThrow(Registries.STRUCTURE); + + var server = (net.minecraft.server.MinecraftServer) alloc(net.minecraft.server.dedicated.DedicatedServer.class); + put(server, net.minecraft.server.MinecraftServer.class, "debugSubscribers", alloc(net.minecraft.util.debug.ServerDebugSubscribers.class)); + Lvl level = (Lvl) alloc(Lvl.class); + level.fake = server; + put(level, ServerLevel.class, "server", server); + level.registries = access; + // Level.playSound draws a sound seed before the (ignored) seeded sound. + put(level, net.minecraft.world.level.Level.class, "soundSeedGenerator", RandomSource.create(0L)); + level.entities = new ArrayList<>(); + level.scoreboard = new net.minecraft.server.ServerScoreboard(server); + level.border = new WorldBorder(); + level.rules = new GameRules(net.minecraft.world.flag.FeatureFlags.REGISTRY.allFlags()); + level.biomeManager = new BiomeManager((x, y, z) -> level.biome, 0L) { + @Override public Holder getBiome(BlockPos pos) { return level.biome; } + @Override public Holder getBiome(int x, int y, int z) { return level.biome; } + }; + Structures structures = (Structures) alloc(Structures.class); + structures.level = level; + level.structures = structures; + wrapFactories(); + + List lines = Files.readAllLines(Path.of(args[0])); + try (var w = new PrintWriter(Files.newBufferedWriter(Path.of(args[1]), StandardCharsets.UTF_8))) { + w.println("halloween " + net.minecraft.util.SpecialDates.isHalloween()); + for (String raw : lines) { + if (raw.isBlank()) continue; + Map c = new LinkedHashMap<>(); + for (String token : raw.trim().split("\\s+")) { + int eq = token.indexOf('='); + if (eq < 0) c.put("name", token); else c.put(token.substring(0, eq), token.substring(eq + 1)); + } + try { + level.dimensionKey = ResourceKey.create(Registries.DIMENSION, Identifier.parse(c.getOrDefault("dim", "minecraft:overworld"))); + level.dimensionType = dimensions.getOrThrow(ResourceKey.create(Registries.DIMENSION_TYPE, level.dimensionKey.identifier())).value(); + level.biome = biomes.getOrThrow(ResourceKey.create(Registries.BIOME, Identifier.parse(c.getOrDefault("biome", "minecraft:plains")))); + level.difficulty = Difficulty.byName(c.getOrDefault("difficulty", "normal")); + level.time = Long.parseLong(c.getOrDefault("time", "0")); + level.rules.set(GameRules.SPAWN_MONSTERS, !c.getOrDefault("monsters", "1").equals("0"), null); + int phase = Integer.parseInt(c.getOrDefault("phase", "0")); + level.attributes = net.minecraft.world.attribute.EnvironmentAttributeSystem.builder().addStaticLayers(level) + .addConstantLayer(net.minecraft.world.attribute.EnvironmentAttributeMap.builder() + .set(net.minecraft.world.attribute.EnvironmentAttributes.MOON_PHASE, MoonPhase.values()[phase]).build()) + .build(); + String[] p = c.get("pos").split(","); + double x = Double.parseDouble(p[0]), y = Double.parseDouble(p[1]), z = Double.parseDouble(p[2]); + BlockPos block = BlockPos.containing(x, y, z); + level.floorY = block.getY() - 1; + level.floor = net.minecraft.commands.arguments.blocks.BlockStateParser.parseForBlock(blockLookup, c.getOrDefault("floor", "minecraft:grass_block"), false).blockState(); + level.roofY = c.containsKey("roof") ? block.getY() + Integer.parseInt(c.get("roof")) : Integer.MIN_VALUE; + structures.start = null; + if (c.containsKey("structure")) { + structures.structure = structureLookup.getValue(Identifier.parse(c.get("structure"))); + BoundingBox box = new BoundingBox(block.getX() - 8, block.getY() - 8, block.getZ() - 8, block.getX() + 8, block.getY() + 8, block.getZ() + 8); + structures.start = new StructureStart(structures.structure, new ChunkPos(block.getX() >> 4, block.getZ() >> 4), 0, + new PiecesContainer(List.of(new Piece(box)))); + } + level.entities.clear(); + level.levelRandom = RandomSource.create(Long.parseLong(c.getOrDefault("seed", "0"))); + put(level, Level.class, "random", level.levelRandom); + created = new ArrayList<>(); + entitySeed = Long.parseLong(c.getOrDefault("eseed", "0")); + seeding = true; + // Chickens already standing by (zombie jockeys look for one). + int chickens = Integer.parseInt(c.getOrDefault("chickens", "0")); + for (int i = 0; i < chickens; i++) { + Entity chicken = EntityTypes.CHICKEN.create(level, EntitySpawnReason.LOAD); + chicken.setPos(x + i, y, z); + level.entities.add(chicken); + } + EntityType type = BuiltInRegistries.ENTITY_TYPE.getValue(Identifier.parse(c.get("type"))); + EntitySpawnReason reason = EntitySpawnReason.valueOf(c.getOrDefault("reason", "NATURAL")); + float moon = DimensionType.MOON_BRIGHTNESS_PER_PHASE[phase]; + DifficultyInstance difficulty = new DifficultyInstance(level.difficulty, level.time, Long.parseLong(c.getOrDefault("inhabited", "0")), moon); + int pack = Integer.parseInt(c.getOrDefault("pack", "1")); + SpawnGroupData group = null; + for (int i = 0; i < pack; i++) { + Entity e = type.create(level, reason); + if (e == null) break; // monsters do not exist in peaceful + if (!(e instanceof Mob mob)) throw new IllegalStateException("not a mob"); + mob.snapTo(x, y, z, 0.0F, 0.0F); + group = mob.finalizeSpawn(level, difficulty, reason, group); + level.addFreshEntityWithPassengers(mob); + } + seeding = false; + List out = new ArrayList<>(); + for (Entity e : created) out.add(describe(e, created)); + for (Entity e : created) out.add("rng " + e.getRandom().nextLong()); + out.add("next " + level.levelRandom.nextLong()); + w.println(c.get("name") + " " + String.join(" | ", out)); + } catch (Throwable t) { + seeding = false; + Throwable cause = t; + while ((cause instanceof InvocationTargetException || cause instanceof RuntimeException && cause.getCause() != null) && cause.getCause() != null) cause = cause.getCause(); + StringBuilder line = new StringBuilder(c.get("name") + " error " + cause); + if (System.getenv("ORACLE_TRACE") != null) for (var frame : cause.getStackTrace()) line.append("\n at ").append(frame); + w.println(line); + } + } + } + System.exit(0); + } +} diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaLanOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaLanOracle.java new file mode 100644 index 000000000..b7c4c1b0f --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaLanOracle.java @@ -0,0 +1,52 @@ +package dev.horizonlab.parity; + +import java.io.PrintWriter; +import java.net.InetAddress; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HexFormat; +import java.util.List; +import net.minecraft.client.server.LanServer; +import net.minecraft.client.server.LanServerDetection; +import net.minecraft.client.server.LanServerPinger; + +/** + * Vanilla's LAN world discovery for tools/test_lan_parity.py. Each input + * line is a case: `ping MOTD PORT` prints the announcement + * LanServerPinger sends (its UTF-8 bytes); `heard IP HEX` decodes a received + * datagram as LanServerDetection does and adds it to the list + * (LanServerList.addServer), printing what it parsed and the list when it + * changed; `reset` starts a new list (a new multiplayer screen). + */ +public final class VanillaLanOracle { + public static void main(String[] args) throws Exception { + List cases = Files.readAllLines(Path.of(args[0]), StandardCharsets.UTF_8); + try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(Path.of(args[1]), StandardCharsets.UTF_8))) { + LanServerDetection.LanServerList list = new LanServerDetection.LanServerList(); + for (String line : cases) { + String[] f = line.split("\t", -1); + switch (f[0]) { + case "ping" -> { + String ping = LanServerPinger.createPingString(f[1], f[2]); + out.println("ping " + HexFormat.of().formatHex(ping.getBytes(StandardCharsets.UTF_8))); + } + case "heard" -> { + byte[] bytes = HexFormat.of().parseHex(f[2]); + String received = new String(bytes, 0, bytes.length, StandardCharsets.UTF_8); + out.println("heard motd=" + LanServerPinger.parseMotd(received) + " address=" + LanServerPinger.parseAddress(received)); + list.addServer(received, InetAddress.getByName(f[1])); + List dirty = list.takeDirtyServers(); + if (dirty != null) { + StringBuilder b = new StringBuilder("list"); + for (LanServer s : dirty) b.append(" [").append(s.getMotd()).append(" @ ").append(s.getAddress()).append(']'); + out.println(b); + } + } + case "reset" -> list = new LanServerDetection.LanServerList(); + default -> throw new IllegalArgumentException(line); + } + } + } + } +} diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaMobAiOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaMobAiOracle.java index f42710e2f..6185d65f0 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaMobAiOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaMobAiOracle.java @@ -67,6 +67,8 @@ import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.AgeableMob; +import net.minecraft.world.entity.item.ItemEntity; +import net.minecraft.world.entity.npc.villager.Villager; import net.minecraft.world.entity.ai.Brain; import net.minecraft.world.entity.ai.behavior.BehaviorControl; import net.minecraft.world.entity.ai.behavior.BlockPosTracker; @@ -140,6 +142,10 @@ public final class VanillaMobAiOracle { } } static final List TRACE = new ArrayList<>(); + /** The entities a scenario ticks, in order: its mobs, then what they spawned. */ + static final List ENTS = new ArrayList<>(); + static final Map LABELS = new java.util.IdentityHashMap<>(); + static final UUID PLAYER_UUID = new UUID(0x1234L, 0x5678L); static final class MobSpec { String type; @@ -166,6 +172,12 @@ public final class VanillaMobAiOracle { String hand; List mobs = new ArrayList<>(); long levelSeed = 0; + /** The day time advances with the game time (sleeping, waking). */ + boolean daycycle; + /** Blocks changed before a given tick: (tick, block or fill line). */ + List timed = new ArrayList<>(); + /** Boxes whose block changes are printed every tick. */ + List watches = new ArrayList<>(); /** Blocks placed when the scenario starts, so their block entities start afresh and tick. */ List hearts = new ArrayList<>(); /** ServerLevel.isSpawningMonsters during the scenario (a heart spawns creakings). */ @@ -192,6 +204,7 @@ public final class VanillaMobAiOracle { case "rain" -> s.rain = true; case "thunder" -> s.thunder = true; case "levelseed" -> s.levelSeed = Long.parseLong(kv[1]); + case "daycycle" -> s.daycycle = true; case "spawning" -> s.spawning = true; default -> throw new IllegalArgumentException(line); } @@ -199,6 +212,12 @@ public final class VanillaMobAiOracle { } case "origin" -> { s.originX = Integer.parseInt(f[1]); s.originZ = Integer.parseInt(f[2]); } case "fill", "block" -> s.blocks.add(f); + case "at" -> s.timed.add(new Object[] {Integer.parseInt(f[1]), java.util.Arrays.copyOfRange(f, 2, f.length)}); + case "watch" -> { + int[] box = new int[6]; + for (int i = 0; i < 6; i++) box[i] = Integer.parseInt(f[i + 1]); + s.watches.add(box); + } case "heart" -> s.hearts.add(f); case "hurt" -> s.hurts.add(new int[] {Integer.parseInt(f[1]), Integer.parseInt(f[2])}); case "player" -> { @@ -425,8 +444,12 @@ public final class VanillaMobAiOracle { } void build(Scenario s, ServerLevel level) throws Exception { + for (String[] b : s.blocks) place(b, level); + } + + static void place(String[] b, ServerLevel level) throws Exception { var blocks = level.holderLookup(Registries.BLOCK); - for (String[] b : s.blocks) { + { if (b[0].equals("block")) { BlockState state = BlockStateParser.parseForBlock(blocks, b[4], false).blockState(); level.setBlock(new BlockPos(Integer.parseInt(b[1]), Integer.parseInt(b[2]), Integer.parseInt(b[3])), state, 2 | 16); @@ -453,7 +476,7 @@ public final class VanillaMobAiOracle { level.setThunderLevel(s.thunder ? 1.0F : 0.0F); if (s.player != null) { if (player == null) { - CommonListenerCookie cookie = CommonListenerCookie.createInitial(new GameProfile(new UUID(0x1234L, 0x5678L), "horizon-ai"), false); + CommonListenerCookie cookie = CommonListenerCookie.createInitial(new GameProfile(PLAYER_UUID, "horizon-ai"), false); player = new ServerPlayer(this, level, cookie.gameProfile(), cookie.clientInformation()); Connection connection = new Connection(PacketFlow.SERVERBOUND); new EmbeddedChannel(connection); @@ -479,14 +502,15 @@ public final class VanillaMobAiOracle { } else if (player != null) { player.snapTo(s.originX + 5000, -60, s.originZ, 0.0F, 0.0F); } - List mobs = new ArrayList<>(); + ENTS.clear(); + LABELS.clear(); List> brains = new ArrayList<>(); List seeds = new ArrayList<>(); int id = 100000; for (MobSpec spec : s.mobs) { EntityType type = BuiltInRegistries.ENTITY_TYPE.getValue(net.minecraft.resources.Identifier.parse(spec.type)); Mob mob = (Mob) type.create(level, EntitySpawnReason.COMMAND); - if (tracing(s)) field(Entity.class, "random").set(mob, new TracingRandom(0L, TRACE, mobs.size())); + if (tracing(s)) field(Entity.class, "random").set(mob, new TracingRandom(0L, TRACE, ENTS.size())); mob.setId(id++); mob.snapTo(spec.x, spec.y, spec.z, spec.yaw, 0.0F); mob.setYHeadRot(spec.yaw); @@ -495,36 +519,74 @@ public final class VanillaMobAiOracle { if (mob instanceof AgeableMob a) a.setBaby(true); else mob.setBaby(true); } + List afterAdd = new ArrayList<>(); for (String flag : spec.flags) { - if (flag.equals("persistent")) mob.setPersistenceRequired(); - else if (flag.equals("noai")) mob.setNoAi(true); - else if (flag.equals("inlove")) ((net.minecraft.world.entity.animal.Animal) mob).setInLoveTime(600); - else if (flag.equals("tame") && mob instanceof net.minecraft.world.entity.animal.equine.AbstractHorse horse) horse.setTamed(true); - else if (flag.startsWith("age=")) ((AgeableMob) mob).setAge(Integer.parseInt(flag.substring(4))); - else if (flag.startsWith("strength=")) method(net.minecraft.world.entity.animal.equine.Llama.class, "setStrength", int.class) - .invoke(mob, Integer.parseInt(flag.substring(9))); - else if (flag.equals("leashed") || flag.startsWith("leashto=")) { - // Leashed once every mob exists (below). + String[] kv = flag.split("=", 2); + String v = kv.length > 1 ? kv[1] : ""; + switch (kv[0]) { + case "persistent" -> mob.setPersistenceRequired(); + case "noai" -> mob.setNoAi(true); + case "profession" -> { + Villager villager = (Villager) mob; + villager.setVillagerData(villager.getVillagerData().withProfession(level.registryAccess(), + ResourceKey.create(Registries.VILLAGER_PROFESSION, net.minecraft.resources.Identifier.withDefaultNamespace(v)))); + villager.refreshBrain(level); + } + case "level" -> { + Villager villager = (Villager) mob; + villager.setVillagerData(villager.getVillagerData().withLevel(Integer.parseInt(v))); + } + case "xp" -> ((Villager) mob).setVillagerXp(Integer.parseInt(v)); + case "food" -> field(Villager.class, "foodLevel").setInt(mob, Integer.parseInt(v)); + case "age" -> ((AgeableMob) mob).setAge(Integer.parseInt(v)); + case "inv" -> { + for (String item : v.split(",")) { + String[] ic = item.split("\\*"); + ((Villager) mob).getInventory().addItem(new net.minecraft.world.item.ItemStack( + BuiltInRegistries.ITEM.getValue(net.minecraft.resources.Identifier.parse(ic[0])), + ic.length > 1 ? Integer.parseInt(ic[1]) : 1)); + } + } + case "gossip" -> { + // gossip=player:minor_negative:100 + String[] g = v.split(":"); + UUID target = g[0].equals("player") ? PLAYER_UUID : UUID.fromString(g[0]); + ((Villager) mob).getGossips().add(target, + net.minecraft.world.entity.ai.gossip.GossipType.valueOf(g[1].toUpperCase(java.util.Locale.ROOT)), Integer.parseInt(g[2])); + } + case "slept" -> { + long at = Long.parseLong(v); + afterAdd.add(() -> mob.getBrain().setMemory(MemoryModuleType.LAST_SLEPT, at)); + } + case "inlove" -> ((net.minecraft.world.entity.animal.Animal) mob).setInLoveTime(600); + case "tame" -> { if (mob instanceof net.minecraft.world.entity.animal.equine.AbstractHorse horse) horse.setTamed(true); } + case "strength" -> method(net.minecraft.world.entity.animal.equine.Llama.class, "setStrength", int.class) + .invoke(mob, Integer.parseInt(v)); + case "leashed", "leashto" -> { + // Leashed once every mob exists (below). + } + default -> throw new IllegalArgumentException(flag); } - else throw new IllegalArgumentException(flag); } level.addFreshEntity(mob); + for (Runnable r : afterAdd) r.run(); mob.getRandom().setSeed(spec.seed); Brain brain = mob.getBrain(); @SuppressWarnings("unchecked") Map> sensors = (Map>) get(brain, "sensors"); for (Sensor sensor : sensors.values()) sensor.randomlyDelayStart(mob.getRandom()); seedGates(brain, spec.seed); - mobs.add(mob); + LABELS.put(mob, ENTS.size()); + ENTS.add(mob); brains.add(brain); seeds.add(spec.seed); } // Leads: to the player ("leashed") or to the n-th mob ("leashto=n"). for (int i = 0; i < s.mobs.size(); i++) { for (String flag : s.mobs.get(i).flags) { - if (flag.equals("leashed")) ((net.minecraft.world.entity.Leashable) mobs.get(i)).setLeashedTo(player, true); - else if (flag.startsWith("leashto=")) ((net.minecraft.world.entity.Leashable) mobs.get(i)) - .setLeashedTo(mobs.get(Integer.parseInt(flag.substring(8))), true); + if (flag.equals("leashed")) ((net.minecraft.world.entity.Leashable) ENTS.get(i)).setLeashedTo(player, true); + else if (flag.startsWith("leashto=")) ((net.minecraft.world.entity.Leashable) ENTS.get(i)) + .setLeashedTo(ENTS.get(Integer.parseInt(flag.substring(8))), true); } } // Hearts start with the scenario: their tickers have not run during the warm-up. @@ -541,12 +603,11 @@ public final class VanillaMobAiOracle { List created = new ArrayList<>(); Field factoryField = field(EntityType.class, "factory"); Object creakingFactory = factoryField.get(EntityTypes.CREAKING); - final List mobsView = mobs; factoryField.set(EntityTypes.CREAKING, (EntityType.EntityFactory) (type, lvl) -> { Creaking c = new Creaking(type, lvl); try { long seed = 1000 + created.size(); - if (tracing(s)) field(Entity.class, "random").set(c, new TracingRandom(0L, TRACE, mobsView.size() + created.size())); + if (tracing(s)) field(Entity.class, "random").set(c, new TracingRandom(0L, TRACE, ENTS.size() + created.size())); c.getRandom().setSeed(seed); @SuppressWarnings("unchecked") Map> cs = (Map>) get(c.getBrain(), "sensors"); @@ -562,14 +623,25 @@ public final class VanillaMobAiOracle { boolean spawnMonsters = this.getGameRules().get(GameRules.SPAWN_MONSTERS); this.getGameRules().set(GameRules.SPAWN_MOBS, s.spawning, this); this.getGameRules().set(GameRules.SPAWN_MONSTERS, s.spawning, this); - int adopted = 0; level.getRandom().setSeed(s.levelSeed); + // Loot drawn from random sequences starts afresh in every scenario. + this.getRandomSequences().clear(); SOUNDS.clear(); Method tickTime = method(ServerLevel.class, "tickTime"); Object manager = get(level, "entityManager"); Method managerTick = method(manager.getClass(), "tick"); - for (int i = 0; i < mobs.size(); i++) print(0, i, mobs.get(i)); + Map watched = new java.util.LinkedHashMap<>(); + for (int[] box : s.watches) + for (int y = box[1]; y <= box[4]; y++) + for (int z = box[2]; z <= box[5]; z++) + for (int x = box[0]; x <= box[3]; x++) { + BlockPos pos = new BlockPos(x + s.originX, y, z + s.originZ); + watched.put(pos, level.getBlockState(pos)); + } + for (int i = 0; i < ENTS.size(); i++) print(0, i, ENTS.get(i)); for (int t = 1; t <= s.ticks; t++) { + if (s.daycycle) this.clockManager().setTotalTicks(clock, s.time + t); + for (Object[] e : s.timed) if ((Integer) e[0] == t) place((String[]) e[1], level); level.environmentAttributes().invalidateTickCache(); level.updateSkyBrightness(); tickTime.invoke(level); @@ -591,41 +663,78 @@ public final class VanillaMobAiOracle { TracingRandom.tick = t; TracingRandom.on = tracing(s) && t >= traceFrom() && t <= traceTo(); SOUNDS.clear(); - for (int i = 0; i < mobs.size(); i++) { + for (int i = 0; i < ENTS.size(); i++) { // Villager.refreshBrain builds a new brain with new gates: seed those too. - if (mobs.get(i).getBrain() != brains.get(i)) { - brains.set(i, mobs.get(i).getBrain()); + if (ENTS.get(i) instanceof Mob m && m.getBrain() != brains.get(i)) { + brains.set(i, m.getBrain()); seedGates(brains.get(i), seeds.get(i)); } } for (int[] hurt : s.hurts) { - if (hurt[0] == t && hurt[1] < mobs.size() && !mobs.get(hurt[1]).isRemoved()) - mobs.get(hurt[1]).hurtServer(level, level.damageSources().playerAttack(player), 1.0F); + if (hurt[0] == t && hurt[1] < ENTS.size() && ENTS.get(hurt[1]) instanceof LivingEntity hurtMob && !hurtMob.isRemoved()) + hurtMob.hurtServer(level, level.damageSources().playerAttack(player), 1.0F); } - for (Mob mob : mobs) { - if (mob.isRemoved()) continue; - mob.checkDespawn(); - if (mob.isRemoved()) continue; - level.tickNonPassenger(mob); + for (Entity e : List.copyOf(ENTS)) { + if (e.isRemoved()) continue; + e.checkDespawn(); + if (e.isRemoved()) continue; + level.tickNonPassenger(e); } if (!heartPositions.isEmpty()) level.tickBlockEntities(); managerTick.invoke(manager); - // Created mobs that made it into the level, in creation order. - for (; adopted < created.size(); adopted++) { - Mob c = created.get(adopted); - if (!c.isRemoved()) { - mobs.add(c); - brains.add(c.getBrain()); - seeds.add(1000L + adopted); + // Villagers the tick spawned (babies) and a heart's creakings tick from the next + // one, in the order they were added, with a seeded random of their own. Other + // newcomers (animal babies, dropped items) are left out, as the probe leaves them. + List fresh = new ArrayList<>(); + for (Entity e : level.getAllEntities()) + if (!LABELS.containsKey(e) && (e instanceof Villager || created.contains(e))) fresh.add(e); + fresh.sort(java.util.Comparator.comparingInt(Entity::getId)); + for (Entity e : fresh) { + int index = ENTS.size(); + int made = created.indexOf(e); + if (made >= 0) { + // A heart's creaking: seeded when it was created. + LABELS.put(e, index); + ENTS.add(e); + seeds.add(1000L + made); + brains.add(((Mob) e).getBrain()); + continue; + } + long seed = spawnSeed(index); + LABELS.put(e, index); + ENTS.add(e); + seeds.add(seed); + if (e instanceof Mob mob) { + // LivingEntity's constructor turns the head at random (Math.random): + // face it the way the body does, as the probe's newborns. + mob.setYHeadRot(mob.getYRot()); + mob.yHeadRotO = mob.getYRot(); + if (tracing(s)) field(Entity.class, "random").set(mob, new TracingRandom(0L, TRACE, index)); + mob.getRandom().setSeed(seed); + @SuppressWarnings("unchecked") + Map> sensors = (Map>) get(mob.getBrain(), "sensors"); + for (Sensor sensor : sensors.values()) sensor.randomlyDelayStart(mob.getRandom()); + seedGates(mob.getBrain(), seed); + brains.add(mob.getBrain()); + } else { + brains.add(null); } } - for (int i = 0; i < mobs.size(); i++) print(t, i, mobs.get(i)); + for (int i = 0; i < ENTS.size(); i++) print(t, i, ENTS.get(i)); for (BlockPos h : heartPositions) printHeart(t, level, h); for (String sound : SOUNDS) out.println(t + " sound " + sound); + for (var w : watched.entrySet()) { + BlockState now = level.getBlockState(w.getKey()); + if (now != w.getValue()) { + w.setValue(now); + BlockPos p = w.getKey(); + out.println(t + " block " + (p.getX() - s.originX) + "," + p.getY() + "," + (p.getZ() - s.originZ) + " " + stateName(now)); + } + } for (String line : TRACE) out.println(line); TRACE.clear(); } - for (Mob mob : mobs) mob.discard(); + for (Entity e : ENTS) e.discard(); for (BlockPos h : heartPositions) level.setBlock(h, net.minecraft.world.level.block.Blocks.AIR.defaultBlockState(), 2 | 16); factoryField.set(EntityTypes.CREAKING, creakingFactory); this.getGameRules().set(GameRules.SPAWN_MOBS, spawnMobs, this); @@ -634,6 +743,18 @@ public final class VanillaMobAiOracle { out.flush(); } + /** The random seed of the entity a scenario spawned at this index. */ + static long spawnSeed(int index) { return 1000L + 7919L * index; } + + static String stateName(BlockState state) { + StringBuilder sb = new StringBuilder(BuiltInRegistries.BLOCK.getKey(state.getBlock()).toString()); + List props = new ArrayList<>(); + state.getValues().forEach(v -> props.add(v.toString())); + props.sort(null); + if (!props.isEmpty()) sb.append('[').append(String.join(",", props)).append(']'); + return sb.toString(); + } + /** A heart's block state and block entity, and the level random. */ static void printHeart(int t, ServerLevel level, BlockPos at) throws Exception { StringBuilder sb = new StringBuilder(); @@ -663,16 +784,44 @@ public final class VanillaMobAiOracle { } static void seedGate(Object behavior, long seed) throws Exception { - if (!(behavior instanceof GateBehavior)) return; - ShufflingList list = (ShufflingList) field(GateBehavior.class, "behaviors").get(behavior); - field(ShufflingList.class, "random").set(list, new net.minecraft.world.level.levelgen.LegacyRandomSource(seed)); - for (Object child : list) seedGate(child, seed); + seedLists(behavior, seed, java.util.Collections.newSetFromMap(new java.util.IdentityHashMap<>()), 0); + } + + /** + * Seeds every ShufflingList a behaviour holds: a GateBehavior's, and the ones TriggerGate + * captures in its lambdas (BehaviorBuilder's one-shots), found through their fields. + */ + static void seedLists(Object o, long seed, Set seen, int depth) throws Exception { + if (o == null || depth > 8 || !seen.add(o)) return; + if (o instanceof ShufflingList list) { + field(ShufflingList.class, "random").set(list, new net.minecraft.world.level.levelgen.LegacyRandomSource(seed)); + for (Object child : list) seedLists(child, seed, seen, depth + 1); + return; + } + String name = o.getClass().getName(); + if (!name.startsWith("net.minecraft.world.entity.ai.behavior") || o instanceof Enum) return; + for (Class c = o.getClass(); c != null && c.getName().startsWith("net.minecraft"); c = c.getSuperclass()) { + for (Field f : c.getDeclaredFields()) { + if (java.lang.reflect.Modifier.isStatic(f.getModifiers()) || f.getType().isPrimitive()) continue; + f.setAccessible(true); + Object v = f.get(o); + if (v instanceof Iterable it && !(v instanceof ShufflingList)) { + for (Object x : it) seedLists(x, seed, seen, depth + 1); + } else if (v instanceof com.mojang.datafixers.util.Pair pair) { + seedLists(pair.getFirst(), seed, seen, depth + 1); + seedLists(pair.getSecond(), seed, seen, depth + 1); + } else { + seedLists(v, seed, seen, depth + 1); + } + } + } } static String who(Entity e) { if (e == null) return "none"; if (e instanceof ServerPlayer) return "player"; - return "e" + e.getId(); + Integer label = LABELS.get(e); + return "e" + (label != null ? 100000 + label : e.getId()); } static String goals(GoalSelector selector) { @@ -705,13 +854,24 @@ public final class VanillaMobAiOracle { return v.getClass().getSimpleName(); } - void print(int t, int i, Mob m) throws Exception { + void print(int t, int i, Entity e) throws Exception { StringBuilder sb = new StringBuilder(); sb.append(t).append(' ').append(i); - if (m.isRemoved()) { - out.println(sb.append(" removed ").append(m.getRemovalReason())); + if (e.isRemoved()) { + out.println(sb.append(" removed ").append(e.getRemovalReason())); return; } + if (e instanceof ItemEntity item) { + sb.append("\tp ").append(d(item.getX())).append(',').append(d(item.getY())).append(',').append(d(item.getZ())); + Vec3 v = item.getDeltaMovement(); + sb.append("\tv ").append(d(v.x)).append(',').append(d(v.y)).append(',').append(d(v.z)); + sb.append("\tg ").append(item.onGround() ? 1 : 0).append(item.horizontalCollision ? 1 : 0); + sb.append("\titem ").append(stack(item.getItem())).append(' ').append(item.getAge()).append(' ') + .append(field(ItemEntity.class, "pickupDelay").getInt(item)); + out.println(sb); + return; + } + Mob m = (Mob) e; sb.append("\tp ").append(d(m.getX())).append(',').append(d(m.getY())).append(',').append(d(m.getZ())); Vec3 v = m.getDeltaMovement(); sb.append("\tv ").append(d(v.x)).append(',').append(d(v.y)).append(',').append(d(v.z)); @@ -752,9 +912,52 @@ public final class VanillaMobAiOracle { mem.sort(null); sb.append("\tmem ").append(String.join(" ", mem)); } + if (m instanceof Villager villager) sb.append("\tvx ").append(villager(villager)); out.println(sb); } + static String stack(net.minecraft.world.item.ItemStack stack) { + if (stack.isEmpty()) return "-"; + return BuiltInRegistries.ITEM.getKey(stack.getItem()).getPath() + "*" + stack.getCount(); + } + + static String uuidLabel(UUID uuid) { + if (uuid.equals(PLAYER_UUID)) return "player"; + for (Entity e : ENTS) if (e.getUUID().equals(uuid)) return who(e); + return uuid.toString(); + } + + /** A villager's data, food, inventory slots, gossip and offers. */ + static String villager(Villager v) throws Exception { + StringBuilder sb = new StringBuilder(); + var data = v.getVillagerData(); + sb.append("prof ").append(data.profession().unwrapKey().get().identifier().getPath()).append(" lvl ").append(data.level()) + .append(" xp ").append(v.getVillagerXp()).append(" food ").append(field(Villager.class, "foodLevel").getInt(v)) + .append(" age ").append(v.getAge()); + List slots = new ArrayList<>(); + var inv = v.getInventory(); + for (int k = 0; k < inv.getContainerSize(); k++) slots.add(stack(inv.getItem(k))); + while (!slots.isEmpty() && slots.getLast().equals("-")) slots.removeLast(); + sb.append(" inv ").append(slots.isEmpty() ? "-" : String.join(",", slots)); + List gossips = new ArrayList<>(); + v.getGossips().getGossipEntries().forEach((uuid, types) -> types.forEach((type, value) -> + gossips.add(uuidLabel(uuid) + ":" + type.getSerializedName() + "=" + value))); + gossips.sort(null); + sb.append(" gossip ").append(gossips.isEmpty() ? "-" : String.join(",", gossips)); + sb.append(" rep ").append(v.getGossips().getReputation(PLAYER_UUID, x -> true)); + sb.append(" sleep ").append(v.getSleepingPos().map(Server::pos).orElse("-")); + Object offers = get(v, "offers"); + if (offers != null) { + List list = new ArrayList<>(); + for (var o : (net.minecraft.world.item.trading.MerchantOffers) offers) { + list.add(stack(o.getBaseCostA()) + "+" + stack(o.getCostB()) + ">" + stack(o.getResult()) + "/" + o.getUses() + "/" + + o.getMaxUses() + "/" + o.getDemand() + "/" + o.getSpecialPriceDiff()); + } + sb.append(" offers ").append(list.isEmpty() ? "-" : String.join(",", list)); + } + return sb.toString(); + } + static String name(MemoryModuleType type) { return BuiltInRegistries.MEMORY_MODULE_TYPE.getKey(type).getPath(); } diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java new file mode 100644 index 000000000..3a21aaae2 --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java @@ -0,0 +1,661 @@ +package dev.horizonlab.parity; + +import com.google.gson.JsonElement; +import com.mojang.serialization.JsonOps; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import java.io.DataInputStream; +import java.io.EOFException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.SocketTimeoutException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.MessageDigest; +import java.time.Instant; +import java.util.ArrayList; +import java.util.BitSet; +import java.util.HexFormat; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.TreeMap; +import java.util.UUID; +import java.util.zip.DataFormatException; +import java.util.zip.Deflater; +import java.util.zip.Inflater; +import net.minecraft.SharedConstants; +import net.minecraft.core.UUIDUtil; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.world.level.chunk.LevelChunkSection; +import net.minecraft.world.level.chunk.PalettedContainerFactory; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.ComponentSerialization; +import net.minecraft.network.chat.LastSeenMessages; +import net.minecraft.network.protocol.Packet; +import net.minecraft.network.ProtocolInfo; +import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket; +import net.minecraft.network.protocol.common.ClientboundDisconnectPacket; +import net.minecraft.network.protocol.common.ClientboundKeepAlivePacket; +import net.minecraft.network.protocol.common.ClientboundPingPacket; +import net.minecraft.network.protocol.common.ServerboundClientInformationPacket; +import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; +import net.minecraft.network.protocol.common.ServerboundKeepAlivePacket; +import net.minecraft.network.protocol.common.ServerboundPongPacket; +import net.minecraft.network.protocol.common.custom.BrandPayload; +import net.minecraft.network.protocol.configuration.ClientboundFinishConfigurationPacket; +import net.minecraft.network.protocol.configuration.ClientboundSelectKnownPacks; +import net.minecraft.network.protocol.configuration.ConfigurationProtocols; +import net.minecraft.network.protocol.configuration.ServerboundFinishConfigurationPacket; +import net.minecraft.network.protocol.configuration.ServerboundSelectKnownPacks; +import net.minecraft.network.protocol.game.ClientboundChunkBatchFinishedPacket; +import net.minecraft.network.protocol.game.ClientboundDisguisedChatPacket; +import net.minecraft.network.protocol.game.ClientboundLevelChunkPacketData; +import net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket; +import net.minecraft.network.protocol.game.ClientboundPlayerChatPacket; +import net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket; +import net.minecraft.network.protocol.game.ClientboundSystemChatPacket; +import net.minecraft.network.protocol.game.GameProtocols; +import net.minecraft.network.protocol.game.ServerboundAcceptTeleportationPacket; +import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; +import net.minecraft.network.protocol.game.ServerboundChatPacket; +import net.minecraft.network.protocol.game.ServerboundChunkBatchReceivedPacket; +import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket; +import net.minecraft.network.protocol.game.ServerboundPlayerLoadedPacket; +import net.minecraft.network.protocol.handshake.ClientIntent; +import net.minecraft.network.protocol.handshake.ClientIntentionPacket; +import net.minecraft.network.protocol.handshake.HandshakeProtocols; +import net.minecraft.network.protocol.login.ClientboundLoginCompressionPacket; +import net.minecraft.network.protocol.login.ClientboundLoginDisconnectPacket; +import net.minecraft.network.protocol.login.ClientboundLoginFinishedPacket; +import net.minecraft.network.protocol.login.LoginProtocols; +import net.minecraft.network.protocol.login.ServerboundHelloPacket; +import net.minecraft.network.protocol.login.ServerboundLoginAcknowledgedPacket; +import net.minecraft.network.protocol.ping.ClientboundPongResponsePacket; +import net.minecraft.network.protocol.ping.ServerboundPingRequestPacket; +import net.minecraft.network.protocol.status.ClientboundStatusResponsePacket; +import net.minecraft.network.protocol.status.ServerStatus; +import net.minecraft.network.protocol.status.ServerboundStatusRequestPacket; +import net.minecraft.network.protocol.status.StatusProtocols; +import net.minecraft.server.level.ClientInformation; +import net.minecraft.world.entity.PositionMoveRotation; +import net.minecraft.world.phys.Vec3; + +/** + * A headless client made of vanilla's own protocol: it frames, compresses and + * decodes every packet with the codecs of the pinned server JAR (the client + * shares them), so any byte a server sends that vanilla would not read is + * recorded as an error. tools/test_dedicated_server_parity.py points it at a + * vanilla dedicated server and at Horizon's, with the same settings and + * world, and compares the transcripts. + * + *

{@code VanillaProtocolClient HOST PORT NAME OUT} pings the server (status + * and ping), joins as NAME in offline mode, goes through login and + * configuration, accepts the spawn teleport, waits for the chunks around the + * spawn, sends a chat message and the {@code /list} command, and leaves. + * Each transcript line is a section and a summary: the login and + * configuration packets in order; in play, the packets up to the first chunk + * batch in order, then the chunks by position with the hash of their bytes + * and the replies to the chat and command. Values that change between runs + * (entity ids, keep-alive ids, times, salts) are not part of the summaries. + */ +public final class VanillaProtocolClient { + private Socket socket; + private DataInputStream in; + private OutputStream out; + private int threshold = -1; + private final List lines = new ArrayList<>(); + private ProtocolInfo inbound; + private ProtocolInfo outbound; + private String state; + + public static void main(String[] args) throws Exception { + ParityData.init(); + String host = args[0]; + int port = Integer.parseInt(args[1]); + String name = args[2]; + Path output = Path.of(args[3]); + VanillaProtocolClient client = new VanillaProtocolClient(); + try { + client.status(host, port); + client.join(host, port, name); + } catch (Exception e) { + client.lines.add("error " + client.state + " " + e); + } finally { + if (client.socket != null) client.socket.close(); + } + try (PrintWriter w = new PrintWriter(Files.newBufferedWriter(output, StandardCharsets.UTF_8))) { + for (String line : client.lines) w.println(line); + } + System.exit(0); + } + + // ---- connection --------------------------------------------------- + + private void open(String host, int port) throws Exception { + socket = new Socket(); + socket.connect(new InetSocketAddress(host, port), 10_000); + socket.setSoTimeout(30_000); + socket.setTcpNoDelay(true); + in = new DataInputStream(socket.getInputStream()); + out = socket.getOutputStream(); + threshold = -1; + } + + private static int readVarInt(InputStream in) throws Exception { + int value = 0; + for (int i = 0; i < 5; i++) { + int b = in.read(); + if (b < 0) throw new EOFException(); + value |= (b & 0x7F) << (7 * i); + if ((b & 0x80) == 0) return value; + } + throw new IllegalStateException("VarInt too big"); + } + + private static void writeVarInt(ByteBuf buf, int value) { + while ((value & ~0x7F) != 0) { + buf.writeByte((value & 0x7F) | 0x80); + value >>>= 7; + } + buf.writeByte(value); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + private void send(Packet packet) throws Exception { + ByteBuf body = Unpooled.buffer(); + ((ProtocolInfo) outbound).codec().encode(body, packet); + byte[] raw = new byte[body.readableBytes()]; + body.readBytes(raw); + ByteBuf frame = Unpooled.buffer(); + if (threshold >= 0) { + ByteBuf inner = Unpooled.buffer(); + if (raw.length >= threshold) { + writeVarInt(inner, raw.length); + Deflater d = new Deflater(); + d.setInput(raw); + d.finish(); + byte[] tmp = new byte[raw.length + 64]; + while (!d.finished()) { + int n = d.deflate(tmp); + inner.writeBytes(tmp, 0, n); + } + d.end(); + } else { + writeVarInt(inner, 0); + inner.writeBytes(raw); + } + writeVarInt(frame, inner.readableBytes()); + frame.writeBytes(inner); + } else { + writeVarInt(frame, raw.length); + frame.writeBytes(raw); + } + byte[] bytes = new byte[frame.readableBytes()]; + frame.readBytes(bytes); + out.write(bytes); + out.flush(); + } + + /** The next packet's raw payload, decompressed as CompressionDecoder does (validating). */ + private byte[] readFrame() throws Exception { + int length = readVarInt(in); + byte[] frame = new byte[length]; + in.readFully(frame); + if (threshold < 0) return frame; + ByteBuf buf = Unpooled.wrappedBuffer(frame); + int dataLength = readVarInt(new java.io.ByteArrayInputStream(frame)); + int header = varIntSize(dataLength); + buf.skipBytes(header); + if (dataLength == 0) { + byte[] raw = new byte[buf.readableBytes()]; + buf.readBytes(raw); + return raw; + } + if (dataLength < threshold) { + throw new IllegalStateException("Badly compressed packet - size of " + dataLength + " is below server threshold of " + threshold); + } + if (dataLength > 8388608) { + throw new IllegalStateException("Badly compressed packet - size of " + dataLength + " is larger than protocol maximum of 8388608"); + } + Inflater inflater = new Inflater(); + inflater.setInput(frame, header, frame.length - header); + byte[] raw = new byte[dataLength]; + int n; + try { + n = inflater.inflate(raw); + } catch (DataFormatException e) { + throw new IllegalStateException("bad zlib data", e); + } + boolean finished = inflater.finished(); + inflater.end(); + if (n != dataLength || !finished) throw new IllegalStateException("decompressed " + n + " of " + dataLength + " bytes"); + return raw; + } + + private static int varIntSize(int v) { + int n = 1; + while ((v & ~0x7F) != 0) { + v >>>= 7; + n++; + } + return n; + } + + private record Received(Packet packet, byte[] payload, String type) {} + + /** The next packet, decoded with the inbound protocol; leftover bytes are an error (PacketDecoder). */ + private Received read() throws Exception { + byte[] payload = readFrame(); + ByteBuf buf = Unpooled.wrappedBuffer(payload); + Packet packet = inbound.codec().decode(buf); + if (buf.readableBytes() > 0) { + throw new IllegalStateException("Packet " + inbound.id().id() + "/" + packet.type().id() + " was larger than I expected, found " + + buf.readableBytes() + " bytes extra whilst reading"); + } + return new Received(packet, payload, packet.type().id().toString()); + } + + // ---- summaries ---------------------------------------------------- + + private static String sha(byte[] bytes) { + try { + return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes)).substring(0, 16); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private static String json(Component c) { + JsonElement e = ComponentSerialization.CODEC.encodeStart(ParityData.access.createSerializationContext(JsonOps.INSTANCE), c).getOrThrow(); + return e.toString(); + } + + /** A record's text without object identities; other objects by their fields. */ + private static String text(Object o) { + String s = String.valueOf(o); + if (o != null && s.matches("[\\w.$]+@[0-9a-f]+")) { + StringBuilder b = new StringBuilder(o.getClass().getSimpleName()).append('{'); + for (Class c = o.getClass(); c != null && c != Object.class; c = c.getSuperclass()) { + for (java.lang.reflect.Field f : c.getDeclaredFields()) { + if (java.lang.reflect.Modifier.isStatic(f.getModifiers())) continue; + try { + f.setAccessible(true); + b.append(f.getName()).append('=').append(text(f.get(o))).append(", "); + } catch (Exception e) { + b.append(f.getName()).append("=?, "); + } + } + } + return b.append('}').toString(); + } + return s.replaceAll("\\$\\$Lambda/0x[0-9a-f]+@", "\\$\\$Lambda@").replaceAll("@[0-9a-f]{4,}", "@"); + } + + private void record(String section, String line) { + lines.add(section + " " + line); + } + + // ---- status ------------------------------------------------------- + + private void status(String host, int port) throws Exception { + state = "status"; + open(host, port); + outbound = HandshakeProtocols.SERVERBOUND; + send(new ClientIntentionPacket(SharedConstants.getProtocolVersion(), host, port, ClientIntent.STATUS)); + outbound = StatusProtocols.SERVERBOUND; + inbound = StatusProtocols.CLIENTBOUND; + send(ServerboundStatusRequestPacket.INSTANCE); + Received r = read(); + if (r.packet instanceof ClientboundStatusResponsePacket p) { + JsonElement e = ServerStatus.CODEC.encodeStart(JsonOps.INSTANCE, p.status()).getOrThrow(); + // The favicon is compared by its bytes' hash. + String s = e.toString().replaceAll("\"favicon\":\"[^\"]*\"", "\"favicon\":\"" + p.status().favicon().map(f -> sha(f.iconBytes())).orElse("") + "\""); + record("status", r.type + " " + s); + } else { + record("status", r.type + " " + text(r.packet)); + } + send(new ServerboundPingRequestPacket(1234567890123L)); + Received pong = read(); + record("status", pong.type + " " + (pong.packet instanceof ClientboundPongResponsePacket p ? p.time() : text(pong.packet))); + int after = in.read(); + record("status", "closed-after-pong " + (after < 0)); + socket.close(); + } + + // ---- join --------------------------------------------------------- + + private void join(String host, int port, String name) throws Exception { + open(host, port); + state = "login"; + outbound = HandshakeProtocols.SERVERBOUND; + send(new ClientIntentionPacket(SharedConstants.getProtocolVersion(), host, port, ClientIntent.LOGIN)); + outbound = LoginProtocols.SERVERBOUND; + inbound = LoginProtocols.CLIENTBOUND; + UUID uuid = UUIDUtil.createOfflinePlayerUUID(name); + send(new ServerboundHelloPacket(name, uuid)); + while (true) { + Received r = read(); + if (r.packet instanceof ClientboundLoginCompressionPacket p) { + threshold = p.getCompressionThreshold(); + record("login", r.type + " " + threshold); + } else if (r.packet instanceof ClientboundLoginFinishedPacket p) { + record("login", r.type + " " + p.gameProfile().id() + " " + p.gameProfile().name() + " " + p.gameProfile().properties().size()); + send(ServerboundLoginAcknowledgedPacket.INSTANCE); + break; + } else if (r.packet instanceof ClientboundLoginDisconnectPacket p) { + record("login", r.type + " " + json(p.reason())); + return; + } else { + record("login", r.type + " " + text(r.packet)); + } + } + state = "configuration"; + outbound = ConfigurationProtocols.SERVERBOUND; + inbound = ConfigurationProtocols.CLIENTBOUND; + // ClientHandshakePacketListenerImpl.handleLoginFinished: brand, then the options. + send(new ServerboundCustomPayloadPacket(new BrandPayload("vanilla"))); + send(new ServerboundClientInformationPacket(ClientInformation.createDefault())); + while (true) { + Received r = read(); + if (r.packet instanceof ClientboundSelectKnownPacks p) { + record("config", r.type + " " + text(p.knownPacks())); + send(new ServerboundSelectKnownPacks(p.knownPacks())); + } else if (r.packet instanceof ClientboundKeepAlivePacket p) { + record("config", r.type); + send(new ServerboundKeepAlivePacket(p.getId())); + } else if (r.packet instanceof ClientboundPingPacket p) { + record("config", r.type); + send(new ServerboundPongPacket(p.getId())); + } else if (r.packet instanceof ClientboundCustomPayloadPacket p) { + record("config", r.type + " " + text(p.payload())); + } else if (r.packet instanceof net.minecraft.network.protocol.common.ClientboundUpdateTagsPacket p) { + // Each registry's tags and what they hold. Vanilla sends the + // registries and tags in the order of maps keyed by interned + // ResourceKeys (identity hashes), which changes between runs. + var registries = new TreeMap(); + for (var e : p.tags().entrySet()) { + var tags = new TreeMap(); + for (var t : e.getValue().tags().entrySet()) { + tags.put(t.getKey().toString(), t.getValue().toString()); + } + registries.put(e.getKey().identifier().toString(), tags.size() + " " + sha(tags.toString().getBytes())); + } + record("config", r.type + " " + registries.size() + " registries"); + registries.forEach((k, v) -> record("config-tags", k + " " + v)); + } else if (r.packet instanceof ClientboundFinishConfigurationPacket) { + record("config", r.type); + send(ServerboundFinishConfigurationPacket.INSTANCE); + break; + } else if (r.packet instanceof ClientboundDisconnectPacket p) { + record("config", r.type + " " + json(p.reason())); + return; + } else { + record("config", r.type + " " + (r.payload.length > 200 ? "sha=" + sha(r.payload) : text(r.packet))); + } + } + play(); + } + + private void play() throws Exception { + state = "play"; + GameProtocols.Context context = new GameProtocols.Context() { + @Override + public boolean hasInfiniteMaterials() { + return false; + } + + @Override + public boolean canUseCommandBlocks() { + return false; + } + }; + outbound = GameProtocols.SERVERBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(ParityData.access), context); + inbound = GameProtocols.CLIENTBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(ParityData.access)); + socket.setSoTimeout(250); + Map chunks = new TreeMap<>(); + Map layers = new TreeMap<>(); + Map counts = new TreeMap<>(); + boolean ordered = true; + boolean loaded = false; + PositionMoveRotation position = new PositionMoveRotation(Vec3.ZERO, Vec3.ZERO, 0, 0); + long lastChunk = 0; + long deadline = System.currentTimeMillis() + 90_000; + int step = 0; + long stepAt = 0; + while (System.currentTimeMillis() < deadline) { + Received r; + try { + r = read(); + } catch (SocketTimeoutException e) { + r = null; + } + long now = System.currentTimeMillis(); + if (r != null) { + Packet packet = r.packet; + counts.merge(r.type, 1, Integer::sum); + if (r.type.equals("minecraft:chunk_batch_start")) ordered = false; + if (packet instanceof ClientboundKeepAlivePacket p) { + send(new ServerboundKeepAlivePacket(p.getId())); + } else if (packet instanceof ClientboundPingPacket p) { + send(new ServerboundPongPacket(p.getId())); + } else if (packet instanceof ClientboundPlayerPositionPacket p) { + position = PositionMoveRotation.calculateAbsolute(position, p.change(), p.relatives()); + record("play-move", text(position.position()) + " " + position.yRot() + " " + position.xRot()); + send(new ServerboundAcceptTeleportationPacket(p.id(), position.position().x, position.position().y, position.position().z, + position.yRot(), position.xRot())); + send(new ServerboundMovePlayerPacket.PosRot(position.position(), position.yRot(), position.xRot(), false, false)); + if (!loaded) { + send(new ServerboundPlayerLoadedPacket()); + loaded = true; + } + } else if (packet instanceof ClientboundChunkBatchFinishedPacket) { + send(new ServerboundChunkBatchReceivedPacket(64.0F)); + } else if (packet instanceof ClientboundLevelChunkWithLightPacket p) { + chunks.put(p.x() + "," + p.z(), chunkHash(p)); + layers.put(p.x() + "," + p.z(), lightLayers(p)); + lastChunk = now; + int px = (int) Math.floor(position.position().x) >> 4; + int pz = (int) Math.floor(position.position().z) >> 4; + if (p.x() == px && p.z() == pz) chunkParts(p); + } else if (packet instanceof ClientboundSystemChatPacket p && step > 0) { + record("play-chat", "system " + p.overlay() + " " + json(p.content())); + } else if (packet instanceof ClientboundPlayerChatPacket p && step > 0) { + record("play-chat", "player " + p.sender() + " " + p.index() + " " + p.body().content() + " " + p.signature().isPresent() + + " " + p.unsignedContent().map(VanillaProtocolClient::json) + " " + text(p.filterMask()) + " " + + p.chatType().chatType().getRegisteredName() + " " + json(p.chatType().name()) + " " + + p.chatType().targetName().map(VanillaProtocolClient::json)); + } else if (packet instanceof ClientboundDisguisedChatPacket p && step > 0) { + record("play-chat", "disguised " + json(p.message()) + " " + p.chatType().chatType().getRegisteredName()); + } else if (packet instanceof ClientboundDisconnectPacket p) { + record("play", r.type + " " + json(p.reason())); + break; + } + if (ordered) { + record("play-start", r.type + " " + summary(r)); + } + if (packet instanceof net.minecraft.network.protocol.game.ClientboundCommandsPacket p && counts.get(r.type) == 1) { + commandTree(p); + } + } + // Once the chunks stop coming: chat, then /list, then leave. + if (loaded && lastChunk > 0 && now - lastChunk > 3000 && step == 0) { + record("play-chunks", chunks.size() + " " + chunks); + BLOCK_ENTITIES.forEach((k, v) -> record("play-block-entities", k + " " + v)); + // Which light layers exist depends on the neighbouring + // columns: compared where all eight were sent. + Map inner = new TreeMap<>(); + layers.forEach((k, v) -> { + String[] xz = k.split(","); + int x = Integer.parseInt(xz[0]), z = Integer.parseInt(xz[1]); + boolean all = true; + for (int dz = -1; dz <= 1; dz++) for (int dx = -1; dx <= 1; dx++) all &= chunks.containsKey((x + dx) + "," + (z + dz)); + if (all) inner.put(k, v); + }); + record("play-light-layers", inner.size() + " " + inner); + step = 1; + stepAt = now; + send(new ServerboundChatPacket("hello from the parity client", Instant.now(), 0L, Optional.empty(), + new LastSeenMessages.Update(0, new BitSet(), (byte) 0))); + } else if (step == 1 && now - stepAt > 1500) { + step = 2; + stepAt = now; + send(new ServerboundChatCommandPacket("list")); + } else if (step == 2 && now - stepAt > 1500) { + record("play-counts", "chunks " + counts.getOrDefault("minecraft:level_chunk_with_light", 0)); + return; + } + } + record("error", "timed out in play, step " + step + ", chunks " + chunks.size()); + } + + /** + * A chunk's blocks, biomes, heightmaps and block entities (its + * ClientboundLevelChunkPacketData bytes). The light is left out: both servers relight around chunks + * loaded with their saved light, which test_light_parity covers. + */ + private static final Map BLOCK_ENTITIES = new TreeMap<>(); + + private static String chunkHash(ClientboundLevelChunkWithLightPacket p) { + var data = p.chunkData(); + var heightmaps = new TreeMap(); + data.getHeightmaps().forEach((k, v) -> heightmaps.put(k.getSerializedName(), java.util.Arrays.toString(v))); + FriendlyByteBuf buf = data.getReadBuffer(); + byte[] sections = new byte[buf.readableBytes()]; + buf.getBytes(buf.readerIndex(), sections); + // What the sections hold, apart from how their palettes encode it. + FriendlyByteBuf copy = new FriendlyByteBuf(Unpooled.wrappedBuffer(sections)); + var factory = PalettedContainerFactory.create(ParityData.access); + StringBuilder content = new StringBuilder(); + while (copy.isReadable()) { + LevelChunkSection section = new LevelChunkSection(factory); + section.read(copy); + for (int y = 0; y < 16; y++) for (int z = 0; z < 16; z++) for (int x = 0; x < 16; x++) { + content.append(net.minecraft.world.level.block.Block.getId(section.getBlockState(x, y, z))).append(','); + } + for (int y = 0; y < 4; y++) for (int z = 0; z < 4; z++) for (int x = 0; x < 4; x++) { + content.append(section.getNoiseBiome(x, y, z).getRegisteredName()).append(','); + } + } + StringBuilder entities = new StringBuilder(); + data.forEachBlockEntityTag(p.x(), p.z(), (pos, type, tag) -> entities.append(pos.toShortString()).append(' ') + .append(net.minecraft.core.registries.BuiltInRegistries.BLOCK_ENTITY_TYPE.getKey(type)).append(' ').append(tag).append(';')); + if (entities.length() > 0) BLOCK_ENTITIES.put(p.x() + "," + p.z(), entities.toString()); + return "c" + sha(content.toString().getBytes()).substring(0, 8) + "s" + sha(sections).substring(0, 8) + "h" + sha(heightmaps.toString().getBytes()).substring(0, 8) + "e" + + sha(entities.toString().getBytes()).substring(0, 8); + } + + /** The light layers a chunk has, sky then block (with data or empty). */ + private static String lightLayers(ClientboundLevelChunkWithLightPacket p) { + var light = p.lightData(); + BitSet sky = (BitSet) light.skyYMask().clone(); + sky.or(light.emptySkyYMask()); + BitSet block = (BitSet) light.blockYMask().clone(); + block.or(light.emptyBlockYMask()); + return sky + "/" + block; + } + + /** The player's chunk part by part, so a difference in its bytes can be told apart. */ + private void chunkParts(ClientboundLevelChunkWithLightPacket p) { + var data = p.chunkData(); + for (var e : new TreeMap<>(data.getHeightmaps().entrySet().stream() + .collect(java.util.stream.Collectors.toMap(x -> x.getKey().getSerializedName(), x -> x.getValue()))).entrySet()) { + record("chunk-heightmap", e.getKey() + " " + java.util.Arrays.toString(e.getValue()).hashCode()); + } + FriendlyByteBuf buf = data.getReadBuffer(); + byte[] all = new byte[buf.readableBytes()]; + buf.getBytes(buf.readerIndex(), all); + var factory = PalettedContainerFactory.create(ParityData.access); + int index = 0; + while (buf.isReadable()) { + int start = buf.readerIndex(); + LevelChunkSection section = new LevelChunkSection(factory); + section.read(buf); + StringBuilder states = new StringBuilder(); + for (int y = 0; y < 16; y++) for (int z = 0; z < 16; z++) for (int x = 0; x < 16; x++) { + states.append(net.minecraft.world.level.block.Block.getId(section.getBlockState(x, y, z))).append(','); + } + StringBuilder biomes = new StringBuilder(); + for (int y = 0; y < 4; y++) for (int z = 0; z < 4; z++) for (int x = 0; x < 4; x++) { + biomes.append(section.getNoiseBiome(x, y, z).getRegisteredName()).append(','); + } + byte[] bytes = new byte[buf.readerIndex() - start]; + buf.getBytes(start, bytes); + record("chunk-section", index++ + " states=" + sha(states.toString().getBytes()) + " biomes=" + sha(biomes.toString().getBytes()) + + " count=" + (((bytes[0] & 0xFF) << 8) | (bytes[1] & 0xFF)) + " bytes=" + sha(bytes)); + } + StringBuilder entities = new StringBuilder(); + data.forEachBlockEntityTag(p.x(), p.z(), (pos, type, tag) -> entities.append(pos.toShortString()).append(' ') + .append(net.minecraft.core.registries.BuiltInRegistries.BLOCK_ENTITY_TYPE.getKey(type)).append(' ').append(tag).append(';')); + record("chunk-block-entities", entities.toString()); + } + + /** The command tree, one line per top-level command with its subtree. */ + private void commandTree(net.minecraft.network.protocol.game.ClientboundCommandsPacket p) { + var context = net.minecraft.commands.CommandBuildContext.simple(ParityData.access, net.minecraft.world.flag.FeatureFlags.VANILLA_SET); + var root = p.getRoot(context, new net.minecraft.network.protocol.game.ClientboundCommandsPacket.NodeBuilder() { + @Override + public com.mojang.brigadier.builder.ArgumentBuilder createLiteral(String id) { + return com.mojang.brigadier.builder.LiteralArgumentBuilder.literal(id); + } + + @Override + public com.mojang.brigadier.builder.ArgumentBuilder createArgument(String id, com.mojang.brigadier.arguments.ArgumentType type, + net.minecraft.resources.Identifier suggestionId) { + return com.mojang.brigadier.builder.RequiredArgumentBuilder.argument(id + ":" + type.getClass().getSimpleName() + ":" + suggestionId, type); + } + + @Override + public com.mojang.brigadier.builder.ArgumentBuilder configure(com.mojang.brigadier.builder.ArgumentBuilder input, + boolean executable, boolean restricted) { + if (executable) input.executes(c -> 0); + return input; + } + }); + // Vanilla sends each node's children in registration order, which + // its commands report (sorted) does not keep: compared as sets. + var lines = new TreeMap(); + for (var child : root.getChildren()) { + var paths = new java.util.TreeSet(); + describe(child, "", paths, new java.util.IdentityHashMap<>()); + lines.put(child.getName(), paths.size() + " " + sha(String.join("\n", paths).getBytes())); + } + lines.forEach((k, v) -> record("play-command", k + " " + v)); + } + + private static void describe(com.mojang.brigadier.tree.CommandNode node, String path, java.util.Set out, + java.util.IdentityHashMap seen) { + String here = path + " " + node.getName() + (node.getCommand() != null ? "*" : ""); + out.add(here + (node.getRedirect() != null ? " -> " + node.getRedirect().getName() : "")); + if (seen.put(node, true) != null) return; + for (var child : node.getChildren()) describe(child, here, out, seen); + } + + /** The first play packets: entity ids, seeds and times aside. */ + private String summary(Received r) { + String t = r.type; + if (t.equals("minecraft:login")) { + // levels: the server's level keys, in an identity-hashed order after the overworld. + var login = (net.minecraft.network.protocol.game.ClientboundLoginPacket) r.packet; + var levels = new java.util.TreeSet(); + login.levels().forEach(l -> levels.add(l.identifier().toString())); + return text(r.packet).replaceAll("playerId=-?\\d+", "playerId=_").replaceAll("levels=\\[(ResourceKey\\[[^\\]]*\\](, )?)*\\]", "levels=" + levels); + } + if (t.equals("minecraft:set_time") || t.equals("minecraft:keep_alive") || t.equals("minecraft:ping")) return ""; + // The tree is compared below, as sets: the children's order is vanilla's registration order. + if (t.equals("minecraft:commands")) return ""; + if (t.equals("minecraft:set_entity_data")) { + return text(((net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket) r.packet).packedItems()); + } + if (t.contains("entity") || t.equals("minecraft:add_entity") || t.equals("minecraft:update_attributes") + || t.equals("minecraft:player_info_update") || t.equals("minecraft:player_position")) { + return ""; + } + return r.payload.length > 200 ? "sha=" + sha(r.payload) : text(r.packet); + } +} diff --git a/tools/test_dedicated_server_parity.py b/tools/test_dedicated_server_parity.py new file mode 100644 index 000000000..044d75bee --- /dev/null +++ b/tools/test_dedicated_server_parity.py @@ -0,0 +1,392 @@ +#!/usr/bin/env python3 +"""The dedicated server against vanilla's, seen from a vanilla client. + +The pinned server JAR first generates a world (offline mode, a fixed seed, +respawn_radius 0) and stops; a vanilla dedicated server and Horizon's +(`horizon-client --dedicated --nogui`) then serve copies of it with the same +server.properties, server-icon.png and lists. VanillaProtocolClient, a +headless client made of vanilla's own codecs (any byte vanilla would not +read is an error), pings each server, joins it in offline mode through +login, configuration and play, accepts the spawn teleport, receives the +chunks around the spawn (their bytes are compared: the world is loaded from +the same files), chats, runs /list and leaves. The console then runs `list` +and `say` and `stop`. Compared, all of them exactly: + +- the status response (MOTD, players, version, favicon) and the ping; +- the legacy (pre-1.7, 0xFE) ping reply; +- the login and configuration packets in order (compression, profile, + brand, features, known packs, every registry, tags); +- the play packets up to the first chunk batch, in order, with entity ids, + keep-alive ids and times left out; the chunks sent and their bytes; the + replies to the chat message and to /list; +- the console lines about the player, the chat, `list`, `say` and `stop`; +- the files the servers leave: server.properties (but the date line), + eula.txt, ops.json, whitelist.json, banned-players.json, banned-ips.json. + +Online mode needs Mojang's session servers and is not tested. +""" +import json +import os +from pathlib import Path +import platform +import re +import shutil +import socket +import struct +import subprocess +import sys +import threading +import time +import zlib + +try: + from . import lab, oracle_harness as oh, test_rust_parity as base +except ImportError: + import lab + import oracle_harness as oh + import test_rust_parity as base + +NAME = 'dedicated-server-parity' +SEED = 12345 +PLAYER = 'Tester' +# Packets Horizon's server does not send yet, listed rather than compared: +# the recipe property sets and stonecutter recipes (ClientboundUpdateRecipes) +# and a player's advancements (ClientboundUpdateAdvancements, and the recipe +# unlocks their first tick grants). Each is reported on every run. +KNOWN_GAPS = { + 'minecraft:update_recipes': 'ClientboundUpdateRecipesPacket (recipe property sets, stonecutter recipes)', + 'minecraft:update_advancements': 'ClientboundUpdateAdvancementsPacket (player advancements)', + 'minecraft:recipe_book_add+': 'recipes unlocked by advancements on the first tick', +} + + +def gap(line): + """The known gap a vanilla play-start line belongs to, if any.""" + parts = line.split(' ') + if len(parts) < 2 or parts[0] != 'play-start': + return None + if parts[1] in KNOWN_GAPS: + return parts[1] + if parts[1] == 'minecraft:recipe_book_add' and 'entries=[]' not in line: + return 'minecraft:recipe_book_add+' + return None + + +PROPERTIES = { + 'online-mode': 'false', + 'white-list': 'false', + 'level-seed': str(SEED), + 'view-distance': '4', + 'simulation-distance': '4', + 'spawn-protection': '0', + 'max-players': '5', + 'motd': '\\u00a7aHorizon \\u00a7rparity \\u00e9t\\u00e9', + 'pause-when-empty-seconds': '-1', +} + + +def free_port(): + with socket.socket() as s: + s.bind(('127.0.0.1', 0)) + return s.getsockname()[1] + + +def icon_png(): + """A 64x64 RGBA PNG (server-icon.png).""" + rows = b''.join(b'\0' + b''.join(bytes((x * 4, y * 4, (x ^ y) * 4, 255)) for x in range(64)) for y in range(64)) + + def chunk(kind, data): + return struct.pack('>I', len(data)) + kind + data + struct.pack('>I', zlib.crc32(kind + data)) + return (b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('>IIBBBBB', 64, 64, 8, 6, 0, 0, 0)) + + chunk(b'IDAT', zlib.compress(rows)) + chunk(b'IEND', b'')) + + +def write_server_dir(directory, port, world=None): + if directory.exists(): + shutil.rmtree(directory) + directory.mkdir(parents=True) + props = dict(PROPERTIES, **{'server-port': str(port)}) + (directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items()), encoding='iso-8859-1') + (directory / 'eula.txt').write_text('eula=true\n') + (directory / 'server-icon.png').write_bytes(icon_png()) + if world is not None: + shutil.copytree(world, directory / 'world') + for lock in (directory / 'world').glob('session.lock'): + lock.unlink() + + +class Server: + """A server process with its console on stdin and its log collected.""" + + def __init__(self, command, directory, log_name): + self.log_path = directory.parent / f'{log_name}.log' + self.log = self.log_path.open('w', encoding='utf-8') + self.lines = [] + self.process = subprocess.Popen(list(map(str, command)), cwd=directory, stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, + encoding='utf-8', errors='replace') + self.reader = threading.Thread(target=self._read, daemon=True) + self.reader.start() + + def _read(self): + for line in self.process.stdout: + self.lines.append(line.rstrip('\n')) + self.log.write(line) + self.log.flush() + + def wait_for(self, pattern, timeout=300): + deadline = time.time() + timeout + regex = re.compile(pattern) + while time.time() < deadline: + if any(regex.search(l) for l in self.lines): + return + if self.process.poll() is not None: + time.sleep(0.5) + raise AssertionError(f'{self.log_path.name}: server exited ({self.process.returncode}):\n' + + '\n'.join(self.lines[-30:])) + time.sleep(0.2) + raise AssertionError(f'{self.log_path.name}: no line matching {pattern!r} in {timeout} s:\n' + '\n'.join(self.lines[-30:])) + + def console(self, command): + self.process.stdin.write(command + '\n') + self.process.stdin.flush() + + def stop(self, timeout=120): + if self.process.poll() is None: + try: + self.console('stop') + self.process.wait(timeout=timeout) + except (BrokenPipeError, subprocess.TimeoutExpired): + self.process.kill() + self.process.wait() + self.reader.join(timeout=10) + self.log.close() + + +def legacy_ping(port): + """The reply to a 1.6 client's 0xFE 0x01 0xFA MC|PingHost query (LegacyQueryHandler).""" + host = 'localhost' + data = (b'\xfe\x01\xfa' + struct.pack('>H', 11) + 'MC|PingHost'.encode('utf-16-be') + + struct.pack('>H', 7 + 2 * len(host)) + bytes([78]) + struct.pack('>H', len(host)) + + host.encode('utf-16-be') + struct.pack('>I', port)) + with socket.create_connection(('127.0.0.1', port), timeout=10) as s: + s.sendall(data) + reply = b'' + while True: + try: + chunk = s.recv(4096) + except TimeoutError: + return reply.hex() + ' ' + if not chunk: + break + reply += chunk + return reply.hex() + + +CONSOLE_LINE = re.compile(r'^\[\d\d:\d\d:\d\d\] \[([^\]]+)\]: (.*)$') +KEPT = re.compile(r'Tester|\[Server\]|\[Not Secure\]|There are|Stopping|Saving|ThreadedAnvilChunkStorage|All dimensions|Unknown or incomplete') + + +def console_lines(lines): + """The console lines the comparison covers, timestamps and ports aside.""" + out = [] + for line in lines: + m = CONSOLE_LINE.match(line) + if not m or not KEPT.search(m.group(2)): + continue + text = re.sub(r'/127\.0\.0\.1:\d+', '/127.0.0.1:_', m.group(2)) + text = re.sub(r'entity id \d+', 'entity id _', text) + out.append(f'[{m.group(1)}] {text}') + # Vanilla saves its levels in the order of an identity-hashed map, which + # changes between runs: each block of saves is compared sorted. + i = 0 + while i < len(out): + j = i + while j < len(out) and 'Saving chunks for level' in out[j]: + j += 1 + out[i:j] = sorted(out[i:j]) + i = j + 1 if j == i else j + return out + + +def server_files(directory): + out = [] + for name in ['server.properties', 'eula.txt', 'ops.json', 'whitelist.json', 'banned-players.json', 'banned-ips.json']: + path = directory / name + if not path.exists(): + out.append(f'{name}: missing') + continue + text = path.read_bytes().decode('iso-8859-1') + if name in ('server.properties', 'eula.txt'): + # The second comment line is the date Properties.store wrote. + text = re.sub(r'^(#[^\n]*\n)#[^\n]*\n', r'\1#\n', text) + # Random per server, and each side has its own port. + text = re.sub(r'\n(management-server-secret|server-port)=[^\n]*', r'\n\1=', text) + out.append(f'{name}: {text!r}') + return out + + +def run_side(label, command, directory, client): + server = Server(command, directory, label) + try: + server.wait_for(r'Done \(') + port = int(PROPERTIES_PORTS[label]) + legacy = legacy_ping(port) + client(port, directory.parent / f'{label}-client.txt') + time.sleep(1) + server.console('list') + server.console('say console says hi') + server.wait_for(r'\[Server\] console says hi', timeout=30) + finally: + server.stop() + transcript = (directory.parent / f'{label}-client.txt').read_text(encoding='utf-8').splitlines() + return (['legacy-ping ' + legacy] + transcript + ['console ' + l for l in console_lines(server.lines)] + + ['file ' + l for l in server_files(directory)]) + + +PROPERTIES_PORTS = {} + + +def horizon_client_joins(vanilla_server, binary, world, build, reference): + """Horizon's client joins a vanilla dedicated server headless: it must + land where the vanilla client did, receive as many chunks, see its chat + and /list reply, and leave as a vanilla client does (the server's log).""" + label = 'vanilla-for-horizon-client' + PROPERTIES_PORTS[label] = port = free_port() + directory = build / label + write_server_dir(directory, port, world) + client_dir = build / 'horizon-client' + shutil.rmtree(client_dir, ignore_errors=True) + client_dir.mkdir() + report = build / 'horizon-client-report.json' + report.unlink(missing_ok=True) + server = Server(vanilla_server, directory, label) + try: + server.wait_for(r'Done \(') + env = dict(os.environ, HORIZON_NO_AUDIO='1', HORIZON_NET_REPORT=str(report)) + with (build / 'horizon-client-run.log').open('w') as log: + subprocess.run([str(c) for c in [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--render-distance', 2, + '--server', f'127.0.0.1:{port}', '--name', PLAYER]], + cwd=client_dir, env=env, stdout=log, stderr=subprocess.STDOUT, timeout=300, check=True) + server.wait_for(r'left the game', timeout=30) + finally: + server.stop() + got = json.loads(report.read_text()) + move = next(l for l in reference if l.startswith('play-move')) + want_position = [float(v) for v in re.match(r'play-move \(([^,]+), ([^,]+), ([^)]+)\)', move).groups()] + want_chunks = int(next(l for l in reference if l.startswith('play-counts')).split()[-1]) + expected = { + 'connected throughout': None, + 'spawn position': want_position, + 'chunks received': want_chunks, + 'own chat shown': True, + '/list reply shown': True, + 'server log': ['logged in', 'Tester joined the game', '[Not Secure] hello from the horizon client', + 'Tester lost connection: Disconnected', 'Tester left the game'], + } + log = '\n'.join(server.lines) + actual = { + 'connected throughout': got['error'], + 'spawn position': got['position'], + 'chunks received': got['chunks'], + 'own chat shown': any(' hello from the horizon client' in c for c in got['chat']), + '/list reply shown': any('There are 1 of a max of 5 players online: Tester' in c for c in got['chat']), + 'server log': [k for k in expected['server log'] if k in log], + } + return [f'horizon-client {k}: {"ok" if actual[k] == v else f"expected {v!r}, got {actual[k]!r}"}' + for k, v in expected.items()] + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java' + classes = build / 'classes' + shutil.rmtree(classes, ignore_errors=True) + classes.mkdir() + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + oh.run_logged(['javac', '--release', lock['minecraft']['java_major'], '-proc:none', '-nowarn', '-sourcepath', + lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle], + build, 'compile-java') + oh.run_logged(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], build, + 'compile-rust') + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + + # The world, generated once by vanilla. + generate = build / 'generate' + write_server_dir(generate, free_port()) + server = Server(vanilla_server, generate, 'generate') + try: + server.wait_for(r'Done \(') + server.console('gamerule respawn_radius 0') + server.wait_for(r'respawn_radius', timeout=30) + # Vanilla keeps no spawn chunks: force-load the area the client is + # sent and its neighbours (the console stands at the world spawn) so + # both servers load full chunks instead of finishing proto-chunks. + server.console('forceload add ~-80 ~-80 ~80 ~80') + server.wait_for(r'chunks in the Overworld are now force loaded|forceloaded|force loaded', timeout=60) + time.sleep(20) + server.console('forceload remove all') + server.console('save-all flush') + server.wait_for(r'Saved the game', timeout=120) + finally: + server.stop() + world = generate / 'world' + (world / 'playerdata').mkdir(exist_ok=True) + + def client(port, out): + oh.run_logged(['java', '-Xmx1g', '-Duser.timezone=UTC', '-cp', str(classes) + os.pathsep + dependencies, + 'dev.horizonlab.parity.VanillaProtocolClient', '127.0.0.1', port, PLAYER, out], build, + out.stem, timeout=300) + + results = {} + for label, command in [('vanilla', vanilla_server), ('horizon', horizon_server)]: + PROPERTIES_PORTS[label] = free_port() + directory = build / label + write_server_dir(directory, PROPERTIES_PORTS[label], world) + results[label] = run_side(label, command, directory, client) + (build / f'{label}.txt').write_text('\n'.join(results[label]) + '\n', encoding='utf-8') + client_checks = horizon_client_joins(vanilla_server, binary, world, build, results['vanilla']) + vanilla, horizon = results['vanilla'], results['horizon'] + gaps = sorted({gap(l) for l in vanilla if gap(l)}) + vanilla = [l for l in vanilla if not gap(l)] + for g in gaps: + print(f'known gap: {KNOWN_GAPS[g]}') + errors = [l for l in vanilla + horizon if l.startswith('error')] + [l for l in client_checks if not l.endswith(': ok')] + for line in client_checks: + print(line) + differences = [] + import difflib + for line in difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=0, lineterm=''): + differences.append(line[:400]) + # Name the chunks that differ instead of the whole line. + chunk_lines = [dict(re.findall(r'(-?\d+,-?\d+)=(\w+)', next((l for l in side if l.startswith('play-chunks')), ''))) + for side in (vanilla, horizon)] + for key in sorted(set(chunk_lines[0]) | set(chunk_lines[1])): + a, b = chunk_lines[0].get(key), chunk_lines[1].get(key) + if a != b: + differences.append(f'chunk {key}: vanilla {a} horizon {b}') + if differences or errors: + raise AssertionError(f'{len([d for d in differences if d[:1] in "+-" and d[:3] not in ("+++", "---")])} differing lines ' + f'(see {build}/vanilla.txt and horizon.txt):\n' + '\n'.join(differences[:80])) + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + lines=len(vanilla), known_gaps=[KNOWN_GAPS[g] for g in gaps], output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='Dedicated server seen by a vanilla-codec client: status, legacy ping, login, configuration, play ' + 'start, chunks, chat, /list, console list/say/stop, files written. Offline mode.')) + print(f'dedicated server: {len(vanilla)} transcript lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') diff --git a/tools/test_finalize_spawn_parity.py b/tools/test_finalize_spawn_parity.py new file mode 100644 index 000000000..23948d7b9 --- /dev/null +++ b/tools/test_finalize_spawn_parity.py @@ -0,0 +1,177 @@ +#!/usr/bin/env python3 +"""Compare Mob.finalizeSpawn (spawned mob gear, enchantments and state) with vanilla. + +Java (VanillaFinalizeSpawnOracle) creates each case's mobs on a stand-in +ServerLevel and runs vanilla's finalizeSpawn with a seeded level random (what +finalizeSpawn reads through level.getRandom()) and seeded entity randoms; Rust +(finalize_spawn_fixture) runs horizon_client::mobs::finalize on the same case. +Cases cover every mob type, every spawn reason, peaceful to hard, game times, +inhabited times and moon phases (the local difficulty and its special +multiplier), packs sharing group data, biomes (variants, villager types, +sheep colours, rabbit and fox types), structures (black cats), roofs (camel +husk room) and chickens standing by (zombie jockeys). + +Each entity created in a case, the mobs and the jockeys finalizeSpawn makes, +is compared: equipment with its enchantments, drop chances, CanPickUpLoot, +LeftHanded, persistence, age, health, attribute values, effects, variants, +the memories finalizeSpawn sets and the vehicle it rides; then the next draw +of every entity random and of the level random, which pins every draw in +between. +""" +import argparse +import json +import os +from pathlib import Path +import platform +import random +import subprocess +try: + from . import lab, test_rust_parity as base +except ImportError: + import lab + import test_rust_parity as base + +REASONS = ['NATURAL', 'CHUNK_GENERATION', 'SPAWNER', 'STRUCTURE', 'BREEDING', 'MOB_SUMMONED', 'JOCKEY', 'EVENT', + 'CONVERSION', 'REINFORCEMENT', 'TRIGGERED', 'BUCKET', 'SPAWN_ITEM_USE', 'COMMAND', 'DISPENSER', 'PATROL', + 'TRIAL_SPAWNER', 'LOAD', 'DIMENSION_TRAVEL'] +# Mobs whose finalizeSpawn draws, by how often the corpus picks them. +GEARED = ['zombie', 'husk', 'drowned', 'zombie_villager', 'zombified_piglin', 'skeleton', 'stray', 'bogged', 'parched', + 'wither_skeleton', 'pillager', 'vindicator', 'piglin', 'piglin_brute', 'vex', 'fox', 'spider', 'illusioner', + 'evoker', 'ravager', 'witch'] +OTHERS = ['pig', 'cow', 'mooshroom', 'sheep', 'chicken', 'rabbit', 'horse', 'donkey', 'mule', 'llama', 'trader_llama', + 'goat', 'panda', 'polar_bear', 'wolf', 'cat', 'ocelot', 'parrot', 'bee', 'turtle', 'armadillo', 'frog', + 'axolotl', 'squid', 'glow_squid', 'cod', 'salmon', 'tropical_fish', 'pufferfish', 'dolphin', 'bat', 'villager', + 'wandering_trader', 'iron_golem', 'snow_golem', 'creeper', 'cave_spider', 'enderman', 'endermite', + 'silverfish', 'slime', 'magma_cube', 'phantom', 'hoglin', 'zoglin', 'ghast', 'blaze', 'strider', 'guardian', + 'elder_guardian', 'shulker', 'camel', 'allay', 'breeze', 'sniffer', 'happy_ghast', 'warden', 'creaking', + 'camel_husk', 'nautilus', 'zombie_nautilus', 'sulfur_cube', 'zombie_horse', 'skeleton_horse', 'tadpole', + 'giant', 'wither'] +BIOMES = ['minecraft:plains', 'minecraft:desert', 'minecraft:snowy_plains', 'minecraft:snowy_taiga', 'minecraft:taiga', + 'minecraft:jungle', 'minecraft:bamboo_jungle', 'minecraft:swamp', 'minecraft:mangrove_swamp', + 'minecraft:savanna', 'minecraft:badlands', 'minecraft:forest', 'minecraft:grove', 'minecraft:old_growth_pine_taiga', + 'minecraft:old_growth_spruce_taiga', 'minecraft:sparse_jungle', 'minecraft:windswept_savanna', + 'minecraft:mushroom_fields', 'minecraft:river', 'minecraft:frozen_river', 'minecraft:ocean', + 'minecraft:warm_ocean', 'minecraft:lukewarm_ocean', 'minecraft:cold_ocean', 'minecraft:frozen_ocean', + 'minecraft:ice_spikes', 'minecraft:cherry_grove', 'minecraft:pale_garden', 'minecraft:dripstone_caves', + 'minecraft:lush_caves', 'minecraft:deep_dark', 'minecraft:nether_wastes', 'minecraft:crimson_forest', + 'minecraft:soul_sand_valley', 'minecraft:the_end', 'minecraft:snowy_slopes', 'minecraft:frozen_peaks', + 'minecraft:wooded_badlands', 'minecraft:beach', 'minecraft:snowy_beach', 'minecraft:meadow', + 'minecraft:sulfur_caves'] +STRUCTURES = ['minecraft:swamp_hut', 'minecraft:village_plains', 'minecraft:pillager_outpost', 'minecraft:fortress', + 'minecraft:trial_chambers'] +FLOORS = ['minecraft:grass_block', 'minecraft:sand', 'minecraft:stone', 'minecraft:snow_block', 'minecraft:netherrack'] + + +def dimension(biome): + if biome in ('minecraft:nether_wastes', 'minecraft:crimson_forest', 'minecraft:soul_sand_valley'): + return 'minecraft:the_nether' + if biome == 'minecraft:the_end': + return 'minecraft:the_end' + return 'minecraft:overworld' + + +def corpus(): + rng = random.Random(0x46494E414C) + cases = [] + for k in range(int(os.environ.get('FINALIZE_SPAWN_CASES', '24000'))): + kind = rng.choice(GEARED) if rng.random() < 0.55 else rng.choice(OTHERS) + reason = rng.choice(['NATURAL'] * 10 + ['CHUNK_GENERATION', 'SPAWNER', 'STRUCTURE', 'COMMAND', 'SPAWN_ITEM_USE', + 'TRIAL_SPAWNER', 'PATROL', 'EVENT', 'JOCKEY'] * 2 + REASONS) + biome = rng.choice(BIOMES) + kv = { + 'type': f'minecraft:{kind}', + 'reason': reason, + 'difficulty': rng.choice(['hard', 'hard', 'normal', 'normal', 'easy', 'peaceful']), + 'time': rng.choice([0, 0, 50000, 72000, 400000, 800000, 1512000, 3000000, rng.randrange(0, 4000000)]), + 'inhabited': rng.choice([0, 0, 0, 100000, 1000000, 3600000, 5000000, rng.randrange(0, 4000000)]), + 'phase': rng.randrange(8), + 'seed': rng.randrange(-2**63, 2**63), + 'eseed': rng.randrange(-2**47, 2**47), + 'pack': rng.choice([1, 1, 1, 2, 3, 4, 6]), + 'pos': f'{rng.randrange(-200, 200) + rng.choice([0.5, 0.25, 0.0]):.2f},{rng.randrange(-30, 120)},' + f'{rng.randrange(-200, 200) + rng.choice([0.5, 0.75, 0.0]):.2f}', + 'biome': biome, + 'dim': dimension(biome), + 'floor': rng.choice(FLOORS), + } + if rng.random() < 0.15: + kv['structure'] = rng.choice(STRUCTURES) + if rng.random() < 0.2: + kv['roof'] = rng.choice([1, 2, 3]) + if rng.random() < 0.15: + kv['chickens'] = rng.choice([1, 2]) + if rng.random() < 0.05: + kv['monsters'] = 0 + cases.append(f'case/{k} ' + ' '.join(f'{a}={b}' for a, b in kv.items())) + return cases + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--prepare', action='store_true') + parser.add_argument('--java-only', action='store_true', help='run the oracle and stop') + args = parser.parse_args() + build = lab.LOCAL / 'rust-finalize-spawn-parity' + build.mkdir(parents=True, exist_ok=True) + if args.prepare: + lab.prepare() + lock, jars, libraries = base.verify_artifacts() + resources = lab.LOCAL / 'horizon-client/26.3' + oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaFinalizeSpawnOracle.java' + sources = [oracle, Path(__file__), lab.ROOT / 'crates/horizon-client/examples/finalize_spawn_fixture.rs', + lab.ROOT / 'crates/horizon-client/src/mobs/finalize.rs'] + tested = base.source_hashes([s for s in sources if s.exists()]) + inputs = build / 'inputs.txt' + cases = corpus() + inputs.write_text(''.join(line + '\n' for line in cases), encoding='utf-8') + classes = build / 'classes' + classes.mkdir(exist_ok=True) + deps = os.pathsep.join(map(str, libraries)) + + def run(command, name): + path = build / (name + '.log') + with path.open('w', encoding='utf-8') as log: + try: + base.run(list(map(str, command)), stdout=log, stderr=subprocess.STDOUT) + except subprocess.CalledProcessError: + log.flush() + raise SystemExit(f'{name} failed:\n' + path.read_text(errors='replace')[-4000:]) + run(['javac', '--release', lock['minecraft']['java_major'], '-proc:none', '-sourcepath', str(lab.ROOT / 'mods/horizon/src/test/java'), + '-cp', str(jars['server']) + os.pathsep + deps, '-d', classes, oracle], 'compile-java') + run(['java', '-Xmx3G', '-cp', os.pathsep.join([str(classes), str(jars['server']), deps]), + 'dev.horizonlab.parity.VanillaFinalizeSpawnOracle', inputs, build / 'server.txt'], 'server') + vanilla = (build / 'server.txt').read_text().splitlines() + errors = [l for l in vanilla if ' error ' in l] + if errors: + raise SystemExit(f'oracle errors ({len(errors)}): {errors[:5]}') + if args.java_only: + print(f'{len(vanilla)} oracle lines') + return + halloween = vanilla[0].split()[1] + run(['cargo', 'build', '--release', '-p', 'horizon-client', '--example', 'finalize_spawn_fixture'], 'compile-rust') + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release/examples/finalize_spawn_fixture' + run([binary, resources, inputs, build / 'rust.txt', halloween], 'rust') + rust = (build / 'rust.txt').read_text().splitlines() + bad = [(a, b) for a, b in zip(vanilla, rust) if a != b] + if bad or len(vanilla) != len(rust): + report = '\n'.join(f' vanilla {a[:2000]}\n rust {b[:2000]}' for a, b in bad[:12]) + raise SystemExit(f'{len(bad)} of {len(vanilla)} lines differ (rust {len(rust)} lines):\n{report}') + base.ensure_sources_unchanged([s for s in sources if s.exists()], tested) + entities = sum(line.count(' | ') for line in vanilla) // 2 + enchanted = sum(line.count('[minecraft:') for line in vanilla) + lab.write_json(build / 'report.json', { + 'schema': 1, 'minecraft': lock['minecraft']['version'], 'platform': platform.platform(), 'cases': len(cases), + 'entities': entities, 'enchanted_items': enchanted, 'sources_sha256': tested, + 'scope': 'Mob.finalizeSpawn for every mob type and spawn reason: populateDefaultEquipmentSlots, ' + 'populateDefaultEquipmentEnchantments through the mob_spawn_equipment and pillager_spawn_crossbow ' + 'providers, drop chances, CanPickUpLoot, LeftHanded, the follow range spawn bonus, babies and ' + 'group data, jockeys (chicken, spider, strider, zombie horse, camel husk, zombie nautilus), ' + 'variants and sound variants, zombie attributes, spider effects, patrol leaders, villager types, ' + 'cube sizes, horse and llama attributes and the memories finalizeSpawn sets, with every draw of the ' + 'level random and of the entities\' own randoms.'}) + print(f'{len(cases):,} finalizeSpawn cases match vanilla ({entities:,} entities, {enchanted:,} enchanted items)') + + +if __name__ == '__main__': + main() diff --git a/tools/test_lan_parity.py b/tools/test_lan_parity.py new file mode 100644 index 000000000..8f207a6f2 --- /dev/null +++ b/tools/test_lan_parity.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +"""LAN world discovery against the pinned client's LanServerPinger and +LanServerDetection: the announcement an opened world multicasts +(`[MOTD]...[/MOTD][AD]port[/AD]` in UTF-8), and how the multiplayer screen +parses what it hears (parseMotd, parseAddress, UTF-8 decoding of malformed +bytes) and keeps its list (LanServerList.addServer: one entry per +address, the first MOTD kept, never expired while the screen is open). +VanillaLanOracle and the `lan_probe` example must print the same lines.""" +import random + +try: + from . import oracle_harness as oh +except ImportError: + import oracle_harness as oh + +NAME = 'lan-parity' + + +def make_input(build): + rng = random.Random(4445) + lines = [] + motds = ['Player - New World', 'Steve - §aGreen §lWorld', 'été 日本語 ✓', '', '[MOTD]', 'a[/MOTD]b', 'x' * 300, + 'tab\\there', '[AD]25565[/AD]'] + for motd in motds: + for port in ['25565', '1024', '65535', '0', 'abc', '']: + lines.append(f'ping\t{motd}\t{port}') + fragments = [b'[MOTD]', b'[/MOTD]', b'[AD]', b'[/AD]', b'25565', b'1234', b'Steve - World', b'\xc3\xa9', b'\xe6\x97', + b'\xff', b'\xc0\x80', b'\xed\xa0\x80', b'\xf0\x9f\x98\x80', b'\xf0\x9f', b'abc', b' ', b'\x00', b'-1'] + ips = ['192.168.1.10', '10.0.0.2', '127.0.0.1'] + good = lambda motd, port: f'[MOTD]{motd}[/MOTD][AD]{port}[/AD]'.encode() + for i in range(400): + if i % 50 == 0: + lines.append('reset') + if rng.random() < 0.4: + data = good(rng.choice(['A - W', 'B - W', 'é', '']), rng.choice(['25565', '25566', 'x', ''])) + else: + data = b''.join(rng.choice(fragments) for _ in range(rng.randint(0, 9))) + lines.append(f'heard\t{rng.choice(ips)}\t{data.hex()}') + path = build / 'cases.txt' + path.write_text('\n'.join(lines) + '\n', encoding='utf-8') + return path, len(lines) + + +if __name__ == '__main__': + oh.differential(NAME, 'VanillaLanOracle', 'lan_probe', make_input, + 'LanServerPinger.createPingString/parseMotd/parseAddress, LanServerDetection decoding and ' + 'LanServerList.addServer/takeDirtyServers', + sources=[oh.lab.ROOT / 'crates/horizon-client/src/lan/discovery.rs']) diff --git a/tools/test_mob_ai_parity.py b/tools/test_mob_ai_parity.py index baef29675..9abc3f960 100644 --- a/tools/test_mob_ai_parity.py +++ b/tools/test_mob_ai_parity.py @@ -77,6 +77,36 @@ def scenarios(): 'player 12.5 -60 0.5', 'mob minecraft:villager 0.5 -60 0.5 0 51 persistent', ]) + # Two villagers gossip (TradeWithVillager): what one heard of the player + # passes to the other, weakened, once in 1200 ticks; idle and at the bell. + gossips = ('gossip=player:minor_negative:100 gossip=player:major_positive:15 ' + 'gossip=player:trading:20 gossip=player:minor_positive:3') + for name, opts, extra in [('villager_gossip_idle', 'time=100', []), + ('villager_gossip_bell', 'time=9500', ['block 0 -60 -6 minecraft:bell'])]: + add(name, 400, extra + [ + 'player 14.5 -60 0.5', + f'mob minecraft:villager 0.5 -60 0.5 0 61 persistent {gossips}', + 'mob minecraft:villager 2.5 -60 0.5 180 62 persistent gossip=player:minor_positive:50', + ], opts) + # Two villagers with bread breed (VillagerMakeLove): they eat, a baby is + # born and takes a free bed, the parents rest for 6000 ticks. + # PoiManager.take hands out the free beds of a section in hash order: + # the parents claim the two near them, the child gets the only one left. + beds = [] + for x in (-6, 3, 6): + beds += [f'block {x} -60 6 minecraft:red_bed[facing=north,part=foot]', + f'block {x} -60 5 minecraft:red_bed[facing=north,part=head]'] + for name, seeds in [('villager_breed', (71, 72)), ('villager_breed_2', (73, 74))]: + add(name, 900, beds + [ + 'player 14.5 -60 0.5', + f'mob minecraft:villager 0.5 -60 0.5 0 {seeds[0]} persistent inv=bread*3', + f'mob minecraft:villager 2.5 -60 0.5 180 {seeds[1]} persistent inv=bread*3 food=4', + ], 'time=100') + # Too little food: no breeding. + add('villager_breed_hungry', 400, beds[:2] + [ + 'mob minecraft:villager 0.5 -60 0.5 0 75 persistent inv=bread*2', + 'mob minecraft:villager 2.5 -60 0.5 180 76 persistent inv=bread*3', + ], 'time=100') # Breezes: idle, and fighting a player near, in, and beyond the inner jump circle. add('breeze_alone', 200, ['mob minecraft:breeze 0.5 -60 0.5 90 61 persistent']) add('breeze_player', 200, ['player 8.5 -60 0.5', 'mob minecraft:breeze 0.5 -60 0.5 0 62 persistent']) @@ -155,6 +185,8 @@ def write_scenarios(path, chosen): lines.append(f'origin {ox} {oz}') for line in body: f = line.split() + # `at TICK block ...` changes blocks before that tick; `watch` boxes stay relative. + head, f = (f[:2], f[2:]) if f[0] == 'at' else ([], f) if f[0] == 'player': f[1] = repr(float(f[1]) + ox) f[3] = repr(float(f[3]) + oz) @@ -169,7 +201,7 @@ def write_scenarios(path, chosen): f[k] = str(int(f[k]) + ox) for k in (3, 6): f[k] = str(int(f[k]) + oz) - lines.append(' '.join(f)) + lines.append(' '.join(head + f)) lines.append('end') path.write_text('\n'.join(lines) + '\n') @@ -187,6 +219,9 @@ def parse(path): if parts[1] == 'sound': cur['sounds'].setdefault(t, []).append(parts[2]) continue + if parts[1] == 'block': + cur.setdefault('blocks', {}).setdefault(t, []).append(parts[2]) + continue if parts[1] == 'trace': cur.setdefault('trace', {}).setdefault(t, []).append(parts[2]) continue @@ -286,11 +321,14 @@ def compare(vanilla, rust, fields=None): report.append(f'{name}: hearts differ at tick {t}: vanilla {v.get("hearts", {}).get(t)} | horizon {r.get("hearts", {}).get(t)}') clean = False break - for t in sorted(set(v['sounds']) | set(r['sounds'])): - if v['sounds'].get(t, []) != r['sounds'].get(t, []): - report.append(f'{name}: sounds differ at tick {t}: vanilla {v["sounds"].get(t, [])} | horizon {r["sounds"].get(t, [])}') - clean = False - break + # Sounds and watched block changes: first differing tick. + for kind in ('sounds', 'blocks'): + va, ra = v.get(kind, {}), r.get(kind, {}) + for t in sorted(set(va) | set(ra)): + if va.get(t, []) != ra.get(t, []): + report.append(f'{name}: {kind} differ at tick {t}: vanilla {va.get(t, [])} | horizon {ra.get(t, [])}') + clean = False + break totals['clean'] += clean if clean: report.append(f'{name}: all {len(v["ticks"])} ticks match')