diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index f3671c69c..2da2bde41 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -481,6 +481,8 @@ struct GameLevel<'w> { player_collision: Option, /// Minecart boxes (detector rails). cart_boxes: Vec<([f64; 3], [f64; 3])>, + /// The other entities that keep blocks from being placed into them. + blockers: Blockers<'w>, biomes: &'w Biomes, seed: i64, behaviours: &'w Behaviours, @@ -504,22 +506,83 @@ struct PlayerCollision { main_hand_count: i32, leather_boots: bool, } -struct PlayerObstruction(Aabb); -impl EntityObstructionWorld for PlayerObstruction { +/// The entities `Entity.blocksBuilding` sets besides the players and +/// minecarts, borrowed for `Level.isUnobstructed`: living entities (mobs, +/// armor stands), boats, end crystals, primed TNT and falling blocks. +#[derive(Clone, Copy)] +struct Blockers<'w> { + mobs: &'w [Mob], + armor_stands: &'w [horizon_client::entities::ArmorStand], + boats: &'w [boats::Boat], + crystals: &'w [dragon::Crystal], + tnts: &'w [tnt::PrimedTnt], + falling: &'w [FallingBlock], +} +/// The boxes of the entities that block building, as obstruction candidates. +struct BuildingObstruction(Vec); +impl EntityObstructionWorld for BuildingObstruction { type Error = std::convert::Infallible; fn obstruction_candidates( &mut self, _excluded: Option, - _bounds: Aabb, + bounds: Aabb, ) -> std::result::Result, Self::Error> { - Ok(vec![ObstructionEntity { - identity: EntityIdentity(0), - root_vehicle: EntityIdentity(0), - bounds: self.0, - removed: false, - spectator: false, - blocks_building: true, - }]) + Ok(self + .0 + .iter() + .filter(|b| b.intersects(bounds)) + .enumerate() + .map(|(i, &bounds)| ObstructionEntity { + identity: EntityIdentity(i as u64), + root_vehicle: EntityIdentity(i as u64), + bounds, + removed: false, + spectator: false, + blocks_building: true, + }) + .collect()) + } +} +impl GameLevel<'_> { + /// Every entity box that blocks building: the players who aren't + /// spectators, the minecarts and the `Blockers` (a removed mob or a marker + /// armor stand, whose box is empty, doesn't). + fn building_blocker_boxes(&self) -> Vec { + let boxed = |(lo, hi): &([f64; 3], [f64; 3])| { + Aabb::new( + Vec3::new(lo[0], lo[1], lo[2]), + Vec3::new(hi[0], hi[1], hi[2]), + ) + }; + let b = self.blockers; + self.player_box + .iter() + .chain(&self.guest_boxes) + .chain(&self.cart_boxes) + .map(boxed) + .chain(b.mobs.iter().filter(|m| !m.removed).map(Mob::bounds)) + .chain( + b.armor_stands + .iter() + .filter(|s| !s.marker) + .map(|s| s.bounds()), + ) + .chain(b.boats.iter().map(boats::Boat::bounds)) + .chain(b.crystals.iter().map(|c| { + // EntityType.END_CRYSTAL: 2 x 2. + let p = c.position; + Aabb::new( + Vec3::new(p.x - 1.0, p.y, p.z - 1.0), + Vec3::new(p.x + 1.0, p.y + 2.0, p.z + 1.0), + ) + })) + .chain(b.tnts.iter().map(tnt::PrimedTnt::bounds)) + .chain( + b.falling + .iter() + .map(|f| horizon_client::falling_block::bounds(f.position)), + ) + .collect() } } impl LevelAccess for GameLevel<'_> { @@ -638,13 +701,12 @@ impl LevelAccess for GameLevel<'_> { self.dimension } /// `Level.isUnobstructed`: the placed state's collision shape against the player. + /// `Level.isUnobstructed(state, pos, CollisionContext.of(player))`: no + /// entity that blocks building is in the state's collision shape. fn is_unobstructed(&self, pos: BlockPos, state: BlockStateId) -> bool { - let Some((lo, hi)) = self.player_box else { - return true; - }; let player = self.player_collision.as_ref(); let context = player.map_or_else( - || CollisionContext::position(lo[1]), + || CollisionContext::empty(false), |player| { CollisionContext::for_entity( CollisionEntity { @@ -686,10 +748,7 @@ impl LevelAccess for GameLevel<'_> { f64::from(pos.z), )); horizon_world::entity_obstruction::is_unobstructed( - &mut PlayerObstruction(Aabb::new( - horizon_core::geometry::Vec3::new(lo[0], lo[1], lo[2]), - horizon_core::geometry::Vec3::new(hi[0], hi[1], hi[2]), - )), + &mut BuildingObstruction(self.building_blocker_boxes()), None, &shape, ) @@ -2061,8 +2120,17 @@ impl Game<'_> { Dim::End => LevelDimension::End, }; let rules = self.level_rules(); + let blockers = Blockers { + mobs: &self.mobs, + armor_stands: &self.armor_stands, + boats: &self.boats, + crystals: &self.crystals, + tnts: &self.tnts, + falling: &self.falling, + }; let mut level = GameLevel { cart_boxes, + blockers, world: &mut self.world, facts: self.res.facts(), collision: self.collision, diff --git a/crates/horizon-client/src/game/tnt.rs b/crates/horizon-client/src/game/tnt.rs index 80ad97d7f..58d6b95ce 100644 --- a/crates/horizon-client/src/game/tnt.rs +++ b/crates/horizon-client/src/game/tnt.rs @@ -22,7 +22,7 @@ pub(super) struct PrimedTnt { } impl PrimedTnt { const SIZE: f64 = 0.98; - fn bounds(&self) -> Aabb { + pub(super) fn bounds(&self) -> Aabb { let h = Self::SIZE / 2.0; Aabb::new( Vec3::new(self.position.x - h, self.position.y, self.position.z - h), diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockUpdateOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockUpdateOracle.java index 956570d0f..d84244ede 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockUpdateOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockUpdateOracle.java @@ -232,7 +232,9 @@ public final class VanillaBlockUpdateOracle { @Override public boolean isRaining() { return raining; } @Override public boolean isThundering() { return thundering; } @Override public boolean isRainingAt(BlockPos p) { return raining; } - @Override public boolean isUnobstructed(BlockState state, BlockPos pos, net.minecraft.world.phys.shapes.CollisionContext c) { return true; } + // With entities (a harness opted in) vanilla's own check runs on them + // (CollisionGetter.isUnobstructed, then EntityGetter's blocksBuilding test). + @Override public boolean isUnobstructed(BlockState state, BlockPos pos, net.minecraft.world.phys.shapes.CollisionContext c) { return entities == null || super.isUnobstructed(state, pos, c); } @Override public boolean isUnobstructed(Entity entity) { return true; } // Entities exist only when a harness opts in (the dispenser oracle); // they are searched in the order they were added, as within one entity section. diff --git a/tools/test_use_item_on_parity.py b/tools/test_use_item_on_parity.py index 42086f200..398f55a92 100644 --- a/tools/test_use_item_on_parity.py +++ b/tools/test_use_item_on_parity.py @@ -2,7 +2,8 @@ """Compare a player's right click on a block with vanilla's, for items that are both food and blocks (sweet and glow berries, carrots, potatoes), for sweet berry bushes, for spawn eggs used on a monster spawner, for shears carving -pumpkins, for respawn anchors and for beds in the Nether and the End. +pumpkins, for blocks placed where mobs and armor stands stand, for respawn +anchors and for beds in the Nether and the End. Every case has a block at the origin, something above it, and a player three blocks south, survival or creative, hungry or full, sneaking or not, who @@ -179,6 +180,31 @@ def pumpkin_case(rng, states, case): case['cells'] = cells +# Entities a block may be placed into (`Entity.blocksBuilding`), and blocks to place. +BLOCKERS = ['skeleton', 'zombie', 'pig', 'chicken', 'cow', 'armor_stand', 'slime', 'bat'] +PLACED = ['obsidian', 'stone', 'oak_slab', 'oak_fence', 'white_carpet', 'torch', 'glass', 'oak_trapdoor', 'snow'] + + +def blocked_case(rng, states, case): + """A block placed on top of the origin where a mob or an armor stand stands, in its box or beside it + (`BlockItem.canPlace`'s `Level.isUnobstructed`).""" + case['cells'] = [[0, 0, 0, states.get(rng.choice(['stone', 'grass_block', 'dirt']))]] + entities = [] + for _ in range(rng.choice([1, 1, 2])): + kind = rng.choice(BLOCKERS) + offset = [rng.choice([0, 0, 0.3, -0.45, 0.7, -1.2]), rng.choice([0, 0, 0.5, 0.95, 1.02, -0.5]), + rng.choice([0, 0, 0.3, -0.6, 0.9])] + e = {'type': f'minecraft:{kind}', 'pos': [0.5 + offset[0], 1 + offset[1], 0.5 + offset[2]]} + if kind == 'armor_stand' and rng.random() < 0.3: + e['marker'] = True + if kind in ('pig', 'cow', 'chicken') and rng.random() < 0.3: + e['baby'] = True + entities.append(e) + case['entities'] = entities + case['use_item'] = {'item': d_plain(rng.choice(PLACED), rng.choice([1, 16])), 'creative': rng.random() < 0.3, + 'sneaking': False, 'food': 20} + + def cases(facts, count, seed): rng = random.Random(seed) states = d.States(facts) @@ -204,6 +230,12 @@ def cases(facts, count, seed): case['mode'] = 'dispense' yield case continue + if rng.random() < 0.1: + blocked_case(rng, states, case) + case['items'] = [] + case['mode'] = 'dispense' + yield case + continue if rng.random() < 0.12: pumpkin_case(rng, states, case) case['items'] = [] @@ -261,6 +293,8 @@ def kind(case, facts): return f'respawn at {first}' item = case['use_item']['item'] item = item.split('"')[1].split(':')[1] if item != '-' else 'empty hand' + if 'entities' in case: + return f'{item} placed by ' + ', '.join(e['type'].split(':')[1] for e in case['entities']) return f'{item} on {first}'