diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 194256040..d7e42f8c6 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -1993,6 +1993,23 @@ impl Game<'_> { self.creatures.projectiles.push(arrow); } } + /// `TargetBlock.onProjectileHit`: a target block takes its power from + /// where the projectile landed and resets after 20 ticks for an arrow, 8 + /// for any other projectile. A player's shot counts as a target hit. + fn target_block_hit(&mut self, p: &Projectile, pos: [i32; 3], at: Vec3, face: [i32; 3]) { + let block = BlockPos::new(pos[0], pos[1], pos[2]); + let arrow = p.arrow.is_some(); + let location = [at.x, at.y, at.z]; + let hit = self.with_level(|b, level, ticks| { + b.target_projectile_hit(level, ticks, block, location, face, arrow) + }); + if hit.is_some() { + let _ = self.process_level_events(); + if p.owner == Some(Who::Player) { + self.stats.custom("target_hit", 1); + } + } + } pub(super) fn projectile_hit(&mut self, p: &Projectile, hit: Hit) -> Result<()> { self.combat.projectile_owner = match p.owner { Some(Who::Mob(id)) => Some(id), @@ -2002,6 +2019,13 @@ impl Game<'_> { Hit::Entity { at, .. } | Hit::Block { at, .. } => at, Hit::None => return Ok(()), }; + // Projectile.onHitBlock: BlockState.onProjectileHit, for every + // projectile (a wind charge out of the world's top has no face). + if let Hit::Block { pos, face, .. } = hit + && face != [0; 3] + { + self.target_block_hit(p, pos, at, face); + } if let Hit::Entity { who: Who::Mob(id), at, diff --git a/crates/horizon-world/src/level/mod.rs b/crates/horizon-world/src/level/mod.rs index 3831efbd6..38b953278 100644 --- a/crates/horizon-world/src/level/mod.rs +++ b/crates/horizon-world/src/level/mod.rs @@ -779,6 +779,22 @@ impl Behaviours { ) -> i32 { self.sim(level, ticks).best_neighbor_signal(pos) } + /// `BlockState.onProjectileHit` of a target block (`TargetBlock`) hit by + /// a projectile at `location` on the face with normal `face`; `arrow` + /// for an `AbstractArrow`. Returns the output strength, or None when the + /// block at `pos` is not a target block. + pub fn target_projectile_hit( + &self, + level: &mut L, + ticks: &mut Ticks, + pos: BlockPos, + location: [f64; 3], + face: [i32; 3], + arrow: bool, + ) -> Option { + self.sim(level, ticks) + .target_projectile_hit(pos, location, face, arrow) + } } /// One mutable view used by the ported behaviour code. diff --git a/crates/horizon-world/src/level/redstone.rs b/crates/horizon-world/src/level/redstone.rs index 92f9059e5..89bcfc5f2 100644 --- a/crates/horizon-world/src/level/redstone.rs +++ b/crates/horizon-world/src/level/redstone.rs @@ -1187,6 +1187,43 @@ impl Sim<'_, L> { self.set_block(pos, next, 2); } } + /// `TargetBlock.onProjectileHit` for a projectile that hit `face` of the + /// block at `pos` at `location`: the output power from how near the + /// face's centre it landed (1 to 15), reset after 20 ticks for an arrow + /// and 8 for anything else, unless a reset is already scheduled. None + /// when the block is not a target block, else the strength. + pub(crate) fn target_projectile_hit( + &mut self, + pos: BlockPos, + location: [f64; 3], + face: [i32; 3], + arrow: bool, + ) -> Option { + let b = self.b; + let state = self.state(pos); + if !b.data(state).is(Class::Target) { + return None; + } + // `getRedstoneStrength`: the distance from the centre along the + // face's two other axes (Mth.frac of the location, minus a half). + let off = |v: f64| (v - v.floor() - 0.5).abs(); + let [dx, dy, dz] = [off(location[0]), off(location[1]), off(location[2])]; + let distance = if face[1] != 0 { + dx.max(dz) + } else if face[2] != 0 { + dx.max(dy) + } else { + dy.max(dz) + }; + let strength = ((15.0 * ((0.5 - distance) / 0.5).clamp(0.0, 1.0)).ceil() as i32).max(1); + let block = b.block(state); + if !self.ticks.has_block_tick(pos, block) { + // `setOutputPower`. + self.set_block_and_update(pos, b.table.set_int(state, b.props.power, strength)); + self.schedule_tick(pos, block, if arrow { 20 } else { 8 }); + } + Some(strength) + } } /// Order `positions` as a `java.util.HashSet` of capacity 16 would