diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 5752d9a57..a548c7c11 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -81,6 +81,7 @@ mod paintings; mod passengers; mod persist; mod perspective; +mod pickups; mod piglins; mod postprocess; mod potions; @@ -734,6 +735,8 @@ struct Game<'a> { dropped: Vec, next_entity: u32, particles: horizon_client::particles::ParticleEngine, + /// `ItemPickupParticle`s: taken items, orbs and arrows flying to their collector. + pickups: pickups::Pickups, /// Sign text being edited (`SignEditScreen`). sign_edit: Option, /// An open book, book and quill or lectern screen. @@ -1415,6 +1418,10 @@ impl Game<'_> { match self.give(item.stack.clone()) { None => { self.stats.add("picked_up", &kind, before as i32); + self.add_pickup( + pickups::Taken::Item(Box::new(item)), + pickups::Collector::Player, + ); let pitch = ((self.world.random.next_float() - self.world.random.next_float()) * 0.7 @@ -1428,6 +1435,10 @@ impl Game<'_> { if rest.count < before { self.stats .add("picked_up", &kind, (before - rest.count) as i32); + self.add_pickup( + pickups::Taken::Item(Box::new(item.clone())), + pickups::Collector::Player, + ); self.audio .play("minecraft:entity.item.pickup", Some(feet), 0.2, 1.0); } @@ -4938,16 +4949,25 @@ impl Game<'_> { ); let right = forward.cross(glam::Vec3::Y).normalize_or_zero(); let up = right.cross(forward).normalize_or_zero(); - for o in &self.orbs { - let p = glam::DVec3::new( + let live = self.orbs.iter().map(|o| { + let at = [ o.previous.x + (o.position.x - o.previous.x) * alpha, - o.previous.y + (o.position.y - o.previous.y) * alpha + 0.1, + o.previous.y + (o.position.y - o.previous.y) * alpha, o.previous.z + (o.position.z - o.previous.z) * alpha, - ) - camera; + ]; + (o, at, alpha as f32) + }); + // ItemPickupParticle: the render state extracted at partial tick 1. + let taken = self.pickups.live.iter().filter_map(|p| match &p.taken { + pickups::Taken::Orb(o) => Some((&**o, pickups::position(p, alpha), 1.0)), + _ => None, + }); + for (o, at, partial) in live.chain(taken) { + let p = glam::DVec3::new(at[0], at[1] + 0.1, at[2]) - camera; let icon = o.icon(); let (du, dv) = ((r.u1 - r.u0) / 4.0, (r.v1 - r.v0) / 4.0); let (u0, v0) = (r.u0 + (icon % 4) as f32 * du, r.v0 + (icon / 4) as f32 * dv); - let t = (o.age as f32 + alpha as f32) / 2.0; + let t = (o.age as f32 + partial) / 2.0; let red = ((t.sin() + 1.0) * 0.5 * 255.0) as u32; let blue = (((t + 4.188_79).sin() + 1.0) * 0.1 * 255.0) as u32; let color = 0x8000_0000 | blue << 16 | 255 << 8 | red; @@ -5707,6 +5727,7 @@ impl Game<'_> { self.creatures.projectiles.clear(); self.dropped.clear(); self.particles.clear(); + self.pickups.clear(); self.orbs.clear(); self.experience = Experience { level: self.level.xp_level, @@ -5858,6 +5879,7 @@ impl Game<'_> { self.creatures.projectiles.clear(); self.dropped.clear(); self.particles.clear(); + self.pickups.clear(); self.orbs.clear(); self.mining = None; self.travelling = Some((player, destination)); @@ -6008,6 +6030,7 @@ impl Game<'_> { self.creatures.projectiles.clear(); self.dropped.clear(); self.particles.clear(); + self.pickups.clear(); self.screen = Screen::Playing; if let Some(r) = self.renderer.as_mut() { r.clear_sections(); @@ -6703,6 +6726,7 @@ impl Game<'_> { let mut orbs = std::mem::take(&mut self.orbs); orbs.retain_mut(|o| o.tick(&self.world, &self.res.facts, self.collision, target)); let mut gained = 0; + let mut taken = Vec::new(); if let Some((min, max)) = bounds { orbs.retain(|o| { let touching = o.position.x + 0.25 > min[0] @@ -6715,12 +6739,16 @@ impl Game<'_> { // ExperienceOrb.playerTouch: one orb every 2 ticks. self.xp_delay = 2; gained += o.value; + taken.push(o.clone()); return false; } true }); } self.orbs = orbs; + for o in taken { + self.add_pickup(pickups::Taken::Orb(Box::new(o)), pickups::Collector::Player); + } if gained > 0 { gained = self.mending(gained); } @@ -9111,6 +9139,7 @@ pub fn run() -> Result<()> { dropped: Vec::new(), next_entity: 0, particles, + pickups: Default::default(), sign_edit: None, book: None, lectern_pulses: Vec::new(), diff --git a/crates/horizon-client/src/game/allays.rs b/crates/horizon-client/src/game/allays.rs index c948663b1..a2a5842b5 100644 --- a/crates/horizon-client/src/game/allays.rs +++ b/crates/horizon-client/src/game/allays.rs @@ -54,6 +54,11 @@ impl Game<'_> { .inflate(horizon_core::geometry::Vec3::new(1.0, 1.0, 1.0)); if b.contains(e.position) { let take = (max - carried).min(e.stack.count as i32); + let (item, id) = (e.clone(), self.mobs[i].id); + self.add_pickup( + super::pickups::Taken::Item(Box::new(item)), + super::pickups::Collector::Mob(id), + ); self.mobs[i].extra.ints[1] += take; let e = &mut self.dropped[j]; e.stack.count -= take as u32; diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 8c749b704..7e2d20ecb 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -1434,6 +1434,7 @@ impl Game<'_> { // Player.hasInfiniteMaterials. let creative = self.creative; let mut projectiles = std::mem::take(&mut self.creatures.projectiles); + let mut picked = Vec::new(); projectiles.retain(|shot| { let Some(a) = shot.arrow.as_ref().filter(|a| !a.removed) else { return true; @@ -1461,10 +1462,17 @@ impl Game<'_> { 0.2, pitch, ); + picked.push(shot.clone()); } !taken }); self.creatures.projectiles = projectiles; + for shot in picked { + self.add_pickup( + super::pickups::Taken::Arrow(Box::new(shot)), + super::pickups::Collector::Player, + ); + } } fn tick_projectiles(&mut self) -> Result<()> { @@ -2672,12 +2680,21 @@ impl Game<'_> { ); let right = forward.cross(glam::Vec3::Y).normalize_or_zero(); let up = right.cross(forward).normalize_or_zero(); - for a in &self.creatures.projectiles { - let p = glam::DVec3::new( + let live = self.creatures.projectiles.iter().map(|a| { + let at = [ a.previous.x + (a.position.x - a.previous.x) * alpha, a.previous.y + (a.position.y - a.previous.y) * alpha, a.previous.z + (a.position.z - a.previous.z) * alpha, - ) - camera; + ]; + (a, at) + }); + // ItemPickupParticle: a picked-up arrow flying to its collector. + let taken = self.pickups.live.iter().filter_map(|p| match &p.taken { + super::pickups::Taken::Arrow(a) => Some((&**a, super::pickups::position(p, alpha))), + _ => None, + }); + for (a, at) in live.chain(taken) { + let p = glam::DVec3::from_array(at) - camera; let c = p.as_vec3(); let light = self.world.light( a.position.x.floor() as i32, diff --git a/crates/horizon-client/src/game/item_draw.rs b/crates/horizon-client/src/game/item_draw.rs index a30a02127..72dd7ea59 100644 --- a/crates/horizon-client/src/game/item_draw.rs +++ b/crates/horizon-client/src/game/item_draw.rs @@ -310,13 +310,25 @@ impl Game<'_> { alpha: f64, out: &mut Vec, ) { - for item in &self.dropped { + let live = self.dropped.iter().map(|item| { let (p, q) = (item.previous, item.position); - let pos = glam::DVec3::new( + let at = [ p.x + (q.x - p.x) * alpha, p.y + (q.y - p.y) * alpha, p.z + (q.z - p.z) * alpha, - ) - camera; + ]; + (item, at, alpha as f32) + }); + // ItemPickupParticle: the render state extracted at partial tick 1. + let taken = self.pickups.live.iter().filter_map(|p| match &p.taken { + super::pickups::Taken::Item(item) => { + Some((&**item, super::pickups::position(p, alpha), 1.0)) + } + _ => None, + }); + for (item, at, partial) in live.chain(taken) { + let q = item.position; + let pos = glam::DVec3::from_array(at) - camera; if pos.length_squared() > 64.0 * 64.0 { continue; } @@ -331,7 +343,7 @@ impl Game<'_> { if state.is_empty() { continue; } - let age = item.age as f32 + alpha as f32; + let age = item.age as f32 + partial; let (lo, _) = state.bounding_box(); let min_offset_y = -lo.y + 0.0625; let bob = (age / 10.0 + item.bob_offset).sin() * 0.1 + 0.1; diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 577bc5777..293afdcdd 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -748,10 +748,12 @@ impl Game<'_> { let rest = self.as_guest(host, conn, |g| g.give(stack)).flatten(); match rest { None => { - self.dropped.remove(i); + let item = self.dropped.remove(i); + self.guest_pickup(host, id, item); taken.push((item_id, before)); } Some(rest) if rest.count < before => { + self.guest_pickup(host, id, self.dropped[i].clone()); self.dropped[i].stack = rest; taken.push((item_id, before - self.dropped[i].stack.count)); } @@ -765,6 +767,23 @@ impl Game<'_> { } } + /// The host's own `ItemPickupParticle` toward the guest's model. + fn guest_pickup( + &mut self, + host: &LanHost, + id: i32, + item: horizon_client::entities::ItemEntity, + ) { + use super::pickups::{Collector, Taken, guest_target}; + if let Some(target) = guest_target(host, id as u32) { + self.add_pickup_toward( + Taken::Item(Box::new(item)), + Collector::Guest(id as u32), + target, + ); + } + } + /// Positional sounds and level events of the tick, for the guests. pub(super) fn lan_broadcast_effects(&mut self, host: &mut LanHost) { let sounds = self.audio.capture.take().unwrap_or_default(); diff --git a/crates/horizon-client/src/game/pickups.rs b/crates/horizon-client/src/game/pickups.rs new file mode 100644 index 000000000..2729dc98f --- /dev/null +++ b/crates/horizon-client/src/game/pickups.rs @@ -0,0 +1,185 @@ +//! `ItemPickupParticle`: a taken item entity, experience orb or arrow flies +//! into whoever took it over three ticks (`ClientPacketListener.handleTakeItemEntity` +//! adds one for every `ClientboundTakeItemEntityPacket`), drawn with the render +//! state it had when it was taken (`extractEntity(from, 1.0F)`). +use super::Game; +use horizon_client::{ + entities::{ExperienceOrb, ItemEntity}, + projectiles::Projectile, +}; + +/// `ItemPickupParticle.LIFE_TIME`. +const LIFE_TIME: u32 = 3; + +/// The taken entity, as it was when taken. +pub(super) enum Taken { + Item(Box), + Orb(Box), + Arrow(Box), +} +impl Taken { + /// The render state's position: the entity's at partial tick 1. + fn position(&self) -> [f64; 3] { + let p = match self { + Taken::Item(i) => i.position, + Taken::Orb(o) => o.position, + Taken::Arrow(a) => a.position, + }; + [p.x, p.y, p.z] + } +} + +/// Whoever took it: the particle's `target`. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub(super) enum Collector { + Player, + /// A mob, or another player on a server (both kept as mobs here). + Mob(u32), + /// A LAN guest, by entity id (the host draws it as a model). + Guest(u32), +} + +pub(super) struct Pickup { + pub taken: Taken, + collector: Collector, + life: u32, + target: [f64; 3], + target_old: [f64; 3], +} + +/// `ParticleEngine`'s ITEM_PICKUP group and the particles waiting to join it. +#[derive(Default)] +pub(super) struct Pickups { + pub live: Vec, + added: Vec, +} +impl Pickups { + pub fn clear(&mut self) { + self.live.clear(); + self.added.clear(); + } +} + +/// `ItemPickupParticleGroup.ParticleInstance.fromParticle`: the position at +/// `partial`, from the taken entity's to the collector's (lerped between ticks) +/// along the square of the particle's age. +pub(super) fn position(p: &Pickup, partial: f64) -> [f64; 3] { + let time = (p.life as f32 + partial as f32) / LIFE_TIME as f32; + let time = f64::from(time * time); + let from = p.taken.position(); + std::array::from_fn(|i| { + let target = p.target_old[i] + partial * (p.target[i] - p.target_old[i]); + from[i] + time * (target - from[i]) + }) +} + +/// A LAN guest's target, from the model the host draws for it. +pub(super) fn guest_target(host: &super::lan::LanHost, id: u32) -> Option<[f64; 3]> { + let m = host.models.values().find(|m| m.id == id)?; + let (p, eye) = (m.position, m.eye_height()); + Some([p.x, (p.y + (p.y + eye)) / 2.0, p.z]) +} + +impl Game<'_> { + /// `ItemPickupParticle.updatePosition`: the collector's x and z, halfway up to its eyes. + fn collector_target(&self, collector: Collector) -> Option<[f64; 3]> { + let (p, eye) = match collector { + Collector::Player => { + let p = self.player.as_ref()?; + (p.position, f64::from(p.dimensions.eye_height)) + } + Collector::Mob(id) => { + let m = self.mobs.iter().find(|m| m.id == id)?; + (m.position, m.eye_height()) + } + Collector::Guest(id) => { + return guest_target(self.lan.as_ref()?, id); + } + }; + Some([p.x, (p.y + (p.y + eye)) / 2.0, p.z]) + } + /// `handleTakeItemEntity`: `particleEngine.add(new ItemPickupParticle(...))` when the + /// collector is known; it joins the group at the end of the next particle tick. + pub(super) fn add_pickup(&mut self, taken: Taken, collector: Collector) { + if let Some(target) = self.collector_target(collector) { + self.add_pickup_toward(taken, collector, target); + } + } + /// [`Self::add_pickup`] with the collector's first target given (a LAN + /// guest's model while the host is taken out for its tick). + pub(super) fn add_pickup_toward( + &mut self, + taken: Taken, + collector: Collector, + target: [f64; 3], + ) { + self.pickups.added.push(Pickup { + taken, + collector, + life: 0, + target, + target_old: target, + }); + } + /// `ParticleEngine.tick` for the ITEM_PICKUP group: age (removed at + /// `LIFE_TIME`), follow the collector, then let the new particles in. + pub(super) fn tick_pickups(&mut self) { + let mut live = std::mem::take(&mut self.pickups.live); + live.retain_mut(|p| { + p.life += 1; + if p.life == LIFE_TIME { + return false; + } + p.target_old = p.target; + if let Some(t) = self.collector_target(p.collector) { + p.target = t; + } + true + }); + live.append(&mut self.pickups.added); + self.pickups.live = live; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn orb_at(x: f64, y: f64, z: f64) -> Taken { + let at = horizon_core::geometry::Vec3::new(x, y, z); + Taken::Orb(Box::new(ExperienceOrb { + position: at, + previous: at, + velocity: horizon_core::geometry::Vec3::new(0.0, 0.0, 0.0), + value: 1, + age: 0, + on_ground: false, + })) + } + + #[test] + fn flies_along_the_square_of_its_age_to_the_moving_target() { + let mut p = Pickup { + taken: orb_at(0.0, 64.0, 0.0), + collector: Collector::Player, + life: 0, + target: [3.0, 65.0, 0.0], + target_old: [3.0, 65.0, 0.0], + }; + // Added this tick: still at the orb. + assert_eq!(position(&p, 0.0), [0.0, 64.0, 0.0]); + // Halfway through the first tick: (0.5 / 3)^2 of the way. + let t = (0.5f32 / 3.0) * (0.5 / 3.0); + let mid = position(&p, 0.5); + assert!((mid[0] - 3.0 * f64::from(t)).abs() < 1e-9, "{mid:?}"); + // Last tick before removal, the target moved: lerped between its ticks. + p.life = 2; + p.target_old = [3.0, 65.0, 0.0]; + p.target = [4.0, 65.0, 0.0]; + let end = position(&p, 1.0); + assert!( + (end[0] - 4.0).abs() < 1e-9 && (end[1] - 65.0).abs() < 1e-9, + "{end:?}" + ); + } +} diff --git a/crates/horizon-client/src/game/piglins.rs b/crates/horizon-client/src/game/piglins.rs index c51941d02..3515b380b 100644 --- a/crates/horizon-client/src/game/piglins.rs +++ b/crates/horizon-client/src/game/piglins.rs @@ -64,6 +64,11 @@ impl Game<'_> { } // Nuggets come whole, anything else one at a time. let item = e.stack.item.clone(); + let (taken_item, id) = (e.clone(), self.mobs[i].id); + self.add_pickup( + super::pickups::Taken::Item(Box::new(taken_item)), + super::pickups::Collector::Mob(id), + ); let taken = if &*item == "minecraft:gold_nugget" { self.dropped.remove(j).stack.count } else { diff --git a/crates/horizon-client/src/game/raids.rs b/crates/horizon-client/src/game/raids.rs index ecb173822..1f1fa3507 100644 --- a/crates/horizon-client/src/game/raids.rs +++ b/crates/horizon-client/src/game/raids.rs @@ -770,6 +770,7 @@ impl Game<'_> { .map(|(i, e)| (i, e.position)) .collect(); let mut taken: Vec = Vec::new(); + let mut collectors: Vec<(usize, u32)> = Vec::new(); let mut recruit: Vec<(i32, Vec3)> = Vec::new(); let mut joins: Vec<(u32, i32, f32)> = Vec::new(); let mut new_leaders: Vec<(i32, i32, u32)> = Vec::new(); @@ -868,6 +869,7 @@ impl Game<'_> { if p.distance_squared(position) < 1.414 * 1.414 { // pickUpItem: the banner becomes the wave's leader. taken.push(*i); + collectors.push((*i, m.id)); leaders.insert((id, s.wave)); new_leaders.push((id, s.wave, m.id)); s.banner_at = None; @@ -878,6 +880,13 @@ impl Game<'_> { } } } + for (i, mob) in collectors { + let item = self.dropped[i].clone(); + self.add_pickup( + super::pickups::Taken::Item(Box::new(item)), + super::pickups::Collector::Mob(mob), + ); + } taken.sort_unstable(); for i in taken.into_iter().rev() { self.dropped.remove(i); diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index bafb71f9e..7156f37c6 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -186,6 +186,7 @@ impl Game<'_> { self.creatures.projectiles.clear(); self.dropped.clear(); self.particles.clear(); + self.pickups.clear(); self.orbs.clear(); self.falling.clear(); self.travelling = None; @@ -278,6 +279,7 @@ impl Game<'_> { self.mobs.clear(); self.dropped.clear(); self.particles.clear(); + self.pickups.clear(); self.score.board = Default::default(); self.tab_list = Default::default(); self.border = Default::default(); @@ -469,6 +471,7 @@ impl Game<'_> { self.mobs.clear(); self.dropped.clear(); self.particles.clear(); + self.pickups.clear(); if let Some(r) = self.remote.as_mut() { r.entities.clear(); r.lerps.clear(); @@ -982,6 +985,19 @@ impl Game<'_> { self.audio .play("minecraft:entity.item.pickup", Some(eye), 0.2, pitch); } + // ItemPickupParticle toward the collector (this player, another one or a mob). + if let Some(item) = self.dropped.iter().find(|d| d.id == p.item as u32).cloned() { + let collector = if self + .remote + .as_ref() + .is_some_and(|r| r.entity_id == p.player) + { + super::pickups::Collector::Player + } else { + super::pickups::Collector::Mob(p.player as u32) + }; + self.add_pickup(super::pickups::Taken::Item(Box::new(item)), collector); + } self.remove_entity(p.item); } C::HurtAnimation(p) => { diff --git a/crates/horizon-client/src/game/render_tick.rs b/crates/horizon-client/src/game/render_tick.rs index 4c916e773..46255667a 100644 --- a/crates/horizon-client/src/game/render_tick.rs +++ b/crates/horizon-client/src/game/render_tick.rs @@ -331,6 +331,7 @@ impl Game<'_> { .map(|p| ([p.position.x, p.position.y, p.position.z], p.velocity.y)), }; self.particles.tick(&level); + self.tick_pickups(); for (name, at, volume, pitch) in std::mem::take(&mut self.particles.sounds) { self.audio.play(name, Some(at), volume, pitch); } diff --git a/crates/horizon-client/src/game/sulfur_cubes.rs b/crates/horizon-client/src/game/sulfur_cubes.rs index 5a8ae361f..4606f6859 100644 --- a/crates/horizon-client/src/game/sulfur_cubes.rs +++ b/crates/horizon-client/src/game/sulfur_cubes.rs @@ -126,6 +126,11 @@ impl Game<'_> { continue; } let item = e.stack.item.clone(); + let (taken_item, id) = (e.clone(), self.mobs[i].id); + self.add_pickup( + super::pickups::Taken::Item(Box::new(taken_item)), + super::pickups::Collector::Mob(id), + ); let e = &mut self.dropped[j]; e.stack.count -= 1; if e.stack.count == 0 { diff --git a/crates/horizon-client/src/game/villagers.rs b/crates/horizon-client/src/game/villagers.rs index 0d547c0b0..72fa2839a 100644 --- a/crates/horizon-client/src/game/villagers.rs +++ b/crates/horizon-client/src/game/villagers.rs @@ -293,7 +293,15 @@ impl Game<'_> { j += 1; continue; } + let before = self.dropped[j].clone(); let rest = villager::ext_mut(&mut self.mobs[i]).add_item(stack, max); + if rest.as_ref().is_none_or(|r| r.count < before.stack.count) { + let id = self.mobs[i].id; + self.add_pickup( + super::pickups::Taken::Item(Box::new(before)), + super::pickups::Collector::Mob(id), + ); + } match rest { None => { self.dropped.remove(j);