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<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/// `ItemStack.getMiningSpeedMultiplier(block)` of a stack of `item` (1 for every other pair).
54#[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/// `ItemStack.isEffectiveOn(block)` of a stack of `item`.
66#[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/// Block strength (hardness) from the jar; `-1.0` = unbreakable.
72#[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/// The block's material needs no tool (`Material.doesBlockMovement` in Legacy Yarn; MCP
81/// `isToolNotRequired`): any hand harvests it.
82#[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/// `PlayerEntity.getMiningSpeed(block)`: `held` is the main-hand item id (`None` = empty hand).
91#[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/// `PlayerEntity.isUsingEffectiveTool(block)`.
119#[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/// `Block.calcBlockBreakingData`: the fraction of the block broken per tick.
125#[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}