Target blocks react to projectile hits
A target block never changed its power when something hit it. Every projectile that hits a block now asks the block how to react, as vanilla does: a target block takes its power from where the projectile landed (1 to 15 by the distance from the face centre) and resets after 20 ticks for an arrow or 8 for any other projectile. A player's hit also counts the target_hit statistic.
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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<L: LevelAccess + ?Sized>(
|
||||
&self,
|
||||
level: &mut L,
|
||||
ticks: &mut Ticks,
|
||||
pos: BlockPos,
|
||||
location: [f64; 3],
|
||||
face: [i32; 3],
|
||||
arrow: bool,
|
||||
) -> Option<i32> {
|
||||
self.sim(level, ticks)
|
||||
.target_projectile_hit(pos, location, face, arrow)
|
||||
}
|
||||
}
|
||||
|
||||
/// One mutable view used by the ported behaviour code.
|
||||
|
||||
@@ -1187,6 +1187,43 @@ impl<L: LevelAccess + ?Sized> 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<i32> {
|
||||
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<BlockPos>` of capacity 16 would
|
||||
|
||||
Reference in New Issue
Block a user