Let frogs croak, shoot their tongue at small slimes and idle-swim

This commit is contained in:
Kacpherek
2026-10-26 23:06:30 +01:00
parent a45d0de43e
commit 04b0b9aed5
5 changed files with 326 additions and 2 deletions
+9
View File
@@ -399,6 +399,15 @@ fn mob_meta(
}
defaults.push((18, serializer::BYTE, Value::Byte(16)));
}
// Frog.DATA_TONGUE_TARGET_ID (after the variant): the entity id plus
// one, 0 for none.
"minecraft:frog" => {
if let Some(target) = e.frog.tongue_target {
let id = i32::try_from(target).map_or(0, |t| t + 1);
meta.push((19, serializer::OPTIONAL_UNSIGNED_INT, Value::Id(id)));
}
defaults.push((19, serializer::OPTIONAL_UNSIGNED_INT, Value::Id(0)));
}
// Enderman.DATA_CARRY_STATE.
"minecraft:enderman" => {
if let Some(state) = e.carried {
+6
View File
@@ -31,6 +31,7 @@ pub mod enderman;
pub mod equine;
pub mod farm;
pub mod finalize;
pub mod frog;
pub mod gear;
pub mod goals;
pub mod golem;
@@ -786,6 +787,8 @@ pub struct Extra {
pub brain: Option<Box<monster_brain::Brain>>,
/// A bee's nectar, anger, flower and hive state.
pub bee: Option<Box<bee::Bee>>,
/// A frog's brain memories and tongue target.
pub frog: frog::State,
}
#[derive(Clone, Debug)]
pub struct Mob {
@@ -1272,6 +1275,8 @@ pub struct Seen {
pub target: Option<Who>,
pub health: f32,
pub variant: u8,
/// A slime's or magma cube's size.
pub size: i32,
/// Dye colour (sheep).
pub color: u8,
/// A villager's claimed job site and bed.
@@ -1367,6 +1372,7 @@ impl Mob {
target: m.ai.target,
health: m.health,
variant: m.extra.variant,
size: m.extra.size,
color: m.extra.color,
job: villager.then_some(m.extra.home).flatten(),
bed: villager.then_some(m.extra.bed).flatten(),
+3
View File
@@ -178,6 +178,7 @@ pub fn register(m: &mut Mob) {
Family::Undead => undead::register(m),
Family::Hostile => hostile::register(m),
Family::Aquatic => aquatic::register(m),
Family::Generic if m.species() == Species::Frog => super::frog::register(m),
Family::Generic => {
generic(m);
if m.species() == Species::Rabbit {
@@ -964,6 +965,7 @@ fn custom_server_ai_step(m: &mut Mob, cx: &mut Cx) {
Species::Evoker if m.extra.counter > 0 => m.extra.counter -= 1,
Species::Bat => super::bat::server_ai_step(m, cx.w, cx.events),
Species::Bee => super::bee::server_ai_step(m, cx),
Species::Frog => super::frog::server_ai_step(m, cx.w),
_ => {}
}
}
@@ -1016,6 +1018,7 @@ fn species_ai_step(
}
Species::Nautilus | Species::ZombieNautilus => super::nautilus::ai_step(m, w, events),
Species::Chicken => farm::chicken_ai_step(m, w, events),
Species::Frog => super::frog::tick(m),
Species::Turtle => super::turtle::ai_step(m, w, events),
Species::Sheep if m.extra.anim_ticks > 0 => {
m.extra.anim_ticks -= 1;
+301
View File
@@ -0,0 +1,301 @@
//! Frogs (`Frog`, `FrogAi`): the `Croak` and `ShootTongue` behaviours as
//! goals, the poses they set and the client's animation states.
//! AnimState states: jump, croak, tongue, swim idle.
//!
//! The brain's idle `RunOne` picks a croak three times as often as a stroll
//! (weights 3 and 1; it also holds a do-nothing option that only repeats
//! the pick), so a frog that is not walking croaks in three of four idle
//! picks: `Croak` takes those, and the frog's stroll then follows at once.
use super::{
Mob, MobEvent, MobWorld, Species, Who,
goals::{
Cx, Float, Goal, LOOK, LookAtPlayer, MOVE, Panic, RandomLookAround, Stroll, goal_clone,
},
sync,
};
use horizon_core::{geometry::Vec3, random::RandomSource};
const JUMP: usize = 0;
const CROAK: usize = 1;
const TONGUE: usize = 2;
const SWIM_IDLE: usize = 3;
/// `Croak.CROAK_TICKS`.
const CROAK_TICKS: i32 = 60;
/// `ShootTongue`'s duration and eating distance.
const TONGUE_TIME_OUT: i32 = 100;
const EATING_DISTANCE: f64 = 1.75;
/// `FrogAttackablesSensor.TARGET_DETECTION_DISTANCE` and the sensor's scan
/// rate.
const DETECTION_DISTANCE: f64 = 10.0;
const SCAN_RATE: i32 = 20;
/// `ShootTongue.State`.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
enum Phase {
MoveToTarget,
CatchAnimation,
EatAnimation,
#[default]
Done,
}
/// The frog's brain memories and `DATA_TONGUE_TARGET_ID`.
#[derive(Clone, Debug, Default)]
pub struct State {
croak: i32,
/// Ticks to the attackables sensor's next scan, and `NEAREST_ATTACKABLE`.
scan: i32,
nearest: Option<u32>,
/// `UNREACHABLE_TONGUE_TARGETS`: the mob and the ticks it stays in mind.
unreachable: Vec<(u32, i32)>,
/// `ATTACK_TARGET` while `ShootTongue` runs, and the synched tongue target.
target: Option<u32>,
pub tongue_target: Option<u32>,
phase: Phase,
eat_timer: i32,
path_counter: i32,
time: i32,
}
pub fn register(m: &mut Mob) {
let g = &mut m.ai.goals;
g.add(0, Float);
g.add(1, Panic::new(1.25));
g.add(2, ShootTongue);
g.add(4, Croak);
g.add(5, Stroll::avoiding_water(1.0).interval(1));
g.add(6, LookAtPlayer::new(6.0));
g.add(7, RandomLookAround::default());
}
/// `Entity.setPose` and `Frog.onSyncedDataUpdated(DATA_POSE)`.
pub fn set_pose(m: &mut Mob, id: u8) {
let old = std::mem::replace(&mut m.extra.pose, id);
if old != id {
pose_changed(m);
}
}
/// The animation states of the pose the frog has taken.
pub fn pose_changed(m: &mut Mob) {
let age = m.age as f32;
let pose = m.extra.pose;
let states = &mut m.anim.states;
for (state, on) in [
(JUMP, pose == sync::LONG_JUMPING),
(CROAK, pose == sync::CROAKING),
(TONGUE, pose == sync::USING_TONGUE),
] {
if on {
states[state].start(age);
} else {
states[state].stop();
}
}
}
/// `Frog.tick` on a client: the idle swim while it floats still. The server
/// runs it too, for a window that shows its own mobs.
pub fn tick(m: &mut Mob) {
let age = m.age as f32;
let still = m.fluids.in_water && m.limb_speed <= 1.0e-5;
m.anim.states[SWIM_IDLE].animate_when(still, age);
}
/// `Frog.canEat`: a slime or magma cube of size 1.
fn can_eat(species: Species, size: i32) -> bool {
matches!(species, Species::Slime | Species::MagmaCube) && size == 1
}
/// `FrogAttackablesSensor` (every 20 ticks) and the memory timers.
pub fn server_ai_step(m: &mut Mob, w: &MobWorld) {
let f = &mut m.extra.frog;
f.unreachable.retain_mut(|(_, ttl)| {
*ttl -= 1;
*ttl > 0
});
f.scan -= 1;
if f.scan > 0 {
return;
}
f.scan = SCAN_RATE;
let unreachable: Vec<u32> = f.unreachable.iter().map(|u| u.0).collect();
let eye = Vec3::new(m.position.x, m.eye_y(), m.position.z);
let nearest = w
.nearby(m.position, DETECTION_DISTANCE, |s| {
can_eat(s.species, s.size)
&& !unreachable.contains(&s.id)
&& w.line_of_sight(eye, Vec3::new(s.position.x, s.eye_y, s.position.z))
})
.find(|s| s.position.distance_squared(m.position) < DETECTION_DISTANCE * DETECTION_DISTANCE)
.map(|s| s.id);
m.extra.frog.nearest = nearest;
}
/// `Croak`: the pose sync::CROAKING for 60 ticks (the croak is the frog's ambient
/// sound).
#[derive(Clone, Debug)]
struct Croak;
impl Goal for Croak {
goal_clone!();
fn flags(&self) -> u8 {
MOVE
}
fn can_use(&mut self, m: &mut Mob, _cx: &mut Cx) -> bool {
// checkExtraStartConditions (standing), no walk target, and not in
// a liquid (start does nothing there).
m.extra.pose == sync::STANDING
&& m.on_ground
&& !m.fluids.in_water
&& !m.fluids.in_lava
&& m.ai.nav.is_done()
&& m.random.next_int_bound(4).unwrap_or(0) != 0
}
fn can_continue(&mut self, m: &mut Mob, _cx: &mut Cx) -> bool {
m.extra.frog.croak < CROAK_TICKS
}
fn start(&mut self, m: &mut Mob, _cx: &mut Cx) {
m.extra.frog.croak = 0;
set_pose(m, sync::CROAKING);
}
fn stop(&mut self, m: &mut Mob, _cx: &mut Cx) {
set_pose(m, sync::STANDING);
}
fn tick(&mut self, m: &mut Mob, _cx: &mut Cx) {
m.extra.frog.croak += 1;
}
fn every_tick(&self) -> bool {
true
}
}
/// `ShootTongue` with the tongue activity's `StartAttacking` and
/// `StopAttackingIfTargetInvalid`: walk up to a small slime or magma cube,
/// shoot the tongue, pull it in and eat it.
#[derive(Clone, Debug)]
struct ShootTongue;
impl Goal for ShootTongue {
goal_clone!();
fn flags(&self) -> u8 {
MOVE | LOOK
}
fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
let Some(id) = m.extra.frog.nearest else {
return false;
};
// The start conditions: no walk target, no panic, not croaking.
if m.ai.panicking || m.extra.pose == sync::CROAKING || !m.ai.nav.is_done() {
return false;
}
let Some(t) = cx.w.seen(id).filter(|t| t.alive) else {
m.extra.frog.nearest = None;
return false;
};
// canPathfindToTarget: a path that ends at the target.
let at = [t.position.x, t.position.y, t.position.z];
let ends = m
.create_path_to_entity(cx.w, at, 0)
.is_some_and(|p| f64::from(p.dist_to_target) < EATING_DISTANCE);
if !ends {
// The target is forgotten and not taken for 100 ticks.
let f = &mut m.extra.frog;
f.nearest = None;
if !f.unreachable.iter().any(|u| u.0 == id) {
if f.unreachable.len() == 5 {
f.unreachable.remove(0);
}
f.unreachable.push((id, 100));
}
return false;
}
m.extra.frog.target = Some(id);
true
}
fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
let f = &m.extra.frog;
f.phase != Phase::Done
&& f.time <= TONGUE_TIME_OUT
&& !m.ai.panicking
&& f.target
.is_some_and(|id| cx.w.seen(id).is_some_and(|t| t.alive))
}
fn start(&mut self, m: &mut Mob, cx: &mut Cx) {
let Some(t) = m.extra.frog.target.and_then(|id| cx.w.seen(id)) else {
return;
};
let at = [t.position.x, t.position.y, t.position.z];
let eye = [t.position.x, t.eye_y, t.position.z];
m.look_at(eye);
m.extra.frog.tongue_target = m.extra.frog.target;
m.nav_move_to_entity(cx.w, at, 0, 2.0);
let f = &mut m.extra.frog;
f.path_counter = 10;
f.time = 0;
f.phase = Phase::MoveToTarget;
}
fn stop(&mut self, m: &mut Mob, _cx: &mut Cx) {
let f = &mut m.extra.frog;
f.target = None;
f.tongue_target = None;
f.phase = Phase::Done;
set_pose(m, sync::STANDING);
}
fn every_tick(&self) -> bool {
true
}
fn tick(&mut self, m: &mut Mob, cx: &mut Cx) {
let Some(id) = m.extra.frog.target else {
return;
};
let Some(t) = cx.w.seen(id) else {
return;
};
let (at, pos) = ([t.position.x, t.position.y, t.position.z], t.position);
m.look_at([t.position.x, t.eye_y, t.position.z]);
m.extra.frog.tongue_target = Some(id);
m.extra.frog.time += 1;
let here = [m.position.x, m.position.y, m.position.z];
match m.extra.frog.phase {
Phase::MoveToTarget => {
if pos.distance_squared(m.position) < EATING_DISTANCE * EATING_DISTANCE {
cx.events
.push(MobEvent::Sound("entity.frog.tongue", here, 2.0, 1.0));
set_pose(m, sync::USING_TONGUE);
// The target is drawn in at 0.75 a tick.
let pull = m.position.subtract(pos).normalize().scale(0.75);
cx.events.push(MobEvent::Push {
target: Who::Mob(id),
velocity: [pull.x, pull.y, pull.z],
});
let f = &mut m.extra.frog;
f.eat_timer = 0;
f.phase = Phase::CatchAnimation;
} else if m.extra.frog.path_counter <= 0 {
m.nav_move_to_entity(cx.w, at, 0, 2.0);
m.extra.frog.path_counter = 10;
} else {
m.extra.frog.path_counter -= 1;
}
}
Phase::CatchAnimation => {
let f = &mut m.extra.frog;
f.eat_timer += 1;
if f.eat_timer > 6 {
f.phase = Phase::EatAnimation;
// eatEntity.
cx.events
.push(MobEvent::Sound("entity.frog.eat", here, 2.0, 1.0));
super::ai::do_hurt_target(m, Who::Mob(id), cx);
}
}
Phase::EatAnimation => {
let f = &mut m.extra.frog;
if f.eat_timer >= 10 {
f.phase = Phase::Done;
} else {
f.eat_timer += 1;
}
}
Phase::Done => {}
}
}
}
+7 -2
View File
@@ -15,11 +15,13 @@
//! them in `entity_event`;
//! - run its client side animation in `client_tick`, sharing the function
//! with the server's `ai_step` where the server computes the same.
use super::{Mob, Species, breeze, camel, shulker, sniffer, warden};
use super::{Mob, Species, breeze, camel, frog, shulker, sniffer, warden};
// `Pose.id` of the poses mobs take.
pub const STANDING: u8 = 0;
pub const LONG_JUMPING: u8 = 6;
pub const CROAKING: u8 = 8;
pub const USING_TONGUE: u8 = 9;
pub const SITTING: u8 = 10;
pub const ROARING: u8 = 11;
pub const SNIFFING: u8 = 12;
@@ -36,14 +38,16 @@ pub fn pose_id(m: &Mob) -> u8 {
Species::Warden => warden::pose_id(m),
Species::Breeze => breeze::pose_id(m),
Species::Camel | Species::CamelHusk if camel::sitting(m) => SITTING,
Species::Frog => m.extra.pose,
_ => STANDING,
}
}
/// `Entity.onSyncedDataUpdated(DATA_POSE)` on a client.
pub fn set_pose(m: &mut Mob, id: u8) {
m.extra.pose = id;
let old = std::mem::replace(&mut m.extra.pose, id);
match m.species() {
Species::Frog if old != id => frog::pose_changed(m),
Species::Warden => warden::set_pose_id(m, id),
Species::Breeze => breeze::set_pose_id(m, id),
_ => {}
@@ -60,6 +64,7 @@ pub fn client_tick(m: &mut Mob, game_time: i64) {
Species::Breeze => breeze::ai_step(m),
Species::Camel | Species::CamelHusk => camel::client_tick(m, game_time),
Species::Shulker => shulker::client_tick(m),
Species::Frog => frog::tick(m),
_ => {}
}
}