From 472da6509dca7ebb9c4084d45ec40f96570e1d63 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Mon, 26 Oct 2026 18:45:57 +0100 Subject: [PATCH] Projectiles ring bells they hit --- crates/horizon-client/src/game/creatures.rs | 25 +++++++++++++++- crates/horizon-world/src/level/interact.rs | 19 ++---------- crates/horizon-world/src/level/mod.rs | 16 +++++++++++ crates/horizon-world/src/level/redstone.rs | 32 +++++++++++++++++++-- 4 files changed, 73 insertions(+), 19 deletions(-) diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 2b65a3289..f82962d21 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -20,7 +20,7 @@ use horizon_client::{ projectiles::{Hit, Projectile, ProjectileKind, ProjectileWorld}, }; use horizon_core::{ - geometry::{Aabb, Vec3, traverse_blocks}, + geometry::{Aabb, Direction, Vec3, traverse_blocks}, position::BlockPos, random::RandomSource, }; @@ -1877,6 +1877,22 @@ impl Game<'_> { None => self.projectile_hit(p, hit), } } + /// `BellBlock.onProjectileHit`: a projectile that strikes the bell proper + /// rings it, and a player's shot counts as the player's ring. + fn bell_projectile_hit(&mut self, p: &Projectile, pos: [i32; 3], at: Vec3, face: [i32; 3]) { + let Some(face) = Direction::ALL.into_iter().find(|d| d.normal() == face) else { + return; + }; + let block = BlockPos::new(pos[0], pos[1], pos[2]); + let rang = self + .with_level(|b, level, ticks| b.bell_projectile_hit(level, ticks, block, face, at.y)); + if rang { + let _ = self.process_level_events(); + if p.owner == Some(Who::Player) { + self.stats.custom("bell_ring", 1); + } + } + } /// What an arrow's tick asked for: its particles and sounds. fn arrow_event(&mut self, at: Vec3, event: horizon_client::arrows::ArrowEvent) { use horizon_client::arrows::{ArrowEvent, ArrowParticle, ArrowSound, BlockReaction}; @@ -2002,6 +2018,13 @@ impl Game<'_> { } } pub(super) fn projectile_hit(&mut self, p: &Projectile, hit: Hit) -> Result<()> { + // Projectile.onHitBlock: BlockState.onProjectileHit, for every + // projectile (a wind charge out of the world's top has no face). + if let Hit::Block { pos, at, face } = hit + && face != [0; 3] + { + self.bell_projectile_hit(p, pos, at, face); + } self.combat.projectile_owner = match p.owner { Some(Who::Mob(id)) => Some(id), _ => None, diff --git a/crates/horizon-world/src/level/interact.rs b/crates/horizon-world/src/level/interact.rs index 7b2195c34..18d131740 100644 --- a/crates/horizon-world/src/level/interact.rs +++ b/crates/horizon-world/src/level/interact.rs @@ -1,11 +1,9 @@ //! `BlockBehaviour.useWithoutItem` (right-click with an empty hand or an item //! that does not handle the block itself). -use super::{ - Behaviours, LevelAccess, LevelEvent, Sim, Ticks, classes::Class, query::axis_of, update::below, -}; +use super::{Behaviours, LevelAccess, LevelEvent, Sim, Ticks, classes::Class, update::below}; use crate::registry::BlockStateId; use horizon_core::{ - geometry::{Axis, Direction, Vec3}, + geometry::{Direction, Vec3}, position::BlockPos, random::RandomSource, }; @@ -338,18 +336,7 @@ impl Sim<'_, L> { } } Class::Bell => { - let click_y = hit.y - f64::from(pos.y); - let proper = axis_of(face) != Axis::Y && click_y <= 0.8124 && { - let facing_axis = axis_of(b.facing(state)); - match table.text(state, p.attachment) { - "floor" => facing_axis == axis_of(face), - "single_wall" | "double_wall" => facing_axis != axis_of(face), - "ceiling" => true, - _ => false, - } - }; - if proper { - self.bell_ring(pos, state, face); + if self.bell_hit(pos, state, face, hit.y - f64::from(pos.y)) { Success } else { Pass diff --git a/crates/horizon-world/src/level/mod.rs b/crates/horizon-world/src/level/mod.rs index a384b38b7..b16315279 100644 --- a/crates/horizon-world/src/level/mod.rs +++ b/crates/horizon-world/src/level/mod.rs @@ -768,6 +768,22 @@ impl Behaviours { self.sim(level, ticks) .big_dripleaf_entity_inside(pos, on_ground, feet_y); } + /// `BellBlock.onProjectileHit` for a projectile that hit `face` of the + /// block at `pos` at height `hit_y`. False when the block is not a bell + /// or the hit missed the bell proper. + pub fn bell_projectile_hit( + &self, + level: &mut L, + ticks: &mut Ticks, + pos: BlockPos, + face: Direction, + hit_y: f64, + ) -> bool { + let mut sim = self.sim(level, ticks); + let state = sim.state(pos); + self.data(state).is(classes::Class::Bell) + && sim.bell_hit(pos, state, face, hit_y - f64::from(pos.y)) + } /// Redstone signal queries (`SignalGetter`). pub fn has_neighbor_signal( &self, diff --git a/crates/horizon-world/src/level/redstone.rs b/crates/horizon-world/src/level/redstone.rs index 92f9059e5..acbd4aaee 100644 --- a/crates/horizon-world/src/level/redstone.rs +++ b/crates/horizon-world/src/level/redstone.rs @@ -5,13 +5,16 @@ use super::{ LevelAccess, LevelEvent, Sim, classes::Class, data::state_flags, - query::{clockwise, counter_clockwise}, + query::{axis_of, clockwise, counter_clockwise}, states::BlockId, update::{HORIZONTAL, above, below, rel}, }; use crate::registry::BlockStateId; use horizon_core::{ - geometry::Direction, position::BlockPos, random::RandomSource, ticks::TickPriority, + geometry::{Axis, Direction}, + position::BlockPos, + random::RandomSource, + ticks::TickPriority, }; const SIGNAL_ORDER: [Direction; 6] = [ @@ -1159,6 +1162,31 @@ impl Sim<'_, L> { self.set_block_and_update(pos, b.table.set_bool(state, b.props.powered, signal)); } } + /// `BellBlock.onHit` with `requireHitFromCorrectSide`: a hit on `face` at + /// height `click_y` in the block rings the bell only when it struck the + /// bell proper. False when the hit missed it. + pub(crate) fn bell_hit( + &mut self, + pos: BlockPos, + state: BlockStateId, + face: Direction, + click_y: f64, + ) -> bool { + let b = self.b; + let facing_axis = axis_of(b.facing(state)); + let proper = axis_of(face) != Axis::Y + && click_y <= f64::from(0.8124f32) + && match b.table.text(state, b.props.attachment) { + "floor" => facing_axis == axis_of(face), + "single_wall" | "double_wall" => facing_axis != axis_of(face), + "ceiling" => true, + _ => false, + }; + if proper { + self.bell_ring(pos, state, face); + } + proper + } /// `BellBlock.attemptToRing`: `BellBlockEntity.onHit` queues block event 1 /// with the struck direction, then the sound and game event. pub(crate) fn bell_ring(&mut self, pos: BlockPos, state: BlockStateId, direction: Direction) {