Merge pull request #2156 from kacpherek/fix/buttons-pressed-by-arrows
🤖 Agent: Gate check by Ludovic's AI agent (Claude). Checked against 26.3: `ButtonBlock.checkPressed` presses a wooden button while an arrow is inside its shape and keeps it pressed as long as one is there. One difference is left for later: wooden pressure plates and tripwires don't count arrows yet. Gate: this wave's 55 PRs merged on main in one tree: clippy `-D warnings` clean, 1186 horizon-client and 163 horizon-world tests pass, and of the 101 harnesses `parity.py --changed-files` picked, 100 pass (`block_removal` failed on one spawner case from #2199's orb merging, fixed there). `lan_e2e.py` passes 24 checks with the host on its server thread and in-frame (chest, xp, pvp, doors, villagers, beds, frames, projectiles, arrows, fishing, damage, mobs, blast, drops, elytra, breath, predict, cracks, barter, kills, hurt, contacts, underwater, plates), plus forced-levels on the server thread. Thanks @kacpherek!
This commit is contained in:
@@ -367,6 +367,29 @@ fn bounds(at: Vec3) -> Aabb {
|
||||
Vec3::new(at.x + w, at.y + f64::from(HEIGHT), at.z + w),
|
||||
)
|
||||
}
|
||||
/// The bounding box of an arrow, or a thrown trident, at `at`.
|
||||
pub fn arrow_bounds(at: Vec3) -> Aabb {
|
||||
bounds(at)
|
||||
}
|
||||
/// The blocks an arrow that moved from `from` to `to` was inside
|
||||
/// (`Entity.checkInsideBlocks`: the sweep of its box, 16 steps at most), for
|
||||
/// the blocks that react to it (`entityInside`).
|
||||
pub fn blocks_inside(from: Vec3, to: Vec3) -> Vec<BlockPos> {
|
||||
let deflated = bounds(to).deflate(Vec3::new(
|
||||
f64::from(1.0e-5f32),
|
||||
f64::from(1.0e-5f32),
|
||||
f64::from(1.0e-5f32),
|
||||
));
|
||||
let mut inside = Vec::new();
|
||||
let _ = sweep_intersected_blocks(from, to, deflated, 1_000_000, |p, iteration| {
|
||||
if iteration >= 16 {
|
||||
return false;
|
||||
}
|
||||
inside.push(p);
|
||||
true
|
||||
});
|
||||
inside
|
||||
}
|
||||
/// `Projectile.lerpRotation`.
|
||||
fn lerp_rotation(mut old: f32, new: f32) -> f32 {
|
||||
while new - old < -180.0 {
|
||||
|
||||
@@ -547,6 +547,8 @@ struct GameLevel<'w> {
|
||||
player_collision: Option<PlayerCollision>,
|
||||
/// Minecart boxes (detector rails).
|
||||
cart_boxes: Vec<([f64; 3], [f64; 3])>,
|
||||
/// Arrow and thrown trident boxes (wooden buttons).
|
||||
arrow_boxes: Vec<([f64; 3], [f64; 3])>,
|
||||
/// The other entities that keep blocks from being placed into them.
|
||||
blockers: Blockers<'w>,
|
||||
biomes: &'w Biomes,
|
||||
@@ -878,8 +880,9 @@ impl LevelAccess for GameLevel<'_> {
|
||||
fn is_ticking_chunk(&self, chunk: horizon_core::position::ChunkPos) -> bool {
|
||||
self.tickets.block_ticking(chunk.x, chunk.z) && ticking_chunk(self.world, chunk.x, chunk.z)
|
||||
}
|
||||
/// Entities that trigger pressure plates, tripwires and detector rails:
|
||||
/// the players (the host and the LAN guests, mobs) and the minecarts.
|
||||
/// Entities that trigger pressure plates, tripwires, detector rails and
|
||||
/// wooden buttons: the players (the host and the LAN guests, mobs) and
|
||||
/// the minecarts, and for `EntityQuery::Arrows` the arrows.
|
||||
fn count_entities(&self, area: Aabb, query: EntityQuery) -> usize {
|
||||
let hit = |(lo, hi): &([f64; 3], [f64; 3])| {
|
||||
area.intersects(Aabb::new(
|
||||
@@ -888,6 +891,7 @@ impl LevelAccess for GameLevel<'_> {
|
||||
))
|
||||
};
|
||||
let carts = || self.cart_boxes.iter().filter(|b| hit(b)).count();
|
||||
let arrows = || self.arrow_boxes.iter().filter(|b| hit(b)).count();
|
||||
let players = || {
|
||||
self.player_box
|
||||
.iter()
|
||||
@@ -897,6 +901,7 @@ impl LevelAccess for GameLevel<'_> {
|
||||
};
|
||||
match query {
|
||||
EntityQuery::Minecart => carts(),
|
||||
EntityQuery::Arrows => arrows(),
|
||||
EntityQuery::Mobs => players(),
|
||||
EntityQuery::Everything => players() + carts(),
|
||||
}
|
||||
@@ -2518,6 +2523,18 @@ impl Game<'_> {
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let arrow_boxes = self
|
||||
.creatures
|
||||
.projectiles
|
||||
.iter()
|
||||
.filter(|p| p.arrow.is_some())
|
||||
.map(|p| p.position)
|
||||
.chain(self.tridents.iter().map(|t| t.position))
|
||||
.map(|at| {
|
||||
let b = horizon_client::arrows::arrow_bounds(at);
|
||||
(b.min_position().components(), b.max_position().components())
|
||||
})
|
||||
.collect();
|
||||
let guest_boxes = self.lan_block_trigger_boxes();
|
||||
let dimension = match self.dim {
|
||||
Dim::Overworld => LevelDimension::Overworld,
|
||||
@@ -2548,6 +2565,7 @@ impl Game<'_> {
|
||||
};
|
||||
let mut level = GameLevel {
|
||||
cart_boxes,
|
||||
arrow_boxes,
|
||||
blockers,
|
||||
world: &mut self.world,
|
||||
facts: self.res.facts(),
|
||||
@@ -2686,8 +2704,9 @@ impl Game<'_> {
|
||||
.copied()
|
||||
.filter(|&(x, z)| levels.random_ticking(x, z) && ticking_chunk(&self.world, x, z))
|
||||
.collect();
|
||||
// Entity.checkInsideBlocks for the plates, tripwires and detector rails
|
||||
// under the players (the host and the LAN guests) and the minecarts.
|
||||
// Entity.checkInsideBlocks for the plates, tripwires, detector rails and
|
||||
// buttons under the players (the host and the LAN guests), the minecarts
|
||||
// and the arrows.
|
||||
let mut inside = Vec::new();
|
||||
let guests = self.lan_block_trigger_boxes();
|
||||
let boxes = self
|
||||
@@ -2719,6 +2738,24 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
// AbstractArrow.applyEffectsFromBlocks: the blocks every arrow passed
|
||||
// through this tick (a wooden button is pressed by one inside it), and
|
||||
// the ones a thrown trident is in.
|
||||
for p in self
|
||||
.creatures
|
||||
.projectiles
|
||||
.iter()
|
||||
.filter(|p| p.arrow.is_some())
|
||||
{
|
||||
inside.extend(horizon_client::arrows::blocks_inside(
|
||||
p.previous, p.position,
|
||||
));
|
||||
}
|
||||
for t in &self.tridents {
|
||||
inside.extend(horizon_client::arrows::blocks_inside(
|
||||
t.position, t.position,
|
||||
));
|
||||
}
|
||||
// BigDripleafBlock.entityInside also asks whether the player stands on
|
||||
// the ground and how high its feet are: the leaves its box's feet
|
||||
// layer is inside (the host and the LAN guests).
|
||||
|
||||
@@ -447,6 +447,13 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
|
||||
if !b.table.bool(state, b.props.powered) {
|
||||
self.detector_rail_check_pressed(pos, state);
|
||||
}
|
||||
} else if data.is(Class::Button) {
|
||||
// ButtonBlock.entityInside: a wooden button an arrow is inside.
|
||||
if data.set_type.as_ref().is_some_and(|s| s.arrows)
|
||||
&& !b.table.bool(state, b.props.powered)
|
||||
{
|
||||
self.button_check_pressed(pos, state);
|
||||
}
|
||||
} else if data.is(Class::TripWire)
|
||||
&& !b.table.bool(state, b.props.powered)
|
||||
&& !self.ticks.has_block_tick(pos, b.block(state))
|
||||
|
||||
@@ -209,6 +209,9 @@ pub enum EntityQuery {
|
||||
Mobs,
|
||||
/// Minecarts only (detector rails).
|
||||
Minecart,
|
||||
/// Arrows only (`AbstractArrow`, thrown tridents included): the entities
|
||||
/// a wooden button senses.
|
||||
Arrows,
|
||||
}
|
||||
|
||||
/// Side effects reported to the caller, in vanilla call order.
|
||||
|
||||
@@ -882,18 +882,84 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
|
||||
}
|
||||
self.game_event(pos, "minecraft:block_activate");
|
||||
}
|
||||
/// `ButtonBlock.checkPressed` with no arrows present.
|
||||
/// `ButtonBlock.tick`.
|
||||
pub(crate) fn button_tick(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
if !b.table.bool(state, b.props.powered) {
|
||||
return;
|
||||
if b.table.bool(state, b.props.powered) {
|
||||
self.button_check_pressed(pos, state);
|
||||
}
|
||||
self.set_block_and_update(pos, b.table.set_bool(state, b.props.powered, false));
|
||||
self.attached_update_neighbours(pos, state);
|
||||
if let Some(set) = b.data(state).set_type.clone() {
|
||||
self.sound(pos, &set.button_off, 1.0, 1.0);
|
||||
}
|
||||
/// `ButtonBlock.checkPressed`: a button whose type allows arrows is
|
||||
/// pressed while an arrow is inside its shape, and stays pressed (the
|
||||
/// release is scheduled again) as long as one is.
|
||||
pub(crate) fn button_check_pressed(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
let data = b.data(state);
|
||||
let should_be_pressed = data.set_type.as_ref().is_some_and(|s| s.arrows)
|
||||
&& self
|
||||
.level
|
||||
.count_entities(self.button_bounds(pos, state), super::EntityQuery::Arrows)
|
||||
> 0;
|
||||
let was_pressed = b.table.bool(state, b.props.powered);
|
||||
if should_be_pressed != was_pressed {
|
||||
self.set_block_and_update(
|
||||
pos,
|
||||
b.table.set_bool(state, b.props.powered, should_be_pressed),
|
||||
);
|
||||
self.attached_update_neighbours(pos, state);
|
||||
if let Some(set) = data.set_type.clone() {
|
||||
let sound = if should_be_pressed {
|
||||
&set.button_on
|
||||
} else {
|
||||
&set.button_off
|
||||
};
|
||||
self.sound(pos, sound, 1.0, 1.0);
|
||||
}
|
||||
self.game_event(
|
||||
pos,
|
||||
if should_be_pressed {
|
||||
"minecraft:block_activate"
|
||||
} else {
|
||||
"minecraft:block_deactivate"
|
||||
},
|
||||
);
|
||||
}
|
||||
self.game_event(pos, "minecraft:block_deactivate");
|
||||
if should_be_pressed {
|
||||
self.schedule_tick(pos, b.block(state), data.ticks_to_stay_pressed);
|
||||
}
|
||||
}
|
||||
/// `ButtonBlock.getShape(...).bounds().move(pos)`: a 6 by 4 plate on the
|
||||
/// face it is attached to, 2 pixels thick (1 when pressed).
|
||||
fn button_bounds(&self, pos: BlockPos, state: BlockStateId) -> horizon_core::geometry::Aabb {
|
||||
use horizon_core::geometry::{Aabb, Vec3};
|
||||
let b = self.b;
|
||||
let thickness = if b.table.bool(state, b.props.powered) {
|
||||
1.0 / 16.0
|
||||
} else {
|
||||
2.0 / 16.0
|
||||
};
|
||||
let attached = self.face_attached_connected(state).opposite().normal();
|
||||
let thick = attached.iter().position(|&n| n != 0).unwrap_or(1);
|
||||
// The 6 pixel side lies across the button's facing.
|
||||
let wide = if b.facing(state).normal()[2] != 0 {
|
||||
0
|
||||
} else {
|
||||
2
|
||||
};
|
||||
let tall = 3 - thick - wide;
|
||||
let (mut lo, mut hi) = ([0.0; 3], [1.0; 3]);
|
||||
if attached[thick] > 0 {
|
||||
lo[thick] = 1.0 - thickness;
|
||||
} else {
|
||||
hi[thick] = thickness;
|
||||
}
|
||||
(lo[wide], hi[wide]) = (5.0 / 16.0, 11.0 / 16.0);
|
||||
(lo[tall], hi[tall]) = (6.0 / 16.0, 10.0 / 16.0);
|
||||
let origin = [f64::from(pos.x), f64::from(pos.y), f64::from(pos.z)];
|
||||
Aabb::new(
|
||||
Vec3::new(origin[0] + lo[0], origin[1] + lo[1], origin[2] + lo[2]),
|
||||
Vec3::new(origin[0] + hi[0], origin[1] + hi[1], origin[2] + hi[2]),
|
||||
)
|
||||
}
|
||||
/// `DaylightDetectorBlock.updateSignalStrength`: the effective sky light
|
||||
/// at the detector, shaped by the sun's angle (or inverted), as its
|
||||
|
||||
Reference in New Issue
Block a user