use ghast_data::v1_8::BlockState;
use super::blocks::{
BoxList, BoxSink, HistoryShapes, add_collision_boxes, id, is_air, is_tall_support,
is_valid_pos, state_at,
};
use super::fluid::{contains_lava, water_contact};
use crate::math::{Aabb, BoxOps, floor, sorted_box, sqrt_f64_as_f32};
use crate::nonliving::{LegacyRandom, Side};
use crate::world::{BlockView, WorldBorder};
pub(crate) mod nid {
pub const FLOWING_LAVA: u8 = 10;
pub const LAVA: u8 = 11;
pub const FIRE: u8 = 51;
pub const CACTUS: u8 = 81;
pub const BREWING_STAND: u8 = 117;
pub const END_PORTAL_FRAME: u8 = 120;
pub const PISTON_EXTENSION: u8 = 36;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Health {
None,
Five,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Water {
Entity,
Item,
Orb,
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct Body {
pub pos: [f64; 3],
pub old: [f64; 3],
pub tick_start: [f64; 3],
pub vel: [f64; 3],
pub bb: Aabb,
pub width: f32,
pub height: 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 fall: f32,
pub in_web: bool,
pub in_water: bool,
pub first_update: bool,
pub no_clip: bool,
pub removed: bool,
pub age: i32,
pub fire_ticks: i32,
pub health: i32,
pub health_model: Health,
pub water: Water,
pub climbs: bool,
pub distance: f32,
pub next_step: i32,
pub history: HistoryShapes,
pub outside_border: bool,
pub touched: u8,
pub rng: LegacyRandom,
}
impl Body {
pub(crate) fn new(pos: [f64; 3], vel: [f64; 3], width: f32, height: f32) -> Body {
let mut b = Body {
pos,
old: pos,
tick_start: pos,
vel,
bb: Aabb::FULL,
width,
height,
yaw: 0.0,
pitch: 0.0,
yaw_o: 0.0,
pitch_o: 0.0,
on_ground: false,
hcol: false,
vcol: false,
fall: 0.0,
in_web: false,
in_water: false,
first_update: true,
no_clip: false,
removed: false,
age: 0,
fire_ticks: -1,
health: 5,
health_model: Health::None,
water: Water::Entity,
climbs: false,
distance: 0.0,
next_step: 1,
history: HistoryShapes::default(),
outside_border: false,
touched: 0,
rng: LegacyRandom::new(0),
};
b.set_position(pos);
b
}
pub(crate) fn set_position(&mut self, p: [f64; 3]) {
self.pos = p;
let f = f64::from(self.width / 2.0);
self.bb = sorted_box(
p[0] - f,
p[1],
p[2] - f,
p[0] + f,
p[1] + f64::from(self.height),
p[2] + f,
);
}
pub(crate) fn start_tick(&mut self) {
self.old = self.pos;
self.tick_start = self.pos;
self.yaw_o = self.yaw;
self.pitch_o = self.pitch;
self.age += 1;
}
pub(crate) fn end_tick(&mut self) {
for a in 0..3 {
if !self.pos[a].is_finite() {
self.pos[a] = self.tick_start[a];
}
}
if !self.pitch.is_finite() {
self.pitch = self.pitch_o;
}
if !self.yaw.is_finite() {
self.yaw = self.yaw_o;
}
}
pub(crate) fn block_pos(&self) -> [i32; 3] {
[floor(self.pos[0]), floor(self.pos[1]), floor(self.pos[2])]
}
pub(crate) fn damage(&mut self, amount: i32) {
if self.health_model == Health::Five {
self.health -= amount;
if self.health <= 0 {
self.removed = true;
}
}
}
fn splash(&mut self) {
self.rng.next_float();
self.rng.next_float();
let n = 1.0f32 + self.width * 20.0;
let mut i = 0;
while (i as f32) < n {
self.rng.next_float();
self.rng.next_float();
self.rng.next_float();
i += 1;
}
let mut i = 0;
while (i as f32) < n {
self.rng.next_float();
self.rng.next_float();
i += 1;
}
}
}
#[inline]
fn end(v: f64) -> i32 {
floor(v + 1.0)
}
pub(crate) fn update_water_state<W: BlockView<State = BlockState>>(b: &mut Body, w: &W) -> bool {
let probe = match b.water {
Water::Entity => {
b.bb.grow(0.0, -f64::from(0.4f32), 0.0)
.shrink(0.001, 0.001, 0.001)
}
Water::Item | Water::Orb => b.bb,
};
let (touching, flow) = water_contact(w, &probe);
if touching && flow.length() > 0.0 {
let f = flow.normalize();
b.vel[0] += f.x * 0.014;
b.vel[1] += f.y * 0.014;
b.vel[2] += f.z * 0.014;
}
match b.water {
Water::Orb => return touching,
_ if touching => {
if !b.in_water && !b.first_update {
b.splash();
}
if b.water == Water::Entity {
b.fall = 0.0;
b.fire_ticks = 0;
}
b.in_water = true;
}
_ => b.in_water = false,
}
b.in_water
}
pub(crate) fn base_tick<W: BlockView<State = BlockState>>(b: &mut Body, w: &W, side: Side) {
update_water_state(b, w);
if side == Side::Client {
b.fire_ticks = 0;
} else if b.fire_ticks > 0 {
if b.fire_ticks % 20 == 0 {
b.damage(1);
}
b.fire_ticks -= 1;
}
let lava =
b.bb.grow(-f64::from(0.1f32), -f64::from(0.4f32), -f64::from(0.1f32));
if contains_lava(w, &lava) {
b.damage(4);
if b.fire_ticks < 300 {
b.fire_ticks = 300;
}
b.fall *= 0.5;
}
if b.pos[1] < -64.0 {
b.removed = true;
}
b.first_update = false;
}
const STONE: BlockState = ghast_data::v1_8::Block::STONE.default_state();
pub(crate) fn gather<W: BlockView<State = BlockState>>(
b: Option<&mut Body>,
w: &W,
history: HistoryShapes,
query: Aabb,
out: &mut BoxList,
) -> u8 {
out.clear();
let border: WorldBorder = w.world_border();
let (x0, x1) = (floor(query.min_x), end(query.max_x));
let (y0, y1) = (floor(query.min_y), end(query.max_y));
let (z0, z1) = (floor(query.min_z), end(query.max_z));
let (was_outside, inside) = match &b {
Some(b) => {
let m = if b.outside_border { 1.0 } else { -1.0 };
let (wx, nz, ex, sz) = (
border.west() + m,
border.north() + m,
border.east() - m,
border.south() - m,
);
let (x, z) = (b.pos[0], b.pos[2]);
(b.outside_border, x > wx && x < ex && z > nz && z < sz)
}
None => (false, false),
};
let entity = b.is_some();
let mut touched = 0u8;
let mut visited = false;
let mut sink = BoxSink {
query,
out,
history,
};
for x in x0..x1 {
for z in z0..z1 {
if !is_valid_pos(x, 64, z) {
continue;
}
visited = true;
let substitute = entity && inside && !border.contains_block(x, z);
for y in (y0 - 1)..y1 {
let s = if substitute {
STONE
} else {
state_at(w, x, y, z)
};
touched |= touch_bit(s);
add_collision_boxes(w, x, y, z, s, &mut sink);
}
}
}
if let Some(b) = b
&& visited
{
if was_outside && inside {
b.outside_border = false;
} else if !was_outside && !inside {
b.outside_border = true;
}
}
touched
}
pub(crate) fn tracked_position<W: BlockView<State = BlockState>>(b: &mut Body, w: &W, p: [f64; 3]) {
b.set_position(p);
let bb = b.bb;
let query = sorted_box(
bb.min_x + 0.03125,
bb.min_y,
bb.min_z + 0.03125,
bb.max_x - 0.03125,
bb.max_y,
bb.max_z - 0.03125,
);
let mut boxes = BoxList::new();
let history = b.history;
let touched = gather(Some(b), w, history, query, &mut boxes);
b.touched |= touched;
if boxes.is_empty() {
return;
}
let mut d = 0.0;
for c in boxes.as_slice() {
if c.max_y > d {
d = c.max_y;
}
}
let y = p[1] + (d - b.bb.min_y);
b.set_position([p[0], y, p[2]]);
}
fn touch_bit(s: BlockState) -> u8 {
match s.block().id() {
nid::BREWING_STAND => 1,
nid::END_PORTAL_FRAME => 2,
_ => 0,
}
}
pub(crate) fn collision_bounds<W: BlockView<State = BlockState>>(
w: &W,
history: HistoryShapes,
x: i32,
y: i32,
z: i32,
) -> Option<Aabb> {
let s = state_at(w, x, y, z);
let mut boxes = BoxList::new();
let (fx, fy, fz) = (f64::from(x), f64::from(y), f64::from(z));
let query = Aabb::new(fx - 1.0, fy - 1.0, fz - 1.0, fx + 2.0, fy + 3.0, fz + 2.0);
let mut sink = BoxSink {
query,
out: &mut boxes,
history,
};
add_collision_boxes(w, x, y, z, s, &mut sink);
let list = boxes.as_slice();
let first = *list.first()?;
if s.block().id() == nid::END_PORTAL_FRAME {
return Some(first);
}
if super::blocks::shape_kind(s.block().id()) == super::blocks::ShapeKind::Pane {
return list.last().copied();
}
let mut u = first;
for b in &list[1..] {
u = Aabb::new(
u.min_x.min(b.min_x),
u.min_y.min(b.min_y),
u.min_z.min(b.min_z),
u.max_x.max(b.max_x),
u.max_y.max(b.max_y),
u.max_z.max(b.max_z),
);
}
if is_tall_support(s) && u.max_y > fy + 1.0 {
u.max_y = fy + 1.0;
}
Some(u)
}
fn full_box<W: BlockView<State = BlockState>>(w: &W, history: HistoryShapes, p: [i32; 3]) -> bool {
collision_bounds(w, history, p[0], p[1], p[2]).is_some_and(|b| {
((b.max_x - b.min_x) + (b.max_y - b.min_y) + (b.max_z - b.min_z)) / 3.0 >= 1.0
})
}
pub(crate) fn push_out_of_blocks<W: BlockView<State = BlockState>>(
b: &mut Body,
w: &W,
x: f64,
y: f64,
z: f64,
) -> bool {
let p = [floor(x), floor(y), floor(z)];
let (d, e, f) = (
x - f64::from(p[0]),
y - f64::from(p[1]),
z - f64::from(p[2]),
);
let mut boxes = BoxList::new();
b.touched |= gather(None, w, b.history, b.bb, &mut boxes);
let h = b.history;
if boxes.is_empty() && !full_box(w, h, p) {
return false;
}
let open = |o: [i32; 3]| !full_box(w, h, [p[0] + o[0], p[1] + o[1], p[2] + o[2]]);
let mut i = 3;
let mut g = 9999.0;
if open([-1, 0, 0]) && d < g {
g = d;
i = 0;
}
if open([1, 0, 0]) && 1.0 - d < g {
g = 1.0 - d;
i = 1;
}
if open([0, 1, 0]) && 1.0 - e < g {
g = 1.0 - e;
i = 3;
}
if open([0, 0, -1]) && f < g {
g = f;
i = 4;
}
if open([0, 0, 1]) && 1.0 - f < g {
i = 5;
}
let h = b.rng.next_float() * 0.2 + 0.1;
match i {
0 => b.vel[0] = f64::from(-h),
1 => b.vel[0] = f64::from(h),
4 => b.vel[2] = f64::from(-h),
5 => b.vel[2] = f64::from(h),
_ => b.vel[1] = f64::from(h),
}
true
}
pub(crate) fn entity_move<W: BlockView<State = BlockState>>(b: &mut Body, w: &W, m: [f64; 3]) {
let [mut dx, mut dy, mut dz] = m;
if b.no_clip {
b.bb = b.bb.offset(dx, dy, dz);
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,
];
return;
}
let start = b.pos;
if b.in_web {
b.in_web = false;
dx *= 0.25;
dy *= f64::from(0.05f32);
dz *= 0.25;
b.vel = [0.0; 3];
}
let (n, o, p) = (dx, dy, dz);
let mut boxes = BoxList::new();
let query = b.bb.stretch(dx, dy, dz);
let history = b.history;
let touched = gather(Some(b), w, history, query, &mut boxes);
b.touched |= touched;
let list = boxes.as_slice();
let mut bb = b.bb;
for c in list {
dy = c.clip_y(&bb, dy);
}
bb = bb.offset(0.0, dy, 0.0);
for c in list {
dx = c.clip_x(&bb, dx);
}
bb = bb.offset(dx, 0.0, 0.0);
for c in list {
dz = c.clip_z(&bb, dz);
}
bb = bb.offset(0.0, 0.0, dz);
b.bb = bb;
b.pos = [
(bb.min_x + bb.max_x) / 2.0,
bb.min_y,
(bb.min_z + bb.max_z) / 2.0,
];
b.hcol = n != dx || p != dz;
b.vcol = o != dy;
b.on_ground = b.vcol && o < 0.0;
let lp = [
floor(b.pos[0]),
floor(b.pos[1] - f64::from(0.2f32)),
floor(b.pos[2]),
];
let mut landed = state_at(w, lp[0], lp[1], lp[2]);
if is_air(landed) {
let below = state_at(w, lp[0], lp[1] - 1, lp[2]);
if is_tall_support(below) {
landed = below;
}
}
if b.on_ground {
b.fall = 0.0;
} else if dy < 0.0 {
b.fall = (f64::from(b.fall) - dy) as f32;
}
if n != dx {
b.vel[0] = 0.0;
}
if p != dz {
b.vel[2] = 0.0;
}
let block = landed.block().id();
if o != dy {
if block == id::SLIME {
if b.vel[1] < 0.0 {
b.vel[1] = -b.vel[1];
}
} else {
b.vel[1] = 0.0;
}
}
if b.climbs {
let (ax, az) = (b.pos[0] - start[0], b.pos[2] - start[2]);
let ay = if block == id::LADDER {
b.pos[1] - start[1]
} else {
0.0
};
if b.on_ground && block == id::SLIME {
let d = b.vel[1].abs();
if d < 0.1 {
let k = 0.4 + d * 0.2;
b.vel[0] *= k;
b.vel[2] *= k;
}
}
b.distance = (f64::from(b.distance)
+ f64::from(sqrt_f64_as_f32(ax * ax + ay * ay + az * az)) * 0.6)
as f32;
if b.distance > b.next_step as f32 && !is_air(landed) {
b.next_step = b.distance as i32 + 1;
if b.in_water {
b.rng.next_float();
b.rng.next_float();
}
}
}
check_block_collision(b, w);
let wet = b.in_water;
if fire_source_in(w, &b.bb.shrink(0.001, 0.001, 0.001)) {
b.damage(1);
if !wet {
b.fire_ticks += 1;
if b.fire_ticks == 0 {
b.fire_ticks = 160;
}
}
} else if b.fire_ticks <= 0 {
b.fire_ticks = -1;
}
if wet && b.fire_ticks > 0 {
b.rng.next_float();
b.rng.next_float();
b.fire_ticks = -1;
}
}
fn fire_source_in<W: BlockView<State = BlockState>>(w: &W, bb: &Aabb) -> bool {
let (x0, x1) = (floor(bb.min_x), end(bb.max_x));
let (y0, y1) = (floor(bb.min_y), end(bb.max_y));
let (z0, z1) = (floor(bb.min_z), end(bb.max_z));
if !(y1 >= 0 && y0 < 256) {
return false;
}
for x in x0..x1 {
for y in y0..y1 {
for z in z0..z1 {
let id = state_at(w, x, y, z).block().id();
if id == nid::FIRE || id == nid::FLOWING_LAVA || id == nid::LAVA {
return true;
}
}
}
}
false
}
pub(crate) fn check_block_collision<W: BlockView<State = BlockState>>(b: &mut Body, 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 !(y1 >= 0 && y0 < 256) {
return;
}
for x in x0..=x1 {
for y in y0..=y1 {
for z in z0..=z1 {
block_touch(b, state_at(w, x, y, z));
}
}
}
}
pub(crate) fn block_touch(b: &mut Body, s: BlockState) {
match s.block().id() {
id::WEB => {
b.in_web = true;
b.fall = 0.0;
}
id::SOUL_SAND => {
b.vel[0] *= 0.4;
b.vel[2] *= 0.4;
}
nid::CACTUS => b.damage(1),
_ => {}
}
}