Compare villager gossip and breeding with vanilla

Gossip and gifts between villagers (TradeWithVillager) move into the
library so the mob_ai probe runs them as the game does; both villagers now
lock their gaze on each other, as lockGazeAndWalkToEachOther does. The
probe reads the oracle's villager flags, prints villager data (food,
inventory, gossip, reputation) and adds babies as they are born. The
oracle seeds TriggerGate's shuffles too, which were left to the clock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-13 00:27:35 +02:00
parent ad2758fc9c
commit e0a025c5bd
8 changed files with 446 additions and 160 deletions
+17 -95
View File
@@ -59,68 +59,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);
}
}
@@ -269,41 +214,18 @@ impl Game<'_> {
}
/// `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<u32, bool> = 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)
villager::take_vacant_bed(&self.mobs, from, |x, y, z| {
let state = self.world.block(x, y, z);
!self.res.facts.is_air(state)
&& *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")
})
})
})
}
fn particles_at(&mut self, kind: &'static str, at: [f64; 3]) {
if let Some(ty) = horizon_client::particles::ParticleType::from_name(kind) {
+9
View File
@@ -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<Item = (Target, GossipType, i32)> + '_ {
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()])
}
+1
View File
@@ -1054,6 +1054,7 @@ pub fn apply_brain_events(mobs: &mut [Mob], events: &mut Vec<MobEvent>, 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);
}
+103
View File
@@ -536,6 +536,109 @@ pub fn seen(m: &Mob) -> Seen {
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<ItemStack>,
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 no villager has
/// claimed (`free_head`: a bed head that is not occupied).
pub fn take_vacant_bed(
mobs: &[Mob],
from: [i32; 3],
mut free_head: impl FnMut(i32, i32, i32) -> bool,
) -> Option<[i32; 3]> {
let claimed: Vec<[i32; 3]> = mobs
.iter()
.filter(|m| m.species() == Species::Villager)
.filter_map(|m| m.extra.bed)
.collect();
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
@@ -1609,13 +1609,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 +1622,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 +2847,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,7 +2865,7 @@ 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 {
cx.events.push(MobEvent::VillagerBirth {
parent: m.id,
@@ -3366,8 +3361,23 @@ 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) {
/// `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 {