From bc4d0cc80604171262cc6753b1db08fac3a46730 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 15:48:53 +0200 Subject: [PATCH] Villagers: know each block state's POI type from a table built once PoiTypes.forState ran per palette entry of every section a villager searched (and per block of the sections that hold one), with a string match on the block name each time and a per-search hash map in front. At world load, when every villager looks for a job site, bed and meeting point at once, that was 16 % of the tick. The POI type of every state is now worked out once, the first time a villager looks, and read from a table after that. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/mobs/data.rs | 5 ++++ .../horizon-client/src/mobs/villager_brain.rs | 29 +++++++++++-------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/crates/horizon-client/src/mobs/data.rs b/crates/horizon-client/src/mobs/data.rs index 48fc4234f..47c40b093 100644 --- a/crates/horizon-client/src/mobs/data.rs +++ b/crates/horizon-client/src/mobs/data.rs @@ -50,6 +50,9 @@ pub struct MobData { pub biome_tags: HashMap>, /// Entity type tags (`data/*/tags/entity_type`). pub entity_tags: HashMap>, + /// The villager POI type of every block state (`PoiTypes.forState`), + /// worked out the first time a villager looks for one. + pub pois: std::sync::OnceLock>>, } impl MobData { pub fn load(root: &Path, facts: &BlockFacts) -> Self { @@ -130,6 +133,7 @@ impl MobData { animations, biome_tags: HashMap::new(), entity_tags: HashMap::new(), + pois: Default::default(), } } /// Without the export, approximate path types from the gameplay facts. @@ -184,6 +188,7 @@ impl MobData { animations: HashMap::new(), biome_tags: HashMap::new(), entity_tags: HashMap::new(), + pois: Default::default(), } } pub fn state(&self, id: horizon_world::registry::BlockStateId) -> StateData { diff --git a/crates/horizon-client/src/mobs/villager_brain.rs b/crates/horizon-client/src/mobs/villager_brain.rs index e318fb9f2..942afd233 100644 --- a/crates/horizon-client/src/mobs/villager_brain.rs +++ b/crates/horizon-client/src/mobs/villager_brain.rs @@ -17,7 +17,6 @@ use horizon_core::{ geometry::Vec3, random::{LegacyRandom, RandomSource}, }; -use std::collections::HashMap; /// `MemorySlot.NEVER_EXPIRE`. pub(super) const NEVER: i64 = i64::MAX; @@ -846,8 +845,20 @@ impl Poi { pub fn poi_at(w: &MobWorld, x: i32, y: i32, z: i32) -> Option { poi_of_state(w, w.world.block(x, y, z)) } -/// The POI a block state is, if any (`PoiTypes.forState`). +/// The POI a block state is, if any (`PoiTypes.forState`), from the table +/// of every state built on first use. fn poi_of_state(w: &MobWorld, state: horizon_world::registry::BlockStateId) -> Option { + let table = w.data.pois.get_or_init(|| { + (0..w.registry.len()) + .map(|i| state_poi(w, horizon_world::registry::BlockStateId(i as u32))) + .collect() + }); + match table.get(state.0 as usize) { + Some(poi) => *poi, + None => state_poi(w, state), + } +} +fn state_poi(w: &MobWorld, state: horizon_world::registry::BlockStateId) -> Option { let name = &*w.facts.get(state).name; if name == "minecraft:bell" { return Some(Poi::Meeting); @@ -929,16 +940,10 @@ fn pois_in_range( let chunks = radius.div_euclid(16) + 1; let y0 = (c[1] - radius).max(crate::chunk::MIN_Y); let y1 = (c[1] + radius).min(crate::chunk::MIN_Y + crate::chunk::HEIGHT - 1); - // The POI each block state is, worked out once per search, and the - // sections whose palette holds none skipped whole, as PoiManager only - // visits sections with records: the same positions in the same order, - // without reading and naming every block of the sphere. - let mut kinds: HashMap> = HashMap::new(); - let mut kind = |state: u32| { - *kinds - .entry(state) - .or_insert_with(|| poi_of_state(w, horizon_world::registry::BlockStateId(state))) - }; + // Sections whose palette holds no POI state are skipped whole, as + // PoiManager only visits sections with records: the same positions in + // the same order, without reading and naming every block of the sphere. + let kind = |state: u32| poi_of_state(w, horizon_world::registry::BlockStateId(state)); for qz in cz - chunks..=cz + chunks { for qx in cx - chunks..=cx + chunks { let Some(chunk) = w.world.chunks.get(&(qx, qz)) else {