Changed buffs from using hash maps to an enum map and a slot map.

This commit is contained in:
Sam 2023-11-12 12:37:28 -05:00
parent d7b07ee250
commit 3f19c61426
12 changed files with 137 additions and 112 deletions

View File

@ -5,58 +5,65 @@ ItemDef(
kind: Drink,
effects: Any([
Buff((
kind: Polymorphed(QuadrupedSmall(( species: Frog, body_type: Female ))),
kind: Polymorphed,
data: (
strength: 0.0,
duration: Some(60),
misc_data: Some(Body(QuadrupedSmall(( species: Frog, body_type: Female )))),
),
cat_ids: [Natural],
)),
Buff((
kind: Polymorphed(QuadrupedSmall(( species: Rabbit, body_type: Female ))),
kind: Polymorphed,
data: (
strength: 0.0,
duration: Some(60),
misc_data: Some(Body(QuadrupedSmall(( species: Rabbit, body_type: Female )))),
),
cat_ids: [Natural],
)),
Buff((
kind: Polymorphed(QuadrupedSmall(( species: Rat, body_type: Female ))),
kind: Polymorphed,
data: (
strength: 0.0,
duration: Some(60),
misc_data: Some(Body(QuadrupedSmall(( species: Rat, body_type: Female )))),
),
cat_ids: [Natural],
)),
Buff((
kind: Polymorphed(QuadrupedSmall(( species: Squirrel, body_type: Female ))),
kind: Polymorphed,
data: (
strength: 0.0,
duration: Some(60),
misc_data: Some(Body(QuadrupedSmall(( species: Squirrel, body_type: Female )))),
),
cat_ids: [Natural],
)),
Buff((
kind: Polymorphed(QuadrupedSmall(( species: Cat, body_type: Female ))),
kind: Polymorphed,
data: (
strength: 0.0,
duration: Some(60),
misc_data: Some(Body(QuadrupedSmall(( species: Cat, body_type: Female )))),
),
cat_ids: [Natural],
)),
Buff((
kind: Polymorphed(QuadrupedSmall(( species: Fungome, body_type: Female ))),
kind: Polymorphed,
data: (
strength: 0.0,
duration: Some(60),
misc_data: Some(Body(QuadrupedSmall(( species: Fungome, body_type: Female )))),
),
cat_ids: [Natural],
)),
Buff((
kind: Polymorphed(QuadrupedSmall(( species: Pig, body_type: Female ))),
kind: Polymorphed,
data: (
strength: 0.0,
duration: Some(60),
misc_data: Some(Body(QuadrupedSmall(( species: Pig, body_type: Female )))),
),
cat_ids: [Natural],
)),

View File

@ -172,7 +172,7 @@ lazy_static! {
BuffKind::Parried => "parried",
BuffKind::PotionSickness => "potion_sickness",
BuffKind::Reckless => "reckless",
BuffKind::Polymorphed(_) => "polymorphed",
BuffKind::Polymorphed => "polymorphed",
BuffKind::Flame => "flame",
BuffKind::Frigid => "frigid",
BuffKind::Lifesteal => "lifesteal",

View File

@ -10,18 +10,21 @@ use crate::{
};
use core::cmp::Ordering;
use hashbrown::HashMap;
use enum_map::{Enum, EnumMap};
use itertools::Either;
use serde::{Deserialize, Serialize};
use slotmap::{new_key_type, SlotMap};
use specs::{Component, DerefFlaggedStorage, VecStorage};
use strum::EnumIter;
use super::Body;
new_key_type! { pub struct BuffKey; }
/// De/buff Kind.
/// This is used to determine what effects a buff will have
#[derive(
Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize, PartialOrd, Ord, EnumIter,
Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize, PartialOrd, Ord, EnumIter, Enum,
)]
pub enum BuffKind {
// Buffs
@ -157,7 +160,7 @@ pub enum BuffKind {
/// Decreases the health gained from subsequent potions.
PotionSickness,
/// Changed into another body.
Polymorphed(Body),
Polymorphed,
}
impl BuffKind {
@ -197,7 +200,7 @@ impl BuffKind {
| BuffKind::Poisoned
| BuffKind::Parried
| BuffKind::PotionSickness
| BuffKind::Polymorphed(_) => false,
| BuffKind::Polymorphed => false,
}
}
@ -348,7 +351,13 @@ impl BuffKind {
BuffEffect::DamageReduction(-data.strength),
BuffEffect::AttackDamage(1.0 + data.strength),
],
BuffKind::Polymorphed(body) => vec![BuffEffect::BodyChange(*body)],
BuffKind::Polymorphed => {
let mut effects = Vec::new();
if let Some(MiscBuffData::Body(body)) = data.misc_data {
effects.push(BuffEffect::BodyChange(body));
}
effects
},
BuffKind::Flame => vec![BuffEffect::AttackEffect(AttackEffect::new(
None,
CombatEffect::Buff(CombatBuff {
@ -433,6 +442,13 @@ pub struct BuffData {
pub force_immediate: bool,
/// Used for buffs that have rider buffs (e.g. Flame, Frigid)
pub secondary_duration: Option<Secs>,
/// Used to add random data to buffs if needed (e.g. polymorphed)
pub misc_data: Option<MiscBuffData>,
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub enum MiscBuffData {
Body(Body),
}
impl BuffData {
@ -443,6 +459,7 @@ impl BuffData {
force_immediate: false,
delay: None,
secondary_duration: None,
misc_data: None,
}
}
@ -461,6 +478,11 @@ impl BuffData {
self.force_immediate = force_immediate;
self
}
pub fn with_misc_data(mut self, misc_data: MiscBuffData) -> Self {
self.misc_data = Some(misc_data);
self
}
}
/// De/buff category ID.
@ -588,9 +610,8 @@ pub enum BuffChange {
RemoveByKind(BuffKind),
/// Removes all buffs with this ID, but not debuffs.
RemoveFromController(BuffKind),
/// Removes buffs of these indices (first vec is for active buffs, second is
/// for inactive buffs), should only be called when buffs expire
RemoveById(Vec<BuffId>),
/// Removes buffs of these indices, should only be called when buffs expire
RemoveByKey(Vec<BuffKey>),
/// 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
/// required, third vec is of categories that will not be removed)
@ -704,93 +725,88 @@ pub enum BuffSource {
/// would be probably an undesired effect).
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct Buffs {
/// Uid used for synchronization
id_counter: u64,
/// Maps Kinds of buff to Id's of currently applied buffs of that kind and
/// Maps kinds of buff to currently applied buffs of that kind and
/// the time that the first buff was added (time gets reset if entity no
/// longer has buffs of that kind)
pub kinds: HashMap<BuffKind, (Vec<BuffId>, Time)>,
// All currently applied buffs stored by Id
pub buffs: HashMap<BuffId, Buff>,
pub kinds: EnumMap<BuffKind, Option<(Vec<BuffKey>, Time)>>,
// All buffs currently present on an entity
pub buffs: SlotMap<BuffKey, Buff>,
}
impl Buffs {
fn sort_kind(&mut self, kind: BuffKind) {
if let Some(buff_order) = self.kinds.get_mut(&kind) {
if let Some(buff_order) = self.kinds[kind].as_mut() {
if buff_order.0.is_empty() {
self.kinds.remove(&kind);
self.kinds[kind] = None;
} else {
let buffs = &self.buffs;
// Intentionally sorted in reverse so that the strongest buffs are earlier in
// the vector
buff_order
.0
.sort_by(|a, b| buffs[b].partial_cmp(&buffs[a]).unwrap_or(Ordering::Equal));
.sort_by(|a, b| buffs[*b].partial_cmp(&buffs[*a]).unwrap_or(Ordering::Equal));
}
}
}
pub fn remove_kind(&mut self, kind: BuffKind) {
if let Some(buff_ids) = self.kinds.get_mut(&kind) {
for id in &buff_ids.0 {
self.buffs.remove(id);
if let Some((buff_keys, _)) = self.kinds[kind].as_ref() {
for key in buff_keys {
self.buffs.remove(*key);
}
self.kinds.remove(&kind);
self.kinds[kind] = None;
}
}
fn force_insert(&mut self, id: BuffId, buff: Buff, current_time: Time) -> BuffId {
pub fn insert(&mut self, buff: Buff, current_time: Time) -> BuffKey {
let kind = buff.kind;
self.kinds
.entry(kind)
.or_insert((Vec::new(), current_time))
let key = self.buffs.insert(buff);
self.kinds[kind]
.get_or_insert_with(|| (Vec::new(), current_time))
.0
.push(id);
self.buffs.insert(id, buff);
.push(key);
self.sort_kind(kind);
if kind.queues() {
self.delay_queueable_buffs(kind, current_time);
}
id
key
}
pub fn insert(&mut self, buff: Buff, current_time: Time) -> BuffId {
self.id_counter += 1;
self.force_insert(self.id_counter, buff, current_time)
}
pub fn contains(&self, kind: BuffKind) -> bool { self.kinds.contains_key(&kind) }
pub fn contains(&self, kind: BuffKind) -> bool { self.kinds[kind].is_some() }
// Iterate through buffs of a given kind in effect order (most powerful first)
pub fn iter_kind(&self, kind: BuffKind) -> impl Iterator<Item = (BuffId, &Buff)> + '_ {
self.kinds
.get(&kind)
.map(|ids| ids.0.iter())
pub fn iter_kind(&self, kind: BuffKind) -> impl Iterator<Item = (BuffKey, &Buff)> + '_ {
self.kinds[kind]
.as_ref()
.map(|keys| keys.0.iter())
.unwrap_or_else(|| [].iter())
.map(move |id| (*id, &self.buffs[id]))
.map(move |&key| (key, &self.buffs[key]))
}
// Iterates through all active buffs (the most powerful buff of each
// non-stacking kind, and all of the stacking ones)
pub fn iter_active(&self) -> impl Iterator<Item = impl Iterator<Item = &Buff>> + '_ {
self.kinds.iter().map(move |(kind, ids)| {
self.kinds
.iter()
.filter_map(|(kind, keys)| keys.as_ref().map(|keys| (kind, keys)))
.map(move |(kind, keys)| {
if kind.stacks() {
// Iterate stackable buffs in reverse order to show the timer of the soonest one
// to expire
Either::Left(ids.0.iter().filter_map(|id| self.buffs.get(id)).rev())
Either::Left(keys.0.iter().filter_map(|key| self.buffs.get(*key)).rev())
} else {
Either::Right(self.buffs.get(&ids.0[0]).into_iter())
Either::Right(self.buffs.get(keys.0[0]).into_iter())
}
})
}
// Gets most powerful buff of a given kind
pub fn remove(&mut self, buff_id: BuffId) {
if let Some(kind) = self.buffs.remove(&buff_id) {
let kind = kind.kind;
self.kinds
.get_mut(&kind)
.map(|ids| ids.0.retain(|id| *id != buff_id));
pub fn remove(&mut self, buff_key: BuffKey) {
if let Some(buff) = self.buffs.remove(buff_key) {
let kind = buff.kind;
self.kinds[kind]
.as_mut()
.map(|keys| keys.0.retain(|key| *key != buff_key));
self.sort_kind(kind);
}
}
@ -798,9 +814,9 @@ impl Buffs {
fn delay_queueable_buffs(&mut self, kind: BuffKind, current_time: Time) {
let mut next_start_time: Option<Time> = None;
debug_assert!(kind.queues());
if let Some(buffs) = self.kinds.get(&kind) {
buffs.0.iter().for_each(|id| {
if let Some(buff) = self.buffs.get_mut(id) {
if let Some(buffs) = self.kinds[kind].as_mut() {
buffs.0.iter().for_each(|key| {
if let Some(buff) = self.buffs.get_mut(*key) {
// End time only being updated when there is some next_start_time will
// technically cause buffs to "end early" if they have a weaker strength than a
// buff with an infinite duration, but this is fine since those buffs wouldn't
@ -826,8 +842,6 @@ impl Buffs {
}
}
pub type BuffId = u64;
impl Component for Buffs {
type Storage = DerefFlaggedStorage<Self, VecStorage<Self>>;
}

View File

@ -57,7 +57,7 @@ pub use self::{
quadruped_small, ship, theropod, AllBodies, Body, BodyData,
},
buff::{
Buff, BuffCategory, BuffChange, BuffData, BuffEffect, BuffId, BuffKind, BuffSource, Buffs,
Buff, BuffCategory, BuffChange, BuffData, BuffEffect, BuffKey, BuffKind, BuffSource, Buffs,
ModifierKind,
},
character_state::{CharacterActivity, CharacterState, StateUpdate},

View File

@ -4,7 +4,7 @@ use common::{
aura::Auras,
body::{object, Body},
buff::{
Buff, BuffCategory, BuffChange, BuffData, BuffEffect, BuffId, BuffKind, BuffSource,
Buff, BuffCategory, BuffChange, BuffData, BuffEffect, BuffKey, BuffKind, BuffSource,
Buffs,
},
fluid_dynamics::{Fluid, LiquidKind},
@ -281,7 +281,7 @@ impl<'a> System<'a> for Sys {
kind: LiquidKind::Water,
..
})
) && buff_comp.kinds.contains_key(&BuffKind::Burning)
) && buff_comp.kinds[BuffKind::Burning].is_some()
{
// If in water fluid and currently burning, remove burning debuffs
server_emitter.emit(ServerEvent::Buff {
@ -291,14 +291,14 @@ impl<'a> System<'a> for Sys {
}
}
let mut expired_buffs = Vec::<BuffId>::new();
let mut expired_buffs = Vec::<BuffKey>::new();
// Replace buffs from an active aura with a normal buff when out of range of the
// aura
buff_comp
.buffs
.iter()
.filter_map(|(id, buff)| {
.filter_map(|(buff_key, buff)| {
if let Some((uid, aura_key)) = buff.cat_ids.iter().find_map(|cat_id| {
if let BuffCategory::FromActiveAura(uid, aura_key) = cat_id {
Some((uid, aura_key))
@ -306,12 +306,12 @@ impl<'a> System<'a> for Sys {
None
}
}) {
Some((id, buff, uid, aura_key))
Some((buff_key, buff, uid, aura_key))
} else {
None
}
})
.for_each(|(buff_id, buff, uid, aura_key)| {
.for_each(|(buff_key, buff, uid, aura_key)| {
let replace = if let Some(aura_entity) = read_data.id_maps.uid_entity(*uid) {
if let Some(aura) = read_data
.auras
@ -333,7 +333,7 @@ impl<'a> System<'a> for Sys {
true
};
if replace {
expired_buffs.push(*buff_id);
expired_buffs.push(buff_key);
server_emitter.emit(ServerEvent::Buff {
entity,
buff_change: BuffChange::Add(Buff::new(
@ -355,9 +355,9 @@ impl<'a> System<'a> for Sys {
}
});
buff_comp.buffs.iter().for_each(|(id, buff)| {
buff_comp.buffs.iter().for_each(|(buff_key, buff)| {
if buff.end_time.map_or(false, |end| end.0 < read_data.time.0) {
expired_buffs.push(*id)
expired_buffs.push(buff_key)
}
});
@ -370,9 +370,9 @@ impl<'a> System<'a> for Sys {
.abs()
< f32::EPSILON;
if infinite_damage_reduction {
for (id, buff) in buff_comp.buffs.iter() {
for (key, buff) in buff_comp.buffs.iter() {
if !buff.kind.is_buff() {
expired_buffs.push(*id);
expired_buffs.push(key);
}
}
}
@ -386,24 +386,24 @@ impl<'a> System<'a> for Sys {
let mut buff_kinds = buff_comp
.kinds
.iter()
.map(|(kind, ids)| (*kind, ids.clone()))
.collect::<Vec<(BuffKind, (Vec<BuffId>, Time))>>();
.filter_map(|(kind, keys)| keys.as_ref().map(|keys| (kind, keys.clone())))
.collect::<Vec<(BuffKind, (Vec<BuffKey>, Time))>>();
buff_kinds.sort_by_key(|(kind, _)| !kind.affects_subsequent_buffs());
for (buff_kind, (buff_ids, kind_start_time)) in buff_kinds.into_iter() {
let mut active_buff_ids = Vec::new();
for (buff_kind, (buff_keys, kind_start_time)) in buff_kinds.into_iter() {
let mut active_buff_keys = Vec::new();
if infinite_damage_reduction && !buff_kind.is_buff() {
continue;
}
if buff_kind.stacks() {
// Process all the buffs of this kind
active_buff_ids = buff_ids;
active_buff_keys = buff_keys;
} else {
// Only process the strongest of this buff kind
active_buff_ids.push(buff_ids[0]);
active_buff_keys.push(buff_keys[0]);
}
for buff_id in active_buff_ids.into_iter() {
if let Some(buff) = buff_comp.buffs.get(&buff_id) {
for buff_key in active_buff_keys.into_iter() {
if let Some(buff) = buff_comp.buffs.get(buff_key) {
// Skip the effect of buffs whose start delay hasn't expired.
if buff.start_time.0 > read_data.time.0 {
continue;
@ -434,7 +434,7 @@ impl<'a> System<'a> for Sys {
&mut server_emitter,
dt,
*read_data.time,
expired_buffs.contains(&buff_id),
expired_buffs.contains(&buff_key),
buff_comp,
);
}
@ -452,7 +452,7 @@ impl<'a> System<'a> for Sys {
if !expired_buffs.is_empty() {
server_emitter.emit(ServerEvent::Buff {
entity,
buff_change: BuffChange::RemoveById(expired_buffs),
buff_change: BuffChange::RemoveByKey(expired_buffs),
});
}

View File

@ -32,7 +32,7 @@ pub fn is_dead(entity: EcsEntity, read_data: &ReadData) -> bool {
pub fn is_invulnerable(entity: EcsEntity, read_data: &ReadData) -> bool {
let buffs = read_data.buffs.get(entity);
buffs.map_or(false, |b| b.kinds.contains_key(&BuffKind::Invulnerability))
buffs.map_or(false, |b| b.kinds[BuffKind::Invulnerability].is_some())
}
/// Gets alignment of owner if alignment given is `Owned`.
@ -204,7 +204,7 @@ impl<'a> AgentData<'a> {
read_data
.buffs
.get(*self.entity)
.map_or(false, |b| b.kinds.contains_key(&buff))
.map_or(false, |b| b.kinds[buff].is_some())
}
pub fn extract_ability(&self, input: AbilityInput) -> Option<AbilityData> {

View File

@ -1226,9 +1226,9 @@ pub fn handle_buff(server: &mut Server, entity: EcsEntity, buff_change: buff::Bu
buffs.insert(new_buff, *time);
}
},
BuffChange::RemoveById(ids) => {
for id in ids {
buffs.remove(id);
BuffChange::RemoveByKey(keys) => {
for key in keys {
buffs.remove(key);
}
},
BuffChange::RemoveByKind(kind) => {
@ -1244,8 +1244,8 @@ pub fn handle_buff(server: &mut Server, entity: EcsEntity, buff_change: buff::Bu
any_required,
none_required,
} => {
let mut ids_to_remove = Vec::new();
for (id, buff) in buffs.buffs.iter() {
let mut keys_to_remove = Vec::new();
for (key, buff) in buffs.buffs.iter() {
let mut required_met = true;
for required in &all_required {
if !buff.cat_ids.iter().any(|cat| cat == required) {
@ -1268,11 +1268,11 @@ pub fn handle_buff(server: &mut Server, entity: EcsEntity, buff_change: buff::Bu
}
}
if required_met && any_met && none_met {
ids_to_remove.push(*id);
keys_to_remove.push(key);
}
}
for id in ids_to_remove {
buffs.remove(id);
for key in keys_to_remove {
buffs.remove(key);
}
},
BuffChange::Refresh(kind) => {

View File

@ -213,7 +213,7 @@ fn react_if_on_fire(bdata: &mut BehaviorData) -> bool {
.read_data
.buffs
.get(*bdata.agent_data.entity)
.map_or(false, |b| b.kinds.contains_key(&BuffKind::Burning));
.map_or(false, |b| b.kinds[BuffKind::Burning].is_some());
if is_on_fire
&& bdata.agent_data.body.map_or(false, |b| b.is_humanoid())

View File

@ -137,7 +137,7 @@ pub fn localize_chat_message(
| BuffKind::Poisoned
| BuffKind::Parried
| BuffKind::PotionSickness
| BuffKind::Polymorphed(_) => {
| BuffKind::Polymorphed => {
tracing::error!("Player was killed by a debuff that doesn't do damage!");
"mysterious"
},

View File

@ -5191,7 +5191,7 @@ pub fn get_buff_image(buff: BuffKind, imgs: &Imgs) -> conrod_core::image::Id {
BuffKind::Poisoned => imgs.debuff_poisoned_0,
BuffKind::Parried => imgs.debuff_parried_0,
BuffKind::PotionSickness => imgs.debuff_potionsickness_0,
BuffKind::Polymorphed(_) => imgs.debuff_polymorphed,
BuffKind::Polymorphed => imgs.debuff_polymorphed,
}
}
@ -5232,7 +5232,7 @@ pub fn get_buff_title(buff: BuffKind, localized_strings: &Localization) -> Cow<s
BuffKind::Poisoned => localized_strings.get_msg("buff-title-poisoned"),
BuffKind::Parried => localized_strings.get_msg("buff-title-parried"),
BuffKind::PotionSickness => localized_strings.get_msg("buff-title-potionsickness"),
BuffKind::Polymorphed(_) => localized_strings.get_msg("buff-title-polymorphed"),
BuffKind::Polymorphed => localized_strings.get_msg("buff-title-polymorphed"),
}
}
@ -5277,7 +5277,7 @@ pub fn get_buff_desc(buff: BuffKind, data: BuffData, localized_strings: &Localiz
BuffKind::Poisoned => localized_strings.get_msg("buff-desc-poisoned"),
BuffKind::Parried => localized_strings.get_msg("buff-desc-parried"),
BuffKind::PotionSickness => localized_strings.get_msg("buff-desc-potionsickness"),
BuffKind::Polymorphed(_) => localized_strings.get_msg("buff-desc-polymorphed"),
BuffKind::Polymorphed => localized_strings.get_msg("buff-desc-polymorphed"),
}
}

View File

@ -205,7 +205,7 @@ pub fn consumable_desc(effects: &Effects, i18n: &Localization) -> Vec<String> {
| BuffKind::Fortitude
| BuffKind::Parried
| BuffKind::Reckless
| BuffKind::Polymorphed(_)
| BuffKind::Polymorphed
| BuffKind::Flame
| BuffKind::Frigid
| BuffKind::Lifesteal
@ -233,7 +233,7 @@ pub fn consumable_desc(effects: &Effects, i18n: &Localization) -> Vec<String> {
| BuffKind::IncreaseMaxHealth
| BuffKind::Invulnerability
| BuffKind::PotionSickness
| BuffKind::Polymorphed(_) => {
| BuffKind::Polymorphed => {
i18n.get_msg_ctx("buff-text-for_seconds", &i18n::fluent_args! {
"dur_secs" => dur_secs
})

View File

@ -1482,7 +1482,11 @@ impl ParticleMgr {
{
let pos = interp.map_or(pos.0, |i| i.pos);
for (buff_kind, buff_ids) in buffs.kinds.iter() {
for (buff_kind, buff_keys) in buffs
.kinds
.iter()
.filter_map(|(kind, keys)| keys.as_ref().map(|keys| (kind, keys)))
{
use buff::BuffKind;
match buff_kind {
BuffKind::Cursed | BuffKind::Burning => {
@ -1519,9 +1523,9 @@ impl ParticleMgr {
let mut multiplicity = 0;
// Only show particles for potion sickness at the beginning, after the
// drinking animation finishes
if buff_ids.0
if buff_keys.0
.iter()
.filter_map(|id| buffs.buffs.get(id))
.filter_map(|key| buffs.buffs.get(*key))
.any(|buff| {
matches!(buff.elapsed(Time(time)), dur if (1.0..=1.5).contains(&dur.0))
})
@ -1583,13 +1587,13 @@ impl ParticleMgr {
},
);
},
BuffKind::Polymorphed(_) => {
BuffKind::Polymorphed => {
let mut multiplicity = 0;
// Only show particles for polymorph at the beginning, after the
// drinking animation finishes
if buff_ids.0
if buff_keys.0
.iter()
.filter_map(|id| buffs.buffs.get(id))
.filter_map(|key| buffs.buffs.get(*key))
.any(|buff| {
matches!(buff.elapsed(Time(time)), dur if (0.1..=0.3).contains(&dur.0))
})