1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//! Fluids of the modern client: `EntityFluidInteraction` (one sweep for height, eyes and current),
//! fluid heights and `FlowingFluid.getFlow`.
use super::rules::shared::{StatePhys, block_flags as bf, state_flags as sf};
use super::collide::{Extra, World};
use super::math::{V3, ceil, floor};
use super::rules::Rules;
use crate::math::Aabb;
use crate::world::BlockView;
/// Water (1) or lava (2) in a fluid byte; 0 = none.
#[inline]
pub(crate) fn kind(f: u8) -> u8 {
f >> 6
}
/// Fluid amount `1..=8`.
#[inline]
pub(crate) fn amount(f: u8) -> u8 {
f & 15
}
/// `FluidState.getOwnHeight()`: `amount / 9.0F`.
#[inline]
pub(crate) fn own_height(f: u8) -> f32 {
f32::from(amount(f)) / 9.0
}
/// Per-fluid tracker result.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct Tracker {
pub height: f64,
pub eyes_inside: bool,
current: V3,
count: u32,
}
/// The result of one `EntityFluidInteraction.update`.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct Interaction {
pub water: Tracker,
pub lava: Tracker,
}
impl Tracker {
/// `Tracker.applyCurrentTo` for a player: the averaged current, scaled, with vanilla's minimum
/// push when the entity barely moves.
pub(crate) fn current_impulse(&self, vel: V3, scale: f64) -> Option<V3> {
if self.count == 0 || self.current.length_sqr() < f64::from(1.0e-5f32) {
return None;
}
let mut imp = self.current.scale(1.0 / f64::from(self.count)).scale(scale);
if vel.x.abs() < 0.003 && vel.z.abs() < 0.003 && imp.length() < 0.004_500_000_000_000_000_5
{
imp = imp.normalize().scale(0.004_500_000_000_000_000_5);
}
Some(imp)
}
/// [`Tracker::current_impulse`] for an entity that is not a player: the current is normalized
/// instead of averaged.
pub(crate) fn current_impulse_entity(&self, vel: V3, scale: f64) -> Option<V3> {
if self.count == 0 || self.current.length_sqr() < f64::from(1.0e-5f32) {
return None;
}
let mut imp = self.current.normalize().scale(scale);
if vel.x.abs() < 0.003 && vel.z.abs() < 0.003 && imp.length() < 0.004_500_000_000_000_000_5
{
imp = imp.normalize().scale(0.004_500_000_000_000_000_5);
}
Some(imp)
}
}
impl<R: Rules, W: BlockView<State = R::State>, E: Extra> World<'_, R, W, E> {
/// The fluid byte at a position (`Level.getFluidState`). Always inlined: it is one cached
/// block read, and the compiler's size heuristic otherwise keeps it out of line at some
/// callers (measured with the engine bench).
#[inline(always)]
pub(crate) fn fluid(&self, x: i32, y: i32, z: i32) -> u8 {
self.phys(x, y, z).fluid()
}
/// `FluidState.getHeight(level, pos)`: 1 with the same fluid above, else `amount / 9`.
#[inline]
pub(crate) fn fluid_height(&self, x: i32, y: i32, z: i32, f: u8) -> f32 {
if kind(self.fluid(x, y + 1, z)) == kind(f) {
1.0
} else {
own_height(f)
}
}
/// `EntityFluidInteraction.update(entity, ignoreCurrent)`.
pub(crate) fn fluid_interaction(
&self,
bb: &Aabb,
block_xz: [i32; 2],
eye_y: f64,
ignore_current: bool,
) -> Interaction {
// getFluidInteractionBox: the box deflated by 0.001.
let b = super::math::deflate(bb, 0.001);
self.fluid_interaction_in(&b, bb.min_y, block_xz, eye_y, ignore_current)
}
/// [`World::fluid_interaction`] for an interaction box `b` already made (a passenger's box cut
/// by its boat); `entity_y` is the bottom of the entity's own box.
#[inline(always)]
pub(crate) fn fluid_interaction_in(
&self,
b: &Aabb,
entity_y: f64,
block_xz: [i32; 2],
eye_y: f64,
ignore_current: bool,
) -> Interaction {
let b = *b;
let mut out = Interaction::default();
let (x0, y0, z0) = (floor(b.min_x), floor(b.min_y), floor(b.min_z));
let (x1, y1, z1) = (ceil(b.max_x) - 1, ceil(b.max_y) - 1, ceil(b.max_z) - 1);
if !self.fluid_region_loaded(x0 - 1, y0, z0 - 1, x1 + 1, y1, z1 + 1) {
return out;
}
for x in x0..=x1 {
for y in y0..=y1 {
for z in z0..=z1 {
let f = self.fluid(x, y, z);
let k = kind(f);
if k == 0 {
continue;
}
let bottom = f64::from(y);
let top = bottom + f64::from(self.fluid_height(x, y, z, f));
if top < b.min_y {
continue;
}
let t = if k == 1 {
&mut out.water
} else {
&mut out.lava
};
if x == block_xz[0] && z == block_xz[1] && eye_y >= bottom {
// 26.3 `FluidState.getHeightForCamera`: a source under a block whose
// bottom face is sturdy is full height for the eyes.
let eye_top = if R::EYE_CAMERA_HEIGHT
&& amount(f) == 8
&& f & 16 == 0
&& R::sturdy_down(self.state(x, y + 1, z))
{
bottom + 1.0
} else {
top
};
if eye_y <= eye_top {
t.eyes_inside = true;
}
}
t.height = super::math::java_max(top - entity_y, t.height);
if !ignore_current {
let mut flow = self.flow(x, y, z, f);
if t.height < 0.4 {
flow = flow.scale(t.height);
}
t.current = t.current.add(flow);
t.count += 1;
}
}
}
}
out
}
/// `hasFluidAndLoaded`: every chunk of the range is loaded. (Vanilla also skips the sweep when
/// no section in the range holds a fluid, which cannot change the result.)
fn fluid_region_loaded(&self, x0: i32, y0: i32, z0: i32, x1: i32, y1: i32, z1: i32) -> bool {
let _ = (y0, y1);
for cz in (z0 >> 4)..=(z1 >> 4) {
for cx in (x0 >> 4)..=(x1 >> 4) {
if !self.w.is_chunk_loaded(cx, cz) {
return false;
}
}
}
true
}
/// `FlowingFluid.getFlow(level, pos, fluidState)`.
pub(crate) fn flow(&self, x: i32, y: i32, z: i32, f: u8) -> V3 {
// Direction.Plane.HORIZONTAL order: north, east, south, west.
const SIDES: [(i32, i32); 4] = [(0, -1), (1, 0), (0, 1), (-1, 0)];
let k = kind(f);
let here = own_height(f);
let affects = |n: u8| kind(n) == 0 || kind(n) == k;
let (mut fx, mut fz) = (0.0f64, 0.0f64);
for (dx, dz) in SIDES {
let (nx, nz) = (x + dx, z + dz);
let np = self.phys(nx, y, nz);
let n = np.fluid();
if !affects(n) {
continue;
}
let mut nh = if kind(n) == 0 { 0.0 } else { own_height(n) };
let mut dist = 0.0f32;
if nh == 0.0 {
// 26.3: the `blocks_fluid_flow` tag (`blocks_motion` + `all_signs`).
let blocks = np.flags() & sf::BLOCKS_MOTION != 0
|| R::FLOW_BLOCKED_BY_SIGNS
&& R::block_name(self.state(nx, y, nz)).ends_with("_sign");
if !blocks {
let below = self.fluid(nx, y - 1, nz);
if affects(below) {
nh = if kind(below) == 0 {
0.0
} else {
own_height(below)
};
if nh > 0.0 {
dist = here - (nh - 0.888_888_9);
}
}
}
} else if nh > 0.0 {
dist = here - nh;
}
if dist != 0.0 {
fx += f64::from(dx as f32 * dist);
fz += f64::from(dz as f32 * dist);
}
}
let mut flow = V3::new(fx, 0.0, fz);
if f & 16 != 0 {
for (i, (dx, dz)) in SIDES.into_iter().enumerate() {
if self.solid_face(x + dx, y, z + dz, i, k)
|| self.solid_face(x + dx, y + 1, z + dz, i, k)
{
flow = flow.normalize().add(V3::new(0.0, -6.0, 0.0));
break;
}
}
}
flow.normalize()
}
/// `FlowingFluid.isSolidFace(level, pos, direction)` for a horizontal direction index.
fn solid_face(&self, x: i32, y: i32, z: i32, side: usize, k: u8) -> bool {
let s = self.state(x, y, z);
let p: StatePhys = R::phys(s);
if kind(p.fluid()) == k {
return false;
}
if R::block_flags(s) & bf::ICE != 0 {
return false;
}
p.flags() & (sf::STURDY_NORTH << side) != 0
}
/// `containsAnyLiquid(box)`: the cells `floor(min)..ceil(max)`, or from 26.3 on
/// ([`Rules::LIQUID_SCAN_CLOSED`]) the closed range `floor(min)..=floor(max)`.
pub(crate) fn contains_any_liquid(&self, b: &Aabb) -> bool {
let hi = |v: f64| {
if R::LIQUID_SCAN_CLOSED {
floor(v) + 1
} else {
ceil(v)
}
};
let (x0, x1) = (floor(b.min_x), hi(b.max_x));
let (y0, y1) = (floor(b.min_y), hi(b.max_y));
let (z0, z1) = (floor(b.min_z), hi(b.max_z));
for x in x0..x1 {
for y in y0..y1 {
for z in z0..z1 {
if kind(self.fluid(x, y, z)) != 0 {
return true;
}
}
}
}
false
}
}