Merge main into feat/energy-swirl
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -106,6 +106,7 @@ pub enum Family {
|
||||
Strider,
|
||||
Bat,
|
||||
Bee,
|
||||
PolarBear,
|
||||
}
|
||||
pub fn family(s: Species) -> Family {
|
||||
use Species as S;
|
||||
@@ -167,6 +168,7 @@ pub fn family(s: Species) -> Family {
|
||||
S::Strider => Family::Strider,
|
||||
S::Bat => Family::Bat,
|
||||
S::Bee => Family::Bee,
|
||||
S::PolarBear => Family::PolarBear,
|
||||
_ => Family::Generic,
|
||||
}
|
||||
}
|
||||
@@ -186,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),
|
||||
@@ -227,6 +243,7 @@ pub fn register(m: &mut Mob) {
|
||||
Family::Strider => super::strider::register(m),
|
||||
Family::Bat => super::bat::register(m),
|
||||
Family::Bee => super::bee::register(m),
|
||||
Family::PolarBear => super::polar_bear::register(m),
|
||||
}
|
||||
// FloatGoal's constructor lets the navigation float, as do the
|
||||
// constructors of these brain-driven and flying mobs. A floater rises
|
||||
@@ -416,6 +433,7 @@ pub fn tick(
|
||||
super::axolotl::tick_animation(m);
|
||||
m.anim.end_tick(m.ai.swinging);
|
||||
tick_swing(m);
|
||||
flower_contact_effects(m, w);
|
||||
if m.species() == Species::CopperGolem {
|
||||
// CopperGolem.tick: updateWeathering after the living tick.
|
||||
super::copper_golem::update_weathering(m, w, level_random, events);
|
||||
@@ -444,6 +462,36 @@ pub fn tick_swing(m: &mut Mob) {
|
||||
}
|
||||
m.anim.attack_anim = m.ai.swing_time as f32 / 6.0;
|
||||
}
|
||||
/// `Entity.isInRain`: rain at the block position, or at the top of the box.
|
||||
pub(super) fn in_rain(m: &Mob, w: &MobWorld) -> bool {
|
||||
let (x, y, z) = (
|
||||
m.position.x.floor() as i32,
|
||||
m.position.y.floor() as i32,
|
||||
m.position.z.floor() as i32,
|
||||
);
|
||||
let top = (m.position.y + f64::from(m.height())).floor() as i32;
|
||||
w.raining_at(x, y, z) || w.raining_at(x, top, z)
|
||||
}
|
||||
/// `LivingEntity.baseTick` refills a breather's air by 4, then
|
||||
/// `Axolotl.handleAirSupply` (unless the mob has no AI): water or rain fills
|
||||
/// it, anywhere else it runs down from the pre-tick value, and at -20 the
|
||||
/// axolotl dries out.
|
||||
fn axolotl_air_supply(m: &mut Mob, w: &MobWorld) {
|
||||
let before = m.air;
|
||||
m.air = (m.air + 4).min(super::axolotl::MAX_AIR);
|
||||
if m.no_ai {
|
||||
return;
|
||||
}
|
||||
if m.fluids.in_water || in_rain(m, w) {
|
||||
m.air = super::axolotl::MAX_AIR;
|
||||
} else {
|
||||
m.air = before - 1;
|
||||
if m.air <= -20 {
|
||||
m.air = 0;
|
||||
m.damage(2.0, Damage::of(DamageKind::DryOut));
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Entity.baseTick + LivingEntity.baseTick + Mob.baseTick.
|
||||
fn base_tick(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
|
||||
m.was_eye_in_water = m.fluids.eye_in_water;
|
||||
@@ -462,8 +510,9 @@ fn base_tick(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
|
||||
);
|
||||
m.damage(fire, Damage::of(DamageKind::InFire));
|
||||
}
|
||||
// Fire: water extinguishes, lava ignites, burning hurts every second.
|
||||
if m.fluids.in_water && m.fire_ticks > 0 {
|
||||
// Fire: water or rain (Entity.isInRain) extinguishes, lava ignites,
|
||||
// burning hurts every second.
|
||||
if m.fire_ticks > 0 && (m.fluids.in_water || in_rain(m, w)) {
|
||||
m.fire_ticks = 0;
|
||||
events.push(MobEvent::Sound(
|
||||
"entity.generic.extinguish_fire",
|
||||
@@ -492,18 +541,25 @@ 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).
|
||||
if m.kind.flags.needs_water {
|
||||
if m.species() == Species::Axolotl {
|
||||
axolotl_air_supply(m, w);
|
||||
} else if m.kind.flags.needs_water {
|
||||
if !m.fluids.in_water {
|
||||
m.air -= 1;
|
||||
if m.air == -20 {
|
||||
@@ -517,17 +573,20 @@ fn base_tick(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
|
||||
m.air -= 1;
|
||||
if m.air == -20 {
|
||||
m.air = 0;
|
||||
events.push(MobEvent::Drown {
|
||||
mob: m.id,
|
||||
at: [m.position.x, m.position.y, m.position.z],
|
||||
});
|
||||
m.damage(2.0, Damage::of(DamageKind::Drown));
|
||||
}
|
||||
} else if m.air < 300 {
|
||||
m.air = (m.air + 4).min(300);
|
||||
}
|
||||
|
||||
if m.kind.flags.water_sensitive
|
||||
&& (m.fluids.in_water || w.raining && w.can_see_sky(ex, ey, ez))
|
||||
{
|
||||
if m.kind.flags.water_sensitive && (m.fluids.in_water || in_rain(m, w)) {
|
||||
m.damage(1.0, Damage::of(DamageKind::Drown));
|
||||
}
|
||||
freeze_tick(m, w);
|
||||
}
|
||||
tick_effects(m);
|
||||
if m.species() == Species::Illusioner {
|
||||
@@ -557,6 +616,57 @@ fn base_tick(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
|
||||
// Entity.baseTick ends with Leashable.tickLeash.
|
||||
super::leash::tick(m, w, events);
|
||||
}
|
||||
/// `LivingEntity.aiStep`'s "freezing" with `InsideBlockEffectType.FREEZE`:
|
||||
/// powder snow builds `ticks_frozen` up, anywhere else it melts twice as
|
||||
/// fast, and a fully frozen mob takes one freeze damage every 40 ticks.
|
||||
fn freeze_tick(m: &mut Mob, w: &MobWorld) {
|
||||
let in_snow = in_powder_snow(m, w);
|
||||
if !in_snow && m.ticks_frozen == 0 {
|
||||
return;
|
||||
}
|
||||
let can_freeze = can_freeze(m, w);
|
||||
if in_snow && can_freeze {
|
||||
m.ticks_frozen =
|
||||
(m.ticks_frozen + 1).min(crate::motion::PlayerMotion::TICKS_REQUIRED_TO_FREEZE);
|
||||
} else {
|
||||
m.ticks_frozen = (m.ticks_frozen - 2).max(0);
|
||||
}
|
||||
if m.age.is_multiple_of(40) && m.is_fully_frozen() && can_freeze {
|
||||
m.damage(1.0, Damage::of(DamageKind::Freeze));
|
||||
}
|
||||
}
|
||||
/// Whether the mob's box (shrunk by `1.0E-5`, as in `Entity.checkInsideBlocks`)
|
||||
/// touches powder snow.
|
||||
fn in_powder_snow(m: &Mob, w: &MobWorld) -> bool {
|
||||
let b = m.bounds().deflate(Vec3::new(1e-5, 1e-5, 1e-5));
|
||||
let (min, max) = (b.min_position(), b.max_position());
|
||||
for x in min.x.floor() as i32..=max.x.floor() as i32 {
|
||||
for y in min.y.floor() as i32..=max.y.floor() as i32 {
|
||||
for z in min.z.floor() as i32..=max.z.floor() as i32 {
|
||||
if &*w.facts.get(w.world.block(x, y, z)).name == "minecraft:powder_snow" {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
/// `LivingEntity.canFreeze`: not a `#freeze_immune_entity_types` mob and no
|
||||
/// worn piece in `#freeze_immune_wearables`.
|
||||
fn can_freeze(m: &Mob, w: &MobWorld) -> bool {
|
||||
if w.data
|
||||
.entity_has("minecraft:freeze_immune_entity_types", &m.kind.id)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
let immune = w
|
||||
.items
|
||||
.and_then(|i| i.item_tags.get("minecraft:freeze_immune_wearables"));
|
||||
![&m.extra.head, &m.extra.chest, &m.extra.legs, &m.extra.feet]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.any(|item| immune.is_some_and(|t| t.contains(&**item)))
|
||||
}
|
||||
/// `Mob.playAmbientSound` on demand: a spawn egg or a mob bucket just placed `m`.
|
||||
pub fn ambient_sound(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
|
||||
play_ambient(m, w, events);
|
||||
@@ -644,7 +754,7 @@ fn ai_step(m: &mut Mob, w: &MobWorld, level_random: &mut LegacyRandom, events: &
|
||||
}
|
||||
// AbstractHorse.aiStep: the tail swishes now and then (moveTail).
|
||||
if super::equine::is_equine(m.species()) {
|
||||
let _ = m.random.next_int_bound(200);
|
||||
super::equine::tail_tick(m);
|
||||
}
|
||||
// Monster.aiStep: updateNoActionTime (Raider: always +2).
|
||||
if is_monster(m.species())
|
||||
@@ -671,6 +781,9 @@ fn ai_step(m: &mut Mob, w: &MobWorld, level_random: &mut LegacyRandom, events: &
|
||||
// serverAiStep has no goals, so it only falls and is pushed.
|
||||
let immobile = (m.species() == Species::Ravager && super::ravager::immobile(m))
|
||||
|| m.species() == Species::Mannequin;
|
||||
if m.species() == Species::Piglin && m.alive() {
|
||||
super::piglin::celebrate(m, w);
|
||||
}
|
||||
if m.alive() && !immobile {
|
||||
server_ai_step(m, w, level_random, events);
|
||||
} else {
|
||||
@@ -858,7 +971,7 @@ fn sun_burn_tick(m: &mut Mob, w: &MobWorld) -> bool {
|
||||
m.position.z.floor() as i32,
|
||||
);
|
||||
let br = w.brightness(x, y, z);
|
||||
let wet = m.fluids.in_water || w.raining && w.can_see_sky(x, y, z);
|
||||
let wet = m.fluids.in_water || in_rain(m, w);
|
||||
br > 0.5 && m.random.next_float() * 30.0 < (br - 0.4) * 2.0 && !wet && w.can_see_sky(x, y, z)
|
||||
}
|
||||
/// Mob.serverAiStep
|
||||
@@ -995,6 +1108,7 @@ fn species_ai_step(
|
||||
Species::Wither => super::wither::ai_step(m, w, events),
|
||||
Species::Allay => super::allay::ai_step(m),
|
||||
Species::Bee => super::bee::ai_step(m, w),
|
||||
Species::PolarBear => super::polar_bear::ai_step(m, w),
|
||||
Species::Breeze => super::breeze::ai_step(m),
|
||||
Species::Sniffer => super::sniffer::ai_step(m, events),
|
||||
Species::HappyGhast => super::happy_ghast::ai_step(m, w),
|
||||
@@ -1487,6 +1601,56 @@ pub fn custom_travel(m: &mut Mob, w: &MobWorld, input: Vec3, events: &mut Vec<Mo
|
||||
}
|
||||
}
|
||||
|
||||
/// `Entity.applyEffectsFromBlocks` for the flowers that give an effect:
|
||||
/// `WitherRoseBlock.entityInside` (wither for 40 ticks on a living mob that
|
||||
/// can take it, not in peaceful) and `EyeblossomBlock.entityInside` (poison
|
||||
/// for 25 ticks on a bee, when the open flower attracts bees). The blocks
|
||||
/// are those the mob's box touches where it started the tick and where it
|
||||
/// ended.
|
||||
fn flower_contact_effects(m: &mut Mob, w: &MobWorld) {
|
||||
if !m.alive() || w.difficulty.effective == 0.0 {
|
||||
return;
|
||||
}
|
||||
let half = f64::from(m.width()) / 2.0 - 1.0e-5;
|
||||
let height = f64::from(m.height()) - 1.0e-5;
|
||||
let mut visited: Vec<[i32; 3]> = Vec::new();
|
||||
for at in [m.previous, m.position] {
|
||||
let cells = |lo: f64, hi: f64| lo.floor() as i32..=hi.floor() as i32;
|
||||
for x in cells(at.x - half, at.x + half) {
|
||||
for y in cells(at.y + 1.0e-5, at.y + height) {
|
||||
for z in cells(at.z - half, at.z + half) {
|
||||
let p = [x, y, z];
|
||||
if visited.contains(&p) {
|
||||
continue;
|
||||
}
|
||||
match w.block_name(x, y, z) {
|
||||
"minecraft:wither_rose" => {
|
||||
// isInvulnerableTo(wither): the invulnerable time
|
||||
// after a hit; WitherBoss and WitherSkeleton
|
||||
// refuse the effect (`canBeAffected`).
|
||||
if m.invulnerable <= 0
|
||||
&& !m.invulnerable_flag
|
||||
&& !matches!(m.species(), Species::Wither | Species::WitherSkeleton)
|
||||
{
|
||||
m.add_effect("minecraft:wither", 40, 0);
|
||||
}
|
||||
}
|
||||
"minecraft:open_eyeblossom"
|
||||
if m.species() == Species::Bee
|
||||
&& super::bee::attracts_bees(w, p)
|
||||
&& !m.has_effect("minecraft:poison") =>
|
||||
{
|
||||
m.add_effect("minecraft:poison", 25, 0);
|
||||
}
|
||||
_ => continue,
|
||||
}
|
||||
visited.push(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// MobEffect ticking for the effects mobs receive (poison, wither, regeneration).
|
||||
fn tick_effects(m: &mut Mob) {
|
||||
let mut effects = std::mem::take(&mut m.extra.effects);
|
||||
@@ -1511,6 +1675,9 @@ fn tick_effects(m: &mut Mob) {
|
||||
*ticks -= 1;
|
||||
}
|
||||
effects.retain(|e| e.1 > 0);
|
||||
m.extra
|
||||
.hidden_effects
|
||||
.retain(|h| effects.iter().any(|e| e.0 == *h));
|
||||
m.extra.effects = effects;
|
||||
}
|
||||
|
||||
@@ -1591,8 +1758,20 @@ pub fn step_sound(
|
||||
S::Husk => quiet("entity.husk.step"),
|
||||
S::Drowned => quiet("entity.drowned.step"),
|
||||
S::ZombieVillager => quiet("entity.zombie_villager.step"),
|
||||
S::CamelHusk => Some(("entity.camel_husk.step", 0.4, 1.0)),
|
||||
S::Camel => Some(("entity.camel.step", 1.0, 1.0)),
|
||||
// Camel.playStepSound and CamelHusk.playStepSound: sand and concrete
|
||||
// powder have their own step.
|
||||
S::CamelHusk | S::Camel => {
|
||||
let sand = w.has_tag(
|
||||
"minecraft:camel_sand_step_sound_blocks",
|
||||
&w.facts.get(state).name,
|
||||
);
|
||||
Some(match (m.species(), sand) {
|
||||
(S::Camel, true) => ("entity.camel.step_sand", 1.0, 1.0),
|
||||
(S::Camel, false) => ("entity.camel.step", 1.0, 1.0),
|
||||
(_, true) => ("entity.camel_husk.step_sand", 0.4, 1.0),
|
||||
(_, false) => ("entity.camel_husk.step", 0.4, 1.0),
|
||||
})
|
||||
}
|
||||
S::IronGolem => Some(("entity.iron_golem.step", 1.0, 1.0)),
|
||||
// CopperGolem.playStepSound: its oxidation level's step.
|
||||
S::CopperGolem => Some((super::copper_golem::sound(m, "step"), 1.0, 1.0)),
|
||||
@@ -1671,3 +1850,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,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,7 +197,8 @@ pub fn state(m: &Mob, alpha: f32) -> State {
|
||||
let walking = m.alive() && !m.extra.passenger;
|
||||
State {
|
||||
pos: if walking {
|
||||
m.limb_position - m.limb_speed * (1.0 - alpha)
|
||||
// WalkAnimationState.position(partial): babies' legs move 3 times as far.
|
||||
(m.limb_position - m.limb_speed * (1.0 - alpha)) * if m.is_baby() { 3.0 } else { 1.0 }
|
||||
} else {
|
||||
0.0
|
||||
},
|
||||
@@ -248,18 +249,24 @@ pub enum ArmPose {
|
||||
pub fn humanoid_pub(p: &mut Poses, s: &State, _m: &Mob) {
|
||||
humanoid(p, s, &Hum::new(ArmPose::Empty, ArmPose::Empty));
|
||||
}
|
||||
/// `AnimationUtils.swingWeaponDown` with the right arm as the main arm.
|
||||
pub(super) fn swing_weapon_down(p: &mut Poses, swing: f32, age: f32) {
|
||||
/// `AnimationUtils.swingWeaponDown`: the main arm swings down, the other
|
||||
/// one hangs and bobs.
|
||||
pub(super) fn swing_weapon_down(p: &mut Poses, swing: f32, age: f32, left_main: bool) {
|
||||
let attack2 = (swing * PI).msin();
|
||||
let attack = ((1.0 - (1.0 - swing) * (1.0 - swing)) * PI).msin();
|
||||
let (main, off) = if left_main {
|
||||
("left_arm", "right_arm")
|
||||
} else {
|
||||
("right_arm", "left_arm")
|
||||
};
|
||||
p.zrot("right_arm", 0.0);
|
||||
p.zrot("left_arm", 0.0);
|
||||
p.yrot("right_arm", PI / 20.0);
|
||||
p.yrot("left_arm", -PI / 20.0);
|
||||
p.xrot("right_arm", -1.884_955_8 + (age * 0.09).mcos() * 0.15);
|
||||
p.xrot("left_arm", -0.0 + (age * 0.19).mcos() * 0.5);
|
||||
p.get("right_arm")[3] += attack2 * 2.2 - attack * 0.4;
|
||||
p.get("left_arm")[3] += attack2 * 1.2 - attack * 0.4;
|
||||
p.xrot(main, -1.884_955_8 + (age * 0.09).mcos() * 0.15);
|
||||
p.xrot(off, -0.0 + (age * 0.19).mcos() * 0.5);
|
||||
p.get(main)[3] += attack2 * 2.2 - attack * 0.4;
|
||||
p.get(off)[3] += attack2 * 1.2 - attack * 0.4;
|
||||
bob(p, "right_arm", age, 1.0);
|
||||
bob(p, "left_arm", age, -1.0);
|
||||
}
|
||||
@@ -286,6 +293,8 @@ pub struct Hum {
|
||||
pub using_item: bool,
|
||||
/// The arm the current swing moves (`Some(true)` for the right one).
|
||||
pub swing_right_arm: Option<bool>,
|
||||
/// `mainArm` is the left one (`Mob.isLeftHanded`): a mob's swing uses it.
|
||||
pub left_main: bool,
|
||||
}
|
||||
impl Hum {
|
||||
pub fn new(right: ArmPose, left: ArmPose) -> Self {
|
||||
@@ -303,6 +312,7 @@ impl Hum {
|
||||
swim: 0.0,
|
||||
using_item: false,
|
||||
swing_right_arm: None,
|
||||
left_main: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -315,7 +325,7 @@ impl ArmPose {
|
||||
}
|
||||
}
|
||||
/// `AnimationUtils.animateCrossbowHold`.
|
||||
fn crossbow_hold(p: &mut Poses, hx: f32, hy: f32, in_right: bool) {
|
||||
pub(super) fn crossbow_hold(p: &mut Poses, hx: f32, hy: f32, in_right: bool) {
|
||||
let (hold, shoot) = if in_right {
|
||||
("right_arm", "left_arm")
|
||||
} else {
|
||||
@@ -327,7 +337,7 @@ fn crossbow_hold(p: &mut Poses, hx: f32, hy: f32, in_right: bool) {
|
||||
p.xrot(shoot, -1.5 + hx);
|
||||
}
|
||||
/// `AnimationUtils.animateCrossbowCharge`.
|
||||
fn crossbow_charge(p: &mut Poses, max: f32, ticks: f32, in_right: bool) {
|
||||
pub(super) fn crossbow_charge(p: &mut Poses, max: f32, ticks: f32, in_right: bool) {
|
||||
let (hold, pull) = if in_right {
|
||||
("right_arm", "left_arm")
|
||||
} else {
|
||||
@@ -418,13 +428,17 @@ pub(super) fn humanoid(p: &mut Poses, s: &State, h: &Hum) {
|
||||
pose_arm(p, h.right, true, h);
|
||||
}
|
||||
}
|
||||
// setupAttackAnimation (WHACK swing with the right arm).
|
||||
// setupAttackAnimation (WHACK swing with the arm of the swinging hand).
|
||||
let hx = p.get("head")[3];
|
||||
let swing = s.swing;
|
||||
let swing_left = h.swing_right_arm.map_or(h.left_main, |right| !right);
|
||||
if swing > 0.0 && h.weapon_swing {
|
||||
swing_weapon_down(p, swing, s.age);
|
||||
swing_weapon_down(p, swing, s.age, h.left_main);
|
||||
} else if swing > 0.0 {
|
||||
let body_y = (swing.sqrt() * PI * 2.0).msin() * 0.2;
|
||||
let mut body_y = (swing.sqrt() * PI * 2.0).msin() * 0.2;
|
||||
if swing_left {
|
||||
body_y = -body_y;
|
||||
}
|
||||
p.yrot("body", body_y);
|
||||
let age_scale = if s.baby { 0.5 } else { 1.0 };
|
||||
{
|
||||
@@ -444,7 +458,7 @@ pub(super) fn humanoid(p: &mut Poses, s: &State, h: &Hum) {
|
||||
let eased = 1.0 - (1.0 - swing).powi(4);
|
||||
let aa = (eased * PI).msin();
|
||||
let bb = (swing * PI).msin() * -(hx - 0.7) * 0.75;
|
||||
let r = p.get("right_arm");
|
||||
let r = p.get(if swing_left { "left_arm" } else { "right_arm" });
|
||||
r[3] -= aa * 1.2 + bb;
|
||||
r[4] += body_y * 2.0;
|
||||
r[5] += (swing * PI).msin() * -0.4;
|
||||
@@ -652,6 +666,7 @@ pub fn setup(
|
||||
} else {
|
||||
Hum::new(main, ArmPose::Empty)
|
||||
};
|
||||
h.left_main = m.main_arm_left();
|
||||
h.zombie = Some(m.ai.aggressive);
|
||||
h.holding = m.extra.main_hand.is_some();
|
||||
h.crouching = m.extra.crouching;
|
||||
@@ -715,6 +730,7 @@ pub fn setup(
|
||||
h.swim = m.extra.swim.amount(s.partial);
|
||||
h.using_item = m.extra.using_item > 0;
|
||||
h.swing_right_arm = m.extra.swing_right_arm;
|
||||
h.left_main = m.main_arm_left();
|
||||
humanoid(p, s, &h);
|
||||
if m.ai.aggressive && !bow {
|
||||
// SkeletonModel melee pose.
|
||||
|
||||
@@ -24,6 +24,58 @@ const Z_BODY_ROT: usize = 3;
|
||||
const TENTACLE_SPEED: usize = 4;
|
||||
const ROTATE_SPEED: usize = 5;
|
||||
|
||||
/// `Squid.spawnInk`: the squirt sound and 30 ink particles shot from under
|
||||
/// the squid, turned by its body rotation.
|
||||
pub fn spawn_ink(m: &mut Mob) {
|
||||
let sound = if m.species() == Species::GlowSquid {
|
||||
"entity.glow_squid.squirt"
|
||||
} else {
|
||||
"entity.squid.squirt"
|
||||
};
|
||||
let kind = if m.species() == Species::GlowSquid {
|
||||
"glow_squid_ink"
|
||||
} else {
|
||||
"squid_ink"
|
||||
};
|
||||
// makeSound draws the voice pitch even for a silent squid.
|
||||
let pitch = m.voice_pitch();
|
||||
if !m.silent {
|
||||
let at = [m.position.x, m.position.y, m.position.z];
|
||||
m.outbox.push(MobEvent::Sound(
|
||||
sound,
|
||||
at,
|
||||
super::ai::sound_volume(m),
|
||||
pitch,
|
||||
));
|
||||
}
|
||||
// rotateVector: xRot(xBodyRotO), then yRot(-yBodyRotO).
|
||||
let to_radians = (std::f64::consts::PI / 180.0) as f32;
|
||||
let (x_rot, y_rot) = (
|
||||
m.anim.vo[X_BODY_ROT] * to_radians,
|
||||
-m.previous_body_yaw * to_radians,
|
||||
);
|
||||
let rotate = |v: Vec3| v.rotate_x(x_rot).rotate_y(y_rot);
|
||||
let pos = rotate(Vec3::new(0.0, -1.0, 0.0)).add_vector(m.position);
|
||||
let offset_scale: f32 = if m.is_baby() { 0.1 } else { 0.3 };
|
||||
for _ in 0..30 {
|
||||
let x = f64::from(m.random.next_float()) * 0.6 - 0.3;
|
||||
let z = f64::from(m.random.next_float()) * 0.6 - 0.3;
|
||||
let dir = rotate(Vec3::new(x, -1.0, z));
|
||||
let d = dir.scale(f64::from(offset_scale + m.random.next_float() * 2.0));
|
||||
// ServerLevel.sendParticles with a count of 0 and speed 0.1: one
|
||||
// particle moving at the float offset times the float speed.
|
||||
let speed = f64::from(0.1f32);
|
||||
m.outbox.push(MobEvent::ParticleAt {
|
||||
kind,
|
||||
at: [pos.x, pos.y + 0.5, pos.z],
|
||||
velocity: [
|
||||
speed * f64::from(d.x as f32),
|
||||
speed * f64::from(d.y as f32),
|
||||
speed * f64::from(d.z as f32),
|
||||
],
|
||||
});
|
||||
}
|
||||
}
|
||||
pub fn register(m: &mut Mob) {
|
||||
use Species as S;
|
||||
match m.species() {
|
||||
@@ -36,6 +88,7 @@ pub fn register(m: &mut Mob) {
|
||||
m.anim.v[TENTACLE_SPEED] = speed;
|
||||
}
|
||||
S::Axolotl => {
|
||||
m.air = super::axolotl::MAX_AIR;
|
||||
// Native controller scaling is essential: the base speed attribute is 1.0.
|
||||
m.ai.mov.kind = MoveKind::SmoothSwimming {
|
||||
max_turn_x: 85.0,
|
||||
@@ -206,7 +259,7 @@ pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
|
||||
}
|
||||
S::Dolphin => {
|
||||
// Dolphin.tick: moisture out of water, hop on land.
|
||||
if m.fluids.in_water || w.raining {
|
||||
if m.fluids.in_water || super::ai::in_rain(m, w) {
|
||||
m.extra.counter = 2400;
|
||||
} else {
|
||||
m.extra.counter -= 1;
|
||||
|
||||
@@ -3,6 +3,8 @@ use super::{Mob, MobEvent, MobWorld, Species, Who, animal_brain, keyframes::Anim
|
||||
use horizon_core::random::RandomSource;
|
||||
pub const PLAY_DEAD_TICKS: i32 = 200;
|
||||
pub const HUNTING_COOLDOWN: i64 = 2400;
|
||||
/// `Axolotl.getMaxAirSupply`.
|
||||
pub const MAX_AIR: i32 = 6000;
|
||||
|
||||
/// AxolotlAttackablesSensor: always-hostile targets bypass the hunting cooldown.
|
||||
pub fn prey_eligible(
|
||||
|
||||
@@ -959,7 +959,7 @@ pub fn anim(m: &Mob, s: &State, p: &mut Poses, a: &Animations) -> bool {
|
||||
let st = &m.anim.states;
|
||||
match m.species() {
|
||||
Species::Goat => goat(p, s, &Goat::from_mob(m)),
|
||||
Species::PolarBear => polar_bear(p, s, 0.0),
|
||||
Species::PolarBear => polar_bear(p, s, super::polar_bear::standing_scale(m, s.partial)),
|
||||
Species::Panda => panda(p, s, &Panda::default()),
|
||||
Species::Llama | Species::TraderLlama => {
|
||||
llama(p, s, m.extra.chested && !s.baby);
|
||||
@@ -980,9 +980,10 @@ pub fn anim(m: &Mob, s: &State, p: &mut Poses, a: &Animations) -> bool {
|
||||
Species::Parrot => {
|
||||
let flap = m.anim.flap_o + (m.anim.flap - m.anim.flap_o) * s.partial;
|
||||
let speed = m.anim.flap_speed_o + (m.anim.flap_speed - m.anim.flap_speed_o) * s.partial;
|
||||
// `Parrot.isFlying` is `!onGround`, in water too.
|
||||
let pose = if m.extra.sitting {
|
||||
ParrotPose::Sitting
|
||||
} else if !m.on_ground && !m.fluids.in_water {
|
||||
} else if !m.on_ground {
|
||||
ParrotPose::Flying
|
||||
} else {
|
||||
ParrotPose::Standing
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
//! Mob controls: `MoveControl` (with the flying and smooth-swimming
|
||||
//! variants), `LookControl`, `JumpControl` and `BodyRotationControl`.
|
||||
use super::{Mob, MobWorld, living::wrap};
|
||||
use super::{Mob, MobWorld, Species, living::wrap};
|
||||
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 {
|
||||
@@ -113,8 +116,16 @@ pub struct BodyRotation {
|
||||
last_stable_head: f32,
|
||||
}
|
||||
impl Mob {
|
||||
/// `Mob.getMaxHeadXRot`, with `Wolf`'s 20 degrees while sitting.
|
||||
pub fn max_head_x_rot(&self) -> f32 {
|
||||
if self.species() == Species::Wolf && self.extra.sitting {
|
||||
20.0
|
||||
} else {
|
||||
self.kind.max_head_x_rot
|
||||
}
|
||||
}
|
||||
pub fn look_at(&mut self, at: [f64; 3]) {
|
||||
let (y, x) = (self.kind.head_rot_speed, self.kind.max_head_x_rot);
|
||||
let (y, x) = (self.kind.head_rot_speed, self.max_head_x_rot());
|
||||
self.ai.look.set_look_at(at, y, x);
|
||||
}
|
||||
pub fn look_at_speed(&mut self, at: [f64; 3], y_speed: f32, x_max: f32) {
|
||||
@@ -135,6 +146,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 +359,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;
|
||||
|
||||
@@ -184,15 +184,7 @@ pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (m.fluids.in_water
|
||||
|| w.raining
|
||||
&& w.can_see_sky(
|
||||
m.position.x.floor() as i32,
|
||||
m.eye_y().floor() as i32,
|
||||
m.position.z.floor() as i32,
|
||||
))
|
||||
&& m.alive()
|
||||
{
|
||||
if (m.fluids.in_water || super::ai::in_rain(m, w)) && m.alive() {
|
||||
// Hurt by water: teleport away.
|
||||
m.extra.counter2 = m.extra.counter2.max(1);
|
||||
}
|
||||
|
||||
@@ -28,6 +28,8 @@ pub const MARKINGS: [&str; 5] = ["", "white", "whitefield", "whitedots", "blackd
|
||||
pub const EAT: usize = 0;
|
||||
pub const STAND: usize = 1;
|
||||
pub const MOUTH: usize = 2;
|
||||
/// `AbstractHorse.tailCounter`: client-side, it only drives the model.
|
||||
pub const TAIL: usize = 3;
|
||||
|
||||
/// What a rider asks of its mount this tick.
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
@@ -418,6 +420,20 @@ pub fn ridden_step(m: &mut Mob) {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `AbstractHorse.aiStep`'s `moveTail` (one in 200 ticks) and the counter's
|
||||
/// step in `tick` (it runs to 8, then stops). A client runs it on its own
|
||||
/// random, as vanilla's client-side `aiStep` does.
|
||||
pub fn tail_tick(m: &mut Mob) {
|
||||
if m.random.next_int_bound(200).unwrap_or(1) == 0 {
|
||||
m.anim.v[TAIL] = 1.0;
|
||||
}
|
||||
if m.anim.v[TAIL] > 0.0 {
|
||||
m.anim.v[TAIL] += 1.0;
|
||||
if m.anim.v[TAIL] > 8.0 {
|
||||
m.anim.v[TAIL] = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `AbstractHorse.aiStep` and `tick`: healing, grazing, the rear and the
|
||||
/// eating/standing/mouth animations.
|
||||
pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
|
||||
@@ -603,7 +619,15 @@ pub fn anim(m: &Mob, s: &State, p: &mut Poses) {
|
||||
t[1] += speed * age;
|
||||
t[2] += speed * 2.0 * age;
|
||||
}
|
||||
p.yrot("tail", 0.0);
|
||||
// animateTail: the tail swings while the counter runs.
|
||||
p.yrot(
|
||||
"tail",
|
||||
if m.anim.v[TAIL] > 0.0 {
|
||||
(s.age * 0.7).mcos()
|
||||
} else {
|
||||
0.0
|
||||
},
|
||||
);
|
||||
if baby && donkey {
|
||||
// BabyDonkeyModel overrides the state's pitch with -30 degrees and
|
||||
// sets the head parts again.
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1017,6 +1017,8 @@ pub struct MeleeAttack {
|
||||
pub speed: f64,
|
||||
pub follow_unseen: bool,
|
||||
pub zombie: bool,
|
||||
/// `PolarBear.PolarBearMeleeAttackGoal`: rears up before each strike.
|
||||
pub polar_bear: bool,
|
||||
path_target: [f64; 3],
|
||||
recalc: i32,
|
||||
pub until_attack: i32,
|
||||
@@ -1031,6 +1033,7 @@ impl MeleeAttack {
|
||||
speed,
|
||||
follow_unseen,
|
||||
zombie: false,
|
||||
polar_bear: false,
|
||||
path_target: [0.0; 3],
|
||||
recalc: 0,
|
||||
until_attack: 0,
|
||||
@@ -1043,6 +1046,10 @@ impl MeleeAttack {
|
||||
self.zombie = true;
|
||||
self
|
||||
}
|
||||
pub fn polar_bear(mut self) -> Self {
|
||||
self.polar_bear = true;
|
||||
self
|
||||
}
|
||||
pub fn interval(mut self, ticks: i32) -> Self {
|
||||
self.interval = ticks;
|
||||
self
|
||||
@@ -1098,6 +1105,9 @@ impl Goal for MeleeAttack {
|
||||
}
|
||||
m.ai.aggressive = false;
|
||||
m.nav_stop();
|
||||
if self.polar_bear {
|
||||
super::polar_bear::set_standing(m, false);
|
||||
}
|
||||
}
|
||||
fn every_tick(&self) -> bool {
|
||||
true
|
||||
@@ -1131,7 +1141,17 @@ impl Goal for MeleeAttack {
|
||||
self.recalc = adjusted(true, self.recalc);
|
||||
}
|
||||
self.until_attack = (self.until_attack - 1).max(0);
|
||||
if self.until_attack <= 0 && m.within_melee_range(&t) && m.sees(cx.w, who) {
|
||||
if self.polar_bear {
|
||||
let cooldown = adjusted(true, self.interval);
|
||||
super::polar_bear::check_and_perform_attack(
|
||||
&mut self.until_attack,
|
||||
cooldown,
|
||||
m,
|
||||
who,
|
||||
&t,
|
||||
cx,
|
||||
);
|
||||
} else if self.until_attack <= 0 && m.within_melee_range(&t) && m.sees(cx.w, who) {
|
||||
self.until_attack = adjusted(true, self.interval);
|
||||
m.swing();
|
||||
super::ai::do_hurt_target(m, who, cx);
|
||||
@@ -1422,6 +1442,9 @@ pub struct HurtByTarget {
|
||||
/// `Bee.BeeHurtByOtherGoal`: kept only while angry, and alerting others
|
||||
/// only when the attacker is in sight.
|
||||
pub bee: bool,
|
||||
/// `PolarBear.PolarBearHurtByTargetGoal`: a hurt cub only calls the
|
||||
/// adult bears and drops the target itself.
|
||||
pub polar_bear: bool,
|
||||
timestamp: i32,
|
||||
st: TargetState,
|
||||
}
|
||||
@@ -1432,6 +1455,7 @@ impl HurtByTarget {
|
||||
alert_except: vec![],
|
||||
ignore: vec![],
|
||||
bee: false,
|
||||
polar_bear: false,
|
||||
timestamp: 0,
|
||||
st: TargetState {
|
||||
unseen_memory: 300,
|
||||
@@ -1451,6 +1475,10 @@ impl HurtByTarget {
|
||||
self.bee = true;
|
||||
self
|
||||
}
|
||||
pub fn polar_bear(mut self) -> Self {
|
||||
self.polar_bear = true;
|
||||
self
|
||||
}
|
||||
}
|
||||
impl Default for HurtByTarget {
|
||||
fn default() -> Self {
|
||||
@@ -1462,6 +1490,8 @@ impl Goal for HurtByTarget {
|
||||
fn name(&self) -> &'static str {
|
||||
if self.bee {
|
||||
"BeeHurtByOtherGoal"
|
||||
} else if self.polar_bear {
|
||||
"PolarBearHurtByTargetGoal"
|
||||
} else {
|
||||
"HurtByTarget"
|
||||
}
|
||||
@@ -1500,7 +1530,9 @@ impl Goal for HurtByTarget {
|
||||
self.timestamp = m.ai.last_hurt_by_time;
|
||||
self.st.unseen_memory = 300;
|
||||
self.st.unseen = 0;
|
||||
if self.alert
|
||||
// PolarBearHurtByTargetGoal.start: a cub alerts the adults itself.
|
||||
let cub = self.polar_bear && m.is_baby();
|
||||
if (self.alert || cub)
|
||||
&& let Some(by) = m.ai.target
|
||||
{
|
||||
let range = m.attrs.follow_range;
|
||||
@@ -1520,6 +1552,8 @@ impl Goal for HurtByTarget {
|
||||
&& (s.position.z - pos.z).abs() <= range + 1.0
|
||||
})
|
||||
.filter(|s| !self.alert_except.contains(&s.species))
|
||||
// PolarBearHurtByTargetGoal.alertOther: adults only.
|
||||
.filter(|s| !(self.polar_bear && s.baby))
|
||||
.filter(|s| Who::Mob(s.id) != by)
|
||||
.map(|s| s.id)
|
||||
.collect();
|
||||
@@ -1534,6 +1568,9 @@ impl Goal for HurtByTarget {
|
||||
});
|
||||
}
|
||||
}
|
||||
if cub {
|
||||
m.ai.target = None;
|
||||
}
|
||||
}
|
||||
fn stop(&mut self, m: &mut Mob, _: &mut Cx) {
|
||||
m.ai.target = None;
|
||||
@@ -1741,7 +1778,7 @@ impl Goal for Tempt {
|
||||
}
|
||||
fn tick(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
let Some(p) = cx.w.player.clone() else { return };
|
||||
let (y, x) = (m.kind.max_head_y_rot + 20.0, m.kind.max_head_x_rot);
|
||||
let (y, x) = (m.kind.max_head_y_rot + 20.0, m.max_head_x_rot());
|
||||
m.look_at_speed(p.eye, y, x);
|
||||
if m.distance_sqr(p.position) < self.stop_distance * self.stop_distance {
|
||||
m.nav_stop();
|
||||
@@ -1850,7 +1887,7 @@ impl Goal for Breed {
|
||||
let Some(p) = self.partner.and_then(|p| cx.w.seen(p)).cloned() else {
|
||||
return;
|
||||
};
|
||||
let x = m.kind.max_head_x_rot;
|
||||
let x = m.max_head_x_rot();
|
||||
m.look_at_speed([p.position.x, p.eye_y, p.position.z], 10.0, x);
|
||||
m.nav_move_to_entity(
|
||||
cx.w,
|
||||
|
||||
@@ -544,14 +544,15 @@ pub fn ai_step(m: &mut Mob, w: &MobWorld) {
|
||||
m.ai.home = Some((here, radius));
|
||||
}
|
||||
}
|
||||
// continuousHeal: a point every 30 seconds, every second in rain.
|
||||
// continuousHeal: a point every 30 seconds, every second in rain or snow
|
||||
// (Level.precipitationAt).
|
||||
if m.alive() && m.death_time == 0 && m.health < m.attrs.max_health {
|
||||
let (x, y, z) = (
|
||||
m.position.x.floor() as i32,
|
||||
m.position.y.floor() as i32,
|
||||
m.position.z.floor() as i32,
|
||||
);
|
||||
let wet = w.raining && w.can_see_sky(x, y, z);
|
||||
let wet = w.precipitation_at(x, y, z).is_some();
|
||||
let interval = if wet { 20 } else { 600 };
|
||||
if m.age.is_multiple_of(interval) {
|
||||
m.health = (m.health + 1.0).min(m.attrs.max_health);
|
||||
|
||||
@@ -681,7 +681,12 @@ pub fn anim_pose(m: &Mob, s: &State, p: &mut Poses, pose: ArmPose) {
|
||||
p.xrot("right_arm", x + a * 1.2 - b * 0.4);
|
||||
p.xrot("left_arm", x + a * 1.2 - b * 0.4);
|
||||
} else {
|
||||
// swingWeaponDown (right-handed).
|
||||
// swingWeaponDown: the main arm swings down.
|
||||
let (main, off) = if m.main_arm_left() {
|
||||
("left_arm", "right_arm")
|
||||
} else {
|
||||
("right_arm", "left_arm")
|
||||
};
|
||||
let swing = s.swing;
|
||||
let a2 = (swing * PI).msin();
|
||||
let a = ((1.0 - (1.0 - swing) * (1.0 - swing)) * PI).msin();
|
||||
@@ -690,11 +695,11 @@ pub fn anim_pose(m: &Mob, s: &State, p: &mut Poses, pose: ArmPose) {
|
||||
p.yrot("right_arm", PI / 20.0);
|
||||
p.yrot("left_arm", -PI / 20.0);
|
||||
p.xrot(
|
||||
"right_arm",
|
||||
main,
|
||||
-1.884_955_8 + (s.age * 0.09).mcos() * 0.15 + (a2 * 2.2 - a * 0.4),
|
||||
);
|
||||
p.xrot(
|
||||
"left_arm",
|
||||
off,
|
||||
-0.0 + (s.age * 0.19).mcos() * 0.5 + (a2 * 1.2 - a * 0.4),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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`:
|
||||
@@ -281,6 +301,15 @@ impl Mob {
|
||||
return (f64::from(0.2_f32), f64::from(0.2_f32), f64::from(0.2_f32));
|
||||
}
|
||||
let k = &self.kind;
|
||||
// Avatar.POSES: a player's or mannequin's box follows its pose.
|
||||
if matches!(&*k.id, "minecraft:player" | "minecraft:mannequin") {
|
||||
let d = self.extra.avatar_pose.dimensions(1.0);
|
||||
return (
|
||||
f64::from(d.width),
|
||||
f64::from(d.height),
|
||||
f64::from(d.eye_height),
|
||||
);
|
||||
}
|
||||
let (mut w, mut h, mut e) = (k.width as f32, k.height as f32, k.eye_height as f32);
|
||||
// Sniffer.DIGGING_DIMENSIONS: 0.4 lower, its eyes at 0.81.
|
||||
if k.species == super::Species::Sniffer && super::sniffer::digging(self) {
|
||||
@@ -529,13 +558,15 @@ impl Mob {
|
||||
}
|
||||
}
|
||||
/// `Entity.nextStep`: the next whole metre walked; a warden's steps come
|
||||
/// every 0.55, a strider's every 0.6.
|
||||
/// every 0.55, a strider's every 0.6, a turtle's every 0.15.
|
||||
fn next_step(&self) -> f32 {
|
||||
let walked = self.ai.steps.move_dist;
|
||||
if self.kind.species == super::Species::Warden {
|
||||
walked + 0.55
|
||||
} else if self.kind.species == super::Species::Strider {
|
||||
walked + 0.6
|
||||
} else if self.kind.species == super::Species::Turtle {
|
||||
walked + 0.15
|
||||
} else {
|
||||
(walked as i32).wrapping_add(1) as f32
|
||||
}
|
||||
@@ -813,14 +844,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);
|
||||
}
|
||||
@@ -908,7 +949,7 @@ impl Mob {
|
||||
(distance * 4.0).min(1.0)
|
||||
};
|
||||
self.limb_speed += (target - self.limb_speed) * 0.4;
|
||||
self.limb_position += self.limb_speed * if self.is_baby() { 3.0 } else { 1.0 };
|
||||
self.limb_position += self.limb_speed;
|
||||
} else {
|
||||
self.limb_speed = 0.0;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ use super::{
|
||||
},
|
||||
};
|
||||
use horizon_core::geometry::{Vec3, trig::Mth};
|
||||
use horizon_core::random::RandomSource;
|
||||
use horizon_core::random::{LegacyRandom, RandomSource};
|
||||
use std::f32::consts::PI;
|
||||
|
||||
/// `PiglinAi.ADMIRE_DURATION`.
|
||||
@@ -20,6 +20,9 @@ pub const ADMIRE: i32 = 119;
|
||||
pub const ATE_RECENTLY: i32 = 200;
|
||||
/// `AbstractPiglin.CONVERSION_TIME` (and the hoglin's).
|
||||
const CONVERSION: i32 = 300;
|
||||
/// `PiglinAi.CELEBRATION_TIME`: how long the `CELEBRATE_LOCATION` and
|
||||
/// `DANCING` memories last.
|
||||
const CELEBRATION: i32 = 300;
|
||||
|
||||
fn hostile_to_player(m: &Mob, w: &MobWorld) -> bool {
|
||||
if m.is_baby() {
|
||||
@@ -50,6 +53,7 @@ pub fn register_piglin(m: &mut Mob) {
|
||||
g.add(3, MeleeAttack::new(1.0, true));
|
||||
g.add(4, PiglinAvoidRepellent);
|
||||
g.add(4, GoToWantedItem);
|
||||
g.add(4, Celebrate);
|
||||
g.add(5, Stroll::avoiding_water(0.6));
|
||||
g.add(6, LookAtPlayer::new(8.0));
|
||||
g.add(7, RandomLookAround::default());
|
||||
@@ -243,6 +247,45 @@ impl Goal for Admire {
|
||||
m.nav_stop();
|
||||
}
|
||||
}
|
||||
/// The celebrate activity: `GoToTargetLocation` on the spot a killed target
|
||||
/// fell (within 2 blocks, or 4 and slower while dancing).
|
||||
#[derive(Clone, Debug)]
|
||||
struct Celebrate;
|
||||
impl Celebrate {
|
||||
fn walk(m: &mut Mob, cx: &mut Cx) {
|
||||
let Some(at) = m.extra.home else { return };
|
||||
let (close, speed) = if m.extra.scratch[0] > 0 {
|
||||
(4.0, 0.6)
|
||||
} else {
|
||||
(2.0, 1.0)
|
||||
};
|
||||
let center = Vec3::new(
|
||||
f64::from(at[0]) + 0.5,
|
||||
f64::from(at[1]),
|
||||
f64::from(at[2]) + 0.5,
|
||||
);
|
||||
if m.distance_sqr(center) >= close * close {
|
||||
m.nav_move_to(cx.w, [center.x, center.y, center.z], speed);
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Goal for Celebrate {
|
||||
goal_clone!();
|
||||
fn flags(&self) -> u8 {
|
||||
MOVE
|
||||
}
|
||||
fn can_use(&mut self, m: &mut Mob, _: &mut Cx) -> bool {
|
||||
m.extra.scratch[1] > 0 && m.extra.home.is_some() && m.extra.using_item == 0
|
||||
}
|
||||
fn start(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
Self::walk(m, cx);
|
||||
}
|
||||
fn tick(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
if m.ai.nav.is_done() {
|
||||
Self::walk(m, cx);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `GoToWantedItem`: walk to the gold the game spotted.
|
||||
#[derive(Clone, Debug)]
|
||||
struct GoToWantedItem;
|
||||
@@ -294,6 +337,54 @@ fn hoglin_repellent(m: &mut Mob, w: &MobWorld) {
|
||||
m.ai.target = None;
|
||||
}
|
||||
}
|
||||
/// `StartCelebratingIfTargetDead` for a piglin (`scratch[0]` dancing,
|
||||
/// `scratch[1]` ticks of celebration left, `scratch[2]` the hit seen, `home`
|
||||
/// the `CELEBRATE_LOCATION`): a killed target starts a celebration at its
|
||||
/// spot, and a hoglin's death makes the piglin dance one time in ten.
|
||||
/// `PiglinAi.wasHurtBy` ends both. Runs before the target goals drop the
|
||||
/// dead target.
|
||||
pub fn celebrate(m: &mut Mob, w: &MobWorld) {
|
||||
if m.extra.scratch[1] > 0 {
|
||||
m.extra.scratch[1] -= 1;
|
||||
if m.extra.scratch[1] == 0 {
|
||||
m.extra.scratch[0] = 0;
|
||||
m.extra.home = None;
|
||||
}
|
||||
}
|
||||
if let Some(by) = m.ai.last_hurt_by
|
||||
&& m.ai.last_hurt_by_time != m.extra.scratch[2]
|
||||
{
|
||||
m.extra.scratch[2] = m.ai.last_hurt_by_time;
|
||||
// PiglinAi.wasHurtBy: another piglin's hit ends nothing.
|
||||
let piglin =
|
||||
matches!(by, Who::Mob(id) if w.seen(id).is_some_and(|s| s.species == Species::Piglin));
|
||||
if !piglin {
|
||||
m.extra.scratch[0] = 0;
|
||||
m.extra.scratch[1] = 0;
|
||||
m.extra.home = None;
|
||||
}
|
||||
}
|
||||
if m.extra.scratch[1] > 0 {
|
||||
return;
|
||||
}
|
||||
let Some(target) = m.ai.target else { return };
|
||||
let Some(t) = w.target_info(target).filter(|t| !t.alive) else {
|
||||
return;
|
||||
};
|
||||
let hoglin =
|
||||
matches!(target, Who::Mob(id) if w.seen(id).is_some_and(|s| s.species == Species::Hoglin));
|
||||
// PiglinAi.wantsToDance: a fresh random seeded with the game time, not
|
||||
// the piglin's own.
|
||||
if hoglin && LegacyRandom::new(w.game_time).next_float() < 0.1 {
|
||||
m.extra.scratch[0] = 1;
|
||||
}
|
||||
m.extra.scratch[1] = CELEBRATION;
|
||||
m.extra.home = Some([
|
||||
t.position.x.floor() as i32,
|
||||
t.position.y.floor() as i32,
|
||||
t.position.z.floor() as i32,
|
||||
]);
|
||||
}
|
||||
/// Per-tick piglin and hoglin state: admiring, anger, zombification and the
|
||||
/// hoglin attack animation. Returns a conversion when it happens.
|
||||
pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
|
||||
@@ -420,7 +511,10 @@ pub fn ears(p: &mut Poses, s: &State) {
|
||||
/// Piglins' and brutes' ears flap (`AbstractPiglinModel`), arms follow the
|
||||
/// arm pose (`PiglinModel`).
|
||||
pub fn piglin_anim(m: &Mob, s: &State, p: &mut Poses) {
|
||||
let pose = if m.extra.using_item > 0 {
|
||||
// Piglin.getArmPose: dancing comes first.
|
||||
let pose = if m.extra.scratch[0] > 0 {
|
||||
PiglinPose::Dancing
|
||||
} else if m.extra.using_item > 0 {
|
||||
PiglinPose::AdmiringItem
|
||||
} else if m.extra.flag {
|
||||
PiglinPose::CrossbowCharge
|
||||
@@ -431,7 +525,14 @@ pub fn piglin_anim(m: &Mob, s: &State, p: &mut Poses) {
|
||||
} else {
|
||||
PiglinPose::Default
|
||||
};
|
||||
piglin_pose(p, s, pose, 25.0, m.extra.using_item as f32);
|
||||
piglin_pose(
|
||||
p,
|
||||
s,
|
||||
pose,
|
||||
25.0,
|
||||
m.extra.using_item as f32,
|
||||
m.main_arm_left(),
|
||||
);
|
||||
}
|
||||
/// `PiglinArmPose`.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
@@ -443,11 +544,19 @@ pub enum PiglinPose {
|
||||
CrossbowCharge,
|
||||
AdmiringItem,
|
||||
}
|
||||
/// `PiglinModel.setupAnim` for a piglin facing right-handed.
|
||||
pub fn piglin_pose(p: &mut Poses, s: &State, pose: PiglinPose, max_charge: f32, ticks: f32) {
|
||||
use super::anim::{ArmPose, Hum, humanoid};
|
||||
/// `PiglinModel.setupAnim`; `left` when the piglin's main arm is the left one.
|
||||
pub fn piglin_pose(
|
||||
p: &mut Poses,
|
||||
s: &State,
|
||||
pose: PiglinPose,
|
||||
max_charge: f32,
|
||||
ticks: f32,
|
||||
left: bool,
|
||||
) {
|
||||
use super::anim::{ArmPose, Hum, crossbow_charge, crossbow_hold, humanoid};
|
||||
let mut h = Hum::new(ArmPose::Empty, ArmPose::Empty);
|
||||
h.weapon_swing = pose == PiglinPose::MeleeWeapon;
|
||||
h.left_main = left;
|
||||
h.charge = (max_charge, ticks);
|
||||
humanoid(p, s, &h);
|
||||
ears(p, s);
|
||||
@@ -474,27 +583,22 @@ pub fn piglin_pose(p: &mut Poses, s: &State, pose: PiglinPose, max_charge: f32,
|
||||
p.get("left_arm")[1] += (dance * 40.0).msin() * 0.5 + 0.5;
|
||||
p.get("body")[1] += (dance * 40.0).msin() * 0.35;
|
||||
}
|
||||
PiglinPose::MeleeWeapon if s.swing == 0.0 => p.xrot("right_arm", -1.8),
|
||||
PiglinPose::CrossbowHold => {
|
||||
p.yrot("right_arm", -0.3 + hy);
|
||||
p.yrot("left_arm", 0.6 + hy);
|
||||
p.xrot("right_arm", -PI / 2.0 + hx + 0.1);
|
||||
p.xrot("left_arm", -1.5 + hx);
|
||||
}
|
||||
PiglinPose::CrossbowCharge => {
|
||||
p.yrot("right_arm", -0.8);
|
||||
p.xrot("right_arm", -0.970_796_35);
|
||||
p.xrot("left_arm", -0.970_796_35);
|
||||
let alpha = ticks.clamp(0.0, max_charge) / max_charge;
|
||||
p.yrot("left_arm", 0.4 + alpha * (0.85 - 0.4));
|
||||
let x = -0.970_796_35 + alpha * (-PI / 2.0 - -0.970_796_35);
|
||||
p.xrot("left_arm", x);
|
||||
// holdWeaponHigh
|
||||
PiglinPose::MeleeWeapon if s.swing == 0.0 => {
|
||||
p.xrot(if left { "left_arm" } else { "right_arm" }, -1.8)
|
||||
}
|
||||
PiglinPose::CrossbowHold => crossbow_hold(p, hx, hy, !left),
|
||||
PiglinPose::CrossbowCharge => crossbow_charge(p, max_charge, ticks, !left),
|
||||
PiglinPose::AdmiringItem => {
|
||||
p.xrot("head", 0.5);
|
||||
p.yrot("head", 0.0);
|
||||
p.yrot("left_arm", 0.5);
|
||||
p.xrot("left_arm", -0.9);
|
||||
if left {
|
||||
p.yrot("right_arm", -0.5);
|
||||
p.xrot("right_arm", -0.9);
|
||||
} else {
|
||||
p.yrot("left_arm", 0.5);
|
||||
p.xrot("left_arm", -0.9);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
//! Polar bears (`PolarBear`): a neutral animal that defends its cubs,
|
||||
//! fights back when hurt, and rears up before each swing. A cub flees what
|
||||
//! hurts it and calls the adults.
|
||||
//!
|
||||
//! `extra.flag` is `DATA_STANDING_ID`, `extra.counter` the warning sound's
|
||||
//! delay, `extra.anger` the `NeutralMob` anger left and `extra.flag2` whether
|
||||
//! the player is who it is angry at. `anim.v[STAND]` is
|
||||
//! `clientSideStandAnimation`.
|
||||
use horizon_core::{geometry::Vec3, random::RandomSource};
|
||||
|
||||
use super::{
|
||||
DamageKind, Mob, MobWorld, Species, Who,
|
||||
goals::{
|
||||
Cx, Float, FollowParent, Goal, HurtByTarget, LookAtPlayer, MOVE, MeleeAttack,
|
||||
NearestTarget, Panic, RandomLookAround, Stroll, goal_clone,
|
||||
},
|
||||
world::TargetInfo,
|
||||
};
|
||||
|
||||
/// `anim.v` slot of `clientSideStandAnimation`.
|
||||
pub const STAND: usize = 0;
|
||||
/// `STAND_ANIMATION_TICKS`.
|
||||
const STAND_TICKS: f32 = 6.0;
|
||||
|
||||
pub fn register(m: &mut Mob) {
|
||||
let g = &mut m.ai.goals;
|
||||
g.add(0, Float);
|
||||
g.add(1, MeleeAttack::new(1.25, true).polar_bear());
|
||||
g.add(1, PolarBearPanic(Panic::new(2.0)));
|
||||
g.add(4, FollowParent::new(1.25));
|
||||
g.add(5, Stroll::new(1.0));
|
||||
g.add(6, LookAtPlayer::new(6.0));
|
||||
g.add(7, RandomLookAround::default());
|
||||
let t = &mut m.ai.targets;
|
||||
t.add(1, HurtByTarget::new().polar_bear());
|
||||
// PolarBearAttackPlayersGoal: players near a cub, within half the
|
||||
// follow range, never for a cub itself.
|
||||
t.add(
|
||||
2,
|
||||
NearestTarget::player(true)
|
||||
.interval(20)
|
||||
.follow_scale(0.5)
|
||||
.reach()
|
||||
.gate(|m, _| !m.is_baby())
|
||||
.when(cub_nearby)
|
||||
.named("PolarBearAttackPlayersGoal"),
|
||||
);
|
||||
t.add(
|
||||
3,
|
||||
NearestTarget::player(true).when(|m, _| m.extra.anger > 0 && m.extra.flag2),
|
||||
);
|
||||
t.add(
|
||||
4,
|
||||
NearestTarget::species(&[Species::Fox], true)
|
||||
.interval(10)
|
||||
.reach()
|
||||
.when(|m, _| !m.is_baby()),
|
||||
);
|
||||
}
|
||||
|
||||
/// `PolarBearAttackPlayersGoal.canUse`: a polar bear cub within 8 blocks
|
||||
/// (4 up and down).
|
||||
fn cub_nearby(m: &Mob, w: &MobWorld) -> bool {
|
||||
let area = m.bounds().inflate(Vec3::new(8.0, 4.0, 8.0));
|
||||
w.others_near(area)
|
||||
.any(|s| s.species == Species::PolarBear && s.baby && s.bounds.intersects(area))
|
||||
}
|
||||
|
||||
/// `PanicGoal` with a function of the bear's age for the damage tag: a cub
|
||||
/// panics at `PANIC_CAUSES`, an adult only at `PANIC_ENVIRONMENTAL_CAUSES`.
|
||||
#[derive(Clone, Debug)]
|
||||
struct PolarBearPanic(Panic);
|
||||
impl Goal for PolarBearPanic {
|
||||
goal_clone!();
|
||||
fn flags(&self) -> u8 {
|
||||
MOVE
|
||||
}
|
||||
fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
|
||||
(m.is_baby() || environmental_panic(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)
|
||||
}
|
||||
}
|
||||
/// `DamageTypeTags.PANIC_ENVIRONMENTAL_CAUSES` (fire, lava, cactus), or
|
||||
/// burning.
|
||||
fn environmental_panic(m: &Mob) -> bool {
|
||||
m.fire_ticks > 0 && !m.kind.flags.fire_immune
|
||||
|| m.last_damage.is_some_and(|(d, t)| {
|
||||
matches!(
|
||||
d.kind,
|
||||
DamageKind::OnFire | DamageKind::InFire | DamageKind::Lava | DamageKind::Cactus
|
||||
) && i64::from(m.age) - t <= 40
|
||||
})
|
||||
}
|
||||
|
||||
/// `PolarBear.setStanding`.
|
||||
pub fn set_standing(m: &mut Mob, standing: bool) {
|
||||
m.extra.flag = standing;
|
||||
}
|
||||
|
||||
/// `PolarBear.playWarningSound`.
|
||||
fn play_warning_sound(m: &mut Mob, cx: &mut Cx) {
|
||||
if m.extra.counter <= 0 {
|
||||
m.voice("entity.polar_bear.warning", cx.events);
|
||||
m.extra.counter = 40;
|
||||
}
|
||||
}
|
||||
|
||||
/// `PolarBearMeleeAttackGoal.checkAndPerformAttack`: strike when ready and
|
||||
/// in reach, rear up and growl in the last 10 ticks while close.
|
||||
pub fn check_and_perform_attack(
|
||||
until_attack: &mut i32,
|
||||
cooldown: i32,
|
||||
m: &mut Mob,
|
||||
who: Who,
|
||||
t: &TargetInfo,
|
||||
cx: &mut Cx,
|
||||
) {
|
||||
if *until_attack <= 0 && m.within_melee_range(t) && m.sees(cx.w, who) {
|
||||
*until_attack = cooldown;
|
||||
super::ai::do_hurt_target(m, who, cx);
|
||||
set_standing(m, false);
|
||||
return;
|
||||
}
|
||||
let reach = t.bounds.size().x as f32 + 3.0;
|
||||
if m.distance_sqr(t.position) < f64::from(reach * reach) {
|
||||
if *until_attack <= 0 {
|
||||
set_standing(m, false);
|
||||
*until_attack = cooldown;
|
||||
}
|
||||
if *until_attack <= 10 {
|
||||
set_standing(m, true);
|
||||
play_warning_sound(m, cx);
|
||||
}
|
||||
} else {
|
||||
*until_attack = cooldown;
|
||||
set_standing(m, false);
|
||||
}
|
||||
}
|
||||
|
||||
/// `PolarBear.tick`'s client half: `clientSideStandAnimation` follows the
|
||||
/// synched standing flag by one tick a tick. A client of a server runs it
|
||||
/// too.
|
||||
pub fn client_tick(m: &mut Mob) {
|
||||
let v = m.anim.v[STAND];
|
||||
m.anim.vo[STAND] = v;
|
||||
let step = if m.extra.flag { 1.0 } else { -1.0 };
|
||||
m.anim.v[STAND] = (v + step).clamp(0.0, STAND_TICKS);
|
||||
}
|
||||
/// `PolarBear.getStandingAnimationScale`.
|
||||
pub fn standing_scale(m: &Mob, partial: f32) -> f32 {
|
||||
m.anim.lerp(STAND, partial) / STAND_TICKS
|
||||
}
|
||||
|
||||
/// `PolarBear.tick`: the stand animation, the warning sound's delay and
|
||||
/// `NeutralMob.updatePersistentAnger`.
|
||||
pub fn ai_step(m: &mut Mob, w: &MobWorld) {
|
||||
client_tick(m);
|
||||
if m.extra.counter > 0 {
|
||||
m.extra.counter -= 1;
|
||||
}
|
||||
if m.ai.target.is_some() {
|
||||
// Anger follows the target and starts over every tick it is held:
|
||||
// 20 to 39 seconds (`PERSISTENT_ANGER_TIME`).
|
||||
m.extra.flag2 = m.ai.target == Some(Who::Player);
|
||||
m.extra.anger = 400 + m.random.next_int_bound(381).unwrap_or(0);
|
||||
} else if m.extra.anger > 0 {
|
||||
m.extra.anger -= 1;
|
||||
if m.extra.anger == 0 {
|
||||
stop_being_angry(m);
|
||||
}
|
||||
}
|
||||
// A player in creative or spectator mode is forgiven at once.
|
||||
if m.extra.flag2 && w.player.as_ref().is_some_and(|p| p.creative || p.spectator) {
|
||||
stop_being_angry(m);
|
||||
}
|
||||
}
|
||||
/// `NeutralMob.stopBeingAngry`.
|
||||
fn stop_being_angry(m: &mut Mob) {
|
||||
m.ai.last_hurt_by = None;
|
||||
m.ai.target = None;
|
||||
m.extra.anger = 0;
|
||||
m.extra.flag2 = false;
|
||||
}
|
||||
@@ -177,8 +177,71 @@ pub fn body(m: &Mob) -> (String, String) {
|
||||
"frog#main".into(),
|
||||
format!("entity/frog/frog_{}", climate(m)),
|
||||
),
|
||||
S::Rabbit => (
|
||||
baby_or(m, "rabbit#main", "rabbit_baby#main"),
|
||||
// RabbitRenderer.getTextureLocation: the Toast name tag, else the
|
||||
// variant (the killer bunny's id is 99).
|
||||
S::Rabbit => {
|
||||
let name = if m.custom_name.as_deref() == Some("Toast") {
|
||||
"toast"
|
||||
} else {
|
||||
match m.extra.variant {
|
||||
1 => "white",
|
||||
2 => "black",
|
||||
3 => "white_splotched",
|
||||
4 => "gold",
|
||||
5 => "salt",
|
||||
99 => "caerbannog",
|
||||
_ => "brown",
|
||||
}
|
||||
};
|
||||
(
|
||||
baby_or(m, "rabbit#main", "rabbit_baby#main"),
|
||||
b(&format!("entity/rabbit/rabbit_{name}")),
|
||||
)
|
||||
}
|
||||
// ParrotRenderer.getVariantTexture.
|
||||
S::Parrot => {
|
||||
let name = ["red_blue", "blue", "green", "yellow_blue", "grey"]
|
||||
[usize::from(m.extra.variant).min(4)];
|
||||
(m.kind.model_key(), format!("entity/parrot/parrot_{name}"))
|
||||
}
|
||||
// AxolotlRenderer.getTextureLocation.
|
||||
S::Axolotl => {
|
||||
let name =
|
||||
["lucy", "wild", "gold", "cyan", "blue"][usize::from(m.extra.variant).min(4)];
|
||||
(
|
||||
baby_or(m, "axolotl#main", "axolotl_baby#main"),
|
||||
b(&format!("entity/axolotl/axolotl_{name}")),
|
||||
)
|
||||
}
|
||||
// SalmonRenderer.submit: the model of the salmon's size.
|
||||
S::Salmon => {
|
||||
let model = match m.extra.variant {
|
||||
0 => "salmon_small#main",
|
||||
2 => "salmon_large#main",
|
||||
_ => "salmon#main",
|
||||
};
|
||||
(model.into(), "entity/fish/salmon".into())
|
||||
}
|
||||
// The baby models and textures of AgeableMobRenderer's other
|
||||
// subclasses.
|
||||
S::Goat => (
|
||||
baby_or(m, "goat#main", "goat_baby#main"),
|
||||
b(&m.kind.texture),
|
||||
),
|
||||
S::PolarBear => (
|
||||
baby_or(m, "polar_bear#main", "polar_bear_baby#main"),
|
||||
b(&m.kind.texture),
|
||||
),
|
||||
S::Ocelot => (
|
||||
baby_or(m, "ocelot#main", "ocelot_baby#main"),
|
||||
b(&m.kind.texture),
|
||||
),
|
||||
S::Turtle => (
|
||||
baby_or(m, "turtle#main", "turtle_baby#main"),
|
||||
b(&m.kind.texture),
|
||||
),
|
||||
S::Armadillo => (
|
||||
baby_or(m, "armadillo#main", "armadillo_baby#main"),
|
||||
b(&m.kind.texture),
|
||||
),
|
||||
S::Pig => {
|
||||
@@ -241,7 +304,7 @@ pub fn body(m: &Mob) -> (String, String) {
|
||||
),
|
||||
S::ZombieVillager => (
|
||||
baby_or(m, "zombie_villager#main", "zombie_villager_baby#main"),
|
||||
"entity/zombie_villager/zombie_villager".into(),
|
||||
b("entity/zombie_villager/zombie_villager"),
|
||||
),
|
||||
S::Villager => (
|
||||
baby_or(m, "villager#main", "villager_baby#main"),
|
||||
@@ -757,7 +820,14 @@ pub fn base_transform_at(m: &Mob, alpha: f32, origin: glam::Mat4) -> glam::Mat4
|
||||
let rotation = match sleep {
|
||||
// setupRotations for Pose.SLEEPING, with the head moved toward the pillow.
|
||||
Some((angle, step)) => {
|
||||
let head = (m.eye_y() - m.position.y) as f32 - 0.1;
|
||||
// The eye height of the standing pose (LivingEntityRenderer
|
||||
// extracts `getEyeHeight(Pose.STANDING)` for a bed).
|
||||
let eye = if avatar(m) {
|
||||
m.kind.eye_height as f32
|
||||
} else {
|
||||
(m.eye_y() - m.position.y) as f32
|
||||
};
|
||||
let head = eye - 0.1;
|
||||
glam::Mat4::from_translation(glam::Vec3::new(-step[0] * head, 0.0, -step[1] * head))
|
||||
* glam::Mat4::from_rotation_y(angle.to_radians())
|
||||
* glam::Mat4::from_rotation_z(90.0_f32.to_radians())
|
||||
@@ -825,10 +895,15 @@ pub fn base_transform_at(m: &Mob, alpha: f32, origin: glam::Mat4) -> glam::Mat4
|
||||
base *= species_scale(m, alpha);
|
||||
base * glam::Mat4::from_translation(glam::Vec3::new(0.0, -1.501, 0.0))
|
||||
}
|
||||
/// `LivingEntityRenderer.isShaking`: converting zombies, piglins and
|
||||
/// hoglins, and a zombie villager being cured.
|
||||
/// `LivingEntityRenderer.isShaking`: a fully frozen mob (the skeleton's own
|
||||
/// `isShaking` is its stray conversion only), converting zombies, piglins
|
||||
/// and hoglins, a zombie villager being cured and a cold strider.
|
||||
fn shaking(m: &Mob) -> bool {
|
||||
if m.is_fully_frozen() && m.species() != Species::Skeleton {
|
||||
return true;
|
||||
}
|
||||
match m.species() {
|
||||
Species::Strider => super::strider::suffocating(m),
|
||||
Species::ZombieVillager => super::zombie_villager::is_converting(m),
|
||||
Species::Zombie | Species::Husk => m.extra.counter2 > 0,
|
||||
Species::Piglin | Species::PiglinBrute | Species::Hoglin => {
|
||||
@@ -1267,6 +1342,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 {
|
||||
@@ -1453,4 +1537,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,31 +229,102 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
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.hurtServer` before the damage: arrows bounce off a closed shell.
|
||||
pub fn accepts(m: &Mob, source: &Damage) -> bool {
|
||||
!(m.extra.counter == 0 && source.kind == DamageKind::Arrow)
|
||||
@@ -319,7 +492,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
|
||||
@@ -362,24 +535,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.
|
||||
|
||||
@@ -1500,7 +1500,7 @@ impl Def {
|
||||
),
|
||||
// Nautilus.handleAirSupply: it dries out on land.
|
||||
needs_water: aquatic || s == S::Nautilus,
|
||||
water_sensitive: matches!(s, S::Enderman | S::Blaze | S::SnowGolem),
|
||||
water_sensitive: matches!(s, S::Enderman | S::Blaze | S::SnowGolem | S::Strider),
|
||||
animal: !matches!(s, S::Bat | S::SulfurCube | S::Mannequin)
|
||||
&& !self.hostile_class()
|
||||
&& !aquatic
|
||||
@@ -1629,6 +1629,7 @@ impl Def {
|
||||
match self.species {
|
||||
Species::Dolphin | Species::Axolotl => 1.0,
|
||||
Species::Slime | Species::MagmaCube | Species::SulfurCube => 0.0,
|
||||
Species::Guardian | Species::ElderGuardian | Species::Shulker => 180.0,
|
||||
_ => 40.0,
|
||||
}
|
||||
}
|
||||
@@ -1640,6 +1641,9 @@ impl Def {
|
||||
Species::Sniffer => 50.0,
|
||||
Species::Goat => 15.0,
|
||||
Species::Armadillo => 32.0,
|
||||
Species::Ravager => 45.0,
|
||||
Species::Camel | Species::CamelHusk => 30.0,
|
||||
Species::Shulker => 180.0,
|
||||
_ => 75.0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -297,6 +297,7 @@ pub fn accepts_damage(m: &mut Mob, source: &Damage, amount: f32) -> bool {
|
||||
| DamageKind::Fall
|
||||
| DamageKind::Sting
|
||||
| DamageKind::Cactus
|
||||
| DamageKind::Freeze
|
||||
);
|
||||
if immune {
|
||||
if let Some(from) = source.from {
|
||||
|
||||
@@ -360,7 +360,7 @@ impl Goal for FollowOwner {
|
||||
};
|
||||
let far = m.distance_sqr(at) >= 144.0;
|
||||
if !far {
|
||||
let max = max_head_x_rot(m);
|
||||
let max = m.max_head_x_rot();
|
||||
m.look_at_speed([at.x, eye, at.z], 10.0, max);
|
||||
}
|
||||
self.recalc -= 1;
|
||||
@@ -507,7 +507,7 @@ impl Goal for Beg {
|
||||
}
|
||||
fn tick(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
if let Some(p) = &cx.w.player {
|
||||
let max = max_head_x_rot(m);
|
||||
let max = m.max_head_x_rot();
|
||||
m.look_at_speed([p.position.x, p.eye[1], p.position.z], 10.0, max);
|
||||
}
|
||||
self.ticks -= 1;
|
||||
@@ -529,12 +529,7 @@ pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
|
||||
let v = &mut m.anim.v;
|
||||
v[INTEREST] += (goal - v[INTEREST]) * 0.4;
|
||||
// isWet (extra.flag2), isShaking (counter2 > 0).
|
||||
let rain = w.raining
|
||||
&& w.can_see_sky(
|
||||
m.position.x.floor() as i32,
|
||||
(m.position.y + f64::from(m.height())).floor() as i32,
|
||||
m.position.z.floor() as i32,
|
||||
);
|
||||
let rain = super::ai::in_rain(m, w);
|
||||
let shaking = m.extra.counter2 > 0;
|
||||
if m.fluids.in_water || rain {
|
||||
m.extra.flag2 = true;
|
||||
@@ -683,14 +678,6 @@ pub fn texture(m: &Mob) -> String {
|
||||
let baby = if m.is_baby() { "_baby" } else { "" };
|
||||
format!("entity/wolf/{file}{state}{baby}")
|
||||
}
|
||||
/// `Wolf.getMaxHeadXRot`: 20 degrees while sitting.
|
||||
fn max_head_x_rot(m: &Mob) -> f32 {
|
||||
if m.extra.sitting {
|
||||
20.0
|
||||
} else {
|
||||
m.kind.max_head_x_rot
|
||||
}
|
||||
}
|
||||
|
||||
/// Cat variants (`CatVariants`): registry name, texture file.
|
||||
pub const CAT_VARIANTS: [&str; 11] = [
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,8 +232,10 @@ pub fn drowned_travel(
|
||||
m.velocity = Vec3::new(m.velocity.x * 0.9, m.velocity.y * 0.9, m.velocity.z * 0.9);
|
||||
true
|
||||
}
|
||||
/// The trident throw of `Drowned.performRangedAttack`, until thrown tridents
|
||||
/// exist: an arrow-like shot with the trident's damage.
|
||||
/// The trident throw of `Drowned.performRangedAttack`: a `ThrownTrident` of
|
||||
/// the held trident (a plain one when it holds none), owned by the drowned,
|
||||
/// which cannot be picked up and does not return (`AbstractArrow` constructor:
|
||||
/// only a player's projectile is `Pickup.ALLOWED`).
|
||||
fn drowned_throw(m: &mut Mob, who: Who, _power: f32, cx: &mut Cx) {
|
||||
let Some(t) = cx.w.target_info(who) else {
|
||||
return;
|
||||
@@ -243,15 +245,18 @@ fn drowned_throw(m: &mut Mob, who: Who, _power: f32, cx: &mut Cx) {
|
||||
let yd = t.position.y + (t.bounds.max_position().y - t.bounds.min_position().y) / 3.0 - start.y;
|
||||
let zd = t.position.z - m.position.z;
|
||||
let dist = (xd * xd + zd * zd).sqrt();
|
||||
let mut p = Projectile::new(
|
||||
ProjectileKind::Arrow {
|
||||
base_damage: 8.0,
|
||||
critical: false,
|
||||
effect: None,
|
||||
},
|
||||
start,
|
||||
Some(Who::Mob(m.id)),
|
||||
);
|
||||
let mut trident = crate::items::ItemStack::new("minecraft:trident", 1);
|
||||
if m.extra.main_hand.as_deref() == Some("minecraft:trident")
|
||||
&& let Some(g) = super::gear::gear(m)
|
||||
{
|
||||
for (e, l) in &g.slots[super::gear::MAIN] {
|
||||
trident.enchant(e, *l);
|
||||
}
|
||||
}
|
||||
let mut p = Projectile::new(ProjectileKind::Trident, start, Some(Who::Mob(m.id)));
|
||||
if let Some(a) = p.arrow.as_mut() {
|
||||
a.set_item(&trident);
|
||||
}
|
||||
p.shoot(
|
||||
&mut crate::projectiles::own_random(m),
|
||||
[xd, yd + dist * f64::from(0.2_f32), zd],
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -96,7 +96,7 @@ pub fn make_invulnerable(m: &mut Mob) {
|
||||
pub fn powered(m: &Mob) -> bool {
|
||||
m.health <= m.attrs.max_health / 2.0
|
||||
}
|
||||
fn head_pos(m: &Mob, i: usize) -> [f64; 3] {
|
||||
pub fn head_pos(m: &Mob, i: usize) -> [f64; 3] {
|
||||
if i == 0 {
|
||||
return [m.position.x, m.position.y + 3.0, m.position.z];
|
||||
}
|
||||
|
||||
@@ -206,6 +206,25 @@ impl MobWorld<'_> {
|
||||
}
|
||||
self.world.light(x, y, z) >> 4 == 15
|
||||
}
|
||||
/// Level.precipitationAt: raining, open to the sky, nothing motion
|
||||
/// blocking above, then the biome's precipitation at the position:
|
||||
/// `Some(true)` rain, `Some(false)` snow, `None` nothing.
|
||||
pub fn precipitation_at(&self, x: i32, y: i32, z: i32) -> Option<bool> {
|
||||
if !self.raining || !self.can_see_sky(x, y, z) || self.motion_blocking_height(x, z) > y {
|
||||
return None;
|
||||
}
|
||||
let chunk = self.world.chunks.get(&(x >> 4, z >> 4))?;
|
||||
let biome = self.biomes.get(chunk.biome(x, y, z));
|
||||
if !biome.precipitation {
|
||||
return None;
|
||||
}
|
||||
Some(biome.temperature_at(BlockPos::new(x, y, z), self.sea_level) >= 0.15)
|
||||
}
|
||||
/// Level.isRainingAt: the precipitation at the position is rain, not snow
|
||||
/// or none.
|
||||
pub fn raining_at(&self, x: i32, y: i32, z: i32) -> bool {
|
||||
self.precipitation_at(x, y, z) == Some(true)
|
||||
}
|
||||
/// Level.getLightLevelDependentMagicValue: the dimension's brightness curve
|
||||
/// at the raw (sky darkened) light level.
|
||||
pub fn brightness(&self, x: i32, y: i32, z: i32) -> f32 {
|
||||
|
||||
Reference in New Issue
Block a user