1use azalea_block::{
2 BlockState,
3 fluid_state::{FluidKind, FluidState},
4};
5use azalea_core::{
6 direction::Direction,
7 position::{BlockPos, Vec3},
8};
9use azalea_entity::{HasClientLoaded, LocalEntity, Physics, Position};
10use azalea_registry::builtin::{BlockKind, EnvironmentAttribute};
11use azalea_world::{World, WorldName, Worlds};
12use bevy_ecs::prelude::*;
13
14use crate::collision::legacy_blocks_motion;
15
16#[allow(clippy::type_complexity)]
17pub fn update_in_water_state_and_do_fluid_pushing(
18 mut query: Query<
19 (&mut Physics, &Position, &WorldName),
20 (With<LocalEntity>, With<HasClientLoaded>),
21 >,
22 worlds: Res<Worlds>,
23) {
24 for (mut physics, position, world_name) in &mut query {
25 let Some(world_lock) = worlds.get(world_name) else {
26 continue;
27 };
28 let world = world_lock.read();
29
30 physics.water_fluid_height = 0.;
33 physics.lava_fluid_height = 0.;
34
35 update_in_water_state_and_do_water_current_pushing(&mut physics, &world, *position);
36
37 let is_fast_lava = world
42 .registries
43 .dimension_type
44 .map
45 .get(&**world_name)
46 .map(|i| {
47 i.attributes
48 .values
49 .get(&EnvironmentAttribute::GameplayFastLava)
50 .is_some()
51 })
52 .unwrap_or_default();
53 let lava_push_factor = if is_fast_lava {
54 0.007
55 } else {
56 0.0023333333333333335
57 };
58
59 update_fluid_height_and_do_fluid_pushing(
60 &mut physics,
61 &world,
62 FluidKind::Lava,
63 lava_push_factor,
64 );
65 }
66}
67fn update_in_water_state_and_do_water_current_pushing(
68 physics: &mut Physics,
69 world: &World,
70 _position: Position,
71) {
72 if update_fluid_height_and_do_fluid_pushing(physics, world, FluidKind::Water, 0.014) {
81 physics.reset_fall_distance();
86 physics.was_touching_water = true;
87 physics.clear_fire();
88 } else {
89 physics.was_touching_water = false;
90 }
91}
92
93fn update_fluid_height_and_do_fluid_pushing(
94 physics: &mut Physics,
95 world: &World,
96 checking_fluid: FluidKind,
97 fluid_push_factor: f64,
98) -> bool {
99 let checking_liquids_aabb = physics.bounding_box.deflate_all(0.001);
104
105 let min_x = checking_liquids_aabb.min.x.floor() as i32;
106 let min_y = checking_liquids_aabb.min.y.floor() as i32;
107 let min_z = checking_liquids_aabb.min.z.floor() as i32;
108
109 let max_x = checking_liquids_aabb.max.x.ceil() as i32;
110 let max_y = checking_liquids_aabb.max.y.ceil() as i32;
111 let max_z = checking_liquids_aabb.max.z.ceil() as i32;
112
113 let mut min_height_touching = 0.;
114 let is_entity_pushable_by_fluid = true;
115 let mut touching_fluid = false;
116 let mut additional_player_delta = Vec3::ZERO;
117 let mut num_fluids_being_touched = 0;
118
119 for cur_x in min_x..max_x {
120 for cur_y in min_y..max_y {
121 for cur_z in min_z..max_z {
122 let cur_pos = BlockPos::new(cur_x, cur_y, cur_z);
123 let Some(fluid_at_cur_pos) = world.get_fluid_state(cur_pos) else {
124 continue;
125 };
126 if fluid_at_cur_pos.kind != checking_fluid {
127 continue;
128 }
129 let fluid_max_y = (cur_y as f32 + fluid_at_cur_pos.height()) as f64;
130 if fluid_max_y < checking_liquids_aabb.min.y {
131 continue;
132 }
133 touching_fluid = true;
134 min_height_touching = f64::max(
135 fluid_max_y - checking_liquids_aabb.min.y,
136 min_height_touching,
137 );
138 if !is_entity_pushable_by_fluid {
139 continue;
140 }
141 let mut additional_player_delta_for_fluid =
142 get_fluid_flow(&fluid_at_cur_pos, world, cur_pos);
143 if min_height_touching < 0.4 {
144 additional_player_delta_for_fluid *= min_height_touching;
145 };
146
147 additional_player_delta += additional_player_delta_for_fluid;
148 num_fluids_being_touched += 1;
149 }
150 }
151 }
152
153 if additional_player_delta.length() > 0. {
154 additional_player_delta /= num_fluids_being_touched as f64;
155
156 let player_delta = physics.velocity;
161 additional_player_delta *= fluid_push_factor;
162 const MIN_PUSH: f64 = 0.003;
163 const MIN_PUSH_LENGTH: f64 = MIN_PUSH * 1.5;
164
165 if player_delta.x.abs() < MIN_PUSH
166 && player_delta.z.abs() < MIN_PUSH
167 && additional_player_delta.length() < MIN_PUSH_LENGTH
168 {
169 additional_player_delta = additional_player_delta.normalize() * MIN_PUSH_LENGTH;
170 }
171
172 physics.velocity += additional_player_delta;
173 }
174
175 match checking_fluid {
176 FluidKind::Water => physics.water_fluid_height = min_height_touching,
177 FluidKind::Lava => physics.lava_fluid_height = min_height_touching,
178 FluidKind::Empty => panic!("FluidKind::Empty should not be passed to update_fluid_height"),
179 };
180
181 touching_fluid
182}
183
184pub fn update_swimming() {
185 }
187
188pub fn get_fluid_flow(fluid: &FluidState, world: &World, pos: BlockPos) -> Vec3 {
190 let mut z_flow: f64 = 0.;
191 let mut x_flow: f64 = 0.;
192
193 let cur_fluid_height = fluid.height();
194
195 for direction in Direction::HORIZONTAL {
196 let adjacent_block_pos = pos.offset_with_direction(direction);
197
198 let adjacent_block_state = world
199 .get_block_state(adjacent_block_pos)
200 .unwrap_or_default();
201 let adjacent_fluid_state = FluidState::from(adjacent_block_state);
202
203 if !fluid.affects_flow(&adjacent_fluid_state) {
204 continue;
205 };
206 let mut adjacent_fluid_height = adjacent_fluid_state.height();
207 let mut adjacent_height_difference: f32 = 0.;
208
209 if adjacent_fluid_height == 0. {
210 if !legacy_blocks_motion(adjacent_block_state) {
211 let block_pos_below_adjacent = adjacent_block_pos.down(1);
212 let fluid_below_adjacent = world
213 .get_fluid_state(block_pos_below_adjacent)
214 .unwrap_or_default();
215
216 if fluid.affects_flow(&fluid_below_adjacent) {
217 adjacent_fluid_height = fluid_below_adjacent.height();
218 if adjacent_fluid_height > 0. {
219 adjacent_height_difference =
220 cur_fluid_height - (adjacent_fluid_height - 0.8888889);
221 }
222 }
223 }
224 } else if adjacent_fluid_height > 0. {
225 adjacent_height_difference = cur_fluid_height - adjacent_fluid_height;
226 }
227
228 if adjacent_height_difference != 0. {
229 x_flow += (direction.x() as f32 * adjacent_height_difference) as f64;
230 z_flow += (direction.z() as f32 * adjacent_height_difference) as f64;
231 }
232 }
233
234 let mut flow = Vec3::new(x_flow, 0., z_flow);
235 if fluid.falling {
236 for direction in Direction::HORIZONTAL {
237 let adjacent_block_pos = pos.offset_with_direction(direction);
238 if is_solid_face(fluid, world, adjacent_block_pos, direction)
239 || is_solid_face(fluid, world, adjacent_block_pos.up(1), direction)
240 {
241 flow = flow.normalize() + Vec3::new(0., -6., 0.);
242 break;
243 }
244 }
245 }
246
247 flow.normalize()
248}
249
250fn is_solid_face(
252 fluid: &FluidState,
253 world: &World,
254 adjacent_pos: BlockPos,
255 direction: Direction,
256) -> bool {
257 let block_state = world.get_block_state(adjacent_pos).unwrap_or_default();
258 let fluid_state = world.get_fluid_state(adjacent_pos).unwrap_or_default();
259 if fluid_state.is_same_kind(fluid) {
260 return false;
261 }
262 if direction == Direction::Up {
263 return true;
264 }
265 let registry_block = BlockKind::from(block_state);
266 if matches!(
267 registry_block,
268 BlockKind::Ice | BlockKind::FrostedIce
270 ) {
271 return false;
272 }
273 is_face_sturdy(block_state, world, adjacent_pos, direction)
274}
275
276fn is_face_sturdy(
277 _block_state: BlockState,
278 _world: &World,
279 _pos: BlockPos,
280 _direction: Direction,
281) -> bool {
282 false
290}