Files
horizon-airdef/crates/horizon-client/examples/mob_ai_probe.rs
T
Ludovic Marie a9fa46fd0a Run the copper golem on CopperGolemAi's brain and compare its chest sorting
The golem's goals are replaced by vanilla's brain (core and idle activities,
the transport cooldown, look-at-player-sometimes, stroll or wait), and
TransportItemsBetweenContainers moves into the library: nearest wanted
chest in chunk and block entity map order, queueing behind golems with a
chest open, the 60-tick interaction with its states, sounds and opener
counts, picking up and putting down (with vanilla's overcounting), visited
and unreachable positions. Weathering draws from the level random after the
living tick; the spin sound is client-local. mob_ai gains seven copper golem
scenarios that print each golem's state, hand and opened chest and every
chest's contents and openers; all match vanilla.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 03:40:13 +02:00

1142 lines
42 KiB
Rust

//! Replays `tools/test_mob_ai_parity.py` scenarios on Horizon's mob AI and
//! prints each mob's AI-visible state after every tick in the format of
//! `VanillaMobAiOracle`.
//!
//! usage: mob_ai_probe RESOURCES SCENARIOS OUTPUT
use horizon_client::{
assets::Assets,
biome::Biomes,
blocks::BlockFacts,
chunk::{Chunk, HEIGHT, MIN_Y},
collisions,
containers::{BlockEntity, CreakingHeart},
items::{ItemStack, Items},
light::{LightTables, light_chunk},
mobs::{
Damage, MobType, creaking,
creaking_heart::{self, HeartEvent, HeartHost},
},
mobs::{
Mob, MobData, MobEvent, MobTypes, MobWorld, PlayerView, Seen, Species, Who,
control::Operation, spawning::LocalDifficulty,
},
world::World,
};
use horizon_core::{
geometry::{Aabb, Vec3},
random::LegacyRandom,
};
use horizon_world::{chunk::BlockState, registry::BlockRegistry, registry::BlockStateId};
use std::{collections::BTreeMap, fmt::Write as _, fs, path::Path, sync::Arc};
struct MobSpec {
kind: String,
pos: [f64; 3],
yaw: f32,
seed: i64,
baby: bool,
flags: Vec<String>,
}
#[derive(Default)]
struct Scenario {
name: String,
ticks: i32,
time: i64,
rain: bool,
thunder: bool,
origin: [i32; 2],
blocks: Vec<Vec<String>>,
player: Option<[f64; 3]>,
creative: bool,
/// The tick from which the player faces another yaw.
turn: Option<(i32, f32)>,
/// Creaking hearts placed when the scenario starts.
hearts: Vec<Vec<String>>,
/// `ServerLevel.isSpawningMonsters`.
spawning: bool,
/// The player hits a mob for 1 at the start of a tick: (tick, mob index).
hurts: Vec<(i32, usize)>,
hand: Option<String>,
mobs: Vec<MobSpec>,
}
fn parse(text: &str) -> Vec<Scenario> {
let mut out = Vec::new();
let mut cur: Option<Scenario> = None;
for line in text.lines() {
let f: Vec<&str> = line.split_whitespace().collect();
if f.is_empty() || f[0].starts_with('#') {
continue;
}
let s = cur.get_or_insert_with(Scenario::default);
match f[0] {
"scenario" => {
s.name = f[1].to_owned();
s.time = 6000;
for kv in &f[2..] {
let (k, v) = kv.split_once('=').unwrap_or((kv, ""));
match k {
"ticks" => s.ticks = v.parse().unwrap(),
"time" => s.time = v.parse().unwrap(),
"rain" => s.rain = true,
"thunder" => s.thunder = true,
"levelseed" => {}
"spawning" => s.spawning = true,
_ => panic!("unknown scenario option {kv}"),
}
}
}
"origin" => s.origin = [f[1].parse().unwrap(), f[2].parse().unwrap()],
"fill" | "block" | "chest" => {
s.blocks.push(f.iter().map(|x| (*x).to_owned()).collect())
}
"heart" => s.hearts.push(f.iter().map(|x| (*x).to_owned()).collect()),
"hurt" => s.hurts.push((f[1].parse().unwrap(), f[2].parse().unwrap())),
"player" => {
s.player = Some([
f[1].parse().unwrap(),
f[2].parse().unwrap(),
f[3].parse().unwrap(),
]);
for opt in &f[4..] {
if *opt == "creative" {
s.creative = true;
} else if let Some(t) = opt.strip_prefix("turn=") {
let (tick, yaw) = t.split_once(',').unwrap();
s.turn = Some((tick.parse().unwrap(), yaw.parse().unwrap()));
} else if let Some(item) = opt.strip_prefix("hand=") {
s.hand = Some(item.to_owned());
} else {
panic!("unknown player option {opt}");
}
}
}
"mob" => s.mobs.push(MobSpec {
kind: f[1].to_owned(),
pos: [
f[2].parse().unwrap(),
f[3].parse().unwrap(),
f[4].parse().unwrap(),
],
yaw: f[5].parse().unwrap(),
seed: f[6].parse().unwrap(),
baby: f[7..].contains(&"baby"),
flags: f[7..]
.iter()
.filter(|x| **x != "baby")
.map(|x| (*x).to_owned())
.collect(),
}),
"end" => out.push(cur.take().unwrap()),
other => panic!("unknown scenario line {other}"),
}
}
out
}
/// `minecraft:name[key=value,...]`
fn state(registry: &BlockRegistry, text: &str) -> BlockStateId {
let (name, props) = match text.split_once('[') {
Some((n, rest)) => (n, rest.trim_end_matches(']')),
None => (text, ""),
};
let mut properties = BTreeMap::new();
for kv in props.split(',').filter(|s| !s.is_empty()) {
let (k, v) = kv.split_once('=').expect("property");
properties.insert(k.to_owned(), v.to_owned());
}
registry
.resolve(&BlockState {
name: name.to_owned(),
properties,
})
.unwrap_or_else(|e| panic!("{text}: {e}"))
}
fn d(v: f64) -> String {
format!("{:x}", v.to_bits())
}
fn f(v: f32) -> String {
format!("{:x}", v.to_bits())
}
fn who(w: Option<Who>) -> String {
match w {
None => "none".into(),
Some(Who::Player) => "player".into(),
Some(Who::Mob(id)) => format!("e{id}"),
}
}
/// Vanilla's goal class for a Horizon goal (most share the name).
fn goal_name(m: &Mob, name: &str) -> String {
let vanilla = match name {
"Float" => "FloatGoal",
"Stroll" => "WaterAvoidingRandomStrollGoal",
"RandomStroll" => "RandomStrollGoal",
"RandomSwimming" => "RandomSwimmingGoal",
"LookAtPlayer" => "LookAtPlayerGoal",
"RandomLookAround" => "RandomLookAroundGoal",
"HurtByTarget" => "HurtByTargetGoal",
"NearestTarget" => "NearestAttackableTargetGoal",
"Panic" => "PanicGoal",
"Breed" => "BreedGoal",
"Tempt" => "TemptGoal",
"FollowParent" => "FollowParentGoal",
"MeleeAttack" => match m.species() {
Species::Zombie | Species::Husk | Species::ZombieVillager => "ZombieAttackGoal",
_ => "MeleeAttackGoal",
},
"MoveThroughVillage" => "MoveThroughVillageGoal",
"RestrictSun" => "RestrictSunGoal",
"FleeSun" => "FleeSunGoal",
"AvoidEntity" => "AvoidEntityGoal",
"BowAttack" => "RangedBowAttackGoal",
"Swell" => "SwellGoal",
"LeapAtTarget" => "LeapAtTargetGoal",
"EatBlock" => "EatBlockGoal",
"MoveTowardsRestriction" => "MoveTowardsRestrictionGoal",
"TradeWithPlayer" => "TradeWithPlayerGoal",
"LookAtTradingPlayer" => "LookAtTradingPlayerGoal",
"WanderToPosition" => "WanderToPositionGoal",
"DrinkItem" => "UseItemGoal",
"BreathAir" => "BreathAirGoal",
"TryFindWater" => "TryFindWaterGoal",
"RangedAttack" => "RangedAttackGoal",
"MoveTowardsTarget" => "MoveTowardsTargetGoal",
"DrownedGoToWater" => "DrownedGoToWaterGoal",
"TakeBlock" => "EndermanTakeBlockGoal",
"LeaveBlock" => "EndermanLeaveBlockGoal",
"FreezeWhenLookedAt" => "EndermanFreezeWhenLookedAt",
"LookForPlayer" => "EndermanLookForPlayerGoal",
"HealRaiders" => "NearestHealableRaiderTargetGoal",
other => return other.to_owned(),
};
vanilla.to_owned()
}
fn goals(m: &Mob, names: Vec<&'static str>) -> String {
if names.is_empty() {
return "-".into();
}
names
.iter()
.map(|n| goal_name(m, n))
.collect::<Vec<_>>()
.join("|")
}
/// 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<MobEvent>,
from: usize,
w: &MobWorld,
types: &MobTypes,
items: &Items,
born: &mut Vec<Mob>,
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> {
mobs: &'a mut Vec<Mob>,
added: Vec<Mob>,
types: &'a MobTypes,
created: &'a mut i64,
spawning: bool,
}
impl HeartHost for Host<'_> {
fn mob(&self, id: u32) -> Option<&Mob> {
self.mobs
.iter()
.chain(self.added.iter())
.find(|m| m.id == id)
}
fn mob_mut(&mut self, id: u32) -> Option<&mut Mob> {
self.mobs
.iter_mut()
.chain(self.added.iter_mut())
.find(|m| m.id == id)
}
fn mob_for_uuid(&self, uuid: [i32; 4]) -> Option<u32> {
self.mob(uuid[3] as u32)
.filter(|m| !m.removed)
.map(|m| m.id)
}
fn uuid_for(&mut self, id: u32) -> [i32; 4] {
[0, 0, 0, id as i32]
}
fn creaking_type(&self) -> Option<Arc<MobType>> {
self.types
.types
.get("minecraft:creaking")
.map(|t| Arc::new(t.clone()))
}
fn create_mob(&mut self, kind: Arc<MobType>, at: Vec3) -> Mob {
let seed = 1000 + *self.created;
*self.created += 1;
let mut m = Mob::new(200_000 + seed as u32, kind, at, 0.0);
m.random = LegacyRandom::new(seed);
horizon_client::mobs::monster_brain::init(&mut m, seed);
m
}
fn add_mob(&mut self, m: Mob) {
self.added.push(m);
}
fn spawning_monsters(&self) -> bool {
self.spawning
}
}
/// The heart's state, block entity and the level random, as the oracle
/// prints them.
fn print_heart(
out: &mut String,
t: i32,
registry: &BlockRegistry,
world: &World,
at: [i32; 3],
heart: &CreakingHeart,
lr: &LegacyRandom,
) {
let state = registry
.state(world.block(at[0], at[1], at[2]))
.and_then(|s| s.properties.get("creaking_heart_state").cloned())
.unwrap_or_default();
let _ = writeln!(
out,
"{t} heart {},{},{}\t{state}\t{} {} {} {} {}\tlrng {:x}",
at[0],
at[1],
at[2],
heart.ticker,
heart.emitter,
heart.output_signal,
heart.ticks_existed,
if heart.creaking.is_some() {
"linked"
} else {
"none"
},
lr.state()
);
}
/// Whether an event hurts the scenario's player (survival, normal difficulty,
/// no armour, made hurtable again every tick by the oracle).
fn player_hurt(player: Option<&PlayerView>, e: &MobEvent) -> bool {
let Some(p) = player.filter(|p| !p.creative) else {
return false;
};
match e {
MobEvent::AttackPlayer(amount, _) | MobEvent::DamagePlayer { amount, .. } => *amount > 0.0,
// ServerExplosion.hurtEntities: within twice the radius every hit does damage.
MobEvent::Explode(at, radius) => {
let d = Vec3::new(at[0], at[1], at[2])
.distance_squared(p.position)
.sqrt();
let r = 2.0 * f64::from(*radius);
d / r <= 1.0 && d != 0.0
}
_ => false,
}
}
fn print(out: &mut String, t: i32, i: usize, m: &Mob) {
let _ = write!(out, "{t} {i}");
if m.removed {
// Entity.RemovalReason: the end of the death animation, else a discard.
let reason = if m.alive() { "DISCARDED" } else { "KILLED" };
let _ = writeln!(out, " removed {reason}");
return;
}
let p = m.position;
let v = m.velocity;
let _ = write!(
out,
"\tp {},{},{}\tv {},{},{}",
d(p.x),
d(p.y),
d(p.z),
d(v.x),
d(v.y),
d(v.z)
);
let _ = write!(
out,
"\tr {},{},{},{}",
f(m.yaw),
f(m.head_pitch),
f(m.head_yaw),
f(m.body_yaw)
);
let _ = write!(
out,
"\tg {}{}\thp {}",
u8::from(m.on_ground),
u8::from(m.horizontal_collision),
f(m.health)
);
let _ = write!(
out,
"\tgoals {}\ttargets {}\ttarget {}",
goals(m, m.ai.goals.running_names()),
goals(m, m.ai.targets.running_names()),
who(m.ai.target)
);
let _ = write!(out, "\tnav ");
match &m.ai.nav.path {
None => out.push_str("none"),
Some(path) => {
let _ = write!(
out,
"{},{},{} {}/{}{}",
path.target[0],
path.target[1],
path.target[2],
path.next,
path.nodes.len(),
if path.next >= path.nodes.len() {
" done"
} else {
""
}
);
}
}
let mv = &m.ai.mov;
let op = match mv.operation {
Operation::Wait => "WAIT",
Operation::MoveTo => "MOVE_TO",
Operation::Strafe => "STRAFE",
Operation::Jumping => "JUMPING",
};
let _ = write!(
out,
"\tmove {op} {},{},{} {}",
d(mv.wanted[0]),
d(mv.wanted[1]),
d(mv.wanted[2]),
d(mv.speed_modifier)
);
let lk = &m.ai.look;
let _ = write!(
out,
"\tlook {} {},{},{}",
lk.cooldown,
d(lk.wanted[0]),
d(lk.wanted[1]),
d(lk.wanted[2])
);
let _ = write!(
out,
"\tin {},{},{},{}",
f(m.ai.xxa),
f(m.ai.zza),
f(m.ai.speed),
u8::from(m.ai.jumping)
);
let _ = write!(out, "\trng {:x}", m.random.state());
let brain = horizon_client::mobs::villager_brain::describe(m)
.or_else(|| horizon_client::mobs::monster_brain::describe(m))
.or_else(|| horizon_client::mobs::animal_brain::describe(m));
if let Some((acts, mem)) = brain {
let _ = write!(out, "\tact {acts}\tmem {mem}");
}
if m.species() == Species::Villager {
let _ = write!(out, "\tvx {}", villager(m));
}
if m.species() == Species::CopperGolem {
let held = horizon_client::mobs::transport_items::held(m).map_or("-".into(), stack);
let opened = m
.extra
.opened_chest
.map_or("-".into(), |p| format!("{},{},{}", p[0], p[1], p[2]));
let _ = write!(out, "\tcg {} {held} {opened}", m.extra.variant3);
}
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<String> = (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<String> = 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<String> = 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<String> = std::env::args().skip(1).collect();
if args.len() != 3 {
return Err("usage: mob_ai_probe RESOURCES SCENARIOS OUTPUT".into());
}
let root = Path::new(&args[0]);
let assets = Assets::load(root)?;
let registry = assets.registry;
let facts = BlockFacts::load(root, registry)?;
let data = MobData::load(root, &facts);
let types = MobTypes::load(root)?;
let biomes = Biomes::load(root)?;
let items = Items::load(root, registry, &facts)?;
let tables = LightTables::load(root, registry, &facts)?;
let collision = collisions::load(root, registry)?;
let air = registry.default_state("minecraft:air").ok_or("no air")?;
let layers = [
state(registry, "minecraft:bedrock"),
state(registry, "minecraft:dirt"),
state(registry, "minecraft:dirt"),
state(registry, "minecraft:grass_block"),
];
let plains = biomes.id("minecraft:plains");
let mut out = String::new();
for s in parse(&fs::read_to_string(&args[1])?) {
let _ = writeln!(out, "== {}", s.name);
let mut world = World::new(air, 0);
let (ocx, ocz) = (s.origin[0] >> 4, s.origin[1] >> 4);
const R: i32 = 4;
for cz in ocz - R..=ocz + R {
for cx in ocx - R..=ocx + R {
let mut chunk = Chunk::empty(cx, cz);
for qy in 0..HEIGHT / 4 {
for qz in 0..4 {
for qx in 0..4 {
chunk.set_biome_cell(qx, qy, qz, plains);
}
}
}
world.chunks.insert((cx, cz), Arc::new(chunk));
for z in 0..16 {
for x in 0..16 {
for (y, layer) in layers.iter().enumerate() {
world.set(cx * 16 + x, MIN_Y + y as i32, cz * 16 + z, *layer)?;
}
}
}
}
}
for b in &s.blocks {
let n = |i: usize| b[i].parse::<i32>().unwrap();
if b[0] == "block" || b[0] == "chest" {
world.set(n(1), n(2), n(3), state(registry, &b[4]))?;
if b[0] == "chest" {
// chest X Y Z STATE SLOTS: item*count or - per slot.
let mut slots = vec![None; 27];
for (k, item) in b.get(5).map_or("", String::as_str).split(',').enumerate() {
if let Some((id, count)) = item.split_once('*') {
slots[k] = Some(ItemStack::new(
&format!("minecraft:{id}"),
count.parse().unwrap(),
));
}
}
world
.block_entities
.insert((n(1), n(2), n(3)), BlockEntity::Chest(slots));
}
} else {
let st = state(registry, &b[7]);
for x in n(1).min(n(4))..=n(1).max(n(4)) {
for y in n(2).min(n(5))..=n(2).max(n(5)) {
for z in n(3).min(n(6))..=n(3).max(n(6)) {
world.set(x, y, z, st)?;
}
}
}
}
}
let mut hearts: Vec<([i32; 3], CreakingHeart)> = Vec::new();
for h in &s.hearts {
let at = [
h[1].parse::<i32>().unwrap(),
h[2].parse::<i32>().unwrap(),
h[3].parse::<i32>().unwrap(),
];
world.set(at[0], at[1], at[2], state(registry, &h[4]))?;
hearts.push((at, CreakingHeart::default()));
}
let mut created = 0_i64;
let keys: Vec<(i32, i32)> = world.chunks.keys().copied().collect();
let lit: Vec<((i32, i32), Chunk)> = keys
.iter()
.map(|&(x, z)| {
let region: [[Option<&Chunk>; 3]; 3] = std::array::from_fn(|dz| {
std::array::from_fn(|dx| {
world
.chunks
.get(&(x + dx as i32 - 1, z + dz as i32 - 1))
.map(|c| &**c)
})
});
let mut c = (*world.chunks[&(x, z)]).clone();
c.sections = light_chunk(&region, &tables, true);
c.lit = true;
((x, z), c)
})
.collect();
for (k, c) in lit {
world.chunks.insert(k, Arc::new(c));
}
world.day_time = s.time;
world.game_time = 1000;
let mut mobs: Vec<Mob> = Vec::new();
for (i, spec) in s.mobs.iter().enumerate() {
let kind = types
.types
.get(&spec.kind)
.ok_or_else(|| format!("unknown mob {}", spec.kind))?;
let mut m = Mob::new(
100_000 + i as u32,
Arc::new(kind.clone()),
Vec3::new(spec.pos[0], spec.pos[1], spec.pos[2]),
spec.yaw,
);
if spec.baby {
m.extra.age = -24000;
}
for flag in &spec.flags {
match flag.as_str() {
"persistent" => m.persistent = true,
"noai" => m.no_ai = true,
"inlove" => m.extra.in_love = 600,
"tame" => m.extra.tame = true,
x if x.starts_with("age=") => m.extra.age = x[4..].parse().unwrap(),
x if x.starts_with("strength=") => m.extra.variant2 = x[9..].parse().unwrap(),
x if x.starts_with("hand=") => {
let (id, count) = x[5..].split_once('*').unwrap();
let stack =
ItemStack::new(&format!("minecraft:{id}"), count.parse().unwrap());
horizon_client::mobs::transport_items::set_held(&mut m, Some(stack));
}
// Set once the brain exists (below).
x if x.starts_with("cooldown=") => {}
"leashed" => m.extra.leash = Some(horizon_client::mobs::leash::Holder::Player),
x if x.starts_with("leashto=") => {
let n: u32 = x[8..].parse().unwrap();
m.extra.leash = Some(horizon_client::mobs::leash::Holder::Mob(100_000 + n));
}
other => villager_flag(&mut m, other),
}
}
m.random = LegacyRandom::new(spec.seed);
if horizon_client::mobs::monster_brain::has_brain(m.species()) {
// The oracle delays the sensors again with the seeded random
// and seeds RunOne's shuffles with the mob's seed.
horizon_client::mobs::monster_brain::init(&mut m, spec.seed);
}
if m.species() == Species::Villager {
// The oracle seeds the random after the brain is built, then
// delays the sensors again and seeds the gates' shuffles.
horizon_client::mobs::villager_brain::init(
&mut m,
world.game_time,
s.time,
spec.seed,
);
}
// The same for the sniffer's and the happy ghast's brains.
if horizon_client::mobs::animal_brain::kind_of(&m).is_some() {
horizon_client::mobs::animal_brain::init(&mut m, spec.seed);
}
if let Some(c) = spec.flags.iter().find_map(|f| f.strip_prefix("cooldown=")) {
horizon_client::mobs::animal_brain::set_transport_cooldown(
&mut m,
c.parse().unwrap(),
);
}
mobs.push(m);
}
let mut player = s.player.map(|p| {
let position = Vec3::new(p[0], p[1], p[2]);
PlayerView {
position,
bounds: Aabb::new(
Vec3::new(p[0] - 0.3, p[1], p[2] - 0.3),
Vec3::new(p[0] + 0.3, p[1] + 1.8, p[2] + 0.3),
),
eye: [p[0], p[1] + f64::from(1.62_f32), p[2]],
alive: true,
creative: s.creative,
sneaking: false,
look: [0.0, 0.0, 1.0],
main_hand: s.hand.as_deref().map(Into::into),
off_hand: None,
head: None,
armor_cover: 0.0,
spectator: false,
gold_armor: false,
hurt_by: None,
last_hurt: None,
velocity: Vec3::ZERO,
yaw: 0.0,
hero: false,
uuid: PLAYER_UUID,
allies: Default::default(),
}
});
let mut scratch = LegacyRandom::new(0);
for (i, m) in mobs.iter().enumerate() {
print(&mut out, 0, i, m);
}
// The MobWorld every step sees, over a snapshot of the mobs.
macro_rules! mob_world {
($snapshot:expr) => {
MobWorld {
teams: Default::default(),
world: &world,
registry,
facts: &facts,
collision: &collision,
data: &data,
player: player.clone(),
others: $snapshot,
daylight: (s.time.rem_euclid(24000)) < 12000,
game_time: world.game_time,
day_time: world.day_time,
raining: s.rain,
thundering: s.thunder,
nether: false,
block_tags: &items.block_tags,
biomes: &biomes,
mob_griefing: true,
difficulty: LocalDifficulty::new(2, world.day_time, 0, 0.0),
end: false,
world_seed: 0,
// FlatLevelSource.getSeaLevel.
sea_level: -63,
items: Some(&items),
}
};
}
for t in 1..=s.ticks {
world.game_time += 1;
let mut heart_events_later: Vec<HeartEvent> = Vec::new();
// Creaking.tick reads the heart's block entity.
for (at, h) in &hearts {
world.block_entities.insert(
(at[0], at[1], at[2]),
BlockEntity::CreakingHeart(Box::new(h.clone())),
);
}
if let (Some(p), Some((tick, yaw))) = (player.as_mut(), s.turn)
&& t >= tick
{
// Entity.calculateViewVector(0, yaw).
let r = -yaw * horizon_core::geometry::trig::DEG_TO_RAD;
let (cos, sin) = (
horizon_core::geometry::trig::cos(f64::from(r)),
horizon_core::geometry::trig::sin(f64::from(r)),
);
p.look = [f64::from(sin), -0.0, f64::from(cos)];
p.yaw = yaw;
}
let mut snapshot: Vec<Seen> = mobs.iter().filter(|m| !m.removed).map(seen).collect();
let w = mob_world!(&snapshot);
let mut events = Vec::new();
let mut heart_events = Vec::new();
let mut added = Vec::new();
for &(tick, i) in &s.hurts {
if tick != t || i >= mobs.len() || mobs[i].removed {
continue;
}
let at = player
.as_ref()
.map_or([0.0; 3], |p| [p.position.x, p.position.y, p.position.z]);
mobs[i].damage(1.0, Damage::player(at));
// Creaking.hurtServer: the heart's reaction, then the sway.
if let Some((home, by_player)) = creaking::take_hurt(&mut mobs[i], &w) {
if by_player && let Some((_, h)) = hearts.iter_mut().find(|(at, _)| *at == home)
{
let mut host = Host {
mobs: &mut mobs,
added: Vec::new(),
types: &types,
created: &mut created,
spawning: s.spawning,
};
creaking_heart::creaking_hurt(
h,
home,
&w,
&mut scratch,
&mut host,
&mut heart_events,
);
added.append(&mut host.added);
}
for e in heart_events.drain(..) {
match e {
HeartEvent::Sound(name, _, volume, pitch) => {
events.push(MobEvent::Sound(name, [0.0; 3], volume, pitch))
}
HeartEvent::SetBlock(..) => heart_events_later.push(e),
HeartEvent::Mob(ev) => events.push(ev),
_ => {}
}
}
creaking::sway(&mut mobs[i], &mut events);
}
events.append(&mut mobs[i].outbox);
}
#[allow(unused_mut)] // filled only with the rng-trace feature
let mut traces: Vec<String> = Vec::new();
let mut pushes = Vec::new();
let mut born = Vec::new();
for i in 0..mobs.len() {
if mobs[i].removed {
continue;
}
let w = mob_world!(&snapshot);
let m = &mut mobs[i];
horizon_client::mobs::apply_mob_pushes(m, &mut pushes);
let from = events.len();
#[cfg(feature = "rng-trace")]
horizon_core::random::trace::set(true);
let alive = m.tick(&w, &mut scratch, &mut events);
#[cfg(feature = "rng-trace")]
{
horizon_core::random::trace::set(false);
for line in horizon_core::random::trace::take() {
traces.push(format!("{t} trace {i} {line}"));
}
}
horizon_client::mobs::take_mob_pushes(&mut events, from, &mut pushes);
if !alive {
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,
);
// What a copper golem did to chests, before the next mob ticks.
for e in &events[from.min(events.len())..] {
match e {
MobEvent::ChestOpen { chest, open } => {
let o = world
.chest_openers
.entry((chest[0], chest[1], chest[2]))
.or_insert((0, 0.0));
if *open {
o.0 += 1;
o.1 = o.1.max(3.0);
} else {
o.0 -= 1;
if o.0 == 0 {
o.1 = 0.0;
}
}
}
MobEvent::ChestSlot { chest, slot, stack } => {
if let Some(e) = world
.block_entities
.get_mut(&(chest[0], chest[1], chest[2]))
&& let Some(sl) = e.slots_mut().get_mut(*slot)
{
*sl = stack.clone();
}
}
_ => {}
}
}
// 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) {
*e = seen(&mobs[i]);
e.alive &= !mobs[i].removed;
}
}
for m in &mut mobs {
horizon_client::mobs::apply_mob_pushes(m, &mut pushes);
}
let w = mob_world!(&snapshot);
// ServerLevel.tickBlockEntities: the hearts (none ticks once uprooted).
heart_events.append(&mut heart_events_later);
for (at, h) in &mut hearts {
let uprooted = registry
.state(w.world.block(at[0], at[1], at[2]))
.and_then(|s| s.properties.get("creaking_heart_state").cloned())
.is_some_and(|v| v == "uprooted");
if uprooted {
continue;
}
let mut host = Host {
mobs: &mut mobs,
added: Vec::new(),
types: &types,
created: &mut created,
spawning: s.spawning,
};
creaking_heart::server_tick(h, *at, &w, &mut scratch, &mut host, &mut heart_events);
added.append(&mut host.added);
}
drop(w);
mobs.append(&mut born);
mobs.append(&mut added);
for e in &heart_events {
match e {
HeartEvent::SetBlock(at, st) => world.set(at[0], at[1], at[2], *st)?,
HeartEvent::Sound(name, _, volume, pitch) => {
events.push(MobEvent::Sound(name, [0.0; 3], *volume, *pitch))
}
HeartEvent::Mob(ev) => events.push(ev.clone()),
_ => {}
}
}
for (i, m) in mobs.iter().enumerate() {
print(&mut out, t, i, m);
}
for e in &events {
if let MobEvent::Sound(name, _, volume, pitch) = e {
let _ = writeln!(
out,
"{t} sound minecraft:{name} {} {}",
f(*volume),
f(*pitch)
);
} else if let MobEvent::SoundId(name, _, volume, pitch) = e {
let _ = writeln!(out, "{t} sound {name} {} {}", f(*volume), f(*pitch));
} else if player_hurt(player.as_ref(), e) {
// The oracle's survival player, healed and made hurtable every tick,
// takes the damage (LivingEntity.playHurtSound).
let _ = writeln!(
out,
"{t} sound minecraft:entity.player.hurt 3f800000 3f800000"
);
}
}
for (at, h) in &hearts {
print_heart(&mut out, t, registry, &world, *at, h, &scratch);
}
for b in s.blocks.iter().filter(|b| b[0] == "chest") {
let n = |i: usize| b[i].parse::<i32>().unwrap();
let key = (n(1), n(2), n(3));
let Some(e) = world.block_entities.get(&key) else {
continue;
};
let mut slots: Vec<String> = e
.slots()
.iter()
.map(|s| s.as_ref().map_or("-".into(), stack))
.collect();
while slots.last().is_some_and(|s| s == "-") {
slots.pop();
}
let count = world.chest_openers.get(&key).map_or(0, |o| o.0);
let _ = writeln!(
out,
"{t} chest {},{},{} {} {count}",
key.0 - s.origin[0],
key.1,
key.2 - s.origin[1],
if slots.is_empty() {
"-".into()
} else {
slots.join(",")
}
);
}
for line in traces {
let _ = writeln!(out, "{line}");
}
}
}
fs::write(&args[2], out)?;
Ok(())
}