diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index ce82c2da5..9fbfc61ea 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -732,6 +732,7 @@ impl Game<'_> { self.guardian_bubbles(); self.villager_reputation_tick(); self.merchant_tick(); + self.villager_item_sense(); self.villager_pickups(); for m in dead { self.mob_died(&m); diff --git a/crates/horizon-client/src/game/villagers.rs b/crates/horizon-client/src/game/villagers.rs index 36c79f893..a7aa87b9e 100644 --- a/crates/horizon-client/src/game/villagers.rs +++ b/crates/horizon-client/src/game/villagers.rs @@ -240,6 +240,67 @@ impl Game<'_> { } } + /// `NearestItemSensor.doTick` for the villagers whose sensor is due: the + /// nearest dropped item within 32 blocks that `Villager.wantsToPickUp` + /// accepts and the villager sees, or none. + pub(super) fn villager_item_sense(&mut self) { + if self.dropped.is_empty() { + for m in &mut self.mobs { + if m.species() == Species::Villager && villager_brain::take_item_scan(m) { + villager_brain::set_wanted_item(m, None); + } + } + return; + } + let picks_up = self + .res + .items() + .item_tags + .get("minecraft:villager_picks_up") + .cloned(); + let mut due = Vec::new(); + for (i, m) in self.mobs.iter_mut().enumerate() { + if m.species() == Species::Villager && villager_brain::take_item_scan(m) { + due.push(i); + } + } + if due.is_empty() { + return; + } + let w = self.mob_world_base(&[]); + let mut found = Vec::new(); + for i in due { + let m = &self.mobs[i]; + let area = m.bounds().inflate(Vec3::new(32.0, 16.0, 32.0)); + let requested = villager::requested_items(m); + let eye = Vec3::new(m.position.x, m.eye_y(), m.position.z); + let mut best: Option<(f64, Vec3)> = None; + for e in &self.dropped { + let d = e.position.distance_squared(m.position); + if best.is_some_and(|(b, _)| d >= b) + || !e.bounds().intersects(area) + || d >= 32.0 * 32.0 + { + continue; + } + let item = &e.stack.item; + let wants = villager::food_points(item) > 0 + || picks_up.as_ref().is_some_and(|t| t.contains(&**item)) + || requested.contains(&&**item); + let fits = villager::ext(m) + .is_none_or(|x| x.can_add(&e.stack, self.item_max_stack(item))); + // ItemEntity's eye height is 0.2125. + let at = Vec3::new(e.position.x, e.position.y + 0.2125, e.position.z); + if wants && fits && w.line_of_sight(eye, at) { + best = Some((d, e.position)); + } + } + found.push((i, best.map(|(_, p)| [p.x, p.y, p.z]))); + } + for (i, at) in found { + villager_brain::set_wanted_item(&mut self.mobs[i], at); + } + } /// `Mob.aiStep` item pickup for villagers (`InventoryCarrier`): food, /// seeds and what their profession wants, from within a block. pub(super) fn villager_pickups(&mut self) { diff --git a/crates/horizon-client/src/mobs/villager_brain.rs b/crates/horizon-client/src/mobs/villager_brain.rs index 55e16396c..04e90ef18 100644 --- a/crates/horizon-client/src/mobs/villager_brain.rs +++ b/crates/horizon-client/src/mobs/villager_brain.rs @@ -204,6 +204,8 @@ pub struct Memories { pub nearest_hostile: Slot, pub hiding_place: Slot<[i32; 3]>, pub heard_bell_time: Slot, + /// `NEAREST_VISIBLE_WANTED_ITEM`: where the item entity lies. + pub nearest_wanted_item: Slot<[f64; 3]>, pub cant_reach_walk_target_since: Slot, pub golem_detected_recently: Slot, pub last_slept: Slot, @@ -236,6 +238,7 @@ impl Memories { self.nearest_hostile.tick(); self.hiding_place.tick(); self.heard_bell_time.tick(); + self.nearest_wanted_item.tick(); self.cant_reach_walk_target_since.tick(); self.golem_detected_recently.tick(); self.last_slept.tick(); @@ -322,6 +325,8 @@ pub struct Brain { baby: bool, /// Whether the nearest-bed sensor's batch cache is used (`NearestBedSensor`). bed_cache: Vec<([i32; 3], i64)>, + /// `NearestItemSensor` is due; the game, which knows the items, answers. + scan_items: bool, } #[derive(Clone, Debug)] struct Entry { @@ -362,6 +367,7 @@ impl Brain { shuffle_seed, baby, bed_cache: Vec::new(), + scan_items: false, }; b.list = packages(baby, shuffle_seed, farmer); b @@ -556,7 +562,7 @@ fn tick_sensors(m: &mut Mob, w: &MobWorld, lr: &mut LegacyRandom) { match i { 0 => sense_living(m, w), 1 => sense_players(m, w), - 2 => {} + 2 => brain_mut(m).scan_items = true, 3 => sense_bed(m, w, lr), 4 => sense_hurt(m, w), 5 => sense_hostiles(m, w), @@ -1868,7 +1874,32 @@ impl B { }, r, ), - B::GoToWantedItem(_) => false, + B::GoToWantedItem(r) => one( + || { + let mm = mem(m); + if mm.walk_target.present() { + return false; + } + let Some(at) = mm.nearest_wanted_item.value() else { + return false; + }; + // item.closerThan(body, 4); a villager can pick up loot. + if m.position.distance_squared(Vec3::new(at[0], at[1], at[2])) >= 16.0 { + return false; + } + let mm = mem(m); + // The item's eye height is 0.2125. + mm.look_target + .set(Tracker::Point([at[0], at[1] + 0.2125, at[2]])); + mm.walk_target.set(WalkTarget { + target: Tracker::Point(at), + speed: 0.5, + close: 0, + }); + true + }, + r, + ), B::AcquirePoi { running, filter, @@ -4337,6 +4368,14 @@ pub fn heard_bell(m: &mut Mob, now: i64) { mem(m).heard_bell_time.set(now); } } +/// Whether `NearestItemSensor` is due for this villager (it is then reset). +pub fn take_item_scan(m: &mut Mob) -> bool { + brain(m).is_some() && std::mem::take(&mut brain_mut(m).scan_items) +} +/// `NearestItemSensor.doTick`: the nearest wanted item in sight, or none. +pub fn set_wanted_item(m: &mut Mob, at: Option<[f64; 3]>) { + mem(m).nearest_wanted_item.set_opt(at); +} /// What other villagers see of this one's brain. pub fn potential_job_site(m: &Mob) -> Option<[i32; 3]> { mem_ref(m).and_then(|mm| mm.potential_job_site.value())