Port the sniffer's and the ghastling's brains
SnifferAi and HappyGhastAi as vanilla's Brain runs them: the sensors and their scan rates, the activities and their requirements, and every behaviour (AnimalPanic, MoveToTargetSink, FollowTemptation, AnimalMakeLove, LookAtTargetSink, RunOne and the sniffer's scenting, sniffing, searching, digging, rising and happy states) in availableBehaviorsByPriority order. Adds AirAndWaterRandomPos and the mob AI scenarios for llamas, sniffers and happy ghasts. The brain is not ticked yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -850,6 +850,11 @@ impl Game<'_> {
|
||||
}
|
||||
},
|
||||
MobEvent::Breed { parent, partner } => self.breed(parent, partner),
|
||||
MobEvent::BreedTarget { mob, partner } => {
|
||||
if let Some(m) = self.mobs.iter_mut().find(|m| m.id == mob) {
|
||||
horizon_client::mobs::animal_brain::set_breed_partner(m, partner);
|
||||
}
|
||||
}
|
||||
MobEvent::LeashBreak { mob, drop, sound } => self.break_leash(mob, drop, sound),
|
||||
MobEvent::Vibration { from, to, ticks } => {
|
||||
self.mob_vibration_particle(from, to, ticks)
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
pub mod ai;
|
||||
pub mod allay;
|
||||
pub mod anim;
|
||||
pub mod animal_brain;
|
||||
pub mod aquatic;
|
||||
pub mod body_anim;
|
||||
pub mod brain;
|
||||
@@ -537,6 +538,8 @@ pub struct Extra {
|
||||
pub raider: Option<Box<raider::State>>,
|
||||
/// A villager's raid activity and gossip.
|
||||
pub villager: Option<Box<villager::Ext>>,
|
||||
/// The sniffer's and the ghastling's brain.
|
||||
pub animal_brain: Option<Box<animal_brain::Brain>>,
|
||||
/// The mob this one rides, and the mob riding it (`getVehicle`, `getFirstPassenger`).
|
||||
pub mount: Option<u32>,
|
||||
pub rider: Option<u32>,
|
||||
@@ -620,6 +623,12 @@ pub struct Mob {
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum MobEvent {
|
||||
Sound(&'static str, [f64; 3], f32, f32),
|
||||
/// `AnimalMakeLove`: the partner remembers this mob as its breed target
|
||||
/// and walks to it and looks at it.
|
||||
BreedTarget {
|
||||
mob: u32,
|
||||
partner: u32,
|
||||
},
|
||||
/// A sound event id that is not a static string (variant sounds).
|
||||
SoundId(String, [f64; 3], f32, f32),
|
||||
AttackPlayer(f32, [f64; 3]),
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -304,6 +304,100 @@ impl Mob {
|
||||
let water = w.fluid(p[0], p[1], p[2]) == crate::blocks::Fluid::Water;
|
||||
(!water && self.malus(w.path_type_static(p[0], p[1], p[2])) == 0.0).then_some(p)
|
||||
}
|
||||
/// The home a mob stays near (`Mob.getHomePosition` / `getHomeRadius`):
|
||||
/// only happy ghasts keep one here.
|
||||
fn home(&self) -> Option<([i32; 3], i32)> {
|
||||
if self.species() != Species::HappyGhast {
|
||||
return None;
|
||||
}
|
||||
self.extra.home.map(|h| (h, self.extra.ints[4]))
|
||||
}
|
||||
/// `Entity.getViewVector(0)`: `calculateViewVector(xRotO, yRotO)`.
|
||||
pub fn view_vector(&self) -> [f64; 3] {
|
||||
use horizon_core::geometry::trig::Mth;
|
||||
let to_rad = std::f32::consts::PI / 180.0;
|
||||
let x = self.previous_head_pitch * to_rad;
|
||||
let y = -self.previous_yaw * to_rad;
|
||||
let (y_cos, y_sin, x_cos, x_sin) = (y.mcos(), y.msin(), x.mcos(), x.msin());
|
||||
[
|
||||
f64::from(y_sin * x_cos),
|
||||
f64::from(-x_sin),
|
||||
f64::from(y_cos * x_cos),
|
||||
]
|
||||
}
|
||||
/// AirAndWaterRandomPos.getPos: within the radians of a direction, kept
|
||||
/// near home, moved up out of solid blocks and free of path maluses.
|
||||
pub fn air_and_water_random_pos(
|
||||
&mut self,
|
||||
w: &MobWorld,
|
||||
h: i32,
|
||||
v: i32,
|
||||
fly_height: i32,
|
||||
xd: f64,
|
||||
zd: f64,
|
||||
) -> Option<[f64; 3]> {
|
||||
let home = self.home();
|
||||
let at = self.position;
|
||||
let within = |c: [i32; 3], r: f64, p: [f64; 3]| {
|
||||
let d = |i: usize| f64::from(c[i]) + 0.5 - p[i];
|
||||
d(0) * d(0) + d(1) * d(1) + d(2) * d(2) < r * r
|
||||
};
|
||||
// GoalUtils.mobRestricted
|
||||
let restrict = home
|
||||
.is_some_and(|(c, r)| within(c, f64::from(r) + f64::from(h) + 1.0, [at.x, at.y, at.z]));
|
||||
self.pick_best(w, |m| {
|
||||
let d = m.random_direction_within(
|
||||
0.0,
|
||||
f64::from(h),
|
||||
v,
|
||||
fly_height,
|
||||
xd,
|
||||
zd,
|
||||
std::f64::consts::FRAC_PI_2,
|
||||
)?;
|
||||
// RandomPos.generateRandomPosTowardDirection, toward home.
|
||||
let (mut xt, mut zt) = (f64::from(d[0]), f64::from(d[2]));
|
||||
if let Some((c, _)) = home
|
||||
&& h > 1
|
||||
{
|
||||
let half = f64::from(h) / 2.0;
|
||||
if m.position.x > f64::from(c[0]) {
|
||||
xt -= m.random.next_double() * half;
|
||||
} else {
|
||||
xt += m.random.next_double() * half;
|
||||
}
|
||||
if m.position.z > f64::from(c[2]) {
|
||||
zt -= m.random.next_double() * half;
|
||||
} else {
|
||||
zt += m.random.next_double() * half;
|
||||
}
|
||||
}
|
||||
let mut p = [
|
||||
(xt + m.position.x).floor() as i32,
|
||||
(f64::from(d[1]) + m.position.y).floor() as i32,
|
||||
(zt + m.position.z).floor() as i32,
|
||||
];
|
||||
if p[1] < crate::chunk::MIN_Y || p[1] >= 320 {
|
||||
return None;
|
||||
}
|
||||
// GoalUtils.isRestricted: Mob.isWithinHome(BlockPos).
|
||||
if restrict && let Some((c, r)) = home {
|
||||
let dd = |i: usize| i64::from(c[i] - p[i]);
|
||||
let r = i64::from(r);
|
||||
if dd(0) * dd(0) + dd(1) * dd(1) + dd(2) * dd(2) >= r * r {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
// RandomPos.moveUpOutOfSolid
|
||||
if w.solid(p[0], p[1], p[2]) {
|
||||
p[1] += 1;
|
||||
while p[1] <= 319 && w.solid(p[0], p[1], p[2]) {
|
||||
p[1] += 1;
|
||||
}
|
||||
}
|
||||
(m.malus(w.path_type_static(p[0], p[1], p[2])) == 0.0).then_some(p)
|
||||
})
|
||||
}
|
||||
/// LandRandomPos.getPos
|
||||
pub fn land_random_pos(&mut self, w: &MobWorld, h: i32, v: i32) -> Option<[f64; 3]> {
|
||||
self.pick_best(w, |m| {
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
//! ghastling follows players and adults, a harnessed adult can be ridden and
|
||||
//! flown, healing is slow (fast in the rain) and `HappyGhastModel`.
|
||||
use super::{
|
||||
Mob, MobEvent, MobWorld, Species,
|
||||
Mob, MobEvent, MobWorld,
|
||||
anim::{Poses, State},
|
||||
control::{MoveKind, Operation},
|
||||
goals::{Cx, Float, Goal, LOOK, MOVE, Panic, Stroll, Tempt, goal_clone},
|
||||
goals::{Cx, Float, Goal, LOOK, MOVE, goal_clone},
|
||||
nav::NavKind,
|
||||
};
|
||||
use horizon_core::{
|
||||
@@ -25,8 +25,11 @@ pub const AIR_SPEED: f32 = 0.05 * 5.0 / 3.0;
|
||||
pub fn register(m: &mut Mob) {
|
||||
setup(m, m.is_baby());
|
||||
}
|
||||
/// `adultGhastSetup` and `babyGhastSetup`.
|
||||
/// `adultGhastSetup` and `babyGhastSetup`: the ghastling flies with a
|
||||
/// `FlyingMoveControl` and has no goals (its brain drives it); the adult
|
||||
/// floats with `GhastMoveControl` and three goals, its brain stopped.
|
||||
fn setup(m: &mut Mob, baby: bool) {
|
||||
let was = m.extra.scratch[0];
|
||||
m.extra.scratch[0] = if baby { 1 } else { 2 };
|
||||
m.ai.goals.clear();
|
||||
m.ai.nav.stop();
|
||||
@@ -36,20 +39,15 @@ fn setup(m: &mut Mob, baby: bool) {
|
||||
max_turn: 180.0,
|
||||
hovers: true,
|
||||
};
|
||||
// BabyFlyingPathNavigation.
|
||||
m.ai.nav.kind = NavKind::Flying;
|
||||
m.ai.nav.can_float = true;
|
||||
m.ai.nav.required_path_length = 48.0;
|
||||
let tempt = m.kind.food.clone();
|
||||
let g = &mut m.ai.goals;
|
||||
g.add(0, Float);
|
||||
g.add(0, Panic::new(2.0));
|
||||
let mut t = Tempt::new(1.25, tempt, false);
|
||||
t.stop_distance = 3.0;
|
||||
g.add(1, t);
|
||||
g.add(2, FollowAdult::default());
|
||||
g.add(4, Stroll::flying(1.0));
|
||||
return;
|
||||
}
|
||||
if was == 1 {
|
||||
super::animal_brain::grown_up(m);
|
||||
}
|
||||
m.ai.mov.kind = MoveKind::Custom;
|
||||
m.ai.nav.kind = NavKind::Ground;
|
||||
let g = &mut m.ai.goals;
|
||||
@@ -274,53 +272,6 @@ impl Goal for FloatAround {
|
||||
m.ai.mov.set_wanted(t.x, t.y, t.z, 1.0);
|
||||
}
|
||||
}
|
||||
/// `BabyFollowAdult`: a ghastling drifts toward the nearest player or adult
|
||||
/// until it is a few blocks away.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
struct FollowAdult {
|
||||
close: f64,
|
||||
}
|
||||
impl FollowAdult {
|
||||
fn target(m: &Mob, cx: &Cx) -> Option<Vec3> {
|
||||
let player =
|
||||
cx.w.player
|
||||
.as_ref()
|
||||
.filter(|p| p.alive && !p.spectator && m.distance_sqr(p.position) <= 32.0 * 32.0)
|
||||
.map(|p| p.position);
|
||||
player.or_else(|| {
|
||||
cx.w.nearby(m.position, 32.0, |s| {
|
||||
s.species == Species::HappyGhast && !s.baby
|
||||
})
|
||||
.next()
|
||||
.map(|s| s.position)
|
||||
})
|
||||
}
|
||||
}
|
||||
impl Goal for FollowAdult {
|
||||
goal_clone!();
|
||||
fn flags(&self) -> u8 {
|
||||
MOVE
|
||||
}
|
||||
fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
|
||||
m.is_baby()
|
||||
&& Self::target(m, cx).is_some_and(|t| {
|
||||
let close = if self.close > 0.0 { self.close } else { 16.0 };
|
||||
m.position.distance_squared(t) > close * close
|
||||
})
|
||||
}
|
||||
fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
|
||||
Self::target(m, cx).is_some_and(|t| m.position.distance_squared(t) > 9.0)
|
||||
}
|
||||
fn start(&mut self, m: &mut Mob, _: &mut Cx) {
|
||||
// UniformInt.of(3, 16).
|
||||
self.close = f64::from(3 + m.random.next_int_bound(14).unwrap_or(0));
|
||||
}
|
||||
fn tick(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
if let Some(t) = Self::target(m, cx) {
|
||||
m.nav_move_to(cx.w, [t.x, t.y, t.z], 1.1);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// What a rider asks (`getRiddenInput`, `tickRidden`).
|
||||
pub fn ridden(m: &mut Mob) {
|
||||
let Some(r) = m.rider else { return };
|
||||
|
||||
@@ -3,13 +3,10 @@
|
||||
//! digging up a seed, the explored positions, laying eggs when bred, and
|
||||
//! `SnifferModel`'s keyframe animations.
|
||||
use super::{
|
||||
Mob, MobEvent, MobWorld,
|
||||
Mob, MobEvent,
|
||||
anim::{Poses, State},
|
||||
brain::{Sink, Ttl, WalkTarget, duration},
|
||||
goals::{Breed, Cx, Float, Goal, Panic, Tempt, goal_clone},
|
||||
keyframes::Animation,
|
||||
};
|
||||
use horizon_core::random::RandomSource;
|
||||
use std::collections::HashMap;
|
||||
|
||||
// Sniffer state: `variant` the state (below), `scratch[0]` the state the
|
||||
@@ -24,15 +21,14 @@ pub const SEARCHING: u8 = 4;
|
||||
pub const DIGGING: u8 = 5;
|
||||
pub const RISING: u8 = 6;
|
||||
|
||||
const SNIFF_COOLDOWN: i32 = 9600;
|
||||
/// `Sniffer.getBabyStartAge`.
|
||||
pub const BABY_START_AGE: i32 = -48000;
|
||||
|
||||
pub fn state(m: &Mob) -> u8 {
|
||||
m.extra.variant
|
||||
}
|
||||
fn set_state(m: &mut Mob, s: u8) {
|
||||
m.extra.variant = s;
|
||||
fn now(m: &Mob) -> i32 {
|
||||
m.age as i32
|
||||
}
|
||||
/// `Sniffer.getDefaultDimensions`: lower while digging.
|
||||
pub fn digging(m: &Mob) -> bool {
|
||||
@@ -44,468 +40,36 @@ pub fn register(m: &mut Mob) {
|
||||
m.set_malus(PathType::Water, -1.0);
|
||||
m.set_malus(PathType::OnTopOfPowderSnow, -1.0);
|
||||
m.set_malus(PathType::DamageCautious, -1.0);
|
||||
let tempt = m.kind.tempt.clone();
|
||||
let g = &mut m.ai.goals;
|
||||
g.add(0, Float);
|
||||
g.add(0, SnifferPanic(Panic::new(2.0)));
|
||||
g.add(
|
||||
0,
|
||||
Brain {
|
||||
walk: None,
|
||||
sink: Sink::new(500, 700),
|
||||
look: None,
|
||||
look_until: 0,
|
||||
digging: false,
|
||||
happy: false,
|
||||
cooldown: Ttl::default(),
|
||||
sniff_target: None,
|
||||
run: Run::None,
|
||||
until: 0,
|
||||
breed: Breed::new(1.0),
|
||||
tempt: Tempt::new(1.25, tempt, false),
|
||||
love_on: false,
|
||||
tempt_on: false,
|
||||
},
|
||||
);
|
||||
}
|
||||
/// The core's `AnimalPanic`, forgetting any sniffing when it starts.
|
||||
#[derive(Clone, Debug)]
|
||||
struct SnifferPanic(Panic);
|
||||
impl Goal for SnifferPanic {
|
||||
goal_clone!();
|
||||
fn flags(&self) -> u8 {
|
||||
self.0.flags()
|
||||
}
|
||||
fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
|
||||
self.0.can_use(m, cx)
|
||||
}
|
||||
fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
|
||||
self.0.can_continue(m, cx)
|
||||
}
|
||||
fn start(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
set_state(m, IDLING);
|
||||
self.0.start(m, cx)
|
||||
}
|
||||
fn stop(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
self.0.stop(m, cx)
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum Run {
|
||||
None,
|
||||
Digging,
|
||||
FinishedDigging,
|
||||
Searching,
|
||||
Happy,
|
||||
Scenting,
|
||||
Sniffing,
|
||||
DoNothing,
|
||||
}
|
||||
#[derive(Clone, Debug)]
|
||||
struct Brain {
|
||||
walk: Option<WalkTarget>,
|
||||
sink: Sink,
|
||||
/// `LOOK_TARGET` and the end of `LookAtTargetSink`'s run (0 when not running).
|
||||
look: Option<[f64; 3]>,
|
||||
look_until: i32,
|
||||
/// `SNIFFER_DIGGING`, `SNIFFER_HAPPY`, `SNIFF_COOLDOWN`, `SNIFFER_SNIFFING_TARGET`.
|
||||
digging: bool,
|
||||
happy: bool,
|
||||
cooldown: Ttl,
|
||||
sniff_target: Option<[i32; 3]>,
|
||||
/// The timed behaviour running and when it times out.
|
||||
run: Run,
|
||||
until: i32,
|
||||
breed: Breed,
|
||||
tempt: Tempt,
|
||||
love_on: bool,
|
||||
tempt_on: bool,
|
||||
}
|
||||
impl Goal for Brain {
|
||||
goal_clone!();
|
||||
fn flags(&self) -> u8 {
|
||||
0
|
||||
}
|
||||
fn can_use(&mut self, _: &mut Mob, _: &mut Cx) -> bool {
|
||||
true
|
||||
}
|
||||
fn every_tick(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn tick(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
self.brain(m, cx);
|
||||
}
|
||||
}
|
||||
fn now(m: &Mob) -> i32 {
|
||||
m.age as i32
|
||||
}
|
||||
fn sound(m: &Mob, cx: &mut Cx, name: &'static str, pitch: f32) {
|
||||
cx.events
|
||||
.push(MobEvent::Sound(name, m.center(), 1.0, pitch));
|
||||
/// `Sniffer.setState`.
|
||||
pub fn set_state(m: &mut Mob, s: u8) {
|
||||
m.extra.variant = s;
|
||||
}
|
||||
/// `Sniffer.getOnPos`: the block under its feet.
|
||||
fn on_pos(m: &Mob) -> [i32; 3] {
|
||||
pub fn on_pos(m: &Mob) -> [i32; 3] {
|
||||
[
|
||||
m.position.x.floor() as i32,
|
||||
(m.position.y - 0.2).floor() as i32,
|
||||
(m.position.y - f64::from(0.2_f32)).floor() as i32,
|
||||
m.position.z.floor() as i32,
|
||||
]
|
||||
}
|
||||
/// `Sniffer.getHeadBlock`: 2.25 blocks ahead of it, a little above its feet.
|
||||
/// `Sniffer.getHeadBlock`: 2.25 blocks along `getForward()`, at its feet
|
||||
/// plus 0.2.
|
||||
pub fn head_block(m: &Mob) -> [i32; 3] {
|
||||
let (yaw, pitch) = (m.yaw.to_radians(), m.head_pitch.to_radians());
|
||||
let forward = [
|
||||
f64::from(-yaw.sin() * pitch.cos()),
|
||||
f64::from(-pitch.sin()),
|
||||
f64::from(yaw.cos() * pitch.cos()),
|
||||
];
|
||||
use horizon_core::geometry::trig::Mth;
|
||||
// Vec3.directionFromRotation(xRot, yRot).
|
||||
let to_rad = std::f32::consts::PI / 180.0;
|
||||
let y = -m.yaw * to_rad - std::f32::consts::PI;
|
||||
let x = -m.head_pitch * to_rad;
|
||||
let (y_cos, y_sin) = (y.mcos(), y.msin());
|
||||
let x_cos = -x.mcos();
|
||||
let forward = [f64::from(y_sin * x_cos), f64::from(y_cos * x_cos)];
|
||||
[
|
||||
(m.position.x + forward[0] * 2.25).floor() as i32,
|
||||
(m.position.y + f64::from(0.2_f32)).floor() as i32,
|
||||
(m.position.z + forward[2] * 2.25).floor() as i32,
|
||||
(m.position.z + forward[1] * 2.25).floor() as i32,
|
||||
]
|
||||
}
|
||||
impl Brain {
|
||||
fn tempted(&self) -> bool {
|
||||
self.tempt_on
|
||||
}
|
||||
/// `Sniffer.canSniff`.
|
||||
fn can_sniff(&self, m: &Mob) -> bool {
|
||||
!self.tempted()
|
||||
&& !m.ai.panicking
|
||||
&& !m.fluids.in_water
|
||||
&& m.extra.in_love <= 0
|
||||
&& m.on_ground
|
||||
&& m.rider.is_none()
|
||||
&& m.extra.leash.is_none()
|
||||
}
|
||||
/// `Sniffer.canDig(BlockPos)`: diggable, unexplored and reachable.
|
||||
fn can_dig_at(&self, m: &mut Mob, w: &MobWorld, p: [i32; 3]) -> bool {
|
||||
w.has_tag(
|
||||
"minecraft:sniffer_diggable_block",
|
||||
w.block_name(p[0], p[1], p[2]),
|
||||
) && !m.extra.positions.contains(&p)
|
||||
&& m.create_path_to(w, p, 1).is_some_and(|path| path.reached)
|
||||
}
|
||||
/// `Sniffer.canDig`.
|
||||
fn can_dig(&self, m: &mut Mob, w: &MobWorld) -> bool {
|
||||
if m.ai.panicking
|
||||
|| self.tempted()
|
||||
|| m.is_baby()
|
||||
|| m.fluids.in_water
|
||||
|| !m.on_ground
|
||||
|| m.rider.is_some()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
let head = head_block(m);
|
||||
self.can_dig_at(m, w, [head[0], head[1] - 1, head[2]])
|
||||
}
|
||||
/// `Sniffer.calculateDigPosition`.
|
||||
fn dig_position(&self, m: &mut Mob, w: &MobWorld) -> Option<[i32; 3]> {
|
||||
for idx in 0..5 {
|
||||
let Some(p) = m.land_random_pos(w, 10 + 2 * idx, 3) else {
|
||||
continue;
|
||||
};
|
||||
let below = [
|
||||
p[0].floor() as i32,
|
||||
p[1].floor() as i32 - 1,
|
||||
p[2].floor() as i32,
|
||||
];
|
||||
if self.can_dig_at(m, w, below) {
|
||||
return Some(below);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
fn transition(&mut self, m: &mut Mob, cx: &mut Cx, to: u8) {
|
||||
match to {
|
||||
FEELING_HAPPY => sound(m, cx, "entity.sniffer.happy", 1.0),
|
||||
SCENTING => {
|
||||
let pitch = if m.is_baby() { 1.3 } else { 1.0 };
|
||||
sound(m, cx, "entity.sniffer.scenting", pitch)
|
||||
}
|
||||
SNIFFING => sound(m, cx, "entity.sniffer.sniffing", 1.0),
|
||||
DIGGING => m.extra.ints[3] = now(m) + 120,
|
||||
RISING => sound(m, cx, "entity.sniffer.digging_stop", 1.0),
|
||||
_ => {}
|
||||
}
|
||||
set_state(m, to);
|
||||
}
|
||||
/// `SnifferAi.resetSniffing`.
|
||||
fn reset_sniffing(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
self.digging = false;
|
||||
self.sniff_target = None;
|
||||
self.transition(m, cx, IDLING);
|
||||
}
|
||||
fn brain(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
self.cooldown.tick();
|
||||
// Tempted: the tempting player memory while the goal runs.
|
||||
if m.ai.panicking {
|
||||
self.walk = None;
|
||||
self.sink.stop(m, &mut self.walk);
|
||||
self.love_on = false;
|
||||
self.tempt_on = false;
|
||||
} else {
|
||||
self.start_behaviours(m, cx);
|
||||
}
|
||||
self.sink.tick(m, cx, &mut self.walk);
|
||||
self.tick_running(m, cx);
|
||||
}
|
||||
/// `Brain.startEachNonRunningBehavior` for the first valid activity.
|
||||
fn start_behaviours(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
let n = now(m);
|
||||
// DIG, SNIFF, IDLE.
|
||||
let dig = self.digging && self.walk.is_none();
|
||||
let sniff = self.sniff_target.is_some() && self.walk.is_some();
|
||||
if dig {
|
||||
if self.run == Run::None && !self.cooldown.present() && self.can_sniff(m) {
|
||||
self.run = Run::Digging;
|
||||
self.until = n + duration(m, 160, 180);
|
||||
self.transition(m, cx, DIGGING);
|
||||
}
|
||||
if self.run == Run::None && self.cooldown.present() {
|
||||
self.run = Run::FinishedDigging;
|
||||
self.until = n + 40;
|
||||
self.transition(m, cx, RISING);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if sniff {
|
||||
if self.run == Run::None && self.can_sniff(m) {
|
||||
self.run = Run::Searching;
|
||||
self.until = n + 600;
|
||||
self.transition(m, cx, SEARCHING);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if self.digging {
|
||||
return;
|
||||
}
|
||||
// IDLE: love, tempt, look at the target, feeling happy, one of the rest.
|
||||
if !self.love_on && !self.tempt_on && self.breed.can_use(m, cx) {
|
||||
self.reset_sniffing(m, cx);
|
||||
self.breed.start(m, cx);
|
||||
self.love_on = true;
|
||||
}
|
||||
if !self.love_on && !self.tempt_on && self.tempt.can_use(m, cx) {
|
||||
self.reset_sniffing(m, cx);
|
||||
self.tempt.start(m, cx);
|
||||
self.tempt_on = true;
|
||||
}
|
||||
if self.look.is_some() && self.look_until == 0 {
|
||||
self.look_until = n + duration(m, 45, 90);
|
||||
}
|
||||
if self.run == Run::None && self.happy {
|
||||
self.run = Run::Happy;
|
||||
self.until = n + duration(m, 40, 100);
|
||||
self.transition(m, cx, FEELING_HAPPY);
|
||||
}
|
||||
if self.run == Run::None {
|
||||
self.run_one(m, cx);
|
||||
}
|
||||
}
|
||||
/// The idle `RunOne`: a weighted random pick among what can start.
|
||||
fn run_one(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
let n = now(m);
|
||||
// (weight, option)
|
||||
let mut options: Vec<(i32, u8)> = Vec::new();
|
||||
if self.look.is_some() && self.walk.is_none() {
|
||||
options.push((2, 0));
|
||||
}
|
||||
if !self.happy && !self.love_on && !self.tempted() && self.sniff_target.is_none() {
|
||||
options.push((1, 1));
|
||||
}
|
||||
if self.walk.is_none()
|
||||
&& self.sniff_target.is_none()
|
||||
&& !self.cooldown.present()
|
||||
&& !m.is_baby()
|
||||
&& self.can_sniff(m)
|
||||
{
|
||||
options.push((1, 2));
|
||||
}
|
||||
if cx.w.player.as_ref().is_some_and(|p| {
|
||||
p.alive && !p.creative && m.distance_sqr(p.position) <= 36.0 && !p.spectator
|
||||
}) {
|
||||
options.push((1, 3));
|
||||
}
|
||||
if self.walk.is_none() {
|
||||
options.push((1, 4));
|
||||
}
|
||||
options.push((2, 5));
|
||||
let total: i32 = options.iter().map(|o| o.0).sum();
|
||||
// A weighted pick from the high bits (`WeightedList.getRandom`).
|
||||
let mut roll = ((m.random.next_float() * total as f32) as i32).min(total - 1);
|
||||
let mut pick = options[0].1;
|
||||
for (w, o) in &options {
|
||||
if roll < *w {
|
||||
pick = *o;
|
||||
break;
|
||||
}
|
||||
roll -= w;
|
||||
}
|
||||
match pick {
|
||||
// SetWalkTargetFromLookTarget(1.0, 3).
|
||||
0 => {
|
||||
if let Some(l) = self.look {
|
||||
self.walk = Some(WalkTarget::new(
|
||||
[
|
||||
l[0].floor() as i32,
|
||||
l[1].floor() as i32,
|
||||
l[2].floor() as i32,
|
||||
],
|
||||
1.0,
|
||||
3,
|
||||
));
|
||||
}
|
||||
}
|
||||
1 => {
|
||||
self.run = Run::Scenting;
|
||||
self.until = n + duration(m, 40, 80);
|
||||
self.transition(m, cx, SCENTING);
|
||||
}
|
||||
2 => {
|
||||
self.run = Run::Sniffing;
|
||||
self.until = n + duration(m, 40, 80);
|
||||
self.transition(m, cx, SNIFFING);
|
||||
}
|
||||
// SetEntityLookTarget(PLAYER, 6).
|
||||
3 => {
|
||||
if let Some(p) = &cx.w.player {
|
||||
self.look = Some(p.eye);
|
||||
self.look_until = 0;
|
||||
}
|
||||
}
|
||||
// RandomStroll.stroll(1.0).
|
||||
4 => {
|
||||
if let Some(p) = m.land_random_pos(cx.w, 10, 7) {
|
||||
self.walk = Some(WalkTarget::new(
|
||||
[
|
||||
p[0].floor() as i32,
|
||||
p[1].floor() as i32,
|
||||
p[2].floor() as i32,
|
||||
],
|
||||
1.0,
|
||||
0,
|
||||
));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
self.run = Run::DoNothing;
|
||||
self.until = n + duration(m, 5, 20);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `Brain.tickRunningBehaviors`.
|
||||
fn tick_running(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
let n = now(m);
|
||||
let timed_out = n > self.until;
|
||||
// The delegated idle behaviours.
|
||||
if self.love_on {
|
||||
if self.breed.can_continue(m, cx) {
|
||||
self.breed.tick(m, cx);
|
||||
} else {
|
||||
self.breed.stop(m, cx);
|
||||
self.love_on = false;
|
||||
}
|
||||
}
|
||||
if self.tempt_on {
|
||||
self.tempt.stop_distance = if m.is_baby() { 2.5 } else { 3.5 };
|
||||
if self.tempt.can_continue(m, cx) {
|
||||
self.tempt.tick(m, cx);
|
||||
} else {
|
||||
self.tempt.stop(m, cx);
|
||||
self.tempt_on = false;
|
||||
}
|
||||
}
|
||||
// LookAtTargetSink.
|
||||
if self.look_until != 0 {
|
||||
if let Some(l) = self.look
|
||||
&& n <= self.look_until
|
||||
{
|
||||
m.look_at(l);
|
||||
} else {
|
||||
self.look = None;
|
||||
self.look_until = 0;
|
||||
}
|
||||
}
|
||||
match self.run {
|
||||
Run::None => {}
|
||||
Run::Digging => {
|
||||
let ok = self.digging && self.can_dig(m, cx.w) && m.extra.in_love <= 0;
|
||||
if timed_out || !ok {
|
||||
if timed_out {
|
||||
self.cooldown.set(SNIFF_COOLDOWN);
|
||||
} else {
|
||||
self.reset_sniffing(m, cx);
|
||||
}
|
||||
self.run = Run::None;
|
||||
}
|
||||
}
|
||||
Run::FinishedDigging => {
|
||||
if timed_out || !self.digging {
|
||||
self.transition(m, cx, IDLING);
|
||||
// onDiggingComplete: remember the spot when it finished.
|
||||
if timed_out {
|
||||
let p = on_pos(m);
|
||||
m.extra.positions.insert(0, p);
|
||||
m.extra.positions.truncate(21);
|
||||
}
|
||||
self.digging = false;
|
||||
self.happy = true;
|
||||
self.run = Run::None;
|
||||
}
|
||||
}
|
||||
Run::Searching => {
|
||||
let still = if !self.can_sniff(m) {
|
||||
self.transition(m, cx, IDLING);
|
||||
false
|
||||
} else {
|
||||
match (self.walk, self.sniff_target) {
|
||||
(Some(w), Some(t)) => w.pos == t,
|
||||
_ => false,
|
||||
}
|
||||
};
|
||||
if timed_out || !still {
|
||||
if self.can_dig(m, cx.w) && self.can_sniff(m) {
|
||||
self.digging = true;
|
||||
}
|
||||
self.sink.stop(m, &mut self.walk);
|
||||
self.sniff_target = None;
|
||||
self.run = Run::None;
|
||||
}
|
||||
}
|
||||
Run::Happy => {
|
||||
if timed_out {
|
||||
self.transition(m, cx, IDLING);
|
||||
self.happy = false;
|
||||
self.run = Run::None;
|
||||
}
|
||||
}
|
||||
Run::Scenting => {
|
||||
if timed_out {
|
||||
self.transition(m, cx, IDLING);
|
||||
self.run = Run::None;
|
||||
}
|
||||
}
|
||||
Run::Sniffing => {
|
||||
if timed_out || !self.can_sniff(m) {
|
||||
self.transition(m, cx, IDLING);
|
||||
if timed_out && let Some(p) = self.dig_position(m, cx.w) {
|
||||
self.sniff_target = Some(p);
|
||||
self.walk = Some(WalkTarget::new(p, 1.25, 0));
|
||||
}
|
||||
self.run = Run::None;
|
||||
}
|
||||
}
|
||||
Run::DoNothing => {
|
||||
if timed_out {
|
||||
self.run = Run::None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `Sniffer.tick` and `Sniffer.aiStep` additions: the animation states,
|
||||
/// the dropped seed and the water malus.
|
||||
pub fn ai_step(m: &mut Mob, events: &mut Vec<MobEvent>) {
|
||||
|
||||
@@ -73,6 +73,27 @@ def scenarios():
|
||||
'player 12.5 -60 0.5',
|
||||
'mob minecraft:villager 0.5 -60 0.5 0 51 persistent',
|
||||
])
|
||||
# Llamas and trader llamas: strolling, looking at the player, a baby following its parent and a
|
||||
# llama hunting a wolf (LlamaAttackWolfGoal) and spitting at it.
|
||||
for species, seed in [('llama', 61), ('trader_llama', 62)]:
|
||||
add(f'{species}_player', 200, ['player 6.5 -60 0.5', f'mob minecraft:{species} 0.5 -60 0.5 0 {seed} persistent'])
|
||||
add(f'{species}_alone', 200, [f'mob minecraft:{species} 0.5 -60 0.5 90 {seed * 7 + 1} persistent'])
|
||||
add('llama_family', 200, ['mob minecraft:llama 0.5 -60 0.5 0 63 persistent',
|
||||
'mob minecraft:llama 6.5 -60 2.5 0 64 persistent baby'])
|
||||
add('llama_wolf', 200, ['mob minecraft:llama 0.5 -60 0.5 0 65 persistent',
|
||||
'mob minecraft:wolf 8.5 -60 0.5 0 66 persistent'])
|
||||
# Sniffers on diggable grass: scenting, sniffing, searching and digging.
|
||||
add('sniffer_player', 400, ['player 8.5 -60 0.5', 'mob minecraft:sniffer 0.5 -60 0.5 0 71 persistent'])
|
||||
for i, seed in enumerate([72, 73, 74]):
|
||||
add(f'sniffer_alone_{i}', 600, [f'mob minecraft:sniffer 0.5 -60 0.5 {90 * i} {seed} persistent'])
|
||||
add('sniffer_baby', 300, ['mob minecraft:sniffer 0.5 -60 0.5 0 75 persistent baby'])
|
||||
# Happy ghasts float above the grass; ghastlings follow players and adults.
|
||||
add('happy_ghast_player', 200, ['player 6.5 -60 0.5', 'mob minecraft:happy_ghast 0.5 -55 0.5 0 81 persistent'])
|
||||
add('happy_ghast_alone', 200, ['mob minecraft:happy_ghast 0.5 -55 0.5 90 82 persistent'])
|
||||
add('ghastling_player', 200, ['player 8.5 -60 0.5', 'mob minecraft:happy_ghast 0.5 -57 0.5 0 83 persistent baby'])
|
||||
add('ghastling_alone', 200, ['mob minecraft:happy_ghast 0.5 -57 0.5 0 84 persistent baby'])
|
||||
add('ghastling_adult', 200, ['mob minecraft:happy_ghast 10.5 -55 0.5 0 85 persistent',
|
||||
'mob minecraft:happy_ghast 0.5 -57 0.5 0 86 persistent baby'])
|
||||
return s
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user