1use crate::generated::v1_8 as g;
11
12pub use crate::common::{BreakTime, break_time};
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub struct DigConditions {
17 pub efficiency: u8,
19 pub haste: Option<i32>,
21 pub mining_fatigue: Option<i32>,
23 pub eye_in_water: bool,
25 pub aqua_affinity: bool,
27 pub on_ground: bool,
29}
30
31impl Default for DigConditions {
32 fn default() -> Self {
33 DigConditions {
34 efficiency: 0,
35 haste: None,
36 mining_fatigue: None,
37 eye_in_water: false,
38 aqua_affinity: false,
39 on_ground: true,
40 }
41 }
42}
43
44fn item_entry(item: u16) -> Option<crate::common::ItemDig> {
45 let i = g::dig::ITEM_DIG.binary_search_by_key(&item, |e| e.0).ok()?;
46 Some(crate::common::item_dig(
47 &g::dig::ITEM_DIG[i],
48 &g::dig::ITEM_DIG_SPEEDS,
49 &g::dig::ITEM_DIG_BLOCKS,
50 ))
51}
52
53#[must_use]
55pub fn speed_multiplier(item: u16, block: u16) -> f32 {
56 item_entry(item)
57 .and_then(|e| {
58 e.1.binary_search_by_key(&block, |x| x.0)
59 .ok()
60 .map(|i| e.1[i].1)
61 })
62 .unwrap_or(1.0)
63}
64
65#[must_use]
67pub fn is_effective(item: u16, block: u16) -> bool {
68 item_entry(item).is_some_and(|e| e.2.binary_search(&block).is_ok())
69}
70
71#[must_use]
73pub fn strength(block: u16) -> f32 {
74 g::dig::BLOCK_STRENGTH
75 .get(usize::from(block))
76 .copied()
77 .unwrap_or(f32::NAN)
78}
79
80#[must_use]
83pub fn no_tool_needed(block: u16) -> bool {
84 let i = usize::from(block);
85 g::dig::BLOCK_TOOL_FLAG
86 .get(i / 64)
87 .is_some_and(|w| w & (1 << (i % 64)) != 0)
88}
89
90#[must_use]
92pub fn mining_speed(held: Option<u16>, block: u16, c: &DigConditions) -> f32 {
93 let mut f = held.map_or(1.0, |i| speed_multiplier(i, block));
94 if f > 1.0 && c.efficiency > 0 && held.is_some() {
95 let i = i32::from(c.efficiency);
96 f += (i * i + 1) as f32;
97 }
98 if let Some(amp) = c.haste {
99 f *= 1.0 + amp.wrapping_add(1) as f32 * 0.2;
100 }
101 if let Some(amp) = c.mining_fatigue {
102 f *= match amp {
103 0 => 0.3,
104 1 => 0.09,
105 2 => 0.0027,
106 _ => 8.1e-4,
107 };
108 }
109 if c.eye_in_water && !c.aqua_affinity {
110 f /= 5.0;
111 }
112 if !c.on_ground {
113 f /= 5.0;
114 }
115 f
116}
117
118#[must_use]
120pub fn effective_tool(held: Option<u16>, block: u16) -> bool {
121 no_tool_needed(block) || held.is_some_and(|i| is_effective(i, block))
122}
123
124#[must_use]
126pub fn destroy_progress(held: Option<u16>, block: u16, c: &DigConditions) -> f32 {
127 let f = strength(block);
128 if f < 0.0 {
129 return 0.0;
130 }
131 let speed = mining_speed(held, block, c);
132 if effective_tool(held, block) {
133 speed / f / 30.0
134 } else {
135 speed / f / 100.0
136 }
137}