From a79077b30e1fb734615b83c762f6a13defda3b28 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 15:42:20 +0200 Subject: [PATCH] Mob brains share Timed, compute_path, reached and the living-entity scan Behavior's Timed was copied into the villager, animal, monster and warden brains: one copy now, in brain.rs, drawing its duration in Java's int arithmetic (two copies used plain +, the same for every duration they have). MoveToTargetSink's tryComputePath and reachedTarget, copied into the villager, monster and warden brains on the same WalkTarget and Slot types, are the villager brain's, given each brain's CANT_REACH_WALK_TARGET_SINCE. NearestLivingEntitySensor's scan, copied into the villager, animal and monster brains, is brain.rs's living_near. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/src/mobs/animal_brain.rs | 89 ++++--------- crates/horizon-client/src/mobs/brain.rs | 58 ++++++++- .../horizon-client/src/mobs/monster_brain.rs | 116 +++-------------- .../horizon-client/src/mobs/villager_brain.rs | 118 +++++++----------- .../horizon-client/src/mobs/warden_brain.rs | 108 +++------------- 5 files changed, 156 insertions(+), 333 deletions(-) diff --git a/crates/horizon-client/src/mobs/animal_brain.rs b/crates/horizon-client/src/mobs/animal_brain.rs index 331870b4a..bd0eb9c90 100644 --- a/crates/horizon-client/src/mobs/animal_brain.rs +++ b/crates/horizon-client/src/mobs/animal_brain.rs @@ -9,7 +9,12 @@ //! own random drives positions, sounds and jumps. `RunOne` shuffles with a //! random of its own, seeded with the brain's `shuffle_seed` when it is //! built. -use super::{Mob, MobEvent, MobWorld, Species, Who, path::Path, sniffer}; +use super::{ + Mob, MobEvent, MobWorld, Species, Who, + brain::{Timed, living_near}, + path::Path, + sniffer, +}; use horizon_core::{ geometry::Vec3, random::{LegacyRandom, RandomSource}, @@ -603,22 +608,7 @@ fn tick_sensors(m: &mut Mob, w: &MobWorld) { /// `NearestLivingEntitySensor`: everything alive within the follow range, /// nearest first. fn sense_living(m: &mut Mob, w: &MobWorld) { - let r = m.attrs.follow_range; - let area = m.bounds().inflate(Vec3::new(r, r, r)); - let mut found: Vec<(Who, f64)> = Vec::new(); - for o in w.others_near(area) { - if o.id != m.id && o.alive && o.bounds.intersects(area) { - found.push((Who::Mob(o.id), m.distance_sqr(o.position))); - } - } - if let Some(p) = &w.player - && p.alive - && p.bounds.intersects(area) - { - found.push((Who::Player, m.distance_sqr(p.position))); - } - found.sort_by(|a, b| a.1.total_cmp(&b.1)); - let list: Vec = found.into_iter().map(|(w, _)| w).collect(); + let list = living_near(m, w); let mm = mem(m); mm.mobs.set_list(list.clone()); mm.visible_mobs.set(Visible { @@ -794,37 +784,6 @@ fn followable_friendly(s: Species) -> bool { // ---- Behaviours ------------------------------------------------------------ -/// `Behavior`'s running state and end time. -#[derive(Clone, Copy, Debug)] -struct Timed { - running: bool, - end: i64, - min: i32, - max: i32, -} -impl Timed { - fn new(min: i32, max: i32) -> Self { - Self { - running: false, - end: 0, - min, - max, - } - } - /// `Behavior.tryStart`'s duration draw. - fn begin(&mut self, cx: &mut Bx) { - self.running = true; - let bound = self.max.wrapping_add(1).wrapping_sub(self.min); - let d = self - .min - .wrapping_add(cx.lr.next_int_bound(bound).unwrap_or(0)); - self.end = cx.now + i64::from(d); - } - fn timed_out(&self, now: i64) -> bool { - now > self.end - } -} - /// `RunOne`: shuffles its children by weight and starts the first that can. #[derive(Clone, Debug)] struct Gate { @@ -1076,7 +1035,7 @@ impl B { { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); mem(m).walk_target.clear(); mem(m).look_target.clear(); m.add_effect("minecraft:regeneration", 200, 0); @@ -1086,7 +1045,7 @@ impl B { if !swim_condition(m) { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); true } B::AnimalPanic { t, .. } => { @@ -1094,7 +1053,7 @@ impl B { if !(mm.hurt_by.get() == Some(&true) || mm.is_panicking.present()) { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); if brain_mut(m).kind == Kind::Sniffer { reset_sniffing(m, cx); } @@ -1123,7 +1082,7 @@ impl B { let reached = reached(m, cx.w, &walk); if !reached && compute_path(m, cx, &walk, path, speed) { *last_target = walk.target.block(cx.w); - t.begin(cx); + t.begin(cx.lr, cx.now); mem(m).path.set_opt(path.clone()); m.nav_move_to_path(path.clone(), f64::from(*speed)); return true; @@ -1139,21 +1098,21 @@ impl B { if !mem(m).temptation_cooldown_ticks.present() { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); true } B::CountDownGaze(t) => { if !mem(m).gaze_cooldown_ticks.present() { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); true } B::CountDownTransport(t) => { if !mem(m).transport_items_cooldown_ticks.present() { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); true } B::Transport(x) => x.try_start(m, cx), @@ -1171,7 +1130,7 @@ impl B { let Some(partner) = breed_partner(m, cx.w) else { return false; }; - t.begin(cx); + t.begin(cx.lr, cx.now); if brain_mut(m).kind == Kind::Sniffer { reset_sniffing(m, cx); } @@ -1200,7 +1159,7 @@ impl B { { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); if brain_mut(m).kind == Kind::Sniffer { reset_sniffing(m, cx); } @@ -1210,14 +1169,14 @@ impl B { if !mem(m).look_target.present() { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); true } B::FeelingHappy(t) => { if !mem(m).sniffer_happy.present() { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); transition(m, cx, sniffer::FEELING_HAPPY); true } @@ -1233,7 +1192,7 @@ impl B { if !can_sniff(m) { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); transition(m, cx, sniffer::DIGGING); true } @@ -1246,7 +1205,7 @@ impl B { { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); transition(m, cx, sniffer::RISING); true } @@ -1261,7 +1220,7 @@ impl B { if !can_sniff(m) { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); transition(m, cx, sniffer::SEARCHING); true } @@ -1278,7 +1237,7 @@ impl B { if is_tempted(m) { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); transition(m, cx, sniffer::SCENTING); true } @@ -1293,12 +1252,12 @@ impl B { if m.is_baby() || !can_sniff(m) { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); transition(m, cx, sniffer::SNIFFING); true } B::DoNothing(t) => { - t.begin(cx); + t.begin(cx.lr, cx.now); true } B::AxolotlAction { .. } diff --git a/crates/horizon-client/src/mobs/brain.rs b/crates/horizon-client/src/mobs/brain.rs index da83ee93d..67bb6a2a6 100644 --- a/crates/horizon-client/src/mobs/brain.rs +++ b/crates/horizon-client/src/mobs/brain.rs @@ -1,8 +1,11 @@ //! Pieces of vanilla's `Brain` that species implemented as brains share: //! memories that expire, behaviour durations, walk targets and //! `MoveToTargetSink`. -use super::{Mob, goals::Cx}; -use horizon_core::random::RandomSource; +use super::{Mob, MobWorld, Who, goals::Cx}; +use horizon_core::{ + geometry::Vec3, + random::{LegacyRandom, RandomSource}, +}; /// A memory with an expiry (`Brain.setMemoryWithExpiry`): present for the /// tick it was set and `ttl` more ticks. `Ttl::default()` is absent. @@ -25,6 +28,57 @@ impl Ttl { } } } +/// `Behavior`'s bookkeeping in the brains ported behaviour for behaviour +/// (villager, animal, monster, warden): running, and until when. +#[derive(Clone, Copy, Debug)] +pub struct Timed { + pub running: bool, + pub end: i64, + pub min: i32, + pub max: i32, +} +impl Timed { + pub fn new(min: i32, max: i32) -> Self { + Self { + running: false, + end: 0, + min, + max, + } + } + /// `Behavior.tryStart`'s duration draw from the level random, in Java's + /// int arithmetic (made even when it cannot vary). + pub fn begin(&mut self, lr: &mut LegacyRandom, now: i64) { + self.running = true; + let bound = self.max.wrapping_add(1).wrapping_sub(self.min); + let d = self.min.wrapping_add(lr.next_int_bound(bound).unwrap_or(0)); + self.end = now + i64::from(d); + } + /// `Behavior.timedOut`. + pub fn timed_out(&self, now: i64) -> bool { + now > self.end + } +} +/// `NearestLivingEntitySensor`'s scan: the living entities (the player +/// among them) whose boxes meet the follow range around `m`, nearest first. +pub fn living_near(m: &Mob, w: &MobWorld) -> Vec { + let r = m.attrs.follow_range; + let area = m.bounds().inflate(Vec3::new(r, r, r)); + let mut found: Vec<(Who, f64)> = Vec::new(); + for o in w.others_near(area) { + if o.id != m.id && o.alive && o.bounds.intersects(area) { + found.push((Who::Mob(o.id), m.distance_sqr(o.position))); + } + } + if let Some(p) = &w.player + && p.alive + && p.bounds.intersects(area) + { + found.push((Who::Player, m.distance_sqr(p.position))); + } + found.sort_by(|a, b| a.1.total_cmp(&b.1)); + found.into_iter().map(|(w, _)| w).collect() +} /// A random duration in `min..=max` (`Behavior`'s end time). pub fn duration(m: &mut Mob, min: i32, max: i32) -> i32 { if max <= min { diff --git a/crates/horizon-client/src/mobs/monster_brain.rs b/crates/horizon-client/src/mobs/monster_brain.rs index eeb2c42f2..be310cdd7 100644 --- a/crates/horizon-client/src/mobs/monster_brain.rs +++ b/crates/horizon-client/src/mobs/monster_brain.rs @@ -11,9 +11,10 @@ //! seeded when the brain is built. use super::{ Mob, MobEvent, MobWorld, Species, Who, + brain::{Timed, living_near}, goals::Cx, path::Path, - villager_brain::{NEVER, Slot, Tracker, Visible, WalkTarget}, + villager_brain::{NEVER, Slot, Tracker, Visible, WalkTarget, compute_path, reached}, }; use crate::projectiles::{Projectile, ProjectileKind}; use horizon_core::{ @@ -129,6 +130,10 @@ fn brain_mut(m: &mut Mob) -> &mut Brain { fn mem(m: &mut Mob) -> &mut Memories { &mut brain_mut(m).mem } +/// `CANT_REACH_WALK_TARGET_SINCE`, for `compute_path`. +fn cant_reach(m: &mut Mob) -> &mut Slot { + &mut mem(m).cant_reach_walk_target_since +} fn mem_ref(m: &Mob) -> &Memories { &brain(m).expect("brain").mem } @@ -296,22 +301,7 @@ fn tick_sensors(m: &mut Mob, w: &MobWorld) { /// `NearestLivingEntitySensor`: everything alive within the follow range, /// nearest first. fn sense_living(m: &mut Mob, w: &MobWorld) { - let r = m.attrs.follow_range; - let area = m.bounds().inflate(Vec3::new(r, r, r)); - let mut found: Vec<(Who, f64)> = Vec::new(); - for o in w.others_near(area) { - if o.id != m.id && o.alive && o.bounds.intersects(area) { - found.push((Who::Mob(o.id), m.distance_sqr(o.position))); - } - } - if let Some(p) = &w.player - && p.alive - && p.bounds.intersects(area) - { - found.push((Who::Player, m.distance_sqr(p.position))); - } - found.sort_by(|a, b| a.1.total_cmp(&b.1)); - let list: Vec = found.into_iter().map(|(w, _)| w).collect(); + let list = living_near(m, w); let mm = mem(m); mm.mobs.set_list(list.clone()); mm.visible_mobs.set(Visible { @@ -460,31 +450,6 @@ fn sense_breeze_attack(m: &mut Mob, w: &MobWorld) { // ---- Behaviours --------------------------------------------------------- -/// `Behavior`'s bookkeeping: running, and until when. -#[derive(Clone, Debug)] -struct Timed { - running: bool, - end: i64, - min: i32, - max: i32, -} -impl Timed { - fn new(min: i32, max: i32) -> Self { - Self { - running: false, - end: 0, - min, - max, - } - } - /// `Behavior.tryStart`'s duration draw from the level random. - fn begin(&mut self, cx: &mut Bx) { - self.running = true; - let d = self.min + cx.lr.next_int_bound(self.max + 1 - self.min).unwrap_or(0); - self.end = cx.now + i64::from(d); - } -} - /// `StartAttacking`'s target finders. #[derive(Clone, Copy, Debug)] enum Finder { @@ -611,14 +576,14 @@ impl B { if *creaking && !super::creaking::can_move(m) || !swim_condition(m) { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); true } B::LookAtTargetSink(t) => { if !mem(m).look_target.present() { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); true } B::MoveToTargetSink { @@ -640,9 +605,9 @@ impl B { } let walk = mm.walk_target.value().unwrap(); let reached = reached(m, cx.w, &walk); - if !reached && compute_path(m, cx, &walk, path, speed) { + if !reached && compute_path(m, cx.w, cx.now, &walk, path, speed, cant_reach) { *last_target = walk.target.block(cx.w); - t.begin(cx); + t.begin(cx.lr, cx.now); // start mem(m).path.set_opt(path.clone()); m.nav_move_to_path(path.clone(), f64::from(*speed)); @@ -723,7 +688,7 @@ impl B { true } B::DoNothing(t) => { - t.begin(cx); + t.begin(cx.lr, cx.now); true } B::RandomStroll(running, speed) => { @@ -852,7 +817,7 @@ impl B { mem(m).breeze_shoot.clear(); return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); // start super::breeze::set_pose(m, super::breeze::SHOOTING); mem(m).breeze_shoot_charging.set_ttl((), 15); @@ -871,7 +836,7 @@ impl B { if !long_jump_can_run(m, cx) { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); // start if !mem(m).breeze_jump_inhaling.present() { mem(m).breeze_jump_inhaling.set_ttl((), 10); @@ -909,7 +874,7 @@ impl B { if !stuck { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); mem(m).breeze_shoot.set_ttl((), 60); true } @@ -928,7 +893,7 @@ impl B { { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); slide_start(m, cx); true } @@ -993,7 +958,7 @@ impl B { && let Some(now_target) = walk.target.block(cx.w) { let d = manhattan_sq(now_target, last); - if d > 4 && compute_path(m, cx, &walk, path, speed) { + if d > 4 && compute_path(m, cx.w, cx.now, &walk, path, speed, cant_reach) { *last_target = Some(now_target); mem(m).path.set_opt(path.clone()); m.nav_move_to_path(path.clone(), f64::from(*speed)); @@ -1126,53 +1091,6 @@ fn tracker_visible(m: &mut Mob, w: &MobWorld, t: Tracker) -> bool { _ => true, } } -/// `MoveToTargetSink.reachedTarget`. -fn reached(m: &Mob, w: &MobWorld, walk: &WalkTarget) -> bool { - walk.target.block(w).is_some_and(|p| { - let h = block_of(m.position); - (p[0] - h[0]).abs() + (p[1] - h[1]).abs() + (p[2] - h[2]).abs() <= walk.close - }) -} -/// `MoveToTargetSink.tryComputePath`. -fn compute_path( - m: &mut Mob, - cx: &mut Bx, - walk: &WalkTarget, - path: &mut Option, - speed: &mut f32, -) -> bool { - let Some(target) = walk.target.block(cx.w) else { - return false; - }; - *path = m.create_path_to(cx.w, target, 0); - *speed = walk.speed; - if reached(m, cx.w, walk) { - mem(m).cant_reach_walk_target_since.clear(); - } else { - let can_reach = path.as_ref().is_some_and(|p| p.reached); - let mm = mem(m); - if can_reach { - mm.cant_reach_walk_target_since.clear(); - } else if !mm.cant_reach_walk_target_since.present() { - mm.cant_reach_walk_target_since.set(cx.now); - } - if path.is_some() { - return true; - } - let to = [ - f64::from(target[0]) + 0.5, - f64::from(target[1]), - f64::from(target[2]) + 0.5, - ]; - if let Some(step) = - m.default_random_pos_towards(cx.w, 10, 7, to, f64::from(std::f32::consts::FRAC_PI_2)) - { - *path = m.create_path_to(cx.w, block_of(Vec3::new(step[0], step[1], step[2])), 0); - return path.is_some(); - } - } - false -} /// `BehaviorUtils.isWithinAttackRange(body, target, 1)` for a mob without a /// ranged weapon: `Mob.isWithinMeleeAttackRange`. fn within_attack_range(m: &Mob, w: &MobWorld, target: Who) -> bool { diff --git a/crates/horizon-client/src/mobs/villager_brain.rs b/crates/horizon-client/src/mobs/villager_brain.rs index e02cfa176..cdfd49e39 100644 --- a/crates/horizon-client/src/mobs/villager_brain.rs +++ b/crates/horizon-client/src/mobs/villager_brain.rs @@ -11,6 +11,7 @@ //! `Brain::shuffle_seed` when the brain is built. use super::{ Mob, MobEvent, MobWorld, Seen, Species, Who, + brain::{Timed, living_near}, path::{Path, PathNode}, }; use horizon_core::{ @@ -341,6 +342,10 @@ fn brain_mut(m: &mut Mob) -> &mut Brain { fn mem(m: &mut Mob) -> &mut Memories { &mut brain_mut(m).mem } +/// `CANT_REACH_WALK_TARGET_SINCE`, for `compute_path`. +fn cant_reach(m: &mut Mob) -> &mut Slot { + &mut mem(m).cant_reach_walk_target_since +} fn mem_ref(m: &Mob) -> Option<&Memories> { brain(m).map(|b| &b.mem) } @@ -565,22 +570,7 @@ const FOLLOW_RANGE: f64 = 16.0; /// `NearestLivingEntitySensor`: everything alive within the follow range, /// nearest first. fn sense_living(m: &mut Mob, w: &MobWorld) { - let r = m.attrs.follow_range; - let area = m.bounds().inflate(Vec3::new(r, r, r)); - let mut found: Vec<(Who, f64)> = Vec::new(); - for o in w.others_near(area) { - if o.id != m.id && o.alive && o.bounds.intersects(area) { - found.push((Who::Mob(o.id), m.distance_sqr(o.position))); - } - } - if let Some(p) = &w.player - && p.alive - && p.bounds.intersects(area) - { - found.push((Who::Player, m.distance_sqr(p.position))); - } - found.sort_by(|a, b| a.1.total_cmp(&b.1)); - let list: Vec = found.into_iter().map(|(w, _)| w).collect(); + let list = living_near(m, w); let mm = mem(m); mm.mobs.set_list(list.clone()); mm.visible_mobs.set(Visible { @@ -791,34 +781,6 @@ fn sense_golem(m: &mut Mob, w: &MobWorld) { // ---- Behaviours --------------------------------------------------------- -/// `Behavior`'s bookkeeping: running, and until when. -#[derive(Clone, Debug)] -struct Timed { - running: bool, - end: i64, - min: i32, - max: i32, -} -impl Timed { - fn new(min: i32, max: i32) -> Self { - Self { - running: false, - end: 0, - min, - max, - } - } - /// `Behavior.tryStart`'s duration draw. - fn begin(&mut self, cx: &mut Bx) { - self.running = true; - let bound = self.max.wrapping_add(1).wrapping_sub(self.min); - let d = self - .min - .wrapping_add(cx.lr.next_int_bound(bound).unwrap_or(0)); - self.end = cx.now + i64::from(d); - } -} - /// The POI types villagers use. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Poi { @@ -1498,18 +1460,18 @@ impl B { if !swim_condition(m) { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); true } B::LookAtTargetSink(t) => { if !mem(m).look_target.present() { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); true } B::PanicTrigger(t) => { - t.begin(cx); + t.begin(cx.lr, cx.now); // VillagerPanicTrigger.start if panic_condition(m) { if !brain_mut(m).is_active(Act::Panic) { @@ -1542,9 +1504,9 @@ impl B { } let walk = mm.walk_target.value().unwrap(); let reached = reached(m, cx.w, &walk); - if !reached && compute_path(m, cx, &walk, path, speed) { + if !reached && compute_path(m, cx.w, cx.now, &walk, path, speed, cant_reach) { *last_target = walk.target.block(cx.w); - t.begin(cx); + t.begin(cx.lr, cx.now); // start mem(m).path.set_opt(path.clone()); m.nav_move_to_path(path.clone(), f64::from(*speed)); @@ -1561,7 +1523,7 @@ impl B { if !follow_trading_condition(m, cx.w) { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); follow_trading_player(m); true } @@ -1573,7 +1535,7 @@ impl B { if !matches!(a, Act::Idle | Act::Work | Act::Play) { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); true } B::JumpOnBed { @@ -1590,7 +1552,7 @@ impl B { if mm.walk_target.present() || !m.is_baby() { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); *bed = Some(nearest); *time_left = 100; *jumps = 3 + cx.lr.next_int_bound(4).unwrap_or(0); @@ -1599,7 +1561,7 @@ impl B { true } B::DoNothing(t) => { - t.begin(cx); + t.begin(cx.lr, cx.now); true } B::GiveGiftToHero { @@ -1618,7 +1580,7 @@ impl B { *until_next -= 1; return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); *given = false; *started = now; let mm = mem(m); @@ -1630,7 +1592,7 @@ impl B { if !mem(m).interaction_target.present() || !show_trades_condition(m, cx.w) { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); // start if let Some(target) = mem(m).interaction_target.value() { mem(m).look_target.set(Tracker::Entity(target, true)); @@ -1646,7 +1608,7 @@ impl B { if !villager_target_valid(m, cx.w, Mem::InteractionTarget) { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); let target = mem(m).interaction_target.value().unwrap(); lock_gaze(m, target, cx); true @@ -1659,7 +1621,7 @@ impl B { if !breeding_possible(m, cx.w) { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); let target = mem(m).breed_target.value().unwrap(); lock_gaze(m, target, cx); *birth = now + 275 + i64::from(m.random.next_int_bound(50).unwrap_or(0)); @@ -1680,7 +1642,7 @@ impl B { if !closer_to_center(site, m.position, 1.73) { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); // start let mm = mem(m); mm.last_worked_at_poi.set(now); @@ -1709,7 +1671,7 @@ impl B { *valid = farmland_around(m, cx.w); *target = pick_farmland(valid, cx); let Some(p) = *target else { return false }; - t.begin(cx); + t.begin(cx.lr, cx.now); if now > *next_ok { let mm = mem(m); mm.look_target.set(Tracker::Block(p)); @@ -2288,7 +2250,7 @@ impl B { { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); if now > *next_ok { if super::villager::start_sleeping(m, home, w) { mem(m).last_slept.set(now); @@ -2609,7 +2571,7 @@ impl B { { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); true } B::LocateHidingPlace { @@ -2769,7 +2731,7 @@ impl B { && let Some(now_target) = walk.target.block(cx.w) { let d = manhattan_sq(now_target, last); - if d > 4 && compute_path(m, cx, &walk, path, speed) { + if d > 4 && compute_path(m, cx.w, cx.now, &walk, path, speed, cant_reach) { *last_target = Some(now_target); mem(m).path.set_opt(path.clone()); m.nav_move_to_path(path.clone(), f64::from(*speed)); @@ -3141,33 +3103,37 @@ fn tracker_visible(m: &mut Mob, w: &MobWorld, t: Tracker) -> bool { } } /// `MoveToTargetSink.reachedTarget`. -fn reached(m: &Mob, w: &MobWorld, walk: &WalkTarget) -> bool { +pub(super) fn reached(m: &Mob, w: &MobWorld, walk: &WalkTarget) -> bool { walk.target .block(w) .is_some_and(|p| manhattan(p, block_of(m.position)) <= walk.close) } -/// `MoveToTargetSink.tryComputePath`. -fn compute_path( +/// `MoveToTargetSink.tryComputePath`, for the brains on these memories +/// (villager, monster, warden): `cant_reach` is the brain's +/// `CANT_REACH_WALK_TARGET_SINCE`. +pub(super) fn compute_path( m: &mut Mob, - cx: &mut Bx, + w: &MobWorld, + now: i64, walk: &WalkTarget, path: &mut Option, speed: &mut f32, + cant_reach: fn(&mut Mob) -> &mut Slot, ) -> bool { - let Some(target) = walk.target.block(cx.w) else { + let Some(target) = walk.target.block(w) else { return false; }; - *path = m.create_path_to(cx.w, target, 0); + *path = m.create_path_to(w, target, 0); *speed = walk.speed; - if reached(m, cx.w, walk) { - mem(m).cant_reach_walk_target_since.clear(); + if reached(m, w, walk) { + cant_reach(m).clear(); } else { let can_reach = path.as_ref().is_some_and(|p| p.reached); - let mm = mem(m); + let since = cant_reach(m); if can_reach { - mm.cant_reach_walk_target_since.clear(); - } else if !mm.cant_reach_walk_target_since.present() { - mm.cant_reach_walk_target_since.set(cx.now); + since.clear(); + } else if !since.present() { + since.set(now); } if path.is_some() { return true; @@ -3178,14 +3144,14 @@ fn compute_path( f64::from(target[2]) + 0.5, ]; if let Some(step) = - m.default_random_pos_towards(cx.w, 10, 7, to, f64::from(std::f32::consts::FRAC_PI_2)) + m.default_random_pos_towards(w, 10, 7, to, f64::from(std::f32::consts::FRAC_PI_2)) { let p = [ step[0].floor() as i32, step[1].floor() as i32, step[2].floor() as i32, ]; - *path = m.create_path_to(cx.w, p, 0); + *path = m.create_path_to(w, p, 0); return path.is_some(); } } diff --git a/crates/horizon-client/src/mobs/warden_brain.rs b/crates/horizon-client/src/mobs/warden_brain.rs index 518347e77..8caafefc9 100644 --- a/crates/horizon-client/src/mobs/warden_brain.rs +++ b/crates/horizon-client/src/mobs/warden_brain.rs @@ -14,9 +14,10 @@ //! shuffle seed. use super::{ Mob, MobEvent, MobWorld, Who, + brain::Timed, goals::Cx, path::Path, - villager_brain::{Slot, Tracker, Visible, WalkTarget}, + villager_brain::{Slot, Tracker, Visible, WalkTarget, compute_path, reached}, warden::{self, Pose}, }; use horizon_core::{ @@ -145,31 +146,6 @@ impl Memories { } } -/// `Behavior`'s bookkeeping: running, and until when. -#[derive(Clone, Debug)] -struct Timed { - running: bool, - end: i64, - min: i32, - max: i32, -} -impl Timed { - fn new(min: i32, max: i32) -> Self { - Self { - running: false, - end: 0, - min, - max, - } - } - /// `Behavior.tryStart`'s duration draw (made even when it cannot vary). - fn begin(&mut self, cx: &mut Bx) { - self.running = true; - let d = self.min + cx.lr.next_int_bound(self.max + 1 - self.min).unwrap_or(0); - self.end = cx.now + i64::from(d); - } -} - /// `RunOne`'s `ShufflingList` over a stroll (weight 2) and `DoNothing` (1). #[derive(Clone, Debug)] struct Gate { @@ -383,6 +359,10 @@ fn brain(m: &Mob) -> Option<&Brain> { pub(super) fn mem(m: &mut Mob) -> &mut Memories { &mut m.extra.warden.as_mut().expect("a warden").brain.mem } +/// `CANT_REACH_WALK_TARGET_SINCE`, for `compute_path`. +fn cant_reach(m: &mut Mob) -> &mut Slot { + &mut mem(m).cant_reach_walk_target_since +} pub(super) fn mem_ref(m: &Mob) -> Option<&Memories> { brain(m).map(|b| &b.mem) } @@ -558,9 +538,6 @@ fn find_closest(m: &mut Mob, w: &MobWorld, filter: impl Fn(&Mob, Who) -> bool) - fn block_of(p: Vec3) -> [i32; 3] { [p.x.floor() as i32, p.y.floor() as i32, p.z.floor() as i32] } -fn manhattan(a: [i32; 3], b: [i32; 3]) -> i32 { - (a[0] - b[0]).abs() + (a[1] - b[1]).abs() + (a[2] - b[2]).abs() -} fn dist_sqr(a: [i32; 3], b: [i32; 3]) -> i64 { (0..3).map(|i| i64::from(a[i] - b[i]).pow(2)).sum() } @@ -607,57 +584,6 @@ fn tracker_visible(m: &mut Mob, w: &MobWorld, t: Tracker) -> bool { _ => true, } } -/// `MoveToTargetSink.reachedTarget`. -fn reached(m: &Mob, w: &MobWorld, walk: &WalkTarget) -> bool { - walk.target - .block(w) - .is_some_and(|p| manhattan(p, block_of(m.position)) <= walk.close) -} -/// `MoveToTargetSink.tryComputePath`. -fn compute_path( - m: &mut Mob, - cx: &mut Bx, - walk: &WalkTarget, - path: &mut Option, - speed: &mut f32, -) -> bool { - let Some(target) = walk.target.block(cx.w) else { - return false; - }; - *path = m.create_path_to(cx.w, target, 0); - *speed = walk.speed; - if reached(m, cx.w, walk) { - mem(m).cant_reach_walk_target_since.clear(); - } else { - let can_reach = path.as_ref().is_some_and(|p| p.reached); - let mm = mem(m); - if can_reach { - mm.cant_reach_walk_target_since.clear(); - } else if !mm.cant_reach_walk_target_since.present() { - mm.cant_reach_walk_target_since.set(cx.now); - } - if path.is_some() { - return true; - } - let to = [ - f64::from(target[0]) + 0.5, - f64::from(target[1]), - f64::from(target[2]) + 0.5, - ]; - if let Some(step) = - m.default_random_pos_towards(cx.w, 10, 7, to, f64::from(std::f32::consts::FRAC_PI_2)) - { - let p = [ - step[0].floor() as i32, - step[1].floor() as i32, - step[2].floor() as i32, - ]; - *path = m.create_path_to(cx.w, p, 0); - return path.is_some(); - } - } - false -} fn same_path(a: Option<&Path>, b: Option<&Path>) -> bool { match (a, b) { (None, None) => true, @@ -710,7 +636,7 @@ impl B { if !swim_condition(m) { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); true } B::SetWardenLookTarget(r) => one(r, || { @@ -732,7 +658,7 @@ impl B { if !mem(m).look_target.present() { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); true } B::MoveToTargetSink { @@ -755,9 +681,9 @@ impl B { return false; }; let reached = reached(m, cx.w, &walk); - if !reached && compute_path(m, cx, &walk, path, speed) { + if !reached && compute_path(m, cx.w, cx.now, &walk, path, speed, cant_reach) { *last_target = walk.target.block(cx.w); - t.begin(cx); + t.begin(cx.lr, cx.now); // start mem(m).path.set_opt(path.clone()); m.nav_move_to_path(path.clone(), f64::from(*speed)); @@ -775,7 +701,7 @@ impl B { if !mm.is_emerging.present() || mm.walk_target.present() { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); warden::set_pose(m, Pose::Emerging); sound(m, "entity.warden.emerge", 5.0, 1.0, cx.events); true @@ -791,7 +717,7 @@ impl B { if !(m.on_ground || liquid) { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); if m.on_ground { warden::set_pose(m, Pose::Digging); sound(m, "entity.warden.dig", 5.0, 1.0, cx.events); @@ -872,7 +798,7 @@ impl B { true }), B::DoNothing(t) => { - t.begin(cx); + t.begin(cx.lr, cx.now); true } B::GoToDisturbance(r) => one(r, || { @@ -907,7 +833,7 @@ impl B { { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); sound(m, "entity.warden.sniff", 5.0, 1.0, cx.events); true } @@ -919,7 +845,7 @@ impl B { let Some(target) = mm.roar_target.value() else { return false; }; - t.begin(cx); + t.begin(cx.lr, cx.now); let mm = mem(m); mm.roar_sound_delay.set_ttl((), ROAR_SOUND_DELAY); mm.walk_target.clear(); @@ -1004,7 +930,7 @@ impl B { if !close { return false; } - t.begin(cx); + t.begin(cx.lr, cx.now); let mm = mem(m); mm.attack_cooling_down .set_ttl(true, i64::from(SONIC_DURATION)); @@ -1092,7 +1018,7 @@ impl B { && let Some(walk) = mem(m).walk_target.value() && let Some(now_target) = walk.target.block(cx.w) && dist_sqr(now_target, last) > 4 - && compute_path(m, cx, &walk, path, speed) + && compute_path(m, cx.w, cx.now, &walk, path, speed, cant_reach) { *last_target = Some(now_target); mem(m).path.set_opt(path.clone());