Skip to main content

ghast_data/v1_12/
dig.rs

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