From 9266e0c7c9b3e633971555657aafe033b9ba3c1f Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Mon, 26 Oct 2026 23:34:03 +0100 Subject: [PATCH 1/2] Mobs are pushed by flowing water and lava --- .../examples/probe/mob_ai_probe.rs | 1 + crates/horizon-client/src/game/creatures.rs | 1 + crates/horizon-client/src/mobs.rs | 2 + crates/horizon-client/src/mobs/living.rs | 122 ++++++++++++++++++ 4 files changed, 126 insertions(+) diff --git a/crates/horizon-client/examples/probe/mob_ai_probe.rs b/crates/horizon-client/examples/probe/mob_ai_probe.rs index 5b6c26d2f..9c7eed643 100644 --- a/crates/horizon-client/examples/probe/mob_ai_probe.rs +++ b/crates/horizon-client/examples/probe/mob_ai_probe.rs @@ -1081,6 +1081,7 @@ pub fn main() -> horizon_client::Result<()> { world: &world, registry, facts: &facts, + fluid_tables: &motion.fluids, movement: &motion.blocks, collision: &collision, data: &data, diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index f6961ab13..951db09ac 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -401,6 +401,7 @@ impl Game<'_> { world: &self.world, registry: self.pack.assets.registry, facts: self.res.facts(), + fluid_tables: &self.motion.fluids, movement: &self.motion.blocks, collision: self.collision, data: self.res.mob_data(), diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index f4086216d..81f81f330 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -1560,6 +1560,8 @@ pub struct MobWorld<'a> { pub world: &'a World, pub registry: &'a horizon_world::registry::BlockRegistry, pub facts: &'a BlockFacts, + /// Fluid states and flow facts, for the currents that push mobs. + pub fluid_tables: &'a crate::motion::FluidResources, pub movement: &'a horizon_world::movement_properties::BoundMovementProperties, pub collision: &'a BlockCollisionRegistry<'a>, pub data: &'a MobData, diff --git a/crates/horizon-client/src/mobs/living.rs b/crates/horizon-client/src/mobs/living.rs index 394821ace..ce3183721 100644 --- a/crates/horizon-client/src/mobs/living.rs +++ b/crates/horizon-client/src/mobs/living.rs @@ -9,8 +9,15 @@ use crate::{ }; use horizon_core::{ geometry::{Aabb, Vec3, trig}, + position::BlockPos, random::RandomSource, }; +use horizon_world::{ + entity_fluid::{CurrentAccumulator, CurrentTarget}, + fluid::{FluidKind, FluidState}, + fluid_flow::{FluidFlowWorld, flow_at}, + registry::BlockStateId, +}; /// `Entity.moveDist`, `flyDist` and `nextStep`. #[derive(Clone, Copy, Debug)] @@ -36,6 +43,50 @@ pub struct Fluids { pub in_lava: bool, pub eye_in_water: bool, } +/// `FluidFlowWorld` over the mob's scene: the neighbours a flow vector reads. +struct FlowScene<'a, 'w>(&'a MobWorld<'w>); +impl FluidFlowWorld for FlowScene<'_, '_> { + type Error = std::convert::Infallible; + type Block = BlockStateId; + fn fluid(&mut self, p: BlockPos) -> Result { + Ok(self.0.fluid_tables.fluid(self.0.world.block(p.x, p.y, p.z))) + } + fn block(&mut self, p: BlockPos) -> Result { + Ok(self.0.world.block(p.x, p.y, p.z)) + } + fn blocks_fluid_flow(&self, s: BlockStateId) -> bool { + self.0.fluid_tables.blocks_flow(s) + } + fn is_ice_class(&self, s: BlockStateId) -> bool { + self.0.fluid_tables.ice(s) + } + fn is_face_sturdy( + &mut self, + s: BlockStateId, + _: BlockPos, + d: horizon_core::geometry::Direction, + ) -> Result { + Ok(self.0.fluid_tables.sturdy(s, d)) + } +} +/// The mob's velocity, for `CurrentAccumulator::apply_to`. +struct Pushed<'a>(&'a mut Vec3); +impl CurrentTarget for Pushed<'_> { + type Error = std::convert::Infallible; + fn is_player_class(&self) -> bool { + false + } + fn velocity(&mut self) -> Result { + Ok(*self.0) + } + fn add_delta_movement(&mut self, delta: Vec3) -> Result<(), Self::Error> { + let sum = Vec3::new(self.0.x + delta.x, self.0.y + delta.y, self.0.z + delta.z); + if delta.is_finite() && sum.is_finite() { + *self.0 = sum; + } + Ok(()) + } +} pub fn wrap(a: f32) -> f32 { let mut a = a % 360.0; if a >= 180.0 { @@ -140,6 +191,40 @@ impl MobWorld<'_> { } out } + /// Entity.updateFluidHeightAndDoFluidPushing's currents over the deflated + /// box: the flow of every water and lava block it touches, summed (a thin + /// fluid layer scales its flow down), as (water, lava). + fn fluid_currents(&self, bounds: Aabb) -> (CurrentAccumulator, CurrentAccumulator) { + let base_y = bounds.min_position().y; + let b = bounds.deflate(Vec3::new(0.001, 0.001, 0.001)); + let (lo, hi) = (b.min_position(), b.max_position()); + let mut scene = FlowScene(self); + let (mut water, mut lava) = (CurrentAccumulator::default(), CurrentAccumulator::default()); + for x in lo.x.floor() as i32..=(hi.x.ceil() as i32 - 1) { + for y in lo.y.floor() as i32..=(hi.y.ceil() as i32 - 1) { + for z in lo.z.floor() as i32..=(hi.z.ceil() as i32 - 1) { + let p = BlockPos::new(x, y, z); + let state = self.fluid_tables.fluid(self.world.block(x, y, z)); + let current = match state.kind() { + FluidKind::Empty => continue, + FluidKind::Water | FluidKind::FlowingWater => &mut water, + FluidKind::Lava | FluidKind::FlowingLava => &mut lava, + }; + let surface = f64::from(y) + self.fluid_height(x, y, z).1; + if surface < lo.y { + continue; + } + current.height = current.height.max(surface - base_y); + let mut flow = flow_at(&mut scene, state, p).unwrap_or(Vec3::ZERO); + if current.height < 0.4 { + flow = flow.scale(current.height); + } + current.accumulate(flow); + } + } + } + (water, lava) + } /// `BlockState.getCollisionShape(level, pos)` (the empty collision /// context) as boxes in the world. pub fn block_collision_boxes(&self, x: i32, y: i32, z: i32) -> Vec { @@ -334,6 +419,27 @@ impl Mob { pub fn is_in_liquid(&self) -> bool { self.fluids.in_water || self.fluids.in_lava } + /// `Entity.isPushedByFluid`: water animals, turtles, axolotls, frogs and + /// nautiluses keep their own course. + fn pushed_by_fluid(&self) -> bool { + use super::Species as S; + !matches!( + self.species(), + S::Tadpole + | S::Cod + | S::Salmon + | S::TropicalFish + | S::Pufferfish + | S::Squid + | S::GlowSquid + | S::Dolphin + | S::Turtle + | S::Axolotl + | S::Frog + | S::Nautilus + | S::ZombieNautilus + ) + } fn affected_by_fluids(&self) -> bool { true } @@ -450,6 +556,22 @@ impl Mob { pub(super) fn update_fluids(&mut self, w: &MobWorld, events: &mut Vec) { let was_touching_water = self.fluids.in_water; self.fluids = w.fluids(self.bounds(), self.eye_y()); + // EntityFluidInteraction.applyCurrentTo: the flow pushes the mob. + if (self.fluids.in_water || self.fluids.in_lava) && self.pushed_by_fluid() { + let (water, lava) = w.fluid_currents(self.bounds()); + let lava_scale = if w.nether { + 0.007 + } else { + 0.0023333333333333335 + }; + let mut pushed = Pushed(&mut self.velocity); + if water.height > 0.0 { + let _ = water.apply_to(&mut pushed, 0.014); + } + if lava.height > 0.0 { + let _ = lava.apply_to(&mut pushed, lava_scale); + } + } if self.fluids.in_water { self.fall_distance = 0.0; if !was_touching_water && !self.first_tick { From a19cad22cb4efc178809343a4fb1b5df6298805c Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Tue, 27 Oct 2026 05:51:10 +0100 Subject: [PATCH 2/2] A mob jumps out of a fluid only where Entity.isFree allows: no liquid in the box --- crates/horizon-client/src/mobs/living.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/mobs/living.rs b/crates/horizon-client/src/mobs/living.rs index ce3183721..b1b2a3c38 100644 --- a/crates/horizon-client/src/mobs/living.rs +++ b/crates/horizon-client/src/mobs/living.rs @@ -320,6 +320,18 @@ impl MobWorld<'_> { }; scene.unobstructed(b).unwrap_or(false) } + /// `Entity.isFree`: no collision and `LevelReader.containsAnyLiquid` false + /// over the blocks from the box's minimum corner to its maximum corner. + pub fn is_free(&self, b: Aabb) -> bool { + let (lo, hi) = (b.min_position(), b.max_position()); + let liquid = (lo.x.floor() as i32..=hi.x.floor() as i32).any(|x| { + (lo.y.floor() as i32..=hi.y.floor() as i32).any(|y| { + (lo.z.floor() as i32..=hi.z.floor() as i32) + .any(|z| self.facts.get(self.world.block(x, y, z)).fluid != Fluid::None) + }) + }); + !liquid && self.no_collision(b) + } } impl Mob { /// `Entity.refreshDimensions` after growing from `old_w` by `old_h`: @@ -938,7 +950,7 @@ impl Mob { // jumpOutOfFluid let m = self.velocity; if self.horizontal_collision - && w.no_collision(self.bounds().move_by(Vec3::new( + && w.is_free(self.bounds().move_by(Vec3::new( m.x, m.y + f64::from(0.6_f32) - self.position.y + old_y, m.z,