Skip to main content

ghast_data/v26_1/
dig.rs

1//! 26.1 block breaking time: vanilla's destroy progress per tick and the tick on which the client
2//! reports the block broken (`MultiPlayerGameMode.startDestroyBlock` / `continueDestroyBlock`,
3//! `BlockBehaviour.getDestroyProgress`, `Player.getDestroySpeed`), in `f32` exactly as the client
4//! computes it. Verified against the oracle (`tests/golden/26.1/dig.jsonl.gz`).
5//!
6//! The functions take the tool's numbers rather than an item, so a stack whose `minecraft:tool`
7//! component a server changed works the same: pass that component's speed and correctness (for
8//! default stacks: [`Item::destroy_speed`](super::Item::destroy_speed) /
9//! [`Item::correct_for_drops`](super::Item::correct_for_drops)).
10
11use super::{BlockState, Item};
12
13/// The player's mining conditions (attribute values as the server sent them, effect amplifiers).
14#[derive(Clone, Copy, Debug, PartialEq)]
15pub struct DigConditions {
16    /// `minecraft:mining_efficiency` (efficiency enchantment: level² + 1; default 0).
17    pub mining_efficiency: f64,
18    /// `minecraft:block_break_speed` (default 1).
19    pub block_break_speed: f64,
20    /// `minecraft:submerged_mining_speed` (default 0.2; aqua affinity raises it).
21    pub submerged_mining_speed: f64,
22    /// Haste amplifier (0 = Haste I), if active.
23    pub haste: Option<i32>,
24    /// Conduit power amplifier, if active (counts like haste).
25    pub conduit_power: Option<i32>,
26    /// Mining fatigue amplifier, if active.
27    pub mining_fatigue: Option<i32>,
28    /// The eyes are in water (`isEyeInFluid(WATER)`).
29    pub eye_in_water: bool,
30    /// On the ground (otherwise the speed is divided by 5).
31    pub on_ground: bool,
32}
33
34impl Default for DigConditions {
35    fn default() -> Self {
36        DigConditions {
37            mining_efficiency: 0.0,
38            block_break_speed: 1.0,
39            submerged_mining_speed: 0.2,
40            haste: None,
41            conduit_power: None,
42            mining_fatigue: None,
43            eye_in_water: false,
44            on_ground: true,
45        }
46    }
47}
48
49/// `Player.getDestroySpeed`: the held tool's speed on the block with efficiency, effects,
50/// attributes, water and air penalties applied.
51#[must_use]
52pub fn destroy_speed(tool_speed: f32, c: &DigConditions) -> f32 {
53    let mut speed = tool_speed;
54    if speed > 1.0 {
55        speed += c.mining_efficiency as f32;
56    }
57    if c.haste.is_some() || c.conduit_power.is_some() {
58        let amp = c.haste.unwrap_or(0).max(c.conduit_power.unwrap_or(0));
59        speed *= 1.0 + amp.wrapping_add(1) as f32 * 0.2;
60    }
61    if let Some(amp) = c.mining_fatigue {
62        speed *= match amp {
63            0 => 0.3,
64            1 => 0.09,
65            2 => 0.0027,
66            _ => 8.1e-4,
67        };
68    }
69    speed *= c.block_break_speed as f32;
70    if c.eye_in_water {
71        speed *= c.submerged_mining_speed as f32;
72    }
73    if !c.on_ground {
74        speed /= 5.0;
75    }
76    speed
77}
78
79/// `BlockBehaviour.getDestroyProgress`: the fraction of the block broken per tick.
80/// `tool_correct` is the held stack's "correct for drops" on this block.
81#[must_use]
82pub fn destroy_progress(
83    state: BlockState,
84    tool_speed: f32,
85    tool_correct: bool,
86    c: &DigConditions,
87) -> f32 {
88    let time = state.destroy_time();
89    if time == -1.0 {
90        return 0.0;
91    }
92    let modifier = if !state.requires_correct_tool_for_drops() || tool_correct {
93        30
94    } else {
95        100
96    };
97    destroy_speed(tool_speed, c) / time / modifier as f32
98}
99
100/// [`destroy_progress`] for a held item with its default components (`None` = empty hand).
101#[must_use]
102pub fn destroy_progress_with(state: BlockState, held: Option<Item>, c: &DigConditions) -> f32 {
103    let b = state.block();
104    let (speed, correct) = held.map_or((1.0, false), |i| {
105        (i.destroy_speed(b), i.correct_for_drops(b))
106    });
107    destroy_progress(state, speed, correct, c)
108}
109
110pub use crate::common::{BreakTime, break_time};