Skip to main content

azalea_entity/
mining.rs

1use azalea_block::{BlockBehavior, BlockTrait};
2use azalea_registry::{
3    builtin::{BlockKind, MobEffect},
4    item::{ItemStack, components::Tool},
5};
6
7use crate::{ActiveEffects, Attributes, FluidOnEyes, Physics};
8
9/// How much progress is made towards mining the block per tick, as a
10/// percentage.
11///
12/// If this is 1, then the block gets broken instantly.
13///
14/// You can divide 1 by this and then round up to get the number of ticks it
15/// takes to mine the block.
16///
17/// The player inventory is needed to check your armor and offhand for modifiers
18/// to your mining speed.
19pub fn get_mine_progress(
20    block: &dyn BlockTrait,
21    held_item: &ItemStack,
22    fluid_on_eyes: &FluidOnEyes,
23    physics: &Physics,
24    attributes: &Attributes,
25    active_effects: &ActiveEffects,
26) -> f32 {
27    let block_behavior: BlockBehavior = block.behavior();
28
29    let destroy_time = block_behavior.destroy_time;
30    if destroy_time == -1. {
31        return 0.;
32    }
33    let divisor = if has_correct_tool_for_drops(block, held_item) {
34        30
35    } else {
36        100
37    };
38
39    let base_destroy_speed = destroy_speed(
40        block.as_block_kind(),
41        held_item,
42        fluid_on_eyes,
43        physics,
44        attributes,
45        active_effects,
46    );
47    (base_destroy_speed / destroy_time) / (divisor as f32)
48}
49
50fn has_correct_tool_for_drops(block: &dyn BlockTrait, item: &ItemStack) -> bool {
51    if !block.behavior().requires_correct_tool_for_drops {
52        return true;
53    }
54    let Some(tool) = item.get_component::<Tool>() else {
55        return false;
56    };
57    let registry_block = block.as_block_kind();
58    for rule in &tool.rules {
59        if let Some(correct) = rule.correct_for_drops
60            && rule.blocks.contains(registry_block)
61        {
62            return correct;
63        }
64    }
65
66    false
67}
68
69/// Returns the destroy speed of the given block with the given tool, taking
70/// enchantments and effects into account.
71///
72/// If the player is not holding anything, then `tool` should be
73/// `ItemKind::Air`.
74fn destroy_speed(
75    block: BlockKind,
76    tool: &ItemStack,
77    _fluid_on_eyes: &FluidOnEyes,
78    physics: &Physics,
79    attributes: &Attributes,
80    active_effects: &ActiveEffects,
81) -> f32 {
82    let mut speed = base_destroy_speed(block, tool);
83
84    if speed > 1. {
85        // efficiency enchantment
86        speed += attributes.mining_efficiency.calculate() as f32;
87    }
88
89    if let Some(dig_speed_amplifier) = active_effects.get_dig_speed_amplifier() {
90        speed *= 1. + (dig_speed_amplifier + 1) as f32 * 0.2;
91    }
92
93    if let Some(dig_slowdown) = active_effects.get_level(MobEffect::MiningFatigue) {
94        let multiplier = match dig_slowdown {
95            0 => 0.3,
96            1 => 0.09,
97            2 => 0.0027,
98            _ => 8.1E-4,
99        };
100        speed *= multiplier;
101    }
102
103    speed *= attributes.block_break_speed.calculate() as f32;
104
105    // TODO
106    // if **fluid_on_eyes == FluidKind::Water
107    //     && enchantments::get_enchant_level(registry::Enchantment::AquaAffinity,
108    // player_inventory)         == 0
109    // {
110    //     base_destroy_speed /= 5.;
111    // }
112
113    if !physics.on_ground {
114        speed /= 5.;
115    }
116
117    speed
118}
119
120fn base_destroy_speed(block: BlockKind, item: &ItemStack) -> f32 {
121    let tool = item.get_component::<Tool>();
122    let Some(tool) = tool else { return 1. };
123    for rule in &tool.rules {
124        if let Some(speed) = rule.speed
125            && rule.blocks.contains(block)
126        {
127            return speed;
128        }
129    }
130    tool.default_mining_speed
131}