Merge pull request #2402 from kacpherek/fix/parrot-imitate-dance
🤖 Agent: Merged by Ludovic's AI agent (orchestrator). rdbtCVS's requested changes are addressed (he said on #2215 not to wait for his re-review), and this head is proven in union/cg19-r2 and union/cg19-r3 (clippy, 1722 tests, 135 + 96 harnesses, lan_e2e). Thanks @kacpherek!
This commit is contained in:
@@ -1839,6 +1839,9 @@ struct Game<'a> {
|
||||
guest_use: Option<GuestUse>,
|
||||
/// Level events of this tick for the guests, and the guest acting now.
|
||||
lan_events: Vec<(i32, [i32; 3], i32, Option<u32>)>,
|
||||
/// Jukebox level events 1010 (song name) and 1011 (`None`) for the guests;
|
||||
/// the song's network id is only known once the hub is in hand.
|
||||
lan_songs: Vec<([i32; 3], Option<String>)>,
|
||||
lan_cushion_breaks: Vec<(horizon_world::registry::BlockStateId, [f64; 3])>,
|
||||
/// Block events (position, b0, b1, state) and entity events (id, event,
|
||||
/// position) for the guests.
|
||||
@@ -3228,6 +3231,12 @@ impl Game<'_> {
|
||||
data,
|
||||
} => self.sculk_charge_event(pos, data),
|
||||
LevelEvent::LevelEvent { id: 3007, pos, .. } => self.shriek_event(pos),
|
||||
// LevelEventHandler 1010 and 1011: the parrots nearby dance or stop.
|
||||
LevelEvent::LevelEvent {
|
||||
id: id @ (1010 | 1011),
|
||||
pos,
|
||||
..
|
||||
} => self.notify_record_playing([pos.x, pos.y, pos.z], id == 1010),
|
||||
// LevelEventHandler 2019 and 2020: the crack on a face of the
|
||||
// block being mined, with its hit sound for 2020.
|
||||
LevelEvent::LevelEvent {
|
||||
@@ -13747,6 +13756,7 @@ impl<'a> Game<'a> {
|
||||
forced_hit: None,
|
||||
guest_use: None,
|
||||
lan_events: Vec::new(),
|
||||
lan_songs: Vec::new(),
|
||||
lan_cushion_breaks: Vec::new(),
|
||||
lan_block_events: Vec::new(),
|
||||
lan_entity_events: Vec::new(),
|
||||
|
||||
@@ -6,10 +6,15 @@ use super::Game;
|
||||
use horizon_client::{
|
||||
containers::{BlockEntity, Jukebox},
|
||||
items::ItemStack,
|
||||
mobs::Species,
|
||||
particles::{ParticleOptions, ParticleType},
|
||||
text::Text,
|
||||
};
|
||||
use horizon_core::{position::BlockPos, random::RandomSource};
|
||||
use horizon_core::{
|
||||
geometry::{Aabb, Vec3},
|
||||
position::BlockPos,
|
||||
random::RandomSource,
|
||||
};
|
||||
use horizon_world::level::flags as level_flags;
|
||||
|
||||
impl Game<'_> {
|
||||
@@ -97,7 +102,7 @@ impl Game<'_> {
|
||||
}
|
||||
/// Level event 1010: the song from the jukebox, and the "Now Playing" overlay.
|
||||
fn start_song(&mut self, pos: [i32; 3], song: &str) {
|
||||
self.stop_song(pos);
|
||||
self.stop_sound(pos);
|
||||
let Some(s) = self.res.items().songs.get(song).cloned() else {
|
||||
return;
|
||||
};
|
||||
@@ -109,12 +114,38 @@ impl Game<'_> {
|
||||
self.titles
|
||||
.set_overlay(Text::translate("record.nowPlaying", &[description]), true);
|
||||
self.update_song_volumes();
|
||||
self.notify_record_playing(pos, true);
|
||||
if self.audio.capture.is_some() {
|
||||
self.lan_songs.push((pos, Some(song.to_owned())));
|
||||
}
|
||||
}
|
||||
fn stop_song(&mut self, pos: [i32; 3]) {
|
||||
fn stop_sound(&mut self, pos: [i32; 3]) {
|
||||
if let Some(id) = self.jukebox_sounds.remove(&(pos[0], pos[1], pos[2])) {
|
||||
self.audio.stop_and_release(id);
|
||||
}
|
||||
}
|
||||
/// Level event 1011: the song ends, here and for the guests.
|
||||
fn stop_song(&mut self, pos: [i32; 3]) {
|
||||
self.stop_sound(pos);
|
||||
self.notify_record_playing(pos, false);
|
||||
if self.audio.capture.is_some() {
|
||||
self.lan_songs.push((pos, None));
|
||||
}
|
||||
}
|
||||
/// `LevelEventHandler.notifyNearbyEntities` (level events 1010 and 1011):
|
||||
/// parrots within three blocks of the jukebox start or stop dancing.
|
||||
pub(super) fn notify_record_playing(&mut self, pos: [i32; 3], playing: bool) {
|
||||
let min = Vec3::new(f64::from(pos[0]), f64::from(pos[1]), f64::from(pos[2]));
|
||||
let area = Aabb::new(min, min.add_vector(Vec3::new(1.0, 1.0, 1.0)))
|
||||
.inflate(Vec3::new(3.0, 3.0, 3.0));
|
||||
for m in self
|
||||
.mobs
|
||||
.iter_mut()
|
||||
.filter(|m| m.species() == Species::Parrot && m.bounds().intersects(area))
|
||||
{
|
||||
m.extra.party_jukebox = playing.then_some(pos);
|
||||
}
|
||||
}
|
||||
/// `popOutTheItem`: the disc above the jukebox, and the song stops.
|
||||
fn eject_record(&mut self, pos: [i32; 3]) {
|
||||
let key = (pos[0], pos[1], pos[2]);
|
||||
|
||||
@@ -1094,6 +1094,7 @@ impl Game<'_> {
|
||||
self.world.light_log = None;
|
||||
self.audio.capture = None;
|
||||
self.lan_events.clear();
|
||||
self.lan_songs.clear();
|
||||
self.lan_critical_hits.clear();
|
||||
self.lan_cushion_breaks.clear();
|
||||
self.streaming.extra.clear();
|
||||
@@ -1790,6 +1791,16 @@ impl Game<'_> {
|
||||
}
|
||||
host.hub.level_event(kind, pos, data, acting);
|
||||
}
|
||||
// Level events 1010 and 1011: the guests' parrots dance or stop.
|
||||
for (pos, song) in std::mem::take(&mut self.lan_songs) {
|
||||
match song {
|
||||
Some(name) => {
|
||||
let id = host.hub.registry_id("minecraft:jukebox_song", &name);
|
||||
host.hub.level_event(1010, pos, id.unwrap_or(0), None);
|
||||
}
|
||||
None => host.hub.level_event(1011, pos, 0, None),
|
||||
}
|
||||
}
|
||||
for (pos, b0, b1, state) in std::mem::take(&mut self.lan_block_events) {
|
||||
host.hub.block_event(pos, b0, b1, state);
|
||||
}
|
||||
|
||||
@@ -53,6 +53,7 @@ pub mod nautilus;
|
||||
pub mod nav;
|
||||
pub mod nether;
|
||||
pub mod panda;
|
||||
pub mod parrot;
|
||||
pub mod path;
|
||||
pub mod phantom;
|
||||
pub mod piglin;
|
||||
@@ -715,6 +716,8 @@ pub struct Extra {
|
||||
pub inventory: Vec<crate::items::ItemStack>,
|
||||
pub tame: bool,
|
||||
pub sitting: bool,
|
||||
/// `Parrot.partyParrot` and `jukebox`: the playing jukebox it dances to.
|
||||
pub party_jukebox: Option<[i32; 3]>,
|
||||
/// The owner's UUID (tamed animals).
|
||||
pub owner: Option<[i32; 4]>,
|
||||
/// Slime/magma cube size (0 = not sized).
|
||||
@@ -1466,6 +1469,8 @@ pub struct Seen {
|
||||
pub stats: [f64; 3],
|
||||
/// `Wolf.isInSittingPose`.
|
||||
pub sitting: bool,
|
||||
/// `Entity.isSilent`.
|
||||
pub silent: bool,
|
||||
/// `Entity.isVehicle` or `isPassenger`.
|
||||
pub ridden: bool,
|
||||
/// `Attributes.KNOCKBACK_RESISTANCE`.
|
||||
@@ -1560,6 +1565,7 @@ impl Mob {
|
||||
m.attrs.speed,
|
||||
],
|
||||
sitting: m.extra.sitting,
|
||||
silent: m.silent,
|
||||
ridden: m.rider.is_some() || m.extra.rider.is_some() || m.extra.mount.is_some(),
|
||||
knockback_resistance: m.attrs.knockback_resistance,
|
||||
effects: m
|
||||
|
||||
@@ -786,6 +786,7 @@ fn play_ambient(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
|
||||
"entity.wolf.pant"
|
||||
}
|
||||
}
|
||||
Species::Parrot => super::parrot::ambient(m, w),
|
||||
Species::Cat if m.extra.tame && m.extra.in_love > 0 => "entity.cat.purr",
|
||||
Species::Cat if !m.extra.tame => "entity.cat.stray_ambient",
|
||||
Species::PolarBear if m.is_baby() => "entity.polar_bear.ambient_baby",
|
||||
@@ -837,6 +838,9 @@ fn play_ambient(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
|
||||
}
|
||||
/// LivingEntity.aiStep (and Mob/AgeableMob/Animal additions).
|
||||
fn ai_step(m: &mut Mob, w: &MobWorld, level_random: &mut LegacyRandom, events: &mut Vec<MobEvent>) {
|
||||
if m.species() == Species::Parrot {
|
||||
super::parrot::ai_step(m, w, level_random, events);
|
||||
}
|
||||
// Witch.aiStep runs before super.aiStep().
|
||||
if m.species() == Species::Witch {
|
||||
let far = m
|
||||
|
||||
@@ -981,7 +981,9 @@ pub fn anim(m: &Mob, s: &State, p: &mut Poses, a: &Animations) -> bool {
|
||||
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 {
|
||||
let pose = if m.extra.party_jukebox.is_some() {
|
||||
ParrotPose::Party
|
||||
} else if m.extra.sitting {
|
||||
ParrotPose::Sitting
|
||||
} else if !m.on_ground {
|
||||
ParrotPose::Flying
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
//! What `Parrot.aiStep` adds: imitating the mobs around it (`imitateNearbyMobs`,
|
||||
//! the ambient sound of `getAmbient`) and dancing near a playing jukebox.
|
||||
use super::{Mob, MobEvent, MobWorld, Species};
|
||||
use horizon_core::{
|
||||
geometry::Vec3,
|
||||
random::{LegacyRandom, RandomSource},
|
||||
};
|
||||
|
||||
/// `Parrot.MOB_SOUND_MAP` (the happy ghast's is `SoundEvents.EMPTY`: "").
|
||||
const IMITATIONS: [(Species, &str); 41] = [
|
||||
(Species::Blaze, "entity.parrot.imitate.blaze"),
|
||||
(Species::Bogged, "entity.parrot.imitate.bogged"),
|
||||
(Species::Breeze, "entity.parrot.imitate.breeze"),
|
||||
(Species::CamelHusk, "entity.parrot.imitate.camel_husk"),
|
||||
(Species::CaveSpider, "entity.parrot.imitate.spider"),
|
||||
(Species::Creaking, "entity.parrot.imitate.creaking"),
|
||||
(Species::Creeper, "entity.parrot.imitate.creeper"),
|
||||
(Species::Drowned, "entity.parrot.imitate.drowned"),
|
||||
(
|
||||
Species::ElderGuardian,
|
||||
"entity.parrot.imitate.elder_guardian",
|
||||
),
|
||||
(Species::Endermite, "entity.parrot.imitate.endermite"),
|
||||
(Species::Evoker, "entity.parrot.imitate.evoker"),
|
||||
(Species::Ghast, "entity.parrot.imitate.ghast"),
|
||||
(Species::HappyGhast, ""),
|
||||
(Species::Guardian, "entity.parrot.imitate.guardian"),
|
||||
(Species::Hoglin, "entity.parrot.imitate.hoglin"),
|
||||
(Species::Husk, "entity.parrot.imitate.husk"),
|
||||
(Species::Illusioner, "entity.parrot.imitate.illusioner"),
|
||||
(Species::MagmaCube, "entity.parrot.imitate.magma_cube"),
|
||||
(Species::Parched, "entity.parrot.imitate.parched"),
|
||||
(Species::Phantom, "entity.parrot.imitate.phantom"),
|
||||
(Species::Piglin, "entity.parrot.imitate.piglin"),
|
||||
(Species::PiglinBrute, "entity.parrot.imitate.piglin_brute"),
|
||||
(Species::Pillager, "entity.parrot.imitate.pillager"),
|
||||
(Species::Ravager, "entity.parrot.imitate.ravager"),
|
||||
(Species::Shulker, "entity.parrot.imitate.shulker"),
|
||||
(Species::Silverfish, "entity.parrot.imitate.silverfish"),
|
||||
(Species::Skeleton, "entity.parrot.imitate.skeleton"),
|
||||
(Species::Slime, "entity.parrot.imitate.slime"),
|
||||
(Species::Spider, "entity.parrot.imitate.spider"),
|
||||
(Species::Stray, "entity.parrot.imitate.stray"),
|
||||
(Species::Vex, "entity.parrot.imitate.vex"),
|
||||
(Species::Vindicator, "entity.parrot.imitate.vindicator"),
|
||||
(Species::Warden, "entity.parrot.imitate.warden"),
|
||||
(Species::Witch, "entity.parrot.imitate.witch"),
|
||||
(Species::Wither, "entity.parrot.imitate.wither"),
|
||||
(
|
||||
Species::WitherSkeleton,
|
||||
"entity.parrot.imitate.wither_skeleton",
|
||||
),
|
||||
(Species::Zoglin, "entity.parrot.imitate.zoglin"),
|
||||
(Species::Zombie, "entity.parrot.imitate.zombie"),
|
||||
(Species::ZombieHorse, "entity.parrot.imitate.zombie_horse"),
|
||||
(
|
||||
Species::ZombieNautilus,
|
||||
"entity.parrot.imitate.zombie_nautilus",
|
||||
),
|
||||
(
|
||||
Species::ZombieVillager,
|
||||
"entity.parrot.imitate.zombie_villager",
|
||||
),
|
||||
];
|
||||
|
||||
fn imitation(species: Species) -> Option<&'static str> {
|
||||
IMITATIONS.iter().find(|i| i.0 == species).map(|i| i.1)
|
||||
}
|
||||
|
||||
/// `Parrot.getPitch`.
|
||||
fn pitch(random: &mut LegacyRandom) -> f32 {
|
||||
(random.next_float() - random.next_float()) * 0.2 + 1.0
|
||||
}
|
||||
|
||||
/// The start of `Parrot.aiStep`: a jukebox that is gone or farther than
|
||||
/// 3.46 blocks stops the dance, and one tick in 400 the parrot imitates a
|
||||
/// mob nearby.
|
||||
pub fn ai_step(
|
||||
m: &mut Mob,
|
||||
w: &MobWorld,
|
||||
level_random: &mut LegacyRandom,
|
||||
events: &mut Vec<MobEvent>,
|
||||
) {
|
||||
if let Some(at) = m.extra.party_jukebox {
|
||||
let d = |i: usize, v: f64| f64::from(at[i]) + 0.5 - v;
|
||||
let (x, y, z) = (d(0, m.position.x), d(1, m.position.y), d(2, m.position.z));
|
||||
let jukebox = &*w.facts.get(w.world.block(at[0], at[1], at[2])).name == "minecraft:jukebox";
|
||||
if x * x + y * y + z * z >= 3.46 * 3.46 || !jukebox {
|
||||
m.extra.party_jukebox = None;
|
||||
}
|
||||
}
|
||||
if level_random.next_int_bound(400).unwrap_or(1) == 0 {
|
||||
imitate_nearby_mobs(m, w, level_random, events);
|
||||
}
|
||||
}
|
||||
|
||||
/// `Parrot.imitateNearbyMobs`: half the time, a random imitable mob within 20
|
||||
/// blocks is heard through the parrot's own mouth.
|
||||
fn imitate_nearby_mobs(
|
||||
m: &Mob,
|
||||
w: &MobWorld,
|
||||
random: &mut LegacyRandom,
|
||||
events: &mut Vec<MobEvent>,
|
||||
) {
|
||||
if !m.alive() || m.silent || random.next_int_bound(2).unwrap_or(1) != 0 {
|
||||
return;
|
||||
}
|
||||
let area = m.bounds().inflate(Vec3::new(20.0, 20.0, 20.0));
|
||||
let mobs: Vec<_> = w
|
||||
.others
|
||||
.iter()
|
||||
.filter(|s| s.alive && imitation(s.species).is_some() && s.bounds.intersects(area))
|
||||
.collect();
|
||||
if mobs.is_empty() {
|
||||
return;
|
||||
}
|
||||
let mob = mobs[random.next_int_bound(mobs.len() as i32).unwrap_or(0) as usize];
|
||||
if mob.silent {
|
||||
return;
|
||||
}
|
||||
if let Some(sound) = imitation(mob.species).filter(|s| !s.is_empty()) {
|
||||
let at = [m.position.x, m.position.y, m.position.z];
|
||||
events.push(MobEvent::Sound(sound, at, 0.7, pitch(random)));
|
||||
}
|
||||
}
|
||||
|
||||
/// `Parrot.getAmbient`: now and then (outside peaceful) the voice of a random
|
||||
/// imitable mob instead of the parrot's own.
|
||||
pub fn ambient(m: &mut Mob, w: &MobWorld) -> &'static str {
|
||||
if w.difficulty.effective > 0.0 && m.random.next_int_bound(1000).unwrap_or(1) == 0 {
|
||||
let i = m
|
||||
.random
|
||||
.next_int_bound(IMITATIONS.len() as i32)
|
||||
.unwrap_or(0);
|
||||
return IMITATIONS[i as usize].1;
|
||||
}
|
||||
"entity.parrot.ambient"
|
||||
}
|
||||
Reference in New Issue
Block a user