From 7405427199e5a994d4a6a706f84434a69cb7a7d9 Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Fri, 14 Jul 2023 22:20:40 -0500 Subject: Mining (#95) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * more mining stuff * initialize azalea-tags crate * more mining stuff 2 * mining in ecs * well technically mining works but no codegen for how long it takes to mine each block yet * rename downloads to __cache__ it was bothering me since it's not *just* downloads * codegen block behavior * fix not sending packet to finish breaking block * mining animation 🎉 * clippy * cleanup, move Client::mine into a client extension * add azalea/src/mining.rs --------- Co-authored-by: mat --- azalea-core/src/lib.rs | 63 +--------------- azalea-core/src/math.rs | 56 +++++++++++++++ azalea-core/src/particle.rs | 155 ++++++++++++++++++++++++++++++++++++++++ azalea-core/src/particle/mod.rs | 155 ---------------------------------------- azalea-core/src/tier.rs | 46 ++++++++++++ 5 files changed, 260 insertions(+), 215 deletions(-) create mode 100644 azalea-core/src/math.rs create mode 100755 azalea-core/src/particle.rs delete mode 100755 azalea-core/src/particle/mod.rs create mode 100644 azalea-core/src/tier.rs (limited to 'azalea-core/src') diff --git a/azalea-core/src/lib.rs b/azalea-core/src/lib.rs index 7bf4a12c..8ed62b06 100755 --- a/azalea-core/src/lib.rs +++ b/azalea-core/src/lib.rs @@ -22,8 +22,7 @@ pub use direction::*; mod delta; pub use delta::*; -mod particle; -pub use particle::*; +pub mod particle; mod cursor3d; pub use cursor3d::*; @@ -37,61 +36,5 @@ pub use aabb::*; mod block_hit_result; pub use block_hit_result::*; -// some random math things used in minecraft are defined down here - -// TODO: make this generic -pub fn binary_search(mut min: i32, max: i32, predicate: &dyn Fn(i32) -> bool) -> i32 { - let mut diff = max - min; - while diff > 0 { - let diff_mid = diff / 2; - let mid = min + diff_mid; - if predicate(mid) { - diff = diff_mid; - } else { - min = mid + 1; - diff -= diff_mid + 1; - } - } - - min -} - -pub fn lcm(a: u32, b: u32) -> u64 { - let gcd = gcd(a, b); - (a as u64) * (b / gcd) as u64 -} -pub fn gcd(mut a: u32, mut b: u32) -> u32 { - while b != 0 { - let t = b; - b = a % b; - a = t; - } - a -} - -pub fn lerp(amount: T, a: T, b: T) -> T { - a + amount * (b - a) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_gcd() { - assert_eq!(gcd(0, 0), 0); - assert_eq!(gcd(1, 1), 1); - - assert_eq!(gcd(0, 1), 1); - assert_eq!(gcd(1, 0), 1); - - assert_eq!(gcd(12, 8), 4); - assert_eq!(gcd(8, 12), 4); - - assert_eq!(gcd(12, 9), 3); - assert_eq!(gcd(9, 12), 3); - - assert_eq!(gcd(12, 7), 1); - assert_eq!(gcd(7, 12), 1); - } -} +pub mod math; +pub mod tier; diff --git a/azalea-core/src/math.rs b/azalea-core/src/math.rs new file mode 100644 index 00000000..2e5e78f6 --- /dev/null +++ b/azalea-core/src/math.rs @@ -0,0 +1,56 @@ +// TODO: make this generic +pub fn binary_search(mut min: i32, max: i32, predicate: &dyn Fn(i32) -> bool) -> i32 { + let mut diff = max - min; + while diff > 0 { + let diff_mid = diff / 2; + let mid = min + diff_mid; + if predicate(mid) { + diff = diff_mid; + } else { + min = mid + 1; + diff -= diff_mid + 1; + } + } + + min +} + +pub fn lcm(a: u32, b: u32) -> u64 { + let gcd = gcd(a, b); + (a as u64) * (b / gcd) as u64 +} +pub fn gcd(mut a: u32, mut b: u32) -> u32 { + while b != 0 { + let t = b; + b = a % b; + a = t; + } + a +} + +pub fn lerp(amount: T, a: T, b: T) -> T { + a + amount * (b - a) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_gcd() { + assert_eq!(gcd(0, 0), 0); + assert_eq!(gcd(1, 1), 1); + + assert_eq!(gcd(0, 1), 1); + assert_eq!(gcd(1, 0), 1); + + assert_eq!(gcd(12, 8), 4); + assert_eq!(gcd(8, 12), 4); + + assert_eq!(gcd(12, 9), 3); + assert_eq!(gcd(9, 12), 3); + + assert_eq!(gcd(12, 7), 1); + assert_eq!(gcd(7, 12), 1); + } +} diff --git a/azalea-core/src/particle.rs b/azalea-core/src/particle.rs new file mode 100755 index 00000000..60128f3f --- /dev/null +++ b/azalea-core/src/particle.rs @@ -0,0 +1,155 @@ +use crate::BlockPos; +use azalea_buf::McBuf; +use azalea_inventory::ItemSlot; + +#[cfg_attr(feature = "bevy_ecs", derive(bevy_ecs::component::Component))] +#[derive(Debug, Clone, McBuf, Default)] +pub struct Particle { + #[var] + pub id: i32, + pub data: ParticleData, +} + +#[derive(Clone, Debug, McBuf, Default)] +pub enum ParticleData { + AmbientEntityEffect, + AngryVillager, + Block(BlockParticle), + BlockMarker(BlockParticle), + Bubble, + Cloud, + Crit, + DamageIndicator, + DragonBreath, + DrippingLava, + FallingLava, + LandingLava, + DrippingWater, + FallingWater, + Dust(DustParticle), + DustColorTransition(DustColorTransitionParticle), + Effect, + ElderGuardian, + EnchantedHit, + Enchant, + EndRod, + #[default] + EntityEffect, + ExplosionEmitter, + Explosion, + FallingDust(BlockParticle), + Firework, + Fishing, + Flame, + SoulFireFlame, + Soul, + Flash, + HappyVillager, + Composter, + Heart, + InstantEffect, + Item(ItemParticle), + Vibration(VibrationParticle), + ItemSlime, + ItemSnowball, + LargeSmoke, + Lava, + Mycelium, + Note, + Poof, + Portal, + Rain, + Smoke, + Sneeze, + Spit, + SquidInk, + SweepAttack, + TotemOfUndying, + Underwater, + Splash, + Witch, + BubblePop, + CurrentDown, + BubbleColumnUp, + Nautilus, + Dolphin, + CampfireCozySmoke, + CampfireSignalSmoke, + DrippingHoney, + FallingHoney, + LandingHoney, + FallingNectar, + FallingSporeBlossom, + Ash, + CrimsonSpore, + WarpedSpore, + SporeBlossomAir, + DrippingObsidianTear, + FallingObsidianTear, + LandingObsidianTear, + ReversePortal, + WhiteAsh, + SmallFlame, + Snowflake, + DrippingDripstoneLava, + FallingDripstoneLava, + DrippingDripstoneWater, + FallingDripstoneWater, + GlowSquidInk, + Glow, + WaxOn, + WaxOff, + ElectricSpark, + Scrape, +} + +#[derive(Debug, Clone, McBuf)] +pub struct BlockParticle { + #[var] + pub block_state: i32, +} +#[derive(Debug, Clone, McBuf)] +pub struct DustParticle { + /// Red value, 0-1 + pub red: f32, + /// Green value, 0-1 + pub green: f32, + /// Blue value, 0-1 + pub blue: f32, + /// The scale, will be clamped between 0.01 and 4. + pub scale: f32, +} + +#[derive(Debug, Clone, McBuf)] +pub struct DustColorTransitionParticle { + /// Red value, 0-1 + pub from_red: f32, + /// Green value, 0-1 + pub from_green: f32, + /// Blue value, 0-1 + pub from_blue: f32, + /// The scale, will be clamped between 0.01 and 4. + pub scale: f32, + /// Red value, 0-1 + pub to_red: f32, + /// Green value, 0-1 + pub to_green: f32, + /// Blue value, 0-1 + pub to_blue: f32, +} + +#[derive(Debug, Clone, McBuf)] +pub struct ItemParticle { + pub item: ItemSlot, +} + +#[derive(Debug, Clone, McBuf)] +pub struct VibrationParticle { + pub origin: BlockPos, + pub position_type: String, + pub block_position: BlockPos, + #[var] + pub entity_id: u32, + #[var] + pub ticks: u32, +} diff --git a/azalea-core/src/particle/mod.rs b/azalea-core/src/particle/mod.rs deleted file mode 100755 index 60128f3f..00000000 --- a/azalea-core/src/particle/mod.rs +++ /dev/null @@ -1,155 +0,0 @@ -use crate::BlockPos; -use azalea_buf::McBuf; -use azalea_inventory::ItemSlot; - -#[cfg_attr(feature = "bevy_ecs", derive(bevy_ecs::component::Component))] -#[derive(Debug, Clone, McBuf, Default)] -pub struct Particle { - #[var] - pub id: i32, - pub data: ParticleData, -} - -#[derive(Clone, Debug, McBuf, Default)] -pub enum ParticleData { - AmbientEntityEffect, - AngryVillager, - Block(BlockParticle), - BlockMarker(BlockParticle), - Bubble, - Cloud, - Crit, - DamageIndicator, - DragonBreath, - DrippingLava, - FallingLava, - LandingLava, - DrippingWater, - FallingWater, - Dust(DustParticle), - DustColorTransition(DustColorTransitionParticle), - Effect, - ElderGuardian, - EnchantedHit, - Enchant, - EndRod, - #[default] - EntityEffect, - ExplosionEmitter, - Explosion, - FallingDust(BlockParticle), - Firework, - Fishing, - Flame, - SoulFireFlame, - Soul, - Flash, - HappyVillager, - Composter, - Heart, - InstantEffect, - Item(ItemParticle), - Vibration(VibrationParticle), - ItemSlime, - ItemSnowball, - LargeSmoke, - Lava, - Mycelium, - Note, - Poof, - Portal, - Rain, - Smoke, - Sneeze, - Spit, - SquidInk, - SweepAttack, - TotemOfUndying, - Underwater, - Splash, - Witch, - BubblePop, - CurrentDown, - BubbleColumnUp, - Nautilus, - Dolphin, - CampfireCozySmoke, - CampfireSignalSmoke, - DrippingHoney, - FallingHoney, - LandingHoney, - FallingNectar, - FallingSporeBlossom, - Ash, - CrimsonSpore, - WarpedSpore, - SporeBlossomAir, - DrippingObsidianTear, - FallingObsidianTear, - LandingObsidianTear, - ReversePortal, - WhiteAsh, - SmallFlame, - Snowflake, - DrippingDripstoneLava, - FallingDripstoneLava, - DrippingDripstoneWater, - FallingDripstoneWater, - GlowSquidInk, - Glow, - WaxOn, - WaxOff, - ElectricSpark, - Scrape, -} - -#[derive(Debug, Clone, McBuf)] -pub struct BlockParticle { - #[var] - pub block_state: i32, -} -#[derive(Debug, Clone, McBuf)] -pub struct DustParticle { - /// Red value, 0-1 - pub red: f32, - /// Green value, 0-1 - pub green: f32, - /// Blue value, 0-1 - pub blue: f32, - /// The scale, will be clamped between 0.01 and 4. - pub scale: f32, -} - -#[derive(Debug, Clone, McBuf)] -pub struct DustColorTransitionParticle { - /// Red value, 0-1 - pub from_red: f32, - /// Green value, 0-1 - pub from_green: f32, - /// Blue value, 0-1 - pub from_blue: f32, - /// The scale, will be clamped between 0.01 and 4. - pub scale: f32, - /// Red value, 0-1 - pub to_red: f32, - /// Green value, 0-1 - pub to_green: f32, - /// Blue value, 0-1 - pub to_blue: f32, -} - -#[derive(Debug, Clone, McBuf)] -pub struct ItemParticle { - pub item: ItemSlot, -} - -#[derive(Debug, Clone, McBuf)] -pub struct VibrationParticle { - pub origin: BlockPos, - pub position_type: String, - pub block_position: BlockPos, - #[var] - pub entity_id: u32, - #[var] - pub ticks: u32, -} diff --git a/azalea-core/src/tier.rs b/azalea-core/src/tier.rs new file mode 100644 index 00000000..e921899f --- /dev/null +++ b/azalea-core/src/tier.rs @@ -0,0 +1,46 @@ +pub fn get_item_tier(item: azalea_registry::Item) -> Option { + use azalea_registry::Item::*; + Some(match item { + WoodenPickaxe | WoodenShovel | WoodenAxe | WoodenHoe | WoodenSword => Tier::Wood, + StonePickaxe | StoneShovel | StoneAxe | StoneHoe | StoneSword => Tier::Stone, + IronPickaxe | IronShovel | IronAxe | IronHoe | IronSword => Tier::Iron, + DiamondPickaxe | DiamondShovel | DiamondAxe | DiamondHoe | DiamondSword => Tier::Diamond, + GoldenPickaxe | GoldenShovel | GoldenAxe | GoldenHoe | GoldenSword => Tier::Gold, + NetheritePickaxe | NetheriteShovel | NetheriteAxe | NetheriteHoe | NetheriteSword => { + Tier::Netherite + } + _ => return None, + }) +} + +pub enum Tier { + Wood, + Stone, + Iron, + Diamond, + Gold, + Netherite, +} + +impl Tier { + pub fn level(&self) -> u8 { + match self { + Tier::Wood => 0, + Tier::Stone => 1, + Tier::Iron => 2, + Tier::Diamond => 3, + Tier::Gold => 0, // gold is the same tier as wood + Tier::Netherite => 4, + } + } + pub fn speed(&self) -> f32 { + match self { + Tier::Wood => 2., + Tier::Stone => 4., + Tier::Iron => 6., + Tier::Diamond => 8., + Tier::Gold => 12., + Tier::Netherite => 9., + } + } +} -- cgit v1.2.3