Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use mcrs_minecraft_block::block_keys::Block;
use mcrs_minecraft_entity::damage_type::DamageType;
use mcrs_minecraft_entity::entity_type_keys::EntityType;
use mcrs_minecraft_registries::Registries;
use mcrs_minecraft_registries::Tags;
Expand All @@ -15,6 +16,7 @@ pub struct ItemTableInputs {
names: [Vec<ResourceLocationBuf>; NAMED_REGISTRIES.len()],
block_tags: Vec<TagName>,
entity_type_tags: Vec<TagName>,
damage_type_tags: Vec<TagName>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand All @@ -31,6 +33,7 @@ impl ItemTableInputs {
names: NAMED_REGISTRIES.map(|named| names(registries, named.key)),
block_tags: tag_names(registries.tags::<Block>()),
entity_type_tags: tag_names(registries.tags::<EntityType>()),
damage_type_tags: tag_names(registries.tags::<DamageType>()),
}
}
}
Expand Down
16 changes: 15 additions & 1 deletion crates/mcrs_minecraft_engine_registries/src/registry_names.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use mcrs_minecraft_block::block_keys::Block;
use mcrs_minecraft_entity::damage_type::DamageType;
use mcrs_minecraft_entity::entity_type_keys::EntityType;
use mcrs_minecraft_item_stack::NameLookup;
use mcrs_minecraft_item_stack::NameTable;
Expand All @@ -13,6 +14,7 @@ pub struct RegistryNames<'a> {
items: Option<&'a ItemTable>,
block_tags: Option<Tags<Block>>,
entity_type_tags: Option<Tags<EntityType>>,
damage_type_tags: Option<Tags<DamageType>>,
}

impl<'a> RegistryNames<'a> {
Expand All @@ -23,6 +25,7 @@ impl<'a> RegistryNames<'a> {
items: Some(items),
block_tags: registries.tags::<Block>(),
entity_type_tags: registries.tags::<EntityType>(),
damage_type_tags: registries.tags::<DamageType>(),
}
}

Expand All @@ -32,6 +35,7 @@ impl<'a> RegistryNames<'a> {
items: None,
block_tags: registries.tags::<Block>(),
entity_type_tags: registries.tags::<EntityType>(),
damage_type_tags: registries.tags::<DamageType>(),
}
}
}
Expand All @@ -41,7 +45,7 @@ pub(crate) struct NamedRegistry {
pub(crate) key: &'static str,
}

