Merge origin/main into fix/1938-ghast-dismount
This commit is contained in:
@@ -4,14 +4,14 @@
|
||||
//! own simulation, which a client of a server doesn't run.
|
||||
use super::Game;
|
||||
use horizon_client::mobs::render::{PROFESSIONS, VILLAGER_TYPES};
|
||||
use horizon_client::mobs::{Mob, Species, tame};
|
||||
use horizon_client::mobs::{Mob, Species, Who, bat, tame};
|
||||
use horizon_protocol::entity_data::Value;
|
||||
use horizon_protocol::registry::Builtin;
|
||||
|
||||
/// The values `apply` takes from one SetEntityData packet, by vanilla's entry
|
||||
/// indices (Entity 0..=7, LivingEntity 8..=14, Mob 15, then the subclasses).
|
||||
/// `builtin` names the ids of built-in registries.
|
||||
fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin) {
|
||||
fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin, local_entity: i32) {
|
||||
let species = m.species();
|
||||
let baby = super::lan::baby_index(&m.kind.id);
|
||||
for (index, _, value) in values {
|
||||
@@ -46,6 +46,21 @@ fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin) {
|
||||
_ => {}
|
||||
}
|
||||
match (species, *index, value) {
|
||||
(Species::Guardian | Species::ElderGuardian, 16, Value::Bool(moving)) => {
|
||||
m.extra.flag2 = *moving;
|
||||
}
|
||||
(Species::Guardian | Species::ElderGuardian, 17, Value::Int(target)) => {
|
||||
// Guardian.onSyncedDataUpdated resets the visual charge clock.
|
||||
m.extra.counter2 = 0;
|
||||
m.extra.flag = *target != 0;
|
||||
m.ai.target = (*target != 0).then_some(if *target == local_entity {
|
||||
Who::Player
|
||||
} else {
|
||||
Who::Mob(*target as u32)
|
||||
});
|
||||
}
|
||||
// Bat.DATA_ID_FLAGS: resting in bit 0.
|
||||
(Species::Bat, 16, Value::Byte(flags)) => bat::set_resting(m, flags & 1 != 0),
|
||||
// Sheep.DATA_WOOL_ID: the colour in the low nibble, 16 sheared.
|
||||
(Species::Sheep, 18, Value::Byte(wool)) => {
|
||||
m.extra.color = (*wool & 15) as u8;
|
||||
@@ -118,7 +133,7 @@ impl Game<'_> {
|
||||
return;
|
||||
};
|
||||
if let Some(m) = self.mobs.iter_mut().find(|m| m.id == id as u32) {
|
||||
apply(m, values, &r.builtin);
|
||||
apply(m, values, &r.builtin, r.entity_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -130,6 +145,10 @@ pub(super) fn client_defaults(m: &mut Mob) {
|
||||
if matches!(m.species(), Species::Wolf | Species::Cat) {
|
||||
m.extra.color = tame::RED;
|
||||
}
|
||||
if m.species() == Species::Bat {
|
||||
// Bat's constructor sets resting only on the server; the data default is 0.
|
||||
bat::set_resting(m, false);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -151,7 +170,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn apply(m: &mut Mob, values: &[(u8, i32, Value)]) {
|
||||
super::apply(m, values, &builtin());
|
||||
super::apply(m, values, &builtin(), 99);
|
||||
}
|
||||
|
||||
fn mob(kind: &str) -> Option<Mob> {
|
||||
@@ -162,6 +181,49 @@ mod tests {
|
||||
Some(m)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guardian_charge_follows_target_updates_and_caps_at_its_duration() {
|
||||
use horizon_client::mobs::guardian::{beam, client_tick};
|
||||
for (kind, duration) in [("guardian", 80), ("elder_guardian", 60)] {
|
||||
let Some(mut m) = mob(&format!("minecraft:{kind}")) else {
|
||||
return;
|
||||
};
|
||||
assert!(beam(&m, 0.0).is_none());
|
||||
apply(
|
||||
&mut m,
|
||||
&[
|
||||
(16, s::BOOLEAN, Value::Bool(true)),
|
||||
(17, s::INT, Value::Int(99)),
|
||||
],
|
||||
);
|
||||
assert!(m.extra.flag2);
|
||||
assert_eq!(m.ai.target, Some(Who::Player));
|
||||
assert_eq!(beam(&m, 0.0), Some((0.0, 0.0)));
|
||||
for _ in 0..duration + 10 {
|
||||
client_tick(&mut m);
|
||||
}
|
||||
assert_eq!(beam(&m, 0.0), Some((duration as f32, 1.0)));
|
||||
// Another player or a mob retains its network entity id.
|
||||
apply(&mut m, &[(17, s::INT, Value::Int(42))]);
|
||||
assert_eq!(m.ai.target, Some(Who::Mob(42)));
|
||||
assert_eq!(m.extra.counter2, 0);
|
||||
client_tick(&mut m);
|
||||
assert_eq!(m.extra.counter2, 1);
|
||||
apply(
|
||||
&mut m,
|
||||
&[
|
||||
(16, s::BOOLEAN, Value::Bool(false)),
|
||||
(17, s::INT, Value::Int(0)),
|
||||
],
|
||||
);
|
||||
client_tick(&mut m);
|
||||
assert!(!m.extra.flag2);
|
||||
assert_eq!(m.ai.target, None);
|
||||
assert_eq!(m.extra.counter2, 0);
|
||||
assert!(beam(&m, 0.0).is_none());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_sheep_takes_its_wool_and_a_calf_its_size() {
|
||||
let Some(mut m) = mob("minecraft:sheep") else {
|
||||
@@ -178,6 +240,69 @@ mod tests {
|
||||
assert!(!calf.is_baby());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bat_flags_select_continuous_client_clocks_without_changing_motion() {
|
||||
use horizon_client::mobs::{MobTypes, anim, keyframes::Animation};
|
||||
use horizon_core::geometry::Vec3;
|
||||
use serde_json::json;
|
||||
use std::{collections::HashMap, fs, sync::Arc};
|
||||
|
||||
// Original minimal resources keep this regression runnable without vanilla data.
|
||||
let root =
|
||||
std::env::temp_dir().join(format!("horizon-bat-animation-{}", std::process::id()));
|
||||
fs::create_dir(&root).unwrap();
|
||||
fs::write(root.join("entity-types.json"), r#"{"minecraft:bat": {}}"#).unwrap();
|
||||
fs::write(
|
||||
root.join("entity-models.json"),
|
||||
r#"{"minecraft:bat#main": {"children": {"right_wing": {}, "head": {}}}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
let mut types = MobTypes::load(&root).unwrap();
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
let position = Vec3::new(1.0, 2.25, 3.0);
|
||||
let velocity = Vec3::new(0.2, 0.3, 0.4);
|
||||
let mut m = Mob::new(
|
||||
1,
|
||||
Arc::new(types.types.remove("minecraft:bat").unwrap()),
|
||||
position,
|
||||
0.0,
|
||||
);
|
||||
assert!(bat::resting(&m));
|
||||
client_defaults(&mut m);
|
||||
assert!(!bat::resting(&m));
|
||||
m.velocity = velocity;
|
||||
m.age = 1;
|
||||
bat::setup_animation_states(&mut m);
|
||||
m.age = 4;
|
||||
apply(&mut m, &[(16, s::BYTE, Value::Byte(2))]);
|
||||
bat::setup_animation_states(&mut m);
|
||||
assert_eq!(m.anim.states[0].started, Some(1.0));
|
||||
|
||||
let curve = Animation::parse(&json!({"length": 1, "looping": true,
|
||||
"bones": {"right_wing": [{"target": "rotation", "keyframes":
|
||||
[[0,[0,0,0],[0,0,0],"linear"],[1,[0,1,0],[0,1,0],"linear"]]}]}}));
|
||||
let animations = HashMap::from([("BatAnimation.BAT_FLYING".into(), curve)]);
|
||||
let wing = |partial| {
|
||||
let mut poses = anim::Poses::new(&m.kind.model);
|
||||
anim::setup(&m, &anim::state(&m, partial), &mut poses, &animations);
|
||||
poses.get("right_wing")[4]
|
||||
};
|
||||
assert!(wing(0.5) > wing(0.0));
|
||||
|
||||
m.age = 5;
|
||||
apply(&mut m, &[(16, s::BYTE, Value::Byte(3))]);
|
||||
bat::setup_animation_states(&mut m);
|
||||
assert!(bat::resting(&m));
|
||||
assert_eq!(m.anim.states[0].started, None);
|
||||
assert_eq!(m.anim.states[1].started, Some(5.0));
|
||||
m.age = 7;
|
||||
apply(&mut m, &[(16, s::BYTE, Value::Byte(0))]);
|
||||
bat::setup_animation_states(&mut m);
|
||||
assert_eq!(m.anim.states[0].started, Some(7.0));
|
||||
assert_eq!(m.anim.states[1].started, None);
|
||||
assert_eq!((m.position, m.velocity), (position, velocity));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_wolf_takes_its_owner_flags_and_collar() {
|
||||
let Some(mut m) = mob("minecraft:wolf") else {
|
||||
|
||||
Reference in New Issue
Block a user