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<&'static crate::common::ItemDig> {
45 g::dig::ITEM_DIG
46 .binary_search_by_key(&item, |e| e.0)
47 .ok()
48 .map(|i| &g::dig::ITEM_DIG[i])
49}
50
51#[must_use]
53pub fn speed_multiplier(item: u16, block: u16) -> f32 {
54 item_entry(item)
55 .and_then(|e| {
56 e.1.binary_search_by_key(&block, |x| x.0)
57 .ok()
58 .map(|i| e.1[i].1)
59 })
60 .unwrap_or(1.0)
61}
62
63#[must_use]
65pub fn is_effective(item: u16, block: u16) -> bool {
66 item_entry(item).is_some_and(|e| e.2.binary_search(&block).is_ok())
67}
68
69#[must_use]
71pub fn strength(block: u16) -> f32 {
72 g::dig::BLOCK_STRENGTH
73 .get(usize::from(block))
74 .copied()
75 .unwrap_or(f32::NAN)
76}
77
78#[must_use]
81pub fn no_tool_needed(block: u16) -> bool {
82 let i = usize::from(block);
83 g::dig::BLOCK_TOOL_FLAG
84 .get(i / 64)
85 .is_some_and(|w| w & (1 << (i % 64)) != 0)
86}
87
88#[must_use]
90pub fn mining_speed(held: Option<u16>, block: u16, c: &DigConditions) -> f32 {
91 let mut f = held.map_or(1.0, |i| speed_multiplier(i, block));
92 if f > 1.0 && c.efficiency > 0 && held.is_some() {
93 let i = i32::from(c.efficiency);
94 f += (i * i + 1) as f32;
95 }
96 if let Some(amp) = c.haste {
97 f *= 1.0 + amp.wrapping_add(1) as f32 * 0.2;
98 }
99 if let Some(amp) = c.mining_fatigue {
100 f *= match amp {
101 0 => 0.3,
102 1 => 0.09,
103 2 => 0.0027,
104 _ => 8.1e-4,
105 };
106 }
107 if c.eye_in_water && !c.aqua_affinity {
108 f /= 5.0;
109 }
110 if !c.on_ground {
111 f /= 5.0;
112 }
113 f
114}
115
116#[must_use]
118pub fn effective_tool(held: Option<u16>, block: u16) -> bool {
119 no_tool_needed(block) || held.is_some_and(|i| is_effective(i, block))
120}
121
122#[must_use]
124pub fn destroy_progress(held: Option<u16>, block: u16, c: &DigConditions) -> f32 {
125 let f = strength(block);
126 if f < 0.0 {
127 return 0.0;
128 }
129 let speed = mining_speed(held, block, c);
130 if effective_tool(held, block) {
131 speed / f / 30.0
132 } else {
133 speed / f / 100.0
134 }
135}