use ghast_data::v1_16_5::{Block, BlockState, OffsetType};
use ghast_data::Aabb;
use super::{
BlockHit, Direction, PickEntity, PlacedShape, Target, entity_hit, length_sqr, search_box,
};
use crate::math::{cos, sin};
use crate::world::BlockView;
pub const REACH: f64 = 4.5;
pub const CREATIVE_REACH: f64 = 5.0;
#[must_use]
pub fn eye_position(pos: [f64; 3], eye_height: f32) -> [f64; 3] {
[pos[0], pos[1] + f64::from(eye_height), pos[2]]
}
#[must_use]
pub fn rotation_vector(yaw: f32, pitch: f32) -> [f64; 3] {
const DEG: f32 = (std::f64::consts::PI / 180.0) as f32;
let f = pitch * DEG;
let g = -yaw * DEG;
let h = cos(g);
let i = sin(g);
let j = cos(f);
let k = sin(f);
[f64::from(i * j), f64::from(-k), f64::from(h * j)]
}
#[must_use]
pub fn model_offset(offset: OffsetType, x: i32, z: i32) -> [f64; 3] {
if offset == OffsetType::None {
return [0.0; 3];
}
let mut l = i64::from(x.wrapping_mul(3_129_871)) ^ i64::from(z).wrapping_mul(116_129_781);
l = l
.wrapping_mul(l)
.wrapping_mul(42_317_861)
.wrapping_add(l.wrapping_mul(11));
let l = l >> 16;
let h = |bits: i64| (f64::from((bits & 15) as f32 / 15.0) - 0.5) * 0.5;
let y = if offset == OffsetType::Xyz {
(f64::from(((l >> 4) & 15) as f32 / 15.0) - 1.0) * 0.2
} else {
0.0
};
[h(l), y, h(l >> 8)]
}
#[must_use]
pub fn shapes(
state: BlockState,
pos: [i32; 3],
held_block: Option<Block>,
) -> (PlacedShape, PlacedShape) {
let outline = if held_block == Some(state.block()) {
state.outline_shape_holding_own_item()
} else {
state.outline_shape()
};
let shift = state
.outline_offset()
.then(|| model_offset(state.block().vanilla().offset, pos[0], pos[2]));
(
PlacedShape {
shape: outline,
shift,
},
PlacedShape::fixed(state.raycast_shape()),
)
}
pub fn clip<W: BlockView<State = BlockState>>(
world: &W,
from: [f64; 3],
to: [f64; 3],
held_block: Option<Block>,
) -> BlockHit {
super::clip(from, to, |pos| {
if !(0..256).contains(&pos[1]) {
return None;
}
let (o, i) = shapes(world.block(pos[0], pos[1], pos[2]), pos, held_block);
(!o.shape.empty).then_some((o, i))
})
}
pub fn pick<W: BlockView<State = BlockState>>(
world: &W,
eye: [f64; 3],
look: [f64; 3],
reach: f64,
held_block: Option<Block>,
) -> BlockHit {
let to = [
eye[0] + look[0] * reach,
eye[1] + look[1] * reach,
eye[2] + look[2] * reach,
];
clip(world, eye, to, held_block)
}
pub fn pick_target<W: BlockView<State = BlockState>>(
world: &W,
player_bb: &Aabb,
eye: [f64; 3],
look: [f64; 3],
creative: bool,
held_block: Option<Block>,
entities: &[PickEntity],
) -> Target {
let reach = if creative { CREATIVE_REACH } else { REACH };
let block = pick(world, eye, look, reach, held_block);
let (d, limited) = if creative {
(6.0, false)
} else {
(reach, reach > 3.0)
};
let block_sq = length_sqr(block.location, eye);
let delta = [look[0] * d, look[1] * d, look[2] * d];
let to = [eye[0] + delta[0], eye[1] + delta[1], eye[2] + delta[2]];
if let Some(e) = entity_hit(eye, to, &search_box(player_bb, delta), entities, block_sq) {
let g = length_sqr(e.location, eye);
if limited && g > 9.0 {
let face = Direction::approximate_nearest(look[0], look[1], look[2]);
return Target::Block(BlockHit::miss(e.location, face));
}
if g < block_sq {
return Target::Entity(e);
}
}
Target::Block(block)
}