From 18078e058d52e7aef20cea156326d3816ca61918 Mon Sep 17 00:00:00 2001 From: levzzz <86072227+levzzz5154@users.noreply.github.com> Date: Fri, 23 Oct 2026 03:47:04 +0500 Subject: [PATCH] Explosions hurt and push minecarts, so TNT minecarts chain ServerExplosion.hurtEntities skipped minecarts: a TNT minecart's blast never reached the carts beside it. Carts now take the explosion's damage through VehicleEntity.hurtServer (destroyed past 40, where MinecartTNT.destroy primes a TNT cart with a 0-39 tick fuse, as damageSourceIgnitesTnt counts explosions) and the knockback from their eyes. The exploding cart is the explosion's source, so it is removed before the blast and can't hit itself. Cart ticks now look carts up by id, since a blast can remove or prime other carts mid-loop. --- crates/horizon-client/src/game/explosion.rs | 30 ++++++++++++++++++++- crates/horizon-client/src/game/minecarts.rs | 30 ++++++++++++++------- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/crates/horizon-client/src/game/explosion.rs b/crates/horizon-client/src/game/explosion.rs index 5cf67f586..0923b4110 100644 --- a/crates/horizon-client/src/game/explosion.rs +++ b/crates/horizon-client/src/game/explosion.rs @@ -224,7 +224,7 @@ impl Game<'_> { } set.entries().map(|(k, _)| k.0).collect() } - /// `ServerExplosion.hurtEntities` (player, mobs, items, primed TNT); + /// `ServerExplosion.hurtEntities` (player, mobs, items, primed TNT, minecarts); /// the player takes `damage_type`. /// `ServerExplosion.hurtEntities` for the player: damage scaled by /// distance and exposure, through a raised shield (`hurtServer` blocks @@ -460,6 +460,34 @@ impl Game<'_> { ); } self.tnts = tnts; + // Minecarts: VehicleEntity.hurtServer (explosions ignite TNT carts), + // then the push from their eyes (0.85 of their height). + let ids: Vec = self.carts.iter().map(|c| c.id).collect(); + for id in ids { + let Some(i) = self.carts.iter().position(|c| c.id == id) else { + continue; + }; + let bounds = self.carts[i].bounds(); + let feet = self.carts[i].position; + let dist = distance([feet.x, feet.y, feet.z]) / diameter; + if dist > 1.0 { + continue; + } + let (lo, hi) = (bounds.min_position(), bounds.max_position()); + let exposure = self.seen_percent(center, [lo.x, lo.y, lo.z], [hi.x, hi.y, hi.z]); + self.hurt_cart(i, damage(dist, exposure), false, true); + let Some(c) = self.carts.iter_mut().find(|c| c.id == id) else { + continue; + }; + let eye_height = f64::from((hi.y - lo.y) as f32 * 0.85f32); + let dir = direction([feet.x, feet.y + eye_height, feet.z]); + let power = (1.0 - dist) * f64::from(exposure); + c.velocity = Vec3::new( + c.velocity.x + dir[0] * power, + c.velocity.y + dir[1] * power, + c.velocity.z + dir[2] * power, + ); + } } /// `Level.explode` on the server: positions, entities, blocks, fire; then /// the client's explosion sound. diff --git a/crates/horizon-client/src/game/minecarts.rs b/crates/horizon-client/src/game/minecarts.rs index b9d7f3893..9173139aa 100644 --- a/crates/horizon-client/src/game/minecarts.rs +++ b/crates/horizon-client/src/game/minecarts.rs @@ -637,6 +637,9 @@ impl Game<'_> { let r = self.world.random.next_double(); let power = (4.0 + r * 1.5 * speed) as f32; let at = [c.position.x, c.position.y, c.position.z]; + // The cart is the explosion's source: it is gone before the blast + // looks for entities, so it can't hit itself. + self.carts.retain(|x| x.id != c.id); let _ = self.server_explode(at, power, BlockInteraction::Destroy, false); } /// `MinecartHopper.suckInItems`: items above, into the five slots. @@ -792,14 +795,17 @@ impl Game<'_> { let ticking = self.entity_ticking_area(); // A server player's minecart moves with its rider (lan_vehicles.rs). let ridden = self.guest_vehicles(); - let mut i = 0; - while i < self.carts.len() { + // By id: an exploding cart can destroy or prime others mid-loop. + let ids: Vec = self.carts.iter().map(|c| c.id).collect(); + for id in ids { + let Some(i) = self.carts.iter().position(|c| c.id == id) else { + continue; + }; let p = self.carts[i].position; - if Some(self.carts[i].id) == self.riding - || ridden.contains(&self.carts[i].id) + if Some(id) == self.riding + || ridden.contains(&id) || !ticking(p.x.floor() as i32, p.z.floor() as i32) { - i += 1; continue; } let mut c = self.carts[i].clone(); @@ -807,10 +813,12 @@ impl Game<'_> { if c.kind == CartKind::Hopper { self.hopper_cart_pickup(&mut c); } + let Some(i) = self.carts.iter().position(|c| c.id == id) else { + continue; + }; if alive { self.carts[i] = c; self.push_from_cart(i); - i += 1; } else { self.carts.remove(i); } @@ -881,6 +889,10 @@ impl Game<'_> { p.velocity = Vec3::ZERO; p.fall_distance = 0.0; } + let Some(i) = self.carts.iter().position(|x| x.id == c.id) else { + self.riding = None; + return true; + }; if alive { self.carts[i] = c; self.push_from_cart(i); @@ -1138,8 +1150,8 @@ impl Game<'_> { } /// `VehicleEntity.hurtServer`: shake, add the damage, break past 40 (at /// once for creative players); TNT minecarts prime instead when moving - /// or hurt by fire. - fn hurt_cart(&mut self, i: usize, damage: f32, creative: bool, fire: bool) { + /// or hurt by fire or an explosion (`damageSourceIgnitesTnt`). + pub(super) fn hurt_cart(&mut self, i: usize, damage: f32, creative: bool, fire: bool) { let c = &mut self.carts[i]; c.hurt_dir = -c.hurt_dir; c.hurt_time = 10; @@ -1148,7 +1160,7 @@ impl Game<'_> { if destroyed { let mut c = self.carts.remove(i); let speed = c.velocity.x * c.velocity.x + c.velocity.z * c.velocity.z; - if c.kind == CartKind::Tnt && (fire || speed >= 0.01) { + if c.kind == CartKind::Tnt && (fire || speed >= f64::from(0.01f32)) { // MinecartTNT.destroy: prime instead of breaking. if c.fuse < 0 { self.prime_cart(&mut c);