Merge origin/main into fix/enchanted-item-trigger

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-27 11:56:59 +01:00
29 changed files with 2427 additions and 47 deletions
+141 -5
View File
@@ -54,6 +54,27 @@ fn strider_liquid_shape() -> VoxelShape {
VoxelShape::from_cube([1, 2, 1], &[true, false]).expect("a valid half-block column")
}
// Vanilla scopes this direction to Entity.move while a piston pushes it.
thread_local! {
static PISTON_NOCLIP: std::cell::Cell<Option<horizon_core::geometry::Direction>> = const { std::cell::Cell::new(None) };
}
/// Suppress the moving part of pistons moving in this direction during a push.
pub fn with_piston_noclip<T>(
direction: horizon_core::geometry::Direction,
f: impl FnOnce() -> T,
) -> T {
struct Reset(Option<horizon_core::geometry::Direction>);
impl Drop for Reset {
fn drop(&mut self) {
PISTON_NOCLIP.with(|value| value.set(self.0));
}
}
let previous = PISTON_NOCLIP.with(|value| value.replace(Some(direction)));
let _reset = Reset(previous);
f()
}
pub struct SceneCollisions<'w, 'r> {
pub world: &'w World,
pub binding: &'w BlockCollisionRegistry<'r>,
@@ -140,8 +161,21 @@ impl<W: EntityMovementWorld<Error = io::Error>> EntityMovementWorld for EntityCo
border.collision_shape().map(Some).map_err(io::Error::other)
}
}
impl EntityCollisions<'_, SceneCollisions<'_, '_>> {
/// `SceneCollisions::resolve` that also stops at the entity boxes.
impl<'a, W> EntityCollisions<'a, W>
where
W: BlockCollisionWorld<Error = io::Error> + EntityMovementWorld<Error = io::Error>,
{
/// `EntityCollisions` for a body the world border also stops: the border
/// and the body's own x and z (`WorldBorder.isInsideCloseToBorder`).
pub fn with_border(blocks: W, border: &'a BorderGeometry, at: [f64; 2]) -> Self {
Self {
blocks,
entities: &[],
border: Some((border, at)),
}
}
/// `SceneCollisions::resolve` that also stops at the entity boxes and,
/// with a border, at the world border's walls.
pub fn resolve(
&mut self,
bounds: Aabb,
@@ -149,12 +183,11 @@ impl EntityCollisions<'_, SceneCollisions<'_, '_>> {
max_step: f32,
on_ground: bool,
) -> Result<Vec3> {
let context = self.blocks.context(bounds);
Ok(resolve_world_movement(
self,
MovementRequest {
entity: EntityIdentity(0),
context,
context: CollisionContext::position(bounds.min_position().y),
bounds,
movement,
max_step,
@@ -324,7 +357,7 @@ impl DynamicSupportWorld for SceneCollisions<'_, '_> {
return Ok(None);
};
pistons
.collision_shape(self, p, piston, None)
.collision_shape(self, p, piston, PISTON_NOCLIP.with(|value| value.get()))
.map(Some)
.map_err(io::Error::other)
}
@@ -388,6 +421,42 @@ impl PowderSnowWorld for SceneCollisions<'_, '_> {
}
}
/// A collision world without blocks: no chunk is resident, so only the
/// world border stops a body.
pub struct EmptyCollisions;
impl BlockCollisionWorld for EmptyCollisions {
type Error = io::Error;
type Chunk = ChunkPos;
fn collision_chunk(&mut self, _: ChunkPos) -> io::Result<Option<ChunkPos>> {
Ok(None)
}
fn collision_block(&mut self, _: ChunkPos, _: BlockPos) -> io::Result<CollisionBlock> {
Err(io::Error::other("no chunk is resident"))
}
fn is_suffocating(&mut self, _: ChunkPos, _: CollisionBlock, _: BlockPos) -> io::Result<bool> {
Ok(false)
}
fn context_shape(
&mut self,
_: CollisionBlock,
_: BlockPos,
_: &CollisionContext<'_>,
) -> io::Result<VoxelShape> {
Ok(VoxelShape::empty())
}
}
impl EntityMovementWorld for EmptyCollisions {
type Error = io::Error;
fn entity_collisions(&mut self, _: EntityIdentity, _: Aabb) -> io::Result<Vec<VoxelShape>> {
Ok(Vec::new())
}
fn movement_border(&mut self, _: EntityIdentity, _: Aabb) -> io::Result<Option<VoxelShape>> {
Ok(None)
}
}
/// Collision queries over any block lookup (test worlds without chunks):
/// every chunk is present, nothing is suffocating, no entities collide.
pub struct LookupCollisions<'w, 'r> {
@@ -475,3 +544,70 @@ impl PowderSnowWorld for LookupCollisions<'_, '_> {
Ok(false)
}
}
#[cfg(test)]
mod piston_noclip_tests {
use super::*;
use horizon_core::geometry::Direction;
#[test]
fn piston_noclip_is_scoped_and_restored_after_unwind() {
assert_eq!(PISTON_NOCLIP.with(|value| value.get()), None);
with_piston_noclip(Direction::East, || {
assert_eq!(
PISTON_NOCLIP.with(|value| value.get()),
Some(Direction::East)
);
let _ =
std::panic::catch_unwind(|| with_piston_noclip(Direction::West, || panic!("test")));
assert_eq!(
PISTON_NOCLIP.with(|value| value.get()),
Some(Direction::East)
);
});
assert_eq!(PISTON_NOCLIP.with(|value| value.get()), None);
}
}
#[cfg(test)]
mod border_movement_tests {
use super::*;
/// A border 10 wide around the origin: its wall's inner face is at x = 5.
fn border() -> BorderGeometry {
BorderGeometry::new([0.0, 0.0], 10.0, 29_999_984).expect("a border")
}
/// `WorldBorder.isInsideCloseToBorder` and `WorldBorder.getCollisionShape`:
/// a body inside close to the border collides with the border's walls.
#[test]
fn a_body_next_to_the_border_stops_at_its_wall() {
// A body 0.6 wide, 0.4 blocks short of the wall, moved east into it.
let bounds = Aabb::new(Vec3::new(4.0, 0.0, 0.0), Vec3::new(4.6, 1.8, 0.6));
let border = border();
let mut collisions = EntityCollisions::with_border(EmptyCollisions, &border, [4.3, 0.3]);
let moved = collisions
.resolve(bounds, Vec3::new(0.51, 0.0, 0.0), 0.0, false)
.expect("the movement resolves");
// The wall stops the body 0.4 blocks short of the whole move.
assert!(
(moved.x - 0.4).abs() < 1.0e-9,
"the body was not stopped by the border: {moved:?}"
);
}
/// Away from the border, the same body moves the whole way.
#[test]
fn a_body_away_from_the_border_moves_whole() {
let bounds = Aabb::new(Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.6, 1.8, 0.6));
let border = border();
let mut collisions = EntityCollisions::with_border(EmptyCollisions, &border, [0.3, 0.3]);
let moved = collisions
.resolve(bounds, Vec3::new(0.51, 0.0, 0.0), 0.0, false)
.expect("the movement resolves");
assert!(
(moved.x - 0.51).abs() < 1.0e-9,
"the body did not move the whole way: {moved:?}"
);
}
}
+7
View File
@@ -219,6 +219,13 @@ fn crumbling_uv(uv: vec2<f32>, packed_origin: vec2<f32>, sprite_size: vec2<f32>,
return vec4(mix(color.rgb, globals.fog_color.rgb, f * globals.fog_color.a), color.a);
}
// rendertype_outline.fsh: the vertex colour (the team colour; outline vertices carry no
// normal, so no cardinal light) wherever the texture is not fully clear.
@fragment fn fs_outline(in: Out) -> @location(0) vec4<f32> {
if textureSample(atlas, atlas_sampler, in.uv).a == 0.0 { discard; }
return vec4(in.color.rgb, 1.0);
}
fn celestial_color(in: Out) -> vec4<f32> {
return textureSample(atlas, atlas_sampler, in.uv) * in.color * in.light_color;
}
+28 -9
View File
@@ -65,6 +65,7 @@ mod functions;
mod game_mode;
mod gamemode_switcher;
mod ghasts;
mod glowing;
mod golems;
mod growth;
mod hand;
@@ -132,6 +133,7 @@ mod persist;
mod perspective;
mod pickups;
mod piglins;
mod piston_push;
mod player_equip;
mod postprocess;
mod potions;
@@ -1388,6 +1390,8 @@ struct Game<'a> {
/// Runs of `entity_vertices` by entity, for a shader pack's per-entity draws; recorded only
/// while `pack_ids` is set (game/shader_packs.rs).
entity_spans: std::cell::RefCell<Vec<horizon_client::gfx::pack_draw::EntitySpan>>,
/// The glowing entities' silhouettes this frame (game/glowing.rs).
glowing_vertices: std::cell::RefCell<Vec<Vertex>>,
/// The shader pack's entity and block entity ids, while one draws entities.
pack_ids: Option<shader_packs::PackIds>,
started: Instant,
@@ -2981,20 +2985,29 @@ impl Game<'_> {
b.run_block_events(level, ticks);
b.tick_block_entities(level, ticks);
});
// `PistonMovingBlockEntity.tick` pushes what its block just ran into,
// with the progress it is about to take.
let pushes = std::mem::take(&mut self.block_ticks.piston_pushes);
self.push_piston_entities(pushes);
self.process_level_events()?;
self.sync_moving_pistons();
self.process_level_events()
Ok(())
}
/// Copies the moving-piston block entities into the world, where the
/// collision queries and the renderer read them.
fn sync_moving_pistons(&mut self) {
let pistons = &mut self.world.moving_pistons;
pistons.clear();
pistons.extend(
self.block_ticks
.moving_pistons
.iter()
.map(|(p, piston)| ([p.x, p.y, p.z], piston.shape_state())),
);
let previous = std::mem::take(&mut self.world.moving_pistons);
let current: Vec<_> = self
.block_ticks
.moving_pistons
.iter()
.map(|(p, piston)| ([p.x, p.y, p.z], piston.shape_state()))
.collect();
self.world.moving_piston_fallback = previous
.into_iter()
.filter(|(pos, _)| !current.iter().any(|(next, _)| next == pos))
.collect();
self.world.moving_pistons = current;
}
/// `ServerLevel.tick`'s scheduled block then fluid ticks, one at a time:
/// the side effects of each (a shrieker summoning a warden, drops, game
@@ -11283,6 +11296,7 @@ impl Game<'_> {
let mut entities = std::mem::take(&mut self.entity_vertices);
entities.clear();
self.see_through_vertices.clear();
self.glowing_vertices.get_mut().clear();
if !title {
entities = horizon_client::spans::time("entity_geometry", || {
self.entity_geometry(camera, alpha, entities)
@@ -11350,6 +11364,7 @@ impl Game<'_> {
// map_patches any earlier leaves them a frame behind.
hud.patches.append(&mut self.map_patches);
let entity_spans = std::mem::take(self.entity_spans.get_mut());
let glowing = std::mem::take(self.glowing_vertices.get_mut());
let frame = Frame {
weather: &weather,
weather_time: horizon_client::weather::Time::new(self.world.game_time, alpha as f32),
@@ -11416,6 +11431,7 @@ impl Game<'_> {
}),
entities: &entities,
entity_spans: &entity_spans,
glowing: &glowing,
dynamic_lights: &self.dynamic_lights,
lightning: horizon_client::gfx::lightning::Input {
bolts: &bolts,
@@ -11471,6 +11487,7 @@ impl Game<'_> {
// Restore storage even when rendering fails (for example, a lost surface).
self.entity_vertices = entities;
*self.entity_spans.get_mut() = entity_spans;
*self.glowing_vertices.get_mut() = glowing;
self.see_through_vertices = see_through;
result
}
@@ -13186,6 +13203,7 @@ impl<'a> Game<'a> {
luminance: None,
see_through_vertices: Vec::new(),
entity_spans: Default::default(),
glowing_vertices: Default::default(),
pack_ids: None,
health_hud: hud_layout::HealthHud::default(),
started: now,
@@ -14180,6 +14198,7 @@ fn loading_frame(hud: &HudBatch) -> Frame<'_> {
nether_lighting: false,
entities: &[],
entity_spans: &[],
glowing: &[],
dynamic_lights: &[],
clouds: None,
lightning: Default::default(),
+25 -3
View File
@@ -3591,8 +3591,10 @@ impl Game<'_> {
let scale = self.view_scale();
for mob in &self.mobs {
let entity_start = out.len();
// Entity.DATA_SHARED_FLAGS_ID invisible: only the name tag draws.
if mob.invisible {
let outline = self.mob_outline(mob);
// Entity.DATA_SHARED_FLAGS_ID invisible: only the name tag draws (and
// the outline of a glowing mob, LivingEntityRenderer.getRenderType).
if mob.invisible && outline.is_none() {
continue;
}
let d = glam::DVec3::new(mob.position.x, mob.position.y, mob.position.z) - camera;
@@ -3661,9 +3663,18 @@ impl Game<'_> {
out,
);
self.mob_block_geometry(mob, camera, alpha, packed, out);
if let Some(rgb) = outline {
self.mark_outline(&out[entity_start..], rgb);
if mob.invisible {
out.truncate(entity_start);
continue;
}
}
// Glowing outlines and a guardian's beam remain visible independently
// of whether the body is hidden behind terrain.
if !mob.glowing && horizon_client::mobs::guardian::beam(mob, alpha as f32).is_none() {
if outline.is_none()
&& horizon_client::mobs::guardian::beam(mob, alpha as f32).is_none()
{
self.cull_entity_vertices(out, entity_start);
}
// GuardianRenderer: the laser to the locked target.
@@ -3711,7 +3722,15 @@ impl Game<'_> {
super::pickups::Taken::Arrow(a) => Some((&**a, super::pickups::position(p, alpha))),
_ => None,
});
// A glowing projectile's outline: what it drew up to the next one.
let mut glowing = None;
for (a, at) in live.chain(taken) {
if let Some((start, rgb)) = glowing.take() {
self.mark_outline(&out[start..], rgb);
}
if a.id != 0 && self.entity_glowing(a.id) {
glowing = Some((out.len(), super::glowing::team_color(self.mob_team(a.id))));
}
let p = glam::DVec3::from_array(at) - camera;
let c = p.as_vec3();
let light = self.world.light(
@@ -3843,6 +3862,9 @@ impl Game<'_> {
});
}
}
if let Some((start, rgb)) = glowing {
self.mark_outline(&out[start..], rgb);
}
}
}
/// `Bucketable.getBucketItemStack` and `getPickupSound` of a mob that a water
+53
View File
@@ -0,0 +1,53 @@
//! Glowing entities (`EntityRenderer.extractRenderState`'s outlineColor): which entities
//! appear glowing, in which team colour, and their silhouettes for the renderer's entity
//! outline (`OutlineBufferSource`).
use super::{Game, Vertex};
use horizon_client::{mobs::Mob, scoreboard as sb};
/// `Entity.getTeamColor`: the team's colour, else white.
pub(super) fn team_color(team: Option<&sb::Team>) -> u32 {
team.and_then(|t| t.color.as_deref())
.and_then(sb::team_color_rgb)
.unwrap_or(0xFF_FFFF)
}
impl Game<'_> {
/// `Entity.isCurrentlyGlowing` of an entity that is not living (an item, a projectile):
/// its `Glowing` tag, or shared flag 6 as a server sent it.
pub(super) fn entity_glowing(&self, id: u32) -> bool {
self.remote_glowing(id).unwrap_or_else(|| {
self.persist_raw(id)
.and_then(|t| horizon_client::save::number(t.get("Glowing")))
.is_some_and(|b| b != 0)
})
}
/// `Minecraft.shouldEntityAppearGlowing` of a mob or another player, with its outline
/// colour (`getTeamColor`; another player's team goes by name).
pub(super) fn mob_outline(&self, m: &Mob) -> Option<u32> {
if !m.is_currently_glowing() {
return None;
}
let team = if &*m.kind.id == "minecraft:player" {
let names = self.remote_player_names();
let name = names.into_iter().find(|(id, _)| *id == m.id);
name.and_then(|(_, name)| self.score.board.players_team(&name))
} else {
self.mob_team(m.id)
};
Some(team_color(team))
}
/// Adds one glowing entity's geometry to the outline pass in `rgb`, the textures'
/// alpha kept (`OutlineBufferSource`'s team-coloured copy of each draw).
pub(super) fn mark_outline(&self, vertices: &[Vertex], rgb: u32) {
let color = horizon_client::mobs::render::abgr(rgb);
self.glowing_vertices
.borrow_mut()
.extend(vertices.iter().map(|v| Vertex {
color,
normal: [0; 4],
..*v
}));
}
}
+6 -1
View File
@@ -407,7 +407,12 @@ impl Game<'_> {
) {
self.submit_item_world(&state, copy, vertex_light(packed), out);
}
self.cull_entity_vertices(out, entity_start);
if self.entity_glowing(item.id) {
let rgb = super::glowing::team_color(self.mob_team(item.id));
self.mark_outline(&out[entity_start..], rgb);
} else {
self.cull_entity_vertices(out, entity_start);
}
}
}
}
+15 -3
View File
@@ -166,8 +166,11 @@ fn mob_meta(
defaults: &mut Vec<(u8, i32, Value)>,
) {
let kind = m.kind.id.as_str();
if m.fire_ticks > 0 {
meta.push((0, serializer::BYTE, Value::Byte(1)));
// Entity.DATA_SHARED_FLAGS_ID: on fire 0x01, glowing 0x40
// (LivingEntity.updateGlowingStatus).
let shared = i8::from(m.fire_ticks > 0) | i8::from(m.is_currently_glowing()) << 6;
if shared != 0 {
meta.push((0, serializer::BYTE, Value::Byte(shared)));
}
if let Some(name) = &m.custom_name {
meta.push((
@@ -964,6 +967,10 @@ impl Game<'_> {
e.position = [d.position.x, d.position.y, d.position.z];
e.on_ground = d.on_ground;
e.velocity = [d.velocity.x, d.velocity.y, d.velocity.z];
// Entity.DATA_SHARED_FLAGS_ID: glowing 0x40 (the Glowing tag).
if self.entity_glowing(d.id) {
e.meta.push((0, serializer::BYTE, Value::Byte(0x40)));
}
e.meta.push((8, serializer::ITEM_STACK, Value::Item(stack)));
}
for o in &self.orbs {
@@ -1620,7 +1627,12 @@ impl Game<'_> {
out,
);
self.held_item_geometry(m, camera, alpha, packed, true, out);
if !m.glowing {
// LivingEntity.isCurrentlyGlowing: the guest's Glowing effect.
let guest = host.hub.guest(*conn);
if m.glowing || guest.is_some_and(|g| g.effects.get("glowing").is_some()) {
let team = guest.and_then(|g| self.score.board.players_team(&g.name));
self.mark_outline(&out[entity_start..], super::glowing::team_color(team));
} else {
self.cull_entity_vertices(out, entity_start);
}
}
+1 -1
View File
@@ -73,7 +73,7 @@ pub(super) struct Minecart {
pub yaw: f32,
pub previous_yaw: f32,
pub flipped: bool,
on_ground: bool,
pub(super) on_ground: bool,
horizontal_collision: bool,
fall_distance: f64,
pub hurt_time: i32,
@@ -14,7 +14,9 @@ impl Game<'_> {
out: &mut Vec<Vertex>,
) {
let partial = alpha as f32;
for &(p, piston) in &self.world.moving_pistons {
let mut states = self.world.moving_pistons.clone();
states.extend(self.world.moving_piston_fallback.iter().copied());
for (p, piston) in states {
let Some((block, base)) = self.moving_piston_blocks(piston, partial) else {
continue;
};
@@ -452,6 +452,7 @@ impl Game<'_> {
let packed = (u32::from(light >> 4) * 16) | ((u32::from(light & 15) * 16) << 8);
let atlas = &self.gui.atlas;
let lookup = |name: &str| atlas.sprites.get(name).map(|r| [r.u0, r.v0, r.u1, r.v1]);
let start = out.len();
horizon_client::mobs::render::render_with(
m,
alpha as f32,
@@ -465,6 +466,15 @@ impl Game<'_> {
self.player_elytra_geometry(m, camera, alpha as f32, packed, out);
self.spin_attack_geometry(m, camera, alpha as f32, packed, out);
self.held_item_geometry(m, camera, alpha, packed, true, out);
// LivingEntity.isCurrentlyGlowing: the Glowing effect. A server's client is
// in its team by its profile name.
if self.potions.effects.get("glowing").is_some() {
let team = match &self.remote {
Some(r) => self.score.board.players_team(&r.profile_name),
None => self.player_team(),
};
self.mark_outline(&out[start..], super::glowing::team_color(team));
}
}
/// `extractEntityInInventoryFollowsMouse`: the player in the box
/// `[x0, x1) x [y0, y1)`, `size` pixels per block, turned toward the
@@ -0,0 +1,573 @@
//! `PistonMovingBlockEntity.moveCollidedEntities` and `moveStuckEntities` for
//! the entities the game moves itself: the players and the mobs.
//!
//! The geometry is in [`horizon_world::piston_push`]; this file supplies the
//! bodies and applies the movement the way `Entity.move(MoverType.PISTON, …)`
//! does: limited to 0.51 blocks per tick per axis (`Entity.limitPistonMovement`),
//! then run through the block collision scan, so a pushed entity stops at a
//! wall instead of passing through it.
//!
use super::Game;
use horizon_client::collisions::{EntityCollisions, SceneCollisions, with_piston_noclip};
use horizon_core::geometry::{Aabb, Direction, Vec3, border::BorderGeometry};
use horizon_core::position::BlockPos;
use horizon_world::{
block_collision_scan::BlockCollisionWorld, level::pistons::MovingPiston,
piston_movement::PistonMovement, piston_push, world_movement::EntityMovementWorld,
};
/// What one piston asks for this tick: where it is, its state, and the progress
/// `moveCollidedEntities` runs at (before the tick stores it).
pub(super) type Push = (BlockPos, MovingPiston, f32);
/// Which entity was pushed, so its storage can be reached after the world
/// borrow ends.
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
enum Target {
Player,
Guest(u32),
Mob(usize),
Item(usize),
Boat(usize),
Cart(usize),
ArmorStand(usize),
}
/// `Entity.getStepHeight` of each body, read before the push.
struct StepHeights {
player: f32,
guests: std::collections::HashMap<u32, f32>,
}
/// What the push needs from one entity.
#[derive(Clone, Copy)]
struct Body {
bounds: Aabb,
position: Vec3,
on_ground: bool,
step_height: f32,
/// Standing on the piston's own block (`Entity.isSupportedBy`).
supported: bool,
}
impl Game<'_> {
/// Push whatever this tick's moving pistons ran into, in the order the
/// pistons ticked: vanilla pushes from each block entity's own tick.
pub(super) fn push_piston_entities(&mut self, pushes: Vec<Push>) {
let mut accumulators = std::collections::HashMap::new();
for push in pushes {
self.push_for_piston(push, &mut accumulators);
}
}
fn push_for_piston(
&mut self,
(pos, piston, new_progress): Push,
accumulators: &mut std::collections::HashMap<Target, PistonMovement>,
) {
let delta_progress = f64::from(new_progress - piston.progress);
if delta_progress <= 0.0 {
return;
}
let Some(bindings) = self.collision.pistons() else {
return;
};
let Ok(state) = bindings.collision_related_state(piston.shape_state()) else {
return;
};
let mut scene = SceneCollisions {
world: &self.world,
binding: self.collision,
subject: None,
};
let Ok(shape) = self
.collision
.collision_shape(&mut scene, state, [pos.x, pos.y, pos.z])
else {
return;
};
if shape.is_empty() {
return;
}
let movement = piston.movement_direction();
let boxes: Vec<Aabb> = shape.boxes().to_vec();
let facts = self.res.facts();
let slime = &*facts.get(piston.moved_state).name == "minecraft:slime_block";
let honey = &*facts.get(piston.moved_state).name == "minecraft:honey_block";
// `moveStuckEntities` asks the moved state's own top for its box.
let sticky_top = if honey {
let mut scene = SceneCollisions {
world: &self.world,
binding: self.collision,
subject: None,
};
self.collision
.collision_shape(&mut scene, piston.moved_state, [pos.x, pos.y, pos.z])
.ok()
.and_then(|s| s.bounds())
.map_or(0.0, |b| b.max_position().y)
} else {
0.0
};
let steps = self.push_step_heights();
let search = shape.bounds().map(|bounds| {
piston_push::movement_search_area(
piston_push::move_by_position_and_progress(pos, bounds, piston.shape_state()),
movement,
delta_progress,
)
});
let targets = self.push_targets();
for &target in &targets {
let Some(body) = self.push_body(target, pos, &steps) else {
continue;
};
let amount = piston_push::collided_amount(
&boxes,
pos,
piston.shape_state(),
new_progress,
movement,
body.bounds,
);
if slime && search.is_some_and(|area| area.intersects(body.bounds)) {
self.bounce(target, movement);
}
if amount > 0.0 {
let step = piston_push::step_vector(movement);
let before = body.bounds;
self.move_pushed(
target,
before,
[body.position.x, body.position.z],
body.step_height,
body.on_ground,
step.scale(amount),
movement,
accumulators,
);
// `fixEntityWithinPistonBase`: a retracting source piston drags
// an entity that is still inside its own block.
if !piston.extending
&& piston.source
&& let Some(after) = self.push_body(target, pos, &steps)
&& let Some(fix) = piston_push::fix_within_piston_base(
pos,
after.bounds,
movement,
delta_progress,
)
{
let back = piston_push::step_vector(movement.opposite());
self.move_pushed(
target,
after.bounds,
[after.position.x, after.position.z],
after.step_height,
after.on_ground,
back.scale(fix),
movement,
accumulators,
);
}
}
}
// Vanilla selects honey passengers after all collided entities have moved.
if honey && movement.normal()[1] == 0 {
let area = piston_push::sticky_area(pos, piston.shape_state(), sticky_top);
for target in targets {
let Some(body) = self.push_body(target, pos, &steps) else {
continue;
};
if piston_push::sticky_entity_selected(
area,
body.bounds,
horizon_world::level::data::PushReaction::PushPull,
body.on_ground,
body.supported,
body.position,
) {
self.move_pushed(
target,
body.bounds,
[body.position.x, body.position.z],
body.step_height,
body.on_ground,
piston_push::step_vector(movement).scale(delta_progress),
movement,
accumulators,
);
}
}
}
}
/// `Entity.getStepHeight` for each body, read once: the attribute lookup
/// needs the mutable body, and the push only needs the number.
fn push_step_heights(&mut self) -> StepHeights {
let player = self
.player
.as_mut()
.and_then(|p| p.attribute("minecraft:step_height").ok())
.map_or(0.6, |v| v as f32);
let mut guests = std::collections::HashMap::new();
if let Some(host) = self.lan.as_mut() {
for g in &mut host.hub.guests {
let step = g
.motion
.as_mut()
.and_then(|m| m.attribute("minecraft:step_height").ok())
.map_or(0.6, |v| v as f32);
guests.insert(g.conn, step);
}
}
StepHeights { player, guests }
}
/// The entities a moving block can run into: the window's player, the LAN
/// guests in the level, then the mobs.
fn push_targets(&self) -> Vec<Target> {
let mut targets = Vec::new();
if self.player.is_some() {
targets.push(Target::Player);
}
if let Some(host) = self.lan.as_ref() {
targets.extend(
host.hub
.guests
.iter()
.filter(|g| host.hub.here(g) && g.loaded && !g.dead)
.map(|g| Target::Guest(g.conn)),
);
}
targets.extend((0..self.mobs.len()).map(Target::Mob));
targets.extend((0..self.dropped.len()).map(Target::Item));
targets.extend((0..self.boats.len()).map(Target::Boat));
targets.extend((0..self.carts.len()).map(Target::Cart));
targets.extend((0..self.armor_stands.len()).map(Target::ArmorStand));
targets
}
/// What the push needs from one entity, or `None` when it is not pushed:
/// removed, dead, a spectator, or an armor-stand marker.
fn push_body(&self, target: Target, pos: BlockPos, steps: &StepHeights) -> Option<Body> {
let at = [pos.x, pos.y, pos.z];
match target {
Target::Player => {
let p = self.player.as_ref()?;
if p.spectator {
return None;
}
Some(Body {
bounds: p.bounds(),
position: p.position,
on_ground: p.ground.on_ground,
step_height: steps.player,
supported: p.ground.supporting_block == Some(pos),
})
}
Target::Guest(conn) => {
let host = self.lan.as_ref()?;
let g = host.hub.guests.iter().find(|g| g.conn == conn)?;
if g.dead || g.game_mode == 3 {
return None;
}
let p = g.position;
Some(Body {
bounds: Aabb::new(
Vec3::new(p[0] - 0.3, p[1], p[2] - 0.3),
Vec3::new(p[0] + 0.3, p[1] + 1.8, p[2] + 0.3),
),
position: Vec3::new(p[0], p[1], p[2]),
on_ground: g.on_ground,
step_height: steps.guests.get(&conn).copied().unwrap_or(0.6),
supported: g
.motion
.as_ref()
.is_some_and(|m| m.ground.supporting_block == Some(pos)),
})
}
Target::Mob(i) => {
let m = &self.mobs[i];
if m.removed || !m.alive() {
return None;
}
Some(Body {
bounds: m.bounds(),
position: m.position,
on_ground: m.on_ground,
step_height: m.kind.step_height as f32,
supported: m.supporting == Some(at),
})
}
Target::Item(i) => {
let entity = &self.dropped[i];
Some(Body {
bounds: entity.bounds(),
position: entity.position,
on_ground: entity.on_ground,
step_height: 0.0,
supported: false,
})
}
Target::Boat(i) => {
let entity = &self.boats[i];
Some(Body {
bounds: entity.bounds(),
position: entity.position,
on_ground: entity.on_ground,
step_height: 0.0,
supported: false,
})
}
Target::Cart(i) => {
let entity = &self.carts[i];
Some(Body {
bounds: entity.bounds(),
position: entity.position,
on_ground: entity.on_ground,
step_height: 0.0,
supported: false,
})
}
Target::ArmorStand(i) => {
let entity = &self.armor_stands[i];
if entity.marker {
return None;
}
Some(Body {
bounds: entity.bounds(),
position: entity.position,
on_ground: entity.on_ground,
step_height: 0.0,
supported: false,
})
}
}
}
/// `Entity.move(MoverType.PISTON, movement)` for one pushed entity: the
/// per-tick piston limit, then the block and world border collision scan.
fn move_pushed(
&mut self,
target: Target,
before: Aabb,
at: [f64; 2],
step_height: f32,
on_ground: bool,
movement: Vec3,
piston_direction: Direction,
accumulators: &mut std::collections::HashMap<Target, PistonMovement>,
) {
let Some(limited) = self.limit_piston(target, movement, accumulators) else {
return;
};
// `limitPistonMovement` returns zero once the tick's budget is spent.
if limited == Vec3::ZERO {
return;
}
let resolved = with_piston_noclip(piston_direction, || {
pushed_movement(
SceneCollisions {
world: &self.world,
binding: self.collision,
subject: None,
},
self.border.geometry(),
at,
before,
limited,
step_height,
on_ground,
)
.unwrap_or(Vec3::ZERO)
});
if resolved.length_squared() <= 1.0e-7
&& limited.length_squared() - resolved.length_squared() >= 1.0e-7
{
return;
}
self.apply_push(target, resolved);
}
/// `Entity.limitPistonMovement`: each axis may travel at most 0.51 blocks
/// per game tick, and a move touches only its first non-zero axis.
fn limit_piston(
&mut self,
target: Target,
movement: Vec3,
accumulators: &mut std::collections::HashMap<Target, PistonMovement>,
) -> Option<Vec3> {
let time = self.world.game_time;
let limit = |state: &mut PistonMovement| state.limit(movement, || Ok::<i64, ()>(time)).ok();
match target {
Target::Player => {
let p = self.player.as_mut()?;
limit(&mut p.piston)
}
Target::Guest(conn) => {
let host = self.lan.as_mut()?;
let g = host.hub.guests.iter_mut().find(|g| g.conn == conn)?;
limit(&mut g.motion.as_deref_mut()?.piston)
}
Target::Mob(i) => limit(&mut self.mobs[i].piston),
_ => limit(accumulators.entry(target).or_default()),
}
}
fn apply_push(&mut self, target: Target, movement: Vec3) {
match target {
Target::Player => {
if let Some(p) = self.player.as_mut() {
p.position = p.position.add_vector(movement);
}
}
Target::Guest(conn) => {
if let Some(host) = self.lan.as_mut()
&& let Some(g) = host.hub.guests.iter_mut().find(|g| g.conn == conn)
{
// The body and the hub's copy of its position move together:
// `leave_guest` writes the body back over the hub's.
for (slot, delta) in g.position.iter_mut().zip(movement.components()) {
*slot += delta;
}
if let Some(m) = g.motion.as_deref_mut() {
m.position = m.position.add_vector(movement);
}
}
}
Target::Mob(i) => {
let m = &mut self.mobs[i];
m.position = m.position.add_vector(movement);
}
Target::Item(i) => {
self.dropped[i].position = self.dropped[i].position.add_vector(movement)
}
Target::Boat(i) => self.boats[i].position = self.boats[i].position.add_vector(movement),
Target::Cart(i) => self.carts[i].position = self.carts[i].position.add_vector(movement),
Target::ArmorStand(i) => {
self.armor_stands[i].position = self.armor_stands[i].position.add_vector(movement)
}
}
}
/// `causeBounce`: a slime block sets the velocity along the movement axis.
fn bounce(&mut self, target: Target, movement: Direction) {
match target {
Target::Player => {
if let Some(p) = self.player.as_mut() {
p.velocity = piston_push::bounce_velocity(movement, p.velocity);
}
}
Target::Mob(i) => {
let m = &mut self.mobs[i];
m.velocity = piston_push::bounce_velocity(movement, m.velocity);
}
Target::Guest(conn) => {
if let Some(host) = self.lan.as_mut()
&& let Some(guest) = host.hub.guests.iter_mut().find(|g| g.conn == conn)
&& let Some(motion) = guest.motion.as_deref_mut()
{
motion.velocity = piston_push::bounce_velocity(movement, motion.velocity);
}
}
Target::Item(i) => {
self.dropped[i].velocity =
piston_push::bounce_velocity(movement, self.dropped[i].velocity)
}
Target::Boat(i) => {
self.boats[i].velocity =
piston_push::bounce_velocity(movement, self.boats[i].velocity)
}
Target::Cart(i) => {
self.carts[i].velocity =
piston_push::bounce_velocity(movement, self.carts[i].velocity)
}
Target::ArmorStand(i) => {
self.armor_stands[i].velocity =
piston_push::bounce_velocity(movement, self.armor_stands[i].velocity)
}
}
}
}
/// `Entity.move(MoverType.PISTON, movement)` for a pushed body: the movement
/// it makes. The world's blocks stop it, and so does the world border:
/// `Entity.collectCollidersIgnoringWorldBorder` adds the border's walls when
/// the body is inside close to the border, as ordinary movement does.
fn pushed_movement<W>(
blocks: W,
border: &BorderGeometry,
at: [f64; 2],
before: Aabb,
movement: Vec3,
step_height: f32,
on_ground: bool,
) -> horizon_client::Result<Vec3>
where
W: BlockCollisionWorld<Error = std::io::Error> + EntityMovementWorld<Error = std::io::Error>,
{
EntityCollisions::with_border(blocks, border, at).resolve(
before,
movement,
step_height,
on_ground,
)
}
#[cfg(test)]
mod tests {
use super::*;
use horizon_client::collisions::EmptyCollisions;
/// A border 10 wide around the origin: its wall's inner face is at x = 5.
fn border() -> BorderGeometry {
BorderGeometry::new([0.0, 0.0], 10.0, 29_999_984).expect("a border")
}
/// `Entity.collectCollidersIgnoringWorldBorder`: the border's walls stop a
/// pushed body that is inside close to the border.
#[test]
fn a_pushed_body_stops_at_the_world_border() {
// A body 0.6 wide, 0.4 blocks short of the wall, pushed east into it.
let before = Aabb::new(Vec3::new(4.0, 0.0, 0.0), Vec3::new(4.6, 1.8, 0.6));
let border = border();
let moved = pushed_movement(
EmptyCollisions,
&border,
[4.3, 0.3],
before,
Vec3::new(0.51, 0.0, 0.0),
0.0,
false,
)
.expect("the push resolves");
// The wall stops the body 0.4 blocks short of the whole push.
assert!(
(moved.x - 0.4).abs() < 1.0e-9,
"the body was not stopped by the border: {moved:?}"
);
}
/// Away from the border, the same push moves the whole way.
#[test]
fn a_pushed_body_moves_whole_away_from_the_border() {
let before = Aabb::new(Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.6, 1.8, 0.6));
let border = border();
let moved = pushed_movement(
EmptyCollisions,
&border,
[0.3, 0.3],
before,
Vec3::new(0.51, 0.0, 0.0),
0.0,
false,
)
.expect("the push resolves");
assert!(
(moved.x - 0.51).abs() < 1.0e-9,
"the body did not move the whole push: {moved:?}"
);
}
}
+31 -1
View File
@@ -186,6 +186,9 @@ pub(super) struct Remote {
online_mode: bool,
/// Player entities in the level: entity id to profile UUID.
level_players: HashMap<i32, u128>,
/// Entities with shared flag 6 (`Entity.isCurrentlyGlowing` on a client); mobs keep
/// theirs in `Mob::glowing`.
glowing: std::collections::HashSet<i32>,
/// The local player's profile UUID (always in the level).
profile: u128,
/// The local player's profile name: its score holder and team member.
@@ -483,6 +486,7 @@ impl Game<'_> {
listed: Default::default(),
online_mode: false,
level_players: HashMap::new(),
glowing: Default::default(),
profile: 0,
profile_name: String::new(),
destroying: None,
@@ -1495,7 +1499,9 @@ impl Game<'_> {
self.remove_remote_dragon(id);
self.remove_remote_object(id as u32);
self.remove_remote_projectile(id as u32);
self.persist.uuids.remove(&(id as u32));
if let Some(r) = self.remote.as_mut() {
r.glowing.remove(&id);
r.entities.remove(&id);
r.lerps.remove(&id);
r.stepped.remove(&id);
@@ -1812,6 +1818,11 @@ impl Game<'_> {
r.level_players.remove(&p.id);
}
}
// Entity.getTeam: a mob is on a team by its UUID.
self.persist.uuids.insert(
p.id as u32,
horizon_client::lan::playerdata::uuid_ints(p.uuid),
);
self.spawn_entity(p.id, &kind, p.position, rotation);
// Entity.recreateFromPacket: setDeltaMovement(packet.getMovement()).
self.remote_motion(p.id, p.velocity);
@@ -1930,7 +1941,10 @@ impl Game<'_> {
if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) {
for (index, _, value) in &p.values {
match (index, value) {
(0, Value::Byte(flags)) => m.invisible = flags & 0x20 != 0,
(0, Value::Byte(flags)) => {
m.invisible = flags & 0x20 != 0;
m.glowing = flags & 0x40 != 0;
}
(2, Value::OptText(name)) => {
let text = name.as_ref().map(horizon_client::entity_nbt::name_text);
m.custom_name =
@@ -1947,6 +1961,15 @@ impl Game<'_> {
}
}
}
if let Some((_, _, Value::Byte(flags))) = p.values.iter().find(|v| v.0 == 0)
&& let Some(r) = self.remote.as_mut()
{
if flags & 0x40 != 0 {
r.glowing.insert(p.id);
} else {
r.glowing.remove(&p.id);
}
}
self.remote_mob_data(p.id, &p.values);
if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32)
&& m.species() == horizon_client::mobs::Species::Creeper
@@ -3323,6 +3346,8 @@ impl Game<'_> {
self.with_level(|b, level, ticks| {
b.client_tick_block_entities(level, ticks);
});
let pushes = std::mem::take(&mut self.block_ticks.piston_pushes);
self.push_piston_entities(pushes);
self.sync_moving_pistons();
self.tick_hand();
self.tick_particles();
@@ -5428,6 +5453,11 @@ impl Game<'_> {
batch.text(atlas, message, (w - tw) / 2.0, 85.0, u32::MAX, true);
}
}
/// Shared flag 6 of an entity on a server's client (`None` without a server).
pub(super) fn remote_glowing(&self, id: u32) -> Option<bool> {
let r = self.remote.as_ref()?;
Some(r.glowing.contains(&(id as i32)))
}
/// Status for `horizon-ctl status`.
/// The other players' entities in the level and their names (from the
/// player list).
+28 -1
View File
@@ -35,6 +35,7 @@ fn entity_shader() -> String {
#[cfg(test)]
mod crumbling_tests;
mod entity_batch;
mod entity_outline;
mod weather;
use entity_batch::EntityBatch;
pub use entity_batch::{
@@ -265,6 +266,9 @@ pub struct Frame<'a> {
pub nether_lighting: bool,
/// Camera-relative entity geometry textured from the shared atlas.
pub entities: &'a [Vertex],
/// The glowing entities' silhouettes in their team colours (`OutlineBufferSource`),
/// outlined through walls after the hand.
pub glowing: &'a [Vertex],
/// Runs of `entities` by entity, while a shader pack draws them (`pack_draw::EntitySpan`).
pub entity_spans: &'a [pack_draw::EntitySpan],
pub weather: &'a [crate::weather::Vertex],
@@ -414,6 +418,8 @@ pub struct Renderer {
celestial_buffer: wgpu::Buffer,
end_sky: end_sky::EndSky,
stars: stars::Stars,
/// The glowing entities' outline (`LevelRenderer.entityOutlineTarget`).
entity_outline: entity_outline::EntityOutline,
panorama: panorama::Panorama,
/// `RenderPipelines.WORLD_BORDER` and its buffers.
border_pipeline: wgpu::RenderPipeline,
@@ -2345,6 +2351,11 @@ impl Renderer {
let uploads = upload::Uploads::new(&device);
let section_uploads = upload::Uploads::with_chunk(&device, SECTION_UPLOAD_CHUNK);
let stars = stars::Stars::new(&device, config.format.remove_srgb_suffix());
let entity_outline = entity_outline::EntityOutline::new(
&device,
(&entity_shader, &entity_layout, &entity_vertex),
config.format.remove_srgb_suffix(),
);
let panorama = panorama::Panorama::new(
&device,
&queue,
@@ -2417,6 +2428,7 @@ impl Renderer {
celestial_buffer,
end_sky: end_sky::EndSky::default(),
stars,
entity_outline,
panorama,
border_pipeline,
border_group,
@@ -4115,6 +4127,13 @@ impl Renderer {
bytemuck::cast_slice(all_entities),
);
}
self.entity_outline.prepare(
&self.device,
&mut self.uploads,
&mut encoder,
frame.glowing,
[self.config.width, self.config.height],
);
if let Some((rotation, brightness)) = frame.stars {
self.stars.prepare(
&mut self.uploads,
@@ -4142,7 +4161,8 @@ impl Renderer {
} else {
lightning::QUADS
})
.max(weather_quads);
.max(weather_quads)
.max(frame.glowing.len() as u32 / 4);
if max_quads > self.index_quads {
self.index_quads = max_quads.next_power_of_two();
self.indices = Self::quad_indices(&self.device, self.index_quads);
@@ -4884,6 +4904,13 @@ impl Renderer {
0..1,
);
}
// GameRenderer.render: LevelRenderer.blitEntityOutline after the level and the hand.
self.entity_outline.draw(
&mut encoder,
&view_target,
&self.entity_group,
&self.indices,
);
if !frame.screen.is_empty() {
let attachments = [Some(wgpu::RenderPassColorAttachment {
view: &view_target,
@@ -0,0 +1,311 @@
//! The entity outline (`LevelRenderer.entityOutlineTarget`): the glowing entities'
//! silhouettes in their team colours, drawn without depth so walls do not hide them
//! (`RenderTypes.outline`), the `entity_outline` post chain over them (`entity_sobel`, then
//! `entity_outline_box_blur` across and up), and `LevelRenderer.blitEntityOutline` onto
//! the frame after the hand.
use super::Vertex;
/// `entityOutlineTarget`'s `GpuFormat.RGBA8_UNORM`.
const FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8Unorm;
struct Targets {
size: [u32; 2],
/// `entityOutlineTarget`.
outline: wgpu::TextureView,
/// The chain's `swap` target.
swap: wgpu::TextureView,
/// What each pass reads: the outline (nearest), swap (bilinear), the outline
/// (bilinear), swap (nearest).
groups: [wgpu::BindGroup; 4],
}
pub(super) struct EntityOutline {
silhouette: wgpu::RenderPipeline,
/// Sobel, blur across, blur up (into `FORMAT`) and the blended blit (onto the frame).
passes: [wgpu::RenderPipeline; 4],
layout: wgpu::BindGroupLayout,
nearest: wgpu::Sampler,
bilinear: wgpu::Sampler,
vertices: wgpu::Buffer,
capacity: u64,
quads: u32,
targets: Option<Targets>,
}
impl EntityOutline {
/// `entity` draws the silhouettes with the entity shader's vertex stage and
/// `fs_outline`; `format` is the frame's.
pub(super) fn new(
device: &wgpu::Device,
entity: (
&wgpu::ShaderModule,
&wgpu::PipelineLayout,
&[Option<wgpu::VertexBufferLayout>],
),
format: wgpu::TextureFormat,
) -> Self {
let (entity_shader, entity_layout, entity_vertex) = entity;
// RenderPipelines.OUTLINE_NO_CULL: no depth test, the colour replaced.
let silhouette = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Entity outline silhouettes"),
layout: Some(entity_layout),
vertex: wgpu::VertexState {
module: entity_shader,
entry_point: Some("vs_entity"),
compilation_options: Default::default(),
buffers: entity_vertex,
},
primitive: Default::default(),
depth_stencil: None,
multisample: Default::default(),
fragment: Some(wgpu::FragmentState {
module: entity_shader,
entry_point: Some("fs_outline"),
compilation_options: Default::default(),
targets: &[Some(FORMAT.into())],
}),
multiview_mask: None,
cache: None,
});
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("Entity outline"),
source: wgpu::ShaderSource::Wgsl(include_str!("entity_outline.wgsl").into()),
});
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Entity outline bindings"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
],
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Entity outline"),
bind_group_layouts: &[Some(&layout)],
..Default::default()
});
let pass = |entry: &str, target: wgpu::ColorTargetState| {
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some(entry),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some("vs_quad"),
compilation_options: Default::default(),
buffers: &[],
},
primitive: Default::default(),
depth_stencil: None,
multisample: Default::default(),
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some(entry),
compilation_options: Default::default(),
targets: &[Some(target)],
}),
multiview_mask: None,
cache: None,
})
};
// BlendFunction.ENTITY_OUTLINE_BLIT over the colour only (write mask 7).
let blit = wgpu::ColorTargetState {
format,
blend: Some(wgpu::BlendState {
color: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
alpha: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::Zero,
dst_factor: wgpu::BlendFactor::One,
operation: wgpu::BlendOperation::Add,
},
}),
write_mask: wgpu::ColorWrites::COLOR,
};
let sampler = |label, filter| {
device.create_sampler(&wgpu::SamplerDescriptor {
label: Some(label),
mag_filter: filter,
min_filter: filter,
..Default::default()
})
};
Self {
silhouette,
passes: [
pass("fs_sobel", FORMAT.into()),
pass("fs_blur_x", FORMAT.into()),
pass("fs_blur_y", FORMAT.into()),
pass("fs_blit", blit),
],
layout,
nearest: sampler("Entity outline nearest", wgpu::FilterMode::Nearest),
bilinear: sampler("Entity outline bilinear", wgpu::FilterMode::Linear),
vertices: Self::vertex_buffer(device, 256),
capacity: 256,
quads: 0,
targets: None,
}
}
fn vertex_buffer(device: &wgpu::Device, capacity: u64) -> wgpu::Buffer {
device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Entity outline vertices"),
size: capacity * std::mem::size_of::<Vertex>() as u64,
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
})
}
/// Uploads this frame's silhouettes (`LevelRenderer.currentFrameRendersEntityOutline`
/// when there are any) and sizes the targets like the window.
pub(super) fn prepare(
&mut self,
device: &wgpu::Device,
uploads: &mut super::upload::Uploads,
encoder: &mut wgpu::CommandEncoder,
silhouettes: &[Vertex],
size: [u32; 2],
) {
self.quads = (silhouettes.len() / 4) as u32;
if self.quads == 0 {
return;
}
let vertices = silhouettes.len() as u64;
if vertices > self.capacity {
self.capacity = vertices.next_power_of_two();
self.vertices = Self::vertex_buffer(device, self.capacity);
}
uploads.buffer(
encoder,
&self.vertices,
0,
bytemuck::cast_slice(silhouettes),
);
if self.targets.as_ref().is_some_and(|t| t.size == size) {
return;
}
let texture = |label| {
device
.create_texture(&wgpu::TextureDescriptor {
label: Some(label),
size: wgpu::Extent3d {
width: size[0],
height: size[1],
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: FORMAT,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
})
.create_view(&Default::default())
};
let outline = texture("Entity outline");
let swap = texture("Entity outline swap");
let group = |view: &wgpu::TextureView, sampler: &wgpu::Sampler| {
device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Entity outline pass"),
layout: &self.layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(sampler),
},
],
})
};
let groups = [
group(&outline, &self.nearest),
group(&swap, &self.bilinear),
group(&outline, &self.bilinear),
group(&swap, &self.nearest),
];
self.targets = Some(Targets {
size,
outline,
swap,
groups,
});
}
/// `LevelRenderer.executeOutline`, the post chain and `blitEntityOutline` onto `frame`.
pub(super) fn draw(
&self,
encoder: &mut wgpu::CommandEncoder,
frame: &wgpu::TextureView,
entity_group: &wgpu::BindGroup,
indices: &wgpu::Buffer,
) {
let Some(t) = self.targets.as_ref().filter(|_| self.quads > 0) else {
return;
};
let clear = wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT);
{
let mut pass = begin(encoder, "Entity outline", &t.outline, clear);
pass.set_pipeline(&self.silhouette);
pass.set_bind_group(0, entity_group, &[]);
pass.set_index_buffer(indices.slice(..), wgpu::IndexFormat::Uint32);
pass.set_vertex_buffer(0, self.vertices.slice(..));
pass.draw_indexed(0..self.quads * 6, 0, 0..1);
}
// post_effect/entity_outline.json; its last pass only copies swap back to the
// outline target, so the blit reads swap.
let steps = [
("Entity outline sobel", &t.swap, clear),
("Entity outline blur across", &t.outline, clear),
("Entity outline blur up", &t.swap, clear),
("Entity outline blit", frame, wgpu::LoadOp::Load),
];
for (i, (label, view, load)) in steps.into_iter().enumerate() {
let mut pass = begin(encoder, label, view, load);
pass.set_pipeline(&self.passes[i]);
pass.set_bind_group(0, &t.groups[i], &[]);
pass.draw(0..3, 0..1);
}
}
}
fn begin<'e>(
encoder: &'e mut wgpu::CommandEncoder,
label: &str,
view: &wgpu::TextureView,
load: wgpu::LoadOp<wgpu::Color>,
) -> wgpu::RenderPass<'e> {
let attachments = [Some(wgpu::RenderPassColorAttachment {
view,
resolve_target: None,
depth_slice: None,
ops: wgpu::Operations {
load,
store: wgpu::StoreOp::Store,
},
})];
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some(label),
color_attachments: &attachments,
..Default::default()
})
}
@@ -0,0 +1,60 @@
// The entity_outline post chain (post_effect/entity_outline.json) over the silhouettes of
// the glowing entities, then RenderTarget.blitAndBlendToTexture onto the frame.
@group(0) @binding(0) var source: texture_2d<f32>;
@group(0) @binding(1) var source_sampler: sampler;
struct PostOut {
@builtin(position) clip: vec4<f32>,
@location(0) uv: vec2<f32>,
};
// screenquad.vsh, with v running down the rows as wgpu's textures do.
@vertex fn vs_quad(@builtin(vertex_index) i: u32) -> PostOut {
let p = vec2<f32>(f32((i << 1u) & 2u), f32(i & 2u));
var out: PostOut;
out.clip = vec4(p * 2.0 - 1.0, 0.0, 1.0);
out.uv = vec2(p.x, 1.0 - p.y);
return out;
}
fn tap(uv: vec2<f32>) -> vec4<f32> {
return textureSampleLevel(source, source_sampler, uv, 0.0);
}
// entity_sobel.fsh: alpha marks where coverage changes, the colour is the mean of the
// covered texels around.
@fragment fn fs_sobel(in: PostOut) -> @location(0) vec4<f32> {
let texel = 1.0 / vec2<f32>(textureDimensions(source));
let center = tap(in.uv);
let left = tap(in.uv - vec2(texel.x, 0.0));
let right = tap(in.uv + vec2(texel.x, 0.0));
let up = tap(in.uv - vec2(0.0, texel.y));
let down = tap(in.uv + vec2(0.0, texel.y));
let total = clamp(abs(center.a - left.a) + abs(center.a - right.a)
+ abs(center.a - up.a) + abs(center.a - down.a), 0.0, 1.0);
let color = center.rgb * center.a + left.rgb * left.a + right.rgb * right.a
+ up.rgb * up.a + down.rgb * down.a;
return vec4(color * 0.2, total);
}
// entity_outline_box_blur.fsh at Radius 2: bilinear taps at -1.5 and 0.5 texels and half
// a tap at 2; the colour averaged, the alpha summed.
fn blur(uv: vec2<f32>, step: vec2<f32>) -> vec4<f32> {
let blurred = tap(uv + step * -1.5) + tap(uv + step * 0.5) + tap(uv + step * 2.0) / 2.0;
return vec4(blurred.rgb / 2.5, blurred.a);
}
// BlurDir (1, 0).
@fragment fn fs_blur_x(in: PostOut) -> @location(0) vec4<f32> {
return blur(in.uv, vec2(1.0 / f32(textureDimensions(source).x), 0.0));
}
// BlurDir (0, 1): up the screen in GL's bottom-up textures, towards row 0 here.
@fragment fn fs_blur_y(in: PostOut) -> @location(0) vec4<f32> {
return blur(in.uv, vec2(0.0, -1.0 / f32(textureDimensions(source).y)));
}
// blit_screen.fsh, blended by RenderPipelines.ENTITY_OUTLINE_BLIT.
@fragment fn fs_blit(in: PostOut) -> @location(0) vec4<f32> {
return tap(in.uv);
}
+27
View File
@@ -796,6 +796,33 @@ pub fn cluster_poses(state: &ItemRenderState, pose: Mat4, amount: u32, seed: i64
mod tests {
use super::*;
/// A bad item model surfaces only once items.json has loaded, so a broken
/// items.json still reports itself first.
#[test]
fn item_model_errors_wait_for_the_item_definitions() -> crate::Result<()> {
let root =
std::env::temp_dir().join(format!("horizon-item-model-errors-{}", std::process::id()));
let model = root.join("assets/horizon/models/INVALID.json");
fs::create_dir_all(model.parent().unwrap())?;
fs::write(model, "{}")?;
let library = crate::model_library::ParsedModels::load(&root).into_item_library();
assert!(library.is_err());
fs::write(root.join("items.json"), "{")?;
let load = || {
ItemRenderer::load_with_library(&root, library.clone())
.err()
.unwrap()
.to_string()
};
let broken_items = load();
fs::write(root.join("items.json"), "{}")?;
let bad_model = load();
let _ = fs::remove_dir_all(&root);
assert!(!broken_items.starts_with("item models:"));
assert!(bad_model.starts_with("item models:"));
Ok(())
}
#[test]
fn rendered_amounts_follow_the_count_bands() {
let bands: Vec<u32> = [0, 1, 2, 16, 17, 32, 33, 48, 49, 64]
+9
View File
@@ -845,6 +845,9 @@ pub struct Mob {
pub previous_swell: i32,
pub ambient_timer: i32,
pub fall_distance: f64,
/// `Entity.pistonDeltas` and `pistonDeltasGameTime`: how far a moving
/// block has pushed this mob this tick (0.51 at most on any axis).
pub piston: horizon_world::piston_movement::PistonMovement,
pub persistent: bool,
/// `CustomName` (plain text) and `CustomNameVisible`.
pub custom_name: Option<String>,
@@ -1697,6 +1700,7 @@ impl Mob {
// Mob.ambientSoundTime starts at 0.
ambient_timer: 0,
fall_distance: 0.0,
piston: Default::default(),
persistent: false,
random,
ai: Box::default(),
@@ -2008,6 +2012,11 @@ impl Mob {
pub fn has_effect(&self, effect: &str) -> bool {
self.extra.effects.iter().any(|e| e.0 == effect)
}
/// `LivingEntity.isCurrentlyGlowing`: the Glowing effect or the `Glowing` tag (on a
/// server's client, `glowing` holds shared flag 6 instead).
pub fn is_currently_glowing(&self) -> bool {
self.glowing || self.has_effect("minecraft:glowing")
}
pub fn add_effect(&mut self, effect: &'static str, ticks: i32, amplifier: i32) {
if let Some(e) = self.extra.effects.iter_mut().find(|e| e.0 == effect) {
// MobEffectInstance.update: a stronger effect replaces the old one
+1 -12
View File
@@ -232,18 +232,7 @@ mod tests {
.block_library(&fixture.0, vec!["horizon:nested/child".into()])
.is_ok()
);
let library = parsed.into_item_library();
assert!(library.is_err());
fs::write(fixture.0.join("items.json"), "{")?;
let load = || {
crate::item_render::ItemRenderer::load_with_library(&fixture.0, library.clone())
.err()
.unwrap()
.to_string()
};
assert!(!load().starts_with("item models:"));
fs::write(fixture.0.join("items.json"), "{}")?;
assert!(load().starts_with("item models:"));
assert!(parsed.into_item_library().is_err());
Ok(())
}
}
+12 -2
View File
@@ -259,6 +259,9 @@ pub struct PlayerMotion {
pub history: MovementHistory,
pub fall_distance: f64,
pub stuck_multiplier: Vec3,
/// `Entity.pistonDeltas` and `pistonDeltasGameTime`: how far a moving
/// block has pushed this entity this tick (0.51 at most on any axis).
pub piston: horizon_world::piston_movement::PistonMovement,
pub yaw: f32,
pub head_yaw: f32,
pub jumping: bool,
@@ -472,6 +475,7 @@ impl PlayerMotion {
history: MovementHistory::default(),
fall_distance: 0.0,
stuck_multiplier: Vec3::ZERO,
piston: Default::default(),
yaw: 0.0,
head_yaw: 0.0,
jumping: false,
@@ -1907,8 +1911,14 @@ impl EntityMoveWorld for MotionWorld<'_, '_, '_> {
fn set_collision_flags(&mut self, value: CollisionFlags) {
self.player.collision = value;
}
fn limit_piston_movement(&mut self, _: Vec3) -> Result<Vec3> {
Err("piston movement requires a ticking entity runtime".into())
/// `Entity.limitPistonMovement`: the per-tick 0.51-block budget shared by
/// every move on an axis. A failed game-time read leaves the accumulator
/// as it was, as native does.
fn limit_piston_movement(&mut self, movement: Vec3) -> Result<Vec3> {
let time = self.scene.game_time;
self.player
.piston
.limit(movement, || Ok::<i64, Box<dyn std::error::Error>>(time))
}
fn stuck_multiplier(&self) -> Vec3 {
self.player.stuck_multiplier
+4 -1
View File
@@ -93,11 +93,13 @@ pub struct World {
pub light_log: Option<crate::fast_hash::FxHashMap<(i32, i32), u32>>,
/// The villager POIs of each section (`PoiManager`'s sections).
pub pois: crate::poi_sections::PoiSections<crate::mobs::villager_brain::Poi>,
/// Moving-piston states removed during the last tick; retained until the
/// final block mesh is visible.
pub moving_piston_fallback: Vec<([i32; 3], horizon_world::piston_shape::MovingPistonState)>,
/// The moving-piston block entities of the level, by position, copied
/// from the block ticks after each tick: collision and rendering read them.
pub moving_pistons: Vec<([i32; 3], horizon_world::piston_shape::MovingPistonState)>,
}
/// ServerLevel weather state (advanceWeatherCycle).
#[derive(Clone, Copy, Debug, Default)]
pub struct Weather {
pub clear_time: i32,
@@ -187,6 +189,7 @@ impl World {
weather: Weather::default(),
block_log: None,
light_log: None,
moving_piston_fallback: Vec::new(),
moving_pistons: Vec::new(),
pois: Default::default(),
}
@@ -137,6 +137,7 @@ mod movement_support_fixture;
mod object_open_map_fixture;
mod object_open_set_fixture;
mod piston_movement_fixture;
mod piston_push_fixture;
mod piston_shape_fixture;
mod placement_fixture;
mod player_equipment_fixture;
@@ -309,6 +310,7 @@ probes! {
object_open_map_fixture,
object_open_set_fixture,
piston_movement_fixture,
piston_push_fixture,
piston_shape_fixture,
placement_fixture,
player_equipment_fixture,
@@ -0,0 +1,163 @@
//! The moving-piston push maths against the real game: `PistonMath.getMovementArea`,
//! `moveByPositionAndProgress`, `getMovement`, the `moveCollidedEntities` loop
//! over the block's own boxes, and `moveStuckEntities`' box with
//! `matchesStickyCritera`.
use crate::support::common::f64_hex;
use horizon_core::{
geometry::{Aabb, Direction, Vec3},
position::BlockPos,
};
use horizon_world::{level::data::PushReaction, piston_push, piston_shape::MovingPistonState};
use serde_json::Value;
use std::{
fs,
io::{self, BufWriter, Write},
path::Path,
};
fn number(v: &Value) -> f64 {
f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap())
}
fn float(v: &Value) -> f32 {
f32::from_bits(u32::from_str_radix(v.as_str().unwrap(), 16).unwrap())
}
fn aabb(v: &Value) -> Aabb {
let a = v.as_array().unwrap();
Aabb::new(
Vec3::new(number(&a[0]), number(&a[1]), number(&a[2])),
Vec3::new(number(&a[3]), number(&a[4]), number(&a[5])),
)
}
fn direction(index: i64) -> Direction {
Direction::ALL[index.rem_euclid(6) as usize]
}
fn reaction(index: i64) -> PushReaction {
match index.rem_euclid(5) {
0 => PushReaction::PushPull,
1 => PushReaction::Push,
2 => PushReaction::Popped,
3 => PushReaction::Immoveable,
_ => PushReaction::IgnoreEntity,
}
}
fn line(out: &mut impl Write, name: &str, b: Aabb) -> io::Result<()> {
let (min, max) = (b.min_position(), b.max_position());
writeln!(
out,
"{name} {} {} {} {} {} {}",
f64_hex(min.x),
f64_hex(min.y),
f64_hex(min.z),
f64_hex(max.x),
f64_hex(max.y),
f64_hex(max.z)
)
}
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let arg = crate::args().nth(1).expect("private fixture directory");
let dir = Path::new(&arg);
let mut out = BufWriter::new(fs::File::create(dir.join("rust.txt"))?);
for text in fs::read_to_string(dir.join("inputs.txt"))?.lines() {
let f: Value = serde_json::from_str(text)?;
let p = &f["pos"];
let pos = BlockPos::new(
p[0].as_i64().unwrap() as i32,
p[1].as_i64().unwrap() as i32,
p[2].as_i64().unwrap() as i32,
);
let progress = float(&f["progress"]);
let piston = MovingPistonState {
moved_state: horizon_world::registry::BlockStateId(0),
direction: direction(f["direction"].as_i64().unwrap()),
extending: f["extending"].as_bool().unwrap(),
source: f["source"].as_bool().unwrap(),
progress,
previous_progress: progress,
};
let new_progress = float(&f["new_progress"]);
let delta_progress = f64::from(new_progress - progress);
let movement = piston.movement_direction();
let block_box = aabb(&f["block_box"]);
let entity = aabb(&f["entity"]);
let movement_box = aabb(&f["movement_box"]);
writeln!(
out,
"extended {:08x}",
piston_push::extended_progress(piston, progress).to_bits()
)?;
let origin = piston_push::move_by_position_and_progress(pos, block_box, piston);
line(&mut out, "origin", origin)?;
line(
&mut out,
"area",
piston_push::movement_area(block_box, movement, delta_progress),
)?;
for (name, face) in [
("east", Direction::East),
("west", Direction::West),
("up", Direction::Up),
("down", Direction::Down),
("south", Direction::South),
("north", Direction::North),
] {
writeln!(
out,
"movement {name} {}",
f64_hex(piston_push::get_movement(movement_box, face, entity))
)?;
}
// `moveCollidedEntities`' loop: the furthest box reaches into the
// entity, capped at the progress delta, plus the push offset.
let boxes: Vec<Aabb> = f["boxes"].as_array().unwrap().iter().map(aabb).collect();
let mut delta: f64 = 0.0;
let mut capped = false;
for block in &boxes {
let moving = piston_push::movement_area(
piston_push::move_by_position_and_progress(pos, *block, piston),
movement,
delta_progress,
);
if moving.intersects(entity) {
delta = delta.max(piston_push::get_movement(moving, movement, entity));
if delta >= delta_progress {
capped = true;
break;
}
}
}
let collided = if delta <= 0.0 {
0.0
} else {
delta.min(delta_progress) + piston_push::PUSH_OFFSET
};
writeln!(out, "collided {} {capped}", f64_hex(collided))?;
line(
&mut out,
"search",
piston_push::movement_search_area(origin, movement, delta_progress),
)?;
let sticky = piston_push::sticky_area(pos, piston, number(&f["sticky_top"]));
line(&mut out, "sticky", sticky)?;
writeln!(
out,
"matches {}",
piston_push::matches_sticky_criteria(
sticky,
reaction(f["reaction"].as_i64().unwrap()),
f["on_ground"].as_bool().unwrap(),
f["supported"].as_bool().unwrap(),
Vec3::new(number(&f["entity_x"]), 0.0, number(&f["entity_z"])),
)
)?;
}
out.flush()?;
Ok(())
}
+13 -5
View File
@@ -182,7 +182,9 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
self.neighbor_changed_at(pos, block);
}
}
/// `PistonMovingBlockEntity.tick` (entity pushing excluded).
/// `PistonMovingBlockEntity.tick`. A piston that is still moving reports
/// the push `moveCollidedEntities` would do at its new progress; the caller
/// moves the entities, because `Ticks` has none (`piston_pushes`).
fn moving_piston_tick(&mut self, pos: BlockPos) {
let Some(mut piston) = self.moving_piston(pos) else {
return;
@@ -227,14 +229,20 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
}
}
} else {
piston.progress = (piston.progress + 0.5).min(1.0);
if let Some(entry) = self
// moveCollidedEntities and moveStuckEntities run with the new
// progress but read the old one off the entity (their delta),
// so the push carries the piston as it was before the advance.
let old = piston;
let new_progress = (old.progress + 0.5).min(1.0);
let current = self
.ticks
.moving_pistons
.iter_mut()
.find(|(p, _)| *p == pos)
{
.find(|(p, _)| *p == pos);
if let Some(entry) = current {
piston.progress = new_progress;
entry.1 = piston;
self.ticks.piston_pushes.push((pos, old, new_progress));
}
}
}
+6
View File
@@ -29,6 +29,11 @@ pub struct Ticks {
/// Moving-piston block entities in ticker order (render and collide
/// moving blocks from these; see `piston_shape`).
pub moving_pistons: Vec<(BlockPos, super::pistons::MovingPiston)>,
/// The pushes this tick's moving-piston block entities asked for: each
/// piston with the progress it is about to reach, in the order they ticked
/// (`PistonMovingBlockEntity.tick`'s `moveCollidedEntities` call). The
/// caller moves entities by these; `Ticks` has no entities of its own.
pub piston_pushes: Vec<(BlockPos, super::pistons::MovingPiston, f32)>,
pub(crate) next_block_entity_id: u64,
/// `ServerLevel.blockEvents`, run by `Behaviours::run_block_events`.
pub block_events: Vec<super::pistons::BlockEventData>,
@@ -65,6 +70,7 @@ impl Ticks {
comparator_outputs: Default::default(),
torch_toggles: Vec::new(),
moving_pistons: Vec::new(),
piston_pushes: Vec::new(),
next_block_entity_id: 0,
block_events: Vec::new(),
handling_tick: false,
+1
View File
@@ -94,6 +94,7 @@ pub mod nbt;
pub mod object_open_map;
pub mod object_open_set;
pub mod piston_movement;
pub mod piston_push;
pub mod piston_shape;
pub mod player_food;
pub mod player_health;
+461
View File
@@ -0,0 +1,461 @@
//! `PistonMovingBlockEntity.moveCollidedEntities`, `moveStuckEntities` and
//! `fixEntityWithinPistonBase` over immutable geometry.
//!
//! Entity storage, the moving entities themselves and the blocks they collide
//! with belong to the caller: these functions decide *how far* an entity is
//! pushed, which is the part that must match vanilla to the bit. The movement
//! they describe is an `Entity.move(MoverType.PISTON, …)`, so the caller still
//! applies it through its own collision resolution and per-tick piston limit.
use crate::{level::data::PushReaction, piston_shape::MovingPistonState};
use horizon_core::{
geometry::{Aabb, Direction, Vec3},
position::BlockPos,
};
/// `PistonMovingBlockEntity.PUSH_OFFSET`.
pub const PUSH_OFFSET: f64 = 0.01;
/// `PistonMovingBlockEntity.getExtendedProgress`: where along its travel the
/// moved block is, in blocks (negative before it starts, 0 when it arrives).
pub fn extended_progress(piston: MovingPistonState, progress: f32) -> f32 {
if piston.extending {
progress - 1.0
} else {
1.0 - progress
}
}
/// `Direction.getAxisDirection().getStep()`.
fn step_along(direction: Direction) -> i32 {
match direction {
Direction::East | Direction::Up | Direction::South => 1,
Direction::West | Direction::Down | Direction::North => -1,
}
}
/// `Direction.getStepX/Y/Z` as a vector.
pub fn step_vector(direction: Direction) -> Vec3 {
let [x, y, z] = direction.normal();
Vec3::new(f64::from(x), f64::from(y), f64::from(z))
}
/// `PistonMath.getMovementArea`: the slab an entity's box must reach into to be
/// run over by `amount` of further travel along `direction`.
pub fn movement_area(aabb: Aabb, direction: Direction, amount: f64) -> Aabb {
let delta = amount * f64::from(step_along(direction));
let low = delta.min(0.0);
let high = delta.max(0.0);
let (min, max) = (aabb.min_position(), aabb.max_position());
match direction {
Direction::West => Aabb::new(
Vec3::new(min.x + low, min.y, min.z),
Vec3::new(min.x + high, max.y, max.z),
),
Direction::East => Aabb::new(
Vec3::new(max.x + low, min.y, min.z),
Vec3::new(max.x + high, max.y, max.z),
),
Direction::Down => Aabb::new(
Vec3::new(min.x, min.y + low, min.z),
Vec3::new(max.x, min.y + high, max.z),
),
Direction::Up => Aabb::new(
Vec3::new(min.x, max.y + low, min.z),
Vec3::new(max.x, max.y + high, max.z),
),
Direction::North => Aabb::new(
Vec3::new(min.x, min.y, min.z + low),
Vec3::new(max.x, max.y, min.z + high),
),
Direction::South => Aabb::new(
Vec3::new(min.x, min.y, max.z + low),
Vec3::new(max.x, max.y, max.z + high),
),
}
}
/// `PistonMovingBlockEntity.moveByPositionAndProgress`: a box moved to the
/// block position and on to where the moved block currently is.
pub fn move_by_position_and_progress(pos: BlockPos, aabb: Aabb, piston: MovingPistonState) -> Aabb {
let current = f64::from(extended_progress(piston, piston.progress));
let step = step_vector(piston.direction);
aabb.move_by(Vec3::new(
f64::from(pos.x) + current * step.x,
f64::from(pos.y) + current * step.y,
f64::from(pos.z) + current * step.z,
))
}
/// `PistonMovingBlockEntity.getMovement`: how far `entity` stands inside the
/// block it must be pushed out of, measured along `movement`'s axis.
pub fn get_movement(block: Aabb, movement: Direction, entity: Aabb) -> f64 {
let (bmin, bmax) = (block.min_position(), block.max_position());
let (emin, emax) = (entity.min_position(), entity.max_position());
match movement {
Direction::East => bmax.x - emin.x,
Direction::West => emax.x - bmin.x,
Direction::Up => bmax.y - emin.y,
Direction::Down => emax.y - bmin.y,
Direction::South => bmax.z - emin.z,
Direction::North => emax.z - bmin.z,
}
}
/// `moveCollidedEntities`' inner loop: the furthest any of the moved block's
/// boxes reaches into `entity`, capped at the progress delta, plus
/// [`PUSH_OFFSET`]. `0.0` when the block does not touch the entity.
pub fn collided_amount(
boxes: &[Aabb],
pos: BlockPos,
piston: MovingPistonState,
new_progress: f32,
movement: Direction,
entity: Aabb,
) -> f64 {
let delta_progress = f64::from(new_progress - piston.progress);
let mut delta: f64 = 0.0;
for block in boxes {
let moving = movement_area(
move_by_position_and_progress(pos, *block, piston),
movement,
delta_progress,
);
if moving.intersects(entity) {
delta = delta.max(get_movement(moving, movement, entity));
if delta >= delta_progress {
break;
}
}
}
if delta <= 0.0 {
return 0.0;
}
delta.min(delta_progress) + PUSH_OFFSET
}
/// The box `level.getEntities` searches for a push: the moved block's own box
/// widened by the slab it is about to sweep.
pub fn movement_search_area(block_box: Aabb, movement: Direction, delta_progress: f64) -> Aabb {
movement_area(block_box, movement, delta_progress).union_bounds(block_box)
}
/// `moveStuckEntities`: the box of standing entities a moving honey block drags
/// with it. `sticky_top` is the moved state's top collision height.
pub fn sticky_area(pos: BlockPos, piston: MovingPistonState, sticky_top: f64) -> Aabb {
let area = Aabb::new(
Vec3::new(0.0, sticky_top, 0.0),
Vec3::new(1.0, 1.500_001_000_000_000_1, 1.0),
);
move_by_position_and_progress(pos, area, piston)
}
/// `PistonMovingBlockEntity.matchesStickyCritera`.
pub fn matches_sticky_criteria(
area: Aabb,
reaction: PushReaction,
on_ground: bool,
supported_by_piston: bool,
entity_position: Vec3,
) -> bool {
if reaction != PushReaction::PushPull || !on_ground {
return false;
}
if supported_by_piston {
return true;
}
let (min, max) = (area.min_position(), area.max_position());
entity_position.x >= min.x
&& entity_position.x <= max.x
&& entity_position.z >= min.z
&& entity_position.z <= max.z
}
/// The level query intersects the sticky box before applying the predicate.
pub fn sticky_entity_selected(
area: Aabb,
bounds: Aabb,
reaction: PushReaction,
on_ground: bool,
supported_by_piston: bool,
position: Vec3,
) -> bool {
area.intersects(bounds)
&& matches_sticky_criteria(area, reaction, on_ground, supported_by_piston, position)
}
/// `fixEntityWithinPistonBase`: a retracting source piston drags an entity that
/// is still inside the piston's own block out along the opposite of `movement`.
/// The returned distance is measured along that opposite direction.
pub fn fix_within_piston_base(
pos: BlockPos,
entity: Aabb,
movement: Direction,
delta_progress: f64,
) -> Option<f64> {
let block = Aabb::unit_cube(Vec3::from_block(pos));
if !entity.intersects(block) {
return None;
}
let opposite = movement.opposite();
let delta = get_movement(block, opposite, entity) + PUSH_OFFSET;
let intersected = entity.intersect(block);
let delta_intersected = get_movement(block, opposite, intersected) + PUSH_OFFSET;
// As deep inside as the block allows: it is stuck in the base itself, so it
// leaves along with the retraction.
if (delta - delta_intersected).abs() < PUSH_OFFSET {
Some(delta.min(delta_progress) + PUSH_OFFSET)
} else {
None
}
}
/// `PistonMovingBlockEntity.getCollisionRelatedBlockState` uses the moved state
/// unless a source piston is retracting, where it shows a piston head instead.
/// The state itself comes from [`PistonBlockBindings::collision_related_state`].
///
/// [`PistonBlockBindings::collision_related_state`]:
/// crate::piston_shape::PistonBlockBindings::collision_related_state
pub fn uses_moved_state(piston: MovingPistonState) -> bool {
piston.extending || !piston.source
}
/// `moveCollidedEntities`' slime bounce: a slime block sets the velocity along
/// the movement axis to that direction's step, leaving the other axes.
pub fn bounce_velocity(movement: Direction, velocity: Vec3) -> Vec3 {
let mut velocity = velocity;
match movement {
Direction::East => velocity.x = 1.0,
Direction::West => velocity.x = -1.0,
Direction::Up => velocity.y = 1.0,
Direction::Down => velocity.y = -1.0,
Direction::South => velocity.z = 1.0,
Direction::North => velocity.z = -1.0,
}
velocity
}
/// `Entity.getPistonPushReaction() == PushReaction.IGNORE_ENTITY`.
///
/// `Marker`, `Interaction`, `Display`, `AreaEffectCloud` and
/// `OminousItemSpawner` return `IGNORE_ENTITY`, and an armor stand does when it
/// is a marker. Horizon gives those kinds no pushed body, so a caller that has
/// no box to push never reaches the maths above; this names the predicate for
/// the kinds that do have one.
pub fn ignores_push(marker: bool) -> bool {
marker
}
#[cfg(test)]
mod tests {
use super::*;
fn unit() -> Aabb {
Aabb::new(Vec3::new(0.0, 0.0, 0.0), Vec3::new(1.0, 1.0, 1.0))
}
fn moving(direction: Direction, extending: bool, progress: f32) -> MovingPistonState {
MovingPistonState {
moved_state: crate::registry::BlockStateId(0),
direction,
extending,
source: false,
progress,
previous_progress: progress,
}
}
/// `getMovementArea` puts the slab on the leading face and widens it by the
/// travel in that direction only.
#[test]
fn movement_area_sits_on_the_leading_face() {
let east = movement_area(unit(), Direction::East, 0.5);
assert_eq!(east.min_position(), Vec3::new(1.0, 0.0, 0.0));
assert_eq!(east.max_position(), Vec3::new(1.5, 1.0, 1.0));
let west = movement_area(unit(), Direction::West, 0.5);
assert_eq!(west.min_position(), Vec3::new(-0.5, 0.0, 0.0));
assert_eq!(west.max_position(), Vec3::new(0.0, 1.0, 1.0));
let up = movement_area(unit(), Direction::Up, 0.5);
assert_eq!(up.min_position(), Vec3::new(0.0, 1.0, 0.0));
assert_eq!(up.max_position(), Vec3::new(1.0, 1.5, 1.0));
let north = movement_area(unit(), Direction::North, 0.5);
assert_eq!(north.min_position(), Vec3::new(0.0, 0.0, -0.5));
assert_eq!(north.max_position(), Vec3::new(1.0, 1.0, 0.0));
}
/// A negative amount sweeps behind the leading face.
#[test]
fn movement_area_follows_a_negative_amount() {
let area = movement_area(unit(), Direction::East, -0.5);
assert_eq!(area.min_position(), Vec3::new(0.5, 0.0, 0.0));
assert_eq!(area.max_position(), Vec3::new(1.0, 1.0, 1.0));
}
/// The arm of a piston at the origin: the block entity sits in the block it
/// pushes into, and at progress 0 its shape is still a block back.
const ARM: BlockPos = BlockPos::new(1, 0, 0);
/// An entity in the way is pushed the progress delta, plus the push offset;
/// here it hangs 0.6 into the slab and the delta caps it at 0.5.
#[test]
fn a_collided_entity_moves_the_progress_delta() {
let piston = moving(Direction::East, true, 0.0);
let entity = Aabb::new(Vec3::new(0.9, 0.0, 0.2), Vec3::new(1.5, 1.8, 0.8));
let amount = collided_amount(&[unit()], ARM, piston, 0.5, Direction::East, entity);
assert!((amount - (0.5 + PUSH_OFFSET)).abs() < 1.0e-12, "{amount}");
}
/// The delta is not a floor: an entity barely nicking the slab moves by only
/// what it is owed, plus the offset.
#[test]
fn a_grazed_entity_moves_only_what_it_overlaps() {
let piston = moving(Direction::East, true, 0.0);
let entity = Aabb::new(Vec3::new(1.4, 0.0, 0.2), Vec3::new(2.0, 1.8, 0.8));
let amount = collided_amount(&[unit()], ARM, piston, 0.5, Direction::East, entity);
assert!((amount - (0.1 + PUSH_OFFSET)).abs() < 1.0e-12, "{amount}");
}
/// Clear of the block, nothing moves.
#[test]
fn an_entity_out_of_the_way_is_left_alone() {
let piston = moving(Direction::East, true, 0.0);
let entity = Aabb::new(Vec3::new(3.0, 0.0, 0.2), Vec3::new(3.6, 1.8, 0.8));
let amount = collided_amount(&[unit()], ARM, piston, 0.5, Direction::East, entity);
assert_eq!(amount, 0.0);
}
/// A retracting base drags an entity stuck inside its own block, limited by
/// the progress delta.
#[test]
fn a_retracting_base_drags_a_stuck_entity() {
let entity = Aabb::new(Vec3::new(0.75, 0.0, 0.2), Vec3::new(1.35, 1.8, 0.8));
let amount = fix_within_piston_base(BlockPos::new(0, 0, 0), entity, Direction::West, 0.5)
.expect("stuck in the base");
assert!(amount > 0.0 && amount <= 0.5 + PUSH_OFFSET, "{amount}");
}
/// An entity outside the base's block is not dragged by it.
#[test]
fn a_free_entity_is_not_dragged_by_the_base() {
let entity = Aabb::new(Vec3::new(2.0, 0.0, 0.2), Vec3::new(2.6, 1.8, 0.8));
assert_eq!(
fix_within_piston_base(BlockPos::new(0, 0, 0), entity, Direction::West, 0.5),
None
);
}
/// Honey drags only entities that stand on it and that blocks may push.
#[test]
fn honey_drags_standing_entities() {
let area = Aabb::new(Vec3::new(0.0, 0.0, 0.0), Vec3::new(1.0, 1.5, 1.0));
let on_top = Vec3::new(0.5, 0.0, 0.5);
assert!(matches_sticky_criteria(
area,
PushReaction::PushPull,
true,
false,
on_top
));
assert!(!matches_sticky_criteria(
area,
PushReaction::PushPull,
false,
false,
on_top
));
assert!(!matches_sticky_criteria(
area,
PushReaction::Immoveable,
true,
false,
on_top
));
// Beside it: only standing on the piston's own block holds it.
let beside = Vec3::new(2.0, 0.0, 0.5);
assert!(!matches_sticky_criteria(
area,
PushReaction::PushPull,
true,
false,
beside
));
assert!(matches_sticky_criteria(
area,
PushReaction::PushPull,
true,
true,
beside
));
}
#[test]
fn honey_selection_uses_entity_position_and_intersection() {
let area = Aabb::new(Vec3::new(0.0, 1.0, 0.0), Vec3::new(1.0, 1.5, 1.0));
let position = Vec3::new(0.1, 1.0, 0.5);
let bounds = Aabb::new(Vec3::new(-0.2, 1.0, 0.2), Vec3::new(0.4, 2.8, 0.8));
assert!(sticky_entity_selected(
area,
bounds,
PushReaction::PushPull,
true,
false,
position
));
let above = bounds.move_by(Vec3::new(0.0, 3.0, 0.0));
assert!(!sticky_entity_selected(
area,
above,
PushReaction::PushPull,
true,
true,
position
));
}
/// The slime bounce rewrites only the movement axis.
#[test]
fn a_slime_block_bounces_along_its_axis() {
let v = Vec3::new(0.2, -0.5, 0.3);
assert_eq!(
bounce_velocity(Direction::East, v),
Vec3::new(1.0, -0.5, 0.3)
);
assert_eq!(bounce_velocity(Direction::Up, v), Vec3::new(0.2, 1.0, 0.3));
assert_eq!(
bounce_velocity(Direction::North, v),
Vec3::new(0.2, -0.5, -1.0)
);
}
/// The moved block sits along the piston's own direction, half a block short
/// at mid-travel and in place once landed.
#[test]
fn the_moved_block_sits_along_the_piston_direction() {
let half = moving(Direction::East, true, 0.5);
let moved = move_by_position_and_progress(BlockPos::new(2, 3, 4), unit(), half);
assert_eq!(moved.min_position(), Vec3::new(1.5, 3.0, 4.0));
let landed = moving(Direction::East, true, 1.0);
assert_eq!(
move_by_position_and_progress(BlockPos::new(2, 3, 4), unit(), landed).min_position(),
Vec3::new(2.0, 3.0, 4.0)
);
// Retracting runs the other way: two thirds out at progress 1/3.
let pulling = moving(Direction::East, false, 0.5);
assert_eq!(
move_by_position_and_progress(BlockPos::new(2, 3, 4), unit(), pulling).min_position(),
Vec3::new(2.5, 3.0, 4.0)
);
}
/// The search area covers the block and the slab in front of it.
#[test]
fn the_search_area_covers_block_and_sweep() {
let area = movement_search_area(unit(), Direction::East, 0.5);
assert_eq!(area.min_position(), Vec3::new(0.0, 0.0, 0.0));
assert_eq!(area.max_position(), Vec3::new(1.5, 1.0, 1.0));
}
}
@@ -0,0 +1,78 @@
package dev.horizonlab.parity;
import com.google.gson.*;
import java.io.*;
import java.lang.reflect.*;
import java.nio.file.*;
import java.util.*;
import net.minecraft.core.*;
import net.minecraft.world.entity.*;
import net.minecraft.world.entity.monster.zombie.Zombie;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.piston.*;
import net.minecraft.world.phys.*;
import net.minecraft.world.phys.shapes.*;
/**
* The real moving-piston push maths: PistonMath.getMovementArea,
* moveByPositionAndProgress, getMovement, the moveCollidedEntities loop over
* the block's own boxes, and moveStuckEntities' box and matchesStickyCritera.
* fixEntityWithinPistonBase is left out: it moves the entity
* (`Entity.move(MoverType.PISTON, ...)`), which needs a live level, so the
* Rust side is covered by its own unit tests.
*/
public final class VanillaPistonPushOracle {
private static float number(JsonElement v){return Float.intBitsToFloat(Integer.parseUnsignedInt(v.getAsString(),16));}
private static double dbl(JsonElement v){return Double.longBitsToDouble(Long.parseUnsignedLong(v.getAsString(),16));}
private static AABB box(JsonElement v){var a=v.getAsJsonArray();return new AABB(dbl(a.get(0)),dbl(a.get(1)),dbl(a.get(2)),dbl(a.get(3)),dbl(a.get(4)),dbl(a.get(5)));}
private static void line(PrintWriter out,String name,AABB b){out.printf(name+" %016x %016x %016x %016x %016x %016x%n",Double.doubleToLongBits(b.minX),Double.doubleToLongBits(b.minY),Double.doubleToLongBits(b.minZ),Double.doubleToLongBits(b.maxX),Double.doubleToLongBits(b.maxY),Double.doubleToLongBits(b.maxZ));}
private static Field field(Class<?> c,String name)throws Exception{var f=c.getDeclaredField(name);f.setAccessible(true);return f;}
private static Method method(Class<?> c,String name,Class<?>...types)throws Exception{var m=c.getDeclaredMethod(name,types);m.setAccessible(true);return m;}
@SuppressWarnings("unchecked") public static void main(String[] args)throws Exception {
net.minecraft.SharedConstants.tryDetectVersion();net.minecraft.server.Bootstrap.bootStrap();Path dir=Path.of(args[1]);
var allocator=Class.forName("sun.misc.Unsafe");var uf=allocator.getDeclaredField("theUnsafe");uf.setAccessible(true);var unsafe=uf.get(null);var allocate=allocator.getMethod("allocateInstance",Class.class);
var progress=field(PistonMovingBlockEntity.class,"progress");
var movedBy=method(PistonMovingBlockEntity.class,"moveByPositionAndProgress",BlockPos.class,AABB.class,PistonMovingBlockEntity.class);
var extended=method(PistonMovingBlockEntity.class,"getExtendedProgress",float.class);
var getMovement=method(PistonMovingBlockEntity.class,"getMovement",AABB.class,Direction.class,AABB.class);
var matches=method(PistonMovingBlockEntity.class,"matchesStickyCritera",AABB.class,Entity.class,BlockPos.class);
var movementDir=method(PistonMovingBlockEntity.class,"getMovementDirection");
var position=field(Entity.class,"position");var onGroundField=field(Entity.class,"onGround");var supporting=field(Entity.class,"mainSupportingBlockPos");var bbField=field(Entity.class,"bb");
var faces=new Direction[]{Direction.EAST,Direction.WEST,Direction.UP,Direction.DOWN,Direction.SOUTH,Direction.NORTH};
try(var out=new PrintWriter(Files.newBufferedWriter(dir.resolve(args[0]+".txt")));var inputs=Files.lines(dir.resolve("inputs.txt"))) {
for(var line:(Iterable<String>)inputs::iterator) {
var f=JsonParser.parseString(line).getAsJsonObject();var v=f.getAsJsonArray("pos");var p=new BlockPos(v.get(0).getAsInt(),v.get(1).getAsInt(),v.get(2).getAsInt());
var direction=Direction.values()[f.get("direction").getAsInt()];
var piston=new PistonMovingBlockEntity(p,Blocks.MOVING_PISTON.defaultBlockState(),Blocks.STONE.defaultBlockState(),direction,f.get("extending").getAsBoolean(),f.get("source").getAsBoolean());
float current=number(f.get("progress"));
progress.setFloat(piston,current);
float newProgress=number(f.get("new_progress"));
double deltaProgress=newProgress-current;
Direction movement=(Direction)movementDir.invoke(piston);
AABB blockBox=box(f.get("block_box"));
AABB entityBox=box(f.get("entity"));
AABB movementBox=box(f.get("movement_box"));
out.printf("extended %08x%n",Float.floatToIntBits((float)extended.invoke(piston,current)));
AABB origin=(AABB)movedBy.invoke(null,p,blockBox,piston);
line(out,"origin",origin);
line(out,"area",PistonMath.getMovementArea(blockBox,movement,deltaProgress));
for(var face:faces)out.printf("movement %s %016x%n",face.getSerializedName(),Double.doubleToLongBits((double)getMovement.invoke(null,movementBox,face,entityBox)));
double delta=0.0;boolean capped=false;
for(var value:f.getAsJsonArray("boxes")){
AABB movingAABB=PistonMath.getMovementArea((AABB)movedBy.invoke(null,p,box(value),piston),movement,deltaProgress);
if(movingAABB.intersects(entityBox)){delta=Math.max(delta,(double)getMovement.invoke(null,movingAABB,movement,entityBox));if(delta>=deltaProgress){capped=true;break;}}
}
double collided=delta<=0.0?0.0:Math.min(delta,deltaProgress)+0.01;
out.printf("collided %016x %b%n",Double.doubleToLongBits(collided),capped);
line(out,"search",PistonMath.getMovementArea(origin,movement,deltaProgress).minmax(origin));
AABB sticky=(AABB)movedBy.invoke(null,p,new AABB(0.0,dbl(f.get("sticky_top")),0.0,1.0,1.5000010000000001,1.0),piston);
line(out,"sticky",sticky);
boolean pushable=f.get("reaction").getAsInt()==0;
var entity=(Entity)allocate.invoke(unsafe,pushable?Zombie.class:Marker.class);
position.set(entity,new Vec3(dbl(f.get("entity_x")),0.0,dbl(f.get("entity_z"))));
bbField.set(entity,entityBox);
onGroundField.setBoolean(entity,f.get("on_ground").getAsBoolean());
supporting.set(entity,f.get("supported").getAsBoolean()?Optional.of(p):Optional.empty());
out.printf("matches %b%n",(boolean)matches.invoke(null,sticky,entity,p));
}
}
}
}
+165 -2
View File
@@ -304,7 +304,7 @@ CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sp
'arrows', 'kills', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'styles',
'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing',
'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push',
'eat', 'eating', 'xp', 'objects', 'projectiles', 'thrown', 'sparks', 'hud', 'sidebar', 'border', 'particles',
'piston-push', 'piston-slime', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'thrown', 'sparks', 'hud', 'sidebar', 'border', 'particles',
'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects',
'portals', 'distance', 'growth', 'doors', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'hands',
'lightning', 'riding', 'occupied_horse', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption',
@@ -2410,6 +2410,168 @@ def wait_state(game, pos, key, want, ticks):
return state_of(game, pos, key)
def check_piston_push(host, guest, out):
"""A moving piston pushes both players and a stationary mob on each host path."""
hx, _, hz = pos_of(host)
x, y, z = (int(hx // 16) * 16 + 8, 100, int(hz // 16) * 16 + 8)
host('command', command=f'fill {x-4} {y-1} {z-4} {x+7} {y-1} {z+4} minecraft:stone')
host('command', command=f'fill {x-4} {y} {z-4} {x+7} {y+5} {z+4} minecraft:air')
host('command', command=f'setblock {x} {y} {z} minecraft:piston[facing=east]')
results = {}
for player, name in ((host, host.player), (guest, GUEST_NAME)):
host('command', command=f'setblock {x-1} {y} {z} minecraft:air')
host.wait(10)
host('command', command=f'tp @s {x+5.5} {y} {z+2.5}')
host('command', command=f'tp {GUEST_NAME} {x+5.5} {y} {z-1.5}')
host('command', command=f'tp {"@s" if player is host else GUEST_NAME} {x+1.65} {y} {z+0.5}')
player.wait(15)
before = pos_of(player)
assert abs(before[0] - (x + 1.65)) < 0.1, f'{name} did not reach piston: {before}'
host('command', command=f'setblock {x-1} {y} {z} minecraft:redstone_block')
player.wait(30)
after = pos_of(player)
results[name] = after[0] - before[0]
assert results[name] > 0.3, f'piston did not push {name}: {before} -> {after}'
host('command', command=f'setblock {x-1} {y} {z} minecraft:air')
host.wait(10)
host('command', command=f'tp {GUEST_NAME} {x+5.5} {y} {z-1.5}')
host('command', command=f'tp @s {x+1.65} {y} {z+0.5}')
host.wait(10)
host.raw('key', 'e', 'tap')
host.raw('wait-for', 'screen', 'inventory')
before = pos_of(host)
guest('command', command=f'setblock {x-1} {y} {z} minecraft:redstone_block')
host.wait(30)
after = pos_of(host)
results['inventory'] = after[0] - before[0]
assert results['inventory'] > 0.3, f'piston did not push player with inventory open: {before} -> {after}'
host.raw('key', 'esc', 'tap')
host('command', command=f'setblock {x-1} {y} {z} minecraft:air')
host.wait(10)
host('command', command=f'tp @s {x+5.5} {y} {z+2.5}')
host('command', command=f'summon minecraft:pig {x+1.65} {y} {z+0.5} {{NoAI:1b}}')
host.wait(10)
pigs = host('entities', radius=16, type='pig')['entities']
pig = min(pigs, key=lambda p: abs(p['pos'][0] - (x + 1.65)) + abs(p['pos'][2] - (z + 0.5)))
before = pig['pos'][0]
host('command', command=f'setblock {x-1} {y} {z} minecraft:redstone_block')
host.wait(30)
pigs = host('entities', radius=16, type='pig')['entities']
after = next(p['pos'][0] for p in pigs if p['id'] == pig['id'])
results['pig'] = after - before
assert results['pig'] > 0.3, f'piston did not push pig: {before} -> {after}'
results.update(piston_push_at_border(host, guest))
return results
def piston_push_at_border(host, guest):
"""A piston next to the world border: the border's wall stops the pushed
body, as it stops ordinary movement (`Entity.collectCollidersIgnoringWorldBorder`)."""
hx, _, hz = pos_of(host)
x, y, z = (int(hx // 16) * 16 + 8, 100, int(hz // 16) * 16 + 8)
host('command', command=f'fill {x-4} {y-1} {z-4} {x+7} {y-1} {z+4} minecraft:stone')
host('command', command=f'fill {x-4} {y} {z-4} {x+7} {y+5} {z+4} minecraft:air')
host('command', command=f'setblock {x} {y} {z} minecraft:piston[facing=east]')
# The border's east wall stands at `x + 2`, the east face of the block the
# head extends into: a body pushed east stops there instead of being
# pushed out through the border. `worldborder center` centres integers on
# the block, so the centre is `x - 8.5` and the wall lands on `x + 2`.
wall = x + 2
center = [x - 8.5, z + 0.5]
results = {}
try:
host('command', command=f'worldborder center {x - 9} {z}')
host('command', command='worldborder set 20.5')
seen = None
for _ in range(10):
host.wait(2)
seen = [g('status').get('border') for g in (host, guest)]
if all(b and b['size'] == 20.5 and b['center'] == center for b in seen):
break
results['border'] = seen
for player, name in ((host, host.player), (guest, GUEST_NAME)):
host('command', command=f'setblock {x-1} {y} {z} minecraft:air')
host.wait(10)
host('command', command=f'tp @s {x+5.5} {y} {z+2.5}')
host('command', command=f'tp {GUEST_NAME} {x+5.5} {y} {z-1.5}')
host('command', command=f'tp {"@s" if player is host else GUEST_NAME} {x+1.65} {y} {z+0.5}')
player.wait(15)
before = pos_of(player)
assert abs(before[0] - (x + 1.65)) < 0.1, f'{name} did not reach the piston: {before}'
host('command', command=f'setblock {x-1} {y} {z} minecraft:redstone_block')
player.wait(30)
after = pos_of(player)
# The wall's inner face is at `wall`: the body's box stops there.
results[name] = round(after[0] - before[0], 3)
assert after[0] > before[0], f'the piston did not push {name}: {before} -> {after}'
assert after[0] <= wall - 0.3 + 0.01, f'the border did not stop {name}: {before} -> {after}'
host('command', command=f'setblock {x-1} {y} {z} minecraft:air')
host.wait(10)
finally:
host('command', command='worldborder center 0 0')
host('command', command='worldborder set 59999968')
return results
def check_piston_slime(host, guest, out):
"""A piston pushing a slime block: the block pushes the body in front of
it and bounces it (`PistonMovingBlockEntity.moveCollidedEntities`)."""
hx, _, hz = pos_of(host)
x, y, z = (int(hx // 16) * 16 + 8, 100, int(hz // 16) * 16 + 8)
results = {}
def setup():
# The previous case may have left a mob where the body stands.
host('command', command='kill @e[type=minecraft:pig]')
host('command', command=f'setblock {x} {y} {z} minecraft:air')
host.wait(10)
host('command', command=f'fill {x-4} {y-1} {z-4} {x+7} {y-1} {z+4} minecraft:stone')
host('command', command=f'fill {x-4} {y} {z-4} {x+7} {y+5} {z+4} minecraft:air')
host('command', command=f'setblock {x} {y} {z} minecraft:piston[facing=east]')
host.wait(5)
for player, name in ((host, host.player), (guest, GUEST_NAME)):
setup()
host('command', command=f'setblock {x+1} {y} {z} minecraft:slime_block')
host('command', command=f'tp @s {x+5.5} {y} {z+2.5}')
host('command', command=f'tp {GUEST_NAME} {x+5.5} {y} {z-1.5}')
host('command', command=f'tp {"@s" if player is host else GUEST_NAME} {x+2.5} {y} {z+0.5}')
player.wait(15)
before = pos_of(player)
assert abs(before[0] - (x + 2.5)) < 0.1, f'{name} did not reach the slime: {before}'
host('command', command=f'setblock {x-1} {y} {z} minecraft:redstone_block')
# The bounce sets the velocity along the push axis; it decays fast, so
# sample it while the block is still moving.
speed = 0.0
for _ in range(6):
player.wait(2)
speed = max(speed, player('state')['player']['velocity'][0])
after = pos_of(player)
results[name] = round(after[0] - before[0], 3)
# The block pushes the body out of its way, and the bounce launches it.
assert results[name] > 1.5, f'the slime block did not push and bounce {name}: {before} -> {after}'
assert speed > 0.05, f'the slime block did not bounce {name}: {speed}'
host('command', command=f'setblock {x-1} {y} {z} minecraft:air')
player.wait(10)
# A plain block only pushes: the body is not launched.
setup()
host('command', command=f'setblock {x+1} {y} {z} minecraft:stone')
host('command', command=f'tp @s {x+2.5} {y} {z+0.5}')
host.wait(15)
before = pos_of(host)
host('command', command=f'setblock {x-1} {y} {z} minecraft:redstone_block')
speed = 0.0
for _ in range(6):
host.wait(2)
speed = max(speed, host('state')['player']['velocity'][0])
after = pos_of(host)
results['stone'] = round(after[0] - before[0], 3)
assert results['stone'] < 1.5, f'a plain block bounced the player: {before} -> {after}'
host('command', command=f'setblock {x-1} {y} {z} minecraft:air')
host.wait(10)
return results
def check_plates(host, guest, out):
guest('command', command='gamemode survival')
x, y, z = (int(v // 1) for v in pos_of(guest))
@@ -4769,7 +4931,8 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour,
'cartography': check_cartography, 'hurt': check_hurt, 'commands': check_commands,
'recipes': check_recipes, 'trials': check_trials, 'walls': check_walls,
'effects': check_effects, 'portals': check_portals, 'distance': check_distance,
'clicks': check_clicks, 'sleep': check_sleep, 'push': check_push,
'clicks': check_clicks, 'sleep': check_sleep, 'push': check_push, 'piston-push': check_piston_push,
'piston-slime': check_piston_slime,
'eat': check_eat, 'eating': check_eating, 'xp': check_xp,
'objects': check_objects, 'projectiles': check_projectiles, 'blast': check_blast,
'light': check_light, 'plates': check_plates, 'swings': check_swings,
+193
View File
@@ -0,0 +1,193 @@
#!/usr/bin/env python3
"""Compare moving-piston push geometry and predicates with pinned vanilla.
`PistonMovingBlockEntity.moveCollidedEntities` decides how far an entity is
pushed; the movement it describes is `Entity.move(MoverType.PISTON, ...)`, whose
collision and per-tick limit the game owns. This harness covers the deciding
half: the movement area, the offset block box, `getMovement` on every face, the
per-box loop, `moveStuckEntities`' box and `matchesStickyCritera`, and
`fixEntityWithinPistonBase`.
"""
import argparse
import json
import os
from pathlib import Path
import platform
import random
import struct
import subprocess
try:
from . import lab, test_rust_parity as base
except ImportError:
import lab
import test_rust_parity as base
def corpus():
def f32(v): return struct.pack('>f', float(v)).hex()
def f64(v): return struct.pack('>d', float(v)).hex()
def box(values): return [f64(v) for v in values]
step = [1.0 / 3.0, 0.25, 0.5, 0.75, 1.0]
unit = [0.0, 0.0, 0.0, 1.0, 1.0, 1.0]
offsets = [[0, 0, 0], [-17, -64, 30000000], [2147483647, -2147483648, -2147483648]]
faces = [0, 1, 2, 3, 4, 5]
# Progress pairs around the 0.25/0.5/1.0 thresholds the head and the
# extended-progress offset change at.
progress = [0.0, 0.25, 0.49999997, 0.5, 0.75, 0.99999994, 1.0]
entities = [
[0.9, 0.0, 0.2, 1.5, 1.8, 0.8],
[1.4, 0.0, 0.2, 2.0, 1.8, 0.8],
[0.75, 0.0, 0.2, 1.35, 1.8, 0.8],
[3.0, 0.0, 0.2, 3.6, 1.8, 0.8],
[0.49, 0.0, 0.49, 0.51, 1.8, 0.51],
[0.0, 1.0, 0.0, 1.0, 2.8, 1.0],
]
for index, position in enumerate(offsets):
for direction in faces:
for extending in (False, True):
for source in (False, True):
for current in progress:
advanced = min(current + 0.5, 1.0)
for entity in entities:
yield {
'pos': position,
'direction': direction,
'extending': extending,
'source': source,
'progress': f32(current),
'new_progress': f32(advanced),
'block_box': box(unit),
'movement_box': box(unit),
'entity': box(entity),
'boxes': [box(unit)],
'sticky_top': f64(0.0),
'reaction': 0,
'on_ground': True,
'supported': False,
'entity_x': f64(entity[0]),
'entity_z': f64(entity[2]),
}
# Split boxes, a retracting honey drag with every push reaction, and the
# predicate's supported/on-ground corners.
for index in range(4000):
rng = random.Random(0x50555348 + index)
current = rng.choice(progress)
advanced = min(current + 0.5, 1.0)
count = rng.randint(1, 4)
boxes = []
for _ in range(count):
low = [rng.choice([0.0, 0.25, 0.5]), rng.choice([0.0, 0.5]), rng.choice([0.0, 0.5])]
high = [min(v + rng.choice([0.25, 0.5, 1.0]), 1.0) for v in low]
boxes.append(box(low + high))
yield {
'pos': rng.choice(offsets),
'direction': rng.randrange(6),
'extending': rng.random() < 0.5,
'source': rng.random() < 0.5,
'progress': f32(current),
'new_progress': f32(advanced),
'block_box': boxes[0],
'movement_box': boxes[rng.randrange(len(boxes))],
'entity': box([
rng.choice([0.0, 0.49, 0.75, 0.9, 1.4]),
rng.choice([0.0, 0.5]),
rng.choice([0.0, 0.2, 0.49]),
rng.choice([0.51, 1.35, 1.5, 2.0]),
rng.choice([1.8, 2.8]),
rng.choice([0.51, 0.8, 1.0]),
]),
'boxes': boxes,
'sticky_top': f64(rng.choice([0.0, 0.5, 0.9375])),
'reaction': rng.randrange(5),
'on_ground': rng.random() < 0.5,
'supported': rng.random() < 0.5,
'entity_x': f64(rng.choice([0.0, 0.5, 1.0, 2.0])),
'entity_z': f64(rng.choice([0.0, 0.5, 1.0, 2.0])),
}
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--prepare', action='store_true')
args = parser.parse_args()
build = lab.LOCAL / 'rust-piston-push-parity'
build.mkdir(parents=True, exist_ok=True)
report_path = build / 'report.json'
report_path.unlink(missing_ok=True)
if args.prepare:
lab.prepare()
lock, jars, libraries = base.verify_artifacts()
oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaPistonPushOracle.java'
helpers = [oracle.with_name(name + '.java')
for name in ['VanillaCollisionOracle', 'VanillaBlockStateCodecOracle', 'VanillaFaceSupportOracle']]
sources = [oracle, *helpers, Path(__file__), lab.ROOT / 'tools/test_rust_parity.py', lab.ROOT / 'tools/lab.py',
lab.ROOT / 'Cargo.toml', lab.ROOT / 'Cargo.lock',
lab.ROOT / 'crates/horizon-core/Cargo.toml', lab.ROOT / 'crates/horizon-world/Cargo.toml',
*sorted((lab.ROOT / 'crates/horizon-core/src').rglob('*.rs')),
*sorted((lab.ROOT / 'crates/horizon-world/src').rglob('*.rs')),
lab.ROOT / 'crates/horizon-world/examples/probe/piston_push_fixture.rs']
tested_sources = base.source_hashes(sources)
classes = build / 'classes'
classes.mkdir(exist_ok=True)
def run(command, name):
with (build / (name + '.log')).open('w', encoding='utf-8') as log:
base.run(list(map(str, command)), stdout=log, stderr=subprocess.STDOUT)
dependency_path = os.pathsep.join(map(str, libraries))
run(['javac', '--release', lock['minecraft']['java_major'], '-proc:none',
'-sourcepath', str(lab.ROOT / 'mods/horizon/src/test/java'),
'-cp', str(jars['server']) + os.pathsep + dependency_path,
'-d', classes, oracle, *helpers], 'compile-java')
def java(side, mode, path):
run(['java', '-Xmx512M', '-cp', os.pathsep.join([str(classes), str(jars[side]), dependency_path]),
'dev.horizonlab.parity.VanillaPistonPushOracle', mode, path], side + '-' + mode)
fixtures = list(corpus())
inputs = build / 'inputs.txt'
inputs.write_text(''.join(json.dumps(value, separators=(',', ':')) + '\n' for value in fixtures), encoding='utf-8')
fixture_paths = [inputs]
tested_fixtures = base.source_hashes(fixture_paths)
run(['cargo', 'build', '--release', '-p', 'horizon-world', '--example', 'world_probe'], 'compile-rust')
metadata = json.loads(subprocess.check_output(
['cargo', 'metadata', '--no-deps', '--format-version', '1'], cwd=lab.ROOT, text=True))
binary = Path(metadata['target_directory']) / 'release/examples' / ('world_probe.exe' if os.name == 'nt' else 'world_probe')
run([binary, 'piston_push_fixture', build], 'rust')
references = []
for side, jar in jars.items():
java(side, side, build)
reference = build / f'{side}.txt'
matched = base.compare(reference, build / 'rust.txt')
references.append({'side': side, 'jar_sha256': lab.digest(jar, 'sha256'),
'output_sha256': lab.digest(reference, 'sha256'), 'matched_output_lines': matched})
print(f'{side}: {matched:,} piston-push output lines match Rust', flush=True)
base.ensure_sources_unchanged(sources, tested_sources)
base.ensure_sources_unchanged(fixture_paths, tested_fixtures)
report = {
'schema': 1,
'minecraft': lock['minecraft']['version'],
'manifest_sha1': lock['minecraft']['manifest_sha1'],
'platform': platform.platform(),
'java': subprocess.check_output(['java', '--version'], text=True).splitlines()[0],
'rustc': subprocess.check_output(['rustc', '--version'], text=True).strip(),
'fixtures': len(fixtures),
'sources_sha256': tested_sources,
'fixtures_sha256': tested_fixtures,
'rust_output_sha256': lab.digest(build / 'rust.txt', 'sha256'),
'references': references,
'scope': 'Actual PistonMovingBlockEntity push geometry and predicates: getExtendedProgress, '
'moveByPositionAndProgress, PistonMath.getMovementArea, getMovement on every face, the '
'moveCollidedEntities loop over split shapes with its delta cap and PUSH_OFFSET, the '
'getEntities search box, and moveStuckEntities\' box with matchesStickyCritera for every push '
'reaction. fixEntityWithinPistonBase is covered by unit tests only: it calls '
'Entity.move(MoverType.PISTON, ...) and so needs a live level. Entity selection order, that '
'collision pass, the per-tick 0.51 limit, block behaviour of the moved state and full gameplay '
'remain elsewhere.',
}
lab.write_json(report_path, report)
print(f'Scoped piston-push parity passed: {report_path}')
if __name__ == '__main__':
main()