From c83ebbb56cc2cf1d856527554f2260140f2f08d3 Mon Sep 17 00:00:00 2001 From: Yui Tanabe Date: Fri, 4 Jun 2021 07:27:35 +0900 Subject: [PATCH 1/8] Implementing Potion Effects --- Cargo.lock | 8 +++ Cargo.toml | 1 + feather/common/src/entities/player.rs | 3 +- libcraft/effects/Cargo.toml | 10 ++++ libcraft/effects/src/lib.rs | 84 +++++++++++++++++++++++++++ quill/common/Cargo.toml | 1 + quill/common/src/component.rs | 2 +- quill/common/src/components.rs | 12 ++++ 8 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 libcraft/effects/Cargo.toml create mode 100644 libcraft/effects/src/lib.rs 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/common/src/entities/player.rs b/feather/common/src/entities/player.rs index 49ef576c8..48c54eb7f 100644 --- a/feather/common/src/entities/player.rs +++ b/feather/common/src/entities/player.rs @@ -1,13 +1,14 @@ use anyhow::bail; use base::EntityKind; use ecs::{EntityBuilder, SysResult}; -use quill_common::{components::CreativeFlying, entities::Player}; +use quill_common::{components::{CreativeFlying, PotionEffects}, entities::Player}; pub fn build_default(builder: &mut EntityBuilder) { super::build_default(builder); builder .add(Player) .add(CreativeFlying(false)) + .add(PotionEffects(vec![])) .add(EntityKind::Player); } 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..fca69fcfe --- /dev/null +++ b/libcraft/effects/src/lib.rs @@ -0,0 +1,84 @@ +use std::convert::TryFrom; +use serde::{Serialize, Deserialize}; + +#[derive(Clone, Debug, Hash, 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 25342584d..25f997956 100644 --- a/quill/common/src/component.rs +++ b/quill/common/src/component.rs @@ -186,7 +186,7 @@ host_component_enum! { BlockInteractEvent = 1008, CreativeFlying = 1009, CreativeFlyingEvent = 1010, - + PotionEffects = 1011, } } diff --git a/quill/common/src/components.rs b/quill/common/src/components.rs index b145d594f..a01c395ec 100644 --- a/quill/common/src/components.rs +++ b/quill/common/src/components.rs @@ -104,3 +104,15 @@ impl Display for CustomName { pub struct CreativeFlying(pub bool); bincode_component_impl!(CreativeFlying); +type EPotionEffect = libcraft_effects::PotionEffect; +#[derive(Clone, Debug, Hash, Serialize, Deserialize)] +pub struct PotionEffect { + effect: EPotionEffect, + amplifier: u8, + duration: u32, + particle: bool, + icon: bool, // show effect icon. +} +#[derive(Clone, Debug, Hash, Serialize, Deserialize)] +pub struct PotionEffects(pub Vec); +bincode_component_impl!(PotionEffects); From d598cb1ea5580933800b5b0c3cf1ca1f806c1eb5 Mon Sep 17 00:00:00 2001 From: Yui Tanabe Date: Fri, 4 Jun 2021 07:35:26 +0900 Subject: [PATCH 2/8] cargo fmt --- feather/common/src/entities/player.rs | 5 ++++- libcraft/effects/src/lib.rs | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/feather/common/src/entities/player.rs b/feather/common/src/entities/player.rs index 48c54eb7f..dae025a57 100644 --- a/feather/common/src/entities/player.rs +++ b/feather/common/src/entities/player.rs @@ -1,7 +1,10 @@ use anyhow::bail; use base::EntityKind; use ecs::{EntityBuilder, SysResult}; -use quill_common::{components::{CreativeFlying, PotionEffects}, entities::Player}; +use quill_common::{ + components::{CreativeFlying, PotionEffects}, + entities::Player, +}; pub fn build_default(builder: &mut EntityBuilder) { super::build_default(builder); diff --git a/libcraft/effects/src/lib.rs b/libcraft/effects/src/lib.rs index fca69fcfe..46f87f875 100644 --- a/libcraft/effects/src/lib.rs +++ b/libcraft/effects/src/lib.rs @@ -1,5 +1,5 @@ +use serde::{Deserialize, Serialize}; use std::convert::TryFrom; -use serde::{Serialize, Deserialize}; #[derive(Clone, Debug, Hash, Serialize, Deserialize)] pub enum PotionEffect { From d1ad164f39d23c321f8f64644046ef54ea139887 Mon Sep 17 00:00:00 2001 From: Yui Tanabe Date: Fri, 4 Jun 2021 07:56:33 +0900 Subject: [PATCH 3/8] cargo fmt --- quill/common/src/components.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/quill/common/src/components.rs b/quill/common/src/components.rs index aa1a97b71..66ee5ea21 100644 --- a/quill/common/src/components.rs +++ b/quill/common/src/components.rs @@ -118,9 +118,7 @@ pub struct PotionEffect { pub struct PotionEffects(pub Vec); bincode_component_impl!(PotionEffects); - /// Wheather an entity is sneaking, like in pressing shift. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct Sneaking(pub bool); bincode_component_impl!(Sneaking); - From 065fee61600701956800b192f3cf5ad6ed9c5bc2 Mon Sep 17 00:00:00 2001 From: Yui Tanabe Date: Fri, 4 Jun 2021 18:21:54 +0900 Subject: [PATCH 4/8] Change Vec to HashSet --- feather/common/src/entities/player.rs | 1 - feather/plugin-host/src/host_calls.rs | 1 + quill/common/src/components.rs | 3 ++- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/feather/common/src/entities/player.rs b/feather/common/src/entities/player.rs index 7e921a943..275283c35 100644 --- a/feather/common/src/entities/player.rs +++ b/feather/common/src/entities/player.rs @@ -11,7 +11,6 @@ pub fn build_default(builder: &mut EntityBuilder) { builder .add(Player) .add(CreativeFlying(false)) - .add(PotionEffects(vec![])) .add(Sneaking(false)) .add(EntityKind::Player); } diff --git a/feather/plugin-host/src/host_calls.rs b/feather/plugin-host/src/host_calls.rs index bf90ccb81..c35d98223 100644 --- a/feather/plugin-host/src/host_calls.rs +++ b/feather/plugin-host/src/host_calls.rs @@ -15,6 +15,7 @@ mod entity_builder; mod plugin_message; mod query; mod system; +mod effects; macro_rules! host_calls { ( diff --git a/quill/common/src/components.rs b/quill/common/src/components.rs index 66ee5ea21..f31b8f63e 100644 --- a/quill/common/src/components.rs +++ b/quill/common/src/components.rs @@ -10,6 +10,7 @@ use std::{ use serde::{Deserialize, Serialize}; use smartstring::{LazyCompact, SmartString}; +use std::collections::HashSet; /// Whether an entity is touching the ground. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] @@ -115,7 +116,7 @@ pub struct PotionEffect { icon: bool, // show effect icon. } #[derive(Clone, Debug, Hash, Serialize, Deserialize)] -pub struct PotionEffects(pub Vec); +pub struct PotionEffects(pub HashSet); bincode_component_impl!(PotionEffects); /// Wheather an entity is sneaking, like in pressing shift. From 74aa42b4b09e10336df9800113879ffccdf33848 Mon Sep 17 00:00:00 2001 From: Yui Tanabe Date: Sat, 5 Jun 2021 00:05:21 +0900 Subject: [PATCH 5/8] Implementing Potion Effect (Plugin Host Call) --- feather/common/src/entities/player.rs | 2 +- feather/plugin-host/src/host_calls.rs | 2 +- libcraft/effects/src/lib.rs | 2 +- quill/common/src/components.rs | 12 +++++++----- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/feather/common/src/entities/player.rs b/feather/common/src/entities/player.rs index 275283c35..4f62d4a48 100644 --- a/feather/common/src/entities/player.rs +++ b/feather/common/src/entities/player.rs @@ -2,7 +2,7 @@ use anyhow::bail; use base::EntityKind; use ecs::{EntityBuilder, SysResult}; use quill_common::{ - components::{CreativeFlying, PotionEffects, Sneaking}, + components::{CreativeFlying, Sneaking}, entities::Player, }; diff --git a/feather/plugin-host/src/host_calls.rs b/feather/plugin-host/src/host_calls.rs index c35d98223..19582f934 100644 --- a/feather/plugin-host/src/host_calls.rs +++ b/feather/plugin-host/src/host_calls.rs @@ -10,12 +10,12 @@ use crate::host_function::{NativeHostFunction, WasmHostFunction}; mod block; mod component; +mod effects; mod entity; mod entity_builder; mod plugin_message; mod query; mod system; -mod effects; macro_rules! host_calls { ( diff --git a/libcraft/effects/src/lib.rs b/libcraft/effects/src/lib.rs index 46f87f875..d1165f90a 100644 --- a/libcraft/effects/src/lib.rs +++ b/libcraft/effects/src/lib.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use std::convert::TryFrom; -#[derive(Clone, Debug, Hash, Serialize, Deserialize)] +#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)] pub enum PotionEffect { None = 0, Speed = 1, diff --git a/quill/common/src/components.rs b/quill/common/src/components.rs index f31b8f63e..2751c1012 100644 --- a/quill/common/src/components.rs +++ b/quill/common/src/components.rs @@ -106,16 +106,18 @@ pub struct CreativeFlying(pub bool); bincode_component_impl!(CreativeFlying); -type EPotionEffect = libcraft_effects::PotionEffect; -#[derive(Clone, Debug, Hash, Serialize, Deserialize)] +type EffectKind = libcraft_effects::PotionEffect; +#[derive(Clone, Debug, Hash, Serialize, PartialEq, Eq, Deserialize)] pub struct PotionEffect { - effect: EPotionEffect, + effect: EffectKind, amplifier: u8, duration: u32, particle: bool, - icon: bool, // show effect icon. + ambient: bool, // given from beacon or not. + icon: bool, // show effect icon. } -#[derive(Clone, Debug, Hash, Serialize, Deserialize)] + +#[derive(Clone, Debug, Serialize, PartialEq, Eq, Deserialize)] pub struct PotionEffects(pub HashSet); bincode_component_impl!(PotionEffects); From c19dc69ef43ba8fd6e893eae7e2dbc6db25f3abe Mon Sep 17 00:00:00 2001 From: Yui Tanabe Date: Mon, 7 Jun 2021 07:13:52 +0900 Subject: [PATCH 6/8] Register each effects as component --- feather/plugin-host/src/host_calls.rs | 1 - quill/common/src/component.rs | 36 +++++++++++- quill/common/src/components.rs | 80 ++++++++++++++++++++++----- 3 files changed, 102 insertions(+), 15 deletions(-) diff --git a/feather/plugin-host/src/host_calls.rs b/feather/plugin-host/src/host_calls.rs index 19582f934..bf90ccb81 100644 --- a/feather/plugin-host/src/host_calls.rs +++ b/feather/plugin-host/src/host_calls.rs @@ -10,7 +10,6 @@ use crate::host_function::{NativeHostFunction, WasmHostFunction}; mod block; mod component; -mod effects; mod entity; mod entity_builder; mod plugin_message; diff --git a/quill/common/src/component.rs b/quill/common/src/component.rs index 42e3fc8ef..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, @@ -188,7 +222,7 @@ host_component_enum! { CreativeFlyingEvent = 1010, Sneaking = 1011, SneakEvent = 1012, - PotionEffects = 1013, + } } diff --git a/quill/common/src/components.rs b/quill/common/src/components.rs index 2751c1012..1e2ed8746 100644 --- a/quill/common/src/components.rs +++ b/quill/common/src/components.rs @@ -10,7 +10,8 @@ use std::{ use serde::{Deserialize, Serialize}; use smartstring::{LazyCompact, SmartString}; -use std::collections::HashSet; +use std::cmp::Ordering; +use std::collections::BTreeSet; /// Whether an entity is touching the ground. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] @@ -106,20 +107,73 @@ pub struct CreativeFlying(pub bool); bincode_component_impl!(CreativeFlying); -type EffectKind = libcraft_effects::PotionEffect; -#[derive(Clone, Debug, Hash, Serialize, PartialEq, Eq, Deserialize)] -pub struct PotionEffect { - effect: EffectKind, - amplifier: u8, - duration: u32, - particle: bool, - ambient: bool, // given from beacon or not. - icon: bool, // show effect icon. +#[derive(Copy, Clone, Debug, Hash, Serialize, PartialEq, Eq, Deserialize)] +pub struct PotionApplication { + pub amplifier: u8, + pub duration: u32, + pub particle: bool, + pub ambient: bool, // given from beacon or not. + pub icon: bool, // show effect icon. } -#[derive(Clone, Debug, Serialize, PartialEq, Eq, Deserialize)] -pub struct PotionEffects(pub HashSet); -bincode_component_impl!(PotionEffects); +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)] From 777e662eb70274afcda1fc2c75d9d12088e796b9 Mon Sep 17 00:00:00 2001 From: Yui Tanabe Date: Tue, 8 Jun 2021 00:20:53 +0900 Subject: [PATCH 7/8] Added doc comment for the struct PotionApplication --- quill/common/src/components.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/quill/common/src/components.rs b/quill/common/src/components.rs index 1e2ed8746..9da04db6d 100644 --- a/quill/common/src/components.rs +++ b/quill/common/src/components.rs @@ -108,12 +108,19 @@ 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, - pub ambient: bool, // given from beacon or not. - pub icon: bool, // show effect icon. + /// 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 { From 97c82b5cad20916e2cabb39d420c2d0ec775cc18 Mon Sep 17 00:00:00 2001 From: Yui Tanabe Date: Tue, 8 Jun 2021 22:19:24 +0900 Subject: [PATCH 8/8] removed dumb things --- feather/plugin-host/src/host_calls.rs | 1 + 1 file changed, 1 insertion(+) 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),* $(,)?