//! Firework rockets (`FireworkRocketItem`, `FireworkRocketEntity`): launching //! from a block face, boosting a gliding player, the 1.15 horizontal //! acceleration and 0.04 lift, the flight-duration lifetime, explosion //! damage with line of sight, and the client explosion (`createFireworks`). use super::{Game, Hit, view_vector}; use horizon_client::{ collisions::SceneCollisions, items::ItemStack, mesher::Vertex, mobs::{Damage, DamageKind}, particles::{FireworkExplosion, ParticleOptions, ParticleType}, }; use horizon_core::{ geometry::{Aabb, Vec3}, random::RandomSource, }; pub(super) struct Rocket { position: Vec3, previous: Vec3, velocity: Vec3, life: i32, lifetime: i32, explosions: Vec, /// Boosting the player (attachedToEntity). attached: bool, /// `DATA_SHOT_AT_ANGLE` (dispensers): constant velocity, exploding on /// the first block it hits. angled: bool, icon: Option, } impl Rocket { /// Position and motion (the dispenser parity fixture). pub(super) fn motion(&self) -> (Vec3, Vec3) { (self.position, self.velocity) } } fn fireworks_of(stack: &ItemStack) -> (u8, Vec) { // The item's default component: flight 1, no explosions. stack .data() .and_then(|d| d.fireworks.clone()) .unwrap_or((1, Vec::new())) } impl Game<'_> { pub(super) fn new_rocket(&mut self, at: Vec3, stack: &ItemStack, attached: bool) -> Rocket { let (flight, explosions) = fireworks_of(stack); // FireworkRocketEntity: motion and lifetime come from the rocket's own random. let r = &mut super::dispenser::entity_random(); let tri = |r: &mut horizon_core::random::LegacyRandom| r.triangle(0.0, 0.002297); let velocity = Vec3::new(tri(r), 0.05, tri(r)); let lifetime = 10 * (1 + i32::from(flight)) + r.next_int_bound(6).unwrap_or(0) + r.next_int_bound(7).unwrap_or(0); Rocket { position: at, previous: at, velocity, life: 0, lifetime, explosions, attached, angled: false, icon: Some("item/firework_rocket".into()), } } /// `FireworkRocketItem.useOn`: launch from the clicked point. pub(super) fn launch_firework(&mut self, hit: &Hit, stack: &ItemStack) -> bool { if &*stack.item != "minecraft:firework_rocket" { return false; } let at = Vec3::new( hit.point[0] + f64::from(hit.face[0]) * 0.15, hit.point[1] + f64::from(hit.face[1]) * 0.15, hit.point[2] + f64::from(hit.face[2]) * 0.15, ); let rocket = self.new_rocket(at, stack, false); self.rockets.push(rocket); if !self.creative { self.consume_held(); } self.swing_arm(); true } /// `FireworkRocketItem.asProjectile` shot out of a dispenser. pub(super) fn launch_dispensed_rocket(&mut self, at: Vec3, velocity: Vec3, stack: &ItemStack) { let mut rocket = self.new_rocket(at, stack, false); rocket.velocity = velocity; rocket.angled = true; self.rockets.push(rocket); } /// `FireworkRocketItem.use` while gliding: a rocket attached to the player. pub(super) fn boost_firework(&mut self, stack: &ItemStack) -> bool { if &*stack.item != "minecraft:firework_rocket" || !self.player.as_ref().is_some_and(|p| p.fall_flying) { return false; } let at = self.player.as_ref().map_or(Vec3::ZERO, |p| p.position); let rocket = self.new_rocket(at, stack, true); self.rockets.push(rocket); if !self.creative { self.consume_held(); } true } pub(super) fn tick_rockets(&mut self) { let mut i = 0; while i < self.rockets.len() { let mut r = self.rockets.remove(i); r.previous = r.position; if r.attached { if let Some(p) = self.player.as_mut() { if 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; } } else { // Not shot at an angle: accelerate (unless blocked sideways) and rise. let mut scene = SceneCollisions { world: &self.world, binding: self.collision, subject: None, }; let h = 0.125; let bounds = Aabb::new( Vec3::new(r.position.x - h, r.position.y, r.position.z - h), Vec3::new(r.position.x + h, r.position.y + 2.0 * h, r.position.z + h), ); let wanted = if r.angled { r.velocity } else { Vec3::new( r.velocity.x * 1.15, r.velocity.y + 0.04, r.velocity.z * 1.15, ) }; if let Ok(moved) = scene.resolve(bounds, wanted, 0.0, false) { let blocked = (moved.x - wanted.x).abs() > 1e-7 || (moved.y - wanted.y).abs() > 1e-7 || (moved.z - wanted.z).abs() > 1e-7; if r.angled && blocked { // hitTargetOrDeflectSelf: explode on the block. r.life = r.lifetime; } let horizontal = (moved.x - wanted.x).abs() > 1e-7 || (moved.z - wanted.z).abs() > 1e-7; r.position = Vec3::new( r.position.x + moved.x, r.position.y + moved.y, r.position.z + moved.z, ); r.velocity = if horizontal { Vec3::new(r.velocity.x, wanted.y, r.velocity.z) } else { wanted }; } } let at = [r.position.x, r.position.y, r.position.z]; if r.life == 0 { self.audio.play( "minecraft:entity.firework_rocket.launch", Some(at), 3.0, 1.0, ); } r.life += 1; // The trail: a spark each tick drifting against the rocket's motion. let g = |g: &mut horizon_core::random::LegacyRandom| g.next_gaussian() * 0.05; let v = [ g(&mut self.world.random), -r.velocity.y * 0.5, g(&mut self.world.random), ]; self.particles .add(&ParticleOptions::simple(ParticleType::Firework), at, v); if r.life > r.lifetime { self.explode_rocket(&r); continue; } self.rockets.insert(i, r); i += 1; } } /// `FireworkRocketEntity.explode` and `dealExplosionDamage`. fn explode_rocket(&mut self, r: &Rocket) { 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; if r.attached { self.hurt_player_by("minecraft:fireworks", damage); } let from = r.position; let hits: Vec<(u32, f32)> = { let world = self.mob_world_base(&[]); self.mobs .iter() .filter(|m| m.alive()) .filter_map(|m| { let d2 = (m.position.x - from.x).powi(2) + (m.position.y - from.y).powi(2) + (m.position.z - from.z).powi(2); // Line of sight to the feet or the middle of the mob. let sees = d2 <= 25.0 && (0..2).any(|step| { let to = Vec3::new( m.position.x, m.position.y + f64::from(m.height()) * 0.5 * f64::from(step), m.position.z, ); world.clear_between(from, to, false) }); sees.then(|| (m.id, damage * ((5.0 - d2.sqrt()) / 5.0).sqrt() as f32)) }) .collect() }; for (id, amount) in hits { if let Some(m) = self.mobs.iter_mut().find(|m| m.id == id) { m.damage( amount, Damage { kind: DamageKind::Explosion, attacker: None, from: Some(at), }, ); } } } let camera = self.eye_position().unwrap_or(at); let v = [r.velocity.x, r.velocity.y, r.velocity.z]; self.particles .fireworks(at, v, r.explosions.clone(), camera); } pub(super) fn rocket_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec) { let forward = glam::DVec3::from_array(view_vector(self.yaw, self.pitch)); let right = forward.cross(glam::DVec3::Y).normalize_or_zero(); let up = right.cross(forward).normalize_or_zero(); for r in self.rockets.iter().filter(|r| !r.attached) { let Some(s) = r .icon .as_ref() .and_then(|n| self.res.atlas.sprites.get(n)) .copied() else { continue; }; let p = glam::DVec3::new( r.previous.x + (r.position.x - r.previous.x) * alpha, r.previous.y + (r.position.y - r.previous.y) * alpha, r.previous.z + (r.position.z - r.previous.z) * alpha, ) - camera; let h = 0.25; let c = [ p - right * h - up * h, p + right * h - up * h, p + right * h + up * h, p - right * h + up * h, ]; for (v, uv) in c .iter() .zip([[s.u0, s.v1], [s.u1, s.v1], [s.u1, s.v0], [s.u0, s.v0]]) { out.push(Vertex { normal: [0; 4], pos: v.as_vec3().to_array(), uv, layer: 0, color: 0xFFFF_FFFF, light: 240 | 240 << 8, }); } } } }