diff --git a/crates/horizon-client/src/game/fireworks.rs b/crates/horizon-client/src/game/fireworks.rs index 85fccc2de..7838c9590 100644 --- a/crates/horizon-client/src/game/fireworks.rs +++ b/crates/horizon-client/src/game/fireworks.rs @@ -25,9 +25,15 @@ pub(super) struct Rocket { pub(super) velocity: Vec3, life: i32, lifetime: i32, - explosions: Vec, + pub(super) explosions: Vec, /// Boosting the player (attachedToEntity). pub(super) attached: bool, + /// On a server, the player it boosts when not this game's own: a guest's + /// connection. + pub(super) guest: Option, + /// `DATA_ATTACHED_TO_TARGET`: the server entity the rocket follows; + /// boosting happens when it is the local player. + pub(super) attached_to: Option, /// `DATA_SHOT_AT_ANGLE` (dispensers): constant velocity, exploding on /// the first block it hits. pub(super) angled: bool, @@ -48,6 +54,8 @@ impl Rocket { lifetime: i32::MAX, explosions, attached: false, + guest: None, + attached_to: None, angled: false, item, } @@ -63,7 +71,7 @@ impl Rocket { (self.position, self.velocity) } } -fn fireworks_of(stack: &ItemStack) -> (u8, Vec) { +pub(super) fn fireworks_of(stack: &ItemStack) -> (u8, Vec) { // The item's default component: flight 1, no explosions. stack .data() @@ -89,6 +97,8 @@ impl Game<'_> { lifetime, explosions, attached, + guest: None, + attached_to: None, angled: false, item: stack.clone(), } @@ -127,7 +137,8 @@ impl Game<'_> { return false; } let at = self.player.as_ref().map_or(Vec3::ZERO, |p| p.position); - let rocket = self.new_rocket(at, stack, true); + let mut rocket = self.new_rocket(at, stack, true); + rocket.guest = self.lan_acting; self.rockets.push(rocket); if !self.creative { self.consume_held(); @@ -140,7 +151,18 @@ impl Game<'_> { while i < self.rockets.len() { let mut r = self.rockets.remove(i); r.previous = r.position; - if r.attached { + if r.attached + && let Some(conn) = r.guest + { + // A guest's rocket follows them; their own client boosts them. + let guest = self + .lan + .as_ref() + .and_then(|host| host.hub.guests.iter().find(|g| g.conn == conn)); + if let Some(g) = guest { + r.position = Vec3::new(g.position[0], g.position[1], g.position[2]); + } + } else if r.attached { if let Some(p) = self.player.as_mut() { if p.fall_flying { let look = view_vector(self.yaw, self.pitch); @@ -229,8 +251,19 @@ impl Game<'_> { let at = [r.position.x, r.position.y, r.position.z]; if !r.explosions.is_empty() { let damage = 5.0 + r.explosions.len() as f32 * 2.0; + // The player it boosts takes the full damage, a guest as well. if r.attached { - self.hurt_player_by("minecraft:fireworks", damage); + match r.guest { + None => self.hurt_player_by("minecraft:fireworks", damage), + Some(conn) => { + if let Some(mut host) = self.lan.take() { + self.as_guest(&mut host, conn, |g| { + g.hurt_player_by("minecraft:fireworks", damage); + }); + self.lan = Some(host); + } + } + } } let from = r.position; let hits: Vec<(u32, f32)> = { diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index ef07fa20a..9d4d868e0 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -275,6 +275,8 @@ impl Game<'_> { motion.sprinting = g.sprinting; motion.may_fly = g.creative() || g.game_mode == SPECTATOR; motion.flying = g.flying; + // FireworkRocketItem.use boosts only a gliding player. + motion.fall_flying = g.fall_flying; motion.invulnerable = motion.may_fly; let _ = motion.set_health(g.health, false); motion.health.food.food = g.food; diff --git a/crates/horizon-client/src/game/lan_entities.rs b/crates/horizon-client/src/game/lan_entities.rs index dd272719a..cd4ba2fb8 100644 --- a/crates/horizon-client/src/game/lan_entities.rs +++ b/crates/horizon-client/src/game/lan_entities.rs @@ -404,11 +404,19 @@ impl Game<'_> { Value::Item(hub.stack(&Some(r.item.clone()))), )); } + let boosted = match r.guest { + Some(conn) => hub + .guests + .iter() + .find(|g| g.conn == conn) + .map(|g| g.entity_id), + None => host_entity, + }; if r.attached - && let Some(host) = host_entity + && let Some(player) = boosted { e.meta - .push((9, serializer::OPTIONAL_UNSIGNED_INT, Value::Id(host + 1))); + .push((9, serializer::OPTIONAL_UNSIGNED_INT, Value::Id(player + 1))); } if r.angled { e.meta.push((10, serializer::BOOLEAN, Value::Bool(true))); diff --git a/crates/horizon-client/src/game/remote_projectiles.rs b/crates/horizon-client/src/game/remote_projectiles.rs index bc6d0b13e..5c494b9ae 100644 --- a/crates/horizon-client/src/game/remote_projectiles.rs +++ b/crates/horizon-client/src/game/remote_projectiles.rs @@ -2,7 +2,7 @@ //! end crystals on a server's client (`ClientPacketListener.handleAddEntity` //! and `handleSetEntityData`): the server simulates them, the client draws //! them where it says and interpolates their moves. -use super::{FallStep, FallingBlock, Game}; +use super::{FallStep, FallingBlock, Game, fireworks::fireworks_of, view_vector}; use horizon_client::projectiles::{Projectile, ProjectileKind}; use horizon_core::{geometry::Vec3, position::BlockPos}; use horizon_protocol::entity_data::Value; @@ -125,6 +125,37 @@ impl Game<'_> { pub(super) fn remote_projectile_data(&mut self, id: i32, values: &[(u8, i32, Value)]) { let id = id as u32; self.remote_hook_data(id, values); + if self.rockets.iter().any(|r| r.id == id) { + for (index, _, value) in values { + match (index, value) { + // FireworkRocketEntity.DATA_ID_FIREWORKS_ITEM: its + // explosions for `createFireworks`. + (8, Value::Item(stack)) => { + if let Some(stack) = self.to_stack(stack) + && let Some(r) = self.rockets.iter_mut().find(|r| r.id == id) + { + let (_, explosions) = fireworks_of(&stack); + r.explosions = explosions; + r.item = stack; + } + } + // DATA_ATTACHED_TO_TARGET (OptionalInt: the id plus 1). + (9, Value::Id(v)) => { + let v = (*v > 0).then(|| (*v - 1) as u32); + if let Some(r) = self.rockets.iter_mut().find(|r| r.id == id) { + r.attached_to = v; + } + } + // DATA_SHOT_AT_ANGLE. + (10, Value::Bool(a)) => { + if let Some(r) = self.rockets.iter_mut().find(|r| r.id == id) { + r.angled = *a; + } + } + _ => {} + } + } + } if self.potions.has_thrown(id) { // ThrowableItemProjectile.DATA_ITEM_STACK: the potion it is. for (index, _, value) in values { @@ -213,9 +244,30 @@ impl Game<'_> { let id = t.id; t.remote_step(|at| step(id, at)); } + // FireworkRocketEntity.tick: a rocket attached to the gliding local + // player boosts it client-side too (attachedToEntity.isFallFlying). + let me = self + .remote + .as_ref() + .and_then(|r| u32::try_from(r.entity_id).ok()); for r in &mut self.rockets { let id = r.id; r.remote_step(|at| step(id, at)); + if me.is_some() + && r.attached_to == me + && let Some(p) = self.player.as_mut() + && p.fall_flying + { + let look = view_vector(self.yaw, self.pitch); + let m = p.velocity; + p.velocity = Vec3::new( + m.x + look[0] * 0.1 + (look[0] * 1.5 - m.x) * 0.5, + m.y + look[1] * 0.1 + (look[1] * 1.5 - m.y) * 0.5, + m.z + look[2] * 0.1 + (look[2] * 1.5 - m.z) * 0.5, + ); + r.position = p.position; + r.velocity = p.velocity; + } } for c in &mut self.crystals { c.time += 1; diff --git a/crates/horizon-client/src/item_sync.rs b/crates/horizon-client/src/item_sync.rs index cf37479bd..930304ec4 100644 --- a/crates/horizon-client/src/item_sync.rs +++ b/crates/horizon-client/src/item_sync.rs @@ -87,6 +87,14 @@ fn components_tag(s: &Stack, builtin: &Builtin, synced: &Dynamic) -> Option ("pages", Tag::List(b.pages.clone())), ("resolved", Tag::Byte(i8::from(b.resolved))), ]), + Component::Fireworks { flight, explosions } => Tag::compound([ + ("flight_duration", Tag::Byte(*flight as i8)), + ( + "explosions", + Tag::List(explosions.iter().map(fireworks_tag).collect()), + ), + ]), + Component::FireworkExplosion(e) => fireworks_tag(e), Component::Enchantments(list) => { // Enchantments are a synced (data-driven) registry: the // ids index the server's RegistryData, not the builtin @@ -178,6 +186,29 @@ fn components_tag(s: &Stack, builtin: &Builtin, synced: &Dynamic) -> Option Some(Tag::compound(entries)) } +/// A wire `FireworkExplosion` as vanilla's NBT codec writes it +/// (`FireworkExplosion.Shape`'s BY_ID order). +fn fireworks_tag(e: &horizon_protocol::item::FireworkExplosion) -> Tag { + const SHAPES: [&str; 5] = ["small_ball", "large_ball", "star", "creeper", "burst"]; + let ints = |c: &[i32]| Tag::List(c.iter().map(|c| Tag::Int(*c)).collect()); + Tag::compound([ + ( + "shape", + Tag::String( + SHAPES + .get(e.shape.max(0) as usize) + .copied() + .unwrap_or("small_ball") + .into(), + ), + ), + ("colors", ints(&e.colors)), + ("fade_colors", ints(&e.fade)), + ("has_trail", Tag::Byte(i8::from(e.trail))), + ("has_twinkle", Tag::Byte(i8::from(e.twinkle))), + ]) +} + /// A `minecraft:profile` component as the item data JSON the model reads. fn profile_tag(p: &Profile) -> Tag { let properties = p diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index d729a2625..aa1994257 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -445,6 +445,43 @@ impl Guest { } } +/// `FireworkExplosion.STREAM_CODEC`'s fields (`Shape` by its BY_ID order). +fn network_explosion( + e: &crate::particles::FireworkExplosion, +) -> horizon_protocol::item::FireworkExplosion { + horizon_protocol::item::FireworkExplosion { + shape: e.shape as i32, + colors: e.colors.iter().map(|c| *c as i32).collect(), + fade: e.fade.iter().map(|c| *c as i32).collect(), + trail: e.trail, + twinkle: e.twinkle, + } +} + +/// A rocket's `minecraft:fireworks` and a star's `minecraft:firework_explosion`. +pub fn firework_components( + d: &crate::item_data::StackData, +) -> Vec<(i32, horizon_protocol::item::Component)> { + use horizon_protocol::item::{Component, component}; + let mut out = Vec::new(); + if let Some((flight, explosions)) = &d.fireworks { + out.push(( + component::FIREWORKS, + Component::Fireworks { + flight: i32::from(*flight), + explosions: explosions.iter().map(network_explosion).collect(), + }, + )); + } + if let Some(e) = &d.firework_explosion { + out.push(( + component::FIREWORK_EXPLOSION, + Component::FireworkExplosion(network_explosion(e)), + )); + } + out +} + /// The off hand slot is conventionally 40 in saves and 45 in the menu. /// `synced_id` names an entry of a synced registry (enchantments, trim /// materials and patterns) by its id on this connection. @@ -535,6 +572,7 @@ fn network_stack( horizon_protocol::item::Component::Int(i32::from(mode)), )); } + stack.added.extend(firework_components(d)); // PotionContents: the potion by its `minecraft:potion` id. if let Some(id) = (d.potion.as_deref()).and_then(|potion| { synced_id( diff --git a/crates/horizon-protocol/src/item.rs b/crates/horizon-protocol/src/item.rs index 2c04b5c61..abc530278 100644 --- a/crates/horizon-protocol/src/item.rs +++ b/crates/horizon-protocol/src/item.rs @@ -39,6 +39,8 @@ pub mod component { pub const WRITABLE_BOOK_CONTENT: i32 = 56; pub const WRITTEN_BOOK_CONTENT: i32 = 57; pub const TRIM: i32 = 58; + pub const FIREWORK_EXPLOSION: i32 = 70; + pub const FIREWORKS: i32 = 71; } /// A written book's content (`WrittenBookContent.STREAM_CODEC`): the @@ -89,6 +91,13 @@ pub enum Component { WrittenBook(WrittenBook), /// `ChargedProjectiles`: a loaded crossbow's ammunition, as templates. Stacks(Vec), + /// `Fireworks`: flight duration and its explosions. + Fireworks { + flight: i32, + explosions: Vec, + }, + /// `FireworkExplosion` (a firework star). + FireworkExplosion(FireworkExplosion), /// `ArmorTrim` naming its material and pattern by registry id (`trim_material`, /// `trim_pattern`); a trim with a direct holder stays [`Component::Raw`]. Trim { @@ -100,6 +109,41 @@ pub enum Component { Raw(Vec), } +/// `FireworkExplosion.STREAM_CODEC`: shape id, colors, fade colors, trail +/// and twinkle (FireworkExplosion.Shape's BY_ID order). +#[derive(Clone, Debug, PartialEq)] +pub struct FireworkExplosion { + pub shape: i32, + pub colors: Vec, + pub fade: Vec, + pub trail: bool, + pub twinkle: bool, +} +impl FireworkExplosion { + fn read(r: &mut Reader) -> Result { + let ints = |r: &mut Reader| -> Result> { + let n = r.count(1024)?; + (0..n).map(|_| r.i32()).collect() + }; + Ok(Self { + shape: r.varint()?, + colors: ints(r)?, + fade: ints(r)?, + trail: r.bool()?, + twinkle: r.bool()?, + }) + } + fn write(&self, w: &mut Writer) { + w.varint(self.shape); + w.varint(self.colors.len() as i32); + self.colors.iter().for_each(|c| w.i32(*c)); + w.varint(self.fade.len() as i32); + self.fade.iter().for_each(|c| w.i32(*c)); + w.bool(self.trail); + w.bool(self.twinkle); + } +} + /// `Filterable.streamCodec`: the raw value, then an optional filtered one /// (dropped). fn filterable(r: &mut Reader, value: impl Fn(&mut Reader) -> Result) -> Result { @@ -260,6 +304,17 @@ impl Component { hidden: (0..n).map(|_| r.varint()).collect::>()?, } } + FIREWORKS => { + let flight = r.varint()?; + let n = r.count(256)?; + Self::Fireworks { + flight, + explosions: (0..n) + .map(|_| FireworkExplosion::read(r)) + .collect::>()?, + } + } + FIREWORK_EXPLOSION => Self::FireworkExplosion(FireworkExplosion::read(r)?), // ArmorTrim.STREAM_CODEC: two holders, each a reference's id plus // one. A direct material or pattern has no registry name: read past. TRIM => { @@ -375,6 +430,12 @@ impl Component { w.varint(stacks.len() as i32); stacks.iter().for_each(|s| Template::write(s, w)); } + Self::Fireworks { flight, explosions } => { + w.varint(*flight); + w.varint(explosions.len() as i32); + explosions.iter().for_each(|e| e.write(w)); + } + Self::FireworkExplosion(e) => e.write(w), Self::WritableBook(pages) => { w.varint(pages.len() as i32); for page in pages { @@ -670,6 +731,44 @@ mod tests { round::, HashedSlot>(None); } + #[test] + fn fireworks_components_round_trip() { + let mut rocket = Stack::new(42, 1); + rocket.added.push(( + component::FIREWORKS, + Component::Fireworks { + flight: 3, + explosions: vec![ + FireworkExplosion { + shape: 4, + colors: vec![0xff0000, 0x00ff00], + fade: vec![0x0000ff], + trail: true, + twinkle: false, + }, + FireworkExplosion { + shape: 0, + colors: vec![], + fade: vec![], + trail: false, + twinkle: true, + }, + ], + }, + )); + rocket.added.push(( + component::FIREWORK_EXPLOSION, + Component::FireworkExplosion(FireworkExplosion { + shape: 2, + colors: vec![7], + fade: vec![], + trail: false, + twinkle: false, + }), + )); + round::, Slot>(Some(rocket)); + } + #[test] fn charged_projectiles_are_item_stack_templates() { // A crossbow (id 7) loaded with one arrow (id 9): ChargedProjectiles