Give turtles vanilla's goals: panic, tempt, go to water, travel and stroll
This commit is contained in:
@@ -180,14 +180,13 @@ pub fn register(m: &mut Mob) {
|
||||
Family::Undead => undead::register(m),
|
||||
Family::Hostile => hostile::register(m),
|
||||
Family::Aquatic => aquatic::register(m),
|
||||
// Turtle.registerGoals has its own list, none of the generic goals.
|
||||
Family::Generic if m.species() == Species::Turtle => super::turtle::register(m),
|
||||
Family::Generic => {
|
||||
generic(m);
|
||||
if m.species() == Species::Rabbit {
|
||||
super::rabbit::register(m);
|
||||
}
|
||||
if m.species() == Species::Turtle {
|
||||
super::turtle::register(m);
|
||||
}
|
||||
}
|
||||
Family::Inert => {}
|
||||
Family::Witch => super::witch::register(m),
|
||||
@@ -1298,6 +1297,9 @@ pub fn walk_target_value(m: &Mob, w: &MobWorld, p: [i32; 3]) -> f32 {
|
||||
if m.species() == Species::Strider {
|
||||
return super::strider::walk_target_value(m, w, p);
|
||||
}
|
||||
if m.species() == Species::Turtle {
|
||||
return super::turtle::walk_target_value(m, w, p, light());
|
||||
}
|
||||
// Hoglin.getWalkTargetValue: nowhere near a repellent, crimson nylium best.
|
||||
if m.species() == Species::Hoglin {
|
||||
let near = m.extra.repellent.is_some_and(|r| {
|
||||
|
||||
@@ -654,15 +654,24 @@ impl Goal for Float {
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Panic {
|
||||
pub speed: f64,
|
||||
/// `TurtlePanicGoal`: always look for water this far away, not only
|
||||
/// when on fire.
|
||||
pub water_range: Option<i32>,
|
||||
pos: [f64; 3],
|
||||
}
|
||||
impl Panic {
|
||||
pub fn new(speed: f64) -> Self {
|
||||
Self {
|
||||
speed,
|
||||
water_range: None,
|
||||
pos: [0.0; 3],
|
||||
}
|
||||
}
|
||||
/// `TurtlePanicGoal`.
|
||||
pub fn looking_for_water(mut self, range: i32) -> Self {
|
||||
self.water_range = Some(range);
|
||||
self
|
||||
}
|
||||
}
|
||||
pub fn should_panic(m: &Mob) -> bool {
|
||||
m.last_damage
|
||||
@@ -678,18 +687,19 @@ impl Goal for Panic {
|
||||
if !should_panic(m) {
|
||||
return false;
|
||||
}
|
||||
if m.fire_ticks > 0 {
|
||||
let range = self.water_range.or((m.fire_ticks > 0).then_some(5));
|
||||
if let Some(range) = range {
|
||||
let (bx, by, bz) = (
|
||||
m.position.x.floor() as i32,
|
||||
m.position.y.floor() as i32,
|
||||
m.position.z.floor() as i32,
|
||||
);
|
||||
if cx.w.data.state(cx.w.world.block(bx, by, bz)).top == 0.0 {
|
||||
// findBlocksInBoxByManhattanDistance(pos, 5, 1) for water.
|
||||
// findBlocksInBoxByManhattanDistance(pos, range, 1) for water.
|
||||
let mut best: Option<([i32; 3], i32)> = None;
|
||||
for dy in -1i32..=1 {
|
||||
for dx in -5i32..=5 {
|
||||
for dz in -5i32..=5 {
|
||||
for dx in -range..=range {
|
||||
for dz in -range..=range {
|
||||
let d = dx.abs() + dy.abs() + dz.abs();
|
||||
if cx.w.fluid(bx + dx, by + dy, bz + dz) == crate::blocks::Fluid::Water
|
||||
&& best.is_none_or(|b| d < b.1)
|
||||
|
||||
@@ -580,6 +580,11 @@ impl Mob {
|
||||
if self.species() == super::Species::Bee {
|
||||
return super::bee::stable_destination(w, p);
|
||||
}
|
||||
if self.species() == super::Species::Turtle
|
||||
&& let Some(stable) = super::turtle::stable_destination(self, w, p)
|
||||
{
|
||||
return stable;
|
||||
}
|
||||
// StriderPathNavigation: lava too.
|
||||
if self.kind.flags.lava_walker && w.block_name(p[0], p[1], p[2]) == "minecraft:lava" {
|
||||
return true;
|
||||
|
||||
@@ -1,18 +1,73 @@
|
||||
//! Turtles (`Turtle`): the egg a bred mother carries (`TurtleBreedGoal`), her
|
||||
//! walk home (`TurtleGoHomeGoal`) and the egg she digs into the sand
|
||||
//! (`TurtleLayEggGoal`). `extra.flag` is `hasEgg`, `extra.flag2` `isLayingEgg`,
|
||||
//! `extra.counter` `layEggCounter` and `extra.home` `homePos`.
|
||||
//! `extra.counter` `layEggCounter`, `extra.home` `homePos`, `extra.counter2`
|
||||
//! `goingHome` and `extra.point` `travelPos`.
|
||||
use super::{
|
||||
Mob, MobEvent, MobWorld,
|
||||
goals::{Breed, Cx, Goal, JUMP, MOVE, MoveToBlock, goal_clone, reduced},
|
||||
goals::{
|
||||
Breed, Cx, Goal, JUMP, LookAtPlayer, MOVE, MoveToBlock, Panic, Stroll, Tempt, goal_clone,
|
||||
reduced,
|
||||
},
|
||||
nav::NavKind,
|
||||
path::PathType,
|
||||
};
|
||||
use crate::blocks::Fluid;
|
||||
use horizon_core::random::RandomSource;
|
||||
|
||||
/// `Turtle.registerGoals`, and the path maluses and navigation `Turtle` and
|
||||
/// `TurtlePathNavigation` give it.
|
||||
pub fn register(m: &mut Mob) {
|
||||
m.set_malus(PathType::Water, 0.0);
|
||||
for t in [
|
||||
PathType::DoorIronClosed,
|
||||
PathType::DoorWoodClosed,
|
||||
PathType::DoorOpen,
|
||||
] {
|
||||
m.set_malus(t, -1.0);
|
||||
}
|
||||
m.ai.nav.kind = NavKind::Amphibious;
|
||||
let food = m.kind.food.clone();
|
||||
let g = &mut m.ai.goals;
|
||||
g.add(0, Panic::new(1.2).looking_for_water(7));
|
||||
g.add(1, Breed::new(1.0));
|
||||
g.add(1, LayEgg(MoveToBlock::new(1.0, 16, 1)));
|
||||
g.add(2, Tempt::new(1.1, food, false));
|
||||
g.add(
|
||||
3,
|
||||
GoToWater(MoveToBlock {
|
||||
vertical_start: -1,
|
||||
..MoveToBlock::new(1.0, 24, 1)
|
||||
}),
|
||||
);
|
||||
g.add(4, GoHome::default());
|
||||
g.add(7, Travel::default());
|
||||
g.add(8, LookAtPlayer::new(8.0));
|
||||
g.add(9, RandomStroll(Stroll::new(1.0).interval(100)));
|
||||
}
|
||||
/// `Turtle.goingHome`.
|
||||
fn going_home(m: &Mob) -> bool {
|
||||
m.extra.counter2 != 0
|
||||
}
|
||||
fn set_going_home(m: &mut Mob, on: bool) {
|
||||
m.extra.counter2 = i32::from(on);
|
||||
}
|
||||
/// `Turtle.getWalkTargetValue`; `light` is `getPathfindingCostFromLightLevels`.
|
||||
pub fn walk_target_value(m: &Mob, w: &MobWorld, p: [i32; 3], light: f32) -> f32 {
|
||||
if !going_home(m) && w.fluid(p[0], p[1], p[2]) == Fluid::Water
|
||||
|| is_sand(w, [p[0], p[1] - 1, p[2]])
|
||||
{
|
||||
10.0
|
||||
} else {
|
||||
light
|
||||
}
|
||||
}
|
||||
/// `TurtlePathNavigation.isStableDestination` while travelling: water only.
|
||||
/// `None` without a `travelPos`, the amphibious rule then.
|
||||
pub fn stable_destination(m: &Mob, w: &MobWorld, p: [i32; 3]) -> Option<bool> {
|
||||
m.extra
|
||||
.point
|
||||
.map(|_| w.block_name(p[0], p[1], p[2]) == "minecraft:water")
|
||||
}
|
||||
/// `Turtle.hasEgg`.
|
||||
pub fn has_egg(m: &Mob) -> bool {
|
||||
@@ -165,10 +220,14 @@ impl Goal for GoHome {
|
||||
m.random.next_int_bound(reduced(700)).unwrap_or(1) == 0 && !close_to_home(m, 64.0)
|
||||
}
|
||||
}
|
||||
fn start(&mut self, _m: &mut Mob, _cx: &mut Cx) {
|
||||
fn start(&mut self, m: &mut Mob, _cx: &mut Cx) {
|
||||
set_going_home(m, true);
|
||||
self.stuck = false;
|
||||
self.close_to_home_try_ticks = 0;
|
||||
}
|
||||
fn stop(&mut self, m: &mut Mob, _cx: &mut Cx) {
|
||||
set_going_home(m, false);
|
||||
}
|
||||
fn can_continue(&mut self, m: &mut Mob, _cx: &mut Cx) -> bool {
|
||||
!close_to_home(m, 7.0) && !self.stuck && self.close_to_home_try_ticks <= reduced(600)
|
||||
}
|
||||
@@ -207,3 +266,170 @@ impl Goal for GoHome {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `Turtle.TurtleGoToWaterGoal`: a turtle on land walks to water within 24
|
||||
/// blocks, unless it is going home or carries an egg.
|
||||
#[derive(Clone, Debug)]
|
||||
struct GoToWater(MoveToBlock);
|
||||
impl GoToWater {
|
||||
fn valid(w: &MobWorld, p: [i32; 3]) -> bool {
|
||||
w.block_name(p[0], p[1], p[2]) == "minecraft:water"
|
||||
}
|
||||
}
|
||||
impl Goal for GoToWater {
|
||||
goal_clone!();
|
||||
fn flags(&self) -> u8 {
|
||||
MOVE | JUMP
|
||||
}
|
||||
fn name(&self) -> &'static str {
|
||||
"TurtleGoToWaterGoal"
|
||||
}
|
||||
fn every_tick(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
|
||||
let wanted = if m.is_baby() && !m.fluids.in_water {
|
||||
true
|
||||
} else {
|
||||
!going_home(m) && !m.fluids.in_water && !has_egg(m)
|
||||
};
|
||||
if !wanted {
|
||||
return false;
|
||||
}
|
||||
// MoveToBlockGoal.canUse.
|
||||
if self.0.next_start > 0 {
|
||||
self.0.next_start -= 1;
|
||||
return false;
|
||||
}
|
||||
self.0.next_start = reduced(200 + m.random.next_int_bound(200).unwrap_or(0));
|
||||
self.0.find_nearest(m, cx.w, Self::valid)
|
||||
}
|
||||
fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
|
||||
!m.fluids.in_water && self.0.try_ticks <= 1200 && Self::valid(cx.w, self.0.block)
|
||||
}
|
||||
fn start(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
self.0.start(m, cx.w);
|
||||
}
|
||||
fn tick(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
// MoveToBlockGoal.tick, with shouldRecalculatePath every 160 ticks.
|
||||
let b = self.0.block;
|
||||
let d = [
|
||||
f64::from(b[0]) + 0.5 - m.position.x,
|
||||
f64::from(b[1] + 1) + 0.5 - m.position.y,
|
||||
f64::from(b[2]) + 0.5 - m.position.z,
|
||||
];
|
||||
if d[0] * d[0] + d[1] * d[1] + d[2] * d[2] >= 1.0 {
|
||||
self.0.reached = false;
|
||||
self.0.try_ticks += 1;
|
||||
if self.0.try_ticks % 160 == 0 {
|
||||
let at = [
|
||||
f64::from(b[0]) + 0.5,
|
||||
f64::from(b[1] + 1),
|
||||
f64::from(b[2]) + 0.5,
|
||||
];
|
||||
m.nav_move_to(cx.w, at, self.0.speed);
|
||||
}
|
||||
} else {
|
||||
self.0.reached = true;
|
||||
self.0.try_ticks -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `Turtle.TurtleTravelGoal`: a turtle in water swims towards a spot up to
|
||||
/// 512 blocks away, a leg at a time.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
struct Travel {
|
||||
stuck: bool,
|
||||
}
|
||||
impl Goal for Travel {
|
||||
goal_clone!();
|
||||
fn flags(&self) -> u8 {
|
||||
0
|
||||
}
|
||||
fn name(&self) -> &'static str {
|
||||
"TurtleTravelGoal"
|
||||
}
|
||||
fn can_use(&mut self, m: &mut Mob, _cx: &mut Cx) -> bool {
|
||||
!going_home(m) && !has_egg(m) && m.fluids.in_water
|
||||
}
|
||||
fn start(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
let xt = m.random.next_int_bound(1025).unwrap_or(0) - 512;
|
||||
let mut yt = m.random.next_int_bound(9).unwrap_or(0) - 4;
|
||||
let zt = m.random.next_int_bound(1025).unwrap_or(0) - 512;
|
||||
if f64::from(yt) + m.position.y > f64::from(cx.w.sea_level - 1) {
|
||||
yt = 0;
|
||||
}
|
||||
m.extra.point = Some([
|
||||
(f64::from(xt) + m.position.x).floor(),
|
||||
(f64::from(yt) + m.position.y).floor(),
|
||||
(f64::from(zt) + m.position.z).floor(),
|
||||
]);
|
||||
self.stuck = false;
|
||||
}
|
||||
fn can_continue(&mut self, m: &mut Mob, _cx: &mut Cx) -> bool {
|
||||
!m.ai.nav.is_done() && !self.stuck && !going_home(m) && m.extra.in_love <= 0 && !has_egg(m)
|
||||
}
|
||||
fn stop(&mut self, m: &mut Mob, _cx: &mut Cx) {
|
||||
m.extra.point = None;
|
||||
}
|
||||
fn tick(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
let Some(travel) = m.extra.point else {
|
||||
self.stuck = true;
|
||||
return;
|
||||
};
|
||||
if !m.ai.nav.is_done() {
|
||||
return;
|
||||
}
|
||||
// Vec3.atBottomCenterOf(travelPos).
|
||||
let to = [travel[0] + 0.5, travel[1], travel[2] + 0.5];
|
||||
let tenth = f64::from((std::f64::consts::PI / 10.0) as f32);
|
||||
let half = f64::from(std::f32::consts::FRAC_PI_2);
|
||||
let mut next = m
|
||||
.default_random_pos_towards(cx.w, 16, 3, to, tenth)
|
||||
.or_else(|| m.default_random_pos_towards(cx.w, 8, 7, to, half));
|
||||
if let Some(n) = next {
|
||||
let (xc, zc) = (n[0].floor() as i32, n[2].floor() as i32);
|
||||
// Level.hasChunksAt(xc - 34, zc - 34, xc + 34, zc + 34).
|
||||
let loaded = ((xc - 34) >> 4..=(xc + 34) >> 4).all(|chunk_x| {
|
||||
((zc - 34) >> 4..=(zc + 34) >> 4)
|
||||
.all(|chunk_z| cx.w.world.chunks.contains_key(&(chunk_x, chunk_z)))
|
||||
});
|
||||
if !loaded {
|
||||
next = None;
|
||||
}
|
||||
}
|
||||
match next {
|
||||
Some(at) => {
|
||||
m.nav_move_to(cx.w, at, 1.0);
|
||||
}
|
||||
None => self.stuck = true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `Turtle.TurtleRandomStrollGoal`: no stroll in water, going home or with an
|
||||
/// egg.
|
||||
#[derive(Clone, Debug)]
|
||||
struct RandomStroll(Stroll);
|
||||
impl Goal for RandomStroll {
|
||||
goal_clone!();
|
||||
fn flags(&self) -> u8 {
|
||||
self.0.flags()
|
||||
}
|
||||
fn name(&self) -> &'static str {
|
||||
"TurtleRandomStrollGoal"
|
||||
}
|
||||
fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
|
||||
!m.fluids.in_water && !going_home(m) && !has_egg(m) && 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) {
|
||||
self.0.start(m, cx);
|
||||
}
|
||||
fn stop(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
self.0.stop(m, cx);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user