pub(crate) const NAMED_REGISTRIES: [NamedRegistry; 14] = [
pub(crate) const NAMED_REGISTRIES: [NamedRegistry; 15] = [
NamedRegistry {
table: NameTable::Attribute,
key: "minecraft:attribute",
Expand Down Expand Up @@ -98,6 +102,10 @@ pub(crate) const NAMED_REGISTRIES: [NamedRegistry; 14] = [
table: NameTable::PaintingVariant,
key: "minecraft:painting_variant",
},
NamedRegistry {
table: NameTable::DamageType,
key: "minecraft:damage_type",
},
];

fn registry_key(table: NameTable) -> Option<&'static str> {
Expand Down Expand Up @@ -130,6 +138,9 @@ impl NameLookup for RegistryNames<'_> {
if table == NameTable::EntityTypeTag {
return tag_id(self.entity_type_tags.as_ref()?, name);
}
if table == NameTable::DamageTypeTag {
return tag_id(self.damage_type_tags.as_ref()?, name);
}
let key = ResourceLocation::read(registry_key(table)?).ok()?;
self.registries.id_of(key, name)
}
Expand All @@ -144,6 +155,9 @@ impl NameLookup for RegistryNames<'_> {
if table == NameTable::EntityTypeTag {
return tag_name(self.entity_type_tags.as_ref()?, id);
}
if table == NameTable::DamageTypeTag {
return tag_name(self.damage_type_tags.as_ref()?, id);
}
let key = ResourceLocation::read(registry_key(table)?).ok()?;
let names = self.registries.names_of(key)?;
Some(names.get(usize::from(id))?.as_location())
Expand Down
94 changes: 94 additions & 0 deletions crates/mcrs_minecraft_item_stack/src/blocks_attacks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use alloc::vec;
use alloc::vec::Vec;
use core::hash::Hash;
use core::hash::Hasher;
use core::mem::align_of;
use core::mem::size_of;

use crate::DamageReduction;
use crate::DamageTypeSet;
use crate::ItemDamage;
use crate::SoundEventRef;
use crate::float_key;

#[derive(Clone, Debug)]
#[cfg_attr(feature = "bevy", derive(bevy_ecs::component::Component))]
pub struct BlocksAttacks {
pub block_delay_seconds: f32,
pub disable_cooldown_scale: f32,
pub damage_reductions: Vec<DamageReduction>,
pub item_damage: ItemDamage,
pub bypassed_by: Option<DamageTypeSet>,
pub block_sound: Option<SoundEventRef>,
pub disabled_sound: Option<SoundEventRef>,
}

const _: () = {
assert!(
size_of::<BlocksAttacks>()
>= size_of::<Vec<DamageReduction>>()
+ size_of::<Option<DamageTypeSet>>()
+ 2 * size_of::<Option<SoundEventRef>>()
+ 20,
"BlocksAttacks layout matches expected fields"
);
assert!(
align_of::<BlocksAttacks>() == align_of::<Option<SoundEventRef>>(),
"BlocksAttacks aligns with pointer fields"
);
};

impl BlocksAttacks {
pub const DEFAULT_BLOCK_DELAY_SECONDS: f32 = 0.0;
pub const DEFAULT_DISABLE_COOLDOWN_SCALE: f32 = 1.0;

#[must_use]
pub fn new() -> Self {
Self {
block_delay_seconds: Self::DEFAULT_BLOCK_DELAY_SECONDS,
disable_cooldown_scale: Self::DEFAULT_DISABLE_COOLDOWN_SCALE,
damage_reductions: vec![DamageReduction::default()],
item_damage: ItemDamage::default(),
bypassed_by: None,
block_sound: None,
disabled_sound: None,
}
}

pub(crate) fn has_default_reductions(&self) -> bool {
matches!(self.damage_reductions.as_slice(), [only] if only.is_default_exact())
}
}

impl Default for BlocksAttacks {
fn default() -> Self {
Self::new()
}
}

impl PartialEq for BlocksAttacks {
fn eq(&self, other: &Self) -> bool {
float_key::of_f32(self.block_delay_seconds) == float_key::of_f32(other.block_delay_seconds)
&& float_key::of_f32(self.disable_cooldown_scale)
== float_key::of_f32(other.disable_cooldown_scale)
&& self.damage_reductions == other.damage_reductions
&& self.item_damage == other.item_damage
&& self.bypassed_by == other.bypassed_by
&& self.block_sound == other.block_sound
&& self.disabled_sound == other.disabled_sound
}
}

impl Eq for BlocksAttacks {}

impl Hash for BlocksAttacks {
fn hash<H: Hasher>(&self, state: &mut H) {
float_key::of_f32(self.block_delay_seconds).hash(state);
float_key::of_f32(self.disable_cooldown_scale).hash(state);
self.damage_reductions.hash(state);
self.item_damage.hash(state);
self.bypassed_by.hash(state);
self.block_sound.hash(state);
self.disabled_sound.hash(state);
}
}
77 changes: 77 additions & 0 deletions crates/mcrs_minecraft_item_stack/src/damage_reduction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use core::hash::Hash;
use core::hash::Hasher;
use core::mem::align_of;
use core::mem::size_of;

use crate::DamageTypeSet;
use crate::float_key;

#[derive(Clone, Debug)]
pub struct DamageReduction {
pub horizontal_blocking_angle: f32,
pub kind: Option<DamageTypeSet>,
pub base: f32,
pub factor: f32,
}

const _: () = {
assert!(
size_of::<DamageReduction>() >= size_of::<Option<DamageTypeSet>>() + 12,
"DamageReduction packs optional DamageTypeSet and three floats"
);
assert!(
align_of::<DamageReduction>() == align_of::<DamageTypeSet>(),
"DamageReduction aligns with DamageTypeSet"
);
};

impl DamageReduction {
pub const DEFAULT_HORIZONTAL_BLOCKING_ANGLE: f32 = 90.0;
pub const DEFAULT_BASE: f32 = 0.0;
pub const DEFAULT_FACTOR: f32 = 1.0;

#[must_use]
pub const fn new() -> Self {
Self {
horizontal_blocking_angle: Self::DEFAULT_HORIZONTAL_BLOCKING_ANGLE,
kind: None,
base: Self::DEFAULT_BASE,
factor: Self::DEFAULT_FACTOR,
}
}

pub(crate) fn is_default_exact(&self) -> bool {
self.horizontal_blocking_angle.to_bits()
== Self::DEFAULT_HORIZONTAL_BLOCKING_ANGLE.to_bits()
&& self.kind.is_none()
&& self.base.to_bits() == Self::DEFAULT_BASE.to_bits()
&& self.factor.to_bits() == Self::DEFAULT_FACTOR.to_bits()
}
}

impl Default for DamageReduction {
fn default() -> Self {
Self::new()
}
}

impl PartialEq for DamageReduction {
fn eq(&self, other: &Self) -> bool {
float_key::of_f32(self.horizontal_blocking_angle)
== float_key::of_f32(other.horizontal_blocking_angle)
&& self.kind == other.kind
&& float_key::of_f32(self.base) == float_key::of_f32(other.base)
&& float_key::of_f32(self.factor) == float_key::of_f32(other.factor)
}
}

impl Eq for DamageReduction {}

impl Hash for DamageReduction {
fn hash<H: Hasher>(&self, state: &mut H) {
float_key::of_f32(self.horizontal_blocking_angle).hash(state);
self.kind.hash(state);
float_key::of_f32(self.base).hash(state);
float_key::of_f32(self.factor).hash(state);
}
}
16 changes: 16 additions & 0 deletions crates/mcrs_minecraft_item_stack/src/damage_type_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use core::mem::align_of;
use core::mem::size_of;

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DamageTypeId(pub u16);

const _: () = {
assert!(
size_of::<DamageTypeId>() == size_of::<u16>(),
"DamageTypeId matches u16 size"
);
assert!(
align_of::<DamageTypeId>() == align_of::<u16>(),
"DamageTypeId matches u16 alignment"
);
};
24 changes: 24 additions & 0 deletions crates/mcrs_minecraft_item_stack/src/damage_type_set.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use alloc::borrow::Cow;
use core::mem::align_of;
use core::mem::size_of;

use crate::BlockSet;
use crate::DamageTypeId;
use crate::DamageTypeTagId;

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum DamageTypeSet {
Tag(DamageTypeTagId),
Types(Cow<'static, [DamageTypeId]>),
}

const _: () = {
assert!(
size_of::<DamageTypeSet>() == size_of::<BlockSet>(),
"DamageTypeSet matches BlockSet size"
);
assert!(
align_of::<DamageTypeSet>() == align_of::<BlockSet>(),
"DamageTypeSet matches BlockSet alignment"
);
};
16 changes: 16 additions & 0 deletions crates/mcrs_minecraft_item_stack/src/damage_type_tag_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use core::mem::align_of;
use core::mem::size_of;

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DamageTypeTagId(pub u16);

const _: () = {
assert!(
size_of::<DamageTypeTagId>() == size_of::<u16>(),
"DamageTypeTagId matches u16 size"
);
assert!(
align_of::<DamageTypeTagId>() == align_of::<u16>(),
"DamageTypeTagId matches u16 alignment"
);
};
2 changes: 2 additions & 0 deletions crates/mcrs_minecraft_item_stack/src/entity_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::AttackAnimation;
use crate::AttributeModifiers;
use crate::BannerPatterns;
use crate::Bees;
use crate::BlocksAttacks;
use crate::BundleContents;
use crate::ChargedProjectiles;
use crate::Container;
Expand Down Expand Up @@ -58,6 +59,7 @@ impl EntityForm for Unbreakable {}

impl EntityForm for Food {}
impl EntityForm for UseEffects {}
impl EntityForm for BlocksAttacks {}

impl EntityForm for MapId {}

Expand Down
1 change: 1 addition & 0 deletions crates/mcrs_minecraft_item_stack/src/for_each_builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ macro_rules! for_each_builtin {
UseEffects => "use_effects",
Food => "food",
Tool => "tool",
BlocksAttacks => "blocks_attacks",
MapId => "map_id",
MapDecorations => "map_decorations",
Profile => "profile",
Expand Down
Loading
Loading