Skip to main content

ghast_data/v1_8/
dig.rs

1//! 1.8.9 block breaking time, from the jar (oracle `tests/golden/1.8.9/dig_facts.jsonl.gz`) and
2//! proven against the real client (`dig.jsonl.gz`: `ClientPlayerInteractionManager.attackBlock` /
3//! `updateBlockBreakingProgress`, `Block.calcBlockBreakingData`, `PlayerEntity.getMiningSpeed`),
4//! in `f32` exactly as the client computes it.
5//!
6//! The tool tables here are the jar's (`ItemStack.getMiningSpeedMultiplier` / `isEffectiveOn` per
7//! item and block); minecraft-data's material / harvest-tool tables in [`super::BlockInfo`] differ
8//! from them in places — use these for timing.
9
10use crate::generated::v1_8 as g;
11
12pub use crate::common::{BreakTime, break_time};
13
14/// The player's mining conditions.
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub struct DigConditions {
17    /// Efficiency enchantment level of the held stack (0 = none).
18    pub efficiency: u8,
19    /// Haste amplifier (0 = Haste I), if active.
20    pub haste: Option<i32>,
21    /// Mining fatigue amplifier, if active.
22    pub mining_fatigue: Option<i32>,
23    /// The eyes are in water (`isSubmergedIn(Material.WATER)`).
24    pub eye_in_water: bool,
25    /// Aqua affinity on any armour piece.
26    pub aqua_affinity: bool,
27    /// On the ground (otherwise the speed is divided by 5).
28    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/// `ItemStack.getMiningSpeedMultiplier(block)` of a stack of `item` (1 for every other pair).
52#[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/// `ItemStack.isEffectiveOn(block)` of a stack of `item`.
64#[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/// Block strength (hardness) from the jar; `-1.0` = unbreakable.
70#[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/// The block's material needs no tool (`Material.doesBlockMovement` in Legacy Yarn; MCP
79/// `isToolNotRequired`): any hand harvests it.
80#[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/// `PlayerEntity.getMiningSpeed(block)`: `held` is the main-hand item id (`None` = empty hand).
89#[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/// `PlayerEntity.isUsingEffectiveTool(block)`.
117#[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/// `Block.calcBlockBreakingData`: the fraction of the block broken per tick.
123#[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}