use ghast_data::DigMaterial;
use ghast_protocol::BlockPos;
use crate::common::{DigStatus, Face, Outbound, Vec3};
pub const REACH: f64 = 4.5;
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct BlockQuery {
pub air: bool,
pub water: bool,
pub block: u16,
pub hardness: f32,
pub dig_material: Option<DigMaterial>,
pub harvest_tools: &'static [u16],
pub selection: Option<[f64; 6]>,
pub stairs: Option<[Option<[f64; 6]>; 8]>,
pub activates: bool,
pub(crate) exact: Option<ExactDig>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) enum ExactDig {
#[cfg(feature = "v1_8")]
V1_8,
#[cfg(feature = "v1_12_2")]
V1_12,
#[cfg(feature = "v1_16_5")]
V1_16_5,
#[cfg(feature = "v26_1")]
V26_1(ModernDig),
#[cfg(feature = "v26_2")]
V26_2(ModernDig),
#[cfg(feature = "v26_3")]
V26_3(ModernDig),
}
#[cfg(ghast_modern)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct ModernDig {
pub(crate) state: u16,
pub(crate) mining_efficiency: f64,
pub(crate) block_break_speed: f64,
pub(crate) submerged_mining_speed: f64,
pub(crate) conduit_power: Option<i32>,
}
pub(crate) fn attack_strength_at(ticks: u32, attack_speed: f64) -> f32 {
let period = (1.0 / attack_speed * 20.0) as f32;
((ticks as f32 + 0.5) / period).clamp(0.0, 1.0)
}
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct HeldItem {
pub id: u16,
pub count: u8,
pub efficiency: u8,
pub places_block: bool,
pub usable: bool,
pub food: bool,
pub always_edible: bool,
pub tool_speed: Option<fn(DigMaterial, u16) -> f32>,
#[cfg_attr(not(ghast_modern), allow(dead_code))]
pub(crate) modern_item: Option<u16>,
pub(crate) swing_on_use: bool,
pub(crate) equips: bool,
pub(crate) equip_swings: bool,
pub(crate) creative_keeps_blocks: bool,
pub(crate) use_succeeds: bool,
}
impl BlockQuery {
#[must_use]
pub const fn new(block: u16) -> Self {
BlockQuery {
exact: None,
air: false,
water: false,
block,
hardness: 0.0,
dig_material: None,
harvest_tools: &[],
selection: None,
stairs: None,
activates: false,
}
}
pub const AIR: BlockQuery = BlockQuery::new(0).with_air(true);
#[must_use]
pub const fn with_air(mut self, on: bool) -> Self {
self.air = on;
self
}
#[must_use]
pub const fn with_water(mut self, on: bool) -> Self {
self.water = on;
self
}
#[must_use]
pub const fn with_hardness(mut self, hardness: f32) -> Self {
self.hardness = hardness;
self
}
#[must_use]
pub const fn with_dig_material(mut self, m: Option<DigMaterial>) -> Self {
self.dig_material = m;
self
}
#[must_use]
pub const fn with_harvest_tools(mut self, tools: &'static [u16]) -> Self {
self.harvest_tools = tools;
self
}
#[must_use]
pub const fn with_selection(mut self, b: Option<[f64; 6]>) -> Self {
self.selection = b;
self
}
#[must_use]
pub const fn with_stairs(mut self, passes: Option<[Option<[f64; 6]>; 8]>) -> Self {
self.stairs = passes;
self
}
#[must_use]
pub const fn with_activates(mut self, on: bool) -> Self {
self.activates = on;
self
}
}
impl HeldItem {
#[must_use]
pub const fn new(id: u16) -> Self {
HeldItem {
modern_item: None,
swing_on_use: false,
equips: false,
equip_swings: false,
creative_keeps_blocks: false,
use_succeeds: false,
id,
count: 1,
efficiency: 0,
places_block: false,
usable: false,
food: false,
always_edible: false,
tool_speed: None,
}
}
#[must_use]
pub const fn with_count(mut self, count: u8) -> Self {
self.count = count;
self
}
#[must_use]
pub const fn with_efficiency(mut self, level: u8) -> Self {
self.efficiency = level;
self
}
#[must_use]
pub const fn with_places_block(mut self, on: bool) -> Self {
self.places_block = on;
self
}
#[must_use]
pub const fn with_usable(mut self, on: bool) -> Self {
self.usable = on;
self
}
#[must_use]
pub const fn with_food(mut self, food: bool, always_edible: bool) -> Self {
self.food = food;
self.always_edible = always_edible;
self
}
#[must_use]
pub const fn with_tool_speed(mut self, f: Option<fn(DigMaterial, u16) -> f32>) -> Self {
self.tool_speed = f;
self
}
}
impl DigFactors {
#[must_use]
pub const fn with_on_ground(mut self, on: bool) -> Self {
self.on_ground = on;
self
}
#[must_use]
pub const fn with_in_water(mut self, on: bool) -> Self {
self.in_water = on;
self
}
#[must_use]
pub const fn with_aqua_affinity(mut self, on: bool) -> Self {
self.aqua_affinity = on;
self
}
#[must_use]
pub const fn with_haste(mut self, level: u8) -> Self {
self.haste = level;
self
}
#[must_use]
pub const fn with_fatigue(mut self, level: u8) -> Self {
self.fatigue = level;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct DigFactors {
pub on_ground: bool,
pub in_water: bool,
pub aqua_affinity: bool,
pub haste: u8,
pub fatigue: u8,
}
fn exact_progress(b: &BlockQuery, e: ExactDig, held: Option<&HeldItem>, f: DigFactors) -> f32 {
let item = held.map(|h| h.id);
let efficiency = held.map_or(0, |h| h.efficiency);
let amp = |level: u8| (level > 0).then(|| i32::from(level) - 1);
let _ = (item, efficiency, &, b);
match e {
#[cfg(feature = "v1_8")]
ExactDig::V1_8 => ghast_data::v1_8::dig::destroy_progress(
item,
b.block,
&ghast_data::v1_8::dig::DigConditions {
efficiency,
haste: amp(f.haste),
mining_fatigue: amp(f.fatigue),
eye_in_water: f.in_water,
aqua_affinity: f.aqua_affinity,
on_ground: f.on_ground,
},
),
#[cfg(feature = "v1_12_2")]
ExactDig::V1_12 => ghast_data::v1_12::dig::destroy_progress(
item,
b.block,
&ghast_data::v1_12::dig::DigConditions {
efficiency,
haste: amp(f.haste),
mining_fatigue: amp(f.fatigue),
eye_in_water: f.in_water,
aqua_affinity: f.aqua_affinity,
on_ground: f.on_ground,
},
),
#[cfg(feature = "v1_16_5")]
ExactDig::V1_16_5 => ghast_data::v1_16_5::dig::destroy_progress(
item,
b.block,
&ghast_data::v1_16_5::dig::DigConditions {
efficiency,
haste: amp(f.haste),
conduit_power: None,
mining_fatigue: amp(f.fatigue),
eye_in_water: f.in_water,
aqua_affinity: f.aqua_affinity,
on_ground: f.on_ground,
},
),
#[cfg(feature = "v26_1")]
ExactDig::V26_1(m) => crate::sim::v26_1::dig_progress(&m, held, f),
#[cfg(feature = "v26_2")]
ExactDig::V26_2(m) => crate::sim::v26_2::dig_progress(&m, held, f),
#[cfg(feature = "v26_3")]
ExactDig::V26_3(m) => crate::sim::v26_3::dig_progress(&m, held, f),
}
}
#[must_use]
pub fn relative_hardness(b: &BlockQuery, held: Option<&HeldItem>, f: DigFactors) -> f32 {
if let Some(e) = b.exact {
return exact_progress(b, e, held, f);
}
let h = b.hardness;
if h < 0.0 {
return 0.0;
}
let can_harvest =
b.harvest_tools.is_empty() || held.is_some_and(|i| b.harvest_tools.contains(&i.id));
let mut speed = match (held, b.dig_material) {
(Some(i), Some(m)) => i.tool_speed.map_or(1.0, |t| t(m, i.id)),
_ => 1.0,
};
if speed > 1.0
&& let Some(i) = held
&& i.efficiency > 0
{
let e = f32::from(i.efficiency);
speed += e * e + 1.0;
}
if f.haste > 0 {
speed *= 1.0 + f32::from(f.haste) * 0.2;
}
if f.fatigue > 0 {
speed *= match f.fatigue {
1 => 0.3,
2 => 0.09,
3 => 0.0027,
_ => 8.1e-4,
};
}
if f.in_water && !f.aqua_affinity {
speed /= 5.0;
}
if !f.on_ground {
speed /= 5.0;
}
if can_harvest {
speed / h / 30.0
} else {
speed / h / 100.0
}
}
#[must_use]
pub fn look_vector(yaw: f32, pitch: f32) -> Vec3 {
use ghast_physics::math::{cos, sin};
const RAD: f32 = 0.017_453_292;
let f = cos(-yaw * RAD - std::f32::consts::PI);
let f1 = sin(-yaw * RAD - std::f32::consts::PI);
let f2 = -cos(-pitch * RAD);
let f3 = sin(-pitch * RAD);
Vec3::new(f64::from(f1 * f2), f64::from(f3), f64::from(f * f2))
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct BlockHit {
pub pos: BlockPos,
pub face: Face,
pub hit: Vec3,
pub distance: f64,
}
const INSIDE_HIT: f64 = 0.0051;
fn floor(v: f64) -> i32 {
let i = v as i32;
if v < f64::from(i) { i - 1 } else { i }
}
fn intermediate(a: Vec3, b: Vec3, axis: usize, v: f64) -> Option<Vec3> {
let d = [b.x - a.x, b.y - a.y, b.z - a.z];
let o = [a.x, a.y, a.z];
if d[axis] * d[axis] < 1.0e-7 {
return None;
}
let t = (v - o[axis]) / d[axis];
(0.0..=1.0)
.contains(&t)
.then(|| Vec3::new(a.x + d[0] * t, a.y + d[1] * t, a.z + d[2] * t))
}
fn sq_dist(a: Vec3, b: Vec3) -> f64 {
let (x, y, z) = (b.x - a.x, b.y - a.y, b.z - a.z);
x * x + y * y + z * z
}
fn block_ray(pos: BlockPos, b: &[f64; 6], start: Vec3, end: Vec3) -> Option<(Vec3, Face)> {
let off = Vec3::new(f64::from(pos.x), f64::from(pos.y), f64::from(pos.z));
let s = Vec3::new(start.x - off.x, start.y - off.y, start.z - off.z);
let e = Vec3::new(end.x - off.x, end.y - off.y, end.z - off.z);
let in_yz = |v: &Vec3| v.y >= b[1] && v.y <= b[4] && v.z >= b[2] && v.z <= b[5];
let in_xz = |v: &Vec3| v.x >= b[0] && v.x <= b[3] && v.z >= b[2] && v.z <= b[5];
let in_xy = |v: &Vec3| v.x >= b[0] && v.x <= b[3] && v.y >= b[1] && v.y <= b[4];
let candidates = [
(intermediate(s, e, 0, b[0]).filter(in_yz), Face::West),
(intermediate(s, e, 0, b[3]).filter(in_yz), Face::East),
(intermediate(s, e, 1, b[1]).filter(in_xz), Face::Down),
(intermediate(s, e, 1, b[4]).filter(in_xz), Face::Up),
(intermediate(s, e, 2, b[2]).filter(in_xy), Face::North),
(intermediate(s, e, 2, b[5]).filter(in_xy), Face::South),
];
let mut best: Option<(Vec3, Face)> = None;
for (v, face) in candidates {
if let Some(v) = v
&& best.is_none_or(|(bv, _)| sq_dist(s, v) < sq_dist(s, bv))
{
best = Some((v, face));
}
}
best.map(|(v, f)| (Vec3::new(v.x + off.x, v.y + off.y, v.z + off.z), f))
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub enum RayResult {
Block(BlockHit),
Miss {
pos: BlockPos,
face: Face,
hit: Vec3,
},
None,
}
pub fn ray_trace(
blocks: &dyn Fn(i32, i32, i32) -> Option<BlockQuery>,
start: Vec3,
end: Vec3,
return_last: bool,
) -> RayResult {
let nan = |v: Vec3| v.x.is_nan() || v.y.is_nan() || v.z.is_nan();
if nan(start) || nan(end) {
return RayResult::None;
}
let dist = |hit: Vec3, eyes: Vec3| sq_dist(eyes, hit).sqrt();
let eyes = start;
let (ex, ey, ez) = (floor(end.x), floor(end.y), floor(end.z));
let (mut x, mut y, mut z) = (floor(start.x), floor(start.y), floor(start.z));
let test = |p: BlockPos, s: Vec3| -> Option<BlockHit> {
let b = blocks(p.x, p.y, p.z)?;
let found = if let Some(passes) = &b.stairs {
let mut best: Option<(Vec3, Face)> = None;
let mut best_d = 0.0;
for bounds in passes.iter().flatten() {
if let Some((hit, face)) = block_ray(p, bounds, s, end) {
let d = sq_dist(hit, end);
if d > best_d {
best = Some((hit, face));
best_d = d;
}
}
}
best
} else {
block_ray(p, &b.selection?, s, end)
};
found.map(|(hit, face)| BlockHit {
pos: p,
face,
hit,
distance: dist(hit, eyes),
})
};
let first = BlockPos::new(x, y, z);
if let Some(h) = test(first, start) {
return RayResult::Block(h);
}
let mut last = RayResult::None;
let mut cur = start;
let mut steps = 200;
while steps >= 0 {
steps -= 1;
if nan(cur) {
return RayResult::None;
}
if x == ex && y == ey && z == ez {
break;
}
let plane = |e: i32, c: i32| {
if e > c {
Some(f64::from(c) + 1.0)
} else if e < c {
Some(f64::from(c))
} else {
None
}
};
let (px, py, pz) = (plane(ex, x), plane(ey, y), plane(ez, z));
let (dx, dy, dz) = (end.x - cur.x, end.y - cur.y, end.z - cur.z);
let frac = |p: Option<f64>, o: f64, d: f64| {
let t = p.map_or(999.0, |p| (p - o) / d);
if t == 0.0 { -1.0e-4 } else { t }
};
let (tx, ty, tz) = (
frac(px, cur.x, dx),
frac(py, cur.y, dy),
frac(pz, cur.z, dz),
);
let face;
if tx < ty && tx < tz {
face = if ex > x { Face::West } else { Face::East };
cur = Vec3::new(px.unwrap_or(999.0), cur.y + dy * tx, cur.z + dz * tx);
} else if ty < tz {
face = if ey > y { Face::Down } else { Face::Up };
cur = Vec3::new(cur.x + dx * ty, py.unwrap_or(999.0), cur.z + dz * ty);
} else {
face = if ez > z { Face::North } else { Face::South };
cur = Vec3::new(cur.x + dx * tz, cur.y + dy * tz, pz.unwrap_or(999.0));
}
x = floor(cur.x) - i32::from(face == Face::East);
y = floor(cur.y) - i32::from(face == Face::Up);
z = floor(cur.z) - i32::from(face == Face::South);
let p = BlockPos::new(x, y, z);
match blocks(x, y, z).filter(|b| b.selection.is_some() || b.stairs.is_some()) {
Some(_) => {
if let Some(h) = test(p, cur) {
return RayResult::Block(h);
}
}
None => {
last = RayResult::Miss {
pos: p,
face,
hit: cur,
}
}
}
}
if return_last { last } else { RayResult::None }
}
#[must_use]
pub fn pick(
blocks: &dyn Fn(i32, i32, i32) -> Option<BlockQuery>,
eyes: Vec3,
look: Vec3,
reach: f64,
) -> Option<BlockHit> {
let end = Vec3::new(
eyes.x + look.x * reach,
eyes.y + look.y * reach,
eyes.z + look.z * reach,
);
match ray_trace(blocks, eyes, end, true) {
RayResult::Block(h) => Some(h),
_ => None,
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct EntityHit {
pub id: i32,
pub hit: Vec3,
pub entity_pos: Vec3,
pub distance: f64,
pub(crate) interact: [(Interact, InteractEffect); 2],
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(not(any(feature = "v1_8", feature = "v1_12_2")), allow(dead_code))]
pub(crate) enum InteractEffect {
#[default]
None,
Take,
Damage,
IntoMilk,
#[cfg_attr(
not(any(feature = "v26_1", feature = "v26_2", feature = "v26_3")),
allow(dead_code)
)]
Into(u16),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub(crate) enum Interact {
#[default]
NotHandled,
AtHandled,
Handled,
#[cfg_attr(not(feature = "v1_16_5"), allow(dead_code))]
HandledSwing,
}
fn box_intercept(b: &[f64; 6], a: Vec3, e: Vec3) -> Option<Vec3> {
let in_yz = |v: &Vec3| v.y >= b[1] && v.y <= b[4] && v.z >= b[2] && v.z <= b[5];
let in_xz = |v: &Vec3| v.x >= b[0] && v.x <= b[3] && v.z >= b[2] && v.z <= b[5];
let in_xy = |v: &Vec3| v.x >= b[0] && v.x <= b[3] && v.y >= b[1] && v.y <= b[4];
let cands = [
intermediate(a, e, 0, b[0]).filter(in_yz),
intermediate(a, e, 0, b[3]).filter(in_yz),
intermediate(a, e, 1, b[1]).filter(in_xz),
intermediate(a, e, 1, b[4]).filter(in_xz),
intermediate(a, e, 2, b[2]).filter(in_xy),
intermediate(a, e, 2, b[5]).filter(in_xy),
];
let mut best: Option<Vec3> = None;
for v in cands.into_iter().flatten() {
if best.is_none_or(|bv| sq_dist(a, v) < sq_dist(a, bv)) {
best = Some(v);
}
}
best
}
#[must_use]
pub(crate) fn ray_hits_cube(eyes: Vec3, look: Vec3, pos: BlockPos, reach: f64) -> bool {
let (x, y, z) = (f64::from(pos.x), f64::from(pos.y), f64::from(pos.z));
let b = [x, y, z, x + 1.0, y + 1.0, z + 1.0];
let inside = eyes.x >= b[0]
&& eyes.x <= b[3]
&& eyes.y >= b[1]
&& eyes.y <= b[4]
&& eyes.z >= b[2]
&& eyes.z <= b[5];
let end = Vec3::new(
eyes.x + look.x * reach,
eyes.y + look.y * reach,
eyes.z + look.z * reach,
);
inside || box_intercept(&b, eyes, end).is_some()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Buttons {
pub left: bool,
pub right: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Button {
Left,
Right,
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub enum PressOutcome {
Entity(EntityHit),
Block(BlockHit),
Item,
Nothing,
}
pub(crate) struct HandsIo<'a> {
pub blocks: &'a dyn Fn(i32, i32, i32) -> Option<BlockQuery>,
pub target: Option<BlockHit>,
pub held: Option<HeldItem>,
pub held_stack: Option<ghast_inventory::Stack>,
pub inv_version: ghast_inventory::Version,
pub food: i32,
pub off_held: Option<HeldItem>,
pub off_stack: Option<ghast_inventory::Stack>,
pub factors: DigFactors,
pub sneaking: bool,
pub entity: Option<EntityHit>,
pub out: &'a mut Vec<Outbound>,
pub broken: &'a mut Vec<BlockPos>,
pub placed: &'a mut bool,
pub spectator: bool,
pub caps: &'static crate::version::Capabilities,
pub creative: bool,
pub creative_dig: bool,
pub place_blocked: &'a dyn Fn(BlockPos, Face, u16) -> bool,
pub rules: &'a dyn RightRules,
}
pub(crate) trait RightRules {
fn place(
&self,
pos: BlockPos,
face: Face,
cursor: [f32; 3],
off_hand: bool,
) -> crate::sim::predict::PlaceRules;
fn air(&self, off_hand: bool) -> crate::sim::predict::AirAnswer;
fn cooling(&self, off_hand: bool) -> bool {
let _ = off_hand;
false
}
}
pub(crate) struct NoRightRules;
impl RightRules for NoRightRules {
fn place(&self, _: BlockPos, _: Face, _: [f32; 3], _: bool) -> crate::sim::predict::PlaceRules {
crate::sim::predict::PlaceRules::default()
}
fn air(&self, _: bool) -> crate::sim::predict::AirAnswer {
crate::sim::predict::AirAnswer::Flags
}
}
impl HandsIo<'_> {
#[inline]
fn attack_swing(&mut self) {
self.out.push(if self.caps.sends_punch {
Outbound::Punch
} else {
Outbound::Swing
});
}
#[inline]
fn use_swing(&mut self, off_hand: bool) {
if !self.caps.sends_punch {
self.out.push(if off_hand {
Outbound::SwingOffHand
} else {
Outbound::Swing
});
}
}
fn solid(&self, pos: BlockPos) -> Option<BlockQuery> {
if self.broken.contains(&pos) {
return None;
}
(self.blocks)(pos.x, pos.y, pos.z).filter(|b| !b.air)
}
}
#[derive(Debug, Clone, Default)]
pub struct Hands {
hitting: bool,
current: Option<BlockPos>,
damage: f32,
hit_stack: Option<ghast_inventory::Stack>,
hit_delay: u32,
destroy_face: Option<Face>,
left_click_counter: u32,
right_click_delay: u32,
using: bool,
use_hand_off: bool,
use_item: u16,
use_ticks: u32,
pub(crate) use_serial: u32,
released: Option<(bool, u16, u32)>,
left_was: bool,
right_was: bool,
pub(crate) attacks: Vec<(i32, u32)>,
pub broken: u64,
pub(crate) dug: bool,
pub(crate) placing: Vec<u32>,
pub ticks_since_swing: u32,
pub(crate) cooldown_reset_pending: bool,
pub(crate) equipped: Option<bool>,
pub(crate) use_events: Vec<crate::common::UseEvent>,
pub(crate) air_used: Vec<(bool, u32, crate::sim::predict::AirUse)>,
pub(crate) used_ok: u8,
pub(crate) cooled: u8,
}
const MAX_USE_EVENTS: usize = 64;
impl Hands {
pub fn reset(&mut self) {
self.stop_using();
let (broken, dug) = (self.broken, self.dug);
let placing = std::mem::take(&mut self.placing);
let use_events = std::mem::take(&mut self.use_events);
*self = Hands::default();
self.broken = broken;
self.dug = dug;
self.placing = placing;
self.use_events = use_events;
}
#[cold]
#[inline(never)]
fn use_event(&mut self, e: crate::common::UseEvent) {
if self.use_events.len() < MAX_USE_EVENTS {
self.use_events.push(e);
}
}
#[cold]
#[inline(never)]
fn end_use(&mut self, make: fn(i32, bool, u32) -> crate::common::UseEvent) {
if self.using {
self.using = false;
let e = make(i32::from(self.use_item), self.use_hand_off, self.use_ticks);
self.use_event(e);
}
}
#[must_use]
pub fn mining(&self) -> Option<(BlockPos, f32)> {
match (self.hitting, self.current) {
(true, Some(p)) => Some((p, self.damage.min(1.0))),
_ => None,
}
}
#[must_use]
pub fn using_item(&self) -> bool {
self.using
}
#[must_use]
pub fn use_item(&self) -> Option<(bool, u16)> {
self.using.then_some((self.use_hand_off, self.use_item))
}
#[cfg(test)]
#[allow(dead_code)] pub(crate) fn test_start_using(&mut self, id: u16) {
self.start_using(false, id);
}
pub(crate) fn server_start_using(&mut self, off_hand: bool, id: u16) {
if !self.using {
self.start_using(off_hand, id);
}
}
fn start_using(&mut self, off_hand: bool, id: u16) {
self.use_ticks = 0;
self.using = true;
self.use_serial = self.use_serial.wrapping_add(1);
self.use_hand_off = off_hand;
self.use_item = id;
self.use_event(crate::common::UseEvent::Started {
item: i32::from(id),
off_hand,
});
}
#[must_use]
pub(crate) fn released_use(&self) -> Option<(bool, u16, u32)> {
self.released
}
pub(crate) fn count_use_tick(&mut self) {
if self.using {
self.use_ticks = self.use_ticks.saturating_add(1);
}
}
#[inline]
pub fn stop_using(&mut self) {
if self.using {
self.end_use(
|item, off_hand, ticks| crate::common::UseEvent::Interrupted {
item,
off_hand,
ticks,
},
);
}
}
pub(crate) fn finish_using(&mut self) {
if self.using {
self.end_use(|item, off_hand, ticks| crate::common::UseEvent::Finished {
item,
off_hand,
ticks,
});
}
}
#[must_use]
pub fn attack_strength(&self, attack_speed: f64) -> f32 {
attack_strength_at(self.ticks_since_swing, attack_speed)
}
pub(crate) fn attack(&mut self, id: i32, io: &mut HandsIo<'_>) {
io.out.push(Outbound::UseEntity {
target: id,
action: crate::common::EntityUse::Attack,
off_hand: false,
sneaking: io.sneaking,
});
if !io.spectator {
self.attacks.push((id, self.ticks_since_swing));
}
self.ticks_since_swing = 0;
if io.caps.creative_attack_dig_delay && io.creative {
self.hit_delay = 5;
}
}
fn interact(&mut self, e: EntityHit, off_hand: bool, io: &mut HandsIo<'_>) {
self.interact_at(e, off_hand, io);
io.out.push(Outbound::UseEntity {
target: e.id,
action: crate::common::EntityUse::Interact,
off_hand,
sneaking: io.sneaking,
});
let fx = e.interact[usize::from(off_hand)].1;
if fx != InteractEffect::None {
let stack = if off_hand {
&mut io.off_stack
} else {
&mut io.held_stack
};
*stack = match (fx, stack.take()) {
(InteractEffect::Damage, Some(st)) => {
let d = st.damage().saturating_add(1);
Some(st.with_damage(d))
}
(InteractEffect::Take, Some(st)) => {
(st.count() > 1).then(|| st.with_count(st.count() - 1))
}
(_, st) => st,
};
}
}
fn interact_at(&mut self, e: EntityHit, off_hand: bool, io: &mut HandsIo<'_>) {
let rel = Vec3::new(
e.hit.x - e.entity_pos.x,
e.hit.y - e.entity_pos.y,
e.hit.z - e.entity_pos.z,
);
io.out.push(Outbound::UseEntity {
target: e.id,
action: crate::common::EntityUse::InteractAt(rel),
off_hand,
sneaking: io.sneaking,
});
}
pub(crate) fn tick(&mut self, left: bool, right: bool, clicks: u8, io: &mut HandsIo<'_>) {
if self.cooldown_reset_pending {
self.cooldown_reset_pending = false;
self.ticks_since_swing = 0;
} else {
self.ticks_since_swing = self.ticks_since_swing.saturating_add(1);
}
if self.left_click_counter > 0 {
self.left_click_counter -= 1;
}
if self.right_click_delay > 0 {
self.right_click_delay -= 1;
}
let t = io.target;
self.released = None;
if self.using {
if !right {
self.released = Some((self.use_hand_off, self.use_item, self.use_ticks));
io.out.push(Outbound::Dig {
status: DigStatus::ReleaseUseItem,
pos: BlockPos::new(0, 0, 0),
face: Face::Down,
});
self.end_use(|item, off_hand, ticks| crate::common::UseEvent::Released {
item,
off_hand,
ticks,
});
}
} else {
if left && !self.left_was {
self.click_mouse(t, io);
}
for _ in 0..clicks {
self.click_mouse(t, io);
}
if right && !self.right_was {
self.right_click_mouse(t, io);
}
}
if right && self.right_click_delay == 0 && !self.using {
self.right_click_mouse(t, io);
}
self.send_click_block_to_controller(left, t, io);
self.left_was = left;
self.right_was = right;
if self.using {
let hand = if self.use_hand_off {
io.off_held
} else {
io.held
};
if hand.is_none_or(|h| h.id != self.use_item) {
self.stop_using();
}
}
}
fn click_mouse(&mut self, t: Option<BlockHit>, io: &mut HandsIo<'_>) {
if self.left_click_counter > 0 {
return;
}
if let Some(e) = io.entity {
if io.caps.combat_1_9 {
self.attack(e.id, io);
io.attack_swing();
} else {
io.attack_swing();
self.attack(e.id, io);
}
return;
}
if io.caps.combat_1_9 {
match t {
Some(hit) if io.solid(hit.pos).is_some() => {
self.click_block(hit.pos, hit.face, io);
}
_ => {
if !io.creative {
self.left_click_counter = 10;
}
self.ticks_since_swing = 0;
}
}
io.attack_swing();
return;
}
io.attack_swing();
match t {
Some(hit) if io.solid(hit.pos).is_some() => {
self.click_block(hit.pos, hit.face, io);
}
_ if !io.creative => self.left_click_counter = 10,
_ => {}
}
}
fn send_click_block_to_controller(
&mut self,
left: bool,
t: Option<BlockHit>,
io: &mut HandsIo<'_>,
) {
if !left {
self.left_click_counter = 0;
}
if self.left_click_counter > 0 || self.using {
return;
}
match t.filter(|_| io.entity.is_none()) {
Some(hit) if left => {
let solid = io.solid(hit.pos).is_some();
if solid && self.damage_block(hit.pos, hit.face, io) {
io.attack_swing();
}
}
_ => self.reset_block_removing(io),
}
}
fn hitting_position(&self, pos: BlockPos, io: &HandsIo<'_>) -> bool {
self.hitting && self.breaking_at(pos, io)
}
fn breaking_at(&self, pos: BlockPos, io: &HandsIo<'_>) -> bool {
if self.current != Some(pos) {
return false;
}
match (&self.hit_stack, &io.held_stack) {
(None, None) => true,
(Some(was), Some(now)) => {
was.id() == now.id()
&& was.data() == now.data()
&& (now.is_damageable(io.inv_version) || was.damage() == now.damage())
}
_ => false,
}
}
#[inline]
pub(crate) fn dig_follows_held(&self) -> bool {
self.current.is_some()
}
pub(crate) fn held_changed_in_place(
&mut self,
before: Option<&ghast_inventory::Stack>,
after: Option<&ghast_inventory::Stack>,
) {
if self.hit_stack.as_ref() == before {
self.hit_stack = after.cloned();
}
}
pub(crate) fn click_block(&mut self, pos: BlockPos, face: Face, io: &mut HandsIo<'_>) -> bool {
if io.creative_dig {
return self.creative_dig(pos, face, io);
}
if io.spectator {
return false;
}
if self.hitting && self.hitting_position(pos, io) {
return true;
}
if self.hitting
&& let Some(cur) = self.current
{
io.out.push(Outbound::Dig {
status: DigStatus::Abort,
pos: cur,
face,
});
}
io.out.push(Outbound::Dig {
status: DigStatus::Start,
pos,
face,
});
self.dug = true;
if let Some(b) = io.solid(pos)
&& relative_hardness(&b, io.held.as_ref(), io.factors) >= 1.0
{
self.destroyed(pos, io);
return true;
}
self.hitting = true;
self.current = Some(pos);
self.hit_stack.clone_from(&io.held_stack);
self.damage = 0.0;
self.destroy_face = Some(face);
true
}
#[cold]
#[inline(never)]
fn creative_dig(&mut self, pos: BlockPos, face: Face, io: &mut HandsIo<'_>) -> bool {
io.out.push(Outbound::Dig {
status: DigStatus::Start,
pos,
face,
});
self.dug = true;
if !io.held.is_some_and(|h| h.creative_keeps_blocks) && io.solid(pos).is_some() {
self.destroyed(pos, io);
}
self.hit_delay = 5;
true
}
fn damage_block(&mut self, pos: BlockPos, face: Face, io: &mut HandsIo<'_>) -> bool {
if self.hit_delay > 0 {
self.hit_delay -= 1;
return true;
}
if io.creative_dig {
return self.creative_dig(pos, face, io);
}
if !self.breaking_at(pos, io) {
return self.click_block(pos, face, io);
}
let Some(b) = io.solid(pos) else {
self.hitting = false;
return false;
};
self.damage += relative_hardness(&b, io.held.as_ref(), io.factors);
if self.damage >= 1.0 {
self.hitting = false;
io.out.push(Outbound::Dig {
status: DigStatus::Finish,
pos,
face,
});
self.dug = true;
self.destroyed(pos, io);
self.destroy_face = None;
self.damage = 0.0;
self.hit_delay = 5;
} else if io.caps.dig_change_direction && self.destroy_face != Some(face) {
self.destroy_face = Some(face);
io.out.push(Outbound::Dig {
status: DigStatus::ChangeDirection,
pos,
face,
});
}
true
}
fn destroyed(&mut self, pos: BlockPos, io: &mut HandsIo<'_>) {
io.broken.push(pos);
if matches!(
io.inv_version,
ghast_inventory::Version::V1_8 | ghast_inventory::Version::V1_12
) && let Some(c) = self.current
{
self.current = Some(BlockPos::new(c.x, -1, c.z));
}
self.broken += 1;
}
pub(crate) fn stop_destroy_block(&mut self, out: &mut Vec<Outbound>) {
if !self.hitting {
return;
}
if let Some(cur) = self.current {
out.push(Outbound::Dig {
status: DigStatus::Abort,
pos: cur,
face: Face::Down,
});
self.dug = true;
}
self.hitting = false;
self.damage = 0.0;
self.ticks_since_swing = 0;
}
pub(crate) fn reset_block_removing(&mut self, io: &mut HandsIo<'_>) {
if !self.hitting {
return;
}
if let Some(cur) = self.current {
io.out.push(Outbound::Dig {
status: DigStatus::Abort,
pos: cur,
face: Face::Down,
});
self.dug = true;
}
self.hitting = false;
self.damage = 0.0;
}
fn right_click_mouse(&mut self, t: Option<BlockHit>, io: &mut HandsIo<'_>) {
if self.hitting {
return;
}
self.right_click_delay = 4;
if io.caps.combat_1_9 {
self.right_click_modern(t, io);
return;
}
if let Some(e) = io.entity {
if e.interact[0].0 == Interact::AtHandled {
self.interact_at(e, false, io);
return;
}
self.interact(e, false, io);
if e.interact[0].0 == Interact::Handled {
return;
}
if let Some(h) = io.held.filter(|_| !io.spectator) {
io.out.push(Outbound::UseItem {
held: io.held_stack.clone(),
off_hand: false,
});
self.used_legacy(h, io);
}
return;
}
let mut fall_through = true;
if let Some(hit) = t
&& let Some(b) = io.solid(hit.pos)
{
if self.right_click_block(hit, &b, io) {
fall_through = false;
io.use_swing(false);
}
if io.held.is_none() {
return;
}
}
if fall_through
&& !io.spectator
&& let Some(h) = io.held
{
io.out.push(Outbound::UseItem {
held: io.held_stack.clone(),
off_hand: false,
});
self.used_legacy(h, io);
}
}
fn used_legacy(&mut self, h: HeldItem, io: &mut HandsIo<'_>) {
if let crate::sim::predict::AirAnswer::Known(a) = io.rules.air(false) {
let k = io.out.len() as u32 - 1;
if a.starts {
self.start_using(false, h.id);
}
if a.result == crate::sim::predict::AirResult::Success {
io.use_swing(false);
}
if !matches!(a.hand, crate::sim::predict::HandChange::Keep)
|| a.blocks.as_ref().is_some_and(|b| !b.is_empty())
{
self.air_used.push((false, k, a));
}
return;
}
if h.usable && (!h.food || (io.food >= 0 && (h.always_edible || io.food < 20))) {
self.start_using(false, h.id);
} else if h.equips {
self.equipped = Some(false);
}
}
fn right_click_modern(&mut self, t: Option<BlockHit>, io: &mut HandsIo<'_>) {
let any_item = io.held.is_some() || io.off_held.is_some();
let mut main_decided = true;
for off_hand in [false, true] {
let (held, stack) = if off_hand {
(io.off_held, io.off_stack.clone())
} else {
(io.held, io.held_stack.clone())
};
if let Some(e) = io.entity {
match e.interact[usize::from(off_hand)].0 {
Interact::AtHandled => {
self.interact_at(e, off_hand, io);
return;
}
Interact::Handled => {
self.interact(e, off_hand, io);
return;
}
Interact::HandledSwing => {
self.interact(e, off_hand, io);
io.use_swing(off_hand);
return;
}
Interact::NotHandled => self.interact(e, off_hand, io),
}
} else if let Some(hit) = t
&& let Some(b) = io.solid(hit.pos)
&& let rules = if off_hand && !main_decided {
crate::sim::predict::PlaceRules::default()
} else {
io.rules.place(
hit.pos,
hit.face,
[
(hit.hit.x - f64::from(hit.pos.x)) as f32,
(hit.hit.y - f64::from(hit.pos.y)) as f32,
(hit.hit.z - f64::from(hit.pos.z)) as f32,
],
off_hand,
)
}
&& {
if !off_hand {
main_decided = rules.block_packet.is_some()
&& rules.used.is_some()
&& !rules.air_use_unknown;
}
true
}
&& rules.block_packet.unwrap_or_else(|| {
!(!io.spectator
&& !(b.activates && !(io.sneaking && any_item))
&& held.is_some_and(|h| (io.place_blocked)(hit.pos, hit.face, h.id)))
})
{
let cursor = Vec3::new(
hit.hit.x - f64::from(hit.pos.x),
hit.hit.y - f64::from(hit.pos.y),
hit.hit.z - f64::from(hit.pos.z),
);
io.out.push(if hit.distance <= INSIDE_HIT {
Outbound::PlaceInside {
pos: hit.pos,
face: hit.face,
cursor,
held: stack.clone(),
off_hand,
}
} else {
Outbound::Place {
pos: hit.pos,
face: hit.face,
cursor,
held: stack.clone(),
off_hand,
}
});
if io.spectator {
if io.caps.spectator_block_click_swings {
io.use_swing(off_hand);
}
return;
}
if let Some(used) = rules.used {
if used {
if rules.starts_use
&& let Some(h) = held
{
self.start_using(off_hand, h.id);
return;
}
if held.is_some() {
if !off_hand {
*io.placed = true;
}
self.placing.push(io.out.len() as u32 - 1);
}
if !rules.no_swing {
io.use_swing(off_hand);
}
return;
}
if rules.ends {
return;
}
} else if b.activates && !(io.sneaking && any_item) {
io.use_swing(off_hand);
return;
}
if rules.used.is_none()
&& let Some(h) = held
&& h.places_block
&& h.count > 0
&& !io.rules.cooling(off_hand)
{
self.used_ok |= 4 << u8::from(off_hand);
if !off_hand {
*io.placed = true;
}
if !off_hand || main_decided {
self.placing.push(io.out.len() as u32 - 1);
}
io.use_swing(off_hand);
return;
}
}
if let Some(h) = held.filter(|_| !io.spectator) {
let k = io.out.len() as u32;
io.out.push(Outbound::UseItem {
held: stack,
off_hand,
});
let answer = if io.rules.cooling(off_hand) {
self.cooled |= 1 << u8::from(off_hand);
crate::sim::predict::AirAnswer::Known(crate::sim::predict::AirUse {
result: crate::sim::predict::AirResult::Pass,
starts: false,
blocks: Some(Vec::new()),
hand: crate::sim::predict::HandChange::Keep,
})
} else {
io.rules.air(off_hand)
};
match answer {
crate::sim::predict::AirAnswer::Known(a) => {
use crate::sim::predict::AirResult;
if a.starts {
self.start_using(off_hand, h.id);
}
let result = a.result;
if result != AirResult::Pass {
self.used_ok |= 1 << u8::from(off_hand);
}
if (!off_hand || main_decided)
&& (!matches!(a.hand, crate::sim::predict::HandChange::Keep)
|| a.blocks.as_ref().is_none_or(|b| !b.is_empty()))
{
self.air_used.push((off_hand, k, a));
}
if result == AirResult::Pass {
continue;
}
if result == AirResult::Success {
io.use_swing(off_hand);
}
return;
}
crate::sim::predict::AirAnswer::Unknown => {
if !off_hand {
main_decided = false;
}
}
crate::sim::predict::AirAnswer::Flags => {}
}
if h.swing_on_use {
self.used_ok |= 1 << u8::from(off_hand);
io.use_swing(off_hand);
return;
}
if h.use_succeeds {
self.used_ok |= 1 << u8::from(off_hand);
return;
}
if h.usable {
if !h.food || (io.food >= 0 && (h.always_edible || io.food < 20)) {
self.used_ok |= 1 << u8::from(off_hand);
self.start_using(off_hand, h.id);
return;
}
} else if h.equips {
self.used_ok |= 1 << u8::from(off_hand);
self.equipped = Some(off_hand);
if h.equip_swings {
io.use_swing(off_hand);
}
return;
}
}
}
}
pub(crate) fn right_click_block(
&mut self,
hit: BlockHit,
b: &BlockQuery,
io: &mut HandsIo<'_>,
) -> bool {
let cursor = Vec3::new(
f64::from((hit.hit.x - f64::from(hit.pos.x)) as f32),
f64::from((hit.hit.y - f64::from(hit.pos.y)) as f32),
f64::from((hit.hit.z - f64::from(hit.pos.z)) as f32),
);
let activated = b.activates && (!io.sneaking || io.held.is_none());
let rules = io.rules.place(
hit.pos,
hit.face,
[cursor.x as f32, cursor.y as f32, cursor.z as f32],
false,
);
let blocked = match rules.block_packet {
Some(send) => !send,
None => {
!io.spectator
&& !activated
&& io
.held
.is_some_and(|h| (io.place_blocked)(hit.pos, hit.face, h.id))
}
};
if blocked {
return false;
}
io.out.push(Outbound::Place {
pos: hit.pos,
face: hit.face,
cursor,
held: io.held_stack.clone(),
off_hand: false,
});
if io.spectator {
return true;
}
if let Some(used) = rules.used {
if used && io.held.is_some() {
*io.placed = true;
self.placing.push(io.out.len() as u32 - 1);
}
return used;
}
if activated {
return true;
}
match io.held {
Some(h) if h.places_block && h.count > 0 => {
*io.placed = true;
self.placing.push(io.out.len() as u32 - 1);
true
}
_ => false,
}
}
}
#[cfg(test)]
mod tests {
fn test_caps(modern: bool, swings: bool) -> &'static crate::version::Capabilities {
Box::leak(Box::new(crate::version::Capabilities {
combat_1_9: modern,
spectator_block_click_swings: swings,
..*crate::version::GameVersion::V1_8_9.caps()
}))
}
use super::*;
fn stone() -> BlockQuery {
BlockQuery {
exact: None,
air: false,
water: false,
block: 1,
hardness: 1.5,
dig_material: Some(DigMaterial::Rock),
harvest_tools: &[270, 274],
selection: Some([0.0, 0.0, 0.0, 1.0, 1.0, 1.0]),
stairs: None,
activates: false,
}
}
#[test]
fn hand_stone_time() {
let f = DigFactors {
on_ground: true,
..DigFactors::default()
};
let per_tick = relative_hardness(&stone(), None, f);
let mut d = 0.0_f32;
let mut n = 0;
while d < 1.0 {
d += per_tick;
n += 1;
}
assert_eq!(n, 151);
}
#[test]
fn look_south_hits_block() {
let look = look_vector(0.0, 0.0);
assert!(look.z > 0.99 && look.x.abs() < 1e-3);
let blocks = |x: i32, y: i32, z: i32| {
Some(if (x, y, z) == (0, 1, 2) {
stone()
} else {
BlockQuery {
air: true,
selection: None,
..stone()
}
})
};
let hit = pick(&blocks, Vec3::new(0.5, 1.62, 0.5), look, REACH).unwrap();
assert_eq!(hit.pos, BlockPos::new(0, 1, 2));
assert_eq!(hit.face, Face::North);
assert!((hit.distance - 1.5).abs() < 1e-3);
}
fn io_blocks(x: i32, y: i32, z: i32) -> Option<BlockQuery> {
Some(if (x, y, z) == (0, 0, 0) {
stone()
} else {
BlockQuery {
air: true,
selection: None,
..stone()
}
})
}
fn kinds(out: &[Outbound]) -> Vec<&'static str> {
out.iter()
.map(|o| match o {
Outbound::Place {
off_hand: false, ..
}
| Outbound::PlaceInside {
off_hand: false, ..
} => "place_main",
Outbound::Place { off_hand: true, .. }
| Outbound::PlaceInside { off_hand: true, .. } => "place_off",
Outbound::UseItem {
off_hand: false, ..
} => "use_main",
Outbound::UseItem { off_hand: true, .. } => "use_off",
Outbound::Swing => "swing",
Outbound::SwingOffHand => "swing_off",
Outbound::Dig { .. } => "dig",
_ => "other",
})
.collect()
}
#[test]
fn air_swing_miss_delay_outside_creative() {
let hit = Some(BlockHit {
pos: BlockPos::new(0, 0, 0),
face: Face::Up,
hit: Vec3::new(0.5, 1.0, 0.5),
distance: 1.6,
});
for modern in [false, true] {
for creative in [false, true] {
let mut h = Hands::default();
let mut first_dig = None;
for t in 0..20u32 {
let (mut out, mut broken, mut placed) = (Vec::new(), Vec::new(), false);
let mut io = HandsIo {
blocks: &io_blocks,
target: if t == 0 { None } else { hit },
held: None,
held_stack: None,
inv_version: ghast_inventory::Version::V1_8,
food: 20,
off_held: None,
off_stack: None,
entity: None,
factors: DigFactors::default(),
sneaking: false,
out: &mut out,
broken: &mut broken,
placed: &mut placed,
spectator: false,
caps: test_caps(modern, true),
creative,
creative_dig: creative,
place_blocked: &|_, _, _| false,
rules: &NoRightRules,
};
h.tick(true, false, 0, &mut io);
if t == 0 {
assert_eq!(kinds(&out), ["swing"], "{modern} {creative}");
}
if first_dig.is_none() && kinds(&out).contains(&"dig") {
first_dig = Some(t);
}
}
let want = if creative { 1 } else { 10 };
assert_eq!(first_dig, Some(want), "modern {modern} creative {creative}");
}
}
}
#[test]
fn modern_right_click_hands() {
let hit = Some(BlockHit {
pos: BlockPos::new(0, 0, 0),
face: Face::Up,
hit: Vec3::new(0.5, 1.0, 0.5),
distance: 1.6,
});
let run = |held: Option<HeldItem>| {
let mut h = Hands::default();
let (mut out, mut broken, mut placed) = (Vec::new(), Vec::new(), false);
let mut io = HandsIo {
blocks: &io_blocks,
target: hit,
held,
held_stack: None,
inv_version: ghast_inventory::Version::V1_8,
food: 20,
off_held: None,
off_stack: None,
entity: None,
factors: DigFactors::default(),
sneaking: false,
out: &mut out,
broken: &mut broken,
placed: &mut placed,
spectator: false,
caps: test_caps(true, true),
creative: false,
creative_dig: false,
place_blocked: &|_, _, _| false,
rules: &NoRightRules,
};
h.tick(false, true, 0, &mut io);
kinds(&out)
};
assert_eq!(run(None), ["place_main", "place_off"]);
let dirt = HeldItem {
id: 3,
count: 10,
places_block: true,
..HeldItem::default()
};
assert_eq!(run(Some(dirt)), ["place_main", "swing"]);
let stick = HeldItem {
id: 280,
count: 1,
..HeldItem::default()
};
assert_eq!(run(Some(stick)), ["place_main", "use_main", "place_off"]);
}
#[test]
fn unplaceable_block_item_sends_no_block_packet() {
let hit = Some(BlockHit {
pos: BlockPos::new(0, 0, 0),
face: Face::Up,
hit: Vec3::new(0.5, 1.0, 0.5),
distance: 1.6,
});
let dirt = HeldItem {
id: 3,
count: 10,
places_block: true,
..HeldItem::default()
};
let run = |modern: bool, blocked: bool| {
let mut h = Hands::default();
let (mut out, mut broken, mut placed) = (Vec::new(), Vec::new(), false);
let check = move |_: BlockPos, _: Face, id: u16| blocked && id == 3;
let mut io = HandsIo {
blocks: &io_blocks,
target: hit,
held: Some(dirt),
held_stack: None,
inv_version: ghast_inventory::Version::V1_8,
food: 20,
off_held: None,
off_stack: None,
entity: None,
factors: DigFactors::default(),
sneaking: false,
out: &mut out,
broken: &mut broken,
placed: &mut placed,
spectator: false,
caps: test_caps(modern, true),
creative: false,
creative_dig: false,
place_blocked: &check,
rules: &NoRightRules,
};
h.tick(false, true, 0, &mut io);
assert_eq!(placed, !blocked);
assert_eq!(h.placing.is_empty(), blocked);
kinds(&out)
};
assert_eq!(run(false, false), ["place_main", "swing"]);
assert_eq!(run(false, true), ["use_main"]);
assert_eq!(run(true, false), ["place_main", "swing"]);
assert_eq!(run(true, true), ["use_main", "place_off"]);
}
#[test]
fn spectator_block_click_ends_with_the_packet() {
let hit = Some(BlockHit {
pos: BlockPos::new(0, 0, 0),
face: Face::Up,
hit: Vec3::new(0.5, 1.0, 0.5),
distance: 1.6,
});
let dirt = HeldItem {
id: 3,
count: 10,
places_block: true,
..HeldItem::default()
};
let run = |modern: bool, swings: bool, held: Option<HeldItem>| {
let mut h = Hands::default();
let (mut out, mut broken, mut placed) = (Vec::new(), Vec::new(), false);
let mut io = HandsIo {
blocks: &io_blocks,
target: hit,
held,
held_stack: None,
inv_version: ghast_inventory::Version::V1_8,
food: 20,
off_held: None,
off_stack: None,
entity: None,
factors: DigFactors::default(),
sneaking: false,
out: &mut out,
broken: &mut broken,
placed: &mut placed,
spectator: true,
caps: test_caps(modern, swings),
creative: false,
creative_dig: false,
place_blocked: &|_, _, _| true,
rules: &NoRightRules,
};
h.tick(false, true, 0, &mut io);
assert!(!placed);
assert!(h.placing.is_empty());
kinds(&out)
};
for held in [None, Some(dirt)] {
assert_eq!(run(false, true, held), ["place_main", "swing"]);
assert_eq!(run(true, true, held), ["place_main", "swing"]);
assert_eq!(run(true, false, held), ["place_main"]);
}
}
#[test]
fn instant_break_press_starts_once() {
fn instant(x: i32, y: i32, z: i32) -> Option<BlockQuery> {
Some(if (x, y, z) == (0, 0, 0) {
BlockQuery {
hardness: 0.0,
..stone()
}
} else {
BlockQuery {
air: true,
selection: None,
..stone()
}
})
}
let hit = Some(BlockHit {
pos: BlockPos::new(0, 0, 0),
face: Face::Up,
hit: Vec3::new(0.5, 1.0, 0.5),
distance: 1.6,
});
for modern in [false, true] {
for pre_broken in [false, true] {
let mut h = Hands::default();
let (mut out, mut placed) = (Vec::new(), false);
let mut broken = if pre_broken {
vec![BlockPos::new(0, 0, 0)]
} else {
Vec::new()
};
let mut io = HandsIo {
blocks: &instant,
target: hit,
held: None,
held_stack: None,
inv_version: ghast_inventory::Version::V1_8,
food: 20,
off_held: None,
off_stack: None,
entity: None,
factors: DigFactors::default(),
sneaking: false,
out: &mut out,
broken: &mut broken,
placed: &mut placed,
spectator: false,
caps: test_caps(modern, true),
creative: false,
creative_dig: false,
place_blocked: &|_, _, _| false,
rules: &NoRightRules,
};
h.tick(true, false, 0, &mut io);
let starts = out
.iter()
.filter(|o| {
matches!(
o,
Outbound::Dig {
status: DigStatus::Start,
..
}
)
})
.count();
let want = usize::from(!pre_broken);
assert_eq!(
starts, want,
"modern {modern} pre_broken {pre_broken}: {out:?}"
);
assert_eq!(h.broken, want as u64);
}
}
}
#[test]
fn modern_left_click_order() {
let hit = Some(BlockHit {
pos: BlockPos::new(0, 0, 0),
face: Face::Up,
hit: Vec3::new(0.5, 1.0, 0.5),
distance: 1.6,
});
for (modern, want) in [(true, ["dig", "swing"]), (false, ["swing", "dig"])] {
let mut h = Hands::default();
let (mut out, mut broken, mut placed) = (Vec::new(), Vec::new(), false);
let mut io = HandsIo {
blocks: &io_blocks,
target: hit,
held: None,
held_stack: None,
inv_version: ghast_inventory::Version::V1_8,
food: 20,
off_held: None,
off_stack: None,
entity: None,
factors: DigFactors::default(),
sneaking: false,
out: &mut out,
broken: &mut broken,
placed: &mut placed,
spectator: false,
caps: test_caps(modern, true),
creative: false,
creative_dig: false,
place_blocked: &|_, _, _| false,
rules: &NoRightRules,
};
h.click_mouse(hit, &mut io);
assert_eq!(kinds(&out)[..2], want);
}
}
#[test]
fn dig_sequence() {
let blocks = |x: i32, y: i32, z: i32| {
Some(if (x, y, z) == (0, 0, 0) {
BlockQuery {
hardness: 0.6,
..stone()
}
} else {
BlockQuery {
air: true,
selection: None,
..stone()
}
})
};
let target = Some(BlockHit {
pos: BlockPos::new(0, 0, 0),
face: Face::Up,
hit: Vec3::new(0.5, 1.0, 0.5),
distance: 1.6,
});
let mut h = Hands::default();
let mut out = Vec::new();
let mut broken = Vec::new();
let mut placed = false;
let mut ticks = 0;
while broken.is_empty() && ticks < 200 {
let mut io = HandsIo {
blocks: &blocks,
target,
held: None,
held_stack: None,
inv_version: ghast_inventory::Version::V1_8,
food: 20,
off_held: None,
off_stack: None,
entity: None,
factors: DigFactors {
on_ground: true,
..DigFactors::default()
},
sneaking: false,
out: &mut out,
broken: &mut broken,
placed: &mut placed,
spectator: false,
caps: test_caps(false, true),
creative: false,
creative_dig: false,
place_blocked: &|_, _, _| false,
rules: &NoRightRules,
};
h.tick(true, false, 0, &mut io);
ticks += 1;
}
assert_eq!(broken, [BlockPos::new(0, 0, 0)]);
let digs: Vec<_> = out
.iter()
.filter_map(|o| match o {
Outbound::Dig { status, .. } => Some(*status),
_ => None,
})
.collect();
assert_eq!(digs, [DigStatus::Start, DigStatus::Finish]);
assert_eq!(ticks, 61);
assert_eq!(
out.iter().filter(|o| matches!(o, Outbound::Swing)).count(),
62
);
}
}