#![allow(clippy::neg_cmp_op_on_partial_ord)]
use ghast_data::FluidKind;
use ghast_data::PhysicsFlags;
use ghast_data::v1_16_5::{Block, BlockState};
use super::blocks::{
ShapeCtx, collision_boxes, flags, fluid, is_bed, slipperiness, state_at, velocity_multiplier,
};
use super::collide::{BorderShape, Collider, bx, offset};
use super::fluid::{V3, ceil, height, region_loaded, update_in_fluid_as};
use crate::math::{self, Aabb, floor};
use crate::world::BlockView;
use crate::{Packet, Packets};
pub const BOAT_WIDTH: f32 = 1.375;
pub const BOAT_HEIGHT: f32 = 0.5625;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum BoatLocation {
InWater,
UnderWater,
UnderFlowingWater,
OnLand,
InAir,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum BoatSeat {
Alone,
Front,
Back,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Boat {
pub pos: [f64; 3],
pub bb: Aabb,
pub vel: [f64; 3],
pub yaw: f32,
pub pitch: f32,
pub on_ground: bool,
pub horizontal_collision: bool,
pub vertical_collision: bool,
pub fall_distance: f32,
touching_water: bool,
first_update: bool,
movement_multiplier: [f64; 3],
location: Option<BoatLocation>,
last_location: Option<BoatLocation>,
ticks_underwater: f32,
yaw_velocity: f32,
water_level: f64,
land_friction: f32,
fall_velocity: f64,
input: [bool; 4],
paddle: [bool; 2],
seat: BoatSeat,
driver: bool,
tracked: [f64; 3],
target: [f64; 3],
target_yaw: f64,
target_pitch: f64,
steps: i32,
}
fn boat_box(x: f64, y: f64, z: f64) -> Aabb {
let r = f64::from(BOAT_WIDTH / 2.0);
bx(x - r, y, z - r, x + r, y + f64::from(BOAT_HEIGHT), z + r)
}
impl Boat {
#[must_use]
pub fn new(pos: [f64; 3], yaw: f32, pitch: f32) -> Boat {
Boat {
pos,
bb: boat_box(pos[0], pos[1], pos[2]),
vel: [0.0; 3],
yaw,
pitch,
on_ground: false,
horizontal_collision: false,
vertical_collision: false,
fall_distance: 0.0,
touching_water: false,
first_update: true,
movement_multiplier: [0.0; 3],
location: None,
last_location: None,
ticks_underwater: 0.0,
yaw_velocity: 0.0,
water_level: 0.0,
land_friction: 0.0,
fall_velocity: 0.0,
input: [false; 4],
paddle: [false; 2],
seat: BoatSeat::Alone,
driver: true,
tracked: pos,
target: pos,
target_yaw: 0.0,
target_pitch: 0.0,
steps: 0,
}
}
pub fn set_seat(&mut self, index: usize, count: usize) {
self.driver = index == 0;
self.seat = match (count > 1, index) {
(false, _) => BoatSeat::Alone,
(true, 0) => BoatSeat::Front,
(true, _) => BoatSeat::Back,
};
}
pub fn leave(&mut self) {
self.driver = false;
}
pub fn tick_unridden<W: BlockView<State = BlockState>>(&mut self, world: &W, ultrawarm: bool) {
self.driver = false;
passive_tick(self, world, ultrawarm);
}
pub fn pushed_by_player<W: BlockView<State = BlockState>>(
&mut self,
p: &super::PlayerState,
filed: [f64; 3],
world: &W,
) {
if let Some([dx, dz]) = boat_push_from_player(self.pos, &self.bb, p, filed, world) {
self.vel[0] += dx;
self.vel[1] += 0.0;
self.vel[2] += dz;
}
}
#[must_use]
pub fn seat(&self) -> BoatSeat {
self.seat
}
#[must_use]
pub fn controlled(&self) -> bool {
self.driver
}
pub fn set_interpolation(
&mut self,
target: [f64; 3],
yaw: f32,
pitch: f32,
steps: i32,
tracked: [f64; 3],
) {
self.target = target;
self.target_yaw = f64::from(yaw);
self.target_pitch = f64::from(pitch);
self.steps = steps;
self.tracked = tracked;
}
pub fn set_turn(&mut self, yaw_velocity: f32, paddle: [bool; 2]) {
self.yaw_velocity = yaw_velocity;
self.paddle = paddle;
}
#[must_use]
pub fn tracked(&self) -> [f64; 3] {
self.tracked
}
#[must_use]
pub fn interpolation_steps(&self) -> i32 {
self.steps
}
pub fn server_teleport(&mut self, pos: [f64; 3], yaw: i8, pitch: i8, on_ground: bool) {
self.tracked = pos;
if !self.driver {
let yaw = f32::from(yaw) * 360.0 / 256.0;
let pitch = f32::from(pitch) * 360.0 / 256.0;
self.retarget(pos, yaw, pitch);
self.on_ground = on_ground;
}
}
pub fn server_update(
&mut self,
delta: Option<[i16; 3]>,
rotation: Option<(i8, i8)>,
on_ground: bool,
) {
if self.driver {
return;
}
let angles =
|(y, p): (i8, i8)| (f32::from(y) * 360.0 / 256.0, f32::from(p) * 360.0 / 256.0);
match delta {
Some(d) => {
for k in 0..3 {
if d[k] != 0 {
let e = (self.tracked[k] * 4096.0).floor() as i64;
self.tracked[k] = (e + i64::from(d[k])) as f64 / 4096.0;
}
}
let (yaw, pitch) = rotation.map_or((self.yaw, self.pitch), angles);
self.retarget(self.tracked, yaw, pitch);
}
None => {
if let Some(r) = rotation {
let (yaw, pitch) = angles(r);
self.retarget(self.pos, yaw, pitch);
}
}
}
self.on_ground = on_ground;
}
fn retarget(&mut self, target: [f64; 3], yaw: f32, pitch: f32) {
self.target = target;
self.target_yaw = f64::from(yaw);
self.target_pitch = f64::from(pitch);
self.steps = 10;
}
fn interpolate(&mut self) {
if self.driver {
self.steps = 0;
self.tracked = self.pos;
}
if self.steps > 0 {
let n = f64::from(self.steps);
let x = self.pos[0] + (self.target[0] - self.pos[0]) / n;
let y = self.pos[1] + (self.target[1] - self.pos[1]) / n;
let z = self.pos[2] + (self.target[2] - self.pos[2]) / n;
let g = wrap_degrees_f64(self.target_yaw - f64::from(self.yaw));
let yaw = (f64::from(self.yaw) + g / n) as f32;
let pitch =
(f64::from(self.pitch) + (self.target_pitch - f64::from(self.pitch)) / n) as f32;
self.steps -= 1;
self.set_position(x, y, z);
self.yaw = yaw % 360.0;
self.pitch = pitch % 360.0;
}
}
#[must_use]
pub fn location(&self) -> Option<BoatLocation> {
self.location
}
#[must_use]
pub fn yaw_velocity(&self) -> f32 {
self.yaw_velocity
}
#[must_use]
pub fn input(&self) -> [bool; 4] {
self.input
}
#[must_use]
pub fn paddles(&self) -> [bool; 2] {
self.paddle
}
pub fn set_position(&mut self, x: f64, y: f64, z: f64) {
self.pos = [x, y, z];
self.bb = boat_box(x, y, z);
}
pub fn server_move(&mut self, pos: [f64; 3], yaw: f32, pitch: f32) -> Packet {
self.yaw = yaw % 360.0;
self.pitch = pitch.clamp(-90.0, 90.0) % 360.0;
self.set_position(
pos[0].clamp(-3.0e7, 3.0e7),
pos[1],
pos[2].clamp(-3.0e7, 3.0e7),
);
self.vehicle_move()
}
#[must_use]
pub fn vehicle_move(&self) -> Packet {
Packet::MoveVehicle {
pos: self.pos,
yaw: self.yaw,
pitch: self.pitch,
on_ground: self.on_ground,
}
}
pub fn set_input(&mut self, left: bool, right: bool, forward: bool, back: bool) {
self.input = [left, right, forward, back];
}
#[must_use]
pub fn clamp_passenger_yaw(&self, yaw: f32) -> f32 {
let f = crate::rotation::wrap_degrees(yaw - self.yaw);
let g = f.clamp(-105.0, 105.0);
yaw + (g - f)
}
#[must_use]
pub fn passenger_pos(&self) -> [f64; 3] {
let g = (-0.1f64 + -0.35f64) as f32;
let f: f32 = match self.seat {
BoatSeat::Alone => return [self.pos[0], self.pos[1] + f64::from(g), self.pos[2]],
BoatSeat::Front => 0.2,
BoatSeat::Back => -0.6,
};
let a =
-self.yaw * (std::f64::consts::PI / 180.0) as f32 - std::f64::consts::FRAC_PI_2 as f32;
let (c, s) = (math::cos(a), math::sin(a));
let f = f64::from(f);
let dx = f * f64::from(c) + 0.0 * f64::from(s);
let dz = 0.0 * f64::from(c) - f * f64::from(s);
[
self.pos[0] + dx,
self.pos[1] + f64::from(g),
self.pos[2] + dz,
]
}
}
#[cold]
#[inline(never)]
pub fn boat_push_from_player<W: BlockView<State = BlockState>>(
pos: [f64; 3],
bb: &Aabb,
p: &super::PlayerState,
filed: [f64; 3],
world: &W,
) -> Option<[f64; 2]> {
let (h, v) = (f64::from(0.2f32), f64::from(-0.01f32));
let area = bx(
bb.min_x - h,
bb.min_y - v,
bb.min_z - h,
bb.max_x + h,
bb.max_y + v,
bb.max_z + h,
);
let o = &p.bb;
let touches = o.min_x < area.max_x
&& o.max_x > area.min_x
&& o.min_y < area.max_y
&& o.max_y > area.min_y
&& o.min_z < area.max_z
&& o.max_z > area.min_z;
if !touches
|| !super::query_reaches(&area, filed)
|| p.spectator
|| super::is_climbing(p, world)
|| !(p.bb.min_y <= bb.min_y)
{
return None;
}
let (mut d, mut e) = (p.pos[0] - pos[0], p.pos[2] - pos[2]);
let mut f = d.abs().max(e.abs());
if !(f >= f64::from(0.01f32)) {
return None;
}
f = f64::from(f.sqrt() as f32);
d /= f;
e /= f;
let mut g = 1.0 / f;
if g > 1.0 {
g = 1.0;
}
d *= g;
e *= g;
d *= f64::from(0.05f32);
e *= f64::from(0.05f32);
Some([-d, -e])
}
#[allow(clippy::too_many_arguments)]
pub fn tick_boat<W: BlockView<State = BlockState>>(
p: &mut super::PlayerState,
x: &mut super::PlayerExt,
boat: &mut Boat,
around: &super::Surroundings<'_>,
boat_entities: &[Aabb],
input: &crate::TickInput,
world: &W,
out: &mut Packets,
) -> bool {
let mut ti = *input;
ti.yaw = boat.clamp_passenger_yaw(input.yaw);
if boat.driver {
boat_tick(boat, world, boat_entities, x.ultrawarm, out);
} else {
passive_tick(boat, world, x.ultrawarm);
}
if p.input.sneaking {
boat.driver = false;
p.age += 1;
p.yaw = ti.yaw;
p.pitch = ti.pitch;
return false;
}
let ride = super::player::RideCtx {
boat: true,
bb: boat.bb,
submerged: matches!(
boat.location,
Some(BoatLocation::UnderWater | BoatLocation::UnderFlowingWater)
),
jumping_mount: false,
};
super::player::tick_passenger(p, x, around, ride, &ti, world, out);
if boat.driver {
out.push(boat.vehicle_move());
}
let [sx, sy, sz] = boat.passenger_pos();
p.set_position(sx, sy, sz);
p.yaw += boat.yaw_velocity;
p.yaw = boat.clamp_passenger_yaw(p.yaw);
p.fall_distance = 0.0;
let i = p.input;
boat.set_input(
i.pressing_left,
i.pressing_right,
i.pressing_forward,
i.pressing_back,
);
true
}
pub(super) fn boat_tick<W: BlockView<State = BlockState>>(
b: &mut Boat,
w: &W,
entities: &[Aabb],
ultrawarm: bool,
out: &mut Packets,
) {
b.last_location = b.location;
b.location = Some(check_location(b, w));
if matches!(
b.location,
Some(BoatLocation::UnderWater | BoatLocation::UnderFlowingWater)
) {
b.ticks_underwater += 1.0;
} else {
b.ticks_underwater = 0.0;
}
base_tick(b, w, ultrawarm);
b.interpolate();
update_velocity(b, w);
update_paddles(b);
out.push(Packet::PaddleBoat {
left: b.paddle[0],
right: b.paddle[1],
});
let v = V3::new(b.vel[0], b.vel[1], b.vel[2]);
boat_move(b, w, entities, v);
block_contacts(b, w);
}
#[cold]
#[inline(never)]
fn passive_tick<W: BlockView<State = BlockState>>(b: &mut Boat, w: &W, ultrawarm: bool) {
b.last_location = b.location;
b.location = Some(check_location(b, w));
if matches!(
b.location,
Some(BoatLocation::UnderWater | BoatLocation::UnderFlowingWater)
) {
b.ticks_underwater += 1.0;
} else {
b.ticks_underwater = 0.0;
}
base_tick(b, w, ultrawarm);
b.interpolate();
b.vel = [0.0; 3];
block_contacts(b, w);
}
fn wrap_degrees_f64(d: f64) -> f64 {
let mut e = d % 360.0;
if e >= 180.0 {
e -= 360.0;
}
if e < -180.0 {
e += 360.0;
}
e
}
fn fluid_water<W: BlockView<State = BlockState>>(
w: &W,
x: i32,
y: i32,
z: i32,
) -> Option<super::blocks::FluidAt> {
fluid(state_at(w, x, y, z)).filter(|f| f.kind == FluidKind::Water)
}
fn check_location<W: BlockView<State = BlockState>>(b: &mut Boat, w: &W) -> BoatLocation {
if let Some(l) = under_water_location(b, w) {
b.water_level = b.bb.max_y;
return l;
}
if boat_in_water(b, w) {
return BoatLocation::InWater;
}
let f = nearby_slipperiness(b, w);
if f > 0.0 {
b.land_friction = f;
BoatLocation::OnLand
} else {
BoatLocation::InAir
}
}
fn under_water_location<W: BlockView<State = BlockState>>(b: &Boat, w: &W) -> Option<BoatLocation> {
let bb = b.bb;
let d = bb.max_y + 0.001;
let (x0, x1) = (floor(bb.min_x), ceil(bb.max_x));
let (y0, y1) = (floor(bb.max_y), ceil(d));
let (z0, z1) = (floor(bb.min_z), ceil(bb.max_z));
let mut under = false;
for x in x0..x1 {
for y in y0..y1 {
for z in z0..z1 {
if let Some(f) = fluid_water(w, x, y, z)
&& d < f64::from(y as f32 + height(w, x, y, z, f))
{
if !(f.amount == 8 && !f.falling) {
return Some(BoatLocation::UnderFlowingWater);
}
under = true;
}
}
}
}
under.then_some(BoatLocation::UnderWater)
}
fn boat_in_water<W: BlockView<State = BlockState>>(b: &mut Boat, w: &W) -> bool {
let bb = b.bb;
let (x0, x1) = (floor(bb.min_x), ceil(bb.max_x));
let (y0, y1) = (floor(bb.min_y), ceil(bb.min_y + 0.001));
let (z0, z1) = (floor(bb.min_z), ceil(bb.max_z));
let mut found = false;
b.water_level = f64::from_bits(1);
for x in x0..x1 {
for y in y0..y1 {
for z in z0..z1 {
if let Some(f) = fluid_water(w, x, y, z) {
let top = y as f32 + height(w, x, y, z, f);
b.water_level = math::java_max(f64::from(top), b.water_level);
found |= bb.min_y < f64::from(top);
}
}
}
}
found
}
fn nearby_slipperiness<W: BlockView<State = BlockState>>(b: &Boat, w: &W) -> f32 {
let bb = b.bb;
let q = Aabb::new(
bb.min_x,
bb.min_y - 0.001,
bb.min_z,
bb.max_x,
bb.min_y,
bb.max_z,
);
let i = floor(q.min_x) - 1;
let j = ceil(q.max_x) + 1;
let k = floor(q.min_y) - 1;
let l = ceil(q.max_y) + 1;
let m = floor(q.min_z) - 1;
let n = ceil(q.max_z) + 1;
let mut f = 0.0f32;
let mut o = 0i32;
for p in i..j {
for qz in m..n {
let r = i32::from(p == i || p == j - 1) + i32::from(qz == m || qz == n - 1);
if r == 2 {
continue;
}
for s in k..l {
if r > 0 && (s == k || s == l - 1) {
continue;
}
let st = state_at(w, p, s, qz);
if st.block() == Block::LILY_PAD {
continue;
}
let hit = shape_boxes(st, p, s, qz, None)
.iter()
.any(|bb| super::blocks::boxes_match(bb, &q));
if hit {
f += slipperiness(st);
o += 1;
}
}
}
}
f / o as f32
}
fn shape_boxes(st: BlockState, x: i32, y: i32, z: i32, ctx: Option<&ShapeCtx>) -> Vec<Aabb> {
if st.block() == Block::BAMBOO {
return vec![super::blocks::bamboo_box(x, z).at(x, y, z)];
}
collision_boxes(st, y, ctx)
.iter()
.map(|b| b.at(x, y, z))
.collect()
}
fn water_height_below<W: BlockView<State = BlockState>>(b: &Boat, w: &W) -> f32 {
let bb = b.bb;
let (i, j) = (floor(bb.min_x), ceil(bb.max_x));
let k = floor(bb.max_y);
let l = ceil(bb.max_y - b.fall_velocity);
let (m, n) = (floor(bb.min_z), ceil(bb.max_z));
'rows: for o in k..l {
let mut f = 0.0f32;
for p in i..j {
for q in m..n {
if let Some(fl) = fluid_water(w, p, o, q) {
f = f.max(height(w, p, o, q, fl));
}
if f >= 1.0 {
continue 'rows;
}
}
}
if f < 1.0 {
return o as f32 + f;
}
}
(l + 1) as f32
}
fn base_tick<W: BlockView<State = BlockState>>(b: &mut Boat, w: &W, ultrawarm: bool) {
let mut v = V3::new(b.vel[0], b.vel[1], b.vel[2]);
let water = update_in_fluid_as::<W, false>(w, &b.bb, FluidKind::Water, 0.014, true, &mut v);
if water.touching {
b.fall_distance = 0.0;
}
b.touching_water = water.touching;
let lava_speed = if ultrawarm {
0.007
} else {
0.002_333_333_333_333_333_5
};
let lava = update_in_fluid_as::<W, false>(w, &b.bb, FluidKind::Lava, lava_speed, true, &mut v);
b.vel = [v.x, v.y, v.z];
if !b.first_update && lava.height > 0.0 {
b.fall_distance *= 0.5;
}
b.first_update = false;
}
fn update_velocity<W: BlockView<State = BlockState>>(b: &mut Boat, w: &W) {
let mut e = f64::from(-0.04f32);
let mut f = 0.0;
let mut decay = 0.05f32;
let loc = b.location;
if b.last_location == Some(BoatLocation::InAir)
&& loc != Some(BoatLocation::InAir)
&& loc != Some(BoatLocation::OnLand)
{
b.water_level = b.pos[1] + f64::from(BOAT_HEIGHT);
let y = f64::from(water_height_below(b, w)) - f64::from(BOAT_HEIGHT) + 0.101;
b.set_position(b.pos[0], y, b.pos[2]);
b.vel = [b.vel[0] * 1.0, b.vel[1] * 0.0, b.vel[2] * 1.0];
b.fall_velocity = 0.0;
b.location = Some(BoatLocation::InWater);
return;
}
match loc {
Some(BoatLocation::InWater) => {
f = (b.water_level - b.pos[1]) / f64::from(BOAT_HEIGHT);
decay = 0.9;
}
Some(BoatLocation::UnderFlowingWater) => {
e = -7.0e-4;
decay = 0.9;
}
Some(BoatLocation::UnderWater) => {
f = f64::from(0.01f32);
decay = 0.45;
}
Some(BoatLocation::InAir) => decay = 0.9,
Some(BoatLocation::OnLand) => {
decay = b.land_friction;
b.land_friction /= 2.0;
}
None => {}
}
let d = f64::from(decay);
b.vel = [b.vel[0] * d, b.vel[1] + e, b.vel[2] * d];
b.yaw_velocity *= decay;
if f > 0.0 {
b.vel[1] = (b.vel[1] + f * 0.061_538_460_162_969_73) * 0.75;
}
}
fn update_paddles(b: &mut Boat) {
let [left, right, forward, back] = b.input;
let mut f = 0.0f32;
if left {
b.yaw_velocity -= 1.0;
}
if right {
b.yaw_velocity += 1.0;
}
if right != left && !forward && !back {
f += 0.005;
}
b.yaw += b.yaw_velocity;
if forward {
f += 0.04;
}
if back {
f -= 0.005;
}
let r = (std::f64::consts::PI / 180.0) as f32;
b.vel[0] += f64::from(math::sin(-b.yaw * r) * f);
b.vel[1] += 0.0;
b.vel[2] += f64::from(math::cos(b.yaw * r) * f);
b.paddle = [right && !left || forward, left && !right || forward];
}
fn boat_move<W: BlockView<State = BlockState>>(
b: &mut Boat,
w: &W,
entities: &[Aabb],
movement: V3,
) {
let mut m = movement;
let mm = b.movement_multiplier;
if mm[0] * mm[0] + mm[1] * mm[1] + mm[2] * mm[2] > 1.0e-7 {
m = V3::new(m.x * mm[0], m.y * mm[1], m.z * mm[2]);
b.movement_multiplier = [0.0; 3];
b.vel = [0.0; 3];
}
let border = BorderShape::new(&w.world_border());
let col = Collider {
w,
border: &border,
ctx: ShapeCtx {
min_y: b.pos[1],
descending: false,
},
entities,
};
let v = col.adjust(m, &b.bb, b.on_ground, 0.0);
if v.length_sq() > 1.0e-7 {
b.bb = offset(&b.bb, v.x, v.y, v.z);
b.pos = [
(b.bb.min_x + b.bb.max_x) / 2.0,
b.bb.min_y,
(b.bb.min_z + b.bb.max_z) / 2.0,
];
}
let approx = |a: f64, c: f64| (c - a).abs() < f64::from(1.0e-5f32);
b.horizontal_collision = !approx(m.x, v.x) || !approx(m.z, v.z);
b.vertical_collision = m.y != v.y;
b.on_ground = b.vertical_collision && m.y < 0.0;
let (lx, ly, lz) = (
floor(b.pos[0]),
floor(b.pos[1] - f64::from(0.2f32)),
floor(b.pos[2]),
);
let mut landed = state_at(w, lx, ly, lz);
if landed.is_air() {
let below = state_at(w, lx, ly - 1, lz);
let f = flags(below);
if f.contains(PhysicsFlags::FENCE)
|| f.contains(PhysicsFlags::WALL)
|| f.contains(PhysicsFlags::FENCE_GATE)
{
landed = below;
}
}
boat_fall(b, w, v.y);
let old = b.vel;
if m.x != v.x {
b.vel = [0.0, old[1], old[2]];
}
if m.z != v.z {
b.vel = [old[0], old[1], 0.0];
}
let block = landed.block();
if m.y != v.y {
if block == Block::SLIME_BLOCK {
if b.vel[1] < 0.0 {
b.vel[1] = -b.vel[1] * 0.8;
}
} else if is_bed(landed) {
if b.vel[1] < 0.0 {
b.vel[1] = -b.vel[1] * f64::from(0.66f32) * 0.8;
}
} else {
b.vel[1] *= 0.0;
}
}
if b.on_ground && block == Block::SLIME_BLOCK {
let d = b.vel[1].abs();
if d < 0.1 {
let e = 0.4 + d * 0.2;
b.vel[0] *= e;
b.vel[2] *= e;
}
}
block_contacts(b, w);
let k = f64::from(boat_velocity_multiplier(b, w));
b.vel[0] *= k;
b.vel[2] *= k;
}
fn boat_fall<W: BlockView<State = BlockState>>(b: &mut Boat, w: &W, dy: f64) {
b.fall_velocity = b.vel[1];
if b.on_ground {
if b.fall_distance > 3.0 && b.location != Some(BoatLocation::OnLand) {
b.fall_distance = 0.0;
return;
}
b.fall_distance = 0.0;
} else {
let (x, y, z) = (floor(b.pos[0]), floor(b.pos[1]) - 1, floor(b.pos[2]));
if fluid_water(w, x, y, z).is_none() && dy < 0.0 {
b.fall_distance = (f64::from(b.fall_distance) - dy) as f32;
}
}
}
fn boat_velocity_multiplier<W: BlockView<State = BlockState>>(b: &Boat, w: &W) -> f32 {
let here = state_at(w, floor(b.pos[0]), floor(b.pos[1]), floor(b.pos[2]));
let f = velocity_multiplier(here);
if here.block() == Block::WATER || here.block() == Block::BUBBLE_COLUMN || f != 1.0 {
f
} else {
let (x, y, z) = (
floor(b.pos[0]),
floor(b.bb.min_y - 0.500_000_1),
floor(b.pos[2]),
);
velocity_multiplier(state_at(w, x, y, z))
}
}
fn block_contacts<W: BlockView<State = BlockState>>(b: &mut Boat, w: &W) {
let bb = b.bb;
let (x0, y0, z0) = (
floor(bb.min_x + 0.001),
floor(bb.min_y + 0.001),
floor(bb.min_z + 0.001),
);
let (x1, y1, z1) = (
floor(bb.max_x - 0.001),
floor(bb.max_y - 0.001),
floor(bb.max_z - 0.001),
);
if !region_loaded(w, x0, y0, z0, x1, y1, z1) {
return;
}
for x in x0..=x1 {
for y in y0..=y1 {
for z in z0..=z1 {
let s = state_at(w, x, y, z);
let block = s.block();
if block == Block::COBWEB {
b.fall_distance = 0.0;
b.movement_multiplier = [0.25, f64::from(0.05f32), 0.25];
} else if block == Block::HONEY_BLOCK {
let sliding = !b.on_ground
&& !(b.pos[1] > f64::from(y) + 0.9375 - 1.0e-7)
&& !(b.vel[1] >= -0.08)
&& {
let d = (f64::from(x) + 0.5 - b.pos[0]).abs();
let e = (f64::from(z) + 0.5 - b.pos[2]).abs();
let f = 0.4375 + f64::from(BOAT_WIDTH / 2.0);
d + 1.0e-7 > f || e + 1.0e-7 > f
};
if sliding {
if b.vel[1] < -0.13 {
let d = -0.05 / b.vel[1];
b.vel = [b.vel[0] * d, -0.05, b.vel[2] * d];
} else {
b.vel[1] = -0.05;
}
b.fall_distance = 0.0;
}
}
}
}
}
}
#[must_use]
pub fn boat_item_fits<W: BlockView<State = BlockState>>(
w: &W,
hit: [f64; 3],
entities: &[Aabb],
) -> bool {
let bb = boat_box(hit[0], hit[1], hit[2]);
let query = super::collide::expand(&bb, -0.1);
let border = BorderShape::new(&w.world_border());
Collider {
w,
border: &border,
ctx: ShapeCtx {
min_y: hit[1],
descending: false,
},
entities,
}
.is_space_empty(&bb, &query)
}