2020-08-10 22:54:45 +00:00
|
|
|
use crate::sync::Uid;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use specs::{Component, FlaggedStorage};
|
|
|
|
use specs_idvs::IdvStorage;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2020-10-19 03:00:35 +00:00
|
|
|
/// De/buff Kind.
|
|
|
|
/// This is used to determine what effects a buff will have, as well as
|
|
|
|
/// determine the strength and duration of the buff effects using the internal
|
|
|
|
/// values
|
2020-10-02 17:15:10 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)]
|
2020-10-19 03:00:35 +00:00
|
|
|
pub enum BuffKind {
|
2020-08-10 22:54:45 +00:00
|
|
|
/// Restores health/time for some period
|
2020-10-04 00:35:25 +00:00
|
|
|
Regeneration {
|
|
|
|
strength: f32,
|
|
|
|
duration: Option<Duration>,
|
|
|
|
},
|
2020-10-01 00:40:46 +00:00
|
|
|
/// Lowers health over time for some duration
|
2020-10-04 00:35:25 +00:00
|
|
|
Bleeding {
|
|
|
|
strength: f32,
|
|
|
|
duration: Option<Duration>,
|
|
|
|
},
|
2020-10-02 17:15:10 +00:00
|
|
|
/// Prefixes an entity's name with "Cursed"
|
2020-10-01 17:33:35 +00:00
|
|
|
/// Currently placeholder buff to show other stuff is possible
|
2020-10-04 00:35:25 +00:00
|
|
|
Cursed { duration: Option<Duration> },
|
2020-08-10 22:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// De/buff category ID.
|
2020-10-19 03:00:35 +00:00
|
|
|
/// Similar to `BuffKind`, but to mark a category (for more generic usage, like
|
2020-08-10 22:54:45 +00:00
|
|
|
/// positive/negative buffs).
|
2020-10-02 19:09:19 +00:00
|
|
|
#[derive(Clone, Copy, Eq, PartialEq, Debug, Serialize, Deserialize)]
|
2020-08-10 22:54:45 +00:00
|
|
|
pub enum BuffCategoryId {
|
2020-10-19 03:00:35 +00:00
|
|
|
// Buff and debuff get added in builder function based off of the buff kind
|
|
|
|
Debuff,
|
|
|
|
Buff,
|
2020-08-10 22:54:45 +00:00
|
|
|
Natural,
|
2020-10-01 00:40:46 +00:00
|
|
|
Physical,
|
2020-08-10 22:54:45 +00:00
|
|
|
Magical,
|
|
|
|
Divine,
|
2020-10-13 00:48:25 +00:00
|
|
|
PersistOnDeath,
|
2020-08-10 22:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Data indicating and configuring behaviour of a de/buff.
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2020-10-01 00:40:46 +00:00
|
|
|
pub enum BuffEffect {
|
|
|
|
/// Periodically damages or heals entity
|
2020-10-01 01:35:57 +00:00
|
|
|
HealthChangeOverTime { rate: f32, accumulated: f32 },
|
2020-08-10 22:54:45 +00:00
|
|
|
/// Changes name on_add/on_remove
|
|
|
|
NameChange { prefix: String },
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Actual de/buff.
|
|
|
|
/// Buff can timeout after some time if `time` is Some. If `time` is None,
|
|
|
|
/// Buff will last indefinitely, until removed manually (by some action, like
|
2020-10-19 03:00:35 +00:00
|
|
|
/// uncursing).
|
2020-08-10 22:54:45 +00:00
|
|
|
///
|
2020-10-19 03:00:35 +00:00
|
|
|
/// Buff has a kind, which is used to determine the effects in a builder
|
|
|
|
/// function.
|
2020-08-10 22:54:45 +00:00
|
|
|
///
|
|
|
|
/// To provide more classification info when needed,
|
|
|
|
/// buff can be in one or more buff category.
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Buff {
|
2020-10-19 03:00:35 +00:00
|
|
|
pub kind: BuffKind,
|
2020-08-10 22:54:45 +00:00
|
|
|
pub cat_ids: Vec<BuffCategoryId>,
|
|
|
|
pub time: Option<Duration>,
|
2020-10-01 00:40:46 +00:00
|
|
|
pub effects: Vec<BuffEffect>,
|
2020-10-03 18:48:56 +00:00
|
|
|
pub source: BuffSource,
|
2020-08-10 22:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Information about whether buff addition or removal was requested.
|
|
|
|
/// This to implement "on_add" and "on_remove" hooks for constant buffs.
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum BuffChange {
|
|
|
|
/// Adds this buff.
|
|
|
|
Add(Buff),
|
|
|
|
/// Removes all buffs with this ID.
|
2020-10-19 03:00:35 +00:00
|
|
|
RemoveByKind(BuffKind),
|
|
|
|
/// Removes all buffs with this ID, but not debuffs.
|
|
|
|
RemoveFromClient(BuffKind),
|
2020-10-02 19:09:19 +00:00
|
|
|
/// Removes buffs of these indices (first vec is for active buffs, second is
|
2020-10-19 03:00:35 +00:00
|
|
|
/// for inactive buffs), should only be called when buffs expire
|
|
|
|
RemoveExpiredByIndex(Vec<usize>, Vec<usize>),
|
2020-10-02 19:09:19 +00:00
|
|
|
/// Removes buffs of these categories (first vec is of categories of which
|
|
|
|
/// all are required, second vec is of categories of which at least one is
|
2020-10-13 00:48:25 +00:00
|
|
|
/// required, third vec is of categories that will not be removed)
|
|
|
|
RemoveByCategory {
|
|
|
|
required: Vec<BuffCategoryId>,
|
|
|
|
optional: Vec<BuffCategoryId>,
|
|
|
|
blacklisted: Vec<BuffCategoryId>,
|
|
|
|
},
|
2020-08-10 22:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Source of the de/buff
|
|
|
|
#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum BuffSource {
|
|
|
|
/// Applied by a character
|
|
|
|
Character { by: Uid },
|
|
|
|
/// Applied by world, like a poisonous fumes from a swamp
|
|
|
|
World,
|
|
|
|
/// Applied by command
|
|
|
|
Command,
|
|
|
|
/// Applied by an item
|
|
|
|
Item,
|
|
|
|
/// Applied by another buff (like an after-effect)
|
|
|
|
Buff,
|
|
|
|
/// Some other source
|
|
|
|
Unknown,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Component holding all de/buffs that gets resolved each tick.
|
|
|
|
/// On each tick, remaining time of buffs get lowered and
|
2020-10-01 00:40:46 +00:00
|
|
|
/// buff effect of each buff is applied or not, depending on the `BuffEffect`
|
2020-10-01 01:35:57 +00:00
|
|
|
/// (specs system will decide based on `BuffEffect`, to simplify
|
|
|
|
/// implementation). TODO: Something like `once` flag for `Buff` to remove the
|
|
|
|
/// dependence on `BuffEffect` enum?
|
2020-08-10 22:54:45 +00:00
|
|
|
///
|
|
|
|
/// In case of one-time buffs, buff effects will be applied on addition
|
|
|
|
/// and undone on removal of the buff (by the specs system).
|
|
|
|
/// Example could be decreasing max health, which, if repeated each tick,
|
|
|
|
/// would be probably an undesired effect).
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
|
|
|
|
pub struct Buffs {
|
|
|
|
/// Active de/buffs.
|
2020-10-02 17:15:10 +00:00
|
|
|
pub active_buffs: Vec<Buff>,
|
|
|
|
/// Inactive de/buffs (used so that only 1 buff of a particular type is
|
|
|
|
/// active at any time)
|
|
|
|
pub inactive_buffs: Vec<Buff>,
|
2020-08-10 22:54:45 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 17:33:35 +00:00
|
|
|
impl Buff {
|
2020-10-19 03:00:35 +00:00
|
|
|
/// Builder function for buffs
|
|
|
|
pub fn new(kind: BuffKind, cat_ids: Vec<BuffCategoryId>, source: BuffSource) -> Self {
|
|
|
|
let mut cat_ids = cat_ids;
|
|
|
|
let (effects, time) = match kind {
|
|
|
|
BuffKind::Bleeding { strength, duration } => {
|
|
|
|
cat_ids.push(BuffCategoryId::Debuff);
|
|
|
|
(
|
|
|
|
vec![BuffEffect::HealthChangeOverTime {
|
|
|
|
rate: -strength,
|
|
|
|
accumulated: 0.0,
|
|
|
|
}],
|
|
|
|
duration,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
BuffKind::Regeneration { strength, duration } => {
|
|
|
|
cat_ids.push(BuffCategoryId::Buff);
|
|
|
|
(
|
|
|
|
vec![BuffEffect::HealthChangeOverTime {
|
|
|
|
rate: strength,
|
|
|
|
accumulated: 0.0,
|
|
|
|
}],
|
|
|
|
duration,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
BuffKind::Cursed { duration } => {
|
|
|
|
cat_ids.push(BuffCategoryId::Debuff);
|
|
|
|
(
|
|
|
|
vec![BuffEffect::NameChange {
|
|
|
|
prefix: String::from("Cursed "),
|
|
|
|
}],
|
|
|
|
duration,
|
|
|
|
)
|
|
|
|
},
|
2020-10-01 17:33:35 +00:00
|
|
|
};
|
2020-10-13 00:48:25 +00:00
|
|
|
assert_eq!(
|
|
|
|
cat_ids
|
|
|
|
.iter()
|
|
|
|
.any(|cat| *cat == BuffCategoryId::Buff || *cat == BuffCategoryId::Debuff),
|
|
|
|
true,
|
|
|
|
"Buff must have either buff or debuff category."
|
|
|
|
);
|
2020-10-01 17:33:35 +00:00
|
|
|
Buff {
|
2020-10-19 03:00:35 +00:00
|
|
|
kind,
|
2020-10-01 17:33:35 +00:00
|
|
|
cat_ids,
|
|
|
|
time,
|
|
|
|
effects,
|
2020-10-03 18:48:56 +00:00
|
|
|
source,
|
2020-10-01 17:33:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 22:54:45 +00:00
|
|
|
impl Component for Buffs {
|
|
|
|
type Storage = FlaggedStorage<Self, IdvStorage<Self>>;
|
|
|
|
}
|