use ghast_data::v1_12::BlockState;
use super::blocks::{
BoxList, BoxSink, ShapeKind, add_collision_boxes, id, is_air, is_tall_support, shape_kind,
state_at,
};
use super::fluid::{contains_lava, water_contact};
use crate::math::{Aabb, BoxOps, floor, sorted_box};
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 END_PORTAL_FRAME: u8 = 120;
pub const CONCRETE_POWDER: u8 = 252;
}
#[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_gravity: 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 outside_border: bool,
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_gravity: false,
no_clip: false,
removed: false,
age: 0,
fire_ticks: -1,
health: 5,
health_model: Health::None,
water: Water::Entity,
outside_border: false,
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])]
}
fn on_fire(&self) -> bool {
self.fire_ticks > 0
}
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;
}
}
}
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 touching_lava<W: BlockView<State = BlockState>>(b: &Body, w: &W) -> bool {
contains_lava(
w,
&b.bb
.grow(-f64::from(0.1f32), -f64::from(0.4f32), -f64::from(0.1f32)),
)
}
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;
}
if touching_lava(b, w) {
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;
}
fn ceil(v: f64) -> i32 {
let i = v as i32;
if v > f64::from(i) { i + 1 } else { i }
}
const STONE: BlockState = ghast_data::v1_12::Block::STONE.default_state();
pub(crate) fn gather<W: BlockView<State = BlockState>>(
b: Option<&mut Body>,
w: &W,
query: Aabb,
out: &mut BoxList,
) {
out.clear();
let border: WorldBorder = w.world_border();
let (x0, x1) = (floor(query.min_x) - 1, ceil(query.max_x) + 1);
let (y0, y1) = (floor(query.min_y) - 1, ceil(query.max_y) + 1);
let (z0, z1) = (floor(query.min_z) - 1, ceil(query.max_z) + 1);
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 visited = false;
let mut sink = BoxSink { query, out };
for x in x0..x1 {
let edge_x = x == x0 || x == x1 - 1;
for z in z0..z1 {
let edge_z = z == z0 || z == z1 - 1;
if edge_x && edge_z {
continue;
}
let substitute = entity && inside && !border.contains_block(x, z);
for y in y0..y1 {
if (edge_x || edge_z) && y == y1 - 1 {
continue;
}
visited = true;
let s = if substitute {
STONE
} else {
state_at(w, x, y, z)
};
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;
}
}
}
pub(crate) fn collides_any<W: BlockView<State = BlockState>>(w: &W, query: &Aabb) -> bool {
let mut boxes = BoxList::new();
gather(None, w, *query, &mut boxes);
!boxes.is_empty()
}
pub(crate) fn collision_bounds<W: BlockView<State = BlockState>>(
w: &W,
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,
};
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);
}
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 matches!(
shape_kind(s.block().id()),
ShapeKind::Fence | ShapeKind::Wall | ShapeKind::Gate
) && u.max_y > fy + 1.0
{
u.max_y = fy + 1.0;
}
Some(u)
}
fn full_box<W: BlockView<State = BlockState>>(w: &W, p: [i32; 3]) -> bool {
collision_bounds(w, 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]),
);
if !collides_any(w, &b.bb) {
return false;
}
let mut dir = (1usize, 1.0f32);
let mut g = f64::MAX;
let open = |o: [i32; 3]| !full_box(w, [p[0] + o[0], p[1] + o[1], p[2] + o[2]]);
if open([-1, 0, 0]) && d < g {
g = d;
dir = (0, -1.0);
}
if open([1, 0, 0]) && 1.0 - d < g {
g = 1.0 - d;
dir = (0, 1.0);
}
if open([0, 0, -1]) && f < g {
g = f;
dir = (2, -1.0);
}
if open([0, 0, 1]) && 1.0 - f < g {
g = 1.0 - f;
dir = (2, 1.0);
}
if open([0, 1, 0]) && 1.0 - e < g {
dir = (1, 1.0);
}
let h = b.rng.next_float() * 0.2 + 0.1;
let v = f64::from(dir.1 * h);
for (a, c) in b.vel.iter_mut().enumerate() {
*c = if a == dir.0 { v } else { *c * 0.75 };
}
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;
}
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);
gather(Some(b), w, query, &mut boxes);
let list = boxes.as_slice();
let mut bb = b.bb;
if dy != 0.0 {
for c in list {
dy = c.clip_y(&bb, dy);
}
bb = bb.offset(0.0, dy, 0.0);
}
if dx != 0.0 {
for c in list {
dx = c.clip_x(&bb, dx);
}
if dx != 0.0 {
bb = bb.offset(dx, 0.0, 0.0);
}
}
if dz != 0.0 {
for c in list {
dz = c.clip_z(&bb, dz);
}
if dz != 0.0 {
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 {
match block {
id::SLIME => {
if b.vel[1] < 0.0 {
b.vel[1] = -b.vel[1] * 0.8;
}
}
id::BED => {
if b.vel[1] < 0.0 {
b.vel[1] = -b.vel[1] * f64::from(0.66f32) * 0.8;
}
}
_ => b.vel[1] = 0.0,
}
}
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 {
b.fire_ticks = 160;
}
}
} else if b.fire_ticks <= 0 {
b.fire_ticks = -1;
}
if wet && b.on_fire() {
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), ceil(bb.max_x));
let (y0, y1) = (floor(bb.min_y), ceil(bb.max_y));
let (z0, z1) = (floor(bb.min_z), ceil(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),
_ => {}
}
}