use super::collide::{Placed, Plain, ShapeCtx, World, collide, make_box, placed_shape};
use super::fluid::kind;
use super::inside::{Movement, Visited, axis_order, for_each_block_between};
use super::math::{V3, approx_equal, bx, deflate, floor, intersects, java_max, java_min, mv};
use super::rules::Rules;
use super::rules::shared::{
BlockKind, VoxelClass, VoxelShapeRef, block_flags as bf, state_flags as sf,
};
use crate::math::Aabb;
use crate::pick::{BlockHit, Cells, Direction, HitKind};
use crate::world::BlockView;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Side {
Server,
Client,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct LegacyRandom {
seed: i64,
}
const MULTIPLIER: i64 = 0x5_DEEC_E66D;
const MASK: i64 = (1 << 48) - 1;
impl LegacyRandom {
pub(crate) fn new(seed: i64) -> LegacyRandom {
LegacyRandom {
seed: (seed ^ MULTIPLIER) & MASK,
}
}
fn next(&mut self, bits: u32) -> i32 {
self.seed = self.seed.wrapping_mul(MULTIPLIER).wrapping_add(11) & MASK;
(self.seed >> (48 - bits)) as i32
}
pub(crate) fn next_float(&mut self) -> f32 {
self.next(24) as f32 * 5.960_464_5e-8_f32
}
pub(crate) fn next_double(&mut self) -> f64 {
let a = i64::from(self.next(26));
let b = i64::from(self.next(27));
((a << 27) + b) as f64 * f64::from(1.110_223e-16_f32)
}
pub(crate) fn triangle(&mut self, mean: f64, spread: f64) -> f64 {
let a = self.next_double();
let b = self.next_double();
mean + spread * (a - b)
}
pub(crate) fn splash(&mut self, width: f32) {
self.next_float();
self.next_float();
let n = 1.0f32 + width * 20.0;
let mut i = 0;
while (i as f32) < n {
self.next_double();
self.next_double();
self.next_double();
i += 1;
}
let mut i = 0;
while (i as f32) < n {
self.next_double();
self.next_double();
i += 1;
}
}
}
#[path = "../v1_8/atan_table.rs"]
#[allow(dead_code, clippy::duplicate_mod)]
mod atan_table;
pub(crate) fn atan2(mut y: f64, mut x: f64) -> f64 {
let d = x * x + y * y;
if d.is_nan() {
return f64::NAN;
}
let neg_y = y < 0.0;
if neg_y {
y = -y;
}
let neg_x = x < 0.0;
if neg_x {
x = -x;
}
let steep = y > x;
if steep {
core::mem::swap(&mut x, &mut y);
}
let half = 0.5 * d;
let r = f64::from_bits((6_910_469_410_427_058_090_i64 - ((d.to_bits() as i64) >> 1)) as u64);
let r = r * (1.5 - half * r * r);
x *= r;
y *= r;
let bias = f64::from_bits(4_805_340_802_404_319_232);
let yp = bias + y;
let i = yp.to_bits() as i32 as usize;
let phi = f64::from_bits(atan_table::ARCSINE[i]);
let c = f64::from_bits(atan_table::COSINE[i]);
let s = yp - bias;
let sd = y * c - x * s;
let dd = (6.0 + sd * sd) * sd * 0.166_666_666_666_666_66;
let mut t = phi + dd;
if steep {
t = core::f64::consts::FRAC_PI_2 - t;
}
if neg_x {
t = core::f64::consts::PI - t;
}
if neg_y {
t = -t;
}
t
}
#[inline]
pub(crate) fn degrees(radians: f64) -> f32 {
(radians * f64::from(180.0f32 / core::f32::consts::PI)) as f32
}
pub(crate) fn lerp_rotation(mut o: f32, r: f32) -> f32 {
while r - o < -180.0 {
o -= 360.0;
}
while r - o >= 180.0 {
o += 360.0;
}
o + 0.2 * (r - o)
}
const MOVES: usize = 4;
#[derive(Clone, Copy, Debug)]
pub(crate) struct Body {
pub pos: V3,
pub old: V3,
pub vel: V3,
pub bb: Aabb,
pub width: f32,
pub height: f32,
pub eye_height: f32,
pub box_drop: f32,
pub below_offset: f32,
pub yaw: f32,
pub pitch: f32,
pub yaw_o: f32,
pub pitch_o: f32,
pub on_ground: bool,
pub hcol: bool,
pub vcol: bool,
pub vcol_below: bool,
pub fall: f64,
pub stuck: V3,
pub in_water: bool,
pub water_height: f64,
pub lava_height: f64,
pub eye_in_water: bool,
pub first_tick: bool,
pub supporting: Option<[i32; 3]>,
pub no_blocks: bool,
pub no_physics: bool,
pub removed: bool,
pub tick_count: i32,
pub moves: [Option<Movement>; MOVES],
pub last_moves: [Option<Movement>; MOVES + 1],
pub rng: LegacyRandom,
pub pending_old: Option<(V3, f32, f32)>,
pub moved: [[f64; 3]; MOVED],
pub moved_n: u8,
}
pub(crate) const MOVED: usize = 4;
impl Body {
pub(crate) fn new(pos: V3, vel: V3, width: f32, height: f32, eye_height: f32) -> Body {
let mut b = Body {
pos,
old: pos,
vel,
bb: make_box(width, height, pos.x, pos.y, pos.z),
width,
height,
eye_height,
box_drop: 0.0,
below_offset: 0.500_001,
yaw: 0.0,
pitch: 0.0,
yaw_o: 0.0,
pitch_o: 0.0,
on_ground: false,
hcol: false,
vcol: false,
vcol_below: false,
fall: 0.0,
stuck: V3::ZERO,
in_water: false,
water_height: 0.0,
lava_height: 0.0,
eye_in_water: false,
first_tick: true,
supporting: None,
no_blocks: false,
no_physics: false,
removed: false,
tick_count: 0,
moves: [None; MOVES],
last_moves: [None; MOVES + 1],
rng: LegacyRandom::new(0),
pending_old: None,
moved: [[0.0; 3]; MOVED],
moved_n: 0,
};
b.set_pos(pos);
b
}
pub(crate) fn set_pos(&mut self, p: V3) {
self.pos = p;
self.bb = self.box_at(p);
}
pub(crate) fn box_at(&self, p: V3) -> Aabb {
if self.box_drop == 0.0 {
make_box(self.width, self.height, p.x, p.y, p.z)
} else {
let w = f64::from(self.width / 2.0);
let y = p.y - f64::from(self.box_drop);
bx(
p.x - w,
y,
p.z - w,
p.x + w,
y + f64::from(self.height),
p.z + w,
)
}
}
pub(crate) fn start_tick(&mut self) {
if let Some((p, y, x)) = self.pending_old.take() {
self.old = p;
self.yaw_o = y;
self.pitch_o = x;
} else {
self.old = self.pos;
self.yaw_o = self.yaw;
self.pitch_o = self.pitch;
}
self.moved_n = 0;
self.tick_count += 1;
}
#[inline]
fn note_moved(&mut self, m: V3) {
if usize::from(self.moved_n) < MOVED {
self.moved[usize::from(self.moved_n)] = m.arr();
self.moved_n += 1;
}
}
pub(crate) fn ctx(&self) -> ShapeCtx {
ShapeCtx {
feet_y: self.pos.y,
descending: false,
fall_distance: self.fall,
leather_boots: false,
}
}
#[inline]
pub(crate) fn apply_gravity(&mut self, g: f64) {
if g != 0.0 {
self.vel = self.vel.add(V3::new(0.0, -g, 0.0));
}
}
pub(crate) fn in_lava(&self) -> bool {
!self.first_tick && self.lava_height > 0.0
}
pub(crate) fn block_pos(&self) -> [i32; 3] {
[floor(self.pos.x), floor(self.pos.y), floor(self.pos.z)]
}
fn record_movement(&mut self, m: Movement) {
if let Some(slot) = self.moves.iter_mut().find(|m| m.is_none()) {
*slot = Some(m);
} else {
let a = self.moves[0].unwrap_or(m);
let b = self.moves[1].unwrap_or(m);
self.moves.copy_within(2.., 1);
self.moves[0] = Some(Movement {
from: a.from,
to: b.to,
axis_dependent: None,
});
self.moves[MOVES - 1] = Some(m);
}
}
}
pub(crate) type Env<'a, R, W> = World<'a, R, W, Plain>;
pub(crate) fn with_env<R: Rules, W: BlockView<State = R::State>, T>(
world: &W,
f: impl FnOnce(&Env<'_, R, W>) -> T,
) -> T {
let border = super::collide::Border::new(&world.world_border());
let w = World::<R, W, Plain> {
w: world,
border: &border,
min_y: -64,
max_y: 319,
ext: Plain,
_r: core::marker::PhantomData,
};
f(&w)
}
pub(crate) fn update_fluids<R: Rules, W: BlockView<State = R::State>>(
b: &mut Body,
w: &Env<'_, R, W>,
pushed: bool,
splash: bool,
) {
let fi = w.fluid_interaction(
&b.bb,
[floor(b.pos.x), floor(b.pos.z)],
b.pos.y + f64::from(b.eye_height),
!pushed,
);
b.water_height = fi.water.height;
b.lava_height = fi.lava.height;
b.eye_in_water = fi.water.eyes_inside;
let in_water = fi.water.height > 0.0;
let in_lava = fi.lava.height > 0.0;
if in_water {
b.fall = 0.0;
if !b.in_water && !b.first_tick && splash {
b.rng.splash(b.width);
}
}
b.in_water = in_water;
if pushed {
if in_water && let Some(imp) = fi.water.current_impulse_entity(b.vel, 0.014) {
b.vel = b.vel.add(imp);
}
if in_lava
&& let Some(imp) = fi
.lava
.current_impulse_entity(b.vel, 0.002_333_333_333_333_333_5)
{
b.vel = b.vel.add(imp);
}
}
}
pub(crate) fn base_tick<R: Rules, W: BlockView<State = R::State>>(
b: &mut Body,
w: &Env<'_, R, W>,
pushed: bool,
splash: bool,
) {
update_fluids::<R, W>(b, w, pushed, splash);
if b.in_lava() {
b.fall *= 0.5;
}
if b.pos.y < f64::from(w.min_y - 64) {
b.removed = true;
}
b.first_tick = false;
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct MoveRules {
pub authoritative: bool,
pub simulate: bool,
pub gravity: f64,
pub air_drag: f32,
}
pub(crate) fn entity_move<R: Rules, W: BlockView<State = R::State>>(
b: &mut Body,
w: &Env<'_, R, W>,
delta: V3,
mr: MoveRules,
) {
if b.no_physics {
let p = b.pos.add(delta);
b.set_pos(p);
b.note_moved(delta);
b.hcol = false;
b.vcol = false;
b.vcol_below = false;
return;
}
let mut delta = delta;
if b.stuck.length_sqr() > 1.0e-7 {
delta = delta.mul(b.stuck.x, b.stuck.y, b.stuck.z);
b.stuck = V3::ZERO;
b.vel = V3::ZERO;
}
let ctx = b.ctx();
let movement = collide::<R, _, Plain>(w, b.pos.arr(), &b.bb, delta, b.on_ground, 0.0, &ctx);
let len = movement.length_sqr();
if len > 1.0e-7 || delta.length_sqr() - len < 1.0e-7 {
if b.fall != 0.0 && len >= 1.0 && fall_reset_clip::<R, W>(b, w, movement) {
b.fall = 0.0;
}
let from = b.pos;
let to = from.add(movement);
b.record_movement(Movement {
from,
to,
axis_dependent: Some(delta),
});
b.set_pos(to);
b.note_moved(movement);
}
let xc = !approx_equal(delta.x, movement.x);
let zc = !approx_equal(delta.z, movement.z);
b.hcol = xc || zc;
let moved_vertically = delta.y.abs() > 0.0;
if moved_vertically || mr.authoritative {
b.vcol = delta.y != movement.y;
b.vcol_below = b.vcol && delta.y < 0.0;
b.on_ground = b.vcol_below;
check_supporting_block::<R, W>(b, w, b.on_ground, Some(movement));
}
let ep = on_pos::<R, W>(b, w, 0.2);
let effect = w.state(ep[0], ep[1], ep[2]);
if mr.authoritative {
if !b.in_water && movement.y < 0.0 {
b.fall -= f64::from(movement.y as f32);
}
if b.on_ground {
b.fall = 0.0;
}
}
if b.removed {
return;
}
if R::RESTITUTION {
if mr.simulate && (moved_vertically && b.vcol || b.hcol) {
restitute::<R>(b, effect, xc, zc, movement, mr);
}
} else {
if b.hcol {
b.vel = V3::new(
if xc { 0.0 } else { b.vel.x },
b.vel.y,
if zc { 0.0 } else { b.vel.z },
);
}
if mr.simulate && delta.y != movement.y {
after_fall_on::<R>(b, effect);
}
}
let k = f64::from(block_speed_factor::<R, W>(b, w));
b.vel = V3::new(b.vel.x * k, b.vel.y, b.vel.z * k);
}
fn fall_reset_clip<R: Rules, W: BlockView<State = R::State>>(
b: &Body,
w: &Env<'_, R, W>,
movement: V3,
) -> bool {
let d = java_min(movement.length(), 8.0);
let to = b.pos.add(movement.normalize().scale(d));
let full = R::voxel(R::FULL_CUBE_VOXEL);
crate::pick::traverse(b.pos.arr(), to.arr(), |p| {
let s = w.state(p[0], p[1], p[2]);
let block = if R::block_flags(s) & bf::FALL_DAMAGE_RESETTING != 0 {
clip_voxel(full, None, b.pos.arr(), to.arr(), p)
} else {
None
};
let f = R::phys(s).fluid();
let liquid = if kind(f) == 1 {
let h = f64::from(w.fluid_height(p[0], p[1], p[2], f));
let fluid_box = [Aabb::new(0.0, 0.0, 0.0, 1.0, h, 1.0)];
fluid_clip(&fluid_box, b.pos.arr(), to.arr(), p)
} else {
None
};
let dist = |h: &Option<BlockHit>| {
h.map_or(f64::MAX, |h| {
crate::pick::length_sqr(h.location, b.pos.arr())
})
};
if dist(&block) <= dist(&liquid) {
block
} else {
liquid
}
})
.is_some()
}
fn fluid_clip(boxes: &[Aabb], from: [f64; 3], to: [f64; 3], pos: [i32; 3]) -> Option<BlockHit> {
if boxes[0].max_y <= 0.0 {
return None;
}
let cells = Cells {
cube: false,
sizes: [1, 1, 1],
xs: &[0.0, 1.0],
ys: &[0.0, boxes[0].max_y],
zs: &[0.0, 1.0],
cells: &[1],
empty: false,
boxes,
};
crate::pick::clip_cells(&cells, None, from, to, pos)
}
pub(crate) fn check_supporting_block<R: Rules, W: BlockView<State = R::State>>(
b: &mut Body,
w: &Env<'_, R, W>,
on_ground: bool,
movement: Option<V3>,
) {
if !on_ground {
b.no_blocks = false;
b.supporting = None;
return;
}
let bb = b.bb;
let test = bx(
bb.min_x,
bb.min_y - 1.0e-6,
bb.min_z,
bb.max_x,
bb.min_y,
bb.max_z,
);
let ctx = b.ctx();
let mut s = w.supporting_block(b.pos.arr(), &test, &ctx);
if s.is_some() || b.no_blocks {
b.supporting = s;
} else if let Some(m) = movement {
let t2 = mv(&test, -m.x, 0.0, -m.z);
s = w.supporting_block(b.pos.arr(), &t2, &ctx);
b.supporting = s;
}
b.no_blocks = s.is_none();
}
pub(crate) fn on_pos<R: Rules, W: BlockView<State = R::State>>(
b: &Body,
w: &Env<'_, R, W>,
offset: f32,
) -> [i32; 3] {
if let Some(p) = b.supporting {
if offset <= 1.0e-5 {
return p;
}
let below = w.state(p[0], p[1], p[2]);
let flags = R::block_flags(below);
let keep = (offset <= 0.5 && flags & bf::FENCES != 0)
|| flags & bf::WALLS != 0
|| flags & bf::FENCE_GATE != 0;
if keep {
p
} else {
[p[0], floor(b.pos.y - f64::from(offset)), p[2]]
}
} else {
[
floor(b.pos.x),
floor(b.pos.y - f64::from(offset)),
floor(b.pos.z),
]
}
}
fn block_speed_factor<R: Rules, W: BlockView<State = R::State>>(
b: &Body,
w: &Env<'_, R, W>,
) -> f32 {
let bp = b.block_pos();
let s = w.state(bp[0], bp[1], bp[2]);
let here = R::speed_factor(s);
let k = R::phys(s).kind();
if k == BlockKind::Water as u8 || k == BlockKind::BubbleColumn as u8 {
return here;
}
if here == 1.0 {
let p = on_pos::<R, W>(b, w, b.below_offset);
R::speed_factor(w.state(p[0], p[1], p[2]))
} else {
here
}
}
fn after_fall_on<R: Rules>(b: &mut Body, s: R::State) {
let v = b.vel;
if R::phys(s).kind() == BlockKind::SlimeBlock as u8 {
if v.y < 0.0 {
b.vel = V3::new(v.x, -v.y * 0.8, v.z);
}
} else if R::block_flags(s) & bf::BED != 0 {
if v.y < 0.0 {
b.vel = V3::new(v.x, -v.y * f64::from(0.66f32) * 0.8, v.z);
}
} else {
b.vel = V3::new(v.x, v.y * 0.0, v.z);
}
}
fn restitute<R: Rules>(
b: &mut Body,
effect: R::State,
xc: bool,
zc: bool,
movement: V3,
mr: MoveRules,
) {
let c = b.vel;
let mut o = c;
let mut r = 0.0;
if xc {
o.x = -c.x * r;
}
if zc {
o.z = -c.z * r;
}
if b.vcol {
let g = mr.gravity;
if b.vcol_below {
let fast = if R::BOUNCE_ABOVE_GRAVITY {
-c.y > g
} else {
-c.y >= g || (-c.y).is_nan()
};
r = if fast && R::phys(effect).kind() != BlockKind::HoneyBlock as u8 {
java_max(r, f64::from(R::bounce_restitution(effect) * 0.8f32))
} else {
0.0
};
}
let (gc, drag) = if r > 0.0 {
let portion = movement.y / c.y;
(
portion * g,
super::math::lerp(portion, 1.0, f64::from(mr.air_drag)),
)
} else {
(0.0, 1.0)
};
o.y = (gc - c.y) * drag * r;
}
b.vel = o;
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct InsideRules {
pub projectile_bubbles: bool,
pub no_bubbles: bool,
}
pub(crate) fn apply_effects_from_blocks<R: Rules, W: BlockView<State = R::State>>(
b: &mut Body,
w: &Env<'_, R, W>,
ir: InsideRules,
) {
let mut list = [None; MOVES + 1];
let mut n = 0;
for m in b.moves.iter().flatten() {
list[n] = Some(*m);
n += 1;
}
b.moves = [None; MOVES];
if n == 0 {
list[0] = Some(Movement {
from: b.old,
to: b.pos,
axis_dependent: None,
});
} else if list[n - 1].is_some_and(|l| l.to.dist_sqr(b.pos) > f64::from(9.999_999_4e-11f32)) {
let last = list[n - 1].map_or(b.pos, |l| l.to);
list[n] = Some(Movement {
from: last,
to: b.pos,
axis_dependent: None,
});
}
b.last_moves = list;
apply_effects_of::<R, W>(b, w, &list, ir);
}
pub(crate) fn apply_effects_last<R: Rules, W: BlockView<State = R::State>>(
b: &mut Body,
w: &Env<'_, R, W>,
ir: InsideRules,
) {
let list = b.last_moves;
apply_effects_of::<R, W>(b, w, &list, ir);
}
pub(crate) fn apply_effects_between<R: Rules, W: BlockView<State = R::State>>(
b: &mut Body,
w: &Env<'_, R, W>,
from: V3,
to: V3,
ir: InsideRules,
) {
let mut list = [None; MOVES + 1];
list[0] = Some(Movement {
from,
to,
axis_dependent: None,
});
apply_effects_of::<R, W>(b, w, &list, ir);
}
fn apply_effects_of<R: Rules, W: BlockView<State = R::State>>(
b: &mut Body,
w: &Env<'_, R, W>,
list: &[Option<Movement>],
ir: InsideRules,
) {
if b.removed || b.no_physics {
return;
}
if b.on_ground {
let ep = on_pos::<R, W>(b, w, 0.2);
let s = w.state(ep[0], ep[1], ep[2]);
if R::phys(s).kind() == BlockKind::SlimeBlock as u8 {
let v = b.vel;
let ad = v.y.abs();
if ad < 0.1 {
let k = 0.4 + ad * 0.2;
b.vel = V3::new(v.x * k, v.y, v.z * k);
}
}
}
let mut visited = Visited::default();
for m in list.iter().flatten() {
let mut budget = 16i32;
let delta = m.to.sub(m.from);
match m.axis_dependent {
Some(orig) if delta.length_sqr() > 0.0 => {
let mut pos = m.from;
for axis in axis_order(orig) {
let am = delta.get(axis);
if am != 0.0 {
let to = pos.with(axis, pos.get(axis) + am);
budget -= check_inside::<R, W>(b, w, pos, to, &mut visited, budget, ir);
pos = to;
}
}
}
_ => budget -= check_inside::<R, W>(b, w, m.from, m.to, &mut visited, 16, ir),
}
if budget <= 0 {
check_inside::<R, W>(b, w, m.to, m.to, &mut visited, 1, ir);
}
}
}
fn check_inside<R: Rules, W: BlockView<State = R::State>>(
b: &mut Body,
w: &Env<'_, R, W>,
from: V3,
to: V3,
visited: &mut Visited,
max: i32,
ir: InsideRules,
) -> i32 {
let at_target = deflate(&b.box_at(to), f64::from(1.0e-5f32));
let far = from.dist_sqr(to) > 0.999_990_000_000_252_6 * 0.999_990_000_000_252_6;
let mut last_iter = 0;
for_each_block_between(from, to, &at_target, |pos, it| {
if it >= max {
return false;
}
last_iter = it;
let s = w.state(pos[0], pos[1], pos[2]);
let ph = R::phys(s);
if ph.flags() & sf::AIR != 0 {
return true;
}
let k = ph.kind();
let effect_kind = k == BlockKind::Cobweb as u8
|| k == BlockKind::PowderSnow as u8
|| k == BlockKind::HoneyBlock as u8
|| k == BlockKind::BubbleColumn as u8;
if !effect_kind
|| (k == BlockKind::PowderSnow as u8
&& !inside_powder_snow::<R, W>(b, w, pos, s, from, to))
|| !visited.add(pos)
{
return true;
}
let precise = far
|| intersects(
&at_target,
f64::from(pos[0]),
f64::from(pos[1]),
f64::from(pos[2]),
f64::from(pos[0]) + 1.0,
f64::from(pos[1]) + 1.0,
f64::from(pos[2]) + 1.0,
);
entity_inside::<R, W>(b, w, pos, s, precise, ir);
true
});
last_iter + 1
}
fn inside_powder_snow<R: Rules, W: BlockView<State = R::State>>(
b: &Body,
w: &Env<'_, R, W>,
pos: [i32; 3],
s: R::State,
from: V3,
to: V3,
) -> bool {
let ctx = b.ctx();
match w.shape_of_state(s, pos[0], pos[1], pos[2], Some(&ctx)) {
None | Some(Placed::Cube(_)) => true,
Some(sh @ Placed::Shape(..)) => {
let at_from = b.box_at(from);
let mut boxes = [Aabb::new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0); 8];
let mut n = 0;
sh.for_boxes(|bb| {
if n < boxes.len() {
boxes[n] = bb;
n += 1;
}
true
});
super::math::collided_along(&at_from, to.sub(from), boxes[..n].iter().copied())
}
Some(Placed::Border) => false,
Some(Placed::Moving(n, _)) => match n {},
}
}
fn entity_inside<R: Rules, W: BlockView<State = R::State>>(
b: &mut Body,
w: &Env<'_, R, W>,
pos: [i32; 3],
s: R::State,
precise: bool,
ir: InsideRules,
) {
let ph = R::phys(s);
let k = ph.kind();
if k == BlockKind::Cobweb as u8 {
b.fall = 0.0;
b.stuck = V3::new(0.25, f64::from(0.05f32), 0.25);
} else if k == BlockKind::PowderSnow as u8 {
b.fall = 0.0;
b.stuck = V3::new(f64::from(0.9f32), 1.5, f64::from(0.9f32));
} else if k == BlockKind::HoneyBlock as u8 {
if honey_sliding(b, pos) {
let v = b.vel;
let old = v.y / f64::from(0.98f32) + 0.08;
let new_y = (-0.05 - 0.08) * f64::from(0.98f32);
if old < -0.13 {
let r = -0.05 / old;
b.vel = V3::new(v.x * r, new_y, v.z * r);
} else {
b.vel = V3::new(v.x, new_y, v.z);
}
b.fall = 0.0;
}
} else if k == BlockKind::BubbleColumn as u8 && precise && !ir.no_bubbles {
let above = R::phys(w.state(pos[0], pos[1] + 1, pos[2]));
let nothing_above = (above.flags() & sf::EMPTY != 0 && above.flags() & sf::CONTEXT == 0
|| above.flags() & sf::CONTEXT != 0 && above.kind() == BlockKind::PowderSnow as u8)
&& kind(above.fluid()) == 0;
bubble_column(b, ph.aux() & 1 != 0, nothing_above, ir);
}
}
pub(crate) fn bubble_column(b: &mut Body, drag: bool, above: bool, ir: InsideRules) {
let v = b.vel;
if ir.projectile_bubbles {
let yd = match (above, drag) {
(true, true) | (false, true) => -0.03,
(true, false) => 0.1,
(false, false) => 0.06,
};
b.vel = v.add(V3::new(0.0, yd, 0.0));
if !above {
b.fall = 0.0;
}
return;
}
let y = match (above, drag) {
(true, true) => java_max(-0.9, v.y - 0.03),
(true, false) => java_min(1.8, v.y + 0.1),
(false, true) => java_max(-0.3, v.y - 0.03),
(false, false) => java_min(0.7, v.y + 0.06),
};
b.vel = V3::new(v.x, y, v.z);
if !above {
b.fall = 0.0;
}
}
fn honey_sliding(b: &Body, pos: [i32; 3]) -> bool {
if b.on_ground {
return false;
}
if b.pos.y > f64::from(pos[1]) + 0.9375 - 1.0e-7 {
return false;
}
if b.vel.y / f64::from(0.98f32) + 0.08 >= -0.08 {
return false;
}
let dx = (f64::from(pos[0]) + 0.5 - b.pos.x).abs();
let dz = (f64::from(pos[2]) + 0.5 - b.pos.z).abs();
let o = 0.4375 + f64::from(b.width / 2.0);
dx + 1.0e-7 > o || dz + 1.0e-7 > o
}
pub(crate) fn no_collision_empty<R: Rules, W: BlockView<State = R::State>>(
w: &Env<'_, R, W>,
query: &Aabb,
) -> bool {
let mut empty = true;
w.each_collision(query, None, false, |_, _, _, _| {
empty = false;
false
});
empty
}
pub(crate) fn full_block<R: Rules>(s: R::State) -> bool {
match placed_shape::<R>(s, 0, 0, 0, None) {
Some(Placed::Cube(_)) => true,
Some(Placed::Shape(r, None, _)) => {
r.boxes.len() == 1 && {
let a = r.boxes[0];
a.min_x == 0.0
&& a.min_y == 0.0
&& a.min_z == 0.0
&& a.max_x == 1.0
&& a.max_y == 1.0
&& a.max_z == 1.0
}
}
_ => false,
}
}
pub(crate) fn move_towards_closest_space<R: Rules, W: BlockView<State = R::State>>(
b: &mut Body,
w: &Env<'_, R, W>,
x: f64,
y: f64,
z: f64,
) {
let p = [floor(x), floor(y), floor(z)];
let d = [
x - f64::from(p[0]),
y - f64::from(p[1]),
z - f64::from(p[2]),
];
const DIRS: [(usize, bool, [i32; 3]); 5] = [
(2, false, [0, 0, -1]),
(2, true, [0, 0, 1]),
(0, false, [-1, 0, 0]),
(0, true, [1, 0, 0]),
(1, true, [0, 1, 0]),
];
let mut best = (1, true);
let mut closest = f64::MAX;
for (axis, positive, o) in DIRS {
let n = w.state(p[0] + o[0], p[1] + o[1], p[2] + o[2]);
if !full_block::<R>(n) {
let v = d[axis];
let oriented = if positive { 1.0 - v } else { v };
if oriented < closest {
closest = oriented;
best = (axis, positive);
}
}
}
let speed = b.rng.next_float() * 0.2 + 0.1;
let step = if best.1 { 1.0f32 } else { -1.0 };
let s = b.vel.scale(0.75);
let v = f64::from(step * speed);
b.vel = match best.0 {
0 => V3::new(v, s.y, s.z),
1 => V3::new(s.x, v, s.z),
_ => V3::new(s.x, s.y, v),
};
}
fn voxel_cells(r: &'static VoxelShapeRef) -> Cells<'static> {
Cells {
cube: r.class == VoxelClass::Simple,
sizes: r.sizes,
xs: r.xs,
ys: r.ys,
zs: r.zs,
cells: r.cells,
empty: r.empty,
boxes: r.boxes,
}
}
fn clip_voxel(
r: &'static VoxelShapeRef,
shift: Option<[f64; 3]>,
from: [f64; 3],
to: [f64; 3],
pos: [i32; 3],
) -> Option<BlockHit> {
crate::pick::clip_cells(&voxel_cells(r), shift, from, to, pos)
}
pub(crate) fn clip<R: Rules, W: BlockView<State = R::State>>(
w: &Env<'_, R, W>,
from: V3,
to: V3,
ctx: &ShapeCtx,
) -> (BlockHit, bool) {
let (f, t) = (from.arr(), to.arr());
let hit = crate::pick::traverse(f, t, |p| {
let s = w.state(p[0], p[1], p[2]);
let (r, shift) = match placed_shape::<R>(s, p[0], p[1], p[2], Some(ctx))? {
Placed::Cube(_) => (R::voxel(R::FULL_CUBE_VOXEL), None),
Placed::Shape(r, shift, _) => (r, shift),
Placed::Border | Placed::Moving(..) => return None,
};
let mut hit = clip_voxel(r, shift, f, t, p)?;
let inter = crate::pick::PlacedShape::fixed(R::interaction_shape(s));
if let Some(o) = crate::pick::clip_shape(&inter, f, t, p)
&& crate::pick::length_sqr(o.location, f) < crate::pick::length_sqr(hit.location, f)
{
hit.face = o.face;
}
Some(hit)
})
.unwrap_or_else(|| {
BlockHit::miss(
t,
Direction::approximate_nearest(f[0] - t[0], f[1] - t[1], f[2] - t[2]),
)
});
let wb = w.w.world_border();
let max = 29_999_984.0;
let c = |v: f64| v.clamp(-max, max);
let (min_x, max_x) = (
c(wb.center_x - wb.size / 2.0),
c(wb.center_x + wb.size / 2.0),
);
let (min_z, max_z) = (
c(wb.center_z - wb.size / 2.0),
c(wb.center_z + wb.size / 2.0),
);
let within = |x: f64, z: f64| x >= min_x && x < max_x && z >= min_z && z < max_z;
if within(f[0], f[2]) && !within(hit.location[0], hit.location[2]) {
let l = hit.location;
let d = [l[0] - f[0], l[1] - f[1], l[2] - f[2]];
let face = Direction::approximate_nearest(d[0], d[1], d[2]);
let eps = f64::from(1.0e-5f32);
let at = [
super::math::clamp(l[0], min_x, max_x - eps),
l[1],
super::math::clamp(l[2], min_z, max_z - eps),
];
let hit = BlockHit {
kind: HitKind::Block,
pos: at.map(floor),
face,
location: at,
inside: false,
};
return (hit, true);
}
(hit, false)
}