diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index 51b203f12..c98de4421 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -246,6 +246,14 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag { } map.insert("equipment".into(), Tag::Compound(equipment)); } + "minecraft:camel" => { + // LastPoseTick: negative while sitting. + let sitting = crate::mobs::camel::sitting(m); + map.insert( + "LastPoseTick".into(), + Tag::Long(if sitting { -1 } else { 0 }), + ); + } "minecraft:wither" => { map.insert("Invul".into(), Tag::Int(m.extra.counter)); } @@ -416,6 +424,13 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap>, id: u32) - .unwrap_or(0) as u8; } } + if m.species() == crate::mobs::Species::Camel { + let sitting = matches!(tag.get("LastPoseTick"), Some(Tag::Long(v)) if *v < 0); + crate::mobs::camel::stand_up_instantly(&mut m); + if sitting { + m.extra.egg_time = -1 - 400; + } + } if m.species() == crate::mobs::Species::Wither { m.extra.counter = number(tag.get("Invul")).unwrap_or(0) as i32; } @@ -459,6 +474,10 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap>, id: u32) - if m.health <= 0.0 && m.death_time == 0 { return None; } + // DeathTime only counts while dead. + if m.health > 0.0 { + m.death_time = 0; + } Some(m) } diff --git a/crates/horizon-client/src/game/mounts.rs b/crates/horizon-client/src/game/mounts.rs index 649c97728..3865dfc82 100644 --- a/crates/horizon-client/src/game/mounts.rs +++ b/crates/horizon-client/src/game/mounts.rs @@ -151,7 +151,13 @@ impl Game<'_> { } /// `AbstractHorse.handleEating`. fn feed_mount(&mut self, index: usize, item: &str) -> bool { + let camel = self.mobs[index].species() == Species::Camel; + if camel != (item == "minecraft:cactus") { + return false; + } let (heal, age_up, temper, love) = match item { + // Camel.handleEating. + "minecraft:cactus" => (2.0, 10, 0, true), "minecraft:wheat" => (2.0, 20, 3, false), "minecraft:sugar" => (1.0, 30, 3, false), "minecraft:hay_block" => (20.0, 180, 0, false), @@ -191,6 +197,7 @@ impl Game<'_> { m.anim.v[2] = 1.0; let at = m.center(); let sound = match m.species() { + Species::Camel => "minecraft:entity.camel.eat", Species::Donkey => "minecraft:entity.donkey.eat", Species::Mule => "minecraft:entity.mule.eat", _ => "minecraft:entity.horse.eat", @@ -272,6 +279,7 @@ impl Game<'_> { pitch: self.pitch, jump: if jump > 0.0 { jump } else { pending }, steer, + sprint: self.keys.contains(&winit::keyboard::KeyCode::ControlLeft), }); true } @@ -281,7 +289,23 @@ impl Game<'_> { return; }; let m = &self.mobs[i]; - let at = Vec3::new(m.position.x, m.position.y + seat(m.species()), m.position.z); + let mut at = Vec3::new(m.position.x, m.position.y + seat(m.species()), m.position.z); + if m.species() == Species::Camel { + // Camel.getPassengerAttachmentPoint: the driver half a block + // forward, at the body anchor (lower while sitting). + let scale = if m.is_baby() { 0.6 } else { 1.0 }; + let anchor = if horizon_client::mobs::camel::sitting(m) { + (2.375 - 1.43) * scale - if m.is_baby() { 0.09375 } else { 0.375 } + 0.2 * scale + } else { + 2.375 * scale - if m.is_baby() { 0.09375 } else { 0.375 } + }; + let yaw = m.yaw.to_radians(); + at = Vec3::new( + m.position.x - f64::from(0.5 * scale * yaw.sin()), + m.position.y + f64::from(anchor) - 0.6, + m.position.z + f64::from(0.5 * scale * yaw.cos()), + ); + } if let Some(p) = self.player.as_mut() { self.previous_position = p.position; p.position = at; diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 4f3ebe8c9..0e6be5a20 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -8,6 +8,7 @@ pub mod ai; pub mod anim; pub mod aquatic; +pub mod camel; pub mod control; pub mod cube; pub mod data; diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index cc461f4fd..581e568d7 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -119,7 +119,9 @@ pub fn family(s: Species) -> Family { S::Ravager => Family::Ravager, S::Wither => Family::Wither, S::Piglin | S::PiglinBrute | S::Hoglin | S::Zoglin => Family::Piglin, - S::Horse | S::Donkey | S::Mule | S::SkeletonHorse | S::ZombieHorse => Family::Equine, + S::Horse | S::Donkey | S::Mule | S::SkeletonHorse | S::ZombieHorse | S::Camel => { + Family::Equine + } S::Phantom => Family::Phantom, S::Enderman => Family::Enderman, S::Slime | S::MagmaCube => Family::Cube, @@ -547,7 +549,7 @@ fn species_ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { super::piglin::ai_step(m, w, events) } Species::Zoglin if m.extra.anim_ticks > 0 => m.extra.anim_ticks -= 1, - s if super::equine::is_equine(s) => super::equine::ai_step(m, w), + s if super::equine::is_equine(s) => super::equine::ai_step(m, w, events), Species::Phantom => super::phantom::flap_sound(m, events), Species::Guardian | Species::ElderGuardian => super::guardian::ai_step(m, w, events), Species::Vex => super::vex::ai_step(m, w), diff --git a/crates/horizon-client/src/mobs/anim.rs b/crates/horizon-client/src/mobs/anim.rs index 25c4d970f..6d635efe1 100644 --- a/crates/horizon-client/src/mobs/anim.rs +++ b/crates/horizon-client/src/mobs/anim.rs @@ -8,6 +8,7 @@ use std::f32::consts::PI; /// The setupAnim implementation a model uses. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Anim { + Camel, Wither, Vex, Ravager, @@ -74,7 +75,7 @@ pub struct AnimState { pub flap_speed_o: f32, pub flapping: f32, /// Generic per-species keyframe animation states. - pub states: [AnimationState; 4], + pub states: [AnimationState; 6], /// Per-species animation values (tail wag, squid tentacles, squish...) /// with their previous-tick copies for interpolation. pub v: [f32; 8], @@ -341,8 +342,8 @@ pub fn setup( p: &mut Poses, w_anims: &HashMap, ) { - let _ = w_anims; match m.kind.anim { + Anim::Camel => super::camel::anim(m, s, p, w_anims), Anim::Squid | Anim::Fish | Anim::Pufferfish | Anim::Dolphin => { super::aquatic::anim(m, s, p) } diff --git a/crates/horizon-client/src/mobs/camel.rs b/crates/horizon-client/src/mobs/camel.rs new file mode 100644 index 000000000..7d1c200b8 --- /dev/null +++ b/crates/horizon-client/src/mobs/camel.rs @@ -0,0 +1,229 @@ +//! Camels (`Camel`): ridden by two, sitting down and standing up now and +//! then, dashing instead of jumping, eating cactus, always "tame". +use super::{ + Mob, MobEvent, + anim::{Poses, State}, + goals::{ + Breed, Cx, Float, FollowParent, Goal, LookAtPlayer, MOVE, Panic, RandomLookAround, Stroll, + Tempt, goal_clone, + }, +}; +use horizon_core::random::RandomSource; +use std::collections::HashMap; + +// Camel state: `egg_time` the last pose change tick (negative while +// sitting), `anim_ticks` the dash cooldown, `flag2` dashing, `ints[0]` the +// idle animation timeout. AnimState states: sit, sit pose, stand up, idle, +// dash. +const SIT_DOWN: i64 = 40; +const STAND_UP: i64 = 52; +pub const DASH_COOLDOWN: i32 = 55; + +pub fn register(m: &mut Mob) { + m.extra.tame = true; + let tempt = m.kind.tempt.clone(); + let g = &mut m.ai.goals; + g.add(0, Float); + g.add(1, CamelPanic(Panic::new(4.0 * 0.25))); + g.add(2, Breed::new(1.0)); + g.add(3, Tempt::new(2.5 * 0.25, tempt, false)); + g.add(4, FollowParent::new(2.5 * 0.25)); + g.add(5, Stroll::new(2.0 * 0.25)); + g.add(5, RandomSitting); + g.add(6, LookAtPlayer::new(6.0)); + g.add(7, RandomLookAround::default()); + // finalizeSpawn: fully standing. + m.extra.egg_time = (m.age as i64 - STAND_UP - 1).max(0) as i32; +} +fn pose_time(m: &Mob) -> i64 { + i64::from(m.age) - i64::from(m.extra.egg_time).abs() +} +pub fn sitting(m: &Mob) -> bool { + m.extra.egg_time < 0 +} +fn in_transition(m: &Mob) -> bool { + pose_time(m) < if sitting(m) { SIT_DOWN } else { STAND_UP } +} +/// `Camel.refuseToMove`: sitting or getting up or down. +pub fn refuse_to_move(m: &Mob) -> bool { + sitting(m) || in_transition(m) +} +fn sit_down(m: &mut Mob, events: &mut Vec) { + if !sitting(m) { + sound(m, "entity.camel.sit", events); + m.extra.egg_time = -(m.age as i32).max(1); + } +} +pub fn stand_up(m: &mut Mob, events: &mut Vec) { + if sitting(m) { + sound(m, "entity.camel.stand", events); + m.extra.egg_time = m.age as i32; + } +} +/// `standUpInstantly`. +pub fn stand_up_instantly(m: &mut Mob) { + m.extra.egg_time = (i64::from(m.age) - STAND_UP - 1).max(0) as i32; +} +fn sound(m: &Mob, name: &'static str, events: &mut Vec) { + events.push(MobEvent::Sound( + name, + [m.position.x, m.position.y, m.position.z], + 1.0, + 1.0, + )); +} +/// `CamelAi.CamelPanic`: standing up at once to flee. +#[derive(Clone, Debug)] +struct CamelPanic(Panic); +impl Goal for CamelPanic { + goal_clone!(); + fn flags(&self) -> u8 { + self.0.flags() + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + m.rider.is_none() && 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) { + stand_up_instantly(m); + self.0.start(m, cx); + } + fn stop(&mut self, m: &mut Mob, cx: &mut Cx) { + self.0.stop(m, cx); + } +} +/// `CamelAi.RandomSitting`: after 20 seconds in a pose, sit or stand. +#[derive(Clone, Debug)] +struct RandomSitting; +impl Goal for RandomSitting { + goal_clone!(); + fn flags(&self) -> u8 { + MOVE + } + fn can_use(&mut self, m: &mut Mob, _cx: &mut Cx) -> bool { + !m.fluids.in_water + && pose_time(m) >= 400 + && m.on_ground + && m.rider.is_none() + && m.random.next_int_bound(4).unwrap_or(1) == 0 + } + fn can_continue(&mut self, _m: &mut Mob, _cx: &mut Cx) -> bool { + false + } + fn start(&mut self, m: &mut Mob, cx: &mut Cx) { + if sitting(m) { + stand_up(m, cx.events); + } else if !super::goals::should_panic(m) { + sit_down(m, cx.events); + m.ai.nav.stop(); + } + } +} +/// `Camel.tick`: the dash clock, water stands it up; keyframe states. +pub fn ai_step(m: &mut Mob, events: &mut Vec) { + if m.extra.flag2 && m.extra.anim_ticks < 50 && (m.on_ground || m.fluids.in_water) { + m.extra.flag2 = false; + } + if m.extra.anim_ticks > 0 { + m.extra.anim_ticks -= 1; + if m.extra.anim_ticks == 0 { + sound(m, "entity.camel.dash_ready", events); + } + } + if sitting(m) && m.fluids.in_water { + stand_up_instantly(m); + } + if refuse_to_move(m) { + m.velocity.x = 0.0; + m.velocity.z = 0.0; + m.ai.xxa = 0.0; + m.ai.zza = 0.0; + m.ai.nav.stop(); + } + // setupAnimationStates. + let age = m.age as f32; + if m.extra.ints[0] <= 0 { + m.extra.ints[0] = m.random.next_int_bound(40).unwrap_or(0) + 80; + m.anim.states[3].start(age); + } else { + m.extra.ints[0] -= 1; + } + // isCamelVisuallySitting: (poseTime < 0) != sitting. + let visually_sitting = (pose_time(m) < 0) != sitting(m); + let sitting_down = sitting(m) && pose_time(m) < SIT_DOWN && pose_time(m) >= 0; + let transition = in_transition(m) && pose_time(m) >= 0; + let dashing = m.extra.flag2; + let st = &mut m.anim.states; + if visually_sitting { + st[2].stop(); + st[4].stop(); + if sitting_down { + st[0].start_if_stopped(age); + st[1].stop(); + } else { + st[0].stop(); + st[1].start_if_stopped(age); + } + } else { + st[0].stop(); + st[1].stop(); + st[4].animate_when(dashing, age); + st[2].animate_when(transition, age); + } +} +/// `Camel.executeRidersJump`: the dash along the look, once cooled down. +pub fn dash(m: &mut Mob, amount: f32, events: &mut Vec) { + if m.extra.anim_ticks > 0 || !m.on_ground || refuse_to_move(m) { + return; + } + let yaw = m.yaw.to_radians(); + let (dx, dz) = (f64::from(-yaw.sin()), f64::from(yaw.cos())); + let speed = m.movement_speed(); + let a = f64::from(amount); + m.velocity.x += dx * 22.2222 * a * speed; + m.velocity.z += dz * 22.2222 * a * speed; + m.velocity.y += 1.4285 * a * 0.42; + m.extra.anim_ticks = DASH_COOLDOWN; + m.extra.flag2 = true; + sound(m, "entity.camel.dash", events); +} +/// `CamelModel.setupAnim`. +pub fn anim( + m: &Mob, + s: &State, + p: &mut Poses, + animations: &HashMap, +) { + let mut y = s.y_rot.clamp(-30.0, 30.0); + let mut x = s.x_rot.clamp(-25.0, 45.0); + let cooldown = (m.extra.anim_ticks as f32 - s.partial).max(0.0); + if cooldown > 0.0 { + x = (x + 45.0 * cooldown / 55.0).clamp(-25.0, 70.0); + } + y = y.to_radians(); + p.yrot("head", y); + p.xrot("head", x.to_radians()); + let prefix = if m.is_baby() { + "CamelBabyAnimation.CAMEL_BABY_" + } else { + "CamelAnimation.CAMEL_" + }; + let get = |n: &str| animations.get(&format!("{prefix}{n}")); + if let Some(walk) = get("WALK") { + walk.apply_walk(s.pos, s.speed, 2.0, 2.5, p); + } + let age = s.age; + for (state, name) in [ + (&m.anim.states[0], "SIT"), + (&m.anim.states[1], "SIT_POSE"), + (&m.anim.states[2], "STANDUP"), + (&m.anim.states[3], "IDLE"), + (&m.anim.states[4], "DASH"), + ] { + if let (Some(ms), Some(a)) = (state.millis(age), get(name)) { + a.apply(ms, 1.0, p); + } + } +} diff --git a/crates/horizon-client/src/mobs/equine.rs b/crates/horizon-client/src/mobs/equine.rs index 2b3b20bfe..33be2a248 100644 --- a/crates/horizon-client/src/mobs/equine.rs +++ b/crates/horizon-client/src/mobs/equine.rs @@ -39,6 +39,8 @@ pub struct Rider { pub jump: f32, /// Holding the mount's steering stick (pigs, striders). pub steer: bool, + /// Sprinting (camels run faster). + pub sprint: bool, } pub fn is_equine(s: Species) -> bool { @@ -49,6 +51,7 @@ pub fn is_equine(s: Species) -> bool { | Species::Mule | Species::SkeletonHorse | Species::ZombieHorse + | Species::Camel ) } /// `canPerformRearing`: not donkeys or mules. @@ -59,6 +62,9 @@ fn can_rear(s: Species) -> bool { ) } pub fn register(m: &mut Mob) { + if m.species() == Species::Camel { + return super::camel::register(m); + } let tempt = m.kind.tempt.clone(); let rears = can_rear(m.species()); let g = &mut m.ai.goals; @@ -300,6 +306,40 @@ pub fn ridden_step(m: &mut Mob) { m.set_speed((m.attrs.speed * factor) as f32 * boost); return; } + if m.species() == Species::Camel { + // Camel.tickRidden / getRiddenInput: forward stands it up; no input + // while sitting; the jump is a dash. + if r.forward > 0.0 && super::camel::sitting(m) { + let mut ev = Vec::new(); + super::camel::stand_up(m, &mut ev); + } + if super::camel::refuse_to_move(m) { + m.ai.xxa = 0.0; + m.ai.zza = 0.0; + return; + } + let forward = if r.forward <= 0.0 { + r.forward * 0.25 + } else { + r.forward + }; + m.ai.xxa = r.strafe * 0.5; + m.ai.zza = forward; + let bonus = if r.sprint && m.extra.anim_ticks == 0 { + 0.1 + } else { + 0.0 + }; + m.set_speed(m.attrs.speed as f32 + bonus); + if r.jump > 0.0 { + let mut ev = Vec::new(); + super::camel::dash(m, r.jump, &mut ev); + if let Some(rider) = m.rider.as_mut() { + rider.jump = 0.0; + } + } + return; + } let standing = m.extra.counter > 0; if m.on_ground && r.jump == 0.0 && standing { m.ai.xxa = 0.0; @@ -331,7 +371,10 @@ pub fn ridden_step(m: &mut Mob) { } /// `AbstractHorse.aiStep` and `tick`: healing, grazing, the rear and the /// eating/standing/mouth animations. -pub fn ai_step(m: &mut Mob, w: &MobWorld) { +pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { + if m.species() == Species::Camel { + return super::camel::ai_step(m, events); + } if m.alive() && m.random.next_int_bound(900).unwrap_or(1) == 0 && m.death_time == 0 { m.health = (m.health + 1.0).min(m.attrs.max_health); } @@ -388,6 +431,7 @@ pub fn texture(m: &Mob) -> String { Species::Donkey => format!("entity/horse/donkey{baby}"), Species::Mule => format!("entity/horse/mule{baby}"), Species::SkeletonHorse => format!("entity/horse/horse_skeleton{baby}"), + Species::Camel => format!("entity/camel/camel{baby}"), _ => format!("entity/horse/horse_zombie{baby}"), } } @@ -409,6 +453,7 @@ pub fn model_name(s: Species) -> &'static str { Species::Donkey => "donkey", Species::Mule => "mule", Species::SkeletonHorse => "skeleton_horse", + Species::Camel => "camel", _ => "zombie_horse", } } @@ -419,6 +464,7 @@ pub fn saddle_texture(s: Species) -> &'static str { Species::Mule => "entity/equipment/mule_saddle/saddle", Species::SkeletonHorse => "entity/equipment/skeleton_horse_saddle/saddle", Species::ZombieHorse => "entity/equipment/zombie_horse_saddle/saddle", + Species::Camel => "entity/equipment/camel_saddle/saddle", _ => "entity/equipment/horse_saddle/saddle", } } diff --git a/crates/horizon-client/src/mobs/spawning.rs b/crates/horizon-client/src/mobs/spawning.rs index 072bcd259..23e568c96 100644 --- a/crates/horizon-client/src/mobs/spawning.rs +++ b/crates/horizon-client/src/mobs/spawning.rs @@ -231,6 +231,8 @@ pub fn finalize(m: &mut Mob, biome: &BiomeFacts) { S::Horse | S::Donkey | S::Mule | S::SkeletonHorse | S::ZombieHorse => { super::equine::finalize(m) } + // Camel.finalizeSpawn: standing, with fixed attributes. + S::Camel => super::camel::stand_up_instantly(m), // Piglin.createSpawnWeapon: half crossbows, half golden swords. S::Piglin if !m.is_baby() => { let crossbow = m.random.next_float() < 0.5; diff --git a/crates/horizon-client/src/mobs/species.rs b/crates/horizon-client/src/mobs/species.rs index e243488d5..04e8c9e5d 100644 --- a/crates/horizon-client/src/mobs/species.rs +++ b/crates/horizon-client/src/mobs/species.rs @@ -81,6 +81,7 @@ pub enum Species { Vex, Ravager, Wither, + Camel, } /// Baby bounding boxes (each class's BABY_DIMENSIONS). #[derive(Clone, Copy, Debug)] @@ -898,6 +899,20 @@ pub const ALL: &[Def] = &[ None, Baby::None ), + def!( + Camel, + "camel", + "camel#main", + "entity/camel/camel", + A::Camel, + [ + "entity.camel.ambient", + "entity.camel.hurt", + "entity.camel.death" + ], + Some("entity.camel.step"), + Baby::Abs(0.95, 1.4, 1.38) + ), def!( Wither, "wither", diff --git a/crates/horizon-client/src/mobs/wither.rs b/crates/horizon-client/src/mobs/wither.rs index 0ddfcdc97..f7e34d2cc 100644 --- a/crates/horizon-client/src/mobs/wither.rs +++ b/crates/horizon-client/src/mobs/wither.rs @@ -166,6 +166,9 @@ fn target_pos(w: &MobWorld, id: i32) -> Option<([f64; 3], f64, bool)> { } /// `WitherBoss.aiStep` (heads, particles) and `customServerAiStep`. pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { + if !m.alive() { + return; + } m.extra.point = m .target_info(w) .map(|t| [t.position.x, t.position.y, t.position.z]);