diff --git a/Cargo.lock b/Cargo.lock index 6fa00dadf..03abd7ec5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1405,6 +1405,13 @@ dependencies = [ "vek", ] +[[package]] +name = "libcraft-effects" +version = "0.1.0" +dependencies = [ + "serde", +] + [[package]] name = "libcraft-generators" version = "0.1.0" @@ -2067,6 +2074,7 @@ dependencies = [ "bincode", "bytemuck", "libcraft-core", + "libcraft-effects", "libcraft-particles", "libcraft-text", "quill", diff --git a/Cargo.toml b/Cargo.toml index 3253e06c8..c881baa8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ members = [ "libcraft/macros", "libcraft/particles", "libcraft/text", + "libcraft/effects", # Quill "quill/sys-macros", diff --git a/feather/plugin-host/src/host_calls.rs b/feather/plugin-host/src/host_calls.rs index bf90ccb81..446bb6cf0 100644 --- a/feather/plugin-host/src/host_calls.rs +++ b/feather/plugin-host/src/host_calls.rs @@ -16,6 +16,7 @@ mod plugin_message; mod query; mod system; + macro_rules! host_calls { ( $($name:literal => $function:ident),* $(,)? diff --git a/libcraft/effects/Cargo.toml b/libcraft/effects/Cargo.toml new file mode 100644 index 000000000..ed4cbbe6f --- /dev/null +++ b/libcraft/effects/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "libcraft-effects" +version = "0.1.0" +authors = ["Yui Tanabe "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde = { version = "1", features = ["derive"] } diff --git a/libcraft/effects/src/lib.rs b/libcraft/effects/src/lib.rs new file mode 100644 index 000000000..d1165f90a --- /dev/null +++ b/libcraft/effects/src/lib.rs @@ -0,0 +1,84 @@ +use serde::{Deserialize, Serialize}; +use std::convert::TryFrom; + +#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)] +pub enum PotionEffect { + None = 0, + Speed = 1, + Slowness = 2, + Haste = 3, + MiningFatigue = 4, + Strength = 5, + InstantHealth = 6, + InstantDamage = 7, + JumpBoost = 8, + Nausea = 9, + Regeneration = 10, + Resistance = 11, + FireResistance = 12, + WaterBreathing = 13, + Invisibility = 14, + Blindness = 15, + NightVision = 16, + Hunger = 17, + Weakness = 18, + Poison = 19, + Wither = 20, + HealthBoost = 21, + Absorption = 22, + Saturation = 23, + Glowing = 24, + Levitation = 25, + Luck = 26, + BadLuck = 27, + SlowFalling = 28, + ConduitPower = 29, + DolphinsGrace = 30, + BadOmen = 31, + HeroOfTheVillage = 32, +} + +#[derive(Debug, Copy, Clone)] +pub struct PotionEffectConvertError; + +impl TryFrom<&str> for PotionEffect { + type Error = PotionEffectConvertError; + + fn try_from(value: &str) -> Result { + match value { + "speed" => Ok(Self::Speed), + "slowness" => Ok(Self::Slowness), + "haste" => Ok(Self::Haste), + "mining_fatigue" => Ok(Self::MiningFatigue), + "strength" => Ok(Self::Strength), + "instant_health" => Ok(Self::InstantHealth), + "instant_damage" => Ok(Self::InstantDamage), + "jump_boost" => Ok(Self::JumpBoost), + "nausea" => Ok(Self::Nausea), + "regeneration" => Ok(Self::Regeneration), + "resistance" => Ok(Self::Resistance), + "fire_resistance" => Ok(Self::FireResistance), + "water_breathing" => Ok(Self::WaterBreathing), + "invisibility" => Ok(Self::Invisibility), + "blindness" => Ok(Self::Blindness), + "night_vision" => Ok(Self::NightVision), + "hunger" => Ok(Self::Hunger), + "weakness" => Ok(Self::Weakness), + "poison" => Ok(Self::Poison), + "wither" => Ok(Self::Wither), + "health_boost" => Ok(Self::HealthBoost), + "absorption" => Ok(Self::Absorption), + "saturation" => Ok(Self::Saturation), + "glowing" => Ok(Self::Glowing), + "levitation" => Ok(Self::Levitation), + "luck" => Ok(Self::Luck), + "unluck" => Ok(Self::BadLuck), + "slow_falling" => Ok(Self::SlowFalling), + "conduit_power" => Ok(Self::ConduitPower), + "dolphins_grace" => Ok(Self::DolphinsGrace), + "bad_omen" => Ok(Self::BadOmen), + "hero_of_the_village" => Ok(Self::HeroOfTheVillage), + _ => Err(PotionEffectConvertError), + } + } +} diff --git a/quill/common/Cargo.toml b/quill/common/Cargo.toml index c03eb0166..50f9b95bf 100644 --- a/quill/common/Cargo.toml +++ b/quill/common/Cargo.toml @@ -10,6 +10,7 @@ bytemuck = { version = "1", features = ["derive"] } libcraft-core = { path = "../../libcraft/core" } libcraft-particles = { path = "../../libcraft/particles" } libcraft-text = { path = "../../libcraft/text" } +libcraft-effects = { path = "../../libcraft/effects" } serde = { version = "1", features = ["derive"] } smartstring = { version = "0.2", features = ["serde"] } uuid = { version = "0.8", features = ["serde"] } diff --git a/quill/common/src/component.rs b/quill/common/src/component.rs index 9d80223ea..3174a5016 100644 --- a/quill/common/src/component.rs +++ b/quill/common/src/component.rs @@ -64,6 +64,40 @@ host_component_enum! { // `Pod` components Position = 0, + // Effect Components + Speed = 1, + Slowness = 2, + Haste = 3, + MiningFatigue = 4, + Strength = 5, + InstantHealth = 6, + InstantDamage = 7, + JumpBoost = 8, + Nausea = 9, + Regeneration = 10, + Resistance = 11, + FireResistance = 12, + WaterBreathing = 13, + Invisibility = 14, + Blindness = 15, + NightVision = 16, + Hunger = 17, + Weakness = 18, + Poison = 19, + WitherEffect = 20, + HealthBoost = 21, + Absorption = 22, + Saturation = 23, + Glowing = 24, + Levitation = 25, + Luck = 26, + BadLuck = 27, + SlowFalling = 28, + ConduitPower = 29, + DolphinsGrace = 30, + BadOmen = 31, + HeroOfTheVillage = 32, + // Entity marker components AreaEffectCloud = 100, ArmorStand = 101, @@ -189,7 +223,6 @@ host_component_enum! { Sneaking = 1011, SneakEvent = 1012, - } } diff --git a/quill/common/src/components.rs b/quill/common/src/components.rs index 3d6cec3d8..9da04db6d 100644 --- a/quill/common/src/components.rs +++ b/quill/common/src/components.rs @@ -10,6 +10,8 @@ use std::{ use serde::{Deserialize, Serialize}; use smartstring::{LazyCompact, SmartString}; +use std::cmp::Ordering; +use std::collections::BTreeSet; /// Whether an entity is touching the ground. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] @@ -105,6 +107,81 @@ pub struct CreativeFlying(pub bool); bincode_component_impl!(CreativeFlying); +#[derive(Copy, Clone, Debug, Hash, Serialize, PartialEq, Eq, Deserialize)] + +/// Storing Potion effects info. +pub struct PotionApplication { + /// Strength Level of effect. + pub amplifier: u8, + /// Tick-based duration of the effect. + pub duration: u32, + /// Whether spawn particles or not. + pub particle: bool, + /// Whether the effect was given by a beacon or not. + pub ambient: bool, + /// Show effect icon or not. + pub icon: bool, +} + +impl Ord for PotionApplication { + fn cmp(&self, other: &Self) -> Ordering { + if self.amplifier > other.amplifier || self.duration > other.duration { + Ordering::Greater + } else if self.amplifier == self.amplifier || self.duration == other.duration { + Ordering::Equal + } else { + Ordering::Less + } + } +} +impl PartialOrd for PotionApplication { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +macro_rules! impl_effect { + ($ident:ident) => { + #[derive(Serialize, Deserialize, Eq, PartialEq, Hash)] + pub struct $ident(BTreeSet); + impl $ident {} + bincode_component_impl!($ident); + }; +} + +impl_effect!(Speed); +impl_effect!(Slowness); +impl_effect!(Haste); +impl_effect!(MiningFatigue); +impl_effect!(Strength); +impl_effect!(InstantHealth); +impl_effect!(InstantDamage); +impl_effect!(JumpBoost); +impl_effect!(Nausea); +impl_effect!(Regeneration); +impl_effect!(Resistance); +impl_effect!(FireResistance); +impl_effect!(WaterBreathing); +impl_effect!(Invisibility); +impl_effect!(Blindness); +impl_effect!(NightVision); +impl_effect!(Hunger); +impl_effect!(Weakness); +impl_effect!(Poison); +impl_effect!(WitherEffect); +impl_effect!(HealthBoost); +impl_effect!(Absorption); +impl_effect!(Saturation); +impl_effect!(Glowing); +impl_effect!(Levitation); +impl_effect!(Luck); +impl_effect!(BadLuck); +impl_effect!(SlowFalling); +impl_effect!(ConduitPower); +impl_effect!(DolphinsGrace); +impl_effect!(BadOmen); +impl_effect!(HeroOfTheVillage); + /// Wheather an entity is sneaking, like in pressing shift. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct Sneaking(pub bool);