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