Merge pull request #1082 from ludovic111/fix/cobweb-falling-blocks
🤖 Agent: merged. Cobwebs slow falling blocks (fixes #933). Integration proof (main + #1079 #1081 #1082 #1083 #1085 #1086 #1087), 2026-10-21: desktop parity 103/105; the 2 others (world_reopen, save_roundtrip) need a display and pass on the Mac mini. Verify passed.
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
//! FallingBlockEntity's impact damage, anvil wear and saved impact settings.
|
||||
//! FallingBlockEntity's impact damage, anvil wear, saved impact settings and
|
||||
//! the cobwebs that hold it back.
|
||||
use crate::{
|
||||
mobs::{Damage as MobDamage, DamageKind, Mob},
|
||||
save::{float, number, put},
|
||||
};
|
||||
use horizon_core::geometry::{Aabb, Vec3};
|
||||
use horizon_core::{
|
||||
geometry::{Aabb, Axis, Direction, Vec3, sweep_intersected_blocks},
|
||||
position::BlockPos,
|
||||
};
|
||||
use horizon_world::{chunk::BlockState, nbt::Tag};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
@@ -97,6 +101,65 @@ pub fn bounds(position: Vec3) -> Aabb {
|
||||
),
|
||||
)
|
||||
}
|
||||
/// `WebBlock.entityInside`'s `makeStuckInBlock` multiplier: the next move
|
||||
/// goes at a quarter of the speed across, a twentieth down.
|
||||
pub fn web_multiplier() -> Vec3 {
|
||||
Vec3::new(0.25, f64::from(0.05_f32), 0.25)
|
||||
}
|
||||
/// `Entity.applyEffectsFromBlocks` after a falling block's move this tick
|
||||
/// (`from` to `to`; `original` the move it asked for, when the move was
|
||||
/// recorded): whether `checkInsideBlocks` reaches a cobweb (`is_web`), whose
|
||||
/// `entityInside` makes it stuck. Each movement sweeps its axes in
|
||||
/// `Direction.axisStepOrder` within 16 iterations, then the end alone.
|
||||
pub fn passes_cobweb(
|
||||
from: Vec3,
|
||||
to: Vec3,
|
||||
original: Option<Vec3>,
|
||||
mut is_web: impl FnMut(BlockPos) -> bool,
|
||||
) -> bool {
|
||||
let mut stuck = false;
|
||||
// checkInsideBlocks(from, to, ..): the iterations used plus one.
|
||||
let mut check = |from: Vec3, to: Vec3, max: i32| {
|
||||
let d = f64::from(1.0e-5_f32);
|
||||
let target = bounds(to).deflate(Vec3::new(d, d, d));
|
||||
let mut iterations = 0;
|
||||
let _ = sweep_intersected_blocks(from, to, target, 1_000_000, |p, iteration| {
|
||||
if iteration >= max {
|
||||
return false;
|
||||
}
|
||||
iterations = iteration;
|
||||
// getEntityInsideCollisionShape is the whole block for cobwebs.
|
||||
stuck |= is_web(p);
|
||||
true
|
||||
});
|
||||
iterations + 1
|
||||
};
|
||||
let delta = to.subtract(from);
|
||||
let mut budget = 16;
|
||||
match original {
|
||||
Some(original) if delta.length_squared() > 0.0 => {
|
||||
let mut at = from;
|
||||
for axis in Axis::step_order(original) {
|
||||
let step = delta.component(axis);
|
||||
if step != 0.0 {
|
||||
let positive = match axis {
|
||||
Axis::X => Direction::East,
|
||||
Axis::Y => Direction::Up,
|
||||
Axis::Z => Direction::South,
|
||||
};
|
||||
let next = at.relative(positive, step);
|
||||
budget -= check(at, next, budget);
|
||||
at = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => budget -= check(from, to, 16),
|
||||
}
|
||||
if budget <= 0 {
|
||||
check(to, to, 1);
|
||||
}
|
||||
stuck
|
||||
}
|
||||
pub fn hurt_mobs(mobs: &mut [Mob], position: Vec3, amount: f32) {
|
||||
if amount <= 0.0 {
|
||||
return;
|
||||
@@ -163,6 +226,41 @@ mod tests {
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn a_cobweb_on_the_path_or_at_the_end_holds_a_falling_block() {
|
||||
let web = BlockPos::new(0, 5, 0);
|
||||
let is_web = |p: BlockPos| p == web;
|
||||
let at = |y: f64| Vec3::new(0.5, y, 0.5);
|
||||
// Falling through the web's block in one move, landing in it, resting in it.
|
||||
assert!(passes_cobweb(
|
||||
at(7.2),
|
||||
at(3.1),
|
||||
Some(Vec3::new(0.0, -4.1, 0.0)),
|
||||
is_web
|
||||
));
|
||||
assert!(passes_cobweb(
|
||||
at(6.5),
|
||||
at(5.5),
|
||||
Some(Vec3::new(0.0, -1.0, 0.0)),
|
||||
is_web
|
||||
));
|
||||
assert!(passes_cobweb(at(5.5), at(5.5), None, is_web));
|
||||
// Above it, below it, or beside it.
|
||||
assert!(!passes_cobweb(
|
||||
at(8.0),
|
||||
at(6.01),
|
||||
Some(Vec3::new(0.0, -1.99, 0.0)),
|
||||
is_web
|
||||
));
|
||||
assert!(!passes_cobweb(
|
||||
at(4.0),
|
||||
at(3.0),
|
||||
Some(Vec3::new(0.0, -1.0, 0.0)),
|
||||
is_web
|
||||
));
|
||||
let beside = Vec3::new(1.5, 5.5, 0.5);
|
||||
assert!(!passes_cobweb(beside, beside, None, is_web));
|
||||
}
|
||||
#[test]
|
||||
#[ignore = "requires prepared entity models/data in HORIZON_TEST_RESOURCES"]
|
||||
fn impact_hurts_only_living_intersecting_noninvulnerable_mobs() {
|
||||
use crate::mobs::{Mob, MobTypes};
|
||||
|
||||
@@ -835,6 +835,8 @@ struct FallingBlock {
|
||||
drop_item: bool,
|
||||
/// `Entity.fallDistance` (powder snow holds a block that fell far at 0.9).
|
||||
fall_distance: f64,
|
||||
/// `Entity.stuckSpeedMultiplier`: a cobweb it is in scales its next move.
|
||||
stuck: Vec3,
|
||||
damage: horizon_client::falling_block::Damage,
|
||||
/// The UUID and compound it was loaded with, kept while it moves.
|
||||
saved: Option<Box<([i32; 4], horizon_world::nbt::Tag)>>,
|
||||
@@ -2473,6 +2475,7 @@ impl Game<'_> {
|
||||
no_gravity: false,
|
||||
drop_item: true,
|
||||
fall_distance: 0.0,
|
||||
stuck: Vec3::ZERO,
|
||||
damage: if self.res.facts().get(state).name.ends_with("anvil") {
|
||||
horizon_client::falling_block::Damage::anvil()
|
||||
} else {
|
||||
@@ -2707,7 +2710,14 @@ impl Game<'_> {
|
||||
if !f.no_gravity {
|
||||
f.velocity.y -= 0.04;
|
||||
}
|
||||
let wanted = f.velocity;
|
||||
let mut wanted = f.velocity;
|
||||
// Entity.move: a cobweb it was in scales this move, which then
|
||||
// starts from rest.
|
||||
if f.stuck.length_squared() > 1.0e-7 {
|
||||
wanted = wanted.multiply(f.stuck);
|
||||
f.stuck = Vec3::ZERO;
|
||||
f.velocity = Vec3::ZERO;
|
||||
}
|
||||
let mut scene = collisions::SceneCollisions {
|
||||
world: &self.world,
|
||||
binding: self.collision,
|
||||
@@ -2760,6 +2770,7 @@ impl Game<'_> {
|
||||
if next.y < f.position.y {
|
||||
f.fall_distance += f.position.y - next.y;
|
||||
}
|
||||
let from = f.position;
|
||||
f.position = next;
|
||||
if on_ground {
|
||||
let pos = [
|
||||
@@ -2770,6 +2781,22 @@ impl Game<'_> {
|
||||
landed.push((f.clone(), pos));
|
||||
return false;
|
||||
}
|
||||
// applyEffectsFromBlocks: WebBlock.entityInside makes it stuck
|
||||
// (Entity.makeStuckInBlock, which forgets the fall). Entity.move
|
||||
// records the move unless it was blocked short of a tiny one.
|
||||
let moved = movement.length_squared();
|
||||
let recorded = moved > 1.0e-7 || wanted.length_squared() - moved < 1.0e-7;
|
||||
let world = &self.world;
|
||||
let facts = self.res.facts();
|
||||
if horizon_client::falling_block::passes_cobweb(
|
||||
from,
|
||||
f.position,
|
||||
recorded.then_some(wanted),
|
||||
|p| &*facts.get(world.block(p.x, p.y, p.z)).name == "minecraft:cobweb",
|
||||
) {
|
||||
f.fall_distance = 0.0;
|
||||
f.stuck = horizon_client::falling_block::web_multiplier();
|
||||
}
|
||||
if movement.x != wanted.x {
|
||||
f.velocity.x = 0.0;
|
||||
}
|
||||
|
||||
@@ -213,6 +213,7 @@ impl Game<'_> {
|
||||
no_gravity,
|
||||
drop_item,
|
||||
fall_distance,
|
||||
stuck: Vec3::ZERO,
|
||||
damage: horizon_client::falling_block::Damage::from_nbt(
|
||||
&tag,
|
||||
self.res.facts().get(state).name.ends_with("anvil"),
|
||||
|
||||
@@ -66,6 +66,7 @@ impl Game<'_> {
|
||||
no_gravity: false,
|
||||
drop_item: false,
|
||||
fall_distance: 0.0,
|
||||
stuck: Vec3::ZERO,
|
||||
damage: Default::default(),
|
||||
saved: None,
|
||||
}),
|
||||
|
||||
@@ -7,9 +7,10 @@ client (VanillaProtocolClient in its entity mode) joins, and once its chunks are
|
||||
console freezes the ticks, builds a wall and a rail and summons, around the spawn, every kind of minecart (one
|
||||
set rolling along the rail), boats and a raft (one falling onto the ground), armor stands (posed, small, with
|
||||
arms, wearing a helmet, one falling), a chicken jockey, a slime, a pig whose scale `/attribute` sets, item
|
||||
frames (one holding a turned diamond), paintings, primed TNT (one falling), a floating falling block, an arrow
|
||||
that lands and an end crystal. The client records for 20 seconds, the ticks running for 200 more after 3, what
|
||||
ClientPacketListener would make of the packets (EntityTranscript): each entity's spawn
|
||||
frames (one holding a turned diamond), paintings, primed TNT (one falling), a floating falling block, sand
|
||||
falling into a cobweb, an arrow that lands and an end crystal. The client records for 20 seconds, the ticks
|
||||
running for 200 more after 3, what ClientPacketListener would make of the packets (EntityTranscript): each
|
||||
entity's spawn
|
||||
packet (position, rotations, data, motion), the entity data it was paired with and all it was sent since, its
|
||||
equipment, its attribute instances (ClientboundUpdateAttributesPacket: the ones its first tick creates, the
|
||||
type's set by finalizeSpawn, the baby speed modifier, effects', equipment's and /attribute's) and passengers, and
|
||||
@@ -81,6 +82,9 @@ SCENE = [
|
||||
'summon minecraft:tnt 6.5 -60 6.5 {fuse:2000s}',
|
||||
'summon minecraft:tnt 6.5 -56 4.5 {fuse:2000s}',
|
||||
'summon minecraft:falling_block 2.5 -58 -0.5 {BlockState:{Name:"minecraft:sand"},NoGravity:1b}',
|
||||
# Sand dropped into a cobweb, which holds it (WebBlock.entityInside).
|
||||
'setblock -7 -58 0 minecraft:cobweb',
|
||||
'summon minecraft:falling_block -6.5 -54 0.5 {BlockState:{Name:"minecraft:sand"}}',
|
||||
'summon minecraft:arrow 0.5 -55 -1.5 {Motion:[0.3d,0d,0d]}',
|
||||
'summon minecraft:end_crystal -6.5 -60 6.5 {ShowBottom:0b}',
|
||||
]
|
||||
@@ -185,7 +189,8 @@ def main():
|
||||
java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0],
|
||||
entities=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'),
|
||||
scope='Minecarts (one rolling), boats (one falling), armor stands, a chicken jockey, a slime, a pig with a '
|
||||
'scale, item frames, paintings, primed TNT, a falling block, an arrow and an end crystal '
|
||||
'scale, item frames, paintings, primed TNT, falling blocks (one held by a cobweb), an arrow and an '
|
||||
'end crystal '
|
||||
'tracked to a vanilla-codec client by a dedicated server: spawn packets, entity data, equipment, '
|
||||
'attributes, passengers and the positions the syncs leave them at.'))
|
||||
print(f'server entities: {len(vanilla)} entities identical')
|
||||
|
||||
Reference in New Issue
Block a user