Merge main into fix/server-mob-animation-sync
main's ease_peek and onPeekAmountChange; client_tick eases with it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -188,6 +188,20 @@ pub fn register(m: &mut Mob) {
|
||||
if m.species() == Species::Turtle {
|
||||
super::turtle::register(m);
|
||||
}
|
||||
if m.species() == Species::Frog {
|
||||
// `Frog()`: SmoothSwimmingMoveControl(85, 10, 0.02, 0.1, true).
|
||||
// The frog's movement speed is 1.0, so the controller's 0.1
|
||||
// outside-water speed is what keeps a walking frog at
|
||||
// vanilla's pace; the plain ground controller walks ten
|
||||
// times too fast (#2213).
|
||||
m.ai.mov.kind = super::control::MoveKind::SmoothSwimming {
|
||||
max_turn_x: 85.0,
|
||||
max_turn_y: 10.0,
|
||||
in_water: 0.02,
|
||||
outside: 0.1,
|
||||
gravity: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
Family::Inert => {}
|
||||
Family::Witch => super::witch::register(m),
|
||||
@@ -524,14 +538,19 @@ fn base_tick(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
|
||||
}
|
||||
m.first_tick = false;
|
||||
if m.alive() {
|
||||
// LivingEntity.baseTick: suffocation inside full blocks.
|
||||
// LivingEntity.baseTick: suffocation inside full blocks. Entity.isInWall
|
||||
// is false under noPhysics, which Vex.tick holds while it ticks.
|
||||
let (ex, ey, ez) = (
|
||||
m.position.x.floor() as i32,
|
||||
m.eye_y().floor() as i32,
|
||||
m.position.z.floor() as i32,
|
||||
);
|
||||
let at_eye = w.facts.get(w.world.block(ex, ey, ez));
|
||||
if at_eye.full_collision && at_eye.solid_render && m.width() < 1.0 {
|
||||
if at_eye.full_collision
|
||||
&& at_eye.solid_render
|
||||
&& m.width() < 1.0
|
||||
&& m.species() != Species::Vex
|
||||
{
|
||||
m.damage(1.0, Damage::of(DamageKind::InWall));
|
||||
}
|
||||
// Air supply and drowning (WaterAnimal.handleAirSupply for aquatic mobs).
|
||||
@@ -1818,3 +1837,41 @@ mod despawn_tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod frog_tests {
|
||||
use super::*;
|
||||
use crate::mobs::MobTypes;
|
||||
use crate::mobs::control::MoveKind;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// `Frog()`: `new SmoothSwimmingMoveControl<>(this, 85, 10, 0.02F, 0.1F, true)`.
|
||||
/// The frog's movement speed is 1.0, so the controller's 0.1 outside-water
|
||||
/// speed is what keeps a walking frog at vanilla's pace. The plain ground
|
||||
/// controller walks it ten times too fast, which is the jitter, the
|
||||
/// teleporting and the flying of #2213.
|
||||
#[test]
|
||||
fn a_frog_walks_at_the_vanilla_speed() {
|
||||
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("../../.local/horizon-client")
|
||||
.join(horizon_core::VANILLA_VERSION);
|
||||
if !root.join("entity-types.json").exists() {
|
||||
return;
|
||||
}
|
||||
let data = MobTypes::load(&root).unwrap();
|
||||
let kind = Arc::new(data.types.get("minecraft:frog").unwrap().clone());
|
||||
assert_eq!(kind.speed, 1.0, "the frog's movement speed attribute");
|
||||
let mut m = Mob::new(1, kind, Vec3::new(0.0, 4.0, 0.0), 0.0);
|
||||
register(&mut m);
|
||||
assert_eq!(
|
||||
m.ai.mov.kind,
|
||||
MoveKind::SmoothSwimming {
|
||||
max_turn_x: 85.0,
|
||||
max_turn_y: 10.0,
|
||||
in_water: 0.02,
|
||||
outside: 0.1,
|
||||
gravity: true,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use horizon_core::geometry::trig;
|
||||
/// `2.5000003E-7F` widened: the squared distance controls treat as arrived.
|
||||
const ARRIVED: f64 = 2.500_000_3e-7_f32 as f64;
|
||||
/// `1.0E-5F` widened.
|
||||
const EPSILON: f64 = 1.0e-5_f32 as f64;
|
||||
pub(super) const EPSILON: f64 = 1.0e-5_f32 as f64;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
|
||||
pub enum Operation {
|
||||
@@ -90,6 +90,9 @@ pub enum LookKind {
|
||||
Fixed,
|
||||
/// `HappyGhast.HappyGhastLookControl`.
|
||||
HappyGhast,
|
||||
/// `Shulker.ShulkerLookControl`: the yaw turns in the attach face's
|
||||
/// plane and the head is never clamped to the body.
|
||||
Shulker,
|
||||
}
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct LookControl {
|
||||
@@ -135,6 +138,9 @@ impl Mob {
|
||||
if self.species() == super::Species::Rabbit {
|
||||
return super::rabbit::move_control(self, w);
|
||||
}
|
||||
if self.species() == super::Species::Turtle {
|
||||
return super::turtle::move_control(self);
|
||||
}
|
||||
let attr_speed = self.movement_speed();
|
||||
match self.ai.mov.kind {
|
||||
MoveKind::Custom => super::ai::custom_move_control(self, w, events),
|
||||
@@ -345,6 +351,20 @@ impl Mob {
|
||||
self.body_yaw += 4.0;
|
||||
}
|
||||
}
|
||||
LookKind::Shulker => {
|
||||
// ShulkerLookControl: resetXRotOnTick, getXRotD 0, and the
|
||||
// yaw worked out in the attach face's plane (getYRotD).
|
||||
self.head_pitch = 0.0;
|
||||
if look.cooldown > 0 {
|
||||
self.ai.look.cooldown -= 1;
|
||||
if let Some(y) = super::shulker::look_yaw(self, look.wanted) {
|
||||
self.head_yaw = rotate_towards(self.head_yaw, y, look.y_max_rot_speed);
|
||||
}
|
||||
} else {
|
||||
self.head_yaw = rotate_towards(self.head_yaw, self.body_yaw, 10.0);
|
||||
}
|
||||
// clampHeadRotationToBody does nothing for a shulker.
|
||||
}
|
||||
LookKind::Default | LookKind::Fixed => {
|
||||
if look.kind == LookKind::Default {
|
||||
self.head_pitch = 0.0;
|
||||
|
||||
@@ -1508,7 +1508,7 @@ impl<L: FinalizeLevel + ?Sized, R: RandomSource> Ctx<'_, L, R> {
|
||||
}
|
||||
fn sound(&mut self, m: &mut Mob, registry: fn(&SpawnRegistries) -> &VariantRegistry) {
|
||||
let r = registry(self.data.registries);
|
||||
m.extra.sound_variant = r.random(self.random) as u8;
|
||||
m.extra.sound_variant = Some(r.random(self.random) as u8);
|
||||
}
|
||||
|
||||
/// `finalizeSpawn` of the mob's class.
|
||||
@@ -2014,6 +2014,17 @@ pub fn species_variant_component(s: Species, variant: u8) -> Option<(&'static st
|
||||
};
|
||||
Some((component, format!("minecraft:{}", variant_name(s, variant))))
|
||||
}
|
||||
/// The `*_sound_variant` registry of a species, in registry order.
|
||||
pub fn sound_variants(s: Species) -> &'static [&'static str] {
|
||||
match s {
|
||||
Species::Pig => &["big", "classic", "mini"],
|
||||
Species::Cow => &["classic", "moody"],
|
||||
Species::Chicken => &["classic", "picky"],
|
||||
Species::Cat => &["classic", "royal"],
|
||||
Species::Wolf => &["angry", "big", "classic", "cute", "grumpy", "puglin", "sad"],
|
||||
_ => &[],
|
||||
}
|
||||
}
|
||||
/// The registry name of the game's variant index.
|
||||
pub fn variant_name(s: Species, index: u8) -> &'static str {
|
||||
let i = index as usize;
|
||||
|
||||
@@ -235,6 +235,26 @@ impl MobWorld<'_> {
|
||||
};
|
||||
scene.unobstructed(b).unwrap_or(false)
|
||||
}
|
||||
/// `LevelReader.containsAnyLiquid`: the blocks from the box's minimum
|
||||
/// corner to the block holding its maximum corner.
|
||||
pub fn contains_any_liquid(&self, b: Aabb) -> bool {
|
||||
let (lo, hi) = (b.min_position(), b.max_position());
|
||||
let (x0, y0, z0) = (
|
||||
lo.x.floor() as i32,
|
||||
lo.y.floor() as i32,
|
||||
lo.z.floor() as i32,
|
||||
);
|
||||
let (x1, y1, z1) = (
|
||||
hi.x.floor() as i32,
|
||||
hi.y.floor() as i32,
|
||||
hi.z.floor() as i32,
|
||||
);
|
||||
(x0..=x1).any(|x| {
|
||||
(y0..=y1).any(|y| {
|
||||
(z0..=z1).any(|z| self.facts.get(self.world.block(x, y, z)).fluid != Fluid::None)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
impl Mob {
|
||||
/// `Entity.refreshDimensions` after growing from `old_w` by `old_h`:
|
||||
@@ -813,14 +833,24 @@ impl Mob {
|
||||
self.velocity.y -= gravity / 4.0;
|
||||
}
|
||||
}
|
||||
// jumpOutOfFluid
|
||||
// jumpOutOfFluid: Entity.isFree wants the box dry and clear of the
|
||||
// entities a mob collides with (canBeCollidedWith: living shulkers).
|
||||
let m = self.velocity;
|
||||
let ahead = self.bounds().move_by(Vec3::new(
|
||||
m.x,
|
||||
m.y + f64::from(0.6_f32) - self.position.y + old_y,
|
||||
m.z,
|
||||
));
|
||||
let wide = ahead.inflate(Vec3::new(1.0e-7, 1.0e-7, 1.0e-7));
|
||||
if self.horizontal_collision
|
||||
&& w.no_collision(self.bounds().move_by(Vec3::new(
|
||||
m.x,
|
||||
m.y + f64::from(0.6_f32) - self.position.y + old_y,
|
||||
m.z,
|
||||
)))
|
||||
&& w.no_collision(ahead)
|
||||
&& !w.contains_any_liquid(ahead)
|
||||
&& !w.others_near(wide).any(|s| {
|
||||
s.id != self.id
|
||||
&& s.alive
|
||||
&& s.species == super::Species::Shulker
|
||||
&& s.bounds.intersects(wide)
|
||||
})
|
||||
{
|
||||
self.velocity.y = f64::from(0.3_f32);
|
||||
}
|
||||
|
||||
@@ -1240,6 +1240,15 @@ fn hand_base(
|
||||
0.0
|
||||
};
|
||||
let mut t = part_matrix(model, base, poses, arm, offset)?;
|
||||
if m.species() == Species::Vex {
|
||||
// `VexModel.translateToHand`: the held item is drawn at 0.55 scale.
|
||||
t *= glam::Mat4::from_scale(glam::Vec3::splat(0.55));
|
||||
t *= glam::Mat4::from_translation(glam::Vec3::new(
|
||||
if left { -0.046_875 } else { 0.046_875 },
|
||||
-0.156_25,
|
||||
0.078_125,
|
||||
));
|
||||
}
|
||||
let (ox, oy, oz) = if m.is_baby() {
|
||||
(0.0, 1.0, -4.5)
|
||||
} else {
|
||||
@@ -1426,4 +1435,40 @@ mod tests {
|
||||
assert!(vertices.iter().all(|v| v.uv[0] >= 0.5));
|
||||
}
|
||||
}
|
||||
/// `VexModel.translateToHand`: the held item is drawn at 0.55 scale and
|
||||
/// moved by the vex's own offset, so the vex's sword is the tiny one
|
||||
/// vanilla shows. It was drawn at the full size, like a zombie's.
|
||||
#[test]
|
||||
fn the_vex_holds_its_item_at_the_small_scale() {
|
||||
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("../../.local/horizon-client/26.3");
|
||||
if !root.join("entity-models.json").exists() {
|
||||
return;
|
||||
}
|
||||
let types = super::super::MobTypes::load(&root).unwrap();
|
||||
let kind = std::sync::Arc::new(types.types["minecraft:vex"].clone());
|
||||
let mob = Mob::new(1, kind, horizon_core::geometry::Vec3::ZERO, 0.0);
|
||||
let data = MobData {
|
||||
states: Vec::new(),
|
||||
placements: Default::default(),
|
||||
heightmaps: Default::default(),
|
||||
animations: Default::default(),
|
||||
biome_tags: Default::default(),
|
||||
entity_tags: Default::default(),
|
||||
villager_hats: Default::default(),
|
||||
pois: Default::default(),
|
||||
};
|
||||
for left in [false, true] {
|
||||
let t = held_item_transform(&mob, 0.5, glam::DVec3::ZERO, &data, left).unwrap();
|
||||
// The model's own part scale is 1, so the item's scale is the
|
||||
// renderer's: `poseStack.scale(0.55F, 0.55F, 0.55F)`.
|
||||
for axis in [t.x_axis, t.y_axis, t.z_axis] {
|
||||
let scale = axis.truncate().length();
|
||||
assert!(
|
||||
(scale - 0.55).abs() < 1.0e-5,
|
||||
"the vex's item is drawn at 0.55 scale, got {scale}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
use super::{
|
||||
Damage, DamageKind, Mob, MobEvent, MobWorld, Species, Who,
|
||||
anim::{Poses, State},
|
||||
control::LookKind,
|
||||
goals::{
|
||||
Cx, Goal, HurtByTarget, LOOK, LookAtPlayer, MOVE, NearestTarget, RandomLookAround,
|
||||
goal_clone, reduced,
|
||||
@@ -37,7 +38,13 @@ pub fn register(m: &mut Mob) {
|
||||
g.add(4, ShulkerAttack { attack_time: 0 });
|
||||
g.add(7, Peek { time: 0 });
|
||||
g.add(8, RandomLookAround::default());
|
||||
m.ai.targets.add(1, HurtByTarget::new().alert());
|
||||
// `ShulkerLookControl`: the head turns in the attach face's plane.
|
||||
m.ai.look.kind = LookKind::Shulker;
|
||||
// `HurtByTargetGoal(this, Shulker.class).setAlertOthers()`: damage from
|
||||
// another shulker draws no retaliation (`toIgnoreDamage`), but a hurt
|
||||
// shulker alerts every targetless shulker around.
|
||||
m.ai.targets
|
||||
.add(1, HurtByTarget::new().alert().ignoring(&[Species::Shulker]));
|
||||
// ShulkerNearestAttackGoal: the search box is only 4 deep along the
|
||||
// attach axis.
|
||||
m.ai.targets.add(
|
||||
@@ -57,6 +64,10 @@ pub fn register(m: &mut Mob) {
|
||||
fn face_step(face: u8) -> [i32; 3] {
|
||||
STEPS[face.min(5) as usize]
|
||||
}
|
||||
/// `Direction.getOpposite` for a 3D data value.
|
||||
fn opposite(face: u8) -> u8 {
|
||||
face.min(5) ^ 1
|
||||
}
|
||||
fn block_pos(m: &Mob) -> [i32; 3] {
|
||||
[
|
||||
m.position.x.floor() as i32,
|
||||
@@ -64,19 +75,83 @@ fn block_pos(m: &Mob) -> [i32; 3] {
|
||||
m.position.z.floor() as i32,
|
||||
]
|
||||
}
|
||||
/// `Shulker.canStayAt`: an empty spot against a sturdy face.
|
||||
fn can_stay_at(w: &MobWorld, p: [i32; 3], face: u8) -> bool {
|
||||
if !w.facts.is_air(w.world.block(p[0], p[1], p[2])) {
|
||||
/// `Shulker.getProgressDeltaAabb` at scale 1: the shell's box from `pos`
|
||||
/// (the bottom centre), grown `from`..`to` along `direction`.
|
||||
fn progress_aabb(direction: u8, from: f64, to: f64, pos: Vec3) -> Aabb {
|
||||
let s = face_step(direction).map(f64::from);
|
||||
let (max, min) = (from.max(to), from.min(to));
|
||||
Aabb::new(Vec3::new(-0.5, 0.0, -0.5), Vec3::new(0.5, 1.0, 0.5))
|
||||
.expand_towards(Vec3::new(s[0] * max, s[1] * max, s[2] * max))
|
||||
.contract(Vec3::new(
|
||||
-s[0] * (1.0 + min),
|
||||
-s[1] * (1.0 + min),
|
||||
-s[2] * (1.0 + min),
|
||||
))
|
||||
.move_by(pos)
|
||||
}
|
||||
/// `Level.noCollision(shulker, aabb)`: no block collision under the empty
|
||||
/// context, and none of the entities a shulker collides with overlaps
|
||||
/// (`canBeCollidedWith`: living shulkers and adult happy ghasts; players
|
||||
/// and other mobs never block it).
|
||||
fn no_collision(m: &Mob, w: &MobWorld, bounds: Aabb) -> bool {
|
||||
let mut scene = crate::collisions::SceneCollisions {
|
||||
world: w.world,
|
||||
binding: w.collision,
|
||||
subject: None,
|
||||
};
|
||||
if !crate::collisions::no_collision_empty(&mut scene, bounds) {
|
||||
return false;
|
||||
}
|
||||
let grown = bounds.inflate(Vec3::new(1.0e-7, 1.0e-7, 1.0e-7));
|
||||
!w.others_near(grown).any(|s| {
|
||||
s.id != m.id
|
||||
&& s.alive
|
||||
&& (s.species == Species::Shulker || (s.species == Species::HappyGhast && !s.baby))
|
||||
&& s.bounds.intersects(grown)
|
||||
})
|
||||
}
|
||||
/// `Shulker.canStayAt`: an empty spot against a sturdy face, with room
|
||||
/// for the fully opened shell (`canStayAt`).
|
||||
fn can_stay_at(m: &Mob, w: &MobWorld, p: [i32; 3], face: u8) -> bool {
|
||||
// isPositionBlocked: anything but air blocks, except a moving piston
|
||||
// being driven through the shulker's own cell.
|
||||
if !w.facts.is_air(w.world.block(p[0], p[1], p[2]))
|
||||
&& (p != block_pos(m) || w.block_name(p[0], p[1], p[2]) != "minecraft:moving_piston")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// loadedAndEntityCanStandOnFace: the supporting block's face toward
|
||||
// the shell is sturdy.
|
||||
let s = face_step(face);
|
||||
let n = w.world.block(p[0] + s[0], p[1] + s[1], p[2] + s[2]);
|
||||
w.facts.get(n).full_collision
|
||||
let support = w.world.block(p[0] + s[0], p[1] + s[1], p[2] + s[2]);
|
||||
let open = opposite(face);
|
||||
if w.facts.get(support).sturdy_full & (1 << open) == 0 {
|
||||
return false;
|
||||
}
|
||||
// The fully opened shell's box, shrunk by the skin vanilla uses.
|
||||
let opened = progress_aabb(
|
||||
open,
|
||||
-1.0,
|
||||
1.0,
|
||||
Vec3::new(
|
||||
f64::from(p[0]) + 0.5,
|
||||
f64::from(p[1]),
|
||||
f64::from(p[2]) + 0.5,
|
||||
),
|
||||
)
|
||||
.deflate(Vec3::new(1.0e-6, 1.0e-6, 1.0e-6));
|
||||
no_collision(m, w, opened)
|
||||
}
|
||||
/// `findAttachableSurface`: the first face (down, up, north, south, west,
|
||||
/// east) to cling to.
|
||||
fn attachable(w: &MobWorld, p: [i32; 3]) -> Option<u8> {
|
||||
(0..6).find(|&f| can_stay_at(w, p, f))
|
||||
fn attachable(m: &Mob, w: &MobWorld, p: [i32; 3]) -> Option<u8> {
|
||||
(0..6).find(|&f| can_stay_at(m, w, p, f))
|
||||
}
|
||||
/// `BlockUtil.packDifferenceInPosition` within 8 blocks of `pos`.
|
||||
fn pack_diff(pos: [i32; 3], to: [i32; 3]) -> i32 {
|
||||
((to[0] - pos[0] + 8) & 0xFF) << 16
|
||||
| ((to[1] - pos[1] + 8) & 0xFF) << 8
|
||||
| (to[2] - pos[2] + 8) & 0xFF
|
||||
}
|
||||
/// `Shulker.teleportSomewhere`: up to five tries within 8 blocks.
|
||||
fn teleport(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) -> bool {
|
||||
@@ -87,12 +162,31 @@ fn teleport(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) -> bool {
|
||||
for _ in 0..5 {
|
||||
let r = |m: &mut Mob| m.random.next_int_bound(17).unwrap_or(8) - 8;
|
||||
let t = [old[0] + r(m), old[1] + r(m), old[2] + r(m)];
|
||||
if t[1] <= crate::chunk::MIN_Y || !w.facts.is_air(w.world.block(t[0], t[1], t[2])) {
|
||||
let unit = Aabb::new(
|
||||
Vec3::new(f64::from(t[0]), f64::from(t[1]), f64::from(t[2])),
|
||||
Vec3::new(
|
||||
f64::from(t[0]) + 1.0,
|
||||
f64::from(t[1]) + 1.0,
|
||||
f64::from(t[2]) + 1.0,
|
||||
),
|
||||
);
|
||||
if t[1] <= crate::chunk::MIN_Y
|
||||
|| !w.facts.is_air(w.world.block(t[0], t[1], t[2]))
|
||||
|| !no_collision(m, w, unit.deflate(Vec3::new(1.0e-6, 1.0e-6, 1.0e-6)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let Some(face) = attachable(w, t) else {
|
||||
let Some(face) = attachable(m, w, t) else {
|
||||
continue;
|
||||
};
|
||||
// `#minecraft:shulker_does_not_teleport_to` (bedrock) is never clung to.
|
||||
let s = face_step(face);
|
||||
if w.has_tag(
|
||||
"minecraft:shulker_does_not_teleport_to",
|
||||
w.block_name(t[0] + s[0], t[1] + s[1], t[2] + s[2]),
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
m.extra.variant3 = face;
|
||||
events.push(MobEvent::Sound(
|
||||
"entity.shulker.teleport",
|
||||
@@ -106,6 +200,14 @@ fn teleport(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) -> bool {
|
||||
f64::from(t[2]) + 0.5,
|
||||
);
|
||||
m.previous = m.position;
|
||||
// Level.levelEvent(2016, oldPos, packedDiff): the teleport's
|
||||
// particles at both ends, seen by the players around.
|
||||
events.push(MobEvent::LevelEvent {
|
||||
id: 2016,
|
||||
pos: old,
|
||||
data: pack_diff(old, t),
|
||||
});
|
||||
m.needs_sync = true;
|
||||
m.extra.counter = 0;
|
||||
m.ai.target = None;
|
||||
return true;
|
||||
@@ -127,40 +229,107 @@ fn set_peek(m: &mut Mob, amount: i32, events: &mut Vec<MobEvent>) {
|
||||
));
|
||||
m.extra.counter = amount;
|
||||
}
|
||||
/// `Shulker.tick` additions: keep a face (or teleport), ease the peek,
|
||||
/// stay still and square on its block.
|
||||
/// `Shulker.updatePeekAmount`/`updatePeekAmountO`: the eased peek.
|
||||
fn ease_peek(m: &mut Mob) -> (f32, f32) {
|
||||
let old = m.anim.v[PEEK];
|
||||
let target = m.extra.counter as f32 * 0.01;
|
||||
let new = if old > target {
|
||||
(old - 0.05).clamp(target, 1.0)
|
||||
} else {
|
||||
(old + 0.05).clamp(0.0, target)
|
||||
};
|
||||
m.anim.v[PEEK] = new;
|
||||
(old, new)
|
||||
}
|
||||
/// `Shulker.tick` additions: keep a face (or teleport), ease the peek
|
||||
/// (pushing whatever it opens into), stay still and square on its block.
|
||||
pub fn tick(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
|
||||
m.velocity = Vec3::ZERO;
|
||||
m.body_yaw = 0.0;
|
||||
m.yaw = 0.0;
|
||||
m.head_pitch = 0.0;
|
||||
let p = block_pos(m);
|
||||
if !can_stay_at(w, p, m.extra.variant3) {
|
||||
match attachable(w, p) {
|
||||
if !can_stay_at(m, w, p, m.extra.variant3) {
|
||||
match attachable(m, w, p) {
|
||||
Some(f) => m.extra.variant3 = f,
|
||||
None => {
|
||||
teleport(m, w, events);
|
||||
}
|
||||
}
|
||||
}
|
||||
peek_step(m);
|
||||
}
|
||||
/// `Shulker.updatePeekAmount`: the lid eases to the raw peek.
|
||||
fn peek_step(m: &mut Mob) {
|
||||
let a = &mut m.anim;
|
||||
let target = m.extra.counter as f32 * 0.01;
|
||||
let cur = a.v[PEEK];
|
||||
if cur > target {
|
||||
a.v[PEEK] = (cur - 0.05).clamp(target, 1.0);
|
||||
} else if cur < target {
|
||||
a.v[PEEK] = (cur + 0.05).clamp(0.0, target);
|
||||
let (old, new) = ease_peek(m);
|
||||
if new != old {
|
||||
on_peek_change(m, w, old, new, events);
|
||||
}
|
||||
}
|
||||
/// `Shulker.getPhysicalPeek`: the lid's lift for a peek amount.
|
||||
fn physical_peek(amount: f32) -> f64 {
|
||||
f64::from(0.5 - ((0.5 + amount) * PI).msin() * 0.5)
|
||||
}
|
||||
/// `Shulker.onPeekAmountChange`: an opening shell pushes out of its way
|
||||
/// every entity that can be collided with (except other shulkers).
|
||||
fn on_peek_change(m: &Mob, w: &MobWorld, old: f32, new: f32, events: &mut Vec<MobEvent>) {
|
||||
let dir = opposite(m.extra.variant3);
|
||||
let s = face_step(dir).map(f64::from);
|
||||
let (old_p, new_p) = (physical_peek(old), physical_peek(new));
|
||||
let push = new_p - old_p;
|
||||
if push <= 0.0 {
|
||||
return;
|
||||
}
|
||||
let delta = [push * s[0], push * s[1], push * s[2]];
|
||||
let area = progress_aabb(dir, old_p, new_p, m.position);
|
||||
if let Some(p) = &w.player
|
||||
&& p.alive
|
||||
&& !p.spectator
|
||||
&& p.bounds.intersects(area)
|
||||
{
|
||||
events.push(MobEvent::Shift {
|
||||
target: Who::Player,
|
||||
delta,
|
||||
});
|
||||
}
|
||||
for seen in w.others_near(area) {
|
||||
if seen.id == m.id
|
||||
|| !seen.alive
|
||||
|| seen.species == Species::Shulker
|
||||
|| !seen.bounds.intersects(area)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
events.push(MobEvent::Shift {
|
||||
target: Who::Mob(seen.id),
|
||||
delta,
|
||||
});
|
||||
}
|
||||
}
|
||||
/// `ShulkerLookControl.getYRotD`: the look yaw in the open direction's
|
||||
/// plane (`None`: the target is on the head's own axis).
|
||||
pub fn look_yaw(m: &Mob, at: [f64; 3]) -> Option<f32> {
|
||||
let open = opposite(m.extra.variant3);
|
||||
// `attachFace.getRotation().transform(FORWARD)`: the model's forward
|
||||
// rotated as the renderer does (`rotations` uses the same quats).
|
||||
let forward = face_quat(m.extra.variant3) * glam::Vec3::Z;
|
||||
let s = face_step(open).map(|v| v as f32);
|
||||
let right = glam::Vec3::from_array(s).cross(forward);
|
||||
let out = glam::Vec3::new(
|
||||
(at[0] - m.position.x) as f32,
|
||||
(at[1] - m.eye_y()) as f32,
|
||||
(at[2] - m.position.z) as f32,
|
||||
);
|
||||
let (delta_right, delta_forward) = (right.dot(out), forward.dot(out));
|
||||
if delta_right.abs() <= 1.0e-5 && delta_forward.abs() <= 1.0e-5 {
|
||||
return None;
|
||||
}
|
||||
Some(horizon_core::geometry::trig::atan2_degrees(
|
||||
f64::from(-delta_right),
|
||||
f64::from(delta_forward),
|
||||
))
|
||||
}
|
||||
/// `Shulker.tick` on a client that is not the server: the lid eases to the
|
||||
/// peek it was sent, and its body stays square (`ShulkerBodyRotationControl`).
|
||||
pub fn client_tick(m: &mut Mob) {
|
||||
m.anim.vo = m.anim.v;
|
||||
peek_step(m);
|
||||
ease_peek(m);
|
||||
m.body_yaw = 0.0;
|
||||
}
|
||||
/// `Shulker.hurtServer` before the damage: arrows bounce off a closed shell.
|
||||
@@ -330,7 +499,7 @@ impl Goal for Peek {
|
||||
fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
|
||||
m.ai.target.is_none()
|
||||
&& m.random.next_int_bound(reduced(40)).unwrap_or(1) == 0
|
||||
&& can_stay_at(cx.w, block_pos(m), m.extra.variant3)
|
||||
&& can_stay_at(m, cx.w, block_pos(m), m.extra.variant3)
|
||||
}
|
||||
fn can_continue(&mut self, m: &mut Mob, _cx: &mut Cx) -> bool {
|
||||
m.ai.target.is_none() && self.time > 0
|
||||
@@ -373,24 +542,27 @@ pub fn texture(m: &Mob) -> String {
|
||||
None => "entity/shulker/shulker".into(),
|
||||
}
|
||||
}
|
||||
/// `attachFace.getOpposite().getRotation()` by attach face: the open
|
||||
/// direction's rotation (`Direction.getRotation` of `face ^ 1`).
|
||||
fn face_quat(face: u8) -> glam::Quat {
|
||||
match face.min(5) {
|
||||
// Attached below: upright.
|
||||
0 => glam::Quat::IDENTITY,
|
||||
1 => glam::Quat::from_rotation_x(PI),
|
||||
2 => glam::Quat::from_rotation_x(PI / 2.0),
|
||||
3 => glam::Quat::from_euler(glam::EulerRot::XYZ, PI / 2.0, 0.0, PI),
|
||||
4 => glam::Quat::from_euler(glam::EulerRot::XYZ, PI / 2.0, 0.0, -PI / 2.0),
|
||||
_ => glam::Quat::from_euler(glam::EulerRot::XYZ, PI / 2.0, 0.0, PI / 2.0),
|
||||
}
|
||||
}
|
||||
/// `ShulkerRenderer.setupRotations`: turned onto its attach face around the
|
||||
/// block's centre (after a half turn of the body).
|
||||
pub fn rotations(m: &Mob) -> glam::Mat4 {
|
||||
{
|
||||
let q = match m.extra.variant3 {
|
||||
// Attached below: upright.
|
||||
0 => glam::Quat::IDENTITY,
|
||||
1 => glam::Quat::from_rotation_x(PI),
|
||||
2 => glam::Quat::from_rotation_x(PI / 2.0),
|
||||
3 => glam::Quat::from_euler(glam::EulerRot::XYZ, PI / 2.0, 0.0, PI),
|
||||
4 => glam::Quat::from_euler(glam::EulerRot::XYZ, PI / 2.0, 0.0, -PI / 2.0),
|
||||
_ => glam::Quat::from_euler(glam::EulerRot::XYZ, PI / 2.0, 0.0, PI / 2.0),
|
||||
};
|
||||
glam::Mat4::from_rotation_y(PI)
|
||||
* glam::Mat4::from_translation(glam::Vec3::new(0.0, 0.5, 0.0))
|
||||
* glam::Mat4::from_quat(q)
|
||||
* glam::Mat4::from_translation(glam::Vec3::new(0.0, -0.5, 0.0))
|
||||
}
|
||||
let q = face_quat(m.extra.variant3);
|
||||
glam::Mat4::from_rotation_y(PI)
|
||||
* glam::Mat4::from_translation(glam::Vec3::new(0.0, 0.5, 0.0))
|
||||
* glam::Mat4::from_quat(q)
|
||||
* glam::Mat4::from_translation(glam::Vec3::new(0.0, -0.5, 0.0))
|
||||
}
|
||||
/// `ShulkerModel.setupAnim`: the lid rises and twists with the peek, the
|
||||
/// head turns.
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
//! 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` and `extra.home` `homePos`. Its own
|
||||
//! `TurtleMoveControl` sets the speed it walks and swims at. Not ported yet:
|
||||
//! `Turtle.travelInWater` (swimming still uses the generic fluid travel),
|
||||
//! `TurtlePathNavigation` and the rest of the turtle goal set, so movement in
|
||||
//! water still differs from vanilla.
|
||||
use super::{
|
||||
Mob, MobEvent, MobWorld,
|
||||
control::{EPSILON, Operation, rotlerp},
|
||||
goals::{Breed, Cx, Goal, JUMP, MOVE, MoveToBlock, goal_clone, reduced},
|
||||
};
|
||||
use horizon_core::random::RandomSource;
|
||||
use horizon_core::{geometry::trig, random::RandomSource};
|
||||
|
||||
pub fn register(m: &mut Mob) {
|
||||
let g = &mut m.ai.goals;
|
||||
@@ -49,6 +54,45 @@ fn block_of(m: &Mob) -> [i32; 3] {
|
||||
fn is_sand(w: &MobWorld, p: [i32; 3]) -> bool {
|
||||
w.has_tag("minecraft:sand", w.block_name(p[0], p[1], p[2]))
|
||||
}
|
||||
/// `Turtle.TurtleMoveControl.updateSpeed`: slow in water away from home
|
||||
/// (slower still as a baby), and halved every tick on land, never under 0.06.
|
||||
fn update_speed(m: &mut Mob) {
|
||||
if m.fluids.in_water {
|
||||
m.velocity.y += 0.005;
|
||||
if !close_to_home(m, 16.0) {
|
||||
m.set_speed((m.ai.speed / 2.0).max(0.08));
|
||||
}
|
||||
if m.is_baby() {
|
||||
m.set_speed((m.ai.speed / 3.0).max(0.06));
|
||||
}
|
||||
} else if m.on_ground {
|
||||
m.set_speed((m.ai.speed / 2.0).max(0.06));
|
||||
}
|
||||
}
|
||||
/// `Turtle.TurtleMoveControl.tick`: ease the speed an eighth of the way to
|
||||
/// the wanted one each tick (after `updateSpeed` cut it), face the wanted
|
||||
/// point and lift towards it; no jumping, and the operation is never reset.
|
||||
pub fn move_control(m: &mut Mob) {
|
||||
update_speed(m);
|
||||
if m.ai.mov.operation != Operation::MoveTo || m.ai.nav.is_done() {
|
||||
m.set_speed(0.0);
|
||||
return;
|
||||
}
|
||||
let [wx, wy, wz] = m.ai.mov.wanted;
|
||||
let (xd, yd, zd) = (wx - m.position.x, wy - m.position.y, wz - m.position.z);
|
||||
let dist = (xd * xd + yd * yd + zd * zd).sqrt();
|
||||
if dist < EPSILON {
|
||||
m.set_speed(0.0);
|
||||
return;
|
||||
}
|
||||
let y_rot = trig::atan2_degrees(zd, xd) - 90.0;
|
||||
m.yaw = rotlerp(m.yaw, y_rot, 90.0);
|
||||
m.body_yaw = m.yaw;
|
||||
let wanted = (m.ai.mov.speed_modifier * m.movement_speed()) as f32;
|
||||
// Mth.lerp(0.125F, getSpeed(), wanted).
|
||||
m.set_speed(m.ai.speed + 0.125 * (wanted - m.ai.speed));
|
||||
m.velocity.y += f64::from(m.ai.speed) * (yd / dist) * 0.1;
|
||||
}
|
||||
/// `Turtle.aiStep`: while digging, sand crumbs fly every fifth tick.
|
||||
pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
|
||||
if m.alive() && m.extra.flag2 && m.extra.counter >= 1 && m.extra.counter % 5 == 0 {
|
||||
@@ -207,3 +251,149 @@ impl Goal for GoHome {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::mobs::{
|
||||
Species,
|
||||
path::{Path, PathNode, PathType},
|
||||
};
|
||||
use horizon_core::geometry::Vec3;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// A turtle (movement speed 0.25) strolling (`speedModifier` 1.0) to a
|
||||
/// point ten blocks ahead along a path it has not finished.
|
||||
fn strolling_turtle() -> Mob {
|
||||
let mut kind = crate::mobs::sensor_scan_tests::kind();
|
||||
let k = Arc::make_mut(&mut kind);
|
||||
k.id = "minecraft:turtle".into();
|
||||
k.species = Species::Turtle;
|
||||
k.speed = 0.25;
|
||||
k.step_height = 1.0;
|
||||
let mut m = Mob::new(1, kind, Vec3::ZERO, 0.0);
|
||||
m.on_ground = true;
|
||||
let node = PathNode {
|
||||
x: 0,
|
||||
y: 0,
|
||||
z: 10,
|
||||
kind: PathType::Walkable,
|
||||
};
|
||||
m.ai.nav.path = Some(Path {
|
||||
nodes: vec![node],
|
||||
next: 0,
|
||||
target: [0, 0, 10],
|
||||
dist_to_target: 0.0,
|
||||
reached: true,
|
||||
});
|
||||
m.ai.mov.set_wanted(0.0, 0.0, 10.0, 1.0);
|
||||
m
|
||||
}
|
||||
fn speeds(m: &mut Mob, ticks: usize) -> Vec<f32> {
|
||||
(0..ticks)
|
||||
.map(|_| {
|
||||
move_control(m);
|
||||
assert_eq!(m.ai.zza, m.ai.speed, "setSpeed sets the forward input");
|
||||
m.ai.speed
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn walks_on_land_at_a_third_of_its_movement_speed() {
|
||||
// TurtleMoveControl.tick on the ground: updateSpeed makes it
|
||||
// max(speed / 2, 0.06) = 0.06, then Mth.lerp(0.125F, 0.06, 0.25),
|
||||
// every tick. The default MoveControl walked it at the full 0.25, so
|
||||
// the walk animation (and TurtleModel's 8 * walkSpeed leg swing) ran
|
||||
// about nine times too fast (#1776).
|
||||
let mut m = strolling_turtle();
|
||||
let land = 0.06_f32 + 0.125 * (0.25 - 0.06_f32);
|
||||
assert_eq!(speeds(&mut m, 40), vec![land; 40]);
|
||||
// Facing the wanted point, the body with it; it never jumps.
|
||||
assert_eq!(m.body_yaw, m.yaw);
|
||||
assert!(crate::mobs::living::wrap(m.yaw).abs() < 1.0e-3);
|
||||
assert!(!m.ai.jump);
|
||||
assert_eq!(m.ai.mov.operation, Operation::MoveTo);
|
||||
assert_eq!(m.velocity.y, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn water_speed_is_cut_away_from_home_and_for_babies() {
|
||||
// updateSpeed in water near home: no cut, the lerp climbs towards
|
||||
// 0.25, and it lifts the turtle 0.005 a tick. (Only the control's
|
||||
// speed: Turtle.travelInWater is not ported.)
|
||||
let mut m = strolling_turtle();
|
||||
m.on_ground = false;
|
||||
m.fluids.in_water = true;
|
||||
let s = speeds(&mut m, 60);
|
||||
assert_eq!(s[0], 0.125 * 0.25);
|
||||
assert!(s.windows(2).all(|w| w[1] > w[0]) && s[59] > 0.24 && s[59] < 0.25);
|
||||
assert!((m.velocity.y - 60.0 * 0.005).abs() < 1.0e-9);
|
||||
// More than 16 blocks from home: max(speed / 2, 0.08) first.
|
||||
let mut m = strolling_turtle();
|
||||
m.on_ground = false;
|
||||
m.fluids.in_water = true;
|
||||
m.extra.home = Some([0, 0, 40]);
|
||||
let far = 0.08_f32 + 0.125 * (0.25 - 0.08_f32);
|
||||
assert_eq!(speeds(&mut m, 20), vec![far; 20]);
|
||||
// A baby: max(speed / 3, 0.06).
|
||||
let mut m = strolling_turtle();
|
||||
m.on_ground = false;
|
||||
m.fluids.in_water = true;
|
||||
m.extra.age = -24000;
|
||||
let young = 0.06_f32 + 0.125 * (0.25 - 0.06_f32);
|
||||
assert_eq!(speeds(&mut m, 20), vec![young; 20]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stops_when_the_path_is_done_or_the_point_reached() {
|
||||
let mut m = strolling_turtle();
|
||||
m.ai.nav.stop();
|
||||
assert_eq!(speeds(&mut m, 1), vec![0.0]);
|
||||
let mut m = strolling_turtle();
|
||||
m.ai.mov.set_wanted(0.0, 0.0, 0.0, 1.0);
|
||||
assert_eq!(speeds(&mut m, 1), vec![0.0]);
|
||||
// updateSpeed runs before the early return: a stopped turtle in
|
||||
// water is still lifted 0.005, path done or not moving at all.
|
||||
let mut m = strolling_turtle();
|
||||
m.fluids.in_water = true;
|
||||
m.ai.nav.stop();
|
||||
assert_eq!(speeds(&mut m, 1), vec![0.0]);
|
||||
assert_eq!(m.velocity.y, 0.005);
|
||||
let mut m = strolling_turtle();
|
||||
m.fluids.in_water = true;
|
||||
m.ai.mov.operation = Operation::Wait;
|
||||
assert_eq!(speeds(&mut m, 1), vec![0.0]);
|
||||
assert_eq!(m.velocity.y, 0.005);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lifts_towards_a_point_above_by_the_slope_of_the_way_there() {
|
||||
// setDeltaMovement(... + getSpeed() * (dy / dist) * 0.1): two up,
|
||||
// three across and ten ahead.
|
||||
let mut m = strolling_turtle();
|
||||
m.ai.mov.set_wanted(3.0, 2.0, 10.0, 1.0);
|
||||
let speed = speeds(&mut m, 1)[0];
|
||||
let dist = (3.0_f64 * 3.0 + 2.0 * 2.0 + 10.0 * 10.0).sqrt();
|
||||
assert_eq!(m.velocity.y, f64::from(speed) * (2.0 / dist) * 0.1);
|
||||
assert!(m.velocity.y > 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn turns_at_most_ninety_degrees_a_tick_and_wraps_into_a_turn() {
|
||||
// rotlerp(yRot, target, 90): from 150 towards about -16.7 it turns
|
||||
// the full 90 to 60, then reaches the target, kept in [0, 360] as
|
||||
// about 343.3; the body follows the yaw each tick.
|
||||
let mut m = strolling_turtle();
|
||||
m.ai.mov.set_wanted(3.0, 0.0, 10.0, 1.0);
|
||||
m.yaw = 150.0;
|
||||
m.body_yaw = 150.0;
|
||||
let target = trig::atan2_degrees(10.0, 3.0) - 90.0;
|
||||
assert!((-17.0..-16.0).contains(&target));
|
||||
move_control(&mut m);
|
||||
assert_eq!((m.yaw, m.body_yaw), (60.0, 60.0));
|
||||
move_control(&mut m);
|
||||
assert_eq!(m.yaw, 60.0 + (target - 60.0) + 360.0);
|
||||
assert_eq!(m.body_yaw, m.yaw);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -905,6 +905,53 @@ mod tests {
|
||||
assert!(t.unset);
|
||||
assert_eq!(m.extra.variant2, 2);
|
||||
}
|
||||
/// The reported sequence: a villager works at a bench, the bench is
|
||||
/// destroyed (its job site is gone, so `ResetProfession` makes it
|
||||
/// unemployed) and it takes another bench's job. The new profession's
|
||||
/// trades are offered again.
|
||||
///
|
||||
/// `Villager.setVillagerData` drops the offers (`offers = null`) when the
|
||||
/// profession changes, so `AbstractVillager.getOffers` rolls the new
|
||||
/// profession's trade set the next time the offers are asked for. A
|
||||
/// villager that keeps its old offers marked as made never trades again:
|
||||
/// it shakes its head instead of opening the trading menu.
|
||||
#[test]
|
||||
fn a_villager_that_loses_its_job_site_offers_its_new_profession_trades() {
|
||||
let mut m = villager();
|
||||
// The composter's job: the villager is a farmer.
|
||||
set_profession(&mut m, 5);
|
||||
assert_eq!(m.extra.variant2, 5);
|
||||
// Its trades were made, so the offers are there.
|
||||
let trade = m.extra.trade.get_or_insert_default();
|
||||
trade.unset = false;
|
||||
trade.offers = vec![crate::merchant::MerchantOffer {
|
||||
base_cost_a: crate::merchant::ItemCost {
|
||||
stack: ItemStack::new("minecraft:wheat", 20),
|
||||
},
|
||||
cost_b: None,
|
||||
result: ItemStack::new("minecraft:emerald", 1),
|
||||
uses: 0,
|
||||
max_uses: 16,
|
||||
reward_exp: true,
|
||||
special_price_diff: 0,
|
||||
demand: 0,
|
||||
price_multiplier: 0.0,
|
||||
xp: 1,
|
||||
}];
|
||||
// The work bench is destroyed: the job site is gone, so the villager
|
||||
// is unemployed and its offers are dropped (`offers = null`).
|
||||
set_profession(&mut m, NONE);
|
||||
let t = m.extra.trade.as_ref().unwrap();
|
||||
assert!(t.offers.is_empty());
|
||||
assert!(t.unset);
|
||||
// It takes the smithing table's job. The new profession's trades are
|
||||
// rolled by the next `getOffers` (ensure_offers).
|
||||
set_profession(&mut m, 13);
|
||||
let t = m.extra.trade.as_ref().unwrap();
|
||||
assert!(t.offers.is_empty());
|
||||
assert!(t.unset);
|
||||
assert_eq!(m.extra.variant2, 13);
|
||||
}
|
||||
|
||||
fn stack(item: &str, count: u32) -> ItemStack {
|
||||
ItemStack::new(item, count)
|
||||
|
||||
Reference in New Issue
Block a user