From 6ce1e21da38fe7364f04052f8cf4fd98cdf8317a Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 13 Apr 2023 16:18:09 +0800 Subject: [PATCH] 1 --- server/gameserver/ability.cc | 83 +++++++++++++++++++++++++++++++++++- server/gameserver/ability.h | 11 ++--- server/gameserver/types.h | 6 +++ 3 files changed, 94 insertions(+), 6 deletions(-) diff --git a/server/gameserver/ability.cc b/server/gameserver/ability.cc index f5c923b5..d879ea00 100644 --- a/server/gameserver/ability.cc +++ b/server/gameserver/ability.cc @@ -97,6 +97,28 @@ struct AttrRuduce } }; +struct AttrDirectPtr +{ + struct AttrDirect* data; + AttrDirectPtr(AttrDirect* data) { this->data = data; }; +}; + +struct AttrDirect +{ + list_head entry; + int attr_id; + float value; + std::shared_ptr ptr; + + AttrDirect(int attr_id, float value) + { + this->attr_id = attr_id; + this->value = value; + INIT_LIST_HEAD(&entry); + ptr = std::make_shared(this); + } +}; + Ability::Ability(CreatureWeakPtr owner) { for (auto& tuple : attr_abs_) { @@ -123,11 +145,14 @@ Ability::Ability(CreatureWeakPtr owner) std::get<0>(tuple) = 0.0f; INIT_LIST_HEAD(&std::get<1>(tuple)); } + for (auto& tuple : attr_direct_) { + std::get<0>(tuple) = 0.0f; + INIT_LIST_HEAD(&std::get<1>(tuple)); + } } void Ability::Clear() { - for (auto& tuple : attr_abs_) { std::get<0>(tuple) = 0.0f; while (!list_empty(&std::get<1>(tuple))) { @@ -196,6 +221,17 @@ void Ability::Clear() delete e; } } + for (auto& tuple : attr_direct_) { + std::get<0>(tuple) = 0.0f; + while (!list_empty(&std::get<1>(tuple))) { + AttrDirect* e = list_first_entry(&std::get<1>(tuple), + AttrDirect, + entry); + e->ptr->data = nullptr; + list_del_init(&e->entry); + delete e; + } + } switch_times_ = {}; immune_tags_.clear(); @@ -906,3 +942,48 @@ int Ability::GetSwitchTimes(int type) } return 0; } + +AttrDirectHandle Ability::AddAttrDirect(int attr_id, float value) +{ + if (IsValidHumanAttr(attr_id)) { + auto p = new AttrDirect(attr_id, value); + list_add_tail(&p->entry, &std::get<1>(attr_direct_[attr_id])); + RecalcAttrDirect(attr_id); + return p->ptr; + } + return AttrDirectHandle(); +} + +void Ability::RemoveAttrDirect(AttrDirectHandle handle) +{ + if (!handle.expired()) { + auto p = handle.lock(); + list_del_init(&p->data->entry); + RecalcAttrDirect(p->data->attr_id); + delete p->data; + } +} + +float Ability::GetAttrDirect(int attr_id) +{ + if (IsValidHumanAttr(attr_id)) { + return std::get<0>(attr_direct_[attr_id]); + } else { + return 0.0f; + } +} + +void Ability::RecalcAttrDirect(int attr_id) +{ + list_head* head = &std::get<1>(attr_direct_[attr_id]); + list_head* pos = nullptr; + list_head* next = nullptr; + float new_val = 0.0f; + list_for_each_safe(pos, next, head) { + AttrDirect* e = list_entry(pos, + AttrDirect, + entry); + new_val = std::max(e->value, new_val); + } + std::get<0>(attr_direct_[attr_id]) = new_val; +} diff --git a/server/gameserver/ability.h b/server/gameserver/ability.h index 6b598899..fcaf21a0 100644 --- a/server/gameserver/ability.h +++ b/server/gameserver/ability.h @@ -2,11 +2,6 @@ #include "attrdefine.h" #include "weakptr.h" -typedef std::weak_ptr AttrAdditionHandle; -typedef std::weak_ptr AttrRuduceHandle; -typedef std::weak_ptr AttrAbsHandle; -typedef std::weak_ptr AttrRateHandle; - class Ability { public: @@ -34,6 +29,10 @@ class Ability float GetAttrAddition(int attr_id); float GetAttrRuduce(int attr_id); + AttrDirectHandle AddAttrDirect(int attr_id, float value); + void RemoveAttrDirect(AttrDirectHandle handle); + float GetAttrDirect(int attr_id); + void GMDelBaseAttr(int type, int attr_id, int idx); void GMClearBaseAttr(int type); void GMDelGrowAttr(int type, int attr_id, int idx); @@ -45,6 +44,7 @@ private: void RecalcAttrRate(int attr_id); void RecalcAttrAddition(int attr_id); void RecalcAttrRuduce(int attr_id); + void RecalcAttrDirect(int attr_id); private: CreatureWeakPtr owner_; std::array, kHAT_End> attr_abs_ = {}; @@ -53,6 +53,7 @@ private: std::array, kHAT_End> attr_dec_ = {}; std::array, kHVAT_End - kHVAT_Begin> vattr_add_ = {}; std::array, kHVAT_End - kHVAT_Begin> vattr_dec_ = {}; + std::array, kHAT_End> attr_direct_ = {}; std::array switch_times_ = {}; std::map immune_tags_; diff --git a/server/gameserver/types.h b/server/gameserver/types.h index 2218bab0..a935d4f2 100644 --- a/server/gameserver/types.h +++ b/server/gameserver/types.h @@ -93,3 +93,9 @@ class IBullet virtual float GetHitRadius() = 0; virtual void ProcRequestBulletDmg(int shield_hit, int strength_wall_uniid, int target_uniid, const glm::vec3& pos) = 0; }; + +typedef std::weak_ptr AttrAdditionHandle; +typedef std::weak_ptr AttrRuduceHandle; +typedef std::weak_ptr AttrAbsHandle; +typedef std::weak_ptr AttrRateHandle; +typedef std::weak_ptr AttrDirectHandle;